Merge "Initial interface for MemoryTrackerHAL" into klp-dev
diff --git a/include/hardware/audio.h b/include/hardware/audio.h
index f841980..6ba2544 100644
--- a/include/hardware/audio.h
+++ b/include/hardware/audio.h
@@ -358,10 +358,18 @@
    int (*flush)(struct audio_stream_out* stream);
 
     /**
-     * Return the number of audio frames presented to an external observer.
+     * Return a recent count of the number of audio frames presented to an external observer.
      * This excludes frames which have been written but are still in the pipeline.
      * The count is not reset to zero when output enters standby.
      * Also returns the value of CLOCK_MONOTONIC as of this presentation count.
+     * The returned count is expected to be 'recent',
+     * but does not need to be the most recent possible value.
+     * However, the associated time should correspond to whatever count is returned.
+     * Example:  assume that N+M frames have been presented, where M is a 'small' number.
+     * Then it is permissible to return N instead of N+M,
+     * and the timestamp should correspond to N rather than N+M.
+     * The terms 'recent' and 'small' are not defined.
+     * They reflect the quality of the implementation.
      *
      * 3.0 and higher only.
      */
diff --git a/include/hardware/consumerir.h b/include/hardware/consumerir.h
new file mode 100644
index 0000000..5adf6be
--- /dev/null
+++ b/include/hardware/consumerir.h
@@ -0,0 +1,80 @@
+/*
+ * Copyright (C) 2013 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.
+ */
+
+#ifndef ANDROID_INCLUDE_HARDWARE_CONSUMERIR_H
+#define ANDROID_INCLUDE_HARDWARE_CONSUMERIR_H
+
+#include <stdint.h>
+#include <sys/cdefs.h>
+#include <hardware/hardware.h>
+#include <hardware/hwcomposer_defs.h>
+
+#define CONSUMERIR_MODULE_API_VERSION_1_0 HARDWARE_MODULE_API_VERSION(1, 0)
+#define CONSUMERIR_HARDWARE_MODULE_ID "consumerir"
+#define CONSUMERIR_TRANSMITTER "transmitter"
+
+typedef struct consumerir_freq_range {
+    int min;
+    int max;
+} consumerir_freq_range_t;
+
+typedef struct consumerir_module {
+    struct hw_module_t common;
+} consumerir_module_t;
+
+typedef struct consumerir_device {
+    struct hw_device_t common;
+
+    /*
+     * (*transmit)() is called to by the ConsumerIrService to send an IR pattern
+     * at a given carrier_freq.
+     *
+     * The pattern is alternating series of carrier on and off periods measured in
+     * microseconds.  The carrier should be turned off at the end of a transmit
+     * even if there are and odd number of entries in the pattern array.
+     *
+     * This call should return when the transmit is complete or encounters an error.
+     *
+     * returns: 0 on success. A negative error code on error.
+     */
+    int (*transmit)(struct consumerir_device *dev, int carrier_freq,
+            const int pattern[], int pattern_len);
+
+    /*
+     * (*get_num_carrier_freqs)() is called by the ConsumerIrService to get the
+     * number of carrier freqs to allocate space for, which is then filled by
+     * a subsequent call to (*get_carrier_freqs)().
+     *
+     * returns: the number of ranges on success. A negative error code on error.
+     */
+    int (*get_num_carrier_freqs)(struct consumerir_device *dev);
+
+    /*
+     * (*get_carrier_freqs)() is called by the ConsumerIrService to enumerate
+     * which frequencies the IR transmitter supports.  The HAL implementation
+     * should fill an array of consumerir_freq_range structs with the
+     * appropriate values for the transmitter, up to len elements.
+     *
+     * returns: the number of ranges on success. A negative error code on error.
+     */
+    int (*get_carrier_freqs)(struct consumerir_device *dev,
+            size_t len, consumerir_freq_range_t *ranges);
+
+    /* Reserved for future use. Must be NULL. */
+    void* reserved[8 - 3];
+} consumerir_device_t;
+
+#endif /* ANDROID_INCLUDE_HARDWARE_CONSUMERIR_H */
diff --git a/modules/Android.mk b/modules/Android.mk
index 486b42d..b2d5a2a 100644
--- a/modules/Android.mk
+++ b/modules/Android.mk
@@ -1,2 +1,3 @@
-hardware_modules := gralloc hwcomposer audio nfc nfc-nci local_time power usbaudio audio_remote_submix camera
+hardware_modules := gralloc hwcomposer audio nfc nfc-nci local_time \
+	power usbaudio audio_remote_submix camera consumerir
 include $(call all-named-subdir-makefiles,$(hardware_modules))
