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 "DevicesFactoryHalLocal"
18 //#define LOG_NDEBUG 0
19 
20 #include <string.h>
21 
22 #include <hardware/audio.h>
23 #include <utils/Log.h>
24 
25 #include "DeviceHalLocal.h"
26 #include "DevicesFactoryHalLocal.h"
27 
28 namespace android {
29 namespace CPP_VERSION {
30 
load_audio_interface(const char * if_name,audio_hw_device_t ** dev)31 static status_t load_audio_interface(const char *if_name, audio_hw_device_t **dev)
32 {
33     const hw_module_t *mod;
34     int rc;
35 
36     rc = hw_get_module_by_class(AUDIO_HARDWARE_MODULE_ID, if_name, &mod);
37     if (rc) {
38         ALOGE("%s couldn't load audio hw module %s.%s (%s)", __func__,
39                 AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc));
40         goto out;
41     }
42     rc = audio_hw_device_open(mod, dev);
43     if (rc) {
44         ALOGE("%s couldn't open audio hw device in %s.%s (%s)", __func__,
45                 AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc));
46         goto out;
47     }
48     if ((*dev)->common.version < AUDIO_DEVICE_API_VERSION_MIN) {
49         ALOGE("%s wrong audio hw device version %04x", __func__, (*dev)->common.version);
50         rc = BAD_VALUE;
51         audio_hw_device_close(*dev);
52         goto out;
53     }
54     return OK;
55 
56 out:
57     *dev = NULL;
58     return rc;
59 }
60 
openDevice(const char * name,sp<DeviceHalInterface> * device)61 status_t DevicesFactoryHalLocal::openDevice(const char *name, sp<DeviceHalInterface> *device) {
62     audio_hw_device_t *dev;
63     status_t rc = load_audio_interface(name, &dev);
64     if (rc == OK) {
65         *device = new DeviceHalLocal(dev);
66     }
67     return rc;
68 }
69 
70 } // namespace CPP_VERSION
71 } // namespace android
72