1/*
2 * Copyright (C) 2014 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 <system/audio.h>
18#include <system/sound_trigger.h>
19#include <hardware/hardware.h>
20
21#ifndef ANDROID_SOUND_TRIGGER_HAL_H
22#define ANDROID_SOUND_TRIGGER_HAL_H
23
24
25__BEGIN_DECLS
26
27/**
28 * The id of this module
29 */
30#define SOUND_TRIGGER_HARDWARE_MODULE_ID "sound_trigger"
31
32/**
33 * Name of the audio devices to open
34 */
35#define SOUND_TRIGGER_HARDWARE_INTERFACE "sound_trigger_hw_if"
36
37#define SOUND_TRIGGER_MODULE_API_VERSION_1_0 HARDWARE_MODULE_API_VERSION(1, 0)
38#define SOUND_TRIGGER_MODULE_API_VERSION_CURRENT SOUND_TRIGGER_MODULE_API_VERSION_1_0
39
40
41#define SOUND_TRIGGER_DEVICE_API_VERSION_1_0 HARDWARE_DEVICE_API_VERSION(1, 0)
42#define SOUND_TRIGGER_DEVICE_API_VERSION_1_1 HARDWARE_DEVICE_API_VERSION(1, 1)
43#define SOUND_TRIGGER_DEVICE_API_VERSION_CURRENT SOUND_TRIGGER_DEVICE_API_VERSION_1_1
44
45/**
46 * List of known sound trigger HAL modules. This is the base name of the sound_trigger HAL
47 * library composed of the "sound_trigger." prefix, one of the base names below and
48 * a suffix specific to the device.
49 * e.g: sondtrigger.primary.goldfish.so or sound_trigger.primary.default.so
50 */
51
52#define SOUND_TRIGGER_HARDWARE_MODULE_ID_PRIMARY "primary"
53
54
55/**
56 * Every hardware module must have a data structure named HAL_MODULE_INFO_SYM
57 * and the fields of this data structure must begin with hw_module_t
58 * followed by module specific information.
59 */
60struct sound_trigger_module {
61    struct hw_module_t common;
62};
63
64typedef void (*recognition_callback_t)(struct sound_trigger_recognition_event *event, void *cookie);
65typedef void (*sound_model_callback_t)(struct sound_trigger_model_event *event, void *cookie);
66
67struct sound_trigger_hw_device {
68    struct hw_device_t common;
69
70    /*
71     * Retrieve implementation properties.
72     */
73    int (*get_properties)(const struct sound_trigger_hw_device *dev,
74                          struct sound_trigger_properties *properties);
75
76    /*
77     * Load a sound model. Once loaded, recognition of this model can be started and stopped.
78     * Only one active recognition per model at a time. The SoundTrigger service will handle
79     * concurrent recognition requests by different users/applications on the same model.
80     * The implementation returns a unique handle used by other functions (unload_sound_model(),
81     * start_recognition(), etc...
82     */
83    int (*load_sound_model)(const struct sound_trigger_hw_device *dev,
84                            struct sound_trigger_sound_model *sound_model,
85                            sound_model_callback_t callback,
86                            void *cookie,
87                            sound_model_handle_t *handle);
88
89    /*
90     * Unload a sound model. A sound model can be unloaded to make room for a new one to overcome
91     * implementation limitations.
92     */
93    int (*unload_sound_model)(const struct sound_trigger_hw_device *dev,
94                              sound_model_handle_t handle);
95
96    /* Start recognition on a given model. Only one recognition active at a time per model.
97     * Once recognition succeeds of fails, the callback is called.
98     * TODO: group recognition configuration parameters into one struct and add key phrase options.
99     */
100    int (*start_recognition)(const struct sound_trigger_hw_device *dev,
101                             sound_model_handle_t sound_model_handle,
102                             const struct sound_trigger_recognition_config *config,
103                             recognition_callback_t callback,
104                             void *cookie);
105
106    /* Stop recognition on a given model.
107     * The implementation does not have to call the callback when stopped via this method.
108     */
109    int (*stop_recognition)(const struct sound_trigger_hw_device *dev,
110                            sound_model_handle_t sound_model_handle);
111
112    /* Stop recognition on all models.
113     * Only supported for device api versions SOUND_TRIGGER_DEVICE_API_VERSION_1_1 or above.
114     * If no implementation is provided, stop_recognition will be called for each running model.
115     */
116    int (*stop_all_recognitions)(const struct sound_trigger_hw_device* dev);
117};
118
119typedef struct sound_trigger_hw_device sound_trigger_hw_device_t;
120
121/** convenience API for opening and closing a supported device */
122
123static inline int sound_trigger_hw_device_open(const struct hw_module_t* module,
124                                       struct sound_trigger_hw_device** device)
125{
126    return module->methods->open(module, SOUND_TRIGGER_HARDWARE_INTERFACE,
127                                 TO_HW_DEVICE_T_OPEN(device));
128}
129
130static inline int sound_trigger_hw_device_close(struct sound_trigger_hw_device* device)
131{
132    return device->common.close(&device->common);
133}
134
135__END_DECLS
136
137#endif  // ANDROID_SOUND_TRIGGER_HAL_H
138