DevicesFactoryHalHidl.cpp revision 34e5d30e54650b210c1b9730b1e4b412ec1cdb0c
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
25#include "DeviceHalHidl.h"
26#include "DevicesFactoryHalHidl.h"
27
28using ::android::hardware::audio::V2_0::IDevice;
29using ::android::hardware::audio::V2_0::Result;
30using ::android::hardware::Return;
31using ::android::hardware::Status;
32
33namespace android {
34
35// static
36sp<DevicesFactoryHalInterface> DevicesFactoryHalInterface::create() {
37    return new DevicesFactoryHalHidl();
38}
39
40DevicesFactoryHalHidl::DevicesFactoryHalHidl() {
41    mDevicesFactory = IDevicesFactory::getService("audio_devices_factory");
42}
43
44DevicesFactoryHalHidl::~DevicesFactoryHalHidl() {
45}
46
47// static
48status_t DevicesFactoryHalHidl::nameFromHal(const char *name, IDevicesFactory::Device *device) {
49    if (strcmp(name, AUDIO_HARDWARE_MODULE_ID_PRIMARY) == 0) {
50        *device = IDevicesFactory::Device::PRIMARY;
51        return OK;
52    } else if(strcmp(name, AUDIO_HARDWARE_MODULE_ID_A2DP) == 0) {
53        *device = IDevicesFactory::Device::A2DP;
54        return OK;
55    } else if(strcmp(name, AUDIO_HARDWARE_MODULE_ID_USB) == 0) {
56        *device = IDevicesFactory::Device::USB;
57        return OK;
58    } else if(strcmp(name, AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX) == 0) {
59        *device = IDevicesFactory::Device::R_SUBMIX;
60        return OK;
61    }
62    ALOGE("Invalid device name %s", name);
63    return BAD_VALUE;
64}
65
66status_t DevicesFactoryHalHidl::openDevice(const char *name, sp<DeviceHalInterface> *device) {
67    if (mDevicesFactory == 0) return NO_INIT;
68    IDevicesFactory::Device hidlDevice;
69    status_t status = nameFromHal(name, &hidlDevice);
70    if (status != OK) return status;
71    Result retval = Result::NOT_INITIALIZED;
72    Return<void> ret = mDevicesFactory->openDevice(
73            hidlDevice,
74            [&](Result r, const sp<IDevice>& result) {
75                retval = r;
76                if (retval == Result::OK) {
77                    *device = new DeviceHalHidl(result);
78                }
79            });
80    if (ret.getStatus().isOk()) {
81        if (retval == Result::OK) return OK;
82        else if (retval == Result::INVALID_ARGUMENTS) return BAD_VALUE;
83        else return NO_INIT;
84    }
85    return ret.getStatus().transactionError();
86}
87
88} // namespace android
89