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