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