1 /* 2 * Copyright (C) 2021 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 NAPI_UTIL_H 17 #define NAPI_UTIL_H 18 #include "napi/native_api.h" 19 #include "napi/native_node_api.h" 20 #include <string> 21 namespace OHOS { 22 namespace USB { 23 class NapiUtil { 24 public: 25 static void JsValueToString( 26 const napi_env &env, const napi_value &value, const int32_t bufLen, std::string &target); 27 static void JsObjectToString(const napi_env &env, const napi_value &object, std::string fieldStr, 28 const int32_t bufLen, std::string &fieldRef); 29 static bool JsObjectGetProperty( 30 const napi_env &env, const napi_value &object, std::string fieldStr, napi_value &value); 31 static void JsObjectToBool(const napi_env &env, const napi_value &object, std::string fieldStr, bool &fieldRef); 32 static void JsObjectToInt(const napi_env &env, const napi_value &object, std::string fieldStr, int32_t &fieldRef); 33 static void JsObjectToUint( 34 const napi_env &env, const napi_value &object, const std::string &fieldStr, uint32_t &fieldRef); 35 static bool JsObjectToUint32( 36 const napi_env &env, const napi_value &object, const std::string &fieldStr, uint32_t &fieldRef); 37 static bool JsObjectToUint8( 38 const napi_env &env, const napi_value &object, const std::string &fieldStr, uint8_t &fieldRef); 39 static bool JsUint8ArrayParse( 40 const napi_env &env, const napi_value &object, uint8_t **uint8Buffer, size_t &bufferSize, size_t &offset); 41 static void Uint8ArrayToJsValue( 42 const napi_env &env, std::vector<uint8_t> &uint8Buffer, size_t bufferSize, napi_value &result); 43 static void SetValueUtf8String(const napi_env &env, std::string fieldStr, std::string str, napi_value &result); 44 static void SetValueInt32(const napi_env &env, std::string fieldStr, const int32_t intValue, napi_value &result); 45 static void SetValueUint32( 46 const napi_env &env, const std::string &fieldStr, const uint32_t uintValue, napi_value &result); 47 static void SetValueBool(const napi_env &env, std::string fieldStr, const bool boolValue, napi_value &result); 48 }; 49 50 enum class TransferFlagsJs { 51 /** Report short frames as errors */ 52 TRANSFER_SHORT_NOT_OK = 0, 53 54 /** Automatically free transfer buffer */ 55 TRANSFER_FREE_BUFFER = 1, 56 57 /** Automatically free transfer after callback returns */ 58 TRANSFER_FREE_TRANSFER = 2, 59 60 /** Transmissions that are multiples of wMaxPacketSize will add an additional zero packet. */ 61 TRANSFER_ADD_ZERO_PACKET = 3, 62 63 TRANSFER_FLAGS_UNKNOWN = 4 64 }; 65 66 enum class TransferStatusJs { 67 /** Transfer completed */ 68 TRANSFER_COMPLETED = 0, 69 70 /** Transfer failed */ 71 TRANSFER_ERROR = 1, 72 73 /** Transfer timed out */ 74 TRANSFER_TIMED_OUT = 2, 75 76 /** Transfer was cancelled */ 77 TRANSFER_CANCELLED = 3, 78 79 /** For bulk/interrupt endpoints: halt condition detected (endpoint 80 * stalled). For control endpoints: control request not supported. 81 */ 82 TRANSFER_STALL = 4, 83 84 /** Device was disconnected */ 85 TRANSFER_NO_DEVICE = 5, 86 87 /** Device sent more data than requested */ 88 TRANSFER_OVERFLOW = 6, 89 90 TRANSFER_STATUS_UNKNOWN = 7 91 }; 92 93 enum class EndpointTransferTypeJs { 94 /** Control endpoint */ 95 TRANSFER_TYPE_CONTROL = 0x0, 96 97 /** Isochronous endpoint */ 98 TRANSFER_TYPE_ISOCHRONOUS = 0x1, 99 100 /** Bulk endpoint */ 101 TRANSFER_TYPE_BULK = 0x2, 102 103 /** Interrupt endpoint */ 104 TRANSFER_TYPE_INTERRUPT = 0x3, 105 106 TRANSFER_TYPE_UNKNOWN = 0x4 107 }; 108 109 } // namespace USB 110 } // namespace OHOS 111 #endif // NAPI_UTIL_H 112