New sensor HAL and test

Change-Id: I216e3dd0b2b45829fef7c682774d2cdd8271d4e7
diff --git a/include/hardware/sensors.h b/include/hardware/sensors.h
index 610651f..883cb47 100644
--- a/include/hardware/sensors.h
+++ b/include/hardware/sensors.h
@@ -36,6 +36,7 @@
  */
 #define SENSORS_HARDWARE_CONTROL    "control"
 #define SENSORS_HARDWARE_DATA       "data"
+#define SENSORS_HARDWARE_POLL       "poll"
 
 /**
  * Handles must be higher than SENSORS_HANDLE_BASE and must be unique.
@@ -66,18 +67,7 @@
  */
 
 #define GRAVITY_SUN             (275.0f)
-#define GRAVITY_MERCURY         (3.70f)
-#define GRAVITY_VENUS           (8.87f)
 #define GRAVITY_EARTH           (9.80665f)
-#define GRAVITY_MOON            (1.6f)
-#define GRAVITY_MARS            (3.71f)
-#define GRAVITY_JUPITER         (23.12f)
-#define GRAVITY_SATURN          (8.96f)
-#define GRAVITY_URANUS          (8.69f)
-#define GRAVITY_NEPTUNE         (11.0f)
-#define GRAVITY_PLUTO           (0.6f)
-#define GRAVITY_DEATH_STAR_I    (0.000000353036145f)
-#define GRAVITY_THE_ISLAND      (4.815162342f)
 
 /** Maximum magnetic field on Earth's surface */
 #define MAGNETIC_FIELD_EARTH_MAX    (60.0f)
@@ -283,7 +273,7 @@
     /* vendor of the hardware part */
     const char*     vendor;
     /* version of the hardware part + driver. The value of this field is
-     * left to the implementation and doesn't have to be monotonicaly
+     * left to the implementation and doesn't have to be monotonically
      * increasing.
      */    
     int             version;
@@ -309,6 +299,38 @@
  * Every device data structure must begin with hw_device_t
  * followed by module specific public methods and attributes.
  */
