blob: 8825107bbddc99a497abec9e0a01601b75ac2384 [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 }
42}
43
44// static
45int DevicesFactory::loadAudioInterface(const char *if_name, audio_hw_device_t **dev)
46{
47 const hw_module_t *mod;
48 int rc;
49
50 rc = hw_get_module_by_class(AUDIO_HARDWARE_MODULE_ID, if_name, &mod);
51 if (rc) {
52 ALOGE("%s couldn't load audio hw module %s.%s (%s)", __func__,
53 AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc));
54 goto out;
55 }
56 rc = audio_hw_device_open(mod, dev);
57 if (rc) {
58 ALOGE("%s couldn't open audio hw device in %s.%s (%s)", __func__,
59 AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc));
60 goto out;
61 }
62 if ((*dev)->common.version < AUDIO_DEVICE_API_VERSION_MIN) {
63 ALOGE("%s wrong audio hw device version %04x", __func__, (*dev)->common.version);
64 rc = -EINVAL;
65 audio_hw_device_close(*dev);
66 goto out;
67 }
68 return OK;
69
70out:
71 *dev = NULL;
72 return rc;
73}
74
75// Methods from ::android::hardware::audio::V2_0::IDevicesFactory follow.
76Return<void> DevicesFactory::openDevice(IDevicesFactory::Device device, openDevice_cb _hidl_cb) {
77 audio_hw_device_t *halDevice;
78 int halStatus = loadAudioInterface(deviceToString(device), &halDevice);
79 Result retval(Result::OK);
80 sp<IDevice> result;
81 if (halStatus == OK) {
82 if (device == IDevicesFactory::Device::PRIMARY) {
83 result = new PrimaryDevice(halDevice);
84 } else {
85 result = new ::android::hardware::audio::V2_0::implementation::Device(halDevice);
86 }
87 } else if (halStatus == -EINVAL) {
88 retval = Result::NOT_INITIALIZED;
89 } else {
90 retval = Result::INVALID_ARGUMENTS;
91 }
92 _hidl_cb(retval, result);
93 return Void();
94}
95
96IDevicesFactory* HIDL_FETCH_IDevicesFactory(const char* /* name */) {
97 return new DevicesFactory();
98}
99
100} // namespace implementation
101} // namespace V2_0
102} // namespace audio
103} // namespace hardware
104} // namespace android