Allow Multiple Triggers and Simulate DSP Events

Triggers can be sent via socket connection from an app
or from the command line.

BUG: 22860713
Change-Id: I7ba8fa50286be2d08f2699e601e7c76138cc4831
diff --git a/modules/soundtrigger/sound_trigger_hw.c b/modules/soundtrigger/sound_trigger_hw.c
index e7f9baf..8dba27a 100644
--- a/modules/soundtrigger/sound_trigger_hw.c
+++ b/modules/soundtrigger/sound_trigger_hw.c
@@ -14,10 +14,37 @@
  * limitations under the License.
  */
 
+
+/* This HAL simulates triggers from the DSP.
+ * To send a trigger from the command line you can type:
+ *
+ * adb forward tcp:14035 tcp:14035
+ * echo $'\001' | nc -q -1 localhost 14035
+ *
+ * $'\001' corresponds to the index_position of loaded sound
+ * model, you can also send $'\002' etc. $'\000' is the kill
+ * signal for the trigger listening thread.
+ *
+ * To enable this file, you must change the src file in this
+ * directory's Android.mk file, then change the sound_trigger
+ * product package in the device-specific device.mk file,
+ * for example, in device/htc/flounder
+ */
+
 #define LOG_TAG "sound_trigger_hw_default"
 /*#define LOG_NDEBUG 0*/
 
 #include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <arpa/inet.h>
+#include <sys/types.h>
+#include <netinet/in.h>
+#include <sys/socket.h>
+
+#include <errno.h>
 #include <pthread.h>
 #include <sys/prctl.h>
 #include <cutils/log.h>
@@ -26,12 +53,16 @@
 #include <system/sound_trigger.h>
 #include <hardware/sound_trigger.h>
 
+/* Although max_sound_models is specified in sound_trigger_properties, having a maximum maximum
+allows a dramatic simplification of data structures in this file */
+#define MAX_MAX_SOUND_MODELS 5
+
 static const struct sound_trigger_properties hw_properties = {
         "The Android Open Source Project", // implementor
         "Sound Trigger stub HAL", // description
         1, // version
         { 0xed7a7d60, 0xc65e, 0x11e3, 0x9be4, { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } }, // uuid
-        1, // max_sound_models
+        2, // max_sound_models
         1, // max_key_phrases
         1, // max_users
         RECOGNITION_MODE_VOICE_TRIGGER, // recognition_modes
@@ -42,68 +73,207 @@
         0 // power_consumption_mw
 };
 
