[sensors] new functions and types for direct report mode

Added functions:

  * registerDirectChannel
  * unregisterDirectChannel
  * configDirectReport

Added structs and enums related to direct report functionality.

Stub default implementation is also included.

Test: compiles and runs
Bug: 30985702
Change-Id: Ia542673e2b180e0aa62c1374cf8c1671bf2f5721
diff --git a/sensors/1.0/ISensors.hal b/sensors/1.0/ISensors.hal
index adacfe0..bb57f73 100644
--- a/sensors/1.0/ISensors.hal
+++ b/sensors/1.0/ISensors.hal
@@ -121,4 +121,60 @@
      *         BAD_VALUE if sensor event cannot be injected
      */
     injectSensorData(Event event) generates (Result result);
+
+    /*
+     * Register direct report channel.
+     *
+     * Register a direct channel with supplied shared memory information. Upon
+     * return, the sensor hardware is responsible for resetting the memory
+     * content to initial value (depending on memory format settings).
+     *
+     * @param   mem shared memory info data structure.
+     * @return  result OK on success; BAD_VALUE if shared memory information is
+     *          not consistent; NO_MEMORY if shared memory cannot be used by
+     *          sensor system; INVALID_OPERATION if functionality is not
+     *          supported.
+     * @return  channelHandle a positive integer used for referencing registered
+     *          direct channel (>0) in configureDirectReport and
+     *          unregisterDirectChannel if result is OK, -1 otherwise.
+     */
+    registerDirectChannel(SharedMemInfo mem)
+            generates (Result result, int32_t channelHandle);
+
+    /*
+     * Unregister direct report channel.
+     *
+     * Unregister a direct channel previously registered using
+     * registerDirectChannel. If there is still active sensor report configured
+     * in the direct channel, HAL should remove them.
+     *
+     * @param   channelHandle handle of direct channel to be unregistered.
+     * @return  result OK if direct report is supported; INVALID_OPERATION
+     *          otherwise.
+     */
+    unregisterDirectChannel(int32_t channelHandle) generates (Result result);
+
+    /*
+     * Configure direct sensor event report in direct channel.
+     *
+     * This function start, modify rate or stop direct report of a sensor in a
+     * certain direct channel.
+     *
+     * @param   sensorHandle handle of sensor to be configured. When combined
+     *          with STOP rate, sensorHandle can be -1 to denote all active
+     *          sensors in the direct channel specified by channel Handle.
+     * @param   channelHandle handle of direct channel to be configured.
+     * @param   rate rate level, see RateLevel enum.
+     *
+     * @return  result OK on success; BAD_VALUE if parameter is invalid (such as
+     *          rate level is not supported by sensor, channelHandle does not
+     *          exist, etc); INVALID_OPERATION if functionality is not
+     *          supported.
+     * @return  reportToken positive integer to identify multiple sensors of
+     *          the same type in a single direct channel. Ignored if rate is
+     *          STOP. See SharedMemFormat.
+     */
+    configDirectReport(
+            int32_t sensorHandle, int32_t channelHandle, RateLevel rate)
+            generates (Result result, int32_t reportToken);
 };
diff --git a/sensors/1.0/default/Sensors.cpp b/sensors/1.0/default/Sensors.cpp
index c76369f..d79f5c7 100644
--- a/sensors/1.0/default/Sensors.cpp
+++ b/sensors/1.0/default/Sensors.cpp
@@ -234,6 +234,32 @@
             mSensorDevice->inject_sensor_data(mSensorDevice, &out));
 }
 
