1 /*
2  * Copyright (c) 2022-2024 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 "dm_device_state_manager.h"
17 
18 #include <pthread.h>
19 
20 #include "dm_adapter_manager.h"
21 #include "dm_anonymous.h"
22 #include "dm_constants.h"
23 #include "dm_crypto.h"
24 #include "dm_distributed_hardware_load.h"
25 #include "dm_log.h"
26 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
27 #include "deviceprofile_connector.h"
28 #endif
29 
30 namespace OHOS {
31 namespace DistributedHardware {
32 const uint32_t DM_EVENT_QUEUE_CAPACITY = 20;
33 const uint32_t DM_EVENT_WAIT_TIMEOUT = 2;
34 constexpr const char* THREAD_LOOP = "ThreadLoop";
DmDeviceStateManager(std::shared_ptr<SoftbusConnector> softbusConnector,std::shared_ptr<IDeviceManagerServiceListener> listener,std::shared_ptr<HiChainConnector> hiChainConnector,std::shared_ptr<HiChainAuthConnector> hiChainAuthConnector)35 DmDeviceStateManager::DmDeviceStateManager(std::shared_ptr<SoftbusConnector> softbusConnector,
36     std::shared_ptr<IDeviceManagerServiceListener> listener, std::shared_ptr<HiChainConnector> hiChainConnector,
37     std::shared_ptr<HiChainAuthConnector> hiChainAuthConnector)
38     : softbusConnector_(softbusConnector), listener_(listener), hiChainConnector_(hiChainConnector),
39     hiChainAuthConnector_(hiChainAuthConnector)
40 {
41     decisionSoName_ = "libdevicemanagerext_decision.z.so";
42     StartEventThread();
43     LOGI("DmDeviceStateManager constructor");
44 }
45 
~DmDeviceStateManager()46 DmDeviceStateManager::~DmDeviceStateManager()
47 {
48     LOGI("DmDeviceStateManager destructor");
49     softbusConnector_->UnRegisterSoftbusStateCallback();
50     StopEventThread();
51 }
52 
RegisterSoftbusStateCallback()53 int32_t DmDeviceStateManager::RegisterSoftbusStateCallback()
54 {
55     if (softbusConnector_ != nullptr) {
56         return softbusConnector_->RegisterSoftbusStateCallback(shared_from_this());
57     }
58     return DM_OK;
59 }
60 
SaveOnlineDeviceInfo(const DmDeviceInfo & info)61 void DmDeviceStateManager::SaveOnlineDeviceInfo(const DmDeviceInfo &info)
62 {
63     LOGI("SaveOnlineDeviceInfo begin, deviceId = %{public}s", GetAnonyString(std::string(info.deviceId)).c_str());
64     std::string udid;
65     if (SoftbusConnector::GetUdidByNetworkId(info.networkId, udid) == DM_OK) {
66         std::string uuid;
67         DmDeviceInfo saveInfo = info;
68         SoftbusConnector::GetUuidByNetworkId(info.networkId, uuid);
69         {
70             std::lock_guard<std::mutex> mutexLock(remoteDeviceInfosMutex_);
71             remoteDeviceInfos_[uuid] = saveInfo;
72             stateDeviceInfos_[udid] = saveInfo;
73         }
74         LOGI("SaveOnlineDeviceInfo complete, networkId = %{public}s, udid = %{public}s, uuid = %{public}s",
75              GetAnonyString(std::string(info.networkId)).c_str(),
76              GetAnonyString(udid).c_str(), GetAnonyString(uuid).c_str());
77     }
78 }
79 
DeleteOfflineDeviceInfo(const DmDeviceInfo & info)80 void DmDeviceStateManager::DeleteOfflineDeviceInfo(const DmDeviceInfo &info)
81 {
82     LOGI("DeleteOfflineDeviceInfo begin, deviceId = %{public}s", GetAnonyString(std::string(info.deviceId)).c_str());
83     std::lock_guard<std::mutex> mutexLock(remoteDeviceInfosMutex_);
84     std::string deviceId = std::string(info.deviceId);
85     for (auto iter: remoteDeviceInfos_) {
86         if (std::string(iter.second.deviceId) == deviceId) {
87             remoteDeviceInfos_.erase(iter.first);
88             LOGI("Delete remoteDeviceInfos complete");
89             break;
90         }
91     }
92     for (auto iter: stateDeviceInfos_) {
93         if (std::string(iter.second.deviceId) == deviceId) {
94             stateDeviceInfos_.erase(iter.first);
95             LOGI("Delete stateDeviceInfos complete");
96             break;
97         }
98     }
99 }
100 
OnDeviceOnline(std::string deviceId,int32_t authForm)101 void DmDeviceStateManager::OnDeviceOnline(std::string deviceId, int32_t authForm)
102 {
103     LOGI("DmDeviceStateManager::OnDeviceOnline, deviceId = %{public}s", GetAnonyString(deviceId).c_str());
104     DmDeviceInfo devInfo = softbusConnector_->GetDeviceInfoByDeviceId(deviceId);
105     devInfo.authForm = static_cast<DmAuthForm>(authForm);
106     {
107         std::lock_guard<std::mutex> mutexLock(remoteDeviceInfosMutex_);
108         if (stateDeviceInfos_.find(deviceId) == stateDeviceInfos_.end()) {
109             stateDeviceInfos_[deviceId] = devInfo;
110         }
111     }
112     ProcessDeviceStateChange(DEVICE_STATE_ONLINE, devInfo);
113     softbusConnector_->ClearPkgName();
114 }
115 
OnDeviceOffline(std::string deviceId)116 void DmDeviceStateManager::OnDeviceOffline(std::string deviceId)
117 {
118     LOGI("DmDeviceStateManager::OnDeviceOffline, deviceId = %{public}s", GetAnonyString(deviceId).c_str());
119     DmDeviceInfo devInfo;
120     {
121         std::lock_guard<std::mutex> mutexLock(remoteDeviceInfosMutex_);
122         if (stateDeviceInfos_.find(deviceId) == stateDeviceInfos_.end()) {
123             LOGE("DmDeviceStateManager::OnDeviceOnline not find deviceId");
124             return;
125         }
126         devInfo = stateDeviceInfos_[deviceId];
127     }
128     ProcessDeviceStateChange(DEVICE_STATE_OFFLINE, devInfo);
129     softbusConnector_->ClearPkgName();
130 }
131 
HandleDeviceStatusChange(DmDeviceState devState,DmDeviceInfo & devInfo)132 void DmDeviceStateManager::HandleDeviceStatusChange(DmDeviceState devState, DmDeviceInfo &devInfo)
133 {
134     LOGI("Handle device status change: devState=%{public}d, deviceId=%{public}s.", devState,
135         GetAnonyString(devInfo.deviceId).c_str());
136     switch (devState) {
137         case DEVICE_STATE_ONLINE:
138             RegisterOffLineTimer(devInfo);
139             SaveOnlineDeviceInfo(devInfo);
140             DmDistributedHardwareLoad::GetInstance().LoadDistributedHardwareFwk();
141             ProcessDeviceStateChange(devState, devInfo);
142             softbusConnector_->ClearPkgName();
143             break;
144         case DEVICE_STATE_OFFLINE:
145             StartOffLineTimer(devInfo);
146             DeleteOfflineDeviceInfo(devInfo);
147             if (softbusConnector_ != nullptr) {
148                 std::string udid;
149                 softbusConnector_->GetUdidByNetworkId(devInfo.networkId, udid);
150                 softbusConnector_->EraseUdidFromMap(udid);
151             }
152             ProcessDeviceStateChange(devState, devInfo);
153             softbusConnector_->ClearPkgName();
154             break;
155         case DEVICE_INFO_CHANGED:
156             ChangeDeviceInfo(devInfo);
157             ProcessDeviceStateChange(devState, devInfo);
158             softbusConnector_->ClearPkgName();
159             break;
160         default:
161             LOGE("HandleDeviceStatusChange error, unknown device state = %{public}d", devState);
162             break;
163     }
164 }
165 
ProcessDeviceStateChange(const DmDeviceState devState,const DmDeviceInfo & devInfo)166 void DmDeviceStateManager::ProcessDeviceStateChange(const DmDeviceState devState, const DmDeviceInfo &devInfo)
167 {
168     if (softbusConnector_ == nullptr || listener_ == nullptr) {
169         LOGE("ProcessDeviceStateChange failed, callback_ptr is null.");
170         return;
171     }
172     std::vector<std::string> pkgName = softbusConnector_->GetPkgName();
173     if (pkgName.size() == 0) {
174         listener_->OnDeviceStateChange(std::string(DM_PKG_NAME), devState, devInfo);
175     } else {
176         for (auto item : pkgName) {
177             listener_->OnDeviceStateChange(item, devState, devInfo);
178         }
179     }
180 }
181 
OnDbReady(const std::string & pkgName,const std::string & uuid)182 void DmDeviceStateManager::OnDbReady(const std::string &pkgName, const std::string &uuid)
183 {
184     LOGI("OnDbReady function is called with pkgName: %{public}s and uuid = %{public}s",
185          pkgName.c_str(), GetAnonyString(uuid).c_str());
186     if (pkgName.empty() || uuid.empty()) {
187         LOGE("On db ready pkgName is empty or uuid is empty");
188         return;
189     }
190     LOGI("OnDbReady function is called with pkgName: %{public}s and uuid = %{public}s", pkgName.c_str(),
191          GetAnonyString(uuid).c_str());
192     DmDeviceInfo saveInfo;
193     {
194         std::lock_guard<std::mutex> mutexLock(remoteDeviceInfosMutex_);
195         auto iter = remoteDeviceInfos_.find(uuid);
196         if (iter == remoteDeviceInfos_.end()) {
197             LOGE("OnDbReady complete not find uuid: %{public}s", GetAnonyString(uuid).c_str());
198             return;
199         }
200         saveInfo = iter->second;
201     }
202     if (listener_ != nullptr) {
203         DmDeviceState state = DEVICE_INFO_READY;
204         listener_->OnDeviceStateChange(pkgName, state, saveInfo);
205     }
206 }
207 
RegisterOffLineTimer(const DmDeviceInfo & deviceInfo)208 void DmDeviceStateManager::RegisterOffLineTimer(const DmDeviceInfo &deviceInfo)
209 {
210     std::string deviceUdid;
211     int32_t ret = softbusConnector_->GetUdidByNetworkId(deviceInfo.networkId, deviceUdid);
212     if (ret != DM_OK) {
213         LOGE("fail to get udid by networkId");
214         return;
215     }
216     char udidHash[DM_MAX_DEVICE_ID_LEN] = {0};
217     if (Crypto::GetUdidHash(deviceUdid, reinterpret_cast<uint8_t *>(udidHash)) != DM_OK) {
218         LOGE("get udidhash by udid: %{public}s failed.", GetAnonyString(deviceUdid).c_str());
219         return;
220     }
221     LOGI("Register offline timer for udidHash: %{public}s", GetAnonyString(std::string(udidHash)).c_str());
222     std::lock_guard<std::mutex> mutexLock(timerMapMutex_);
223     for (auto &iter : stateTimerInfoMap_) {
224         if ((iter.first == std::string(udidHash)) && (timer_ != nullptr)) {
225             timer_->DeleteTimer(iter.second.timerName);
226             stateTimerInfoMap_.erase(iter.first);
227             auto idIter = udidhash2udidMap_.find(udidHash);
228             if (idIter != udidhash2udidMap_.end()) {
229                 udidhash2udidMap_.erase(idIter->first);
230             }
231             break;
232         }
233     }
234     if (stateTimerInfoMap_.find(std::string(udidHash)) == stateTimerInfoMap_.end()) {
235         std::string timerName = std::string(STATE_TIMER_PREFIX) + GetAnonyString(std::string(udidHash));
236         StateTimerInfo stateTimer = {
237             .timerName = timerName,
238             .networkId = deviceInfo.networkId,
239             .isStart = false,
240         };
241         stateTimerInfoMap_[std::string(udidHash)] = stateTimer;
242     }
243     if (udidhash2udidMap_.find(std::string(udidHash)) == udidhash2udidMap_.end()) {
244         udidhash2udidMap_[std::string(udidHash)] = deviceUdid;
245     }
246 }
247 
StartOffLineTimer(const DmDeviceInfo & deviceInfo)248 void DmDeviceStateManager::StartOffLineTimer(const DmDeviceInfo &deviceInfo)
249 {
250     std::lock_guard<std::mutex> mutexLock(timerMapMutex_);
251     std::string networkId = deviceInfo.networkId;
252     LOGI("Start offline timer for networkId: %{public}s", GetAnonyString(networkId).c_str());
253     if (timer_ == nullptr) {
254         timer_ = std::make_shared<DmTimer>();
255     }
256     for (auto &iter : stateTimerInfoMap_) {
257         if ((iter.second.networkId == networkId) && !iter.second.isStart) {
258             timer_->StartTimer(iter.second.timerName, OFFLINE_TIMEOUT,
259                 [this] (std::string name) {
260                     DmDeviceStateManager::DeleteTimeOutGroup(name);
261                 });
262             iter.second.isStart = true;
263         }
264     }
265 }
266 
DeleteOffLineTimer(std::string udidHash)267 void DmDeviceStateManager::DeleteOffLineTimer(std::string udidHash)
268 {
269     std::lock_guard<std::mutex> mutexLock(timerMapMutex_);
270     LOGI("DELETE offline timer for networkId: %{public}s", GetAnonyString(udidHash).c_str());
271     if (timer_ == nullptr || udidHash.empty()) {
272         return;
273     }
274     auto iter = stateTimerInfoMap_.find(udidHash);
275     if (iter != stateTimerInfoMap_.end()) {
276         timer_->DeleteTimer(iter->second.timerName);
277         iter->second.isStart = false;
278         stateTimerInfoMap_.erase(iter->first);
279         auto idIter = udidhash2udidMap_.find(udidHash);
280         if (idIter != udidhash2udidMap_.end()) {
281             udidhash2udidMap_.erase(idIter->first);
282         }
283     }
284     return;
285 }
286 
DeleteTimeOutGroup(std::string name)287 void DmDeviceStateManager::DeleteTimeOutGroup(std::string name)
288 {
289     std::lock_guard<std::mutex> mutexLock(timerMapMutex_);
290     for (auto iter = stateTimerInfoMap_.begin(); iter != stateTimerInfoMap_.end(); iter++) {
291         if (((iter->second).timerName == name) && (hiChainConnector_ != nullptr)) {
292             auto idIter = udidhash2udidMap_.find(iter->first);
293             if (idIter == udidhash2udidMap_.end()) {
294                 LOGE("remove hichain group find deviceId: %{public}s failed.", GetAnonyString(iter->first).c_str());
295                 break;
296             }
297             LOGI("remove hichain group with deviceId: %{public}s", GetAnonyString(idIter->second).c_str());
298             hiChainConnector_->DeleteTimeOutGroup((idIter->second).c_str());
299 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
300             DeleteGroupByDP(idIter->second);
301             uint32_t res = DeviceProfileConnector::GetInstance().DeleteTimeOutAcl(idIter->second);
302             if (res == 0) {
303                 hiChainAuthConnector_->DeleteCredential(idIter->second,
304                                                         MultipleUserConnector::GetCurrentAccountUserID());
305             }
306 #endif
307             stateTimerInfoMap_.erase(iter);
308             break;
309         }
310     }
311 }
312 
StartEventThread()313 void DmDeviceStateManager::StartEventThread()
314 {
315     LOGI("StartEventThread begin");
316     eventTask_.threadRunning_ = true;
317     eventTask_.queueThread_ = std::thread([this]() { this->ThreadLoop(); });
318     LOGI("StartEventThread complete");
319 }
320 
StopEventThread()321 void DmDeviceStateManager::StopEventThread()
322 {
323     LOGI("StopEventThread begin");
324     eventTask_.threadRunning_ = false;
325     eventTask_.queueCond_.notify_all();
326     eventTask_.queueFullCond_.notify_all();
327     if (eventTask_.queueThread_.joinable()) {
328         eventTask_.queueThread_.join();
329     }
330     LOGI("StopEventThread complete");
331 }
332 
AddTask(const std::shared_ptr<NotifyEvent> & task)333 int32_t DmDeviceStateManager::AddTask(const std::shared_ptr<NotifyEvent> &task)
334 {
335     LOGI("AddTask begin, eventId: %{public}d", task->GetEventId());
336     {
337         std::unique_lock<std::mutex> lock(eventTask_.queueMtx_);
338         while (eventTask_.queue_.size() >= DM_EVENT_QUEUE_CAPACITY) {
339             eventTask_.queueFullCond_.wait_for(lock, std::chrono::seconds(DM_EVENT_WAIT_TIMEOUT));
340         }
341         eventTask_.queue_.push(task);
342     }
343     eventTask_.queueCond_.notify_one();
344     LOGI("AddTask complete");
345     return DM_OK;
346 }
347 
ThreadLoop()348 void DmDeviceStateManager::ThreadLoop()
349 {
350     LOGI("ThreadLoop begin");
351     int32_t ret = pthread_setname_np(pthread_self(), THREAD_LOOP);
352     if (ret != DM_OK) {
353         LOGE("ThreadLoop setname failed.");
354     }
355     while (eventTask_.threadRunning_) {
356         std::shared_ptr<NotifyEvent> task = nullptr;
357         {
358             std::unique_lock<std::mutex> lock(eventTask_.queueMtx_);
359             while (eventTask_.queue_.empty() && eventTask_.threadRunning_) {
360                 eventTask_.queueCond_.wait_for(lock, std::chrono::seconds(DM_EVENT_WAIT_TIMEOUT));
361             }
362             if (!eventTask_.queue_.empty()) {
363                 task = eventTask_.queue_.front();
364                 eventTask_.queue_.pop();
365                 eventTask_.queueFullCond_.notify_one();
366             }
367         }
368         if (task != nullptr) {
369             RunTask(task);
370         }
371     }
372     LOGI("ThreadLoop end");
373 }
374 
RunTask(const std::shared_ptr<NotifyEvent> & task)375 void DmDeviceStateManager::RunTask(const std::shared_ptr<NotifyEvent> &task)
376 {
377     LOGI("RunTask begin, eventId: %{public}d", task->GetEventId());
378     if (task->GetEventId() == DM_NOTIFY_EVENT_ONDEVICEREADY) {
379         OnDbReady(std::string(DM_PKG_NAME), task->GetDeviceId());
380     }
381     LOGI("RunTask complete");
382 }
383 
GetAuthForm(const std::string & networkId)384 DmAuthForm DmDeviceStateManager::GetAuthForm(const std::string &networkId)
385 {
386     LOGI("GetAuthForm start");
387     if (hiChainConnector_ == nullptr) {
388         LOGE("hiChainConnector_ is nullptr");
389         return DmAuthForm::INVALID_TYPE;
390     }
391 
392     if (networkId.empty()) {
393         LOGE("networkId is empty");
394         return DmAuthForm::INVALID_TYPE;
395     }
396 
397     std::string udid;
398     if (SoftbusConnector::GetUdidByNetworkId(networkId.c_str(), udid) == DM_OK) {
399         return hiChainConnector_->GetGroupType(udid);
400     }
401 
402     return DmAuthForm::INVALID_TYPE;
403 }
404 
ProcNotifyEvent(const int32_t eventId,const std::string & deviceId)405 int32_t DmDeviceStateManager::ProcNotifyEvent(const int32_t eventId, const std::string &deviceId)
406 {
407     LOGI("ProcNotifyEvent in, eventId: %{public}d", eventId);
408     return AddTask(std::make_shared<NotifyEvent>(eventId, deviceId));
409 }
410 
ChangeDeviceInfo(const DmDeviceInfo & info)411 void DmDeviceStateManager::ChangeDeviceInfo(const DmDeviceInfo &info)
412 {
413     std::lock_guard<std::mutex> mutexLock(remoteDeviceInfosMutex_);
414     for (auto iter : remoteDeviceInfos_) {
415         if (std::string(iter.second.deviceId) == std::string(info.deviceId)) {
416             if (memcpy_s(iter.second.deviceName, sizeof(iter.second.deviceName), info.deviceName,
417                 sizeof(info.deviceName)) != DM_OK) {
418                     LOGE("ChangeDeviceInfo remoteDeviceInfos copy deviceName failed");
419             }
420             if (memcpy_s(iter.second.networkId, sizeof(iter.second.networkId), info.networkId,
421                 sizeof(info.networkId)) != DM_OK) {
422                     LOGE("ChangeDeviceInfo remoteDeviceInfos copy networkId failed");
423             }
424             iter.second.deviceTypeId = info.deviceTypeId;
425             LOGI("Change remoteDeviceInfos complete");
426             break;
427         }
428     }
429     for (auto iter : stateDeviceInfos_) {
430         if (std::string(iter.second.deviceId) == std::string(info.deviceId)) {
431             if (memcpy_s(iter.second.deviceName, sizeof(iter.second.deviceName), info.deviceName,
432                 sizeof(info.deviceName)) != DM_OK) {
433                     LOGE("ChangeDeviceInfo stateDeviceInfos copy deviceName failed");
434             }
435             if (memcpy_s(iter.second.networkId, sizeof(iter.second.networkId), info.networkId,
436                 sizeof(info.networkId)) != DM_OK) {
437                     LOGE("ChangeDeviceInfo stateDeviceInfos copy networkId failed");
438             }
439             iter.second.deviceTypeId = info.deviceTypeId;
440             LOGI("Change stateDeviceInfos complete");
441             break;
442         }
443     }
444 }
445 
GetUdidByNetWorkId(std::string networkId)446 std::string DmDeviceStateManager::GetUdidByNetWorkId(std::string networkId)
447 {
448     LOGI("DmDeviceStateManager::GetUdidByNetWorkId networkId %{public}s", GetAnonyString(networkId).c_str());
449     {
450         std::lock_guard<std::mutex> mutexLock(remoteDeviceInfosMutex_);
451         for (auto &iter : stateDeviceInfos_) {
452             if (networkId == iter.second.networkId) {
453                 return iter.first;
454             }
455         }
456     }
457     LOGI("Not find udid by networkid in stateDeviceInfos.");
458     return "";
459 }
460 
461 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
DeleteGroupByDP(const std::string & deviceId)462 int32_t DmDeviceStateManager::DeleteGroupByDP(const std::string &deviceId)
463 {
464     std::vector<DistributedDeviceProfile::AccessControlProfile> profiles =
465         DeviceProfileConnector::GetInstance().GetAccessControlProfile();
466     LOGI("DeleteGroupByDP, AccessControlProfile size is %{public}zu", profiles.size());
467     std::vector<std::string> delPkgNameVec;
468     for (auto &item : profiles) {
469         std::string trustDeviceId = item.GetTrustDeviceId();
470         if (trustDeviceId != deviceId) {
471             continue;
472         }
473         if (item.GetAuthenticationType() == ALLOW_AUTH_ONCE && !item.GetAccesser().GetAccesserBundleName().empty()) {
474             delPkgNameVec.push_back(item.GetAccesser().GetAccesserBundleName());
475         }
476     }
477     if (delPkgNameVec.size() == 0) {
478         LOGI("delPkgNameVec is empty");
479         return DM_OK;
480     }
481     if (hiChainConnector_ == nullptr) {
482         LOGE("hiChainConnector_ is nullptr");
483         return ERR_DM_POINT_NULL;
484     }
485     std::vector<GroupInfo> groupListExt;
486     hiChainConnector_->GetRelatedGroupsExt(deviceId, groupListExt);
487     for (auto &iter : groupListExt) {
488         for (auto &pkgName : delPkgNameVec) {
489             if (iter.groupName.find(pkgName) != std::string::npos) {
490                 int32_t ret = hiChainConnector_->DeleteGroupExt(iter.groupId);
491                 LOGI("DeleteGroupByDP delete groupId %{public}s ,result %{public}d.",
492                     GetAnonyString(iter.groupId).c_str(), ret);
493             }
494         }
495     }
496     return DM_OK;
497 }
498 #endif
499 
CheckIsOnline(const std::string & udid)500 bool DmDeviceStateManager::CheckIsOnline(const std::string &udid)
501 {
502     LOGI("DmDeviceStateManager::CheckIsOnline start.");
503     {
504         std::lock_guard<std::mutex> mutexLock(remoteDeviceInfosMutex_);
505         if (stateDeviceInfos_.find(udid) != stateDeviceInfos_.end()) {
506             return true;
507         }
508     }
509     return false;
510 }
511 
HandleDeviceScreenStatusChange(DmDeviceInfo & devInfo)512 void DmDeviceStateManager::HandleDeviceScreenStatusChange(DmDeviceInfo &devInfo)
513 {
514     if (softbusConnector_ == nullptr || listener_ == nullptr) {
515         LOGE("failed, ptr is null.");
516         return;
517     }
518     std::vector<std::string> pkgName = softbusConnector_->GetPkgName();
519     LOGI("pkgName size: %{public}zu", pkgName.size());
520     if (pkgName.size() == 0) {
521         listener_->OnDeviceScreenStateChange(std::string(DM_PKG_NAME), devInfo);
522     } else {
523         for (auto item : pkgName) {
524             listener_->OnDeviceScreenStateChange(item, devInfo);
525         }
526     }
527     softbusConnector_->ClearPkgName();
528 }
529 
HandleCredentialAuthStatus(const std::string & proofInfo,uint16_t deviceTypeId,int32_t errcode)530 void DmDeviceStateManager::HandleCredentialAuthStatus(const std::string &proofInfo, uint16_t deviceTypeId,
531                                                       int32_t errcode)
532 {
533     if (listener_ == nullptr) {
534         LOGE("Failed, listener_ is null.");
535         return;
536     }
537     listener_->OnCredentialAuthStatus(std::string(DM_PKG_NAME), proofInfo, deviceTypeId, errcode);
538 }
539 } // namespace DistributedHardware
540 } // namespace OHOS