Lais Andrade | c86c1d2 | 2020-03-30 20:17:42 +0100 | [diff] [blame^] | 1 | /* |
| 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 | |
| 27 | using IPowerV1_1 = android::hardware::power::V1_1::IPower; |
| 28 | using IPowerV1_0 = android::hardware::power::V1_0::IPower; |
| 29 | using IPowerAidl = android::hardware::power::IPower; |
| 30 | |
| 31 | namespace android { |
| 32 | |
| 33 | // ------------------------------------------------------------------------------------------------- |
| 34 | |
| 35 | template <typename T, typename F> |
| 36 | sp<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 | |
| 55 | std::mutex PowerHalLoader::gHalMutex; |
| 56 | sp<IPowerAidl> PowerHalLoader::gHalAidl = nullptr; |
| 57 | sp<IPowerV1_0> PowerHalLoader::gHalHidlV1_0 = nullptr; |
| 58 | sp<IPowerV1_1> PowerHalLoader::gHalHidlV1_1 = nullptr; |
| 59 | |
| 60 | void PowerHalLoader::unloadAll() { |
| 61 | std::lock_guard<std::mutex> lock(gHalMutex); |
| 62 | gHalAidl = nullptr; |
| 63 | gHalHidlV1_0 = nullptr; |
| 64 | gHalHidlV1_1 = nullptr; |
| 65 | } |
| 66 | |
| 67 | sp<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 | |
| 74 | sp<IPowerV1_0> PowerHalLoader::loadHidlV1_0() { |
| 75 | std::lock_guard<std::mutex> lock(gHalMutex); |
| 76 | return loadHidlV1_0Locked(); |
| 77 | } |
| 78 | |
| 79 | sp<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 | |
| 86 | sp<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 |