At least, it won't make zygote crash
authorPaul Kocialkowski <contact@paulk.fr>
Mon, 18 Feb 2013 21:44:25 +0000 (22:44 +0100)
committerPaul Kocialkowski <contact@paulk.fr>
Mon, 18 Feb 2013 21:44:25 +0000 (22:44 +0100)
Android.mk [new file with mode: 0644]
bh1721.c [new file with mode: 0644]
input.c [new file with mode: 0644]
piranha_sensors.c [new file with mode: 0644]
piranha_sensors.h [new file with mode: 0644]

diff --git a/Android.mk b/Android.mk
new file mode 100644 (file)
index 0000000..bafe58c
--- /dev/null
@@ -0,0 +1,34 @@
+#
+# 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 \
+       bh1721.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
+
+include $(BUILD_SHARED_LIBRARY)
diff --git a/bh1721.c b/bh1721.c
new file mode 100644 (file)
index 0000000..69ac854
--- /dev/null
+++ b/bh1721.c
@@ -0,0 +1,113 @@
+/*
+ * 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 <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"
+
+int bh1721_activate(struct piranha_sensors_handlers *handlers)
+{
+       int input_fd;
+
+       LOGD("%s(%p)", __func__, handlers);
+
+       if (handlers == NULL)
+               return -EINVAL;
+
+       input_fd = input_open("light_sensor");
+       if (input_fd < 0) {
+               LOGE("%s: Unable to open input", __func__);
+               return -1;
+       }
+LOGD("opened %d", input_fd);
+       handlers->poll_fd = input_fd;
+       handlers->activated = 1;
+
+       return 0;
+}
+
+int bh1721_deactivate(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;
+       handlers->activated = 0;
+
+       return 0;
+}
+
+int bh1721_set_delay(struct piranha_sensors_handlers *handlers, int delay)
+{
+       LOGD("%s(%p, %d)", __func__, handlers, delay);
+
+       return 0;
+}
+
+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)
+               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;
+
+       LOGE("Got %d lux", input_event.value);
+
+       return 0;
+}
+
+struct piranha_sensors_handlers bh1721 = {
+       .name = "BH1721",
+       .handle = SENSOR_TYPE_LIGHT,
+       .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/input.c b/input.c
new file mode 100644 (file)
index 0000000..2f0f97c
--- /dev/null
+++ b/input.c
@@ -0,0 +1,68 @@
+/*
+ * 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 <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"
+
+int input_open(char *name)
+{
+       DIR *d;
+       struct dirent *di;
+
+       char input_name[80] = { 0 };
+       char path[PATH_MAX];
+       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;
+
+               if (strcmp(input_name, name) == 0)
+                       return fd;
+               else
+                       close(fd);
+       }
+
+       return -1;      
+}
diff --git a/piranha_sensors.c b/piranha_sensors.c
new file mode 100644 (file)
index 0000000..e641791
--- /dev/null
@@ -0,0 +1,258 @@
+/*
+ * 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 <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, {}, },
+       { "BH1721 Light Sensor", "ROHM", 1, SENSOR_TYPE_LIGHT,
+               SENSOR_TYPE_LIGHT, 0.0f, 0.0f, 0.0f, 0, {}, },
+};
+
+int piranha_sensors_count = sizeof(piranha_sensors) / sizeof(struct sensor_t);
+
+struct piranha_sensors_handlers *piranha_sensors_handlers[] = {
+       &bh1721,
+};
+
+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, %d)", __func__, dev, handle, 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 index, 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)
+               return -EINVAL;
+
+       memset(device->poll_fds, 0, device->handlers_count * sizeof(struct pollfd));
+
+       index = 0;
+       for (i=0 ; i < device->handlers_count ; i++) {
+               if (device->handlers[i] == NULL)
+                       continue;
+
+               if (device->handlers[i]->activated && device->handlers[i]->poll_fd >= 0) {
+                       device->poll_fds[index].fd = device->handlers[i]->poll_fd;
+                       device->poll_fds[index].events = POLLIN;
+                       index++;
+               }
+       }
+
+       // No sensor is enabled
+       if (index == 0) {
+               usleep(3000);
+               return 0;
+       }
+
+       n = 0;
+       for (c=0 ; c < count ; c++) {
+               rc = poll(device->poll_fds, index, 100);
+               if (rc <= 0)
+                       continue;
+
+               for (i=0 ; i < index ; i++) {
+                       for (j=0 ; j < device->handlers_count ; j++) {
+                               if (device->handlers[j] == NULL)
+                                       continue;
+
+                               if (device->poll_fds[i].revents & POLLIN && device->poll_fds[i].fd == device->handlers[j]->poll_fd && device->handlers[j]->get_data != NULL) {
+                                       device->poll_fds[i].revents = 0;
+                                       device->handlers[j]->get_data(device->handlers[j], &data[n]);
+                                       n++;
+                               }
+                       }
+
+               }
+       }
+
+       return n;
+
+error:
+       return 0;
+}
+
+/*
+ * Interface
+ */
+
+int piranha_sensors_close(hw_device_t *device)
+{
+       struct piranha_sensors_device *piranha_sensors_device;
+
+       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);
+
+       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;
+
+       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 *)
+               malloc(piranha_sensors_handlers_count * sizeof(struct pollfd));
+
+       *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/piranha_sensors.h b/piranha_sensors.h
new file mode 100644 (file)
index 0000000..44f835d
--- /dev/null
@@ -0,0 +1,62 @@
+/*
+ * 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 <poll.h>
+
+#include <hardware/sensors.h>
+#include <hardware/hardware.h>
+
+#ifndef _SENSORS_H_
+#define _SENSORS_H_
+
+struct piranha_sensors_handlers {
+       char *name;
+       int handle;
+
+       int (*activate)(struct piranha_sensors_handlers *handlers);
+       int (*deactivate)(struct piranha_sensors_handlers *handlers);
+       int (*set_delay)(struct piranha_sensors_handlers *handlers, int 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;
+};
+
+/*
+ * Input
+ */
+
+int input_open(char *name);
+
+/*
+ * Sensors
+ */
+
+extern struct piranha_sensors_handlers bh1721;
+
+#endif