diff options
author | Paul Kocialkowski <contact@paulk.fr> | 2013-09-06 09:58:44 -0400 |
---|---|---|
committer | Andreas Blaesius <skate4life@gmx.de> | 2015-08-15 14:20:53 -0700 |
commit | c9541c3d21b7f1ada9c37fb65d9abace51cdcdc5 (patch) | |
tree | 4c9a80db17a92282ff88258ec3dfd333cae610a7 | |
parent | 6a407b3a77615fe3895e165106b7cc5bf1c84a7c (diff) | |
download | device_samsung_espressowifi-c9541c3d21b7f1ada9c37fb65d9abace51cdcdc5.zip device_samsung_espressowifi-c9541c3d21b7f1ada9c37fb65d9abace51cdcdc5.tar.gz device_samsung_espressowifi-c9541c3d21b7f1ada9c37fb65d9abace51cdcdc5.tar.bz2 |
espresso-common: add opensource sensor hal
Change-Id: I45912eb1b0820d4045704fdfa77769dd26b6b22c
-rw-r--r-- | espresso-common.mk | 5 | ||||
-rw-r--r-- | libsensors/Android.mk | 85 | ||||
-rw-r--r-- | libsensors/bh1721.c | 241 | ||||
-rw-r--r-- | libsensors/bma250.c | 293 | ||||
-rw-r--r-- | libsensors/geomagneticd/geomagneticd.c | 368 | ||||
-rw-r--r-- | libsensors/gp2a_light.c | 242 | ||||
-rw-r--r-- | libsensors/gp2a_proximity.c | 224 | ||||
-rw-r--r-- | libsensors/input.c | 125 | ||||
-rw-r--r-- | libsensors/orientationd/input.c | 125 | ||||
-rw-r--r-- | libsensors/orientationd/orientationd.c | 372 | ||||
-rw-r--r-- | libsensors/orientationd/orientationd.h | 50 | ||||
-rw-r--r-- | libsensors/orientationd/vector.c | 61 | ||||
-rw-r--r-- | libsensors/piranha_sensors.c | 289 | ||||
-rw-r--r-- | libsensors/piranha_sensors.h | 87 | ||||
-rw-r--r-- | libsensors/yas530c.c | 293 | ||||
-rw-r--r-- | libsensors/yas_orientation.c | 389 |
16 files changed, 3248 insertions, 1 deletions
diff --git a/espresso-common.mk b/espresso-common.mk index 0b54a03..0ec2e73 100644 --- a/espresso-common.mk +++ b/espresso-common.mk @@ -65,7 +65,10 @@ PRODUCT_PACKAGES += \ hwcomposer.piranha \ lights.piranha \ libinvensense_mpl \ - power.piranha + power.piranha \ + sensors.piranha \ + geomagneticd \ + orientationd # F2FS filesystem PRODUCT_PACKAGES += \ diff --git a/libsensors/Android.mk b/libsensors/Android.mk new file mode 100644 index 0000000..45edc5f --- /dev/null +++ b/libsensors/Android.mk @@ -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/>. +# + +LOCAL_PATH := $(call my-dir) +PIRANHA_SENSORS_PATH := $(LOCAL_PATH) + +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),p5110) + LOCAL_CFLAGS += -DTARGET_DEVICE_P5100 +endif +ifeq ($(TARGET_DEVICE),p3100) + LOCAL_CFLAGS += -DTARGET_DEVICE_P3100 +endif +ifeq ($(TARGET_DEVICE),p3110) + LOCAL_CFLAGS += -DTARGET_DEVICE_P3100 +endif + +include $(BUILD_SHARED_LIBRARY) + +LOCAL_PATH := $(PIRANHA_SENSORS_PATH)/geomagneticd + +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) + +LOCAL_PATH := $(PIRANHA_SENSORS_PATH)/orientationd + +include $(CLEAR_VARS) + +LOCAL_SRC_FILES := \ + input.c \ + vector.c \ + orientationd.c + +LOCAL_SHARED_LIBRARIES := libutils libcutils liblog +LOCAL_PRELINK_MODULE := false + +LOCAL_MODULE := orientationd +LOCAL_MODULE_TAGS := optional + +include $(BUILD_EXECUTABLE) diff --git a/libsensors/bh1721.c b/libsensors/bh1721.c new file mode 100644 index 0000000..1486ef9 --- /dev/null +++ b/libsensors/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; + + ALOGD("%s(%p, %p)", __func__, handlers, device); + + if (handlers == NULL) + return -EINVAL; + + input_fd = input_open("light_sensor"); + if (input_fd < 0) { + ALOGE("%s: Unable to open input", __func__); + goto error; + } + + rc = sysfs_path_prefix("light_sensor", (char *) &path); + if (rc < 0 || path[0] == '\0') { + ALOGE("%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; + + ALOGD("%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; + + ALOGD("%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) { + ALOGE("%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; + + ALOGD("%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) { + ALOGE("%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; + +// ALOGD("%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) { + ALOGE("%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/libsensors/bma250.c b/libsensors/bma250.c new file mode 100644 index 0000000..ce851d7 --- /dev/null +++ b/libsensors/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; + + ALOGD("%s(%p, %p)", __func__, handlers, device); + + if (handlers == NULL) + return -EINVAL; + + input_fd = input_open("accelerometer"); + if (input_fd < 0) { + ALOGE("%s: Unable to open input", __func__); + goto error; + } + + rc = sysfs_path_prefix("accelerometer", (char *) &path); + if (rc < 0 || path[0] == '\0') { + ALOGE("%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; + + ALOGD("%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; + + ALOGD("%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) { + ALOGE("%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; + + ALOGD("%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) { + ALOGE("%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; + +// ALOGD("%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) { + ALOGE("%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 -1; + } + + 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/libsensors/geomagneticd/geomagneticd.c b/libsensors/geomagneticd/geomagneticd.c new file mode 100644 index 0000000..918edbf --- /dev/null +++ b/libsensors/geomagneticd/geomagneticd.c @@ -0,0 +1,368 @@ +/* + * 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 <sys/stat.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, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); + 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. + */ + + ALOGD("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; + } + + ALOGD("Reading config"); + + rc = yas_cfg_read(&hard_offset, &calib_offset, &accuracy); + if (rc == 0) { + ALOGD("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); + ALOGD("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) { + ALOGD("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/libsensors/gp2a_light.c b/libsensors/gp2a_light.c new file mode 100644 index 0000000..9f2d37e --- /dev/null +++ b/libsensors/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; + + ALOGD("%s(%p, %p)", __func__, handlers, device); + + if (handlers == NULL) + return -EINVAL; + + input_fd = input_open("light_sensor"); + if (input_fd < 0) { + ALOGE("%s: Unable to open input", __func__); + goto error; + } + + rc = sysfs_path_prefix("light_sensor", (char *) &path); + if (rc < 0 || path[0] == '\0') { + ALOGE("%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; + + ALOGD("%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; + + ALOGD("%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) { + ALOGE("%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; + + ALOGD("%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) { + ALOGE("%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; + +// ALOGD("%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) { + ALOGE("%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/libsensors/gp2a_proximity.c b/libsensors/gp2a_proximity.c new file mode 100644 index 0000000..873ad94 --- /dev/null +++ b/libsensors/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; + + ALOGD("%s(%p, %p)", __func__, handlers, device); + + if (handlers == NULL) + return -EINVAL; + + input_fd = input_open("proximity_sensor"); + if (input_fd < 0) { + ALOGE("%s: Unable to open input", __func__); + goto error; + } + + rc = sysfs_path_prefix("proximity_sensor", (char *) &path); + if (rc < 0 || path[0] == '\0') { + ALOGE("%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; + + ALOGD("%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; + + ALOGD("%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) { + ALOGE("%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; + + ALOGD("%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) { + ALOGE("%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; + +// ALOGD("%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/libsensors/input.c b/libsensors/input.c new file mode 100644 index 0000000..f82cfd2 --- /dev/null +++ b/libsensors/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/libsensors/orientationd/input.c b/libsensors/orientationd/input.c new file mode 100644 index 0000000..eea1494 --- /dev/null +++ b/libsensors/orientationd/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 <stdint.h> +#include <string.h> +#include <unistd.h> +#include <dirent.h> +#include <stdio.h> +#include <fcntl.h> +#include <errno.h> + +#include <linux/ioctl.h> +#include <linux/input.h> + +#include "orientationd.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, int write) +{ + 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, write ? O_RDWR : 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/libsensors/orientationd/orientationd.c b/libsensors/orientationd/orientationd.c new file mode 100644 index 0000000..7cc9f0a --- /dev/null +++ b/libsensors/orientationd/orientationd.c @@ -0,0 +1,372 @@ +/* + * Copyright (C) 2013 Paul Kocialkowski + * + * Orientation calculation based on AK8975_FS: + * Copyright (C) 2012 Asahi Kasei Microdevices Corporation, Japan + * + * 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 <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <errno.h> +#include <poll.h> +#include <math.h> +#include <linux/ioctl.h> +#include <linux/input.h> + +#include <sys/types.h> +#include <sys/stat.h> +#include <fcntl.h> + +#include "orientationd.h" + +#include <hardware/sensors.h> + +#define ABS_CONTROL_REPORT (ABS_THROTTLE) + +#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 sensor_device { + char *name; + int handle; + float (*get_data)(int value); + int (*set_data)(float value); + + int fd; +}; + +struct sensor_data { + vector v; + int flags; +}; + +float rad2deg(float v) +{ + return (v * 180.0f / 3.1415926535f); +} + +void orientation_calculate(vector *a, vector *m, vector *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->x = rad2deg(azimuth); + o->y = rad2deg(pitch); + o->z = rad2deg(roll); + + if (o->x < 0) + o->x += 360.0f; +} + +float bma250_acceleration(int value) +{ + return (float) (value * GRAVITY_EARTH) / 256.0f; +} + +float yas530c_magnetic(int value) +{ + return (float) value / 1000.0f; +} + +int yas_orientation(float value) +{ + return (int) (value * 1000); +} + +struct sensor_device bma250_device = { + .name = "accelerometer", + .handle = SENSOR_TYPE_ACCELEROMETER, + .get_data = bma250_acceleration, + .set_data = NULL, + .fd = -1, +}; + +struct sensor_device yas530c_device = { + .name = "geomagnetic", + .handle = SENSOR_TYPE_MAGNETIC_FIELD, + .get_data = yas530c_magnetic, + .set_data = NULL, + .fd = -1, +}; + +struct sensor_device yas_orientation_device = { + .name = "orientation", + .handle = SENSOR_TYPE_ORIENTATION, + .get_data = NULL, + .set_data = yas_orientation, + .fd = -1, +}; + +struct sensor_device *sensor_devices[] = { + &bma250_device, + &yas530c_device, + &yas_orientation_device, +}; + +int sensors_devices_count = sizeof(sensor_devices) / sizeof(struct sensor_device *); + +int sensor_device_open(struct sensor_device *dev) +{ + int fd; + + if (dev == NULL || dev->name == NULL) + return -EINVAL; + + printf("Opening %s\n", dev->name); + + fd = input_open(dev->name, dev->handle == SENSOR_TYPE_ORIENTATION ? 1 : 0); + if (fd < 0) + return -1; + + dev->fd = fd; + + return 0; +} + +void sensor_device_close(struct sensor_device *dev) +{ + if (dev == NULL || dev->fd < 0) + return; + + close(dev->fd); + dev->fd = -1; +} + +struct sensor_device *sensor_device_find_handle(int handle) +{ + int i; + + for (i=0 ; i < sensors_devices_count ; i++) { + if (sensor_devices[i]->handle == handle) + return sensor_devices[i]; + } + + return NULL; +} + +struct sensor_device *sensor_device_find_fd(int fd) +{ + int i; + + for (i=0 ; i < sensors_devices_count ; i++) { + if (sensor_devices[i]->fd == fd) + return sensor_devices[i]; + } + + return NULL; +} + +int sensor_device_get_data(struct sensor_device *dev, struct sensor_data *d, + struct input_event *e) +{ + if (dev == NULL || d == NULL || e == NULL || dev->get_data == NULL) + return -EINVAL; + + if (e->type == EV_ABS) { + switch (e->code) { + case ABS_X: + d->v.x = dev->get_data(e->value); + d->flags |= FLAG_X; + return 0; + case ABS_Y: + d->v.y = dev->get_data(e->value); + d->flags |= FLAG_Y; + return 0; + case ABS_Z: + d->v.z = dev->get_data(e->value); + d->flags |= FLAG_Z; + return 0; + } + } + + return -1; +} + +int sensor_device_set_data(struct sensor_device *dev, struct sensor_data *d) +{ + struct input_event event; + + if (dev == NULL || d == NULL || dev->set_data == NULL) + return -EINVAL; + + event.type = EV_ABS; + event.code = ABS_X; + event.value = dev->set_data(d->v.x); + gettimeofday(&event.time, NULL); + write(dev->fd, &event, sizeof(event)); + + event.type = EV_ABS; + event.code = ABS_Y; + event.value = dev->set_data(d->v.y); + gettimeofday(&event.time, NULL); + write(dev->fd, &event, sizeof(event)); + + event.type = EV_ABS; + event.code = ABS_Z; + event.value = dev->set_data(d->v.z); + gettimeofday(&event.time, NULL); + write(dev->fd, &event, sizeof(event)); + + event.type = EV_SYN; + event.code = SYN_REPORT; + event.value = 0; + gettimeofday(&event.time, NULL); + write(dev->fd, &event, sizeof(event)); + + return 0; +} + +int sensor_device_control(struct sensor_device *dev, struct input_event *e) +{ + int enabled; + + if (dev == NULL || e == NULL) + return -EINVAL; + + if (e->type == EV_ABS && e->code == ABS_CONTROL_REPORT) { + enabled = e->value & (1 << 16); + if (enabled) + return 1; + else + return 0; + } + + return -1; +} + +int main(int argc, char *argv[]) +{ + struct input_event event; + struct sensor_data a, m, o; + + struct sensor_device *dev; + struct pollfd *poll_fds; + + int enabled, data; + int index; + + int rc, c, i; + + memset(&a, 0, sizeof(a)); + memset(&m, 0, sizeof(m)); + memset(&o, 0, sizeof(o)); + + poll_fds = (struct pollfd *) calloc(1, sizeof(struct pollfd) * sensors_devices_count); + + index = -1; + c = 0; + + for (i=0 ; i < sensors_devices_count ; i++) { + rc = sensor_device_open(sensor_devices[i]); + if (rc < 0) + continue; + + poll_fds[c].fd = sensor_devices[i]->fd; + poll_fds[c].events = POLLIN; + + if (sensor_devices[i]->handle == SENSOR_TYPE_ORIENTATION && index < 0) + index = c; + + c++; + } + + if (c <= 0 || index <= 0) + goto exit; + + printf("Starting main loop\n"); + + enabled = 0; + while (1) { + data = 0; + + if (enabled) + rc = poll(poll_fds, c, -1); + else + rc = poll(&poll_fds[index], 1, -1); + + if (rc < 0) + goto exit; + + for (i=0 ; i < c ; i++) { + if (poll_fds[i].revents & POLLIN) { + dev = sensor_device_find_fd(poll_fds[i].fd); + if (dev == NULL) + continue; + + read(dev->fd, &event, sizeof(event)); + + switch (dev->handle) { + case SENSOR_TYPE_ACCELEROMETER: + rc = sensor_device_get_data(dev, &a, &event); + if (rc >= 0) + data = 1; + break; + case SENSOR_TYPE_MAGNETIC_FIELD: + rc = sensor_device_get_data(dev, &m, &event); + if (rc >= 0) + data = 1; + break; + case SENSOR_TYPE_ORIENTATION: + rc = sensor_device_control(dev, &event); + if (rc == 1) + enabled = 1; + else if (rc == 0) + enabled = 0; + break; + } + } + + if (data && a.flags & FLAG_ALL && m.flags & FLAG_ALL) { + dev = sensor_device_find_handle(SENSOR_TYPE_ORIENTATION); + if (dev == NULL) + continue; + + orientation_calculate(&a.v, &m.v, &o.v); + sensor_device_set_data(dev, &o); + } + } + } + + +exit: + for (i=0 ; i < sensors_devices_count ; i++) + sensor_device_close(sensor_devices[i]); + + while (1) { + sleep(3600); + } + + return 0; +} diff --git a/libsensors/orientationd/orientationd.h b/libsensors/orientationd/orientationd.h new file mode 100644 index 0000000..41aad5e --- /dev/null +++ b/libsensors/orientationd/orientationd.h @@ -0,0 +1,50 @@ +/* + * 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 <linux/input.h> + +#ifndef _ORIENTATION_H_ +#define _ORIENTATION_H_ + +/* + * Vector + */ + +struct vector { + float x; + float y; + float z; + +}; + +typedef struct vector vector; + +void vector_add(vector *v, vector *a); +void vector_multiply(vector *v, float k); +void vector_cross(vector *v, vector *c, vector *out); +float vector_scalar(vector *v, vector *d); +float vector_length(vector *v); + +/* + * Input + */ + +int64_t input_timestamp(struct input_event *event); +int input_open(char *name, int write); +int sysfs_path_prefix(char *name, char *path_prefix); + +#endif diff --git a/libsensors/orientationd/vector.c b/libsensors/orientationd/vector.c new file mode 100644 index 0000000..ee8ba49 --- /dev/null +++ b/libsensors/orientationd/vector.c @@ -0,0 +1,61 @@ +/* + * 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 <math.h> + +#include "orientationd.h" + +void vector_copy(vector *in, vector *out) +{ + out->x = in->x; + out->y = in->y; + out->z = in ->z; +} + +void vector_add(vector *v, vector *a) +{ + v->x += a->x; + v->y += a->y; + v->z += a->z; +} + +void vector_multiply(vector *v, float k) +{ + v->x *= k; + v->y *= k; + v->z *= k; +} + +void vector_cross(vector *v, vector *c, vector *out) +{ + struct vector t; + + t.x = v->x * c->z - v->z * c->y; + t.y = v->z * c->x - v->x * c->z; + t.y = v->y * c->y - v->y * c->x; + vector_copy(&t, out); +} + +float vector_scalar(vector *v, vector *d) +{ + return v->x * d->x + v->y * d->y + v->z * d->z; +} + +float vector_length(vector *v) +{ + return sqrtf(vector_scalar(v, v)); +} diff --git a/libsensors/piranha_sensors.c b/libsensors/piranha_sensors.c new file mode 100644 index 0000000..2d1ff71 --- /dev/null +++ b/libsensors/piranha_sensors.c @@ -0,0 +1,289 @@ +/* + * 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; + + ALOGD("%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; + + ALOGD("%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 poll_rc, rc; + +// ALOGD("%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; + + do { + poll_rc = poll(device->poll_fds, device->poll_fds_count, PIRANHA_POLL_DELAY); + 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--; + } + } + + if (count <= 0) + break; + } + } while ((poll_rc > 0 || n < 1) && count > 0); + + return n; +} + +/* + * Interface + */ + +int piranha_sensors_close(hw_device_t *device) +{ + struct piranha_sensors_device *piranha_sensors_device; + int i; + + ALOGD("%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; + + ALOGD("%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) +{ + ALOGD("%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/libsensors/piranha_sensors.h b/libsensors/piranha_sensors.h new file mode 100644 index 0000000..b93f65e --- /dev/null +++ b/libsensors/piranha_sensors.h @@ -0,0 +1,87 @@ +/* + * 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_ + +#define PIRANHA_POLL_DELAY 100 + +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/libsensors/yas530c.c b/libsensors/yas530c.c new file mode 100644 index 0000000..f13fb61 --- /dev/null +++ b/libsensors/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; + + ALOGD("%s(%p, %p)", __func__, handlers, device); + + if (handlers == NULL) + return -EINVAL; + + input_fd = input_open("geomagnetic"); + if (input_fd < 0) { + ALOGE("%s: Unable to open input", __func__); + goto error; + } + + rc = sysfs_path_prefix("geomagnetic", (char *) &path); + if (rc < 0 || path[0] == '\0') { + ALOGE("%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; + + ALOGD("%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; + + ALOGD("%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) { + ALOGE("%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; + + ALOGD("%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) { + ALOGE("%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; + +// ALOGD("%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) { + ALOGE("%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/libsensors/yas_orientation.c b/libsensors/yas_orientation.c new file mode 100644 index 0000000..2cd23d3 --- /dev/null +++ b/libsensors/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; + + ALOGD("%s(%p, %p)", __func__, handlers, device); + + if (handlers == NULL || device == NULL) + return -EINVAL; + + input_fd = input_open("orientation"); + if (input_fd < 0) { + ALOGE("%s: Unable to open input", __func__); + goto error; + } + + rc = sysfs_path_prefix("orientation", (char *) &path); + if (rc < 0 || path[0] == '\0') { + ALOGE("%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') { + ALOGE("%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') { + ALOGE("%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; + + ALOGD("%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; + + ALOGD("%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) { + ALOGE("%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) { + ALOGE("%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) { + ALOGE("%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; + + ALOGD("%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) { + ALOGE("%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) { + ALOGE("%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) { + ALOGE("%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; + +// ALOGD("%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) { + ALOGE("%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) { + ALOGE("%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) { + ALOGE("%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, +}; |