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_serializer.h"
19 #include "iproxy_client.h"
20 #include "samgr_lite.h"
21 #include "serializer.h"
22 #include "softbus_adapter_file.h"
23 #include "softbus_adapter_timer.h"
24 #include "softbus_def.h"
25 #include "softbus_errcode.h"
26 #include "softbus_ipc_def.h"
27 #include "softbus_log.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 SoftBusLog(SOFTBUS_LOG_DISC, SOFTBUS_LOG_INFO, "server proxy has initialized.");
37 return SOFTBUS_OK;
38 }
39
40 SoftBusLog(SOFTBUS_LOG_DISC, SOFTBUS_LOG_INFO, "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 SoftBusLog(SOFTBUS_LOG_DISC, SOFTBUS_LOG_ERROR, "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 SoftBusLog(SOFTBUS_LOG_DISC, SOFTBUS_LOG_ERROR, "QueryInterface failed [%d]", ret);
57 SoftBusSleepMs(WAIT_SERVER_READY_INTERVAL);
58 continue;
59 }
60 }
61 SoftBusLog(SOFTBUS_LOG_DISC, SOFTBUS_LOG_INFO, "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 SoftBusLog(SOFTBUS_LOG_DISC, SOFTBUS_LOG_INFO, "publish service ipc client push.");
73 if (pkgName == NULL || info == NULL) {
74 SoftBusLog(SOFTBUS_LOG_DISC, SOFTBUS_LOG_ERROR, "Invalid param");
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 IpcIoPushString(&request, pkgName);
85 DiscSerializer serializer = {
86 .dataLen = info->dataLen,
87 .freq = info->freq,
88 .medium = info->medium,
89 .mode = info->mode,
90 .id.publishId = info->publishId
91 };
92 PublishSerializer publishSerializer = {
93 .commonSerializer = serializer
94 };
95 IpcIoPushFlatObj(&request, (void*)&publishSerializer, sizeof(PublishSerializer));
96 IpcIoPushString(&request, info->capability);
97 if (info->dataLen != 0) {
98 IpcIoPushString(&request, (const char *)(info->capabilityData));
99 }
100 /* asynchronous invocation */
101 int32_t ans = g_serverProxy->Invoke(g_serverProxy, SERVER_PUBLISH_SERVICE, &request, NULL, NULL);
102 if (ans != SOFTBUS_OK) {
103 SoftBusLog(SOFTBUS_LOG_DISC, SOFTBUS_LOG_ERROR, "publish service invoke failed[%d].", ans);
104 return SOFTBUS_ERR;
105 }
106 return SOFTBUS_OK;
107 }
108
ServerIpcUnPublishService(const char * pkgName,int publishId)109 int ServerIpcUnPublishService(const char *pkgName, int publishId)
110 {
111 SoftBusLog(SOFTBUS_LOG_DISC, SOFTBUS_LOG_INFO, "unpublish service ipc client push.");
112 if (pkgName == NULL) {
113 SoftBusLog(SOFTBUS_LOG_DISC, SOFTBUS_LOG_ERROR, "Invalid param");
114 return SOFTBUS_INVALID_PARAM;
115 }
116 if (g_serverProxy == NULL) {
117 return SOFTBUS_ERR;
118 }
119
120 uint8_t data[MAX_SOFT_BUS_IPC_LEN] = {0};
121 IpcIo request = {0};
122 IpcIoInit(&request, data, MAX_SOFT_BUS_IPC_LEN, 0);
123 IpcIoPushString(&request, pkgName);
124 IpcIoPushInt32(&request, publishId);
125 /* asynchronous invocation */
126 int32_t ans = g_serverProxy->Invoke(g_serverProxy, SERVER_UNPUBLISH_SERVICE, &request, NULL, NULL);
127 if (ans != SOFTBUS_OK) {
128 SoftBusLog(SOFTBUS_LOG_DISC, SOFTBUS_LOG_ERROR, "unpublish service invoke failed[%d].", ans);
129 return SOFTBUS_ERR;
130 }
131 return SOFTBUS_OK;
132 }
133
ServerIpcStartDiscovery(const char * pkgName,const SubscribeInfo * info)134 int ServerIpcStartDiscovery(const char *pkgName, const SubscribeInfo *info)
135 {
136 SoftBusLog(SOFTBUS_LOG_DISC, SOFTBUS_LOG_INFO, "start discovery ipc client push.");
137 if (pkgName == NULL || info == NULL) {
138 SoftBusLog(SOFTBUS_LOG_DISC, SOFTBUS_LOG_ERROR, "Invalid param");
139 return SOFTBUS_INVALID_PARAM;
140 }
141 if (g_serverProxy == NULL) {
142 return SOFTBUS_ERR;
143 }
144
145 uint8_t data[MAX_SOFT_BUS_IPC_LEN] = {0};
146 IpcIo request = {0};
147 IpcIoInit(&request, data, MAX_SOFT_BUS_IPC_LEN, 0);
148 IpcIoPushString(&request, pkgName);
149 DiscSerializer serializer = {
150 .dataLen = info->dataLen,
151 .freq = info->freq,
152 .medium = info->medium,
153 .mode = info->mode,
154 .id.subscribeId = info->subscribeId
155 };
156 SubscribeSerializer subscribeSerializer = {
157 .commonSerializer = serializer,
158 .isSameAccount = info->isSameAccount,
159 .isWakeRemote = info->isWakeRemote
160 };
161 IpcIoPushFlatObj(&request, (void*)&subscribeSerializer, sizeof(SubscribeSerializer));
162 IpcIoPushString(&request, info->capability);
163 if (info->dataLen != 0) {
164 IpcIoPushString(&request, (const char *)(info->capabilityData));
165 }
166 /* asynchronous invocation */
167 int32_t ans = g_serverProxy->Invoke(g_serverProxy, SERVER_START_DISCOVERY, &request, NULL, NULL);
168 if (ans != SOFTBUS_OK) {
169 SoftBusLog(SOFTBUS_LOG_DISC, SOFTBUS_LOG_ERROR, "start discovery invoke failed[%d].", ans);
170 return SOFTBUS_ERR;
171 }
172 return SOFTBUS_OK;
173 }
174
ServerIpcStopDiscovery(const char * pkgName,int subscribeId)175 int ServerIpcStopDiscovery(const char *pkgName, int subscribeId)
176 {
177 SoftBusLog(SOFTBUS_LOG_DISC, SOFTBUS_LOG_INFO, "stop discovery ipc client push.");
178 if (pkgName == NULL) {
179 SoftBusLog(SOFTBUS_LOG_DISC, SOFTBUS_LOG_ERROR, "Invalid param");
180 return SOFTBUS_INVALID_PARAM;
181 }
182 if (g_serverProxy == NULL) {
183 return SOFTBUS_ERR;
184 }
185
186 uint8_t data[MAX_SOFT_BUS_IPC_LEN] = {0};
187 IpcIo request = {0};
188 IpcIoInit(&request, data, MAX_SOFT_BUS_IPC_LEN, 0);
189 IpcIoPushString(&request, pkgName);
190 IpcIoPushInt32(&request, subscribeId);
191 /* asynchronous invocation */
192 int32_t ans = g_serverProxy->Invoke(g_serverProxy, SERVER_STOP_DISCOVERY, &request, NULL, NULL);
193 if (ans != SOFTBUS_OK) {
194 SoftBusLog(SOFTBUS_LOG_DISC, SOFTBUS_LOG_ERROR, "stop discovery invoke failed[%d].", ans);
195 return SOFTBUS_ERR;
196 }
197 return SOFTBUS_OK;
198 }
199