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 }; 67 68 // Empty Power HAL wrapper that ignores all api calls. 69 class EmptyHalWrapper : public HalWrapper { 70 public: 71 EmptyHalWrapper() = default; 72 ~EmptyHalWrapper() override = default; 73 74 HalResult<void> setBoost(aidl::android::hardware::power::Boost boost, 75 int32_t durationMs) override; 76 HalResult<void> setMode(aidl::android::hardware::power::Mode mode, bool enabled) override; 77 HalResult<std::shared_ptr<PowerHintSessionWrapper>> createHintSession( 78 int32_t tgid, int32_t uid, const std::vector<int32_t>& threadIds, 79 int64_t durationNanos) override; 80 HalResult<std::shared_ptr<PowerHintSessionWrapper>> createHintSessionWithConfig( 81 int32_t tgid, int32_t uid, const std::vector<int32_t>& threadIds, int64_t durationNanos, 82 aidl::android::hardware::power::SessionTag tag, 83 aidl::android::hardware::power::SessionConfig* config) override; 84 HalResult<int64_t> getHintSessionPreferredRate() override; 85 HalResult<aidl::android::hardware::power::ChannelConfig> getSessionChannel(int tgid, 86 int uid) override; 87 HalResult<void> closeSessionChannel(int tgid, int uid) override; 88 89 protected: 90 virtual const char* getUnsupportedMessage(); 91 }; 92 93 // Wrapper for the HIDL Power HAL v1.0. 94 class HidlHalWrapperV1_0 : public EmptyHalWrapper { 95 public: HidlHalWrapperV1_0(sp<hardware::power::V1_0::IPower> handleV1_0)96 explicit HidlHalWrapperV1_0(sp<hardware::power::V1_0::IPower> handleV1_0) 97 : mHandleV1_0(std::move(handleV1_0)) {} 98 ~HidlHalWrapperV1_0() override = default; 99 100 HalResult<void> setBoost(aidl::android::hardware::power::Boost boost, 101 int32_t durationMs) override; 102 HalResult<void> setMode(aidl::android::hardware::power::Mode mode, bool enabled) override; 103 104 protected: 105 const sp<hardware::power::V1_0::IPower> mHandleV1_0; 106 virtual HalResult<void> sendPowerHint(hardware::power::V1_3::PowerHint hintId, uint32_t data); 107 const char* getUnsupportedMessage(); 108 109 private: 110 HalResult<void> setInteractive(bool enabled); 111 HalResult<void> setFeature(hardware::power::V1_0::Feature feature, bool enabled); 112 }; 113 114 // Wrapper for the HIDL Power HAL v1.1. 115 class HidlHalWrapperV1_1 : public HidlHalWrapperV1_0 { 116 public: HidlHalWrapperV1_1(sp<hardware::power::V1_1::IPower> handleV1_1)117 explicit HidlHalWrapperV1_1(sp<hardware::power::V1_1::IPower> handleV1_1) 118 : HidlHalWrapperV1_0(std::move(handleV1_1)) {} 119 ~HidlHalWrapperV1_1() override = default; 120 121 protected: 122 HalResult<void> sendPowerHint(hardware::power::V1_3::PowerHint hintId, uint32_t data) override; 123 }; 124 125 // Wrapper for the HIDL Power HAL v1.2. 126 class HidlHalWrapperV1_2 : public HidlHalWrapperV1_1 { 127 public: 128 HalResult<void> setBoost(aidl::android::hardware::power::Boost boost, 129 int32_t durationMs) override; 130 HalResult<void> setMode(aidl::android::hardware::power::Mode mode, bool enabled) override; HidlHalWrapperV1_2(sp<hardware::power::V1_2::IPower> handleV1_2)131 explicit HidlHalWrapperV1_2(sp<hardware::power::V1_2::IPower> handleV1_2) 132 : HidlHalWrapperV1_1(std::move(handleV1_2)) {} 133 ~HidlHalWrapperV1_2() override = default; 134 135 protected: 136 HalResult<void> sendPowerHint(hardware::power::V1_3::PowerHint hintId, uint32_t data) override; 137 }; 138 139 // Wrapper for the HIDL Power HAL v1.3. 140 class HidlHalWrapperV1_3 : public HidlHalWrapperV1_2 { 141 public: 142 HalResult<void> setMode(aidl::android::hardware::power::Mode mode, bool enabled) override; HidlHalWrapperV1_3(sp<hardware::power::V1_3::IPower> handleV1_3)143 explicit HidlHalWrapperV1_3(sp<hardware::power::V1_3::IPower> handleV1_3) 144 : HidlHalWrapperV1_2(std::move(handleV1_3)) {} 145 ~HidlHalWrapperV1_3() override = default; 146 147 protected: 148 HalResult<void> sendPowerHint(hardware::power::V1_3::PowerHint hintId, uint32_t data) override; 149 }; 150 151 // Wrapper for the AIDL Power HAL. 152 class AidlHalWrapper : public EmptyHalWrapper { 153 public: AidlHalWrapper(std::shared_ptr<aidl::android::hardware::power::IPower> handle)154 explicit AidlHalWrapper(std::shared_ptr<aidl::android::hardware::power::IPower> handle) 155 : mHandle(std::move(handle)) {} 156 ~AidlHalWrapper() override = default; 157 158 HalResult<void> setBoost(aidl::android::hardware::power::Boost boost, 159 int32_t durationMs) override; 160 HalResult<void> setMode(aidl::android::hardware::power::Mode mode, bool enabled) override; 161 HalResult<std::shared_ptr<PowerHintSessionWrapper>> createHintSession( 162 int32_t tgid, int32_t uid, const std::vector<int32_t>& threadIds, 163 int64_t durationNanos) override; 164 HalResult<std::shared_ptr<PowerHintSessionWrapper>> createHintSessionWithConfig( 165 int32_t tgid, int32_t uid, const std::vector<int32_t>& threadIds, int64_t durationNanos, 166 aidl::android::hardware::power::SessionTag tag, 167 aidl::android::hardware::power::SessionConfig* config) override; 168 169 HalResult<int64_t> getHintSessionPreferredRate() override; 170 HalResult<aidl::android::hardware::power::ChannelConfig> getSessionChannel(int tgid, 171 int uid) override; 172 HalResult<void> closeSessionChannel(int tgid, int uid) override; 173 174 protected: 175 const char* getUnsupportedMessage() override; 176 177 private: 178 // Control access to the boost and mode supported arrays. 179 std::mutex mBoostMutex; 180 std::mutex mModeMutex; 181 std::shared_ptr<aidl::android::hardware::power::IPower> mHandle; 182 std::array<HalSupport, 183 static_cast<int32_t>( 184 *(ndk::enum_range<aidl::android::hardware::power::Boost>().end() - 1)) + 185 1> 186 mBoostSupportedArray GUARDED_BY(mBoostMutex) = {HalSupport::UNKNOWN}; 187 std::array<HalSupport, 188 static_cast<int32_t>( 189 *(ndk::enum_range<aidl::android::hardware::power::Mode>().end() - 1)) + 190 1> 191 mModeSupportedArray GUARDED_BY(mModeMutex) = {HalSupport::UNKNOWN}; 192 }; 193 194 }; // namespace power 195 196 }; // namespace android 197