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