• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     if (g_serverProxy->GetRemoteObject(g_oldObject) != SOFTBUS_OK) {
110         g_oldObject = nullptr;
111         TRANS_LOGW(TRANS_SDK, "Failed to get old remote object.");
112     }
113     g_serverProxy.clear();
114     g_serverProxy = nullptr;
115 }
116 
TransServerProxyDeInit(void)117 void TransServerProxyDeInit(void)
118 {
119     TRANS_LOGI(TRANS_SDK, "enter");
120     std::lock_guard<std::mutex> lock(g_mutex);
121     g_serverProxy.clear();
122     g_serverProxy = nullptr;
123 }
124 
ServerIpcCreateSessionServer(const char * pkgName,const char * sessionName)125 int32_t ServerIpcCreateSessionServer(const char *pkgName, const char *sessionName)
126 {
127     sptr<TransServerProxy> proxy = RetryGetProxy();
128     TRANS_CHECK_AND_RETURN_RET_LOGE(
129         proxy != nullptr, SOFTBUS_NO_INIT, TRANS_SDK, "softbus server g_serverProxy is nullptr");
130 
131     if ((pkgName == nullptr) || (sessionName == nullptr)) {
132         TRANS_LOGE(TRANS_SDK, "pkgName or sessionName is nullptr!");
133         return SOFTBUS_INVALID_PARAM;
134     }
135 
136     return proxy->CreateSessionServer(pkgName, sessionName);
137 }
138 
ServerIpcRemoveSessionServer(const char * pkgName,const char * sessionName)139 int32_t ServerIpcRemoveSessionServer(const char *pkgName, const char *sessionName)
140 {
141     sptr<TransServerProxy> proxy = GetProxy();
142     TRANS_CHECK_AND_RETURN_RET_LOGE(
143         proxy != nullptr, SOFTBUS_NO_INIT, TRANS_SDK, "softbus server g_serverProxy is nullptr");
144 
145     if ((pkgName == nullptr) || (sessionName == nullptr)) {
146         TRANS_LOGE(TRANS_SDK, "pkgName or sessionName is nullptr!");
147         return SOFTBUS_INVALID_PARAM;
148     }
149     return proxy->RemoveSessionServer(pkgName, sessionName);
150 }
151 
ServerIpcOpenSession(const SessionParam * param,TransInfo * info)152 int32_t ServerIpcOpenSession(const SessionParam *param, TransInfo *info)
153 {
154     sptr<TransServerProxy> proxy = RetryGetProxy();
155     TRANS_CHECK_AND_RETURN_RET_LOGE(
156         proxy != nullptr, SOFTBUS_NO_INIT, TRANS_SDK, "softbus server g_serverProxy is nullptr");
157 
158     if ((param->sessionName == nullptr) || (param->peerSessionName == nullptr) ||
159         (param->peerDeviceId == nullptr) || (param->groupId == nullptr) || (param->attr == nullptr)) {
160         TRANS_LOGE(TRANS_SDK, "parameter is nullptr!");
161         return SOFTBUS_INVALID_PARAM;
162     }
163 
164     int ret = proxy->OpenSession(param, info);
165     if (ret < SOFTBUS_OK) {
166         TRANS_LOGE(TRANS_SDK, "OpenSession failed! ret=%{public}d.", ret);
167     }
168 
169     return ret;
170 }
171 
ServerIpcOpenAuthSession(const char * sessionName,const ConnectionAddr * addrInfo)172 int32_t ServerIpcOpenAuthSession(const char *sessionName, const ConnectionAddr *addrInfo)
173 {
174     sptr<TransServerProxy> proxy = RetryGetProxy();
175     TRANS_CHECK_AND_RETURN_RET_LOGE(
176         proxy != nullptr, SOFTBUS_NO_INIT, TRANS_SDK, "softbus server g_serverProxy is nullptr");
177 
178     if ((sessionName == nullptr) || (addrInfo == nullptr)) {
179         TRANS_LOGE(TRANS_SDK, "parameter is nullptr!");
180         return SOFTBUS_INVALID_PARAM;
181     }
182 
183     int channelId = proxy->OpenAuthSession(sessionName, addrInfo);
184     if (channelId < SOFTBUS_OK) {
185         TRANS_LOGE(TRANS_SDK, "OpenAuthSession failed!");
186         return SOFTBUS_NO_INIT;
187     }
188     return channelId;
189 }
190 
ServerIpcNotifyAuthSuccess(int32_t channelId,int32_t channelType)191 int32_t ServerIpcNotifyAuthSuccess(int32_t channelId, int32_t channelType)
192 {
193     sptr<TransServerProxy> proxy = GetProxy();
194     TRANS_CHECK_AND_RETURN_RET_LOGE(
195         proxy != nullptr, SOFTBUS_NO_INIT, TRANS_SDK, "softbus server g_serverProxy is nullptr");
196 
197     return proxy->NotifyAuthSuccess(channelId, channelType);
198 }
199 
ServerIpcCloseChannel(const char * sessionName,int32_t channelId,int32_t channelType)200 int32_t ServerIpcCloseChannel(const char *sessionName, int32_t channelId, int32_t channelType)
201 {
202     sptr<TransServerProxy> proxy = GetProxy();
203     TRANS_CHECK_AND_RETURN_RET_LOGE(
204         proxy != nullptr, SOFTBUS_NO_INIT, TRANS_SDK, "softbus server g_serverProxy is nullptr");
205 
206     if (channelId < SOFTBUS_OK) {
207         TRANS_LOGE(TRANS_SDK, "invalid channel Id!");
208         return SOFTBUS_INVALID_PARAM;
209     }
210 
211     return proxy->CloseChannel(sessionName, channelId, channelType);
212 }
213 
ServerIpcCloseChannelWithStatistics(int32_t channelId,int32_t channelType,uint64_t laneId,const void * dataInfo,uint32_t len)214 int32_t ServerIpcCloseChannelWithStatistics(int32_t channelId, int32_t channelType, uint64_t laneId,
215     const void *dataInfo, uint32_t len)
216 {
217     if (dataInfo == nullptr) {
218         TRANS_LOGE(TRANS_SDK, "invalid param");
219         return SOFTBUS_INVALID_PARAM;
220     }
221     sptr<TransServerProxy> proxy = GetProxy();
222     TRANS_CHECK_AND_RETURN_RET_LOGE(
223         proxy != nullptr, SOFTBUS_NO_INIT, TRANS_SDK, "softbus server g_serverProxy is nullptr");
224 
225     if (channelId < MIN_CHANNEL_ID) {
226         TRANS_LOGE(TRANS_SDK, "invalid channelId=%{public}d", channelId);
227         return SOFTBUS_INVALID_PARAM;
228     }
229 
230     return proxy->CloseChannelWithStatistics(channelId, channelType, laneId, dataInfo, len);
231 }
232 
ServerIpcReleaseResources(int32_t channelId)233 int32_t ServerIpcReleaseResources(int32_t channelId)
234 {
235     sptr<TransServerProxy> proxy = GetProxy();
236     TRANS_CHECK_AND_RETURN_RET_LOGE(
237         proxy != nullptr, SOFTBUS_NO_INIT, TRANS_SDK, "softbus server g_serverProxy is nullptr");
238 
239     if ((channelId < MIN_CHANNEL_ID) || (channelId > MAX_CHANNEL_ID)) {
240         TRANS_LOGE(TRANS_SDK, "channelId=%{public}d is invalid.", channelId);
241         return SOFTBUS_TRANS_INVALID_CHANNEL_ID;
242     }
243     return proxy->ReleaseResources(channelId);
244 }
245 
ServerIpcSendMessage(int32_t channelId,int32_t channelType,const void * data,uint32_t len,int32_t msgType)246 int32_t ServerIpcSendMessage(int32_t channelId, int32_t channelType, const void *data, uint32_t len, int32_t msgType)
247 {
248     TRANS_CHECK_AND_RETURN_RET_LOGE(
249         g_serverProxy != nullptr, SOFTBUS_NO_INIT, TRANS_SDK, "softbus server g_serverProxy is nullptr");
250 
251     return g_serverProxy->SendMessage(channelId, channelType, data, len, msgType);
252 }
253 
ServerIpcQosReport(int32_t channelId,int32_t chanType,int32_t appType,int32_t quality)254 int32_t ServerIpcQosReport(int32_t channelId, int32_t chanType, int32_t appType, int32_t quality)
255 {
256     sptr<TransServerProxy> proxy = GetProxy();
257     TRANS_CHECK_AND_RETURN_RET_LOGE(
258         proxy != nullptr, SOFTBUS_NO_INIT, TRANS_SDK, "softbus server g_serverProxy is nullptr");
259 
260     return proxy->QosReport(channelId, chanType, appType, quality);
261 }
262 
ServerIpcStreamStats(int32_t channelId,int32_t channelType,const StreamSendStats * data)263 int32_t ServerIpcStreamStats(int32_t channelId, int32_t channelType, const StreamSendStats *data)
264 {
265     sptr<TransServerProxy> proxy = GetProxy();
266     TRANS_CHECK_AND_RETURN_RET_LOGE(
267         proxy != nullptr, SOFTBUS_NO_INIT, TRANS_SDK, "softbus server g_serverProxy is nullptr");
268 
269     return proxy->StreamStats(channelId, channelType, data);
270 }
271 
ServerIpcRippleStats(int32_t channelId,int32_t channelType,const TrafficStats * data)272 int32_t ServerIpcRippleStats(int32_t channelId, int32_t channelType, const TrafficStats *data)
273 {
274     sptr<TransServerProxy> proxy = GetProxy();
275     TRANS_CHECK_AND_RETURN_RET_LOGE(
276         proxy != nullptr, SOFTBUS_NO_INIT, TRANS_SDK, "softbus server g_serverProxy is nullptr");
277 
278     return proxy->RippleStats(channelId, channelType, data);
279 }
280 
ServerIpcGrantPermission(int uid,int pid,const char * sessionName)281 int32_t ServerIpcGrantPermission(int uid, int pid, const char *sessionName)
282 {
283     sptr<TransServerProxy> proxy = GetProxy();
284     TRANS_CHECK_AND_RETURN_RET_LOGE(
285         proxy != nullptr, SOFTBUS_NO_INIT, TRANS_SDK, "softbus server g_serverProxy is nullptr");
286 
287     if (sessionName == nullptr) {
288         TRANS_LOGE(TRANS_SDK, "sessionName is nullptr");
289         return SOFTBUS_INVALID_PARAM;
290     }
291 
292     return proxy->GrantPermission(uid, pid, sessionName);
293 }
294 
ServerIpcRemovePermission(const char * sessionName)295 int32_t ServerIpcRemovePermission(const char *sessionName)
296 {
297     sptr<TransServerProxy> proxy = GetProxy();
298     TRANS_CHECK_AND_RETURN_RET_LOGE(
299         proxy != nullptr, SOFTBUS_NO_INIT, TRANS_SDK, "softbus server g_serverProxy is nullptr");
300 
301     if (sessionName == nullptr) {
302         TRANS_LOGE(TRANS_SDK, "sessionName is nullptr");
303         return SOFTBUS_INVALID_PARAM;
304     }
305 
306     return proxy->RemovePermission(sessionName);
307 }
308 
ServerIpcEvaluateQos(const char * peerNetworkId,TransDataType dataType,const QosTV * qos,uint32_t qosCount)309 int32_t ServerIpcEvaluateQos(const char *peerNetworkId, TransDataType dataType, const QosTV *qos, uint32_t qosCount)
310 {
311     sptr<TransServerProxy> proxy = GetProxy();
312     TRANS_CHECK_AND_RETURN_RET_LOGE(
313         proxy != nullptr, SOFTBUS_NO_INIT, TRANS_SDK, "softbus server g_serverProxy is nullptr");
314 
315     if (peerNetworkId == NULL || dataType >= DATA_TYPE_BUTT || qosCount > QOS_TYPE_BUTT) {
316         TRANS_LOGE(TRANS_SDK, "invalid param");
317         return SOFTBUS_INVALID_PARAM;
318     }
319 
320     return proxy->EvaluateQos(peerNetworkId, dataType, qos, qosCount);
321 }
322 
ServerIpcProcessInnerEvent(int32_t eventType,uint8_t * buf,uint32_t len)323 int32_t ServerIpcProcessInnerEvent(int32_t eventType, uint8_t *buf, uint32_t len)
324 {
325     sptr<TransServerProxy> proxy = GetProxy();
326     TRANS_CHECK_AND_RETURN_RET_LOGE(
327         proxy != nullptr, SOFTBUS_NO_INIT, TRANS_SDK, "softbus server g_serverProxy is nullptr");
328     if (eventType >= EVENT_TYPE_BUTT || eventType < EVENT_TYPE_CHANNEL_OPENED || buf == NULL) {
329         TRANS_LOGE(TRANS_SDK, "invalid param");
330         return SOFTBUS_INVALID_PARAM;
331     }
332 
333     return proxy->ProcessInnerEvent(eventType, buf, len);
334 }
335 
ServerIpcPrivilegeCloseChannel(uint64_t tokenId,int32_t pid,const char * peerNetworkId)336 int32_t ServerIpcPrivilegeCloseChannel(uint64_t tokenId, int32_t pid, const char *peerNetworkId)
337 {
338     if (peerNetworkId == nullptr) {
339         TRANS_LOGE(TRANS_SDK, "invalid param");
340         return SOFTBUS_INVALID_PARAM;
341     }
342     sptr<TransServerProxy> proxy = GetProxy();
343     TRANS_CHECK_AND_RETURN_RET_LOGE(
344         proxy != nullptr, SOFTBUS_NO_INIT, TRANS_SDK, "softbus server g_serverProxy is nullptr");
345     return proxy->PrivilegeCloseChannel(tokenId, pid, peerNetworkId);
346 }