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 V2_0 {
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::V2_0::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 powerSupplyProvider_ = std::make_unique<OHOS::HDI::Battery::V2_0::PowerSupplyProvider>();
49 if (powerSupplyProvider_ == nullptr) {
50 BATTERY_HILOGE(COMP_HDI, "make_unique PowerSupplyProvider error");
51 return HDF_ERR_MALLOC_FAIL;
52 }
53 powerSupplyProvider_->InitBatteryPath();
54 powerSupplyProvider_->InitPowerSupplySysfs();
55
56 auto& batteryConfig = BatteryConfig::GetInstance();
57
58 batteryConfig.ParseConfig();
59
60 loop_ = std::make_unique<OHOS::HDI::Battery::V2_0::BatteryThread>();
61 if (loop_ == nullptr) {
62 BATTERY_HILOGE(COMP_HDI, "make_unique BatteryThread error");
63 return HDF_ERR_MALLOC_FAIL;
64 }
65
66 if (batteryCallback_ != nullptr) {
67 loop_->InitCallback(batteryCallback_);
68 } else {
69 BATTERY_HILOGW(COMP_HDI, "batteryCallback_ is nullptr");
70 }
71 loop_->StartThread(this);
72
73 return HDF_SUCCESS;
74 }
75
Register(const sptr<IBatteryCallback> & callback)76 int32_t BatteryInterfaceImpl::Register(const sptr<IBatteryCallback>& callback)
77 {
78 if (callback == nullptr) {
79 BATTERY_HILOGW(FEATURE_BATT_INFO, "callback is nullptr");
80 return HDF_ERR_INVALID_PARAM;
81 }
82 if (!g_isHdiStart) {
83 batteryCallback_ = callback;
84 loop_->InitCallback(batteryCallback_);
85
86 g_deathRecipient = new BatteryInterfaceImpl::BatteryDeathRecipient(this);
87 if (g_deathRecipient == nullptr) {
88 BATTERY_HILOGE(COMP_HDI, "Failed to allocate BatteryDeathRecipient");
89 return HDF_ERR_MALLOC_FAIL;
90 }
91 AddBatteryDeathRecipient(batteryCallback_);
92 g_isHdiStart = true;
93 }
94 return HDF_SUCCESS;
95 }
96
UnRegister()97 int32_t BatteryInterfaceImpl::UnRegister()
98 {
99 RemoveBatteryDeathRecipient(batteryCallback_);
100 batteryCallback_ = nullptr;
101 g_isHdiStart = false;
102 return HDF_SUCCESS;
103 }
104
ChangePath(const std::string & path)105 int32_t BatteryInterfaceImpl::ChangePath(const std::string& path)
106 {
107 powerSupplyProvider_->SetSysFilePath(path);
108 powerSupplyProvider_->InitPowerSupplySysfs();
109 return HDF_SUCCESS;
110 }
111
GetCapacity(int32_t & capacity)112 int32_t BatteryInterfaceImpl::GetCapacity(int32_t& capacity)
113 {
114 return powerSupplyProvider_->ParseCapacity(&capacity);
115 }
116
GetVoltage(int32_t & voltage)117 int32_t BatteryInterfaceImpl::GetVoltage(int32_t& voltage)
118 {
119 return powerSupplyProvider_->ParseVoltage(&voltage);
120 }
121
GetTemperature(int32_t & temperature)122 int32_t BatteryInterfaceImpl::GetTemperature(int32_t& temperature)
123 {
124 return powerSupplyProvider_->ParseTemperature(&temperature);
125 }
126
GetHealthState(BatteryHealthState & healthState)127 int32_t BatteryInterfaceImpl::GetHealthState(BatteryHealthState& healthState)
128 {
129 int32_t state = 0;
130 int32_t ret = powerSupplyProvider_->ParseHealthState(&state);
131 if (ret != HDF_SUCCESS) {
132 return ret;
133 }
134
135 healthState = BatteryHealthState(state);
136 return HDF_SUCCESS;
137 }
138
GetPluggedType(BatteryPluggedType & pluggedType)139 int32_t BatteryInterfaceImpl::GetPluggedType(BatteryPluggedType& pluggedType)
140 {
141 int32_t type = 0;
142 int32_t ret = powerSupplyProvider_->ParsePluggedType(&type);
143 if (ret != HDF_SUCCESS) {
144 return ret;
145 }
146
147 pluggedType = BatteryPluggedType(type);
148 return HDF_SUCCESS;
149 }
150
GetChargeState(BatteryChargeState & chargeState)151 int32_t BatteryInterfaceImpl::GetChargeState(BatteryChargeState& chargeState)
152 {
153 int32_t state = 0;
154 int32_t ret = powerSupplyProvider_->ParseChargeState(&state);
155 if (ret != HDF_SUCCESS) {
156 return ret;
157 }
158
159 chargeState = BatteryChargeState(state);
160 return HDF_SUCCESS;
161 }
162
GetPresent(bool & present)163 int32_t BatteryInterfaceImpl::GetPresent(bool& present)
164 {
165 int8_t isPresent = 0;
166 int32_t ret = powerSupplyProvider_->ParsePresent(&isPresent);
167 if (ret != HDF_SUCCESS) {
168 return ret;
169 }
170
171 present = bool(isPresent);
172 return HDF_SUCCESS;
173 }
174
GetTechnology(std::string & technology)175 int32_t BatteryInterfaceImpl::GetTechnology(std::string& technology)
176 {
177 return powerSupplyProvider_->ParseTechnology(technology);
178 }
179
GetTotalEnergy(int32_t & totalEnergy)180 int32_t BatteryInterfaceImpl::GetTotalEnergy(int32_t& totalEnergy)
181 {
182 return powerSupplyProvider_->ParseTotalEnergy(&totalEnergy);
183 }
184
GetCurrentAverage(int32_t & curAverage)185 int32_t BatteryInterfaceImpl::GetCurrentAverage(int32_t& curAverage)
186 {
187 return powerSupplyProvider_->ParseCurrentAverage(&curAverage);
188 }
189
GetCurrentNow(int32_t & curNow)190 int32_t BatteryInterfaceImpl::GetCurrentNow(int32_t& curNow)
191 {
192 return powerSupplyProvider_->ParseCurrentNow(&curNow);
193 }
194
GetRemainEnergy(int32_t & remainEnergy)195 int32_t BatteryInterfaceImpl::GetRemainEnergy(int32_t& remainEnergy)
196 {
197 return powerSupplyProvider_->ParseRemainEnergy(&remainEnergy);
198 }
199
GetBatteryInfo(BatteryInfo & info)200 int32_t BatteryInterfaceImpl::GetBatteryInfo(BatteryInfo& info)
201 {
202 if (powerSupplyProvider_ == nullptr) {
203 return HDF_FAILURE;
204 }
205
206 BatterydInfo batteryInfo = powerSupplyProvider_->GetBatteryInfo();
207 info.capacity = batteryInfo.capacity_;
208 info.voltage = batteryInfo.voltage_;
209 info.temperature = batteryInfo.temperature_;
210 info.healthState = batteryInfo.healthState_;
211 info.pluggedType = batteryInfo.pluggedType_;
212 info.pluggedMaxCurrent = batteryInfo.pluggedMaxCurrent_;
213 info.pluggedMaxVoltage = batteryInfo.pluggedMaxVoltage_;
214 info.chargeState = batteryInfo.chargeState_;
215 info.chargeCounter = batteryInfo.chargeCounter_;
216 info.curNow = batteryInfo.curNow_;
217 info.curAverage = batteryInfo.curAverage_;
218 info.remainEnergy = batteryInfo.remainEnergy_;
219 info.totalEnergy = batteryInfo.totalEnergy_;
220 info.present = batteryInfo.present_;
221 info.technology = batteryInfo.technology_;
222
223 return HDF_SUCCESS;
224 }
225
SetChargingLimit(const std::vector<ChargingLimit> & chargingLimit)226 int32_t BatteryInterfaceImpl::SetChargingLimit(const std::vector<ChargingLimit>& chargingLimit)
227 {
228 auto& batteryConfig = BatteryConfig::GetInstance();
229 BatteryConfig::ChargerConfig chargerConfig = batteryConfig.GetChargerConfig();
230
231 return powerSupplyProvider_->SetChargingLimit(chargingLimit, chargerConfig.currentPath, chargerConfig.voltagePath);
232 }
233
GetChargeType(ChargeType & chargeType)234 int32_t BatteryInterfaceImpl::GetChargeType(ChargeType& chargeType)
235 {
236 auto& batteryConfig = BatteryConfig::GetInstance();
237 BatteryConfig::ChargerConfig chargerConfig = batteryConfig.GetChargerConfig();
238
239 int32_t type = static_cast<int32_t>(CHARGE_TYPE_NONE);
240 int32_t ret = powerSupplyProvider_->ParseChargeType(&type, chargerConfig.chargeTypePath);
241 if (ret != HDF_SUCCESS) {
242 return ret;
243 }
244
245 chargeType = ChargeType(type);
246 return HDF_SUCCESS;
247 }
248
SetBatteryConfig(const std::string & sceneName,const std::string & value)249 int32_t BatteryInterfaceImpl::SetBatteryConfig(const std::string& sceneName, const std::string& value)
250 {
251 auto& batteryConfig = BatteryConfig::GetInstance();
252 std::map<std::string, BatteryConfig::ChargeSceneConfig>
253 chargeSceneConfigMap = batteryConfig.GetChargeSceneConfigMap();
254 if (chargeSceneConfigMap.empty()) {
255 BATTERY_HILOGE(FEATURE_BATT_INFO, "chargeSceneConfigMap is empty");
256 return HDF_ERR_NOT_SUPPORT;
257 }
258
259 std::map<std::string, BatteryConfig::ChargeSceneConfig>::iterator it = chargeSceneConfigMap.find(sceneName);
260 if (it != chargeSceneConfigMap.end()) {
261 std::string setPath = (it -> second).setPath;
262 return powerSupplyProvider_->SetConfigByPath(setPath, value);
263 }
264
265 BATTERY_HILOGW(FEATURE_BATT_INFO, "key:%{public}s not found", sceneName.c_str());
266 return HDF_ERR_NOT_SUPPORT;
267 }
268
GetBatteryConfig(const std::string & sceneName,std::string & value)269 int32_t BatteryInterfaceImpl::GetBatteryConfig(const std::string& sceneName, std::string& value)
270 {
271 auto& batteryConfig = BatteryConfig::GetInstance();
272 std::map<std::string, BatteryConfig::ChargeSceneConfig>
273 chargeSceneConfigMap = batteryConfig.GetChargeSceneConfigMap();
274 if (chargeSceneConfigMap.empty()) {
275 BATTERY_HILOGE(FEATURE_BATT_INFO, "chargeSceneConfigMap is empty");
276 value = "";
277 return HDF_ERR_NOT_SUPPORT;
278 }
279
280 std::map<std::string, BatteryConfig::ChargeSceneConfig>::iterator it = chargeSceneConfigMap.find(sceneName);
281 if (it != chargeSceneConfigMap.end()) {
282 std::string getPath = (it -> second).getPath;
283 return powerSupplyProvider_->GetConfigByPath(getPath, value);
284 }
285
286 BATTERY_HILOGE(FEATURE_BATT_INFO, "key:%{public}s not found", sceneName.c_str());
287 value = "";
288 return HDF_ERR_NOT_SUPPORT;
289 }
290
IsBatteryConfigSupported(const std::string & sceneName,bool & value)291 int32_t BatteryInterfaceImpl::IsBatteryConfigSupported(const std::string& sceneName, bool& value)
292 {
293 auto& batteryConfig = BatteryConfig::GetInstance();
294 std::map<std::string, BatteryConfig::ChargeSceneConfig>
295 chargeSceneConfigMap = batteryConfig.GetChargeSceneConfigMap();
296 if (chargeSceneConfigMap.empty()) {
297 BATTERY_HILOGE(FEATURE_BATT_INFO, "chargeSceneConfigMap is empty");
298 value = false;
299 return HDF_ERR_NOT_SUPPORT;
300 }
301
302 std::map<std::string, BatteryConfig::ChargeSceneConfig>::iterator it = chargeSceneConfigMap.find(sceneName);
303 if (it != chargeSceneConfigMap.end()) {
304 std::string supportPath = (it -> second).supportPath;
305 std::string type = (it -> second).type;
306 std::string expectValue = (it -> second).expectValue;
307 BATTERY_HILOGI(FEATURE_BATT_INFO,
308 "is support charge config, path:%{public}s, type:%{public}s, expect_value:%{public}s",
309 supportPath.c_str(), type.c_str(), expectValue.c_str());
310
311 if (type == "dir") {
312 return powerSupplyProvider_->CheckPathExists(supportPath, value);
313 } else if (type == "file") {
314 std::string temp;
315 int ret = powerSupplyProvider_->GetConfigByPath(supportPath, temp);
316 value = ret == HDF_SUCCESS ? expectValue == temp : false;
317 return ret;
318 } else {
319 value = false;
320 return HDF_SUCCESS;
321 }
322 }
323 BATTERY_HILOGE(FEATURE_BATT_INFO, "key:%{public}s not found", sceneName.c_str());
324 value = false;
325 return HDF_ERR_NOT_SUPPORT;
326 }
327
AddBatteryDeathRecipient(const sptr<IBatteryCallback> & callback)328 int32_t BatteryInterfaceImpl::AddBatteryDeathRecipient(const sptr<IBatteryCallback>& callback)
329 {
330 const sptr<IRemoteObject>& remote = OHOS::HDI::hdi_objcast<IBatteryCallback>(callback);
331 bool result = remote->AddDeathRecipient(g_deathRecipient);
332 if (!result) {
333 BATTERY_HILOGE(COMP_HDI, "AddDeathRecipient fail");
334 return HDF_FAILURE;
335 }
336
337 return HDF_SUCCESS;
338 }
339
RemoveBatteryDeathRecipient(const sptr<IBatteryCallback> & callback)340 int32_t BatteryInterfaceImpl::RemoveBatteryDeathRecipient(const sptr<IBatteryCallback>& callback)
341 {
342 if (callback == nullptr) {
343 BATTERY_HILOGW(FEATURE_BATT_INFO, "remove callback is nullptr");
344 return HDF_ERR_INVALID_PARAM;
345 }
346 const sptr<IRemoteObject>& remote = OHOS::HDI::hdi_objcast<IBatteryCallback>(callback);
347 bool result = remote->RemoveDeathRecipient(g_deathRecipient);
348 if (!result) {
349 BATTERY_HILOGE(COMP_HDI, "RemoveDeathRecipient fail");
350 return HDF_FAILURE;
351 }
352
353 return HDF_SUCCESS;
354 }
355
OnRemoteDied(const wptr<IRemoteObject> & object)356 void BatteryInterfaceImpl::BatteryDeathRecipient::OnRemoteDied(const wptr<IRemoteObject>& object)
357 {
358 interfaceImpl_->UnRegister();
359 }
360 } // namespace V2_0
361 } // namespace Battery
362 } // namespace Hdi
363 } // namespace OHOS
364