blob: 0b5d869496e5fb27860ca7669aaf9e6caca70715 [file] [log] [blame]
Yifan Hong50c9e252019-10-03 16:26:13 -07001/*
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
27using android::hardware::health::HealthLoop;
28using android::hardware::health::V2_0::implementation::Health;
29
30struct healthd_mode_ops* healthd_mode_ops = nullptr;
31
32// Adapter of HealthLoop to use legacy healthd_mode_ops.
33class 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};
49static std::unique_ptr<HealthLoopAdapter> health_loop;
50
51int healthd_register_event(int fd, void (*handler)(uint32_t), EventWakeup wakeup) {
Hridya Valsaraju55f1a1f2019-11-22 15:06:08 -080052 if (!health_loop) return -1;
53
Yifan Hong50c9e252019-10-03 16:26:13 -070054 auto wrapped_handler = [handler](auto*, uint32_t epevents) { handler(epevents); };
55 return health_loop->RegisterEvent(fd, wrapped_handler, wakeup);
56}
57
58void healthd_battery_update_internal(bool charger_online) {
Hridya Valsaraju55f1a1f2019-11-22 15:06:08 -080059 if (!health_loop) return;
Yifan Hong50c9e252019-10-03 16:26:13 -070060 health_loop->AdjustWakealarmPeriods(charger_online);
61}
62
63int healthd_main() {
64 if (!healthd_mode_ops) {
65 KLOG_ERROR("healthd ops not set, exiting\n");
66 exit(1);
67 }
68
69 health_loop = std::make_unique<HealthLoopAdapter>();
70
71 int ret = health_loop->StartLoop();
72
73 // Should not reach here. The following will exit().
74 health_loop.reset();
75
76 return ret;
77}