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