diff --git a/modules/consumerir/Android.mk b/modules/consumerir/Android.mk
new file mode 100644
index 0000000..ed6aa0f
--- /dev/null
+++ b/modules/consumerir/Android.mk
@@ -0,0 +1,25 @@
+# Copyright (C) 2013 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)
+
+include $(CLEAR_VARS)
+
+LOCAL_MODULE := consumerir.default
+LOCAL_MODULE_PATH := $(TARGET_OUT_SHARED_LIBRARIES)/hw
+LOCAL_SRC_FILES := consumerir.c
+LOCAL_SHARED_LIBRARIES := liblog libcutils
+LOCAL_MODULE_TAGS := optional
+
+include $(BUILD_SHARED_LIBRARY)
diff --git a/modules/consumerir/consumerir.c b/modules/consumerir/consumerir.c
new file mode 100644
index 0000000..83eba75
--- /dev/null
+++ b/modules/consumerir/consumerir.c
@@ -0,0 +1,116 @@
+/*
+ * Copyright (C) 2013 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 "ConsumerIrHal"
+
+#include <errno.h>
+#include <string.h>
+#include <cutils/log.h>
+#include <hardware/hardware.h>
+#include <hardware/consumerir.h>
+
+#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
+
+static const consumerir_freq_range_t consumerir_freqs[] = {
+    {.min = 30000, .max = 30000},
+    {.min = 33000, .max = 33000},
+    {.min = 36000, .max = 36000},
+    {.min = 38000, .max = 38000},
+    {.min = 40000, .max = 40000},
+    {.min = 56000, .max = 56000},
+};
+
+static int consumerir_transmit(struct consumerir_device *dev,
+   int carrier_freq, int pattern[], int pattern_len)
+{
+    int total_time = 0;
+    long i;
+
+    for (i = 0; i < pattern_len; i++)
+        total_time += pattern[i];
+
+    /* simulate the time spent transmitting by sleeping */
+    ALOGD("transmit for %d uS at %d Hz", total_time, carrier_freq);
+    usleep(total_time);
+
+    return 0;
+}
+
+static int consumerir_get_num_carrier_freqs(struct consumerir_device *dev)
+{
+    return ARRAY_SIZE(consumerir_freqs);
+}
+
+static int consumerir_get_carrier_freqs(struct consumerir_device *dev,
+    size_t len, consumerir_freq_range_t *ranges)
+{
+    size_t to_copy = ARRAY_SIZE(consumerir_freqs);
+
+    to_copy = len < to_copy ? len : to_copy;
+    memcpy(ranges, consumerir_freqs, to_copy * sizeof(consumerir_freq_range_t));
+    return to_copy;
+}
+
+static int consumerir_close(hw_device_t *dev)
+{
+    free(dev);
+    return 0;
+}
+
+/*
+ * Generic device handling
+ */
+static int consumerir_open(const hw_module_t* module, const char* name,
+        hw_device_t** device)
+{
+    if (strcmp(name, CONSUMERIR_TRANSMITTER) != 0) {
+        return -EINVAL;
+    }
+    if (device == NULL) {
+        ALOGE("NULL device on open");
+        return -EINVAL;
+    }
+
+    consumerir_device_t *dev = malloc(sizeof(consumerir_device_t));
+    memset(dev, 0, sizeof(consumerir_device_t));
+
+    dev->common.tag = HARDWARE_DEVICE_TAG;
+    dev->common.version = 0;
+    dev->common.module = (struct hw_module_t*) module;
+    dev->common.close = consumerir_close;
+
+    dev->transmit = consumerir_transmit;
+    dev->get_num_carrier_freqs = consumerir_get_num_carrier_freqs;
+    dev->get_carrier_freqs = consumerir_get_carrier_freqs;
+
+    *device = (hw_device_t*) dev;
+    return 0;
+}
+
+static struct hw_module_methods_t consumerir_module_methods = {
+    .open = consumerir_open,
+};
+
+consumerir_module_t HAL_MODULE_INFO_SYM = {
+    .common = {
+        .tag                = HARDWARE_MODULE_TAG,
+        .module_api_version = CONSUMERIR_MODULE_API_VERSION_1_0,
+        .hal_api_version    = HARDWARE_HAL_API_VERSION,
+        .id                 = CONSUMERIR_HARDWARE_MODULE_ID,
+        .name               = "Demo IR HAL",
+        .author             = "The Android Open Source Project",
+        .methods            = &consumerir_module_methods,
+    },
+};
diff --git a/tests/camera2/CameraMultiStreamTests.cpp b/tests/camera2/CameraMultiStreamTests.cpp
index f2044f5..9e26066 100644
--- a/tests/camera2/CameraMultiStreamTests.cpp
+++ b/tests/camera2/CameraMultiStreamTests.cpp
@@ -35,6 +35,7 @@
 #define CAMERA_MULTI_STREAM_DEBUGGING  0
 #define CAMERA_FRAME_TIMEOUT    1000000000LL // nsecs (1 secs)
 #define PREVIEW_RENDERING_TIME_INTERVAL 200000 // in unit of us, 200ms
