blob: 8aad1aa930aa1bace989b8e7818879fb3ed40e9a [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
22 * echo $'\001' | nc -q -1 localhost 14035
23 *
24 * $'\001' corresponds to the index_position of loaded sound
25 * model, you can also send $'\002' etc. $'\000' is the kill
26 * signal for the trigger listening thread.
27 *
Ryan Bavettac2bdc552016-02-15 15:51:46 -080028 * The default event type is a Recognition event, to specify a
29 * type of event, you can send another byte. $'\000' corresponds
30 * to a recognition event, $'\001' is a sound model event.
31 *
32 * adb forward tcp:14035 tcp:14035
33 * echo $'\001'$'\001' | nc -q -1 localhost 14035
34 *
Ryan Bavettabe0c8fd2016-02-01 15:17:12 -080035 * To enable this file, you can make with command line parameter
36 * SOUND_TRIGGER_USE_STUB_MODULE=1
Ryan Bavetta4615aad2016-01-27 16:12:04 -080037 */
38
Eric Laurentcbca9052014-04-18 17:54:10 -070039#define LOG_TAG "sound_trigger_hw_default"
Ryan Bavettacc6541c2016-02-09 21:20:51 -080040#define LOG_NDEBUG 1
Arunesh Mishraf1d59fc2016-02-15 17:48:27 -080041#define PARSE_BUF_LEN 1024 // Length of the parsing buffer.S
42
43// The following commands work with the network port:
44#define COMMAND_LS "ls"
45#define COMMAND_TRIGGER "trig" // Argument: model index.
46#define COMMAND_MODEL_EVENT "model_event" // Argument: model index.
47#define COMMAND_CLOSE "close" // Close just closes the network port, keeps thread running.
48#define COMMAND_END "end" // Closes connection and stops the thread.
49
50#define ERROR_BAD_COMMAND "Bad command"
Eric Laurentcbca9052014-04-18 17:54:10 -070051
52#include <errno.h>
Arunesh Mishraf1d59fc2016-02-15 17:48:27 -080053#include <stdarg.h>
Ryan Bavetta4615aad2016-01-27 16:12:04 -080054#include <stdio.h>
55#include <stdlib.h>
56#include <string.h>
57#include <unistd.h>
58#include <arpa/inet.h>
59#include <sys/types.h>
60#include <netinet/in.h>
61#include <sys/socket.h>
62
63#include <errno.h>
Eric Laurentcbca9052014-04-18 17:54:10 -070064#include <pthread.h>
65#include <sys/prctl.h>
66#include <cutils/log.h>
67
68#include <hardware/hardware.h>
69#include <system/sound_trigger.h>
70#include <hardware/sound_trigger.h>
71
72static const struct sound_trigger_properties hw_properties = {
73 "The Android Open Source Project", // implementor
74 "Sound Trigger stub HAL", // description
75 1, // version
76 { 0xed7a7d60, 0xc65e, 0x11e3, 0x9be4, { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } }, // uuid
Arunesh Mishraf1d59fc2016-02-15 17:48:27 -080077 4, // max_sound_models
Eric Laurentcbca9052014-04-18 17:54:10 -070078 1, // max_key_phrases
79 1, // max_users
80 RECOGNITION_MODE_VOICE_TRIGGER, // recognition_modes
81 false, // capture_transition
82 0, // max_buffer_ms
83 false, // concurrent_capture
Eric Laurent83cd8302014-07-30 08:58:39 -070084 false, // trigger_in_event
Eric Laurentcbca9052014-04-18 17:54:10 -070085 0 // power_consumption_mw
86};
87
Ryan Bavetta4615aad2016-01-27 16:12:04 -080088struct recognition_context {
Ryan Bavettacc6541c2016-02-09 21:20:51 -080089 // Sound Model information, added in method load_sound_model
90 sound_model_handle_t model_handle;
91 sound_trigger_sound_model_type_t model_type;
92 sound_model_callback_t model_callback;
93 void *model_cookie;
Ryan Bavetta4615aad2016-01-27 16:12:04 -080094
Ryan Bavettacc6541c2016-02-09 21:20:51 -080095 // Sound Model information, added in start_recognition
Ryan Bavetta4615aad2016-01-27 16:12:04 -080096 struct sound_trigger_recognition_config *config;
97 recognition_callback_t recognition_callback;
98 void *recognition_cookie;
Ryan Bavettacc6541c2016-02-09 21:20:51 -080099
100 // Next recognition_context in the linked list
101 struct recognition_context *next;
Ryan Bavettabe0c8fd2016-02-01 15:17:12 -0800102};
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800103
Arunesh Mishraf1d59fc2016-02-15 17:48:27 -0800104char tmp_write_buffer[PARSE_BUF_LEN];
105
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800106struct stub_sound_trigger_device {
107 struct sound_trigger_hw_device device;
Eric Laurentcbca9052014-04-18 17:54:10 -0700108 pthread_mutex_t lock;
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800109 pthread_t callback_thread;
110
Ryan Bavettacc6541c2016-02-09 21:20:51 -0800111 // Recognition contexts are stored as a linked list
112 struct recognition_context *root_model_context;
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800113
114 int next_sound_model_id;
Eric Laurentcbca9052014-04-18 17:54:10 -0700115};
116
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800117/* Will reuse ids when overflow occurs */
118static unsigned int generate_sound_model_id(const struct sound_trigger_hw_device *dev) {
119 struct stub_sound_trigger_device *stdev = (struct stub_sound_trigger_device *)dev;
120 int new_id = stdev->next_sound_model_id;
121 ++stdev->next_sound_model_id;
122 if (stdev->next_sound_model_id == 0) {
123 stdev->next_sound_model_id = 1;
124 }
125 return new_id;
126}
Eric Laurentcbca9052014-04-18 17:54:10 -0700127
Ryan Bavettacc6541c2016-02-09 21:20:51 -0800128static char *sound_trigger_event_alloc(sound_model_handle_t handle,
129 sound_trigger_sound_model_type_t model_type,
130 struct sound_trigger_recognition_config *config) {
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800131 char *data;
Ryan Bavettacc6541c2016-02-09 21:20:51 -0800132 if (model_type == SOUND_MODEL_TYPE_KEYPHRASE) {
133 struct sound_trigger_phrase_recognition_event *event;
134 data = (char *)calloc(1, sizeof(struct sound_trigger_phrase_recognition_event));
135 if (!data)
136 return NULL;
137 event = (struct sound_trigger_phrase_recognition_event *)data;
138 event->common.status = RECOGNITION_STATUS_SUCCESS;
139 event->common.type = SOUND_MODEL_TYPE_KEYPHRASE;
140 event->common.model = handle;
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800141
Ryan Bavettacc6541c2016-02-09 21:20:51 -0800142 if (config) {
143 unsigned int i;
144
145 event->num_phrases = config->num_phrases;
146 if (event->num_phrases > SOUND_TRIGGER_MAX_PHRASES)
147 event->num_phrases = SOUND_TRIGGER_MAX_PHRASES;
148 for (i=0; i < event->num_phrases; i++)
149 memcpy(&event->phrase_extras[i],
150 &config->phrases[i],
151 sizeof(struct sound_trigger_phrase_recognition_extra));
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800152 }
Ryan Bavettacc6541c2016-02-09 21:20:51 -0800153
154 event->num_phrases = 1;
155 event->phrase_extras[0].confidence_level = 100;
156 event->phrase_extras[0].num_levels = 1;
157 event->phrase_extras[0].levels[0].level = 100;
158 event->phrase_extras[0].levels[0].user_id = 0;
159 // Signify that all the data is comming through streaming, not through the buffer.
160 event->common.capture_available = true;
161 event->common.audio_config = AUDIO_CONFIG_INITIALIZER;
162 event->common.audio_config.sample_rate = 16000;
163 event->common.audio_config.channel_mask = AUDIO_CHANNEL_IN_MONO;
164 event->common.audio_config.format = AUDIO_FORMAT_PCM_16_BIT;
165 } else if (model_type == SOUND_MODEL_TYPE_GENERIC) {
166 struct sound_trigger_generic_recognition_event *event;
167 data = (char *)calloc(1, sizeof(struct sound_trigger_generic_recognition_event));
168 if (!data)
169 return NULL;
170 event = (struct sound_trigger_generic_recognition_event *)data;
171 event->common.status = RECOGNITION_STATUS_SUCCESS;
172 event->common.type = SOUND_MODEL_TYPE_GENERIC;
173 event->common.model = handle;
174
175 // Signify that all the data is comming through streaming, not through the buffer.
176 event->common.capture_available = true;
177 event->common.audio_config = AUDIO_CONFIG_INITIALIZER;
178 event->common.audio_config.sample_rate = 16000;
179 event->common.audio_config.channel_mask = AUDIO_CHANNEL_IN_MONO;
180 event->common.audio_config.format = AUDIO_FORMAT_PCM_16_BIT;
181 } else {
182 ALOGW("No Valid Event Type Known");
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800183 return NULL;
184 }
Ryan Bavettacc6541c2016-02-09 21:20:51 -0800185 return data;
186}
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800187
Ryan Bavettacc6541c2016-02-09 21:20:51 -0800188static void send_recognition_event(sound_model_handle_t model_handle,
189 sound_trigger_sound_model_type_t model_type,
190 recognition_callback_t recognition_callback, void *recognition_cookie,
191 struct sound_trigger_recognition_config *config) {
192 if (recognition_callback == NULL) {
193 ALOGI("%s No matching callback for handle %d", __func__, model_handle);
194 return;
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800195 }
196
Ryan Bavettacc6541c2016-02-09 21:20:51 -0800197 if (model_type == SOUND_MODEL_TYPE_KEYPHRASE) {
198 struct sound_trigger_phrase_recognition_event *event;
199 event = (struct sound_trigger_phrase_recognition_event *)
200 sound_trigger_event_alloc(model_handle, model_type, config);
201 if (event) {
Ryan Bavettac2bdc552016-02-15 15:51:46 -0800202 recognition_callback(event, recognition_cookie);
Ryan Bavettacc6541c2016-02-09 21:20:51 -0800203 free(event);
204 }
205 } else if (model_type == SOUND_MODEL_TYPE_GENERIC) {
206 struct sound_trigger_generic_recognition_event *event;
207 event = (struct sound_trigger_generic_recognition_event *)
208 sound_trigger_event_alloc(model_handle, model_type, config);
209 if (event) {
Ryan Bavettac2bdc552016-02-15 15:51:46 -0800210 recognition_callback(event, recognition_cookie);
Ryan Bavettacc6541c2016-02-09 21:20:51 -0800211 free(event);
212 }
213 } else {
214 ALOGI("Unknown Sound Model Type, No Event to Send");
215 }
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800216}
217
Ryan Bavettac2bdc552016-02-15 15:51:46 -0800218static void send_model_event(sound_model_handle_t model_handle,
219 sound_model_callback_t model_callback, void *model_cookie) {
220
221 if (model_callback == NULL) {
222 ALOGI("%s No matching callback for handle %d", __func__, model_handle);
223 return;
224 }
225
226 char *data;
227 data = (char *)calloc(1, sizeof(struct sound_trigger_model_event));
228 if (!data) {
229 ALOGW("%s Could not allocate event %d", __func__, model_handle);
230 return;
231 }
232
233 struct sound_trigger_model_event *event;
234 event = (struct sound_trigger_model_event *)data;
235 event->status = SOUND_MODEL_STATUS_UPDATED;
236 event->model = model_handle;
237
238 if (event) {
239 model_callback(&event, model_cookie);
240 free(event);
241 }
242}
243
244static bool recognition_callback_exists(struct stub_sound_trigger_device *stdev) {
245 bool callback_found = false;
246 if (stdev->root_model_context) {
247 struct recognition_context *current_model_context = stdev->root_model_context;
248 while(current_model_context) {
249 if (current_model_context->recognition_callback != NULL) {
250 callback_found = true;
251 break;
252 }
253 current_model_context = current_model_context->next;
254 }
255 }
256 return callback_found;
257}
258
259static struct recognition_context * get_model_context(struct stub_sound_trigger_device *stdev,
260 sound_model_handle_t handle) {
261 struct recognition_context *model_context = NULL;
262 if (stdev->root_model_context) {
263 struct recognition_context *current_model_context = stdev->root_model_context;
264 while(current_model_context) {
265 if (current_model_context->model_handle == handle) {
266 model_context = current_model_context;
267 break;
268 }
269 current_model_context = current_model_context->next;
270 }
271 }
272 return model_context;
273}
274
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800275static void *callback_thread_loop(void *context) {
Eric Laurentcbca9052014-04-18 17:54:10 -0700276 struct stub_sound_trigger_device *stdev = (struct stub_sound_trigger_device *)context;
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800277 struct sockaddr_in incoming_info;
278 struct sockaddr_in self_info;
279 int self_socket;
280 socklen_t sock_size = sizeof(struct sockaddr_in);
281 memset(&self_info, 0, sizeof(self_info));
282 self_info.sin_family = AF_INET;
283 self_info.sin_addr.s_addr = htonl(INADDR_ANY);
284 self_info.sin_port = htons(14035);
Eric Laurentcbca9052014-04-18 17:54:10 -0700285
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800286 bool exit = false;
287 while(!exit) {
288 int received_count;
Ryan Bavettac2bdc552016-02-15 15:51:46 -0800289 int requested_count = 2;
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800290 char buffer[requested_count];
291 ALOGE("Opening socket");
292 self_socket = socket(AF_INET, SOCK_STREAM, 0);
293 if (self_socket < 0) {
294 ALOGE("Error on socket creation");
295 exit = true;
296 } else {
297 ALOGI("Socket created");
298 }
Eric Laurentcbca9052014-04-18 17:54:10 -0700299
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800300 int reuse = 1;
301 if (setsockopt(self_socket, SOL_SOCKET, SO_REUSEADDR, (const char*)&reuse, sizeof(reuse)) < 0) {
302 ALOGE("setsockopt(SO_REUSEADDR) failed");
303 }
304
305 int bind_result = bind(self_socket, (struct sockaddr *)&self_info, sizeof(struct sockaddr));
306 if (bind_result < 0) {
307 ALOGE("Error on bind");
308 exit = true;
309 }
310
311 int listen_result = listen(self_socket, 1);
312 if (listen_result < 0) {
313 ALOGE("Error on Listen");
314 exit = true;
315 }
316
317 while(!exit) {
318 int con_socket = accept(self_socket, (struct sockaddr *)&incoming_info, &sock_size);
319 if (!con_socket) {
320 ALOGE("Lost socket, cannot send trigger");
321 break;
322 }
323 ALOGI("Connection from %s", inet_ntoa(incoming_info.sin_addr));
Arunesh Mishraf1d59fc2016-02-15 17:48:27 -0800324 if (!parse_socket_data(con_socket, stdev)) {
325 ALOGI("Done processing commands over network. Stopping thread.");
326 exit = true;
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800327 }
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800328 close(con_socket);
329 }
330 ALOGE("Closing socket");
331 close(self_socket);
Eric Laurentcbca9052014-04-18 17:54:10 -0700332 }
Eric Laurentcbca9052014-04-18 17:54:10 -0700333
334 return NULL;
335}
336
Arunesh Mishraf1d59fc2016-02-15 17:48:27 -0800337void write_bad_command_error(int conn_socket, char* command) {
338 int num = snprintf(tmp_write_buffer, PARSE_BUF_LEN, "Bad command received: %s", command);
339 tmp_write_buffer[PARSE_BUF_LEN - 1] = '\0'; // Just to be sure.
340 tmp_write_buffer[PARSE_BUF_LEN - 2] = '\n';
341 write(conn_socket, tmp_write_buffer, num);
342}
343
344void write_string(int conn_socket, char* str) {
345 int num = snprintf(tmp_write_buffer, PARSE_BUF_LEN, "%s", str);
346 tmp_write_buffer[PARSE_BUF_LEN - 1] = '\0';
347 tmp_write_buffer[PARSE_BUF_LEN - 2] = '\n';
348 write(conn_socket, tmp_write_buffer, num);
349}
350
351void write_vastr(int conn_socket, char* format, ...) {
352 va_list argptr;
353 va_start(argptr, format);
354 int num = vsnprintf(tmp_write_buffer, PARSE_BUF_LEN, format, argptr);
355 va_end(argptr);
356 tmp_write_buffer[PARSE_BUF_LEN - 1] = '\0';
357 tmp_write_buffer[PARSE_BUF_LEN - 2] = '\n';
358 write(conn_socket, tmp_write_buffer, num);
359}
360
361void list_models(int conn_socket, char* buffer,
362 struct stub_sound_trigger_device* stdev) {
363 ALOGI("%s", __func__);
364 struct recognition_context *last_model_context = stdev->root_model_context;
365 int model_index = 0;
366 if (!last_model_context) {
367 ALOGI("ZERO Models exist.");
368 write_string(conn_socket, "Zero models exist.\n");
369 }
370 while (last_model_context) {
371 write_vastr(conn_socket, "Model Index: %d\n", model_index);
372 ALOGI("Model Index: %d\n", model_index);
373 write_vastr(conn_socket, "Model handle: %d\n",
374 last_model_context->model_handle);
375 ALOGI("Model handle: %d\n", last_model_context->model_handle);
376 sound_trigger_sound_model_type_t model_type = last_model_context->model_type;
377
378 if (model_type == SOUND_MODEL_TYPE_KEYPHRASE) {
379 write_string(conn_socket, "Keyphrase sound Model.\n");
380 ALOGI("Keyphrase sound Model.\n");
381 } else if (model_type == SOUND_MODEL_TYPE_GENERIC) {
382 write_string(conn_socket, "Generic sound Model.\n");
383 ALOGI("Generic sound Model.\n");
384 } else {
385 write_vastr(conn_socket, "Unknown sound model type: %d\n",
386 model_type);
387 ALOGI("Unknown sound model type: %d\n", model_type);
388 }
389 write_string(conn_socket, "----\n\n");
390 ALOGI("----\n\n");
391 last_model_context = last_model_context->next;
392 model_index++;
393 }
394}
395
396// Returns model at the given index, null otherwise (error, doesn't exist, etc).
397// Note that here index starts from zero.
398struct recognition_context* fetch_model_at_index(
399 struct stub_sound_trigger_device* stdev, int index) {
400 ALOGI("%s", __func__);
401 struct recognition_context *model_context = NULL;
402 struct recognition_context *last_model_context = stdev->root_model_context;
403 int model_index = 0;
404 while(last_model_context) {
405 if (model_index == index) {
406 model_context = last_model_context;
407 break;
408 }
409 last_model_context = last_model_context->next;
410 model_index++;
411 }
412 return model_context;
413}
414
415void send_trigger(int conn_socket, char* buffer,
416 struct stub_sound_trigger_device* stdev) {
417 ALOGI("%s", __func__);
418 char* model_handle_str = strtok(NULL, " ");
419 if (model_handle_str == NULL) {
420 write_string(conn_socket, "Bad sound model id.\n");
421 return;
422 }
423 int index = -1;
424 if (sscanf(model_handle_str, "%d", &index) <= 0) {
425 write_vastr(conn_socket, "Unable to parse sound model index: %s\n", model_handle_str);
426 return;
427 }
428
429 if (index < (int)hw_properties.max_sound_models) {
430 ALOGI("Going to send trigger for model index #%d", index );
431 struct recognition_context *model_context = fetch_model_at_index(stdev, index);
432 if (model_context) {
433 send_recognition_event(model_context->model_handle,
434 model_context->model_type,
435 model_context->recognition_callback,
436 model_context->recognition_cookie,
437 model_context->config);
438 } else {
439 ALOGI("Sound Model Does Not Exist at this Index: %d", index);
440 write_string(conn_socket, "Sound Model Does Not Exist at given Index.\n");
441 }
442 }
443}
444
445void process_send_model_event(int conn_socket, char* buffer,
446 struct stub_sound_trigger_device* stdev) {
447 ALOGI("%s", __func__);
448 char* model_handle_str = strtok(NULL, " ");
449 if (model_handle_str == NULL) {
450 write_string(conn_socket, "Bad sound model id.\n");
451 return;
452 }
453 int index = -1;
454 if (sscanf(model_handle_str, "%d", &index) <= 0) {
455 write_vastr(conn_socket, "Unable to parse sound model index: %s\n", model_handle_str);
456 return;
457 }
458
459 if (index < (int)hw_properties.max_sound_models) {
460 ALOGI("Going to model event for model index #%d", index );
461 struct recognition_context *model_context = fetch_model_at_index(stdev, index);
462 if (model_context) {
463
464 send_model_event(model_context->model_handle,
465 model_context->model_callback,
466 model_context->model_cookie);
467 } else {
468 ALOGI("Sound Model Does Not Exist at this Index: %d", index);
469 write_string(conn_socket, "Sound Model Does Not Exist at given Index.\n");
470 }
471 }
472}
473
474// Gets the next word from buffer, replaces '\n' or ' ' with '\0'.
475char* get_command(char* buffer) {
476 char* command = strtok(buffer, " ");
477 char* newline = strchr(command, '\n');
478 if (newline != NULL) {
479 *newline = '\0';
480 }
481 return command;
482}
483
484// Parses data coming in from the local socket, executes commands. Returns when
485// done. Return code indicates whether the server should continue listening or
486// abort (true if continue listening).
487bool parse_socket_data(int conn_socket, struct stub_sound_trigger_device* stdev) {
488 ALOGI("Calling parse_socket_data");
489 bool input_done = false;
490 char buffer[PARSE_BUF_LEN];
491 FILE* input_fp = fdopen(conn_socket, "r");
492 pthread_mutex_lock(&stdev->lock);
493 bool continue_listening = true;
494 while(!input_done) {
495 if (fgets(buffer, PARSE_BUF_LEN, input_fp) != NULL) {
496 char* command = strtok(buffer, " \n");
497 if (command == NULL) {
498 write_bad_command_error(conn_socket, command);
499 } else if (strncmp(command, COMMAND_LS, 2) == 0) {
500 list_models(conn_socket, buffer, stdev);
501 } else if (strcmp(command, COMMAND_TRIGGER) == 0) {
502 send_trigger(conn_socket, buffer, stdev);
503 } else if (strcmp(command, COMMAND_MODEL_EVENT) == 0) {
504 send_model_event(conn_socket, buffer, stdev);
505 } else if (strncmp(command, COMMAND_CLOSE, 5) == 0) {
506 ALOGI("Closing this connection.");
507 write_string(conn_socket, "Closing this connection.");
508 break;
509 } else if (strncmp(command, COMMAND_END, 3) == 0) {
510 ALOGI("End command received.");
511 write_string(conn_socket, "End command received. Stopping connection.");
512 continue_listening = false;
513 break;
514 }
515 } else {
516 ALOGI("parse_socket_data done (got null)");
517 input_done = true; // break.
518 }
519 }
520 pthread_mutex_unlock(&stdev->lock);
521 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