OSDN Git Service

GMINL-7944: Introduce internal sensor types for ALS
[android-x86/hardware-intel-libsensors.git] / sens.c
diff --git a/sens.c b/sens.c
index ccde528..8a6f6cc 100644 (file)
--- a/sens.c
+++ b/sens.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2014 Intel Corporation.
+ * Copyright (C) 2014-2015 Intel Corporation.
  */
 
 #include <stdio.h>
@@ -9,13 +9,14 @@
 #include <dlfcn.h>
 #include <pthread.h>
 #include <errno.h>
+#include <signal.h>
 
 #include <hardware/sensors.h>
 #include <utils/Log.h>
 
 int usage(void)
 {
-       fprintf(stderr, "sens start hal.so\n");
+       fprintf(stderr, "sens start [sensors.gmin.so]\n");
        fprintf(stderr, "sens [activate | deactivate] sensor_id\n");
        fprintf(stderr, "sens set_delay sensor_id delay\n");
        fprintf(stderr, "sens poll\n");
@@ -50,7 +51,9 @@ static const char* types[] = {
 
 static const char *type_str(int type)
 {
-       if (type < 0 || type > (int)sizeof(types)/sizeof(char *))
+       int type_count = sizeof(types)/sizeof(char *);
+
+       if (type < 0 || type >= type_count)
                return "unknown";
        return types[type];
 }
@@ -148,7 +151,7 @@ static void print_event(struct sensors_event_t *e)
                fprintf(f, "event: step_detector=%10.2f\n", e->data[0]);
                break;
        case SENSOR_TYPE_STEP_COUNTER:
-               fprintf(f, "event: step_detector=%llu\n",
+               fprintf(f, "event: step_counter=%llu\n",
                        (unsigned long long)e->u64.step_counter);
                break;
        }
@@ -174,8 +177,16 @@ static void run_sensors_poll_v0(void)
        }
 }
 
-static void *run_sensors_thread(void *arg)
+static void sig_pipe(int sig)
+{
+       client = NULL;
+}
+
+static void *run_sensors_thread(void *arg __attribute((unused)))
 {
+
+       signal(SIGPIPE, sig_pipe);
+
        switch (dev->version) {
        case SENSORS_DEVICE_API_VERSION_0_1:
        default:
@@ -231,7 +242,7 @@ static int sensor_activate(int handle, int enable)
 #define CLIENT_ERR(f, fmt...)                  \
        { if (f) { fprintf(f, fmt); fprintf(f, "\n"); } ALOGE(fmt); }
 
-static int dispatch_cmd(char *cmd, int cmd_len, FILE *f)
+static int dispatch_cmd(char *cmd, FILE *f)
 {
        char *argv[16], *tmp;
        int argc = 0, handle;
@@ -295,6 +306,8 @@ static int dispatch_cmd(char *cmd, int cmd_len, FILE *f)
        } else if (!strcmp(argv[0], "poll")) {
 
                pthread_mutex_lock(&client_mutex);
+               if (client)
+                       fclose(client);
                client = f;
                pthread_mutex_unlock(&client_mutex);
 
@@ -357,6 +370,7 @@ static int start_server(void)
                        .msg_controllen = sizeof(cmsg_buffer),
                };
                FILE *f =NULL;
+               struct cmsghdr *cmsg;
 
                conn = accept(sock, NULL, NULL);
                if (conn < 0) {
@@ -364,54 +378,79 @@ static int start_server(void)
                        continue;
                }
 
-               while (1) {
-                       struct cmsghdr *cmsg;
+               err = recvmsg(conn, &msg, 0);
+               if (err < 0) {
+                       ALOGE("error in recvmsg: %s", strerror(errno));
+                       close(conn);
+                       continue;
+               }
 
-                       err = recvmsg(conn, &msg, 0);
-                       if (err < 0) {
-                               ALOGE("error in recvmsg: %s", strerror(errno));
-                               break;
-                       }
+               if (err == 0)
+                       continue;
 
-                       if (err == 0)
+               for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL;
+                    cmsg = CMSG_NXTHDR(&msg,cmsg)) {
+                       if (cmsg->cmsg_level == SOL_SOCKET
+                           && cmsg->cmsg_type == SCM_RIGHTS) {
+                               int *fd = (int *)CMSG_DATA(cmsg);
+                               f = fdopen(*fd, "w");
                                break;
-
-                       for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL;
-                            cmsg = CMSG_NXTHDR(&msg,cmsg)) {
-                               if (cmsg->cmsg_level == SOL_SOCKET
-                                   && cmsg->cmsg_type == SCM_RIGHTS) {
-                                       int *fd = (int *)CMSG_DATA(cmsg);
-                                       f = fdopen(*fd, "w");
-                                       break;
-                               }
                        }
+               }
 
-                       err = dispatch_cmd(data_buff, err, f);
-                       if (err < 0) {
-                               ALOGE("error dispatching command: %d", err);
-                               break;
-                       }
+               if (data_buff[err - 1] != 0) {
+                       ALOGE("command is not NULL terminated\n");
+                       close(conn);
+                       continue;
+               }
 
-                       /* send ack */
-                       if (!err)
-                               write(conn, data_buff, 1);
+               err = dispatch_cmd(data_buff, f);
+               if (err < 0) {
+                       ALOGE("error dispatching command: %d", err);
+                       close(conn);
+                       continue;
                }
 
-               pthread_mutex_lock(&client_mutex);
-               client = NULL;
-               pthread_mutex_unlock(&client_mutex);
+               /* send ack */
+               if (!err) {
+                       write(conn, data_buff, 1);
+                       fclose(f);
+               }
 
-               fclose(f); close(conn);
+               close(conn);
        }
 }
 
