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 | |
| 23 | #include <android/hardware/audio/2.0/IDevice.h> |
Kevin Rocard | 070e751 | 2018-05-22 09:29:13 -0700 | [diff] [blame^] | 24 | #include <android/hardware/audio/4.0/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 | |
Kevin Rocard | 070e751 | 2018-05-22 09:29:13 -0700 | [diff] [blame^] | 32 | using ::android::hardware::audio::CPP_VERSION::IDevice; |
| 33 | using ::android::hardware::audio::CPP_VERSION::Result; |
Mikhail Naganov | f558e02 | 2016-11-14 17:45:17 -0800 | [diff] [blame] | 34 | using ::android::hardware::Return; |
Mikhail Naganov | f558e02 | 2016-11-14 17:45:17 -0800 | [diff] [blame] | 35 | |
| 36 | namespace android { |
Kevin Rocard | 070e751 | 2018-05-22 09:29:13 -0700 | [diff] [blame^] | 37 | namespace CPP_VERSION { |
Mikhail Naganov | f558e02 | 2016-11-14 17:45:17 -0800 | [diff] [blame] | 38 | |
Mikhail Naganov | f558e02 | 2016-11-14 17:45:17 -0800 | [diff] [blame] | 39 | DevicesFactoryHalHidl::DevicesFactoryHalHidl() { |
Kevin Rocard | df9b420 | 2018-05-10 19:56:08 -0700 | [diff] [blame] | 40 | sp<IDevicesFactory> defaultFactory{IDevicesFactory::getService()}; |
| 41 | if (!defaultFactory) { |
| 42 | ALOGE("Failed to obtain IDevicesFactory/default service, terminating process."); |
Mikhail Naganov | 1ba4041 | 2017-03-20 10:58:07 -0700 | [diff] [blame] | 43 | exit(1); |
Mikhail Naganov | d621ac8 | 2017-01-12 17:17:45 -0800 | [diff] [blame] | 44 | } |
Kevin Rocard | df9b420 | 2018-05-10 19:56:08 -0700 | [diff] [blame] | 45 | mDeviceFactories.push_back(defaultFactory); |
Kevin Rocard | 070e751 | 2018-05-22 09:29:13 -0700 | [diff] [blame^] | 46 | 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 Rocard | df9b420 | 2018-05-10 19:56:08 -0700 | [diff] [blame] | 53 | 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 Naganov | f558e02 | 2016-11-14 17:45:17 -0800 | [diff] [blame] | 58 | } |
| 59 | |
Mikhail Naganov | f558e02 | 2016-11-14 17:45:17 -0800 | [diff] [blame] | 60 | |
Kevin Rocard | 070e751 | 2018-05-22 09:29:13 -0700 | [diff] [blame^] | 61 | #if MAJOR_VERSION == 2 |
Kevin Rocard | df9b420 | 2018-05-10 19:56:08 -0700 | [diff] [blame] | 62 | static IDevicesFactory::Device idFromHal(const char *name, status_t* status) { |
| 63 | *status = OK; |
Mikhail Naganov | f558e02 | 2016-11-14 17:45:17 -0800 | [diff] [blame] | 64 | if (strcmp(name, AUDIO_HARDWARE_MODULE_ID_PRIMARY) == 0) { |
Kevin Rocard | df9b420 | 2018-05-10 19:56:08 -0700 | [diff] [blame] | 65 | return IDevicesFactory::Device::PRIMARY; |
Mikhail Naganov | f558e02 | 2016-11-14 17:45:17 -0800 | [diff] [blame] | 66 | } else if(strcmp(name, AUDIO_HARDWARE_MODULE_ID_A2DP) == 0) { |
Kevin Rocard | df9b420 | 2018-05-10 19:56:08 -0700 | [diff] [blame] | 67 | return IDevicesFactory::Device::A2DP; |
Mikhail Naganov | f558e02 | 2016-11-14 17:45:17 -0800 | [diff] [blame] | 68 | } else if(strcmp(name, AUDIO_HARDWARE_MODULE_ID_USB) == 0) { |
Kevin Rocard | df9b420 | 2018-05-10 19:56:08 -0700 | [diff] [blame] | 69 | return IDevicesFactory::Device::USB; |
Mikhail Naganov | f558e02 | 2016-11-14 17:45:17 -0800 | [diff] [blame] | 70 | } else if(strcmp(name, AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX) == 0) { |
Kevin Rocard | df9b420 | 2018-05-10 19:56:08 -0700 | [diff] [blame] | 71 | return IDevicesFactory::Device::R_SUBMIX; |
Eric Laurent | cb9d2eb | 2017-01-18 17:00:39 -0800 | [diff] [blame] | 72 | } else if(strcmp(name, AUDIO_HARDWARE_MODULE_ID_STUB) == 0) { |
Kevin Rocard | df9b420 | 2018-05-10 19:56:08 -0700 | [diff] [blame] | 73 | return IDevicesFactory::Device::STUB; |
Mikhail Naganov | f558e02 | 2016-11-14 17:45:17 -0800 | [diff] [blame] | 74 | } |
| 75 | ALOGE("Invalid device name %s", name); |
Kevin Rocard | df9b420 | 2018-05-10 19:56:08 -0700 | [diff] [blame] | 76 | *status = BAD_VALUE; |
| 77 | return {}; |
Mikhail Naganov | f558e02 | 2016-11-14 17:45:17 -0800 | [diff] [blame] | 78 | } |
Kevin Rocard | 070e751 | 2018-05-22 09:29:13 -0700 | [diff] [blame^] | 79 | #elif MAJOR_VERSION == 4 |
| 80 | static const char* idFromHal(const char *name, status_t* status) { |
| 81 | *status = OK; |
| 82 | return name; |
| 83 | } |
| 84 | #endif |
Mikhail Naganov | f558e02 | 2016-11-14 17:45:17 -0800 | [diff] [blame] | 85 | |
| 86 | status_t DevicesFactoryHalHidl::openDevice(const char *name, sp<DeviceHalInterface> *device) { |
Kevin Rocard | df9b420 | 2018-05-10 19:56:08 -0700 | [diff] [blame] | 87 | if (mDeviceFactories.empty()) return NO_INIT; |
| 88 | status_t status; |
| 89 | auto hidlId = idFromHal(name, &status); |
Mikhail Naganov | f558e02 | 2016-11-14 17:45:17 -0800 | [diff] [blame] | 90 | if (status != OK) return status; |
| 91 | Result retval = Result::NOT_INITIALIZED; |
Kevin Rocard | df9b420 | 2018-05-10 19:56:08 -0700 | [diff] [blame] | 92 | 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 Naganov | f558e02 | 2016-11-14 17:45:17 -0800 | [diff] [blame] | 110 | } |
Kevin Rocard | df9b420 | 2018-05-10 19:56:08 -0700 | [diff] [blame] | 111 | ALOGW("The specified device name is not recognized: \"%s\"", name); |
| 112 | return BAD_VALUE; |
Mikhail Naganov | f558e02 | 2016-11-14 17:45:17 -0800 | [diff] [blame] | 113 | } |
| 114 | |
Kevin Rocard | 070e751 | 2018-05-22 09:29:13 -0700 | [diff] [blame^] | 115 | } // namespace CPP_VERSION |
Mikhail Naganov | f558e02 | 2016-11-14 17:45:17 -0800 | [diff] [blame] | 116 | } // namespace android |