DevicesFactory.cpp revision 10548295023bee99108e418499aff09fe578211e
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#define LOG_TAG "DevicesFactoryHAL" 18 19#include <string.h> 20 21#include <utils/Log.h> 22 23#include "Device.h" 24#include "DevicesFactory.h" 25#include "PrimaryDevice.h" 26 27namespace android { 28namespace hardware { 29namespace audio { 30namespace V2_0 { 31namespace implementation { 32 33// static 34const char* DevicesFactory::deviceToString(IDevicesFactory::Device device) { 35 switch (device) { 36 case IDevicesFactory::Device::PRIMARY: return AUDIO_HARDWARE_MODULE_ID_PRIMARY; 37 case IDevicesFactory::Device::A2DP: return AUDIO_HARDWARE_MODULE_ID_A2DP; 38 case IDevicesFactory::Device::USB: return AUDIO_HARDWARE_MODULE_ID_USB; 39 case IDevicesFactory::Device::R_SUBMIX: return AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX; 40 } 41} 42 43// static 44int DevicesFactory::loadAudioInterface(const char *if_name, audio_hw_device_t **dev) 45{ 46 const hw_module_t *mod; 47 int rc; 48 49 rc = hw_get_module_by_class(AUDIO_HARDWARE_MODULE_ID, if_name, &mod); 50 if (rc) { 51 ALOGE("%s couldn't load audio hw module %s.%s (%s)", __func__, 52 AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc)); 53 goto out; 54 } 55 rc = audio_hw_device_open(mod, dev); 56 if (rc) { 57 ALOGE("%s couldn't open audio hw device in %s.%s (%s)", __func__, 58 AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc)); 59 goto out; 60 } 61 if ((*dev)->common.version < AUDIO_DEVICE_API_VERSION_MIN) { 62 ALOGE("%s wrong audio hw device version %04x", __func__, (*dev)->common.version); 63 rc = -EINVAL; 64 audio_hw_device_close(*dev); 65 goto out; 66 } 67 return OK; 68 69out: 70 *dev = NULL; 71 return rc; 72} 73 74// Methods from ::android::hardware::audio::V2_0::IDevicesFactory follow. 75Return<void> DevicesFactory::openDevice(IDevicesFactory::Device device, openDevice_cb _hidl_cb) { 76 audio_hw_device_t *halDevice; 77 int halStatus = loadAudioInterface(deviceToString(device), &halDevice); 78 Result retval(Result::OK); 79 sp<IDevice> result; 80 if (halStatus == OK) { 81 if (device == IDevicesFactory::Device::PRIMARY) { 82 result = new PrimaryDevice(halDevice); 83 } else { 84 result = new ::android::hardware::audio::V2_0::implementation::Device(halDevice); 85 } 86 } else if (halStatus == -EINVAL) { 87 retval = Result::NOT_INITIALIZED; 88 } else { 89 retval = Result::INVALID_ARGUMENTS; 90 } 91 _hidl_cb(retval, result); 92 return Void(); 93} 94 95IDevicesFactory* HIDL_FETCH_IDevicesFactory(const char* /* name */) { 96 return new DevicesFactory(); 97} 98 99} // namespace implementation 100} // namespace V2_0 101} // namespace audio 102} // namespace hardware 103} // namespace android 104