am 1ee5f5ef: Merge "BLE peripheral mode (1/4): change HAL to support service data and service uuids." into klp-modular-dev

* commit '1ee5f5ef5a3d75bebcc9cd78fd9150c68f5485fb':
  BLE peripheral mode (1/4): change HAL to support service data and service uuids.
diff --git a/include/hardware/audio_effect.h b/include/hardware/audio_effect.h
index b49d02d..ee48e4c 100644
--- a/include/hardware/audio_effect.h
+++ b/include/hardware/audio_effect.h
@@ -815,7 +815,7 @@
     uint32_t   samplingRate;    // sampling rate
     uint32_t   channels;        // channel mask (see audio_channel_mask_t in audio.h)
     buffer_provider_t bufferProvider;   // buffer provider
-    uint8_t    format;          // Audio format  (see see audio_format_t in audio.h)
+    uint8_t    format;          // Audio format (see audio_format_t in audio.h)
     uint8_t    accessMode;      // read/write or accumulate in buffer (effect_buffer_access_e)
     uint16_t   mask;            // indicates which of the above fields is valid
 } buffer_config_t;
diff --git a/include/hardware/camera3.h b/include/hardware/camera3.h
index afc9d9f..6009706 100644
--- a/include/hardware/camera3.h
+++ b/include/hardware/camera3.h
@@ -1534,6 +1534,16 @@
      */
     CAMERA3_TEMPLATE_ZERO_SHUTTER_LAG = 5,
 
+    /**
+     * A basic template for direct application control of capture
+     * parameters. All automatic control is disabled (auto-exposure, auto-white
+     * balance, auto-focus), and post-processing parameters are set to preview
+     * quality. The manual capture parameters (exposure, sensitivity, etc.)
+     * are set to reasonable defaults, but should be overridden by the
+     * application depending on the intended use case.
+     */
+    CAMERA3_TEMPLATE_MANUAL = 6,
+
     /* Total number of templates */
     CAMERA3_TEMPLATE_COUNT,
 
diff --git a/include/hardware/gps.h b/include/hardware/gps.h
index 458b5b4..4167793 100644
--- a/include/hardware/gps.h
+++ b/include/hardware/gps.h
@@ -221,6 +221,11 @@
 #define AGPS_INTERFACE      "agps"
 
 /**
+ * Name of the Supl Certificate interface.
+ */
+#define SUPL_CERTIFICATE_INTERFACE  "supl-certificate"
+
+/**
  * Name for NI interface
  */
 #define GPS_NI_INTERFACE "gps-ni"
@@ -507,7 +512,7 @@
      */
     void  (*init)( AGpsCallbacks* callbacks );
     /**
-     * Notifies that a data connection is available and sets 
+     * Notifies that a data connection is available and sets
      * the name of the APN to be used for SUPL.
      */
     int  (*data_conn_open)( const char* apn );
@@ -516,7 +521,7 @@
      */
     int  (*data_conn_closed)();
     /**
-     * Notifies that a data connection is not available for AGPS. 
+     * Notifies that a data connection is not available for AGPS.
      */
     int  (*data_conn_failed)();
     /**
@@ -525,6 +530,72 @@
     int  (*set_server)( AGpsType type, const char* hostname, int port );
 } AGpsInterface;
 
+/** Error codes associated with certificate operations */
+#define AGPS_CERTIFICATE_OPERATION_SUCCESS               0
+#define AGPS_CERTIFICATE_ERROR_GENERIC                -100
+#define AGPS_CERTIFICATE_ERROR_TOO_MANY_CERTIFICATES  -101
+
+/** A data structure that represents an X.509 certificate using DER encoding */
+typedef struct {
+    size_t  length;
+    u_char* data;
+} DerEncodedCertificate;
+
+/**
+ * A type definition for SHA1 Fingerprints used to identify X.509 Certificates
+ * The Fingerprint is a digest of the DER Certificate that uniquely identifies it.
+ */
+typedef struct {
+    u_char data[20];
+} Sha1CertificateFingerprint;
+
+/** AGPS Inteface to handle SUPL certificate operations */
+typedef struct {
+    /** set to sizeof(SuplCertificateInterface) */
+    size_t size;
+
+    /**
+     * Installs a set of Certificates used for SUPL connections to the AGPS server.
+     * If needed the HAL should find out internally any certificates that need to be removed to
+     * accommodate the certificates to install.
+     * The certificates installed represent a full set of valid certificates needed to connect to
+     * AGPS SUPL servers.
+     * The list of certificates is required, and all must be available at the same time, when trying
+     * to establish a connection with the AGPS Server.
+     *
+     * Parameters:
+     *      certificates - A pointer to an array of DER encoded certificates that are need to be
+     *                     installed in the HAL.
+     *      length - The number of certificates to install.
+     * Returns:
+     *      AGPS_CERTIFICATE_OPERATION_SUCCESS if the operation is completed successfully
+     *      AGPS_CERTIFICATE_ERROR_TOO_MANY_CERTIFICATES if the HAL cannot store the number of
+     *          certificates attempted to be installed, the state of the certificates stored should
+     *          remain the same as before on this error case.
+     *
+     * IMPORTANT:
+     *      If needed the HAL should find out internally the set of certificates that need to be
+     *      removed to accommodate the certificates to install.
+     */
+    int  (*install_certificates) ( const DerEncodedCertificate* certificates, size_t length );
+
+    /**
+     * Notifies the HAL that a list of certificates used for SUPL connections are revoked. It is
+     * expected that the given set of certificates is removed from the internal store of the HAL.
+     *
+     * Parameters:
+     *      fingerprints - A pointer to an array of SHA1 Fingerprints to identify the set of
+     *                     certificates to revoke.
+     *      length - The number of fingerprints provided.
+     * Returns:
+     *      AGPS_CERTIFICATE_OPERATION_SUCCESS if the operation is completed successfully.
+     *
+     * IMPORTANT:
+     *      If any of the certificates provided (through its fingerprint) is not known by the HAL,
+     *      it should be ignored and continue revoking/deleting the rest of them.
+     */
+    int  (*revoke_certificates) ( const Sha1CertificateFingerprint* fingerprints, size_t length );
+} SuplCertificateInterface;
 
 /** Represents an NI request */
 typedef struct {
diff --git a/include/hardware/hardware.h b/include/hardware/hardware.h
index 416ae39..74f57aa 100644
--- a/include/hardware/hardware.h
+++ b/include/hardware/hardware.h
@@ -144,8 +144,12 @@
     /** module's dso */
     void* dso;
 
+#ifdef __LP64__
+    uint64_t reserved[32-7];
+#else
     /** padding to 128 bytes, reserved for future use */
     uint32_t reserved[32-7];
+#endif
 
 } hw_module_t;
 
@@ -186,7 +190,11 @@
     struct hw_module_t* module;
 
     /** padding reserved for future use */
+#ifdef __LP64__
+    uint64_t reserved[12];
+#else
     uint32_t reserved[12];
+#endif
 
     /** Close this device */
     int (*close)(struct hw_device_t* device);
diff --git a/include/hardware/hwcomposer.h b/include/hardware/hwcomposer.h
index 86479d3..82e7671 100644
--- a/include/hardware/hwcomposer.h
+++ b/include/hardware/hwcomposer.h
@@ -262,7 +262,7 @@
     };
 
     /* Allow for expansion w/o breaking binary compatibility.
-     * Pad layer to 96 bytes, assuming 32-bit pointers.
+     * Pad layer to 96 bytes.
      */
     int32_t reserved[24 - 19];
 
diff --git a/include/hardware/nfc_tag.h b/include/hardware/nfc_tag.h
new file mode 100644
index 0000000..72028f4
--- /dev/null
+++ b/include/hardware/nfc_tag.h
@@ -0,0 +1,83 @@
+/*
+ * 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_NFC_TAG_HAL_INTERFACE_H
+#define ANDROID_NFC_TAG_HAL_INTERFACE_H
+
+#include <stdint.h>
+
+#include <hardware/hardware.h>
+
+__BEGIN_DECLS
+
+/*
+ * HAL for programmable NFC tags.
+ *
+ */
+
+#define NFC_TAG_HARDWARE_MODULE_ID "nfc_tag"
+#define NFC_TAG_ID "tag"
+
+typedef struct nfc_tag_module_t {
+    struct hw_module_t common;
+} nfc_tag_module_t;
+
+typedef struct nfc_tag_device {
+    struct hw_device_t common;
+
+    /**
+     * Initialize the NFC tag.
+     *
+     * The driver must:
+     *   * Set the static lock bytes to read only
+     *   * Configure the Capability Container to disable write acess
+     *         eg: 0xE1 0x10 <size> 0x0F
+     *
+     * This function is called once before any calls to setContent().
+     *
+     * Return 0 on success or -errno on error.
+     */
+    int (*init)(const struct nfc_tag_device *dev);
+
+    /**
+     * Set the NFC tag content.
+     *
+     * The driver must write <data> in the data area of the tag starting at
+     * byte 0 of block 4 and zero the rest of the data area.
+     *
+     * Returns 0 on success or -errno on error.
+     */
+    int (*setContent)(const struct nfc_tag_device *dev, const uint8_t *data, size_t len);
+
+    /**
+     * Returns the memory size of the data area.
+     */
+    int (*getMemorySize)(const struct nfc_tag_device *dev);
+} nfc_tag_device_t;
+
+static inline int nfc_tag_open(const struct hw_module_t* module,
+                               nfc_tag_device_t** dev) {
+    return module->methods->open(module, NFC_TAG_ID,
+                                 (struct hw_device_t**)dev);
+}
+
+static inline int nfc_tag_close(nfc_tag_device_t* dev) {
+    return dev->common.close(&dev->common);
+}
+
+__END_DECLS
+
+#endif // ANDROID_NFC_TAG_HAL_INTERFACE_H
diff --git a/include/hardware/sensors.h b/include/hardware/sensors.h
index 4c13848..2abca72 100644
--- a/include/hardware/sensors.h
+++ b/include/hardware/sensors.h
@@ -35,6 +35,12 @@
 #define SENSORS_DEVICE_API_VERSION_1_1  HARDWARE_DEVICE_API_VERSION_2(1, 1, SENSORS_HEADER_VERSION)
 
 /**
+ * Please see the Sensors section of source.android.com for an
+ * introduction to and detailed descriptions of Android sensor types:
+ * http://source.android.com/devices/sensors/index.html
+ */
+
+/**
  * The id of this module
  */
 #define SENSORS_HARDWARE_MODULE_ID "sensors"
@@ -74,74 +80,14 @@
     META_DATA_VERSION   /* always last, leave auto-assigned */
 };
 
-/**
- * Definition of the axis used by the sensor HAL API
- *
- * This API is relative to the screen of the device in its default orientation,
- * that is, if the device can be used in portrait or landscape, this API
- * is only relative to the NATURAL orientation of the screen. In other words,
- * the axis are not swapped when the device's screen orientation changes.
- * Higher level services /may/ perform this transformation.
- *
- *   x<0         x>0
- *                ^
- *                |
- *    +-----------+-->  y>0
- *    |           |
- *    |           |
- *    |           |
- *    |           |   / z<0
- *    |           |  /
- *    |           | /
- *    O-----------+/
- *    |[]  [ ]  []/
- *    +----------/+     y<0
- *              /
- *             /
- *           |/ z>0 (toward the sky)
- *
- *    O: Origin (x=0,y=0,z=0)
- *
- */
-
-/*
- * Interaction with suspend mode
- *
- * Unless otherwise noted, an enabled sensor shall not prevent the
- * SoC to go into suspend mode. It is the responsibility of applications
- * to keep a partial wake-lock should they wish to receive sensor
- * events while the screen is off. While in suspend mode, and unless
- * otherwise noted (batch mode, sensor particularities, ...), enabled sensors'
- * events are lost.
- *
- * Note that conceptually, the sensor itself is not de-activated while in
- * suspend mode -- it's just that the data it returns are lost. As soon as
- * the SoC gets out of suspend mode, operations resume as usual. Of course,
- * in practice sensors shall be disabled while in suspend mode to
- * save power, unless batch mode is active, in which case they must
- * continue fill their internal FIFO (see the documentation of batch() to
- * learn how suspend interacts with batch mode).
- *
- * In batch mode, and only when the flag SENSORS_BATCH_WAKE_UPON_FIFO_FULL is
- * set and supported, the specified sensor must be able to wake-up the SoC and
- * be able to buffer at least 10 seconds worth of the requested sensor events.
- *
- * There are notable exceptions to this behavior, which are sensor-dependent
- * (see sensor types definitions below)
- *
- *
- * The sensor type documentation below specifies the wake-up behavior of
- * each sensor:
- *   wake-up: yes     this sensor must wake-up the SoC to deliver events
- *   wake-up: no      this sensor shall not wake-up the SoC, events are dropped
- *
- */
-
 /*
  * Sensor type
  *
  * Each sensor has a type which defines what this sensor measures and how
- * measures are reported. All types are defined below.
+ * measures are reported. See the Base sensors and Composite sensors lists
+ * for complete descriptions:
+ * http://source.android.com/devices/sensors/base_triggers.html
+ * http://source.android.com/devices/sensors/composite_sensors.html
  *
  * Device manufacturers (OEMs) can define their own sensor types, for
  * their private use by applications or services provided by them. Such
@@ -156,47 +102,6 @@
 #define SENSOR_TYPE_DEVICE_PRIVATE_BASE     0x10000
 
 /*
- * Sensor fusion and virtual sensors
- *
- * Many sensor types are or can be implemented as virtual sensors from
- * physical sensors on the device. For instance the rotation vector sensor,
- * orientation sensor, step-detector, step-counter, etc...
- *
- * From the point of view of this API these virtual sensors MUST appear as
- * real, individual sensors. It is the responsibility of the driver and HAL
- * to make sure this is the case.
- *
- * In particular, all sensors must be able to function concurrently.
- * For example, if defining both an accelerometer and a step counter,
- * then both must be able to work concurrently.
- */
-
-/*
- * Trigger modes
- *
- * Sensors can report events in different ways called trigger modes,
- * each sensor type has one and only one trigger mode associated to it.
- * Currently there are four trigger modes defined:
- *
- * continuous: events are reported at a constant rate defined by setDelay().
- *             eg: accelerometers, gyroscopes.
- * on-change:  events are reported only if the sensor's value has changed.
- *             setDelay() is used to set a lower limit to the reporting
- *             period (minimum time between two events).
- *             The HAL must return an event immediately when an on-change
- *             sensor is activated.
- *             eg: proximity, light sensors
- * one-shot:   upon detection of an event, the sensor deactivates itself and
- *             then sends a single event. Order matters to avoid race
- *             conditions. No other event is sent until the sensor get
- *             reactivated. setDelay() is ignored.
- *             eg: significant motion sensor
- * special:    see details in the sensor type specification below
- *
- */
-
-
-/*
  * SENSOR_TYPE_META_DATA
  * trigger-mode: n/a
  * wake-up sensor: n/a
@@ -237,30 +142,6 @@
  *  All values are in SI units (m/s^2) and measure the acceleration of the
  *  device minus the force of gravity.
  *
- *  Acceleration sensors return sensor events for all 3 axes at a constant
- *  rate defined by setDelay().
- *
- *  x: Acceleration on the x-axis
- *  y: Acceleration on the y-axis
- *  z: Acceleration on the z-axis
- *
- * Note that the readings from the accelerometer include the acceleration
- * due to gravity (which is opposite to the direction of the gravity vector).
- *
- *  Examples:
- *    The norm of <x, y, z>  should be close to 0 when in free fall.
- *
- *    When the device lies flat on a table and is pushed on its left side
- *    toward the right, the x acceleration value is positive.
- *
- *    When the device lies flat on a table, the acceleration value is +9.81,
- *    which correspond to the acceleration of the device (0 m/s^2) minus the
- *    force of gravity (-9.81 m/s^2).
- *
- *    When the device lies flat on a table and is pushed toward the sky, the
- *    acceleration value is greater than +9.81, which correspond to the
- *    acceleration of the device (+A m/s^2) minus the force of
- *    gravity (-9.81 m/s^2).
  */
 #define SENSOR_TYPE_ACCELEROMETER                    (1)
 
@@ -272,12 +153,6 @@
  *  All values are in micro-Tesla (uT) and measure the geomagnetic
  *  field in the X, Y and Z axis.
  *
- *  Returned values include calibration mechanisms such that the vector is
- *  aligned with the magnetic declination and heading of the earth's
- *  geomagnetic field.
- *
- *  Magnetic Field sensors return sensor events for all 3 axes at a constant
- *  rate defined by setDelay().
  */
 #define SENSOR_TYPE_GEOMAGNETIC_FIELD                (2)
 #define SENSOR_TYPE_MAGNETIC_FIELD  SENSOR_TYPE_GEOMAGNETIC_FIELD
@@ -286,39 +161,11 @@
  * SENSOR_TYPE_ORIENTATION
  * trigger-mode: continuous
  * wake-up sensor: no
- * 
+ *
  * All values are angles in degrees.
- * 
+ *
  * Orientation sensors return sensor events for all 3 axes at a constant
  * rate defined by setDelay().
- *
- * azimuth: angle between the magnetic north direction and the Y axis, around 
- *  the Z axis (0<=azimuth<360).
- *      0=North, 90=East, 180=South, 270=West
- * 
- * pitch: Rotation around X axis (-180<=pitch<=180), with positive values when
- *  the z-axis moves toward the y-axis.
- *
- * roll: Rotation around Y axis (-90<=roll<=90), with positive values when
- *  the x-axis moves towards the z-axis.
- *
- * Note: For historical reasons the roll angle is positive in the clockwise
- *  direction (mathematically speaking, it should be positive in the
- *  counter-clockwise direction):
- *
- *                Z
- *                ^
- *  (+roll)  .--> |
- *          /     |
- *         |      |  roll: rotation around Y axis
- *     X <-------(.)
- *                 Y
- *       note that +Y == -roll
- *
- *
- *
- * Note: This definition is different from yaw, pitch and roll used in aviation
- *  where the X axis is along the long side of the plane (tail to nose).
  */
 #define SENSOR_TYPE_ORIENTATION                      (3)
 
