• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     if (sp<IPower> halAidl = PowerHalLoader::loadAidl()) {
37         return std::make_unique<AidlHalWrapper>(halAidl);
38     }
39     // If V1_0 isn't defined, none of them are
40     if (sp<V1_0::IPower> halHidlV1_0 = PowerHalLoader::loadHidlV1_0()) {
41         if (sp<V1_3::IPower> halHidlV1_3 = PowerHalLoader::loadHidlV1_3()) {
42             return std::make_unique<HidlHalWrapperV1_3>(halHidlV1_3);
43         }
44         if (sp<V1_2::IPower> halHidlV1_2 = PowerHalLoader::loadHidlV1_2()) {
45             return std::make_unique<HidlHalWrapperV1_2>(halHidlV1_2);
46         }
47         if (sp<V1_1::IPower> halHidlV1_1 = PowerHalLoader::loadHidlV1_1()) {
48             return std::make_unique<HidlHalWrapperV1_1>(halHidlV1_1);
49         }
50         return std::make_unique<HidlHalWrapperV1_0>(halHidlV1_0);
51     }
52     return nullptr;
53 }
54 
reset()55 void HalConnector::reset() {
56     PowerHalLoader::unloadAll();
57 }
58 
59 // -------------------------------------------------------------------------------------------------
60 
init()61 void PowerHalController::init() {
62     initHal();
63 }
64 
65 // Check validity of current handle to the power HAL service, and create a new
66 // one if necessary.
initHal()67 std::shared_ptr<HalWrapper> PowerHalController::initHal() {
68     std::lock_guard<std::mutex> lock(mConnectedHalMutex);
69     if (mConnectedHal == nullptr) {
70         mConnectedHal = mHalConnector->connect();
71         if (mConnectedHal == nullptr) {
72             // Unable to connect to Power HAL service. Fallback to default.
73             return mDefaultHal;
74         }
75     }
76     return mConnectedHal;
77 }
78 
79 // Check if a call to Power HAL function failed; if so, log the failure and
80 // invalidate the current Power HAL handle.
81 template <typename T>
processHalResult(HalResult<T> result,const char * fnName)82 HalResult<T> PowerHalController::processHalResult(HalResult<T> result, const char* fnName) {
83     if (result.isFailed()) {
84         ALOGE("%s failed: %s", fnName, result.errorMessage());
85         std::lock_guard<std::mutex> lock(mConnectedHalMutex);
86         // Drop Power HAL handle. This will force future api calls to reconnect.
87         mConnectedHal = nullptr;
88         mHalConnector->reset();
89     }
90     return result;
91 }
92 
setBoost(Boost boost,int32_t durationMs)93 HalResult<void> PowerHalController::setBoost(Boost boost, int32_t durationMs) {
94     std::shared_ptr<HalWrapper> handle = initHal();
95     auto result = handle->setBoost(boost, durationMs);
96     return processHalResult(result, "setBoost");
97 }
98 
setMode(Mode mode,bool enabled)99 HalResult<void> PowerHalController::setMode(Mode mode, bool enabled) {
100     std::shared_ptr<HalWrapper> handle = initHal();
101     auto result = handle->setMode(mode, enabled);
102     return processHalResult(result, "setMode");
103 }
104 
createHintSession(int32_t tgid,int32_t uid,const std::vector<int32_t> & threadIds,int64_t durationNanos)105 HalResult<sp<IPowerHintSession>> PowerHalController::createHintSession(
106         int32_t tgid, int32_t uid, const std::vector<int32_t>& threadIds, int64_t durationNanos) {
107     std::shared_ptr<HalWrapper> handle = initHal();
108     auto result = handle->createHintSession(tgid, uid, threadIds, durationNanos);
109     return processHalResult(result, "createHintSession");
110 }
111 
getHintSessionPreferredRate()112 HalResult<int64_t> PowerHalController::getHintSessionPreferredRate() {
113     std::shared_ptr<HalWrapper> handle = initHal();
114     auto result = handle->getHintSessionPreferredRate();
115     return processHalResult(result, "getHintSessionPreferredRate");
116 }
117 
118 } // namespace power
119 
120 } // namespace android
121