We're good with lights
authorPaul Kocialkowski <contact@paulk.fr>
Wed, 20 Feb 2013 19:16:03 +0000 (20:16 +0100)
committerPaul Kocialkowski <contact@paulk.fr>
Wed, 20 Feb 2013 19:16:03 +0000 (20:16 +0100)
Signed-off-by: Paul Kocialkowski <contact@paulk.fr>
bh1721.c
input.c
piranha_sensors.c
piranha_sensors.h

index 69ac854..d7fec25 100644 (file)
--- a/bh1721.c
+++ b/bh1721.c
@@ -17,6 +17,7 @@
 
 #include <stdlib.h>
 #include <unistd.h>
+#include <stdint.h>
 #include <fcntl.h>
 #include <errno.h>
 #include <linux/ioctl.h>
 
 #include "piranha_sensors.h"
 
-int bh1721_activate(struct piranha_sensors_handlers *handlers)
+struct bh1721_data {
+       char path_enable[PATH_MAX];
+       char path_delay[PATH_MAX];
+};
+
+int bh1721_init(struct piranha_sensors_handlers *handlers)
 {
-       int input_fd;
+       struct bh1721_data *data = NULL;
+       char path[PATH_MAX] = { 0 };
+       int input_fd = -1;
+       int rc;
 
        LOGD("%s(%p)", __func__, handlers);
 
@@ -42,16 +51,39 @@ int bh1721_activate(struct piranha_sensors_handlers *handlers)
        input_fd = input_open("light_sensor");
        if (input_fd < 0) {
                LOGE("%s: Unable to open input", __func__);
-               return -1;
+               goto error;
+       }
+
+       rc = sysfs_path_prefix("light_sensor", (char *) &path);
+       if (rc < 0 || path[0] == '\0') {
+               LOGE("%s: Unable to open sysfs", __func__);
+               goto error;
        }
-LOGD("opened %d", input_fd);
+
+       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->activated = 1;
+       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_deactivate(struct piranha_sensors_handlers *handlers)
+int bh1721_deinit(struct piranha_sensors_handlers *handlers)
 {
        int input_fd;
 
@@ -65,14 +97,96 @@ int bh1721_deactivate(struct piranha_sensors_handlers *handlers)
                close(input_fd);
 
        handlers->poll_fd = -1;
+
+       if (handlers->data != NULL)
+               free(handlers->data);
+
+       handlers->data = NULL;
+
+       return 0;
+}
+
+int bh1721_activate(struct piranha_sensors_handlers *handlers)
+{
+       struct bh1721_data *data;
+       char enable[] = "1\n";
+       int fd;
+
+       LOGD("%s(%p)", __func__, handlers);
+
+       if (handlers == NULL || handlers->data == NULL)
+               return -EINVAL;
+
+       data = (struct bh1721_data *) handlers->data;
+
+       fd = open(data->path_enable, O_WRONLY);
+       if (fd < 0) {
+               LOGE("%s: Unable to open enable path", __func__);
+               return -1;
+       }
+
+       write(fd, &enable, sizeof(enable));
+       close(fd);
+
+       handlers->activated = 1;
+
+       return 0;
+}
+
+int bh1721_deactivate(struct piranha_sensors_handlers *handlers)
+{
+       struct bh1721_data *data;
+       char enable[] = "0\n";
+       int fd;
+
+       LOGD("%s(%p)", __func__, handlers);
+
+       if (handlers == NULL || handlers->data == NULL)
+               return -EINVAL;
+
+       data = (struct bh1721_data *) handlers->data;
+
+       fd = open(data->path_enable, O_WRONLY);
+       if (fd < 0) {
+               LOGE("%s: Unable to open enable path", __func__);
+               return -1;
+       }
+
+       write(fd, &enable, sizeof(enable));
+       close(fd);
+
        handlers->activated = 0;
 
        return 0;
 }
 
-int bh1721_set_delay(struct piranha_sensors_handlers *handlers, int delay)
+int bh1721_set_delay(struct piranha_sensors_handlers *handlers, int64_t delay)
 {
-       LOGD("%s(%p, %d)", __func__, handlers, delay);
+       struct bh1721_data *data;
+       char *value = NULL;
+       int c;
+       int fd;
+
+       LOGD("%s(%p, %ld)", __func__, handlers, (long int) delay);
+
+       if (handlers == NULL || handlers->data == NULL)
+               return -EINVAL;
+
+       data = (struct bh1721_data *) handlers->data;
+
+       fd = open(data->path_delay, O_WRONLY);
+       if (fd < 0) {
+               LOGE("%s: Unable to open delay path", __func__);
+               return -1;
+       }
+
+       c = asprintf(&value, "%ld\n", (long int) delay);
+
+       write(fd, value, c);
+       close(fd);
+
+       if (value != NULL)
+               free(value);
 
        return 0;
 }
@@ -84,7 +198,7 @@ int bh1721_get_data(struct piranha_sensors_handlers *handlers,
        int input_fd;
        int rc;
 
-       if (handlers == NULL)
+       if (handlers == NULL || event == NULL)
                return -EINVAL;
 
        input_fd = handlers->poll_fd;
@@ -95,7 +209,14 @@ int bh1721_get_data(struct piranha_sensors_handlers *handlers,
        if (rc < (int) sizeof(input_event))
                return -EINVAL;
 
-       LOGE("Got %d lux", input_event.value);
+       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 = (float) input_event.value * 0.712f;
 
        return 0;
 }
@@ -103,6 +224,8 @@ int bh1721_get_data(struct piranha_sensors_handlers *handlers,
 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,
diff --git a/input.c b/input.c
index 2f0f97c..811c106 100644 (file)
--- a/input.c
+++ b/input.c
@@ -17,6 +17,7 @@
 
 #include <stdlib.h>
 #include <unistd.h>
+#include <stdint.h>
 #include <fcntl.h>
 #include <errno.h>
 #include <dirent.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;
@@ -58,7 +67,7 @@ int input_open(char *name)
                if (rc < 0)
                        continue;
 
-               if (strcmp(input_name, name) == 0)
+               if (strncmp(input_name, name, strlen(name)) == 0)
                        return fd;
                else
                        close(fd);
@@ -66,3 +75,41 @@ int input_open(char *name)
 
        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];
+       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);
+
+               if (strncmp(input_name, name, strlen(name)) == 0) {
+                       snprintf(path_prefix, PATH_MAX, "/sys/class/input/%s", di->d_name);
+                       return 0;
+               }
+       }
+
+       return -1;
+}
index e641791..0d21a0c 100644 (file)
@@ -17,6 +17,7 @@
 
 #include <stdlib.h>
 #include <unistd.h>
