• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 TYPE_SYSTEM_VIBRATE = 1;
34 constexpr uint32_t STRING_LENGTH_MAX = 64;
35 
36 class AsyncCallbackInfo : public RefBase {
37 public:
38     struct AsyncCallbackError {
39         int32_t code {0};
40         string message;
41         string name;
42         string stack;
43     };
44 
45     napi_env env = nullptr;
46     napi_async_work asyncWork = nullptr;
47     napi_deferred deferred = nullptr;
48     napi_ref callback[CALLBACK_NUM] = {0};
49     AsyncCallbackError error;
50     uint32_t callbackType = 0;
AsyncCallbackInfo(napi_env env)51     AsyncCallbackInfo(napi_env env) : env(env) {}
52     ~AsyncCallbackInfo();
53 };
54 
55 bool IsMatchType(const napi_env &env, const napi_value &value, const napi_valuetype &type);
56 bool GetNapiInt32(const napi_env &env, const int32_t value, napi_value &result);
57 bool GetInt32Value(const napi_env &env, const napi_value &value, int32_t &result);
58 bool GetStringValue(const napi_env &env, const napi_value &value, string &result);
59 bool GetPropertyString(const napi_env &env, const napi_value &value, const std::string &type, std::string &result);
60 bool GetPropertyInt32(const napi_env &env, const napi_value &value, const std::string &type, int32_t &result);
61 bool GetNapiParam(const napi_env &env, const napi_callback_info &info, size_t &argc, napi_value &argv);
62 void EmitAsyncCallbackWork(sptr<AsyncCallbackInfo> async_callback_info);
63 void EmitPromiseWork(sptr<AsyncCallbackInfo> asyncCallbackInfo);
64 }  // namespace Sensors
65 }  // namespace OHOS
66 #endif // VIBRATOR_NAPI_UTILS_H