1 /* Copyright (c) 2017 The Chromium OS Authors. All rights reserved. 2 * Use of this source code is governed by a BSD-style license that can be 3 * found in the LICENSE file. 4 */ 5 6 #include <stdint.h> 7 #include <string.h> 8 #include <syslog.h> 9 10 #include "cras_main_message.h" 11 #include "cras_observer.h" 12 13 struct hotword_triggered_msg { 14 struct cras_main_message header; 15 int64_t tv_sec; 16 int64_t tv_nsec; 17 }; 18 19 /* The following functions are called from audio thread. */ 20 init_hotword_triggered_msg(struct hotword_triggered_msg * msg)21static void init_hotword_triggered_msg(struct hotword_triggered_msg *msg) 22 { 23 struct timespec now; 24 25 clock_gettime(CLOCK_MONOTONIC, &now); 26 27 memset(msg, 0, sizeof(*msg)); 28 msg->header.type = CRAS_MAIN_HOTWORD_TRIGGERED; 29 msg->header.length = sizeof(*msg); 30 msg->tv_sec = now.tv_sec; 31 msg->tv_nsec = now.tv_nsec; 32 } 33 cras_hotword_send_triggered_msg()34int cras_hotword_send_triggered_msg() 35 { 36 struct hotword_triggered_msg msg; 37 int rc; 38 39 init_hotword_triggered_msg(&msg); 40 41 rc = cras_main_message_send((struct cras_main_message *)&msg); 42 if (rc < 0) 43 syslog(LOG_ERR, "Failed to send hotword triggered message!"); 44 45 return rc; 46 } 47 48 /* The following functions are called from main thread. */ 49 handle_hotword_message(struct cras_main_message * msg,void * arg)50static void handle_hotword_message(struct cras_main_message *msg, void *arg) 51 { 52 struct hotword_triggered_msg *hotword_msg = 53 (struct hotword_triggered_msg *)msg; 54 55 cras_observer_notify_hotword_triggered(hotword_msg->tv_sec, 56 hotword_msg->tv_nsec); 57 } 58 cras_hotword_handler_init()59int cras_hotword_handler_init() 60 { 61 cras_main_message_add_handler(CRAS_MAIN_HOTWORD_TRIGGERED, 62 handle_hotword_message, NULL); 63 return 0; 64 } 65