1 /* 2 * Copyright (c) 2023 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 VIBRATOR_CONVERT_NAPI_UTILS_H 17 #define VIBRATOR_CONVERT_NAPI_UTILS_H 18 19 #include <cstring> 20 #include <iostream> 21 #include <map> 22 #include <optional> 23 #include <vector> 24 25 #include "napi/native_api.h" 26 #include "napi/native_node_api.h" 27 #include "refbase.h" 28 29 #include "sensors_errors.h" 30 #include "vibration_convert_type.h" 31 32 namespace OHOS { 33 namespace Sensors { 34 constexpr int32_t CALLBACK_NUM = 1; 35 36 enum CallbackType { 37 AUDIO_ATTRIBUTE_CALLBACK = 1, 38 AUDIO_DATA_CALLBACK, 39 AUDIO_TO_HAPTIC_CALLBACK, 40 }; 41 42 class AsyncCallbackInfo : public RefBase { 43 public: 44 struct AsyncCallbackError { 45 int32_t code { 0 }; 46 std::string message; 47 }; 48 49 napi_env env = nullptr; 50 napi_async_work asyncWork = nullptr; 51 napi_deferred deferred = nullptr; 52 napi_ref callback[CALLBACK_NUM] = { 0 }; 53 AudioAttribute audioAttribute; 54 AudioData audioData; 55 AsyncCallbackError error; 56 CallbackType callbackType { AUDIO_ATTRIBUTE_CALLBACK }; 57 int32_t eventCount { 0 }; 58 std::vector<HapticEvent> hapticEvents; AsyncCallbackInfo(napi_env env)59 explicit AsyncCallbackInfo(napi_env env) : env(env) {} 60 ~AsyncCallbackInfo(); 61 }; 62 63 using ConstructResultFunc = bool(*)(const napi_env &env, sptr<AsyncCallbackInfo> asyncCallbackInfo, 64 napi_value result[], int32_t length); 65 66 bool IsMatchNapiType(const napi_env &env, const napi_value &value, const napi_valuetype &type); 67 bool GetInt32Value(const napi_env &env, const napi_value &value, int32_t &result); 68 bool GetInt64Value(const napi_env &env, const napi_value &value, int64_t &result); 69 bool GetPropertyInt32(const napi_env &env, const napi_value &value, const std::string &type, int32_t &result); 70 bool GetPropertyInt64(const napi_env &env, const napi_value &value, const std::string &type, int64_t &result); 71 bool ConvertErrorToResult(const napi_env &env, sptr<AsyncCallbackInfo> asyncCallbackInfo, napi_value &result); 72 bool GetAudioAttributeResult(const napi_env &env, sptr<AsyncCallbackInfo> asyncCallbackInfo, 73 napi_value result[], int32_t length); 74 bool GetAudioDataResult(const napi_env &env, sptr<AsyncCallbackInfo> asyncCallbackInfo, 75 napi_value result[], int32_t length); 76 bool ConvertAudioToHapticResult(const napi_env &env, sptr<AsyncCallbackInfo> asyncCallbackInfo, 77 napi_value result[], int32_t length); 78 bool CreateArray(const napi_env &env, float *data, int32_t dataLength, napi_value &result); 79 napi_value GetResultInfo(sptr<AsyncCallbackInfo> asyncCallbackInfo); 80 napi_value GetAudioDataInfo(sptr<AsyncCallbackInfo> asyncCallbackInfo); 81 napi_value GetAudioToHapticInfo(sptr<AsyncCallbackInfo> asyncCallbackInfo); 82 void EmitHapticAsyncCallbackWork(sptr<AsyncCallbackInfo> async_callback_info); 83 void EmitHapticPromiseWork(sptr<AsyncCallbackInfo> asyncCallbackInfo); 84 int64_t GetFileSize(int32_t fd); 85 } // namespace Sensors 86 } // namespace OHOS 87 #endif // VIBRATOR_CONVERT_NAPI_UTILS_H