status listeners now always get Status: messages
[svn42.git] / door_daemon / door_daemon.c
index c917f3b..ea3e0d6 100644 (file)
@@ -20,6 +20,9 @@
 
 #include "datatypes.h"
 
+#include <termios.h>
+#include <unistd.h>
+
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
@@ -225,11 +228,20 @@ int process_door(int door_fd, cmd_t **cmd_q, client_t* client_lst)
     char* saveptr;
     char* tok = strtok_r(buffer, "\n\r", &saveptr);
     do {
-      if(!cmd_q || !(*cmd_q))
-        break;
-      ret = send_response((*cmd_q)->fd, tok);
-      if(ret < 0)
-        return ret;
+      log_printf(NOTICE, "door-firmware: %s", tok);
+
+      int cmd_fd = -1;
+      if(cmd_q && (*cmd_q)) {
+        cmd_fd = (*cmd_q)->fd;
+        send_response(cmd_fd, tok);
+      }
+
+      if(!strncmp(tok, "Status:", 7)) {
+        client_t* client;
+        for(client = client_lst; client; client = client->next)
+          if(client->status_listener && client->fd != cmd_fd)
+            send_response(client->fd, tok);
+      }
 
       cmd_pop(cmd_q);
     } while(tok = strtok_r(NULL, "\n\r", &saveptr));
@@ -324,6 +336,37 @@ int main_loop(int door_fd, int cmd_listen_fd)
   return return_value;
 }
 
+int setup_tty(int fd)
+{
+  struct termios tmio;
+  
+  int ret = tcgetattr(fd, &tmio);
+  if(ret) {
+    log_printf(ERROR, "Error on tcgetattr(): %s", strerror(errno));
+    return ret;
+  }
+
+  ret = cfsetospeed(&tmio, B9600);
+  if(ret) {
+    log_printf(ERROR, "Error on cfsetospeed(): %s", strerror(errno));
+    return ret;
+  }
+
+  ret = cfsetispeed(&tmio, B9600);
+  if(ret) {
+    log_printf(ERROR, "Error on cfsetispeed(): %s", strerror(errno));
+    return ret;
+  }
+
+  ret = tcsetattr(fd, TCSANOW, &tmio);
+  if(ret) {
+    log_printf(ERROR, "Error on tcsetattr(): %s", strerror(errno));
+    return ret;
+  }
+  
+  return 0;
+}
+
 int main(int argc, char* argv[])
 {
   log_init();
@@ -411,13 +454,21 @@ int main(int argc, char* argv[])
     fclose(pid_file);
   }
 
-  int door_fd = open(opt.door_dev_, O_RDWR);
+  int door_fd = open(opt.door_dev_, O_RDWR | O_NOCTTY);
   if(door_fd < 0) {
     log_printf(ERROR, "unable to open %s: %s", opt.door_dev_, strerror(errno));
     options_clear(&opt);
     log_close();
     exit(-1);
   }
+  ret = setup_tty(door_fd);
+  if(ret) {
+    close(door_fd);
+    options_clear(&opt);
+    log_close();
+    exit(-1);
+  }
+  
 
   int cmd_listen_fd = init_command_socket(opt.command_sock_);
   if(cmd_listen_fd < 0) {