• 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 <mutex>
20 #include "ipc_skeleton.h"
21 #include "iremote_broker.h"
22 #include "iremote_object.h"
23 #include "iremote_proxy.h"
24 #include "softbus_errcode.h"
25 #include "softbus_server_ipc_interface_code.h"
26 #include "trans_log.h"
27 
28 using namespace OHOS;
29 
30 namespace {
31 sptr<TransServerProxy> g_serverProxy = nullptr;
32 const std::u16string SAMANAGER_INTERFACE_TOKEN = u"ohos.samgr.accessToken";
33 uint32_t g_getSystemAbilityId = 2;
34 std::mutex g_mutex;
35 }
36 
GetSystemAbility()37 static sptr<IRemoteObject> GetSystemAbility()
38 {
39     MessageParcel data;
40 
41     if (!data.WriteInterfaceToken(SAMANAGER_INTERFACE_TOKEN)) {
42         return nullptr;
43     }
44 
45     data.WriteInt32(SOFTBUS_SERVER_SA_ID_INNER);
46     MessageParcel reply;
47     MessageOption option;
48     sptr<IRemoteObject> samgr = IPCSkeleton::GetContextObject();
49     if (samgr == nullptr) {
50         TRANS_LOGE(TRANS_SDK, "Get samgr failed!");
51         return nullptr;
52     }
53     int32_t err = samgr->SendRequest(g_getSystemAbilityId, data, reply, option);
54     if (err != 0) {
55         TRANS_LOGE(TRANS_SDK, "Get GetSystemAbility failed!");
56         return nullptr;
57     }
58     return reply.ReadRemoteObject();
59 }
60 
TransServerProxyInit(void)61 int32_t TransServerProxyInit(void)
62 {
63     std::lock_guard<std::mutex> lock(g_mutex);
64     if (g_serverProxy != nullptr) {
65         TRANS_LOGI(TRANS_SDK, "Init success");
66         return SOFTBUS_OK;
67     }
68 
69     sptr<IRemoteObject> object = GetSystemAbility();
70     if (object == nullptr) {
71         TRANS_LOGE(TRANS_SDK, "Get remote softbus object failed!");
72         return SOFTBUS_ERR;
73     }
74     g_serverProxy = new (std::nothrow) TransServerProxy(object);
75     if (g_serverProxy == nullptr) {
76         TRANS_LOGE(TRANS_SDK, "Create trans server proxy failed!");
77         return SOFTBUS_ERR;
78     }
79     return SOFTBUS_OK;
80 }
81 
TransServerProxyDeInit(void)82 void TransServerProxyDeInit(void)
83 {
84     TRANS_LOGI(TRANS_SDK, "enter");
85     g_serverProxy.clear();
86 }
87 
ServerIpcCreateSessionServer(const char * pkgName,const char * sessionName)88 int32_t ServerIpcCreateSessionServer(const char *pkgName, const char *sessionName)
89 {
90     if (g_serverProxy == nullptr) {
91         TRANS_LOGE(TRANS_SDK, "softbus server g_serverProxy is nullptr!");
92         return SOFTBUS_ERR;
93     }
94     if ((pkgName == nullptr) || (sessionName == nullptr)) {
95         TRANS_LOGE(TRANS_SDK, "pkgName or sessionName is nullptr!");
96         return SOFTBUS_ERR;
97     }
98     return g_serverProxy->CreateSessionServer(pkgName, sessionName);
99 }
100 
ServerIpcRemoveSessionServer(const char * pkgName,const char * sessionName)101 int32_t ServerIpcRemoveSessionServer(const char *pkgName, const char *sessionName)
102 {
103     if (g_serverProxy == nullptr) {
104         TRANS_LOGE(TRANS_SDK, "softbus server g_serverProxy is nullptr!");
105         return SOFTBUS_ERR;
106     }
107     if ((pkgName == nullptr) || (sessionName == nullptr)) {
108         TRANS_LOGE(TRANS_SDK, "pkgName or sessionName is nullptr!");
109         return SOFTBUS_ERR;
110     }
111     return g_serverProxy->RemoveSessionServer(pkgName, sessionName);
112 }
113 
ServerIpcOpenSession(const SessionParam * param,TransInfo * info)114 int32_t ServerIpcOpenSession(const SessionParam *param, TransInfo *info)
115 {
116     if (g_serverProxy == nullptr) {
117         TRANS_LOGE(TRANS_SDK, "softbus server g_serverProxy is nullptr!");
118         return SOFTBUS_NO_INIT;
119     }
120     if ((param->sessionName == nullptr) || (param->peerSessionName == nullptr) ||
121         (param->peerDeviceId == nullptr) || (param->groupId == nullptr) || (param->attr == nullptr)) {
122         TRANS_LOGE(TRANS_SDK, "parameter is nullptr!");
123         return SOFTBUS_INVALID_PARAM;
124     }
125     int ret = g_serverProxy->OpenSession(param, info);
126     if (ret < SOFTBUS_OK) {
127         TRANS_LOGE(TRANS_SDK, "OpenSession failed! ret=%{public}d.", ret);
128         return ret;
129     }
130     return ret;
131 }
132 
ServerIpcOpenAuthSession(const char * sessionName,const ConnectionAddr * addrInfo)133 int32_t ServerIpcOpenAuthSession(const char *sessionName, const ConnectionAddr *addrInfo)
134 {
135     if (g_serverProxy == nullptr) {
136         TRANS_LOGE(TRANS_SDK, "softbus server g_serverProxy is nullptr!");
137         return SOFTBUS_ERR;
138     }
139     if ((sessionName == nullptr) || (addrInfo == nullptr)) {
140         TRANS_LOGE(TRANS_SDK, "parameter is nullptr!");
141         return SOFTBUS_ERR;
142     }
143     int channelId = g_serverProxy->OpenAuthSession(sessionName, addrInfo);
144     if (channelId < SOFTBUS_OK) {
145         TRANS_LOGE(TRANS_SDK, "OpenAuthSession failed!");
146         return SOFTBUS_ERR;
147     }
148     return channelId;
149 }
150 
ServerIpcNotifyAuthSuccess(int32_t channelId,int32_t channelType)151 int32_t ServerIpcNotifyAuthSuccess(int32_t channelId, int32_t channelType)
152 {
153     return g_serverProxy->NotifyAuthSuccess(channelId, channelType);
154 }
155 
ServerIpcCloseChannel(int32_t channelId,int32_t channelType)156 int32_t ServerIpcCloseChannel(int32_t channelId, int32_t channelType)
157 {
158     if (g_serverProxy == nullptr) {
159         TRANS_LOGE(TRANS_SDK, "softbus server g_serverProxy is nullptr!");
160         return SOFTBUS_ERR;
161     }
162     if (channelId < SOFTBUS_OK) {
163         TRANS_LOGE(TRANS_SDK, "invalid channel Id!");
164         return SOFTBUS_ERR;
165     }
166     return g_serverProxy->CloseChannel(channelId, channelType);
167 }
168 
ServerIpcSendMessage(int32_t channelId,int32_t channelType,const void * data,uint32_t len,int32_t msgType)169 int32_t ServerIpcSendMessage(int32_t channelId, int32_t channelType, const void *data, uint32_t len, int32_t msgType)
170 {
171     if (g_serverProxy == nullptr) {
172         TRANS_LOGE(TRANS_SDK, "softbus server g_serverProxy is nullptr!");
173         return SOFTBUS_ERR;
174     }
175 
176     return g_serverProxy->SendMessage(channelId, channelType, data, len, msgType);
177 }
178 
ServerIpcQosReport(int32_t channelId,int32_t chanType,int32_t appType,int32_t quality)179 int32_t ServerIpcQosReport(int32_t channelId, int32_t chanType, int32_t appType, int32_t quality)
180 {
181     if (g_serverProxy == nullptr) {
182         TRANS_LOGE(TRANS_QOS, "softbus server g_serverProxy is nullptr!");
183         return SOFTBUS_ERR;
184     }
185     return g_serverProxy->QosReport(channelId, chanType, appType, quality);
186 }
187 
ServerIpcStreamStats(int32_t channelId,int32_t channelType,const StreamSendStats * data)188 int32_t ServerIpcStreamStats(int32_t channelId, int32_t channelType, const StreamSendStats *data)
189 {
190     if (g_serverProxy == nullptr) {
191         TRANS_LOGE(TRANS_STREAM, "softbus server g_serverProxy is nullptr");
192         return SOFTBUS_ERR;
193     }
194     return g_serverProxy->StreamStats(channelId, channelType, data);
195 }
196 
ServerIpcRippleStats(int32_t channelId,int32_t channelType,const TrafficStats * data)197 int32_t ServerIpcRippleStats(int32_t channelId, int32_t channelType, const TrafficStats *data)
198 {
199     if (g_serverProxy == nullptr) {
200         TRANS_LOGE(TRANS_SDK, "softbus server g_serverProxy is nullptr");
201         return SOFTBUS_ERR;
202     }
203     return g_serverProxy->RippleStats(channelId, channelType, data);
204 }
205 
ServerIpcGrantPermission(int uid,int pid,const char * sessionName)206 int32_t ServerIpcGrantPermission(int uid, int pid, const char *sessionName)
207 {
208     if (g_serverProxy == nullptr) {
209         if (TransServerProxyInit() != SOFTBUS_OK) {
210             TRANS_LOGE(TRANS_SDK, "grant permission g_serverProxy is nullptr!");
211             return SOFTBUS_ERR;
212         }
213     }
214     if (sessionName == nullptr) {
215         TRANS_LOGE(TRANS_SDK, "sessionName is nullptr");
216         return SOFTBUS_ERR;
217     }
218     return g_serverProxy->GrantPermission(uid, pid, sessionName);
219 }
220 
ServerIpcRemovePermission(const char * sessionName)221 int32_t ServerIpcRemovePermission(const char *sessionName)
222 {
223     if (g_serverProxy == nullptr) {
224         TRANS_LOGE(TRANS_SDK, "softbus server g_serverProxy is nullptr!");
225         return SOFTBUS_ERR;
226     }
227     if (sessionName == nullptr) {
228         TRANS_LOGE(TRANS_SDK, "sessionName is nullptr");
229         return SOFTBUS_ERR;
230     }
231     return g_serverProxy->RemovePermission(sessionName);
232 }
233 
ServerIpcEvaluateQos(const char * peerNetworkId,TransDataType dataType,const QosTV * qos,uint32_t qosCount)234 int32_t ServerIpcEvaluateQos(const char *peerNetworkId, TransDataType dataType, const QosTV *qos, uint32_t qosCount)
235 {
236     if (g_serverProxy == nullptr) {
237         TRANS_LOGE(TRANS_SDK, "softbus server g_serverProxy is nullptr!");
238         return SOFTBUS_NO_INIT;
239     }
240 
241     if (peerNetworkId == NULL || dataType >= DATA_TYPE_BUTT || qosCount > QOS_TYPE_BUTT) {
242         TRANS_LOGE(TRANS_SDK, "invalid param");
243         return SOFTBUS_INVALID_PARAM;
244     }
245 
246     return g_serverProxy->EvaluateQos(peerNetworkId, dataType, qos, qosCount);
247 }
248 
TransBroadCastReInit(void)249 void TransBroadCastReInit(void)
250 {
251     TRANS_LOGI(TRANS_SDK, "server died, try to ReRegistereventLisenter");
252     TransBroadCastInit();
253 }
254