1 /*
2 * Copyright (c) 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 "driver_ext_mgr.h"
17
18 #include "bus_extension_core.h"
19 #include "dev_change_callback.h"
20 #include "driver_extension_controller.h"
21 #include "driver_pkg_manager.h"
22 #include "edm_errors.h"
23 #include "etx_device_mgr.h"
24 #include "ext_permission_manager.h"
25 #include "hilog_wrapper.h"
26 #include "iservice_registry.h"
27 #include "system_ability_definition.h"
28 #include "usb_device_info.h"
29 #include "usb_driver_info.h"
30
31 namespace OHOS {
32 namespace ExternalDeviceManager {
33 const bool G_REGISTER_RESULT =
34 SystemAbility::MakeAndRegisterAbility(DelayedSingleton<DriverExtMgr>::GetInstance().get());
35 static const std::string PERMISSION_NAME = "ohos.permission.ACCESS_EXTENSIONAL_DEVICE_DRIVER";
36
DriverExtMgr()37 DriverExtMgr::DriverExtMgr() : SystemAbility(HDF_EXTERNAL_DEVICE_MANAGER_SA_ID, true) {}
~DriverExtMgr()38 DriverExtMgr::~DriverExtMgr() {}
39
OnStart()40 void DriverExtMgr::OnStart()
41 {
42 int32_t ret;
43 EDM_LOGI(MODULE_SERVICE, "hdf_ext_devmgr OnStart");
44 BusExtensionCore::GetInstance().LoadBusExtensionLibs();
45 ret = DriverPkgManager::GetInstance().Init(bmsFuture_, accountFuture_, commEventFuture_);
46 AddSystemAbilityListener(SUBSYS_ACCOUNT_SYS_ABILITY_ID_BEGIN);
47 AddSystemAbilityListener(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
48 AddSystemAbilityListener(COMMON_EVENT_SERVICE_ID);
49 if (ret != EDM_OK) {
50 EDM_LOGE(MODULE_SERVICE, "DriverPkgManager Init failed %{public}d", ret);
51 }
52 ret = ExtDeviceManager::GetInstance().Init();
53 if (ret != EDM_OK) {
54 EDM_LOGE(MODULE_SERVICE, "ExtDeviceManager Init failed %{public}d", ret);
55 }
56 std::shared_ptr<DevChangeCallback> callback = std::make_shared<DevChangeCallback>();
57 ret = BusExtensionCore::GetInstance().Init(callback);
58 if (ret != EDM_OK) {
59 EDM_LOGE(MODULE_SERVICE, "BusExtensionCore Init failed %{public}d", ret);
60 }
61 if (!Publish(AsObject())) {
62 EDM_LOGE(MODULE_DEV_MGR, "OnStart register to system ability manager failed.");
63 return;
64 }
65 }
66
OnStop()67 void DriverExtMgr::OnStop()
68 {
69 EDM_LOGI(MODULE_SERVICE, "hdf_ext_devmgr OnStop");
70 }
71
Dump(int fd,const std::vector<std::u16string> & args)72 int DriverExtMgr::Dump(int fd, const std::vector<std::u16string> &args)
73 {
74 return 0;
75 }
76
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)77 void DriverExtMgr::OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId)
78 {
79 EDM_LOGI(MODULE_SERVICE, "OnAddSystemAbility systemAbilityId: %{public}d", systemAbilityId);
80 std::lock_guard<std::mutex> lock(promiseMutex_);
81 switch (systemAbilityId) {
82 case SUBSYS_ACCOUNT_SYS_ABILITY_ID_BEGIN: {
83 EDM_LOGI(MODULE_SERVICE, "OnAddSystemAbility accountMgr");
84 if (!accountPromiseUsed_) {
85 DriverPkgManager::GetInstance().SubscribeOsAccountSwitch();
86 accountPromise_.set_value(systemAbilityId);
87 accountPromiseUsed_ = true;
88 }
89 break;
90 }
91 case BUNDLE_MGR_SERVICE_SYS_ABILITY_ID: {
92 EDM_LOGI(MODULE_SERVICE, "OnAddSystemAbility BMS");
93 if (!bmsPromiseUsed_) {
94 bmsPromise_.set_value(systemAbilityId);
95 bmsPromiseUsed_ = true;
96 }
97 break;
98 }
99 case COMMON_EVENT_SERVICE_ID: {
100 EDM_LOGI(MODULE_SERVICE, "OnAddSystemAbility CommonEventService");
101 DriverPkgManager::GetInstance().RegisterBundleStatusCallback();
102 if (!cesPromiseUsed_) {
103 commEventPromise_.set_value(systemAbilityId);
104 cesPromiseUsed_ = true;
105 }
106 break;
107 }
108 default:
109 break;
110 }
111 }
112
QueryDevice(uint32_t busType,std::vector<std::shared_ptr<DeviceData>> & devices)113 UsbErrCode DriverExtMgr::QueryDevice(uint32_t busType, std::vector<std::shared_ptr<DeviceData>> &devices)
114 {
115 if (!ExtPermissionManager::VerifyPermission(PERMISSION_NAME)) {
116 EDM_LOGE(MODULE_DEV_MGR, "%{public}s no permission", __func__);
117 return UsbErrCode::EDM_ERR_NO_PERM;
118 }
119
120 if (busType == BusType::BUS_TYPE_INVALID) {
121 EDM_LOGE(MODULE_DEV_MGR, "invalid busType:%{public}d", static_cast<int32_t>(busType));
122 return UsbErrCode::EDM_ERR_INVALID_PARAM;
123 }
124
125 std::vector<std::shared_ptr<DeviceInfo>> deviceInfos =
126 ExtDeviceManager::GetInstance().QueryDevice(static_cast<BusType>(busType));
127 for (const auto &deviceInfo : deviceInfos) {
128 switch (deviceInfo->GetBusType()) {
129 case BusType::BUS_TYPE_USB: {
130 std::shared_ptr<UsbDeviceInfo> usbDeviceInfo = std::static_pointer_cast<UsbDeviceInfo>(deviceInfo);
131 std::shared_ptr<USBDevice> device = std::make_shared<USBDevice>();
132 device->busType = usbDeviceInfo->GetBusType();
133 device->deviceId = usbDeviceInfo->GetDeviceId();
134 device->descripton = usbDeviceInfo->GetDeviceDescription();
135 device->productId = usbDeviceInfo->GetProductId();
136 device->vendorId = usbDeviceInfo->GetVendorId();
137 devices.push_back(device);
138 break;
139 }
140 default: {
141 break;
142 }
143 }
144 }
145
146 return UsbErrCode::EDM_OK;
147 }
148
BindDevice(uint64_t deviceId,const sptr<IDriverExtMgrCallback> & connectCallback)149 UsbErrCode DriverExtMgr::BindDevice(uint64_t deviceId, const sptr<IDriverExtMgrCallback> &connectCallback)
150 {
151 EDM_LOGI(MODULE_DEV_MGR, "%{public}s enter", __func__);
152 if (!ExtPermissionManager::VerifyPermission(PERMISSION_NAME)) {
153 EDM_LOGE(MODULE_DEV_MGR, "%{public}s no permission", __func__);
154 return UsbErrCode::EDM_ERR_NO_PERM;
155 }
156
157 return static_cast<UsbErrCode>(ExtDeviceManager::GetInstance().ConnectDevice(deviceId, connectCallback));
158 }
159
UnBindDevice(uint64_t deviceId)160 UsbErrCode DriverExtMgr::UnBindDevice(uint64_t deviceId)
161 {
162 EDM_LOGD(MODULE_DEV_MGR, "%{public}s enter", __func__);
163 if (!ExtPermissionManager::VerifyPermission(PERMISSION_NAME)) {
164 EDM_LOGE(MODULE_DEV_MGR, "%{public}s no permission", __func__);
165 return UsbErrCode::EDM_ERR_NO_PERM;
166 }
167
168 return static_cast<UsbErrCode>(ExtDeviceManager::GetInstance().DisConnectDevice(deviceId));
169 }
170
ParseToDeviceInfoData(const std::shared_ptr<Device> & device)171 static std::shared_ptr<DeviceInfoData> ParseToDeviceInfoData(const std::shared_ptr<Device> &device)
172 {
173 EDM_LOGD(MODULE_DEV_MGR, "%{public}s enter", __func__);
174 if (device == nullptr || device->GetDeviceInfo() == nullptr) {
175 EDM_LOGD(MODULE_DEV_MGR, "device or deviceInfo is null");
176 return nullptr;
177 }
178 auto deviceInfo = device->GetDeviceInfo();
179 auto busType = deviceInfo->GetBusType();
180 if (busType <= BusType::BUS_TYPE_INVALID || busType >= BusType::BUS_TYPE_MAX) {
181 EDM_LOGD(MODULE_DEV_MGR, "invalid busType:%{public}u", busType);
182 return nullptr;
183 }
184 std::shared_ptr<DeviceInfoData> tempDeviceInfo = nullptr;
185
186 switch (busType) {
187 case BusType::BUS_TYPE_USB: {
188 std::shared_ptr<UsbDeviceInfo> usbDeviceInfo = std::static_pointer_cast<UsbDeviceInfo>(deviceInfo);
189 std::shared_ptr<USBDeviceInfoData> tempUsbDeviceInfo = std::make_shared<USBDeviceInfoData>();
190 tempUsbDeviceInfo->productId = usbDeviceInfo->GetProductId();
191 tempUsbDeviceInfo->vendorId = usbDeviceInfo->GetVendorId();
192 std::transform(usbDeviceInfo->interfaceDescList_.begin(), usbDeviceInfo->interfaceDescList_.end(),
193 std::back_inserter(tempUsbDeviceInfo->interfaceDescList),
194 [](UsbInterfaceDescriptor desc) {
195 std::shared_ptr<USBInterfaceDesc> interfaceDesc = std::make_shared<USBInterfaceDesc>();
196 interfaceDesc->bInterfaceNumber = desc.bInterfaceNumber;
197 interfaceDesc->bClass = desc.bInterfaceClass;
198 interfaceDesc->bSubClass = desc.bInterfaceSubClass;
199 interfaceDesc->bProtocol = desc.bInterfaceProtocol;
200 return interfaceDesc;
201 });
202 tempDeviceInfo = tempUsbDeviceInfo;
203 break;
204 }
205 default:
206 break;
207 }
208 if (tempDeviceInfo != nullptr) {
209 tempDeviceInfo->deviceId = deviceInfo->GetDeviceId();
210 tempDeviceInfo->isDriverMatched = device->HasDriver();
211 if (tempDeviceInfo->isDriverMatched) {
212 tempDeviceInfo->driverUid = device->GetDriverUid();
213 }
214 }
215
216 return tempDeviceInfo;
217 }
218
ParseToDriverInfoData(const std::shared_ptr<DriverInfo> & driverInfo)219 static std::shared_ptr<DriverInfoData> ParseToDriverInfoData(const std::shared_ptr<DriverInfo> &driverInfo)
220 {
221 EDM_LOGD(MODULE_DEV_MGR, "%{public}s enter", __func__);
222 if (driverInfo == nullptr || driverInfo->GetInfoExt() == nullptr) {
223 EDM_LOGD(MODULE_DEV_MGR, "driverInfo or extInfo is null");
224 return nullptr;
225 }
226 auto busType = driverInfo->GetBusType();
227 if (busType <= BusType::BUS_TYPE_INVALID || busType >= BusType::BUS_TYPE_MAX) {
228 EDM_LOGD(MODULE_DEV_MGR, "invalid busType:%{public}u", busType);
229 return nullptr;
230 }
231 std::shared_ptr<DriverInfoData> tempDriverInfo = nullptr;
232
233 switch (busType) {
234 case BusType::BUS_TYPE_USB: {
235 std::shared_ptr<UsbDriverInfo> usbDriverInfo
236 = std::static_pointer_cast<UsbDriverInfo>(driverInfo->GetInfoExt());
237 std::shared_ptr<USBDriverInfoData> tempUsbDriverInfo = std::make_shared<USBDriverInfoData>();
238 tempUsbDriverInfo->pids = usbDriverInfo->GetProductIds();
239 tempUsbDriverInfo->vids = usbDriverInfo->GetVendorIds();
240 tempDriverInfo = tempUsbDriverInfo;
241 break;
242 }
243 default:
244 break;
245 }
246 if (tempDriverInfo != nullptr) {
247 tempDriverInfo->busType = busType;
248 tempDriverInfo->driverUid = driverInfo->GetDriverUid();
249 tempDriverInfo->driverName = driverInfo->GetDriverName();
250 tempDriverInfo->bundleSize = driverInfo->GetDriverSize();
251 tempDriverInfo->version = driverInfo->GetVersion();
252 tempDriverInfo->description = driverInfo->GetDescription();
253 }
254 return tempDriverInfo;
255 }
256
QueryDeviceInfo(std::vector<std::shared_ptr<DeviceInfoData>> & deviceInfos,bool isByDeviceId,const uint64_t deviceId)257 UsbErrCode DriverExtMgr::QueryDeviceInfo(std::vector<std::shared_ptr<DeviceInfoData>> &deviceInfos,
258 bool isByDeviceId, const uint64_t deviceId)
259 {
260 EDM_LOGD(MODULE_DEV_MGR, "%{public}s enter", __func__);
261 if (!ExtPermissionManager::IsSystemApp()) {
262 EDM_LOGE(MODULE_DEV_MGR, "%{public}s none system app", __func__);
263 return UsbErrCode::EDM_ERR_NOT_SYSTEM_APP;
264 }
265
266 if (!ExtPermissionManager::VerifyPermission(PERMISSION_NAME)) {
267 EDM_LOGE(MODULE_DEV_MGR, "%{public}s no permission", __func__);
268 return UsbErrCode::EDM_ERR_NO_PERM;
269 }
270
271 vector<shared_ptr<Device>> devices;
272 if (isByDeviceId) {
273 devices = ExtDeviceManager::GetInstance().QueryDevicesById(deviceId);
274 } else {
275 devices = ExtDeviceManager::GetInstance().QueryAllDevices();
276 }
277
278 for (const auto &device : devices) {
279 auto tempDeviceInfo = ParseToDeviceInfoData(device);
280 if (tempDeviceInfo != nullptr) {
281 deviceInfos.push_back(tempDeviceInfo);
282 }
283 }
284 return UsbErrCode::EDM_OK;
285 }
286
QueryDriverInfo(std::vector<std::shared_ptr<DriverInfoData>> & driverInfos,bool isByDriverUid,const std::string & driverUid)287 UsbErrCode DriverExtMgr::QueryDriverInfo(std::vector<std::shared_ptr<DriverInfoData>> &driverInfos,
288 bool isByDriverUid, const std::string &driverUid)
289 {
290 if (!ExtPermissionManager::IsSystemApp()) {
291 EDM_LOGE(MODULE_DEV_MGR, "%{public}s none system app", __func__);
292 return UsbErrCode::EDM_ERR_NOT_SYSTEM_APP;
293 }
294
295 if (!ExtPermissionManager::VerifyPermission(PERMISSION_NAME)) {
296 EDM_LOGE(MODULE_DEV_MGR, "%{public}s no permission", __func__);
297 return UsbErrCode::EDM_ERR_NO_PERM;
298 }
299
300 vector<shared_ptr<DriverInfo>> tempDriverInfos;
301 int32_t ret = DriverPkgManager::GetInstance().QueryDriverInfo(tempDriverInfos, isByDriverUid, driverUid);
302 if (ret != UsbErrCode::EDM_OK) {
303 return static_cast<UsbErrCode>(ret);
304 }
305 for (const auto &driverInfo : tempDriverInfos) {
306 auto tempDriverInfo = ParseToDriverInfoData(driverInfo);
307 if (tempDriverInfo != nullptr) {
308 driverInfos.push_back(tempDriverInfo);
309 }
310 }
311 EDM_LOGD(MODULE_DEV_MGR, "driverInfos size: %{public}zu enter", driverInfos.size());
312
313 return UsbErrCode::EDM_OK;
314 }
315 } // namespace ExternalDeviceManager
316 } // namespace OHOS
317