@@ -328,17 +175,7 @@
  * wake-up sensor: no
  *
  *  All values are in radians/second and measure the rate of rotation
- *  around the X, Y and Z axis.  The coordinate system is the same as is
- *  used for the acceleration sensor. Rotation is positive in the
- *  counter-clockwise direction (right-hand rule). That is, an observer
- *  looking from some positive location on the x, y or z axis at a device
- *  positioned on the origin would report positive rotation if the device
- *  appeared to be rotating counter clockwise. Note that this is the
- *  standard mathematical definition of positive rotation and does not agree
- *  with the definition of roll given earlier.
- *  The range should at least be 17.45 rad/s (ie: ~1000 deg/s).
- *
- *  automatic gyro-drift compensation is allowed but not required.
+ *  around the X, Y and Z axis.
  */
 #define SENSOR_TYPE_GYROSCOPE                        (4)
 
@@ -368,10 +205,7 @@
  * trigger-mode: on-change
  * wake-up sensor: yes
  *
- * The distance value is measured in centimeters.  Note that some proximity
- * sensors only support a binary "close" or "far" measurement.  In this case,
- * the sensor should report its maxRange value in the "far" state and a value
- * less than maxRange in the "near" state.
+ * The value corresponds to the distance to the nearest object in centimeters.
  */
 #define SENSOR_TYPE_PROXIMITY                        (8)
 
@@ -381,10 +215,7 @@
  * wake-up sensor: no
  *
  * A gravity output indicates the direction of and magnitude of gravity in
- * the devices's coordinates.  On Earth, the magnitude is 9.8 m/s^2.
- * Units are m/s^2.  The coordinate system is the same as is used for the
- * acceleration sensor. When the device is at rest, the output of the
- * gravity sensor should be identical to that of the accelerometer.
+ * the devices's coordinates.
  */
 #define SENSOR_TYPE_GRAVITY                          (9)
 
@@ -395,13 +226,6 @@
  *
  * Indicates the linear acceleration of the device in device coordinates,
  * not including gravity.
- *
- * The output is conceptually:
- *    output of TYPE_ACCELERATION - output of TYPE_GRAVITY
- *
- * Readings on all axes should be close to 0 when device lies on a table.
- * Units are m/s^2.
- * The coordinate system is the same as is used for the acceleration sensor.
  */
 #define SENSOR_TYPE_LINEAR_ACCELERATION             (10)
 
@@ -412,46 +236,7 @@
  * wake-up sensor: no
  *
  * The rotation vector symbolizes the orientation of the device relative to the
- * East-North-Up coordinates frame. It is usually obtained by integration of
- * accelerometer, gyroscope and magnetometer readings.
- *
- * The East-North-Up coordinate system is defined as a direct orthonormal basis
- * where:
- * - X points east and is tangential to the ground.
- * - Y points north and is tangential to the ground.
- * - Z points towards the sky and is perpendicular to the ground.
- *
- * The orientation of the phone is represented by the rotation necessary to
- * align the East-North-Up coordinates with the phone's coordinates. That is,
- * applying the rotation to the world frame (X,Y,Z) would align them with the
- * phone coordinates (x,y,z).
- *
- * The rotation can be seen as rotating the phone by an angle theta around
- * an axis rot_axis to go from the reference (East-North-Up aligned) device
- * orientation to the current device orientation.
- *
- * The rotation is encoded as the 4 (reordered) components of a unit quaternion:
- *   sensors_event_t.data[0] = rot_axis.x*sin(theta/2)
- *   sensors_event_t.data[1] = rot_axis.y*sin(theta/2)
- *   sensors_event_t.data[2] = rot_axis.z*sin(theta/2)
- *   sensors_event_t.data[3] = cos(theta/2)
- * where
- *   - rot_axis.x,y,z are the North-East-Up coordinates of a unit length vector
- *     representing the rotation axis
- *   - theta is the rotation angle
- *
- * The quaternion must be of norm 1 (it is a unit quaternion). Failure to ensure
- * this will cause erratic client behaviour.
- *
- * In addition, this sensor reports an estimated heading accuracy.
- *   sensors_event_t.data[4] = estimated_accuracy (in radians)
- * The heading error must be less than estimated_accuracy 95% of the time
- *
- * This sensor must use a gyroscope and an accelerometer as main orientation
- * change input.
- *
- * This sensor can also include magnetometer input to make up for gyro drift,
- * but it cannot be implemented using only a magnetometer.
+ * East-North-Up coordinates frame.
  */
 #define SENSOR_TYPE_ROTATION_VECTOR                 (11)
 
@@ -481,35 +266,6 @@
  *
  *  Similar to SENSOR_TYPE_MAGNETIC_FIELD, but the hard iron calibration is
  *  reported separately instead of being included in the measurement.
- *  Factory calibration and temperature compensation should still be applied to
- *  the "uncalibrated" measurement.
- *  Separating away the hard iron calibration estimation allows the system to
- *  better recover from bad hard iron estimation.
- *
- *  All values are in micro-Tesla (uT) and measure the ambient magnetic
- *  field in the X, Y and Z axis. Assumptions that the the magnetic field
- *  is due to the Earth's poles should be avoided.
- *
- *  The uncalibrated_magnetic event contains
- *  - 3 fields for uncalibrated measurement: x_uncalib, y_uncalib, z_uncalib.
- *    Each is a component of the measured magnetic field, with soft iron
- *    and temperature compensation applied, but not hard iron calibration.
- *    These values should be continuous (no re-calibration should cause a jump).
- *  - 3 fields for hard iron bias estimates: x_bias, y_bias, z_bias.
- *    Each field is a component of the estimated hard iron calibration.
- *    They represent the offsets to apply to the calibrated readings to obtain
- *    uncalibrated readings (x_uncalib ~= x_calibrated + x_bias)
- *    These values are expected to jump as soon as the estimate of the hard iron
- *    changes, and they should be stable the rest of the time.
- *
- *  If this sensor is present, then the corresponding
- *  SENSOR_TYPE_MAGNETIC_FIELD must be present and both must return the
- *  same sensor_t::name and sensor_t::vendor.
- *
- *  Minimum filtering should be applied to this sensor. In particular, low pass
- *  filters should be avoided.
- *
- * See SENSOR_TYPE_MAGNETIC_FIELD for more information
  */
 #define SENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED     (14)
 
@@ -519,21 +275,7 @@
  * wake-up sensor: no
  *
  *  Similar to SENSOR_TYPE_ROTATION_VECTOR, but not using the geomagnetic
