• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <media/audiohal/hidl/HalDeathHandler.h>
24 #include <utils/Log.h>
25 
26 #include "ConversionHelperHidl.h"
27 #include "DeviceHalHidl.h"
28 #include "DevicesFactoryHalHidl.h"
29 
30 using ::android::hardware::audio::V2_0::IDevice;
31 using ::android::hardware::audio::V2_0::Result;
32 using ::android::hardware::Return;
33 
34 namespace android {
35 
DevicesFactoryHalHidl()36 DevicesFactoryHalHidl::DevicesFactoryHalHidl() {
37     mDevicesFactory = IDevicesFactory::getService();
38     if (mDevicesFactory != 0) {
39         // It is assumed that DevicesFactory is owned by AudioFlinger
40         // and thus have the same lifespan.
41         mDevicesFactory->linkToDeath(HalDeathHandler::getInstance(), 0 /*cookie*/);
42     } else {
43         ALOGE("Failed to obtain IDevicesFactory service, terminating process.");
44         exit(1);
45     }
46     // The MSD factory is optional
47     mDevicesFactoryMsd = IDevicesFactory::getService(AUDIO_HAL_SERVICE_NAME_MSD);
48     // TODO: Register death handler, and add 'restart' directive to audioserver.rc
49 }
50 
~DevicesFactoryHalHidl()51 DevicesFactoryHalHidl::~DevicesFactoryHalHidl() {
52 }
53 
54 // static
nameFromHal(const char * name,IDevicesFactory::Device * device)55 status_t DevicesFactoryHalHidl::nameFromHal(const char *name, IDevicesFactory::Device *device) {
56     if (strcmp(name, AUDIO_HARDWARE_MODULE_ID_PRIMARY) == 0) {
57         *device = IDevicesFactory::Device::PRIMARY;
58         return OK;
59     } else if(strcmp(name, AUDIO_HARDWARE_MODULE_ID_A2DP) == 0) {
60         *device = IDevicesFactory::Device::A2DP;
61         return OK;
62     } else if(strcmp(name, AUDIO_HARDWARE_MODULE_ID_USB) == 0) {
63         *device = IDevicesFactory::Device::USB;
64         return OK;
65     } else if(strcmp(name, AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX) == 0) {
66         *device = IDevicesFactory::Device::R_SUBMIX;
67         return OK;
68     } else if(strcmp(name, AUDIO_HARDWARE_MODULE_ID_STUB) == 0) {
69         *device = IDevicesFactory::Device::STUB;
70         return OK;
71     }
72     ALOGE("Invalid device name %s", name);
73     return BAD_VALUE;
74 }
75 
openDevice(const char * name,sp<DeviceHalInterface> * device)76 status_t DevicesFactoryHalHidl::openDevice(const char *name, sp<DeviceHalInterface> *device) {
77     if (mDevicesFactory == 0) return NO_INIT;
78     IDevicesFactory::Device hidlDevice;
79     status_t status = nameFromHal(name, &hidlDevice);
80     if (status != OK) return status;
81     Result retval = Result::NOT_INITIALIZED;
82     Return<void> ret = mDevicesFactory->openDevice(
83             hidlDevice,
84             [&](Result r, const sp<IDevice>& result) {
85                 retval = r;
86                 if (retval == Result::OK) {
87                     *device = new DeviceHalHidl(result);
88                 }
89             });
90     if (ret.isOk()) {
91         if (retval == Result::OK) return OK;
92         else if (retval == Result::INVALID_ARGUMENTS) return BAD_VALUE;
93         else return NO_INIT;
94     }
95     return FAILED_TRANSACTION;
96 }
97 
98 } // namespace android
99