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