diff --git a/extdevmgr.gni b/extdevmgr.gni index 23f3e82bf17b622223f5d7f9fe79319c46a4e938..2c2cf2a6ea77dbaa96821d8fae2d7d5d317b4bd6 100644 --- a/extdevmgr.gni +++ b/extdevmgr.gni @@ -1,4 +1,4 @@ -# Copyright (c) 2023 Huawei Device Co., Ltd. +# Copyright (c) 2023-2025 Huawei Device Co., Ltd. # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at @@ -14,3 +14,7 @@ import("//build/ohos.gni") ext_mgr_path = "//drivers/external_device_manager" utils_path = "//drivers/external_device_manager/utils" + +declare_args() { + extdevmgr_usb_pass_through = true +} diff --git a/sa_profile/hdf_ext_devmgr.cfg b/sa_profile/hdf_ext_devmgr.cfg index 8d936eb2269806b55228c491ba016ab61fa86f97..929c4339f2f1b57256d74cc752418138f1025711 100644 --- a/sa_profile/hdf_ext_devmgr.cfg +++ b/sa_profile/hdf_ext_devmgr.cfg @@ -13,7 +13,7 @@ "path" : ["/system/bin/sa_main", "/system/profile/hdf_ext_devmgr.json"], "ondemand" : true, "uid" : "hdf_ext_devmgr", - "gid" : ["hdf_ext_devmgr", "shell", "uhid"], + "gid" : ["hdf_ext_devmgr", "shell", "uhid", "usb_host"], "permission" : [ "ohos.permission.LISTEN_BUNDLE_CHANGE", "ohos.permission.DISTRIBUTED_DATASYNC", diff --git a/services/native/driver_extension_manager/include/bus_extension/usb/usb_bus_extension.h b/services/native/driver_extension_manager/include/bus_extension/usb/usb_bus_extension.h index ef33f3a533ea6daa5d4118fd46ebda6b1d62629a..d8b09ba023ddb910d1755aab4d042bfd1c4387e2 100644 --- a/services/native/driver_extension_manager/include/bus_extension/usb/usb_bus_extension.h +++ b/services/native/driver_extension_manager/include/bus_extension/usb/usb_bus_extension.h @@ -22,13 +22,20 @@ #include "usb_dev_subscriber.h" #include "usb_device_info.h" #include "usb_driver_change_callback.h" -#include "v1_0/iusb_interface.h" #include "v1_1/iusb_ddk.h" - +#ifdef EXTDEVMGR_USB_PASS_THROUGH +#include "v2_0/iusb_host_interface.h" +#else +#include "v1_0/iusb_interface.h" +#endif // EXTDEVMGR_USB_PASS_THROUGH namespace OHOS { namespace ExternalDeviceManager { using namespace std; +#ifdef EXTDEVMGR_USB_PASS_THROUGH +using namespace OHOS::HDI::Usb::V2_0; +#else using namespace OHOS::HDI::Usb::V1_0; +#endif // EXTDEVMGR_USB_PASS_THROUGH using namespace OHOS::HDI::Usb::Ddk; class UsbBusExtension : public IBusExtension { public: @@ -38,21 +45,29 @@ public: bool MatchDriver(const DriverInfo &driver, const DeviceInfo &device, const std::string &type = "") override; shared_ptr ParseDriverInfo(const map &metadata) override; shared_ptr GetNewDriverInfoExtObject() override; +#ifdef EXTDEVMGR_USB_PASS_THROUGH + void SetUsbInferface(sptr iusb); +#else void SetUsbInferface(sptr iusb); +#endif // EXTDEVMGR_USB_PASS_THROUGH void SetUsbDdk(sptr iUsbDdk); BusType GetBusType() override; shared_ptr AcquireDriverChangeCallback() override; private: + sptr subScriber_ = nullptr; + sptr iUsbDdk_ = nullptr; + vector ParseCommaStrToVectorUint16(const string &str); +#ifdef EXTDEVMGR_USB_PASS_THROUGH + sptr usbInterface_ = nullptr; +#else class UsbdDeathRecipient : public IRemoteObject::DeathRecipient { public: void OnRemoteDied(const wptr &object) override; }; - sptr subScriber_ = nullptr; sptr usbInterface_ = nullptr; // in usb HDI; - sptr iUsbDdk_ = nullptr; - vector ParseCommaStrToVectorUint16(const string &str); sptr recipient_; +#endif // EXTDEVMGR_USB_PASS_THROUGH }; } } diff --git a/services/native/driver_extension_manager/include/bus_extension/usb/usb_dev_subscriber.h b/services/native/driver_extension_manager/include/bus_extension/usb/usb_dev_subscriber.h index 51e4ea7bdf0a0683afca862257bae9f43689ae6a..9e0658f04ef16e81af9f3db9f507962e904a99fc 100644 --- a/services/native/driver_extension_manager/include/bus_extension/usb/usb_dev_subscriber.h +++ b/services/native/driver_extension_manager/include/bus_extension/usb/usb_dev_subscriber.h @@ -17,13 +17,21 @@ #define USB_DEV_SUBSCRIBER_H #include "ibus_extension.h" #include "usb_device_info.h" +#ifdef EXTDEVMGR_USB_PASS_THROUGH +#include "v2_0/iusb_host_interface.h" +#else #include "v1_0/iusbd_subscriber.h" #include "v1_0/iusb_interface.h" +#endif // EXTDEVMGR_USB_PASS_THROUGH #include "v1_1/iusb_ddk.h" #include "usb_config_desc_parser.h" namespace OHOS { namespace ExternalDeviceManager { +#ifdef EXTDEVMGR_USB_PASS_THROUGH +using namespace OHOS::HDI::Usb::V2_0; +#else using namespace OHOS::HDI::Usb::V1_0; +#endif // EXTDEVMGR_USB_PASS_THROUGH using namespace OHOS::HDI::Usb::Ddk; struct UsbDevDescLite { @@ -45,12 +53,20 @@ struct UsbDevDescLite { class UsbDevSubscriber : public IUsbdSubscriber { public: +#ifdef EXTDEVMGR_USB_PASS_THROUGH + void Init(shared_ptr callback, sptr iusb, sptr iUsbDdk); +#else void Init(shared_ptr callback, sptr iusb, sptr iUsbDdk); +#endif // EXTDEVMGR_USB_PASS_THROUGH int32_t DeviceEvent(const USBDeviceInfo &info) override; int32_t PortChangedEvent(const PortInfo &info) override; private: shared_ptr callback_; +#ifdef EXTDEVMGR_USB_PASS_THROUGH + sptr iusb_; +#else sptr iusb_; +#endif // EXTDEVMGR_USB_PASS_THROUGH sptr iUsbDdk_; int32_t OnDeviceConnect(const UsbDev &usbDev); int32_t OnDeviceDisconnect(const UsbDev &usbDev); diff --git a/services/native/driver_extension_manager/src/bus_extension/usb/BUILD.gn b/services/native/driver_extension_manager/src/bus_extension/usb/BUILD.gn index 9d2204ab4b4b4c28b69c4c9c7927539a523e3e03..330207f01fcad3b4be547723424302ce8ec0df5b 100644 --- a/services/native/driver_extension_manager/src/bus_extension/usb/BUILD.gn +++ b/services/native/driver_extension_manager/src/bus_extension/usb/BUILD.gn @@ -34,15 +34,23 @@ ohos_shared_library("driver_extension_usb_bus") { "${ext_mgr_path}/services/native/driver_extension_manager/src/bus_extension/core:driver_extension_bus_core", "${ext_mgr_path}/services/native/driver_extension_manager/src/drivers_hisysevent:report_sys_event", ] + defines = [] + if (extdevmgr_usb_pass_through) { + defines += [ " EXTDEVMGR_USB_PASS_THROUGH" ] + } external_deps = [ "cJSON:cjson", "c_utils:utils", "drivers_interface_usb:libusb_ddk_proxy_1.1", - "drivers_interface_usb:libusb_proxy_1.0", "hilog:libhilog", "ipc:ipc_core", "samgr:samgr_proxy", ] + if (extdevmgr_usb_pass_through) { + external_deps += [ "drivers_interface_usb:libusb_proxy_2.0" ] + } else { + external_deps += [ "drivers_interface_usb:libusb_proxy_1.0" ] + } cflags_cc = [ "-fno-asynchronous-unwind-tables", "-fno-unwind-tables", diff --git a/services/native/driver_extension_manager/src/bus_extension/usb/usb_bus_extension.cpp b/services/native/driver_extension_manager/src/bus_extension/usb/usb_bus_extension.cpp index 4ec0a31e45c220fbd19ceb815c2f280d2ad1b33b..c5178d164e11c406dad9f5f3669005111cd2144d 100644 --- a/services/native/driver_extension_manager/src/bus_extension/usb/usb_bus_extension.cpp +++ b/services/native/driver_extension_manager/src/bus_extension/usb/usb_bus_extension.cpp @@ -31,6 +31,9 @@ namespace OHOS { namespace ExternalDeviceManager { using namespace std; +#ifdef EXTDEVMGR_USB_PASS_THROUGH +const std::string SERVICE_NAME = "usb_host_interface_service"; +#endif // EXTDEVMGR_USB_PASS_THROUGH UsbBusExtension::UsbBusExtension() { @@ -40,18 +43,31 @@ UsbBusExtension::UsbBusExtension() UsbBusExtension::~UsbBusExtension() { +#ifdef EXTDEVMGR_USB_PASS_THROUGH + if (this->usbInterface_ != nullptr && this->subScriber_ != nullptr) { + this->usbInterface_->UnbindUsbdHostSubscriber(this->subScriber_); + } +#else if (this->usbInterface_ != nullptr && this->subScriber_ != nullptr && this->recipient_ != nullptr) { this->usbInterface_->UnbindUsbdSubscriber(this->subScriber_); sptr remote = OHOS::HDI::hdi_objcast(usbInterface_); remote->RemoveDeathRecipient(recipient_); recipient_.clear(); } +#endif // EXTDEVMGR_USB_PASS_THROUGH } +#ifdef EXTDEVMGR_USB_PASS_THROUGH +void UsbBusExtension::SetUsbInferface(sptr iusb) +{ + this->usbInterface_ = iusb; +} +#else void UsbBusExtension::SetUsbInferface(sptr iusb) { this->usbInterface_ = iusb; } +#endif // EXTDEVMGR_USB_PASS_THROUGH void UsbBusExtension::SetUsbDdk(sptr iUsbDdk) { @@ -78,6 +94,16 @@ shared_ptr UsbBusExtension::AcquireDriverChangeCallback() int32_t UsbBusExtension::SetDevChangeCallback(shared_ptr devCallback) { +#ifdef EXTDEVMGR_USB_PASS_THROUGH + if (this->usbInterface_ == nullptr) { + this->usbInterface_ = IUsbHostInterface::Get(SERVICE_NAME, true); + if (this->usbInterface_ == nullptr) { + EDM_LOGE(MODULE_BUS_USB, "get IUsbHostInterface error"); + return EDM_ERR_INVALID_OBJECT; + } + EDM_LOGD(MODULE_BUS_USB, "get IUsbHostInterface sucess"); + } +#else if (this->usbInterface_ == nullptr) { this->usbInterface_ = IUsbInterface::Get(); if (this->usbInterface_ == nullptr) { @@ -92,6 +118,7 @@ int32_t UsbBusExtension::SetDevChangeCallback(shared_ptr dev return EDM_NOK; } } +#endif // EXTDEVMGR_USB_PASS_THROUGH if (this->iUsbDdk_ == nullptr) { this->iUsbDdk_ = V1_1::IUsbDdk::Get(); @@ -111,8 +138,11 @@ int32_t UsbBusExtension::SetDevChangeCallback(shared_ptr dev } this->subScriber_->Init(devCallback, usbInterface_, iUsbDdk_); +#ifdef EXTDEVMGR_USB_PASS_THROUGH + this->usbInterface_->BindUsbdHostSubscriber(subScriber_); +#else this->usbInterface_->BindUsbdSubscriber(subScriber_); - +#endif // EXTDEVMGR_USB_PASS_THROUGH return 0; }; @@ -200,6 +230,8 @@ vector UsbBusExtension::ParseCommaStrToVectorUint16(const string &str) } return ret; } + +#ifndef EXTDEVMGR_USB_PASS_THROUGH void UsbBusExtension::UsbdDeathRecipient::OnRemoteDied(const wptr &object) { auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); @@ -213,6 +245,7 @@ void UsbBusExtension::UsbdDeathRecipient::OnRemoteDied(const wptr EDM_LOGE(MODULE_BUS_USB, "unload failed"); } } +#endif // EXTDEVMGR_USB_PASS_THROUGH shared_ptr UsbBusExtension::GetNewDriverInfoExtObject() { diff --git a/services/native/driver_extension_manager/src/bus_extension/usb/usb_dev_subscriber.cpp b/services/native/driver_extension_manager/src/bus_extension/usb/usb_dev_subscriber.cpp index 91c4ff561c5aa9f588cd88e9f7e4efe9871d32ca..ddf017d92d7100db721d089756634e95b4bea8f7 100644 --- a/services/native/driver_extension_manager/src/bus_extension/usb/usb_dev_subscriber.cpp +++ b/services/native/driver_extension_manager/src/bus_extension/usb/usb_dev_subscriber.cpp @@ -57,7 +57,15 @@ static uint64_t ToDdkDeviceId(const UsbDev& usbDev) return ((uint64_t)usbDev.busNum << SHIFT_32) + usbDev.devAddr; } - +#ifdef EXTDEVMGR_USB_PASS_THROUGH +void UsbDevSubscriber::Init(shared_ptr callback, sptr iusb, + sptr iUsbDdk) +{ + this->iusb_ = iusb; + this->callback_ = callback; + this->iUsbDdk_ = iUsbDdk; +}; +#else void UsbDevSubscriber::Init(shared_ptr callback, sptr iusb, sptr iUsbDdk) { @@ -65,6 +73,7 @@ void UsbDevSubscriber::Init(shared_ptr callback, sptrcallback_ = callback; this->iUsbDdk_ = iUsbDdk; }; +#endif // EXTDEVMGR_USB_PASS_THROUGH int32_t UsbDevSubscriber::GetInterfaceDescriptor(const UsbDev &usbDev, std::vector &interfaceList) diff --git a/test/unittest/BUILD.gn b/test/unittest/BUILD.gn index 2436df12e4bc493d37c6c381edd04d63455f7312..315e866e9054b7bf007b2ddc8987b42f685302f1 100644 --- a/test/unittest/BUILD.gn +++ b/test/unittest/BUILD.gn @@ -21,9 +21,13 @@ ohos_unittest("bus_extension_usb_test") { "bus_extension_usb_test/src/usb_bus_extension_test.cpp", "bus_extension_usb_test/src/usb_ddk_service_mock.cpp", "bus_extension_usb_test/src/usb_driver_info_test.cpp", - "bus_extension_usb_test/src/usb_impl_mock.cpp", "bus_extension_usb_test/src/usb_subscriber_test.cpp", ] + if (extdevmgr_usb_pass_through) { + sources += [ "bus_extension_usb_test/src/usb_host_impl_mock.cpp" ] + } else { + sources += [ "bus_extension_usb_test/src/usb_impl_mock.cpp" ] + } include_dirs = [ "bus_extension_usb_test/include", "${ext_mgr_path}/frameworks/ddk/usb/", @@ -36,16 +40,24 @@ ohos_unittest("bus_extension_usb_test") { "//third_party/googletest:gmock_main", "//third_party/googletest:gtest_main", ] + defines = [] + if (extdevmgr_usb_pass_through) { + defines += [ "EXTDEVMGR_USB_PASS_THROUGH" ] + } external_deps = [ "bundle_framework:appexecfwk_base", "cJSON:cjson", "c_utils:utils", "drivers_interface_usb:libusb_ddk_proxy_1.1", - "drivers_interface_usb:libusb_proxy_1.0", "hilog:libhilog", "jsoncpp:jsoncpp", "samgr:samgr_proxy", ] + if (extdevmgr_usb_pass_through) { + external_deps += [ "drivers_interface_usb:libusb_proxy_2.0" ] + } else { + external_deps += [ "drivers_interface_usb:libusb_proxy_1.0" ] + } configs = [ "${utils_path}:utils_config" ] } diff --git a/test/unittest/bus_extension_usb_test/include/usb_host_impl_mock.h b/test/unittest/bus_extension_usb_test/include/usb_host_impl_mock.h new file mode 100644 index 0000000000000000000000000000000000000000..e66dcc2daa5cd178fcc31c2d6eeb9cc871799726 --- /dev/null +++ b/test/unittest/bus_extension_usb_test/include/usb_host_impl_mock.h @@ -0,0 +1,116 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef USB_IMPL_MOCK_H +#define USB_IMPL_MOCK_H + +#include +#include + +#include "v2_0/iusb_host_interface.h" +#include "v2_0/usb_types.h" + +using OHOS::HDI::Usb::V2_0::IUsbdBulkCallback; +using OHOS::HDI::Usb::V2_0::IUsbdSubscriber; +using OHOS::HDI::Usb::V2_0::IUsbHostInterface; +using OHOS::HDI::Usb::V2_0::PortInfo; +using OHOS::HDI::Usb::V2_0::UsbCtrlTransfer; +using OHOS::HDI::Usb::V2_0::UsbDev; +using OHOS::HDI::Usb::V2_0::USBDeviceInfo; +using OHOS::HDI::Usb::V2_0::UsbPipe; +using OHOS::HDI::Usb::V2_0::UsbCtrlTransferParams; +using OHOS::HDI::Usb::V2_0::USBTransferInfo; +using OHOS::HDI::Usb::V2_0::IUsbdTransferCallback; +namespace OHOS { +namespace USB { +constexpr int32_t DEV_ADDR_OK = 2; +constexpr int32_t DEV_ADDR_OK_2 = 3; +constexpr int32_t DEV_ADDR_OK_ERR_DESC = 4; +constexpr int32_t DEV_ADDR_OK_NULL_DESC = 5; + +constexpr int32_t BUS_NUM_OK = 6; +constexpr int32_t BUS_NUM_OK_2 = 7; +constexpr int32_t DEV_ADDR_INVALID = 255; +constexpr int32_t BUS_NUM_INVALID = 255; +constexpr int32_t DEFAULT_PORT_ID = 1; +constexpr int32_t RETVAL_INVALID = -1; + +class UsbHostImplMock : public IUsbHostInterface { +public: + UsbHostImplMock(); + ~UsbHostImplMock(); + int32_t SubscriberDeviceEvent(const USBDeviceInfo &info); + int32_t BindUsbdHostSubscriber(const sptr &subscriber) override; + int32_t UnbindUsbdHostSubscriber(const sptr &subscriber) override; + int32_t GetDeviceDescriptor(const UsbDev& dev, std::vector& descriptor) override; + int32_t GetRawDescriptor(const UsbDev &dev, std::vector &decriptor) override; + int32_t GetStringDescriptor(const UsbDev &dev, uint8_t descId, std::vector &decriptor) override; + int32_t GetConfig(const UsbDev &dev, uint8_t &configIndex) override; + MOCK_METHOD1(OpenDevice, int32_t(const UsbDev &dev)); + MOCK_METHOD1(CloseDevice, int32_t(const UsbDev &dev)); + MOCK_METHOD3(GetConfigDescriptor, int32_t(const UsbDev &dev, uint8_t descId, std::vector &decriptor)); + MOCK_METHOD2(GetFileDescriptor, int32_t(const UsbDev &dev, int32_t &fd)); + MOCK_METHOD2(SetConfig, int32_t(const UsbDev &dev, uint8_t configIndex)); + MOCK_METHOD3(ClaimInterface, int32_t(const UsbDev &dev, uint8_t interfaceid, uint8_t force)); + MOCK_METHOD2(ReleaseInterface, int32_t(const UsbDev &dev, uint8_t interfaceid)); + MOCK_METHOD3(SetInterface, int32_t(const UsbDev &dev, uint8_t interfaceid, uint8_t altIndex)); + MOCK_METHOD4( + BulkTransferRead, int32_t(const UsbDev &dev, const UsbPipe &pipe, int32_t timeout, std::vector &data)); + MOCK_METHOD4(BulkTransferWrite, + int32_t(const UsbDev &dev, const UsbPipe &pipe, int32_t timeout, const std::vector &data)); + MOCK_METHOD3( + ControlTransferRead, int32_t(const UsbDev &dev, const UsbCtrlTransfer &ctrl, std::vector &data)); + MOCK_METHOD3(ControlTransferWrite, + int32_t(const UsbDev &dev, const UsbCtrlTransfer &ctrl, const std::vector &data)); + MOCK_METHOD4(InterruptTransferRead, + int32_t(const UsbDev &dev, const UsbPipe &pipe, int32_t timeout, std::vector &data)); + MOCK_METHOD4(InterruptTransferWrite, + int32_t(const UsbDev &dev, const UsbPipe &pipe, int32_t timeout, const std::vector &data)); + MOCK_METHOD4( + IsoTransferRead, int32_t(const UsbDev &dev, const UsbPipe &pipe, int32_t timeout, std::vector &data)); + MOCK_METHOD4(IsoTransferWrite, + int32_t(const UsbDev &dev, const UsbPipe &pipe, int32_t timeout, const std::vector &data)); + MOCK_METHOD4(RequestQueue, + int32_t(const UsbDev &dev, const UsbPipe &pipe, const std::vector &clientData, + const std::vector &buffer)); + MOCK_METHOD4(RequestWait, + int32_t(const UsbDev &dev, std::vector &clientData, std::vector &buffer, int32_t timeout)); + MOCK_METHOD2(RequestCancel, int32_t(const UsbDev &dev, const UsbPipe &pipe)); + MOCK_METHOD1(GetCurrentFunctions, int32_t(int32_t &funcs)); + MOCK_METHOD1(SetCurrentFunctions, int32_t(int32_t funcs)); + MOCK_METHOD3(RegBulkCallback, int32_t(const UsbDev &dev, const UsbPipe &pipe, const sptr &cb)); + MOCK_METHOD2(UnRegBulkCallback, int32_t(const UsbDev &dev, const UsbPipe &pipe)); + MOCK_METHOD3(BulkRead, int32_t(const UsbDev &dev, const UsbPipe &pipe, const sptr &ashmem)); + MOCK_METHOD3(BulkWrite, int32_t(const UsbDev &dev, const UsbPipe &pipe, const sptr &ashmem)); + MOCK_METHOD2(BulkCancel, int32_t(const UsbDev &dev, const UsbPipe &pipe)); + MOCK_METHOD3(ManageInterface, int32_t(const UsbDev &dev, uint8_t interfaceid, bool disable)); + MOCK_METHOD3(GetInterfaceActiveStatus, int32_t(const UsbDev &dev, uint8_t interfaceid, bool &unactivated)); + MOCK_METHOD2(GetDeviceSpeed, int32_t(const UsbDev &dev, uint8_t &speed)); + MOCK_METHOD1(ResetDevice, int32_t(const UsbDev &dev)); + MOCK_METHOD2(ClearHalt, int32_t(const UsbDev &dev, const UsbPipe &pipe)); + MOCK_METHOD2(GetDeviceFileDescriptor, int32_t(const UsbDev &dev, int32_t &fd)); + MOCK_METHOD5(BulkTransferReadwithLength, int32_t(const UsbDev &dev, + const UsbPipe &pipe, int32_t timeout, int32_t length, std::vector &data)); + MOCK_METHOD3(ControlTransferReadwithLength, + int32_t(const UsbDev &dev, const UsbCtrlTransferParams &ctrlParams, std::vector &data)); + MOCK_METHOD4(UsbSubmitTransfer, int32_t(const UsbDev &dev, const USBTransferInfo &info, + const sptr &cb, const sptr &ashmem)); + MOCK_METHOD2(UsbCancelTransfer, int32_t(const UsbDev &dev, const int32_t endpoint)); +private: + sptr subscriber_ = nullptr; +}; +} // namespace USB +} // namespace OHOS +#endif diff --git a/test/unittest/bus_extension_usb_test/src/usb_host_impl_mock.cpp b/test/unittest/bus_extension_usb_test/src/usb_host_impl_mock.cpp new file mode 100644 index 0000000000000000000000000000000000000000..047a2ee52eec576bcafabfc32c0c03c4c0b74320 --- /dev/null +++ b/test/unittest/bus_extension_usb_test/src/usb_host_impl_mock.cpp @@ -0,0 +1,106 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "usb_host_impl_mock.h" + +namespace OHOS { +namespace USB { +using namespace OHOS; +using namespace OHOS::HDI; + +std::vector g_descBuf { + 0x12, 0x01, 0x20, 0x03, 0x00, 0x00, 0x00, 0x09, 0x07, 0x22, 0x18, 0x00, 0x23, 0x02, 0x01, 0x02, 0x03, 0x01, 0x09, + 0x02, 0x5D, 0x00, 0x02, 0x01, 0x04, 0xC0, 0x3E, 0x08, 0x0B, 0x00, 0x02, 0x02, 0x02, 0x01, 0x07, 0x09, 0x04, 0x00, + 0x00, 0x01, 0x02, 0x02, 0x01, 0x05, 0x05, 0x24, 0x00, 0x10, 0x01, 0x05, 0x24, 0x01, 0x00, 0x01, 0x04, 0x24, 0x02, + 0x02, 0x05, 0x24, 0x06, 0x00, 0x01, 0x07, 0x05, 0x81, 0x03, 0x0A, 0x00, 0x09, 0x06, 0x30, 0x00, 0x00, 0x00, 0x00, + 0x09, 0x04, 0x01, 0x00, 0x02, 0x0A, 0x00, 0x02, 0x06, 0x07, 0x05, 0x82, 0x02, 0x00, 0x04, 0x00, 0x06, 0x30, 0x00, + 0x00, 0x00, 0x00, 0x07, 0x05, 0x01, 0x02, 0x00, 0x04, 0x00, 0x06, 0x30, 0x00, 0x00, 0x00, 0x00 +}; + +std::vector g_descBufErr { + 0x10, 0x01, 0x20, 0x03, 0x00, 0x00, 0x00, 0x09, 0x07, 0x22, 0x18, 0x00, 0x23, 0x02, 0x01, 0x02, 0x03, 0x01, 0x09, + 0x02, 0x5D, 0x00, 0x02, 0x01, 0x04, 0xC0, 0x3E, 0x08, 0x0B, 0x00, 0x02, 0x02, 0x02, 0x01, 0x07, 0x09, 0x04, 0x00, + 0x00, 0x01, 0x02, 0x02, 0x01, 0x05, 0x05, 0x24, 0x00, 0x10, 0x01, 0x05, 0x24, 0x01, 0x00, 0x01, 0x04, 0x24, 0x02, + 0x02, 0x05, 0x24, 0x06, 0x00, 0x01, 0x07, 0x05, 0x81, 0x03, 0x0A, 0x00, 0x09, 0x06, 0x30, 0x00, 0x00, 0x00, 0x00, + 0x09, 0x04, 0x01, 0x00, 0x02, 0x0A, 0x00, 0x02, 0x06, 0x07, 0x05, 0x82, 0x02, 0x00, 0x04, 0x00, 0x06, 0x30, 0x00, + 0x00, 0x00, 0x00, 0x07, 0x05, 0x01, 0x02, 0x00, 0x04, 0x00, 0x06, 0x30, 0x00, 0x00, 0x00, 0x00 +}; + +UsbHostImplMock::UsbHostImplMock() { } + +UsbHostImplMock::~UsbHostImplMock() { } + +int32_t UsbHostImplMock::GetRawDescriptor(const UsbDev &dev, std::vector &descriptor) +{ + if ((BUS_NUM_OK != dev.busNum) && (BUS_NUM_OK_2 != dev.busNum)) { + return HDF_DEV_ERR_NO_DEVICE; + } + if ((DEV_ADDR_OK !=dev.devAddr) \ + && (DEV_ADDR_OK_2 != dev.devAddr)\ + && (DEV_ADDR_OK_ERR_DESC != dev.devAddr)\ + && (DEV_ADDR_OK_NULL_DESC != dev.devAddr)\ + ) { + return HDF_DEV_ERR_NO_DEVICE; + } + if (dev.devAddr == DEV_ADDR_OK_ERR_DESC) { + descriptor = g_descBufErr; + } else if (dev.devAddr == DEV_ADDR_OK_NULL_DESC) { + // do nothing + } else { + descriptor = g_descBuf; + } + return HDF_SUCCESS; +} + +int32_t UsbHostImplMock::GetDeviceDescriptor(const UsbDev& dev, std::vector& descriptor) +{ + return GetRawDescriptor(dev, descriptor); +} +int32_t UsbHostImplMock::GetStringDescriptor(const UsbDev &dev, uint8_t descId, std::vector &decriptor) +{ + (void)descId; + if ((BUS_NUM_OK != dev.busNum) || (DEV_ADDR_OK != dev.devAddr)) { + return HDF_DEV_ERR_NO_DEVICE; + } + decriptor = g_descBuf; + return HDF_SUCCESS; +} + +int32_t UsbHostImplMock::BindUsbdHostSubscriber(const sptr &subscriber) +{ + subscriber_ = subscriber; + return HDF_SUCCESS; +} + +int32_t UsbHostImplMock::UnbindUsbdHostSubscriber(const sptr &subscriber) +{ + (void)subscriber; + subscriber_ = nullptr; + return HDF_SUCCESS; +} + +int32_t UsbHostImplMock::SubscriberDeviceEvent(const USBDeviceInfo &info) +{ + auto ret = subscriber_->DeviceEvent(info); + return ret; +} +int32_t UsbHostImplMock::GetConfig(const UsbDev &dev, uint8_t &configIndex) +{ + (void)dev; + configIndex = 1; + return HDF_SUCCESS; +} +} // namespace USB +} // namespace OHOS diff --git a/test/unittest/bus_extension_usb_test/src/usb_subscriber_test.cpp b/test/unittest/bus_extension_usb_test/src/usb_subscriber_test.cpp index 3c4372cdc9c0be601156d7bf2b664ce842758a74..cdbdec43f47971f87173e176b838ea84ce13b6a7 100644 --- a/test/unittest/bus_extension_usb_test/src/usb_subscriber_test.cpp +++ b/test/unittest/bus_extension_usb_test/src/usb_subscriber_test.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023 Huawei Device Co., Ltd. + * Copyright (c) 2023-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -16,7 +16,11 @@ #include "gtest/gtest.h" #include "map" #include "gmock/gmock.h" +#ifdef EXTDEVMGR_USB_PASS_THROUGH +#include "usb_host_impl_mock.h" +#else #include "usb_impl_mock.h" +#endif // EXTDEVMGR_USB_PASS_THROUGH #include "usb_ddk_service_mock.h" #define private public #include "ibus_extension.h" @@ -37,7 +41,11 @@ public: cout << "UsbSubscriberTest SetUp" << endl; busExt = make_shared(); usbBusExt = static_cast(busExt.get()); +#ifdef EXTDEVMGR_USB_PASS_THROUGH + mockUsb = sptr(new UsbHostImplMock()); +#else mockUsb = sptr(new UsbImplMock()); +#endif // EXTDEVMGR_USB_PASS_THROUGH mockUsbDdk = sptr(new UsbDdkServiceMock()); usbBusExt->SetUsbInferface(mockUsb); usbBusExt->SetUsbDdk(mockUsbDdk); @@ -50,7 +58,11 @@ public: } shared_ptr busExt; +#ifdef EXTDEVMGR_USB_PASS_THROUGH + sptr mockUsb; +#else sptr mockUsb; +#endif // EXTDEVMGR_USB_PASS_THROUGH sptr mockUsbDdk; UsbBusExtension *usbBusExt; }; @@ -152,6 +164,7 @@ HWTEST_F(UsbSubscriberTest, UsbDevCallbackErrorTest, TestSize.Level1) EXPECT_EQ(ret, 0); } +#ifndef EXTDEVMGR_USB_PASS_THROUGH HWTEST_F(UsbSubscriberTest, PortChangEeventTest, TestSize.Level1) { int ret = 0; @@ -159,5 +172,6 @@ HWTEST_F(UsbSubscriberTest, PortChangEeventTest, TestSize.Level1) ret = mockUsb->SetPortRole(0, 0, 0); ASSERT_EQ(ret, 0); } +#endif // EXTDEVMGR_USB_PASS_THROUGH } } \ No newline at end of file