blob: 1f1b43a607bca27295c0135aa1e79dbd2d316ac6 [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>
20#include <android/hardware/power/IPower.h>
21#include <binder/IServiceManager.h>
22#include <hardware/power.h>
23#include <hardware_legacy/power.h>
Lais Andradec86c1d22020-03-30 20:17:42 +010024#include <powermanager/PowerHalLoader.h>
25
Lais Andradeb59a9b52020-05-07 17:23:42 +010026using namespace android::hardware::power;
Lais Andradec86c1d22020-03-30 20:17:42 +010027
28namespace android {
29
Lais Andradeb59a9b52020-05-07 17:23:42 +010030namespace power {
31
Lais Andradec86c1d22020-03-30 20:17:42 +010032// -------------------------------------------------------------------------------------------------
33
34template <typename T, typename F>
35sp<T> loadHal(bool& exists, sp<T>& hal, F& loadFn, const char* halName) {
36 if (!exists) {
37 return nullptr;
38 }
39 if (hal) {
40 return hal;
41 }
42 hal = loadFn();
43 if (hal) {
44 ALOGV("Successfully connected to Power HAL %s service.", halName);
45 } else {
46 ALOGV("Power HAL %s service not available.", halName);
47 exists = false;
48 }
49 return hal;
50}
51
52// -------------------------------------------------------------------------------------------------
53
54std::mutex PowerHalLoader::gHalMutex;
Lais Andradeb59a9b52020-05-07 17:23:42 +010055sp<IPower> PowerHalLoader::gHalAidl = nullptr;
56sp<V1_0::IPower> PowerHalLoader::gHalHidlV1_0 = nullptr;
57sp<V1_1::IPower> PowerHalLoader::gHalHidlV1_1 = nullptr;
Lais Andradec86c1d22020-03-30 20:17:42 +010058
59void PowerHalLoader::unloadAll() {
60 std::lock_guard<std::mutex> lock(gHalMutex);
61 gHalAidl = nullptr;
62 gHalHidlV1_0 = nullptr;
63 gHalHidlV1_1 = nullptr;
64}
65
Lais Andradeb59a9b52020-05-07 17:23:42 +010066sp<IPower> PowerHalLoader::loadAidl() {
Lais Andradec86c1d22020-03-30 20:17:42 +010067 std::lock_guard<std::mutex> lock(gHalMutex);
68 static bool gHalExists = true;
Lais Andradeb59a9b52020-05-07 17:23:42 +010069 static auto loadFn = []() { return waitForVintfService<IPower>(); };
70 return loadHal<IPower>(gHalExists, gHalAidl, loadFn, "AIDL");
Lais Andradec86c1d22020-03-30 20:17:42 +010071}
72
Lais Andradeb59a9b52020-05-07 17:23:42 +010073sp<V1_0::IPower> PowerHalLoader::loadHidlV1_0() {
Lais Andradec86c1d22020-03-30 20:17:42 +010074 std::lock_guard<std::mutex> lock(gHalMutex);
75 return loadHidlV1_0Locked();
76}
77
Lais Andradeb59a9b52020-05-07 17:23:42 +010078sp<V1_1::IPower> PowerHalLoader::loadHidlV1_1() {
Lais Andradec86c1d22020-03-30 20:17:42 +010079 std::lock_guard<std::mutex> lock(gHalMutex);
80 static bool gHalExists = true;
Lais Andradeb59a9b52020-05-07 17:23:42 +010081 static auto loadFn = []() { return V1_1::IPower::castFrom(loadHidlV1_0Locked()); };
82 return loadHal<V1_1::IPower>(gHalExists, gHalHidlV1_1, loadFn, "HIDL v1.1");
Lais Andradec86c1d22020-03-30 20:17:42 +010083}
84
Lais Andradeb59a9b52020-05-07 17:23:42 +010085sp<V1_0::IPower> PowerHalLoader::loadHidlV1_0Locked() {
Lais Andradec86c1d22020-03-30 20:17:42 +010086 static bool gHalExists = true;
Lais Andradeb59a9b52020-05-07 17:23:42 +010087 static auto loadFn = []() { return V1_0::IPower::getService(); };
88 return loadHal<V1_0::IPower>(gHalExists, gHalHidlV1_0, loadFn, "HIDL v1.0");
Lais Andradec86c1d22020-03-30 20:17:42 +010089}
90
91// -------------------------------------------------------------------------------------------------
92
Lais Andradeb59a9b52020-05-07 17:23:42 +010093} // namespace power
94
Lais Andradec86c1d22020-03-30 20:17:42 +010095} // namespace android