Eric Laurent | cbca905 | 2014-04-18 17:54:10 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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 | |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 17 | |
| 18 | /* This HAL simulates triggers from the DSP. |
| 19 | * To send a trigger from the command line you can type: |
| 20 | * |
| 21 | * adb forward tcp:14035 tcp:14035 |
| 22 | * echo $'\001' | nc -q -1 localhost 14035 |
| 23 | * |
| 24 | * $'\001' corresponds to the index_position of loaded sound |
| 25 | * model, you can also send $'\002' etc. $'\000' is the kill |
| 26 | * signal for the trigger listening thread. |
| 27 | * |
Ryan Bavetta | be0c8fd | 2016-02-01 15:17:12 -0800 | [diff] [blame] | 28 | * To enable this file, you can make with command line parameter |
| 29 | * SOUND_TRIGGER_USE_STUB_MODULE=1 |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 30 | */ |
| 31 | |
Eric Laurent | cbca905 | 2014-04-18 17:54:10 -0700 | [diff] [blame] | 32 | #define LOG_TAG "sound_trigger_hw_default" |
Ryan Bavetta | cc6541c | 2016-02-09 21:20:51 -0800 | [diff] [blame^] | 33 | #define LOG_NDEBUG 1 |
Eric Laurent | cbca905 | 2014-04-18 17:54:10 -0700 | [diff] [blame] | 34 | |
| 35 | #include <errno.h> |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 36 | #include <stdio.h> |
| 37 | #include <stdlib.h> |
| 38 | #include <string.h> |
| 39 | #include <unistd.h> |
| 40 | #include <arpa/inet.h> |
| 41 | #include <sys/types.h> |
| 42 | #include <netinet/in.h> |
| 43 | #include <sys/socket.h> |
| 44 | |
| 45 | #include <errno.h> |
Eric Laurent | cbca905 | 2014-04-18 17:54:10 -0700 | [diff] [blame] | 46 | #include <pthread.h> |
| 47 | #include <sys/prctl.h> |
| 48 | #include <cutils/log.h> |
| 49 | |
| 50 | #include <hardware/hardware.h> |
| 51 | #include <system/sound_trigger.h> |
| 52 | #include <hardware/sound_trigger.h> |
| 53 | |
| 54 | static const struct sound_trigger_properties hw_properties = { |
| 55 | "The Android Open Source Project", // implementor |
| 56 | "Sound Trigger stub HAL", // description |
| 57 | 1, // version |
| 58 | { 0xed7a7d60, 0xc65e, 0x11e3, 0x9be4, { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } }, // uuid |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 59 | 2, // max_sound_models |
Eric Laurent | cbca905 | 2014-04-18 17:54:10 -0700 | [diff] [blame] | 60 | 1, // max_key_phrases |
| 61 | 1, // max_users |
| 62 | RECOGNITION_MODE_VOICE_TRIGGER, // recognition_modes |
| 63 | false, // capture_transition |
| 64 | 0, // max_buffer_ms |
| 65 | false, // concurrent_capture |
Eric Laurent | 83cd830 | 2014-07-30 08:58:39 -0700 | [diff] [blame] | 66 | false, // trigger_in_event |
Eric Laurent | cbca905 | 2014-04-18 17:54:10 -0700 | [diff] [blame] | 67 | 0 // power_consumption_mw |
| 68 | }; |
| 69 | |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 70 | struct recognition_context { |
Ryan Bavetta | cc6541c | 2016-02-09 21:20:51 -0800 | [diff] [blame^] | 71 | // Sound Model information, added in method load_sound_model |
| 72 | sound_model_handle_t model_handle; |
| 73 | sound_trigger_sound_model_type_t model_type; |
| 74 | sound_model_callback_t model_callback; |
| 75 | void *model_cookie; |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 76 | |
Ryan Bavetta | cc6541c | 2016-02-09 21:20:51 -0800 | [diff] [blame^] | 77 | // Sound Model information, added in start_recognition |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 78 | struct sound_trigger_recognition_config *config; |
| 79 | recognition_callback_t recognition_callback; |
| 80 | void *recognition_cookie; |
Ryan Bavetta | cc6541c | 2016-02-09 21:20:51 -0800 | [diff] [blame^] | 81 | |
| 82 | // Next recognition_context in the linked list |
| 83 | struct recognition_context *next; |
Ryan Bavetta | be0c8fd | 2016-02-01 15:17:12 -0800 | [diff] [blame] | 84 | }; |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 85 | |
| 86 | struct stub_sound_trigger_device { |
| 87 | struct sound_trigger_hw_device device; |
Eric Laurent | cbca905 | 2014-04-18 17:54:10 -0700 | [diff] [blame] | 88 | pthread_mutex_t lock; |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 89 | pthread_t callback_thread; |
| 90 | |
Ryan Bavetta | cc6541c | 2016-02-09 21:20:51 -0800 | [diff] [blame^] | 91 | // Recognition contexts are stored as a linked list |
| 92 | struct recognition_context *root_model_context; |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 93 | |
| 94 | int next_sound_model_id; |
Eric Laurent | cbca905 | 2014-04-18 17:54:10 -0700 | [diff] [blame] | 95 | }; |
| 96 | |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 97 | /* Will reuse ids when overflow occurs */ |
| 98 | static unsigned int generate_sound_model_id(const struct sound_trigger_hw_device *dev) { |
| 99 | struct stub_sound_trigger_device *stdev = (struct stub_sound_trigger_device *)dev; |
| 100 | int new_id = stdev->next_sound_model_id; |
| 101 | ++stdev->next_sound_model_id; |
| 102 | if (stdev->next_sound_model_id == 0) { |
| 103 | stdev->next_sound_model_id = 1; |
| 104 | } |
| 105 | return new_id; |
| 106 | } |
Eric Laurent | cbca905 | 2014-04-18 17:54:10 -0700 | [diff] [blame] | 107 | |
Ryan Bavetta | cc6541c | 2016-02-09 21:20:51 -0800 | [diff] [blame^] | 108 | static char *sound_trigger_event_alloc(sound_model_handle_t handle, |
| 109 | sound_trigger_sound_model_type_t model_type, |
| 110 | struct sound_trigger_recognition_config *config) { |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 111 | char *data; |
Ryan Bavetta | cc6541c | 2016-02-09 21:20:51 -0800 | [diff] [blame^] | 112 | if (model_type == SOUND_MODEL_TYPE_KEYPHRASE) { |
| 113 | struct sound_trigger_phrase_recognition_event *event; |
| 114 | data = (char *)calloc(1, sizeof(struct sound_trigger_phrase_recognition_event)); |
| 115 | if (!data) |
| 116 | return NULL; |
| 117 | event = (struct sound_trigger_phrase_recognition_event *)data; |
| 118 | event->common.status = RECOGNITION_STATUS_SUCCESS; |
| 119 | event->common.type = SOUND_MODEL_TYPE_KEYPHRASE; |
| 120 | event->common.model = handle; |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 121 | |
Ryan Bavetta | cc6541c | 2016-02-09 21:20:51 -0800 | [diff] [blame^] | 122 | if (config) { |
| 123 | unsigned int i; |
| 124 | |
| 125 | event->num_phrases = config->num_phrases; |
| 126 | if (event->num_phrases > SOUND_TRIGGER_MAX_PHRASES) |
| 127 | event->num_phrases = SOUND_TRIGGER_MAX_PHRASES; |
| 128 | for (i=0; i < event->num_phrases; i++) |
| 129 | memcpy(&event->phrase_extras[i], |
| 130 | &config->phrases[i], |
| 131 | sizeof(struct sound_trigger_phrase_recognition_extra)); |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 132 | } |
Ryan Bavetta | cc6541c | 2016-02-09 21:20:51 -0800 | [diff] [blame^] | 133 | |
| 134 | event->num_phrases = 1; |
| 135 | event->phrase_extras[0].confidence_level = 100; |
| 136 | event->phrase_extras[0].num_levels = 1; |
| 137 | event->phrase_extras[0].levels[0].level = 100; |
| 138 | event->phrase_extras[0].levels[0].user_id = 0; |
| 139 | // Signify that all the data is comming through streaming, not through the buffer. |
| 140 | event->common.capture_available = true; |
| 141 | event->common.audio_config = AUDIO_CONFIG_INITIALIZER; |
| 142 | event->common.audio_config.sample_rate = 16000; |
| 143 | event->common.audio_config.channel_mask = AUDIO_CHANNEL_IN_MONO; |
| 144 | event->common.audio_config.format = AUDIO_FORMAT_PCM_16_BIT; |
| 145 | } else if (model_type == SOUND_MODEL_TYPE_GENERIC) { |
| 146 | struct sound_trigger_generic_recognition_event *event; |
| 147 | data = (char *)calloc(1, sizeof(struct sound_trigger_generic_recognition_event)); |
| 148 | if (!data) |
| 149 | return NULL; |
| 150 | event = (struct sound_trigger_generic_recognition_event *)data; |
| 151 | event->common.status = RECOGNITION_STATUS_SUCCESS; |
| 152 | event->common.type = SOUND_MODEL_TYPE_GENERIC; |
| 153 | event->common.model = handle; |
| 154 | |
| 155 | // Signify that all the data is comming through streaming, not through the buffer. |
| 156 | event->common.capture_available = true; |
| 157 | event->common.audio_config = AUDIO_CONFIG_INITIALIZER; |
| 158 | event->common.audio_config.sample_rate = 16000; |
| 159 | event->common.audio_config.channel_mask = AUDIO_CHANNEL_IN_MONO; |
| 160 | event->common.audio_config.format = AUDIO_FORMAT_PCM_16_BIT; |
| 161 | } else { |
| 162 | ALOGW("No Valid Event Type Known"); |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 163 | return NULL; |
| 164 | } |
Ryan Bavetta | cc6541c | 2016-02-09 21:20:51 -0800 | [diff] [blame^] | 165 | return data; |
| 166 | } |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 167 | |
Ryan Bavetta | cc6541c | 2016-02-09 21:20:51 -0800 | [diff] [blame^] | 168 | static void send_recognition_event(sound_model_handle_t model_handle, |
| 169 | sound_trigger_sound_model_type_t model_type, |
| 170 | recognition_callback_t recognition_callback, void *recognition_cookie, |
| 171 | struct sound_trigger_recognition_config *config) { |
| 172 | if (recognition_callback == NULL) { |
| 173 | ALOGI("%s No matching callback for handle %d", __func__, model_handle); |
| 174 | return; |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 175 | } |
| 176 | |
Ryan Bavetta | cc6541c | 2016-02-09 21:20:51 -0800 | [diff] [blame^] | 177 | if (model_type == SOUND_MODEL_TYPE_KEYPHRASE) { |
| 178 | struct sound_trigger_phrase_recognition_event *event; |
| 179 | event = (struct sound_trigger_phrase_recognition_event *) |
| 180 | sound_trigger_event_alloc(model_handle, model_type, config); |
| 181 | if (event) { |
| 182 | recognition_callback(&event->common, recognition_cookie); |
| 183 | free(event); |
| 184 | } |
| 185 | } else if (model_type == SOUND_MODEL_TYPE_GENERIC) { |
| 186 | struct sound_trigger_generic_recognition_event *event; |
| 187 | event = (struct sound_trigger_generic_recognition_event *) |
| 188 | sound_trigger_event_alloc(model_handle, model_type, config); |
| 189 | if (event) { |
| 190 | recognition_callback(&event->common, recognition_cookie); |
| 191 | free(event); |
| 192 | } |
| 193 | } else { |
| 194 | ALOGI("Unknown Sound Model Type, No Event to Send"); |
| 195 | } |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | static void *callback_thread_loop(void *context) { |
Eric Laurent | cbca905 | 2014-04-18 17:54:10 -0700 | [diff] [blame] | 199 | struct stub_sound_trigger_device *stdev = (struct stub_sound_trigger_device *)context; |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 200 | struct sockaddr_in incoming_info; |
| 201 | struct sockaddr_in self_info; |
| 202 | int self_socket; |
| 203 | socklen_t sock_size = sizeof(struct sockaddr_in); |
| 204 | memset(&self_info, 0, sizeof(self_info)); |
| 205 | self_info.sin_family = AF_INET; |
| 206 | self_info.sin_addr.s_addr = htonl(INADDR_ANY); |
| 207 | self_info.sin_port = htons(14035); |
Eric Laurent | cbca905 | 2014-04-18 17:54:10 -0700 | [diff] [blame] | 208 | |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 209 | bool exit = false; |
| 210 | while(!exit) { |
| 211 | int received_count; |
| 212 | int requested_count = 1; |
| 213 | char buffer[requested_count]; |
| 214 | ALOGE("Opening socket"); |
| 215 | self_socket = socket(AF_INET, SOCK_STREAM, 0); |
| 216 | if (self_socket < 0) { |
| 217 | ALOGE("Error on socket creation"); |
| 218 | exit = true; |
| 219 | } else { |
| 220 | ALOGI("Socket created"); |
| 221 | } |
Eric Laurent | cbca905 | 2014-04-18 17:54:10 -0700 | [diff] [blame] | 222 | |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 223 | int reuse = 1; |
| 224 | if (setsockopt(self_socket, SOL_SOCKET, SO_REUSEADDR, (const char*)&reuse, sizeof(reuse)) < 0) { |
| 225 | ALOGE("setsockopt(SO_REUSEADDR) failed"); |
| 226 | } |
| 227 | |
| 228 | int bind_result = bind(self_socket, (struct sockaddr *)&self_info, sizeof(struct sockaddr)); |
| 229 | if (bind_result < 0) { |
| 230 | ALOGE("Error on bind"); |
| 231 | exit = true; |
| 232 | } |
| 233 | |
| 234 | int listen_result = listen(self_socket, 1); |
| 235 | if (listen_result < 0) { |
| 236 | ALOGE("Error on Listen"); |
| 237 | exit = true; |
| 238 | } |
| 239 | |
| 240 | while(!exit) { |
| 241 | int con_socket = accept(self_socket, (struct sockaddr *)&incoming_info, &sock_size); |
| 242 | if (!con_socket) { |
| 243 | ALOGE("Lost socket, cannot send trigger"); |
| 244 | break; |
| 245 | } |
| 246 | ALOGI("Connection from %s", inet_ntoa(incoming_info.sin_addr)); |
| 247 | received_count = recv(con_socket, buffer, requested_count, 0); |
| 248 | unsigned int index = buffer[0] - 1; |
| 249 | ALOGI("Received data"); |
| 250 | pthread_mutex_lock(&stdev->lock); |
| 251 | if (received_count > 0) { |
| 252 | if (buffer[0] == 0) { |
| 253 | ALOGI("Received kill signal: stop listening to incoming server messages"); |
| 254 | exit = true; |
| 255 | } else if (index < hw_properties.max_sound_models) { |
Ryan Bavetta | cc6541c | 2016-02-09 21:20:51 -0800 | [diff] [blame^] | 256 | ALOGI("Going to send trigger for model index #%d", index ); |
| 257 | struct recognition_context *model_context = NULL; |
| 258 | struct recognition_context *last_model_context = stdev->root_model_context; |
| 259 | int model_index = 0; |
| 260 | while(last_model_context) { |
| 261 | if (model_index == index) { |
| 262 | model_context = last_model_context; |
| 263 | break; |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 264 | } |
Ryan Bavetta | cc6541c | 2016-02-09 21:20:51 -0800 | [diff] [blame^] | 265 | last_model_context = last_model_context->next; |
| 266 | model_index++; |
| 267 | } |
| 268 | if (model_context) { |
| 269 | send_recognition_event(model_context->model_handle, |
| 270 | model_context->model_type, |
| 271 | model_context->recognition_callback, |
| 272 | model_context->recognition_cookie, |
| 273 | model_context->config); |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 274 | } else { |
Ryan Bavetta | cc6541c | 2016-02-09 21:20:51 -0800 | [diff] [blame^] | 275 | ALOGI("Sound Model Does Not Exist at this Index: %d", index); |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 276 | } |
| 277 | } else { |
| 278 | ALOGI("Data is not recognized: %d", index); |
| 279 | } |
| 280 | } else { |
| 281 | ALOGI("Received sata is size 0"); |
| 282 | } |
| 283 | pthread_mutex_unlock(&stdev->lock); |
| 284 | close(con_socket); |
| 285 | } |
| 286 | ALOGE("Closing socket"); |
| 287 | close(self_socket); |
Eric Laurent | cbca905 | 2014-04-18 17:54:10 -0700 | [diff] [blame] | 288 | } |
Eric Laurent | cbca905 | 2014-04-18 17:54:10 -0700 | [diff] [blame] | 289 | |
| 290 | return NULL; |
| 291 | } |
| 292 | |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 293 | static void send_loop_kill_signal() { |
| 294 | ALOGI("Sending loop thread kill signal"); |
| 295 | int self_socket = socket(AF_INET, SOCK_STREAM, 0); |
| 296 | struct sockaddr_in remote_info; |
| 297 | memset(&remote_info, 0, sizeof(remote_info)); |
| 298 | remote_info.sin_family = AF_INET; |
| 299 | remote_info.sin_addr.s_addr = htonl(INADDR_LOOPBACK); |
| 300 | remote_info.sin_port = htons(14035); |
| 301 | if (connect(self_socket, (struct sockaddr *)&remote_info, sizeof(struct sockaddr)) == 0) { |
| 302 | char msg[] = {0}; |
| 303 | send(self_socket, msg, 1, 0); |
| 304 | } else { |
| 305 | ALOGI("Could not connect"); |
| 306 | } |
| 307 | close(self_socket); |
| 308 | ALOGI("Sent loop thread kill signal"); |
| 309 | } |
| 310 | |
Eric Laurent | cbca905 | 2014-04-18 17:54:10 -0700 | [diff] [blame] | 311 | static int stdev_get_properties(const struct sound_trigger_hw_device *dev, |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 312 | struct sound_trigger_properties *properties) { |
Eric Laurent | cbca905 | 2014-04-18 17:54:10 -0700 | [diff] [blame] | 313 | struct stub_sound_trigger_device *stdev = (struct stub_sound_trigger_device *)dev; |
| 314 | |
| 315 | ALOGI("%s", __func__); |
| 316 | if (properties == NULL) |
| 317 | return -EINVAL; |
| 318 | memcpy(properties, &hw_properties, sizeof(struct sound_trigger_properties)); |
| 319 | return 0; |
| 320 | } |
| 321 | |
| 322 | static int stdev_load_sound_model(const struct sound_trigger_hw_device *dev, |
| 323 | struct sound_trigger_sound_model *sound_model, |
| 324 | sound_model_callback_t callback, |
| 325 | void *cookie, |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 326 | sound_model_handle_t *handle) { |
Eric Laurent | cbca905 | 2014-04-18 17:54:10 -0700 | [diff] [blame] | 327 | struct stub_sound_trigger_device *stdev = (struct stub_sound_trigger_device *)dev; |
| 328 | int status = 0; |
| 329 | |
| 330 | ALOGI("%s stdev %p", __func__, stdev); |
| 331 | pthread_mutex_lock(&stdev->lock); |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 332 | |
Eric Laurent | cbca905 | 2014-04-18 17:54:10 -0700 | [diff] [blame] | 333 | if (handle == NULL || sound_model == NULL) { |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 334 | pthread_mutex_unlock(&stdev->lock); |
| 335 | return -EINVAL; |
Eric Laurent | cbca905 | 2014-04-18 17:54:10 -0700 | [diff] [blame] | 336 | } |
| 337 | if (sound_model->data_size == 0 || |
| 338 | sound_model->data_offset < sizeof(struct sound_trigger_sound_model)) { |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 339 | pthread_mutex_unlock(&stdev->lock); |
| 340 | return -EINVAL; |
Eric Laurent | cbca905 | 2014-04-18 17:54:10 -0700 | [diff] [blame] | 341 | } |
| 342 | |
Ryan Bavetta | cc6541c | 2016-02-09 21:20:51 -0800 | [diff] [blame^] | 343 | struct recognition_context *model_context; |
| 344 | model_context = malloc(sizeof(struct recognition_context)); |
| 345 | if(!model_context) { |
| 346 | ALOGW("Could not allocate recognition_context"); |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 347 | pthread_mutex_unlock(&stdev->lock); |
| 348 | return -ENOSYS; |
| 349 | } |
| 350 | |
Ryan Bavetta | cc6541c | 2016-02-09 21:20:51 -0800 | [diff] [blame^] | 351 | // Add the new model context to the recognition_context linked list |
| 352 | if (stdev->root_model_context) { |
| 353 | // Find the tail |
| 354 | struct recognition_context *last_model_context = stdev->root_model_context; |
| 355 | int model_count = 0; |
| 356 | while(last_model_context->next) { |
| 357 | last_model_context = last_model_context->next; |
| 358 | model_count++; |
| 359 | if (model_count >= hw_properties.max_sound_models) { |
| 360 | ALOGW("Can't load model: reached max sound model limit"); |
| 361 | free(model_context); |
| 362 | pthread_mutex_unlock(&stdev->lock); |
| 363 | return -ENOSYS; |
| 364 | } |
| 365 | } |
| 366 | last_model_context->next = model_context; |
| 367 | } else { |
| 368 | stdev->root_model_context = model_context; |
| 369 | } |
| 370 | |
| 371 | model_context->model_handle = generate_sound_model_id(dev); |
| 372 | *handle = model_context->model_handle; |
| 373 | model_context->model_type = sound_model->type; |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 374 | |
Eric Laurent | cbca905 | 2014-04-18 17:54:10 -0700 | [diff] [blame] | 375 | char *data = (char *)sound_model + sound_model->data_offset; |
| 376 | ALOGI("%s data size %d data %d - %d", __func__, |
| 377 | sound_model->data_size, data[0], data[sound_model->data_size - 1]); |
Ryan Bavetta | cc6541c | 2016-02-09 21:20:51 -0800 | [diff] [blame^] | 378 | model_context->model_callback = callback; |
| 379 | model_context->model_cookie = cookie; |
| 380 | model_context->config = NULL; |
| 381 | model_context->recognition_callback = NULL; |
| 382 | model_context->recognition_cookie = NULL; |
Eric Laurent | cbca905 | 2014-04-18 17:54:10 -0700 | [diff] [blame] | 383 | |
Eric Laurent | cbca905 | 2014-04-18 17:54:10 -0700 | [diff] [blame] | 384 | pthread_mutex_unlock(&stdev->lock); |
| 385 | return status; |
| 386 | } |
| 387 | |
| 388 | static int stdev_unload_sound_model(const struct sound_trigger_hw_device *dev, |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 389 | sound_model_handle_t handle) { |
Eric Laurent | cbca905 | 2014-04-18 17:54:10 -0700 | [diff] [blame] | 390 | struct stub_sound_trigger_device *stdev = (struct stub_sound_trigger_device *)dev; |
| 391 | int status = 0; |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 392 | ALOGI("unload_sound_model"); |
Eric Laurent | cbca905 | 2014-04-18 17:54:10 -0700 | [diff] [blame] | 393 | pthread_mutex_lock(&stdev->lock); |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 394 | |
Ryan Bavetta | cc6541c | 2016-02-09 21:20:51 -0800 | [diff] [blame^] | 395 | |
| 396 | struct recognition_context *model_context = NULL; |
| 397 | struct recognition_context *previous_model_context = NULL; |
| 398 | if (stdev->root_model_context) { |
| 399 | struct recognition_context *last_model_context = stdev->root_model_context; |
| 400 | while(last_model_context) { |
| 401 | if (last_model_context->model_handle == handle) { |
| 402 | model_context = last_model_context; |
| 403 | break; |
| 404 | } |
| 405 | previous_model_context = last_model_context; |
| 406 | last_model_context = last_model_context->next; |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 407 | } |
Eric Laurent | cbca905 | 2014-04-18 17:54:10 -0700 | [diff] [blame] | 408 | } |
Ryan Bavetta | cc6541c | 2016-02-09 21:20:51 -0800 | [diff] [blame^] | 409 | if (!model_context) { |
| 410 | ALOGW("Can't find sound model handle %d in registered list", handle); |
Eric Laurent | cbca905 | 2014-04-18 17:54:10 -0700 | [diff] [blame] | 411 | pthread_mutex_unlock(&stdev->lock); |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 412 | return -ENOSYS; |
Eric Laurent | cbca905 | 2014-04-18 17:54:10 -0700 | [diff] [blame] | 413 | } |
| 414 | |
Ryan Bavetta | cc6541c | 2016-02-09 21:20:51 -0800 | [diff] [blame^] | 415 | if(previous_model_context) { |
| 416 | previous_model_context->next = model_context->next; |
| 417 | } else { |
| 418 | stdev->root_model_context = model_context->next; |
| 419 | } |
| 420 | free(model_context->config); |
| 421 | free(model_context); |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 422 | |
| 423 | /* If no more models running with callbacks, stop trigger thread */ |
Ryan Bavetta | cc6541c | 2016-02-09 21:20:51 -0800 | [diff] [blame^] | 424 | bool other_callbacks_found = false; |
| 425 | if (stdev->root_model_context) { |
| 426 | struct recognition_context *last_model_context = stdev->root_model_context; |
| 427 | while(last_model_context) { |
| 428 | if (last_model_context->recognition_callback != NULL) { |
| 429 | other_callbacks_found = true; |
| 430 | break; |
| 431 | } |
| 432 | last_model_context = last_model_context->next; |
| 433 | } |
| 434 | } |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 435 | if (!other_callbacks_found) { |
| 436 | send_loop_kill_signal(); |
| 437 | pthread_mutex_unlock(&stdev->lock); |
| 438 | pthread_join(stdev->callback_thread, (void **)NULL); |
| 439 | } else { |
| 440 | pthread_mutex_unlock(&stdev->lock); |
| 441 | } |
| 442 | |
Eric Laurent | cbca905 | 2014-04-18 17:54:10 -0700 | [diff] [blame] | 443 | return status; |
| 444 | } |
| 445 | |
| 446 | static int stdev_start_recognition(const struct sound_trigger_hw_device *dev, |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 447 | sound_model_handle_t handle, |
Eric Laurent | 30f3e6d | 2014-07-06 16:08:45 -0700 | [diff] [blame] | 448 | const struct sound_trigger_recognition_config *config, |
Eric Laurent | cbca905 | 2014-04-18 17:54:10 -0700 | [diff] [blame] | 449 | recognition_callback_t callback, |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 450 | void *cookie) { |
| 451 | ALOGI("%s", __func__); |
Eric Laurent | cbca905 | 2014-04-18 17:54:10 -0700 | [diff] [blame] | 452 | struct stub_sound_trigger_device *stdev = (struct stub_sound_trigger_device *)dev; |
Eric Laurent | cbca905 | 2014-04-18 17:54:10 -0700 | [diff] [blame] | 453 | pthread_mutex_lock(&stdev->lock); |
Ryan Bavetta | cc6541c | 2016-02-09 21:20:51 -0800 | [diff] [blame^] | 454 | |
| 455 | struct recognition_context *model_context = NULL; |
| 456 | if (stdev->root_model_context) { |
| 457 | struct recognition_context *last_model_context = stdev->root_model_context; |
| 458 | while(last_model_context) { |
| 459 | if (last_model_context->model_handle == handle) { |
| 460 | model_context = last_model_context; |
| 461 | break; |
| 462 | } |
| 463 | last_model_context = last_model_context->next; |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 464 | } |
Eric Laurent | cbca905 | 2014-04-18 17:54:10 -0700 | [diff] [blame] | 465 | } |
Ryan Bavetta | cc6541c | 2016-02-09 21:20:51 -0800 | [diff] [blame^] | 466 | if (!model_context) { |
| 467 | ALOGW("Can't find sound model handle %d in registered list", handle); |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 468 | pthread_mutex_unlock(&stdev->lock); |
| 469 | return -ENOSYS; |
Eric Laurent | cbca905 | 2014-04-18 17:54:10 -0700 | [diff] [blame] | 470 | } |
| 471 | |
Ryan Bavetta | cc6541c | 2016-02-09 21:20:51 -0800 | [diff] [blame^] | 472 | free(model_context->config); |
| 473 | model_context->config = NULL; |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 474 | if (config) { |
Ryan Bavetta | cc6541c | 2016-02-09 21:20:51 -0800 | [diff] [blame^] | 475 | model_context->config = malloc(sizeof(*config)); |
| 476 | if (!model_context->config) { |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 477 | pthread_mutex_unlock(&stdev->lock); |
| 478 | return -ENOMEM; |
| 479 | } |
Ryan Bavetta | cc6541c | 2016-02-09 21:20:51 -0800 | [diff] [blame^] | 480 | memcpy(model_context->config, config, sizeof(*config)); |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 481 | } |
Ryan Bavetta | cc6541c | 2016-02-09 21:20:51 -0800 | [diff] [blame^] | 482 | model_context->recognition_callback = callback; |
| 483 | model_context->recognition_cookie = cookie; |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 484 | |
Eric Laurent | cbca905 | 2014-04-18 17:54:10 -0700 | [diff] [blame] | 485 | pthread_create(&stdev->callback_thread, (const pthread_attr_t *) NULL, |
| 486 | callback_thread_loop, stdev); |
Eric Laurent | cbca905 | 2014-04-18 17:54:10 -0700 | [diff] [blame] | 487 | pthread_mutex_unlock(&stdev->lock); |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 488 | return 0; |
Eric Laurent | cbca905 | 2014-04-18 17:54:10 -0700 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | static int stdev_stop_recognition(const struct sound_trigger_hw_device *dev, |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 492 | sound_model_handle_t handle) { |
Eric Laurent | cbca905 | 2014-04-18 17:54:10 -0700 | [diff] [blame] | 493 | struct stub_sound_trigger_device *stdev = (struct stub_sound_trigger_device *)dev; |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 494 | ALOGI("%s", __func__); |
Eric Laurent | cbca905 | 2014-04-18 17:54:10 -0700 | [diff] [blame] | 495 | pthread_mutex_lock(&stdev->lock); |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 496 | |
Ryan Bavetta | cc6541c | 2016-02-09 21:20:51 -0800 | [diff] [blame^] | 497 | struct recognition_context *model_context = NULL; |
| 498 | bool other_callbacks_found = false; |
| 499 | if (stdev->root_model_context) { |
| 500 | struct recognition_context *last_model_context = stdev->root_model_context; |
| 501 | while(last_model_context) { |
| 502 | if (last_model_context->model_handle == handle) { |
| 503 | model_context = last_model_context; |
| 504 | } else if (last_model_context->recognition_callback != NULL) { |
| 505 | other_callbacks_found = true; |
| 506 | } |
| 507 | last_model_context = last_model_context->next; |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 508 | } |
Eric Laurent | cbca905 | 2014-04-18 17:54:10 -0700 | [diff] [blame] | 509 | } |
Ryan Bavetta | cc6541c | 2016-02-09 21:20:51 -0800 | [diff] [blame^] | 510 | if (!model_context) { |
| 511 | ALOGW("Can't find sound model handle %d in registered list", handle); |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 512 | pthread_mutex_unlock(&stdev->lock); |
| 513 | return -ENOSYS; |
Eric Laurent | cbca905 | 2014-04-18 17:54:10 -0700 | [diff] [blame] | 514 | } |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 515 | |
Ryan Bavetta | cc6541c | 2016-02-09 21:20:51 -0800 | [diff] [blame^] | 516 | free(model_context->config); |
| 517 | model_context->config = NULL; |
| 518 | model_context->recognition_callback = NULL; |
| 519 | model_context->recognition_cookie = NULL; |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 520 | |
Ryan Bavetta | cc6541c | 2016-02-09 21:20:51 -0800 | [diff] [blame^] | 521 | /* If no more models running with callbacks, stop trigger thread */ |
| 522 | if (!other_callbacks_found) { |
| 523 | send_loop_kill_signal(); |
| 524 | pthread_mutex_unlock(&stdev->lock); |
| 525 | pthread_join(stdev->callback_thread, (void **)NULL); |
| 526 | } else { |
| 527 | pthread_mutex_unlock(&stdev->lock); |
| 528 | } |
| 529 | |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 530 | return 0; |
Eric Laurent | cbca905 | 2014-04-18 17:54:10 -0700 | [diff] [blame] | 531 | } |
| 532 | |
Ryan Bavetta | be0c8fd | 2016-02-01 15:17:12 -0800 | [diff] [blame] | 533 | __attribute__ ((visibility ("default"))) |
Ryan Bavetta | cc6541c | 2016-02-09 21:20:51 -0800 | [diff] [blame^] | 534 | int sound_trigger_open_for_streaming() { |
Ryan Bavetta | be0c8fd | 2016-02-01 15:17:12 -0800 | [diff] [blame] | 535 | int ret = 0; |
| 536 | return ret; |
| 537 | } |
| 538 | |
| 539 | __attribute__ ((visibility ("default"))) |
Ryan Bavetta | cc6541c | 2016-02-09 21:20:51 -0800 | [diff] [blame^] | 540 | size_t sound_trigger_read_samples(int audio_handle, void *buffer, size_t buffer_len) { |
Ryan Bavetta | be0c8fd | 2016-02-01 15:17:12 -0800 | [diff] [blame] | 541 | size_t ret = 0; |
| 542 | return ret; |
| 543 | } |
| 544 | |
| 545 | __attribute__ ((visibility ("default"))) |
Ryan Bavetta | cc6541c | 2016-02-09 21:20:51 -0800 | [diff] [blame^] | 546 | int sound_trigger_close_for_streaming(int audio_handle __unused) { |
Ryan Bavetta | be0c8fd | 2016-02-01 15:17:12 -0800 | [diff] [blame] | 547 | return 0; |
| 548 | } |
| 549 | |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 550 | static int stdev_close(hw_device_t *device) { |
Eric Laurent | cbca905 | 2014-04-18 17:54:10 -0700 | [diff] [blame] | 551 | free(device); |
| 552 | return 0; |
| 553 | } |
| 554 | |
| 555 | static int stdev_open(const hw_module_t* module, const char* name, |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 556 | hw_device_t** device) { |
Eric Laurent | cbca905 | 2014-04-18 17:54:10 -0700 | [diff] [blame] | 557 | struct stub_sound_trigger_device *stdev; |
| 558 | int ret; |
| 559 | |
| 560 | if (strcmp(name, SOUND_TRIGGER_HARDWARE_INTERFACE) != 0) |
| 561 | return -EINVAL; |
| 562 | |
| 563 | stdev = calloc(1, sizeof(struct stub_sound_trigger_device)); |
| 564 | if (!stdev) |
| 565 | return -ENOMEM; |
| 566 | |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 567 | stdev->next_sound_model_id = 1; |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 568 | |
Eric Laurent | cbca905 | 2014-04-18 17:54:10 -0700 | [diff] [blame] | 569 | stdev->device.common.tag = HARDWARE_DEVICE_TAG; |
| 570 | stdev->device.common.version = SOUND_TRIGGER_DEVICE_API_VERSION_1_0; |
| 571 | stdev->device.common.module = (struct hw_module_t *) module; |
| 572 | stdev->device.common.close = stdev_close; |
| 573 | stdev->device.get_properties = stdev_get_properties; |
| 574 | stdev->device.load_sound_model = stdev_load_sound_model; |
| 575 | stdev->device.unload_sound_model = stdev_unload_sound_model; |
| 576 | stdev->device.start_recognition = stdev_start_recognition; |
| 577 | stdev->device.stop_recognition = stdev_stop_recognition; |
| 578 | |
| 579 | pthread_mutex_init(&stdev->lock, (const pthread_mutexattr_t *) NULL); |
Eric Laurent | cbca905 | 2014-04-18 17:54:10 -0700 | [diff] [blame] | 580 | |
| 581 | *device = &stdev->device.common; |
| 582 | |
| 583 | return 0; |
| 584 | } |
| 585 | |
| 586 | static struct hw_module_methods_t hal_module_methods = { |
| 587 | .open = stdev_open, |
| 588 | }; |
| 589 | |
| 590 | struct sound_trigger_module HAL_MODULE_INFO_SYM = { |
| 591 | .common = { |
| 592 | .tag = HARDWARE_MODULE_TAG, |
| 593 | .module_api_version = SOUND_TRIGGER_MODULE_API_VERSION_1_0, |
| 594 | .hal_api_version = HARDWARE_HAL_API_VERSION, |
| 595 | .id = SOUND_TRIGGER_HARDWARE_MODULE_ID, |
| 596 | .name = "Default sound trigger HAL", |
| 597 | .author = "The Android Open Source Project", |
| 598 | .methods = &hal_module_methods, |
| 599 | }, |
| 600 | }; |
Ryan Bavetta | cc6541c | 2016-02-09 21:20:51 -0800 | [diff] [blame^] | 601 | |