Lais Andrade | 3f7ecc5 | 2020-03-25 23:57:08 +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 | |
| 17 | #ifndef ANDROID_POWERHALCONTROLLER_H |
| 18 | #define ANDROID_POWERHALCONTROLLER_H |
| 19 | |
| 20 | #include <android-base/thread_annotations.h> |
| 21 | #include <android/hardware/power/Boost.h> |
| 22 | #include <android/hardware/power/IPower.h> |
| 23 | #include <android/hardware/power/Mode.h> |
| 24 | |
| 25 | #include <powermanager/PowerHalWrapper.h> |
| 26 | |
| 27 | using android::hardware::power::Boost; |
| 28 | using android::hardware::power::Mode; |
| 29 | using android::hardware::power::V1_0::Feature; |
| 30 | using android::hardware::power::V1_0::PowerHint; |
| 31 | |
| 32 | namespace android { |
| 33 | |
| 34 | // ------------------------------------------------------------------------------------------------- |
| 35 | |
| 36 | // Connects to underlying Power HAL handles. |
| 37 | class PowerHalConnector { |
| 38 | public: |
| 39 | PowerHalConnector() = default; |
| 40 | virtual ~PowerHalConnector() = default; |
| 41 | |
| 42 | virtual std::unique_ptr<PowerHalWrapper> connect(); |
| 43 | virtual void reset(); |
| 44 | }; |
| 45 | |
| 46 | // ------------------------------------------------------------------------------------------------- |
| 47 | |
| 48 | // Controller for Power HAL handle. |
| 49 | // This relies on PowerHalConnector to connect to the underlying Power HAL service and reconnects to |
| 50 | // it after each failed api call. This also ensures connecting to the service is thread-safe. |
| 51 | class PowerHalController : public PowerHalWrapper { |
| 52 | public: |
| 53 | PowerHalController() : PowerHalController(std::make_unique<PowerHalConnector>()) {} |
| 54 | explicit PowerHalController(std::unique_ptr<PowerHalConnector> connector) |
| 55 | : mHalConnector(std::move(connector)) {} |
| 56 | |
| 57 | void init(); |
| 58 | |
| 59 | PowerHalResult setBoost(Boost boost, int32_t durationMs) override; |
| 60 | PowerHalResult setMode(Mode mode, bool enabled) override; |
| 61 | |
| 62 | private: |
| 63 | std::mutex mConnectedHalMutex; |
| 64 | std::unique_ptr<PowerHalConnector> mHalConnector; |
| 65 | |
| 66 | // Shared pointers to keep global pointer and allow local copies to be used in different threads |
| 67 | std::shared_ptr<PowerHalWrapper> mConnectedHal GUARDED_BY(mConnectedHalMutex) = nullptr; |
| 68 | const std::shared_ptr<PowerHalWrapper> mDefaultHal = std::make_shared<EmptyPowerHalWrapper>(); |
| 69 | |
| 70 | std::shared_ptr<PowerHalWrapper> initHal(); |
| 71 | PowerHalResult processHalResult(PowerHalResult result, const char* functionName); |
| 72 | }; |
| 73 | |
| 74 | // ------------------------------------------------------------------------------------------------- |
| 75 | |
| 76 | }; // namespace android |
| 77 | |
| 78 | #endif // ANDROID_POWERHALCONTROLLER_H |