1 /*
2 * Copyright (C) 2024 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 #include "napi/native_api.h"
16 #include "BasicServicesKit/ohbattery_info.h"
17 #include "native_common.h"
18 #include "hilog/log.h"
19 #include <string>
20
Add(napi_env env,napi_callback_info info)21 static napi_value Add(napi_env env, napi_callback_info info)
22 {
23 OH_LOG_Print(LOG_APP, LOG_ERROR, 0, "testLog", "func called");
24 size_t argc = 0;
25 napi_value args[2] = {nullptr};
26
27 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
28 int32_t value = OH_BatteryInfo_GetCapacity();
29 int value2 = OH_BatteryInfo_GetPluggedType();
30 OH_LOG_Print(LOG_APP, LOG_ERROR, 0, "testLog", "get value = %{public}d, %{public}d", value, value2);
31 napi_value ret;
32 napi_create_int32(env, value, &ret);
33 return ret;
34 }
35
GetCapacity(napi_env env,napi_callback_info info)36 static napi_value GetCapacity(napi_env env, napi_callback_info info)
37 {
38 OH_LOG_Print(LOG_APP, LOG_ERROR, 0, "testLog", "GetCapacity func called");
39 size_t argc = 0;
40 napi_value args[2] = {nullptr};
41
42 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
43 int32_t value = OH_BatteryInfo_GetCapacity();
44 OH_LOG_Print(LOG_APP, LOG_ERROR, 0, "testLog", "get value = %{public}d", value);
45 napi_value ret;
46 napi_create_int32(env, value, &ret);
47 return ret;
48 }
49
GetPluggedType(napi_env env,napi_callback_info info)50 static napi_value GetPluggedType(napi_env env, napi_callback_info info)
51 {
52 OH_LOG_Print(LOG_APP, LOG_ERROR, 0, "testLog", "GetPluggedType func called");
53 size_t argc = 0;
54 napi_value args[2] = {nullptr};
55
56 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
57 BatteryInfo_BatteryPluggedType type = OH_BatteryInfo_GetPluggedType();
58 std::string str;
59 switch (type) {
60 case PLUGGED_TYPE_NONE:
61 str = "NONE";
62 break;
63 case PLUGGED_TYPE_AC:
64 str = "AC";
65 break;
66 case PLUGGED_TYPE_USB:
67 str = "USB";
68 break;
69 case PLUGGED_TYPE_WIRELESS:
70 str = "WIRELESS";
71 break;
72 case PLUGGED_TYPE_BUTT:
73 str = "BUTT";
74 break;
75 default:
76 str = "UNKNOWN";
77 break;
78 }
79 napi_value ret;
80 napi_create_string_utf8(env, str.c_str(), NAPI_AUTO_LENGTH, &ret);
81 return ret;
82 }
83
CommonEventKeyCapacity(napi_env env,napi_callback_info info)84 static napi_value CommonEventKeyCapacity(napi_env env, napi_callback_info info)
85 {
86 OH_LOG_Print(LOG_APP, LOG_ERROR, 0, "testLog", "CommonEventKeyCapacity func called");
87 size_t argc = 0;
88 napi_value args[2] = {nullptr};
89
90 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
91 std::string value = COMMON_EVENT_KEY_CAPACITY;
92 OH_LOG_Print(LOG_APP, LOG_ERROR, 0, "testLog", "get value = %{public}s", value.c_str());
93 napi_value ret;
94 napi_create_string_utf8(env, value.c_str(), NAPI_AUTO_LENGTH, &ret);
95 return ret;
96 }
97
CommonEventKeyChargeState(napi_env env,napi_callback_info info)98 static napi_value CommonEventKeyChargeState(napi_env env, napi_callback_info info)
99 {
100 OH_LOG_Print(LOG_APP, LOG_ERROR, 0, "testLog", "CommonEventKeyChargeState func called");
101 size_t argc = 0;
102 napi_value args[2] = {nullptr};
103
104 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
105 std::string value = COMMON_EVENT_KEY_CHARGE_STATE;
106 OH_LOG_Print(LOG_APP, LOG_ERROR, 0, "testLog", "get value = %{public}s", value.c_str());
107 napi_value ret;
108 napi_create_string_utf8(env, value.c_str(), NAPI_AUTO_LENGTH, &ret);
109 return ret;
110 }
111
CommonEventKeyPluggedType(napi_env env,napi_callback_info info)112 static napi_value CommonEventKeyPluggedType(napi_env env, napi_callback_info info)
113 {
114 OH_LOG_Print(LOG_APP, LOG_ERROR, 0, "testLog", "CommonEventKeyPluggedType func called");
115 size_t argc = 0;
116 napi_value args[2] = {nullptr};
117
118 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
119 std::string value = COMMON_EVENT_KEY_PLUGGED_TYPE;
120 OH_LOG_Print(LOG_APP, LOG_ERROR, 0, "testLog", "get value = %{public}s", value.c_str());
121 napi_value ret;
122 napi_create_string_utf8(env, value.c_str(), NAPI_AUTO_LENGTH, &ret);
123 return ret;
124 }
125
EnumPluggedClassConstructor(napi_env env,napi_callback_info info)126 static napi_value EnumPluggedClassConstructor(napi_env env, napi_callback_info info)
127 {
128 napi_value thisArg = nullptr;
129 void* data = nullptr;
130
131 napi_get_cb_info(env, info, nullptr, nullptr, &thisArg, &data);
132
133 napi_value global = nullptr;
134 napi_get_global(env, &global);
135
136 return thisArg;
137 }
138
CreateEnumPluggedType(napi_env env,napi_value exports)139 static napi_value CreateEnumPluggedType(napi_env env, napi_value exports)
140 {
141 napi_value none = nullptr;
142 napi_value ac = nullptr;
143 napi_value usb = nullptr;
144 napi_value wireless = nullptr;
145
146 napi_create_int32(env, (int32_t)BatteryInfo_BatteryPluggedType::PLUGGED_TYPE_NONE, &none);
147 napi_create_int32(env, (int32_t)BatteryInfo_BatteryPluggedType::PLUGGED_TYPE_AC, &ac);
148 napi_create_int32(env, (int32_t)BatteryInfo_BatteryPluggedType::PLUGGED_TYPE_USB, &usb);
149 napi_create_int32(env, (int32_t)BatteryInfo_BatteryPluggedType::PLUGGED_TYPE_WIRELESS, &wireless);
150
151 napi_property_descriptor desc[] = {
152 DECLARE_NAPI_STATIC_PROPERTY("NONE", none),
153 DECLARE_NAPI_STATIC_PROPERTY("AC", ac),
154 DECLARE_NAPI_STATIC_PROPERTY("USB", usb),
155 DECLARE_NAPI_STATIC_PROPERTY("WIRELESS", wireless),
156 };
157
158 napi_value result = nullptr;
159 napi_define_class(env, "BatteryInfo_BatteryPluggedType", NAPI_AUTO_LENGTH, EnumPluggedClassConstructor, nullptr,
160 sizeof(desc) / sizeof(*desc), desc, &result);
161
162 napi_set_named_property(env, exports, "BatteryInfo_BatteryPluggedType", result);
163
164 return exports;
165 }
166
167 EXTERN_C_START
Init(napi_env env,napi_value exports)168 static napi_value Init(napi_env env, napi_value exports)
169 {
170 napi_property_descriptor desc[] = {
171 {"add", nullptr, Add, nullptr, nullptr, nullptr, napi_default, nullptr},
172 {"getCapacity", nullptr, GetCapacity, nullptr, nullptr, nullptr, napi_default, nullptr},
173 {"getPluggedType", nullptr, GetPluggedType, nullptr, nullptr, nullptr, napi_default, nullptr},
174 {"commonEventKeyCapacity", nullptr, CommonEventKeyCapacity, nullptr, nullptr, nullptr, napi_default, nullptr},
175 {"commonEventKeyChargeState", nullptr, CommonEventKeyChargeState, nullptr, nullptr, nullptr, napi_default,
176 nullptr},
177 {"commonEventKeyPluggedType", nullptr, CommonEventKeyPluggedType, nullptr, nullptr, nullptr,
178 napi_default, nullptr}};
179 napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
180
181 CreateEnumPluggedType(env, exports);
182 return exports;
183 }
184 EXTERN_C_END
185
186 static napi_module demoModule = {
187 .nm_version = 1,
188 .nm_flags = 0,
189 .nm_filename = nullptr,
190 .nm_register_func = Init,
191 .nm_modname = "entry",
192 .nm_priv = ((void*)0),
193 .reserved = { 0 },
194 };
195
RegisterEntryModule(void)196 extern "C" __attribute__((constructor)) void RegisterEntryModule(void)
197 {
198 napi_module_register(&demoModule);
199 }
200