• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 "dm_log.h"
18 #include "nlohmann/json.hpp"
19 #include "dm_constants.h"
20 #include "device_manager.h"
21 
22 namespace OHOS {
23 namespace DistributedHardware {
24 IMPLEMENT_SINGLE_INSTANCE(DeviceManagerNotify);
25 
RegisterDeathRecipientCallback(const std::string & pkgName,std::shared_ptr<DmInitCallback> dmInitCallback)26 void DeviceManagerNotify::RegisterDeathRecipientCallback(const std::string &pkgName,
27                                                          std::shared_ptr<DmInitCallback> dmInitCallback)
28 {
29     dmInitCallback_[pkgName] = dmInitCallback;
30 }
31 
UnRegisterDeathRecipientCallback(const std::string & pkgName)32 void DeviceManagerNotify::UnRegisterDeathRecipientCallback(const std::string &pkgName)
33 {
34     dmInitCallback_.erase(pkgName);
35 }
36 
RegisterDeviceStateCallback(const std::string & pkgName,std::shared_ptr<DeviceStateCallback> callback)37 void DeviceManagerNotify::RegisterDeviceStateCallback(const std::string &pkgName,
38                                                       std::shared_ptr<DeviceStateCallback> callback)
39 {
40     deviceStateCallback_[pkgName] = callback;
41 }
42 
UnRegisterDeviceStateCallback(const std::string & pkgName)43 void DeviceManagerNotify::UnRegisterDeviceStateCallback(const std::string &pkgName)
44 {
45     deviceStateCallback_.erase(pkgName);
46 }
47 
RegisterDiscoveryCallback(const std::string & pkgName,uint16_t subscribeId,std::shared_ptr<DiscoveryCallback> callback)48 void DeviceManagerNotify::RegisterDiscoveryCallback(const std::string &pkgName, uint16_t subscribeId,
49                                                     std::shared_ptr<DiscoveryCallback> callback)
50 {
51     if (deviceDiscoveryCallbacks_.count(pkgName) == 0) {
52         deviceDiscoveryCallbacks_[pkgName] = std::map<uint16_t, std::shared_ptr<DiscoveryCallback>>();
53     }
54     deviceDiscoveryCallbacks_[pkgName][subscribeId] = callback;
55 }
56 
UnRegisterDiscoveryCallback(const std::string & pkgName,uint16_t subscribeId)57 void DeviceManagerNotify::UnRegisterDiscoveryCallback(const std::string &pkgName, uint16_t subscribeId)
58 {
59     if (deviceDiscoveryCallbacks_.count(pkgName) > 0) {
60         deviceDiscoveryCallbacks_[pkgName].erase(subscribeId);
61         if (deviceDiscoveryCallbacks_[pkgName].empty()) {
62             deviceDiscoveryCallbacks_.erase(pkgName);
63         }
64     }
65 }
66 
RegisterAuthenticateCallback(const std::string & pkgName,const std::string & deviceId,std::shared_ptr<AuthenticateCallback> callback)67 void DeviceManagerNotify::RegisterAuthenticateCallback(const std::string &pkgName, const std::string &deviceId,
68                                                        std::shared_ptr<AuthenticateCallback> callback)
69 {
70     if (authenticateCallback_.count(pkgName) == 0) {
71         authenticateCallback_[pkgName] = std::map<std::string, std::shared_ptr<AuthenticateCallback>>();
72     }
73     authenticateCallback_[pkgName][deviceId] = callback;
74 }
75 
UnRegisterAuthenticateCallback(const std::string & pkgName,const std::string & deviceId)76 void DeviceManagerNotify::UnRegisterAuthenticateCallback(const std::string &pkgName, const std::string &deviceId)
77 {
78     if (authenticateCallback_.count(pkgName) > 0) {
79         authenticateCallback_[pkgName].erase(deviceId);
80         if (authenticateCallback_[pkgName].empty()) {
81             authenticateCallback_.erase(pkgName);
82         }
83     }
84 }
85 
UnRegisterPackageCallback(const std::string & pkgName)86 void DeviceManagerNotify::UnRegisterPackageCallback(const std::string &pkgName)
87 {
88     deviceStateCallback_.erase(pkgName);
89     deviceDiscoveryCallbacks_.erase(pkgName);
90     authenticateCallback_.erase(pkgName);
91     dmInitCallback_.erase(pkgName);
92 }
93 
RegisterVerifyAuthenticationCallback(const std::string & pkgName,const std::string & authPara,std::shared_ptr<VerifyAuthCallback> callback)94 void DeviceManagerNotify::RegisterVerifyAuthenticationCallback(const std::string &pkgName, const std::string &authPara,
95                                                                std::shared_ptr<VerifyAuthCallback> callback)
96 {
97     verifyAuthCallback_[pkgName] = callback;
98 }
99 
UnRegisterVerifyAuthenticationCallback(const std::string & pkgName)100 void DeviceManagerNotify::UnRegisterVerifyAuthenticationCallback(const std::string &pkgName)
101 {
102     verifyAuthCallback_.erase(pkgName);
103 }
104 
RegisterDeviceManagerFaCallback(const std::string & pkgName,std::shared_ptr<DeviceManagerUiCallback> callback)105 void DeviceManagerNotify::RegisterDeviceManagerFaCallback(const std::string &pkgName,
106                                                           std::shared_ptr<DeviceManagerUiCallback> callback)
107 {
108     dmUiCallback_[pkgName] = callback;
109 }
110 
UnRegisterDeviceManagerFaCallback(const std::string & pkgName)111 void DeviceManagerNotify::UnRegisterDeviceManagerFaCallback(const std::string &pkgName)
112 {
113     if (dmUiCallback_.count(pkgName) == 0) {
114         LOGE("DeviceManager UnRegisterDeviceManagerFaCallback not register");
115         return;
116     }
117     dmUiCallback_.erase(pkgName);
118 }
119 
120 
OnRemoteDied()121 void DeviceManagerNotify::OnRemoteDied()
122 {
123     LOGW("DeviceManager : OnRemoteDied");
124     for (auto iter : dmInitCallback_) {
125         iter.second->OnRemoteDied();
126     }
127 }
128 
OnDeviceOnline(const std::string & pkgName,const DmDeviceInfo & deviceInfo)129 void DeviceManagerNotify::OnDeviceOnline(const std::string &pkgName, const DmDeviceInfo &deviceInfo)
130 {
131     LOGI("DeviceManager OnDeviceOnline pkgName:%s", pkgName.c_str());
132     if (deviceStateCallback_.count(pkgName) == 0) {
133         LOGE("DeviceManager OnDeviceOnlinecallback not register");
134         return;
135     }
136     deviceStateCallback_[pkgName]->OnDeviceOnline(deviceInfo);
137 }
138 
OnDeviceOffline(const std::string & pkgName,const DmDeviceInfo & deviceInfo)139 void DeviceManagerNotify::OnDeviceOffline(const std::string &pkgName, const DmDeviceInfo &deviceInfo)
140 {
141     LOGI("DeviceManager OnDeviceOffline pkgName:%s", pkgName.c_str());
142 
143     if (deviceStateCallback_.count(pkgName) == 0) {
144         LOGE("DeviceManager OnDeviceOfflinecallback not register");
145         return;
146     }
147     deviceStateCallback_[pkgName]->OnDeviceOffline(deviceInfo);
148 }
149 
OnDeviceChanged(const std::string & pkgName,const DmDeviceInfo & deviceInfo)150 void DeviceManagerNotify::OnDeviceChanged(const std::string &pkgName, const DmDeviceInfo &deviceInfo)
151 {
152     LOGI("DeviceManager OnDeviceChanged pkgName:%s", pkgName.c_str());
153 
154     if (deviceStateCallback_.count(pkgName) == 0) {
155         LOGE("DeviceManager OnDeviceChangedcallback not register");
156         return;
157     }
158     deviceStateCallback_[pkgName]->OnDeviceChanged(deviceInfo);
159 }
160 
OnDeviceFound(const std::string & pkgName,uint16_t subscribeId,const DmDeviceInfo & deviceInfo)161 void DeviceManagerNotify::OnDeviceFound(const std::string &pkgName, uint16_t subscribeId,
162     const DmDeviceInfo &deviceInfo)
163 {
164     LOGI("DeviceManager OnDeviceFound pkgName:%s, subscribeId:%d.", pkgName.c_str(), (int32_t)subscribeId);
165     if (deviceDiscoveryCallbacks_.count(pkgName) == 0) {
166         LOGE("DeviceManager OnDeviceFound: no register DiscoveryCallback for this package");
167         return;
168     }
169     std::map<uint16_t, std::shared_ptr<DiscoveryCallback>> &discoverCallMap = deviceDiscoveryCallbacks_[pkgName];
170     auto iter = discoverCallMap.find(subscribeId);
171     if (iter == discoverCallMap.end()) {
172         LOGE("DeviceManager OnDeviceFound: no register DiscoveryCallback for subscribeId %d", subscribeId);
173         return;
174     }
175     iter->second->OnDeviceFound(subscribeId, deviceInfo);
176 }
177 
OnDiscoveryFailed(const std::string & pkgName,uint16_t subscribeId,int32_t failedReason)178 void DeviceManagerNotify::OnDiscoveryFailed(const std::string &pkgName, uint16_t subscribeId, int32_t failedReason)
179 {
180     LOGI("DeviceManager OnDiscoveryFailed pkgName:%s, subscribeId %d, reason %d", pkgName.c_str(), subscribeId,
181          failedReason);
182 
183     if (deviceDiscoveryCallbacks_.count(pkgName) == 0) {
184         LOGE("DeviceManager OnDiscoveryFailed: no register DiscoveryCallback for this package");
185         return;
186     }
187     std::map<uint16_t, std::shared_ptr<DiscoveryCallback>> &discoverCallMap = deviceDiscoveryCallbacks_[pkgName];
188     auto iter = discoverCallMap.find(subscribeId);
189     if (iter == discoverCallMap.end()) {
190         LOGE("DeviceManager OnDiscoveryFailed: no register DiscoveryCallback for subscribeId %d", subscribeId);
191         return;
192     }
193     iter->second->OnDiscoveryFailed(subscribeId, failedReason);
194 }
195 
OnDiscoverySuccess(const std::string & pkgName,uint16_t subscribeId)196 void DeviceManagerNotify::OnDiscoverySuccess(const std::string &pkgName, uint16_t subscribeId)
197 {
198     LOGI("DeviceManager OnDiscoverySuccess pkgName:%s, subscribeId:%d.", pkgName.c_str(), subscribeId);
199 
200     if (deviceDiscoveryCallbacks_.count(pkgName) == 0) {
201         LOGE("DeviceManager OnDiscoverySuccess: no register DiscoveryCallback for this package");
202         return;
203     }
204     std::map<uint16_t, std::shared_ptr<DiscoveryCallback>> &discoverCallMap = deviceDiscoveryCallbacks_[pkgName];
205     auto iter = discoverCallMap.find(subscribeId);
206     if (iter == discoverCallMap.end()) {
207         LOGE("DeviceManager OnDiscoverySuccess: no register DiscoveryCallback for subscribeId %d", subscribeId);
208         return;
209     }
210     iter->second->OnDiscoverySuccess(subscribeId);
211 }
212 
OnAuthResult(const std::string & pkgName,const std::string & deviceId,const std::string & token,uint32_t status,uint32_t reason)213 void DeviceManagerNotify::OnAuthResult(const std::string &pkgName, const std::string &deviceId,
214                                        const std::string &token, uint32_t status, uint32_t reason)
215 {
216     LOGI("DeviceManagerNotify::OnAuthResult pkgName:%s, status:%d, reason:%d", pkgName.c_str(), status, reason);
217 
218     if (authenticateCallback_.count(pkgName) == 0) {
219         LOGE("DeviceManager OnAuthResult: no register authCallback for this package");
220         return;
221     }
222     std::map<std::string, std::shared_ptr<AuthenticateCallback>> &authCallMap = authenticateCallback_[pkgName];
223     auto iter = authCallMap.find(deviceId);
224     if (iter == authCallMap.end()) {
225         LOGE("DeviceManager OnAuthResult: no register authCallback for deviceID ");
226         return;
227     }
228     iter->second->OnAuthResult(deviceId, token, status, reason);
229     authenticateCallback_[pkgName].erase(deviceId);
230     if (authenticateCallback_[pkgName].empty()) {
231         authenticateCallback_.erase(pkgName);
232     }
233 }
234 
OnVerifyAuthResult(const std::string & pkgName,const std::string & deviceId,int32_t resultCode,int32_t flag)235 void DeviceManagerNotify::OnVerifyAuthResult(const std::string &pkgName, const std::string &deviceId,
236                                              int32_t resultCode, int32_t flag)
237 {
238     LOGI("DeviceManagerNotify::OnCheckAuthResult pkgName:%s, resultCode:%d, flag:%d", pkgName.c_str(), resultCode,
239          flag);
240 
241     if (verifyAuthCallback_.count(pkgName) == 0) {
242         LOGE("DeviceManager OnCheckAuthResult: no register authCallback for this package");
243         return;
244     }
245     verifyAuthCallback_[pkgName]->OnVerifyAuthResult(deviceId, resultCode, flag);
246     verifyAuthCallback_.erase(pkgName);
247 }
248 
OnUiCall(std::string & pkgName,std::string & paramJson)249 void DeviceManagerNotify::OnUiCall(std::string &pkgName, std::string &paramJson)
250 {
251     LOGI("DeviceManager OnUiCallback pkgName:%s", pkgName.c_str());
252 
253     if (dmUiCallback_.count(pkgName) == 0) {
254         LOGE("DeviceManager dmUiCallback_ not register");
255         return;
256     }
257     dmUiCallback_[pkgName]->OnCall(paramJson);
258 }
259 } // namespace DistributedHardware
260 } // namespace OHOS
261