1 /*
2 * Copyright (c) 2021-2022 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 "trans_server_proxy.h"
17
18 #include "iproxy_client.h"
19 #include "samgr_lite.h"
20 #include "serializer.h"
21 #include "softbus_adapter_file.h"
22 #include "softbus_adapter_mem.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
ProxyCallback(IOwner owner,int code,IpcIo * reply)33 static int ProxyCallback(IOwner owner, int code, IpcIo *reply)
34 {
35 if (code != SOFTBUS_OK) {
36 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "publish service callback error[%d].", code);
37 return SOFTBUS_ERR;
38 }
39
40 ReadInt32(reply, (int *)owner);
41 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "publish service return[%d].", *(int32_t*)owner);
42 return SOFTBUS_OK;
43 }
44
OpenSessionProxyCallback(IOwner owner,int code,IpcIo * reply)45 static int OpenSessionProxyCallback(IOwner owner, int code, IpcIo *reply)
46 {
47 if (code != SOFTBUS_OK) {
48 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "publish service callback error[%d].", code);
49 return SOFTBUS_ERR;
50 }
51 uint32_t size;
52 ReadUint32(reply, &size);
53 void *data = (void *)ReadBuffer(reply, size);
54 if (data == NULL) {
55 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "pop data is null.");
56 return SOFTBUS_ERR;
57 }
58 *(TransSerializer *)owner = *(TransSerializer *)data;
59 return SOFTBUS_OK;
60 }
61
TransServerProxyInit(void)62 int32_t TransServerProxyInit(void)
63 {
64 if (g_serverProxy != NULL) {
65 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "server proxy has initialized.");
66 return SOFTBUS_OK;
67 }
68
69 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "trans start get server proxy");
70 int32_t proxyInitCount = 0;
71 while (g_serverProxy == NULL) {
72 proxyInitCount++;
73 if (proxyInitCount == WAIT_SERVER_READY_INTERVAL_COUNT) {
74 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "trans get server proxy error");
75 return SOFTBUS_OK;
76 }
77 IUnknown *iUnknown = SAMGR_GetInstance()->GetDefaultFeatureApi(SOFTBUS_SERVICE);
78 if (iUnknown == NULL) {
79 SoftBusSleepMs(WAIT_SERVER_READY_INTERVAL);
80 continue;
81 }
82
83 int32_t ret = iUnknown->QueryInterface(iUnknown, CLIENT_PROXY_VER, (void **)&g_serverProxy);
84 if (ret != EC_SUCCESS || g_serverProxy == NULL) {
85 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "QueryInterface failed [%d]", ret);
86 SoftBusSleepMs(WAIT_SERVER_READY_INTERVAL);
87 continue;
88 }
89 }
90 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "trans get server proxy ok");
91 return SOFTBUS_OK;
92 }
93
TransServerProxyDeInit(void)94 void TransServerProxyDeInit(void)
95 {
96 g_serverProxy = NULL;
97 }
98
ServerIpcCreateSessionServer(const char * pkgName,const char * sessionName)99 int32_t ServerIpcCreateSessionServer(const char *pkgName, const char *sessionName)
100 {
101 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "ServerIpcCreateSessionServer");
102 if ((pkgName == NULL) || (sessionName == NULL)) {
103 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "Invalid param");
104 return SOFTBUS_INVALID_PARAM;
105 }
106
107 uint8_t data[MAX_SOFT_BUS_IPC_LEN] = {0};
108 IpcIo request = {0};
109 IpcIoInit(&request, data, MAX_SOFT_BUS_IPC_LEN, 0);
110 WriteString(&request, pkgName);
111 WriteString(&request, sessionName);
112
113 int32_t ret = SOFTBUS_ERR;
114 /* sync */
115 if (g_serverProxy == NULL) {
116 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "server proxy not init");
117 return SOFTBUS_NO_INIT;
118 }
119 int32_t ans = g_serverProxy->Invoke(g_serverProxy, SERVER_CREATE_SESSION_SERVER, &request, &ret, ProxyCallback);
120 if (ans != EC_SUCCESS) {
121 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "ServerIpcCreateSessionServer callback ret [%d]", ret);
122 return SOFTBUS_ERR;
123 }
124 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "ServerIpcCreateSessionServer succ");
125 return ret;
126 }
127
ServerIpcRemoveSessionServer(const char * pkgName,const char * sessionName)128 int32_t ServerIpcRemoveSessionServer(const char *pkgName, const char *sessionName)
129 {
130 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "ServerIpcRemoveSessionServer");
131 if ((pkgName == NULL) || (sessionName == NULL)) {
132 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "Invalid param");
133 return SOFTBUS_INVALID_PARAM;
134 }
135
136 uint8_t data[MAX_SOFT_BUS_IPC_LEN] = {0};
137 IpcIo request = {0};
138 IpcIoInit(&request, data, MAX_SOFT_BUS_IPC_LEN, 0);
139 WriteString(&request, pkgName);
140 WriteString(&request, sessionName);
141
142 int32_t ret = SOFTBUS_ERR;
143 /* sync */
144 if (g_serverProxy == NULL) {
145 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "server proxy not init");
146 return SOFTBUS_NO_INIT;
147 }
148 int32_t ans = g_serverProxy->Invoke(g_serverProxy, SERVER_REMOVE_SESSION_SERVER, &request, &ret, ProxyCallback);
149 if (ans != EC_SUCCESS) {
150 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "ServerIpcRemoveSessionServer callback ret [%d]", ret);
151 return SOFTBUS_TRANS_PROXY_INVOKE_FAILED;
152 }
153 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "ServerIpcRemoveSessionServer");
154 return ret;
155 }
156
ServerIpcOpenSession(const SessionParam * param,TransInfo * info)157 int32_t ServerIpcOpenSession(const SessionParam *param, TransInfo *info)
158 {
159 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "ServerIpcOpenSession");
160
161 uint8_t data[MAX_SOFT_BUS_IPC_LEN] = {0};
162 IpcIo request = {0};
163 IpcIoInit(&request, data, MAX_SOFT_BUS_IPC_LEN, 0);
164 WriteString(&request, param->sessionName);
165 WriteString(&request, param->peerSessionName);
166 WriteString(&request, param->peerDeviceId);
167 WriteString(&request, param->groupId);
168 bool value = WriteRawData(&request, (void*)param->attr, sizeof(SessionAttribute));
169 if (!value) {
170 return SOFTBUS_TRANS_PROXY_WRITERAWDATA_FAILED;
171 }
172
173 TransSerializer transSerializer;
174 transSerializer.ret = SOFTBUS_ERR;
175 /* sync */
176 if (g_serverProxy == NULL) {
177 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "server proxy not init");
178 return SOFTBUS_NO_INIT;
179 }
180 int32_t ans = g_serverProxy->Invoke(g_serverProxy, SERVER_OPEN_SESSION, &request,
181 &transSerializer, OpenSessionProxyCallback);
182 if (ans != EC_SUCCESS) {
183 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "ServerIpcOpenSession callback ret [%d]", transSerializer.ret);
184 return SOFTBUS_TRANS_PROXY_INVOKE_FAILED;
185 }
186 info->channelId = transSerializer.transInfo.channelId;
187 info->channelType = transSerializer.transInfo.channelType;
188 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "ServerIpcOpenSession");
189 return transSerializer.ret;
190 }
191
ServerIpcOpenAuthSession(const char * sessionName,const ConnectionAddr * addrInfo)192 int32_t ServerIpcOpenAuthSession(const char *sessionName, const ConnectionAddr *addrInfo)
193 {
194 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "ServerIpcOpenAuthSession begin");
195
196 uint8_t data[MAX_SOFT_BUS_IPC_LEN] = {0};
197 IpcIo request = {0};
198 IpcIoInit(&request, data, MAX_SOFT_BUS_IPC_LEN, 0);
199 WriteString(&request, sessionName);
200 bool value = WriteRawData(&request, (void*)addrInfo, sizeof(ConnectionAddr));
201 if (!value) {
202 return SOFTBUS_ERR;
203 }
204
205 int32_t ret = SOFTBUS_ERR;
206 if (g_serverProxy == NULL) {
207 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "server proxy not init");
208 return SOFTBUS_NO_INIT;
209 }
210 int32_t ans = g_serverProxy->Invoke(g_serverProxy, SERVER_OPEN_AUTH_SESSION, &request, &ret, ProxyCallback);
211 if (ans != EC_SUCCESS) {
212 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "ServerIpcOpenAuthSession callback ret [%d]", ret);
213 return SOFTBUS_ERR;
214 }
215 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "ServerIpcOpenAuthSession end");
216 return ret;
217 }
218
ServerIpcNotifyAuthSuccess(int32_t channelId,int32_t channelType)219 int32_t ServerIpcNotifyAuthSuccess(int32_t channelId, int32_t channelType)
220 {
221 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "ServerIpcNotifyAuthSuccess begin");
222
223 uint8_t data[MAX_SOFT_BUS_IPC_LEN] = {0};
224 IpcIo request = {0};
225 IpcIoInit(&request, data, MAX_SOFT_BUS_IPC_LEN, 0);
226 WriteInt32(&request, channelId);
227 WriteInt32(&request, channelType);
228 int32_t ret = SOFTBUS_ERR;
229 if (g_serverProxy == NULL) {
230 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "server proxy not init");
231 return SOFTBUS_NO_INIT;
232 }
233 int32_t ans = g_serverProxy->Invoke(g_serverProxy, SERVER_NOTIFY_AUTH_SUCCESS, &request, &ret, ProxyCallback);
234 if (ans != EC_SUCCESS) {
235 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "ServerIpcNotifyAuthSuccess callback ret [%d]", ret);
236 return SOFTBUS_ERR;
237 }
238 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "ServerIpcNotifyAuthSuccess end");
239 return ret;
240 }
241
ServerIpcCloseChannel(int32_t channelId,int32_t channelType)242 int32_t ServerIpcCloseChannel(int32_t channelId, int32_t channelType)
243 {
244 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "ServerIpcCloseSession");
245 uint8_t data[MAX_SOFT_BUS_IPC_LEN] = {0};
246 IpcIo request = {0};
247 IpcIoInit(&request, data, MAX_SOFT_BUS_IPC_LEN, 0);
248 WriteInt32(&request, channelId);
249 WriteInt32(&request, channelType);
250
251 int32_t ret = SOFTBUS_ERR;
252 /* sync */
253 if (g_serverProxy == NULL) {
254 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "server proxy not init");
255 return SOFTBUS_NO_INIT;
256 }
257 int32_t ans = g_serverProxy->Invoke(g_serverProxy, SERVER_CLOSE_CHANNEL, &request, &ret, ProxyCallback);
258 if (ans != EC_SUCCESS) {
259 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "ServerIpcCloseSession callback ret [%d]", ret);
260 return SOFTBUS_ERR;
261 }
262 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "ServerIpcCloseSession");
263 return ret;
264 }
265
ServerIpcSendMessage(int32_t channelId,int32_t channelType,const void * data,uint32_t len,int32_t msgType)266 int32_t ServerIpcSendMessage(int32_t channelId, int32_t channelType, const void *data, uint32_t len, int32_t msgType)
267 {
268 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "ServerIpcSendMessage");
269
270 uint32_t ipcDataLen = len + MAX_SOFT_BUS_IPC_LEN;
271 uint8_t *ipcData = (uint8_t *)SoftBusCalloc(ipcDataLen);
272 if (ipcData == NULL) {
273 SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "ServerIpcSendMessage malloc failed!");
274 return SOFTBUS_MALLOC_ERR;
275 }
276
277 IpcIo request = {0};
278 IpcIoInit(&request, ipcData, ipcDataLen, 0);
279 WriteInt32(&request, channelId);
280 WriteInt32(&request, channelType);
281 WriteInt32(&request, msgType);
282 WriteUint32(&request, len);
283 WriteBuffer(&request, data, len);
284
285 int32_t ret = SOFTBUS_ERR;
286 /* sync */
287 if (g_serverProxy == NULL) {
288 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "server proxy not init");
289 SoftBusFree(ipcData);
290 return SOFTBUS_NO_INIT;
291 }
292 int32_t ans = g_serverProxy->Invoke(g_serverProxy, SERVER_SESSION_SENDMSG, &request, &ret, ProxyCallback);
293 SoftBusFree(ipcData);
294 if (ans != EC_SUCCESS) {
295 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "ServerIpcSendMessage callback ret [%d]", ret);
296 return SOFTBUS_ERR;
297 }
298 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "ServerIpcSendMessage");
299 return ret;
300 }
301
ServerIpcQosReport(int32_t channelId,int32_t chanType,int32_t appType,int32_t quality)302 int32_t ServerIpcQosReport(int32_t channelId, int32_t chanType, int32_t appType, int32_t quality)
303 {
304 (void)channelId;
305 (void)chanType;
306 (void)appType;
307 (void)quality;
308 return SOFTBUS_NOT_IMPLEMENT;
309 }
310
ServerIpcGrantPermission(int uid,int pid,const char * sessionName)311 int32_t ServerIpcGrantPermission(int uid, int pid, const char *sessionName)
312 {
313 (void)uid;
314 (void)pid;
315 (void)sessionName;
316 return SOFTBUS_NOT_IMPLEMENT;
317 }
318
ServerIpcRemovePermission(const char * sessionName)319 int32_t ServerIpcRemovePermission(const char *sessionName)
320 {
321 (void)sessionName;
322 return SOFTBUS_NOT_IMPLEMENT;
323 }
324
ServerIpcStreamStats(int32_t channelId,int32_t channelType,const StreamSendStats * data)325 int32_t ServerIpcStreamStats(int32_t channelId, int32_t channelType, const StreamSendStats *data)
326 {
327 (void)channelId;
328 (void)channelType;
329 (void)data;
330 return SOFTBUS_NOT_IMPLEMENT;
331 }
332
ServerIpcRippleStats(int32_t channelId,int32_t channelType,const TrafficStats * data)333 int32_t ServerIpcRippleStats(int32_t channelId, int32_t channelType, const TrafficStats *data)
334 {
335 (void)channelId;
336 (void)channelType;
337 (void)data;
338 return SOFTBUS_NOT_IMPLEMENT;
339 }