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