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 #include <mutex>
21 
22 #include "ipc_skeleton.h"
23 #include "iremote_broker.h"
24 #include "iremote_object.h"
25 #include "iremote_proxy.h"
26 #include "softbus_adapter_timer.h"
27 #include "softbus_errcode.h"
28 #include "softbus_server_ipc_interface_code.h"
29 #include "trans_log.h"
30 
31 using namespace OHOS;
32 
33 namespace {
34 sptr<TransServerProxy> g_serverProxy = nullptr;
35 const std::u16string SAMANAGER_INTERFACE_TOKEN = u"ohos.samgr.accessToken";
36 constexpr int32_t MIN_CHANNEL_ID = 0; // UDP channelId minmum value
37 constexpr int32_t MAX_CHANNEL_ID = 19; // UDP channelId maxmum value
38 uint32_t g_getSystemAbilityId = 2;
39 std::mutex g_mutex;
40 constexpr uint32_t RETRY_COUNT = 10;    // retry count of getting proxy object
41 constexpr uint32_t RETRY_INTERVAL = 50; // retry interval(ms)
42 }
43 
GetSystemAbility()44 static sptr<IRemoteObject> GetSystemAbility()
45 {
46     MessageParcel data;
47 
48     TRANS_CHECK_AND_RETURN_RET_LOGE(
49         data.WriteInterfaceToken(SAMANAGER_INTERFACE_TOKEN), nullptr, TRANS_SDK, "write samgr interface token failed");
50 
51     data.WriteInt32(SOFTBUS_SERVER_SA_ID_INNER);
52     MessageParcel reply;
53     MessageOption option;
54     sptr<IRemoteObject> samgr = IPCSkeleton::GetContextObject();
55     TRANS_CHECK_AND_RETURN_RET_LOGE(samgr != nullptr, nullptr, TRANS_SDK, "Get samgr failed");
56 
57     int32_t err = samgr->SendRequest(g_getSystemAbilityId, data, reply, option);
58     TRANS_CHECK_AND_RETURN_RET_LOGE(err == 0, nullptr, TRANS_SDK, "Get samgr failed");
59 
60     return reply.ReadRemoteObject();
61 }
62 
GetProxy()63 static sptr<TransServerProxy> GetProxy()
64 {
65     std::lock_guard<std::mutex> lock(g_mutex);
66     if (g_serverProxy != nullptr) {
67         return g_serverProxy;
68     }
69 
70     sptr<IRemoteObject> object = GetSystemAbility();
71     TRANS_CHECK_AND_RETURN_RET_LOGE(object != nullptr, nullptr, TRANS_SDK, "Get remote softbus object failed");
72 
73     g_serverProxy = new (std::nothrow) TransServerProxy(object);
74     TRANS_CHECK_AND_RETURN_RET_LOGE(g_serverProxy != nullptr, nullptr, TRANS_SDK, "Create trans server proxy failed");
75 
76     return g_serverProxy;
77 }
78 
RetryGetProxy()79 static sptr<TransServerProxy> RetryGetProxy()
80 {
81     // retry 'RETRY_COUNT' times with an interval of 'RETRY_INTERVAL' ms
82     sptr<TransServerProxy> proxy = nullptr;
83     for (uint32_t count = 0; count < RETRY_COUNT; ++count) {
84         proxy = GetProxy();
85         if (proxy != nullptr) {
86             return proxy;
87         }
88         TRANS_LOGD(TRANS_SDK, "softbus server g_serverProxy is nullptr, retry %{public}" PRIu32 "th time", count);
89         SoftBusSleepMs(RETRY_INTERVAL);
90     }
91     TRANS_LOGE(TRANS_SDK, "Failed to get softbus server g_serverProxy");
92     return nullptr;
93 }
94 
TransServerProxyInit(void)95 int32_t TransServerProxyInit(void)
96 {
97     mallopt(M_DELAYED_FREE, M_DELAYED_FREE_ENABLE);
98     TRANS_CHECK_AND_RETURN_RET_LOGE(
99         GetProxy() != nullptr, SOFTBUS_NO_INIT, TRANS_SDK, "Failed to initialize the server proxy");
100 
101     TRANS_LOGI(TRANS_SDK, "Init success");
102     return SOFTBUS_OK;
103 }
104 
TransServerProxyDeInit(void)105 void TransServerProxyDeInit(void)
106 {
107     TRANS_LOGI(TRANS_SDK, "enter");
108     std::lock_guard<std::mutex> lock(g_mutex);
109     g_serverProxy.clear();
110 }
111 
ServerIpcCreateSessionServer(const char * pkgName,const char * sessionName)112 int32_t ServerIpcCreateSessionServer(const char *pkgName, const char *sessionName)
113 {
114     sptr<TransServerProxy> proxy = RetryGetProxy();
115     TRANS_CHECK_AND_RETURN_RET_LOGE(
116         proxy != nullptr, SOFTBUS_NO_INIT, TRANS_SDK, "softbus server g_serverProxy is nullptr");
117 
118     if ((pkgName == nullptr) || (sessionName == nullptr)) {
119         TRANS_LOGE(TRANS_SDK, "pkgName or sessionName is nullptr!");
120         return SOFTBUS_INVALID_PARAM;
121     }
122 
123     return proxy->CreateSessionServer(pkgName, sessionName);
124 }
125 
ServerIpcRemoveSessionServer(const char * pkgName,const char * sessionName)126 int32_t ServerIpcRemoveSessionServer(const char *pkgName, const char *sessionName)
127 {
128     sptr<TransServerProxy> proxy = GetProxy();
129     TRANS_CHECK_AND_RETURN_RET_LOGE(
130         proxy != nullptr, SOFTBUS_NO_INIT, TRANS_SDK, "softbus server g_serverProxy is nullptr");
131 
132     if ((pkgName == nullptr) || (sessionName == nullptr)) {
133         TRANS_LOGE(TRANS_SDK, "pkgName or sessionName is nullptr!");
134         return SOFTBUS_INVALID_PARAM;
135     }
136     return proxy->RemoveSessionServer(pkgName, sessionName);
137 }
138 
ServerIpcOpenSession(const SessionParam * param,TransInfo * info)139 int32_t ServerIpcOpenSession(const SessionParam *param, TransInfo *info)
140 {
141     sptr<TransServerProxy> proxy = RetryGetProxy();
142     TRANS_CHECK_AND_RETURN_RET_LOGE(
143         proxy != nullptr, SOFTBUS_NO_INIT, TRANS_SDK, "softbus server g_serverProxy is nullptr");
144 
145     if ((param->sessionName == nullptr) || (param->peerSessionName == nullptr) ||
146         (param->peerDeviceId == nullptr) || (param->groupId == nullptr) || (param->attr == nullptr)) {
147         TRANS_LOGE(TRANS_SDK, "parameter is nullptr!");
148         return SOFTBUS_INVALID_PARAM;
149     }
150 
151     int ret = proxy->OpenSession(param, info);
152     if (ret < SOFTBUS_OK) {
153         TRANS_LOGE(TRANS_SDK, "OpenSession failed! ret=%{public}d.", ret);
154     }
155 
156     return ret;
157 }
158 
ServerIpcOpenAuthSession(const char * sessionName,const ConnectionAddr * addrInfo)159 int32_t ServerIpcOpenAuthSession(const char *sessionName, const ConnectionAddr *addrInfo)
160 {
161     sptr<TransServerProxy> proxy = RetryGetProxy();
162     TRANS_CHECK_AND_RETURN_RET_LOGE(
163         proxy != nullptr, SOFTBUS_NO_INIT, TRANS_SDK, "softbus server g_serverProxy is nullptr");
164 
165     if ((sessionName == nullptr) || (addrInfo == nullptr)) {
166         TRANS_LOGE(TRANS_SDK, "parameter is nullptr!");
167         return SOFTBUS_INVALID_PARAM;
168     }
169 
170     int channelId = proxy->OpenAuthSession(sessionName, addrInfo);
171     if (channelId < SOFTBUS_OK) {
172         TRANS_LOGE(TRANS_SDK, "OpenAuthSession failed!");
173         return SOFTBUS_NO_INIT;
174     }
175     return channelId;
176 }
177 
ServerIpcNotifyAuthSuccess(int32_t channelId,int32_t channelType)178 int32_t ServerIpcNotifyAuthSuccess(int32_t channelId, int32_t channelType)
179 {
180     sptr<TransServerProxy> proxy = GetProxy();
181     TRANS_CHECK_AND_RETURN_RET_LOGE(
182         proxy != nullptr, SOFTBUS_NO_INIT, TRANS_SDK, "softbus server g_serverProxy is nullptr");
183 
184     return proxy->NotifyAuthSuccess(channelId, channelType);
185 }
186 
ServerIpcCloseChannel(const char * sessionName,int32_t channelId,int32_t channelType)187 int32_t ServerIpcCloseChannel(const char *sessionName, int32_t channelId, int32_t channelType)
188 {
189     sptr<TransServerProxy> proxy = GetProxy();
190     TRANS_CHECK_AND_RETURN_RET_LOGE(
191         proxy != nullptr, SOFTBUS_NO_INIT, TRANS_SDK, "softbus server g_serverProxy is nullptr");
192 
193     if (channelId < SOFTBUS_OK) {
194         TRANS_LOGE(TRANS_SDK, "invalid channel Id!");
195         return SOFTBUS_INVALID_PARAM;
196     }
197 
198     return proxy->CloseChannel(sessionName, channelId, channelType);
199 }
200 
ServerIpcCloseChannelWithStatistics(int32_t channelId,int32_t channelType,uint64_t laneId,const void * dataInfo,uint32_t len)201 int32_t ServerIpcCloseChannelWithStatistics(int32_t channelId, int32_t channelType, uint64_t laneId,
202     const void *dataInfo, uint32_t len)
203 {
204     if (dataInfo == nullptr) {
205         TRANS_LOGE(TRANS_SDK, "invalid param");
206         return SOFTBUS_INVALID_PARAM;
207     }
208     sptr<TransServerProxy> proxy = GetProxy();
209     TRANS_CHECK_AND_RETURN_RET_LOGE(
210         proxy != nullptr, SOFTBUS_NO_INIT, TRANS_SDK, "softbus server g_serverProxy is nullptr");
211 
212     if (channelId < MIN_CHANNEL_ID) {
213         TRANS_LOGE(TRANS_SDK, "invalid channelId=%{public}d", channelId);
214         return SOFTBUS_INVALID_PARAM;
215     }
216 
217     return proxy->CloseChannelWithStatistics(channelId, channelType, laneId, dataInfo, len);
218 }
219 
ServerIpcReleaseResources(int32_t channelId)220 int32_t ServerIpcReleaseResources(int32_t channelId)
221 {
222     sptr<TransServerProxy> proxy = GetProxy();
223     TRANS_CHECK_AND_RETURN_RET_LOGE(
224         proxy != nullptr, SOFTBUS_NO_INIT, TRANS_SDK, "softbus server g_serverProxy is nullptr");
225 
226     if ((channelId < MIN_CHANNEL_ID) || (channelId > MAX_CHANNEL_ID)) {
227         TRANS_LOGE(TRANS_SDK, "channelId=%{public}d is invalid.", channelId);
228         return SOFTBUS_TRANS_INVALID_CHANNEL_ID;
229     }
230     return proxy->ReleaseResources(channelId);
231 }
232 
ServerIpcSendMessage(int32_t channelId,int32_t channelType,const void * data,uint32_t len,int32_t msgType)233 int32_t ServerIpcSendMessage(int32_t channelId, int32_t channelType, const void *data, uint32_t len, int32_t msgType)
234 {
235     TRANS_CHECK_AND_RETURN_RET_LOGE(
236         g_serverProxy != nullptr, SOFTBUS_NO_INIT, TRANS_SDK, "softbus server g_serverProxy is nullptr");
237 
238     return g_serverProxy->SendMessage(channelId, channelType, data, len, msgType);
239 }
240 
ServerIpcQosReport(int32_t channelId,int32_t chanType,int32_t appType,int32_t quality)241 int32_t ServerIpcQosReport(int32_t channelId, int32_t chanType, int32_t appType, int32_t quality)
242 {
243     sptr<TransServerProxy> proxy = GetProxy();
244     TRANS_CHECK_AND_RETURN_RET_LOGE(
245         proxy != nullptr, SOFTBUS_NO_INIT, TRANS_SDK, "softbus server g_serverProxy is nullptr");
246 
247     return proxy->QosReport(channelId, chanType, appType, quality);
248 }
249 
ServerIpcStreamStats(int32_t channelId,int32_t channelType,const StreamSendStats * data)250 int32_t ServerIpcStreamStats(int32_t channelId, int32_t channelType, const StreamSendStats *data)
251 {
252     sptr<TransServerProxy> proxy = GetProxy();
253     TRANS_CHECK_AND_RETURN_RET_LOGE(
254         proxy != nullptr, SOFTBUS_NO_INIT, TRANS_SDK, "softbus server g_serverProxy is nullptr");
255 
256     return proxy->StreamStats(channelId, channelType, data);
257 }
258 
ServerIpcRippleStats(int32_t channelId,int32_t channelType,const TrafficStats * data)259 int32_t ServerIpcRippleStats(int32_t channelId, int32_t channelType, const TrafficStats *data)
260 {
261     sptr<TransServerProxy> proxy = GetProxy();
262     TRANS_CHECK_AND_RETURN_RET_LOGE(
263         proxy != nullptr, SOFTBUS_NO_INIT, TRANS_SDK, "softbus server g_serverProxy is nullptr");
264 
265     return proxy->RippleStats(channelId, channelType, data);
266 }
267 
ServerIpcGrantPermission(int uid,int pid,const char * sessionName)268 int32_t ServerIpcGrantPermission(int uid, int pid, const char *sessionName)
269 {
270     sptr<TransServerProxy> proxy = GetProxy();
271     TRANS_CHECK_AND_RETURN_RET_LOGE(
272         proxy != nullptr, SOFTBUS_NO_INIT, TRANS_SDK, "softbus server g_serverProxy is nullptr");
273 
274     if (sessionName == nullptr) {
275         TRANS_LOGE(TRANS_SDK, "sessionName is nullptr");
276         return SOFTBUS_INVALID_PARAM;
277     }
278 
279     return proxy->GrantPermission(uid, pid, sessionName);
280 }
281 
ServerIpcRemovePermission(const char * sessionName)282 int32_t ServerIpcRemovePermission(const char *sessionName)
283 {
284     sptr<TransServerProxy> proxy = GetProxy();
285     TRANS_CHECK_AND_RETURN_RET_LOGE(
286         proxy != nullptr, SOFTBUS_NO_INIT, TRANS_SDK, "softbus server g_serverProxy is nullptr");
287 
288     if (sessionName == nullptr) {
289         TRANS_LOGE(TRANS_SDK, "sessionName is nullptr");
290         return SOFTBUS_INVALID_PARAM;
291     }
292 
293     return proxy->RemovePermission(sessionName);
294 }
295 
ServerIpcEvaluateQos(const char * peerNetworkId,TransDataType dataType,const QosTV * qos,uint32_t qosCount)296 int32_t ServerIpcEvaluateQos(const char *peerNetworkId, TransDataType dataType, const QosTV *qos, uint32_t qosCount)
297 {
298     sptr<TransServerProxy> proxy = GetProxy();
299     TRANS_CHECK_AND_RETURN_RET_LOGE(
300         proxy != nullptr, SOFTBUS_NO_INIT, TRANS_SDK, "softbus server g_serverProxy is nullptr");
301 
302     if (peerNetworkId == NULL || dataType >= DATA_TYPE_BUTT || qosCount > QOS_TYPE_BUTT) {
303         TRANS_LOGE(TRANS_SDK, "invalid param");
304         return SOFTBUS_INVALID_PARAM;
305     }
306 
307     return proxy->EvaluateQos(peerNetworkId, dataType, qos, qosCount);
308 }
309