blob: 32ceb2805be5ad0825d2f8829960c229b81eb62c [file] [log] [blame]
Lais Andrade3f7ecc52020-03-25 23:57:08 +00001/*
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
Xiang Wang99f6f3c2023-05-22 13:12:16 -070020#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>
Lais Andrade3f7ecc52020-03-25 23:57:08 +000024#include <android-base/thread_annotations.h>
Lais Andrade3f7ecc52020-03-25 23:57:08 +000025#include <powermanager/PowerHalWrapper.h>
26
Lais Andrade3f7ecc52020-03-25 23:57:08 +000027namespace android {
28
Lais Andradeb59a9b52020-05-07 17:23:42 +010029namespace power {
30
Lais Andrade3f7ecc52020-03-25 23:57:08 +000031// -------------------------------------------------------------------------------------------------
32
33// Connects to underlying Power HAL handles.
Lais Andradeb59a9b52020-05-07 17:23:42 +010034class HalConnector {
Lais Andrade3f7ecc52020-03-25 23:57:08 +000035public:
Lais Andradeb59a9b52020-05-07 17:23:42 +010036 HalConnector() = default;
37 virtual ~HalConnector() = default;
Lais Andrade3f7ecc52020-03-25 23:57:08 +000038
Lais Andradeb59a9b52020-05-07 17:23:42 +010039 virtual std::unique_ptr<HalWrapper> connect();
Lais Andrade3f7ecc52020-03-25 23:57:08 +000040 virtual void reset();
41};
42
43// -------------------------------------------------------------------------------------------------
44
45// Controller for Power HAL handle.
Lais Andradeb59a9b52020-05-07 17:23:42 +010046// This relies on HalConnector to connect to the underlying Power HAL
47// service and reconnects to it after each failed api call. This also ensures
48// connecting to the service is thread-safe.
49class PowerHalController : public HalWrapper {
Lais Andrade3f7ecc52020-03-25 23:57:08 +000050public:
Lais Andradeb59a9b52020-05-07 17:23:42 +010051 PowerHalController() : PowerHalController(std::make_unique<HalConnector>()) {}
52 explicit PowerHalController(std::unique_ptr<HalConnector> connector)
53 : mHalConnector(std::move(connector)) {}
54 virtual ~PowerHalController() = default;
Lais Andrade3f7ecc52020-03-25 23:57:08 +000055
56 void init();
57
Xiang Wang99f6f3c2023-05-22 13:12:16 -070058 virtual HalResult<void> setBoost(aidl::android::hardware::power::Boost boost,
59 int32_t durationMs) override;
60 virtual HalResult<void> setMode(aidl::android::hardware::power::Mode mode,
61 bool enabled) override;
62 virtual HalResult<std::shared_ptr<aidl::android::hardware::power::IPowerHintSession>>
63 createHintSession(int32_t tgid, int32_t uid, const std::vector<int32_t>& threadIds,
64 int64_t durationNanos) override;
Jimmy Shiu0b264bb2021-03-03 00:30:50 +080065 virtual HalResult<int64_t> getHintSessionPreferredRate() override;
Lais Andrade3f7ecc52020-03-25 23:57:08 +000066
67private:
68 std::mutex mConnectedHalMutex;
Lais Andradeb59a9b52020-05-07 17:23:42 +010069 std::unique_ptr<HalConnector> mHalConnector;
Lais Andrade3f7ecc52020-03-25 23:57:08 +000070
Lais Andradeb59a9b52020-05-07 17:23:42 +010071 // Shared pointers to keep global pointer and allow local copies to be used in
72 // different threads
73 std::shared_ptr<HalWrapper> mConnectedHal GUARDED_BY(mConnectedHalMutex) = nullptr;
74 const std::shared_ptr<HalWrapper> mDefaultHal = std::make_shared<EmptyHalWrapper>();
Lais Andrade3f7ecc52020-03-25 23:57:08 +000075
Lais Andradeb59a9b52020-05-07 17:23:42 +010076 std::shared_ptr<HalWrapper> initHal();
Jimmy Shiu0b264bb2021-03-03 00:30:50 +080077 template <typename T>
78 HalResult<T> processHalResult(HalResult<T> result, const char* functionName);
Lais Andrade3f7ecc52020-03-25 23:57:08 +000079};
80
81// -------------------------------------------------------------------------------------------------
82
Lais Andradeb59a9b52020-05-07 17:23:42 +010083}; // namespace power
84
Lais Andrade3f7ecc52020-03-25 23:57:08 +000085}; // namespace android
86
87#endif // ANDROID_POWERHALCONTROLLER_H