blob: 9a23c848c95ad1d67257f98904bffa609c040574 [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>
83HalResult<T> PowerHalController::processHalResult(HalResult<T> result, const char* fnName) {
84 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 }
91 return result;
92}
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();
Lais Andrade3f7ecc52020-03-25 23:57:08 +000097 auto result = handle->setBoost(boost, durationMs);
98 return processHalResult(result, "setBoost");
99}
100
Xiang Wang99f6f3c2023-05-22 13:12:16 -0700101HalResult<void> PowerHalController::setMode(aidl::android::hardware::power::Mode mode,
102 bool enabled) {
Lais Andradeb59a9b52020-05-07 17:23:42 +0100103 std::shared_ptr<HalWrapper> handle = initHal();
Lais Andrade3f7ecc52020-03-25 23:57:08 +0000104 auto result = handle->setMode(mode, enabled);
105 return processHalResult(result, "setMode");
106}
107
Xiang Wang99f6f3c2023-05-22 13:12:16 -0700108HalResult<std::shared_ptr<aidl::android::hardware::power::IPowerHintSession>>
109PowerHalController::createHintSession(int32_t tgid, int32_t uid,
110 const std::vector<int32_t>& threadIds,
111 int64_t durationNanos) {
Jimmy Shiu0b264bb2021-03-03 00:30:50 +0800112 std::shared_ptr<HalWrapper> handle = initHal();
113 auto result = handle->createHintSession(tgid, uid, threadIds, durationNanos);
114 return processHalResult(result, "createHintSession");
115}
116
117HalResult<int64_t> PowerHalController::getHintSessionPreferredRate() {
118 std::shared_ptr<HalWrapper> handle = initHal();
119 auto result = handle->getHintSessionPreferredRate();
120 return processHalResult(result, "getHintSessionPreferredRate");
121}
122
Lais Andradeb59a9b52020-05-07 17:23:42 +0100123} // namespace power
124
125} // namespace android