1 /*
2 * Copyright (C) 2016 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 <string.h>
18 #include <vector>
19
20 #define LOG_TAG "DevicesFactoryHalHidl"
21 //#define LOG_NDEBUG 0
22
23 #include PATH(android/hardware/audio/FILE_VERSION/IDevice.h)
24 #include <media/audiohal/hidl/HalDeathHandler.h>
25 #include <utils/Log.h>
26
27 #include "ConversionHelperHidl.h"
28 #include "DeviceHalHidl.h"
29 #include "DevicesFactoryHalHidl.h"
30
31 using ::android::hardware::audio::CPP_VERSION::IDevice;
32 using ::android::hardware::audio::CPP_VERSION::Result;
33 using ::android::hardware::Return;
34
35 namespace android {
36 namespace CPP_VERSION {
37
DevicesFactoryHalHidl()38 DevicesFactoryHalHidl::DevicesFactoryHalHidl() {
39 sp<IDevicesFactory> defaultFactory{IDevicesFactory::getService()};
40 if (!defaultFactory) {
41 ALOGE("Failed to obtain IDevicesFactory/default service, terminating process.");
42 exit(1);
43 }
44 mDeviceFactories.push_back(defaultFactory);
45 if (MAJOR_VERSION >= 4) {
46 // The MSD factory is optional and only available starting at HAL 4.0
47 sp<IDevicesFactory> msdFactory{IDevicesFactory::getService(AUDIO_HAL_SERVICE_NAME_MSD)};
48 if (msdFactory) {
49 mDeviceFactories.push_back(msdFactory);
50 }
51 }
52 for (const auto& factory : mDeviceFactories) {
53 // It is assumed that the DevicesFactoryHalInterface instance is owned
54 // by AudioFlinger and thus have the same lifespan.
55 factory->linkToDeath(HalDeathHandler::getInstance(), 0 /*cookie*/);
56 }
57 }
58
59
60 #if MAJOR_VERSION == 2
idFromHal(const char * name,status_t * status)61 static IDevicesFactory::Device idFromHal(const char *name, status_t* status) {
62 *status = OK;
63 if (strcmp(name, AUDIO_HARDWARE_MODULE_ID_PRIMARY) == 0) {
64 return IDevicesFactory::Device::PRIMARY;
65 } else if(strcmp(name, AUDIO_HARDWARE_MODULE_ID_A2DP) == 0) {
66 return IDevicesFactory::Device::A2DP;
67 } else if(strcmp(name, AUDIO_HARDWARE_MODULE_ID_USB) == 0) {
68 return IDevicesFactory::Device::USB;
69 } else if(strcmp(name, AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX) == 0) {
70 return IDevicesFactory::Device::R_SUBMIX;
71 } else if(strcmp(name, AUDIO_HARDWARE_MODULE_ID_STUB) == 0) {
72 return IDevicesFactory::Device::STUB;
73 }
74 ALOGE("Invalid device name %s", name);
75 *status = BAD_VALUE;
76 return {};
77 }
78 #elif MAJOR_VERSION >= 4
idFromHal(const char * name,status_t * status)79 static const char* idFromHal(const char *name, status_t* status) {
80 *status = OK;
81 return name;
82 }
83 #endif
84
openDevice(const char * name,sp<DeviceHalInterface> * device)85 status_t DevicesFactoryHalHidl::openDevice(const char *name, sp<DeviceHalInterface> *device) {
86 if (mDeviceFactories.empty()) return NO_INIT;
87 status_t status;
88 auto hidlId = idFromHal(name, &status);
89 if (status != OK) return status;
90 Result retval = Result::NOT_INITIALIZED;
91 for (const auto& factory : mDeviceFactories) {
92 Return<void> ret = factory->openDevice(
93 hidlId,
94 [&](Result r, const sp<IDevice>& result) {
95 retval = r;
96 if (retval == Result::OK) {
97 *device = new DeviceHalHidl(result);
98 }
99 });
100 if (!ret.isOk()) return FAILED_TRANSACTION;
101 switch (retval) {
102 // Device was found and was initialized successfully.
103 case Result::OK: return OK;
104 // Device was found but failed to initalize.
105 case Result::NOT_INITIALIZED: return NO_INIT;
106 // Otherwise continue iterating.
107 default: ;
108 }
109 }
110 ALOGW("The specified device name is not recognized: \"%s\"", name);
111 return BAD_VALUE;
112 }
113
114 } // namespace CPP_VERSION
115 } // namespace android
116