autosample sends actual command now
authorChristian Pointner <equinox@realraum.at>
Sun, 7 Mar 2010 20:29:18 +0000 (20:29 +0000)
committerChristian Pointner <equinox@realraum.at>
Sun, 7 Mar 2010 20:29:18 +0000 (20:29 +0000)
powersensordaemon/autosample.c
powersensordaemon/autosample.h
powersensordaemon/powersensordaemon.c

index 69b9184..b79e1b9 100644 (file)
@@ -25,6 +25,7 @@
 #include "log.h"
 
 #include <stdlib.h>
 #include "log.h"
 
 #include <stdlib.h>
+#include <stdio.h>
 #include <unistd.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <unistd.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 
 #include "autosample.h"
  
 
 #include "autosample.h"
  
+int send_sample_cmd(int fd, const char* device_name)
+{
+  if(!device_name)
+    return -1;
+
+  char* buf;
+  int len = asprintf(&buf, "sample %s\n", device_name);
+  if(len <= 0)
+    return len;
+  int offset = 0;
+  int ret;
+  for(;;) {
+    ret = write(fd, &buf[offset], len - offset);
+    if(ret < 0) {
+      if(errno != EINTR) {
+        free(buf);
+        return ret;
+      }
+
+      ret = 0;
+    }
+
+    offset += ret;
+    if(offset+1 >= len)
+      break;
+  }
+  free(buf);
+
+  if(ret > 0)
+    return 0;
+
+  return ret;
+}
+
 int start_autosample_process(options_t* opt)
 {
   int pipefd[2];
 int start_autosample_process(options_t* opt)
 {
   int pipefd[2];
@@ -112,7 +147,7 @@ int autosample_process(options_t *opt, int pipefd)
         devices[i].cnt_++;
         if(devices[i].cnt_ >= devices[i].delay_) {
           log_printf(DEBUG, "autosample send sample command for '%s'", devices[i].device_name_);
         devices[i].cnt_++;
         if(devices[i].cnt_ >= devices[i].delay_) {
           log_printf(DEBUG, "autosample send sample command for '%s'", devices[i].device_name_);
-              // call send sample
+          send_sample_cmd(pipefd, devices[i].device_name_);
           devices[i].cnt_ = 0;
         }
       }
           devices[i].cnt_ = 0;
         }
       }
index 396b130..4ea195e 100644 (file)
@@ -31,6 +31,7 @@ struct autosample_device_struct {
 };
 typedef struct autosample_device_struct autosample_device_t;
 
 };
 typedef struct autosample_device_struct autosample_device_t;
 
+int send_sample_cmd(int fd, const char* device_name);
 int start_autosample_process(options_t* opt);
 int autosample_process(options_t *opt, int pipefd);
 
 int start_autosample_process(options_t* opt);
 int autosample_process(options_t *opt, int pipefd);
 
index 0c79e94..1aee114 100644 (file)
@@ -76,6 +76,25 @@ int init_command_socket(const char* path)
   return fd;
 }
 
   return fd;
 }
 
+void clear_fd(int fd)
+{
+  fd_set fds;
+  struct timeval tv;
+  FD_ZERO(&fds);
+  FD_SET(fd, &fds);
+  tv.tv_sec = 0;
+  tv.tv_usec = 50000;
+  for(;;) {
+    int ret = select(fd+1, &fds, NULL, NULL, &tv);
+    if(ret > 0) {
+      char buffer[100];
+      ret = read(fd, buffer, sizeof(buffer));
+    }
+    else
+      break;
+  }
+}
+
 int send_command(int tty_fd, cmd_t* cmd)
 {
   if(!cmd)
 int send_command(int tty_fd, cmd_t* cmd)
 {
   if(!cmd)
@@ -390,6 +409,9 @@ int main_loop(int tty_fd, int cmd_listen_fd, int autosample_fd, options_t* opt)
 {
   log_printf(NOTICE, "entering main loop");
 
 {
   log_printf(NOTICE, "entering main loop");
 
+  clear_fd(tty_fd);
+  clear_fd(autosample_fd);
+
   fd_set readfds, tmpfds;
   FD_ZERO(&readfds);
   FD_SET(tty_fd, &readfds);
   fd_set readfds, tmpfds;
   FD_ZERO(&readfds);
   FD_SET(tty_fd, &readfds);
@@ -540,21 +562,7 @@ int setup_tty(int fd)
     return ret;
   }
 
     return ret;
   }
 
-  fd_set fds;
-  struct timeval tv;
-  FD_ZERO(&fds);
-  FD_SET(fd, &fds);
-  tv.tv_sec = 0;
-  tv.tv_usec = 50000;
-  for(;;) {
-    ret = select(fd+1, &fds, NULL, NULL, &tv);
-    if(ret > 0) {
-      char buffer[100];
-      ret = read(fd, buffer, sizeof(buffer));
-    }
-    else
-      break;
-  }
+  clear_fd(fd);
 
   return 0;
 }
 
   return 0;
 }