+#define TOLERANCE_MARGIN 0.01 // 1% tolerance margin for exposure sanity check.
 /* constants for display */
 #define DISPLAY_BUFFER_HEIGHT 1024
 #define DISPLAY_BUFFER_WIDTH 1024
@@ -269,64 +270,57 @@
             int previewStreamId,
             int meteringStreamId,
             int captureStreamId) {
-    int32_t requestId = 1;
-    Vector<int32_t> previewStreamIds;
-    previewStreamIds.push(previewStreamId);
-    ASSERT_EQ(OK, mDevice->createDefaultRequest(CAMERA2_TEMPLATE_PREVIEW,
-            &previewRequest));
-    ASSERT_EQ(OK, previewRequest.update(ANDROID_REQUEST_OUTPUT_STREAMS,
-            previewStreamIds));
-    ASSERT_EQ(OK, previewRequest.update(ANDROID_REQUEST_ID,
-            &requestId, 1));
+        int32_t requestId = 0;
+        Vector<int32_t> previewStreamIds;
+        previewStreamIds.push(previewStreamId);
+        ASSERT_EQ(OK, mDevice->createDefaultRequest(CAMERA2_TEMPLATE_PREVIEW,
+                &previewRequest));
+        ASSERT_EQ(OK, previewRequest.update(ANDROID_REQUEST_OUTPUT_STREAMS,
+                previewStreamIds));
+        ASSERT_EQ(OK, previewRequest.update(ANDROID_REQUEST_ID,
+                &requestId, 1));
 
-    // Create metering request, manual settings
-    // Manual control: Disable 3A, noise reduction, edge sharping
-    uint8_t cmOff = static_cast<uint8_t>(ANDROID_CONTROL_MODE_OFF);
-    uint8_t nrOff = static_cast<uint8_t>(ANDROID_NOISE_REDUCTION_MODE_OFF);
-    uint8_t sharpOff = static_cast<uint8_t>(ANDROID_EDGE_MODE_OFF);
-    Vector<int32_t> meteringStreamIds;
-    meteringStreamIds.push(meteringStreamId);
-    ASSERT_EQ(OK, mDevice->createDefaultRequest(
-            CAMERA2_TEMPLATE_PREVIEW,
-            &meteringRequest));
-    ASSERT_EQ(OK, meteringRequest.update(
-            ANDROID_REQUEST_OUTPUT_STREAMS,
-            meteringStreamIds));
-    ASSERT_EQ(OK, meteringRequest.update(
-            ANDROID_REQUEST_ID,
-            &requestId, 1));
-    ASSERT_EQ(OK, meteringRequest.update(
-            ANDROID_CONTROL_MODE,
-            &cmOff, 1));
-    ASSERT_EQ(OK, meteringRequest.update(
-            ANDROID_NOISE_REDUCTION_MODE,
-            &nrOff, 1));
-    ASSERT_EQ(OK, meteringRequest.update(
-            ANDROID_EDGE_MODE,
-            &sharpOff, 1));
+        // Create metering request, manual settings
+        // Manual control: Disable 3A, noise reduction, edge sharping
+        uint8_t cmOff = static_cast<uint8_t>(ANDROID_CONTROL_MODE_OFF);
+        uint8_t nrOff = static_cast<uint8_t>(ANDROID_NOISE_REDUCTION_MODE_OFF);
+        uint8_t sharpOff = static_cast<uint8_t>(ANDROID_EDGE_MODE_OFF);
+        Vector<int32_t> meteringStreamIds;
+        meteringStreamIds.push(meteringStreamId);
+        ASSERT_EQ(OK, mDevice->createDefaultRequest(
+                CAMERA2_TEMPLATE_PREVIEW,
+                &meteringRequest));
+        ASSERT_EQ(OK, meteringRequest.update(
+                ANDROID_REQUEST_OUTPUT_STREAMS,
+                meteringStreamIds));
+        ASSERT_EQ(OK, meteringRequest.update(
+                ANDROID_CONTROL_MODE,
+                &cmOff, 1));
+        ASSERT_EQ(OK, meteringRequest.update(
+                ANDROID_NOISE_REDUCTION_MODE,
+                &nrOff, 1));
+        ASSERT_EQ(OK, meteringRequest.update(
+                ANDROID_EDGE_MODE,
+                &sharpOff, 1));
 
-    // Create capture request, manual settings
-    requestId++;
-    Vector<int32_t> captureStreamIds;
-    captureStreamIds.push(captureStreamId);
-    ASSERT_EQ(OK, mDevice->createDefaultRequest(
-            CAMERA2_TEMPLATE_PREVIEW,
-            &captureRequest));
-    ASSERT_EQ(OK, captureRequest.update(
-            ANDROID_REQUEST_OUTPUT_STREAMS,
-            captureStreamIds));
-    ASSERT_EQ(OK, captureRequest.update(
-            ANDROID_REQUEST_ID,
-            &requestId, 1));
-    ASSERT_EQ(OK, captureRequest.update(
-            ANDROID_CONTROL_MODE,
-            &cmOff, 1));
-    ASSERT_EQ(OK, captureRequest.update(
-            ANDROID_NOISE_REDUCTION_MODE,
-            &nrOff, 1));
-    ASSERT_EQ(OK, captureRequest.update(
-            ANDROID_EDGE_MODE,
-            &sharpOff, 1));
+        // Create capture request, manual settings
+        Vector<int32_t> captureStreamIds;
+        captureStreamIds.push(captureStreamId);
+        ASSERT_EQ(OK, mDevice->createDefaultRequest(
+                CAMERA2_TEMPLATE_PREVIEW,
+                &captureRequest));
+        ASSERT_EQ(OK, captureRequest.update(
+                ANDROID_REQUEST_OUTPUT_STREAMS,
+                captureStreamIds));
+        ASSERT_EQ(OK, captureRequest.update(
+                ANDROID_CONTROL_MODE,
+                &cmOff, 1));
+        ASSERT_EQ(OK, captureRequest.update(
+                ANDROID_NOISE_REDUCTION_MODE,
+                &nrOff, 1));
+        ASSERT_EQ(OK, captureRequest.update(
+                ANDROID_EDGE_MODE,
+                &sharpOff, 1));
     }
 
     sp<CameraStream> CreateStream(
@@ -345,21 +339,23 @@
             const Vector<int64_t>& exposures,
             const Vector<int32_t>& sensitivities,
             const sp<CameraStream>& stream,
-            int64_t minFrameDuration) {
+            int64_t minFrameDuration,
+            int32_t* requestIdStart) {
         ASSERT_EQ(OK, request.update(ANDROID_SENSOR_FRAME_DURATION,
                 &minFrameDuration, 1));
         // Submit a series of requests with the specified exposure/gain values.
+        int32_t targetRequestId = *requestIdStart;
         for (size_t i = 0; i < requestCount; i++) {
-            ASSERT_EQ(OK, request.update(ANDROID_SENSOR_EXPOSURE_TIME,
-                    &exposures[i], 1));
-            ASSERT_EQ(OK, request.update(ANDROID_SENSOR_SENSITIVITY,
-                    &sensitivities[i], 1));
+            ASSERT_EQ(OK, request.update(ANDROID_REQUEST_ID, requestIdStart, 1));
+            ASSERT_EQ(OK, request.update(ANDROID_SENSOR_EXPOSURE_TIME, &exposures[i], 1));
+            ASSERT_EQ(OK, request.update(ANDROID_SENSOR_SENSITIVITY, &sensitivities[i], 1));
             ASSERT_EQ(OK, mDevice->capture(request));
-            ALOGV("Submitting capture %d with exposure %lld, sensitivity %d",
-                    i, exposures[i], sensitivities[i]);
+            ALOGV("Submitting request with: id %d with exposure %lld, sensitivity %d",
+                    *requestIdStart, exposures[i], sensitivities[i]);
             if (CAMERA_MULTI_STREAM_DEBUGGING) {
                 request.dump(STDOUT_FILENO);
             }
+            (*requestIdStart)++;
         }
         // Get capture burst results.
         Vector<nsecs_t> captureBurstTimes;
@@ -379,18 +375,34 @@
                 waitLimit = exposures[i] * EXP_WAIT_MULTIPLIER;
             }
 
