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 <utils/Log.h>
18#include "SoundTriggerHalLegacy.h"
19
20namespace android {
21
22/* static */
23sp<SoundTriggerHalInterface> SoundTriggerHalInterface::connectModule(const char *moduleName)
24{
25    return new SoundTriggerHalLegacy(moduleName);
26}
27
28SoundTriggerHalLegacy::SoundTriggerHalLegacy(const char *moduleName)
29    : mModuleName(moduleName), mHwDevice(NULL)
30{
31}
32
33void SoundTriggerHalLegacy::onFirstRef()
34{
35    const hw_module_t *mod;
36    int rc;
37
38    if (mModuleName == NULL) {
39        mModuleName = "primary";
40    }
41
42    rc = hw_get_module_by_class(SOUND_TRIGGER_HARDWARE_MODULE_ID, mModuleName, &mod);
43    if (rc != 0) {
44        ALOGE("couldn't load sound trigger module %s.%s (%s)",
45              SOUND_TRIGGER_HARDWARE_MODULE_ID, mModuleName, strerror(-rc));
46        return;
47    }
48    rc = sound_trigger_hw_device_open(mod, &mHwDevice);
49    if (rc != 0) {
50        ALOGE("couldn't open sound trigger hw device in %s.%s (%s)",
51              SOUND_TRIGGER_HARDWARE_MODULE_ID, mModuleName, strerror(-rc));
52        mHwDevice = NULL;
53        return;
54    }
55    if (mHwDevice->common.version < SOUND_TRIGGER_DEVICE_API_VERSION_1_0 ||
56            mHwDevice->common.version > SOUND_TRIGGER_DEVICE_API_VERSION_CURRENT) {
57        ALOGE("wrong sound trigger hw device version %04x", mHwDevice->common.version);
58        return;
59    }
60}
61
62SoundTriggerHalLegacy::~SoundTriggerHalLegacy()
63{
64    if (mHwDevice != NULL) {
65        sound_trigger_hw_device_close(mHwDevice);
66    }
67}
68
69int SoundTriggerHalLegacy::getProperties(struct sound_trigger_properties *properties)
70{
71    if (mHwDevice == NULL) {
72        return -ENODEV;
73    }
74    return mHwDevice->get_properties(mHwDevice, properties);
75}
76
77int SoundTriggerHalLegacy::loadSoundModel(struct sound_trigger_sound_model *sound_model,
78                        sound_model_callback_t callback,
79                        void *cookie,
80                        sound_model_handle_t *handle)
81{
82    if (mHwDevice == NULL) {
83        return -ENODEV;
84    }
85    return mHwDevice->load_sound_model(mHwDevice, sound_model, callback, cookie, handle);
86}
87
88int SoundTriggerHalLegacy::unloadSoundModel(sound_model_handle_t handle)
89{
90    if (mHwDevice == NULL) {
91        return -ENODEV;
92    }
93    return mHwDevice->unload_sound_model(mHwDevice, handle);
94}
95
96int SoundTriggerHalLegacy::startRecognition(sound_model_handle_t handle,
97                         const struct sound_trigger_recognition_config *config,
98                         recognition_callback_t callback,
99                         void *cookie)
100{
101    if (mHwDevice == NULL) {
102        return -ENODEV;
103    }
104    return mHwDevice->start_recognition(mHwDevice, handle, config, callback, cookie);
105}
106
107int SoundTriggerHalLegacy::stopRecognition(sound_model_handle_t handle)
108{
109    if (mHwDevice == NULL) {
110        return -ENODEV;
111    }
112    return mHwDevice->stop_recognition(mHwDevice, handle);
113}
114
115int SoundTriggerHalLegacy::stopAllRecognitions()
116{
117    if (mHwDevice == NULL) {
118        return -ENODEV;
119    }
120    if (mHwDevice->common.version >= SOUND_TRIGGER_DEVICE_API_VERSION_1_1 &&
121     mHwDevice->stop_all_recognitions) {
122        return mHwDevice->stop_all_recognitions(mHwDevice);
123    }
124    return -ENOSYS;
125}
126
127} // namespace android
128