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 "device_manager.h"
20 #include "service_control.h"
21 #include "system_ability_definition.h"
22
23 namespace OHOS {
24 namespace AppExecFwk {
25 namespace {
26 const std::string BUNDLE_NAME = "ohos.appexefwk.appexefwk_standard";
27 const std::string SERVICES_NAME = "d-bms";
28 }
29
BmsDeviceManager()30 BmsDeviceManager::BmsDeviceManager()
31 {
32 APP_LOGD("BmsDeviceManager instance is created");
33 }
34
InitDeviceManager()35 bool BmsDeviceManager::InitDeviceManager()
36 {
37 initCallback_ = std::make_shared<DeviceInitCallBack>();
38 int32_t ret =
39 DistributedHardware::DeviceManager::GetInstance().InitDeviceManager(BUNDLE_NAME, initCallback_);
40 if (ret != 0) {
41 APP_LOGE("init device manager failed, ret:%{public}d", ret);
42 return false;
43 }
44 stateCallback_ = std::make_shared<BmsDeviceStateCallback>();
45 ret =
46 DistributedHardware::DeviceManager::GetInstance().RegisterDevStateCallback(BUNDLE_NAME, "", stateCallback_);
47 if (ret != 0) {
48 APP_LOGE("register devStateCallback failed, ret:%{public}d", ret);
49 return false;
50 }
51 APP_LOGD("register device manager success");
52 return true;
53 }
54
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)55 void BmsDeviceManager::OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
56 {
57 APP_LOGD("OnAddSystemAbility systemAbilityId:%{public}d add!", systemAbilityId);
58 if (DISTRIBUTED_HARDWARE_DEVICEMANAGER_SA_ID == systemAbilityId) {
59 InitDeviceManager();
60 if (GetTrustedDeviceListSize() > 0) {
61 StartDynamicSystemProcess(DISTRIBUTED_BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
62 }
63 }
64 }
65
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)66 void BmsDeviceManager::OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
67 {
68 APP_LOGD("OnRemoveSystemAbility systemAbilityId:%{public}d removed!", systemAbilityId);
69 if (DISTRIBUTED_BUNDLE_MGR_SERVICE_SYS_ABILITY_ID == systemAbilityId) {
70 if (GetTrustedDeviceListSize() > 0) {
71 StartDynamicSystemProcess(DISTRIBUTED_BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
72 }
73 }
74 }
75
GetTrustedDeviceListSize()76 int BmsDeviceManager::GetTrustedDeviceListSize()
77 {
78 std::vector<DistributedHardware::DmDeviceInfo> deviceList;
79 int32_t ret =
80 DistributedHardware::DeviceManager::GetInstance().GetTrustedDeviceList(BUNDLE_NAME, "", deviceList);
81 if (ret != 0) {
82 APP_LOGE("GetTrustedDeviceList failed, ret:%{public}d", ret);
83 return -1;
84 }
85 APP_LOGD("GetTrustedDeviceList size :%{public}ud", static_cast<uint32_t>(deviceList.size()));
86 return deviceList.size();
87 }
88
StartDynamicSystemProcess(int32_t systemAbilityId)89 void BmsDeviceManager::StartDynamicSystemProcess(int32_t systemAbilityId)
90 {
91 APP_LOGD("StartDynamicSystemProcess systemAbilityId:%{public}d !", systemAbilityId);
92 std::string strExtra = std::to_string(systemAbilityId);
93 auto extraArgv = strExtra.c_str();
94 int ret = ServiceControlWithExtra(SERVICES_NAME.c_str(), START, &extraArgv, 1);
95 APP_LOGD("StartDynamicSystemProcess, ret:%{public}d", ret);
96 }
97
StopDynamicSystemProcess(int32_t systemAbilityId)98 void BmsDeviceManager::StopDynamicSystemProcess(int32_t systemAbilityId)
99 {
100 APP_LOGD("StopDynamicSystemProcess systemAbilityId:%{public}d !", systemAbilityId);
101 std::string strExtra = std::to_string(systemAbilityId);
102 auto extraArgv = strExtra.c_str();
103 int ret = ServiceControlWithExtra(SERVICES_NAME.c_str(), STOP, &extraArgv, 1);
104 APP_LOGD("StopDynamicSystemProcess, ret:%{public}d", ret);
105 }
106
GetUdidByNetworkId(const std::string & netWorkId,std::string & udid)107 int32_t BmsDeviceManager::GetUdidByNetworkId(const std::string &netWorkId, std::string &udid)
108 {
109 APP_LOGD("GetUdidByNetworkId");
110 return DistributedHardware::DeviceManager::GetInstance().GetUdidByNetworkId(BUNDLE_NAME, netWorkId, udid);
111 }
112
OnRemoteDied()113 void BmsDeviceManager::DeviceInitCallBack::OnRemoteDied()
114 {
115 APP_LOGD("DeviceInitCallBack OnRemoteDied");
116 }
117
OnDeviceOnline(const DistributedHardware::DmDeviceInfo & deviceInfo)118 void BmsDeviceManager::BmsDeviceStateCallback::OnDeviceOnline(const DistributedHardware::DmDeviceInfo &deviceInfo)
119 {
120 APP_LOGD("DeviceInitCallBack OnDeviceOnline");
121 BmsDeviceManager::StartDynamicSystemProcess(DISTRIBUTED_BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
122 }
123
OnDeviceOffline(const DistributedHardware::DmDeviceInfo & deviceInfo)124 void BmsDeviceManager::BmsDeviceStateCallback::OnDeviceOffline(const DistributedHardware::DmDeviceInfo &deviceInfo)
125 {
126 APP_LOGD("DeviceInitCallBack OnDeviceOffline");
127 std::vector<DistributedHardware::DmDeviceInfo> deviceList;
128 int32_t ret =
129 DistributedHardware::DeviceManager::GetInstance().GetTrustedDeviceList(BUNDLE_NAME, "", deviceList);
130 if (ret != 0) {
131 APP_LOGE("GetTrustedDeviceList failed, ret:%{public}d", ret);
132 return;
133 }
134 APP_LOGD("OnDeviceOffline GetTrustedDeviceList size :%{public}u", static_cast<uint32_t>(deviceList.size()));
135 if (deviceList.size() == 0) {
136 BmsDeviceManager::StopDynamicSystemProcess(DISTRIBUTED_BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
137 }
138 }
139
OnDeviceChanged(const DistributedHardware::DmDeviceInfo & deviceInfo)140 void BmsDeviceManager::BmsDeviceStateCallback::OnDeviceChanged(const DistributedHardware::DmDeviceInfo &deviceInfo)
141 {
142 APP_LOGD("DeviceInitCallBack OnDeviceChanged");
143 }
144
OnDeviceReady(const DistributedHardware::DmDeviceInfo & deviceInfo)145 void BmsDeviceManager::BmsDeviceStateCallback::OnDeviceReady(const DistributedHardware::DmDeviceInfo &deviceInfo)
146 {
147 APP_LOGD("DeviceInitCallBack OnDeviceReady");
148 }
149 }
150 }