blob: 74f3eec213b9f495a58e9edca862438247034b0f [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 Hong1106e832017-11-06 20:50:45 +0000123 // notifyListeners.
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 Hong1106e832017-11-06 20:50:45 +0000132void Health::notifyListeners(const HealthInfo& info) {
Yifan Hong2763df82017-09-19 17:57:50 -0700133 std::lock_guard<std::mutex> _lock(callbacks_lock_);
134 for (auto it = callbacks_.begin(); it != callbacks_.end();) {
Yifan Hong1106e832017-11-06 20:50:45 +0000135 auto ret = (*it)->healthInfoChanged(info);
Yifan Hong2763df82017-09-19 17:57:50 -0700136 if (!ret.isOk() && ret.isDeadObject()) {
137 it = callbacks_.erase(it);
138 } else {
139 ++it;
140 }
141 }
142}
143
144Return<void> Health::debug(const hidl_handle& handle, const hidl_vec<hidl_string>&) {
145 if (handle != nullptr && handle->numFds >= 1) {
146 int fd = handle->data[0];
147 battery_monitor_->dumpState(fd);
148 fsync(fd);
149 }
150 return Void();
151}
152
153void Health::serviceDied(uint64_t /* cookie */, const wp<IBase>& who) {
154 (void)unregisterCallbackInternal(who.promote());
155}
156
157// Methods from ::android::hidl::base::V1_0::IBase follow.
158
159} // namespace implementation
160} // namespace V2_0
161} // namespace health
162} // namespace hardware
163} // namespace android