fixed pipe direction, added test sample command
[svn42.git] / powersensordaemon / autosample.c
index 24edfc6..bcba8c2 100644 (file)
@@ -51,21 +51,54 @@ int start_autosample_process(options_t* opt)
   }
   
   if (cpid == 0) {
-    close(pipefd[1]);
-    return autosample_process(opt, pipefd[0]);
+    close(pipefd[0]);
+    return autosample_process(opt, pipefd[1]);
   }
 
-  close(pipefd[0]);
-  return pipefd[1];
+  close(pipefd[1]);
+  return pipefd[0];
 }
 
 int autosample_process(options_t *opt, int pipefd)
 {
   log_printf(NOTICE, "autosample process just started");
 
-  sleep(5);
+  int sig_fd = signal_init();
+  if(sig_fd < 0)
+    return -3;
+
+  fd_set readfds;
+  struct timeval timeout;
+  int return_value = 0;
+  while(!return_value) {
+    FD_SET(sig_fd, &readfds);
+    timeout.tv_sec = 0;
+    timeout.tv_usec = 1000000;
+    int ret = select(sig_fd+1, &readfds, NULL, NULL, &timeout);
+    if(ret == -1 && errno != EINTR) {
+      log_printf(ERROR, "autosample process select returned with error: %s", strerror(errno));
+      return_value = -3;
+      break;
+    }
+    if(ret == -1)
+      continue;
+    if(!ret) {
+          // timout has expired...
+      write(pipefd, "sample temp0", 12);
+      char c = '\n';
+      write(pipefd, &c, 1);
+    }
+
+    if(FD_ISSET(sig_fd, &readfds)) {
+      if(signal_handle()) {
+        return_value = -2;
+        break;
+      }
+    } 
+  }
 
-  return 0;
+  signal_stop();
+  return return_value;
 }