changed temp/photo listener to sensor listener
[svn42.git] / powersensordaemon / powersensordaemon.c
index 0c79e94..8b12743 100644 (file)
@@ -76,6 +76,25 @@ int init_command_socket(const char* path)
   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)
@@ -244,16 +263,14 @@ int process_cmd(char* cmd, int fd, cmd_t **cmd_q, client_t* client_lst, options_
       if(!param || !strncmp(param, "all", 3)) {
         listener->request_listener = 1;
         listener->error_listener = 1;
-        listener->temp_listener = 1;
-        listener->photo_listener = 1;
+        listener->sensor_listener = 1;
         listener->movement_listener = 1;
         listener->button_listener = 1;
       }
       else if(!strncmp(param, "none", 4)) {
         listener->request_listener = 0;
         listener->error_listener = 0;
-        listener->temp_listener = 0;
-        listener->photo_listener = 0;
+        listener->sensor_listener = 0;
         listener->movement_listener = 0;
         listener->button_listener = 0;
       }
@@ -261,10 +278,8 @@ int process_cmd(char* cmd, int fd, cmd_t **cmd_q, client_t* client_lst, options_
         listener->request_listener = 1;
       else if(!strncmp(param, "error", 5))
         listener->error_listener = 1;
-      else if(!strncmp(param, "temp", 4))
-        listener->temp_listener = 1;      
-      else if(!strncmp(param, "photo", 5))
-        listener->photo_listener = 1;      
+      else if(!strncmp(param, "sensor", 6))
+        listener->sensor_listener = 1;      
       else if(!strncmp(param, "movement", 8))
         listener->movement_listener = 1;      
       else if(!strncmp(param, "button", 6))
@@ -363,11 +378,11 @@ int process_tty(read_buffer_t* buffer, int tty_fd, cmd_t **cmd_q, client_t* clie
       }
 
       if(!strncmp(buffer->buf, "PanicButton", 11)) {
-        SEND_TO_LISTENER(button_listener, "panic buttont", cmd_fd, buffer->buf);
+        SEND_TO_LISTENER(button_listener, "panic button", cmd_fd, buffer->buf);
       }
 
-      if(!strncmp(buffer->buf, "Temp ", 5)) {
-        SEND_TO_LISTENER(temp_listener, "", cmd_fd, buffer->buf);
+      if(!strncmp(buffer->buf, "Sensor ", 7)) {
+        SEND_TO_LISTENER(sensor_listener, "", cmd_fd, buffer->buf);
       }
 
       cmd_pop(cmd_q);
@@ -386,17 +401,24 @@ int process_tty(read_buffer_t* buffer, int tty_fd, cmd_t **cmd_q, client_t* clie
   return ret;
 }
 
-int main_loop(int tty_fd, int cmd_listen_fd, int autosample_fd, options_t* opt)
+int main_loop(int tty_fd, int cmd_listen_fd, autosample_process_t* autosample, options_t* opt)
 {
   log_printf(NOTICE, "entering main loop");
 
   fd_set readfds, tmpfds;
   FD_ZERO(&readfds);
+
+  clear_fd(tty_fd);
   FD_SET(tty_fd, &readfds);
   FD_SET(cmd_listen_fd, &readfds);
   int max_fd = tty_fd > cmd_listen_fd ? tty_fd : cmd_listen_fd;
-  FD_SET(autosample_fd, &readfds);
-  max_fd = (max_fd < autosample_fd) ? autosample_fd : max_fd;
+
+  int autosample_enabled = 0;
+  if(autosample->pid_ > 0) {
+    clear_fd(autosample->write_fd_);
+    FD_SET(autosample->write_fd_, &readfds);
+    max_fd = (max_fd < autosample->write_fd_) ? autosample->write_fd_ : max_fd;
+  }
   cmd_t* cmd_q = NULL;
   client_t* client_lst = NULL;
 
@@ -462,11 +484,11 @@ int main_loop(int tty_fd, int cmd_listen_fd, int autosample_fd, options_t* opt)
       client_add(&client_lst, new_fd);
     }
 
-    if(FD_ISSET(autosample_fd, &tmpfds)) {
-      return_value = nonblock_readline(&autosample_buffer, autosample_fd, &cmd_q, client_lst, opt);
+    if(autosample->pid_ > 0 && FD_ISSET(autosample->write_fd_, &tmpfds)) {
+      return_value = nonblock_readline(&autosample_buffer, autosample->write_fd_, &cmd_q, client_lst, opt);
       if(return_value == 2) {
         log_printf(WARNING, "autosample not running, removing pipe to it");
-        FD_CLR(autosample_fd, &readfds);
+        FD_CLR(autosample->write_fd_, &readfds);
         return_value = 0;
         continue;
       }
@@ -496,6 +518,25 @@ int main_loop(int tty_fd, int cmd_listen_fd, int autosample_fd, options_t* opt)
 
     if(cmd_q && !cmd_q->sent)
       send_command(tty_fd, cmd_q);
+
+    if(autosample->pid_ > 0) {
+      lst = client_lst;
+      int listener_cnt = 0;
+      while(lst) {
+        if(lst->sensor_listener)
+          listener_cnt++;
+        lst = lst->next;
+      }
+      if((!autosample_enabled && listener_cnt > 0) ||
+         (autosample_enabled && listener_cnt == 0)) {
+        if(autosample_enabled) autosample_enabled = 0;
+        else autosample_enabled = 1;
+        int ret;
+        do {
+          ret = write(autosample->read_fd_, &autosample_enabled, 1);
+        } while(!ret || (ret == -1 && errno == EINTR));
+      }
+    }
   }
 
   cmd_clear(&cmd_q);
@@ -540,21 +581,7 @@ int setup_tty(int fd)
     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;
 }
@@ -650,23 +677,14 @@ int main(int argc, char* argv[])
     fclose(pid_file);
   }
   
-  int autosample_fd = -1;
+  autosample_process_t autosample;
+  autosample.pid_ = -1;
+  autosample.write_fd_ = -1;
+  autosample.read_fd_ = -1;
   if(key_value_storage_length(&opt.autosampledevs_) > 0) {
     log_printf(NOTICE, "starting autosample process");
-    autosample_fd = start_autosample_process(&opt);
-    if(autosample_fd == -1) {
-      options_clear(&opt);
-      log_close();
-      exit(1);
-    }
-    else if(autosample_fd <= 0) {
-      if(!autosample_fd)
-        log_printf(NOTICE, "autosample process normal shutdown");
-      else if(autosample_fd == -2)
-        log_printf(NOTICE, "autosample shutdown after signal");
-      else
-        log_printf(NOTICE, "autosample shutdown after error");
-      
+    int ret = start_autosample_process(&opt, &autosample);
+    if(ret == -1) {
       options_clear(&opt);
       log_close();
       exit(1);
@@ -690,7 +708,7 @@ int main(int argc, char* argv[])
       if(ret)
         ret = 2;
       else
-        ret = main_loop(tty_fd, cmd_listen_fd, autosample_fd, &opt);
+        ret = main_loop(tty_fd, cmd_listen_fd, &autosample, &opt);
     }
 
     if(ret == 2) {
@@ -706,6 +724,8 @@ int main(int argc, char* argv[])
   close(cmd_listen_fd);
   if(tty_fd > 0)
     close(tty_fd);
+  if(autosample.pid_ > 0)
+    kill(autosample.pid_, SIGTERM);
 
   if(!ret)
     log_printf(NOTICE, "normal shutdown");