1 /*
2 * Copyright (c) 2021-2023 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 "dbms_device_manager.h"
17
18 #include "account_manager_helper.h"
19 #include "app_log_wrapper.h"
20 #include "bundle_constants.h"
21 #include "device_manager.h"
22 #include "service_control.h"
23 #include "system_ability_definition.h"
24
25 namespace OHOS {
26 namespace AppExecFwk {
27 namespace {
28 const std::string DISTRIBUTED_BUNDLE_NAME = "distributed_bundle_framework";
29 const std::string SERVICES_NAME = "d-bms";
30 }
31
DbmsDeviceManager()32 DbmsDeviceManager::DbmsDeviceManager()
33 {
34 APP_LOGI("DbmsDeviceManager instance is created");
35 }
36
InitDeviceManager()37 bool DbmsDeviceManager::InitDeviceManager()
38 {
39 std::lock_guard<std::mutex> lock(isInitMutex_);
40 if (isInit_) {
41 APP_LOGI("device manager already init");
42 return true;
43 }
44
45 initCallback_ = std::make_shared<DeviceInitCallBack>();
46 int32_t ret =
47 DistributedHardware::DeviceManager::GetInstance().InitDeviceManager(DISTRIBUTED_BUNDLE_NAME, initCallback_);
48 if (ret != 0) {
49 APP_LOGE("init device manager failed, ret:%{public}d", ret);
50 return false;
51 }
52 isInit_ = true;
53 APP_LOGI("register device manager success");
54 return true;
55 }
56
OnRemoteDied()57 void DbmsDeviceManager::DeviceInitCallBack::OnRemoteDied()
58 {
59 APP_LOGI("DeviceInitCallBack OnRemoteDied");
60 }
61
GetUdidByNetworkId(const std::string & netWorkId,std::string & udid)62 int32_t DbmsDeviceManager::GetUdidByNetworkId(const std::string &netWorkId, std::string &udid)
63 {
64 APP_LOGI("GetUdidByNetworkId");
65 if (!InitDeviceManager()) {
66 return -1;
67 }
68 return DistributedHardware::DeviceManager::GetInstance().GetUdidByNetworkId(
69 DISTRIBUTED_BUNDLE_NAME, netWorkId, udid);
70 }
71
GetUuidByNetworkId(const std::string & netWorkId,std::string & uuid)72 int32_t DbmsDeviceManager::GetUuidByNetworkId(const std::string &netWorkId, std::string &uuid)
73 {
74 APP_LOGI("GetUuidByNetworkId");
75 if (!InitDeviceManager()) {
76 return -1;
77 }
78 int32_t errCode = DistributedHardware::DeviceManager::GetInstance()
79 .GetUuidByNetworkId(DISTRIBUTED_BUNDLE_NAME, netWorkId, uuid);
80 if (errCode != ERR_OK) {
81 APP_LOGE("GetUuidByNetworkId failed");
82 }
83 return errCode;
84 }
85
GetLocalDevice(DistributedHardware::DmDeviceInfo & dmDeviceInfo)86 bool DbmsDeviceManager::GetLocalDevice(DistributedHardware::DmDeviceInfo& dmDeviceInfo)
87 {
88 APP_LOGI("GetLocalDeviceId");
89 if (!InitDeviceManager()) {
90 return false;
91 }
92 int32_t ret = DistributedHardware::DeviceManager::GetInstance()
93 .GetLocalDeviceInfo(DISTRIBUTED_BUNDLE_NAME, dmDeviceInfo);
94 if (ret != ERR_OK) {
95 APP_LOGE("GetLocalDeviceInfo failed");
96 return false;
97 }
98 return true;
99 }
100
CheckAclData(DistributedBmsAclInfo info)101 bool DbmsDeviceManager::CheckAclData(DistributedBmsAclInfo info)
102 {
103 #ifdef ACCOUNT_ENABLE
104 DistributedHardware::DmAccessCaller dmSrecaller = {
105 .accountId = info.accountId,
106 .pkgName = info.pkgName,
107 .networkId = info.networkId,
108 .userId = info.userId,
109 .tokenId = info.tokenId
110 };
111 AccountSA::OhosAccountInfo osAccountInfo;
112 if (!AccountManagerHelper::GetOsAccountData(osAccountInfo)) {
113 APP_LOGE("GetOsAccountData failed");
114 return false;
115 }
116 DistributedHardware::DmDeviceInfo dmDeviceInfo;
117 if (!GetLocalDevice(dmDeviceInfo)) {
118 return false;
119 }
120 DistributedHardware::DmAccessCallee dmDstCallee = {
121 .accountId = osAccountInfo.uid_,
122 .networkId = dmDeviceInfo.networkId,
123 .userId = AccountManagerHelper::GetCurrentActiveUserId(),
124 .tokenId = OHOS::Security::AccessToken::AccessTokenKit::GetHapTokenID(dmDstCallee.userId, info.pkgName, 0)
125 };
126 return DistributedHardware::DeviceManager::GetInstance().CheckAccessControl(dmSrecaller, dmDstCallee);
127 #else
128 APP_LOGI("ACCOUNT_ENABLE is false");
129 return false;
130 #endif
131 }
132 }
133 }