-            ASSERT_EQ(OK, mDevice->waitForNextFrame(waitLimit));
             CameraMetadata frameMetadata;
-            ASSERT_EQ(OK, mDevice->getNextFrame(&frameMetadata));
+            int32_t resultRequestId;
+            do {
+                ASSERT_EQ(OK, mDevice->waitForNextFrame(waitLimit));
+                ASSERT_EQ(OK, mDevice->getNextFrame(&frameMetadata));
+
+                camera_metadata_entry_t resultEntry = frameMetadata.find(ANDROID_REQUEST_ID);
+                ASSERT_EQ(1u, resultEntry.count);
+                resultRequestId = resultEntry.data.i32[0];
+                if (CAMERA_MULTI_STREAM_DEBUGGING) {
+                    std::cout << "capture result req id: " << resultRequestId << std::endl;
+                }
+            } while (resultRequestId != targetRequestId);
+            targetRequestId++;
             ALOGV("Got capture burst result for request %d", i);
+
             // Validate capture result
             if (CAMERA_MULTI_STREAM_DEBUGGING) {
                 frameMetadata.dump(STDOUT_FILENO);
             }
 
             // TODO: Need revisit it to figure out an accurate margin.
-            EXPECT_EQ(sensitivities[i], GetSensitivity(frameMetadata));
-            EXPECT_EQ(exposures[i], GetExposureValue(frameMetadata));
+            int64_t resultExposure = GetExposureValue(frameMetadata);
+            int32_t resultSensitivity = GetSensitivity(frameMetadata);
+            EXPECT_LE(sensitivities[i] * (1.0 - TOLERANCE_MARGIN), resultSensitivity);
+            EXPECT_GE(sensitivities[i] * (1.0 + TOLERANCE_MARGIN), resultSensitivity);
+            EXPECT_LE(exposures[i] * (1.0 - TOLERANCE_MARGIN), resultExposure);
+            EXPECT_GE(exposures[i] * (1.0 + TOLERANCE_MARGIN), resultExposure);
 
             ASSERT_EQ(OK, listener->waitForFrame(waitLimit));
             captureBurstTimes.push_back(systemTime());
@@ -625,6 +637,9 @@
         requestCount = exposures.size();
     }
 
+    // To maintain the request id uniqueness (preview request id is 0), make burst capture start
+    // request id 1 here.
+    int32_t requestIdStart = 1;
     /**
      * Submit metering request, set default frame duration to minimal possible
      * value, we want the capture to run as fast as possible. HAL should adjust
@@ -632,7 +647,7 @@
      * exposure value if exposure is larger than frame duration.
      */
     CaptureBurst(meteringRequest, requestCount, exposures, sensitivities,
-            meteringStream, minFrameDuration);
+            meteringStream, minFrameDuration, &requestIdStart);
 
     /**
      * Submit capture request, set default frame duration to minimal possible
@@ -641,7 +656,7 @@
      * exposure value if exposure is larger than frame duration.
      */
     CaptureBurst(captureRequest, requestCount, exposures, sensitivities,
-            captureStream, minFrameDuration);
+            captureStream, minFrameDuration, &requestIdStart);
 
     ASSERT_EQ(OK, mDevice->clearStreamingRequest());
 }