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 |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 22 | * |
Arunesh Mishra | 7db0a7a | 2016-02-19 16:20:10 -0800 | [diff] [blame] | 23 | * telnet localhost 14035 |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 24 | * |
Arunesh Mishra | 7db0a7a | 2016-02-19 16:20:10 -0800 | [diff] [blame] | 25 | * Commands include: |
| 26 | * ls : Lists all models that have been loaded. |
| 27 | * trig <index> : Sends a recognition event for the model at the given index. |
| 28 | * update <index> : Sends a model update event for the model at the given index. |
| 29 | * close : Closes the network connection. |
Ryan Bavetta | c2bdc55 | 2016-02-15 15:51:46 -0800 | [diff] [blame] | 30 | * |
Ryan Bavetta | be0c8fd | 2016-02-01 15:17:12 -0800 | [diff] [blame] | 31 | * To enable this file, you can make with command line parameter |
| 32 | * SOUND_TRIGGER_USE_STUB_MODULE=1 |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 33 | */ |
| 34 | |
Eric Laurent | cbca905 | 2014-04-18 17:54:10 -0700 | [diff] [blame] | 35 | #define LOG_TAG "sound_trigger_hw_default" |
Ryan Bavetta | cc6541c | 2016-02-09 21:20:51 -0800 | [diff] [blame] | 36 | #define LOG_NDEBUG 1 |
Arunesh Mishra | f1d59fc | 2016-02-15 17:48:27 -0800 | [diff] [blame] | 37 | #define PARSE_BUF_LEN 1024 // Length of the parsing buffer.S |
| 38 | |
| 39 | // The following commands work with the network port: |
| 40 | #define COMMAND_LS "ls" |
| 41 | #define COMMAND_TRIGGER "trig" // Argument: model index. |
Arunesh Mishra | 7db0a7a | 2016-02-19 16:20:10 -0800 | [diff] [blame] | 42 | #define COMMAND_UPDATE "update" // Argument: model index. |
Arunesh Mishra | f1d59fc | 2016-02-15 17:48:27 -0800 | [diff] [blame] | 43 | #define COMMAND_CLOSE "close" // Close just closes the network port, keeps thread running. |
| 44 | #define COMMAND_END "end" // Closes connection and stops the thread. |
| 45 | |
| 46 | #define ERROR_BAD_COMMAND "Bad command" |
Eric Laurent | cbca905 | 2014-04-18 17:54:10 -0700 | [diff] [blame] | 47 | |
| 48 | #include <errno.h> |
Arunesh Mishra | f1d59fc | 2016-02-15 17:48:27 -0800 | [diff] [blame] | 49 | #include <stdarg.h> |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 50 | #include <stdio.h> |
| 51 | #include <stdlib.h> |
| 52 | #include <string.h> |
| 53 | #include <unistd.h> |
| 54 | #include <arpa/inet.h> |
| 55 | #include <sys/types.h> |
| 56 | #include <netinet/in.h> |
| 57 | #include <sys/socket.h> |
| 58 | |
| 59 | #include <errno.h> |
Eric Laurent | cbca905 | 2014-04-18 17:54:10 -0700 | [diff] [blame] | 60 | #include <pthread.h> |
| 61 | #include <sys/prctl.h> |
| 62 | #include <cutils/log.h> |
| 63 | |
| 64 | #include <hardware/hardware.h> |
| 65 | #include <system/sound_trigger.h> |
| 66 | #include <hardware/sound_trigger.h> |
| 67 | |
| 68 | static const struct sound_trigger_properties hw_properties = { |
| 69 | "The Android Open Source Project", // implementor |
| 70 | "Sound Trigger stub HAL", // description |
| 71 | 1, // version |
| 72 | { 0xed7a7d60, 0xc65e, 0x11e3, 0x9be4, { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } }, // uuid |
Arunesh Mishra | f1d59fc | 2016-02-15 17:48:27 -0800 | [diff] [blame] | 73 | 4, // max_sound_models |
Eric Laurent | cbca905 | 2014-04-18 17:54:10 -0700 | [diff] [blame] | 74 | 1, // max_key_phrases |
| 75 | 1, // max_users |
| 76 | RECOGNITION_MODE_VOICE_TRIGGER, // recognition_modes |
| 77 | false, // capture_transition |
| 78 | 0, // max_buffer_ms |
| 79 | false, // concurrent_capture |
Eric Laurent | 83cd830 | 2014-07-30 08:58:39 -0700 | [diff] [blame] | 80 | false, // trigger_in_event |
Eric Laurent | cbca905 | 2014-04-18 17:54:10 -0700 | [diff] [blame] | 81 | 0 // power_consumption_mw |
| 82 | }; |
| 83 | |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 84 | struct recognition_context { |
Ryan Bavetta | cc6541c | 2016-02-09 21:20:51 -0800 | [diff] [blame] | 85 | // Sound Model information, added in method load_sound_model |
| 86 | sound_model_handle_t model_handle; |
| 87 | sound_trigger_sound_model_type_t model_type; |
| 88 | sound_model_callback_t model_callback; |
| 89 | void *model_cookie; |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 90 | |
Ryan Bavetta | cc6541c | 2016-02-09 21:20:51 -0800 | [diff] [blame] | 91 | // Sound Model information, added in start_recognition |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 92 | struct sound_trigger_recognition_config *config; |
| 93 | recognition_callback_t recognition_callback; |
| 94 | void *recognition_cookie; |
Ryan Bavetta | cc6541c | 2016-02-09 21:20:51 -0800 | [diff] [blame] | 95 | |
| 96 | // Next recognition_context in the linked list |
| 97 | struct recognition_context *next; |
Ryan Bavetta | be0c8fd | 2016-02-01 15:17:12 -0800 | [diff] [blame] | 98 | }; |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 99 | |
Arunesh Mishra | f1d59fc | 2016-02-15 17:48:27 -0800 | [diff] [blame] | 100 | char tmp_write_buffer[PARSE_BUF_LEN]; |
| 101 | |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 102 | struct stub_sound_trigger_device { |
| 103 | struct sound_trigger_hw_device device; |
Eric Laurent | cbca905 | 2014-04-18 17:54:10 -0700 | [diff] [blame] | 104 | pthread_mutex_t lock; |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 105 | pthread_t callback_thread; |
| 106 | |
Ryan Bavetta | cc6541c | 2016-02-09 21:20:51 -0800 | [diff] [blame] | 107 | // Recognition contexts are stored as a linked list |
| 108 | struct recognition_context *root_model_context; |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 109 | |
| 110 | int next_sound_model_id; |
Eric Laurent | cbca905 | 2014-04-18 17:54:10 -0700 | [diff] [blame] | 111 | }; |
| 112 | |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 113 | /* Will reuse ids when overflow occurs */ |
Ryan Bavetta | f9f0871 | 2016-02-19 21:18:46 -0800 | [diff] [blame] | 114 | static sound_model_handle_t generate_sound_model_handle(const struct sound_trigger_hw_device *dev) { |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 115 | struct stub_sound_trigger_device *stdev = (struct stub_sound_trigger_device *)dev; |
| 116 | int new_id = stdev->next_sound_model_id; |
| 117 | ++stdev->next_sound_model_id; |
| 118 | if (stdev->next_sound_model_id == 0) { |
| 119 | stdev->next_sound_model_id = 1; |
| 120 | } |
Ryan Bavetta | f9f0871 | 2016-02-19 21:18:46 -0800 | [diff] [blame] | 121 | return (sound_model_handle_t) new_id; |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 122 | } |
Eric Laurent | cbca905 | 2014-04-18 17:54:10 -0700 | [diff] [blame] | 123 | |
Arunesh Mishra | 7db0a7a | 2016-02-19 16:20:10 -0800 | [diff] [blame] | 124 | bool parse_socket_data(int conn_socket, struct stub_sound_trigger_device* stdev); |
| 125 | |
Ryan Bavetta | cc6541c | 2016-02-09 21:20:51 -0800 | [diff] [blame] | 126 | static char *sound_trigger_event_alloc(sound_model_handle_t handle, |
| 127 | sound_trigger_sound_model_type_t model_type, |
| 128 | struct sound_trigger_recognition_config *config) { |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 129 | char *data; |
Ryan Bavetta | cc6541c | 2016-02-09 21:20:51 -0800 | [diff] [blame] | 130 | if (model_type == SOUND_MODEL_TYPE_KEYPHRASE) { |
| 131 | struct sound_trigger_phrase_recognition_event *event; |
| 132 | data = (char *)calloc(1, sizeof(struct sound_trigger_phrase_recognition_event)); |
| 133 | if (!data) |
| 134 | return NULL; |
| 135 | event = (struct sound_trigger_phrase_recognition_event *)data; |
| 136 | event->common.status = RECOGNITION_STATUS_SUCCESS; |
| 137 | event->common.type = SOUND_MODEL_TYPE_KEYPHRASE; |
| 138 | event->common.model = handle; |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 139 | |
Ryan Bavetta | cc6541c | 2016-02-09 21:20:51 -0800 | [diff] [blame] | 140 | if (config) { |
| 141 | unsigned int i; |
| 142 | |
| 143 | event->num_phrases = config->num_phrases; |
| 144 | if (event->num_phrases > SOUND_TRIGGER_MAX_PHRASES) |
| 145 | event->num_phrases = SOUND_TRIGGER_MAX_PHRASES; |
| 146 | for (i=0; i < event->num_phrases; i++) |
| 147 | memcpy(&event->phrase_extras[i], |
| 148 | &config->phrases[i], |
| 149 | sizeof(struct sound_trigger_phrase_recognition_extra)); |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 150 | } |
Ryan Bavetta | cc6541c | 2016-02-09 21:20:51 -0800 | [diff] [blame] | 151 | |
| 152 | event->num_phrases = 1; |
| 153 | event->phrase_extras[0].confidence_level = 100; |
| 154 | event->phrase_extras[0].num_levels = 1; |
| 155 | event->phrase_extras[0].levels[0].level = 100; |
| 156 | event->phrase_extras[0].levels[0].user_id = 0; |
| 157 | // Signify that all the data is comming through streaming, not through the buffer. |
| 158 | event->common.capture_available = true; |
| 159 | event->common.audio_config = AUDIO_CONFIG_INITIALIZER; |
| 160 | event->common.audio_config.sample_rate = 16000; |
| 161 | event->common.audio_config.channel_mask = AUDIO_CHANNEL_IN_MONO; |
| 162 | event->common.audio_config.format = AUDIO_FORMAT_PCM_16_BIT; |
| 163 | } else if (model_type == SOUND_MODEL_TYPE_GENERIC) { |
| 164 | struct sound_trigger_generic_recognition_event *event; |
| 165 | data = (char *)calloc(1, sizeof(struct sound_trigger_generic_recognition_event)); |
| 166 | if (!data) |
| 167 | return NULL; |
| 168 | event = (struct sound_trigger_generic_recognition_event *)data; |
| 169 | event->common.status = RECOGNITION_STATUS_SUCCESS; |
| 170 | event->common.type = SOUND_MODEL_TYPE_GENERIC; |
| 171 | event->common.model = handle; |
| 172 | |
| 173 | // Signify that all the data is comming through streaming, not through the buffer. |
| 174 | event->common.capture_available = true; |
| 175 | event->common.audio_config = AUDIO_CONFIG_INITIALIZER; |
| 176 | event->common.audio_config.sample_rate = 16000; |
| 177 | event->common.audio_config.channel_mask = AUDIO_CHANNEL_IN_MONO; |
| 178 | event->common.audio_config.format = AUDIO_FORMAT_PCM_16_BIT; |
| 179 | } else { |
| 180 | ALOGW("No Valid Event Type Known"); |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 181 | return NULL; |
| 182 | } |
Ryan Bavetta | cc6541c | 2016-02-09 21:20:51 -0800 | [diff] [blame] | 183 | return data; |
| 184 | } |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 185 | |
Ryan Bavetta | cc6541c | 2016-02-09 21:20:51 -0800 | [diff] [blame] | 186 | static void send_recognition_event(sound_model_handle_t model_handle, |
| 187 | sound_trigger_sound_model_type_t model_type, |
| 188 | recognition_callback_t recognition_callback, void *recognition_cookie, |
| 189 | struct sound_trigger_recognition_config *config) { |
| 190 | if (recognition_callback == NULL) { |
| 191 | ALOGI("%s No matching callback for handle %d", __func__, model_handle); |
| 192 | return; |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 193 | } |
| 194 | |
Ryan Bavetta | cc6541c | 2016-02-09 21:20:51 -0800 | [diff] [blame] | 195 | if (model_type == SOUND_MODEL_TYPE_KEYPHRASE) { |
| 196 | struct sound_trigger_phrase_recognition_event *event; |
| 197 | event = (struct sound_trigger_phrase_recognition_event *) |
| 198 | sound_trigger_event_alloc(model_handle, model_type, config); |
| 199 | if (event) { |
Ryan Bavetta | c2bdc55 | 2016-02-15 15:51:46 -0800 | [diff] [blame] | 200 | recognition_callback(event, recognition_cookie); |
Ryan Bavetta | cc6541c | 2016-02-09 21:20:51 -0800 | [diff] [blame] | 201 | free(event); |
| 202 | } |
| 203 | } else if (model_type == SOUND_MODEL_TYPE_GENERIC) { |
| 204 | struct sound_trigger_generic_recognition_event *event; |
| 205 | event = (struct sound_trigger_generic_recognition_event *) |
| 206 | sound_trigger_event_alloc(model_handle, model_type, config); |
| 207 | if (event) { |
Ryan Bavetta | c2bdc55 | 2016-02-15 15:51:46 -0800 | [diff] [blame] | 208 | recognition_callback(event, recognition_cookie); |
Ryan Bavetta | cc6541c | 2016-02-09 21:20:51 -0800 | [diff] [blame] | 209 | free(event); |
| 210 | } |
| 211 | } else { |
| 212 | ALOGI("Unknown Sound Model Type, No Event to Send"); |
| 213 | } |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 214 | } |
| 215 | |
Ryan Bavetta | c2bdc55 | 2016-02-15 15:51:46 -0800 | [diff] [blame] | 216 | static void send_model_event(sound_model_handle_t model_handle, |
| 217 | sound_model_callback_t model_callback, void *model_cookie) { |
| 218 | |
| 219 | if (model_callback == NULL) { |
| 220 | ALOGI("%s No matching callback for handle %d", __func__, model_handle); |
| 221 | return; |
| 222 | } |
| 223 | |
| 224 | char *data; |
| 225 | data = (char *)calloc(1, sizeof(struct sound_trigger_model_event)); |
| 226 | if (!data) { |
| 227 | ALOGW("%s Could not allocate event %d", __func__, model_handle); |
| 228 | return; |
| 229 | } |
| 230 | |
| 231 | struct sound_trigger_model_event *event; |
| 232 | event = (struct sound_trigger_model_event *)data; |
| 233 | event->status = SOUND_MODEL_STATUS_UPDATED; |
| 234 | event->model = model_handle; |
| 235 | |
| 236 | if (event) { |
| 237 | model_callback(&event, model_cookie); |
| 238 | free(event); |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | static bool recognition_callback_exists(struct stub_sound_trigger_device *stdev) { |
| 243 | bool callback_found = false; |
| 244 | if (stdev->root_model_context) { |
| 245 | struct recognition_context *current_model_context = stdev->root_model_context; |
| 246 | while(current_model_context) { |
| 247 | if (current_model_context->recognition_callback != NULL) { |
| 248 | callback_found = true; |
| 249 | break; |
| 250 | } |
| 251 | current_model_context = current_model_context->next; |
| 252 | } |
| 253 | } |
| 254 | return callback_found; |
| 255 | } |
| 256 | |
| 257 | static struct recognition_context * get_model_context(struct stub_sound_trigger_device *stdev, |
| 258 | sound_model_handle_t handle) { |
| 259 | struct recognition_context *model_context = NULL; |
| 260 | if (stdev->root_model_context) { |
| 261 | struct recognition_context *current_model_context = stdev->root_model_context; |
| 262 | while(current_model_context) { |
| 263 | if (current_model_context->model_handle == handle) { |
| 264 | model_context = current_model_context; |
| 265 | break; |
| 266 | } |
| 267 | current_model_context = current_model_context->next; |
| 268 | } |
| 269 | } |
| 270 | return model_context; |
| 271 | } |
| 272 | |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 273 | static void *callback_thread_loop(void *context) { |
Eric Laurent | cbca905 | 2014-04-18 17:54:10 -0700 | [diff] [blame] | 274 | struct stub_sound_trigger_device *stdev = (struct stub_sound_trigger_device *)context; |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 275 | struct sockaddr_in incoming_info; |
| 276 | struct sockaddr_in self_info; |
| 277 | int self_socket; |
| 278 | socklen_t sock_size = sizeof(struct sockaddr_in); |
| 279 | memset(&self_info, 0, sizeof(self_info)); |
| 280 | self_info.sin_family = AF_INET; |
| 281 | self_info.sin_addr.s_addr = htonl(INADDR_ANY); |
| 282 | self_info.sin_port = htons(14035); |
Eric Laurent | cbca905 | 2014-04-18 17:54:10 -0700 | [diff] [blame] | 283 | |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 284 | bool exit = false; |
| 285 | while(!exit) { |
| 286 | int received_count; |
Ryan Bavetta | c2bdc55 | 2016-02-15 15:51:46 -0800 | [diff] [blame] | 287 | int requested_count = 2; |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 288 | char buffer[requested_count]; |
| 289 | ALOGE("Opening socket"); |
| 290 | self_socket = socket(AF_INET, SOCK_STREAM, 0); |
| 291 | if (self_socket < 0) { |
| 292 | ALOGE("Error on socket creation"); |
| 293 | exit = true; |
| 294 | } else { |
| 295 | ALOGI("Socket created"); |
| 296 | } |
Eric Laurent | cbca905 | 2014-04-18 17:54:10 -0700 | [diff] [blame] | 297 | |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 298 | int reuse = 1; |
| 299 | if (setsockopt(self_socket, SOL_SOCKET, SO_REUSEADDR, (const char*)&reuse, sizeof(reuse)) < 0) { |
| 300 | ALOGE("setsockopt(SO_REUSEADDR) failed"); |
| 301 | } |
| 302 | |
| 303 | int bind_result = bind(self_socket, (struct sockaddr *)&self_info, sizeof(struct sockaddr)); |
| 304 | if (bind_result < 0) { |
| 305 | ALOGE("Error on bind"); |
| 306 | exit = true; |
| 307 | } |
| 308 | |
| 309 | int listen_result = listen(self_socket, 1); |
| 310 | if (listen_result < 0) { |
| 311 | ALOGE("Error on Listen"); |
| 312 | exit = true; |
| 313 | } |
| 314 | |
| 315 | while(!exit) { |
| 316 | int con_socket = accept(self_socket, (struct sockaddr *)&incoming_info, &sock_size); |
| 317 | if (!con_socket) { |
| 318 | ALOGE("Lost socket, cannot send trigger"); |
| 319 | break; |
| 320 | } |
| 321 | ALOGI("Connection from %s", inet_ntoa(incoming_info.sin_addr)); |
Arunesh Mishra | f1d59fc | 2016-02-15 17:48:27 -0800 | [diff] [blame] | 322 | if (!parse_socket_data(con_socket, stdev)) { |
| 323 | ALOGI("Done processing commands over network. Stopping thread."); |
| 324 | exit = true; |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 325 | } |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 326 | close(con_socket); |
| 327 | } |
| 328 | ALOGE("Closing socket"); |
| 329 | close(self_socket); |
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 | |
| 332 | return NULL; |
| 333 | } |
| 334 | |
Arunesh Mishra | f1d59fc | 2016-02-15 17:48:27 -0800 | [diff] [blame] | 335 | void write_bad_command_error(int conn_socket, char* command) { |
| 336 | int num = snprintf(tmp_write_buffer, PARSE_BUF_LEN, "Bad command received: %s", command); |
| 337 | tmp_write_buffer[PARSE_BUF_LEN - 1] = '\0'; // Just to be sure. |
| 338 | tmp_write_buffer[PARSE_BUF_LEN - 2] = '\n'; |
| 339 | write(conn_socket, tmp_write_buffer, num); |
| 340 | } |
| 341 | |
| 342 | void write_string(int conn_socket, char* str) { |
| 343 | int num = snprintf(tmp_write_buffer, PARSE_BUF_LEN, "%s", str); |
| 344 | tmp_write_buffer[PARSE_BUF_LEN - 1] = '\0'; |
| 345 | tmp_write_buffer[PARSE_BUF_LEN - 2] = '\n'; |
| 346 | write(conn_socket, tmp_write_buffer, num); |
| 347 | } |
| 348 | |
| 349 | void write_vastr(int conn_socket, char* format, ...) { |
| 350 | va_list argptr; |
| 351 | va_start(argptr, format); |
| 352 | int num = vsnprintf(tmp_write_buffer, PARSE_BUF_LEN, format, argptr); |
| 353 | va_end(argptr); |
| 354 | tmp_write_buffer[PARSE_BUF_LEN - 1] = '\0'; |
| 355 | tmp_write_buffer[PARSE_BUF_LEN - 2] = '\n'; |
| 356 | write(conn_socket, tmp_write_buffer, num); |
| 357 | } |
| 358 | |
| 359 | void list_models(int conn_socket, char* buffer, |
| 360 | struct stub_sound_trigger_device* stdev) { |
| 361 | ALOGI("%s", __func__); |
| 362 | struct recognition_context *last_model_context = stdev->root_model_context; |
Ryan Bavetta | f9f0871 | 2016-02-19 21:18:46 -0800 | [diff] [blame] | 363 | unsigned int model_index = 0; |
Arunesh Mishra | f1d59fc | 2016-02-15 17:48:27 -0800 | [diff] [blame] | 364 | if (!last_model_context) { |
| 365 | ALOGI("ZERO Models exist."); |
| 366 | write_string(conn_socket, "Zero models exist.\n"); |
| 367 | } |
| 368 | while (last_model_context) { |
| 369 | write_vastr(conn_socket, "Model Index: %d\n", model_index); |
| 370 | ALOGI("Model Index: %d\n", model_index); |
| 371 | write_vastr(conn_socket, "Model handle: %d\n", |
| 372 | last_model_context->model_handle); |
| 373 | ALOGI("Model handle: %d\n", last_model_context->model_handle); |
| 374 | sound_trigger_sound_model_type_t model_type = last_model_context->model_type; |
| 375 | |
| 376 | if (model_type == SOUND_MODEL_TYPE_KEYPHRASE) { |
| 377 | write_string(conn_socket, "Keyphrase sound Model.\n"); |
| 378 | ALOGI("Keyphrase sound Model.\n"); |
| 379 | } else if (model_type == SOUND_MODEL_TYPE_GENERIC) { |
| 380 | write_string(conn_socket, "Generic sound Model.\n"); |
| 381 | ALOGI("Generic sound Model.\n"); |
| 382 | } else { |
| 383 | write_vastr(conn_socket, "Unknown sound model type: %d\n", |
| 384 | model_type); |
| 385 | ALOGI("Unknown sound model type: %d\n", model_type); |
| 386 | } |
| 387 | write_string(conn_socket, "----\n\n"); |
| 388 | ALOGI("----\n\n"); |
| 389 | last_model_context = last_model_context->next; |
| 390 | model_index++; |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | // Returns model at the given index, null otherwise (error, doesn't exist, etc). |
| 395 | // Note that here index starts from zero. |
| 396 | struct recognition_context* fetch_model_at_index( |
| 397 | struct stub_sound_trigger_device* stdev, int index) { |
| 398 | ALOGI("%s", __func__); |
| 399 | struct recognition_context *model_context = NULL; |
| 400 | struct recognition_context *last_model_context = stdev->root_model_context; |
Ryan Bavetta | f9f0871 | 2016-02-19 21:18:46 -0800 | [diff] [blame] | 401 | unsigned int model_index = 0; |
Arunesh Mishra | f1d59fc | 2016-02-15 17:48:27 -0800 | [diff] [blame] | 402 | while(last_model_context) { |
| 403 | if (model_index == index) { |
| 404 | model_context = last_model_context; |
| 405 | break; |
| 406 | } |
| 407 | last_model_context = last_model_context->next; |
| 408 | model_index++; |
| 409 | } |
| 410 | return model_context; |
| 411 | } |
| 412 | |
| 413 | void send_trigger(int conn_socket, char* buffer, |
| 414 | struct stub_sound_trigger_device* stdev) { |
| 415 | ALOGI("%s", __func__); |
| 416 | char* model_handle_str = strtok(NULL, " "); |
| 417 | if (model_handle_str == NULL) { |
| 418 | write_string(conn_socket, "Bad sound model id.\n"); |
| 419 | return; |
| 420 | } |
| 421 | int index = -1; |
| 422 | if (sscanf(model_handle_str, "%d", &index) <= 0) { |
| 423 | write_vastr(conn_socket, "Unable to parse sound model index: %s\n", model_handle_str); |
| 424 | return; |
| 425 | } |
| 426 | |
| 427 | if (index < (int)hw_properties.max_sound_models) { |
| 428 | ALOGI("Going to send trigger for model index #%d", index ); |
| 429 | struct recognition_context *model_context = fetch_model_at_index(stdev, index); |
| 430 | if (model_context) { |
| 431 | send_recognition_event(model_context->model_handle, |
| 432 | model_context->model_type, |
| 433 | model_context->recognition_callback, |
| 434 | model_context->recognition_cookie, |
| 435 | model_context->config); |
| 436 | } else { |
| 437 | ALOGI("Sound Model Does Not Exist at this Index: %d", index); |
| 438 | write_string(conn_socket, "Sound Model Does Not Exist at given Index.\n"); |
| 439 | } |
| 440 | } |
| 441 | } |
| 442 | |
| 443 | void process_send_model_event(int conn_socket, char* buffer, |
| 444 | struct stub_sound_trigger_device* stdev) { |
| 445 | ALOGI("%s", __func__); |
| 446 | char* model_handle_str = strtok(NULL, " "); |
| 447 | if (model_handle_str == NULL) { |
| 448 | write_string(conn_socket, "Bad sound model id.\n"); |
| 449 | return; |
| 450 | } |
| 451 | int index = -1; |
| 452 | if (sscanf(model_handle_str, "%d", &index) <= 0) { |
| 453 | write_vastr(conn_socket, "Unable to parse sound model index: %s\n", model_handle_str); |
| 454 | return; |
| 455 | } |
| 456 | |
| 457 | if (index < (int)hw_properties.max_sound_models) { |
| 458 | ALOGI("Going to model event for model index #%d", index ); |
| 459 | struct recognition_context *model_context = fetch_model_at_index(stdev, index); |
| 460 | if (model_context) { |
| 461 | |
| 462 | send_model_event(model_context->model_handle, |
| 463 | model_context->model_callback, |
| 464 | model_context->model_cookie); |
| 465 | } else { |
| 466 | ALOGI("Sound Model Does Not Exist at this Index: %d", index); |
| 467 | write_string(conn_socket, "Sound Model Does Not Exist at given Index.\n"); |
| 468 | } |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | // Gets the next word from buffer, replaces '\n' or ' ' with '\0'. |
| 473 | char* get_command(char* buffer) { |
| 474 | char* command = strtok(buffer, " "); |
| 475 | char* newline = strchr(command, '\n'); |
| 476 | if (newline != NULL) { |
| 477 | *newline = '\0'; |
| 478 | } |
| 479 | return command; |
| 480 | } |
| 481 | |
| 482 | // Parses data coming in from the local socket, executes commands. Returns when |
| 483 | // done. Return code indicates whether the server should continue listening or |
| 484 | // abort (true if continue listening). |
| 485 | bool parse_socket_data(int conn_socket, struct stub_sound_trigger_device* stdev) { |
| 486 | ALOGI("Calling parse_socket_data"); |
| 487 | bool input_done = false; |
| 488 | char buffer[PARSE_BUF_LEN]; |
| 489 | FILE* input_fp = fdopen(conn_socket, "r"); |
Arunesh Mishra | f1d59fc | 2016-02-15 17:48:27 -0800 | [diff] [blame] | 490 | bool continue_listening = true; |
| 491 | while(!input_done) { |
| 492 | if (fgets(buffer, PARSE_BUF_LEN, input_fp) != NULL) { |
Arunesh Mishra | 7db0a7a | 2016-02-19 16:20:10 -0800 | [diff] [blame] | 493 | pthread_mutex_lock(&stdev->lock); |
Arunesh Mishra | f1d59fc | 2016-02-15 17:48:27 -0800 | [diff] [blame] | 494 | char* command = strtok(buffer, " \n"); |
| 495 | if (command == NULL) { |
| 496 | write_bad_command_error(conn_socket, command); |
| 497 | } else if (strncmp(command, COMMAND_LS, 2) == 0) { |
| 498 | list_models(conn_socket, buffer, stdev); |
| 499 | } else if (strcmp(command, COMMAND_TRIGGER) == 0) { |
| 500 | send_trigger(conn_socket, buffer, stdev); |
Arunesh Mishra | 7db0a7a | 2016-02-19 16:20:10 -0800 | [diff] [blame] | 501 | } else if (strcmp(command, COMMAND_UPDATE) == 0) { |
| 502 | process_send_model_event(conn_socket, buffer, stdev); |
Arunesh Mishra | f1d59fc | 2016-02-15 17:48:27 -0800 | [diff] [blame] | 503 | } else if (strncmp(command, COMMAND_CLOSE, 5) == 0) { |
| 504 | ALOGI("Closing this connection."); |
| 505 | write_string(conn_socket, "Closing this connection."); |
| 506 | break; |
| 507 | } else if (strncmp(command, COMMAND_END, 3) == 0) { |
| 508 | ALOGI("End command received."); |
| 509 | write_string(conn_socket, "End command received. Stopping connection."); |
| 510 | continue_listening = false; |
| 511 | break; |
Arunesh Mishra | 7db0a7a | 2016-02-19 16:20:10 -0800 | [diff] [blame] | 512 | } else { |
| 513 | write_vastr(conn_socket, "Bad command %s.\n", command); |
Arunesh Mishra | f1d59fc | 2016-02-15 17:48:27 -0800 | [diff] [blame] | 514 | } |
Arunesh Mishra | 7db0a7a | 2016-02-19 16:20:10 -0800 | [diff] [blame] | 515 | pthread_mutex_unlock(&stdev->lock); |
Arunesh Mishra | f1d59fc | 2016-02-15 17:48:27 -0800 | [diff] [blame] | 516 | } else { |
| 517 | ALOGI("parse_socket_data done (got null)"); |
| 518 | input_done = true; // break. |
| 519 | } |
| 520 | } |
Arunesh Mishra | f1d59fc | 2016-02-15 17:48:27 -0800 | [diff] [blame] | 521 | return continue_listening; |
| 522 | } |
| 523 | |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 524 | static void send_loop_kill_signal() { |
| 525 | ALOGI("Sending loop thread kill signal"); |
| 526 | int self_socket = socket(AF_INET, SOCK_STREAM, 0); |
| 527 | struct sockaddr_in remote_info; |
| 528 | memset(&remote_info, 0, sizeof(remote_info)); |
| 529 | remote_info.sin_family = AF_INET; |
| 530 | remote_info.sin_addr.s_addr = htonl(INADDR_LOOPBACK); |
| 531 | remote_info.sin_port = htons(14035); |
| 532 | if (connect(self_socket, (struct sockaddr *)&remote_info, sizeof(struct sockaddr)) == 0) { |
Ryan Bavetta | f9f0871 | 2016-02-19 21:18:46 -0800 | [diff] [blame] | 533 | send(self_socket, COMMAND_END, 3, 0); |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 534 | } else { |
| 535 | ALOGI("Could not connect"); |
| 536 | } |
| 537 | close(self_socket); |
| 538 | ALOGI("Sent loop thread kill signal"); |
| 539 | } |
| 540 | |
Eric Laurent | cbca905 | 2014-04-18 17:54:10 -0700 | [diff] [blame] | 541 | static int stdev_get_properties(const struct sound_trigger_hw_device *dev, |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 542 | struct sound_trigger_properties *properties) { |
Eric Laurent | cbca905 | 2014-04-18 17:54:10 -0700 | [diff] [blame] | 543 | struct stub_sound_trigger_device *stdev = (struct stub_sound_trigger_device *)dev; |
| 544 | |
| 545 | ALOGI("%s", __func__); |
| 546 | if (properties == NULL) |
| 547 | return -EINVAL; |
| 548 | memcpy(properties, &hw_properties, sizeof(struct sound_trigger_properties)); |
| 549 | return 0; |
| 550 | } |
| 551 | |
| 552 | static int stdev_load_sound_model(const struct sound_trigger_hw_device *dev, |
| 553 | struct sound_trigger_sound_model *sound_model, |
| 554 | sound_model_callback_t callback, |
| 555 | void *cookie, |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 556 | sound_model_handle_t *handle) { |
Arunesh Mishra | f1d59fc | 2016-02-15 17:48:27 -0800 | [diff] [blame] | 557 | ALOGI("load_sound_model."); |
Eric Laurent | cbca905 | 2014-04-18 17:54:10 -0700 | [diff] [blame] | 558 | struct stub_sound_trigger_device *stdev = (struct stub_sound_trigger_device *)dev; |
| 559 | int status = 0; |
| 560 | |
| 561 | ALOGI("%s stdev %p", __func__, stdev); |
| 562 | pthread_mutex_lock(&stdev->lock); |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 563 | |
Eric Laurent | cbca905 | 2014-04-18 17:54:10 -0700 | [diff] [blame] | 564 | if (handle == NULL || sound_model == NULL) { |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 565 | pthread_mutex_unlock(&stdev->lock); |
| 566 | return -EINVAL; |
Eric Laurent | cbca905 | 2014-04-18 17:54:10 -0700 | [diff] [blame] | 567 | } |
| 568 | if (sound_model->data_size == 0 || |
| 569 | sound_model->data_offset < sizeof(struct sound_trigger_sound_model)) { |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 570 | pthread_mutex_unlock(&stdev->lock); |
| 571 | return -EINVAL; |
Eric Laurent | cbca905 | 2014-04-18 17:54:10 -0700 | [diff] [blame] | 572 | } |
| 573 | |
Ryan Bavetta | cc6541c | 2016-02-09 21:20:51 -0800 | [diff] [blame] | 574 | struct recognition_context *model_context; |
| 575 | model_context = malloc(sizeof(struct recognition_context)); |
| 576 | if(!model_context) { |
| 577 | ALOGW("Could not allocate recognition_context"); |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 578 | pthread_mutex_unlock(&stdev->lock); |
| 579 | return -ENOSYS; |
| 580 | } |
| 581 | |
Ryan Bavetta | cc6541c | 2016-02-09 21:20:51 -0800 | [diff] [blame] | 582 | // Add the new model context to the recognition_context linked list |
| 583 | if (stdev->root_model_context) { |
| 584 | // Find the tail |
Ryan Bavetta | c2bdc55 | 2016-02-15 15:51:46 -0800 | [diff] [blame] | 585 | struct recognition_context *current_model_context = stdev->root_model_context; |
Ryan Bavetta | f9f0871 | 2016-02-19 21:18:46 -0800 | [diff] [blame] | 586 | unsigned int model_count = 0; |
Ryan Bavetta | c2bdc55 | 2016-02-15 15:51:46 -0800 | [diff] [blame] | 587 | while(current_model_context->next) { |
| 588 | current_model_context = current_model_context->next; |
Ryan Bavetta | cc6541c | 2016-02-09 21:20:51 -0800 | [diff] [blame] | 589 | model_count++; |
| 590 | if (model_count >= hw_properties.max_sound_models) { |
| 591 | ALOGW("Can't load model: reached max sound model limit"); |
| 592 | free(model_context); |
| 593 | pthread_mutex_unlock(&stdev->lock); |
| 594 | return -ENOSYS; |
| 595 | } |
| 596 | } |
Ryan Bavetta | c2bdc55 | 2016-02-15 15:51:46 -0800 | [diff] [blame] | 597 | current_model_context->next = model_context; |
Ryan Bavetta | cc6541c | 2016-02-09 21:20:51 -0800 | [diff] [blame] | 598 | } else { |
| 599 | stdev->root_model_context = model_context; |
| 600 | } |
| 601 | |
Ryan Bavetta | f9f0871 | 2016-02-19 21:18:46 -0800 | [diff] [blame] | 602 | model_context->model_handle = generate_sound_model_handle(dev); |
Ryan Bavetta | cc6541c | 2016-02-09 21:20:51 -0800 | [diff] [blame] | 603 | *handle = model_context->model_handle; |
| 604 | model_context->model_type = sound_model->type; |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 605 | |
Eric Laurent | cbca905 | 2014-04-18 17:54:10 -0700 | [diff] [blame] | 606 | char *data = (char *)sound_model + sound_model->data_offset; |
| 607 | ALOGI("%s data size %d data %d - %d", __func__, |
| 608 | sound_model->data_size, data[0], data[sound_model->data_size - 1]); |
Ryan Bavetta | cc6541c | 2016-02-09 21:20:51 -0800 | [diff] [blame] | 609 | model_context->model_callback = callback; |
| 610 | model_context->model_cookie = cookie; |
| 611 | model_context->config = NULL; |
| 612 | model_context->recognition_callback = NULL; |
| 613 | model_context->recognition_cookie = NULL; |
Ryan Bavetta | f9f0871 | 2016-02-19 21:18:46 -0800 | [diff] [blame] | 614 | model_context->next = NULL; |
Arunesh Mishra | f1d59fc | 2016-02-15 17:48:27 -0800 | [diff] [blame] | 615 | ALOGI("Sound model loaded: Handle %d ", *handle); |
Eric Laurent | cbca905 | 2014-04-18 17:54:10 -0700 | [diff] [blame] | 616 | |
Eric Laurent | cbca905 | 2014-04-18 17:54:10 -0700 | [diff] [blame] | 617 | pthread_mutex_unlock(&stdev->lock); |
| 618 | return status; |
| 619 | } |
| 620 | |
| 621 | 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] | 622 | sound_model_handle_t handle) { |
Ryan Bavetta | c2bdc55 | 2016-02-15 15:51:46 -0800 | [diff] [blame] | 623 | // If recognizing, stop_recognition must be called for a sound model before unload_sound_model |
Eric Laurent | cbca905 | 2014-04-18 17:54:10 -0700 | [diff] [blame] | 624 | struct stub_sound_trigger_device *stdev = (struct stub_sound_trigger_device *)dev; |
| 625 | int status = 0; |
Arunesh Mishra | f1d59fc | 2016-02-15 17:48:27 -0800 | [diff] [blame] | 626 | ALOGI("unload_sound_model."); |
Eric Laurent | cbca905 | 2014-04-18 17:54:10 -0700 | [diff] [blame] | 627 | pthread_mutex_lock(&stdev->lock); |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 628 | |
Ryan Bavetta | cc6541c | 2016-02-09 21:20:51 -0800 | [diff] [blame] | 629 | struct recognition_context *model_context = NULL; |
| 630 | struct recognition_context *previous_model_context = NULL; |
| 631 | if (stdev->root_model_context) { |
Ryan Bavetta | c2bdc55 | 2016-02-15 15:51:46 -0800 | [diff] [blame] | 632 | struct recognition_context *current_model_context = stdev->root_model_context; |
| 633 | while(current_model_context) { |
| 634 | if (current_model_context->model_handle == handle) { |
| 635 | model_context = current_model_context; |
Ryan Bavetta | cc6541c | 2016-02-09 21:20:51 -0800 | [diff] [blame] | 636 | break; |
| 637 | } |
Ryan Bavetta | c2bdc55 | 2016-02-15 15:51:46 -0800 | [diff] [blame] | 638 | previous_model_context = current_model_context; |
| 639 | current_model_context = current_model_context->next; |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 640 | } |
Eric Laurent | cbca905 | 2014-04-18 17:54:10 -0700 | [diff] [blame] | 641 | } |
Ryan Bavetta | cc6541c | 2016-02-09 21:20:51 -0800 | [diff] [blame] | 642 | if (!model_context) { |
| 643 | ALOGW("Can't find sound model handle %d in registered list", handle); |
Eric Laurent | cbca905 | 2014-04-18 17:54:10 -0700 | [diff] [blame] | 644 | pthread_mutex_unlock(&stdev->lock); |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 645 | return -ENOSYS; |
Eric Laurent | cbca905 | 2014-04-18 17:54:10 -0700 | [diff] [blame] | 646 | } |
| 647 | |
Ryan Bavetta | cc6541c | 2016-02-09 21:20:51 -0800 | [diff] [blame] | 648 | if(previous_model_context) { |
| 649 | previous_model_context->next = model_context->next; |
| 650 | } else { |
| 651 | stdev->root_model_context = model_context->next; |
| 652 | } |
| 653 | free(model_context->config); |
| 654 | free(model_context); |
Ryan Bavetta | c2bdc55 | 2016-02-15 15:51:46 -0800 | [diff] [blame] | 655 | pthread_mutex_unlock(&stdev->lock); |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 656 | |
Eric Laurent | cbca905 | 2014-04-18 17:54:10 -0700 | [diff] [blame] | 657 | return status; |
| 658 | } |
| 659 | |
| 660 | static int stdev_start_recognition(const struct sound_trigger_hw_device *dev, |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 661 | sound_model_handle_t handle, |
Eric Laurent | 30f3e6d | 2014-07-06 16:08:45 -0700 | [diff] [blame] | 662 | const struct sound_trigger_recognition_config *config, |
Eric Laurent | cbca905 | 2014-04-18 17:54:10 -0700 | [diff] [blame] | 663 | recognition_callback_t callback, |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 664 | void *cookie) { |
| 665 | ALOGI("%s", __func__); |
Eric Laurent | cbca905 | 2014-04-18 17:54:10 -0700 | [diff] [blame] | 666 | struct stub_sound_trigger_device *stdev = (struct stub_sound_trigger_device *)dev; |
Eric Laurent | cbca905 | 2014-04-18 17:54:10 -0700 | [diff] [blame] | 667 | pthread_mutex_lock(&stdev->lock); |
Ryan Bavetta | cc6541c | 2016-02-09 21:20:51 -0800 | [diff] [blame] | 668 | |
Ryan Bavetta | c2bdc55 | 2016-02-15 15:51:46 -0800 | [diff] [blame] | 669 | /* If other models running with callbacks, don't start trigger thread */ |
| 670 | bool other_callbacks_found = recognition_callback_exists(stdev); |
| 671 | |
| 672 | struct recognition_context *model_context = get_model_context(stdev, handle); |
Ryan Bavetta | cc6541c | 2016-02-09 21:20:51 -0800 | [diff] [blame] | 673 | if (!model_context) { |
| 674 | ALOGW("Can't find sound model handle %d in registered list", handle); |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 675 | pthread_mutex_unlock(&stdev->lock); |
| 676 | return -ENOSYS; |
Eric Laurent | cbca905 | 2014-04-18 17:54:10 -0700 | [diff] [blame] | 677 | } |
| 678 | |
Ryan Bavetta | cc6541c | 2016-02-09 21:20:51 -0800 | [diff] [blame] | 679 | free(model_context->config); |
| 680 | model_context->config = NULL; |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 681 | if (config) { |
Ryan Bavetta | cc6541c | 2016-02-09 21:20:51 -0800 | [diff] [blame] | 682 | model_context->config = malloc(sizeof(*config)); |
| 683 | if (!model_context->config) { |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 684 | pthread_mutex_unlock(&stdev->lock); |
| 685 | return -ENOMEM; |
| 686 | } |
Ryan Bavetta | cc6541c | 2016-02-09 21:20:51 -0800 | [diff] [blame] | 687 | memcpy(model_context->config, config, sizeof(*config)); |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 688 | } |
Ryan Bavetta | cc6541c | 2016-02-09 21:20:51 -0800 | [diff] [blame] | 689 | model_context->recognition_callback = callback; |
| 690 | model_context->recognition_cookie = cookie; |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 691 | |
Ryan Bavetta | c2bdc55 | 2016-02-15 15:51:46 -0800 | [diff] [blame] | 692 | if (!other_callbacks_found) { |
| 693 | pthread_create(&stdev->callback_thread, (const pthread_attr_t *) NULL, |
| 694 | callback_thread_loop, stdev); |
| 695 | } |
| 696 | |
Eric Laurent | cbca905 | 2014-04-18 17:54:10 -0700 | [diff] [blame] | 697 | pthread_mutex_unlock(&stdev->lock); |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 698 | return 0; |
Eric Laurent | cbca905 | 2014-04-18 17:54:10 -0700 | [diff] [blame] | 699 | } |
| 700 | |
| 701 | static int stdev_stop_recognition(const struct sound_trigger_hw_device *dev, |
Ryan Bavetta | c2bdc55 | 2016-02-15 15:51:46 -0800 | [diff] [blame] | 702 | sound_model_handle_t handle) { |
Eric Laurent | cbca905 | 2014-04-18 17:54:10 -0700 | [diff] [blame] | 703 | struct stub_sound_trigger_device *stdev = (struct stub_sound_trigger_device *)dev; |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 704 | ALOGI("%s", __func__); |
Eric Laurent | cbca905 | 2014-04-18 17:54:10 -0700 | [diff] [blame] | 705 | pthread_mutex_lock(&stdev->lock); |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 706 | |
Ryan Bavetta | c2bdc55 | 2016-02-15 15:51:46 -0800 | [diff] [blame] | 707 | struct recognition_context *model_context = get_model_context(stdev, handle); |
Ryan Bavetta | cc6541c | 2016-02-09 21:20:51 -0800 | [diff] [blame] | 708 | if (!model_context) { |
| 709 | ALOGW("Can't find sound model handle %d in registered list", handle); |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 710 | pthread_mutex_unlock(&stdev->lock); |
| 711 | return -ENOSYS; |
Eric Laurent | cbca905 | 2014-04-18 17:54:10 -0700 | [diff] [blame] | 712 | } |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 713 | |
Ryan Bavetta | cc6541c | 2016-02-09 21:20:51 -0800 | [diff] [blame] | 714 | free(model_context->config); |
| 715 | model_context->config = NULL; |
| 716 | model_context->recognition_callback = NULL; |
| 717 | model_context->recognition_cookie = NULL; |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 718 | |
Ryan Bavetta | cc6541c | 2016-02-09 21:20:51 -0800 | [diff] [blame] | 719 | /* If no more models running with callbacks, stop trigger thread */ |
Ryan Bavetta | c2bdc55 | 2016-02-15 15:51:46 -0800 | [diff] [blame] | 720 | if (!recognition_callback_exists(stdev)) { |
Ryan Bavetta | cc6541c | 2016-02-09 21:20:51 -0800 | [diff] [blame] | 721 | send_loop_kill_signal(); |
| 722 | pthread_mutex_unlock(&stdev->lock); |
| 723 | pthread_join(stdev->callback_thread, (void **)NULL); |
| 724 | } else { |
| 725 | pthread_mutex_unlock(&stdev->lock); |
| 726 | } |
| 727 | |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 728 | return 0; |
Eric Laurent | cbca905 | 2014-04-18 17:54:10 -0700 | [diff] [blame] | 729 | } |
| 730 | |
Ryan Bavetta | be0c8fd | 2016-02-01 15:17:12 -0800 | [diff] [blame] | 731 | __attribute__ ((visibility ("default"))) |
Ryan Bavetta | cc6541c | 2016-02-09 21:20:51 -0800 | [diff] [blame] | 732 | int sound_trigger_open_for_streaming() { |
Ryan Bavetta | be0c8fd | 2016-02-01 15:17:12 -0800 | [diff] [blame] | 733 | int ret = 0; |
| 734 | return ret; |
| 735 | } |
| 736 | |
| 737 | __attribute__ ((visibility ("default"))) |
Ryan Bavetta | cc6541c | 2016-02-09 21:20:51 -0800 | [diff] [blame] | 738 | 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] | 739 | size_t ret = 0; |
| 740 | return ret; |
| 741 | } |
| 742 | |
| 743 | __attribute__ ((visibility ("default"))) |
Ryan Bavetta | cc6541c | 2016-02-09 21:20:51 -0800 | [diff] [blame] | 744 | int sound_trigger_close_for_streaming(int audio_handle __unused) { |
Ryan Bavetta | be0c8fd | 2016-02-01 15:17:12 -0800 | [diff] [blame] | 745 | return 0; |
| 746 | } |
| 747 | |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 748 | static int stdev_close(hw_device_t *device) { |
Eric Laurent | cbca905 | 2014-04-18 17:54:10 -0700 | [diff] [blame] | 749 | free(device); |
| 750 | return 0; |
| 751 | } |
| 752 | |
| 753 | static int stdev_open(const hw_module_t* module, const char* name, |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 754 | hw_device_t** device) { |
Eric Laurent | cbca905 | 2014-04-18 17:54:10 -0700 | [diff] [blame] | 755 | struct stub_sound_trigger_device *stdev; |
| 756 | int ret; |
| 757 | |
| 758 | if (strcmp(name, SOUND_TRIGGER_HARDWARE_INTERFACE) != 0) |
| 759 | return -EINVAL; |
| 760 | |
| 761 | stdev = calloc(1, sizeof(struct stub_sound_trigger_device)); |
| 762 | if (!stdev) |
| 763 | return -ENOMEM; |
| 764 | |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 765 | stdev->next_sound_model_id = 1; |
Arunesh Mishra | f1d59fc | 2016-02-15 17:48:27 -0800 | [diff] [blame] | 766 | stdev->root_model_context = NULL; |
Ryan Bavetta | 4615aad | 2016-01-27 16:12:04 -0800 | [diff] [blame] | 767 | |
Eric Laurent | cbca905 | 2014-04-18 17:54:10 -0700 | [diff] [blame] | 768 | stdev->device.common.tag = HARDWARE_DEVICE_TAG; |
| 769 | stdev->device.common.version = SOUND_TRIGGER_DEVICE_API_VERSION_1_0; |
| 770 | stdev->device.common.module = (struct hw_module_t *) module; |
| 771 | stdev->device.common.close = stdev_close; |
| 772 | stdev->device.get_properties = stdev_get_properties; |
| 773 | stdev->device.load_sound_model = stdev_load_sound_model; |
| 774 | stdev->device.unload_sound_model = stdev_unload_sound_model; |
| 775 | stdev->device.start_recognition = stdev_start_recognition; |
| 776 | stdev->device.stop_recognition = stdev_stop_recognition; |
| 777 | |
| 778 | pthread_mutex_init(&stdev->lock, (const pthread_mutexattr_t *) NULL); |
Eric Laurent | cbca905 | 2014-04-18 17:54:10 -0700 | [diff] [blame] | 779 | |
| 780 | *device = &stdev->device.common; |
| 781 | |
| 782 | return 0; |
| 783 | } |
| 784 | |
| 785 | static struct hw_module_methods_t hal_module_methods = { |
| 786 | .open = stdev_open, |
| 787 | }; |
| 788 | |
| 789 | struct sound_trigger_module HAL_MODULE_INFO_SYM = { |
| 790 | .common = { |
| 791 | .tag = HARDWARE_MODULE_TAG, |
| 792 | .module_api_version = SOUND_TRIGGER_MODULE_API_VERSION_1_0, |
| 793 | .hal_api_version = HARDWARE_HAL_API_VERSION, |
| 794 | .id = SOUND_TRIGGER_HARDWARE_MODULE_ID, |
| 795 | .name = "Default sound trigger HAL", |
| 796 | .author = "The Android Open Source Project", |
| 797 | .methods = &hal_module_methods, |
| 798 | }, |
| 799 | }; |
Ryan Bavetta | cc6541c | 2016-02-09 21:20:51 -0800 | [diff] [blame] | 800 | |