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_service_impl.h"
17
18 #include <functional>
19
20 #include "dm_anonymous.h"
21 #include "dm_constants.h"
22 #include "dm_crypto.h"
23 #include "dm_distributed_hardware_load.h"
24 #include "dm_log.h"
25 #include "multiple_user_connector.h"
26 #include "app_manager.h"
27 #include "dm_radar_helper.h"
28 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
29 #include "dm_common_event_manager.h"
30 #include "parameter.h"
31 #include "common_event_support.h"
32 using namespace OHOS::EventFwk;
33 #endif
34
35 namespace OHOS {
36 namespace DistributedHardware {
DeviceManagerServiceImpl()37 DeviceManagerServiceImpl::DeviceManagerServiceImpl()
38 {
39 LOGI("DeviceManagerServiceImpl constructor");
40 }
41
~DeviceManagerServiceImpl()42 DeviceManagerServiceImpl::~DeviceManagerServiceImpl()
43 {
44 LOGI("DeviceManagerServiceImpl destructor");
45 }
46
Initialize(const std::shared_ptr<IDeviceManagerServiceListener> & listener)47 int32_t DeviceManagerServiceImpl::Initialize(const std::shared_ptr<IDeviceManagerServiceListener> &listener)
48 {
49 LOGI("DeviceManagerServiceImpl Initialize");
50 if (softbusConnector_ == nullptr) {
51 softbusConnector_ = std::make_shared<SoftbusConnector>();
52 }
53 if (hiChainConnector_ == nullptr) {
54 hiChainConnector_ = std::make_shared<HiChainConnector>();
55 }
56 if (mineHiChainConnector_ == nullptr) {
57 mineHiChainConnector_ = std::make_shared<MineHiChainConnector>();
58 }
59 if (discoveryMgr_ == nullptr) {
60 discoveryMgr_ = std::make_shared<DmDiscoveryManager>(softbusConnector_, listener, hiChainConnector_);
61 }
62 if (publishMgr_ == nullptr) {
63 publishMgr_ = std::make_shared<DmPublishManager>(softbusConnector_, listener);
64 }
65 if (hiChainAuthConnector_ == nullptr) {
66 hiChainAuthConnector_ = std::make_shared<HiChainAuthConnector>();
67 }
68 if (deviceStateMgr_ == nullptr) {
69 deviceStateMgr_ = std::make_shared<DmDeviceStateManager>(softbusConnector_, listener,
70 hiChainConnector_, hiChainAuthConnector_);
71 deviceStateMgr_->RegisterSoftbusStateCallback();
72 }
73 if (authMgr_ == nullptr) {
74 authMgr_ = std::make_shared<DmAuthManager>(softbusConnector_, hiChainConnector_, listener,
75 hiChainAuthConnector_);
76 softbusConnector_->GetSoftbusSession()->RegisterSessionCallback(authMgr_);
77 hiChainConnector_->RegisterHiChainCallback(authMgr_);
78 hiChainAuthConnector_->RegisterHiChainAuthCallback(authMgr_);
79 }
80 if (credentialMgr_ == nullptr) {
81 credentialMgr_ = std::make_shared<DmCredentialManager>(hiChainConnector_, listener);
82 }
83 LOGI("Init success, singleton initialized");
84 return DM_OK;
85 }
86
Release()87 void DeviceManagerServiceImpl::Release()
88 {
89 LOGI("DeviceManagerServiceImpl Release");
90 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
91 commonEventManager_ = nullptr;
92 #endif
93 softbusConnector_->GetSoftbusSession()->UnRegisterSessionCallback();
94 hiChainConnector_->UnRegisterHiChainCallback();
95 authMgr_ = nullptr;
96 deviceStateMgr_ = nullptr;
97 discoveryMgr_ = nullptr;
98 publishMgr_ = nullptr;
99 softbusConnector_ = nullptr;
100 abilityMgr_ = nullptr;
101 hiChainConnector_ = nullptr;
102 }
103
StartDeviceDiscovery(const std::string & pkgName,const DmSubscribeInfo & subscribeInfo,const std::string & extra)104 int32_t DeviceManagerServiceImpl::StartDeviceDiscovery(const std::string &pkgName, const DmSubscribeInfo &subscribeInfo,
105 const std::string &extra)
106 {
107 if (pkgName.empty()) {
108 LOGE("StartDeviceDiscovery failed, pkgName is empty");
109 return ERR_DM_INPUT_PARA_INVALID;
110 }
111 return discoveryMgr_->StartDeviceDiscovery(pkgName, subscribeInfo, extra);
112 }
113
StartDeviceDiscovery(const std::string & pkgName,const uint16_t subscribeId,const std::string & filterOptions)114 int32_t DeviceManagerServiceImpl::StartDeviceDiscovery(const std::string &pkgName, const uint16_t subscribeId,
115 const std::string &filterOptions)
116 {
117 if (pkgName.empty()) {
118 LOGE("StartDeviceDiscovery failed, pkgName is empty");
119 return ERR_DM_INPUT_PARA_INVALID;
120 }
121 return discoveryMgr_->StartDeviceDiscovery(pkgName, subscribeId, filterOptions);
122 }
123
StopDeviceDiscovery(const std::string & pkgName,uint16_t subscribeId)124 int32_t DeviceManagerServiceImpl::StopDeviceDiscovery(const std::string &pkgName, uint16_t subscribeId)
125 {
126 if (pkgName.empty()) {
127 LOGE("StopDeviceDiscovery failed, pkgName is empty");
128 return ERR_DM_INPUT_PARA_INVALID;
129 }
130 return discoveryMgr_->StopDeviceDiscovery(pkgName, subscribeId);
131 }
132
PublishDeviceDiscovery(const std::string & pkgName,const DmPublishInfo & publishInfo)133 int32_t DeviceManagerServiceImpl::PublishDeviceDiscovery(const std::string &pkgName, const DmPublishInfo &publishInfo)
134 {
135 if (pkgName.empty()) {
136 LOGE("PublishDeviceDiscovery failed, pkgName is empty");
137 return ERR_DM_INPUT_PARA_INVALID;
138 }
139 return publishMgr_->PublishDeviceDiscovery(pkgName, publishInfo);
140 }
141
UnPublishDeviceDiscovery(const std::string & pkgName,int32_t publishId)142 int32_t DeviceManagerServiceImpl::UnPublishDeviceDiscovery(const std::string &pkgName, int32_t publishId)
143 {
144 if (pkgName.empty()) {
145 LOGE("UnPublishDeviceDiscovery failed, pkgName is empty");
146 return ERR_DM_INPUT_PARA_INVALID;
147 }
148 return publishMgr_->UnPublishDeviceDiscovery(pkgName, publishId);
149 }
150
AuthenticateDevice(const std::string & pkgName,int32_t authType,const std::string & deviceId,const std::string & extra)151 int32_t DeviceManagerServiceImpl::AuthenticateDevice(const std::string &pkgName, int32_t authType,
152 const std::string &deviceId, const std::string &extra)
153 {
154 if (pkgName.empty() || deviceId.empty()) {
155 LOGE("DeviceManagerServiceImpl::AuthenticateDevice failed, pkgName is %{public}s, deviceId is %{public}s,"
156 "extra is %{public}s", pkgName.c_str(), GetAnonyString(deviceId).c_str(), extra.c_str());
157 return ERR_DM_INPUT_PARA_INVALID;
158 }
159 if (deviceStateMgr_ != nullptr) {
160 deviceStateMgr_->DeleteOffLineTimer(deviceId);
161 }
162 return authMgr_->AuthenticateDevice(pkgName, authType, deviceId, extra);
163 }
164
UnAuthenticateDevice(const std::string & pkgName,const std::string & networkId)165 int32_t DeviceManagerServiceImpl::UnAuthenticateDevice(const std::string &pkgName, const std::string &networkId)
166 {
167 if (pkgName.empty() || networkId.empty()) {
168 LOGE("DeviceManagerServiceImpl::UnAuthenticateDevice failed, pkgName is %{public}s, networkId is %{public}s",
169 pkgName.c_str(), GetAnonyString(networkId).c_str());
170 return ERR_DM_INPUT_PARA_INVALID;
171 }
172 return authMgr_->UnAuthenticateDevice(pkgName, networkId);
173 }
174
StopAuthenticateDevice(const std::string & pkgName)175 int32_t DeviceManagerServiceImpl::StopAuthenticateDevice(const std::string &pkgName)
176 {
177 if (pkgName.empty()) {
178 LOGE("DeviceManagerServiceImpl::StopAuthenticateDevice failed");
179 return ERR_DM_INPUT_PARA_INVALID;
180 }
181 return authMgr_->StopAuthenticateDevice(pkgName);
182 }
183
BindDevice(const std::string & pkgName,int32_t authType,const std::string & udidHash,const std::string & bindParam)184 int32_t DeviceManagerServiceImpl::BindDevice(const std::string &pkgName, int32_t authType, const std::string &udidHash,
185 const std::string &bindParam)
186 {
187 if (pkgName.empty() || udidHash.empty()) {
188 LOGE("DeviceManagerServiceImpl::BindDevice failed, pkgName is %{public}s, udidHash is %{public}s, bindParam is"
189 "%{public}s", pkgName.c_str(), GetAnonyString(udidHash).c_str(), bindParam.c_str());
190 return ERR_DM_INPUT_PARA_INVALID;
191 }
192 if (deviceStateMgr_ != nullptr) {
193 deviceStateMgr_->DeleteOffLineTimer(udidHash);
194 }
195 return authMgr_->AuthenticateDevice(pkgName, authType, udidHash, bindParam);
196 }
197
UnBindDevice(const std::string & pkgName,const std::string & udidHash)198 int32_t DeviceManagerServiceImpl::UnBindDevice(const std::string &pkgName, const std::string &udidHash)
199 {
200 if (pkgName.empty() || udidHash.empty()) {
201 LOGE("DeviceManagerServiceImpl::UnBindDevice failed, pkgName is %{public}s, udidHash is %{public}s",
202 pkgName.c_str(), GetAnonyString(udidHash).c_str());
203 return ERR_DM_INPUT_PARA_INVALID;
204 }
205 return authMgr_->UnBindDevice(pkgName, udidHash);
206 }
207
SetUserOperation(std::string & pkgName,int32_t action,const std::string & params)208 int32_t DeviceManagerServiceImpl::SetUserOperation(std::string &pkgName, int32_t action,
209 const std::string ¶ms)
210 {
211 if (pkgName.empty() || params.empty()) {
212 LOGE("DeviceManagerServiceImpl::SetUserOperation error: Invalid parameter, pkgName: %{public}s, extra:"
213 "%{public}s", pkgName.c_str(), params.c_str());
214 return ERR_DM_INPUT_PARA_INVALID;
215 }
216 if (authMgr_ != nullptr) {
217 authMgr_->OnUserOperation(action, params);
218 }
219 return DM_OK;
220 }
221
HandleOffline(DmDeviceState devState,DmDeviceInfo & devInfo)222 void DeviceManagerServiceImpl::HandleOffline(DmDeviceState devState, DmDeviceInfo &devInfo)
223 {
224 LOGI("DeviceManagerServiceImpl::HandleOffline");
225 std::string trustDeviceId = deviceStateMgr_->GetUdidByNetWorkId(std::string(devInfo.networkId));
226 LOGI("deviceStateMgr Udid: %{public}s", GetAnonyString(trustDeviceId).c_str());
227 if (trustDeviceId == "") {
228 LOGE("HandleOffline not get udid in deviceStateMgr.");
229 return;
230 }
231 std::string udisHash = softbusConnector_->GetDeviceUdidHashByUdid(trustDeviceId);
232 if (memcpy_s(devInfo.deviceId, DM_MAX_DEVICE_ID_LEN, udisHash.c_str(), udisHash.length()) != 0) {
233 LOGE("get deviceId: %{public}s failed", GetAnonyString(udisHash).c_str());
234 return;
235 }
236 char localUdid[DEVICE_UUID_LENGTH] = {0};
237 GetDevUdid(localUdid, DEVICE_UUID_LENGTH);
238 std::string requestDeviceId = static_cast<std::string>(localUdid);
239 DmOfflineParam offlineParam =
240 DeviceProfileConnector::GetInstance().GetOfflineParamFromAcl(trustDeviceId, requestDeviceId);
241 LOGI("The offline device bind type is %{public}d.", offlineParam.bindType);
242 if (offlineParam.leftAclNumber == 0 && offlineParam.bindType == INVALIED_TYPE) {
243 deviceStateMgr_->HandleDeviceStatusChange(devState, devInfo);
244 } else if (offlineParam.bindType == IDENTICAL_ACCOUNT_TYPE) {
245 LOGI("The offline device is identical account bind type.");
246 deviceStateMgr_->HandleDeviceStatusChange(devState, devInfo);
247 } else if (offlineParam.bindType == DEVICE_PEER_TO_PEER_TYPE) {
248 LOGI("The offline device is device-level bind type.");
249 deviceStateMgr_->HandleDeviceStatusChange(devState, devInfo);
250 } else if (offlineParam.bindType == APP_PEER_TO_PEER_TYPE) {
251 LOGI("The offline device is app-level bind type.");
252 softbusConnector_->SetPkgNameVec(offlineParam.pkgNameVec);
253 deviceStateMgr_->HandleDeviceStatusChange(devState, devInfo);
254 }
255 if (offlineParam.leftAclNumber == 0) {
256 LOGI("Delete credential in HandleDeviceOffline.");
257 hiChainAuthConnector_->DeleteCredential(trustDeviceId, MultipleUserConnector::GetCurrentAccountUserID());
258 }
259 }
260
HandleOnline(DmDeviceState devState,DmDeviceInfo & devInfo)261 void DeviceManagerServiceImpl::HandleOnline(DmDeviceState devState, DmDeviceInfo &devInfo)
262 {
263 LOGI("DeviceManagerServiceImpl::HandleOnline");
264 std::string trustDeviceId = "";
265 if (softbusConnector_->GetUdidByNetworkId(devInfo.networkId, trustDeviceId) != DM_OK) {
266 LOGE("HandleDeviceOffline get udid failed.");
267 return;
268 }
269 std::string udisHash = softbusConnector_->GetDeviceUdidHashByUdid(trustDeviceId);
270 if (memcpy_s(devInfo.deviceId, DM_MAX_DEVICE_ID_LEN, udisHash.c_str(), udisHash.length()) != 0) {
271 LOGE("get deviceId: %{public}s failed", GetAnonyString(udisHash).c_str());
272 return;
273 }
274 char localUdid[DEVICE_UUID_LENGTH] = {0};
275 GetDevUdid(localUdid, DEVICE_UUID_LENGTH);
276 std::string requestDeviceId = static_cast<std::string>(localUdid);
277 uint32_t bindType = DeviceProfileConnector::GetInstance().CheckBindType(trustDeviceId, requestDeviceId);
278 LOGI("The online device bind type is %{public}d.", bindType);
279 if (bindType == INVALIED_TYPE && isCredentialType_.load()) {
280 PutIdenticalAccountToAcl(requestDeviceId, trustDeviceId);
281 devInfo.authForm = DmAuthForm::IDENTICAL_ACCOUNT;
282 } else if (bindType == IDENTICAL_ACCOUNT_TYPE) {
283 devInfo.authForm = DmAuthForm::IDENTICAL_ACCOUNT;
284 } else if (bindType == DEVICE_PEER_TO_PEER_TYPE) {
285 devInfo.authForm = DmAuthForm::PEER_TO_PEER;
286 } else if (bindType == DEVICE_ACROSS_ACCOUNT_TYPE) {
287 devInfo.authForm = DmAuthForm::ACROSS_ACCOUNT;
288 } else if (bindType == APP_PEER_TO_PEER_TYPE) {
289 std::vector<std::string> pkgNameVec =
290 DeviceProfileConnector::GetInstance().GetPkgNameFromAcl(requestDeviceId, trustDeviceId);
291 if (pkgNameVec.size() == 0) {
292 LOGI("The online device not need report pkgname");
293 return;
294 }
295 softbusConnector_->SetPkgNameVec(pkgNameVec);
296 devInfo.authForm = DmAuthForm::PEER_TO_PEER;
297 } else if (bindType == APP_ACROSS_ACCOUNT_TYPE) {
298 std::vector<std::string> pkgNameVec =
299 DeviceProfileConnector::GetInstance().GetPkgNameFromAcl(requestDeviceId, trustDeviceId);
300 if (pkgNameVec.size() == 0) {
301 LOGI("The online device not need report pkgname");
302 return;
303 }
304 softbusConnector_->SetPkgNameVec(pkgNameVec);
305 devInfo.authForm = DmAuthForm::ACROSS_ACCOUNT;
306 }
307 LOGI("DeviceManagerServiceImpl::HandleOnline success devInfo auform %{public}d.", devInfo.authForm);
308 deviceStateMgr_->HandleDeviceStatusChange(devState, devInfo);
309 }
310
HandleDeviceStatusChange(DmDeviceState devState,DmDeviceInfo & devInfo)311 void DeviceManagerServiceImpl::HandleDeviceStatusChange(DmDeviceState devState, DmDeviceInfo &devInfo)
312 {
313 if (deviceStateMgr_ == nullptr) {
314 LOGE("deviceStateMgr_ is nullpter!");
315 return;
316 }
317 if (devState == DEVICE_STATE_ONLINE) {
318 HandleOnline(devState, devInfo);
319 } else if (devState == DEVICE_STATE_OFFLINE) {
320 HandleOffline(devState, devInfo);
321 } else {
322 std::string udiddHash = GetUdidHashByNetworkId(devInfo.networkId);
323 if (memcpy_s(devInfo.deviceId, DM_MAX_DEVICE_ID_LEN, udiddHash.c_str(), udiddHash.length()) != 0) {
324 LOGE("get deviceId: %{public}s failed", GetAnonyString(udiddHash).c_str());
325 return;
326 }
327 deviceStateMgr_->HandleDeviceStatusChange(devState, devInfo);
328 }
329 }
330
GetUdidHashByNetworkId(const std::string & networkId)331 std::string DeviceManagerServiceImpl::GetUdidHashByNetworkId(const std::string &networkId)
332 {
333 if (softbusConnector_ == nullptr) {
334 LOGE("softbusConnector_ is nullpter!");
335 return "";
336 }
337 std::string udid = "";
338 int32_t ret = softbusConnector_->GetUdidByNetworkId(networkId.c_str(), udid);
339 if (ret != DM_OK) {
340 LOGE("GetUdidByNetworkId failed ret: %{public}d", ret);
341 return "";
342 }
343 return softbusConnector_->GetDeviceUdidHashByUdid(udid);
344 }
345
OnSessionOpened(int sessionId,int result)346 int DeviceManagerServiceImpl::OnSessionOpened(int sessionId, int result)
347 {
348 std::string peerUdid = "";
349 softbusConnector_->GetSoftbusSession()->GetPeerDeviceId(sessionId, peerUdid);
350 struct RadarInfo info = {
351 .funcName = "OnSessionOpened",
352 .stageRes = static_cast<int32_t>(StageRes::STAGE_SUCC),
353 .isTrust = static_cast<int32_t>(TrustStatus::NOT_TRUST),
354 .peerUdid = peerUdid,
355 .channelId = sessionId,
356 };
357 if (!DmRadarHelper::GetInstance().ReportAuthSessionOpenCb(info)) {
358 LOGE("ReportAuthSessionOpenCb failed");
359 }
360 return SoftbusSession::OnSessionOpened(sessionId, result);
361 }
362
OnSessionClosed(int sessionId)363 void DeviceManagerServiceImpl::OnSessionClosed(int sessionId)
364 {
365 SoftbusSession::OnSessionClosed(sessionId);
366 }
367
OnBytesReceived(int sessionId,const void * data,unsigned int dataLen)368 void DeviceManagerServiceImpl::OnBytesReceived(int sessionId, const void *data, unsigned int dataLen)
369 {
370 SoftbusSession::OnBytesReceived(sessionId, data, dataLen);
371 }
372
RequestCredential(const std::string & reqJsonStr,std::string & returnJsonStr)373 int32_t DeviceManagerServiceImpl::RequestCredential(const std::string &reqJsonStr, std::string &returnJsonStr)
374 {
375 if (reqJsonStr.empty()) {
376 LOGE("reqJsonStr is empty");
377 return ERR_DM_INPUT_PARA_INVALID;
378 }
379 if (credentialMgr_== nullptr) {
380 LOGE("credentialMgr_ is nullptr");
381 return ERR_DM_POINT_NULL;
382 }
383 return credentialMgr_->RequestCredential(reqJsonStr, returnJsonStr);
384 }
385
ImportCredential(const std::string & pkgName,const std::string & credentialInfo)386 int32_t DeviceManagerServiceImpl::ImportCredential(const std::string &pkgName, const std::string &credentialInfo)
387 {
388 if (pkgName.empty() || credentialInfo.empty()) {
389 LOGE("DeviceManagerServiceImpl::ImportCredential failed, pkgName is %{public}s, credentialInfo is %{public}s",
390 pkgName.c_str(), GetAnonyString(credentialInfo).c_str());
391 return ERR_DM_INPUT_PARA_INVALID;
392 }
393 if (credentialMgr_== nullptr) {
394 LOGE("credentialMgr_ is nullptr");
395 return ERR_DM_POINT_NULL;
396 }
397 isCredentialType_.store(true);
398 return credentialMgr_->ImportCredential(pkgName, credentialInfo);
399 }
400
DeleteCredential(const std::string & pkgName,const std::string & deleteInfo)401 int32_t DeviceManagerServiceImpl::DeleteCredential(const std::string &pkgName, const std::string &deleteInfo)
402 {
403 if (pkgName.empty() || deleteInfo.empty()) {
404 LOGE("DeviceManagerServiceImpl::DeleteCredential failed, pkgName is %{public}s, deleteInfo is %{public}s",
405 pkgName.c_str(), GetAnonyString(deleteInfo).c_str());
406 return ERR_DM_INPUT_PARA_INVALID;
407 }
408 if (credentialMgr_== nullptr) {
409 LOGE("credentialMgr_ is nullptr");
410 return ERR_DM_POINT_NULL;
411 }
412 isCredentialType_.store(false);
413 return credentialMgr_->DeleteCredential(pkgName, deleteInfo);
414 }
415
MineRequestCredential(const std::string & pkgName,std::string & returnJsonStr)416 int32_t DeviceManagerServiceImpl::MineRequestCredential(const std::string &pkgName, std::string &returnJsonStr)
417 {
418 (void)pkgName;
419 if (mineHiChainConnector_->RequestCredential(returnJsonStr) != DM_OK) {
420 LOGE("failed to get device credential from hichain");
421 return ERR_DM_HICHAIN_CREDENTIAL_REQUEST_FAILED;
422 }
423 return DM_OK;
424 }
425
CheckCredential(const std::string & pkgName,const std::string & reqJsonStr,std::string & returnJsonStr)426 int32_t DeviceManagerServiceImpl::CheckCredential(const std::string &pkgName, const std::string &reqJsonStr,
427 std::string &returnJsonStr)
428 {
429 (void)pkgName;
430 if (reqJsonStr.empty()) {
431 LOGE("reqJsonStr is empty");
432 return ERR_DM_INPUT_PARA_INVALID;
433 }
434 if (mineHiChainConnector_->CheckCredential(reqJsonStr, returnJsonStr) != DM_OK) {
435 LOGE("failed to check devices credential status");
436 return ERR_DM_HICHAIN_CREDENTIAL_CHECK_FAILED;
437 }
438 return DM_OK;
439 }
440
ImportCredential(const std::string & pkgName,const std::string & reqJsonStr,std::string & returnJsonStr)441 int32_t DeviceManagerServiceImpl::ImportCredential(const std::string &pkgName, const std::string &reqJsonStr,
442 std::string &returnJsonStr)
443 {
444 (void)pkgName;
445 if (reqJsonStr.empty()) {
446 LOGE("reqJsonStr is empty");
447 return ERR_DM_INPUT_PARA_INVALID;
448 }
449 if (mineHiChainConnector_->ImportCredential(reqJsonStr, returnJsonStr) != DM_OK) {
450 LOGE("failed to import devices credential");
451 return ERR_DM_HICHAIN_CREDENTIAL_IMPORT_FAILED;
452 }
453 isCredentialType_.store(true);
454 return DM_OK;
455 }
456
DeleteCredential(const std::string & pkgName,const std::string & reqJsonStr,std::string & returnJsonStr)457 int32_t DeviceManagerServiceImpl::DeleteCredential(const std::string &pkgName, const std::string &reqJsonStr,
458 std::string &returnJsonStr)
459 {
460 (void)pkgName;
461 if (reqJsonStr.empty()) {
462 LOGE("reqJsonStr is empty");
463 return ERR_DM_INPUT_PARA_INVALID;
464 }
465 if (mineHiChainConnector_->DeleteCredential(reqJsonStr, returnJsonStr) != DM_OK) {
466 LOGE("failed to delete devices credential");
467 return ERR_DM_HICHAIN_CREDENTIAL_DELETE_FAILED;
468 }
469 isCredentialType_.store(false);
470 return DM_OK;
471 }
472
RegisterCredentialCallback(const std::string & pkgName)473 int32_t DeviceManagerServiceImpl::RegisterCredentialCallback(const std::string &pkgName)
474 {
475 if (pkgName.empty()) {
476 LOGE("RegisterCredentialCallback failed, pkgName is empty");
477 return ERR_DM_INPUT_PARA_INVALID;
478 }
479 if (credentialMgr_ == nullptr) {
480 LOGE("credentialMgr_ is nullptr");
481 return ERR_DM_POINT_NULL;
482 }
483 return credentialMgr_->RegisterCredentialCallback(pkgName);
484 }
485
UnRegisterCredentialCallback(const std::string & pkgName)486 int32_t DeviceManagerServiceImpl::UnRegisterCredentialCallback(const std::string &pkgName)
487 {
488 if (pkgName.empty()) {
489 LOGE("UnRegisterCredentialCallback failed, pkgName is empty");
490 return ERR_DM_INPUT_PARA_INVALID;
491 }
492 if (credentialMgr_== nullptr) {
493 LOGE("credentialMgr_ is nullptr");
494 return ERR_DM_POINT_NULL;
495 }
496 return credentialMgr_->UnRegisterCredentialCallback(pkgName);
497 }
498
RegisterUiStateCallback(const std::string & pkgName)499 int32_t DeviceManagerServiceImpl::RegisterUiStateCallback(const std::string &pkgName)
500 {
501 if (pkgName.empty()) {
502 LOGE("RegisterUiStateCallback failed, pkgName is empty");
503 return ERR_DM_INPUT_PARA_INVALID;
504 }
505 if (authMgr_ == nullptr) {
506 LOGE("authMgr_ is nullptr");
507 return ERR_DM_POINT_NULL;
508 }
509 return authMgr_->RegisterUiStateCallback(pkgName);
510 }
511
UnRegisterUiStateCallback(const std::string & pkgName)512 int32_t DeviceManagerServiceImpl::UnRegisterUiStateCallback(const std::string &pkgName)
513 {
514 if (pkgName.empty()) {
515 LOGE("RegisterUiStateCallback failed, pkgName is empty");
516 return ERR_DM_INPUT_PARA_INVALID;
517 }
518 if (authMgr_ == nullptr) {
519 LOGE("authMgr_ is nullptr");
520 return ERR_DM_POINT_NULL;
521 }
522 return authMgr_->UnRegisterUiStateCallback(pkgName);
523 }
524
PraseNotifyEventJson(const std::string & event,nlohmann::json & jsonObject)525 int32_t DeviceManagerServiceImpl::PraseNotifyEventJson(const std::string &event, nlohmann::json &jsonObject)
526 {
527 jsonObject = nlohmann::json::parse(event, nullptr, false);
528 if (jsonObject.is_discarded()) {
529 LOGE("event prase error.");
530 return ERR_DM_FAILED;
531 }
532 if ((!jsonObject.contains("extra")) || (!jsonObject["extra"].is_object())) {
533 LOGE("extra error");
534 return ERR_DM_FAILED;
535 }
536 if ((!jsonObject["extra"].contains("deviceId")) || (!jsonObject["extra"]["deviceId"].is_string())) {
537 LOGE("NotifyEvent deviceId invalid");
538 return ERR_DM_FAILED;
539 }
540 return DM_OK;
541 }
542
NotifyEvent(const std::string & pkgName,const int32_t eventId,const std::string & event)543 int32_t DeviceManagerServiceImpl::NotifyEvent(const std::string &pkgName, const int32_t eventId,
544 const std::string &event)
545 {
546 LOGI("NotifyEvent begin, pkgName : %{public}s, eventId : %{public}d", pkgName.c_str(), eventId);
547 if ((eventId <= DM_NOTIFY_EVENT_START) || (eventId >= DM_NOTIFY_EVENT_BUTT)) {
548 LOGE("NotifyEvent eventId invalid");
549 return ERR_DM_INPUT_PARA_INVALID;
550 }
551 if (eventId == DM_NOTIFY_EVENT_ONDEVICEREADY) {
552 nlohmann::json jsonObject;
553 if (PraseNotifyEventJson(event, jsonObject) != DM_OK) {
554 LOGE("NotifyEvent json invalid");
555 return ERR_DM_INPUT_PARA_INVALID;
556 }
557 std::string deviceId;
558 jsonObject["extra"]["deviceId"].get_to(deviceId);
559 if (deviceStateMgr_== nullptr) {
560 LOGE("deviceStateMgr_ is nullptr");
561 return ERR_DM_POINT_NULL;
562 }
563 if (deviceStateMgr_->ProcNotifyEvent(eventId, deviceId) != DM_OK) {
564 LOGE("NotifyEvent failed");
565 return ERR_DM_INPUT_PARA_INVALID;
566 };
567 }
568 return DM_OK;
569 }
570
GetGroupType(std::vector<DmDeviceInfo> & deviceList)571 int32_t DeviceManagerServiceImpl::GetGroupType(std::vector<DmDeviceInfo> &deviceList)
572 {
573 LOGI("GetGroupType begin");
574 if (softbusConnector_ == nullptr || hiChainConnector_ == nullptr) {
575 LOGE("softbusConnector_ or hiChainConnector_ is nullptr");
576 return ERR_DM_POINT_NULL;
577 }
578
579 for (auto it = deviceList.begin(); it != deviceList.end(); ++it) {
580 std::string udid = "";
581 int32_t ret = softbusConnector_->GetUdidByNetworkId(it->networkId, udid);
582 if (ret != DM_OK) {
583 LOGE("GetUdidByNetworkId failed ret: %{public}d", ret);
584 return ret;
585 }
586 std::string deviceId = softbusConnector_->GetDeviceUdidHashByUdid(udid);
587 if (memcpy_s(it->deviceId, DM_MAX_DEVICE_ID_LEN, deviceId.c_str(), deviceId.length()) != 0) {
588 LOGE("get deviceId: %{public}s failed", GetAnonyString(deviceId).c_str());
589 }
590 it->authForm = hiChainConnector_->GetGroupType(udid);
591 }
592 return DM_OK;
593 }
594
GetUdidHashByNetWorkId(const char * networkId,std::string & deviceId)595 int32_t DeviceManagerServiceImpl::GetUdidHashByNetWorkId(const char *networkId, std::string &deviceId)
596 {
597 if (softbusConnector_ == nullptr || hiChainConnector_ == nullptr) {
598 LOGE("softbusConnector_ or hiChainConnector_ is nullptr");
599 return ERR_DM_POINT_NULL;
600 }
601 std::string udid = "";
602 int32_t ret = softbusConnector_->GetUdidByNetworkId(networkId, udid);
603 if (ret != DM_OK) {
604 LOGE("GetUdidByNetworkId failed ret: %{public}d", ret);
605 return ret;
606 }
607 deviceId = softbusConnector_->GetDeviceUdidHashByUdid(udid);
608 return DM_OK;
609 }
610
ImportAuthCode(const std::string & pkgName,const std::string & authCode)611 int32_t DeviceManagerServiceImpl::ImportAuthCode(const std::string &pkgName, const std::string &authCode)
612 {
613 if (pkgName.empty() || authCode.empty()) {
614 LOGE("ImportAuthCode failed, pkgName or authCode is empty");
615 return ERR_DM_INPUT_PARA_INVALID;
616 }
617
618 return authMgr_->ImportAuthCode(pkgName, authCode);
619 }
620
ExportAuthCode(std::string & authCode)621 int32_t DeviceManagerServiceImpl::ExportAuthCode(std::string &authCode)
622 {
623 int32_t ret = authMgr_->GeneratePincode();
624 authCode = std::to_string(ret);
625 LOGI("ExportAuthCode success, authCode: %{public}s.", GetAnonyString(authCode).c_str());
626 return DM_OK;
627 }
628
BindTarget(const std::string & pkgName,const PeerTargetId & targetId,const std::map<std::string,std::string> & bindParam)629 int32_t DeviceManagerServiceImpl::BindTarget(const std::string &pkgName, const PeerTargetId &targetId,
630 const std::map<std::string, std::string> &bindParam)
631 {
632 if (pkgName.empty()) {
633 LOGE("BindTarget failed, pkgName is empty");
634 return ERR_DM_INPUT_PARA_INVALID;
635 }
636 return authMgr_->BindTarget(pkgName, targetId, bindParam);
637 }
638
PutIdenticalAccountToAcl(std::string requestDeviceId,std::string trustDeviceId)639 void DeviceManagerServiceImpl::PutIdenticalAccountToAcl(std::string requestDeviceId, std::string trustDeviceId)
640 {
641 LOGI("DeviceManagerServiceImpl::PutIdenticalAccountAcl start.");
642 char localDeviceId[DEVICE_UUID_LENGTH] = {0};
643 Crypto::GetUdidHash(requestDeviceId, reinterpret_cast<uint8_t *>(localDeviceId));
644 std::string localUdidHash = static_cast<std::string>(localDeviceId);
645 DmAclInfo aclInfo;
646 aclInfo.bindType = IDENTICAL_ACCOUNT;
647 aclInfo.trustDeviceId = trustDeviceId;
648 aclInfo.authenticationType = ALLOW_AUTH_ALWAYS;
649 aclInfo.deviceIdHash = localUdidHash;
650 DmAccesser accesser;
651 accesser.requestUserId = MultipleUserConnector::GetCurrentAccountUserID();
652 accesser.requestAccountId = MultipleUserConnector::GetOhosAccountId();
653 MultipleUserConnector::SetSwitchOldUserId(accesser.requestUserId);
654 MultipleUserConnector::SetSwitchOldAccountId(accesser.requestAccountId);
655 accesser.requestDeviceId = requestDeviceId;
656 DmAccessee accessee;
657 accessee.trustDeviceId = trustDeviceId;
658 DeviceProfileConnector::GetInstance().PutAccessControlList(aclInfo, accesser, accessee);
659 }
660
DpAclAdd(const std::string & udid)661 int32_t DeviceManagerServiceImpl::DpAclAdd(const std::string &udid)
662 {
663 LOGI("DeviceManagerServiceImpl DpAclAdd start.");
664 MultipleUserConnector::SetSwitchOldUserId(MultipleUserConnector::GetCurrentAccountUserID());
665 MultipleUserConnector::SetSwitchOldAccountId(MultipleUserConnector::GetOhosAccountId());
666 if (deviceStateMgr_->CheckIsOnline(udid)) {
667 LOGI("DeviceManagerServiceImpl DpAclAdd identical account and online");
668 deviceStateMgr_->OnDeviceOnline(udid, DmAuthForm::IDENTICAL_ACCOUNT);
669 }
670 LOGI("DeviceManagerServiceImpl::DpAclAdd completed");
671 return DM_OK;
672 }
673
IsSameAccount(const std::string & udid)674 int32_t DeviceManagerServiceImpl::IsSameAccount(const std::string &udid)
675 {
676 if (udid.empty()) {
677 LOGE("DeviceManagerServiceImpl::IsSameAccount error: udid: %{public}s", GetAnonyString(udid).c_str());
678 return ERR_DM_INPUT_PARA_INVALID;
679 }
680
681 return DeviceProfileConnector::GetInstance().IsSameAccount(udid);
682 }
683
GetAppTrustDeviceIdList(std::string pkgname)684 std::unordered_map<std::string, DmAuthForm> DeviceManagerServiceImpl::GetAppTrustDeviceIdList(
685 std::string pkgname)
686 {
687 char localDeviceId[DEVICE_UUID_LENGTH];
688 GetDevUdid(localDeviceId, DEVICE_UUID_LENGTH);
689 std::string deviceId = reinterpret_cast<char *>(localDeviceId);
690 return DeviceProfileConnector::GetInstance().GetAppTrustDeviceList(pkgname, deviceId);
691 }
692
OnUnbindSessionOpened(int32_t socket,PeerSocketInfo info)693 void DeviceManagerServiceImpl::OnUnbindSessionOpened(int32_t socket, PeerSocketInfo info)
694 {
695 SoftbusSession::OnUnbindSessionOpened(socket, info);
696 }
697
OnUnbindSessionCloseed(int32_t socket)698 void DeviceManagerServiceImpl::OnUnbindSessionCloseed(int32_t socket)
699 {
700 SoftbusSession::OnSessionClosed(socket);
701 }
702
OnUnbindBytesReceived(int32_t socket,const void * data,uint32_t dataLen)703 void DeviceManagerServiceImpl::OnUnbindBytesReceived(int32_t socket, const void *data, uint32_t dataLen)
704 {
705 SoftbusSession::OnBytesReceived(socket, data, dataLen);
706 }
707
LoadHardwareFwkService()708 void DeviceManagerServiceImpl::LoadHardwareFwkService()
709 {
710 DmDistributedHardwareLoad::GetInstance().LoadDistributedHardwareFwk();
711 }
712
AccountCommonEventCallback(int32_t userId,std::string commonEventType)713 void DeviceManagerServiceImpl::AccountCommonEventCallback(int32_t userId, std::string commonEventType)
714 {
715 if (commonEventType == EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED ||
716 commonEventType == EventFwk::CommonEventSupport::COMMON_EVENT_HWID_LOGOUT) {
717 authMgr_->CommonEventCallback(userId, commonEventType);
718 LOGI("DeviceManagerServiceImpl::account event: %{public}s, userId: %{public}s",
719 commonEventType.c_str(), GetAnonyInt32(userId).c_str());
720 return;
721 }
722 LOGI("DeviceManagerServiceImpl::AccountCommonEventCallback error.");
723 }
724
ScreenCommonEventCallback(std::string commonEventType)725 void DeviceManagerServiceImpl::ScreenCommonEventCallback(std::string commonEventType)
726 {
727 if (commonEventType == EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_LOCKED) {
728 LOGI("DeviceManagerServiceImpl::ScreenCommonEventCallback on screen locked.");
729 authMgr_->OnScreenLocked();
730 return;
731 }
732 LOGI("DeviceManagerServiceImpl::ScreenCommonEventCallback error.");
733 }
734
CheckIsSameAccount(const DmAccessCaller & caller,const std::string & srcUdid,const DmAccessCallee & callee,const std::string & sinkUdid)735 int32_t DeviceManagerServiceImpl::CheckIsSameAccount(const DmAccessCaller &caller, const std::string &srcUdid,
736 const DmAccessCallee &callee, const std::string &sinkUdid)
737 {
738 return DeviceProfileConnector::GetInstance().CheckIsSameAccount(caller, srcUdid, callee, sinkUdid);
739 }
740
CheckAccessControl(const DmAccessCaller & caller,const std::string & srcUdid,const DmAccessCallee & callee,const std::string & sinkUdid)741 int32_t DeviceManagerServiceImpl::CheckAccessControl(const DmAccessCaller &caller, const std::string &srcUdid,
742 const DmAccessCallee &callee, const std::string &sinkUdid)
743 {
744 return DeviceProfileConnector::GetInstance().CheckAccessControl(caller, srcUdid, callee, sinkUdid);
745 }
746
HandleDeviceNotTrust(const std::string & udid)747 void DeviceManagerServiceImpl::HandleDeviceNotTrust(const std::string &udid)
748 {
749 LOGI("DeviceManagerServiceImpl::HandleDeviceNotTrust udid: %{public}s.", GetAnonyString(udid).c_str());
750 if (udid.empty()) {
751 LOGE("HandleDeviceNotTrust udid is empty.");
752 return;
753 }
754 CHECK_NULL_VOID(authMgr_);
755 authMgr_->HandleDeviceNotTrust(udid);
756 }
757
HandleIdentAccountLogout(const std::string & udid,int32_t userId,const std::string & accountId)758 void DeviceManagerServiceImpl::HandleIdentAccountLogout(const std::string &udid, int32_t userId,
759 const std::string &accountId)
760 {
761 LOGI("Udid %{public}s, userId %{public}d, accountId %{public}s.", GetAnonyString(udid).c_str(),
762 userId, GetAnonyString(accountId).c_str());
763 char localUdidTemp[DEVICE_UUID_LENGTH] = {0};
764 GetDevUdid(localUdidTemp, DEVICE_UUID_LENGTH);
765 std::string localUdid = std::string(localUdidTemp);
766 DeviceProfileConnector::GetInstance().DeleteAclForAccountLogOut(localUdid, userId, udid);
767 CHECK_NULL_VOID(hiChainConnector_);
768 authMgr_->DeleteGroup(DM_PKG_NAME, udid);
769 }
770
HandleUserRemoved(int32_t preUserId)771 void DeviceManagerServiceImpl::HandleUserRemoved(int32_t preUserId)
772 {
773 LOGI("PreUserId %{public}d.", preUserId);
774 DeviceProfileConnector::GetInstance().DeleteAclForUserRemoved(preUserId);
775 CHECK_NULL_VOID(hiChainConnector_);
776 hiChainConnector_->DeleteAllGroup(preUserId);
777 }
778
GetDeviceIdAndBindType(int32_t userId,const std::string & accountId)779 std::map<std::string, int32_t> DeviceManagerServiceImpl::GetDeviceIdAndBindType(int32_t userId,
780 const std::string &accountId)
781 {
782 char localUdidTemp[DEVICE_UUID_LENGTH] = {0};
783 GetDevUdid(localUdidTemp, DEVICE_UUID_LENGTH);
784 std::string localUdid = std::string(localUdidTemp);
785 return DeviceProfileConnector::GetInstance().GetDeviceIdAndBindType(userId, accountId, localUdid);
786 }
787
HandleDeviceScreenStatusChange(DmDeviceInfo & devInfo)788 void DeviceManagerServiceImpl::HandleDeviceScreenStatusChange(DmDeviceInfo &devInfo)
789 {
790 LOGI("In");
791 CHECK_NULL_VOID(deviceStateMgr_);
792 CHECK_NULL_VOID(softbusConnector_);
793 std::string trustDeviceId = "";
794 if (softbusConnector_->GetUdidByNetworkId(devInfo.networkId, trustDeviceId) != DM_OK) {
795 LOGE("get udid failed.");
796 return;
797 }
798 std::string udidHash = softbusConnector_->GetDeviceUdidHashByUdid(trustDeviceId);
799 if (memcpy_s(devInfo.deviceId, DM_MAX_DEVICE_ID_LEN, udidHash.c_str(), udidHash.length()) != 0) {
800 LOGE("get deviceId: %{public}s failed", GetAnonyString(udidHash).c_str());
801 return;
802 }
803 char localUdid[DEVICE_UUID_LENGTH] = {0};
804 GetDevUdid(localUdid, DEVICE_UUID_LENGTH);
805 std::string requestDeviceId = static_cast<std::string>(localUdid);
806 uint32_t bindType = DeviceProfileConnector::GetInstance().CheckBindType(trustDeviceId, requestDeviceId);
807 LOGI("bind type is %{public}d.", bindType);
808 if (bindType == INVALIED_TYPE) {
809 return;
810 } else if (bindType == IDENTICAL_ACCOUNT_TYPE || bindType == DEVICE_PEER_TO_PEER_TYPE ||
811 bindType == DEVICE_ACROSS_ACCOUNT_TYPE) {
812 softbusConnector_->ClearPkgName();
813 LOGI("networkId: %{public}s", GetAnonyString(devInfo.networkId).c_str());
814 } else if (bindType == APP_PEER_TO_PEER_TYPE || bindType == APP_ACROSS_ACCOUNT_TYPE) {
815 std::vector<std::string> pkgNameVec =
816 DeviceProfileConnector::GetInstance().GetPkgNameFromAcl(requestDeviceId, trustDeviceId);
817 if (pkgNameVec.size() == 0) {
818 LOGI("not need report pkgname");
819 return;
820 }
821 softbusConnector_->SetPkgNameVec(pkgNameVec);
822 }
823 deviceStateMgr_->HandleDeviceScreenStatusChange(devInfo);
824 }
825
HandleCredentialAuthStatus(const std::string & proofInfo,uint16_t deviceTypeId,int32_t errcode)826 void DeviceManagerServiceImpl::HandleCredentialAuthStatus(const std::string &proofInfo, uint16_t deviceTypeId,
827 int32_t errcode)
828 {
829 CHECK_NULL_VOID(deviceStateMgr_);
830 deviceStateMgr_->HandleCredentialAuthStatus(proofInfo, deviceTypeId, errcode);
831 }
832
CreateDMServiceObject(void)833 extern "C" IDeviceManagerServiceImpl *CreateDMServiceObject(void)
834 {
835 return new DeviceManagerServiceImpl;
836 }
837 } // namespace DistributedHardware
838 } // namespace OHOS
839