upload android base code part4

This commit is contained in:
August 2018-08-08 17:00:29 +08:00
parent b9e30e05b1
commit 78ea2404cd
23455 changed files with 5250148 additions and 0 deletions

View file

@ -0,0 +1,225 @@
/*
* 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 <fcntl.h>
#include <errno.h>
#include <math.h>
#include <poll.h>
#include <string.h>
#include <unistd.h>
#include <dirent.h>
#include <sys/select.h>
#include <cutils/log.h>
#include "AdxlSensor.h"
#define ADXL_DATA_NAME "ADXL34x accelerometer"
#define ADXL_MAX_SAMPLE_RATE_VAL 11 /* 200 Hz */
#define ADXL_UNIT_CONVERSION(value) ((value) * GRAVITY_EARTH / (256.0f))
/*****************************************************************************/
AdxlSensor::AdxlSensor()
: SensorBase(NULL, ADXL_DATA_NAME),
mEnabled(0),
mDelay(-1),
mInputReader(4),
mHasPendingEvent(false)
{
mPendingEvent.version = sizeof(sensors_event_t);
mPendingEvent.sensor = ID_A;
mPendingEvent.type = SENSOR_TYPE_ACCELEROMETER;
memset(mPendingEvent.data, 0, sizeof(mPendingEvent.data));
if (data_fd >= 0) {
strcpy(input_sysfs_path, "/sys/class/input/");
strcat(input_sysfs_path, input_name);
strcat(input_sysfs_path, "/device/device/");
input_sysfs_path_len = strlen(input_sysfs_path);
ALOGD("AdxlSensor: sysfs_path=%s", input_sysfs_path);
} else {
input_sysfs_path[0] = '\0';
input_sysfs_path_len = 0;
}
}
AdxlSensor::~AdxlSensor() {
if (mEnabled) {
setEnable(0, 0);
}
}
int AdxlSensor::setInitialState() {
struct input_absinfo absinfo;
if (mEnabled) {
if (!ioctl(data_fd, EVIOCGABS(EVENT_TYPE_ACCEL_X), &absinfo)) {
mPendingEvent.acceleration.x = ADXL_UNIT_CONVERSION(absinfo.value);
}
if (!ioctl(data_fd, EVIOCGABS(EVENT_TYPE_ACCEL_Y), &absinfo)) {
mPendingEvent.acceleration.y = ADXL_UNIT_CONVERSION(absinfo.value);
}
if (!ioctl(data_fd, EVIOCGABS(EVENT_TYPE_ACCEL_Z), &absinfo)) {
mPendingEvent.acceleration.z = ADXL_UNIT_CONVERSION(absinfo.value);
}
}
return 0;
}
bool AdxlSensor::hasPendingEvents() const {
return mHasPendingEvent;
}
int AdxlSensor::setEnable(int32_t handle, int enabled) {
int err = 0;
char buffer[2];
/* handle check */
if (handle != ID_A) {
ALOGE("AdxlSensor: Invalid handle (%d)", handle);
return -EINVAL;
}
buffer[0] = '\0';
buffer[1] = '\0';
if (mEnabled <= 0) {
if(enabled) buffer[0] = '0';
} else if (mEnabled == 1) {
if(!enabled) buffer[0] = '1';
}
if (buffer[0] != '\0') {
strcpy(&input_sysfs_path[input_sysfs_path_len], "disable");
err = write_sys_attribute(input_sysfs_path, buffer, 1);
if (err != 0) {
return err;
}
ALOGD("AdxlSensor: Control set %s", buffer);
setInitialState();
}
if (enabled) {
mEnabled++;
if (mEnabled > 32767) mEnabled = 32767;
} else {
mEnabled--;
if (mEnabled < 0) mEnabled = 0;
}
ALOGD("AdxlSensor: mEnabled = %d", mEnabled);
return err;
}
int AdxlSensor::setDelay(int32_t handle, int64_t delay_ns)
{
int err = 0;
int rate_val;
int32_t us;
char buffer[16];
int bytes;
/* handle check */
if (handle != ID_A) {
ALOGE("AdxlSensor: Invalid handle (%d)", handle);
return -EINVAL;
}
if (mDelay != delay_ns) {
/*
* The ADXL34x Supports 16 sample rates ranging from 3200Hz-0.098Hz
* Calculate best fit and limit to max 200Hz (rate_val 11)
*/
us = (int32_t)(delay_ns / 1000);
for (rate_val = 0; rate_val < 16; rate_val++)
if (us >= ((10000000) >> rate_val))
break;
if (rate_val > ADXL_MAX_SAMPLE_RATE_VAL) {
rate_val = ADXL_MAX_SAMPLE_RATE_VAL;
}
strcpy(&input_sysfs_path[input_sysfs_path_len], "rate");
bytes = sprintf(buffer, "%d", rate_val);
err = write_sys_attribute(input_sysfs_path, buffer, bytes);
if (err == 0) {
mDelay = delay_ns;
ALOGD("AdxlSensor: Control set delay %f ms requetsed, using %f ms",
delay_ns/1000000.0f, 1e6 / (3200000 >> (15 - rate_val)));
}
}
return err;
}
int64_t AdxlSensor::getDelay(int32_t handle)
{
return (handle == ID_A) ? mDelay : 0;
}
int AdxlSensor::getEnable(int32_t handle)
{
return (handle == ID_A) ? mEnabled : 0;
}
int AdxlSensor::readEvents(sensors_event_t* data, int count)
{
if (count < 1)
return -EINVAL;
if (mHasPendingEvent) {
mHasPendingEvent = false;
mPendingEvent.timestamp = getTimestamp();
*data = mPendingEvent;
return mEnabled ? 1 : 0;
}
ssize_t n = mInputReader.fill(data_fd);
if (n < 0)
return n;
int numEventReceived = 0;
input_event const* event;
while (count && mInputReader.readEvent(&event)) {
int type = event->type;
if (type == EV_ABS) {
float value = event->value;
if (event->code == EVENT_TYPE_ACCEL_X) {
mPendingEvent.acceleration.x = ADXL_UNIT_CONVERSION(value);
} else if (event->code == EVENT_TYPE_ACCEL_Y) {
mPendingEvent.acceleration.y = ADXL_UNIT_CONVERSION(value);
} else if (event->code == EVENT_TYPE_ACCEL_Z) {
mPendingEvent.acceleration.z = ADXL_UNIT_CONVERSION(value);
}
} else if (type == EV_SYN) {
mPendingEvent.timestamp = timevalToNano(event->time);
if (mEnabled) {
*data++ = mPendingEvent;
count--;
numEventReceived++;
}
} else {
ALOGE("AdxlSensor: unknown event (type=%d, code=%d)",
type, event->code);
}
mInputReader.next();
}
return numEventReceived;
}

