blob: c50bc4a188375b173628ca0b1cba1c059c987107 [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
Xiang Wang74978e12023-05-19 20:39:30 -070056 virtual void init();
Lais Andrade3f7ecc52020-03-25 23:57:08 +000057
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;
Matt Buckleydb4192a2023-12-21 20:00:32 +000065 virtual HalResult<std::shared_ptr<aidl::android::hardware::power::IPowerHintSession>>
66 createHintSessionWithConfig(int32_t tgid, int32_t uid, const std::vector<int32_t>& threadIds,
67 int64_t durationNanos,
68 aidl::android::hardware::power::SessionTag tag,
69 aidl::android::hardware::power::SessionConfig* config) override;
Jimmy Shiu0b264bb2021-03-03 00:30:50 +080070 virtual HalResult<int64_t> getHintSessionPreferredRate() override;
Matt Buckleydb4192a2023-12-21 20:00:32 +000071 virtual HalResult<aidl::android::hardware::power::ChannelConfig> getSessionChannel(
72 int tgid, int uid) override;
73 virtual HalResult<void> closeSessionChannel(int tgid, int uid) override;
Lais Andrade3f7ecc52020-03-25 23:57:08 +000074
75private:
76 std::mutex mConnectedHalMutex;
Lais Andradeb59a9b52020-05-07 17:23:42 +010077 std::unique_ptr<HalConnector> mHalConnector;
Lais Andrade3f7ecc52020-03-25 23:57:08 +000078
Lais Andradeb59a9b52020-05-07 17:23:42 +010079 // Shared pointers to keep global pointer and allow local copies to be used in
80 // different threads
81 std::shared_ptr<HalWrapper> mConnectedHal GUARDED_BY(mConnectedHalMutex) = nullptr;
82 const std::shared_ptr<HalWrapper> mDefaultHal = std::make_shared<EmptyHalWrapper>();
Lais Andrade3f7ecc52020-03-25 23:57:08 +000083
Lais Andradeb59a9b52020-05-07 17:23:42 +010084 std::shared_ptr<HalWrapper> initHal();
Jimmy Shiu0b264bb2021-03-03 00:30:50 +080085 template <typename T>
Matt Buckleydb4192a2023-12-21 20:00:32 +000086 HalResult<T> processHalResult(HalResult<T>&& result, const char* functionName);
Lais Andrade3f7ecc52020-03-25 23:57:08 +000087};
88
89// -------------------------------------------------------------------------------------------------
90
Lais Andradeb59a9b52020-05-07 17:23:42 +010091}; // namespace power
92
Lais Andrade3f7ecc52020-03-25 23:57:08 +000093}; // namespace android
94
95#endif // ANDROID_POWERHALCONTROLLER_H