added send_command to submit command to ttyusb
[svn42.git] / door_daemon / door_daemon.c
index 4ad308c..d9af77a 100644 (file)
 #include "sig_handler.h"
 #include "options.h"
 
+#include "command_queue.h"
+
 #include "daemon.h"
 #include "sysexec.h"
 
-int process_cmd(int fd)
+int init_command_socket(const char* path)
 {
-  log_printf(INFO, "processing command from %d", fd);
+  int fd = socket(AF_UNIX, SOCK_STREAM, 0);
+  if(fd < 0) {
+    log_printf(ERROR, "unable to open socket: %s", strerror(errno));
+    return -1;
+  }
+
+  struct sockaddr_un local;
+  local.sun_family = AF_UNIX;
+  strcpy(local.sun_path, path); // TODO: strlen ???
+  unlink(local.sun_path);
+  int len = SUN_LEN(&local);
+  int ret = bind(fd, (struct sockaddr*)&local, len);
+  if(ret) {
+    log_printf(ERROR, "unable to bind to '%s': %s", local.sun_path, strerror(errno));
+    return -1;
+  }
+  
+  ret = listen(fd, 4);
+  if(ret) {
+    log_printf(ERROR, "unable to listen on command socket: %s", local.sun_path, strerror(errno));
+    return -1;
+  }
+
+  log_printf(INFO, "now listening on %s for incoming commands", path);
+  
+  return fd;
+}
+
+int send_command(int ttyusb_fd, cmd_t* cmd)
+{
+  if(!cmd)
+    return -1;
+  
+  char c;
+  switch(cmd->cmd) {
+  case OPEN: c = 'o'; break;
+  case CLOSE: c = 'c'; break;
+  case TOGGLE: c = 't'; break;
+  case STATUS: c = 's'; break;
+  case RESET: c = 'r'; break;
+  case LOG: return 0;
+  }
+  
+  int ret;
+  do {
+    ret = write(ttyusb_fd, &c, 1);
+  }
+  while(!ret);
+
+  if(ret > 0)
+    return 0;
 
-  char* buffer[100];
+  return ret;
+}
+
+int handle_command(const char* cmd, int fd, cmd_t** cmd_q)
+{
+  if(!cmd_q || !cmd)
+    return -1;
+  
+  cmd_id_t cmd_id;
+  if(!strncmp(cmd, "open", 4))
+    cmd_id = OPEN;
+  else if(!strncmp(cmd, "close", 5))
+    cmd_id = CLOSE;
+  else if(!strncmp(cmd, "toggle", 6))
+    cmd_id = TOGGLE;
+  else if(!strncmp(cmd, "reset", 5))
+    cmd_id = RESET;
+  else if(!strncmp(cmd, "status", 6))
+    cmd_id = STATUS;
+  else if(!strncmp(cmd, "log", 3))
+    cmd_id = LOG;
+  else {
+    log_printf(WARNING, "unknown command '%s'", cmd);
+    return 1;
+  }
+  char* param = strchr(cmd, ' ');
+  if(param) 
+    param++;
+
+  switch(cmd_id) {
+  case OPEN:
+  case CLOSE:
+  case TOGGLE:
+  case STATUS:
+  case RESET: {
+    int ret = cmd_push(cmd_q, fd, cmd_id, param);
+    if(ret)
+      return ret;
+
+    log_printf(NOTICE, "command: %s", cmd); 
+    break;
+  }
+  case LOG: {
+    if(param && param[0])
+      log_printf(NOTICE, "ext msg: %s", param); 
+    else
+      log_printf(DEBUG, "ignoring empty ext log message");
+    break;
+  }
+  }
+  
+  return 0;
+}
+
+int process_cmd(int fd, cmd_t **cmd_q)
+{
+  static char buffer[100];
   int ret = 0;
   do {
+    memset(buffer, 0, 100);
     ret = recv(fd, buffer, sizeof(buffer), 0);
-    if(!ret) return 1;
+    if(!ret)
+      return 1;
+    char* saveptr;
+    char* tok = strtok_r(buffer, "\n\r", &saveptr);
+    do {
+      ret = handle_command(tok, fd, cmd_q);
+      if(ret < 0)
+        return ret;
+    } while(tok = strtok_r(NULL, "\n\r", &saveptr));
   } while (ret == -1 && errno == EINTR);
     
+  log_printf(DEBUG, "processing command from %d", fd);
+
   return 0;
 }
 
-int process_ttyusb(int ttyusb_fd)
+int process_ttyusb(int ttyusb_fd, cmd_t **cmd_q)
 {
-  log_printf(INFO, "processing data from ttyusb (fd=%d)", ttyusb_fd);
-
-  char* buffer[100];
+  static char buffer[100];
   int ret = 0;
   do {
+    memset(buffer, 0, 100);
     ret = read(ttyusb_fd, buffer, sizeof(buffer));
-    if(!ret) return -1;
+    if(!ret) 
+      return 2;
   } while (ret == -1 && errno == EINTR);
 
+  log_printf(INFO, "processing data from ttyusb (fd=%d)", ttyusb_fd);
+
   return 0;
 }
 
@@ -72,6 +193,7 @@ int main_loop(int ttyusb_fd, int cmd_listen_fd)
   FD_SET(ttyusb_fd, &readfds);
   FD_SET(cmd_listen_fd, &readfds);
   int max_fd = ttyusb_fd > cmd_listen_fd ? ttyusb_fd : cmd_listen_fd;