+struct sensors_poll_device_t {
+    struct hw_device_t common;
+
+    /** Activate/deactivate one sensor.
+     *
+     * @param handle is the handle of the sensor to change.
+     * @param enabled set to 1 to enable, or 0 to disable the sensor.
+     *
+     * @return 0 on success, negative errno code otherwise
+     */
+    int (*activate)(struct sensors_poll_device_t *dev,
+            int handle, int enabled);
+
+    /**
+     * Set the delay between sensor events in nanoseconds for a given sensor
+     *
+     * @return 0 if successful, < 0 on error
+     */
+    int (*setDelay)(struct sensors_poll_device_t *dev,
+            int handle, int64_t ns);
+
+    /**
+     * Returns an array of sensor data.
+     *
+     * @return the number of events read on success, or -errno in case of an error.
+     *
+     */
+    int (*poll)(struct sensors_poll_device_t *dev,
+            sensors_data_t* data, int count);
+};
+
+
 struct sensors_control_device_t {
     struct hw_device_t common;
     
@@ -393,6 +415,16 @@
 
 /** convenience API for opening and closing a device */
 
+static inline int sensors_open(const struct hw_module_t* module,
+        struct sensors_poll_device_t** device) {
+    return module->methods->open(module,
+            SENSORS_HARDWARE_POLL, (struct hw_device_t**)device);
+}
+
+static inline int sensors_close(struct sensors_poll_device_t* device) {
+    return device->common.close(&device->common);
+}
+
 static inline int sensors_control_open(const struct hw_module_t* module, 
         struct sensors_control_device_t** device) {
     return module->methods->open(module, 
diff --git a/tests/Android.mk b/tests/Android.mk
new file mode 100644
index 0000000..5053e7d
--- /dev/null
+++ b/tests/Android.mk
@@ -0,0 +1 @@
+include $(call all-subdir-makefiles)
diff --git a/tests/nusensors/Android.mk b/tests/nusensors/Android.mk
new file mode 100644
index 0000000..ef49096
--- /dev/null
+++ b/tests/nusensors/Android.mk
@@ -0,0 +1,14 @@
+LOCAL_PATH:= $(call my-dir)
+include $(CLEAR_VARS)
+
+LOCAL_SRC_FILES:= \
+	nusensors.cpp
+
+LOCAL_SHARED_LIBRARIES := \
+	libcutils libhardware
+
+LOCAL_MODULE:= test-nusensors
+
+LOCAL_MODULE_TAGS := optional
+
+include $(BUILD_EXECUTABLE)
diff --git a/tests/nusensors/nusensors.cpp b/tests/nusensors/nusensors.cpp
new file mode 100644
index 0000000..215c972
--- /dev/null
+++ b/tests/nusensors/nusensors.cpp
@@ -0,0 +1,176 @@
+/*
+ * Copyright (C) 2008 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 <stdint.h>
+#include <sys/cdefs.h>
+#include <sys/types.h>
+
+#include <cutils/log.h>
+
+#include <hardware/sensors.h>
+
+char const* getSensorName(int type) {
+    switch(type) {
+        case SENSOR_TYPE_ACCELEROMETER:
+            return "Acc";
+        case SENSOR_TYPE_MAGNETIC_FIELD:
+            return "Mag";
+        case SENSOR_TYPE_ORIENTATION:
+            return "Ori";
+        case SENSOR_TYPE_PROXIMITY:
+            return "Prx";
+        case SENSOR_TYPE_TEMPERATURE:
+            return "Tmp";
+        case SENSOR_TYPE_LIGHT:
+            return "Lux";
+    }
+    return "ukn";
+}
+
+int main(int argc, char** argv)
+{
+    int err;
+    struct sensors_poll_device_t* device;
+    struct sensors_module_t* module;
+
+    err = hw_get_module(SENSORS_HARDWARE_MODULE_ID, (hw_module_t const**)&module);
+    if (err != 0) {
+        printf("hw_get_module() failed (%s)\n", strerror(-err));
+        return 0;
+    }
+
+    struct sensor_t const* list;
+    int count = module->get_sensors_list(module, &list);
+    for (int i=0 ; i<count ; i++) {
+        printf("%s\n"
+                "\tvendor: %s\n"
+                "\tversion: %d\n"
+                "\thandle: %d\n"
+                "\ttype: %d\n"
+                "\tmaxRange: %f\n"
+                "\tresolution: %f\n"
+                "\tpower: %f mA\n",
+                list[i].name,
+                list[i].vendor,
+                list[i].version,
+                list[i].handle,
+                list[i].type,
+                list[i].maxRange,
+                list[i].resolution,
+                list[i].power);
+    }
+
+    sensors_data_t buffer[16];
+
+    err = sensors_open(&module->common, &device);
+    if (err != 0) {
+        printf("sensors_open() failed (%s)\n", strerror(-err));
+        return 0;
+    }
+
+    for (int i=0 ; i<count ; i++) {
+        err = device->activate(device, list[i].handle, 1);
+        if (err != 0) {
+            printf("activate() for '%s'failed (%s)\n",
+                    list[i].name, strerror(-err));
+            return 0;
+        }
+        device->setDelay(device, list[i].handle, 10000000);
+    }
+
+    do {
+        int n = device->poll(device, buffer, 16);
+        if (n < 0) {
+            printf("poll() failed (%s)\n", strerror(-err));
+            break;
+        }
+
+        printf("read %d events:\n", n);
+        for (int i=0 ; i<n ; i++) {
+            const sensors_data_t& data = buffer[i];
+            switch(data.sensor) {
+                case SENSOR_TYPE_ACCELEROMETER:
+                    printf("sensor=%s, time=%lld, value=<%5.1f,%5.1f,%5.1f>\n",
+                            getSensorName(data.sensor),
+                            data.time,
+                            data.acceleration.x,
+                            data.acceleration.y,
+                            data.acceleration.z);
+                    break;
+                case SENSOR_TYPE_MAGNETIC_FIELD:
+                    printf("sensor=%s, time=%lld, value=<%5.1f,%5.1f,%5.1f>\n",
+                            getSensorName(data.sensor),
+                            data.time,
+                            data.magnetic.x,
+                            data.magnetic.y,
+                            data.magnetic.z);
+                    break;
+                case SENSOR_TYPE_ORIENTATION:
+                    printf("sensor=%s, time=%lld, value=<%5.1f,%5.1f,%5.1f>\n",
+                            getSensorName(data.sensor),
+                            data.time,
+                            data.orientation.azimuth,
+                            data.orientation.pitch,
+                            data.orientation.roll);
+                    break;
+                case SENSOR_TYPE_PROXIMITY:
+                    printf("sensor=%s, time=%lld, value=%f\n",
+                            getSensorName(data.sensor),
+                            data.time,
+                            data.distance);
+                    break;
+                case SENSOR_TYPE_TEMPERATURE:
+                    printf("sensor=%s, time=%lld, value=%f\n",
+                            getSensorName(data.sensor),
+                            data.time,
+                            data.temperature);
+                    break;
+                case SENSOR_TYPE_LIGHT:
+                    printf("sensor=%s, time=%lld, value=%f\n",
+                            getSensorName(data.sensor),
+                            data.time,
+                            data.light);
+                    break;
+                default:
+                    printf("sensor=%s, time=%lld, value=<%f,%f,%f>\n",
+                            getSensorName(data.sensor),
+                            data.time,
+                            data.acceleration.x,
+                            data.acceleration.y,
+                            data.acceleration.z);
+                    break;
+            }
+        }
+
+
+    } while (1); // fix that
+
+
+    for (int i=0 ; i<count ; i++) {
+        err = device->activate(device, list[i].handle, 0);
+        if (err != 0) {
+            printf("deactivate() for '%s'failed (%s)\n",
+                    list[i].name, strerror(-err));
+            return 0;
+        }
+    }
+
+    err = sensors_close(device);
+    if (err != 0) {
+        printf("sensors_close() failed (%s)\n", strerror(-err));
+    }
+    return 0;
+}