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 #ifndef ANDROID_POWERHALCONTROLLER_H 18 #define ANDROID_POWERHALCONTROLLER_H 19 20 #include <aidl/android/hardware/power/Boost.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 <android-base/thread_annotations.h> 25 #include <powermanager/PowerHalWrapper.h> 26 #include <powermanager/PowerHintSessionWrapper.h> 27 28 namespace android { 29 30 namespace power { 31 32 // ------------------------------------------------------------------------------------------------- 33 34 // Connects to underlying Power HAL handles. 35 class HalConnector { 36 public: 37 HalConnector() = default; 38 virtual ~HalConnector() = default; 39 40 virtual std::unique_ptr<HalWrapper> connect(); 41 virtual void reset(); 42 virtual int32_t getAidlVersion(); 43 }; 44 45 // ------------------------------------------------------------------------------------------------- 46 47 // Controller for Power HAL handle. 48 // This relies on HalConnector to connect to the underlying Power HAL 49 // service and reconnects to it after each failed api call. This also ensures 50 // connecting to the service is thread-safe. 51 class PowerHalController : public HalWrapper { 52 public: PowerHalController()53 PowerHalController() : PowerHalController(std::make_unique<HalConnector>()) {} PowerHalController(std::unique_ptr<HalConnector> connector)54 explicit PowerHalController(std::unique_ptr<HalConnector> connector) 55 : mHalConnector(std::move(connector)) {} 56 virtual ~PowerHalController() = default; 57 58 virtual void init(); 59 60 virtual HalResult<void> setBoost(aidl::android::hardware::power::Boost boost, 61 int32_t durationMs) override; 62 virtual HalResult<void> setMode(aidl::android::hardware::power::Mode mode, 63 bool enabled) override; 64 virtual HalResult<std::shared_ptr<PowerHintSessionWrapper>> createHintSession( 65 int32_t tgid, int32_t uid, const std::vector<int32_t>& threadIds, 66 int64_t durationNanos) override; 67 virtual HalResult<std::shared_ptr<PowerHintSessionWrapper>> createHintSessionWithConfig( 68 int32_t tgid, int32_t uid, const std::vector<int32_t>& threadIds, int64_t durationNanos, 69 aidl::android::hardware::power::SessionTag tag, 70 aidl::android::hardware::power::SessionConfig* config) override; 71 virtual HalResult<int64_t> getHintSessionPreferredRate() override; 72 virtual HalResult<aidl::android::hardware::power::ChannelConfig> getSessionChannel( 73 int tgid, int uid) override; 74 virtual HalResult<void> closeSessionChannel(int tgid, int uid) override; 75 76 private: 77 std::mutex mConnectedHalMutex; 78 std::unique_ptr<HalConnector> mHalConnector; 79 80 // Shared pointers to keep global pointer and allow local copies to be used in 81 // different threads 82 std::shared_ptr<HalWrapper> mConnectedHal GUARDED_BY(mConnectedHalMutex) = nullptr; 83 const std::shared_ptr<HalWrapper> mDefaultHal = std::make_shared<EmptyHalWrapper>(); 84 85 std::shared_ptr<HalWrapper> initHal(); 86 template <typename T> 87 HalResult<T> processHalResult(HalResult<T>&& result, const char* functionName); 88 }; 89 90 // ------------------------------------------------------------------------------------------------- 91 92 }; // namespace power 93 94 }; // namespace android 95 96 #endif // ANDROID_POWERHALCONTROLLER_H 97