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