blob: c566728b26b49ec5e52de16dd67a3214c0c2b417 [file] [log] [blame]
Kevin Rocardd4de2882018-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>
Daniel Van Veenede04672018-03-26 10:57:09 +110018#include <vector>
Kevin Rocardd4de2882018-02-28 14:33:38 -080019
20#define LOG_TAG "DevicesFactoryHalHidl"
21//#define LOG_NDEBUG 0
22
Kevin Rocard2390b5d2018-02-28 14:36:53 -080023#include <android/hardware/audio/4.0/IDevice.h>
Kevin Rocardd4de2882018-02-28 14:33:38 -080024#include <media/audiohal/hidl/HalDeathHandler.h>
25#include <utils/Log.h>
26
27#include "ConversionHelperHidl.h"
28#include "DeviceHalHidl.h"
29#include "DevicesFactoryHalHidl.h"
30
Kevin Rocard2390b5d2018-02-28 14:36:53 -080031using ::android::hardware::audio::V4_0::IDevice;
32using ::android::hardware::audio::V4_0::Result;
Kevin Rocardd4de2882018-02-28 14:33:38 -080033using ::android::hardware::Return;
34
35namespace android {
Kevin Rocard2390b5d2018-02-28 14:36:53 -080036namespace V4_0 {
Kevin Rocardd4de2882018-02-28 14:33:38 -080037
38DevicesFactoryHalHidl::DevicesFactoryHalHidl() {
Daniel Van Veenede04672018-03-26 10:57:09 +110039 sp<IDevicesFactory> defaultFactory{IDevicesFactory::getService()};
40 if (!defaultFactory) {
41 ALOGE("Failed to obtain IDevicesFactory/default service, terminating process.");
Kevin Rocardd4de2882018-02-28 14:33:38 -080042 exit(1);
43 }
Daniel Van Veenede04672018-03-26 10:57:09 +110044 mDeviceFactories.push_back(defaultFactory);
Kevin Rocardd4de2882018-02-28 14:33:38 -080045 // The MSD factory is optional
Daniel Van Veenede04672018-03-26 10:57:09 +110046 sp<IDevicesFactory> msdFactory{IDevicesFactory::getService(AUDIO_HAL_SERVICE_NAME_MSD)};
47 if (msdFactory) {
48 mDeviceFactories.push_back(msdFactory);
49 }
50 for (const auto& factory : mDeviceFactories) {
51 // It is assumed that the DevicesFactoryHalInterface instance is owned
52 // by AudioFlinger and thus have the same lifespan.
53 factory->linkToDeath(HalDeathHandler::getInstance(), 0 /*cookie*/);
54 }
Kevin Rocardd4de2882018-02-28 14:33:38 -080055}
56
Kevin Rocardd4de2882018-02-28 14:33:38 -080057status_t DevicesFactoryHalHidl::openDevice(const char *name, sp<DeviceHalInterface> *device) {
Daniel Van Veenede04672018-03-26 10:57:09 +110058 if (mDeviceFactories.empty()) return NO_INIT;
Kevin Rocardd4de2882018-02-28 14:33:38 -080059 Result retval = Result::NOT_INITIALIZED;
Daniel Van Veenede04672018-03-26 10:57:09 +110060 for (const auto& factory : mDeviceFactories) {
61 Return<void> ret = factory->openDevice(
62 name,
63 [&](Result r, const sp<IDevice>& result) {
64 retval = r;
65 if (retval == Result::OK) {
66 *device = new DeviceHalHidl(result);
67 }
68 });
69 if (!ret.isOk()) return FAILED_TRANSACTION;
70 switch (retval) {
71 // Device was found and was initialized successfully.
72 case Result::OK: return OK;
73 // Device was found but failed to initalize.
74 case Result::NOT_INITIALIZED: return NO_INIT;
75 // Otherwise continue iterating.
76 default: ;
77 }
Kevin Rocardd4de2882018-02-28 14:33:38 -080078 }
Daniel Van Veenede04672018-03-26 10:57:09 +110079 ALOGW("The specified device name is not recognized: \"%s\"", name);
80 return BAD_VALUE;
Kevin Rocardd4de2882018-02-28 14:33:38 -080081}
82
Kevin Rocard2390b5d2018-02-28 14:36:53 -080083} // namespace V4_0
Kevin Rocardd4de2882018-02-28 14:33:38 -080084} // namespace android