blob: 42901821bcff2081b0dd9fba6c3893a92cbc3781 [file] [log] [blame]
Lais Andrade4d51f6c2020-03-25 10:58:31 +00001/*
2 * Copyright (C) 2020 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
Matt Buckleydb4192a2023-12-21 20:00:32 +000017#pragma once
Lais Andrade4d51f6c2020-03-25 10:58:31 +000018
Xiang Wang99f6f3c2023-05-22 13:12:16 -070019#include <aidl/android/hardware/power/Boost.h>
Matt Buckleydb4192a2023-12-21 20:00:32 +000020#include <aidl/android/hardware/power/ChannelConfig.h>
Xiang Wang99f6f3c2023-05-22 13:12:16 -070021#include <aidl/android/hardware/power/IPower.h>
22#include <aidl/android/hardware/power/IPowerHintSession.h>
23#include <aidl/android/hardware/power/Mode.h>
Matt Buckleydb4192a2023-12-21 20:00:32 +000024#include <aidl/android/hardware/power/SessionConfig.h>
Lais Andrade4d51f6c2020-03-25 10:58:31 +000025#include <android-base/thread_annotations.h>
Lais Andrade4d51f6c2020-03-25 10:58:31 +000026#include <android/hardware/power/1.1/IPower.h>
Matt Buckleyc3894a42022-09-01 21:17:15 +000027#include <android/hardware/power/1.2/IPower.h>
28#include <android/hardware/power/1.3/IPower.h>
Matt Buckley6c18e6d2024-02-07 23:39:50 +000029#include <powermanager/HalResult.h>
30#include <powermanager/PowerHintSessionWrapper.h>
31
Xiang Wang99f6f3c2023-05-22 13:12:16 -070032#include <binder/Status.h>
Lais Andrade4d51f6c2020-03-25 10:58:31 +000033
Matt Buckleydb4192a2023-12-21 20:00:32 +000034#include <utility>
35
Lais Andrade4d51f6c2020-03-25 10:58:31 +000036namespace android {
37
Lais Andradeb59a9b52020-05-07 17:23:42 +010038namespace power {
39
Lais Andrade4d51f6c2020-03-25 10:58:31 +000040// State of Power HAL support for individual apis.
Lais Andradeb59a9b52020-05-07 17:23:42 +010041enum class HalSupport {
Lais Andrade4d51f6c2020-03-25 10:58:31 +000042 UNKNOWN = 0,
43 ON = 1,
44 OFF = 2,
45};
46
Lais Andrade4d51f6c2020-03-25 10:58:31 +000047// Wrapper for Power HAL handlers.
Lais Andradeb59a9b52020-05-07 17:23:42 +010048class HalWrapper {
Lais Andrade4d51f6c2020-03-25 10:58:31 +000049public:
Lais Andradeb59a9b52020-05-07 17:23:42 +010050 virtual ~HalWrapper() = default;
Lais Andrade4d51f6c2020-03-25 10:58:31 +000051
Xiang Wang99f6f3c2023-05-22 13:12:16 -070052 virtual HalResult<void> setBoost(aidl::android::hardware::power::Boost boost,
53 int32_t durationMs) = 0;
54 virtual HalResult<void> setMode(aidl::android::hardware::power::Mode mode, bool enabled) = 0;
Matt Buckley6c18e6d2024-02-07 23:39:50 +000055 virtual HalResult<std::shared_ptr<PowerHintSessionWrapper>> createHintSession(
56 int32_t tgid, int32_t uid, const std::vector<int32_t>& threadIds,
57 int64_t durationNanos) = 0;
58 virtual HalResult<std::shared_ptr<PowerHintSessionWrapper>> createHintSessionWithConfig(
59 int32_t tgid, int32_t uid, const std::vector<int32_t>& threadIds, int64_t durationNanos,
60 aidl::android::hardware::power::SessionTag tag,
61 aidl::android::hardware::power::SessionConfig* config) = 0;
Jimmy Shiu0b264bb2021-03-03 00:30:50 +080062 virtual HalResult<int64_t> getHintSessionPreferredRate() = 0;
Matt Buckleydb4192a2023-12-21 20:00:32 +000063 virtual HalResult<aidl::android::hardware::power::ChannelConfig> getSessionChannel(int tgid,
64 int uid) = 0;
65 virtual HalResult<void> closeSessionChannel(int tgid, int uid) = 0;
Matt Buckley8f997cb2024-11-16 19:26:34 -080066 virtual HalResult<aidl::android::hardware::power::SupportInfo> getSupportInfo() = 0;
Lais Andrade4d51f6c2020-03-25 10:58:31 +000067};
68
69// Empty Power HAL wrapper that ignores all api calls.
Lais Andradeb59a9b52020-05-07 17:23:42 +010070class EmptyHalWrapper : public HalWrapper {
Lais Andrade4d51f6c2020-03-25 10:58:31 +000071public:
Lais Andradeb59a9b52020-05-07 17:23:42 +010072 EmptyHalWrapper() = default;
Xiang Wang99f6f3c2023-05-22 13:12:16 -070073 ~EmptyHalWrapper() override = default;
Lais Andrade4d51f6c2020-03-25 10:58:31 +000074
Xiang Wang99f6f3c2023-05-22 13:12:16 -070075 HalResult<void> setBoost(aidl::android::hardware::power::Boost boost,
76 int32_t durationMs) override;
77 HalResult<void> setMode(aidl::android::hardware::power::Mode mode, bool enabled) override;
Matt Buckley6c18e6d2024-02-07 23:39:50 +000078 HalResult<std::shared_ptr<PowerHintSessionWrapper>> createHintSession(
Jimmy Shiu0b264bb2021-03-03 00:30:50 +080079 int32_t tgid, int32_t uid, const std::vector<int32_t>& threadIds,
80 int64_t durationNanos) override;
Matt Buckley6c18e6d2024-02-07 23:39:50 +000081 HalResult<std::shared_ptr<PowerHintSessionWrapper>> createHintSessionWithConfig(
82 int32_t tgid, int32_t uid, const std::vector<int32_t>& threadIds, int64_t durationNanos,
83 aidl::android::hardware::power::SessionTag tag,
84 aidl::android::hardware::power::SessionConfig* config) override;
Xiang Wang99f6f3c2023-05-22 13:12:16 -070085 HalResult<int64_t> getHintSessionPreferredRate() override;
Matt Buckleydb4192a2023-12-21 20:00:32 +000086 HalResult<aidl::android::hardware::power::ChannelConfig> getSessionChannel(int tgid,
87 int uid) override;
88 HalResult<void> closeSessionChannel(int tgid, int uid) override;
Matt Buckley8f997cb2024-11-16 19:26:34 -080089 HalResult<aidl::android::hardware::power::SupportInfo> getSupportInfo() override;
Matt Buckleydb4192a2023-12-21 20:00:32 +000090
91protected:
92 virtual const char* getUnsupportedMessage();
Lais Andrade4d51f6c2020-03-25 10:58:31 +000093};
94
95// Wrapper for the HIDL Power HAL v1.0.
Matt Buckleydb4192a2023-12-21 20:00:32 +000096class HidlHalWrapperV1_0 : public EmptyHalWrapper {
Lais Andrade4d51f6c2020-03-25 10:58:31 +000097public:
Matt Buckleyc3894a42022-09-01 21:17:15 +000098 explicit HidlHalWrapperV1_0(sp<hardware::power::V1_0::IPower> handleV1_0)
99 : mHandleV1_0(std::move(handleV1_0)) {}
Xiang Wang99f6f3c2023-05-22 13:12:16 -0700100 ~HidlHalWrapperV1_0() override = default;
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000101
Xiang Wang99f6f3c2023-05-22 13:12:16 -0700102 HalResult<void> setBoost(aidl::android::hardware::power::Boost boost,
103 int32_t durationMs) override;
104 HalResult<void> setMode(aidl::android::hardware::power::Mode mode, bool enabled) override;
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000105
106protected:
Matt Buckleyc3894a42022-09-01 21:17:15 +0000107 const sp<hardware::power::V1_0::IPower> mHandleV1_0;
108 virtual HalResult<void> sendPowerHint(hardware::power::V1_3::PowerHint hintId, uint32_t data);
Matt Buckleydb4192a2023-12-21 20:00:32 +0000109 const char* getUnsupportedMessage();
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000110
111private:
Jimmy Shiu0b264bb2021-03-03 00:30:50 +0800112 HalResult<void> setInteractive(bool enabled);
113 HalResult<void> setFeature(hardware::power::V1_0::Feature feature, bool enabled);
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000114};
115
116// Wrapper for the HIDL Power HAL v1.1.
Lais Andradeb59a9b52020-05-07 17:23:42 +0100117class HidlHalWrapperV1_1 : public HidlHalWrapperV1_0 {
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000118public:
Xiang Wang99f6f3c2023-05-22 13:12:16 -0700119 explicit HidlHalWrapperV1_1(sp<hardware::power::V1_1::IPower> handleV1_1)
Matt Buckleyc3894a42022-09-01 21:17:15 +0000120 : HidlHalWrapperV1_0(std::move(handleV1_1)) {}
Xiang Wang99f6f3c2023-05-22 13:12:16 -0700121 ~HidlHalWrapperV1_1() override = default;
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000122
123protected:
Xiang Wang99f6f3c2023-05-22 13:12:16 -0700124 HalResult<void> sendPowerHint(hardware::power::V1_3::PowerHint hintId, uint32_t data) override;
Matt Buckleyc3894a42022-09-01 21:17:15 +0000125};
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000126
Matt Buckleyc3894a42022-09-01 21:17:15 +0000127// Wrapper for the HIDL Power HAL v1.2.
128class HidlHalWrapperV1_2 : public HidlHalWrapperV1_1 {
129public:
Xiang Wang99f6f3c2023-05-22 13:12:16 -0700130 HalResult<void> setBoost(aidl::android::hardware::power::Boost boost,
131 int32_t durationMs) override;
132 HalResult<void> setMode(aidl::android::hardware::power::Mode mode, bool enabled) override;
133 explicit HidlHalWrapperV1_2(sp<hardware::power::V1_2::IPower> handleV1_2)
Matt Buckleyc3894a42022-09-01 21:17:15 +0000134 : HidlHalWrapperV1_1(std::move(handleV1_2)) {}
Xiang Wang99f6f3c2023-05-22 13:12:16 -0700135 ~HidlHalWrapperV1_2() override = default;
Matt Buckleyc3894a42022-09-01 21:17:15 +0000136
137protected:
Xiang Wang99f6f3c2023-05-22 13:12:16 -0700138 HalResult<void> sendPowerHint(hardware::power::V1_3::PowerHint hintId, uint32_t data) override;
Matt Buckleyc3894a42022-09-01 21:17:15 +0000139};
140
141// Wrapper for the HIDL Power HAL v1.3.
142class HidlHalWrapperV1_3 : public HidlHalWrapperV1_2 {
143public:
Xiang Wang99f6f3c2023-05-22 13:12:16 -0700144 HalResult<void> setMode(aidl::android::hardware::power::Mode mode, bool enabled) override;
145 explicit HidlHalWrapperV1_3(sp<hardware::power::V1_3::IPower> handleV1_3)
Matt Buckleyc3894a42022-09-01 21:17:15 +0000146 : HidlHalWrapperV1_2(std::move(handleV1_3)) {}
Xiang Wang99f6f3c2023-05-22 13:12:16 -0700147 ~HidlHalWrapperV1_3() override = default;
Matt Buckleyc3894a42022-09-01 21:17:15 +0000148
149protected:
Xiang Wang99f6f3c2023-05-22 13:12:16 -0700150 HalResult<void> sendPowerHint(hardware::power::V1_3::PowerHint hintId, uint32_t data) override;
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000151};
152
153// Wrapper for the AIDL Power HAL.
Matt Buckleydb4192a2023-12-21 20:00:32 +0000154class AidlHalWrapper : public EmptyHalWrapper {
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000155public:
Xiang Wang99f6f3c2023-05-22 13:12:16 -0700156 explicit AidlHalWrapper(std::shared_ptr<aidl::android::hardware::power::IPower> handle)
157 : mHandle(std::move(handle)) {}
158 ~AidlHalWrapper() override = default;
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000159
Xiang Wang99f6f3c2023-05-22 13:12:16 -0700160 HalResult<void> setBoost(aidl::android::hardware::power::Boost boost,
161 int32_t durationMs) override;
162 HalResult<void> setMode(aidl::android::hardware::power::Mode mode, bool enabled) override;
Matt Buckley6c18e6d2024-02-07 23:39:50 +0000163 HalResult<std::shared_ptr<PowerHintSessionWrapper>> createHintSession(
Jimmy Shiu0b264bb2021-03-03 00:30:50 +0800164 int32_t tgid, int32_t uid, const std::vector<int32_t>& threadIds,
165 int64_t durationNanos) override;
Matt Buckley6c18e6d2024-02-07 23:39:50 +0000166 HalResult<std::shared_ptr<PowerHintSessionWrapper>> createHintSessionWithConfig(
167 int32_t tgid, int32_t uid, const std::vector<int32_t>& threadIds, int64_t durationNanos,
168 aidl::android::hardware::power::SessionTag tag,
169 aidl::android::hardware::power::SessionConfig* config) override;
Matt Buckleydb4192a2023-12-21 20:00:32 +0000170
Xiang Wang99f6f3c2023-05-22 13:12:16 -0700171 HalResult<int64_t> getHintSessionPreferredRate() override;
Matt Buckleydb4192a2023-12-21 20:00:32 +0000172 HalResult<aidl::android::hardware::power::ChannelConfig> getSessionChannel(int tgid,
173 int uid) override;
174 HalResult<void> closeSessionChannel(int tgid, int uid) override;
Matt Buckley8f997cb2024-11-16 19:26:34 -0800175 HalResult<aidl::android::hardware::power::SupportInfo> getSupportInfo() override;
Matt Buckleydb4192a2023-12-21 20:00:32 +0000176
177protected:
178 const char* getUnsupportedMessage() override;
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000179
180private:
181 // Control access to the boost and mode supported arrays.
182 std::mutex mBoostMutex;
183 std::mutex mModeMutex;
Xiang Wang99f6f3c2023-05-22 13:12:16 -0700184 std::shared_ptr<aidl::android::hardware::power::IPower> mHandle;
Matt Buckley6c18e6d2024-02-07 23:39:50 +0000185 std::array<HalSupport,
186 static_cast<int32_t>(
187 *(ndk::enum_range<aidl::android::hardware::power::Boost>().end() - 1)) +
188 1>
Lais Andradeb59a9b52020-05-07 17:23:42 +0100189 mBoostSupportedArray GUARDED_BY(mBoostMutex) = {HalSupport::UNKNOWN};
Matt Buckley6c18e6d2024-02-07 23:39:50 +0000190 std::array<HalSupport,
Xiang Wang99f6f3c2023-05-22 13:12:16 -0700191 static_cast<int32_t>(
192 *(ndk::enum_range<aidl::android::hardware::power::Mode>().end() - 1)) +
193 1>
Lais Andradeb59a9b52020-05-07 17:23:42 +0100194 mModeSupportedArray GUARDED_BY(mModeMutex) = {HalSupport::UNKNOWN};
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000195};
196
Lais Andradeb59a9b52020-05-07 17:23:42 +0100197}; // namespace power
198
Lais Andrade4d51f6c2020-03-25 10:58:31 +0000199}; // namespace android