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