blob: 7e0bd5bedc5f55e5360ff927988df6ff1cd2e782 [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>
Matt Buckley6c18e6d2024-02-07 23:39:50 +000026#include <powermanager/PowerHintSessionWrapper.h>
Lais Andrade3f7ecc52020-03-25 23:57:08 +000027
Lais Andrade3f7ecc52020-03-25 23:57:08 +000028namespace android {
29
Lais Andradeb59a9b52020-05-07 17:23:42 +010030namespace power {
31
Lais Andrade3f7ecc52020-03-25 23:57:08 +000032// -------------------------------------------------------------------------------------------------
33
34// Connects to underlying Power HAL handles.
Lais Andradeb59a9b52020-05-07 17:23:42 +010035class HalConnector {
Lais Andrade3f7ecc52020-03-25 23:57:08 +000036public:
Lais Andradeb59a9b52020-05-07 17:23:42 +010037 HalConnector() = default;
38 virtual ~HalConnector() = default;
Lais Andrade3f7ecc52020-03-25 23:57:08 +000039
Lais Andradeb59a9b52020-05-07 17:23:42 +010040 virtual std::unique_ptr<HalWrapper> connect();
Lais Andrade3f7ecc52020-03-25 23:57:08 +000041 virtual void reset();
Matt Buckley6c18e6d2024-02-07 23:39:50 +000042 virtual int32_t getAidlVersion();
Lais Andrade3f7ecc52020-03-25 23:57:08 +000043};
44
45// -------------------------------------------------------------------------------------------------
46
47// Controller for Power HAL handle.
Lais Andradeb59a9b52020-05-07 17:23:42 +010048// 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.
51class PowerHalController : public HalWrapper {
Lais Andrade3f7ecc52020-03-25 23:57:08 +000052public:
Lais Andradeb59a9b52020-05-07 17:23:42 +010053 PowerHalController() : PowerHalController(std::make_unique<HalConnector>()) {}
54 explicit PowerHalController(std::unique_ptr<HalConnector> connector)
55 : mHalConnector(std::move(connector)) {}
56 virtual ~PowerHalController() = default;
Lais Andrade3f7ecc52020-03-25 23:57:08 +000057
Xiang Wang74978e12023-05-19 20:39:30 -070058 virtual void init();
Lais Andrade3f7ecc52020-03-25 23:57:08 +000059
Xiang Wang99f6f3c2023-05-22 13:12:16 -070060 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;
Matt Buckley6c18e6d2024-02-07 23:39:50 +000064 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;
Jimmy Shiu0b264bb2021-03-03 00:30:50 +080071 virtual HalResult<int64_t> getHintSessionPreferredRate() override;
Matt Buckleydb4192a2023-12-21 20:00:32 +000072 virtual HalResult<aidl::android::hardware::power::ChannelConfig> getSessionChannel(
73 int tgid, int uid) override;
74 virtual HalResult<void> closeSessionChannel(int tgid, int uid) override;
Lais Andrade3f7ecc52020-03-25 23:57:08 +000075
76private:
77 std::mutex mConnectedHalMutex;
Lais Andradeb59a9b52020-05-07 17:23:42 +010078 std::unique_ptr<HalConnector> mHalConnector;
Lais Andrade3f7ecc52020-03-25 23:57:08 +000079
Lais Andradeb59a9b52020-05-07 17:23:42 +010080 // 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>();
Lais Andrade3f7ecc52020-03-25 23:57:08 +000084
Lais Andradeb59a9b52020-05-07 17:23:42 +010085 std::shared_ptr<HalWrapper> initHal();
Jimmy Shiu0b264bb2021-03-03 00:30:50 +080086 template <typename T>
Matt Buckleydb4192a2023-12-21 20:00:32 +000087 HalResult<T> processHalResult(HalResult<T>&& result, const char* functionName);
Lais Andrade3f7ecc52020-03-25 23:57:08 +000088};
89
90// -------------------------------------------------------------------------------------------------
91
Lais Andradeb59a9b52020-05-07 17:23:42 +010092}; // namespace power
93
Lais Andrade3f7ecc52020-03-25 23:57:08 +000094}; // namespace android
95
96#endif // ANDROID_POWERHALCONTROLLER_H