• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "disc_server_proxy.h"
17 
18 #include "disc_log.h"
19 #include "disc_serializer.h"
20 #include "iproxy_client.h"
21 #include "samgr_lite.h"
22 #include "serializer.h"
23 #include "softbus_adapter_file.h"
24 #include "softbus_adapter_timer.h"
25 #include "softbus_def.h"
26 #include "softbus_errcode.h"
27 #include "softbus_server_ipc_interface_code.h"
28 
29 #define WAIT_SERVER_READY_INTERVAL_COUNT 50
30 
31 static IClientProxy *g_serverProxy = NULL;
32 
DiscServerProxyInit(void)33 int32_t DiscServerProxyInit(void)
34 {
35     if (g_serverProxy != NULL) {
36         DISC_LOGI(DISC_INIT, "server proxy has initialized.");
37         return SOFTBUS_OK;
38     }
39 
40     DISC_LOGI(DISC_INIT, "disc start get server proxy");
41     int32_t proxyInitCount = 0;
42     while (g_serverProxy == NULL) {
43         proxyInitCount++;
44         if (proxyInitCount == WAIT_SERVER_READY_INTERVAL_COUNT) {
45             DISC_LOGE(DISC_INIT, "disc get server proxy error");
46             return SOFTBUS_ERR;
47         }
48         IUnknown *iUnknown = SAMGR_GetInstance()->GetDefaultFeatureApi(SOFTBUS_SERVICE);
49         if (iUnknown == NULL) {
50             SoftBusSleepMs(WAIT_SERVER_READY_INTERVAL);
51             continue;
52         }
53 
54         int32_t ret = iUnknown->QueryInterface(iUnknown, CLIENT_PROXY_VER, (void **)&g_serverProxy);
55         if (ret != EC_SUCCESS || g_serverProxy == NULL) {
56             DISC_LOGE(DISC_INIT, "QueryInterface failed. ret=%{public}d", ret);
57             SoftBusSleepMs(WAIT_SERVER_READY_INTERVAL);
58             continue;
59         }
60     }
61     DISC_LOGI(DISC_INIT, "disc get server proxy ok");
62     return SOFTBUS_OK;
63 }
64 
DiscServerProxyDeInit(void)65 void DiscServerProxyDeInit(void)
66 {
67     g_serverProxy = NULL;
68 }
69 
ServerIpcPublishService(const char * pkgName,const PublishInfo * info)70 int ServerIpcPublishService(const char *pkgName, const PublishInfo *info)
71 {
72     DISC_LOGI(DISC_CONTROL, "publish service ipc client push.");
73     if (pkgName == NULL || info == NULL) {
74         DISC_LOGE(DISC_SDK, "Invalid param:null");
75         return SOFTBUS_INVALID_PARAM;
76     }
77     if (g_serverProxy == NULL) {
78         return SOFTBUS_ERR;
79     }
80 
81     uint8_t data[MAX_SOFT_BUS_IPC_LEN] = {0};
82     IpcIo request = {0};
83     IpcIoInit(&request, data, MAX_SOFT_BUS_IPC_LEN, 0);
84     bool ret = WriteString(&request, pkgName);
85     if (!ret) {
86         DISC_LOGE(DISC_SDK, "Write pkgName failed");
87         return SOFTBUS_ERR;
88     }
89     DiscSerializer serializer = {
90         .dataLen = info->dataLen,
91         .freq = info->freq,
92         .medium = info->medium,
93         .mode = info->mode,
94         .id.publishId = info->publishId
95     };
96     PublishSerializer publishSerializer = {
97         .commonSerializer = serializer
98     };
99     ret = WriteRawData(&request, (void*)&publishSerializer, sizeof(PublishSerializer));
100     if (!ret) {
101         DISC_LOGE(DISC_SDK, "Write publish serializer failed");
102         return SOFTBUS_ERR;
103     }
104 
105     ret = WriteString(&request, info->capability);
106     if (!ret) {
107         DISC_LOGE(DISC_SDK, "Write capability failed");
108         return SOFTBUS_ERR;
109     }
110     if (info->dataLen != 0) {
111         ret = WriteString(&request, (const char *)(info->capabilityData));
112         if (!ret) {
113             DISC_LOGE(DISC_SDK, "Write capability Data failed");
114             return SOFTBUS_ERR;
115         }
116     }
117     /* asynchronous invocation */
118     int32_t ans = g_serverProxy->Invoke(g_serverProxy, SERVER_PUBLISH_SERVICE, &request, NULL, NULL);
119     if (ans != SOFTBUS_OK) {
120         DISC_LOGE(DISC_CONTROL, "publish service invoke failed. ans=%{public}d", ans);
121         return SOFTBUS_ERR;
122     }
123     return SOFTBUS_OK;
124 }
125 
ServerIpcUnPublishService(const char * pkgName,int publishId)126 int ServerIpcUnPublishService(const char *pkgName, int publishId)
127 {
128     DISC_LOGI(DISC_CONTROL, "unpublish service ipc client push.");
129     if (pkgName == NULL) {
130         DISC_LOGE(DISC_SDK, "Invalid param:null");
131         return SOFTBUS_INVALID_PARAM;
132     }
133     if (g_serverProxy == NULL) {
134         return SOFTBUS_NO_INIT;
135     }
136 
137     uint8_t data[MAX_SOFT_BUS_IPC_LEN] = {0};
138     IpcIo request = {0};
139     IpcIoInit(&request, data, MAX_SOFT_BUS_IPC_LEN, 0);
140     bool ret = WriteString(&request, pkgName);
141     if (!ret) {
142         DISC_LOGE(DISC_SDK, "Write pkgName failed");
143         return SOFTBUS_ERR;
144     }
145 
146     ret = WriteInt32(&request, publishId);
147     if (!ret) {
148         DISC_LOGE(DISC_SDK, "Write publishId failed");
149         return SOFTBUS_ERR;
150     }
151     /* asynchronous invocation */
152     int32_t ans = g_serverProxy->Invoke(g_serverProxy, SERVER_UNPUBLISH_SERVICE, &request, NULL, NULL);
153     if (ans != SOFTBUS_OK) {
154         DISC_LOGE(DISC_CONTROL, "unpublish service invoke failed. ans=%{public}d", ans);
155         return SOFTBUS_ERR;
156     }
157     return SOFTBUS_OK;
158 }
159 
ServerIpcStartDiscovery(const char * pkgName,const SubscribeInfo * info)160 int ServerIpcStartDiscovery(const char *pkgName, const SubscribeInfo *info)
161 {
162     DISC_LOGI(DISC_CONTROL, "start discovery ipc client push.");
163     if (pkgName == NULL || info == NULL) {
164         DISC_LOGE(DISC_SDK, "Invalid param:null");
165         return SOFTBUS_INVALID_PARAM;
166     }
167     if (g_serverProxy == NULL) {
168         return SOFTBUS_NO_INIT;
169     }
170 
171     uint8_t data[MAX_SOFT_BUS_IPC_LEN] = {0};
172     IpcIo request = {0};
173     IpcIoInit(&request, data, MAX_SOFT_BUS_IPC_LEN, 0);
174     bool ret = WriteString(&request, pkgName);
175     if (!ret) {
176         DISC_LOGE(DISC_SDK, "Write pkgName failed");
177         return SOFTBUS_ERR;
178     }
179     DiscSerializer serializer = {
180         .dataLen = info->dataLen,
181         .freq = info->freq,
182         .medium = info->medium,
183         .mode = info->mode,
184         .id.subscribeId = info->subscribeId
185     };
186     SubscribeSerializer subscribeSerializer = {
187         .commonSerializer = serializer,
188         .isSameAccount = info->isSameAccount,
189         .isWakeRemote = info->isWakeRemote
190     };
191     ret = WriteRawData(&request, (void*)&subscribeSerializer, sizeof(SubscribeSerializer));
192     if (!ret) {
193         DISC_LOGE(DISC_SDK, "Write SubscribeSerializer failed");
194         return SOFTBUS_ERR;
195     }
196     ret = WriteString(&request, info->capability);
197     if (!ret) {
198         DISC_LOGE(DISC_SDK, "Write capability failed");
199         return SOFTBUS_ERR;
200     }
201     if (info->dataLen != 0) {
202         ret = WriteString(&request, (const char *)(info->capabilityData));
203         if (!ret) {
204             DISC_LOGE(DISC_SDK, "Write capabilityData failed");
205             return SOFTBUS_ERR;
206         }
207     }
208     /* asynchronous invocation */
209     int32_t ans = g_serverProxy->Invoke(g_serverProxy, SERVER_START_DISCOVERY, &request, NULL, NULL);
210     if (ans != SOFTBUS_OK) {
211         DISC_LOGE(DISC_CONTROL, "start discovery invoke failed. ans=%{public}d", ans);
212         return SOFTBUS_ERR;
213     }
214     return SOFTBUS_OK;
215 }
216 
ServerIpcStopDiscovery(const char * pkgName,int subscribeId)217 int ServerIpcStopDiscovery(const char *pkgName, int subscribeId)
218 {
219     DISC_LOGI(DISC_SDK, "stop discovery ipc client push.");
220     if (pkgName == NULL) {
221         DISC_LOGE(DISC_SDK, "Invalid param:null");
222         return SOFTBUS_INVALID_PARAM;
223     }
224     if (g_serverProxy == NULL) {
225         return SOFTBUS_NO_INIT;
226     }
227 
228     uint8_t data[MAX_SOFT_BUS_IPC_LEN] = {0};
229     IpcIo request = {0};
230     IpcIoInit(&request, data, MAX_SOFT_BUS_IPC_LEN, 0);
231     bool ret = WriteString(&request, pkgName);
232     if (!ret) {
233         DISC_LOGE(DISC_SDK, "Write pkgName failed");
234         return SOFTBUS_ERR;
235     }
236     ret = WriteInt32(&request, subscribeId);
237     if (!ret) {
238         DISC_LOGE(DISC_SDK, "Write subscribeId failed");
239         return SOFTBUS_ERR;
240     }
241     /* asynchronous invocation */
242     int32_t ans = g_serverProxy->Invoke(g_serverProxy, SERVER_STOP_DISCOVERY, &request, NULL, NULL);
243     if (ans != SOFTBUS_OK) {
244         DISC_LOGE(DISC_CONTROL, "stop discovery invoke failed. ans=%{public}d", ans);
245         return SOFTBUS_ERR;
246     }
247     return SOFTBUS_OK;
248 }