1 /*
2 * Copyright (c) 2021-2025 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 "softbus_server.h"
17
18 #include "br_proxy_server_manager.h"
19 #include "bus_center_ex_obj.h"
20 #include "ipc_skeleton.h"
21 #include "lnn_bus_center_ipc.h"
22 #include "securec.h"
23 #include "string_ex.h"
24 #include "softbus_client_info_manager.h"
25 #include "legacy/softbus_hidumper_interface.h"
26 #include "softbus_server_death_recipient.h"
27 #include "softbus_server_frame.h"
28 #include "softbus_utils.h"
29 #include "system_ability_definition.h"
30 #include "trans_channel_manager.h"
31 #include "trans_session_service.h"
32 #include "trans_spec_object.h"
33 #include "lnn_lane_interface.h"
34 #include "general_connection_client_proxy.h"
35 #include "softbus_conn_general_connection.h"
36
37 #ifdef SUPPORT_BUNDLENAME
38 #include "bundle_mgr_proxy.h"
39 #include "iservice_registry.h"
40 #include "system_ability_definition.h"
41 #endif
42
43 #define SOFTBUS_IPC_THREAD_NUM 32
44 #define OPEN_AUTH_BR_CONNECT_TIMEOUT_MILLIS (15 * 1000)
45
46 namespace OHOS {
47 REGISTER_SYSTEM_ABILITY_BY_ID(SoftBusServer, SOFTBUS_SERVER_SA_ID, true);
48
ConvertConnectType(ConnectionAddrType type)49 static ConnectType ConvertConnectType(ConnectionAddrType type)
50 {
51 switch (type) {
52 case CONNECTION_ADDR_BR:
53 return CONNECT_BR;
54 case CONNECTION_ADDR_BLE:
55 return CONNECT_BLE;
56 case CONNECTION_ADDR_ETH:
57 return CONNECT_TCP;
58 case CONNECTION_ADDR_WLAN:
59 case CONNECTION_ADDR_NCM:
60 return CONNECT_TCP;
61 default:
62 return CONNECT_TYPE_MAX;
63 }
64 }
65
SoftBusServer(int32_t saId,bool runOnCreate)66 SoftBusServer::SoftBusServer(int32_t saId, bool runOnCreate) : SystemAbility(saId, runOnCreate)
67 {
68 }
69
SoftbusRegisterService(const char * clientPkgName,const sptr<IRemoteObject> & object)70 int32_t SoftBusServer::SoftbusRegisterService(const char *clientPkgName, const sptr<IRemoteObject> &object)
71 {
72 if (clientPkgName == nullptr || object == nullptr) {
73 COMM_LOGE(COMM_SVC, "package name or object is nullptr");
74 return SOFTBUS_INVALID_PARAM;
75 }
76 int32_t pid = (int32_t)(OHOS::IPCSkeleton::GetCallingPid());
77 if (SoftbusClientInfoManager::GetInstance().SoftbusClientIsExist(clientPkgName, pid)) {
78 COMM_LOGW(COMM_SVC, "softbus client is exist");
79 return SOFTBUS_OK;
80 }
81 sptr<IRemoteObject::DeathRecipient> abilityDeath = new (std::nothrow) SoftBusDeathRecipient();
82 if (abilityDeath == nullptr) {
83 COMM_LOGE(COMM_SVC, "DeathRecipient object is nullptr");
84 return SOFTBUS_TRANS_DEATH_RECIPIENT_INVALID;
85 }
86 bool ret = object->AddDeathRecipient(abilityDeath);
87 if (!ret) {
88 COMM_LOGE(COMM_SVC, "AddDeathRecipient failed");
89 return SOFTBUS_TRANS_ADD_DEATH_RECIPIENT_FAILED;
90 }
91 if (SoftbusClientInfoManager::GetInstance().SoftbusAddService(clientPkgName,
92 object, abilityDeath, pid) != SOFTBUS_OK) {
93 COMM_LOGE(COMM_SVC, "softbus add client service failed");
94 return SOFTBUS_TRANS_ADD_CLIENT_SERVICE_FAILED;
95 }
96 COMM_LOGI(COMM_SVC, "softbus register service success. clientPkgName=%{public}s", clientPkgName);
97 return SOFTBUS_OK;
98 }
99
CreateSessionServer(const char * pkgName,const char * sessionName,uint64_t timestamp)100 int32_t SoftBusServer::CreateSessionServer(const char *pkgName, const char *sessionName, uint64_t timestamp)
101 {
102 (void)timestamp;
103 pid_t callingUid = OHOS::IPCSkeleton::GetCallingUid();
104 pid_t callingPid = OHOS::IPCSkeleton::GetCallingPid();
105 return TransCreateSessionServer(pkgName, sessionName, (int32_t)callingUid, (int32_t)callingPid);
106 }
107
RemoveSessionServer(const char * pkgName,const char * sessionName,uint64_t timestamp)108 int32_t SoftBusServer::RemoveSessionServer(const char *pkgName, const char *sessionName, uint64_t timestamp)
109 {
110 (void)timestamp;
111 return TransRemoveSessionServer(pkgName, sessionName);
112 }
113
OpenSession(const SessionParam * param,TransInfo * info)114 int32_t SoftBusServer::OpenSession(const SessionParam *param, TransInfo *info)
115 {
116 return TransOpenSession(param, info);
117 }
118
IsNcmAddrType(ConnectionAddrType addrType)119 static bool IsNcmAddrType(ConnectionAddrType addrType)
120 {
121 return (addrType == CONNECTION_ADDR_NCM) ? true : false;
122 }
123
OpenAuthSession(const char * sessionName,const ConnectionAddr * addrInfo)124 int32_t SoftBusServer::OpenAuthSession(const char *sessionName, const ConnectionAddr *addrInfo)
125 {
126 if (sessionName == nullptr || addrInfo == nullptr) {
127 COMM_LOGE(COMM_SVC, "session name or addrinfo is nullptr");
128 return SOFTBUS_INVALID_PARAM;
129 }
130 ConnectOption connOpt;
131 (void)memset_s(&connOpt, sizeof(ConnectOption), 0, sizeof(ConnectOption));
132 ConnectParam param;
133 (void)memset_s(¶m, sizeof(ConnectParam), 0, sizeof(ConnectParam));
134 connOpt.type = ConvertConnectType(addrInfo->type);
135 switch (connOpt.type) {
136 case CONNECT_TCP:
137 if (memcpy_s(connOpt.socketOption.addr, sizeof(connOpt.socketOption.addr), addrInfo->info.ip.ip, IP_LEN) !=
138 EOK) {
139 COMM_LOGE(COMM_SVC, "connect TCP memory error");
140 return SOFTBUS_MEM_ERR;
141 }
142 connOpt.socketOption.port = static_cast<int32_t>(addrInfo->info.ip.port);
143 connOpt.socketOption.protocol = IsNcmAddrType(addrInfo->type) ? LNN_PROTOCOL_USB : LNN_PROTOCOL_IP;
144 connOpt.socketOption.moduleId = IsNcmAddrType(addrInfo->type) ? AUTH_USB : AUTH;
145 break;
146 case CONNECT_BLE:
147 if (memcpy_s(connOpt.bleOption.bleMac, BT_MAC_LEN, addrInfo->info.ble.bleMac, BT_MAC_LEN) != EOK) {
148 COMM_LOGE(COMM_SVC, "connect BLE memory error");
149 return SOFTBUS_MEM_ERR;
150 }
151 if (memcpy_s(connOpt.bleOption.deviceIdHash, sizeof(connOpt.bleOption.deviceIdHash),
152 addrInfo->info.ble.udidHash, sizeof(addrInfo->info.ble.udidHash)) != EOK) {
153 COMM_LOGE(COMM_SVC, "connect BLE memory error");
154 return SOFTBUS_MEM_ERR;
155 }
156 connOpt.bleOption.protocol = addrInfo->info.ble.protocol;
157 connOpt.bleOption.psm = addrInfo->info.ble.psm;
158 connOpt.bleOption.fastestConnectEnable = true;
159 param.blePriority = addrInfo->info.ble.priority;
160 break;
161 case CONNECT_BR:
162 if (memcpy_s(connOpt.brOption.brMac, BT_MAC_LEN, addrInfo->info.br.brMac, BT_MAC_LEN) != EOK) {
163 COMM_LOGE(COMM_SVC, "connect BR memory error");
164 return SOFTBUS_MEM_ERR;
165 }
166 connOpt.brOption.waitTimeoutDelay = OPEN_AUTH_BR_CONNECT_TIMEOUT_MILLIS;
167 break;
168 default:
169 COMM_LOGE(COMM_SVC, "connect type error");
170 return SOFTBUS_TRANS_INVALID_CONNECT_TYPE;
171 }
172 return TransOpenAuthChannel(sessionName, &connOpt, "", ¶m);
173 }
174
NotifyAuthSuccess(int32_t channelId,int32_t channelType)175 int32_t SoftBusServer::NotifyAuthSuccess(int32_t channelId, int32_t channelType)
176 {
177 return TransNotifyAuthSuccess(channelId, channelType);
178 }
179
ReleaseResources(int32_t channelId)180 int32_t SoftBusServer::ReleaseResources(int32_t channelId)
181 {
182 return TransReleaseUdpResources(channelId);
183 }
184
CloseChannel(const char * sessionName,int32_t channelId,int32_t channelType)185 int32_t SoftBusServer::CloseChannel(const char *sessionName, int32_t channelId, int32_t channelType)
186 {
187 return TransCloseChannel(sessionName, channelId, channelType);
188 }
189
CloseChannelWithStatistics(int32_t channelId,int32_t channelType,uint64_t laneId,const void * dataInfo,uint32_t len)190 int32_t SoftBusServer::CloseChannelWithStatistics(int32_t channelId, int32_t channelType, uint64_t laneId,
191 const void *dataInfo, uint32_t len)
192 {
193 return TransCloseChannelWithStatistics(channelId, channelType, laneId, dataInfo, len);
194 }
195
SendMessage(int32_t channelId,int32_t channelType,const void * data,uint32_t len,int32_t msgType)196 int32_t SoftBusServer::SendMessage(int32_t channelId, int32_t channelType, const void *data,
197 uint32_t len, int32_t msgType)
198 {
199 return TransSendMsg(channelId, channelType, data, len, msgType);
200 }
201
JoinLNN(const char * pkgName,void * addr,uint32_t addrTypeLen,bool isForceJoin)202 int32_t SoftBusServer::JoinLNN(const char *pkgName, void *addr, uint32_t addrTypeLen, bool isForceJoin)
203 {
204 pid_t callingPid = OHOS::IPCSkeleton::GetCallingPid();
205 return LnnIpcServerJoin(pkgName, (int32_t)callingPid, addr, addrTypeLen, isForceJoin);
206 }
207
LeaveLNN(const char * pkgName,const char * networkId)208 int32_t SoftBusServer::LeaveLNN(const char *pkgName, const char *networkId)
209 {
210 pid_t callingPid = OHOS::IPCSkeleton::GetCallingPid();
211 return LnnIpcServerLeave(pkgName, (int32_t)callingPid, networkId);
212 }
213
GetAllOnlineNodeInfo(const char * pkgName,void ** info,uint32_t infoTypeLen,int * infoNum)214 int32_t SoftBusServer::GetAllOnlineNodeInfo(const char *pkgName, void **info, uint32_t infoTypeLen, int *infoNum)
215 {
216 return LnnIpcGetAllOnlineNodeInfo(pkgName, info, infoTypeLen, infoNum);
217 }
218
GetLocalDeviceInfo(const char * pkgName,void * info,uint32_t infoTypeLen)219 int32_t SoftBusServer::GetLocalDeviceInfo(const char *pkgName, void *info, uint32_t infoTypeLen)
220 {
221 return LnnIpcGetLocalDeviceInfo(pkgName, info, infoTypeLen);
222 }
223
GetNodeKeyInfo(const char * pkgName,const char * networkId,int32_t key,unsigned char * buf,uint32_t len)224 int32_t SoftBusServer::GetNodeKeyInfo(const char *pkgName, const char *networkId, int32_t key, unsigned char *buf,
225 uint32_t len)
226 {
227 return LnnIpcGetNodeKeyInfo(pkgName, networkId, key, buf, len);
228 }
229
SetNodeDataChangeFlag(const char * pkgName,const char * networkId,uint16_t dataChangeFlag)230 int32_t SoftBusServer::SetNodeDataChangeFlag(const char *pkgName, const char *networkId, uint16_t dataChangeFlag)
231 {
232 return LnnIpcSetNodeDataChangeFlag(pkgName, networkId, dataChangeFlag);
233 }
234
RegDataLevelChangeCb(const char * pkgName)235 int32_t SoftBusServer::RegDataLevelChangeCb(const char *pkgName)
236 {
237 int32_t callingPid = (int32_t)OHOS::IPCSkeleton::GetCallingPid();
238 return LnnIpcRegDataLevelChangeCb(pkgName, callingPid);
239 }
240
UnregDataLevelChangeCb(const char * pkgName)241 int32_t SoftBusServer::UnregDataLevelChangeCb(const char *pkgName)
242 {
243 int32_t callingPid = (int32_t)OHOS::IPCSkeleton::GetCallingPid();
244 return LnnIpcUnregDataLevelChangeCb(pkgName, callingPid);
245 }
246
SetDataLevel(const DataLevel * dataLevel)247 int32_t SoftBusServer::SetDataLevel(const DataLevel *dataLevel)
248 {
249 return LnnIpcSetDataLevel(dataLevel);
250 }
251
StartTimeSync(const char * pkgName,const char * targetNetworkId,int32_t accuracy,int32_t period)252 int32_t SoftBusServer::StartTimeSync(const char *pkgName, const char *targetNetworkId, int32_t accuracy,
253 int32_t period)
254 {
255 pid_t callingPid = OHOS::IPCSkeleton::GetCallingPid();
256 return LnnIpcStartTimeSync(pkgName, (int32_t)callingPid, targetNetworkId, accuracy, period);
257 }
258
StopTimeSync(const char * pkgName,const char * targetNetworkId)259 int32_t SoftBusServer::StopTimeSync(const char *pkgName, const char *targetNetworkId)
260 {
261 pid_t callingPid = OHOS::IPCSkeleton::GetCallingPid();
262 return LnnIpcStopTimeSync(pkgName, targetNetworkId, callingPid);
263 }
264
QosReport(int32_t channelId,int32_t chanType,int32_t appType,int32_t quality)265 int32_t SoftBusServer::QosReport(int32_t channelId, int32_t chanType, int32_t appType, int32_t quality)
266 {
267 return TransRequestQos(channelId, chanType, appType, quality);
268 }
269
StreamStats(int32_t channelId,int32_t channelType,const StreamSendStats * data)270 int32_t SoftBusServer::StreamStats(int32_t channelId, int32_t channelType, const StreamSendStats *data)
271 {
272 return TransStreamStats(channelId, channelType, data);
273 }
274
RippleStats(int32_t channelId,int32_t channelType,const TrafficStats * data)275 int32_t SoftBusServer::RippleStats(int32_t channelId, int32_t channelType, const TrafficStats *data)
276 {
277 return TransRippleStats(channelId, channelType, data);
278 }
279
PublishLNN(const char * pkgName,const PublishInfo * info)280 int32_t SoftBusServer::PublishLNN(const char *pkgName, const PublishInfo *info)
281 {
282 pid_t callingPid = OHOS::IPCSkeleton::GetCallingPid();
283 return LnnIpcPublishLNN(pkgName, (int32_t)callingPid, info);
284 }
285
StopPublishLNN(const char * pkgName,int32_t publishId)286 int32_t SoftBusServer::StopPublishLNN(const char *pkgName, int32_t publishId)
287 {
288 pid_t callingPid = OHOS::IPCSkeleton::GetCallingPid();
289 return LnnIpcStopPublishLNN(pkgName, (int32_t)callingPid, publishId);
290 }
291
RefreshLNN(const char * pkgName,const SubscribeInfo * info)292 int32_t SoftBusServer::RefreshLNN(const char *pkgName, const SubscribeInfo *info)
293 {
294 pid_t callingPid = OHOS::IPCSkeleton::GetCallingPid();
295 return LnnIpcRefreshLNN(pkgName, (int32_t)callingPid, info);
296 }
297
StopRefreshLNN(const char * pkgName,int32_t refreshId)298 int32_t SoftBusServer::StopRefreshLNN(const char *pkgName, int32_t refreshId)
299 {
300 pid_t callingPid = OHOS::IPCSkeleton::GetCallingPid();
301 return LnnIpcStopRefreshLNN(pkgName, (int32_t)callingPid, refreshId);
302 }
303
ActiveMetaNode(const MetaNodeConfigInfo * info,char * metaNodeId)304 int32_t SoftBusServer::ActiveMetaNode(const MetaNodeConfigInfo *info, char *metaNodeId)
305 {
306 return LnnIpcActiveMetaNode(info, metaNodeId);
307 }
308
DeactiveMetaNode(const char * metaNodeId)309 int32_t SoftBusServer::DeactiveMetaNode(const char *metaNodeId)
310 {
311 return LnnIpcDeactiveMetaNode(metaNodeId);
312 }
313
GetAllMetaNodeInfo(MetaNodeInfo * info,int32_t * infoNum)314 int32_t SoftBusServer::GetAllMetaNodeInfo(MetaNodeInfo *info, int32_t *infoNum)
315 {
316 return LnnIpcGetAllMetaNodeInfo(info, infoNum);
317 }
318
ShiftLNNGear(const char * pkgName,const char * callerId,const char * targetNetworkId,const GearMode * mode)319 int32_t SoftBusServer::ShiftLNNGear(const char *pkgName, const char *callerId, const char *targetNetworkId,
320 const GearMode *mode)
321 {
322 return LnnIpcShiftLNNGear(pkgName, callerId, targetNetworkId, mode);
323 }
324
TriggerRangeForMsdp(const char * pkgName,const RangeConfig * config)325 int32_t SoftBusServer::TriggerRangeForMsdp(const char *pkgName, const RangeConfig *config)
326 {
327 return LnnIpcTriggerRangeForMsdp(pkgName, config);
328 }
329
StopRangeForMsdp(const char * pkgName,const RangeConfig * config)330 int32_t SoftBusServer::StopRangeForMsdp(const char *pkgName, const RangeConfig *config)
331 {
332 return LnnIpcStopRangeForMsdp(pkgName, config);
333 }
334
RegisterRangeCallbackForMsdp(const char * pkgName)335 int32_t SoftBusServer::RegisterRangeCallbackForMsdp(const char *pkgName)
336 {
337 int32_t callingPid = (int32_t)OHOS::IPCSkeleton::GetCallingPid();
338 return LnnIpcRegRangeCbForMsdp(pkgName, callingPid);
339 }
340
UnregisterRangeCallbackForMsdp(const char * pkgName)341 int32_t SoftBusServer::UnregisterRangeCallbackForMsdp(const char *pkgName)
342 {
343 int32_t callingPid = (int32_t)OHOS::IPCSkeleton::GetCallingPid();
344 return LnnIpcUnregRangeCbForMsdp(pkgName, callingPid);
345 }
346
SyncTrustedRelationShip(const char * pkgName,const char * msg,uint32_t msgLen)347 int32_t SoftBusServer::SyncTrustedRelationShip(const char *pkgName, const char *msg, uint32_t msgLen)
348 {
349 return LnnIpcSyncTrustedRelationShip(pkgName, msg, msgLen);
350 }
351
Dump(int fd,const std::vector<std::u16string> & args)352 int SoftBusServer::Dump(int fd, const std::vector<std::u16string> &args)
353 {
354 if (fd < 0) {
355 COMM_LOGE(COMM_SVC, "hidumper fd is invalid");
356 return SOFTBUS_INVALID_PARAM;
357 }
358 std::vector<std::string> argsStr;
359 for (auto item : args) {
360 argsStr.emplace_back(Str16ToStr8(item));
361 }
362
363 int32_t argc = (int)argsStr.size();
364 const char *argv[argc];
365
366 for (int32_t i = 0; i < argc; i++) {
367 argv[i] = argsStr[i].c_str();
368 }
369
370 return SoftBusDumpProcess(fd, argc, argv);
371 }
372
OnStart()373 void SoftBusServer::OnStart()
374 {
375 COMM_LOGI(COMM_SVC, "SoftBusServer OnStart called!");
376 InitSoftBusServer();
377 if (!Publish(this)) {
378 COMM_LOGE(COMM_SVC, "SoftBusServer publish failed!");
379 }
380 IPCSkeleton::SetMaxWorkThreadNum(SOFTBUS_IPC_THREAD_NUM);
381 }
382
OnStop()383 void SoftBusServer::OnStop() {}
384
GetSoftbusSpecObject(sptr<IRemoteObject> & object)385 int32_t SoftBusServer::GetSoftbusSpecObject(sptr<IRemoteObject> &object)
386 {
387 static sptr<TransSpecObject> instance = nullptr;
388 static std::mutex instanceLock;
389 std::lock_guard<std::mutex> autoLock(instanceLock);
390 if (instance == nullptr) {
391 instance = new(std::nothrow) TransSpecObject();
392 if (instance == nullptr) {
393 return SOFTBUS_MEM_ERR;
394 }
395 }
396 object = instance;
397 return SOFTBUS_OK;
398 }
399
GetBusCenterExObj(sptr<IRemoteObject> & object)400 int32_t SoftBusServer::GetBusCenterExObj(sptr<IRemoteObject> &object)
401 {
402 COMM_LOGI(COMM_SVC, "SoftBusServer GetBusCenterExObj enter.");
403 sptr<BusCenterExObj> result = new(std::nothrow) BusCenterExObj();
404 if (result == nullptr) {
405 COMM_LOGE(COMM_SVC, "SoftBusServer GetBusCenterExObj failed!");
406 return SOFTBUS_MEM_ERR;
407 }
408 object = result;
409 return SOFTBUS_OK;
410 }
411
ConvertTransType(TransDataType dataType)412 static LaneTransType ConvertTransType(TransDataType dataType)
413 {
414 switch (dataType) {
415 case DATA_TYPE_MESSAGE:
416 return LANE_T_MSG;
417 case DATA_TYPE_BYTES:
418 return LANE_T_BYTE;
419 case DATA_TYPE_FILE:
420 return LANE_T_FILE;
421 case DATA_TYPE_RAW_STREAM:
422 return LANE_T_RAW_STREAM;
423 case DATA_TYPE_VIDEO_STREAM:
424 return LANE_T_COMMON_VIDEO;
425 case DATA_TYPE_AUDIO_STREAM:
426 return LANE_T_COMMON_VOICE;
427 case DATA_TYPE_SLICE_STREAM:
428 return LANE_T_RAW_STREAM;
429 default:
430 return LANE_T_BUTT;
431 }
432 }
433
ConvertQosInfo(const QosTV * qos,uint32_t qosCount,QosInfo * qosInfo)434 static void ConvertQosInfo(const QosTV *qos, uint32_t qosCount, QosInfo *qosInfo)
435 {
436 if (qos == NULL || qosCount == 0) {
437 return;
438 }
439
440 for (uint32_t i = 0; i < qosCount; i++) {
441 switch (qos[i].qos) {
442 case QOS_TYPE_MIN_BW:
443 qosInfo->minBW = qos[i].value;
444 break;
445 case QOS_TYPE_MAX_LATENCY:
446 qosInfo->maxLaneLatency = qos[i].value;
447 break;
448 case QOS_TYPE_MIN_LATENCY:
449 qosInfo->minLaneLatency = qos[i].value;
450 break;
451 default:
452 break;
453 }
454 }
455 }
456
EvaluateQos(const char * peerNetworkId,TransDataType dataType,const QosTV * qos,uint32_t qosCount)457 int32_t SoftBusServer::EvaluateQos(const char *peerNetworkId, TransDataType dataType, const QosTV *qos,
458 uint32_t qosCount)
459 {
460 if (!IsValidString(peerNetworkId, NETWORK_ID_BUF_LEN - 1) || dataType >= DATA_TYPE_BUTT
461 || qosCount > QOS_TYPE_BUTT) {
462 COMM_LOGE(COMM_SVC, "SoftBusServer invalid param");
463 return SOFTBUS_INVALID_PARAM;
464 }
465
466 LaneQueryInfo info;
467 (void)memset_s(&info, sizeof(LaneQueryInfo), 0, sizeof(LaneQueryInfo));
468 if (strcpy_s(info.networkId, NETWORK_ID_BUF_LEN, peerNetworkId) != EOK) {
469 COMM_LOGE(COMM_SVC, "STRCPY fail");
470 return SOFTBUS_STRCPY_ERR;
471 }
472 info.transType = ConvertTransType(dataType);
473
474 QosInfo qosInfo;
475 (void)memset_s(&qosInfo, sizeof(QosInfo), 0, sizeof(QosInfo));
476 ConvertQosInfo(qos, qosCount, &qosInfo);
477 return LnnQueryLaneResource(&info, &qosInfo);
478 }
479
ProcessInnerEvent(int32_t eventType,uint8_t * buf,uint32_t len)480 int32_t SoftBusServer::ProcessInnerEvent(int32_t eventType, uint8_t *buf, uint32_t len)
481 {
482 return TransProcessInnerEvent(eventType, buf, len);
483 }
484
PrivilegeCloseChannel(uint64_t tokenId,int32_t pid,const char * peerNetworkId)485 int32_t SoftBusServer::PrivilegeCloseChannel(uint64_t tokenId, int32_t pid, const char *peerNetworkId)
486 {
487 if (peerNetworkId == nullptr) {
488 COMM_LOGE(COMM_SVC, "invalid param");
489 return SOFTBUS_INVALID_PARAM;
490 }
491 return TransPrivilegeCloseChannel(tokenId, pid, peerNetworkId);
492 }
493
SetDisplayName(const char * pkgName,const char * nameData,uint32_t len)494 int32_t SoftBusServer::SetDisplayName(const char *pkgName, const char *nameData, uint32_t len)
495 {
496 return LnnIpcSetDisplayName(pkgName, nameData, len);
497 }
498
499 #ifdef SUPPORT_BUNDLENAME
FillBundleName(char * bundleNameStr,uint32_t size)500 static int32_t FillBundleName(char *bundleNameStr, uint32_t size)
501 {
502 pid_t callingUid = OHOS::IPCSkeleton::GetCallingUid();
503 std::string bundleName;
504 sptr<ISystemAbilityManager> systemAbilityManager =
505 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
506 if (systemAbilityManager == nullptr) {
507 COMM_LOGE(COMM_SVC, "Failed to get system ability manager.");
508 return SOFTBUS_TRANS_SYSTEM_ABILITY_MANAGER_FAILED;
509 }
510 sptr<IRemoteObject> remoteObject = systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
511 if (remoteObject == nullptr) {
512 COMM_LOGE(COMM_SVC, "Failed to get bundle manager service.");
513 return SOFTBUS_TRANS_GET_SYSTEM_ABILITY_FAILED;
514 }
515 sptr<AppExecFwk::IBundleMgr> bundleMgr = iface_cast<AppExecFwk::IBundleMgr>(remoteObject);
516 if (bundleMgr == nullptr) {
517 COMM_LOGE(COMM_SVC, "iface_cast failed");
518 return SOFTBUS_TRANS_GET_BUNDLE_MGR_FAILED;
519 }
520 if (bundleMgr->GetNameForUid(callingUid, bundleName) != SOFTBUS_OK) {
521 COMM_LOGE(COMM_SVC, "get bundleName failed");
522 return SOFTBUS_TRANS_GET_BUNDLENAME_FAILED;
523 }
524
525 if (strcpy_s(bundleNameStr, size, bundleName.c_str()) != EOK) {
526 COMM_LOGE(COMM_SVC, "copy name failed");
527 return SOFTBUS_STRCPY_ERR;
528 }
529 return SOFTBUS_OK;
530 }
531 #endif
532
CreateServer(const char * pkgName,const char * name)533 int32_t SoftBusServer::CreateServer(const char *pkgName, const char *name)
534 {
535 GeneralConnectionParam param = {0};
536 param.pid = OHOS::IPCSkeleton::GetCallingPid();
537 if (strcpy_s(param.name, GENERAL_NAME_LEN, name) != EOK) {
538 COMM_LOGE(COMM_SVC, "copy name failed");
539 return SOFTBUS_STRCPY_ERR;
540 }
541 if (strcpy_s(param.pkgName, PKG_NAME_SIZE_MAX, pkgName) != EOK) {
542 COMM_LOGE(COMM_SVC, "copy pkgName failed");
543 return SOFTBUS_STRCPY_ERR;
544 }
545 if (strcpy_s(param.name, GENERAL_NAME_LEN, name) != EOK) {
546 COMM_LOGE(COMM_SVC, "copy name failed");
547 return SOFTBUS_STRCPY_ERR;
548 }
549 int32_t ret = SOFTBUS_OK;
550 #ifdef SUPPORT_BUNDLENAME
551 ret = FillBundleName(param.bundleName, BUNDLE_NAME_MAX);
552 if (ret != SOFTBUS_OK) {
553 COMM_LOGE(COMM_SVC, "get bundle name failed");
554 return ret;
555 }
556 #endif
557 GeneralConnectionManager *manager = GetGeneralConnectionManager();
558 if (manager == nullptr || manager->createServer == nullptr) {
559 COMM_LOGE(COMM_SVC, "invalid param");
560 return SOFTBUS_NO_INIT;
561 }
562 ret = manager->createServer(¶m);
563 if (ret != SOFTBUS_OK) {
564 COMM_LOGE(COMM_SVC, "createServer failed,ret=%{public}d", ret);
565 }
566 COMM_LOGI(COMM_SVC, "create server success");
567 return ret;
568 }
569
RemoveServer(const char * pkgName,const char * name)570 int32_t SoftBusServer::RemoveServer(const char *pkgName, const char *name)
571 {
572 GeneralConnectionParam param = {0};
573 int32_t ret = SOFTBUS_OK;
574 #ifdef SUPPORT_BUNDLENAME
575 ret = FillBundleName(param.bundleName, BUNDLE_NAME_MAX);
576 if (ret != SOFTBUS_OK) {
577 COMM_LOGE(COMM_SVC, "get bundle name failed!");
578 return ret;
579 }
580 #endif
581 if (strcpy_s(param.name, GENERAL_NAME_LEN, name) != EOK) {
582 COMM_LOGE(COMM_SVC, "copy name failed");
583 return SOFTBUS_STRCPY_ERR;
584 }
585 GeneralConnectionManager *manager = GetGeneralConnectionManager();
586 if (manager == nullptr || manager->closeServer == nullptr) {
587 COMM_LOGE(COMM_SVC, "invalid param");
588 return SOFTBUS_NO_INIT;
589 }
590 manager->closeServer(¶m);
591 COMM_LOGI(COMM_SVC, "remove server success");
592 return ret;
593 }
594
Connect(const char * pkgName,const char * name,const Address * address)595 int32_t SoftBusServer::Connect(const char *pkgName, const char *name, const Address *address)
596 {
597 GeneralConnectionManager *manager = GetGeneralConnectionManager();
598 if (manager == nullptr || manager->connect == nullptr) {
599 COMM_LOGE(COMM_SVC, "invalid param");
600 return SOFTBUS_NO_INIT;
601 }
602 GeneralConnectionParam param = {0};
603 if (strcpy_s(param.name, GENERAL_NAME_LEN, name) != EOK) {
604 COMM_LOGE(COMM_SVC, "copy name failed");
605 return SOFTBUS_STRCPY_ERR;
606 }
607 if (strcpy_s(param.pkgName, PKG_NAME_SIZE_MAX, pkgName) != EOK) {
608 COMM_LOGE(COMM_SVC, "copy pkgName failed");
609 return SOFTBUS_STRCPY_ERR;
610 }
611 #ifdef SUPPORT_BUNDLENAME
612 int32_t ret = FillBundleName(param.bundleName, BUNDLE_NAME_MAX);
613 if (ret != SOFTBUS_OK) {
614 COMM_LOGE(COMM_SVC, "get bundle name failed");
615 return ret;
616 }
617 #endif
618 param.pid = OHOS::IPCSkeleton::GetCallingPid();
619 int32_t handle = manager->connect(¶m, address->addr.ble.mac);
620 COMM_LOGI(COMM_SVC, "connect start, handle=%{public}d", handle);
621 return handle;
622 }
623
Disconnect(uint32_t handle)624 int32_t SoftBusServer::Disconnect(uint32_t handle)
625 {
626 GeneralConnectionManager *manager = GetGeneralConnectionManager();
627 if (manager == nullptr || manager->disconnect == nullptr) {
628 COMM_LOGE(COMM_SVC, "invalid param");
629 return SOFTBUS_NO_INIT;
630 }
631 manager->disconnect(handle, OHOS::IPCSkeleton::GetCallingPid());
632 COMM_LOGI(COMM_SVC, "disconnect success, handle=%{public}d", handle);
633 return SOFTBUS_OK;
634 }
635
Send(uint32_t handle,const uint8_t * data,uint32_t len)636 int32_t SoftBusServer::Send(uint32_t handle, const uint8_t *data, uint32_t len)
637 {
638 GeneralConnectionManager *manager = GetGeneralConnectionManager();
639 if (manager == nullptr || manager->send == nullptr) {
640 COMM_LOGE(COMM_SVC, "invalid param");
641 return SOFTBUS_NO_INIT;
642 }
643 int32_t ret = manager->send(handle, data, len, OHOS::IPCSkeleton::GetCallingPid());
644 if (ret != SOFTBUS_OK) {
645 COMM_LOGE(COMM_SVC, "send failed, handle=%{public}d", handle);
646 }
647 return ret;
648 }
649
ConnGetPeerDeviceId(uint32_t handle,char * deviceId,uint32_t len)650 int32_t SoftBusServer::ConnGetPeerDeviceId(uint32_t handle, char *deviceId, uint32_t len)
651 {
652 GeneralConnectionManager *manager = GetGeneralConnectionManager();
653 if (manager == nullptr || manager->getPeerDeviceId == nullptr) {
654 COMM_LOGE(COMM_SVC, "invalid param");
655 return SOFTBUS_NO_INIT;
656 }
657 uint32_t callingTokenId = IPCSkeleton::GetCallingTokenID();
658 int32_t ret = manager->getPeerDeviceId(handle, deviceId, len,
659 callingTokenId, OHOS::IPCSkeleton::GetCallingPid());
660 if (ret != SOFTBUS_OK) {
661 COMM_LOGE(COMM_SVC, "get peer deviceId failed, handle=%{public}d", handle);
662 }
663 return ret;
664 }
665
OpenBrProxy(const char * brMac,const char * uuid)666 int32_t SoftBusServer::OpenBrProxy(const char *brMac, const char *uuid)
667 {
668 return TransOpenBrProxy(brMac, uuid);
669 }
670
CloseBrProxy(int32_t channelId)671 int32_t SoftBusServer::CloseBrProxy(int32_t channelId)
672 {
673 return TransCloseBrProxy(channelId, false);
674 }
675
SendBrProxyData(int32_t channelId,char * data,uint32_t dataLen)676 int32_t SoftBusServer::SendBrProxyData(int32_t channelId, char *data, uint32_t dataLen)
677 {
678 return TransSendBrProxyData(channelId, data, dataLen);
679 }
680
SetListenerState(int32_t channelId,int32_t type,bool CbEnabled)681 int32_t SoftBusServer::SetListenerState(int32_t channelId, int32_t type, bool CbEnabled)
682 {
683 return TransSetListenerState(channelId, type, CbEnabled);
684 }
685
IsProxyChannelEnabled(int32_t uid)686 bool SoftBusServer::IsProxyChannelEnabled(int32_t uid)
687 {
688 return TransIsProxyChannelEnabled(uid);
689 }
690
PushIdentifyCheck()691 static int32_t PushIdentifyCheck()
692 {
693 #define PUSH_SERVICE_UID 7023
694 std::string pkgName = "PUSH_SERVICE";
695 sptr<IRemoteObject> clientObject =
696 SoftbusClientInfoManager::GetInstance().GetSoftbusClientProxy(pkgName);
697 if (clientObject == nullptr) {
698 COMM_LOGE(COMM_SVC, "get remote object failed!");
699 return SOFTBUS_TRANS_PROXY_REMOTE_NULL;
700 }
701 pid_t uid = IPCSkeleton::GetCallingUid();
702 COMM_LOGI(COMM_SVC, "[br_proxy] uid:%{public}d", uid);
703 if (uid != PUSH_SERVICE_UID) {
704 pid_t pid;
705 SoftbusClientInfoManager::GetInstance().SoftbusRemoveService(clientObject, pkgName, &pid);
706 return SOFTBUS_TRANS_BR_PROXY_CALLER_RESTRICTED;
707 }
708 return SOFTBUS_OK;
709 }
710
PushRegisterHook()711 int32_t SoftBusServer::PushRegisterHook()
712 {
713 int32_t ret = PushIdentifyCheck();
714 if (ret != SOFTBUS_OK) {
715 COMM_LOGE(COMM_SVC, "[br_proxy] Push identity verification failed! ret=%{public}d", ret);
716 return ret;
717 }
718 return TransRegisterPushHook();
719 }
720 } // namespace OHOS
721