Yifan Hong | 2763df8 | 2017-09-19 17:57:50 -0700 | [diff] [blame] | 1 | #define LOG_TAG "Health" |
| 2 | #include <android-base/logging.h> |
| 3 | |
| 4 | #include <health2/Health.h> |
| 5 | |
| 6 | #include <hidl/HidlTransportSupport.h> |
| 7 | |
| 8 | extern void healthd_battery_update_internal(bool); |
| 9 | |
| 10 | namespace android { |
| 11 | namespace hardware { |
| 12 | namespace health { |
| 13 | namespace V2_0 { |
| 14 | namespace implementation { |
| 15 | |
Yifan Hong | 9cffa8a | 2017-11-07 14:59:14 -0800 | [diff] [blame] | 16 | sp<Health> Health::instance_; |
| 17 | |
Yifan Hong | 2763df8 | 2017-09-19 17:57:50 -0700 | [diff] [blame] | 18 | Health::Health(struct healthd_config* c) { |
| 19 | battery_monitor_ = std::make_unique<BatteryMonitor>(); |
| 20 | battery_monitor_->init(c); |
| 21 | } |
| 22 | |
| 23 | // Methods from IHealth follow. |
| 24 | Return<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 | |
| 45 | bool 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 | |
| 62 | Return<Result> Health::unregisterCallback(const sp<IHealthInfoCallback>& callback) { |
| 63 | return unregisterCallbackInternal(callback) ? Result::SUCCESS : Result::NOT_FOUND; |
| 64 | } |
| 65 | |
| 66 | template<typename T> |
| 67 | void 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 | |
| 86 | Return<void> Health::getChargeCounter(getChargeCounter_cb _hidl_cb) { |
| 87 | getProperty(battery_monitor_, BATTERY_PROP_CHARGE_COUNTER, INT32_MIN, _hidl_cb); |
| 88 | return Void(); |
| 89 | } |
| 90 | |
| 91 | Return<void> Health::getCurrentNow(getCurrentNow_cb _hidl_cb) { |
| 92 | getProperty(battery_monitor_, BATTERY_PROP_CURRENT_NOW, INT32_MIN, _hidl_cb); |
| 93 | return Void(); |
| 94 | } |
| 95 | |
| 96 | Return<void> Health::getCurrentAverage(getCurrentAverage_cb _hidl_cb) { |
| 97 | getProperty(battery_monitor_, BATTERY_PROP_CURRENT_AVG, INT32_MIN, _hidl_cb); |
| 98 | return Void(); |
| 99 | } |
| 100 | |
| 101 | Return<void> Health::getCapacity(getCapacity_cb _hidl_cb) { |
| 102 | getProperty(battery_monitor_, BATTERY_PROP_CAPACITY, INT32_MIN, _hidl_cb); |
| 103 | return Void(); |
| 104 | } |
| 105 | |
| 106 | Return<void> Health::getEnergyCounter(getEnergyCounter_cb _hidl_cb) { |
| 107 | getProperty(battery_monitor_, BATTERY_PROP_ENERGY_COUNTER, INT64_MIN, _hidl_cb); |
| 108 | return Void(); |
| 109 | } |
| 110 | |
| 111 | Return<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 | |
| 117 | Return<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 Hong | 1106e83 | 2017-11-06 20:50:45 +0000 | [diff] [blame] | 125 | // notifyListeners. |
Yifan Hong | 2763df8 | 2017-09-19 17:57:50 -0700 | [diff] [blame] | 126 | 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 Hong | 1106e83 | 2017-11-06 20:50:45 +0000 | [diff] [blame] | 134 | void Health::notifyListeners(const HealthInfo& info) { |
Yifan Hong | 2763df8 | 2017-09-19 17:57:50 -0700 | [diff] [blame] | 135 | std::lock_guard<std::mutex> _lock(callbacks_lock_); |
| 136 | for (auto it = callbacks_.begin(); it != callbacks_.end();) { |
Yifan Hong | 1106e83 | 2017-11-06 20:50:45 +0000 | [diff] [blame] | 137 | auto ret = (*it)->healthInfoChanged(info); |
Yifan Hong | 2763df8 | 2017-09-19 17:57:50 -0700 | [diff] [blame] | 138 | if (!ret.isOk() && ret.isDeadObject()) { |
| 139 | it = callbacks_.erase(it); |
| 140 | } else { |
| 141 | ++it; |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | Return<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 | |
| 155 | void Health::serviceDied(uint64_t /* cookie */, const wp<IBase>& who) { |
| 156 | (void)unregisterCallbackInternal(who.promote()); |
| 157 | } |
| 158 | |
Yifan Hong | 9cffa8a | 2017-11-07 14:59:14 -0800 | [diff] [blame] | 159 | sp<IHealth> Health::initInstance(struct healthd_config* c) { |
| 160 | if (instance_ == nullptr) { |
| 161 | instance_ = new Health(c); |
| 162 | } |
| 163 | return instance_; |
| 164 | } |
| 165 | |
| 166 | sp<Health> Health::getImplementation() { |
| 167 | CHECK(instance_ != nullptr); |
| 168 | return instance_; |
| 169 | } |
Yifan Hong | 2763df8 | 2017-09-19 17:57:50 -0700 | [diff] [blame] | 170 | |
| 171 | } // namespace implementation |
| 172 | } // namespace V2_0 |
| 173 | } // namespace health |
| 174 | } // namespace hardware |
| 175 | } // namespace android |