Arglllll
authorBernhard Tittelbach <xro@realraum.at>
Tue, 16 Mar 2010 04:56:43 +0000 (04:56 +0000)
committerBernhard Tittelbach <xro@realraum.at>
Tue, 16 Mar 2010 04:56:43 +0000 (04:56 +0000)
powersensordaemon/key_value_storage.c
powersensordaemon/key_value_storage.h
powersensordaemon/powersensordaemon.c

index db3c19b..de5e054 100644 (file)
@@ -77,7 +77,7 @@ 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)
+char const * key_value_storage_find_first_stringvalue(key_value_storage_t* stor, char const * value)
 {
   if(!stor || !value)
     return NULL;
index bec26f8..ce8c3c8 100644 (file)
@@ -35,7 +35,7 @@ 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);
+char const * key_value_storage_find_first_stringvalue(key_value_storage_t* stor, char const * 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 9ef8001..bf03330 100644 (file)
@@ -332,6 +332,7 @@ int nonblock_readline(read_buffer_t* buffer, int fd, cmd_t** cmd_q, client_t* cl
 
 int process_tty(read_buffer_t* buffer, int tty_fd, cmd_t **cmd_q, client_t* client_lst, options_t* opt)
 {
+  u_int8_t *response_data;
   int ret = 0;
   struct timeval tv;
   fd_set fds;
@@ -363,50 +364,54 @@ int process_tty(read_buffer_t* buffer, int tty_fd, cmd_t **cmd_q, client_t* clie
 
       log_printf(NOTICE, "firmware: %s", buffer->buf);      
 
+      /* modify response if necessary */
+      response_data = buffer->buf;
+      if(!strncmp(buffer->buf, "Sensor ", 7)) {
+        if (buffer->buf[7] != 0)
+        {
+          char *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 *rev_lookuped_output;
+              if (asprintf(&rev_lookuped_output, "%s%s", sampledev_name, &(buffer->buf[8]) ))
+                response_data = rev_lookuped_output;
+            }
+            else
+              log_printf(WARNING, "unknown sample device key '%s' encountered", sampledev_key);
+            free(sampledev_key);
+          }
+        }
+      }
+
       int cmd_fd = -1;
       if(cmd_q && (*cmd_q)) {
         cmd_fd = (*cmd_q)->fd;
-        send_response(cmd_fd, buffer->buf);
+        send_response(cmd_fd, response_data);
       }
       
       if(!strncmp(buffer->buf, "Error:", 6)) {
-        SEND_TO_LISTENER(error_listener, "error", cmd_fd, buffer->buf);
+        SEND_TO_LISTENER(error_listener, "error", cmd_fd, response_data);
       }
       
       if(!strncmp(buffer->buf, "movement", 8)) {
-        SEND_TO_LISTENER(movement_listener, "movement", cmd_fd, buffer->buf);
+        SEND_TO_LISTENER(movement_listener, "movement", cmd_fd, response_data);
       }
 
       if(!strncmp(buffer->buf, "PanicButton", 11)) {
-        SEND_TO_LISTENER(button_listener, "panic button", cmd_fd, buffer->buf);
+        SEND_TO_LISTENER(button_listener, "panic button", cmd_fd, response_data);
       }
 
       if(!strncmp(buffer->buf, "Sensor ", 7)) {
-        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);
-          }
-        }
+        SEND_TO_LISTENER(sensor_listener, "sensor data", cmd_fd, response_data);
       }
 
+      /* free allocated buffer if response was modified */
+      if (response_data != buffer->buf)
+        free(response_data);
+
       cmd_pop(cmd_q);
       buffer->offset = 0;
       return 0;