blob: 6bd40f8ff2543845c2cd866aed51aa867f46ba1b [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
19#include <android/hardware/power/1.1/IPower.h>
Matt Buckleyc3894a42022-09-01 21:17:15 +000020#include <android/hardware/power/1.2/IPower.h>
21#include <android/hardware/power/1.3/IPower.h>
Lais Andradec86c1d22020-03-30 20:17:42 +010022#include <android/hardware/power/IPower.h>
23#include <binder/IServiceManager.h>
24#include <hardware/power.h>
25#include <hardware_legacy/power.h>
Lais Andradec86c1d22020-03-30 20:17:42 +010026#include <powermanager/PowerHalLoader.h>
27
Lais Andradeb59a9b52020-05-07 17:23:42 +010028using namespace android::hardware::power;
Lais Andradec86c1d22020-03-30 20:17:42 +010029
30namespace android {
31
Lais Andradeb59a9b52020-05-07 17:23:42 +010032namespace power {
33
Lais Andradec86c1d22020-03-30 20:17:42 +010034// -------------------------------------------------------------------------------------------------
35
36template <typename T, typename F>
37sp<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
56std::mutex PowerHalLoader::gHalMutex;
Lais Andradeb59a9b52020-05-07 17:23:42 +010057sp<IPower> PowerHalLoader::gHalAidl = nullptr;
58sp<V1_0::IPower> PowerHalLoader::gHalHidlV1_0 = nullptr;
59sp<V1_1::IPower> PowerHalLoader::gHalHidlV1_1 = nullptr;
Matt Buckleyc3894a42022-09-01 21:17:15 +000060sp<V1_2::IPower> PowerHalLoader::gHalHidlV1_2 = nullptr;
61sp<V1_3::IPower> PowerHalLoader::gHalHidlV1_3 = nullptr;
Lais Andradec86c1d22020-03-30 20:17:42 +010062
63void PowerHalLoader::unloadAll() {
64 std::lock_guard<std::mutex> lock(gHalMutex);
65 gHalAidl = nullptr;
66 gHalHidlV1_0 = nullptr;
67 gHalHidlV1_1 = nullptr;
Matt Buckleyc3894a42022-09-01 21:17:15 +000068 gHalHidlV1_2 = nullptr;
69 gHalHidlV1_3 = nullptr;
Lais Andradec86c1d22020-03-30 20:17:42 +010070}
71
Lais Andradeb59a9b52020-05-07 17:23:42 +010072sp<IPower> PowerHalLoader::loadAidl() {
Lais Andradec86c1d22020-03-30 20:17:42 +010073 std::lock_guard<std::mutex> lock(gHalMutex);
74 static bool gHalExists = true;
Lais Andradeb59a9b52020-05-07 17:23:42 +010075 static auto loadFn = []() { return waitForVintfService<IPower>(); };
76 return loadHal<IPower>(gHalExists, gHalAidl, loadFn, "AIDL");
Lais Andradec86c1d22020-03-30 20:17:42 +010077}
78
Lais Andradeb59a9b52020-05-07 17:23:42 +010079sp<V1_0::IPower> PowerHalLoader::loadHidlV1_0() {
Lais Andradec86c1d22020-03-30 20:17:42 +010080 std::lock_guard<std::mutex> lock(gHalMutex);
81 return loadHidlV1_0Locked();
82}
83
Lais Andradeb59a9b52020-05-07 17:23:42 +010084sp<V1_1::IPower> PowerHalLoader::loadHidlV1_1() {
Lais Andradec86c1d22020-03-30 20:17:42 +010085 std::lock_guard<std::mutex> lock(gHalMutex);
86 static bool gHalExists = true;
Lais Andradeb59a9b52020-05-07 17:23:42 +010087 static auto loadFn = []() { return V1_1::IPower::castFrom(loadHidlV1_0Locked()); };
88 return loadHal<V1_1::IPower>(gHalExists, gHalHidlV1_1, loadFn, "HIDL v1.1");
Lais Andradec86c1d22020-03-30 20:17:42 +010089}
90
Matt Buckleyc3894a42022-09-01 21:17:15 +000091sp<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
98sp<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
Lais Andradeb59a9b52020-05-07 17:23:42 +0100105sp<V1_0::IPower> PowerHalLoader::loadHidlV1_0Locked() {
Lais Andradec86c1d22020-03-30 20:17:42 +0100106 static bool gHalExists = true;
Lais Andradeb59a9b52020-05-07 17:23:42 +0100107 static auto loadFn = []() { return V1_0::IPower::getService(); };
108 return loadHal<V1_0::IPower>(gHalExists, gHalHidlV1_0, loadFn, "HIDL v1.0");
Lais Andradec86c1d22020-03-30 20:17:42 +0100109}
110
111// -------------------------------------------------------------------------------------------------
112
Lais Andradeb59a9b52020-05-07 17:23:42 +0100113} // namespace power
114
Lais Andradec86c1d22020-03-30 20:17:42 +0100115} // namespace android