• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2025 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 "app_manager.h"
21 #include "dm_anonymous.h"
22 #include "dm_constants.h"
23 #include "dm_crypto.h"
24 #include "dm_distributed_hardware_load.h"
25 #include "dm_log.h"
26 #include "dm_radar_helper.h"
27 #include "dm_softbus_cache.h"
28 #include "multiple_user_connector.h"
29 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
30 #include "dm_common_event_manager.h"
31 #include "parameter.h"
32 #include "common_event_support.h"
33 using namespace OHOS::EventFwk;
34 #endif
35 
36 namespace OHOS {
37 namespace DistributedHardware {
38 // One year 365 * 24 * 60 * 60
39 constexpr int32_t MAX_ALWAYS_ALLOW_SECONDS = 31536000;
40 
DeviceManagerServiceImpl()41 DeviceManagerServiceImpl::DeviceManagerServiceImpl()
42 {
43     LOGI("DeviceManagerServiceImpl constructor");
44 }
45 
~DeviceManagerServiceImpl()46 DeviceManagerServiceImpl::~DeviceManagerServiceImpl()
47 {
48     LOGI("DeviceManagerServiceImpl destructor");
49 }
50 
Initialize(const std::shared_ptr<IDeviceManagerServiceListener> & listener)51 int32_t DeviceManagerServiceImpl::Initialize(const std::shared_ptr<IDeviceManagerServiceListener> &listener)
52 {
53     LOGI("DeviceManagerServiceImpl Initialize");
54     if (softbusConnector_ == nullptr) {
55         softbusConnector_ = std::make_shared<SoftbusConnector>();
56     }
57     if (hiChainConnector_ == nullptr) {
58         hiChainConnector_ = std::make_shared<HiChainConnector>();
59     }
60     if (mineHiChainConnector_ == nullptr) {
61         mineHiChainConnector_ = std::make_shared<MineHiChainConnector>();
62     }
63     if (hiChainAuthConnector_ == nullptr) {
64         hiChainAuthConnector_ = std::make_shared<HiChainAuthConnector>();
65     }
66     if (deviceStateMgr_ == nullptr) {
67         deviceStateMgr_ = std::make_shared<DmDeviceStateManager>(softbusConnector_, listener,
68                                                                  hiChainConnector_, hiChainAuthConnector_);
69         deviceStateMgr_->RegisterSoftbusStateCallback();
70     }
71     if (authMgr_ == nullptr) {
72         authMgr_ = std::make_shared<DmAuthManager>(softbusConnector_, hiChainConnector_, listener,
73             hiChainAuthConnector_);
74         softbusConnector_->RegisterConnectorCallback(authMgr_);
75         softbusConnector_->GetSoftbusSession()->RegisterSessionCallback(authMgr_);
76         hiChainConnector_->RegisterHiChainCallback(authMgr_);
77         hiChainAuthConnector_->RegisterHiChainAuthCallback(authMgr_);
78     }
79     if (credentialMgr_ == nullptr) {
80         credentialMgr_ = std::make_shared<DmCredentialManager>(hiChainConnector_, listener);
81     }
82     if (dpInitedCallback_ == nullptr) {
83         dpInitedCallback_ = sptr<DpInitedCallback>(new DpInitedCallback());
84         DeviceProfileConnector::GetInstance().SubscribeDeviceProfileInited(dpInitedCallback_);
85     }
86     listener_ = listener;
87     LOGI("Init success, singleton initialized");
88     return DM_OK;
89 }
90 
Release()91 void DeviceManagerServiceImpl::Release()
92 {
93     LOGI("DeviceManagerServiceImpl Release");
94 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
95     commonEventManager_ = nullptr;
96 #endif
97     softbusConnector_->UnRegisterConnectorCallback();
98     softbusConnector_->GetSoftbusSession()->UnRegisterSessionCallback();
99     hiChainConnector_->UnRegisterHiChainCallback();
100     authMgr_ = nullptr;
101     deviceStateMgr_ = nullptr;
102     softbusConnector_ = nullptr;
103     abilityMgr_ = nullptr;
104     hiChainConnector_ = nullptr;
105     DeviceProfileConnector::GetInstance().UnSubscribeDeviceProfileInited();
106     dpInitedCallback_ = nullptr;
107 }
108 
UnAuthenticateDevice(const std::string & pkgName,const std::string & udid,int32_t bindLevel)109 int32_t DeviceManagerServiceImpl::UnAuthenticateDevice(const std::string &pkgName, const std::string &udid,
110     int32_t bindLevel)
111 {
112     if (pkgName.empty() || udid.empty()) {
113         LOGE("DeviceManagerServiceImpl::UnAuthenticateDevice failed, pkgName is %{public}s, udid is %{public}s",
114             pkgName.c_str(), GetAnonyString(udid).c_str());
115         return ERR_DM_INPUT_PARA_INVALID;
116     }
117     return authMgr_->UnAuthenticateDevice(pkgName, udid, bindLevel);
118 }
119 
StopAuthenticateDevice(const std::string & pkgName)120 int32_t DeviceManagerServiceImpl::StopAuthenticateDevice(const std::string &pkgName)
121 {
122     if (pkgName.empty()) {
123         LOGE("DeviceManagerServiceImpl::StopAuthenticateDevice failed");
124         return ERR_DM_INPUT_PARA_INVALID;
125     }
126     return authMgr_->StopAuthenticateDevice(pkgName);
127 }
128 
UnBindDevice(const std::string & pkgName,const std::string & udid,int32_t bindLevel)129 int32_t DeviceManagerServiceImpl::UnBindDevice(const std::string &pkgName, const std::string &udid,
130     int32_t bindLevel)
131 {
132     if (pkgName.empty() || udid.empty()) {
133         LOGE("DeviceManagerServiceImpl::UnBindDevice failed, pkgName is %{public}s, udid is %{public}s",
134             pkgName.c_str(), GetAnonyString(udid).c_str());
135         return ERR_DM_INPUT_PARA_INVALID;
136     }
137     std::string extra = "";
138     return authMgr_->UnBindDevice(pkgName, udid, bindLevel, extra);
139 }
140 
UnBindDevice(const std::string & pkgName,const std::string & udid,int32_t bindLevel,const std::string & extra)141 int32_t DeviceManagerServiceImpl::UnBindDevice(const std::string &pkgName, const std::string &udid,
142     int32_t bindLevel, const std::string &extra)
143 {
144     if (pkgName.empty() || udid.empty()) {
145         LOGE("DeviceManagerServiceImpl::UnBindDevice failed, pkgName is %{public}s, udid is %{public}s",
146             pkgName.c_str(), GetAnonyString(udid).c_str());
147         return ERR_DM_INPUT_PARA_INVALID;
148     }
149     return authMgr_->UnBindDevice(pkgName, udid, bindLevel, extra);
150 }
151 
SetUserOperation(std::string & pkgName,int32_t action,const std::string & params)152 int32_t DeviceManagerServiceImpl::SetUserOperation(std::string &pkgName, int32_t action,
153     const std::string &params)
154 {
155     if (pkgName.empty() || params.empty()) {
156         LOGE("DeviceManagerServiceImpl::SetUserOperation error: Invalid parameter, pkgName: %{public}s, extra:"
157             "%{public}s", pkgName.c_str(), params.c_str());
158         return ERR_DM_INPUT_PARA_INVALID;
159     }
160     if (authMgr_ != nullptr) {
161         authMgr_->OnUserOperation(action, params);
162     }
163     return DM_OK;
164 }
165 
HandleOffline(DmDeviceState devState,DmDeviceInfo & devInfo)166 void DeviceManagerServiceImpl::HandleOffline(DmDeviceState devState, DmDeviceInfo &devInfo)
167 {
168     LOGI("DeviceManagerServiceImpl::HandleOffline");
169     std::string trustDeviceId = deviceStateMgr_->GetUdidByNetWorkId(std::string(devInfo.networkId));
170     LOGI("deviceStateMgr Udid: %{public}s", GetAnonyString(trustDeviceId).c_str());
171     if (trustDeviceId == "") {
172         LOGE("HandleOffline not get udid in deviceStateMgr.");
173         return;
174     }
175     std::string udisHash = softbusConnector_->GetDeviceUdidHashByUdid(trustDeviceId);
176     if (memcpy_s(devInfo.deviceId, DM_MAX_DEVICE_ID_LEN, udisHash.c_str(), udisHash.length()) != 0) {
177         LOGE("get deviceId: %{public}s failed", GetAnonyString(udisHash).c_str());
178         return;
179     }
180     char localUdid[DEVICE_UUID_LENGTH] = {0};
181     GetDevUdid(localUdid, DEVICE_UUID_LENGTH);
182     std::string requestDeviceId = std::string(localUdid);
183     std::map<int32_t, int32_t> userIdAndBindLevel =
184         DeviceProfileConnector::GetInstance().GetUserIdAndBindLevel(requestDeviceId, trustDeviceId);
185     ProcessInfo processInfo;
186     processInfo.pkgName = std::string(DM_PKG_NAME);
187     processInfo.userId = MultipleUserConnector::GetFirstForegroundUserId();
188     if (userIdAndBindLevel.empty() || userIdAndBindLevel.find(processInfo.userId) == userIdAndBindLevel.end()) {
189         userIdAndBindLevel[processInfo.userId] = INVALIED_TYPE;
190     }
191     for (const auto &item : userIdAndBindLevel) {
192         if (item.second == INVALIED_TYPE) {
193             LOGI("The offline device is identical account bind type.");
194             devInfo.authForm = DmAuthForm::IDENTICAL_ACCOUNT;
195             processInfo.userId = item.first;
196             softbusConnector_->SetProcessInfo(processInfo);
197         } else if (item.second == DEVICE) {
198             LOGI("The offline device is device bind type.");
199             devInfo.authForm = DmAuthForm::PEER_TO_PEER;
200             processInfo.userId = item.first;
201             softbusConnector_->SetProcessInfo(processInfo);
202         } else if (item.second == SERVICE || item.second == APP) {
203             LOGI("The offline device is APP_PEER_TO_PEER_TYPE bind type.");
204             std::vector<ProcessInfo> processInfoVec =
205                 DeviceProfileConnector::GetInstance().GetProcessInfoFromAclByUserId(requestDeviceId, trustDeviceId,
206                     item.first);
207             softbusConnector_->SetProcessInfoVec(processInfoVec);
208         }
209         deviceStateMgr_->HandleDeviceStatusChange(devState, devInfo);
210     }
211 }
212 
HandleOnline(DmDeviceState devState,DmDeviceInfo & devInfo)213 void DeviceManagerServiceImpl::HandleOnline(DmDeviceState devState, DmDeviceInfo &devInfo)
214 {
215     LOGI("DeviceManagerServiceImpl::HandleOnline");
216     std::string trustDeviceId = "";
217     if (softbusConnector_->GetUdidByNetworkId(devInfo.networkId, trustDeviceId) != DM_OK) {
218         LOGE("HandleOnline get udid failed.");
219         return;
220     }
221     std::string udisHash = softbusConnector_->GetDeviceUdidHashByUdid(trustDeviceId);
222     if (memcpy_s(devInfo.deviceId, DM_MAX_DEVICE_ID_LEN, udisHash.c_str(), udisHash.length()) != 0) {
223         LOGE("get deviceId: %{public}s failed", GetAnonyString(udisHash).c_str());
224         return;
225     }
226     char localUdid[DEVICE_UUID_LENGTH] = {0};
227     GetDevUdid(localUdid, DEVICE_UUID_LENGTH);
228     std::string requestDeviceId = std::string(localUdid);
229     uint32_t bindType = DeviceProfileConnector::GetInstance().CheckBindType(trustDeviceId, requestDeviceId);
230     LOGI("The online device bind type is %{public}d.", bindType);
231     ProcessInfo processInfo;
232     processInfo.pkgName = std::string(DM_PKG_NAME);
233     processInfo.userId = MultipleUserConnector::GetFirstForegroundUserId();
234     if (bindType == IDENTICAL_ACCOUNT_TYPE) {
235         devInfo.authForm = DmAuthForm::IDENTICAL_ACCOUNT;
236         softbusConnector_->SetProcessInfo(processInfo);
237     } else if (bindType == DEVICE_PEER_TO_PEER_TYPE) {
238         devInfo.authForm = DmAuthForm::PEER_TO_PEER;
239         softbusConnector_->SetProcessInfo(processInfo);
240     } else if (bindType == DEVICE_ACROSS_ACCOUNT_TYPE) {
241         devInfo.authForm = DmAuthForm::ACROSS_ACCOUNT;
242         softbusConnector_->SetProcessInfo(processInfo);
243     } else if (bindType == APP_PEER_TO_PEER_TYPE) {
244         std::vector<ProcessInfo> processInfoVec =
245             DeviceProfileConnector::GetInstance().GetProcessInfoFromAclByUserId(requestDeviceId, trustDeviceId,
246                 MultipleUserConnector::GetFirstForegroundUserId());
247         softbusConnector_->SetProcessInfoVec(processInfoVec);
248         devInfo.authForm = DmAuthForm::PEER_TO_PEER;
249     } else if (bindType == APP_ACROSS_ACCOUNT_TYPE) {
250         std::vector<ProcessInfo> processInfoVec =
251             DeviceProfileConnector::GetInstance().GetProcessInfoFromAclByUserId(requestDeviceId, trustDeviceId,
252                 MultipleUserConnector::GetFirstForegroundUserId());
253         softbusConnector_->SetProcessInfoVec(processInfoVec);
254         devInfo.authForm = DmAuthForm::ACROSS_ACCOUNT;
255     }
256     LOGI("DeviceManagerServiceImpl::HandleOnline success devInfo auform %{public}d.", devInfo.authForm);
257     deviceStateMgr_->HandleDeviceStatusChange(devState, devInfo);
258 }
259 
HandleDeviceStatusChange(DmDeviceState devState,DmDeviceInfo & devInfo)260 void DeviceManagerServiceImpl::HandleDeviceStatusChange(DmDeviceState devState, DmDeviceInfo &devInfo)
261 {
262     LOGI("DeviceManagerServiceImpl::HandleDeviceStatusChange start, devState = %{public}d.", devState);
263     if (deviceStateMgr_ == nullptr) {
264         LOGE("deviceStateMgr_ is nullpter!");
265         return;
266     }
267     if (devState == DEVICE_STATE_ONLINE) {
268         HandleOnline(devState, devInfo);
269     } else if (devState == DEVICE_STATE_OFFLINE) {
270         HandleOffline(devState, devInfo);
271     } else {
272         std::string udiddHash = GetUdidHashByNetworkId(devInfo.networkId);
273         if (memcpy_s(devInfo.deviceId, DM_MAX_DEVICE_ID_LEN, udiddHash.c_str(), udiddHash.length()) != 0) {
274             LOGE("get deviceId: %{public}s failed", GetAnonyString(udiddHash).c_str());
275             return;
276         }
277         ProcessInfo processInfo;
278         processInfo.pkgName = std::string(DM_PKG_NAME);
279         processInfo.userId = MultipleUserConnector::GetFirstForegroundUserId();
280         softbusConnector_->SetProcessInfo(processInfo);
281         deviceStateMgr_->HandleDeviceStatusChange(devState, devInfo);
282     }
283 }
284 
GetUdidHashByNetworkId(const std::string & networkId)285 std::string DeviceManagerServiceImpl::GetUdidHashByNetworkId(const std::string &networkId)
286 {
287     if (softbusConnector_ == nullptr) {
288         LOGE("softbusConnector_ is nullpter!");
289         return "";
290     }
291     std::string udid = "";
292     int32_t ret = softbusConnector_->GetUdidByNetworkId(networkId.c_str(), udid);
293     if (ret != DM_OK) {
294         LOGE("GetUdidByNetworkId failed ret: %{public}d", ret);
295         return "";
296     }
297     return softbusConnector_->GetDeviceUdidHashByUdid(udid);
298 }
299 
OnSessionOpened(int sessionId,int result)300 int DeviceManagerServiceImpl::OnSessionOpened(int sessionId, int result)
301 {
302     std::string peerUdid = "";
303     softbusConnector_->GetSoftbusSession()->GetPeerDeviceId(sessionId, peerUdid);
304     struct RadarInfo info = {
305         .funcName = "OnSessionOpened",
306         .stageRes = static_cast<int32_t>(StageRes::STAGE_SUCC),
307         .isTrust = static_cast<int32_t>(TrustStatus::NOT_TRUST),
308         .peerUdid = peerUdid,
309         .channelId = sessionId,
310     };
311     if (!DmRadarHelper::GetInstance().ReportAuthSessionOpenCb(info)) {
312         LOGE("ReportAuthSessionOpenCb failed");
313     }
314     return SoftbusSession::OnSessionOpened(sessionId, result);
315 }
316 
OnSessionClosed(int sessionId)317 void DeviceManagerServiceImpl::OnSessionClosed(int sessionId)
318 {
319     SoftbusSession::OnSessionClosed(sessionId);
320 }
321 
OnBytesReceived(int sessionId,const void * data,unsigned int dataLen)322 void DeviceManagerServiceImpl::OnBytesReceived(int sessionId, const void *data, unsigned int dataLen)
323 {
324     SoftbusSession::OnBytesReceived(sessionId, data, dataLen);
325 }
326 
RequestCredential(const std::string & reqJsonStr,std::string & returnJsonStr)327 int32_t DeviceManagerServiceImpl::RequestCredential(const std::string &reqJsonStr, std::string &returnJsonStr)
328 {
329     if (reqJsonStr.empty()) {
330         LOGE("reqJsonStr is empty");
331         return ERR_DM_INPUT_PARA_INVALID;
332     }
333     if (credentialMgr_== nullptr) {
334         LOGE("credentialMgr_ is nullptr");
335         return ERR_DM_POINT_NULL;
336     }
337     return credentialMgr_->RequestCredential(reqJsonStr, returnJsonStr);
338 }
339 
ImportCredential(const std::string & pkgName,const std::string & credentialInfo)340 int32_t DeviceManagerServiceImpl::ImportCredential(const std::string &pkgName, const std::string &credentialInfo)
341 {
342     if (pkgName.empty() || credentialInfo.empty()) {
343         LOGE("DeviceManagerServiceImpl::ImportCredential failed, pkgName is %{public}s, credentialInfo is %{public}s",
344             pkgName.c_str(), GetAnonyString(credentialInfo).c_str());
345         return ERR_DM_INPUT_PARA_INVALID;
346     }
347     if (credentialMgr_== nullptr) {
348         LOGE("credentialMgr_ is nullptr");
349         return ERR_DM_POINT_NULL;
350     }
351     isCredentialType_.store(true);
352     return credentialMgr_->ImportCredential(pkgName, credentialInfo);
353 }
354 
DeleteCredential(const std::string & pkgName,const std::string & deleteInfo)355 int32_t DeviceManagerServiceImpl::DeleteCredential(const std::string &pkgName, const std::string &deleteInfo)
356 {
357     if (pkgName.empty() || deleteInfo.empty()) {
358         LOGE("DeviceManagerServiceImpl::DeleteCredential failed, pkgName is %{public}s, deleteInfo is %{public}s",
359             pkgName.c_str(), GetAnonyString(deleteInfo).c_str());
360         return ERR_DM_INPUT_PARA_INVALID;
361     }
362     if (credentialMgr_== nullptr) {
363         LOGE("credentialMgr_ is nullptr");
364         return ERR_DM_POINT_NULL;
365     }
366     isCredentialType_.store(false);
367     return credentialMgr_->DeleteCredential(pkgName, deleteInfo);
368 }
369 
MineRequestCredential(const std::string & pkgName,std::string & returnJsonStr)370 int32_t DeviceManagerServiceImpl::MineRequestCredential(const std::string &pkgName, std::string &returnJsonStr)
371 {
372     (void)pkgName;
373     if (mineHiChainConnector_->RequestCredential(returnJsonStr) != DM_OK) {
374         LOGE("failed to get device credential from hichain");
375         return ERR_DM_HICHAIN_CREDENTIAL_REQUEST_FAILED;
376     }
377     return DM_OK;
378 }
379 
CheckCredential(const std::string & pkgName,const std::string & reqJsonStr,std::string & returnJsonStr)380 int32_t DeviceManagerServiceImpl::CheckCredential(const std::string &pkgName, const std::string &reqJsonStr,
381     std::string &returnJsonStr)
382 {
383     (void)pkgName;
384     if (reqJsonStr.empty()) {
385         LOGE("reqJsonStr is empty");
386         return ERR_DM_INPUT_PARA_INVALID;
387     }
388     if (mineHiChainConnector_->CheckCredential(reqJsonStr, returnJsonStr) != DM_OK) {
389         LOGE("failed to check devices credential status");
390         return ERR_DM_HICHAIN_CREDENTIAL_CHECK_FAILED;
391     }
392     return DM_OK;
393 }
394 
ImportCredential(const std::string & pkgName,const std::string & reqJsonStr,std::string & returnJsonStr)395 int32_t DeviceManagerServiceImpl::ImportCredential(const std::string &pkgName, const std::string &reqJsonStr,
396     std::string &returnJsonStr)
397 {
398     (void)pkgName;
399     if (reqJsonStr.empty()) {
400         LOGE("reqJsonStr is empty");
401         return ERR_DM_INPUT_PARA_INVALID;
402     }
403     if (mineHiChainConnector_->ImportCredential(reqJsonStr, returnJsonStr) != DM_OK) {
404         LOGE("failed to import devices credential");
405         return ERR_DM_HICHAIN_CREDENTIAL_IMPORT_FAILED;
406     }
407     isCredentialType_.store(true);
408     return DM_OK;
409 }
410 
DeleteCredential(const std::string & pkgName,const std::string & reqJsonStr,std::string & returnJsonStr)411 int32_t DeviceManagerServiceImpl::DeleteCredential(const std::string &pkgName, const std::string &reqJsonStr,
412     std::string &returnJsonStr)
413 {
414     (void)pkgName;
415     if (reqJsonStr.empty()) {
416         LOGE("reqJsonStr is empty");
417         return ERR_DM_INPUT_PARA_INVALID;
418     }
419     if (mineHiChainConnector_->DeleteCredential(reqJsonStr, returnJsonStr) != DM_OK) {
420         LOGE("failed to delete devices credential");
421         return ERR_DM_HICHAIN_CREDENTIAL_DELETE_FAILED;
422     }
423     isCredentialType_.store(false);
424     return DM_OK;
425 }
426 
RegisterCredentialCallback(const std::string & pkgName)427 int32_t DeviceManagerServiceImpl::RegisterCredentialCallback(const std::string &pkgName)
428 {
429     if (pkgName.empty()) {
430         LOGE("RegisterCredentialCallback failed, pkgName is empty");
431         return ERR_DM_INPUT_PARA_INVALID;
432     }
433     if (credentialMgr_ == nullptr) {
434         LOGE("credentialMgr_ is nullptr");
435         return ERR_DM_POINT_NULL;
436     }
437     return credentialMgr_->RegisterCredentialCallback(pkgName);
438 }
439 
UnRegisterCredentialCallback(const std::string & pkgName)440 int32_t DeviceManagerServiceImpl::UnRegisterCredentialCallback(const std::string &pkgName)
441 {
442     if (pkgName.empty()) {
443         LOGE("UnRegisterCredentialCallback failed, pkgName is empty");
444         return ERR_DM_INPUT_PARA_INVALID;
445     }
446     if (credentialMgr_== nullptr) {
447         LOGE("credentialMgr_ is nullptr");
448         return ERR_DM_POINT_NULL;
449     }
450     return credentialMgr_->UnRegisterCredentialCallback(pkgName);
451 }
452 
RegisterUiStateCallback(const std::string & pkgName)453 int32_t DeviceManagerServiceImpl::RegisterUiStateCallback(const std::string &pkgName)
454 {
455     if (pkgName.empty()) {
456         LOGE("RegisterUiStateCallback failed, pkgName is empty");
457         return ERR_DM_INPUT_PARA_INVALID;
458     }
459     if (authMgr_ == nullptr) {
460         LOGE("authMgr_ is nullptr");
461         return ERR_DM_POINT_NULL;
462     }
463     return authMgr_->RegisterUiStateCallback(pkgName);
464 }
465 
UnRegisterUiStateCallback(const std::string & pkgName)466 int32_t DeviceManagerServiceImpl::UnRegisterUiStateCallback(const std::string &pkgName)
467 {
468     if (pkgName.empty()) {
469         LOGE("UnRegisterUiStateCallback failed, pkgName is empty");
470         return ERR_DM_INPUT_PARA_INVALID;
471     }
472     if (authMgr_ == nullptr) {
473         LOGE("authMgr_ is nullptr");
474         return ERR_DM_POINT_NULL;
475     }
476     return authMgr_->UnRegisterUiStateCallback(pkgName);
477 }
478 
PraseNotifyEventJson(const std::string & event,JsonObject & jsonObject)479 int32_t DeviceManagerServiceImpl::PraseNotifyEventJson(const std::string &event, JsonObject &jsonObject)
480 {
481     jsonObject.Parse(event);
482     if (jsonObject.IsDiscarded()) {
483         LOGE("event prase error.");
484         return ERR_DM_FAILED;
485     }
486     if ((!jsonObject.Contains("extra")) || (!jsonObject["extra"].IsObject())) {
487         LOGE("extra error");
488         return ERR_DM_FAILED;
489     }
490     if ((!jsonObject["extra"].Contains("deviceId")) || (!jsonObject["extra"]["deviceId"].IsString())) {
491         LOGE("NotifyEvent deviceId invalid");
492         return ERR_DM_FAILED;
493     }
494     return DM_OK;
495 }
496 
NotifyEvent(const std::string & pkgName,const int32_t eventId,const std::string & event)497 int32_t DeviceManagerServiceImpl::NotifyEvent(const std::string &pkgName, const int32_t eventId,
498     const std::string &event)
499 {
500     LOGI("NotifyEvent begin, pkgName : %{public}s, eventId : %{public}d", pkgName.c_str(), eventId);
501     if ((eventId <= DM_NOTIFY_EVENT_START) || (eventId >= DM_NOTIFY_EVENT_BUTT)) {
502         LOGE("NotifyEvent eventId invalid");
503         return ERR_DM_INPUT_PARA_INVALID;
504     }
505     if (eventId == DM_NOTIFY_EVENT_ONDEVICEREADY) {
506         JsonObject jsonObject;
507         if (PraseNotifyEventJson(event, jsonObject) != DM_OK) {
508             LOGE("NotifyEvent json invalid");
509             return ERR_DM_INPUT_PARA_INVALID;
510         }
511         std::string deviceId;
512         jsonObject["extra"]["deviceId"].GetTo(deviceId);
513         if (deviceStateMgr_== nullptr) {
514             LOGE("deviceStateMgr_ is nullptr");
515             return ERR_DM_POINT_NULL;
516         }
517         if (deviceStateMgr_->ProcNotifyEvent(eventId, deviceId) != DM_OK) {
518             LOGE("NotifyEvent failed");
519             return ERR_DM_INPUT_PARA_INVALID;
520         };
521     }
522     return DM_OK;
523 }
524 
GetGroupType(std::vector<DmDeviceInfo> & deviceList)525 int32_t DeviceManagerServiceImpl::GetGroupType(std::vector<DmDeviceInfo> &deviceList)
526 {
527     LOGI("GetGroupType begin");
528     if (softbusConnector_ == nullptr || hiChainConnector_ == nullptr) {
529         LOGE("softbusConnector_ or hiChainConnector_ is nullptr");
530         return ERR_DM_POINT_NULL;
531     }
532 
533     for (auto it = deviceList.begin(); it != deviceList.end(); ++it) {
534         std::string udid = "";
535         int32_t ret = softbusConnector_->GetUdidByNetworkId(it->networkId, udid);
536         if (ret != DM_OK) {
537             LOGE("GetUdidByNetworkId failed ret: %{public}d", ret);
538             return ret;
539         }
540         std::string deviceId = softbusConnector_->GetDeviceUdidHashByUdid(udid);
541         if (memcpy_s(it->deviceId, DM_MAX_DEVICE_ID_LEN, deviceId.c_str(), deviceId.length()) != 0) {
542             LOGE("get deviceId: %{public}s failed", GetAnonyString(deviceId).c_str());
543             return ERR_DM_SECURITY_FUNC_FAILED;
544         }
545         it->authForm = hiChainConnector_->GetGroupType(udid);
546     }
547     return DM_OK;
548 }
549 
GetUdidHashByNetWorkId(const char * networkId,std::string & deviceId)550 int32_t DeviceManagerServiceImpl::GetUdidHashByNetWorkId(const char *networkId, std::string &deviceId)
551 {
552     if (softbusConnector_ == nullptr || hiChainConnector_ == nullptr) {
553         LOGE("softbusConnector_ or hiChainConnector_ is nullptr");
554         return ERR_DM_POINT_NULL;
555     }
556     std::string udid = "";
557     int32_t ret = softbusConnector_->GetUdidByNetworkId(networkId, udid);
558     if (ret != DM_OK) {
559         LOGE("GetUdidByNetworkId failed ret: %{public}d", ret);
560         return ret;
561     }
562     deviceId = softbusConnector_->GetDeviceUdidHashByUdid(udid);
563     return DM_OK;
564 }
565 
ImportAuthCode(const std::string & pkgName,const std::string & authCode)566 int32_t DeviceManagerServiceImpl::ImportAuthCode(const std::string &pkgName, const std::string &authCode)
567 {
568     if (pkgName.empty() || authCode.empty()) {
569         LOGE("ImportAuthCode failed, pkgName or authCode is empty");
570         return ERR_DM_INPUT_PARA_INVALID;
571     }
572 
573     return authMgr_->ImportAuthCode(pkgName, authCode);
574 }
575 
ExportAuthCode(std::string & authCode)576 int32_t DeviceManagerServiceImpl::ExportAuthCode(std::string &authCode)
577 {
578     int32_t ret = authMgr_->GeneratePincode();
579     authCode = std::to_string(ret);
580     LOGI("ExportAuthCode success, authCode: %{public}s.", GetAnonyString(authCode).c_str());
581     return DM_OK;
582 }
583 
BindTarget(const std::string & pkgName,const PeerTargetId & targetId,const std::map<std::string,std::string> & bindParam)584 int32_t DeviceManagerServiceImpl::BindTarget(const std::string &pkgName, const PeerTargetId &targetId,
585     const std::map<std::string, std::string> &bindParam)
586 {
587     if (pkgName.empty()) {
588         LOGE("BindTarget failed, pkgName is empty");
589         return ERR_DM_INPUT_PARA_INVALID;
590     }
591     return authMgr_->BindTarget(pkgName, targetId, bindParam);
592 }
593 
PutIdenticalAccountToAcl(std::string requestDeviceId,std::string trustDeviceId)594 void DeviceManagerServiceImpl::PutIdenticalAccountToAcl(std::string requestDeviceId, std::string trustDeviceId)
595 {
596     LOGI("DeviceManagerServiceImpl::PutIdenticalAccountAcl start.");
597     char localDeviceId[DEVICE_UUID_LENGTH] = {0};
598     Crypto::GetUdidHash(requestDeviceId, reinterpret_cast<uint8_t *>(localDeviceId));
599     std::string localUdidHash = std::string(localDeviceId);
600     DmAclInfo aclInfo;
601     aclInfo.bindType = IDENTICAL_ACCOUNT;
602     aclInfo.trustDeviceId = trustDeviceId;
603     aclInfo.authenticationType = ALLOW_AUTH_ALWAYS;
604     aclInfo.deviceIdHash = localUdidHash;
605     DmAccesser accesser;
606     accesser.requestUserId = MultipleUserConnector::GetFirstForegroundUserId();
607     accesser.requestAccountId = MultipleUserConnector::GetOhosAccountIdByUserId(accesser.requestUserId);
608     MultipleUserConnector::SetSwitchOldUserId(accesser.requestUserId);
609     MultipleUserConnector::SetSwitchOldAccountId(accesser.requestAccountId);
610     accesser.requestDeviceId = requestDeviceId;
611     DmAccessee accessee;
612     accessee.trustDeviceId = trustDeviceId;
613     DeviceProfileConnector::GetInstance().PutAccessControlList(aclInfo, accesser, accessee);
614 }
615 
DpAclAdd(const std::string & udid)616 int32_t DeviceManagerServiceImpl::DpAclAdd(const std::string &udid)
617 {
618     LOGI("DeviceManagerServiceImpl DpAclAdd start.");
619     MultipleUserConnector::SetSwitchOldUserId(MultipleUserConnector::GetCurrentAccountUserID());
620     MultipleUserConnector::SetSwitchOldAccountId(MultipleUserConnector::GetOhosAccountId());
621     if (deviceStateMgr_->CheckIsOnline(udid)) {
622         LOGI("DeviceManagerServiceImpl DpAclAdd identical account and online");
623         ProcessInfo processInfo;
624         processInfo.pkgName = std::string(DM_PKG_NAME);
625         processInfo.userId = MultipleUserConnector::GetFirstForegroundUserId();
626         softbusConnector_->SetProcessInfo(processInfo);
627         deviceStateMgr_->OnDeviceOnline(udid, DmAuthForm::IDENTICAL_ACCOUNT);
628     }
629     LOGI("DeviceManagerServiceImpl::DpAclAdd completed");
630     return DM_OK;
631 }
632 
IsSameAccount(const std::string & udid)633 int32_t DeviceManagerServiceImpl::IsSameAccount(const std::string &udid)
634 {
635     if (udid.empty()) {
636         LOGE("DeviceManagerServiceImpl::IsSameAccount error: udid: %{public}s", GetAnonyString(udid).c_str());
637         return ERR_DM_INPUT_PARA_INVALID;
638     }
639 
640     return DeviceProfileConnector::GetInstance().IsSameAccount(udid);
641 }
642 
GetTokenIdByNameAndDeviceId(std::string pkgName,std::string requestDeviceId)643 uint64_t DeviceManagerServiceImpl::GetTokenIdByNameAndDeviceId(std::string pkgName,
644     std::string requestDeviceId)
645 {
646     if (pkgName.empty()) {
647         LOGE("DeviceManagerServiceImpl::GetTokenIdByNameAndDeviceId error: pkgName.");
648         return ERR_DM_INPUT_PARA_INVALID;
649     }
650 
651     if (requestDeviceId.empty()) {
652         LOGE("DeviceManagerServiceImpl::GetTokenIdByNameAndDeviceId error: requestDeviceId.");
653         return ERR_DM_INPUT_PARA_INVALID;
654     }
655 
656     return DeviceProfileConnector::GetInstance().GetTokenIdByNameAndDeviceId(pkgName, requestDeviceId);
657 }
658 
GetAppTrustDeviceIdList(std::string pkgname)659 std::unordered_map<std::string, DmAuthForm> DeviceManagerServiceImpl::GetAppTrustDeviceIdList(
660     std::string pkgname)
661 {
662     char localDeviceId[DEVICE_UUID_LENGTH];
663     GetDevUdid(localDeviceId, DEVICE_UUID_LENGTH);
664     std::string deviceId = reinterpret_cast<char *>(localDeviceId);
665     return DeviceProfileConnector::GetInstance().GetAppTrustDeviceList(pkgname, deviceId);
666 }
667 
LoadHardwareFwkService()668 void DeviceManagerServiceImpl::LoadHardwareFwkService()
669 {
670     DmDistributedHardwareLoad::GetInstance().LoadDistributedHardwareFwk();
671 }
672 
HandleIdentAccountLogout(const std::string & localUdid,int32_t localUserId,const std::string & peerUdid,int32_t peerUserId)673 void DeviceManagerServiceImpl::HandleIdentAccountLogout(const std::string &localUdid, int32_t localUserId,
674     const std::string &peerUdid, int32_t peerUserId)
675 {
676     LOGI("localUdid %{public}s, localUserId %{public}d, peerUdid %{public}s, peerUserId %{public}d.",
677         GetAnonyString(localUdid).c_str(), localUserId, GetAnonyString(peerUdid).c_str(), peerUserId);
678     bool notifyOffline = DeviceProfileConnector::GetInstance().DeleteAclForAccountLogOut(localUdid, localUserId,
679         peerUdid, peerUserId);
680     if (notifyOffline) {
681         ProcessInfo processInfo;
682         processInfo.pkgName = std::string(DM_PKG_NAME);
683         processInfo.userId = localUserId;
684         CHECK_NULL_VOID(softbusConnector_);
685         softbusConnector_->SetProcessInfo(processInfo);
686         CHECK_NULL_VOID(deviceStateMgr_);
687         deviceStateMgr_->OnDeviceOffline(peerUdid);
688     }
689 }
690 
HandleUserRemoved(int32_t preUserId)691 void DeviceManagerServiceImpl::HandleUserRemoved(int32_t preUserId)
692 {
693     LOGI("PreUserId %{public}d.", preUserId);
694     char localDeviceId[DEVICE_UUID_LENGTH];
695     GetDevUdid(localDeviceId, DEVICE_UUID_LENGTH);
696     std::string localUdid = reinterpret_cast<char *>(localDeviceId);
697     DeviceProfileConnector::GetInstance().DeleteAclForUserRemoved(localUdid, preUserId);
698     CHECK_NULL_VOID(hiChainConnector_);
699     hiChainConnector_->DeleteAllGroup(preUserId);
700 }
701 
HandleRemoteUserRemoved(int32_t userId,const std::string & remoteUdid)702 void DeviceManagerServiceImpl::HandleRemoteUserRemoved(int32_t userId, const std::string &remoteUdid)
703 {
704     LOGI("remoteUdid %{public}s, userId %{public}d", GetAnonyString(remoteUdid).c_str(), userId);
705     std::vector<int32_t> localUserIds;
706     DeviceProfileConnector::GetInstance().DeleteAclForRemoteUserRemoved(remoteUdid, userId, localUserIds);
707     if (localUserIds.empty()) {
708         return;
709     }
710     CHECK_NULL_VOID(hiChainConnector_);
711     std::vector<std::pair<int32_t, std::string>> delInfoVec;
712     for (int32_t localUserId : localUserIds) {
713         delInfoVec.push_back(std::pair<int32_t, std::string>(localUserId, remoteUdid));
714     }
715     hiChainConnector_->DeleteGroupByACL(delInfoVec, localUserIds);
716 }
717 
HandleUserSwitched(const std::vector<std::string> & deviceVec,int32_t currentUserId,int32_t beforeUserId)718 void DeviceManagerServiceImpl::HandleUserSwitched(const std::vector<std::string> &deviceVec,
719     int32_t currentUserId, int32_t beforeUserId)
720 {
721     LOGI("currentUserId: %{public}s, beforeUserId: %{public}s", GetAnonyInt32(currentUserId).c_str(),
722         GetAnonyInt32(beforeUserId).c_str());
723     char localDeviceId[DEVICE_UUID_LENGTH] = {0};
724     GetDevUdid(localDeviceId, DEVICE_UUID_LENGTH);
725     std::string localUdid = static_cast<std::string>(localDeviceId);
726     DeviceProfileConnector::GetInstance().HandleUserSwitched(localUdid, deviceVec, currentUserId, beforeUserId);
727     CHECK_NULL_VOID(hiChainConnector_);
728     hiChainConnector_->DeleteAllGroup(beforeUserId);
729 }
730 
ScreenCommonEventCallback(std::string commonEventType)731 void DeviceManagerServiceImpl::ScreenCommonEventCallback(std::string commonEventType)
732 {
733     if (commonEventType == EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_LOCKED) {
734         LOGI("DeviceManagerServiceImpl::ScreenCommonEventCallback on screen locked.");
735         authMgr_->OnScreenLocked();
736         return;
737     }
738     LOGI("DeviceManagerServiceImpl::ScreenCommonEventCallback error.");
739 }
740 
CheckIsSameAccount(const DmAccessCaller & caller,const std::string & srcUdid,const DmAccessCallee & callee,const std::string & sinkUdid)741 int32_t DeviceManagerServiceImpl::CheckIsSameAccount(const DmAccessCaller &caller, const std::string &srcUdid,
742     const DmAccessCallee &callee, const std::string &sinkUdid)
743 {
744     return DeviceProfileConnector::GetInstance().CheckIsSameAccount(caller, srcUdid, callee, sinkUdid);
745 }
746 
CheckAccessControl(const DmAccessCaller & caller,const std::string & srcUdid,const DmAccessCallee & callee,const std::string & sinkUdid)747 int32_t DeviceManagerServiceImpl::CheckAccessControl(const DmAccessCaller &caller, const std::string &srcUdid,
748     const DmAccessCallee &callee, const std::string &sinkUdid)
749 {
750     CHECK_NULL_RETURN(hiChainConnector_, ERR_DM_POINT_NULL);
751     bool ret = hiChainConnector_->IsDevicesInP2PGroup(srcUdid, sinkUdid);
752     if (!ret) {
753         int32_t checkRet = DeviceProfileConnector::GetInstance().CheckAccessControl(caller,
754             srcUdid, callee, sinkUdid);
755         return checkRet;
756     } else {
757         return DM_OK;
758     }
759 }
760 
HandleDeviceNotTrust(const std::string & udid)761 void DeviceManagerServiceImpl::HandleDeviceNotTrust(const std::string &udid)
762 {
763     LOGI("DeviceManagerServiceImpl::HandleDeviceNotTrust udid: %{public}s.", GetAnonyString(udid).c_str());
764     if (udid.empty()) {
765         LOGE("HandleDeviceNotTrust udid is empty.");
766         return;
767     }
768     CHECK_NULL_VOID(authMgr_);
769     authMgr_->HandleDeviceNotTrust(udid);
770 }
771 
GetBindLevel(const std::string & pkgName,const std::string & localUdid,const std::string & udid,uint64_t & tokenId)772 int32_t DeviceManagerServiceImpl::GetBindLevel(const std::string &pkgName, const std::string &localUdid,
773     const std::string &udid, uint64_t &tokenId)
774 {
775     return DeviceProfileConnector::GetInstance().GetBindLevel(pkgName, localUdid, udid, tokenId);
776 }
777 
GetDeviceIdAndBindLevel(int32_t userId)778 std::map<std::string, int32_t> DeviceManagerServiceImpl::GetDeviceIdAndBindLevel(int32_t userId)
779 {
780     char localUdidTemp[DEVICE_UUID_LENGTH] = {0};
781     GetDevUdid(localUdidTemp, DEVICE_UUID_LENGTH);
782     std::string localUdid = std::string(localUdidTemp);
783     std::vector<int32_t> userIds;
784     userIds.push_back(userId);
785     return DeviceProfileConnector::GetInstance().GetDeviceIdAndBindLevel(userIds, localUdid);
786 }
787 
GetDeviceIdAndUserId(int32_t userId,const std::string & accountId)788 std::multimap<std::string, int32_t> DeviceManagerServiceImpl::GetDeviceIdAndUserId(int32_t userId,
789     const std::string &accountId)
790 {
791     char localUdidTemp[DEVICE_UUID_LENGTH] = {0};
792     GetDevUdid(localUdidTemp, DEVICE_UUID_LENGTH);
793     std::string localUdid = std::string(localUdidTemp);
794     return DeviceProfileConnector::GetInstance().GetDeviceIdAndUserId(userId, accountId, localUdid);
795 }
796 
HandleAccountLogoutEvent(int32_t remoteUserId,const std::string & remoteAccountHash,const std::string & remoteUdid)797 void DeviceManagerServiceImpl::HandleAccountLogoutEvent(int32_t remoteUserId, const std::string &remoteAccountHash,
798     const std::string &remoteUdid)
799 {
800     char localUdidTemp[DEVICE_UUID_LENGTH] = {0};
801     GetDevUdid(localUdidTemp, DEVICE_UUID_LENGTH);
802     std::string localUdid = std::string(localUdidTemp);
803     std::multimap<std::string, int32_t> devIdAndUserMap =
804         DeviceProfileConnector::GetInstance().GetDevIdAndUserIdByActHash(localUdid, remoteUdid,
805             remoteUserId, remoteAccountHash);
806     CHECK_NULL_VOID(listener_);
807     std::string uuid = "";
808     SoftbusCache::GetInstance().GetUuidByUdid(remoteUdid, uuid);
809     listener_->OnDeviceTrustChange(remoteUdid, uuid, DmAuthForm::IDENTICAL_ACCOUNT);
810     for (const auto &item : devIdAndUserMap) {
811         LOGI("remoteUdid %{public}s.", GetAnonyString(remoteUdid).c_str());
812         bool notifyOffline = DeviceProfileConnector::GetInstance().DeleteAclForAccountLogOut(item.first, item.second,
813             remoteUdid, remoteUserId);
814         if (notifyOffline) {
815             ProcessInfo processInfo;
816             processInfo.pkgName = std::string(DM_PKG_NAME);
817             processInfo.userId = item.second;
818             CHECK_NULL_VOID(softbusConnector_);
819             softbusConnector_->SetProcessInfo(processInfo);
820             CHECK_NULL_VOID(deviceStateMgr_);
821             deviceStateMgr_->OnDeviceOffline(remoteUdid);
822         }
823     }
824 }
825 
ConvertBindTypeToAuthForm(int32_t bindType)826 DmAuthForm DeviceManagerServiceImpl::ConvertBindTypeToAuthForm(int32_t bindType)
827 {
828     LOGI("BindType %{public}d.", bindType);
829     DmAuthForm authForm = DmAuthForm::INVALID_TYPE;
830     if (bindType == DM_IDENTICAL_ACCOUNT) {
831         authForm = IDENTICAL_ACCOUNT;
832     } else if (bindType == DM_POINT_TO_POINT) {
833         authForm = PEER_TO_PEER;
834     } else if (bindType == DM_ACROSS_ACCOUNT) {
835         authForm = ACROSS_ACCOUNT;
836     } else {
837         LOGE("Invalied bindType.");
838     }
839     return authForm;
840 }
841 
HandleDevUnBindEvent(int32_t remoteUserId,const std::string & remoteUdid)842 void DeviceManagerServiceImpl::HandleDevUnBindEvent(int32_t remoteUserId, const std::string &remoteUdid)
843 {
844     char localUdidTemp[DEVICE_UUID_LENGTH] = {0};
845     GetDevUdid(localUdidTemp, DEVICE_UUID_LENGTH);
846     std::string localUdid = std::string(localUdidTemp);
847     int32_t bindType = DeviceProfileConnector::GetInstance().HandleDevUnBindEvent(remoteUserId, remoteUdid, localUdid);
848     if (bindType == DM_INVALIED_BINDTYPE) {
849         LOGE("Invalied bindtype.");
850         return;
851     }
852     CHECK_NULL_VOID(authMgr_);
853     authMgr_->DeleteGroup(DM_PKG_NAME, remoteUdid);
854 }
855 
HandleAppUnBindEvent(int32_t remoteUserId,const std::string & remoteUdid,int32_t tokenId)856 void DeviceManagerServiceImpl::HandleAppUnBindEvent(int32_t remoteUserId, const std::string &remoteUdid,
857     int32_t tokenId)
858 {
859     char localUdidTemp[DEVICE_UUID_LENGTH] = {0};
860     GetDevUdid(localUdidTemp, DEVICE_UUID_LENGTH);
861     std::string localUdid = std::string(localUdidTemp);
862     ProcessInfo processInfo =
863         DeviceProfileConnector::GetInstance().HandleAppUnBindEvent(remoteUserId, remoteUdid, tokenId, localUdid);
864     if (processInfo.pkgName.empty()) {
865         LOGE("Pkgname is empty.");
866         return;
867     }
868     CHECK_NULL_VOID(softbusConnector_);
869     softbusConnector_->SetProcessInfo(processInfo);
870     softbusConnector_->HandleDeviceOffline(remoteUdid);
871 }
872 
HandleAppUnBindEvent(int32_t remoteUserId,const std::string & remoteUdid,int32_t tokenId,int32_t peerTokenId)873 void DeviceManagerServiceImpl::HandleAppUnBindEvent(int32_t remoteUserId, const std::string &remoteUdid,
874     int32_t tokenId, int32_t peerTokenId)
875 {
876     LOGI("HandleAppUnBindEvent peerTokenId = %{public}d.", peerTokenId);
877     char localUdidTemp[DEVICE_UUID_LENGTH] = {0};
878     GetDevUdid(localUdidTemp, DEVICE_UUID_LENGTH);
879     std::string localUdid = std::string(localUdidTemp);
880     ProcessInfo processInfo =
881         DeviceProfileConnector::GetInstance().HandleAppUnBindEvent(remoteUserId, remoteUdid,
882         tokenId, localUdid, peerTokenId);
883     if (processInfo.pkgName.empty()) {
884         LOGE("Pkgname is empty.");
885         return;
886     }
887     CHECK_NULL_VOID(softbusConnector_);
888     softbusConnector_->SetProcessInfo(processInfo);
889     softbusConnector_->HandleDeviceOffline(remoteUdid);
890 }
891 
HandleSyncUserIdEvent(const std::vector<uint32_t> & foregroundUserIds,const std::vector<uint32_t> & backgroundUserIds,const std::string & remoteUdid,bool isCheckUserStatus)892 void DeviceManagerServiceImpl::HandleSyncUserIdEvent(const std::vector<uint32_t> &foregroundUserIds,
893     const std::vector<uint32_t> &backgroundUserIds, const std::string &remoteUdid, bool isCheckUserStatus)
894 {
895     LOGI("remote udid: %{public}s, foregroundUserIds: %{public}s, backgroundUserIds: %{public}s",
896         GetAnonyString(remoteUdid).c_str(), GetIntegerList<uint32_t>(foregroundUserIds).c_str(),
897         GetIntegerList<uint32_t>(backgroundUserIds).c_str());
898     char localUdidTemp[DEVICE_UUID_LENGTH] = {0};
899     GetDevUdid(localUdidTemp, DEVICE_UUID_LENGTH);
900     std::string localUdid = std::string(localUdidTemp);
901     std::vector<int32_t> rmtFrontUserIdsTemp(foregroundUserIds.begin(), foregroundUserIds.end());
902     std::vector<int32_t> rmtBackUserIdsTemp(backgroundUserIds.begin(), backgroundUserIds.end());
903     std::vector<int32_t> localUserIds;
904     int32_t ret = MultipleUserConnector::GetForegroundUserIds(localUserIds);
905     if (ret != DM_OK) {
906         LOGE("Get foreground userids failed, ret: %{public}d", ret);
907         return;
908     }
909     if (isCheckUserStatus) {
910         MultipleUserConnector::ClearLockedUser(localUserIds);
911     }
912     DeviceProfileConnector::GetInstance().UpdateACL(localUdid, localUserIds, remoteUdid,
913         rmtFrontUserIdsTemp, rmtBackUserIdsTemp);
914     DeviceProfileConnector::GetInstance().HandleSyncBackgroundUserIdEvent(rmtBackUserIdsTemp, remoteUdid,
915         localUserIds, localUdid);
916     DeviceProfileConnector::GetInstance().HandleSyncForegroundUserIdEvent(rmtFrontUserIdsTemp, remoteUdid,
917         localUserIds, localUdid);
918 }
919 
HandleDeviceScreenStatusChange(DmDeviceInfo & devInfo)920 void DeviceManagerServiceImpl::HandleDeviceScreenStatusChange(DmDeviceInfo &devInfo)
921 {
922     LOGI("In");
923     CHECK_NULL_VOID(deviceStateMgr_);
924     CHECK_NULL_VOID(softbusConnector_);
925     std::string trustDeviceId = "";
926     if (softbusConnector_->GetUdidByNetworkId(devInfo.networkId, trustDeviceId) != DM_OK) {
927         LOGE("get udid failed.");
928         return;
929     }
930     std::string udidHash = softbusConnector_->GetDeviceUdidHashByUdid(trustDeviceId);
931     if (memcpy_s(devInfo.deviceId, DM_MAX_DEVICE_ID_LEN, udidHash.c_str(), udidHash.length()) != 0) {
932         LOGE("get deviceId: %{public}s failed", GetAnonyString(udidHash).c_str());
933         return;
934     }
935     char localUdid[DEVICE_UUID_LENGTH] = {0};
936     GetDevUdid(localUdid, DEVICE_UUID_LENGTH);
937     std::string requestDeviceId = static_cast<std::string>(localUdid);
938     uint32_t bindType = DeviceProfileConnector::GetInstance().CheckBindType(trustDeviceId, requestDeviceId);
939     LOGI("bind type is %{public}d.", bindType);
940     if (bindType == INVALIED_TYPE) {
941         return;
942     } else if (bindType == IDENTICAL_ACCOUNT_TYPE || bindType == DEVICE_PEER_TO_PEER_TYPE ||
943         bindType == DEVICE_ACROSS_ACCOUNT_TYPE) {
944         ProcessInfo processInfo;
945         processInfo.pkgName = std::string(DM_PKG_NAME);
946         processInfo.userId = MultipleUserConnector::GetFirstForegroundUserId();
947         softbusConnector_->SetProcessInfo(processInfo);
948     } else if (bindType == APP_PEER_TO_PEER_TYPE || bindType == APP_ACROSS_ACCOUNT_TYPE) {
949         std::vector<ProcessInfo> processInfoVec =
950             DeviceProfileConnector::GetInstance().GetProcessInfoFromAclByUserId(requestDeviceId, trustDeviceId,
951                 MultipleUserConnector::GetFirstForegroundUserId());
952         softbusConnector_->SetProcessInfoVec(processInfoVec);
953     }
954     deviceStateMgr_->HandleDeviceScreenStatusChange(devInfo);
955 }
956 
HandleCredentialAuthStatus(const std::string & deviceList,uint16_t deviceTypeId,int32_t errcode)957 void DeviceManagerServiceImpl::HandleCredentialAuthStatus(const std::string &deviceList, uint16_t deviceTypeId,
958                                                           int32_t errcode)
959 {
960     CHECK_NULL_VOID(credentialMgr_);
961     credentialMgr_->HandleCredentialAuthStatus(deviceList, deviceTypeId, errcode);
962 }
963 
ProcessAppUnintall(const std::string & appId,int32_t accessTokenId)964 int32_t DeviceManagerServiceImpl::ProcessAppUnintall(const std::string &appId, int32_t accessTokenId)
965 {
966     CHECK_NULL_RETURN(listener_, ERR_DM_POINT_NULL);
967     std::vector<DistributedDeviceProfile::AccessControlProfile> profiles =
968         DeviceProfileConnector::GetInstance().GetAllAccessControlProfile();
969     LOGI("delete ACL size is %{public}zu, appId %{public}s", profiles.size(), GetAnonyString(appId).c_str());
970     if (profiles.size() == 0) {
971         return DM_OK;
972     }
973     std::vector<std::pair<int32_t, std::string>> delACLInfoVec;
974     std::vector<int32_t> userIdVec;
975     for (auto &item : profiles) {
976         int64_t tokenId = item.GetAccesser().GetAccesserTokenId();
977         if (accessTokenId != static_cast<int32_t>(tokenId)) {
978             continue;
979         }
980         DeviceProfileConnector::GetInstance().DeleteAccessControlById(item.GetAccessControlId());
981         listener_->OnAppUnintall(item.GetAccesser().GetAccesserBundleName());
982         if (item.GetBindLevel() == DEVICE) {
983             userIdVec.push_back(item.GetAccesser().GetAccesserUserId());
984             delACLInfoVec.push_back(std::pair<int32_t, std::string>(item.GetAccesser().GetAccesserUserId(),
985                 item.GetAccessee().GetAccesseeDeviceId()));
986         }
987     }
988     if (delACLInfoVec.size() == 0) {
989         LOGI("delACLInfoVec is empty");
990         return DM_OK;
991     }
992     if (userIdVec.size() == 0) {
993         LOGI("userIdVec is empty");
994         return DM_OK;
995     }
996     CHECK_NULL_RETURN(hiChainConnector_, ERR_DM_POINT_NULL);
997     hiChainConnector_->DeleteGroupByACL(delACLInfoVec, userIdVec);
998     return DM_OK;
999 }
1000 
GetDeviceIdAndUserId(int32_t localUserId)1001 std::multimap<std::string, int32_t> DeviceManagerServiceImpl::GetDeviceIdAndUserId(int32_t localUserId)
1002 {
1003     LOGI("localUserId %{public}d.", localUserId);
1004     char localdeviceId[DEVICE_UUID_LENGTH] = {0};
1005     GetDevUdid(localdeviceId, DEVICE_UUID_LENGTH);
1006     std::string localUdid = std::string(localdeviceId);
1007     return DeviceProfileConnector::GetInstance().GetDeviceIdAndUserId(localUdid, localUserId);
1008 }
1009 
SaveOnlineDeviceInfo(const std::vector<DmDeviceInfo> & deviceList)1010 int32_t DeviceManagerServiceImpl::SaveOnlineDeviceInfo(const std::vector<DmDeviceInfo> &deviceList)
1011 {
1012     CHECK_NULL_RETURN(deviceStateMgr_, ERR_DM_POINT_NULL);
1013     for (auto item : deviceList) {
1014         deviceStateMgr_->SaveOnlineDeviceInfo(item);
1015     }
1016     return DM_OK;
1017 }
1018 
HandleDeviceUnBind(int32_t bindType,const std::string & peerUdid,const std::string & localUdid,int32_t localUserId,const std::string & localAccountId)1019 void DeviceManagerServiceImpl::HandleDeviceUnBind(int32_t bindType, const std::string &peerUdid,
1020     const std::string &localUdid, int32_t localUserId, const std::string &localAccountId)
1021 {
1022     return DeviceProfileConnector::GetInstance().HandleDeviceUnBind(bindType, peerUdid,
1023         localUdid, localUserId, localAccountId);
1024 }
1025 
RegisterAuthenticationType(int32_t authenticationType)1026 int32_t DeviceManagerServiceImpl::RegisterAuthenticationType(int32_t authenticationType)
1027 {
1028     CHECK_NULL_RETURN(authMgr_, ERR_DM_POINT_NULL);
1029     return authMgr_->RegisterAuthenticationType(authenticationType);
1030 }
1031 
DeleteAlwaysAllowTimeOut()1032 void DeviceManagerServiceImpl::DeleteAlwaysAllowTimeOut()
1033 {
1034     LOGI("Start DeleteAlwaysAllowTimeOut");
1035     std::vector<DistributedDeviceProfile::AccessControlProfile> profiles =
1036         DeviceProfileConnector::GetInstance().GetAllAccessControlProfile();
1037     std::string remoteUdid = "";
1038     int64_t currentTime =
1039         std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch()).count();
1040     for (auto &item : profiles) {
1041         if (item.GetBindType() == DM_IDENTICAL_ACCOUNT) {
1042             continue;
1043         }
1044         if ((currentTime - item.GetLastAuthTime()) > MAX_ALWAYS_ALLOW_SECONDS && item.GetLastAuthTime() > 0) {
1045             DeviceProfileConnector::GetInstance().DeleteAccessControlById(item.GetAccessControlId());
1046             remoteUdid = item.GetTrustDeviceId();
1047             CheckDeleteCredential(remoteUdid);
1048         }
1049     }
1050 }
1051 
CheckDeleteCredential(const std::string & remoteUdid)1052 void DeviceManagerServiceImpl::CheckDeleteCredential(const std::string &remoteUdid)
1053 {
1054     std::vector<DistributedDeviceProfile::AccessControlProfile> profiles =
1055         DeviceProfileConnector::GetInstance().GetAllAccessControlProfile();
1056     bool leftAcl = false;
1057     for (auto &item : profiles) {
1058         if (item.GetTrustDeviceId() == remoteUdid) {
1059             leftAcl = true;
1060         }
1061     }
1062     if (!leftAcl) {
1063         LOGI("CheckDeleteCredential delete credential");
1064         hiChainAuthConnector_->DeleteCredential(remoteUdid, MultipleUserConnector::GetCurrentAccountUserID());
1065     }
1066 }
1067 
CheckDeviceInfoPermission(const std::string & localUdid,const std::string & peerDeviceId)1068 int32_t DeviceManagerServiceImpl::CheckDeviceInfoPermission(const std::string &localUdid,
1069     const std::string &peerDeviceId)
1070 {
1071     int32_t ret = DeviceProfileConnector::GetInstance().CheckDeviceInfoPermission(localUdid, peerDeviceId);
1072     if (ret != DM_OK) {
1073         LOGE("CheckDeviceInfoPermission failed, ret: %{public}d", ret);
1074         return ret;
1075     }
1076     return DM_OK;
1077 }
1078 
CreateDMServiceObject(void)1079 extern "C" IDeviceManagerServiceImpl *CreateDMServiceObject(void)
1080 {
1081     return new DeviceManagerServiceImpl;
1082 }
1083 } // namespace DistributedHardware
1084 } // namespace OHOS
1085