blob: 644407920359f5c5202b475932aa784b784e855e [file] [log] [blame]
Mikhail Naganovf558e022016-11-14 17:45:17 -08001/*
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
Mikhail Naganov9f57e3c2016-12-05 12:54:36 -080025#include "ConversionHelperHidl.h"
Mikhail Naganovf558e022016-11-14 17:45:17 -080026#include "DeviceHalHidl.h"
27#include "DevicesFactoryHalHidl.h"
28
29using ::android::hardware::audio::V2_0::IDevice;
30using ::android::hardware::audio::V2_0::Result;
31using ::android::hardware::Return;
32using ::android::hardware::Status;
33
34namespace android {
35
36// static
37sp<DevicesFactoryHalInterface> DevicesFactoryHalInterface::create() {
38 return new DevicesFactoryHalHidl();
39}
40
41DevicesFactoryHalHidl::DevicesFactoryHalHidl() {
42 mDevicesFactory = IDevicesFactory::getService("audio_devices_factory");
43}
44
45DevicesFactoryHalHidl::~DevicesFactoryHalHidl() {
46}
47
48// static
49status_t DevicesFactoryHalHidl::nameFromHal(const char *name, IDevicesFactory::Device *device) {
50 if (strcmp(name, AUDIO_HARDWARE_MODULE_ID_PRIMARY) == 0) {
51 *device = IDevicesFactory::Device::PRIMARY;
52 return OK;
53 } else if(strcmp(name, AUDIO_HARDWARE_MODULE_ID_A2DP) == 0) {
54 *device = IDevicesFactory::Device::A2DP;
55 return OK;
56 } else if(strcmp(name, AUDIO_HARDWARE_MODULE_ID_USB) == 0) {
57 *device = IDevicesFactory::Device::USB;
58 return OK;
59 } else if(strcmp(name, AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX) == 0) {
60 *device = IDevicesFactory::Device::R_SUBMIX;
61 return OK;
62 }
63 ALOGE("Invalid device name %s", name);
64 return BAD_VALUE;
65}
66
67status_t DevicesFactoryHalHidl::openDevice(const char *name, sp<DeviceHalInterface> *device) {
68 if (mDevicesFactory == 0) return NO_INIT;
69 IDevicesFactory::Device hidlDevice;
70 status_t status = nameFromHal(name, &hidlDevice);
71 if (status != OK) return status;
72 Result retval = Result::NOT_INITIALIZED;
73 Return<void> ret = mDevicesFactory->openDevice(
74 hidlDevice,
75 [&](Result r, const sp<IDevice>& result) {
76 retval = r;
77 if (retval == Result::OK) {
78 *device = new DeviceHalHidl(result);
79 }
80 });
Steven Morelande83be8a2017-01-06 11:06:33 -080081 if (ret.isOk()) {
Mikhail Naganovf558e022016-11-14 17:45:17 -080082 if (retval == Result::OK) return OK;
83 else if (retval == Result::INVALID_ARGUMENTS) return BAD_VALUE;
84 else return NO_INIT;
85 }
Steven Morelande83be8a2017-01-06 11:06:33 -080086 return UNKNOWN_ERROR;
Mikhail Naganovf558e022016-11-14 17:45:17 -080087}
88
89} // namespace android