blob: 1335a0c36c2214e1cddb8dae58c58870720aef35 [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
Kevin Rocarda8f13932019-06-25 16:08:29 -070038DevicesFactoryHalHidl::DevicesFactoryHalHidl(sp<IDevicesFactory> devicesFactory) {
39 ALOG_ASSERT(devicesFactory != nullptr, "Provided IDevicesFactory service is NULL");
40
41 mDeviceFactories.push_back(devicesFactory);
Kevin Rocard070e7512018-05-22 09:29:13 -070042 if (MAJOR_VERSION >= 4) {
43 // The MSD factory is optional and only available starting at HAL 4.0
44 sp<IDevicesFactory> msdFactory{IDevicesFactory::getService(AUDIO_HAL_SERVICE_NAME_MSD)};
45 if (msdFactory) {
46 mDeviceFactories.push_back(msdFactory);
47 }
48 }
Kevin Rocarddf9b4202018-05-10 19:56:08 -070049 for (const auto& factory : mDeviceFactories) {
50 // It is assumed that the DevicesFactoryHalInterface instance is owned
51 // by AudioFlinger and thus have the same lifespan.
52 factory->linkToDeath(HalDeathHandler::getInstance(), 0 /*cookie*/);
53 }
Mikhail Naganovf558e022016-11-14 17:45:17 -080054}
55
Mikhail Naganovf558e022016-11-14 17:45:17 -080056
Kevin Rocard070e7512018-05-22 09:29:13 -070057#if MAJOR_VERSION == 2
Kevin Rocarddf9b4202018-05-10 19:56:08 -070058static IDevicesFactory::Device idFromHal(const char *name, status_t* status) {
59 *status = OK;
Mikhail Naganovf558e022016-11-14 17:45:17 -080060 if (strcmp(name, AUDIO_HARDWARE_MODULE_ID_PRIMARY) == 0) {
Kevin Rocarddf9b4202018-05-10 19:56:08 -070061 return IDevicesFactory::Device::PRIMARY;
Mikhail Naganovf558e022016-11-14 17:45:17 -080062 } else if(strcmp(name, AUDIO_HARDWARE_MODULE_ID_A2DP) == 0) {
Kevin Rocarddf9b4202018-05-10 19:56:08 -070063 return IDevicesFactory::Device::A2DP;
Mikhail Naganovf558e022016-11-14 17:45:17 -080064 } else if(strcmp(name, AUDIO_HARDWARE_MODULE_ID_USB) == 0) {
Kevin Rocarddf9b4202018-05-10 19:56:08 -070065 return IDevicesFactory::Device::USB;
Mikhail Naganovf558e022016-11-14 17:45:17 -080066 } else if(strcmp(name, AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX) == 0) {
Kevin Rocarddf9b4202018-05-10 19:56:08 -070067 return IDevicesFactory::Device::R_SUBMIX;
Eric Laurentcb9d2eb2017-01-18 17:00:39 -080068 } else if(strcmp(name, AUDIO_HARDWARE_MODULE_ID_STUB) == 0) {
Kevin Rocarddf9b4202018-05-10 19:56:08 -070069 return IDevicesFactory::Device::STUB;
Mikhail Naganovf558e022016-11-14 17:45:17 -080070 }
71 ALOGE("Invalid device name %s", name);
Kevin Rocarddf9b4202018-05-10 19:56:08 -070072 *status = BAD_VALUE;
73 return {};
Mikhail Naganovf558e022016-11-14 17:45:17 -080074}
Kevin Rocard3d48dce2018-11-08 17:16:57 -080075#elif MAJOR_VERSION >= 4
Kevin Rocard070e7512018-05-22 09:29:13 -070076static const char* idFromHal(const char *name, status_t* status) {
77 *status = OK;
78 return name;
79}
80#endif
Mikhail Naganovf558e022016-11-14 17:45:17 -080081
82status_t DevicesFactoryHalHidl::openDevice(const char *name, sp<DeviceHalInterface> *device) {
Kevin Rocarddf9b4202018-05-10 19:56:08 -070083 if (mDeviceFactories.empty()) return NO_INIT;
84 status_t status;
85 auto hidlId = idFromHal(name, &status);
Mikhail Naganovf558e022016-11-14 17:45:17 -080086 if (status != OK) return status;
87 Result retval = Result::NOT_INITIALIZED;
Kevin Rocarddf9b4202018-05-10 19:56:08 -070088 for (const auto& factory : mDeviceFactories) {
89 Return<void> ret = factory->openDevice(
90 hidlId,
91 [&](Result r, const sp<IDevice>& result) {
92 retval = r;
93 if (retval == Result::OK) {
94 *device = new DeviceHalHidl(result);
95 }
96 });
97 if (!ret.isOk()) return FAILED_TRANSACTION;
98 switch (retval) {
99 // Device was found and was initialized successfully.
100 case Result::OK: return OK;
101 // Device was found but failed to initalize.
102 case Result::NOT_INITIALIZED: return NO_INIT;
103 // Otherwise continue iterating.
104 default: ;
105 }
Mikhail Naganovf558e022016-11-14 17:45:17 -0800106 }
Kevin Rocarddf9b4202018-05-10 19:56:08 -0700107 ALOGW("The specified device name is not recognized: \"%s\"", name);
108 return BAD_VALUE;
Mikhail Naganovf558e022016-11-14 17:45:17 -0800109}
110
Kevin Rocard070e7512018-05-22 09:29:13 -0700111} // namespace CPP_VERSION
Mikhail Naganovf558e022016-11-14 17:45:17 -0800112} // namespace android