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
16 #include "usb_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
UsbImplMock()41 UsbImplMock::UsbImplMock() { }
42
~UsbImplMock()43 UsbImplMock::~UsbImplMock() { }
44
GetRawDescriptor(const UsbDev & dev,std::vector<uint8_t> & descriptor)45 int32_t UsbImplMock::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 UsbImplMock::OpenDevice(const UsbDev &dev)
68 {
69 if (dev.busNum == BUS_NUM_ERR && dev.devAddr == DEV_ADDR_ERR) {
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 UsbImplMock::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 UsbImplMock::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
QueryPort(int32_t & portId,int32_t & powerRole,int32_t & dataRole,int32_t & mode)89 int32_t UsbImplMock::QueryPort(int32_t &portId, int32_t &powerRole, int32_t &dataRole, int32_t &mode)
90 {
91 return HDF_SUCCESS;
92 }
93
BindUsbdSubscriber(const sptr<IUsbdSubscriber> & subscriber)94 int32_t UsbImplMock::BindUsbdSubscriber(const sptr<IUsbdSubscriber> &subscriber)
95 {
96 subscriber_ = subscriber;
97 return HDF_SUCCESS;
98 }
99
UnbindUsbdSubscriber(const sptr<IUsbdSubscriber> & subscriber)100 int32_t UsbImplMock::UnbindUsbdSubscriber(const sptr<IUsbdSubscriber> &subscriber)
101 {
102 (void)subscriber;
103 subscriber_ = nullptr;
104 return HDF_SUCCESS;
105 }
106
SetPortRole(int32_t portId,int32_t powerRole,int32_t dataRole)107 int32_t UsbImplMock::SetPortRole(int32_t portId, int32_t powerRole, int32_t dataRole)
108 {
109 PortInfo info;
110 info.dataRole = dataRole;
111 info.portId = portId;
112 info.powerRole = powerRole;
113 return this->subscriber_->PortChangedEvent(info);
114 }
115
SubscriberDeviceEvent(const USBDeviceInfo & info)116 int32_t UsbImplMock::SubscriberDeviceEvent(const USBDeviceInfo &info)
117 {
118 auto ret = subscriber_->DeviceEvent(info);
119 return ret;
120 }
GetConfig(const UsbDev & dev,uint8_t & configIndex)121 int32_t UsbImplMock::GetConfig(const UsbDev &dev, uint8_t &configIndex)
122 {
123 (void)dev;
124 configIndex = 1;
125 return HDF_SUCCESS;
126 }
127 } // namespace USB
128 } // namespace OHOS
129