blob: ec05398f442bd2902c2c7da10ac2ea47ac0d7e49 [file] [log] [blame]
Yifan Hong2763df82017-09-19 17:57:50 -07001#define LOG_TAG "Health"
2#include <android-base/logging.h>
3
4#include <health2/Health.h>
5
6#include <hidl/HidlTransportSupport.h>
7
8extern void healthd_battery_update_internal(bool);
9
10namespace android {
11namespace hardware {
12namespace health {
13namespace V2_0 {
14namespace implementation {
15
16Health::Health(struct healthd_config* c) {
17 battery_monitor_ = std::make_unique<BatteryMonitor>();
18 battery_monitor_->init(c);
19}
20
21// Methods from IHealth follow.
22Return<Result> Health::registerCallback(const sp<IHealthInfoCallback>& callback) {
23 if (callback == nullptr) {
24 return Result::SUCCESS;
25 }
26
27 {
28 std::lock_guard<std::mutex> _lock(callbacks_lock_);
29 callbacks_.push_back(callback);
30 // unlock
31 }
32
33 auto linkRet = callback->linkToDeath(this, 0u /* cookie */);
34 if (!linkRet.withDefault(false)) {
35 LOG(WARNING) << __func__ << "Cannot link to death: "
36 << (linkRet.isOk() ? "linkToDeath returns false" : linkRet.description());
37 // ignore the error
38 }
39
40 return update();
41}
42
43bool Health::unregisterCallbackInternal(const sp<IBase>& callback) {
44 if (callback == nullptr) return false;
45
46 bool removed = false;
47 std::lock_guard<std::mutex> _lock(callbacks_lock_);
48 for (auto it = callbacks_.begin(); it != callbacks_.end();) {
49 if (interfacesEqual(*it, callback)) {
50 it = callbacks_.erase(it);
51 removed = true;
52 } else {
53 ++it;
54 }
55 }
56 (void)callback->unlinkToDeath(this).isOk(); // ignore errors
57 return removed;
58}
59
60Return<Result> Health::unregisterCallback(const sp<IHealthInfoCallback>& callback) {
61 return unregisterCallbackInternal(callback) ? Result::SUCCESS : Result::NOT_FOUND;
62}
63
64template<typename T>
65void getProperty(const std::unique_ptr<BatteryMonitor>& monitor, int id, T defaultValue,
66 const std::function<void(Result, T)>& callback) {
67 struct BatteryProperty prop;
68 T ret = defaultValue;
69 Result result = Result::SUCCESS;
70 status_t err = monitor->getProperty(static_cast<int>(id), &prop);
71 if (err != OK) {
72 LOG(DEBUG) << "getProperty(" << id << ")" << " fails: (" << err << ") " << strerror(-err);
73 } else {
74 ret = static_cast<T>(prop.valueInt64);
75 }
76 switch (err) {
77 case OK: result = Result::SUCCESS; break;
78 case NAME_NOT_FOUND: result = Result::NOT_SUPPORTED; break;
79 default: result = Result::UNKNOWN; break;
80 }
81 callback(result, static_cast<T>(ret));
82}
83
84Return<void> Health::getChargeCounter(getChargeCounter_cb _hidl_cb) {
85 getProperty(battery_monitor_, BATTERY_PROP_CHARGE_COUNTER, INT32_MIN, _hidl_cb);
86 return Void();
87}
88
89Return<void> Health::getCurrentNow(getCurrentNow_cb _hidl_cb) {
90 getProperty(battery_monitor_, BATTERY_PROP_CURRENT_NOW, INT32_MIN, _hidl_cb);
91 return Void();
92}
93
94Return<void> Health::getCurrentAverage(getCurrentAverage_cb _hidl_cb) {
95 getProperty(battery_monitor_, BATTERY_PROP_CURRENT_AVG, INT32_MIN, _hidl_cb);
96 return Void();
97}
98
99Return<void> Health::getCapacity(getCapacity_cb _hidl_cb) {
100 getProperty(battery_monitor_, BATTERY_PROP_CAPACITY, INT32_MIN, _hidl_cb);
101 return Void();
102}
103
104Return<void> Health::getEnergyCounter(getEnergyCounter_cb _hidl_cb) {
105 getProperty(battery_monitor_, BATTERY_PROP_ENERGY_COUNTER, INT64_MIN, _hidl_cb);
106 return Void();
107}
108
109Return<void> Health::getChargeStatus(getChargeStatus_cb _hidl_cb) {
110 getProperty(battery_monitor_, BATTERY_PROP_BATTERY_STATUS, BatteryStatus::UNKNOWN, _hidl_cb);
111 return Void();
112}
113
114
115Return<Result> Health::update() {
116 if (!healthd_mode_ops || !healthd_mode_ops->battery_update) {
117 LOG(WARNING) << "health@2.0: update: not initialized. "
118 << "update() should not be called in charger / recovery.";
119 return Result::UNKNOWN;
120 }
121
122 // Retrieve all information and call healthd_mode_ops->battery_update, which calls
Yifan Hongbe17a4f2017-10-11 10:40:56 -0700123 // updateAndNotify.
Yifan Hong2763df82017-09-19 17:57:50 -0700124 bool chargerOnline = battery_monitor_->update();
125
126 // adjust uevent / wakealarm periods
127 healthd_battery_update_internal(chargerOnline);
128
129 return Result::SUCCESS;
130}
131
Yifan Hongbe17a4f2017-10-11 10:40:56 -0700132void Health::updateAndNotify(HealthInfo* info) {
133 // update 2.0 specific fields
134 struct BatteryProperty prop;
135 if (battery_monitor_->getProperty(BATTERY_PROP_CURRENT_AVG, &prop) == OK)
136 info->batteryCurrentAverage = static_cast<int32_t>(prop.valueInt64);
137 if (battery_monitor_->getProperty(BATTERY_PROP_CAPACITY, &prop) == OK)
138 info->batteryCapacity = static_cast<int32_t>(prop.valueInt64);
139 if (battery_monitor_->getProperty(BATTERY_PROP_ENERGY_COUNTER, &prop) == OK)
140 info->energyCounter = prop.valueInt64;
141
Yifan Hong2763df82017-09-19 17:57:50 -0700142 std::lock_guard<std::mutex> _lock(callbacks_lock_);
143 for (auto it = callbacks_.begin(); it != callbacks_.end();) {
Yifan Hongbe17a4f2017-10-11 10:40:56 -0700144 auto ret = (*it)->healthInfoChanged(*info);
Yifan Hong2763df82017-09-19 17:57:50 -0700145 if (!ret.isOk() && ret.isDeadObject()) {
146 it = callbacks_.erase(it);
147 } else {
148 ++it;
149 }
150 }
151}
152
153Return<void> Health::debug(const hidl_handle& handle, const hidl_vec<hidl_string>&) {
154 if (handle != nullptr && handle->numFds >= 1) {
155 int fd = handle->data[0];
156 battery_monitor_->dumpState(fd);
157 fsync(fd);
158 }
159 return Void();
160}
161
162void Health::serviceDied(uint64_t /* cookie */, const wp<IBase>& who) {
163 (void)unregisterCallbackInternal(who.promote());
164}
165
166// Methods from ::android::hidl::base::V1_0::IBase follow.
167
168} // namespace implementation
169} // namespace V2_0
170} // namespace health
171} // namespace hardware
172} // namespace android