• 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 <set>
17 #include "cJSON.h"
18 
19 #include "device_manager_service_listener.h"
20 
21 #include "app_manager.h"
22 #include "device_manager_ipc_interface_code.h"
23 #include "device_manager_service_notify.h"
24 #include "dm_anonymous.h"
25 #include "dm_constants.h"
26 #include "dm_crypto.h"
27 #include "dm_log.h"
28 #include "dm_softbus_cache.h"
29 #include "ipc_create_pin_holder_req.h"
30 #include "ipc_credential_auth_status_req.h"
31 #include "ipc_destroy_pin_holder_req.h"
32 #include "ipc_notify_auth_result_req.h"
33 #include "ipc_notify_bind_result_req.h"
34 #include "ipc_notify_credential_req.h"
35 #include "ipc_notify_devicetrustchange_req.h"
36 #include "ipc_notify_device_found_req.h"
37 #include "ipc_notify_discover_result_req.h"
38 #include "ipc_notify_get_device_icon_info_req.h"
39 #include "ipc_notify_get_device_profile_info_list_req.h"
40 #include "ipc_notify_pin_holder_event_req.h"
41 #include "ipc_notify_publish_result_req.h"
42 #include "ipc_notify_set_local_device_name_req.h"
43 #include "ipc_notify_set_remote_device_name_req.h"
44 #include "ipc_server_stub.h"
45 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
46 #include "datetime_ex.h"
47 #include "device_name_manager.h"
48 #include "device_manager_service.h"
49 #include "kv_adapter_manager.h"
50 #include "multiple_user_connector.h"
51 #endif
52 #include "parameter.h"
53 #include "permission_manager.h"
54 
55 namespace OHOS {
56 namespace DistributedHardware {
57 std::mutex DeviceManagerServiceListener::alreadyNotifyPkgNameLock_;
58 std::map<std::string, DmDeviceInfo> DeviceManagerServiceListener::alreadyOnlinePkgName_ = {};
59 std::mutex DeviceManagerServiceListener::actUnrelatedPkgNameLock_;
60 std::set<std::string> DeviceManagerServiceListener::actUnrelatedPkgName_ = {};
61 std::unordered_set<std::string> DeviceManagerServiceListener::highPriorityPkgNameSet_ = { "ohos.deviceprofile",
62     "ohos.distributeddata.service" };
63 
handleExtraData(const DmDeviceInfo & info,DmDeviceBasicInfo & deviceBasicInfo)64 void handleExtraData(const DmDeviceInfo &info, DmDeviceBasicInfo &deviceBasicInfo)
65 {
66     cJSON *extraDataJsonObj = cJSON_Parse(info.extraData.c_str());
67     if (extraDataJsonObj == NULL) {
68         return;
69     }
70     cJSON *customDataJson = cJSON_GetObjectItem(extraDataJsonObj, PARAM_KEY_CUSTOM_DATA);
71     if (customDataJson == NULL || !cJSON_IsString(customDataJson)) {
72         cJSON_Delete(extraDataJsonObj);
73         return;
74     }
75     char *customData = cJSON_PrintUnformatted(customDataJson);
76     if (customData == nullptr) {
77         cJSON_Delete(extraDataJsonObj);
78         return;
79     }
80     cJSON_Delete(extraDataJsonObj);
81     cJSON *basicExtraDataJsonObj = cJSON_CreateObject();
82     if (basicExtraDataJsonObj == NULL) {
83         cJSON_free(customData);
84         return;
85     }
86     cJSON_AddStringToObject(basicExtraDataJsonObj, PARAM_KEY_CUSTOM_DATA, customData);
87     char *basicExtraData = cJSON_PrintUnformatted(basicExtraDataJsonObj);
88     if (basicExtraData == nullptr) {
89         cJSON_free(customData);
90         cJSON_Delete(basicExtraDataJsonObj);
91         return;
92     }
93     deviceBasicInfo.extraData = std::string(basicExtraData);
94     cJSON_free(customData);
95     cJSON_free(basicExtraData);
96     cJSON_Delete(basicExtraDataJsonObj);
97 }
98 
ConvertDeviceInfoToDeviceBasicInfo(const std::string & pkgName,const DmDeviceInfo & info,DmDeviceBasicInfo & deviceBasicInfo)99 void DeviceManagerServiceListener::ConvertDeviceInfoToDeviceBasicInfo(const std::string &pkgName,
100     const DmDeviceInfo &info, DmDeviceBasicInfo &deviceBasicInfo)
101 {
102     (void)pkgName;
103     if (memset_s(&deviceBasicInfo, sizeof(DmDeviceBasicInfo), 0, sizeof(DmDeviceBasicInfo)) != DM_OK) {
104         LOGE("ConvertDeviceInfoToDeviceBasicInfo memset_s failed.");
105         return;
106     }
107     if (memcpy_s(deviceBasicInfo.deviceName, sizeof(deviceBasicInfo.deviceName), info.deviceName,
108                  std::min(sizeof(deviceBasicInfo.deviceName), sizeof(info.deviceName))) != DM_OK) {
109         LOGE("ConvertDeviceInfoToDmDevice copy deviceName data failed.");
110         return;
111     }
112     if (memcpy_s(deviceBasicInfo.networkId, sizeof(deviceBasicInfo.networkId), info.networkId,
113                  std::min(sizeof(deviceBasicInfo.networkId), sizeof(info.networkId))) != DM_OK) {
114         LOGE("ConvertNodeBasicInfoToDmDevice copy networkId data failed.");
115         return;
116     }
117     if (memcpy_s(deviceBasicInfo.deviceId, sizeof(deviceBasicInfo.deviceId), info.deviceId,
118                  std::min(sizeof(deviceBasicInfo.deviceId), sizeof(info.deviceId))) != DM_OK) {
119         LOGE("ConvertNodeBasicInfoToDmDevice copy deviceId data failed.");
120         return;
121     }
122     deviceBasicInfo.deviceTypeId = info.deviceTypeId;
123     handleExtraData(info, deviceBasicInfo);
124 }
125 
SetDeviceInfo(std::shared_ptr<IpcNotifyDeviceStateReq> pReq,const ProcessInfo & processInfo,const DmDeviceState & state,const DmDeviceInfo & deviceInfo,const DmDeviceBasicInfo & deviceBasicInfo)126 void DeviceManagerServiceListener::SetDeviceInfo(std::shared_ptr<IpcNotifyDeviceStateReq> pReq,
127     const ProcessInfo &processInfo, const DmDeviceState &state, const DmDeviceInfo &deviceInfo,
128     const DmDeviceBasicInfo &deviceBasicInfo)
129 {
130     pReq->SetPkgName(processInfo.pkgName);
131     pReq->SetProcessInfo(processInfo);
132     pReq->SetDeviceState(state);
133     DmDeviceInfo dmDeviceInfo = deviceInfo;
134     FillUdidAndUuidToDeviceInfo(processInfo.pkgName, dmDeviceInfo);
135 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
136     std::string appId = "";
137     if (AppManager::GetInstance().GetAppIdByPkgName(processInfo.pkgName, appId) != DM_OK) {
138         pReq->SetDeviceInfo(dmDeviceInfo);
139         pReq->SetDeviceBasicInfo(deviceBasicInfo);
140         return;
141     }
142     ConvertUdidHashToAnoyAndSave(processInfo.pkgName, dmDeviceInfo);
143     DmDeviceBasicInfo dmDeviceBasicInfo = deviceBasicInfo;
144     if (memset_s(dmDeviceBasicInfo.deviceId, DM_MAX_DEVICE_ID_LEN, 0, DM_MAX_DEVICE_ID_LEN) != DM_OK) {
145         LOGE("ConvertNodeBasicInfoToDmDevice memset failed.");
146         return;
147     }
148     if (memcpy_s(dmDeviceBasicInfo.deviceId, sizeof(dmDeviceBasicInfo.deviceId), dmDeviceInfo.deviceId,
149                  std::min(sizeof(dmDeviceBasicInfo.deviceId), sizeof(dmDeviceInfo.deviceId))) != DM_OK) {
150         LOGE("ConvertNodeBasicInfoToDmDevice copy deviceId data failed.");
151         return;
152     }
153     pReq->SetDeviceInfo(dmDeviceInfo);
154     pReq->SetDeviceBasicInfo(dmDeviceBasicInfo);
155     return;
156 #endif
157     pReq->SetDeviceInfo(dmDeviceInfo);
158     pReq->SetDeviceBasicInfo(deviceBasicInfo);
159 }
160 
FillUdidAndUuidToDeviceInfo(const std::string & pkgName,DmDeviceInfo & dmDeviceInfo)161 int32_t DeviceManagerServiceListener::FillUdidAndUuidToDeviceInfo(const std::string &pkgName,
162     DmDeviceInfo &dmDeviceInfo)
163 {
164     if (highPriorityPkgNameSet_.find(pkgName) == highPriorityPkgNameSet_.end()) {
165         return DM_OK;
166     }
167     std::string udid = "";
168     std::string uuid = "";
169     if (SoftbusCache::GetInstance().GetUdidFromCache(dmDeviceInfo.networkId, udid) != DM_OK) {
170         LOGE("GetUdidFromCache fail, networkId:%{public}s ", GetAnonyString(dmDeviceInfo.networkId).c_str());
171         return ERR_DM_FAILED;
172     }
173     if (SoftbusCache::GetInstance().GetUuidFromCache(dmDeviceInfo.networkId, uuid) != DM_OK) {
174         LOGE("GetUuidFromCache fail, networkId:%{public}s ", GetAnonyString(dmDeviceInfo.networkId).c_str());
175         return ERR_DM_FAILED;
176     }
177     std::string extraData = dmDeviceInfo.extraData;
178     if (extraData.empty()) {
179         LOGE("extraData is empty, networkId:%{public}s ", GetAnonyString(dmDeviceInfo.networkId).c_str());
180         return ERR_DM_FAILED;
181     }
182     JsonObject extraJson(extraData);
183     if (extraJson.IsDiscarded()) {
184         LOGE("parse extraData fail, networkId:%{public}s ", GetAnonyString(dmDeviceInfo.networkId).c_str());
185         return ERR_DM_FAILED;
186     }
187     extraJson[PARAM_KEY_UDID] = udid;
188     extraJson[PARAM_KEY_UUID] = uuid;
189     dmDeviceInfo.extraData = ToString(extraJson);
190     return DM_OK;
191 }
192 
ProcessDeviceStateChange(const ProcessInfo & processInfo,const DmDeviceState & state,const DmDeviceInfo & info,const DmDeviceBasicInfo & deviceBasicInfo)193 void DeviceManagerServiceListener::ProcessDeviceStateChange(const ProcessInfo &processInfo, const DmDeviceState &state,
194     const DmDeviceInfo &info, const DmDeviceBasicInfo &deviceBasicInfo)
195 {
196     std::vector<ProcessInfo> processInfoVec = GetNotifyProcessInfoByUserId(processInfo.userId,
197         DmCommonNotifyEvent::REG_DEVICE_STATE);
198     std::vector<ProcessInfo> hpProcessInfoVec;
199     std::vector<ProcessInfo> lpProcessInfoVec;
200     for (const auto &it : processInfoVec) {
201         if (highPriorityPkgNameSet_.find(it.pkgName) != highPriorityPkgNameSet_.end()) {
202             hpProcessInfoVec.push_back(it);
203         } else {
204             lpProcessInfoVec.push_back(it);
205         }
206     }
207 
208     switch (static_cast<int32_t>(state)) {
209         case static_cast<int32_t>(DmDeviceState::DEVICE_STATE_ONLINE):
210             ProcessDeviceOnline(hpProcessInfoVec, processInfo, state, info, deviceBasicInfo);
211             ProcessDeviceOnline(lpProcessInfoVec, processInfo, state, info, deviceBasicInfo);
212             break;
213         case static_cast<int32_t>(DmDeviceState::DEVICE_STATE_OFFLINE):
214             ProcessDeviceOffline(lpProcessInfoVec, processInfo, state, info, deviceBasicInfo);
215             ProcessDeviceOffline(hpProcessInfoVec, processInfo, state, info, deviceBasicInfo);
216             break;
217         case static_cast<int32_t>(DmDeviceState::DEVICE_INFO_READY):
218         case static_cast<int32_t>(DmDeviceState::DEVICE_INFO_CHANGED):
219             ProcessDeviceInfoChange(hpProcessInfoVec, processInfo, state, info, deviceBasicInfo);
220             ProcessDeviceInfoChange(lpProcessInfoVec, processInfo, state, info, deviceBasicInfo);
221             break;
222         default:
223             break;
224     }
225 }
226 
ProcessAppStateChange(const ProcessInfo & processInfo,const DmDeviceState & state,const DmDeviceInfo & info,const DmDeviceBasicInfo & deviceBasicInfo)227 void DeviceManagerServiceListener::ProcessAppStateChange(const ProcessInfo &processInfo, const DmDeviceState &state,
228     const DmDeviceInfo &info, const DmDeviceBasicInfo &deviceBasicInfo)
229 {
230     LOGI("In");
231     std::vector<ProcessInfo> processInfoVec = GetWhiteListSAProcessInfo(DmCommonNotifyEvent::REG_DEVICE_STATE);
232     ProcessInfo bindProcessInfo = DealBindProcessInfo(processInfo);
233     processInfoVec.push_back(bindProcessInfo);
234     std::vector<ProcessInfo> allProcessInfos = ipcServerListener_.GetAllProcessInfo();
235     switch (static_cast<int32_t>(state)) {
236         case static_cast<int32_t>(DmDeviceState::DEVICE_STATE_ONLINE):
237             ProcessAppOnline(processInfoVec, processInfo, state, info, deviceBasicInfo);
238             break;
239         case static_cast<int32_t>(DmDeviceState::DEVICE_STATE_OFFLINE):
240             ProcessAppOffline(processInfoVec, processInfo, state, info, deviceBasicInfo);
241             break;
242         case static_cast<int32_t>(DmDeviceState::DEVICE_INFO_READY):
243         case static_cast<int32_t>(DmDeviceState::DEVICE_INFO_CHANGED):
244             ProcessDeviceInfoChange(processInfoVec, processInfo, state, info, deviceBasicInfo);
245             break;
246         default:
247             break;
248     }
249 }
250 
OnDeviceStateChange(const ProcessInfo & processInfo,const DmDeviceState & state,const DmDeviceInfo & info)251 void DeviceManagerServiceListener::OnDeviceStateChange(const ProcessInfo &processInfo, const DmDeviceState &state,
252                                                        const DmDeviceInfo &info)
253 {
254     DmDeviceBasicInfo deviceBasicInfo;
255     ConvertDeviceInfoToDeviceBasicInfo(processInfo.pkgName, info, deviceBasicInfo);
256     if (processInfo.pkgName == std::string(DM_PKG_NAME)) {
257         ProcessDeviceStateChange(processInfo, state, info, deviceBasicInfo);
258     } else {
259         ProcessAppStateChange(processInfo, state, info, deviceBasicInfo);
260     }
261 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
262     KVAdapterManager::GetInstance().DeleteAgedEntry();
263 #endif
264 }
265 
OnDeviceFound(const ProcessInfo & processInfo,uint16_t subscribeId,const DmDeviceInfo & info)266 void DeviceManagerServiceListener::OnDeviceFound(const ProcessInfo &processInfo, uint16_t subscribeId,
267                                                  const DmDeviceInfo &info)
268 {
269     std::shared_ptr<IpcNotifyDeviceFoundReq> pReq = std::make_shared<IpcNotifyDeviceFoundReq>();
270     std::shared_ptr<IpcRsp> pRsp = std::make_shared<IpcRsp>();
271     DmDeviceInfo deviceInfo = info;
272 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
273     ConvertUdidHashToAnoyAndSave(processInfo.pkgName, deviceInfo);
274 #endif
275     DmDeviceBasicInfo devBasicInfo;
276     ConvertDeviceInfoToDeviceBasicInfo(processInfo.pkgName, deviceInfo, devBasicInfo);
277     pReq->SetDeviceBasicInfo(devBasicInfo);
278     pReq->SetPkgName(processInfo.pkgName);
279     pReq->SetSubscribeId(subscribeId);
280     pReq->SetDeviceInfo(deviceInfo);
281     pReq->SetProcessInfo(processInfo);
282     ipcServerListener_.SendRequest(SERVER_DEVICE_FOUND, pReq, pRsp);
283 }
284 
OnDiscoveryFailed(const ProcessInfo & processInfo,uint16_t subscribeId,int32_t failedReason)285 void DeviceManagerServiceListener::OnDiscoveryFailed(const ProcessInfo &processInfo, uint16_t subscribeId,
286                                                      int32_t failedReason)
287 {
288     LOGI("OnDiscoveryFailed");
289     std::shared_ptr<IpcNotifyDiscoverResultReq> pReq = std::make_shared<IpcNotifyDiscoverResultReq>();
290     std::shared_ptr<IpcRsp> pRsp = std::make_shared<IpcRsp>();
291 
292     pReq->SetPkgName(processInfo.pkgName);
293     pReq->SetSubscribeId(subscribeId);
294     pReq->SetResult(failedReason);
295     pReq->SetProcessInfo(processInfo);
296     ipcServerListener_.SendRequest(SERVER_DISCOVER_FINISH, pReq, pRsp);
297 }
298 
OnDiscoverySuccess(const ProcessInfo & processInfo,int32_t subscribeId)299 void DeviceManagerServiceListener::OnDiscoverySuccess(const ProcessInfo &processInfo, int32_t subscribeId)
300 {
301     LOGI("OnDiscoverySuccess");
302     std::shared_ptr<IpcNotifyDiscoverResultReq> pReq = std::make_shared<IpcNotifyDiscoverResultReq>();
303     std::shared_ptr<IpcRsp> pRsp = std::make_shared<IpcRsp>();
304 
305     pReq->SetPkgName(processInfo.pkgName);
306     pReq->SetSubscribeId((uint16_t)subscribeId);
307     pReq->SetResult(DM_OK);
308     pReq->SetProcessInfo(processInfo);
309     ipcServerListener_.SendRequest(SERVER_DISCOVER_FINISH, pReq, pRsp);
310 }
311 
OnPublishResult(const std::string & pkgName,int32_t publishId,int32_t publishResult)312 void DeviceManagerServiceListener::OnPublishResult(const std::string &pkgName, int32_t publishId, int32_t publishResult)
313 {
314     LOGI("OnPublishResult : %{public}d", publishResult);
315     std::shared_ptr<IpcNotifyPublishResultReq> pReq = std::make_shared<IpcNotifyPublishResultReq>();
316     std::shared_ptr<IpcRsp> pRsp = std::make_shared<IpcRsp>();
317 
318     pReq->SetPkgName(pkgName);
319     pReq->SetPublishId(publishId);
320     pReq->SetResult(publishResult);
321     ipcServerListener_.SendRequest(SERVER_PUBLISH_FINISH, pReq, pRsp);
322 }
323 
OnAuthResult(const ProcessInfo & processInfo,const std::string & deviceId,const std::string & token,int32_t status,int32_t reason)324 void DeviceManagerServiceListener::OnAuthResult(const ProcessInfo &processInfo, const std::string &deviceId,
325                                                 const std::string &token, int32_t status, int32_t reason)
326 {
327     std::shared_ptr<IpcNotifyAuthResultReq> pReq = std::make_shared<IpcNotifyAuthResultReq>();
328     std::shared_ptr<IpcRsp> pRsp = std::make_shared<IpcRsp>();
329     if (status < STATUS_DM_AUTH_FINISH && status > STATUS_DM_AUTH_DEFAULT) {
330         status = STATUS_DM_AUTH_DEFAULT;
331     }
332     pReq->SetDeviceId(deviceId);
333 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
334     std::string deviceIdTemp = "";
335     if (ConvertUdidHashToAnoyDeviceId(processInfo.pkgName, deviceId, deviceIdTemp) == DM_OK) {
336         pReq->SetDeviceId(deviceIdTemp);
337     }
338 #endif
339     pReq->SetPkgName(processInfo.pkgName);
340     pReq->SetToken(token);
341     pReq->SetStatus(status);
342     pReq->SetReason(reason);
343     pReq->SetProcessInfo(processInfo);
344     ipcServerListener_.SendRequest(SERVER_AUTH_RESULT, pReq, pRsp);
345 }
346 
OnUiCall(const ProcessInfo & processInfo,std::string & paramJson)347 void DeviceManagerServiceListener::OnUiCall(const ProcessInfo &processInfo, std::string &paramJson)
348 {
349     LOGI("OnUiCall in");
350     std::shared_ptr<IpcNotifyDMFAResultReq> pReq = std::make_shared<IpcNotifyDMFAResultReq>();
351     std::shared_ptr<IpcRsp> pRsp = std::make_shared<IpcRsp>();
352 
353     pReq->SetPkgName(processInfo.pkgName);
354     pReq->SetJsonParam(paramJson);
355     pReq->SetProcessInfo(processInfo);
356     ipcServerListener_.SendRequest(SERVER_DEVICE_FA_NOTIFY, pReq, pRsp);
357 }
358 
OnCredentialResult(const ProcessInfo & processInfo,int32_t action,const std::string & resultInfo)359 void DeviceManagerServiceListener::OnCredentialResult(const ProcessInfo &processInfo, int32_t action,
360     const std::string &resultInfo)
361 {
362     LOGI("call OnCredentialResult for %{public}s, action %{public}d", processInfo.pkgName.c_str(), action);
363     std::shared_ptr<IpcNotifyCredentialReq> pReq = std::make_shared<IpcNotifyCredentialReq>();
364     std::shared_ptr<IpcRsp> pRsp = std::make_shared<IpcRsp>();
365 
366     pReq->SetPkgName(processInfo.pkgName);
367     pReq->SetCredentialAction(action);
368     pReq->SetCredentialResult(resultInfo);
369     pReq->SetProcessInfo(processInfo);
370     ipcServerListener_.SendRequest(SERVER_CREDENTIAL_RESULT, pReq, pRsp);
371 }
372 
OnBindResult(const ProcessInfo & processInfo,const PeerTargetId & targetId,int32_t result,int32_t status,std::string content)373 void DeviceManagerServiceListener::OnBindResult(const ProcessInfo &processInfo, const PeerTargetId &targetId,
374     int32_t result, int32_t status, std::string content)
375 {
376     std::shared_ptr<IpcNotifyBindResultReq> pReq = std::make_shared<IpcNotifyBindResultReq>();
377     std::shared_ptr<IpcRsp> pRsp = std::make_shared<IpcRsp>();
378     if (status < STATUS_DM_AUTH_FINISH && status > STATUS_DM_AUTH_DEFAULT) {
379         status = STATUS_DM_AUTH_DEFAULT;
380     }
381     PeerTargetId returnTargetId = targetId;
382 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
383     std::string deviceIdTemp = "";
384     DmKVValue kvValue;
385     if (ConvertUdidHashToAnoyDeviceId(processInfo.pkgName, targetId.deviceId, deviceIdTemp) == DM_OK &&
386         KVAdapterManager::GetInstance().Get(deviceIdTemp, kvValue) == DM_OK) {
387         returnTargetId.deviceId = deviceIdTemp;
388     }
389 #endif
390     pReq->SetPkgName(processInfo.pkgName);
391     pReq->SetPeerTargetId(returnTargetId);
392     pReq->SetResult(result);
393     pReq->SetStatus(status);
394     pReq->SetContent(content);
395     pReq->SetProcessInfo(processInfo);
396     ipcServerListener_.SendRequest(BIND_TARGET_RESULT, pReq, pRsp);
397 }
398 
OnUnbindResult(const ProcessInfo & processInfo,const PeerTargetId & targetId,int32_t result,std::string content)399 void DeviceManagerServiceListener::OnUnbindResult(const ProcessInfo &processInfo, const PeerTargetId &targetId,
400     int32_t result, std::string content)
401 {
402     std::shared_ptr<IpcNotifyBindResultReq> pReq = std::make_shared<IpcNotifyBindResultReq>();
403     std::shared_ptr<IpcRsp> pRsp = std::make_shared<IpcRsp>();
404     PeerTargetId returnTargetId = targetId;
405 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
406     std::string deviceIdTemp = "";
407     DmKVValue kvValue;
408     if (ConvertUdidHashToAnoyDeviceId(processInfo.pkgName, targetId.deviceId, deviceIdTemp) == DM_OK &&
409         KVAdapterManager::GetInstance().Get(deviceIdTemp, kvValue) == DM_OK) {
410         returnTargetId.deviceId = deviceIdTemp;
411     }
412 #endif
413     pReq->SetPkgName(processInfo.pkgName);
414     pReq->SetPeerTargetId(returnTargetId);
415     pReq->SetResult(result);
416     pReq->SetContent(content);
417     pReq->SetProcessInfo(processInfo);
418     ipcServerListener_.SendRequest(UNBIND_TARGET_RESULT, pReq, pRsp);
419 }
420 
OnPinHolderCreate(const ProcessInfo & processInfo,const std::string & deviceId,DmPinType pinType,const std::string & payload)421 void DeviceManagerServiceListener::OnPinHolderCreate(const ProcessInfo &processInfo, const std::string &deviceId,
422     DmPinType pinType, const std::string &payload)
423 {
424     LOGI("OnPinHolderCreate : %{public}s", processInfo.pkgName.c_str());
425     std::shared_ptr<IpcCreatePinHolderReq> pReq = std::make_shared<IpcCreatePinHolderReq>();
426     std::shared_ptr<IpcRsp> pRsp = std::make_shared<IpcRsp>();
427 
428     pReq->SetPkgName(processInfo.pkgName);
429     pReq->SetDeviceId(deviceId);
430     pReq->SetPinType(pinType);
431     pReq->SetPayload(payload);
432     pReq->SetProcessInfo(processInfo);
433     ipcServerListener_.SendRequest(SERVER_CREATE_PIN_HOLDER, pReq, pRsp);
434 }
435 
OnPinHolderDestroy(const ProcessInfo & processInfo,DmPinType pinType,const std::string & payload)436 void DeviceManagerServiceListener::OnPinHolderDestroy(const ProcessInfo &processInfo, DmPinType pinType,
437     const std::string &payload)
438 {
439     LOGI("OnPinHolderDestroy : %{public}s", processInfo.pkgName.c_str());
440     std::shared_ptr<IpcDestroyPinHolderReq> pReq = std::make_shared<IpcDestroyPinHolderReq>();
441     std::shared_ptr<IpcRsp> pRsp = std::make_shared<IpcRsp>();
442 
443     pReq->SetPkgName(processInfo.pkgName);
444     pReq->SetPinType(pinType);
445     pReq->SetPayload(payload);
446     pReq->SetProcessInfo(processInfo);
447     ipcServerListener_.SendRequest(SERVER_DESTROY_PIN_HOLDER, pReq, pRsp);
448 }
449 
OnCreateResult(const ProcessInfo & processInfo,int32_t result)450 void DeviceManagerServiceListener::OnCreateResult(const ProcessInfo &processInfo, int32_t result)
451 {
452     LOGI("OnCreateResult : %{public}d", result);
453     std::shared_ptr<IpcNotifyPublishResultReq> pReq = std::make_shared<IpcNotifyPublishResultReq>();
454     std::shared_ptr<IpcRsp> pRsp = std::make_shared<IpcRsp>();
455 
456     pReq->SetPkgName(processInfo.pkgName);
457     pReq->SetResult(result);
458     pReq->SetProcessInfo(processInfo);
459     ipcServerListener_.SendRequest(SERVER_CREATE_PIN_HOLDER_RESULT, pReq, pRsp);
460 }
461 
OnDestroyResult(const ProcessInfo & processInfo,int32_t result)462 void DeviceManagerServiceListener::OnDestroyResult(const ProcessInfo &processInfo, int32_t result)
463 {
464     LOGI("OnDestroyResult : %{public}d", result);
465     std::shared_ptr<IpcNotifyPublishResultReq> pReq = std::make_shared<IpcNotifyPublishResultReq>();
466     std::shared_ptr<IpcRsp> pRsp = std::make_shared<IpcRsp>();
467 
468     pReq->SetPkgName(processInfo.pkgName);
469     pReq->SetResult(result);
470     pReq->SetProcessInfo(processInfo);
471     ipcServerListener_.SendRequest(SERVER_DESTROY_PIN_HOLDER_RESULT, pReq, pRsp);
472 }
473 
OnPinHolderEvent(const ProcessInfo & processInfo,DmPinHolderEvent event,int32_t result,const std::string & content)474 void DeviceManagerServiceListener::OnPinHolderEvent(const ProcessInfo &processInfo, DmPinHolderEvent event,
475     int32_t result, const std::string &content)
476 {
477     LOGI("OnPinHolderEvent pkgName: %{public}s, event: %{public}d, result: %{public}d",
478         processInfo.pkgName.c_str(), event, result);
479     std::shared_ptr<IpcNotifyPinHolderEventReq> pReq = std::make_shared<IpcNotifyPinHolderEventReq>();
480     std::shared_ptr<IpcRsp> pRsp = std::make_shared<IpcRsp>();
481 
482     pReq->SetPkgName(processInfo.pkgName);
483     pReq->SetPinHolderEvent(event);
484     pReq->SetResult(result);
485     pReq->SetContent(content);
486     pReq->SetProcessInfo(processInfo);
487     ipcServerListener_.SendRequest(SERVER_ON_PIN_HOLDER_EVENT, pReq, pRsp);
488 }
489 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
ConvertUdidHashToAnoyAndSave(const std::string & pkgName,DmDeviceInfo & deviceInfo)490 int32_t DeviceManagerServiceListener::ConvertUdidHashToAnoyAndSave(const std::string &pkgName, DmDeviceInfo &deviceInfo)
491 {
492     std::string appId = "";
493     if (AppManager::GetInstance().GetAppIdByPkgName(pkgName, appId) != DM_OK) {
494         LOGD("GetAppIdByPkgName failed");
495         return ERR_DM_FAILED;
496     }
497     DmKVValue kvValue;
498     int32_t ret = Crypto::ConvertUdidHashToAnoyAndSave(appId, std::string(deviceInfo.deviceId), kvValue);
499     if (ret != DM_OK) {
500         return ERR_DM_FAILED;
501     }
502     if (memset_s(deviceInfo.deviceId, DM_MAX_DEVICE_ID_LEN, 0, DM_MAX_DEVICE_ID_LEN) != DM_OK) {
503         LOGE("ConvertUdidHashToAnoyAndSave memset failed.");
504         return ERR_DM_FAILED;
505     }
506     if (memcpy_s(deviceInfo.deviceId, sizeof(deviceInfo.deviceId), kvValue.anoyDeviceId.c_str(),
507                  std::min(sizeof(deviceInfo.deviceId), kvValue.anoyDeviceId.length())) != DM_OK) {
508         LOGE("copy deviceId data failed.");
509         return ERR_DM_FAILED;
510     }
511     return DM_OK;
512 }
513 
ConvertUdidHashToAnoyDeviceId(const std::string & pkgName,const std::string & udidHash,std::string & anoyDeviceId)514 int32_t DeviceManagerServiceListener::ConvertUdidHashToAnoyDeviceId(const std::string &pkgName,
515     const std::string &udidHash, std::string &anoyDeviceId)
516 {
517     std::string appId = "";
518     if (AppManager::GetInstance().GetAppIdByPkgName(pkgName, appId) != DM_OK) {
519         LOGD("GetAppIdByPkgName failed");
520         return ERR_DM_FAILED;
521     }
522     DmKVValue kvValue;
523     int32_t ret = Crypto::ConvertUdidHashToAnoyDeviceId(appId, udidHash, kvValue);
524     if (ret == DM_OK) {
525         anoyDeviceId = kvValue.anoyDeviceId;
526     }
527     return ret;
528 }
529 #endif
530 
OnDeviceTrustChange(const std::string & udid,const std::string & uuid,DmAuthForm authForm)531 void DeviceManagerServiceListener::OnDeviceTrustChange(const std::string &udid, const std::string &uuid,
532     DmAuthForm authForm)
533 {
534     LOGI("udid %{public}s, authForm %{public}d, uuid %{public}s.", GetAnonyString(udid).c_str(),
535         authForm, GetAnonyString(uuid).c_str());
536     std::shared_ptr<IpcNotifyDevTrustChangeReq> pReq = std::make_shared<IpcNotifyDevTrustChangeReq>();
537     std::shared_ptr<IpcRsp> pRsp = std::make_shared<IpcRsp>();
538     int32_t userId = -1;
539 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
540     userId = MultipleUserConnector::GetFirstForegroundUserId();
541 #endif
542     std::vector<ProcessInfo> processInfoVec = GetNotifyProcessInfoByUserId(userId,
543         DmCommonNotifyEvent::REG_REMOTE_DEVICE_TRUST_CHANGE);
544     for (const auto &item : processInfoVec) {
545         pReq->SetPkgName(item.pkgName);
546         pReq->SetUdid(udid);
547         pReq->SetUuid(uuid);
548         pReq->SetAuthForm(authForm);
549         pReq->SetProcessInfo(item);
550         ipcServerListener_.SendRequest(REMOTE_DEVICE_TRUST_CHANGE, pReq, pRsp);
551     }
552 }
553 
SetDeviceScreenInfo(std::shared_ptr<IpcNotifyDeviceStateReq> pReq,const ProcessInfo & processInfo,const DmDeviceInfo & deviceInfo)554 void DeviceManagerServiceListener::SetDeviceScreenInfo(std::shared_ptr<IpcNotifyDeviceStateReq> pReq,
555     const ProcessInfo &processInfo, const DmDeviceInfo &deviceInfo)
556 {
557     LOGI("In");
558     pReq->SetPkgName(processInfo.pkgName);
559     pReq->SetProcessInfo(processInfo);
560 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
561     std::string appId = "";
562     if (AppManager::GetInstance().GetAppIdByPkgName(processInfo.pkgName, appId) != DM_OK) {
563         pReq->SetDeviceInfo(deviceInfo);
564         return;
565     }
566     DmDeviceInfo dmDeviceInfo = deviceInfo;
567     ConvertUdidHashToAnoyAndSave(processInfo.pkgName, dmDeviceInfo);
568     pReq->SetDeviceInfo(dmDeviceInfo);
569     return;
570 #endif
571     pReq->SetDeviceInfo(deviceInfo);
572 }
573 
OnDeviceScreenStateChange(const ProcessInfo & processInfo,DmDeviceInfo & devInfo)574 void DeviceManagerServiceListener::OnDeviceScreenStateChange(const ProcessInfo &processInfo, DmDeviceInfo &devInfo)
575 {
576     LOGI("In, pkgName = %{public}s", processInfo.pkgName.c_str());
577     if (processInfo.pkgName == std::string(DM_PKG_NAME)) {
578         std::shared_ptr<IpcNotifyDeviceStateReq> pReq = std::make_shared<IpcNotifyDeviceStateReq>();
579         std::shared_ptr<IpcRsp> pRsp = std::make_shared<IpcRsp>();
580         int32_t userId = -1;
581 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
582         userId = MultipleUserConnector::GetFirstForegroundUserId();
583 #endif
584         std::vector<ProcessInfo> processInfoVec =
585             GetNotifyProcessInfoByUserId(userId, DmCommonNotifyEvent::REG_DEVICE_SCREEN_STATE);
586         for (const auto &item : processInfoVec) {
587             SetDeviceScreenInfo(pReq, item, devInfo);
588             ipcServerListener_.SendRequest(SERVER_DEVICE_SCREEN_STATE_NOTIFY, pReq, pRsp);
589         }
590     } else {
591         std::shared_ptr<IpcNotifyDeviceStateReq> pReq = std::make_shared<IpcNotifyDeviceStateReq>();
592         std::shared_ptr<IpcRsp> pRsp = std::make_shared<IpcRsp>();
593         std::vector<ProcessInfo> processInfoVec =
594             GetWhiteListSAProcessInfo(DmCommonNotifyEvent::REG_DEVICE_SCREEN_STATE);
595         processInfoVec.push_back(processInfo);
596         for (const auto &item : processInfoVec) {
597             SetDeviceScreenInfo(pReq, item, devInfo);
598             ipcServerListener_.SendRequest(SERVER_DEVICE_SCREEN_STATE_NOTIFY, pReq, pRsp);
599         }
600     }
601 }
602 
RemoveOnlinePkgName(const DmDeviceInfo & info)603 void DeviceManagerServiceListener::RemoveOnlinePkgName(const DmDeviceInfo &info)
604 {
605     LOGI("udidHash: %{public}s.", GetAnonyString(info.deviceId).c_str());
606     {
607         std::lock_guard<std::mutex> autoLock(alreadyNotifyPkgNameLock_);
608         for (auto item = alreadyOnlinePkgName_.begin(); item != alreadyOnlinePkgName_.end();) {
609             if (std::string(item->second.deviceId) == std::string(info.deviceId)) {
610                 item = alreadyOnlinePkgName_.erase(item);
611             } else {
612                 ++item;
613             }
614         }
615     }
616 }
617 
OnCredentialAuthStatus(const ProcessInfo & processInfo,const std::string & deviceList,uint16_t deviceTypeId,int32_t errcode)618 void DeviceManagerServiceListener::OnCredentialAuthStatus(const ProcessInfo &processInfo,
619     const std::string &deviceList, uint16_t deviceTypeId, int32_t errcode)
620 {
621     LOGI("In, pkgName = %{public}s", processInfo.pkgName.c_str());
622     int32_t userId = -1;
623 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
624     userId = MultipleUserConnector::GetFirstForegroundUserId();
625 #endif
626     std::vector<ProcessInfo> processInfoVec =
627         GetNotifyProcessInfoByUserId(userId, DmCommonNotifyEvent::REG_CREDENTIAL_AUTH_STATUS_NOTIFY);
628     for (const auto &item : processInfoVec) {
629         std::shared_ptr<IpcNotifyCredentialAuthStatusReq> pReq = std::make_shared<IpcNotifyCredentialAuthStatusReq>();
630         std::shared_ptr<IpcRsp> pRsp = std::make_shared<IpcRsp>();
631         pReq->SetDeviceList(deviceList);
632         pReq->SetDeviceTypeId(deviceTypeId);
633         pReq->SetErrCode(errcode);
634         pReq->SetPkgName(item.pkgName);
635         pReq->SetProcessInfo(item);
636         ipcServerListener_.SendRequest(SERVICE_CREDENTIAL_AUTH_STATUS_NOTIFY, pReq, pRsp);
637     }
638 }
639 
OnAppUnintall(const std::string & pkgName)640 void DeviceManagerServiceListener::OnAppUnintall(const std::string &pkgName)
641 {
642     std::lock_guard<std::mutex> autoLock(alreadyNotifyPkgNameLock_);
643     for (auto it = alreadyOnlinePkgName_.begin(); it != alreadyOnlinePkgName_.end();) {
644         if (it->first.find(pkgName) != std::string::npos) {
645             it = alreadyOnlinePkgName_.erase(it);
646         } else {
647             ++it;
648         }
649     }
650 }
651 
OnSinkBindResult(const ProcessInfo & processInfo,const PeerTargetId & targetId,int32_t result,int32_t status,std::string content)652 void DeviceManagerServiceListener::OnSinkBindResult(const ProcessInfo &processInfo, const PeerTargetId &targetId,
653     int32_t result, int32_t status, std::string content)
654 {
655     LOGI("pkgName %{public}s, userId %{public}d.", processInfo.pkgName.c_str(), processInfo.userId);
656     std::shared_ptr<IpcNotifyBindResultReq> pReq = std::make_shared<IpcNotifyBindResultReq>();
657     std::shared_ptr<IpcRsp> pRsp = std::make_shared<IpcRsp>();
658     if (status < STATUS_DM_AUTH_FINISH && status > STATUS_DM_AUTH_DEFAULT) {
659         status = STATUS_DM_AUTH_DEFAULT;
660     }
661     PeerTargetId returnTargetId = targetId;
662 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
663     std::string deviceIdTemp = "";
664     DmKVValue kvValue;
665     if (ConvertUdidHashToAnoyDeviceId(processInfo.pkgName, targetId.deviceId, deviceIdTemp) == DM_OK &&
666         KVAdapterManager::GetInstance().Get(deviceIdTemp, kvValue) == DM_OK) {
667         returnTargetId.deviceId = deviceIdTemp;
668     }
669 #endif
670     pReq->SetPkgName(processInfo.pkgName);
671     std::vector<ProcessInfo> processInfos = ipcServerListener_.GetAllProcessInfo();
672     ProcessInfo processInfoTemp;
673     for (auto item : processInfos) {
674         if (item.pkgName == processInfo.pkgName) {
675             processInfoTemp = item;
676         }
677     }
678     if (processInfoTemp.pkgName.empty()) {
679         LOGI("not register listener");
680         return;
681     }
682     pReq->SetProcessInfo(processInfoTemp);
683     pReq->SetPeerTargetId(returnTargetId);
684     pReq->SetResult(result);
685     pReq->SetStatus(status);
686     pReq->SetContent(content);
687     ipcServerListener_.SendRequest(SINK_BIND_TARGET_RESULT, pReq, pRsp);
688 }
689 
GetWhiteListSAProcessInfo(DmCommonNotifyEvent dmCommonNotifyEvent)690 std::vector<ProcessInfo> DeviceManagerServiceListener::GetWhiteListSAProcessInfo(
691     DmCommonNotifyEvent dmCommonNotifyEvent)
692 {
693     if (!IsDmCommonNotifyEventValid(dmCommonNotifyEvent)) {
694         LOGE("Invalid dmCommonNotifyEvent: %{public}d.", dmCommonNotifyEvent);
695         return {};
696     }
697     std::set<ProcessInfo> notifyProcessInfos;
698     DeviceManagerServiceNotify::GetInstance().GetCallBack(dmCommonNotifyEvent, notifyProcessInfos);
699     if (notifyProcessInfos.size() == 0) {
700         LOGE("callback not exist dmCommonNotifyEvent: %{public}d", dmCommonNotifyEvent);
701         return {};
702     }
703     std::unordered_set<std::string> notifyPkgnames = PermissionManager::GetInstance().GetWhiteListSystemSA();
704     std::vector<ProcessInfo> processInfos;
705     for (const auto &it : notifyPkgnames) {
706         ProcessInfo processInfo;
707         processInfo.pkgName = it;
708         processInfo.userId = 0;
709         if (notifyProcessInfos.find(processInfo) == notifyProcessInfos.end()) {
710             continue;
711         }
712         processInfos.push_back(processInfo);
713     }
714     return processInfos;
715 }
716 
GetNotifyProcessInfoByUserId(int32_t userId,DmCommonNotifyEvent dmCommonNotifyEvent)717 std::vector<ProcessInfo> DeviceManagerServiceListener::GetNotifyProcessInfoByUserId(int32_t userId,
718     DmCommonNotifyEvent dmCommonNotifyEvent)
719 {
720     if (!IsDmCommonNotifyEventValid(dmCommonNotifyEvent)) {
721         LOGE("Invalid dmCommonNotifyEvent: %{public}d.", dmCommonNotifyEvent);
722         return {};
723     }
724     std::set<ProcessInfo> notifyProcessInfos;
725     DeviceManagerServiceNotify::GetInstance().GetCallBack(dmCommonNotifyEvent, notifyProcessInfos);
726     if (notifyProcessInfos.size() == 0) {
727         LOGE("callback not exist dmCommonNotifyEvent: %{public}d", dmCommonNotifyEvent);
728         return {};
729     }
730     std::vector<ProcessInfo> processInfos = ipcServerListener_.GetAllProcessInfo();
731     std::set<std::string> systemSA = ipcServerListener_.GetSystemSA();
732     std::vector<ProcessInfo> processInfosTemp;
733     for (auto item : processInfos) {
734         if (systemSA.find(item.pkgName) != systemSA.end()) {
735             item.userId = 0;
736             if (notifyProcessInfos.find(item) == notifyProcessInfos.end()) {
737                 continue;
738             }
739             processInfosTemp.push_back(item);
740         } else if (item.userId == userId) {
741             if (notifyProcessInfos.find(item) == notifyProcessInfos.end()) {
742                 continue;
743             }
744             processInfosTemp.push_back(item);
745         }
746     }
747     return processInfosTemp;
748 }
749 
DealBindProcessInfo(const ProcessInfo & processInfo)750 ProcessInfo DeviceManagerServiceListener::DealBindProcessInfo(const ProcessInfo &processInfo)
751 {
752     std::set<std::string> systemSA = ipcServerListener_.GetSystemSA();
753     if (systemSA.find(processInfo.pkgName) == systemSA.end()) {
754         return processInfo;
755     }
756     ProcessInfo bindProcessInfo = processInfo;
757     bindProcessInfo.userId = 0;
758     return bindProcessInfo;
759 }
760 
ProcessDeviceOnline(const std::vector<ProcessInfo> & procInfoVec,const ProcessInfo & processInfo,const DmDeviceState & state,const DmDeviceInfo & info,const DmDeviceBasicInfo & deviceBasicInfo)761 void DeviceManagerServiceListener::ProcessDeviceOnline(const std::vector<ProcessInfo> &procInfoVec,
762     const ProcessInfo &processInfo, const DmDeviceState &state, const DmDeviceInfo &info,
763     const DmDeviceBasicInfo &deviceBasicInfo)
764 {
765     LOGI("userId %{public}d, state %{public}d, udidhash %{public}s.", processInfo.userId, static_cast<int32_t>(state),
766         GetAnonyString(info.deviceId).c_str());
767     std::shared_ptr<IpcNotifyDeviceStateReq> pReq = std::make_shared<IpcNotifyDeviceStateReq>();
768     std::shared_ptr<IpcRsp> pRsp = std::make_shared<IpcRsp>();
769     for (const auto &it : procInfoVec) {
770         std::string notifyPkgName = it.pkgName + "#" + std::to_string(it.userId) + "#" + std::string(info.deviceId);
771         DmDeviceState notifyState = state;
772         {
773             std::lock_guard<std::mutex> autoLock(alreadyNotifyPkgNameLock_);
774             if (alreadyOnlinePkgName_.find(notifyPkgName) != alreadyOnlinePkgName_.end()) {
775                 notifyState = DmDeviceState::DEVICE_INFO_CHANGED;
776             } else {
777                 alreadyOnlinePkgName_[notifyPkgName] = info;
778             }
779         }
780         SetDeviceInfo(pReq, it, notifyState, info, deviceBasicInfo);
781         ipcServerListener_.SendRequest(SERVER_DEVICE_STATE_NOTIFY, pReq, pRsp);
782     }
783 }
784 
ProcessDeviceOffline(const std::vector<ProcessInfo> & procInfoVec,const ProcessInfo & processInfo,const DmDeviceState & state,const DmDeviceInfo & info,const DmDeviceBasicInfo & deviceBasicInfo)785 void DeviceManagerServiceListener::ProcessDeviceOffline(const std::vector<ProcessInfo> &procInfoVec,
786     const ProcessInfo &processInfo, const DmDeviceState &state, const DmDeviceInfo &info,
787     const DmDeviceBasicInfo &deviceBasicInfo)
788 {
789     LOGI("userId %{public}d, state %{public}d, udidhash %{public}s.", processInfo.userId, static_cast<int32_t>(state),
790         GetAnonyString(info.deviceId).c_str());
791     RemoveNotExistProcess();
792     std::shared_ptr<IpcNotifyDeviceStateReq> pReq = std::make_shared<IpcNotifyDeviceStateReq>();
793     std::shared_ptr<IpcRsp> pRsp = std::make_shared<IpcRsp>();
794     for (const auto &it : procInfoVec) {
795         {
796             std::lock_guard<std::mutex> autoLock(alreadyNotifyPkgNameLock_);
797             if (actUnrelatedPkgName_.find(it.pkgName) != actUnrelatedPkgName_.end()) {
798                 continue;
799             }
800         }
801         std::string notifyPkgName = it.pkgName + "#" + std::to_string(it.userId) + "#" + std::string(info.deviceId);
802         {
803             std::lock_guard<std::mutex> autoLock(alreadyNotifyPkgNameLock_);
804             if (alreadyOnlinePkgName_.find(notifyPkgName) != alreadyOnlinePkgName_.end()) {
805                 alreadyOnlinePkgName_.erase(notifyPkgName);
806             } else {
807                 continue;
808             }
809         }
810         SetDeviceInfo(pReq, it, state, info, deviceBasicInfo);
811         ipcServerListener_.SendRequest(SERVER_DEVICE_STATE_NOTIFY, pReq, pRsp);
812     }
813     {
814         std::lock_guard<std::mutex> autoLock(alreadyNotifyPkgNameLock_);
815         actUnrelatedPkgName_.clear();
816     }
817 }
818 
ProcessDeviceInfoChange(const std::vector<ProcessInfo> & procInfoVec,const ProcessInfo & processInfo,const DmDeviceState & state,const DmDeviceInfo & info,const DmDeviceBasicInfo & deviceBasicInfo)819 void DeviceManagerServiceListener::ProcessDeviceInfoChange(const std::vector<ProcessInfo> &procInfoVec,
820     const ProcessInfo &processInfo, const DmDeviceState &state, const DmDeviceInfo &info,
821     const DmDeviceBasicInfo &deviceBasicInfo)
822 {
823     LOGI("userId %{public}d, state %{public}d, udidhash %{public}s.", processInfo.userId, static_cast<int32_t>(state),
824         GetAnonyString(info.deviceId).c_str());
825     std::shared_ptr<IpcNotifyDeviceStateReq> pReq = std::make_shared<IpcNotifyDeviceStateReq>();
826     std::shared_ptr<IpcRsp> pRsp = std::make_shared<IpcRsp>();
827     for (const auto &it : procInfoVec) {
828         SetDeviceInfo(pReq, it, state, info, deviceBasicInfo);
829         ipcServerListener_.SendRequest(SERVER_DEVICE_STATE_NOTIFY, pReq, pRsp);
830     }
831 }
832 
ProcessAppOnline(const std::vector<ProcessInfo> & procInfoVec,const ProcessInfo & processInfo,const DmDeviceState & state,const DmDeviceInfo & info,const DmDeviceBasicInfo & deviceBasicInfo)833 void DeviceManagerServiceListener::ProcessAppOnline(const std::vector<ProcessInfo> &procInfoVec,
834     const ProcessInfo &processInfo, const DmDeviceState &state, const DmDeviceInfo &info,
835     const DmDeviceBasicInfo &deviceBasicInfo)
836 {
837     LOGI("userId %{public}d, state %{public}d, udidhash %{public}s.", processInfo.userId, static_cast<int32_t>(state),
838         GetAnonyString(info.deviceId).c_str());
839     std::shared_ptr<IpcNotifyDeviceStateReq> pReq = std::make_shared<IpcNotifyDeviceStateReq>();
840     std::shared_ptr<IpcRsp> pRsp = std::make_shared<IpcRsp>();
841     for (const auto &it : procInfoVec) {
842         std::string notifyPkgName = it.pkgName + "#" + std::to_string(it.userId) + "#" + std::string(info.deviceId);
843         DmDeviceState notifyState = state;
844         {
845             std::lock_guard<std::mutex> autoLock(alreadyNotifyPkgNameLock_);
846             if (alreadyOnlinePkgName_.find(notifyPkgName) != alreadyOnlinePkgName_.end()) {
847                 notifyState = DmDeviceState::DEVICE_INFO_CHANGED;
848             } else {
849                 alreadyOnlinePkgName_[notifyPkgName] = info;
850             }
851         }
852         LOGI("ProcessAppOnline notifyState = %{public}d", notifyState);
853         SetDeviceInfo(pReq, it, notifyState, info, deviceBasicInfo);
854         ipcServerListener_.SendRequest(SERVER_DEVICE_STATE_NOTIFY, pReq, pRsp);
855     }
856 }
857 
ProcessAppOffline(const std::vector<ProcessInfo> & procInfoVec,const ProcessInfo & processInfo,const DmDeviceState & state,const DmDeviceInfo & info,const DmDeviceBasicInfo & deviceBasicInfo)858 void DeviceManagerServiceListener::ProcessAppOffline(const std::vector<ProcessInfo> &procInfoVec,
859     const ProcessInfo &processInfo, const DmDeviceState &state, const DmDeviceInfo &info,
860     const DmDeviceBasicInfo &deviceBasicInfo)
861 {
862     LOGI("userId %{public}d, state %{public}d, udidhash %{public}s.", processInfo.userId, static_cast<int32_t>(state),
863         GetAnonyString(info.deviceId).c_str());
864     RemoveNotExistProcess();
865     std::shared_ptr<IpcNotifyDeviceStateReq> pReq = std::make_shared<IpcNotifyDeviceStateReq>();
866     std::shared_ptr<IpcRsp> pRsp = std::make_shared<IpcRsp>();
867     if (!SoftbusCache::GetInstance().CheckIsOnline(std::string(info.deviceId))) {
868         for (const auto &it : procInfoVec) {
869             std::string notifyPkgName = it.pkgName + "#" + std::to_string(it.userId) + "#" + std::string(info.deviceId);
870             {
871                 std::lock_guard<std::mutex> autoLock(alreadyNotifyPkgNameLock_);
872                 if (alreadyOnlinePkgName_.find(notifyPkgName) != alreadyOnlinePkgName_.end()) {
873                     alreadyOnlinePkgName_.erase(notifyPkgName);
874                 } else {
875                     continue;
876                 }
877             }
878             SetDeviceInfo(pReq, it, state, info, deviceBasicInfo);
879             ipcServerListener_.SendRequest(SERVER_DEVICE_STATE_NOTIFY, pReq, pRsp);
880         }
881     } else {
882         std::string notifyPkgName = processInfo.pkgName + "#" + std::to_string(processInfo.userId) + "#" +
883             std::string(info.deviceId);
884         {
885             std::lock_guard<std::mutex> autoLock(alreadyNotifyPkgNameLock_);
886             if (alreadyOnlinePkgName_.find(notifyPkgName) != alreadyOnlinePkgName_.end()) {
887                 alreadyOnlinePkgName_.erase(notifyPkgName);
888             } else {
889                 return;
890             }
891         }
892         SetDeviceInfo(pReq, processInfo, state, info, deviceBasicInfo);
893         ipcServerListener_.SendRequest(SERVER_DEVICE_STATE_NOTIFY, pReq, pRsp);
894     }
895 }
896 
OnProcessRemove(const ProcessInfo & processInfo)897 void DeviceManagerServiceListener::OnProcessRemove(const ProcessInfo &processInfo)
898 {
899     std::lock_guard<std::mutex> autoLock(alreadyNotifyPkgNameLock_);
900     std::string notifyPkgName = processInfo.pkgName + "#" + std::to_string(processInfo.userId);
901     for (auto it = alreadyOnlinePkgName_.begin(); it != alreadyOnlinePkgName_.end();) {
902         if (it->first.find(notifyPkgName) != std::string::npos) {
903             it = alreadyOnlinePkgName_.erase(it);
904         } else {
905             ++it;
906         }
907     }
908 }
909 
OnDevStateCallbackAdd(const ProcessInfo & processInfo,const std::vector<DmDeviceInfo> & deviceList)910 void DeviceManagerServiceListener::OnDevStateCallbackAdd(const ProcessInfo &processInfo,
911     const std::vector<DmDeviceInfo> &deviceList)
912 {
913     for (auto item : deviceList) {
914         std::string notifyPkgName = processInfo.pkgName + "#" + std::to_string(processInfo.userId) + "#" +
915             std::string(item.deviceId);
916         {
917             std::lock_guard<std::mutex> autoLock(alreadyNotifyPkgNameLock_);
918             if (alreadyOnlinePkgName_.find(notifyPkgName) != alreadyOnlinePkgName_.end()) {
919                 continue;
920             }
921             alreadyOnlinePkgName_[notifyPkgName] = item;
922         }
923         DmDeviceBasicInfo deviceBasicInfo;
924         std::shared_ptr<IpcNotifyDeviceStateReq> pReq = std::make_shared<IpcNotifyDeviceStateReq>();
925         std::shared_ptr<IpcRsp> pRsp = std::make_shared<IpcRsp>();
926         ConvertDeviceInfoToDeviceBasicInfo(processInfo.pkgName, item, deviceBasicInfo);
927         SetDeviceInfo(pReq, processInfo, DmDeviceState::DEVICE_STATE_ONLINE, item, deviceBasicInfo);
928         ipcServerListener_.SendRequest(SERVER_DEVICE_STATE_NOTIFY, pReq, pRsp);
929     }
930 }
931 
RemoveNotExistProcess()932 void DeviceManagerServiceListener::RemoveNotExistProcess()
933 {
934     std::set<ProcessInfo> notifyProcessInfos;
935     std::set<std::string> notifyPkgNames;
936     DeviceManagerServiceNotify::GetInstance().GetCallBack(DmCommonNotifyEvent::REG_DEVICE_STATE, notifyProcessInfos);
937     int32_t pkgNameIndex = 0;
938     for (ProcessInfo processInfo : notifyProcessInfos) {
939         notifyPkgNames.insert(processInfo.pkgName);
940     }
941     std::lock_guard<std::mutex> autoLock(alreadyNotifyPkgNameLock_);
942     for (auto it = alreadyOnlinePkgName_.begin(); it != alreadyOnlinePkgName_.end();) {
943         std::string pkgName = GetSubStr(it->first, "#", pkgNameIndex);
944         if (find(notifyPkgNames.begin(), notifyPkgNames.end(), pkgName) == notifyPkgNames.end()) {
945             it = alreadyOnlinePkgName_.erase(it);
946             LOGI("pkgName %{public}s.", pkgName.c_str());
947         } else {
948             ++it;
949         }
950     }
951 }
952 
OnGetDeviceProfileInfoListResult(const ProcessInfo & processInfo,const std::vector<DmDeviceProfileInfo> & deviceProfileInfos,int32_t code)953 void DeviceManagerServiceListener::OnGetDeviceProfileInfoListResult(const ProcessInfo &processInfo,
954     const std::vector<DmDeviceProfileInfo> &deviceProfileInfos, int32_t code)
955 {
956     LOGI("pkgName %{public}s.", processInfo.pkgName.c_str());
957     std::shared_ptr<IpcNotifyGetDeviceProfileInfoListReq> pReq =
958         std::make_shared<IpcNotifyGetDeviceProfileInfoListReq>();
959     std::shared_ptr<IpcRsp> pRsp = std::make_shared<IpcRsp>();
960     pReq->SetPkgName(processInfo.pkgName);
961     pReq->SetDeviceProfileInfoList(deviceProfileInfos);
962 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
963     std::string userDefinedDeviceName = DeviceNameManager::GetInstance().GetUserDefinedDeviceName();
964     if (code == DM_OK && !userDefinedDeviceName.empty()) {
965         std::vector<DmDeviceProfileInfo> temVec = deviceProfileInfos;
966         for (auto &item : temVec) {
967             if (item.isLocalDevice) {
968                 item.deviceName = userDefinedDeviceName;
969                 break;
970             }
971         }
972         pReq->SetDeviceProfileInfoList(temVec);
973     }
974 #endif
975     pReq->SetResult(code);
976     pReq->SetProcessInfo(processInfo);
977     ipcServerListener_.SendRequest(GET_DEVICE_PROFILE_INFO_LIST_RESULT, pReq, pRsp);
978 }
979 
OnGetDeviceIconInfoResult(const ProcessInfo & processInfo,const DmDeviceIconInfo & dmDeviceIconInfo,int32_t code)980 void DeviceManagerServiceListener::OnGetDeviceIconInfoResult(const ProcessInfo &processInfo,
981     const DmDeviceIconInfo &dmDeviceIconInfo, int32_t code)
982 {
983     LOGI("pkgName %{public}s.", processInfo.pkgName.c_str());
984     std::shared_ptr<IpcNotifyGetDeviceIconInfoReq> pReq = std::make_shared<IpcNotifyGetDeviceIconInfoReq>();
985     std::shared_ptr<IpcRsp> pRsp = std::make_shared<IpcRsp>();
986     pReq->SetPkgName(processInfo.pkgName);
987     pReq->SetDmDeviceIconInfo(dmDeviceIconInfo);
988     pReq->SetResult(code);
989     pReq->SetProcessInfo(processInfo);
990     ipcServerListener_.SendRequest(GET_DEVICE_ICON_INFO_RESULT, pReq, pRsp);
991 }
992 
OnSetLocalDeviceNameResult(const ProcessInfo & processInfo,const std::string & deviceName,int32_t code)993 void DeviceManagerServiceListener::OnSetLocalDeviceNameResult(const ProcessInfo &processInfo,
994     const std::string &deviceName, int32_t code)
995 {
996     LOGI("pkgName %{public}s.", processInfo.pkgName.c_str());
997     std::shared_ptr<IpcNotifySetLocalDeviceNameReq> pReq = std::make_shared<IpcNotifySetLocalDeviceNameReq>();
998     std::shared_ptr<IpcRsp> pRsp = std::make_shared<IpcRsp>();
999     pReq->SetPkgName(processInfo.pkgName);
1000     pReq->SetResult(code);
1001     pReq->SetProcessInfo(processInfo);
1002 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
1003     if (code == DM_OK) {
1004         DeviceNameManager::GetInstance().ModifyUserDefinedName(deviceName);
1005     }
1006 #else
1007     (void) deviceName;
1008 #endif
1009     ipcServerListener_.SendRequest(SET_LOCAL_DEVICE_NAME_RESULT, pReq, pRsp);
1010 }
1011 
OnSetRemoteDeviceNameResult(const ProcessInfo & processInfo,const std::string & deviceId,const std::string & deviceName,int32_t code)1012 void DeviceManagerServiceListener::OnSetRemoteDeviceNameResult(const ProcessInfo &processInfo,
1013     const std::string &deviceId, const std::string &deviceName, int32_t code)
1014 {
1015     LOGI("pkgName %{public}s.", processInfo.pkgName.c_str());
1016     std::shared_ptr<IpcNotifySetRemoteDeviceNameReq> pReq = std::make_shared<IpcNotifySetRemoteDeviceNameReq>();
1017     std::shared_ptr<IpcRsp> pRsp = std::make_shared<IpcRsp>();
1018     pReq->SetPkgName(processInfo.pkgName);
1019     pReq->SetDeviceId(deviceId);
1020     pReq->SetResult(code);
1021     pReq->SetProcessInfo(processInfo);
1022     (void) deviceName;
1023     ipcServerListener_.SendRequest(SET_REMOTE_DEVICE_NAME_RESULT, pReq, pRsp);
1024 }
1025 
SetExistPkgName(const std::set<std::string> & pkgNameSet)1026 void DeviceManagerServiceListener::SetExistPkgName(const std::set<std::string> &pkgNameSet)
1027 {
1028     std::lock_guard<std::mutex> autoLock(actUnrelatedPkgNameLock_);
1029     actUnrelatedPkgName_.clear();
1030     for (auto it : pkgNameSet) {
1031         actUnrelatedPkgName_.insert(it);
1032     }
1033 }
1034 
GetLocalDisplayDeviceName()1035 std::string DeviceManagerServiceListener::GetLocalDisplayDeviceName()
1036 {
1037 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
1038     std::string displayName = "";
1039     DeviceNameManager::GetInstance().GetLocalDisplayDeviceName(0, displayName);
1040     return displayName;
1041 #else
1042     return "";
1043 #endif
1044 }
1045 
OpenAuthSessionWithPara(const std::string & deviceId,int32_t actionId,bool isEnable160m)1046 int32_t DeviceManagerServiceListener::OpenAuthSessionWithPara(const std::string &deviceId,
1047     int32_t actionId, bool isEnable160m)
1048 {
1049 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
1050     return DeviceManagerService::GetInstance().OpenAuthSessionWithPara(deviceId, actionId, isEnable160m);
1051 #else
1052     return ERR_DM_UNSUPPORTED_METHOD;
1053 #endif
1054 }
1055 } // namespace DistributedHardware
1056 } // namespace OHOS
1057