blob: 5d97000b7ddf9a468b7f6668f0b4a8939b3bb339 [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
Kevin Rocard95213bf2018-11-08 17:16:57 -080023#include PATH(android/hardware/audio/FILE_VERSION/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
Kevin Rocard070e7512018-05-22 09:29:13 -070031using ::android::hardware::audio::CPP_VERSION::IDevice;
32using ::android::hardware::audio::CPP_VERSION::Result;
Mikhail Naganovf558e022016-11-14 17:45:17 -080033using ::android::hardware::Return;
Mikhail Naganovf558e022016-11-14 17:45:17 -080034
35namespace android {
Kevin Rocard070e7512018-05-22 09:29:13 -070036namespace CPP_VERSION {
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);
Kevin Rocard070e7512018-05-22 09:29:13 -070045 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 }
Kevin Rocarddf9b4202018-05-10 19:56:08 -070052 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 }
Mikhail Naganovf558e022016-11-14 17:45:17 -080057}
58
Mikhail Naganovf558e022016-11-14 17:45:17 -080059
Kevin Rocard070e7512018-05-22 09:29:13 -070060#if MAJOR_VERSION == 2
Kevin Rocarddf9b4202018-05-10 19:56:08 -070061static IDevicesFactory::Device idFromHal(const char *name, status_t* status) {
62 *status = OK;
Mikhail Naganovf558e022016-11-14 17:45:17 -080063 if (strcmp(name, AUDIO_HARDWARE_MODULE_ID_PRIMARY) == 0) {
Kevin Rocarddf9b4202018-05-10 19:56:08 -070064 return IDevicesFactory::Device::PRIMARY;
Mikhail Naganovf558e022016-11-14 17:45:17 -080065 } else if(strcmp(name, AUDIO_HARDWARE_MODULE_ID_A2DP) == 0) {
Kevin Rocarddf9b4202018-05-10 19:56:08 -070066 return IDevicesFactory::Device::A2DP;
Mikhail Naganovf558e022016-11-14 17:45:17 -080067 } else if(strcmp(name, AUDIO_HARDWARE_MODULE_ID_USB) == 0) {
Kevin Rocarddf9b4202018-05-10 19:56:08 -070068 return IDevicesFactory::Device::USB;
Mikhail Naganovf558e022016-11-14 17:45:17 -080069 } else if(strcmp(name, AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX) == 0) {
Kevin Rocarddf9b4202018-05-10 19:56:08 -070070 return IDevicesFactory::Device::R_SUBMIX;
Eric Laurentcb9d2eb2017-01-18 17:00:39 -080071 } else if(strcmp(name, AUDIO_HARDWARE_MODULE_ID_STUB) == 0) {
Kevin Rocarddf9b4202018-05-10 19:56:08 -070072 return IDevicesFactory::Device::STUB;
Mikhail Naganovf558e022016-11-14 17:45:17 -080073 }
74 ALOGE("Invalid device name %s", name);
Kevin Rocarddf9b4202018-05-10 19:56:08 -070075 *status = BAD_VALUE;
76 return {};
Mikhail Naganovf558e022016-11-14 17:45:17 -080077}
Kevin Rocard070e7512018-05-22 09:29:13 -070078#elif MAJOR_VERSION == 4
79static const char* idFromHal(const char *name, status_t* status) {
80 *status = OK;
81 return name;
82}
83#endif
Mikhail Naganovf558e022016-11-14 17:45:17 -080084
85status_t DevicesFactoryHalHidl::openDevice(const char *name, sp<DeviceHalInterface> *device) {
Kevin Rocarddf9b4202018-05-10 19:56:08 -070086 if (mDeviceFactories.empty()) return NO_INIT;
87 status_t status;
88 auto hidlId = idFromHal(name, &status);
Mikhail Naganovf558e022016-11-14 17:45:17 -080089 if (status != OK) return status;
90 Result retval = Result::NOT_INITIALIZED;
Kevin Rocarddf9b4202018-05-10 19:56:08 -070091 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 }
Mikhail Naganovf558e022016-11-14 17:45:17 -0800109 }
Kevin Rocarddf9b4202018-05-10 19:56:08 -0700110 ALOGW("The specified device name is not recognized: \"%s\"", name);
111 return BAD_VALUE;
Mikhail Naganovf558e022016-11-14 17:45:17 -0800112}
113
Kevin Rocard070e7512018-05-22 09:29:13 -0700114} // namespace CPP_VERSION
Mikhail Naganovf558e022016-11-14 17:45:17 -0800115} // namespace android