1 /*
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 <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/IPowerHintSession.h>
22 #include <android/hardware/power/Mode.h>
23 #include <powermanager/PowerHalController.h>
24 #include <powermanager/PowerHalLoader.h>
25 #include <utils/Log.h>
26
27 using namespace android::hardware::power;
28
29 namespace android {
30
31 namespace power {
32
33 // -------------------------------------------------------------------------------------------------
34
connect()35 std::unique_ptr<HalWrapper> HalConnector::connect() {
36 sp<IPower> halAidl = PowerHalLoader::loadAidl();
37 if (halAidl) {
38 return std::make_unique<AidlHalWrapper>(halAidl);
39 }
40 sp<V1_0::IPower> halHidlV1_0 = PowerHalLoader::loadHidlV1_0();
41 sp<V1_1::IPower> halHidlV1_1 = PowerHalLoader::loadHidlV1_1();
42 if (halHidlV1_1) {
43 return std::make_unique<HidlHalWrapperV1_1>(halHidlV1_0, halHidlV1_1);
44 }
45 if (halHidlV1_0) {
46 return std::make_unique<HidlHalWrapperV1_0>(halHidlV1_0);
47 }
48 return nullptr;
49 }
50
reset()51 void HalConnector::reset() {
52 PowerHalLoader::unloadAll();
53 }
54
55 // -------------------------------------------------------------------------------------------------
56
init()57 void PowerHalController::init() {
58 initHal();
59 }
60
61 // Check validity of current handle to the power HAL service, and create a new
62 // one if necessary.
initHal()63 std::shared_ptr<HalWrapper> PowerHalController::initHal() {
64 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
75 // Check if a call to Power HAL function failed; if so, log the failure and
76 // invalidate the current Power HAL handle.
77 template <typename T>
processHalResult(HalResult<T> result,const char * fnName)78 HalResult<T> PowerHalController::processHalResult(HalResult<T> result, const char* fnName) {
79 if (result.isFailed()) {
80 ALOGE("%s failed: %s", fnName, result.errorMessage());
81 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
setBoost(Boost boost,int32_t durationMs)89 HalResult<void> PowerHalController::setBoost(Boost boost, int32_t durationMs) {
90 std::shared_ptr<HalWrapper> handle = initHal();
91 auto result = handle->setBoost(boost, durationMs);
92 return processHalResult(result, "setBoost");
93 }
94
setMode(Mode mode,bool enabled)95 HalResult<void> PowerHalController::setMode(Mode mode, bool enabled) {
96 std::shared_ptr<HalWrapper> handle = initHal();
97 auto result = handle->setMode(mode, enabled);
98 return processHalResult(result, "setMode");
99 }
100
createHintSession(int32_t tgid,int32_t uid,const std::vector<int32_t> & threadIds,int64_t durationNanos)101 HalResult<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
getHintSessionPreferredRate()108 HalResult<int64_t> PowerHalController::getHintSessionPreferredRate() {
109 std::shared_ptr<HalWrapper> handle = initHal();
110 auto result = handle->getHintSessionPreferredRate();
111 return processHalResult(result, "getHintSessionPreferredRate");
112 }
113
114 } // namespace power
115
116 } // namespace android
117