blob: 8c225d5d02319ab59db713dde975f98386f4dd01 [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"
Lais Andrade3f7ecc52020-03-25 23:57:08 +000018#include <android/hardware/power/1.1/IPower.h>
19#include <android/hardware/power/Boost.h>
20#include <android/hardware/power/IPower.h>
Jimmy Shiu0b264bb2021-03-03 00:30:50 +080021#include <android/hardware/power/IPowerHintSession.h>
Lais Andrade3f7ecc52020-03-25 23:57:08 +000022#include <android/hardware/power/Mode.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() {
36 sp<IPower> halAidl = PowerHalLoader::loadAidl();
Lais Andrade3f7ecc52020-03-25 23:57:08 +000037 if (halAidl) {
Lais Andradeb59a9b52020-05-07 17:23:42 +010038 return std::make_unique<AidlHalWrapper>(halAidl);
Lais Andrade3f7ecc52020-03-25 23:57:08 +000039 }
Lais Andradeb59a9b52020-05-07 17:23:42 +010040 sp<V1_0::IPower> halHidlV1_0 = PowerHalLoader::loadHidlV1_0();
41 sp<V1_1::IPower> halHidlV1_1 = PowerHalLoader::loadHidlV1_1();
Lais Andrade3f7ecc52020-03-25 23:57:08 +000042 if (halHidlV1_1) {
Lais Andradeb59a9b52020-05-07 17:23:42 +010043 return std::make_unique<HidlHalWrapperV1_1>(halHidlV1_0, halHidlV1_1);
Lais Andrade3f7ecc52020-03-25 23:57:08 +000044 }
45 if (halHidlV1_0) {
Lais Andradeb59a9b52020-05-07 17:23:42 +010046 return std::make_unique<HidlHalWrapperV1_0>(halHidlV1_0);
Lais Andrade3f7ecc52020-03-25 23:57:08 +000047 }
48 return nullptr;
49}
50
Lais Andradeb59a9b52020-05-07 17:23:42 +010051void HalConnector::reset() {
Lais Andrade3f7ecc52020-03-25 23:57:08 +000052 PowerHalLoader::unloadAll();
53}
54
55// -------------------------------------------------------------------------------------------------
56
57void PowerHalController::init() {
58 initHal();
59}
60
Lais Andradeb59a9b52020-05-07 17:23:42 +010061// Check validity of current handle to the power HAL service, and create a new
62// one if necessary.
63std::shared_ptr<HalWrapper> PowerHalController::initHal() {
Lais Andrade3f7ecc52020-03-25 23:57:08 +000064 std::lock_guard<std::mutex> lock(mConnectedHalMutex);
65 if (mConnectedHal == nullptr) {
66 mConnectedHal = mHalConnector->connect();
67 if (mConnectedHal == nullptr) {
68 // Unable to connect to Power HAL service. Fallback to default.
69 return mDefaultHal;
70 }
71 }
72 return mConnectedHal;
73}
74
Lais Andradeb59a9b52020-05-07 17:23:42 +010075// Check if a call to Power HAL function failed; if so, log the failure and
76// invalidate the current Power HAL handle.
Jimmy Shiu0b264bb2021-03-03 00:30:50 +080077template <typename T>
78HalResult<T> PowerHalController::processHalResult(HalResult<T> result, const char* fnName) {
79 if (result.isFailed()) {
80 ALOGE("%s failed: %s", fnName, result.errorMessage());
Lais Andrade3f7ecc52020-03-25 23:57:08 +000081 std::lock_guard<std::mutex> lock(mConnectedHalMutex);
82 // Drop Power HAL handle. This will force future api calls to reconnect.
83 mConnectedHal = nullptr;
84 mHalConnector->reset();
85 }
86 return result;
87}
88
Jimmy Shiu0b264bb2021-03-03 00:30:50 +080089HalResult<void> PowerHalController::setBoost(Boost boost, int32_t durationMs) {
Lais Andradeb59a9b52020-05-07 17:23:42 +010090 std::shared_ptr<HalWrapper> handle = initHal();
Lais Andrade3f7ecc52020-03-25 23:57:08 +000091 auto result = handle->setBoost(boost, durationMs);
92 return processHalResult(result, "setBoost");
93}
94
Jimmy Shiu0b264bb2021-03-03 00:30:50 +080095HalResult<void> PowerHalController::setMode(Mode mode, bool enabled) {
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->setMode(mode, enabled);
98 return processHalResult(result, "setMode");
99}
100
Jimmy Shiu0b264bb2021-03-03 00:30:50 +0800101HalResult<sp<IPowerHintSession>> PowerHalController::createHintSession(
102 int32_t tgid, int32_t uid, const std::vector<int32_t>& threadIds, int64_t durationNanos) {
103 std::shared_ptr<HalWrapper> handle = initHal();
104 auto result = handle->createHintSession(tgid, uid, threadIds, durationNanos);
105 return processHalResult(result, "createHintSession");
106}
107
108HalResult<int64_t> PowerHalController::getHintSessionPreferredRate() {
109 std::shared_ptr<HalWrapper> handle = initHal();
110 auto result = handle->getHintSessionPreferredRate();
111 return processHalResult(result, "getHintSessionPreferredRate");
112}
113
Lais Andradeb59a9b52020-05-07 17:23:42 +0100114} // namespace power
115
116} // namespace android