blob: d26d582c43e97b94aa39c81fb4231229943f8383 [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"
18#include <utils/Log.h>
19
20#include <android/hardware/power/1.1/IPower.h>
21#include <android/hardware/power/Boost.h>
22#include <android/hardware/power/IPower.h>
23#include <android/hardware/power/Mode.h>
24
25#include <powermanager/PowerHalController.h>
26#include <powermanager/PowerHalLoader.h>
27
28using android::hardware::power::Boost;
29using android::hardware::power::Mode;
30
31namespace android {
32
33// -------------------------------------------------------------------------------------------------
34
35std::unique_ptr<PowerHalWrapper> PowerHalConnector::connect() {
36 sp<IPowerAidl> halAidl = PowerHalLoader::loadAidl();
37 if (halAidl) {
38 return std::make_unique<AidlPowerHalWrapper>(halAidl);
39 }
40 sp<IPowerV1_0> halHidlV1_0 = PowerHalLoader::loadHidlV1_0();
41 sp<IPowerV1_1> halHidlV1_1 = PowerHalLoader::loadHidlV1_1();
42 if (halHidlV1_1) {
43 return std::make_unique<HidlPowerHalWrapperV1_1>(halHidlV1_0, halHidlV1_1);
44 }
45 if (halHidlV1_0) {
46 return std::make_unique<HidlPowerHalWrapperV1_0>(halHidlV1_0);
47 }
48 return nullptr;
49}
50
51void PowerHalConnector::reset() {
52 PowerHalLoader::unloadAll();
53}
54
55// -------------------------------------------------------------------------------------------------
56
57void PowerHalController::init() {
58 initHal();
59}
60
61// Check validity of current handle to the power HAL service, and create a new one if necessary.
62std::shared_ptr<PowerHalWrapper> PowerHalController::initHal() {
63 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
74// Check if a call to Power HAL function failed; if so, log the failure and invalidate the
75// current Power HAL handle.
76PowerHalResult PowerHalController::processHalResult(PowerHalResult result, const char* fnName) {
77 if (result == PowerHalResult::FAILED) {
78 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
87PowerHalResult PowerHalController::setBoost(Boost boost, int32_t durationMs) {
88 std::shared_ptr<PowerHalWrapper> handle = initHal();
89 auto result = handle->setBoost(boost, durationMs);
90 return processHalResult(result, "setBoost");
91}
92
93PowerHalResult PowerHalController::setMode(Mode mode, bool enabled) {
94 std::shared_ptr<PowerHalWrapper> handle = initHal();
95 auto result = handle->setMode(mode, enabled);
96 return processHalResult(result, "setMode");
97}
98
99}; // namespace android