Bug 714413 - Sensor support for gonk [r=cjones]
authorSteven Lee <slee@mozilla.com>
Sun, 05 Feb 2012 18:41:15 -0800
changeset 109365 8f61f23de4931c5caa46e03659e48f65866d0847
parent 109364 30cd74e4d40786166ac50b6518170ef0861bc8cd
child 109366 d5ae3cb82473e9f7822638d96de2d244d044386a
push id2248
push userakeybl@mozilla.com
push dateMon, 08 Oct 2012 19:23:44 +0000
treeherdermozilla-aurora@118a3b748323 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerscjones
bugs714413
milestone13.0a1
Bug 714413 - Sensor support for gonk [r=cjones]
accessible/tests/mochitest/name/test_browserui.xul
configure.in
hal/Makefile.in
hal/gonk/GonkSensor.cpp
toolkit/library/Makefile.in
--- a/configure.in
+++ b/configure.in
@@ -308,17 +308,17 @@ if test -n "$gonkdir" ; then
     LD="$gonk_toolchain"/bin/"$android_tool_prefix"-ld
     AR="$gonk_toolchain"/bin/"$android_tool_prefix"-ar
     RANLIB="$gonk_toolchain"/bin/"$android_tool_prefix"-ranlib
     STRIP="$gonk_toolchain"/bin/"$android_tool_prefix"-strip
 
     STLPORT_CPPFLAGS="-I$gonkdir/ndk/sources/cxx-stl/stlport/stlport/"
     STLPORT_LIBS="-lstlport"
 
-    CPPFLAGS="-DANDROID -I$gonkdir/bionic/libc/include/ -I$gonkdir/bionic/libc/kernel/common -I$gonkdir/bionic/libc/arch-arm/include -I$gonkdir/bionic/libc/kernel/arch-arm -I$gonkdir/bionic/libm/include -I$gonkdir/frameworks/base/opengl/include -I$gonkdir/frameworks/base/native/include -I$gonkdir/hardware/libhardware/include -I$gonkdir/hardware/libhardware_legacy/include -I$gonkdir/system/core/include -I$gonkdir/bionic -I$gonkdir/frameworks/base/include $STLPORT_CPPFLAGS $CPPFLAGS"
+    CPPFLAGS="-DANDROID -I$gonkdir/bionic/libc/include/ -I$gonkdir/bionic/libc/kernel/common -I$gonkdir/bionic/libc/arch-arm/include -I$gonkdir/bionic/libc/kernel/arch-arm -I$gonkdir/bionic/libm/include -I$gonkdir/frameworks/base/opengl/include -I$gonkdir/frameworks/base/native/include -I$gonkdir/hardware/libhardware/include -I$gonkdir/hardware/libhardware_legacy/include -I$gonkdir/system/core/include -I$gonkdir/bionic -I$gonkdir/frameworks/base/include $STLPORT_CPPFLAGS $CPPFLAGS -I$gonkdir/frameworks/base/services/sensorservice"
     CFLAGS="-mandroid -fno-short-enums -fno-exceptions $CFLAGS"
     CXXFLAGS="-mandroid -fno-short-enums -fno-exceptions $CXXFLAGS"
     LIBS="$LIBS $STLPORT_LIBS"
 
     dnl Add -llog by default, since we use it all over the place.
     LDFLAGS="-mandroid -L$gonkdir/out/target/product/$GONK_PRODUCT/obj/lib -Wl,-rpath-link=$gonkdir/out/target/product/$GONK_PRODUCT/obj/lib --sysroot=$gonkdir/out/target/product/$GONK_PRODUCT/obj/ -llog $LDFLAGS"
 
     dnl prevent cross compile section from using these flags as host flags
--- a/hal/Makefile.in
+++ b/hal/Makefile.in
@@ -74,17 +74,17 @@ ifeq (android,$(MOZ_WIDGET_TOOLKIT))
 CPPSRCS += \
   AndroidHal.cpp \
   AndroidSensor.cpp \
   $(NULL)
 else ifeq (gonk,$(MOZ_WIDGET_TOOLKIT))
 CPPSRCS += \
   GonkHal.cpp \
   Power.cpp \
-  FallbackSensor.cpp \
+  GonkSensor.cpp \
   $(NULL)
 else ifeq (Linux,$(OS_TARGET))
 CPPSRCS += \
   LinuxHal.cpp \
   FallbackSensor.cpp \
   Power.cpp \
   $(NULL)
 ifdef MOZ_ENABLE_DBUS
@@ -103,9 +103,9 @@ CPPSRCS += \
   $(NULL)
 endif
 
 include $(topsrcdir)/config/config.mk
 include $(topsrcdir)/ipc/chromium/chromium-config.mk
 include $(topsrcdir)/config/rules.mk
 
 CFLAGS          += $(MOZ_DBUS_GLIB_CFLAGS)
