1 /*
2 * Copyright (c) 2021 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 "device_manager_notify.h"
19 #include "dm_anonymous.h"
20 #include "dm_constants.h"
21 #include "dm_log.h"
22 #include "ipc_authenticate_device_req.h"
23 #include "ipc_get_dmfaparam_rsp.h"
24 #include "ipc_get_info_by_network_req.h"
25 #include "ipc_get_info_by_network_rsp.h"
26 #include "ipc_get_local_device_info_rsp.h"
27 #include "ipc_get_trustdevice_req.h"
28 #include "ipc_get_trustdevice_rsp.h"
29 #include "ipc_req.h"
30 #include "ipc_rsp.h"
31 #include "ipc_set_useroperation_req.h"
32 #include "ipc_start_discovery_req.h"
33 #include "ipc_stop_discovery_req.h"
34 #include "ipc_unauthenticate_device_req.h"
35 #include "ipc_verify_authenticate_req.h"
36 #include "ipc_register_dev_state_callback_req.h"
37 #include "securec.h"
38
39 namespace OHOS {
40 namespace DistributedHardware {
GetInstance()41 DeviceManagerImpl &DeviceManagerImpl::GetInstance()
42 {
43 static DeviceManagerImpl instance;
44 return instance;
45 }
46
InitDeviceManager(const std::string & pkgName,std::shared_ptr<DmInitCallback> dmInitCallback)47 int32_t DeviceManagerImpl::InitDeviceManager(const std::string &pkgName, std::shared_ptr<DmInitCallback> dmInitCallback)
48 {
49 LOGI("DeviceManager::InitDeviceManager start, pkgName: %s", pkgName.c_str());
50 if (pkgName.empty() || dmInitCallback == nullptr) {
51 LOGE("InitDeviceManager error: Invalid parameter");
52 return DM_INVALID_VALUE;
53 }
54
55 int32_t ret = DM_OK;
56 int32_t retryNum = 0;
57 while (retryNum < SERVICE_INIT_TRY_MAX_NUM) {
58 ret = ipcClientProxy_->Init(pkgName);
59 if (ret != DM_NOT_INIT) {
60 break;
61 }
62 usleep(SLEEP_TIME_MS);
63 retryNum++;
64 if (retryNum == SERVICE_INIT_TRY_MAX_NUM) {
65 LOGE("InitDeviceManager error: wait for device manager service starting timeout.");
66 return DM_NOT_INIT;
67 }
68 }
69 if (ret != DM_OK) {
70 LOGE("InitDeviceManager error: proxy init failed ret: %d", ret);
71 return DM_INIT_FAILED;
72 }
73
74 DeviceManagerNotify::GetInstance().RegisterDeathRecipientCallback(pkgName, dmInitCallback);
75 LOGI("InitDeviceManager success");
76 return DM_OK;
77 }
78
UnInitDeviceManager(const std::string & pkgName)79 int32_t DeviceManagerImpl::UnInitDeviceManager(const std::string &pkgName)
80 {
81 LOGI("DeviceManager::UnInitDeviceManager start, pkgName: %s", pkgName.c_str());
82 if (pkgName.empty()) {
83 LOGE("UnInitDeviceManager error: Invalid parameter");
84 return DM_INVALID_VALUE;
85 }
86
87 int32_t ret = ipcClientProxy_->UnInit(pkgName);
88 if (ret != DM_OK) {
89 LOGE("UnInitDeviceManager error: proxy unInit failed ret: %d", ret);
90 return DM_UNINIT_FAILED;
91 }
92
93 DeviceManagerNotify::GetInstance().UnRegisterPackageCallback(pkgName);
94 LOGI("UnInitDeviceManager success");
95 return DM_OK;
96 }
97
GetTrustedDeviceList(const std::string & pkgName,const std::string & extra,std::vector<DmDeviceInfo> & deviceList)98 int32_t DeviceManagerImpl::GetTrustedDeviceList(const std::string &pkgName, const std::string &extra,
99 std::vector<DmDeviceInfo> &deviceList)
100 {
101 LOGI("DeviceManager::GetTrustedDeviceList start, pkgName: %s", pkgName.c_str());
102 if (pkgName.empty()) {
103 LOGE("GetTrustedDeviceList error: Invalid para");
104 return DM_INVALID_VALUE;
105 }
106
107 std::shared_ptr<IpcGetTrustDeviceReq> req = std::make_shared<IpcGetTrustDeviceReq>();
108 std::shared_ptr<IpcGetTrustDeviceRsp> rsp = std::make_shared<IpcGetTrustDeviceRsp>();
109 req->SetPkgName(pkgName);
110 req->SetExtra(extra);
111 int32_t ret = ipcClientProxy_->SendRequest(GET_TRUST_DEVICE_LIST, req, rsp);
112 if (ret != DM_OK) {
113 LOGE("GetTrustedDeviceList error: Send Request failed ret: %d", ret);
114 return DM_IPC_SEND_REQUEST_FAILED;
115 }
116
117 ret = rsp->GetErrCode();
118 if (ret != DM_OK) {
119 LOGI("GetTrustedDeviceList error: failed ret: %d", ret);
120 return DM_IPC_RESPOND_ERROR;
121 }
122
123 deviceList = rsp->GetDeviceVec();
124 LOGI("GetTrustedDeviceList completed, pkgName: %s", pkgName.c_str());
125 return DM_OK;
126 }
127
GetLocalDeviceInfo(const std::string & pkgName,DmDeviceInfo & info)128 int32_t DeviceManagerImpl::GetLocalDeviceInfo(const std::string &pkgName, DmDeviceInfo &info)
129 {
130 LOGI("DeviceManager::GetLocalDeviceInfo start, pkgName: %s", pkgName.c_str());
131 std::shared_ptr<IpcReq> req = std::make_shared<IpcReq>();
132 std::shared_ptr<IpcGetLocalDeviceInfoRsp> rsp = std::make_shared<IpcGetLocalDeviceInfoRsp>();
133 req->SetPkgName(pkgName);
134 int32_t ret = ipcClientProxy_->SendRequest(GET_LOCAL_DEVICE_INFO, req, rsp);
135 if (ret != DM_OK) {
136 LOGE("GetLocalDeviceInfo error: Send Request failed ret: %d", ret);
137 return DM_IPC_SEND_REQUEST_FAILED;
138 }
139
140 ret = rsp->GetErrCode();
141 if (ret != DM_OK) {
142 LOGI("GetLocalDeviceInfo error: failed ret: %d", ret);
143 return DM_IPC_RESPOND_ERROR;
144 }
145
146 info = rsp->GetLocalDeviceInfo();
147 LOGI("GetLocalDeviceInfo completed,pkgname%s", req->GetPkgName().c_str());
148 return DM_OK;
149 }
150
RegisterDevStateCallback(const std::string & pkgName,const std::string & extra,std::shared_ptr<DeviceStateCallback> callback)151 int32_t DeviceManagerImpl::RegisterDevStateCallback(const std::string &pkgName, const std::string &extra,
152 std::shared_ptr<DeviceStateCallback> callback)
153 {
154 LOGI("DeviceManager::RegisterDevStateCallback start, pkgName: %s", pkgName.c_str());
155 if (pkgName.empty() || callback == nullptr) {
156 LOGE("RegisterDevStateCallback error: Invalid para");
157 return DM_INVALID_VALUE;
158 }
159
160 DeviceManagerNotify::GetInstance().RegisterDeviceStateCallback(pkgName, callback);
161 if (!extra.empty()) {
162 RegisterDevStateCallback(pkgName, extra);
163 }
164 LOGI("RegisterDevStateCallback completed, pkgName: %s", pkgName.c_str());
165 return DM_OK;
166 }
167
UnRegisterDevStateCallback(const std::string & pkgName)168 int32_t DeviceManagerImpl::UnRegisterDevStateCallback(const std::string &pkgName)
169 {
170 LOGI("DeviceManager::UnRegisterDevStateCallback start, pkgName: %s", pkgName.c_str());
171 if (pkgName.empty()) {
172 LOGE("UnRegisterDevStateCallback error: Invalid para");
173 return DM_INVALID_VALUE;
174 }
175
176 DeviceManagerNotify::GetInstance().UnRegisterDeviceStateCallback(pkgName);
177 std::string extra = "";
178 UnRegisterDevStateCallback(pkgName, extra);
179 LOGI("UnRegisterDevStateCallback completed, pkgName: %s", pkgName.c_str());
180 return DM_OK;
181 }
182
StartDeviceDiscovery(const std::string & pkgName,const DmSubscribeInfo & subscribeInfo,const std::string & extra,std::shared_ptr<DiscoveryCallback> callback)183 int32_t DeviceManagerImpl::StartDeviceDiscovery(const std::string &pkgName, const DmSubscribeInfo &subscribeInfo,
184 const std::string &extra, std::shared_ptr<DiscoveryCallback> callback)
185 {
186 LOGI("DeviceManager::StartDeviceDiscovery start, pkgName: %s", pkgName.c_str());
187 if (pkgName.empty() || callback == nullptr) {
188 LOGE("StartDeviceDiscovery error: Invalid para");
189 return DM_INVALID_VALUE;
190 }
191
192 LOGI("DeviceManager StartDeviceDiscovery in, pkgName %s", pkgName.c_str());
193 DeviceManagerNotify::GetInstance().RegisterDiscoveryCallback(pkgName, subscribeInfo.subscribeId, callback);
194
195 std::shared_ptr<IpcStartDiscoveryReq> req = std::make_shared<IpcStartDiscoveryReq>();
196 std::shared_ptr<IpcRsp> rsp = std::make_shared<IpcRsp>();
197 req->SetPkgName(pkgName);
198 req->SetExtra(extra);
199 req->SetSubscribeInfo(subscribeInfo);
200 int32_t ret = ipcClientProxy_->SendRequest(START_DEVICE_DISCOVER, req, rsp);
201 if (ret != DM_OK) {
202 LOGE("StartDeviceDiscovery error: Send Request failed ret: %d", ret);
203 return DM_IPC_SEND_REQUEST_FAILED;
204 }
205
206 ret = rsp->GetErrCode();
207 if (ret != DM_OK) {
208 LOGE("StartDeviceDiscovery error: Failed with ret %d", ret);
209 return ret;
210 }
211
212 LOGI("StartDeviceDiscovery completed, pkgName: %s", pkgName.c_str());
213 return DM_OK;
214 }
215
StopDeviceDiscovery(const std::string & pkgName,uint16_t subscribeId)216 int32_t DeviceManagerImpl::StopDeviceDiscovery(const std::string &pkgName, uint16_t subscribeId)
217 {
218 LOGI("DeviceManager::StopDeviceDiscovery start , pkgName: %s", pkgName.c_str());
219 if (pkgName.empty()) {
220 LOGE("StopDeviceDiscovery error: Invalid para");
221 return DM_INVALID_VALUE;
222 }
223
224 LOGI("StopDeviceDiscovery in, pkgName %s", pkgName.c_str());
225 std::shared_ptr<IpcStopDiscoveryReq> req = std::make_shared<IpcStopDiscoveryReq>();
226 std::shared_ptr<IpcRsp> rsp = std::make_shared<IpcRsp>();
227 req->SetPkgName(pkgName);
228 req->SetSubscribeId(subscribeId);
229 int32_t ret = ipcClientProxy_->SendRequest(STOP_DEVICE_DISCOVER, req, rsp);
230 if (ret != DM_OK) {
231 LOGE("StopDeviceDiscovery error: Send Request failed ret: %d", ret);
232 return DM_IPC_SEND_REQUEST_FAILED;
233 }
234
235 ret = rsp->GetErrCode();
236 if (ret != DM_OK) {
237 LOGE("StopDeviceDiscovery error: Failed with ret %d", ret);
238 return ret;
239 }
240
241 DeviceManagerNotify::GetInstance().UnRegisterDiscoveryCallback(pkgName, subscribeId);
242 LOGI("StopDeviceDiscovery completed, pkgName: %s", pkgName.c_str());
243 return DM_OK;
244 }
245
AuthenticateDevice(const std::string & pkgName,int32_t authType,const DmDeviceInfo & deviceInfo,const std::string & extra,std::shared_ptr<AuthenticateCallback> callback)246 int32_t DeviceManagerImpl::AuthenticateDevice(const std::string &pkgName, int32_t authType,
247 const DmDeviceInfo &deviceInfo, const std::string &extra,
248 std::shared_ptr<AuthenticateCallback> callback)
249 {
250 LOGI("DeviceManager::AuthenticateDevice start , pkgName: %s", pkgName.c_str());
251 if (pkgName.empty()) {
252 LOGE("AuthenticateDevice error: Invalid para");
253 return DM_INVALID_VALUE;
254 }
255
256 std::string strDeviceId = deviceInfo.deviceId;
257 DeviceManagerNotify::GetInstance().RegisterAuthenticateCallback(pkgName, strDeviceId, callback);
258 std::shared_ptr<IpcAuthenticateDeviceReq> req = std::make_shared<IpcAuthenticateDeviceReq>();
259 std::shared_ptr<IpcRsp> rsp = std::make_shared<IpcRsp>();
260 req->SetPkgName(pkgName);
261 req->SetExtra(extra);
262 req->SetAuthType(authType);
263 req->SetDeviceInfo(deviceInfo);
264 int32_t ret = ipcClientProxy_->SendRequest(AUTHENTICATE_DEVICE, req, rsp);
265 if (ret != DM_OK) {
266 LOGE("AuthenticateDevice error: Send Request failed ret: %d", ret);
267 return DM_IPC_SEND_REQUEST_FAILED;
268 }
269
270 ret = rsp->GetErrCode();
271 if (ret != DM_OK) {
272 LOGE("AuthenticateDevice error: Failed with ret %d", ret);
273 return DM_IPC_RESPOND_ERROR;
274 }
275 LOGI("DeviceManager::AuthenticateDevice completed, pkgName: %s", pkgName.c_str());
276 return DM_OK;
277 }
278
UnAuthenticateDevice(const std::string & pkgName,const DmDeviceInfo & deviceInfo)279 int32_t DeviceManagerImpl::UnAuthenticateDevice(const std::string &pkgName, const DmDeviceInfo &deviceInfo)
280 {
281 LOGI("DeviceManager::UnAuthenticateDevice start , pkgName: %s, deviceId: %s", pkgName.c_str(), deviceInfo.deviceId);
282
283 if (pkgName.empty() || (deviceInfo.deviceId[0] == '\0')) {
284 LOGE("UnAuthenticateDevice error: Invalid para");
285 return DM_INVALID_VALUE;
286 }
287
288 std::shared_ptr<IpcUnAuthenticateDeviceReq> req = std::make_shared<IpcUnAuthenticateDeviceReq>();
289 std::shared_ptr<IpcRsp> rsp = std::make_shared<IpcRsp>();
290 req->SetPkgName(pkgName);
291 req->SetDeviceInfo(deviceInfo);
292 int32_t ret = ipcClientProxy_->SendRequest(UNAUTHENTICATE_DEVICE, req, rsp);
293 if (ret != DM_OK) {
294 LOGE("UnAuthenticateDevice error: Send Request failed ret: %d", ret);
295 return DM_IPC_SEND_REQUEST_FAILED;
296 }
297 ret = rsp->GetErrCode();
298 if (ret != DM_OK) {
299 LOGE("UnAuthenticateDevice error: Failed with ret %d", ret);
300 return DM_IPC_RESPOND_ERROR;
301 }
302
303 LOGI("UnAuthenticateDevice completed, pkgName: %s", pkgName.c_str());
304 return DM_OK;
305 }
306
RegisterDeviceManagerFaCallback(const std::string & pkgName,std::shared_ptr<DeviceManagerUiCallback> callback)307 int32_t DeviceManagerImpl::RegisterDeviceManagerFaCallback(const std::string &pkgName,
308 std::shared_ptr<DeviceManagerUiCallback> callback)
309 {
310 LOGI("DeviceManager::RegisterDeviceManagerFaCallback start, pkgName: %s", pkgName.c_str());
311 if (pkgName.empty() || callback == nullptr) {
312 LOGE("RegisterDeviceManagerFaCallback error: Invalid para");
313 return DM_INVALID_VALUE;
314 }
315 DeviceManagerNotify::GetInstance().RegisterDeviceManagerFaCallback(pkgName, callback);
316 LOGI("DeviceManager::RegisterDevStateCallback completed, pkgName: %s", pkgName.c_str());
317 return DM_OK;
318 }
319
UnRegisterDeviceManagerFaCallback(const std::string & pkgName)320 int32_t DeviceManagerImpl::UnRegisterDeviceManagerFaCallback(const std::string &pkgName)
321 {
322 LOGI("DeviceManager::UnRegisterDeviceManagerFaCallback start, pkgName: %s", pkgName.c_str());
323 if (pkgName.empty()) {
324 LOGE("UnRegisterDeviceManagerFaCallback error: Invalid para");
325 return DM_INVALID_VALUE;
326 }
327 DeviceManagerNotify::GetInstance().UnRegisterDeviceManagerFaCallback(pkgName);
328 LOGI("DeviceManager::UnRegisterDevStateCallback completed, pkgName: %s", pkgName.c_str());
329 return DM_OK;
330 }
331
VerifyAuthentication(const std::string & pkgName,const std::string & authPara,std::shared_ptr<VerifyAuthCallback> callback)332 int32_t DeviceManagerImpl::VerifyAuthentication(const std::string &pkgName, const std::string &authPara,
333 std::shared_ptr<VerifyAuthCallback> callback)
334 {
335 LOGI("DeviceManager::VerifyAuthentication start, pkgName: %s", pkgName.c_str());
336 if (pkgName.empty()) {
337 LOGE("VerifyAuthentication error: Invalid para");
338 return DM_INVALID_VALUE;
339 }
340
341 DeviceManagerNotify::GetInstance().RegisterVerifyAuthenticationCallback(pkgName, authPara, callback);
342
343 std::shared_ptr<IpcVerifyAuthenticateReq> req = std::make_shared<IpcVerifyAuthenticateReq>();
344 std::shared_ptr<IpcRsp> rsp = std::make_shared<IpcRsp>();
345 req->SetPkgName(pkgName);
346 req->SetAuthPara(authPara);
347
348 int32_t ret = ipcClientProxy_->SendRequest(VERIFY_AUTHENTICATION, req, rsp);
349 if (ret != DM_OK) {
350 LOGE("VerifyAuthentication error: Send Request failed ret: %d", ret);
351 return DM_IPC_SEND_REQUEST_FAILED;
352 }
353 ret = rsp->GetErrCode();
354 if (ret != DM_OK) {
355 LOGE("VerifyAuthentication error: Failed with ret %d", ret);
356 return ret;
357 }
358
359 LOGI("VerifyAuthentication completed, pkgName: %s", pkgName.c_str());
360 return DM_OK;
361 }
362
GetFaParam(const std::string & pkgName,DmAuthParam & dmFaParam)363 int32_t DeviceManagerImpl::GetFaParam(const std::string &pkgName, DmAuthParam &dmFaParam)
364 {
365 LOGI("DeviceManager::GetFaParam start, pkgName: %s", pkgName.c_str());
366 if (pkgName.empty()) {
367 LOGE("VerifyAuthentication failed, pkgName is empty");
368 return DM_INVALID_VALUE;
369 }
370
371 std::shared_ptr<IpcReq> req = std::make_shared<IpcReq>();
372 std::shared_ptr<IpcGetDmFaParamRsp> rsp = std::make_shared<IpcGetDmFaParamRsp>();
373 req->SetPkgName(pkgName);
374
375 if (ipcClientProxy_->SendRequest(SERVER_GET_DMFA_INFO, req, rsp) != DM_OK) {
376 LOGI("DeviceManagerImpl::GetFaParam start 2");
377 return DM_IPC_SEND_REQUEST_FAILED;
378 }
379 dmFaParam = rsp->GetDmAuthParam();
380 LOGI("GetFaParam completed, pkgName: %s", pkgName.c_str());
381 return DM_OK;
382 }
383
SetUserOperation(const std::string & pkgName,int32_t action,const std::string & params)384 int32_t DeviceManagerImpl::SetUserOperation(const std::string &pkgName, int32_t action, const std::string ¶ms)
385 {
386 if (pkgName.empty() || params.empty()) {
387 LOGE("DeviceManager::SetUserOperation start, pkgName: %s, params: %s", pkgName.c_str(), params.c_str());
388 return DM_INVALID_VALUE;;
389 }
390 LOGI("SetUserOperation start, pkgName: %s", pkgName.c_str());
391
392 std::shared_ptr<IpcGetOperationReq> req = std::make_shared<IpcGetOperationReq>();
393 std::shared_ptr<IpcRsp> rsp = std::make_shared<IpcRsp>();
394 req->SetPkgName(pkgName);
395 req->SetOperation(action);
396 req->SetParams(params);
397
398 if (ipcClientProxy_->SendRequest(SERVER_USER_AUTH_OPERATION, req, rsp) != DM_OK) {
399 return DM_IPC_SEND_REQUEST_FAILED;
400 }
401 int32_t ret = rsp->GetErrCode();
402 if (ret != DM_OK) {
403 LOGE("CheckAuthentication Failed with ret %d", ret);
404 return ret;
405 }
406 LOGI("SetUserOperation completed, pkgName: %s", pkgName.c_str());
407 return DM_OK;
408 }
409
GetUdidByNetworkId(const std::string & pkgName,const std::string & netWorkId,std::string & udid)410 int32_t DeviceManagerImpl::GetUdidByNetworkId(const std::string &pkgName, const std::string &netWorkId,
411 std::string &udid)
412 {
413 if (pkgName.empty() || netWorkId.empty()) {
414 LOGE("DeviceManagerImpl::GetUdidByNetworkId error: Invalid para, pkgName: %s, netWorkId: %s",
415 pkgName.c_str(), GetAnonyString(netWorkId).c_str());
416 return DM_INVALID_VALUE;
417 }
418
419 std::shared_ptr<IpcGetInfoByNetWorkReq> req = std::make_shared<IpcGetInfoByNetWorkReq>();
420 std::shared_ptr<IpcGetInfoByNetWorkRsp> rsp = std::make_shared<IpcGetInfoByNetWorkRsp>();
421 req->SetPkgName(pkgName);
422 req->SetNetWorkId(netWorkId);
423
424 if (ipcClientProxy_->SendRequest(GET_UDID_BY_NETWORK, req, rsp) != DM_OK) {
425 return DM_IPC_SEND_REQUEST_FAILED;
426 }
427 int32_t ret = rsp->GetErrCode();
428 if (ret != DM_OK) {
429 LOGE("CheckAuthentication Failed with ret %d", ret);
430 return ret;
431 }
432 udid = rsp->GetUdid();
433 return DM_OK;
434 }
435
GetUuidByNetworkId(const std::string & pkgName,const std::string & netWorkId,std::string & uuid)436 int32_t DeviceManagerImpl::GetUuidByNetworkId(const std::string &pkgName, const std::string &netWorkId,
437 std::string &uuid)
438 {
439 if (pkgName.empty() || netWorkId.empty()) {
440 LOGE("DeviceManagerImpl::GetUuidByNetworkId error: Invalid para, pkgName: %s, netWorkId: %s, uuid: %s",
441 pkgName.c_str(), GetAnonyString(netWorkId).c_str(), GetAnonyString(uuid).c_str());
442 return DM_INVALID_VALUE;
443 }
444
445 std::shared_ptr<IpcGetInfoByNetWorkReq> req = std::make_shared<IpcGetInfoByNetWorkReq>();
446 std::shared_ptr<IpcGetInfoByNetWorkRsp> rsp = std::make_shared<IpcGetInfoByNetWorkRsp>();
447 req->SetPkgName(pkgName);
448 req->SetNetWorkId(netWorkId);
449
450 if (ipcClientProxy_->SendRequest(GET_UUID_BY_NETWORK, req, rsp) != DM_OK) {
451 return DM_IPC_SEND_REQUEST_FAILED;
452 }
453 int32_t ret = rsp->GetErrCode();
454 if (ret != DM_OK) {
455 LOGE("CheckAuthentication Failed with ret %d", ret);
456 return ret;
457 }
458 uuid = rsp->GetUuid();
459 return DM_OK;
460 }
461
RegisterDevStateCallback(const std::string & pkgName,const std::string & extra)462 int32_t DeviceManagerImpl::RegisterDevStateCallback(const std::string &pkgName, const std::string &extra)
463 {
464 if (pkgName.empty()) {
465 LOGE("RegisterDevStateCallback failed, pkgName is empty");
466 return DM_INVALID_VALUE;
467 }
468
469 std::shared_ptr<IpcRegisterDevStateCallbackReq> req = std::make_shared<IpcRegisterDevStateCallbackReq>();
470 std::shared_ptr<IpcRsp> rsp = std::make_shared<IpcRsp>();
471 req->SetPkgName(pkgName);
472 req->SetExtra(extra);
473
474 if (ipcClientProxy_->SendRequest(REGISTER_DEV_STATE_CALLBACK, req, rsp) != DM_OK) {
475 return DM_IPC_SEND_REQUEST_FAILED;
476 }
477 int32_t ret = rsp->GetErrCode();
478 if (ret != DM_OK) {
479 LOGE("RegisterDevStateCallback Failed with ret %d", ret);
480 return ret;
481 }
482 return DM_OK;
483 }
484
UnRegisterDevStateCallback(const std::string & pkgName,const std::string & extra)485 int32_t DeviceManagerImpl::UnRegisterDevStateCallback(const std::string &pkgName, const std::string &extra)
486 {
487 if (pkgName.empty()) {
488 LOGE("UnRegisterDevStateCallback failed, pkgName is empty");
489 return DM_INVALID_VALUE;
490 }
491
492 std::shared_ptr<IpcRegisterDevStateCallbackReq> req = std::make_shared<IpcRegisterDevStateCallbackReq>();
493 std::shared_ptr<IpcRsp> rsp = std::make_shared<IpcRsp>();
494 req->SetPkgName(pkgName);
495 req->SetExtra(extra);
496
497 if (ipcClientProxy_->SendRequest(UNREGISTER_DEV_STATE_CALLBACK, req, rsp) != DM_OK) {
498 return DM_IPC_SEND_REQUEST_FAILED;
499 }
500 int32_t ret = rsp->GetErrCode();
501 if (ret != DM_OK) {
502 LOGE("UnRegisterDevStateCallback Failed with ret %d", ret);
503 return ret;
504 }
505 return DM_OK;
506 }
507 } // namespace DistributedHardware
508 } // namespace OHOS
509