blob: 6d7cd34ee1b6efbbaf56eecb6c4796bdd484c4bd [file] [log] [blame]
Eric Laurentcbca9052014-04-18 17:54:10 -07001/*
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 Bavetta4615aad2016-01-27 16:12:04 -080017
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 Bavetta4615aad2016-01-27 16:12:04 -080022 *
Arunesh Mishra7db0a7a2016-02-19 16:20:10 -080023 * telnet localhost 14035
Ryan Bavetta4615aad2016-01-27 16:12:04 -080024 *
Arunesh Mishra7db0a7a2016-02-19 16:20:10 -080025 * Commands include:
26 * ls : Lists all models that have been loaded.
27 * trig <index> : Sends a recognition event for the model at the given index.
28 * update <index> : Sends a model update event for the model at the given index.
29 * close : Closes the network connection.
Ryan Bavettac2bdc552016-02-15 15:51:46 -080030 *
Ryan Bavettabe0c8fd2016-02-01 15:17:12 -080031 * To enable this file, you can make with command line parameter
32 * SOUND_TRIGGER_USE_STUB_MODULE=1
Ryan Bavetta4615aad2016-01-27 16:12:04 -080033 */
34
Eric Laurentcbca9052014-04-18 17:54:10 -070035#define LOG_TAG "sound_trigger_hw_default"
Ryan Bavettacc6541c2016-02-09 21:20:51 -080036#define LOG_NDEBUG 1
Arunesh Mishraf1d59fc2016-02-15 17:48:27 -080037#define PARSE_BUF_LEN 1024 // Length of the parsing buffer.S
38
39// The following commands work with the network port:
40#define COMMAND_LS "ls"
41#define COMMAND_TRIGGER "trig" // Argument: model index.
Arunesh Mishra7db0a7a2016-02-19 16:20:10 -080042#define COMMAND_UPDATE "update" // Argument: model index.
Arunesh Mishraf1d59fc2016-02-15 17:48:27 -080043#define COMMAND_CLOSE "close" // Close just closes the network port, keeps thread running.
44#define COMMAND_END "end" // Closes connection and stops the thread.
45
46#define ERROR_BAD_COMMAND "Bad command"
Eric Laurentcbca9052014-04-18 17:54:10 -070047
48#include <errno.h>
Arunesh Mishraf1d59fc2016-02-15 17:48:27 -080049#include <stdarg.h>
Ryan Bavetta4615aad2016-01-27 16:12:04 -080050#include <stdio.h>
51#include <stdlib.h>
52#include <string.h>
53#include <unistd.h>
54#include <arpa/inet.h>
55#include <sys/types.h>
56#include <netinet/in.h>
57#include <sys/socket.h>
58
59#include <errno.h>
Eric Laurentcbca9052014-04-18 17:54:10 -070060#include <pthread.h>
61#include <sys/prctl.h>
62#include <cutils/log.h>
63
64#include <hardware/hardware.h>
65#include <system/sound_trigger.h>
66#include <hardware/sound_trigger.h>
67
68static const struct sound_trigger_properties hw_properties = {
69 "The Android Open Source Project", // implementor
70 "Sound Trigger stub HAL", // description
71 1, // version
72 { 0xed7a7d60, 0xc65e, 0x11e3, 0x9be4, { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } }, // uuid
Arunesh Mishraf1d59fc2016-02-15 17:48:27 -080073 4, // max_sound_models
Eric Laurentcbca9052014-04-18 17:54:10 -070074 1, // max_key_phrases
75 1, // max_users
76 RECOGNITION_MODE_VOICE_TRIGGER, // recognition_modes
77 false, // capture_transition
78 0, // max_buffer_ms
79 false, // concurrent_capture
Eric Laurent83cd8302014-07-30 08:58:39 -070080 false, // trigger_in_event
Eric Laurentcbca9052014-04-18 17:54:10 -070081 0 // power_consumption_mw
82};
83
Ryan Bavetta4615aad2016-01-27 16:12:04 -080084struct recognition_context {
Ryan Bavettacc6541c2016-02-09 21:20:51 -080085 // Sound Model information, added in method load_sound_model
86 sound_model_handle_t model_handle;
87 sound_trigger_sound_model_type_t model_type;
88 sound_model_callback_t model_callback;
89 void *model_cookie;
Ryan Bavetta4615aad2016-01-27 16:12:04 -080090
Ryan Bavettacc6541c2016-02-09 21:20:51 -080091 // Sound Model information, added in start_recognition
Ryan Bavetta4615aad2016-01-27 16:12:04 -080092 struct sound_trigger_recognition_config *config;
93 recognition_callback_t recognition_callback;
94 void *recognition_cookie;
Ryan Bavettacc6541c2016-02-09 21:20:51 -080095
96 // Next recognition_context in the linked list
97 struct recognition_context *next;
Ryan Bavettabe0c8fd2016-02-01 15:17:12 -080098};
Ryan Bavetta4615aad2016-01-27 16:12:04 -080099
Arunesh Mishraf1d59fc2016-02-15 17:48:27 -0800100char tmp_write_buffer[PARSE_BUF_LEN];
101
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800102struct stub_sound_trigger_device {
103 struct sound_trigger_hw_device device;
Eric Laurentcbca9052014-04-18 17:54:10 -0700104 pthread_mutex_t lock;
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800105 pthread_t callback_thread;
106
Ryan Bavettacc6541c2016-02-09 21:20:51 -0800107 // Recognition contexts are stored as a linked list
108 struct recognition_context *root_model_context;
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800109
110 int next_sound_model_id;
Eric Laurentcbca9052014-04-18 17:54:10 -0700111};
112
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800113/* Will reuse ids when overflow occurs */
114static unsigned int generate_sound_model_id(const struct sound_trigger_hw_device *dev) {
115 struct stub_sound_trigger_device *stdev = (struct stub_sound_trigger_device *)dev;
116 int new_id = stdev->next_sound_model_id;
117 ++stdev->next_sound_model_id;
118 if (stdev->next_sound_model_id == 0) {
119 stdev->next_sound_model_id = 1;
120 }
121 return new_id;
122}
Eric Laurentcbca9052014-04-18 17:54:10 -0700123
Arunesh Mishra7db0a7a2016-02-19 16:20:10 -0800124bool parse_socket_data(int conn_socket, struct stub_sound_trigger_device* stdev);
125
Ryan Bavettacc6541c2016-02-09 21:20:51 -0800126static char *sound_trigger_event_alloc(sound_model_handle_t handle,
127 sound_trigger_sound_model_type_t model_type,
128 struct sound_trigger_recognition_config *config) {
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800129 char *data;
Ryan Bavettacc6541c2016-02-09 21:20:51 -0800130 if (model_type == SOUND_MODEL_TYPE_KEYPHRASE) {
131 struct sound_trigger_phrase_recognition_event *event;
132 data = (char *)calloc(1, sizeof(struct sound_trigger_phrase_recognition_event));
133 if (!data)
134 return NULL;
135 event = (struct sound_trigger_phrase_recognition_event *)data;
136 event->common.status = RECOGNITION_STATUS_SUCCESS;
137 event->common.type = SOUND_MODEL_TYPE_KEYPHRASE;
138 event->common.model = handle;
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800139
Ryan Bavettacc6541c2016-02-09 21:20:51 -0800140 if (config) {
141 unsigned int i;
142
143 event->num_phrases = config->num_phrases;
144 if (event->num_phrases > SOUND_TRIGGER_MAX_PHRASES)
145 event->num_phrases = SOUND_TRIGGER_MAX_PHRASES;
146 for (i=0; i < event->num_phrases; i++)
147 memcpy(&event->phrase_extras[i],
148 &config->phrases[i],
149 sizeof(struct sound_trigger_phrase_recognition_extra));
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800150 }
Ryan Bavettacc6541c2016-02-09 21:20:51 -0800151
152 event->num_phrases = 1;
153 event->phrase_extras[0].confidence_level = 100;
154 event->phrase_extras[0].num_levels = 1;
155 event->phrase_extras[0].levels[0].level = 100;
156 event->phrase_extras[0].levels[0].user_id = 0;
157 // Signify that all the data is comming through streaming, not through the buffer.
158 event->common.capture_available = true;
159 event->common.audio_config = AUDIO_CONFIG_INITIALIZER;
160 event->common.audio_config.sample_rate = 16000;
161 event->common.audio_config.channel_mask = AUDIO_CHANNEL_IN_MONO;
162 event->common.audio_config.format = AUDIO_FORMAT_PCM_16_BIT;
163 } else if (model_type == SOUND_MODEL_TYPE_GENERIC) {
164 struct sound_trigger_generic_recognition_event *event;
165 data = (char *)calloc(1, sizeof(struct sound_trigger_generic_recognition_event));
166 if (!data)
167 return NULL;
168 event = (struct sound_trigger_generic_recognition_event *)data;
169 event->common.status = RECOGNITION_STATUS_SUCCESS;
170 event->common.type = SOUND_MODEL_TYPE_GENERIC;
171 event->common.model = handle;
172
173 // Signify that all the data is comming through streaming, not through the buffer.
174 event->common.capture_available = true;
175 event->common.audio_config = AUDIO_CONFIG_INITIALIZER;
176 event->common.audio_config.sample_rate = 16000;
177 event->common.audio_config.channel_mask = AUDIO_CHANNEL_IN_MONO;
178 event->common.audio_config.format = AUDIO_FORMAT_PCM_16_BIT;
179 } else {
180 ALOGW("No Valid Event Type Known");
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800181 return NULL;
182 }
Ryan Bavettacc6541c2016-02-09 21:20:51 -0800183 return data;
184}
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800185
Ryan Bavettacc6541c2016-02-09 21:20:51 -0800186static void send_recognition_event(sound_model_handle_t model_handle,
187 sound_trigger_sound_model_type_t model_type,
188 recognition_callback_t recognition_callback, void *recognition_cookie,
189 struct sound_trigger_recognition_config *config) {
190 if (recognition_callback == NULL) {
191 ALOGI("%s No matching callback for handle %d", __func__, model_handle);
192 return;
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800193 }
194
Ryan Bavettacc6541c2016-02-09 21:20:51 -0800195 if (model_type == SOUND_MODEL_TYPE_KEYPHRASE) {
196 struct sound_trigger_phrase_recognition_event *event;
197 event = (struct sound_trigger_phrase_recognition_event *)
198 sound_trigger_event_alloc(model_handle, model_type, config);
199 if (event) {
Ryan Bavettac2bdc552016-02-15 15:51:46 -0800200 recognition_callback(event, recognition_cookie);
Ryan Bavettacc6541c2016-02-09 21:20:51 -0800201 free(event);
202 }
203 } else if (model_type == SOUND_MODEL_TYPE_GENERIC) {
204 struct sound_trigger_generic_recognition_event *event;
205 event = (struct sound_trigger_generic_recognition_event *)
206 sound_trigger_event_alloc(model_handle, model_type, config);
207 if (event) {
Ryan Bavettac2bdc552016-02-15 15:51:46 -0800208 recognition_callback(event, recognition_cookie);
Ryan Bavettacc6541c2016-02-09 21:20:51 -0800209 free(event);
210 }
211 } else {
212 ALOGI("Unknown Sound Model Type, No Event to Send");
213 }
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800214}
215
Ryan Bavettac2bdc552016-02-15 15:51:46 -0800216static void send_model_event(sound_model_handle_t model_handle,
217 sound_model_callback_t model_callback, void *model_cookie) {
218
219 if (model_callback == NULL) {
220 ALOGI("%s No matching callback for handle %d", __func__, model_handle);
221 return;
222 }
223
224 char *data;
225 data = (char *)calloc(1, sizeof(struct sound_trigger_model_event));
226 if (!data) {
227 ALOGW("%s Could not allocate event %d", __func__, model_handle);
228 return;
229 }
230
231 struct sound_trigger_model_event *event;
232 event = (struct sound_trigger_model_event *)data;
233 event->status = SOUND_MODEL_STATUS_UPDATED;
234 event->model = model_handle;
235
236 if (event) {
237 model_callback(&event, model_cookie);
238 free(event);
239 }
240}
241
242static bool recognition_callback_exists(struct stub_sound_trigger_device *stdev) {
243 bool callback_found = false;
244 if (stdev->root_model_context) {
245 struct recognition_context *current_model_context = stdev->root_model_context;
246 while(current_model_context) {
247 if (current_model_context->recognition_callback != NULL) {
248 callback_found = true;
249 break;
250 }
251 current_model_context = current_model_context->next;
252 }
253 }
254 return callback_found;
255}
256
257static struct recognition_context * get_model_context(struct stub_sound_trigger_device *stdev,
258 sound_model_handle_t handle) {
259 struct recognition_context *model_context = NULL;
260 if (stdev->root_model_context) {
261 struct recognition_context *current_model_context = stdev->root_model_context;
262 while(current_model_context) {
263 if (current_model_context->model_handle == handle) {
264 model_context = current_model_context;
265 break;
266 }
267 current_model_context = current_model_context->next;
268 }
269 }
270 return model_context;
271}
272
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800273static void *callback_thread_loop(void *context) {
Eric Laurentcbca9052014-04-18 17:54:10 -0700274 struct stub_sound_trigger_device *stdev = (struct stub_sound_trigger_device *)context;
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800275 struct sockaddr_in incoming_info;
276 struct sockaddr_in self_info;
277 int self_socket;
278 socklen_t sock_size = sizeof(struct sockaddr_in);
279 memset(&self_info, 0, sizeof(self_info));
280 self_info.sin_family = AF_INET;
281 self_info.sin_addr.s_addr = htonl(INADDR_ANY);
282 self_info.sin_port = htons(14035);
Eric Laurentcbca9052014-04-18 17:54:10 -0700283
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800284 bool exit = false;
285 while(!exit) {
286 int received_count;
Ryan Bavettac2bdc552016-02-15 15:51:46 -0800287 int requested_count = 2;
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800288 char buffer[requested_count];
289 ALOGE("Opening socket");
290 self_socket = socket(AF_INET, SOCK_STREAM, 0);
291 if (self_socket < 0) {
292 ALOGE("Error on socket creation");
293 exit = true;
294 } else {
295 ALOGI("Socket created");
296 }
Eric Laurentcbca9052014-04-18 17:54:10 -0700297
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800298 int reuse = 1;
299 if (setsockopt(self_socket, SOL_SOCKET, SO_REUSEADDR, (const char*)&reuse, sizeof(reuse)) < 0) {
300 ALOGE("setsockopt(SO_REUSEADDR) failed");
301 }
302
303 int bind_result = bind(self_socket, (struct sockaddr *)&self_info, sizeof(struct sockaddr));
304 if (bind_result < 0) {
305 ALOGE("Error on bind");
306 exit = true;
307 }
308
309 int listen_result = listen(self_socket, 1);
310 if (listen_result < 0) {
311 ALOGE("Error on Listen");
312 exit = true;
313 }
314
315 while(!exit) {
316 int con_socket = accept(self_socket, (struct sockaddr *)&incoming_info, &sock_size);
317 if (!con_socket) {
318 ALOGE("Lost socket, cannot send trigger");
319 break;
320 }
321 ALOGI("Connection from %s", inet_ntoa(incoming_info.sin_addr));
Arunesh Mishraf1d59fc2016-02-15 17:48:27 -0800322 if (!parse_socket_data(con_socket, stdev)) {
323 ALOGI("Done processing commands over network. Stopping thread.");
324 exit = true;
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800325 }
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800326 close(con_socket);
327 }
328 ALOGE("Closing socket");
329 close(self_socket);
Eric Laurentcbca9052014-04-18 17:54:10 -0700330 }
Eric Laurentcbca9052014-04-18 17:54:10 -0700331
332 return NULL;
333}
334
Arunesh Mishraf1d59fc2016-02-15 17:48:27 -0800335void write_bad_command_error(int conn_socket, char* command) {
336 int num = snprintf(tmp_write_buffer, PARSE_BUF_LEN, "Bad command received: %s", command);
337 tmp_write_buffer[PARSE_BUF_LEN - 1] = '\0'; // Just to be sure.
338 tmp_write_buffer[PARSE_BUF_LEN - 2] = '\n';
339 write(conn_socket, tmp_write_buffer, num);
340}
341
342void write_string(int conn_socket, char* str) {
343 int num = snprintf(tmp_write_buffer, PARSE_BUF_LEN, "%s", str);
344 tmp_write_buffer[PARSE_BUF_LEN - 1] = '\0';
345 tmp_write_buffer[PARSE_BUF_LEN - 2] = '\n';
346 write(conn_socket, tmp_write_buffer, num);
347}
348
349void write_vastr(int conn_socket, char* format, ...) {
350 va_list argptr;
351 va_start(argptr, format);
352 int num = vsnprintf(tmp_write_buffer, PARSE_BUF_LEN, format, argptr);
353 va_end(argptr);
354 tmp_write_buffer[PARSE_BUF_LEN - 1] = '\0';
355 tmp_write_buffer[PARSE_BUF_LEN - 2] = '\n';
356 write(conn_socket, tmp_write_buffer, num);
357}
358
359void list_models(int conn_socket, char* buffer,
360 struct stub_sound_trigger_device* stdev) {
361 ALOGI("%s", __func__);
362 struct recognition_context *last_model_context = stdev->root_model_context;
363 int model_index = 0;
364 if (!last_model_context) {
365 ALOGI("ZERO Models exist.");
366 write_string(conn_socket, "Zero models exist.\n");
367 }
368 while (last_model_context) {
369 write_vastr(conn_socket, "Model Index: %d\n", model_index);
370 ALOGI("Model Index: %d\n", model_index);
371 write_vastr(conn_socket, "Model handle: %d\n",
372 last_model_context->model_handle);
373 ALOGI("Model handle: %d\n", last_model_context->model_handle);
374 sound_trigger_sound_model_type_t model_type = last_model_context->model_type;
375
376 if (model_type == SOUND_MODEL_TYPE_KEYPHRASE) {
377 write_string(conn_socket, "Keyphrase sound Model.\n");
378 ALOGI("Keyphrase sound Model.\n");
379 } else if (model_type == SOUND_MODEL_TYPE_GENERIC) {
380 write_string(conn_socket, "Generic sound Model.\n");
381 ALOGI("Generic sound Model.\n");
382 } else {
383 write_vastr(conn_socket, "Unknown sound model type: %d\n",
384 model_type);
385 ALOGI("Unknown sound model type: %d\n", model_type);
386 }
387 write_string(conn_socket, "----\n\n");
388 ALOGI("----\n\n");
389 last_model_context = last_model_context->next;
390 model_index++;
391 }
392}
393
394// Returns model at the given index, null otherwise (error, doesn't exist, etc).
395// Note that here index starts from zero.
396struct recognition_context* fetch_model_at_index(
397 struct stub_sound_trigger_device* stdev, int index) {
398 ALOGI("%s", __func__);
399 struct recognition_context *model_context = NULL;
400 struct recognition_context *last_model_context = stdev->root_model_context;
401 int model_index = 0;
402 while(last_model_context) {
403 if (model_index == index) {
404 model_context = last_model_context;
405 break;
406 }
407 last_model_context = last_model_context->next;
408 model_index++;
409 }
410 return model_context;
411}
412
413void send_trigger(int conn_socket, char* buffer,
414 struct stub_sound_trigger_device* stdev) {
415 ALOGI("%s", __func__);
416 char* model_handle_str = strtok(NULL, " ");
417 if (model_handle_str == NULL) {
418 write_string(conn_socket, "Bad sound model id.\n");
419 return;
420 }
421 int index = -1;
422 if (sscanf(model_handle_str, "%d", &index) <= 0) {
423 write_vastr(conn_socket, "Unable to parse sound model index: %s\n", model_handle_str);
424 return;
425 }
426
427 if (index < (int)hw_properties.max_sound_models) {
428 ALOGI("Going to send trigger for model index #%d", index );
429 struct recognition_context *model_context = fetch_model_at_index(stdev, index);
430 if (model_context) {
431 send_recognition_event(model_context->model_handle,
432 model_context->model_type,
433 model_context->recognition_callback,
434 model_context->recognition_cookie,
435 model_context->config);
436 } else {
437 ALOGI("Sound Model Does Not Exist at this Index: %d", index);
438 write_string(conn_socket, "Sound Model Does Not Exist at given Index.\n");
439 }
440 }
441}
442
443void process_send_model_event(int conn_socket, char* buffer,
444 struct stub_sound_trigger_device* stdev) {
445 ALOGI("%s", __func__);
446 char* model_handle_str = strtok(NULL, " ");
447 if (model_handle_str == NULL) {
448 write_string(conn_socket, "Bad sound model id.\n");
449 return;
450 }
451 int index = -1;
452 if (sscanf(model_handle_str, "%d", &index) <= 0) {
453 write_vastr(conn_socket, "Unable to parse sound model index: %s\n", model_handle_str);
454 return;
455 }
456
457 if (index < (int)hw_properties.max_sound_models) {
458 ALOGI("Going to model event for model index #%d", index );
459 struct recognition_context *model_context = fetch_model_at_index(stdev, index);
460 if (model_context) {
461
462 send_model_event(model_context->model_handle,
463 model_context->model_callback,
464 model_context->model_cookie);
465 } else {
466 ALOGI("Sound Model Does Not Exist at this Index: %d", index);
467 write_string(conn_socket, "Sound Model Does Not Exist at given Index.\n");
468 }
469 }
470}
471
472// Gets the next word from buffer, replaces '\n' or ' ' with '\0'.
473char* get_command(char* buffer) {
474 char* command = strtok(buffer, " ");
475 char* newline = strchr(command, '\n');
476 if (newline != NULL) {
477 *newline = '\0';
478 }
479 return command;
480}
481
482// Parses data coming in from the local socket, executes commands. Returns when
483// done. Return code indicates whether the server should continue listening or
484// abort (true if continue listening).
485bool parse_socket_data(int conn_socket, struct stub_sound_trigger_device* stdev) {
486 ALOGI("Calling parse_socket_data");
487 bool input_done = false;
488 char buffer[PARSE_BUF_LEN];
489 FILE* input_fp = fdopen(conn_socket, "r");
Arunesh Mishraf1d59fc2016-02-15 17:48:27 -0800490 bool continue_listening = true;
491 while(!input_done) {
492 if (fgets(buffer, PARSE_BUF_LEN, input_fp) != NULL) {
Arunesh Mishra7db0a7a2016-02-19 16:20:10 -0800493 pthread_mutex_lock(&stdev->lock);
Arunesh Mishraf1d59fc2016-02-15 17:48:27 -0800494 char* command = strtok(buffer, " \n");
495 if (command == NULL) {
496 write_bad_command_error(conn_socket, command);
497 } else if (strncmp(command, COMMAND_LS, 2) == 0) {
498 list_models(conn_socket, buffer, stdev);
499 } else if (strcmp(command, COMMAND_TRIGGER) == 0) {
500 send_trigger(conn_socket, buffer, stdev);
Arunesh Mishra7db0a7a2016-02-19 16:20:10 -0800501 } else if (strcmp(command, COMMAND_UPDATE) == 0) {
502 process_send_model_event(conn_socket, buffer, stdev);
Arunesh Mishraf1d59fc2016-02-15 17:48:27 -0800503 } else if (strncmp(command, COMMAND_CLOSE, 5) == 0) {
504 ALOGI("Closing this connection.");
505 write_string(conn_socket, "Closing this connection.");
506 break;
507 } else if (strncmp(command, COMMAND_END, 3) == 0) {
508 ALOGI("End command received.");
509 write_string(conn_socket, "End command received. Stopping connection.");
510 continue_listening = false;
511 break;
Arunesh Mishra7db0a7a2016-02-19 16:20:10 -0800512 } else {
513 write_vastr(conn_socket, "Bad command %s.\n", command);
Arunesh Mishraf1d59fc2016-02-15 17:48:27 -0800514 }
Arunesh Mishra7db0a7a2016-02-19 16:20:10 -0800515 pthread_mutex_unlock(&stdev->lock);
Arunesh Mishraf1d59fc2016-02-15 17:48:27 -0800516 } else {
517 ALOGI("parse_socket_data done (got null)");
518 input_done = true; // break.
519 }
520 }
Arunesh Mishraf1d59fc2016-02-15 17:48:27 -0800521 return continue_listening;
522}
523
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800524static void send_loop_kill_signal() {
525 ALOGI("Sending loop thread kill signal");
526 int self_socket = socket(AF_INET, SOCK_STREAM, 0);
527 struct sockaddr_in remote_info;
528 memset(&remote_info, 0, sizeof(remote_info));
529 remote_info.sin_family = AF_INET;
530 remote_info.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
531 remote_info.sin_port = htons(14035);
532 if (connect(self_socket, (struct sockaddr *)&remote_info, sizeof(struct sockaddr)) == 0) {
Arunesh Mishraf1d59fc2016-02-15 17:48:27 -0800533 send(self_socket, COMMAND_END, 1, 0);
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800534 } else {
535 ALOGI("Could not connect");
536 }
537 close(self_socket);
538 ALOGI("Sent loop thread kill signal");
539}
540
Eric Laurentcbca9052014-04-18 17:54:10 -0700541static int stdev_get_properties(const struct sound_trigger_hw_device *dev,
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800542 struct sound_trigger_properties *properties) {
Eric Laurentcbca9052014-04-18 17:54:10 -0700543 struct stub_sound_trigger_device *stdev = (struct stub_sound_trigger_device *)dev;
544
545 ALOGI("%s", __func__);
546 if (properties == NULL)
547 return -EINVAL;
548 memcpy(properties, &hw_properties, sizeof(struct sound_trigger_properties));
549 return 0;
550}
551
552static int stdev_load_sound_model(const struct sound_trigger_hw_device *dev,
553 struct sound_trigger_sound_model *sound_model,
554 sound_model_callback_t callback,
555 void *cookie,
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800556 sound_model_handle_t *handle) {
Arunesh Mishraf1d59fc2016-02-15 17:48:27 -0800557 ALOGI("load_sound_model.");
Eric Laurentcbca9052014-04-18 17:54:10 -0700558 struct stub_sound_trigger_device *stdev = (struct stub_sound_trigger_device *)dev;
559 int status = 0;
560
561 ALOGI("%s stdev %p", __func__, stdev);
562 pthread_mutex_lock(&stdev->lock);
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800563
Eric Laurentcbca9052014-04-18 17:54:10 -0700564 if (handle == NULL || sound_model == NULL) {
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800565 pthread_mutex_unlock(&stdev->lock);
566 return -EINVAL;
Eric Laurentcbca9052014-04-18 17:54:10 -0700567 }
568 if (sound_model->data_size == 0 ||
569 sound_model->data_offset < sizeof(struct sound_trigger_sound_model)) {
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800570 pthread_mutex_unlock(&stdev->lock);
571 return -EINVAL;
Eric Laurentcbca9052014-04-18 17:54:10 -0700572 }
573
Ryan Bavettacc6541c2016-02-09 21:20:51 -0800574 struct recognition_context *model_context;
575 model_context = malloc(sizeof(struct recognition_context));
576 if(!model_context) {
577 ALOGW("Could not allocate recognition_context");
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800578 pthread_mutex_unlock(&stdev->lock);
579 return -ENOSYS;
580 }
581
Ryan Bavettacc6541c2016-02-09 21:20:51 -0800582 // Add the new model context to the recognition_context linked list
583 if (stdev->root_model_context) {
584 // Find the tail
Ryan Bavettac2bdc552016-02-15 15:51:46 -0800585 struct recognition_context *current_model_context = stdev->root_model_context;
Ryan Bavettacc6541c2016-02-09 21:20:51 -0800586 int model_count = 0;
Ryan Bavettac2bdc552016-02-15 15:51:46 -0800587 while(current_model_context->next) {
588 current_model_context = current_model_context->next;
Ryan Bavettacc6541c2016-02-09 21:20:51 -0800589 model_count++;
590 if (model_count >= hw_properties.max_sound_models) {
591 ALOGW("Can't load model: reached max sound model limit");
592 free(model_context);
593 pthread_mutex_unlock(&stdev->lock);
594 return -ENOSYS;
595 }
596 }
Ryan Bavettac2bdc552016-02-15 15:51:46 -0800597 current_model_context->next = model_context;
Ryan Bavettacc6541c2016-02-09 21:20:51 -0800598 } else {
599 stdev->root_model_context = model_context;
600 }
601
602 model_context->model_handle = generate_sound_model_id(dev);
603 *handle = model_context->model_handle;
604 model_context->model_type = sound_model->type;
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800605
Eric Laurentcbca9052014-04-18 17:54:10 -0700606 char *data = (char *)sound_model + sound_model->data_offset;
607 ALOGI("%s data size %d data %d - %d", __func__,
608 sound_model->data_size, data[0], data[sound_model->data_size - 1]);
Ryan Bavettacc6541c2016-02-09 21:20:51 -0800609 model_context->model_callback = callback;
610 model_context->model_cookie = cookie;
611 model_context->config = NULL;
612 model_context->recognition_callback = NULL;
613 model_context->recognition_cookie = NULL;
Arunesh Mishraf1d59fc2016-02-15 17:48:27 -0800614 ALOGI("Sound model loaded: Handle %d ", *handle);
Eric Laurentcbca9052014-04-18 17:54:10 -0700615
Eric Laurentcbca9052014-04-18 17:54:10 -0700616 pthread_mutex_unlock(&stdev->lock);
617 return status;
618}
619
620static int stdev_unload_sound_model(const struct sound_trigger_hw_device *dev,
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800621 sound_model_handle_t handle) {
Ryan Bavettac2bdc552016-02-15 15:51:46 -0800622 // If recognizing, stop_recognition must be called for a sound model before unload_sound_model
Eric Laurentcbca9052014-04-18 17:54:10 -0700623 struct stub_sound_trigger_device *stdev = (struct stub_sound_trigger_device *)dev;
624 int status = 0;
Arunesh Mishraf1d59fc2016-02-15 17:48:27 -0800625 ALOGI("unload_sound_model.");
Eric Laurentcbca9052014-04-18 17:54:10 -0700626 pthread_mutex_lock(&stdev->lock);
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800627
Ryan Bavettacc6541c2016-02-09 21:20:51 -0800628 struct recognition_context *model_context = NULL;
629 struct recognition_context *previous_model_context = NULL;
630 if (stdev->root_model_context) {
Ryan Bavettac2bdc552016-02-15 15:51:46 -0800631 struct recognition_context *current_model_context = stdev->root_model_context;
632 while(current_model_context) {
633 if (current_model_context->model_handle == handle) {
634 model_context = current_model_context;
Ryan Bavettacc6541c2016-02-09 21:20:51 -0800635 break;
636 }
Ryan Bavettac2bdc552016-02-15 15:51:46 -0800637 previous_model_context = current_model_context;
638 current_model_context = current_model_context->next;
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800639 }
Eric Laurentcbca9052014-04-18 17:54:10 -0700640 }
Ryan Bavettacc6541c2016-02-09 21:20:51 -0800641 if (!model_context) {
642 ALOGW("Can't find sound model handle %d in registered list", handle);
Eric Laurentcbca9052014-04-18 17:54:10 -0700643 pthread_mutex_unlock(&stdev->lock);
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800644 return -ENOSYS;
Eric Laurentcbca9052014-04-18 17:54:10 -0700645 }
646
Ryan Bavettacc6541c2016-02-09 21:20:51 -0800647 if(previous_model_context) {
648 previous_model_context->next = model_context->next;
649 } else {
650 stdev->root_model_context = model_context->next;
651 }
652 free(model_context->config);
653 free(model_context);
Ryan Bavettac2bdc552016-02-15 15:51:46 -0800654 pthread_mutex_unlock(&stdev->lock);
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800655
Eric Laurentcbca9052014-04-18 17:54:10 -0700656 return status;
657}
658
659static int stdev_start_recognition(const struct sound_trigger_hw_device *dev,
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800660 sound_model_handle_t handle,
Eric Laurent30f3e6d2014-07-06 16:08:45 -0700661 const struct sound_trigger_recognition_config *config,
Eric Laurentcbca9052014-04-18 17:54:10 -0700662 recognition_callback_t callback,
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800663 void *cookie) {
664 ALOGI("%s", __func__);
Eric Laurentcbca9052014-04-18 17:54:10 -0700665 struct stub_sound_trigger_device *stdev = (struct stub_sound_trigger_device *)dev;
Eric Laurentcbca9052014-04-18 17:54:10 -0700666 pthread_mutex_lock(&stdev->lock);
Ryan Bavettacc6541c2016-02-09 21:20:51 -0800667
Ryan Bavettac2bdc552016-02-15 15:51:46 -0800668 /* If other models running with callbacks, don't start trigger thread */
669 bool other_callbacks_found = recognition_callback_exists(stdev);
670
671 struct recognition_context *model_context = get_model_context(stdev, handle);
Ryan Bavettacc6541c2016-02-09 21:20:51 -0800672 if (!model_context) {
673 ALOGW("Can't find sound model handle %d in registered list", handle);
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800674 pthread_mutex_unlock(&stdev->lock);
675 return -ENOSYS;
Eric Laurentcbca9052014-04-18 17:54:10 -0700676 }
677
Ryan Bavettacc6541c2016-02-09 21:20:51 -0800678 free(model_context->config);
679 model_context->config = NULL;
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800680 if (config) {
Ryan Bavettacc6541c2016-02-09 21:20:51 -0800681 model_context->config = malloc(sizeof(*config));
682 if (!model_context->config) {
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800683 pthread_mutex_unlock(&stdev->lock);
684 return -ENOMEM;
685 }
Ryan Bavettacc6541c2016-02-09 21:20:51 -0800686 memcpy(model_context->config, config, sizeof(*config));
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800687 }
Ryan Bavettacc6541c2016-02-09 21:20:51 -0800688 model_context->recognition_callback = callback;
689 model_context->recognition_cookie = cookie;
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800690
Ryan Bavettac2bdc552016-02-15 15:51:46 -0800691 if (!other_callbacks_found) {
692 pthread_create(&stdev->callback_thread, (const pthread_attr_t *) NULL,
693 callback_thread_loop, stdev);
694 }
695
Eric Laurentcbca9052014-04-18 17:54:10 -0700696 pthread_mutex_unlock(&stdev->lock);
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800697 return 0;
Eric Laurentcbca9052014-04-18 17:54:10 -0700698}
699
700static int stdev_stop_recognition(const struct sound_trigger_hw_device *dev,
Ryan Bavettac2bdc552016-02-15 15:51:46 -0800701 sound_model_handle_t handle) {
Eric Laurentcbca9052014-04-18 17:54:10 -0700702 struct stub_sound_trigger_device *stdev = (struct stub_sound_trigger_device *)dev;
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800703 ALOGI("%s", __func__);
Eric Laurentcbca9052014-04-18 17:54:10 -0700704 pthread_mutex_lock(&stdev->lock);
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800705
Ryan Bavettac2bdc552016-02-15 15:51:46 -0800706 struct recognition_context *model_context = get_model_context(stdev, handle);
Ryan Bavettacc6541c2016-02-09 21:20:51 -0800707 if (!model_context) {
708 ALOGW("Can't find sound model handle %d in registered list", handle);
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800709 pthread_mutex_unlock(&stdev->lock);
710 return -ENOSYS;
Eric Laurentcbca9052014-04-18 17:54:10 -0700711 }
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800712
Ryan Bavettacc6541c2016-02-09 21:20:51 -0800713 free(model_context->config);
714 model_context->config = NULL;
715 model_context->recognition_callback = NULL;
716 model_context->recognition_cookie = NULL;
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800717
Ryan Bavettacc6541c2016-02-09 21:20:51 -0800718 /* If no more models running with callbacks, stop trigger thread */
Ryan Bavettac2bdc552016-02-15 15:51:46 -0800719 if (!recognition_callback_exists(stdev)) {
Ryan Bavettacc6541c2016-02-09 21:20:51 -0800720 send_loop_kill_signal();
721 pthread_mutex_unlock(&stdev->lock);
722 pthread_join(stdev->callback_thread, (void **)NULL);
723 } else {
724 pthread_mutex_unlock(&stdev->lock);
725 }
726
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800727 return 0;
Eric Laurentcbca9052014-04-18 17:54:10 -0700728}
729
Ryan Bavettabe0c8fd2016-02-01 15:17:12 -0800730__attribute__ ((visibility ("default")))
Ryan Bavettacc6541c2016-02-09 21:20:51 -0800731int sound_trigger_open_for_streaming() {
Ryan Bavettabe0c8fd2016-02-01 15:17:12 -0800732 int ret = 0;
733 return ret;
734}
735
736__attribute__ ((visibility ("default")))
Ryan Bavettacc6541c2016-02-09 21:20:51 -0800737size_t sound_trigger_read_samples(int audio_handle, void *buffer, size_t buffer_len) {
Ryan Bavettabe0c8fd2016-02-01 15:17:12 -0800738 size_t ret = 0;
739 return ret;
740}
741
742__attribute__ ((visibility ("default")))
Ryan Bavettacc6541c2016-02-09 21:20:51 -0800743int sound_trigger_close_for_streaming(int audio_handle __unused) {
Ryan Bavettabe0c8fd2016-02-01 15:17:12 -0800744 return 0;
745}
746
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800747static int stdev_close(hw_device_t *device) {
Eric Laurentcbca9052014-04-18 17:54:10 -0700748 free(device);
749 return 0;
750}
751
752static int stdev_open(const hw_module_t* module, const char* name,
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800753 hw_device_t** device) {
Eric Laurentcbca9052014-04-18 17:54:10 -0700754 struct stub_sound_trigger_device *stdev;
755 int ret;
756
757 if (strcmp(name, SOUND_TRIGGER_HARDWARE_INTERFACE) != 0)
758 return -EINVAL;
759
760 stdev = calloc(1, sizeof(struct stub_sound_trigger_device));
761 if (!stdev)
762 return -ENOMEM;
763
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800764 stdev->next_sound_model_id = 1;
Arunesh Mishraf1d59fc2016-02-15 17:48:27 -0800765 stdev->root_model_context = NULL;
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800766
Eric Laurentcbca9052014-04-18 17:54:10 -0700767 stdev->device.common.tag = HARDWARE_DEVICE_TAG;
768 stdev->device.common.version = SOUND_TRIGGER_DEVICE_API_VERSION_1_0;
769 stdev->device.common.module = (struct hw_module_t *) module;
770 stdev->device.common.close = stdev_close;
771 stdev->device.get_properties = stdev_get_properties;
772 stdev->device.load_sound_model = stdev_load_sound_model;
773 stdev->device.unload_sound_model = stdev_unload_sound_model;
774 stdev->device.start_recognition = stdev_start_recognition;
775 stdev->device.stop_recognition = stdev_stop_recognition;
776
777 pthread_mutex_init(&stdev->lock, (const pthread_mutexattr_t *) NULL);
Eric Laurentcbca9052014-04-18 17:54:10 -0700778
779 *device = &stdev->device.common;
780
781 return 0;
782}
783
784static struct hw_module_methods_t hal_module_methods = {
785 .open = stdev_open,
786};
787
788struct sound_trigger_module HAL_MODULE_INFO_SYM = {
789 .common = {
790 .tag = HARDWARE_MODULE_TAG,
791 .module_api_version = SOUND_TRIGGER_MODULE_API_VERSION_1_0,
792 .hal_api_version = HARDWARE_HAL_API_VERSION,
793 .id = SOUND_TRIGGER_HARDWARE_MODULE_ID,
794 .name = "Default sound trigger HAL",
795 .author = "The Android Open Source Project",
796 .methods = &hal_module_methods,
797 },
798};
Ryan Bavettacc6541c2016-02-09 21:20:51 -0800799