View file

@ -0,0 +1,57 @@
/*
* 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.
*/
#ifndef ANDROID_ADXL_SENSOR_H
#define ANDROID_ADXL_SENSOR_H
#include <stdint.h>
#include <errno.h>
#include <sys/cdefs.h>
#include <sys/types.h>
#include "sensors.h"
#include "SensorBase.h"
#include "InputEventReader.h"
/*****************************************************************************/
struct input_event;
class AdxlSensor : public SensorBase {
int mEnabled;
int64_t mDelay;
InputEventCircularReader mInputReader;
sensors_event_t mPendingEvent;
bool mHasPendingEvent;
char input_sysfs_path[PATH_MAX];
int input_sysfs_path_len;
int setInitialState();
public:
AdxlSensor();
virtual ~AdxlSensor();
virtual int readEvents(sensors_event_t* data, int count);
virtual bool hasPendingEvents() const;
virtual int setDelay(int32_t handle, int64_t ns);
virtual int setEnable(int32_t handle, int enabled);
virtual int64_t getDelay(int32_t handle);
virtual int getEnable(int32_t handle);
};
/*****************************************************************************/
#endif // ANDROID_ADXL_SENSOR_H

View file

@ -0,0 +1,324 @@
/*
* 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 <fcntl.h>
#include <errno.h>
#include <math.h>
#include <poll.h>
#include <string.h>
#include <unistd.h>
#include <dirent.h>
#include <sys/select.h>
#include <dlfcn.h>
#include <cutils/log.h>
#include "AkmSensor.h"
#define AKMD_DEFAULT_INTERVAL 200000000
/*****************************************************************************/
AkmSensor::AkmSensor()
: SensorBase(NULL, "compass"),
mPendingMask(0),
mInputReader(32)
{
for (int i=0; i<numSensors; i++) {
mEnabled[i] = 0;
mDelay[i] = -1;
}
memset(mPendingEvents, 0, sizeof(mPendingEvents));
mPendingEvents[Accelerometer].version = sizeof(sensors_event_t);
mPendingEvents[Accelerometer].sensor = ID_A;
mPendingEvents[Accelerometer].type = SENSOR_TYPE_ACCELEROMETER;
mPendingEvents[Accelerometer].acceleration.status = SENSOR_STATUS_ACCURACY_HIGH;
mPendingEvents[MagneticField].version = sizeof(sensors_event_t);
mPendingEvents[MagneticField].sensor = ID_M;
mPendingEvents[MagneticField].type = SENSOR_TYPE_MAGNETIC_FIELD;
mPendingEvents[MagneticField].magnetic.status = SENSOR_STATUS_ACCURACY_HIGH;
mPendingEvents[Orientation ].version = sizeof(sensors_event_t);
mPendingEvents[Orientation ].sensor = ID_O;
mPendingEvents[Orientation ].type = SENSOR_TYPE_ORIENTATION;
mPendingEvents[Orientation ].orientation.status = SENSOR_STATUS_ACCURACY_HIGH;
if (data_fd) {
strcpy(input_sysfs_path, "/sys/class/compass/akm8975/");
input_sysfs_path_len = strlen(input_sysfs_path);
} else {
input_sysfs_path[0] = '\0';
input_sysfs_path_len = 0;
}
}
AkmSensor::~AkmSensor()
{
for (int i=0; i<numSensors; i++) {
setEnable(i, 0);
}
}
int AkmSensor::setEnable(int32_t handle, int enabled)
{
int id = handle2id(handle);
int err = 0;
char buffer[2];
switch (id) {
case Accelerometer:
strcpy(&input_sysfs_path[input_sysfs_path_len], "enable_acc");
break;
case MagneticField:
strcpy(&input_sysfs_path[input_sysfs_path_len], "enable_mag");
break;
case Orientation:
strcpy(&input_sysfs_path[input_sysfs_path_len], "enable_ori");
break;
default:
ALOGE("AkmSensor: unknown handle (%d)", handle);
return -EINVAL;
}
buffer[0] = '\0';
buffer[1] = '\0';
if (mEnabled[id] <= 0) {
if(enabled) buffer[0] = '1';
} else if (mEnabled[id] == 1) {
if(!enabled) buffer[0] = '0';
}
if (buffer[0] != '\0') {
err = write_sys_attribute(input_sysfs_path, buffer, 1);
if (err != 0) {
return err;
}
ALOGD("AkmSensor: set %s to %s",
&input_sysfs_path[input_sysfs_path_len], buffer);
/* for AKMD specification */
if (buffer[0] == '1') {
setDelay(handle, AKMD_DEFAULT_INTERVAL);
} else {
setDelay(handle, -1);
}
}
if (enabled) {
(mEnabled[id])++;
if (mEnabled[id] > 32767) mEnabled[id] = 32767;
} else {
(mEnabled[id])--;
if (mEnabled[id] < 0) mEnabled[id] = 0;
}
ALOGD("AkmSensor: mEnabled[%d] = %d", id, mEnabled[id]);
return err;
}
int AkmSensor::setDelay(int32_t handle, int64_t ns)
{
int id = handle2id(handle);
int err = 0;
char buffer[32];
int bytes;
if (ns < -1 || 2147483647 < ns) {
ALOGE("AkmSensor: invalid delay (%lld)", ns);
return -EINVAL;
}
switch (id) {
case Accelerometer:
strcpy(&input_sysfs_path[input_sysfs_path_len], "delay_acc");
break;
case MagneticField:
strcpy(&input_sysfs_path[input_sysfs_path_len], "delay_mag");
break;
case Orientation:
strcpy(&input_sysfs_path[input_sysfs_path_len], "delay_ori");
break;
default:
ALOGE("AkmSensor: unknown handle (%d)", handle);
return -EINVAL;
}
if (ns != mDelay[id]) {
bytes = sprintf(buffer, "%lld", ns);
err = write_sys_attribute(input_sysfs_path, buffer, bytes);
if (err == 0) {
mDelay[id] = ns;
ALOGD("AkmSensor: set %s to %f ms.",
&input_sysfs_path[input_sysfs_path_len], ns/1000000.0f);
}
}
return err;
}
int64_t AkmSensor::getDelay(int32_t handle)
{
int id = handle2id(handle);
if (id > 0) {
return mDelay[id];
} else {
return 0;
}
}
int AkmSensor::getEnable(int32_t handle)
{
int id = handle2id(handle);
if (id >= 0) {
return mEnabled[id];
} else {
return 0;
}
}
int AkmSensor::readEvents(sensors_event_t* data, int count)
{
if (count < 1)
return -EINVAL;
ssize_t n = mInputReader.fill(data_fd);
if (n < 0)
return n;
int numEventReceived = 0;
input_event const* event;
while (count && mInputReader.readEvent(&event)) {
int type = event->type;
if (type == EV_ABS) {
processEvent(event->code, event->value);
mInputReader.next();
} else if (type == EV_SYN) {
int64_t time = timevalToNano(event->time);
for (int j=0 ; count && mPendingMask && j<numSensors ; j++) {
if (mPendingMask & (1<<j)) {
mPendingMask &= ~(1<<j);
mPendingEvents[j].timestamp = time;
//ALOGD("data=%8.5f,%8.5f,%8.5f",
//mPendingEvents[j].data[0],
//mPendingEvents[j].data[1],
//mPendingEvents[j].data[2]);
if (mEnabled[j]) {
*data++ = mPendingEvents[j];
count--;
numEventReceived++;
}
}
}
if (!mPendingMask) {
mInputReader.next();
}
} else {
ALOGE("AkmSensor: unknown event (type=%d, code=%d)",
type, event->code);
mInputReader.next();
}
}
return numEventReceived;
}
int AkmSensor::setAccel(sensors_event_t* data)
{
int err;
int16_t acc[3];
acc[0] = (int16_t)(data->acceleration.x / GRAVITY_EARTH * AKSC_LSG);
acc[1] = (int16_t)(data->acceleration.y / GRAVITY_EARTH * AKSC_LSG);
acc[2] = (int16_t)(data->acceleration.z / GRAVITY_EARTH * AKSC_LSG);
strcpy(&input_sysfs_path[input_sysfs_path_len], "accel");
err = write_sys_attribute(input_sysfs_path, (char*)acc, 6);
if (err < 0) {
ALOGD("AkmSensor: %s write failed.",
&input_sysfs_path[input_sysfs_path_len]);
}
return err;
}
int AkmSensor::handle2id(int32_t handle)
{
switch (handle) {
case ID_A:
return Accelerometer;
case ID_M:
return MagneticField;
case ID_O:
return Orientation;
default:
ALOGE("AkmSensor: unknown handle (%d)", handle);
return -EINVAL;
}
}
void AkmSensor::processEvent(int code, int value)
{
switch (code) {
case EVENT_TYPE_ACCEL_X:
mPendingMask |= 1<<Accelerometer;
mPendingEvents[Accelerometer].acceleration.x = value * CONVERT_A;
break;
case EVENT_TYPE_ACCEL_Y:
mPendingMask |= 1<<Accelerometer;
mPendingEvents[Accelerometer].acceleration.y = value * CONVERT_A;
break;
case EVENT_TYPE_ACCEL_Z:
mPendingMask |= 1<<Accelerometer;
mPendingEvents[Accelerometer].acceleration.z = value * CONVERT_A;
break;
case EVENT_TYPE_MAGV_X:
mPendingMask |= 1<<MagneticField;
mPendingEvents[MagneticField].magnetic.x = value * CONVERT_M;
break;
case EVENT_TYPE_MAGV_Y:
mPendingMask |= 1<<MagneticField;
mPendingEvents[MagneticField].magnetic.y = value * CONVERT_M;
break;
case EVENT_TYPE_MAGV_Z:
mPendingMask |= 1<<MagneticField;
mPendingEvents[MagneticField].magnetic.z = value * CONVERT_M;
break;
case EVENT_TYPE_MAGV_STATUS:
mPendingMask |= 1<<MagneticField;
mPendingEvents[MagneticField].magnetic.status = value;
break;
case EVENT_TYPE_YAW:
mPendingMask |= 1<<Orientation;
mPendingEvents[Orientation].orientation.azimuth = value * CONVERT_O;
break;
case EVENT_TYPE_PITCH:
mPendingMask |= 1<<Orientation;
mPendingEvents[Orientation].orientation.pitch = value * CONVERT_O;
break;
case EVENT_TYPE_ROLL:
mPendingMask |= 1<<Orientation;
mPendingEvents[Orientation].orientation.roll = value * CONVERT_O;
break;
case EVENT_TYPE_ORIENT_STATUS:
mPendingMask |= 1<<Orientation;
mPendingEvents[Orientation].orientation.status = value;
break;
}
}

