1 /*
2  * Copyright (c) 2021-2022 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 #ifndef VIBRATOR_NAPI_UTILS_H
16 #define VIBRATOR_NAPI_UTILS_H
17 
18 #include <cstring>
19 #include <iostream>
20 #include <map>
21 #include <optional>
22 
23 #include "napi/native_api.h"
24 #include "napi/native_node_api.h"
25 #include "refbase.h"
26 
27 #include "sensors_errors.h"
28 
29 namespace OHOS {
30 namespace Sensors {
31 using std::string;
32 constexpr int32_t CALLBACK_NUM = 3;
33 constexpr uint32_t STRING_LENGTH_MAX = 64;
34 
35 enum CallbackType {
36     SYSTEM_VIBRATE_CALLBACK = 1,
37     COMMON_CALLBACK,
38     IS_SUPPORT_EFFECT_CALLBACK,
39 };
40 
41 class AsyncCallbackInfo : public RefBase {
42 public:
43     struct AsyncCallbackError {
44         int32_t code {0};
45         string message;
46         string name;
47         string stack;
48     };
49 
50     napi_env env = nullptr;
51     napi_async_work asyncWork = nullptr;
52     napi_deferred deferred = nullptr;
53     napi_ref callback[CALLBACK_NUM] = {0};
54     AsyncCallbackError error;
55     CallbackType callbackType = COMMON_CALLBACK;
56     bool isSupportEffect {false};
AsyncCallbackInfo(napi_env env)57     AsyncCallbackInfo(napi_env env) : env(env) {}
58     ~AsyncCallbackInfo();
59 };
60 
61 using ConstructResultFunc = bool(*)(const napi_env &env, sptr<AsyncCallbackInfo> asyncCallbackInfo,
62     napi_value result[], int32_t length);
63 
64 bool IsMatchType(const napi_env &env, const napi_value &value, const napi_valuetype &type);
65 bool GetNapiInt32(const napi_env &env, const int32_t value, napi_value &result);
66 bool GetInt32Value(const napi_env &env, const napi_value &value, int32_t &result);
67 bool GetInt64Value(const napi_env &env, const napi_value &value, int64_t &result);
68 bool GetStringValue(const napi_env &env, const napi_value &value, string &result);
69 bool GetPropertyBool(const napi_env &env, const napi_value &value, const std::string &type, bool &result);
70 bool GetPropertyItem(const napi_env &env, const napi_value &value, const std::string &type, napi_value &item);
71 bool GetPropertyString(const napi_env &env, const napi_value &value, const std::string &type, std::string &result);
72 bool GetPropertyInt32(const napi_env &env, const napi_value &value, const std::string &type, int32_t &result);
73 bool GetPropertyInt64(const napi_env &env, const napi_value &value, const std::string &type, int64_t &result);
74 bool GetNapiParam(const napi_env &env, const napi_callback_info &info, size_t &argc, napi_value &argv);
75 bool ConvertErrorToResult(const napi_env &env, sptr<AsyncCallbackInfo> asyncCallbackInfo, napi_value &result);
76 bool ConstructCommonResult(const napi_env &env, sptr<AsyncCallbackInfo> asyncCallbackInfo, napi_value result[],
77     int32_t length);
78 bool ConstructIsSupportEffectResult(const napi_env &env, sptr<AsyncCallbackInfo> asyncCallbackInfo,
79     napi_value result[], int32_t length);
80 void EmitAsyncCallbackWork(sptr<AsyncCallbackInfo> async_callback_info);
81 void EmitPromiseWork(sptr<AsyncCallbackInfo> asyncCallbackInfo);
82 } // namespace Sensors
83 } // namespace OHOS
84 #endif // VIBRATOR_NAPI_UTILS_H