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