blob: 28001dafdfdb44dc6b030da176213cd22055415b [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>
Kevin Rocard070e7512018-05-22 09:29:13 -070024#include <android/hardware/audio/4.0/IDevice.h>
Mikhail Naganovd621ac82017-01-12 17:17:45 -080025#include <media/audiohal/hidl/HalDeathHandler.h>
Mikhail Naganovf558e022016-11-14 17:45:17 -080026#include <utils/Log.h>
27
Mikhail Naganov9f57e3c2016-12-05 12:54:36 -080028#include "ConversionHelperHidl.h"
Mikhail Naganovf558e022016-11-14 17:45:17 -080029#include "DeviceHalHidl.h"
30#include "DevicesFactoryHalHidl.h"
31
Kevin Rocard070e7512018-05-22 09:29:13 -070032using ::android::hardware::audio::CPP_VERSION::IDevice;
33using ::android::hardware::audio::CPP_VERSION::Result;
Mikhail Naganovf558e022016-11-14 17:45:17 -080034using ::android::hardware::Return;
Mikhail Naganovf558e022016-11-14 17:45:17 -080035
36namespace android {
Kevin Rocard070e7512018-05-22 09:29:13 -070037namespace CPP_VERSION {
Mikhail Naganovf558e022016-11-14 17:45:17 -080038
Mikhail Naganovf558e022016-11-14 17:45:17 -080039DevicesFactoryHalHidl::DevicesFactoryHalHidl() {
Kevin Rocarddf9b4202018-05-10 19:56:08 -070040 sp<IDevicesFactory> defaultFactory{IDevicesFactory::getService()};
41 if (!defaultFactory) {
42 ALOGE("Failed to obtain IDevicesFactory/default service, terminating process.");
Mikhail Naganov1ba40412017-03-20 10:58:07 -070043 exit(1);
Mikhail Naganovd621ac82017-01-12 17:17:45 -080044 }
Kevin Rocarddf9b4202018-05-10 19:56:08 -070045 mDeviceFactories.push_back(defaultFactory);
Kevin Rocard070e7512018-05-22 09:29:13 -070046 if (MAJOR_VERSION >= 4) {
47 // The MSD factory is optional and only available starting at HAL 4.0
48 sp<IDevicesFactory> msdFactory{IDevicesFactory::getService(AUDIO_HAL_SERVICE_NAME_MSD)};
49 if (msdFactory) {
50 mDeviceFactories.push_back(msdFactory);
51 }
52 }
Kevin Rocarddf9b4202018-05-10 19:56:08 -070053 for (const auto& factory : mDeviceFactories) {
54 // It is assumed that the DevicesFactoryHalInterface instance is owned
55 // by AudioFlinger and thus have the same lifespan.
56 factory->linkToDeath(HalDeathHandler::getInstance(), 0 /*cookie*/);
57 }
Mikhail Naganovf558e022016-11-14 17:45:17 -080058}
59
Mikhail Naganovf558e022016-11-14 17:45:17 -080060
Kevin Rocard070e7512018-05-22 09:29:13 -070061#if MAJOR_VERSION == 2
Kevin Rocarddf9b4202018-05-10 19:56:08 -070062static IDevicesFactory::Device idFromHal(const char *name, status_t* status) {
63 *status = OK;
Mikhail Naganovf558e022016-11-14 17:45:17 -080064 if (strcmp(name, AUDIO_HARDWARE_MODULE_ID_PRIMARY) == 0) {
Kevin Rocarddf9b4202018-05-10 19:56:08 -070065 return IDevicesFactory::Device::PRIMARY;
Mikhail Naganovf558e022016-11-14 17:45:17 -080066 } else if(strcmp(name, AUDIO_HARDWARE_MODULE_ID_A2DP) == 0) {
Kevin Rocarddf9b4202018-05-10 19:56:08 -070067 return IDevicesFactory::Device::A2DP;
Mikhail Naganovf558e022016-11-14 17:45:17 -080068 } else if(strcmp(name, AUDIO_HARDWARE_MODULE_ID_USB) == 0) {
Kevin Rocarddf9b4202018-05-10 19:56:08 -070069 return IDevicesFactory::Device::USB;
Mikhail Naganovf558e022016-11-14 17:45:17 -080070 } else if(strcmp(name, AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX) == 0) {
Kevin Rocarddf9b4202018-05-10 19:56:08 -070071 return IDevicesFactory::Device::R_SUBMIX;
Eric Laurentcb9d2eb2017-01-18 17:00:39 -080072 } else if(strcmp(name, AUDIO_HARDWARE_MODULE_ID_STUB) == 0) {
Kevin Rocarddf9b4202018-05-10 19:56:08 -070073 return IDevicesFactory::Device::STUB;
Mikhail Naganovf558e022016-11-14 17:45:17 -080074 }
75 ALOGE("Invalid device name %s", name);
Kevin Rocarddf9b4202018-05-10 19:56:08 -070076 *status = BAD_VALUE;
77 return {};
Mikhail Naganovf558e022016-11-14 17:45:17 -080078}
Kevin Rocard070e7512018-05-22 09:29:13 -070079#elif MAJOR_VERSION == 4
80static const char* idFromHal(const char *name, status_t* status) {
81 *status = OK;
82 return name;
83}
84#endif
Mikhail Naganovf558e022016-11-14 17:45:17 -080085
86status_t DevicesFactoryHalHidl::openDevice(const char *name, sp<DeviceHalInterface> *device) {
Kevin Rocarddf9b4202018-05-10 19:56:08 -070087 if (mDeviceFactories.empty()) return NO_INIT;
88 status_t status;
89 auto hidlId = idFromHal(name, &status);
Mikhail Naganovf558e022016-11-14 17:45:17 -080090 if (status != OK) return status;
91 Result retval = Result::NOT_INITIALIZED;
Kevin Rocarddf9b4202018-05-10 19:56:08 -070092 for (const auto& factory : mDeviceFactories) {
93 Return<void> ret = factory->openDevice(
94 hidlId,
95 [&](Result r, const sp<IDevice>& result) {
96 retval = r;
97 if (retval == Result::OK) {
98 *device = new DeviceHalHidl(result);
99 }
100 });
101 if (!ret.isOk()) return FAILED_TRANSACTION;
102 switch (retval) {
103 // Device was found and was initialized successfully.
104 case Result::OK: return OK;
105 // Device was found but failed to initalize.
106 case Result::NOT_INITIALIZED: return NO_INIT;
107 // Otherwise continue iterating.
108 default: ;
109 }
Mikhail Naganovf558e022016-11-14 17:45:17 -0800110 }
Kevin Rocarddf9b4202018-05-10 19:56:08 -0700111 ALOGW("The specified device name is not recognized: \"%s\"", name);
112 return BAD_VALUE;
Mikhail Naganovf558e022016-11-14 17:45:17 -0800113}
114
Kevin Rocard070e7512018-05-22 09:29:13 -0700115} // namespace CPP_VERSION
Mikhail Naganovf558e022016-11-14 17:45:17 -0800116} // namespace android