Yifan Hong | 50c9e25 | 2019-10-03 16:26:13 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | // Support legacy functions in healthd/healthd.h using healthd_mode_ops. |
| 18 | // New code should use HealthLoop directly instead. |
| 19 | |
| 20 | #include <memory> |
| 21 | |
| 22 | #include <cutils/klog.h> |
| 23 | #include <health/HealthLoop.h> |
| 24 | #include <health2/Health.h> |
| 25 | #include <healthd/healthd.h> |
| 26 | |
| 27 | using android::hardware::health::HealthLoop; |
| 28 | using android::hardware::health::V2_0::implementation::Health; |
| 29 | |
| 30 | struct healthd_mode_ops* healthd_mode_ops = nullptr; |
| 31 | |
| 32 | // Adapter of HealthLoop to use legacy healthd_mode_ops. |
| 33 | class HealthLoopAdapter : public HealthLoop { |
| 34 | public: |
| 35 | // Expose internal functions, assuming clients calls them in the same thread |
| 36 | // where StartLoop is called. |
| 37 | int RegisterEvent(int fd, BoundFunction func, EventWakeup wakeup) { |
| 38 | return HealthLoop::RegisterEvent(fd, func, wakeup); |
| 39 | } |
| 40 | void AdjustWakealarmPeriods(bool charger_online) { |
| 41 | return HealthLoop::AdjustWakealarmPeriods(charger_online); |
| 42 | } |
| 43 | protected: |
| 44 | void Init(healthd_config* config) override { healthd_mode_ops->init(config); } |
| 45 | void Heartbeat() override { healthd_mode_ops->heartbeat(); } |
| 46 | int PrepareToWait() override { return healthd_mode_ops->preparetowait(); } |
| 47 | void ScheduleBatteryUpdate() override { Health::getImplementation()->update(); } |
| 48 | }; |
| 49 | static std::unique_ptr<HealthLoopAdapter> health_loop; |
| 50 | |
| 51 | int healthd_register_event(int fd, void (*handler)(uint32_t), EventWakeup wakeup) { |
| 52 | auto wrapped_handler = [handler](auto*, uint32_t epevents) { handler(epevents); }; |
| 53 | return health_loop->RegisterEvent(fd, wrapped_handler, wakeup); |
| 54 | } |
| 55 | |
| 56 | void healthd_battery_update_internal(bool charger_online) { |
| 57 | health_loop->AdjustWakealarmPeriods(charger_online); |
| 58 | } |
| 59 | |
| 60 | int healthd_main() { |
| 61 | if (!healthd_mode_ops) { |
| 62 | KLOG_ERROR("healthd ops not set, exiting\n"); |
| 63 | exit(1); |
| 64 | } |
| 65 | |
| 66 | health_loop = std::make_unique<HealthLoopAdapter>(); |
| 67 | |
| 68 | int ret = health_loop->StartLoop(); |
| 69 | |
| 70 | // Should not reach here. The following will exit(). |
| 71 | health_loop.reset(); |
| 72 | |
| 73 | return ret; |
| 74 | } |