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