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