blob: c30da3c3100e00c0089038b346be167fa3a93ec3 [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
Eric Laurent42896a02019-09-27 15:40:33 -070023#include "android/hidl/manager/1.0/IServiceManager.h"
Kevin Rocard95213bf2018-11-08 17:16:57 -080024#include PATH(android/hardware/audio/FILE_VERSION/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
Eric Laurent42896a02019-09-27 15:40:33 -070032#include <set>
33
Kevin Rocard070e7512018-05-22 09:29:13 -070034using ::android::hardware::audio::CPP_VERSION::IDevice;
35using ::android::hardware::audio::CPP_VERSION::Result;
Mikhail Naganovf558e022016-11-14 17:45:17 -080036using ::android::hardware::Return;
Mikhail Naganovf558e022016-11-14 17:45:17 -080037
38namespace android {
Kevin Rocard070e7512018-05-22 09:29:13 -070039namespace CPP_VERSION {
Mikhail Naganovf558e022016-11-14 17:45:17 -080040
Kevin Rocardb9448c72019-06-25 16:08:29 -070041DevicesFactoryHalHidl::DevicesFactoryHalHidl(sp<IDevicesFactory> devicesFactory) {
42 ALOG_ASSERT(devicesFactory != nullptr, "Provided IDevicesFactory service is NULL");
43
44 mDeviceFactories.push_back(devicesFactory);
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 Rocard3d48dce2018-11-08 17:16:57 -080078#elif MAJOR_VERSION >= 4
Kevin Rocard070e7512018-05-22 09:29:13 -070079static 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
Eric Laurent42896a02019-09-27 15:40:33 -0700114status_t DevicesFactoryHalHidl::getHalPids(std::vector<pid_t> *pids) {
115 std::set<pid_t> pidsSet;
116
117 for (const auto& factory : mDeviceFactories) {
118 using ::android::hidl::base::V1_0::DebugInfo;
119 using android::hidl::manager::V1_0::IServiceManager;
120
121 DebugInfo debugInfo;
122 auto ret = factory->getDebugInfo([&] (const auto &info) {
123 debugInfo = info;
124 });
125 if (!ret.isOk()) {
126 return INVALID_OPERATION;
127 }
128 if (debugInfo.pid == (int)IServiceManager::PidConstant::NO_PID) {
129 continue;
130 }
131 pidsSet.insert(debugInfo.pid);
132 }
133
134 *pids = {pidsSet.begin(), pidsSet.end()};
135 return NO_ERROR;
136}
137
Kevin Rocard070e7512018-05-22 09:29:13 -0700138} // namespace CPP_VERSION
Mikhail Naganovf558e022016-11-14 17:45:17 -0800139} // namespace android