• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_1 {
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_1::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_1::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 
61     // The configuration can be released when it is not needed
62     BatteryConfig::DestroyInstance();
63 
64     loop_ = std::make_unique<OHOS::HDI::Battery::V1_1::BatteryThread>();
65     if (loop_ == nullptr) {
66         BATTERY_HILOGE(COMP_HDI, "make_unique BatteryThread error");
67         return HDF_ERR_MALLOC_FAIL;
68     }
69 
70     if (batteryCallback_ != nullptr) {
71         loop_->InitCallback(batteryCallback_);
72     } else {
73         BATTERY_HILOGW(COMP_HDI, "batteryCallback_ is nullptr");
74     }
75     loop_->StartThread(this);
76 
77     return HDF_SUCCESS;
78 }
79 
Register(const sptr<IBatteryCallback> & callback)80 int32_t BatteryInterfaceImpl::Register(const sptr<IBatteryCallback>& callback)
81 {
82     if (callback == nullptr) {
83         BATTERY_HILOGW(FEATURE_BATT_INFO, "callback is nullptr");
84         return HDF_ERR_INVALID_PARAM;
85     }
86     if (!g_isHdiStart) {
87         batteryCallback_ = callback;
88         loop_->InitCallback(batteryCallback_);
89 
90         g_deathRecipient = new BatteryInterfaceImpl::BatteryDeathRecipient(this);
91         if (g_deathRecipient == nullptr) {
92             BATTERY_HILOGE(COMP_HDI, "Failed to allocate BatteryDeathRecipient");
93             return HDF_ERR_MALLOC_FAIL;
94         }
95         AddBatteryDeathRecipient(batteryCallback_);
96         g_isHdiStart = true;
97     }
98     return HDF_SUCCESS;
99 }
100 
UnRegister()101 int32_t BatteryInterfaceImpl::UnRegister()
102 {
103     RemoveBatteryDeathRecipient(batteryCallback_);
104     batteryCallback_ = nullptr;
105     return HDF_SUCCESS;
106 }
107 
ChangePath(const std::string & path)108 int32_t BatteryInterfaceImpl::ChangePath(const std::string& path)
109 {
110     provider_->SetSysFilePath(path);
111     provider_->InitPowerSupplySysfs();
112     return HDF_SUCCESS;
113 }
114 
GetCapacity(int32_t & capacity)115 int32_t BatteryInterfaceImpl::GetCapacity(int32_t& capacity)
116 {
117     return provider_->ParseCapacity(&capacity);
118 }
119 
GetVoltage(int32_t & voltage)120 int32_t BatteryInterfaceImpl::GetVoltage(int32_t& voltage)
121 {
122     return provider_->ParseVoltage(&voltage);
123 }
124 
GetTemperature(int32_t & temperature)125 int32_t BatteryInterfaceImpl::GetTemperature(int32_t& temperature)
126 {
127     return provider_->ParseTemperature(&temperature);
128 }
129 
GetHealthState(BatteryHealthState & healthState)130 int32_t BatteryInterfaceImpl::GetHealthState(BatteryHealthState& healthState)
131 {
132     int32_t state = 0;
133     int32_t ret = provider_->ParseHealthState(&state);
134     if (ret != HDF_SUCCESS) {
135         return ret;
136     }
137 
138     healthState = BatteryHealthState(state);
139     return HDF_SUCCESS;
140 }
141 
GetPluggedType(BatteryPluggedType & pluggedType)142 int32_t BatteryInterfaceImpl::GetPluggedType(BatteryPluggedType& pluggedType)
143 {
144     int32_t type = 0;
145     int32_t ret = provider_->ParsePluggedType(&type);
146     if (ret != HDF_SUCCESS) {
147         return ret;
148     }
149 
150     pluggedType = BatteryPluggedType(type);
151     return HDF_SUCCESS;
152 }
153 
GetChargeState(BatteryChargeState & chargeState)154 int32_t BatteryInterfaceImpl::GetChargeState(BatteryChargeState& chargeState)
155 {
156     int32_t state = 0;
157     int32_t ret = provider_->ParseChargeState(&state);
158     if (ret != HDF_SUCCESS) {
159         return ret;
160     }
161 
162     chargeState = BatteryChargeState(state);
163     return HDF_SUCCESS;
164 }
165 
GetPresent(bool & present)166 int32_t BatteryInterfaceImpl::GetPresent(bool& present)
167 {
168     int8_t isPresent = 0;
169     int32_t ret = provider_->ParsePresent(&isPresent);
170     if (ret != HDF_SUCCESS) {
171         return ret;
172     }
173 
174     present = bool(isPresent);
175     return HDF_SUCCESS;
176 }
177 
GetTechnology(std::string & technology)178 int32_t BatteryInterfaceImpl::GetTechnology(std::string& technology)
179 {
180     return provider_->ParseTechnology(technology);
181 }
182 
GetTotalEnergy(int32_t & totalEnergy)183 int32_t BatteryInterfaceImpl::GetTotalEnergy(int32_t& totalEnergy)
184 {
185     return provider_->ParseTotalEnergy(&totalEnergy);
186 }
187 
GetCurrentAverage(int32_t & curAverage)188 int32_t BatteryInterfaceImpl::GetCurrentAverage(int32_t& curAverage)
189 {
190     return provider_->ParseCurrentAverage(&curAverage);
191 }
192 
GetCurrentNow(int32_t & curNow)193 int32_t BatteryInterfaceImpl::GetCurrentNow(int32_t& curNow)
194 {
195     return provider_->ParseCurrentNow(&curNow);
196 }
197 
GetRemainEnergy(int32_t & remainEnergy)198 int32_t BatteryInterfaceImpl::GetRemainEnergy(int32_t& remainEnergy)
199 {
200     return provider_->ParseRemainEnergy(&remainEnergy);
201 }
202 
GetBatteryInfo(BatteryInfo & info)203 int32_t BatteryInterfaceImpl::GetBatteryInfo(BatteryInfo& info)
204 {
205     if (provider_ == nullptr) {
206         return HDF_FAILURE;
207     }
208 
209     BatterydInfo batteryInfo = provider_->GetBatteryInfo();
210     info.capacity = batteryInfo.capacity_;
211     info.voltage = batteryInfo.voltage_;
212     info.temperature = batteryInfo.temperature_;
213     info.healthState = batteryInfo.healthState_;
214     info.pluggedType = batteryInfo.pluggedType_;
215     info.pluggedMaxCurrent = batteryInfo.pluggedMaxCurrent_;
216     info.pluggedMaxVoltage = batteryInfo.pluggedMaxVoltage_;
217     info.chargeState = batteryInfo.chargeState_;
218     info.chargeCounter = batteryInfo.chargeCounter_;
219     info.curNow = batteryInfo.curNow_;
220     info.curAverage = batteryInfo.curAverage_;
221     info.remainEnergy = batteryInfo.remainEnergy_;
222     info.totalEnergy = batteryInfo.totalEnergy_;
223     info.present = batteryInfo.present_;
224     info.technology = batteryInfo.technology_;
225 
226     return HDF_SUCCESS;
227 }
228 
SetChargingLimit(const std::vector<ChargingLimit> & chargingLimit)229 int32_t BatteryInterfaceImpl::SetChargingLimit(const std::vector<ChargingLimit>& chargingLimit)
230 {
231     return provider_->SetChargingLimit(chargingLimit, currentPath_, voltagePath_);
232 }
233 
AddBatteryDeathRecipient(const sptr<IBatteryCallback> & callback)234 int32_t BatteryInterfaceImpl::AddBatteryDeathRecipient(const sptr<IBatteryCallback>& callback)
235 {
236     const sptr<IRemoteObject>& remote = OHOS::HDI::hdi_objcast<IBatteryCallback>(callback);
237     bool result = remote->AddDeathRecipient(g_deathRecipient);
238     if (!result) {
239         BATTERY_HILOGE(COMP_HDI, "AddDeathRecipient fail");
240         return HDF_FAILURE;
241     }
242 
243     return HDF_SUCCESS;
244 }
245 
RemoveBatteryDeathRecipient(const sptr<IBatteryCallback> & callback)246 int32_t BatteryInterfaceImpl::RemoveBatteryDeathRecipient(const sptr<IBatteryCallback>& callback)
247 {
248     if (callback == nullptr) {
249         BATTERY_HILOGW(FEATURE_BATT_INFO, "remove callback is nullptr");
250         return HDF_ERR_INVALID_PARAM;
251     }
252     const sptr<IRemoteObject>& remote = OHOS::HDI::hdi_objcast<IBatteryCallback>(callback);
253     bool result = remote->RemoveDeathRecipient(g_deathRecipient);
254     if (!result) {
255         BATTERY_HILOGE(COMP_HDI, "RemoveDeathRecipient fail");
256         return HDF_FAILURE;
257     }
258 
259     return HDF_SUCCESS;
260 }
261 
OnRemoteDied(const wptr<IRemoteObject> & object)262 void BatteryInterfaceImpl::BatteryDeathRecipient::OnRemoteDied(const wptr<IRemoteObject>& object)
263 {
264     interfaceImpl_->UnRegister();
265 }
266 }  // namespace V1_1
267 }  // namespace Battery
268 }  // namespace Hdi
269 }  // namespace OHOS
270