-CXXFLAGS        += $(MOZ_DBUS_GLIB_CFLAGS)
+CXXFLAGS        += $(MOZ_DBUS_GLIB_CFLAGS) -DHAVE_PTHREADS
new file mode 100644
--- /dev/null
+++ b/hal/gonk/GonkSensor.cpp
@@ -0,0 +1,167 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this file,
+ * You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#include <pthread.h>
+#include <stdio.h>
+
+#include "Hal.h"
+#include "HalSensor.h"
+#include "hardware/sensors.h"
+#include "mozilla/Util.h"
+#include "SensorDevice.h"
+
+using namespace mozilla::hal;
+using namespace android;
+
+static SensorType
+HardwareSensorToHalSensor(int type)
+{     
+  switch(type) {
+    case SENSOR_TYPE_ORIENTATION:
+      return SENSOR_ORIENTATION;
+    case SENSOR_TYPE_ACCELEROMETER:
+      return SENSOR_ACCELERATION;
+    case SENSOR_TYPE_PROXIMITY:
+      return SENSOR_PROXIMITY;
+    default:
+      return SENSOR_UNKNOWN;
+  }
+}
+
+static int
+HalSensorToHardwareSensor(SensorType type)
+{
+  switch(type) {
+    case SENSOR_ORIENTATION:
+      return SENSOR_TYPE_ORIENTATION;
+    case SENSOR_ACCELERATION:
+      return SENSOR_TYPE_ACCELEROMETER;
+    case SENSOR_PROXIMITY:
+      return SENSOR_TYPE_PROXIMITY;
+    default:
+      return -1;
+  }
+}
+
+static bool
+SensorseventToSensorData(const sensors_event_t& data, SensorData* aSensorData)
+{
+  aSensorData->sensor() = HardwareSensorToHalSensor(data.type);
+
+  if (aSensorData->sensor() == SENSOR_UNKNOWN)
+    return false;
+
+  aSensorData->timestamp() = data.timestamp;
+  aSensorData->values()[0] = data.data[0];
+  aSensorData->values()[1] = data.data[1];
+  aSensorData->values()[2] = data.data[2];
+  return true;
+}
+
+static void
+onSensorChanged(const sensors_event_t& data, SensorData* aSensorData)
+{
+  mozilla::DebugOnly<bool> convertedData = SensorseventToSensorData(data, aSensorData);
+  MOZ_ASSERT(convertedData);
+  NotifySensorChange(*aSensorData);
+}
+
+namespace mozilla {
+namespace hal_impl {
+
+static pthread_t sThread;
+static bool sInitialized = false;
+static bool sContinue = false;
+static int sActivatedSensors = 0;
+static SensorData sSensordata[NUM_SENSOR_TYPE];
+
+static void*
+UpdateSensorData(void* /*unused*/)
+{
+  SensorDevice &device = SensorDevice::getInstance();
+  const size_t numEventMax = 16;
+  sensors_event_t buffer[numEventMax];
+  int count = 0;
+
+  while (sContinue) {
+    count = device.poll(buffer, numEventMax);
+    if (count < 0) {
+      continue;
+    }
+
+    for (int i=0; i<count; i++) {
+      onSensorChanged(buffer[i], &sSensordata[HardwareSensorToHalSensor(buffer[i].type)]);
+    }
+  }
+
+  return NULL;
+}
+
+static void 
+InitialResources()
+{
+  pthread_create(&sThread, NULL, &UpdateSensorData, NULL);
+  sInitialized = true;
+  sContinue = true;
+}
+
+static void 
+ReleaseResources()
+{
+  sContinue = false;
+  pthread_join(sThread, NULL);
+  sInitialized = false;
+}
+
+static void
+SensorSwitch(SensorType aSensor, bool activate)
+{
+  int type = HalSensorToHardwareSensor(aSensor);
+  const sensor_t* sensors = NULL;
+  SensorDevice& device = SensorDevice::getInstance();
+  size_t size = device.getSensorList(&sensors);
+
+  for (size_t i=0; i<size; i++) {
+    if (sensors[i].type == type) {
+      device.activate((void*)pthread_self(), sensors[i].handle, activate);
+      break;
+    }
+  }
+}
+
+void
+EnableSensorNotifications(SensorType aSensor) 
+{
+  if (!sInitialized) {
+    InitialResources();
+  }
+  
+  SensorSwitch(aSensor, true);
+  sActivatedSensors++;
+}
+
+void
+DisableSensorNotifications(SensorType aSensor) 
+{
+  if (!sInitialized) {
+    NS_WARNING("Disable sensors without initializing first");
+    return;
+  }
+  
+  SensorSwitch(aSensor, false);  
+  sActivatedSensors--;
+
+  if (!sActivatedSensors) {
+    ReleaseResources();  
+  }
+}
+
+void 
+GetCurrentSensorDataInformation(SensorType aSensor, SensorData* aSensorData) {
+  *aSensorData = sSensordata[aSensor];
+}
+
+} // hal_impl
+} // mozilla
--- a/toolkit/library/Makefile.in
+++ b/toolkit/library/Makefile.in
@@ -424,16 +424,17 @@ OS_LIBS += \
   -lui \
   -lmedia \
   -lhardware_legacy \
   -lhardware \
   -lutils \
   -lcutils \
   -lcamera_client \
   -lbinder \
+  -lsensorservice \
   $(NULL)
 endif
 
 EXTRA_DEPS += \
   $(topsrcdir)/intl/unicharutil/util/objs.mk \
   $(topsrcdir)/rdf/util/src/objs.mk \
   $(NULL)