-struct stub_sound_trigger_device {
-    struct sound_trigger_hw_device device;
-    sound_model_handle_t model_handle;
-    recognition_callback_t recognition_callback;
-    void *recognition_cookie;
+struct recognition_context {
+      /* Sound Model Information Modified On Load */
+    sound_model_handle_t loaded_sound_model;
     sound_model_callback_t sound_model_callback;
     void *sound_model_cookie;
-    pthread_t callback_thread;
+
+    /* Sound Model Information Modified On Recognition Start */
+    struct sound_trigger_recognition_config *config;
+    recognition_callback_t recognition_callback;
+    void *recognition_cookie;
+}
+
+struct stub_sound_trigger_device {
+    struct sound_trigger_hw_device device;
     pthread_mutex_t lock;
-    pthread_cond_t  cond;
+    pthread_t callback_thread;
+
+    struct recognition_context re_context[MAX_MAX_SOUND_MODELS];
+
+    int next_sound_model_id;
 };
 
+/* Will reuse ids when overflow occurs */
+static unsigned int generate_sound_model_id(const struct sound_trigger_hw_device *dev) {
+    struct stub_sound_trigger_device *stdev = (struct stub_sound_trigger_device *)dev;
+    int new_id = stdev->next_sound_model_id;
+    ++stdev->next_sound_model_id;
+    if (stdev->next_sound_model_id == 0) {
+        stdev->next_sound_model_id = 1;
+    }
+    return new_id;
+}
 
-static void *callback_thread_loop(void *context)
-{
+static char *sound_trigger_event_alloc(struct stub_sound_trigger_device *stdev,
+                                       sound_model_handle_t handle) {
+    struct sound_trigger_phrase_recognition_event *event;
+    char *data;
+    data = (char *)calloc(1, sizeof(struct sound_trigger_phrase_recognition_event));
+    if (!data)
+        return NULL;
+
+    unsigned int model_index;
+    bool found = false;
+    for(model_index = 0; model_index < hw_properties.max_sound_models; model_index++) {
+        if (stdev->re_context[model_index]->loaded_sound_model == handle) {
+            found = true;
+            break;
+        }
+    }
+    if (found == false) {
+        ALOGW("Can't find model");
+        return NULL;
+    }
+
+    event = (struct sound_trigger_phrase_recognition_event *)data;
+    event->common.status = RECOGNITION_STATUS_SUCCESS;
+    event->common.type = SOUND_MODEL_TYPE_KEYPHRASE;
+    event->common.model = handle;
+
+    if (stdev->re_context[model_index]->config) {
+        unsigned int i;
+
+        event->num_phrases = stdev->re_context[model_index]->config->num_phrases;
+        if (event->num_phrases > SOUND_TRIGGER_MAX_PHRASES)
+            event->num_phrases = SOUND_TRIGGER_MAX_PHRASES;
+        for (i=0; i < event->num_phrases; i++)
+            memcpy(&event->phrase_extras[i], &stdev->re_context[model_index]->config->phrases[i],
+                   sizeof(struct sound_trigger_phrase_recognition_extra));
+    }
+
+    event->num_phrases = 1;
+    event->phrase_extras[0].confidence_level = 100;
+    event->phrase_extras[0].num_levels = 1;
+    event->phrase_extras[0].levels[0].level = 100;
+    event->phrase_extras[0].levels[0].user_id = 0;
+    // Signify that all the data is comming through streaming, not through the buffer.
+    event->common.capture_available = true;
+
+    event->common.audio_config = AUDIO_CONFIG_INITIALIZER;
+    event->common.audio_config.sample_rate = 16000;
+    event->common.audio_config.channel_mask = AUDIO_CHANNEL_IN_MONO;
+    event->common.audio_config.format = AUDIO_FORMAT_PCM_16_BIT;
+
+    return data;
+}
+
+static void *callback_thread_loop(void *context) {
     struct stub_sound_trigger_device *stdev = (struct stub_sound_trigger_device *)context;
-    ALOGI("%s", __func__);
+    struct sockaddr_in incoming_info;
+    struct sockaddr_in self_info;
+    int self_socket;
+    socklen_t sock_size = sizeof(struct sockaddr_in);
+    memset(&self_info, 0, sizeof(self_info));
+    self_info.sin_family = AF_INET;
+    self_info.sin_addr.s_addr = htonl(INADDR_ANY);
+    self_info.sin_port = htons(14035);
 
-    prctl(PR_SET_NAME, (unsigned long)"sound trigger callback", 0, 0, 0);
+    bool exit = false;
+    while(!exit) {
+        int received_count;
+        int requested_count = 1;
+        char buffer[requested_count];
+        ALOGE("Opening socket");
+        self_socket = socket(AF_INET, SOCK_STREAM, 0);
+        if (self_socket < 0) {
+            ALOGE("Error on socket creation");
+            exit = true;
+        } else {
+            ALOGI("Socket created");
+        }
 
-    pthread_mutex_lock(&stdev->lock);
-    if (stdev->recognition_callback == NULL) {
-        goto exit;
+        int reuse = 1;
+        if (setsockopt(self_socket, SOL_SOCKET, SO_REUSEADDR, (const char*)&reuse, sizeof(reuse)) < 0) {
+            ALOGE("setsockopt(SO_REUSEADDR) failed");
+        }
+
+        int bind_result = bind(self_socket, (struct sockaddr *)&self_info, sizeof(struct sockaddr));
+        if (bind_result < 0) {
+            ALOGE("Error on bind");
+            exit = true;
+        }
+
+        int listen_result = listen(self_socket, 1);
+        if (listen_result < 0) {
+            ALOGE("Error on Listen");
+            exit = true;
+        }
+
+        while(!exit) {
+            int con_socket = accept(self_socket, (struct sockaddr *)&incoming_info, &sock_size);
+            if (!con_socket) {
+                ALOGE("Lost socket, cannot send trigger");
+                break;
+            }
+            ALOGI("Connection from %s", inet_ntoa(incoming_info.sin_addr));
+            received_count = recv(con_socket, buffer, requested_count, 0);
+            unsigned int index = buffer[0] - 1;
+            ALOGI("Received data");
+            pthread_mutex_lock(&stdev->lock);
+            if (received_count > 0) {
+                if (buffer[0] == 0) {
+                    ALOGI("Received kill signal: stop listening to incoming server messages");
+                    exit = true;
+                } else if (index < hw_properties.max_sound_models) {
+                    ALOGI("Going to send trigger for model #%d", index );
+                    if (stdev->re_context[index]->recognition_callback != NULL) {
+                        sound_model_handle_t handle = stdev->re_context[index]->loaded_sound_model;
+                        if (handle == 0) {
+                            ALOGW("This trigger is not loaded");
+                        } else {
+                            struct sound_trigger_phrase_recognition_event *event;
+                            event = (struct sound_trigger_phrase_recognition_event *)
+                                   sound_trigger_event_alloc(stdev, handle);
+                            if (event) {
+                               ALOGI("%s send callback model %d", __func__, index);
+                               stdev->re_context[index]->recognition_callback(&event->common,
+                                                     stdev->re_context[index]->recognition_cookie);
+                               free(event);
+                               stdev->re_context[index]->recognition_callback = NULL;
+                            }
+                            exit = true;
+                        }
+                    } else {
+                       ALOGI("%s No matching callback for %d", __func__, index);
+                    }
+                } else {
+                    ALOGI("Data is not recognized: %d", index);
+                }
+            } else {
+                ALOGI("Received sata is size 0");
+            }
+            pthread_mutex_unlock(&stdev->lock);
+            close(con_socket);
+        }
+        ALOGE("Closing socket");
+        close(self_socket);
     }
-    struct timespec ts;
-    clock_gettime(CLOCK_REALTIME, &ts);
-    ts.tv_sec += 3;
-    ALOGI("%s wait 3 sec", __func__);
-    int rc = pthread_cond_timedwait(&stdev->cond, &stdev->lock, &ts);
-    if (rc == ETIMEDOUT && stdev->recognition_callback != NULL) {
-        char *data = (char *)calloc(1, sizeof(struct sound_trigger_phrase_recognition_event) + 1);
-        struct sound_trigger_phrase_recognition_event *event =
-                (struct sound_trigger_phrase_recognition_event *)data;
-        event->common.status = RECOGNITION_STATUS_SUCCESS;
-        event->common.type = SOUND_MODEL_TYPE_KEYPHRASE;
-        event->common.model = stdev->model_handle;
-        event->num_phrases = 1;
-        event->phrase_extras[0].recognition_modes = RECOGNITION_MODE_VOICE_TRIGGER;
-        event->phrase_extras[0].confidence_level = 100;
-        event->phrase_extras[0].num_levels = 1;
-        event->phrase_extras[0].levels[0].level = 100;
-        event->phrase_extras[0].levels[0].user_id = 0;
-        event->common.data_offset = sizeof(struct sound_trigger_phrase_recognition_event);
-        event->common.data_size = 1;
-        data[event->common.data_offset] = 8;
-        ALOGI("%s send callback model %d", __func__, stdev->model_handle);
-        stdev->recognition_callback(&event->common, stdev->recognition_cookie);
-        free(data);
-    } else {
-        ALOGI("%s abort recognition model %d", __func__, stdev->model_handle);
-    }
-    stdev->recognition_callback = NULL;
-
-exit:
-    pthread_mutex_unlock(&stdev->lock);
 
     return NULL;
 }
 
+static void send_loop_kill_signal() {
+    ALOGI("Sending loop thread kill signal");
+    int self_socket = socket(AF_INET, SOCK_STREAM, 0);
+    struct sockaddr_in remote_info;
+    memset(&remote_info, 0, sizeof(remote_info));
+    remote_info.sin_family = AF_INET;
+    remote_info.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
+    remote_info.sin_port = htons(14035);
+    if (connect(self_socket, (struct sockaddr *)&remote_info, sizeof(struct sockaddr)) == 0) {
+        char msg[] = {0};
+        send(self_socket, msg, 1, 0);
+    } else {
+        ALOGI("Could not connect");
+    }
+    close(self_socket);
+    ALOGI("Sent loop thread kill signal");
+}
+
 static int stdev_get_properties(const struct sound_trigger_hw_device *dev,
-                                struct sound_trigger_properties *properties)
-{
+                                struct sound_trigger_properties *properties) {
     struct stub_sound_trigger_device *stdev = (struct stub_sound_trigger_device *)dev;
 
     ALOGI("%s", __func__);
@@ -117,140 +287,177 @@
                                   struct sound_trigger_sound_model *sound_model,
                                   sound_model_callback_t callback,
                                   void *cookie,
-                                  sound_model_handle_t *handle)
-{
+                                  sound_model_handle_t *handle) {
     struct stub_sound_trigger_device *stdev = (struct stub_sound_trigger_device *)dev;
     int status = 0;
 
     ALOGI("%s stdev %p", __func__, stdev);
     pthread_mutex_lock(&stdev->lock);
+
     if (handle == NULL || sound_model == NULL) {
-        status = -EINVAL;
-        goto exit;
+        pthread_mutex_unlock(&stdev->lock);
+        return -EINVAL;
     }
     if (sound_model->data_size == 0 ||
             sound_model->data_offset < sizeof(struct sound_trigger_sound_model)) {
-        status = -EINVAL;
-        goto exit;
+        pthread_mutex_unlock(&stdev->lock);
+        return -EINVAL;
     }
 
-    if (stdev->model_handle == 1) {
-        status = -ENOSYS;
-        goto exit;
+    /* Find if there is space for this sound model */
+    unsigned int model_index;
+    bool found = false;
+    for(model_index = 0; model_index < hw_properties.max_sound_models; model_index++) {
+        if (stdev->re_context[model_index]->loaded_sound_model == 0) {
+            found = true;
+            break;
+        }
     }
+    if (found == false) {
+        ALOGW("Can't load model: reached max sound model limit");
+        pthread_mutex_unlock(&stdev->lock);
+        return -ENOSYS;
+    }
+
+    stdev->re_context[model_index]->loaded_sound_model = generate_sound_model_id(dev);
+    *handle = stdev->re_context[model_index]->loaded_sound_model;
+
     char *data = (char *)sound_model + sound_model->data_offset;
     ALOGI("%s data size %d data %d - %d", __func__,
           sound_model->data_size, data[0], data[sound_model->data_size - 1]);
-    stdev->model_handle = 1;
-    stdev->sound_model_callback = callback;
-    stdev->sound_model_cookie = cookie;
+    stdev->re_context[model_index]->sound_model_callback = callback;
+    stdev->re_context[model_index]->sound_model_cookie = cookie;
 
-    *handle = stdev->model_handle;
-
-exit:
     pthread_mutex_unlock(&stdev->lock);
     return status;
 }
 
 static int stdev_unload_sound_model(const struct sound_trigger_hw_device *dev,
-                                    sound_model_handle_t handle)
-{
+                                    sound_model_handle_t handle) {
     struct stub_sound_trigger_device *stdev = (struct stub_sound_trigger_device *)dev;
     int status = 0;
-
-    ALOGI("%s handle %d", __func__, handle);
+    ALOGI("unload_sound_model");
     pthread_mutex_lock(&stdev->lock);
-    if (handle != 1) {
-        status = -EINVAL;
-        goto exit;
+
+    unsigned int i;
+    unsigned int model_index;
+    bool found = false;
+    bool other_callbacks_found = false;
+    for(i = 0; i < hw_properties.max_sound_models; i++) {
+        if (stdev->re_context[i]->loaded_sound_model == handle) {
+            found = true;
+            model_index = i;
+            break;
+        } else if (stdev->re_context[i]->recognition_callback != NULL) {
+            other_callbacks_found = true;
+        }
     }
-    if (stdev->model_handle == 0) {
-        status = -ENOSYS;
-        goto exit;
-    }
-    stdev->model_handle = 0;
-    if (stdev->recognition_callback != NULL) {
-        stdev->recognition_callback = NULL;
-        pthread_cond_signal(&stdev->cond);
+    if (found == false) {
+        ALOGW("Can't sound model %d in registered list", handle);
         pthread_mutex_unlock(&stdev->lock);
-        pthread_join(stdev->callback_thread, (void **) NULL);
-        pthread_mutex_lock(&stdev->lock);
+        return -ENOSYS;
     }
 
-exit:
-    pthread_mutex_unlock(&stdev->lock);
+    stdev->re_context[i]->loaded_sound_model = 0;
+    stdev->re_context[i]->sound_model_callback = NULL;
+    stdev->re_context[i]->sound_model_cookie = NULL;
+
+    free(stdev->re_context[i]->config);
+    stdev->re_context[i]->config = NULL;
+    stdev->re_context[i]->recognition_callback = NULL;
+    stdev->re_context[i]->recognition_cookie = NULL;
+
+    /* If no more models running with callbacks, stop trigger thread */
+    if (!other_callbacks_found) {
+        send_loop_kill_signal();
+        pthread_mutex_unlock(&stdev->lock);
+        pthread_join(stdev->callback_thread, (void **)NULL);
+    } else {
+        pthread_mutex_unlock(&stdev->lock);
+    }
+
     return status;
 }
 
 static int stdev_start_recognition(const struct sound_trigger_hw_device *dev,
-                                   sound_model_handle_t sound_model_handle,
+                                   sound_model_handle_t handle,
                                    const struct sound_trigger_recognition_config *config,
                                    recognition_callback_t callback,
-                                   void *cookie)
-{
+                                   void *cookie) {
+    ALOGI("%s", __func__);
     struct stub_sound_trigger_device *stdev = (struct stub_sound_trigger_device *)dev;
-    int status = 0;
-    ALOGI("%s sound model %d", __func__, sound_model_handle);
     pthread_mutex_lock(&stdev->lock);
-    if (stdev->model_handle != sound_model_handle) {
-        status = -ENOSYS;
-        goto exit;
+    unsigned int i;
+    bool found = false;
+    for(i = 0; i < hw_properties.max_sound_models; i++) {
+        if (stdev->re_context[i]->loaded_sound_model == handle) {
+            found = true;
+            break;
+        }
     }
-    if (stdev->recognition_callback != NULL) {
-        status = -ENOSYS;
-        goto exit;
-    }
-    if (config->data_size != 0) {
-        char *data = (char *)config + config->data_offset;
-        ALOGI("%s data size %d data %d - %d", __func__,
-              config->data_size, data[0], data[config->data_size - 1]);
+    if (found == false) {
+        ALOGW("Can't sound model %d in registered list", handle);
+        pthread_mutex_unlock(&stdev->lock);
+        return -ENOSYS;
     }
 
-    stdev->recognition_callback = callback;
-    stdev->recognition_cookie = cookie;
+    free(stdev->re_context[i]->config);
+    stdev->re_context[i]->config = NULL;
+    if (config) {
+        stdev->re_context[i]->config = malloc(sizeof(*config));
+        if (!stdev->re_context[i]->config) {
+            pthread_mutex_unlock(&stdev->lock);
+            return -ENOMEM;
+        }
+        memcpy(stdev->re_context[i]->config, config, sizeof(*config));
+    }
+    stdev->re_context[i]->recognition_callback = callback;
+    stdev->re_context[i]->recognition_cookie = cookie;
+
     pthread_create(&stdev->callback_thread, (const pthread_attr_t *) NULL,
                         callback_thread_loop, stdev);
-exit:
     pthread_mutex_unlock(&stdev->lock);
-    return status;
+    return 0;
 }
 
 static int stdev_stop_recognition(const struct sound_trigger_hw_device *dev,
-                                 sound_model_handle_t sound_model_handle)
-{
+                                 sound_model_handle_t handle) {
     struct stub_sound_trigger_device *stdev = (struct stub_sound_trigger_device *)dev;
-    int status = 0;
-    ALOGI("%s sound model %d", __func__, sound_model_handle);
+    ALOGI("%s", __func__);
     pthread_mutex_lock(&stdev->lock);
-    if (stdev->model_handle != sound_model_handle) {
-        status = -ENOSYS;
-        goto exit;
+
+    unsigned int i;
+    bool found = false;
+    for(i = 0; i < hw_properties.max_sound_models; i++) {
+        if (stdev->re_context[i]->loaded_sound_model == handle) {
+            found = true;
+            break;
+        }
     }
-    if (stdev->recognition_callback == NULL) {
-        status = -ENOSYS;
-        goto exit;
+    if (found == false) {
+        ALOGW("Can't sound model %d in registered list", handle);
+        pthread_mutex_unlock(&stdev->lock);
+        return -ENOSYS;
     }
-    stdev->recognition_callback = NULL;
-    pthread_cond_signal(&stdev->cond);
+
+    free(stdev->re_context[i]->config);
+    stdev->re_context[i]->config = NULL;
+    stdev->re_context[i]->recognition_callback = NULL;
+    stdev->re_context[i]->recognition_cookie = NULL;
+
+    send_loop_kill_signal();
     pthread_mutex_unlock(&stdev->lock);
     pthread_join(stdev->callback_thread, (void **) NULL);
-    pthread_mutex_lock(&stdev->lock);
-
-exit:
-    pthread_mutex_unlock(&stdev->lock);
-    return status;
+    return 0;
 }
 
-
-static int stdev_close(hw_device_t *device)
-{
+static int stdev_close(hw_device_t *device) {
     free(device);
     return 0;
 }
 
 static int stdev_open(const hw_module_t* module, const char* name,
-                     hw_device_t** device)
-{
+                     hw_device_t** device) {
     struct stub_sound_trigger_device *stdev;
     int ret;
 
@@ -261,6 +468,22 @@
     if (!stdev)
         return -ENOMEM;
 
+    if (MAX_MAX_SOUND_MODELS < hw_properties.max_sound_models) {
+        ALOGW("max_sound_models is greater than the allowed %d", MAX_MAX_SOUND_MODELS);
+        return -EINVAL;
+    }
+
+    stdev->next_sound_model_id = 1;
+    unsigned int i;
+    for(i = 0; i < hw_properties.max_sound_models; i++) {
+        stdev->re_context[i]->loaded_sound_model = 0;
+        stdev->re_context[i]->sound_model_callback = NULL;
+        stdev->re_context[i]->sound_model_cookie = NULL;
+        stdev->re_context[i]->config = NULL;
+        stdev->re_context[i]->recognition_callback = NULL;
+        stdev->re_context[i]->recognition_cookie = NULL;
+    }
+
     stdev->device.common.tag = HARDWARE_DEVICE_TAG;
     stdev->device.common.version = SOUND_TRIGGER_DEVICE_API_VERSION_1_0;
     stdev->device.common.module = (struct hw_module_t *) module;
@@ -272,7 +495,6 @@
     stdev->device.stop_recognition = stdev_stop_recognition;
 
     pthread_mutex_init(&stdev->lock, (const pthread_mutexattr_t *) NULL);
-    pthread_cond_init(&stdev->cond, (const pthread_condattr_t *) NULL);
 
     *device = &stdev->device.common;