• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "device_manager_notify.h"
17 
18 #include "device_manager.h"
19 #include "dm_anonymous.h"
20 #include "dm_constants.h"
21 #include "dm_log.h"
22 
23 namespace OHOS {
24 namespace DistributedHardware {
25 IMPLEMENT_SINGLE_INSTANCE(DeviceManagerNotify);
26 
RegisterDeathRecipientCallback(const std::string & pkgName,std::shared_ptr<DmInitCallback> dmInitCallback)27 void DeviceManagerNotify::RegisterDeathRecipientCallback(const std::string &pkgName,
28                                                          std::shared_ptr<DmInitCallback> dmInitCallback)
29 {
30     if (pkgName.empty() || dmInitCallback == nullptr) {
31         LOGE("Invalid parameter, pkgName is empty or callback is nullptr.");
32         return;
33     }
34     std::lock_guard<std::mutex> autoLock(lock_);
35     dmInitCallback_[pkgName] = dmInitCallback;
36 }
37 
UnRegisterDeathRecipientCallback(const std::string & pkgName)38 void DeviceManagerNotify::UnRegisterDeathRecipientCallback(const std::string &pkgName)
39 {
40     if (pkgName.empty()) {
41         LOGE("Invalid parameter, pkgName is empty.");
42         return;
43     }
44     std::lock_guard<std::mutex> autoLock(lock_);
45     dmInitCallback_.erase(pkgName);
46 }
47 
RegisterDeviceStateCallback(const std::string & pkgName,std::shared_ptr<DeviceStateCallback> callback)48 void DeviceManagerNotify::RegisterDeviceStateCallback(const std::string &pkgName,
49                                                       std::shared_ptr<DeviceStateCallback> callback)
50 {
51     if (pkgName.empty() || callback == nullptr) {
52         LOGE("Invalid parameter, pkgName is empty or callback is nullptr.");
53         return;
54     }
55     std::lock_guard<std::mutex> autoLock(lock_);
56     deviceStateCallback_[pkgName] = callback;
57 }
58 
UnRegisterDeviceStateCallback(const std::string & pkgName)59 void DeviceManagerNotify::UnRegisterDeviceStateCallback(const std::string &pkgName)
60 {
61     if (pkgName.empty()) {
62         LOGE("Invalid parameter, pkgName is empty.");
63         return;
64     }
65     std::lock_guard<std::mutex> autoLock(lock_);
66     deviceStateCallback_.erase(pkgName);
67 }
68 
UnRegisterDeviceStatusCallback(const std::string & pkgName)69 void DeviceManagerNotify::UnRegisterDeviceStatusCallback(const std::string &pkgName)
70 {
71     if (pkgName.empty()) {
72         LOGE("Invalid parameter, pkgName is empty.");
73         return;
74     }
75     std::lock_guard<std::mutex> autoLock(lock_);
76     deviceStatusCallback_.erase(pkgName);
77 }
78 
RegisterDeviceStatusCallback(const std::string & pkgName,std::shared_ptr<DeviceStatusCallback> callback)79 void DeviceManagerNotify::RegisterDeviceStatusCallback(const std::string &pkgName,
80     std::shared_ptr<DeviceStatusCallback> callback)
81 {
82     if (pkgName.empty() || callback == nullptr) {
83         LOGE("Invalid parameter, pkgName is empty or callback is nullptr.");
84         return;
85     }
86     std::lock_guard<std::mutex> autoLock(lock_);
87     deviceStatusCallback_[pkgName] = callback;
88 }
89 
RegisterDiscoveryCallback(const std::string & pkgName,uint16_t subscribeId,std::shared_ptr<DiscoveryCallback> callback)90 void DeviceManagerNotify::RegisterDiscoveryCallback(const std::string &pkgName, uint16_t subscribeId,
91                                                     std::shared_ptr<DiscoveryCallback> callback)
92 {
93     if (pkgName.empty() || callback == nullptr) {
94         LOGE("Invalid parameter, pkgName is empty or callback is nullptr.");
95         return;
96     }
97     std::lock_guard<std::mutex> autoLock(lock_);
98     if (deviceDiscoveryCallbacks_.count(pkgName) == 0) {
99         deviceDiscoveryCallbacks_[pkgName] = std::map<uint16_t, std::shared_ptr<DiscoveryCallback>>();
100     }
101     deviceDiscoveryCallbacks_[pkgName][subscribeId] = callback;
102 }
103 
UnRegisterDiscoveryCallback(const std::string & pkgName,uint16_t subscribeId)104 void DeviceManagerNotify::UnRegisterDiscoveryCallback(const std::string &pkgName, uint16_t subscribeId)
105 {
106     if (pkgName.empty()) {
107         LOGE("Invalid parameter, pkgName is empty.");
108         return;
109     }
110     std::lock_guard<std::mutex> autoLock(lock_);
111     if (deviceDiscoveryCallbacks_.count(pkgName) > 0) {
112         deviceDiscoveryCallbacks_[pkgName].erase(subscribeId);
113         if (deviceDiscoveryCallbacks_[pkgName].empty()) {
114             deviceDiscoveryCallbacks_.erase(pkgName);
115         }
116     }
117 }
118 
RegisterPublishCallback(const std::string & pkgName,int32_t publishId,std::shared_ptr<PublishCallback> callback)119 void DeviceManagerNotify::RegisterPublishCallback(const std::string &pkgName,
120                                                   int32_t publishId,
121                                                   std::shared_ptr<PublishCallback> callback)
122 {
123     if (pkgName.empty() || callback == nullptr) {
124         LOGE("Invalid parameter, pkgName is empty or callback is nullptr.");
125         return;
126     }
127     std::lock_guard<std::mutex> autoLock(lock_);
128     if (devicePublishCallbacks_.count(pkgName) == 0) {
129         devicePublishCallbacks_[pkgName] = std::map<int32_t, std::shared_ptr<PublishCallback>>();
130     }
131     devicePublishCallbacks_[pkgName][publishId] = callback;
132 }
133 
UnRegisterPublishCallback(const std::string & pkgName,int32_t publishId)134 void DeviceManagerNotify::UnRegisterPublishCallback(const std::string &pkgName, int32_t publishId)
135 {
136     if (pkgName.empty()) {
137         LOGE("Invalid parameter, pkgName is empty.");
138         return;
139     }
140     std::lock_guard<std::mutex> autoLock(lock_);
141     if (devicePublishCallbacks_.count(pkgName) > 0) {
142         devicePublishCallbacks_[pkgName].erase(publishId);
143         if (devicePublishCallbacks_[pkgName].empty()) {
144             devicePublishCallbacks_.erase(pkgName);
145         }
146     }
147 }
148 
RegisterAuthenticateCallback(const std::string & pkgName,const std::string & deviceId,std::shared_ptr<AuthenticateCallback> callback)149 void DeviceManagerNotify::RegisterAuthenticateCallback(const std::string &pkgName, const std::string &deviceId,
150                                                        std::shared_ptr<AuthenticateCallback> callback)
151 {
152     if (pkgName.empty() || deviceId.empty() || callback == nullptr) {
153         LOGE("DeviceManagerNotify::RegisterAuthenticateCallback error: Invalid parameter, pkgName: %s, deviceId: %s",
154             pkgName.c_str(), GetAnonyString(deviceId).c_str());
155         return;
156     }
157     std::lock_guard<std::mutex> autoLock(lock_);
158     if (authenticateCallback_.count(pkgName) == 0) {
159         authenticateCallback_[pkgName] = std::map<std::string, std::shared_ptr<AuthenticateCallback>>();
160     }
161     authenticateCallback_[pkgName][deviceId] = callback;
162 }
163 
UnRegisterAuthenticateCallback(const std::string & pkgName,const std::string & deviceId)164 void DeviceManagerNotify::UnRegisterAuthenticateCallback(const std::string &pkgName, const std::string &deviceId)
165 {
166     if (pkgName.empty() || deviceId.empty()) {
167         LOGE("DeviceManagerNotify::UnRegisterAuthenticateCallback error: Invalid parameter, pkgName: %s, deviceId: %s",
168             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].erase(deviceId);
174         if (authenticateCallback_[pkgName].empty()) {
175             authenticateCallback_.erase(pkgName);
176         }
177     }
178 }
179 
UnRegisterPackageCallback(const std::string & pkgName)180 void DeviceManagerNotify::UnRegisterPackageCallback(const std::string &pkgName)
181 {
182     if (pkgName.empty()) {
183         LOGE("Invalid parameter, pkgName is empty.");
184         return;
185     }
186     std::lock_guard<std::mutex> autoLock(lock_);
187     deviceStateCallback_.erase(pkgName);
188     deviceStatusCallback_.erase(pkgName);
189     deviceDiscoveryCallbacks_.erase(pkgName);
190     devicePublishCallbacks_.erase(pkgName);
191     authenticateCallback_.erase(pkgName);
192     dmInitCallback_.erase(pkgName);
193 }
194 
RegisterDeviceManagerFaCallback(const std::string & pkgName,std::shared_ptr<DeviceManagerUiCallback> callback)195 void DeviceManagerNotify::RegisterDeviceManagerFaCallback(const std::string &pkgName,
196                                                           std::shared_ptr<DeviceManagerUiCallback> callback)
197 {
198     std::lock_guard<std::mutex> autoLock(lock_);
199     dmUiCallback_[pkgName] = callback;
200 }
201 
UnRegisterDeviceManagerFaCallback(const std::string & pkgName)202 void DeviceManagerNotify::UnRegisterDeviceManagerFaCallback(const std::string &pkgName)
203 {
204     if (pkgName.empty()) {
205         LOGE("Invalid parameter, pkgName is empty.");
206         return;
207     }
208     std::lock_guard<std::mutex> autoLock(lock_);
209     dmUiCallback_.erase(pkgName);
210 }
211 
RegisterCredentialCallback(const std::string & pkgName,std::shared_ptr<CredentialCallback> callback)212 void DeviceManagerNotify::RegisterCredentialCallback(const std::string &pkgName,
213                                                      std::shared_ptr<CredentialCallback> callback)
214 {
215     if (pkgName.empty() || callback == nullptr) {
216         LOGE("Invalid parameter, pkgName is empty or callback is nullptr.");
217         return;
218     }
219     std::lock_guard<std::mutex> autoLock(lock_);
220     credentialCallback_[pkgName] = callback;
221 }
222 
UnRegisterCredentialCallback(const std::string & pkgName)223 void DeviceManagerNotify::UnRegisterCredentialCallback(const std::string &pkgName)
224 {
225     if (pkgName.empty()) {
226         LOGE("Invalid parameter, pkgName is empty.");
227         return;
228     }
229     std::lock_guard<std::mutex> autoLock(lock_);
230     credentialCallback_.erase(pkgName);
231 }
232 
RegisterPinHolderCallback(const std::string & pkgName,std::shared_ptr<PinHolderCallback> callback)233 void DeviceManagerNotify::RegisterPinHolderCallback(const std::string &pkgName,
234     std::shared_ptr<PinHolderCallback> callback)
235 {
236     if (pkgName.empty() || callback == nullptr) {
237         LOGE("Invalid parameter, pkgName is empty or callback is nullptr.");
238         return;
239     }
240     std::lock_guard<std::mutex> autoLock(lock_);
241     pinHolderCallback_[pkgName] = callback;
242 }
243 
OnRemoteDied()244 void DeviceManagerNotify::OnRemoteDied()
245 {
246     LOGW("DeviceManagerNotify::OnRemoteDied");
247     std::lock_guard<std::mutex> autoLock(lock_);
248     for (auto iter : dmInitCallback_) {
249         LOGI("DeviceManagerNotify::OnRemoteDied, pkgName:%s", iter.first.c_str());
250         if (iter.second != nullptr) {
251             iter.second->OnRemoteDied();
252         }
253     }
254 }
255 
OnDeviceOnline(const std::string & pkgName,const DmDeviceInfo & deviceInfo)256 void DeviceManagerNotify::OnDeviceOnline(const std::string &pkgName, const DmDeviceInfo &deviceInfo)
257 {
258     if (pkgName.empty()) {
259         LOGE("Invalid parameter, pkgName is empty.");
260         return;
261     }
262     LOGI("DeviceManagerNotify::OnDeviceOnline with DmDeviceInfo, pkgName:%s", pkgName.c_str());
263     std::shared_ptr<DeviceStateCallback> tempCbk;
264     {
265         std::lock_guard<std::mutex> autoLock(lock_);
266         auto iter = deviceStateCallback_.find(pkgName);
267         if (iter == deviceStateCallback_.end()) {
268             LOGE("OnDeviceOnline error, device state callback not register.");
269             return;
270         }
271         tempCbk = iter->second;
272     }
273     if (tempCbk == nullptr) {
274         LOGE("OnDeviceOnline error, registered device state callback is nullptr.");
275         return;
276     }
277     LOGI("DeviceManagerNotify::OnDeviceOnline complete with DmDeviceInfo, pkgName:%s", pkgName.c_str());
278     tempCbk->OnDeviceOnline(deviceInfo);
279 }
280 
OnDeviceOnline(const std::string & pkgName,const DmDeviceBasicInfo & deviceBasicInfo)281 void DeviceManagerNotify::OnDeviceOnline(const std::string &pkgName, const DmDeviceBasicInfo &deviceBasicInfo)
282 {
283     if (pkgName.empty()) {
284         LOGE("Invalid parameter, pkgName is empty.");
285         return;
286     }
287     LOGI("DeviceManagerNotify::OnDeviceOnline with DmDeviceBasicInfo, pkgName:%s", pkgName.c_str());
288     std::shared_ptr<DeviceStatusCallback> tempCbk;
289     {
290         std::lock_guard<std::mutex> autoLock(lock_);
291         auto iter = deviceStatusCallback_.find(pkgName);
292         if (iter == deviceStatusCallback_.end()) {
293             LOGE("OnDeviceOnline error, device status callback not register.");
294             return;
295         }
296         tempCbk = iter->second;
297     }
298     if (tempCbk == nullptr) {
299         LOGE("OnDeviceOnline error, registered device status callback is nullptr.");
300         return;
301     }
302     LOGI("DeviceManagerNotify::OnDeviceOnline complete with DmDeviceBasicInfo, pkgName:%s", pkgName.c_str());
303     tempCbk->OnDeviceOnline(deviceBasicInfo);
304 }
305 
OnDeviceOffline(const std::string & pkgName,const DmDeviceInfo & deviceInfo)306 void DeviceManagerNotify::OnDeviceOffline(const std::string &pkgName, const DmDeviceInfo &deviceInfo)
307 {
308     if (pkgName.empty()) {
309         LOGE("Invalid parameter, pkgName is empty.");
310         return;
311     }
312     LOGI("DeviceManagerNotify::OnDeviceOffline with DmDeviceInfo, pkgName:%s", pkgName.c_str());
313     std::shared_ptr<DeviceStateCallback> tempCbk;
314     {
315         std::lock_guard<std::mutex> autoLock(lock_);
316         auto iter = deviceStateCallback_.find(pkgName);
317         if (iter == deviceStateCallback_.end()) {
318             LOGE("OnDeviceOffline error, device state callback not register.");
319             return;
320         }
321         tempCbk = iter->second;
322     }
323     if (tempCbk == nullptr) {
324         LOGE("OnDeviceOffline error, registered device state callback is nullptr.");
325         return;
326     }
327     LOGI("DeviceManagerNotify::OnDeviceOffline complete with DmDeviceInfo, pkgName:%s", pkgName.c_str());
328     tempCbk->OnDeviceOffline(deviceInfo);
329 }
330 
OnDeviceOffline(const std::string & pkgName,const DmDeviceBasicInfo & deviceBasicInfo)331 void DeviceManagerNotify::OnDeviceOffline(const std::string &pkgName, const DmDeviceBasicInfo &deviceBasicInfo)
332 {
333     if (pkgName.empty()) {
334         LOGE("Invalid parameter, pkgName is empty.");
335         return;
336     }
337     LOGI("DeviceManagerNotify::OnDeviceOffline with DmDeviceBasicInfo, pkgName:%s", pkgName.c_str());
338     std::shared_ptr<DeviceStatusCallback> tempCbk;
339     {
340         std::lock_guard<std::mutex> autoLock(lock_);
341         auto iter = deviceStatusCallback_.find(pkgName);
342         if (iter == deviceStatusCallback_.end()) {
343             LOGE("OnDeviceOffline error, device status callback not register.");
344             return;
345         }
346         tempCbk = iter->second;
347     }
348     if (tempCbk == nullptr) {
349         LOGE("OnDeviceOffline error, registered device status callback is nullptr.");
350         return;
351     }
352     LOGI("DeviceManagerNotify::OnDeviceOffline complete with DmDeviceBasicInfo, pkgName:%s", pkgName.c_str());
353     tempCbk->OnDeviceOffline(deviceBasicInfo);
354 }
355 
OnDeviceChanged(const std::string & pkgName,const DmDeviceInfo & deviceInfo)356 void DeviceManagerNotify::OnDeviceChanged(const std::string &pkgName, const DmDeviceInfo &deviceInfo)
357 {
358     if (pkgName.empty()) {
359         LOGE("Invalid parameter, pkgName is empty.");
360         return;
361     }
362     LOGI("DeviceManagerNotify::OnDeviceChanged with DmDeviceInfo, pkgName:%s", pkgName.c_str());
363     std::shared_ptr<DeviceStateCallback> tempCbk;
364     {
365         std::lock_guard<std::mutex> autoLock(lock_);
366         auto iter = deviceStateCallback_.find(pkgName);
367         if (iter == deviceStateCallback_.end()) {
368             LOGE("OnDeviceChanged error, device state callback not register.");
369             return;
370         }
371         tempCbk = iter->second;
372     }
373     if (tempCbk == nullptr) {
374         LOGE("OnDeviceChanged error, registered device state callback is nullptr.");
375         return;
376     }
377     LOGI("DeviceManagerNotify::OnDeviceChanged complete with DmDeviceInfo, pkgName:%s", pkgName.c_str());
378     tempCbk->OnDeviceChanged(deviceInfo);
379 }
380 
OnDeviceChanged(const std::string & pkgName,const DmDeviceBasicInfo & deviceBasicInfo)381 void DeviceManagerNotify::OnDeviceChanged(const std::string &pkgName, const DmDeviceBasicInfo &deviceBasicInfo)
382 {
383     if (pkgName.empty()) {
384         LOGE("Invalid parameter, pkgName is empty.");
385         return;
386     }
387     LOGI("DeviceManagerNotify::OnDeviceChanged with DmDeviceBasicInfo, pkgName:%s", pkgName.c_str());
388     std::shared_ptr<DeviceStatusCallback> tempCbk;
389     {
390         std::lock_guard<std::mutex> autoLock(lock_);
391         auto iter = deviceStatusCallback_.find(pkgName);
392         if (iter == deviceStatusCallback_.end()) {
393             LOGE("OnDeviceChanged error, device state callback not register.");
394             return;
395         }
396         tempCbk = iter->second;
397     }
398     if (tempCbk == nullptr) {
399         LOGE("OnDeviceChanged error, registered device state callback is nullptr.");
400         return;
401     }
402     LOGI("DeviceManagerNotify::OnDeviceChanged complete with DmDeviceBasicInfo, pkgName:%s", pkgName.c_str());
403     tempCbk->OnDeviceChanged(deviceBasicInfo);
404 }
405 
OnDeviceReady(const std::string & pkgName,const DmDeviceInfo & deviceInfo)406 void DeviceManagerNotify::OnDeviceReady(const std::string &pkgName, const DmDeviceInfo &deviceInfo)
407 {
408     if (pkgName.empty()) {
409         LOGE("Invalid parameter, pkgName is empty.");
410         return;
411     }
412     LOGI("DeviceManagerNotify::OnDeviceReady with DmDeviceInfo, pkgName:%s", pkgName.c_str());
413     std::shared_ptr<DeviceStateCallback> tempCbk;
414     {
415         std::lock_guard<std::mutex> autoLock(lock_);
416         auto iter = deviceStateCallback_.find(pkgName);
417         if (iter == deviceStateCallback_.end()) {
418             LOGE("OnDeviceReady error, device state callback not register.");
419             return;
420         }
421         tempCbk = iter->second;
422     }
423     if (tempCbk == nullptr) {
424         LOGE("OnDeviceReady error, registered device state callback is nullptr.");
425         return;
426     }
427     LOGI("DeviceManagerNotify::OnDeviceReady complete with DmDeviceInfo, pkgName:%s", pkgName.c_str());
428     tempCbk->OnDeviceReady(deviceInfo);
429 }
430 
OnDeviceReady(const std::string & pkgName,const DmDeviceBasicInfo & deviceBasicInfo)431 void DeviceManagerNotify::OnDeviceReady(const std::string &pkgName, const DmDeviceBasicInfo &deviceBasicInfo)
432 {
433     if (pkgName.empty()) {
434         LOGE("Invalid parameter, pkgName is empty.");
435         return;
436     }
437     LOGI("DeviceManagerNotify::OnDeviceReady with DmDeviceBasicInfo, pkgName:%s", pkgName.c_str());
438     std::shared_ptr<DeviceStatusCallback> tempCbk;
439     {
440         std::lock_guard<std::mutex> autoLock(lock_);
441         auto iter = deviceStatusCallback_.find(pkgName);
442         if (iter == deviceStatusCallback_.end()) {
443             LOGE("OnDeviceReady error, device status callback not register.");
444             return;
445         }
446         tempCbk = iter->second;
447     }
448     if (tempCbk == nullptr) {
449         LOGE("OnDeviceReady error, registered device status callback is nullptr.");
450         return;
451     }
452     LOGI("DeviceManagerNotify::OnDeviceReady complete with DmDeviceBasicInfo, pkgName:%s", pkgName.c_str());
453     tempCbk->OnDeviceReady(deviceBasicInfo);
454 }
455 
OnDeviceFound(const std::string & pkgName,uint16_t subscribeId,const DmDeviceInfo & deviceInfo)456 void DeviceManagerNotify::OnDeviceFound(const std::string &pkgName, uint16_t subscribeId,
457                                         const DmDeviceInfo &deviceInfo)
458 {
459     if (pkgName.empty()) {
460         LOGE("Invalid parameter, pkgName is empty.");
461         return;
462     }
463     LOGI("DeviceManagerNotify::OnDeviceFound with DmDeviceInfo, pkgName:%s, subscribeId:%d.",
464          pkgName.c_str(), (int32_t)subscribeId);
465     std::shared_ptr<DiscoveryCallback> tempCbk;
466     {
467         std::lock_guard<std::mutex> autoLock(lock_);
468         if (deviceDiscoveryCallbacks_.count(pkgName) == 0) {
469             LOGE("DeviceManagerNotify::OnDeviceFound error, device discovery callback not register for pkgName %s.",
470                 pkgName.c_str());
471             return;
472         }
473         std::map<uint16_t, std::shared_ptr<DiscoveryCallback>> &discoverCallMap = deviceDiscoveryCallbacks_[pkgName];
474         auto iter = discoverCallMap.find(subscribeId);
475         if (iter == discoverCallMap.end()) {
476             LOGE("OnDeviceFound error, no register deviceDiscoveryCallback for subscribeId %d.", (int32_t)subscribeId);
477             return;
478         }
479         tempCbk = iter->second;
480     }
481     if (tempCbk == nullptr) {
482         LOGE("OnDeviceFound error, registered device discovery callback is nullptr.");
483         return;
484     }
485     LOGI("DeviceManagerNotify::OnDeviceFound complete with DmDeviceInfo, pkgName:%s, subscribeId:%d.",
486          pkgName.c_str(), (int32_t)subscribeId);
487     tempCbk->OnDeviceFound(subscribeId, deviceInfo);
488 }
489 
OnDeviceFound(const std::string & pkgName,uint16_t subscribeId,const DmDeviceBasicInfo & deviceBasicInfo)490 void DeviceManagerNotify::OnDeviceFound(const std::string &pkgName, uint16_t subscribeId,
491                                         const DmDeviceBasicInfo &deviceBasicInfo)
492 {
493     if (pkgName.empty()) {
494         LOGE("Invalid parameter, pkgName is empty.");
495         return;
496     }
497     LOGI("DeviceManagerNotify::OnDeviceFound with DmDeviceBasicInfo, pkgName:%s, subscribeId:%d.",
498          pkgName.c_str(), (int32_t)subscribeId);
499     std::shared_ptr<DiscoveryCallback> tempCbk;
500     {
501         std::lock_guard<std::mutex> autoLock(lock_);
502         if (deviceDiscoveryCallbacks_.count(pkgName) == 0) {
503             LOGE("DeviceManagerNotify::OnDeviceFound error, device discovery callback not register for pkgName %s.",
504                 pkgName.c_str());
505             return;
506         }
507         std::map<uint16_t, std::shared_ptr<DiscoveryCallback>> &discoverCallMap = deviceDiscoveryCallbacks_[pkgName];
508         auto iter = discoverCallMap.find(subscribeId);
509         if (iter == discoverCallMap.end()) {
510             LOGE("OnDeviceFound error, no register deviceDiscoveryCallback for subscribeId %d.", (int32_t)subscribeId);
511             return;
512         }
513         tempCbk = iter->second;
514     }
515     if (tempCbk == nullptr) {
516         LOGE("OnDeviceFound error, registered device discovery callback is nullptr.");
517         return;
518     }
519     LOGI("DeviceManagerNotify::OnDeviceFound complete with DmDeviceBasicInfo, pkgName:%s, subscribeId:%d.",
520          pkgName.c_str(), (int32_t)subscribeId);
521     tempCbk->OnDeviceFound(subscribeId, deviceBasicInfo);
522 }
523 
OnDiscoveryFailed(const std::string & pkgName,uint16_t subscribeId,int32_t failedReason)524 void DeviceManagerNotify::OnDiscoveryFailed(const std::string &pkgName, uint16_t subscribeId, int32_t failedReason)
525 {
526     if (pkgName.empty()) {
527         LOGE("Invalid parameter, pkgName is empty.");
528         return;
529     }
530     LOGI("DeviceManagerNotify::OnDiscoveryFailed in, pkgName:%s, subscribeId %d, failed reason %d", pkgName.c_str(),
531         (int32_t)subscribeId, failedReason);
532     std::shared_ptr<DiscoveryCallback> tempCbk;
533     {
534         std::lock_guard<std::mutex> autoLock(lock_);
535         if (deviceDiscoveryCallbacks_.count(pkgName) == 0) {
536             LOGE("DeviceManagerNotify::OnDiscoveryFailed error, device discovery callback not register for pkgName %s.",
537                 pkgName.c_str());
538             return;
539         }
540         std::map<uint16_t, std::shared_ptr<DiscoveryCallback>> &discoverCallMap = deviceDiscoveryCallbacks_[pkgName];
541         auto iter = discoverCallMap.find(subscribeId);
542         if (iter == discoverCallMap.end()) {
543             LOGE("OnDiscoveryFailed error, device discovery callback not register for subscribeId %d.", subscribeId);
544             return;
545         }
546         tempCbk = iter->second;
547     }
548     if (tempCbk == nullptr) {
549         LOGE("OnDiscoveryFailed error, registered device discovery callback is nullptr.");
550         return;
551     }
552     tempCbk->OnDiscoveryFailed(subscribeId, failedReason);
553 }
554 
OnDiscoverySuccess(const std::string & pkgName,uint16_t subscribeId)555 void DeviceManagerNotify::OnDiscoverySuccess(const std::string &pkgName, uint16_t subscribeId)
556 {
557     if (pkgName.empty()) {
558         LOGE("Invalid parameter, pkgName is empty.");
559         return;
560     }
561     LOGI("DeviceManagerNotify::OnDiscoverySuccess in, pkgName:%s, subscribeId:%d.", pkgName.c_str(), subscribeId);
562     std::shared_ptr<DiscoveryCallback> tempCbk;
563     {
564         std::lock_guard<std::mutex> autoLock(lock_);
565         if (deviceDiscoveryCallbacks_.count(pkgName) == 0) {
566             LOGE("OnDiscoverySuccess error, device discovery callback not register for pkgName %s.", pkgName.c_str());
567             return;
568         }
569         std::map<uint16_t, std::shared_ptr<DiscoveryCallback>> &discoverCallMap = deviceDiscoveryCallbacks_[pkgName];
570         auto iter = discoverCallMap.find(subscribeId);
571         if (iter == discoverCallMap.end()) {
572             LOGE("OnDiscoverySuccess error, device discovery callback not register for subscribeId %d.",
573                 (int32_t)subscribeId);
574             return;
575         }
576         tempCbk = iter->second;
577     }
578     if (tempCbk == nullptr) {
579         LOGE("OnDiscoverySuccess error, registered device discovery callback is nullptr.");
580         return;
581     }
582     tempCbk->OnDiscoverySuccess(subscribeId);
583 }
584 
OnPublishResult(const std::string & pkgName,int32_t publishId,int32_t publishResult)585 void DeviceManagerNotify::OnPublishResult(const std::string &pkgName, int32_t publishId, int32_t publishResult)
586 {
587     if (pkgName.empty()) {
588         LOGE("Invalid parameter, pkgName is empty.");
589         return;
590     }
591     LOGI("DeviceManagerNotify::OnPublishResult in, pkgName:%s, publishId %d, publishResult %d", pkgName.c_str(),
592         publishId, publishResult);
593     std::shared_ptr<PublishCallback> tempCbk;
594     {
595         std::lock_guard<std::mutex> autoLock(lock_);
596         if (devicePublishCallbacks_.count(pkgName) == 0) {
597             LOGE("DeviceManagerNotify::OnPublishResult error, device publish callback not register for pkgName %s.",
598                 pkgName.c_str());
599             return;
600         }
601         std::map<int32_t, std::shared_ptr<PublishCallback>> &publishCallMap = devicePublishCallbacks_[pkgName];
602         auto iter = publishCallMap.find(publishId);
603         if (iter == publishCallMap.end()) {
604             LOGE("OnPublishResult error, device publish callback not register for publishId %d.", publishId);
605             return;
606         }
607         tempCbk = iter->second;
608     }
609     if (tempCbk == nullptr) {
610         LOGE("OnPublishResult error, registered device publish callback is nullptr.");
611         return;
612     }
613     tempCbk->OnPublishResult(publishId, publishResult);
614 }
615 
OnAuthResult(const std::string & pkgName,const std::string & deviceId,const std::string & token,int32_t status,int32_t reason)616 void DeviceManagerNotify::OnAuthResult(const std::string &pkgName, const std::string &deviceId,
617                                        const std::string &token, int32_t status, int32_t reason)
618 {
619     if (pkgName.empty() || token.empty() || deviceId.empty()) {
620         LOGE("Invalid para, pkgName: %s, token: %s", pkgName.c_str(), token.c_str());
621         return;
622     }
623     LOGI("DeviceManagerNotify::OnAuthResult in, pkgName:%s, status:%d, reason:%d", pkgName.c_str(), status, reason);
624     std::shared_ptr<AuthenticateCallback> tempCbk;
625     {
626         std::lock_guard<std::mutex> autoLock(lock_);
627         if (authenticateCallback_.count(pkgName) == 0) {
628             LOGE("DeviceManagerNotify::OnAuthResult error, authenticate callback not register for pkgName %s.",
629                 pkgName.c_str());
630             return;
631         }
632         std::map<std::string, std::shared_ptr<AuthenticateCallback>> &authCallMap = authenticateCallback_[pkgName];
633         auto iter = authCallMap.find(deviceId);
634         if (iter == authCallMap.end()) {
635             LOGE("OnAuthResult error, authenticate callback not register.");
636             return;
637         }
638         tempCbk = iter->second;
639     }
640     if (tempCbk == nullptr) {
641         LOGE("OnAuthResult error, registered authenticate callback is nullptr.");
642         return;
643     }
644     tempCbk->OnAuthResult(deviceId, token, status, reason);
645     if (reason == DM_OK && (status <= STATUS_DM_CLOSE_PIN_INPUT_UI && status >= STATUS_DM_SHOW_AUTHORIZE_UI)) {
646         LOGI("update ui change, status: %d, reason: %d", status, reason);
647         return;
648     }
649     {
650         std::lock_guard<std::mutex> autoLock(lock_);
651         authenticateCallback_[pkgName].erase(deviceId);
652         if (authenticateCallback_[pkgName].empty()) {
653             authenticateCallback_.erase(pkgName);
654         }
655     }
656 }
657 
OnUiCall(std::string & pkgName,std::string & paramJson)658 void DeviceManagerNotify::OnUiCall(std::string &pkgName, std::string &paramJson)
659 {
660     if (pkgName.empty()) {
661         LOGE("DeviceManagerNotify::OnUiCall error: Invalid parameter, pkgName: %s", pkgName.c_str());
662         return;
663     }
664     LOGI("DeviceManagerNotify::OnUiCall in, pkgName:%s", pkgName.c_str());
665     std::shared_ptr<DeviceManagerUiCallback> tempCbk;
666     {
667         std::lock_guard<std::mutex> autoLock(lock_);
668         if (dmUiCallback_.count(pkgName) == 0) {
669             LOGE("OnUiCall error, dm Ui callback not register for pkgName %s.", pkgName.c_str());
670             return;
671         }
672         tempCbk = dmUiCallback_[pkgName];
673     }
674     if (tempCbk == nullptr) {
675         LOGE("OnUiCall error, registered dm Ui callback is nullptr.");
676         return;
677     }
678     tempCbk->OnCall(paramJson);
679 }
680 
OnCredentialResult(const std::string & pkgName,int32_t & action,const std::string & credentialResult)681 void DeviceManagerNotify::OnCredentialResult(const std::string &pkgName, int32_t &action,
682                                              const std::string &credentialResult)
683 {
684     if (pkgName.empty()) {
685         LOGE("DeviceManagerNotify::OnCredentialResult error: Invalid parameter, pkgName: %s", pkgName.c_str());
686         return;
687     }
688     LOGI("DeviceManagerNotify::OnCredentialResult in, pkgName:%s, action:%d", pkgName.c_str(), action);
689     std::shared_ptr<CredentialCallback> tempCbk;
690     {
691         std::lock_guard<std::mutex> autoLock(lock_);
692         if (credentialCallback_.count(pkgName) == 0) {
693             LOGE("DeviceManagerNotify::OnCredentialResult error, credential callback not register for pkgName %s.",
694                 pkgName.c_str());
695             return;
696         }
697         tempCbk = credentialCallback_[pkgName];
698     }
699     if (tempCbk == nullptr) {
700         LOGE("OnCredentialResult error, registered credential callback is nullptr.");
701         return;
702     }
703     tempCbk->OnCredentialResult(action, credentialResult);
704 }
705 
RegisterBindCallback(const std::string & pkgName,const PeerTargetId & targetId,std::shared_ptr<BindTargetCallback> callback)706 void DeviceManagerNotify::RegisterBindCallback(const std::string &pkgName, const PeerTargetId &targetId,
707     std::shared_ptr<BindTargetCallback> callback)
708 {
709     if (pkgName.empty() || IsInvalidPeerTargetId(targetId) || (callback == nullptr)) {
710         LOGE("DeviceManagerNotify::RegisterBindCallback error: Invalid parameter, pkgName: %s.", pkgName.c_str());
711         return;
712     }
713     std::lock_guard<std::mutex> autoLock(lock_);
714     if (bindCallback_.count(pkgName) == 0) {
715         bindCallback_[pkgName] = std::map<PeerTargetId, std::shared_ptr<BindTargetCallback>>();
716     }
717     bindCallback_[pkgName][targetId] = callback;
718 }
719 
RegisterUnbindCallback(const std::string & pkgName,const PeerTargetId & targetId,std::shared_ptr<UnbindTargetCallback> callback)720 void DeviceManagerNotify::RegisterUnbindCallback(const std::string &pkgName, const PeerTargetId &targetId,
721     std::shared_ptr<UnbindTargetCallback> callback)
722 {
723     if (pkgName.empty() || IsInvalidPeerTargetId(targetId) || (callback == nullptr)) {
724         LOGE("DeviceManagerNotify::RegisterUnbindCallback error: Invalid parameter, pkgName: %s.", pkgName.c_str());
725         return;
726     }
727     std::lock_guard<std::mutex> autoLock(lock_);
728     if (unbindCallback_.count(pkgName) == 0) {
729         unbindCallback_[pkgName] = std::map<PeerTargetId, std::shared_ptr<UnbindTargetCallback>>();
730     }
731     unbindCallback_[pkgName][targetId] = callback;
732 }
733 
OnBindResult(const std::string & pkgName,const PeerTargetId & targetId,int32_t result,int32_t status,std::string content)734 void DeviceManagerNotify::OnBindResult(const std::string &pkgName, const PeerTargetId &targetId,
735     int32_t result, int32_t status, std::string content)
736 {
737     if (pkgName.empty() || IsInvalidPeerTargetId(targetId)) {
738         LOGE("Invalid para, pkgName: %s.", pkgName.c_str());
739         return;
740     }
741     LOGI("DeviceManagerNotify::OnBindResult in, pkgName:%s, result:%d", pkgName.c_str(), result);
742     std::shared_ptr<BindTargetCallback> tempCbk;
743     {
744         std::lock_guard<std::mutex> autoLock(lock_);
745         if (bindCallback_.count(pkgName) == 0) {
746             LOGE("DeviceManagerNotify::OnBindResult error, callback not register for pkgName %s.", pkgName.c_str());
747             return;
748         }
749         std::map<PeerTargetId, std::shared_ptr<BindTargetCallback>> &bindCbkMap = bindCallback_[pkgName];
750         auto iter = bindCbkMap.find(targetId);
751         if (iter == bindCbkMap.end()) {
752             LOGE("OnBindResult error, bind callback not register for targetId.");
753             return;
754         }
755         tempCbk = iter->second;
756     }
757     if (tempCbk == nullptr) {
758         LOGE("OnBindResult error, registered bind callback is nullptr.");
759         return;
760     }
761     tempCbk->OnBindResult(targetId, result, status, content);
762     if (result == DM_OK && (status <= STATUS_DM_CLOSE_PIN_INPUT_UI && status >= STATUS_DM_SHOW_AUTHORIZE_UI)) {
763         LOGI("notify bind status, result: %d, status: %d", result, status);
764         return;
765     }
766     {
767         std::lock_guard<std::mutex> autoLock(lock_);
768         bindCallback_[pkgName].erase(targetId);
769         if (bindCallback_[pkgName].empty()) {
770             bindCallback_.erase(pkgName);
771         }
772     }
773 }
774 
OnUnbindResult(const std::string & pkgName,const PeerTargetId & targetId,int32_t result,std::string content)775 void DeviceManagerNotify::OnUnbindResult(const std::string &pkgName, const PeerTargetId &targetId,
776     int32_t result, std::string content)
777 {
778     if (pkgName.empty() || IsInvalidPeerTargetId(targetId)) {
779         LOGE("Invalid para, pkgName: %s.", pkgName.c_str());
780         return;
781     }
782     LOGI("DeviceManagerNotify::OnUnbindResult in, pkgName:%s, result:%d", pkgName.c_str(), result);
783     std::shared_ptr<UnbindTargetCallback> tempCbk;
784     {
785         std::lock_guard<std::mutex> autoLock(lock_);
786         if (unbindCallback_.count(pkgName) == 0) {
787             LOGE("DeviceManagerNotify::OnUnbindResult error, callback not register for pkgName %s.", pkgName.c_str());
788             return;
789         }
790         std::map<PeerTargetId, std::shared_ptr<UnbindTargetCallback>> &unbindCbkMap = unbindCallback_[pkgName];
791         auto iter = unbindCbkMap.find(targetId);
792         if (iter == unbindCbkMap.end()) {
793             LOGE("OnUnbindResult error, unbind callback not register for targetId.");
794             return;
795         }
796         tempCbk = iter->second;
797     }
798     if (tempCbk == nullptr) {
799         LOGE("OnUnbindResult error, registered unbind callback is nullptr.");
800         return;
801     }
802     tempCbk->OnUnbindResult(targetId, result, content);
803     {
804         std::lock_guard<std::mutex> autoLock(lock_);
805         unbindCallback_[pkgName].erase(targetId);
806         if (unbindCallback_[pkgName].empty()) {
807             unbindCallback_.erase(pkgName);
808         }
809     }
810 }
811 
OnPinHolderCreate(const std::string & pkgName,const std::string & deviceId,DmPinType pinType,const std::string & payload)812 void DeviceManagerNotify::OnPinHolderCreate(const std::string &pkgName, const std::string &deviceId,
813     DmPinType pinType, const std::string &payload)
814 {
815     if (pkgName.empty()) {
816         LOGE("Invalid parameter, pkgName is empty.");
817         return;
818     }
819     LOGI("DeviceManagerNotify::OnPinHolderCreate in, pkgName:%s", pkgName.c_str());
820     std::shared_ptr<PinHolderCallback> tempCbk;
821     {
822         std::lock_guard<std::mutex> autoLock(lock_);
823         if (pinHolderCallback_.count(pkgName) == 0) {
824             LOGE("OnPinHolderCreate error, device state callback not register.");
825             return;
826         }
827         tempCbk = pinHolderCallback_[pkgName];
828     }
829     if (tempCbk == nullptr) {
830         LOGE("OnPinHolderCreate error, registered device state callback is nullptr.");
831         return;
832     }
833     tempCbk->OnPinHolderCreate(deviceId, pinType, payload);
834 }
835 
OnPinHolderDestroy(const std::string & pkgName,DmPinType pinType,const std::string & payload)836 void DeviceManagerNotify::OnPinHolderDestroy(const std::string &pkgName, DmPinType pinType,
837     const std::string &payload)
838 {
839     if (pkgName.empty()) {
840         LOGE("Invalid parameter, pkgName is empty.");
841         return;
842     }
843     LOGI("DeviceManagerNotify::OnPinHolderDestroy in, pkgName:%s", pkgName.c_str());
844     std::shared_ptr<PinHolderCallback> tempCbk;
845     {
846         std::lock_guard<std::mutex> autoLock(lock_);
847         if (pinHolderCallback_.count(pkgName) == 0) {
848             LOGE("OnPinHolderDestroy error, device state callback not register.");
849             return;
850         }
851         tempCbk = pinHolderCallback_[pkgName];
852     }
853     if (tempCbk == nullptr) {
854         LOGE("OnPinHolderDestroy error, registered device state callback is nullptr.");
855         return;
856     }
857     tempCbk->OnPinHolderDestroy(pinType, payload);
858 }
859 
OnCreateResult(const std::string & pkgName,int32_t result)860 void DeviceManagerNotify::OnCreateResult(const std::string &pkgName, int32_t result)
861 {
862     if (pkgName.empty()) {
863         LOGE("Invalid parameter, pkgName is empty.");
864         return;
865     }
866     LOGI("DeviceManagerNotify::OnCreateResult in, pkgName:%s", pkgName.c_str());
867     std::shared_ptr<PinHolderCallback> tempCbk;
868     {
869         std::lock_guard<std::mutex> autoLock(lock_);
870         if (pinHolderCallback_.count(pkgName) == 0) {
871             LOGE("OnCreateResult error, device state callback not register.");
872             return;
873         }
874         tempCbk = pinHolderCallback_[pkgName];
875     }
876     if (tempCbk == nullptr) {
877         LOGE("OnCreateResult error, registered device state callback is nullptr.");
878         return;
879     }
880     tempCbk->OnCreateResult(result);
881 }
882 
OnDestroyResult(const std::string & pkgName,int32_t result)883 void DeviceManagerNotify::OnDestroyResult(const std::string &pkgName, int32_t result)
884 {
885     if (pkgName.empty()) {
886         LOGE("Invalid parameter, pkgName is empty.");
887         return;
888     }
889     LOGI("DeviceManagerNotify::OnDestroyResult in, pkgName:%s", pkgName.c_str());
890     std::shared_ptr<PinHolderCallback> tempCbk;
891     {
892         std::lock_guard<std::mutex> autoLock(lock_);
893         if (pinHolderCallback_.count(pkgName) == 0) {
894             LOGE("OnDestroyResult error, device state callback not register.");
895             return;
896         }
897         tempCbk = pinHolderCallback_[pkgName];
898     }
899     if (tempCbk == nullptr) {
900         LOGE("OnDestroyResult error, registered device state callback is nullptr.");
901         return;
902     }
903     tempCbk->OnDestroyResult(result);
904 }
905 
GetDmInitCallback()906 std::map<std::string, std::shared_ptr<DmInitCallback>> DeviceManagerNotify::GetDmInitCallback()
907 {
908     std::lock_guard<std::mutex> autoLock(lock_);
909     std::map<std::string, std::shared_ptr<DmInitCallback>> currentDmInitCallback = dmInitCallback_;
910     return currentDmInitCallback;
911 }
912 } // namespace DistributedHardware
913 } // namespace OHOS
914