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> |
| 18 | |
| 19 | #define LOG_TAG "DevicesFactoryHalHidl" |
| 20 | //#define LOG_NDEBUG 0 |
| 21 | |
| 22 | #include <android/hardware/audio/2.0/IDevice.h> |
| 23 | #include <utils/Log.h> |
| 24 | |
Mikhail Naganov | 9f57e3c | 2016-12-05 12:54:36 -0800 | [diff] [blame] | 25 | #include "ConversionHelperHidl.h" |
Mikhail Naganov | f558e02 | 2016-11-14 17:45:17 -0800 | [diff] [blame] | 26 | #include "DeviceHalHidl.h" |
| 27 | #include "DevicesFactoryHalHidl.h" |
| 28 | |
| 29 | using ::android::hardware::audio::V2_0::IDevice; |
| 30 | using ::android::hardware::audio::V2_0::Result; |
| 31 | using ::android::hardware::Return; |
| 32 | using ::android::hardware::Status; |
| 33 | |
| 34 | namespace android { |
| 35 | |
| 36 | // static |
| 37 | sp<DevicesFactoryHalInterface> DevicesFactoryHalInterface::create() { |
| 38 | return new DevicesFactoryHalHidl(); |
| 39 | } |
| 40 | |
| 41 | DevicesFactoryHalHidl::DevicesFactoryHalHidl() { |
| 42 | mDevicesFactory = IDevicesFactory::getService("audio_devices_factory"); |
| 43 | } |
| 44 | |
| 45 | DevicesFactoryHalHidl::~DevicesFactoryHalHidl() { |
| 46 | } |
| 47 | |
| 48 | // static |
| 49 | status_t DevicesFactoryHalHidl::nameFromHal(const char *name, IDevicesFactory::Device *device) { |
| 50 | if (strcmp(name, AUDIO_HARDWARE_MODULE_ID_PRIMARY) == 0) { |
| 51 | *device = IDevicesFactory::Device::PRIMARY; |
| 52 | return OK; |
| 53 | } else if(strcmp(name, AUDIO_HARDWARE_MODULE_ID_A2DP) == 0) { |
| 54 | *device = IDevicesFactory::Device::A2DP; |
| 55 | return OK; |
| 56 | } else if(strcmp(name, AUDIO_HARDWARE_MODULE_ID_USB) == 0) { |
| 57 | *device = IDevicesFactory::Device::USB; |
| 58 | return OK; |
| 59 | } else if(strcmp(name, AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX) == 0) { |
| 60 | *device = IDevicesFactory::Device::R_SUBMIX; |
| 61 | return OK; |
| 62 | } |
| 63 | ALOGE("Invalid device name %s", name); |
| 64 | return BAD_VALUE; |
| 65 | } |
| 66 | |
| 67 | status_t DevicesFactoryHalHidl::openDevice(const char *name, sp<DeviceHalInterface> *device) { |
| 68 | if (mDevicesFactory == 0) return NO_INIT; |
| 69 | IDevicesFactory::Device hidlDevice; |
| 70 | status_t status = nameFromHal(name, &hidlDevice); |
| 71 | if (status != OK) return status; |
| 72 | Result retval = Result::NOT_INITIALIZED; |
| 73 | Return<void> ret = mDevicesFactory->openDevice( |
| 74 | hidlDevice, |
| 75 | [&](Result r, const sp<IDevice>& result) { |
| 76 | retval = r; |
| 77 | if (retval == Result::OK) { |
| 78 | *device = new DeviceHalHidl(result); |
| 79 | } |
| 80 | }); |
Steven Moreland | e83be8a | 2017-01-06 11:06:33 -0800 | [diff] [blame^] | 81 | if (ret.isOk()) { |
Mikhail Naganov | f558e02 | 2016-11-14 17:45:17 -0800 | [diff] [blame] | 82 | if (retval == Result::OK) return OK; |
| 83 | else if (retval == Result::INVALID_ARGUMENTS) return BAD_VALUE; |
| 84 | else return NO_INIT; |
| 85 | } |
Steven Moreland | e83be8a | 2017-01-06 11:06:33 -0800 | [diff] [blame^] | 86 | return UNKNOWN_ERROR; |
Mikhail Naganov | f558e02 | 2016-11-14 17:45:17 -0800 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | } // namespace android |