1 /*
2  * Copyright (c) 2022-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 DEVICESTATUS_NAPI_H
17 #define DEVICESTATUS_NAPI_H
18 
19 #include <map>
20 #include <tuple>
21 
22 #include "napi/native_api.h"
23 #include "napi/native_node_api.h"
24 #include <uv.h>
25 
26 #include "devicestatus_callback_stub.h"
27 #include "devicestatus_event.h"
28 #include "stationary_data.h"
29 
30 namespace OHOS {
31 namespace Msdp {
32 namespace DeviceStatus {
33 class DeviceStatusCallback : public DeviceStatusCallbackStub {
34 public:
DeviceStatusCallback(napi_env env)35     explicit DeviceStatusCallback(napi_env env) : env_(env) {}
~DeviceStatusCallback()36     virtual ~DeviceStatusCallback() {};
37     void OnDeviceStatusChanged(const Data &devicestatusData) override;
38     static void EmitOnEvent(uv_work_t *work, int32_t status);
39 private:
40     napi_env env_ { nullptr };
41     std::mutex mutex_;
42     Data data_;
43 };
44 
45 class DeviceStatusNapi : public DeviceStatusEvent {
46 public:
47     explicit DeviceStatusNapi(napi_env env);
48     virtual ~DeviceStatusNapi();
49 
50     static napi_value Init(napi_env env, napi_value exports);
51     static napi_value SubscribeDeviceStatus(napi_env env, napi_callback_info info);
52     static napi_value SubscribeDeviceStatusCallback(napi_env env, napi_callback_info info, napi_value handler,
53         int32_t type, int32_t event, int32_t latency);
54     static napi_value UnsubscribeDeviceStatus(napi_env env, napi_callback_info info);
55     static napi_value GetDeviceStatus(napi_env env, napi_callback_info info);
56     static napi_value EnumActivityEventConstructor(napi_env env, napi_callback_info info);
57     static napi_value DeclareEventTypeInterface(napi_env env, napi_value exports);
58     static int32_t ConvertTypeToInt(const std::string &type);
59     void OnDeviceStatusChangedDone(int32_t type, int32_t value, bool isOnce);
60     static DeviceStatusNapi* GetDeviceStatusNapi();
61 
62     static std::map<int32_t, sptr<IRemoteDevStaCallback>> callbacks_;
63 
64 private:
65     static bool CheckArguments(napi_env env, napi_callback_info info);
66     static bool IsMatchType(napi_env env, napi_value value, napi_valuetype type);
67     static napi_value UnsubscribeCallback(napi_env env, int32_t type, int32_t event);
68     static bool CheckGetArguments(napi_env env, napi_callback_info info);
69     static std::tuple<bool, napi_value, std::string, int32_t, int32_t> CheckSubscribeParam(napi_env env,
70         napi_callback_info info);
71     static std::tuple<bool, napi_value, int32_t> CheckGetParam(napi_env env, napi_callback_info info);
72     static napi_value GetParameters(napi_env env, size_t argc, const napi_value* args);
73 
74     static napi_ref devicestatusValueRef_;
75     napi_env env_ { nullptr };
76 };
77 } // namespace DeviceStatus
78 } // namespace Msdp
79 } // namespace OHOS
80 #endif // DEVICESTATUS_NAPI_H
81