blob: ac65fcc55cd68cc08cd4dfaee26edf8ed5a69062 [file] [log] [blame]
Mikhail Naganovf558e022016-11-14 17:45:17 -08001/*
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>
Kevin Rocarddf9b4202018-05-10 19:56:08 -070018#include <vector>
Mikhail Naganovf558e022016-11-14 17:45:17 -080019
20#define LOG_TAG "DevicesFactoryHalHidl"
21//#define LOG_NDEBUG 0
22
23#include <android/hardware/audio/2.0/IDevice.h>
Mikhail Naganovd621ac82017-01-12 17:17:45 -080024#include <media/audiohal/hidl/HalDeathHandler.h>
Mikhail Naganovf558e022016-11-14 17:45:17 -080025#include <utils/Log.h>
26
Mikhail Naganov9f57e3c2016-12-05 12:54:36 -080027#include "ConversionHelperHidl.h"
Mikhail Naganovf558e022016-11-14 17:45:17 -080028#include "DeviceHalHidl.h"
29#include "DevicesFactoryHalHidl.h"
30
31using ::android::hardware::audio::V2_0::IDevice;
32using ::android::hardware::audio::V2_0::Result;
33using ::android::hardware::Return;
Mikhail Naganovf558e022016-11-14 17:45:17 -080034
35namespace android {
Kevin Rocarddf9b4202018-05-10 19:56:08 -070036namespace V2_0 {
Mikhail Naganovf558e022016-11-14 17:45:17 -080037
Mikhail Naganovf558e022016-11-14 17:45:17 -080038DevicesFactoryHalHidl::DevicesFactoryHalHidl() {
Kevin Rocarddf9b4202018-05-10 19:56:08 -070039 sp<IDevicesFactory> defaultFactory{IDevicesFactory::getService()};
40 if (!defaultFactory) {
41 ALOGE("Failed to obtain IDevicesFactory/default service, terminating process.");
Mikhail Naganov1ba40412017-03-20 10:58:07 -070042 exit(1);
Mikhail Naganovd621ac82017-01-12 17:17:45 -080043 }
Kevin Rocarddf9b4202018-05-10 19:56:08 -070044 mDeviceFactories.push_back(defaultFactory);
45 for (const auto& factory : mDeviceFactories) {
46 // It is assumed that the DevicesFactoryHalInterface instance is owned
47 // by AudioFlinger and thus have the same lifespan.
48 factory->linkToDeath(HalDeathHandler::getInstance(), 0 /*cookie*/);
49 }
Mikhail Naganovf558e022016-11-14 17:45:17 -080050}
51
Mikhail Naganovf558e022016-11-14 17:45:17 -080052
Kevin Rocarddf9b4202018-05-10 19:56:08 -070053static IDevicesFactory::Device idFromHal(const char *name, status_t* status) {
54 *status = OK;
Mikhail Naganovf558e022016-11-14 17:45:17 -080055 if (strcmp(name, AUDIO_HARDWARE_MODULE_ID_PRIMARY) == 0) {
Kevin Rocarddf9b4202018-05-10 19:56:08 -070056 return IDevicesFactory::Device::PRIMARY;
Mikhail Naganovf558e022016-11-14 17:45:17 -080057 } else if(strcmp(name, AUDIO_HARDWARE_MODULE_ID_A2DP) == 0) {
Kevin Rocarddf9b4202018-05-10 19:56:08 -070058 return IDevicesFactory::Device::A2DP;
Mikhail Naganovf558e022016-11-14 17:45:17 -080059 } else if(strcmp(name, AUDIO_HARDWARE_MODULE_ID_USB) == 0) {
Kevin Rocarddf9b4202018-05-10 19:56:08 -070060 return IDevicesFactory::Device::USB;
Mikhail Naganovf558e022016-11-14 17:45:17 -080061 } else if(strcmp(name, AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX) == 0) {
Kevin Rocarddf9b4202018-05-10 19:56:08 -070062 return IDevicesFactory::Device::R_SUBMIX;
Eric Laurentcb9d2eb2017-01-18 17:00:39 -080063 } else if(strcmp(name, AUDIO_HARDWARE_MODULE_ID_STUB) == 0) {
Kevin Rocarddf9b4202018-05-10 19:56:08 -070064 return IDevicesFactory::Device::STUB;
Mikhail Naganovf558e022016-11-14 17:45:17 -080065 }
66 ALOGE("Invalid device name %s", name);
Kevin Rocarddf9b4202018-05-10 19:56:08 -070067 *status = BAD_VALUE;
68 return {};
Mikhail Naganovf558e022016-11-14 17:45:17 -080069}
70
71status_t DevicesFactoryHalHidl::openDevice(const char *name, sp<DeviceHalInterface> *device) {
Kevin Rocarddf9b4202018-05-10 19:56:08 -070072 if (mDeviceFactories.empty()) return NO_INIT;
73 status_t status;
74 auto hidlId = idFromHal(name, &status);
Mikhail Naganovf558e022016-11-14 17:45:17 -080075 if (status != OK) return status;
76 Result retval = Result::NOT_INITIALIZED;
Kevin Rocarddf9b4202018-05-10 19:56:08 -070077 for (const auto& factory : mDeviceFactories) {
78 Return<void> ret = factory->openDevice(
79 hidlId,
80 [&](Result r, const sp<IDevice>& result) {
81 retval = r;
82 if (retval == Result::OK) {
83 *device = new DeviceHalHidl(result);
84 }
85 });
86 if (!ret.isOk()) return FAILED_TRANSACTION;
87 switch (retval) {
88 // Device was found and was initialized successfully.
89 case Result::OK: return OK;
90 // Device was found but failed to initalize.
91 case Result::NOT_INITIALIZED: return NO_INIT;
92 // Otherwise continue iterating.
93 default: ;
94 }
Mikhail Naganovf558e022016-11-14 17:45:17 -080095 }
Kevin Rocarddf9b4202018-05-10 19:56:08 -070096 ALOGW("The specified device name is not recognized: \"%s\"", name);
97 return BAD_VALUE;
Mikhail Naganovf558e022016-11-14 17:45:17 -080098}
99
Kevin Rocarddf9b4202018-05-10 19:56:08 -0700100} // namespace V2_0
Mikhail Naganovf558e022016-11-14 17:45:17 -0800101} // namespace android