• 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_ipc_def.h"
26 #include "softbus_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     int32_t err = samgr->SendRequest(g_getSystemAbilityId, data, reply, option);
50     if (err != 0) {
51         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "Get GetSystemAbility failed!\n");
52         return nullptr;
53     }
54     return reply.ReadRemoteObject();
55 }
56 
TransServerProxyInit(void)57 int32_t TransServerProxyInit(void)
58 {
59     std::lock_guard<std::mutex> lock(g_mutex);
60     if (g_serverProxy != nullptr) {
61         return SOFTBUS_OK;
62     }
63 
64     sptr<IRemoteObject> object = GetSystemAbility();
65     if (object == nullptr) {
66         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "Get remote softbus object failed!\n");
67         return SOFTBUS_ERR;
68     }
69     g_serverProxy = new (std::nothrow) TransServerProxy(object);
70     if (g_serverProxy == nullptr) {
71         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "Create trans server proxy failed!\n");
72         return SOFTBUS_ERR;
73     }
74     return SOFTBUS_OK;
75 }
76 
TransServerProxyDeInit(void)77 void TransServerProxyDeInit(void)
78 {
79     if (g_serverProxy != nullptr) {
80         delete g_serverProxy;
81     }
82     g_serverProxy = nullptr;
83 }
84 
ServerIpcCreateSessionServer(const char * pkgName,const char * sessionName)85 int32_t ServerIpcCreateSessionServer(const char *pkgName, const char *sessionName)
86 {
87     if (g_serverProxy == nullptr) {
88         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "softbus server g_serverProxy is nullptr!\n");
89         return SOFTBUS_ERR;
90     }
91     if ((pkgName == nullptr) || (sessionName == nullptr)) {
92         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "pkgName or sessionName is nullptr!\n");
93         return SOFTBUS_ERR;
94     }
95     return g_serverProxy->CreateSessionServer(pkgName, sessionName);
96 }
97 
ServerIpcRemoveSessionServer(const char * pkgName,const char * sessionName)98 int32_t ServerIpcRemoveSessionServer(const char *pkgName, const char *sessionName)
99 {
100     if (g_serverProxy == nullptr) {
101         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "softbus server g_serverProxy is nullptr!\n");
102         return SOFTBUS_ERR;
103     }
104     if ((pkgName == nullptr) || (sessionName == nullptr)) {
105         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "pkgName or sessionName is nullptr!\n");
106         return SOFTBUS_ERR;
107     }
108     return g_serverProxy->RemoveSessionServer(pkgName, sessionName);
109 }
110 
ServerIpcOpenSession(const SessionParam * param,TransInfo * info)111 int32_t ServerIpcOpenSession(const SessionParam *param, TransInfo *info)
112 {
113     if (g_serverProxy == nullptr) {
114         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "softbus server g_serverProxy is nullptr!\n");
115         return SOFTBUS_NO_INIT;
116     }
117     if ((param->sessionName == nullptr) || (param->peerSessionName == nullptr) ||
118         (param->peerDeviceId == nullptr) || (param->groupId == nullptr) || (param->attr == nullptr)) {
119         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "parameter is nullptr!\n");
120         return SOFTBUS_INVALID_PARAM;
121     }
122     int ret = g_serverProxy->OpenSession(param, info);
123     if (ret < SOFTBUS_OK) {
124         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "OpenSession failed! ret=%d.\n", ret);
125         return ret;
126     }
127     return ret;
128 }
129 
ServerIpcOpenAuthSession(const char * sessionName,const ConnectionAddr * addrInfo)130 int32_t ServerIpcOpenAuthSession(const char *sessionName, const ConnectionAddr *addrInfo)
131 {
132     if (g_serverProxy == nullptr) {
133         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "softbus server g_serverProxy is nullptr!\n");
134         return SOFTBUS_ERR;
135     }
136     if ((sessionName == nullptr) || (addrInfo == nullptr)) {
137         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "parameter is nullptr!\n");
138         return SOFTBUS_ERR;
139     }
140     int channelId = g_serverProxy->OpenAuthSession(sessionName, addrInfo);
141     if (channelId < SOFTBUS_OK) {
142         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "OpenAuthSession failed!\n");
143         return SOFTBUS_ERR;
144     }
145     return channelId;
146 }
147 
ServerIpcNotifyAuthSuccess(int32_t channelId,int32_t channelType)148 int32_t ServerIpcNotifyAuthSuccess(int32_t channelId, int32_t channelType)
149 {
150     return g_serverProxy->NotifyAuthSuccess(channelId, channelType);
151 }
152 
ServerIpcCloseChannel(int32_t channelId,int32_t channelType)153 int32_t ServerIpcCloseChannel(int32_t channelId, int32_t channelType)
154 {
155     if (g_serverProxy == nullptr) {
156         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "softbus server g_serverProxy is nullptr!\n");
157         return SOFTBUS_ERR;
158     }
159     if (channelId < SOFTBUS_OK) {
160         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "invalid channel Id!\n");
161         return SOFTBUS_ERR;
162     }
163     return g_serverProxy->CloseChannel(channelId, channelType);
164 }
165 
ServerIpcSendMessage(int32_t channelId,int32_t channelType,const void * data,uint32_t len,int32_t msgType)166 int32_t ServerIpcSendMessage(int32_t channelId, int32_t channelType, const void *data, uint32_t len, int32_t msgType)
167 {
168     if (g_serverProxy == nullptr) {
169         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "softbus server g_serverProxy is nullptr!\n");
170         return SOFTBUS_ERR;
171     }
172 
173     return g_serverProxy->SendMessage(channelId, channelType, data, len, msgType);
174 }
175 
ServerIpcQosReport(int32_t channelId,int32_t chanType,int32_t appType,int32_t quality)176 int32_t ServerIpcQosReport(int32_t channelId, int32_t chanType, int32_t appType, int32_t quality)
177 {
178     if (g_serverProxy == nullptr) {
179         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "softbus server g_serverProxy is nullptr!\n");
180         return SOFTBUS_ERR;
181     }
182     return g_serverProxy->QosReport(channelId, chanType, appType, quality);
183 }
184 
ServerIpcStreamStats(int32_t channelId,int32_t channelType,const StreamSendStats * data)185 int32_t ServerIpcStreamStats(int32_t channelId, int32_t channelType, const StreamSendStats *data)
186 {
187     if (g_serverProxy == nullptr) {
188         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "softbus server g_serverProxy is nullptr");
189         return SOFTBUS_ERR;
190     }
191     return g_serverProxy->StreamStats(channelId, channelType, data);
192 }
193 
ServerIpcRippleStats(int32_t channelId,int32_t channelType,const TrafficStats * data)194 int32_t ServerIpcRippleStats(int32_t channelId, int32_t channelType, const TrafficStats *data)
195 {
196     if (g_serverProxy == nullptr) {
197         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "softbus server g_serverProxy is nullptr");
198         return SOFTBUS_ERR;
199     }
200     return g_serverProxy->RippleStats(channelId, channelType, data);
201 }
202 
ServerIpcGrantPermission(int uid,int pid,const char * sessionName)203 int32_t ServerIpcGrantPermission(int uid, int pid, const char *sessionName)
204 {
205     if (g_serverProxy == nullptr) {
206         if (TransServerProxyInit() != SOFTBUS_OK) {
207             SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "grant permission g_serverProxy is nullptr!");
208             return SOFTBUS_ERR;
209         }
210     }
211     if (sessionName == nullptr) {
212         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "sessionName is nullptr");
213         return SOFTBUS_ERR;
214     }
215     return g_serverProxy->GrantPermission(uid, pid, sessionName);
216 }
217 
ServerIpcRemovePermission(const char * sessionName)218 int32_t ServerIpcRemovePermission(const char *sessionName)
219 {
220     if (g_serverProxy == nullptr) {
221         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "softbus server g_serverProxy is nullptr!");
222         return SOFTBUS_ERR;
223     }
224     if (sessionName == nullptr) {
225         SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "sessionName is nullptr");
226         return SOFTBUS_ERR;
227     }
228     return g_serverProxy->RemovePermission(sessionName);
229 }