blob: c46aaa4cb667624513a6f0e522a749167b043a47 [file] [log] [blame]
Yifan Hong830cdb12021-01-11 20:47:23 -08001/*
2 * Copyright (C) 2021 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#pragma once
18
19#include <memory>
20#include <optional>
21
22#include <aidl/android/hardware/health/IHealth.h>
23#include <health/HealthLoop.h>
24
25namespace aidl::android::hardware::health {
26
27class HalHealthLoop;
28
29class HalHealthLoopCallback {
30 public:
31 virtual ~HalHealthLoopCallback() = default;
32
33 // Called by HalHealthLoop::Init
34 virtual void OnInit(HalHealthLoop* hal_health_loop, struct healthd_config* config) = 0;
35 // Called by HalHealthLoop::Heartbeat
36 virtual void OnHeartbeat(){};
37 // Called by HalHealthLoop::PrepareToWait
38 virtual int OnPrepareToWait() { return -1; }
39 // Called by HalHealthLoop::ScheduleBatteryUpdate
40 virtual void OnHealthInfoChanged(const HealthInfo&) {}
41};
42
43// AIDL version of android::hardware::health::V2_1::implementation::HalHealthLoop.
44// An implementation of HealthLoop for using a given health HAL.
45class HalHealthLoop final : public ::android::hardware::health::HealthLoop {
46 public:
47 // Caller must ensure that the lifetime of service_ is not shorter than this object.
48 HalHealthLoop(std::shared_ptr<IHealth> service, std::shared_ptr<HalHealthLoopCallback> callback)
49 : service_(std::move(service)), callback_(std::move(callback)) {}
50
51 using HealthLoop::RegisterEvent;
52
53 bool charger_online() const { return charger_online_; }
54
55 protected:
56 virtual void Init(struct healthd_config* config) override;
57 virtual void Heartbeat() override;
58 virtual int PrepareToWait() override;
59 virtual void ScheduleBatteryUpdate() override;
60
61 private:
62 std::shared_ptr<IHealth> service_;
63 std::shared_ptr<HalHealthLoopCallback> callback_;
64 bool charger_online_ = false;
65
66 // Helpers of OnHealthInfoChanged.
67 void set_charger_online(const HealthInfo& health_info);
68
69 // HealthLoop periodically calls ScheduleBatteryUpdate, which calls
70 // OnHealthInfoChanged callback. A subclass of the callback can override
71 // HalHealthLoopCallback::OnHealthInfoChanged to
72 // broadcast the health_info to interested listeners.
73 // This adjust uevents / wakealarm periods.
74 void OnHealthInfoChanged(const HealthInfo& health_info);
75};
76
77} // namespace aidl::android::hardware::health