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