blob: 31da2637abe0a70ed144cea93ffd328d70871fa6 [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>
Mikhail Naganovd621ac82017-01-12 17:17:45 -080023#include <media/audiohal/hidl/HalDeathHandler.h>
Mikhail Naganovf558e022016-11-14 17:45:17 -080024#include <utils/Log.h>
25
Mikhail Naganov9f57e3c2016-12-05 12:54:36 -080026#include "ConversionHelperHidl.h"
Mikhail Naganovf558e022016-11-14 17:45:17 -080027#include "DeviceHalHidl.h"
28#include "DevicesFactoryHalHidl.h"
29
30using ::android::hardware::audio::V2_0::IDevice;
31using ::android::hardware::audio::V2_0::Result;
32using ::android::hardware::Return;
Mikhail Naganovf558e022016-11-14 17:45:17 -080033
34namespace android {
35
Mikhail Naganovf558e022016-11-14 17:45:17 -080036DevicesFactoryHalHidl::DevicesFactoryHalHidl() {
Chris Phoenixeea329e2017-01-25 15:04:54 -080037 mDevicesFactory = IDevicesFactory::getService();
Mikhail Naganovd621ac82017-01-12 17:17:45 -080038 if (mDevicesFactory != 0) {
Mikhail Naganov425a5022017-03-14 13:35:37 -070039 // It is assumed that DevicesFactory is owned by AudioFlinger
Mikhail Naganovd621ac82017-01-12 17:17:45 -080040 // and thus have the same lifespan.
41 mDevicesFactory->linkToDeath(HalDeathHandler::getInstance(), 0 /*cookie*/);
Mikhail Naganov425a5022017-03-14 13:35:37 -070042 } else {
Mikhail Naganov1ba40412017-03-20 10:58:07 -070043 ALOGE("Failed to obtain IDevicesFactory service, terminating process.");
44 exit(1);
Mikhail Naganovd621ac82017-01-12 17:17:45 -080045 }
Mikhail Naganovf558e022016-11-14 17:45:17 -080046}
47
48DevicesFactoryHalHidl::~DevicesFactoryHalHidl() {
49}
50
51// static
52status_t DevicesFactoryHalHidl::nameFromHal(const char *name, IDevicesFactory::Device *device) {
53 if (strcmp(name, AUDIO_HARDWARE_MODULE_ID_PRIMARY) == 0) {
54 *device = IDevicesFactory::Device::PRIMARY;
55 return OK;
56 } else if(strcmp(name, AUDIO_HARDWARE_MODULE_ID_A2DP) == 0) {
57 *device = IDevicesFactory::Device::A2DP;
58 return OK;
59 } else if(strcmp(name, AUDIO_HARDWARE_MODULE_ID_USB) == 0) {
60 *device = IDevicesFactory::Device::USB;
61 return OK;
62 } else if(strcmp(name, AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX) == 0) {
63 *device = IDevicesFactory::Device::R_SUBMIX;
64 return OK;
Eric Laurentcb9d2eb2017-01-18 17:00:39 -080065 } else if(strcmp(name, AUDIO_HARDWARE_MODULE_ID_STUB) == 0) {
66 *device = IDevicesFactory::Device::STUB;
67 return OK;
Mikhail Naganovf558e022016-11-14 17:45:17 -080068 }
69 ALOGE("Invalid device name %s", name);
70 return BAD_VALUE;
71}
72
73status_t DevicesFactoryHalHidl::openDevice(const char *name, sp<DeviceHalInterface> *device) {
74 if (mDevicesFactory == 0) return NO_INIT;
75 IDevicesFactory::Device hidlDevice;
76 status_t status = nameFromHal(name, &hidlDevice);
77 if (status != OK) return status;
78 Result retval = Result::NOT_INITIALIZED;
79 Return<void> ret = mDevicesFactory->openDevice(
80 hidlDevice,
81 [&](Result r, const sp<IDevice>& result) {
82 retval = r;
83 if (retval == Result::OK) {
84 *device = new DeviceHalHidl(result);
85 }
86 });
Steven Morelande83be8a2017-01-06 11:06:33 -080087 if (ret.isOk()) {
Mikhail Naganovf558e022016-11-14 17:45:17 -080088 if (retval == Result::OK) return OK;
89 else if (retval == Result::INVALID_ARGUMENTS) return BAD_VALUE;
90 else return NO_INIT;
91 }
Mikhail Naganovd621ac82017-01-12 17:17:45 -080092 return FAILED_TRANSACTION;
Mikhail Naganovf558e022016-11-14 17:45:17 -080093}
94
95} // namespace android