blob: 178f545b2e0d44ffa2b7cf7d6484b120f7321f00 [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>
21#include <android/hardware/power/Mode.h>
Lais Andrade3f7ecc52020-03-25 23:57:08 +000022#include <powermanager/PowerHalController.h>
23#include <powermanager/PowerHalLoader.h>
Lais Andradeb59a9b52020-05-07 17:23:42 +010024#include <utils/Log.h>
Lais Andrade3f7ecc52020-03-25 23:57:08 +000025
Lais Andradeb59a9b52020-05-07 17:23:42 +010026using namespace android::hardware::power;
Lais Andrade3f7ecc52020-03-25 23:57:08 +000027
28namespace android {
29
Lais Andradeb59a9b52020-05-07 17:23:42 +010030namespace power {
31
Lais Andrade3f7ecc52020-03-25 23:57:08 +000032// -------------------------------------------------------------------------------------------------
33
Lais Andradeb59a9b52020-05-07 17:23:42 +010034std::unique_ptr<HalWrapper> HalConnector::connect() {
35 sp<IPower> halAidl = PowerHalLoader::loadAidl();
Lais Andrade3f7ecc52020-03-25 23:57:08 +000036 if (halAidl) {
Lais Andradeb59a9b52020-05-07 17:23:42 +010037 return std::make_unique<AidlHalWrapper>(halAidl);
Lais Andrade3f7ecc52020-03-25 23:57:08 +000038 }
Lais Andradeb59a9b52020-05-07 17:23:42 +010039 sp<V1_0::IPower> halHidlV1_0 = PowerHalLoader::loadHidlV1_0();
40 sp<V1_1::IPower> halHidlV1_1 = PowerHalLoader::loadHidlV1_1();
Lais Andrade3f7ecc52020-03-25 23:57:08 +000041 if (halHidlV1_1) {
Lais Andradeb59a9b52020-05-07 17:23:42 +010042 return std::make_unique<HidlHalWrapperV1_1>(halHidlV1_0, halHidlV1_1);
Lais Andrade3f7ecc52020-03-25 23:57:08 +000043 }
44 if (halHidlV1_0) {
Lais Andradeb59a9b52020-05-07 17:23:42 +010045 return std::make_unique<HidlHalWrapperV1_0>(halHidlV1_0);
Lais Andrade3f7ecc52020-03-25 23:57:08 +000046 }
47 return nullptr;
48}
49
Lais Andradeb59a9b52020-05-07 17:23:42 +010050void HalConnector::reset() {
Lais Andrade3f7ecc52020-03-25 23:57:08 +000051 PowerHalLoader::unloadAll();
52}
53
54// -------------------------------------------------------------------------------------------------
55
56void PowerHalController::init() {
57 initHal();
58}
59
Lais Andradeb59a9b52020-05-07 17:23:42 +010060// Check validity of current handle to the power HAL service, and create a new
61// one if necessary.
62std::shared_ptr<HalWrapper> PowerHalController::initHal() {
Lais Andrade3f7ecc52020-03-25 23:57:08 +000063 std::lock_guard<std::mutex> lock(mConnectedHalMutex);
64 if (mConnectedHal == nullptr) {
65 mConnectedHal = mHalConnector->connect();
66 if (mConnectedHal == nullptr) {
67 // Unable to connect to Power HAL service. Fallback to default.
68 return mDefaultHal;
69 }
70 }
71 return mConnectedHal;
72}
73
Lais Andradeb59a9b52020-05-07 17:23:42 +010074// Check if a call to Power HAL function failed; if so, log the failure and
75// invalidate the current Power HAL handle.
76HalResult PowerHalController::processHalResult(HalResult result, const char* fnName) {
77 if (result == HalResult::FAILED) {
Lais Andrade3f7ecc52020-03-25 23:57:08 +000078 ALOGE("%s() failed: power HAL service not available.", fnName);
79 std::lock_guard<std::mutex> lock(mConnectedHalMutex);
80 // Drop Power HAL handle. This will force future api calls to reconnect.
81 mConnectedHal = nullptr;
82 mHalConnector->reset();
83 }
84 return result;
85}
86
Lais Andradeb59a9b52020-05-07 17:23:42 +010087HalResult PowerHalController::setBoost(Boost boost, int32_t durationMs) {
88 std::shared_ptr<HalWrapper> handle = initHal();
Lais Andrade3f7ecc52020-03-25 23:57:08 +000089 auto result = handle->setBoost(boost, durationMs);
90 return processHalResult(result, "setBoost");
91}
92
Lais Andradeb59a9b52020-05-07 17:23:42 +010093HalResult PowerHalController::setMode(Mode mode, bool enabled) {
94 std::shared_ptr<HalWrapper> handle = initHal();
Lais Andrade3f7ecc52020-03-25 23:57:08 +000095 auto result = handle->setMode(mode, enabled);
96 return processHalResult(result, "setMode");
97}
98
Lais Andradeb59a9b52020-05-07 17:23:42 +010099} // namespace power
100
101} // namespace android