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_server_ipc_interface_code.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 WriteString(&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 bool ret = WriteRawData(&request, (void*)&publishSerializer, sizeof(PublishSerializer));
96 if (!ret) {
97 return SOFTBUS_ERR;
98 }
99 WriteString(&request, info->capability);
100 if (info->dataLen != 0) {
101 WriteString(&request, (const char *)(info->capabilityData));
102 }
103 /* asynchronous invocation */
104 int32_t ans = g_serverProxy->Invoke(g_serverProxy, SERVER_PUBLISH_SERVICE, &request, NULL, NULL);
105 if (ans != SOFTBUS_OK) {
106 SoftBusLog(SOFTBUS_LOG_DISC, SOFTBUS_LOG_ERROR, "publish service invoke failed[%d].", ans);
107 return SOFTBUS_ERR;
108 }
109 return SOFTBUS_OK;
110 }
111
ServerIpcUnPublishService(const char * pkgName,int publishId)112 int ServerIpcUnPublishService(const char *pkgName, int publishId)
113 {
114 SoftBusLog(SOFTBUS_LOG_DISC, SOFTBUS_LOG_INFO, "unpublish service ipc client push.");
115 if (pkgName == NULL) {
116 SoftBusLog(SOFTBUS_LOG_DISC, SOFTBUS_LOG_ERROR, "Invalid param");
117 return SOFTBUS_INVALID_PARAM;
118 }
119 if (g_serverProxy == NULL) {
120 return SOFTBUS_ERR;
121 }
122
123 uint8_t data[MAX_SOFT_BUS_IPC_LEN] = {0};
124 IpcIo request = {0};
125 IpcIoInit(&request, data, MAX_SOFT_BUS_IPC_LEN, 0);
126 WriteString(&request, pkgName);
127 WriteInt32(&request, publishId);
128 /* asynchronous invocation */
129 int32_t ans = g_serverProxy->Invoke(g_serverProxy, SERVER_UNPUBLISH_SERVICE, &request, NULL, NULL);
130 if (ans != SOFTBUS_OK) {
131 SoftBusLog(SOFTBUS_LOG_DISC, SOFTBUS_LOG_ERROR, "unpublish service invoke failed[%d].", ans);
132 return SOFTBUS_ERR;
133 }
134 return SOFTBUS_OK;
135 }
136
ServerIpcStartDiscovery(const char * pkgName,const SubscribeInfo * info)137 int ServerIpcStartDiscovery(const char *pkgName, const SubscribeInfo *info)
138 {
139 SoftBusLog(SOFTBUS_LOG_DISC, SOFTBUS_LOG_INFO, "start discovery ipc client push.");
140 if (pkgName == NULL || info == NULL) {
141 SoftBusLog(SOFTBUS_LOG_DISC, SOFTBUS_LOG_ERROR, "Invalid param");
142 return SOFTBUS_INVALID_PARAM;
143 }
144 if (g_serverProxy == NULL) {
145 return SOFTBUS_ERR;
146 }
147
148 uint8_t data[MAX_SOFT_BUS_IPC_LEN] = {0};
149 IpcIo request = {0};
150 IpcIoInit(&request, data, MAX_SOFT_BUS_IPC_LEN, 0);
151 WriteString(&request, pkgName);
152 DiscSerializer serializer = {
153 .dataLen = info->dataLen,
154 .freq = info->freq,
155 .medium = info->medium,
156 .mode = info->mode,
157 .id.subscribeId = info->subscribeId
158 };
159 SubscribeSerializer subscribeSerializer = {
160 .commonSerializer = serializer,
161 .isSameAccount = info->isSameAccount,
162 .isWakeRemote = info->isWakeRemote
163 };
164 bool ret = WriteRawData(&request, (void*)&subscribeSerializer, sizeof(SubscribeSerializer));
165 if (!ret) {
166 return SOFTBUS_ERR;
167 }
168 WriteString(&request, info->capability);
169 if (info->dataLen != 0) {
170 WriteString(&request, (const char *)(info->capabilityData));
171 }
172 /* asynchronous invocation */
173 int32_t ans = g_serverProxy->Invoke(g_serverProxy, SERVER_START_DISCOVERY, &request, NULL, NULL);
174 if (ans != SOFTBUS_OK) {
175 SoftBusLog(SOFTBUS_LOG_DISC, SOFTBUS_LOG_ERROR, "start discovery invoke failed[%d].", ans);
176 return SOFTBUS_ERR;
177 }
178 return SOFTBUS_OK;
179 }
180
ServerIpcStopDiscovery(const char * pkgName,int subscribeId)181 int ServerIpcStopDiscovery(const char *pkgName, int subscribeId)
182 {
183 SoftBusLog(SOFTBUS_LOG_DISC, SOFTBUS_LOG_INFO, "stop discovery ipc client push.");
184 if (pkgName == NULL) {
185 SoftBusLog(SOFTBUS_LOG_DISC, SOFTBUS_LOG_ERROR, "Invalid param");
186 return SOFTBUS_INVALID_PARAM;
187 }
188 if (g_serverProxy == NULL) {
189 return SOFTBUS_ERR;
190 }
191
192 uint8_t data[MAX_SOFT_BUS_IPC_LEN] = {0};
193 IpcIo request = {0};
194 IpcIoInit(&request, data, MAX_SOFT_BUS_IPC_LEN, 0);
195 WriteString(&request, pkgName);
196 WriteInt32(&request, subscribeId);
197 /* asynchronous invocation */
198 int32_t ans = g_serverProxy->Invoke(g_serverProxy, SERVER_STOP_DISCOVERY, &request, NULL, NULL);
199 if (ans != SOFTBUS_OK) {
200 SoftBusLog(SOFTBUS_LOG_DISC, SOFTBUS_LOG_ERROR, "stop discovery invoke failed[%d].", ans);
201 return SOFTBUS_ERR;
202 }
203 return SOFTBUS_OK;
204 }
205