blob: 590fec5443ad5969018158d79eabfbdd00a58324 [file] [log] [blame]
Mikhail Naganovd7b2ff02020-02-07 13:51:04 -08001/*
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 "FactoryHalHidl"
18
Mikhail Naganov288a3432022-03-25 00:29:56 +000019#include <algorithm>
20#include <array>
21#include <utility>
22
Mikhail Naganovd7b2ff02020-02-07 13:51:04 -080023#include <media/audiohal/FactoryHalHidl.h>
24
25#include <dlfcn.h>
26
27#include <android/hidl/manager/1.0/IServiceManager.h>
28#include <hidl/ServiceManagement.h>
29#include <hidl/Status.h>
30#include <utils/Log.h>
31
32namespace android::detail {
33
34namespace {
Mikhail Naganov288a3432022-03-25 00:29:56 +000035/** Supported HAL versions, from most recent to least recent.
Mikhail Naganovd7b2ff02020-02-07 13:51:04 -080036 */
Mikhail Naganov288a3432022-03-25 00:29:56 +000037#define CONC_VERSION(maj, min) #maj "." #min
38#define DECLARE_VERSION(maj, min) std::make_pair(std::make_pair(maj, min), CONC_VERSION(maj, min))
39static constexpr std::array<std::pair<std::pair<int, int>, const char*>, 5> sAudioHALVersions = {
40 DECLARE_VERSION(7, 1),
41 DECLARE_VERSION(7, 0),
42 DECLARE_VERSION(6, 0),
43 DECLARE_VERSION(5, 0),
44 DECLARE_VERSION(4, 0)
Mikhail Naganovd7b2ff02020-02-07 13:51:04 -080045};
46
47bool createHalService(const std::string& version, const std::string& interface,
48 void** rawInterface) {
49 const std::string libName = "libaudiohal@" + version + ".so";
50 const std::string factoryFunctionName = "create" + interface;
51 constexpr int dlMode = RTLD_LAZY;
52 void* handle = nullptr;
53 dlerror(); // clear
54 handle = dlopen(libName.c_str(), dlMode);
55 if (handle == nullptr) {
56 const char* error = dlerror();
57 ALOGE("Failed to dlopen %s: %s", libName.c_str(),
58 error != nullptr ? error : "unknown error");
59 return false;
60 }
61 void* (*factoryFunction)();
62 *(void **)(&factoryFunction) = dlsym(handle, factoryFunctionName.c_str());
63 if (!factoryFunction) {
64 const char* error = dlerror();
65 ALOGE("Factory function %s not found in library %s: %s",
66 factoryFunctionName.c_str(), libName.c_str(),
67 error != nullptr ? error : "unknown error");
68 dlclose(handle);
69 return false;
70 }
71 *rawInterface = (*factoryFunction)();
72 ALOGW_IF(!*rawInterface, "Factory function %s from %s returned nullptr",
73 factoryFunctionName.c_str(), libName.c_str());
74 return true;
75}
76
77bool hasHalService(const std::string& package, const std::string& version,
78 const std::string& interface) {
79 using ::android::hidl::manager::V1_0::IServiceManager;
80 sp<IServiceManager> sm = ::android::hardware::defaultServiceManager();
81 if (!sm) {
82 ALOGE("Failed to obtain HIDL ServiceManager");
83 return false;
84 }
85 // Since audio HAL doesn't support multiple clients, avoid instantiating
86 // the interface right away. Instead, query the transport type for it.
87 using ::android::hardware::Return;
88 using Transport = IServiceManager::Transport;
89 const std::string fqName = package + "@" + version + "::" + interface;
90 const std::string instance = "default";
91 Return<Transport> transport = sm->getTransport(fqName, instance);
92 if (!transport.isOk()) {
93 ALOGE("Failed to obtain transport type for %s/%s: %s",
94 fqName.c_str(), instance.c_str(), transport.description().c_str());
95 return false;
96 }
97 return transport != Transport::EMPTY;
98}
99
100} // namespace
101
Mikhail Naganov288a3432022-03-25 00:29:56 +0000102void* createPreferredImpl(const InterfaceName& iface, const InterfaceName& siblingIface) {
103 auto findMostRecentVersion = [](const InterfaceName& iface) {
104 return std::find_if(detail::sAudioHALVersions.begin(), detail::sAudioHALVersions.end(),
105 [&](const auto& v) { return hasHalService(iface.first, v.second, iface.second); });
106 };
107 auto ifaceVersionIt = findMostRecentVersion(iface);
108 auto siblingVersionIt = findMostRecentVersion(siblingIface);
109 if (ifaceVersionIt != detail::sAudioHALVersions.end() &&
110 siblingVersionIt != detail::sAudioHALVersions.end() &&
111 // same major version
112 ifaceVersionIt->first.first == siblingVersionIt->first.first) {
113 std::string libraryVersion =
114 ifaceVersionIt->first >= siblingVersionIt->first ?
115 ifaceVersionIt->second : siblingVersionIt->second;
116 void* rawInterface;
117 if (createHalService(libraryVersion, iface.second, &rawInterface)) {
Mikhail Naganovd7b2ff02020-02-07 13:51:04 -0800118 return rawInterface;
119 }
120 }
121 return nullptr;
122}
123
124} // namespace android::detail