+Return<void> Sensors::registerDirectChannel(
+        const SharedMemInfo& mem, registerDirectChannel_cb _aidl_cb) {
+    //TODO(b/30985702): finish implementation
+    (void) mem;
+    _aidl_cb(Result::INVALID_OPERATION, -1);
+    return Void();
+}
+
+Return<Result> Sensors::unregisterDirectChannel(int32_t channelHandle) {
+    //TODO(b/30985702): finish implementation
+    (void) channelHandle;
+    return Result::INVALID_OPERATION;
+}
+
+Return<void> Sensors::configDirectReport(
+        int32_t sensorHandle, int32_t channelHandle, RateLevel rate,
+        configDirectReport_cb _hidl_cb) {
+    //TODO(b/30985702): finish implementation
+    (void) sensorHandle;
+    (void) channelHandle;
+    (void) rate;
+
+    _hidl_cb(Result::INVALID_OPERATION, -1);
+    return Void();
+}
+
 // static
 void Sensors::convertFromSensorEvents(
         size_t count,
diff --git a/sensors/1.0/default/Sensors.h b/sensors/1.0/default/Sensors.h
index f9b837d..abe7f43 100644
--- a/sensors/1.0/default/Sensors.h
+++ b/sensors/1.0/default/Sensors.h
@@ -54,6 +54,15 @@
 
     Return<Result> injectSensorData(const Event& event) override;
 
+    Return<void> registerDirectChannel(
+            const SharedMemInfo& mem, registerDirectChannel_cb _aidl_cb) override;
+
+    Return<Result> unregisterDirectChannel(int32_t channelHandle) override;
+
+    Return<void> configDirectReport(
+            int32_t sensorHandle, int32_t channelHandle, RateLevel rate,
+            configDirectReport_cb _hidl_cb) override;
+
 private:
     status_t mInitCheck;
     sensors_module_t *mSensorModule;
diff --git a/sensors/1.0/types.hal b/sensors/1.0/types.hal
index 460cef5..6c72c39 100644
--- a/sensors/1.0/types.hal
+++ b/sensors/1.0/types.hal
@@ -25,9 +25,10 @@
 /* Type enumerating various result codes returned from ISensors methods */
 enum Result : int32_t {
     OK,
-    BAD_VALUE,
-    PERMISSION_DENIED,
-    INVALID_OPERATION,
+    PERMISSION_DENIED                   = -1,
+    NO_MEMORY                           = -12,
+    BAD_VALUE                           = -22,
+    INVALID_OPERATION                   = -38,
 };
 
 /*
@@ -748,6 +749,35 @@
      * See ADDITIONAL_INFO and AdditionalInfo for details.
      */
     SENSOR_FLAG_ADDITIONAL_INFO             = 0x40,
+
+    /*
+     * Set this flag if sensor suppor direct channel backed by ashmem.
+     * See SharedMemType and registerDirectChannel for more details.
+     */
+    SENSOR_FLAG_DIRECT_CHANNEL_ASHMEM       = 0x400,
+
+    /*
+     * Set this flag if sensor suppor direct channel backed by gralloc HAL memory.
+     * See SharedMemType and registerDirectChannel for more details.
+     */
+    SENSOR_FLAG_DIRECT_CHANNEL_GRALLOC      = 0x800,
+
+    /*
+     * Flags mask for reporting mode of sensor.
+     */
+    SENSOR_FLAG_MASK_REPORTING_MODE         = 0xE,
+
+    /*
+     * Flags mask for direct report maximum rate level support.
+     * See RateLevel.
+     */
+    SENSOR_FLAG_MASK_DIRECT_REPORT          = 0x380,
+
+    /*
+     * Flags mask for all direct channel support bits.
+     * See SharedMemType.
+     */
+    SENSOR_FLAG_MASK_DIRECT_CHANNEL         = 0xC00,
 };
 
 struct SensorInfo {
@@ -1085,3 +1115,66 @@
     /* Union discriminated on sensorType */
     EventPayload u;
 };
+
+/**
+ * Direct report rate level definition. Except for SENSOR_DIRECT_RATE_STOP, each
+ * rate level covers the range (55%, 220%] * nominal report rate. For example,
+ * if config direct report specify a rate level SENSOR_DIRECT_RATE_FAST, sensor
+ * hardware should report event at a rate greater than 110Hz, and less or equal
+ * to 440Hz.
+ */
+@export(name="direct_rate_level_t", value_prefix="SENSOR_DIRECT_RATE_")
+enum RateLevel : int32_t {
+    STOP,            // stop
+    NORMAL,          // nominal 50Hz
+    FAST,            // nominal 200Hz
+    VERY_FAST,       // nominal 800Hz
+};
+
+/**
+ * Direct channel shared memory types. See struct SharedMemInfo.
+ */
+@export(name="direct_mem_type_t", value_prefix="SENSOR_DIRECT_MEM_TYPE_")
+enum SharedMemType : int32_t {
+    // handle contains 1 fd (ashmem handle) and 0 int.
+    ASHMEM = 1,
+    // handle definition matches gralloc HAL.
+    GRALLOC
+};
+
+
+/**
+ * Direct channel lock-free queue format, this defines how the shared memory
+ * should be interpreted by both sensor hardware and application. See struct
+ * SharedMemInfo.
+ */
+@export(name="direct_format_t", value_prefix="SENSOR_DIRECT_FMT_")
+enum SharedMemFormat : int32_t {
+    SENSORS_EVENT = 1,  // shared memory is formated as an array of data
+                        // elements, each sized 104 bytes. Details of fields:
+                        //
+                        // offset   type        name
+                        //-----------------------------------
+                        // 0x0000   int32_t     size (always 104)
+                        // 0x0004   int32_t     sensor report token
+                        // 0x0008   int32_t     type (see SensorType)
+                        // 0x000C   int32_t     atomic counter
+                        // 0x0010   int64_t     timestamp (see Event)
+                        // 0x0014   float[16]/  data
+                        //          int64_t[8]
+                        // 0x0058   int32_t[4]  reserved
+                        //
+                        // Upon return of channel registration call, the
+                        // shared memory space must be formated to all 0 by HAL.
+};
+
+/**
+ * Shared memory information for a direct channel
+ */
+struct SharedMemInfo {
+    SharedMemType type;         // shared memory type
+    SharedMemFormat format;
+    uint32_t size;              // size of the memory region, in bytes
+    handle memoryHandle;        // shared memory handle, it is interpreted
+                                // depending on type field, see SharedMemType.
+};