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 #include <system/audio.h>
18 #include <log/log.h>
19 #include "device_factory.h"
20 #include "primary_device.h"
21 #include "debug.h"
22
23 namespace android {
24 namespace hardware {
25 namespace audio {
26 namespace CPP_VERSION {
27 namespace implementation {
28
29 using ::android::hardware::Void;
30
31 #ifdef __LP64__
32 #define LIB_PATH_PREFIX "vendor/lib64/hw/"
33 #else
34 #define LIB_PATH_PREFIX "vendor/lib/hw/"
35 #endif
36
37 #define QUOTE(x) #x
38 #define STRINGIFY(x) QUOTE(x)
39
DevicesFactory()40 DevicesFactory::DevicesFactory() {
41 mLegacyLib.reset(dlopen(
42 LIB_PATH_PREFIX "android.hardware.audio.legacy@" STRINGIFY(FILE_VERSION) "-impl.ranchu.so",
43 RTLD_NOW));
44 LOG_ALWAYS_FATAL_IF(!mLegacyLib);
45
46 typedef IDevicesFactory *(*Func)(const char *);
47 const auto func = reinterpret_cast<Func>(
48 dlsym(mLegacyLib.get(), "HIDL_FETCH_IDevicesFactory"));
49 LOG_ALWAYS_FATAL_IF(!func);
50
51 mLegacyFactory.reset((*func)("default"));
52 LOG_ALWAYS_FATAL_IF(!mLegacyFactory);
53 }
54
openDevice(const hidl_string & device,openDevice_cb _hidl_cb)55 Return<void> DevicesFactory::openDevice(const hidl_string& device,
56 openDevice_cb _hidl_cb) {
57 if (device == AUDIO_HARDWARE_MODULE_ID_PRIMARY) {
58 _hidl_cb(Result::OK, new PrimaryDevice);
59 } else {
60 mLegacyFactory->openDevice(device, _hidl_cb);
61 }
62 return Void();
63 }
64
openPrimaryDevice(openPrimaryDevice_cb _hidl_cb)65 Return<void> DevicesFactory::openPrimaryDevice(openPrimaryDevice_cb _hidl_cb) {
66 _hidl_cb(Result::OK, new PrimaryDevice);
67 return Void();
68 }
69
70 #if MAJOR_VERSION == 7 && MINOR_VERSION == 1
openDevice_7_1(const hidl_string & device,openDevice_7_1_cb _hidl_cb)71 Return<void> DevicesFactory::openDevice_7_1(const hidl_string& device, openDevice_7_1_cb _hidl_cb) {
72 if (device == AUDIO_HARDWARE_MODULE_ID_PRIMARY) {
73 auto primary = sp<PrimaryDevice>::make();
74 auto getDeviceRet = primary->getDevice();
75 if (getDeviceRet.isOk()) {
76 _hidl_cb(Result::OK, getDeviceRet);
77 } else {
78 _hidl_cb(Result::NOT_INITIALIZED, nullptr);
79 }
80 } else {
81 mLegacyFactory->openDevice_7_1(device, _hidl_cb);
82 }
83 return Void();
84 }
85
openPrimaryDevice_7_1(openPrimaryDevice_7_1_cb _hidl_cb)86 Return<void> DevicesFactory::openPrimaryDevice_7_1(openPrimaryDevice_7_1_cb _hidl_cb) {
87 _hidl_cb(Result::OK, new PrimaryDevice);
88 return Void();
89 }
90 #endif
91
92 } // namespace implementation
93 } // namespace CPP_VERSION
94 } // namespace audio
95 } // namespace hardware
96 } // namespace android
97