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#ifndef OHOS_HDI_USB_DDK_V1_0_IUSBDDK_H
17#define OHOS_HDI_USB_DDK_V1_0_IUSBDDK_H
18
19#include <stdint.h>
20#include <vector>
21#include <hdf_base.h>
22#include <hdi_base.h>
23#include "usb/ddk/v1_0/usb_ddk_types.h"
24
25#ifndef HDI_BUFF_MAX_SIZE
26#define HDI_BUFF_MAX_SIZE (1024 * 200)
27#endif
28
29#ifndef HDI_CHECK_VALUE_RETURN
30#define HDI_CHECK_VALUE_RETURN(lv, compare, rv, ret) do { \
31    if ((lv) compare (rv)) { \
32        return ret; \
33    } \
34} while (false)
35#endif
36
37#ifndef HDI_CHECK_VALUE_RET_GOTO
38#define HDI_CHECK_VALUE_RET_GOTO(lv, compare, rv, ret, value, table) do { \
39    if ((lv) compare (rv)) { \
40        ret = value; \
41        goto table; \
42    } \
43} while (false)
44#endif
45
46namespace OHOS {
47namespace HDI {
48namespace Usb {
49namespace Ddk {
50namespace V1_0 {
51using namespace OHOS;
52using namespace OHOS::HDI;
53
54enum {
55    CMD_USB_DDK_GET_VERSION = 0,
56    CMD_USB_DDK_INIT = 1,
57    CMD_USB_DDK_RELEASE = 2,
58    CMD_USB_DDK_GET_DEVICE_DESCRIPTOR = 3,
59    CMD_USB_DDK_GET_CONFIG_DESCRIPTOR = 4,
60    CMD_USB_DDK_CLAIM_INTERFACE = 5,
61    CMD_USB_DDK_RELEASE_INTERFACE = 6,
62    CMD_USB_DDK_SELECT_INTERFACE_SETTING = 7,
63    CMD_USB_DDK_GET_CURRENT_INTERFACE_SETTING = 8,
64    CMD_USB_DDK_SEND_CONTROL_READ_REQUEST = 9,
65    CMD_USB_DDK_SEND_CONTROL_WRITE_REQUEST = 10,
66    CMD_USB_DDK_SEND_PIPE_REQUEST = 11,
67    CMD_USB_DDK_GET_DEVICE_MEM_MAP_FD = 12,
68};
69
70class IUsbDdk : public HdiBase {
71public:
72    DECLARE_HDI_DESCRIPTOR(u"ohos.hdi.usb.ddk.v1_0.IUsbDdk");
73
74    virtual ~IUsbDdk() = default;
75
76    static sptr<OHOS::HDI::Usb::Ddk::V1_0::IUsbDdk> Get(bool isStub = false);
77    static sptr<OHOS::HDI::Usb::Ddk::V1_0::IUsbDdk> Get(const std::string &serviceName, bool isStub = false);
78
79    virtual int32_t Init() = 0;
80
81    virtual int32_t Release() = 0;
82
83    virtual int32_t GetDeviceDescriptor(uint64_t deviceId, OHOS::HDI::Usb::Ddk::V1_0::UsbDeviceDescriptor& desc) = 0;
84
85    virtual int32_t GetConfigDescriptor(uint64_t deviceId, uint8_t configIndex, std::vector<uint8_t>& configDesc) = 0;
86
87    virtual int32_t ClaimInterface(uint64_t deviceId, uint8_t interfaceIndex, uint64_t& interfaceHandle) = 0;
88
89    virtual int32_t ReleaseInterface(uint64_t interfaceHandle) = 0;
90
91    virtual int32_t SelectInterfaceSetting(uint64_t interfaceHandle, uint8_t settingIndex) = 0;
92
93    virtual int32_t GetCurrentInterfaceSetting(uint64_t interfaceHandle, uint8_t& settingIndex) = 0;
94
95    virtual int32_t SendControlReadRequest(uint64_t interfaceHandle,
96         const OHOS::HDI::Usb::Ddk::V1_0::UsbControlRequestSetup& setup, uint32_t timeout, std::vector<uint8_t>& data) = 0;
97
98    virtual int32_t SendControlWriteRequest(uint64_t interfaceHandle,
99         const OHOS::HDI::Usb::Ddk::V1_0::UsbControlRequestSetup& setup, uint32_t timeout, const std::vector<uint8_t>& data) = 0;
100
101    virtual int32_t SendPipeRequest(const OHOS::HDI::Usb::Ddk::V1_0::UsbRequestPipe& pipe, uint32_t size,
102         uint32_t offset, uint32_t length, uint32_t& transferedLength) = 0;
103
104    virtual int32_t GetDeviceMemMapFd(uint64_t deviceId, int& fd) = 0;
105
106    virtual int32_t GetVersion(uint32_t& majorVer, uint32_t& minorVer)
107    {
108        majorVer = 1;
109        minorVer = 0;
110        return HDF_SUCCESS;
111    }
112
113    virtual bool IsProxy()
114    {
115        return false;
116    }
117
118    virtual const std::u16string GetDesc()
119    {
120        return metaDescriptor_;
121    }
122};
123} // V1_0
124} // Ddk
125} // Usb
126} // HDI
127} // OHOS
128
129#endif // OHOS_HDI_USB_DDK_V1_0_IUSBDDK_H
130
131