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