View file

@ -0,0 +1,68 @@
/*
* 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.
*/
#ifndef ANDROID_AKM_SENSOR_H
#define ANDROID_AKM_SENSOR_H
#include <stdint.h>
#include <errno.h>
#include <sys/cdefs.h>
#include <sys/types.h>
#include "sensors.h"
#include "SensorBase.h"
#include "InputEventReader.h"
/*****************************************************************************/
struct input_event;
class AkmSensor : public SensorBase {
public:
AkmSensor();
virtual ~AkmSensor();
enum {
Accelerometer = 0,
MagneticField,
Orientation,
numSensors
};
virtual int readEvents(sensors_event_t* data, int count);
virtual int setDelay(int32_t handle, int64_t ns);
virtual int setEnable(int32_t handle, int enabled);
virtual int64_t getDelay(int32_t handle);
virtual int getEnable(int32_t handle);
int setAccel(sensors_event_t* data);
private:
int mEnabled[numSensors];
int64_t mDelay[numSensors];
uint32_t mPendingMask;
InputEventCircularReader mInputReader;
sensors_event_t mPendingEvents[numSensors];
char input_sysfs_path[PATH_MAX];
int input_sysfs_path_len;
int handle2id(int32_t handle);
void processEvent(int code, int value);
};
/*****************************************************************************/
#endif // ANDROID_AKM_SENSOR_H

