2012-02-06 06:41:15 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-07-20 13:10:44 +04:00
|
|
|
/* Copyright 2012 Mozilla Foundation and Mozilla contributors
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
2012-02-06 06:41:15 +04:00
|
|
|
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2012-12-15 03:58:45 +04:00
|
|
|
#include "mozilla/DebugOnly.h"
|
|
|
|
|
2012-08-29 19:26:18 +04:00
|
|
|
#include "base/basictypes.h"
|
|
|
|
#include "base/thread.h"
|
|
|
|
|
2012-02-06 06:41:15 +04:00
|
|
|
#include "Hal.h"
|
|
|
|
#include "HalSensor.h"
|
|
|
|
#include "hardware/sensors.h"
|
2012-09-18 06:51:35 +04:00
|
|
|
|
2012-08-29 19:26:18 +04:00
|
|
|
#undef LOG
|
2012-02-06 06:41:15 +04:00
|
|
|
|
2012-03-26 11:59:02 +04:00
|
|
|
#include <android/log.h>
|
|
|
|
|
2012-08-29 19:26:18 +04:00
|
|
|
using namespace mozilla::hal;
|
2012-02-06 06:41:15 +04:00
|
|
|
|
2012-09-18 06:51:35 +04:00
|
|
|
#define LOGE(args...) __android_log_print(ANDROID_LOG_ERROR, "GonkSensor" , ## args)
|
2012-10-31 11:03:19 +04:00
|
|
|
#define LOGW(args...) __android_log_print(ANDROID_LOG_WARN, "GonkSensor" , ## args)
|
2012-03-26 11:59:02 +04:00
|
|
|
|
2012-03-07 20:27:09 +04:00
|
|
|
namespace mozilla {
|
|
|
|
|
2012-09-18 06:51:35 +04:00
|
|
|
// The value from SensorDevice.h (Android)
|
|
|
|
#define DEFAULT_DEVICE_POLL_RATE 200000000 /*200ms*/
|
2012-03-21 10:36:17 +04:00
|
|
|
|
|
|
|
double radToDeg(double a) {
|
|
|
|
return a * (180.0 / M_PI);
|
|
|
|
}
|
|
|
|
|
2012-02-06 06:41:15 +04:00
|
|
|
static SensorType
|
|
|
|
HardwareSensorToHalSensor(int type)
|
2012-09-18 06:51:35 +04:00
|
|
|
{
|
2012-02-06 06:41:15 +04:00
|
|
|
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 20:43:45 +04:00
|
|
|
case SENSOR_TYPE_LIGHT:
|
|
|
|
return SENSOR_LIGHT;
|
2012-03-21 10:36:17 +04:00
|
|
|
case SENSOR_TYPE_GYROSCOPE:
|
|
|
|
return SENSOR_GYROSCOPE;
|
|
|
|
case SENSOR_TYPE_LINEAR_ACCELERATION:
|
|
|
|
return SENSOR_LINEAR_ACCELERATION;
|
2012-02-06 06:41:15 +04:00
|
|
|
default:
|
|
|
|
return SENSOR_UNKNOWN;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-21 10:36:17 +04:00
|
|
|
static SensorAccuracyType
|
|
|
|
HardwareStatusToHalAccuracy(int status) {
|
|
|
|
return static_cast<SensorAccuracyType>(status);
|
|
|
|
}
|
|
|
|
|
2012-02-06 06:41:15 +04: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 20:43:45 +04:00
|
|
|
case SENSOR_LIGHT:
|
|
|
|
return SENSOR_TYPE_LIGHT;
|
2012-03-21 10:36:17 +04:00
|
|
|
case SENSOR_GYROSCOPE:
|
|
|
|
return SENSOR_TYPE_GYROSCOPE;
|
|
|
|
case SENSOR_LINEAR_ACCELERATION:
|
|
|
|
return SENSOR_TYPE_LINEAR_ACCELERATION;
|
2012-02-06 06:41:15 +04:00
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-21 10:36:17 +04:00
|
|
|
static int
|
|
|
|
SensorseventStatus(const sensors_event_t& data)
|
2012-02-06 06:41:15 +04:00
|
|
|
{
|
2012-03-21 10:36:17 +04: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-06 06:41:15 +04:00
|
|
|
|
2012-03-21 10:36:17 +04:00
|
|
|
return SENSOR_STATUS_UNRELIABLE;
|
2012-02-06 06:41:15 +04:00
|
|
|
}
|
|
|
|
|
2012-03-21 10:36:17 +04:00
|
|
|
class SensorRunnable : public nsRunnable
|
2012-02-06 06:41:15 +04:00
|
|
|
{
|
2012-03-21 10:36:17 +04:00
|
|
|
public:
|
2012-09-18 06:51:35 +04:00
|
|
|
SensorRunnable(const sensors_event_t& data, const sensor_t* sensors, ssize_t size)
|
2012-03-21 10:36:17 +04:00
|
|
|
{
|
|
|
|
mSensorData.sensor() = HardwareSensorToHalSensor(data.type);
|
|
|
|
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 20:43:45 +04:00
|
|
|
} else if (mSensorData.sensor() == SENSOR_PROXIMITY) {
|
|
|
|
mSensorValues.AppendElement(data.data[0]);
|
2012-09-18 06:51:35 +04:00
|
|
|
mSensorValues.AppendElement(0);
|
2012-05-02 20:43:45 +04:00
|
|
|
|
|
|
|
// Determine the maxRange for this sensor.
|
2012-05-16 22:52:00 +04:00
|
|
|
for (ssize_t i = 0; i < size; i++) {
|
2012-05-02 20:43:45 +04:00
|
|
|
if (sensors[i].type == SENSOR_TYPE_PROXIMITY) {
|
2012-09-18 06:51:35 +04:00
|
|
|
mSensorValues.AppendElement(sensors[i].maxRange);
|
2012-05-02 20:43:45 +04:00
|
|
|
}
|
|
|
|
}
|
2012-05-02 20:43:45 +04:00
|
|
|
} else if (mSensorData.sensor() == SENSOR_LIGHT) {
|
|
|
|
mSensorValues.AppendElement(data.data[0]);
|
2012-03-21 10:36:17 +04: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-06 06:41:15 +04:00
|
|
|
|
|
|
|
namespace hal_impl {
|
|
|
|
|
2012-09-04 13:56:20 +04:00
|
|
|
static DebugOnly<int> sSensorRefCount[NUM_SENSOR_TYPE];
|
2012-09-18 06:51:35 +04:00
|
|
|
static base::Thread* sPollingThread;
|
|
|
|
static sensors_poll_device_t* sSensorDevice;
|
|
|
|
static sensors_module_t* sSensorModule;
|
2012-03-26 11:59:02 +04:00
|
|
|
|
2012-08-29 19:26:18 +04:00
|
|
|
static void
|
2012-09-18 06:51:35 +04:00
|
|
|
PollSensors()
|
2012-08-29 19:26:18 +04:00
|
|
|
{
|
|
|
|
const size_t numEventMax = 16;
|
|
|
|
sensors_event_t buffer[numEventMax];
|
2012-09-18 06:51:35 +04:00
|
|
|
const sensor_t* sensors;
|
|
|
|
int size = sSensorModule->get_sensors_list(sSensorModule, &sensors);
|
|
|
|
|
|
|
|
do {
|
|
|
|
// didn't check sSensorDevice because already be done on creating pollingThread.
|
|
|
|
int n = sSensorDevice->poll(sSensorDevice, buffer, numEventMax);
|
|
|
|
if (n < 0) {
|
|
|
|
LOGE("Error polling for sensor data (err=%d)", n);
|
|
|
|
break;
|
|
|
|
}
|
2012-02-06 06:41:15 +04:00
|
|
|
|
2012-09-18 06:51:35 +04:00
|
|
|
for (int i = 0; i < n; ++i) {
|
2012-10-16 09:11:06 +04:00
|
|
|
// FIXME: bug 802004, add proper support for the magnetic field sensor.
|
|
|
|
if (buffer[i].type == SENSOR_TYPE_MAGNETIC_FIELD)
|
|
|
|
continue;
|
|
|
|
|
2012-10-31 11:03:19 +04:00
|
|
|
if (HardwareSensorToHalSensor(buffer[i].type) == SENSOR_UNKNOWN) {
|
|
|
|
// Emulator is broken and gives us events without types set
|
2013-01-24 06:45:07 +04:00
|
|
|
int index;
|
|
|
|
for (index = 0; index < size; index++) {
|
|
|
|
if (sensors[index].handle == buffer[i].sensor) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (index < size &&
|
|
|
|
HardwareSensorToHalSensor(sensors[index].type) != SENSOR_UNKNOWN) {
|
|
|
|
buffer[i].type = sensors[index].type;
|
2012-10-31 11:03:19 +04:00
|
|
|
} else {
|
2013-01-24 06:45:07 +04:00
|
|
|
LOGW("Could not determine sensor type of event");
|
2012-10-31 11:03:19 +04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-18 06:51:35 +04:00
|
|
|
NS_DispatchToMainThread(new SensorRunnable(buffer[i], sensors, size));
|
|
|
|
}
|
|
|
|
} while (true);
|
2012-08-29 19:26:18 +04:00
|
|
|
}
|
2012-02-06 06:41:15 +04:00
|
|
|
|
2012-08-29 19:26:18 +04:00
|
|
|
static void
|
|
|
|
SwitchSensor(bool aActivate, sensor_t aSensor, pthread_t aThreadId)
|
|
|
|
{
|
|
|
|
int index = HardwareSensorToHalSensor(aSensor.type);
|
2012-03-07 20:27:09 +04:00
|
|
|
|
2012-09-04 13:56:20 +04:00
|
|
|
MOZ_ASSERT(sSensorRefCount[index] || aActivate);
|
2012-03-26 11:59:02 +04:00
|
|
|
|
2012-09-18 06:51:35 +04:00
|
|
|
sSensorDevice->activate(sSensorDevice, aSensor.handle, aActivate);
|
2012-03-26 11:59:02 +04:00
|
|
|
|
2012-09-18 06:51:35 +04:00
|
|
|
if (aActivate) {
|
|
|
|
sSensorDevice->setDelay(sSensorDevice, aSensor.handle,
|
|
|
|
DEFAULT_DEVICE_POLL_RATE);
|
|
|
|
}
|
2012-03-26 11:59:02 +04:00
|
|
|
|
2012-08-29 19:26:18 +04:00
|
|
|
if (aActivate) {
|
2012-09-04 13:56:20 +04:00
|
|
|
sSensorRefCount[index]++;
|
2012-08-29 19:26:18 +04:00
|
|
|
} else {
|
2012-09-04 13:56:20 +04:00
|
|
|
sSensorRefCount[index]--;
|
2012-08-29 19:26:18 +04:00
|
|
|
}
|
|
|
|
}
|
2012-03-26 11:59:02 +04:00
|
|
|
|
2012-02-06 06:41:15 +04:00
|
|
|
static void
|
2012-03-26 11:59:02 +04:00
|
|
|
SetSensorState(SensorType aSensor, bool activate)
|
2012-02-06 06:41:15 +04:00
|
|
|
{
|
|
|
|
int type = HalSensorToHardwareSensor(aSensor);
|
2013-08-23 23:51:15 +04:00
|
|
|
const sensor_t* sensors = nullptr;
|
2012-09-18 06:51:35 +04:00
|
|
|
|
|
|
|
int size = sSensorModule->get_sensors_list(sSensorModule, &sensors);
|
2012-05-16 22:52:00 +04:00
|
|
|
for (ssize_t i = 0; i < size; i++) {
|
2012-02-06 06:41:15 +04:00
|
|
|
if (sensors[i].type == type) {
|
2012-09-18 06:51:35 +04:00
|
|
|
SwitchSensor(activate, sensors[i], pthread_self());
|
2012-02-06 06:41:15 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-09-18 06:51:35 +04:00
|
|
|
EnableSensorNotifications(SensorType aSensor)
|
2012-02-06 06:41:15 +04:00
|
|
|
{
|
2012-09-18 06:51:35 +04:00
|
|
|
if (!sSensorModule) {
|
|
|
|
hw_get_module(SENSORS_HARDWARE_MODULE_ID,
|
|
|
|
(hw_module_t const**)&sSensorModule);
|
|
|
|
if (!sSensorModule) {
|
|
|
|
LOGE("Can't get sensor HAL module\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
sensors_open(&sSensorModule->common, &sSensorDevice);
|
|
|
|
if (!sSensorDevice) {
|
2013-08-23 23:51:15 +04:00
|
|
|
sSensorModule = nullptr;
|
2012-09-18 06:51:35 +04:00
|
|
|
LOGE("Can't get sensor poll device from module \n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
sensor_t const* sensors;
|
|
|
|
int count = sSensorModule->get_sensors_list(sSensorModule, &sensors);
|
|
|
|
for (size_t i=0 ; i<size_t(count) ; i++) {
|
|
|
|
sSensorDevice->activate(sSensorDevice, sensors[i].handle, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!sPollingThread) {
|
|
|
|
sPollingThread = new base::Thread("GonkSensors");
|
|
|
|
MOZ_ASSERT(sPollingThread);
|
|
|
|
// sPollingThread never terminates because poll may never return
|
|
|
|
sPollingThread->Start();
|
|
|
|
sPollingThread->message_loop()->PostTask(FROM_HERE,
|
|
|
|
NewRunnableFunction(PollSensors));
|
2012-02-06 06:41:15 +04:00
|
|
|
}
|
2012-09-18 06:51:35 +04:00
|
|
|
|
2012-03-26 11:59:02 +04:00
|
|
|
SetSensorState(aSensor, true);
|
2012-02-06 06:41:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-09-18 06:51:35 +04:00
|
|
|
DisableSensorNotifications(SensorType aSensor)
|
2012-02-06 06:41:15 +04:00
|
|
|
{
|
2012-09-18 06:51:35 +04:00
|
|
|
if (!sSensorModule) {
|
|
|
|
return;
|
|
|
|
}
|
2012-03-26 11:59:02 +04:00
|
|
|
SetSensorState(aSensor, false);
|
2012-02-06 06:41:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
} // hal_impl
|
|
|
|
} // mozilla
|