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.
RegisterEvent(int fd,BoundFunction func,EventWakeup wakeup)37     int RegisterEvent(int fd, BoundFunction func, EventWakeup wakeup) {
38         return HealthLoop::RegisterEvent(fd, func, wakeup);
39     }
AdjustWakealarmPeriods(bool charger_online)40     void AdjustWakealarmPeriods(bool charger_online) {
41         return HealthLoop::AdjustWakealarmPeriods(charger_online);
42     }
43    protected:
Init(healthd_config * config)44     void Init(healthd_config* config) override { healthd_mode_ops->init(config); }
Heartbeat()45     void Heartbeat() override { healthd_mode_ops->heartbeat(); }
PrepareToWait()46     int PrepareToWait() override { return healthd_mode_ops->preparetowait(); }
ScheduleBatteryUpdate()47     void ScheduleBatteryUpdate() override { Health::getImplementation()->update(); }
48 };
49 static std::unique_ptr<HealthLoopAdapter> health_loop;
50 
healthd_register_event(int fd,void (* handler)(uint32_t),EventWakeup wakeup)51 int healthd_register_event(int fd, void (*handler)(uint32_t), EventWakeup wakeup) {
52     if (!health_loop) return -1;
53 
54     auto wrapped_handler = [handler](auto*, uint32_t epevents) { handler(epevents); };
55     return health_loop->RegisterEvent(fd, wrapped_handler, wakeup);
56 }
57 
healthd_battery_update_internal(bool charger_online)58 void healthd_battery_update_internal(bool charger_online) {
59     if (!health_loop) return;
60     health_loop->AdjustWakealarmPeriods(charger_online);
61 }
62 
healthd_main()63 int 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 }
78