View file

@ -0,0 +1,43 @@
# 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.
LOCAL_PATH := $(call my-dir)
# HAL module implemenation, not prelinked, and stored in
# hw/<SENSORS_HARDWARE_MODULE_ID>.<ro.product.board>.so
include $(CLEAR_VARS)
LOCAL_MODULE := sensors.default
LOCAL_MODULE_RELATIVE_PATH := hw
LOCAL_MODULE_TAGS := optional
LOCAL_CFLAGS := -DLOG_TAG=\"Sensors\" \
-Wall \
-DSENSORHAL_ACC_ADXL346
# -DSENSORHAL_ACC_KXTF9
LOCAL_SRC_FILES := \
SensorBase.cpp \
InputEventReader.cpp \
AkmSensor.cpp \
sensors.cpp \
AdxlSensor.cpp
# KionixSensor.cpp
LOCAL_SHARED_LIBRARIES := liblog libcutils libdl
include $(BUILD_SHARED_LIBRARY)

View file

@ -0,0 +1,89 @@
/*
* 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 <errno.h>
#include <poll.h>
#include <string.h>
#include <unistd.h>
#include <sys/cdefs.h>
#include <sys/types.h>
#include <linux/input.h>
#include <cutils/log.h>
#include "InputEventReader.h"
/*****************************************************************************/
struct input_event;
InputEventCircularReader::InputEventCircularReader(size_t numEvents)
: mBuffer(new input_event[numEvents * 2]),
mBufferEnd(mBuffer + numEvents),
mHead(mBuffer),
mCurr(mBuffer),
mFreeSpace(numEvents)
{
}
InputEventCircularReader::~InputEventCircularReader()
{
delete [] mBuffer;
}
ssize_t InputEventCircularReader::fill(int fd)
{
size_t numEventsRead = 0;
if (mFreeSpace) {
const ssize_t nread = read(fd, mHead, mFreeSpace * sizeof(input_event));
if (nread<0 || nread % sizeof(input_event)) {
// we got a partial event!!
return nread<0 ? -errno : -EINVAL;
}
numEventsRead = nread / sizeof(input_event);
if (numEventsRead) {
mHead += numEventsRead;
mFreeSpace -= numEventsRead;
if (mHead > mBufferEnd) {
size_t s = mHead - mBufferEnd;
memcpy(mBuffer, mBufferEnd, s * sizeof(input_event));
mHead = mBuffer + s;
}
}
}
return numEventsRead;
}
ssize_t InputEventCircularReader::readEvent(input_event const** events)
{
*events = mCurr;
ssize_t available = (mBufferEnd - mBuffer) - mFreeSpace;
return available ? 1 : 0;
}
void InputEventCircularReader::next()
{
mCurr++;
mFreeSpace++;
if (mCurr >= mBufferEnd) {
mCurr = mBuffer;
}
}

View file

@ -0,0 +1,47 @@
/*
* 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.
*/
#ifndef ANDROID_INPUT_EVENT_READER_H
#define ANDROID_INPUT_EVENT_READER_H
#include <stdint.h>
#include <errno.h>
#include <sys/cdefs.h>
#include <sys/types.h>
/*****************************************************************************/
struct input_event;
class InputEventCircularReader
{
struct input_event* const mBuffer;
struct input_event* const mBufferEnd;
struct input_event* mHead;
struct input_event* mCurr;
ssize_t mFreeSpace;
public:
InputEventCircularReader(size_t numEvents);
~InputEventCircularReader();
ssize_t fill(int fd);
ssize_t readEvent(input_event const** events);
void next();
};
/*****************************************************************************/
#endif // ANDROID_INPUT_EVENT_READER_H

View file

