1094332d3Sopenharmony_ci/*
2094332d3Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
3094332d3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4094332d3Sopenharmony_ci * you may not use this file except in compliance with the License.
5094332d3Sopenharmony_ci * You may obtain a copy of the License at
6094332d3Sopenharmony_ci *
7094332d3Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8094332d3Sopenharmony_ci *
9094332d3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10094332d3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11094332d3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12094332d3Sopenharmony_ci * See the License for the specific language governing permissions and
13094332d3Sopenharmony_ci * limitations under the License.
14094332d3Sopenharmony_ci */
15094332d3Sopenharmony_ci
16094332d3Sopenharmony_ci#include "battery_interface_impl.h"
17094332d3Sopenharmony_ci#include "battery_config.h"
18094332d3Sopenharmony_ci#include "battery_log.h"
19094332d3Sopenharmony_ci#include "battery_xcollie.h"
20094332d3Sopenharmony_ci
21094332d3Sopenharmony_cinamespace OHOS {
22094332d3Sopenharmony_cinamespace HDI {
23094332d3Sopenharmony_cinamespace Battery {
24094332d3Sopenharmony_cinamespace V2_0 {
25094332d3Sopenharmony_cinamespace {
26094332d3Sopenharmony_cisptr<BatteryInterfaceImpl::BatteryDeathRecipient> g_deathRecipient = nullptr;
27094332d3Sopenharmony_cibool g_isHdiStart = false;
28094332d3Sopenharmony_ci}
29094332d3Sopenharmony_ci
30094332d3Sopenharmony_ciextern "C" IBatteryInterface *BatteryInterfaceImplGetInstance(void)
31094332d3Sopenharmony_ci{
32094332d3Sopenharmony_ci    using OHOS::HDI::Battery::V2_0::BatteryInterfaceImpl;
33094332d3Sopenharmony_ci    BatteryInterfaceImpl *service = new (std::nothrow) BatteryInterfaceImpl();
34094332d3Sopenharmony_ci    if (service == nullptr) {
35094332d3Sopenharmony_ci        return nullptr;
36094332d3Sopenharmony_ci    }
37094332d3Sopenharmony_ci
38094332d3Sopenharmony_ci    if (service->Init() != HDF_SUCCESS) {
39094332d3Sopenharmony_ci        delete service;
40094332d3Sopenharmony_ci        return nullptr;
41094332d3Sopenharmony_ci    }
42094332d3Sopenharmony_ci
43094332d3Sopenharmony_ci    return service;
44094332d3Sopenharmony_ci}
45094332d3Sopenharmony_ci
46094332d3Sopenharmony_ciint32_t BatteryInterfaceImpl::Init()
47094332d3Sopenharmony_ci{
48094332d3Sopenharmony_ci    powerSupplyProvider_ = std::make_unique<OHOS::HDI::Battery::V2_0::PowerSupplyProvider>();
49094332d3Sopenharmony_ci    if (powerSupplyProvider_ == nullptr) {
50094332d3Sopenharmony_ci        BATTERY_HILOGE(COMP_HDI, "make_unique PowerSupplyProvider error");
51094332d3Sopenharmony_ci        return HDF_ERR_MALLOC_FAIL;
52094332d3Sopenharmony_ci    }
53094332d3Sopenharmony_ci    powerSupplyProvider_->InitBatteryPath();
54094332d3Sopenharmony_ci    powerSupplyProvider_->InitPowerSupplySysfs();
55094332d3Sopenharmony_ci
56094332d3Sopenharmony_ci    auto& batteryConfig = BatteryConfig::GetInstance();
57094332d3Sopenharmony_ci    batteryConfig.ParseConfig();
58094332d3Sopenharmony_ci
59094332d3Sopenharmony_ci    loop_ = std::make_unique<OHOS::HDI::Battery::V2_0::BatteryThread>();
60094332d3Sopenharmony_ci    if (loop_ == nullptr) {
61094332d3Sopenharmony_ci        BATTERY_HILOGE(COMP_HDI, "make_unique BatteryThread error");
62094332d3Sopenharmony_ci        return HDF_ERR_MALLOC_FAIL;
63094332d3Sopenharmony_ci    }
64094332d3Sopenharmony_ci
65094332d3Sopenharmony_ci    if (batteryCallback_ != nullptr) {
66094332d3Sopenharmony_ci        loop_->InitCallback(batteryCallback_);
67094332d3Sopenharmony_ci    } else {
68094332d3Sopenharmony_ci        BATTERY_HILOGW(COMP_HDI, "batteryCallback_ is nullptr");
69094332d3Sopenharmony_ci    }
70094332d3Sopenharmony_ci    loop_->StartThread(this);
71094332d3Sopenharmony_ci
72094332d3Sopenharmony_ci    return HDF_SUCCESS;
73094332d3Sopenharmony_ci}
74094332d3Sopenharmony_ci
75094332d3Sopenharmony_ciint32_t BatteryInterfaceImpl::Register(const sptr<IBatteryCallback>& callback)
76094332d3Sopenharmony_ci{
77094332d3Sopenharmony_ci    if (callback == nullptr) {
78094332d3Sopenharmony_ci        BATTERY_HILOGW(FEATURE_BATT_INFO, "callback is nullptr");
79094332d3Sopenharmony_ci        return HDF_ERR_INVALID_PARAM;
80094332d3Sopenharmony_ci    }
81094332d3Sopenharmony_ci    if (!g_isHdiStart) {
82094332d3Sopenharmony_ci        batteryCallback_ = callback;
83094332d3Sopenharmony_ci        loop_->InitCallback(batteryCallback_);
84094332d3Sopenharmony_ci
85094332d3Sopenharmony_ci        g_deathRecipient = new BatteryInterfaceImpl::BatteryDeathRecipient(this);
86094332d3Sopenharmony_ci        if (g_deathRecipient == nullptr) {
87094332d3Sopenharmony_ci            BATTERY_HILOGE(COMP_HDI, "Failed to allocate BatteryDeathRecipient");
88094332d3Sopenharmony_ci            return HDF_ERR_MALLOC_FAIL;
89094332d3Sopenharmony_ci        }
90094332d3Sopenharmony_ci        AddBatteryDeathRecipient(batteryCallback_);
91094332d3Sopenharmony_ci        g_isHdiStart = true;
92094332d3Sopenharmony_ci    }
93094332d3Sopenharmony_ci    return HDF_SUCCESS;
94094332d3Sopenharmony_ci}
95094332d3Sopenharmony_ci
96094332d3Sopenharmony_ciint32_t BatteryInterfaceImpl::UnRegister()
97094332d3Sopenharmony_ci{
98094332d3Sopenharmony_ci    RemoveBatteryDeathRecipient(batteryCallback_);
99094332d3Sopenharmony_ci    batteryCallback_ = nullptr;
100094332d3Sopenharmony_ci    g_isHdiStart = false;
101094332d3Sopenharmony_ci    return HDF_SUCCESS;
102094332d3Sopenharmony_ci}
103094332d3Sopenharmony_ci
104094332d3Sopenharmony_ciint32_t BatteryInterfaceImpl::ChangePath(const std::string& path)
105094332d3Sopenharmony_ci{
106094332d3Sopenharmony_ci    powerSupplyProvider_->SetSysFilePath(path);
107094332d3Sopenharmony_ci    powerSupplyProvider_->InitPowerSupplySysfs();
108094332d3Sopenharmony_ci    return HDF_SUCCESS;
109094332d3Sopenharmony_ci}
110094332d3Sopenharmony_ci
111094332d3Sopenharmony_ciint32_t BatteryInterfaceImpl::GetCapacity(int32_t& capacity)
112094332d3Sopenharmony_ci{
113094332d3Sopenharmony_ci    return powerSupplyProvider_->ParseCapacity(&capacity);
114094332d3Sopenharmony_ci}
115094332d3Sopenharmony_ci
116094332d3Sopenharmony_ciint32_t BatteryInterfaceImpl::GetVoltage(int32_t& voltage)
117094332d3Sopenharmony_ci{
118094332d3Sopenharmony_ci    return powerSupplyProvider_->ParseVoltage(&voltage);
119094332d3Sopenharmony_ci}
120094332d3Sopenharmony_ci
121094332d3Sopenharmony_ciint32_t BatteryInterfaceImpl::GetTemperature(int32_t& temperature)
122094332d3Sopenharmony_ci{
123094332d3Sopenharmony_ci    return powerSupplyProvider_->ParseTemperature(&temperature);
124094332d3Sopenharmony_ci}
125094332d3Sopenharmony_ci
126094332d3Sopenharmony_ciint32_t BatteryInterfaceImpl::GetHealthState(BatteryHealthState& healthState)
127094332d3Sopenharmony_ci{
128094332d3Sopenharmony_ci    int32_t state = 0;
129094332d3Sopenharmony_ci    int32_t ret = powerSupplyProvider_->ParseHealthState(&state);
130094332d3Sopenharmony_ci    if (ret != HDF_SUCCESS) {
131094332d3Sopenharmony_ci        return ret;
132094332d3Sopenharmony_ci    }
133094332d3Sopenharmony_ci
134094332d3Sopenharmony_ci    healthState = BatteryHealthState(state);
135094332d3Sopenharmony_ci    return HDF_SUCCESS;
136094332d3Sopenharmony_ci}
137094332d3Sopenharmony_ci
138094332d3Sopenharmony_ciint32_t BatteryInterfaceImpl::GetPluggedType(BatteryPluggedType& pluggedType)
139094332d3Sopenharmony_ci{
140094332d3Sopenharmony_ci    int32_t type = 0;
141094332d3Sopenharmony_ci    int32_t ret = powerSupplyProvider_->ParsePluggedType(&type);
142094332d3Sopenharmony_ci    if (ret != HDF_SUCCESS) {
143094332d3Sopenharmony_ci        return ret;
144094332d3Sopenharmony_ci    }
145094332d3Sopenharmony_ci
146094332d3Sopenharmony_ci    pluggedType = BatteryPluggedType(type);
147094332d3Sopenharmony_ci    return HDF_SUCCESS;
148094332d3Sopenharmony_ci}
149094332d3Sopenharmony_ci
150094332d3Sopenharmony_ciint32_t BatteryInterfaceImpl::GetChargeState(BatteryChargeState& chargeState)
151094332d3Sopenharmony_ci{
152094332d3Sopenharmony_ci    int32_t state = 0;
153094332d3Sopenharmony_ci    int32_t ret = powerSupplyProvider_->ParseChargeState(&state);
154094332d3Sopenharmony_ci    if (ret != HDF_SUCCESS) {
155094332d3Sopenharmony_ci        return ret;
156094332d3Sopenharmony_ci    }
157094332d3Sopenharmony_ci
158094332d3Sopenharmony_ci    chargeState = BatteryChargeState(state);
159094332d3Sopenharmony_ci    return HDF_SUCCESS;
160094332d3Sopenharmony_ci}
161094332d3Sopenharmony_ci
162094332d3Sopenharmony_ciint32_t BatteryInterfaceImpl::GetPresent(bool& present)
163094332d3Sopenharmony_ci{
164094332d3Sopenharmony_ci    int8_t isPresent = 0;
165094332d3Sopenharmony_ci    int32_t ret = powerSupplyProvider_->ParsePresent(&isPresent);
166094332d3Sopenharmony_ci    if (ret != HDF_SUCCESS) {
167094332d3Sopenharmony_ci        return ret;
168094332d3Sopenharmony_ci    }
169094332d3Sopenharmony_ci
170094332d3Sopenharmony_ci    present = bool(isPresent);
171094332d3Sopenharmony_ci    return HDF_SUCCESS;
172094332d3Sopenharmony_ci}
173094332d3Sopenharmony_ci
174094332d3Sopenharmony_ciint32_t BatteryInterfaceImpl::GetTechnology(std::string& technology)
175094332d3Sopenharmony_ci{
176094332d3Sopenharmony_ci    return powerSupplyProvider_->ParseTechnology(technology);
177094332d3Sopenharmony_ci}
178094332d3Sopenharmony_ci
179094332d3Sopenharmony_ciint32_t BatteryInterfaceImpl::GetTotalEnergy(int32_t& totalEnergy)
180094332d3Sopenharmony_ci{
181094332d3Sopenharmony_ci    return powerSupplyProvider_->ParseTotalEnergy(&totalEnergy);
182094332d3Sopenharmony_ci}
183094332d3Sopenharmony_ci
184094332d3Sopenharmony_ciint32_t BatteryInterfaceImpl::GetCurrentAverage(int32_t& curAverage)
185094332d3Sopenharmony_ci{
186094332d3Sopenharmony_ci    return powerSupplyProvider_->ParseCurrentAverage(&curAverage);
187094332d3Sopenharmony_ci}
188094332d3Sopenharmony_ci
189094332d3Sopenharmony_ciint32_t BatteryInterfaceImpl::GetCurrentNow(int32_t& curNow)
190094332d3Sopenharmony_ci{
191094332d3Sopenharmony_ci    return powerSupplyProvider_->ParseCurrentNow(&curNow);
192094332d3Sopenharmony_ci}
193094332d3Sopenharmony_ci
194094332d3Sopenharmony_ciint32_t BatteryInterfaceImpl::GetRemainEnergy(int32_t& remainEnergy)
195094332d3Sopenharmony_ci{
196094332d3Sopenharmony_ci    return powerSupplyProvider_->ParseRemainEnergy(&remainEnergy);
197094332d3Sopenharmony_ci}
198094332d3Sopenharmony_ci
199094332d3Sopenharmony_ciint32_t BatteryInterfaceImpl::GetBatteryInfo(BatteryInfo& info)
200094332d3Sopenharmony_ci{
201094332d3Sopenharmony_ci    if (powerSupplyProvider_ == nullptr) {
202094332d3Sopenharmony_ci        return HDF_FAILURE;
203094332d3Sopenharmony_ci    }
204094332d3Sopenharmony_ci
205094332d3Sopenharmony_ci    BatterydInfo batteryInfo = powerSupplyProvider_->GetBatteryInfo();
206094332d3Sopenharmony_ci    info.capacity = batteryInfo.capacity_;
207094332d3Sopenharmony_ci    info.voltage = batteryInfo.voltage_;
208094332d3Sopenharmony_ci    info.temperature = batteryInfo.temperature_;
209094332d3Sopenharmony_ci    info.healthState = batteryInfo.healthState_;
210094332d3Sopenharmony_ci    info.pluggedType = batteryInfo.pluggedType_;
211094332d3Sopenharmony_ci    info.pluggedMaxCurrent = batteryInfo.pluggedMaxCurrent_;
212094332d3Sopenharmony_ci    info.pluggedMaxVoltage = batteryInfo.pluggedMaxVoltage_;
213094332d3Sopenharmony_ci    info.chargeState = batteryInfo.chargeState_;
214094332d3Sopenharmony_ci    info.chargeCounter = batteryInfo.chargeCounter_;
215094332d3Sopenharmony_ci    info.curNow = batteryInfo.curNow_;
216094332d3Sopenharmony_ci    info.curAverage = batteryInfo.curAverage_;
217094332d3Sopenharmony_ci    info.remainEnergy = batteryInfo.remainEnergy_;
218094332d3Sopenharmony_ci    info.totalEnergy = batteryInfo.totalEnergy_;
219094332d3Sopenharmony_ci    info.present = batteryInfo.present_;
220094332d3Sopenharmony_ci    info.technology = batteryInfo.technology_;
221094332d3Sopenharmony_ci
222094332d3Sopenharmony_ci    return HDF_SUCCESS;
223094332d3Sopenharmony_ci}
224094332d3Sopenharmony_ci
225094332d3Sopenharmony_ciint32_t BatteryInterfaceImpl::SetChargingLimit(const std::vector<ChargingLimit>& chargingLimit)
226094332d3Sopenharmony_ci{
227094332d3Sopenharmony_ci    auto& batteryConfig = BatteryConfig::GetInstance();
228094332d3Sopenharmony_ci    BatteryConfig::ChargerConfig chargerConfig = batteryConfig.GetChargerConfig();
229094332d3Sopenharmony_ci
230094332d3Sopenharmony_ci    return powerSupplyProvider_->SetChargingLimit(chargingLimit, chargerConfig.currentPath, chargerConfig.voltagePath);
231094332d3Sopenharmony_ci}
232094332d3Sopenharmony_ci
233094332d3Sopenharmony_ciint32_t BatteryInterfaceImpl::GetChargeType(ChargeType& chargeType)
234094332d3Sopenharmony_ci{
235094332d3Sopenharmony_ci    auto& batteryConfig = BatteryConfig::GetInstance();
236094332d3Sopenharmony_ci    BatteryConfig::ChargerConfig chargerConfig = batteryConfig.GetChargerConfig();
237094332d3Sopenharmony_ci
238094332d3Sopenharmony_ci    int32_t type = static_cast<int32_t>(CHARGE_TYPE_NONE);
239094332d3Sopenharmony_ci    int32_t ret = powerSupplyProvider_->ParseChargeType(&type, chargerConfig.chargeTypePath);
240094332d3Sopenharmony_ci    if (ret != HDF_SUCCESS) {
241094332d3Sopenharmony_ci        return ret;
242094332d3Sopenharmony_ci    }
243094332d3Sopenharmony_ci
244094332d3Sopenharmony_ci    chargeType = ChargeType(type);
245094332d3Sopenharmony_ci    return HDF_SUCCESS;
246094332d3Sopenharmony_ci}
247094332d3Sopenharmony_ci
248094332d3Sopenharmony_ciint32_t BatteryInterfaceImpl::SetBatteryConfig(const std::string& sceneName, const std::string& value)
249094332d3Sopenharmony_ci{
250094332d3Sopenharmony_ci    Battery::BatteryXCollie batteryXcollie("Battery_SetBatteryConfig");
251094332d3Sopenharmony_ci    auto& batteryConfig = BatteryConfig::GetInstance();
252094332d3Sopenharmony_ci    std::map<std::string, BatteryConfig::ChargeSceneConfig>
253094332d3Sopenharmony_ci        chargeSceneConfigMap = batteryConfig.GetChargeSceneConfigMap();
254094332d3Sopenharmony_ci    if (chargeSceneConfigMap.empty()) {
255094332d3Sopenharmony_ci        BATTERY_HILOGE(FEATURE_BATT_INFO, "chargeSceneConfigMap is empty");
256094332d3Sopenharmony_ci        return HDF_ERR_NOT_SUPPORT;
257094332d3Sopenharmony_ci    }
258094332d3Sopenharmony_ci
259094332d3Sopenharmony_ci    std::map<std::string, BatteryConfig::ChargeSceneConfig>::iterator it = chargeSceneConfigMap.find(sceneName);
260094332d3Sopenharmony_ci    if (it != chargeSceneConfigMap.end()) {
261094332d3Sopenharmony_ci        std::string setPath = (it -> second).setPath;
262094332d3Sopenharmony_ci        return powerSupplyProvider_->SetConfigByPath(setPath, value);
263094332d3Sopenharmony_ci    }
264094332d3Sopenharmony_ci
265094332d3Sopenharmony_ci    BATTERY_HILOGW(FEATURE_BATT_INFO, "key:%{public}s not found", sceneName.c_str());
266094332d3Sopenharmony_ci    return HDF_ERR_NOT_SUPPORT;
267094332d3Sopenharmony_ci}
268094332d3Sopenharmony_ci
269094332d3Sopenharmony_ciint32_t BatteryInterfaceImpl::GetBatteryConfig(const std::string& sceneName, std::string& value)
270094332d3Sopenharmony_ci{
271094332d3Sopenharmony_ci    Battery::BatteryXCollie batteryXcollie("Battery_GetBatteryConfig");
272094332d3Sopenharmony_ci    auto& batteryConfig = BatteryConfig::GetInstance();
273094332d3Sopenharmony_ci    std::map<std::string, BatteryConfig::ChargeSceneConfig>
274094332d3Sopenharmony_ci        chargeSceneConfigMap = batteryConfig.GetChargeSceneConfigMap();
275094332d3Sopenharmony_ci    if (chargeSceneConfigMap.empty()) {
276094332d3Sopenharmony_ci        BATTERY_HILOGE(FEATURE_BATT_INFO, "chargeSceneConfigMap is empty");
277094332d3Sopenharmony_ci        value = "";
278094332d3Sopenharmony_ci        return HDF_ERR_NOT_SUPPORT;
279094332d3Sopenharmony_ci    }
280094332d3Sopenharmony_ci
281094332d3Sopenharmony_ci    std::map<std::string, BatteryConfig::ChargeSceneConfig>::iterator it = chargeSceneConfigMap.find(sceneName);
282094332d3Sopenharmony_ci    if (it != chargeSceneConfigMap.end()) {
283094332d3Sopenharmony_ci        std::string getPath = (it -> second).getPath;
284094332d3Sopenharmony_ci        return powerSupplyProvider_->GetConfigByPath(getPath, value);
285094332d3Sopenharmony_ci    }
286094332d3Sopenharmony_ci
287094332d3Sopenharmony_ci    BATTERY_HILOGE(FEATURE_BATT_INFO, "key:%{public}s not found", sceneName.c_str());
288094332d3Sopenharmony_ci    value = "";
289094332d3Sopenharmony_ci    return HDF_ERR_NOT_SUPPORT;
290094332d3Sopenharmony_ci}
291094332d3Sopenharmony_ci
292094332d3Sopenharmony_ciint32_t BatteryInterfaceImpl::IsBatteryConfigSupported(const std::string& sceneName, bool& value)
293094332d3Sopenharmony_ci{
294094332d3Sopenharmony_ci    Battery::BatteryXCollie batteryXcollie("Battery_IsBatteryConfigSupported");
295094332d3Sopenharmony_ci    auto& batteryConfig = BatteryConfig::GetInstance();
296094332d3Sopenharmony_ci    std::map<std::string, BatteryConfig::ChargeSceneConfig>
297094332d3Sopenharmony_ci        chargeSceneConfigMap = batteryConfig.GetChargeSceneConfigMap();
298094332d3Sopenharmony_ci    if (chargeSceneConfigMap.empty()) {
299094332d3Sopenharmony_ci        BATTERY_HILOGE(FEATURE_BATT_INFO, "chargeSceneConfigMap is empty");
300094332d3Sopenharmony_ci        value = false;
301094332d3Sopenharmony_ci        return HDF_ERR_NOT_SUPPORT;
302094332d3Sopenharmony_ci    }
303094332d3Sopenharmony_ci
304094332d3Sopenharmony_ci    std::map<std::string, BatteryConfig::ChargeSceneConfig>::iterator it = chargeSceneConfigMap.find(sceneName);
305094332d3Sopenharmony_ci    if (it != chargeSceneConfigMap.end()) {
306094332d3Sopenharmony_ci        std::string supportPath = (it -> second).supportPath;
307094332d3Sopenharmony_ci        std::string type = (it -> second).type;
308094332d3Sopenharmony_ci        std::string expectValue = (it -> second).expectValue;
309094332d3Sopenharmony_ci        BATTERY_HILOGI(FEATURE_BATT_INFO,
310094332d3Sopenharmony_ci            "is support charge config, path:%{public}s, type:%{public}s, expect_value:%{public}s",
311094332d3Sopenharmony_ci            supportPath.c_str(), type.c_str(), expectValue.c_str());
312094332d3Sopenharmony_ci
313094332d3Sopenharmony_ci        if (type == "dir") {
314094332d3Sopenharmony_ci            return powerSupplyProvider_->CheckPathExists(supportPath, value);
315094332d3Sopenharmony_ci        } else if (type == "file") {
316094332d3Sopenharmony_ci            std::string temp;
317094332d3Sopenharmony_ci            int ret = powerSupplyProvider_->GetConfigByPath(supportPath, temp);
318094332d3Sopenharmony_ci            value = ret == HDF_SUCCESS ? expectValue == temp : false;
319094332d3Sopenharmony_ci            return ret;
320094332d3Sopenharmony_ci        } else {
321094332d3Sopenharmony_ci            value = false;
322094332d3Sopenharmony_ci            return HDF_SUCCESS;
323094332d3Sopenharmony_ci        }
324094332d3Sopenharmony_ci    }
325094332d3Sopenharmony_ci    BATTERY_HILOGE(FEATURE_BATT_INFO, "key:%{public}s not found", sceneName.c_str());
326094332d3Sopenharmony_ci    value = false;
327094332d3Sopenharmony_ci    return HDF_ERR_NOT_SUPPORT;
328094332d3Sopenharmony_ci}
329094332d3Sopenharmony_ci
330094332d3Sopenharmony_ciint32_t BatteryInterfaceImpl::AddBatteryDeathRecipient(const sptr<IBatteryCallback>& callback)
331094332d3Sopenharmony_ci{
332094332d3Sopenharmony_ci    const sptr<IRemoteObject>& remote = OHOS::HDI::hdi_objcast<IBatteryCallback>(callback);
333094332d3Sopenharmony_ci    bool result = remote->AddDeathRecipient(g_deathRecipient);
334094332d3Sopenharmony_ci    if (!result) {
335094332d3Sopenharmony_ci        BATTERY_HILOGE(COMP_HDI, "AddDeathRecipient fail");
336094332d3Sopenharmony_ci        return HDF_FAILURE;
337094332d3Sopenharmony_ci    }
338094332d3Sopenharmony_ci
339094332d3Sopenharmony_ci    return HDF_SUCCESS;
340094332d3Sopenharmony_ci}
341094332d3Sopenharmony_ci
342094332d3Sopenharmony_ciint32_t BatteryInterfaceImpl::RemoveBatteryDeathRecipient(const sptr<IBatteryCallback>& callback)
343094332d3Sopenharmony_ci{
344094332d3Sopenharmony_ci    if (callback == nullptr) {
345094332d3Sopenharmony_ci        BATTERY_HILOGW(FEATURE_BATT_INFO, "remove callback is nullptr");
346094332d3Sopenharmony_ci        return HDF_ERR_INVALID_PARAM;
347094332d3Sopenharmony_ci    }
348094332d3Sopenharmony_ci    const sptr<IRemoteObject>& remote = OHOS::HDI::hdi_objcast<IBatteryCallback>(callback);
349094332d3Sopenharmony_ci    bool result = remote->RemoveDeathRecipient(g_deathRecipient);
350094332d3Sopenharmony_ci    if (!result) {
351094332d3Sopenharmony_ci        BATTERY_HILOGE(COMP_HDI, "RemoveDeathRecipient fail");
352094332d3Sopenharmony_ci        return HDF_FAILURE;
353094332d3Sopenharmony_ci    }
354094332d3Sopenharmony_ci
355094332d3Sopenharmony_ci    return HDF_SUCCESS;
356094332d3Sopenharmony_ci}
357094332d3Sopenharmony_ci
358094332d3Sopenharmony_civoid BatteryInterfaceImpl::BatteryDeathRecipient::OnRemoteDied(const wptr<IRemoteObject>& object)
359094332d3Sopenharmony_ci{
360094332d3Sopenharmony_ci    interfaceImpl_->UnRegister();
361094332d3Sopenharmony_ci}
362094332d3Sopenharmony_ci}  // namespace V2_0
363094332d3Sopenharmony_ci}  // namespace Battery
364094332d3Sopenharmony_ci}  // namespace Hdi
365094332d3Sopenharmony_ci}  // namespace OHOS
366