added autosample parameter
authorChristian Pointner <equinox@realraum.at>
Sun, 7 Mar 2010 20:11:44 +0000 (20:11 +0000)
committerChristian Pointner <equinox@realraum.at>
Sun, 7 Mar 2010 20:11:44 +0000 (20:11 +0000)
powersensordaemon/autosample.c
powersensordaemon/autosample.h
powersensordaemon/key_value_storage.c
powersensordaemon/key_value_storage.h
powersensordaemon/options.c
powersensordaemon/options.h
powersensordaemon/powersensordaemon.c

index bcba8c2..69b9184 100644 (file)
@@ -24,6 +24,7 @@
 #include "options.h"
 #include "log.h"
 
+#include <stdlib.h>
 #include <unistd.h>
 #include <sys/types.h>
 #include <sys/stat.h>
@@ -63,6 +64,29 @@ int autosample_process(options_t *opt, int pipefd)
 {
   log_printf(NOTICE, "autosample process just started");
 
+  int device_num = key_value_storage_length(&opt->autosampledevs_);
+  if(device_num <= 0) {
+    log_printf(WARNING, "autosample no devices to sample, exiting");
+    return 0;
+  }
+
+  autosample_device_t* devices = malloc(sizeof(autosample_device_t)*device_num);
+  if(!devices) {
+    log_printf(WARNING, "autosample memory error, exiting");
+    return -3;
+  }
+
+  int i = 0;
+  string_list_element_t* k = opt->autosampledevs_.keys_.first_;
+  string_list_element_t* v = opt->autosampledevs_.values_.first_;
+  while(k && v) {
+    devices[i].delay_ = atoi(v->string_);
+    devices[i].cnt_ = 0;
+    devices[i].device_name_ = k->string_;
+    k = k->next_;
+    v = v->next_;
+  }
+
   int sig_fd = signal_init();
   if(sig_fd < 0)
     return -3;
@@ -83,10 +107,15 @@ int autosample_process(options_t *opt, int pipefd)
     if(ret == -1)
       continue;
     if(!ret) {
-          // timout has expired...
-      write(pipefd, "sample temp0", 12);
-      char c = '\n';
-      write(pipefd, &c, 1);
+      int i;
+      for(i = 0; i < device_num; i++) {
+        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
+          devices[i].cnt_ = 0;
+        }
+      }
     }
 
     if(FD_ISSET(sig_fd, &readfds)) {
@@ -98,6 +127,7 @@ int autosample_process(options_t *opt, int pipefd)
   }
 
   signal_stop();
+  free(devices);
   return return_value;
 }
 
index ee3b2f8..396b130 100644 (file)
 
 #include "options.h"
 
+struct autosample_device_struct {
+  int delay_;
+  int cnt_;
+  char* device_name_;
+};
+typedef struct autosample_device_struct autosample_device_t;
+
 int start_autosample_process(options_t* opt);
 int autosample_process(options_t *opt, int pipefd);
 
index 7f4b584..118be99 100644 (file)
@@ -76,6 +76,21 @@ char* key_value_storage_find(key_value_storage_t* stor, const char* key)
   return NULL;
 }
 
+int key_value_storage_length(key_value_storage_t* stor)
+{
+  if(!stor)
+    return 0;
+
+  int length = 0;
+  string_list_element_t* k = stor->keys_.first_;
+  while(k) {
+    length++;
+    k = k->next_;
+  }
+
+  return length;
+}
+
 void key_value_storage_print(key_value_storage_t* stor, const char* head, const char* sep, const char* tail)
 {
   if(!stor)
index bf7a493..9fa701c 100644 (file)
@@ -34,6 +34,7 @@ void key_value_storage_init(key_value_storage_t* stor);
 void key_value_storage_clear(key_value_storage_t* stor);
 int key_value_storage_add(key_value_storage_t* stor, const char* key, const char* value);
 char* key_value_storage_find(key_value_storage_t* stor, const char* key);
+int key_value_storage_length(key_value_storage_t* stor);
 
 void key_value_storage_print(key_value_storage_t* stor, const char* head, const char* sep, const char* tail);
 
index 3411f9d..c0addda 100644 (file)
       i++;                                               \
     }
 
