1 /*
2 * Copyright (C) 2018 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 #define LOG_TAG "DevicesFactoryHAL"
18
19 #include "core/default/DevicesFactory.h"
20 #include "core/default/Device.h"
21 #include "core/default/PrimaryDevice.h"
22
23 #include <string.h>
24
25 #include <android/log.h>
26
27 namespace android {
28 namespace hardware {
29 namespace audio {
30 namespace CPP_VERSION {
31 namespace implementation {
32
33 #if MAJOR_VERSION == 2
openDevice(IDevicesFactory::Device device,openDevice_cb _hidl_cb)34 Return<void> DevicesFactory::openDevice(IDevicesFactory::Device device, openDevice_cb _hidl_cb) {
35 switch (device) {
36 case IDevicesFactory::Device::PRIMARY:
37 return openDevice<PrimaryDevice>(AUDIO_HARDWARE_MODULE_ID_PRIMARY, _hidl_cb);
38 case IDevicesFactory::Device::A2DP:
39 return openDevice(AUDIO_HARDWARE_MODULE_ID_A2DP, _hidl_cb);
40 case IDevicesFactory::Device::USB:
41 return openDevice(AUDIO_HARDWARE_MODULE_ID_USB, _hidl_cb);
42 case IDevicesFactory::Device::R_SUBMIX:
43 return openDevice(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, _hidl_cb);
44 case IDevicesFactory::Device::STUB:
45 return openDevice(AUDIO_HARDWARE_MODULE_ID_STUB, _hidl_cb);
46 }
47 _hidl_cb(Result::INVALID_ARGUMENTS, nullptr);
48 return Void();
49 }
50 #elif MAJOR_VERSION >= 4
51 Return<void> DevicesFactory::openDevice(const hidl_string& moduleName, openDevice_cb _hidl_cb) {
52 if (moduleName == AUDIO_HARDWARE_MODULE_ID_PRIMARY) {
53 return openDevice<PrimaryDevice>(moduleName.c_str(), _hidl_cb);
54 }
55 return openDevice(moduleName.c_str(), _hidl_cb);
56 }
57 Return<void> DevicesFactory::openPrimaryDevice(openPrimaryDevice_cb _hidl_cb) {
58 return openDevice<PrimaryDevice>(AUDIO_HARDWARE_MODULE_ID_PRIMARY, _hidl_cb);
59 }
60 #endif
61
openDevice(const char * moduleName,openDevice_cb _hidl_cb)62 Return<void> DevicesFactory::openDevice(const char* moduleName, openDevice_cb _hidl_cb) {
63 return openDevice<implementation::Device>(moduleName, _hidl_cb);
64 }
65
66 template <class DeviceShim, class Callback>
openDevice(const char * moduleName,Callback _hidl_cb)67 Return<void> DevicesFactory::openDevice(const char* moduleName, Callback _hidl_cb) {
68 audio_hw_device_t* halDevice;
69 Result retval(Result::INVALID_ARGUMENTS);
70 sp<DeviceShim> result;
71 int halStatus = loadAudioInterface(moduleName, &halDevice);
72 if (halStatus == OK) {
73 result = new DeviceShim(halDevice);
74 retval = Result::OK;
75 } else if (halStatus == -EINVAL) {
76 retval = Result::NOT_INITIALIZED;
77 }
78 _hidl_cb(retval, result);
79 return Void();
80 }
81
82 // static
loadAudioInterface(const char * if_name,audio_hw_device_t ** dev)83 int DevicesFactory::loadAudioInterface(const char* if_name, audio_hw_device_t** dev) {
84 const hw_module_t* mod;
85 int rc;
86
87 rc = hw_get_module_by_class(AUDIO_HARDWARE_MODULE_ID, if_name, &mod);
88 if (rc) {
89 ALOGE("%s couldn't load audio hw module %s.%s (%s)", __func__, AUDIO_HARDWARE_MODULE_ID,
90 if_name, strerror(-rc));
91 goto out;
92 }
93 rc = audio_hw_device_open(mod, dev);
94 if (rc) {
95 ALOGE("%s couldn't open audio hw device in %s.%s (%s)", __func__, AUDIO_HARDWARE_MODULE_ID,
96 if_name, strerror(-rc));
97 goto out;
98 }
99 if ((*dev)->common.version < AUDIO_DEVICE_API_VERSION_MIN) {
100 ALOGE("%s wrong audio hw device version %04x", __func__, (*dev)->common.version);
101 rc = -EINVAL;
102 audio_hw_device_close(*dev);
103 goto out;
104 }
105 return OK;
106
107 out:
108 *dev = NULL;
109 return rc;
110 }
111
HIDL_FETCH_IDevicesFactory(const char * name)112 IDevicesFactory* HIDL_FETCH_IDevicesFactory(const char* name) {
113 return strcmp(name, "default") == 0 ? new DevicesFactory() : nullptr;
114 }
115
116 } // namespace implementation
117 } // namespace CPP_VERSION
118 } // namespace audio
119 } // namespace hardware
120 } // namespace android
121