1 /*
2 * Copyright (c) 2022-2023 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_listener.h"
17
18 #include <securec.h>
19 #include <unistd.h>
20 #if defined(__LITEOS_M__)
21 #include "dm_mutex.h"
22 #include "dm_thread.h"
23 #else
24 #include <pthread.h>
25 #include <thread>
26 #include <mutex>
27 #endif
28
29 #include "device_manager_service.h"
30 #include "dm_constants.h"
31 #include "dm_device_info.h"
32 #include "dm_log.h"
33 #include "parameter.h"
34 #include "system_ability_definition.h"
35
36 namespace OHOS {
37 namespace DistributedHardware {
38 const int32_t DISCOVER_STATUS_LEN = 20;
39 const int32_t SOFTBUS_CHECK_INTERVAL = 100000; // 100ms
40
41 constexpr const char* DISCOVER_STATUS_KEY = "persist.distributed_hardware.device_manager.discover_status";
42 constexpr const char* DISCOVER_STATUS_ON = "1";
43 constexpr const char* DISCOVER_STATUS_OFF = "0";
44 constexpr const char* DEVICE_ONLINE = "deviceOnLine";
45 constexpr const char* DEVICE_OFFLINE = "deviceOffLine";
46 constexpr const char* DEVICE_NAME_CHANGE = "deviceNameChange";
47
48 SoftbusListener::PulishStatus SoftbusListener::publishStatus = SoftbusListener::STATUS_UNKNOWN;
49 IPublishCb SoftbusListener::softbusPublishCallback_ = {
50 .OnPublishResult = SoftbusListener::OnPublishResult,
51 };
52
53 INodeStateCb SoftbusListener::softbusNodeStateCb_ = {
54 .events = EVENT_NODE_STATE_ONLINE | EVENT_NODE_STATE_OFFLINE | EVENT_NODE_STATE_INFO_CHANGED,
55 .onNodeOnline = SoftbusListener::OnSoftBusDeviceOnline,
56 .onNodeOffline = SoftbusListener::OnSoftbusDeviceOffline,
57 .onNodeBasicInfoChanged = SoftbusListener::OnSoftbusDeviceInfoChanged};
58
DeviceOnLine(DmDeviceInfo deviceInfo)59 void DeviceOnLine(DmDeviceInfo deviceInfo)
60 {
61 #if defined(__LITEOS_M__)
62 DmMutex lockDeviceOnLine;
63 #else
64 std::mutex lockDeviceOnLine;
65 std::lock_guard<std::mutex> lock(lockDeviceOnLine);
66 #endif
67 DeviceManagerService::GetInstance().HandleDeviceOnline(deviceInfo);
68 }
69
DeviceOffLine(DmDeviceInfo deviceInfo)70 void DeviceOffLine(DmDeviceInfo deviceInfo)
71 {
72 #if defined(__LITEOS_M__)
73 DmMutex lockDeviceOffLine;
74 #else
75 std::mutex lockDeviceOffLine;
76 std::lock_guard<std::mutex> lock(lockDeviceOffLine);
77 #endif
78 DeviceManagerService::GetInstance().HandleDeviceOffline(deviceInfo);
79 }
80
DeviceNameChange(DmDeviceInfo deviceInfo)81 void DeviceNameChange(DmDeviceInfo deviceInfo)
82 {
83 #if defined(__LITEOS_M__)
84 DmMutex lockDeviceOffLine;
85 #else
86 std::mutex lockDeviceOffLine;
87 std::lock_guard<std::mutex> lock(lockDeviceOffLine);
88 #endif
89 DeviceManagerService::GetInstance().HandleDeviceNameChange(deviceInfo);
90 }
91
SoftbusListener()92 SoftbusListener::SoftbusListener()
93 {
94 ISessionListener sessionListener = {.OnSessionOpened = SoftbusListener::OnSessionOpened,
95 .OnSessionClosed = SoftbusListener::OnSessionClosed,
96 .OnBytesReceived = SoftbusListener::OnBytesReceived,
97 .OnMessageReceived = nullptr,
98 .OnStreamReceived = nullptr};
99 LOGD("SoftbusListener constructor.");
100 int32_t ret = CreateSessionServer(DM_PKG_NAME, DM_SESSION_NAME, &sessionListener);
101 if (ret != DM_OK) {
102 LOGE("[SOFTBUS]CreateSessionServer failed, ret: %d.", ret);
103 } else {
104 LOGI("[SOFTBUS]CreateSessionServer ok.");
105 }
106 Init();
107 }
108
~SoftbusListener()109 SoftbusListener::~SoftbusListener()
110 {
111 RemoveSessionServer(DM_PKG_NAME, DM_SESSION_NAME);
112 LOGD("SoftbusListener destructor.");
113 }
114
SetPublishInfo(PublishInfo & dmPublishInfo)115 void SoftbusListener::SetPublishInfo(PublishInfo &dmPublishInfo)
116 {
117 dmPublishInfo.publishId = DISTRIBUTED_HARDWARE_DEVICEMANAGER_SA_ID;
118 dmPublishInfo.mode = DiscoverMode::DISCOVER_MODE_ACTIVE;
119 dmPublishInfo.medium = ExchangeMedium::AUTO;
120 dmPublishInfo.freq = ExchangeFreq::HIGH;
121 dmPublishInfo.capability = DM_CAPABILITY_OSD;
122 dmPublishInfo.ranging = false;
123 return;
124 }
125
Init()126 int32_t SoftbusListener::Init()
127 {
128 int32_t ret;
129 int32_t retryTimes = 0;
130 do {
131 ret = RegNodeDeviceStateCb(DM_PKG_NAME, &softbusNodeStateCb_);
132 if (ret != DM_OK) {
133 ++retryTimes;
134 LOGE("[SOFTBUS]RegNodeDeviceStateCb failed with ret: %d, retryTimes: %d.", ret, retryTimes);
135 usleep(SOFTBUS_CHECK_INTERVAL);
136 }
137 } while (ret != DM_OK);
138
139 PublishInfo dmPublishInfo;
140 (void)memset_s(&dmPublishInfo, sizeof(PublishInfo), 0, sizeof(PublishInfo));
141 SetPublishInfo(dmPublishInfo);
142 #if (defined(__LITEOS_M__) || defined(LITE_DEVICE))
143 ret = PublishLNN(DM_PKG_NAME, &dmPublishInfo, &softbusPublishCallback_);
144 if (ret == DM_OK) {
145 publishStatus = ALLOW_BE_DISCOVERY;
146 }
147 #else
148 char discoverStatus[DISCOVER_STATUS_LEN + 1] = {0};
149 ret = GetParameter(DISCOVER_STATUS_KEY, "not exist", discoverStatus, DISCOVER_STATUS_LEN);
150 if (strcmp(discoverStatus, "not exist") == 0) {
151 ret = SetParameter(DISCOVER_STATUS_KEY, DISCOVER_STATUS_ON);
152 LOGI("[SOFTBUS]service set parameter result, ret: %d.", ret);
153
154 ret = PublishLNN(DM_PKG_NAME, &dmPublishInfo, &softbusPublishCallback_);
155 if (ret == DM_OK) {
156 publishStatus = ALLOW_BE_DISCOVERY;
157 }
158 LOGI("[SOFTBUS]service publish result, ret: %d.", ret);
159 } else if (ret >= 0 && strcmp(discoverStatus, DISCOVER_STATUS_ON) == 0) {
160 ret = PublishLNN(DM_PKG_NAME, &dmPublishInfo, &softbusPublishCallback_);
161 if (ret == DM_OK) {
162 publishStatus = ALLOW_BE_DISCOVERY;
163 }
164 LOGI("[SOFTBUS]service publish result, ret: %d.", ret);
165 } else if (ret >= 0 && strcmp(discoverStatus, DISCOVER_STATUS_OFF) == 0) {
166 ret = StopPublishLNN(DM_PKG_NAME, DISTRIBUTED_HARDWARE_DEVICEMANAGER_SA_ID);
167 if (ret == DM_OK) {
168 publishStatus = NOT_ALLOW_BE_DISCOVERY;
169 }
170 LOGI("[SOFTBUS]service unpublish. ret: %d.", ret);
171 }
172
173 ret = WatchParameter(DISCOVER_STATUS_KEY, &SoftbusListener::OnParameterChgCallback, nullptr);
174 #endif
175 return ret;
176 }
177
GetTrustedDeviceList(std::vector<DmDeviceInfo> & deviceInfoList)178 int32_t SoftbusListener::GetTrustedDeviceList(std::vector<DmDeviceInfo> &deviceInfoList)
179 {
180 int32_t deviceCount = 0;
181 NodeBasicInfo *nodeInfo = nullptr;
182 int32_t ret = GetAllNodeDeviceInfo(DM_PKG_NAME, &nodeInfo, &deviceCount);
183 if (ret != DM_OK) {
184 LOGE("[SOFTBUS]GetAllNodeDeviceInfo failed, ret: %d.", ret);
185 return ERR_DM_FAILED;
186 }
187 DmDeviceInfo *info = static_cast<DmDeviceInfo *>(malloc(sizeof(DmDeviceInfo) * (deviceCount)));
188 if (info == nullptr) {
189 FreeNodeInfo(nodeInfo);
190 return ERR_DM_MALLOC_FAILED;
191 }
192 DmDeviceInfo **pInfoList = &info;
193 for (int32_t i = 0; i < deviceCount; ++i) {
194 NodeBasicInfo *nodeBasicInfo = nodeInfo + i;
195 DmDeviceInfo *deviceInfo = *pInfoList + i;
196 ConvertNodeBasicInfoToDmDevice(*nodeBasicInfo, *deviceInfo);
197 deviceInfoList.push_back(*deviceInfo);
198 }
199 FreeNodeInfo(nodeInfo);
200 free(info);
201 LOGI("GetTrustDevices success, deviceCount: %d.", deviceCount);
202 return ret;
203 }
204
GetAvailableDeviceList(std::vector<DmDeviceBasicInfo> & deviceBasicInfoList)205 int32_t SoftbusListener::GetAvailableDeviceList(std::vector<DmDeviceBasicInfo> &deviceBasicInfoList)
206 {
207 int32_t deviceCount = 0;
208 NodeBasicInfo *nodeInfo = nullptr;
209 int32_t ret = GetAllNodeDeviceInfo(DM_PKG_NAME, &nodeInfo, &deviceCount);
210 if (ret != DM_OK) {
211 LOGE("[SOFTBUS]GetAllNodeDeviceInfo failed, ret: %d.", ret);
212 return ERR_DM_FAILED;
213 }
214 DmDeviceBasicInfo *info = static_cast<DmDeviceBasicInfo *>(malloc(sizeof(DmDeviceBasicInfo) * (deviceCount)));
215 if (info == nullptr) {
216 FreeNodeInfo(nodeInfo);
217 return ERR_DM_MALLOC_FAILED;
218 }
219 DmDeviceBasicInfo **pInfoList = &info;
220 for (int32_t i = 0; i < deviceCount; ++i) {
221 NodeBasicInfo *nodeBasicInfo = nodeInfo + i;
222 DmDeviceBasicInfo *deviceBasicInfo = *pInfoList + i;
223 ConvertNodeBasicInfoToDmDevice(*nodeBasicInfo, *deviceBasicInfo);
224 deviceBasicInfoList.push_back(*deviceBasicInfo);
225 }
226 FreeNodeInfo(nodeInfo);
227 free(info);
228 LOGI("GetAvailableDevices success, deviceCount: %d.", deviceCount);
229 return ret;
230 }
231
GetDeviceInfo(const std::string & networkId,DmDeviceInfo & info)232 int32_t SoftbusListener::GetDeviceInfo(const std::string &networkId, DmDeviceInfo &info)
233 {
234 int32_t nodeInfoCount = 0;
235 NodeBasicInfo *nodeInfo = nullptr;
236 int32_t ret = GetAllNodeDeviceInfo(DM_PKG_NAME, &nodeInfo, &nodeInfoCount);
237 if (ret != DM_OK) {
238 LOGE("[SOFTBUS]GetAllNodeDeviceInfo failed, ret: %d.", ret);
239 return ERR_DM_FAILED;
240 }
241 for (int32_t i = 0; i < nodeInfoCount; ++i) {
242 NodeBasicInfo *nodeBasicInfo = nodeInfo + i;
243 if (networkId == nodeBasicInfo->networkId) {
244 LOGI("GetDeviceInfo name : %s.", nodeBasicInfo->deviceName);
245 if (memcpy_s(info.deviceName, sizeof(info.deviceName), nodeBasicInfo->deviceName,
246 std::min(sizeof(info.deviceName), sizeof(nodeBasicInfo->deviceName))) != DM_OK) {
247 LOGE("GetDeviceInfo deviceName copy deviceName data failed.");
248 }
249 info.deviceTypeId = nodeBasicInfo->deviceTypeId;
250 break;
251 }
252 }
253 FreeNodeInfo(nodeInfo);
254 LOGI("GetDeviceInfo complete, deviceName : %s, deviceTypeId : %d.", info.deviceName, info.deviceTypeId);
255 return ret;
256 }
257
GetLocalDeviceInfo(DmDeviceInfo & deviceInfo)258 int32_t SoftbusListener::GetLocalDeviceInfo(DmDeviceInfo &deviceInfo)
259 {
260 NodeBasicInfo nodeBasicInfo;
261 int32_t ret = GetLocalNodeDeviceInfo(DM_PKG_NAME, &nodeBasicInfo);
262 if (ret != DM_OK) {
263 LOGE("[SOFTBUS]GetLocalNodeDeviceInfo failed, ret: %d.", ret);
264 return ERR_DM_FAILED;
265 }
266 ConvertNodeBasicInfoToDmDevice(nodeBasicInfo, deviceInfo);
267 return ret;
268 }
269
GetLocalDeviceNetworkId(std::string & networkId)270 int32_t SoftbusListener::GetLocalDeviceNetworkId(std::string &networkId)
271 {
272 NodeBasicInfo nodeBasicInfo;
273 int32_t ret = GetLocalNodeDeviceInfo(DM_PKG_NAME, &nodeBasicInfo);
274 if (ret != DM_OK) {
275 LOGE("[SOFTBUS]GetLocalNodeDeviceInfo failed, ret: %d.", ret);
276 return ERR_DM_FAILED;
277 }
278 networkId = nodeBasicInfo.networkId;
279 return ret;
280 }
281
GetLocalDeviceName(std::string & deviceName)282 int32_t SoftbusListener::GetLocalDeviceName(std::string &deviceName)
283 {
284 NodeBasicInfo nodeBasicInfo;
285 int32_t ret = GetLocalNodeDeviceInfo(DM_PKG_NAME, &nodeBasicInfo);
286 if (ret != DM_OK) {
287 LOGE("[SOFTBUS]GetLocalNodeDeviceInfo failed, ret: %d.", ret);
288 return ERR_DM_FAILED;
289 }
290 deviceName = nodeBasicInfo.deviceName;
291 return ret;
292 }
293
GetLocalDeviceType(int32_t & deviceType)294 int32_t SoftbusListener::GetLocalDeviceType(int32_t &deviceType)
295 {
296 NodeBasicInfo nodeBasicInfo;
297 int32_t ret = GetLocalNodeDeviceInfo(DM_PKG_NAME, &nodeBasicInfo);
298 if (ret != DM_OK) {
299 LOGE("[SOFTBUS]GetLocalNodeDeviceInfo failed, ret: %d.", ret);
300 return ERR_DM_FAILED;
301 }
302 deviceType = nodeBasicInfo.deviceTypeId;
303 return ret;
304 }
305
GetUdidByNetworkId(const char * networkId,std::string & udid)306 int32_t SoftbusListener::GetUdidByNetworkId(const char *networkId, std::string &udid)
307 {
308 uint8_t mUdid[UDID_BUF_LEN] = {0};
309 int32_t ret = GetNodeKeyInfo(DM_PKG_NAME, networkId, NodeDeviceInfoKey::NODE_KEY_UDID, mUdid, sizeof(mUdid));
310 if (ret != DM_OK) {
311 LOGE("[SOFTBUS]GetNodeKeyInfo failed, ret: %d.", ret);
312 return ERR_DM_FAILED;
313 }
314 udid = reinterpret_cast<char *>(mUdid);
315 return ret;
316 }
317
GetUuidByNetworkId(const char * networkId,std::string & uuid)318 int32_t SoftbusListener::GetUuidByNetworkId(const char *networkId, std::string &uuid)
319 {
320 uint8_t mUuid[UUID_BUF_LEN] = {0};
321 int32_t ret = GetNodeKeyInfo(DM_PKG_NAME, networkId, NodeDeviceInfoKey::NODE_KEY_UUID, mUuid, sizeof(mUuid));
322 if (ret != DM_OK) {
323 LOGE("[SOFTBUS]GetNodeKeyInfo failed, ret: %d.", ret);
324 return ERR_DM_FAILED;
325 }
326 uuid = reinterpret_cast<char *>(mUuid);
327 return ret;
328 }
329
ShiftLNNGear()330 int32_t SoftbusListener::ShiftLNNGear()
331 {
332 GearMode mode = {
333 .cycle = HIGH_FREQ_CYCLE,
334 .duration = DEFAULT_DURATION,
335 .wakeupFlag = false,
336 };
337
338 int32_t ret = ::ShiftLNNGear(DM_PKG_NAME, DM_PKG_NAME, nullptr, &mode);
339 if (ret != DM_OK) {
340 LOGE("[SOFTBUS]ShiftLNNGear error, failed ret: %d", ret);
341 return ret;
342 }
343 LOGI("[SOFTBUS]ShiftLNNGear success.");
344 return DM_OK;
345 }
346
OnSoftBusDeviceOnline(NodeBasicInfo * info)347 void SoftbusListener::OnSoftBusDeviceOnline(NodeBasicInfo *info)
348 {
349 LOGI("received device online callback from softbus.");
350 if (info == nullptr) {
351 LOGE("NodeBasicInfo is nullptr.");
352 return;
353 }
354 DmDeviceInfo dmDeviceInfo;
355 ConvertNodeBasicInfoToDmDevice(*info, dmDeviceInfo);
356 #if defined(__LITEOS_M__)
357 DmThread deviceOnLine(DeviceOnLine, dmDeviceInfo);
358 deviceOnLine.DmCreatThread();
359 #else
360 std::thread deviceOnLine(DeviceOnLine, dmDeviceInfo);
361 int32_t ret = pthread_setname_np(deviceOnLine.native_handle(), DEVICE_ONLINE);
362 if (ret != DM_OK) {
363 LOGE("deviceOnLine setname failed.");
364 }
365 deviceOnLine.detach();
366 #endif
367 }
368
OnSoftbusDeviceOffline(NodeBasicInfo * info)369 void SoftbusListener::OnSoftbusDeviceOffline(NodeBasicInfo *info)
370 {
371 LOGI("received device offline callback from softbus.");
372 if (info == nullptr) {
373 LOGE("NodeBasicInfo is nullptr.");
374 return;
375 }
376 DmDeviceInfo dmDeviceInfo;
377 ConvertNodeBasicInfoToDmDevice(*info, dmDeviceInfo);
378 #if defined(__LITEOS_M__)
379 DmThread deviceOffLine(DeviceOffLine, dmDeviceInfo);
380 deviceOffLine.DmCreatThread();
381 #else
382 std::thread deviceOffLine(DeviceOffLine, dmDeviceInfo);
383 int32_t ret = pthread_setname_np(deviceOffLine.native_handle(), DEVICE_OFFLINE);
384 if (ret != DM_OK) {
385 LOGE("deviceOffLine setname failed.");
386 }
387 deviceOffLine.detach();
388 #endif
389 }
390
ConvertNodeBasicInfoToDmDevice(const NodeBasicInfo & nodeBasicInfo,DmDeviceInfo & dmDeviceInfo)391 int32_t SoftbusListener::ConvertNodeBasicInfoToDmDevice(const NodeBasicInfo &nodeBasicInfo, DmDeviceInfo &dmDeviceInfo)
392 {
393 (void)memset_s(&dmDeviceInfo, sizeof(DmDeviceInfo), 0, sizeof(DmDeviceInfo));
394 if (memcpy_s(dmDeviceInfo.networkId, sizeof(dmDeviceInfo.networkId), nodeBasicInfo.networkId,
395 std::min(sizeof(dmDeviceInfo.networkId), sizeof(nodeBasicInfo.networkId))) != DM_OK) {
396 LOGE("ConvertNodeBasicInfoToDmDevice copy networkId data failed.");
397 }
398
399 if (memcpy_s(dmDeviceInfo.deviceName, sizeof(dmDeviceInfo.deviceName), nodeBasicInfo.deviceName,
400 std::min(sizeof(dmDeviceInfo.deviceName), sizeof(nodeBasicInfo.deviceName))) != DM_OK) {
401 LOGE("ConvertNodeBasicInfoToDmDevice copy deviceName data failed.");
402 }
403 dmDeviceInfo.deviceTypeId = nodeBasicInfo.deviceTypeId;
404 return DM_OK;
405 }
406
ConvertNodeBasicInfoToDmDevice(const NodeBasicInfo & nodeBasicInfo,DmDeviceBasicInfo & dmDeviceBasicInfo)407 int32_t SoftbusListener::ConvertNodeBasicInfoToDmDevice(const NodeBasicInfo &nodeBasicInfo,
408 DmDeviceBasicInfo &dmDeviceBasicInfo)
409 {
410 (void)memset_s(&dmDeviceBasicInfo, sizeof(DmDeviceBasicInfo), 0, sizeof(DmDeviceBasicInfo));
411 if (memcpy_s(dmDeviceBasicInfo.networkId, sizeof(dmDeviceBasicInfo.networkId), nodeBasicInfo.networkId,
412 std::min(sizeof(dmDeviceBasicInfo.networkId), sizeof(nodeBasicInfo.networkId))) != DM_OK) {
413 LOGE("ConvertNodeBasicInfoToDmDevice copy networkId data failed.");
414 }
415
416 if (memcpy_s(dmDeviceBasicInfo.deviceName, sizeof(dmDeviceBasicInfo.deviceName), nodeBasicInfo.deviceName,
417 std::min(sizeof(dmDeviceBasicInfo.deviceName), sizeof(nodeBasicInfo.deviceName))) != DM_OK) {
418 LOGE("ConvertNodeBasicInfoToDmDevice copy deviceName data failed.");
419 }
420 dmDeviceBasicInfo.deviceTypeId = nodeBasicInfo.deviceTypeId;
421 return DM_OK;
422 }
423
OnParameterChgCallback(const char * key,const char * value,void * context)424 void SoftbusListener::OnParameterChgCallback(const char *key, const char *value, void *context)
425 {
426 (void)key;
427 (void)context;
428 if (strcmp(value, DISCOVER_STATUS_ON) == 0 && publishStatus != ALLOW_BE_DISCOVERY) {
429 PublishInfo dmPublishInfo;
430 (void)memset_s(&dmPublishInfo, sizeof(PublishInfo), 0, sizeof(PublishInfo));
431 dmPublishInfo.publishId = DISTRIBUTED_HARDWARE_DEVICEMANAGER_SA_ID;
432 dmPublishInfo.mode = DiscoverMode::DISCOVER_MODE_ACTIVE;
433 dmPublishInfo.medium = ExchangeMedium::AUTO;
434 dmPublishInfo.freq = ExchangeFreq::HIGH;
435 dmPublishInfo.capability = DM_CAPABILITY_OSD;
436 dmPublishInfo.ranging = false;
437 int32_t ret = ::PublishLNN(DM_PKG_NAME, &dmPublishInfo, &softbusPublishCallback_);
438 if (ret == DM_OK) {
439 publishStatus = ALLOW_BE_DISCOVERY;
440 }
441 LOGI("[SOFTBUS]PublishLNN return ret: %d.", ret);
442 } else if (strcmp(value, DISCOVER_STATUS_OFF) == 0 && publishStatus != NOT_ALLOW_BE_DISCOVERY) {
443 int32_t ret = ::StopPublishLNN(DM_PKG_NAME, DISTRIBUTED_HARDWARE_DEVICEMANAGER_SA_ID);
444 if (ret == DM_OK) {
445 publishStatus = NOT_ALLOW_BE_DISCOVERY;
446 }
447 LOGI("[SOFTBUS]StopPublishLNN return ret: %d.", ret);
448 }
449 }
450
OnSessionOpened(int sessionId,int result)451 int SoftbusListener::OnSessionOpened(int sessionId, int result)
452 {
453 return DeviceManagerService::GetInstance().OnSessionOpened(sessionId, result);
454 }
455
OnSessionClosed(int sessionId)456 void SoftbusListener::OnSessionClosed(int sessionId)
457 {
458 DeviceManagerService::GetInstance().OnSessionClosed(sessionId);
459 }
460
OnBytesReceived(int sessionId,const void * data,unsigned int dataLen)461 void SoftbusListener::OnBytesReceived(int sessionId, const void *data, unsigned int dataLen)
462 {
463 DeviceManagerService::GetInstance().OnBytesReceived(sessionId, data, dataLen);
464 }
465
OnPublishResult(int publishId,PublishResult result)466 void SoftbusListener::OnPublishResult(int publishId, PublishResult result)
467 {
468 LOGD("OnPublishResult, publishId: %d, result: %d.", publishId, result);
469 }
470
OnSoftbusDeviceInfoChanged(NodeBasicInfoType type,NodeBasicInfo * info)471 void SoftbusListener::OnSoftbusDeviceInfoChanged(NodeBasicInfoType type, NodeBasicInfo *info)
472 {
473 LOGI("received device info change from softbus.");
474 if (info == nullptr) {
475 LOGE("NodeBasicInfo is nullptr.");
476 return;
477 }
478 if (type == NodeBasicInfoType::TYPE_DEVICE_NAME || type == NodeBasicInfoType::TYPE_NETWORK_INFO) {
479 LOGI("DeviceInfo %d change.", type);
480 DmDeviceInfo dmDeviceInfo;
481 int32_t networkType = -1;
482 if (type == NodeBasicInfoType::TYPE_NETWORK_INFO) {
483 if (GetNodeKeyInfo(DM_PKG_NAME, info->networkId, NodeDeviceInfoKey::NODE_KEY_NETWORK_TYPE,
484 reinterpret_cast<uint8_t *>(&networkType), LNN_COMMON_LEN) != DM_OK) {
485 LOGE("[SOFTBUS]GetNodeKeyInfo networkType failed.");
486 return;
487 }
488 LOGI("OnSoftbusDeviceInfoChanged NetworkType %d.", networkType);
489 }
490 ConvertNodeBasicInfoToDmDevice(*info, dmDeviceInfo);
491 dmDeviceInfo.networkType = networkType;
492 std::thread deviceInfoChange(DeviceNameChange, dmDeviceInfo);
493 if (pthread_setname_np(deviceInfoChange.native_handle(), DEVICE_NAME_CHANGE) != DM_OK) {
494 LOGE("DeviceNameChange setname failed.");
495 }
496 deviceInfoChange.detach();
497 LOGD("OnSoftbusDeviceInfoChanged.");
498 return;
499 }
500 }
501
GetNetworkTypeByNetworkId(const char * networkId,int32_t & networkType)502 int32_t SoftbusListener::GetNetworkTypeByNetworkId(const char *networkId, int32_t &networkType)
503 {
504 int32_t tempNetworkType = -1;
505 if (GetNodeKeyInfo(DM_PKG_NAME, networkId, NodeDeviceInfoKey::NODE_KEY_NETWORK_TYPE,
506 reinterpret_cast<uint8_t *>(&tempNetworkType), LNN_COMMON_LEN) != DM_OK) {
507 LOGE("[SOFTBUS]GetNodeKeyInfo networkType failed.");
508 return ERR_DM_FAILED;
509 }
510 networkType = tempNetworkType;
511 LOGI("GetNetworkTypeByNetworkId networkType %d.", tempNetworkType);
512 return DM_OK;
513 }
514 } // namespace DistributedHardware
515 } // namespace OHOS
516