+#include <stdint.h>
 #include <fcntl.h>
 #include <errno.h>
 #include <poll.h>
@@ -90,7 +91,7 @@ int piranha_sensors_set_delay(struct sensors_poll_device_t *dev, int handle, int
        struct piranha_sensors_device *device;
        int i;
 
-       LOGD("%s(%p, %d, %d)", __func__, dev, handle, ns);
+       LOGD("%s(%p, %d, %ld)", __func__, dev, handle, (long int) ns);
 
        if (dev == NULL)
                return -EINVAL;
@@ -119,7 +120,7 @@ int piranha_sensors_poll(struct sensors_poll_device_t *dev,
        int c, n;
        int rc;
 
-       LOGD("%s(%p, %p, %d)", __func__, dev, data, count);
+//     LOGD("%s(%p, %p, %d)", __func__, dev, data, count);
 
        if (dev == NULL)
                return -EINVAL;
@@ -151,7 +152,7 @@ int piranha_sensors_poll(struct sensors_poll_device_t *dev,
 
        n = 0;
        for (c=0 ; c < count ; c++) {
-               rc = poll(device->poll_fds, index, 100);
+               rc = poll(device->poll_fds, index, 10);
                if (rc <= 0)
                        continue;
 
@@ -162,7 +163,10 @@ int piranha_sensors_poll(struct sensors_poll_device_t *dev,
 
                                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]);
+                                       rc = device->handlers[j]->get_data(device->handlers[j], &data[n]);
+                                       if (rc < 0)
+                                               continue;
+
                                        n++;
                                }
                        }
@@ -183,6 +187,7 @@ error:
 int piranha_sensors_close(hw_device_t *device)
 {
        struct piranha_sensors_device *piranha_sensors_device;
+       int i;
 
        LOGD("%s(%p)", __func__, device);
 
@@ -190,9 +195,17 @@ int piranha_sensors_close(hw_device_t *device)
                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;
@@ -202,6 +215,7 @@ 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 i;
 
        LOGD("%s(%p, %s, %p)", __func__, module, id, device);
 
@@ -223,6 +237,13 @@ int piranha_sensors_open(const struct hw_module_t* module, const char *id,
        piranha_sensors_device->poll_fds = (struct pollfd *)
                malloc(piranha_sensors_handlers_count * sizeof(struct pollfd));
 
+       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]);
+       }
+
        *device = &(piranha_sensors_device->device.common);
 
        return 0;
index 44f835d..38478c6 100644 (file)
  * 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>
 
@@ -27,9 +30,11 @@ struct piranha_sensors_handlers {
        char *name;
        int handle;
 
+       int (*init)(struct piranha_sensors_handlers *handlers);
+       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, int delay);
+       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;
@@ -51,7 +56,9 @@ struct piranha_sensors_device {
  * Input
  */
 
+int64_t input_timestamp(struct input_event *event);
 int input_open(char *name);
+int sysfs_path_prefix(char *name, char *path_prefix);
 
 /*
  * Sensors