blob: 7228b221b9d55c80f2a072aeda567dd52ce549b8 [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
19#include <media/audiohal/FactoryHalHidl.h>
20
21#include <dlfcn.h>
22
23#include <android/hidl/manager/1.0/IServiceManager.h>
24#include <hidl/ServiceManagement.h>
25#include <hidl/Status.h>
26#include <utils/Log.h>
27
28namespace android::detail {
29
30namespace {
31/** Supported HAL versions, in order of preference.
32 */
33const char* sAudioHALVersions[] = {
Mikhail Naganov980e5f12020-07-23 18:10:20 +000034 "7.0",
Mikhail Naganovd7b2ff02020-02-07 13:51:04 -080035 "6.0",
36 "5.0",
37 "4.0",
38 "2.0",
39 nullptr
40};
41
42bool createHalService(const std::string& version, const std::string& interface,
43 void** rawInterface) {
44 const std::string libName = "libaudiohal@" + version + ".so";
45 const std::string factoryFunctionName = "create" + interface;
46 constexpr int dlMode = RTLD_LAZY;
47 void* handle = nullptr;
48 dlerror(); // clear
49 handle = dlopen(libName.c_str(), dlMode);
50 if (handle == nullptr) {
51 const char* error = dlerror();
52 ALOGE("Failed to dlopen %s: %s", libName.c_str(),
53 error != nullptr ? error : "unknown error");
54 return false;
55 }
56 void* (*factoryFunction)();
57 *(void **)(&factoryFunction) = dlsym(handle, factoryFunctionName.c_str());
58 if (!factoryFunction) {
59 const char* error = dlerror();
60 ALOGE("Factory function %s not found in library %s: %s",
61 factoryFunctionName.c_str(), libName.c_str(),
62 error != nullptr ? error : "unknown error");
63 dlclose(handle);
64 return false;
65 }
66 *rawInterface = (*factoryFunction)();
67 ALOGW_IF(!*rawInterface, "Factory function %s from %s returned nullptr",
68 factoryFunctionName.c_str(), libName.c_str());
69 return true;
70}
71
72bool hasHalService(const std::string& package, const std::string& version,
73 const std::string& interface) {
74 using ::android::hidl::manager::V1_0::IServiceManager;
75 sp<IServiceManager> sm = ::android::hardware::defaultServiceManager();
76 if (!sm) {
77 ALOGE("Failed to obtain HIDL ServiceManager");
78 return false;
79 }
80 // Since audio HAL doesn't support multiple clients, avoid instantiating
81 // the interface right away. Instead, query the transport type for it.
82 using ::android::hardware::Return;
83 using Transport = IServiceManager::Transport;
84 const std::string fqName = package + "@" + version + "::" + interface;
85 const std::string instance = "default";
86 Return<Transport> transport = sm->getTransport(fqName, instance);
87 if (!transport.isOk()) {
88 ALOGE("Failed to obtain transport type for %s/%s: %s",
89 fqName.c_str(), instance.c_str(), transport.description().c_str());
90 return false;
91 }
92 return transport != Transport::EMPTY;
93}
94
95} // namespace
96
97void* createPreferredImpl(const std::string& package, const std::string& interface) {
98 for (auto version = detail::sAudioHALVersions; version != nullptr; ++version) {
99 void* rawInterface = nullptr;
100 if (hasHalService(package, *version, interface)
101 && createHalService(*version, interface, &rawInterface)) {
102 return rawInterface;
103 }
104 }
105 return nullptr;
106}
107
108} // namespace android::detail