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