• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-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 "device_manager_notify.h"
17 #include <thread>
18 #include "dm_constants.h"
19 #include "dm_anonymous.h"
20 #include "dm_error_type.h"
21 #include "dm_device_info.h"
22 #include "dm_log.h"
23 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
24 #include "ipc_model_codec.h"
25 #endif
26 
27 namespace OHOS {
28 namespace DistributedHardware {
29 namespace {
30 constexpr const char* UK_SEPARATOR = "#";
31 }
32 DM_IMPLEMENT_SINGLE_INSTANCE(DeviceManagerNotify);
33 #if (defined(__LITEOS_M__) || defined(LITE_DEVICE))
34 constexpr const char* DEVICE_ONLINE = "deviceOnline";
35 constexpr const char* DEVICE_OFFLINE = "deviceOffline";
36 constexpr const char* DEVICEINFO_CHANGE = "deviceInfoChange";
37 constexpr const char* DEVICE_READY = "deviceReady";
38 constexpr const char* DEVICE_TRUST_CHANGE = "deviceTrustChange";
39 #endif
40 const uint16_t DM_INVALID_FLAG_ID = 0;
RegisterDeathRecipientCallback(const std::string & pkgName,std::shared_ptr<DmInitCallback> dmInitCallback)41 void DeviceManagerNotify::RegisterDeathRecipientCallback(const std::string &pkgName,
42                                                          std::shared_ptr<DmInitCallback> dmInitCallback)
43 {
44     if (pkgName.empty() || dmInitCallback == nullptr) {
45         LOGE("Invalid parameter, pkgName is empty or callback is nullptr.");
46         return;
47     }
48     std::lock_guard<std::mutex> autoLock(lock_);
49     CHECK_SIZE_VOID(dmInitCallback_);
50     dmInitCallback_[pkgName] = dmInitCallback;
51 }
52 
UnRegisterDeathRecipientCallback(const std::string & pkgName)53 void DeviceManagerNotify::UnRegisterDeathRecipientCallback(const std::string &pkgName)
54 {
55     if (pkgName.empty()) {
56         LOGE("Invalid parameter, pkgName is empty.");
57         return;
58     }
59     std::lock_guard<std::mutex> autoLock(lock_);
60     dmInitCallback_.erase(pkgName);
61 }
62 
RegisterDeviceStateCallback(const std::string & pkgName,std::shared_ptr<DeviceStateCallback> callback)63 void DeviceManagerNotify::RegisterDeviceStateCallback(const std::string &pkgName,
64                                                       std::shared_ptr<DeviceStateCallback> callback)
65 {
66     if (pkgName.empty() || callback == nullptr) {
67         LOGE("Invalid parameter, pkgName is empty or callback is nullptr.");
68         return;
69     }
70     std::lock_guard<std::mutex> autoLock(lock_);
71     CHECK_SIZE_VOID(deviceStateCallback_);
72     deviceStateCallback_[pkgName] = callback;
73 }
74 
UnRegisterDeviceStateCallback(const std::string & pkgName)75 void DeviceManagerNotify::UnRegisterDeviceStateCallback(const std::string &pkgName)
76 {
77     if (pkgName.empty()) {
78         LOGE("Invalid parameter, pkgName is empty.");
79         return;
80     }
81     std::lock_guard<std::mutex> autoLock(lock_);
82     deviceStateCallback_.erase(pkgName);
83 }
84 
UnRegisterDeviceStatusCallback(const std::string & pkgName)85 void DeviceManagerNotify::UnRegisterDeviceStatusCallback(const std::string &pkgName)
86 {
87     if (pkgName.empty()) {
88         LOGE("Invalid parameter, pkgName is empty.");
89         return;
90     }
91     std::lock_guard<std::mutex> autoLock(lock_);
92     deviceStatusCallback_.erase(pkgName);
93 }
94 
RegisterDeviceStatusCallback(const std::string & pkgName,std::shared_ptr<DeviceStatusCallback> callback)95 void DeviceManagerNotify::RegisterDeviceStatusCallback(const std::string &pkgName,
96     std::shared_ptr<DeviceStatusCallback> callback)
97 {
98     if (pkgName.empty() || callback == nullptr) {
99         LOGE("Invalid parameter, pkgName is empty or callback is nullptr.");
100         return;
101     }
102     std::lock_guard<std::mutex> autoLock(lock_);
103     CHECK_SIZE_VOID(deviceStatusCallback_);
104     deviceStatusCallback_[pkgName] = callback;
105 }
106 
RegisterDiscoveryCallback(const std::string & pkgName,uint16_t subscribeId,std::shared_ptr<DiscoveryCallback> callback)107 void DeviceManagerNotify::RegisterDiscoveryCallback(const std::string &pkgName, uint16_t subscribeId,
108                                                     std::shared_ptr<DiscoveryCallback> callback)
109 {
110     if (pkgName.empty() || callback == nullptr) {
111         LOGE("Invalid parameter, pkgName is empty or callback is nullptr.");
112         return;
113     }
114     std::lock_guard<std::mutex> autoLock(lock_);
115     if (deviceDiscoveryCallbacks_.count(pkgName) == 0) {
116         CHECK_SIZE_VOID(deviceDiscoveryCallbacks_);
117         deviceDiscoveryCallbacks_[pkgName] = std::map<uint16_t, std::shared_ptr<DiscoveryCallback>>();
118     }
119     deviceDiscoveryCallbacks_[pkgName][subscribeId] = callback;
120 }
121 
UnRegisterDiscoveryCallback(const std::string & pkgName,uint16_t subscribeId)122 void DeviceManagerNotify::UnRegisterDiscoveryCallback(const std::string &pkgName, uint16_t subscribeId)
123 {
124     if (pkgName.empty()) {
125         LOGE("Invalid parameter, pkgName is empty.");
126         return;
127     }
128     std::lock_guard<std::mutex> autoLock(lock_);
129     if (deviceDiscoveryCallbacks_.count(pkgName) > 0) {
130         deviceDiscoveryCallbacks_[pkgName].erase(subscribeId);
131         if (deviceDiscoveryCallbacks_[pkgName].empty()) {
132             deviceDiscoveryCallbacks_.erase(pkgName);
133         }
134     }
135 }
136 
RegisterPublishCallback(const std::string & pkgName,int32_t publishId,std::shared_ptr<PublishCallback> callback)137 void DeviceManagerNotify::RegisterPublishCallback(const std::string &pkgName,
138                                                   int32_t publishId,
139                                                   std::shared_ptr<PublishCallback> callback)
140 {
141     if (pkgName.empty() || callback == nullptr) {
142         LOGE("Invalid parameter, pkgName is empty or callback is nullptr.");
143         return;
144     }
145     std::lock_guard<std::mutex> autoLock(lock_);
146     if (devicePublishCallbacks_.count(pkgName) == 0) {
147         CHECK_SIZE_VOID(devicePublishCallbacks_);
148         devicePublishCallbacks_[pkgName] = std::map<int32_t, std::shared_ptr<PublishCallback>>();
149     }
150     devicePublishCallbacks_[pkgName][publishId] = callback;
151 }
152 
UnRegisterPublishCallback(const std::string & pkgName,int32_t publishId)153 void DeviceManagerNotify::UnRegisterPublishCallback(const std::string &pkgName, int32_t publishId)
154 {
155     if (pkgName.empty()) {
156         LOGE("Invalid parameter, pkgName is empty.");
157         return;
158     }
159     std::lock_guard<std::mutex> autoLock(lock_);
160     if (devicePublishCallbacks_.count(pkgName) > 0) {
161         devicePublishCallbacks_[pkgName].erase(publishId);
162         if (devicePublishCallbacks_[pkgName].empty()) {
163             devicePublishCallbacks_.erase(pkgName);
164         }
165     }
166 }
167 
RegisterAuthenticateCallback(const std::string & pkgName,const std::string & deviceId,std::shared_ptr<AuthenticateCallback> callback)168 void DeviceManagerNotify::RegisterAuthenticateCallback(const std::string &pkgName, const std::string &deviceId,
169                                                        std::shared_ptr<AuthenticateCallback> callback)
170 {
171     if (pkgName.empty() || deviceId.empty() || callback == nullptr) {
172         LOGE("DeviceManagerNotify::RegisterAuthenticateCallback error: Invalid parameter, pkgName: %{public}s,"
173             "deviceId: %{public}s", pkgName.c_str(), GetAnonyString(deviceId).c_str());
174         return;
175     }
176     std::lock_guard<std::mutex> autoLock(lock_);
177     if (authenticateCallback_.count(pkgName) == 0) {
178         CHECK_SIZE_VOID(authenticateCallback_);
179         authenticateCallback_[pkgName] = std::map<std::string, std::shared_ptr<AuthenticateCallback>>();
180     }
181     authenticateCallback_[pkgName][deviceId] = callback;
182 }
183 
UnRegisterAuthenticateCallback(const std::string & pkgName,const std::string & deviceId)184 void DeviceManagerNotify::UnRegisterAuthenticateCallback(const std::string &pkgName, const std::string &deviceId)
185 {
186     if (pkgName.empty() || deviceId.empty()) {
187         LOGE("DeviceManagerNotify::UnRegisterAuthenticateCallback error: Invalid parameter, pkgName: %{public}s,"
188             "deviceId: %{public}s", pkgName.c_str(), GetAnonyString(deviceId).c_str());
189         return;
190     }
191     std::lock_guard<std::mutex> autoLock(lock_);
192     if (authenticateCallback_.count(pkgName) > 0) {
193         authenticateCallback_[pkgName].erase(deviceId);
194         if (authenticateCallback_[pkgName].empty()) {
195             authenticateCallback_.erase(pkgName);
196         }
197     }
198 }
199 
UnRegisterPackageCallback(const std::string & pkgName)200 void DeviceManagerNotify::UnRegisterPackageCallback(const std::string &pkgName)
201 {
202     if (pkgName.empty()) {
203         LOGE("Invalid parameter, pkgName is empty.");
204         return;
205     }
206     std::lock_guard<std::mutex> autoLock(lock_);
207     deviceStateCallback_.erase(pkgName);
208     deviceStatusCallback_.erase(pkgName);
209     devicePublishCallbacks_.erase(pkgName);
210     authenticateCallback_.erase(pkgName);
211     dmInitCallback_.erase(pkgName);
212     for (auto it = deviceDiscoveryCallbacks_.begin(); it != deviceDiscoveryCallbacks_.end();) {
213         if (it->first.find(pkgName) != std::string::npos) {
214             it = deviceDiscoveryCallbacks_.erase(it);
215         } else {
216             ++it;
217         }
218     }
219 }
220 
RegisterDeviceManagerFaCallback(const std::string & pkgName,std::shared_ptr<DeviceManagerUiCallback> callback)221 void DeviceManagerNotify::RegisterDeviceManagerFaCallback(const std::string &pkgName,
222                                                           std::shared_ptr<DeviceManagerUiCallback> callback)
223 {
224     std::lock_guard<std::mutex> autoLock(lock_);
225     CHECK_SIZE_VOID(dmUiCallback_);
226     dmUiCallback_[pkgName] = callback;
227 }
228 
UnRegisterDeviceManagerFaCallback(const std::string & pkgName)229 void DeviceManagerNotify::UnRegisterDeviceManagerFaCallback(const std::string &pkgName)
230 {
231     if (pkgName.empty()) {
232         LOGE("Invalid parameter, pkgName is empty.");
233         return;
234     }
235     std::lock_guard<std::mutex> autoLock(lock_);
236     dmUiCallback_.erase(pkgName);
237 }
238 
RegisterCredentialCallback(const std::string & pkgName,std::shared_ptr<CredentialCallback> callback)239 void DeviceManagerNotify::RegisterCredentialCallback(const std::string &pkgName,
240                                                      std::shared_ptr<CredentialCallback> callback)
241 {
242     if (pkgName.empty() || callback == nullptr) {
243         LOGE("Invalid parameter, pkgName is empty or callback is nullptr.");
244         return;
245     }
246     std::lock_guard<std::mutex> autoLock(lock_);
247     CHECK_SIZE_VOID(credentialCallback_);
248     credentialCallback_[pkgName] = callback;
249 }
250 
UnRegisterCredentialCallback(const std::string & pkgName)251 void DeviceManagerNotify::UnRegisterCredentialCallback(const std::string &pkgName)
252 {
253     if (pkgName.empty()) {
254         LOGE("Invalid parameter, pkgName is empty.");
255         return;
256     }
257     std::lock_guard<std::mutex> autoLock(lock_);
258     credentialCallback_.erase(pkgName);
259 }
260 
RegisterPinHolderCallback(const std::string & pkgName,std::shared_ptr<PinHolderCallback> callback)261 void DeviceManagerNotify::RegisterPinHolderCallback(const std::string &pkgName,
262     std::shared_ptr<PinHolderCallback> callback)
263 {
264     if (pkgName.empty() || callback == nullptr) {
265         LOGE("Invalid parameter, pkgName is empty or callback is nullptr.");
266         return;
267     }
268     std::lock_guard<std::mutex> autoLock(lock_);
269     CHECK_SIZE_VOID(pinHolderCallback_);
270     pinHolderCallback_[pkgName] = callback;
271 }
272 
OnRemoteDied()273 void DeviceManagerNotify::OnRemoteDied()
274 {
275     LOGW("OnRemoteDied");
276     std::map<std::string, std::shared_ptr<DmInitCallback>> dmInitCallback = GetDmInitCallback();
277     for (auto iter : dmInitCallback) {
278         LOGI("OnRemoteDied, pkgName:%{public}s", iter.first.c_str());
279         if (iter.second != nullptr) {
280             iter.second->OnRemoteDied();
281         }
282     }
283 }
284 
OnDeviceOnline(const std::string & pkgName,const DmDeviceInfo & deviceInfo)285 void DeviceManagerNotify::OnDeviceOnline(const std::string &pkgName, const DmDeviceInfo &deviceInfo)
286 {
287     if (pkgName.empty()) {
288         LOGE("Invalid parameter, pkgName is empty.");
289         return;
290     }
291     std::shared_ptr<DeviceStateCallback> tempCbk;
292     {
293         std::lock_guard<std::mutex> autoLock(lock_);
294         auto iter = deviceStateCallback_.find(pkgName);
295         if (iter == deviceStateCallback_.end()) {
296             return;
297         }
298         tempCbk = iter->second;
299     }
300     if (tempCbk == nullptr) {
301         LOGE("OnDeviceOnline error, registered device state callback is nullptr.");
302         return;
303     }
304 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
305     ffrt::submit([=]() { DeviceInfoOnline(deviceInfo, tempCbk); });
306 #else
307     std::thread deviceOnline([=]() { DeviceInfoOnline(deviceInfo, tempCbk); });
308     if (pthread_setname_np(deviceOnline.native_handle(), DEVICE_ONLINE) != DM_OK) {
309         LOGE("DeviceInfoOnline set name failed.");
310     }
311     deviceOnline.detach();
312 #endif
313 }
314 
OnDeviceOnline(const std::string & pkgName,const DmDeviceBasicInfo & deviceBasicInfo)315 void DeviceManagerNotify::OnDeviceOnline(const std::string &pkgName, const DmDeviceBasicInfo &deviceBasicInfo)
316 {
317     if (pkgName.empty()) {
318         LOGE("Invalid parameter, pkgName is empty.");
319         return;
320     }
321     std::shared_ptr<DeviceStatusCallback> tempCbk;
322     {
323         std::lock_guard<std::mutex> autoLock(lock_);
324         auto iter = deviceStatusCallback_.find(pkgName);
325         if (iter == deviceStatusCallback_.end()) {
326             return;
327         }
328         tempCbk = iter->second;
329     }
330     if (tempCbk == nullptr) {
331         LOGE("Error, registered device status callback is nullptr.");
332         return;
333     }
334 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
335     ffrt::submit([=]() { DeviceBasicInfoOnline(deviceBasicInfo, tempCbk); });
336 #else
337     std::thread deviceOnline([=]() { DeviceBasicInfoOnline(deviceBasicInfo, tempCbk); });
338     if (pthread_setname_np(deviceOnline.native_handle(), DEVICE_ONLINE) != DM_OK) {
339         LOGE("DeviceInfoOnline set name failed.");
340     }
341     deviceOnline.detach();
342 #endif
343 }
344 
OnDeviceOffline(const std::string & pkgName,const DmDeviceInfo & deviceInfo)345 void DeviceManagerNotify::OnDeviceOffline(const std::string &pkgName, const DmDeviceInfo &deviceInfo)
346 {
347     if (pkgName.empty()) {
348         LOGE("Invalid parameter, pkgName is empty.");
349         return;
350     }
351     std::shared_ptr<DeviceStateCallback> tempCbk;
352     {
353         std::lock_guard<std::mutex> autoLock(lock_);
354         auto iter = deviceStateCallback_.find(pkgName);
355         if (iter == deviceStateCallback_.end()) {
356             return;
357         }
358         tempCbk = iter->second;
359     }
360     if (tempCbk == nullptr) {
361         LOGE("Error, registered device state callback is nullptr.");
362         return;
363     }
364 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
365     ffrt::submit([=]() { DeviceInfoOffline(deviceInfo, tempCbk); });
366     LOGI("Completed, Offline with DmDeviceInfo, pkgName:%{public}s", pkgName.c_str());
367 #else
368     std::thread deviceOffline([=]() { DeviceInfoOffline(deviceInfo, tempCbk); });
369     if (pthread_setname_np(deviceOffline.native_handle(), DEVICE_OFFLINE) != DM_OK) {
370         LOGE("DeviceInfoOffline set name failed.");
371     }
372     deviceOffline.detach();
373 #endif
374 }
375 
OnDeviceOffline(const std::string & pkgName,const DmDeviceBasicInfo & deviceBasicInfo)376 void DeviceManagerNotify::OnDeviceOffline(const std::string &pkgName, const DmDeviceBasicInfo &deviceBasicInfo)
377 {
378     if (pkgName.empty()) {
379         LOGE("Invalid parameter, pkgName is empty.");
380         return;
381     }
382     std::shared_ptr<DeviceStatusCallback> tempCbk;
383     {
384         std::lock_guard<std::mutex> autoLock(lock_);
385         auto iter = deviceStatusCallback_.find(pkgName);
386         if (iter == deviceStatusCallback_.end()) {
387             return;
388         }
389         tempCbk = iter->second;
390     }
391     if (tempCbk == nullptr) {
392         LOGE("Error, registered device status callback is nullptr.");
393         return;
394     }
395 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
396     ffrt::submit([=]() { DeviceBasicInfoOffline(deviceBasicInfo, tempCbk); });
397 #else
398     std::thread deviceOffline([=]() { DeviceBasicInfoOffline(deviceBasicInfo, tempCbk); });
399     if (pthread_setname_np(deviceOffline.native_handle(), DEVICE_OFFLINE) != DM_OK) {
400         LOGE("DeviceInfoOffline set name failed.");
401     }
402     deviceOffline.detach();
403 #endif
404 }
405 
OnDeviceChanged(const std::string & pkgName,const DmDeviceInfo & deviceInfo)406 void DeviceManagerNotify::OnDeviceChanged(const std::string &pkgName, const DmDeviceInfo &deviceInfo)
407 {
408     if (pkgName.empty()) {
409         LOGE("Invalid parameter, pkgName is empty.");
410         return;
411     }
412 
413     std::shared_ptr<DeviceStateCallback> tempCbk;
414     {
415         std::lock_guard<std::mutex> autoLock(lock_);
416         auto iter = deviceStateCallback_.find(pkgName);
417         if (iter == deviceStateCallback_.end()) {
418             return;
419         }
420         tempCbk = iter->second;
421     }
422     if (tempCbk == nullptr) {
423         LOGE("OnDeviceChanged error, registered device state callback is nullptr, pkgName:%{public}s", pkgName.c_str());
424         return;
425     }
426 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
427     ffrt::submit([=]() { DeviceInfoChanged(deviceInfo, tempCbk); });
428 #else
429     std::thread deviceChanged([=]() { DeviceInfoChanged(deviceInfo, tempCbk); });
430     if (pthread_setname_np(deviceChanged.native_handle(), DEVICEINFO_CHANGE) != DM_OK) {
431         LOGE("deviceChanged set name failed.");
432     }
433     deviceChanged.detach();
434 #endif
435 }
436 
OnDeviceChanged(const std::string & pkgName,const DmDeviceBasicInfo & deviceBasicInfo)437 void DeviceManagerNotify::OnDeviceChanged(const std::string &pkgName, const DmDeviceBasicInfo &deviceBasicInfo)
438 {
439     if (pkgName.empty()) {
440         LOGE("Invalid parameter, pkgName is empty.");
441         return;
442     }
443 
444     std::shared_ptr<DeviceStatusCallback> tempCbk;
445     {
446         std::lock_guard<std::mutex> autoLock(lock_);
447         auto iter = deviceStatusCallback_.find(pkgName);
448         if (iter == deviceStatusCallback_.end()) {
449             return;
450         }
451         tempCbk = iter->second;
452     }
453     if (tempCbk == nullptr) {
454         LOGE("OnDeviceChanged error, registered device state callback is nullptr, pkgName:%{public}s", pkgName.c_str());
455         return;
456     }
457 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
458     ffrt::submit([=]() { DeviceBasicInfoChanged(deviceBasicInfo, tempCbk); });
459 #else
460     std::thread deviceChanged([=]() { DeviceBasicInfoChanged(deviceBasicInfo, tempCbk); });
461     if (pthread_setname_np(deviceChanged.native_handle(), DEVICEINFO_CHANGE) != DM_OK) {
462         LOGE("deviceChanged set name failed.");
463     }
464     deviceChanged.detach();
465 #endif
466 }
467 
OnDeviceReady(const std::string & pkgName,const DmDeviceInfo & deviceInfo)468 void DeviceManagerNotify::OnDeviceReady(const std::string &pkgName, const DmDeviceInfo &deviceInfo)
469 {
470     if (pkgName.empty()) {
471         LOGE("Invalid parameter, pkgName is empty.");
472         return;
473     }
474 
475     std::shared_ptr<DeviceStateCallback> tempCbk;
476     {
477         std::lock_guard<std::mutex> autoLock(lock_);
478         auto iter = deviceStateCallback_.find(pkgName);
479         if (iter == deviceStateCallback_.end()) {
480             return;
481         }
482         tempCbk = iter->second;
483     }
484     if (tempCbk == nullptr) {
485         LOGE("OnDeviceReady error, registered device state callback is nullptr, pkgName:%{public}s", pkgName.c_str());
486         return;
487     }
488 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
489     ffrt::submit([=]() { DeviceInfoReady(deviceInfo, tempCbk); });
490 #else
491     std::thread deviceReady([=]() { DeviceInfoReady(deviceInfo, tempCbk); });
492     if (pthread_setname_np(deviceReady.native_handle(), DEVICE_READY) != DM_OK) {
493         LOGE("deviceReady set name failed.");
494     }
495     deviceReady.detach();
496 #endif
497 }
498 
OnDeviceReady(const std::string & pkgName,const DmDeviceBasicInfo & deviceBasicInfo)499 void DeviceManagerNotify::OnDeviceReady(const std::string &pkgName, const DmDeviceBasicInfo &deviceBasicInfo)
500 {
501     if (pkgName.empty()) {
502         LOGE("Invalid parameter, pkgName is empty.");
503         return;
504     }
505     std::shared_ptr<DeviceStatusCallback> tempCbk;
506     {
507         std::lock_guard<std::mutex> autoLock(lock_);
508         auto iter = deviceStatusCallback_.find(pkgName);
509         if (iter == deviceStatusCallback_.end()) {
510             return;
511         }
512         tempCbk = iter->second;
513     }
514     if (tempCbk == nullptr) {
515         LOGE("OnDeviceReady error, registered device status callback is nullptr.");
516         return;
517     }
518 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
519     ffrt::submit([=]() { DeviceBasicInfoReady(deviceBasicInfo, tempCbk); });
520 #else
521     std::thread deviceReady([=]() { DeviceBasicInfoReady(deviceBasicInfo, tempCbk); });
522     if (pthread_setname_np(deviceReady.native_handle(), DEVICE_READY) != DM_OK) {
523         LOGE("deviceReady set name failed.");
524     }
525     deviceReady.detach();
526 #endif
527 }
528 
OnDeviceFound(const std::string & pkgName,uint16_t subscribeId,const DmDeviceInfo & deviceInfo)529 void DeviceManagerNotify::OnDeviceFound(const std::string &pkgName, uint16_t subscribeId,
530                                         const DmDeviceInfo &deviceInfo)
531 {
532     if (pkgName.empty()) {
533         LOGE("Invalid parameter, pkgName is empty.");
534         return;
535     }
536     std::shared_ptr<DiscoveryCallback> tempCbk = GetDiscoveryCallback(pkgName, subscribeId);
537     if (tempCbk == nullptr) {
538         LOGE("OnDeviceFound error, registered device discovery callback is nullptr.");
539         return;
540     }
541     tempCbk->OnDeviceFound(subscribeId, deviceInfo);
542 }
543 
OnDeviceFound(const std::string & pkgName,uint16_t subscribeId,const DmDeviceBasicInfo & deviceBasicInfo)544 void DeviceManagerNotify::OnDeviceFound(const std::string &pkgName, uint16_t subscribeId,
545                                         const DmDeviceBasicInfo &deviceBasicInfo)
546 {
547     if (pkgName.empty()) {
548         LOGE("Invalid parameter, pkgName is empty.");
549         return;
550     }
551     std::shared_ptr<DiscoveryCallback> tempCbk = GetDiscoveryCallback(pkgName, subscribeId);
552     if (tempCbk == nullptr) {
553         LOGE("OnDeviceFound error, registered device discovery callback is nullptr.");
554         return;
555     }
556     tempCbk->OnDeviceFound(subscribeId, deviceBasicInfo);
557 }
558 
OnDiscoveryFailed(const std::string & pkgName,uint16_t subscribeId,int32_t failedReason)559 void DeviceManagerNotify::OnDiscoveryFailed(const std::string &pkgName, uint16_t subscribeId, int32_t failedReason)
560 {
561     if (pkgName.empty()) {
562         LOGE("Invalid parameter, pkgName is empty.");
563         return;
564     }
565     LOGE("in, pkgName:%{public}s, subscribeId %{public}d, failed"
566         "reason %{public}d", pkgName.c_str(), (int32_t)subscribeId, failedReason);
567     std::shared_ptr<DiscoveryCallback> tempCbk = GetDiscoveryCallback(pkgName, subscribeId);
568     if (tempCbk == nullptr) {
569         LOGE("OnDiscoveryFailed error, registered device discovery callback is nullptr.");
570         return;
571     }
572     tempCbk->OnDiscoveryFailed(subscribeId, failedReason);
573 }
574 
OnDiscoverySuccess(const std::string & pkgName,uint16_t subscribeId)575 void DeviceManagerNotify::OnDiscoverySuccess(const std::string &pkgName, uint16_t subscribeId)
576 {
577     if (pkgName.empty()) {
578         LOGE("Invalid parameter, pkgName is empty.");
579         return;
580     }
581     LOGI("PkgName:%{public}s, subscribeId:%{public}d.", GetAnonyString(pkgName).c_str(), subscribeId);
582     std::shared_ptr<DiscoveryCallback> tempCbk = GetDiscoveryCallback(pkgName, subscribeId);
583     if (tempCbk == nullptr) {
584         LOGE("OnDiscoverySuccess error, registered device discovery callback is nullptr.");
585         return;
586     }
587     tempCbk->OnDiscoverySuccess(subscribeId);
588 }
589 
OnPublishResult(const std::string & pkgName,int32_t publishId,int32_t publishResult)590 void DeviceManagerNotify::OnPublishResult(const std::string &pkgName, int32_t publishId, int32_t publishResult)
591 {
592     if (pkgName.empty()) {
593         LOGE("Invalid parameter, pkgName is empty.");
594         return;
595     }
596     LOGI("in, pkgName:%{public}s, publishId %{public}d, publishResult %{public}d",
597         pkgName.c_str(), publishId, publishResult);
598     std::shared_ptr<PublishCallback> tempCbk;
599     {
600         std::lock_guard<std::mutex> autoLock(lock_);
601         if (devicePublishCallbacks_.count(pkgName) == 0) {
602             LOGE("DeviceManagerNotify::OnPublishResult error, device publish callback not register for"
603                 "pkgName %{public}s.", pkgName.c_str());
604             return;
605         }
606         std::map<int32_t, std::shared_ptr<PublishCallback>> &publishCallMap = devicePublishCallbacks_[pkgName];
607         auto iter = publishCallMap.find(publishId);
608         if (iter == publishCallMap.end()) {
609             LOGE("OnPublishResult error, device publish callback not register for publishId %{public}d.", publishId);
610             return;
611         }
612         tempCbk = iter->second;
613     }
614     if (tempCbk == nullptr) {
615         LOGE("OnPublishResult error, registered device publish callback is nullptr.");
616         return;
617     }
618     tempCbk->OnPublishResult(publishId, publishResult);
619 }
620 
OnAuthResult(const std::string & pkgName,const std::string & deviceId,const std::string & token,int32_t status,int32_t reason)621 void DeviceManagerNotify::OnAuthResult(const std::string &pkgName, const std::string &deviceId,
622                                        const std::string &token, int32_t status, int32_t reason)
623 {
624     if (pkgName.empty() || token.empty() || deviceId.empty()) {
625         LOGE("Invalid para, pkgName: %{public}s, token: %{public}s", pkgName.c_str(), GetAnonyString(token).c_str());
626         return;
627     }
628     LOGI("in, pkgName:%{public}s, status:%{public}d, reason:%{public}d", pkgName.c_str(), status, reason);
629     std::shared_ptr<AuthenticateCallback> tempCbk;
630     {
631         std::lock_guard<std::mutex> autoLock(lock_);
632         if (authenticateCallback_.count(pkgName) == 0) {
633             LOGE("DeviceManagerNotify::OnAuthResult error, authenticate callback not register for pkgName %{public}s.",
634                 pkgName.c_str());
635             return;
636         }
637         std::map<std::string, std::shared_ptr<AuthenticateCallback>> &authCallMap = authenticateCallback_[pkgName];
638         auto iter = authCallMap.find(deviceId);
639         if (iter == authCallMap.end()) {
640             LOGE("OnAuthResult error, authenticate callback not register.");
641             return;
642         }
643         tempCbk = iter->second;
644     }
645     if (tempCbk == nullptr) {
646         LOGE("OnAuthResult error, registered authenticate callback is nullptr.");
647         return;
648     }
649     tempCbk->OnAuthResult(deviceId, token, status, reason);
650     if (reason == DM_OK && (status <= STATUS_DM_CLOSE_PIN_INPUT_UI && status >= STATUS_DM_SHOW_AUTHORIZE_UI)) {
651         LOGI("update ui change, status: %{public}d, reason: %{public}d", status, reason);
652         return;
653     }
654     {
655         std::lock_guard<std::mutex> autoLock(lock_);
656         authenticateCallback_[pkgName].erase(deviceId);
657         if (authenticateCallback_[pkgName].empty()) {
658             authenticateCallback_.erase(pkgName);
659         }
660     }
661 }
662 
OnUiCall(std::string & pkgName,std::string & paramJson)663 void DeviceManagerNotify::OnUiCall(std::string &pkgName, std::string &paramJson)
664 {
665     if (pkgName.empty()) {
666         LOGE("DeviceManagerNotify::OnUiCall error: Invalid parameter, pkgName: %{public}s", pkgName.c_str());
667         return;
668     }
669     LOGI("in, pkgName:%{public}s", pkgName.c_str());
670     std::shared_ptr<DeviceManagerUiCallback> tempCbk;
671     {
672         std::lock_guard<std::mutex> autoLock(lock_);
673         if (dmUiCallback_.count(pkgName) == 0) {
674             LOGE("OnUiCall error, dm Ui callback not register for pkgName %{public}s.", pkgName.c_str());
675             return;
676         }
677         tempCbk = dmUiCallback_[pkgName];
678     }
679     if (tempCbk == nullptr) {
680         LOGE("OnUiCall error, registered dm Ui callback is nullptr.");
681         return;
682     }
683     tempCbk->OnCall(paramJson);
684 }
685 
OnCredentialResult(const std::string & pkgName,int32_t & action,const std::string & credentialResult)686 void DeviceManagerNotify::OnCredentialResult(const std::string &pkgName, int32_t &action,
687                                              const std::string &credentialResult)
688 {
689     if (pkgName.empty()) {
690         LOGE("DeviceManagerNotify::OnCredentialResult error: Invalid parameter, pkgName: %{public}s", pkgName.c_str());
691         return;
692     }
693     LOGI("in, pkgName:%{public}s, action:%{public}d", pkgName.c_str(), action);
694     std::shared_ptr<CredentialCallback> tempCbk;
695     {
696         std::lock_guard<std::mutex> autoLock(lock_);
697         if (credentialCallback_.count(pkgName) == 0) {
698             LOGE("DeviceManagerNotify::OnCredentialResult error, credential callback not register for"
699                 "pkgName %{public}s.", pkgName.c_str());
700             return;
701         }
702         tempCbk = credentialCallback_[pkgName];
703     }
704     if (tempCbk == nullptr) {
705         LOGE("OnCredentialResult error, registered credential callback is nullptr.");
706         return;
707     }
708     tempCbk->OnCredentialResult(action, credentialResult);
709 }
710 
RegisterBindCallback(const std::string & pkgName,const PeerTargetId & targetId,std::shared_ptr<BindTargetCallback> callback)711 void DeviceManagerNotify::RegisterBindCallback(const std::string &pkgName, const PeerTargetId &targetId,
712     std::shared_ptr<BindTargetCallback> callback)
713 {
714     if (pkgName.empty() || IsInvalidPeerTargetId(targetId) || (callback == nullptr)) {
715         LOGE("DeviceManagerNotify::RegisterBindCallback error: Invalid parameter, pkgName: %{public}s.",
716             pkgName.c_str());
717         return;
718     }
719     std::lock_guard<std::mutex> autoLock(bindLock_);
720     if (bindCallback_.count(pkgName) == 0) {
721         CHECK_SIZE_VOID(bindCallback_);
722         bindCallback_[pkgName] = std::map<PeerTargetId, std::shared_ptr<BindTargetCallback>>();
723     }
724     bindCallback_[pkgName][targetId] = callback;
725 }
726 
RegisterUnbindCallback(const std::string & pkgName,const PeerTargetId & targetId,std::shared_ptr<UnbindTargetCallback> callback)727 void DeviceManagerNotify::RegisterUnbindCallback(const std::string &pkgName, const PeerTargetId &targetId,
728     std::shared_ptr<UnbindTargetCallback> callback)
729 {
730     if (pkgName.empty() || IsInvalidPeerTargetId(targetId) || (callback == nullptr)) {
731         LOGE("DeviceManagerNotify::RegisterUnbindCallback error: Invalid parameter, pkgName: %{public}s.",
732             pkgName.c_str());
733         return;
734     }
735     std::lock_guard<std::mutex> autoLock(lock_);
736     if (unbindCallback_.count(pkgName) == 0) {
737         CHECK_SIZE_VOID(unbindCallback_);
738         unbindCallback_[pkgName] = std::map<PeerTargetId, std::shared_ptr<UnbindTargetCallback>>();
739     }
740     unbindCallback_[pkgName][targetId] = callback;
741 }
742 
OnBindResult(const std::string & pkgName,const PeerTargetId & targetId,int32_t result,int32_t status,std::string content)743 void DeviceManagerNotify::OnBindResult(const std::string &pkgName, const PeerTargetId &targetId,
744     int32_t result, int32_t status, std::string content)
745 {
746     if (pkgName.empty() || IsInvalidPeerTargetId(targetId)) {
747         LOGE("Invalid para, pkgName: %{public}s.", pkgName.c_str());
748         return;
749     }
750     LOGI("in, pkgName:%{public}s, result:%{public}d", pkgName.c_str(), result);
751     std::shared_ptr<BindTargetCallback> tempCbk;
752     {
753         std::lock_guard<std::mutex> autoLock(bindLock_);
754         if (bindCallback_.count(pkgName) == 0) {
755             LOGE("DeviceManagerNotify::OnBindResult error, callback not register for pkgName %{public}s.",
756                 pkgName.c_str());
757             return;
758         }
759         std::map<PeerTargetId, std::shared_ptr<BindTargetCallback>> &bindCbkMap = bindCallback_[pkgName];
760         auto iter = bindCbkMap.find(targetId);
761         if (iter == bindCbkMap.end()) {
762             LOGE("OnBindResult error, bind callback not register for targetId.");
763             return;
764         }
765         tempCbk = iter->second;
766         if (result != DM_OK || status == STATUS_DM_AUTH_FINISH || status == STATUS_DM_AUTH_DEFAULT) {
767             LOGI("notify end, result: %{public}d, status: %{public}d", result, status);
768             bindCallback_[pkgName].erase(targetId);
769             if (bindCallback_[pkgName].empty()) {
770                 bindCallback_.erase(pkgName);
771             }
772         }
773     }
774     if (tempCbk == nullptr) {
775         LOGE("OnBindResult error, registered bind callback is nullptr.");
776         return;
777     }
778     tempCbk->OnBindResult(targetId, result, status, content);
779 }
780 
OnUnbindResult(const std::string & pkgName,const PeerTargetId & targetId,int32_t result,std::string content)781 void DeviceManagerNotify::OnUnbindResult(const std::string &pkgName, const PeerTargetId &targetId,
782     int32_t result, std::string content)
783 {
784     if (pkgName.empty() || IsInvalidPeerTargetId(targetId)) {
785         LOGE("Invalid para, pkgName: %{public}s.", pkgName.c_str());
786         return;
787     }
788     LOGI("in, pkgName:%{public}s, result:%{public}d", pkgName.c_str(), result);
789     std::shared_ptr<UnbindTargetCallback> tempCbk;
790     {
791         std::lock_guard<std::mutex> autoLock(lock_);
792         if (unbindCallback_.count(pkgName) == 0) {
793             LOGE("DeviceManagerNotify::OnUnbindResult error, callback not register for pkgName %{public}s.",
794                 pkgName.c_str());
795             return;
796         }
797         std::map<PeerTargetId, std::shared_ptr<UnbindTargetCallback>> &unbindCbkMap = unbindCallback_[pkgName];
798         auto iter = unbindCbkMap.find(targetId);
799         if (iter == unbindCbkMap.end()) {
800             LOGE("OnUnbindResult error, unbind callback not register for targetId.");
801             return;
802         }
803         tempCbk = iter->second;
804     }
805     if (tempCbk == nullptr) {
806         LOGE("OnUnbindResult error, registered unbind callback is nullptr.");
807         return;
808     }
809     tempCbk->OnUnbindResult(targetId, result, content);
810     {
811         std::lock_guard<std::mutex> autoLock(lock_);
812         unbindCallback_[pkgName].erase(targetId);
813         if (unbindCallback_[pkgName].empty()) {
814             unbindCallback_.erase(pkgName);
815         }
816     }
817 }
818 
OnPinHolderCreate(const std::string & pkgName,const std::string & deviceId,DmPinType pinType,const std::string & payload)819 void DeviceManagerNotify::OnPinHolderCreate(const std::string &pkgName, const std::string &deviceId,
820     DmPinType pinType, const std::string &payload)
821 {
822     if (pkgName.empty()) {
823         LOGE("Invalid parameter, pkgName is empty.");
824         return;
825     }
826     LOGI("in, pkgName:%{public}s", pkgName.c_str());
827     std::shared_ptr<PinHolderCallback> tempCbk;
828     {
829         std::lock_guard<std::mutex> autoLock(lock_);
830         if (pinHolderCallback_.count(pkgName) == 0) {
831             LOGE("OnPinHolderCreate error, device state callback not register.");
832             return;
833         }
834         tempCbk = pinHolderCallback_[pkgName];
835     }
836     if (tempCbk == nullptr) {
837         LOGE("OnPinHolderCreate error, registered device state callback is nullptr.");
838         return;
839     }
840     tempCbk->OnPinHolderCreate(deviceId, pinType, payload);
841 }
842 
OnPinHolderDestroy(const std::string & pkgName,DmPinType pinType,const std::string & payload)843 void DeviceManagerNotify::OnPinHolderDestroy(const std::string &pkgName, DmPinType pinType,
844     const std::string &payload)
845 {
846     if (pkgName.empty()) {
847         LOGE("Invalid parameter, pkgName is empty.");
848         return;
849     }
850     LOGI("in, pkgName:%{public}s", pkgName.c_str());
851     std::shared_ptr<PinHolderCallback> tempCbk;
852     {
853         std::lock_guard<std::mutex> autoLock(lock_);
854         if (pinHolderCallback_.count(pkgName) == 0) {
855             LOGE("OnPinHolderDestroy error, device state callback not register.");
856             return;
857         }
858         tempCbk = pinHolderCallback_[pkgName];
859     }
860     if (tempCbk == nullptr) {
861         LOGE("OnPinHolderDestroy error, registered device state callback is nullptr.");
862         return;
863     }
864     tempCbk->OnPinHolderDestroy(pinType, payload);
865 }
866 
OnCreateResult(const std::string & pkgName,int32_t result)867 void DeviceManagerNotify::OnCreateResult(const std::string &pkgName, int32_t result)
868 {
869     if (pkgName.empty()) {
870         LOGE("Invalid parameter, pkgName is empty.");
871         return;
872     }
873     LOGI("in, pkgName:%{public}s", pkgName.c_str());
874     std::shared_ptr<PinHolderCallback> tempCbk;
875     {
876         std::lock_guard<std::mutex> autoLock(lock_);
877         if (pinHolderCallback_.count(pkgName) == 0) {
878             LOGE("OnCreateResult error, device state callback not register.");
879             return;
880         }
881         tempCbk = pinHolderCallback_[pkgName];
882     }
883     if (tempCbk == nullptr) {
884         LOGE("OnCreateResult error, registered device state callback is nullptr.");
885         return;
886     }
887     tempCbk->OnCreateResult(result);
888 }
889 
OnDestroyResult(const std::string & pkgName,int32_t result)890 void DeviceManagerNotify::OnDestroyResult(const std::string &pkgName, int32_t result)
891 {
892     if (pkgName.empty()) {
893         LOGE("Invalid parameter, pkgName is empty.");
894         return;
895     }
896     LOGI("in, pkgName:%{public}s", pkgName.c_str());
897     std::shared_ptr<PinHolderCallback> tempCbk;
898     {
899         std::lock_guard<std::mutex> autoLock(lock_);
900         if (pinHolderCallback_.count(pkgName) == 0) {
901             LOGE("OnDestroyResult error, device state callback not register.");
902             return;
903         }
904         tempCbk = pinHolderCallback_[pkgName];
905     }
906     if (tempCbk == nullptr) {
907         LOGE("OnDestroyResult error, registered device state callback is nullptr.");
908         return;
909     }
910     tempCbk->OnDestroyResult(result);
911 }
912 
OnPinHolderEvent(const std::string & pkgName,DmPinHolderEvent event,int32_t result,const std::string & content)913 void DeviceManagerNotify::OnPinHolderEvent(const std::string &pkgName, DmPinHolderEvent event, int32_t result,
914                                            const std::string &content)
915 {
916     if (pkgName.empty()) {
917         LOGE("Invalid parameter, pkgName is empty.");
918         return;
919     }
920     LOGI("in, pkgName:%{public}s", pkgName.c_str());
921     std::shared_ptr<PinHolderCallback> tempCbk;
922     {
923         std::lock_guard<std::mutex> autoLock(lock_);
924         if (pinHolderCallback_.count(pkgName) == 0) {
925             LOGE("OnPinHolderEvent error, pin holder callback not register.");
926             return;
927         }
928         tempCbk = pinHolderCallback_[pkgName];
929     }
930     if (tempCbk == nullptr) {
931         LOGE("OnPinHolderEvent error, registered pin holder callback is nullptr.");
932         return;
933     }
934     tempCbk->OnPinHolderEvent(event, result, content);
935 }
936 
GetDmInitCallback()937 std::map<std::string, std::shared_ptr<DmInitCallback>> DeviceManagerNotify::GetDmInitCallback()
938 {
939     std::lock_guard<std::mutex> autoLock(lock_);
940     std::map<std::string, std::shared_ptr<DmInitCallback>> currentDmInitCallback = dmInitCallback_;
941     return currentDmInitCallback;
942 }
943 
DeviceInfoOnline(const DmDeviceInfo & deviceInfo,std::shared_ptr<DeviceStateCallback> tempCbk)944 void DeviceManagerNotify::DeviceInfoOnline(const DmDeviceInfo &deviceInfo, std::shared_ptr<DeviceStateCallback> tempCbk)
945 {
946     tempCbk->OnDeviceOnline(deviceInfo);
947 }
948 
DeviceInfoOffline(const DmDeviceInfo & deviceInfo,std::shared_ptr<DeviceStateCallback> tempCbk)949 void DeviceManagerNotify::DeviceInfoOffline(const DmDeviceInfo &deviceInfo,
950     std::shared_ptr<DeviceStateCallback> tempCbk)
951 {
952     tempCbk->OnDeviceOffline(deviceInfo);
953 }
954 
DeviceInfoChanged(const DmDeviceInfo & deviceInfo,std::shared_ptr<DeviceStateCallback> tempCbk)955 void DeviceManagerNotify::DeviceInfoChanged(const DmDeviceInfo &deviceInfo,
956     std::shared_ptr<DeviceStateCallback> tempCbk)
957 {
958     tempCbk->OnDeviceChanged(deviceInfo);
959 }
960 
DeviceInfoReady(const DmDeviceInfo & deviceInfo,std::shared_ptr<DeviceStateCallback> tempCbk)961 void DeviceManagerNotify::DeviceInfoReady(const DmDeviceInfo &deviceInfo, std::shared_ptr<DeviceStateCallback> tempCbk)
962 {
963     tempCbk->OnDeviceReady(deviceInfo);
964 }
965 
DeviceBasicInfoOnline(const DmDeviceBasicInfo & deviceBasicInfo,std::shared_ptr<DeviceStatusCallback> tempCbk)966 void DeviceManagerNotify::DeviceBasicInfoOnline(const DmDeviceBasicInfo &deviceBasicInfo,
967     std::shared_ptr<DeviceStatusCallback> tempCbk)
968 {
969     tempCbk->OnDeviceOnline(deviceBasicInfo);
970 }
971 
DeviceBasicInfoOffline(const DmDeviceBasicInfo & deviceBasicInfo,std::shared_ptr<DeviceStatusCallback> tempCbk)972 void DeviceManagerNotify::DeviceBasicInfoOffline(const DmDeviceBasicInfo &deviceBasicInfo,
973     std::shared_ptr<DeviceStatusCallback> tempCbk)
974 {
975     tempCbk->OnDeviceOffline(deviceBasicInfo);
976 }
977 
DeviceBasicInfoChanged(const DmDeviceBasicInfo & deviceBasicInfo,std::shared_ptr<DeviceStatusCallback> tempCbk)978 void DeviceManagerNotify::DeviceBasicInfoChanged(const DmDeviceBasicInfo &deviceBasicInfo,
979     std::shared_ptr<DeviceStatusCallback> tempCbk)
980 {
981     tempCbk->OnDeviceChanged(deviceBasicInfo);
982 }
983 
DeviceBasicInfoReady(const DmDeviceBasicInfo & deviceBasicInfo,std::shared_ptr<DeviceStatusCallback> tempCbk)984 void DeviceManagerNotify::DeviceBasicInfoReady(const DmDeviceBasicInfo &deviceBasicInfo,
985     std::shared_ptr<DeviceStatusCallback> tempCbk)
986 {
987     tempCbk->OnDeviceReady(deviceBasicInfo);
988 }
989 
RegDevTrustChangeCallback(const std::string & pkgName,std::shared_ptr<DevTrustChangeCallback> callback)990 void DeviceManagerNotify::RegDevTrustChangeCallback(const std::string &pkgName,
991     std::shared_ptr<DevTrustChangeCallback> callback)
992 {
993     if (pkgName.empty() || callback == nullptr) {
994         LOGE("Invalid parameter, pkgName is empty or callback is nullptr.");
995         return;
996     }
997     std::lock_guard<std::mutex> autoLock(lock_);
998     CHECK_SIZE_VOID(devTrustChangeCallback_);
999     devTrustChangeCallback_[pkgName] = callback;
1000 }
1001 
OnDeviceTrustChange(const std::string & pkgName,const std::string & udid,const std::string & uuid,int32_t authForm)1002 void DeviceManagerNotify::OnDeviceTrustChange(const std::string &pkgName, const std::string &udid,
1003     const std::string &uuid, int32_t authForm)
1004 {
1005     LOGI("PkgName %{public}s, udid %{public}s, uuid %{public}s, authForm %{public}d", pkgName.c_str(),
1006         GetAnonyString(udid).c_str(), GetAnonyString(uuid).c_str(), authForm);
1007     if (pkgName.empty() || authForm < static_cast<int32_t>(INVALID_TYPE) ||
1008         authForm > static_cast<int32_t>(ACROSS_ACCOUNT)) {
1009         LOGE("Invalid parameter, pkgName is empty.");
1010         return;
1011     }
1012     std::shared_ptr<DevTrustChangeCallback> tempCbk;
1013     {
1014         std::lock_guard<std::mutex> autoLock(lock_);
1015         auto iter = devTrustChangeCallback_.find(pkgName);
1016         if (iter == devTrustChangeCallback_.end()) {
1017             LOGE("PkgName %{public}s device_trust_change callback not register.", pkgName.c_str());
1018             return;
1019         }
1020         tempCbk = iter->second;
1021     }
1022     if (tempCbk == nullptr) {
1023         LOGE("error, registered device status callback is nullptr.");
1024         return;
1025     }
1026     DmAuthForm dmAuthForm = static_cast<DmAuthForm>(authForm);
1027 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
1028     ffrt::submit([=]() { DeviceTrustChange(udid, uuid, dmAuthForm, tempCbk); });
1029 #else
1030     std::thread deviceTrustChange([=]() { DeviceTrustChange(udid, uuid, dmAuthForm, tempCbk); });
1031     if (pthread_setname_np(deviceTrustChange.native_handle(), DEVICE_TRUST_CHANGE) != DM_OK) {
1032         LOGE("deviceTrustChange set name failed.");
1033     }
1034     deviceTrustChange.detach();
1035 #endif
1036 }
1037 
DeviceTrustChange(const std::string & udid,const std::string & uuid,DmAuthForm authForm,std::shared_ptr<DevTrustChangeCallback> tempCbk)1038 void DeviceManagerNotify::DeviceTrustChange(const std::string &udid, const std::string &uuid, DmAuthForm authForm,
1039     std::shared_ptr<DevTrustChangeCallback> tempCbk)
1040 {
1041     if (tempCbk == nullptr) {
1042         LOGE("Callback ptr is nullptr.");
1043         return;
1044     }
1045     tempCbk->OnDeviceTrustChange(udid, uuid, authForm);
1046 }
1047 
RegisterDeviceScreenStatusCallback(const std::string & pkgName,std::shared_ptr<DeviceScreenStatusCallback> callback)1048 void DeviceManagerNotify::RegisterDeviceScreenStatusCallback(const std::string &pkgName,
1049     std::shared_ptr<DeviceScreenStatusCallback> callback)
1050 {
1051     if (pkgName.empty() || callback == nullptr) {
1052         LOGE("Invalid parameter, pkgName is empty or callback is nullptr.");
1053         return;
1054     }
1055     std::lock_guard<std::mutex> autoLock(lock_);
1056     CHECK_SIZE_VOID(deviceScreenStatusCallback_);
1057     deviceScreenStatusCallback_[pkgName] = callback;
1058 }
1059 
UnRegisterDeviceScreenStatusCallback(const std::string & pkgName)1060 void DeviceManagerNotify::UnRegisterDeviceScreenStatusCallback(const std::string &pkgName)
1061 {
1062     if (pkgName.empty()) {
1063         LOGE("Invalid parameter, pkgName is empty.");
1064         return;
1065     }
1066     std::lock_guard<std::mutex> autoLock(lock_);
1067     deviceScreenStatusCallback_.erase(pkgName);
1068 }
1069 
OnDeviceScreenStatus(const std::string & pkgName,const DmDeviceInfo & deviceInfo)1070 void DeviceManagerNotify::OnDeviceScreenStatus(const std::string &pkgName, const DmDeviceInfo &deviceInfo)
1071 {
1072     if (pkgName.empty()) {
1073         LOGE("Invalid parameter, pkgName is empty.");
1074         return;
1075     }
1076     LOGI("In, pkgName:%{public}s", pkgName.c_str());
1077     std::shared_ptr<DeviceScreenStatusCallback> tempCbk;
1078     {
1079         std::lock_guard<std::mutex> autoLock(lock_);
1080         if (deviceScreenStatusCallback_.count(pkgName) == 0) {
1081             LOGE("error, device screen status not register.");
1082             return;
1083         }
1084         tempCbk = deviceScreenStatusCallback_[pkgName];
1085     }
1086     if (tempCbk == nullptr) {
1087         LOGE("error, registered device screen status callback is nullptr.");
1088         return;
1089     }
1090     tempCbk->OnDeviceScreenStatus(deviceInfo);
1091 }
1092 
RegisterCredentialAuthStatusCallback(const std::string & pkgName,std::shared_ptr<CredentialAuthStatusCallback> callback)1093 void DeviceManagerNotify::RegisterCredentialAuthStatusCallback(const std::string &pkgName,
1094     std::shared_ptr<CredentialAuthStatusCallback> callback)
1095 {
1096     if (pkgName.empty() || callback == nullptr) {
1097         LOGE("Invalid parameter, pkgName is empty or callback is nullptr.");
1098         return;
1099     }
1100     std::lock_guard<std::mutex> autoLock(lock_);
1101     CHECK_SIZE_VOID(credentialAuthStatusCallback_);
1102     credentialAuthStatusCallback_[pkgName] = callback;
1103 }
1104 
UnRegisterCredentialAuthStatusCallback(const std::string & pkgName)1105 void DeviceManagerNotify::UnRegisterCredentialAuthStatusCallback(const std::string &pkgName)
1106 {
1107     if (pkgName.empty()) {
1108         LOGE("Invalid parameter, pkgName is empty.");
1109         return;
1110     }
1111     std::lock_guard<std::mutex> autoLock(lock_);
1112     credentialAuthStatusCallback_.erase(pkgName);
1113 }
1114 
OnCredentialAuthStatus(const std::string & pkgName,const std::string & deviceList,uint16_t deviceTypeId,int32_t errcode)1115 void DeviceManagerNotify::OnCredentialAuthStatus(const std::string &pkgName, const std::string &deviceList,
1116                                                  uint16_t deviceTypeId, int32_t errcode)
1117 {
1118     if (pkgName.empty()) {
1119         LOGE("Invalid parameter, pkgName is empty.");
1120         return;
1121     }
1122     LOGI("In, pkgName:%{public}s", pkgName.c_str());
1123     std::shared_ptr<CredentialAuthStatusCallback> tempCbk;
1124     {
1125         std::lock_guard<std::mutex> autoLock(lock_);
1126         if (credentialAuthStatusCallback_.find(pkgName) == credentialAuthStatusCallback_.end()) {
1127             LOGE("error, credential auth statusnot register.");
1128             return;
1129         }
1130         tempCbk = credentialAuthStatusCallback_[pkgName];
1131     }
1132     if (tempCbk == nullptr) {
1133         LOGE("error, registered credential auth status callback is nullptr.");
1134         return;
1135     }
1136     tempCbk->OnCredentialAuthStatus(deviceList, deviceTypeId, errcode);
1137 }
1138 
RegisterSinkBindCallback(const std::string & pkgName,std::shared_ptr<BindTargetCallback> callback)1139 void DeviceManagerNotify::RegisterSinkBindCallback(const std::string &pkgName,
1140     std::shared_ptr<BindTargetCallback> callback)
1141 {
1142     if (pkgName.empty() || callback == nullptr) {
1143         LOGE("Invalid parameter, pkgName is empty or callback is nullptr.");
1144         return;
1145     }
1146     std::lock_guard<std::mutex> autoLock(lock_);
1147     CHECK_SIZE_VOID(sinkBindTargetCallback_);
1148     sinkBindTargetCallback_[pkgName] = callback;
1149 }
1150 
UnRegisterSinkBindCallback(const std::string & pkgName)1151 void DeviceManagerNotify::UnRegisterSinkBindCallback(const std::string &pkgName)
1152 {
1153     if (pkgName.empty()) {
1154         LOGE("Invalid parameter, pkgName is empty.");
1155         return;
1156     }
1157     std::lock_guard<std::mutex> autoLock(lock_);
1158     sinkBindTargetCallback_.erase(pkgName);
1159 }
1160 
OnSinkBindResult(const std::string & pkgName,const PeerTargetId & targetId,int32_t result,int32_t status,std::string content)1161 void DeviceManagerNotify::OnSinkBindResult(const std::string &pkgName, const PeerTargetId &targetId,
1162     int32_t result, int32_t status, std::string content)
1163 {
1164     if (pkgName.empty()) {
1165         LOGE("Invalid para, pkgName: %{public}s.", pkgName.c_str());
1166         return;
1167     }
1168     LOGI("in, pkgName:%{public}s, result:%{public}d", pkgName.c_str(), result);
1169     std::shared_ptr<BindTargetCallback> tempCbk;
1170     {
1171         std::lock_guard<std::mutex> autoLock(lock_);
1172         if (sinkBindTargetCallback_.find(pkgName) == sinkBindTargetCallback_.end()) {
1173             LOGE("error, sink bind callback not register.");
1174             return;
1175         }
1176         tempCbk = sinkBindTargetCallback_[pkgName];
1177     }
1178     if (tempCbk == nullptr) {
1179         LOGE("error, registered sink bind callback is nullptr.");
1180         return;
1181     }
1182     tempCbk->OnBindResult(targetId, result, status, content);
1183 }
1184 
GetDiscoveryCallback(const std::string & pkgName,uint16_t subscribeId)1185 std::shared_ptr<DiscoveryCallback> DeviceManagerNotify::GetDiscoveryCallback(const std::string &pkgName,
1186     uint16_t subscribeId)
1187 {
1188     std::string discWithSubscribeId = ComposeStr(pkgName, subscribeId);
1189     std::lock_guard<std::mutex> autoLock(lock_);
1190     auto iter = deviceDiscoveryCallbacks_.find(discWithSubscribeId);
1191     if (iter != deviceDiscoveryCallbacks_.end()) {
1192         auto subIter = iter->second.find(subscribeId);
1193         if (subIter != iter->second.end()) {
1194             return subIter->second;
1195         }
1196         return nullptr;
1197     }
1198     std::string discNoSubscribeId = ComposeStr(pkgName, DM_INVALID_FLAG_ID);
1199     iter = deviceDiscoveryCallbacks_.find(discNoSubscribeId);
1200     if (iter != deviceDiscoveryCallbacks_.end()) {
1201         auto subIter = iter->second.find(subscribeId);
1202         if (subIter != iter->second.end()) {
1203             return subIter->second;
1204         }
1205     }
1206     return nullptr;
1207 }
1208 
GetCallBack(std::map<DmCommonNotifyEvent,std::set<std::string>> & callbackMap)1209 void DeviceManagerNotify::GetCallBack(std::map<DmCommonNotifyEvent, std::set<std::string>> &callbackMap)
1210 {
1211     std::lock_guard<std::mutex> autoLock(lock_);
1212     std::set<std::string> statePkgnameSet;
1213     for (auto it : deviceStateCallback_) {
1214         statePkgnameSet.insert(it.first);
1215     }
1216     for (auto it : deviceStatusCallback_) {
1217         statePkgnameSet.insert(it.first);
1218     }
1219     if (statePkgnameSet.size() > 0) {
1220         callbackMap[DmCommonNotifyEvent::REG_DEVICE_STATE] = statePkgnameSet;
1221     }
1222 
1223     std::set<std::string> trustChangePkgnameSet;
1224     for (auto it : devTrustChangeCallback_) {
1225         trustChangePkgnameSet.insert(it.first);
1226     }
1227     if (trustChangePkgnameSet.size() > 0) {
1228         callbackMap[DmCommonNotifyEvent::REG_REMOTE_DEVICE_TRUST_CHANGE] = trustChangePkgnameSet;
1229     }
1230 
1231     std::set<std::string> screenStatusPkgnameSet;
1232     for (auto it : deviceScreenStatusCallback_) {
1233         screenStatusPkgnameSet.insert(it.first);
1234     }
1235     if (screenStatusPkgnameSet.size() > 0) {
1236         callbackMap[DmCommonNotifyEvent::REG_DEVICE_SCREEN_STATE] = screenStatusPkgnameSet;
1237     }
1238 
1239     std::set<std::string> authStatusPkgnameSet;
1240     for (auto it : credentialAuthStatusCallback_) {
1241         authStatusPkgnameSet.insert(it.first);
1242     }
1243     if (authStatusPkgnameSet.size() > 0) {
1244         callbackMap[DmCommonNotifyEvent::REG_CREDENTIAL_AUTH_STATUS_NOTIFY] = authStatusPkgnameSet;
1245     }
1246 }
1247 
RegisterGetDeviceProfileInfoListCallback(const std::string & pkgName,std::shared_ptr<GetDeviceProfileInfoListCallback> callback)1248 int32_t DeviceManagerNotify::RegisterGetDeviceProfileInfoListCallback(const std::string &pkgName,
1249     std::shared_ptr<GetDeviceProfileInfoListCallback> callback)
1250 {
1251     LOGI("In, pkgName: %{public}s.", pkgName.c_str());
1252     if (callback == nullptr || pkgName.empty()) {
1253         LOGE("callback is null or pkgName is empty");
1254         return ERR_DM_CALLBACK_REGISTER_FAILED;
1255     }
1256     std::lock_guard<std::mutex> autoLock(bindLock_);
1257     CHECK_SIZE_RETURN(getDeviceProfileInfoCallback_, ERR_DM_CALLBACK_REGISTER_FAILED);
1258     getDeviceProfileInfoCallback_[pkgName] = callback;
1259     return DM_OK;
1260 }
1261 
OnGetDeviceProfileInfoListResult(const std::string & pkgName,const std::vector<DmDeviceProfileInfo> & deviceProfileInfos,int32_t code)1262 void DeviceManagerNotify::OnGetDeviceProfileInfoListResult(const std::string &pkgName,
1263     const std::vector<DmDeviceProfileInfo> &deviceProfileInfos, int32_t code)
1264 {
1265     if (pkgName.empty()) {
1266         LOGE("Invalid para, pkgName: %{public}s.", pkgName.c_str());
1267         return;
1268     }
1269     LOGI("In, pkgName:%{public}s, code:%{public}d", pkgName.c_str(), code);
1270     std::shared_ptr<GetDeviceProfileInfoListCallback> tempCbk;
1271     {
1272         std::lock_guard<std::mutex> autoLock(bindLock_);
1273         if (getDeviceProfileInfoCallback_.count(pkgName) == 0) {
1274             LOGE("error, callback not register for pkgName %{public}s.", pkgName.c_str());
1275             return;
1276         }
1277         tempCbk = getDeviceProfileInfoCallback_[pkgName];
1278         getDeviceProfileInfoCallback_.erase(pkgName);
1279     }
1280     if (tempCbk == nullptr) {
1281         LOGE("error, registered GetDeviceProfileInfoList callback is nullptr.");
1282         return;
1283     }
1284     tempCbk->OnResult(deviceProfileInfos, code);
1285 }
1286 
RegisterGetDeviceIconInfoCallback(const std::string & pkgName,const std::string & uk,std::shared_ptr<GetDeviceIconInfoCallback> callback)1287 int32_t DeviceManagerNotify::RegisterGetDeviceIconInfoCallback(const std::string &pkgName, const std::string &uk,
1288     std::shared_ptr<GetDeviceIconInfoCallback> callback)
1289 {
1290     LOGI("In, pkgName: %{public}s.", pkgName.c_str());
1291     if (callback == nullptr || pkgName.empty()) {
1292         LOGE("callback is null or pkgName is empty");
1293         return ERR_DM_CALLBACK_REGISTER_FAILED;
1294     }
1295     std::lock_guard<std::mutex> autoLock(bindLock_);
1296     CHECK_SIZE_RETURN(getDeviceIconInfoCallback_, ERR_DM_CALLBACK_REGISTER_FAILED);
1297     auto iter = getDeviceIconInfoCallback_.find(pkgName);
1298     if (iter == getDeviceIconInfoCallback_.end()) {
1299         getDeviceIconInfoCallback_[pkgName][uk] = {callback};
1300         return DM_OK;
1301     }
1302     CHECK_SIZE_RETURN(iter->second, ERR_DM_CALLBACK_REGISTER_FAILED);
1303     CHECK_SIZE_RETURN(iter->second[uk], ERR_DM_CALLBACK_REGISTER_FAILED);
1304     iter->second[uk].insert(callback);
1305     return DM_OK;
1306 }
1307 
OnGetDeviceIconInfoResult(const std::string & pkgName,const DmDeviceIconInfo & deviceIconInfo,int32_t code)1308 void DeviceManagerNotify::OnGetDeviceIconInfoResult(const std::string &pkgName, const DmDeviceIconInfo &deviceIconInfo,
1309     int32_t code)
1310 {
1311     if (pkgName.empty()) {
1312         LOGE("Invalid para, pkgName: %{public}s.", pkgName.c_str());
1313         return;
1314     }
1315     LOGI("In, pkgName:%{public}s, code:%{public}d", pkgName.c_str(), code);
1316     std::map<std::string, std::set<std::shared_ptr<GetDeviceIconInfoCallback>>> tempCbks;
1317 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
1318     std::string uk = IpcModelCodec::GetDeviceIconInfoUniqueKey(deviceIconInfo);
1319     {
1320         std::lock_guard<std::mutex> autoLock(bindLock_);
1321         auto iter = getDeviceIconInfoCallback_.find(pkgName);
1322         if (iter == getDeviceIconInfoCallback_.end()) {
1323             LOGE("error, callback not register for pkgName %{public}s.", pkgName.c_str());
1324             return;
1325         }
1326         if (ERR_DM_HILINKSVC_DISCONNECT == code) {
1327             tempCbks = iter->second;
1328             getDeviceIconInfoCallback_.erase(pkgName);
1329         } else if (iter->second.count(uk) != 0) {
1330             tempCbks[uk] = iter->second[uk];
1331             iter->second.erase(uk);
1332         }
1333     }
1334 #endif
1335     if (tempCbks.empty()) {
1336         LOGE("error, registered GetDeviceIconInfoResult callback is nullptr.");
1337         return;
1338     }
1339     for (const auto &[key, callbacks] : tempCbks) {
1340         for (auto callback : callbacks) {
1341             if (callback != nullptr) {
1342                 callback->OnResult(deviceIconInfo, code);
1343             }
1344         }
1345     }
1346 }
1347 
RegisterSetLocalDeviceNameCallback(const std::string & pkgName,std::shared_ptr<SetLocalDeviceNameCallback> callback)1348 int32_t DeviceManagerNotify::RegisterSetLocalDeviceNameCallback(const std::string &pkgName,
1349     std::shared_ptr<SetLocalDeviceNameCallback> callback)
1350 {
1351     LOGI("In, pkgName: %{public}s.", pkgName.c_str());
1352     if (callback == nullptr || pkgName.empty()) {
1353         LOGE("callback is null or pkgName is empty");
1354         return ERR_DM_CALLBACK_REGISTER_FAILED;
1355     }
1356     std::lock_guard<std::mutex> autoLock(bindLock_);
1357     CHECK_SIZE_RETURN(setLocalDeviceNameCallback_, ERR_DM_CALLBACK_REGISTER_FAILED);
1358     setLocalDeviceNameCallback_[pkgName] = callback;
1359     return DM_OK;
1360 }
1361 
RegisterSetRemoteDeviceNameCallback(const std::string & pkgName,const std::string & deviceId,std::shared_ptr<SetRemoteDeviceNameCallback> callback)1362 int32_t DeviceManagerNotify::RegisterSetRemoteDeviceNameCallback(const std::string &pkgName,
1363     const std::string &deviceId, std::shared_ptr<SetRemoteDeviceNameCallback> callback)
1364 {
1365     LOGI("In, pkgName: %{public}s.", pkgName.c_str());
1366     if (callback == nullptr || pkgName.empty() || deviceId.empty()) {
1367         LOGE("callback is null or pkgName is empty or deviceId is empty");
1368         return ERR_DM_CALLBACK_REGISTER_FAILED;
1369     }
1370     std::lock_guard<std::mutex> autoLock(bindLock_);
1371     CHECK_SIZE_RETURN(setRemoteDeviceNameCallback_, ERR_DM_CALLBACK_REGISTER_FAILED);
1372     auto iter = setRemoteDeviceNameCallback_.find(pkgName);
1373     if (iter == setRemoteDeviceNameCallback_.end()) {
1374         setRemoteDeviceNameCallback_[pkgName][deviceId] = callback;
1375         return DM_OK;
1376     }
1377     CHECK_SIZE_RETURN(iter->second, ERR_DM_CALLBACK_REGISTER_FAILED);
1378     iter->second[deviceId] = callback;
1379     return DM_OK;
1380 }
1381 
OnSetLocalDeviceNameResult(const std::string & pkgName,int32_t code)1382 void DeviceManagerNotify::OnSetLocalDeviceNameResult(const std::string &pkgName, int32_t code)
1383 {
1384     if (pkgName.empty()) {
1385         LOGE("Invalid para, pkgName : %{public}s", pkgName.c_str());
1386         return;
1387     }
1388     LOGI("In, pkgName:%{public}s, code:%{public}d", pkgName.c_str(), code);
1389     std::shared_ptr<SetLocalDeviceNameCallback> tempCbk;
1390     {
1391         std::lock_guard<std::mutex> autoLock(bindLock_);
1392         if (setLocalDeviceNameCallback_.find(pkgName) == setLocalDeviceNameCallback_.end()) {
1393             LOGE("error, callback not register for pkgName %{public}s.", pkgName.c_str());
1394             return;
1395         }
1396         tempCbk = setLocalDeviceNameCallback_[pkgName];
1397         setLocalDeviceNameCallback_.erase(pkgName);
1398     }
1399     if (tempCbk == nullptr) {
1400         LOGE("error, registered SetLocalDeviceName callback is nullptr.");
1401         return;
1402     }
1403     tempCbk->OnResult(code);
1404 }
1405 
OnSetRemoteDeviceNameResult(const std::string & pkgName,const std::string & deviceId,int32_t code)1406 void DeviceManagerNotify::OnSetRemoteDeviceNameResult(const std::string &pkgName, const std::string &deviceId,
1407     int32_t code)
1408 {
1409     if (pkgName.empty() || deviceId.empty()) {
1410         LOGE("Invalid para, pkgName : %{public}s, deviceId : %{public}s",
1411             pkgName.c_str(), GetAnonyString(deviceId).c_str());
1412         return;
1413     }
1414     LOGI("In, pkgName:%{public}s, code:%{public}d", pkgName.c_str(), code);
1415     std::map<std::string, std::shared_ptr<SetRemoteDeviceNameCallback>> tempCbks;
1416     {
1417         std::lock_guard<std::mutex> autoLock(bindLock_);
1418         auto iter = setRemoteDeviceNameCallback_.find(pkgName);
1419         if (iter == setRemoteDeviceNameCallback_.end()) {
1420             LOGE("error, callback not register for pkgName %{public}s.", pkgName.c_str());
1421             return;
1422         }
1423         if (ERR_DM_HILINKSVC_DISCONNECT == code) {
1424             tempCbks = iter->second;
1425             setRemoteDeviceNameCallback_.erase(pkgName);
1426         } else {
1427             if (iter->second.find(deviceId) != iter->second.end()) {
1428                 tempCbks[deviceId] = iter->second[deviceId];
1429                 iter->second.erase(deviceId);
1430             }
1431         }
1432     }
1433     if (tempCbks.empty()) {
1434         LOGE("error, registered SetRemoteDeviceNameResult callback is empty.");
1435         return;
1436     }
1437     for (const auto &[key, callback] : tempCbks) {
1438         if (callback != nullptr) {
1439             callback->OnResult(code);
1440         }
1441     }
1442 }
1443 
UnRegisterPinHolderCallback(const std::string & pkgName)1444 void DeviceManagerNotify::UnRegisterPinHolderCallback(const std::string &pkgName)
1445 {
1446     if (pkgName.empty()) {
1447         LOGE("Invalid parameter, pkgName is empty.");
1448         return;
1449     }
1450     std::lock_guard<std::mutex> autoLock(lock_);
1451     pinHolderCallback_.erase(pkgName);
1452 }
1453 } // namespace DistributedHardware
1454 } // namespace OHOS
1455