1 /*
2 * Copyright (c) 2021 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 "bms_device_manager.h"
17
18 #include "app_log_wrapper.h"
19 #include "bundle_mgr_service.h"
20 #include "device_manager.h"
21 #include "service_control.h"
22 #include "system_ability_definition.h"
23
24 namespace OHOS {
25 namespace AppExecFwk {
26 namespace {
27 const std::string BUNDLE_NAME = "ohos.appexefwk.appexefwk_standard";
28 const std::string SERVICES_NAME = "d-bms";
29 }
30
BmsDeviceManager()31 BmsDeviceManager::BmsDeviceManager()
32 {
33 APP_LOGD("BmsDeviceManager instance is created");
34 }
35
InitDeviceManager()36 bool BmsDeviceManager::InitDeviceManager()
37 {
38 std::lock_guard<std::mutex> lock(isInitMutex_);
39 if (isInit_) {
40 APP_LOGI("device manager already init");
41 return true;
42 }
43
44 initCallback_ = std::make_shared<DeviceInitCallBack>();
45 int32_t ret =
46 DistributedHardware::DeviceManager::GetInstance().InitDeviceManager(BUNDLE_NAME, initCallback_);
47 if (ret != 0) {
48 APP_LOGE("init device manager failed, ret:%{public}d", ret);
49 return false;
50 }
51 stateCallback_ = std::make_shared<BmsDeviceStateCallback>();
52 ret =
53 DistributedHardware::DeviceManager::GetInstance().RegisterDevStateCallback(BUNDLE_NAME, "", stateCallback_);
54 if (ret != 0) {
55 APP_LOGE("register devStateCallback failed, ret:%{public}d", ret);
56 return false;
57 }
58 isInit_ = true;
59 APP_LOGI("register device manager success");
60 return true;
61 }
62
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)63 void BmsDeviceManager::OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
64 {
65 APP_LOGI("OnAddSystemAbility systemAbilityId:%{public}d add!", systemAbilityId);
66 if (DISTRIBUTED_HARDWARE_DEVICEMANAGER_SA_ID == systemAbilityId) {
67 InitDeviceManager();
68 std::vector<DistributedHardware::DmDeviceInfo> deviceList;
69 if (!GetTrustedDeviceList(deviceList)) {
70 APP_LOGW("deviceManager GetTrustedDeviceList failed");
71 return;
72 }
73 if (deviceList.size() > 0) {
74 StartDynamicSystemProcess(DISTRIBUTED_BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
75 }
76 }
77 }
78
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)79 void BmsDeviceManager::OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
80 {
81 APP_LOGI("OnRemoveSystemAbility systemAbilityId:%{public}d removed!", systemAbilityId);
82 if (DISTRIBUTED_BUNDLE_MGR_SERVICE_SYS_ABILITY_ID == systemAbilityId) {
83 std::vector<DistributedHardware::DmDeviceInfo> deviceList;
84 if (!GetTrustedDeviceList(deviceList)) {
85 APP_LOGW("deviceManager GetTrustedDeviceList failed");
86 return;
87 }
88 if (deviceList.size() > 0) {
89 StartDynamicSystemProcess(DISTRIBUTED_BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
90 }
91 }
92 }
93
GetAllDeviceList(std::vector<std::string> & deviceIds)94 bool BmsDeviceManager::GetAllDeviceList(std::vector<std::string> &deviceIds)
95 {
96 std::vector<DistributedHardware::DmDeviceInfo> deviceList;
97 if (!GetTrustedDeviceList(deviceList)) {
98 APP_LOGE("GetTrustedDeviceList failed");
99 return false;
100 }
101 std::transform(deviceList.begin(),
102 deviceList.end(),
103 std::back_inserter(deviceIds),
104 [](const auto &device) { return device.deviceId; });
105
106 DistributedHardware::DmDeviceInfo dmDeviceInfo;
107 int32_t ret =
108 DistributedHardware::DeviceManager::GetInstance().GetLocalDeviceInfo(BUNDLE_NAME, dmDeviceInfo);
109 if (ret != 0) {
110 APP_LOGE("GetLocalDeviceInfo failed");
111 return false;
112 }
113 deviceIds.emplace_back(dmDeviceInfo.deviceId);
114 return true;
115 }
116
GetTrustedDeviceList(std::vector<DistributedHardware::DmDeviceInfo> & deviceList)117 bool BmsDeviceManager::GetTrustedDeviceList(std::vector<DistributedHardware::DmDeviceInfo> &deviceList)
118 {
119 if (!InitDeviceManager()) {
120 return false;
121 }
122 int32_t ret =
123 DistributedHardware::DeviceManager::GetInstance().GetTrustedDeviceList(BUNDLE_NAME, "", deviceList);
124 if (ret != 0) {
125 APP_LOGW("GetTrustedDeviceList failed, ret:%{public}d", ret);
126 return false;
127 }
128 APP_LOGD("GetTrustedDeviceList size :%{public}ud", static_cast<uint32_t>(deviceList.size()));
129 return true;
130 }
131
StartDynamicSystemProcess(int32_t systemAbilityId)132 void BmsDeviceManager::StartDynamicSystemProcess(int32_t systemAbilityId)
133 {
134 APP_LOGD("StartDynamicSystemProcess systemAbilityId:%{public}d !", systemAbilityId);
135 std::string strExtra = std::to_string(systemAbilityId);
136 auto extraArgv = strExtra.c_str();
137 int ret = ServiceControlWithExtra(SERVICES_NAME.c_str(), START, &extraArgv, 1);
138 APP_LOGI("StartDynamicSystemProcess, ret:%{public}d", ret);
139 }
140
StopDynamicSystemProcess(int32_t systemAbilityId)141 void BmsDeviceManager::StopDynamicSystemProcess(int32_t systemAbilityId)
142 {
143 APP_LOGD("StopDynamicSystemProcess systemAbilityId:%{public}d !", systemAbilityId);
144 std::string strExtra = std::to_string(systemAbilityId);
145 auto extraArgv = strExtra.c_str();
146 int ret = ServiceControlWithExtra(SERVICES_NAME.c_str(), STOP, &extraArgv, 1);
147 APP_LOGI("StopDynamicSystemProcess, ret:%{public}d", ret);
148 }
149
GetUdidByNetworkId(const std::string & netWorkId,std::string & udid)150 int32_t BmsDeviceManager::GetUdidByNetworkId(const std::string &netWorkId, std::string &udid)
151 {
152 APP_LOGI("GetUdidByNetworkId");
153 if (!InitDeviceManager()) {
154 return -1;
155 }
156 return DistributedHardware::DeviceManager::GetInstance().GetUdidByNetworkId(BUNDLE_NAME, netWorkId, udid);
157 }
158
OnRemoteDied()159 void BmsDeviceManager::DeviceInitCallBack::OnRemoteDied()
160 {
161 APP_LOGD("DeviceInitCallBack OnRemoteDied");
162 }
163
OnDeviceOnline(const DistributedHardware::DmDeviceInfo & deviceInfo)164 void BmsDeviceManager::BmsDeviceStateCallback::OnDeviceOnline(const DistributedHardware::DmDeviceInfo &deviceInfo)
165 {
166 APP_LOGI("DeviceInitCallBack OnDeviceOnline");
167 }
168
OnDeviceOffline(const DistributedHardware::DmDeviceInfo & deviceInfo)169 void BmsDeviceManager::BmsDeviceStateCallback::OnDeviceOffline(const DistributedHardware::DmDeviceInfo &deviceInfo)
170 {
171 APP_LOGI("DeviceInitCallBack OnDeviceOffline");
172 auto deviceManager = DelayedSingleton<BundleMgrService>::GetInstance()->GetDeviceManager();
173 if (deviceManager == nullptr) {
174 APP_LOGW("deviceManager is nullptr");
175 return;
176 }
177 std::vector<DistributedHardware::DmDeviceInfo> deviceList;
178 if (!deviceManager->GetTrustedDeviceList(deviceList)) {
179 APP_LOGW("deviceManager GetTrustedDeviceList failed");
180 return;
181 }
182 if (deviceList.size() == 0) {
183 BmsDeviceManager::StopDynamicSystemProcess(DISTRIBUTED_BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
184 }
185 }
186
OnDeviceChanged(const DistributedHardware::DmDeviceInfo & deviceInfo)187 void BmsDeviceManager::BmsDeviceStateCallback::OnDeviceChanged(const DistributedHardware::DmDeviceInfo &deviceInfo)
188 {
189 APP_LOGD("DeviceInitCallBack OnDeviceChanged");
190 }
191
OnDeviceReady(const DistributedHardware::DmDeviceInfo & deviceInfo)192 void BmsDeviceManager::BmsDeviceStateCallback::OnDeviceReady(const DistributedHardware::DmDeviceInfo &deviceInfo)
193 {
194 APP_LOGI("DeviceInitCallBack OnDeviceReady");
195 BmsDeviceManager::StartDynamicSystemProcess(DISTRIBUTED_BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
196 }
197 }
198 }