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 <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>
22 #include <android/hardware/power/1.1/IPower.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 if (std::shared_ptr<aidl::android::hardware::power::IPower> halAidl =
37 PowerHalLoader::loadAidl()) {
38 return std::make_unique<AidlHalWrapper>(halAidl);
39 }
40 // 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 }
51 return std::make_unique<HidlHalWrapperV1_0>(halHidlV1_0);
52 }
53 return nullptr;
54 }
55
reset()56 void HalConnector::reset() {
57 PowerHalLoader::unloadAll();
58 }
59
getAidlVersion()60 int32_t HalConnector::getAidlVersion() {
61 return PowerHalLoader::getAidlVersion();
62 }
63
64 // -------------------------------------------------------------------------------------------------
65
init()66 void PowerHalController::init() {
67 initHal();
68 }
69
70 // Check validity of current handle to the power HAL service, and create a new
71 // one if necessary.
initHal()72 std::shared_ptr<HalWrapper> PowerHalController::initHal() {
73 std::lock_guard<std::mutex> lock(mConnectedHalMutex);
74 if (mConnectedHal == nullptr) {
75 mConnectedHal = mHalConnector->connect();
76 if (mConnectedHal == nullptr) {
77 // Unable to connect to Power HAL service. Fallback to default.
78 return mDefaultHal;
79 }
80 }
81 return mConnectedHal;
82 }
83
84 // Using statement expression macro instead of a method lets the static be
85 // scoped to the outer method while dodging the need for a support lookup table
86 // This only works for AIDL methods that do not vary supported/unsupported depending
87 // on their arguments (not setBoost, setMode) which do their own support checks
88 #define CACHE_SUPPORT(version, method) \
89 ({ \
90 static bool support = mHalConnector->getAidlVersion() >= version; \
91 !support ? decltype(method)::unsupported() : ({ \
92 auto result = method; \
93 if (result.isUnsupported()) { \
94 support = false; \
95 } \
96 std::move(result); \
97 }); \
98 })
99
100 // Check if a call to Power HAL function failed; if so, log the failure and
101 // invalidate the current Power HAL handle.
102 template <typename T>
processHalResult(HalResult<T> && result,const char * fnName)103 HalResult<T> PowerHalController::processHalResult(HalResult<T>&& result, const char* fnName) {
104 if (result.isFailed()) {
105 ALOGE("%s failed: %s", fnName, result.errorMessage());
106 std::lock_guard<std::mutex> lock(mConnectedHalMutex);
107 // Drop Power HAL handle. This will force future api calls to reconnect.
108 mConnectedHal = nullptr;
109 mHalConnector->reset();
110 }
111 return std::move(result);
112 }
113
setBoost(aidl::android::hardware::power::Boost boost,int32_t durationMs)114 HalResult<void> PowerHalController::setBoost(aidl::android::hardware::power::Boost boost,
115 int32_t durationMs) {
116 std::shared_ptr<HalWrapper> handle = initHal();
117 return processHalResult(handle->setBoost(boost, durationMs), "setBoost");
118 }
119
setMode(aidl::android::hardware::power::Mode mode,bool enabled)120 HalResult<void> PowerHalController::setMode(aidl::android::hardware::power::Mode mode,
121 bool enabled) {
122 std::shared_ptr<HalWrapper> handle = initHal();
123 return processHalResult(handle->setMode(mode, enabled), "setMode");
124 }
125
126 // Aidl-only methods
127
createHintSession(int32_t tgid,int32_t uid,const std::vector<int32_t> & threadIds,int64_t durationNanos)128 HalResult<std::shared_ptr<PowerHintSessionWrapper>> PowerHalController::createHintSession(
129 int32_t tgid, int32_t uid, const std::vector<int32_t>& threadIds, int64_t durationNanos) {
130 std::shared_ptr<HalWrapper> handle = initHal();
131 return CACHE_SUPPORT(2,
132 processHalResult(handle->createHintSession(tgid, uid, threadIds,
133 durationNanos),
134 "createHintSession"));
135 }
136
createHintSessionWithConfig(int32_t tgid,int32_t uid,const std::vector<int32_t> & threadIds,int64_t durationNanos,aidl::android::hardware::power::SessionTag tag,aidl::android::hardware::power::SessionConfig * config)137 HalResult<std::shared_ptr<PowerHintSessionWrapper>> PowerHalController::createHintSessionWithConfig(
138 int32_t tgid, int32_t uid, const std::vector<int32_t>& threadIds, int64_t durationNanos,
139 aidl::android::hardware::power::SessionTag tag,
140 aidl::android::hardware::power::SessionConfig* config) {
141 std::shared_ptr<HalWrapper> handle = initHal();
142 return CACHE_SUPPORT(5,
143 processHalResult(handle->createHintSessionWithConfig(tgid, uid, threadIds,
144 durationNanos, tag,
145 config),
146 "createHintSessionWithConfig"));
147 }
148
getHintSessionPreferredRate()149 HalResult<int64_t> PowerHalController::getHintSessionPreferredRate() {
150 std::shared_ptr<HalWrapper> handle = initHal();
151 return CACHE_SUPPORT(2,
152 processHalResult(handle->getHintSessionPreferredRate(),
153 "getHintSessionPreferredRate"));
154 }
155
getSessionChannel(int tgid,int uid)156 HalResult<aidl::android::hardware::power::ChannelConfig> PowerHalController::getSessionChannel(
157 int tgid, int uid) {
158 std::shared_ptr<HalWrapper> handle = initHal();
159 return CACHE_SUPPORT(5,
160 processHalResult(handle->getSessionChannel(tgid, uid),
161 "getSessionChannel"));
162 }
163
closeSessionChannel(int tgid,int uid)164 HalResult<void> PowerHalController::closeSessionChannel(int tgid, int uid) {
165 std::shared_ptr<HalWrapper> handle = initHal();
166 return CACHE_SUPPORT(5,
167 processHalResult(handle->closeSessionChannel(tgid, uid),
168 "closeSessionChannel"));
169 }
170
171 } // namespace power
172
173 } // namespace android
174