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