blob: 3ae538447b2134e1f5facf58b94c7f1e6735c8cf [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>
24
25#include <powermanager/PowerHalLoader.h>
26
27using IPowerV1_1 = android::hardware::power::V1_1::IPower;
28using IPowerV1_0 = android::hardware::power::V1_0::IPower;
29using IPowerAidl = android::hardware::power::IPower;
30
31namespace android {
32
33// -------------------------------------------------------------------------------------------------
34
35template <typename T, typename F>
36sp<T> loadHal(bool& exists, sp<T>& hal, F& loadFn, const char* halName) {
37 if (!exists) {
38 return nullptr;
39 }
40 if (hal) {
41 return hal;
42 }
43 hal = loadFn();
44 if (hal) {
45 ALOGV("Successfully connected to Power HAL %s service.", halName);
46 } else {
47 ALOGV("Power HAL %s service not available.", halName);
48 exists = false;
49 }
50 return hal;
51}
52
53// -------------------------------------------------------------------------------------------------
54
55std::mutex PowerHalLoader::gHalMutex;
56sp<IPowerAidl> PowerHalLoader::gHalAidl = nullptr;
57sp<IPowerV1_0> PowerHalLoader::gHalHidlV1_0 = nullptr;
58sp<IPowerV1_1> PowerHalLoader::gHalHidlV1_1 = nullptr;
59
60void PowerHalLoader::unloadAll() {
61 std::lock_guard<std::mutex> lock(gHalMutex);
62 gHalAidl = nullptr;
63 gHalHidlV1_0 = nullptr;
64 gHalHidlV1_1 = nullptr;
65}
66
67sp<IPowerAidl> PowerHalLoader::loadAidl() {
68 std::lock_guard<std::mutex> lock(gHalMutex);
69 static bool gHalExists = true;
70 static auto loadFn = []() { return waitForVintfService<IPowerAidl>(); };
71 return loadHal<IPowerAidl>(gHalExists, gHalAidl, loadFn, "AIDL");
72}
73
74sp<IPowerV1_0> PowerHalLoader::loadHidlV1_0() {
75 std::lock_guard<std::mutex> lock(gHalMutex);
76 return loadHidlV1_0Locked();
77}
78
79sp<IPowerV1_1> PowerHalLoader::loadHidlV1_1() {
80 std::lock_guard<std::mutex> lock(gHalMutex);
81 static bool gHalExists = true;
82 static auto loadFn = []() { return IPowerV1_1::castFrom(loadHidlV1_0Locked()); };
83 return loadHal<IPowerV1_1>(gHalExists, gHalHidlV1_1, loadFn, "HIDL v1.1");
84}
85
86sp<IPowerV1_0> PowerHalLoader::loadHidlV1_0Locked() {
87 static bool gHalExists = true;
88 static auto loadFn = []() { return IPowerV1_0::getService(); };
89 return loadHal<IPowerV1_0>(gHalExists, gHalHidlV1_0, loadFn, "HIDL v1.0");
90}
91
92// -------------------------------------------------------------------------------------------------
93
94} // namespace android