blob: b344968dd92d0803308373e823e2471681ef13dd [file] [log] [blame]
Mikhail Naganov10548292016-10-31 10:39:47 -07001/*
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
Yifan Hongf9d30342016-11-30 13:45:34 -080021#include <android/log.h>
Mikhail Naganov10548292016-10-31 10:39:47 -070022
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;
Eric Laurentf247b8d2017-01-18 17:04:48 -080040 case IDevicesFactory::Device::STUB: return AUDIO_HARDWARE_MODULE_ID_STUB;
Mikhail Naganov10548292016-10-31 10:39:47 -070041 }
Mikhail Naganov8604a732017-04-24 09:29:22 -070042 return nullptr;
Mikhail Naganov10548292016-10-31 10:39:47 -070043}
44
45// static
46int DevicesFactory::loadAudioInterface(const char *if_name, audio_hw_device_t **dev)
47{
48 const hw_module_t *mod;
49 int rc;
50
51 rc = hw_get_module_by_class(AUDIO_HARDWARE_MODULE_ID, if_name, &mod);
52 if (rc) {
53 ALOGE("%s couldn't load audio hw module %s.%s (%s)", __func__,
54 AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc));
55 goto out;
56 }
57 rc = audio_hw_device_open(mod, dev);
58 if (rc) {
59 ALOGE("%s couldn't open audio hw device in %s.%s (%s)", __func__,
60 AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc));
61 goto out;
62 }
63 if ((*dev)->common.version < AUDIO_DEVICE_API_VERSION_MIN) {
64 ALOGE("%s wrong audio hw device version %04x", __func__, (*dev)->common.version);
65 rc = -EINVAL;
66 audio_hw_device_close(*dev);
67 goto out;
68 }
69 return OK;
70
71out:
72 *dev = NULL;
73 return rc;
74}
75
76// Methods from ::android::hardware::audio::V2_0::IDevicesFactory follow.
77Return<void> DevicesFactory::openDevice(IDevicesFactory::Device device, openDevice_cb _hidl_cb) {
78 audio_hw_device_t *halDevice;
Mikhail Naganov8604a732017-04-24 09:29:22 -070079 Result retval(Result::INVALID_ARGUMENTS);
Mikhail Naganov10548292016-10-31 10:39:47 -070080 sp<IDevice> result;
Mikhail Naganov8604a732017-04-24 09:29:22 -070081 const char* moduleName = deviceToString(device);
82 if (moduleName != nullptr) {
83 int halStatus = loadAudioInterface(moduleName, &halDevice);
84 if (halStatus == OK) {
85 if (device == IDevicesFactory::Device::PRIMARY) {
86 result = new PrimaryDevice(halDevice);
87 } else {
88 result = new ::android::hardware::audio::V2_0::implementation::
Mikhail Naganov6c0f76a2017-05-03 12:08:22 -070089 Device(halDevice, moduleName);
Mikhail Naganov8604a732017-04-24 09:29:22 -070090 }
91 retval = Result::OK;
92 } else if (halStatus == -EINVAL) {
93 retval = Result::NOT_INITIALIZED;
Mikhail Naganov10548292016-10-31 10:39:47 -070094 }
Mikhail Naganov10548292016-10-31 10:39:47 -070095 }
96 _hidl_cb(retval, result);
97 return Void();
98}
99
100IDevicesFactory* HIDL_FETCH_IDevicesFactory(const char* /* name */) {
101 return new DevicesFactory();
102}
103
104} // namespace implementation
105} // namespace V2_0
106} // namespace audio
107} // namespace hardware
108} // namespace android