blob: ea284c36d8589201729409d2584116695b5f7850 [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;
Matt Buckley6c18e6d2024-02-07 23:39:50 +000063int32_t PowerHalLoader::gAidlInterfaceVersion = 0;
Lais Andradec86c1d22020-03-30 20:17:42 +010064
65void PowerHalLoader::unloadAll() {
66 std::lock_guard<std::mutex> lock(gHalMutex);
67 gHalAidl = nullptr;
68 gHalHidlV1_0 = nullptr;
69 gHalHidlV1_1 = nullptr;
Matt Buckleyc3894a42022-09-01 21:17:15 +000070 gHalHidlV1_2 = nullptr;
71 gHalHidlV1_3 = nullptr;
Lais Andradec86c1d22020-03-30 20:17:42 +010072}
73
Xiang Wang99f6f3c2023-05-22 13:12:16 -070074std::shared_ptr<aidl::android::hardware::power::IPower> PowerHalLoader::loadAidl() {
Lais Andradec86c1d22020-03-30 20:17:42 +010075 std::lock_guard<std::mutex> lock(gHalMutex);
76 static bool gHalExists = true;
Xiang Wang99f6f3c2023-05-22 13:12:16 -070077 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.");
Matt Buckley6c18e6d2024-02-07 23:39:50 +000093 gHalAidl->getInterfaceVersion(&gAidlInterfaceVersion);
94
Xiang Wang99f6f3c2023-05-22 13:12:16 -070095 } else {
96 ALOGI("Power HAL AIDL service not available.");
97 gHalExists = false;
98 }
99 return gHalAidl;
Lais Andradec86c1d22020-03-30 20:17:42 +0100100}
101
Lais Andradeb59a9b52020-05-07 17:23:42 +0100102sp<V1_0::IPower> PowerHalLoader::loadHidlV1_0() {
Lais Andradec86c1d22020-03-30 20:17:42 +0100103 std::lock_guard<std::mutex> lock(gHalMutex);
104 return loadHidlV1_0Locked();
105}
106
Lais Andradeb59a9b52020-05-07 17:23:42 +0100107sp<V1_1::IPower> PowerHalLoader::loadHidlV1_1() {
Lais Andradec86c1d22020-03-30 20:17:42 +0100108 std::lock_guard<std::mutex> lock(gHalMutex);
109 static bool gHalExists = true;
Lais Andradeb59a9b52020-05-07 17:23:42 +0100110 static auto loadFn = []() { return V1_1::IPower::castFrom(loadHidlV1_0Locked()); };
111 return loadHal<V1_1::IPower>(gHalExists, gHalHidlV1_1, loadFn, "HIDL v1.1");
Lais Andradec86c1d22020-03-30 20:17:42 +0100112}
113
Matt Buckleyc3894a42022-09-01 21:17:15 +0000114sp<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
121sp<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
Lais Andradeb59a9b52020-05-07 17:23:42 +0100128sp<V1_0::IPower> PowerHalLoader::loadHidlV1_0Locked() {
Lais Andradec86c1d22020-03-30 20:17:42 +0100129 static bool gHalExists = true;
Lais Andradeb59a9b52020-05-07 17:23:42 +0100130 static auto loadFn = []() { return V1_0::IPower::getService(); };
131 return loadHal<V1_0::IPower>(gHalExists, gHalHidlV1_0, loadFn, "HIDL v1.0");
Lais Andradec86c1d22020-03-30 20:17:42 +0100132}
133
Matt Buckley6c18e6d2024-02-07 23:39:50 +0000134int32_t PowerHalLoader::getAidlVersion() {
135 return gAidlInterfaceVersion;
136}
137
Lais Andradec86c1d22020-03-30 20:17:42 +0100138// -------------------------------------------------------------------------------------------------
139
Lais Andradeb59a9b52020-05-07 17:23:42 +0100140} // namespace power
141
Lais Andradec86c1d22020-03-30 20:17:42 +0100142} // namespace android