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 #include <cstdint>
17 #include <cstdio>
18 #include <string>
19 #include "js_native_api.h"
20 #include "js_native_api_types.h"
21 #include "napi/native_common.h"
22 #include "napi/native_api.h"
23 #include "battery_info.h"
24 #include "battery_log.h"
25 
26 using namespace OHOS::PowerMgr;
27 
EnumChargeTypeClassConstructor(napi_env env,napi_callback_info info)28 static napi_value EnumChargeTypeClassConstructor(napi_env env, napi_callback_info info)
29 {
30     napi_value thisArg = nullptr;
31     void* data = nullptr;
32 
33     napi_get_cb_info(env, info, nullptr, nullptr, &thisArg, &data);
34 
35     napi_value global = nullptr;
36     napi_get_global(env, &global);
37 
38     return thisArg;
39 }
40 
CreateEnumChargeType(napi_env env,napi_value exports)41 static napi_value CreateEnumChargeType(napi_env env, napi_value exports)
42 {
43     napi_value none = nullptr;
44     napi_value wired_normal = nullptr;
45     napi_value wired_quick = nullptr;
46     napi_value wired_super_quick = nullptr;
47     napi_value wireless_normal = nullptr;
48     napi_value wireless_quick = nullptr;
49     napi_value wireless_super_quick = nullptr;
50 
51     napi_create_int32(env, (int32_t)ChargeType::NONE, &none);
52     napi_create_int32(env, (int32_t)ChargeType::WIRED_NORMAL, &wired_normal);
53     napi_create_int32(env, (int32_t)ChargeType::WIRED_QUICK, &wired_quick);
54     napi_create_int32(env, (int32_t)ChargeType::WIRED_SUPER_QUICK, &wired_super_quick);
55     napi_create_int32(env, (int32_t)ChargeType::WIRELESS_NORMAL, &wireless_normal);
56     napi_create_int32(env, (int32_t)ChargeType::WIRELESS_QUICK, &wireless_quick);
57     napi_create_int32(env, (int32_t)ChargeType::WIRELESS_SUPER_QUICK, &wireless_super_quick);
58 
59     napi_property_descriptor desc[] = {
60         DECLARE_NAPI_STATIC_PROPERTY("NONE", none),
61         DECLARE_NAPI_STATIC_PROPERTY("WIRED_NORMAL", wired_normal),
62         DECLARE_NAPI_STATIC_PROPERTY("WIRED_QUICK", wired_quick),
63         DECLARE_NAPI_STATIC_PROPERTY("WIRED_SUPER_QUICK", wired_super_quick),
64         DECLARE_NAPI_STATIC_PROPERTY("WIRELESS_NORMAL", wireless_normal),
65         DECLARE_NAPI_STATIC_PROPERTY("WIRELESS_QUICK", wireless_quick),
66         DECLARE_NAPI_STATIC_PROPERTY("WIRELESS_SUPER_QUICK", wireless_super_quick),
67     };
68 
69     napi_value result = nullptr;
70     napi_define_class(env, "ChargeType", NAPI_AUTO_LENGTH, EnumChargeTypeClassConstructor, nullptr,
71         sizeof(desc) / sizeof(*desc), desc, &result);
72 
73     napi_set_named_property(env, exports, "ChargeType", result);
74 
75     return exports;
76 }
77 
78 EXTERN_C_START
79 /*
80  * function for module exports
81  */
ChargeTypeInit(napi_env env,napi_value exports)82 static napi_value ChargeTypeInit(napi_env env, napi_value exports)
83 {
84     BATTERY_HILOGD(COMP_FWK, "Enter");
85     CreateEnumChargeType(env, exports);
86 
87     BATTERY_HILOGD(COMP_FWK, "Success");
88     return exports;
89 }
90 EXTERN_C_END
91 
92 /*
93  * Module definition
94  */
95 static napi_module g_module = {
96     .nm_version = 1,
97     .nm_flags = 0,
98     .nm_filename = "charger",
99     .nm_register_func = ChargeTypeInit,
100     .nm_modname = "charger",
101     .nm_priv = ((void*)0),
102     .reserved = {0}
103 };
104 
105 /*
106  * Module registration
107  */
RegisterModule(void)108 extern "C" __attribute__((constructor)) void RegisterModule(void)
109 {
110     napi_module_register(&g_module);
111 }