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_service_listener.h"
17 
18 #include "app_manager.h"
19 #include "device_manager_ipc_interface_code.h"
20 #include "dm_anonymous.h"
21 #include "dm_constants.h"
22 #include "dm_crypto.h"
23 #include "dm_log.h"
24 #include "dm_softbus_cache.h"
25 #include "ipc_create_pin_holder_req.h"
26 #include "ipc_credential_auth_status_req.h"
27 #include "ipc_destroy_pin_holder_req.h"
28 #include "ipc_notify_auth_result_req.h"
29 #include "ipc_notify_bind_result_req.h"
30 #include "ipc_notify_credential_req.h"
31 #include "ipc_notify_device_found_req.h"
32 #include "ipc_notify_device_discovery_req.h"
33 #include "ipc_notify_device_state_req.h"
34 #include "ipc_notify_discover_result_req.h"
35 #include "ipc_notify_pin_holder_event_req.h"
36 #include "ipc_notify_publish_result_req.h"
37 #include "ipc_server_stub.h"
38 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
39 #include "datetime_ex.h"
40 #include "kv_adapter_manager.h"
41 #endif
42 #include "parameter.h"
43 #include "permission_manager.h"
44 
45 namespace OHOS {
46 namespace DistributedHardware {
47 std::mutex DeviceManagerServiceListener::alreadyOnlineSetLock_;
48 std::unordered_set<std::string> DeviceManagerServiceListener::alreadyOnlineSet_ = {};
49 const int32_t LAST_APP_ONLINE_NUMS = 8;
ConvertDeviceInfoToDeviceBasicInfo(const std::string & pkgName,const DmDeviceInfo & info,DmDeviceBasicInfo & deviceBasicInfo)50 void DeviceManagerServiceListener::ConvertDeviceInfoToDeviceBasicInfo(const std::string &pkgName,
51     const DmDeviceInfo &info, DmDeviceBasicInfo &deviceBasicInfo)
52 {
53     (void)memset_s(&deviceBasicInfo, sizeof(DmDeviceBasicInfo), 0, sizeof(DmDeviceBasicInfo));
54     if (memcpy_s(deviceBasicInfo.deviceName, sizeof(deviceBasicInfo.deviceName), info.deviceName,
55                  std::min(sizeof(deviceBasicInfo.deviceName), sizeof(info.deviceName))) != DM_OK) {
56         LOGE("ConvertDeviceInfoToDmDevice copy deviceName data failed.");
57     }
58 
59     if (memcpy_s(deviceBasicInfo.networkId, sizeof(deviceBasicInfo.networkId), info.networkId,
60         std::min(sizeof(deviceBasicInfo.networkId), sizeof(info.networkId))) != DM_OK) {
61         LOGE("ConvertNodeBasicInfoToDmDevice copy networkId data failed.");
62     }
63     if (memcpy_s(deviceBasicInfo.deviceId, sizeof(deviceBasicInfo.deviceId), info.deviceId,
64         std::min(sizeof(deviceBasicInfo.deviceId), sizeof(info.deviceId))) != DM_OK) {
65         LOGE("ConvertNodeBasicInfoToDmDevice copy deviceId data failed.");
66     }
67     deviceBasicInfo.deviceTypeId = info.deviceTypeId;
68 }
69 
SetDeviceInfo(std::shared_ptr<IpcNotifyDeviceStateReq> pReq,const std::string & pkgName,const DmDeviceState & state,const DmDeviceInfo & deviceInfo,const DmDeviceBasicInfo & deviceBasicInfo)70 void DeviceManagerServiceListener::SetDeviceInfo(std::shared_ptr<IpcNotifyDeviceStateReq> pReq,
71     const std::string &pkgName, const DmDeviceState &state, const DmDeviceInfo &deviceInfo,
72     const DmDeviceBasicInfo &deviceBasicInfo)
73 {
74     LOGD("DeviceManagerServiceListener::SetDeviceInfo");
75     pReq->SetPkgName(pkgName);
76     pReq->SetDeviceState(state);
77 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
78     std::string appId = "";
79     if (AppManager::GetInstance().GetAppIdByPkgName(pkgName, appId) != DM_OK) {
80         pReq->SetDeviceInfo(deviceInfo);
81         pReq->SetDeviceBasicInfo(deviceBasicInfo);
82         return;
83     }
84     DmDeviceInfo dmDeviceInfo = deviceInfo;
85     ConvertUdidHashToAnoyAndSave(pkgName, dmDeviceInfo);
86     DmDeviceBasicInfo dmDeviceBasicInfo = deviceBasicInfo;
87     (void)memset_s(dmDeviceBasicInfo.deviceId, DM_MAX_DEVICE_ID_LEN, 0, DM_MAX_DEVICE_ID_LEN);
88     if (memcpy_s(dmDeviceBasicInfo.deviceId, sizeof(dmDeviceBasicInfo.deviceId), dmDeviceInfo.deviceId,
89         std::min(sizeof(dmDeviceBasicInfo.deviceId), sizeof(dmDeviceInfo.deviceId))) != DM_OK) {
90         LOGE("ConvertNodeBasicInfoToDmDevice copy deviceId data failed.");
91     }
92     pReq->SetDeviceInfo(dmDeviceInfo);
93     pReq->SetDeviceBasicInfo(dmDeviceBasicInfo);
94     return;
95 #endif
96     pReq->SetDeviceInfo(deviceInfo);
97     pReq->SetDeviceBasicInfo(deviceBasicInfo);
98 }
99 
ComposeOnlineKey(const std::string & pkgName,const std::string & devId)100 std::string DeviceManagerServiceListener::ComposeOnlineKey(const std::string &pkgName, const std::string &devId)
101 {
102     return pkgName + "_" + devId;
103 }
104 
ProcessDeviceStateChange(const DmDeviceState & state,const DmDeviceInfo & info,const DmDeviceBasicInfo & deviceBasicInfo)105 void DeviceManagerServiceListener::ProcessDeviceStateChange(const DmDeviceState &state, const DmDeviceInfo &info,
106     const DmDeviceBasicInfo &deviceBasicInfo)
107 {
108     LOGI("In");
109     std::shared_ptr<IpcNotifyDeviceStateReq> pReq = std::make_shared<IpcNotifyDeviceStateReq>();
110     std::shared_ptr<IpcRsp> pRsp = std::make_shared<IpcRsp>();
111     std::vector<std::string> PkgNameVec = ipcServerListener_.GetAllPkgName();
112     if (state == DEVICE_STATE_OFFLINE) {
113         for (const auto &it : PkgNameVec) {
114             std::string notifyKey = ComposeOnlineKey(it, std::string(info.deviceId));
115             {
116                 std::lock_guard<std::mutex> autoLock(alreadyOnlineSetLock_);
117                 alreadyOnlineSet_.erase(notifyKey);
118             }
119             SetDeviceInfo(pReq, it, state, info, deviceBasicInfo);
120             ipcServerListener_.SendRequest(SERVER_DEVICE_STATE_NOTIFY, pReq, pRsp);
121         }
122     }
123     if (state == DEVICE_STATE_ONLINE) {
124         for (const auto &it : PkgNameVec) {
125             std::string notifyKey = ComposeOnlineKey(it, std::string(info.deviceId));
126             DmDeviceState notifyState = state;
127             {
128                 std::lock_guard<std::mutex> autoLock(alreadyOnlineSetLock_);
129                 if (alreadyOnlineSet_.find(notifyKey) != alreadyOnlineSet_.end()) {
130                     notifyState = DmDeviceState::DEVICE_INFO_CHANGED;
131                 } else {
132                     alreadyOnlineSet_.insert(notifyKey);
133                 }
134             }
135             SetDeviceInfo(pReq, it, notifyState, info, deviceBasicInfo);
136             ipcServerListener_.SendRequest(SERVER_DEVICE_STATE_NOTIFY, pReq, pRsp);
137         }
138     }
139     if (state == DEVICE_INFO_READY || state == DEVICE_INFO_CHANGED) {
140         for (const auto &it : PkgNameVec) {
141             SetDeviceInfo(pReq, it, state, info, deviceBasicInfo);
142             ipcServerListener_.SendRequest(SERVER_DEVICE_STATE_NOTIFY, pReq, pRsp);
143         }
144     }
145 }
146 
ProcessAppStateChange(const std::string & pkgName,const DmDeviceState & state,const DmDeviceInfo & info,const DmDeviceBasicInfo & deviceBasicInfo)147 void DeviceManagerServiceListener::ProcessAppStateChange(const std::string &pkgName, const DmDeviceState &state,
148     const DmDeviceInfo &info, const DmDeviceBasicInfo &deviceBasicInfo)
149 {
150     LOGI("In");
151     std::shared_ptr<IpcNotifyDeviceStateReq> pReq = std::make_shared<IpcNotifyDeviceStateReq>();
152     std::shared_ptr<IpcRsp> pRsp = std::make_shared<IpcRsp>();
153     std::unordered_set<std::string> notifyPkgnames = PermissionManager::GetInstance().GetSystemSA();
154     notifyPkgnames.insert(pkgName);
155     if (state == DEVICE_STATE_ONLINE) {
156         for (const auto &it : notifyPkgnames) {
157             std::string notifyKey =  it + "_" + info.deviceId;
158             {
159                 std::lock_guard<std::mutex> autoLock(alreadyOnlineSetLock_);
160                 if (alreadyOnlineSet_.find(notifyKey) != alreadyOnlineSet_.end()) {
161                     continue;
162                 }
163                 alreadyOnlineSet_.insert(notifyKey);
164             }
165             SetDeviceInfo(pReq, it, state, info, deviceBasicInfo);
166             ipcServerListener_.SendRequest(SERVER_DEVICE_STATE_NOTIFY, pReq, pRsp);
167         }
168     }
169     if (state == DEVICE_STATE_OFFLINE) {
170         if (alreadyOnlineSet_.size() == LAST_APP_ONLINE_NUMS) {
171             {
172                 std::lock_guard<std::mutex> autoLock(alreadyOnlineSetLock_);
173                 alreadyOnlineSet_.clear();
174             }
175             for (const auto &it : notifyPkgnames) {
176                 SetDeviceInfo(pReq, it, state, info, deviceBasicInfo);
177                 ipcServerListener_.SendRequest(SERVER_DEVICE_STATE_NOTIFY, pReq, pRsp);
178             }
179         } else {
180             std::string notifyKey =  pkgName + "_" + info.deviceId;
181             {
182                 std::lock_guard<std::mutex> autoLock(alreadyOnlineSetLock_);
183                 if (alreadyOnlineSet_.find(notifyKey) != alreadyOnlineSet_.end()) {
184                     alreadyOnlineSet_.erase(notifyKey);
185                 }
186             }
187             SetDeviceInfo(pReq, pkgName, state, info, deviceBasicInfo);
188             ipcServerListener_.SendRequest(SERVER_DEVICE_STATE_NOTIFY, pReq, pRsp);
189         }
190     }
191     if (state == DEVICE_INFO_READY || state == DEVICE_INFO_CHANGED) {
192         SetDeviceInfo(pReq, pkgName, state, info, deviceBasicInfo);
193         ipcServerListener_.SendRequest(SERVER_DEVICE_STATE_NOTIFY, pReq, pRsp);
194     }
195 }
196 
OnDeviceStateChange(const std::string & pkgName,const DmDeviceState & state,const DmDeviceInfo & info)197 void DeviceManagerServiceListener::OnDeviceStateChange(const std::string &pkgName, const DmDeviceState &state,
198                                                        const DmDeviceInfo &info)
199 {
200     LOGI("OnDeviceStateChange, state = %{public}d", state);
201     DmDeviceBasicInfo deviceBasicInfo;
202     ConvertDeviceInfoToDeviceBasicInfo(pkgName, info, deviceBasicInfo);
203     if (pkgName == std::string(DM_PKG_NAME)) {
204         ProcessDeviceStateChange(state, info, deviceBasicInfo);
205     } else {
206         ProcessAppStateChange(pkgName, state, info, deviceBasicInfo);
207     }
208 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
209     KVAdapterManager::GetInstance().DeleteAgedEntry();
210 #endif
211 }
212 
OnDeviceFound(const std::string & pkgName,uint16_t subscribeId,const DmDeviceInfo & info)213 void DeviceManagerServiceListener::OnDeviceFound(const std::string &pkgName, uint16_t subscribeId,
214                                                  const DmDeviceInfo &info)
215 {
216     std::shared_ptr<IpcNotifyDeviceFoundReq> pReq = std::make_shared<IpcNotifyDeviceFoundReq>();
217     std::shared_ptr<IpcRsp> pRsp = std::make_shared<IpcRsp>();
218     DmDeviceInfo deviceInfo = info;
219 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
220     ConvertUdidHashToAnoyAndSave(pkgName, deviceInfo);
221 #endif
222     DmDeviceBasicInfo devBasicInfo;
223     ConvertDeviceInfoToDeviceBasicInfo(pkgName, deviceInfo, devBasicInfo);
224     pReq->SetDeviceBasicInfo(devBasicInfo);
225     pReq->SetPkgName(pkgName);
226     pReq->SetSubscribeId(subscribeId);
227     pReq->SetDeviceInfo(deviceInfo);
228     ipcServerListener_.SendRequest(SERVER_DEVICE_FOUND, pReq, pRsp);
229 }
230 
OnDeviceFound(const std::string & pkgName,uint16_t subscribeId,DmDeviceBasicInfo & info)231 void DeviceManagerServiceListener::OnDeviceFound(const std::string &pkgName, uint16_t subscribeId,
232                                                  DmDeviceBasicInfo &info)
233 {
234     (void)pkgName;
235     (void)subscribeId;
236     (void)info;
237 }
238 
OnDiscoveryFailed(const std::string & pkgName,uint16_t subscribeId,int32_t failedReason)239 void DeviceManagerServiceListener::OnDiscoveryFailed(const std::string &pkgName, uint16_t subscribeId,
240                                                      int32_t failedReason)
241 {
242     LOGI("DeviceManagerServiceListener::OnDiscoveryFailed");
243     std::shared_ptr<IpcNotifyDiscoverResultReq> pReq = std::make_shared<IpcNotifyDiscoverResultReq>();
244     std::shared_ptr<IpcRsp> pRsp = std::make_shared<IpcRsp>();
245 
246     pReq->SetPkgName(pkgName);
247     pReq->SetSubscribeId(subscribeId);
248     pReq->SetResult(failedReason);
249     ipcServerListener_.SendRequest(SERVER_DISCOVER_FINISH, pReq, pRsp);
250 }
251 
OnDiscoverySuccess(const std::string & pkgName,int32_t subscribeId)252 void DeviceManagerServiceListener::OnDiscoverySuccess(const std::string &pkgName, int32_t subscribeId)
253 {
254     LOGI("DeviceManagerServiceListener::OnDiscoverySuccess");
255     std::shared_ptr<IpcNotifyDiscoverResultReq> pReq = std::make_shared<IpcNotifyDiscoverResultReq>();
256     std::shared_ptr<IpcRsp> pRsp = std::make_shared<IpcRsp>();
257 
258     pReq->SetPkgName(pkgName);
259     pReq->SetSubscribeId((uint16_t)subscribeId);
260     pReq->SetResult(DM_OK);
261     ipcServerListener_.SendRequest(SERVER_DISCOVER_FINISH, pReq, pRsp);
262 }
263 
OnPublishResult(const std::string & pkgName,int32_t publishId,int32_t publishResult)264 void DeviceManagerServiceListener::OnPublishResult(const std::string &pkgName, int32_t publishId, int32_t publishResult)
265 {
266     LOGI("DeviceManagerServiceListener::OnPublishResult : %{public}d", publishResult);
267     std::shared_ptr<IpcNotifyPublishResultReq> pReq = std::make_shared<IpcNotifyPublishResultReq>();
268     std::shared_ptr<IpcRsp> pRsp = std::make_shared<IpcRsp>();
269 
270     pReq->SetPkgName(pkgName);
271     pReq->SetPublishId(publishId);
272     pReq->SetResult(publishResult);
273     ipcServerListener_.SendRequest(SERVER_PUBLISH_FINISH, pReq, pRsp);
274 }
275 
OnAuthResult(const std::string & pkgName,const std::string & deviceId,const std::string & token,int32_t status,int32_t reason)276 void DeviceManagerServiceListener::OnAuthResult(const std::string &pkgName, const std::string &deviceId,
277                                                 const std::string &token, int32_t status, int32_t reason)
278 {
279     std::shared_ptr<IpcNotifyAuthResultReq> pReq = std::make_shared<IpcNotifyAuthResultReq>();
280     std::shared_ptr<IpcRsp> pRsp = std::make_shared<IpcRsp>();
281     if (status < STATUS_DM_AUTH_FINISH && status > STATUS_DM_AUTH_DEFAULT) {
282         status = STATUS_DM_AUTH_DEFAULT;
283     }
284     pReq->SetDeviceId(deviceId);
285 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
286     std::string deviceIdTemp = "";
287     if (ConvertUdidHashToAnoyDeviceId(pkgName, deviceId, deviceIdTemp) == DM_OK) {
288         pReq->SetDeviceId(deviceIdTemp);
289     }
290 #endif
291     pReq->SetPkgName(pkgName);
292     pReq->SetToken(token);
293     pReq->SetStatus(status);
294     pReq->SetReason(reason);
295     ipcServerListener_.SendRequest(SERVER_AUTH_RESULT, pReq, pRsp);
296 }
297 
OnUiCall(std::string & pkgName,std::string & paramJson)298 void DeviceManagerServiceListener::OnUiCall(std::string &pkgName, std::string &paramJson)
299 {
300     LOGI("OnUiCall in");
301     std::shared_ptr<IpcNotifyDMFAResultReq> pReq = std::make_shared<IpcNotifyDMFAResultReq>();
302     std::shared_ptr<IpcRsp> pRsp = std::make_shared<IpcRsp>();
303 
304     pReq->SetPkgName(pkgName);
305     pReq->SetJsonParam(paramJson);
306     ipcServerListener_.SendRequest(SERVER_DEVICE_FA_NOTIFY, pReq, pRsp);
307 }
308 
OnCredentialResult(const std::string & pkgName,int32_t action,const std::string & resultInfo)309 void DeviceManagerServiceListener::OnCredentialResult(const std::string &pkgName, int32_t action,
310     const std::string &resultInfo)
311 {
312     LOGI("call OnCredentialResult for %{public}s, action %{public}d", pkgName.c_str(), action);
313     std::shared_ptr<IpcNotifyCredentialReq> pReq = std::make_shared<IpcNotifyCredentialReq>();
314     std::shared_ptr<IpcRsp> pRsp = std::make_shared<IpcRsp>();
315 
316     pReq->SetPkgName(pkgName);
317     pReq->SetCredentialAction(action);
318     pReq->SetCredentialResult(resultInfo);
319     ipcServerListener_.SendRequest(SERVER_CREDENTIAL_RESULT, pReq, pRsp);
320 }
321 
OnBindResult(const std::string & pkgName,const PeerTargetId & targetId,int32_t result,int32_t status,std::string content)322 void DeviceManagerServiceListener::OnBindResult(const std::string &pkgName, const PeerTargetId &targetId,
323     int32_t result, int32_t status, std::string content)
324 {
325     std::shared_ptr<IpcNotifyBindResultReq> pReq = std::make_shared<IpcNotifyBindResultReq>();
326     std::shared_ptr<IpcRsp> pRsp = std::make_shared<IpcRsp>();
327     if (status < STATUS_DM_AUTH_FINISH && status > STATUS_DM_AUTH_DEFAULT) {
328         status = STATUS_DM_AUTH_DEFAULT;
329     }
330     PeerTargetId returnTargetId = targetId;
331 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
332     std::string deviceIdTemp = "";
333     DmKVValue kvValue;
334     if (ConvertUdidHashToAnoyDeviceId(pkgName, targetId.deviceId, deviceIdTemp) == DM_OK &&
335         KVAdapterManager::GetInstance().Get(deviceIdTemp, kvValue) == DM_OK) {
336         returnTargetId.deviceId = deviceIdTemp;
337     }
338 #endif
339     pReq->SetPkgName(pkgName);
340     pReq->SetPeerTargetId(returnTargetId);
341     pReq->SetResult(result);
342     pReq->SetStatus(status);
343     pReq->SetContent(content);
344     ipcServerListener_.SendRequest(BIND_TARGET_RESULT, pReq, pRsp);
345 }
346 
OnUnbindResult(const std::string & pkgName,const PeerTargetId & targetId,int32_t result,std::string content)347 void DeviceManagerServiceListener::OnUnbindResult(const std::string &pkgName, const PeerTargetId &targetId,
348     int32_t result, std::string content)
349 {
350     std::shared_ptr<IpcNotifyBindResultReq> pReq = std::make_shared<IpcNotifyBindResultReq>();
351     std::shared_ptr<IpcRsp> pRsp = std::make_shared<IpcRsp>();
352     PeerTargetId returnTargetId = targetId;
353 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
354     std::string deviceIdTemp = "";
355     DmKVValue kvValue;
356     if (ConvertUdidHashToAnoyDeviceId(pkgName, targetId.deviceId, deviceIdTemp) == DM_OK &&
357         KVAdapterManager::GetInstance().Get(deviceIdTemp, kvValue) == DM_OK) {
358         returnTargetId.deviceId = deviceIdTemp;
359     }
360 #endif
361     pReq->SetPkgName(pkgName);
362     pReq->SetPeerTargetId(returnTargetId);
363     pReq->SetResult(result);
364     pReq->SetContent(content);
365     ipcServerListener_.SendRequest(UNBIND_TARGET_RESULT, pReq, pRsp);
366 }
367 
OnPinHolderCreate(const std::string & pkgName,const std::string & deviceId,DmPinType pinType,const std::string & payload)368 void DeviceManagerServiceListener::OnPinHolderCreate(const std::string &pkgName, const std::string &deviceId,
369     DmPinType pinType, const std::string &payload)
370 {
371     LOGI("DeviceManagerServiceListener::OnPinHolderCreate : %{public}s", pkgName.c_str());
372     std::shared_ptr<IpcCreatePinHolderReq> pReq = std::make_shared<IpcCreatePinHolderReq>();
373     std::shared_ptr<IpcRsp> pRsp = std::make_shared<IpcRsp>();
374 
375     pReq->SetPkgName(pkgName);
376     pReq->SetDeviceId(deviceId);
377     pReq->SetPinType(pinType);
378     pReq->SetPayload(payload);
379     ipcServerListener_.SendRequest(SERVER_CREATE_PIN_HOLDER, pReq, pRsp);
380 }
381 
OnPinHolderDestroy(const std::string & pkgName,DmPinType pinType,const std::string & payload)382 void DeviceManagerServiceListener::OnPinHolderDestroy(const std::string &pkgName, DmPinType pinType,
383     const std::string &payload)
384 {
385     LOGI("DeviceManagerServiceListener::OnPinHolderDestroy : %{public}s", pkgName.c_str());
386     std::shared_ptr<IpcDestroyPinHolderReq> pReq = std::make_shared<IpcDestroyPinHolderReq>();
387     std::shared_ptr<IpcRsp> pRsp = std::make_shared<IpcRsp>();
388 
389     pReq->SetPkgName(pkgName);
390     pReq->SetPinType(pinType);
391     pReq->SetPayload(payload);
392     ipcServerListener_.SendRequest(SERVER_DESTROY_PIN_HOLDER, pReq, pRsp);
393 }
394 
OnCreateResult(const std::string & pkgName,int32_t result)395 void DeviceManagerServiceListener::OnCreateResult(const std::string &pkgName, int32_t result)
396 {
397     LOGI("DeviceManagerServiceListener::OnCreateResult : %{public}d", result);
398     std::shared_ptr<IpcNotifyPublishResultReq> pReq = std::make_shared<IpcNotifyPublishResultReq>();
399     std::shared_ptr<IpcRsp> pRsp = std::make_shared<IpcRsp>();
400 
401     pReq->SetPkgName(pkgName);
402     pReq->SetResult(result);
403     ipcServerListener_.SendRequest(SERVER_CREATE_PIN_HOLDER_RESULT, pReq, pRsp);
404 }
405 
OnDestroyResult(const std::string & pkgName,int32_t result)406 void DeviceManagerServiceListener::OnDestroyResult(const std::string &pkgName, int32_t result)
407 {
408     LOGI("DeviceManagerServiceListener::OnDestroyResult : %{public}d", result);
409     std::shared_ptr<IpcNotifyPublishResultReq> pReq = std::make_shared<IpcNotifyPublishResultReq>();
410     std::shared_ptr<IpcRsp> pRsp = std::make_shared<IpcRsp>();
411 
412     pReq->SetPkgName(pkgName);
413     pReq->SetResult(result);
414     ipcServerListener_.SendRequest(SERVER_DESTROY_PIN_HOLDER_RESULT, pReq, pRsp);
415 }
416 
OnPinHolderEvent(const std::string & pkgName,DmPinHolderEvent event,int32_t result,const std::string & content)417 void DeviceManagerServiceListener::OnPinHolderEvent(const std::string &pkgName, DmPinHolderEvent event,
418     int32_t result, const std::string &content)
419 {
420     LOGI("OnPinHolderEvent pkgName: %{public}s, event: %{public}d, result: %{public}d",
421         pkgName.c_str(), event, result);
422     std::shared_ptr<IpcNotifyPinHolderEventReq> pReq = std::make_shared<IpcNotifyPinHolderEventReq>();
423     std::shared_ptr<IpcRsp> pRsp = std::make_shared<IpcRsp>();
424 
425     pReq->SetPkgName(pkgName);
426     pReq->SetPinHolderEvent(event);
427     pReq->SetResult(result);
428     pReq->SetContent(content);
429     ipcServerListener_.SendRequest(SERVER_ON_PIN_HOLDER_EVENT, pReq, pRsp);
430 }
431 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
ConvertUdidHashToAnoyAndSave(const std::string & pkgName,DmDeviceInfo & deviceInfo)432 int32_t DeviceManagerServiceListener::ConvertUdidHashToAnoyAndSave(const std::string &pkgName, DmDeviceInfo &deviceInfo)
433 {
434     LOGI("pkgName %{public}s.", pkgName.c_str());
435     std::string appId = "";
436     if (AppManager::GetInstance().GetAppIdByPkgName(pkgName, appId) != DM_OK) {
437         LOGD("GetAppIdByPkgName failed");
438         return ERR_DM_FAILED;
439     }
440     DmKVValue kvValue;
441     int32_t ret = Crypto::ConvertUdidHashToAnoyAndSave(appId, std::string(deviceInfo.deviceId), kvValue);
442     if (ret != DM_OK) {
443         return ERR_DM_FAILED;
444     }
445     (void)memset_s(deviceInfo.deviceId, DM_MAX_DEVICE_ID_LEN, 0, DM_MAX_DEVICE_ID_LEN);
446     if (memcpy_s(deviceInfo.deviceId, sizeof(deviceInfo.deviceId), kvValue.anoyDeviceId.c_str(),
447         std::min(sizeof(deviceInfo.deviceId), kvValue.anoyDeviceId.length())) != DM_OK) {
448         LOGE("copy deviceId data failed.");
449         return ERR_DM_FAILED;
450     }
451     return DM_OK;
452 }
453 
ConvertUdidHashToAnoyDeviceId(const std::string & pkgName,const std::string & udidHash,std::string & anoyDeviceId)454 int32_t DeviceManagerServiceListener::ConvertUdidHashToAnoyDeviceId(const std::string &pkgName,
455     const std::string &udidHash, std::string &anoyDeviceId)
456 {
457     LOGI("pkgName %{public}s, udidHash %{public}s.", pkgName.c_str(), GetAnonyString(udidHash).c_str());
458     std::string appId = "";
459     if (AppManager::GetInstance().GetAppIdByPkgName(pkgName, appId) != DM_OK) {
460         LOGD("GetAppIdByPkgName failed");
461         return ERR_DM_FAILED;
462     }
463     DmKVValue kvValue;
464     int32_t ret = Crypto::ConvertUdidHashToAnoyDeviceId(appId, udidHash, kvValue);
465     if (ret == DM_OK) {
466         anoyDeviceId = kvValue.anoyDeviceId;
467     }
468     return ret;
469 }
470 #endif
471 
SetDeviceScreenInfo(std::shared_ptr<IpcNotifyDeviceStateReq> pReq,const std::string & pkgName,const DmDeviceInfo & deviceInfo)472 void DeviceManagerServiceListener::SetDeviceScreenInfo(std::shared_ptr<IpcNotifyDeviceStateReq> pReq,
473     const std::string &pkgName, const DmDeviceInfo &deviceInfo)
474 {
475     LOGI("In");
476     pReq->SetPkgName(pkgName);
477 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
478     std::string appId = "";
479     if (AppManager::GetInstance().GetAppIdByPkgName(pkgName, appId) != DM_OK) {
480         pReq->SetDeviceInfo(deviceInfo);
481         return;
482     }
483     DmDeviceInfo dmDeviceInfo = deviceInfo;
484     ConvertUdidHashToAnoyAndSave(pkgName, dmDeviceInfo);
485     pReq->SetDeviceInfo(dmDeviceInfo);
486     return;
487 #endif
488     pReq->SetDeviceInfo(deviceInfo);
489 }
490 
OnDeviceScreenStateChange(const std::string & pkgName,DmDeviceInfo & devInfo)491 void DeviceManagerServiceListener::OnDeviceScreenStateChange(const std::string &pkgName, DmDeviceInfo &devInfo)
492 {
493     LOGI("In, pkgName = %{public}s", pkgName.c_str());
494     if (pkgName == std::string(DM_PKG_NAME)) {
495         std::shared_ptr<IpcNotifyDeviceStateReq> pReq = std::make_shared<IpcNotifyDeviceStateReq>();
496         std::shared_ptr<IpcRsp> pRsp = std::make_shared<IpcRsp>();
497         std::vector<std::string> PkgNameVec = ipcServerListener_.GetAllPkgName();
498         for (const auto &it : PkgNameVec) {
499             SetDeviceScreenInfo(pReq, it, devInfo);
500             ipcServerListener_.SendRequest(SERVER_DEVICE_SCREEN_STATE_NOTIFY, pReq, pRsp);
501         }
502     } else {
503         std::shared_ptr<IpcNotifyDeviceStateReq> pReq = std::make_shared<IpcNotifyDeviceStateReq>();
504         std::shared_ptr<IpcRsp> pRsp = std::make_shared<IpcRsp>();
505         std::unordered_set<std::string> notifyPkgnames = PermissionManager::GetInstance().GetSystemSA();
506         notifyPkgnames.insert(pkgName);
507         for (const auto &it : notifyPkgnames) {
508             SetDeviceScreenInfo(pReq, it, devInfo);
509             ipcServerListener_.SendRequest(SERVER_DEVICE_SCREEN_STATE_NOTIFY, pReq, pRsp);
510         }
511     }
512 }
513 
OnCredentialAuthStatus(const std::string & pkgName,const std::string & proofInfo,uint16_t deviceTypeId,int32_t errcode)514 void DeviceManagerServiceListener::OnCredentialAuthStatus(const std::string &pkgName,
515     const std::string &proofInfo, uint16_t deviceTypeId, int32_t errcode)
516 {
517     LOGI("In, pkgName = %{public}s", pkgName.c_str());
518     std::shared_ptr<IpcNotifyCredentialAuthStatusReq> pReq =
519         std::make_shared<IpcNotifyCredentialAuthStatusReq>();
520     std::shared_ptr<IpcRsp> pRsp = std::make_shared<IpcRsp>();
521     pReq->SetProofInfo(proofInfo);
522     pReq->SetDeviceTypeId(deviceTypeId);
523     pReq->SetErrCode(errcode);
524     if (pkgName == std::string(DM_PKG_NAME)) {
525         std::vector<std::string> PkgNameVec = ipcServerListener_.GetAllPkgName();
526         for (const auto &it : PkgNameVec) {
527             pReq->SetPkgName(it);
528             ipcServerListener_.SendRequest(SERVICE_CREDENTIAL_AUTH_STATUS_NOTIFY, pReq, pRsp);
529         }
530     } else {
531         pReq->SetPkgName(pkgName);
532         ipcServerListener_.SendRequest(SERVICE_CREDENTIAL_AUTH_STATUS_NOTIFY, pReq, pRsp);
533     }
534 }
535 } // namespace DistributedHardware
536 } // namespace OHOS
537