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 #include "string_ex.h"
16 #include "sstream"
17 #include "iostream"
18
19 #include "edm_errors.h"
20 #include "hilog_wrapper.h"
21 #include "ibus_extension.h"
22 #include "iservice_registry.h"
23 #include "system_ability_definition.h"
24 #include "bus_extension_core.h"
25 #include "usb_dev_subscriber.h"
26 #include "usb_device_info.h"
27 #include "usb_driver_info.h"
28 #include "usb_bus_extension.h"
29
30 namespace OHOS {
31 namespace ExternalDeviceManager {
32 using namespace std;
33 #ifdef EXTDEVMGR_USB_PASS_THROUGH
34 const std::string SERVICE_NAME = "usb_host_interface_service";
35 #endif // EXTDEVMGR_USB_PASS_THROUGH
36
UsbBusExtension()37 UsbBusExtension::UsbBusExtension()
38 {
39 this->subScriber_ = nullptr;
40 this->usbInterface_ = nullptr;
41 }
42
~UsbBusExtension()43 UsbBusExtension::~UsbBusExtension()
44 {
45 #ifdef EXTDEVMGR_USB_PASS_THROUGH
46 if (this->usbInterface_ != nullptr && this->subScriber_ != nullptr) {
47 this->usbInterface_->UnbindUsbdHostSubscriber(this->subScriber_);
48 }
49 #else
50 if (this->usbInterface_ != nullptr && this->subScriber_ != nullptr && this->recipient_ != nullptr) {
51 this->usbInterface_->UnbindUsbdSubscriber(this->subScriber_);
52 sptr<IRemoteObject> remote = OHOS::HDI::hdi_objcast<HDI::Usb::V1_0::IUsbInterface>(usbInterface_);
53 remote->RemoveDeathRecipient(recipient_);
54 recipient_.clear();
55 }
56 #endif // EXTDEVMGR_USB_PASS_THROUGH
57 }
58
59 #ifdef EXTDEVMGR_USB_PASS_THROUGH
SetUsbInferface(sptr<IUsbHostInterface> iusb)60 void UsbBusExtension::SetUsbInferface(sptr<IUsbHostInterface> iusb)
61 {
62 this->usbInterface_ = iusb;
63 }
64 #else
SetUsbInferface(sptr<IUsbInterface> iusb)65 void UsbBusExtension::SetUsbInferface(sptr<IUsbInterface> iusb)
66 {
67 this->usbInterface_ = iusb;
68 }
69 #endif // EXTDEVMGR_USB_PASS_THROUGH
70
SetUsbDdk(sptr<V1_1::IUsbDdk> iUsbDdk)71 void UsbBusExtension::SetUsbDdk(sptr<V1_1::IUsbDdk> iUsbDdk)
72 {
73 this->iUsbDdk_ = iUsbDdk;
74 }
75
GetBusType()76 BusType UsbBusExtension::GetBusType()
77 {
78 return BusType::BUS_TYPE_USB;
79 }
80
AcquireDriverChangeCallback()81 shared_ptr<IDriverChangeCallback> UsbBusExtension::AcquireDriverChangeCallback()
82 {
83 if (this->iUsbDdk_ == nullptr) {
84 this->iUsbDdk_ = V1_1::IUsbDdk::Get();
85 if (this->iUsbDdk_ == nullptr) {
86 EDM_LOGE(MODULE_BUS_USB, "driver get IUsbDdk error");
87 return nullptr;
88 }
89 }
90
91 return make_shared<UsbDriverChangeCallback>(this->iUsbDdk_);
92 }
93
SetDevChangeCallback(shared_ptr<IDevChangeCallback> devCallback)94 int32_t UsbBusExtension::SetDevChangeCallback(shared_ptr<IDevChangeCallback> devCallback)
95 {
96 #ifdef EXTDEVMGR_USB_PASS_THROUGH
97 if (this->usbInterface_ == nullptr) {
98 this->usbInterface_ = IUsbHostInterface::Get(SERVICE_NAME, true);
99 if (this->usbInterface_ == nullptr) {
100 EDM_LOGE(MODULE_BUS_USB, "get IUsbHostInterface error");
101 return EDM_ERR_INVALID_OBJECT;
102 }
103 EDM_LOGD(MODULE_BUS_USB, "get IUsbHostInterface sucess");
104 }
105 #else
106 if (this->usbInterface_ == nullptr) {
107 this->usbInterface_ = IUsbInterface::Get();
108 if (this->usbInterface_ == nullptr) {
109 EDM_LOGE(MODULE_BUS_USB, "get IUsbInterface error");
110 return EDM_ERR_INVALID_OBJECT;
111 }
112 EDM_LOGD(MODULE_BUS_USB, "get usbInferface sucess");
113 recipient_ = new UsbdDeathRecipient();
114 sptr<IRemoteObject> remote = OHOS::HDI::hdi_objcast<HDI::Usb::V1_0::IUsbInterface>(usbInterface_);
115 if (!remote->AddDeathRecipient(recipient_)) {
116 EDM_LOGE(MODULE_BUS_USB, "add DeathRecipient failed");
117 return EDM_NOK;
118 }
119 }
120 #endif // EXTDEVMGR_USB_PASS_THROUGH
121
122 if (this->iUsbDdk_ == nullptr) {
123 this->iUsbDdk_ = V1_1::IUsbDdk::Get();
124 if (this->iUsbDdk_ == nullptr) {
125 EDM_LOGE(MODULE_BUS_USB, "get IUsbDdk error");
126 return EDM_ERR_INVALID_OBJECT;
127 }
128 }
129
130 if (this->subScriber_ == nullptr) {
131 this->subScriber_ = new UsbDevSubscriber();
132 if (this->subScriber_ == nullptr) {
133 EDM_LOGE(MODULE_BUS_USB, "get usbDevSubscriber error");
134 return EDM_EER_MALLOC_FAIL;
135 }
136 EDM_LOGD(MODULE_BUS_USB, "get subScriber_ sucess");
137 }
138
139 this->subScriber_->Init(devCallback, usbInterface_, iUsbDdk_);
140 #ifdef EXTDEVMGR_USB_PASS_THROUGH
141 this->usbInterface_->BindUsbdHostSubscriber(subScriber_);
142 #else
143 this->usbInterface_->BindUsbdSubscriber(subScriber_);
144 #endif // EXTDEVMGR_USB_PASS_THROUGH
145 return 0;
146 };
147
MatchDriver(const DriverInfo & driver,const DeviceInfo & device,const std::string & type)148 bool UsbBusExtension::MatchDriver(const DriverInfo &driver, const DeviceInfo &device, const std::string &type)
149 {
150 if (LowerStr(driver.GetBusName()) != "usb") {
151 EDM_LOGW(MODULE_BUS_USB, "driver bus not support by this module [UsbBusExtension]");
152 return false;
153 }
154
155 if (device.GetBusType() != BusType::BUS_TYPE_USB) {
156 EDM_LOGW(MODULE_BUS_USB, "deivce type not support %d != %d",
157 (uint32_t)device.GetBusType(), (uint32_t)BusType::BUS_TYPE_USB);
158 return false;
159 }
160 const UsbDriverInfo *usbDriverInfo = static_cast<const UsbDriverInfo *>(driver.GetInfoExt().get());
161 const UsbDeviceInfo *usbDeviceInfo = static_cast<const UsbDeviceInfo *>(&device);
162 if (usbDriverInfo == nullptr || usbDeviceInfo == nullptr) {
163 EDM_LOGE(MODULE_BUS_USB, "static_cast error, the usbDriverInfo or usbDeviceInfo is nullptr");
164 return false;
165 }
166 string usbDrvInfoStr;
167 const_cast<UsbDriverInfo*>(usbDriverInfo)->Serialize(usbDrvInfoStr);
168 auto vidFind = find(usbDriverInfo->vids_.begin(), usbDriverInfo->vids_.end(), usbDeviceInfo->idVendor_);
169 if (vidFind == usbDriverInfo->vids_.end()) {
170 EDM_LOGI(MODULE_BUS_USB, "vid not match\n");
171 return false;
172 }
173 auto pidFind = find(usbDriverInfo->pids_.begin(), usbDriverInfo->pids_.end(), usbDeviceInfo->idProduct_);
174 if (pidFind == usbDriverInfo->pids_.end()) {
175 EDM_LOGI(MODULE_BUS_USB, "pid not match\n");
176 return false;
177 }
178 EDM_LOGI(MODULE_BUS_USB, "Driver and Device match sucess\n");
179 return true;
180 }
181
ParseDriverInfo(const map<string,string> & metadata)182 shared_ptr<DriverInfoExt> UsbBusExtension::ParseDriverInfo(const map<string, string> &metadata)
183 {
184 shared_ptr<UsbDriverInfo> usbDriverInfo = make_shared<UsbDriverInfo>();
185 if (usbDriverInfo == nullptr) {
186 EDM_LOGE(MODULE_BUS_USB, "creat UsbDriverInfo obj fail\n");
187 return nullptr;
188 }
189 for (auto& meta : metadata) {
190 if (LowerStr(meta.first) == "pid") {
191 usbDriverInfo->pids_ = this->ParseCommaStrToVectorUint16(meta.second);
192 } else if (LowerStr(meta.first) == "vid") {
193 usbDriverInfo->vids_ = this->ParseCommaStrToVectorUint16(meta.second);
194 }
195 }
196 return usbDriverInfo;
197 }
198
ParseCommaStrToVectorUint16(const string & str)199 vector<uint16_t> UsbBusExtension::ParseCommaStrToVectorUint16(const string &str)
200 {
201 vector<uint16_t> ret;
202 stringstream ss(str);
203 string s;
204 int num;
205 while (getline(ss, s, ',')) {
206 stringstream out;
207 out << hex << s;
208 out >> num;
209 ret.push_back(num);
210 }
211 if (ret.size() == 0) {
212 EDM_LOGW(MODULE_BUS_USB, "parse error, size 0, str:%{public}s.", str.c_str());
213 } else {
214 EDM_LOGD(MODULE_BUS_USB, "parse sucess, size %{public}zu, str:%{public}s", ret.size(), str.c_str());
215 }
216 return ret;
217 }
218
219 #ifndef EXTDEVMGR_USB_PASS_THROUGH
OnRemoteDied(const wptr<IRemoteObject> & object)220 void UsbBusExtension::UsbdDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &object)
221 {
222 auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
223 if (samgrProxy == nullptr) {
224 EDM_LOGE(MODULE_BUS_USB, "get samgr failed");
225 return;
226 }
227
228 auto ret = samgrProxy->UnloadSystemAbility(HDF_EXTERNAL_DEVICE_MANAGER_SA_ID);
229 if (ret != EDM_OK) {
230 EDM_LOGE(MODULE_BUS_USB, "unload failed");
231 }
232 }
233 #endif // EXTDEVMGR_USB_PASS_THROUGH
234
GetNewDriverInfoExtObject()235 shared_ptr<DriverInfoExt> UsbBusExtension::GetNewDriverInfoExtObject()
236 {
237 return make_shared<UsbDriverInfo>();
238 }
239
RegBusExtension()240 __attribute__ ((constructor)) static void RegBusExtension()
241 {
242 EDM_LOGI(MODULE_COMMON, "installing UsbBusExtension");
243 RegisterBusExtension<UsbBusExtension>(BusType::BUS_TYPE_USB);
244 }
245 }
246 }