summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--device_base.mk2
-rw-r--r--libsensors/AkmSensor.cpp306
-rw-r--r--libsensors/AkmSensor.h62
-rw-r--r--libsensors/Android.mk40
-rw-r--r--libsensors/GyroSensor.cpp185
-rw-r--r--libsensors/GyroSensor.h55
-rw-r--r--libsensors/InputEventReader.cpp88
-rw-r--r--libsensors/InputEventReader.h47
-rw-r--r--libsensors/LightSensor.cpp146
-rw-r--r--libsensors/LightSensor.h54
-rw-r--r--libsensors/ProximitySensor.cpp144
-rw-r--r--libsensors/ProximitySensor.h54
-rw-r--r--libsensors/SensorBase.cpp128
-rw-r--r--libsensors/SensorBase.h65
-rw-r--r--libsensors/ak8973b.h51
-rw-r--r--libsensors/sensors.cpp329
-rw-r--r--libsensors/sensors.h115
-rw-r--r--sensors/Android.mk40
-rw-r--r--sensors/ak8973-reg.h47
-rw-r--r--sensors/ak8973.h72
-rw-r--r--sensors/akm8973.c960
-rw-r--r--sensors/gp2a_light.c239
-rw-r--r--sensors/gp2a_proximity.c213
-rw-r--r--sensors/herring_sensors.c289
-rw-r--r--sensors/herring_sensors.h104
-rw-r--r--sensors/input.c335
-rw-r--r--sensors/k3g.c254
-rw-r--r--sensors/kr3dm.c375
-rw-r--r--sensors/kr3dm.h46
-rw-r--r--sensors/orientation.c444
30 files changed, 3419 insertions, 1870 deletions
diff --git a/device_base.mk b/device_base.mk
index ee654ef..3e8ef63 100644
--- a/device_base.mk
+++ b/device_base.mk
@@ -120,7 +120,7 @@ PRODUCT_PACKAGES += \
lights.s5pc110 \
hwcomposer.s5pc110 \
sensors.herring \
- power.s5pc110
+ power.s5pc110
# Torch
PRODUCT_PACKAGES += \
diff --git a/libsensors/AkmSensor.cpp b/libsensors/AkmSensor.cpp
deleted file mode 100644
index ce28dbf..0000000
--- a/libsensors/AkmSensor.cpp
+++ /dev/null
@@ -1,306 +0,0 @@
-/*
- * 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 <dlfcn.h>
-
-#include "ak8973b.h"
-
-#include <cutils/log.h>
-
-#include "AkmSensor.h"
-
-/*****************************************************************************/
-
-int (*akm_is_sensor_enabled)(uint32_t sensor_type);
-int (*akm_enable_sensor)(uint32_t sensor_type);
-int (*akm_disable_sensor)(uint32_t sensor_type);
-int (*akm_set_delay)(uint32_t sensor_type, uint64_t delay);
-
-int stub_is_sensor_enabled(uint32_t sensor_type) {
- return 0;
-}
-
-int stub_enable_disable_sensor(uint32_t sensor_type) {
- return -ENODEV;
-}
-
-int stub_set_delay(uint32_t sensor_type, uint64_t delay) {
- return -ENODEV;
-}
-
-AkmSensor::AkmSensor()
-: SensorBase(NULL, NULL),
- mEnabled(0),
- mPendingMask(0),
- mInputReader(32)
-{
- /* Open the library before opening the input device. The library
- * creates a uinput device.
- */
- if (loadAKMLibrary() == 0) {
- data_name = "compass";
- data_fd = openInput("compass");
- }
-
- 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;
-
- // read the actual value of all sensors if they're enabled already
- struct input_absinfo absinfo;
- short flags = 0;
-
- if (akm_is_sensor_enabled(SENSOR_TYPE_ACCELEROMETER)) {
- mEnabled |= 1<<Accelerometer;
- if (!ioctl(data_fd, EVIOCGABS(EVENT_TYPE_ACCEL_X), &absinfo)) {
- mPendingEvents[Accelerometer].acceleration.x = absinfo.value * CONVERT_A_X;
- }
- if (!ioctl(data_fd, EVIOCGABS(EVENT_TYPE_ACCEL_Y), &absinfo)) {
- mPendingEvents[Accelerometer].acceleration.y = absinfo.value * CONVERT_A_Y;
- }
- if (!ioctl(data_fd, EVIOCGABS(EVENT_TYPE_ACCEL_Z), &absinfo)) {
- mPendingEvents[Accelerometer].acceleration.z = absinfo.value * CONVERT_A_Z;
- }
- }
- if (akm_is_sensor_enabled(SENSOR_TYPE_MAGNETIC_FIELD)) {
- mEnabled |= 1<<MagneticField;
- if (!ioctl(data_fd, EVIOCGABS(EVENT_TYPE_MAGV_X), &absinfo)) {
- mPendingEvents[MagneticField].magnetic.x = absinfo.value * CONVERT_M_X;
- }
- if (!ioctl(data_fd, EVIOCGABS(EVENT_TYPE_MAGV_Y), &absinfo)) {
- mPendingEvents[MagneticField].magnetic.y = absinfo.value * CONVERT_M_Y;
- }
- if (!ioctl(data_fd, EVIOCGABS(EVENT_TYPE_MAGV_Z), &absinfo)) {
- mPendingEvents[MagneticField].magnetic.z = absinfo.value * CONVERT_M_Z;
- }
- }
- if (akm_is_sensor_enabled(SENSOR_TYPE_ORIENTATION)) {
- mEnabled |= 1<<Orientation;
- if (!ioctl(data_fd, EVIOCGABS(EVENT_TYPE_YAW), &absinfo)) {
- mPendingEvents[Orientation].orientation.azimuth = absinfo.value;
- }
- if (!ioctl(data_fd, EVIOCGABS(EVENT_TYPE_PITCH), &absinfo)) {
- mPendingEvents[Orientation].orientation.pitch = absinfo.value;
- }
- if (!ioctl(data_fd, EVIOCGABS(EVENT_TYPE_ROLL), &absinfo)) {
- mPendingEvents[Orientation].orientation.roll = -absinfo.value;
- }
- if (!ioctl(data_fd, EVIOCGABS(EVENT_TYPE_ORIENT_STATUS), &absinfo)) {
- mPendingEvents[Orientation].orientation.status = uint8_t(absinfo.value & SENSOR_STATE_MASK);
- }
- }
-
- // disable temperature sensor, since it is not supported
- akm_disable_sensor(SENSOR_TYPE_TEMPERATURE);
-}
-
-AkmSensor::~AkmSensor()
-{
- if (mLibAKM) {
- unsigned ref = ::dlclose(mLibAKM);
- }
-}
-
-int AkmSensor::enable(int32_t handle, int en)
-{
- int what = -1;
-
- switch (handle) {
- case ID_A: what = Accelerometer; break;
- case ID_M: what = MagneticField; break;
- case ID_O: what = Orientation; break;
- }
-
- if (uint32_t(what) >= numSensors)
- return -EINVAL;
-
- int newState = en ? 1 : 0;
- int err = 0;
-
- if ((uint32_t(newState)<<what) != (mEnabled & (1<<what))) {
- uint32_t sensor_type;
- switch (what) {
- case Accelerometer: sensor_type = SENSOR_TYPE_ACCELEROMETER; break;
- case MagneticField: sensor_type = SENSOR_TYPE_MAGNETIC_FIELD; break;
- case Orientation: sensor_type = SENSOR_TYPE_ORIENTATION; break;
- }
- short flags = newState;
- if (en)
- err = akm_enable_sensor(sensor_type);
- else
- err = akm_disable_sensor(sensor_type);
-
- ALOGE_IF(err, "Could not change sensor state (%s)", strerror(-err));
- if (!err) {
- mEnabled &= ~(1<<what);
- mEnabled |= (uint32_t(flags)<<what);
- }
- }
- return err;
-}
-
-int AkmSensor::setDelay(int32_t handle, int64_t ns)
-{
- uint32_t sensor_type = 0;
-
- if (ns < 0)
- return -EINVAL;
-
- switch (handle) {
- case ID_A: sensor_type = SENSOR_TYPE_ACCELEROMETER; break;
- case ID_M: sensor_type = SENSOR_TYPE_MAGNETIC_FIELD; break;
- case ID_O: sensor_type = SENSOR_TYPE_ORIENTATION; break;
- }
-
- if (sensor_type == 0)
- return -EINVAL;
-
- return akm_set_delay(sensor_type, ns);
-}
-
-int AkmSensor::loadAKMLibrary()
-{
- mLibAKM = dlopen("libakm.so", RTLD_NOW);
-
- if (!mLibAKM) {
- akm_is_sensor_enabled = stub_is_sensor_enabled;
- akm_enable_sensor = stub_enable_disable_sensor;
- akm_disable_sensor = stub_enable_disable_sensor;
- akm_set_delay = stub_set_delay;
- ALOGE("AkmSensor: unable to load AKM Library, %s", dlerror());
- return -ENOENT;
- }
-
- *(void **)&akm_is_sensor_enabled = dlsym(mLibAKM, "akm_is_sensor_enabled");
- *(void **)&akm_enable_sensor = dlsym(mLibAKM, "akm_enable_sensor");
- *(void **)&akm_disable_sensor = dlsym(mLibAKM, "akm_disable_sensor");
- *(void **)&akm_set_delay = dlsym(mLibAKM, "akm_set_delay");
-
- 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_REL) {
- 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;
- if (mEnabled & (1<<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;
-}
-
-void AkmSensor::processEvent(int code, int value)
-{
- switch (code) {
- case EVENT_TYPE_ACCEL_X:
- mPendingMask |= 1<<Accelerometer;
- mPendingEvents[Accelerometer].acceleration.x = value * CONVERT_A_X;
- break;
- case EVENT_TYPE_ACCEL_Y:
- mPendingMask |= 1<<Accelerometer;
- mPendingEvents[Accelerometer].acceleration.y = value * CONVERT_A_Y;
- break;
- case EVENT_TYPE_ACCEL_Z:
- mPendingMask |= 1<<Accelerometer;
- mPendingEvents[Accelerometer].acceleration.z = value * CONVERT_A_Z;
- break;
-
- case EVENT_TYPE_MAGV_X:
- mPendingMask |= 1<<MagneticField;
- mPendingEvents[MagneticField].magnetic.x = value * CONVERT_M_X;
- break;
- case EVENT_TYPE_MAGV_Y:
- mPendingMask |= 1<<MagneticField;
- mPendingEvents[MagneticField].magnetic.y = value * CONVERT_M_Y;
- break;
- case EVENT_TYPE_MAGV_Z:
- mPendingMask |= 1<<MagneticField;
- mPendingEvents[MagneticField].magnetic.z = value * CONVERT_M_Z;
- break;
-
- case EVENT_TYPE_YAW:
- mPendingMask |= 1<<Orientation;
- mPendingEvents[Orientation].orientation.azimuth = value * CONVERT_O_A;
- break;
- case EVENT_TYPE_PITCH:
- mPendingMask |= 1<<Orientation;
- mPendingEvents[Orientation].orientation.pitch = value * CONVERT_O_P;
- break;
- case EVENT_TYPE_ROLL:
- mPendingMask |= 1<<Orientation;
- mPendingEvents[Orientation].orientation.roll = value * CONVERT_O_R;
- break;
- case EVENT_TYPE_ORIENT_STATUS:
- uint8_t status = uint8_t(value & SENSOR_STATE_MASK);
- if (status == 4)
- status = 0;
- mPendingMask |= 1<<Orientation;
- mPendingEvents[Orientation].orientation.status = status;
- break;
- }
-}
diff --git a/libsensors/AkmSensor.h b/libsensors/AkmSensor.h
deleted file mode 100644
index 44214e0..0000000
--- a/libsensors/AkmSensor.h
+++ /dev/null
@@ -1,62 +0,0 @@
-/*
- * 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 = 1,
- Orientation = 2,
- numSensors
- };
-
- virtual int setDelay(int32_t handle, int64_t ns);
- virtual int enable(int32_t handle, int enabled);
- virtual int readEvents(sensors_event_t* data, int count);
- void processEvent(int code, int value);
-
-private:
- int loadAKMLibrary();
- void *mLibAKM;
- uint32_t mEnabled;
- uint32_t mPendingMask;
- InputEventCircularReader mInputReader;
- sensors_event_t mPendingEvents[numSensors];
-};
-
-/*****************************************************************************/
-
-#endif // ANDROID_AKM_SENSOR_H
diff --git a/libsensors/Android.mk b/libsensors/Android.mk
deleted file mode 100644
index b451d4a..0000000
--- a/libsensors/Android.mk
+++ /dev/null
@@ -1,40 +0,0 @@
-# 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 stored in
-# hw/<SENSORS_HARDWARE_MODULE_ID>.<ro.product.board>.so
-include $(CLEAR_VARS)
-
-LOCAL_MODULE := sensors.herring
-
-LOCAL_MODULE_PATH := $(TARGET_OUT_SHARED_LIBRARIES)/hw
-
-LOCAL_MODULE_TAGS := optional
-
-LOCAL_CFLAGS := -DLOG_TAG=\"Sensors\"
-LOCAL_SRC_FILES := \
- sensors.cpp \
- SensorBase.cpp \
- LightSensor.cpp \
- ProximitySensor.cpp \
- AkmSensor.cpp \
- GyroSensor.cpp \
- InputEventReader.cpp
-
-LOCAL_SHARED_LIBRARIES := liblog libcutils libdl
-
-include $(BUILD_SHARED_LIBRARY)
diff --git a/libsensors/GyroSensor.cpp b/libsensors/GyroSensor.cpp
deleted file mode 100644
index c54235d..0000000
--- a/libsensors/GyroSensor.cpp
+++ /dev/null
@@ -1,185 +0,0 @@
-/*
- * 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 "GyroSensor.h"
-
-#define FETCH_FULL_EVENT_BEFORE_RETURN 1
-#define IGNORE_EVENT_TIME 350000000
-/*****************************************************************************/
-
-GyroSensor::GyroSensor()
- : SensorBase(NULL, "gyro"),
- mEnabled(0),
- mInputReader(4),
- mHasPendingEvent(false),
- mEnabledTime(0)
-{
- mPendingEvent.version = sizeof(sensors_event_t);
- mPendingEvent.sensor = ID_GY;
- mPendingEvent.type = SENSOR_TYPE_GYROSCOPE;
- memset(mPendingEvent.data, 0, sizeof(mPendingEvent.data));
-
- if (data_fd) {
- strcpy(input_sysfs_path, "/sys/class/input/");
- strcat(input_sysfs_path, input_name);
- strcat(input_sysfs_path, "/device/");
- input_sysfs_path_len = strlen(input_sysfs_path);
- enable(0, 1);
- }
-}
-
-GyroSensor::~GyroSensor() {
- if (mEnabled) {
- enable(0, 0);
- }
-}
-
-int GyroSensor::setInitialState() {
- struct input_absinfo absinfo_x;
- struct input_absinfo absinfo_y;
- struct input_absinfo absinfo_z;
- float value;
- if (!ioctl(data_fd, EVIOCGABS(EVENT_TYPE_GYRO_X), &absinfo_x) &&
- !ioctl(data_fd, EVIOCGABS(EVENT_TYPE_GYRO_X), &absinfo_y) &&
- !ioctl(data_fd, EVIOCGABS(EVENT_TYPE_GYRO_X), &absinfo_z)) {
- value = absinfo_x.value;
- mPendingEvent.data[0] = value * CONVERT_GYRO_X;
- value = absinfo_x.value;
- mPendingEvent.data[1] = value * CONVERT_GYRO_Y;
- value = absinfo_x.value;
- mPendingEvent.data[2] = value * CONVERT_GYRO_Z;
- mHasPendingEvent = true;
- }
- return 0;
-}
-
-int GyroSensor::enable(int32_t, int en) {
- int flags = en ? 1 : 0;
- if (flags != mEnabled) {
- int fd;
- strcpy(&input_sysfs_path[input_sysfs_path_len], "enable");
- fd = open(input_sysfs_path, O_RDWR);
- if (fd >= 0) {
- char buf[2];
- int err;
- buf[1] = 0;
- if (flags) {
- buf[0] = '1';
- mEnabledTime = getTimestamp() + IGNORE_EVENT_TIME;
- } else {
- buf[0] = '0';
- }
- err = write(fd, buf, sizeof(buf));
- close(fd);
- mEnabled = flags;
- setInitialState();
- return 0;
- }
- return -1;
- }
- return 0;
-}
-
-bool GyroSensor::hasPendingEvents() const {
- return mHasPendingEvent;
-}
-
-int GyroSensor::setDelay(int32_t handle, int64_t delay_ns)
-{
- int fd;
- strcpy(&input_sysfs_path[input_sysfs_path_len], "poll_delay");
- fd = open(input_sysfs_path, O_RDWR);
- if (fd >= 0) {
- char buf[80];
- sprintf(buf, "%lld", delay_ns);
- write(fd, buf, strlen(buf)+1);
- close(fd);
- return 0;
- }
- return -1;
-}
-
-int GyroSensor::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;
-
-#if FETCH_FULL_EVENT_BEFORE_RETURN
-again:
-#endif
- while (count && mInputReader.readEvent(&event)) {
- int type = event->type;
- if (type == EV_REL) {
- float value = event->value;
- if (event->code == EVENT_TYPE_GYRO_X) {
- mPendingEvent.data[0] = value * CONVERT_GYRO_X;
- } else if (event->code == EVENT_TYPE_GYRO_Y) {
- mPendingEvent.data[1] = value * CONVERT_GYRO_Y;
- } else if (event->code == EVENT_TYPE_GYRO_Z) {
- mPendingEvent.data[2] = value * CONVERT_GYRO_Z;
- }
- } else if (type == EV_SYN) {
- mPendingEvent.timestamp = timevalToNano(event->time);
- if (mEnabled) {
- if (mPendingEvent.timestamp >= mEnabledTime) {
- *data++ = mPendingEvent;
- numEventReceived++;
- }
- count--;
- }
- } else {
- ALOGE("GyroSensor: unknown event (type=%d, code=%d)",
- type, event->code);
- }
- mInputReader.next();
- }
-
-#if FETCH_FULL_EVENT_BEFORE_RETURN
- /* if we didn't read a complete event, see if we can fill and
- try again instead of returning with nothing and redoing poll. */
- if (numEventReceived == 0 && mEnabled == 1) {
- n = mInputReader.fill(data_fd);
- if (n)
- goto again;
- }
-#endif
-
- return numEventReceived;
-}
-
diff --git a/libsensors/GyroSensor.h b/libsensors/GyroSensor.h
deleted file mode 100644
index e8997de..0000000
--- a/libsensors/GyroSensor.h
+++ /dev/null
@@ -1,55 +0,0 @@
-/*
- * 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_GYRO_SENSOR_H
-#define ANDROID_GYRO_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 GyroSensor : public SensorBase {
- int mEnabled;
- InputEventCircularReader mInputReader;
- sensors_event_t mPendingEvent;
- bool mHasPendingEvent;
- char input_sysfs_path[PATH_MAX];
- int input_sysfs_path_len;
- int64_t mEnabledTime;
-
- int setInitialState();
-
-public:
- GyroSensor();
- virtual ~GyroSensor();
- virtual int readEvents(sensors_event_t* data, int count);
- virtual bool hasPendingEvents() const;
- virtual int setDelay(int32_t handle, int64_t ns);
- virtual int enable(int32_t handle, int enabled);
-};
-
-/*****************************************************************************/
-
-#endif // ANDROID_GYRO_SENSOR_H
diff --git a/libsensors/InputEventReader.cpp b/libsensors/InputEventReader.cpp
deleted file mode 100644
index 1014f29..0000000
--- a/libsensors/InputEventReader.cpp
+++ /dev/null
@@ -1,88 +0,0 @@
-/*
- * 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 <unistd.h>
-#include <poll.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;
- }
-}
diff --git a/libsensors/InputEventReader.h b/libsensors/InputEventReader.h
deleted file mode 100644
index 180aade..0000000
--- a/libsensors/InputEventReader.h
+++ /dev/null
@@ -1,47 +0,0 @@
-/*
- * 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
diff --git a/libsensors/LightSensor.cpp b/libsensors/LightSensor.cpp
deleted file mode 100644
index fe0b8b3..0000000
--- a/libsensors/LightSensor.cpp
+++ /dev/null
@@ -1,146 +0,0 @@
-/*
- * 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 "LightSensor.h"
-
-/*****************************************************************************/
-
-LightSensor::LightSensor()
- : SensorBase(NULL, "lightsensor-level"),
- mEnabled(0),
- mInputReader(4),
- mHasPendingEvent(false)
-{
- mPendingEvent.version = sizeof(sensors_event_t);
- mPendingEvent.sensor = ID_L;
- mPendingEvent.type = SENSOR_TYPE_LIGHT;
- memset(mPendingEvent.data, 0, sizeof(mPendingEvent.data));
-
- if (data_fd) {
- strcpy(input_sysfs_path, "/sys/class/input/");
- strcat(input_sysfs_path, input_name);
- strcat(input_sysfs_path, "/device/");
- input_sysfs_path_len = strlen(input_sysfs_path);
- enable(0, 1);
- }
-}
-
-LightSensor::~LightSensor() {
- if (mEnabled) {
- enable(0, 0);
- }
-}
-
-int LightSensor::setDelay(int32_t handle, int64_t ns)
-{
- int fd;
- strcpy(&input_sysfs_path[input_sysfs_path_len], "poll_delay");
- fd = open(input_sysfs_path, O_RDWR);
- if (fd >= 0) {
- char buf[80];
- sprintf(buf, "%lld", ns);
- write(fd, buf, strlen(buf)+1);
- close(fd);
- return 0;
- }
- return -1;
-}
-
-int LightSensor::enable(int32_t handle, int en)
-{
- int flags = en ? 1 : 0;
- if (flags != mEnabled) {
- int fd;
- strcpy(&input_sysfs_path[input_sysfs_path_len], "enable");
- fd = open(input_sysfs_path, O_RDWR);
- if (fd >= 0) {
- char buf[2];
- int err;
- buf[1] = 0;
- if (flags) {
- buf[0] = '1';
- } else {
- buf[0] = '0';
- }
- err = write(fd, buf, sizeof(buf));
- close(fd);
- mEnabled = flags;
- return 0;
- }
- return -1;
- }
- return 0;
-}
-
-bool LightSensor::hasPendingEvents() const {
- return mHasPendingEvent;
-}
-
-int LightSensor::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) {
- if (event->code == EVENT_TYPE_LIGHT) {
- // Convert adc value to lux assuming:
- // I = 10 * log(Ev) uA
- // R = 47kOhm
- // Max adc value 4095 = 3.3V
- // 1/4 of light reaches sensor
- mPendingEvent.light = powf(10, event->value * (330.0f / 4095.0f / 47.0f)) * 4;
- }
- } else if (type == EV_SYN) {
- mPendingEvent.timestamp = timevalToNano(event->time);
- if (mEnabled) {
- *data++ = mPendingEvent;
- count--;
- numEventReceived++;
- }
- } else {
- ALOGE("LightSensor: unknown event (type=%d, code=%d)",
- type, event->code);
- }
- mInputReader.next();
- }
-
- return numEventReceived;
-}
diff --git a/libsensors/LightSensor.h b/libsensors/LightSensor.h
deleted file mode 100644
index 79e0ccf..0000000
--- a/libsensors/LightSensor.h
+++ /dev/null
@@ -1,54 +0,0 @@
-/*
- * 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_LIGHT_SENSOR_H
-#define ANDROID_LIGHT_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 LightSensor : public SensorBase {
- int mEnabled;
- InputEventCircularReader mInputReader;
- sensors_event_t mPendingEvent;
- bool mHasPendingEvent;
- char input_sysfs_path[PATH_MAX];
- int input_sysfs_path_len;
-
- int setInitialState();
-
-public:
- LightSensor();
- virtual ~LightSensor();
- virtual int readEvents(sensors_event_t* data, int count);
- virtual bool hasPendingEvents() const;
- virtual int setDelay(int32_t handle, int64_t ns);
- virtual int enable(int32_t handle, int enabled);
-};
-
-/*****************************************************************************/
-
-#endif // ANDROID_LIGHT_SENSOR_H
diff --git a/libsensors/ProximitySensor.cpp b/libsensors/ProximitySensor.cpp
deleted file mode 100644
index 77a7f3c..0000000
--- a/libsensors/ProximitySensor.cpp
+++ /dev/null
@@ -1,144 +0,0 @@
-/*
- * 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 "ProximitySensor.h"
-
-/*****************************************************************************/
-
-ProximitySensor::ProximitySensor()
- : SensorBase(NULL, "proximity"),
- mEnabled(0),
- mInputReader(4),
- mHasPendingEvent(false)
-{
- mPendingEvent.version = sizeof(sensors_event_t);
- mPendingEvent.sensor = ID_P;
- mPendingEvent.type = SENSOR_TYPE_PROXIMITY;
- memset(mPendingEvent.data, 0, sizeof(mPendingEvent.data));
-
- if (data_fd) {
- strcpy(input_sysfs_path, "/sys/class/input/");
- strcat(input_sysfs_path, input_name);
- strcat(input_sysfs_path, "/device/");
- input_sysfs_path_len = strlen(input_sysfs_path);
- enable(0, 1);
- }
-}
-
-ProximitySensor::~ProximitySensor() {
- if (mEnabled) {
- enable(0, 0);
- }
-}
-
-int ProximitySensor::setInitialState() {
- struct input_absinfo absinfo;
- if (!ioctl(data_fd, EVIOCGABS(EVENT_TYPE_PROXIMITY), &absinfo)) {
- // make sure to report an event immediately
- mHasPendingEvent = true;
- mPendingEvent.distance = indexToValue(absinfo.value);
- }
- return 0;
-}
-
-int ProximitySensor::enable(int32_t, int en) {
- int flags = en ? 1 : 0;
- if (flags != mEnabled) {
- int fd;
- strcpy(&input_sysfs_path[input_sysfs_path_len], "enable");
- fd = open(input_sysfs_path, O_RDWR);
- if (fd >= 0) {
- char buf[2];
- buf[1] = 0;
- if (flags) {
- buf[0] = '1';
- } else {
- buf[0] = '0';
- }
- write(fd, buf, sizeof(buf));
- close(fd);
- mEnabled = flags;
- setInitialState();
- return 0;
- }
- return -1;
- }
- return 0;
-}
-
-bool ProximitySensor::hasPendingEvents() const {
- return mHasPendingEvent;
-}
-
-int ProximitySensor::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) {
- if (event->code == EVENT_TYPE_PROXIMITY) {
- if (event->value != -1) {
- // FIXME: not sure why we're getting -1 sometimes
- mPendingEvent.distance = indexToValue(event->value);
- }
- }
- } else if (type == EV_SYN) {
- mPendingEvent.timestamp = timevalToNano(event->time);
- if (mEnabled) {
- *data++ = mPendingEvent;
- count--;
- numEventReceived++;
- }
- } else {
- ALOGE("ProximitySensor: unknown event (type=%d, code=%d)",
- type, event->code);
- }
- mInputReader.next();
- }
-
- return numEventReceived;
-}
-
-float ProximitySensor::indexToValue(size_t index) const
-{
- return index * PROXIMITY_THRESHOLD_GP2A;
-}
diff --git a/libsensors/ProximitySensor.h b/libsensors/ProximitySensor.h
deleted file mode 100644
index 08ea49c..0000000
--- a/libsensors/ProximitySensor.h
+++ /dev/null
@@ -1,54 +0,0 @@
-/*
- * 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_PROXIMITY_SENSOR_H
-#define ANDROID_PROXIMITY_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 ProximitySensor : public SensorBase {
- int mEnabled;
- InputEventCircularReader mInputReader;
- sensors_event_t mPendingEvent;
- bool mHasPendingEvent;
- char input_sysfs_path[PATH_MAX];
- int input_sysfs_path_len;
-
- int setInitialState();
- float indexToValue(size_t index) const;
-
-public:
- ProximitySensor();
- virtual ~ProximitySensor();
- virtual int readEvents(sensors_event_t* data, int count);
- virtual bool hasPendingEvents() const;
- virtual int enable(int32_t handle, int enabled);
-};
-
-/*****************************************************************************/
-
-#endif // ANDROID_PROXIMITY_SENSOR_H
diff --git a/libsensors/SensorBase.cpp b/libsensors/SensorBase.cpp
deleted file mode 100644
index 79b1ee2..0000000
--- a/libsensors/SensorBase.cpp
+++ /dev/null
@@ -1,128 +0,0 @@
-/*
- * 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/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::getFd() const {
- if (!data_name) {
- return dev_fd;
- }
- return data_fd;
-}
-
-int SensorBase::setDelay(int32_t handle, int64_t ns) {
- 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;
-}
diff --git a/libsensors/SensorBase.h b/libsensors/SensorBase.h
deleted file mode 100644
index bb4d055..0000000
--- a/libsensors/SensorBase.h
+++ /dev/null
@@ -1,65 +0,0 @@
-/*
- * 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();
-
-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 int enable(int32_t handle, int enabled) = 0;
-};
-
-/*****************************************************************************/
-
-#endif // ANDROID_SENSOR_BASE_H
diff --git a/libsensors/ak8973b.h b/libsensors/ak8973b.h
deleted file mode 100644
index 9b7ab60..0000000
--- a/libsensors/ak8973b.h
+++ /dev/null
@@ -1,51 +0,0 @@
-/*
- * Definitions for akm8973 compass chip.
- */
-#ifndef AKM8973_H
-#define AKM8973_H
-
-#include <linux/ioctl.h>
-
-#define AKM8973_I2C_NAME "ak8973b"
-
-#define AKMIO 0xA1
-
-/* IOCTLs for AKM library */
-#define ECS_IOCTL_WRITE _IOW(AKMIO, 0x01, char*)
-#define ECS_IOCTL_READ _IOWR(AKMIO, 0x02, char*)
-#define ECS_IOCTL_RESET _IO(AKMIO, 0x03)
-#define ECS_IOCTL_SET_MODE _IOW(AKMIO, 0x04, short)
-#define ECS_IOCTL_GETDATA _IOR(AKMIO, 0x05, char[SENSOR_DATA_SIZE])
-#define ECS_IOCTL_SET_YPR _IOW(AKMIO, 0x06, short[12])
-#define ECS_IOCTL_GET_OPEN_STATUS _IOR(AKMIO, 0x07, int)
-#define ECS_IOCTL_GET_CLOSE_STATUS _IOR(AKMIO, 0x08, int)
-#define ECS_IOCTL_GET_DELAY _IOR(AKMIO, 0x30, int64_t)
-#define ECS_IOCTL_GET_PROJECT_NAME _IOR(AKMIO, 0x0D, char[64])
-#define ECS_IOCTL_GET_MATRIX _IOR(AKMIO, 0x0E, short [4][3][3])
-
-/* IOCTLs for APPs */
-#define ECS_IOCTL_APP_SET_MODE _IOW(AKMIO, 0x10, short)
-#define ECS_IOCTL_APP_SET_MFLAG _IOW(AKMIO, 0x11, short)
-#define ECS_IOCTL_APP_GET_MFLAG _IOW(AKMIO, 0x12, short)
-#define ECS_IOCTL_APP_SET_AFLAG _IOW(AKMIO, 0x13, short)
-#define ECS_IOCTL_APP_GET_AFLAG _IOR(AKMIO, 0x14, short)
-#define ECS_IOCTL_APP_SET_TFLAG _IOR(AKMIO, 0x15, short)
-#define ECS_IOCTL_APP_GET_TFLAG _IOR(AKMIO, 0x16, short)
-#define ECS_IOCTL_APP_RESET_PEDOMETER _IO(AKMIO, 0x17)
-#define ECS_IOCTL_APP_SET_DELAY _IOW(AKMIO, 0x18, int64_t)
-#define ECS_IOCTL_APP_GET_DELAY ECS_IOCTL_GET_DELAY
-
-/* Set raw magnetic vector flag */
-#define ECS_IOCTL_APP_SET_MVFLAG _IOW(AKMIO, 0x19, short)
-
-/* Get raw magnetic vector flag */
-#define ECS_IOCTL_APP_GET_MVFLAG _IOR(AKMIO, 0x1A, short)
-
-struct akm8973_platform_data {
- short layouts[4][3][3];
- char project_name[64];
- int gpio_RST;
- int gpio_INT;
-};
-
-#endif
diff --git a/libsensors/sensors.cpp b/libsensors/sensors.cpp
deleted file mode 100644
index 850cbf9..0000000
--- a/libsensors/sensors.cpp
+++ /dev/null
@@ -1,329 +0,0 @@
-/*
- * 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 <linux/input.h>
-#include <linux/akm8973.h>
-
-#include <utils/Atomic.h>
-#include <utils/Log.h>
-
-#include "sensors.h"
-
-#include "LightSensor.h"
-#include "ProximitySensor.h"
-#include "AkmSensor.h"
-#include "GyroSensor.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_LIGHT (1<<ID_L)
-#define SENSORS_PROXIMITY (1<<ID_P)
-#define SENSORS_GYROSCOPE (1<<ID_GY)
-
-#define SENSORS_ACCELERATION_HANDLE 0
-#define SENSORS_MAGNETIC_FIELD_HANDLE 1
-#define SENSORS_ORIENTATION_HANDLE 2
-#define SENSORS_LIGHT_HANDLE 3
-#define SENSORS_PROXIMITY_HANDLE 4
-#define SENSORS_GYROSCOPE_HANDLE 5
-
-#define AKM_FTRACE 0
-#define AKM_DEBUG 0
-#define AKM_DATA 0
-
-/*****************************************************************************/
-
-/* The SENSORS Module */
-static const struct sensor_t sSensorList[] = {
- { "KR3DM 3-axis Accelerometer",
- "STMicroelectronics",
- 1, SENSORS_ACCELERATION_HANDLE,
- SENSOR_TYPE_ACCELEROMETER, RANGE_A, CONVERT_A, 0.23f, 20000, { } },
- { "AK8973 3-axis Magnetic field sensor",
- "Asahi Kasei Microdevices",
- 1, SENSORS_MAGNETIC_FIELD_HANDLE,
- SENSOR_TYPE_MAGNETIC_FIELD, 2000.0f, CONVERT_M, 6.8f, 16667, { } },
- { "AK8973 Orientation sensor",
- "Asahi Kasei Microdevices",
- 1, SENSORS_ORIENTATION_HANDLE,
- SENSOR_TYPE_ORIENTATION, 360.0f, CONVERT_O, 7.8f, 16667, { } },
- { "GP2A Light sensor",
- "Sharp",
- 1, SENSORS_LIGHT_HANDLE,
- SENSOR_TYPE_LIGHT, powf(10, (280.0f / 47.0f)) * 4, 1.0f, 0.75f, 0, { } },
- { "GP2A Proximity sensor",
- "Sharp",
- 1, SENSORS_PROXIMITY_HANDLE,
- SENSOR_TYPE_PROXIMITY, 5.0f, 5.0f, 0.75f, 0, { } },
- { "K3G Gyroscope sensor",
- "STMicroelectronics",
- 1, SENSORS_GYROSCOPE_HANDLE,
- SENSOR_TYPE_GYROSCOPE, RANGE_GYRO, CONVERT_GYRO, 6.1f, 1190, { } },
-};
-
-
-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: "Samsung Sensor module",
- author: "Samsung Electronic Company",
- methods: &sensors_module_methods,
- },
- 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 pollEvents(sensors_event_t* data, int count);
-
-private:
- enum {
- light = 0,
- proximity = 1,
- akm = 2,
- gyro = 3,
- numSensorDrivers,
- numFds,
- };
-
- static const size_t wake = numFds - 1;
- static const char WAKE_MESSAGE = 'W';
- struct pollfd mPollFds[numFds];
- int mWritePipeFd;
- SensorBase* mSensors[numSensorDrivers];
-
- int handleToDriver(int handle) const {
- switch (handle) {
- case ID_A:
- case ID_M:
- case ID_O:
- return akm;
- case ID_P:
- return proximity;
- case ID_L:
- return light;
- case ID_GY:
- return gyro;
- }
- return -EINVAL;
- }
-};
-
-/*****************************************************************************/
-
-sensors_poll_context_t::sensors_poll_context_t()
-{
- mSensors[light] = new LightSensor();
- mPollFds[light].fd = mSensors[light]->getFd();
- mPollFds[light].events = POLLIN;
- mPollFds[light].revents = 0;
-
- mSensors[proximity] = new ProximitySensor();
- mPollFds[proximity].fd = mSensors[proximity]->getFd();
- mPollFds[proximity].events = POLLIN;
- mPollFds[proximity].revents = 0;
-
- mSensors[akm] = new AkmSensor();
- mPollFds[akm].fd = mSensors[akm]->getFd();
- mPollFds[akm].events = POLLIN;
- mPollFds[akm].revents = 0;
-
- mSensors[gyro] = new GyroSensor();
- mPollFds[gyro].fd = mSensors[gyro]->getFd();
- mPollFds[gyro].events = POLLIN;
- mPollFds[gyro].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::activate(int handle, int enabled) {
- int index = handleToDriver(handle);
- if (index < 0) return index;
- int err = mSensors[index]->enable(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) {
-
- int index = handleToDriver(handle);
- if (index < 0) return index;
- return mSensors[index]->setDelay(handle, ns);
-}
-
-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;
- }
- 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
- do {
- n = poll(mPollFds, numFds, nbEvents ? 0 : -1);
- } while (n < 0 && errno == EINTR);
- 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;
-}
-
diff --git a/libsensors/sensors.h b/libsensors/sensors.h
deleted file mode 100644
index 4b12606..0000000
--- a/libsensors/sensors.h
+++ /dev/null
@@ -1,115 +0,0 @@
-/*
- * 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)
-#define ID_L (3)
-#define ID_P (4)
-#define ID_GY (5)
-
-/*****************************************************************************/
-
-/*
- * The SENSORS Module
- */
-
-/* the GP2A is a binary proximity sensor that triggers around 5 cm on
- * this hardware */
-#define PROXIMITY_THRESHOLD_GP2A 5.0f
-
-/*****************************************************************************/
-
-#define AKM_DEVICE_NAME "/dev/akm8973_aot"
-
-#define EVENT_TYPE_ACCEL_X REL_Y
-#define EVENT_TYPE_ACCEL_Y REL_X
-#define EVENT_TYPE_ACCEL_Z REL_Z
-
-#define EVENT_TYPE_YAW REL_RX
-#define EVENT_TYPE_PITCH REL_RY
-#define EVENT_TYPE_ROLL REL_RZ
-#define EVENT_TYPE_ORIENT_STATUS REL_WHEEL
-
-/* For AK8973iB */
-#define EVENT_TYPE_MAGV_X REL_DIAL
-#define EVENT_TYPE_MAGV_Y REL_HWHEEL
-#define EVENT_TYPE_MAGV_Z REL_MISC
-
-#define EVENT_TYPE_PROXIMITY ABS_DISTANCE
-#define EVENT_TYPE_LIGHT ABS_MISC
-
-#define EVENT_TYPE_GYRO_X REL_RY
-#define EVENT_TYPE_GYRO_Y REL_RX
-#define EVENT_TYPE_GYRO_Z REL_RZ
-
-
-// 64 LSB = 1G for KR3DM
-#define LSB (64.0f)
-#define NUMOFACCDATA (8.0f)
-
-// conversion of acceleration data to SI units (m/s^2)
-#define RANGE_A (2*GRAVITY_EARTH)
-#define CONVERT_A (GRAVITY_EARTH / LSB / NUMOFACCDATA)
-#define CONVERT_A_X (CONVERT_A)
-#define CONVERT_A_Y (CONVERT_A)
-#define CONVERT_A_Z (CONVERT_A)
-
-// conversion of magnetic data to uT units
-#define CONVERT_M (1.0f/16.0f)
-#define CONVERT_M_X (-CONVERT_M)
-#define CONVERT_M_Y (-CONVERT_M)
-#define CONVERT_M_Z (-CONVERT_M)
-
-/* conversion of orientation data to degree units */
-#define CONVERT_O (1.0f/64.0f)
-#define CONVERT_O_A (CONVERT_O)
-#define CONVERT_O_P (CONVERT_O)
-#define CONVERT_O_R (-CONVERT_O)
-
-// conversion of gyro data to SI units (radian/sec)
-#define RANGE_GYRO (2000.0f*(float)M_PI/180.0f)
-#define CONVERT_GYRO ((70.0f / 1000.0f) * ((float)M_PI / 180.0f))
-#define CONVERT_GYRO_X (CONVERT_GYRO)
-#define CONVERT_GYRO_Y (-CONVERT_GYRO)
-#define CONVERT_GYRO_Z (CONVERT_GYRO)
-
-#define SENSOR_STATE_MASK (0x7FFF)
-
-/*****************************************************************************/
-
-__END_DECLS
-
-#endif // ANDROID_SENSORS_H
diff --git a/sensors/Android.mk b/sensors/Android.mk
new file mode 100644
index 0000000..d3a5656
--- /dev/null
+++ b/sensors/Android.mk
@@ -0,0 +1,40 @@
+# Copyright (C) 2013 Paul Kocialkowski <contact@paulk.fr>
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+LOCAL_PATH := $(call my-dir)
+
+include $(CLEAR_VARS)
+
+LOCAL_SRC_FILES := \
+ herring_sensors.c \
+ input.c \
+ orientation.c \
+ akm8973.c \
+ kr3dm.c \
+ gp2a_light.c \
+ gp2a_proximity.c \
+ k3g.c
+
+LOCAL_C_INCLUDES := \
+ $(LOCAL_PATH)
+
+LOCAL_SHARED_LIBRARIES := libutils libcutils liblog libhardware
+LOCAL_PRELINK_MODULE := false
+
+LOCAL_MODULE := sensors.herring
+LOCAL_MODULE_PATH := $(TARGET_OUT_SHARED_LIBRARIES)/hw
+LOCAL_MODULE_TAGS := optional
+
+include $(BUILD_SHARED_LIBRARY)
diff --git a/sensors/ak8973-reg.h b/sensors/ak8973-reg.h
new file mode 100644
index 0000000..1582076
--- /dev/null
+++ b/sensors/ak8973-reg.h
@@ -0,0 +1,47 @@
+/* linux/drivers/misc/ak8973-reg.h
+ *
+ * Copyright (c) 2010 Samsung Electronics Co., Ltd.
+ * http://www.samsung.com/
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+*/
+#ifndef __AK8973_REG__
+#define __AK8983_REG__
+
+/* Compass device dependent definition */
+#define AK8973_MODE_MEASURE 0x00 /* Starts measurement. */
+#define AK8973_MODE_E2P_READ 0x02 /* E2P access mode (read). */
+#define AK8973_MODE_POWERDOWN 0x03 /* Power down mode */
+
+/* Rx buffer size. i.e ST,TMPS,H1X,H1Y,H1Z*/
+#define SENSOR_DATA_SIZE 5
+
+/* Read/Write buffer size.*/
+#define RWBUF_SIZE 16
+
+/* AK8973 register address */
+#define AK8973_REG_ST 0xC0
+#define AK8973_REG_TMPS 0xC1
+#define AK8973_REG_H1X 0xC2
+#define AK8973_REG_H1Y 0xC3
+#define AK8973_REG_H1Z 0xC4
+
+#define AK8973_REG_MS1 0xE0
+#define AK8973_REG_HXDA 0xE1
+#define AK8973_REG_HYDA 0xE2
+#define AK8973_REG_HZDA 0xE3
+#define AK8973_REG_HXGA 0xE4
+#define AK8973_REG_HYGA 0xE5
+#define AK8973_REG_HZGA 0xE6
+
+#define AK8973_EEP_ETS 0x62
+#define AK8973_EEP_EVIR 0x63
+#define AK8973_EEP_EIHE 0x64
+#define AK8973_EEP_ETST 0x65
+#define AK8973_EEP_EHXGA 0x66
+#define AK8973_EEP_EHYGA 0x67
+#define AK8973_EEP_EHZGA 0x68
+
+#endif /* __AK8983_REG__ */
diff --git a/sensors/ak8973.h b/sensors/ak8973.h
new file mode 100644
index 0000000..3f6a28b
--- /dev/null
+++ b/sensors/ak8973.h
@@ -0,0 +1,72 @@
+/*
+ * Copyright (C) 2010 Samsung Electronics. All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * version 2 as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA
+ */
+#ifndef AKM8973_H
+#define AKM8973_H
+
+#include <linux/ioctl.h>
+
+#define AKM8973_I2C_NAME "ak8973"
+
+#define AKMIO 0xA1
+
+/* IOCTLs for AKM library */
+/* WRITE and READ sizes don't include data. On WRITE, the first value is data
+ * size plus one and the second value is the register address. On READ
+ * the first value is the data size and second value is the register
+ * address and the data is written back into the buffer starting at
+ * the second byte (the length is unchanged).
+ */
+#define ECS_IOCTL_WRITE _IOW(AKMIO, 0x01, char[2])
+#define ECS_IOCTL_READ _IOWR(AKMIO, 0x02, char[2])
+#define ECS_IOCTL_RESET _IO(AKMIO, 0x03)
+#define ECS_IOCTL_SET_MODE _IOW(AKMIO, 0x04, short)
+#define ECS_IOCTL_GETDATA _IOR(AKMIO, 0x05, char[5])
+#define ECS_IOCTL_SET_YPR _IOW(AKMIO, 0x06, short[12])
+#define ECS_IOCTL_GET_OPEN_STATUS _IOR(AKMIO, 0x07, int)
+#define ECS_IOCTL_GET_CLOSE_STATUS _IOR(AKMIO, 0x08, int)
+#define ECS_IOCTL_GET_DELAY _IOR(AKMIO, 0x30, int64_t)
+#define ECS_IOCTL_GET_PROJECT_NAME _IOR(AKMIO, 0x0D, char[64])
+#define ECS_IOCTL_GET_MATRIX _IOR(AKMIO, 0x0E, short[4][3][3])
+
+/* IOCTLs for APPs */
+#define ECS_IOCTL_APP_SET_MODE _IOW(AKMIO, 0x10, short)
+#define ECS_IOCTL_APP_SET_MFLAG _IOW(AKMIO, 0x11, short)
+#define ECS_IOCTL_APP_GET_MFLAG _IOR(AKMIO, 0x12, short)
+#define ECS_IOCTL_APP_SET_AFLAG _IOW(AKMIO, 0x13, short)
+#define ECS_IOCTL_APP_GET_AFLAG _IOR(AKMIO, 0x14, short)
+#define ECS_IOCTL_APP_SET_TFLAG _IOW(AKMIO, 0x15, short)
+#define ECS_IOCTL_APP_GET_TFLAG _IOR(AKMIO, 0x16, short)
+#define ECS_IOCTL_APP_RESET_PEDOMETER _IO(AKMIO, 0x17)
+#define ECS_IOCTL_APP_SET_DELAY _IOW(AKMIO, 0x18, int64_t)
+#define ECS_IOCTL_APP_GET_DELAY ECS_IOCTL_GET_DELAY
+
+/* Set raw magnetic vector flag */
+#define ECS_IOCTL_APP_SET_MVFLAG _IOW(AKMIO, 0x19, short)
+
+/* Get raw magnetic vector flag */
+#define ECS_IOCTL_APP_GET_MVFLAG _IOR(AKMIO, 0x1A, short)
+
+#ifdef __KERNEL__
+struct akm8973_platform_data {
+ int reset_line;
+ int reset_asserted;
+ int gpio_data_ready_int;
+};
+#endif
+
+#endif
diff --git a/sensors/akm8973.c b/sensors/akm8973.c
new file mode 100644
index 0000000..509c365
--- /dev/null
+++ b/sensors/akm8973.c
@@ -0,0 +1,960 @@
+/*
+ * Copyright (C) 2013 Paul Kocialkowski <contact@paulk.fr>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <stdlib.h>
+#include <unistd.h>
+#include <stdint.h>
+#include <fcntl.h>
+#include <errno.h>
+
+#include <hardware/sensors.h>
+#include <hardware/hardware.h>
+
+#define LOG_TAG "herring_sensors"
+#include <utils/Log.h>
+
+#include "herring_sensors.h"
+#include "ak8973-reg.h"
+#include "ak8973.h"
+
+#define AKM8973_CONFIG_PATH "/data/misc/akmd_set.txt"
+
+struct akm8973_data {
+ struct herring_sensors_handlers *orientation_sensor;
+
+ sensors_vec_t magnetic;
+ unsigned char magnetic_data[4][3];
+ int magnetic_data_count;
+ int magnetic_data_index;
+
+ unsigned char magnetic_extrema[2][3];
+ unsigned char gain_indexes[3];
+ unsigned char hdac[3];
+ int ho[3];
+
+ long int delay;
+ int device_fd;
+ int uinput_fd;
+
+ pthread_t thread;
+ pthread_mutex_t mutex;
+ int thread_continue;
+};
+
+// This AKM8973 implementation is based on intuitive understanding of how the
+// AKM8973 data is translated to SI units.
+//
+// Different parameters are used to properly configure and interpret the data:
+// * Hardware gain, which is stored in the chip's EEPROM and is a factory
+// setting that shouldn't be changed. We don't have to deal with this value at
+// all for our computation.
+// * Software gain, that is a factory setting we use to determine the
+// coefficient to use at some point in the final equation.
+// * Hardware DAC offset (HDAC) that we can tune in order to be able to see
+// variations of the raw values. This value is not a linear representation
+// of the offset! See page 22 of the AKM8973 datasheet for further details.
+// * Software offset (HO) that we can tune in order to have the maximum final
+// value at ~45uT and the minimal final value at ~-45uT for each axis.
+//
+// The final equation used to determine usable values from the raw input data
+// was found to be:
+// out = ((in - 128 - HO / 16) * k / 16
+//
+// * out: Final value in uT units
+// * in: Raw value as received from the sensor
+// * HO: HO software offset
+// * k: Coefficient that is determined from the software gain
+//
+// When no software calibration is used, we can translate HDAC to HO using the
+// following formulas:
+// if HDAC < 128: HO = HDAC * -1 * 16 * 16
+// if HDAC >= 128: HO = (HDAC - 128) * 16 * 16
+
+// Constant gain-specific coefficients used in the final formula
+float akm8973_gain_coefficient[] = {
+ 16.33f,
+ 16.28f,
+ 16.24f,
+ 16.18f,
+ 16.14f,
+ 16.10f,
+ 16.04f,
+ 16.00f,
+ 15.96f,
+ 15.90f,
+ 15.86f,
+ 15.82f,
+ 15.76f,
+ 15.72f,
+ 15.69f,
+ 15.65f,
+};
+
+int akm8973_gain_coefficient_count = sizeof(akm8973_gain_coefficient) /
+ sizeof(float);
+
+int akm8973_hdac(struct akm8973_data *data)
+{
+ char i2c_data[RWBUF_SIZE] = { 0 };
+ int device_fd;
+ int rc;
+
+ if (data == NULL)
+ return -EINVAL;
+
+ device_fd = data->device_fd;
+ if (device_fd < 0)
+ return -1;
+
+ i2c_data[0] = 4;
+ i2c_data[1] = AK8973_REG_HXDA;
+ i2c_data[2] = data->hdac[0];
+ i2c_data[3] = data->hdac[1];
+ i2c_data[4] = data->hdac[2];
+
+ rc = ioctl(device_fd, ECS_IOCTL_WRITE, &i2c_data);
+ if (rc < 0) {
+ ALOGE("%s: Unable to write akm8973 data", __func__);
+ return -1;
+ }
+
+ return 0;
+}
+
+int akm8973_magnetic_extrema(struct akm8973_data *data, int index)
+{
+ int gain_index;
+
+ if (data == NULL || index < 0 || index >= 3)
+ return -EINVAL;
+
+ gain_index = data->gain_indexes[index];
+ if (gain_index < 0 || gain_index >= akm8973_gain_coefficient_count)
+ return -1;
+
+ // Calculate the extrema from HO (software offset)
+ data->magnetic_extrema[0][index] = (unsigned char) ((int) ((16.0f * -45.0f) / akm8973_gain_coefficient[gain_index] + 128 + data->ho[index] / 16.0f + 10.0f) & 0xff);
+ data->magnetic_extrema[1][index] = (unsigned char) ((int) ((16.0f * 45.0f) / akm8973_gain_coefficient[gain_index] + 128 + data->ho[index] / 16.0f - 10.0f) & 0xff);
+
+ return 0;
+}
+
+int akm8973_config_read(struct akm8973_data *data)
+{
+ char buffer[256] = { 0 };
+ int config_fd = -1;
+ int offset = 0;
+ int length;
+ int count;
+ int value;
+ char *p;
+ int rc;
+
+ if (data == NULL)
+ return -EINVAL;
+
+ config_fd = open(AKM8973_CONFIG_PATH, O_RDONLY);
+ if (config_fd < 0) {
+ ALOGE("%s: Unable to open akm8973 config %d %s", __func__, errno, strerror(errno));
+ goto error;
+ }
+
+ rc = 0;
+
+ do {
+ lseek(config_fd, offset, SEEK_SET);
+
+ length = read(config_fd, buffer, sizeof(buffer));
+ if (length <= 0)
+ break;
+
+ p = strchr((const char *) &buffer, '\n');
+ if (p != NULL) {
+ offset += (int) p - (int) buffer + 1;
+ *p = '\0';
+ } else if ((size_t) length < sizeof(buffer)) {
+ buffer[length] = '\0';
+ }
+
+ count = sscanf((char const *) &buffer, "HSUC_HDAC_FORM0.x = %d", &value);
+ if (count == 1) {
+ data->hdac[0] = (unsigned char) (value & 0xff);
+ rc |= 1;
+ }
+
+ count = sscanf((char const *) &buffer, "HSUC_HDAC_FORM0.y = %d", &value);
+ if (count == 1) {
+ data->hdac[1] = (unsigned char) (value & 0xff);
+ rc |= 1;
+ }
+
+ count = sscanf((char const *) &buffer, "HSUC_HDAC_FORM0.z = %d", &value);
+ if (count == 1) {
+ data->hdac[2] = (unsigned char) (value & 0xff);
+ rc |= 1;
+ }
+
+ count = sscanf((char const *) &buffer, "HSUC_HO_FORM0.x = %d", &value);
+ if (count == 1) {
+ data->ho[0] = value;
+ rc |= akm8973_magnetic_extrema(data, 0);
+ }
+
+ count = sscanf((char const *) &buffer, "HSUC_HO_FORM0.y = %d", &value);
+ if (count == 1) {
+ data->ho[1] = value;
+ rc |= akm8973_magnetic_extrema(data, 1);
+ }
+
+ count = sscanf((char const *) &buffer, "HSUC_HO_FORM0.z = %d", &value);
+ if (count == 1) {
+ data->ho[2] = value;
+ rc |= akm8973_magnetic_extrema(data, 2);
+ }
+ } while (p != NULL && length > 0);
+
+ goto complete;
+
+error:
+ rc = -1;
+
+complete:
+ if (config_fd >= 0)
+ close(config_fd);
+
+ return rc;
+}
+
+int akm8973_config_write(struct akm8973_data *data)
+{
+ char buffer[256] = { 0 };
+ int config_fd = -1;
+ int length;
+ int value;
+ int rc;
+
+ if (data == NULL)
+ return -EINVAL;
+
+ config_fd = open(AKM8973_CONFIG_PATH, O_WRONLY | O_TRUNC | O_CREAT, 0664);
+ if (config_fd < 0) {
+ ALOGE("%s: Unable to open akm8973 config", __func__);
+ goto error;
+ }
+
+ value = (int) data->hdac[0];
+ length = snprintf((char *) &buffer, sizeof(buffer), "HSUC_HDAC_FORM0.x = %d\n", value);
+
+ rc = write(config_fd, buffer, length);
+ if (rc < length) {
+ ALOGE("%s: Unable to write akm8973 config", __func__);
+ goto error;
+ }
+
+ value = (int) data->hdac[1];
+ length = snprintf((char *) &buffer, sizeof(buffer), "HSUC_HDAC_FORM0.y = %d\n", value);
+
+ rc = write(config_fd, buffer, length);
+ if (rc < length) {
+ ALOGE("%s: Unable to write akm8973 config", __func__);
+ goto error;
+ }
+
+ value = (int) data->hdac[2];
+ length = snprintf((char *) &buffer, sizeof(buffer), "HSUC_HDAC_FORM0.z = %d\n", value);
+
+ rc = write(config_fd, buffer, length);
+ if (rc < length) {
+ ALOGE("%s: Unable to write akm8973 config", __func__);
+ goto error;
+ }
+
+ value = (int) data->ho[0];
+ length = snprintf((char *) &buffer, sizeof(buffer), "HSUC_HO_FORM0.x = %d\n", value);
+
+ rc = write(config_fd, buffer, length);
+ if (rc < length) {
+ ALOGE("%s: Unable to write akm8973 config", __func__);
+ goto error;
+ }
+
+ value = (int) data->ho[1];
+ length = snprintf((char *) &buffer, sizeof(buffer), "HSUC_HO_FORM0.y = %d\n", value);
+
+ rc = write(config_fd, buffer, length);
+ if (rc < length) {
+ ALOGE("%s: Unable to write akm8973 config", __func__);
+ goto error;
+ }
+
+ value = (int) data->ho[2];
+ length = snprintf((char *) &buffer, sizeof(buffer), "HSUC_HO_FORM0.z = %d\n", value);
+
+ rc = write(config_fd, buffer, length);
+ if (rc < length) {
+ ALOGE("%s: Unable to write akm8973 config", __func__);
+ goto error;
+ }
+
+ rc = 0;
+ goto complete;
+
+error:
+ rc = -1;
+
+complete:
+ if (config_fd >= 0)
+ close(config_fd);
+
+ return rc;
+}
+
+int akm8973_hdac_calibration(struct akm8973_data *data,
+ unsigned char *magnetic_data, size_t magnetic_data_size)
+{
+ unsigned char value;
+ int update;
+ int rc;
+ int i;
+
+ if (data == NULL || magnetic_data == NULL || magnetic_data_size < 3)
+ return -EINVAL;
+
+ update = 0;
+
+ for (i = 0; i < 3; i++) {
+ // Transform non-linear HDAC to a linear value
+ if (data->hdac[i] == 0)
+ value = 0x80;
+ else if (data->hdac[i] < 0x80)
+ value = 0x80 - data->hdac[i];
+ else
+ value = data->hdac[i];
+
+ // Adjust the (linear) HDAC offset if the value is out of range.
+ // The correct range is [50;205] (in raw magnetic data).
+
+ if (magnetic_data[i] < 50 ) {
+ if (value > (0xff - 4))
+ continue;
+
+ if (magnetic_data[i] < 10)
+ value += 4;
+ else if (magnetic_data[i] < 20)
+ value += 3;
+ else if (magnetic_data[i] < 30)
+ value += 2;
+ else
+ value += 1;
+
+ update = 1;
+ }
+
+ if (magnetic_data[i] > 205) {
+ if (value < (0x00 + 4))
+ continue;
+
+ if (magnetic_data[i] > 245)
+ value -= 4;
+ else if (magnetic_data[i] > 235)
+ value -= 3;
+ else if (magnetic_data[i] > 225)
+ value -= 2;
+ else
+ value -= 1;
+
+ update = 1;
+ }
+
+ if (update) {
+ // When the HDAC (hardware offset) value is changed, the HO value and magnetic extrema become irrelevant.
+ // We can calculate HO (software offset) from HDAC but it'll need to be finely tuned later on.
+
+ data->magnetic_extrema[0][i] = 0;
+ data->magnetic_extrema[1][i] = 0;
+
+ // Transform linear value to non-linear HDAC
+ if (value == 0x80) {
+ data->hdac[i] = 0;
+ data->ho[i] = 0;
+ } else if (value < 0x80) {
+ data->hdac[i] = 0x80 - value;
+ data->ho[i] = data->hdac[i] * -1 * 16 * 16;
+ } else {
+ data->hdac[i] = value;
+ data->ho[i] = (data->hdac[i] - 128) * 16 * 16;
+ }
+ }
+ }
+
+ if (update) {
+ rc = akm8973_hdac(data);
+ if (rc < 0) {
+ ALOGE("%s: Unable to set akm8973 HDAC", __func__);
+ return -1;
+ }
+ }
+
+ return 0;
+}
+
+int akm8973_ho_calibration(struct akm8973_data *data,
+ unsigned char *magnetic_data, size_t magnetic_data_size)
+{
+ float ho[2];
+ int gain_index;
+ int i;
+
+ if (data == NULL || magnetic_data == NULL || magnetic_data_size < 3)
+ return -EINVAL;
+
+ // Update the extrema from the current raw magnetic data
+ for (i = 0; i < 3; i++) {
+ if (magnetic_data[i] < data->magnetic_extrema[0][i] || data->magnetic_extrema[0][i] == 0)
+ data->magnetic_extrema[0][i] = magnetic_data[i];
+ if (magnetic_data[i] > data->magnetic_extrema[1][i] || data->magnetic_extrema[1][i] == 0)
+ data->magnetic_extrema[1][i] = magnetic_data[i];
+ }
+
+ // Calculate HO (software offset)
+ if (data->magnetic_data_count % 10 == 0) {
+ for (i = 0; i < 3; i++) {
+ gain_index = data->gain_indexes[i];
+ if (gain_index < 0 || gain_index >= akm8973_gain_coefficient_count)
+ continue;
+
+ // Calculate offset for minimum to be at -45uT
+ ho[0] = ((float) (data->magnetic_extrema[0][i] - 0x80) + (16.0f * 45.0f) / akm8973_gain_coefficient[gain_index] ) * 16.0f;
+ // Calculate offset for maximum to be at +45uT
+ ho[1] = ((float) (data->magnetic_extrema[1][i] - 0x80) - (16.0f * 45.0f) / akm8973_gain_coefficient[gain_index] ) * 16.0f;
+ // Average offset to make everyone (mostly) happy
+ data->ho[i] = (int) (ho[0] + ho[1]) / 2.0f;
+ }
+ }
+
+ return 0;
+}
+
+int akm8973_magnetic_axis(struct akm8973_data *data, int index, float *axis)
+{
+ float value;
+ int count;
+ int gain_index;
+ int i;
+
+ if (data == NULL || axis == NULL || index < 0 || index >= 3)
+ return -EINVAL;
+
+ count = data->magnetic_data_count >= 4 ? 4 : data->magnetic_data_count;
+ value = 0;
+
+ // Average the last 4 (or less) raw magnetic values
+ for (i = 0; i < count; i++)
+ value += (float) data->magnetic_data[i][index];
+ value /= count;
+
+ gain_index = data->gain_indexes[index];
+ if (gain_index < 0 || gain_index >= akm8973_gain_coefficient_count)
+ return -1;
+
+ // Formula to get the magnetic field in uT from the raw magnetic value, HO and coefficient from gain
+ *axis = ((value - 128 - ((float) data->ho[index] / 16.0f)) * akm8973_gain_coefficient[gain_index]) / 16.0f;
+
+ return 0;
+}
+
+int akm8973_magnetic(struct akm8973_data *data)
+{
+ int rc;
+
+ if (data == NULL)
+ return -EINVAL;
+
+ rc = 0;
+ rc |= akm8973_magnetic_axis(data, 0, &data->magnetic.x);
+ rc |= akm8973_magnetic_axis(data, 1, &data->magnetic.y);
+ rc |= akm8973_magnetic_axis(data, 2, &data->magnetic.z);
+
+ return rc;
+}
+
+void *akm8973_thread(void *thread_data)
+{
+ struct herring_sensors_handlers *handlers = NULL;
+ struct akm8973_data *data = NULL;
+ struct input_event event;
+ struct timeval time;
+ char i2c_data[SENSOR_DATA_SIZE] = { 0 };
+ unsigned char magnetic_data[3] = { 0 };
+ int index;
+ int value;
+ short mode;
+ long int before, after;
+ int diff;
+ int device_fd;
+ int uinput_fd;
+ int rc;
+ int i, j;
+
+ if (thread_data == NULL)
+ return NULL;
+
+ handlers = (struct herring_sensors_handlers *) thread_data;
+ if (handlers->data == NULL)
+ return NULL;
+
+ data = (struct akm8973_data *) handlers->data;
+
+ device_fd = data->device_fd;
+ if (device_fd < 0)
+ return NULL;
+
+ uinput_fd = data->uinput_fd;
+ if (uinput_fd < 0)
+ return NULL;
+
+ while (data->thread_continue) {
+ pthread_mutex_lock(&data->mutex);
+ if (!data->thread_continue)
+ break;
+
+ while (handlers->activated) {
+ gettimeofday(&time, NULL);
+ before = timestamp(&time);
+
+ mode = AK8973_MODE_MEASURE;
+ rc = ioctl(device_fd, ECS_IOCTL_SET_MODE, &mode);
+ if (rc < 0) {
+ ALOGE("%s: Unable to set akm8973 mode", __func__);
+ goto next;
+ }
+
+ memset(&i2c_data, 0, sizeof(i2c_data));
+ rc = ioctl(device_fd, ECS_IOCTL_GETDATA, &i2c_data);
+ if (rc < 0) {
+ ALOGE("%s: Unable to get akm8973 data", __func__);
+ goto next;
+ }
+
+ if (!(i2c_data[0] & 0x01)) {
+ ALOGE("%s: akm8973 data is not ready", __func__);
+ goto next;
+ }
+
+ magnetic_data[0] = (unsigned char) i2c_data[2];
+ magnetic_data[1] = (unsigned char) i2c_data[3];
+ magnetic_data[2] = (unsigned char) i2c_data[4];
+
+ rc = akm8973_hdac_calibration(data, (unsigned char *) &magnetic_data, sizeof(magnetic_data));
+ if (rc < 0) {
+ ALOGE("%s: Unable to calibrate akm8973 HDAC", __func__);
+ goto next;
+ }
+
+ index = data->magnetic_data_index;
+
+ data->magnetic_data[index][0] = magnetic_data[0];
+ data->magnetic_data[index][1] = magnetic_data[1];
+ data->magnetic_data[index][2] = magnetic_data[2];
+
+ data->magnetic_data_index = (index + 1) % 4;
+ data->magnetic_data_count++;
+
+ rc = akm8973_ho_calibration(data, (unsigned char *) &magnetic_data, sizeof(magnetic_data));
+ if (rc < 0) {
+ ALOGE("%s: Unable to calibrate akm8973 HO", __func__);
+ goto next;
+ }
+
+ rc = akm8973_magnetic(data);
+ if (rc < 0) {
+ ALOGE("%s: Unable to get akm8973 magnetic", __func__);
+ goto next;
+ }
+
+ input_event_set(&event, EV_REL, REL_X, (int) (data->magnetic.x * 1000));
+ write(uinput_fd, &event, sizeof(event));
+ input_event_set(&event, EV_REL, REL_Y, (int) (data->magnetic.y * 1000));
+ write(uinput_fd, &event, sizeof(event));
+ input_event_set(&event, EV_REL, REL_Z, (int) (data->magnetic.z * 1000));
+ write(uinput_fd, &event, sizeof(event));
+ input_event_set(&event, EV_SYN, 0, 0);
+ write(uinput_fd, &event, sizeof(event));
+
+next:
+ gettimeofday(&time, NULL);
+ after = timestamp(&time);
+
+ diff = (int) (data->delay - (after - before)) / 1000;
+ if (diff <= 0)
+ continue;
+
+ usleep(diff);
+ }
+ }
+ return NULL;
+}
+
+int akm8973_init(struct herring_sensors_handlers *handlers,
+ struct herring_sensors_device *device)
+{
+ struct akm8973_data *data = NULL;
+ pthread_attr_t thread_attr;
+ char i2c_data[RWBUF_SIZE] = { 0 };
+ char mode;
+ int device_fd = -1;
+ int uinput_fd = -1;
+ int input_fd = -1;
+ int rc;
+ int i;
+
+ ALOGD("%s(%p, %p)", __func__, handlers, device);
+
+ if (handlers == NULL || device == NULL)
+ return -EINVAL;
+
+ data = (struct akm8973_data *) calloc(1, sizeof(struct akm8973_data));
+
+ for (i = 0; i < device->handlers_count; i++) {
+ if (device->handlers[i] == NULL)
+ continue;
+
+ if (device->handlers[i]->handle == SENSOR_TYPE_ORIENTATION)
+ data->orientation_sensor = device->handlers[i];
+ }
+
+ device_fd = open("/dev/akm8973", O_RDONLY);
+ if (device_fd < 0) {
+ ALOGE("%s: Unable to open device", __func__);
+ goto error;
+ }
+
+ rc = ioctl(device_fd, ECS_IOCTL_RESET, NULL);
+ if (rc < 0) {
+ ALOGE("%s: Unable to reset akm8973", __func__);
+ goto error;
+ }
+
+ mode = AK8973_MODE_E2P_READ;
+ rc = ioctl(device_fd, ECS_IOCTL_SET_MODE, &mode);
+ if (rc < 0) {
+ ALOGE("%s: Unable to set akm8973 mode", __func__);
+ goto error;
+ }
+
+ i2c_data[0] = 3;
+ i2c_data[1] = AK8973_EEP_EHXGA;
+ rc = ioctl(device_fd, ECS_IOCTL_READ, &i2c_data);
+ if (rc < 0) {
+ ALOGE("%s: Unable to read akm8973 EEPROM data", __func__);
+ goto error;
+ }
+
+ data->gain_indexes[0] = (i2c_data[1] & 0xf0) >> 4;
+ data->gain_indexes[1] = (i2c_data[2] & 0xf0) >> 4;
+ data->gain_indexes[2] = (i2c_data[3] & 0xf0) >> 4;
+
+ ALOGD("AKM8973 gain indexes are: (%d, %d, %d)", data->gain_indexes[0],
+ data->gain_indexes[1], data->gain_indexes[2]);
+
+ mode = AK8973_MODE_POWERDOWN;
+ rc = ioctl(device_fd, ECS_IOCTL_SET_MODE, &mode);
+ if (rc < 0) {
+ ALOGE("%s: Unable to set akm8973 mode", __func__);
+ goto error;
+ }
+
+ i2c_data[5] = i2c_data[1] & 0x0f;
+ i2c_data[6] = i2c_data[2] & 0x0f;
+ i2c_data[7] = i2c_data[3] & 0x0f;
+
+ i2c_data[0] = 7;
+ i2c_data[1] = AK8973_REG_HXDA;
+
+ i2c_data[2] = 0;
+ i2c_data[3] = 0;
+ i2c_data[4] = 0;
+
+ rc = ioctl(device_fd, ECS_IOCTL_WRITE, &i2c_data);
+ if (rc < 0) {
+ ALOGE("%s: Unable to write akm8973 data", __func__);
+ goto error;
+ }
+
+ uinput_fd = uinput_rel_create("magnetic");
+ if (uinput_fd < 0) {
+ ALOGD("%s: Unable to create uinput", __func__);
+ goto error;
+ }
+
+ input_fd = input_open("magnetic");
+ if (input_fd < 0) {
+ ALOGE("%s: Unable to open magnetic input", __func__);
+ goto error;
+ }
+
+ data->thread_continue = 1;
+
+ pthread_mutex_init(&data->mutex, NULL);
+ pthread_mutex_lock(&data->mutex);
+
+ pthread_attr_init(&thread_attr);
+ pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED);
+
+ rc = pthread_create(&data->thread, &thread_attr, akm8973_thread, (void *) handlers);
+ if (rc < 0) {
+ ALOGE("%s: Unable to create akm8973 thread", __func__);
+ pthread_mutex_destroy(&data->mutex);
+ goto error;
+ }
+
+ data->device_fd = device_fd;
+ data->uinput_fd = uinput_fd;
+ handlers->poll_fd = input_fd;
+ handlers->data = (void *) data;
+
+ return 0;
+
+error:
+ if (data != NULL)
+ free(data);
+
+ if (uinput_fd >= 0)
+ close(uinput_fd);
+
+ if (input_fd >= 0)
+ close(input_fd);
+
+ if (device_fd >= 0)
+ close(device_fd);
+
+ handlers->poll_fd = -1;
+ handlers->data = NULL;
+
+ return -1;
+}
+
+int akm8973_deinit(struct herring_sensors_handlers *handlers)
+{
+ struct akm8973_data *data = NULL;
+ char mode;
+ int rc;
+
+ ALOGD("%s(%p)", __func__, handlers);
+
+ if (handlers == NULL || handlers->data == NULL)
+ return -EINVAL;
+
+ data = (struct akm8973_data *) handlers->data;
+
+ handlers->activated = 0;
+ data->thread_continue = 0;
+ pthread_mutex_unlock(&data->mutex);
+
+ pthread_mutex_destroy(&data->mutex);
+
+ if (data->uinput_fd >= 0) {
+ uinput_destroy(data->uinput_fd);
+ close(data->uinput_fd);
+ }
+ data->uinput_fd = -1;
+
+ if (handlers->poll_fd >= 0)
+ close(handlers->poll_fd);
+ handlers->poll_fd = -1;
+
+ mode = AK8973_MODE_POWERDOWN;
+ rc = ioctl(data->device_fd, ECS_IOCTL_SET_MODE, &mode);
+ if (rc < 0)
+ ALOGE("%s: Unable to set akm8973 mode", __func__);
+
+ if (data->device_fd >= 0)
+ close(data->device_fd);
+ data->device_fd = -1;
+
+ free(handlers->data);
+ handlers->data = NULL;
+
+ return 0;
+}
+
+int akm8973_activate(struct herring_sensors_handlers *handlers)
+{
+ struct akm8973_data *data;
+ int rc;
+
+ ALOGD("%s(%p)", __func__, handlers);
+
+ if (handlers == NULL || handlers->data == NULL)
+ return -EINVAL;
+
+ data = (struct akm8973_data *) handlers->data;
+
+ rc = akm8973_config_read(data);
+ if (rc < 0) {
+ ALOGE("%s: Unable to read akm8973 config", __func__);
+ } else if (rc > 0) {
+ rc = akm8973_hdac(data);
+ if (rc < 0) {
+ ALOGE("%s: Unable to set akm8973 HDAC", __func__);
+ return -1;
+ }
+ }
+
+ handlers->activated = 1;
+ pthread_mutex_unlock(&data->mutex);
+
+ return 0;
+}
+
+int akm8973_deactivate(struct herring_sensors_handlers *handlers)
+{
+ struct akm8973_data *data;
+ int device_fd;
+ char mode;
+ int empty;
+ int rc;
+ int i;
+
+ ALOGD("%s(%p)", __func__, handlers);
+
+ if (handlers == NULL || handlers->data == NULL)
+ return -EINVAL;
+
+ data = (struct akm8973_data *) handlers->data;
+
+ empty = 1;
+
+ for (i = 0; i < (ssize_t) sizeof(data->magnetic_extrema) / 2; i++) {
+ if (data->magnetic_extrema[0][i] != 0 || data->magnetic_extrema[1][i] != 0) {
+ empty = 0;
+ break;
+ }
+ }
+
+ if (!empty) {
+ rc = akm8973_config_write(data);
+ if (rc < 0)
+ ALOGE("%s: Unable to write akm8973 config", __func__);
+ }
+
+ device_fd = data->device_fd;
+ if (device_fd < 0)
+ return -1;
+
+ mode = AK8973_MODE_POWERDOWN;
+ rc = ioctl(data->device_fd, ECS_IOCTL_SET_MODE, &mode);
+ if (rc < 0)
+ ALOGE("%s: Unable to set akm8973 mode", __func__);
+
+ handlers->activated = 0;
+
+ return 0;
+}
+
+int akm8973_set_delay(struct herring_sensors_handlers *handlers, long int delay)
+{
+ struct akm8973_data *data;
+
+ ALOGD("%s(%p, %ld)", __func__, handlers, delay);
+
+ if (handlers == NULL || handlers->data == NULL)
+ return -EINVAL;
+
+ data = (struct akm8973_data *) handlers->data;
+
+ data->delay = delay;
+
+ return 0;
+}
+
+float akm8973_convert(int value)
+{
+ return (float) value / -1000.0f;
+}
+
+int akm8973_get_data(struct herring_sensors_handlers *handlers,
+ struct sensors_event_t *event)
+{
+ struct akm8973_data *data;
+ struct input_event input_event;
+ int input_fd;
+ int rc;
+
+// ALOGD("%s(%p, %p)", __func__, handlers, event);
+
+ if (handlers == NULL || handlers->data == NULL || event == NULL)
+ return -EINVAL;
+
+ data = (struct akm8973_data *) handlers->data;
+
+ input_fd = handlers->poll_fd;
+ if (input_fd < 0)
+ return -1;
+
+ memset(event, 0, sizeof(struct sensors_event_t));
+ event->version = sizeof(struct sensors_event_t);
+ event->sensor = handlers->handle;
+ event->type = handlers->handle;
+
+ event->magnetic.status = SENSOR_STATUS_ACCURACY_MEDIUM;
+
+ do {
+ rc = read(input_fd, &input_event, sizeof(input_event));
+ if (rc < (int) sizeof(input_event))
+ break;
+
+ if (input_event.type == EV_REL) {
+ switch (input_event.code) {
+ case REL_X:
+ event->magnetic.y = akm8973_convert(input_event.value);
+ break;
+ case REL_Y:
+ event->magnetic.x = akm8973_convert(input_event.value);
+ break;
+ case REL_Z:
+ event->magnetic.z = akm8973_convert(input_event.value);
+ break;
+ default:
+ continue;
+ }
+ } else if (input_event.type == EV_SYN) {
+ if (input_event.code == SYN_REPORT)
+ event->timestamp = input_timestamp(&input_event);
+ }
+ } while (input_event.type != EV_SYN);
+
+ if (data->orientation_sensor != NULL)
+ orientation_fill(data->orientation_sensor, NULL, &event->magnetic);
+
+ return 0;
+}
+
+struct herring_sensors_handlers akm8973 = {
+ .name = "AKM8973",
+ .handle = SENSOR_TYPE_MAGNETIC_FIELD,
+ .init = akm8973_init,
+ .deinit = akm8973_deinit,
+ .activate = akm8973_activate,
+ .deactivate = akm8973_deactivate,
+ .set_delay = akm8973_set_delay,
+ .get_data = akm8973_get_data,
+ .activated = 0,
+ .needed = 0,
+ .poll_fd = -1,
+ .data = NULL,
+};
diff --git a/sensors/gp2a_light.c b/sensors/gp2a_light.c
new file mode 100644
index 0000000..10388cd
--- /dev/null
+++ b/sensors/gp2a_light.c
@@ -0,0 +1,239 @@
+/*
+ * Copyright (C) 2013 Paul Kocialkowski <contact@paulk.fr>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <stdlib.h>
+#include <unistd.h>
+#include <stdint.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <math.h>
+#include <sys/types.h>
+#include <linux/ioctl.h>
+#include <linux/input.h>
+
+#include <hardware/sensors.h>
+#include <hardware/hardware.h>
+
+#define LOG_TAG "herring_sensors"
+#include <utils/Log.h>
+
+#include "herring_sensors.h"
+
+struct gp2a_light_data {
+ char path_enable[PATH_MAX];
+ char path_delay[PATH_MAX];
+};
+
+int gp2a_light_init(struct herring_sensors_handlers *handlers,
+ struct herring_sensors_device *device)
+{
+ struct gp2a_light_data *data = NULL;
+ char path[PATH_MAX] = { 0 };
+ int input_fd = -1;
+ int rc;
+
+ ALOGD("%s(%p, %p)", __func__, handlers, device);
+
+ if (handlers == NULL)
+ return -EINVAL;
+
+ data = (struct gp2a_light_data *) calloc(1, sizeof(struct gp2a_light_data));
+
+ input_fd = input_open("lightsensor-level");
+ if (input_fd < 0) {
+ ALOGE("%s: Unable to open input", __func__);
+ goto error;
+ }
+
+ rc = sysfs_path_prefix("lightsensor-level", (char *) &path);
+ if (rc < 0 || path[0] == '\0') {
+ ALOGE("%s: Unable to open sysfs", __func__);
+ goto error;
+ }
+
+ snprintf(data->path_enable, PATH_MAX, "%s/enable", path);
+ snprintf(data->path_delay, PATH_MAX, "%s/poll_delay", path);
+
+ handlers->poll_fd = input_fd;
+ handlers->data = (void *) data;
+
+ return 0;
+
+error:
+ if (data != NULL)
+ free(data);
+
+ if (input_fd >= 0)
+ close(input_fd);
+
+ handlers->poll_fd = -1;
+ handlers->data = NULL;
+
+ return -1;
+}
+
+int gp2a_light_deinit(struct herring_sensors_handlers *handlers)
+{
+ ALOGD("%s(%p)", __func__, handlers);
+
+ if (handlers == NULL)
+ return -EINVAL;
+
+ if (handlers->poll_fd >= 0)
+ close(handlers->poll_fd);
+ handlers->poll_fd = -1;
+
+ if (handlers->data != NULL)
+ free(handlers->data);
+ handlers->data = NULL;
+
+ return 0;
+}
+
+int gp2a_light_activate(struct herring_sensors_handlers *handlers)
+{
+ struct gp2a_light_data *data;
+ int rc;
+
+ ALOGD("%s(%p)", __func__, handlers);
+
+ if (handlers == NULL || handlers->data == NULL)
+ return -EINVAL;
+
+ data = (struct gp2a_light_data *) handlers->data;
+
+ rc = sysfs_value_write(data->path_enable, 1);
+ if (rc < 0) {
+ ALOGE("%s: Unable to write sysfs value", __func__);
+ return -1;
+ }
+
+ handlers->activated = 1;
+
+ return 0;
+}
+
+int gp2a_light_deactivate(struct herring_sensors_handlers *handlers)
+{
+ struct gp2a_light_data *data;
+ int rc;
+
+ ALOGD("%s(%p)", __func__, handlers);
+
+ if (handlers == NULL || handlers->data == NULL)
+ return -EINVAL;
+
+ data = (struct gp2a_light_data *) handlers->data;
+
+ rc = sysfs_value_write(data->path_enable, 0);
+ if (rc < 0) {
+ ALOGE("%s: Unable to write sysfs value", __func__);
+ return -1;
+ }
+
+ handlers->activated = 1;
+
+ return 0;
+}
+
+int gp2a_light_set_delay(struct herring_sensors_handlers *handlers, long int delay)
+{
+ struct gp2a_light_data *data;
+ int rc;
+
+ ALOGD("%s(%p, %ld)", __func__, handlers, delay);
+
+ if (handlers == NULL || handlers->data == NULL)
+ return -EINVAL;
+
+ data = (struct gp2a_light_data *) handlers->data;
+
+ rc = sysfs_value_write(data->path_delay, (int) delay);
+ if (rc < 0) {
+ ALOGE("%s: Unable to write sysfs value", __func__);
+ return -1;
+ }
+
+ return 0;
+}
+
+float gp2a_light_convert(int value)
+{
+ // Converting the raw value to lux units is done with:
+ // I = 10 * log(light) uA
+ // U = raw * 3300 / 4095 (max ADC value is 3.3V for 4095 LSB)
+ // R = 47kOhm
+ // => light = 10 ^ (I / 10) = 10 ^ (U / R / 10)
+ // => light = 10 ^ (raw * 330 / 4095 / 47)
+ // Only 1/4 of light reaches the sensor:
+ // => light = 4 * (10 ^ (raw * 330 / 4095 / 47))
+
+ return powf(10, value * (330.0f / 4095.0f / 47.0f)) * 4;
+}
+
+int gp2a_light_get_data(struct herring_sensors_handlers *handlers,
+ struct sensors_event_t *event)
+{
+ struct input_event input_event;
+ int input_fd;
+ int rc;
+
+// ALOGD("%s(%p, %p)", __func__, handlers, event);
+
+ if (handlers == NULL || event == NULL)
+ return -EINVAL;
+
+ input_fd = handlers->poll_fd;
+ if (input_fd < 0)
+ return -EINVAL;
+
+ memset(event, 0, sizeof(struct sensors_event_t));
+ event->version = sizeof(struct sensors_event_t);
+ event->sensor = handlers->handle;
+ event->type = handlers->handle;
+
+ do {
+ rc = read(input_fd, &input_event, sizeof(input_event));
+ if (rc < (int) sizeof(input_event))
+ break;
+
+ if (input_event.type == EV_ABS) {
+ if (input_event.code == ABS_MISC)
+ event->light = gp2a_light_convert(input_event.value);
+ } else if (input_event.type == EV_SYN) {
+ if (input_event.code == SYN_REPORT)
+ event->timestamp = input_timestamp(&input_event);
+ }
+ } while (input_event.type != EV_SYN);
+
+ return 0;
+}
+
+struct herring_sensors_handlers gp2a_light = {
+ .name = "GP2A Light",
+ .handle = SENSOR_TYPE_LIGHT,
+ .init = gp2a_light_init,
+ .deinit = gp2a_light_deinit,
+ .activate = gp2a_light_activate,
+ .deactivate = gp2a_light_deactivate,
+ .set_delay = gp2a_light_set_delay,
+ .get_data = gp2a_light_get_data,
+ .activated = 0,
+ .needed = 0,
+ .poll_fd = -1,
+ .data = NULL,
+};
diff --git a/sensors/gp2a_proximity.c b/sensors/gp2a_proximity.c
new file mode 100644
index 0000000..b524814
--- /dev/null
+++ b/sensors/gp2a_proximity.c
@@ -0,0 +1,213 @@
+/*
+ * Copyright (C) 2013 Paul Kocialkowski <contact@paulk.fr>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <stdlib.h>
+#include <unistd.h>
+#include <stdint.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <sys/types.h>
+#include <linux/ioctl.h>
+#include <linux/input.h>
+
+#include <hardware/sensors.h>
+#include <hardware/hardware.h>
+
+#define LOG_TAG "herring_sensors"
+#include <utils/Log.h>
+
+#include "herring_sensors.h"
+
+struct gp2a_proximity_data {
+ char path_enable[PATH_MAX];
+};
+
+int gp2a_proximity_init(struct herring_sensors_handlers *handlers,
+ struct herring_sensors_device *device)
+{
+ struct gp2a_proximity_data *data = NULL;
+ char path[PATH_MAX] = { 0 };
+ int input_fd = -1;
+ int rc;
+
+ ALOGD("%s(%p, %p)", __func__, handlers, device);
+
+ if (handlers == NULL)
+ return -EINVAL;
+
+ data = (struct gp2a_proximity_data *) calloc(1, sizeof(struct gp2a_proximity_data));
+
+ input_fd = input_open("proximity");
+ if (input_fd < 0) {
+ ALOGE("%s: Unable to open input", __func__);
+ goto error;
+ }
+
+ rc = sysfs_path_prefix("proximity", (char *) &path);
+ if (rc < 0 || path[0] == '\0') {
+ ALOGE("%s: Unable to open sysfs", __func__);
+ goto error;
+ }
+
+ snprintf(data->path_enable, PATH_MAX, "%s/enable", path);
+
+ handlers->poll_fd = input_fd;
+ handlers->data = (void *) data;
+
+ return 0;
+
+error:
+ if (data != NULL)
+ free(data);
+
+ if (input_fd >= 0)
+ close(input_fd);
+
+ handlers->poll_fd = -1;
+ handlers->data = NULL;
+
+ return -1;
+}
+
+int gp2a_proximity_deinit(struct herring_sensors_handlers *handlers)
+{
+ ALOGD("%s(%p)", __func__, handlers);
+
+ if (handlers == NULL)
+ return -EINVAL;
+
+ if (handlers->poll_fd >= 0)
+ close(handlers->poll_fd);
+ handlers->poll_fd = -1;
+
+ if (handlers->data != NULL)
+ free(handlers->data);
+ handlers->data = NULL;
+
+ return 0;
+}
+
+int gp2a_proximity_activate(struct herring_sensors_handlers *handlers)
+{
+ struct gp2a_proximity_data *data;
+ int rc;
+
+ ALOGD("%s(%p)", __func__, handlers);
+
+ if (handlers == NULL || handlers->data == NULL)
+ return -EINVAL;
+
+ data = (struct gp2a_proximity_data *) handlers->data;
+
+ rc = sysfs_value_write(data->path_enable, 1);
+ if (rc < 0) {
+ ALOGE("%s: Unable to write sysfs value", __func__);
+ return -1;
+ }
+
+ handlers->activated = 1;
+
+ return 0;
+}
+
+int gp2a_proximity_deactivate(struct herring_sensors_handlers *handlers)
+{
+ struct gp2a_proximity_data *data;
+ int rc;
+
+ ALOGD("%s(%p)", __func__, handlers);
+
+ if (handlers == NULL || handlers->data == NULL)
+ return -EINVAL;
+
+ data = (struct gp2a_proximity_data *) handlers->data;
+
+ rc = sysfs_value_write(data->path_enable, 0);
+ if (rc < 0) {
+ ALOGE("%s: Unable to write sysfs value", __func__);
+ return -1;
+ }
+
+ handlers->activated = 1;
+
+ return 0;
+}
+
+int gp2a_proximity_set_delay(struct herring_sensors_handlers *handlers, long int delay)
+{
+ ALOGD("%s(%p, %ld)", __func__, handlers, delay);
+
+ return 0;
+}
+
+float gp2a_proximity_convert(int value)
+{
+ return (float) value * 5.0f;
+}
+
+int gp2a_proximity_get_data(struct herring_sensors_handlers *handlers,
+ struct sensors_event_t *event)
+{
+ struct input_event input_event;
+ int input_fd;
+ int rc;
+
+// ALOGD("%s(%p, %p)", __func__, handlers, event);
+
+ if (handlers == NULL || event == NULL)
+ return -EINVAL;
+
+ input_fd = handlers->poll_fd;
+ if (input_fd < 0)
+ return -EINVAL;
+
+ memset(event, 0, sizeof(struct sensors_event_t));
+ event->version = sizeof(struct sensors_event_t);
+ event->sensor = handlers->handle;
+ event->type = handlers->handle;
+
+ do {
+ rc = read(input_fd, &input_event, sizeof(input_event));
+ if (rc < (int) sizeof(input_event))
+ break;
+
+ if (input_event.type == EV_ABS) {
+ if (input_event.code == ABS_DISTANCE)
+ event->distance = gp2a_proximity_convert(input_event.value);
+ } else if (input_event.type == EV_SYN) {
+ if (input_event.code == SYN_REPORT)
+ event->timestamp = input_timestamp(&input_event);
+ }
+ } while (input_event.type != EV_SYN);
+
+ return 0;
+}
+
+struct herring_sensors_handlers gp2a_proximity = {
+ .name = "GP2A Proximity",
+ .handle = SENSOR_TYPE_PROXIMITY,
+ .init = gp2a_proximity_init,
+ .deinit = gp2a_proximity_deinit,
+ .activate = gp2a_proximity_activate,
+ .deactivate = gp2a_proximity_deactivate,
+ .set_delay = gp2a_proximity_set_delay,
+ .get_data = gp2a_proximity_get_data,
+ .activated = 0,
+ .needed = 0,
+ .poll_fd = -1,
+ .data = NULL,
+};
diff --git a/sensors/herring_sensors.c b/sensors/herring_sensors.c
new file mode 100644
index 0000000..362bbe1
--- /dev/null
+++ b/sensors/herring_sensors.c
@@ -0,0 +1,289 @@
+/*
+ * Copyright (C) 2013 Paul Kocialkowski <contact@paulk.fr>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <stdlib.h>
+#include <unistd.h>
+#include <stdint.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <poll.h>
+#include <sys/select.h>
+#include <hardware/sensors.h>
+#include <hardware/hardware.h>
+
+#define LOG_TAG "herring_sensors"
+#include <utils/Log.h>
+
+#include "herring_sensors.h"
+
+/*
+ * Sensors list
+ */
+
+struct sensor_t herring_sensors[] = {
+ { "KR3DM Acceleration Sensor", "STMicroelectronics", 1, SENSOR_TYPE_ACCELEROMETER,
+ SENSOR_TYPE_ACCELEROMETER, 2 * GRAVITY_EARTH, GRAVITY_EARTH / 64.0f / 8.0f, 0.23f, 20000, {}, },
+ { "AKM8973 Magnetic Sensor", "Asahi Kasei", 1, SENSOR_TYPE_MAGNETIC_FIELD,
+ SENSOR_TYPE_MAGNETIC_FIELD, 2000.0f, 1.0f / 16, 6.8f, 16667, {}, },
+ { "Orientation Sensor", "Herring Sensors", 1, SENSOR_TYPE_ORIENTATION,
+ SENSOR_TYPE_ORIENTATION, 360.0f, 0.1f, 0.0f, 16667, {}, },
+ { "GP2A Light Sensor", "Sharp", 1, SENSOR_TYPE_LIGHT,
+ SENSOR_TYPE_LIGHT, 3626657.75f, 1.0f, 0.75f, 0, {}, }, // 3626657.75 = (10 ^ (280.0f / 47.0f)) * 4
+ { "GP2A Proximity Sensor", "Sharp", 1, SENSOR_TYPE_PROXIMITY,
+ SENSOR_TYPE_PROXIMITY, 5.0f, 5.0f, 0.75f, 0, {}, },
+ { "K3G Gyroscope Sensor", "STMicroelectronics", 1, SENSOR_TYPE_GYROSCOPE,
+ SENSOR_TYPE_GYROSCOPE, 2000.0f * (3.1415926535f / 180.0f), (70.0f / 1000.0f) * (3.1415926535f / 180.0f), 6.1f, 1190, {}, },
+};
+
+int herring_sensors_count = sizeof(herring_sensors) / sizeof(struct sensor_t);
+
+struct herring_sensors_handlers *herring_sensors_handlers[] = {
+ &kr3dm,
+ &akm8973,
+ &orientation,
+ &gp2a_light,
+ &gp2a_proximity,
+ &k3g,
+};
+
+int herring_sensors_handlers_count = sizeof(herring_sensors_handlers) /
+ sizeof(struct herring_sensors_handlers *);
+
+/*
+ * Herring Sensors
+ */
+
+int herring_sensors_activate(struct sensors_poll_device_t *dev, int handle,
+ int enabled)
+{
+ struct herring_sensors_device *device;
+ int i;
+
+ ALOGD("%s(%p, %d, %d)", __func__, dev, handle, enabled);
+
+ if (dev == NULL)
+ return -EINVAL;
+
+ device = (struct herring_sensors_device *) dev;
+
+ if (device->handlers == NULL || device->handlers_count <= 0)
+ return -EINVAL;
+
+ for (i = 0; i < device->handlers_count; i++) {
+ if (device->handlers[i] == NULL)
+ continue;
+
+ if (device->handlers[i]->handle == handle) {
+ if (enabled && device->handlers[i]->activate != NULL) {
+ device->handlers[i]->needed |= HERRING_SENSORS_NEEDED_API;
+ if (device->handlers[i]->needed == HERRING_SENSORS_NEEDED_API)
+ return device->handlers[i]->activate(device->handlers[i]);
+ else
+ return 0;
+ } else if (!enabled && device->handlers[i]->deactivate != NULL) {
+ device->handlers[i]->needed &= ~HERRING_SENSORS_NEEDED_API;
+ if (device->handlers[i]->needed == 0)
+ return device->handlers[i]->deactivate(device->handlers[i]);
+ else
+ return 0;
+ }
+ }
+ }
+
+ return -1;
+}
+
+int herring_sensors_set_delay(struct sensors_poll_device_t *dev, int handle,
+ int64_t ns)
+{
+ struct herring_sensors_device *device;
+ int i;
+
+ ALOGD("%s(%p, %d, %ld)", __func__, dev, handle, (long int) ns);
+
+ if (dev == NULL)
+ return -EINVAL;
+
+ device = (struct herring_sensors_device *) dev;
+
+ if (device->handlers == NULL || device->handlers_count <= 0)
+ return -EINVAL;
+
+ for (i = 0; i < device->handlers_count; i++) {
+ if (device->handlers[i] == NULL)
+ continue;
+
+ if (device->handlers[i]->handle == handle && device->handlers[i]->set_delay != NULL)
+ return device->handlers[i]->set_delay(device->handlers[i], (long int) ns);
+ }
+
+ return 0;
+}
+
+int herring_sensors_poll(struct sensors_poll_device_t *dev,
+ struct sensors_event_t* data, int count)
+{
+ struct herring_sensors_device *device;
+ int i, j;
+ int c, n;
+ int poll_rc, rc;
+
+// ALOGD("%s(%p, %p, %d)", __func__, dev, data, count);
+
+ if (dev == NULL)
+ return -EINVAL;
+
+ device = (struct herring_sensors_device *) dev;
+
+ if (device->handlers == NULL || device->handlers_count <= 0 ||
+ device->poll_fds == NULL || device->poll_fds_count <= 0)
+ return -EINVAL;
+
+ n = 0;
+
+ do {
+ poll_rc = poll(device->poll_fds, device->poll_fds_count, n > 0 ? 0 : -1);
+ if (poll_rc < 0)
+ return -1;
+
+ for (i = 0; i < device->poll_fds_count; i++) {
+ if (!(device->poll_fds[i].revents & POLLIN))
+ continue;
+
+ for (j = 0; j < device->handlers_count; j++) {
+ if (device->handlers[j] == NULL || device->handlers[j]->poll_fd != device->poll_fds[i].fd || device->handlers[j]->get_data == NULL)
+ continue;
+
+ rc = device->handlers[j]->get_data(device->handlers[j], &data[n]);
+ if (rc < 0) {
+ device->poll_fds[i].revents = 0;
+ poll_rc = -1;
+ } else {
+ n++;
+ count--;
+ }
+ }
+ }
+ } while ((poll_rc > 0 || n < 1) && count > 0);
+
+ return n;
+}
+
+/*
+ * Interface
+ */
+
+int herring_sensors_close(hw_device_t *device)
+{
+ struct herring_sensors_device *herring_sensors_device;
+ int i;
+
+ ALOGD("%s(%p)", __func__, device);
+
+ if (device == NULL)
+ return -EINVAL;
+
+ herring_sensors_device = (struct herring_sensors_device *) device;
+
+ if (herring_sensors_device->poll_fds != NULL)
+ free(herring_sensors_device->poll_fds);
+
+ for (i = 0; i < herring_sensors_device->handlers_count; i++) {
+ if (herring_sensors_device->handlers[i] == NULL || herring_sensors_device->handlers[i]->deinit == NULL)
+ continue;
+
+ herring_sensors_device->handlers[i]->deinit(herring_sensors_device->handlers[i]);
+ }
+
+ free(device);
+
+ return 0;
+}
+
+int herring_sensors_open(const struct hw_module_t* module, const char *id,
+ struct hw_device_t** device)
+{
+ struct herring_sensors_device *herring_sensors_device;
+ int p, i;
+
+ ALOGD("%s(%p, %s, %p)", __func__, module, id, device);
+
+ if (module == NULL || device == NULL)
+ return -EINVAL;
+
+ herring_sensors_device = (struct herring_sensors_device *)
+ calloc(1, sizeof(struct herring_sensors_device));
+ herring_sensors_device->device.common.tag = HARDWARE_DEVICE_TAG;
+ herring_sensors_device->device.common.version = 0;
+ herring_sensors_device->device.common.module = (struct hw_module_t *) module;
+ herring_sensors_device->device.common.close = herring_sensors_close;
+ herring_sensors_device->device.activate = herring_sensors_activate;
+ herring_sensors_device->device.setDelay = herring_sensors_set_delay;
+ herring_sensors_device->device.poll = herring_sensors_poll;
+ herring_sensors_device->handlers = herring_sensors_handlers;
+ herring_sensors_device->handlers_count = herring_sensors_handlers_count;
+ herring_sensors_device->poll_fds = (struct pollfd *)
+ calloc(1, herring_sensors_handlers_count * sizeof(struct pollfd));
+
+ p = 0;
+ for (i = 0; i < herring_sensors_handlers_count; i++) {
+ if (herring_sensors_handlers[i] == NULL || herring_sensors_handlers[i]->init == NULL)
+ continue;
+
+ herring_sensors_handlers[i]->init(herring_sensors_handlers[i], herring_sensors_device);
+ if (herring_sensors_handlers[i]->poll_fd >= 0) {
+ herring_sensors_device->poll_fds[p].fd = herring_sensors_handlers[i]->poll_fd;
+ herring_sensors_device->poll_fds[p].events = POLLIN;
+ p++;
+ }
+ }
+
+ herring_sensors_device->poll_fds_count = p;
+
+ *device = &(herring_sensors_device->device.common);
+
+ return 0;
+}
+
+int herring_sensors_get_sensors_list(struct sensors_module_t* module,
+ const struct sensor_t **sensors_p)
+{
+ ALOGD("%s(%p, %p)", __func__, module, sensors_p);
+
+ if (sensors_p == NULL)
+ return -EINVAL;
+
+ *sensors_p = herring_sensors;
+ return herring_sensors_count;
+}
+
+struct hw_module_methods_t herring_sensors_module_methods = {
+ .open = herring_sensors_open,
+};
+
+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 = "Herring Sensors",
+ .author = "Paul Kocialkowski",
+ .methods = &herring_sensors_module_methods,
+ },
+ .get_sensors_list = herring_sensors_get_sensors_list,
+};
diff --git a/sensors/herring_sensors.h b/sensors/herring_sensors.h
new file mode 100644
index 0000000..4037c23
--- /dev/null
+++ b/sensors/herring_sensors.h
@@ -0,0 +1,104 @@
+/*
+ * Copyright (C) 2013 Paul Kocialkowski <contact@paulk.fr>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <stdint.h>
+#include <poll.h>
+#include <linux/input.h>
+
+#include <hardware/sensors.h>
+#include <hardware/hardware.h>
+
+#ifndef _HERRING_SENSORS_H_
+#define _HERRING_SENSORS_H_
+
+#define HERRING_SENSORS_NEEDED_API (1 << 0)
+#define HERRING_SENSORS_NEEDED_ORIENTATION (1 << 1)
+
+struct herring_sensors_device;
+
+struct herring_sensors_handlers {
+ char *name;
+ int handle;
+
+ int (*init)(struct herring_sensors_handlers *handlers,
+ struct herring_sensors_device *device);
+ int (*deinit)(struct herring_sensors_handlers *handlers);
+ int (*activate)(struct herring_sensors_handlers *handlers);
+ int (*deactivate)(struct herring_sensors_handlers *handlers);
+ int (*set_delay)(struct herring_sensors_handlers *handlers,
+ long int delay);
+ int (*get_data)(struct herring_sensors_handlers *handlers,
+ struct sensors_event_t *event);
+
+ int activated;
+ int needed;
+ int poll_fd;
+
+ void *data;
+};
+
+struct herring_sensors_device {
+ struct sensors_poll_device_t device;
+
+ struct herring_sensors_handlers **handlers;
+ int handlers_count;
+
+ struct pollfd *poll_fds;
+ int poll_fds_count;
+};
+
+extern struct herring_sensors_handlers *herring_sensors_handlers[];
+extern int herring_sensors_handlers_count;
+
+int herring_sensors_activate(struct sensors_poll_device_t *dev, int handle,
+ int enabled);
+int herring_sensors_set_delay(struct sensors_poll_device_t *dev, int handle,
+ int64_t ns);
+int herring_sensors_poll(struct sensors_poll_device_t *dev,
+ struct sensors_event_t* data, int count);
+
+/*
+ * Input
+ */
+
+void input_event_set(struct input_event *event, int type, int code, int value);
+long int timestamp(struct timeval *time);
+long int input_timestamp(struct input_event *event);
+int uinput_rel_create(const char *name);
+void uinput_destroy(int uinput_fd);
+int input_open(char *name);
+int sysfs_path_prefix(char *name, char *path_prefix);
+int sysfs_value_read(char *path);
+int sysfs_value_write(char *path, int value);
+int sysfs_string_read(char *path, char *buffer, size_t length);
+int sysfs_string_write(char *path, char *buffer, size_t length);
+
+/*
+ * Sensors
+ */
+
+int orientation_fill(struct herring_sensors_handlers *handlers,
+ sensors_vec_t *acceleration, sensors_vec_t *magnetic);
+
+extern struct herring_sensors_handlers kr3dm;
+extern struct herring_sensors_handlers akm8973;
+extern struct herring_sensors_handlers orientation;
+extern struct herring_sensors_handlers gp2a_light;
+extern struct herring_sensors_handlers gp2a_proximity;
+extern struct herring_sensors_handlers k3g;
+
+#endif
diff --git a/sensors/input.c b/sensors/input.c
new file mode 100644
index 0000000..2022f21
--- /dev/null
+++ b/sensors/input.c
@@ -0,0 +1,335 @@
+/*
+ * Copyright (C) 2013 Paul Kocialkowski <contact@paulk.fr>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <stdlib.h>
+#include <unistd.h>
+#include <stdint.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <dirent.h>
+#include <linux/ioctl.h>
+#include <linux/input.h>
+#include <linux/uinput.h>
+
+#define LOG_TAG "herring_sensors"
+#include <utils/Log.h>
+
+#include "herring_sensors.h"
+
+void input_event_set(struct input_event *event, int type, int code, int value)
+{
+ if (event == NULL)
+ return;
+
+ memset(event, 0, sizeof(struct input_event));
+
+ event->type = type,
+ event->code = code;
+ event->value = value;
+
+ gettimeofday(&event->time, NULL);
+}
+
+long int timestamp(struct timeval *time)
+{
+ if (time == NULL)
+ return -1;
+
+ return time->tv_sec * 1000000000LL + time->tv_usec * 1000;
+}
+
+long int input_timestamp(struct input_event *event)
+{
+ if (event == NULL)
+ return -1;
+
+ return timestamp(&event->time);
+}
+
+int uinput_rel_create(const char *name)
+{
+ struct uinput_user_dev uinput_dev;
+ int uinput_fd;
+ int rc;
+
+ if (name == NULL)
+ return -1;
+
+ uinput_fd = open("/dev/uinput", O_WRONLY | O_NONBLOCK);
+ if (uinput_fd < 0) {
+ ALOGE("%s: Unable to open uinput device", __func__);
+ goto error;
+ }
+
+ memset(&uinput_dev, 0, sizeof(uinput_dev));
+
+ strncpy(uinput_dev.name, name, sizeof(uinput_dev.name));
+ uinput_dev.id.bustype = BUS_I2C;
+ uinput_dev.id.vendor = 0;
+ uinput_dev.id.product = 0;
+ uinput_dev.id.version = 0;
+
+ rc = 0;
+ rc |= ioctl(uinput_fd, UI_SET_EVBIT, EV_REL);
+ rc |= ioctl(uinput_fd, UI_SET_RELBIT, REL_X);
+ rc |= ioctl(uinput_fd, UI_SET_RELBIT, REL_Y);
+ rc |= ioctl(uinput_fd, UI_SET_RELBIT, REL_Z);
+ rc |= ioctl(uinput_fd, UI_SET_EVBIT, EV_SYN);
+
+ if (rc < 0) {
+ ALOGE("%s: Unable to set uinput bits", __func__);
+ goto error;
+ }
+
+ rc = write(uinput_fd, &uinput_dev, sizeof(uinput_dev));
+ if (rc < 0) {
+ ALOGE("%s: Unable to write uinput device", __func__);
+ goto error;
+ }
+
+ rc = ioctl(uinput_fd, UI_DEV_CREATE);
+ if (rc < 0) {
+ ALOGE("%s: Unable to create uinput device", __func__);
+ goto error;
+ }
+
+ usleep(3000);
+
+ return uinput_fd;
+
+error:
+ if (uinput_fd >= 0)
+ close(uinput_fd);
+
+ return -1;
+}
+
+void uinput_destroy(int uinput_fd)
+{
+ if (uinput_fd < 0)
+ return;
+
+ ioctl(uinput_fd, UI_DEV_DESTROY);
+}
+
+int input_open(char *name)
+{
+ DIR *d;
+ struct dirent *di;
+
+ char input_name[80] = { 0 };
+ char path[PATH_MAX];
+ char *c;
+ int fd;
+ int rc;
+
+ if (name == NULL)
+ return -EINVAL;
+
+ d = opendir("/dev/input");
+ if (d == NULL)
+ return -1;
+
+ while ((di = readdir(d))) {
+ if (di == NULL || strcmp(di->d_name, ".") == 0 || strcmp(di->d_name, "..") == 0)
+ continue;
+
+ snprintf(path, PATH_MAX, "/dev/input/%s", di->d_name);
+ fd = open(path, O_RDONLY | O_NONBLOCK);
+ if (fd < 0)
+ continue;
+
+ rc = ioctl(fd, EVIOCGNAME(sizeof(input_name) - 1), &input_name);
+ if (rc < 0)
+ continue;
+
+ c = strstr((char *) &input_name, "\n");
+ if (c != NULL)
+ *c = '\0';
+
+ if (strcmp(input_name, name) == 0)
+ return fd;
+ else
+ close(fd);
+ }
+
+ return -1;
+}
+
+int sysfs_path_prefix(char *name, char *path_prefix)
+{
+ DIR *d;
+ struct dirent *di;
+
+ char input_name[80] = { 0 };
+ char path[PATH_MAX];
+ char *c;
+ int fd;
+
+ if (name == NULL || path_prefix == NULL)
+ return -EINVAL;
+
+ d = opendir("/sys/class/input");
+ if (d == NULL)
+ return -1;
+
+ while ((di = readdir(d))) {
+ if (di == NULL || strcmp(di->d_name, ".") == 0 || strcmp(di->d_name, "..") == 0)
+ continue;
+
+ snprintf(path, PATH_MAX, "/sys/class/input/%s/name", di->d_name);
+
+ fd = open(path, O_RDONLY);
+ if (fd < 0)
+ continue;
+
+ read(fd, &input_name, sizeof(input_name));
+ close(fd);
+
+ c = strstr((char *) &input_name, "\n");
+ if (c != NULL)
+ *c = '\0';
+
+ if (strcmp(input_name, name) == 0) {
+ snprintf(path_prefix, PATH_MAX, "/sys/class/input/%s", di->d_name);
+ return 0;
+ }
+ }
+
+ return -1;
+}
+
+int sysfs_value_read(char *path)
+{
+ char buffer[100];
+ int value;
+ int fd = -1;
+ int rc;
+
+ if (path == NULL)
+ return -1;
+
+ fd = open(path, O_RDONLY);
+ if (fd < 0)
+ goto error;
+
+ rc = read(fd, &buffer, sizeof(buffer));
+ if (rc <= 0)
+ goto error;
+
+ value = atoi(buffer);
+ goto complete;
+
+error:
+ value = -1;
+
+complete:
+ if (fd >= 0)
+ close(fd);
+
+ return value;
+}
+
+int sysfs_value_write(char *path, int value)
+{
+ char buffer[100];
+ int fd = -1;
+ int rc;
+
+ if (path == NULL)
+ return -1;
+
+ fd = open(path, O_WRONLY);
+ if (fd < 0)
+ goto error;
+
+ snprintf((char *) &buffer, sizeof(buffer), "%d\n", value);
+
+ rc = write(fd, buffer, strlen(buffer));
+ if (rc < (int) strlen(buffer))
+ goto error;
+
+ rc = 0;
+ goto complete;
+
+error:
+ rc = -1;
+
+complete:
+ if (fd >= 0)
+ close(fd);
+
+ return rc;
+}
+
+int sysfs_string_read(char *path, char *buffer, size_t length)
+{
+ int fd = -1;
+ int rc;
+
+ if (path == NULL || buffer == NULL || length == 0)
+ return -1;
+
+ fd = open(path, O_RDONLY);
+ if (fd < 0)
+ goto error;
+
+ rc = read(fd, buffer, length);
+ if (rc <= 0)
+ goto error;
+
+ rc = 0;
+ goto complete;
+
+error:
+ rc = -1;
+
+complete:
+ if (fd >= 0)
+ close(fd);
+
+ return rc;
+}
+
+int sysfs_string_write(char *path, char *buffer, size_t length)
+{
+ int fd = -1;
+ int rc;
+
+ if (path == NULL || buffer == NULL || length == 0)
+ return -1;
+
+ fd = open(path, O_WRONLY);
+ if (fd < 0)
+ goto error;
+
+ rc = write(fd, buffer, length);
+ if (rc <= 0)
+ goto error;
+
+ rc = 0;
+ goto complete;
+
+error:
+ rc = -1;
+
+complete:
+ if (fd >= 0)
+ close(fd);
+
+ return rc;
+}
diff --git a/sensors/k3g.c b/sensors/k3g.c
new file mode 100644
index 0000000..5f166f5
--- /dev/null
+++ b/sensors/k3g.c
@@ -0,0 +1,254 @@
+/*
+ * Copyright (C) 2013 Paul Kocialkowski <contact@paulk.fr>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <stdlib.h>
+#include <unistd.h>
+#include <stdint.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <math.h>
+#include <sys/types.h>
+#include <linux/ioctl.h>
+#include <linux/input.h>
+
+#include <hardware/sensors.h>
+#include <hardware/hardware.h>
+
+#define LOG_TAG "herring_sensors"
+#include <utils/Log.h>
+
+#include "herring_sensors.h"
+
+struct k3g_data {
+ char path_enable[PATH_MAX];
+ char path_delay[PATH_MAX];
+
+ sensors_vec_t gyro;
+};
+
+int k3g_init(struct herring_sensors_handlers *handlers,
+ struct herring_sensors_device *device)
+{
+ struct k3g_data *data = NULL;
+ char path[PATH_MAX] = { 0 };
+ int input_fd = -1;
+ int rc;
+
+ ALOGD("%s(%p, %p)", __func__, handlers, device);
+
+ if (handlers == NULL)
+ return -EINVAL;
+
+ data = (struct k3g_data *) calloc(1, sizeof(struct k3g_data));
+
+ input_fd = input_open("gyro");
+ if (input_fd < 0) {
+ ALOGE("%s: Unable to open input", __func__);
+ goto error;
+ }
+
+ rc = sysfs_path_prefix("gyro", (char *) &path);
+ if (rc < 0 || path[0] == '\0') {
+ ALOGE("%s: Unable to open sysfs", __func__);
+ goto error;
+ }
+
+ snprintf(data->path_enable, PATH_MAX, "%s/enable", path);
+ snprintf(data->path_delay, PATH_MAX, "%s/poll_delay", path);
+
+ handlers->poll_fd = input_fd;
+ handlers->data = (void *) data;
+
+ return 0;
+
+error:
+ if (data != NULL)
+ free(data);
+
+ if (input_fd >= 0)
+ close(input_fd);
+
+ handlers->poll_fd = -1;
+ handlers->data = NULL;
+
+ return -1;
+}
+
+int k3g_deinit(struct herring_sensors_handlers *handlers)
+{
+ ALOGD("%s(%p)", __func__, handlers);
+
+ if (handlers == NULL)
+ return -EINVAL;
+
+ if (handlers->poll_fd >= 0)
+ close(handlers->poll_fd);
+ handlers->poll_fd = -1;
+
+ if (handlers->data != NULL)
+ free(handlers->data);
+ handlers->data = NULL;
+
+ return 0;
+}
+
+int k3g_activate(struct herring_sensors_handlers *handlers)
+{
+ struct k3g_data *data;
+ int rc;
+
+ ALOGD("%s(%p)", __func__, handlers);
+
+ if (handlers == NULL || handlers->data == NULL)
+ return -EINVAL;
+
+ data = (struct k3g_data *) handlers->data;
+
+ rc = sysfs_value_write(data->path_enable, 1);
+ if (rc < 0) {
+ ALOGE("%s: Unable to write sysfs value", __func__);
+ return -1;
+ }
+
+ handlers->activated = 1;
+
+ return 0;
+}
+
+int k3g_deactivate(struct herring_sensors_handlers *handlers)
+{
+ struct k3g_data *data;
+ int rc;
+
+ ALOGD("%s(%p)", __func__, handlers);
+
+ if (handlers == NULL || handlers->data == NULL)
+ return -EINVAL;
+
+ data = (struct k3g_data *) handlers->data;
+
+ rc = sysfs_value_write(data->path_enable, 0);
+ if (rc < 0) {
+ ALOGE("%s: Unable to write sysfs value", __func__);
+ return -1;
+ }
+
+ handlers->activated = 1;
+
+ return 0;
+}
+
+int k3g_set_delay(struct herring_sensors_handlers *handlers, long int delay)
+{
+ struct k3g_data *data;
+ int rc;
+
+ ALOGD("%s(%p, %ld)", __func__, handlers, delay);
+
+ if (handlers == NULL || handlers->data == NULL)
+ return -EINVAL;
+
+ data = (struct k3g_data *) handlers->data;
+
+ rc = sysfs_value_write(data->path_delay, (int) delay);
+ if (rc < 0) {
+ ALOGE("%s: Unable to write sysfs value", __func__);
+ return -1;
+ }
+
+ return 0;
+}
+
+float k3g_convert(int value)
+{
+ return value * (70.0f / 1000.0f) * (3.1415926535f / 180.0f);
+}
+
+int k3g_get_data(struct herring_sensors_handlers *handlers,
+ struct sensors_event_t *event)
+{
+ struct k3g_data *data;
+ struct input_event input_event;
+ int input_fd;
+ int rc;
+
+// ALOGD("%s(%p, %p)", __func__, handlers, event);
+
+ if (handlers == NULL || handlers->data == NULL || event == NULL)
+ return -EINVAL;
+
+ data = (struct k3g_data *) handlers->data;
+
+ input_fd = handlers->poll_fd;
+ if (input_fd < 0)
+ return -EINVAL;
+
+ memset(event, 0, sizeof(struct sensors_event_t));
+ event->version = sizeof(struct sensors_event_t);
+ event->sensor = handlers->handle;
+ event->type = handlers->handle;
+
+ event->gyro.x = data->gyro.x;
+ event->gyro.y = data->gyro.y;
+ event->gyro.z = data->gyro.z;
+
+ do {
+ rc = read(input_fd, &input_event, sizeof(input_event));
+ if (rc < (int) sizeof(input_event))
+ break;
+
+ if (input_event.type == EV_REL) {
+ switch (input_event.code) {
+ case REL_RX:
+ event->gyro.y = k3g_convert(input_event.value * -1);
+ break;
+ case REL_RY:
+ event->gyro.x = k3g_convert(input_event.value);
+ break;
+ case REL_RZ:
+ event->gyro.z = k3g_convert(input_event.value);
+ break;
+ default:
+ continue;
+ }
+ } else if (input_event.type == EV_SYN) {
+ if (input_event.code == SYN_REPORT)
+ event->timestamp = input_timestamp(&input_event);
+ }
+ } while (input_event.type != EV_SYN);
+
+ data->gyro.x = event->gyro.x;
+ data->gyro.y = event->gyro.y;
+ data->gyro.z = event->gyro.z;
+
+ return 0;
+}
+
+struct herring_sensors_handlers k3g = {
+ .name = "K3G",
+ .handle = SENSOR_TYPE_GYROSCOPE,
+ .init = k3g_init,
+ .deinit = k3g_deinit,
+ .activate = k3g_activate,
+ .deactivate = k3g_deactivate,
+ .set_delay = k3g_set_delay,
+ .get_data = k3g_get_data,
+ .activated = 0,
+ .needed = 0,
+ .poll_fd = -1,
+ .data = NULL,
+};
diff --git a/sensors/kr3dm.c b/sensors/kr3dm.c
new file mode 100644
index 0000000..c68c77d
--- /dev/null
+++ b/sensors/kr3dm.c
@@ -0,0 +1,375 @@
+/*
+ * Copyright (C) 2013 Paul Kocialkowski <contact@paulk.fr>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <stdlib.h>
+#include <unistd.h>
+#include <stdint.h>
+#include <fcntl.h>
+#include <errno.h>
+
+#include <hardware/sensors.h>
+#include <hardware/hardware.h>
+
+#define LOG_TAG "herring_sensors"
+#include <utils/Log.h>
+
+#include "herring_sensors.h"
+#include "kr3dm.h"
+
+struct kr3dm_data {
+ struct herring_sensors_handlers *orientation_sensor;
+
+ long int delay;
+ int device_fd;
+ int uinput_fd;
+
+ pthread_t thread;
+ pthread_mutex_t mutex;
+ int thread_continue;
+};
+
+void *kr3dm_thread(void *thread_data)
+{
+ struct herring_sensors_handlers *handlers = NULL;
+ struct kr3dm_data *data = NULL;
+ struct input_event event;
+ struct timeval time;
+ struct kr3dm_acceldata acceleration_data;
+ long int before, after;
+ int diff;
+ int device_fd;
+ int uinput_fd;
+ int rc;
+
+ if (thread_data == NULL)
+ return NULL;
+
+ handlers = (struct herring_sensors_handlers *) thread_data;
+ if (handlers->data == NULL)
+ return NULL;
+
+ data = (struct kr3dm_data *) handlers->data;
+
+ device_fd = data->device_fd;
+ if (device_fd < 0)
+ return NULL;
+
+ uinput_fd = data->uinput_fd;
+ if (uinput_fd < 0)
+ return NULL;
+
+ while (data->thread_continue) {
+ pthread_mutex_lock(&data->mutex);
+ if (!data->thread_continue)
+ break;
+
+ while (handlers->activated) {
+ gettimeofday(&time, NULL);
+ before = timestamp(&time);
+
+ memset(&acceleration_data, 0, sizeof(acceleration_data));
+
+ rc = ioctl(device_fd, KR3DM_IOCTL_READ_ACCEL_XYZ, &acceleration_data);
+ if (rc < 0) {
+ ALOGE("%s: Unable to get kr3dm data", __func__);
+ return NULL;
+ }
+
+ input_event_set(&event, EV_REL, REL_X, (int) (acceleration_data.x * 1000));
+ write(uinput_fd, &event, sizeof(event));
+ input_event_set(&event, EV_REL, REL_Y, (int) (acceleration_data.y * 1000));
+ write(uinput_fd, &event, sizeof(event));
+ input_event_set(&event, EV_REL, REL_Z, (int) (acceleration_data.z * 1000));
+ write(uinput_fd, &event, sizeof(event));
+ input_event_set(&event, EV_SYN, 0, 0);
+ write(uinput_fd, &event, sizeof(event));
+
+next:
+ gettimeofday(&time, NULL);
+ after = timestamp(&time);
+
+ diff = (int) (data->delay - (after - before)) / 1000;
+ if (diff <= 0)
+ continue;
+
+ usleep(diff);
+ }
+ }
+ return NULL;
+}
+
+int kr3dm_init(struct herring_sensors_handlers *handlers,
+ struct herring_sensors_device *device)
+{
+ struct kr3dm_data *data = NULL;
+ pthread_attr_t thread_attr;
+ int device_fd = -1;
+ int uinput_fd = -1;
+ int input_fd = -1;
+ int rc;
+ int i;
+
+ ALOGD("%s(%p, %p)", __func__, handlers, device);
+
+ if (handlers == NULL || device == NULL)
+ return -EINVAL;
+
+ data = (struct kr3dm_data *) calloc(1, sizeof(struct kr3dm_data));
+
+ for (i = 0; i < device->handlers_count; i++) {
+ if (device->handlers[i] == NULL)
+ continue;
+
+ if (device->handlers[i]->handle == SENSOR_TYPE_ORIENTATION)
+ data->orientation_sensor = device->handlers[i];
+ }
+
+ device_fd = open("/dev/accelerometer", O_RDONLY);
+ if (device_fd < 0) {
+ ALOGE("%s: Unable to open device", __func__);
+ goto error;
+ }
+
+ uinput_fd = uinput_rel_create("acceleration");
+ if (uinput_fd < 0) {
+ ALOGD("%s: Unable to create uinput", __func__);
+ goto error;
+ }
+
+ input_fd = input_open("acceleration");
+ if (input_fd < 0) {
+ ALOGE("%s: Unable to open acceleration input", __func__);
+ goto error;
+ }
+
+ data->thread_continue = 1;
+
+ pthread_mutex_init(&data->mutex, NULL);
+ pthread_mutex_lock(&data->mutex);
+
+ pthread_attr_init(&thread_attr);
+ pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED);
+
+ rc = pthread_create(&data->thread, &thread_attr, kr3dm_thread, (void *) handlers);
+ if (rc < 0) {
+ ALOGE("%s: Unable to create kr3dm thread", __func__);
+ pthread_mutex_destroy(&data->mutex);
+ goto error;
+ }
+
+ data->device_fd = device_fd;
+ data->uinput_fd = uinput_fd;
+ handlers->poll_fd = input_fd;
+ handlers->data = (void *) data;
+
+ return 0;
+
+error:
+ if (data != NULL)
+ free(data);
+
+ if (uinput_fd >= 0)
+ close(uinput_fd);
+
+ if (input_fd >= 0)
+ close(input_fd);
+
+ if (device_fd >= 0)
+ close(device_fd);
+
+ handlers->poll_fd = -1;
+ handlers->data = NULL;
+
+ return -1;
+}
+
+int kr3dm_deinit(struct herring_sensors_handlers *handlers)
+{
+ struct kr3dm_data *data = NULL;
+
+ ALOGD("%s(%p)", __func__, handlers);
+
+ if (handlers == NULL || handlers->data == NULL)
+ return -EINVAL;
+
+ data = (struct kr3dm_data *) handlers->data;
+
+ handlers->activated = 0;
+ data->thread_continue = 0;
+ pthread_mutex_unlock(&data->mutex);
+
+ pthread_mutex_destroy(&data->mutex);
+
+ if (data->uinput_fd >= 0) {
+ uinput_destroy(data->uinput_fd);
+ close(data->uinput_fd);
+ }
+ data->uinput_fd = -1;
+
+ if (handlers->poll_fd >= 0)
+ close(handlers->poll_fd);
+ handlers->poll_fd = -1;
+
+ if (data->device_fd >= 0)
+ close(data->device_fd);
+ data->device_fd = -1;
+
+ free(handlers->data);
+ handlers->data = NULL;
+
+ return 0;
+}
+
+int kr3dm_activate(struct herring_sensors_handlers *handlers)
+{
+ struct kr3dm_data *data;
+
+ ALOGD("%s(%p)", __func__, handlers);
+
+ if (handlers == NULL || handlers->data == NULL)
+ return -EINVAL;
+
+ data = (struct kr3dm_data *) handlers->data;
+
+ handlers->activated = 1;
+ pthread_mutex_unlock(&data->mutex);
+
+ return 0;
+}
+
+int kr3dm_deactivate(struct herring_sensors_handlers *handlers)
+{
+ struct kr3dm_data *data;
+
+ ALOGD("%s(%p)", __func__, handlers);
+
+ if (handlers == NULL || handlers->data == NULL)
+ return -EINVAL;
+
+ data = (struct kr3dm_data *) handlers->data;
+
+ handlers->activated = 0;
+
+ return 0;
+}
+
+int kr3dm_set_delay(struct herring_sensors_handlers *handlers, long int delay)
+{
+ struct kr3dm_data *data;
+ int64_t d;
+ int device_fd;
+ int rc;
+
+ ALOGD("%s(%p, %ld)", __func__, handlers, delay);
+
+ if (handlers == NULL || handlers->data == NULL)
+ return -EINVAL;
+
+ data = (struct kr3dm_data *) handlers->data;
+
+ device_fd = data->device_fd;
+ if (device_fd < 0)
+ return -1;
+
+ d = (int64_t) delay;
+ rc = ioctl(device_fd, KR3DM_IOCTL_SET_DELAY, &d);
+ if (rc < 0) {
+ ALOGE("%s: Unable to set kr3dm delay", __func__);
+ return -1;
+ }
+
+ data->delay = delay;
+
+ return 0;
+}
+
+float kr3dm_convert(int value)
+{
+ return (float) (value / 1000.0f) * GRAVITY_EARTH / 64.0f / 8.0f;
+}
+
+int kr3dm_get_data(struct herring_sensors_handlers *handlers,
+ struct sensors_event_t *event)
+{
+ struct kr3dm_data *data;
+ struct input_event input_event;
+ int input_fd;
+ int rc;
+
+// ALOGD("%s(%p, %p)", __func__, handlers, event);
+
+ if (handlers == NULL || handlers->data == NULL || event == NULL)
+ return -EINVAL;
+
+ data = (struct kr3dm_data *) handlers->data;
+
+ input_fd = handlers->poll_fd;
+ if (input_fd < 0)
+ return -1;
+
+ memset(event, 0, sizeof(struct sensors_event_t));
+ event->version = sizeof(struct sensors_event_t);
+ event->sensor = handlers->handle;
+ event->type = handlers->handle;
+
+ event->magnetic.status = SENSOR_STATUS_ACCURACY_MEDIUM;
+
+ do {
+ rc = read(input_fd, &input_event, sizeof(input_event));
+ if (rc < (int) sizeof(input_event))
+ break;
+
+ if (input_event.type == EV_REL) {
+ switch (input_event.code) {
+ case REL_X:
+ event->acceleration.y = kr3dm_convert(input_event.value * -1);
+ break;
+ case REL_Y:
+ event->acceleration.x = kr3dm_convert(input_event.value);
+ break;
+ case REL_Z:
+ event->acceleration.z = kr3dm_convert(input_event.value);
+ break;
+ default:
+ continue;
+ }
+ } else if (input_event.type == EV_SYN) {
+ if (input_event.code == SYN_REPORT)
+ event->timestamp = input_timestamp(&input_event);
+ }
+ } while (input_event.type != EV_SYN);
+
+ if (data->orientation_sensor != NULL)
+ orientation_fill(data->orientation_sensor, &event->acceleration, NULL);
+
+ return 0;
+}
+
+struct herring_sensors_handlers kr3dm = {
+ .name = "KR3DM",
+ .handle = SENSOR_TYPE_ACCELEROMETER,
+ .init = kr3dm_init,
+ .deinit = kr3dm_deinit,
+ .activate = kr3dm_activate,
+ .deactivate = kr3dm_deactivate,
+ .set_delay = kr3dm_set_delay,
+ .get_data = kr3dm_get_data,
+ .activated = 0,
+ .needed = 0,
+ .poll_fd = -1,
+ .data = NULL,
+};
diff --git a/sensors/kr3dm.h b/sensors/kr3dm.h
new file mode 100644
index 0000000..d404d28
--- /dev/null
+++ b/sensors/kr3dm.h
@@ -0,0 +1,46 @@
+/*
+ * STMicroelectronics kr3dm acceleration sensor driver
+ *
+ * Copyright (C) 2010 Samsung Electronics Co.Ltd
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ */
+
+#ifndef __KR3DM_ACC_HEADER__
+#define __KR3DM__ACC_HEADER__
+
+#include <linux/types.h>
+#include <linux/ioctl.h>
+
+struct kr3dm_acceldata {
+ __s16 x;
+ __s16 y;
+ __s16 z;
+};
+
+/* dev info */
+#define ACC_DEV_NAME "accelerometer"
+
+/* kr3dm ioctl command label */
+#define KR3DM_IOCTL_BASE 'a'
+#define KR3DM_IOCTL_SET_DELAY _IOW(KR3DM_IOCTL_BASE, 0, int64_t)
+#define KR3DM_IOCTL_GET_DELAY _IOR(KR3DM_IOCTL_BASE, 1, int64_t)
+#define KR3DM_IOCTL_READ_ACCEL_XYZ _IOR(KR3DM_IOCTL_BASE, 8, \
+ struct kr3dm_acceldata)
+
+#ifdef __KERNEL__
+struct kr3dm_platform_data {
+ int gpio_acc_int; /* gpio for kr3dm int output */
+ s8 *rotation; /* rotation matrix, if NULL assume Id */
+};
+#endif /* __KERNEL__ */
+
+#endif
diff --git a/sensors/orientation.c b/sensors/orientation.c
new file mode 100644
index 0000000..40a7872
--- /dev/null
+++ b/sensors/orientation.c
@@ -0,0 +1,444 @@
+/*
+ * Copyright (C) 2013 Paul Kocialkowski <contact@paulk.fr>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <stdlib.h>
+#include <unistd.h>
+#include <stddef.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <math.h>
+#include <linux/ioctl.h>
+#include <linux/uinput.h>
+#include <linux/input.h>
+
+#include <hardware/sensors.h>
+#include <hardware/hardware.h>
+
+#define LOG_TAG "herring_sensors"
+#include <utils/Log.h>
+
+#include "herring_sensors.h"
+
+struct orientation_data {
+ struct herring_sensors_handlers *acceleration_sensor;
+ struct herring_sensors_handlers *magnetic_sensor;
+
+ sensors_vec_t orientation;
+ sensors_vec_t acceleration;
+ sensors_vec_t magnetic;
+
+ long int delay;
+ int uinput_fd;
+
+ pthread_t thread;
+ pthread_mutex_t mutex;
+ int thread_continue;
+};
+
+static float rad2deg(float v)
+{
+ return (v * 180.0f / 3.1415926535f);
+}
+
+static float vector_scalar(sensors_vec_t *v, sensors_vec_t *d)
+{
+ return v->x * d->x + v->y * d->y + v->z * d->z;
+}
+
+static float vector_length(sensors_vec_t *v)
+{
+ return sqrtf(vector_scalar(v, v));
+}
+
+void orientation_calculate(sensors_vec_t *a, sensors_vec_t *m, sensors_vec_t *o)
+{
+ float azimuth, pitch, roll;
+ float la, sinp, cosp, sinr, cosr, x, y;
+
+ if (a == NULL || m == NULL || o == NULL)
+ return;
+
+ la = vector_length(a);
+ pitch = asinf(-(a->y) / la);
+ roll = asinf((a->x) / la);
+
+ sinp = sinf(pitch);
+ cosp = cosf(pitch);
+ sinr = sinf(roll);
+ cosr = cosf(roll);
+
+ y = -(m->x) * cosr + m->z * sinr;
+ x = m->x * sinp * sinr + m->y * cosp + m->z * sinp * cosr;
+ azimuth = atan2f(y, x);
+
+ o->azimuth = rad2deg(azimuth);
+ o->pitch = rad2deg(pitch);
+ o->roll = rad2deg(roll);
+
+ if (o->azimuth < 0)
+ o->azimuth += 360.0f;
+}
+
+void *orientation_thread(void *thread_data)
+{
+ struct herring_sensors_handlers *handlers = NULL;
+ struct orientation_data *data = NULL;
+ struct input_event event;
+ struct timeval time;
+ long int before, after;
+ int diff;
+ int uinput_fd;
+
+ if (thread_data == NULL)
+ return NULL;
+
+ handlers = (struct herring_sensors_handlers *) thread_data;
+ if (handlers->data == NULL)
+ return NULL;
+
+ data = (struct orientation_data *) handlers->data;
+
+ uinput_fd = data->uinput_fd;
+ if (uinput_fd < 0)
+ return NULL;
+
+ while (data->thread_continue) {
+ pthread_mutex_lock(&data->mutex);
+ if (!data->thread_continue)
+ break;
+
+ while (handlers->activated) {
+ gettimeofday(&time, NULL);
+ before = timestamp(&time);
+
+ orientation_calculate(&data->acceleration, &data->magnetic, &data->orientation);
+
+ input_event_set(&event, EV_REL, REL_X, (int) (data->orientation.azimuth * 1000));
+ write(uinput_fd, &event, sizeof(event));
+ input_event_set(&event, EV_REL, REL_Y, (int) (data->orientation.pitch * 1000));
+ write(uinput_fd, &event, sizeof(event));
+ input_event_set(&event, EV_REL, REL_Z, (int) (data->orientation.roll * 1000));
+ write(uinput_fd, &event, sizeof(event));
+ input_event_set(&event, EV_SYN, 0, 0);
+ write(uinput_fd, &event, sizeof(event));
+
+ gettimeofday(&time, NULL);
+ after = timestamp(&time);
+
+ diff = (int) (data->delay - (after - before)) / 1000;
+ if (diff <= 0)
+ continue;
+
+ usleep(diff);
+ }
+ }
+
+ return NULL;
+}
+
+int orientation_fill(struct herring_sensors_handlers *handlers,
+ sensors_vec_t *acceleration, sensors_vec_t *magnetic)
+{
+ struct orientation_data *data;
+
+// ALOGD("%s(%p, %p, %p)", __func__, handlers, acceleration, magnetic);
+
+ if (handlers == NULL || handlers->data == NULL)
+ return -EINVAL;
+
+ data = (struct orientation_data *) handlers->data;
+
+ if (acceleration != NULL) {
+ data->acceleration.x = acceleration->x;
+ data->acceleration.y = acceleration->y;
+ data->acceleration.z = acceleration->z;
+ }
+
+ if (magnetic != NULL) {
+ data->magnetic.x = magnetic->x;
+ data->magnetic.y = magnetic->y;
+ data->magnetic.z = magnetic->z;
+ }
+
+ return 0;
+}
+
+int orientation_init(struct herring_sensors_handlers *handlers,
+ struct herring_sensors_device *device)
+{
+ struct orientation_data *data = NULL;
+ pthread_attr_t thread_attr;
+ int uinput_fd = -1;
+ int input_fd = -1;
+ int rc;
+ int i;
+
+ ALOGD("%s(%p, %p)", __func__, handlers, device);
+
+ if (handlers == NULL || device == NULL)
+ return -EINVAL;
+
+ data = (struct orientation_data *) calloc(1, sizeof(struct orientation_data));
+
+ for (i = 0; i < device->handlers_count; i++) {
+ if (device->handlers[i] == NULL)
+ continue;
+
+ if (device->handlers[i]->handle == SENSOR_TYPE_ACCELEROMETER)
+ data->acceleration_sensor = device->handlers[i];
+ else if (device->handlers[i]->handle == SENSOR_TYPE_MAGNETIC_FIELD)
+ data->magnetic_sensor = device->handlers[i];
+ }
+
+ if (data->acceleration_sensor == NULL || data->magnetic_sensor == NULL) {
+ ALOGE("%s: Missing sensors for orientation", __func__);
+ goto error;
+ }
+
+ uinput_fd = uinput_rel_create("orientation");
+ if (uinput_fd < 0) {
+ ALOGD("%s: Unable to create uinput", __func__);
+ goto error;
+ }
+
+ input_fd = input_open("orientation");
+ if (input_fd < 0) {
+ ALOGE("%s: Unable to open orientation input", __func__);
+ goto error;
+ }
+
+ data->thread_continue = 1;
+
+ pthread_mutex_init(&data->mutex, NULL);
+ pthread_mutex_lock(&data->mutex);
+
+ pthread_attr_init(&thread_attr);
+ pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED);
+
+ rc = pthread_create(&data->thread, &thread_attr, orientation_thread, (void *) handlers);
+ if (rc < 0) {
+ ALOGE("%s: Unable to create orientation thread", __func__);
+ pthread_mutex_destroy(&data->mutex);
+ goto error;
+ }
+
+ data->uinput_fd = uinput_fd;
+ handlers->poll_fd = input_fd;
+ handlers->data = (void *) data;
+
+ return 0;
+
+error:
+ if (data != NULL)
+ free(data);
+
+ if (uinput_fd >= 0)
+ close(uinput_fd);
+
+ if (input_fd >= 0)
+ close(input_fd);
+
+ handlers->poll_fd = -1;
+ handlers->data = NULL;
+
+ return -1;
+}
+
+int orientation_deinit(struct herring_sensors_handlers *handlers)
+{
+ struct orientation_data *data;
+
+ ALOGD("%s(%p)", __func__, handlers);
+
+ if (handlers == NULL || handlers->data == NULL)
+ return -EINVAL;
+
+ data = (struct orientation_data *) handlers->data;
+
+ handlers->activated = 0;
+ data->thread_continue = 0;
+ pthread_mutex_unlock(&data->mutex);
+
+ pthread_mutex_destroy(&data->mutex);
+
+ if (data->uinput_fd >= 0) {
+ uinput_destroy(data->uinput_fd);
+ close(data->uinput_fd);
+ }
+ data->uinput_fd = -1;
+
+ if (handlers->poll_fd >= 0)
+ close(handlers->poll_fd);
+ handlers->poll_fd = -1;
+
+ free(handlers->data);
+ handlers->data = NULL;
+
+ return 0;
+}
+
+int orientation_activate(struct herring_sensors_handlers *handlers)
+{
+ struct orientation_data *data;
+
+ ALOGD("%s(%p)", __func__, handlers);
+
+ if (handlers == NULL || handlers->data == NULL)
+ return -EINVAL;
+
+ data = (struct orientation_data *) handlers->data;
+
+ if (data->acceleration_sensor == NULL || data->magnetic_sensor == NULL)
+ return -1;
+
+ data->acceleration_sensor->needed |= HERRING_SENSORS_NEEDED_ORIENTATION;
+ if (data->acceleration_sensor->needed == HERRING_SENSORS_NEEDED_ORIENTATION)
+ data->acceleration_sensor->activate(data->acceleration_sensor);
+
+ data->magnetic_sensor->needed |= HERRING_SENSORS_NEEDED_ORIENTATION;
+ if (data->magnetic_sensor->needed == HERRING_SENSORS_NEEDED_ORIENTATION)
+ data->magnetic_sensor->activate(data->magnetic_sensor);
+
+ handlers->activated = 1;
+ pthread_mutex_unlock(&data->mutex);
+
+ return 0;
+}
+
+int orientation_deactivate(struct herring_sensors_handlers *handlers)
+{
+ struct orientation_data *data;
+
+ ALOGD("%s(%p)", __func__, handlers);
+
+ if (handlers == NULL || handlers->data == NULL)
+ return -EINVAL;
+
+ data = (struct orientation_data *) handlers->data;
+
+ if (data->acceleration_sensor == NULL || data->magnetic_sensor == NULL)
+ return -1;
+
+ data->acceleration_sensor->needed &= ~(HERRING_SENSORS_NEEDED_ORIENTATION);
+ if (data->acceleration_sensor->needed == 0)
+ data->acceleration_sensor->deactivate(data->acceleration_sensor);
+
+ data->magnetic_sensor->needed &= ~(HERRING_SENSORS_NEEDED_ORIENTATION);
+ if (data->magnetic_sensor->needed == 0)
+ data->magnetic_sensor->deactivate(data->magnetic_sensor);
+
+ handlers->activated = 0;
+
+ return 0;
+}
+
+int orientation_set_delay(struct herring_sensors_handlers *handlers,
+ long int delay)
+{
+ struct orientation_data *data;
+
+ ALOGD("%s(%p)", __func__, handlers);
+
+ if (handlers == NULL || handlers->data == NULL)
+ return -EINVAL;
+
+ data = (struct orientation_data *) handlers->data;
+
+ if (data->acceleration_sensor == NULL || data->magnetic_sensor == NULL)
+ return -1;
+
+ if (data->acceleration_sensor->needed == HERRING_SENSORS_NEEDED_ORIENTATION)
+ data->acceleration_sensor->set_delay(data->acceleration_sensor, delay);
+
+ if (data->magnetic_sensor->needed == HERRING_SENSORS_NEEDED_ORIENTATION)
+ data->magnetic_sensor->set_delay(data->magnetic_sensor, delay);
+
+ data->delay = delay;
+
+ return 0;
+}
+
+float orientation_convert(int value)
+{
+ return (float) value / 1000.0f;
+}
+
+int orientation_get_data(struct herring_sensors_handlers *handlers,
+ struct sensors_event_t *event)
+{
+ struct input_event input_event;
+ int input_fd = -1;
+ int rc;
+
+// ALOGD("%s(%p, %p)", __func__, handlers, event);
+
+ if (handlers == NULL || event == NULL)
+ return -EINVAL;
+
+ input_fd = handlers->poll_fd;
+ if (input_fd < 0)
+ return -EINVAL;
+
+ memset(event, 0, sizeof(struct sensors_event_t));
+ event->version = sizeof(struct sensors_event_t);
+ event->sensor = handlers->handle;
+ event->type = handlers->handle;
+
+ event->orientation.status = SENSOR_STATUS_ACCURACY_MEDIUM;
+
+ do {
+ rc = read(input_fd, &input_event, sizeof(input_event));
+ if (rc < (int) sizeof(input_event))
+ break;
+
+ if (input_event.type == EV_REL) {
+ switch (input_event.code) {
+ case REL_X:
+ event->orientation.azimuth = orientation_convert(input_event.value);
+ break;
+ case REL_Y:
+ event->orientation.pitch = orientation_convert(input_event.value);
+ break;
+ case REL_Z:
+ event->orientation.roll = orientation_convert(input_event.value);
+ break;
+ default:
+ continue;
+ }
+ } else if (input_event.type == EV_SYN) {
+ if (input_event.code == SYN_REPORT)
+ event->timestamp = input_timestamp(&input_event);
+ }
+ } while (input_event.type != EV_SYN);
+
+ return 0;
+}
+
+struct herring_sensors_handlers orientation = {
+ .name = "Orientation",
+ .handle = SENSOR_TYPE_ORIENTATION,
+ .init = orientation_init,
+ .deinit = orientation_deinit,
+ .activate = orientation_activate,
+ .deactivate = orientation_deactivate,
+ .set_delay = orientation_set_delay,
+ .get_data = orientation_get_data,
+ .activated = 0,
+ .needed = 0,
+ .poll_fd = -1,
+ .data = NULL,
+};