-static int start_hal(const char *hal_path)
+static const char *hal_paths[] = {
+       "/system/lib/hw/sensors.gmin.so",
+       "sensors.gmin.so",
+       "/lib/sensors.gmin.so",
+};
+
+static int start_hal(int argc, char **argv)
 {
        void *hal;
        pid_t child;
        int err;
        pthread_t sensors_thread;
+       const char *hal_path = NULL;
+
+       if (argc == 2) {
+               unsigned i;
 
+               for(i = 0; i < sizeof(hal_paths)/sizeof(const char*); i++) {
+                       if (!access(hal_paths[i], R_OK)) {
+                               hal_path = hal_paths[i];
+                               break;
+                       }
+               }
+
+               if (!hal_path) {
+                       fprintf(stderr, "unable to find HAL\n");
+                       exit(1);
+               }
+       } else
+               hal_path = argv[2];
 
        hal = dlopen(hal_path, RTLD_NOW);
        if (!hal) {
@@ -467,9 +506,8 @@ int main(int argc, char **argv)
 {
        char cmd[1024];
        int sock, i;
-       struct iovec recv_buff = {
+       struct iovec buff = {
                .iov_base = cmd,
-               .iov_len = sizeof(cmd),
        };
        struct cmsg_fd {
                struct cmsghdr hdr;
@@ -485,7 +523,7 @@ int main(int argc, char **argv)
        struct msghdr msg = {
                .msg_name = NULL,
                .msg_namelen = 0,
-               .msg_iov = &recv_buff,
+               .msg_iov = &buff,
                .msg_iovlen = 1,
                .msg_control = &cmsg_buff,
                .msg_controllen = sizeof(cmsg_buff),
@@ -498,15 +536,19 @@ int main(int argc, char **argv)
        }
 
        if (!strcmp(argv[1], "start")) {
-               if (argc < 3)
+               if (argc < 2)
                        return usage();
 
-               return start_hal(argv[2]);
+               return start_hal(argc, argv);
        }
 
-       strcpy(cmd, argv[1]); strcat(cmd, " ");
+       if (strlen(argv[1]) >= sizeof(cmd))
+               return usage();
+       strncpy(cmd, argv[1], sizeof(cmd) - 1);
+       strncat(cmd, " ", sizeof(cmd) - strlen(cmd) - 1);
        for(i = 2; i < argc; i++) {
-               strcat(cmd, argv[i]); strcat(cmd, " ");
+               strncat(cmd, argv[i], sizeof(cmd) - strlen(cmd) - 1);
+               strncat(cmd, " ", sizeof(cmd) - strlen(cmd) - 1);
        }
 
        sock = socket(AF_UNIX, SOCK_SEQPACKET, 0);
@@ -520,11 +562,13 @@ int main(int argc, char **argv)
                return 5;
        }
 
+       buff.iov_len = strlen(cmd) + 1;
        if (sendmsg(sock, &msg, 0) < 0) {
                fprintf(stderr, "failed sending command to server: %s\n", strerror(errno));
                return 6;
        }
 
+       buff.iov_len = sizeof(cmd);
        if (read(sock, cmd, 1) < 0) {
                fprintf(stderr, "failed getting ack from server: %s\n", strerror(errno));
                return 7;