2012-02-05 18:41:15 -08:00
|
|
|
/* -*- 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"
|
2012-03-07 08:27:09 -08:00
|
|
|
#include "nsThreadUtils.h"
|
2012-02-05 18:41:15 -08:00
|
|
|
|
2012-03-26 00:59:02 -07:00
|
|
|
#include <android/log.h>
|
|
|
|
|
2012-02-05 18:41:15 -08:00
|
|
|
using namespace mozilla::hal;
|
|
|
|
using namespace android;
|
|
|
|
|
2012-03-26 00:59:02 -07:00
|
|
|
#define LOG(args...) __android_log_print(ANDROID_LOG_INFO, "GonkSensor" , ## args)
|
|
|
|
|
2012-03-07 08:27:09 -08:00
|
|
|
namespace mozilla {
|
|
|
|
|
2012-03-20 23:36:17 -07:00
|
|
|
#define DEFAULT_DEVICE_POLL_RATE 100000000 /*100ms*/
|
|
|
|
|
|
|
|
double radToDeg(double a) {
|
|
|
|
return a * (180.0 / M_PI);
|
|
|
|
}
|
|
|
|
|
2012-02-05 18:41:15 -08:00
|
|
|
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;
|
2012-05-02 09:43:45 -07:00
|
|
|
case SENSOR_TYPE_LIGHT:
|
|
|
|
return SENSOR_LIGHT;
|
2012-03-20 23:36:17 -07:00
|
|
|
case SENSOR_TYPE_GYROSCOPE:
|
|
|
|
return SENSOR_GYROSCOPE;
|
|
|
|
case SENSOR_TYPE_LINEAR_ACCELERATION:
|
|
|
|
return SENSOR_LINEAR_ACCELERATION;
|
2012-02-05 18:41:15 -08:00
|
|
|
default:
|
|
|
|
return SENSOR_UNKNOWN;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-20 23:36:17 -07:00
|
|
|
static SensorAccuracyType
|
|
|
|
HardwareStatusToHalAccuracy(int status) {
|
|
|
|
return static_cast<SensorAccuracyType>(status);
|
|
|
|
}
|
|
|
|
|
2012-02-05 18:41:15 -08:00
|
|
|
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;
|
2012-05-02 09:43:45 -07:00
|
|
|
case SENSOR_LIGHT:
|
|
|
|
return SENSOR_TYPE_LIGHT;
|
2012-03-20 23:36:17 -07:00
|
|
|
case SENSOR_GYROSCOPE:
|
|
|
|
return SENSOR_TYPE_GYROSCOPE;
|
|
|
|
case SENSOR_LINEAR_ACCELERATION:
|
|
|
|
return SENSOR_TYPE_LINEAR_ACCELERATION;
|
2012-02-05 18:41:15 -08:00
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-20 23:36:17 -07:00
|
|
|
static int
|
|
|
|
SensorseventStatus(const sensors_event_t& data)
|
2012-02-05 18:41:15 -08:00
|
|
|
{
|
2012-03-20 23:36:17 -07:00
|
|
|
int type = data.type;
|
|
|
|
switch(type) {
|
|
|
|
case SENSOR_ORIENTATION:
|
|
|
|
return data.orientation.status;
|
|
|
|
case SENSOR_LINEAR_ACCELERATION:
|
|
|
|
case SENSOR_ACCELERATION:
|
|
|
|
return data.acceleration.status;
|
|
|
|
case SENSOR_GYROSCOPE:
|
|
|
|
return data.gyro.status;
|
|
|
|
}
|
2012-02-05 18:41:15 -08:00
|
|
|
|
2012-03-20 23:36:17 -07:00
|
|
|
return SENSOR_STATUS_UNRELIABLE;
|
2012-02-05 18:41:15 -08:00
|
|
|
}
|
|
|
|
|
2012-03-20 23:36:17 -07:00
|
|
|
class SensorRunnable : public nsRunnable
|
2012-02-05 18:41:15 -08:00
|
|
|
{
|
2012-03-20 23:36:17 -07:00
|
|
|
public:
|
|
|
|
SensorRunnable(const sensors_event_t& data)
|
|
|
|
{
|
|
|
|
mSensorData.sensor() = HardwareSensorToHalSensor(data.type);
|
2012-04-18 14:18:08 -07:00
|
|
|
if (mSensorData.sensor() == SENSOR_UNKNOWN) {
|
|
|
|
// Emulator is broken and gives us events without types set
|
|
|
|
const sensor_t* sensors = NULL;
|
|
|
|
SensorDevice& device = SensorDevice::getInstance();
|
2012-05-16 11:52:00 -07:00
|
|
|
ssize_t size = device.getSensorList(&sensors);
|
2012-04-18 14:18:08 -07:00
|
|
|
if (data.sensor < size)
|
|
|
|
mSensorData.sensor() = HardwareSensorToHalSensor(sensors[data.sensor].type);
|
|
|
|
}
|
2012-03-20 23:36:17 -07:00
|
|
|
mSensorData.accuracy() = HardwareStatusToHalAccuracy(SensorseventStatus(data));
|
|
|
|
mSensorData.timestamp() = data.timestamp;
|
|
|
|
if (mSensorData.sensor() == SENSOR_GYROSCOPE) {
|
|
|
|
// libhardware returns gyro as rad. convert.
|
|
|
|
mSensorValues.AppendElement(radToDeg(data.data[0]));
|
|
|
|
mSensorValues.AppendElement(radToDeg(data.data[1]));
|
|
|
|
mSensorValues.AppendElement(radToDeg(data.data[2]));
|
2012-05-02 09:43:45 -07:00
|
|
|
} else if (mSensorData.sensor() == SENSOR_PROXIMITY) {
|
|
|
|
mSensorValues.AppendElement(data.data[0]);
|
|
|
|
mSensorValues.AppendElement(0);
|
|
|
|
|
|
|
|
// Determine the maxRange for this sensor.
|
|
|
|
const sensor_t* sensors = NULL;
|
2012-05-16 11:52:00 -07:00
|
|
|
ssize_t size = SensorDevice::getInstance().getSensorList(&sensors);
|
|
|
|
for (ssize_t i = 0; i < size; i++) {
|
2012-05-02 09:43:45 -07:00
|
|
|
if (sensors[i].type == SENSOR_TYPE_PROXIMITY) {
|
|
|
|
mSensorValues.AppendElement(sensors[i].maxRange);
|
|
|
|
}
|
|
|
|
}
|
2012-05-02 09:43:45 -07:00
|
|
|
} else if (mSensorData.sensor() == SENSOR_LIGHT) {
|
|
|
|
mSensorValues.AppendElement(data.data[0]);
|
2012-03-20 23:36:17 -07:00
|
|
|
} else {
|
|
|
|
mSensorValues.AppendElement(data.data[0]);
|
|
|
|
mSensorValues.AppendElement(data.data[1]);
|
|
|
|
mSensorValues.AppendElement(data.data[2]);
|
|
|
|
}
|
|
|
|
mSensorData.values() = mSensorValues;
|
|
|
|
}
|
|
|
|
|
|
|
|
~SensorRunnable() {}
|
|
|
|
|
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
|
|
|
NotifySensorChange(mSensorData);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
SensorData mSensorData;
|
|
|
|
InfallibleTArray<float> mSensorValues;
|
|
|
|
};
|
2012-02-05 18:41:15 -08:00
|
|
|
|
|
|
|
namespace hal_impl {
|
|
|
|
|
2012-03-26 00:59:02 -07:00
|
|
|
class SensorStatus {
|
|
|
|
public:
|
|
|
|
SensorData data;
|
|
|
|
DebugOnly<int> count;
|
|
|
|
};
|
|
|
|
|
2012-03-20 23:36:17 -07:00
|
|
|
static int sActivatedSensors = 0;
|
2012-03-26 00:59:02 -07:00
|
|
|
static SensorStatus sSensorStatus[NUM_SENSOR_TYPE];
|
2012-03-07 08:27:09 -08:00
|
|
|
static nsCOMPtr<nsIThread> sSwitchThread;
|
2012-02-05 18:41:15 -08:00
|
|
|
|
2012-03-26 00:59:02 -07:00
|
|
|
class PollSensor {
|
|
|
|
public:
|
|
|
|
NS_INLINE_DECL_REFCOUNTING(PollSensor);
|
|
|
|
|
|
|
|
static nsCOMPtr<nsIRunnable> GetRunnable() {
|
|
|
|
if (!mRunnable)
|
|
|
|
mRunnable = NS_NewRunnableMethod(new PollSensor(), &PollSensor::Poll);
|
|
|
|
return mRunnable;
|
2012-02-05 18:41:15 -08:00
|
|
|
}
|
2012-03-26 00:59:02 -07:00
|
|
|
|
|
|
|
void Poll() {
|
|
|
|
if (!sActivatedSensors) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
SensorDevice &device = SensorDevice::getInstance();
|
|
|
|
const size_t numEventMax = 16;
|
|
|
|
sensors_event_t buffer[numEventMax];
|
2012-02-05 18:41:15 -08:00
|
|
|
|
2012-03-26 00:59:02 -07:00
|
|
|
int n = device.poll(buffer, numEventMax);
|
|
|
|
if (n < 0) {
|
|
|
|
LOG("Error polling for sensor data (err=%d)", n);
|
|
|
|
return;
|
2012-03-20 23:36:17 -07:00
|
|
|
}
|
2012-02-05 18:41:15 -08:00
|
|
|
|
2012-03-26 00:59:02 -07:00
|
|
|
for (int i = 0; i < n; ++i) {
|
|
|
|
NS_DispatchToMainThread(new SensorRunnable(buffer[i]));
|
|
|
|
}
|
2012-02-05 18:41:15 -08:00
|
|
|
|
2012-03-26 00:59:02 -07:00
|
|
|
if (sActivatedSensors) {
|
|
|
|
sSwitchThread->Dispatch(GetRunnable(), NS_DISPATCH_NORMAL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
static nsCOMPtr<nsIRunnable> mRunnable;
|
|
|
|
};
|
2012-02-05 18:41:15 -08:00
|
|
|
|
2012-03-26 00:59:02 -07:00
|
|
|
nsCOMPtr<nsIRunnable> PollSensor::mRunnable = NULL;
|
2012-02-05 18:41:15 -08:00
|
|
|
|
2012-05-24 02:09:05 -07:00
|
|
|
class SwitchSensor : public RefCounted<SwitchSensor> {
|
2012-03-07 08:27:09 -08:00
|
|
|
public:
|
2012-03-26 00:59:02 -07:00
|
|
|
SwitchSensor(bool aActivate, sensor_t aSensor, pthread_t aThreadId) :
|
|
|
|
mActivate(aActivate), mSensor(aSensor), mThreadId(aThreadId) { }
|
2012-03-07 08:27:09 -08:00
|
|
|
|
|
|
|
void Switch() {
|
2012-03-26 00:59:02 -07:00
|
|
|
int index = HardwareSensorToHalSensor(mSensor.type);
|
|
|
|
|
2012-06-10 16:44:50 -07:00
|
|
|
MOZ_ASSERT(sSensorStatus[index].count || mActivate);
|
2012-03-26 00:59:02 -07:00
|
|
|
|
|
|
|
SensorDevice& device = SensorDevice::getInstance();
|
|
|
|
|
|
|
|
device.activate((void*)mThreadId, mSensor.handle, mActivate);
|
|
|
|
device.setDelay((void*)mThreadId, mSensor.handle, DEFAULT_DEVICE_POLL_RATE);
|
|
|
|
|
|
|
|
if (mActivate) {
|
|
|
|
if (++sActivatedSensors == 1) {
|
|
|
|
sSwitchThread->Dispatch(PollSensor::GetRunnable(), NS_DISPATCH_NORMAL);
|
|
|
|
}
|
|
|
|
sSensorStatus[index].count++;
|
|
|
|
} else {
|
|
|
|
sSensorStatus[index].count--;
|
|
|
|
--sActivatedSensors;
|
|
|
|
}
|
2012-03-07 08:27:09 -08:00
|
|
|
}
|
2012-03-26 00:59:02 -07:00
|
|
|
|
2012-03-07 08:27:09 -08:00
|
|
|
protected:
|
2012-03-26 00:59:02 -07:00
|
|
|
SwitchSensor() { };
|
|
|
|
bool mActivate;
|
|
|
|
sensor_t mSensor;
|
|
|
|
pthread_t mThreadId;
|
2012-03-07 08:27:09 -08:00
|
|
|
};
|
|
|
|
|
2012-03-26 00:59:02 -07:00
|
|
|
|
2012-02-05 18:41:15 -08:00
|
|
|
static void
|
2012-03-26 00:59:02 -07:00
|
|
|
SetSensorState(SensorType aSensor, bool activate)
|
2012-02-05 18:41:15 -08:00
|
|
|
{
|
|
|
|
int type = HalSensorToHardwareSensor(aSensor);
|
|
|
|
const sensor_t* sensors = NULL;
|
|
|
|
SensorDevice& device = SensorDevice::getInstance();
|
2012-05-16 11:52:00 -07:00
|
|
|
ssize_t size = device.getSensorList(&sensors);
|
|
|
|
for (ssize_t i = 0; i < size; i++) {
|
2012-02-05 18:41:15 -08:00
|
|
|
if (sensors[i].type == type) {
|
2012-03-26 00:59:02 -07:00
|
|
|
// Post an event to the sensor thread
|
|
|
|
nsCOMPtr<nsIRunnable> event = NS_NewRunnableMethod(new SwitchSensor(activate, sensors[i], pthread_self()),
|
|
|
|
&SwitchSensor::Switch);
|
|
|
|
|
2012-03-07 08:27:09 -08:00
|
|
|
sSwitchThread->Dispatch(event, NS_DISPATCH_NORMAL);
|
2012-02-05 18:41:15 -08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
EnableSensorNotifications(SensorType aSensor)
|
|
|
|
{
|
2012-03-26 00:59:02 -07:00
|
|
|
if (sSwitchThread == nsnull) {
|
|
|
|
NS_NewThread(getter_AddRefs(sSwitchThread));
|
2012-02-05 18:41:15 -08:00
|
|
|
}
|
|
|
|
|
2012-03-26 00:59:02 -07:00
|
|
|
SetSensorState(aSensor, true);
|
2012-02-05 18:41:15 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DisableSensorNotifications(SensorType aSensor)
|
|
|
|
{
|
2012-03-26 00:59:02 -07:00
|
|
|
SetSensorState(aSensor, false);
|
2012-02-05 18:41:15 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // hal_impl
|
|
|
|
} // mozilla
|