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