1 /*
2 * Copyright (c) 2021-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_srv_proxy.h"
17
18 #include "errors.h"
19 #include "message_option.h"
20 #include "message_parcel.h"
21 #include "battery_log.h"
22 #include "battery_manager_ipc_interface_code.h"
23 #include "power_common.h"
24 #include "power_mgr_errors.h"
25 #include "string_ex.h"
26
27 namespace OHOS {
28 namespace PowerMgr {
GetCapacity()29 int32_t BatterySrvProxy::GetCapacity()
30 {
31 sptr<IRemoteObject> remote = Remote();
32 RETURN_IF_WITH_RET(remote == nullptr, INVALID_BATT_INT_VALUE);
33 MessageParcel data;
34 MessageParcel reply;
35 MessageOption option;
36
37 if (!data.WriteInterfaceToken(BatterySrvProxy::GetDescriptor())) {
38 BATTERY_HILOGW(FEATURE_BATT_INFO, "Write descriptor failed");
39 return INVALID_BATT_INT_VALUE;
40 }
41
42 int ret = remote->SendRequest(
43 static_cast<int>(PowerMgr::BatterySrvInterfaceCode::BATT_GET_CAPACITY),
44 data, reply, option);
45 if (ret != ERR_OK) {
46 BATTERY_HILOGW(FEATURE_BATT_INFO, "SendRequest failed, error code: %{public}d", ret);
47 return INVALID_BATT_INT_VALUE;
48 }
49 int32_t capacity = INVALID_BATT_INT_VALUE;
50 RETURN_IF_READ_PARCEL_FAILED_WITH_RET(reply, Int32, capacity, INVALID_BATT_INT_VALUE);
51 return capacity;
52 }
53
GetChargingStatus()54 BatteryChargeState BatterySrvProxy::GetChargingStatus()
55 {
56 sptr<IRemoteObject> remote = Remote();
57 RETURN_IF_WITH_RET(remote == nullptr, BatteryChargeState::CHARGE_STATE_BUTT);
58 MessageParcel data;
59 MessageParcel reply;
60 MessageOption option;
61
62 if (!data.WriteInterfaceToken(BatterySrvProxy::GetDescriptor())) {
63 BATTERY_HILOGW(FEATURE_BATT_INFO, "Write descriptor failed");
64 return BatteryChargeState::CHARGE_STATE_BUTT;
65 }
66
67 int ret = remote->SendRequest(
68 static_cast<int>(PowerMgr::BatterySrvInterfaceCode::BATT_GET_CHARGING_STATUS),
69 data, reply, option);
70 if (ret != ERR_OK) {
71 BATTERY_HILOGW(FEATURE_BATT_INFO, "SendRequest failed, error code: %{public}d", ret);
72 return BatteryChargeState::CHARGE_STATE_BUTT;
73 }
74 uint32_t chargingState = static_cast<uint32_t>(BatteryChargeState::CHARGE_STATE_BUTT);
75 RETURN_IF_READ_PARCEL_FAILED_WITH_RET(reply, Uint32, chargingState, BatteryChargeState::CHARGE_STATE_BUTT);
76 return static_cast<BatteryChargeState>(chargingState);
77 }
78
GetHealthStatus()79 BatteryHealthState BatterySrvProxy::GetHealthStatus()
80 {
81 sptr<IRemoteObject> remote = Remote();
82 RETURN_IF_WITH_RET(remote == nullptr, BatteryHealthState::HEALTH_STATE_BUTT);
83 MessageParcel data;
84 MessageParcel reply;
85 MessageOption option;
86
87 if (!data.WriteInterfaceToken(BatterySrvProxy::GetDescriptor())) {
88 BATTERY_HILOGW(FEATURE_BATT_INFO, "Write descriptor failed");
89 return BatteryHealthState::HEALTH_STATE_BUTT;
90 }
91
92 int ret = remote->SendRequest(
93 static_cast<int>(PowerMgr::BatterySrvInterfaceCode::BATT_GET_HEALTH_STATUS),
94 data, reply, option);
95 if (ret != ERR_OK) {
96 BATTERY_HILOGW(FEATURE_BATT_INFO, "SendRequest failed, error code: %{public}d", ret);
97 return BatteryHealthState::HEALTH_STATE_BUTT;
98 }
99 uint32_t healthStatus = static_cast<uint32_t>(BatteryHealthState::HEALTH_STATE_BUTT);
100 RETURN_IF_READ_PARCEL_FAILED_WITH_RET(reply, Uint32, healthStatus, BatteryHealthState::HEALTH_STATE_BUTT);
101 return static_cast<BatteryHealthState>(healthStatus);
102 }
103
GetPluggedType()104 BatteryPluggedType BatterySrvProxy::GetPluggedType()
105 {
106 sptr<IRemoteObject> remote = Remote();
107 RETURN_IF_WITH_RET(remote == nullptr, BatteryPluggedType::PLUGGED_TYPE_BUTT);
108 MessageParcel data;
109 MessageParcel reply;
110 MessageOption option;
111
112 if (!data.WriteInterfaceToken(BatterySrvProxy::GetDescriptor())) {
113 BATTERY_HILOGW(FEATURE_BATT_INFO, "Write descriptor failed");
114 return BatteryPluggedType::PLUGGED_TYPE_BUTT;
115 }
116
117 int ret = remote->SendRequest(
118 static_cast<int>(PowerMgr::BatterySrvInterfaceCode::BATT_GET_PLUG_TYPE),
119 data, reply, option);
120 if (ret != ERR_OK) {
121 BATTERY_HILOGW(FEATURE_BATT_INFO, "SendRequest failed, error code: %{public}d", ret);
122 return BatteryPluggedType::PLUGGED_TYPE_BUTT;
123 }
124 uint32_t pluggedType = static_cast<uint32_t>(BatteryPluggedType::PLUGGED_TYPE_BUTT);
125 RETURN_IF_READ_PARCEL_FAILED_WITH_RET(reply, Uint32, pluggedType, BatteryPluggedType::PLUGGED_TYPE_BUTT);
126 return static_cast<BatteryPluggedType>(pluggedType);
127 }
128
GetVoltage()129 int32_t BatterySrvProxy::GetVoltage()
130 {
131 sptr<IRemoteObject> remote = Remote();
132 RETURN_IF_WITH_RET(remote == nullptr, INVALID_BATT_INT_VALUE);
133 MessageParcel data;
134 MessageParcel reply;
135 MessageOption option;
136
137 if (!data.WriteInterfaceToken(BatterySrvProxy::GetDescriptor())) {
138 BATTERY_HILOGW(FEATURE_BATT_INFO, "Write descriptor failed");
139 return INVALID_BATT_INT_VALUE;
140 }
141
142 int ret = remote->SendRequest(
143 static_cast<int>(PowerMgr::BatterySrvInterfaceCode::BATT_GET_VOLTAGE),
144 data, reply, option);
145 if (ret != ERR_OK) {
146 BATTERY_HILOGW(FEATURE_BATT_INFO, "SendRequest failed, error code: %{public}d", ret);
147 return INVALID_BATT_INT_VALUE;
148 }
149 int32_t voltage = INVALID_BATT_INT_VALUE;
150 RETURN_IF_READ_PARCEL_FAILED_WITH_RET(reply, Int32, voltage, INVALID_BATT_INT_VALUE);
151 return voltage;
152 }
153
GetPresent()154 bool BatterySrvProxy::GetPresent()
155 {
156 sptr<IRemoteObject> remote = Remote();
157 RETURN_IF_WITH_RET(remote == nullptr, INVALID_BATT_BOOL_VALUE);
158 MessageParcel data;
159 MessageParcel reply;
160 MessageOption option;
161
162 if (!data.WriteInterfaceToken(BatterySrvProxy::GetDescriptor())) {
163 BATTERY_HILOGW(FEATURE_BATT_INFO, "Write descriptor failed");
164 return INVALID_BATT_BOOL_VALUE;
165 }
166
167 int ret = remote->SendRequest(
168 static_cast<int>(PowerMgr::BatterySrvInterfaceCode::BATT_GET_PRESENT),
169 data, reply, option);
170 if (ret != ERR_OK) {
171 BATTERY_HILOGW(FEATURE_BATT_INFO, "SendRequest failed, error code: %{public}d", ret);
172 return INVALID_BATT_BOOL_VALUE;
173 }
174 bool present = INVALID_BATT_BOOL_VALUE;
175 RETURN_IF_READ_PARCEL_FAILED_WITH_RET(reply, Bool, present, INVALID_BATT_BOOL_VALUE);
176 return present;
177 }
178
GetTechnology()179 std::string BatterySrvProxy::GetTechnology()
180 {
181 sptr<IRemoteObject> remote = Remote();
182 RETURN_IF_WITH_RET(remote == nullptr, INVALID_STRING_VALUE);
183 MessageParcel data;
184 MessageParcel reply;
185 MessageOption option;
186
187 if (!data.WriteInterfaceToken(BatterySrvProxy::GetDescriptor())) {
188 BATTERY_HILOGW(FEATURE_BATT_INFO, "Write descriptor failed");
189 return INVALID_STRING_VALUE;
190 }
191
192 int ret = remote->SendRequest(
193 static_cast<int>(PowerMgr::BatterySrvInterfaceCode::BATT_GET_TECHNOLOGY),
194 data, reply, option);
195 if (ret != ERR_OK) {
196 BATTERY_HILOGW(FEATURE_BATT_INFO, "SendRequest failed, error code: %{public}d", ret);
197 return INVALID_STRING_VALUE;
198 }
199 std::u16string technology = Str8ToStr16(INVALID_STRING_VALUE);
200 RETURN_IF_READ_PARCEL_FAILED_WITH_RET(reply, String16, technology, INVALID_STRING_VALUE);
201 return Str16ToStr8(technology);
202 }
203
GetBatteryTemperature()204 int32_t BatterySrvProxy::GetBatteryTemperature()
205 {
206 sptr<IRemoteObject> remote = Remote();
207 RETURN_IF_WITH_RET(remote == nullptr, INVALID_BATT_TEMP_VALUE);
208 MessageParcel data;
209 MessageParcel reply;
210 MessageOption option;
211
212 if (!data.WriteInterfaceToken(BatterySrvProxy::GetDescriptor())) {
213 BATTERY_HILOGW(FEATURE_BATT_INFO, "Write descriptor failed");
214 return INVALID_BATT_TEMP_VALUE;
215 }
216
217 int ret = remote->SendRequest(
218 static_cast<int>(PowerMgr::BatterySrvInterfaceCode::BATT_GET_TEMPERATURE),
219 data, reply, option);
220 if (ret != ERR_OK) {
221 BATTERY_HILOGW(FEATURE_BATT_INFO, "SendRequest failed, error code: %{public}d", ret);
222 return INVALID_BATT_TEMP_VALUE;
223 }
224 int32_t temperature = INVALID_BATT_TEMP_VALUE;
225 RETURN_IF_READ_PARCEL_FAILED_WITH_RET(reply, Int32, temperature, INVALID_BATT_TEMP_VALUE);
226 return temperature;
227 }
228
GetCapacityLevel()229 BatteryCapacityLevel BatterySrvProxy::GetCapacityLevel()
230 {
231 sptr<IRemoteObject> remote = Remote();
232 RETURN_IF_WITH_RET(remote == nullptr, BatteryCapacityLevel::LEVEL_NONE);
233 MessageParcel data;
234 MessageParcel reply;
235 MessageOption option;
236
237 if (!data.WriteInterfaceToken(BatterySrvProxy::GetDescriptor())) {
238 BATTERY_HILOGW(FEATURE_BATT_INFO, "Write descriptor failed");
239 return BatteryCapacityLevel::LEVEL_NONE;
240 }
241
242 int ret = remote->SendRequest(
243 static_cast<int>(PowerMgr::BatterySrvInterfaceCode::BATT_GET_BATTERY_LEVEL),
244 data, reply, option);
245 if (ret != ERR_OK) {
246 BATTERY_HILOGW(FEATURE_BATT_INFO, "SendRequest failed, error code: %{public}d", ret);
247 return BatteryCapacityLevel::LEVEL_NONE;
248 }
249 uint32_t level = static_cast<uint32_t>(BatteryCapacityLevel::LEVEL_NONE);
250 RETURN_IF_READ_PARCEL_FAILED_WITH_RET(reply, Uint32, level, BatteryCapacityLevel::LEVEL_NONE);
251 return static_cast<BatteryCapacityLevel>(level);
252 }
253
GetRemainingChargeTime()254 int64_t BatterySrvProxy::GetRemainingChargeTime()
255 {
256 sptr<IRemoteObject> remote = Remote();
257 RETURN_IF_WITH_RET(remote == nullptr, INVALID_REMAINING_CHARGE_TIME_VALUE);
258 MessageParcel data;
259 MessageParcel reply;
260 MessageOption option;
261
262 if (!data.WriteInterfaceToken(BatterySrvProxy::GetDescriptor())) {
263 BATTERY_HILOGW(FEATURE_BATT_INFO, "BatterySrvProxy::%{public}s write descriptor failed!", __func__);
264 return INVALID_REMAINING_CHARGE_TIME_VALUE;
265 }
266
267 int ret = remote->SendRequest(
268 static_cast<int>(PowerMgr::BatterySrvInterfaceCode::BATT_GET_REMAINING_CHARGE_TIME),
269 data, reply, option);
270 if (ret != ERR_OK) {
271 BATTERY_HILOGW(FEATURE_BATT_INFO, "BatterySrvProxy::%{public}s SendRequest is failed, error code: %d",
272 __func__, ret);
273 return INVALID_REMAINING_CHARGE_TIME_VALUE;
274 }
275 int64_t time = INVALID_REMAINING_CHARGE_TIME_VALUE;
276 RETURN_IF_READ_PARCEL_FAILED_WITH_RET(reply, Int64, time, INVALID_REMAINING_CHARGE_TIME_VALUE);
277 return time;
278 }
279
GetTotalEnergy()280 int32_t BatterySrvProxy::GetTotalEnergy()
281 {
282 sptr<IRemoteObject> remote = Remote();
283 RETURN_IF_WITH_RET(remote == nullptr, INVALID_BATT_INT_VALUE);
284 MessageParcel data;
285 MessageParcel reply;
286 MessageOption option;
287
288 if (!data.WriteInterfaceToken(BatterySrvProxy::GetDescriptor())) {
289 BATTERY_HILOGW(FEATURE_BATT_INFO, "Write descriptor failed");
290 return INVALID_BATT_INT_VALUE;
291 }
292
293 int ret = remote->SendRequest(
294 static_cast<int>(PowerMgr::BatterySrvInterfaceCode::BATT_GET_BATTERY_TOTAL_ENERGY),
295 data, reply, option);
296 if (ret != ERR_OK) {
297 BATTERY_HILOGW(FEATURE_BATT_INFO, "SendRequest failed, error code: %{public}d", ret);
298 return INVALID_BATT_INT_VALUE;
299 }
300 int32_t totalEnergy = INVALID_BATT_INT_VALUE;
301 RETURN_IF_READ_PARCEL_FAILED_WITH_RET(reply, Int32, totalEnergy, INVALID_BATT_INT_VALUE);
302 return totalEnergy;
303 }
304
GetCurrentAverage()305 int32_t BatterySrvProxy::GetCurrentAverage()
306 {
307 sptr<IRemoteObject> remote = Remote();
308 RETURN_IF_WITH_RET(remote == nullptr, INVALID_BATT_INT_VALUE);
309 MessageParcel data;
310 MessageParcel reply;
311 MessageOption option;
312
313 if (!data.WriteInterfaceToken(BatterySrvProxy::GetDescriptor())) {
314 BATTERY_HILOGW(FEATURE_BATT_INFO, "Write descriptor failed");
315 return INVALID_BATT_INT_VALUE;
316 }
317
318 int ret = remote->SendRequest(
319 static_cast<int>(PowerMgr::BatterySrvInterfaceCode::BATT_GET_BATTERY_CURRENT_AVERAGE),
320 data, reply, option);
321 if (ret != ERR_OK) {
322 BATTERY_HILOGW(FEATURE_BATT_INFO, "SendRequest failed, error code: %{public}d", ret);
323 return INVALID_BATT_INT_VALUE;
324 }
325 int32_t curAverage = INVALID_BATT_INT_VALUE;
326 RETURN_IF_READ_PARCEL_FAILED_WITH_RET(reply, Int32, curAverage, INVALID_BATT_INT_VALUE);
327 return curAverage;
328 }
329
GetNowCurrent()330 int32_t BatterySrvProxy::GetNowCurrent()
331 {
332 sptr<IRemoteObject> remote = Remote();
333 RETURN_IF_WITH_RET(remote == nullptr, INVALID_BATT_INT_VALUE);
334 MessageParcel data;
335 MessageParcel reply;
336 MessageOption option;
337
338 if (!data.WriteInterfaceToken(BatterySrvProxy::GetDescriptor())) {
339 BATTERY_HILOGW(FEATURE_BATT_INFO, "Write descriptor failed");
340 return INVALID_BATT_INT_VALUE;
341 }
342
343 int ret = remote->SendRequest(
344 static_cast<int>(PowerMgr::BatterySrvInterfaceCode::BATT_GET_BATTERY_CURRENT_NOW),
345 data, reply, option);
346 if (ret != ERR_OK) {
347 BATTERY_HILOGW(FEATURE_BATT_INFO, "SendRequest failed, error code: %{public}d", ret);
348 return INVALID_BATT_INT_VALUE;
349 }
350 int32_t nowCurr = INVALID_BATT_INT_VALUE;
351 RETURN_IF_READ_PARCEL_FAILED_WITH_RET(reply, Int32, nowCurr, INVALID_BATT_INT_VALUE);
352 return nowCurr;
353 }
354
GetRemainEnergy()355 int32_t BatterySrvProxy::GetRemainEnergy()
356 {
357 sptr<IRemoteObject> remote = Remote();
358 RETURN_IF_WITH_RET(remote == nullptr, INVALID_BATT_INT_VALUE);
359 MessageParcel data;
360 MessageParcel reply;
361 MessageOption option;
362
363 if (!data.WriteInterfaceToken(BatterySrvProxy::GetDescriptor())) {
364 BATTERY_HILOGW(FEATURE_BATT_INFO, "Write descriptor failed");
365 return INVALID_BATT_INT_VALUE;
366 }
367
368 int ret = remote->SendRequest(
369 static_cast<int>(PowerMgr::BatterySrvInterfaceCode::BATT_GET_BATTERY_REMAIN_ENERGY),
370 data, reply, option);
371 if (ret != ERR_OK) {
372 BATTERY_HILOGW(FEATURE_BATT_INFO, "SendRequest failed, error code: %{public}d", ret);
373 return INVALID_BATT_INT_VALUE;
374 }
375 int32_t remainEnergy = INVALID_BATT_INT_VALUE;
376 RETURN_IF_READ_PARCEL_FAILED_WITH_RET(reply, Int32, remainEnergy, INVALID_BATT_INT_VALUE);
377 return remainEnergy;
378 }
379
SetBatteryConfig(const std::string & sceneName,const std::string & value)380 int32_t BatterySrvProxy::SetBatteryConfig(const std::string& sceneName, const std::string& value)
381 {
382 sptr<IRemoteObject> remote = Remote();
383 RETURN_IF_WITH_RET(remote == nullptr, INVALID_BATT_INT_VALUE);
384 MessageParcel data;
385 MessageParcel reply;
386 MessageOption option;
387
388 if (!data.WriteInterfaceToken(BatterySrvProxy::GetDescriptor())) {
389 BATTERY_HILOGW(FEATURE_BATT_INFO, "Write descriptor failed");
390 return INVALID_BATT_INT_VALUE;
391 }
392
393 RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(data, String16, Str8ToStr16(sceneName), E_WRITE_PARCEL_ERROR);
394 RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(data, String16, Str8ToStr16(value), E_WRITE_PARCEL_ERROR);
395
396 int ret = remote->SendRequest(
397 static_cast<int>(PowerMgr::BatterySrvInterfaceCode::SET_BATTERY_CONFIG),
398 data, reply, option);
399 if (ret != ERR_OK) {
400 BATTERY_HILOGW(FEATURE_BATT_INFO, "SendRequest failed, error code: %{public}d", ret);
401 return INVALID_BATT_INT_VALUE;
402 }
403
404 RETURN_IF_READ_PARCEL_FAILED_WITH_RET(reply, Int32, ret, INVALID_BATT_INT_VALUE);
405 return ret;
406 }
407
GetBatteryConfig(const std::string & sceneName)408 std::string BatterySrvProxy::GetBatteryConfig(const std::string& sceneName)
409 {
410 sptr<IRemoteObject> remote = Remote();
411 RETURN_IF_WITH_RET(remote == nullptr, "");
412 MessageParcel data;
413 MessageParcel reply;
414 MessageOption option;
415
416 if (!data.WriteInterfaceToken(BatterySrvProxy::GetDescriptor())) {
417 BATTERY_HILOGW(FEATURE_BATT_INFO, "Write descriptor failed");
418 return "";
419 }
420
421 RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(data, String16, Str8ToStr16(sceneName), "");
422
423 int ret = remote->SendRequest(
424 static_cast<int>(PowerMgr::BatterySrvInterfaceCode::GET_BATTERY_CONFIG),
425 data, reply, option);
426 if (ret != ERR_OK) {
427 BATTERY_HILOGW(FEATURE_BATT_INFO, "SendRequest failed, error code: %{public}d", ret);
428 return "";
429 }
430
431 std::u16string result;
432 RETURN_IF_READ_PARCEL_FAILED_WITH_RET(reply, String16, result, "");
433 return Str16ToStr8(result);
434 }
435
IsBatteryConfigSupported(const std::string & sceneName)436 bool BatterySrvProxy::IsBatteryConfigSupported(const std::string& sceneName)
437 {
438 sptr<IRemoteObject> remote = Remote();
439 RETURN_IF_WITH_RET(remote == nullptr, false);
440 MessageParcel data;
441 MessageParcel reply;
442 MessageOption option;
443
444 if (!data.WriteInterfaceToken(BatterySrvProxy::GetDescriptor())) {
445 BATTERY_HILOGW(FEATURE_BATT_INFO, "Write descriptor failed");
446 return false;
447 }
448
449 RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(data, String16, Str8ToStr16(sceneName), false);
450
451 int ret = remote->SendRequest(
452 static_cast<int>(PowerMgr::BatterySrvInterfaceCode::SUPPORT_BATTERY_CONFIG),
453 data, reply, option);
454 if (ret != ERR_OK) {
455 BATTERY_HILOGW(FEATURE_BATT_INFO, "SendRequest failed, error code: %{public}d", ret);
456 return false;
457 }
458
459 bool result = false;
460 RETURN_IF_READ_PARCEL_FAILED_WITH_RET(reply, Bool, result, false);
461 return result;
462 }
463 } // namespace PowerMgr
464 } // namespace OHOS
465