+#define PARSE_KEY_VALUE(SHORT, LONG, SEP, KV)            \
+    else if(!strcmp(str,SHORT) || !strcmp(str,LONG))     \
+    {                                                    \
+      if(argc < 1 || argv[i+1][0] == '-')                \
+        return i;                                        \
+      char* value = strchr(argv[i+1], SEP);              \
+      if(!value || value[1] == 0)                        \
+        return i+1;                                      \
+      value[0] = 0;                                      \
+      value++;                                           \
+      if(key_value_storage_add(&KV, argv[i+1], value))   \
+        return -2;                                       \
+      argc--;                                            \
+      i++;                                               \
+    }
+
 int options_parse_hex_string(const char* hex, buffer_t* buffer)
 {
   if(!hex || !buffer)
@@ -176,6 +192,8 @@ int options_parse(options_t* opt, int argc, char* argv[])
     PARSE_STRING_PARAM("-s","--socket", opt->command_sock_)
     PARSE_STRING_PARAM("-p","--powerid-file", opt->powerid_file_)
     PARSE_STRING_PARAM("-a","--sampledev-file", opt->sampledev_file_)
+    PARSE_STRING_PARAM("-a","--sampledev-file", opt->sampledev_file_)
+    PARSE_KEY_VALUE("-A","--autosampledev", ',', opt->autosampledevs_)
     else 
       return i;
   }
@@ -229,6 +247,23 @@ int options_parse_post(options_t* opt)
     if(ret)
       return ret;
   }
+
+  string_list_element_t* k = opt->autosampledevs_.keys_.first_;
+  string_list_element_t* v = opt->autosampledevs_.values_.first_;
+  while(k && v) {
+    if(!key_value_storage_find(&opt->sampledevs_, k->string_)) {
+      log_printf(ERROR, "sample dev '%s' not in file '%s'", k->string_, opt->sampledev_file_);
+      return -1;
+    }
+    if(atoi(v->string_) <= 0) {
+      log_printf(ERROR, "invalid rate '%s' for sample dev '%s'", v->string_, k->string_);
+      return -1;
+    }
+    k = k->next_;
+    v = v->next_;
+  }
+  
+  return 0;
 }
 
 void options_default(options_t* opt)
@@ -250,6 +285,7 @@ void options_default(options_t* opt)
   key_value_storage_init(&opt->powerids_);
   opt->sampledev_file_ = NULL;
   key_value_storage_init(&opt->sampledevs_);
+  key_value_storage_init(&opt->autosampledevs_);
 }
 
 void options_clear(options_t* opt)
@@ -279,6 +315,7 @@ void options_clear(options_t* opt)
   if(opt->sampledev_file_)
     free(opt->sampledev_file_);
   key_value_storage_clear(&opt->sampledevs_);
+  key_value_storage_clear(&opt->autosampledevs_);
 }
 
 void options_print_usage()
@@ -297,6 +334,8 @@ void options_print_usage()
   printf("            [-s|--command-sock] <unix sock>     the command socket e.g. /var/run/powersensordaemon/cmd.sock\n");
   printf("            [-p|--powerid-file] <path>          file that contains the power ids\n");
   printf("            [-a|--sampledev-file] <path>        file that contains all sample devices\n");
+  printf("            [-A|--autosampledev] <device>,<delay between samples in seconds>\n");
+  printf("                                                automatic sample this devices, can be invoked several times\n");
 }
 
 void options_print(options_t* opt)
@@ -321,4 +360,5 @@ void options_print(options_t* opt)
   printf("sampledev_file: '%s'\n", opt->sampledev_file_);
   printf("sampledevs: \n");
   key_value_storage_print(&opt->sampledevs_, "  '", "' -> '", "'\n");
+  key_value_storage_print(&opt->autosampledevs_, "  '", "' -> '", "'\n");
 }
index ab7b525..8ca8d3f 100644 (file)
@@ -40,6 +40,7 @@ struct options_struct {
   key_value_storage_t powerids_;
   char* sampledev_file_;
   key_value_storage_t sampledevs_;
+  key_value_storage_t autosampledevs_;
 };
 typedef struct options_struct options_t;
 
index 67158c3..0c79e94 100644 (file)
@@ -651,7 +651,7 @@ int main(int argc, char* argv[])
   }
   
   int autosample_fd = -1;
-//  if(opt.led_filename_) {
+  if(key_value_storage_length(&opt.autosampledevs_) > 0) {
     log_printf(NOTICE, "starting autosample process");
     autosample_fd = start_autosample_process(&opt);
     if(autosample_fd == -1) {
@@ -671,7 +671,7 @@ int main(int argc, char* argv[])
       log_close();
       exit(1);
     }
-//  }
+  }
 
   int cmd_listen_fd = init_command_socket(opt.command_sock_);
   if(cmd_listen_fd < 0) {