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 "DevicesFactoryHalLocal"
18//#define LOG_NDEBUG 0
19
20#include <string.h>
21
22#include <hardware/audio.h>
23#include <utils/Log.h>
24
25#include "DeviceHalLocal.h"
26#include "DevicesFactoryHalLocal.h"
27
28namespace android {
29
30static status_t load_audio_interface(const char *if_name, audio_hw_device_t **dev)
31{
32    const hw_module_t *mod;
33    int rc;
34
35    rc = hw_get_module_by_class(AUDIO_HARDWARE_MODULE_ID, if_name, &mod);
36    if (rc) {
37        ALOGE("%s couldn't load audio hw module %s.%s (%s)", __func__,
38                AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc));
39        goto out;
40    }
41    rc = audio_hw_device_open(mod, dev);
42    if (rc) {
43        ALOGE("%s couldn't open audio hw device in %s.%s (%s)", __func__,
44                AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc));
45        goto out;
46    }
47    if ((*dev)->common.version < AUDIO_DEVICE_API_VERSION_MIN) {
48        ALOGE("%s wrong audio hw device version %04x", __func__, (*dev)->common.version);
49        rc = BAD_VALUE;
50        audio_hw_device_close(*dev);
51        goto out;
52    }
53    return OK;
54
55out:
56    *dev = NULL;
57    return rc;
58}
59
60status_t DevicesFactoryHalLocal::openDevice(const char *name, sp<DeviceHalInterface> *device) {
61    audio_hw_device_t *dev;
62    status_t rc = load_audio_interface(name, &dev);
63    if (rc == OK) {
64        *device = new DeviceHalLocal(dev);
65    }
66    return rc;
67}
68
69} // namespace android
70