@ -0,0 +1,202 @@
/*
* 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 <fcntl.h>
#include <errno.h>
#include <math.h>
#include <poll.h>
#include <unistd.h>
#include <dirent.h>
#include <sys/select.h>
#include <cutils/log.h>
#include <linux/kxtf9.h>
#include "KionixSensor.h"
#define KIONIX_IOCTL_ENABLE_OUTPUT KXTF9_IOCTL_ENABLE_OUTPUT
#define KIONIX_IOCTL_DISABLE_OUTPUT KXTF9_IOCTL_DISABLE_OUTPUT
#define KIONIX_IOCTL_GET_ENABLE KXTF9_IOCTL_GET_ENABLE
#define KIONIX_IOCTL_UPDATE_ODR KXTF9_IOCTL_UPDATE_ODR
#define KIONIX_UNIT_CONVERSION(value) ((value) * GRAVITY_EARTH / (1024.0f))
/*****************************************************************************/
KionixSensor::KionixSensor()
: SensorBase(DIR_DEV, INPUT_NAME_ACC),
mEnabled(0),
mDelay(-1),
mInputReader(32),
mHasPendingEvent(false)
{
mPendingEvent.version = sizeof(sensors_event_t);
mPendingEvent.sensor = ID_A;
mPendingEvent.type = SENSOR_TYPE_ACCELEROMETER;
memset(mPendingEvent.data, 0, sizeof(mPendingEvent.data));
open_device();
}
KionixSensor::~KionixSensor() {
if (mEnabled) {
setEnable(0, 0);
}
close_device();
}
int KionixSensor::setInitialState() {
struct input_absinfo absinfo;
if (mEnabled) {
if (!ioctl(data_fd, EVIOCGABS(EVENT_TYPE_ACCEL_X), &absinfo)) {
mPendingEvent.acceleration.x = KIONIX_UNIT_CONVERSION(absinfo.value);
}
if (!ioctl(data_fd, EVIOCGABS(EVENT_TYPE_ACCEL_Y), &absinfo)) {
mPendingEvent.acceleration.y = KIONIX_UNIT_CONVERSION(absinfo.value);
}
if (!ioctl(data_fd, EVIOCGABS(EVENT_TYPE_ACCEL_Z), &absinfo)) {
mPendingEvent.acceleration.z = KIONIX_UNIT_CONVERSION(absinfo.value);
}
}
return 0;
}
bool KionixSensor::hasPendingEvents() const {
return mHasPendingEvent;
}
int KionixSensor::setEnable(int32_t handle, int enabled) {
int err = 0;
int opDone = 0;
/* handle check */
if (handle != ID_A) {
ALOGE("KionixSensor: Invalid handle (%d)", handle);
return -EINVAL;
}
if (mEnabled <= 0) {
if (enabled) {
err = ioctl(dev_fd, KIONIX_IOCTL_ENABLE_OUTPUT);
opDone = 1;
}
} else if (mEnabled == 1) {
if (!enabled) {
err = ioctl(dev_fd, KIONIX_IOCTL_DISABLE_OUTPUT);
opDone = 1;
}
}
if (err != 0) {
ALOGE("KionixSensor: IOCTL failed (%s)", strerror(errno));
return err;
}
if (opDone) {
ALOGD("KionixSensor: Control set %d", enabled);
setInitialState();
}
if (enabled) {
mEnabled++;
if (mEnabled > 32767) mEnabled = 32767;
} else {
mEnabled--;
if (mEnabled < 0) mEnabled = 0;
}
ALOGD("KionixSensor: mEnabled = %d", mEnabled);
return err;
}
int KionixSensor::setDelay(int32_t handle, int64_t delay_ns)
{
int err = 0;
int ms;
/* handle check */
if (handle != ID_A) {
ALOGE("KionixSensor: Invalid handle (%d)", handle);
return -EINVAL;
}
if (mDelay != delay_ns) {
ms = delay_ns / 1000000;
if (ioctl(dev_fd, KIONIX_IOCTL_UPDATE_ODR, &ms)) {
return -errno;
}
mDelay = delay_ns;
}
return err;
}
int64_t KionixSensor::getDelay(int32_t handle)
{
return (handle == ID_A) ? mDelay : 0;
}
int KionixSensor::getEnable(int32_t handle)
{
return (handle == ID_A) ? mEnabled : 0;
}
int KionixSensor::readEvents(sensors_event_t* data, int count)
{
if (count < 1)
return -EINVAL;
if (mHasPendingEvent) {
mHasPendingEvent = false;
mPendingEvent.timestamp = getTimestamp();
*data = mPendingEvent;
return mEnabled ? 1 : 0;
}
ssize_t n = mInputReader.fill(data_fd);
if (n < 0)
return n;
int numEventReceived = 0;
input_event const* event;
while (count && mInputReader.readEvent(&event)) {
int type = event->type;
if (type == EV_ABS) {
float value = event->value;
if (event->code == EVENT_TYPE_ACCEL_X) {
mPendingEvent.acceleration.x = KIONIX_UNIT_CONVERSION(value);
} else if (event->code == EVENT_TYPE_ACCEL_Y) {
mPendingEvent.acceleration.y = KIONIX_UNIT_CONVERSION(value);
} else if (event->code == EVENT_TYPE_ACCEL_Z) {
mPendingEvent.acceleration.z = KIONIX_UNIT_CONVERSION(value);
}
} else if (type == EV_SYN) {
mPendingEvent.timestamp = timevalToNano(event->time);
if (mEnabled) {
*data++ = mPendingEvent;
count--;
numEventReceived++;
}
} else {
ALOGE("KionixSensor: unknown event (type=%d, code=%d)",
type, event->code);
}
mInputReader.next();
}
return numEventReceived;
}

View file

@ -0,0 +1,57 @@
/*
* Copyright (C) 2011 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.
*/
#ifndef ANDROID_KIONIX_SENSOR_H
#define ANDROID_KIONIX_SENSOR_H
#include <stdint.h>
#include <errno.h>
#include <sys/cdefs.h>
#include <sys/types.h>
#include "sensors.h"
#include "SensorBase.h"
#include "InputEventReader.h"
/*****************************************************************************/
struct input_event;
class KionixSensor : public SensorBase {
int mEnabled;
int64_t mDelay;
InputEventCircularReader mInputReader;
sensors_event_t mPendingEvent;
bool mHasPendingEvent;
char input_sysfs_path[PATH_MAX];
int input_sysfs_path_len;
int setInitialState();
public:
KionixSensor();
virtual ~KionixSensor();
virtual int readEvents(sensors_event_t* data, int count);
virtual bool hasPendingEvents() const;
virtual int setDelay(int32_t handle, int64_t ns);
virtual int setEnable(int32_t handle, int enabled);
virtual int64_t getDelay(int32_t handle);
virtual int getEnable(int32_t handle);
};
/*****************************************************************************/
#endif // ANDROID_KIONIX_SENSOR_H

View file

