Merge "hwcomposer: Correct padding for 32/64-bit"
diff --git a/include/hardware/sound_trigger.h b/include/hardware/sound_trigger.h
new file mode 100644
index 0000000..fc3ac47
--- /dev/null
+++ b/include/hardware/sound_trigger.h
@@ -0,0 +1,133 @@
+/*
+ * Copyright (C) 2014 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <system/audio.h>
+#include <system/sound_trigger.h>
+#include <hardware/hardware.h>
+
+#ifndef ANDROID_SOUND_TRIGGER_HAL_H
+#define ANDROID_SOUND_TRIGGER_HAL_H
+
+
+__BEGIN_DECLS
+
+/**
+ * The id of this module
+ */
+#define SOUND_TRIGGER_HARDWARE_MODULE_ID "sound_trigger"
+
+/**
+ * Name of the audio devices to open
+ */
+#define SOUND_TRIGGER_HARDWARE_INTERFACE "sound_trigger_hw_if"
+
+#define SOUND_TRIGGER_MODULE_API_VERSION_1_0 HARDWARE_MODULE_API_VERSION(1, 0)
+#define SOUND_TRIGGER_MODULE_API_VERSION_CURRENT SOUND_TRIGGER_MODULE_API_VERSION_1_0
+
+
+#define SOUND_TRIGGER_DEVICE_API_VERSION_1_0 HARDWARE_DEVICE_API_VERSION(1, 0)
+#define SOUND_TRIGGER_DEVICE_API_VERSION_CURRENT SOUND_TRIGGER_DEVICE_API_VERSION_1_0
+
+/**
+ * List of known sound trigger HAL modules. This is the base name of the sound_trigger HAL
+ * library composed of the "sound_trigger." prefix, one of the base names below and
+ * a suffix specific to the device.
+ * e.g: sondtrigger.primary.goldfish.so or sound_trigger.primary.default.so
+ */
+
+#define SOUND_TRIGGER_HARDWARE_MODULE_ID_PRIMARY "primary"
+
+
+/**
+ * Every hardware module must have a data structure named HAL_MODULE_INFO_SYM
+ * and the fields of this data structure must begin with hw_module_t
+ * followed by module specific information.
+ */
+struct sound_trigger_module {
+    struct hw_module_t common;
+};
+
+typedef void (*recognition_callback_t)(struct sound_trigger_recognition_event *event, void *cookie);
+typedef void (*sound_model_callback_t)(struct sound_trigger_model_event *event, void *cookie);
+
+struct sound_trigger_hw_device {
+    struct hw_device_t common;
+
+    /*
+     * Retrieve implementation properties.
+     */
+    int (*get_properties)(const struct sound_trigger_hw_device *dev,
+                          struct sound_trigger_properties *properties);
+
+    /*
+     * Load a sound model. Once loaded, recognition of this model can be started and stopped.
+     * Only one active recognition per model at a time. The SoundTrigger service will handle
+     * concurrent recognition requests by different users/applications on the same model.
+     * The implementation returns a unique handle used by other functions (unload_sound_model(),
+     * start_recognition(), etc...
+     */
+    int (*load_sound_model)(const struct sound_trigger_hw_device *dev,
+                            struct sound_trigger_sound_model *sound_model,
+                            sound_model_callback_t callback,
+                            void *cookie,
+                            sound_model_handle_t *handle);
+
+    /*
+     * Unload a sound model. A sound model can be unloaded to make room for a new one to overcome
+     * implementation limitations.
+     */
+    int (*unload_sound_model)(const struct sound_trigger_hw_device *dev,
+                              sound_model_handle_t handle);
+
+    /* Start recognition on a given model. Only one recognition active at a time per model.
+     * Once recognition succeeds of fails, the callback is called.
+     * TODO: group recognition configuration parameters into one struct and add key phrase options.
+     */
+    int (*start_recognition)(const struct sound_trigger_hw_device *dev,
+                             sound_model_handle_t sound_model_handle,
+                             audio_io_handle_t capture_handle,
+                             audio_devices_t capture_device,
+                             recognition_callback_t callback,
+                             void *cookie,
+                             unsigned int data_size,
+                             char *data);
+
+    /* Stop recognition on a given model.
+     * The implementation does not have to call the callback when stopped via this method.
+     */
+    int (*stop_recognition)(const struct sound_trigger_hw_device *dev,
+                           sound_model_handle_t sound_model_handle);
+};
+
+typedef struct sound_trigger_hw_device sound_trigger_hw_device_t;
+
+/** convenience API for opening and closing a supported device */
+
+static inline int sound_trigger_hw_device_open(const struct hw_module_t* module,
+                                       struct sound_trigger_hw_device** device)
+{
+    return module->methods->open(module, SOUND_TRIGGER_HARDWARE_INTERFACE,
+                                 (struct hw_device_t**)device);
+}
+
+static inline int sound_trigger_hw_device_close(struct sound_trigger_hw_device* device)
+{
+    return device->common.close(&device->common);
+}
+
+__END_DECLS
+
+#endif  // ANDROID_SOUND_TRIGGER_HAL_H
diff --git a/modules/soundtrigger/Android.mk b/modules/soundtrigger/Android.mk
new file mode 100644
index 0000000..325980c
--- /dev/null
+++ b/modules/soundtrigger/Android.mk
@@ -0,0 +1,27 @@
+# Copyright (C) 2011 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+LOCAL_PATH := $(call my-dir)
+
+# Stub sound_trigger HAL module, used for tests
+include $(CLEAR_VARS)
+
+LOCAL_MODULE := sound_trigger.stub.default
+LOCAL_MODULE_RELATIVE_PATH := hw
+LOCAL_SRC_FILES := sound_trigger_hw.c
+LOCAL_SHARED_LIBRARIES := liblog libcutils
+LOCAL_MODULE_TAGS := optional
+LOCAL_32_BIT_ONLY := true
+
+include $(BUILD_SHARED_LIBRARY)
diff --git a/modules/soundtrigger/sound_trigger_hw.c b/modules/soundtrigger/sound_trigger_hw.c
new file mode 100644
index 0000000..8347d02
--- /dev/null
+++ b/modules/soundtrigger/sound_trigger_hw.c
@@ -0,0 +1,301 @@
+/*
+ * Copyright (C) 2011 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#define LOG_TAG "sound_trigger_hw_default"
+/*#define LOG_NDEBUG 0*/
+
+#include <errno.h>
+#include <pthread.h>
+#include <sys/prctl.h>
+#include <cutils/log.h>
+
+#include <hardware/hardware.h>
+#include <system/sound_trigger.h>
+#include <hardware/sound_trigger.h>
+
+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
+        1, // max_key_phrases
+        1, // max_users
+        RECOGNITION_MODE_VOICE_TRIGGER, // recognition_modes
+        false, // capture_transition
+        0, // max_buffer_ms
+        false, // concurrent_capture
+        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;
+    sound_model_callback_t sound_model_callback;
+    void *sound_model_cookie;
+    pthread_t callback_thread;
+    pthread_mutex_t lock;
+    pthread_cond_t  cond;
+
+};
+
+
+static void *callback_thread_loop(void *context)
+{
+    struct stub_sound_trigger_device *stdev = (struct stub_sound_trigger_device *)context;
+    ALOGI("%s", __func__);
+
+    prctl(PR_SET_NAME, (unsigned long)"sound trigger callback", 0, 0, 0);
+
+    pthread_mutex_lock(&stdev->lock);
+    if (stdev->recognition_callback == NULL) {
+        goto exit;
+    }
+    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->key_phrase_in_capture = false;
+        event->num_phrases = 1;
+        event->phrase_extras[0].recognition_modes = RECOGNITION_MODE_VOICE_TRIGGER;
+        event->phrase_extras[0].num_users = 1;
+        event->phrase_extras[0].confidence_levels[0] = 100;
+        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 int stdev_get_properties(const struct sound_trigger_hw_device *dev,
+                                struct sound_trigger_properties *properties)
+{
+    struct stub_sound_trigger_device *stdev = (struct stub_sound_trigger_device *)dev;
+
+    ALOGI("%s", __func__);
+    if (properties == NULL)
+        return -EINVAL;
+    memcpy(properties, &hw_properties, sizeof(struct sound_trigger_properties));
+    return 0;
+}
+
+static int stdev_load_sound_model(const struct sound_trigger_hw_device *dev,
+                                  struct sound_trigger_sound_model *sound_model,
+                                  sound_model_callback_t callback,
+                                  void *cookie,
+                                  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;
+    }
+    if (sound_model->data_size == 0 ||
+            sound_model->data_offset < sizeof(struct sound_trigger_sound_model)) {
+        status = -EINVAL;
+        goto exit;
+    }
+
+    if (stdev->model_handle == 1) {
+        status = -ENOSYS;
+        goto exit;
+    }
+    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;
+
+    *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)
+{
+    struct stub_sound_trigger_device *stdev = (struct stub_sound_trigger_device *)dev;
+    int status = 0;
+
+    ALOGI("%s handle %d", __func__, handle);
+    pthread_mutex_lock(&stdev->lock);
+    if (handle != 1) {
+        status = -EINVAL;
+        goto exit;
+    }
+    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);
+        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;
+}
+
+static int stdev_start_recognition(const struct sound_trigger_hw_device *dev,
+                                   sound_model_handle_t sound_model_handle,
+                                   audio_io_handle_t capture_handle __unused,
+                                   audio_devices_t capture_device __unused,
+                                   recognition_callback_t callback,
+                                   void *cookie,
+                                   unsigned int data_size,
+                                   char *data)
+{
+    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;
+    }
+    if (stdev->recognition_callback != NULL) {
+        status = -ENOSYS;
+        goto exit;
+    }
+    if (data_size != 0 && data == NULL) {
+        status = -EINVAL;
+        goto exit;
+    }
+    if (data_size != 0) {
+        ALOGI("%s data size %d data %d - %d", __func__,
+              data_size, data[0], data[data_size - 1]);
+    }
+
+    stdev->recognition_callback = callback;
+    stdev->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;
+}
+
+static int stdev_stop_recognition(const struct sound_trigger_hw_device *dev,
+                                 sound_model_handle_t sound_model_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);
+    pthread_mutex_lock(&stdev->lock);
+    if (stdev->model_handle != sound_model_handle) {
+        status = -ENOSYS;
+        goto exit;
+    }
+    if (stdev->recognition_callback == NULL) {
+        status = -ENOSYS;
+        goto exit;
+    }
+    stdev->recognition_callback = NULL;
+    pthread_cond_signal(&stdev->cond);
+    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;
+}
+
+
+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)
+{
+    struct stub_sound_trigger_device *stdev;
+    int ret;
+
+    if (strcmp(name, SOUND_TRIGGER_HARDWARE_INTERFACE) != 0)
+        return -EINVAL;
+
+    stdev = calloc(1, sizeof(struct stub_sound_trigger_device));
+    if (!stdev)
+        return -ENOMEM;
+
+    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;
+    stdev->device.common.close = stdev_close;
+    stdev->device.get_properties = stdev_get_properties;
+    stdev->device.load_sound_model = stdev_load_sound_model;
+    stdev->device.unload_sound_model = stdev_unload_sound_model;
+    stdev->device.start_recognition = stdev_start_recognition;
+    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;
+
+    return 0;
+}
+
+static struct hw_module_methods_t hal_module_methods = {
+    .open = stdev_open,
+};
+
+struct sound_trigger_module HAL_MODULE_INFO_SYM = {
+    .common = {
+        .tag = HARDWARE_MODULE_TAG,
+        .module_api_version = SOUND_TRIGGER_MODULE_API_VERSION_1_0,
+        .hal_api_version = HARDWARE_HAL_API_VERSION,
+        .id = SOUND_TRIGGER_HARDWARE_MODULE_ID,
+        .name = "Default sound trigger HAL",
+        .author = "The Android Open Source Project",
+        .methods = &hal_module_methods,
+    },
+};