• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 
16 #ifndef DEVICE_STATUS_NAPI_H
17 #define DEVICE_STATUS_NAPI_H
18 
19 #include <map>
20 #include <uv.h>
21 
22 #include "napi/native_api.h"
23 #include "napi/native_node_api.h"
24 
25 #include "devicestatus_data_utils.h"
26 #include "devicestatus_callback_stub.h"
27 #include "devicestatus_event.h"
28 
29 namespace OHOS {
30 namespace Msdp {
31 class DeviceStatusCallback : public DevicestatusCallbackStub {
32 public:
DeviceStatusCallback(napi_env env)33     explicit DeviceStatusCallback(napi_env env) : env_(env) {}
~DeviceStatusCallback()34     virtual ~DeviceStatusCallback() {};
35     void OnDevicestatusChanged(const DevicestatusDataUtils::DevicestatusData &devicestatusData) override;
36     static void EmitOnEvent(uv_work_t *work, int status);
37 private:
38     napi_env env_ = { nullptr };
39     std::mutex mutex_;
40     DevicestatusDataUtils::DevicestatusData data_;
41 };
42 
43 class DeviceStatusNapi : public DeviceStatusEvent {
44 public:
45     explicit DeviceStatusNapi(napi_env env);
46     virtual ~DeviceStatusNapi();
47 
48     static napi_value Init(napi_env env, napi_value exports);
49     static napi_value SubscribeDeviceStatus(napi_env env, napi_callback_info info);
50     static napi_value SubscribeDeviceStatusCallback(napi_env env, napi_callback_info info, napi_value *args,
51         int32_t type, int32_t event, int32_t latency);
52     static napi_value UnsubscribeDeviceStatus(napi_env env, napi_callback_info info);
53     static napi_value GetDeviceStatus(napi_env env, napi_callback_info info);
54 
55     static int32_t ConvertTypeToInt(const std::string &type);
56     void OnDeviceStatusChangedDone(int32_t type, int32_t value, bool isOnce);
57     static DeviceStatusNapi* GetDeviceStatusNapi();
58     static std::map<int32_t, sptr<IdevicestatusCallback>> callbackMap_;
59 
60 private:
61     static bool CheckArguments(napi_env env, napi_callback_info info);
62     static bool CheckUnsubArguments(napi_env env, napi_callback_info info);
63     static bool CheckGetArguments(napi_env env, napi_callback_info info);
64     napi_ref callbackRef_ = { nullptr };
65     static napi_ref devicestatusValueRef_;
66     napi_env env_ = { nullptr };
67 };
68 } // namespace Msdp
69 } // namespace OHOS
70 #endif // DEVICESTATUS_NAPI_H
71