1/*
2 * Copyright (c) 2022 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#ifndef OHOS_HDI_USB_V1_0_IUSBINTERFACE_H
17#define OHOS_HDI_USB_V1_0_IUSBINTERFACE_H
18
19#include <stdint.h>
20#include <vector>
21#include <ashmem.h>
22#include <hdf_base.h>
23#include <hdi_base.h>
24#include "usb/v1_0/iusbd_bulk_callback.h"
25#include "usb/v1_0/iusbd_subscriber.h"
26#include "usb/v1_0/usb_types.h"
27
28#ifndef HDI_BUFF_MAX_SIZE
29#define HDI_BUFF_MAX_SIZE (1024 * 200)
30#endif
31
32#ifndef HDI_CHECK_VALUE_RETURN
33#define HDI_CHECK_VALUE_RETURN(lv, compare, rv, ret) do { \
34    if ((lv) compare (rv)) { \
35        return ret; \
36    } \
37} while (false)
38#endif
39
40#ifndef HDI_CHECK_VALUE_RET_GOTO
41#define HDI_CHECK_VALUE_RET_GOTO(lv, compare, rv, ret, value, table) do { \
42    if ((lv) compare (rv)) { \
43        ret = value; \
44        goto table; \
45    } \
46} while (false)
47#endif
48
49namespace OHOS {
50namespace HDI {
51namespace Usb {
52namespace V1_0 {
53using namespace OHOS;
54using namespace OHOS::HDI;
55
56enum {
57    CMD_USB_INTERFACE_GET_VERSION = 0,
58    CMD_USB_INTERFACE_OPEN_DEVICE = 1,
59    CMD_USB_INTERFACE_CLOSE_DEVICE = 2,
60    CMD_USB_INTERFACE_GET_DEVICE_DESCRIPTOR = 3,
61    CMD_USB_INTERFACE_GET_STRING_DESCRIPTOR = 4,
62    CMD_USB_INTERFACE_GET_CONFIG_DESCRIPTOR = 5,
63    CMD_USB_INTERFACE_GET_RAW_DESCRIPTOR = 6,
64    CMD_USB_INTERFACE_GET_FILE_DESCRIPTOR = 7,
65    CMD_USB_INTERFACE_SET_CONFIG = 8,
66    CMD_USB_INTERFACE_GET_CONFIG = 9,
67    CMD_USB_INTERFACE_CLAIM_INTERFACE = 10,
68    CMD_USB_INTERFACE_MANAGE_INTERFACE = 11,
69    CMD_USB_INTERFACE_RELEASE_INTERFACE = 12,
70    CMD_USB_INTERFACE_SET_INTERFACE = 13,
71    CMD_USB_INTERFACE_BULK_TRANSFER_READ = 14,
72    CMD_USB_INTERFACE_BULK_TRANSFER_WRITE = 15,
73    CMD_USB_INTERFACE_CONTROL_TRANSFER_READ = 16,
74    CMD_USB_INTERFACE_CONTROL_TRANSFER_WRITE = 17,
75    CMD_USB_INTERFACE_INTERRUPT_TRANSFER_READ = 18,
76    CMD_USB_INTERFACE_INTERRUPT_TRANSFER_WRITE = 19,
77    CMD_USB_INTERFACE_ISO_TRANSFER_READ = 20,
78    CMD_USB_INTERFACE_ISO_TRANSFER_WRITE = 21,
79    CMD_USB_INTERFACE_REQUEST_QUEUE = 22,
80    CMD_USB_INTERFACE_REQUEST_WAIT = 23,
81    CMD_USB_INTERFACE_REQUEST_CANCEL = 24,
82    CMD_USB_INTERFACE_GET_CURRENT_FUNCTIONS = 25,
83    CMD_USB_INTERFACE_SET_CURRENT_FUNCTIONS = 26,
84    CMD_USB_INTERFACE_SET_PORT_ROLE = 27,
85    CMD_USB_INTERFACE_QUERY_PORT = 28,
86    CMD_USB_INTERFACE_BIND_USBD_SUBSCRIBER = 29,
87    CMD_USB_INTERFACE_UNBIND_USBD_SUBSCRIBER = 30,
88    CMD_USB_INTERFACE_REG_BULK_CALLBACK = 31,
89    CMD_USB_INTERFACE_UN_REG_BULK_CALLBACK = 32,
90    CMD_USB_INTERFACE_BULK_READ = 33,
91    CMD_USB_INTERFACE_BULK_WRITE = 34,
92    CMD_USB_INTERFACE_BULK_CANCEL = 35,
93};
94
95class IUsbInterface : public HdiBase {
96public:
97    DECLARE_HDI_DESCRIPTOR(u"ohos.hdi.usb.v1_0.IUsbInterface");
98
99    virtual ~IUsbInterface() = default;
100
101    static sptr<OHOS::HDI::Usb::V1_0::IUsbInterface> Get(bool isStub = false);
102    static sptr<OHOS::HDI::Usb::V1_0::IUsbInterface> Get(const std::string &serviceName, bool isStub = false);
103
104    virtual int32_t OpenDevice(const OHOS::HDI::Usb::V1_0::UsbDev& dev) = 0;
105
106    virtual int32_t CloseDevice(const OHOS::HDI::Usb::V1_0::UsbDev& dev) = 0;
107
108    virtual int32_t GetDeviceDescriptor(const OHOS::HDI::Usb::V1_0::UsbDev& dev, std::vector<uint8_t>& descriptor) = 0;
109
110    virtual int32_t GetStringDescriptor(const OHOS::HDI::Usb::V1_0::UsbDev& dev, uint8_t descId,
111         std::vector<uint8_t>& descriptor) = 0;
112
113    virtual int32_t GetConfigDescriptor(const OHOS::HDI::Usb::V1_0::UsbDev& dev, uint8_t descId,
114         std::vector<uint8_t>& descriptor) = 0;
115
116    virtual int32_t GetRawDescriptor(const OHOS::HDI::Usb::V1_0::UsbDev& dev, std::vector<uint8_t>& descriptor) = 0;
117
118    virtual int32_t GetFileDescriptor(const OHOS::HDI::Usb::V1_0::UsbDev& dev, int32_t& fd) = 0;
119
120    virtual int32_t SetConfig(const OHOS::HDI::Usb::V1_0::UsbDev& dev, uint8_t configIndex) = 0;
121
122    virtual int32_t GetConfig(const OHOS::HDI::Usb::V1_0::UsbDev& dev, uint8_t& configIndex) = 0;
123
124    virtual int32_t ClaimInterface(const OHOS::HDI::Usb::V1_0::UsbDev& dev, uint8_t interfaceid, uint8_t force) = 0;
125
126    virtual int32_t ManageInterface(const OHOS::HDI::Usb::V1_0::UsbDev& dev, uint8_t interfaceid, bool disable) = 0;
127
128    virtual int32_t ReleaseInterface(const OHOS::HDI::Usb::V1_0::UsbDev& dev, uint8_t interfaceid) = 0;
129
130    virtual int32_t SetInterface(const OHOS::HDI::Usb::V1_0::UsbDev& dev, uint8_t interfaceid, uint8_t altIndex) = 0;
131
132    virtual int32_t BulkTransferRead(const OHOS::HDI::Usb::V1_0::UsbDev& dev, const OHOS::HDI::Usb::V1_0::UsbPipe& pipe,
133         int32_t timeout, std::vector<uint8_t>& data) = 0;
134
135    virtual int32_t BulkTransferWrite(const OHOS::HDI::Usb::V1_0::UsbDev& dev,
136         const OHOS::HDI::Usb::V1_0::UsbPipe& pipe, int32_t timeout, const std::vector<uint8_t>& data) = 0;
137
138    virtual int32_t ControlTransferRead(const OHOS::HDI::Usb::V1_0::UsbDev& dev,
139         const OHOS::HDI::Usb::V1_0::UsbCtrlTransfer& ctrl, std::vector<uint8_t>& data) = 0;
140
141    virtual int32_t ControlTransferWrite(const OHOS::HDI::Usb::V1_0::UsbDev& dev,
142         const OHOS::HDI::Usb::V1_0::UsbCtrlTransfer& ctrl, const std::vector<uint8_t>& data) = 0;
143
144    virtual int32_t InterruptTransferRead(const OHOS::HDI::Usb::V1_0::UsbDev& dev,
145         const OHOS::HDI::Usb::V1_0::UsbPipe& pipe, int32_t timeout, std::vector<uint8_t>& data) = 0;
146
147    virtual int32_t InterruptTransferWrite(const OHOS::HDI::Usb::V1_0::UsbDev& dev,
148         const OHOS::HDI::Usb::V1_0::UsbPipe& pipe, int32_t timeout, const std::vector<uint8_t>& data) = 0;
149
150    virtual int32_t IsoTransferRead(const OHOS::HDI::Usb::V1_0::UsbDev& dev, const OHOS::HDI::Usb::V1_0::UsbPipe& pipe,
151         int32_t timeout, std::vector<uint8_t>& data) = 0;
152
153    virtual int32_t IsoTransferWrite(const OHOS::HDI::Usb::V1_0::UsbDev& dev, const OHOS::HDI::Usb::V1_0::UsbPipe& pipe,
154         int32_t timeout, const std::vector<uint8_t>& data) = 0;
155
156    virtual int32_t RequestQueue(const OHOS::HDI::Usb::V1_0::UsbDev& dev, const OHOS::HDI::Usb::V1_0::UsbPipe& pipe,
157         const std::vector<uint8_t>& clientData, const std::vector<uint8_t>& buffer) = 0;
158
159    virtual int32_t RequestWait(const OHOS::HDI::Usb::V1_0::UsbDev& dev, std::vector<uint8_t>& clientData,
160         std::vector<uint8_t>& buffer, int32_t timeout) = 0;
161
162    virtual int32_t RequestCancel(const OHOS::HDI::Usb::V1_0::UsbDev& dev,
163         const OHOS::HDI::Usb::V1_0::UsbPipe& pipe) = 0;
164
165    virtual int32_t GetCurrentFunctions(int32_t& funcs) = 0;
166
167    virtual int32_t SetCurrentFunctions(int32_t funcs) = 0;
168
169    virtual int32_t SetPortRole(int32_t portId, int32_t powerRole, int32_t dataRole) = 0;
170
171    virtual int32_t QueryPort(int32_t& portId, int32_t& powerRole, int32_t& dataRole, int32_t& mode) = 0;
172
173    virtual int32_t BindUsbdSubscriber(const sptr<OHOS::HDI::Usb::V1_0::IUsbdSubscriber>& subscriber) = 0;
174
175    virtual int32_t UnbindUsbdSubscriber(const sptr<OHOS::HDI::Usb::V1_0::IUsbdSubscriber>& subscriber) = 0;
176
177    virtual int32_t RegBulkCallback(const OHOS::HDI::Usb::V1_0::UsbDev& dev, const OHOS::HDI::Usb::V1_0::UsbPipe& pipe,
178         const sptr<OHOS::HDI::Usb::V1_0::IUsbdBulkCallback>& cb) = 0;
179
180    virtual int32_t UnRegBulkCallback(const OHOS::HDI::Usb::V1_0::UsbDev& dev,
181         const OHOS::HDI::Usb::V1_0::UsbPipe& pipe) = 0;
182
183    virtual int32_t BulkRead(const OHOS::HDI::Usb::V1_0::UsbDev& dev, const OHOS::HDI::Usb::V1_0::UsbPipe& pipe,
184         const sptr<Ashmem>& ashmem) = 0;
185
186    virtual int32_t BulkWrite(const OHOS::HDI::Usb::V1_0::UsbDev& dev, const OHOS::HDI::Usb::V1_0::UsbPipe& pipe,
187         const sptr<Ashmem>& ashmem) = 0;
188
189    virtual int32_t BulkCancel(const OHOS::HDI::Usb::V1_0::UsbDev& dev, const OHOS::HDI::Usb::V1_0::UsbPipe& pipe) = 0;
190
191    virtual int32_t GetVersion(uint32_t& majorVer, uint32_t& minorVer)
192    {
193        majorVer = 1;
194        minorVer = 0;
195        return HDF_SUCCESS;
196    }
197
198    virtual bool IsProxy()
199    {
200        return false;
201    }
202
203    virtual const std::u16string GetDesc()
204    {
205        return metaDescriptor_;
206    }
207};
208} // V1_0
209} // Usb
210} // HDI
211} // OHOS
212
213#endif // OHOS_HDI_USB_V1_0_IUSBINTERFACE_H
214
215