@ -0,0 +1,153 @@
/*
* 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 <fcntl.h>
#include <errno.h>
#include <math.h>
#include <poll.h>
#include <string.h>
#include <unistd.h>
#include <dirent.h>
#include <sys/select.h>
#include <cutils/log.h>
#include <linux/input.h>
#include "SensorBase.h"
/*****************************************************************************/
SensorBase::SensorBase(
const char* dev_name,
const char* data_name)
: dev_name(dev_name), data_name(data_name),
dev_fd(-1), data_fd(-1)
{
if (data_name) {
data_fd = openInput(data_name);
}
}
SensorBase::~SensorBase() {
if (data_fd >= 0) {
close(data_fd);
}
if (dev_fd >= 0) {
close(dev_fd);
}
}
int SensorBase::open_device() {
if (dev_fd<0 && dev_name) {
dev_fd = open(dev_name, O_RDONLY);
ALOGE_IF(dev_fd<0, "Couldn't open %s (%s)", dev_name, strerror(errno));
}
return 0;
}
int SensorBase::close_device() {
if (dev_fd >= 0) {
close(dev_fd);
dev_fd = -1;
}
return 0;
}
int SensorBase::write_sys_attribute(
const char *path, const char *value, int bytes)
{
int fd, amt;
fd = open(path, O_WRONLY);
if (fd < 0) {
ALOGE("SensorBase: write_attr failed to open %s (%s)",
path, strerror(errno));
return -1;
}
amt = write(fd, value, bytes);
amt = ((amt == -1) ? -errno : 0);
ALOGE_IF(amt < 0, "SensorBase: write_int failed to write %s (%s)",
path, strerror(errno));
close(fd);
return amt;
}
int SensorBase::getFd() const {
if (!data_name) {
return dev_fd;
}
return data_fd;
}
int SensorBase::setDelay(int32_t handle, int64_t ns) {
return 0;
}
int64_t SensorBase::getDelay(int32_t handle) {
return 0;
}
bool SensorBase::hasPendingEvents() const {
return false;
}
int64_t SensorBase::getTimestamp() {
struct timespec t;
t.tv_sec = t.tv_nsec = 0;
clock_gettime(CLOCK_MONOTONIC, &t);
return int64_t(t.tv_sec)*1000000000LL + t.tv_nsec;
}
int SensorBase::openInput(const char* inputName) {
int fd = -1;
const char *dirname = "/dev/input";
char devname[PATH_MAX];
char *filename;
DIR *dir;
struct dirent *de;
dir = opendir(dirname);
if(dir == NULL)
return -1;
strcpy(devname, dirname);
filename = devname + strlen(devname);
*filename++ = '/';
while((de = readdir(dir))) {
if(de->d_name[0] == '.' &&
(de->d_name[1] == '\0' ||
(de->d_name[1] == '.' && de->d_name[2] == '\0')))
continue;
strcpy(filename, de->d_name);
fd = open(devname, O_RDONLY);
if (fd>=0) {
char name[80];
if (ioctl(fd, EVIOCGNAME(sizeof(name) - 1), &name) < 1) {
name[0] = '\0';
}
if (!strcmp(name, inputName)) {
strcpy(input_name, filename);
break;
} else {
close(fd);
fd = -1;
}
}
}
closedir(dir);
ALOGE_IF(fd<0, "couldn't find '%s' input device", inputName);
return fd;
}

View file

@ -0,0 +1,74 @@
/*
* 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.
*/
#ifndef ANDROID_SENSOR_BASE_H
#define ANDROID_SENSOR_BASE_H
#include <stdint.h>
#include <errno.h>
#include <sys/cdefs.h>
#include <sys/types.h>
/*****************************************************************************/
struct sensors_event_t;
class SensorBase {
protected:
const char* dev_name;
const char* data_name;
char input_name[PATH_MAX];
int dev_fd;
int data_fd;
int openInput(const char* inputName);
static int64_t getTimestamp();
static int64_t timevalToNano(timeval const& t) {
return t.tv_sec*1000000000LL + t.tv_usec*1000;
}
int open_device();
int close_device();
int write_int(char const *path, int value);
int write_sys_attribute(
char const *path, char const *value, int bytes);
public:
SensorBase(
const char* dev_name,
const char* data_name);
virtual ~SensorBase();
virtual int readEvents(sensors_event_t* data, int count) = 0;
virtual bool hasPendingEvents() const;
virtual int getFd() const;
virtual int setDelay(int32_t handle, int64_t ns);
virtual int64_t getDelay(int32_t handle);
/* When this function is called, increments the reference counter. */
virtual int setEnable(int32_t handle, int enabled) = 0;
/* It returns the number of reference. */
virtual int getEnable(int32_t handle) = 0;
};
/*****************************************************************************/
#endif // ANDROID_SENSOR_BASE_H

View file

