blob: dadad82b490b0e5bd736b61b2d65196a7e0d1480 [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.
Ryan Bavettaa8328a32016-03-11 00:00:30 -080027 * 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 Mishra7db0a7a2016-02-19 16:20:10 -080029 * 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"
Ryan Bavetta9beb06c2016-02-29 18:37:29 -080041#define COMMAND_RECOGNITION_TRIGGER "trig" // Argument: model index.
42#define COMMAND_RECOGNITION_ABORT "abort" // Argument: model index.
43#define COMMAND_RECOGNITION_FAILURE "fail" // Argument: model index.
Arunesh Mishra7db0a7a2016-02-19 16:20:10 -080044#define COMMAND_UPDATE "update" // Argument: model index.
Arunesh Mishraf1d59fc2016-02-15 17:48:27 -080045#define COMMAND_CLOSE "close" // Close just closes the network port, keeps thread running.
46#define COMMAND_END "end" // Closes connection and stops the thread.
47
48#define ERROR_BAD_COMMAND "Bad command"
Eric Laurentcbca9052014-04-18 17:54:10 -070049
50#include <errno.h>
Arunesh Mishraf1d59fc2016-02-15 17:48:27 -080051#include <stdarg.h>
Ryan Bavetta4615aad2016-01-27 16:12:04 -080052#include <stdio.h>
53#include <stdlib.h>
54#include <string.h>
55#include <unistd.h>
56#include <arpa/inet.h>
57#include <sys/types.h>
58#include <netinet/in.h>
59#include <sys/socket.h>
60
61#include <errno.h>
Eric Laurentcbca9052014-04-18 17:54:10 -070062#include <pthread.h>
63#include <sys/prctl.h>
64#include <cutils/log.h>
65
66#include <hardware/hardware.h>
67#include <system/sound_trigger.h>
68#include <hardware/sound_trigger.h>
69
70static const struct sound_trigger_properties hw_properties = {
71 "The Android Open Source Project", // implementor
72 "Sound Trigger stub HAL", // description
73 1, // version
74 { 0xed7a7d60, 0xc65e, 0x11e3, 0x9be4, { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } }, // uuid
Arunesh Mishraf1d59fc2016-02-15 17:48:27 -080075 4, // max_sound_models
Eric Laurentcbca9052014-04-18 17:54:10 -070076 1, // max_key_phrases
77 1, // max_users
78 RECOGNITION_MODE_VOICE_TRIGGER, // recognition_modes
79 false, // capture_transition
80 0, // max_buffer_ms
Ryan Bavetta9beb06c2016-02-29 18:37:29 -080081 true, // concurrent_capture
Eric Laurent83cd8302014-07-30 08:58:39 -070082 false, // trigger_in_event
Eric Laurentcbca9052014-04-18 17:54:10 -070083 0 // power_consumption_mw
84};
85
Ryan Bavetta4615aad2016-01-27 16:12:04 -080086struct recognition_context {
Ryan Bavettacc6541c2016-02-09 21:20:51 -080087 // Sound Model information, added in method load_sound_model
88 sound_model_handle_t model_handle;
Ryan Bavettaa8328a32016-03-11 00:00:30 -080089 sound_trigger_uuid_t model_uuid;
Ryan Bavettacc6541c2016-02-09 21:20:51 -080090 sound_trigger_sound_model_type_t model_type;
91 sound_model_callback_t model_callback;
92 void *model_cookie;
Ryan Bavetta4615aad2016-01-27 16:12:04 -080093
Ryan Bavettacc6541c2016-02-09 21:20:51 -080094 // Sound Model information, added in start_recognition
Ryan Bavetta4615aad2016-01-27 16:12:04 -080095 struct sound_trigger_recognition_config *config;
96 recognition_callback_t recognition_callback;
97 void *recognition_cookie;
Ryan Bavettacc6541c2016-02-09 21:20:51 -080098
99 // Next recognition_context in the linked list
100 struct recognition_context *next;
Ryan Bavettabe0c8fd2016-02-01 15:17:12 -0800101};
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800102
Arunesh Mishraf1d59fc2016-02-15 17:48:27 -0800103char tmp_write_buffer[PARSE_BUF_LEN];
104
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800105struct stub_sound_trigger_device {
106 struct sound_trigger_hw_device device;
Eric Laurentcbca9052014-04-18 17:54:10 -0700107 pthread_mutex_t lock;
Arunesh Mishrad0535ae2016-02-20 20:52:25 -0800108
109 // This thread opens a port that can be used to monitor and inject events
110 // into the stub HAL.
111 pthread_t control_thread;
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800112
Ryan Bavettacc6541c2016-02-09 21:20:51 -0800113 // Recognition contexts are stored as a linked list
114 struct recognition_context *root_model_context;
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800115
116 int next_sound_model_id;
Eric Laurentcbca9052014-04-18 17:54:10 -0700117};
118
Ryan Bavettaa8328a32016-03-11 00:00:30 -0800119static bool check_uuid_equality(sound_trigger_uuid_t uuid1, sound_trigger_uuid_t uuid2) {
120 if (uuid1.timeLow != uuid2.timeLow ||
121 uuid1.timeMid != uuid2.timeMid ||
122 uuid1.timeHiAndVersion != uuid2.timeHiAndVersion ||
123 uuid1.clockSeq != uuid2.clockSeq) {
124 return false;
125 }
126 for (int i = 0; i < 6; i++) {
127 if(uuid1.node[i] != uuid2.node[i]) {
128 return false;
129 }
130 }
131 return true;
132}
133
134bool str_to_uuid(char* uuid_str, sound_trigger_uuid_t* uuid) {
135 if (uuid_str == NULL) {
136 ALOGI("Invalid str_to_uuid input.");
137 return false;
138 }
139
140 int tmp[10];
141 if (sscanf(uuid_str, "%08x-%04x-%04x-%04x-%02x%02x%02x%02x%02x%02x",
142 tmp, tmp+1, tmp+2, tmp+3, tmp+4, tmp+5, tmp+6, tmp+7, tmp+8, tmp+9) < 10) {
143 ALOGI("Invalid UUID, got: %s", uuid_str);
144 return false;
145 }
146 uuid->timeLow = (unsigned int)tmp[0];
147 uuid->timeMid = (unsigned short)tmp[1];
148 uuid->timeHiAndVersion = (unsigned short)tmp[2];
149 uuid->clockSeq = (unsigned short)tmp[3];
150 uuid->node[0] = (unsigned char)tmp[4];
151 uuid->node[1] = (unsigned char)tmp[5];
152 uuid->node[2] = (unsigned char)tmp[6];
153 uuid->node[3] = (unsigned char)tmp[7];
154 uuid->node[4] = (unsigned char)tmp[8];
155 uuid->node[5] = (unsigned char)tmp[9];
156 return true;
157}
158
Ryan Bavetta9beb06c2016-02-29 18:37:29 -0800159// Returns model at the given index, null otherwise (error, doesn't exist, etc).
160// Note that here index starts from zero.
Ryan Bavettaa8328a32016-03-11 00:00:30 -0800161struct recognition_context* fetch_model_with_handle(
162 struct stub_sound_trigger_device* stdev, sound_model_handle_t* model_handle) {
Ryan Bavetta9beb06c2016-02-29 18:37:29 -0800163 ALOGI("%s", __func__);
164 struct recognition_context *model_context = NULL;
165 struct recognition_context *last_model_context = stdev->root_model_context;
Ryan Bavetta9beb06c2016-02-29 18:37:29 -0800166 while(last_model_context) {
Ryan Bavettaa8328a32016-03-11 00:00:30 -0800167 if (last_model_context->model_handle == *model_handle) {
Ryan Bavetta9beb06c2016-02-29 18:37:29 -0800168 model_context = last_model_context;
169 break;
170 }
171 last_model_context = last_model_context->next;
Ryan Bavetta9beb06c2016-02-29 18:37:29 -0800172 }
173 return model_context;
174}
175
Ryan Bavettaa8328a32016-03-11 00:00:30 -0800176// Returns the first model that matches the sound model UUID.
177static sound_model_handle_t* get_model_handle_with_uuid(struct stub_sound_trigger_device* stdev,
178 sound_trigger_uuid_t uuid) {
179 sound_model_handle_t* model_handle_str = NULL;
180 struct recognition_context *last_model_context = stdev->root_model_context;
181 while(last_model_context) {
182 if (check_uuid_equality(last_model_context->model_uuid, uuid)) {
183 model_handle_str = &last_model_context->model_handle;
184 break;
185 }
186 last_model_context = last_model_context->next;
187 }
188 return model_handle_str;
189}
190
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800191/* Will reuse ids when overflow occurs */
Ryan Bavettaf9f08712016-02-19 21:18:46 -0800192static sound_model_handle_t generate_sound_model_handle(const struct sound_trigger_hw_device *dev) {
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800193 struct stub_sound_trigger_device *stdev = (struct stub_sound_trigger_device *)dev;
194 int new_id = stdev->next_sound_model_id;
195 ++stdev->next_sound_model_id;
196 if (stdev->next_sound_model_id == 0) {
197 stdev->next_sound_model_id = 1;
198 }
Ryan Bavettaf9f08712016-02-19 21:18:46 -0800199 return (sound_model_handle_t) new_id;
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800200}
Eric Laurentcbca9052014-04-18 17:54:10 -0700201
Arunesh Mishra7db0a7a2016-02-19 16:20:10 -0800202bool parse_socket_data(int conn_socket, struct stub_sound_trigger_device* stdev);
203
Ryan Bavetta9beb06c2016-02-29 18:37:29 -0800204static char *sound_trigger_keyphrase_event_alloc(sound_model_handle_t handle,
205 struct sound_trigger_recognition_config *config,
206 int recognition_status) {
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800207 char *data;
Ryan Bavetta9beb06c2016-02-29 18:37:29 -0800208 struct sound_trigger_phrase_recognition_event *event;
209 data = (char *)calloc(1, sizeof(struct sound_trigger_phrase_recognition_event));
210 if (!data)
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800211 return NULL;
Ryan Bavetta9beb06c2016-02-29 18:37:29 -0800212 event = (struct sound_trigger_phrase_recognition_event *)data;
213 event->common.status = recognition_status;
214 event->common.type = SOUND_MODEL_TYPE_KEYPHRASE;
215 event->common.model = handle;
216
217 if (config) {
218 unsigned int i;
219
220 event->num_phrases = config->num_phrases;
221 if (event->num_phrases > SOUND_TRIGGER_MAX_PHRASES)
222 event->num_phrases = SOUND_TRIGGER_MAX_PHRASES;
223 for (i=0; i < event->num_phrases; i++)
224 memcpy(&event->phrase_extras[i],
225 &config->phrases[i],
226 sizeof(struct sound_trigger_phrase_recognition_extra));
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800227 }
Ryan Bavetta9beb06c2016-02-29 18:37:29 -0800228
229 event->num_phrases = 1;
230 event->phrase_extras[0].confidence_level = 100;
231 event->phrase_extras[0].num_levels = 1;
232 event->phrase_extras[0].levels[0].level = 100;
233 event->phrase_extras[0].levels[0].user_id = 0;
234 // Signify that all the data is comming through streaming, not through the buffer.
235 event->common.capture_available = true;
236 event->common.audio_config = AUDIO_CONFIG_INITIALIZER;
237 event->common.audio_config.sample_rate = 16000;
238 event->common.audio_config.channel_mask = AUDIO_CHANNEL_IN_MONO;
239 event->common.audio_config.format = AUDIO_FORMAT_PCM_16_BIT;
Ryan Bavettacc6541c2016-02-09 21:20:51 -0800240 return data;
241}
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800242
Ryan Bavetta9beb06c2016-02-29 18:37:29 -0800243static char *sound_trigger_generic_event_alloc(sound_model_handle_t handle,
244 struct sound_trigger_recognition_config *config,
245 int recognition_status) {
246 char *data;
247 struct sound_trigger_generic_recognition_event *event;
248 data = (char *)calloc(1, sizeof(struct sound_trigger_generic_recognition_event));
249 if (!data)
250 return NULL;
251 event = (struct sound_trigger_generic_recognition_event *)data;
252 event->common.status = recognition_status;
253 event->common.type = SOUND_MODEL_TYPE_GENERIC;
254 event->common.model = handle;
255
256 // Signify that all the data is comming through streaming, not through the buffer.
257 event->common.capture_available = true;
258 event->common.audio_config = AUDIO_CONFIG_INITIALIZER;
259 event->common.audio_config.sample_rate = 16000;
260 event->common.audio_config.channel_mask = AUDIO_CHANNEL_IN_MONO;
261 event->common.audio_config.format = AUDIO_FORMAT_PCM_16_BIT;
262 return data;
263}
264
265void write_bad_command_error(int conn_socket, char* command) {
266 int num = snprintf(tmp_write_buffer, PARSE_BUF_LEN, "Bad command received: %s", command);
267 tmp_write_buffer[PARSE_BUF_LEN - 1] = '\0'; // Just to be sure.
268 tmp_write_buffer[PARSE_BUF_LEN - 2] = '\n';
269 write(conn_socket, tmp_write_buffer, num);
270}
271
272void write_string(int conn_socket, char* str) {
273 int num = snprintf(tmp_write_buffer, PARSE_BUF_LEN, "%s", str);
274 tmp_write_buffer[PARSE_BUF_LEN - 1] = '\0';
275 tmp_write_buffer[PARSE_BUF_LEN - 2] = '\n';
276 write(conn_socket, tmp_write_buffer, num);
277}
278
279void write_vastr(int conn_socket, char* format, ...) {
280 va_list argptr;
281 va_start(argptr, format);
282 int num = vsnprintf(tmp_write_buffer, PARSE_BUF_LEN, format, argptr);
283 va_end(argptr);
284 tmp_write_buffer[PARSE_BUF_LEN - 1] = '\0';
285 tmp_write_buffer[PARSE_BUF_LEN - 2] = '\n';
286 write(conn_socket, tmp_write_buffer, num);
287}
288
Ryan Bavettaa8328a32016-03-11 00:00:30 -0800289static void print_uuid(sound_trigger_uuid_t uuid) {
290 ALOGI("%s %x-%x-%x-%x-%x%x%x%x%x%x", __func__, uuid.timeLow, uuid.timeMid,
291 uuid.timeHiAndVersion, uuid.clockSeq, uuid.node[0], uuid.node[1], uuid.node[2],
292 uuid.node[3], uuid.node[4], uuid.node[5]);
293}
294
295static void write_uuid(int conn_socket, sound_trigger_uuid_t uuid) {
296 write_vastr(conn_socket, "%d-%x-%x-%x-%x%x%x%x%x%x\n", uuid.timeLow, uuid.timeMid,
297 uuid.timeHiAndVersion, uuid.clockSeq, uuid.node[0], uuid.node[1], uuid.node[2],
298 uuid.node[3], uuid.node[4], uuid.node[5]);
299}
300
301void send_recognition_event_with_handle(int conn_socket, sound_model_handle_t* model_handle_str,
302 struct stub_sound_trigger_device* stdev,
303 int recognition_status) {
Ryan Bavetta9beb06c2016-02-29 18:37:29 -0800304 ALOGI("%s", __func__);
Ryan Bavetta9beb06c2016-02-29 18:37:29 -0800305 if (model_handle_str == NULL) {
Ryan Bavettaa8328a32016-03-11 00:00:30 -0800306 ALOGI("%s Bad sound model handle.", __func__);
307 write_string(conn_socket, "Bad sound model handle.\n");
Ryan Bavettacc6541c2016-02-09 21:20:51 -0800308 return;
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800309 }
310
Ryan Bavettaa8328a32016-03-11 00:00:30 -0800311 ALOGI("Going to send trigger for model");
312 struct recognition_context *model_context = fetch_model_with_handle(stdev, model_handle_str);
313 if (model_context) {
314 if (model_context->recognition_callback == NULL) {
315 ALOGI("%s No matching callback for handle %d", __func__,
316 model_context->model_handle);
317 return;
318 }
319
320 if (model_context->model_type == SOUND_MODEL_TYPE_KEYPHRASE) {
321 struct sound_trigger_phrase_recognition_event *event;
322 event = (struct sound_trigger_phrase_recognition_event *)
323 sound_trigger_keyphrase_event_alloc(model_context->model_handle,
324 model_context->config,
325 recognition_status);
326 if (event) {
327 model_context->recognition_callback(event, model_context->recognition_cookie);
328 free(event);
Ryan Bavetta9beb06c2016-02-29 18:37:29 -0800329 }
Ryan Bavettaa8328a32016-03-11 00:00:30 -0800330 } else if (model_context->model_type == SOUND_MODEL_TYPE_GENERIC) {
331 struct sound_trigger_generic_recognition_event *event;
332 event = (struct sound_trigger_generic_recognition_event *)
333 sound_trigger_generic_event_alloc(model_context->model_handle,
334 model_context->config,
335 recognition_status);
336 if (event) {
337 model_context->recognition_callback(event, model_context->recognition_cookie);
338 free(event);
Ryan Bavetta9beb06c2016-02-29 18:37:29 -0800339 }
340 } else {
Ryan Bavettaa8328a32016-03-11 00:00:30 -0800341 ALOGI("Unknown Sound Model Type, No Event to Send");
Ryan Bavettacc6541c2016-02-09 21:20:51 -0800342 }
Ryan Bavettaa8328a32016-03-11 00:00:30 -0800343 } else {
344 ALOGI("No model for this handle");
345 }
346}
347
348static void send_recognition_event(int conn_socket, struct stub_sound_trigger_device* stdev,
349 int recognition_status) {
350 char* model_uuid_str = strtok(NULL, " \r\n");
351 sound_trigger_uuid_t model_uuid;
352 if (str_to_uuid(model_uuid_str, &model_uuid)) {
353 sound_model_handle_t* model_handle_str = get_model_handle_with_uuid(stdev, model_uuid);
354 send_recognition_event_with_handle(conn_socket, model_handle_str, stdev,
355 recognition_status);
Ryan Bavettacc6541c2016-02-09 21:20:51 -0800356 }
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800357}
358
Ryan Bavettac2bdc552016-02-15 15:51:46 -0800359static void send_model_event(sound_model_handle_t model_handle,
360 sound_model_callback_t model_callback, void *model_cookie) {
361
362 if (model_callback == NULL) {
363 ALOGI("%s No matching callback for handle %d", __func__, model_handle);
364 return;
365 }
366
367 char *data;
368 data = (char *)calloc(1, sizeof(struct sound_trigger_model_event));
369 if (!data) {
370 ALOGW("%s Could not allocate event %d", __func__, model_handle);
371 return;
372 }
373
374 struct sound_trigger_model_event *event;
375 event = (struct sound_trigger_model_event *)data;
376 event->status = SOUND_MODEL_STATUS_UPDATED;
377 event->model = model_handle;
378
379 if (event) {
380 model_callback(&event, model_cookie);
381 free(event);
382 }
383}
384
385static bool recognition_callback_exists(struct stub_sound_trigger_device *stdev) {
386 bool callback_found = false;
387 if (stdev->root_model_context) {
388 struct recognition_context *current_model_context = stdev->root_model_context;
389 while(current_model_context) {
390 if (current_model_context->recognition_callback != NULL) {
391 callback_found = true;
392 break;
393 }
394 current_model_context = current_model_context->next;
395 }
396 }
397 return callback_found;
398}
399
400static struct recognition_context * get_model_context(struct stub_sound_trigger_device *stdev,
401 sound_model_handle_t handle) {
402 struct recognition_context *model_context = NULL;
403 if (stdev->root_model_context) {
404 struct recognition_context *current_model_context = stdev->root_model_context;
405 while(current_model_context) {
406 if (current_model_context->model_handle == handle) {
407 model_context = current_model_context;
408 break;
409 }
410 current_model_context = current_model_context->next;
411 }
412 }
413 return model_context;
414}
415
Arunesh Mishrad0535ae2016-02-20 20:52:25 -0800416static void *control_thread_loop(void *context) {
Eric Laurentcbca9052014-04-18 17:54:10 -0700417 struct stub_sound_trigger_device *stdev = (struct stub_sound_trigger_device *)context;
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800418 struct sockaddr_in incoming_info;
419 struct sockaddr_in self_info;
420 int self_socket;
421 socklen_t sock_size = sizeof(struct sockaddr_in);
422 memset(&self_info, 0, sizeof(self_info));
423 self_info.sin_family = AF_INET;
424 self_info.sin_addr.s_addr = htonl(INADDR_ANY);
425 self_info.sin_port = htons(14035);
Eric Laurentcbca9052014-04-18 17:54:10 -0700426
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800427 bool exit = false;
428 while(!exit) {
429 int received_count;
Ryan Bavettac2bdc552016-02-15 15:51:46 -0800430 int requested_count = 2;
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800431 char buffer[requested_count];
432 ALOGE("Opening socket");
433 self_socket = socket(AF_INET, SOCK_STREAM, 0);
434 if (self_socket < 0) {
Ryan Bavettaa8328a32016-03-11 00:00:30 -0800435 ALOGE("Error on socket creation: %s", strerror(errno));
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800436 exit = true;
437 } else {
438 ALOGI("Socket created");
439 }
Eric Laurentcbca9052014-04-18 17:54:10 -0700440
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800441 int reuse = 1;
442 if (setsockopt(self_socket, SOL_SOCKET, SO_REUSEADDR, (const char*)&reuse, sizeof(reuse)) < 0) {
443 ALOGE("setsockopt(SO_REUSEADDR) failed");
444 }
445
446 int bind_result = bind(self_socket, (struct sockaddr *)&self_info, sizeof(struct sockaddr));
447 if (bind_result < 0) {
448 ALOGE("Error on bind");
449 exit = true;
450 }
451
452 int listen_result = listen(self_socket, 1);
453 if (listen_result < 0) {
454 ALOGE("Error on Listen");
455 exit = true;
456 }
457
458 while(!exit) {
459 int con_socket = accept(self_socket, (struct sockaddr *)&incoming_info, &sock_size);
460 if (!con_socket) {
461 ALOGE("Lost socket, cannot send trigger");
462 break;
463 }
464 ALOGI("Connection from %s", inet_ntoa(incoming_info.sin_addr));
Arunesh Mishraf1d59fc2016-02-15 17:48:27 -0800465 if (!parse_socket_data(con_socket, stdev)) {
466 ALOGI("Done processing commands over network. Stopping thread.");
467 exit = true;
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800468 }
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800469 close(con_socket);
470 }
471 ALOGE("Closing socket");
472 close(self_socket);
Eric Laurentcbca9052014-04-18 17:54:10 -0700473 }
Eric Laurentcbca9052014-04-18 17:54:10 -0700474
475 return NULL;
476}
477
Arunesh Mishraf1d59fc2016-02-15 17:48:27 -0800478void list_models(int conn_socket, char* buffer,
479 struct stub_sound_trigger_device* stdev) {
480 ALOGI("%s", __func__);
481 struct recognition_context *last_model_context = stdev->root_model_context;
Ryan Bavettaf9f08712016-02-19 21:18:46 -0800482 unsigned int model_index = 0;
Arunesh Mishraf1d59fc2016-02-15 17:48:27 -0800483 if (!last_model_context) {
484 ALOGI("ZERO Models exist.");
485 write_string(conn_socket, "Zero models exist.\n");
486 }
487 while (last_model_context) {
488 write_vastr(conn_socket, "Model Index: %d\n", model_index);
Ryan Bavettaa8328a32016-03-11 00:00:30 -0800489 ALOGI("Model Index: %d", model_index);
490 write_vastr(conn_socket, "Model handle: %d\n", last_model_context->model_handle);
491 ALOGI("Model handle: %d", last_model_context->model_handle);
492 write_uuid(conn_socket, last_model_context->model_uuid);
493 print_uuid(last_model_context->model_uuid);
Arunesh Mishraf1d59fc2016-02-15 17:48:27 -0800494 sound_trigger_sound_model_type_t model_type = last_model_context->model_type;
495
496 if (model_type == SOUND_MODEL_TYPE_KEYPHRASE) {
497 write_string(conn_socket, "Keyphrase sound Model.\n");
Ryan Bavettaa8328a32016-03-11 00:00:30 -0800498 ALOGI("Keyphrase sound Model.");
Arunesh Mishraf1d59fc2016-02-15 17:48:27 -0800499 } else if (model_type == SOUND_MODEL_TYPE_GENERIC) {
500 write_string(conn_socket, "Generic sound Model.\n");
Ryan Bavettaa8328a32016-03-11 00:00:30 -0800501 ALOGI("Generic sound Model.");
Arunesh Mishraf1d59fc2016-02-15 17:48:27 -0800502 } else {
Ryan Bavettaa8328a32016-03-11 00:00:30 -0800503 write_vastr(conn_socket, "Unknown sound model type: %d\n", model_type);
504 ALOGI("Unknown sound model type: %d", model_type);
Arunesh Mishraf1d59fc2016-02-15 17:48:27 -0800505 }
506 write_string(conn_socket, "----\n\n");
Ryan Bavettaa8328a32016-03-11 00:00:30 -0800507 ALOGI("----");
Arunesh Mishraf1d59fc2016-02-15 17:48:27 -0800508 last_model_context = last_model_context->next;
509 model_index++;
510 }
511}
512
Arunesh Mishraf1d59fc2016-02-15 17:48:27 -0800513void process_send_model_event(int conn_socket, char* buffer,
514 struct stub_sound_trigger_device* stdev) {
515 ALOGI("%s", __func__);
516 char* model_handle_str = strtok(NULL, " ");
517 if (model_handle_str == NULL) {
518 write_string(conn_socket, "Bad sound model id.\n");
519 return;
520 }
Arunesh Mishraf1d59fc2016-02-15 17:48:27 -0800521
Ryan Bavettaa8328a32016-03-11 00:00:30 -0800522 ALOGI("Going to model event for model index #%d", index );
523 struct recognition_context *model_context = fetch_model_with_handle(stdev, model_handle_str);
524 if (model_context) {
Arunesh Mishraf1d59fc2016-02-15 17:48:27 -0800525
Ryan Bavettaa8328a32016-03-11 00:00:30 -0800526 send_model_event(model_context->model_handle,
527 model_context->model_callback,
528 model_context->model_cookie);
529 } else {
530 ALOGI("Sound Model Does Not Exist with this handle");
531 write_string(conn_socket, "Sound Model Does Not Exist with this handle.\n");
Arunesh Mishraf1d59fc2016-02-15 17:48:27 -0800532 }
533}
534
535// Gets the next word from buffer, replaces '\n' or ' ' with '\0'.
536char* get_command(char* buffer) {
537 char* command = strtok(buffer, " ");
538 char* newline = strchr(command, '\n');
539 if (newline != NULL) {
540 *newline = '\0';
541 }
542 return command;
543}
544
545// Parses data coming in from the local socket, executes commands. Returns when
546// done. Return code indicates whether the server should continue listening or
547// abort (true if continue listening).
548bool parse_socket_data(int conn_socket, struct stub_sound_trigger_device* stdev) {
549 ALOGI("Calling parse_socket_data");
550 bool input_done = false;
551 char buffer[PARSE_BUF_LEN];
552 FILE* input_fp = fdopen(conn_socket, "r");
Arunesh Mishraf1d59fc2016-02-15 17:48:27 -0800553 bool continue_listening = true;
Arunesh Mishrae1df8132016-02-20 17:34:12 -0800554
555 // Note: Since we acquire a lock inside this loop, do not use break or other
556 // exit methods without releasing this lock.
Arunesh Mishraf1d59fc2016-02-15 17:48:27 -0800557 while(!input_done) {
558 if (fgets(buffer, PARSE_BUF_LEN, input_fp) != NULL) {
Arunesh Mishra7db0a7a2016-02-19 16:20:10 -0800559 pthread_mutex_lock(&stdev->lock);
Ryan Bavettaa8328a32016-03-11 00:00:30 -0800560 char* command = strtok(buffer, " \r\n");
Arunesh Mishraf1d59fc2016-02-15 17:48:27 -0800561 if (command == NULL) {
562 write_bad_command_error(conn_socket, command);
563 } else if (strncmp(command, COMMAND_LS, 2) == 0) {
564 list_models(conn_socket, buffer, stdev);
Ryan Bavetta9beb06c2016-02-29 18:37:29 -0800565 } else if (strcmp(command, COMMAND_RECOGNITION_TRIGGER) == 0) {
Ryan Bavettaa8328a32016-03-11 00:00:30 -0800566 send_recognition_event(conn_socket, stdev, RECOGNITION_STATUS_SUCCESS);
Ryan Bavetta9beb06c2016-02-29 18:37:29 -0800567 } else if (strcmp(command, COMMAND_RECOGNITION_ABORT) == 0) {
Ryan Bavettaa8328a32016-03-11 00:00:30 -0800568 send_recognition_event(conn_socket, stdev, RECOGNITION_STATUS_ABORT);
Ryan Bavetta9beb06c2016-02-29 18:37:29 -0800569 } else if (strcmp(command, COMMAND_RECOGNITION_FAILURE) == 0) {
Ryan Bavettaa8328a32016-03-11 00:00:30 -0800570 send_recognition_event(conn_socket, stdev, RECOGNITION_STATUS_FAILURE);
Arunesh Mishra7db0a7a2016-02-19 16:20:10 -0800571 } else if (strcmp(command, COMMAND_UPDATE) == 0) {
572 process_send_model_event(conn_socket, buffer, stdev);
Arunesh Mishraf1d59fc2016-02-15 17:48:27 -0800573 } else if (strncmp(command, COMMAND_CLOSE, 5) == 0) {
574 ALOGI("Closing this connection.");
575 write_string(conn_socket, "Closing this connection.");
Arunesh Mishrae1df8132016-02-20 17:34:12 -0800576 input_done = true;
Arunesh Mishraf1d59fc2016-02-15 17:48:27 -0800577 } else if (strncmp(command, COMMAND_END, 3) == 0) {
578 ALOGI("End command received.");
579 write_string(conn_socket, "End command received. Stopping connection.");
580 continue_listening = false;
Arunesh Mishrae1df8132016-02-20 17:34:12 -0800581 input_done = true;
Arunesh Mishra7db0a7a2016-02-19 16:20:10 -0800582 } else {
583 write_vastr(conn_socket, "Bad command %s.\n", command);
Arunesh Mishraf1d59fc2016-02-15 17:48:27 -0800584 }
Arunesh Mishra7db0a7a2016-02-19 16:20:10 -0800585 pthread_mutex_unlock(&stdev->lock);
Arunesh Mishraf1d59fc2016-02-15 17:48:27 -0800586 } else {
587 ALOGI("parse_socket_data done (got null)");
588 input_done = true; // break.
589 }
590 }
Arunesh Mishraf1d59fc2016-02-15 17:48:27 -0800591 return continue_listening;
592}
593
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800594static void send_loop_kill_signal() {
595 ALOGI("Sending loop thread kill signal");
596 int self_socket = socket(AF_INET, SOCK_STREAM, 0);
597 struct sockaddr_in remote_info;
598 memset(&remote_info, 0, sizeof(remote_info));
599 remote_info.sin_family = AF_INET;
600 remote_info.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
601 remote_info.sin_port = htons(14035);
602 if (connect(self_socket, (struct sockaddr *)&remote_info, sizeof(struct sockaddr)) == 0) {
Ryan Bavettaf9f08712016-02-19 21:18:46 -0800603 send(self_socket, COMMAND_END, 3, 0);
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800604 } else {
605 ALOGI("Could not connect");
606 }
607 close(self_socket);
608 ALOGI("Sent loop thread kill signal");
609}
610
Eric Laurentcbca9052014-04-18 17:54:10 -0700611static int stdev_get_properties(const struct sound_trigger_hw_device *dev,
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800612 struct sound_trigger_properties *properties) {
Eric Laurentcbca9052014-04-18 17:54:10 -0700613 struct stub_sound_trigger_device *stdev = (struct stub_sound_trigger_device *)dev;
614
615 ALOGI("%s", __func__);
616 if (properties == NULL)
617 return -EINVAL;
618 memcpy(properties, &hw_properties, sizeof(struct sound_trigger_properties));
619 return 0;
620}
621
622static int stdev_load_sound_model(const struct sound_trigger_hw_device *dev,
623 struct sound_trigger_sound_model *sound_model,
624 sound_model_callback_t callback,
625 void *cookie,
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800626 sound_model_handle_t *handle) {
Eric Laurentcbca9052014-04-18 17:54:10 -0700627 struct stub_sound_trigger_device *stdev = (struct stub_sound_trigger_device *)dev;
Eric Laurentcbca9052014-04-18 17:54:10 -0700628 ALOGI("%s stdev %p", __func__, stdev);
Ryan Bavettaa8328a32016-03-11 00:00:30 -0800629 int status = 0;
Eric Laurentcbca9052014-04-18 17:54:10 -0700630 pthread_mutex_lock(&stdev->lock);
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800631
Eric Laurentcbca9052014-04-18 17:54:10 -0700632 if (handle == NULL || sound_model == NULL) {
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800633 pthread_mutex_unlock(&stdev->lock);
634 return -EINVAL;
Eric Laurentcbca9052014-04-18 17:54:10 -0700635 }
636 if (sound_model->data_size == 0 ||
637 sound_model->data_offset < sizeof(struct sound_trigger_sound_model)) {
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800638 pthread_mutex_unlock(&stdev->lock);
639 return -EINVAL;
Eric Laurentcbca9052014-04-18 17:54:10 -0700640 }
641
Ryan Bavettacc6541c2016-02-09 21:20:51 -0800642 struct recognition_context *model_context;
643 model_context = malloc(sizeof(struct recognition_context));
644 if(!model_context) {
645 ALOGW("Could not allocate recognition_context");
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800646 pthread_mutex_unlock(&stdev->lock);
647 return -ENOSYS;
648 }
649
Ryan Bavettacc6541c2016-02-09 21:20:51 -0800650 // Add the new model context to the recognition_context linked list
651 if (stdev->root_model_context) {
652 // Find the tail
Ryan Bavettac2bdc552016-02-15 15:51:46 -0800653 struct recognition_context *current_model_context = stdev->root_model_context;
Ryan Bavettaf9f08712016-02-19 21:18:46 -0800654 unsigned int model_count = 0;
Ryan Bavettac2bdc552016-02-15 15:51:46 -0800655 while(current_model_context->next) {
656 current_model_context = current_model_context->next;
Ryan Bavettacc6541c2016-02-09 21:20:51 -0800657 model_count++;
658 if (model_count >= hw_properties.max_sound_models) {
659 ALOGW("Can't load model: reached max sound model limit");
660 free(model_context);
661 pthread_mutex_unlock(&stdev->lock);
662 return -ENOSYS;
663 }
664 }
Ryan Bavettac2bdc552016-02-15 15:51:46 -0800665 current_model_context->next = model_context;
Ryan Bavettacc6541c2016-02-09 21:20:51 -0800666 } else {
667 stdev->root_model_context = model_context;
668 }
669
Ryan Bavettaf9f08712016-02-19 21:18:46 -0800670 model_context->model_handle = generate_sound_model_handle(dev);
Ryan Bavettacc6541c2016-02-09 21:20:51 -0800671 *handle = model_context->model_handle;
672 model_context->model_type = sound_model->type;
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800673
Eric Laurentcbca9052014-04-18 17:54:10 -0700674 char *data = (char *)sound_model + sound_model->data_offset;
675 ALOGI("%s data size %d data %d - %d", __func__,
676 sound_model->data_size, data[0], data[sound_model->data_size - 1]);
Ryan Bavettaa8328a32016-03-11 00:00:30 -0800677 model_context->model_uuid = sound_model->uuid;
Ryan Bavettacc6541c2016-02-09 21:20:51 -0800678 model_context->model_callback = callback;
679 model_context->model_cookie = cookie;
680 model_context->config = NULL;
681 model_context->recognition_callback = NULL;
682 model_context->recognition_cookie = NULL;
Ryan Bavettaf9f08712016-02-19 21:18:46 -0800683 model_context->next = NULL;
Arunesh Mishraf1d59fc2016-02-15 17:48:27 -0800684 ALOGI("Sound model loaded: Handle %d ", *handle);
Eric Laurentcbca9052014-04-18 17:54:10 -0700685
Eric Laurentcbca9052014-04-18 17:54:10 -0700686 pthread_mutex_unlock(&stdev->lock);
687 return status;
688}
689
690static int stdev_unload_sound_model(const struct sound_trigger_hw_device *dev,
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800691 sound_model_handle_t handle) {
Ryan Bavettac2bdc552016-02-15 15:51:46 -0800692 // If recognizing, stop_recognition must be called for a sound model before unload_sound_model
Ryan Bavettaa8328a32016-03-11 00:00:30 -0800693 ALOGI("%s", __func__);
Eric Laurentcbca9052014-04-18 17:54:10 -0700694 struct stub_sound_trigger_device *stdev = (struct stub_sound_trigger_device *)dev;
695 int status = 0;
Eric Laurentcbca9052014-04-18 17:54:10 -0700696 pthread_mutex_lock(&stdev->lock);
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800697
Ryan Bavettacc6541c2016-02-09 21:20:51 -0800698 struct recognition_context *model_context = NULL;
699 struct recognition_context *previous_model_context = NULL;
700 if (stdev->root_model_context) {
Ryan Bavettac2bdc552016-02-15 15:51:46 -0800701 struct recognition_context *current_model_context = stdev->root_model_context;
702 while(current_model_context) {
703 if (current_model_context->model_handle == handle) {
704 model_context = current_model_context;
Ryan Bavettacc6541c2016-02-09 21:20:51 -0800705 break;
706 }
Ryan Bavettac2bdc552016-02-15 15:51:46 -0800707 previous_model_context = current_model_context;
708 current_model_context = current_model_context->next;
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800709 }
Eric Laurentcbca9052014-04-18 17:54:10 -0700710 }
Ryan Bavettacc6541c2016-02-09 21:20:51 -0800711 if (!model_context) {
712 ALOGW("Can't find sound model handle %d in registered list", handle);
Eric Laurentcbca9052014-04-18 17:54:10 -0700713 pthread_mutex_unlock(&stdev->lock);
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800714 return -ENOSYS;
Eric Laurentcbca9052014-04-18 17:54:10 -0700715 }
716
Ryan Bavettacc6541c2016-02-09 21:20:51 -0800717 if(previous_model_context) {
718 previous_model_context->next = model_context->next;
719 } else {
720 stdev->root_model_context = model_context->next;
721 }
722 free(model_context->config);
723 free(model_context);
Ryan Bavettac2bdc552016-02-15 15:51:46 -0800724 pthread_mutex_unlock(&stdev->lock);
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800725
Eric Laurentcbca9052014-04-18 17:54:10 -0700726 return status;
727}
728
729static int stdev_start_recognition(const struct sound_trigger_hw_device *dev,
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800730 sound_model_handle_t handle,
Eric Laurent30f3e6d2014-07-06 16:08:45 -0700731 const struct sound_trigger_recognition_config *config,
Eric Laurentcbca9052014-04-18 17:54:10 -0700732 recognition_callback_t callback,
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800733 void *cookie) {
734 ALOGI("%s", __func__);
Eric Laurentcbca9052014-04-18 17:54:10 -0700735 struct stub_sound_trigger_device *stdev = (struct stub_sound_trigger_device *)dev;
Eric Laurentcbca9052014-04-18 17:54:10 -0700736 pthread_mutex_lock(&stdev->lock);
Ryan Bavettacc6541c2016-02-09 21:20:51 -0800737
Ryan Bavettac2bdc552016-02-15 15:51:46 -0800738 /* If other models running with callbacks, don't start trigger thread */
739 bool other_callbacks_found = recognition_callback_exists(stdev);
740
741 struct recognition_context *model_context = get_model_context(stdev, handle);
Ryan Bavettacc6541c2016-02-09 21:20:51 -0800742 if (!model_context) {
743 ALOGW("Can't find sound model handle %d in registered list", handle);
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800744 pthread_mutex_unlock(&stdev->lock);
745 return -ENOSYS;
Eric Laurentcbca9052014-04-18 17:54:10 -0700746 }
747
Ryan Bavettacc6541c2016-02-09 21:20:51 -0800748 free(model_context->config);
749 model_context->config = NULL;
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800750 if (config) {
Ryan Bavettacc6541c2016-02-09 21:20:51 -0800751 model_context->config = malloc(sizeof(*config));
752 if (!model_context->config) {
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800753 pthread_mutex_unlock(&stdev->lock);
754 return -ENOMEM;
755 }
Ryan Bavettacc6541c2016-02-09 21:20:51 -0800756 memcpy(model_context->config, config, sizeof(*config));
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800757 }
Ryan Bavettacc6541c2016-02-09 21:20:51 -0800758 model_context->recognition_callback = callback;
759 model_context->recognition_cookie = cookie;
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800760
Eric Laurentcbca9052014-04-18 17:54:10 -0700761 pthread_mutex_unlock(&stdev->lock);
Arunesh Mishrad0535ae2016-02-20 20:52:25 -0800762 ALOGI("%s done for handle %d", __func__, handle);
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800763 return 0;
Eric Laurentcbca9052014-04-18 17:54:10 -0700764}
765
766static int stdev_stop_recognition(const struct sound_trigger_hw_device *dev,
Ryan Bavettac2bdc552016-02-15 15:51:46 -0800767 sound_model_handle_t handle) {
Eric Laurentcbca9052014-04-18 17:54:10 -0700768 struct stub_sound_trigger_device *stdev = (struct stub_sound_trigger_device *)dev;
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800769 ALOGI("%s", __func__);
Eric Laurentcbca9052014-04-18 17:54:10 -0700770 pthread_mutex_lock(&stdev->lock);
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800771
Ryan Bavettac2bdc552016-02-15 15:51:46 -0800772 struct recognition_context *model_context = get_model_context(stdev, handle);
Ryan Bavettacc6541c2016-02-09 21:20:51 -0800773 if (!model_context) {
774 ALOGW("Can't find sound model handle %d in registered list", handle);
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800775 pthread_mutex_unlock(&stdev->lock);
776 return -ENOSYS;
Eric Laurentcbca9052014-04-18 17:54:10 -0700777 }
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800778
Ryan Bavettacc6541c2016-02-09 21:20:51 -0800779 free(model_context->config);
780 model_context->config = NULL;
781 model_context->recognition_callback = NULL;
782 model_context->recognition_cookie = NULL;
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800783
Arunesh Mishrad0535ae2016-02-20 20:52:25 -0800784 pthread_mutex_unlock(&stdev->lock);
785 ALOGI("%s done for handle %d", __func__, handle);
Ryan Bavettacc6541c2016-02-09 21:20:51 -0800786
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800787 return 0;
Eric Laurentcbca9052014-04-18 17:54:10 -0700788}
789
Ryan Bavettabe0c8fd2016-02-01 15:17:12 -0800790__attribute__ ((visibility ("default")))
Ryan Bavettacc6541c2016-02-09 21:20:51 -0800791int sound_trigger_open_for_streaming() {
Ryan Bavettabe0c8fd2016-02-01 15:17:12 -0800792 int ret = 0;
793 return ret;
794}
795
796__attribute__ ((visibility ("default")))
Ryan Bavettacc6541c2016-02-09 21:20:51 -0800797size_t sound_trigger_read_samples(int audio_handle, void *buffer, size_t buffer_len) {
Ryan Bavettabe0c8fd2016-02-01 15:17:12 -0800798 size_t ret = 0;
799 return ret;
800}
801
802__attribute__ ((visibility ("default")))
Ryan Bavettacc6541c2016-02-09 21:20:51 -0800803int sound_trigger_close_for_streaming(int audio_handle __unused) {
Ryan Bavettabe0c8fd2016-02-01 15:17:12 -0800804 return 0;
805}
806
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800807static int stdev_close(hw_device_t *device) {
Arunesh Mishrad0535ae2016-02-20 20:52:25 -0800808 // TODO: Implement the ability to stop the control thread. Since this is a
809 // test hal, we have skipped implementing this for now. A possible method
810 // would register a signal handler for the control thread so that any
811 // blocking socket calls can be interrupted. We would send that signal here
812 // to interrupt and quit the thread.
Eric Laurentcbca9052014-04-18 17:54:10 -0700813 free(device);
814 return 0;
815}
816
817static int stdev_open(const hw_module_t* module, const char* name,
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800818 hw_device_t** device) {
Eric Laurentcbca9052014-04-18 17:54:10 -0700819 struct stub_sound_trigger_device *stdev;
820 int ret;
821
822 if (strcmp(name, SOUND_TRIGGER_HARDWARE_INTERFACE) != 0)
823 return -EINVAL;
824
825 stdev = calloc(1, sizeof(struct stub_sound_trigger_device));
826 if (!stdev)
827 return -ENOMEM;
828
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800829 stdev->next_sound_model_id = 1;
Arunesh Mishraf1d59fc2016-02-15 17:48:27 -0800830 stdev->root_model_context = NULL;
Ryan Bavetta4615aad2016-01-27 16:12:04 -0800831
Eric Laurentcbca9052014-04-18 17:54:10 -0700832 stdev->device.common.tag = HARDWARE_DEVICE_TAG;
833 stdev->device.common.version = SOUND_TRIGGER_DEVICE_API_VERSION_1_0;
834 stdev->device.common.module = (struct hw_module_t *) module;
835 stdev->device.common.close = stdev_close;
836 stdev->device.get_properties = stdev_get_properties;
837 stdev->device.load_sound_model = stdev_load_sound_model;
838 stdev->device.unload_sound_model = stdev_unload_sound_model;
839 stdev->device.start_recognition = stdev_start_recognition;
840 stdev->device.stop_recognition = stdev_stop_recognition;
841
842 pthread_mutex_init(&stdev->lock, (const pthread_mutexattr_t *) NULL);
Eric Laurentcbca9052014-04-18 17:54:10 -0700843
844 *device = &stdev->device.common;
845
Arunesh Mishrad0535ae2016-02-20 20:52:25 -0800846 pthread_create(&stdev->control_thread, (const pthread_attr_t *) NULL,
847 control_thread_loop, stdev);
848 ALOGI("Starting control thread for the stub hal.");
849
Eric Laurentcbca9052014-04-18 17:54:10 -0700850 return 0;
851}
852
853static struct hw_module_methods_t hal_module_methods = {
854 .open = stdev_open,
855};
856
857struct sound_trigger_module HAL_MODULE_INFO_SYM = {
858 .common = {
859 .tag = HARDWARE_MODULE_TAG,
860 .module_api_version = SOUND_TRIGGER_MODULE_API_VERSION_1_0,
861 .hal_api_version = HARDWARE_HAL_API_VERSION,
862 .id = SOUND_TRIGGER_HARDWARE_MODULE_ID,
863 .name = "Default sound trigger HAL",
864 .author = "The Android Open Source Project",
865 .methods = &hal_module_methods,
866 },
867};
Ryan Bavettacc6541c2016-02-09 21:20:51 -0800868