blob: bc178bce8ff0dc77683217847035f356bf10b637 [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#define LOG_TAG "PowerHalController"
Xiang Wang99f6f3c2023-05-22 13:12:16 -070018#include <aidl/android/hardware/power/Boost.h>
19#include <aidl/android/hardware/power/IPower.h>
20#include <aidl/android/hardware/power/IPowerHintSession.h>
21#include <aidl/android/hardware/power/Mode.h>
Lais Andrade3f7ecc52020-03-25 23:57:08 +000022#include <android/hardware/power/1.1/IPower.h>
Lais Andrade3f7ecc52020-03-25 23:57:08 +000023#include <powermanager/PowerHalController.h>
24#include <powermanager/PowerHalLoader.h>
Lais Andradeb59a9b52020-05-07 17:23:42 +010025#include <utils/Log.h>
Lais Andrade3f7ecc52020-03-25 23:57:08 +000026
Lais Andradeb59a9b52020-05-07 17:23:42 +010027using namespace android::hardware::power;
Lais Andrade3f7ecc52020-03-25 23:57:08 +000028
29namespace android {
30
Lais Andradeb59a9b52020-05-07 17:23:42 +010031namespace power {
32
Lais Andrade3f7ecc52020-03-25 23:57:08 +000033// -------------------------------------------------------------------------------------------------
34
Lais Andradeb59a9b52020-05-07 17:23:42 +010035std::unique_ptr<HalWrapper> HalConnector::connect() {
Xiang Wang99f6f3c2023-05-22 13:12:16 -070036 if (std::shared_ptr<aidl::android::hardware::power::IPower> halAidl =
37 PowerHalLoader::loadAidl()) {
Lais Andradeb59a9b52020-05-07 17:23:42 +010038 return std::make_unique<AidlHalWrapper>(halAidl);
Lais Andrade3f7ecc52020-03-25 23:57:08 +000039 }
Matt Buckleyc3894a42022-09-01 21:17:15 +000040 // If V1_0 isn't defined, none of them are
41 if (sp<V1_0::IPower> halHidlV1_0 = PowerHalLoader::loadHidlV1_0()) {
42 if (sp<V1_3::IPower> halHidlV1_3 = PowerHalLoader::loadHidlV1_3()) {
43 return std::make_unique<HidlHalWrapperV1_3>(halHidlV1_3);
44 }
45 if (sp<V1_2::IPower> halHidlV1_2 = PowerHalLoader::loadHidlV1_2()) {
46 return std::make_unique<HidlHalWrapperV1_2>(halHidlV1_2);
47 }
48 if (sp<V1_1::IPower> halHidlV1_1 = PowerHalLoader::loadHidlV1_1()) {
49 return std::make_unique<HidlHalWrapperV1_1>(halHidlV1_1);
50 }
Lais Andradeb59a9b52020-05-07 17:23:42 +010051 return std::make_unique<HidlHalWrapperV1_0>(halHidlV1_0);
Lais Andrade3f7ecc52020-03-25 23:57:08 +000052 }
53 return nullptr;
54}
55
Lais Andradeb59a9b52020-05-07 17:23:42 +010056void HalConnector::reset() {
Lais Andrade3f7ecc52020-03-25 23:57:08 +000057 PowerHalLoader::unloadAll();
58}
59
60// -------------------------------------------------------------------------------------------------
61
62void PowerHalController::init() {
63 initHal();
64}
65
Lais Andradeb59a9b52020-05-07 17:23:42 +010066// Check validity of current handle to the power HAL service, and create a new
67// one if necessary.
68std::shared_ptr<HalWrapper> PowerHalController::initHal() {
Lais Andrade3f7ecc52020-03-25 23:57:08 +000069 std::lock_guard<std::mutex> lock(mConnectedHalMutex);
70 if (mConnectedHal == nullptr) {
71 mConnectedHal = mHalConnector->connect();
72 if (mConnectedHal == nullptr) {
73 // Unable to connect to Power HAL service. Fallback to default.
74 return mDefaultHal;
75 }
76 }
77 return mConnectedHal;
78}
79
Lais Andradeb59a9b52020-05-07 17:23:42 +010080// Check if a call to Power HAL function failed; if so, log the failure and
81// invalidate the current Power HAL handle.
Jimmy Shiu0b264bb2021-03-03 00:30:50 +080082template <typename T>
Matt Buckleydb4192a2023-12-21 20:00:32 +000083HalResult<T> PowerHalController::processHalResult(HalResult<T>&& result, const char* fnName) {
Jimmy Shiu0b264bb2021-03-03 00:30:50 +080084 if (result.isFailed()) {
85 ALOGE("%s failed: %s", fnName, result.errorMessage());
Lais Andrade3f7ecc52020-03-25 23:57:08 +000086 std::lock_guard<std::mutex> lock(mConnectedHalMutex);
87 // Drop Power HAL handle. This will force future api calls to reconnect.
88 mConnectedHal = nullptr;
89 mHalConnector->reset();
90 }
Matt Buckleyf4e95dc2024-01-24 22:45:57 +000091 return std::move(result);
Lais Andrade3f7ecc52020-03-25 23:57:08 +000092}
93
Xiang Wang99f6f3c2023-05-22 13:12:16 -070094HalResult<void> PowerHalController::setBoost(aidl::android::hardware::power::Boost boost,
95 int32_t durationMs) {
Lais Andradeb59a9b52020-05-07 17:23:42 +010096 std::shared_ptr<HalWrapper> handle = initHal();
Matt Buckleydb4192a2023-12-21 20:00:32 +000097 return processHalResult(handle->setBoost(boost, durationMs), "setBoost");
Lais Andrade3f7ecc52020-03-25 23:57:08 +000098}
99
Xiang Wang99f6f3c2023-05-22 13:12:16 -0700100HalResult<void> PowerHalController::setMode(aidl::android::hardware::power::Mode mode,
101 bool enabled) {
Lais Andradeb59a9b52020-05-07 17:23:42 +0100102 std::shared_ptr<HalWrapper> handle = initHal();
Matt Buckleydb4192a2023-12-21 20:00:32 +0000103 return processHalResult(handle->setMode(mode, enabled), "setMode");
Lais Andrade3f7ecc52020-03-25 23:57:08 +0000104}
105
Xiang Wang99f6f3c2023-05-22 13:12:16 -0700106HalResult<std::shared_ptr<aidl::android::hardware::power::IPowerHintSession>>
107PowerHalController::createHintSession(int32_t tgid, int32_t uid,
108 const std::vector<int32_t>& threadIds,
109 int64_t durationNanos) {
Jimmy Shiu0b264bb2021-03-03 00:30:50 +0800110 std::shared_ptr<HalWrapper> handle = initHal();
Matt Buckleydb4192a2023-12-21 20:00:32 +0000111 return processHalResult(handle->createHintSession(tgid, uid, threadIds, durationNanos),
112 "createHintSession");
113}
114
115HalResult<std::shared_ptr<aidl::android::hardware::power::IPowerHintSession>>
116PowerHalController::createHintSessionWithConfig(
117 int32_t tgid, int32_t uid, const std::vector<int32_t>& threadIds, int64_t durationNanos,
118 aidl::android::hardware::power::SessionTag tag,
119 aidl::android::hardware::power::SessionConfig* config) {
120 std::shared_ptr<HalWrapper> handle = initHal();
121 return processHalResult(handle->createHintSessionWithConfig(tgid, uid, threadIds, durationNanos,
122 tag, config),
123 "createHintSessionWithConfig");
Jimmy Shiu0b264bb2021-03-03 00:30:50 +0800124}
125
126HalResult<int64_t> PowerHalController::getHintSessionPreferredRate() {
127 std::shared_ptr<HalWrapper> handle = initHal();
Matt Buckleydb4192a2023-12-21 20:00:32 +0000128 return processHalResult(handle->getHintSessionPreferredRate(), "getHintSessionPreferredRate");
129}
130
131HalResult<aidl::android::hardware::power::ChannelConfig> PowerHalController::getSessionChannel(
132 int tgid, int uid) {
133 std::shared_ptr<HalWrapper> handle = initHal();
134 return processHalResult(handle->getSessionChannel(tgid, uid), "getSessionChannel");
135}
136
137HalResult<void> PowerHalController::closeSessionChannel(int tgid, int uid) {
138 std::shared_ptr<HalWrapper> handle = initHal();
139 return processHalResult(handle->closeSessionChannel(tgid, uid), "closeSessionChannel");
Jimmy Shiu0b264bb2021-03-03 00:30:50 +0800140}
141
Lais Andradeb59a9b52020-05-07 17:23:42 +0100142} // namespace power
143
144} // namespace android