blob: 22144615da43f0c0448cb96f9630188321ff6b89 [file] [log] [blame]
Lais Andradec86c1d22020-03-30 20:17:42 +01001/*
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
Xiang Wang99f6f3c2023-05-22 13:12:16 -070019#include <aidl/android/hardware/power/IPower.h>
20#include <android/binder_manager.h>
Lais Andradec86c1d22020-03-30 20:17:42 +010021#include <android/hardware/power/1.1/IPower.h>
Matt Buckleyc3894a42022-09-01 21:17:15 +000022#include <android/hardware/power/1.2/IPower.h>
23#include <android/hardware/power/1.3/IPower.h>
Lais Andradec86c1d22020-03-30 20:17:42 +010024#include <binder/IServiceManager.h>
25#include <hardware/power.h>
26#include <hardware_legacy/power.h>
Lais Andradec86c1d22020-03-30 20:17:42 +010027#include <powermanager/PowerHalLoader.h>
28
Lais Andradeb59a9b52020-05-07 17:23:42 +010029using namespace android::hardware::power;
Lais Andradec86c1d22020-03-30 20:17:42 +010030
31namespace android {
32
Lais Andradeb59a9b52020-05-07 17:23:42 +010033namespace power {
34
Lais Andradec86c1d22020-03-30 20:17:42 +010035// -------------------------------------------------------------------------------------------------
36
37template <typename T, typename F>
38sp<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
57std::mutex PowerHalLoader::gHalMutex;
Xiang Wang99f6f3c2023-05-22 13:12:16 -070058std::shared_ptr<aidl::android::hardware::power::IPower> PowerHalLoader::gHalAidl = nullptr;
Lais Andradeb59a9b52020-05-07 17:23:42 +010059sp<V1_0::IPower> PowerHalLoader::gHalHidlV1_0 = nullptr;
60sp<V1_1::IPower> PowerHalLoader::gHalHidlV1_1 = nullptr;
Matt Buckleyc3894a42022-09-01 21:17:15 +000061sp<V1_2::IPower> PowerHalLoader::gHalHidlV1_2 = nullptr;
62sp<V1_3::IPower> PowerHalLoader::gHalHidlV1_3 = nullptr;
Lais Andradec86c1d22020-03-30 20:17:42 +010063
64void PowerHalLoader::unloadAll() {
65 std::lock_guard<std::mutex> lock(gHalMutex);
66 gHalAidl = nullptr;
67 gHalHidlV1_0 = nullptr;
68 gHalHidlV1_1 = nullptr;
Matt Buckleyc3894a42022-09-01 21:17:15 +000069 gHalHidlV1_2 = nullptr;
70 gHalHidlV1_3 = nullptr;
Lais Andradec86c1d22020-03-30 20:17:42 +010071}
72
Xiang Wang99f6f3c2023-05-22 13:12:16 -070073std::shared_ptr<aidl::android::hardware::power::IPower> PowerHalLoader::loadAidl() {
Lais Andradec86c1d22020-03-30 20:17:42 +010074 std::lock_guard<std::mutex> lock(gHalMutex);
75 static bool gHalExists = true;
Xiang Wang99f6f3c2023-05-22 13:12:16 -070076 if (!gHalExists) {
77 return nullptr;
78 }
79 if (gHalAidl) {
80 return gHalAidl;
81 }
82 auto aidlServiceName =
83 std::string(aidl::android::hardware::power::IPower::descriptor) + "/default";
84 if (!AServiceManager_isDeclared(aidlServiceName.c_str())) {
85 gHalExists = false;
86 return nullptr;
87 }
88 gHalAidl = aidl::android::hardware::power::IPower::fromBinder(
89 ndk::SpAIBinder(AServiceManager_waitForService(aidlServiceName.c_str())));
90 if (gHalAidl) {
91 ALOGI("Successfully connected to Power HAL AIDL service.");
92 } else {
93 ALOGI("Power HAL AIDL service not available.");
94 gHalExists = false;
95 }
96 return gHalAidl;
Lais Andradec86c1d22020-03-30 20:17:42 +010097}
98
Lais Andradeb59a9b52020-05-07 17:23:42 +010099sp<V1_0::IPower> PowerHalLoader::loadHidlV1_0() {
Lais Andradec86c1d22020-03-30 20:17:42 +0100100 std::lock_guard<std::mutex> lock(gHalMutex);
101 return loadHidlV1_0Locked();
102}
103
Lais Andradeb59a9b52020-05-07 17:23:42 +0100104sp<V1_1::IPower> PowerHalLoader::loadHidlV1_1() {
Lais Andradec86c1d22020-03-30 20:17:42 +0100105 std::lock_guard<std::mutex> lock(gHalMutex);
106 static bool gHalExists = true;
Lais Andradeb59a9b52020-05-07 17:23:42 +0100107 static auto loadFn = []() { return V1_1::IPower::castFrom(loadHidlV1_0Locked()); };
108 return loadHal<V1_1::IPower>(gHalExists, gHalHidlV1_1, loadFn, "HIDL v1.1");
Lais Andradec86c1d22020-03-30 20:17:42 +0100109}
110
Matt Buckleyc3894a42022-09-01 21:17:15 +0000111sp<V1_2::IPower> PowerHalLoader::loadHidlV1_2() {
112 std::lock_guard<std::mutex> lock(gHalMutex);
113 static bool gHalExists = true;
114 static auto loadFn = []() { return V1_2::IPower::castFrom(loadHidlV1_0Locked()); };
115 return loadHal<V1_2::IPower>(gHalExists, gHalHidlV1_2, loadFn, "HIDL v1.2");
116}
117
118sp<V1_3::IPower> PowerHalLoader::loadHidlV1_3() {
119 std::lock_guard<std::mutex> lock(gHalMutex);
120 static bool gHalExists = true;
121 static auto loadFn = []() { return V1_3::IPower::castFrom(loadHidlV1_0Locked()); };
122 return loadHal<V1_3::IPower>(gHalExists, gHalHidlV1_3, loadFn, "HIDL v1.3");
123}
124
Lais Andradeb59a9b52020-05-07 17:23:42 +0100125sp<V1_0::IPower> PowerHalLoader::loadHidlV1_0Locked() {
Lais Andradec86c1d22020-03-30 20:17:42 +0100126 static bool gHalExists = true;
Lais Andradeb59a9b52020-05-07 17:23:42 +0100127 static auto loadFn = []() { return V1_0::IPower::getService(); };
128 return loadHal<V1_0::IPower>(gHalExists, gHalHidlV1_0, loadFn, "HIDL v1.0");
Lais Andradec86c1d22020-03-30 20:17:42 +0100129}
130
131// -------------------------------------------------------------------------------------------------
132
Lais Andradeb59a9b52020-05-07 17:23:42 +0100133} // namespace power
134
Lais Andradec86c1d22020-03-30 20:17:42 +0100135} // namespace android