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 "battery_module.h"
17 #include "battery_impl.h"
18 #include "hilog_wrapper.h"
19
20 namespace OHOS {
21 namespace ACELite {
22 namespace {
SuccessCallBack(const JSIValue thisVal,const JSIValue args,JSIValue jsiValue)23 void SuccessCallBack(const JSIValue thisVal, const JSIValue args, JSIValue jsiValue)
24 {
25 if (JSI::ValueIsUndefined(args)) {
26 return;
27 }
28 JSIValue success = JSI::GetNamedProperty(args, CB_SUCCESS);
29 JSIValue complete = JSI::GetNamedProperty(args, CB_COMPLETE);
30 if (!JSI::ValueIsUndefined(success)) {
31 if (JSI::ValueIsUndefined(jsiValue)) {
32 JSI::CallFunction(success, thisVal, nullptr, 0);
33 } else {
34 JSI::CallFunction(success, thisVal, &jsiValue, ARGC_ONE);
35 }
36 }
37 if (!JSI::ValueIsUndefined(complete)) {
38 JSI::CallFunction(complete, thisVal, nullptr, 0);
39 }
40 JSI::ReleaseValueList(success, complete, ARGS_END);
41 }
42 }
43
GetBatterySOC(const JSIValue thisVal,const JSIValue * args,uint8_t argsNum)44 JSIValue BatteryModule::GetBatterySOC(const JSIValue thisVal, const JSIValue *args, uint8_t argsNum)
45 {
46 JSIValue undefValue = JSI::CreateUndefined();
47 int32_t batterySoc = 0;
48 if ((args == nullptr) || (argsNum == 0) || JSI::ValueIsUndefined(args[0])) {
49 return undefValue;
50 }
51
52 batterySoc = GetBatSocImpl();
53 JSIValue result = JSI::CreateObject();
54 JSI::SetNumberProperty(result, "batterySoc", batterySoc);
55 SuccessCallBack(thisVal, args[0], result);
56 JSI::ReleaseValue(result);
57 return undefValue;
58 }
59
GetChargingStatus(const JSIValue thisVal,const JSIValue * args,uint8_t argsNum)60 JSIValue BatteryModule::GetChargingStatus(const JSIValue thisVal, const JSIValue *args, uint8_t argsNum)
61 {
62 JSIValue undefValue = JSI::CreateUndefined();
63 int32_t chargingStatus = 0;
64 if ((args == nullptr) || (argsNum == 0) || JSI::ValueIsUndefined(args[0])) {
65 return undefValue;
66 }
67
68 chargingStatus = GetChargingStatusImpl();
69 JSIValue result = JSI::CreateObject();
70 JSI::SetNumberProperty(result, "chargingStatus", chargingStatus);
71 SuccessCallBack(thisVal, args[0], result);
72 JSI::ReleaseValue(result);
73 return undefValue;
74 }
75
GetHealthStatus(const JSIValue thisVal,const JSIValue * args,uint8_t argsNum)76 JSIValue BatteryModule::GetHealthStatus(const JSIValue thisVal, const JSIValue *args, uint8_t argsNum)
77 {
78 JSIValue undefValue = JSI::CreateUndefined();
79 int32_t healthStatus = 0;
80 if ((args == nullptr) || (argsNum == 0) || JSI::ValueIsUndefined(args[0])) {
81 return undefValue;
82 }
83
84 healthStatus = GetHealthStatusImpl();
85 JSIValue result = JSI::CreateObject();
86 JSI::SetNumberProperty(result, "healthStatus", healthStatus);
87 SuccessCallBack(thisVal, args[0], result);
88 JSI::ReleaseValue(result);
89 return undefValue;
90 }
91
GetPluggedType(const JSIValue thisVal,const JSIValue * args,uint8_t argsNum)92 JSIValue BatteryModule::GetPluggedType(const JSIValue thisVal, const JSIValue *args, uint8_t argsNum)
93 {
94 JSIValue undefValue = JSI::CreateUndefined();
95 int32_t pluggedType = 0;
96 if ((args == nullptr) || (argsNum == 0) || JSI::ValueIsUndefined(args[0])) {
97 return undefValue;
98 }
99
100 pluggedType = GetPluggedTypeImpl();
101 JSIValue result = JSI::CreateObject();
102 JSI::SetNumberProperty(result, "pluggedType", pluggedType);
103 SuccessCallBack(thisVal, args[0], result);
104 JSI::ReleaseValue(result);
105 return undefValue;
106 }
107
108
GetVoltage(const JSIValue thisVal,const JSIValue * args,uint8_t argsNum)109 JSIValue BatteryModule::GetVoltage(const JSIValue thisVal, const JSIValue *args, uint8_t argsNum)
110 {
111 JSIValue undefValue = JSI::CreateUndefined();
112 int32_t voltage = 0;
113 if ((args == nullptr) || (argsNum == 0) || JSI::ValueIsUndefined(args[0])) {
114 return undefValue;
115 }
116
117 voltage = GetBatVoltageImpl();
118 JSIValue result = JSI::CreateObject();
119 JSI::SetNumberProperty(result, "voltage", voltage);
120 SuccessCallBack(thisVal, args[0], result);
121 JSI::ReleaseValue(result);
122 return undefValue;
123 }
124
GetTechnology(const JSIValue thisVal,const JSIValue * args,uint8_t argsNum)125 JSIValue BatteryModule::GetTechnology(const JSIValue thisVal, const JSIValue *args, uint8_t argsNum)
126 {
127 JSIValue undefValue = JSI::CreateUndefined();
128 char *technology = NULL;
129 if ((args == nullptr) || (argsNum == 0) || JSI::ValueIsUndefined(args[0])) {
130 return undefValue;
131 }
132
133 technology = GetBatTechnologyImpl();
134 JSIValue result = JSI::CreateObject();
135 JSI::SetStringProperty(result, "technology", technology);
136 SuccessCallBack(thisVal, args[0], result);
137 JSI::ReleaseValue(result);
138 return undefValue;
139 }
140
GetTemperature(const JSIValue thisVal,const JSIValue * args,uint8_t argsNum)141 JSIValue BatteryModule::GetTemperature(const JSIValue thisVal, const JSIValue *args, uint8_t argsNum)
142 {
143 JSIValue undefValue = JSI::CreateUndefined();
144 int32_t temperature = 0;
145 if ((args == nullptr) || (argsNum == 0) || JSI::ValueIsUndefined(args[0])) {
146 return undefValue;
147 }
148
149 temperature = GetBatTemperature();
150 JSIValue result = JSI::CreateObject();
151 JSI::SetNumberProperty(result, "temperature", temperature);
152 SuccessCallBack(thisVal, args[0], result);
153 JSI::ReleaseValue(result);
154 return undefValue;
155 }
156
InitBatteryModule(JSIValue exports)157 void InitBatteryModule(JSIValue exports)
158 {
159 POWER_HILOGE("InitBatteryModule start");
160 JSI::SetModuleAPI(exports, "BatterySOC", BatteryModule::GetBatterySOC);
161 JSI::SetModuleAPI(exports, "ChargingStatus", BatteryModule::GetChargingStatus);
162 JSI::SetModuleAPI(exports, "HealthStatus", BatteryModule::GetHealthStatus);
163 JSI::SetModuleAPI(exports, "PluggedType", BatteryModule::GetPluggedType);
164 JSI::SetModuleAPI(exports, "Voltage", BatteryModule::GetVoltage);
165 JSI::SetModuleAPI(exports, "Technology", BatteryModule::GetTechnology);
166 JSI::SetModuleAPI(exports, "Temperature", BatteryModule::GetTemperature);
167 POWER_HILOGE("InitBatteryModule end");
168 }
169 } // ACELite
170 } // OHOS
171
172