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