Implements sensors emulation using a connected Android device

There are three major things in this CL:
1. Abstract a connection with an Android device that is connected to the host via
   USB, and there is a TCP port forwarding to this device via 'adb forward' command.
   This abstraction is implemented in android/android-device.*
2. A client for android device API that talks to an app on the connected device that
   provides values for sensors available on the device. This is implemented in
   android/sensors-port.*
3. Changes to the sensor emulation code in android/hw-sensors.c to use sensors port
   (when available) for sensors emulation.

Change-Id: I12901e8db6b6a6262fc1703ed96a9f714335d666
diff --git a/android/hw-sensors.c b/android/hw-sensors.c
index 9c39196..6a7721c 100644
--- a/android/hw-sensors.c
+++ b/android/hw-sensors.c
@@ -20,7 +20,10 @@
 #include "hw/hw.h"
 #include "qemu-char.h"
 #include "qemu-timer.h"
+#include "android/sensors-port.h"
 
+#define  E(...)    derror(__VA_ARGS__)
+#define  W(...)    dwarning(__VA_ARGS__)
 #define  D(...)  VERBOSE_PRINT(sensors,__VA_ARGS__)
 
 /* define T_ACTIVE to 1 to debug transport communications */
@@ -165,9 +168,10 @@
 typedef struct HwSensorClient   HwSensorClient;
 
 typedef struct {
-    QemudService*    service;
-    Sensor           sensors[MAX_SENSORS];
-    HwSensorClient*  clients;
+    QemudService*       service;
+    Sensor              sensors[MAX_SENSORS];
+    HwSensorClient*     clients;
+    AndroidSensorsPort* sensors_port;
 } HwSensors;
 
 struct HwSensorClient {
@@ -301,7 +305,8 @@
 
     if (_hwSensorClient_enabled(cl, ANDROID_SENSOR_MAGNETIC_FIELD)) {
         sensor = &hw->sensors[ANDROID_SENSOR_MAGNETIC_FIELD];
-        snprintf(buffer, sizeof buffer, "magnetic-field:%g:%g:%g",
+        /* NOTE: sensors HAL expects "magnetic", not "magnetic-field" name here. */
+        snprintf(buffer, sizeof buffer, "magnetic:%g:%g:%g",
                  sensor->u.magnetic.x,
                  sensor->u.magnetic.y,
                  sensor->u.magnetic.z);
@@ -430,6 +435,16 @@
             D("%s: %s %s sensor", __FUNCTION__,
                 (cl->enabledMask & (1 << id))  ? "enabling" : "disabling",  msg);
         }
+
+        /* If emulating device is connected update sensor state there too. */
+        if (hw->sensors_port != NULL && sensors_port_is_connected(hw->sensors_port)) {
+            if (enabled) {
+                sensors_port_enable_sensor(hw->sensors_port, (const char*)msg);
+            } else {
+                sensors_port_disable_sensor(hw->sensors_port, (const char*)msg);
+            }
+        }
+
         _hwSensorClient_tick(cl);
         return;
     }
@@ -645,14 +660,40 @@
 static void
 _hwSensors_init( HwSensors*  h )
 {
+    /* Try to see if there is a device attached that can be used for
+     * sensor emulation. */
+    h->sensors_port = sensors_port_create(h);
+    if (h->sensors_port == NULL) {
+        W("Unable to create sensors port: %s", strerror(errno));
+    }
+
     h->service = qemud_service_register("sensors", 0, h, _hwSensors_connect,
                                         _hwSensors_save, _hwSensors_load);
 
-    if (android_hw->hw_accelerometer)
+    if (android_hw->hw_accelerometer) {
         h->sensors[ANDROID_SENSOR_ACCELERATION].enabled = 1;
+    }
 
-    if (android_hw->hw_sensors_proximity)
+    if (android_hw->hw_sensors_proximity) {
         h->sensors[ANDROID_SENSOR_PROXIMITY].enabled = 1;
+    }
+
+    if (android_hw->hw_sensors_magnetic_field) {
+        h->sensors[ANDROID_SENSOR_MAGNETIC_FIELD].enabled = 1;
+    }
+
+    if (android_hw->hw_sensors_orientation) {
+        h->sensors[ANDROID_SENSOR_ORIENTATION].enabled = 1;
+    }
+
+    if (android_hw->hw_sensors_temperature) {
+        h->sensors[ANDROID_SENSOR_TEMPERATURE].enabled = 1;
+    }
+
+    if (h->sensors_port != NULL) {
+        /* Init sensors on the attached device. */
+        sensors_port_init_sensors(h->sensors_port);
+    }
 
     /* XXX: TODO: Add other tests when we add the corresponding
         * properties to hardware-properties.ini et al. */