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 JsObjectToInt(const napi_env &env, const napi_value &object, std::string fieldStr, int32_t &fieldRef); 32 static void JsObjectToUint( 33 const napi_env &env, const napi_value &object, const std::string &fieldStr, uint32_t &fieldRef); 34 static bool JsUint8ArrayParse( 35 const napi_env &env, const napi_value &object, uint8_t **uint8Buffer, size_t &bufferSize, size_t &offset); 36 static void Uint8ArrayToJsValue( 37 const napi_env &env, std::vector<uint8_t> &uint8Buffer, size_t bufferSize, napi_value &result); 38 static void SetValueUtf8String(const napi_env &env, std::string fieldStr, std::string str, napi_value &result); 39 static void SetValueInt32(const napi_env &env, std::string fieldStr, const int32_t intValue, napi_value &result); 40 static void SetValueUint32( 41 const napi_env &env, const std::string &fieldStr, const uint32_t uintValue, napi_value &result); 42 static void SetValueBool(const napi_env &env, std::string fieldStr, const bool boolValue, napi_value &result); 43 }; 44 } // namespace USB 45 } // namespace OHOS 46 #endif // NAPI_UTIL_H 47