• 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 "device_manager_notify.h"
17 #include <thread>
18 
19 #include "device_manager.h"
20 #include "dm_anonymous.h"
21 #include "dm_constants.h"
22 #include "dm_log.h"
23 
24 namespace OHOS {
25 namespace DistributedHardware {
26 DM_IMPLEMENT_SINGLE_INSTANCE(DeviceManagerNotify);
27 
28 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
29 constexpr const char* DEVICE_STATE_INIT_QUEUE = "deviceStateInitQueue";
30 #else
31 constexpr const char* DEVICE_ONLINE = "deviceOnLine";
32 constexpr const char* DEVICE_OFFLINE = "deviceOffLine";
33 constexpr const char* DEVICEINFO_CHANGE = "deviceInfoChange";
34 constexpr const char* DEVICE_READY = "deviceReady";
35 #endif
36 
RegisterDeathRecipientCallback(const std::string & pkgName,std::shared_ptr<DmInitCallback> dmInitCallback)37 void DeviceManagerNotify::RegisterDeathRecipientCallback(const std::string &pkgName,
38                                                          std::shared_ptr<DmInitCallback> dmInitCallback)
39 {
40     if (pkgName.empty() || dmInitCallback == nullptr) {
41         LOGE("Invalid parameter, pkgName is empty or callback is nullptr.");
42         return;
43     }
44     std::lock_guard<std::mutex> autoLock(lock_);
45     dmInitCallback_[pkgName] = dmInitCallback;
46 
47 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
48     if (ffrtQueue_ != nullptr) {
49         LOGI("FfrtQueue has created!");
50         return;
51     }
52     ffrtQueue_ = std::make_shared<ffrt::queue>(DEVICE_STATE_INIT_QUEUE,
53         ffrt::queue_attr().qos(ffrt::qos_default));
54     if (ffrtQueue_ == nullptr) {
55         LOGE("FfrtQueue create failed!");
56         return;
57     }
58 #endif
59 }
60 
UnRegisterDeathRecipientCallback(const std::string & pkgName)61 void DeviceManagerNotify::UnRegisterDeathRecipientCallback(const std::string &pkgName)
62 {
63     if (pkgName.empty()) {
64         LOGE("Invalid parameter, pkgName is empty.");
65         return;
66     }
67     std::lock_guard<std::mutex> autoLock(lock_);
68     dmInitCallback_.erase(pkgName);
69 }
70 
RegisterDeviceStateCallback(const std::string & pkgName,std::shared_ptr<DeviceStateCallback> callback)71 void DeviceManagerNotify::RegisterDeviceStateCallback(const std::string &pkgName,
72                                                       std::shared_ptr<DeviceStateCallback> callback)
73 {
74     if (pkgName.empty() || callback == nullptr) {
75         LOGE("Invalid parameter, pkgName is empty or callback is nullptr.");
76         return;
77     }
78     std::lock_guard<std::mutex> autoLock(lock_);
79     deviceStateCallback_[pkgName] = callback;
80 }
81 
UnRegisterDeviceStateCallback(const std::string & pkgName)82 void DeviceManagerNotify::UnRegisterDeviceStateCallback(const std::string &pkgName)
83 {
84     if (pkgName.empty()) {
85         LOGE("Invalid parameter, pkgName is empty.");
86         return;
87     }
88     std::lock_guard<std::mutex> autoLock(lock_);
89     deviceStateCallback_.erase(pkgName);
90 }
91 
UnRegisterDeviceStatusCallback(const std::string & pkgName)92 void DeviceManagerNotify::UnRegisterDeviceStatusCallback(const std::string &pkgName)
93 {
94     if (pkgName.empty()) {
95         LOGE("Invalid parameter, pkgName is empty.");
96         return;
97     }
98     std::lock_guard<std::mutex> autoLock(lock_);
99     deviceStatusCallback_.erase(pkgName);
100 }
101 
RegisterDeviceStatusCallback(const std::string & pkgName,std::shared_ptr<DeviceStatusCallback> callback)102 void DeviceManagerNotify::RegisterDeviceStatusCallback(const std::string &pkgName,
103     std::shared_ptr<DeviceStatusCallback> callback)
104 {
105     if (pkgName.empty() || callback == nullptr) {
106         LOGE("Invalid parameter, pkgName is empty or callback is nullptr.");
107         return;
108     }
109     std::lock_guard<std::mutex> autoLock(lock_);
110     deviceStatusCallback_[pkgName] = callback;
111 }
112 
RegisterDiscoveryCallback(const std::string & pkgName,uint16_t subscribeId,std::shared_ptr<DiscoveryCallback> callback)113 void DeviceManagerNotify::RegisterDiscoveryCallback(const std::string &pkgName, uint16_t subscribeId,
114                                                     std::shared_ptr<DiscoveryCallback> callback)
115 {
116     if (pkgName.empty() || callback == nullptr) {
117         LOGE("Invalid parameter, pkgName is empty or callback is nullptr.");
118         return;
119     }
120     std::lock_guard<std::mutex> autoLock(lock_);
121     if (deviceDiscoveryCallbacks_.count(pkgName) == 0) {
122         deviceDiscoveryCallbacks_[pkgName] = std::map<uint16_t, std::shared_ptr<DiscoveryCallback>>();
123     }
124     deviceDiscoveryCallbacks_[pkgName][subscribeId] = callback;
125 }
126 
UnRegisterDiscoveryCallback(const std::string & pkgName,uint16_t subscribeId)127 void DeviceManagerNotify::UnRegisterDiscoveryCallback(const std::string &pkgName, uint16_t subscribeId)
128 {
129     if (pkgName.empty()) {
130         LOGE("Invalid parameter, pkgName is empty.");
131         return;
132     }
133     std::lock_guard<std::mutex> autoLock(lock_);
134     if (deviceDiscoveryCallbacks_.count(pkgName) > 0) {
135         deviceDiscoveryCallbacks_[pkgName].erase(subscribeId);
136         if (deviceDiscoveryCallbacks_[pkgName].empty()) {
137             deviceDiscoveryCallbacks_.erase(pkgName);
138         }
139     }
140 }
141 
RegisterPublishCallback(const std::string & pkgName,int32_t publishId,std::shared_ptr<PublishCallback> callback)142 void DeviceManagerNotify::RegisterPublishCallback(const std::string &pkgName,
143                                                   int32_t publishId,
144                                                   std::shared_ptr<PublishCallback> callback)
145 {
146     if (pkgName.empty() || callback == nullptr) {
147         LOGE("Invalid parameter, pkgName is empty or callback is nullptr.");
148         return;
149     }
150     std::lock_guard<std::mutex> autoLock(lock_);
151     if (devicePublishCallbacks_.count(pkgName) == 0) {
152         devicePublishCallbacks_[pkgName] = std::map<int32_t, std::shared_ptr<PublishCallback>>();
153     }
154     devicePublishCallbacks_[pkgName][publishId] = callback;
155 }
156 
UnRegisterPublishCallback(const std::string & pkgName,int32_t publishId)157 void DeviceManagerNotify::UnRegisterPublishCallback(const std::string &pkgName, int32_t publishId)
158 {
159     if (pkgName.empty()) {
160         LOGE("Invalid parameter, pkgName is empty.");
161         return;
162     }
163     std::lock_guard<std::mutex> autoLock(lock_);
164     if (devicePublishCallbacks_.count(pkgName) > 0) {
165         devicePublishCallbacks_[pkgName].erase(publishId);
166         if (devicePublishCallbacks_[pkgName].empty()) {
167             devicePublishCallbacks_.erase(pkgName);
168         }
169     }
170 }
171 
RegisterAuthenticateCallback(const std::string & pkgName,const std::string & deviceId,std::shared_ptr<AuthenticateCallback> callback)172 void DeviceManagerNotify::RegisterAuthenticateCallback(const std::string &pkgName, const std::string &deviceId,
173                                                        std::shared_ptr<AuthenticateCallback> callback)
174 {
175     if (pkgName.empty() || deviceId.empty() || callback == nullptr) {
176         LOGE("DeviceManagerNotify::RegisterAuthenticateCallback error: Invalid parameter, pkgName: %{public}s,"
177             "deviceId: %{public}s", pkgName.c_str(), GetAnonyString(deviceId).c_str());
178         return;
179     }
180     std::lock_guard<std::mutex> autoLock(lock_);
181     if (authenticateCallback_.count(pkgName) == 0) {
182         authenticateCallback_[pkgName] = std::map<std::string, std::shared_ptr<AuthenticateCallback>>();
183     }
184     authenticateCallback_[pkgName][deviceId] = callback;
185 }
186 
UnRegisterAuthenticateCallback(const std::string & pkgName,const std::string & deviceId)187 void DeviceManagerNotify::UnRegisterAuthenticateCallback(const std::string &pkgName, const std::string &deviceId)
188 {
189     if (pkgName.empty() || deviceId.empty()) {
190         LOGE("DeviceManagerNotify::UnRegisterAuthenticateCallback error: Invalid parameter, pkgName: %{public}s,"
191             "deviceId: %{public}s", pkgName.c_str(), GetAnonyString(deviceId).c_str());
192         return;
193     }
194     std::lock_guard<std::mutex> autoLock(lock_);
195     if (authenticateCallback_.count(pkgName) > 0) {
196         authenticateCallback_[pkgName].erase(deviceId);
197         if (authenticateCallback_[pkgName].empty()) {
198             authenticateCallback_.erase(pkgName);
199         }
200     }
201 }
202 
UnRegisterPackageCallback(const std::string & pkgName)203 void DeviceManagerNotify::UnRegisterPackageCallback(const std::string &pkgName)
204 {
205     if (pkgName.empty()) {
206         LOGE("Invalid parameter, pkgName is empty.");
207         return;
208     }
209     std::lock_guard<std::mutex> autoLock(lock_);
210     deviceStateCallback_.erase(pkgName);
211     deviceStatusCallback_.erase(pkgName);
212     deviceDiscoveryCallbacks_.erase(pkgName);
213     devicePublishCallbacks_.erase(pkgName);
214     authenticateCallback_.erase(pkgName);
215     dmInitCallback_.erase(pkgName);
216 }
217 
RegisterDeviceManagerFaCallback(const std::string & pkgName,std::shared_ptr<DeviceManagerUiCallback> callback)218 void DeviceManagerNotify::RegisterDeviceManagerFaCallback(const std::string &pkgName,
219                                                           std::shared_ptr<DeviceManagerUiCallback> callback)
220 {
221     std::lock_guard<std::mutex> autoLock(lock_);
222     dmUiCallback_[pkgName] = callback;
223 }
224 
UnRegisterDeviceManagerFaCallback(const std::string & pkgName)225 void DeviceManagerNotify::UnRegisterDeviceManagerFaCallback(const std::string &pkgName)
226 {
227     if (pkgName.empty()) {
228         LOGE("Invalid parameter, pkgName is empty.");
229         return;
230     }
231     std::lock_guard<std::mutex> autoLock(lock_);
232     dmUiCallback_.erase(pkgName);
233 }
234 
RegisterCredentialCallback(const std::string & pkgName,std::shared_ptr<CredentialCallback> callback)235 void DeviceManagerNotify::RegisterCredentialCallback(const std::string &pkgName,
236                                                      std::shared_ptr<CredentialCallback> callback)
237 {
238     if (pkgName.empty() || callback == nullptr) {
239         LOGE("Invalid parameter, pkgName is empty or callback is nullptr.");
240         return;
241     }
242     std::lock_guard<std::mutex> autoLock(lock_);
243     credentialCallback_[pkgName] = callback;
244 }
245 
UnRegisterCredentialCallback(const std::string & pkgName)246 void DeviceManagerNotify::UnRegisterCredentialCallback(const std::string &pkgName)
247 {
248     if (pkgName.empty()) {
249         LOGE("Invalid parameter, pkgName is empty.");
250         return;
251     }
252     std::lock_guard<std::mutex> autoLock(lock_);
253     credentialCallback_.erase(pkgName);
254 }
255 
RegisterPinHolderCallback(const std::string & pkgName,std::shared_ptr<PinHolderCallback> callback)256 void DeviceManagerNotify::RegisterPinHolderCallback(const std::string &pkgName,
257     std::shared_ptr<PinHolderCallback> callback)
258 {
259     if (pkgName.empty() || callback == nullptr) {
260         LOGE("Invalid parameter, pkgName is empty or callback is nullptr.");
261         return;
262     }
263     std::lock_guard<std::mutex> autoLock(lock_);
264     pinHolderCallback_[pkgName] = callback;
265 }
266 
OnRemoteDied()267 void DeviceManagerNotify::OnRemoteDied()
268 {
269     LOGW("DeviceManagerNotify::OnRemoteDied");
270     std::lock_guard<std::mutex> autoLock(lock_);
271     for (auto iter : dmInitCallback_) {
272         LOGI("DeviceManagerNotify::OnRemoteDied, pkgName:%{public}s", iter.first.c_str());
273         if (iter.second != nullptr) {
274             iter.second->OnRemoteDied();
275         }
276     }
277 }
278 
OnDeviceOnline(const std::string & pkgName,const DmDeviceInfo & deviceInfo)279 void DeviceManagerNotify::OnDeviceOnline(const std::string &pkgName, const DmDeviceInfo &deviceInfo)
280 {
281     if (pkgName.empty()) {
282         LOGE("Invalid parameter, pkgName is empty.");
283         return;
284     }
285     LOGI("Online with DmDeviceInfo, pkgName:%{public}s", pkgName.c_str());
286     std::shared_ptr<DeviceStateCallback> tempCbk;
287     {
288         std::lock_guard<std::mutex> autoLock(lock_);
289         auto iter = deviceStateCallback_.find(pkgName);
290         if (iter == deviceStateCallback_.end()) {
291             LOGE("OnDeviceOnline error, device state callback not register.");
292             return;
293         }
294         tempCbk = iter->second;
295     }
296     if (tempCbk == nullptr) {
297         LOGE("OnDeviceOnline error, registered device state callback is nullptr.");
298         return;
299     }
300 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
301     if (ffrtQueue_ != nullptr) {
302         ffrtQueue_->submit([=]() { DeviceInfoOnline(deviceInfo, tempCbk); });
303     }
304 #else
305     std::thread deviceOnline([=]() { DeviceInfoOnline(deviceInfo, tempCbk); });
306     int32_t ret = pthread_setname_np(deviceOnline.native_handle(), DEVICE_ONLINE);
307     if (ret != DM_OK) {
308         LOGE("DeviceManagerNotify deviceOnline setname failed.");
309     }
310     deviceOnline.detach();
311 #endif
312 }
313 
OnDeviceOnline(const std::string & pkgName,const DmDeviceBasicInfo & deviceBasicInfo)314 void DeviceManagerNotify::OnDeviceOnline(const std::string &pkgName, const DmDeviceBasicInfo &deviceBasicInfo)
315 {
316     if (pkgName.empty()) {
317         LOGE("Invalid parameter, pkgName is empty.");
318         return;
319     }
320     LOGI("Online with DmDeviceBasicInfo, pkgName:%{public}s", pkgName.c_str());
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             LOGE("Error, device status callback not register.");
327             return;
328         }
329         tempCbk = iter->second;
330     }
331     if (tempCbk == nullptr) {
332         LOGE("Error, registered device status callback is nullptr.");
333         return;
334     }
335 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
336     if (ffrtQueue_ != nullptr) {
337         ffrtQueue_->submit([=]() { DeviceBasicInfoOnline(deviceBasicInfo, tempCbk); });
338     }
339 #else
340     std::thread deviceOnline([=]() { DeviceBasicInfoOnline(deviceBasicInfo, tempCbk); });
341     int32_t ret = pthread_setname_np(deviceOnline.native_handle(), DEVICE_ONLINE);
342     if (ret != DM_OK) {
343         LOGE("DeviceManagerNotify deviceOnline setname failed.");
344     }
345     deviceOnline.detach();
346 #endif
347 }
348 
OnDeviceOffline(const std::string & pkgName,const DmDeviceInfo & deviceInfo)349 void DeviceManagerNotify::OnDeviceOffline(const std::string &pkgName, const DmDeviceInfo &deviceInfo)
350 {
351     if (pkgName.empty()) {
352         LOGE("Invalid parameter, pkgName is empty.");
353         return;
354     }
355     LOGI("Offline with DmDeviceInfo, pkgName:%{public}s", pkgName.c_str());
356     std::shared_ptr<DeviceStateCallback> tempCbk;
357     {
358         std::lock_guard<std::mutex> autoLock(lock_);
359         auto iter = deviceStateCallback_.find(pkgName);
360         if (iter == deviceStateCallback_.end()) {
361             LOGE("Error, device state callback not register.");
362             return;
363         }
364         tempCbk = iter->second;
365     }
366     if (tempCbk == nullptr) {
367         LOGE("Error, registered device state callback is nullptr.");
368         return;
369     }
370 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
371     if (ffrtQueue_ != nullptr) {
372         ffrtQueue_->submit([=]() { DeviceInfoOffline(deviceInfo, tempCbk); });
373     }
374 #else
375     std::thread deviceOffline([=]() { DeviceInfoOffline(deviceInfo, tempCbk); });
376     int32_t ret = pthread_setname_np(deviceOffline.native_handle(), DEVICE_OFFLINE);
377     if (ret != DM_OK) {
378         LOGE("DeviceManagerNotify deviceOffline setname failed.");
379     }
380     deviceOffline.detach();
381 #endif
382 }
383 
OnDeviceOffline(const std::string & pkgName,const DmDeviceBasicInfo & deviceBasicInfo)384 void DeviceManagerNotify::OnDeviceOffline(const std::string &pkgName, const DmDeviceBasicInfo &deviceBasicInfo)
385 {
386     if (pkgName.empty()) {
387         LOGE("Invalid parameter, pkgName is empty.");
388         return;
389     }
390     LOGI("Offline with DmDeviceBasicInfo, pkgName:%{public}s", pkgName.c_str());
391     std::shared_ptr<DeviceStatusCallback> tempCbk;
392     {
393         std::lock_guard<std::mutex> autoLock(lock_);
394         auto iter = deviceStatusCallback_.find(pkgName);
395         if (iter == deviceStatusCallback_.end()) {
396             LOGE("Error, device status callback not register.");
397             return;
398         }
399         tempCbk = iter->second;
400     }
401     if (tempCbk == nullptr) {
402         LOGE("Error, registered device status callback is nullptr.");
403         return;
404     }
405 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
406     if (ffrtQueue_ != nullptr) {
407         ffrtQueue_->submit([=]() { DeviceBasicInfoOffline(deviceBasicInfo, tempCbk); });
408     }
409 #else
410     std::thread deviceOffline([=]() { DeviceBasicInfoOffline(deviceBasicInfo, tempCbk); });
411     int32_t ret = pthread_setname_np(deviceOffline.native_handle(), DEVICE_OFFLINE);
412     if (ret != DM_OK) {
413         LOGE("DeviceManagerNotify deviceOffline setname failed.");
414     }
415     deviceOffline.detach();
416 #endif
417 }
418 
OnDeviceChanged(const std::string & pkgName,const DmDeviceInfo & deviceInfo)419 void DeviceManagerNotify::OnDeviceChanged(const std::string &pkgName, const DmDeviceInfo &deviceInfo)
420 {
421     if (pkgName.empty()) {
422         LOGE("Invalid parameter, pkgName is empty.");
423         return;
424     }
425 
426     std::shared_ptr<DeviceStateCallback> tempCbk;
427     {
428         std::lock_guard<std::mutex> autoLock(lock_);
429         auto iter = deviceStateCallback_.find(pkgName);
430         if (iter == deviceStateCallback_.end()) {
431             LOGE("OnDeviceChanged error, device state callback not register, pkgName:%{public}s", pkgName.c_str());
432             return;
433         }
434         tempCbk = iter->second;
435     }
436     if (tempCbk == nullptr) {
437         LOGE("OnDeviceChanged error, registered device state callback is nullptr, pkgName:%{public}s", pkgName.c_str());
438         return;
439     }
440 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
441     if (ffrtQueue_ != nullptr) {
442         ffrtQueue_->submit([=]() { DeviceInfoChanged(deviceInfo, tempCbk); });
443     }
444 #else
445     std::thread deviceChanged([=]() { DeviceInfoChanged(deviceInfo, tempCbk); });
446     int32_t ret = pthread_setname_np(deviceChanged.native_handle(), DEVICEINFO_CHANGE);
447     if (ret != DM_OK) {
448         LOGE("DeviceManagerNotify deviceChanged setname failed.");
449     }
450     deviceChanged.detach();
451 #endif
452 }
453 
OnDeviceChanged(const std::string & pkgName,const DmDeviceBasicInfo & deviceBasicInfo)454 void DeviceManagerNotify::OnDeviceChanged(const std::string &pkgName, const DmDeviceBasicInfo &deviceBasicInfo)
455 {
456     if (pkgName.empty()) {
457         LOGE("Invalid parameter, pkgName is empty.");
458         return;
459     }
460 
461     std::shared_ptr<DeviceStatusCallback> tempCbk;
462     {
463         std::lock_guard<std::mutex> autoLock(lock_);
464         auto iter = deviceStatusCallback_.find(pkgName);
465         if (iter == deviceStatusCallback_.end()) {
466             LOGE("OnDeviceChanged error, device state callback not register, pkgName:%{public}s", pkgName.c_str());
467             return;
468         }
469         tempCbk = iter->second;
470     }
471     if (tempCbk == nullptr) {
472         LOGE("OnDeviceChanged error, registered device state callback is nullptr, pkgName:%{public}s", pkgName.c_str());
473         return;
474     }
475 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
476     if (ffrtQueue_ != nullptr) {
477         ffrtQueue_->submit([=]() { DeviceBasicInfoChanged(deviceBasicInfo, tempCbk); });
478     }
479 #else
480     std::thread deviceChanged([=]() { DeviceBasicInfoChanged(deviceBasicInfo, tempCbk); });
481     int32_t ret = pthread_setname_np(deviceChanged.native_handle(), DEVICEINFO_CHANGE);
482     if (ret != DM_OK) {
483         LOGE("DeviceManagerNotify deviceChanged setname failed.");
484     }
485     deviceChanged.detach();
486 #endif
487 }
488 
OnDeviceReady(const std::string & pkgName,const DmDeviceInfo & deviceInfo)489 void DeviceManagerNotify::OnDeviceReady(const std::string &pkgName, const DmDeviceInfo &deviceInfo)
490 {
491     if (pkgName.empty()) {
492         LOGE("Invalid parameter, pkgName is empty.");
493         return;
494     }
495 
496     std::shared_ptr<DeviceStateCallback> tempCbk;
497     {
498         std::lock_guard<std::mutex> autoLock(lock_);
499         auto iter = deviceStateCallback_.find(pkgName);
500         if (iter == deviceStateCallback_.end()) {
501             LOGE("OnDeviceReady error, device state callback not register, pkgName:%{public}s", pkgName.c_str());
502             return;
503         }
504         tempCbk = iter->second;
505     }
506     if (tempCbk == nullptr) {
507         LOGE("OnDeviceReady error, registered device state callback is nullptr, pkgName:%{public}s", pkgName.c_str());
508         return;
509     }
510 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
511     if (ffrtQueue_ != nullptr) {
512         ffrtQueue_->submit([=]() { DeviceInfoReady(deviceInfo, tempCbk); });
513     }
514 #else
515     std::thread deviceReady([=]() { DeviceInfoReady(deviceInfo, tempCbk); });
516     int32_t ret = pthread_setname_np(deviceReady.native_handle(), DEVICE_READY);
517     if (ret != DM_OK) {
518         LOGE("DeviceManagerNotify deviceReady setname failed.");
519     }
520     deviceReady.detach();
521 #endif
522 }
523 
OnDeviceReady(const std::string & pkgName,const DmDeviceBasicInfo & deviceBasicInfo)524 void DeviceManagerNotify::OnDeviceReady(const std::string &pkgName, const DmDeviceBasicInfo &deviceBasicInfo)
525 {
526     if (pkgName.empty()) {
527         LOGE("Invalid parameter, pkgName is empty.");
528         return;
529     }
530     LOGI("DeviceManagerNotify::OnDeviceReady with DmDeviceBasicInfo, pkgName:%{public}s", pkgName.c_str());
531     std::shared_ptr<DeviceStatusCallback> tempCbk;
532     {
533         std::lock_guard<std::mutex> autoLock(lock_);
534         auto iter = deviceStatusCallback_.find(pkgName);
535         if (iter == deviceStatusCallback_.end()) {
536             LOGE("OnDeviceReady error, device status callback not register, pkgName:%{public}s", pkgName.c_str());
537             return;
538         }
539         tempCbk = iter->second;
540     }
541     if (tempCbk == nullptr) {
542         LOGE("OnDeviceReady error, registered device status callback is nullptr.");
543         return;
544     }
545 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
546     if (ffrtQueue_ != nullptr) {
547         ffrtQueue_->submit([=]() { DeviceBasicInfoReady(deviceBasicInfo, tempCbk); });
548     }
549 #else
550     std::thread deviceReady([=]() { DeviceBasicInfoReady(deviceBasicInfo, tempCbk); });
551     int32_t ret = pthread_setname_np(deviceReady.native_handle(), DEVICE_READY);
552     if (ret != DM_OK) {
553         LOGE("DeviceManagerNotify deviceReady setname failed.");
554     }
555     deviceReady.detach();
556 #endif
557 }
558 
OnDeviceFound(const std::string & pkgName,uint16_t subscribeId,const DmDeviceInfo & deviceInfo)559 void DeviceManagerNotify::OnDeviceFound(const std::string &pkgName, uint16_t subscribeId,
560                                         const DmDeviceInfo &deviceInfo)
561 {
562     if (pkgName.empty()) {
563         LOGE("Invalid parameter, pkgName is empty.");
564         return;
565     }
566 
567     std::shared_ptr<DiscoveryCallback> tempCbk;
568     {
569         std::lock_guard<std::mutex> autoLock(lock_);
570         if (deviceDiscoveryCallbacks_.count(pkgName) == 0) {
571             LOGE("DeviceManagerNotify::OnDeviceFound error, device discovery callback not register for"
572                 "pkgName %{public}s.", pkgName.c_str());
573             return;
574         }
575         std::map<uint16_t, std::shared_ptr<DiscoveryCallback>> &discoverCallMap = deviceDiscoveryCallbacks_[pkgName];
576         auto iter = discoverCallMap.find(subscribeId);
577         if (iter == discoverCallMap.end()) {
578             LOGE("OnDeviceFound error, no register deviceDiscoveryCallback for subscribeId %{public}d.",
579                 (int32_t)subscribeId);
580             return;
581         }
582         tempCbk = iter->second;
583     }
584     if (tempCbk == nullptr) {
585         LOGE("OnDeviceFound error, registered device discovery callback is nullptr.");
586         return;
587     }
588     LOGI("Complete with devInfo, pkgName:%{public}s, subscribeId:%{public}d.",
589          GetAnonyString(pkgName).c_str(), (int32_t)subscribeId);
590     tempCbk->OnDeviceFound(subscribeId, deviceInfo);
591 }
592 
OnDeviceFound(const std::string & pkgName,uint16_t subscribeId,const DmDeviceBasicInfo & deviceBasicInfo)593 void DeviceManagerNotify::OnDeviceFound(const std::string &pkgName, uint16_t subscribeId,
594                                         const DmDeviceBasicInfo &deviceBasicInfo)
595 {
596     if (pkgName.empty()) {
597         LOGE("Invalid parameter, pkgName is empty.");
598         return;
599     }
600 
601     std::shared_ptr<DiscoveryCallback> tempCbk;
602     {
603         std::lock_guard<std::mutex> autoLock(lock_);
604         if (deviceDiscoveryCallbacks_.count(pkgName) == 0) {
605             LOGE("DeviceManagerNotify::OnDeviceFound error, device discovery callback not register for"
606                 "pkgName %{public}s.", pkgName.c_str());
607             return;
608         }
609         std::map<uint16_t, std::shared_ptr<DiscoveryCallback>> &discoverCallMap = deviceDiscoveryCallbacks_[pkgName];
610         auto iter = discoverCallMap.find(subscribeId);
611         if (iter == discoverCallMap.end()) {
612             LOGE("OnDeviceFound error, no register deviceDiscoveryCallback for subscribeId %{public}d.",
613                 (int32_t)subscribeId);
614             return;
615         }
616         tempCbk = iter->second;
617     }
618     if (tempCbk == nullptr) {
619         LOGE("OnDeviceFound error, registered device discovery callback is nullptr.");
620         return;
621     }
622     LOGD("Complete with DmDeviceBasicInfo, pkgName:%{public}s, subscribeId:%{public}d.",
623          GetAnonyString(pkgName).c_str(), (int32_t)subscribeId);
624     tempCbk->OnDeviceFound(subscribeId, deviceBasicInfo);
625 }
626 
OnDiscoveryFailed(const std::string & pkgName,uint16_t subscribeId,int32_t failedReason)627 void DeviceManagerNotify::OnDiscoveryFailed(const std::string &pkgName, uint16_t subscribeId, int32_t failedReason)
628 {
629     if (pkgName.empty()) {
630         LOGE("Invalid parameter, pkgName is empty.");
631         return;
632     }
633     LOGI("DeviceManagerNotify::OnDiscoveryFailed in, pkgName:%{public}s, subscribeId %{public}d, failed"
634         "reason %{public}d", pkgName.c_str(), (int32_t)subscribeId, failedReason);
635     std::shared_ptr<DiscoveryCallback> tempCbk;
636     {
637         std::lock_guard<std::mutex> autoLock(lock_);
638         if (deviceDiscoveryCallbacks_.count(pkgName) == 0) {
639             LOGE("DeviceManagerNotify::OnDiscoveryFailed error, device discovery callback not register for"
640                 "pkgName %{public}s.", pkgName.c_str());
641             return;
642         }
643         std::map<uint16_t, std::shared_ptr<DiscoveryCallback>> &discoverCallMap = deviceDiscoveryCallbacks_[pkgName];
644         auto iter = discoverCallMap.find(subscribeId);
645         if (iter == discoverCallMap.end()) {
646             LOGE("OnDiscoveryFailed error, device discovery callback not register for subscribeId %{public}d.",
647                 subscribeId);
648             return;
649         }
650         tempCbk = iter->second;
651     }
652     if (tempCbk == nullptr) {
653         LOGE("OnDiscoveryFailed error, registered device discovery callback is nullptr.");
654         return;
655     }
656     tempCbk->OnDiscoveryFailed(subscribeId, failedReason);
657 }
658 
OnDiscoverySuccess(const std::string & pkgName,uint16_t subscribeId)659 void DeviceManagerNotify::OnDiscoverySuccess(const std::string &pkgName, uint16_t subscribeId)
660 {
661     if (pkgName.empty()) {
662         LOGE("Invalid parameter, pkgName is empty.");
663         return;
664     }
665     LOGI("PkgName:%{public}s, subscribeId:%{public}d.", GetAnonyString(pkgName).c_str(), subscribeId);
666     std::shared_ptr<DiscoveryCallback> tempCbk;
667     {
668         std::lock_guard<std::mutex> autoLock(lock_);
669         if (deviceDiscoveryCallbacks_.count(pkgName) == 0) {
670             LOGE("OnDiscoverySuccess error, device discovery callback not register for pkgName %{public}s.",
671                 pkgName.c_str());
672             return;
673         }
674         std::map<uint16_t, std::shared_ptr<DiscoveryCallback>> &discoverCallMap = deviceDiscoveryCallbacks_[pkgName];
675         auto iter = discoverCallMap.find(subscribeId);
676         if (iter == discoverCallMap.end()) {
677             LOGE("OnDiscoverySuccess error, device discovery callback not register for subscribeId %{public}d.",
678                 (int32_t)subscribeId);
679             return;
680         }
681         tempCbk = iter->second;
682     }
683     if (tempCbk == nullptr) {
684         LOGE("OnDiscoverySuccess error, registered device discovery callback is nullptr.");
685         return;
686     }
687     tempCbk->OnDiscoverySuccess(subscribeId);
688 }
689 
OnPublishResult(const std::string & pkgName,int32_t publishId,int32_t publishResult)690 void DeviceManagerNotify::OnPublishResult(const std::string &pkgName, int32_t publishId, int32_t publishResult)
691 {
692     if (pkgName.empty()) {
693         LOGE("Invalid parameter, pkgName is empty.");
694         return;
695     }
696     LOGI("DeviceManagerNotify::OnPublishResult in, pkgName:%{public}s, publishId %{public}d, publishResult %{public}d",
697         pkgName.c_str(), publishId, publishResult);
698     std::shared_ptr<PublishCallback> tempCbk;
699     {
700         std::lock_guard<std::mutex> autoLock(lock_);
701         if (devicePublishCallbacks_.count(pkgName) == 0) {
702             LOGE("DeviceManagerNotify::OnPublishResult error, device publish callback not register for"
703                 "pkgName %{public}s.", pkgName.c_str());
704             return;
705         }
706         std::map<int32_t, std::shared_ptr<PublishCallback>> &publishCallMap = devicePublishCallbacks_[pkgName];
707         auto iter = publishCallMap.find(publishId);
708         if (iter == publishCallMap.end()) {
709             LOGE("OnPublishResult error, device publish callback not register for publishId %{public}d.", publishId);
710             return;
711         }
712         tempCbk = iter->second;
713     }
714     if (tempCbk == nullptr) {
715         LOGE("OnPublishResult error, registered device publish callback is nullptr.");
716         return;
717     }
718     tempCbk->OnPublishResult(publishId, publishResult);
719 }
720 
OnAuthResult(const std::string & pkgName,const std::string & deviceId,const std::string & token,int32_t status,int32_t reason)721 void DeviceManagerNotify::OnAuthResult(const std::string &pkgName, const std::string &deviceId,
722                                        const std::string &token, int32_t status, int32_t reason)
723 {
724     if (pkgName.empty() || token.empty() || deviceId.empty()) {
725         LOGE("Invalid para, pkgName: %{public}s, token: %{public}s", pkgName.c_str(), GetAnonyString(token).c_str());
726         return;
727     }
728     LOGI("DeviceManagerNotify::OnAuthResult in, pkgName:%{public}s, status:%{public}d, reason:%{public}d",
729         pkgName.c_str(), status, reason);
730     std::shared_ptr<AuthenticateCallback> tempCbk;
731     {
732         std::lock_guard<std::mutex> autoLock(lock_);
733         if (authenticateCallback_.count(pkgName) == 0) {
734             LOGE("DeviceManagerNotify::OnAuthResult error, authenticate callback not register for pkgName %{public}s.",
735                 pkgName.c_str());
736             return;
737         }
738         std::map<std::string, std::shared_ptr<AuthenticateCallback>> &authCallMap = authenticateCallback_[pkgName];
739         auto iter = authCallMap.find(deviceId);
740         if (iter == authCallMap.end()) {
741             LOGE("OnAuthResult error, authenticate callback not register.");
742             return;
743         }
744         tempCbk = iter->second;
745     }
746     if (tempCbk == nullptr) {
747         LOGE("OnAuthResult error, registered authenticate callback is nullptr.");
748         return;
749     }
750     tempCbk->OnAuthResult(deviceId, token, status, reason);
751     if (reason == DM_OK && (status <= STATUS_DM_CLOSE_PIN_INPUT_UI && status >= STATUS_DM_SHOW_AUTHORIZE_UI)) {
752         LOGI("update ui change, status: %{public}d, reason: %{public}d", status, reason);
753         return;
754     }
755     {
756         std::lock_guard<std::mutex> autoLock(lock_);
757         authenticateCallback_[pkgName].erase(deviceId);
758         if (authenticateCallback_[pkgName].empty()) {
759             authenticateCallback_.erase(pkgName);
760         }
761     }
762 }
763 
OnUiCall(std::string & pkgName,std::string & paramJson)764 void DeviceManagerNotify::OnUiCall(std::string &pkgName, std::string &paramJson)
765 {
766     if (pkgName.empty()) {
767         LOGE("DeviceManagerNotify::OnUiCall error: Invalid parameter, pkgName: %{public}s", pkgName.c_str());
768         return;
769     }
770     LOGI("DeviceManagerNotify::OnUiCall in, pkgName:%{public}s", pkgName.c_str());
771     std::shared_ptr<DeviceManagerUiCallback> tempCbk;
772     {
773         std::lock_guard<std::mutex> autoLock(lock_);
774         if (dmUiCallback_.count(pkgName) == 0) {
775             LOGE("OnUiCall error, dm Ui callback not register for pkgName %{public}s.", pkgName.c_str());
776             return;
777         }
778         tempCbk = dmUiCallback_[pkgName];
779     }
780     if (tempCbk == nullptr) {
781         LOGE("OnUiCall error, registered dm Ui callback is nullptr.");
782         return;
783     }
784     tempCbk->OnCall(paramJson);
785 }
786 
OnCredentialResult(const std::string & pkgName,int32_t & action,const std::string & credentialResult)787 void DeviceManagerNotify::OnCredentialResult(const std::string &pkgName, int32_t &action,
788                                              const std::string &credentialResult)
789 {
790     if (pkgName.empty()) {
791         LOGE("DeviceManagerNotify::OnCredentialResult error: Invalid parameter, pkgName: %{public}s", pkgName.c_str());
792         return;
793     }
794     LOGI("DeviceManagerNotify::OnCredentialResult in, pkgName:%{public}s, action:%{public}d", pkgName.c_str(), action);
795     std::shared_ptr<CredentialCallback> tempCbk;
796     {
797         std::lock_guard<std::mutex> autoLock(lock_);
798         if (credentialCallback_.count(pkgName) == 0) {
799             LOGE("DeviceManagerNotify::OnCredentialResult error, credential callback not register for"
800                 "pkgName %{public}s.", pkgName.c_str());
801             return;
802         }
803         tempCbk = credentialCallback_[pkgName];
804     }
805     if (tempCbk == nullptr) {
806         LOGE("OnCredentialResult error, registered credential callback is nullptr.");
807         return;
808     }
809     tempCbk->OnCredentialResult(action, credentialResult);
810 }
811 
RegisterBindCallback(const std::string & pkgName,const PeerTargetId & targetId,std::shared_ptr<BindTargetCallback> callback)812 void DeviceManagerNotify::RegisterBindCallback(const std::string &pkgName, const PeerTargetId &targetId,
813     std::shared_ptr<BindTargetCallback> callback)
814 {
815     if (pkgName.empty() || IsInvalidPeerTargetId(targetId) || (callback == nullptr)) {
816         LOGE("DeviceManagerNotify::RegisterBindCallback error: Invalid parameter, pkgName: %{public}s.",
817             pkgName.c_str());
818         return;
819     }
820     std::lock_guard<std::mutex> autoLock(bindLock_);
821     if (bindCallback_.count(pkgName) == 0) {
822         bindCallback_[pkgName] = std::map<PeerTargetId, std::shared_ptr<BindTargetCallback>>();
823     }
824     bindCallback_[pkgName][targetId] = callback;
825 }
826 
RegisterUnbindCallback(const std::string & pkgName,const PeerTargetId & targetId,std::shared_ptr<UnbindTargetCallback> callback)827 void DeviceManagerNotify::RegisterUnbindCallback(const std::string &pkgName, const PeerTargetId &targetId,
828     std::shared_ptr<UnbindTargetCallback> callback)
829 {
830     if (pkgName.empty() || IsInvalidPeerTargetId(targetId) || (callback == nullptr)) {
831         LOGE("DeviceManagerNotify::RegisterUnbindCallback error: Invalid parameter, pkgName: %{public}s.",
832             pkgName.c_str());
833         return;
834     }
835     std::lock_guard<std::mutex> autoLock(lock_);
836     if (unbindCallback_.count(pkgName) == 0) {
837         unbindCallback_[pkgName] = std::map<PeerTargetId, std::shared_ptr<UnbindTargetCallback>>();
838     }
839     unbindCallback_[pkgName][targetId] = callback;
840 }
841 
OnBindResult(const std::string & pkgName,const PeerTargetId & targetId,int32_t result,int32_t status,std::string content)842 void DeviceManagerNotify::OnBindResult(const std::string &pkgName, const PeerTargetId &targetId,
843     int32_t result, int32_t status, std::string content)
844 {
845     if (pkgName.empty() || IsInvalidPeerTargetId(targetId)) {
846         LOGE("Invalid para, pkgName: %{public}s.", pkgName.c_str());
847         return;
848     }
849     LOGI("DeviceManagerNotify::OnBindResult in, pkgName:%{public}s, result:%{public}d", pkgName.c_str(), result);
850     std::shared_ptr<BindTargetCallback> tempCbk;
851     {
852         std::lock_guard<std::mutex> autoLock(bindLock_);
853         if (bindCallback_.count(pkgName) == 0) {
854             LOGE("DeviceManagerNotify::OnBindResult error, callback not register for pkgName %{public}s.",
855                 pkgName.c_str());
856             return;
857         }
858         std::map<PeerTargetId, std::shared_ptr<BindTargetCallback>> &bindCbkMap = bindCallback_[pkgName];
859         auto iter = bindCbkMap.find(targetId);
860         if (iter == bindCbkMap.end()) {
861             LOGE("OnBindResult error, bind callback not register for targetId.");
862             return;
863         }
864         tempCbk = iter->second;
865         if (result != DM_OK || status == STATUS_DM_AUTH_FINISH || status == STATUS_DM_AUTH_DEFAULT) {
866             LOGI("notify end, result: %{public}d, status: %{public}d", result, status);
867             bindCallback_[pkgName].erase(targetId);
868             if (bindCallback_[pkgName].empty()) {
869                 bindCallback_.erase(pkgName);
870             }
871         }
872     }
873     if (tempCbk == nullptr) {
874         LOGE("OnBindResult error, registered bind callback is nullptr.");
875         return;
876     }
877     tempCbk->OnBindResult(targetId, result, status, content);
878 }
879 
OnUnbindResult(const std::string & pkgName,const PeerTargetId & targetId,int32_t result,std::string content)880 void DeviceManagerNotify::OnUnbindResult(const std::string &pkgName, const PeerTargetId &targetId,
881     int32_t result, std::string content)
882 {
883     if (pkgName.empty() || IsInvalidPeerTargetId(targetId)) {
884         LOGE("Invalid para, pkgName: %{public}s.", pkgName.c_str());
885         return;
886     }
887     LOGI("DeviceManagerNotify::OnUnbindResult in, pkgName:%{public}s, result:%{public}d", pkgName.c_str(), result);
888     std::shared_ptr<UnbindTargetCallback> tempCbk;
889     {
890         std::lock_guard<std::mutex> autoLock(lock_);
891         if (unbindCallback_.count(pkgName) == 0) {
892             LOGE("DeviceManagerNotify::OnUnbindResult error, callback not register for pkgName %{public}s.",
893                 pkgName.c_str());
894             return;
895         }
896         std::map<PeerTargetId, std::shared_ptr<UnbindTargetCallback>> &unbindCbkMap = unbindCallback_[pkgName];
897         auto iter = unbindCbkMap.find(targetId);
898         if (iter == unbindCbkMap.end()) {
899             LOGE("OnUnbindResult error, unbind callback not register for targetId.");
900             return;
901         }
902         tempCbk = iter->second;
903     }
904     if (tempCbk == nullptr) {
905         LOGE("OnUnbindResult error, registered unbind callback is nullptr.");
906         return;
907     }
908     tempCbk->OnUnbindResult(targetId, result, content);
909     {
910         std::lock_guard<std::mutex> autoLock(lock_);
911         unbindCallback_[pkgName].erase(targetId);
912         if (unbindCallback_[pkgName].empty()) {
913             unbindCallback_.erase(pkgName);
914         }
915     }
916 }
917 
OnPinHolderCreate(const std::string & pkgName,const std::string & deviceId,DmPinType pinType,const std::string & payload)918 void DeviceManagerNotify::OnPinHolderCreate(const std::string &pkgName, const std::string &deviceId,
919     DmPinType pinType, const std::string &payload)
920 {
921     if (pkgName.empty()) {
922         LOGE("Invalid parameter, pkgName is empty.");
923         return;
924     }
925     LOGI("DeviceManagerNotify::OnPinHolderCreate in, pkgName:%{public}s", pkgName.c_str());
926     std::shared_ptr<PinHolderCallback> tempCbk;
927     {
928         std::lock_guard<std::mutex> autoLock(lock_);
929         if (pinHolderCallback_.count(pkgName) == 0) {
930             LOGE("OnPinHolderCreate error, device state callback not register.");
931             return;
932         }
933         tempCbk = pinHolderCallback_[pkgName];
934     }
935     if (tempCbk == nullptr) {
936         LOGE("OnPinHolderCreate error, registered device state callback is nullptr.");
937         return;
938     }
939     tempCbk->OnPinHolderCreate(deviceId, pinType, payload);
940 }
941 
OnPinHolderDestroy(const std::string & pkgName,DmPinType pinType,const std::string & payload)942 void DeviceManagerNotify::OnPinHolderDestroy(const std::string &pkgName, DmPinType pinType,
943     const std::string &payload)
944 {
945     if (pkgName.empty()) {
946         LOGE("Invalid parameter, pkgName is empty.");
947         return;
948     }
949     LOGI("DeviceManagerNotify::OnPinHolderDestroy in, pkgName:%{public}s", pkgName.c_str());
950     std::shared_ptr<PinHolderCallback> tempCbk;
951     {
952         std::lock_guard<std::mutex> autoLock(lock_);
953         if (pinHolderCallback_.count(pkgName) == 0) {
954             LOGE("OnPinHolderDestroy error, device state callback not register.");
955             return;
956         }
957         tempCbk = pinHolderCallback_[pkgName];
958     }
959     if (tempCbk == nullptr) {
960         LOGE("OnPinHolderDestroy error, registered device state callback is nullptr.");
961         return;
962     }
963     tempCbk->OnPinHolderDestroy(pinType, payload);
964 }
965 
OnCreateResult(const std::string & pkgName,int32_t result)966 void DeviceManagerNotify::OnCreateResult(const std::string &pkgName, int32_t result)
967 {
968     if (pkgName.empty()) {
969         LOGE("Invalid parameter, pkgName is empty.");
970         return;
971     }
972     LOGI("DeviceManagerNotify::OnCreateResult in, pkgName:%{public}s", pkgName.c_str());
973     std::shared_ptr<PinHolderCallback> tempCbk;
974     {
975         std::lock_guard<std::mutex> autoLock(lock_);
976         if (pinHolderCallback_.count(pkgName) == 0) {
977             LOGE("OnCreateResult error, device state callback not register.");
978             return;
979         }
980         tempCbk = pinHolderCallback_[pkgName];
981     }
982     if (tempCbk == nullptr) {
983         LOGE("OnCreateResult error, registered device state callback is nullptr.");
984         return;
985     }
986     tempCbk->OnCreateResult(result);
987 }
988 
OnDestroyResult(const std::string & pkgName,int32_t result)989 void DeviceManagerNotify::OnDestroyResult(const std::string &pkgName, int32_t result)
990 {
991     if (pkgName.empty()) {
992         LOGE("Invalid parameter, pkgName is empty.");
993         return;
994     }
995     LOGI("DeviceManagerNotify::OnDestroyResult in, pkgName:%{public}s", pkgName.c_str());
996     std::shared_ptr<PinHolderCallback> tempCbk;
997     {
998         std::lock_guard<std::mutex> autoLock(lock_);
999         if (pinHolderCallback_.count(pkgName) == 0) {
1000             LOGE("OnDestroyResult error, device state callback not register.");
1001             return;
1002         }
1003         tempCbk = pinHolderCallback_[pkgName];
1004     }
1005     if (tempCbk == nullptr) {
1006         LOGE("OnDestroyResult error, registered device state callback is nullptr.");
1007         return;
1008     }
1009     tempCbk->OnDestroyResult(result);
1010 }
1011 
OnPinHolderEvent(const std::string & pkgName,DmPinHolderEvent event,int32_t result,const std::string & content)1012 void DeviceManagerNotify::OnPinHolderEvent(const std::string &pkgName, DmPinHolderEvent event, int32_t result,
1013                                            const std::string &content)
1014 {
1015     if (pkgName.empty()) {
1016         LOGE("Invalid parameter, pkgName is empty.");
1017         return;
1018     }
1019     LOGI("DeviceManagerNotify::OnPinHolderEvent in, pkgName:%{public}s", pkgName.c_str());
1020     std::shared_ptr<PinHolderCallback> tempCbk;
1021     {
1022         std::lock_guard<std::mutex> autoLock(lock_);
1023         if (pinHolderCallback_.count(pkgName) == 0) {
1024             LOGE("OnPinHolderEvent error, pin holder callback not register.");
1025             return;
1026         }
1027         tempCbk = pinHolderCallback_[pkgName];
1028     }
1029     if (tempCbk == nullptr) {
1030         LOGE("OnPinHolderEvent error, registered pin holder callback is nullptr.");
1031         return;
1032     }
1033     tempCbk->OnPinHolderEvent(event, result, content);
1034 }
1035 
GetDmInitCallback()1036 std::map<std::string, std::shared_ptr<DmInitCallback>> DeviceManagerNotify::GetDmInitCallback()
1037 {
1038     std::lock_guard<std::mutex> autoLock(lock_);
1039     std::map<std::string, std::shared_ptr<DmInitCallback>> currentDmInitCallback = dmInitCallback_;
1040     return currentDmInitCallback;
1041 }
1042 
DeviceInfoOnline(const DmDeviceInfo & deviceInfo,std::shared_ptr<DeviceStateCallback> tempCbk)1043 void DeviceManagerNotify::DeviceInfoOnline(const DmDeviceInfo &deviceInfo, std::shared_ptr<DeviceStateCallback> tempCbk)
1044 {
1045     tempCbk->OnDeviceOnline(deviceInfo);
1046 }
1047 
DeviceInfoOffline(const DmDeviceInfo & deviceInfo,std::shared_ptr<DeviceStateCallback> tempCbk)1048 void DeviceManagerNotify::DeviceInfoOffline(const DmDeviceInfo &deviceInfo,
1049     std::shared_ptr<DeviceStateCallback> tempCbk)
1050 {
1051     tempCbk->OnDeviceOffline(deviceInfo);
1052 }
1053 
DeviceInfoChanged(const DmDeviceInfo & deviceInfo,std::shared_ptr<DeviceStateCallback> tempCbk)1054 void DeviceManagerNotify::DeviceInfoChanged(const DmDeviceInfo &deviceInfo,
1055     std::shared_ptr<DeviceStateCallback> tempCbk)
1056 {
1057     tempCbk->OnDeviceChanged(deviceInfo);
1058 }
1059 
DeviceInfoReady(const DmDeviceInfo & deviceInfo,std::shared_ptr<DeviceStateCallback> tempCbk)1060 void DeviceManagerNotify::DeviceInfoReady(const DmDeviceInfo &deviceInfo, std::shared_ptr<DeviceStateCallback> tempCbk)
1061 {
1062     tempCbk->OnDeviceReady(deviceInfo);
1063 }
1064 
DeviceBasicInfoOnline(const DmDeviceBasicInfo & deviceBasicInfo,std::shared_ptr<DeviceStatusCallback> tempCbk)1065 void DeviceManagerNotify::DeviceBasicInfoOnline(const DmDeviceBasicInfo &deviceBasicInfo,
1066     std::shared_ptr<DeviceStatusCallback> tempCbk)
1067 {
1068     tempCbk->OnDeviceOnline(deviceBasicInfo);
1069 }
1070 
DeviceBasicInfoOffline(const DmDeviceBasicInfo & deviceBasicInfo,std::shared_ptr<DeviceStatusCallback> tempCbk)1071 void DeviceManagerNotify::DeviceBasicInfoOffline(const DmDeviceBasicInfo &deviceBasicInfo,
1072     std::shared_ptr<DeviceStatusCallback> tempCbk)
1073 {
1074     tempCbk->OnDeviceOffline(deviceBasicInfo);
1075 }
1076 
DeviceBasicInfoChanged(const DmDeviceBasicInfo & deviceBasicInfo,std::shared_ptr<DeviceStatusCallback> tempCbk)1077 void DeviceManagerNotify::DeviceBasicInfoChanged(const DmDeviceBasicInfo &deviceBasicInfo,
1078     std::shared_ptr<DeviceStatusCallback> tempCbk)
1079 {
1080     tempCbk->OnDeviceChanged(deviceBasicInfo);
1081 }
1082 
DeviceBasicInfoReady(const DmDeviceBasicInfo & deviceBasicInfo,std::shared_ptr<DeviceStatusCallback> tempCbk)1083 void DeviceManagerNotify::DeviceBasicInfoReady(const DmDeviceBasicInfo &deviceBasicInfo,
1084     std::shared_ptr<DeviceStatusCallback> tempCbk)
1085 {
1086     tempCbk->OnDeviceReady(deviceBasicInfo);
1087 }
1088 
RegisterDeviceScreenStatusCallback(const std::string & pkgName,std::shared_ptr<DeviceScreenStatusCallback> callback)1089 void DeviceManagerNotify::RegisterDeviceScreenStatusCallback(const std::string &pkgName,
1090     std::shared_ptr<DeviceScreenStatusCallback> callback)
1091 {
1092     if (pkgName.empty() || callback == nullptr) {
1093         LOGE("Invalid parameter, pkgName is empty or callback is nullptr.");
1094         return;
1095     }
1096     std::lock_guard<std::mutex> autoLock(lock_);
1097     deviceScreenStatusCallback_[pkgName] = callback;
1098 }
1099 
UnRegisterDeviceScreenStatusCallback(const std::string & pkgName)1100 void DeviceManagerNotify::UnRegisterDeviceScreenStatusCallback(const std::string &pkgName)
1101 {
1102     if (pkgName.empty()) {
1103         LOGE("Invalid parameter, pkgName is empty.");
1104         return;
1105     }
1106     std::lock_guard<std::mutex> autoLock(lock_);
1107     deviceScreenStatusCallback_.erase(pkgName);
1108 }
1109 
OnDeviceScreenStatus(const std::string & pkgName,const DmDeviceInfo & deviceInfo)1110 void DeviceManagerNotify::OnDeviceScreenStatus(const std::string &pkgName, const DmDeviceInfo &deviceInfo)
1111 {
1112     if (pkgName.empty()) {
1113         LOGE("Invalid parameter, pkgName is empty.");
1114         return;
1115     }
1116     LOGI("In, pkgName:%{public}s", pkgName.c_str());
1117     std::shared_ptr<DeviceScreenStatusCallback> tempCbk;
1118     {
1119         std::lock_guard<std::mutex> autoLock(lock_);
1120         if (deviceScreenStatusCallback_.count(pkgName) == 0) {
1121             LOGE("error, device screen status not register.");
1122             return;
1123         }
1124         tempCbk = deviceScreenStatusCallback_[pkgName];
1125     }
1126     if (tempCbk == nullptr) {
1127         LOGE("error, registered device screen status callback is nullptr.");
1128         return;
1129     }
1130     tempCbk->OnDeviceScreenStatus(deviceInfo);
1131 }
1132 
RegisterCredentialAuthStatusCallback(const std::string & pkgName,std::shared_ptr<CredentialAuthStatusCallback> callback)1133 void DeviceManagerNotify::RegisterCredentialAuthStatusCallback(const std::string &pkgName,
1134     std::shared_ptr<CredentialAuthStatusCallback> callback)
1135 {
1136     if (pkgName.empty() || callback == nullptr) {
1137         LOGE("Invalid parameter, pkgName is empty or callback is nullptr.");
1138         return;
1139     }
1140     std::lock_guard<std::mutex> autoLock(lock_);
1141     credentialAuthStatusCallback_[pkgName] = callback;
1142 }
1143 
UnRegisterCredentialAuthStatusCallback(const std::string & pkgName)1144 void DeviceManagerNotify::UnRegisterCredentialAuthStatusCallback(const std::string &pkgName)
1145 {
1146     if (pkgName.empty()) {
1147         LOGE("Invalid parameter, pkgName is empty.");
1148         return;
1149     }
1150     std::lock_guard<std::mutex> autoLock(lock_);
1151     credentialAuthStatusCallback_.erase(pkgName);
1152 }
1153 
OnCredentialAuthStatus(const std::string & pkgName,const std::string & proofInfo,uint16_t deviceTypeId,int32_t errcode)1154 void DeviceManagerNotify::OnCredentialAuthStatus(const std::string &pkgName, const std::string &proofInfo,
1155                                                  uint16_t deviceTypeId, int32_t errcode)
1156 {
1157     if (pkgName.empty()) {
1158         LOGE("Invalid parameter, pkgName is empty.");
1159         return;
1160     }
1161     LOGI("In, pkgName:%{public}s", pkgName.c_str());
1162     std::shared_ptr<CredentialAuthStatusCallback> tempCbk;
1163     {
1164         std::lock_guard<std::mutex> autoLock(lock_);
1165         if (credentialAuthStatusCallback_.find(pkgName) == credentialAuthStatusCallback_.end()) {
1166             LOGE("error, credential auth statusnot register.");
1167             return;
1168         }
1169         tempCbk = credentialAuthStatusCallback_[pkgName];
1170     }
1171     if (tempCbk == nullptr) {
1172         LOGE("error, registered credential auth status callback is nullptr.");
1173         return;
1174     }
1175     tempCbk->OnCredentialAuthStatus(proofInfo, deviceTypeId, errcode);
1176 }
1177 } // namespace DistributedHardware
1178 } // namespace OHOS
1179