SampleDevice Reverse Lookup
authorBernhard Tittelbach <xro@realraum.at>
Tue, 16 Mar 2010 02:44:40 +0000 (02:44 +0000)
committerBernhard Tittelbach <xro@realraum.at>
Tue, 16 Mar 2010 02:44:40 +0000 (02:44 +0000)
powersensordaemon/key_value_storage.c
powersensordaemon/key_value_storage.h
powersensordaemon/options.c
powersensordaemon/powersensordaemon.c

index 118be99..db3c19b 100644 (file)
@@ -76,6 +76,25 @@ char* key_value_storage_find(key_value_storage_t* stor, const char* key)
   return NULL;
 }
 
+/* Warning: this function only works if you actually store \0-terminated strings as values!! */
+char* key_value_storage_find_first_stringvalue(key_value_storage_t* stor, const char* value)
+{
+  if(!stor || !value)
+    return NULL;
+
+  string_list_element_t* k = stor->keys_.first_;
+  string_list_element_t* v = stor->values_.first_;
+  while(v && k) {
+    if(!strcmp(v->string_, value))
+      return k->string_;
+    
+    k = k->next_;
+    v = v->next_;
+  }
+
+  return NULL;
+}
+
 int key_value_storage_length(key_value_storage_t* stor)
 {
   if(!stor)
index 9fa701c..bec26f8 100644 (file)
@@ -34,6 +34,8 @@ 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);
+/* Warning: this function only works if you actually store \0-terminated strings as values!! */
+char* key_value_storage_find_first_stringvalue(key_value_storage_t* stor, const char* value);
 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 4d64b3e..51aabd9 100644 (file)
@@ -192,7 +192,6 @@ 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;
index 8b12743..9ef8001 100644 (file)
@@ -211,7 +211,7 @@ int process_cmd(char* cmd, int fd, cmd_t **cmd_q, client_t* client_lst, options_
 
   if(cmd_id == POWER_ON || cmd_id == POWER_OFF) {
     char* orig_param = param;
-    param = key_value_storage_find(&opt->powerids_, param);
+    param = key_value_storage_find(&(opt->powerids_), param);
     if(!param) {
       send_response(fd, "Error: invalid power id");
       log_printf(WARNING, "invalid power id '%s' in command from %d", orig_param, fd);
@@ -220,7 +220,7 @@ int process_cmd(char* cmd, int fd, cmd_t **cmd_q, client_t* client_lst, options_
 
   if(cmd_id == SAMPLE) {
     char* orig_param = param;
-    param = key_value_storage_find(&opt->sampledevs_, param);
+    param = key_value_storage_find(&(opt->sampledevs_), param);
     if(!param) {
       send_response(fd, "Error: invalid sample device");
       log_printf(WARNING, "invalid sample device '%s' in command from %d", orig_param, fd);
@@ -330,7 +330,7 @@ int nonblock_readline(read_buffer_t* buffer, int fd, cmd_t** cmd_q, client_t* cl
   return ret;
 }
 
-int process_tty(read_buffer_t* buffer, int tty_fd, cmd_t **cmd_q, client_t* client_lst)
+int process_tty(read_buffer_t* buffer, int tty_fd, cmd_t **cmd_q, client_t* client_lst, options_t* opt)
 {
   int ret = 0;
   struct timeval tv;
@@ -382,7 +382,29 @@ int process_tty(read_buffer_t* buffer, int tty_fd, cmd_t **cmd_q, client_t* clie
       }
 
       if(!strncmp(buffer->buf, "Sensor ", 7)) {
-        SEND_TO_LISTENER(sensor_listener, "", cmd_fd, buffer->buf);
+        if (buffer->buf[7] != 0)
+        {
+          char const *sampledev_key;
+          if (asprintf(sampledev_key, "%c",buffer->buf[7]))
+          {
+            char const *sampledev_name = key_value_storage_find_first_stringvalue(&(opt->sampledevs_), sampledev_key);
+            if(sampledev_name)
+            {
+              char const *rev_lookuped_output;
+              if (asprintf(rev_lookuped_output, "%s%s", sampledev_name, &(buffer->buf[8])))
+              {
+                SEND_TO_LISTENER(sensor_listener, "", cmd_fd, rev_lookuped_output);
+                free((void*) rev_lookuped_output);
+              }
+            }
+            else
+            {
+              log_printf(WARNING, "unknown sample device key '%s' encountered", sampledev_key);
+              SEND_TO_LISTENER(sensor_listener, "", cmd_fd, buffer->buf);
+            }
+            free((void*) sampledev_key);
+          }
+        }
       }
 
       cmd_pop(cmd_q);
@@ -465,7 +487,7 @@ int main_loop(int tty_fd, int cmd_listen_fd, autosample_process_t* autosample, o
     }
    
     if(FD_ISSET(tty_fd, &tmpfds)) {
-      return_value = process_tty(&tty_buffer, tty_fd, &cmd_q, client_lst);
+      return_value = process_tty(&tty_buffer, tty_fd, &cmd_q, client_lst, opt);
       if(return_value)
         break;
     }