Kevin Rocard | d4de288 | 2018-02-28 14:33:38 -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> |
Daniel Van Veen | ede0467 | 2018-03-26 10:57:09 +1100 | [diff] [blame^] | 18 | #include <vector> |
Kevin Rocard | d4de288 | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 19 | |
| 20 | #define LOG_TAG "DevicesFactoryHalHidl" |
| 21 | //#define LOG_NDEBUG 0 |
| 22 | |
Kevin Rocard | 2390b5d | 2018-02-28 14:36:53 -0800 | [diff] [blame] | 23 | #include <android/hardware/audio/4.0/IDevice.h> |
Kevin Rocard | d4de288 | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 24 | #include <media/audiohal/hidl/HalDeathHandler.h> |
| 25 | #include <utils/Log.h> |
| 26 | |
| 27 | #include "ConversionHelperHidl.h" |
| 28 | #include "DeviceHalHidl.h" |
| 29 | #include "DevicesFactoryHalHidl.h" |
| 30 | |
Kevin Rocard | 2390b5d | 2018-02-28 14:36:53 -0800 | [diff] [blame] | 31 | using ::android::hardware::audio::V4_0::IDevice; |
| 32 | using ::android::hardware::audio::V4_0::Result; |
Kevin Rocard | d4de288 | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 33 | using ::android::hardware::Return; |
| 34 | |
| 35 | namespace android { |
Kevin Rocard | 2390b5d | 2018-02-28 14:36:53 -0800 | [diff] [blame] | 36 | namespace V4_0 { |
Kevin Rocard | d4de288 | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 37 | |
| 38 | DevicesFactoryHalHidl::DevicesFactoryHalHidl() { |
Daniel Van Veen | ede0467 | 2018-03-26 10:57:09 +1100 | [diff] [blame^] | 39 | sp<IDevicesFactory> defaultFactory{IDevicesFactory::getService()}; |
| 40 | if (!defaultFactory) { |
| 41 | ALOGE("Failed to obtain IDevicesFactory/default service, terminating process."); |
Kevin Rocard | d4de288 | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 42 | exit(1); |
| 43 | } |
Daniel Van Veen | ede0467 | 2018-03-26 10:57:09 +1100 | [diff] [blame^] | 44 | mDeviceFactories.push_back(defaultFactory); |
Kevin Rocard | d4de288 | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 45 | // The MSD factory is optional |
Daniel Van Veen | ede0467 | 2018-03-26 10:57:09 +1100 | [diff] [blame^] | 46 | sp<IDevicesFactory> msdFactory{IDevicesFactory::getService(AUDIO_HAL_SERVICE_NAME_MSD)}; |
| 47 | if (msdFactory) { |
| 48 | mDeviceFactories.push_back(msdFactory); |
| 49 | } |
| 50 | for (const auto& factory : mDeviceFactories) { |
| 51 | // It is assumed that the DevicesFactoryHalInterface instance is owned |
| 52 | // by AudioFlinger and thus have the same lifespan. |
| 53 | factory->linkToDeath(HalDeathHandler::getInstance(), 0 /*cookie*/); |
| 54 | } |
Kevin Rocard | d4de288 | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 55 | } |
| 56 | |
Kevin Rocard | d4de288 | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 57 | status_t DevicesFactoryHalHidl::openDevice(const char *name, sp<DeviceHalInterface> *device) { |
Daniel Van Veen | ede0467 | 2018-03-26 10:57:09 +1100 | [diff] [blame^] | 58 | if (mDeviceFactories.empty()) return NO_INIT; |
Kevin Rocard | d4de288 | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 59 | Result retval = Result::NOT_INITIALIZED; |
Daniel Van Veen | ede0467 | 2018-03-26 10:57:09 +1100 | [diff] [blame^] | 60 | for (const auto& factory : mDeviceFactories) { |
| 61 | Return<void> ret = factory->openDevice( |
| 62 | name, |
| 63 | [&](Result r, const sp<IDevice>& result) { |
| 64 | retval = r; |
| 65 | if (retval == Result::OK) { |
| 66 | *device = new DeviceHalHidl(result); |
| 67 | } |
| 68 | }); |
| 69 | if (!ret.isOk()) return FAILED_TRANSACTION; |
| 70 | switch (retval) { |
| 71 | // Device was found and was initialized successfully. |
| 72 | case Result::OK: return OK; |
| 73 | // Device was found but failed to initalize. |
| 74 | case Result::NOT_INITIALIZED: return NO_INIT; |
| 75 | // Otherwise continue iterating. |
| 76 | default: ; |
| 77 | } |
Kevin Rocard | d4de288 | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 78 | } |
Daniel Van Veen | ede0467 | 2018-03-26 10:57:09 +1100 | [diff] [blame^] | 79 | ALOGW("The specified device name is not recognized: \"%s\"", name); |
| 80 | return BAD_VALUE; |
Kevin Rocard | d4de288 | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 81 | } |
| 82 | |
Kevin Rocard | 2390b5d | 2018-02-28 14:36:53 -0800 | [diff] [blame] | 83 | } // namespace V4_0 |
Kevin Rocard | d4de288 | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 84 | } // namespace android |