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 ) {
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
GetDeviceDescriptor(const UsbDev & dev,std::vector<uint8_t> & descriptor)67 int32_t UsbHostImplMock::GetDeviceDescriptor(const UsbDev& dev, std::vector<uint8_t>& descriptor)
68 {
69 return GetRawDescriptor(dev, descriptor);
70 }
GetStringDescriptor(const UsbDev & dev,uint8_t descId,std::vector<uint8_t> & decriptor)71 int32_t UsbHostImplMock::GetStringDescriptor(const UsbDev &dev, uint8_t descId, std::vector<uint8_t> &decriptor)
72 {
73 (void)descId;
74 if ((BUS_NUM_OK != dev.busNum) || (DEV_ADDR_OK != dev.devAddr)) {
75 return HDF_DEV_ERR_NO_DEVICE;
76 }
77 decriptor = g_descBuf;
78 return HDF_SUCCESS;
79 }
80
BindUsbdHostSubscriber(const sptr<IUsbdSubscriber> & subscriber)81 int32_t UsbHostImplMock::BindUsbdHostSubscriber(const sptr<IUsbdSubscriber> &subscriber)
82 {
83 subscriber_ = subscriber;
84 return HDF_SUCCESS;
85 }
86
UnbindUsbdHostSubscriber(const sptr<IUsbdSubscriber> & subscriber)87 int32_t UsbHostImplMock::UnbindUsbdHostSubscriber(const sptr<IUsbdSubscriber> &subscriber)
88 {
89 (void)subscriber;
90 subscriber_ = nullptr;
91 return HDF_SUCCESS;
92 }
93
SubscriberDeviceEvent(const USBDeviceInfo & info)94 int32_t UsbHostImplMock::SubscriberDeviceEvent(const USBDeviceInfo &info)
95 {
96 auto ret = subscriber_->DeviceEvent(info);
97 return ret;
98 }
GetConfig(const UsbDev & dev,uint8_t & configIndex)99 int32_t UsbHostImplMock::GetConfig(const UsbDev &dev, uint8_t &configIndex)
100 {
101 (void)dev;
102 configIndex = 1;
103 return HDF_SUCCESS;
104 }
105 } // namespace USB
106 } // namespace OHOS
107