summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPaul Kocialkowski <contact@paulk.fr>2013-02-25 19:40:48 +0100
committerPaul Kocialkowski <contact@paulk.fr>2013-02-25 19:40:48 +0100
commit0c2e6b3feb59560333e7d4e0b1ff24e83c26ccb4 (patch)
tree50cdcf739ff205c34a7d16fe81d0755fb2710da5
parent52227ada7600e138054dfb286aa60e551978796e (diff)
downloaddevice_samsung_p5100-0c2e6b3feb59560333e7d4e0b1ff24e83c26ccb4.zip
device_samsung_p5100-0c2e6b3feb59560333e7d4e0b1ff24e83c26ccb4.tar.gz
device_samsung_p5100-0c2e6b3feb59560333e7d4e0b1ff24e83c26ccb4.tar.bz2
Piranha Sensors module and geomagneticd
Signed-off-by: Paul Kocialkowski <contact@paulk.fr>
-rw-r--r--geomagneticd/Android.mk31
-rw-r--r--geomagneticd/geomagneticd.c367
-rwxr-xr-xp51xx-common.mk5
-rw-r--r--sensors/Android.mk46
-rw-r--r--sensors/bh1721.c241
-rw-r--r--sensors/bma250.c293
-rw-r--r--sensors/gp2a_light.c242
-rw-r--r--sensors/gp2a_proximity.c224
-rw-r--r--sensors/input.c125
-rw-r--r--sensors/piranha_sensors.c283
-rw-r--r--sensors/piranha_sensors.h85
-rw-r--r--sensors/yas530c.c293
-rw-r--r--sensors/yas_orientation.c389
13 files changed, 2624 insertions, 0 deletions
diff --git a/geomagneticd/Android.mk b/geomagneticd/Android.mk
new file mode 100644
index 0000000..3aa3dc9
--- /dev/null
+++ b/geomagneticd/Android.mk
@@ -0,0 +1,31 @@
+#
+# Copyright (C) 2013 Paul Kocialkowski
+#
+# 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 := \
+ geomagneticd.c
+
+LOCAL_SHARED_LIBRARIES := libutils libcutils liblog
+LOCAL_PRELINK_MODULE := false
+
+LOCAL_MODULE := geomagneticd
+LOCAL_MODULE_TAGS := optional
+
+include $(BUILD_EXECUTABLE)
diff --git a/geomagneticd/geomagneticd.c b/geomagneticd/geomagneticd.c
new file mode 100644
index 0000000..5de6a7a
--- /dev/null
+++ b/geomagneticd/geomagneticd.c
@@ -0,0 +1,367 @@
+/*
+ * Copyright (C) 2013 Paul Kocialkowski
+ *
+ * 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>
+
+#define LOG_TAG "geomagneticd"
+#include <utils/Log.h>
+
+/*
+ * This is a very intuitive implementation of what's going on with p5100/p3100
+ * geomagneticd daemon. It seemed that geomagneticd sets an offset so that
+ * the biggest value (after setting the offset) is 45µT or negative -45µT.
+ * On the X axis, it happens more often to find the max around 40µT/-40µT.
+ * The reference offsets I used were: 5005 420432 1153869, and we're getting
+ * pretty close to this with that implementation.
+ *
+ */
+
+/*
+ * Input
+ */
+
+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);
+ 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;
+}
+
+/*
+ * Geomagneticd
+ */
+
+int offset_read(char *path, int *hard_offset, int *calib_offset, int *accuracy)
+{
+ char buf[100] = { 0 };
+ int fd;
+ int rc;
+
+ fd = open(path, O_RDONLY);
+ if (fd < 0)
+ return -1;
+
+ rc = read(fd, &buf, sizeof(buf));
+ close(fd);
+ if (rc <= 0)
+ return -1;
+
+ rc = sscanf(buf, "%d %d %d %d %d %d %d",
+ &hard_offset[0], &hard_offset[1], &hard_offset[2],
+ &calib_offset[0], &calib_offset[1], &calib_offset[2], accuracy);
+
+ if (rc != 7)
+ return -1;
+
+ return 0;
+}
+
+int offset_write(char *path, int *hard_offset, int *calib_offset, int accuracy)
+{
+ char buf[100] = { 0 };
+ int fd;
+ int rc;
+
+ sprintf(buf, "%d %d %d %d %d %d %d\n",
+ hard_offset[0], hard_offset[1], hard_offset[2],
+ calib_offset[0], calib_offset[1], calib_offset[2], accuracy);
+
+ fd = open(path, O_WRONLY);
+ if (fd < 0)
+ return -1;
+
+ write(fd, buf, strlen(buf) + 1);
+ close(fd);
+
+ return 0;
+}
+
+int yas_cfg_read(int *hard_offset, int *calib_offset, int *accuracy)
+{
+ char buf[100] = { 0 };
+ int fd;
+ int rc;
+
+ fd = open("/data/system/yas.cfg", O_RDONLY);
+ if (fd < 0)
+ return -1;
+
+ rc = read(fd, &buf, sizeof(buf));
+ close(fd);
+ if (rc <= 0)
+ return -1;
+
+ rc = sscanf(buf, "%d,%d,%d,%d,%d,%d,%d",
+ &hard_offset[0], &hard_offset[1], &hard_offset[2],
+ &calib_offset[0], &calib_offset[1], &calib_offset[2], accuracy);
+
+ if (rc != 7)
+ return -1;
+
+ return 0;
+}
+
+int yas_cfg_write(int *hard_offset, int *calib_offset, int accuracy)
+{
+ char buf[100] = { 0 };
+ int fd;
+ int rc;
+
+ fd = open("/data/system/yas-backup.cfg", O_WRONLY | O_TRUNC | O_CREAT);
+ if (fd < 0)
+ return -1;
+
+ sprintf(buf, "%d,%d,%d,%d,%d,%d,%d\n",
+ hard_offset[0], hard_offset[1], hard_offset[2],
+ calib_offset[0], calib_offset[1], calib_offset[2], accuracy);
+
+ write(fd, buf, strlen(buf) + 1);
+ close(fd);
+
+ chmod("/data/system/yas-backup.cfg", 0644);
+ rename("/data/system/yas-backup.cfg", "/data/system/yas.cfg");
+
+ return 0;
+}
+
+int main(int argc, char *argv[])
+{
+ struct input_event event;
+
+ char path[PATH_MAX] = { 0 };
+ char path_offset[PATH_MAX] = { 0 };
+
+ int offset_fd;
+ int input_fd;
+
+ int max_coeff[3] = { 40, 45, 45 };
+ int hard_offset[3] = { 0 };
+ int calib_offset[3] = { 0 };
+ int accuracy = 0;
+
+ int axis_min[3] = { 0 };
+ int axis_max[3] = { 0 };
+ int axis_calib[3] = { 0 };
+
+ int x, y, z;
+
+ int rc;
+ int i;
+
+ /*
+ * Wait for something to be ready and properly report the hard coeff.
+ * Without that, the hard coeff are reported to be around 127.
+ */
+
+ LOGD("Geomagneticd start");
+
+ input_fd = input_open("geomagnetic_raw");
+ if (input_fd < 0)
+ goto sleep_loop;
+
+ rc = sysfs_path_prefix("geomagnetic_raw", &path);
+ if (rc < 0)
+ goto sleep_loop;
+
+ snprintf(path_offset, PATH_MAX, "%s/offsets", path);
+
+ for (i=0 ; i < 3 ; i++) {
+ axis_min[i] = 0;
+ axis_max[i] = 0;
+ calib_offset[i] = 0;
+ }
+
+ LOGD("Reading config");
+
+ rc = yas_cfg_read(&hard_offset, &calib_offset, &accuracy);
+ if (rc == 0) {
+ LOGD("Setting initial offsets: %d %d %d, %d %d %d", hard_offset[0], hard_offset[1], hard_offset[2], calib_offset[0], calib_offset[1], calib_offset[2]);
+
+ offset_write(path_offset, &hard_offset, &calib_offset, accuracy);
+
+ for (i=0 ; i < 3 ; i++) {
+ axis_min[i] = - calib_offset[i] - max_coeff[i] * 1000;
+ axis_max[i] = calib_offset[i] + max_coeff[i] * 1000;
+ axis_calib[i] = calib_offset[i];
+ }
+ } else {
+ offset_read(path_offset, &hard_offset, &calib_offset, &accuracy);
+ LOGD("Reading initial offsets: %d %d %d", hard_offset[0], hard_offset[1], hard_offset[2]);
+
+ for (i=0 ; i < 3 ; i++) {
+ axis_min[i] = 0;
+ axis_max[i] = 0;
+ calib_offset[i] = 0;
+ }
+ }
+
+loop:
+ while (1) {
+ read(input_fd, &event, sizeof(event));
+
+ if (event.type == EV_SYN) {
+ for (i=0 ; i < 3 ; i++) {
+ if (-axis_min[i] < axis_max[i]) {
+ axis_calib[i] = axis_max[i] - max_coeff[i] * 1000;
+ } else {
+ axis_calib[i] = axis_min[i] + max_coeff[i] * 1000;
+ }
+
+ axis_calib[i] = axis_calib[i] < 0 ? -axis_calib[i] : axis_calib[i];
+
+ if (axis_calib[i] != calib_offset[i]) {
+ calib_offset[i] = axis_calib[i];
+ accuracy = 1;
+
+ offset_write(path_offset, &hard_offset, &calib_offset, accuracy);
+ yas_cfg_write(&hard_offset, &calib_offset, accuracy);
+ }
+
+// printf("axis_calib[%d]=%d\n", i, axis_calib[i]);
+ }
+
+ if (hard_offset[0] == 127 && hard_offset[1] == 127 && hard_offset[2] == 127) {
+ offset_read(path_offset, &hard_offset, &calib_offset, &accuracy);
+
+ if (hard_offset[0] != 127 || hard_offset[1] != 127 || hard_offset[2] != 127) {
+ LOGD("Reading offsets: %d %d %d", hard_offset[0], hard_offset[1], hard_offset[2]);
+ yas_cfg_write(&hard_offset, &calib_offset, accuracy);
+ }
+ }
+ }
+
+ if(event.type == EV_ABS) {
+ switch (event.code) {
+ case ABS_X:
+ x = event.value;
+ if (x < axis_min[0])
+ axis_min[0] = x;
+ if (x > axis_max[0])
+ axis_max[0] = x;
+ break;
+ case ABS_Y:
+ y = event.value;
+ if (y < axis_min[1])
+ axis_min[1] = y;
+ if (y > axis_max[1])
+ axis_max[1] = y;
+ break;
+ case ABS_Z:
+ z = event.value;
+ if (z < axis_min[2])
+ axis_min[2] = z;
+ if (z > axis_max[2])
+ axis_max[2] = z;
+ break;
+ }
+ }
+ }
+
+sleep_loop:
+ while (1) {
+ sleep(3600);
+ }
+
+ return 0;
+}
diff --git a/p51xx-common.mk b/p51xx-common.mk
index e305535..97eefda 100755
--- a/p51xx-common.mk
+++ b/p51xx-common.mk
@@ -93,6 +93,11 @@ PRODUCT_PACKAGES += \
smc_pa_ctrl \
tf_daemon
+# Sensors
+PRODUCT_PACKAGES += \
+ sensors.piranha \
+ geomagneticd
+
# Filesystem management tools
PRODUCT_PACKAGES += \
static_busybox \
diff --git a/sensors/Android.mk b/sensors/Android.mk
new file mode 100644
index 0000000..a0a9b83
--- /dev/null
+++ b/sensors/Android.mk
@@ -0,0 +1,46 @@
+#
+# Copyright (C) 2013 Paul Kocialkowski
+#
+# 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 := \
+ piranha_sensors.c \
+ input.c \
+ bma250.c \
+ yas530c.c \
+ yas_orientation.c \
+ bh1721.c \
+ gp2a_light.c \
+ gp2a_proximity.c
+
+LOCAL_SHARED_LIBRARIES := libutils libcutils liblog libhardware
+LOCAL_PRELINK_MODULE := false
+
+LOCAL_MODULE := sensors.piranha
+LOCAL_MODULE_PATH := $(TARGET_OUT_SHARED_LIBRARIES)/hw
+LOCAL_MODULE_TAGS := optional
+
+ifeq ($(TARGET_DEVICE),p5100)
+ LOCAL_CFLAGS += -DTARGET_DEVICE_P5100
+endif
+ifeq ($(TARGET_DEVICE),p3100)
+ LOCAL_CFLAGS += -DTARGET_DEVICE_P3100
+endif
+
+include $(BUILD_SHARED_LIBRARY)
diff --git a/sensors/bh1721.c b/sensors/bh1721.c
new file mode 100644
index 0000000..6b0e76e
--- /dev/null
+++ b/sensors/bh1721.c
@@ -0,0 +1,241 @@
+/*
+ * Copyright (C) 2013 Paul Kocialkowski
+ *
+ * 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 <linux/ioctl.h>
+#include <linux/input.h>
+
+#include <hardware/sensors.h>
+#include <hardware/hardware.h>
+
+#define LOG_TAG "piranha_sensors"
+#include <utils/Log.h>
+
+#include "piranha_sensors.h"
+
+struct bh1721_data {
+ char path_enable[PATH_MAX];
+ char path_delay[PATH_MAX];
+};
+
+int bh1721_init(struct piranha_sensors_handlers *handlers, struct piranha_sensors_device *device)
+{
+ struct bh1721_data *data = NULL;
+ char path[PATH_MAX] = { 0 };
+ int input_fd = -1;
+ int rc;
+
+ LOGD("%s(%p, %p)", __func__, handlers, device);
+
+ if (handlers == NULL)
+ return -EINVAL;
+
+ input_fd = input_open("light_sensor");
+ if (input_fd < 0) {
+ LOGE("%s: Unable to open input", __func__);
+ goto error;
+ }
+
+ rc = sysfs_path_prefix("light_sensor", (char *) &path);
+ if (rc < 0 || path[0] == '\0') {
+ LOGE("%s: Unable to open sysfs", __func__);
+ goto error;
+ }
+
+ data = (struct bh1721_data *) calloc(1, sizeof(struct bh1721_data));
+
+ 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 (input_fd >= 0)
+ close(input_fd);
+
+ if (data != NULL)
+ free(data);
+
+ handlers->poll_fd = -1;
+ handlers->data = NULL;
+
+ return -1;
+}
+
+int bh1721_deinit(struct piranha_sensors_handlers *handlers)
+{
+ int input_fd;
+
+ LOGD("%s(%p)", __func__, handlers);
+
+ if (handlers == NULL)
+ return -EINVAL;
+
+ input_fd = handlers->poll_fd;
+ if (input_fd >= 0)
+ close(input_fd);
+
+ handlers->poll_fd = -1;
+
+ if (handlers->data != NULL)
+ free(handlers->data);
+
+ handlers->data = NULL;
+
+ return 0;
+}
+
+int bh1721_activate(struct piranha_sensors_handlers *handlers)
+{
+ struct bh1721_data *data;
+ char enable[] = "1\n";
+ int fd;
+
+ LOGD("%s(%p)", __func__, handlers);
+
+ if (handlers == NULL || handlers->data == NULL)
+ return -EINVAL;
+
+ data = (struct bh1721_data *) handlers->data;
+
+ fd = open(data->path_enable, O_WRONLY);
+ if (fd < 0) {
+ LOGE("%s: Unable to open enable path", __func__);
+ return -1;
+ }
+
+ write(fd, &enable, sizeof(enable));
+ close(fd);
+
+ handlers->activated = 1;
+
+ return 0;
+}
+
+int bh1721_deactivate(struct piranha_sensors_handlers *handlers)
+{
+ struct bh1721_data *data;
+ char enable[] = "0\n";
+ int fd;
+
+ LOGD("%s(%p)", __func__, handlers);
+
+ if (handlers == NULL || handlers->data == NULL)
+ return -EINVAL;
+
+ data = (struct bh1721_data *) handlers->data;
+
+ fd = open(data->path_enable, O_WRONLY);
+ if (fd < 0) {
+ LOGE("%s: Unable to open enable path", __func__);
+ return -1;
+ }
+
+ write(fd, &enable, sizeof(enable));
+ close(fd);
+
+ handlers->activated = 0;
+
+ return 0;
+}
+
+int bh1721_set_delay(struct piranha_sensors_handlers *handlers, int64_t delay)
+{
+ struct bh1721_data *data;
+ char *value = NULL;
+ int c;
+ int fd;
+
+// LOGD("%s(%p, %ld)", __func__, handlers, (long int) delay);
+
+ if (handlers == NULL || handlers->data == NULL)
+ return -EINVAL;
+
+ data = (struct bh1721_data *) handlers->data;
+
+ c = asprintf(&value, "%ld\n", (long int) delay);
+
+ fd = open(data->path_delay, O_WRONLY);
+ if (fd < 0) {
+ LOGE("%s: Unable to open delay path", __func__);
+ return -1;
+ }
+
+ write(fd, value, c);
+ close(fd);
+
+ if (value != NULL)
+ free(value);
+
+ return 0;
+}
+
+float bh1721_light(int value)
+{
+ return (float) value * 0.712f;
+}
+
+int bh1721_get_data(struct piranha_sensors_handlers *handlers,
+ struct sensors_event_t *event)
+{
+ struct input_event input_event;
+ int input_fd;
+ int rc;
+
+ if (handlers == NULL || event == NULL)
+ return -EINVAL;
+
+ input_fd = handlers->poll_fd;
+ if (input_fd < 0)
+ return -EINVAL;
+
+ rc = read(input_fd, &input_event, sizeof(input_event));
+ if (rc < (int) sizeof(input_event))
+ return -EINVAL;
+
+ if (input_event.type != EV_ABS || input_event.code != ABS_MISC)
+ return -1;
+
+ event->version = sizeof(struct sensors_event_t);
+ event->sensor = handlers->handle;
+ event->type = handlers->handle;
+ event->timestamp = input_timestamp(&input_event);
+ event->light = bh1721_light(input_event.value);
+
+ return 0;
+}
+
+struct piranha_sensors_handlers bh1721 = {
+ .name = "BH1721",
+ .handle = SENSOR_TYPE_LIGHT,
+ .init = bh1721_init,
+ .deinit = bh1721_deinit,
+ .activate = bh1721_activate,
+ .deactivate = bh1721_deactivate,
+ .set_delay = bh1721_set_delay,
+ .get_data = bh1721_get_data,
+ .activated = 0,
+ .poll_fd = -1,
+ .data = NULL,
+};
diff --git a/sensors/bma250.c b/sensors/bma250.c
new file mode 100644
index 0000000..09d2f84
--- /dev/null
+++ b/sensors/bma250.c
@@ -0,0 +1,293 @@
+/*
+ * Copyright (C) 2013 Paul Kocialkowski
+ *
+ * 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 <linux/ioctl.h>
+#include <linux/input.h>
+
+#include <hardware/sensors.h>
+#include <hardware/hardware.h>
+
+#define LOG_TAG "piranha_sensors"
+#include <utils/Log.h>
+
+#include "piranha_sensors.h"
+
+#define FLAG_X (1 << 0)
+#define FLAG_Y (1 << 1)
+#define FLAG_Z (1 << 2)
+#define FLAG_ALL (FLAG_X | FLAG_Y | FLAG_Z)
+
+struct bma250_data {
+ char path_enable[PATH_MAX];
+ char path_delay[PATH_MAX];
+
+ sensors_vec_t acceleration;
+};
+
+int bma250_init(struct piranha_sensors_handlers *handlers, struct piranha_sensors_device *device)
+{
+ struct bma250_data *data = NULL;
+ char path[PATH_MAX] = { 0 };
+ int input_fd = -1;
+ int rc;
+
+ LOGD("%s(%p, %p)", __func__, handlers, device);
+
+ if (handlers == NULL)
+ return -EINVAL;
+
+ input_fd = input_open("accelerometer");
+ if (input_fd < 0) {
+ LOGE("%s: Unable to open input", __func__);
+ goto error;
+ }
+
+ rc = sysfs_path_prefix("accelerometer", (char *) &path);
+ if (rc < 0 || path[0] == '\0') {
+ LOGE("%s: Unable to open sysfs", __func__);
+ goto error;
+ }
+
+ data = (struct bma250_data *) calloc(1, sizeof(struct bma250_data));
+
+ snprintf(data->path_enable, PATH_MAX, "%s/enable", path);
+ snprintf(data->path_delay, PATH_MAX, "%s/delay", path);
+
+ handlers->poll_fd = input_fd;
+ handlers->data = (void *) data;
+
+ return 0;
+
+error:
+ if (input_fd >= 0)
+ close(input_fd);
+
+ if (data != NULL)
+ free(data);
+
+ handlers->poll_fd = -1;
+ handlers->data = NULL;
+
+ return -1;
+}
+
+int bma250_deinit(struct piranha_sensors_handlers *handlers)
+{
+ int input_fd;
+
+ LOGD("%s(%p)", __func__, handlers);
+
+ if (handlers == NULL)
+ return -EINVAL;
+
+ input_fd = handlers->poll_fd;
+ if (input_fd >= 0)
+ close(input_fd);
+
+ handlers->poll_fd = -1;
+
+ if (handlers->data != NULL)
+ free(handlers->data);
+
+ handlers->data = NULL;
+
+ return 0;
+}
+
+int bma250_activate(struct piranha_sensors_handlers *handlers)
+{
+ struct bma250_data *data;
+ char enable[] = "1\n";
+ int fd;
+
+ LOGD("%s(%p)", __func__, handlers);
+
+ if (handlers == NULL || handlers->data == NULL)
+ return -EINVAL;
+
+ data = (struct bma250_data *) handlers->data;
+
+ fd = open(data->path_enable, O_WRONLY);
+ if (fd < 0) {
+ LOGE("%s: Unable to open enable path", __func__);
+ return -1;
+ }
+
+ write(fd, &enable, sizeof(enable));
+ close(fd);
+
+ handlers->activated = 1;
+
+ return 0;
+}
+
+int bma250_deactivate(struct piranha_sensors_handlers *handlers)
+{
+ struct bma250_data *data;
+ char enable[] = "0\n";
+ int fd;
+
+ LOGD("%s(%p)", __func__, handlers);
+
+ if (handlers == NULL || handlers->data == NULL)
+ return -EINVAL;
+
+ data = (struct bma250_data *) handlers->data;
+
+ fd = open(data->path_enable, O_WRONLY);
+ if (fd < 0) {
+ LOGE("%s: Unable to open enable path", __func__);
+ return -1;
+ }
+
+ write(fd, &enable, sizeof(enable));
+ close(fd);
+
+ handlers->activated = 0;
+
+ return 0;
+}
+
+int bma250_set_delay(struct piranha_sensors_handlers *handlers, int64_t delay)
+{
+ struct bma250_data *data;
+ char *value = NULL;
+ int d;
+ int c;
+ int fd;
+
+// LOGD("%s(%p, %ld)", __func__, handlers, (long int) delay);
+
+ if (handlers == NULL || handlers->data == NULL)
+ return -EINVAL;
+
+ data = (struct bma250_data *) handlers->data;
+
+ if (delay < 1000000)
+ d = 0;
+ else
+ d = (int) (delay / 1000000);
+
+ c = asprintf(&value, "%d\n", d);
+
+ fd = open(data->path_delay, O_WRONLY);
+ if (fd < 0) {
+ LOGE("%s: Unable to open delay path", __func__);
+ return -1;
+ }
+
+ write(fd, value, c);
+ close(fd);
+
+ if (value != NULL)
+ free(value);
+
+ return 0;
+}
+
+float bma250_acceleration(int value)
+{
+ return (float) (value * GRAVITY_EARTH) / 256.0f;
+}
+
+int bma250_get_data(struct piranha_sensors_handlers *handlers,
+ struct sensors_event_t *event)
+{
+ struct bma250_data *data;
+ struct input_event input_event;
+ int input_fd;
+ int flag;
+ int rc;
+
+ if (handlers == NULL || handlers->data == NULL || event == NULL)
+ return -EINVAL;
+
+ data = (struct bma250_data *) handlers->data;
+
+ input_fd = handlers->poll_fd;
+ if (input_fd < 0)
+ return -EINVAL;
+
+ event->version = sizeof(struct sensors_event_t);
+ event->sensor = handlers->handle;
+ event->type = handlers->handle;
+
+ event->acceleration.x = data->acceleration.x;
+ event->acceleration.y = data->acceleration.y;
+ event->acceleration.z = data->acceleration.z;
+ event->acceleration.status = SENSOR_STATUS_ACCURACY_MEDIUM;
+
+ flag = 0;
+ while ((flag & FLAG_ALL) != FLAG_ALL) {
+ rc = read(input_fd, &input_event, sizeof(input_event));
+ if (rc < (int) sizeof(input_event)) {
+ if (flag & FLAG_ALL)
+ break;
+ else
+ return -EINVAL;
+ }
+
+ if (input_event.type != EV_ABS)
+ continue;
+
+ switch (input_event.code) {
+ case ABS_X:
+ flag |= FLAG_X;
+ event->acceleration.x = bma250_acceleration(input_event.value);
+ break;
+ case ABS_Y:
+ flag |= FLAG_Y;
+ event->acceleration.y = bma250_acceleration(input_event.value);
+ break;
+ case ABS_Z:
+ flag |= FLAG_Z;
+ event->acceleration.z = bma250_acceleration(input_event.value);
+ break;
+ default:
+ continue;
+ }
+ event->timestamp = input_timestamp(&input_event);
+ }
+
+ if (data->acceleration.x != event->acceleration.x)
+ data->acceleration.x = event->acceleration.x;
+ if (data->acceleration.y != event->acceleration.y)
+ data->acceleration.y = event->acceleration.y;
+ if (data->acceleration.z != event->acceleration.z)
+ data->acceleration.z = event->acceleration.z;
+
+ return 0;
+}
+
+struct piranha_sensors_handlers bma250 = {
+ .name = "BMA250",
+ .handle = SENSOR_TYPE_ACCELEROMETER,
+ .init = bma250_init,
+ .deinit = bma250_deinit,
+ .activate = bma250_activate,
+ .deactivate = bma250_deactivate,
+ .set_delay = bma250_set_delay,
+ .get_data = bma250_get_data,
+ .activated = 0,
+ .poll_fd = -1,
+ .data = NULL,
+};
diff --git a/sensors/gp2a_light.c b/sensors/gp2a_light.c
new file mode 100644
index 0000000..6f6c79b
--- /dev/null
+++ b/sensors/gp2a_light.c
@@ -0,0 +1,242 @@
+/*
+ * Copyright (C) 2013 Paul Kocialkowski
+ *
+ * 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 <linux/ioctl.h>
+#include <linux/input.h>
+
+#include <hardware/sensors.h>
+#include <hardware/hardware.h>
+
+#define LOG_TAG "piranha_sensors"
+#include <utils/Log.h>
+
+#include "piranha_sensors.h"
+
+struct gp2a_light_data {
+ char path_enable[PATH_MAX];
+ char path_delay[PATH_MAX];
+};
+
+int gp2a_light_init(struct piranha_sensors_handlers *handlers, struct piranha_sensors_device *device)
+{
+ struct gp2a_light_data *data = NULL;
+ char path[PATH_MAX] = { 0 };
+ int input_fd = -1;
+ int rc;
+
+ LOGD("%s(%p, %p)", __func__, handlers, device);
+
+ if (handlers == NULL)
+ return -EINVAL;
+
+ input_fd = input_open("light_sensor");
+ if (input_fd < 0) {
+ LOGE("%s: Unable to open input", __func__);
+ goto error;
+ }
+
+ rc = sysfs_path_prefix("light_sensor", (char *) &path);
+ if (rc < 0 || path[0] == '\0') {
+ LOGE("%s: Unable to open sysfs", __func__);
+ goto error;
+ }
+
+ data = (struct gp2a_light_data *) calloc(1, sizeof(struct gp2a_light_data));
+
+ 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 (input_fd >= 0)
+ close(input_fd);
+
+ if (data != NULL)
+ free(data);
+
+ handlers->poll_fd = -1;
+ handlers->data = NULL;
+
+ return -1;
+}
+
+int gp2a_light_deinit(struct piranha_sensors_handlers *handlers)
+{
+ int input_fd;
+
+ LOGD("%s(%p)", __func__, handlers);
+
+ if (handlers == NULL)
+ return -EINVAL;
+
+ input_fd = handlers->poll_fd;
+ if (input_fd >= 0)
+ close(input_fd);
+
+ handlers->poll_fd = -1;
+
+ if (handlers->data != NULL)
+ free(handlers->data);
+
+ handlers->data = NULL;
+
+ return 0;
+}
+
+int gp2a_light_activate(struct piranha_sensors_handlers *handlers)
+{
+ struct gp2a_light_data *data;
+ char enable[] = "1\n";
+ int fd;
+
+ LOGD("%s(%p)", __func__, handlers);
+
+ if (handlers == NULL || handlers->data == NULL)
+ return -EINVAL;
+
+ data = (struct gp2a_light_data *) handlers->data;
+
+ fd = open(data->path_enable, O_WRONLY);
+ if (fd < 0) {
+ LOGE("%s: Unable to open enable path", __func__);
+ return -1;
+ }
+
+ write(fd, &enable, sizeof(enable));
+ close(fd);
+
+ handlers->activated = 1;
+
+ return 0;
+}
+
+int gp2a_light_deactivate(struct piranha_sensors_handlers *handlers)
+{
+ struct gp2a_light_data *data;
+ char enable[] = "0\n";
+ int fd;
+
+ LOGD("%s(%p)", __func__, handlers);
+
+ if (handlers == NULL || handlers->data == NULL)
+ return -EINVAL;
+
+ data = (struct gp2a_light_data *) handlers->data;
+
+ fd = open(data->path_enable, O_WRONLY);
+ if (fd < 0) {
+ LOGE("%s: Unable to open enable path", __func__);
+ return -1;
+ }
+
+ write(fd, &enable, sizeof(enable));
+ close(fd);
+
+ handlers->activated = 0;
+
+ return 0;
+}
+
+int gp2a_light_set_delay(struct piranha_sensors_handlers *handlers, int64_t delay)
+{
+ struct gp2a_light_data *data;
+ char *value = NULL;
+ int c;
+ int fd;
+
+// LOGD("%s(%p, %ld)", __func__, handlers, (long int) delay);
+
+ if (handlers == NULL || handlers->data == NULL)
+ return -EINVAL;
+
+ data = (struct gp2a_light_data *) handlers->data;
+
+ c = asprintf(&value, "%ld\n", (long int) delay);
+
+ fd = open(data->path_delay, O_WRONLY);
+ if (fd < 0) {
+ LOGE("%s: Unable to open delay path", __func__);
+ return -1;
+ }
+
+ write(fd, value, c);
+ close(fd);
+
+ if (value != NULL)
+ free(value);
+
+ return 0;
+}
+
+float gp2a_light_light(int value)
+{
+ return (float) powf(10, value * (125.0f / 1023.0f / 24.0f)) * 4;
+}
+
+int gp2a_light_get_data(struct piranha_sensors_handlers *handlers,
+ struct sensors_event_t *event)
+{
+ struct input_event input_event;
+ int input_fd;
+ int rc;
+
+ if (handlers == NULL || event == NULL)
+ return -EINVAL;
+
+ input_fd = handlers->poll_fd;
+ if (input_fd < 0)
+ return -EINVAL;
+
+ rc = read(input_fd, &input_event, sizeof(input_event));
+ if (rc < (int) sizeof(input_event))
+ return -EINVAL;
+
+ if (input_event.type != EV_ABS || input_event.code != ABS_MISC)
+ return -1;
+
+ event->version = sizeof(struct sensors_event_t);
+ event->sensor = handlers->handle;
+ event->type = handlers->handle;
+ event->timestamp = input_timestamp(&input_event);
+ event->light = gp2a_light_light(input_event.value);
+
+ return 0;
+}
+
+struct piranha_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,
+ .poll_fd = -1,
+ .data = NULL,
+};
diff --git a/sensors/gp2a_proximity.c b/sensors/gp2a_proximity.c
new file mode 100644
index 0000000..65dd4d7
--- /dev/null
+++ b/sensors/gp2a_proximity.c
@@ -0,0 +1,224 @@
+/*
+ * Copyright (C) 2013 Paul Kocialkowski
+ *
+ * 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 <linux/ioctl.h>
+#include <linux/input.h>
+
+#include <hardware/sensors.h>
+#include <hardware/hardware.h>
+
+#define LOG_TAG "piranha_sensors"
+#include <utils/Log.h>
+
+#include "piranha_sensors.h"
+
+struct gp2a_proximity_data {
+ char path_enable[PATH_MAX];
+};
+
+int gp2a_proximity_init(struct piranha_sensors_handlers *handlers, struct piranha_sensors_device *device)
+{
+ struct gp2a_proximity_data *data = NULL;
+ char path[PATH_MAX] = { 0 };
+ int input_fd = -1;
+ int rc;
+
+ LOGD("%s(%p, %p)", __func__, handlers, device);
+
+ if (handlers == NULL)
+ return -EINVAL;
+
+ input_fd = input_open("proximity_sensor");
+ if (input_fd < 0) {
+ LOGE("%s: Unable to open input", __func__);
+ goto error;
+ }
+
+ rc = sysfs_path_prefix("proximity_sensor", (char *) &path);
+ if (rc < 0 || path[0] == '\0') {
+ LOGE("%s: Unable to open sysfs", __func__);
+ goto error;
+ }
+
+ data = (struct gp2a_proximity_data *) calloc(1, sizeof(struct gp2a_proximity_data));
+
+ snprintf(data->path_enable, PATH_MAX, "%s/enable", path);
+
+ handlers->poll_fd = input_fd;
+ handlers->data = (void *) data;
+
+ return 0;
+
+error:
+ if (input_fd >= 0)
+ close(input_fd);
+
+ if (data != NULL)
+ free(data);
+
+ handlers->poll_fd = -1;
+ handlers->data = NULL;
+
+ return -1;
+}
+
+int gp2a_proximity_deinit(struct piranha_sensors_handlers *handlers)
+{
+ int input_fd;
+
+ LOGD("%s(%p)", __func__, handlers);
+
+ if (handlers == NULL)
+ return -EINVAL;
+
+ input_fd = handlers->poll_fd;
+ if (input_fd >= 0)
+ close(input_fd);
+
+ handlers->poll_fd = -1;
+
+ if (handlers->data != NULL)
+ free(handlers->data);
+
+ handlers->data = NULL;
+
+ return 0;
+}
+
+int gp2a_proximity_activate(struct piranha_sensors_handlers *handlers)
+{
+ struct gp2a_proximity_data *data;
+ char enable[] = "1\n";
+ int fd;
+
+ LOGD("%s(%p)", __func__, handlers);
+
+ if (handlers == NULL || handlers->data == NULL)
+ return -EINVAL;
+
+ data = (struct gp2a_proximity_data *) handlers->data;
+
+ fd = open(data->path_enable, O_WRONLY);
+ if (fd < 0) {
+ LOGE("%s: Unable to open enable path", __func__);
+ return -1;
+ }
+
+ write(fd, &enable, sizeof(enable));
+ close(fd);
+
+ handlers->activated = 1;
+
+ return 0;
+}
+
+int gp2a_proximity_deactivate(struct piranha_sensors_handlers *handlers)
+{
+ struct gp2a_proximity_data *data;
+ char enable[] = "0\n";
+ int fd;
+
+ LOGD("%s(%p)", __func__, handlers);
+
+ if (handlers == NULL || handlers->data == NULL)
+ return -EINVAL;
+
+ data = (struct gp2a_proximity_data *) handlers->data;
+
+ fd = open(data->path_enable, O_WRONLY);
+ if (fd < 0) {
+ LOGE("%s: Unable to open enable path", __func__);
+ return -1;
+ }
+
+ write(fd, &enable, sizeof(enable));
+ close(fd);
+
+ handlers->activated = 0;
+
+ return 0;
+}
+
+int gp2a_proximity_set_delay(struct piranha_sensors_handlers *handlers, int64_t delay)
+{
+ struct gp2a_proximity_data *data;
+ char *value = NULL;
+ int c;
+ int fd;
+
+// LOGD("%s(%p, %ld)", __func__, handlers, (long int) delay);
+
+ if (handlers == NULL || handlers->data == NULL)
+ return -EINVAL;
+
+ return 0;
+}
+
+float gp2a_proximity_proximity(int value)
+{
+ return (float) value * 5.0f;
+}
+
+int gp2a_proximity_get_data(struct piranha_sensors_handlers *handlers,
+ struct sensors_event_t *event)
+{
+ struct input_event input_event;
+ int input_fd;
+ int rc;
+
+ if (handlers == NULL || event == NULL)
+ return -EINVAL;
+
+ input_fd = handlers->poll_fd;
+ if (input_fd < 0)
+ return -EINVAL;
+
+ rc = read(input_fd, &input_event, sizeof(input_event));
+ if (rc < (int) sizeof(input_event))
+ return -EINVAL;
+
+ if (input_event.type != EV_ABS || input_event.code != ABS_DISTANCE)
+ return -1;
+
+ event->version = sizeof(struct sensors_event_t);
+ event->sensor = handlers->handle;
+ event->type = handlers->handle;
+ event->timestamp = input_timestamp(&input_event);
+ event->distance = gp2a_proximity_proximity(input_event.value);
+
+ return 0;
+}
+
+struct piranha_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,
+ .poll_fd = -1,
+ .data = NULL,
+};
diff --git a/sensors/input.c b/sensors/input.c
new file mode 100644
index 0000000..da3bd99
--- /dev/null
+++ b/sensors/input.c
@@ -0,0 +1,125 @@
+/*
+ * Copyright (C) 2013 Paul Kocialkowski
+ *
+ * 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>
+
+#define LOG_TAG "piranha_sensors"
+#include <utils/Log.h>
+
+#include "piranha_sensors.h"
+
+int64_t input_timestamp(struct input_event *event)
+{
+ if (event == NULL)
+ return -1;
+
+ return event->time.tv_sec*1000000000LL + event->time.tv_usec*1000;
+}
+
+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;
+}
diff --git a/sensors/piranha_sensors.c b/sensors/piranha_sensors.c
new file mode 100644
index 0000000..c1fd534
--- /dev/null
+++ b/sensors/piranha_sensors.c
@@ -0,0 +1,283 @@
+/*
+ * Copyright (C) 2013 Paul Kocialkowski
+ *
+ * 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 <hardware/sensors.h>
+#include <hardware/hardware.h>
+
+#define LOG_TAG "piranha_sensors"
+#include <utils/Log.h>
+
+#include "piranha_sensors.h"
+
+/*
+ * Sensors list
+ */
+
+struct sensor_t piranha_sensors[] = {
+ { "BMA254 Acceleration Sensor", "Bosch", 1, SENSOR_TYPE_ACCELEROMETER,
+ SENSOR_TYPE_ACCELEROMETER, 19.6f, 0.0383f, 0.13f, 10000, {}, },
+ { "YAS530 Magnetic Sensor", "Yamaha", 1, SENSOR_TYPE_MAGNETIC_FIELD,
+ SENSOR_TYPE_MAGNETIC_FIELD, 800.0f, 0.3f, 4.0f, 10000, {}, },
+ { "YAS Orientation Sensor", "Yamaha", 1, SENSOR_TYPE_ORIENTATION,
+ SENSOR_TYPE_ORIENTATION, 360.0f, 0.1f, 0.0f, 10000, {}, },
+#ifdef TARGET_DEVICE_P5100
+ { "BH1721 Light Sensor", "ROHM", 1, SENSOR_TYPE_LIGHT,
+ SENSOR_TYPE_LIGHT, 0.0f, 0.0f, 0.0f, 0, {}, },
+#endif
+#ifdef TARGET_DEVICE_P3100
+ { "GP2A Light Sensor", "SHARP", 1, SENSOR_TYPE_LIGHT,
+ SENSOR_TYPE_LIGHT, 0.0f, 0.0f, 0.0f, 0, {}, },
+ { "GP2A Proximity Sensor", "SHARP", 1, SENSOR_TYPE_PROXIMITY,
+ SENSOR_TYPE_PROXIMITY, 5.0f, 0.0f, 0.0f, 0, {}, },
+#endif
+};
+
+int piranha_sensors_count = sizeof(piranha_sensors) / sizeof(struct sensor_t);
+
+struct piranha_sensors_handlers *piranha_sensors_handlers[] = {
+ &bma250,
+ &yas530c,
+ &yas_orientation,
+#ifdef TARGET_DEVICE_P5100
+ &bh1721,
+#endif
+#ifdef TARGET_DEVICE_P3100
+ &gp2a_light,
+ &gp2a_proximity,
+#endif
+};
+
+int piranha_sensors_handlers_count = sizeof(piranha_sensors_handlers) /
+ sizeof(struct piranha_sensors_handlers *);
+
+/*
+ * Piranha Sensors
+ */
+
+int piranha_sensors_activate(struct sensors_poll_device_t *dev, int handle, int enabled)
+{
+ struct piranha_sensors_device *device;
+ int i;
+
+ LOGD("%s(%p, %d, %d)", __func__, dev, handle, enabled);
+
+ if (dev == NULL)
+ return -EINVAL;
+
+ device = (struct piranha_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)
+ return device->handlers[i]->activate(device->handlers[i]);
+ else if (!enabled && device->handlers[i]->deactivate != NULL)
+ return device->handlers[i]->deactivate(device->handlers[i]);
+ }
+ }
+
+ return -1;
+}
+
+int piranha_sensors_set_delay(struct sensors_poll_device_t *dev, int handle, int64_t ns)
+{
+ struct piranha_sensors_device *device;
+ int i;
+
+ LOGD("%s(%p, %d, %ld)", __func__, dev, handle, (long int) ns);
+
+ if (dev == NULL)
+ return -EINVAL;
+
+ device = (struct piranha_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], ns);
+ }
+
+ return 0;
+}
+
+int piranha_sensors_poll(struct sensors_poll_device_t *dev,
+ struct sensors_event_t* data, int count)
+{
+ struct piranha_sensors_device *device;
+ int i, j;
+ int c, n;
+ int rc;
+
+// LOGD("%s(%p, %p, %d)", __func__, dev, data, count);
+
+ if (dev == NULL)
+ return -EINVAL;
+
+ device = (struct piranha_sensors_device *) dev;
+
+ if (device->handlers == NULL || device->handlers_count <= 0 ||
+ device->poll_fds == NULL || device->poll_fds_count <= 0)
+ return -EINVAL;
+
+ n = 0;
+
+ 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;
+ } else {
+ n++;
+ count--;
+ }
+ }
+ }
+
+ rc = poll(device->poll_fds, device->poll_fds_count, n > 0 ? 0 : -1);
+ if (rc < 0)
+ return -1;
+
+ return n;
+}
+
+/*
+ * Interface
+ */
+
+int piranha_sensors_close(hw_device_t *device)
+{
+ struct piranha_sensors_device *piranha_sensors_device;
+ int i;
+
+ LOGD("%s(%p)", __func__, device);
+
+ if (device == NULL)
+ return -EINVAL;
+
+ piranha_sensors_device = (struct piranha_sensors_device *) device;
+
+ if (piranha_sensors_device->poll_fds != NULL)
+ free(piranha_sensors_device->poll_fds);
+
+ for (i=0 ; i < piranha_sensors_device->handlers_count ; i++) {
+ if (piranha_sensors_device->handlers[i] == NULL || piranha_sensors_device->handlers[i]->deinit == NULL)
+ continue;
+
+ piranha_sensors_device->handlers[i]->deinit(piranha_sensors_device->handlers[i]);
+ }
+
+ free(device);
+
+ return 0;
+}
+
+int piranha_sensors_open(const struct hw_module_t* module, const char *id,
+ struct hw_device_t** device)
+{
+ struct piranha_sensors_device *piranha_sensors_device;
+ int p, i;
+
+ LOGD("%s(%p, %s, %p)", __func__, module, id, device);
+
+ if (module == NULL || device == NULL)
+ return -EINVAL;
+
+ piranha_sensors_device = (struct piranha_sensors_device *)
+ calloc(1, sizeof(struct piranha_sensors_device));
+ piranha_sensors_device->device.common.tag = HARDWARE_DEVICE_TAG;
+ piranha_sensors_device->device.common.version = 0;
+ piranha_sensors_device->device.common.module = (struct hw_module_t *) module;
+ piranha_sensors_device->device.common.close = piranha_sensors_close;
+ piranha_sensors_device->device.activate = piranha_sensors_activate;
+ piranha_sensors_device->device.setDelay = piranha_sensors_set_delay;
+ piranha_sensors_device->device.poll = piranha_sensors_poll;
+ piranha_sensors_device->handlers = piranha_sensors_handlers;
+ piranha_sensors_device->handlers_count = piranha_sensors_handlers_count;
+ piranha_sensors_device->poll_fds = (struct pollfd *)
+ calloc(1, piranha_sensors_handlers_count * sizeof(struct pollfd));
+
+ p = 0;
+ for (i=0 ; i < piranha_sensors_handlers_count ; i++) {
+ if (piranha_sensors_handlers[i] == NULL || piranha_sensors_handlers[i]->init == NULL)
+ continue;
+
+ piranha_sensors_handlers[i]->init(piranha_sensors_handlers[i], piranha_sensors_device);
+ if (piranha_sensors_handlers[i]->poll_fd >= 0) {
+ piranha_sensors_device->poll_fds[p].fd = piranha_sensors_handlers[i]->poll_fd;
+ piranha_sensors_device->poll_fds[p].events = POLLIN;
+ p++;
+ }
+ }
+
+ piranha_sensors_device->poll_fds_count = p;
+
+ *device = &(piranha_sensors_device->device.common);
+
+ return 0;
+}
+
+int piranha_sensors_get_sensors_list(struct sensors_module_t* module,
+ const struct sensor_t **sensors_p)
+{
+ LOGD("%s(%p, %p)", __func__, module, sensors_p);
+
+ if (sensors_p == NULL)
+ return -EINVAL;
+
+ *sensors_p = piranha_sensors;
+ return piranha_sensors_count;
+}
+
+struct hw_module_methods_t piranha_sensors_module_methods = {
+ .open = piranha_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 = "Piranha Sensors",
+ .author = "Paul Kocialkowski",
+ .methods = &piranha_sensors_module_methods,
+ },
+ .get_sensors_list = piranha_sensors_get_sensors_list,
+};
diff --git a/sensors/piranha_sensors.h b/sensors/piranha_sensors.h
new file mode 100644
index 0000000..e79f2ce
--- /dev/null
+++ b/sensors/piranha_sensors.h
@@ -0,0 +1,85 @@
+/*
+ * Copyright (C) 2013 Paul Kocialkowski
+ *
+ * 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 _SENSORS_H_
+#define _SENSORS_H_
+
+struct piranha_sensors_device;
+
+struct piranha_sensors_handlers {
+ char *name;
+ int handle;
+
+ int (*init)(struct piranha_sensors_handlers *handlers, struct piranha_sensors_device *device);
+ int (*deinit)(struct piranha_sensors_handlers *handlers);
+ int (*activate)(struct piranha_sensors_handlers *handlers);
+ int (*deactivate)(struct piranha_sensors_handlers *handlers);
+ int (*set_delay)(struct piranha_sensors_handlers *handlers, int64_t delay);
+ int (*get_data)(struct piranha_sensors_handlers *handlers, struct sensors_event_t *event);
+
+ int activated;
+ int poll_fd;
+
+ void *data;
+};
+
+struct piranha_sensors_device {
+ struct sensors_poll_device_t device;
+
+ struct piranha_sensors_handlers **handlers;
+ int handlers_count;
+
+ struct pollfd *poll_fds;
+ int poll_fds_count;
+};
+
+extern struct piranha_sensors_handlers *piranha_sensors_handlers[];
+extern int piranha_sensors_handlers_count;
+
+int piranha_sensors_activate(struct sensors_poll_device_t *dev, int handle, int enabled);
+int piranha_sensors_set_delay(struct sensors_poll_device_t *dev, int handle, int64_t ns);
+int piranha_sensors_poll(struct sensors_poll_device_t *dev,
+ struct sensors_event_t* data, int count);
+
+/*
+ * Input
+ */
+
+int64_t input_timestamp(struct input_event *event);
+int input_open(char *name);
+int sysfs_path_prefix(char *name, char *path_prefix);
+
+/*
+ * Sensors
+ */
+
+extern struct piranha_sensors_handlers bma250;
+extern struct piranha_sensors_handlers yas530c;
+extern struct piranha_sensors_handlers yas_orientation;
+extern struct piranha_sensors_handlers bh1721;
+extern struct piranha_sensors_handlers gp2a_light;
+extern struct piranha_sensors_handlers gp2a_proximity;
+
+#endif
diff --git a/sensors/yas530c.c b/sensors/yas530c.c
new file mode 100644
index 0000000..5cf238d
--- /dev/null
+++ b/sensors/yas530c.c
@@ -0,0 +1,293 @@
+/*
+ * Copyright (C) 2013 Paul Kocialkowski
+ *
+ * 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 <linux/ioctl.h>
+#include <linux/input.h>
+
+#include <hardware/sensors.h>
+#include <hardware/hardware.h>
+
+#define LOG_TAG "piranha_sensors"
+#include <utils/Log.h>
+
+#include "piranha_sensors.h"
+
+#define FLAG_X (1 << 0)
+#define FLAG_Y (1 << 1)
+#define FLAG_Z (1 << 2)
+#define FLAG_ALL (FLAG_X | FLAG_Y | FLAG_Z)
+
+struct yas530c_data {
+ char path_enable[PATH_MAX];
+ char path_delay[PATH_MAX];
+
+ sensors_vec_t magnetic;
+};
+
+int yas530c_init(struct piranha_sensors_handlers *handlers, struct piranha_sensors_device *device)
+{
+ struct yas530c_data *data = NULL;
+ char path[PATH_MAX] = { 0 };
+ int input_fd = -1;
+ int rc;
+
+ LOGD("%s(%p, %p)", __func__, handlers, device);
+
+ if (handlers == NULL)
+ return -EINVAL;
+
+ input_fd = input_open("geomagnetic");
+ if (input_fd < 0) {
+ LOGE("%s: Unable to open input", __func__);
+ goto error;
+ }
+
+ rc = sysfs_path_prefix("geomagnetic", (char *) &path);
+ if (rc < 0 || path[0] == '\0') {
+ LOGE("%s: Unable to open sysfs", __func__);
+ goto error;
+ }
+
+ data = (struct yas530c_data *) calloc(1, sizeof(struct yas530c_data));
+
+ snprintf(data->path_enable, PATH_MAX, "%s/enable", path);
+ snprintf(data->path_delay, PATH_MAX, "%s/delay", path);
+
+ handlers->poll_fd = input_fd;
+ handlers->data = (void *) data;
+
+ return 0;
+
+error:
+ if (input_fd >= 0)
+ close(input_fd);
+
+ if (data != NULL)
+ free(data);
+
+ handlers->poll_fd = -1;
+ handlers->data = NULL;
+
+ return -1;
+}
+
+int yas530c_deinit(struct piranha_sensors_handlers *handlers)
+{
+ int input_fd;
+
+ LOGD("%s(%p)", __func__, handlers);
+
+ if (handlers == NULL)
+ return -EINVAL;
+
+ input_fd = handlers->poll_fd;
+ if (input_fd >= 0)
+ close(input_fd);
+
+ handlers->poll_fd = -1;
+
+ if (handlers->data != NULL)
+ free(handlers->data);
+
+ handlers->data = NULL;
+
+ return 0;
+}
+
+int yas530c_activate(struct piranha_sensors_handlers *handlers)
+{
+ struct yas530c_data *data;
+ char enable[] = "1\n";
+ int fd;
+
+ LOGD("%s(%p)", __func__, handlers);
+
+ if (handlers == NULL || handlers->data == NULL)
+ return -EINVAL;
+
+ data = (struct yas530c_data *) handlers->data;
+
+ fd = open(data->path_enable, O_WRONLY);
+ if (fd < 0) {
+ LOGE("%s: Unable to open enable path", __func__);
+ return -1;
+ }
+
+ write(fd, &enable, sizeof(enable));
+ close(fd);
+
+ handlers->activated = 1;
+
+ return 0;
+}
+
+int yas530c_deactivate(struct piranha_sensors_handlers *handlers)
+{
+ struct yas530c_data *data;
+ char enable[] = "0\n";
+ int fd;
+
+ LOGD("%s(%p)", __func__, handlers);
+
+ if (handlers == NULL || handlers->data == NULL)
+ return -EINVAL;
+
+ data = (struct yas530c_data *) handlers->data;
+
+ fd = open(data->path_enable, O_WRONLY);
+ if (fd < 0) {
+ LOGE("%s: Unable to open enable path", __func__);
+ return -1;
+ }
+
+ write(fd, &enable, sizeof(enable));
+ close(fd);
+
+ handlers->activated = 0;
+
+ return 0;
+}
+
+int yas530c_set_delay(struct piranha_sensors_handlers *handlers, int64_t delay)
+{
+ struct yas530c_data *data;
+ char *value = NULL;
+ int d;
+ int c;
+ int fd;
+
+// LOGD("%s(%p, %ld)", __func__, handlers, (long int) delay);
+
+ if (handlers == NULL || handlers->data == NULL)
+ return -EINVAL;
+
+ data = (struct yas530c_data *) handlers->data;
+
+ if (delay < 1000000)
+ d = 0;
+ else
+ d = (int) (delay / 1000000);
+
+ c = asprintf(&value, "%d\n", d);
+
+ fd = open(data->path_delay, O_WRONLY);
+ if (fd < 0) {
+ LOGE("%s: Unable to open delay path", __func__);
+ return -1;
+ }
+
+ write(fd, value, c);
+ close(fd);
+
+ if (value != NULL)
+ free(value);
+
+ return 0;
+}
+
+float yas530c_magnetic(int value)
+{
+ return (float) value / 1000.0f;
+}
+
+int yas530c_get_data(struct piranha_sensors_handlers *handlers,
+ struct sensors_event_t *event)
+{
+ struct yas530c_data *data;
+ struct input_event input_event;
+ int input_fd;
+ int flag;
+ int rc;
+
+ if (handlers == NULL || handlers->data == NULL || event == NULL)
+ return -EINVAL;
+
+ data = (struct yas530c_data *) handlers->data;
+
+ input_fd = handlers->poll_fd;
+ if (input_fd < 0)
+ return -EINVAL;
+
+ event->version = sizeof(struct sensors_event_t);
+ event->sensor = handlers->handle;
+ event->type = handlers->handle;
+
+ event->magnetic.x = data->magnetic.x;
+ event->magnetic.y = data->magnetic.y;
+ event->magnetic.z = data->magnetic.z;
+ event->magnetic.status = SENSOR_STATUS_ACCURACY_MEDIUM;
+
+ flag = 0;
+ while ((flag & FLAG_ALL) != FLAG_ALL) {
+ rc = read(input_fd, &input_event, sizeof(input_event));
+ if (rc < (int) sizeof(input_event)) {
+ if (flag & FLAG_ALL)
+ break;
+ else
+ return -EINVAL;
+ }
+
+ if (input_event.type != EV_ABS)
+ continue;
+
+ switch (input_event.code) {
+ case ABS_X:
+ flag |= FLAG_X;
+ event->magnetic.x = yas530c_magnetic(input_event.value);
+ break;
+ case ABS_Y:
+ flag |= FLAG_Y;
+ event->magnetic.y = yas530c_magnetic(input_event.value);
+ break;
+ case ABS_Z:
+ flag |= FLAG_Z;
+ event->magnetic.z = yas530c_magnetic(input_event.value);
+ break;
+ default:
+ continue;
+ }
+ event->timestamp = input_timestamp(&input_event);
+ }
+
+ if (data->magnetic.x != event->magnetic.x)
+ data->magnetic.x = event->magnetic.x;
+ if (data->magnetic.y != event->magnetic.y)
+ data->magnetic.y = event->magnetic.y;
+ if (data->magnetic.z != event->magnetic.z)
+ data->magnetic.z = event->magnetic.z;
+
+ return 0;
+}
+
+struct piranha_sensors_handlers yas530c = {
+ .name = "YAS530C",
+ .handle = SENSOR_TYPE_MAGNETIC_FIELD,
+ .init = yas530c_init,
+ .deinit = yas530c_deinit,
+ .activate = yas530c_activate,
+ .deactivate = yas530c_deactivate,
+ .set_delay = yas530c_set_delay,
+ .get_data = yas530c_get_data,
+ .activated = 0,
+ .poll_fd = -1,
+ .data = NULL,
+};
diff --git a/sensors/yas_orientation.c b/sensors/yas_orientation.c
new file mode 100644
index 0000000..5278cf1
--- /dev/null
+++ b/sensors/yas_orientation.c
@@ -0,0 +1,389 @@
+/*
+ * Copyright (C) 2013 Paul Kocialkowski
+ *
+ * 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 <stddef.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <linux/ioctl.h>
+#include <linux/input.h>
+
+#include <hardware/sensors.h>
+#include <hardware/hardware.h>
+
+#define LOG_TAG "piranha_sensors"
+#include <utils/Log.h>
+
+#include "piranha_sensors.h"
+
+#define FLAG_X (1 << 0)
+#define FLAG_Y (1 << 1)
+#define FLAG_Z (1 << 2)
+#define FLAG_ALL (FLAG_X | FLAG_Y | FLAG_Z)
+
+struct yas_orientation_data {
+ struct piranha_sensors_device *device;
+
+ char path_enable[PATH_MAX];
+ char path_delay[PATH_MAX];
+
+ char acc_path_enable[PATH_MAX];
+ char acc_path_delay[PATH_MAX];
+
+ char mag_path_enable[PATH_MAX];
+ char mag_path_delay[PATH_MAX];
+
+ sensors_vec_t orientation;
+};
+
+int yas_orientation_init(struct piranha_sensors_handlers *handlers, struct piranha_sensors_device *device)
+{
+ struct yas_orientation_data *data = NULL;
+ char path[PATH_MAX] = { 0 };
+ int input_fd = -1;
+ int rc;
+
+ LOGD("%s(%p, %p)", __func__, handlers, device);
+
+ if (handlers == NULL || device == NULL)
+ return -EINVAL;
+
+ input_fd = input_open("orientation");
+ if (input_fd < 0) {
+ LOGE("%s: Unable to open input", __func__);
+ goto error;
+ }
+
+ rc = sysfs_path_prefix("orientation", (char *) &path);
+ if (rc < 0 || path[0] == '\0') {
+ LOGE("%s: Unable to open sysfs", __func__);
+ goto error;
+ }
+
+ data = (struct yas_orientation_data *) calloc(1, sizeof(struct yas_orientation_data));
+ data->device = device;
+
+ snprintf(data->path_enable, PATH_MAX, "%s/enable", path);
+ snprintf(data->path_delay, PATH_MAX, "%s/delay", path);
+
+ memset(&path, 0, sizeof(path));
+
+ rc = sysfs_path_prefix("accelerometer", (char *) &path);
+ if (rc < 0 || path[0] == '\0') {
+ LOGE("%s: Unable to open sysfs", __func__);
+ goto error;
+ }
+
+ snprintf(data->acc_path_enable, PATH_MAX, "%s/enable", path);
+ snprintf(data->acc_path_delay, PATH_MAX, "%s/delay", path);
+
+ memset(&path, 0, sizeof(path));
+
+ rc = sysfs_path_prefix("geomagnetic", (char *) &path);
+ if (rc < 0 || path[0] == '\0') {
+ LOGE("%s: Unable to open sysfs", __func__);
+ goto error;
+ }
+
+ snprintf(data->mag_path_enable, PATH_MAX, "%s/enable", path);
+ snprintf(data->mag_path_delay, PATH_MAX, "%s/delay", path);
+
+ handlers->poll_fd = input_fd;
+ handlers->data = (void *) data;
+
+ return 0;
+
+error:
+ if (input_fd >= 0)
+ close(input_fd);
+
+ if (data != NULL)
+ free(data);
+
+ handlers->poll_fd = -1;
+ handlers->data = NULL;
+
+ return -1;
+}
+
+int yas_orientation_deinit(struct piranha_sensors_handlers *handlers)
+{
+ int input_fd;
+
+ LOGD("%s(%p)", __func__, handlers);
+
+ if (handlers == NULL)
+ return -EINVAL;
+
+ input_fd = handlers->poll_fd;
+ if (input_fd >= 0)
+ close(input_fd);
+
+ handlers->poll_fd = -1;
+
+ if (handlers->data != NULL)
+ free(handlers->data);
+
+ handlers->data = NULL;
+
+ return 0;
+}
+
+int yas_orientation_activate(struct piranha_sensors_handlers *handlers)
+{
+ struct yas_orientation_data *data;
+ char enable[] = "1\n";
+ int fd;
+ int rc;
+
+ LOGD("%s(%p)", __func__, handlers);
+
+ if (handlers == NULL || handlers->data == NULL)
+ return -EINVAL;
+
+ data = (struct yas_orientation_data *) handlers->data;
+
+ fd = open(data->acc_path_enable, O_WRONLY);
+ if (fd < 0) {
+ LOGE("%s: Unable to open enable path", __func__);
+ return -1;
+ }
+
+ write(fd, &enable, sizeof(enable));
+ close(fd);
+
+ fd = open(data->mag_path_enable, O_WRONLY);
+ if (fd < 0) {
+ LOGE("%s: Unable to open enable path", __func__);
+ return -1;
+ }
+
+ write(fd, &enable, sizeof(enable));
+ close(fd);
+
+ fd = open(data->path_enable, O_WRONLY);
+ if (fd < 0) {
+ LOGE("%s: Unable to open enable path", __func__);
+ return -1;
+ }
+
+ write(fd, &enable, sizeof(enable));
+ close(fd);
+
+ handlers->activated = 1;
+
+ return 0;
+}
+
+int yas_orientation_deactivate(struct piranha_sensors_handlers *handlers)
+{
+ struct yas_orientation_data *data;
+ char enable[] = "0\n";
+ int fd;
+ int i;
+
+ LOGD("%s(%p)", __func__, handlers);
+
+ if (handlers == NULL || handlers->data == NULL)
+ return -EINVAL;
+
+ data = (struct yas_orientation_data *) handlers->data;
+
+ fd = open(data->path_enable, O_WRONLY);
+ if (fd < 0) {
+ LOGE("%s: Unable to open enable path", __func__);
+ return -1;
+ }
+
+ write(fd, &enable, sizeof(enable));
+ close(fd);
+
+ for (i=0 ; i < data->device->handlers_count ; i++) {
+ if (data->device->handlers[i] == NULL)
+ continue;
+
+ if (data->device->handlers[i]->handle == SENSOR_TYPE_ACCELEROMETER && !data->device->handlers[i]->activated) {
+ fd = open(data->acc_path_enable, O_WRONLY);
+ if (fd < 0) {
+ LOGE("%s: Unable to open enable path", __func__);
+ continue;
+ }
+
+ write(fd, &enable, sizeof(enable));
+ close(fd);
+ } else if (data->device->handlers[i]->handle == SENSOR_TYPE_MAGNETIC_FIELD && !data->device->handlers[i]->activated) {
+ fd = open(data->mag_path_enable, O_WRONLY);
+ if (fd < 0) {
+ LOGE("%s: Unable to open enable path", __func__);
+ continue;
+ }
+
+ write(fd, &enable, sizeof(enable));
+ close(fd);
+ }
+ }
+
+ handlers->activated = 0;
+
+ return 0;
+}
+
+int yas_orientation_set_delay(struct piranha_sensors_handlers *handlers, int64_t delay)
+{
+ struct yas_orientation_data *data;
+ char *value = NULL;
+ int d;
+ int c;
+ int fd;
+ int rc;
+
+// LOGD("%s(%p, %ld)", __func__, handlers, (long int) delay);
+
+ if (handlers == NULL || handlers->data == NULL)
+ return -EINVAL;
+
+ data = (struct yas_orientation_data *) handlers->data;
+
+ if (delay < 1000000)
+ d = 0;
+ else
+ d = (int) (delay / 1000000);
+
+ c = asprintf(&value, "%d\n", d);
+
+ fd = open(data->acc_path_delay, O_WRONLY);
+ if (fd < 0) {
+ LOGE("%s: Unable to open delay path", __func__);
+ return -1;
+ }
+
+ write(fd, value, c);
+ close(fd);
+
+ fd = open(data->mag_path_delay, O_WRONLY);
+ if (fd < 0) {
+ LOGE("%s: Unable to open delay path", __func__);
+ return -1;
+ }
+
+ write(fd, value, c);
+ close(fd);
+
+ fd = open(data->path_delay, O_WRONLY);
+ if (fd < 0) {
+ LOGE("%s: Unable to open delay path", __func__);
+ return -1;
+ }
+
+ write(fd, value, c);
+ close(fd);
+
+ if (value != NULL)
+ free(value);
+
+ return 0;
+}
+
+float yas_orientation_orientation(int value)
+{
+ return (float) value / 1000.f;
+}
+
+int yas_orientation_get_data(struct piranha_sensors_handlers *handlers,
+ struct sensors_event_t *event)
+{
+ struct yas_orientation_data *data;
+ struct input_event input_event;
+ int input_fd;
+ int flag;
+ int rc;
+
+ if (handlers == NULL || handlers->data == NULL || event == NULL)
+ return -EINVAL;
+
+ data = (struct yas_orientation_data *) handlers->data;
+
+ input_fd = handlers->poll_fd;
+ if (input_fd < 0)
+ return -EINVAL;
+
+ event->version = sizeof(struct sensors_event_t);
+ event->sensor = handlers->handle;
+ event->type = handlers->handle;
+
+ event->orientation.x = data->orientation.x;
+ event->orientation.y = data->orientation.y;
+ event->orientation.z = data->orientation.z;
+ event->orientation.status = SENSOR_STATUS_ACCURACY_MEDIUM;
+
+ flag = 0;
+ while ((flag & FLAG_ALL) != FLAG_ALL) {
+ rc = read(input_fd, &input_event, sizeof(input_event));
+ if (rc < (int) sizeof(input_event)) {
+ if (flag & FLAG_ALL)
+ break;
+ else
+ return -EINVAL;
+ }
+
+ if (input_event.type != EV_ABS)
+ continue;
+
+ switch (input_event.code) {
+ case ABS_X:
+ flag |= FLAG_X;
+ event->orientation.x = yas_orientation_orientation(input_event.value);
+ break;
+ case ABS_Y:
+ flag |= FLAG_Y;
+ event->orientation.y = yas_orientation_orientation(input_event.value);
+ break;
+ case ABS_Z:
+ flag |= FLAG_Z;
+ event->orientation.z = yas_orientation_orientation(input_event.value);
+ break;
+ default:
+ continue;
+ }
+ event->timestamp = input_timestamp(&input_event);
+ }
+
+ if (data->orientation.x != event->orientation.x)
+ data->orientation.x = event->orientation.x;
+ if (data->orientation.y != event->orientation.y)
+ data->orientation.y = event->orientation.y;
+ if (data->orientation.z != event->orientation.z)
+ data->orientation.z = event->orientation.z;
+
+ return 0;
+}
+
+struct piranha_sensors_handlers yas_orientation = {
+ .name = "YAS Orientation",
+ .handle = SENSOR_TYPE_ORIENTATION,
+ .init = yas_orientation_init,
+ .deinit = yas_orientation_deinit,
+ .activate = yas_orientation_activate,
+ .deactivate = yas_orientation_deactivate,
+ .set_delay = yas_orientation_set_delay,
+ .get_data = yas_orientation_get_data,
+ .activated = 0,
+ .poll_fd = -1,
+ .data = NULL,
+};