blob: c83194e91eff9235769a43fcca9d12b0f9444bbd [file] [log] [blame]
Kevin Rocard4bcd67f2018-02-28 14:33:38 -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
Kevin Rocard51e076a2018-02-28 14:36:53 -080022#include <android/hardware/audio/4.0/IDevice.h>
Kevin Rocard4bcd67f2018-02-28 14:33:38 -080023#include <media/audiohal/hidl/HalDeathHandler.h>
24#include <utils/Log.h>
25
26#include "ConversionHelperHidl.h"
27#include "DeviceHalHidl.h"
28#include "DevicesFactoryHalHidl.h"
29
Kevin Rocard51e076a2018-02-28 14:36:53 -080030using ::android::hardware::audio::V4_0::IDevice;
31using ::android::hardware::audio::V4_0::Result;
Kevin Rocard4bcd67f2018-02-28 14:33:38 -080032using ::android::hardware::Return;
33
34namespace android {
Kevin Rocard51e076a2018-02-28 14:36:53 -080035namespace V4_0 {
Kevin Rocard4bcd67f2018-02-28 14:33:38 -080036
37DevicesFactoryHalHidl::DevicesFactoryHalHidl() {
38 mDevicesFactory = IDevicesFactory::getService();
39 if (mDevicesFactory != 0) {
40 // It is assumed that DevicesFactory is owned by AudioFlinger
41 // and thus have the same lifespan.
42 mDevicesFactory->linkToDeath(HalDeathHandler::getInstance(), 0 /*cookie*/);
43 } else {
44 ALOGE("Failed to obtain IDevicesFactory service, terminating process.");
45 exit(1);
46 }
47 // The MSD factory is optional
48 mDevicesFactoryMsd = IDevicesFactory::getService(AUDIO_HAL_SERVICE_NAME_MSD);
49 // TODO: Register death handler, and add 'restart' directive to audioserver.rc
50}
51
52DevicesFactoryHalHidl::~DevicesFactoryHalHidl() {
53}
54
Kevin Rocard4bcd67f2018-02-28 14:33:38 -080055status_t DevicesFactoryHalHidl::openDevice(const char *name, sp<DeviceHalInterface> *device) {
56 if (mDevicesFactory == 0) return NO_INIT;
Kevin Rocard4bcd67f2018-02-28 14:33:38 -080057 Result retval = Result::NOT_INITIALIZED;
58 Return<void> ret = mDevicesFactory->openDevice(
Kevin Rocardb9cfbf12018-02-23 19:11:06 -080059 name,
Kevin Rocard4bcd67f2018-02-28 14:33:38 -080060 [&](Result r, const sp<IDevice>& result) {
61 retval = r;
62 if (retval == Result::OK) {
63 *device = new DeviceHalHidl(result);
64 }
65 });
66 if (ret.isOk()) {
67 if (retval == Result::OK) return OK;
68 else if (retval == Result::INVALID_ARGUMENTS) return BAD_VALUE;
69 else return NO_INIT;
70 }
71 return FAILED_TRANSACTION;
72}
73
Kevin Rocard51e076a2018-02-28 14:36:53 -080074} // namespace V4_0
Kevin Rocard4bcd67f2018-02-28 14:33:38 -080075} // namespace android