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