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