@ -0,0 +1,377 @@
/*
* 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.
*/
#define LOG_TAG "Sensors"
#include <hardware/sensors.h>
#include <fcntl.h>
#include <errno.h>
#include <dirent.h>
#include <math.h>
#include <poll.h>
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
#include <linux/input.h>
#include <utils/Atomic.h>
#include <utils/Log.h>
#include "sensors.h"
#if defined SENSORHAL_ACC_ADXL346
#include "AdxlSensor.h"
#elif defined SENSORHAL_ACC_KXTF9
#include "KionixSensor.h"
#else
#error "Sensor configuration ERROR: No sensor is defined."
#endif
#include "AkmSensor.h"
/*****************************************************************************/
#define DELAY_OUT_TIME 0x7FFFFFFF
#define LIGHT_SENSOR_POLLTIME 2000000000
#define SENSORS_ACCELERATION (1<<ID_A)
#define SENSORS_MAGNETIC_FIELD (1<<ID_M)
#define SENSORS_ORIENTATION (1<<ID_O)
#define SENSORS_ACCELERATION_HANDLE 0
#define SENSORS_MAGNETIC_FIELD_HANDLE 1
#define SENSORS_ORIENTATION_HANDLE 2
/*****************************************************************************/
/* The SENSORS Module */
static const struct sensor_t sSensorList[] = {
{ "AK8975 3-axis Magnetic field sensor",
"Asahi Kasei Microdevices",
1,
SENSORS_MAGNETIC_FIELD_HANDLE,
SENSOR_TYPE_MAGNETIC_FIELD, 1228.8f,
CONVERT_M, 0.35f, 10000, 0, 0, 0, 0, 0, 0, { } },
#ifdef SENSORHAL_ACC_ADXL346
{ "Analog Devices ADXL345/6 3-axis Accelerometer",
"ADI",
1, SENSORS_ACCELERATION_HANDLE,
SENSOR_TYPE_ACCELEROMETER, (GRAVITY_EARTH * 16.0f),
(GRAVITY_EARTH * 16.0f) / 4096.0f, 0.145f, 10000, 0, 0, 0, 0, 0, 0, { } },
{ "AK8975 Orientation sensor",
"Asahi Kasei Microdevices",
1, SENSORS_ORIENTATION_HANDLE,
SENSOR_TYPE_ORIENTATION, 360.0f,
CONVERT_O, 0.495f, 10000, 0, 0, 0, 0, 0, 0, { } }
#endif
#ifdef SENSORHAL_ACC_KXTF9
{ "Kionix KXTF9 3-axis Accelerometer",
"Kionix",
1, SENSORS_ACCELERATION_HANDLE,
SENSOR_TYPE_ACCELEROMETER, (GRAVITY_EARTH * 2.0f),
(GRAVITY_EARTH) / 1024.0f, 0.7f, 10000, 0, 0, 0, 0, 0, 0, { } },
{ "AK8975 Orientation sensor",
"Asahi Kasei Microdevices",
1, SENSORS_ORIENTATION_HANDLE,
SENSOR_TYPE_ORIENTATION, 360.0f,
CONVERT_O, 1.05f, 10000, 0, 0, 0, 0, 0, 0, { } }
#endif
};
static int open_sensors(const struct hw_module_t* module, const char* id,
struct hw_device_t** device);
static int sensors__get_sensors_list(struct sensors_module_t* module,
struct sensor_t const** list)
{
*list = sSensorList;
return ARRAY_SIZE(sSensorList);
}
static struct hw_module_methods_t sensors_module_methods = {
.open = open_sensors
};
struct sensors_module_t HAL_MODULE_INFO_SYM = {
.common = {
.tag = HARDWARE_MODULE_TAG,
.version_major = 1,
.version_minor = 0,
.id = SENSORS_HARDWARE_MODULE_ID,
.name = "AKM Sensor module",
.author = "Asahi Kasei Microdevices",
.methods = &sensors_module_methods,
.dso = NULL,
.reserved = {0},
},
.get_sensors_list = sensors__get_sensors_list,
};
struct sensors_poll_context_t {
struct sensors_poll_device_t device; // must be first
sensors_poll_context_t();
~sensors_poll_context_t();
int activate(int handle, int enabled);
int setDelay(int handle, int64_t ns);
int setDelay_sub(int handle, int64_t ns);
int pollEvents(sensors_event_t* data, int count);
private:
enum {
acc = 0,
akm = 1,
numSensorDrivers,
numFds,
};
static const size_t wake = numFds - 1;
static const char WAKE_MESSAGE = 'W';
struct pollfd mPollFds[numFds];
int mWritePipeFd;
SensorBase* mSensors[numSensorDrivers];
/* These function will be different depends on
* which sensor is implemented in AKMD program.
*/
int handleToDriver(int handle);
int proxy_enable(int handle, int enabled);
int proxy_setDelay(int handle, int64_t ns);
};
/*****************************************************************************/
sensors_poll_context_t::sensors_poll_context_t()
{
#ifdef SENSORHAL_ACC_ADXL346
mSensors[acc] = new AdxlSensor();
#endif
#ifdef SENSORHAL_ACC_KXTF9
mSensors[acc] = new KionixSensor();
#endif
mPollFds[acc].fd = mSensors[acc]->getFd();
mPollFds[acc].events = POLLIN;
mPollFds[acc].revents = 0;
mSensors[akm] = new AkmSensor();
mPollFds[akm].fd = mSensors[akm]->getFd();
mPollFds[akm].events = POLLIN;
mPollFds[akm].revents = 0;
int wakeFds[2];
int result = pipe(wakeFds);
ALOGE_IF(result<0, "error creating wake pipe (%s)", strerror(errno));
fcntl(wakeFds[0], F_SETFL, O_NONBLOCK);
fcntl(wakeFds[1], F_SETFL, O_NONBLOCK);
mWritePipeFd = wakeFds[1];
mPollFds[wake].fd = wakeFds[0];
mPollFds[wake].events = POLLIN;
mPollFds[wake].revents = 0;
}
sensors_poll_context_t::~sensors_poll_context_t() {
for (int i=0 ; i<numSensorDrivers ; i++) {
delete mSensors[i];
}
close(mPollFds[wake].fd);
close(mWritePipeFd);
}
int sensors_poll_context_t::handleToDriver(int handle) {
switch (handle) {
case ID_A:
return acc;
case ID_M:
case ID_O:
return akm;
}
return -EINVAL;
}
int sensors_poll_context_t::activate(int handle, int enabled) {
int drv = handleToDriver(handle);
int err;
switch (handle) {
case ID_A:
case ID_M:
/* No dependencies */
break;
case ID_O:
/* These sensors depend on ID_A and ID_M */
mSensors[handleToDriver(ID_A)]->setEnable(ID_A, enabled);
mSensors[handleToDriver(ID_M)]->setEnable(ID_M, enabled);
break;
default:
return -EINVAL;
}
err = mSensors[drv]->setEnable(handle, enabled);
if (enabled && !err) {
const char wakeMessage(WAKE_MESSAGE);
int result = write(mWritePipeFd, &wakeMessage, 1);
ALOGE_IF(result<0, "error sending wake message (%s)", strerror(errno));
}
return err;
}
int sensors_poll_context_t::setDelay(int handle, int64_t ns) {
switch (handle) {
case ID_A:
case ID_M:
/* No dependencies */
break;
case ID_O:
/* These sensors depend on ID_A and ID_M */
setDelay_sub(ID_A, ns);
setDelay_sub(ID_M, ns);
break;
default:
return -EINVAL;
}
return setDelay_sub(handle, ns);
}
int sensors_poll_context_t::setDelay_sub(int handle, int64_t ns) {
int drv = handleToDriver(handle);
int en = mSensors[drv]->getEnable(handle);
int64_t cur = mSensors[drv]->getDelay(handle);
int err = 0;
if (en <= 1) {
/* no dependencies */
if (cur != ns) {
err = mSensors[drv]->setDelay(handle, ns);
}
} else {
/* has dependencies, choose shorter interval */
if (cur > ns) {
err = mSensors[drv]->setDelay(handle, ns);
}
}
return err;
}
int sensors_poll_context_t::pollEvents(sensors_event_t* data, int count)
{
int nbEvents = 0;
int n = 0;
do {
// see if we have some leftover from the last poll()
for (int i=0 ; count && i<numSensorDrivers ; i++) {
SensorBase* const sensor(mSensors[i]);
if ((mPollFds[i].revents & POLLIN) || (sensor->hasPendingEvents())) {
int nb = sensor->readEvents(data, count);
if (nb < count) {
// no more data for this sensor
mPollFds[i].revents = 0;
}
if ((0 != nb) && (acc == i)) {
((AkmSensor*)(mSensors[akm]))->setAccel(&data[nb-1]);
}
count -= nb;
nbEvents += nb;
data += nb;
}
}
if (count) {
// we still have some room, so try to see if we can get
// some events immediately or just wait if we don't have
// anything to return
n = poll(mPollFds, numFds, nbEvents ? 0 : -1);
if (n<0) {
ALOGE("poll() failed (%s)", strerror(errno));
return -errno;
}
if (mPollFds[wake].revents & POLLIN) {
char msg;
int result = read(mPollFds[wake].fd, &msg, 1);
ALOGE_IF(result<0, "error reading from wake pipe (%s)", strerror(errno));
ALOGE_IF(msg != WAKE_MESSAGE, "unknown message on wake queue (0x%02x)", int(msg));
mPollFds[wake].revents = 0;
}
}
// if we have events and space, go read them
} while (n && count);
return nbEvents;
}
/*****************************************************************************/
static int poll__close(struct hw_device_t *dev)
{
sensors_poll_context_t *ctx = (sensors_poll_context_t *)dev;
if (ctx) {
delete ctx;
}
return 0;
}
static int poll__activate(struct sensors_poll_device_t *dev,
int handle, int enabled) {
sensors_poll_context_t *ctx = (sensors_poll_context_t *)dev;
return ctx->activate(handle, enabled);
}
static int poll__setDelay(struct sensors_poll_device_t *dev,
int handle, int64_t ns) {
sensors_poll_context_t *ctx = (sensors_poll_context_t *)dev;
return ctx->setDelay(handle, ns);
}
static int poll__poll(struct sensors_poll_device_t *dev,
sensors_event_t* data, int count) {
sensors_poll_context_t *ctx = (sensors_poll_context_t *)dev;
return ctx->pollEvents(data, count);
}
/*****************************************************************************/
/** Open a new instance of a sensor device using name */
static int open_sensors(const struct hw_module_t* module, const char* id,
struct hw_device_t** device)
{
int status = -EINVAL;
sensors_poll_context_t *dev = new sensors_poll_context_t();
memset(&dev->device, 0, sizeof(sensors_poll_device_t));
dev->device.common.tag = HARDWARE_DEVICE_TAG;
dev->device.common.version = 0;
dev->device.common.module = const_cast<hw_module_t*>(module);
dev->device.common.close = poll__close;
dev->device.activate = poll__activate;
dev->device.setDelay = poll__setDelay;
dev->device.poll = poll__poll;
*device = &dev->device.common;
status = 0;
return status;
}

