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 17 #pragma once 18 19 #include <aidl/android/hardware/power/Boost.h> 20 #include <aidl/android/hardware/power/ChannelConfig.h> 21 #include <aidl/android/hardware/power/IPower.h> 22 #include <aidl/android/hardware/power/IPowerHintSession.h> 23 #include <aidl/android/hardware/power/Mode.h> 24 #include <aidl/android/hardware/power/SessionConfig.h> 25 #include <android-base/thread_annotations.h> 26 #include <android/hardware/power/1.1/IPower.h> 27 #include <android/hardware/power/1.2/IPower.h> 28 #include <android/hardware/power/1.3/IPower.h> 29 #include <powermanager/HalResult.h> 30 #include <powermanager/PowerHintSessionWrapper.h> 31 32 #include <binder/Status.h> 33 34 #include <utility> 35 36 namespace android { 37 38 namespace power { 39 40 // State of Power HAL support for individual apis. 41 enum class HalSupport { 42 UNKNOWN = 0, 43 ON = 1, 44 OFF = 2, 45 }; 46 47 // Wrapper for Power HAL handlers. 48 class HalWrapper { 49 public: 50 virtual ~HalWrapper() = default; 51 52 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; 55 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; 62 virtual HalResult<int64_t> getHintSessionPreferredRate() = 0; 63 virtual HalResult<aidl::android::hardware::power::ChannelConfig> getSessionChannel(int tgid, 64 int uid) = 0; 65 virtual HalResult<void> closeSessionChannel(int tgid, int uid) = 0; 66 virtual HalResult<aidl::android::hardware::power::SupportInfo> getSupportInfo() = 0; 67 }; 68 69 // Empty Power HAL wrapper that ignores all api calls. 70 class EmptyHalWrapper : public HalWrapper { 71 public: 72 EmptyHalWrapper() = default; 73 ~EmptyHalWrapper() override = default; 74 75 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; 78 HalResult<std::shared_ptr<PowerHintSessionWrapper>> createHintSession( 79 int32_t tgid, int32_t uid, const std::vector<int32_t>& threadIds, 80 int64_t durationNanos) override; 81 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; 85 HalResult<int64_t> getHintSessionPreferredRate() override; 86 HalResult<aidl::android::hardware::power::ChannelConfig> getSessionChannel(int tgid, 87 int uid) override; 88 HalResult<void> closeSessionChannel(int tgid, int uid) override; 89 HalResult<aidl::android::hardware::power::SupportInfo> getSupportInfo() override; 90 91 protected: 92 virtual const char* getUnsupportedMessage(); 93 }; 94 95 // Wrapper for the HIDL Power HAL v1.0. 96 class HidlHalWrapperV1_0 : public EmptyHalWrapper { 97 public: HidlHalWrapperV1_0(sp<hardware::power::V1_0::IPower> handleV1_0)98 explicit HidlHalWrapperV1_0(sp<hardware::power::V1_0::IPower> handleV1_0) 99 : mHandleV1_0(std::move(handleV1_0)) {} 100 ~HidlHalWrapperV1_0() override = default; 101 102 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; 105 106 protected: 107 const sp<hardware::power::V1_0::IPower> mHandleV1_0; 108 virtual HalResult<void> sendPowerHint(hardware::power::V1_3::PowerHint hintId, uint32_t data); 109 const char* getUnsupportedMessage(); 110 111 private: 112 HalResult<void> setInteractive(bool enabled); 113 HalResult<void> setFeature(hardware::power::V1_0::Feature feature, bool enabled); 114 }; 115 116 // Wrapper for the HIDL Power HAL v1.1. 117 class HidlHalWrapperV1_1 : public HidlHalWrapperV1_0 { 118 public: HidlHalWrapperV1_1(sp<hardware::power::V1_1::IPower> handleV1_1)119 explicit HidlHalWrapperV1_1(sp<hardware::power::V1_1::IPower> handleV1_1) 120 : HidlHalWrapperV1_0(std::move(handleV1_1)) {} 121 ~HidlHalWrapperV1_1() override = default; 122 123 protected: 124 HalResult<void> sendPowerHint(hardware::power::V1_3::PowerHint hintId, uint32_t data) override; 125 }; 126 127 // Wrapper for the HIDL Power HAL v1.2. 128 class HidlHalWrapperV1_2 : public HidlHalWrapperV1_1 { 129 public: 130 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; HidlHalWrapperV1_2(sp<hardware::power::V1_2::IPower> handleV1_2)133 explicit HidlHalWrapperV1_2(sp<hardware::power::V1_2::IPower> handleV1_2) 134 : HidlHalWrapperV1_1(std::move(handleV1_2)) {} 135 ~HidlHalWrapperV1_2() override = default; 136 137 protected: 138 HalResult<void> sendPowerHint(hardware::power::V1_3::PowerHint hintId, uint32_t data) override; 139 }; 140 141 // Wrapper for the HIDL Power HAL v1.3. 142 class HidlHalWrapperV1_3 : public HidlHalWrapperV1_2 { 143 public: 144 HalResult<void> setMode(aidl::android::hardware::power::Mode mode, bool enabled) override; HidlHalWrapperV1_3(sp<hardware::power::V1_3::IPower> handleV1_3)145 explicit HidlHalWrapperV1_3(sp<hardware::power::V1_3::IPower> handleV1_3) 146 : HidlHalWrapperV1_2(std::move(handleV1_3)) {} 147 ~HidlHalWrapperV1_3() override = default; 148 149 protected: 150 HalResult<void> sendPowerHint(hardware::power::V1_3::PowerHint hintId, uint32_t data) override; 151 }; 152 153 // Wrapper for the AIDL Power HAL. 154 class AidlHalWrapper : public EmptyHalWrapper { 155 public: AidlHalWrapper(std::shared_ptr<aidl::android::hardware::power::IPower> handle)156 explicit AidlHalWrapper(std::shared_ptr<aidl::android::hardware::power::IPower> handle) 157 : mHandle(std::move(handle)) {} 158 ~AidlHalWrapper() override = default; 159 160 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; 163 HalResult<std::shared_ptr<PowerHintSessionWrapper>> createHintSession( 164 int32_t tgid, int32_t uid, const std::vector<int32_t>& threadIds, 165 int64_t durationNanos) override; 166 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; 170 171 HalResult<int64_t> getHintSessionPreferredRate() override; 172 HalResult<aidl::android::hardware::power::ChannelConfig> getSessionChannel(int tgid, 173 int uid) override; 174 HalResult<void> closeSessionChannel(int tgid, int uid) override; 175 HalResult<aidl::android::hardware::power::SupportInfo> getSupportInfo() override; 176 177 protected: 178 const char* getUnsupportedMessage() override; 179 180 private: 181 // Control access to the boost and mode supported arrays. 182 std::mutex mBoostMutex; 183 std::mutex mModeMutex; 184 std::shared_ptr<aidl::android::hardware::power::IPower> mHandle; 185 std::array<HalSupport, 186 static_cast<int32_t>( 187 *(ndk::enum_range<aidl::android::hardware::power::Boost>().end() - 1)) + 188 1> 189 mBoostSupportedArray GUARDED_BY(mBoostMutex) = {HalSupport::UNKNOWN}; 190 std::array<HalSupport, 191 static_cast<int32_t>( 192 *(ndk::enum_range<aidl::android::hardware::power::Mode>().end() - 1)) + 193 1> 194 mModeSupportedArray GUARDED_BY(mModeMutex) = {HalSupport::UNKNOWN}; 195 }; 196 197 }; // namespace power 198 199 }; // namespace android 200