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 #include "trans_server_proxy_standard.h"
18
19 #include <malloc.h>
20
21 #include "ipc_skeleton.h"
22 #include "softbus_adapter_timer.h"
23 #include "softbus_error_code.h"
24 #include "softbus_server_ipc_interface_code.h"
25 #include "trans_log.h"
26
27 using namespace OHOS;
28
29 namespace {
30 sptr<TransServerProxy> g_serverProxy = nullptr;
31 sptr<IRemoteObject> g_oldObject = nullptr;
32 const std::u16string SAMANAGER_INTERFACE_TOKEN = u"ohos.samgr.accessToken";
33 constexpr int32_t MIN_CHANNEL_ID = 0; // UDP channelId minmum value
34 constexpr int32_t MAX_CHANNEL_ID = 19; // UDP channelId maxmum value
35 uint32_t g_getSystemAbilityId = 2;
36 std::mutex g_mutex;
37 constexpr uint32_t RETRY_COUNT = 10; // retry count of getting proxy object
38 constexpr uint32_t RETRY_INTERVAL = 50; // retry interval(ms)
39 }
40
GetSystemAbility()41 static sptr<IRemoteObject> GetSystemAbility()
42 {
43 MessageParcel data;
44
45 TRANS_CHECK_AND_RETURN_RET_LOGE(
46 data.WriteInterfaceToken(SAMANAGER_INTERFACE_TOKEN), nullptr, TRANS_SDK, "write samgr interface token failed");
47
48 data.WriteInt32(SOFTBUS_SERVER_SA_ID_INNER);
49 MessageParcel reply;
50 MessageOption option;
51 sptr<IRemoteObject> samgr = IPCSkeleton::GetContextObject();
52 TRANS_CHECK_AND_RETURN_RET_LOGE(samgr != nullptr, nullptr, TRANS_SDK, "Get samgr failed");
53
54 int32_t err = samgr->SendRequest(g_getSystemAbilityId, data, reply, option);
55 TRANS_CHECK_AND_RETURN_RET_LOGE(err == 0, nullptr, TRANS_SDK, "Get samgr failed");
56
57 return reply.ReadRemoteObject();
58 }
59
GetProxy()60 static sptr<TransServerProxy> GetProxy()
61 {
62 std::lock_guard<std::mutex> lock(g_mutex);
63 if (g_serverProxy != nullptr) {
64 return g_serverProxy;
65 }
66
67 sptr<IRemoteObject> object = GetSystemAbility();
68 TRANS_CHECK_AND_RETURN_RET_LOGE(object != nullptr, nullptr, TRANS_SDK, "Get remote softbus object failed");
69 if (object == g_oldObject) {
70 TRANS_LOGE(TRANS_SDK, "Failed to get latest remote object.");
71 return nullptr;
72 }
73
74 g_serverProxy = new (std::nothrow) TransServerProxy(object);
75 TRANS_CHECK_AND_RETURN_RET_LOGE(g_serverProxy != nullptr, nullptr, TRANS_SDK, "Create trans server proxy failed");
76
77 return g_serverProxy;
78 }
79
RetryGetProxy()80 static sptr<TransServerProxy> RetryGetProxy()
81 {
82 // retry 'RETRY_COUNT' times with an interval of 'RETRY_INTERVAL' ms
83 sptr<TransServerProxy> proxy = nullptr;
84 for (uint32_t count = 0; count < RETRY_COUNT; ++count) {
85 proxy = GetProxy();
86 if (proxy != nullptr) {
87 return proxy;
88 }
89 TRANS_LOGD(TRANS_SDK, "softbus server g_serverProxy is nullptr, retry %{public}" PRIu32 "th time", count);
90 SoftBusSleepMs(RETRY_INTERVAL);
91 }
92 TRANS_LOGE(TRANS_SDK, "Failed to get softbus server g_serverProxy");
93 return nullptr;
94 }
95
TransServerProxyInit(void)96 int32_t TransServerProxyInit(void)
97 {
98 mallopt(M_DELAYED_FREE, M_DELAYED_FREE_ENABLE);
99 TRANS_CHECK_AND_RETURN_RET_LOGE(
100 RetryGetProxy() != nullptr, SOFTBUS_NO_INIT, TRANS_SDK, "Failed to initialize the server proxy");
101 TRANS_LOGI(TRANS_SDK, "Init success");
102 return SOFTBUS_OK;
103 }
104
TransServerProxyClear(void)105 void TransServerProxyClear(void)
106 {
107 TRANS_LOGI(TRANS_SDK, "enter");
108 std::lock_guard<std::mutex> lock(g_mutex);
109 TRANS_CHECK_AND_RETURN_LOGE(
110 g_serverProxy != nullptr, TRANS_SDK, "softbus server g_serverProxy is nullptr");
111 if (g_serverProxy->GetRemoteObject(g_oldObject) != SOFTBUS_OK) {
112 g_oldObject = nullptr;
113 TRANS_LOGW(TRANS_SDK, "Failed to get old remote object.");
114 }
115 g_serverProxy.clear();
116 g_serverProxy = nullptr;
117 }
118
TransServerProxyDeInit(void)119 void TransServerProxyDeInit(void)
120 {
121 TRANS_LOGI(TRANS_SDK, "enter");
122 std::lock_guard<std::mutex> lock(g_mutex);
123 TRANS_CHECK_AND_RETURN_LOGE(
124 g_serverProxy != nullptr, TRANS_SDK, "softbus server g_serverProxy is nullptr");
125 g_serverProxy.clear();
126 g_serverProxy = nullptr;
127 }
128
ServerIpcCreateSessionServer(const char * pkgName,const char * sessionName,uint64_t timestamp)129 int32_t ServerIpcCreateSessionServer(const char *pkgName, const char *sessionName, uint64_t timestamp)
130 {
131 sptr<TransServerProxy> proxy = RetryGetProxy();
132 TRANS_CHECK_AND_RETURN_RET_LOGE(
133 proxy != nullptr, SOFTBUS_NO_INIT, TRANS_SDK, "softbus server g_serverProxy is nullptr");
134
135 if ((pkgName == nullptr) || (sessionName == nullptr)) {
136 TRANS_LOGE(TRANS_SDK, "pkgName or sessionName is nullptr!");
137 return SOFTBUS_INVALID_PARAM;
138 }
139
140 return proxy->CreateSessionServer(pkgName, sessionName, timestamp);
141 }
142
ServerIpcRemoveSessionServer(const char * pkgName,const char * sessionName,uint64_t timestamp)143 int32_t ServerIpcRemoveSessionServer(const char *pkgName, const char *sessionName, uint64_t timestamp)
144 {
145 sptr<TransServerProxy> proxy = GetProxy();
146 TRANS_CHECK_AND_RETURN_RET_LOGE(
147 proxy != nullptr, SOFTBUS_NO_INIT, TRANS_SDK, "softbus server g_serverProxy is nullptr");
148
149 if ((pkgName == nullptr) || (sessionName == nullptr)) {
150 TRANS_LOGE(TRANS_SDK, "pkgName or sessionName is nullptr!");
151 return SOFTBUS_INVALID_PARAM;
152 }
153 return proxy->RemoveSessionServer(pkgName, sessionName, timestamp);
154 }
155
ServerIpcOpenSession(const SessionParam * param,TransInfo * info)156 int32_t ServerIpcOpenSession(const SessionParam *param, TransInfo *info)
157 {
158 sptr<TransServerProxy> proxy = RetryGetProxy();
159 TRANS_CHECK_AND_RETURN_RET_LOGE(
160 proxy != nullptr, SOFTBUS_NO_INIT, TRANS_SDK, "softbus server g_serverProxy is nullptr");
161
162 if ((param->sessionName == nullptr) || (param->peerSessionName == nullptr) ||
163 (param->peerDeviceId == nullptr) || (param->groupId == nullptr) || (param->attr == nullptr)) {
164 TRANS_LOGE(TRANS_SDK, "parameter is nullptr!");
165 return SOFTBUS_INVALID_PARAM;
166 }
167
168 int ret = proxy->OpenSession(param, info);
169 if (ret < SOFTBUS_OK) {
170 TRANS_LOGE(TRANS_SDK, "OpenSession failed! ret=%{public}d.", ret);
171 }
172
173 return ret;
174 }
175
ServerIpcOpenAuthSession(const char * sessionName,const ConnectionAddr * addrInfo)176 int32_t ServerIpcOpenAuthSession(const char *sessionName, const ConnectionAddr *addrInfo)
177 {
178 sptr<TransServerProxy> proxy = RetryGetProxy();
179 TRANS_CHECK_AND_RETURN_RET_LOGE(
180 proxy != nullptr, SOFTBUS_NO_INIT, TRANS_SDK, "softbus server g_serverProxy is nullptr");
181
182 if ((sessionName == nullptr) || (addrInfo == nullptr)) {
183 TRANS_LOGE(TRANS_SDK, "parameter is nullptr!");
184 return SOFTBUS_INVALID_PARAM;
185 }
186
187 int channelId = proxy->OpenAuthSession(sessionName, addrInfo);
188 if (channelId < SOFTBUS_OK) {
189 TRANS_LOGE(TRANS_SDK, "OpenAuthSession failed!");
190 return SOFTBUS_NO_INIT;
191 }
192 return channelId;
193 }
194
ServerIpcNotifyAuthSuccess(int32_t channelId,int32_t channelType)195 int32_t ServerIpcNotifyAuthSuccess(int32_t channelId, int32_t channelType)
196 {
197 sptr<TransServerProxy> proxy = GetProxy();
198 TRANS_CHECK_AND_RETURN_RET_LOGE(
199 proxy != nullptr, SOFTBUS_NO_INIT, TRANS_SDK, "softbus server g_serverProxy is nullptr");
200
201 return proxy->NotifyAuthSuccess(channelId, channelType);
202 }
203
ServerIpcCloseChannel(const char * sessionName,int32_t channelId,int32_t channelType)204 int32_t ServerIpcCloseChannel(const char *sessionName, int32_t channelId, int32_t channelType)
205 {
206 sptr<TransServerProxy> proxy = GetProxy();
207 TRANS_CHECK_AND_RETURN_RET_LOGE(
208 proxy != nullptr, SOFTBUS_NO_INIT, TRANS_SDK, "softbus server g_serverProxy is nullptr");
209
210 if (channelId < SOFTBUS_OK) {
211 TRANS_LOGE(TRANS_SDK, "invalid channel Id!");
212 return SOFTBUS_INVALID_PARAM;
213 }
214
215 return proxy->CloseChannel(sessionName, channelId, channelType);
216 }
217
ServerIpcCloseChannelWithStatistics(int32_t channelId,int32_t channelType,uint64_t laneId,const void * dataInfo,uint32_t len)218 int32_t ServerIpcCloseChannelWithStatistics(int32_t channelId, int32_t channelType, uint64_t laneId,
219 const void *dataInfo, uint32_t len)
220 {
221 if (dataInfo == nullptr) {
222 TRANS_LOGE(TRANS_SDK, "invalid param");
223 return SOFTBUS_INVALID_PARAM;
224 }
225 sptr<TransServerProxy> proxy = GetProxy();
226 TRANS_CHECK_AND_RETURN_RET_LOGE(
227 proxy != nullptr, SOFTBUS_NO_INIT, TRANS_SDK, "softbus server g_serverProxy is nullptr");
228
229 if (channelId < MIN_CHANNEL_ID) {
230 TRANS_LOGE(TRANS_SDK, "invalid channelId=%{public}d", channelId);
231 return SOFTBUS_INVALID_PARAM;
232 }
233
234 return proxy->CloseChannelWithStatistics(channelId, channelType, laneId, dataInfo, len);
235 }
236
ServerIpcReleaseResources(int32_t channelId)237 int32_t ServerIpcReleaseResources(int32_t channelId)
238 {
239 sptr<TransServerProxy> proxy = GetProxy();
240 TRANS_CHECK_AND_RETURN_RET_LOGE(
241 proxy != nullptr, SOFTBUS_NO_INIT, TRANS_SDK, "softbus server g_serverProxy is nullptr");
242
243 if ((channelId < MIN_CHANNEL_ID) || (channelId > MAX_CHANNEL_ID)) {
244 TRANS_LOGE(TRANS_SDK, "channelId=%{public}d is invalid.", channelId);
245 return SOFTBUS_TRANS_INVALID_CHANNEL_ID;
246 }
247 return proxy->ReleaseResources(channelId);
248 }
249
ServerIpcSendMessage(int32_t channelId,int32_t channelType,const void * data,uint32_t len,int32_t msgType)250 int32_t ServerIpcSendMessage(int32_t channelId, int32_t channelType, const void *data, uint32_t len, int32_t msgType)
251 {
252 TRANS_CHECK_AND_RETURN_RET_LOGE(
253 g_serverProxy != nullptr, SOFTBUS_NO_INIT, TRANS_SDK, "softbus server g_serverProxy is nullptr");
254
255 return g_serverProxy->SendMessage(channelId, channelType, data, len, msgType);
256 }
257
ServerIpcQosReport(int32_t channelId,int32_t chanType,int32_t appType,int32_t quality)258 int32_t ServerIpcQosReport(int32_t channelId, int32_t chanType, int32_t appType, int32_t quality)
259 {
260 sptr<TransServerProxy> proxy = GetProxy();
261 TRANS_CHECK_AND_RETURN_RET_LOGE(
262 proxy != nullptr, SOFTBUS_NO_INIT, TRANS_SDK, "softbus server g_serverProxy is nullptr");
263
264 return proxy->QosReport(channelId, chanType, appType, quality);
265 }
266
ServerIpcStreamStats(int32_t channelId,int32_t channelType,const StreamSendStats * data)267 int32_t ServerIpcStreamStats(int32_t channelId, int32_t channelType, const StreamSendStats *data)
268 {
269 sptr<TransServerProxy> proxy = GetProxy();
270 TRANS_CHECK_AND_RETURN_RET_LOGE(
271 proxy != nullptr, SOFTBUS_NO_INIT, TRANS_SDK, "softbus server g_serverProxy is nullptr");
272
273 return proxy->StreamStats(channelId, channelType, data);
274 }
275
ServerIpcRippleStats(int32_t channelId,int32_t channelType,const TrafficStats * data)276 int32_t ServerIpcRippleStats(int32_t channelId, int32_t channelType, const TrafficStats *data)
277 {
278 sptr<TransServerProxy> proxy = GetProxy();
279 TRANS_CHECK_AND_RETURN_RET_LOGE(
280 proxy != nullptr, SOFTBUS_NO_INIT, TRANS_SDK, "softbus server g_serverProxy is nullptr");
281
282 return proxy->RippleStats(channelId, channelType, data);
283 }
284
ServerIpcGrantPermission(int uid,int pid,const char * sessionName)285 int32_t ServerIpcGrantPermission(int uid, int pid, const char *sessionName)
286 {
287 sptr<TransServerProxy> proxy = GetProxy();
288 TRANS_CHECK_AND_RETURN_RET_LOGE(
289 proxy != nullptr, SOFTBUS_NO_INIT, TRANS_SDK, "softbus server g_serverProxy is nullptr");
290
291 if (sessionName == nullptr) {
292 TRANS_LOGE(TRANS_SDK, "sessionName is nullptr");
293 return SOFTBUS_INVALID_PARAM;
294 }
295
296 return proxy->GrantPermission(uid, pid, sessionName);
297 }
298
ServerIpcRemovePermission(const char * sessionName)299 int32_t ServerIpcRemovePermission(const char *sessionName)
300 {
301 sptr<TransServerProxy> proxy = GetProxy();
302 TRANS_CHECK_AND_RETURN_RET_LOGE(
303 proxy != nullptr, SOFTBUS_NO_INIT, TRANS_SDK, "softbus server g_serverProxy is nullptr");
304
305 if (sessionName == nullptr) {
306 TRANS_LOGE(TRANS_SDK, "sessionName is nullptr");
307 return SOFTBUS_INVALID_PARAM;
308 }
309
310 return proxy->RemovePermission(sessionName);
311 }
312
ServerIpcEvaluateQos(const char * peerNetworkId,TransDataType dataType,const QosTV * qos,uint32_t qosCount)313 int32_t ServerIpcEvaluateQos(const char *peerNetworkId, TransDataType dataType, const QosTV *qos, uint32_t qosCount)
314 {
315 sptr<TransServerProxy> proxy = GetProxy();
316 TRANS_CHECK_AND_RETURN_RET_LOGE(
317 proxy != nullptr, SOFTBUS_NO_INIT, TRANS_SDK, "softbus server g_serverProxy is nullptr");
318
319 if (peerNetworkId == NULL || dataType >= DATA_TYPE_BUTT || qosCount > QOS_TYPE_BUTT) {
320 TRANS_LOGE(TRANS_SDK, "invalid param");
321 return SOFTBUS_INVALID_PARAM;
322 }
323
324 return proxy->EvaluateQos(peerNetworkId, dataType, qos, qosCount);
325 }
326
ServerIpcProcessInnerEvent(int32_t eventType,uint8_t * buf,uint32_t len)327 int32_t ServerIpcProcessInnerEvent(int32_t eventType, uint8_t *buf, uint32_t len)
328 {
329 sptr<TransServerProxy> proxy = GetProxy();
330 TRANS_CHECK_AND_RETURN_RET_LOGE(
331 proxy != nullptr, SOFTBUS_NO_INIT, TRANS_SDK, "softbus server g_serverProxy is nullptr");
332 if (eventType >= EVENT_TYPE_BUTT || eventType < EVENT_TYPE_CHANNEL_OPENED || buf == NULL) {
333 TRANS_LOGE(TRANS_SDK, "invalid param");
334 return SOFTBUS_INVALID_PARAM;
335 }
336
337 return proxy->ProcessInnerEvent(eventType, buf, len);
338 }
339
ServerIpcPrivilegeCloseChannel(uint64_t tokenId,int32_t pid,const char * peerNetworkId)340 int32_t ServerIpcPrivilegeCloseChannel(uint64_t tokenId, int32_t pid, const char *peerNetworkId)
341 {
342 if (peerNetworkId == nullptr) {
343 TRANS_LOGE(TRANS_SDK, "invalid param");
344 return SOFTBUS_INVALID_PARAM;
345 }
346 sptr<TransServerProxy> proxy = GetProxy();
347 TRANS_CHECK_AND_RETURN_RET_LOGE(
348 proxy != nullptr, SOFTBUS_NO_INIT, TRANS_SDK, "softbus server g_serverProxy is nullptr");
349 return proxy->PrivilegeCloseChannel(tokenId, pid, peerNetworkId);
350 }
351
ServerIpcOpenBrProxy(const char * brMac,const char * uuid)352 int32_t ServerIpcOpenBrProxy(const char *brMac, const char *uuid)
353 {
354 sptr<TransServerProxy> proxy = RetryGetProxy();
355 TRANS_CHECK_AND_RETURN_RET_LOGE(
356 proxy != nullptr, SOFTBUS_NO_INIT, TRANS_SDK, "[br_proxy] softbus server g_serverProxy is nullptr");
357
358 if (brMac == nullptr || uuid == nullptr) {
359 TRANS_LOGE(TRANS_SDK, "[br_proxy] parameter is nullptr!");
360 return SOFTBUS_INVALID_PARAM;
361 }
362
363 return proxy->OpenBrProxy(brMac, uuid);
364 }
365
ServerIpcCloseBrProxy(int32_t channelId)366 int32_t ServerIpcCloseBrProxy(int32_t channelId)
367 {
368 sptr<TransServerProxy> proxy = RetryGetProxy();
369 TRANS_CHECK_AND_RETURN_RET_LOGE(
370 proxy != nullptr, SOFTBUS_NO_INIT, TRANS_SDK, "[br_proxy] softbus server g_serverProxy is nullptr");
371
372 int32_t ret = proxy->CloseBrProxy(channelId);
373 if (ret != SOFTBUS_OK) {
374 TRANS_LOGE(TRANS_SDK, "[br_proxy] CloseBrProxy failed! ret=%{public}d.", ret);
375 }
376
377 return ret;
378 }
379
ServerIpcSendBrProxyData(int32_t channelId,char * data,uint32_t dataLen)380 int32_t ServerIpcSendBrProxyData(int32_t channelId, char *data, uint32_t dataLen)
381 {
382 sptr<TransServerProxy> proxy = RetryGetProxy();
383 TRANS_CHECK_AND_RETURN_RET_LOGE(
384 proxy != nullptr, SOFTBUS_NO_INIT, TRANS_SDK, "[br_proxy] softbus server g_serverProxy is nullptr");
385
386 int32_t ret = proxy->SendBrProxyData(channelId, data, dataLen);
387 if (ret != SOFTBUS_OK) {
388 TRANS_LOGE(TRANS_SDK, "[br_proxy] SendBrProxyData failed! ret=%{public}d.", ret);
389 }
390
391 return ret;
392 }
393
ServerIpcSetListenerState(int32_t channelId,int32_t type,bool CbEnabled)394 int32_t ServerIpcSetListenerState(int32_t channelId, int32_t type, bool CbEnabled)
395 {
396 sptr<TransServerProxy> proxy = RetryGetProxy();
397 TRANS_CHECK_AND_RETURN_RET_LOGE(
398 proxy != nullptr, SOFTBUS_NO_INIT, TRANS_SDK, "[br_proxy] softbus server g_serverProxy is nullptr");
399
400 int32_t ret = proxy->SetListenerState(channelId, type, CbEnabled);
401 if (ret != SOFTBUS_OK) {
402 TRANS_LOGE(TRANS_SDK, "[br_proxy] SetListenerState failed! ret=%{public}d.", ret);
403 }
404
405 return ret;
406 }
407
ServerIpcIsProxyChannelEnabled(int32_t uid,bool * isEnable)408 int32_t ServerIpcIsProxyChannelEnabled(int32_t uid, bool *isEnable)
409 {
410 sptr<TransServerProxy> proxy = RetryGetProxy();
411 TRANS_CHECK_AND_RETURN_RET_LOGE(
412 proxy != nullptr, SOFTBUS_NO_INIT, TRANS_SDK, "[br_proxy] softbus server g_serverProxy is nullptr");
413
414 int32_t ret = proxy->GetProxyChannelState(uid, isEnable);
415 if (ret != SOFTBUS_OK) {
416 TRANS_LOGE(TRANS_SDK, "[br_proxy] GetProxyChannelState failed! ret=%{public}d.", ret);
417 }
418
419 return ret;
420 }
421
ServerIpcRegisterPushHook()422 int32_t ServerIpcRegisterPushHook()
423 {
424 sptr<TransServerProxy> proxy = RetryGetProxy();
425 TRANS_CHECK_AND_RETURN_RET_LOGE(
426 proxy != nullptr, SOFTBUS_NO_INIT, TRANS_SDK, "[br_proxy] softbus server g_serverProxy is nullptr");
427
428 int32_t ret = proxy->RegisterPushHook();
429 if (ret != SOFTBUS_OK) {
430 TRANS_LOGE(TRANS_SDK, "[br_proxy] RegisterPushHook failed! ret=%{public}d.", ret);
431 }
432
433 return ret;
434 }