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 "device_manager_impl.h"
17 #include <random>
18 #include "device_manager_notify.h"
19 #include "dm_anonymous.h"
20 #include "dm_constants.h"
21 #include "dm_dfx_constants.h"
22 #include "dm_hisysevent.h"
23 #include "dm_hitrace.h"
24 #include "dm_log.h"
25 #include "dm_radar_helper.h"
26 #include "dm_random.h"
27 #include "ipc_acl_profile_req.h"
28 #include "ipc_authenticate_device_req.h"
29 #include "ipc_bind_device_req.h"
30 #include "ipc_bind_target_req.h"
31 #include "ipc_check_access_control.h"
32 #include "ipc_common_param_req.h"
33 #include "ipc_create_pin_holder_req.h"
34 #include "ipc_destroy_pin_holder_req.h"
35 #include "ipc_export_auth_code_rsp.h"
36 #include "ipc_generate_encrypted_uuid_req.h"
37 #include "ipc_get_anony_local_udid_rsp.h"
38 #include "ipc_get_device_icon_info_req.h"
39 #include "ipc_get_device_info_rsp.h"
40 #include "ipc_get_device_network_id_list_req.h"
41 #include "ipc_get_device_network_id_list_rsp.h"
42 #include "ipc_get_device_profile_info_list_req.h"
43 #include "ipc_get_device_screen_status_req.h"
44 #include "ipc_get_device_screen_status_rsp.h"
45 #include "ipc_get_info_by_network_req.h"
46 #include "ipc_get_info_by_network_rsp.h"
47 #include "ipc_get_local_device_info_rsp.h"
48 #include "ipc_get_local_device_name_rsp.h"
49 #include "ipc_get_local_display_device_name_req.h"
50 #include "ipc_get_local_display_device_name_rsp.h"
51 #include "ipc_get_localserviceinfo_rsp.h"
52 #include "ipc_get_trustdevice_req.h"
53 #include "ipc_get_trustdevice_rsp.h"
54 #include "ipc_import_auth_code_req.h"
55 #include "ipc_notify_event_req.h"
56 #include "ipc_permission_req.h"
57 #include "ipc_publish_req.h"
58 #include "ipc_put_device_profile_info_list_req.h"
59 #include "ipc_register_serviceinfo_req.h"
60 #include "ipc_set_credential_req.h"
61 #include "ipc_set_credential_rsp.h"
62 #include "ipc_set_local_device_name_req.h"
63 #include "ipc_set_remote_device_name_req.h"
64 #include "ipc_set_useroperation_req.h"
65 #include "ipc_skeleton.h"
66 #include "ipc_sync_callback_req.h"
67 #include "ipc_unauthenticate_device_req.h"
68 #include "ipc_unbind_device_req.h"
69 #include "ipc_unpublish_req.h"
70 #include "securec.h"
71 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
72 #include "ipc_model_codec.h"
73 #include "iservice_registry.h"
74 #include "system_ability_definition.h"
75 #endif
76 namespace OHOS {
77 namespace DistributedHardware {
78
79 constexpr const char* DM_INIT_DEVICE_MANAGER_SUCCESS = "DM_INIT_DEVICE_MANAGER_SUCCESS";
80 constexpr const char* DM_INIT_DEVICE_MANAGER_FAILED = "DM_INIT_DEVICE_MANAGER_FAILED";
81 constexpr const char* START_DEVICE_DISCOVERY_SUCCESS = "START_DEVICE_DISCOVERY_SUCCESS";
82 constexpr const char* GET_LOCAL_DEVICE_INFO_SUCCESS = "GET_LOCAL_DEVICE_INFO_SUCCESS";
83 constexpr const char* GET_LOCAL_DEVICE_INFO_FAILED = "GET_LOCAL_DEVICE_INFO_FAILED";
84 constexpr const char* DM_SEND_REQUEST_SUCCESS = "DM_SEND_REQUEST_SUCCESS";
85 constexpr const char* DM_SEND_REQUEST_FAILED = "DM_SEND_REQUEST_FAILED";
86 constexpr const char* UNAUTHENTICATE_DEVICE_SUCCESS = "UNAUTHENTICATE_DEVICE_SUCCESS";
87 constexpr const char* UNAUTHENTICATE_DEVICE_FAILED = "UNAUTHENTICATE_DEVICE_FAILED";
88 constexpr const char* DM_INIT_DEVICE_MANAGER_SUCCESS_MSG = "init devicemanager success.";
89 constexpr const char* DM_INIT_DEVICE_MANAGER_FAILED_MSG = "init devicemanager failed.";
90 constexpr const char* START_DEVICE_DISCOVERY_SUCCESS_MSG = "device manager discovery success.";
91 constexpr const char* GET_LOCAL_DEVICE_INFO_SUCCESS_MSG = "get local device info success.";
92 constexpr const char* GET_LOCAL_DEVICE_INFO_FAILED_MSG = "get local device info failed.";
93 constexpr const char* DM_SEND_REQUEST_SUCCESS_MSG = "send request success.";
94 constexpr const char* DM_SEND_REQUEST_FAILED_MSG = "send request failed.";
95 constexpr const char* UNAUTHENTICATE_DEVICE_SUCCESS_MSG = "unauthenticate device success.";
96 constexpr const char* UNAUTHENTICATE_DEVICE_FAILED_MSG = "unauthenticate device failed.";
97 constexpr const char* DM_HITRACE_START_DEVICE = "DM_HITRACE_START_DEVICE";
98 constexpr const char* DM_HITRACE_GET_LOCAL_DEVICE_INFO = "DM_HITRACE_GET_LOCAL_DEVICE_INFO";
99 constexpr const char* DM_HITRACE_AUTH_TO_CONSULT = "DM_HITRACE_AUTH_TO_CONSULT";
100 constexpr const char* DM_HITRACE_INIT = "DM_HITRACE_INIT";
101
102 const uint16_t DM_INVALID_FLAG_ID = 0;
103 const uint16_t DM_IMPORT_AUTH_CODE_MIN_LENGTH = 6;
104 const uint16_t DM_IMPORT_AUTH_CODE_MAX_LENGTH = 1024;
105 const int32_t NORMAL = 0;
106 const int32_t SYSTEM_BASIC = 1;
107 const int32_t SYSTEM_CORE = 2;
108 const int32_t USLEEP_TIME_US_100000 = 100000; // 100ms
109 constexpr int32_t SERVICE_INIT_MAX_NUM = 20;
110 constexpr int32_t DM_STRING_LENGTH_MAX = 1024;
111
GetInstance()112 DeviceManagerImpl &DeviceManagerImpl::GetInstance()
113 {
114 static DeviceManagerImpl instance;
115 return instance;
116 }
117
InitDeviceManager(const std::string & pkgName,std::shared_ptr<DmInitCallback> dmInitCallback)118 int32_t DeviceManagerImpl::InitDeviceManager(const std::string &pkgName, std::shared_ptr<DmInitCallback> dmInitCallback)
119 {
120 if (pkgName.empty() || dmInitCallback == nullptr) {
121 DmRadarHelper::GetInstance().ReportDmBehavior(
122 pkgName, "InitDeviceManager", ERR_DM_INPUT_PARA_INVALID, "***");
123 LOGE("DeviceManagerImpl::InitDeviceManager error: Invalid parameter, pkgName: %{public}s", pkgName.c_str());
124 return ERR_DM_INPUT_PARA_INVALID;
125 }
126 DmTraceStart(std::string(DM_HITRACE_INIT));
127 LOGI("Start, pkgName: %{public}s", GetAnonyString(pkgName).c_str());
128
129 int32_t ret = DM_OK;
130 int32_t retryNum = 0;
131 while (retryNum < SERVICE_INIT_MAX_NUM) {
132 ret = ipcClientProxy_->Init(pkgName);
133 if (ret == DM_OK) {
134 break;
135 }
136 usleep(USLEEP_TIME_US_100000);
137 retryNum++;
138 if (retryNum == SERVICE_INIT_TRY_MAX_NUM) {
139 DmRadarHelper::GetInstance().ReportDmBehavior(pkgName, "InitDeviceManager",
140 ERR_DM_NOT_INIT, "***");
141 LOGE("InitDeviceManager error, wait for device manager service starting timeout.");
142 return ERR_DM_NOT_INIT;
143 }
144 }
145 if (ret != DM_OK) {
146 LOGE("InitDeviceManager error, proxy init failed ret: %{public}d", ret);
147 SysEventWrite(std::string(DM_INIT_DEVICE_MANAGER_FAILED), DM_HISYEVENT_FAULT,
148 std::string(DM_INIT_DEVICE_MANAGER_FAILED_MSG));
149 DmRadarHelper::GetInstance().ReportDmBehavior(pkgName, "InitDeviceManager", ret, "***");
150 return ERR_DM_INIT_FAILED;
151 }
152 if (anonyLocalUdid_.empty()) {
153 GetAnonyLocalUdid(pkgName, anonyLocalUdid_);
154 }
155 DmRadarHelper::GetInstance().ReportDmBehavior(pkgName, "InitDeviceManager", DM_OK, anonyLocalUdid_);
156 DeviceManagerNotify::GetInstance().RegisterDeathRecipientCallback(pkgName, dmInitCallback);
157 DmTraceEnd();
158 LOGI("Success");
159 SysEventWrite(std::string(DM_INIT_DEVICE_MANAGER_SUCCESS), DM_HISYEVENT_BEHAVIOR,
160 std::string(DM_INIT_DEVICE_MANAGER_SUCCESS_MSG));
161 return DM_OK;
162 }
163
UnInitDeviceManager(const std::string & pkgName)164 int32_t DeviceManagerImpl::UnInitDeviceManager(const std::string &pkgName)
165 {
166 if (pkgName.empty()) {
167 DmRadarHelper::GetInstance().ReportDmBehavior(
168 pkgName, "UnInitDeviceManager", ERR_DM_INPUT_PARA_INVALID, anonyLocalUdid_);
169 LOGE("UnInitDeviceManager Invalid parameter, pkgName is empty.");
170 return ERR_DM_INPUT_PARA_INVALID;
171 }
172 LOGI("Start, pkgName: %{public}s", GetAnonyString(pkgName).c_str());
173
174 int32_t ret = ipcClientProxy_->UnInit(pkgName);
175 DmRadarHelper::GetInstance().ReportDmBehavior(pkgName, "UnInitDeviceManager", ret, anonyLocalUdid_);
176 if (ret != DM_OK) {
177 LOGE("UnInitDeviceManager error, proxy unInit failed ret: %{public}d", ret);
178 return ERR_DM_FAILED;
179 }
180
181 DeviceManagerNotify::GetInstance().UnRegisterPackageCallback(pkgName);
182 LOGI("Success");
183 return DM_OK;
184 }
185
ConvertDeviceInfoToDeviceBasicInfo(const DmDeviceInfo & info,DmDeviceBasicInfo & deviceBasicInfo)186 void DeviceManagerImpl::ConvertDeviceInfoToDeviceBasicInfo(const DmDeviceInfo &info,
187 DmDeviceBasicInfo &deviceBasicInfo)
188 {
189 if (memset_s(&deviceBasicInfo, sizeof(DmDeviceBasicInfo), 0, sizeof(DmDeviceBasicInfo)) != DM_OK) {
190 LOGE("ConvertDeviceInfoToDeviceBasicInfo memset_s failed.");
191 return;
192 }
193
194 if (memcpy_s(deviceBasicInfo.deviceName, sizeof(deviceBasicInfo.deviceName), info.deviceName,
195 std::min(sizeof(deviceBasicInfo.deviceName), sizeof(info.deviceName))) != DM_OK) {
196 LOGE("ConvertDeviceInfoToDmDevice copy deviceName data failed.");
197 return;
198 }
199
200 if (memcpy_s(deviceBasicInfo.networkId, sizeof(deviceBasicInfo.networkId), info.networkId,
201 std::min(sizeof(deviceBasicInfo.networkId), sizeof(info.networkId))) != DM_OK) {
202 LOGE("ConvertNodeBasicInfoToDmDevice copy networkId data failed.");
203 return;
204 }
205
206 if (memcpy_s(deviceBasicInfo.deviceId, sizeof(deviceBasicInfo.deviceId), info.deviceId,
207 std::min(sizeof(deviceBasicInfo.deviceId), sizeof(info.deviceId))) != DM_OK) {
208 LOGE("ConvertNodeBasicInfoToDmDevice copy deviceId data failed.");
209 return;
210 }
211
212 deviceBasicInfo.deviceTypeId = info.deviceTypeId;
213 }
214
GetTrustedDeviceList(const std::string & pkgName,const std::string & extra,std::vector<DmDeviceInfo> & deviceList)215 int32_t DeviceManagerImpl::GetTrustedDeviceList(const std::string &pkgName, const std::string &extra,
216 std::vector<DmDeviceInfo> &deviceList)
217 {
218 if (pkgName.empty()) {
219 DmRadarHelper::GetInstance().ReportGetTrustDeviceList(
220 pkgName, "GetTrustedDeviceList", deviceList, ERR_DM_INPUT_PARA_INVALID, anonyLocalUdid_);
221 LOGE("Invalid parameter, pkgName is empty.");
222 return ERR_DM_INPUT_PARA_INVALID;
223 }
224 LOGD("Start, pkgName: %{public}s, extra: %{public}s", GetAnonyString(pkgName).c_str(), extra.c_str());
225
226 std::shared_ptr<IpcGetTrustDeviceReq> req = std::make_shared<IpcGetTrustDeviceReq>();
227 std::shared_ptr<IpcGetTrustDeviceRsp> rsp = std::make_shared<IpcGetTrustDeviceRsp>();
228 req->SetPkgName(pkgName);
229 req->SetExtra(extra);
230 int32_t ret = ipcClientProxy_->SendRequest(GET_TRUST_DEVICE_LIST, req, rsp);
231 if (ret != DM_OK) {
232 DmRadarHelper::GetInstance().ReportGetTrustDeviceList(pkgName, "GetTrustedDeviceList",
233 deviceList, ret, anonyLocalUdid_);
234 LOGE("DeviceManagerImpl::GetTrustedDeviceList error, Send Request failed ret: %{public}d", ret);
235 return ERR_DM_IPC_SEND_REQUEST_FAILED;
236 }
237
238 ret = rsp->GetErrCode();
239 if (ret != DM_OK) {
240 DmRadarHelper::GetInstance().ReportGetTrustDeviceList(pkgName, "GetTrustedDeviceList",
241 deviceList, ret, anonyLocalUdid_);
242 LOGI("GetTrustedDeviceList error, failed ret: %{public}d", ret);
243 return ret;
244 }
245
246 deviceList = rsp->GetDeviceVec();
247 LOGI("Completed, device size %{public}zu", deviceList.size());
248 DmRadarHelper::GetInstance().ReportGetTrustDeviceList(pkgName, "GetTrustedDeviceList",
249 deviceList, DM_OK, anonyLocalUdid_);
250 return DM_OK;
251 }
252
GetTrustedDeviceList(const std::string & pkgName,const std::string & extra,bool isRefresh,std::vector<DmDeviceInfo> & deviceList)253 int32_t DeviceManagerImpl::GetTrustedDeviceList(const std::string &pkgName, const std::string &extra,
254 bool isRefresh, std::vector<DmDeviceInfo> &deviceList)
255 {
256 if (pkgName.empty()) {
257 DmRadarHelper::GetInstance().ReportGetTrustDeviceList(
258 pkgName, "GetTrustedDeviceList", deviceList, ERR_DM_INPUT_PARA_INVALID, anonyLocalUdid_);
259 LOGE("Invalid parameter, pkgName is empty.");
260 return ERR_DM_INPUT_PARA_INVALID;
261 }
262 LOGD("Start, pkgName: %{public}s, extra: %{public}s, isRefresh: %{public}d", GetAnonyString(pkgName).c_str(),
263 extra.c_str(), isRefresh);
264
265 std::shared_ptr<IpcGetTrustDeviceReq> req = std::make_shared<IpcGetTrustDeviceReq>();
266 std::shared_ptr<IpcGetTrustDeviceRsp> rsp = std::make_shared<IpcGetTrustDeviceRsp>();
267 req->SetPkgName(pkgName);
268 req->SetExtra(extra);
269 req->SetRefresh(isRefresh);
270 int32_t ret = ipcClientProxy_->SendRequest(GET_TRUST_DEVICE_LIST, req, rsp);
271 if (ret != DM_OK) {
272 DmRadarHelper::GetInstance().ReportGetTrustDeviceList(pkgName, "GetTrustedDeviceList",
273 deviceList, ret, anonyLocalUdid_);
274 LOGE("DeviceManagerImpl::GetTrustedDeviceList error, Send Request failed ret: %{public}d", ret);
275 return ERR_DM_IPC_SEND_REQUEST_FAILED;
276 }
277
278 ret = rsp->GetErrCode();
279 if (ret != DM_OK) {
280 DmRadarHelper::GetInstance().ReportGetTrustDeviceList(pkgName, "GetTrustedDeviceList",
281 deviceList, ret, anonyLocalUdid_);
282 LOGE("GetTrustedDeviceList error, failed ret: %{public}d", ret);
283 return ret;
284 }
285 deviceList = rsp->GetDeviceVec();
286 LOGI("Completed, device size %{public}zu", deviceList.size());
287 DmRadarHelper::GetInstance().ReportGetTrustDeviceList(pkgName, "GetTrustedDeviceList",
288 deviceList, DM_OK, anonyLocalUdid_);
289 return DM_OK;
290 }
291
GetAvailableDeviceList(const std::string & pkgName,std::vector<DmDeviceBasicInfo> & deviceList)292 int32_t DeviceManagerImpl::GetAvailableDeviceList(const std::string &pkgName,
293 std::vector<DmDeviceBasicInfo> &deviceList)
294 {
295 LOGI("Start, pkgName: %{public}s.", GetAnonyString(pkgName).c_str());
296 std::vector<DmDeviceInfo> deviceListTemp;
297 std::string extra = "";
298 int32_t ret = GetTrustedDeviceList(pkgName, extra, false, deviceListTemp);
299 DmRadarHelper::GetInstance().ReportGetTrustDeviceList(
300 pkgName, "GetTrustedDeviceList", deviceListTemp, ret, anonyLocalUdid_);
301 if (ret != DM_OK) {
302 LOGE("DeviceManagerImpl::GetTrustedDeviceList error.");
303 return ret;
304 }
305 for (auto &item : deviceListTemp) {
306 DmDeviceBasicInfo deviceBasicInfo;
307 ConvertDeviceInfoToDeviceBasicInfo(item, deviceBasicInfo);
308 deviceList.push_back(deviceBasicInfo);
309 }
310 return DM_OK;
311 }
312
GetDeviceInfo(const std::string & pkgName,const std::string networkId,DmDeviceInfo & deviceInfo)313 int32_t DeviceManagerImpl::GetDeviceInfo(const std::string &pkgName, const std::string networkId,
314 DmDeviceInfo &deviceInfo)
315 {
316 if (pkgName.empty() || networkId.empty()) {
317 DmRadarHelper::GetInstance().ReportGetDeviceInfo(
318 pkgName, "GetDeviceInfo", deviceInfo, ERR_DM_INPUT_PARA_INVALID, anonyLocalUdid_);
319 LOGE("Invalid parameter, pkgName: %{public}s, netWorkId: %{public}s", pkgName.c_str(),
320 GetAnonyString(networkId).c_str());
321 return ERR_DM_INPUT_PARA_INVALID;
322 }
323 LOGI("Start, pkgName: %{public}s networKId : %{public}s", pkgName.c_str(),
324 GetAnonyString(networkId).c_str());
325 std::shared_ptr<IpcGetInfoByNetWorkReq> req = std::make_shared<IpcGetInfoByNetWorkReq>();
326 std::shared_ptr<IpcGetDeviceInfoRsp> rsp = std::make_shared<IpcGetDeviceInfoRsp>();
327 req->SetPkgName(pkgName);
328 req->SetNetWorkId(networkId);
329 int32_t ret = ipcClientProxy_->SendRequest(GET_DEVICE_INFO, req, rsp);
330 if (ret != DM_OK) {
331 DmRadarHelper::GetInstance().ReportGetDeviceInfo(pkgName, "GetDeviceInfo",
332 deviceInfo, ret, anonyLocalUdid_);
333 LOGE("DeviceManagerImpl::GetDeviceInfo error, Send Request failed ret: %{public}d", ret);
334 return ERR_DM_IPC_SEND_REQUEST_FAILED;
335 }
336
337 ret = rsp->GetErrCode();
338 if (ret != DM_OK) {
339 DmRadarHelper::GetInstance().ReportGetDeviceInfo(pkgName, "GetDeviceInfo",
340 deviceInfo, ret, anonyLocalUdid_);
341 LOGE("DeviceManagerImpl::GetDeviceInfo error, failed ret: %{public}d", ret);
342 return ret;
343 }
344
345 deviceInfo = rsp->GetDeviceInfo();
346 LOGI("Completed, networKId = %{public}s deviceName = %{public}s", GetAnonyString(req->GetNetWorkId()).c_str(),
347 GetAnonyString(deviceInfo.deviceName).c_str());
348 DmRadarHelper::GetInstance().ReportGetDeviceInfo(pkgName, "GetDeviceInfo",
349 deviceInfo, DM_OK, anonyLocalUdid_);
350 return DM_OK;
351 }
352
GetLocalDeviceInfo(const std::string & pkgName,DmDeviceInfo & info)353 int32_t DeviceManagerImpl::GetLocalDeviceInfo(const std::string &pkgName, DmDeviceInfo &info)
354 {
355 LOGD("Start, pkgName: %{public}s", GetAnonyString(pkgName).c_str());
356 DmTraceStart(std::string(DM_HITRACE_GET_LOCAL_DEVICE_INFO));
357 std::shared_ptr<IpcReq> req = std::make_shared<IpcReq>();
358 std::shared_ptr<IpcGetLocalDeviceInfoRsp> rsp = std::make_shared<IpcGetLocalDeviceInfoRsp>();
359 req->SetPkgName(pkgName);
360 int32_t ret = ipcClientProxy_->SendRequest(GET_LOCAL_DEVICE_INFO, req, rsp);
361 if (ret != DM_OK) {
362 DmRadarHelper::GetInstance().ReportGetLocalDevInfo(pkgName, "GetLocalDeviceInfo",
363 info, ret, anonyLocalUdid_);
364 LOGE("DeviceManagerImpl::GetLocalDeviceInfo error, Send Request failed ret: %{public}d", ret);
365 return ERR_DM_IPC_SEND_REQUEST_FAILED;
366 }
367
368 ret = rsp->GetErrCode();
369 if (ret != DM_OK) {
370 DmRadarHelper::GetInstance().ReportGetLocalDevInfo(pkgName, "GetLocalDeviceInfo",
371 info, ret, anonyLocalUdid_);
372 LOGI("DeviceManagerImpl::GetLocalDeviceInfo error, failed ret: %{public}d", ret);
373 SysEventWrite(std::string(GET_LOCAL_DEVICE_INFO_FAILED), DM_HISYEVENT_BEHAVIOR,
374 std::string(GET_LOCAL_DEVICE_INFO_FAILED_MSG));
375 return ret;
376 }
377
378 info = rsp->GetLocalDeviceInfo();
379 DmTraceEnd();
380 SysEventWrite(std::string(GET_LOCAL_DEVICE_INFO_SUCCESS), DM_HISYEVENT_BEHAVIOR,
381 std::string(GET_LOCAL_DEVICE_INFO_SUCCESS_MSG));
382 DmRadarHelper::GetInstance().ReportGetLocalDevInfo(pkgName, "GetLocalDeviceInfo",
383 info, DM_OK, anonyLocalUdid_);
384 return DM_OK;
385 }
386
RegisterDevStateCallback(const std::string & pkgName,const std::string & extra,std::shared_ptr<DeviceStateCallback> callback)387 int32_t DeviceManagerImpl::RegisterDevStateCallback(const std::string &pkgName, const std::string &extra,
388 std::shared_ptr<DeviceStateCallback> callback)
389 {
390 if (pkgName.empty() || callback == nullptr) {
391 DmRadarHelper::GetInstance().ReportDmBehavior(
392 pkgName, "RegisterDevStateCallback", ERR_DM_INPUT_PARA_INVALID, anonyLocalUdid_);
393 LOGE("RegisterDevStateCallback error: Invalid para");
394 return ERR_DM_INPUT_PARA_INVALID;
395 }
396 LOGI("Start, pkgName: %{public}s", pkgName.c_str());
397 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
398 auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
399 if (samgr == nullptr) {
400 DmRadarHelper::GetInstance().ReportDmBehavior(pkgName, "RegisterDevStateCallback",
401 ERR_DM_INIT_FAILED, anonyLocalUdid_);
402 LOGE("Get SystemAbilityManager Failed");
403 return ERR_DM_INIT_FAILED;
404 }
405 while (samgr->CheckSystemAbility(ACCESS_TOKEN_MANAGER_SERVICE_ID) == nullptr) {
406 LOGD("Access_token SA not start.");
407 usleep(USLEEP_TIME_US_100000);
408 }
409 int32_t ret = CheckApiPermission(SYSTEM_CORE);
410 if (ret != DM_OK) {
411 DmRadarHelper::GetInstance().ReportDmBehavior(pkgName, "RegisterDevStateCallback", ret, anonyLocalUdid_);
412 LOGE("System SA not have permission, ret: %{public}d.", ret);
413 return ret;
414 }
415 #endif
416 DeviceManagerNotify::GetInstance().RegisterDeviceStateCallback(pkgName, callback);
417 SyncCallbackToService(DmCommonNotifyEvent::REG_DEVICE_STATE, pkgName);
418 DmRadarHelper::GetInstance().ReportDmBehavior(pkgName, "RegisterDevStateCallback", DM_OK, anonyLocalUdid_);
419 LOGI("Completed");
420 return DM_OK;
421 }
422
RegisterDevStatusCallback(const std::string & pkgName,const std::string & extra,std::shared_ptr<DeviceStatusCallback> callback)423 int32_t DeviceManagerImpl::RegisterDevStatusCallback(const std::string &pkgName, const std::string &extra,
424 std::shared_ptr<DeviceStatusCallback> callback)
425 {
426 if (pkgName.empty() || callback == nullptr) {
427 DmRadarHelper::GetInstance().ReportDmBehavior(
428 pkgName, "RegisterDevStatusCallback", ERR_DM_INPUT_PARA_INVALID, anonyLocalUdid_);
429 LOGE("RegisterDevStatusCallback error: Invalid para");
430 return ERR_DM_INPUT_PARA_INVALID;
431 }
432 LOGI("Start, pkgName: %{public}s", pkgName.c_str());
433 DeviceManagerNotify::GetInstance().RegisterDeviceStatusCallback(pkgName, callback);
434 SyncCallbackToService(DmCommonNotifyEvent::REG_DEVICE_STATE, pkgName);
435 DmRadarHelper::GetInstance().ReportDmBehavior(pkgName, "RegisterDevStatusCallback", DM_OK, anonyLocalUdid_);
436 LOGI("Completed");
437 return DM_OK;
438 }
439
UnRegisterDevStateCallback(const std::string & pkgName)440 int32_t DeviceManagerImpl::UnRegisterDevStateCallback(const std::string &pkgName)
441 {
442 if (pkgName.empty()) {
443 DmRadarHelper::GetInstance().ReportDmBehavior(
444 pkgName, "UnRegisterDevStateCallback", ERR_DM_INPUT_PARA_INVALID, anonyLocalUdid_);
445 LOGE("UnRegisterDevStateCallback Invalid parameter, pkgName is empty.");
446 return ERR_DM_INPUT_PARA_INVALID;
447 }
448 LOGI("Start, pkgName: %{public}s", pkgName.c_str());
449 SyncCallbackToService(DmCommonNotifyEvent::UN_REG_DEVICE_STATE, pkgName);
450 DeviceManagerNotify::GetInstance().UnRegisterDeviceStateCallback(pkgName);
451 DmRadarHelper::GetInstance().ReportDmBehavior(pkgName, "UnRegisterDevStateCallback", DM_OK, anonyLocalUdid_);
452 LOGI("Completed");
453 return DM_OK;
454 }
455
UnRegisterDevStatusCallback(const std::string & pkgName)456 int32_t DeviceManagerImpl::UnRegisterDevStatusCallback(const std::string &pkgName)
457 {
458 if (pkgName.empty()) {
459 DmRadarHelper::GetInstance().ReportDmBehavior(
460 pkgName, "UnRegisterDevStatusCallback", ERR_DM_INPUT_PARA_INVALID, anonyLocalUdid_);
461 LOGE("UnRegisterDevStatusCallback Invalid parameter, pkgName is empty.");
462 return ERR_DM_INPUT_PARA_INVALID;
463 }
464 LOGI("Start, pkgName: %{public}s", pkgName.c_str());
465 SyncCallbackToService(DmCommonNotifyEvent::UN_REG_DEVICE_STATE, pkgName);
466 DeviceManagerNotify::GetInstance().UnRegisterDeviceStatusCallback(pkgName);
467 DmRadarHelper::GetInstance().ReportDmBehavior(pkgName, "UnRegisterDevStatusCallback", DM_OK, anonyLocalUdid_);
468 LOGI("Completed");
469 return DM_OK;
470 }
471
472
StartDeviceDiscovery(const std::string & pkgName,const DmSubscribeInfo & subscribeInfo,const std::string & extra,std::shared_ptr<DiscoveryCallback> callback)473 int32_t DeviceManagerImpl::StartDeviceDiscovery(const std::string &pkgName, const DmSubscribeInfo &subscribeInfo,
474 const std::string &extra, std::shared_ptr<DiscoveryCallback> callback)
475 {
476 if (pkgName.empty() || callback == nullptr) {
477 LOGE("DeviceManagerImpl::StartDeviceDiscovery error: Invalid para, pkgName: %{public}s", pkgName.c_str());
478 return ERR_DM_INPUT_PARA_INVALID;
479 }
480 LOGI("Start, pkgName: %{public}s", pkgName.c_str());
481 std::map<std::string, std::string> discParam;
482 discParam.insert(std::pair<std::string, std::string>(PARAM_KEY_SUBSCRIBE_ID,
483 std::to_string(subscribeInfo.subscribeId)));
484 discParam.insert(std::pair<std::string, std::string>(PARAM_KEY_DISC_MEDIUM, std::to_string(subscribeInfo.medium)));
485 std::map<std::string, std::string> filterOps;
486 filterOps.insert(std::pair<std::string, std::string>(PARAM_KEY_FILTER_OPTIONS, extra));
487 return StartDiscovering(pkgName, discParam, filterOps, callback);
488 }
489
StartDeviceDiscovery(const std::string & pkgName,uint64_t tokenId,const std::string & filterOptions,std::shared_ptr<DiscoveryCallback> callback)490 int32_t DeviceManagerImpl::StartDeviceDiscovery(const std::string &pkgName, uint64_t tokenId,
491 const std::string &filterOptions, std::shared_ptr<DiscoveryCallback> callback)
492 {
493 if (pkgName.empty() || callback == nullptr) {
494 LOGE("DeviceManagerImpl::StartDeviceDiscovery error: Invalid para, pkgName: %{public}s", pkgName.c_str());
495 return ERR_DM_INPUT_PARA_INVALID;
496 }
497 std::map<std::string, std::string> discParam;
498 discParam.insert(std::pair<std::string, std::string>(PARAM_KEY_SUBSCRIBE_ID, std::to_string(tokenId)));
499 std::map<std::string, std::string> filterOps;
500 filterOps.insert(std::pair<std::string, std::string>(PARAM_KEY_FILTER_OPTIONS, filterOptions));
501 return StartDiscovering(pkgName, discParam, filterOps, callback);
502 }
503
StopDeviceDiscovery(const std::string & pkgName,uint16_t subscribeId)504 int32_t DeviceManagerImpl::StopDeviceDiscovery(const std::string &pkgName, uint16_t subscribeId)
505 {
506 if (pkgName.empty()) {
507 LOGE("DeviceManagerImpl::StopDeviceDiscovery Invalid parameter, pkgName is empty.");
508 return ERR_DM_INPUT_PARA_INVALID;
509 }
510 std::map<std::string, std::string> discParam;
511 discParam.insert(std::pair<std::string, std::string>(PARAM_KEY_SUBSCRIBE_ID, std::to_string(subscribeId)));
512 return StopDiscovering(pkgName, discParam);
513 }
514
StopDeviceDiscovery(uint64_t tokenId,const std::string & pkgName)515 int32_t DeviceManagerImpl::StopDeviceDiscovery(uint64_t tokenId, const std::string &pkgName)
516 {
517 if (pkgName.empty()) {
518 LOGE("DeviceManagerImpl::StopDeviceDiscovery Invalid parameter, pkgName is empty.");
519 return ERR_DM_INPUT_PARA_INVALID;
520 }
521 std::map<std::string, std::string> discParam;
522 discParam.insert(std::pair<std::string, std::string>(PARAM_KEY_SUBSCRIBE_ID, std::to_string(tokenId)));
523 return StopDiscovering(pkgName, discParam);
524 }
525
StartDiscovering(const std::string & pkgName,std::map<std::string,std::string> & discoverParam,const std::map<std::string,std::string> & filterOptions,std::shared_ptr<DiscoveryCallback> callback)526 int32_t DeviceManagerImpl::StartDiscovering(const std::string &pkgName,
527 std::map<std::string, std::string> &discoverParam, const std::map<std::string, std::string> &filterOptions,
528 std::shared_ptr<DiscoveryCallback> callback)
529 {
530 if (pkgName.empty() || callback == nullptr) {
531 LOGE("DeviceManagerImpl::StartDiscovering failed: input callback is null or pkgName is empty.");
532 return ERR_DM_INPUT_PARA_INVALID;
533 }
534 LOGI("Start, pkgName: %{public}s", pkgName.c_str());
535 DmTraceStart(std::string(DM_HITRACE_START_DEVICE));
536
537 uint16_t subscribeId = AddDiscoveryCallback(pkgName, discoverParam, callback);
538 discoverParam.emplace(PARAM_KEY_SUBSCRIBE_ID, std::to_string(subscribeId));
539 std::string discParaStr = ConvertMapToJsonString(discoverParam);
540 std::string filterOpStr = ConvertMapToJsonString(filterOptions);
541
542 std::shared_ptr<IpcCommonParamReq> req = std::make_shared<IpcCommonParamReq>();
543 std::shared_ptr<IpcRsp> rsp = std::make_shared<IpcRsp>();
544 req->SetPkgName(ComposeStr(pkgName, subscribeId));
545 req->SetFirstParam(discParaStr);
546 req->SetSecondParam(filterOpStr);
547 int32_t ret = ipcClientProxy_->SendRequest(START_DISCOVERING, req, rsp);
548 if (ret != DM_OK) {
549 LOGE("StartDiscovering error: Send Request failed ret: %{public}d", ret);
550 return ERR_DM_IPC_SEND_REQUEST_FAILED;
551 }
552 ret = rsp->GetErrCode();
553 if (ret != DM_OK) {
554 LOGE("StartDiscovering error: Failed with ret %{public}d", ret);
555 return ret;
556 }
557
558 DmTraceEnd();
559 LOGI("Completed");
560 SysEventWrite(std::string(START_DEVICE_DISCOVERY_SUCCESS), DM_HISYEVENT_BEHAVIOR,
561 std::string(START_DEVICE_DISCOVERY_SUCCESS_MSG));
562 return DM_OK;
563 }
564
StopDiscovering(const std::string & pkgName,std::map<std::string,std::string> & discoverParam)565 int32_t DeviceManagerImpl::StopDiscovering(const std::string &pkgName,
566 std::map<std::string, std::string> &discoverParam)
567 {
568 if (pkgName.empty()) {
569 LOGE("DeviceManagerImpl::StopDiscovering failed: input pkgName is empty.");
570 return ERR_DM_INPUT_PARA_INVALID;
571 }
572 LOGI("Start, pkgName: %{public}s", pkgName.c_str());
573 uint16_t subscribeId = DM_INVALID_FLAG_ID;
574 if (discoverParam.find(PARAM_KEY_SUBSCRIBE_ID) != discoverParam.end()) {
575 subscribeId = std::atoi((discoverParam.find(PARAM_KEY_SUBSCRIBE_ID)->second).c_str());
576 }
577 std::string pkgNameTemp = ComposeStr(pkgName, subscribeId);
578 subscribeId = GetSubscribeIdFromMap(pkgNameTemp);
579 if (subscribeId == DM_INVALID_FLAG_ID) {
580 LOGE("DeviceManagerImpl::StopDiscovering failed: cannot find pkgName in cache map.");
581 return ERR_DM_INPUT_PARA_INVALID;
582 }
583 discoverParam.emplace(PARAM_KEY_SUBSCRIBE_ID, std::to_string(subscribeId));
584 std::string discParaStr = ConvertMapToJsonString(discoverParam);
585
586 std::shared_ptr<IpcCommonParamReq> req = std::make_shared<IpcCommonParamReq>();
587 std::shared_ptr<IpcRsp> rsp = std::make_shared<IpcRsp>();
588 req->SetPkgName(ComposeStr(pkgName, subscribeId));
589 req->SetFirstParam(discParaStr);
590 int32_t ret = ipcClientProxy_->SendRequest(STOP_DISCOVERING, req, rsp);
591 if (ret != DM_OK) {
592 LOGE("StopDiscovering error: Send Request failed ret: %{public}d", ret);
593 return ERR_DM_IPC_SEND_REQUEST_FAILED;
594 }
595 ret = rsp->GetErrCode();
596 if (ret != DM_OK) {
597 LOGE("StopDiscovering error: Failed with ret %{public}d", ret);
598 return ret;
599 }
600 RemoveDiscoveryCallback(pkgNameTemp);
601 LOGI("Completed");
602 return DM_OK;
603 }
604
PublishDeviceDiscovery(const std::string & pkgName,const DmPublishInfo & publishInfo,std::shared_ptr<PublishCallback> callback)605 int32_t DeviceManagerImpl::PublishDeviceDiscovery(const std::string &pkgName, const DmPublishInfo &publishInfo,
606 std::shared_ptr<PublishCallback> callback)
607 {
608 if (pkgName.empty() || callback == nullptr) {
609 DmRadarHelper::GetInstance().ReportDmBehavior(pkgName, "PublishDeviceDiscovery",
610 ERR_DM_INPUT_PARA_INVALID, anonyLocalUdid_);
611 LOGE("PublishDeviceDiscovery error: pkgName %{public}s invalid para", pkgName.c_str());
612 return ERR_DM_INPUT_PARA_INVALID;
613 }
614
615 LOGI("Start, pkgName %{public}s", pkgName.c_str());
616 DeviceManagerNotify::GetInstance().RegisterPublishCallback(pkgName, publishInfo.publishId, callback);
617
618 std::shared_ptr<IpcPublishReq> req = std::make_shared<IpcPublishReq>();
619 std::shared_ptr<IpcRsp> rsp = std::make_shared<IpcRsp>();
620 req->SetPkgName(pkgName);
621 req->SetPublishInfo(publishInfo);
622 int32_t ret = ipcClientProxy_->SendRequest(PUBLISH_DEVICE_DISCOVER, req, rsp);
623 if (ret != DM_OK) {
624 DmRadarHelper::GetInstance().ReportDmBehavior(pkgName, "PublishDeviceDiscovery", ret, anonyLocalUdid_);
625 LOGE("PublishDeviceDiscovery error: Send Request failed ret: %{public}d", ret);
626 DeviceManagerNotify::GetInstance().UnRegisterPublishCallback(pkgName, publishInfo.publishId);
627 return ERR_DM_IPC_SEND_REQUEST_FAILED;
628 }
629
630 ret = rsp->GetErrCode();
631 if (ret != DM_OK) {
632 DmRadarHelper::GetInstance().ReportDmBehavior(pkgName, "PublishDeviceDiscovery", ret, anonyLocalUdid_);
633 LOGE("PublishDeviceDiscovery error: Failed with ret %{public}d", ret);
634 return ret;
635 }
636 DmRadarHelper::GetInstance().ReportDmBehavior(pkgName, "PublishDeviceDiscovery", DM_OK, anonyLocalUdid_);
637 LOGI("Completed");
638 return DM_OK;
639 }
640
UnPublishDeviceDiscovery(const std::string & pkgName,int32_t publishId)641 int32_t DeviceManagerImpl::UnPublishDeviceDiscovery(const std::string &pkgName, int32_t publishId)
642 {
643 if (pkgName.empty()) {
644 DmRadarHelper::GetInstance().ReportDmBehavior(pkgName, "UnPublishDeviceDiscovery",
645 ERR_DM_INPUT_PARA_INVALID, anonyLocalUdid_);
646 LOGE("Invalid parameter, pkgName is empty.");
647 return ERR_DM_INPUT_PARA_INVALID;
648 }
649
650 LOGI("Start, pkgName %{public}s", pkgName.c_str());
651 std::shared_ptr<IpcUnPublishReq> req = std::make_shared<IpcUnPublishReq>();
652 std::shared_ptr<IpcRsp> rsp = std::make_shared<IpcRsp>();
653 req->SetPkgName(pkgName);
654 req->SetPublishId(publishId);
655 int32_t ret = ipcClientProxy_->SendRequest(UNPUBLISH_DEVICE_DISCOVER, req, rsp);
656 if (ret != DM_OK) {
657 DmRadarHelper::GetInstance().ReportDmBehavior(pkgName, "UnPublishDeviceDiscovery", ret, anonyLocalUdid_);
658 LOGE("UnPublishDeviceDiscovery error: Send Request failed ret: %{public}d", ret);
659 return ERR_DM_IPC_SEND_REQUEST_FAILED;
660 }
661
662 ret = rsp->GetErrCode();
663 if (ret != DM_OK) {
664 DmRadarHelper::GetInstance().ReportDmBehavior(pkgName, "UnPublishDeviceDiscovery", ret, anonyLocalUdid_);
665 LOGE("UnPublishDeviceDiscovery error: Failed with ret %{public}d", ret);
666 return ret;
667 }
668
669 DeviceManagerNotify::GetInstance().UnRegisterPublishCallback(pkgName, publishId);
670 DmRadarHelper::GetInstance().ReportDmBehavior(pkgName, "UnPublishDeviceDiscovery", DM_OK, anonyLocalUdid_);
671 LOGI("Completed");
672 return DM_OK;
673 }
674
AuthenticateDevice(const std::string & pkgName,int32_t authType,const DmDeviceInfo & deviceInfo,const std::string & extra,std::shared_ptr<AuthenticateCallback> callback)675 int32_t DeviceManagerImpl::AuthenticateDevice(const std::string &pkgName, int32_t authType,
676 const DmDeviceInfo &deviceInfo, const std::string &extra,
677 std::shared_ptr<AuthenticateCallback> callback)
678 {
679 if (pkgName.empty() || callback == nullptr) {
680 LOGE("Invalid parameter, pkgName is empty or callback is nullptr.");
681 return ERR_DM_INPUT_PARA_INVALID;
682 }
683 LOGI("Start, pkgName: %{public}s", pkgName.c_str());
684 DmTraceStart(std::string(DM_HITRACE_AUTH_TO_CONSULT));
685
686 std::string strDeviceId = deviceInfo.deviceId;
687 DeviceManagerNotify::GetInstance().RegisterAuthenticateCallback(pkgName, strDeviceId, callback);
688 JsonObject extraJson(extra);
689 if (extraJson.IsDiscarded()) {
690 LOGE("extra bindParam invalid.");
691 return ERR_DM_INPUT_PARA_INVALID;
692 }
693 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
694 extraJson[TOKENID] = std::to_string(OHOS::IPCSkeleton::GetSelfTokenID());
695 #endif
696 std::shared_ptr<IpcAuthenticateDeviceReq> req = std::make_shared<IpcAuthenticateDeviceReq>();
697 std::shared_ptr<IpcRsp> rsp = std::make_shared<IpcRsp>();
698 req->SetPkgName(pkgName);
699 req->SetExtra(extraJson.Dump());
700 req->SetAuthType(authType);
701 req->SetDeviceInfo(deviceInfo);
702 int32_t ret = ipcClientProxy_->SendRequest(AUTHENTICATE_DEVICE, req, rsp);
703 if (ret != DM_OK) {
704 LOGE("AuthenticateDevice error: Send Request failed ret: %{public}d", ret);
705 SysEventWrite(std::string(DM_SEND_REQUEST_FAILED), DM_HISYEVENT_BEHAVIOR,
706 std::string(DM_SEND_REQUEST_FAILED_MSG));
707 return ERR_DM_IPC_SEND_REQUEST_FAILED;
708 }
709 SysEventWrite(std::string(DM_SEND_REQUEST_SUCCESS), DM_HISYEVENT_BEHAVIOR,
710 std::string(DM_SEND_REQUEST_SUCCESS_MSG));
711
712 ret = rsp->GetErrCode();
713 if (ret != DM_OK) {
714 LOGE("AuthenticateDevice error: Failed with ret %{public}d", ret);
715 return ret;
716 }
717 DmTraceEnd();
718 LOGI("Completed");
719 return DM_OK;
720 }
721
UnAuthenticateDevice(const std::string & pkgName,const DmDeviceInfo & deviceInfo)722 int32_t DeviceManagerImpl::UnAuthenticateDevice(const std::string &pkgName, const DmDeviceInfo &deviceInfo)
723 {
724 if (pkgName.empty() || (deviceInfo.networkId[0] == '\0')) {
725 LOGE("UnAuthenticateDevice error: Invalid para. pkgName %{public}s", pkgName.c_str());
726 return ERR_DM_INPUT_PARA_INVALID;
727 }
728 LOGI("Start, pkgName: %{public}s, networkId: %{public}s", pkgName.c_str(),
729 GetAnonyString(std::string(deviceInfo.networkId)).c_str());
730 std::shared_ptr<IpcUnAuthenticateDeviceReq> req = std::make_shared<IpcUnAuthenticateDeviceReq>();
731 std::shared_ptr<IpcRsp> rsp = std::make_shared<IpcRsp>();
732 req->SetPkgName(pkgName);
733 req->SetDeviceInfo(deviceInfo);
734 int32_t ret = ipcClientProxy_->SendRequest(UNAUTHENTICATE_DEVICE, req, rsp);
735 if (ret != DM_OK) {
736 LOGE("UnAuthenticateDevice error: Send Request failed ret: %{public}d", ret);
737 return ERR_DM_IPC_SEND_REQUEST_FAILED;
738 }
739 ret = rsp->GetErrCode();
740 if (ret != DM_OK) {
741 LOGE("UnAuthenticateDevice error: Failed with ret %{public}d", ret);
742 SysEventWrite(std::string(UNAUTHENTICATE_DEVICE_FAILED), DM_HISYEVENT_BEHAVIOR,
743 std::string(UNAUTHENTICATE_DEVICE_FAILED_MSG));
744 return ret;
745 }
746 SysEventWrite(std::string(UNAUTHENTICATE_DEVICE_SUCCESS), DM_HISYEVENT_BEHAVIOR,
747 std::string(UNAUTHENTICATE_DEVICE_SUCCESS_MSG));
748
749 LOGI("Completed");
750 return DM_OK;
751 }
752
StopAuthenticateDevice(const std::string & pkgName)753 int32_t DeviceManagerImpl::StopAuthenticateDevice(const std::string &pkgName)
754 {
755 if (pkgName.empty()) {
756 DmRadarHelper::GetInstance().ReportDmBehavior(pkgName, "StopAuthenticateDevice",
757 ERR_DM_INPUT_PARA_INVALID, anonyLocalUdid_);
758 LOGE("StopAuthenticateDevice error: Invalid para. pkgName %{public}s", pkgName.c_str());
759 return ERR_DM_INPUT_PARA_INVALID;
760 }
761 LOGI("Start, pkgName: %{public}s", pkgName.c_str());
762 std::shared_ptr<IpcCommonParamReq> req = std::make_shared<IpcCommonParamReq>();
763 std::shared_ptr<IpcRsp> rsp = std::make_shared<IpcRsp>();
764 req->SetPkgName(pkgName);
765 int32_t ret = ipcClientProxy_->SendRequest(STOP_AUTHENTICATE_DEVICE, req, rsp);
766 if (ret != DM_OK) {
767 DmRadarHelper::GetInstance().ReportDmBehavior(pkgName, "StopAuthenticateDevice", ret, anonyLocalUdid_);
768 LOGE("StopAuthenticateDevice error: Send Request failed ret: %{public}d", ret);
769 return ERR_DM_IPC_SEND_REQUEST_FAILED;
770 }
771 ret = rsp->GetErrCode();
772 if (ret != DM_OK) {
773 DmRadarHelper::GetInstance().ReportDmBehavior(pkgName, "StopAuthenticateDevice", ret, anonyLocalUdid_);
774 LOGE("StopAuthenticateDevice error: Failed with ret %{public}d", ret);
775 return ret;
776 }
777
778 LOGI("Completed");
779 return DM_OK;
780 }
781
RegisterDeviceManagerFaCallback(const std::string & pkgName,std::shared_ptr<DeviceManagerUiCallback> callback)782 int32_t DeviceManagerImpl::RegisterDeviceManagerFaCallback(const std::string &pkgName,
783 std::shared_ptr<DeviceManagerUiCallback> callback)
784 {
785 if (pkgName.empty() || callback == nullptr) {
786 DmRadarHelper::GetInstance().ReportDmBehavior(
787 pkgName, "RegisterDeviceManagerFaCallback", ERR_DM_INPUT_PARA_INVALID, anonyLocalUdid_);
788 LOGE("RegisterDeviceManagerFaCallback error: Invalid para");
789 return ERR_DM_INPUT_PARA_INVALID;
790 }
791
792 LOGI("Start, pkgName: %{public}s", pkgName.c_str());
793 DeviceManagerNotify::GetInstance().RegisterDeviceManagerFaCallback(pkgName, callback);
794 RegisterUiStateCallback(pkgName);
795 DmRadarHelper::GetInstance().ReportDmBehavior(pkgName, "RegisterDeviceManagerFaCallback", DM_OK, anonyLocalUdid_);
796 LOGI("Completed");
797 return DM_OK;
798 }
799
UnRegisterDeviceManagerFaCallback(const std::string & pkgName)800 int32_t DeviceManagerImpl::UnRegisterDeviceManagerFaCallback(const std::string &pkgName)
801 {
802 if (pkgName.empty()) {
803 DmRadarHelper::GetInstance().ReportDmBehavior(
804 pkgName, "UnRegisterDeviceManagerFaCallback", ERR_DM_INPUT_PARA_INVALID, anonyLocalUdid_);
805 LOGE("Invalid parameter, pkgName is empty.");
806 return ERR_DM_INPUT_PARA_INVALID;
807 }
808
809 LOGI("Start, pkgName: %{public}s", pkgName.c_str());
810 DeviceManagerNotify::GetInstance().UnRegisterDeviceManagerFaCallback(pkgName);
811 UnRegisterUiStateCallback(pkgName);
812 DmRadarHelper::GetInstance().ReportDmBehavior(pkgName, "UnRegisterDeviceManagerFaCallback",
813 DM_OK, anonyLocalUdid_);
814 LOGI("Completed");
815 return DM_OK;
816 }
817
VerifyAuthentication(const std::string & pkgName,const std::string & authPara,std::shared_ptr<VerifyAuthCallback> callback)818 int32_t DeviceManagerImpl::VerifyAuthentication(const std::string &pkgName, const std::string &authPara,
819 std::shared_ptr<VerifyAuthCallback> callback)
820 {
821 (void)pkgName;
822 (void)authPara;
823 (void)callback;
824 LOGI("VerifyAuthentication not support method.");
825 return DM_OK;
826 }
827
GetFaParam(const std::string & pkgName,DmAuthParam & dmFaParam)828 int32_t DeviceManagerImpl::GetFaParam(const std::string &pkgName, DmAuthParam &dmFaParam)
829 {
830 (void)pkgName;
831 (void)dmFaParam;
832 LOGI("GetFaParam not support method.");
833 return DM_OK;
834 }
835
SetUserOperation(const std::string & pkgName,int32_t action,const std::string & params)836 int32_t DeviceManagerImpl::SetUserOperation(const std::string &pkgName, int32_t action, const std::string ¶ms)
837 {
838 if (pkgName.empty() || params.empty()) {
839 LOGE("DeviceManager::SetUserOperation start, pkgName: %{public}s, params: %{public}s", pkgName.c_str(),
840 GetAnonyString(params).c_str());
841 return ERR_DM_INPUT_PARA_INVALID;
842 }
843 LOGI("Start, pkgName: %{public}s", pkgName.c_str());
844
845 std::shared_ptr<IpcGetOperationReq> req = std::make_shared<IpcGetOperationReq>();
846 std::shared_ptr<IpcRsp> rsp = std::make_shared<IpcRsp>();
847 req->SetPkgName(pkgName);
848 req->SetOperation(action);
849 req->SetParams(params);
850
851 int32_t ret = ipcClientProxy_->SendRequest(SERVER_USER_AUTH_OPERATION, req, rsp);
852 if (ret != DM_OK) {
853 LOGI("SetUserOperation Send Request failed ret: %{public}d", ret);
854 return ERR_DM_IPC_SEND_REQUEST_FAILED;
855 }
856
857 ret = rsp->GetErrCode();
858 if (ret != DM_OK) {
859 LOGE("CheckAuthentication Failed with ret %{public}d", ret);
860 return ret;
861 }
862 LOGI("Completed");
863 return DM_OK;
864 }
865
GetUdidByNetworkId(const std::string & pkgName,const std::string & netWorkId,std::string & udid)866 int32_t DeviceManagerImpl::GetUdidByNetworkId(const std::string &pkgName, const std::string &netWorkId,
867 std::string &udid)
868 {
869 if (pkgName.empty() || netWorkId.empty()) {
870 LOGE("DeviceManagerImpl::GetUdidByNetworkId error: Invalid para, pkgName: %{public}s, netWorkId: %{public}s",
871 pkgName.c_str(), GetAnonyString(netWorkId).c_str());
872 return ERR_DM_INPUT_PARA_INVALID;
873 }
874 LOGD("Start, pkgName: %{public}s", GetAnonyString(pkgName).c_str());
875
876 std::shared_ptr<IpcGetInfoByNetWorkReq> req = std::make_shared<IpcGetInfoByNetWorkReq>();
877 std::shared_ptr<IpcGetInfoByNetWorkRsp> rsp = std::make_shared<IpcGetInfoByNetWorkRsp>();
878 req->SetPkgName(pkgName);
879 req->SetNetWorkId(netWorkId);
880
881 int32_t ret = ipcClientProxy_->SendRequest(GET_UDID_BY_NETWORK, req, rsp);
882 if (ret != DM_OK) {
883 LOGI("GetUdidByNetworkId Send Request failed ret: %{public}d", ret);
884 return ERR_DM_IPC_SEND_REQUEST_FAILED;
885 }
886
887 ret = rsp->GetErrCode();
888 if (ret != DM_OK) {
889 LOGE("CheckAuthentication Failed with ret %{public}d", ret);
890 return ret;
891 }
892 udid = rsp->GetUdid();
893 return DM_OK;
894 }
895
GetUuidByNetworkId(const std::string & pkgName,const std::string & netWorkId,std::string & uuid)896 int32_t DeviceManagerImpl::GetUuidByNetworkId(const std::string &pkgName, const std::string &netWorkId,
897 std::string &uuid)
898 {
899 if (pkgName.empty() || netWorkId.empty()) {
900 LOGE("DeviceManagerImpl::GetUuidByNetworkId error: Invalid para, pkgName: %{public}s, netWorkId: %{public}s",
901 pkgName.c_str(), GetAnonyString(netWorkId).c_str());
902 return ERR_DM_INPUT_PARA_INVALID;
903 }
904 LOGI("Start, pkgName: %{public}s", GetAnonyString(pkgName).c_str());
905
906 std::shared_ptr<IpcGetInfoByNetWorkReq> req = std::make_shared<IpcGetInfoByNetWorkReq>();
907 std::shared_ptr<IpcGetInfoByNetWorkRsp> rsp = std::make_shared<IpcGetInfoByNetWorkRsp>();
908 req->SetPkgName(pkgName);
909 req->SetNetWorkId(netWorkId);
910
911 int32_t ret = ipcClientProxy_->SendRequest(GET_UUID_BY_NETWORK, req, rsp);
912 if (ret != DM_OK) {
913 LOGI("GetUuidByNetworkId Send Request failed ret: %{public}d", ret);
914 return ERR_DM_IPC_SEND_REQUEST_FAILED;
915 }
916
917 ret = rsp->GetErrCode();
918 if (ret != DM_OK) {
919 LOGE("CheckAuthentication Failed with ret %{public}d", ret);
920 return ret;
921 }
922 uuid = rsp->GetUuid();
923 return DM_OK;
924 }
925
RegisterDevStateCallback(const std::string & pkgName,const std::string & extra)926 int32_t DeviceManagerImpl::RegisterDevStateCallback(const std::string &pkgName, const std::string &extra)
927 {
928 (void)pkgName;
929 (void)extra;
930 LOGI("RegisterDevStateCallback not support method.");
931 return DM_OK;
932 }
933
UnRegisterDevStateCallback(const std::string & pkgName,const std::string & extra)934 int32_t DeviceManagerImpl::UnRegisterDevStateCallback(const std::string &pkgName, const std::string &extra)
935 {
936 (void)pkgName;
937 (void)extra;
938 LOGI("UnRegisterDevStateCallback not support method.");
939 return DM_OK;
940 }
941
RegisterUiStateCallback(const std::string & pkgName)942 int32_t DeviceManagerImpl::RegisterUiStateCallback(const std::string &pkgName)
943 {
944 if (pkgName.empty()) {
945 DmRadarHelper::GetInstance().ReportDmBehavior(pkgName, "RegisterUiStateCallback",
946 ERR_DM_INPUT_PARA_INVALID, anonyLocalUdid_);
947 LOGE("Invalid parameter, pkgName is empty.");
948 return ERR_DM_INPUT_PARA_INVALID;
949 }
950 LOGI("Start, pkgName: %{public}s", pkgName.c_str());
951
952 std::shared_ptr<IpcReq> req = std::make_shared<IpcReq>();
953 std::shared_ptr<IpcRsp> rsp = std::make_shared<IpcRsp>();
954 req->SetPkgName(pkgName);
955
956 int32_t ret = ipcClientProxy_->SendRequest(REGISTER_UI_STATE_CALLBACK, req, rsp);
957 if (ret != DM_OK) {
958 DmRadarHelper::GetInstance().ReportDmBehavior(pkgName, "RegisterUiStateCallback", ret, anonyLocalUdid_);
959 LOGI("RegisterUiStateCallback Send Request failed ret: %{public}d", ret);
960 return ERR_DM_IPC_SEND_REQUEST_FAILED;
961 }
962
963 ret = rsp->GetErrCode();
964 if (ret != DM_OK) {
965 DmRadarHelper::GetInstance().ReportDmBehavior(pkgName, "RegisterUiStateCallback", ret, anonyLocalUdid_);
966 LOGE("RegisterUiStateCallback Failed with ret %{public}d", ret);
967 return ret;
968 }
969 DmRadarHelper::GetInstance().ReportDmBehavior(pkgName, "RegisterUiStateCallback", DM_OK, anonyLocalUdid_);
970 return DM_OK;
971 }
972
UnRegisterUiStateCallback(const std::string & pkgName)973 int32_t DeviceManagerImpl::UnRegisterUiStateCallback(const std::string &pkgName)
974 {
975 if (pkgName.empty()) {
976 DmRadarHelper::GetInstance().ReportDmBehavior(
977 pkgName, "UnRegisterUiStateCallback", ERR_DM_INPUT_PARA_INVALID, anonyLocalUdid_);
978 LOGE("Invalid parameter, pkgName is empty.");
979 return ERR_DM_INPUT_PARA_INVALID;
980 }
981
982 std::shared_ptr<IpcReq> req = std::make_shared<IpcReq>();
983 std::shared_ptr<IpcRsp> rsp = std::make_shared<IpcRsp>();
984 req->SetPkgName(pkgName);
985
986 int32_t ret = ipcClientProxy_->SendRequest(UNREGISTER_UI_STATE_CALLBACK, req, rsp);
987 if (ret != DM_OK) {
988 DmRadarHelper::GetInstance().ReportDmBehavior(pkgName, "UnRegisterUiStateCallback", ret, anonyLocalUdid_);
989 LOGI("UnRegisterUiStateCallback Send Request failed ret: %{public}d", ret);
990 return ERR_DM_IPC_SEND_REQUEST_FAILED;
991 }
992
993 ret = rsp->GetErrCode();
994 if (ret != DM_OK) {
995 DmRadarHelper::GetInstance().ReportDmBehavior(pkgName, "UnRegisterUiStateCallback", ret, anonyLocalUdid_);
996 LOGE("UnRegisterUiStateCallback Failed with ret %{public}d", ret);
997 return ret;
998 }
999 DmRadarHelper::GetInstance().ReportDmBehavior(pkgName, "UnRegisterUiStateCallback", DM_OK, anonyLocalUdid_);
1000 return DM_OK;
1001 }
1002
RequestCredential(const std::string & pkgName,const std::string & reqJsonStr,std::string & returnJsonStr)1003 int32_t DeviceManagerImpl::RequestCredential(const std::string &pkgName, const std::string &reqJsonStr,
1004 std::string &returnJsonStr)
1005 {
1006 if (pkgName.empty() || reqJsonStr.empty()) {
1007 LOGE("DeviceManagerImpl::RequestCredential error: Invalid para, pkgName is %{public}s, reqJsonStr is"
1008 "%{public}s", pkgName.c_str(), GetAnonyString(reqJsonStr).c_str());
1009 return ERR_DM_INPUT_PARA_INVALID;
1010 }
1011 LOGI("start.");
1012 std::map<std::string, std::string> requestParam;
1013 requestParam.emplace(DM_CREDENTIAL_TYPE, DM_TYPE_OH);
1014 requestParam.emplace(DM_CREDENTIAL_REQJSONSTR, reqJsonStr);
1015 std::string reqParaStr = ConvertMapToJsonString(requestParam);
1016 std::shared_ptr<IpcSetCredentialReq> req = std::make_shared<IpcSetCredentialReq>();
1017 std::shared_ptr<IpcSetCredentialRsp> rsp = std::make_shared<IpcSetCredentialRsp>();
1018 req->SetPkgName(pkgName);
1019 req->SetCredentialParam(reqParaStr);
1020
1021 int32_t ret = ipcClientProxy_->SendRequest(REQUEST_CREDENTIAL, req, rsp);
1022 if (ret != DM_OK) {
1023 LOGI("RequestCredential Send Request failed ret: %{public}d", ret);
1024 return ERR_DM_IPC_SEND_REQUEST_FAILED;
1025 }
1026
1027 ret = rsp->GetErrCode();
1028 if (ret != DM_OK) {
1029 LOGE("failed to get return errcode while request credential.");
1030 return ret;
1031 }
1032 returnJsonStr = rsp->GetCredentialResult();
1033 LOGI("completed.");
1034 return DM_OK;
1035 }
1036
ImportCredential(const std::string & pkgName,const std::string & credentialInfo)1037 int32_t DeviceManagerImpl::ImportCredential(const std::string &pkgName, const std::string &credentialInfo)
1038 {
1039 if (pkgName.empty() || credentialInfo.empty()) {
1040 LOGE("DeviceManagerImpl::ImportCredential failed, pkgName is %{public}s, credentialInfo is %{public}s",
1041 pkgName.c_str(), GetAnonyString(credentialInfo).c_str());
1042 return ERR_DM_INPUT_PARA_INVALID;
1043 }
1044 LOGI("start.");
1045 std::map<std::string, std::string> requestParam;
1046 requestParam.emplace(DM_CREDENTIAL_TYPE, DM_TYPE_OH);
1047 requestParam.emplace(DM_CREDENTIAL_REQJSONSTR, credentialInfo);
1048 std::string reqParaStr = ConvertMapToJsonString(requestParam);
1049 std::shared_ptr<IpcSetCredentialReq> req = std::make_shared<IpcSetCredentialReq>();
1050 std::shared_ptr<IpcRsp> rsp = std::make_shared<IpcRsp>();
1051 req->SetPkgName(pkgName);
1052 req->SetCredentialParam(reqParaStr);
1053
1054 int32_t ret = ipcClientProxy_->SendRequest(IMPORT_CREDENTIAL, req, rsp);
1055 if (ret != DM_OK) {
1056 LOGI("ImportCredential Send Request failed ret: %{public}d", ret);
1057 return ERR_DM_IPC_SEND_REQUEST_FAILED;
1058 }
1059
1060 ret = rsp->GetErrCode();
1061 if (ret != DM_OK) {
1062 LOGE("failed to get return errcode while import credential.");
1063 return ret;
1064 }
1065 LOGI("completed.");
1066 return DM_OK;
1067 }
1068
DeleteCredential(const std::string & pkgName,const std::string & deleteInfo)1069 int32_t DeviceManagerImpl::DeleteCredential(const std::string &pkgName, const std::string &deleteInfo)
1070 {
1071 if (pkgName.empty() || deleteInfo.empty()) {
1072 LOGE("DeviceManagerImpl::DeleteCredential failed, pkgName is %{public}s, deleteInfo is %{public}s",
1073 pkgName.c_str(), GetAnonyString(deleteInfo).c_str());
1074 return ERR_DM_INPUT_PARA_INVALID;
1075 }
1076 LOGI("start.");
1077 std::map<std::string, std::string> requestParam;
1078 requestParam.emplace(DM_CREDENTIAL_TYPE, DM_TYPE_OH);
1079 requestParam.emplace(DM_CREDENTIAL_REQJSONSTR, deleteInfo);
1080 std::string reqParaStr = ConvertMapToJsonString(requestParam);
1081 std::shared_ptr<IpcSetCredentialReq> req = std::make_shared<IpcSetCredentialReq>();
1082 std::shared_ptr<IpcRsp> rsp = std::make_shared<IpcRsp>();
1083 req->SetPkgName(pkgName);
1084 req->SetCredentialParam(reqParaStr);
1085
1086 int32_t ret = ipcClientProxy_->SendRequest(DELETE_CREDENTIAL, req, rsp);
1087 if (ret != DM_OK) {
1088 LOGI("DeleteCredential Send Request failed ret: %{public}d", ret);
1089 return ERR_DM_IPC_SEND_REQUEST_FAILED;
1090 }
1091
1092 ret = rsp->GetErrCode();
1093 if (ret != DM_OK) {
1094 LOGE("failed to get return errcode while import credential.");
1095 return ret;
1096 }
1097 LOGI("completed.");
1098 return DM_OK;
1099 }
1100
RegisterCredentialCallback(const std::string & pkgName,std::shared_ptr<CredentialCallback> callback)1101 int32_t DeviceManagerImpl::RegisterCredentialCallback(const std::string &pkgName,
1102 std::shared_ptr<CredentialCallback> callback)
1103 {
1104 if (pkgName.empty() || callback == nullptr) {
1105 LOGE("RegisterCredentialCallback error: Invalid para");
1106 return ERR_DM_INPUT_PARA_INVALID;
1107 }
1108
1109 LOGI("Start, pkgName: %{public}s", pkgName.c_str());
1110 DeviceManagerNotify::GetInstance().RegisterCredentialCallback(pkgName, callback);
1111 std::shared_ptr<IpcReq> req = std::make_shared<IpcReq>();
1112 std::shared_ptr<IpcRsp> rsp = std::make_shared<IpcRsp>();
1113 req->SetPkgName(pkgName);
1114
1115 int32_t ret = ipcClientProxy_->SendRequest(REGISTER_CREDENTIAL_CALLBACK, req, rsp);
1116 if (ret != DM_OK) {
1117 LOGI("RegisterCredentialCallback Send Request failed ret: %{public}d", ret);
1118 return ERR_DM_IPC_SEND_REQUEST_FAILED;
1119 }
1120
1121 ret = rsp->GetErrCode();
1122 if (ret != DM_OK) {
1123 LOGE("RegisterCredentialCallback error: Failed with ret %{public}d", ret);
1124 return ret;
1125 }
1126 LOGI("Completed");
1127 return DM_OK;
1128 }
1129
UnRegisterCredentialCallback(const std::string & pkgName)1130 int32_t DeviceManagerImpl::UnRegisterCredentialCallback(const std::string &pkgName)
1131 {
1132 if (pkgName.empty()) {
1133 LOGE("Invalid parameter, pkgName is empty.");
1134 return ERR_DM_INPUT_PARA_INVALID;
1135 }
1136 LOGI("Start, pkgName: %{public}s", pkgName.c_str());
1137
1138 DeviceManagerNotify::GetInstance().UnRegisterCredentialCallback(pkgName);
1139 std::shared_ptr<IpcReq> req = std::make_shared<IpcReq>();
1140 std::shared_ptr<IpcRsp> rsp = std::make_shared<IpcRsp>();
1141 req->SetPkgName(pkgName);
1142
1143 int32_t ret = ipcClientProxy_->SendRequest(UNREGISTER_CREDENTIAL_CALLBACK, req, rsp);
1144 if (ret != DM_OK) {
1145 LOGI("UnRegisterCredentialCallback Send Request failed ret: %{public}d", ret);
1146 return ERR_DM_IPC_SEND_REQUEST_FAILED;
1147 }
1148
1149 ret = rsp->GetErrCode();
1150 if (ret != DM_OK) {
1151 LOGE("UnRegisterCredentialCallback Failed with ret %{public}d", ret);
1152 return ret;
1153 }
1154 LOGI("Completed");
1155 return DM_OK;
1156 }
1157
NotifyEvent(const std::string & pkgName,const int32_t eventId,const std::string & event)1158 int32_t DeviceManagerImpl::NotifyEvent(const std::string &pkgName, const int32_t eventId, const std::string &event)
1159 {
1160 if (pkgName.empty()) {
1161 DmRadarHelper::GetInstance().ReportDmBehavior(pkgName, "NotifyEvent",
1162 ERR_DM_INPUT_PARA_INVALID, anonyLocalUdid_);
1163 LOGE("NotifyEvent error: pkgName empty");
1164 return ERR_DM_INPUT_PARA_INVALID;
1165 }
1166 if ((eventId <= DM_NOTIFY_EVENT_START) || (eventId >= DM_NOTIFY_EVENT_BUTT)) {
1167 LOGE("NotifyEvent eventId invalid");
1168 DmRadarHelper::GetInstance().ReportDmBehavior(pkgName, "NotifyEvent",
1169 ERR_DM_INPUT_PARA_INVALID, anonyLocalUdid_);
1170 return ERR_DM_INPUT_PARA_INVALID;
1171 }
1172 LOGI("Start, pkgName: %{public}s", pkgName.c_str());
1173 std::shared_ptr<IpcNotifyEventReq> req = std::make_shared<IpcNotifyEventReq>();
1174 std::shared_ptr<IpcRsp> rsp = std::make_shared<IpcRsp>();
1175 req->SetPkgName(pkgName);
1176 req->SetEventId(eventId);
1177 req->SetEvent(event);
1178
1179 int32_t ret = ipcClientProxy_->SendRequest(NOTIFY_EVENT, req, rsp);
1180 if (ret != DM_OK) {
1181 DmRadarHelper::GetInstance().ReportDmBehavior(pkgName, "NotifyEvent", ret, anonyLocalUdid_);
1182 LOGI("NotifyEvent Send Request failed ret: %{public}d", ret);
1183 return ERR_DM_IPC_SEND_REQUEST_FAILED;
1184 }
1185
1186 ret = rsp->GetErrCode();
1187 if (ret != DM_OK) {
1188 DmRadarHelper::GetInstance().ReportDmBehavior(pkgName, "NotifyEvent", ret, anonyLocalUdid_);
1189 LOGE("NotifyEvent failed with ret %{public}d", ret);
1190 return ret;
1191 }
1192 DmRadarHelper::GetInstance().ReportDmBehavior(pkgName, "NotifyEvent", DM_OK, anonyLocalUdid_);
1193 LOGI("Completed");
1194 return DM_OK;
1195 }
1196
RequestCredential(const std::string & pkgName,std::string & returnJsonStr)1197 int32_t DeviceManagerImpl::RequestCredential(const std::string &pkgName, std::string &returnJsonStr)
1198 {
1199 if (pkgName.empty()) {
1200 LOGE("RequestCredential failed, pkgName is empty.");
1201 return ERR_DM_INPUT_PARA_INVALID;
1202 }
1203 LOGI("Start, pkgName: %{public}s", pkgName.c_str());
1204 std::map<std::string, std::string> requestParam;
1205 requestParam.emplace(DM_CREDENTIAL_TYPE, DM_TYPE_MINE);
1206 std::string reqParaStr = ConvertMapToJsonString(requestParam);
1207 std::shared_ptr<IpcSetCredentialReq> req = std::make_shared<IpcSetCredentialReq>();
1208 std::shared_ptr<IpcSetCredentialRsp> rsp = std::make_shared<IpcSetCredentialRsp>();
1209 req->SetPkgName(pkgName);
1210 req->SetCredentialParam(reqParaStr);
1211 int32_t ret = ipcClientProxy_->SendRequest(REQUEST_CREDENTIAL, req, rsp);
1212 if (ret != DM_OK) {
1213 LOGI("RequestCredential Send Request failed ret: %{public}d", ret);
1214 return ERR_DM_IPC_SEND_REQUEST_FAILED;
1215 }
1216 ret = rsp->GetErrCode();
1217 if (ret != DM_OK) {
1218 LOGE("failed to get return errcode while request credential.");
1219 return ret;
1220 }
1221 returnJsonStr = rsp->GetCredentialResult();
1222 LOGI("Completed.");
1223 return DM_OK;
1224 }
1225
CheckCredential(const std::string & pkgName,const std::string & reqJsonStr,std::string & returnJsonStr)1226 int32_t DeviceManagerImpl::CheckCredential(const std::string &pkgName, const std::string &reqJsonStr,
1227 std::string &returnJsonStr)
1228 {
1229 if (pkgName.empty() || reqJsonStr.empty()) {
1230 LOGE("Check the credential is invalid para.");
1231 return ERR_DM_INPUT_PARA_INVALID;
1232 }
1233 LOGI("Start, pkgName: %{public}s", pkgName.c_str());
1234 std::shared_ptr<IpcSetCredentialReq> req = std::make_shared<IpcSetCredentialReq>();
1235 std::shared_ptr<IpcSetCredentialRsp> rsp = std::make_shared<IpcSetCredentialRsp>();
1236 req->SetPkgName(pkgName);
1237 req->SetCredentialParam(reqJsonStr);
1238
1239 int32_t ret = ipcClientProxy_->SendRequest(SERVER_GET_DMFA_INFO, req, rsp);
1240 if (ret != DM_OK) {
1241 LOGI("CheckCredential Send Request failed ret: %{public}d", ret);
1242 return ERR_DM_IPC_SEND_REQUEST_FAILED;
1243 }
1244
1245 ret = rsp->GetErrCode();
1246 if (ret != DM_OK) {
1247 LOGE("failed to get return errcode while Check credential.");
1248 return ret;
1249 }
1250 returnJsonStr = rsp->GetCredentialResult();
1251 LOGI("Completed.");
1252 return DM_OK;
1253 }
1254
ImportCredential(const std::string & pkgName,const std::string & reqJsonStr,std::string & returnJsonStr)1255 int32_t DeviceManagerImpl::ImportCredential(const std::string &pkgName, const std::string &reqJsonStr,
1256 std::string &returnJsonStr)
1257 {
1258 if (pkgName.empty() || reqJsonStr.empty()) {
1259 LOGE("import the credential is invalid para.");
1260 return ERR_DM_INPUT_PARA_INVALID;
1261 }
1262 LOGI("Start, pkgName: %{public}s", pkgName.c_str());
1263 std::map<std::string, std::string> requestParam;
1264 requestParam.emplace(DM_CREDENTIAL_TYPE, DM_TYPE_MINE);
1265 requestParam.emplace(DM_CREDENTIAL_REQJSONSTR, reqJsonStr);
1266 std::string reqParaStr = ConvertMapToJsonString(requestParam);
1267 std::shared_ptr<IpcSetCredentialReq> req = std::make_shared<IpcSetCredentialReq>();
1268 std::shared_ptr<IpcSetCredentialRsp> rsp = std::make_shared<IpcSetCredentialRsp>();
1269 req->SetPkgName(pkgName);
1270 req->SetCredentialParam(reqParaStr);
1271
1272 int32_t ret = ipcClientProxy_->SendRequest(IMPORT_CREDENTIAL, req, rsp);
1273 if (ret != DM_OK) {
1274 LOGI("ImportCredential Send Request failed ret: %{public}d", ret);
1275 return ERR_DM_IPC_SEND_REQUEST_FAILED;
1276 }
1277
1278 ret = rsp->GetErrCode();
1279 if (ret != DM_OK) {
1280 LOGE("failed to get return errcode while import credential.");
1281 return ret;
1282 }
1283 returnJsonStr = rsp->GetCredentialResult();
1284 LOGI("Completed.");
1285 return DM_OK;
1286 }
1287
DeleteCredential(const std::string & pkgName,const std::string & reqJsonStr,std::string & returnJsonStr)1288 int32_t DeviceManagerImpl::DeleteCredential(const std::string &pkgName, const std::string &reqJsonStr,
1289 std::string &returnJsonStr)
1290 {
1291 if (pkgName.empty() || reqJsonStr.empty()) {
1292 LOGE("Delete the credential is invalid para.");
1293 return ERR_DM_INPUT_PARA_INVALID;
1294 }
1295 LOGI("Start, pkgName: %{public}s", pkgName.c_str());
1296 std::map<std::string, std::string> requestParam;
1297 requestParam.emplace(DM_CREDENTIAL_TYPE, DM_TYPE_MINE);
1298 requestParam.emplace(DM_CREDENTIAL_REQJSONSTR, reqJsonStr);
1299 std::string reqParaStr = ConvertMapToJsonString(requestParam);
1300 std::shared_ptr<IpcSetCredentialReq> req = std::make_shared<IpcSetCredentialReq>();
1301 std::shared_ptr<IpcSetCredentialRsp> rsp = std::make_shared<IpcSetCredentialRsp>();
1302 req->SetPkgName(pkgName);
1303 req->SetCredentialParam(reqParaStr);
1304
1305 int32_t ret = ipcClientProxy_->SendRequest(DELETE_CREDENTIAL, req, rsp);
1306 if (ret != DM_OK) {
1307 LOGI("DeleteCredential Send Request failed ret: %{public}d", ret);
1308 return ERR_DM_IPC_SEND_REQUEST_FAILED;
1309 }
1310
1311 ret = rsp->GetErrCode();
1312 if (ret != DM_OK) {
1313 LOGE("failed to get return errcode while import credential.");
1314 return ret;
1315 }
1316 returnJsonStr = rsp->GetCredentialResult();
1317 LOGI("Completed.");
1318 return DM_OK;
1319 }
1320
OnDmServiceDied()1321 int32_t DeviceManagerImpl::OnDmServiceDied()
1322 {
1323 LOGI("Start");
1324 int32_t ret = ipcClientProxy_->OnDmServiceDied();
1325 if (ret != DM_OK) {
1326 LOGE("OnDmServiceDied failed, ret: %{public}d", ret);
1327 return ERR_DM_FAILED;
1328 }
1329 return DM_OK;
1330 }
1331
GetEncryptedUuidByNetworkId(const std::string & pkgName,const std::string & networkId,std::string & uuid)1332 int32_t DeviceManagerImpl::GetEncryptedUuidByNetworkId(const std::string &pkgName, const std::string &networkId,
1333 std::string &uuid)
1334 {
1335 if (pkgName.empty() || networkId.empty()) {
1336 LOGE("DeviceManagerImpl::GetEncryptedUuidByNetworkId error: Invalid para, pkgName: %{public}s, netWorkId:"
1337 "%{public}s", pkgName.c_str(), GetAnonyString(networkId).c_str());
1338 return ERR_DM_INPUT_PARA_INVALID;
1339 }
1340 LOGI("Start, pkgName: %{public}s", pkgName.c_str());
1341
1342 std::shared_ptr<IpcGetInfoByNetWorkReq> req = std::make_shared<IpcGetInfoByNetWorkReq>();
1343 std::shared_ptr<IpcGetInfoByNetWorkRsp> rsp = std::make_shared<IpcGetInfoByNetWorkRsp>();
1344 req->SetPkgName(pkgName);
1345 req->SetNetWorkId(networkId);
1346
1347 int32_t ret = ipcClientProxy_->SendRequest(GET_ENCRYPTED_UUID_BY_NETWOEKID, req, rsp);
1348 if (ret != DM_OK) {
1349 LOGI("GetEncryptedUuidByNetworkId Send Request failed ret: %{public}d", ret);
1350 return ERR_DM_IPC_SEND_REQUEST_FAILED;
1351 }
1352
1353 ret = rsp->GetErrCode();
1354 if (ret != DM_OK) {
1355 LOGE("CheckAuthentication Failed with ret %{public}d", ret);
1356 return ret;
1357 }
1358 uuid = rsp->GetUuid();
1359 LOGI("Complete, uuid: %{public}s", GetAnonyString(uuid).c_str());
1360 return DM_OK;
1361 }
1362
GenerateEncryptedUuid(const std::string & pkgName,const std::string & uuid,const std::string & appId,std::string & encryptedUuid)1363 int32_t DeviceManagerImpl::GenerateEncryptedUuid(const std::string &pkgName, const std::string &uuid,
1364 const std::string &appId, std::string &encryptedUuid)
1365 {
1366 if (pkgName.empty() || uuid.empty()) {
1367 LOGE("DeviceManagerImpl::GenerateEncryptedUuid error: Invalid para, pkgName: %{public}s", pkgName.c_str());
1368 return ERR_DM_INPUT_PARA_INVALID;
1369 }
1370 LOGI("Start, pkgName: %{public}s", pkgName.c_str());
1371
1372 std::shared_ptr<IpcGenerateEncryptedUuidReq> req = std::make_shared<IpcGenerateEncryptedUuidReq>();
1373 std::shared_ptr<IpcGetInfoByNetWorkRsp> rsp = std::make_shared<IpcGetInfoByNetWorkRsp>();
1374 req->SetPkgName(pkgName);
1375 req->SetUuid(uuid);
1376 req->SetAppId(appId);
1377
1378 int32_t ret = ipcClientProxy_->SendRequest(GENERATE_ENCRYPTED_UUID, req, rsp);
1379 if (ret != DM_OK) {
1380 LOGI("GenerateEncryptedUuid Send Request failed ret: %{public}d", ret);
1381 return ERR_DM_IPC_SEND_REQUEST_FAILED;
1382 }
1383
1384 ret = rsp->GetErrCode();
1385 if (ret != DM_OK) {
1386 LOGE("CheckAuthentication Failed with ret %{public}d", ret);
1387 return ret;
1388 }
1389 encryptedUuid = rsp->GetUuid();
1390 LOGI("Complete, encryptedUuid: %{public}s", GetAnonyString(encryptedUuid).c_str());
1391 return DM_OK;
1392 }
1393
CheckAPIAccessPermission()1394 int32_t DeviceManagerImpl::CheckAPIAccessPermission()
1395 {
1396 LOGI("Start");
1397 return CheckApiPermission(SYSTEM_BASIC);
1398 }
1399
CheckNewAPIAccessPermission()1400 int32_t DeviceManagerImpl::CheckNewAPIAccessPermission()
1401 {
1402 LOGI("Start");
1403 return CheckApiPermission(NORMAL);
1404 }
1405
GetLocalDeviceNetWorkId(const std::string & pkgName,std::string & networkId)1406 int32_t DeviceManagerImpl::GetLocalDeviceNetWorkId(const std::string &pkgName, std::string &networkId)
1407 {
1408 LOGI("Start, pkgName: %{public}s", pkgName.c_str());
1409 DmDeviceInfo info;
1410 int32_t ret = GetLocalDeviceInfo(pkgName, info);
1411 if (ret != DM_OK) {
1412 DmRadarHelper::GetInstance().ReportGetLocalDevInfo(pkgName, "GetLocalDeviceNetWorkId",
1413 info, ret, anonyLocalUdid_);
1414 LOGI("DeviceManagerImpl::GetLocalDeviceNetWorkId failed.");
1415 return ret;
1416 }
1417 networkId = std::string(info.networkId);
1418 LOGI("End, networkId : %{public}s", GetAnonyString(networkId).c_str());
1419 DmRadarHelper::GetInstance().ReportGetLocalDevInfo(pkgName, "GetLocalDeviceNetWorkId",
1420 info, DM_OK, anonyLocalUdid_);
1421 return DM_OK;
1422 }
1423
GetLocalDeviceId(const std::string & pkgName,std::string & deviceId)1424 int32_t DeviceManagerImpl::GetLocalDeviceId(const std::string &pkgName, std::string &deviceId)
1425 {
1426 LOGI("Start, pkgName: %{public}s", pkgName.c_str());
1427 DmDeviceInfo info;
1428 int32_t ret = GetLocalDeviceInfo(pkgName, info);
1429 if (ret != DM_OK) {
1430 DmRadarHelper::GetInstance().ReportGetLocalDevInfo(pkgName, "GetLocalDeviceId", info, ret, anonyLocalUdid_);
1431 LOGI("DeviceManagerImpl::GetLocalDeviceNetWorkId failed.");
1432 return ret;
1433 }
1434 deviceId = std::string(info.deviceId);
1435 LOGI("End, deviceId : %{public}s", GetAnonyString(deviceId).c_str());
1436 DmRadarHelper::GetInstance().ReportGetLocalDevInfo(pkgName, "GetLocalDeviceId", info, DM_OK, anonyLocalUdid_);
1437 return DM_OK;
1438 }
1439
GetLocalDeviceName(const std::string & pkgName,std::string & deviceName)1440 int32_t DeviceManagerImpl::GetLocalDeviceName(const std::string &pkgName, std::string &deviceName)
1441 {
1442 LOGI("Start, pkgName: %{public}s", pkgName.c_str());
1443 std::shared_ptr<IpcReq> req = std::make_shared<IpcReq>();
1444 std::shared_ptr<IpcGetLocalDeviceNameRsp> rsp = std::make_shared<IpcGetLocalDeviceNameRsp>();
1445 int32_t ret = ipcClientProxy_->SendRequest(GET_LOCAL_DEVICE_NAME_OLD, req, rsp);
1446 if (ret != DM_OK) {
1447 LOGE("Send Request failed ret: %{public}d", ret);
1448 return ERR_DM_IPC_SEND_REQUEST_FAILED;
1449 }
1450 ret = rsp->GetErrCode();
1451 if (ret != DM_OK) {
1452 LOGE("Get local device name failed ret: %{public}d", ret);
1453 return ret;
1454 }
1455 deviceName = rsp->GetLocalDeviceName();
1456 LOGI("End, deviceName : %{public}s", GetAnonyString(deviceName).c_str());
1457 return DM_OK;
1458 }
1459
GetLocalDeviceName(std::string & deviceName)1460 int32_t DeviceManagerImpl::GetLocalDeviceName(std::string &deviceName)
1461 {
1462 std::shared_ptr<IpcReq> req = std::make_shared<IpcReq>();
1463 std::shared_ptr<IpcGetLocalDeviceNameRsp> rsp = std::make_shared<IpcGetLocalDeviceNameRsp>();
1464 int32_t ret = ipcClientProxy_->SendRequest(GET_LOCAL_DEVICE_NAME, req, rsp);
1465 if (ret != DM_OK) {
1466 LOGE("Send Request failed ret: %{public}d", ret);
1467 return ERR_DM_IPC_SEND_REQUEST_FAILED;
1468 }
1469 ret = rsp->GetErrCode();
1470 if (ret != DM_OK) {
1471 LOGE("Get local device name failed ret: %{public}d", ret);
1472 return ret;
1473 }
1474 deviceName = rsp->GetLocalDeviceName();
1475 return DM_OK;
1476 }
1477
GetLocalDeviceType(const std::string & pkgName,int32_t & deviceType)1478 int32_t DeviceManagerImpl::GetLocalDeviceType(const std::string &pkgName, int32_t &deviceType)
1479 {
1480 LOGI("Start, pkgName : %{public}s", pkgName.c_str());
1481 DmDeviceInfo info;
1482 int32_t ret = GetLocalDeviceInfo(pkgName, info);
1483 if (ret != DM_OK) {
1484 DmRadarHelper::GetInstance().ReportGetLocalDevInfo(pkgName, "GetLocalDeviceType", info, ret, anonyLocalUdid_);
1485 LOGI("DeviceManagerImpl::GetLocalDeviceNetWorkId failed.");
1486 return ret;
1487 }
1488 deviceType = info.deviceTypeId;
1489 LOGI("End, deviceType : %{public}d", deviceType);
1490 DmRadarHelper::GetInstance().ReportGetLocalDevInfo(pkgName, "GetLocalDeviceType", info, DM_OK, anonyLocalUdid_);
1491 return DM_OK;
1492 }
1493
GetDeviceName(const std::string & pkgName,const std::string & networkId,std::string & deviceName)1494 int32_t DeviceManagerImpl::GetDeviceName(const std::string &pkgName, const std::string &networkId,
1495 std::string &deviceName)
1496 {
1497 DmDeviceInfo deviceInfo;
1498 int32_t ret = GetDeviceInfo(pkgName, networkId, deviceInfo);
1499 if (ret != DM_OK) {
1500 DmRadarHelper::GetInstance().ReportGetDeviceInfo(pkgName, "GetDeviceName", deviceInfo, ret, anonyLocalUdid_);
1501 LOGE("DeviceManagerImpl::GetDeviceName error, failed ret: %{public}d", ret);
1502 return ret;
1503 }
1504 deviceName = std::string(deviceInfo.deviceName);
1505 LOGI("End, pkgName : %{public}s, networkId : %{public}s, deviceName = %{public}s",
1506 pkgName.c_str(), GetAnonyString(networkId).c_str(), GetAnonyString(deviceName).c_str());
1507 DmRadarHelper::GetInstance().ReportGetDeviceInfo(pkgName, "GetDeviceName", deviceInfo, DM_OK, anonyLocalUdid_);
1508 return DM_OK;
1509 }
1510
GetDeviceType(const std::string & pkgName,const std::string & networkId,int32_t & deviceType)1511 int32_t DeviceManagerImpl::GetDeviceType(const std::string &pkgName, const std::string &networkId, int32_t &deviceType)
1512 {
1513 DmDeviceInfo deviceInfo;
1514 int32_t ret = GetDeviceInfo(pkgName, networkId, deviceInfo);
1515 if (ret != DM_OK) {
1516 DmRadarHelper::GetInstance().ReportGetDeviceInfo(pkgName, "GetDeviceType", deviceInfo, ret, anonyLocalUdid_);
1517 LOGE("DeviceManagerImpl::GetDeviceType error, failed ret: %{public}d", ret);
1518 return ret;
1519 }
1520 deviceType = deviceInfo.deviceTypeId;
1521 LOGI("End, pkgName : %{public}s, networkId : %{public}s, deviceType = %{public}d",
1522 pkgName.c_str(), GetAnonyString(networkId).c_str(), deviceType);
1523 DmRadarHelper::GetInstance().ReportGetDeviceInfo(pkgName, "GetDeviceType", deviceInfo, DM_OK, anonyLocalUdid_);
1524 return DM_OK;
1525 }
1526
BindDevice(const std::string & pkgName,int32_t bindType,const std::string & deviceId,const std::string & bindParam,std::shared_ptr<AuthenticateCallback> callback)1527 int32_t DeviceManagerImpl::BindDevice(const std::string &pkgName, int32_t bindType, const std::string &deviceId,
1528 const std::string &bindParam, std::shared_ptr<AuthenticateCallback> callback)
1529 {
1530 if (pkgName.empty() || deviceId.empty()) {
1531 LOGE("BindDevice error: Invalid para. pkgName : %{public}s", pkgName.c_str());
1532 return ERR_DM_INPUT_PARA_INVALID;
1533 }
1534 LOGI("start, pkgName: %{public}s", pkgName.c_str());
1535 JsonObject paramJson(bindParam);
1536 if (paramJson.IsDiscarded()) {
1537 LOGE("BindDevice bindParam invalid.");
1538 return ERR_DM_INPUT_PARA_INVALID;
1539 }
1540 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
1541 paramJson[TOKENID] = std::to_string(OHOS::IPCSkeleton::GetSelfTokenID());
1542 #endif
1543 DeviceManagerNotify::GetInstance().RegisterAuthenticateCallback(pkgName, deviceId, callback);
1544 std::shared_ptr<IpcBindDeviceReq> req = std::make_shared<IpcBindDeviceReq>();
1545 std::shared_ptr<IpcRsp> rsp = std::make_shared<IpcRsp>();
1546 req->SetPkgName(pkgName);
1547 req->SetBindParam(paramJson.Dump());
1548 req->SetBindType(bindType);
1549 req->SetDeviceId(deviceId);
1550 int32_t ret = ipcClientProxy_->SendRequest(BIND_DEVICE, req, rsp);
1551 if (ret != DM_OK) {
1552 LOGE("BindDevice error: Send Request failed ret: %{public}d", ret);
1553 return ERR_DM_IPC_SEND_REQUEST_FAILED;
1554 }
1555
1556 ret = rsp->GetErrCode();
1557 if (ret != DM_OK) {
1558 LOGE("BindDevice error: Failed with ret %{public}d", ret);
1559 return ret;
1560 }
1561 DmTraceEnd();
1562 LOGI("End");
1563 return DM_OK;
1564 }
1565
UnBindDevice(const std::string & pkgName,const std::string & deviceId)1566 int32_t DeviceManagerImpl::UnBindDevice(const std::string &pkgName, const std::string &deviceId)
1567 {
1568 if (pkgName.empty() || deviceId.empty()) {
1569 LOGE("UnBindDevice error: Invalid para. pkgName %{public}s", pkgName.c_str());
1570 return ERR_DM_INPUT_PARA_INVALID;
1571 }
1572 LOGI("Start, pkgName: %{public}s, deviceId: %{public}s", pkgName.c_str(),
1573 GetAnonyString(std::string(deviceId)).c_str());
1574 std::shared_ptr<IpcUnBindDeviceReq> req = std::make_shared<IpcUnBindDeviceReq>();
1575 std::shared_ptr<IpcRsp> rsp = std::make_shared<IpcRsp>();
1576 req->SetPkgName(pkgName);
1577 req->SetDeviceId(deviceId);
1578 int32_t ret = ipcClientProxy_->SendRequest(UNBIND_DEVICE, req, rsp);
1579 if (ret != DM_OK) {
1580 LOGE("UnBindDevice error: Send Request failed ret: %{public}d", ret);
1581 return ERR_DM_IPC_SEND_REQUEST_FAILED;
1582 }
1583 ret = rsp->GetErrCode();
1584 if (ret != DM_OK) {
1585 LOGE("UnBindDevice error: Failed with ret %{public}d", ret);
1586 return ret;
1587 }
1588
1589 LOGI("End");
1590 return DM_OK;
1591 }
1592
UnBindDevice(const std::string & pkgName,const std::string & deviceId,const std::string & extra)1593 int32_t DeviceManagerImpl::UnBindDevice(const std::string &pkgName, const std::string &deviceId,
1594 const std::string &extra)
1595 {
1596 if (pkgName.empty() || deviceId.empty()) {
1597 LOGE("UnBindDevice error: Invalid para. pkgName %{public}s", pkgName.c_str());
1598 return ERR_DM_INPUT_PARA_INVALID;
1599 }
1600 LOGI("Start, pkgName: %{public}s, deviceId: %{public}s", pkgName.c_str(),
1601 GetAnonyString(std::string(deviceId)).c_str());
1602 std::shared_ptr<IpcUnBindDeviceReq> req = std::make_shared<IpcUnBindDeviceReq>();
1603 std::shared_ptr<IpcRsp> rsp = std::make_shared<IpcRsp>();
1604 req->SetPkgName(pkgName);
1605 req->SetDeviceId(deviceId);
1606 req->SetExtraInfo(extra);
1607 int32_t ret = ipcClientProxy_->SendRequest(UNBIND_DEVICE, req, rsp);
1608 if (ret != DM_OK) {
1609 LOGE("UnBindDevice error: Send Request failed ret: %{public}d", ret);
1610 return ERR_DM_IPC_SEND_REQUEST_FAILED;
1611 }
1612 ret = rsp->GetErrCode();
1613 if (ret != DM_OK) {
1614 LOGE("UnBindDevice error: Failed with ret %{public}d", ret);
1615 return ret;
1616 }
1617
1618 LOGI("End");
1619 return DM_OK;
1620 }
1621
GetNetworkTypeByNetworkId(const std::string & pkgName,const std::string & netWorkId,int32_t & netWorkType)1622 int32_t DeviceManagerImpl::GetNetworkTypeByNetworkId(const std::string &pkgName, const std::string &netWorkId,
1623 int32_t &netWorkType)
1624 {
1625 if (pkgName.empty() || netWorkId.empty()) {
1626 LOGE("GetNetworkTypeByNetworkId error: Invalid para, pkgName: %{public}s, netWorkId: %{public}s, netWorkType:"
1627 "%{public}d", pkgName.c_str(), GetAnonyString(netWorkId).c_str(), netWorkType);
1628 return ERR_DM_INPUT_PARA_INVALID;
1629 }
1630 LOGI("Start, pkgName: %{public}s", pkgName.c_str());
1631
1632 std::shared_ptr<IpcGetInfoByNetWorkReq> req = std::make_shared<IpcGetInfoByNetWorkReq>();
1633 std::shared_ptr<IpcGetInfoByNetWorkRsp> rsp = std::make_shared<IpcGetInfoByNetWorkRsp>();
1634 req->SetPkgName(pkgName);
1635 req->SetNetWorkId(netWorkId);
1636
1637 int32_t ret = ipcClientProxy_->SendRequest(GET_NETWORKTYPE_BY_NETWORK, req, rsp);
1638 if (ret != DM_OK) {
1639 LOGI("GetNetworkTypeByNetworkId Send Request failed ret: %{public}d", ret);
1640 return ERR_DM_IPC_SEND_REQUEST_FAILED;
1641 }
1642
1643 ret = rsp->GetErrCode();
1644 if (ret != DM_OK) {
1645 LOGE("CheckAuthentication Failed with ret %{public}d", ret);
1646 return ret;
1647 }
1648 netWorkType = rsp->GetNetworkType();
1649 return DM_OK;
1650 }
1651
ImportAuthCode(const std::string & pkgName,const std::string & authCode)1652 int32_t DeviceManagerImpl::ImportAuthCode(const std::string &pkgName, const std::string &authCode)
1653 {
1654 if (authCode.empty() || pkgName.empty()) {
1655 LOGE("ImportAuthCode error: Invalid para, pkgName: %{public}s", pkgName.c_str());
1656 return ERR_DM_INPUT_PARA_INVALID;
1657 }
1658 int32_t length = static_cast<int32_t>(authCode.length());
1659 if (length < DM_IMPORT_AUTH_CODE_MIN_LENGTH || length > DM_IMPORT_AUTH_CODE_MAX_LENGTH) {
1660 LOGE("ImportAuthCode error: Invalid para, authCode size error.");
1661 return ERR_DM_INPUT_PARA_INVALID;
1662 }
1663
1664 for (int32_t i = 0; i < length; i++) {
1665 if (!isdigit(authCode[i])) {
1666 LOGE("ImportAuthCode error: Invalid para, authCode format error.");
1667 return ERR_DM_INPUT_PARA_INVALID;
1668 }
1669 }
1670
1671 std::shared_ptr<IpcImportAuthCodeReq> req = std::make_shared<IpcImportAuthCodeReq>();
1672 req->SetAuthCode(authCode);
1673 req->SetPkgName(pkgName);
1674 std::shared_ptr<IpcRsp> rsp = std::make_shared<IpcRsp>();
1675
1676 int32_t ret = ipcClientProxy_->SendRequest(IMPORT_AUTH_CODE, req, rsp);
1677 if (ret != DM_OK) {
1678 LOGI("ImportAuthCode Send Request failed ret: %{public}d", ret);
1679 return ERR_DM_IPC_SEND_REQUEST_FAILED;
1680 }
1681
1682 ret = rsp->GetErrCode();
1683 if (ret != DM_OK) {
1684 LOGE("ImportAuthCode Failed with ret %{public}d", ret);
1685 return ret;
1686 }
1687 return DM_OK;
1688 }
1689
ExportAuthCode(std::string & authCode)1690 int32_t DeviceManagerImpl::ExportAuthCode(std::string &authCode)
1691 {
1692 LOGI("Start");
1693
1694 std::shared_ptr<IpcReq> req = std::make_shared<IpcReq>();
1695 std::shared_ptr<IpcExportAuthCodeRsp> rsp = std::make_shared<IpcExportAuthCodeRsp>();
1696
1697 int32_t ret = ipcClientProxy_->SendRequest(EXPORT_AUTH_CODE, req, rsp);
1698 if (ret != DM_OK) {
1699 LOGI("ExportAuthCode Send Request failed ret: %{public}d", ret);
1700 return ERR_DM_IPC_SEND_REQUEST_FAILED;
1701 }
1702
1703 ret = rsp->GetErrCode();
1704 if (ret != DM_OK) {
1705 LOGE("ExportAuthCode Failed with ret %{public}d", ret);
1706 return ret;
1707 }
1708
1709 authCode = rsp->GetAuthCode();
1710 LOGI("Success.");
1711 return DM_OK;
1712 }
1713
RegisterDiscoveryCallback(const std::string & pkgName,std::map<std::string,std::string> & discoverParam,const std::map<std::string,std::string> & filterOptions,std::shared_ptr<DiscoveryCallback> callback)1714 int32_t DeviceManagerImpl::RegisterDiscoveryCallback(const std::string &pkgName,
1715 std::map<std::string, std::string> &discoverParam, const std::map<std::string, std::string> &filterOptions,
1716 std::shared_ptr<DiscoveryCallback> callback)
1717 {
1718 if (pkgName.empty() || callback == nullptr) {
1719 LOGE("DeviceManagerImpl::RegisterDiscoveryCallback failed: input callback is null or pkgName is empty.");
1720 return ERR_DM_INPUT_PARA_INVALID;
1721 }
1722 LOGI("Start, pkgName: %{public}s", pkgName.c_str());
1723 uint16_t subscribeId = AddDiscoveryCallback(pkgName, discoverParam, callback);
1724 discoverParam.emplace(PARAM_KEY_SUBSCRIBE_ID, std::to_string(subscribeId));
1725 std::string discParaStr = ConvertMapToJsonString(discoverParam);
1726 std::string filterOpStr = ConvertMapToJsonString(filterOptions);
1727
1728 std::shared_ptr<IpcCommonParamReq> req = std::make_shared<IpcCommonParamReq>();
1729 std::shared_ptr<IpcRsp> rsp = std::make_shared<IpcRsp>();
1730 req->SetPkgName(ComposeStr(pkgName, subscribeId));
1731 req->SetFirstParam(discParaStr);
1732 req->SetSecondParam(filterOpStr);
1733 int32_t ret = ipcClientProxy_->SendRequest(REGISTER_DISCOVERY_CALLBACK, req, rsp);
1734 if (ret != DM_OK) {
1735 LOGE("RegisterDiscoveryCallback error: Send Request failed ret: %{public}d", ret);
1736 return ERR_DM_IPC_SEND_REQUEST_FAILED;
1737 }
1738 ret = rsp->GetErrCode();
1739 if (ret != DM_OK) {
1740 LOGE("RegisterDiscoveryCallback error: Failed with ret %{public}d", ret);
1741 return ret;
1742 }
1743 LOGI("Completed");
1744 return DM_OK;
1745 }
1746
UnRegisterDiscoveryCallback(const std::string & pkgName)1747 int32_t DeviceManagerImpl::UnRegisterDiscoveryCallback(const std::string &pkgName)
1748 {
1749 if (pkgName.empty()) {
1750 DmRadarHelper::GetInstance().ReportDmBehavior(
1751 pkgName, "UnRegisterDiscoveryCallback", ERR_DM_INPUT_PARA_INVALID, anonyLocalUdid_);
1752 LOGE("DeviceManagerImpl::UnRegisterDiscoveryCallback failed: input pkgName is empty.");
1753 return ERR_DM_INPUT_PARA_INVALID;
1754 }
1755 LOGI("Start, pkgName: %{public}s", pkgName.c_str());
1756 std::string pkgNameTemp = ComposeStr(pkgName, DM_INVALID_FLAG_ID);
1757 uint16_t subscribeId = GetSubscribeIdFromMap(pkgNameTemp);
1758 if (subscribeId == DM_INVALID_FLAG_ID) {
1759 DmRadarHelper::GetInstance().ReportDmBehavior(
1760 pkgName, "UnRegisterDiscoveryCallback", ERR_DM_INPUT_PARA_INVALID, anonyLocalUdid_);
1761 LOGE("DeviceManagerImpl::UnRegisterDiscoveryCallback failed: cannot find pkgName in cache map.");
1762 return ERR_DM_INPUT_PARA_INVALID;
1763 }
1764 std::map<std::string, std::string> extraParam;
1765 extraParam.emplace(PARAM_KEY_SUBSCRIBE_ID, std::to_string(subscribeId));
1766 std::string extraParaStr = ConvertMapToJsonString(extraParam);
1767
1768 std::shared_ptr<IpcCommonParamReq> req = std::make_shared<IpcCommonParamReq>();
1769 std::shared_ptr<IpcRsp> rsp = std::make_shared<IpcRsp>();
1770 req->SetPkgName(ComposeStr(pkgName, subscribeId));
1771 req->SetFirstParam(extraParaStr);
1772 int32_t ret = ipcClientProxy_->SendRequest(UNREGISTER_DISCOVERY_CALLBACK, req, rsp);
1773 if (ret != DM_OK) {
1774 DmRadarHelper::GetInstance().ReportDmBehavior(pkgNameTemp, "UnRegisterDiscoveryCallback",
1775 ret, anonyLocalUdid_);
1776 LOGE("UnRegisterDiscoveryCallback error: Send Request failed ret: %{public}d", ret);
1777 return ERR_DM_IPC_SEND_REQUEST_FAILED;
1778 }
1779 ret = rsp->GetErrCode();
1780 if (ret != DM_OK) {
1781 DmRadarHelper::GetInstance().ReportDmBehavior(pkgNameTemp, "UnRegisterDiscoveryCallback",
1782 ret, anonyLocalUdid_);
1783 LOGE("UnRegisterDiscoveryCallback error: Failed with ret %{public}d", ret);
1784 return ret;
1785 }
1786 RemoveDiscoveryCallback(pkgNameTemp);
1787 DmRadarHelper::GetInstance().ReportDmBehavior(pkgNameTemp, "UnRegisterDiscoveryCallback", DM_OK, anonyLocalUdid_);
1788 LOGI("Completed");
1789 return DM_OK;
1790 }
1791
StartAdvertising(const std::string & pkgName,std::map<std::string,std::string> & advertiseParam,std::shared_ptr<PublishCallback> callback)1792 int32_t DeviceManagerImpl::StartAdvertising(const std::string &pkgName,
1793 std::map<std::string, std::string> &advertiseParam, std::shared_ptr<PublishCallback> callback)
1794 {
1795 if (pkgName.empty() || callback == nullptr) {
1796 LOGE("DeviceManagerImpl::StartAdvertising error: pkgName %{public}s invalid para", pkgName.c_str());
1797 return ERR_DM_INPUT_PARA_INVALID;
1798 }
1799 LOGI("Start, pkgName %{public}s", pkgName.c_str());
1800
1801 int32_t publishId;
1802 if (advertiseParam.find(PARAM_KEY_PUBLISH_ID) == advertiseParam.end()) {
1803 publishId = AddPublishCallback(pkgName);
1804 advertiseParam[PARAM_KEY_PUBLISH_ID] = std::to_string(publishId);
1805 } else {
1806 publishId = std::atoi((advertiseParam.find(PARAM_KEY_PUBLISH_ID)->second).c_str());
1807 }
1808 std::string adverParaStr = ConvertMapToJsonString(advertiseParam);
1809
1810 std::shared_ptr<IpcCommonParamReq> req = std::make_shared<IpcCommonParamReq>();
1811 std::shared_ptr<IpcRsp> rsp = std::make_shared<IpcRsp>();
1812 req->SetPkgName(pkgName);
1813 req->SetFirstParam(adverParaStr);
1814 int32_t ret = ipcClientProxy_->SendRequest(START_ADVERTISING, req, rsp);
1815 if (ret != DM_OK) {
1816 LOGE("StartAdvertising error: Send Request failed ret: %{public}d", ret);
1817 return ERR_DM_IPC_SEND_REQUEST_FAILED;
1818 }
1819 ret = rsp->GetErrCode();
1820 if (ret != DM_OK) {
1821 LOGE("StartAdvertising error: Failed with ret %{public}d", ret);
1822 return ret;
1823 }
1824 DeviceManagerNotify::GetInstance().RegisterPublishCallback(pkgName, publishId, callback);
1825 LOGI("Completed");
1826 return DM_OK;
1827 }
1828
StopAdvertising(const std::string & pkgName,std::map<std::string,std::string> & advertiseParam)1829 int32_t DeviceManagerImpl::StopAdvertising(const std::string &pkgName,
1830 std::map<std::string, std::string> &advertiseParam)
1831 {
1832 if (pkgName.empty()) {
1833 LOGE("DeviceManagerImpl::StopAdvertising failed: input pkgName is empty.");
1834 return ERR_DM_INPUT_PARA_INVALID;
1835 }
1836 LOGI("Start, pkgName: %{public}s", pkgName.c_str());
1837
1838 int32_t publishId;
1839 if (advertiseParam.find(PARAM_KEY_PUBLISH_ID) == advertiseParam.end()) {
1840 publishId = RemovePublishCallback(pkgName);
1841 advertiseParam[PARAM_KEY_PUBLISH_ID] = std::to_string(publishId);
1842 } else {
1843 publishId = std::atoi((advertiseParam.find(PARAM_KEY_PUBLISH_ID)->second).c_str());
1844 }
1845 if (publishId == DM_INVALID_FLAG_ID) {
1846 LOGE("DeviceManagerImpl::StopAdvertising failed: cannot find pkgName in cache map.");
1847 return ERR_DM_INPUT_PARA_INVALID;
1848 }
1849 std::string adverParaStr = ConvertMapToJsonString(advertiseParam);
1850
1851 std::shared_ptr<IpcCommonParamReq> req = std::make_shared<IpcCommonParamReq>();
1852 std::shared_ptr<IpcRsp> rsp = std::make_shared<IpcRsp>();
1853 req->SetPkgName(pkgName);
1854 req->SetFirstParam(adverParaStr);
1855 int32_t ret = ipcClientProxy_->SendRequest(STOP_ADVERTISING, req, rsp);
1856 if (ret != DM_OK) {
1857 LOGE("StopAdvertising error: Send Request failed ret: %{public}d", ret);
1858 return ERR_DM_IPC_SEND_REQUEST_FAILED;
1859 }
1860 ret = rsp->GetErrCode();
1861 if (ret != DM_OK) {
1862 LOGE("StopAdvertising error: Failed with ret %{public}d", ret);
1863 return ret;
1864 }
1865 DeviceManagerNotify::GetInstance().UnRegisterPublishCallback(pkgName, publishId);
1866 LOGI("Completed");
1867 return DM_OK;
1868 }
1869
BindTarget(const std::string & pkgName,const PeerTargetId & targetId,std::map<std::string,std::string> & bindParam,std::shared_ptr<BindTargetCallback> callback)1870 int32_t DeviceManagerImpl::BindTarget(const std::string &pkgName, const PeerTargetId &targetId,
1871 std::map<std::string, std::string> &bindParam, std::shared_ptr<BindTargetCallback> callback)
1872 {
1873 if (pkgName.empty() || IsInvalidPeerTargetId(targetId)) {
1874 LOGE("DeviceManagerImpl::BindTarget failed: input pkgName or targetId is empty.");
1875 return ERR_DM_INPUT_PARA_INVALID;
1876 }
1877 LOGI("start, pkgName: %{public}s", pkgName.c_str());
1878 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
1879 bindParam[TOKENID] = std::to_string(OHOS::IPCSkeleton::GetSelfTokenID());
1880 #endif
1881 std::string bindParamStr = ConvertMapToJsonString(bindParam);
1882 DeviceManagerNotify::GetInstance().RegisterBindCallback(pkgName, targetId, callback);
1883 std::shared_ptr<IpcBindTargetReq> req = std::make_shared<IpcBindTargetReq>();
1884 std::shared_ptr<IpcRsp> rsp = std::make_shared<IpcRsp>();
1885 req->SetPkgName(pkgName);
1886 req->SetPeerTargetId(targetId);
1887 req->SetBindParam(bindParamStr);
1888 int32_t ret = ipcClientProxy_->SendRequest(BIND_TARGET, req, rsp);
1889 if (ret != DM_OK) {
1890 LOGE("BindTarget error: Send Request failed ret: %{public}d", ret);
1891 return ERR_DM_IPC_SEND_REQUEST_FAILED;
1892 }
1893 ret = rsp->GetErrCode();
1894 if (ret != DM_OK) {
1895 LOGE("BindTarget error: Failed with ret %{public}d", ret);
1896 return ret;
1897 }
1898
1899 LOGI("Completed");
1900 return DM_OK;
1901 }
1902
UnbindTarget(const std::string & pkgName,const PeerTargetId & targetId,std::map<std::string,std::string> & unbindParam,std::shared_ptr<UnbindTargetCallback> callback)1903 int32_t DeviceManagerImpl::UnbindTarget(const std::string &pkgName, const PeerTargetId &targetId,
1904 std::map<std::string, std::string> &unbindParam, std::shared_ptr<UnbindTargetCallback> callback)
1905 {
1906 if (pkgName.empty() || IsInvalidPeerTargetId(targetId)) {
1907 LOGE("DeviceManagerImpl::UnbindTarget failed: input pkgName or targetId is empty.");
1908 return ERR_DM_INPUT_PARA_INVALID;
1909 }
1910 LOGI("Start, pkgName: %{public}s", pkgName.c_str());
1911 std::string unbindParamStr = ConvertMapToJsonString(unbindParam);
1912
1913 std::shared_ptr<IpcBindTargetReq> req = std::make_shared<IpcBindTargetReq>();
1914 std::shared_ptr<IpcRsp> rsp = std::make_shared<IpcRsp>();
1915 req->SetPkgName(pkgName);
1916 req->SetPeerTargetId(targetId);
1917 req->SetBindParam(unbindParamStr);
1918 int32_t ret = ipcClientProxy_->SendRequest(UNBIND_TARGET, req, rsp);
1919 if (ret != DM_OK) {
1920 LOGE("UnbindTarget error: Send Request failed ret: %{public}d", ret);
1921 return ERR_DM_IPC_SEND_REQUEST_FAILED;
1922 }
1923 ret = rsp->GetErrCode();
1924 if (ret != DM_OK) {
1925 LOGE("UnbindTarget error: Failed with ret %{public}d", ret);
1926 return ret;
1927 }
1928 DeviceManagerNotify::GetInstance().RegisterUnbindCallback(pkgName, targetId, callback);
1929
1930 LOGI("Completed");
1931 return DM_OK;
1932 }
1933
GetTrustedDeviceList(const std::string & pkgName,const std::map<std::string,std::string> & filterOptions,bool isRefresh,std::vector<DmDeviceInfo> & deviceList)1934 int32_t DeviceManagerImpl::GetTrustedDeviceList(const std::string &pkgName,
1935 const std::map<std::string, std::string> &filterOptions, bool isRefresh,
1936 std::vector<DmDeviceInfo> &deviceList)
1937 {
1938 if (pkgName.empty()) {
1939 LOGE("DeviceManagerImpl::GetTrustedDeviceList failed: input pkgName is empty.");
1940 return ERR_DM_INPUT_PARA_INVALID;
1941 }
1942 std::string filterOpStr = ConvertMapToJsonString(filterOptions);
1943 return GetTrustedDeviceList(pkgName, filterOpStr, isRefresh, deviceList);
1944 }
1945
RegisterDevStateCallback(const std::string & pkgName,const std::map<std::string,std::string> & extraParam,std::shared_ptr<DeviceStateCallback> callback)1946 int32_t DeviceManagerImpl::RegisterDevStateCallback(const std::string &pkgName,
1947 const std::map<std::string, std::string> &extraParam, std::shared_ptr<DeviceStateCallback> callback)
1948 {
1949 (void)extraParam;
1950 if (pkgName.empty() || callback == nullptr) {
1951 DmRadarHelper::GetInstance().ReportDmBehavior(pkgName, "RegisterDevStateCallback",
1952 ERR_DM_INPUT_PARA_INVALID, anonyLocalUdid_);
1953 LOGE("DeviceManagerImpl::RegisterDeviceStateCallback failed: input pkgName or callback is empty.");
1954 return ERR_DM_INPUT_PARA_INVALID;
1955 }
1956 SyncCallbackToService(DmCommonNotifyEvent::REG_DEVICE_STATE, pkgName);
1957 DeviceManagerNotify::GetInstance().RegisterDeviceStateCallback(pkgName, callback);
1958 DmRadarHelper::GetInstance().ReportDmBehavior(pkgName, "RegisterDevStateCallback", DM_OK, anonyLocalUdid_);
1959 LOGI("Completed, pkgName: %{public}s", pkgName.c_str());
1960 return DM_OK;
1961 }
1962
CheckAccessToTarget(uint64_t tokenId,const std::string & targetId)1963 int32_t DeviceManagerImpl::CheckAccessToTarget(uint64_t tokenId, const std::string &targetId)
1964 {
1965 (void)tokenId;
1966 (void)targetId;
1967 LOGI("unsupport");
1968 return ERR_DM_UNSUPPORTED_METHOD;
1969 }
1970
AddDiscoveryCallback(const std::string & pkgName,std::map<std::string,std::string> & discoverParam,std::shared_ptr<DiscoveryCallback> callback)1971 uint16_t DeviceManagerImpl::AddDiscoveryCallback(const std::string &pkgName,
1972 std::map<std::string, std::string> &discoverParam, std::shared_ptr<DiscoveryCallback> callback)
1973 {
1974 if (discoverParam.empty() || callback == nullptr) {
1975 LOGE("input param invalid.");
1976 return DM_INVALID_FLAG_ID;
1977 }
1978 uint16_t subscribeId = DM_INVALID_FLAG_ID;
1979 if (discoverParam.find(PARAM_KEY_SUBSCRIBE_ID) != discoverParam.end()) {
1980 subscribeId = std::atoi((discoverParam.find(PARAM_KEY_SUBSCRIBE_ID)->second).c_str());
1981 }
1982 std::string pkgNameTemp = ComposeStr(pkgName, subscribeId);
1983 {
1984 std::lock_guard<std::mutex> autoLock(subMapLock);
1985 auto item = pkgName2SubIdMap_.find(pkgNameTemp);
1986 if (item == pkgName2SubIdMap_.end() && subscribeId == DM_INVALID_FLAG_ID) {
1987 CHECK_SIZE_RETURN(pkgName2SubIdMap_, DM_INVALID_FLAG_ID);
1988 CHECK_SIZE_RETURN(randSubIdSet_, DM_INVALID_FLAG_ID);
1989 subscribeId = GenUniqueRandUint(randSubIdSet_);
1990 pkgName2SubIdMap_[pkgNameTemp] = subscribeId;
1991 } else if (item == pkgName2SubIdMap_.end() && subscribeId != DM_INVALID_FLAG_ID) {
1992 CHECK_SIZE_RETURN(pkgName2SubIdMap_, DM_INVALID_FLAG_ID);
1993 CHECK_SIZE_RETURN(randSubIdSet_, DM_INVALID_FLAG_ID);
1994 pkgName2SubIdMap_[pkgNameTemp] = subscribeId;
1995 randSubIdSet_.emplace(subscribeId);
1996 } else if (item != pkgName2SubIdMap_.end()) {
1997 subscribeId = pkgName2SubIdMap_[pkgNameTemp];
1998 } else {
1999 LOGE("subscribeId is unreasonable");
2000 }
2001 }
2002 DeviceManagerNotify::GetInstance().RegisterDiscoveryCallback(pkgNameTemp, subscribeId, callback);
2003 DmRadarHelper::GetInstance().ReportDmBehavior(pkgNameTemp, "AddDiscoveryCallback", DM_OK, anonyLocalUdid_);
2004 return subscribeId;
2005 }
2006
RemoveDiscoveryCallback(const std::string & pkgName)2007 uint16_t DeviceManagerImpl::RemoveDiscoveryCallback(const std::string &pkgName)
2008 {
2009 uint16_t subscribeId = DM_INVALID_FLAG_ID;
2010 {
2011 std::lock_guard<std::mutex> autoLock(subMapLock);
2012 if (pkgName2SubIdMap_.find(pkgName) != pkgName2SubIdMap_.end()) {
2013 subscribeId = pkgName2SubIdMap_[pkgName];
2014 randSubIdSet_.erase(subscribeId);
2015 pkgName2SubIdMap_.erase(pkgName);
2016 }
2017 }
2018 DeviceManagerNotify::GetInstance().UnRegisterDiscoveryCallback(pkgName, subscribeId);
2019 DmRadarHelper::GetInstance().ReportDmBehavior(pkgName, "RemoveDiscoveryCallback", DM_OK, anonyLocalUdid_);
2020 return subscribeId;
2021 }
2022
AddPublishCallback(const std::string & pkgName)2023 int32_t DeviceManagerImpl::AddPublishCallback(const std::string &pkgName)
2024 {
2025 int32_t publishId = DM_INVALID_FLAG_ID;
2026 {
2027 std::lock_guard<std::mutex> autoLock(pubMapLock);
2028 if (pkgName2PubIdMap_.find(pkgName) != pkgName2PubIdMap_.end()) {
2029 publishId = pkgName2PubIdMap_[pkgName];
2030 } else {
2031 CHECK_SIZE_RETURN(pkgName2PubIdMap_, DM_INVALID_FLAG_ID);
2032 CHECK_SIZE_RETURN(randPubIdSet_, DM_INVALID_FLAG_ID);
2033 publishId = GenUniqueRandUint(randPubIdSet_);
2034 pkgName2PubIdMap_[pkgName] = publishId;
2035 }
2036 }
2037 DmRadarHelper::GetInstance().ReportDmBehavior(pkgName, "AddPublishCallback", DM_OK, anonyLocalUdid_);
2038 return publishId;
2039 }
2040
RemovePublishCallback(const std::string & pkgName)2041 int32_t DeviceManagerImpl::RemovePublishCallback(const std::string &pkgName)
2042 {
2043 uint16_t publishId = DM_INVALID_FLAG_ID;
2044 {
2045 std::lock_guard<std::mutex> autoLock(pubMapLock);
2046 if (pkgName2PubIdMap_.find(pkgName) != pkgName2PubIdMap_.end()) {
2047 publishId = pkgName2PubIdMap_[pkgName];
2048 randPubIdSet_.erase(publishId);
2049 pkgName2PubIdMap_.erase(pkgName);
2050 }
2051 }
2052 DmRadarHelper::GetInstance().ReportDmBehavior(pkgName, "RemovePublishCallback", DM_OK, anonyLocalUdid_);
2053 return publishId;
2054 }
2055
RegisterPinHolderCallback(const std::string & pkgName,std::shared_ptr<PinHolderCallback> callback)2056 int32_t DeviceManagerImpl::RegisterPinHolderCallback(const std::string &pkgName,
2057 std::shared_ptr<PinHolderCallback> callback)
2058 {
2059 if (pkgName.empty() || callback == nullptr) {
2060 DmRadarHelper::GetInstance().ReportDmBehavior(
2061 pkgName, "RegisterPinHolderCallback", ERR_DM_INPUT_PARA_INVALID, anonyLocalUdid_);
2062 LOGE("RegisterPinHolderCallback error: Invalid para, pkgName: %{public}s", pkgName.c_str());
2063 return ERR_DM_INPUT_PARA_INVALID;
2064 }
2065 DeviceManagerNotify::GetInstance().RegisterPinHolderCallback(pkgName, callback);
2066 std::shared_ptr<IpcReq> req = std::make_shared<IpcReq>();
2067 req->SetPkgName(pkgName);
2068 std::shared_ptr<IpcRsp> rsp = std::make_shared<IpcRsp>();
2069
2070 int32_t ret = ipcClientProxy_->SendRequest(REGISTER_PIN_HOLDER_CALLBACK, req, rsp);
2071 if (ret != DM_OK) {
2072 DmRadarHelper::GetInstance().ReportDmBehavior(pkgName, "RegisterPinHolderCallback", ret, anonyLocalUdid_);
2073 LOGI("RegisterPinHolderCallback Send Request failed ret: %{public}d", ret);
2074 return ERR_DM_IPC_SEND_REQUEST_FAILED;
2075 }
2076 ret = rsp->GetErrCode();
2077 if (ret != DM_OK) {
2078 DmRadarHelper::GetInstance().ReportDmBehavior(pkgName, "RegisterPinHolderCallback", ret, anonyLocalUdid_);
2079 LOGE("RegisterPinHolderCallback Failed with ret %{public}d", ret);
2080 return ret;
2081 }
2082 DmRadarHelper::GetInstance().ReportDmBehavior(pkgName, "RegisterPinHolderCallback", DM_OK, anonyLocalUdid_);
2083 return DM_OK;
2084 }
2085
CreatePinHolder(const std::string & pkgName,const PeerTargetId & targetId,DmPinType pinType,const std::string & payload)2086 int32_t DeviceManagerImpl::CreatePinHolder(const std::string &pkgName, const PeerTargetId &targetId,
2087 DmPinType pinType, const std::string &payload)
2088 {
2089 if (pkgName.empty() || IsInvalidPeerTargetId(targetId) ||
2090 pinType > DmPinType::SUPER_SONIC || pinType < DmPinType::NUMBER_PIN_CODE ||
2091 payload.length() > DM_STRING_LENGTH_MAX) {
2092 LOGE("CreatePinHolder error: Invalid para, pkgName: %{public}s, pinType: %{public}d, payload.length:"
2093 "%{public}zu", pkgName.c_str(), pinType, payload.length());
2094 return ERR_DM_INPUT_PARA_INVALID;
2095 }
2096 std::shared_ptr<IpcCreatePinHolderReq> req = std::make_shared<IpcCreatePinHolderReq>();
2097 req->SetPkgName(pkgName);
2098 req->SetPeerTargetId(targetId);
2099 req->SetPinType(pinType);
2100 req->SetPayload(payload);
2101 std::shared_ptr<IpcRsp> rsp = std::make_shared<IpcRsp>();
2102
2103 int32_t ret = ipcClientProxy_->SendRequest(CREATE_PIN_HOLDER, req, rsp);
2104 if (ret != DM_OK) {
2105 LOGI("CreatePinHolder Send Request failed ret: %{public}d", ret);
2106 return ERR_DM_IPC_SEND_REQUEST_FAILED;
2107 }
2108 ret = rsp->GetErrCode();
2109 if (ret != DM_OK) {
2110 LOGE("CreatePinHolder Failed with ret %{public}d", ret);
2111 return ret;
2112 }
2113 return DM_OK;
2114 }
2115
DestroyPinHolder(const std::string & pkgName,const PeerTargetId & targetId,DmPinType pinType,const std::string & payload)2116 int32_t DeviceManagerImpl::DestroyPinHolder(const std::string &pkgName, const PeerTargetId &targetId,
2117 DmPinType pinType, const std::string &payload)
2118 {
2119 if (pkgName.empty() || IsInvalidPeerTargetId(targetId) ||
2120 pinType > DmPinType::SUPER_SONIC || pinType < DmPinType::NUMBER_PIN_CODE ||
2121 payload.length() > DM_STRING_LENGTH_MAX) {
2122 LOGE("DestroyPinHolder error: Invalid para, pkgName: %{public}s, pinType: %{public}d, payload.length"
2123 "%{public}zu", pkgName.c_str(), pinType, payload.length());
2124 return ERR_DM_INPUT_PARA_INVALID;
2125 }
2126 std::shared_ptr<IpcDestroyPinHolderReq> req = std::make_shared<IpcDestroyPinHolderReq>();
2127 req->SetPkgName(pkgName);
2128 req->SetPeerTargetId(targetId);
2129 req->SetPinType(pinType);
2130 req->SetPayload(payload);
2131 std::shared_ptr<IpcRsp> rsp = std::make_shared<IpcRsp>();
2132
2133 int32_t ret = ipcClientProxy_->SendRequest(DESTROY_PIN_HOLDER, req, rsp);
2134 if (ret != DM_OK) {
2135 LOGI("DestroyPinHolder Send Request failed ret: %{public}d", ret);
2136 return ERR_DM_IPC_SEND_REQUEST_FAILED;
2137 }
2138 ret = rsp->GetErrCode();
2139 if (ret != DM_OK) {
2140 LOGE("DestroyPinHolder Failed with ret %{public}d", ret);
2141 return ret;
2142 }
2143 return DM_OK;
2144 }
2145
DpAclAdd(const int64_t accessControlId,const std::string & udid,const int32_t bindType)2146 int32_t DeviceManagerImpl::DpAclAdd(const int64_t accessControlId, const std::string &udid, const int32_t bindType)
2147 {
2148 if (bindType != IDENTICAL_ACCOUNT) {
2149 LOGI("not identical account");
2150 return DM_OK;
2151 }
2152 LOGI("Start.");
2153 std::shared_ptr<IpcAclProfileReq> req = std::make_shared<IpcAclProfileReq>();
2154 std::shared_ptr<IpcRsp> rsp = std::make_shared<IpcRsp>();
2155 req->SetStr(udid);
2156 int32_t ret = ipcClientProxy_->SendRequest(DP_ACL_ADD, req, rsp);
2157 if (ret != DM_OK) {
2158 LOGE("DpAclAdd error: Send Request failed ret: %{public}d", ret);
2159 return ERR_DM_IPC_SEND_REQUEST_FAILED;
2160 }
2161 ret = rsp->GetErrCode();
2162 if (ret != DM_OK) {
2163 LOGE("DpAclAdd error: Failed with ret %{public}d", ret);
2164 return ret;
2165 }
2166 LOGI("Completed");
2167 return DM_OK;
2168 }
2169
GetDeviceSecurityLevel(const std::string & pkgName,const std::string & networkId,int32_t & securityLevel)2170 int32_t DeviceManagerImpl::GetDeviceSecurityLevel(const std::string &pkgName, const std::string &networkId,
2171 int32_t &securityLevel)
2172 {
2173 if (pkgName.empty() || networkId.empty()) {
2174 DmRadarHelper::GetInstance().ReportDmBehavior(pkgName, "GetDeviceSecurityLevel",
2175 ERR_DM_INPUT_PARA_INVALID, anonyLocalUdid_);
2176 LOGE("DeviceManagerImpl::GetDeviceSecurityLevel error: pkgName: %{public}s, networkId: %{public}s",
2177 pkgName.c_str(), GetAnonyString(networkId).c_str());
2178 return ERR_DM_INPUT_PARA_INVALID;
2179 }
2180 LOGI("Start: pkgName: %{public}s, networkId: %{public}s", pkgName.c_str(),
2181 GetAnonyString(networkId).c_str());
2182
2183 std::shared_ptr<IpcGetInfoByNetWorkReq> req = std::make_shared<IpcGetInfoByNetWorkReq>();
2184 std::shared_ptr<IpcGetInfoByNetWorkRsp> rsp = std::make_shared<IpcGetInfoByNetWorkRsp>();
2185 req->SetPkgName(pkgName);
2186 req->SetNetWorkId(networkId);
2187
2188 int32_t ret = ipcClientProxy_->SendRequest(GET_SECURITY_LEVEL, req, rsp);
2189 if (ret != DM_OK) {
2190 DmRadarHelper::GetInstance().ReportDmBehavior(pkgName, "GetDeviceSecurityLevel", ret, anonyLocalUdid_);
2191 LOGE("GetDeviceSecurityLevel Send Request failed ret: %{public}d", ret);
2192 return ERR_DM_IPC_SEND_REQUEST_FAILED;
2193 }
2194
2195 ret = rsp->GetErrCode();
2196 if (ret != DM_OK) {
2197 DmRadarHelper::GetInstance().ReportDmBehavior(pkgName, "GetDeviceSecurityLevel", ret, anonyLocalUdid_);
2198 LOGE("GetDeviceSecurityLevel Failed with ret %{public}d", ret);
2199 return ret;
2200 }
2201 securityLevel = rsp->GetSecurityLevel();
2202 DmRadarHelper::GetInstance().ReportDmBehavior(pkgName, "GetDeviceSecurityLevel", DM_OK, anonyLocalUdid_);
2203 return DM_OK;
2204 }
2205
CheckApiPermission(int32_t permissionLevel)2206 int32_t DeviceManagerImpl::CheckApiPermission(int32_t permissionLevel)
2207 {
2208 LOGI("PermissionLevel: %{public}d", permissionLevel);
2209 std::shared_ptr<IpcPermissionReq> req = std::make_shared<IpcPermissionReq>();
2210 std::shared_ptr<IpcRsp> rsp = std::make_shared<IpcRsp>();
2211 req->SetPermissionLevel(permissionLevel);
2212 int32_t ret = ipcClientProxy_->SendRequest(CHECK_API_PERMISSION, req, rsp);
2213 if (ret != DM_OK) {
2214 LOGE("Send Request failed ret: %{public}d", ret);
2215 return ERR_DM_IPC_SEND_REQUEST_FAILED;
2216 }
2217
2218 ret = rsp->GetErrCode();
2219 if (ret != DM_OK) {
2220 LOGE("Check permission failed with ret: %{public}d", ret);
2221 return ret;
2222 }
2223 LOGD("The caller declare the DM permission!");
2224 return DM_OK;
2225 }
2226
IsSameAccount(const std::string & netWorkId)2227 bool DeviceManagerImpl::IsSameAccount(const std::string &netWorkId)
2228 {
2229 if (netWorkId.empty()) {
2230 LOGE("DeviceManagerImpl::IsSameAccount error: netWorkId: %{public}s", GetAnonyString(netWorkId).c_str());
2231 return false;
2232 }
2233 std::shared_ptr<IpcAclProfileReq> req = std::make_shared<IpcAclProfileReq>();
2234 std::shared_ptr<IpcRsp> rsp = std::make_shared<IpcRsp>();
2235 req->SetStr(netWorkId);
2236 int32_t ret = ipcClientProxy_->SendRequest(IS_SAME_ACCOUNT, req, rsp);
2237 if (ret != DM_OK) {
2238 LOGE("IsSameAccount Send Request failed ret: %{public}d", ret);
2239 return false;
2240 }
2241 ret = rsp->GetErrCode();
2242 if (ret != DM_OK) {
2243 LOGE("IsSameAccount Failed with ret: %{public}d", ret);
2244 return false;
2245 }
2246 return true;
2247 }
2248
GetErrCode(int32_t errCode)2249 int32_t DeviceManagerImpl::GetErrCode(int32_t errCode)
2250 {
2251 auto flag = MAP_ERROR_CODE.find(errCode);
2252 if (flag == MAP_ERROR_CODE.end()) {
2253 return errCode;
2254 }
2255 return flag->second;
2256 }
2257
ShiftLNNGear(const std::string & pkgName)2258 int32_t DeviceManagerImpl::ShiftLNNGear(const std::string &pkgName)
2259 {
2260 LOGI("Start. pkgName = %{public}s", pkgName.c_str());
2261 std::shared_ptr<IpcReq> req = std::make_shared<IpcReq>();
2262 std::shared_ptr<IpcRsp> rsp = std::make_shared<IpcRsp>();
2263 req->SetPkgName(pkgName);
2264 int32_t ret = ipcClientProxy_->SendRequest(SHIFT_LNN_GEAR, req, rsp);
2265 if (ret != DM_OK) {
2266 DmRadarHelper::GetInstance().ReportDmBehavior(pkgName, "ShiftLNNGear", ret, anonyLocalUdid_);
2267 LOGE("ShiftLNNGear error: Send Request failed ret: %{public}d", ret);
2268 return ERR_DM_IPC_SEND_REQUEST_FAILED;
2269 }
2270 ret = rsp->GetErrCode();
2271 if (ret != DM_OK) {
2272 DmRadarHelper::GetInstance().ReportDmBehavior(pkgName, "ShiftLNNGear", ret, anonyLocalUdid_);
2273 LOGE("ShiftLNNGear error: Failed with ret %{public}d", ret);
2274 return ret;
2275 }
2276 DmRadarHelper::GetInstance().ReportDmBehavior(pkgName, "ShiftLNNGear", DM_OK, anonyLocalUdid_);
2277 return DM_OK;
2278 }
2279
SetDnPolicy(const std::string & pkgName,std::map<std::string,std::string> & policy)2280 int32_t DeviceManagerImpl::SetDnPolicy(const std::string &pkgName, std::map<std::string, std::string> &policy)
2281 {
2282 const size_t SET_DN_POLICY_PARAM_SIZE = 2;
2283 if (pkgName.empty() || policy.size() != SET_DN_POLICY_PARAM_SIZE) {
2284 DmRadarHelper::GetInstance().ReportDmBehavior(pkgName, "SetDnPolicy",
2285 ERR_DM_INPUT_PARA_INVALID, anonyLocalUdid_);
2286 LOGE("Para invalid: policy is less than two or pkgName is empty.");
2287 return ERR_DM_INPUT_PARA_INVALID;
2288 }
2289 LOGI("Start");
2290 std::string strategy = ConvertMapToJsonString(policy);
2291
2292 std::shared_ptr<IpcCommonParamReq> req = std::make_shared<IpcCommonParamReq>();
2293 std::shared_ptr<IpcRsp> rsp = std::make_shared<IpcRsp>();
2294 req->SetPkgName(pkgName);
2295 req->SetFirstParam(strategy);
2296 int32_t ret = ipcClientProxy_->SendRequest(SET_DN_POLICY, req, rsp);
2297 if (ret != DM_OK) {
2298 DmRadarHelper::GetInstance().ReportDmBehavior(pkgName, "SetDnPolicy", ret, anonyLocalUdid_);
2299 LOGE("Send Request failed ret: %{public}d", ret);
2300 return ERR_DM_IPC_SEND_REQUEST_FAILED;
2301 }
2302 ret = rsp->GetErrCode();
2303 if (ret != DM_OK) {
2304 DmRadarHelper::GetInstance().ReportDmBehavior(pkgName, "SetDnPolicy", ret, anonyLocalUdid_);
2305 LOGE("Failed with ret %{public}d", ret);
2306 return ret;
2307 }
2308 LOGI("Completed");
2309 DmRadarHelper::GetInstance().ReportDmBehavior(pkgName, "SetDnPolicy", DM_OK, anonyLocalUdid_);
2310 return DM_OK;
2311 }
2312
RegDevTrustChangeCallback(const std::string & pkgName,std::shared_ptr<DevTrustChangeCallback> callback)2313 int32_t DeviceManagerImpl::RegDevTrustChangeCallback(const std::string &pkgName,
2314 std::shared_ptr<DevTrustChangeCallback> callback)
2315 {
2316 if (pkgName.empty() || callback == nullptr) {
2317 LOGE("Error: Invalid para");
2318 DmRadarHelper::GetInstance().ReportDmBehavior(
2319 pkgName, "RegDevTrustChangeCallback", ERR_DM_INPUT_PARA_INVALID, anonyLocalUdid_);
2320 return ERR_DM_INPUT_PARA_INVALID;
2321 }
2322 LOGI("PkgName %{public}s.", pkgName.c_str());
2323 DeviceManagerNotify::GetInstance().RegDevTrustChangeCallback(pkgName, callback);
2324 SyncCallbackToService(DmCommonNotifyEvent::REG_REMOTE_DEVICE_TRUST_CHANGE, pkgName);
2325 DmRadarHelper::GetInstance().ReportDmBehavior(pkgName, "RegDevTrustChangeCallback", DM_OK, anonyLocalUdid_);
2326 return DM_OK;
2327 }
2328
RegisterDeviceScreenStatusCallback(const std::string & pkgName,std::shared_ptr<DeviceScreenStatusCallback> callback)2329 int32_t DeviceManagerImpl::RegisterDeviceScreenStatusCallback(const std::string &pkgName,
2330 std::shared_ptr<DeviceScreenStatusCallback> callback)
2331 {
2332 if (pkgName.empty() || callback == nullptr) {
2333 LOGE("Error: Invalid para");
2334 return ERR_DM_INPUT_PARA_INVALID;
2335 }
2336 DeviceManagerNotify::GetInstance().RegisterDeviceScreenStatusCallback(pkgName, callback);
2337 SyncCallbackToService(DmCommonNotifyEvent::REG_DEVICE_SCREEN_STATE, pkgName);
2338 LOGI("Completed, pkgName: %{public}s", pkgName.c_str());
2339 return DM_OK;
2340 }
2341
UnRegisterDeviceScreenStatusCallback(const std::string & pkgName)2342 int32_t DeviceManagerImpl::UnRegisterDeviceScreenStatusCallback(const std::string &pkgName)
2343 {
2344 if (pkgName.empty()) {
2345 LOGE("Error: Invalid para");
2346 return ERR_DM_INPUT_PARA_INVALID;
2347 }
2348 SyncCallbackToService(DmCommonNotifyEvent::UN_REG_DEVICE_SCREEN_STATE, pkgName);
2349 DeviceManagerNotify::GetInstance().UnRegisterDeviceScreenStatusCallback(pkgName);
2350 LOGI("Completed, pkgName: %{public}s", pkgName.c_str());
2351 return DM_OK;
2352 }
2353
GetDeviceScreenStatus(const std::string & pkgName,const std::string & networkId,int32_t & screenStatus)2354 int32_t DeviceManagerImpl::GetDeviceScreenStatus(const std::string &pkgName, const std::string &networkId,
2355 int32_t &screenStatus)
2356 {
2357 if (pkgName.empty() || networkId.empty()) {
2358 LOGE("Error: Invalid para");
2359 return ERR_DM_INPUT_PARA_INVALID;
2360 }
2361 LOGI("Start: pkgName: %{public}s, networkId: %{public}s", pkgName.c_str(), GetAnonyString(networkId).c_str());
2362
2363 std::shared_ptr<IpcGetDeviceScreenStatusReq> req = std::make_shared<IpcGetDeviceScreenStatusReq>();
2364 std::shared_ptr<IpcGetDeviceScreenStatusRsp> rsp = std::make_shared<IpcGetDeviceScreenStatusRsp>();
2365 req->SetPkgName(pkgName);
2366 req->SetNetWorkId(networkId);
2367
2368 int32_t ret = ipcClientProxy_->SendRequest(GET_DEVICE_SCREEN_STATUS, req, rsp);
2369 if (ret != DM_OK) {
2370 LOGE("Send Request failed ret: %{public}d", ret);
2371 return ERR_DM_IPC_SEND_REQUEST_FAILED;
2372 }
2373
2374 ret = rsp->GetErrCode();
2375 if (ret != DM_OK) {
2376 LOGE("Failed with ret %{public}d", ret);
2377 return ret;
2378 }
2379 screenStatus = rsp->GetScreenStatus();
2380 return DM_OK;
2381 }
2382
GetNetworkIdByUdid(const std::string & pkgName,const std::string & udid,std::string & networkId)2383 int32_t DeviceManagerImpl::GetNetworkIdByUdid(const std::string &pkgName, const std::string &udid,
2384 std::string &networkId)
2385 {
2386 if (pkgName.empty() || udid.empty()) {
2387 LOGE("DeviceManagerImpl::GetNetworkIdByUdid error: Invalid para, pkgName: %{public}s, udid: %{public}s",
2388 pkgName.c_str(), GetAnonyString(udid).c_str());
2389 return ERR_DM_INPUT_PARA_INVALID;
2390 }
2391 LOGD("Start, pkgName: %{public}s", GetAnonyString(pkgName).c_str());
2392
2393 std::shared_ptr<IpcGetInfoByNetWorkReq> req = std::make_shared<IpcGetInfoByNetWorkReq>();
2394 std::shared_ptr<IpcGetInfoByNetWorkRsp> rsp = std::make_shared<IpcGetInfoByNetWorkRsp>();
2395 req->SetPkgName(pkgName);
2396 req->SetUdid(udid);
2397
2398 int32_t ret = ipcClientProxy_->SendRequest(GET_NETWORKID_BY_UDID, req, rsp);
2399 if (ret != DM_OK) {
2400 LOGI("GetNetworkIdByUdid Send Request failed ret: %{public}d", ret);
2401 return ERR_DM_IPC_SEND_REQUEST_FAILED;
2402 }
2403
2404 ret = rsp->GetErrCode();
2405 if (ret != DM_OK) {
2406 LOGE("GetNetworkIdByUdid Failed with ret %{public}d", ret);
2407 return ret;
2408 }
2409 networkId = rsp->GetNetWorkId();
2410 return DM_OK;
2411 }
2412
RegisterCredentialAuthStatusCallback(const std::string & pkgName,std::shared_ptr<CredentialAuthStatusCallback> callback)2413 int32_t DeviceManagerImpl::RegisterCredentialAuthStatusCallback(const std::string &pkgName,
2414 std::shared_ptr<CredentialAuthStatusCallback> callback)
2415 {
2416 if (pkgName.empty() || callback == nullptr) {
2417 LOGE("Error: Invalid para");
2418 return ERR_DM_INPUT_PARA_INVALID;
2419 }
2420 DeviceManagerNotify::GetInstance().RegisterCredentialAuthStatusCallback(pkgName, callback);
2421 SyncCallbackToService(DmCommonNotifyEvent::REG_CREDENTIAL_AUTH_STATUS_NOTIFY, pkgName);
2422 LOGI("Completed, pkgName: %{public}s", pkgName.c_str());
2423 return DM_OK;
2424 }
2425
UnRegisterCredentialAuthStatusCallback(const std::string & pkgName)2426 int32_t DeviceManagerImpl::UnRegisterCredentialAuthStatusCallback(const std::string &pkgName)
2427 {
2428 if (pkgName.empty()) {
2429 LOGE("Error: Invalid para");
2430 return ERR_DM_INPUT_PARA_INVALID;
2431 }
2432 SyncCallbackToService(DmCommonNotifyEvent::UN_REG_CREDENTIAL_AUTH_STATUS_NOTIFY, pkgName);
2433 DeviceManagerNotify::GetInstance().UnRegisterCredentialAuthStatusCallback(pkgName);
2434 LOGI("Completed, pkgName: %{public}s", pkgName.c_str());
2435 return DM_OK;
2436 }
2437
RegisterSinkBindCallback(const std::string & pkgName,std::shared_ptr<BindTargetCallback> callback)2438 int32_t DeviceManagerImpl::RegisterSinkBindCallback(const std::string &pkgName,
2439 std::shared_ptr<BindTargetCallback> callback)
2440 {
2441 if (pkgName.empty()) {
2442 LOGE("Error: Invalid para");
2443 return ERR_DM_INPUT_PARA_INVALID;
2444 }
2445 DeviceManagerNotify::GetInstance().RegisterSinkBindCallback(pkgName, callback);
2446 LOGI("Completed, pkgName: %{public}s", pkgName.c_str());
2447 return DM_OK;
2448 }
2449
UnRegisterSinkBindCallback(const std::string & pkgName)2450 int32_t DeviceManagerImpl::UnRegisterSinkBindCallback(const std::string &pkgName)
2451 {
2452 if (pkgName.empty()) {
2453 LOGE("Error: Invalid para");
2454 return ERR_DM_INPUT_PARA_INVALID;
2455 }
2456 DeviceManagerNotify::GetInstance().UnRegisterSinkBindCallback(pkgName);
2457 LOGI("Completed, pkgName: %{public}s", pkgName.c_str());
2458 return DM_OK;
2459 }
2460
GetAnonyLocalUdid(const std::string & pkgName,std::string & anonyUdid)2461 int32_t DeviceManagerImpl::GetAnonyLocalUdid(const std::string &pkgName, std::string &anonyUdid)
2462 {
2463 LOGD("Start");
2464 std::shared_ptr<IpcReq> req = std::make_shared<IpcReq>();
2465 std::shared_ptr<IpcGetAnonyLocalUdidRsp> rsp = std::make_shared<IpcGetAnonyLocalUdidRsp>();
2466 req->SetPkgName(pkgName);
2467 int32_t ret = ipcClientProxy_->SendRequest(GET_ANONY_LOCAL_UDID, req, rsp);
2468 if (ret != DM_OK) {
2469 LOGI("GetAnonyLocalUdid Send Request failed ret: %{public}d", ret);
2470 return ERR_DM_IPC_SEND_REQUEST_FAILED;
2471 }
2472
2473 ret = rsp->GetErrCode();
2474 if (ret != DM_OK) {
2475 LOGE("GetAnonyLocalUdid Failed with ret %{public}d", ret);
2476 return ret;
2477 }
2478 anonyUdid = rsp->GetAnonyUdid();
2479 return DM_OK;
2480 }
2481
GetSubscribeIdFromMap(const std::string & pkgName)2482 uint16_t DeviceManagerImpl::GetSubscribeIdFromMap(const std::string &pkgName)
2483 {
2484 {
2485 std::lock_guard<std::mutex> autoLock(subMapLock);
2486 if (pkgName2SubIdMap_.find(pkgName) != pkgName2SubIdMap_.end()) {
2487 return pkgName2SubIdMap_[pkgName];
2488 }
2489 }
2490 return DM_INVALID_FLAG_ID;
2491 }
2492
SyncCallbackToService(DmCommonNotifyEvent dmCommonNotifyEvent,const std::string & pkgName)2493 void DeviceManagerImpl::SyncCallbackToService(DmCommonNotifyEvent dmCommonNotifyEvent, const std::string &pkgName)
2494 {
2495 if (pkgName.empty()) {
2496 LOGE("Invalid parameter, pkgName is empty.");
2497 return;
2498 }
2499 if (!IsDmCommonNotifyEventValid(dmCommonNotifyEvent)) {
2500 LOGE("Invalid dmCommonNotifyEvent: %{public}d.", dmCommonNotifyEvent);
2501 return;
2502 }
2503 std::shared_ptr<IpcSyncCallbackReq> req = std::make_shared<IpcSyncCallbackReq>();
2504 std::shared_ptr<IpcRsp> rsp = std::make_shared<IpcRsp>();
2505 req->SetPkgName(pkgName);
2506 req->SetDmCommonNotifyEvent(static_cast<int32_t>(dmCommonNotifyEvent));
2507 int32_t ret = ipcClientProxy_->SendRequest(SYNC_CALLBACK, req, rsp);
2508 if (ret != DM_OK) {
2509 LOGI("Send Request failed ret: %{public}d", ret);
2510 return;
2511 }
2512 ret = rsp->GetErrCode();
2513 if (ret != DM_OK) {
2514 LOGE("Failed with ret %{public}d", ret);
2515 return;
2516 }
2517 }
2518
GetAllTrustedDeviceList(const std::string & pkgName,const std::string & extra,std::vector<DmDeviceInfo> & deviceList)2519 int32_t DeviceManagerImpl::GetAllTrustedDeviceList(const std::string &pkgName, const std::string &extra,
2520 std::vector<DmDeviceInfo> &deviceList)
2521 {
2522 if (pkgName.empty()) {
2523 DmRadarHelper::GetInstance().ReportGetTrustDeviceList(
2524 pkgName, "GetAllTrustedDeviceList", deviceList, ERR_DM_INPUT_PARA_INVALID, anonyLocalUdid_);
2525 LOGE("Invalid parameter, pkgName is empty.");
2526 return ERR_DM_INPUT_PARA_INVALID;
2527 }
2528 deviceList.clear();
2529 LOGI("Start, pkgName: %{public}s, extra: %{public}s", GetAnonyString(pkgName).c_str(),
2530 GetAnonyString(extra).c_str());
2531
2532 std::shared_ptr<IpcGetTrustDeviceReq> req = std::make_shared<IpcGetTrustDeviceReq>();
2533 std::shared_ptr<IpcGetTrustDeviceRsp> rsp = std::make_shared<IpcGetTrustDeviceRsp>();
2534 req->SetPkgName(pkgName);
2535 req->SetExtra(extra);
2536 int32_t ret = ipcClientProxy_->SendRequest(GET_ALL_TRUST_DEVICE_LIST, req, rsp);
2537 if (ret != DM_OK) {
2538 DmRadarHelper::GetInstance().ReportGetTrustDeviceList(pkgName, "GetAllTrustedDeviceList",
2539 deviceList, ret, anonyLocalUdid_);
2540 LOGE("DeviceManagerImpl::GetAllTrustedDeviceList error, Send Request failed ret: %{public}d", ret);
2541 return ERR_DM_IPC_SEND_REQUEST_FAILED;
2542 }
2543
2544 ret = rsp->GetErrCode();
2545 if (ret != DM_OK) {
2546 DmRadarHelper::GetInstance().ReportGetTrustDeviceList(pkgName, "GetAllTrustedDeviceList",
2547 deviceList, ret, anonyLocalUdid_);
2548 LOGE("GetAllTrustedDeviceList error, failed ret: %{public}d", ret);
2549 return ret;
2550 }
2551
2552 deviceList = rsp->GetDeviceVec();
2553 LOGI("Completed, device size %{public}zu", deviceList.size());
2554 DmRadarHelper::GetInstance().ReportGetTrustDeviceList(pkgName, "GetAllTrustedDeviceList",
2555 deviceList, DM_OK, anonyLocalUdid_);
2556 return DM_OK;
2557 }
2558
SyncCallbacksToService(std::map<DmCommonNotifyEvent,std::set<std::string>> & callbackMap)2559 void DeviceManagerImpl::SyncCallbacksToService(std::map<DmCommonNotifyEvent, std::set<std::string>> &callbackMap)
2560 {
2561 if (callbackMap.size() == 0) {
2562 LOGI("callbackMap is empty.");
2563 return;
2564 }
2565 for (auto iter : callbackMap) {
2566 if (iter.second.size() == 0) {
2567 continue;
2568 }
2569 for (auto item : iter.second) {
2570 SyncCallbackToService(iter.first, item);
2571 }
2572 }
2573 }
2574
RegisterAuthenticationType(const std::string & pkgName,const std::map<std::string,std::string> & authParam)2575 int32_t DeviceManagerImpl::RegisterAuthenticationType(const std::string &pkgName,
2576 const std::map<std::string, std::string> &authParam)
2577 {
2578 const size_t AUTH_TYPE_PARAM_SIZE = 1;
2579 if (pkgName.empty() || authParam.size() != AUTH_TYPE_PARAM_SIZE) {
2580 LOGE("Para invalid: authParam is less than one or pkgName is empty.");
2581 return ERR_DM_INPUT_PARA_INVALID;
2582 }
2583
2584 LOGI("Start");
2585 std::string authTypeStr = ConvertMapToJsonString(authParam);
2586
2587 std::shared_ptr<IpcCommonParamReq> req = std::make_shared<IpcCommonParamReq>();
2588 std::shared_ptr<IpcRsp> rsp = std::make_shared<IpcRsp>();
2589 req->SetPkgName(pkgName);
2590 req->SetFirstParam(authTypeStr);
2591 int32_t ret = ipcClientProxy_->SendRequest(REG_AUTHENTICATION_TYPE, req, rsp);
2592 if (ret != DM_OK) {
2593 LOGE("Send Request failed ret: %{public}d", ret);
2594 return ERR_DM_IPC_SEND_REQUEST_FAILED;
2595 }
2596 ret = rsp->GetErrCode();
2597 if (ret != DM_OK) {
2598 LOGE("Failed with ret %{public}d", ret);
2599 return ret;
2600 }
2601 LOGI("Completed");
2602 return DM_OK;
2603 }
2604
GetDeviceProfileInfoList(const std::string & pkgName,const DmDeviceProfileInfoFilterOptions & filterOptions,std::shared_ptr<GetDeviceProfileInfoListCallback> callback)2605 int32_t DeviceManagerImpl::GetDeviceProfileInfoList(const std::string &pkgName,
2606 const DmDeviceProfileInfoFilterOptions &filterOptions, std::shared_ptr<GetDeviceProfileInfoListCallback> callback)
2607 {
2608 LOGI("In pkgName:%{public}s, isCloud:%{public}d", pkgName.c_str(), filterOptions.isCloud);
2609 int32_t ret = DeviceManagerNotify::GetInstance().RegisterGetDeviceProfileInfoListCallback(pkgName, callback);
2610 if (ret != DM_OK) {
2611 LOGE("register callback error, ret: %{public}d", ret);
2612 return ret;
2613 }
2614 std::shared_ptr<IpcGetDeviceProfileInfoListReq> req = std::make_shared<IpcGetDeviceProfileInfoListReq>();
2615 std::shared_ptr<IpcRsp> rsp = std::make_shared<IpcRsp>();
2616 req->SetPkgName(pkgName);
2617 req->SetFilterOptions(filterOptions);
2618 ret = ipcClientProxy_->SendRequest(GET_DEVICE_PROFILE_INFO_LIST, req, rsp);
2619 if (ret != DM_OK) {
2620 LOGE("error: Send Request failed ret: %{public}d", ret);
2621 DeviceManagerNotify::GetInstance().OnGetDeviceProfileInfoListResult(pkgName, {},
2622 ERR_DM_IPC_SEND_REQUEST_FAILED);
2623 return ERR_DM_IPC_SEND_REQUEST_FAILED;
2624 }
2625 ret = rsp->GetErrCode();
2626 if (ret != DM_OK) {
2627 LOGE("error: Failed with ret %{public}d", ret);
2628 DeviceManagerNotify::GetInstance().OnGetDeviceProfileInfoListResult(pkgName, {}, ret);
2629 return ret;
2630 }
2631 LOGI("Completed");
2632 return DM_OK;
2633 }
2634
GetDeviceIconInfo(const std::string & pkgName,const DmDeviceIconInfoFilterOptions & filterOptions,std::shared_ptr<GetDeviceIconInfoCallback> callback)2635 int32_t DeviceManagerImpl::GetDeviceIconInfo(const std::string &pkgName,
2636 const DmDeviceIconInfoFilterOptions &filterOptions, std::shared_ptr<GetDeviceIconInfoCallback> callback)
2637 {
2638 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
2639 std::string uk = IpcModelCodec::GetDeviceIconInfoUniqueKey(filterOptions);
2640 LOGI("In pkgName:%{public}s, uk:%{public}s", pkgName.c_str(), uk.c_str());
2641 int32_t ret = DeviceManagerNotify::GetInstance().RegisterGetDeviceIconInfoCallback(pkgName, uk, callback);
2642 if (ret != DM_OK) {
2643 LOGE("Register Callback failed ret: %{public}d", ret);
2644 return ret;
2645 }
2646 std::shared_ptr<IpcGetDeviceIconInfoReq> req = std::make_shared<IpcGetDeviceIconInfoReq>();
2647 std::shared_ptr<IpcRsp> rsp = std::make_shared<IpcRsp>();
2648 req->SetPkgName(pkgName);
2649 req->SetFilterOptions(filterOptions);
2650 ret = ipcClientProxy_->SendRequest(GET_DEVICE_ICON_INFO, req, rsp);
2651 if (ret != DM_OK) {
2652 LOGE("Send Request failed ret: %{public}d", ret);
2653 DmDeviceIconInfo deviceIconInfo;
2654 deviceIconInfo.InitByDmDeviceIconInfoFilterOptions(filterOptions);
2655 DeviceManagerNotify::GetInstance().OnGetDeviceIconInfoResult(pkgName, deviceIconInfo,
2656 ERR_DM_IPC_SEND_REQUEST_FAILED);
2657 return ERR_DM_IPC_SEND_REQUEST_FAILED;
2658 }
2659 ret = rsp->GetErrCode();
2660 if (ret != DM_OK) {
2661 LOGE("Failed with ret %{public}d", ret);
2662 DmDeviceIconInfo deviceIconInfo;
2663 deviceIconInfo.InitByDmDeviceIconInfoFilterOptions(filterOptions);
2664 DeviceManagerNotify::GetInstance().OnGetDeviceIconInfoResult(pkgName, deviceIconInfo, ret);
2665 return ret;
2666 }
2667 #endif
2668 (void)pkgName;
2669 (void)filterOptions;
2670 (void)callback;
2671 return DM_OK;
2672 }
2673
PutDeviceProfileInfoList(const std::string & pkgName,const std::vector<OHOS::DistributedHardware::DmDeviceProfileInfo> & deviceProfileInfoList)2674 int32_t DeviceManagerImpl::PutDeviceProfileInfoList(const std::string &pkgName,
2675 const std::vector<OHOS::DistributedHardware::DmDeviceProfileInfo> &deviceProfileInfoList)
2676 {
2677 LOGI("In pkgName:%{public}s,", pkgName.c_str());
2678 std::shared_ptr<IpcPutDeviceProfileInfoListReq> req = std::make_shared<IpcPutDeviceProfileInfoListReq>();
2679 std::shared_ptr<IpcRsp> rsp = std::make_shared<IpcRsp>();
2680 req->SetPkgName(pkgName);
2681 req->SetDeviceProfileInfoList(deviceProfileInfoList);
2682 int32_t ret = ipcClientProxy_->SendRequest(PUT_DEVICE_PROFILE_INFO_LIST, req, rsp);
2683 if (ret != DM_OK) {
2684 LOGE("error: Send Request failed ret: %{public}d", ret);
2685 return ERR_DM_IPC_SEND_REQUEST_FAILED;
2686 }
2687 ret = rsp->GetErrCode();
2688 if (ret != DM_OK) {
2689 LOGE("error: Failed with ret %{public}d", ret);
2690 return ret;
2691 }
2692 LOGI("Completed");
2693 return DM_OK;
2694 }
2695
GetLocalDisplayDeviceName(const std::string & pkgName,int32_t maxNameLength,std::string & displayName)2696 int32_t DeviceManagerImpl::GetLocalDisplayDeviceName(const std::string &pkgName, int32_t maxNameLength,
2697 std::string &displayName)
2698 {
2699 LOGI("In pkgName:%{public}s,", pkgName.c_str());
2700 std::shared_ptr<IpcGetLocalDisplayDeviceNameReq> req = std::make_shared<IpcGetLocalDisplayDeviceNameReq>();
2701 std::shared_ptr<IpcGetLocalDisplayDeviceNameRsp> rsp = std::make_shared<IpcGetLocalDisplayDeviceNameRsp>();
2702 req->SetPkgName(pkgName);
2703 req->SetMaxNameLength(maxNameLength);
2704 int32_t ret = ipcClientProxy_->SendRequest(GET_LOCAL_DISPLAY_DEVICE_NAME, req, rsp);
2705 if (ret != DM_OK) {
2706 return ERR_DM_IPC_SEND_REQUEST_FAILED;
2707 LOGE("error: Send Request failed ret: %{public}d", ret);
2708 }
2709 ret = rsp->GetErrCode();
2710 if (ret != DM_OK) {
2711 LOGE("error: Failed with ret %{public}d", ret);
2712 return ret;
2713 }
2714 displayName = rsp->GetDisplayName();
2715 LOGI("Completed");
2716 return DM_OK;
2717 }
2718
RegisterLocalServiceInfo(const DMLocalServiceInfo & info)2719 int32_t DeviceManagerImpl::RegisterLocalServiceInfo(const DMLocalServiceInfo &info)
2720 {
2721 LOGI("Start");
2722 std::shared_ptr<IpcRegServiceInfoReq> req = std::make_shared<IpcRegServiceInfoReq>();
2723 std::shared_ptr<IpcRsp> rsp = std::make_shared<IpcRsp>();
2724 req->SetLocalServiceInfo(info);
2725 int32_t ret = ipcClientProxy_->SendRequest(REG_LOCALSERVICE_INFO, req, rsp);
2726 if (ret != DM_OK) {
2727 LOGE("Send Request failed ret: %{public}d", ret);
2728 return ERR_DM_IPC_SEND_REQUEST_FAILED;
2729 }
2730 ret = rsp->GetErrCode();
2731 if (ret != DM_OK) {
2732 LOGE("Failed with ret %{public}d", ret);
2733 return ret;
2734 }
2735 LOGI("Completed");
2736 return DM_OK;
2737 }
2738
UnRegisterLocalServiceInfo(const std::string & bundleName,int32_t pinExchangeType)2739 int32_t DeviceManagerImpl::UnRegisterLocalServiceInfo(const std::string &bundleName, int32_t pinExchangeType)
2740 {
2741 LOGI("Start");
2742 std::shared_ptr<IpcCommonParamReq> req = std::make_shared<IpcCommonParamReq>();
2743 std::shared_ptr<IpcRsp> rsp = std::make_shared<IpcRsp>();
2744 req->SetFirstParam(bundleName);
2745 req->SetInt32Param(pinExchangeType);
2746 int32_t ret = ipcClientProxy_->SendRequest(UNREG_LOCALSERVICE_INFO, req, rsp);
2747 if (ret != DM_OK) {
2748 LOGE("Send Request failed ret: %{public}d", ret);
2749 return ERR_DM_IPC_SEND_REQUEST_FAILED;
2750 }
2751 ret = rsp->GetErrCode();
2752 if (ret != DM_OK) {
2753 LOGE("Failed with ret %{public}d", ret);
2754 return ret;
2755 }
2756 LOGI("Completed");
2757 return DM_OK;
2758 }
2759
UpdateLocalServiceInfo(const DMLocalServiceInfo & info)2760 int32_t DeviceManagerImpl::UpdateLocalServiceInfo(const DMLocalServiceInfo &info)
2761 {
2762 LOGI("Start");
2763 std::shared_ptr<IpcRegServiceInfoReq> req = std::make_shared<IpcRegServiceInfoReq>();
2764 std::shared_ptr<IpcRsp> rsp = std::make_shared<IpcRsp>();
2765 req->SetLocalServiceInfo(info);
2766 int32_t ret = ipcClientProxy_->SendRequest(UPDATE_LOCALSERVICE_INFO, req, rsp);
2767 if (ret != DM_OK) {
2768 LOGE("Send Request failed ret: %{public}d", ret);
2769 return ERR_DM_IPC_SEND_REQUEST_FAILED;
2770 }
2771 ret = rsp->GetErrCode();
2772 if (ret != DM_OK) {
2773 LOGE("Failed with ret %{public}d", ret);
2774 return ret;
2775 }
2776 LOGI("Completed");
2777 return DM_OK;
2778 }
2779
GetLocalServiceInfoByBundleNameAndPinExchangeType(const std::string & bundleName,int32_t pinExchangeType,DMLocalServiceInfo & info)2780 int32_t DeviceManagerImpl::GetLocalServiceInfoByBundleNameAndPinExchangeType(
2781 const std::string &bundleName, int32_t pinExchangeType, DMLocalServiceInfo &info)
2782 {
2783 std::shared_ptr<IpcCommonParamReq> req = std::make_shared<IpcCommonParamReq>();
2784 std::shared_ptr<IpcGetLocalServiceInfoRsp> rsp = std::make_shared<IpcGetLocalServiceInfoRsp>();
2785 req->SetFirstParam(bundleName);
2786 req->SetInt32Param(pinExchangeType);
2787 int32_t ret = ipcClientProxy_->SendRequest(GET_SERVICEINFO_BYBUNDLENAME_PINEXCHANGETYPE, req, rsp);
2788 if (ret != DM_OK) {
2789 LOGE("Send Request failed ret: %{public}d", ret);
2790 return ERR_DM_IPC_SEND_REQUEST_FAILED;
2791 }
2792 ret = rsp->GetErrCode();
2793 if (ret != DM_OK) {
2794 LOGE("Failed with ret %{public}d", ret);
2795 return ret;
2796 }
2797 info = rsp->GetLocalServiceInfo();
2798 LOGI("Completed");
2799 return DM_OK;
2800 }
2801
SetLocalDeviceName(const std::string & pkgName,const std::string & deviceName,std::shared_ptr<SetLocalDeviceNameCallback> callback)2802 int32_t DeviceManagerImpl::SetLocalDeviceName(const std::string &pkgName, const std::string &deviceName,
2803 std::shared_ptr<SetLocalDeviceNameCallback> callback)
2804 {
2805 LOGI("Start, pkgName: %{public}s", pkgName.c_str());
2806 int32_t ret = DeviceManagerNotify::GetInstance().RegisterSetLocalDeviceNameCallback(pkgName, callback);
2807 if (ret != DM_OK) {
2808 LOGE("Register Callback failed ret: %{public}d", ret);
2809 return ret;
2810 }
2811 if (pkgName.empty() || deviceName.empty() || deviceName.size() > DEVICE_NAME_MAX_BYTES) {
2812 LOGE("param invalid, pkgName=%{public}s, deviceName=%{public}s",
2813 pkgName.c_str(), GetAnonyString(deviceName).c_str());
2814 DeviceManagerNotify::GetInstance().OnSetLocalDeviceNameResult(pkgName, ERR_DM_INPUT_PARA_INVALID);
2815 return ERR_DM_INPUT_PARA_INVALID;
2816 }
2817 std::shared_ptr<IpcSetLocalDeviceNameReq> req = std::make_shared<IpcSetLocalDeviceNameReq>();
2818 std::shared_ptr<IpcRsp> rsp = std::make_shared<IpcRsp>();
2819 req->SetPkgName(pkgName);
2820 req->SetDeviceName(deviceName);
2821 ret = ipcClientProxy_->SendRequest(SET_LOCAL_DEVICE_NAME, req, rsp);
2822 if (ret != DM_OK) {
2823 LOGE("error: Send Request failed ret: %{public}d", ret);
2824 DeviceManagerNotify::GetInstance().OnSetLocalDeviceNameResult(pkgName, ERR_DM_IPC_SEND_REQUEST_FAILED);
2825 return ERR_DM_IPC_SEND_REQUEST_FAILED;
2826 }
2827 ret = rsp->GetErrCode();
2828 if (ret != DM_OK) {
2829 LOGE("error: Failed with ret %{public}d", ret);
2830 DeviceManagerNotify::GetInstance().OnSetLocalDeviceNameResult(pkgName, ret);
2831 return ret;
2832 }
2833 LOGI("Completed");
2834 return DM_OK;
2835 }
2836
SetRemoteDeviceName(const std::string & pkgName,const std::string & deviceId,const std::string & deviceName,std::shared_ptr<SetRemoteDeviceNameCallback> callback)2837 int32_t DeviceManagerImpl::SetRemoteDeviceName(const std::string &pkgName, const std::string &deviceId,
2838 const std::string &deviceName, std::shared_ptr<SetRemoteDeviceNameCallback> callback)
2839 {
2840 LOGI("Start, pkgName: %{public}s", pkgName.c_str());
2841 int32_t ret = DeviceManagerNotify::GetInstance().RegisterSetRemoteDeviceNameCallback(pkgName, deviceId, callback);
2842 if (ret != DM_OK) {
2843 LOGE("Register Callback failed ret: %{public}d", ret);
2844 return ret;
2845 }
2846 if (pkgName.empty() || deviceName.empty() || deviceName.size() > DEVICE_NAME_MAX_BYTES || deviceId.empty()) {
2847 LOGE("param invalid, pkgName=%{public}s, deviceName=%{public}s, deviceId=%{public}s",
2848 pkgName.c_str(), GetAnonyString(deviceName).c_str(), GetAnonyString(deviceId).c_str());
2849 DeviceManagerNotify::GetInstance().OnSetRemoteDeviceNameResult(pkgName, deviceId, ERR_DM_INPUT_PARA_INVALID);
2850 return ERR_DM_INPUT_PARA_INVALID;
2851 }
2852 std::shared_ptr<IpcSetRemoteDeviceNameReq> req = std::make_shared<IpcSetRemoteDeviceNameReq>();
2853 std::shared_ptr<IpcRsp> rsp = std::make_shared<IpcRsp>();
2854 req->SetPkgName(pkgName);
2855 req->SetDeviceName(deviceName);
2856 req->SetDeviceId(deviceId);
2857 ret = ipcClientProxy_->SendRequest(SET_REMOTE_DEVICE_NAME, req, rsp);
2858 if (ret != DM_OK) {
2859 LOGE("error: Send Request failed ret: %{public}d", ret);
2860 DeviceManagerNotify::GetInstance().OnSetRemoteDeviceNameResult(pkgName, deviceId,
2861 ERR_DM_IPC_SEND_REQUEST_FAILED);
2862 return ERR_DM_IPC_SEND_REQUEST_FAILED;
2863 }
2864 ret = rsp->GetErrCode();
2865 if (ret != DM_OK) {
2866 LOGE("error: Failed with ret %{public}d", ret);
2867 DeviceManagerNotify::GetInstance().OnSetRemoteDeviceNameResult(pkgName, deviceId, ret);
2868 return ret;
2869 }
2870 LOGI("Completed");
2871 return DM_OK;
2872 }
2873
RestoreLocalDeviceName(const std::string & pkgName)2874 int32_t DeviceManagerImpl::RestoreLocalDeviceName(const std::string &pkgName)
2875 {
2876 if (pkgName.empty()) {
2877 LOGE("param invalid, pkgName : %{public}s", pkgName.c_str());
2878 return ERR_DM_INPUT_PARA_INVALID;
2879 }
2880 std::shared_ptr<IpcReq> req = std::make_shared<IpcReq>();
2881 std::shared_ptr<IpcRsp> rsp = std::make_shared<IpcRsp>();
2882 req->SetPkgName(pkgName);
2883 int32_t ret = ipcClientProxy_->SendRequest(RESTORE_LOCAL_DEVICE_NAME, req, rsp);
2884 if (ret != DM_OK) {
2885 LOGE("error: Send Request failed ret: %{public}d", ret);
2886 return ERR_DM_IPC_SEND_REQUEST_FAILED;
2887 }
2888 ret = rsp->GetErrCode();
2889 if (ret != DM_OK) {
2890 LOGE("error: Failed with ret %{public}d", ret);
2891 return ret;
2892 }
2893 LOGI("Completed");
2894 return DM_OK;
2895 }
2896
GetDeviceNetworkIdList(const std::string & bundleName,const NetworkIdQueryFilter & queryFilter,std::vector<std::string> & networkIds)2897 int32_t DeviceManagerImpl::GetDeviceNetworkIdList(const std::string &bundleName,
2898 const NetworkIdQueryFilter &queryFilter, std::vector<std::string> &networkIds)
2899 {
2900 std::shared_ptr<IpcGetDeviceNetworkIdListReq> req = std::make_shared<IpcGetDeviceNetworkIdListReq>();
2901 std::shared_ptr<IpcGetDeviceNetworkIdListRsp> rsp = std::make_shared<IpcGetDeviceNetworkIdListRsp>();
2902 req->SetPkgName(bundleName);
2903 req->SetQueryFilter(queryFilter);
2904 int32_t ret = ipcClientProxy_->SendRequest(GET_DEVICE_NETWORK_ID_LIST, req, rsp);
2905 if (ret != DM_OK) {
2906 LOGE("Send Request failed ret: %{public}d", ret);
2907 return ERR_DM_IPC_SEND_REQUEST_FAILED;
2908 }
2909 ret = rsp->GetErrCode();
2910 if (ret != DM_OK) {
2911 LOGE("Failed with ret %{public}d", ret);
2912 return ret;
2913 }
2914 networkIds = rsp->GetNetworkIds();
2915 LOGI("Completed");
2916 return DM_OK;
2917 }
2918
UnRegisterPinHolderCallback(const std::string & pkgName)2919 int32_t DeviceManagerImpl::UnRegisterPinHolderCallback(const std::string &pkgName)
2920 {
2921 if (pkgName.empty()) {
2922 LOGE("error: Invalid para");
2923 return ERR_DM_INPUT_PARA_INVALID;
2924 }
2925 DeviceManagerNotify::GetInstance().UnRegisterPinHolderCallback(pkgName);
2926 std::shared_ptr<IpcReq> req = std::make_shared<IpcReq>();
2927 req->SetPkgName(pkgName);
2928 std::shared_ptr<IpcRsp> rsp = std::make_shared<IpcRsp>();
2929
2930 int32_t ret = ipcClientProxy_->SendRequest(UNREGISTER_PIN_HOLDER_CALLBACK, req, rsp);
2931 if (ret != DM_OK) {
2932 LOGI("Send Request failed ret: %{public}d", ret);
2933 return ERR_DM_IPC_SEND_REQUEST_FAILED;
2934 }
2935 ret = rsp->GetErrCode();
2936 if (ret != DM_OK) {
2937 LOGE("Failed with ret %{public}d", ret);
2938 return ret;
2939 }
2940 return DM_OK;
2941 }
2942
CheckAccessControl(const DmAccessCaller & caller,const DmAccessCallee & callee)2943 bool DeviceManagerImpl::CheckAccessControl(const DmAccessCaller &caller, const DmAccessCallee &callee)
2944 {
2945 LOGI("Start");
2946 return CheckAclByIpcCode(caller, callee, CHECK_ACCESS_CONTROL);
2947 }
2948
CheckIsSameAccount(const DmAccessCaller & caller,const DmAccessCallee & callee)2949 bool DeviceManagerImpl::CheckIsSameAccount(const DmAccessCaller &caller, const DmAccessCallee &callee)
2950 {
2951 LOGI("Start");
2952 return CheckAclByIpcCode(caller, callee, CHECK_SAME_ACCOUNT);
2953 }
2954
2955
CheckSrcAccessControl(const DmAccessCaller & caller,const DmAccessCallee & callee)2956 bool DeviceManagerImpl::CheckSrcAccessControl(const DmAccessCaller &caller, const DmAccessCallee &callee)
2957 {
2958 LOGI("Start");
2959 return CheckAclByIpcCode(caller, callee, CHECK_SRC_ACCESS_CONTROL);
2960 }
2961
CheckSinkAccessControl(const DmAccessCaller & caller,const DmAccessCallee & callee)2962 bool DeviceManagerImpl::CheckSinkAccessControl(const DmAccessCaller &caller, const DmAccessCallee &callee)
2963 {
2964 LOGI("Start");
2965 return CheckAclByIpcCode(caller, callee, CHECK_SINK_ACCESS_CONTROL);
2966 }
2967
CheckSrcIsSameAccount(const DmAccessCaller & caller,const DmAccessCallee & callee)2968 bool DeviceManagerImpl::CheckSrcIsSameAccount(const DmAccessCaller &caller, const DmAccessCallee &callee)
2969 {
2970 LOGI("Start");
2971 return CheckAclByIpcCode(caller, callee, CHECK_SRC_SAME_ACCOUNT);
2972 }
2973
CheckSinkIsSameAccount(const DmAccessCaller & caller,const DmAccessCallee & callee)2974 bool DeviceManagerImpl::CheckSinkIsSameAccount(const DmAccessCaller &caller, const DmAccessCallee &callee)
2975 {
2976 LOGI("Start");
2977 return CheckAclByIpcCode(caller, callee, CHECK_SINK_SAME_ACCOUNT);
2978 }
2979
CheckAclByIpcCode(const DmAccessCaller & caller,const DmAccessCallee & callee,const DMIpcCmdInterfaceCode & ipcCode)2980 bool DeviceManagerImpl::CheckAclByIpcCode(const DmAccessCaller &caller, const DmAccessCallee &callee,
2981 const DMIpcCmdInterfaceCode &ipcCode)
2982 {
2983 LOGI("start, ipcCode %{public}d.", static_cast<int32_t>(ipcCode));
2984 std::shared_ptr<IpcCheckAcl> req = std::make_shared<IpcCheckAcl>();
2985 std::shared_ptr<IpcRsp> rsp = std::make_shared<IpcRsp>();
2986 req->SetAccessCaller(caller);
2987 req->SetAccessCallee(callee);
2988 int32_t ret = ipcClientProxy_->SendRequest(ipcCode, req, rsp);
2989 if (ret != DM_OK) {
2990 DmRadarHelper::GetInstance().ReportDmBehavior(caller.pkgName, "CheckAclByIpcCode", ret, anonyLocalUdid_);
2991 LOGE("CheckIsSameAccount Send Request failed ret: %{public}d", ret);
2992 return false;
2993 }
2994 bool result = static_cast<bool>(rsp->GetErrCode());
2995 DmRadarHelper::GetInstance().ReportDmBehavior(caller.pkgName, "CheckAclByIpcCode", static_cast<int32_t>(result),
2996 anonyLocalUdid_);
2997 return result;
2998 }
2999 } // namespace DistributedHardware
3000 } // namespace OHOS
3001