1 /* 2 * Copyright (C) 2025 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_SOFTBUS_LINK_ENHANCE_UTILS_H_ 17 #define NAPI_SOFTBUS_LINK_ENHANCE_UTILS_H_ 18 19 #include "napi/native_api.h" 20 #include "napi/native_node_api.h" 21 22 #include "comm_log.h" 23 24 #include <atomic> 25 #include <condition_variable> 26 #include <cstdint> 27 #include <map> 28 #include <mutex> 29 #include <string> 30 #include <vector> 31 32 #include "uv.h" 33 #include "securec.h" 34 #include "softbus_adapter_mem.h" 35 36 namespace Communication { 37 namespace OHOS::Softbus { 38 39 constexpr size_t ARGS_SIZE_ZERO = 0; 40 constexpr size_t ARGS_SIZE_ONE = 1; 41 constexpr size_t ARGS_SIZE_TWO = 2; 42 constexpr size_t ARGS_SIZE_THREE = 3; 43 constexpr size_t ARGS_TYPE_MAX_LEN = 25; 44 constexpr int32_t PARAM0 = 0; 45 constexpr int32_t PARAM1 = 1; 46 constexpr int32_t PARAM2 = 2; 47 48 static const std::string PKG_NAME = "ohos.distributedschedule.dms"; 49 50 #define NAPI_SOFTBUS_CALL_RETURN(func) \ 51 do { \ 52 napi_status ret = (func); \ 53 if (ret != napi_ok) { \ 54 COMM_LOGE(COMM_SDK, "napi call function failed. ret:%{public}d", ret); \ 55 return ret; \ 56 } \ 57 } while (0) 58 59 #define NAPI_SOFTBUS_RETURN_IF(condition, msg, ret) \ 60 do { \ 61 if ((condition)) { \ 62 COMM_LOGE(COMM_SDK, msg); \ 63 return (ret); \ 64 } \ 65 } while (0) 66 67 enum class ConnectionState { 68 STATE_BASE = 0, 69 STATE_CONNECTING = 1, 70 STATE_CONNECTED = 2, 71 STATE_DISCONNECTED = 3, 72 }; 73 74 int DoInJsMainThread(napi_env env, std::function<void(void)> func); 75 76 bool ParseString(napi_env env, std::string ¶m, napi_value args); 77 bool ParseInt32(napi_env env, int32_t ¶m, napi_value args); 78 bool ParseUInt32(napi_env env, uint32_t ¶m, napi_value args); 79 80 napi_value NapiGetNull(napi_env env); 81 napi_value NapiGetUndefinedRet(napi_env env); 82 napi_value NapiGetStringRet(napi_env env, std::string string); 83 napi_value NapiGetInt32Ret(napi_env env, int32_t res); 84 85 void SetNamedPropertyByInteger(napi_env env, napi_value dstObj, int32_t objName, const char *propName); 86 void SetNamedPropertyByString(napi_env env, napi_value dstObj, const std::string &strValue, const char *propName); 87 88 napi_value PropertyInit(napi_env env, napi_value exports); 89 90 void NapiCallFunction(napi_env env, napi_ref callbackRef, napi_value *argv, size_t argc); 91 int32_t ConvertToJsErrcode(int32_t err); 92 void HandleSyncErr(const napi_env &env, int32_t errCode); 93 bool CheckAccessToken(void); 94 } // namespace Softbus 95 } // namespace Communication 96 #endif /* NAPI_SOFTBUS_LINK_ENHANCE_UTILS_H_ */