View file

@ -0,0 +1,85 @@
/*
* 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.
*/
#ifndef ANDROID_SENSORS_H
#define ANDROID_SENSORS_H
#include <stdint.h>
#include <errno.h>
#include <sys/cdefs.h>
#include <sys/types.h>
#include <linux/input.h>
#include <hardware/hardware.h>
#include <hardware/sensors.h>
__BEGIN_DECLS
/*****************************************************************************/
#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
#define ID_A (0)
#define ID_M (1)
#define ID_O (2)
/*****************************************************************************/
/*
* The SENSORS Module
*/
/*****************************************************************************/
/* For ADXL346 */
#define EVENT_TYPE_ACCEL_X ABS_X
#define EVENT_TYPE_ACCEL_Y ABS_Y
#define EVENT_TYPE_ACCEL_Z ABS_Z
#define EVENT_TYPE_ACCEL_STATUS ABS_THROTTLE
/* For AK8975 */
#define EVENT_TYPE_MAGV_X ABS_RX
#define EVENT_TYPE_MAGV_Y ABS_RY
#define EVENT_TYPE_MAGV_Z ABS_RZ
#define EVENT_TYPE_MAGV_STATUS ABS_RUDDER
/* Fro AKM Algorithm */
#define EVENT_TYPE_YAW ABS_HAT0X
#define EVENT_TYPE_PITCH ABS_HAT0Y
#define EVENT_TYPE_ROLL ABS_HAT1X
#define EVENT_TYPE_ORIENT_STATUS ABS_HAT1Y
/* conversion of acceleration data to SI units (m/s^2) */
/* 720 LSB = 1G */
#define LSG (256.0f)
#define AKSC_LSG (720.0f)
#define CONVERT_A (GRAVITY_EARTH / LSG)
/* conversion of magnetic data to uT units */
#define CONVERT_M (0.06f)
/* conversion of orientation data to degree units */
#define CONVERT_O (0.015625f)
#define SENSOR_STATE_MASK (0x7FFF)
/*****************************************************************************/
__END_DECLS
#endif // ANDROID_SENSORS_H