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