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