- *  field. Therefore the Y axis doesn't point north, but instead to some other
- *  reference. That reference is allowed to drift by the same order of
- *  magnitude than the gyroscope drift around the Z axis.
- *
- *  This sensor does not report an estimated heading accuracy:
- *    sensors_event_t.data[4] is reserved and should be set to 0
- *
- *  In the ideal case, a phone rotated and returning to the same real-world
- *  orientation should report the same game rotation vector
- *  (without using the earth's geomagnetic field).
- *
- *  This sensor must be based on a gyroscope. It cannot be implemented using
- *  a magnetometer.
- *
- * see SENSOR_TYPE_ROTATION_VECTOR for more details
+ *  field.
  */
 #define SENSOR_TYPE_GAME_ROTATION_VECTOR            (15)
 
@@ -543,44 +285,10 @@
  * wake-up sensor: no
  *
  *  All values are in radians/second and measure the rate of rotation
- *  around the X, Y and Z axis. An estimation of the drift on each axis is
- *  reported as well.
- *
- *  No gyro-drift compensation shall be performed.
- *  Factory calibration and temperature compensation should still be applied
- *  to the rate of rotation (angular speeds).
- *
- *  The coordinate system is the same as is
- *  used for the acceleration sensor. Rotation is positive in the
- *  counter-clockwise direction (right-hand rule). That is, an observer
- *  looking from some positive location on the x, y or z axis at a device
- *  positioned on the origin would report positive rotation if the device
- *  appeared to be rotating counter clockwise. Note that this is the
- *  standard mathematical definition of positive rotation and does not agree
- *  with the definition of roll given earlier.
- *  The range should at least be 17.45 rad/s (ie: ~1000 deg/s).
- *
- *  Content of an uncalibrated_gyro event: (units are rad/sec)
- *   x_uncalib : angular speed (w/o drift compensation) around the X axis
- *   y_uncalib : angular speed (w/o drift compensation) around the Y axis
- *   z_uncalib : angular speed (w/o drift compensation) around the Z axis
- *   x_bias : estimated drift around X axis in rad/s
- *   y_bias : estimated drift around Y axis in rad/s
- *   z_bias : estimated drift around Z axis in rad/s
- *
- *  IMPLEMENTATION NOTES:
- *
- *  If the implementation is not able to estimate the drift, then this
- *  sensor MUST NOT be reported by this HAL. Instead, the regular
- *  SENSOR_TYPE_GYROSCOPE is used without drift compensation.
- *
- *  If this sensor is present, then the corresponding
- *  SENSOR_TYPE_GYROSCOPE must be present and both must return the
- *  same sensor_t::name and sensor_t::vendor.
+ *  around the X, Y and Z axis.
  */
 #define SENSOR_TYPE_GYROSCOPE_UNCALIBRATED          (16)
 
-
 /*
  * SENSOR_TYPE_SIGNIFICANT_MOTION
  * trigger-mode: one-shot
@@ -589,45 +297,6 @@
  * A sensor of this type triggers an event each time significant motion
  * is detected and automatically disables itself.
  * The only allowed value to return is 1.0.
- *
- * A significant motion is a motion that might lead to a change in the user
- * location.
- * Examples of such motions are:
- *   walking, biking, sitting in a moving car, coach or train.
- * Examples of situations that should not trigger significant motion:
- * - phone in pocket and person is not moving
- * - phone is on a table, even if the table shakes a bit due to nearby traffic
- *   or washing machine
- *
- * A note on false positive / false negative / power consumption tradeoff
- *  - The goal of this sensor is to save power.
- *  - Triggering an event when the user is not moving (false positive) is costly
- *    in terms of power, so it should be avoided.
- *  - Not triggering an event when the user is moving (false negative) is
- *    acceptable as long as it is not done repeatedly. If the user has been
- *    walking for 10 seconds, not triggering an event within those 10 seconds
- *    is not acceptable.
- *
- *  IMPORTANT NOTE: this sensor type is very different from other types
- *  in that it must work when the screen is off without the need of
- *  holding a partial wake-lock and MUST allow the SoC to go into suspend.
- *  When significant motion is detected, the sensor must awaken the SoC and
- *  the event be reported.
- *
- *  If a particular hardware cannot support this mode of operation then this
- *  sensor type MUST NOT be reported by the HAL. ie: it is not acceptable
- *  to "emulate" this sensor in the HAL.
- *
- *  The whole point of this sensor type is to save power by keeping the
- *  SoC in suspend mode when the device is at rest.
- *
- *  When the sensor is not activated, it must also be deactivated in the
- *  hardware: it must not wake up the SoC anymore, even in case of
- *  significant motion.
- *
- *  setDelay() has no effect and is ignored.
- *  Once a "significant motion" event is returned, a sensor of this type
- *  must disables itself automatically, as if activate(..., 0) had been called.
  */
 
 #define SENSOR_TYPE_SIGNIFICANT_MOTION              (17)
@@ -639,21 +308,8 @@
  * wake-up sensor: no
  *
  * A sensor of this type triggers an event each time a step is taken
- * by the user. The only allowed value to return is 1.0 and an event is
- * generated for each step. Like with any other event, the timestamp
- * indicates when the event (here the step) occurred, this corresponds to when
- * the foot hit the ground, generating a high variation in acceleration.
- *
- * While this sensor operates, it shall not disrupt any other sensors, in
- * particular, but not limited to, the accelerometer; which might very well
- * be in use as well.
- *
- * This sensor must be low power. That is, if the step detection cannot be
- * done in hardware, this sensor should not be defined. Also, when the
- * step detector is activated and the accelerometer is not, only steps should
- * trigger interrupts (not accelerometer data).
- *
- * setDelay() has no impact on this sensor type
+ * by the user. The only allowed value to return is 1.0 and an event
+ * is generated for each step.
  */
 
 #define SENSOR_TYPE_STEP_DETECTOR                   (18)
@@ -667,46 +323,6 @@
  * A sensor of this type returns the number of steps taken by the user since
  * the last reboot while activated. The value is returned as a uint64_t and is
  * reset to zero only on a system / android reboot.
- *
- * The timestamp of the event is set to the time when the first step
- * for that event was taken.
- * See SENSOR_TYPE_STEP_DETECTOR for the signification of the time of a step.
- *
- *  The minimum size of the hardware's internal counter shall be 16 bits
- *  (this restriction is here to avoid too frequent wake-ups when the
- *  delay is very large).
- *
- *  IMPORTANT NOTE: this sensor type is different from other types
- *  in that it must work when the screen is off without the need of
- *  holding a partial wake-lock and MUST allow the SoC to go into suspend.
- *  Unlike other sensors, while in suspend mode this sensor must stay active,
- *  no events are reported during that time but, steps continue to be
- *  accounted for; an event will be reported as soon as the SoC resumes if
- *  the timeout has expired.
- *
- *    In other words, when the screen is off and the device allowed to
- *    go into suspend mode, we don't want to be woken up, regardless of the
- *    setDelay() value, but the steps shall continue to be counted.
- *
- *    The driver must however ensure that the internal step count never
- *    overflows. It is allowed in this situation to wake the SoC up so the
- *    driver can do the counter maintenance.
- *
- *  While this sensor operates, it shall not disrupt any other sensors, in
- *  particular, but not limited to, the accelerometer; which might very well
- *  be in use as well.
- *
- *  If a particular hardware cannot support these modes of operation then this
- *  sensor type MUST NOT be reported by the HAL. ie: it is not acceptable
- *  to "emulate" this sensor in the HAL.
- *
- * This sensor must be low power. That is, if the step detection cannot be
- * done in hardware, this sensor should not be defined. Also, when the
- * step counter is activated and the accelerometer is not, only steps should
- * trigger interrupts (not accelerometer data).
- *
- *  The whole point of this sensor type is to save power by keeping the
- *  SoC in suspend mode when the device is at rest.
  */
 
 #define SENSOR_TYPE_STEP_COUNTER                    (19)
@@ -718,18 +334,6 @@
  *
  *  Similar to SENSOR_TYPE_ROTATION_VECTOR, but using a magnetometer instead
  *  of using a gyroscope.
- *
- *  This sensor must be based on a magnetometer. It cannot be implemented using
- *  a gyroscope, and gyroscope input cannot be used by this sensor, as the
- *  goal of this sensor is to be low power.
- *  The accelerometer can be (and usually is) used.
- *
- *  Just like SENSOR_TYPE_ROTATION_VECTOR, this sensor reports an estimated
- *  heading accuracy:
- *    sensors_event_t.data[4] = estimated_accuracy (in radians)
- *  The heading error must be less than estimated_accuracy 95% of the time
- *
- * see SENSOR_TYPE_ROTATION_VECTOR for more details
  */
 #define SENSOR_TYPE_GEOMAGNETIC_ROTATION_VECTOR            (20)
 
@@ -966,7 +570,9 @@
 /*
  * sensors_poll_device_t is used with SENSORS_DEVICE_API_VERSION_0_1
  * and is present for backward binary and source compatibility.
- * (see documentation of the hooks in struct sensors_poll_device_1 below)
+ * See the Sensors HAL interface section for complete descriptions of the
+ * following functions:
+ * http://source.android.com/devices/sensors/index.html#hal
  */
 struct sensors_poll_device_t {
     struct hw_device_t common;
@@ -991,70 +597,24 @@
         struct {
             struct hw_device_t common;
 
-            /* Activate/de-activate one sensor.
+            /* Activate/de-activate one sensor. Return 0 on success, negative
              *
              * handle is the handle of the sensor to change.
              * enabled set to 1 to enable, or 0 to disable the sensor.
              *
-             * if enabled is set to 1, the sensor is activated even if
-             * setDelay() wasn't called before. In this case, a default rate
-             * should be used.
-             *
-             * unless otherwise noted in the sensor types definitions, an
-             * activated sensor never prevents the SoC to go into suspend
-             * mode; that is, the HAL shall not hold a partial wake-lock on
-             * behalf of applications.
-             *
-             * one-shot sensors de-activate themselves automatically upon
-             * receiving an event and they must still accept to be deactivated
-             * through a call to activate(..., ..., 0).
-             *
-             * if "enabled" is 1 and the sensor is already activated, this
-             * function is a no-op and succeeds.
-             *
-             * if "enabled" is 0 and the sensor is already de-activated,
-             * this function is a no-op and succeeds.
-             *
-             * return 0 on success, negative errno code otherwise
+             * Return 0 on success, negative errno code otherwise.
              */
             int (*activate)(struct sensors_poll_device_t *dev,
                     int handle, int enabled);
 
             /**
              * Set the events's period in nanoseconds for a given sensor.
-             *
-             * What the period_ns parameter means depends on the specified
-             * sensor's trigger mode:
-             *
-             * continuous: setDelay() sets the sampling rate.
-             * on-change: setDelay() limits the delivery rate of events
-             * one-shot: setDelay() is ignored. it has no effect.
-             * special: see specific sensor type definitions
-             *
-             * For continuous and on-change sensors, if the requested value is
-             * less than sensor_t::minDelay, then it's silently clamped to
-             * sensor_t::minDelay unless sensor_t::minDelay is 0, in which
-             * case it is clamped to >= 1ms.
-             *
-             * setDelay will not be called when the sensor is in batching mode.
-             * In this case, batch() will be called with the new period.
-             *
-             * @return 0 if successful, < 0 on error
              */
             int (*setDelay)(struct sensors_poll_device_t *dev,
                     int handle, int64_t period_ns);
 
             /**
              * Returns an array of sensor data.
-             * This function must block until events are available.
-             *
-             * return the number of events read on success, or -errno in case
-             * of an error.
-             *
-             * The number of events returned in data must be less or equal
-             * to the "count" argument.
-             *
-             * This function shall never return 0 (no event).
              */
             int (*poll)(struct sensors_poll_device_t *dev,
                     sensors_event_t* data, int count);
@@ -1063,200 +623,9 @@
 
 
     /*
-     * Enables batch mode for the given sensor and sets the delay between events
-     *
-     * A timeout value of zero disables batch mode for the given sensor.
-     *
-     * The period_ns parameter is equivalent to calling setDelay() -- this
-     * function both enables or disables the batch mode AND sets the events's
-     * period in nanosecond. See setDelay() above for a detailed explanation of
-     * the period_ns parameter.
-     *
-     * BATCH MODE:
-     * -----------
-     * In non-batch mode, all sensor events must be reported as soon as they
-     * are detected. For example, an accelerometer activated at 50Hz will
-     * trigger interrupts 50 times per second.
-     * While in batch mode, sensor events do not need to be reported as soon
-     * as they are detected. They can be temporarily stored in batches and
-     * reported in batches, as long as no event is delayed by more than
-     * "timeout" nanoseconds. That is, all events since the previous batch
-     * are recorded and returned all at once. This allows to reduce the amount
-     * of interrupts sent to the SoC, and allow the SoC to switch to a lower
-     * power state (Idle) while the sensor is capturing and batching data.
-     *
-     * setDelay() is not affected and it behaves as usual.
-     *
-     * Each event has a timestamp associated with it, the timestamp
-     * must be accurate and correspond to the time at which the event
-     * physically happened.
-     *
-     * Batching does not modify the behavior of poll(): batches from different
-     * sensors can be interleaved and split. As usual, all events from the same
-     * sensor are time-ordered.
-     *
-     * BEHAVIOUR OUTSIDE OF SUSPEND MODE:
-     * ----------------------------------
-     *
-     * When the SoC is awake (not in suspend mode), events must be reported in
-     * batches at least every "timeout". No event shall be dropped or lost.
-     * If internal h/w FIFOs fill-up before the timeout, then events are
-     * reported at that point to ensure no event is lost.
-     *
-     *
-     * NORMAL BEHAVIOR IN SUSPEND MODE:
-     * ---------------------------------
-     *
-     * By default, batch mode doesn't significantly change the interaction with
-     * suspend mode. That is, sensors must continue to allow the SoC to
-     * go into suspend mode and sensors must stay active to fill their
-     * internal FIFO. In this mode, when the FIFO fills up, it shall wrap
-     * around (basically behave like a circular buffer, overwriting events).
-     * As soon as the SoC comes out of suspend mode, a batch is produced with
-     * as much as the recent history as possible, and batch operation
-     * resumes as usual.
-     *
-     * The behavior described above allows applications to record the recent
-     * history of a set of sensor while keeping the SoC into suspend. It
-     * also allows the hardware to not have to rely on a wake-up interrupt line.
-     *
-     * WAKE_UPON_FIFO_FULL BEHAVIOR IN SUSPEND MODE:
-     * ----------------------------------------------
-     *
-     * There are cases, however, where an application cannot afford to lose
-     * any events, even when the device goes into suspend mode.
-     * For a given rate, if a sensor has the capability to store at least 10
-     * seconds worth of events in its FIFO and is able to wake up the Soc, it
-     * can implement an optional secondary mode: the WAKE_UPON_FIFO_FULL mode.
-     *
-     * The caller will set the SENSORS_BATCH_WAKE_UPON_FIFO_FULL flag to
-     * activate this mode. If the sensor does not support this mode, batch()
-     * will fail when the flag is set.
-     *
-     * When running with the WAKE_UPON_FIFO_FULL flag set, no events can be
-     * lost. When the FIFO is getting full, the sensor must wake up the SoC from
-     * suspend and return a batch before the FIFO fills-up.
-     * Depending on the device, it might take a few miliseconds for the SoC to
-     * entirely come out of suspend and start flushing the FIFO. Enough head
-     * room must be allocated in the FIFO to allow the device to entirely come
-     * out of suspend without the FIFO overflowing (no events shall be lost).
-     *
-     *   Implementing the WAKE_UPON_FIFO_FULL mode is optional.
-     *   If the hardware cannot support this mode, or if the physical
-     *   FIFO is so small that the device would never be allowed to go into
-     *   suspend for at least 10 seconds, then this function MUST fail when
-     *   the flag SENSORS_BATCH_WAKE_UPON_FIFO_FULL is set, regardless of
-     *   the value of the timeout parameter.
-     *
-     *
-     * DRY RUN:
-     * --------
-     *
-     * If the flag SENSORS_BATCH_DRY_RUN is set, this function returns
-     * without modifying the batch mode or the event period and has no side
-     * effects, but returns errors as usual (as it would if this flag was
-     * not set). This flag is used to check if batch mode is available for a
-     * given configuration -- in particular for a given sensor at a given rate.
-     *
-     *
-     * Return values:
-     * --------------
-     *
-     * Because sensors must be independent, the return value must not depend
-     * on the state of the system (whether another sensor is on or not),
-     * nor on whether the flag SENSORS_BATCH_DRY_RUN is set (in other words,
-     * if a batch call with SENSORS_BATCH_DRY_RUN is successful,
-     * the same call without SENSORS_BATCH_DRY_RUN must succeed as well).
-     *
-     * When timeout is not 0:
-     *   If successful, 0 is returned.
-     *   If the specified sensor doesn't support batch mode, return -EINVAL.
-     *   If the specified sensor's trigger-mode is one-shot, return -EINVAL.
-     *   If WAKE_UPON_FIFO_FULL is specified and the specified sensor's internal
-     *   FIFO is too small to store at least 10 seconds worth of data at the
-     *   given rate, -EINVAL is returned. Note that as stated above, this has to
-     *   be determined at compile time, and not based on the state of the
-     *   system.
-     *   If some other constraints above cannot be satisfied, return -EINVAL.
-     *
-     * Note: the timeout parameter, when > 0, has no impact on whether this
-     *       function succeeds or fails.
-     *
-     * When timeout is 0:
-     *   The caller will never set the wake_upon_fifo_full flag.
-     *   The function must succeed, and batch mode must be deactivated.
-     *
-     * Independently of whether DRY_RUN is specified, When the call to batch()
-     * fails, no state should be changed. In particular, a failed call to
-     * batch() should not change the rate of the sensor. Example:
-     *   setDelay(..., 10ms)
-     *   batch(..., 20ms, ...) fails
-     *   rate should stay 10ms.
-     *
-     *
-     * IMPLEMENTATION NOTES:
-     * ---------------------
-     *
-     * Batch mode, if supported, should happen at the hardware level,
-     * typically using hardware FIFOs. In particular, it SHALL NOT be
-     * implemented in the HAL, as this would be counter productive.
-     * The goal here is to save significant amounts of power.
-     *
-     * In some implementations, events from several sensors can share the
-     * same physical FIFO. In that case, all events in the FIFO can be sent and
-     * processed by the HAL as soon as one batch must be reported.
-     * For example, if the following sensors are activated:
-     *  - accelerometer batched with timeout = 20s
-     *  - gyroscope batched with timeout = 5s
-     * then the accelerometer batches can be reported at the same time the
-     * gyroscope batches are reported (every 5 seconds)
-     *
-     * Batch mode can be enabled or disabled at any time, in particular
-     * while the specified sensor is already enabled, and this shall not
-     * result in the loss of events.
-     *
-     * COMPARATIVE IMPORTANCE OF BATCHING FOR DIFFERENT SENSORS:
-     * ---------------------------------------------------------
-     *
-     * On platforms on which hardware fifo size is limited, the system designers
-     * might have to choose how much fifo to reserve for each sensor. To help
-     * with this choice, here is a list of applications made possible when
-     * batching is implemented on the different sensors.
-     *
-     * High value: Low power pedestrian dead reckoning
-     *   Target batching time: 20 seconds to 1 minute
-     *   Sensors to batch:
-     *    - Step detector
-     *    - Rotation vector or game rotation vector at 5Hz
-     *   Gives us step and heading while letting the SoC go to Suspend.
-     *
-     * High value: Medium power activity/gesture recognition
-     *   Target batching time: 3 seconds
-     *   Sensors to batch: accelerometer between 20Hz and 50Hz
-     *   Allows recognizing arbitrary activities and gestures without having
-     *   to keep the SoC fully awake while the data is collected.
-     *
-     * Medium-high value: Interrupt load reduction
-     *   Target batching time: < 1 second
-     *   Sensors to batch: any high frequency sensor.
-     *   If the gyroscope is set at 800Hz, even batching just 10 gyro events can
-     *   reduce the number of interrupts from 800/second to 80/second.
-     *
-     * Medium value: Continuous low frequency data collection
-     *   Target batching time: > 1 minute
-     *   Sensors to batch: barometer, humidity sensor, other low frequency
-     *     sensors.
-     *   Allows creating monitoring applications at low power.
-     *
-     * Medium value: Continuous full-sensors collection
-     *   Target batching time: > 1 minute
-     *   Sensors to batch: all, at high frequencies
-     *   Allows full collection of sensor data while leaving the SoC in
-     *   suspend mode. Only to consider if fifo space is not an issue.
-     *
-     * In each of the cases above, if WAKE_UPON_FIFO_FULL is implemented, the
-     * applications might decide to let the SoC go to suspend, allowing for even
-     * more power savings.
+     * Enables batch mode for the given sensor and sets the delay between events.
+     * See the Batching sensor results page for details:
+     * http://source.android.com/devices/sensors/batching.html
      */
     int (*batch)(struct sensors_poll_device_1* dev,
             int handle, int flags, int64_t period_ns, int64_t timeout);
@@ -1264,29 +633,7 @@
     /*
      * Flush adds a META_DATA_FLUSH_COMPLETE event (sensors_event_meta_data_t)
      * to the end of the "batch mode" FIFO for the specified sensor and flushes
-     * the FIFO; those events are delivered as usual (i.e.: as if the batch
-     * timeout had expired) and removed from the FIFO.
-     *
-     * See the META_DATA_FLUSH_COMPLETE section for details about the
-     * META_DATA_FLUSH_COMPLETE event.
-     *
-     * The flush happens asynchronously (i.e.: this function must return
-     * immediately).
-     *
-     * If the implementation uses a single FIFO for several sensors, that
-     * FIFO is flushed and the META_DATA_FLUSH_COMPLETE event is added only
-     * for the specified sensor.
-     *
-     * If the specified sensor wasn't in batch mode, flush succeeds and
-     * promptly sends a META_DATA_FLUSH_COMPLETE event for that sensor.
-     *
-     * If the FIFO was empty at the time of the call, flush returns
-     * 0 (success) and promptly sends a META_DATA_FLUSH_COMPLETE event
-     * for that sensor.
-     *
-     * If the specified sensor wasn't enabled, flush returns -EINVAL.
-     *
-     * return 0 on success, negative errno code otherwise.
+     * the FIFO.
      */
     int (*flush)(struct sensors_poll_device_1* dev, int handle);
 
diff --git a/modules/camera/Android.mk b/modules/camera/Android.mk
index e02a143..02d8a33 100644
--- a/modules/camera/Android.mk
+++ b/modules/camera/Android.mk
@@ -26,14 +26,17 @@
 LOCAL_SRC_FILES := \
 	CameraHAL.cpp \
 	Camera.cpp \
+	ExampleCamera.cpp \
 	Metadata.cpp \
 	Stream.cpp \
+	VendorTags.cpp \
 
 LOCAL_SHARED_LIBRARIES := \
 	libcamera_metadata \
 	libcutils \
 	liblog \
 	libsync \
+	libutils \
 
 LOCAL_CFLAGS += -Wall -Wextra -fvisibility=hidden
 
diff --git a/modules/camera/Camera.cpp b/modules/camera/Camera.cpp
index 973380e..57cdb47 100644
--- a/modules/camera/Camera.cpp
+++ b/modules/camera/Camera.cpp
@@ -15,11 +15,12 @@
  */
 
 #include <cstdlib>
-#include <pthread.h>
+#include <stdio.h>
 #include <hardware/camera3.h>
 #include <sync/sync.h>
 #include <system/camera_metadata.h>
 #include <system/graphics.h>
+#include <utils/Mutex.h>
 #include "CameraHAL.h"
 #include "Metadata.h"
 #include "Stream.h"
@@ -29,15 +30,12 @@
 #include <cutils/log.h>
 
 #define ATRACE_TAG (ATRACE_TAG_CAMERA | ATRACE_TAG_HAL)
-#include <cutils/trace.h>
-#include "ScopedTrace.h"
+#include <utils/Trace.h>
 
 #include "Camera.h"
 
 #define CAMERA_SYNC_TIMEOUT 5000 // in msecs
 
-#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
-
 namespace default_camera_hal {
 
 extern "C" {
@@ -59,9 +57,7 @@
     mNumStreams(0),
     mSettings(NULL)
 {
-    pthread_mutex_init(&mMutex, NULL);
-    pthread_mutex_init(&mStaticInfoMutex, NULL);
-
+    memset(&mTemplates, 0, sizeof(mTemplates));
     memset(&mDevice, 0, sizeof(mDevice));
     mDevice.common.tag    = HARDWARE_DEVICE_TAG;
     mDevice.common.version = CAMERA_DEVICE_API_VERSION_3_0;
@@ -72,17 +68,18 @@
 
 Camera::~Camera()
 {
-    pthread_mutex_destroy(&mMutex);
-    pthread_mutex_destroy(&mStaticInfoMutex);
+    if (mStaticInfo != NULL) {
+        free_camera_metadata(mStaticInfo);
+    }
 }
 
 int Camera::open(const hw_module_t *module, hw_device_t **device)
 {
     ALOGI("%s:%d: Opening camera device", __func__, mId);
-    CAMTRACE_CALL();
-    pthread_mutex_lock(&mMutex);
+    ATRACE_CALL();
+    android::Mutex::Autolock al(mDeviceLock);
+
     if (mBusy) {
-        pthread_mutex_unlock(&mMutex);
         ALOGE("%s:%d: Error! Camera device already opened", __func__, mId);
         return -EBUSY;
     }
@@ -91,217 +88,62 @@
     mBusy = true;
     mDevice.common.module = const_cast<hw_module_t*>(module);
     *device = &mDevice.common;
-
-    pthread_mutex_unlock(&mMutex);
     return 0;
 }
 
 int Camera::getInfo(struct camera_info *info)
 {
+    android::Mutex::Autolock al(mStaticInfoLock);
+
     info->facing = CAMERA_FACING_FRONT;
     info->orientation = 0;
     info->device_version = mDevice.common.version;
-
-    pthread_mutex_lock(&mStaticInfoMutex);
     if (mStaticInfo == NULL) {
         mStaticInfo = initStaticInfo();
     }
-    pthread_mutex_unlock(&mStaticInfoMutex);
-
     info->static_camera_characteristics = mStaticInfo;
-
     return 0;
 }
 
 int Camera::close()
 {
     ALOGI("%s:%d: Closing camera device", __func__, mId);
-    CAMTRACE_CALL();
-    pthread_mutex_lock(&mMutex);
+    ATRACE_CALL();
+    android::Mutex::Autolock al(mDeviceLock);
+
     if (!mBusy) {
-        pthread_mutex_unlock(&mMutex);
         ALOGE("%s:%d: Error! Camera device not open", __func__, mId);
         return -EINVAL;
     }
 
     // TODO: close camera dev nodes, etc
     mBusy = false;
-
-    pthread_mutex_unlock(&mMutex);
     return 0;
 }
 
 int Camera::initialize(const camera3_callback_ops_t *callback_ops)
 {
+    int res;
+
     ALOGV("%s:%d: callback_ops=%p", __func__, mId, callback_ops);
     mCallbackOps = callback_ops;
-    // Create standard settings templates
-    // 0 is invalid as template
-    mTemplates[0] = NULL;
-    // CAMERA3_TEMPLATE_PREVIEW = 1
-    mTemplates[1] = new Metadata(ANDROID_CONTROL_MODE_OFF,
-            ANDROID_CONTROL_CAPTURE_INTENT_PREVIEW);
-    // CAMERA3_TEMPLATE_STILL_CAPTURE = 2
-    mTemplates[2] = new Metadata(ANDROID_CONTROL_MODE_OFF,
-            ANDROID_CONTROL_CAPTURE_INTENT_STILL_CAPTURE);
-    // CAMERA3_TEMPLATE_VIDEO_RECORD = 3
-    mTemplates[3] = new Metadata(ANDROID_CONTROL_MODE_OFF,
-            ANDROID_CONTROL_CAPTURE_INTENT_VIDEO_RECORD);
-    // CAMERA3_TEMPLATE_VIDEO_SNAPSHOT = 4
-    mTemplates[4] = new Metadata(ANDROID_CONTROL_MODE_OFF,
-            ANDROID_CONTROL_CAPTURE_INTENT_VIDEO_SNAPSHOT);
-    // CAMERA3_TEMPLATE_STILL_ZERO_SHUTTER_LAG = 5
-    mTemplates[5] = new Metadata(ANDROID_CONTROL_MODE_OFF,
-            ANDROID_CONTROL_CAPTURE_INTENT_ZERO_SHUTTER_LAG);
-    // Pre-generate metadata structures
-    for (int i = 1; i < CAMERA3_TEMPLATE_COUNT; i++) {
-        mTemplates[i]->generate();
+    // per-device specific initialization
+    res = initDevice();
+    if (res != 0) {
+        ALOGE("%s:%d: Failed to initialize device!", __func__, mId);
+        return res;
     }
-    // TODO: create vendor templates
     return 0;
 }
 
-camera_metadata_t *Camera::initStaticInfo()
-{
-    /*
-     * Setup static camera info.  This will have to customized per camera
-     * device.
-     */
-    Metadata m;
-
-    /* android.control */
-    int32_t android_control_ae_available_target_fps_ranges[] = {30, 30};
-    m.addInt32(ANDROID_CONTROL_AE_AVAILABLE_TARGET_FPS_RANGES,
-            ARRAY_SIZE(android_control_ae_available_target_fps_ranges),
-            android_control_ae_available_target_fps_ranges);
-
-    int32_t android_control_ae_compensation_range[] = {-4, 4};
-    m.addInt32(ANDROID_CONTROL_AE_COMPENSATION_RANGE,
-            ARRAY_SIZE(android_control_ae_compensation_range),
-            android_control_ae_compensation_range);
-
-    camera_metadata_rational_t android_control_ae_compensation_step[] = {{2,1}};
-    m.addRational(ANDROID_CONTROL_AE_COMPENSATION_STEP,
-            ARRAY_SIZE(android_control_ae_compensation_step),
-            android_control_ae_compensation_step);
-
-    int32_t android_control_max_regions[] = {1};
-    m.addInt32(ANDROID_CONTROL_MAX_REGIONS,
-            ARRAY_SIZE(android_control_max_regions),
-            android_control_max_regions);
-
-    /* android.jpeg */
-    int32_t android_jpeg_available_thumbnail_sizes[] = {0, 0, 128, 96};
-    m.addInt32(ANDROID_JPEG_AVAILABLE_THUMBNAIL_SIZES,
-            ARRAY_SIZE(android_jpeg_available_thumbnail_sizes),
-            android_jpeg_available_thumbnail_sizes);
-
-    /* android.lens */
-    float android_lens_info_available_focal_lengths[] = {1.0};
-    m.addFloat(ANDROID_LENS_INFO_AVAILABLE_FOCAL_LENGTHS,
-            ARRAY_SIZE(android_lens_info_available_focal_lengths),
-            android_lens_info_available_focal_lengths);
-
-    /* android.request */
-    int32_t android_request_max_num_output_streams[] = {0, 3, 1};
-    m.addInt32(ANDROID_REQUEST_MAX_NUM_OUTPUT_STREAMS,
-            ARRAY_SIZE(android_request_max_num_output_streams),
-            android_request_max_num_output_streams);
-
-    /* android.scaler */
-    int32_t android_scaler_available_formats[] = {
-            HAL_PIXEL_FORMAT_RAW_SENSOR,
-            HAL_PIXEL_FORMAT_BLOB,
-            HAL_PIXEL_FORMAT_RGBA_8888,
-            HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED,
-            // These are handled by YCbCr_420_888
-            //        HAL_PIXEL_FORMAT_YV12,
-            //        HAL_PIXEL_FORMAT_YCrCb_420_SP,
-            HAL_PIXEL_FORMAT_YCbCr_420_888};
-    m.addInt32(ANDROID_SCALER_AVAILABLE_FORMATS,
-            ARRAY_SIZE(android_scaler_available_formats),
-            android_scaler_available_formats);
-
-    int64_t android_scaler_available_jpeg_min_durations[] = {1};
-    m.addInt64(ANDROID_SCALER_AVAILABLE_JPEG_MIN_DURATIONS,
-            ARRAY_SIZE(android_scaler_available_jpeg_min_durations),
-            android_scaler_available_jpeg_min_durations);
-
-    int32_t android_scaler_available_jpeg_sizes[] = {640, 480};
-    m.addInt32(ANDROID_SCALER_AVAILABLE_JPEG_SIZES,
-            ARRAY_SIZE(android_scaler_available_jpeg_sizes),
-            android_scaler_available_jpeg_sizes);
-
-    float android_scaler_available_max_digital_zoom[] = {1};
-    m.addFloat(ANDROID_SCALER_AVAILABLE_MAX_DIGITAL_ZOOM,
-            ARRAY_SIZE(android_scaler_available_max_digital_zoom),
-            android_scaler_available_max_digital_zoom);
-
-    int64_t android_scaler_available_processed_min_durations[] = {1};
-    m.addInt64(ANDROID_SCALER_AVAILABLE_PROCESSED_MIN_DURATIONS,
-            ARRAY_SIZE(android_scaler_available_processed_min_durations),
-            android_scaler_available_processed_min_durations);
-
-    int32_t android_scaler_available_processed_sizes[] = {640, 480};
-    m.addInt32(ANDROID_SCALER_AVAILABLE_PROCESSED_SIZES,
-            ARRAY_SIZE(android_scaler_available_processed_sizes),
-            android_scaler_available_processed_sizes);
-
-    int64_t android_scaler_available_raw_min_durations[] = {1};
-    m.addInt64(ANDROID_SCALER_AVAILABLE_RAW_MIN_DURATIONS,
-            ARRAY_SIZE(android_scaler_available_raw_min_durations),
-            android_scaler_available_raw_min_durations);
-
-    int32_t android_scaler_available_raw_sizes[] = {640, 480};
-    m.addInt32(ANDROID_SCALER_AVAILABLE_RAW_SIZES,
-            ARRAY_SIZE(android_scaler_available_raw_sizes),
-            android_scaler_available_raw_sizes);
-
-    /* android.sensor */
-
-    int32_t android_sensor_info_active_array_size[] = {0, 0, 640, 480};
-    m.addInt32(ANDROID_SENSOR_INFO_ACTIVE_ARRAY_SIZE,
-            ARRAY_SIZE(android_sensor_info_active_array_size),
-            android_sensor_info_active_array_size);
-
-    int32_t android_sensor_info_sensitivity_range[] =
-            {100, 1600};
-    m.addInt32(ANDROID_SENSOR_INFO_SENSITIVITY_RANGE,
-            ARRAY_SIZE(android_sensor_info_sensitivity_range),
-            android_sensor_info_sensitivity_range);
-
-    int64_t android_sensor_info_max_frame_duration[] = {30000000000};
-    m.addInt64(ANDROID_SENSOR_INFO_MAX_FRAME_DURATION,
-            ARRAY_SIZE(android_sensor_info_max_frame_duration),
-            android_sensor_info_max_frame_duration);
-
-    float android_sensor_info_physical_size[] = {3.2, 2.4};
-    m.addFloat(ANDROID_SENSOR_INFO_PHYSICAL_SIZE,
-            ARRAY_SIZE(android_sensor_info_physical_size),
-            android_sensor_info_physical_size);
-
-    int32_t android_sensor_info_pixel_array_size[] = {640, 480};
-    m.addInt32(ANDROID_SENSOR_INFO_PIXEL_ARRAY_SIZE,
-            ARRAY_SIZE(android_sensor_info_pixel_array_size),
-            android_sensor_info_pixel_array_size);
-
-    int32_t android_sensor_orientation[] = {0};
-    m.addInt32(ANDROID_SENSOR_ORIENTATION,
-            ARRAY_SIZE(android_sensor_orientation),
-            android_sensor_orientation);
-
-    /* End of static camera characteristics */
-
-    return clone_camera_metadata(m.generate());
-}
-
 int Camera::configureStreams(camera3_stream_configuration_t *stream_config)
 {
     camera3_stream_t *astream;
     Stream **newStreams = NULL;
 
-    CAMTRACE_CALL();
     ALOGV("%s:%d: stream_config=%p", __func__, mId, stream_config);
+    ATRACE_CALL();
+    android::Mutex::Autolock al(mDeviceLock);
 
     if (stream_config == NULL) {
         ALOGE("%s:%d: NULL stream configuration array", __func__, mId);
@@ -317,8 +159,6 @@
     ALOGV("%s:%d: Number of Streams: %d", __func__, mId,
             stream_config->num_streams);
 
-    pthread_mutex_lock(&mMutex);
-
     // Mark all current streams unused for now
     for (int i = 0; i < mNumStreams; i++)
         mStreams[i]->mReuse = false;
@@ -356,14 +196,11 @@
 
     // Clear out last seen settings metadata
     setSettings(NULL);
-
-    pthread_mutex_unlock(&mMutex);
     return 0;
 
 err_out:
     // Clean up temporary streams, preserve existing mStreams/mNumStreams
     destroyStreams(newStreams, stream_config->num_streams);
-    pthread_mutex_unlock(&mMutex);
     return -EINVAL;
 }
 
@@ -469,15 +306,20 @@
     return stream->registerBuffers(buf_set);
 }
 
+bool Camera::isValidTemplateType(int type)
+{
+    return type < 1 || type >= CAMERA3_TEMPLATE_COUNT;
+}
+
 const camera_metadata_t* Camera::constructDefaultRequestSettings(int type)
 {
     ALOGV("%s:%d: type=%d", __func__, mId, type);
 
-    if (type < 1 || type >= CAMERA3_TEMPLATE_COUNT) {
+    if (!isValidTemplateType(type)) {
         ALOGE("%s:%d: Invalid template request type: %d", __func__, mId, type);
         return NULL;
     }
-    return mTemplates[type]->generate();
+    return mTemplates[type];
 }
 
 int Camera::processCaptureRequest(camera3_capture_request_t *request)
@@ -485,7 +327,7 @@
     camera3_capture_result result;
 
     ALOGV("%s:%d: request=%p", __func__, mId, request);
-    CAMTRACE_CALL();
+    ATRACE_CALL();
 
     if (request == NULL) {
         ALOGE("%s:%d: NULL request recieved", __func__, mId);
@@ -565,12 +407,6 @@
         mSettings = clone_camera_metadata(new_settings);
 }
 
-bool Camera::isValidCaptureSettings(const camera_metadata_t* /*settings*/)
-{
-    // TODO: reject settings that cannot be captured
-    return true;
-}
-
 bool Camera::isValidReprocessSettings(const camera_metadata_t* /*settings*/)
 {
     // TODO: reject settings that cannot be reprocessed
@@ -640,7 +476,62 @@
 void Camera::dump(int fd)
 {
     ALOGV("%s:%d: Dumping to fd %d", __func__, mId, fd);
-    // TODO: dprintf all relevant state to fd
+    ATRACE_CALL();
+    android::Mutex::Autolock al(mDeviceLock);
+
+    fdprintf(fd, "Camera ID: %d (Busy: %d)\n", mId, mBusy);
+
+    // TODO: dump all settings
+    fdprintf(fd, "Most Recent Settings: (%p)\n", mSettings);
+
+    fdprintf(fd, "Number of streams: %d\n", mNumStreams);
+    for (int i = 0; i < mNumStreams; i++) {
+        fdprintf(fd, "Stream %d/%d:\n", i, mNumStreams);
+        mStreams[i]->dump(fd);
+    }
+}
+
+const char* Camera::templateToString(int type)
+{
+    switch (type) {
+    case CAMERA3_TEMPLATE_PREVIEW:
+        return "CAMERA3_TEMPLATE_PREVIEW";
+    case CAMERA3_TEMPLATE_STILL_CAPTURE:
+        return "CAMERA3_TEMPLATE_STILL_CAPTURE";
+    case CAMERA3_TEMPLATE_VIDEO_RECORD:
+        return "CAMERA3_TEMPLATE_VIDEO_RECORD";
+    case CAMERA3_TEMPLATE_VIDEO_SNAPSHOT:
+        return "CAMERA3_TEMPLATE_VIDEO_SNAPSHOT";
+    case CAMERA3_TEMPLATE_ZERO_SHUTTER_LAG:
+        return "CAMERA3_TEMPLATE_ZERO_SHUTTER_LAG";
+    }
+    // TODO: support vendor templates
+    return "Invalid template type!";
+}
+
+int Camera::setTemplate(int type, camera_metadata_t *settings)
+{
+    android::Mutex::Autolock al(mDeviceLock);
+
+    if (!isValidTemplateType(type)) {
+        ALOGE("%s:%d: Invalid template request type: %d", __func__, mId, type);
+        return -EINVAL;
+    }
+
+    if (mTemplates[type] != NULL) {
+        ALOGE("%s:%d: Setting already constructed template type %s(%d)",
+                __func__, mId, templateToString(type), type);
+        return -EINVAL;
+    }
+
+    // Make a durable copy of the underlying metadata
+    mTemplates[type] = clone_camera_metadata(settings);
+    if (mTemplates[type] == NULL) {
+        ALOGE("%s:%d: Failed to clone metadata %p for template type %s(%d)",
+                __func__, mId, settings, templateToString(type), type);
+        return -EINVAL;
+    }
+    return 0;
 }
 
 extern "C" {
diff --git a/modules/camera/Camera.h b/modules/camera/Camera.h
index be672f9..f1f33b1 100644
--- a/modules/camera/Camera.h
+++ b/modules/camera/Camera.h
@@ -17,9 +17,9 @@
 #ifndef CAMERA_H_
 #define CAMERA_H_
 
-#include <pthread.h>
 #include <hardware/hardware.h>
 #include <hardware/camera3.h>
+#include <utils/Mutex.h>
 #include "Metadata.h"
 #include "Stream.h"
 
@@ -28,12 +28,14 @@
 // This is constructed when the HAL module is loaded, one per physical camera.
 // It is opened by the framework, and must be closed before it can be opened
 // again.
+// This is an abstract class, containing all logic and data shared between all
+// camera devices (front, back, etc) and common to the ISP.
 class Camera {
     public:
         // id is used to distinguish cameras. 0 <= id < NUM_CAMERAS.
         // module is a handle to the HAL module, used when the device is opened.
         Camera(int id);
-        ~Camera();
+        virtual ~Camera();
 
         // Common Camera Device Operations (see <hardware/camera_common.h>)
         int open(const hw_module_t *module, hw_device_t **device);
@@ -49,12 +51,22 @@
         void getMetadataVendorTagOps(vendor_tag_query_ops_t *ops);
         void dump(int fd);
 
-        // Camera device handle returned to framework for use
-        camera3_device_t mDevice;
+
+    protected:
+        // Initialize static camera characteristics for individual device
+        virtual camera_metadata_t *initStaticInfo() = 0;
+        // Verify settings are valid for a capture
+        virtual bool isValidCaptureSettings(const camera_metadata_t *) = 0;
+        // Separate initialization method for individual devices when opened
+        virtual int initDevice() = 0;
+        // Accessor used by initDevice() to set the templates' metadata
+        int setTemplate(int type, camera_metadata_t *static_info);
+        // Prettyprint template names
+        const char* templateToString(int type);
 
     private:
-        // Separate initialization method for static metadata
-        camera_metadata_t *initStaticInfo();
+        // Camera device handle returned to framework for use
+        camera3_device_t mDevice;
         // Reuse a stream already created by this device
         Stream *reuseStream(camera3_stream_t *astream);
         // Destroy all streams in a stream array, and the array itself
@@ -65,8 +77,6 @@
         void setupStreams(Stream **array, int count);
         // Copy new settings for re-use and clean up old settings.
         void setSettings(const camera_metadata_t *new_settings);
-        // Verify settings are valid for a capture
-        bool isValidCaptureSettings(const camera_metadata_t *settings);
         // Verify settings are valid for reprocessing an input buffer
         bool isValidReprocessSettings(const camera_metadata_t *settings);
         // Process an output buffer
@@ -74,6 +84,8 @@
                 camera3_stream_buffer_t *out);
         // Send a shutter notify message with start of exposure time
         void notifyShutter(uint32_t frame_number, uint64_t timestamp);
+        // Is type a valid template type (and valid index into mTemplates)
+        bool isValidTemplateType(int type);
 
         // Identifier used by framework to distinguish cameras
         const int mId;
@@ -88,16 +100,16 @@
         // Methods used to call back into the framework
         const camera3_callback_ops_t *mCallbackOps;
         // Lock protecting the Camera object for modifications
-        pthread_mutex_t mMutex;
+        android::Mutex mDeviceLock;
         // Lock protecting only static camera characteristics, which may
         // be accessed without the camera device open
-        pthread_mutex_t mStaticInfoMutex;
+        android::Mutex mStaticInfoLock;
         // Array of handles to streams currently in use by the device
         Stream **mStreams;
         // Number of streams in mStreams
         int mNumStreams;
         // Static array of standard camera settings templates
-        Metadata *mTemplates[CAMERA3_TEMPLATE_COUNT];
+        camera_metadata_t *mTemplates[CAMERA3_TEMPLATE_COUNT];
         // Most recent request settings seen, memoized to be reused
         camera_metadata_t *mSettings;
 };
diff --git a/modules/camera/CameraHAL.cpp b/modules/camera/CameraHAL.cpp
index dfbbe4c..148f99c 100644
--- a/modules/camera/CameraHAL.cpp
+++ b/modules/camera/CameraHAL.cpp
@@ -17,7 +17,8 @@
 #include <cstdlib>
 #include <hardware/camera_common.h>
 #include <hardware/hardware.h>
-#include "Camera.h"
+#include "ExampleCamera.h"
+#include "VendorTags.h"
 
 //#define LOG_NDEBUG 0
 #define LOG_TAG "DefaultCameraHAL"
@@ -38,25 +39,24 @@
 
 // Default Camera HAL has 2 cameras, front and rear.
 static CameraHAL gCameraHAL(2);
+// Handle containing vendor tag functionality
+static VendorTags gVendorTags;
 
 CameraHAL::CameraHAL(int num_cameras)
   : mNumberOfCameras(num_cameras),
     mCallbacks(NULL)
 {
-    int i;
-
     // Allocate camera array and instantiate camera devices
     mCameras = new Camera*[mNumberOfCameras];
-    for (i = 0; i < mNumberOfCameras; i++) {
-        mCameras[i] = new Camera(i);
-    }
+    // Rear camera
+    mCameras[0] = new ExampleCamera(0);
+    // Front camera
+    mCameras[1] = new ExampleCamera(1);
 }
 
 CameraHAL::~CameraHAL()
 {
-    int i;
-
-    for (i = 0; i < mNumberOfCameras; i++) {
+    for (int i = 0; i < mNumberOfCameras; i++) {
         delete mCameras[i];
     }
     delete [] mCameras;
@@ -124,6 +124,41 @@
     return gCameraHAL.setCallbacks(callbacks);
 }
 
+static int get_tag_count(const vendor_tag_ops_t* ops)
+{
+    return gVendorTags.getTagCount(ops);
+}
+
+static void get_all_tags(const vendor_tag_ops_t* ops, uint32_t* tag_array)
+{
+    gVendorTags.getAllTags(ops, tag_array);
+}
+
+static const char* get_section_name(const vendor_tag_ops_t* ops, uint32_t tag)
+{
+    return gVendorTags.getSectionName(ops, tag);
+}
+
+static const char* get_tag_name(const vendor_tag_ops_t* ops, uint32_t tag)
+{
+    return gVendorTags.getTagName(ops, tag);
+}
+
+static int get_tag_type(const vendor_tag_ops_t* ops, uint32_t tag)
+{
+    return gVendorTags.getTagType(ops, tag);
+}
+
+static void get_vendor_tag_ops(vendor_tag_ops_t* ops)
+{
+    ALOGV("%s : ops=%p", __func__, ops);
+    ops->get_tag_count      = get_tag_count;
+    ops->get_all_tags       = get_all_tags;
+    ops->get_section_name   = get_section_name;
+    ops->get_tag_name       = get_tag_name;
+    ops->get_tag_type       = get_tag_type;
+}
+
 static int open_dev(const hw_module_t* mod, const char* name, hw_device_t** dev)
 {
     return gCameraHAL.open(mod, name, dev);
@@ -136,7 +171,7 @@
 camera_module_t HAL_MODULE_INFO_SYM __attribute__ ((visibility("default"))) = {
     common : {
         tag                : HARDWARE_MODULE_TAG,
-        module_api_version : CAMERA_MODULE_API_VERSION_2_0,
+        module_api_version : CAMERA_MODULE_API_VERSION_2_2,
         hal_api_version    : HARDWARE_HAL_API_VERSION,
         id                 : CAMERA_HARDWARE_MODULE_ID,
         name               : "Default Camera HAL",
@@ -147,7 +182,8 @@
     },
     get_number_of_cameras : get_number_of_cameras,
     get_camera_info       : get_camera_info,
-    set_callbacks         : set_callbacks
+    set_callbacks         : set_callbacks,
+    get_vendor_tag_ops    : get_vendor_tag_ops
 };
 } // extern "C"
 
diff --git a/modules/camera/CameraHAL.h b/modules/camera/CameraHAL.h
index ba0db4e..a5edc85 100644
--- a/modules/camera/CameraHAL.h
+++ b/modules/camera/CameraHAL.h
@@ -21,6 +21,7 @@
 #include <hardware/hardware.h>
 #include <hardware/camera_common.h>
 #include "Camera.h"
+#include "VendorTags.h"
 
 namespace default_camera_hal {
 // CameraHAL contains all module state that isn't specific to an individual
@@ -34,6 +35,7 @@
         int getNumberOfCameras();
         int getCameraInfo(int camera_id, struct camera_info *info);
         int setCallbacks(const camera_module_callbacks_t *callbacks);
+        void getVendorTagOps(vendor_tag_ops_t* ops);
 
         // Hardware Module Interface (see <hardware/hardware.h>)
         int open(const hw_module_t* mod, const char* name, hw_device_t** dev);
diff --git a/modules/camera/ExampleCamera.cpp b/modules/camera/ExampleCamera.cpp
new file mode 100644
index 0000000..16e1e02
--- /dev/null
+++ b/modules/camera/ExampleCamera.cpp
@@ -0,0 +1,261 @@
+/*
+ * 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.
+ */
+
+#include <system/camera_metadata.h>
+#include "Camera.h"
+
+//#define LOG_NDEBUG 0
+#define LOG_TAG "ExampleCamera"
+#include <cutils/log.h>
+
+#define ATRACE_TAG (ATRACE_TAG_CAMERA | ATRACE_TAG_HAL)
+#include <utils/Trace.h>
+
+#include "ExampleCamera.h"
+
+#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
+
+namespace default_camera_hal {
+
+ExampleCamera::ExampleCamera(int id) : Camera(id)
+{
+}
+
+ExampleCamera::~ExampleCamera()
+{
+}
+
+camera_metadata_t *ExampleCamera::initStaticInfo()
+{
+    /*
+     * Setup static camera info.  This will have to customized per camera
+     * device.
+     */
+    Metadata m;
+
+    /* android.control */
+    int32_t android_control_ae_available_target_fps_ranges[] = {30, 30};
+    m.addInt32(ANDROID_CONTROL_AE_AVAILABLE_TARGET_FPS_RANGES,
+            ARRAY_SIZE(android_control_ae_available_target_fps_ranges),
+            android_control_ae_available_target_fps_ranges);
+
+    int32_t android_control_ae_compensation_range[] = {-4, 4};
+    m.addInt32(ANDROID_CONTROL_AE_COMPENSATION_RANGE,
+            ARRAY_SIZE(android_control_ae_compensation_range),
+            android_control_ae_compensation_range);
+
+    camera_metadata_rational_t android_control_ae_compensation_step[] = {{2,1}};
+    m.addRational(ANDROID_CONTROL_AE_COMPENSATION_STEP,
+            ARRAY_SIZE(android_control_ae_compensation_step),
+            android_control_ae_compensation_step);
+
+    int32_t android_control_max_regions[] = {1};
+    m.addInt32(ANDROID_CONTROL_MAX_REGIONS,
+            ARRAY_SIZE(android_control_max_regions),
+            android_control_max_regions);
+
+    /* android.jpeg */
+    int32_t android_jpeg_available_thumbnail_sizes[] = {0, 0, 128, 96};
+    m.addInt32(ANDROID_JPEG_AVAILABLE_THUMBNAIL_SIZES,
+            ARRAY_SIZE(android_jpeg_available_thumbnail_sizes),
+            android_jpeg_available_thumbnail_sizes);
+
+    int32_t android_jpeg_max_size[] = {13 * 1024 * 1024}; // 13MB
+    m.addInt32(ANDROID_JPEG_MAX_SIZE,
+            ARRAY_SIZE(android_jpeg_max_size),
+            android_jpeg_max_size);
+
+    /* android.lens */
+    float android_lens_info_available_focal_lengths[] = {1.0};
+    m.addFloat(ANDROID_LENS_INFO_AVAILABLE_FOCAL_LENGTHS,
+            ARRAY_SIZE(android_lens_info_available_focal_lengths),
+            android_lens_info_available_focal_lengths);
+
+    /* android.request */
+    int32_t android_request_max_num_output_streams[] = {0, 3, 1};
+    m.addInt32(ANDROID_REQUEST_MAX_NUM_OUTPUT_STREAMS,
+            ARRAY_SIZE(android_request_max_num_output_streams),
+            android_request_max_num_output_streams);
+
+    /* android.scaler */
+    int32_t android_scaler_available_formats[] = {
+            HAL_PIXEL_FORMAT_RAW_SENSOR,
+            HAL_PIXEL_FORMAT_BLOB,
+            HAL_PIXEL_FORMAT_RGBA_8888,
+            HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED,
+            // These are handled by YCbCr_420_888
+            //        HAL_PIXEL_FORMAT_YV12,
+            //        HAL_PIXEL_FORMAT_YCrCb_420_SP,
+            HAL_PIXEL_FORMAT_YCbCr_420_888};
+    m.addInt32(ANDROID_SCALER_AVAILABLE_FORMATS,
+            ARRAY_SIZE(android_scaler_available_formats),
+            android_scaler_available_formats);
+
+    int64_t android_scaler_available_jpeg_min_durations[] = {1};
+    m.addInt64(ANDROID_SCALER_AVAILABLE_JPEG_MIN_DURATIONS,
+            ARRAY_SIZE(android_scaler_available_jpeg_min_durations),
+            android_scaler_available_jpeg_min_durations);
+
+    int32_t android_scaler_available_jpeg_sizes[] = {640, 480};
+    m.addInt32(ANDROID_SCALER_AVAILABLE_JPEG_SIZES,
+            ARRAY_SIZE(android_scaler_available_jpeg_sizes),
+            android_scaler_available_jpeg_sizes);
+
+    float android_scaler_available_max_digital_zoom[] = {1};
+    m.addFloat(ANDROID_SCALER_AVAILABLE_MAX_DIGITAL_ZOOM,
+            ARRAY_SIZE(android_scaler_available_max_digital_zoom),
+            android_scaler_available_max_digital_zoom);
+
+    int64_t android_scaler_available_processed_min_durations[] = {1};
+    m.addInt64(ANDROID_SCALER_AVAILABLE_PROCESSED_MIN_DURATIONS,
+            ARRAY_SIZE(android_scaler_available_processed_min_durations),
+            android_scaler_available_processed_min_durations);
+
+    int32_t android_scaler_available_processed_sizes[] = {640, 480};
+    m.addInt32(ANDROID_SCALER_AVAILABLE_PROCESSED_SIZES,
+            ARRAY_SIZE(android_scaler_available_processed_sizes),
+            android_scaler_available_processed_sizes);
+
+    int64_t android_scaler_available_raw_min_durations[] = {1};
+    m.addInt64(ANDROID_SCALER_AVAILABLE_RAW_MIN_DURATIONS,
+            ARRAY_SIZE(android_scaler_available_raw_min_durations),
+            android_scaler_available_raw_min_durations);
+
+    int32_t android_scaler_available_raw_sizes[] = {640, 480};
+    m.addInt32(ANDROID_SCALER_AVAILABLE_RAW_SIZES,
+            ARRAY_SIZE(android_scaler_available_raw_sizes),
+            android_scaler_available_raw_sizes);
+
+    /* android.sensor */
+
+    int32_t android_sensor_info_active_array_size[] = {0, 0, 640, 480};
+    m.addInt32(ANDROID_SENSOR_INFO_ACTIVE_ARRAY_SIZE,
+            ARRAY_SIZE(android_sensor_info_active_array_size),
+            android_sensor_info_active_array_size);
+
+    int32_t android_sensor_info_sensitivity_range[] =
+            {100, 1600};
+    m.addInt32(ANDROID_SENSOR_INFO_SENSITIVITY_RANGE,
+            ARRAY_SIZE(android_sensor_info_sensitivity_range),
+            android_sensor_info_sensitivity_range);
+
+    int64_t android_sensor_info_max_frame_duration[] = {30000000000};
+    m.addInt64(ANDROID_SENSOR_INFO_MAX_FRAME_DURATION,
+            ARRAY_SIZE(android_sensor_info_max_frame_duration),
+            android_sensor_info_max_frame_duration);
+
+    float android_sensor_info_physical_size[] = {3.2, 2.4};
+    m.addFloat(ANDROID_SENSOR_INFO_PHYSICAL_SIZE,
+            ARRAY_SIZE(android_sensor_info_physical_size),
+            android_sensor_info_physical_size);
+
+    int32_t android_sensor_info_pixel_array_size[] = {640, 480};
+    m.addInt32(ANDROID_SENSOR_INFO_PIXEL_ARRAY_SIZE,
+            ARRAY_SIZE(android_sensor_info_pixel_array_size),
+            android_sensor_info_pixel_array_size);
+
+    int32_t android_sensor_orientation[] = {0};
+    m.addInt32(ANDROID_SENSOR_ORIENTATION,
+            ARRAY_SIZE(android_sensor_orientation),
+            android_sensor_orientation);
+
+    /* End of static camera characteristics */
+
+    return clone_camera_metadata(m.get());
+}
+
+int ExampleCamera::initDevice()
+{
+    int res;
+    Metadata base;
+
+    // Create standard settings templates from copies of base metadata
+    // TODO: use vendor tags in base metadata
+    if (res = base.add1UInt8(ANDROID_CONTROL_MODE, ANDROID_CONTROL_MODE_OFF))
+        return res;
+
+    // Use base settings to create all other templates and set them
+    if (res = setPreviewTemplate(base)) return res;
+    if (res = setStillTemplate(base)) return res;
+    if (res = setRecordTemplate(base)) return res;
+    if (res = setSnapshotTemplate(base)) return res;
+    if (res = setZslTemplate(base)) return res;
+
+    return 0;
+}
+
+int ExampleCamera::setPreviewTemplate(Metadata m)
+{
+    int res;
+    // Setup default preview controls
+    if (res = m.add1UInt8(ANDROID_CONTROL_CAPTURE_INTENT,
+            ANDROID_CONTROL_CAPTURE_INTENT_PREVIEW))
+        return res;
+    // TODO: set fast auto-focus, auto-whitebalance, auto-exposure, auto flash
+    return setTemplate(CAMERA3_TEMPLATE_PREVIEW, m.get());
+}
+
+int ExampleCamera::setStillTemplate(Metadata m)
+{
+    int res;
+    // Setup default still capture controls
+    if (res = m.add1UInt8(ANDROID_CONTROL_CAPTURE_INTENT,
+            ANDROID_CONTROL_CAPTURE_INTENT_STILL_CAPTURE))
+        return res;
+    // TODO: set fast auto-focus, auto-whitebalance, auto-exposure, auto flash
+    return setTemplate(CAMERA3_TEMPLATE_STILL_CAPTURE, m.get());
+}
+
+int ExampleCamera::setRecordTemplate(Metadata m)
+{
+    int res;
+    // Setup default video record controls
+    if (res = m.add1UInt8(ANDROID_CONTROL_CAPTURE_INTENT,
+            ANDROID_CONTROL_CAPTURE_INTENT_VIDEO_RECORD))
+        return res;
+    // TODO: set slow auto-focus, auto-whitebalance, auto-exposure, flash off
+    return setTemplate(CAMERA3_TEMPLATE_VIDEO_RECORD, m.get());
+}
+
+int ExampleCamera::setSnapshotTemplate(Metadata m)
+{
+    int res;
+    // Setup default video snapshot controls
+    if (res = m.add1UInt8(ANDROID_CONTROL_CAPTURE_INTENT,
+            ANDROID_CONTROL_CAPTURE_INTENT_VIDEO_SNAPSHOT))
+        return res;
+    // TODO: set slow auto-focus, auto-whitebalance, auto-exposure, flash off 
+    return setTemplate(CAMERA3_TEMPLATE_VIDEO_SNAPSHOT, m.get());
+}
+
+int ExampleCamera::setZslTemplate(Metadata m)
+{
+    int res;
+    // Setup default zero shutter lag controls
+    if (res = m.add1UInt8(ANDROID_CONTROL_CAPTURE_INTENT,
+            ANDROID_CONTROL_CAPTURE_INTENT_ZERO_SHUTTER_LAG))
+        return res;
+    // TODO: set reprocessing parameters for zsl input queue
+    return setTemplate(CAMERA3_TEMPLATE_ZERO_SHUTTER_LAG, m.get());
+}
+
+bool ExampleCamera::isValidCaptureSettings(const camera_metadata_t* settings)
+{
+    // TODO: reject settings that cannot be captured
+    return true;
+}
+
+} // namespace default_camera_hal
diff --git a/modules/camera/ExampleCamera.h b/modules/camera/ExampleCamera.h
new file mode 100644
index 0000000..45c4a94
--- /dev/null
+++ b/modules/camera/ExampleCamera.h
@@ -0,0 +1,49 @@
+/*
+ * 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 EXAMPLE_CAMERA_H_
+#define EXAMPLE_CAMERA_H_
+
+#include <system/camera_metadata.h>
+#include "Camera.h"
+
+namespace default_camera_hal {
+// ExampleCamera is an example for a specific camera device. The Camera object
+// contains all logic common between all cameras (e.g. front and back cameras),
+// while a specific camera device (e.g. ExampleCamera) holds all specific
+// metadata and logic about that device.
+class ExampleCamera : public Camera {
+    public:
+        ExampleCamera(int id);
+        ~ExampleCamera();
+
+    private:
+        // Initialize static camera characteristics for individual device
+        camera_metadata_t *initStaticInfo();
+        // Initialize whole device (templates/etc) when opened
+        int initDevice();
+        // Initialize each template metadata controls
+        int setPreviewTemplate(Metadata m);
+        int setStillTemplate(Metadata m);
+        int setRecordTemplate(Metadata m);
+        int setSnapshotTemplate(Metadata m);
+        int setZslTemplate(Metadata m);
+        // Verify settings are valid for a capture with this device
+        bool isValidCaptureSettings(const camera_metadata_t* settings);
+};
+} // namespace default_camera_hal
+
+#endif // CAMERA_H_
diff --git a/modules/camera/Metadata.cpp b/modules/camera/Metadata.cpp
index d5854f9..72314d5 100644
--- a/modules/camera/Metadata.cpp
+++ b/modules/camera/Metadata.cpp
@@ -14,7 +14,6 @@
  * limitations under the License.
  */
 
-#include <pthread.h>
 #include <system/camera_metadata.h>
 
 //#define LOG_NDEBUG 0
@@ -22,102 +21,85 @@
 #include <cutils/log.h>
 
 #define ATRACE_TAG (ATRACE_TAG_CAMERA | ATRACE_TAG_HAL)
-#include <cutils/trace.h>
-#include "ScopedTrace.h"
+#include <utils/Trace.h>
 
 #include "Metadata.h"
 
 namespace default_camera_hal {
 
-Metadata::Metadata()
-  : mHead(NULL),
-    mTail(NULL),
-    mEntryCount(0),
-    mDataCount(0),
-    mGenerated(NULL),
-    mDirty(true)
+Metadata::Metadata():
+    mData(NULL)
 {
-    // NULL (default) pthread mutex attributes
-    pthread_mutex_init(&mMutex, NULL);
 }
 
 Metadata::~Metadata()
 {
-    Entry *current = mHead;
-
-    while (current != NULL) {
-        Entry *tmp = current;
-        current = current->mNext;
-        delete tmp;
-    }
-
-    if (mGenerated != NULL)
-        free_camera_metadata(mGenerated);
-
-    pthread_mutex_destroy(&mMutex);
+    replace(NULL);
 }
 
-Metadata::Metadata(uint8_t mode, uint8_t intent)
-  : mHead(NULL),
-    mTail(NULL),
-    mEntryCount(0),
-    mDataCount(0),
-    mGenerated(NULL),
-    mDirty(true)
+void Metadata::replace(camera_metadata_t *m)
 {
-    pthread_mutex_init(&mMutex, NULL);
-
-    if (validate(ANDROID_CONTROL_MODE, TYPE_BYTE, 1)) {
-        int res = add(ANDROID_CONTROL_MODE, 1, &mode);
-        if (res != 0) {
-            ALOGE("%s: Unable to add mode to template!", __func__);
-        }
-    } else {
-        ALOGE("%s: Invalid mode constructing template!", __func__);
+    if (m == mData) {
+        ALOGE("%s: Replacing metadata with itself?!", __func__);
+        return;
     }
-
-    if (validate(ANDROID_CONTROL_CAPTURE_INTENT, TYPE_BYTE, 1)) {
-        int res = add(ANDROID_CONTROL_CAPTURE_INTENT, 1, &intent);
-        if (res != 0) {
-            ALOGE("%s: Unable to add capture intent to template!", __func__);
-        }
-    } else {
-        ALOGE("%s: Invalid capture intent constructing template!", __func__);
-    }
+    if (mData)
+        free_camera_metadata(mData);
+    mData = m;
 }
 
-int Metadata::addUInt8(uint32_t tag, int count, uint8_t *data)
+int Metadata::init(const camera_metadata_t *metadata)
+{
+    camera_metadata_t* tmp;
+
+    if (!validate_camera_metadata_structure(metadata, NULL))
+        return -EINVAL;
+
+    tmp = clone_camera_metadata(metadata);
+    if (tmp == NULL)
+        return -EINVAL;
+
+    replace(tmp);
+    return 0;
+}
+
+int Metadata::addUInt8(uint32_t tag, int count, const uint8_t *data)
 {
     if (!validate(tag, TYPE_BYTE, count)) return -EINVAL;
     return add(tag, count, data);
 }
 
-int Metadata::addInt32(uint32_t tag, int count, int32_t *data)
+int Metadata::add1UInt8(uint32_t tag, const uint8_t data)
+{
+    return addUInt8(tag, 1, &data);
+}
+
+int Metadata::addInt32(uint32_t tag, int count, const int32_t *data)
 {
     if (!validate(tag, TYPE_INT32, count)) return -EINVAL;
     return add(tag, count, data);
 }
 
-int Metadata::addFloat(uint32_t tag, int count, float *data)
+int Metadata::addFloat(uint32_t tag, int count, const float *data)
 {
     if (!validate(tag, TYPE_FLOAT, count)) return -EINVAL;
     return add(tag, count, data);
 }
 
-int Metadata::addInt64(uint32_t tag, int count, int64_t *data)
+int Metadata::addInt64(uint32_t tag, int count, const int64_t *data)
 {
     if (!validate(tag, TYPE_INT64, count)) return -EINVAL;
     return add(tag, count, data);
 }
 
-int Metadata::addDouble(uint32_t tag, int count, double *data)
+int Metadata::addDouble(uint32_t tag, int count, const double *data)
 {
     if (!validate(tag, TYPE_DOUBLE, count)) return -EINVAL;
     return add(tag, count, data);
 }
 
 int Metadata::addRational(uint32_t tag, int count,
-        camera_metadata_rational_t *data)
+        const camera_metadata_rational_t *data)
 {
     if (!validate(tag, TYPE_RATIONAL, count)) return -EINVAL;
     return add(tag, count, data);
@@ -145,102 +127,46 @@
     return true;
 }
 
-int Metadata::add(uint32_t tag, int count, void *tag_data)
+int Metadata::add(uint32_t tag, int count, const void *tag_data)
 {
+    int res;
+    camera_metadata_t* tmp;
     int tag_type = get_camera_metadata_tag_type(tag);
-    size_t type_sz = camera_metadata_type_size[tag_type];
+    size_t size = calculate_camera_metadata_entry_data_size(tag_type, count);
+    size_t entry_capacity = get_camera_metadata_entry_count(mData) + 1;
+    size_t data_capacity = get_camera_metadata_data_count(mData) + size;
 
-    // Allocate array to hold new metadata
-    void *data = malloc(count * type_sz);
-    if (data == NULL)
+    // Opportunistically attempt to add if metadata has room for it
+    if (!add_camera_metadata_entry(mData, tag, tag_data, count))
+        return 0;
+
+    // Double new dimensions to minimize future reallocations
+    tmp = allocate_camera_metadata(entry_capacity * 2, data_capacity * 2);
+    if (tmp == NULL) {
+        ALOGE("%s: Failed to allocate new metadata with %d entries, %d data",
+                __func__, entry_capacity, data_capacity);
         return -ENOMEM;
-    memcpy(data, tag_data, count * type_sz);
+    }
+    // Append the current metadata to the new (empty) metadata
+    if (res = append_camera_metadata(tmp, mData)) {
+        ALOGE("%s: Failed to append old metadata %p to new %p",
+                __func__, mData, tmp);
+        return res;
+    }
+    // Add the remaining new item
+    if (res = add_camera_metadata_entry(tmp, tag, tag_data, count)) {
+        ALOGE("%s: Failed to add new entry (%d, %p, %d) to metadata %p",
+                __func__, tag, tag_data, count, tmp);
+        return res;
+    }
 
-    pthread_mutex_lock(&mMutex);
-    mEntryCount++;
-    mDataCount += calculate_camera_metadata_entry_data_size(tag_type, count);
-    push(new Entry(tag, data, count));
-    mDirty = true;
-    pthread_mutex_unlock(&mMutex);
+    replace(tmp);
     return 0;
 }
 
-camera_metadata_t* Metadata::generate()
+camera_metadata_t* Metadata::get()
 {
-    pthread_mutex_lock(&mMutex);
-    // Reuse if old generated metadata still valid
-    if (!mDirty && mGenerated != NULL) {
-        ALOGV("%s: Reusing generated metadata at %p", __func__, mGenerated);
-        goto out;
-    }
-    // Destroy old metadata
-    if (mGenerated != NULL) {
-        ALOGV("%s: Freeing generated metadata at %p", __func__, mGenerated);
-        free_camera_metadata(mGenerated);
-        mGenerated = NULL;
-    }
-    // Generate new metadata structure
-    ALOGV("%s: Generating new camera metadata structure, Entries:%d Data:%d",
-            __func__, mEntryCount, mDataCount);
-    mGenerated = allocate_camera_metadata(mEntryCount, mDataCount);
-    if (mGenerated == NULL) {
-        ALOGE("%s: Failed to allocate metadata (%d entries %d data)",
-                __func__, mEntryCount, mDataCount);
-        goto out;
-    }
-    // Walk list of entries adding each one to newly allocated metadata
-    for (Entry *current = mHead; current != NULL; current = current->mNext) {
-        int res = add_camera_metadata_entry(mGenerated, current->mTag,
-                current->mData, current->mCount);
-        if (res != 0) {
-            ALOGE("%s: Failed to add camera metadata: %d", __func__, res);
-            free_camera_metadata(mGenerated);
-            mGenerated = NULL;
-            goto out;
-        }
-    }
-
-out:
-    pthread_mutex_unlock(&mMutex);
-    return mGenerated;
-}
-
-Metadata::Entry::Entry(uint32_t tag, void *data, int count)
-  : mNext(NULL),
-    mPrev(NULL),
-    mTag(tag),
-    mData(data),
-    mCount(count)
-{
-}
-
-void Metadata::push(Entry *e)
-{
-    if (mHead == NULL) {
-        mHead = mTail = e;
-    } else {
-        mTail->insertAfter(e);
-        mTail = e;
-    }
-}
-
-Metadata::Entry::~Entry()
-{
-    if (mNext != NULL)
-        mNext->mPrev = mPrev;
-    if (mPrev != NULL)
-        mPrev->mNext = mNext;
-}
-
-void Metadata::Entry::insertAfter(Entry *e)
-{
-    if (e == NULL)
-        return;
-    if (mNext != NULL)
-        mNext->mPrev = e;
-    e->mNext = mNext;
-    e->mPrev = this;
-    mNext = e;
+    return mData;
 }
 
 } // namespace default_camera_hal
diff --git a/modules/camera/Metadata.h b/modules/camera/Metadata.h
index 22d2f22..f432d04 100644
--- a/modules/camera/Metadata.h
+++ b/modules/camera/Metadata.h
@@ -17,10 +17,9 @@
 #ifndef METADATA_H_
 #define METADATA_H_
 
+#include <stdint.h>
 #include <hardware/camera3.h>
-#include <hardware/gralloc.h>
 #include <system/camera_metadata.h>
-#include <system/graphics.h>
 
 namespace default_camera_hal {
 // Metadata is a convenience class for dealing with libcamera_metadata
@@ -28,51 +27,32 @@
     public:
         Metadata();
         ~Metadata();
-        // Constructor used for request metadata templates
-        Metadata(uint8_t mode, uint8_t intent);
+        // Initialize with framework metadata
+        int init(const camera_metadata_t *metadata);
 
-        // Parse and add an entry
-        int addUInt8(uint32_t tag, int count, uint8_t *data);
-        int addInt32(uint32_t tag, int count, int32_t *data);
-        int addFloat(uint32_t tag, int count, float *data);
-        int addInt64(uint32_t tag, int count, int64_t *data);
-        int addDouble(uint32_t tag, int count, double *data);
+        // Parse and add an entry. Allocates and copies new storage for *data.
+        int addUInt8(uint32_t tag, int count, const uint8_t *data);
+        int add1UInt8(uint32_t tag, const uint8_t data);
+        int addInt32(uint32_t tag, int count, const int32_t *data);
+        int addFloat(uint32_t tag, int count, const float *data);
+        int addInt64(uint32_t tag, int count, const int64_t *data);
+        int addDouble(uint32_t tag, int count, const double *data);
         int addRational(uint32_t tag, int count,
-                camera_metadata_rational_t *data);
-        // Generate a camera_metadata structure and fill it with internal data
-        camera_metadata_t *generate();
+                const camera_metadata_rational_t *data);
+
+        // Get a handle to the current metadata
+        // This is not a durable handle, and may be destroyed by add*/init
+        camera_metadata_t* get();
 
     private:
+        // Actual internal storage
+        camera_metadata_t* mData;
+        // Destroy old metadata and replace with new
+        void replace(camera_metadata_t *m);
         // Validate the tag, type and count for a metadata entry
         bool validate(uint32_t tag, int tag_type, int count);
-        // Add a verified tag with data to this Metadata structure
-        int add(uint32_t tag, int count, void *tag_data);
-
-        class Entry {
-            public:
-                Entry(uint32_t tag, void *data, int count);
-                ~Entry();
-                Entry *mNext;
-                Entry *mPrev;
-                const uint32_t mTag;
-                const void *mData;
-                const int mCount;
-                void insertAfter(Entry *e);
-        };
-        // List ends
-        Entry *mHead;
-        Entry *mTail;
-        // Append entry to list
-        void push(Entry *e);
-        // Total of entries and entry data size
-        int mEntryCount;
-        int mDataCount;
-        // Save generated metadata, invalidated on update
-        camera_metadata_t *mGenerated;
-        // Flag to force metadata regeneration
-        bool mDirty;
-        // Lock protecting the Metadata object for modifications
-        pthread_mutex_t mMutex;
+        // Add a verified tag with data
+        int add(uint32_t tag, int count, const void *tag_data);
 };
 } // namespace default_camera_hal
 
diff --git a/modules/camera/ScopedTrace.h b/modules/camera/ScopedTrace.h
deleted file mode 100644
index ed00570..0000000
--- a/modules/camera/ScopedTrace.h
+++ /dev/null
@@ -1,51 +0,0 @@
-/*
- * 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 CAMERA_SCOPED_TRACE_H
-#define CAMERA_SCOPED_TRACE_H
-
-#include <stdint.h>
-#include <cutils/trace.h>
-
-// See <cutils/trace.h> for more tracing macros.
-
-// CAMTRACE_NAME traces the beginning and end of the current scope.  To trace
-// the correct start and end times this macro should be declared first in the
-// scope body.
-#define CAMTRACE_NAME(name) ScopedTrace ___tracer(ATRACE_TAG, name)
-// CAMTRACE_CALL is an ATRACE_NAME that uses the current function name.
-#define CAMTRACE_CALL() CAMTRACE_NAME(__FUNCTION__)
-
-namespace default_camera_hal {
-
-class ScopedTrace {
-public:
-inline ScopedTrace(uint64_t tag, const char* name)
-    : mTag(tag) {
-    atrace_begin(mTag,name);
-}
-
-inline ~ScopedTrace() {
-    atrace_end(mTag);
-}
-
-private:
-    uint64_t mTag;
-};
-
-}; // namespace default_camera_hal
-
-#endif // CAMERA_SCOPED_TRACE_H
diff --git a/modules/camera/Stream.cpp b/modules/camera/Stream.cpp
index aae7adb..9b9ab98 100644
--- a/modules/camera/Stream.cpp
+++ b/modules/camera/Stream.cpp
@@ -14,18 +14,18 @@
  * limitations under the License.
  */
 
-#include <pthread.h>
+#include <stdio.h>
 #include <hardware/camera3.h>
 #include <hardware/gralloc.h>
 #include <system/graphics.h>
+#include <utils/Mutex.h>
 
 //#define LOG_NDEBUG 0
 #define LOG_TAG "Stream"
 #include <cutils/log.h>
 
 #define ATRACE_TAG (ATRACE_TAG_CAMERA | ATRACE_TAG_HAL)
-#include <cutils/trace.h>
-#include "ScopedTrace.h"
+#include <utils/Trace.h>
 
 #include "Stream.h"
 
@@ -45,37 +45,32 @@
     mBuffers(0),
     mNumBuffers(0)
 {
-    // NULL (default) pthread mutex attributes
-    pthread_mutex_init(&mMutex, NULL);
 }
 
 Stream::~Stream()
 {
-    pthread_mutex_lock(&mMutex);
+    android::Mutex::Autolock al(mLock);
     unregisterBuffers_L();
-    pthread_mutex_unlock(&mMutex);
 }
 
 void Stream::setUsage(uint32_t usage)
 {
-    pthread_mutex_lock(&mMutex);
+    android::Mutex::Autolock al(mLock);
     if (usage != mUsage) {
         mUsage = usage;
         mStream->usage = usage;
         unregisterBuffers_L();
     }
-    pthread_mutex_unlock(&mMutex);
 }
 
 void Stream::setMaxBuffers(uint32_t max_buffers)
 {
-    pthread_mutex_lock(&mMutex);
+    android::Mutex::Autolock al(mLock);
     if (max_buffers != mMaxBuffers) {
         mMaxBuffers = max_buffers;
         mStream->max_buffers = max_buffers;
         unregisterBuffers_L();
     }
-    pthread_mutex_unlock(&mMutex);
 }
 
 int Stream::getType()
@@ -95,6 +90,61 @@
         mType == CAMERA3_STREAM_BIDIRECTIONAL;
 }
 
+const char* Stream::typeToString(int type)
+{
+    switch (type) {
+    case CAMERA3_STREAM_INPUT:
+        return "CAMERA3_STREAM_INPUT";
+    case CAMERA3_STREAM_OUTPUT:
+        return "CAMERA3_STREAM_OUTPUT";
+    case CAMERA3_STREAM_BIDIRECTIONAL:
+        return "CAMERA3_STREAM_BIDIRECTIONAL";
+    }
+    return "Invalid stream type!";
+}
+
+const char* Stream::formatToString(int format)
+{
+    // See <system/graphics.h> for full list
+    switch (format) {
+    case HAL_PIXEL_FORMAT_BGRA_8888:
+        return "BGRA 8888";
+    case HAL_PIXEL_FORMAT_RGBA_8888:
+        return "RGBA 8888";
+    case HAL_PIXEL_FORMAT_RGBX_8888:
+        return "RGBX 8888";
+    case HAL_PIXEL_FORMAT_RGB_888:
+        return "RGB 888";
+    case HAL_PIXEL_FORMAT_RGB_565:
+        return "RGB 565";
+    case HAL_PIXEL_FORMAT_sRGB_A_8888:
+        return "sRGB A 8888";
+    case HAL_PIXEL_FORMAT_sRGB_X_8888:
+        return "sRGB B 8888";
+    case HAL_PIXEL_FORMAT_Y8:
+        return "Y8";
+    case HAL_PIXEL_FORMAT_Y16:
+        return "Y16";
+    case HAL_PIXEL_FORMAT_YV12:
+        return "YV12";
+    case HAL_PIXEL_FORMAT_YCbCr_422_SP:
+        return "NV16";
+    case HAL_PIXEL_FORMAT_YCrCb_420_SP:
+        return "NV21";
+    case HAL_PIXEL_FORMAT_YCbCr_422_I:
+        return "YUY2";
+    case HAL_PIXEL_FORMAT_RAW_SENSOR:
+        return "RAW SENSOR";
+    case HAL_PIXEL_FORMAT_BLOB:
+        return "BLOB";
+    case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED:
+        return "IMPLEMENTATION DEFINED";
+    case HAL_PIXEL_FORMAT_YCbCr_420_888:
+        return "FLEXIBLE YCbCr 420 888";
+    }
+    return "Invalid stream format!";
+}
+
 bool Stream::isRegistered()
 {
     return mRegistered;
@@ -113,15 +163,15 @@
         return false;
     }
     if (s->stream_type != mType) {
-        // TODO: prettyprint type string
-        ALOGE("%s:%d: Mismatched type in reused stream. Got %d expect %d",
-                __func__, mId, s->stream_type, mType);
+        ALOGE("%s:%d: Mismatched type in reused stream. Got %s(%d) "
+                "expect %s(%d)", __func__, mId, typeToString(s->stream_type),
+                s->stream_type, typeToString(mType), mType);
         return false;
     }
     if (s->format != mFormat) {
-        // TODO: prettyprint format string
-        ALOGE("%s:%d: Mismatched format in reused stream. Got %d expect %d",
-                __func__, mId, s->format, mFormat);
+        ALOGE("%s:%d: Mismatched format in reused stream. Got %s(%d) "
+                "expect %s(%d)", __func__, mId, formatToString(s->format),
+                s->format, formatToString(mFormat), mFormat);
         return false;
     }
     if (s->width != mWidth) {
@@ -139,7 +189,8 @@
 
 int Stream::registerBuffers(const camera3_stream_buffer_set_t *buf_set)
 {
-    CAMTRACE_CALL();
+    ATRACE_CALL();
+    android::Mutex::Autolock al(mLock);
 
     if (buf_set->stream != mStream) {
         ALOGE("%s:%d: Buffer set for invalid stream. Got %p expect %p",
@@ -147,8 +198,6 @@
         return -EINVAL;
     }
 
-    pthread_mutex_lock(&mMutex);
-
     mNumBuffers = buf_set->num_buffers;
     mBuffers = new buffer_handle_t*[mNumBuffers];
 
@@ -160,12 +209,10 @@
     }
     mRegistered = true;
 
-    pthread_mutex_unlock(&mMutex);
-
     return 0;
 }
 
-// This must only be called with mMutex held
+// This must only be called with mLock held
 void Stream::unregisterBuffers_L()
 {
     mRegistered = false;
@@ -174,4 +221,22 @@
     // TODO: unregister buffers from hw
 }
 
+void Stream::dump(int fd)
+{
+    android::Mutex::Autolock al(mLock);
+
+    fdprintf(fd, "Stream ID: %d (%p)\n", mId, mStream);
+    fdprintf(fd, "Stream Type: %s (%d)\n", typeToString(mType), mType);
+    fdprintf(fd, "Width: %u Height: %u\n", mWidth, mHeight);
+    fdprintf(fd, "Stream Format: %s (%d)", formatToString(mFormat), mFormat);
+    // ToDo: prettyprint usage mask flags
+    fdprintf(fd, "Gralloc Usage Mask: 0x%x\n", mUsage);
+    fdprintf(fd, "Max Buffer Count: %d\n", mMaxBuffers);
+    fdprintf(fd, "Buffers Registered: %s\n", mRegistered ? "true" : "false");
+    fdprintf(fd, "Number of Buffers: %d\n", mNumBuffers);
+    for (int i = 0; i < mNumBuffers; i++) {
+        fdprintf(fd, "Buffer %d/%d: %p\n", i, mNumBuffers, mBuffers[i]);
+    }
+}
+
 } // namespace default_camera_hal
diff --git a/modules/camera/Stream.h b/modules/camera/Stream.h
index 34abd95..5efbc52 100644
--- a/modules/camera/Stream.h
+++ b/modules/camera/Stream.h
@@ -20,6 +20,7 @@
 #include <hardware/camera3.h>
 #include <hardware/gralloc.h>
 #include <system/graphics.h>
+#include <utils/Mutex.h>
 
 namespace default_camera_hal {
 // Stream represents a single input or output stream for a camera device.
@@ -41,12 +42,15 @@
         bool isInputType();
         bool isOutputType();
         bool isRegistered();
+        const char* typeToString(int type);
+        const char* formatToString(int format);
+        void dump(int fd);
 
         // This stream is being reused. Used in stream configuration passes
         bool mReuse;
 
     private:
-        // Clean up buffer state. must be called with mMutex held.
+        // Clean up buffer state. must be called with mLock held.
         void unregisterBuffers_L();
 
         // The camera device id this stream belongs to
@@ -72,7 +76,7 @@
         // Number of buffers in mBuffers
         unsigned int mNumBuffers;
         // Lock protecting the Stream object for modifications
-        pthread_mutex_t mMutex;
+        android::Mutex mLock;
 };
 } // namespace default_camera_hal
 
diff --git a/modules/camera/VendorTags.cpp b/modules/camera/VendorTags.cpp
new file mode 100644
index 0000000..58bd606
--- /dev/null
+++ b/modules/camera/VendorTags.cpp
@@ -0,0 +1,188 @@
+/*
+ * 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.
+ */
+
+#include <system/camera_metadata.h>
+#include "Metadata.h"
+
+//#define LOG_NDEBUG 0
+#define LOG_TAG "VendorTags"
+#include <cutils/log.h>
+
+#define ATRACE_TAG (ATRACE_TAG_CAMERA | ATRACE_TAG_HAL)
+#include <utils/Trace.h>
+
+#include "VendorTags.h"
+
+namespace default_camera_hal {
+
+// Internal representations of vendor tags for convenience.
+// Other classes must access this data via public interfaces.
+// Structured to be easy to extend and contain complexity.
+namespace {
+// Describes a single vendor tag entry
+struct Entry {
+    const char* name;
+    uint8_t     type;
+};
+// Describes a vendor tag section
+struct Section {
+    const char* name;
+    uint32_t start;
+    uint32_t end;
+    const Entry* tags;
+};
+
+// Entry arrays for each section
+const Entry DemoWizardry[DEMO_WIZARDRY_END - DEMO_WIZARDRY_START] = {
+    [DEMO_WIZARDRY_DIMENSION_SIZE - DEMO_WIZARDRY_START] =
+        {"dimensionSize",   TYPE_INT32},
+    [DEMO_WIZARDRY_DIMENSIONS - DEMO_WIZARDRY_START] =
+        {"dimensions",      TYPE_INT32},
+    [DEMO_WIZARDRY_FAMILIAR - DEMO_WIZARDRY_START] =
+        {"familiar",        TYPE_BYTE},
+    [DEMO_WIZARDRY_FIRE - DEMO_WIZARDRY_START] =
+        {"fire",            TYPE_RATIONAL}
+};
+
+const Entry DemoSorcery[DEMO_SORCERY_END - DEMO_SORCERY_START] = {
+    [DEMO_SORCERY_DIFFICULTY - DEMO_SORCERY_START] =
+        {"difficulty",      TYPE_INT64},
+    [DEMO_SORCERY_LIGHT - DEMO_SORCERY_START] =
+        {"light",           TYPE_BYTE}
+};
+
+const Entry DemoMagic[DEMO_MAGIC_END - DEMO_MAGIC_START] = {
+    [DEMO_MAGIC_CARD_TRICK - DEMO_MAGIC_START] =
+        {"cardTrick",       TYPE_DOUBLE},
+    [DEMO_MAGIC_LEVITATION - DEMO_MAGIC_START] =
+        {"levitation",      TYPE_FLOAT}
+};
+
+// Array of all sections
+const Section DemoSections[DEMO_SECTION_COUNT] = {
+    [DEMO_WIZARDRY] = { "demo.wizardry",
+                        DEMO_WIZARDRY_START,
+                        DEMO_WIZARDRY_END,
+                        DemoWizardry },
+    [DEMO_SORCERY]  = { "demo.sorcery",
+                        DEMO_SORCERY_START,
+                        DEMO_SORCERY_END,
+                        DemoSorcery },
+    [DEMO_MAGIC]    = { "demo.magic",
+                        DEMO_MAGIC_START,
+                        DEMO_MAGIC_END,
+                        DemoMagic }
+};
+
+// Get a static handle to a specific vendor tag section
+const Section* getSection(uint32_t tag)
+{
+    uint32_t section = (tag - VENDOR_SECTION_START) >> 16;
+
+    if (tag < VENDOR_SECTION_START) {
+        ALOGE("%s: Tag 0x%x before vendor section", __func__, tag);
+        return NULL;
+    }
+
+    if (section >= DEMO_SECTION_COUNT) {
+        ALOGE("%s: Tag 0x%x after vendor section", __func__, tag);
+        return NULL;
+    }
+
+    return &DemoSections[section];
+}
+
+// Get a static handle to a specific vendor tag entry
+const Entry* getEntry(uint32_t tag)
+{
+    const Section* section = getSection(tag);
+    int index;
+
+    if (section == NULL)
+        return NULL;
+
+    if (tag >= section->end) {
+        ALOGE("%s: Tag 0x%x outside section", __func__, tag);
+        return NULL;
+    }
+
+    index = tag - section->start;
+    return &section->tags[index];
+}
+} // namespace
+
+VendorTags::VendorTags()
+  : mTagCount(0)
+{
+    for (int i = 0; i < DEMO_SECTION_COUNT; i++) {
+        mTagCount += DemoSections[i].end - DemoSections[i].start;
+    }
+}
+
+VendorTags::~VendorTags()
+{
+}
+
+int VendorTags::getTagCount(const vendor_tag_ops_t* ops)
+{
+    return mTagCount;
+}
+
+void VendorTags::getAllTags(const vendor_tag_ops_t* ops, uint32_t* tag_array)
+{
+    if (tag_array == NULL) {
+        ALOGE("%s: NULL tag_array", __func__);
+        return;
+    }
+
+    for (int i = 0; i < DEMO_SECTION_COUNT; i++) {
+        for (uint32_t tag = DemoSections[i].start;
+                tag < DemoSections[i].end; tag++) {
+            *tag_array++ = tag;
+        }
+    }
+}
+
+const char* VendorTags::getSectionName(const vendor_tag_ops_t* ops, uint32_t tag)
+{
+    const Section* section = getSection(tag);
+
+    if (section == NULL)
+        return NULL;
+
+    return section->name;
+}
+
+const char* VendorTags::getTagName(const vendor_tag_ops_t* ops, uint32_t tag)
+{
+    const Entry* entry = getEntry(tag);
+
+    if (entry == NULL)
+        return NULL;
+
+    return entry->name;
+}
+
+int VendorTags::getTagType(const vendor_tag_ops_t* ops, uint32_t tag)
+{
+    const Entry* entry = getEntry(tag);
+
+    if (entry == NULL)
+        return -1;
+
+    return entry->type;
+}
+} // namespace default_camera_hal
diff --git a/modules/camera/VendorTags.h b/modules/camera/VendorTags.h
new file mode 100644
index 0000000..9af9f7d
--- /dev/null
+++ b/modules/camera/VendorTags.h
@@ -0,0 +1,77 @@
+/*
+ * 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 VENDOR_TAGS_H_
+#define VENDOR_TAGS_H_
+
+#include <hardware/camera_common.h>
+#include <system/camera_metadata.h>
+
+namespace default_camera_hal {
+
+// VendorTags contains all vendor-specific metadata tag functionality
+class VendorTags {
+    public:
+        VendorTags();
+        ~VendorTags();
+
+        // Vendor Tags Operations (see <hardware/camera_common.h>)
+        int getTagCount(const vendor_tag_ops_t* ops);
+        void getAllTags(const vendor_tag_ops_t* ops, uint32_t* tag_array);
+        const char* getSectionName(const vendor_tag_ops_t* ops, uint32_t tag);
+        const char* getTagName(const vendor_tag_ops_t* ops, uint32_t tag);
+        int getTagType(const vendor_tag_ops_t* ops, uint32_t tag);
+
+    private:
+        // Total number of vendor tags
+        int mTagCount;
+};
+
+// Tag sections start at the beginning of vendor tags (0x8000_0000)
+// See <system/camera_metadata.h>
+enum {
+    DEMO_WIZARDRY,
+    DEMO_SORCERY,
+    DEMO_MAGIC,
+    DEMO_SECTION_COUNT
+};
+
+// Each section starts at increments of 0x1_0000
+enum {
+    DEMO_WIZARDRY_START = (DEMO_WIZARDRY + VENDOR_SECTION) << 16,
+    DEMO_SORCERY_START  = (DEMO_SORCERY  + VENDOR_SECTION) << 16,
+    DEMO_MAGIC_START    = (DEMO_MAGIC    + VENDOR_SECTION) << 16,
+};
+
+// Vendor Tag values, start value begins each section
+enum {
+    DEMO_WIZARDRY_DIMENSION_SIZE = DEMO_WIZARDRY_START,
+    DEMO_WIZARDRY_DIMENSIONS,
+    DEMO_WIZARDRY_FAMILIAR,
+    DEMO_WIZARDRY_FIRE,
+    DEMO_WIZARDRY_END,
+
+    DEMO_SORCERY_DIFFICULTY = DEMO_SORCERY_START,
+    DEMO_SORCERY_LIGHT,
+    DEMO_SORCERY_END,
+
+    DEMO_MAGIC_CARD_TRICK = DEMO_MAGIC_START,
+    DEMO_MAGIC_LEVITATION,
+    DEMO_MAGIC_END,
+};
+} // namespace default_camera_hal
+
+#endif // VENDOR_TAGS_H_
diff --git a/tests/hardware/Android.mk b/tests/hardware/Android.mk
new file mode 100644
index 0000000..02540c9
--- /dev/null
+++ b/tests/hardware/Android.mk
@@ -0,0 +1,12 @@
+LOCAL_PATH:= $(call my-dir)
+include $(CLEAR_VARS)
+
+LOCAL_MODULE := static-hal-check
+LOCAL_SRC_FILES := struct-size.cpp struct-offset.cpp struct-last.cpp
+LOCAL_SHARED_LIBRARIES := libhardware
+LOCAL_CFLAGS := -std=gnu++11 -O0
+
+LOCAL_C_INCLUDES += \
+    system/media/camera/include
+
+include $(BUILD_STATIC_LIBRARY)
diff --git a/tests/hardware/struct-last.cpp b/tests/hardware/struct-last.cpp
new file mode 100644
index 0000000..44a7b2d
--- /dev/null
+++ b/tests/hardware/struct-last.cpp
@@ -0,0 +1,69 @@
+/*
+ * 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.
+ */
+
+#include <cstddef>
+#include <system/window.h>
+#include <hardware/hardware.h>
+#include <hardware/sensors.h>
+#include <hardware/fb.h>
+#include <hardware/hwcomposer.h>
+#include <hardware/gralloc.h>
+#include <hardware/consumerir.h>
+#include <hardware/camera_common.h>
+#include <hardware/camera3.h>
+
+#define GET_PADDING(align, size) (((align) - ((size) % (align))) % (align))
+
+#define CHECK_LAST_MEMBER(type, member) \
+do { \
+static constexpr size_t calc_size = offsetof(type, member) + sizeof(((type *)0)->member); \
+static_assert(sizeof(type) == calc_size + GET_PADDING(alignof(type), calc_size), \
+"" #member " is not the last element of " #type); \
+} while (0)
+
+void CheckSizes(void) {
+    //Types defined in hardware.h
+    CHECK_LAST_MEMBER(hw_module_t, reserved);
+    CHECK_LAST_MEMBER(hw_device_t, close);
+
+    //Types defined in sensors.h
+    CHECK_LAST_MEMBER(sensors_vec_t, reserved);
+    CHECK_LAST_MEMBER(sensors_event_t, reserved1);
+    CHECK_LAST_MEMBER(struct sensor_t, reserved);
+    CHECK_LAST_MEMBER(sensors_poll_device_1_t, reserved_procs);
+
+    //Types defined in fb.h
+    CHECK_LAST_MEMBER(framebuffer_device_t, reserved_proc);
+
+    //Types defined in hwcomposer.h
+    CHECK_LAST_MEMBER(hwc_layer_1_t, reserved);
+    CHECK_LAST_MEMBER(hwc_composer_device_1_t, reserved_proc);
+
+    //Types defined in gralloc.h
+    CHECK_LAST_MEMBER(gralloc_module_t, reserved_proc);
+    CHECK_LAST_MEMBER(alloc_device_t, reserved_proc);
+
+    //Types defined in consumerir.h
+    CHECK_LAST_MEMBER(consumerir_device_t, reserved);
+
+    //Types defined in camera_common.h
+    CHECK_LAST_MEMBER(vendor_tag_ops_t, reserved);
+    CHECK_LAST_MEMBER(camera_module_t, reserved);
+
+    //Types defined in camera3.h
+    CHECK_LAST_MEMBER(camera3_device_ops_t, reserved);
+}
+
diff --git a/tests/hardware/struct-offset.cpp b/tests/hardware/struct-offset.cpp
new file mode 100644
index 0000000..0a1338a
--- /dev/null
+++ b/tests/hardware/struct-offset.cpp
@@ -0,0 +1,216 @@
+/*
+ * Copyright (C) 2012 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 <cstddef>
+#include <system/window.h>
+#include <hardware/hardware.h>
+#include <hardware/sensors.h>
+#include <hardware/fb.h>
+#include <hardware/hwcomposer.h>
+#include <hardware/gralloc.h>
+#include <hardware/consumerir.h>
+#include <hardware/camera_common.h>
+#include <hardware/camera3.h>
+
+//Ideally this would print type.member instead we need to rely on the line number from the output
+template <size_t actual, size_t expected> void check_member(void) {
+    static_assert(actual == expected, "");
+}
+
+#ifdef __LP64__
+#define CHECK_MEMBER_AT(type, member, off32, off64) \
+    check_member<offsetof(type, member), off64>()
+#else
+#define CHECK_MEMBER_AT(type, member, off32, off64) \
+    check_member<offsetof(type, member), off32>()
+#endif
+
+void CheckOffsets(void) {
+    //Types defined in hardware.h
+    CHECK_MEMBER_AT(hw_module_t, tag, 0, 0);
+    CHECK_MEMBER_AT(hw_module_t, module_api_version, 4, 4);
+    CHECK_MEMBER_AT(hw_module_t, hal_api_version, 6, 6);
+    CHECK_MEMBER_AT(hw_module_t, id, 8, 8);
+    CHECK_MEMBER_AT(hw_module_t, name, 12, 16);
+    CHECK_MEMBER_AT(hw_module_t, author, 16, 24);
+    CHECK_MEMBER_AT(hw_module_t, methods, 20, 32);
+    CHECK_MEMBER_AT(hw_module_t, dso, 24, 40);
+    CHECK_MEMBER_AT(hw_module_t, reserved, 28, 48);
+
+    CHECK_MEMBER_AT(hw_device_t, tag, 0, 0);
+    CHECK_MEMBER_AT(hw_device_t, version, 4, 4);
+    CHECK_MEMBER_AT(hw_device_t, module, 8, 8);
+    CHECK_MEMBER_AT(hw_device_t, reserved, 12, 16);
+    CHECK_MEMBER_AT(hw_device_t, close, 60, 112);
+
+    //Types defined in sensors.h
+    CHECK_MEMBER_AT(sensors_vec_t, v, 0, 0);
+    CHECK_MEMBER_AT(sensors_vec_t, x, 0, 0);
+    CHECK_MEMBER_AT(sensors_vec_t, y, 4, 4);
+    CHECK_MEMBER_AT(sensors_vec_t, z, 8, 8);
+    CHECK_MEMBER_AT(sensors_vec_t, azimuth, 0, 0);
+    CHECK_MEMBER_AT(sensors_vec_t, pitch, 4, 4);
+    CHECK_MEMBER_AT(sensors_vec_t, roll, 8, 8);
+    CHECK_MEMBER_AT(sensors_vec_t, status, 12, 12);
+    CHECK_MEMBER_AT(sensors_vec_t, reserved, 13, 13);
+
+    CHECK_MEMBER_AT(sensors_event_t, version, 0, 0);
+    CHECK_MEMBER_AT(sensors_event_t, sensor, 4, 4);
+    CHECK_MEMBER_AT(sensors_event_t, type, 8, 8);
+    CHECK_MEMBER_AT(sensors_event_t, reserved0, 12, 12);
+    CHECK_MEMBER_AT(sensors_event_t, timestamp, 16, 16);
+    CHECK_MEMBER_AT(sensors_event_t, data, 24, 24);
+    CHECK_MEMBER_AT(sensors_event_t, acceleration, 24, 24);
+    CHECK_MEMBER_AT(sensors_event_t, magnetic, 24, 24);
+    CHECK_MEMBER_AT(sensors_event_t, orientation, 24, 24);
+    CHECK_MEMBER_AT(sensors_event_t, gyro, 24, 24);
+    CHECK_MEMBER_AT(sensors_event_t, temperature, 24, 24);
+    CHECK_MEMBER_AT(sensors_event_t, distance, 24, 24);
+    CHECK_MEMBER_AT(sensors_event_t, light, 24, 24);
+    CHECK_MEMBER_AT(sensors_event_t, pressure, 24, 24);
+    CHECK_MEMBER_AT(sensors_event_t, relative_humidity, 24, 24);
+    CHECK_MEMBER_AT(sensors_event_t, uncalibrated_gyro, 24, 24);
+    CHECK_MEMBER_AT(sensors_event_t, uncalibrated_magnetic, 24, 24);
+    CHECK_MEMBER_AT(sensors_event_t, meta_data, 24, 24);
+    CHECK_MEMBER_AT(sensors_event_t, u64, 24, 24);
+    CHECK_MEMBER_AT(sensors_event_t, u64.data, 24, 24);
+    CHECK_MEMBER_AT(sensors_event_t, u64.step_counter, 24, 24);
+    CHECK_MEMBER_AT(sensors_event_t, reserved1, 88, 88);
+
+    CHECK_MEMBER_AT(struct sensor_t, name, 0, 0);
+    CHECK_MEMBER_AT(struct sensor_t, vendor, 4, 8);
+    CHECK_MEMBER_AT(struct sensor_t, version, 8, 16);
+    CHECK_MEMBER_AT(struct sensor_t, handle, 12, 20);
+    CHECK_MEMBER_AT(struct sensor_t, type, 16, 24);
+    CHECK_MEMBER_AT(struct sensor_t, maxRange, 20, 28);
+    CHECK_MEMBER_AT(struct sensor_t, resolution, 24, 32);
+    CHECK_MEMBER_AT(struct sensor_t, power, 28, 36);
+    CHECK_MEMBER_AT(struct sensor_t, minDelay, 32, 40);
+    CHECK_MEMBER_AT(struct sensor_t, fifoReservedEventCount, 36, 44);
+    CHECK_MEMBER_AT(struct sensor_t, fifoMaxEventCount, 40, 48);
+    CHECK_MEMBER_AT(struct sensor_t, reserved, 44, 56);
+
+    CHECK_MEMBER_AT(sensors_poll_device_1_t, v0, 0, 0);
+    CHECK_MEMBER_AT(sensors_poll_device_1_t, common, 0, 0);
+    CHECK_MEMBER_AT(sensors_poll_device_1_t, activate, 64, 120);
+    CHECK_MEMBER_AT(sensors_poll_device_1_t, setDelay, 68, 128);
+    CHECK_MEMBER_AT(sensors_poll_device_1_t, poll, 72, 136);
+    CHECK_MEMBER_AT(sensors_poll_device_1_t, batch, 76, 144);
+    CHECK_MEMBER_AT(sensors_poll_device_1_t, flush, 80, 152);
+    CHECK_MEMBER_AT(sensors_poll_device_1_t, reserved_procs, 84, 160);
+
+    //Types defined in fb.h
+    CHECK_MEMBER_AT(framebuffer_device_t, common, 0, 0);
+    CHECK_MEMBER_AT(framebuffer_device_t, flags, 64, 120);
+    CHECK_MEMBER_AT(framebuffer_device_t, width, 68, 124);
+    CHECK_MEMBER_AT(framebuffer_device_t, height, 72, 128);
+    CHECK_MEMBER_AT(framebuffer_device_t, stride, 76, 132);
+    CHECK_MEMBER_AT(framebuffer_device_t, format, 80, 136);
+    CHECK_MEMBER_AT(framebuffer_device_t, xdpi, 84, 140);
+    CHECK_MEMBER_AT(framebuffer_device_t, ydpi, 88, 144);
+    CHECK_MEMBER_AT(framebuffer_device_t, fps, 92, 148);
+    CHECK_MEMBER_AT(framebuffer_device_t, minSwapInterval, 96, 152);
+    CHECK_MEMBER_AT(framebuffer_device_t, maxSwapInterval, 100, 156);
+    CHECK_MEMBER_AT(framebuffer_device_t, numFramebuffers, 104, 160);
+    CHECK_MEMBER_AT(framebuffer_device_t, reserved, 108, 164);
+    CHECK_MEMBER_AT(framebuffer_device_t, setSwapInterval, 136, 192);
+    CHECK_MEMBER_AT(framebuffer_device_t, setUpdateRect, 140, 200);
+    CHECK_MEMBER_AT(framebuffer_device_t, post, 144, 208);
+    CHECK_MEMBER_AT(framebuffer_device_t, compositionComplete, 148, 216);
+    CHECK_MEMBER_AT(framebuffer_device_t, dump, 152, 224);
+    CHECK_MEMBER_AT(framebuffer_device_t, enableScreen, 156, 232);
+    CHECK_MEMBER_AT(framebuffer_device_t, reserved_proc, 160, 240);
+
+    //Types defined in hwcomposer.h
+    CHECK_MEMBER_AT(hwc_layer_1_t, compositionType, 0, 0);
+    CHECK_MEMBER_AT(hwc_layer_1_t, hints, 4, 4);
+    CHECK_MEMBER_AT(hwc_layer_1_t, flags, 8, 8);
+    CHECK_MEMBER_AT(hwc_layer_1_t, backgroundColor, 12, 16);
+    CHECK_MEMBER_AT(hwc_layer_1_t, handle, 12, 16);
+    CHECK_MEMBER_AT(hwc_layer_1_t, transform, 16, 24);
+    CHECK_MEMBER_AT(hwc_layer_1_t, blending, 20, 28);
+    CHECK_MEMBER_AT(hwc_layer_1_t, sourceCropi, 24, 32);
+    CHECK_MEMBER_AT(hwc_layer_1_t, sourceCrop, 24, 32);
+    CHECK_MEMBER_AT(hwc_layer_1_t, sourceCropf, 24, 32);
+    CHECK_MEMBER_AT(hwc_layer_1_t, displayFrame, 40, 48);
+    CHECK_MEMBER_AT(hwc_layer_1_t, visibleRegionScreen, 56, 64);
+    CHECK_MEMBER_AT(hwc_layer_1_t, acquireFenceFd, 64, 80);
+    CHECK_MEMBER_AT(hwc_layer_1_t, releaseFenceFd, 68, 84);
+    CHECK_MEMBER_AT(hwc_layer_1_t, planeAlpha, 72, 88);
+    CHECK_MEMBER_AT(hwc_layer_1_t, _pad, 73, 89);
+
+    CHECK_MEMBER_AT(hwc_composer_device_1_t, common, 0, 0);
+    CHECK_MEMBER_AT(hwc_composer_device_1_t, prepare, 64, 120);
+    CHECK_MEMBER_AT(hwc_composer_device_1_t, set, 68, 128);
+    CHECK_MEMBER_AT(hwc_composer_device_1_t, eventControl, 72, 136);
+    CHECK_MEMBER_AT(hwc_composer_device_1_t, blank, 76, 144);
+    CHECK_MEMBER_AT(hwc_composer_device_1_t, query, 80, 152);
+    CHECK_MEMBER_AT(hwc_composer_device_1_t, registerProcs, 84, 160);
+    CHECK_MEMBER_AT(hwc_composer_device_1_t, dump, 88, 168);
+    CHECK_MEMBER_AT(hwc_composer_device_1_t, getDisplayConfigs, 92, 176);
+    CHECK_MEMBER_AT(hwc_composer_device_1_t, getDisplayAttributes, 96, 184);
+    CHECK_MEMBER_AT(hwc_composer_device_1_t, reserved_proc, 100, 192);
+
+    //Types defined in gralloc.h
+    CHECK_MEMBER_AT(gralloc_module_t, common, 0, 0);
+    CHECK_MEMBER_AT(gralloc_module_t, registerBuffer, 128, 248);
+    CHECK_MEMBER_AT(gralloc_module_t, unregisterBuffer, 132, 256);
+    CHECK_MEMBER_AT(gralloc_module_t, lock, 136, 264);
+    CHECK_MEMBER_AT(gralloc_module_t, unlock, 140, 272);
+    CHECK_MEMBER_AT(gralloc_module_t, perform, 144, 280);
+    CHECK_MEMBER_AT(gralloc_module_t, lock_ycbcr, 148, 288);
+    CHECK_MEMBER_AT(gralloc_module_t, reserved_proc, 152, 296);
+
+    CHECK_MEMBER_AT(alloc_device_t, common, 0, 0);
+    CHECK_MEMBER_AT(alloc_device_t, alloc, 64, 120);
+    CHECK_MEMBER_AT(alloc_device_t, free, 68, 128);
+    CHECK_MEMBER_AT(alloc_device_t, dump, 72, 136);
+    CHECK_MEMBER_AT(alloc_device_t, reserved_proc, 76, 144);
+
+    //Types defined in consumerir.h
+    CHECK_MEMBER_AT(consumerir_device_t, common, 0, 0);
+    CHECK_MEMBER_AT(consumerir_device_t, transmit, 64, 120);
+    CHECK_MEMBER_AT(consumerir_device_t, get_num_carrier_freqs, 68, 128);
+    CHECK_MEMBER_AT(consumerir_device_t, get_carrier_freqs, 72, 136);
+    CHECK_MEMBER_AT(consumerir_device_t, reserved, 76, 144);
+
+    //Types defined in camera_common.h
+    CHECK_MEMBER_AT(vendor_tag_ops_t, get_tag_count, 0, 0);
+    CHECK_MEMBER_AT(vendor_tag_ops_t, get_all_tags, 4, 8);
+    CHECK_MEMBER_AT(vendor_tag_ops_t, get_section_name, 8, 16);
+    CHECK_MEMBER_AT(vendor_tag_ops_t, get_tag_name, 12, 24);
+    CHECK_MEMBER_AT(vendor_tag_ops_t, get_tag_type, 16, 32);
+    CHECK_MEMBER_AT(vendor_tag_ops_t, reserved, 20, 40);
+
+    CHECK_MEMBER_AT(camera_module_t, common, 0, 0);
+    CHECK_MEMBER_AT(camera_module_t, get_number_of_cameras, 128, 248);
+    CHECK_MEMBER_AT(camera_module_t, get_camera_info, 132, 256);
+    CHECK_MEMBER_AT(camera_module_t, set_callbacks, 136, 264);
+    CHECK_MEMBER_AT(camera_module_t, get_vendor_tag_ops, 140, 272);
+    CHECK_MEMBER_AT(camera_module_t, reserved, 144, 280);
+
+    //Types defined in camera3.h
+    CHECK_MEMBER_AT(camera3_device_ops_t, initialize, 0, 0);
+    CHECK_MEMBER_AT(camera3_device_ops_t, configure_streams, 4, 8);
+    CHECK_MEMBER_AT(camera3_device_ops_t, register_stream_buffers, 8, 16);
+    CHECK_MEMBER_AT(camera3_device_ops_t, construct_default_request_settings, 12, 24);
+    CHECK_MEMBER_AT(camera3_device_ops_t, process_capture_request, 16, 32);
+    CHECK_MEMBER_AT(camera3_device_ops_t, get_metadata_vendor_tag_ops, 20, 40);
+    CHECK_MEMBER_AT(camera3_device_ops_t, dump, 24, 48);
+    CHECK_MEMBER_AT(camera3_device_ops_t, flush, 28, 56);
+    CHECK_MEMBER_AT(camera3_device_ops_t, reserved, 32, 64);
+}
+
diff --git a/tests/hardware/struct-size.cpp b/tests/hardware/struct-size.cpp
new file mode 100644
index 0000000..4207ea8
--- /dev/null
+++ b/tests/hardware/struct-size.cpp
@@ -0,0 +1,76 @@
+/*
+ * Copyright (C) 2012 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/window.h>
+#include <hardware/hardware.h>
+#include <hardware/sensors.h>
+#include <hardware/fb.h>
+#include <hardware/hwcomposer.h>
+#include <hardware/gralloc.h>
+#include <hardware/consumerir.h>
+#include <hardware/camera_common.h>
+#include <hardware/camera3.h>
+
+template<size_t> static constexpr size_t CheckSizeHelper(size_t, size_t);
+
+template<> constexpr size_t CheckSizeHelper<4>(size_t size32, size_t size64) {
+    return size32;
+}
+
+template<> constexpr size_t CheckSizeHelper<8>(size_t size32, size_t size64) {
+    return size64;
+}
+
+template<typename T, size_t size32, size_t size64> static void CheckTypeSize() {
+    const size_t mySize = CheckSizeHelper<sizeof(void *)>(size32, size64);
+
+    static_assert(sizeof(T) == mySize, "struct is the wrong size");
+}
+
+void CheckSizes(void) {
+    //Types defined in hardware.h
+    CheckTypeSize<hw_module_t, 128, 248>();
+    CheckTypeSize<hw_device_t, 64, 120>();
+
+    //Types defined in sensors.h
+    CheckTypeSize<sensors_vec_t, 16, 16>();
+    CheckTypeSize<sensors_event_t, 104, 104>();
+    CheckTypeSize<struct sensor_t, 68, 104>();
+    CheckTypeSize<sensors_poll_device_1_t, 116, 224>();
+
+    //Types defined in fb.h
+    CheckTypeSize<framebuffer_device_t, 184, 288>();
+
+    //Types defined in hwcomposer.h
+    CheckTypeSize<hwc_layer_1_t, 96, 120>();
+    CheckTypeSize<hwc_composer_device_1_t, 116, 224>();
+
+    //Types defined in gralloc.h
+    CheckTypeSize<gralloc_module_t, 176, 344>();
+    CheckTypeSize<alloc_device_t, 104, 200>();
+
+    //Types defined in consumerir.h
+    CheckTypeSize<consumerir_device_t, 96, 184>();
+
+    //Types defined in camera_common.h
+    CheckTypeSize<vendor_tag_ops_t, 52, 104>();
+    CheckTypeSize<camera_module_t, 176, 344>();
+
+    //Types defined in camera3.h
+    CheckTypeSize<camera3_device_ops_t, 64, 128>();
+}
+