1 /*
2  * Copyright (c) 2021-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 #ifndef USB_ASYNC_CONTEXT_H
16 #define USB_ASYNC_CONTEXT_H
17 
18 #include "napi/native_api.h"
19 #include "napi/native_node_api.h"
20 #include "usb_device_pipe.h"
21 #include "usb_endpoint.h"
22 #include "usb_request.h"
23 #include "usb_accessory.h"
24 
25 namespace OHOS {
26 namespace USB {
27 const int32_t NONE = 0;
28 const int32_t SOURCE = 1;
29 const int32_t SINK = 2;
30 
31 const int32_t HOST = 1;
32 const int32_t DEVICE = 2;
33 
34 const int32_t UFP = 1;
35 const int32_t DFP = 2;
36 const int32_t DRP = 3;
37 const int32_t NUM_MODES = 4;
38 
39 const int32_t USB_REQUEST_TARGET_DEVICE = 0;
40 const int32_t USB_REQUEST_TARGET_INTERFACE = 1;
41 const int32_t USB_REQUEST_TARGET_ENDPOINT = 2;
42 const int32_t USB_REQUEST_TARGET_OTHER = 3;
43 
44 const int32_t USB_REQUEST_TYPE_STANDARD = 0;
45 const int32_t USB_REQUEST_TYPE_CLASS = 1;
46 const int32_t USB_REQUEST_TYPE_VENDOR = 2;
47 
48 const int32_t USB_REQUEST_DIR_TO_DEVICE = 0;
49 const int32_t USB_REQUEST_DIR_FROM_DEVICE = 0x80;
50 
51 const int32_t ACM = 1;
52 const int32_t ECM = 1 << 1;
53 const int32_t HDC = 1 << 2;
54 const int32_t MTP = 1 << 3;
55 const int32_t PTP = 1 << 4;
56 const int32_t RNDIS = 1 << 5;
57 const int32_t MIDI = 1 << 6;
58 const int32_t AUDIO_SOURCE = 1 << 7;
59 const int32_t NCM = 1 << 8;
60 const int32_t STORAGE = 1 << 9;
61 
62 struct USBAsyncContext {
63     napi_env env;
64     napi_async_work work;
65 
66     napi_deferred deferred;
67     napi_status status;
68 };
69 
70 struct USBRightAsyncContext : USBAsyncContext {
71     std::string deviceName;
72     bool hasRight = false;
73 };
74 
75 struct USBAccessoryRightAsyncContext : USBAsyncContext {
76     USBAccessory accessory;
77     bool hasRight = false;
78     int32_t errCode;
79 };
80 
81 struct USBFunctionAsyncContext : USBAsyncContext {
82     int32_t functions;
83     int32_t errCode;
84 };
85 
86 struct USBPortRoleAsyncContext : USBAsyncContext {
87     int32_t portId;
88     int32_t powerRole;
89     int32_t dataRole;
90     int32_t errCode;
91 };
92 
93 struct USBControlTransferAsyncContext : USBAsyncContext {
94     USBDevicePipe pipe;
95     int32_t request;
96     int32_t target;
97     uint32_t reqType;
98     int32_t directon;
99     int32_t value;
100     int32_t index;
101     uint8_t *buffer;
102     uint32_t bufferLength;
103     uint32_t dataSize;
104     int32_t timeOut = 0;
105 };
106 
107 struct USBDeviceControlTransferAsyncContext : USBAsyncContext {
108     USBDevicePipe pipe;
109     uint32_t reqType;
110     int32_t request;
111     int32_t value;
112     int32_t index;
113     int32_t length;
114     uint8_t *buffer;
115     uint32_t bufferLength;
116     uint32_t dataSize;
117     int32_t timeOut = 0;
118 };
119 
120 struct USBBulkTransferAsyncContext : USBAsyncContext {
121     uint8_t *buffer;
122     uint32_t bufferLength;
123     uint32_t dataSize;
124     int32_t timeOut = 0;
125     USBDevicePipe pipe;
126     USBEndpoint endpoint;
127 };
128 } // namespace USB
129 } // namespace OHOS
130 #endif // USB_ASYNC_CONTEXT_H
131