• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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_srv_client.h"
25 #include "battery_log.h"
26 
27 using namespace OHOS::PowerMgr;
28 
29 thread_local static BatterySrvClient& g_battClient = BatterySrvClient::GetInstance();
30 
BatterySOC(napi_env env,napi_callback_info info)31 static napi_value BatterySOC(napi_env env, napi_callback_info info)
32 {
33     napi_value napiValue = nullptr;
34     int32_t capacity = g_battClient.GetCapacity();
35 
36     NAPI_CALL(env, napi_create_int32(env, capacity, &napiValue));
37 
38     BATTERY_HILOGD(FEATURE_BATT_INFO, "capacity %{public}d", capacity);
39     return napiValue;
40 }
41 
GetChargingState(napi_env env,napi_callback_info info)42 static napi_value GetChargingState(napi_env env, napi_callback_info info)
43 {
44     napi_value napiValue = nullptr;
45     int32_t chargingState = (int32_t)g_battClient.GetChargingStatus();
46 
47     NAPI_CALL(env, napi_create_int32(env, chargingState, &napiValue));
48 
49     BATTERY_HILOGD(FEATURE_BATT_INFO, "chargingState %{public}d", chargingState);
50 
51     return napiValue;
52 }
53 
GetHealthState(napi_env env,napi_callback_info info)54 static napi_value GetHealthState(napi_env env, napi_callback_info info)
55 {
56     napi_value napiValue = nullptr;
57     int32_t healthStatus = (int32_t)g_battClient.GetHealthStatus();
58 
59     NAPI_CALL(env, napi_create_int32(env, healthStatus, &napiValue));
60 
61     BATTERY_HILOGD(FEATURE_BATT_INFO, "healthStatus %{public}d", healthStatus);
62 
63     return napiValue;
64 }
65 
GetPluggedType(napi_env env,napi_callback_info info)66 static napi_value GetPluggedType(napi_env env, napi_callback_info info)
67 {
68     napi_value napiValue = nullptr;
69     int32_t pluggedType = (int32_t)g_battClient.GetPluggedType();
70 
71     NAPI_CALL(env, napi_create_int32(env, pluggedType, &napiValue));
72 
73     BATTERY_HILOGD(FEATURE_BATT_INFO, "pluggedType %{public}d", pluggedType);
74 
75     return napiValue;
76 }
77 
GetVoltage(napi_env env,napi_callback_info info)78 static napi_value GetVoltage(napi_env env, napi_callback_info info)
79 {
80     napi_value napiValue = nullptr;
81     int32_t voltage = g_battClient.GetVoltage();
82 
83     NAPI_CALL(env, napi_create_int32(env, voltage, &napiValue));
84 
85     BATTERY_HILOGD(FEATURE_BATT_INFO, "voltage %{public}d", voltage);
86 
87     return napiValue;
88 }
89 
GetTechnology(napi_env env,napi_callback_info info)90 static napi_value GetTechnology(napi_env env, napi_callback_info info)
91 {
92     napi_value napiValue = nullptr;
93     auto technology = g_battClient.GetTechnology();
94     const char* technologyStr = technology.c_str();
95 
96     NAPI_CALL(env, napi_create_string_utf8(env, technologyStr, strlen(technologyStr), &napiValue));
97 
98     BATTERY_HILOGD(FEATURE_BATT_INFO, "technology %{public}s", technologyStr);
99     return napiValue;
100 }
101 
GetBatteryTemperature(napi_env env,napi_callback_info info)102 static napi_value GetBatteryTemperature(napi_env env, napi_callback_info info)
103 {
104     napi_value napiValue = nullptr;
105     int32_t temperature = g_battClient.GetBatteryTemperature();
106 
107     NAPI_CALL(env, napi_create_int32(env, temperature, &napiValue));
108 
109     BATTERY_HILOGD(FEATURE_BATT_INFO, "temperature %{public}d", temperature);
110 
111     return napiValue;
112 }
113 
GetBatteryPresent(napi_env env,napi_callback_info info)114 static napi_value GetBatteryPresent(napi_env env, napi_callback_info info)
115 {
116     napi_value napiValue = nullptr;
117     bool present = g_battClient.GetPresent();
118 
119     NAPI_CALL(env, napi_get_boolean(env, present, &napiValue));
120 
121     BATTERY_HILOGD(FEATURE_BATT_INFO, "present %{public}d", present);
122 
123     return napiValue;
124 }
125 
GetBatteryNowCurrent(napi_env env,napi_callback_info info)126 static napi_value GetBatteryNowCurrent(napi_env env, napi_callback_info info)
127 {
128     napi_value napiValue = nullptr;
129     int32_t curNow = g_battClient.GetNowCurrent();
130 
131     NAPI_CALL(env, napi_create_int32(env, curNow, &napiValue));
132 
133     BATTERY_HILOGD(FEATURE_BATT_INFO, "curNow %{public}d", curNow);
134     return napiValue;
135 }
136 
GetBatteryRemainEnergy(napi_env env,napi_callback_info info)137 static napi_value GetBatteryRemainEnergy(napi_env env, napi_callback_info info)
138 {
139     napi_value napiValue = nullptr;
140     int32_t remainEnergy = g_battClient.GetRemainEnergy();
141 
142     NAPI_CALL(env, napi_create_int32(env, remainEnergy, &napiValue));
143 
144     BATTERY_HILOGD(FEATURE_BATT_INFO, "remainEnergy %{public}d", remainEnergy);
145     return napiValue;
146 }
147 
GetRemainingChargeTime(napi_env env,napi_callback_info info)148 static napi_value GetRemainingChargeTime(napi_env env, napi_callback_info info)
149 {
150     napi_value napiValue = nullptr;
151     int64_t time = g_battClient.GetRemainingChargeTime();
152 
153     NAPI_CALL(env, napi_create_int64(env, time, &napiValue));
154     return napiValue;
155 }
156 
GetTotalEnergy(napi_env env,napi_callback_info info)157 static napi_value GetTotalEnergy(napi_env env, napi_callback_info info)
158 {
159     napi_value napiValue = nullptr;
160     int32_t totalEnergy = (int32_t)g_battClient.GetTotalEnergy();
161 
162     NAPI_CALL(env, napi_create_int32(env, totalEnergy, &napiValue));
163 
164     BATTERY_HILOGD(FEATURE_BATT_INFO, "totalEnergy %{public}d", totalEnergy);
165     return napiValue;
166 }
167 
GetCapacityLevel(napi_env env,napi_callback_info info)168 static napi_value GetCapacityLevel(napi_env env, napi_callback_info info)
169 {
170     napi_value napiValue = nullptr;
171     int32_t batteryCapacityLevel = (int32_t)g_battClient.GetCapacityLevel();
172 
173     NAPI_CALL(env, napi_create_int32(env, batteryCapacityLevel, &napiValue));
174 
175     BATTERY_HILOGD(FEATURE_BATT_INFO, "batteryCapacityLevel %{public}d", batteryCapacityLevel);
176     return napiValue;
177 }
178 
EnumHealthClassConstructor(napi_env env,napi_callback_info info)179 static napi_value EnumHealthClassConstructor(napi_env env, napi_callback_info info)
180 {
181     napi_value thisArg = nullptr;
182     void* data = nullptr;
183 
184     napi_get_cb_info(env, info, nullptr, nullptr, &thisArg, &data);
185 
186     napi_value global = nullptr;
187     napi_get_global(env, &global);
188 
189     return thisArg;
190 }
191 
EnumLevelClassConstructor(napi_env env,napi_callback_info info)192 static napi_value EnumLevelClassConstructor(napi_env env, napi_callback_info info)
193 {
194     napi_value thisArg = nullptr;
195     void* data = nullptr;
196 
197     napi_get_cb_info(env, info, nullptr, nullptr, &thisArg, &data);
198 
199     napi_value global = nullptr;
200     napi_get_global(env, &global);
201 
202     return thisArg;
203 }
204 
CreateEnumHealthState(napi_env env,napi_value exports)205 static napi_value CreateEnumHealthState(napi_env env, napi_value exports)
206 {
207     napi_value unknown = nullptr;
208     napi_value good = nullptr;
209     napi_value overheat = nullptr;
210     napi_value overvoltage = nullptr;
211     napi_value cold = nullptr;
212     napi_value dead = nullptr;
213 
214     napi_create_int32(env, (int32_t)BatteryHealthState::HEALTH_STATE_UNKNOWN, &unknown);
215     napi_create_int32(env, (int32_t)BatteryHealthState::HEALTH_STATE_GOOD, &good);
216     napi_create_int32(env, (int32_t)BatteryHealthState::HEALTH_STATE_OVERHEAT, &overheat);
217     napi_create_int32(env, (int32_t)BatteryHealthState::HEALTH_STATE_OVERVOLTAGE, &overvoltage);
218     napi_create_int32(env, (int32_t)BatteryHealthState::HEALTH_STATE_COLD, &cold);
219     napi_create_int32(env, (int32_t)BatteryHealthState::HEALTH_STATE_DEAD, &dead);
220 
221     napi_property_descriptor desc[] = {
222         DECLARE_NAPI_STATIC_PROPERTY("UNKNOWN", unknown),
223         DECLARE_NAPI_STATIC_PROPERTY("GOOD", good),
224         DECLARE_NAPI_STATIC_PROPERTY("OVERHEAT", overheat),
225         DECLARE_NAPI_STATIC_PROPERTY("OVERVOLTAGE", overvoltage),
226         DECLARE_NAPI_STATIC_PROPERTY("COLD", cold),
227         DECLARE_NAPI_STATIC_PROPERTY("DEAD", dead),
228     };
229     napi_value result = nullptr;
230     napi_define_class(env, "BatteryHealthState", NAPI_AUTO_LENGTH, EnumHealthClassConstructor, nullptr,
231         sizeof(desc) / sizeof(*desc), desc, &result);
232 
233     napi_set_named_property(env, exports, "BatteryHealthState", result);
234 
235     return exports;
236 }
237 
CreateEnumLevelState(napi_env env,napi_value exports)238 static napi_value CreateEnumLevelState(napi_env env, napi_value exports)
239 {
240     napi_value full = nullptr;
241     napi_value high = nullptr;
242     napi_value normal = nullptr;
243     napi_value low = nullptr;
244     napi_value warning = nullptr;
245     napi_value critical = nullptr;
246     napi_value shutdown = nullptr;
247 
248     napi_create_int32(env, (int32_t)BatteryCapacityLevel::LEVEL_FULL, &full);
249     napi_create_int32(env, (int32_t)BatteryCapacityLevel::LEVEL_HIGH, &high);
250     napi_create_int32(env, (int32_t)BatteryCapacityLevel::LEVEL_NORMAL, &normal);
251     napi_create_int32(env, (int32_t)BatteryCapacityLevel::LEVEL_LOW, &low);
252     napi_create_int32(env, (int32_t)BatteryCapacityLevel::LEVEL_WARNING, &warning);
253     napi_create_int32(env, (int32_t)BatteryCapacityLevel::LEVEL_CRITICAL, &critical);
254     napi_create_int32(env, (int32_t)BatteryCapacityLevel::LEVEL_SHUTDOWN, &shutdown);
255 
256     napi_property_descriptor desc[] = {
257         DECLARE_NAPI_STATIC_PROPERTY("LEVEL_FULL", full),
258         DECLARE_NAPI_STATIC_PROPERTY("LEVEL_HIGH", high),
259         DECLARE_NAPI_STATIC_PROPERTY("LEVEL_NORMAL", normal),
260         DECLARE_NAPI_STATIC_PROPERTY("LEVEL_LOW", low),
261         DECLARE_NAPI_STATIC_PROPERTY("LEVEL_WARNING", warning),
262         DECLARE_NAPI_STATIC_PROPERTY("LEVEL_CRITICAL", critical),
263         DECLARE_NAPI_STATIC_PROPERTY("LEVEL_SHUTDOWN", shutdown),
264     };
265     napi_value result = nullptr;
266     napi_define_class(env, "BatteryCapacityLevel", NAPI_AUTO_LENGTH, EnumLevelClassConstructor, nullptr,
267         sizeof(desc) / sizeof(*desc), desc, &result);
268 
269     napi_set_named_property(env, exports, "BatteryCapacityLevel", result);
270 
271     return exports;
272 }
273 
EnumChargeClassConstructor(napi_env env,napi_callback_info info)274 static napi_value EnumChargeClassConstructor(napi_env env, napi_callback_info info)
275 {
276     napi_value thisArg = nullptr;
277     void* data = nullptr;
278 
279     napi_get_cb_info(env, info, nullptr, nullptr, &thisArg, &data);
280 
281     napi_value global = nullptr;
282     napi_get_global(env, &global);
283 
284     return thisArg;
285 }
286 
CreateEnumChargeState(napi_env env,napi_value exports)287 static napi_value CreateEnumChargeState(napi_env env, napi_value exports)
288 {
289     napi_value none = nullptr;
290     napi_value enable = nullptr;
291     napi_value disable = nullptr;
292     napi_value full = nullptr;
293 
294     napi_create_int32(env, (int32_t)BatteryChargeState::CHARGE_STATE_NONE, &none);
295     napi_create_int32(env, (int32_t)BatteryChargeState::CHARGE_STATE_ENABLE, &enable);
296     napi_create_int32(env, (int32_t)BatteryChargeState::CHARGE_STATE_DISABLE, &disable);
297     napi_create_int32(env, (int32_t)BatteryChargeState::CHARGE_STATE_FULL, &full);
298 
299     napi_property_descriptor desc[] = {
300         DECLARE_NAPI_STATIC_PROPERTY("NONE", none),
301         DECLARE_NAPI_STATIC_PROPERTY("ENABLE", enable),
302         DECLARE_NAPI_STATIC_PROPERTY("DISABLE", disable),
303         DECLARE_NAPI_STATIC_PROPERTY("FULL", full),
304     };
305 
306     napi_value result = nullptr;
307     napi_define_class(env, "BatteryChargeState", NAPI_AUTO_LENGTH, EnumChargeClassConstructor, nullptr,
308         sizeof(desc) / sizeof(*desc), desc, &result);
309 
310     napi_set_named_property(env, exports, "BatteryChargeState", result);
311 
312     return exports;
313 }
314 
EnumPluggedClassConstructor(napi_env env,napi_callback_info info)315 static napi_value EnumPluggedClassConstructor(napi_env env, napi_callback_info info)
316 {
317     napi_value thisArg = nullptr;
318     void* data = nullptr;
319 
320     napi_get_cb_info(env, info, nullptr, nullptr, &thisArg, &data);
321 
322     napi_value global = nullptr;
323     napi_get_global(env, &global);
324 
325     return thisArg;
326 }
327 
CreateEnumPluggedType(napi_env env,napi_value exports)328 static napi_value CreateEnumPluggedType(napi_env env, napi_value exports)
329 {
330     napi_value none = nullptr;
331     napi_value ac = nullptr;
332     napi_value usb = nullptr;
333     napi_value wireless = nullptr;
334 
335     napi_create_int32(env, (int32_t)BatteryPluggedType::PLUGGED_TYPE_NONE, &none);
336     napi_create_int32(env, (int32_t)BatteryPluggedType::PLUGGED_TYPE_AC, &ac);
337     napi_create_int32(env, (int32_t)BatteryPluggedType::PLUGGED_TYPE_USB, &usb);
338     napi_create_int32(env, (int32_t)BatteryPluggedType::PLUGGED_TYPE_WIRELESS, &wireless);
339 
340     napi_property_descriptor desc[] = {
341         DECLARE_NAPI_STATIC_PROPERTY("NONE", none),
342         DECLARE_NAPI_STATIC_PROPERTY("AC", ac),
343         DECLARE_NAPI_STATIC_PROPERTY("USB", usb),
344         DECLARE_NAPI_STATIC_PROPERTY("WIRELESS", wireless),
345     };
346 
347     napi_value result = nullptr;
348     napi_define_class(env, "BatteryPluggedType", NAPI_AUTO_LENGTH, EnumPluggedClassConstructor, nullptr,
349         sizeof(desc) / sizeof(*desc), desc, &result);
350 
351     napi_set_named_property(env, exports, "BatteryPluggedType", result);
352 
353     return exports;
354 }
355 
EnumEventChangedCodeConstructor(napi_env env,napi_callback_info info)356 static napi_value EnumEventChangedCodeConstructor(napi_env env, napi_callback_info info)
357 {
358     napi_value thisArg = nullptr;
359     void* data = nullptr;
360 
361     napi_get_cb_info(env, info, nullptr, nullptr, &thisArg, &data);
362 
363     napi_value global = nullptr;
364     napi_get_global(env, &global);
365 
366     return thisArg;
367 }
368 
CreateEventBatteryChangedKey(napi_env env,napi_value exports)369 static napi_value CreateEventBatteryChangedKey(napi_env env, napi_value exports)
370 {
371     napi_value soc = nullptr;
372     napi_value chargeState = nullptr;
373     napi_value healthState = nullptr;
374     napi_value pluggedType = nullptr;
375     napi_value voltage = nullptr;
376     napi_value technology = nullptr;
377     napi_value temperature = nullptr;
378     napi_value present = nullptr;
379     napi_value capacityLevel = nullptr;
380 
381     napi_create_string_utf8(env, BatteryInfo::COMMON_EVENT_KEY_CAPACITY, NAPI_AUTO_LENGTH, &soc);
382     napi_create_string_utf8(env, BatteryInfo::COMMON_EVENT_KEY_CHARGE_STATE, NAPI_AUTO_LENGTH, &chargeState);
383     napi_create_string_utf8(env, BatteryInfo::COMMON_EVENT_KEY_HEALTH_STATE, NAPI_AUTO_LENGTH, &healthState);
384     napi_create_string_utf8(env, BatteryInfo::COMMON_EVENT_KEY_PLUGGED_TYPE, NAPI_AUTO_LENGTH, &pluggedType);
385     napi_create_string_utf8(env, BatteryInfo::COMMON_EVENT_KEY_VOLTAGE, NAPI_AUTO_LENGTH, &voltage);
386     napi_create_string_utf8(env, BatteryInfo::COMMON_EVENT_KEY_TECHNOLOGY, NAPI_AUTO_LENGTH, &technology);
387     napi_create_string_utf8(env, BatteryInfo::COMMON_EVENT_KEY_TEMPERATURE, NAPI_AUTO_LENGTH, &temperature);
388     napi_create_string_utf8(env, BatteryInfo::COMMON_EVENT_KEY_PRESENT, NAPI_AUTO_LENGTH, &present);
389     napi_create_string_utf8(env, BatteryInfo::COMMON_EVENT_KEY_CAPACITY_LEVEL, NAPI_AUTO_LENGTH, &capacityLevel);
390 
391     napi_property_descriptor desc[] = {
392         DECLARE_NAPI_STATIC_PROPERTY("EXTRA_SOC", soc),
393         DECLARE_NAPI_STATIC_PROPERTY("EXTRA_CHARGE_STATE", chargeState),
394         DECLARE_NAPI_STATIC_PROPERTY("EXTRA_HEALTH_STATE", healthState),
395         DECLARE_NAPI_STATIC_PROPERTY("EXTRA_PLUGGED_TYPE", pluggedType),
396         DECLARE_NAPI_STATIC_PROPERTY("EXTRA_VOLTAGE", voltage),
397         DECLARE_NAPI_STATIC_PROPERTY("EXTRA_TECHNOLOGY", technology),
398         DECLARE_NAPI_STATIC_PROPERTY("EXTRA_TEMPERATURE", temperature),
399         DECLARE_NAPI_STATIC_PROPERTY("EXTRA_PRESENT", present),
400         DECLARE_NAPI_STATIC_PROPERTY("EXTRA_CAPACITY_LEVEL", capacityLevel),
401     };
402 
403     napi_value result = nullptr;
404     napi_define_class(env, "CommonEventBatteryChangedKey", NAPI_AUTO_LENGTH, EnumEventChangedCodeConstructor, nullptr,
405         sizeof(desc) / sizeof(*desc), desc, &result);
406 
407     napi_set_named_property(env, exports, "CommonEventBatteryChangedKey", result);
408 
409     return exports;
410 }
411 
CreateEventBatteryChangedCode(napi_env env,napi_value exports)412 static napi_value CreateEventBatteryChangedCode(napi_env env, napi_value exports)
413 {
414     napi_value soc = nullptr;
415     napi_value voltage = nullptr;
416     napi_value temperature = nullptr;
417     napi_value healthState = nullptr;
418     napi_value pluggedType = nullptr;
419     napi_value maxCurrent = nullptr;
420     napi_value maxVoltage = nullptr;
421     napi_value chargeState = nullptr;
422     napi_value chargeCounter = nullptr;
423     napi_value present = nullptr;
424     napi_value technology = nullptr;
425     napi_value batteryCapacityLevel = nullptr;
426 
427     napi_create_int32(env, (int32_t)BatteryInfo::COMMON_EVENT_CODE_CAPACITY, &soc);
428     napi_create_int32(env, (int32_t)BatteryInfo::COMMON_EVENT_CODE_VOLTAGE, &voltage);
429     napi_create_int32(env, (int32_t)BatteryInfo::COMMON_EVENT_CODE_TEMPERATURE, &temperature);
430     napi_create_int32(env, (int32_t)BatteryInfo::COMMON_EVENT_CODE_HEALTH_STATE, &healthState);
431     napi_create_int32(env, (int32_t)BatteryInfo::COMMON_EVENT_CODE_PLUGGED_TYPE, &pluggedType);
432     napi_create_int32(env, (int32_t)BatteryInfo::COMMON_EVENT_CODE_PLUGGED_MAX_CURRENT, &maxCurrent);
433     napi_create_int32(env, (int32_t)BatteryInfo::COMMON_EVENT_CODE_PLUGGED_MAX_VOLTAGE, &maxVoltage);
434     napi_create_int32(env, (int32_t)BatteryInfo::COMMON_EVENT_CODE_CHARGE_STATE, &chargeState);
435     napi_create_int32(env, (int32_t)BatteryInfo::COMMON_EVENT_CODE_CHARGE_COUNTER, &chargeCounter);
436     napi_create_int32(env, (int32_t)BatteryInfo::COMMON_EVENT_CODE_PRESENT, &present);
437     napi_create_int32(env, (int32_t)BatteryInfo::COMMON_EVENT_CODE_TECHNOLOGY, &technology);
438     napi_create_int32(env, (int32_t)BatteryInfo::COMMON_EVENT_CODE_CAPACITY_LEVEL, &batteryCapacityLevel);
439 
440     napi_property_descriptor desc[] = {
441         DECLARE_NAPI_STATIC_PROPERTY("EXTRA_SOC", soc),
442         DECLARE_NAPI_STATIC_PROPERTY("EXTRA_VOLTAGE", voltage),
443         DECLARE_NAPI_STATIC_PROPERTY("EXTRA_TEMPERATURE", temperature),
444         DECLARE_NAPI_STATIC_PROPERTY("EXTRA_HEALTH_STATE", healthState),
445         DECLARE_NAPI_STATIC_PROPERTY("EXTRA_PLUGGED_TYPE", pluggedType),
446         DECLARE_NAPI_STATIC_PROPERTY("EXTRA_MAX_CURRENT", maxCurrent),
447         DECLARE_NAPI_STATIC_PROPERTY("EXTRA_MAX_VOLTAGE", maxVoltage),
448         DECLARE_NAPI_STATIC_PROPERTY("EXTRA_CHARGE_STATE", chargeState),
449         DECLARE_NAPI_STATIC_PROPERTY("EXTRA_CHARGE_COUNTER", chargeCounter),
450         DECLARE_NAPI_STATIC_PROPERTY("EXTRA_PRESENT", present),
451         DECLARE_NAPI_STATIC_PROPERTY("EXTRA_TECHNOLOGY", technology),
452         DECLARE_NAPI_STATIC_PROPERTY("EXTRA_CAPACITY_LEVEL", batteryCapacityLevel),
453     };
454 
455     napi_value result = nullptr;
456     napi_define_class(env, "CommonEventBatteryChangedCode", NAPI_AUTO_LENGTH, EnumEventChangedCodeConstructor, nullptr,
457         sizeof(desc) / sizeof(*desc), desc, &result);
458 
459     napi_set_named_property(env, exports, "CommonEventBatteryChangedCode", result);
460 
461     return exports;
462 }
463 
464 EXTERN_C_START
465 /*
466  * function for module exports
467  */
BatteryInit(napi_env env,napi_value exports)468 static napi_value BatteryInit(napi_env env, napi_value exports)
469 {
470     BATTERY_HILOGD(COMP_FWK, "Enter");
471 
472     napi_property_descriptor desc[] = {
473         DECLARE_NAPI_GETTER("batterySOC", BatterySOC),
474         DECLARE_NAPI_GETTER("chargingStatus", GetChargingState),
475         DECLARE_NAPI_GETTER("healthStatus", GetHealthState),
476         DECLARE_NAPI_GETTER("pluggedType", GetPluggedType),
477         DECLARE_NAPI_GETTER("voltage", GetVoltage),
478         DECLARE_NAPI_GETTER("technology", GetTechnology),
479         DECLARE_NAPI_GETTER("batteryTemperature", GetBatteryTemperature),
480         DECLARE_NAPI_GETTER("isBatteryPresent", GetBatteryPresent),
481         DECLARE_NAPI_GETTER("batteryCapacityLevel", GetCapacityLevel),
482         DECLARE_NAPI_GETTER("estimatedRemainingChargeTime", GetRemainingChargeTime),
483         DECLARE_NAPI_GETTER("nowCurrent", GetBatteryNowCurrent),
484         DECLARE_NAPI_GETTER("remainingEnergy", GetBatteryRemainEnergy),
485         DECLARE_NAPI_GETTER("totalEnergy", GetTotalEnergy),
486     };
487     NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc));
488 
489     CreateEnumPluggedType(env, exports);
490     CreateEnumChargeState(env, exports);
491     CreateEnumHealthState(env, exports);
492     CreateEnumLevelState(env, exports);
493     CreateEventBatteryChangedKey(env, exports);
494     CreateEventBatteryChangedCode(env, exports);
495 
496     BATTERY_HILOGD(COMP_FWK, "Success");
497 
498     return exports;
499 }
500 EXTERN_C_END
501 
502 /*
503  * Module definition
504  */
505 static napi_module g_module = {
506     .nm_version = 1,
507     .nm_flags = 0,
508     .nm_filename = "batteryInfo",
509     .nm_register_func = BatteryInit,
510     .nm_modname = "batteryInfo",
511     .nm_priv = ((void*)0),
512     .reserved = {0}
513 };
514 
515 /*
516  * Module registration
517  */
RegisterModule(void)518 extern "C" __attribute__((constructor)) void RegisterModule(void)
519 {
520     napi_module_register(&g_module);
521 }
522