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 "PowerHalLoader"
18 
19 #include <android/hardware/power/1.1/IPower.h>
20 #include <android/hardware/power/IPower.h>
21 #include <binder/IServiceManager.h>
22 #include <hardware/power.h>
23 #include <hardware_legacy/power.h>
24 #include <powermanager/PowerHalLoader.h>
25 
26 using namespace android::hardware::power;
27 
28 namespace android {
29 
30 namespace power {
31 
32 // -------------------------------------------------------------------------------------------------
33 
34 template <typename T, typename F>
loadHal(bool & exists,sp<T> & hal,F & loadFn,const char * halName)35 sp<T> loadHal(bool& exists, sp<T>& hal, F& loadFn, const char* halName) {
36     if (!exists) {
37         return nullptr;
38     }
39     if (hal) {
40         return hal;
41     }
42     hal = loadFn();
43     if (hal) {
44         ALOGV("Successfully connected to Power HAL %s service.", halName);
45     } else {
46         ALOGV("Power HAL %s service not available.", halName);
47         exists = false;
48     }
49     return hal;
50 }
51 
52 // -------------------------------------------------------------------------------------------------
53 
54 std::mutex PowerHalLoader::gHalMutex;
55 sp<IPower> PowerHalLoader::gHalAidl = nullptr;
56 sp<V1_0::IPower> PowerHalLoader::gHalHidlV1_0 = nullptr;
57 sp<V1_1::IPower> PowerHalLoader::gHalHidlV1_1 = nullptr;
58 
unloadAll()59 void PowerHalLoader::unloadAll() {
60     std::lock_guard<std::mutex> lock(gHalMutex);
61     gHalAidl = nullptr;
62     gHalHidlV1_0 = nullptr;
63     gHalHidlV1_1 = nullptr;
64 }
65 
loadAidl()66 sp<IPower> PowerHalLoader::loadAidl() {
67     std::lock_guard<std::mutex> lock(gHalMutex);
68     static bool gHalExists = true;
69     static auto loadFn = []() { return waitForVintfService<IPower>(); };
70     return loadHal<IPower>(gHalExists, gHalAidl, loadFn, "AIDL");
71 }
72 
loadHidlV1_0()73 sp<V1_0::IPower> PowerHalLoader::loadHidlV1_0() {
74     std::lock_guard<std::mutex> lock(gHalMutex);
75     return loadHidlV1_0Locked();
76 }
77 
loadHidlV1_1()78 sp<V1_1::IPower> PowerHalLoader::loadHidlV1_1() {
79     std::lock_guard<std::mutex> lock(gHalMutex);
80     static bool gHalExists = true;
81     static auto loadFn = []() { return V1_1::IPower::castFrom(loadHidlV1_0Locked()); };
82     return loadHal<V1_1::IPower>(gHalExists, gHalHidlV1_1, loadFn, "HIDL v1.1");
83 }
84 
loadHidlV1_0Locked()85 sp<V1_0::IPower> PowerHalLoader::loadHidlV1_0Locked() {
86     static bool gHalExists = true;
87     static auto loadFn = []() { return V1_0::IPower::getService(); };
88     return loadHal<V1_0::IPower>(gHalExists, gHalHidlV1_0, loadFn, "HIDL v1.0");
89 }
90 
91 // -------------------------------------------------------------------------------------------------
92 
93 } // namespace power
94 
95 } // namespace android
96