1 /*
2 * Copyright (c) 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_interface_impl.h"
17 #include "hdf_base.h"
18 #include "battery_config.h"
19 #include "battery_log.h"
20
21 namespace OHOS {
22 namespace HDI {
23 namespace Battery {
24 namespace V1_2 {
25 namespace {
26 sptr<BatteryInterfaceImpl::BatteryDeathRecipient> g_deathRecipient = nullptr;
27 bool g_isHdiStart = false;
28 }
29
BatteryInterfaceImplGetInstance(void)30 extern "C" IBatteryInterface *BatteryInterfaceImplGetInstance(void)
31 {
32 using OHOS::HDI::Battery::V1_2::BatteryInterfaceImpl;
33 BatteryInterfaceImpl *service = new (std::nothrow) BatteryInterfaceImpl();
34 if (service == nullptr) {
35 return nullptr;
36 }
37
38 if (service->Init() != HDF_SUCCESS) {
39 delete service;
40 return nullptr;
41 }
42
43 return service;
44 }
45
Init()46 int32_t BatteryInterfaceImpl::Init()
47 {
48 provider_ = std::make_unique<OHOS::HDI::Battery::V1_2::PowerSupplyProvider>();
49 if (provider_ == nullptr) {
50 BATTERY_HILOGE(COMP_HDI, "make_unique PowerSupplyProvider error");
51 return HDF_ERR_MALLOC_FAIL;
52 }
53 provider_->InitBatteryPath();
54 provider_->InitPowerSupplySysfs();
55
56 auto& batteryConfig = BatteryConfig::GetInstance();
57 batteryConfig.ParseConfig();
58 currentPath_ = batteryConfig.GetString("charger.current_limit.path");
59 voltagePath_ = batteryConfig.GetString("charger.voltage_limit.path");
60 chargeTypePath_ = batteryConfig.GetString("charger.type.path");
61
62 // The configuration can be released when it is not needed
63 BatteryConfig::DestroyInstance();
64
65 loop_ = std::make_unique<OHOS::HDI::Battery::V1_2::BatteryThread>();
66 if (loop_ == nullptr) {
67 BATTERY_HILOGE(COMP_HDI, "make_unique BatteryThread error");
68 return HDF_ERR_MALLOC_FAIL;
69 }
70
71 if (batteryCallback_ != nullptr) {
72 loop_->InitCallback(batteryCallback_);
73 } else {
74 BATTERY_HILOGW(COMP_HDI, "batteryCallback_ is nullptr");
75 }
76 loop_->StartThread(this);
77
78 return HDF_SUCCESS;
79 }
80
Register(const sptr<IBatteryCallback> & callback)81 int32_t BatteryInterfaceImpl::Register(const sptr<IBatteryCallback>& callback)
82 {
83 if (callback == nullptr) {
84 BATTERY_HILOGW(FEATURE_BATT_INFO, "callback is nullptr");
85 return HDF_ERR_INVALID_PARAM;
86 }
87 if (!g_isHdiStart) {
88 batteryCallback_ = callback;
89 loop_->InitCallback(batteryCallback_);
90
91 g_deathRecipient = new BatteryInterfaceImpl::BatteryDeathRecipient(this);
92 if (g_deathRecipient == nullptr) {
93 BATTERY_HILOGE(COMP_HDI, "Failed to allocate BatteryDeathRecipient");
94 return HDF_ERR_MALLOC_FAIL;
95 }
96 AddBatteryDeathRecipient(batteryCallback_);
97 g_isHdiStart = true;
98 }
99 return HDF_SUCCESS;
100 }
101
UnRegister()102 int32_t BatteryInterfaceImpl::UnRegister()
103 {
104 RemoveBatteryDeathRecipient(batteryCallback_);
105 batteryCallback_ = nullptr;
106 g_isHdiStart = false;
107 return HDF_SUCCESS;
108 }
109
ChangePath(const std::string & path)110 int32_t BatteryInterfaceImpl::ChangePath(const std::string& path)
111 {
112 provider_->SetSysFilePath(path);
113 provider_->InitPowerSupplySysfs();
114 return HDF_SUCCESS;
115 }
116
GetCapacity(int32_t & capacity)117 int32_t BatteryInterfaceImpl::GetCapacity(int32_t& capacity)
118 {
119 return provider_->ParseCapacity(&capacity);
120 }
121
GetVoltage(int32_t & voltage)122 int32_t BatteryInterfaceImpl::GetVoltage(int32_t& voltage)
123 {
124 return provider_->ParseVoltage(&voltage);
125 }
126
GetTemperature(int32_t & temperature)127 int32_t BatteryInterfaceImpl::GetTemperature(int32_t& temperature)
128 {
129 return provider_->ParseTemperature(&temperature);
130 }
131
GetHealthState(BatteryHealthState & healthState)132 int32_t BatteryInterfaceImpl::GetHealthState(BatteryHealthState& healthState)
133 {
134 int32_t state = 0;
135 int32_t ret = provider_->ParseHealthState(&state);
136 if (ret != HDF_SUCCESS) {
137 return ret;
138 }
139
140 healthState = BatteryHealthState(state);
141 return HDF_SUCCESS;
142 }
143
GetPluggedType(BatteryPluggedType & pluggedType)144 int32_t BatteryInterfaceImpl::GetPluggedType(BatteryPluggedType& pluggedType)
145 {
146 int32_t type = 0;
147 int32_t ret = provider_->ParsePluggedType(&type);
148 if (ret != HDF_SUCCESS) {
149 return ret;
150 }
151
152 pluggedType = BatteryPluggedType(type);
153 return HDF_SUCCESS;
154 }
155
GetChargeState(BatteryChargeState & chargeState)156 int32_t BatteryInterfaceImpl::GetChargeState(BatteryChargeState& chargeState)
157 {
158 int32_t state = 0;
159 int32_t ret = provider_->ParseChargeState(&state);
160 if (ret != HDF_SUCCESS) {
161 return ret;
162 }
163
164 chargeState = BatteryChargeState(state);
165 return HDF_SUCCESS;
166 }
167
GetPresent(bool & present)168 int32_t BatteryInterfaceImpl::GetPresent(bool& present)
169 {
170 int8_t isPresent = 0;
171 int32_t ret = provider_->ParsePresent(&isPresent);
172 if (ret != HDF_SUCCESS) {
173 return ret;
174 }
175
176 present = bool(isPresent);
177 return HDF_SUCCESS;
178 }
179
GetTechnology(std::string & technology)180 int32_t BatteryInterfaceImpl::GetTechnology(std::string& technology)
181 {
182 return provider_->ParseTechnology(technology);
183 }
184
GetTotalEnergy(int32_t & totalEnergy)185 int32_t BatteryInterfaceImpl::GetTotalEnergy(int32_t& totalEnergy)
186 {
187 return provider_->ParseTotalEnergy(&totalEnergy);
188 }
189
GetCurrentAverage(int32_t & curAverage)190 int32_t BatteryInterfaceImpl::GetCurrentAverage(int32_t& curAverage)
191 {
192 return provider_->ParseCurrentAverage(&curAverage);
193 }
194
GetCurrentNow(int32_t & curNow)195 int32_t BatteryInterfaceImpl::GetCurrentNow(int32_t& curNow)
196 {
197 return provider_->ParseCurrentNow(&curNow);
198 }
199
GetRemainEnergy(int32_t & remainEnergy)200 int32_t BatteryInterfaceImpl::GetRemainEnergy(int32_t& remainEnergy)
201 {
202 return provider_->ParseRemainEnergy(&remainEnergy);
203 }
204
GetBatteryInfo(BatteryInfo & info)205 int32_t BatteryInterfaceImpl::GetBatteryInfo(BatteryInfo& info)
206 {
207 if (provider_ == nullptr) {
208 return HDF_FAILURE;
209 }
210
211 BatterydInfo batteryInfo = provider_->GetBatteryInfo();
212 info.capacity = batteryInfo.capacity_;
213 info.voltage = batteryInfo.voltage_;
214 info.temperature = batteryInfo.temperature_;
215 info.healthState = batteryInfo.healthState_;
216 info.pluggedType = batteryInfo.pluggedType_;
217 info.pluggedMaxCurrent = batteryInfo.pluggedMaxCurrent_;
218 info.pluggedMaxVoltage = batteryInfo.pluggedMaxVoltage_;
219 info.chargeState = batteryInfo.chargeState_;
220 info.chargeCounter = batteryInfo.chargeCounter_;
221 info.curNow = batteryInfo.curNow_;
222 info.curAverage = batteryInfo.curAverage_;
223 info.remainEnergy = batteryInfo.remainEnergy_;
224 info.totalEnergy = batteryInfo.totalEnergy_;
225 info.present = batteryInfo.present_;
226 info.technology = batteryInfo.technology_;
227
228 return HDF_SUCCESS;
229 }
230
SetChargingLimit(const std::vector<ChargingLimit> & chargingLimit)231 int32_t BatteryInterfaceImpl::SetChargingLimit(const std::vector<ChargingLimit>& chargingLimit)
232 {
233 return provider_->SetChargingLimit(chargingLimit, currentPath_, voltagePath_);
234 }
235
GetChargeType(ChargeType & chargeType)236 int32_t BatteryInterfaceImpl::GetChargeType(ChargeType& chargeType)
237 {
238 int32_t type = static_cast<int32_t>(CHARGE_TYPE_NONE);
239 int32_t ret = provider_->ParseChargeType(&type, chargeTypePath_);
240 if (ret != HDF_SUCCESS) {
241 return ret;
242 }
243
244 chargeType = ChargeType(type);
245 return HDF_SUCCESS;
246 }
247
AddBatteryDeathRecipient(const sptr<IBatteryCallback> & callback)248 int32_t BatteryInterfaceImpl::AddBatteryDeathRecipient(const sptr<IBatteryCallback>& callback)
249 {
250 const sptr<IRemoteObject>& remote = OHOS::HDI::hdi_objcast<IBatteryCallback>(callback);
251 bool result = remote->AddDeathRecipient(g_deathRecipient);
252 if (!result) {
253 BATTERY_HILOGE(COMP_HDI, "AddDeathRecipient fail");
254 return HDF_FAILURE;
255 }
256
257 return HDF_SUCCESS;
258 }
259
RemoveBatteryDeathRecipient(const sptr<IBatteryCallback> & callback)260 int32_t BatteryInterfaceImpl::RemoveBatteryDeathRecipient(const sptr<IBatteryCallback>& callback)
261 {
262 if (callback == nullptr) {
263 BATTERY_HILOGW(FEATURE_BATT_INFO, "remove callback is nullptr");
264 return HDF_ERR_INVALID_PARAM;
265 }
266 const sptr<IRemoteObject>& remote = OHOS::HDI::hdi_objcast<IBatteryCallback>(callback);
267 bool result = remote->RemoveDeathRecipient(g_deathRecipient);
268 if (!result) {
269 BATTERY_HILOGE(COMP_HDI, "RemoveDeathRecipient fail");
270 return HDF_FAILURE;
271 }
272
273 return HDF_SUCCESS;
274 }
275
OnRemoteDied(const wptr<IRemoteObject> & object)276 void BatteryInterfaceImpl::BatteryDeathRecipient::OnRemoteDied(const wptr<IRemoteObject>& object)
277 {
278 interfaceImpl_->UnRegister();
279 }
280 } // namespace V1_2
281 } // namespace Battery
282 } // namespace Hdi
283 } // namespace OHOS
284