Mikhail Naganov | f558e02 | 2016-11-14 17:45:17 -0800 | [diff] [blame] | 1 | /* |
| 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 Rocard | df9b420 | 2018-05-10 19:56:08 -0700 | [diff] [blame] | 18 | #include <vector> |
Mikhail Naganov | f558e02 | 2016-11-14 17:45:17 -0800 | [diff] [blame] | 19 | |
| 20 | #define LOG_TAG "DevicesFactoryHalHidl" |
| 21 | //#define LOG_NDEBUG 0 |
| 22 | |
Eric Laurent | 42896a0 | 2019-09-27 15:40:33 -0700 | [diff] [blame] | 23 | #include "android/hidl/manager/1.0/IServiceManager.h" |
Kevin Rocard | 95213bf | 2018-11-08 17:16:57 -0800 | [diff] [blame] | 24 | #include PATH(android/hardware/audio/FILE_VERSION/IDevice.h) |
Mikhail Naganov | d621ac8 | 2017-01-12 17:17:45 -0800 | [diff] [blame] | 25 | #include <media/audiohal/hidl/HalDeathHandler.h> |
Mikhail Naganov | f558e02 | 2016-11-14 17:45:17 -0800 | [diff] [blame] | 26 | #include <utils/Log.h> |
| 27 | |
Mikhail Naganov | 9f57e3c | 2016-12-05 12:54:36 -0800 | [diff] [blame] | 28 | #include "ConversionHelperHidl.h" |
Mikhail Naganov | f558e02 | 2016-11-14 17:45:17 -0800 | [diff] [blame] | 29 | #include "DeviceHalHidl.h" |
| 30 | #include "DevicesFactoryHalHidl.h" |
| 31 | |
Eric Laurent | 42896a0 | 2019-09-27 15:40:33 -0700 | [diff] [blame] | 32 | #include <set> |
| 33 | |
Kevin Rocard | 070e751 | 2018-05-22 09:29:13 -0700 | [diff] [blame] | 34 | using ::android::hardware::audio::CPP_VERSION::IDevice; |
| 35 | using ::android::hardware::audio::CPP_VERSION::Result; |
Mikhail Naganov | f558e02 | 2016-11-14 17:45:17 -0800 | [diff] [blame] | 36 | using ::android::hardware::Return; |
Mikhail Naganov | f558e02 | 2016-11-14 17:45:17 -0800 | [diff] [blame] | 37 | |
| 38 | namespace android { |
Kevin Rocard | 070e751 | 2018-05-22 09:29:13 -0700 | [diff] [blame] | 39 | namespace CPP_VERSION { |
Mikhail Naganov | f558e02 | 2016-11-14 17:45:17 -0800 | [diff] [blame] | 40 | |
Kevin Rocard | b9448c7 | 2019-06-25 16:08:29 -0700 | [diff] [blame] | 41 | DevicesFactoryHalHidl::DevicesFactoryHalHidl(sp<IDevicesFactory> devicesFactory) { |
| 42 | ALOG_ASSERT(devicesFactory != nullptr, "Provided IDevicesFactory service is NULL"); |
| 43 | |
| 44 | mDeviceFactories.push_back(devicesFactory); |
Kevin Rocard | 070e751 | 2018-05-22 09:29:13 -0700 | [diff] [blame] | 45 | 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 Rocard | df9b420 | 2018-05-10 19:56:08 -0700 | [diff] [blame] | 52 | 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 Naganov | f558e02 | 2016-11-14 17:45:17 -0800 | [diff] [blame] | 57 | } |
| 58 | |
Mikhail Naganov | f558e02 | 2016-11-14 17:45:17 -0800 | [diff] [blame] | 59 | |
Kevin Rocard | 070e751 | 2018-05-22 09:29:13 -0700 | [diff] [blame] | 60 | #if MAJOR_VERSION == 2 |
Kevin Rocard | df9b420 | 2018-05-10 19:56:08 -0700 | [diff] [blame] | 61 | static IDevicesFactory::Device idFromHal(const char *name, status_t* status) { |
| 62 | *status = OK; |
Mikhail Naganov | f558e02 | 2016-11-14 17:45:17 -0800 | [diff] [blame] | 63 | if (strcmp(name, AUDIO_HARDWARE_MODULE_ID_PRIMARY) == 0) { |
Kevin Rocard | df9b420 | 2018-05-10 19:56:08 -0700 | [diff] [blame] | 64 | return IDevicesFactory::Device::PRIMARY; |
Mikhail Naganov | f558e02 | 2016-11-14 17:45:17 -0800 | [diff] [blame] | 65 | } else if(strcmp(name, AUDIO_HARDWARE_MODULE_ID_A2DP) == 0) { |
Kevin Rocard | df9b420 | 2018-05-10 19:56:08 -0700 | [diff] [blame] | 66 | return IDevicesFactory::Device::A2DP; |
Mikhail Naganov | f558e02 | 2016-11-14 17:45:17 -0800 | [diff] [blame] | 67 | } else if(strcmp(name, AUDIO_HARDWARE_MODULE_ID_USB) == 0) { |
Kevin Rocard | df9b420 | 2018-05-10 19:56:08 -0700 | [diff] [blame] | 68 | return IDevicesFactory::Device::USB; |
Mikhail Naganov | f558e02 | 2016-11-14 17:45:17 -0800 | [diff] [blame] | 69 | } else if(strcmp(name, AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX) == 0) { |
Kevin Rocard | df9b420 | 2018-05-10 19:56:08 -0700 | [diff] [blame] | 70 | return IDevicesFactory::Device::R_SUBMIX; |
Eric Laurent | cb9d2eb | 2017-01-18 17:00:39 -0800 | [diff] [blame] | 71 | } else if(strcmp(name, AUDIO_HARDWARE_MODULE_ID_STUB) == 0) { |
Kevin Rocard | df9b420 | 2018-05-10 19:56:08 -0700 | [diff] [blame] | 72 | return IDevicesFactory::Device::STUB; |
Mikhail Naganov | f558e02 | 2016-11-14 17:45:17 -0800 | [diff] [blame] | 73 | } |
| 74 | ALOGE("Invalid device name %s", name); |
Kevin Rocard | df9b420 | 2018-05-10 19:56:08 -0700 | [diff] [blame] | 75 | *status = BAD_VALUE; |
| 76 | return {}; |
Mikhail Naganov | f558e02 | 2016-11-14 17:45:17 -0800 | [diff] [blame] | 77 | } |
Kevin Rocard | 3d48dce | 2018-11-08 17:16:57 -0800 | [diff] [blame] | 78 | #elif MAJOR_VERSION >= 4 |
Kevin Rocard | 070e751 | 2018-05-22 09:29:13 -0700 | [diff] [blame] | 79 | static const char* idFromHal(const char *name, status_t* status) { |
| 80 | *status = OK; |
| 81 | return name; |
| 82 | } |
| 83 | #endif |
Mikhail Naganov | f558e02 | 2016-11-14 17:45:17 -0800 | [diff] [blame] | 84 | |
| 85 | status_t DevicesFactoryHalHidl::openDevice(const char *name, sp<DeviceHalInterface> *device) { |
Kevin Rocard | df9b420 | 2018-05-10 19:56:08 -0700 | [diff] [blame] | 86 | if (mDeviceFactories.empty()) return NO_INIT; |
| 87 | status_t status; |
| 88 | auto hidlId = idFromHal(name, &status); |
Mikhail Naganov | f558e02 | 2016-11-14 17:45:17 -0800 | [diff] [blame] | 89 | if (status != OK) return status; |
| 90 | Result retval = Result::NOT_INITIALIZED; |
Kevin Rocard | df9b420 | 2018-05-10 19:56:08 -0700 | [diff] [blame] | 91 | 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 Naganov | f558e02 | 2016-11-14 17:45:17 -0800 | [diff] [blame] | 109 | } |
Kevin Rocard | df9b420 | 2018-05-10 19:56:08 -0700 | [diff] [blame] | 110 | ALOGW("The specified device name is not recognized: \"%s\"", name); |
| 111 | return BAD_VALUE; |
Mikhail Naganov | f558e02 | 2016-11-14 17:45:17 -0800 | [diff] [blame] | 112 | } |
| 113 | |
Eric Laurent | 42896a0 | 2019-09-27 15:40:33 -0700 | [diff] [blame] | 114 | status_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 Rocard | 070e751 | 2018-05-22 09:29:13 -0700 | [diff] [blame] | 138 | } // namespace CPP_VERSION |
Mikhail Naganov | f558e02 | 2016-11-14 17:45:17 -0800 | [diff] [blame] | 139 | } // namespace android |