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