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