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