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 ) {
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 UsbImplMock::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 UsbImplMock::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
QueryPort(int32_t & portId,int32_t & powerRole,int32_t & dataRole,int32_t & mode)81 int32_t UsbImplMock::QueryPort(int32_t &portId, int32_t &powerRole, int32_t &dataRole, int32_t &mode)
82 {
83 return HDF_SUCCESS;
84 }
85
BindUsbdSubscriber(const sptr<IUsbdSubscriber> & subscriber)86 int32_t UsbImplMock::BindUsbdSubscriber(const sptr<IUsbdSubscriber> &subscriber)
87 {
88 subscriber_ = subscriber;
89 return HDF_SUCCESS;
90 }
91
UnbindUsbdSubscriber(const sptr<IUsbdSubscriber> & subscriber)92 int32_t UsbImplMock::UnbindUsbdSubscriber(const sptr<IUsbdSubscriber> &subscriber)
93 {
94 (void)subscriber;
95 subscriber_ = nullptr;
96 return HDF_SUCCESS;
97 }
98
SetPortRole(int32_t portId,int32_t powerRole,int32_t dataRole)99 int32_t UsbImplMock::SetPortRole(int32_t portId, int32_t powerRole, int32_t dataRole)
100 {
101 PortInfo info;
102 info.dataRole = dataRole;
103 info.portId = portId;
104 info.powerRole = powerRole;
105 return this->subscriber_->PortChangedEvent(info);
106 }
107
SubscriberDeviceEvent(const USBDeviceInfo & info)108 int32_t UsbImplMock::SubscriberDeviceEvent(const USBDeviceInfo &info)
109 {
110 auto ret = subscriber_->DeviceEvent(info);
111 return ret;
112 }
113 } // namespace USB
114 } // namespace OHOS
115