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 <chrono> 19 #include "napi/native_api.h" 20 #include "napi/native_node_api.h" 21 #include "usb_device_pipe.h" 22 #include "usb_endpoint.h" 23 #include "usb_request.h" 24 #include "usb_accessory.h" 25 26 namespace OHOS { 27 namespace USB { 28 const int32_t NONE = 0; 29 const int32_t SOURCE = 1; 30 const int32_t SINK = 2; 31 32 const int32_t HOST = 1; 33 const int32_t DEVICE = 2; 34 35 const int32_t UFP = 1; 36 const int32_t DFP = 2; 37 const int32_t DRP = 3; 38 const int32_t NUM_MODES = 4; 39 40 const int32_t USB_REQUEST_TARGET_DEVICE = 0; 41 const int32_t USB_REQUEST_TARGET_INTERFACE = 1; 42 const int32_t USB_REQUEST_TARGET_ENDPOINT = 2; 43 const int32_t USB_REQUEST_TARGET_OTHER = 3; 44 45 const int32_t USB_REQUEST_TYPE_STANDARD = 0; 46 const int32_t USB_REQUEST_TYPE_CLASS = 1; 47 const int32_t USB_REQUEST_TYPE_VENDOR = 2; 48 49 const int32_t USB_REQUEST_DIR_TO_DEVICE = 0; 50 const int32_t USB_REQUEST_DIR_FROM_DEVICE = 0x80; 51 52 const int32_t ACM = 1; 53 const int32_t ECM = 1 << 1; 54 const int32_t HDC = 1 << 2; 55 const int32_t MTP = 1 << 3; 56 const int32_t PTP = 1 << 4; 57 const int32_t RNDIS = 1 << 5; 58 const int32_t MIDI = 1 << 6; 59 const int32_t AUDIO_SOURCE = 1 << 7; 60 const int32_t NCM = 1 << 8; 61 const int32_t STORAGE = 1 << 9; 62 63 struct USBAsyncContext { 64 napi_env env; 65 napi_async_work work; 66 67 napi_deferred deferred; 68 napi_status status; 69 }; 70 71 struct USBRightAsyncContext : USBAsyncContext { 72 std::string deviceName; 73 bool hasRight = false; 74 }; 75 76 struct USBAccessoryRightAsyncContext : USBAsyncContext { 77 USBAccessory accessory; 78 bool hasRight = false; 79 int32_t errCode; 80 }; 81 82 struct USBFunctionAsyncContext : USBAsyncContext { 83 int32_t functions; 84 int32_t errCode; 85 }; 86 87 struct USBPortRoleAsyncContext : USBAsyncContext { 88 int32_t portId; 89 int32_t powerRole; 90 int32_t dataRole; 91 int32_t errCode; 92 }; 93 94 struct USBControlTransferAsyncContext : USBAsyncContext { 95 USBDevicePipe pipe; 96 int32_t request; 97 int32_t target; 98 uint32_t reqType; 99 int32_t directon; 100 int32_t value; 101 int32_t index; 102 uint8_t *buffer; 103 uint32_t bufferLength; 104 uint32_t dataSize; 105 int32_t timeOut = 0; 106 }; 107 108 struct USBDeviceControlTransferAsyncContext : USBAsyncContext { 109 USBDevicePipe pipe; 110 uint32_t reqType; 111 int32_t request; 112 int32_t value; 113 int32_t index; 114 int32_t length; 115 uint8_t *buffer; 116 uint32_t bufferLength; 117 uint32_t dataSize; 118 int32_t timeOut = 0; 119 }; 120 121 struct USBBulkTransferAsyncContext : USBAsyncContext { 122 uint8_t *buffer; 123 uint32_t bufferLength; 124 uint32_t dataSize; 125 int32_t timeOut = 0; 126 USBDevicePipe pipe; 127 USBEndpoint endpoint; 128 }; 129 130 struct AsyncCallBackContext { 131 napi_env env{nullptr}; 132 napi_deferred deferred{nullptr}; 133 napi_ref callbackRef{nullptr}; 134 135 int32_t actualLength; 136 int32_t status; 137 138 std::vector<HDI::Usb::V1_2::UsbIsoPacketDescriptor> isoInfo; 139 }; 140 141 struct TimesUse { 142 std::chrono::steady_clock::time_point beginTime; 143 std::chrono::steady_clock::time_point endTime; 144 }; 145 146 struct USBTransferAsyncContext : USBAsyncContext { 147 USBDevicePipe pipe; 148 int32_t endpoint; 149 int32_t flags; 150 int32_t type; 151 int32_t status; 152 int32_t timeOut = 0; 153 int32_t length; 154 int32_t actualLength; 155 size_t bufferLength = 0; 156 sptr<Ashmem> ashmem = nullptr; 157 uint8_t *userData; 158 uint8_t *buffer; 159 uint32_t numIsoPackets; 160 std::string name = "CreateAshmem"; 161 napi_ref callbackRef{nullptr}; 162 }; 163 164 165 } // namespace USB 166 } // namespace OHOS 167 #endif // USB_ASYNC_CONTEXT_H 168