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