Lais Andrade | 4d51f6c | 2020-03-25 10:58:31 +0000 | [diff] [blame] | 1 | /* |
| 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 Buckley | db4192a | 2023-12-21 20:00:32 +0000 | [diff] [blame] | 17 | #pragma once |
Lais Andrade | 4d51f6c | 2020-03-25 10:58:31 +0000 | [diff] [blame] | 18 | |
Xiang Wang | 99f6f3c | 2023-05-22 13:12:16 -0700 | [diff] [blame] | 19 | #include <aidl/android/hardware/power/Boost.h> |
Matt Buckley | db4192a | 2023-12-21 20:00:32 +0000 | [diff] [blame] | 20 | #include <aidl/android/hardware/power/ChannelConfig.h> |
Xiang Wang | 99f6f3c | 2023-05-22 13:12:16 -0700 | [diff] [blame] | 21 | #include <aidl/android/hardware/power/IPower.h> |
| 22 | #include <aidl/android/hardware/power/IPowerHintSession.h> |
| 23 | #include <aidl/android/hardware/power/Mode.h> |
Matt Buckley | db4192a | 2023-12-21 20:00:32 +0000 | [diff] [blame] | 24 | #include <aidl/android/hardware/power/SessionConfig.h> |
Lais Andrade | 4d51f6c | 2020-03-25 10:58:31 +0000 | [diff] [blame] | 25 | #include <android-base/thread_annotations.h> |
Lais Andrade | 4d51f6c | 2020-03-25 10:58:31 +0000 | [diff] [blame] | 26 | #include <android/hardware/power/1.1/IPower.h> |
Matt Buckley | c3894a4 | 2022-09-01 21:17:15 +0000 | [diff] [blame] | 27 | #include <android/hardware/power/1.2/IPower.h> |
| 28 | #include <android/hardware/power/1.3/IPower.h> |
Xiang Wang | 99f6f3c | 2023-05-22 13:12:16 -0700 | [diff] [blame] | 29 | #include <binder/Status.h> |
Lais Andrade | 4d51f6c | 2020-03-25 10:58:31 +0000 | [diff] [blame] | 30 | |
Matt Buckley | db4192a | 2023-12-21 20:00:32 +0000 | [diff] [blame] | 31 | #include <utility> |
| 32 | |
Lais Andrade | 4d51f6c | 2020-03-25 10:58:31 +0000 | [diff] [blame] | 33 | namespace android { |
| 34 | |
Lais Andrade | b59a9b5 | 2020-05-07 17:23:42 +0100 | [diff] [blame] | 35 | namespace power { |
| 36 | |
Lais Andrade | 4d51f6c | 2020-03-25 10:58:31 +0000 | [diff] [blame] | 37 | // State of Power HAL support for individual apis. |
Lais Andrade | b59a9b5 | 2020-05-07 17:23:42 +0100 | [diff] [blame] | 38 | enum class HalSupport { |
Lais Andrade | 4d51f6c | 2020-03-25 10:58:31 +0000 | [diff] [blame] | 39 | UNKNOWN = 0, |
| 40 | ON = 1, |
| 41 | OFF = 2, |
| 42 | }; |
| 43 | |
Jimmy Shiu | 0b264bb | 2021-03-03 00:30:50 +0800 | [diff] [blame] | 44 | // Result of a call to the Power HAL wrapper, holding data if successful. |
| 45 | template <typename T> |
| 46 | class HalResult { |
| 47 | public: |
Matt Buckley | db4192a | 2023-12-21 20:00:32 +0000 | [diff] [blame] | 48 | static HalResult<T> ok(T&& value) { return HalResult(std::forward<T>(value)); } |
| 49 | static HalResult<T> ok(T& value) { return HalResult<T>::ok(T{value}); } |
| 50 | static HalResult<T> failed(std::string msg) { return HalResult(msg, /* unsupported= */ false); } |
Jimmy Shiu | 0b264bb | 2021-03-03 00:30:50 +0800 | [diff] [blame] | 51 | static HalResult<T> unsupported() { return HalResult("", /* unsupported= */ true); } |
| 52 | |
Matt Buckley | db4192a | 2023-12-21 20:00:32 +0000 | [diff] [blame] | 53 | static HalResult<T> fromStatus(const binder::Status& status, T&& data) { |
Jimmy Shiu | 0b264bb | 2021-03-03 00:30:50 +0800 | [diff] [blame] | 54 | if (status.exceptionCode() == binder::Status::EX_UNSUPPORTED_OPERATION) { |
| 55 | return HalResult<T>::unsupported(); |
| 56 | } |
| 57 | if (status.isOk()) { |
Matt Buckley | db4192a | 2023-12-21 20:00:32 +0000 | [diff] [blame] | 58 | return HalResult<T>::ok(std::forward<T>(data)); |
Jimmy Shiu | 0b264bb | 2021-03-03 00:30:50 +0800 | [diff] [blame] | 59 | } |
| 60 | return HalResult<T>::failed(std::string(status.toString8().c_str())); |
| 61 | } |
Xiang Wang | 99f6f3c | 2023-05-22 13:12:16 -0700 | [diff] [blame] | 62 | |
Matt Buckley | db4192a | 2023-12-21 20:00:32 +0000 | [diff] [blame] | 63 | static HalResult<T> fromStatus(const binder::Status& status, T& data) { |
| 64 | return HalResult<T>::fromStatus(status, T{data}); |
| 65 | } |
| 66 | |
| 67 | static HalResult<T> fromStatus(const ndk::ScopedAStatus& status, T&& data) { |
Xiang Wang | 99f6f3c | 2023-05-22 13:12:16 -0700 | [diff] [blame] | 68 | if (status.getExceptionCode() == binder::Status::EX_UNSUPPORTED_OPERATION) { |
| 69 | return HalResult<T>::unsupported(); |
| 70 | } |
| 71 | if (status.isOk()) { |
Matt Buckley | db4192a | 2023-12-21 20:00:32 +0000 | [diff] [blame] | 72 | return HalResult<T>::ok(std::forward<T>(data)); |
Xiang Wang | 99f6f3c | 2023-05-22 13:12:16 -0700 | [diff] [blame] | 73 | } |
| 74 | return HalResult<T>::failed(std::string(status.getDescription())); |
| 75 | } |
Jimmy Shiu | 0b264bb | 2021-03-03 00:30:50 +0800 | [diff] [blame] | 76 | |
Matt Buckley | db4192a | 2023-12-21 20:00:32 +0000 | [diff] [blame] | 77 | static HalResult<T> fromStatus(const ndk::ScopedAStatus& status, T& data) { |
| 78 | return HalResult<T>::fromStatus(status, T{data}); |
| 79 | } |
| 80 | |
Jimmy Shiu | 0b264bb | 2021-03-03 00:30:50 +0800 | [diff] [blame] | 81 | template <typename R> |
Matt Buckley | db4192a | 2023-12-21 20:00:32 +0000 | [diff] [blame] | 82 | static HalResult<T> fromReturn(hardware::Return<R>& ret, T&& data) { |
| 83 | return ret.isOk() ? HalResult<T>::ok(std::forward<T>(data)) |
| 84 | : HalResult<T>::failed(ret.description()); |
| 85 | } |
| 86 | |
| 87 | template <typename R> |
| 88 | static HalResult<T> fromReturn(hardware::Return<R>& ret, T& data) { |
| 89 | return HalResult<T>::fromReturn(ret, T{data}); |
Xiang Wang | 99f6f3c | 2023-05-22 13:12:16 -0700 | [diff] [blame] | 90 | } |
Jimmy Shiu | 0b264bb | 2021-03-03 00:30:50 +0800 | [diff] [blame] | 91 | |
| 92 | template <typename R> |
| 93 | static HalResult<T> fromReturn(hardware::Return<R>& ret, hardware::power::V1_0::Status status, |
Matt Buckley | db4192a | 2023-12-21 20:00:32 +0000 | [diff] [blame] | 94 | T&& data) { |
| 95 | return ret.isOk() ? HalResult<T>::fromStatus(status, std::forward<T>(data)) |
Xiang Wang | 99f6f3c | 2023-05-22 13:12:16 -0700 | [diff] [blame] | 96 | : HalResult<T>::failed(ret.description()); |
| 97 | } |
Jimmy Shiu | 0b264bb | 2021-03-03 00:30:50 +0800 | [diff] [blame] | 98 | |
Matt Buckley | db4192a | 2023-12-21 20:00:32 +0000 | [diff] [blame] | 99 | template <typename R> |
| 100 | static HalResult<T> fromReturn(hardware::Return<R>& ret, hardware::power::V1_0::Status status, |
| 101 | T& data) { |
| 102 | return HalResult<T>::fromReturn(ret, status, T{data}); |
| 103 | } |
| 104 | |
Jimmy Shiu | 0b264bb | 2021-03-03 00:30:50 +0800 | [diff] [blame] | 105 | // This will throw std::bad_optional_access if this result is not ok. |
| 106 | const T& value() const { return mValue.value(); } |
| 107 | bool isOk() const { return !mUnsupported && mValue.has_value(); } |
| 108 | bool isFailed() const { return !mUnsupported && !mValue.has_value(); } |
| 109 | bool isUnsupported() const { return mUnsupported; } |
| 110 | const char* errorMessage() const { return mErrorMessage.c_str(); } |
| 111 | |
| 112 | private: |
| 113 | std::optional<T> mValue; |
| 114 | std::string mErrorMessage; |
| 115 | bool mUnsupported; |
| 116 | |
Matt Buckley | db4192a | 2023-12-21 20:00:32 +0000 | [diff] [blame] | 117 | explicit HalResult(T&& value) |
| 118 | : mValue{std::move(value)}, mErrorMessage(), mUnsupported(false) {} |
Jimmy Shiu | 0b264bb | 2021-03-03 00:30:50 +0800 | [diff] [blame] | 119 | explicit HalResult(std::string errorMessage, bool unsupported) |
| 120 | : mValue(), mErrorMessage(std::move(errorMessage)), mUnsupported(unsupported) {} |
| 121 | }; |
| 122 | |
| 123 | // Empty result of a call to the Power HAL wrapper. |
| 124 | template <> |
| 125 | class HalResult<void> { |
| 126 | public: |
| 127 | static HalResult<void> ok() { return HalResult(); } |
| 128 | static HalResult<void> failed(std::string msg) { return HalResult(std::move(msg)); } |
| 129 | static HalResult<void> unsupported() { return HalResult(/* unsupported= */ true); } |
| 130 | |
Xiang Wang | 99f6f3c | 2023-05-22 13:12:16 -0700 | [diff] [blame] | 131 | static HalResult<void> fromStatus(const binder::Status& status) { |
| 132 | if (status.exceptionCode() == binder::Status::EX_UNSUPPORTED_OPERATION) { |
| 133 | return HalResult<void>::unsupported(); |
| 134 | } |
| 135 | if (status.isOk()) { |
| 136 | return HalResult<void>::ok(); |
| 137 | } |
| 138 | return HalResult<void>::failed(std::string(status.toString8().c_str())); |
| 139 | } |
| 140 | |
| 141 | static HalResult<void> fromStatus(const ndk::ScopedAStatus& status) { |
| 142 | if (status.getExceptionCode() == binder::Status::EX_UNSUPPORTED_OPERATION) { |
| 143 | return HalResult<void>::unsupported(); |
| 144 | } |
| 145 | if (status.isOk()) { |
| 146 | return HalResult<void>::ok(); |
| 147 | } |
| 148 | return HalResult<void>::failed(std::string(status.getDescription())); |
| 149 | } |
Jimmy Shiu | 0b264bb | 2021-03-03 00:30:50 +0800 | [diff] [blame] | 150 | |
| 151 | template <typename R> |
Xiang Wang | 99f6f3c | 2023-05-22 13:12:16 -0700 | [diff] [blame] | 152 | static HalResult<void> fromReturn(hardware::Return<R>& ret) { |
| 153 | return ret.isOk() ? HalResult<void>::ok() : HalResult<void>::failed(ret.description()); |
| 154 | } |
Jimmy Shiu | 0b264bb | 2021-03-03 00:30:50 +0800 | [diff] [blame] | 155 | |
| 156 | bool isOk() const { return !mUnsupported && !mFailed; } |
| 157 | bool isFailed() const { return !mUnsupported && mFailed; } |
| 158 | bool isUnsupported() const { return mUnsupported; } |
| 159 | const char* errorMessage() const { return mErrorMessage.c_str(); } |
| 160 | |
| 161 | private: |
| 162 | std::string mErrorMessage; |
| 163 | bool mFailed; |
| 164 | bool mUnsupported; |
| 165 | |
| 166 | explicit HalResult(bool unsupported = false) |
| 167 | : mErrorMessage(), mFailed(false), mUnsupported(unsupported) {} |
| 168 | explicit HalResult(std::string errorMessage) |
| 169 | : mErrorMessage(std::move(errorMessage)), mFailed(true), mUnsupported(false) {} |
Lais Andrade | 4d51f6c | 2020-03-25 10:58:31 +0000 | [diff] [blame] | 170 | }; |
| 171 | |
| 172 | // Wrapper for Power HAL handlers. |
Lais Andrade | b59a9b5 | 2020-05-07 17:23:42 +0100 | [diff] [blame] | 173 | class HalWrapper { |
Lais Andrade | 4d51f6c | 2020-03-25 10:58:31 +0000 | [diff] [blame] | 174 | public: |
Lais Andrade | b59a9b5 | 2020-05-07 17:23:42 +0100 | [diff] [blame] | 175 | virtual ~HalWrapper() = default; |
Lais Andrade | 4d51f6c | 2020-03-25 10:58:31 +0000 | [diff] [blame] | 176 | |
Xiang Wang | 99f6f3c | 2023-05-22 13:12:16 -0700 | [diff] [blame] | 177 | virtual HalResult<void> setBoost(aidl::android::hardware::power::Boost boost, |
| 178 | int32_t durationMs) = 0; |
| 179 | virtual HalResult<void> setMode(aidl::android::hardware::power::Mode mode, bool enabled) = 0; |
| 180 | virtual HalResult<std::shared_ptr<aidl::android::hardware::power::IPowerHintSession>> |
| 181 | createHintSession(int32_t tgid, int32_t uid, const std::vector<int32_t>& threadIds, |
| 182 | int64_t durationNanos) = 0; |
Matt Buckley | db4192a | 2023-12-21 20:00:32 +0000 | [diff] [blame] | 183 | virtual HalResult<std::shared_ptr<aidl::android::hardware::power::IPowerHintSession>> |
| 184 | createHintSessionWithConfig(int32_t tgid, int32_t uid, const std::vector<int32_t>& threadIds, |
| 185 | int64_t durationNanos, |
| 186 | aidl::android::hardware::power::SessionTag tag, |
| 187 | aidl::android::hardware::power::SessionConfig* config) = 0; |
Jimmy Shiu | 0b264bb | 2021-03-03 00:30:50 +0800 | [diff] [blame] | 188 | virtual HalResult<int64_t> getHintSessionPreferredRate() = 0; |
Matt Buckley | db4192a | 2023-12-21 20:00:32 +0000 | [diff] [blame] | 189 | virtual HalResult<aidl::android::hardware::power::ChannelConfig> getSessionChannel(int tgid, |
| 190 | int uid) = 0; |
| 191 | virtual HalResult<void> closeSessionChannel(int tgid, int uid) = 0; |
Lais Andrade | 4d51f6c | 2020-03-25 10:58:31 +0000 | [diff] [blame] | 192 | }; |
| 193 | |
| 194 | // Empty Power HAL wrapper that ignores all api calls. |
Lais Andrade | b59a9b5 | 2020-05-07 17:23:42 +0100 | [diff] [blame] | 195 | class EmptyHalWrapper : public HalWrapper { |
Lais Andrade | 4d51f6c | 2020-03-25 10:58:31 +0000 | [diff] [blame] | 196 | public: |
Lais Andrade | b59a9b5 | 2020-05-07 17:23:42 +0100 | [diff] [blame] | 197 | EmptyHalWrapper() = default; |
Xiang Wang | 99f6f3c | 2023-05-22 13:12:16 -0700 | [diff] [blame] | 198 | ~EmptyHalWrapper() override = default; |
Lais Andrade | 4d51f6c | 2020-03-25 10:58:31 +0000 | [diff] [blame] | 199 | |
Xiang Wang | 99f6f3c | 2023-05-22 13:12:16 -0700 | [diff] [blame] | 200 | HalResult<void> setBoost(aidl::android::hardware::power::Boost boost, |
| 201 | int32_t durationMs) override; |
| 202 | HalResult<void> setMode(aidl::android::hardware::power::Mode mode, bool enabled) override; |
| 203 | HalResult<std::shared_ptr<aidl::android::hardware::power::IPowerHintSession>> createHintSession( |
Jimmy Shiu | 0b264bb | 2021-03-03 00:30:50 +0800 | [diff] [blame] | 204 | int32_t tgid, int32_t uid, const std::vector<int32_t>& threadIds, |
| 205 | int64_t durationNanos) override; |
Matt Buckley | db4192a | 2023-12-21 20:00:32 +0000 | [diff] [blame] | 206 | HalResult<std::shared_ptr<aidl::android::hardware::power::IPowerHintSession>> |
| 207 | createHintSessionWithConfig(int32_t tgid, int32_t uid, const std::vector<int32_t>& threadIds, |
| 208 | int64_t durationNanos, |
| 209 | aidl::android::hardware::power::SessionTag tag, |
| 210 | aidl::android::hardware::power::SessionConfig* config) override; |
Xiang Wang | 99f6f3c | 2023-05-22 13:12:16 -0700 | [diff] [blame] | 211 | HalResult<int64_t> getHintSessionPreferredRate() override; |
Matt Buckley | db4192a | 2023-12-21 20:00:32 +0000 | [diff] [blame] | 212 | HalResult<aidl::android::hardware::power::ChannelConfig> getSessionChannel(int tgid, |
| 213 | int uid) override; |
| 214 | HalResult<void> closeSessionChannel(int tgid, int uid) override; |
| 215 | |
| 216 | protected: |
| 217 | virtual const char* getUnsupportedMessage(); |
Lais Andrade | 4d51f6c | 2020-03-25 10:58:31 +0000 | [diff] [blame] | 218 | }; |
| 219 | |
| 220 | // Wrapper for the HIDL Power HAL v1.0. |
Matt Buckley | db4192a | 2023-12-21 20:00:32 +0000 | [diff] [blame] | 221 | class HidlHalWrapperV1_0 : public EmptyHalWrapper { |
Lais Andrade | 4d51f6c | 2020-03-25 10:58:31 +0000 | [diff] [blame] | 222 | public: |
Matt Buckley | c3894a4 | 2022-09-01 21:17:15 +0000 | [diff] [blame] | 223 | explicit HidlHalWrapperV1_0(sp<hardware::power::V1_0::IPower> handleV1_0) |
| 224 | : mHandleV1_0(std::move(handleV1_0)) {} |
Xiang Wang | 99f6f3c | 2023-05-22 13:12:16 -0700 | [diff] [blame] | 225 | ~HidlHalWrapperV1_0() override = default; |
Lais Andrade | 4d51f6c | 2020-03-25 10:58:31 +0000 | [diff] [blame] | 226 | |
Xiang Wang | 99f6f3c | 2023-05-22 13:12:16 -0700 | [diff] [blame] | 227 | HalResult<void> setBoost(aidl::android::hardware::power::Boost boost, |
| 228 | int32_t durationMs) override; |
| 229 | HalResult<void> setMode(aidl::android::hardware::power::Mode mode, bool enabled) override; |
Lais Andrade | 4d51f6c | 2020-03-25 10:58:31 +0000 | [diff] [blame] | 230 | |
| 231 | protected: |
Matt Buckley | c3894a4 | 2022-09-01 21:17:15 +0000 | [diff] [blame] | 232 | const sp<hardware::power::V1_0::IPower> mHandleV1_0; |
| 233 | virtual HalResult<void> sendPowerHint(hardware::power::V1_3::PowerHint hintId, uint32_t data); |
Matt Buckley | db4192a | 2023-12-21 20:00:32 +0000 | [diff] [blame] | 234 | const char* getUnsupportedMessage(); |
Lais Andrade | 4d51f6c | 2020-03-25 10:58:31 +0000 | [diff] [blame] | 235 | |
| 236 | private: |
Jimmy Shiu | 0b264bb | 2021-03-03 00:30:50 +0800 | [diff] [blame] | 237 | HalResult<void> setInteractive(bool enabled); |
| 238 | HalResult<void> setFeature(hardware::power::V1_0::Feature feature, bool enabled); |
Lais Andrade | 4d51f6c | 2020-03-25 10:58:31 +0000 | [diff] [blame] | 239 | }; |
| 240 | |
| 241 | // Wrapper for the HIDL Power HAL v1.1. |
Lais Andrade | b59a9b5 | 2020-05-07 17:23:42 +0100 | [diff] [blame] | 242 | class HidlHalWrapperV1_1 : public HidlHalWrapperV1_0 { |
Lais Andrade | 4d51f6c | 2020-03-25 10:58:31 +0000 | [diff] [blame] | 243 | public: |
Xiang Wang | 99f6f3c | 2023-05-22 13:12:16 -0700 | [diff] [blame] | 244 | explicit HidlHalWrapperV1_1(sp<hardware::power::V1_1::IPower> handleV1_1) |
Matt Buckley | c3894a4 | 2022-09-01 21:17:15 +0000 | [diff] [blame] | 245 | : HidlHalWrapperV1_0(std::move(handleV1_1)) {} |
Xiang Wang | 99f6f3c | 2023-05-22 13:12:16 -0700 | [diff] [blame] | 246 | ~HidlHalWrapperV1_1() override = default; |
Lais Andrade | 4d51f6c | 2020-03-25 10:58:31 +0000 | [diff] [blame] | 247 | |
| 248 | protected: |
Xiang Wang | 99f6f3c | 2023-05-22 13:12:16 -0700 | [diff] [blame] | 249 | HalResult<void> sendPowerHint(hardware::power::V1_3::PowerHint hintId, uint32_t data) override; |
Matt Buckley | c3894a4 | 2022-09-01 21:17:15 +0000 | [diff] [blame] | 250 | }; |
Lais Andrade | 4d51f6c | 2020-03-25 10:58:31 +0000 | [diff] [blame] | 251 | |
Matt Buckley | c3894a4 | 2022-09-01 21:17:15 +0000 | [diff] [blame] | 252 | // Wrapper for the HIDL Power HAL v1.2. |
| 253 | class HidlHalWrapperV1_2 : public HidlHalWrapperV1_1 { |
| 254 | public: |
Xiang Wang | 99f6f3c | 2023-05-22 13:12:16 -0700 | [diff] [blame] | 255 | HalResult<void> setBoost(aidl::android::hardware::power::Boost boost, |
| 256 | int32_t durationMs) override; |
| 257 | HalResult<void> setMode(aidl::android::hardware::power::Mode mode, bool enabled) override; |
| 258 | explicit HidlHalWrapperV1_2(sp<hardware::power::V1_2::IPower> handleV1_2) |
Matt Buckley | c3894a4 | 2022-09-01 21:17:15 +0000 | [diff] [blame] | 259 | : HidlHalWrapperV1_1(std::move(handleV1_2)) {} |
Xiang Wang | 99f6f3c | 2023-05-22 13:12:16 -0700 | [diff] [blame] | 260 | ~HidlHalWrapperV1_2() override = default; |
Matt Buckley | c3894a4 | 2022-09-01 21:17:15 +0000 | [diff] [blame] | 261 | |
| 262 | protected: |
Xiang Wang | 99f6f3c | 2023-05-22 13:12:16 -0700 | [diff] [blame] | 263 | HalResult<void> sendPowerHint(hardware::power::V1_3::PowerHint hintId, uint32_t data) override; |
Matt Buckley | c3894a4 | 2022-09-01 21:17:15 +0000 | [diff] [blame] | 264 | }; |
| 265 | |
| 266 | // Wrapper for the HIDL Power HAL v1.3. |
| 267 | class HidlHalWrapperV1_3 : public HidlHalWrapperV1_2 { |
| 268 | public: |
Xiang Wang | 99f6f3c | 2023-05-22 13:12:16 -0700 | [diff] [blame] | 269 | HalResult<void> setMode(aidl::android::hardware::power::Mode mode, bool enabled) override; |
| 270 | explicit HidlHalWrapperV1_3(sp<hardware::power::V1_3::IPower> handleV1_3) |
Matt Buckley | c3894a4 | 2022-09-01 21:17:15 +0000 | [diff] [blame] | 271 | : HidlHalWrapperV1_2(std::move(handleV1_3)) {} |
Xiang Wang | 99f6f3c | 2023-05-22 13:12:16 -0700 | [diff] [blame] | 272 | ~HidlHalWrapperV1_3() override = default; |
Matt Buckley | c3894a4 | 2022-09-01 21:17:15 +0000 | [diff] [blame] | 273 | |
| 274 | protected: |
Xiang Wang | 99f6f3c | 2023-05-22 13:12:16 -0700 | [diff] [blame] | 275 | HalResult<void> sendPowerHint(hardware::power::V1_3::PowerHint hintId, uint32_t data) override; |
Lais Andrade | 4d51f6c | 2020-03-25 10:58:31 +0000 | [diff] [blame] | 276 | }; |
| 277 | |
| 278 | // Wrapper for the AIDL Power HAL. |
Matt Buckley | db4192a | 2023-12-21 20:00:32 +0000 | [diff] [blame] | 279 | class AidlHalWrapper : public EmptyHalWrapper { |
Lais Andrade | 4d51f6c | 2020-03-25 10:58:31 +0000 | [diff] [blame] | 280 | public: |
Xiang Wang | 99f6f3c | 2023-05-22 13:12:16 -0700 | [diff] [blame] | 281 | explicit AidlHalWrapper(std::shared_ptr<aidl::android::hardware::power::IPower> handle) |
| 282 | : mHandle(std::move(handle)) {} |
| 283 | ~AidlHalWrapper() override = default; |
Lais Andrade | 4d51f6c | 2020-03-25 10:58:31 +0000 | [diff] [blame] | 284 | |
Xiang Wang | 99f6f3c | 2023-05-22 13:12:16 -0700 | [diff] [blame] | 285 | HalResult<void> setBoost(aidl::android::hardware::power::Boost boost, |
| 286 | int32_t durationMs) override; |
| 287 | HalResult<void> setMode(aidl::android::hardware::power::Mode mode, bool enabled) override; |
| 288 | HalResult<std::shared_ptr<aidl::android::hardware::power::IPowerHintSession>> createHintSession( |
Jimmy Shiu | 0b264bb | 2021-03-03 00:30:50 +0800 | [diff] [blame] | 289 | int32_t tgid, int32_t uid, const std::vector<int32_t>& threadIds, |
| 290 | int64_t durationNanos) override; |
Matt Buckley | db4192a | 2023-12-21 20:00:32 +0000 | [diff] [blame] | 291 | HalResult<std::shared_ptr<aidl::android::hardware::power::IPowerHintSession>> |
| 292 | createHintSessionWithConfig(int32_t tgid, int32_t uid, const std::vector<int32_t>& threadIds, |
| 293 | int64_t durationNanos, |
| 294 | aidl::android::hardware::power::SessionTag tag, |
| 295 | aidl::android::hardware::power::SessionConfig* config) override; |
| 296 | |
Xiang Wang | 99f6f3c | 2023-05-22 13:12:16 -0700 | [diff] [blame] | 297 | HalResult<int64_t> getHintSessionPreferredRate() override; |
Matt Buckley | db4192a | 2023-12-21 20:00:32 +0000 | [diff] [blame] | 298 | HalResult<aidl::android::hardware::power::ChannelConfig> getSessionChannel(int tgid, |
| 299 | int uid) override; |
| 300 | HalResult<void> closeSessionChannel(int tgid, int uid) override; |
| 301 | |
| 302 | protected: |
| 303 | const char* getUnsupportedMessage() override; |
Lais Andrade | 4d51f6c | 2020-03-25 10:58:31 +0000 | [diff] [blame] | 304 | |
| 305 | private: |
| 306 | // Control access to the boost and mode supported arrays. |
| 307 | std::mutex mBoostMutex; |
| 308 | std::mutex mModeMutex; |
Xiang Wang | 99f6f3c | 2023-05-22 13:12:16 -0700 | [diff] [blame] | 309 | std::shared_ptr<aidl::android::hardware::power::IPower> mHandle; |
Lais Andrade | 4d51f6c | 2020-03-25 10:58:31 +0000 | [diff] [blame] | 310 | // Android framework only sends boost upto DISPLAY_UPDATE_IMMINENT. |
| 311 | // Need to increase the array size if more boost supported. |
Xiang Wang | 99f6f3c | 2023-05-22 13:12:16 -0700 | [diff] [blame] | 312 | std::array< |
| 313 | std::atomic<HalSupport>, |
| 314 | static_cast<int32_t>(aidl::android::hardware::power::Boost::DISPLAY_UPDATE_IMMINENT) + |
| 315 | 1> |
Lais Andrade | b59a9b5 | 2020-05-07 17:23:42 +0100 | [diff] [blame] | 316 | mBoostSupportedArray GUARDED_BY(mBoostMutex) = {HalSupport::UNKNOWN}; |
Lais Andrade | b59a9b5 | 2020-05-07 17:23:42 +0100 | [diff] [blame] | 317 | std::array<std::atomic<HalSupport>, |
Xiang Wang | 99f6f3c | 2023-05-22 13:12:16 -0700 | [diff] [blame] | 318 | static_cast<int32_t>( |
| 319 | *(ndk::enum_range<aidl::android::hardware::power::Mode>().end() - 1)) + |
| 320 | 1> |
Lais Andrade | b59a9b5 | 2020-05-07 17:23:42 +0100 | [diff] [blame] | 321 | mModeSupportedArray GUARDED_BY(mModeMutex) = {HalSupport::UNKNOWN}; |
Lais Andrade | 4d51f6c | 2020-03-25 10:58:31 +0000 | [diff] [blame] | 322 | }; |
| 323 | |
Lais Andrade | b59a9b5 | 2020-05-07 17:23:42 +0100 | [diff] [blame] | 324 | }; // namespace power |
| 325 | |
Lais Andrade | 4d51f6c | 2020-03-25 10:58:31 +0000 | [diff] [blame] | 326 | }; // namespace android |