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_1_2 HARDWARE_DEVICE_API_VERSION(1, 2)
44 #define SOUND_TRIGGER_DEVICE_API_VERSION_1_3 HARDWARE_DEVICE_API_VERSION(1, 3)
45 #define SOUND_TRIGGER_DEVICE_API_VERSION_CURRENT SOUND_TRIGGER_DEVICE_API_VERSION_1_3
46 
47 /**
48  * List of known sound trigger HAL modules. This is the base name of the sound_trigger HAL
49  * library composed of the "sound_trigger." prefix, one of the base names below and
50  * a suffix specific to the device.
51  * e.g: sondtrigger.primary.goldfish.so or sound_trigger.primary.default.so
52  */
53 
54 #define SOUND_TRIGGER_HARDWARE_MODULE_ID_PRIMARY "primary"
55 
56 
57 /**
58  * Every hardware module must have a data structure named HAL_MODULE_INFO_SYM
59  * and the fields of this data structure must begin with hw_module_t
60  * followed by module specific information.
61  */
62 struct sound_trigger_module {
63     struct hw_module_t common;
64 };
65 
66 typedef void (*recognition_callback_t)(struct sound_trigger_recognition_event *event, void *cookie);
67 typedef void (*sound_model_callback_t)(struct sound_trigger_model_event *event, void *cookie);
68 
69 struct sound_trigger_hw_device {
70     struct hw_device_t common;
71 
72     /*
73      * Retrieve implementation properties.
74      */
75     int (*get_properties)(const struct sound_trigger_hw_device *dev,
76                           struct sound_trigger_properties *properties);
77 
78     /*
79      * Load a sound model. Once loaded, recognition of this model can be started and stopped.
80      * Only one active recognition per model at a time. The SoundTrigger service will handle
81      * concurrent recognition requests by different users/applications on the same model.
82      * The implementation returns a unique handle used by other functions (unload_sound_model(),
83      * start_recognition(), etc...
84      */
85     int (*load_sound_model)(const struct sound_trigger_hw_device *dev,
86                             struct sound_trigger_sound_model *sound_model,
87                             sound_model_callback_t callback,
88                             void *cookie,
89                             sound_model_handle_t *handle);
90 
91     /*
92      * Unload a sound model. A sound model can be unloaded to make room for a new one to overcome
93      * implementation limitations.
94      */
95     int (*unload_sound_model)(const struct sound_trigger_hw_device *dev,
96                               sound_model_handle_t handle);
97 
98     /* Start recognition on a given model. Only one recognition active at a time per model.
99      * Once recognition succeeds of fails, the callback is called.
100      * TODO: group recognition configuration parameters into one struct and add key phrase options.
101      */
102     int (*start_recognition)(const struct sound_trigger_hw_device *dev,
103                              sound_model_handle_t sound_model_handle,
104                              const struct sound_trigger_recognition_config *config,
105                              recognition_callback_t callback,
106                              void *cookie);
107 
108     /* Stop recognition on a given model.
109      * The implementation does not have to call the callback when stopped via this method.
110      */
111     int (*stop_recognition)(const struct sound_trigger_hw_device *dev,
112                             sound_model_handle_t sound_model_handle);
113 
114     /* Stop recognition on all models.
115      * Only supported for device api versions SOUND_TRIGGER_DEVICE_API_VERSION_1_1 or above.
116      * If no implementation is provided, stop_recognition will be called for each running model.
117      */
118     int (*stop_all_recognitions)(const struct sound_trigger_hw_device* dev);
119 
120     /* Get the current state of a given model.
121      * The state will be returned as a recognition event, via the callback that was registered
122      * in the start_recognition method.
123      * Only supported for device api versions SOUND_TRIGGER_DEVICE_API_VERSION_1_2 or above.
124      */
125     int (*get_model_state)(const struct sound_trigger_hw_device *dev,
126                            sound_model_handle_t sound_model_handle);
127 
128     /* Set a model specific ModelParameter with the given value. This parameter
129      * will keep its value for the duration the model is loaded regardless of starting and stopping
130      * recognition. Once the model is unloaded, the value will be lost.
131      * Returns 0 or an error code.
132      * Only supported for device api versions SOUND_TRIGGER_DEVICE_API_VERSION_1_3 or above.
133      */
134     int (*set_parameter)(const struct sound_trigger_hw_device *dev,
135                            sound_model_handle_t sound_model_handle,
136                            sound_trigger_model_parameter_t model_param, int32_t value);
137 
138     /* Get a model specific ModelParameter. This parameter will keep its value
139      * for the duration the model is loaded regardless of starting and stopping recognition.
140      * Once the model is unloaded, the value will be lost. If the value is not set, a default
141      * value is returned. See sound_trigger_model_parameter_t for parameter default values.
142      * Returns 0 or an error code. On return 0, value pointer will be set.
143      * Only supported for device api versions SOUND_TRIGGER_DEVICE_API_VERSION_1_3 or above.
144      */
145     int (*get_parameter)(const struct sound_trigger_hw_device *dev,
146                            sound_model_handle_t sound_model_handle,
147                            sound_trigger_model_parameter_t model_param, int32_t* value);
148 
149     /* Get supported parameter attributes with respect to the provided model
150      * handle. Along with determining the valid range, this API is also used
151      * to determine if a given parameter ID is supported at all by the
152      * modelHandle for use with getParameter and setParameter APIs.
153      * Only supported for device api versions SOUND_TRIGGER_DEVICE_API_VERSION_1_3 or above.
154      */
155     int (*query_parameter)(const struct sound_trigger_hw_device *dev,
156                            sound_model_handle_t sound_model_handle,
157                            sound_trigger_model_parameter_t model_param,
158                            sound_trigger_model_parameter_range_t* param_range);
159 
160     /*
161      * Retrieve verbose extended implementation properties.
162      * The header pointer is intented to be cast to the proper extended
163      * properties struct based on the header version.
164      * The returned pointer is valid throughout the lifetime of the driver.
165      * Only supported for device api versions SOUND_TRIGGER_DEVICE_API_VERSION_1_3 or above.
166      */
167     const struct sound_trigger_properties_header* (*get_properties_extended)
168             (const struct sound_trigger_hw_device *dev);
169 
170     /* Start recognition on a given model. Only one recognition active at a time per model.
171      * Once recognition succeeds of fails, the callback is called.
172      * Recognition API includes extended config fields. The header is intended to be base to
173      * the proper config struct based on the header version.
174      * Only supported for device api versions SOUND_TRIGGER_DEVICE_API_VERSION_1_3 or above.
175      */
176     int (*start_recognition_extended)(const struct sound_trigger_hw_device *dev,
177                              sound_model_handle_t sound_model_handle,
178                              const struct sound_trigger_recognition_config_header *header,
179                              recognition_callback_t callback,
180                              void *cookie);
181 };
182 
183 typedef struct sound_trigger_hw_device sound_trigger_hw_device_t;
184 
185 /** convenience API for opening and closing a supported device */
186 
sound_trigger_hw_device_open(const struct hw_module_t * module,struct sound_trigger_hw_device ** device)187 static inline int sound_trigger_hw_device_open(const struct hw_module_t* module,
188                                        struct sound_trigger_hw_device** device)
189 {
190     return module->methods->open(module, SOUND_TRIGGER_HARDWARE_INTERFACE,
191                                  TO_HW_DEVICE_T_OPEN(device));
192 }
193 
sound_trigger_hw_device_close(struct sound_trigger_hw_device * device)194 static inline int sound_trigger_hw_device_close(struct sound_trigger_hw_device* device)
195 {
196     return device->common.close(&device->common);
197 }
198 
199 __END_DECLS
200 
201 #endif  // ANDROID_SOUND_TRIGGER_HAL_H
202