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 "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_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
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 *(int32_t*)owner = IpcIoPopInt32(reply);
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
53 void *data = IpcIoPopFlatObj(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 IpcIoPushString(&request, pkgName);
111 IpcIoPushString(&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 IpcIoPushString(&request, pkgName);
140 IpcIoPushString(&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_ERR;
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 IpcIoPushString(&request, param->sessionName);
165 IpcIoPushString(&request, param->peerSessionName);
166 IpcIoPushString(&request, param->peerDeviceId);
167 IpcIoPushString(&request, param->groupId);
168 IpcIoPushFlatObj(&request, (void*)param->attr, sizeof(SessionAttribute));
169
170 TransSerializer transSerializer;
171 transSerializer.ret = SOFTBUS_ERR;
172 /* sync */
173 if (g_serverProxy == NULL) {
174 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "server proxy not init");
175 return SOFTBUS_NO_INIT;
176 }
177 int32_t ans = g_serverProxy->Invoke(g_serverProxy, SERVER_OPEN_SESSION, &request,
178 &transSerializer, OpenSessionProxyCallback);
179 if (ans != EC_SUCCESS) {
180 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "ServerIpcOpenSession callback ret [%d]", transSerializer.ret);
181 return SOFTBUS_ERR;
182 }
183 info->channelId = transSerializer.transInfo.channelId;
184 info->channelType = transSerializer.transInfo.channelType;
185 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "ServerIpcOpenSession");
186 return transSerializer.ret;
187 }
188
ServerIpcOpenAuthSession(const char * sessionName,const ConnectionAddr * addrInfo)189 int32_t ServerIpcOpenAuthSession(const char *sessionName, const ConnectionAddr *addrInfo)
190 {
191 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "ServerIpcOpenAuthSession begin");
192
193 uint8_t data[MAX_SOFT_BUS_IPC_LEN] = {0};
194 IpcIo request = {0};
195 IpcIoInit(&request, data, MAX_SOFT_BUS_IPC_LEN, 0);
196 IpcIoPushString(&request, sessionName);
197 IpcIoPushFlatObj(&request, (void*)addrInfo, sizeof(ConnectionAddr));
198
199 int32_t ret = SOFTBUS_ERR;
200 if (g_serverProxy == NULL) {
201 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "server proxy not init");
202 return SOFTBUS_NO_INIT;
203 }
204 int32_t ans = g_serverProxy->Invoke(g_serverProxy, SERVER_OPEN_AUTH_SESSION, &request, &ret, ProxyCallback);
205 if (ans != EC_SUCCESS) {
206 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "ServerIpcOpenAuthSession callback ret [%d]", ret);
207 return SOFTBUS_ERR;
208 }
209 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "ServerIpcOpenAuthSession end");
210 return ret;
211 }
212
ServerIpcNotifyAuthSuccess(int channelId)213 int32_t ServerIpcNotifyAuthSuccess(int channelId)
214 {
215 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "ServerIpcNotifyAuthSuccess begin");
216
217 uint8_t data[MAX_SOFT_BUS_IPC_LEN] = {0};
218 IpcIo request = {0};
219 IpcIoInit(&request, data, MAX_SOFT_BUS_IPC_LEN, 0);
220 IpcIoPushInt32(&request, channelId);
221 int32_t ret = SOFTBUS_ERR;
222 if (g_serverProxy == NULL) {
223 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "server proxy not init");
224 return SOFTBUS_NO_INIT;
225 }
226 int32_t ans = g_serverProxy->Invoke(g_serverProxy, SERVER_NOTIFY_AUTH_SUCCESS, &request, &ret, ProxyCallback);
227 if (ans != EC_SUCCESS) {
228 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "ServerIpcNotifyAuthSuccess callback ret [%d]", ret);
229 return SOFTBUS_ERR;
230 }
231 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "ServerIpcNotifyAuthSuccess end");
232 return ret;
233 }
234
ServerIpcCloseChannel(int32_t channelId,int32_t channelType)235 int32_t ServerIpcCloseChannel(int32_t channelId, int32_t channelType)
236 {
237 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "ServerIpcCloseSession");
238 uint8_t data[MAX_SOFT_BUS_IPC_LEN] = {0};
239 IpcIo request = {0};
240 IpcIoInit(&request, data, MAX_SOFT_BUS_IPC_LEN, 0);
241 IpcIoPushInt32(&request, channelId);
242 IpcIoPushInt32(&request, channelType);
243
244 int32_t ret = SOFTBUS_ERR;
245 /* sync */
246 if (g_serverProxy == NULL) {
247 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "server proxy not init");
248 return SOFTBUS_NO_INIT;
249 }
250 int32_t ans = g_serverProxy->Invoke(g_serverProxy, SERVER_CLOSE_CHANNEL, &request, &ret, ProxyCallback);
251 if (ans != EC_SUCCESS) {
252 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "ServerIpcCloseSession callback ret [%d]", ret);
253 return SOFTBUS_ERR;
254 }
255 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "ServerIpcCloseSession");
256 return ret;
257 }
258
ServerIpcSendMessage(int32_t channelId,int32_t channelType,const void * data,uint32_t len,int32_t msgType)259 int32_t ServerIpcSendMessage(int32_t channelId, int32_t channelType, const void *data, uint32_t len, int32_t msgType)
260 {
261 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "ServerIpcSendMessage");
262
263 uint32_t ipcDataLen = len + MAX_SOFT_BUS_IPC_LEN;
264 uint8_t *ipcData = (uint8_t *)SoftBusCalloc(ipcDataLen);
265 IpcIo request = {0};
266 IpcIoInit(&request, ipcData, ipcDataLen, 0);
267 IpcIoPushInt32(&request, channelId);
268 IpcIoPushInt32(&request, channelType);
269 IpcIoPushInt32(&request, msgType);
270 IpcIoPushFlatObj(&request, data, len);
271
272 int32_t ret = SOFTBUS_ERR;
273 /* sync */
274 if (g_serverProxy == NULL) {
275 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "server proxy not init");
276 SoftBusFree(ipcData);
277 return SOFTBUS_NO_INIT;
278 }
279 int32_t ans = g_serverProxy->Invoke(g_serverProxy, SERVER_SESSION_SENDMSG, &request, &ret, ProxyCallback);
280 SoftBusFree(ipcData);
281 if (ans != EC_SUCCESS) {
282 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "ServerIpcSendMessage callback ret [%d]", ret);
283 return SOFTBUS_ERR;
284 }
285 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "ServerIpcSendMessage");
286 return ret;
287 }
288
ServerIpcQosReport(int32_t channelId,int32_t chanType,int32_t appType,int32_t quality)289 int32_t ServerIpcQosReport(int32_t channelId, int32_t chanType, int32_t appType, int32_t quality)
290 {
291 (void)channelId;
292 (void)chanType;
293 (void)appType;
294 (void)quality;
295 return SOFTBUS_NOT_IMPLEMENT;
296 }
297