1 /*
2 * Copyright (c) 2021-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 #include "napi/native_api.h"
17 #include "napi/native_common.h"
18 #include "napi/native_node_api.h"
19
20 #include "battery_stats.h"
21 #include "battery_stats_info.h"
22 #include "napi_error.h"
23 #include "napi_utils.h"
24 #include "stats_log.h"
25
26 using namespace OHOS::PowerMgr;
27
28 namespace {
29 constexpr uint32_t MAX_ARGC = 1;
30 constexpr uint32_t ARGV_IND_0 = 0;
31 } // namespace
32
GetBatteryStats(napi_env env,napi_callback_info info)33 static napi_value GetBatteryStats(napi_env env, napi_callback_info info)
34 {
35 size_t argc = MAX_ARGC;
36 napi_value argv[argc];
37
38 NapiUtils::GetCallbackInfo(env, info, argc, argv);
39 NapiError error;
40
41 if (argc > MAX_ARGC) {
42 return error.ThrowError(env, StatsError::ERR_PARAM_INVALID);
43 }
44
45 BatteryStats stats(env);
46 if (argc == MAX_ARGC) {
47 if (!NapiUtils::CheckValueType(env, argv[ARGV_IND_0], napi_function)) {
48 return error.ThrowError(env, StatsError::ERR_PARAM_INVALID);
49 }
50 stats.StatsAsyncCallBack(argv[ARGV_IND_0]);
51 return nullptr;
52 }
53
54 return stats.StatsPromise();
55 }
56
GetAppStatsMah(napi_env env,napi_callback_info info)57 static napi_value GetAppStatsMah(napi_env env, napi_callback_info info)
58 {
59 BatteryStats stats(env);
60 return stats.GetAppStatsMah(info, MAX_ARGC, ARGV_IND_0);
61 }
62
GetAppStatsPercent(napi_env env,napi_callback_info info)63 static napi_value GetAppStatsPercent(napi_env env, napi_callback_info info)
64 {
65 BatteryStats stats(env);
66 return stats.GetAppStatsPercent(info, MAX_ARGC, ARGV_IND_0);
67 }
68
GetPartStatsMah(napi_env env,napi_callback_info info)69 static napi_value GetPartStatsMah(napi_env env, napi_callback_info info)
70 {
71 BatteryStats stats(env);
72 return stats.GetPartStatsMah(info, MAX_ARGC, ARGV_IND_0);
73 }
74
GetPartStatsPercent(napi_env env,napi_callback_info info)75 static napi_value GetPartStatsPercent(napi_env env, napi_callback_info info)
76 {
77 BatteryStats stats(env);
78 return stats.GetPartStatsPercent(info, MAX_ARGC, ARGV_IND_0);
79 }
80
EnumStatsTypeConstructor(napi_env env,napi_callback_info info)81 static napi_value EnumStatsTypeConstructor(napi_env env, napi_callback_info info)
82 {
83 napi_value thisArg = nullptr;
84 void* data = nullptr;
85 napi_get_cb_info(env, info, nullptr, nullptr, &thisArg, &data);
86 napi_value global = nullptr;
87 napi_get_global(env, &global);
88 return thisArg;
89 }
90
CreateEnumStatsType(napi_env env,napi_value exports)91 static napi_value CreateEnumStatsType(napi_env env, napi_value exports)
92 {
93 napi_value invalid = nullptr;
94 napi_value app = nullptr;
95 napi_value bluetooth = nullptr;
96 napi_value idle = nullptr;
97 napi_value phone = nullptr;
98 napi_value radio = nullptr;
99 napi_value screen = nullptr;
100 napi_value user = nullptr;
101 napi_value wifi = nullptr;
102
103 napi_create_int32(env, (int32_t)BatteryStatsInfo::ConsumptionType::CONSUMPTION_TYPE_INVALID, &invalid);
104 napi_create_int32(env, (int32_t)BatteryStatsInfo::ConsumptionType::CONSUMPTION_TYPE_APP, &app);
105 napi_create_int32(env, (int32_t)BatteryStatsInfo::ConsumptionType::CONSUMPTION_TYPE_BLUETOOTH, &bluetooth);
106 napi_create_int32(env, (int32_t)BatteryStatsInfo::ConsumptionType::CONSUMPTION_TYPE_IDLE, &idle);
107 napi_create_int32(env, (int32_t)BatteryStatsInfo::ConsumptionType::CONSUMPTION_TYPE_PHONE, &phone);
108 napi_create_int32(env, (int32_t)BatteryStatsInfo::ConsumptionType::CONSUMPTION_TYPE_RADIO, &radio);
109 napi_create_int32(env, (int32_t)BatteryStatsInfo::ConsumptionType::CONSUMPTION_TYPE_SCREEN, &screen);
110 napi_create_int32(env, (int32_t)BatteryStatsInfo::ConsumptionType::CONSUMPTION_TYPE_USER, &user);
111 napi_create_int32(env, (int32_t)BatteryStatsInfo::ConsumptionType::CONSUMPTION_TYPE_WIFI, &wifi);
112
113 napi_property_descriptor desc[] = {
114 DECLARE_NAPI_STATIC_PROPERTY("CONSUMPTION_TYPE_INVALID", invalid),
115 DECLARE_NAPI_STATIC_PROPERTY("CONSUMPTION_TYPE_APP", app),
116 DECLARE_NAPI_STATIC_PROPERTY("CONSUMPTION_TYPE_BLUETOOTH", bluetooth),
117 DECLARE_NAPI_STATIC_PROPERTY("CONSUMPTION_TYPE_IDLE", idle),
118 DECLARE_NAPI_STATIC_PROPERTY("CONSUMPTION_TYPE_PHONE", phone),
119 DECLARE_NAPI_STATIC_PROPERTY("CONSUMPTION_TYPE_RADIO", radio),
120 DECLARE_NAPI_STATIC_PROPERTY("CONSUMPTION_TYPE_SCREEN", screen),
121 DECLARE_NAPI_STATIC_PROPERTY("CONSUMPTION_TYPE_USER", user),
122 DECLARE_NAPI_STATIC_PROPERTY("CONSUMPTION_TYPE_WIFI", wifi),
123 };
124 napi_value result = nullptr;
125 napi_define_class(env, "ConsumptionType", NAPI_AUTO_LENGTH, EnumStatsTypeConstructor, nullptr,
126 sizeof(desc) / sizeof(*desc), desc, &result);
127
128 napi_set_named_property(env, exports, "ConsumptionType", result);
129 return exports;
130 }
131
132 EXTERN_C_START
133 /*
134 * function for module exports
135 */
BatteryStatsInit(napi_env env,napi_value exports)136 static napi_value BatteryStatsInit(napi_env env, napi_value exports)
137 {
138 napi_property_descriptor desc[] = {
139 DECLARE_NAPI_FUNCTION("getBatteryStats", GetBatteryStats),
140 DECLARE_NAPI_FUNCTION("getAppPowerValue", GetAppStatsMah),
141 DECLARE_NAPI_FUNCTION("getAppPowerPercent", GetAppStatsPercent),
142 DECLARE_NAPI_FUNCTION("getHardwareUnitPowerValue", GetPartStatsMah),
143 DECLARE_NAPI_FUNCTION("getHardwareUnitPowerPercent", GetPartStatsPercent),
144 };
145 NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc));
146
147 CreateEnumStatsType(env, exports);
148 return exports;
149 }
150 EXTERN_C_END
151
152 /*
153 * Module definition
154 */
155 static napi_module batteryStatsModule = {
156 .nm_version = 1,
157 .nm_flags = 0,
158 .nm_filename = "batteryStats",
159 .nm_register_func = BatteryStatsInit,
160 .nm_modname = "batteryStatistics",
161 .nm_priv = ((void*)0),
162 .reserved = {0}};
163
164 /*
165 * Module registration
166 */
RegisterModule(void)167 extern "C" __attribute__((constructor)) void RegisterModule(void)
168 {
169 napi_module_register(&batteryStatsModule);
170 }
171