1 /*
2 * Copyright (c) 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
16 #include "usb_host_impl_mock.h"
17
18 namespace OHOS {
19 namespace USB {
20 using namespace OHOS;
21 using namespace OHOS::HDI;
22
23 std::vector<uint8_t> g_descBuf {
24 0x12, 0x01, 0x20, 0x03, 0x00, 0x00, 0x00, 0x09, 0x07, 0x22, 0x18, 0x00, 0x23, 0x02, 0x01, 0x02, 0x03, 0x01, 0x09,
25 0x02, 0x5D, 0x00, 0x02, 0x01, 0x04, 0xC0, 0x3E, 0x08, 0x0B, 0x00, 0x02, 0x02, 0x02, 0x01, 0x07, 0x09, 0x04, 0x00,
26 0x00, 0x01, 0x02, 0x02, 0x01, 0x05, 0x05, 0x24, 0x00, 0x10, 0x01, 0x05, 0x24, 0x01, 0x00, 0x01, 0x04, 0x24, 0x02,
27 0x02, 0x05, 0x24, 0x06, 0x00, 0x01, 0x07, 0x05, 0x81, 0x03, 0x0A, 0x00, 0x09, 0x06, 0x30, 0x00, 0x00, 0x00, 0x00,
28 0x09, 0x04, 0x01, 0x00, 0x02, 0x0A, 0x00, 0x02, 0x06, 0x07, 0x05, 0x82, 0x02, 0x00, 0x04, 0x00, 0x06, 0x30, 0x00,
29 0x00, 0x00, 0x00, 0x07, 0x05, 0x01, 0x02, 0x00, 0x04, 0x00, 0x06, 0x30, 0x00, 0x00, 0x00, 0x00
30 };
31
32 std::vector<uint8_t> g_descBufErr {
33 0x10, 0x01, 0x20, 0x03, 0x00, 0x00, 0x00, 0x09, 0x07, 0x22, 0x18, 0x00, 0x23, 0x02, 0x01, 0x02, 0x03, 0x01, 0x09,
34 0x02, 0x5D, 0x00, 0x02, 0x01, 0x04, 0xC0, 0x3E, 0x08, 0x0B, 0x00, 0x02, 0x02, 0x02, 0x01, 0x07, 0x09, 0x04, 0x00,
35 0x00, 0x01, 0x02, 0x02, 0x01, 0x05, 0x05, 0x24, 0x00, 0x10, 0x01, 0x05, 0x24, 0x01, 0x00, 0x01, 0x04, 0x24, 0x02,
36 0x02, 0x05, 0x24, 0x06, 0x00, 0x01, 0x07, 0x05, 0x81, 0x03, 0x0A, 0x00, 0x09, 0x06, 0x30, 0x00, 0x00, 0x00, 0x00,
37 0x09, 0x04, 0x01, 0x00, 0x02, 0x0A, 0x00, 0x02, 0x06, 0x07, 0x05, 0x82, 0x02, 0x00, 0x04, 0x00, 0x06, 0x30, 0x00,
38 0x00, 0x00, 0x00, 0x07, 0x05, 0x01, 0x02, 0x00, 0x04, 0x00, 0x06, 0x30, 0x00, 0x00, 0x00, 0x00
39 };
40
UsbHostImplMock()41 UsbHostImplMock::UsbHostImplMock() { }
42
~UsbHostImplMock()43 UsbHostImplMock::~UsbHostImplMock() { }
44
GetRawDescriptor(const UsbDev & dev,std::vector<uint8_t> & descriptor)45 int32_t UsbHostImplMock::GetRawDescriptor(const UsbDev &dev, std::vector<uint8_t> &descriptor)
46 {
47 if ((BUS_NUM_OK != dev.busNum) && (BUS_NUM_OK_2 != dev.busNum)) {
48 return HDF_DEV_ERR_NO_DEVICE;
49 }
50 if ((DEV_ADDR_OK !=dev.devAddr) \
51 && (DEV_ADDR_OK_2 != dev.devAddr)\
52 && (DEV_ADDR_OK_ERR_DESC != dev.devAddr)\
53 && (DEV_ADDR_OK_NULL_DESC != dev.devAddr)\
54 && (DEV_ADDR_INTERFACE_ERR != dev.devAddr)) {
55 return HDF_DEV_ERR_NO_DEVICE;
56 }
57 if (dev.devAddr == DEV_ADDR_OK_ERR_DESC) {
58 descriptor = g_descBufErr;
59 } else if (dev.devAddr == DEV_ADDR_OK_NULL_DESC) {
60 // do nothing
61 } else {
62 descriptor = g_descBuf;
63 }
64 return HDF_SUCCESS;
65 }
66
OpenDevice(const UsbDev & dev)67 int32_t UsbHostImplMock::OpenDevice(const UsbDev &dev)
68 {
69 if (BUS_NUM_ERR == dev.busNum && DEV_ADDR_ERR == dev.devAddr) {
70 return HDF_DEV_ERR_NO_DEVICE;
71 }
72 return HDF_SUCCESS;
73 }
74
GetDeviceDescriptor(const UsbDev & dev,std::vector<uint8_t> & descriptor)75 int32_t UsbHostImplMock::GetDeviceDescriptor(const UsbDev& dev, std::vector<uint8_t>& descriptor)
76 {
77 return GetRawDescriptor(dev, descriptor);
78 }
GetStringDescriptor(const UsbDev & dev,uint8_t descId,std::vector<uint8_t> & decriptor)79 int32_t UsbHostImplMock::GetStringDescriptor(const UsbDev &dev, uint8_t descId, std::vector<uint8_t> &decriptor)
80 {
81 (void)descId;
82 if ((BUS_NUM_OK != dev.busNum) || (DEV_ADDR_OK != dev.devAddr)) {
83 return HDF_DEV_ERR_NO_DEVICE;
84 }
85 decriptor = g_descBuf;
86 return HDF_SUCCESS;
87 }
88
BindUsbdHostSubscriber(const sptr<IUsbdSubscriber> & subscriber)89 int32_t UsbHostImplMock::BindUsbdHostSubscriber(const sptr<IUsbdSubscriber> &subscriber)
90 {
91 subscriber_ = subscriber;
92 return HDF_SUCCESS;
93 }
94
UnbindUsbdHostSubscriber(const sptr<IUsbdSubscriber> & subscriber)95 int32_t UsbHostImplMock::UnbindUsbdHostSubscriber(const sptr<IUsbdSubscriber> &subscriber)
96 {
97 (void)subscriber;
98 subscriber_ = nullptr;
99 return HDF_SUCCESS;
100 }
101
SubscriberDeviceEvent(const USBDeviceInfo & info)102 int32_t UsbHostImplMock::SubscriberDeviceEvent(const USBDeviceInfo &info)
103 {
104 auto ret = subscriber_->DeviceEvent(info);
105 return ret;
106 }
GetConfig(const UsbDev & dev,uint8_t & configIndex)107 int32_t UsbHostImplMock::GetConfig(const UsbDev &dev, uint8_t &configIndex)
108 {
109 (void)dev;
110 configIndex = 1;
111 return HDF_SUCCESS;
112 }
113 } // namespace USB
114 } // namespace OHOS
115