blob: 323f5253755a10410dab600e1e2c6509b5c3ff33 [file] [log] [blame]
Tao Bao832c9cd2019-09-27 23:32:00 -07001/*
2 * Copyright (C) 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "recovery_utils/battery_utils.h"
18
19#include <stdint.h>
20#include <unistd.h>
21
22#include <android-base/logging.h>
23#include <healthhalutils/HealthHalUtils.h>
24
25BatteryInfo GetBatteryInfo() {
26 using android::hardware::health::V1_0::BatteryStatus;
27 using android::hardware::health::V2_0::get_health_service;
28 using android::hardware::health::V2_0::IHealth;
29 using android::hardware::health::V2_0::Result;
30 using android::hardware::health::V2_0::toString;
31
32 android::sp<IHealth> health = get_health_service();
33
34 int wait_second = 0;
35 while (true) {
36 auto charge_status = BatteryStatus::UNKNOWN;
37
38 if (health == nullptr) {
39 LOG(WARNING) << "No health implementation is found; assuming defaults";
40 } else {
41 health
42 ->getChargeStatus([&charge_status](auto res, auto out_status) {
43 if (res == Result::SUCCESS) {
44 charge_status = out_status;
45 }
46 })
47 .isOk(); // should not have transport error
48 }
49
50 // Treat unknown status as on charger. See hardware/interfaces/health/1.0/types.hal for the
51 // meaning of the return values.
52 bool charging = (charge_status != BatteryStatus::DISCHARGING &&
53 charge_status != BatteryStatus::NOT_CHARGING);
54
55 Result res = Result::UNKNOWN;
56 int32_t capacity = INT32_MIN;
57 if (health != nullptr) {
58 health
59 ->getCapacity([&res, &capacity](auto out_res, auto out_capacity) {
60 res = out_res;
61 capacity = out_capacity;
62 })
63 .isOk(); // should not have transport error
64 }
65
66 LOG(INFO) << "charge_status " << toString(charge_status) << ", charging " << charging
67 << ", status " << toString(res) << ", capacity " << capacity;
68
69 constexpr int BATTERY_READ_TIMEOUT_IN_SEC = 10;
70 // At startup, the battery drivers in devices like N5X/N6P take some time to load
71 // the battery profile. Before the load finishes, it reports value 50 as a fake
72 // capacity. BATTERY_READ_TIMEOUT_IN_SEC is set that the battery drivers are expected
73 // to finish loading the battery profile earlier than 10 seconds after kernel startup.
74 if (res == Result::SUCCESS && capacity == 50) {
75 if (wait_second < BATTERY_READ_TIMEOUT_IN_SEC) {
76 sleep(1);
77 wait_second++;
78 continue;
79 }
80 }
81 // If we can't read battery percentage, it may be a device without battery. In this
82 // situation, use 100 as a fake battery percentage.
83 if (res != Result::SUCCESS) {
84 capacity = 100;
85 }
86
87 return BatteryInfo{ charging, capacity };
88 }
89}