+  cmd_t* cmd_q = NULL;
 
   signal_init();
   int return_value = 0;
@@ -93,7 +215,7 @@ int main_loop(int ttyusb_fd, int cmd_listen_fd)
     }
 
     if(FD_ISSET(ttyusb_fd, &tmpfds)) {
-      return_value = process_ttyusb(ttyusb_fd);
+      return_value = process_ttyusb(ttyusb_fd, &cmd_q);
       if(return_value)
         break;
 
@@ -107,7 +229,7 @@ int main_loop(int ttyusb_fd, int cmd_listen_fd)
         return_value = -1;
         break;
       }  
-      log_printf(INFO, "new command connection (fd=%d)", new_fd);
+      log_printf(DEBUG, "new command connection (fd=%d)", new_fd);
       FD_SET(new_fd, &readfds);
       max_fd = (max_fd < new_fd) ? new_fd : max_fd;
       FD_CLR(cmd_listen_fd, &tmpfds);
@@ -116,18 +238,23 @@ int main_loop(int ttyusb_fd, int cmd_listen_fd)
     int fd;
     for(fd = 0; fd <= max_fd; fd++) {
       if(FD_ISSET(fd, &tmpfds)) {
-        return_value = process_cmd(fd); 
+        return_value = process_cmd(fd, &cmd_q); 
         if(return_value == 1) {
+          log_printf(DEBUG, "removing closed command connection (fd=%d)", fd);
           close(fd);
           FD_CLR(fd, &readfds);
           return_value = 0;
         }
         if(return_value)
           break;
+
+        if(cmd_q && !cmd_q->sent)
+          send_command(ttyusb_fd, cmd_q);
       }
     }
   }
 
+  cmd_clear(&cmd_q);
   return return_value;
 }
 
@@ -142,7 +269,7 @@ int main(int argc, char* argv[])
       fprintf(stderr, "syntax error near: %s\n\n", argv[ret]);
     }
     if(ret == -2) {
-      fprintf(stderr, "memory error on options_parse, exitting\n");
+      fprintf(stderr, "memory error on options_parse, exiting\n");
     }
 
     if(ret != -2)
@@ -185,48 +312,6 @@ int main(int argc, char* argv[])
       exit(-1);
     }
 
-  int ttyusb_fd = open(opt.ttyusb_dev_, O_RDWR);
-  if(ttyusb_fd < 0) {
-    log_printf(ERROR, "unable to open %s: %s", opt.ttyusb_dev_, strerror(errno));
-    options_clear(&opt);
-    log_close();
-    exit(-1);
-  }
-
-  int cmd_listen_fd = socket(AF_UNIX, SOCK_STREAM, 0);
-  struct sockaddr_un local;
-  int len;
-  if(cmd_listen_fd < 0) {
-    log_printf(ERROR, "unable to open socket: %s", strerror(errno));
-    close(ttyusb_fd);
-    options_clear(&opt);
-    log_close();
-    exit(-1);
-  }
-  local.sun_family = AF_UNIX;
-  strcpy(local.sun_path, opt.command_sock_); // TODO: strlen ???
-  unlink(local.sun_path);
-  len = SUN_LEN(&local);
-  ret = bind(cmd_listen_fd, (struct sockaddr*)&local, len);
-  if(ret) {
-    log_printf(ERROR, "unable to bind to '%s': %s", local.sun_path, strerror(errno));
-    close(cmd_listen_fd);
-    close(ttyusb_fd);
-    options_clear(&opt);
-    log_close();
-    exit(-1);
-  }
-
-  ret = listen(cmd_listen_fd, 4);
-  if(ret) {
-    log_printf(ERROR, "unable to listen on command socket: %s", local.sun_path, strerror(errno));
-    close(cmd_listen_fd);
-    close(ttyusb_fd);
-    options_clear(&opt);
-    log_close();
-    exit(-1);
-  }
-  
   FILE* pid_file = NULL;
   if(opt.pid_file_) {
     pid_file = fopen(opt.pid_file_, "w");
@@ -260,19 +345,37 @@ int main(int argc, char* argv[])
     fclose(pid_file);
   }
 
+  int ttyusb_fd = open(opt.ttyusb_dev_, O_RDWR);
+  if(ttyusb_fd < 0) {
+    log_printf(ERROR, "unable to open %s: %s", opt.ttyusb_dev_, strerror(errno));
+    options_clear(&opt);
+    log_close();
+    exit(-1);
+  }
+
+  int cmd_listen_fd = init_command_socket(opt.command_sock_);
+  if(cmd_listen_fd < 0) {
+    close(ttyusb_fd);
+    options_clear(&opt);
+    log_close();
+    exit(-1);
+  }
+
   ret = main_loop(ttyusb_fd, cmd_listen_fd);
 
   close(cmd_listen_fd);
   close(ttyusb_fd);
-  options_clear(&opt);
 
   if(!ret)
     log_printf(NOTICE, "normal shutdown");
   else if(ret < 0)
     log_printf(NOTICE, "shutdown after error");
+  else if(ret == 2)
+    log_printf(ERROR, "shutdown after %s read error", opt.ttyusb_dev_);
   else
     log_printf(NOTICE, "shutdown after signal");
 
+  options_clear(&opt);
   log_close();
 
   return ret;