• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "device_manager_impl.h"
17 #include <unistd.h>
18 #include <random>
19 #include "device_manager_ipc_interface_code.h"
20 #include "device_manager_notify.h"
21 #include "dm_anonymous.h"
22 #include "dm_constants.h"
23 #include "dm_dfx_constants.h"
24 #include "dm_hisysevent.h"
25 #include "dm_hitrace.h"
26 #include "dm_log.h"
27 #include "ipc_authenticate_device_req.h"
28 #include "ipc_bind_device_req.h"
29 #include "ipc_generate_encrypted_uuid_req.h"
30 #include "ipc_get_device_info_rsp.h"
31 #include "ipc_get_dmfaparam_rsp.h"
32 #include "ipc_get_encrypted_uuid_req.h"
33 #include "ipc_get_info_by_network_req.h"
34 #include "ipc_get_info_by_network_rsp.h"
35 #include "ipc_get_local_device_info_rsp.h"
36 #include "ipc_get_local_device_networkId_rsp.h"
37 #include "ipc_get_local_deviceId_rsp.h"
38 #include "ipc_get_local_device_name_rsp.h"
39 #include "ipc_get_local_device_type_rsp.h"
40 #include "ipc_get_trustdevice_req.h"
41 #include "ipc_get_trustdevice_rsp.h"
42 #include "ipc_get_availabledevice_req.h"
43 #include "ipc_get_availabledevice_rsp.h"
44 #include "ipc_notify_event_req.h"
45 #include "ipc_publish_req.h"
46 #include "ipc_req.h"
47 #include "ipc_rsp.h"
48 #include "ipc_set_credential_req.h"
49 #include "ipc_set_credential_rsp.h"
50 #include "ipc_set_useroperation_req.h"
51 #include "ipc_start_discovery_req.h"
52 #include "ipc_start_discover_req.h"
53 #include "ipc_stop_discovery_req.h"
54 #include "ipc_unauthenticate_device_req.h"
55 #include "ipc_unbind_device_req.h"
56 #include "ipc_unpublish_req.h"
57 #include "ipc_verify_authenticate_req.h"
58 #include "ipc_register_dev_state_callback_req.h"
59 #include "securec.h"
60 
61 namespace OHOS {
62 namespace DistributedHardware {
63 const int32_t SLEEP_TIME_MS = 50000; // 50ms
64 
65 constexpr const char* DM_INIT_DEVICE_MANAGER_SUCCESS = "DM_INIT_DEVICE_MANAGER_SUCCESS";
66 constexpr const char* DM_INIT_DEVICE_MANAGER_FAILED = "DM_INIT_DEVICE_MANAGER_FAILED";
67 constexpr const char* START_DEVICE_DISCOVERY_SUCCESS = "START_DEVICE_DISCOVERY_SUCCESS";
68 constexpr const char* START_DEVICE_DISCOVERY_FAILED = "START_DEVICE_DISCOVERY_FAILED";
69 constexpr const char* GET_LOCAL_DEVICE_INFO_SUCCESS = "GET_LOCAL_DEVICE_INFO_SUCCESS";
70 constexpr const char* GET_LOCAL_DEVICE_INFO_FAILED = "GET_LOCAL_DEVICE_INFO_FAILED";
71 constexpr const char* DM_SEND_REQUEST_SUCCESS = "DM_SEND_REQUEST_SUCCESS";
72 constexpr const char* DM_SEND_REQUEST_FAILED = "DM_SEND_REQUEST_FAILED";
73 constexpr const char* UNAUTHENTICATE_DEVICE_SUCCESS = "UNAUTHENTICATE_DEVICE_SUCCESS";
74 constexpr const char* UNAUTHENTICATE_DEVICE_FAILED = "UNAUTHENTICATE_DEVICE_FAILED";
75 constexpr const char* DM_INIT_DEVICE_MANAGER_SUCCESS_MSG = "init devicemanager success.";
76 constexpr const char* DM_INIT_DEVICE_MANAGER_FAILED_MSG = "init devicemanager failed.";
77 constexpr const char* START_DEVICE_DISCOVERY_SUCCESS_MSG = "device manager discovery success.";
78 constexpr const char* START_DEVICE_DISCOVERY_FAILED_MSG = "device manager discovery failed.";
79 constexpr const char* GET_LOCAL_DEVICE_INFO_SUCCESS_MSG = "get local device info success.";
80 constexpr const char* GET_LOCAL_DEVICE_INFO_FAILED_MSG = "get local device info failed.";
81 constexpr const char* DM_SEND_REQUEST_SUCCESS_MSG = "send request success.";
82 constexpr const char* DM_SEND_REQUEST_FAILED_MSG = "send request failed.";
83 constexpr const char* UNAUTHENTICATE_DEVICE_SUCCESS_MSG = "unauthenticate device success.";
84 constexpr const char* UNAUTHENTICATE_DEVICE_FAILED_MSG = "unauthenticate device failed.";
85 constexpr const char* DM_HITRACE_START_DEVICE = "DM_HITRACE_START_DEVICE";
86 constexpr const char* DM_HITRACE_GET_LOCAL_DEVICE_INFO = "DM_HITRACE_GET_LOCAL_DEVICE_INFO";
87 constexpr const char* DM_HITRACE_AUTH_TO_CONSULT = "DM_HITRACE_AUTH_TO_CONSULT";
88 constexpr const char* DM_HITRACE_INIT = "DM_HITRACE_INIT";
89 const uint16_t DM_MAX_RANDOM = 65535;
90 
GenRandUint(uint16_t randMin,uint16_t randMax)91 uint16_t GenRandUint(uint16_t randMin, uint16_t randMax)
92 {
93     std::random_device randDevice;
94     std::mt19937 genRand(randDevice());
95     std::uniform_int_distribution<int> disRand(randMin, randMax);
96     return disRand(genRand);
97 }
98 
GetInstance()99 DeviceManagerImpl &DeviceManagerImpl::GetInstance()
100 {
101     static DeviceManagerImpl instance;
102     return instance;
103 }
104 
InitDeviceManager(const std::string & pkgName,std::shared_ptr<DmInitCallback> dmInitCallback)105 int32_t DeviceManagerImpl::InitDeviceManager(const std::string &pkgName, std::shared_ptr<DmInitCallback> dmInitCallback)
106 {
107     if (pkgName.empty() || dmInitCallback == nullptr) {
108         LOGE("DeviceManagerImpl::InitDeviceManager error: Invalid parameter, pkgName: %s", pkgName.c_str());
109         return ERR_DM_INPUT_PARA_INVALID;
110     }
111     DmTraceStart(std::string(DM_HITRACE_INIT));
112     LOGI("InitDeviceManager start, pkgName: %s", pkgName.c_str());
113 
114     int32_t ret = DM_OK;
115     int32_t retryNum = 0;
116     while (retryNum < SERVICE_INIT_TRY_MAX_NUM) {
117         ret = ipcClientProxy_->Init(pkgName);
118         if (ret != ERR_DM_NOT_INIT) {
119             break;
120         }
121         usleep(SLEEP_TIME_MS);
122         retryNum++;
123         if (retryNum == SERVICE_INIT_TRY_MAX_NUM) {
124             LOGE("InitDeviceManager error, wait for device manager service starting timeout.");
125             return ERR_DM_NOT_INIT;
126         }
127     }
128     if (ret != DM_OK) {
129         LOGE("InitDeviceManager error, proxy init failed ret: %d", ret);
130         SysEventWrite(std::string(DM_INIT_DEVICE_MANAGER_FAILED), DM_HISYEVENT_FAULT,
131             std::string(DM_INIT_DEVICE_MANAGER_FAILED_MSG));
132         return ERR_DM_INIT_FAILED;
133     }
134 
135     DeviceManagerNotify::GetInstance().RegisterDeathRecipientCallback(pkgName, dmInitCallback);
136     DmTraceEnd();
137     LOGI("InitDeviceManager success");
138     SysEventWrite(std::string(DM_INIT_DEVICE_MANAGER_SUCCESS), DM_HISYEVENT_BEHAVIOR,
139         std::string(DM_INIT_DEVICE_MANAGER_SUCCESS_MSG));
140     return DM_OK;
141 }
142 
UnInitDeviceManager(const std::string & pkgName)143 int32_t DeviceManagerImpl::UnInitDeviceManager(const std::string &pkgName)
144 {
145     if (pkgName.empty()) {
146         LOGE("UnInitDeviceManager Invalid parameter, pkgName is empty.");
147         return ERR_DM_INPUT_PARA_INVALID;
148     }
149     LOGI("UnInitDeviceManager start, pkgName: %s", pkgName.c_str());
150 
151     int32_t ret = ipcClientProxy_->UnInit(pkgName);
152     if (ret != DM_OK) {
153         LOGE("UnInitDeviceManager error, proxy unInit failed ret: %d", ret);
154         return ERR_DM_FAILED;
155     }
156 
157     DeviceManagerNotify::GetInstance().UnRegisterPackageCallback(pkgName);
158     LOGI("UnInitDeviceManager success");
159     return DM_OK;
160 }
161 
GetTrustedDeviceList(const std::string & pkgName,const std::string & extra,std::vector<DmDeviceInfo> & deviceList)162 int32_t DeviceManagerImpl::GetTrustedDeviceList(const std::string &pkgName, const std::string &extra,
163                                                 std::vector<DmDeviceInfo> &deviceList)
164 {
165     if (pkgName.empty()) {
166         LOGE("Invalid parameter, pkgName is empty.");
167         return ERR_DM_INPUT_PARA_INVALID;
168     }
169     LOGI("GetTrustedDeviceList start, pkgName: %s, extra: %s", pkgName.c_str(), extra.c_str());
170 
171     std::shared_ptr<IpcGetTrustDeviceReq> req = std::make_shared<IpcGetTrustDeviceReq>();
172     std::shared_ptr<IpcGetTrustDeviceRsp> rsp = std::make_shared<IpcGetTrustDeviceRsp>();
173     req->SetPkgName(pkgName);
174     req->SetExtra(extra);
175     int32_t ret = ipcClientProxy_->SendRequest(GET_TRUST_DEVICE_LIST, req, rsp);
176     if (ret != DM_OK) {
177         LOGE("DeviceManagerImpl::GetTrustedDeviceList error, Send Request failed ret: %d", ret);
178         return ERR_DM_IPC_SEND_REQUEST_FAILED;
179     }
180 
181     ret = rsp->GetErrCode();
182     if (ret != DM_OK) {
183         LOGI("GetTrustedDeviceList error, failed ret: %d", ret);
184         return ret;
185     }
186 
187     deviceList = rsp->GetDeviceVec();
188     LOGI("DeviceManagerImpl::GetTrustedDeviceList completed, pkgName: %s", pkgName.c_str());
189     return DM_OK;
190 }
191 
GetTrustedDeviceList(const std::string & pkgName,const std::string & extra,bool isRefresh,std::vector<DmDeviceInfo> & deviceList)192 int32_t DeviceManagerImpl::GetTrustedDeviceList(const std::string &pkgName, const std::string &extra,
193                                                 bool isRefresh, std::vector<DmDeviceInfo> &deviceList)
194 {
195     if (pkgName.empty()) {
196         LOGE("Invalid parameter, pkgName is empty.");
197         return ERR_DM_INPUT_PARA_INVALID;
198     }
199     LOGI("GetTrustedDeviceList start, pkgName: %s, extra: %s, isRefresh: %d", pkgName.c_str(), extra.c_str(),
200         isRefresh);
201 
202     std::shared_ptr<IpcGetTrustDeviceReq> req = std::make_shared<IpcGetTrustDeviceReq>();
203     std::shared_ptr<IpcGetTrustDeviceRsp> rsp = std::make_shared<IpcGetTrustDeviceRsp>();
204     req->SetPkgName(pkgName);
205     req->SetExtra(extra);
206     req->SetRefresh(isRefresh);
207     int32_t ret = ipcClientProxy_->SendRequest(GET_TRUST_DEVICE_LIST, req, rsp);
208     if (ret != DM_OK) {
209         LOGE("DeviceManagerImpl::GetTrustedDeviceList error, Send Request failed ret: %d", ret);
210         return ERR_DM_IPC_SEND_REQUEST_FAILED;
211     }
212 
213     ret = rsp->GetErrCode();
214     if (ret != DM_OK) {
215         LOGE("GetTrustedDeviceList error, failed ret: %d", ret);
216         return ret;
217     }
218     deviceList = rsp->GetDeviceVec();
219     LOGI("DeviceManagerImpl::GetTrustedDeviceList completed, pkgName: %s", pkgName.c_str());
220     return DM_OK;
221 }
222 
GetAvailableDeviceList(const std::string & pkgName,std::vector<DmDeviceBasicInfo> & deviceList)223 int32_t DeviceManagerImpl::GetAvailableDeviceList(const std::string &pkgName,
224     std::vector<DmDeviceBasicInfo> &deviceList)
225 {
226     if (pkgName.empty()) {
227         LOGE("Invalid parameter, pkgName is empty.");
228         return ERR_DM_INPUT_PARA_INVALID;
229     }
230     LOGI("GetAvailableDeviceList start, pkgName: %s.", pkgName.c_str());
231 
232     std::shared_ptr<IpcReq> req = std::make_shared<IpcReq>();
233     std::shared_ptr<IpcGetAvailableDeviceRsp> rsp = std::make_shared<IpcGetAvailableDeviceRsp>();
234     req->SetPkgName(pkgName);
235     int32_t ret = ipcClientProxy_->SendRequest(GET_AVAILABLE_DEVICE_LIST, req, rsp);
236     if (ret != DM_OK) {
237         LOGE("DeviceManagerImpl::GetAvailableDeviceList error, Send Request failed ret: %d", ret);
238         return ERR_DM_IPC_SEND_REQUEST_FAILED;
239     }
240 
241     ret = rsp->GetErrCode();
242     if (ret != DM_OK) {
243         LOGI("GetAvailableDeviceList error, failed ret: %d", ret);
244         return ret;
245     }
246 
247     deviceList = rsp->GetDeviceVec();
248     LOGI("DeviceManagerImpl::GetAvailableDeviceList completed, pkgName: %s", pkgName.c_str());
249     return DM_OK;
250 }
251 
GetDeviceInfo(const std::string & pkgName,const std::string networkId,DmDeviceInfo & deviceInfo)252 int32_t DeviceManagerImpl::GetDeviceInfo(const std::string &pkgName, const std::string networkId,
253                                          DmDeviceInfo &deviceInfo)
254 {
255     if (pkgName.empty() || networkId.empty()) {
256         LOGE("Invalid parameter, pkgName: %s, netWorkId: %s", pkgName.c_str(), GetAnonyString(networkId).c_str());
257         return ERR_DM_INPUT_PARA_INVALID;
258     }
259     LOGI("DeviceManagerImpl::GetDeviceInfo start, pkgName: %s networKId : %s", pkgName.c_str(),
260          GetAnonyString(networkId).c_str());
261     std::shared_ptr<IpcGetInfoByNetWorkReq> req = std::make_shared<IpcGetInfoByNetWorkReq>();
262     std::shared_ptr<IpcGetDeviceInfoRsp> rsp = std::make_shared<IpcGetDeviceInfoRsp>();
263     req->SetPkgName(pkgName);
264     req->SetNetWorkId(networkId);
265     int32_t ret = ipcClientProxy_->SendRequest(GET_DEVICE_INFO, req, rsp);
266     if (ret != DM_OK) {
267         LOGE("DeviceManagerImpl::GetDeviceInfo error, Send Request failed ret: %d", ret);
268         return ERR_DM_IPC_SEND_REQUEST_FAILED;
269     }
270 
271     ret = rsp->GetErrCode();
272     if (ret != DM_OK) {
273         LOGE("DeviceManagerImpl::GetDeviceInfo error, failed ret: %d", ret);
274         return ret;
275     }
276 
277     deviceInfo = rsp->GetDeviceInfo();
278     LOGI("DeviceManagerImpl::GetDeviceInfo completed, pkgname = %s networKId = %s deviceName = %s",
279          req->GetPkgName().c_str(), GetAnonyString(req->GetNetWorkId()).c_str(), deviceInfo.deviceName);
280     return DM_OK;
281 }
282 
GetLocalDeviceInfo(const std::string & pkgName,DmDeviceInfo & info)283 int32_t DeviceManagerImpl::GetLocalDeviceInfo(const std::string &pkgName, DmDeviceInfo &info)
284 {
285     LOGI("DeviceManagerImpl::GetLocalDeviceInfo start, pkgName: %s", pkgName.c_str());
286     DmTraceStart(std::string(DM_HITRACE_GET_LOCAL_DEVICE_INFO));
287     std::shared_ptr<IpcReq> req = std::make_shared<IpcReq>();
288     std::shared_ptr<IpcGetLocalDeviceInfoRsp> rsp = std::make_shared<IpcGetLocalDeviceInfoRsp>();
289     req->SetPkgName(pkgName);
290     int32_t ret = ipcClientProxy_->SendRequest(GET_LOCAL_DEVICE_INFO, req, rsp);
291     if (ret != DM_OK) {
292         LOGE("DeviceManagerImpl::GetLocalDeviceInfo error, Send Request failed ret: %d", ret);
293         return ERR_DM_IPC_SEND_REQUEST_FAILED;
294     }
295 
296     ret = rsp->GetErrCode();
297     if (ret != DM_OK) {
298         LOGI("DeviceManagerImpl::GetLocalDeviceInfo error, failed ret: %d", ret);
299         SysEventWrite(std::string(GET_LOCAL_DEVICE_INFO_FAILED), DM_HISYEVENT_BEHAVIOR,
300             std::string(GET_LOCAL_DEVICE_INFO_FAILED_MSG));
301         return ret;
302     }
303 
304     info = rsp->GetLocalDeviceInfo();
305     DmTraceEnd();
306     LOGI("DeviceManagerImpl::GetLocalDeviceInfo completed, pkgname = %s", req->GetPkgName().c_str());
307     SysEventWrite(std::string(GET_LOCAL_DEVICE_INFO_SUCCESS), DM_HISYEVENT_BEHAVIOR,
308         std::string(GET_LOCAL_DEVICE_INFO_SUCCESS_MSG));
309     return DM_OK;
310 }
311 
RegisterDevStateCallback(const std::string & pkgName,const std::string & extra,std::shared_ptr<DeviceStateCallback> callback)312 int32_t DeviceManagerImpl::RegisterDevStateCallback(const std::string &pkgName, const std::string &extra,
313                                                     std::shared_ptr<DeviceStateCallback> callback)
314 {
315     if (pkgName.empty() || callback == nullptr) {
316         LOGE("RegisterDevStateCallback error: Invalid para");
317         return ERR_DM_INPUT_PARA_INVALID;
318     }
319     LOGI("DeviceManagerImpl::RegisterDevStateCallback start, pkgName: %s", pkgName.c_str());
320     DeviceManagerNotify::GetInstance().RegisterDeviceStateCallback(pkgName, callback);
321     RegisterDevStateCallback(pkgName, extra);
322     LOGI("RegisterDevStateCallback completed, pkgName: %s", pkgName.c_str());
323     return DM_OK;
324 }
325 
RegisterDevStatusCallback(const std::string & pkgName,const std::string & extra,std::shared_ptr<DeviceStatusCallback> callback)326 int32_t DeviceManagerImpl::RegisterDevStatusCallback(const std::string &pkgName, const std::string &extra,
327     std::shared_ptr<DeviceStatusCallback> callback)
328 {
329     if (pkgName.empty() || callback == nullptr) {
330         LOGE("RegisterDevStatusCallback error: Invalid para");
331         return ERR_DM_INPUT_PARA_INVALID;
332     }
333     LOGI("DeviceManagerImpl::RegisterDevStatusCallback start, pkgName: %s", pkgName.c_str());
334     DeviceManagerNotify::GetInstance().RegisterDeviceStatusCallback(pkgName, callback);
335     RegisterDevStateCallback(pkgName, extra);
336     LOGI("RegisterDevStatusCallback completed, pkgName: %s", pkgName.c_str());
337     return DM_OK;
338 }
339 
UnRegisterDevStateCallback(const std::string & pkgName)340 int32_t DeviceManagerImpl::UnRegisterDevStateCallback(const std::string &pkgName)
341 {
342     if (pkgName.empty()) {
343         LOGE("UnRegisterDevStateCallback Invalid parameter, pkgName is empty.");
344         return ERR_DM_INPUT_PARA_INVALID;
345     }
346     LOGI("UnRegisterDevStateCallback start, pkgName: %s", pkgName.c_str());
347     DeviceManagerNotify::GetInstance().UnRegisterDeviceStateCallback(pkgName);
348     std::string extra = "";
349     UnRegisterDevStateCallback(pkgName, extra);
350     LOGI("UnRegisterDevStateCallback completed, pkgName: %s", pkgName.c_str());
351     return DM_OK;
352 }
353 
UnRegisterDevStatusCallback(const std::string & pkgName)354 int32_t DeviceManagerImpl::UnRegisterDevStatusCallback(const std::string &pkgName)
355 {
356     if (pkgName.empty()) {
357         LOGE("UnRegisterDevStatusCallback Invalid parameter, pkgName is empty.");
358         return ERR_DM_INPUT_PARA_INVALID;
359     }
360     LOGI("UnRegisterDevStatusCallback start, pkgName: %s", pkgName.c_str());
361     DeviceManagerNotify::GetInstance().UnRegisterDeviceStatusCallback(pkgName);
362     std::string extra = "";
363     UnRegisterDevStateCallback(pkgName, extra);
364     LOGI("UnRegisterDevStatusCallback completed, pkgName: %s", pkgName.c_str());
365     return DM_OK;
366 }
367 
368 
StartDeviceDiscovery(const std::string & pkgName,const DmSubscribeInfo & subscribeInfo,const std::string & extra,std::shared_ptr<DiscoveryCallback> callback)369 int32_t DeviceManagerImpl::StartDeviceDiscovery(const std::string &pkgName, const DmSubscribeInfo &subscribeInfo,
370                                                 const std::string &extra, std::shared_ptr<DiscoveryCallback> callback)
371 {
372     if (pkgName.empty() || callback == nullptr) {
373         LOGE("DeviceManagerImpl::StartDeviceDiscovery error: Invalid para, pkgName: %s", pkgName.c_str());
374         return ERR_DM_INPUT_PARA_INVALID;
375     }
376 
377     LOGI("StartDeviceDiscovery start, pkgName: %s", pkgName.c_str());
378     DmTraceStart(std::string(DM_HITRACE_START_DEVICE));
379     DeviceManagerNotify::GetInstance().RegisterDiscoveryCallback(pkgName, subscribeInfo.subscribeId, callback);
380 
381     std::shared_ptr<IpcStartDiscoveryReq> req = std::make_shared<IpcStartDiscoveryReq>();
382     std::shared_ptr<IpcRsp> rsp = std::make_shared<IpcRsp>();
383     req->SetPkgName(pkgName);
384     req->SetExtra(extra);
385     req->SetSubscribeInfo(subscribeInfo);
386     int32_t ret = ipcClientProxy_->SendRequest(START_DEVICE_DISCOVER, req, rsp);
387     if (ret != DM_OK) {
388         LOGE("StartDeviceDiscovery error: Send Request failed ret: %d", ret);
389         return ERR_DM_IPC_SEND_REQUEST_FAILED;
390     }
391 
392     ret = rsp->GetErrCode();
393     if (ret != DM_OK) {
394         LOGE("StartDeviceDiscovery error: Failed with ret %d", ret);
395         SysEventWrite(std::string(START_DEVICE_DISCOVERY_FAILED), DM_HISYEVENT_BEHAVIOR,
396             std::string(START_DEVICE_DISCOVERY_FAILED_MSG));
397         return ret;
398     }
399 
400     DmTraceEnd();
401     LOGI("StartDeviceDiscovery completed, pkgName: %s", pkgName.c_str());
402     SysEventWrite(std::string(START_DEVICE_DISCOVERY_SUCCESS), DM_HISYEVENT_BEHAVIOR,
403         std::string(START_DEVICE_DISCOVERY_SUCCESS_MSG));
404     return DM_OK;
405 }
406 
StartDeviceDiscovery(const std::string & pkgName,uint64_t tokenId,const std::string & filterOptions,std::shared_ptr<DiscoveryCallback> callback)407 int32_t DeviceManagerImpl::StartDeviceDiscovery(const std::string &pkgName, uint64_t tokenId,
408     const std::string &filterOptions, std::shared_ptr<DiscoveryCallback> callback)
409 {
410     if (pkgName.empty() || callback == nullptr) {
411         LOGE("DeviceManagerImpl::StartDeviceDiscovery error: Invalid para, pkgName: %s", pkgName.c_str());
412         return ERR_DM_INPUT_PARA_INVALID;
413     }
414 
415     LOGI("StartDeviceDiscovery start, pkgName: %s", pkgName.c_str());
416     uint16_t subscribeId = 0;
417     {
418         std::lock_guard<std::mutex> autoLock(subscribIdLock);
419         if (subscribIdMap_.find(tokenId) != subscribIdMap_.end()) {
420             return ERR_DM_DISCOVERY_REPEATED;
421         }
422         subscribeId = GenRandUint(0, DM_MAX_RANDOM);
423         subscribIdMap_[tokenId] = subscribeId;
424     }
425     DmTraceStart(std::string(DM_HITRACE_START_DEVICE));
426     DeviceManagerNotify::GetInstance().RegisterDiscoveryCallback(pkgName, subscribeId, callback);
427     std::shared_ptr<IpcStartDevDiscoveryByIdReq> req = std::make_shared<IpcStartDevDiscoveryByIdReq>();
428     std::shared_ptr<IpcRsp> rsp = std::make_shared<IpcRsp>();
429     req->SetPkgName(pkgName);
430     req->SetFilterOption(filterOptions);
431     req->SetSubscribeId(subscribeId);
432     int32_t ret = ipcClientProxy_->SendRequest(START_DEVICE_DISCOVERY, req, rsp);
433     if (ret != DM_OK) {
434         LOGE("StartDeviceDiscovery error: Send Request failed ret: %d", ret);
435         return ERR_DM_IPC_SEND_REQUEST_FAILED;
436     }
437 
438     ret = rsp->GetErrCode();
439     if (ret != DM_OK) {
440         LOGE("StartDeviceDiscovery error: Failed with ret %d", ret);
441         SysEventWrite(std::string(START_DEVICE_DISCOVERY_FAILED), DM_HISYEVENT_BEHAVIOR,
442             std::string(START_DEVICE_DISCOVERY_FAILED_MSG));
443         return ret;
444     }
445 
446     DmTraceEnd();
447     LOGI("StartDeviceDiscovery completed, pkgName: %s", pkgName.c_str());
448     SysEventWrite(std::string(START_DEVICE_DISCOVERY_SUCCESS), DM_HISYEVENT_BEHAVIOR,
449         std::string(START_DEVICE_DISCOVERY_SUCCESS_MSG));
450     return DM_OK;
451 }
452 
StopDeviceDiscovery(const std::string & pkgName,uint16_t subscribeId)453 int32_t DeviceManagerImpl::StopDeviceDiscovery(const std::string &pkgName, uint16_t subscribeId)
454 {
455     if (pkgName.empty()) {
456         LOGE("DeviceManagerImpl::StopDeviceDiscovery Invalid parameter, pkgName is empty.");
457         return ERR_DM_INPUT_PARA_INVALID;
458     }
459     LOGI("StopDeviceDiscovery start, pkgName: %s", pkgName.c_str());
460     std::shared_ptr<IpcStopDiscoveryReq> req = std::make_shared<IpcStopDiscoveryReq>();
461     std::shared_ptr<IpcRsp> rsp = std::make_shared<IpcRsp>();
462     req->SetPkgName(pkgName);
463     req->SetSubscribeId(subscribeId);
464     int32_t ret = ipcClientProxy_->SendRequest(STOP_DEVICE_DISCOVER, req, rsp);
465     if (ret != DM_OK) {
466         LOGE("StopDeviceDiscovery error: Send Request failed ret: %d", ret);
467         return ERR_DM_IPC_SEND_REQUEST_FAILED;
468     }
469 
470     ret = rsp->GetErrCode();
471     if (ret != DM_OK) {
472         LOGE("StopDeviceDiscovery error: Failed with ret %d", ret);
473         return ret;
474     }
475 
476     DeviceManagerNotify::GetInstance().UnRegisterDiscoveryCallback(pkgName, subscribeId);
477     LOGI("StopDeviceDiscovery completed, pkgName: %s", pkgName.c_str());
478     return DM_OK;
479 }
480 
StopDeviceDiscovery(uint64_t tokenId,const std::string & pkgName)481 int32_t DeviceManagerImpl::StopDeviceDiscovery(uint64_t tokenId, const std::string &pkgName)
482 {
483     if (pkgName.empty()) {
484         LOGE("DeviceManagerImpl::StopDeviceDiscovery Invalid parameter, pkgName is empty.");
485         return ERR_DM_INPUT_PARA_INVALID;
486     }
487     LOGI("StopDeviceDiscovery start, pkgName: %s", pkgName.c_str());
488     uint16_t subscribeId = 0;
489     {
490         std::lock_guard<std::mutex> autoLock(subscribIdLock);
491         if (subscribIdMap_.find(tokenId) == subscribIdMap_.end()) {
492             return ERR_DM_STOP_DISCOVERY;
493         }
494         subscribeId = subscribIdMap_[tokenId];
495         subscribIdMap_.erase(tokenId);
496     }
497     std::shared_ptr<IpcStopDiscoveryReq> req = std::make_shared<IpcStopDiscoveryReq>();
498     std::shared_ptr<IpcRsp> rsp = std::make_shared<IpcRsp>();
499     req->SetPkgName(pkgName);
500     req->SetSubscribeId(subscribeId);
501     int32_t ret = ipcClientProxy_->SendRequest(STOP_DEVICE_DISCOVER, req, rsp);
502     if (ret != DM_OK) {
503         LOGE("StopDeviceDiscovery error: Send Request failed ret: %d", ret);
504         return ERR_DM_IPC_SEND_REQUEST_FAILED;
505     }
506 
507     ret = rsp->GetErrCode();
508     if (ret != DM_OK) {
509         LOGE("StopDeviceDiscovery error: Failed with ret %d", ret);
510         return ret;
511     }
512 
513     DeviceManagerNotify::GetInstance().UnRegisterDiscoveryCallback(pkgName, subscribeId);
514     LOGI("StopDeviceDiscovery completed, pkgName: %s", pkgName.c_str());
515     return DM_OK;
516 }
517 
PublishDeviceDiscovery(const std::string & pkgName,const DmPublishInfo & publishInfo,std::shared_ptr<PublishCallback> callback)518 int32_t DeviceManagerImpl::PublishDeviceDiscovery(const std::string &pkgName, const DmPublishInfo &publishInfo,
519     std::shared_ptr<PublishCallback> callback)
520 {
521     if (pkgName.empty() || callback == nullptr) {
522         LOGE("PublishDeviceDiscovery error: pkgName %s invalid para", pkgName.c_str());
523         return ERR_DM_INPUT_PARA_INVALID;
524     }
525 
526     LOGI("PublishDeviceDiscovery start, pkgName %s", pkgName.c_str());
527     DeviceManagerNotify::GetInstance().RegisterPublishCallback(pkgName, publishInfo.publishId, callback);
528 
529     std::shared_ptr<IpcPublishReq> req = std::make_shared<IpcPublishReq>();
530     std::shared_ptr<IpcRsp> rsp = std::make_shared<IpcRsp>();
531     req->SetPkgName(pkgName);
532     req->SetPublishInfo(publishInfo);
533     int32_t ret = ipcClientProxy_->SendRequest(PUBLISH_DEVICE_DISCOVER, req, rsp);
534     if (ret != DM_OK) {
535         LOGE("PublishDeviceDiscovery error: Send Request failed ret: %d", ret);
536         DeviceManagerNotify::GetInstance().UnRegisterPublishCallback(pkgName, publishInfo.publishId);
537         return ERR_DM_IPC_SEND_REQUEST_FAILED;
538     }
539 
540     ret = rsp->GetErrCode();
541     if (ret != DM_OK) {
542         LOGE("PublishDeviceDiscovery error: Failed with ret %d", ret);
543         return ret;
544     }
545 
546     LOGI("PublishDeviceDiscovery completed, pkgName: %s", pkgName.c_str());
547     return DM_OK;
548 }
549 
UnPublishDeviceDiscovery(const std::string & pkgName,int32_t publishId)550 int32_t DeviceManagerImpl::UnPublishDeviceDiscovery(const std::string &pkgName, int32_t publishId)
551 {
552     if (pkgName.empty()) {
553         LOGE("Invalid parameter, pkgName is empty.");
554         return ERR_DM_INPUT_PARA_INVALID;
555     }
556 
557     LOGI("UnPublishDeviceDiscovery start, pkgName %s", pkgName.c_str());
558     std::shared_ptr<IpcUnPublishReq> req = std::make_shared<IpcUnPublishReq>();
559     std::shared_ptr<IpcRsp> rsp = std::make_shared<IpcRsp>();
560     req->SetPkgName(pkgName);
561     req->SetPublishId(publishId);
562     int32_t ret = ipcClientProxy_->SendRequest(UNPUBLISH_DEVICE_DISCOVER, req, rsp);
563     if (ret != DM_OK) {
564         LOGE("UnPublishDeviceDiscovery error: Send Request failed ret: %d", ret);
565         return ERR_DM_IPC_SEND_REQUEST_FAILED;
566     }
567 
568     ret = rsp->GetErrCode();
569     if (ret != DM_OK) {
570         LOGE("UnPublishDeviceDiscovery error: Failed with ret %d", ret);
571         return ret;
572     }
573 
574     DeviceManagerNotify::GetInstance().UnRegisterPublishCallback(pkgName, publishId);
575     LOGI("UnPublishDeviceDiscovery completed, pkgName: %s", pkgName.c_str());
576     return DM_OK;
577 }
578 
AuthenticateDevice(const std::string & pkgName,int32_t authType,const DmDeviceInfo & deviceInfo,const std::string & extra,std::shared_ptr<AuthenticateCallback> callback)579 int32_t DeviceManagerImpl::AuthenticateDevice(const std::string &pkgName, int32_t authType,
580                                               const DmDeviceInfo &deviceInfo, const std::string &extra,
581                                               std::shared_ptr<AuthenticateCallback> callback)
582 {
583     if (pkgName.empty() || callback == nullptr) {
584         LOGE("Invalid parameter, pkgName is empty or callback is nullptr.");
585         return ERR_DM_INPUT_PARA_INVALID;
586     }
587     LOGI("AuthenticateDevice start, pkgName: %s", pkgName.c_str());
588     DmTraceStart(std::string(DM_HITRACE_AUTH_TO_CONSULT));
589 
590     std::string strDeviceId = deviceInfo.deviceId;
591     DeviceManagerNotify::GetInstance().RegisterAuthenticateCallback(pkgName, strDeviceId, callback);
592     std::shared_ptr<IpcAuthenticateDeviceReq> req = std::make_shared<IpcAuthenticateDeviceReq>();
593     std::shared_ptr<IpcRsp> rsp = std::make_shared<IpcRsp>();
594     req->SetPkgName(pkgName);
595     req->SetExtra(extra);
596     req->SetAuthType(authType);
597     req->SetDeviceInfo(deviceInfo);
598     int32_t ret = ipcClientProxy_->SendRequest(AUTHENTICATE_DEVICE, req, rsp);
599     if (ret != DM_OK) {
600         LOGE("AuthenticateDevice error: Send Request failed ret: %d", ret);
601         SysEventWrite(std::string(DM_SEND_REQUEST_FAILED), DM_HISYEVENT_BEHAVIOR,
602             std::string(DM_SEND_REQUEST_FAILED_MSG));
603         return ERR_DM_IPC_SEND_REQUEST_FAILED;
604     }
605     SysEventWrite(std::string(DM_SEND_REQUEST_SUCCESS), DM_HISYEVENT_BEHAVIOR,
606         std::string(DM_SEND_REQUEST_SUCCESS_MSG));
607 
608     ret = rsp->GetErrCode();
609     if (ret != DM_OK) {
610         LOGE("AuthenticateDevice error: Failed with ret %d", ret);
611         return ret;
612     }
613     DmTraceEnd();
614     LOGI("AuthenticateDevice completed, pkgName: %s", pkgName.c_str());
615     return DM_OK;
616 }
617 
UnAuthenticateDevice(const std::string & pkgName,const DmDeviceInfo & deviceInfo)618 int32_t DeviceManagerImpl::UnAuthenticateDevice(const std::string &pkgName, const DmDeviceInfo &deviceInfo)
619 {
620     if (pkgName.empty() || (deviceInfo.networkId[0] == '\0')) {
621         LOGE("UnAuthenticateDevice error: Invalid para. pkgName %s", pkgName.c_str());
622         return ERR_DM_INPUT_PARA_INVALID;
623     }
624     LOGI("UnAuthenticateDevice start, pkgName: %s, networkId: %s", pkgName.c_str(),
625         GetAnonyString(std::string(deviceInfo.networkId)).c_str());
626     std::shared_ptr<IpcUnAuthenticateDeviceReq> req = std::make_shared<IpcUnAuthenticateDeviceReq>();
627     std::shared_ptr<IpcRsp> rsp = std::make_shared<IpcRsp>();
628     req->SetPkgName(pkgName);
629     req->SetDeviceInfo(deviceInfo);
630     int32_t ret = ipcClientProxy_->SendRequest(UNAUTHENTICATE_DEVICE, req, rsp);
631     if (ret != DM_OK) {
632         LOGE("UnAuthenticateDevice error: Send Request failed ret: %d", ret);
633         return ERR_DM_IPC_SEND_REQUEST_FAILED;
634     }
635     ret = rsp->GetErrCode();
636     if (ret != DM_OK) {
637         LOGE("UnAuthenticateDevice error: Failed with ret %d", ret);
638         SysEventWrite(std::string(UNAUTHENTICATE_DEVICE_FAILED), DM_HISYEVENT_BEHAVIOR,
639             std::string(UNAUTHENTICATE_DEVICE_FAILED_MSG));
640         return ret;
641     }
642     SysEventWrite(std::string(UNAUTHENTICATE_DEVICE_SUCCESS), DM_HISYEVENT_BEHAVIOR,
643         std::string(UNAUTHENTICATE_DEVICE_SUCCESS_MSG));
644 
645     LOGI("UnAuthenticateDevice completed, pkgName: %s", pkgName.c_str());
646     return DM_OK;
647 }
648 
RegisterDeviceManagerFaCallback(const std::string & pkgName,std::shared_ptr<DeviceManagerUiCallback> callback)649 int32_t DeviceManagerImpl::RegisterDeviceManagerFaCallback(const std::string &pkgName,
650                                                            std::shared_ptr<DeviceManagerUiCallback> callback)
651 {
652     if (pkgName.empty() || callback == nullptr) {
653         LOGE("RegisterDeviceManagerFaCallback error: Invalid para");
654         return ERR_DM_INPUT_PARA_INVALID;
655     }
656 
657     int32_t ret = CheckAPIAccessPermission();
658     if (ret != DM_OK) {
659         LOGE("The caller: %s does not have permission to call RegisterDeviceManagerFaCallback.",
660             pkgName.c_str());
661         return ret;
662     }
663 
664     LOGI("dRegisterDeviceManagerFaCallback start, pkgName: %s", pkgName.c_str());
665     DeviceManagerNotify::GetInstance().RegisterDeviceManagerFaCallback(pkgName, callback);
666     RegisterUiStateCallback(pkgName);
667     LOGI("DeviceManagerImpl::RegisterDevStateCallback completed, pkgName: %s", pkgName.c_str());
668     return DM_OK;
669 }
670 
UnRegisterDeviceManagerFaCallback(const std::string & pkgName)671 int32_t DeviceManagerImpl::UnRegisterDeviceManagerFaCallback(const std::string &pkgName)
672 {
673     if (pkgName.empty()) {
674         LOGE("Invalid parameter, pkgName is empty.");
675         return ERR_DM_INPUT_PARA_INVALID;
676     }
677 
678     int32_t ret = CheckAPIAccessPermission();
679     if (ret != DM_OK) {
680         LOGE("The caller: %s does not have permission to call UnRegisterDeviceManagerFaCallback.",
681             pkgName.c_str());
682         return ret;
683     }
684 
685     LOGI("UnRegisterDeviceManagerFaCallback start, pkgName: %s", pkgName.c_str());
686     DeviceManagerNotify::GetInstance().UnRegisterDeviceManagerFaCallback(pkgName);
687     UnRegisterUiStateCallback(pkgName);
688     LOGI("UnRegisterDevStateCallback completed, pkgName: %s", pkgName.c_str());
689     return DM_OK;
690 }
691 
VerifyAuthentication(const std::string & pkgName,const std::string & authPara,std::shared_ptr<VerifyAuthCallback> callback)692 int32_t DeviceManagerImpl::VerifyAuthentication(const std::string &pkgName, const std::string &authPara,
693                                                 std::shared_ptr<VerifyAuthCallback> callback)
694 {
695     if (pkgName.empty() || callback == nullptr) {
696         LOGE("Invalid parameter, pkgName is empty or callback is nullptr.");
697         return ERR_DM_INPUT_PARA_INVALID;
698     }
699     LOGI("VerifyAuthentication start, pkgName: %s", pkgName.c_str());
700     DeviceManagerNotify::GetInstance().RegisterVerifyAuthenticationCallback(pkgName, authPara, callback);
701 
702     std::shared_ptr<IpcVerifyAuthenticateReq> req = std::make_shared<IpcVerifyAuthenticateReq>();
703     std::shared_ptr<IpcRsp> rsp = std::make_shared<IpcRsp>();
704     req->SetPkgName(pkgName);
705     req->SetAuthPara(authPara);
706 
707     int32_t ret = ipcClientProxy_->SendRequest(VERIFY_AUTHENTICATION, req, rsp);
708     if (ret != DM_OK) {
709         LOGE("VerifyAuthentication error: Send Request failed ret: %d", ret);
710         return ERR_DM_IPC_SEND_REQUEST_FAILED;
711     }
712     ret = rsp->GetErrCode();
713     if (ret != DM_OK) {
714         LOGE("VerifyAuthentication error: Failed with ret %d", ret);
715         return ret;
716     }
717 
718     LOGI("VerifyAuthentication completed, pkgName: %s", pkgName.c_str());
719     return DM_OK;
720 }
721 
GetFaParam(const std::string & pkgName,DmAuthParam & dmFaParam)722 int32_t DeviceManagerImpl::GetFaParam(const std::string &pkgName, DmAuthParam &dmFaParam)
723 {
724     if (pkgName.empty()) {
725         LOGE("DeviceManagerImpl::GetFaParam Invalid parameter, pkgName is empty.");
726         return ERR_DM_INPUT_PARA_INVALID;
727     }
728     LOGI("GetFaParam start, pkgName: %s", pkgName.c_str());
729 
730     std::shared_ptr<IpcReq> req = std::make_shared<IpcReq>();
731     std::shared_ptr<IpcGetDmFaParamRsp> rsp = std::make_shared<IpcGetDmFaParamRsp>();
732     req->SetPkgName(pkgName);
733 
734     int32_t ret = ipcClientProxy_->SendRequest(SERVER_GET_DMFA_INFO, req, rsp);
735     if (ret != DM_OK) {
736         LOGI("GetFaParam Send Request failed ret: %d", ret);
737         return ERR_DM_IPC_SEND_REQUEST_FAILED;
738     }
739     dmFaParam = rsp->GetDmAuthParam();
740     LOGI("GetFaParam completed, pkgName: %s", pkgName.c_str());
741     return DM_OK;
742 }
743 
SetUserOperation(const std::string & pkgName,int32_t action,const std::string & params)744 int32_t DeviceManagerImpl::SetUserOperation(const std::string &pkgName, int32_t action, const std::string &params)
745 {
746     if (pkgName.empty() || params.empty()) {
747         LOGE("DeviceManager::SetUserOperation start, pkgName: %s, params: %s", pkgName.c_str(), params.c_str());
748         return ERR_DM_INPUT_PARA_INVALID;
749     }
750     LOGI("SetUserOperation start, pkgName: %s", pkgName.c_str());
751 
752     std::shared_ptr<IpcGetOperationReq> req = std::make_shared<IpcGetOperationReq>();
753     std::shared_ptr<IpcRsp> rsp = std::make_shared<IpcRsp>();
754     req->SetPkgName(pkgName);
755     req->SetOperation(action);
756     req->SetParams(params);
757 
758     int32_t ret = ipcClientProxy_->SendRequest(SERVER_USER_AUTH_OPERATION, req, rsp);
759     if (ret != DM_OK) {
760         LOGI("SetUserOperation Send Request failed ret: %d", ret);
761         return ERR_DM_IPC_SEND_REQUEST_FAILED;
762     }
763 
764     ret = rsp->GetErrCode();
765     if (ret != DM_OK) {
766         LOGE("CheckAuthentication Failed with ret %d", ret);
767         return ret;
768     }
769     LOGI("SetUserOperation completed, pkgName: %s", pkgName.c_str());
770     return DM_OK;
771 }
772 
GetUdidByNetworkId(const std::string & pkgName,const std::string & netWorkId,std::string & udid)773 int32_t DeviceManagerImpl::GetUdidByNetworkId(const std::string &pkgName, const std::string &netWorkId,
774                                               std::string &udid)
775 {
776     if (pkgName.empty() || netWorkId.empty()) {
777         LOGE("DeviceManagerImpl::GetUdidByNetworkId error: Invalid para, pkgName: %s, netWorkId: %s",
778             pkgName.c_str(), GetAnonyString(netWorkId).c_str());
779         return ERR_DM_INPUT_PARA_INVALID;
780     }
781     LOGI("GetUdidByNetworkId start, pkgName: %s", pkgName.c_str());
782 
783     std::shared_ptr<IpcGetInfoByNetWorkReq> req = std::make_shared<IpcGetInfoByNetWorkReq>();
784     std::shared_ptr<IpcGetInfoByNetWorkRsp> rsp = std::make_shared<IpcGetInfoByNetWorkRsp>();
785     req->SetPkgName(pkgName);
786     req->SetNetWorkId(netWorkId);
787 
788     int32_t ret = ipcClientProxy_->SendRequest(GET_UDID_BY_NETWORK, req, rsp);
789     if (ret != DM_OK) {
790         LOGI("GetUdidByNetworkId Send Request failed ret: %d", ret);
791         return ERR_DM_IPC_SEND_REQUEST_FAILED;
792     }
793 
794     ret = rsp->GetErrCode();
795     if (ret != DM_OK) {
796         LOGE("CheckAuthentication Failed with ret %d", ret);
797         return ret;
798     }
799     udid = rsp->GetUdid();
800     return DM_OK;
801 }
802 
GetUuidByNetworkId(const std::string & pkgName,const std::string & netWorkId,std::string & uuid)803 int32_t DeviceManagerImpl::GetUuidByNetworkId(const std::string &pkgName, const std::string &netWorkId,
804                                               std::string &uuid)
805 {
806     if (pkgName.empty() || netWorkId.empty()) {
807         LOGE("DeviceManagerImpl::GetUuidByNetworkId error: Invalid para, pkgName: %s, netWorkId: %s, uuid: %s",
808             pkgName.c_str(), GetAnonyString(netWorkId).c_str(), uuid.c_str());
809         return ERR_DM_INPUT_PARA_INVALID;
810     }
811     LOGI("GetUuidByNetworkId start, pkgName: %s", pkgName.c_str());
812 
813     std::shared_ptr<IpcGetInfoByNetWorkReq> req = std::make_shared<IpcGetInfoByNetWorkReq>();
814     std::shared_ptr<IpcGetInfoByNetWorkRsp> rsp = std::make_shared<IpcGetInfoByNetWorkRsp>();
815     req->SetPkgName(pkgName);
816     req->SetNetWorkId(netWorkId);
817 
818     int32_t ret = ipcClientProxy_->SendRequest(GET_UUID_BY_NETWORK, req, rsp);
819     if (ret != DM_OK) {
820         LOGI("GetUuidByNetworkId Send Request failed ret: %d", ret);
821         return ERR_DM_IPC_SEND_REQUEST_FAILED;
822     }
823 
824     ret = rsp->GetErrCode();
825     if (ret != DM_OK) {
826         LOGE("CheckAuthentication Failed with ret %d", ret);
827         return ret;
828     }
829     uuid = rsp->GetUuid();
830     return DM_OK;
831 }
832 
RegisterDevStateCallback(const std::string & pkgName,const std::string & extra)833 int32_t DeviceManagerImpl::RegisterDevStateCallback(const std::string &pkgName, const std::string &extra)
834 {
835     if (pkgName.empty()) {
836         LOGE("Invalid parameter, pkgName is empty.");
837         return ERR_DM_INPUT_PARA_INVALID;
838     }
839     LOGI("RegisterDevStateCallback start, pkgName: %s", pkgName.c_str());
840 
841     std::shared_ptr<IpcRegisterDevStateCallbackReq> req = std::make_shared<IpcRegisterDevStateCallbackReq>();
842     std::shared_ptr<IpcRsp> rsp = std::make_shared<IpcRsp>();
843     req->SetPkgName(pkgName);
844     req->SetExtra(extra);
845 
846     int32_t ret = ipcClientProxy_->SendRequest(REGISTER_DEV_STATE_CALLBACK, req, rsp);
847     if (ret != DM_OK) {
848         LOGI("RegisterDevStateCallback Send Request failed ret: %d", ret);
849         return ERR_DM_IPC_SEND_REQUEST_FAILED;
850     }
851 
852     ret = rsp->GetErrCode();
853     if (ret != DM_OK) {
854         LOGE("RegisterDevStateCallback Failed with ret %d", ret);
855         return ret;
856     }
857     return DM_OK;
858 }
859 
UnRegisterDevStateCallback(const std::string & pkgName,const std::string & extra)860 int32_t DeviceManagerImpl::UnRegisterDevStateCallback(const std::string &pkgName, const std::string &extra)
861 {
862     if (pkgName.empty()) {
863         LOGE("Invalid parameter, pkgName is empty.");
864         return ERR_DM_INPUT_PARA_INVALID;
865     }
866 
867     std::shared_ptr<IpcRegisterDevStateCallbackReq> req = std::make_shared<IpcRegisterDevStateCallbackReq>();
868     std::shared_ptr<IpcRsp> rsp = std::make_shared<IpcRsp>();
869     req->SetPkgName(pkgName);
870     req->SetExtra(extra);
871 
872     int32_t ret = ipcClientProxy_->SendRequest(UNREGISTER_DEV_STATE_CALLBACK, req, rsp);
873     if (ret != DM_OK) {
874         LOGI("UnRegisterDevStateCallback Send Request failed ret: %d", ret);
875         return ERR_DM_IPC_SEND_REQUEST_FAILED;
876     }
877 
878     ret = rsp->GetErrCode();
879     if (ret != DM_OK) {
880         LOGE("UnRegisterDevStateCallback Failed with ret %d", ret);
881         return ret;
882     }
883     return DM_OK;
884 }
885 
RegisterUiStateCallback(const std::string & pkgName)886 int32_t DeviceManagerImpl::RegisterUiStateCallback(const std::string &pkgName)
887 {
888     if (pkgName.empty()) {
889         LOGE("Invalid parameter, pkgName is empty.");
890         return ERR_DM_INPUT_PARA_INVALID;
891     }
892     LOGI("RegisterUiStateCallback start, pkgName: %s", pkgName.c_str());
893 
894     std::shared_ptr<IpcReq> req = std::make_shared<IpcReq>();
895     std::shared_ptr<IpcRsp> rsp = std::make_shared<IpcRsp>();
896     req->SetPkgName(pkgName);
897 
898     int32_t ret = ipcClientProxy_->SendRequest(REGISTER_UI_STATE_CALLBACK, req, rsp);
899     if (ret != DM_OK) {
900         LOGI("RegisterUiStateCallback Send Request failed ret: %d", ret);
901         return ERR_DM_IPC_SEND_REQUEST_FAILED;
902     }
903 
904     ret = rsp->GetErrCode();
905     if (ret != DM_OK) {
906         LOGE("RegisterUiStateCallback Failed with ret %d", ret);
907         return ret;
908     }
909     return DM_OK;
910 }
911 
UnRegisterUiStateCallback(const std::string & pkgName)912 int32_t DeviceManagerImpl::UnRegisterUiStateCallback(const std::string &pkgName)
913 {
914     if (pkgName.empty()) {
915         LOGE("Invalid parameter, pkgName is empty.");
916         return ERR_DM_INPUT_PARA_INVALID;
917     }
918 
919     std::shared_ptr<IpcReq> req = std::make_shared<IpcReq>();
920     std::shared_ptr<IpcRsp> rsp = std::make_shared<IpcRsp>();
921     req->SetPkgName(pkgName);
922 
923     int32_t ret = ipcClientProxy_->SendRequest(UNREGISTER_UI_STATE_CALLBACK, req, rsp);
924     if (ret != DM_OK) {
925         LOGI("UnRegisterUiStateCallback Send Request failed ret: %d", ret);
926         return ERR_DM_IPC_SEND_REQUEST_FAILED;
927     }
928 
929     ret = rsp->GetErrCode();
930     if (ret != DM_OK) {
931         LOGE("UnRegisterUiStateCallback Failed with ret %d", ret);
932         return ret;
933     }
934     return DM_OK;
935 }
936 
RequestCredential(const std::string & pkgName,const std::string & reqJsonStr,std::string & returnJsonStr)937 int32_t DeviceManagerImpl::RequestCredential(const std::string &pkgName, const std::string &reqJsonStr,
938     std::string &returnJsonStr)
939 {
940     if (pkgName.empty() || reqJsonStr.empty()) {
941         LOGE("DeviceManagerImpl::RequestCredential error: Invalid para, pkgName is %s, reqJsonStr is %s",
942             pkgName.c_str(), reqJsonStr.c_str());
943         return ERR_DM_INPUT_PARA_INVALID;
944     }
945 
946     LOGI("start to RequestCredential.");
947     std::shared_ptr<IpcSetCredentialReq> req = std::make_shared<IpcSetCredentialReq>();
948     std::shared_ptr<IpcSetCredentialRsp> rsp = std::make_shared<IpcSetCredentialRsp>();
949     req->SetPkgName(pkgName);
950     req->SetCredentialParam(reqJsonStr);
951 
952     int32_t ret = ipcClientProxy_->SendRequest(REQUEST_CREDENTIAL, req, rsp);
953     if (ret != DM_OK) {
954         LOGI("RequestCredential Send Request failed ret: %d", ret);
955         return ERR_DM_IPC_SEND_REQUEST_FAILED;
956     }
957 
958     ret = rsp->GetErrCode();
959     if (ret != DM_OK) {
960         LOGE("failed to get return errcode while request credential.");
961         return ret;
962     }
963     returnJsonStr = rsp->GetCredentialResult();
964     LOGI("request device credential completed.");
965     return DM_OK;
966 }
967 
ImportCredential(const std::string & pkgName,const std::string & credentialInfo)968 int32_t DeviceManagerImpl::ImportCredential(const std::string &pkgName, const std::string &credentialInfo)
969 {
970     if (pkgName.empty() || credentialInfo.empty()) {
971         LOGE("DeviceManagerImpl::ImportCredential failed, pkgName is %s, credentialInfo is %s",
972             pkgName.c_str(), credentialInfo.c_str());
973         return ERR_DM_INPUT_PARA_INVALID;
974     }
975     LOGI("start to ImportCredential.");
976     std::shared_ptr<IpcSetCredentialReq> req = std::make_shared<IpcSetCredentialReq>();
977     std::shared_ptr<IpcRsp> rsp = std::make_shared<IpcRsp>();
978     req->SetPkgName(pkgName);
979     req->SetCredentialParam(credentialInfo);
980 
981     int32_t ret = ipcClientProxy_->SendRequest(IMPORT_CREDENTIAL, req, rsp);
982     if (ret != DM_OK) {
983         LOGI("ImportCredential Send Request failed ret: %d", ret);
984         return ERR_DM_IPC_SEND_REQUEST_FAILED;
985     }
986 
987     ret = rsp->GetErrCode();
988     if (ret != DM_OK) {
989         LOGE("failed to get return errcode while import credential.");
990         return ret;
991     }
992     LOGI("import credential to device completed.");
993     return DM_OK;
994 }
995 
DeleteCredential(const std::string & pkgName,const std::string & deleteInfo)996 int32_t DeviceManagerImpl::DeleteCredential(const std::string &pkgName, const std::string &deleteInfo)
997 {
998     if (pkgName.empty() || deleteInfo.empty()) {
999         LOGE("DeviceManagerImpl::DeleteCredential failed, pkgName is %s, deleteInfo is %s",
1000             pkgName.c_str(), deleteInfo.c_str());
1001         return ERR_DM_INPUT_PARA_INVALID;
1002     }
1003     LOGI("start to DeleteCredential.");
1004     std::shared_ptr<IpcSetCredentialReq> req = std::make_shared<IpcSetCredentialReq>();
1005     std::shared_ptr<IpcRsp> rsp = std::make_shared<IpcRsp>();
1006     req->SetPkgName(pkgName);
1007     req->SetCredentialParam(deleteInfo);
1008 
1009     int32_t ret = ipcClientProxy_->SendRequest(DELETE_CREDENTIAL, req, rsp);
1010     if (ret != DM_OK) {
1011         LOGI("DeleteCredential Send Request failed ret: %d", ret);
1012         return ERR_DM_IPC_SEND_REQUEST_FAILED;
1013     }
1014 
1015     ret = rsp->GetErrCode();
1016     if (ret != DM_OK) {
1017         LOGE("failed to get return errcode while import credential.");
1018         return ret;
1019     }
1020     LOGI("delete credential from device completed.");
1021     return DM_OK;
1022 }
1023 
RegisterCredentialCallback(const std::string & pkgName,std::shared_ptr<CredentialCallback> callback)1024 int32_t DeviceManagerImpl::RegisterCredentialCallback(const std::string &pkgName,
1025     std::shared_ptr<CredentialCallback> callback)
1026 {
1027     if (pkgName.empty() || callback == nullptr) {
1028         LOGE("RegisterCredentialCallback error: Invalid para");
1029         return ERR_DM_INPUT_PARA_INVALID;
1030     }
1031 
1032     LOGI("RegisterCredentialCallback start, pkgName: %s", pkgName.c_str());
1033     DeviceManagerNotify::GetInstance().RegisterCredentialCallback(pkgName, callback);
1034     std::shared_ptr<IpcReq> req = std::make_shared<IpcReq>();
1035     std::shared_ptr<IpcRsp> rsp = std::make_shared<IpcRsp>();
1036     req->SetPkgName(pkgName);
1037 
1038     int32_t ret = ipcClientProxy_->SendRequest(REGISTER_CREDENTIAL_CALLBACK, req, rsp);
1039     if (ret != DM_OK) {
1040         LOGI("RegisterCredentialCallback Send Request failed ret: %d", ret);
1041         return ERR_DM_IPC_SEND_REQUEST_FAILED;
1042     }
1043 
1044     ret = rsp->GetErrCode();
1045     if (ret != DM_OK) {
1046         LOGE("RegisterCredentialCallback error: Failed with ret %d", ret);
1047         return ret;
1048     }
1049     LOGI("RegisterCredentialCallback completed, pkgName: %s", pkgName.c_str());
1050     return DM_OK;
1051 }
1052 
UnRegisterCredentialCallback(const std::string & pkgName)1053 int32_t DeviceManagerImpl::UnRegisterCredentialCallback(const std::string &pkgName)
1054 {
1055     if (pkgName.empty()) {
1056         LOGE("Invalid parameter, pkgName is empty.");
1057         return ERR_DM_INPUT_PARA_INVALID;
1058     }
1059     LOGI("UnRegisterCredentialCallback start, pkgName: %s", pkgName.c_str());
1060 
1061     DeviceManagerNotify::GetInstance().UnRegisterCredentialCallback(pkgName);
1062     std::shared_ptr<IpcReq> req = std::make_shared<IpcReq>();
1063     std::shared_ptr<IpcRsp> rsp = std::make_shared<IpcRsp>();
1064     req->SetPkgName(pkgName);
1065 
1066     int32_t ret = ipcClientProxy_->SendRequest(UNREGISTER_CREDENTIAL_CALLBACK, req, rsp);
1067     if (ret != DM_OK) {
1068         LOGI("UnRegisterCredentialCallback Send Request failed ret: %d", ret);
1069         return ERR_DM_IPC_SEND_REQUEST_FAILED;
1070     }
1071 
1072     ret = rsp->GetErrCode();
1073     if (ret != DM_OK) {
1074         LOGE("UnRegisterCredentialCallback Failed with ret %d", ret);
1075         return ret;
1076     }
1077     LOGI("UnRegisterCredentialCallback completed, pkgName: %s", pkgName.c_str());
1078     return DM_OK;
1079 }
1080 
NotifyEvent(const std::string & pkgName,const int32_t eventId,const std::string & event)1081 int32_t DeviceManagerImpl::NotifyEvent(const std::string &pkgName, const int32_t eventId, const std::string &event)
1082 {
1083     if (pkgName.empty()) {
1084         LOGE("NotifyEvent error: pkgName empty");
1085         return ERR_DM_INPUT_PARA_INVALID;
1086     }
1087     if ((eventId <= DM_NOTIFY_EVENT_START) || (eventId >= DM_NOTIFY_EVENT_BUTT)) {
1088         LOGE("NotifyEvent eventId invalid");
1089         return ERR_DM_INPUT_PARA_INVALID;
1090     }
1091     LOGI("NotifyEvent start, pkgName: %s", pkgName.c_str());
1092     std::shared_ptr<IpcNotifyEventReq> req = std::make_shared<IpcNotifyEventReq>();
1093     std::shared_ptr<IpcRsp> rsp = std::make_shared<IpcRsp>();
1094     req->SetPkgName(pkgName);
1095     req->SetEventId(eventId);
1096     req->SetEvent(event);
1097 
1098     int32_t ret = ipcClientProxy_->SendRequest(NOTIFY_EVENT, req, rsp);
1099     if (ret != DM_OK) {
1100         LOGI("NotifyEvent Send Request failed ret: %d", ret);
1101         return ERR_DM_IPC_SEND_REQUEST_FAILED;
1102     }
1103 
1104     ret = rsp->GetErrCode();
1105     if (ret != DM_OK) {
1106         LOGE("NotifyEvent failed with ret %d", ret);
1107         return ret;
1108     }
1109     LOGI("NotifyEvent completed, pkgName: %s", pkgName.c_str());
1110     return DM_OK;
1111 }
1112 
OnDmServiceDied()1113 int32_t DeviceManagerImpl::OnDmServiceDied()
1114 {
1115     LOGI("OnDmServiceDied begin");
1116     int32_t ret = ipcClientProxy_->OnDmServiceDied();
1117     if (ret != DM_OK) {
1118         LOGE("OnDmServiceDied failed, ret: %d", ret);
1119         return ERR_DM_FAILED;
1120     }
1121     return DM_OK;
1122 }
1123 
GetEncryptedUuidByNetworkId(const std::string & pkgName,const std::string & networkId,std::string & uuid)1124 int32_t DeviceManagerImpl::GetEncryptedUuidByNetworkId(const std::string &pkgName, const std::string &networkId,
1125     std::string &uuid)
1126 {
1127     if (pkgName.empty() || networkId.empty()) {
1128         LOGE("DeviceManagerImpl::GetEncryptedUuidByNetworkId error: Invalid para, pkgName: %s, netWorkId: %s",
1129             pkgName.c_str(), GetAnonyString(networkId).c_str());
1130         return ERR_DM_INPUT_PARA_INVALID;
1131     }
1132     LOGI("GetEncryptedUuidByNetworkId start, pkgName: %s", pkgName.c_str());
1133 
1134     std::shared_ptr<IpcGetInfoByNetWorkReq> req = std::make_shared<IpcGetInfoByNetWorkReq>();
1135     std::shared_ptr<IpcGetInfoByNetWorkRsp> rsp = std::make_shared<IpcGetInfoByNetWorkRsp>();
1136     req->SetPkgName(pkgName);
1137     req->SetNetWorkId(networkId);
1138 
1139     int32_t ret = ipcClientProxy_->SendRequest(GET_ENCRYPTED_UUID_BY_NETWOEKID, req, rsp);
1140     if (ret != DM_OK) {
1141         LOGI("GetEncryptedUuidByNetworkId Send Request failed ret: %d", ret);
1142         return ERR_DM_IPC_SEND_REQUEST_FAILED;
1143     }
1144 
1145     ret = rsp->GetErrCode();
1146     if (ret != DM_OK) {
1147         LOGE("CheckAuthentication Failed with ret %d", ret);
1148         return ret;
1149     }
1150     uuid = rsp->GetUuid();
1151     LOGI("GetEncryptedUuidByNetworkId complete, uuid: %s", GetAnonyString(uuid).c_str());
1152     return DM_OK;
1153 }
1154 
GenerateEncryptedUuid(const std::string & pkgName,const std::string & uuid,const std::string & appId,std::string & encryptedUuid)1155 int32_t DeviceManagerImpl::GenerateEncryptedUuid(const std::string &pkgName, const std::string &uuid,
1156     const std::string &appId, std::string &encryptedUuid)
1157 {
1158     if (pkgName.empty() || uuid.empty()) {
1159         LOGE("DeviceManagerImpl::GenerateEncryptedUuid error: Invalid para, pkgName: %s", pkgName.c_str());
1160         return ERR_DM_INPUT_PARA_INVALID;
1161     }
1162     LOGI("GenerateEncryptedUuid start, pkgName: %s", pkgName.c_str());
1163 
1164     std::shared_ptr<IpcGenerateEncryptedUuidReq> req = std::make_shared<IpcGenerateEncryptedUuidReq>();
1165     std::shared_ptr<IpcGetInfoByNetWorkRsp> rsp = std::make_shared<IpcGetInfoByNetWorkRsp>();
1166     req->SetPkgName(pkgName);
1167     req->SetUuid(uuid);
1168     req->SetAppId(appId);
1169 
1170     int32_t ret = ipcClientProxy_->SendRequest(GENERATE_ENCRYPTED_UUID, req, rsp);
1171     if (ret != DM_OK) {
1172         LOGI("GenerateEncryptedUuid Send Request failed ret: %d", ret);
1173         return ERR_DM_IPC_SEND_REQUEST_FAILED;
1174     }
1175 
1176     ret = rsp->GetErrCode();
1177     if (ret != DM_OK) {
1178         LOGE("CheckAuthentication Failed with ret %d", ret);
1179         return ret;
1180     }
1181     encryptedUuid = rsp->GetUuid();
1182     LOGI("GenerateEncryptedUuid complete, encryptedUuid: %s", GetAnonyString(encryptedUuid).c_str());
1183     return DM_OK;
1184 }
1185 
CheckAPIAccessPermission()1186 int32_t DeviceManagerImpl::CheckAPIAccessPermission()
1187 {
1188     std::shared_ptr<IpcReq> req = std::make_shared<IpcReq>();
1189     std::shared_ptr<IpcRsp> rsp = std::make_shared<IpcRsp>();
1190 
1191     int32_t ret = ipcClientProxy_->SendRequest(CHECK_API_ACCESS_PERMISSION, req, rsp);
1192     if (ret != DM_OK) {
1193         LOGE("Send Request failed ret: %d", ret);
1194         return ERR_DM_IPC_SEND_REQUEST_FAILED;
1195     }
1196 
1197     ret = rsp->GetErrCode();
1198     if (ret != DM_OK) {
1199         LOGE("Check permission failed with ret: %d", ret);
1200         return ret;
1201     }
1202     LOGI("The caller declare the DM permission!");
1203     return DM_OK;
1204 }
1205 
CheckNewAPIAccessPermission()1206 int32_t DeviceManagerImpl::CheckNewAPIAccessPermission()
1207 {
1208     LOGI("CheckNewAPIAccessPermission in");
1209     std::shared_ptr<IpcReq> req = std::make_shared<IpcReq>();
1210     std::shared_ptr<IpcRsp> rsp = std::make_shared<IpcRsp>();
1211 
1212     int32_t ret = ipcClientProxy_->SendRequest(CHECK_API_ACCESS_NEWPERMISSION, req, rsp);
1213     if (ret != DM_OK) {
1214         LOGE("Send Request failed ret: %d", ret);
1215         return ERR_DM_IPC_SEND_REQUEST_FAILED;
1216     }
1217 
1218     ret = rsp->GetErrCode();
1219     if (ret != DM_OK) {
1220         LOGE("Check permission failed with ret: %d", ret);
1221         return ret;
1222     }
1223     LOGI("The caller declare the DM permission!");
1224     return DM_OK;
1225 }
1226 
GetLocalDeviceNetWorkId(const std::string & pkgName,std::string & networkId)1227 int32_t DeviceManagerImpl::GetLocalDeviceNetWorkId(const std::string &pkgName, std::string &networkId)
1228 {
1229     LOGI("DeviceManagerImpl::GetLocalDeviceNetWorkId start, pkgName: %s", pkgName.c_str());
1230     std::shared_ptr<IpcReq> req = std::make_shared<IpcReq>();
1231     std::shared_ptr<IpcGetLocalDeviceNetworkIdRsp> rsp = std::make_shared<IpcGetLocalDeviceNetworkIdRsp>();
1232     req->SetPkgName(pkgName);
1233     int32_t ret = ipcClientProxy_->SendRequest(GET_LOCAL_DEVICE_NETWORKID, req, rsp);
1234     if (ret != DM_OK) {
1235         LOGE("DeviceManagerImpl::GetLocalDeviceNetWorkId error, Send Request failed ret: %d", ret);
1236         return ERR_DM_IPC_SEND_REQUEST_FAILED;
1237     }
1238 
1239     ret = rsp->GetErrCode();
1240     if (ret != DM_OK) {
1241         LOGI("DeviceManagerImpl::GetLocalDeviceNetWorkId error, failed ret: %d", ret);
1242         return ERR_DM_IPC_RESPOND_FAILED;
1243     }
1244 
1245     networkId = rsp->GetLocalDeviceNetworkId();
1246     LOGI("DeviceManagerImpl::GetLocalDeviceNetWorkId end, pkgName : %s, networkId : %s", pkgName.c_str(),
1247         GetAnonyString(networkId).c_str());
1248     return DM_OK;
1249 }
1250 
GetLocalDeviceId(const std::string & pkgName,std::string & deviceId)1251 int32_t DeviceManagerImpl::GetLocalDeviceId(const std::string &pkgName, std::string &deviceId)
1252 {
1253     LOGI("DeviceManagerImpl::GetLocalDeviceId start, pkgName: %s", pkgName.c_str());
1254     std::shared_ptr<IpcReq> req = std::make_shared<IpcReq>();
1255     std::shared_ptr<IpcGetLocalDeviceIdRsp> rsp = std::make_shared<IpcGetLocalDeviceIdRsp>();
1256     req->SetPkgName(pkgName);
1257     int32_t ret = ipcClientProxy_->SendRequest(GET_LOCAL_DEVICEID, req, rsp);
1258     if (ret != DM_OK) {
1259         LOGE("DeviceManagerImpl::GetLocalDeviceId error, Send Request failed ret: %d", ret);
1260         return ERR_DM_IPC_SEND_REQUEST_FAILED;
1261     }
1262 
1263     ret = rsp->GetErrCode();
1264     if (ret != DM_OK) {
1265         LOGI("DeviceManagerImpl::GetLocalDeviceId error, failed ret: %d", ret);
1266         return ERR_DM_IPC_RESPOND_FAILED;
1267     }
1268 
1269     deviceId = rsp->GetLocalDeviceId();
1270     LOGI("DeviceManagerImpl::GetLocalDeviceId end, pkgName : %s, deviceId : %s", pkgName.c_str(),
1271         GetAnonyString(deviceId).c_str());
1272     return DM_OK;
1273 }
1274 
GetLocalDeviceName(const std::string & pkgName,std::string & deviceName)1275 int32_t DeviceManagerImpl::GetLocalDeviceName(const std::string &pkgName, std::string &deviceName)
1276 {
1277     LOGI("DeviceManagerImpl::GetLocalDeviceName start, pkgName: %s", pkgName.c_str());
1278     std::shared_ptr<IpcReq> req = std::make_shared<IpcReq>();
1279     std::shared_ptr<IpcGetLocalDeviceNameRsp> rsp = std::make_shared<IpcGetLocalDeviceNameRsp>();
1280     req->SetPkgName(pkgName);
1281     int32_t ret = ipcClientProxy_->SendRequest(GET_LOCAL_DEVICE_NAME, req, rsp);
1282     if (ret != DM_OK) {
1283         LOGE("DeviceManagerImpl::GetLocalDeviceName error, Send Request failed ret: %d", ret);
1284         return ERR_DM_IPC_SEND_REQUEST_FAILED;
1285     }
1286 
1287     ret = rsp->GetErrCode();
1288     if (ret != DM_OK) {
1289         LOGI("DeviceManagerImpl::GetLocalDeviceName error, failed ret: %d", ret);
1290         return ERR_DM_IPC_RESPOND_FAILED;
1291     }
1292 
1293     deviceName = rsp->GetLocalDeviceName();
1294     LOGI("DeviceManagerImpl::GetLocalDeviceName end, pkgName : %s, deviceName : %s", pkgName.c_str(),
1295         GetAnonyString(deviceName).c_str());
1296     return DM_OK;
1297 }
1298 
GetLocalDeviceType(const std::string & pkgName,int32_t & deviceType)1299 int32_t DeviceManagerImpl::GetLocalDeviceType(const std::string &pkgName,  int32_t &deviceType)
1300 {
1301     LOGI("DeviceManagerImpl::GetLocalDeviceType start, pkgName : %s", pkgName.c_str());
1302     std::shared_ptr<IpcReq> req = std::make_shared<IpcReq>();
1303     std::shared_ptr<IpcGetLocalDeviceTypeRsp> rsp = std::make_shared<IpcGetLocalDeviceTypeRsp>();
1304     req->SetPkgName(pkgName);
1305     int32_t ret = ipcClientProxy_->SendRequest(GET_LOCAL_DEVICE_TYPE, req, rsp);
1306     if (ret != DM_OK) {
1307         LOGE("DeviceManagerImpl::GetLocalDeviceType error, Send Request failed ret: %d", ret);
1308         return ERR_DM_IPC_SEND_REQUEST_FAILED;
1309     }
1310 
1311     ret = rsp->GetErrCode();
1312     if (ret != DM_OK) {
1313         LOGI("DeviceManagerImpl::GetLocalDeviceType error, failed ret: %d", ret);
1314         return ERR_DM_IPC_RESPOND_FAILED;
1315     }
1316 
1317     deviceType = rsp->GetLocalDeviceType();
1318     LOGI("DeviceManagerImpl::GetLocalDeviceType end, pkgName : %s, deviceType : %d", pkgName.c_str(), deviceType);
1319     return DM_OK;
1320 }
1321 
GetDeviceName(const std::string & pkgName,const std::string & networkId,std::string & deviceName)1322 int32_t DeviceManagerImpl::GetDeviceName(const std::string &pkgName, const std::string &networkId,
1323     std::string &deviceName)
1324 {
1325     if (pkgName.empty() || networkId.empty()) {
1326         LOGE("Invalid parameter, pkgName: %s, netWorkId: %s", pkgName.c_str(), GetAnonyString(networkId).c_str());
1327         return ERR_DM_INPUT_PARA_INVALID;
1328     }
1329     LOGI("DeviceManagerImpl::GetDeviceName start, pkgName: %s networKId : %s", pkgName.c_str(),
1330          GetAnonyString(networkId).c_str());
1331     std::shared_ptr<IpcGetInfoByNetWorkReq> req = std::make_shared<IpcGetInfoByNetWorkReq>();
1332     std::shared_ptr<IpcGetDeviceInfoRsp> rsp = std::make_shared<IpcGetDeviceInfoRsp>();
1333     req->SetPkgName(pkgName);
1334     req->SetNetWorkId(networkId);
1335     int32_t ret = ipcClientProxy_->SendRequest(GET_DEVICE_INFO, req, rsp);
1336     if (ret != DM_OK) {
1337         LOGE("DeviceManagerImpl::GetDeviceName error, Send Request failed ret: %d", ret);
1338         return ERR_DM_IPC_SEND_REQUEST_FAILED;
1339     }
1340 
1341     ret = rsp->GetErrCode();
1342     if (ret != DM_OK) {
1343         LOGE("DeviceManagerImpl::GetDeviceName error, failed ret: %d", ret);
1344         return ret;
1345     }
1346 
1347     DmDeviceInfo info = rsp->GetDeviceInfo();
1348     deviceName = info.deviceName;
1349     LOGI("DeviceManagerImpl::GetDeviceName end, pkgName : %s, networkId : %s, deviceName = %s", pkgName.c_str(),
1350         GetAnonyString(networkId).c_str(), GetAnonyString(deviceName).c_str());
1351     return DM_OK;
1352 }
1353 
GetDeviceType(const std::string & pkgName,const std::string & networkId,int32_t & deviceType)1354 int32_t DeviceManagerImpl::GetDeviceType(const std::string &pkgName, const std::string &networkId, int32_t &deviceType)
1355 {
1356     if (pkgName.empty() || networkId.empty()) {
1357         LOGE("Invalid parameter, pkgName: %s, netWorkId: %s", pkgName.c_str(), GetAnonyString(networkId).c_str());
1358         return ERR_DM_INPUT_PARA_INVALID;
1359     }
1360     LOGI("DeviceManagerImpl::GetDeviceType start, pkgName: %s networKId : %s", pkgName.c_str(),
1361          GetAnonyString(networkId).c_str());
1362     std::shared_ptr<IpcGetInfoByNetWorkReq> req = std::make_shared<IpcGetInfoByNetWorkReq>();
1363     std::shared_ptr<IpcGetDeviceInfoRsp> rsp = std::make_shared<IpcGetDeviceInfoRsp>();
1364     req->SetPkgName(pkgName);
1365     req->SetNetWorkId(networkId);
1366     int32_t ret = ipcClientProxy_->SendRequest(GET_DEVICE_INFO, req, rsp);
1367     if (ret != DM_OK) {
1368         LOGE("DeviceManagerImpl::GetDeviceType error, Send Request failed ret: %d", ret);
1369         return ERR_DM_IPC_SEND_REQUEST_FAILED;
1370     }
1371 
1372     ret = rsp->GetErrCode();
1373     if (ret != DM_OK) {
1374         LOGE("DeviceManagerImpl::GetDeviceType error, failed ret: %d", ret);
1375         return ret;
1376     }
1377 
1378     DmDeviceInfo info = rsp->GetDeviceInfo();
1379     deviceType = info.deviceTypeId;
1380     LOGI("DeviceManagerImpl::GetDeviceType end, pkgName : %s, networkId : %s, deviceType = %d", pkgName.c_str(),
1381         GetAnonyString(networkId).c_str(), deviceType);
1382     return DM_OK;
1383 }
1384 
BindDevice(const std::string & pkgName,int32_t bindType,const std::string & deviceId,const std::string & bindParam,std::shared_ptr<AuthenticateCallback> callback)1385 int32_t DeviceManagerImpl::BindDevice(const std::string &pkgName, int32_t bindType, const std::string &deviceId,
1386     const std::string &bindParam, std::shared_ptr<AuthenticateCallback> callback)
1387 {
1388     if (pkgName.empty() || deviceId.empty()) {
1389         LOGE("BindDevice error: Invalid para. pkgName : %s", pkgName.c_str());
1390         return ERR_DM_INPUT_PARA_INVALID;
1391     }
1392     LOGI("BindDevice start, pkgName: %s", pkgName.c_str());
1393     DeviceManagerNotify::GetInstance().RegisterAuthenticateCallback(pkgName, deviceId, callback);
1394     std::shared_ptr<IpcBindDeviceReq> req = std::make_shared<IpcBindDeviceReq>();
1395     std::shared_ptr<IpcRsp> rsp = std::make_shared<IpcRsp>();
1396     req->SetPkgName(pkgName);
1397     req->SetBindParam(bindParam);
1398     req->SetBindType(bindType);
1399     req->SetDeviceId(deviceId);
1400     int32_t ret = ipcClientProxy_->SendRequest(BIND_DEVICE, req, rsp);
1401     if (ret != DM_OK) {
1402         LOGE("BindDevice error: Send Request failed ret: %d", ret);
1403         return ERR_DM_IPC_SEND_REQUEST_FAILED;
1404     }
1405 
1406     ret = rsp->GetErrCode();
1407     if (ret != DM_OK) {
1408         LOGE("BindDevice error: Failed with ret %d", ret);
1409         return ret;
1410     }
1411     DmTraceEnd();
1412     LOGI("BindDevice end, pkgName: %s", pkgName.c_str());
1413     return DM_OK;
1414 }
1415 
UnBindDevice(const std::string & pkgName,const std::string & deviceId)1416 int32_t DeviceManagerImpl::UnBindDevice(const std::string &pkgName, const std::string &deviceId)
1417 {
1418     if (pkgName.empty() || deviceId.empty()) {
1419         LOGE("UnBindDevice error: Invalid para. pkgName %s", pkgName.c_str());
1420         return ERR_DM_INPUT_PARA_INVALID;
1421     }
1422     LOGI("UnBindDevice start, pkgName: %s, deviceId: %s", pkgName.c_str(),
1423         GetAnonyString(std::string(deviceId)).c_str());
1424     std::shared_ptr<IpcUnBindDeviceReq> req = std::make_shared<IpcUnBindDeviceReq>();
1425     std::shared_ptr<IpcRsp> rsp = std::make_shared<IpcRsp>();
1426     req->SetPkgName(pkgName);
1427     req->SetDeviceId(deviceId);
1428     int32_t ret = ipcClientProxy_->SendRequest(UNBIND_DEVICE, req, rsp);
1429     if (ret != DM_OK) {
1430         LOGE("UnBindDevice error: Send Request failed ret: %d", ret);
1431         return ERR_DM_IPC_SEND_REQUEST_FAILED;
1432     }
1433     ret = rsp->GetErrCode();
1434     if (ret != DM_OK) {
1435         LOGE("UnBindDevice error: Failed with ret %d", ret);
1436         return ret;
1437     }
1438 
1439     LOGI("UnBindDevice end, pkgName: %s", pkgName.c_str());
1440     return DM_OK;
1441 }
1442 
GetNetworkTypeByNetworkId(const std::string & pkgName,const std::string & netWorkId,int32_t & netWorkType)1443 int32_t DeviceManagerImpl::GetNetworkTypeByNetworkId(const std::string &pkgName, const std::string &netWorkId,
1444                                                      int32_t &netWorkType)
1445 {
1446     if (pkgName.empty() || netWorkId.empty()) {
1447         LOGE("GetNetworkTypeByNetworkId error: Invalid para, pkgName: %s, netWorkId: %s, netWorkType: %d",
1448             pkgName.c_str(), GetAnonyString(netWorkId).c_str(), netWorkType);
1449         return ERR_DM_INPUT_PARA_INVALID;
1450     }
1451     LOGI("GetNetworkTypeByNetworkId start, pkgName: %s", pkgName.c_str());
1452 
1453     std::shared_ptr<IpcGetInfoByNetWorkReq> req = std::make_shared<IpcGetInfoByNetWorkReq>();
1454     std::shared_ptr<IpcGetInfoByNetWorkRsp> rsp = std::make_shared<IpcGetInfoByNetWorkRsp>();
1455     req->SetPkgName(pkgName);
1456     req->SetNetWorkId(netWorkId);
1457 
1458     int32_t ret = ipcClientProxy_->SendRequest(GET_NETWORKTYPE_BY_NETWORK, req, rsp);
1459     if (ret != DM_OK) {
1460         LOGI("GetNetworkTypeByNetworkId Send Request failed ret: %d", ret);
1461         return ERR_DM_IPC_SEND_REQUEST_FAILED;
1462     }
1463 
1464     ret = rsp->GetErrCode();
1465     if (ret != DM_OK) {
1466         LOGE("CheckAuthentication Failed with ret %d", ret);
1467         return ret;
1468     }
1469     netWorkType = rsp->GetNetworkType();
1470     return DM_OK;
1471 }
1472 } // namespace DistributedHardware
1473 } // namespace OHOS
1474