From: Bernhard Tittelbach Date: Tue, 16 Mar 2010 04:56:43 +0000 (+0000) Subject: Arglllll X-Git-Url: https://git.realraum.at/?a=commitdiff_plain;h=199dad6842c2a42184383ee956b2f08957874fb9;p=svn42.git Arglllll --- diff --git a/powersensordaemon/key_value_storage.c b/powersensordaemon/key_value_storage.c index db3c19b..de5e054 100644 --- a/powersensordaemon/key_value_storage.c +++ b/powersensordaemon/key_value_storage.c @@ -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; diff --git a/powersensordaemon/key_value_storage.h b/powersensordaemon/key_value_storage.h index bec26f8..ce8c3c8 100644 --- a/powersensordaemon/key_value_storage.h +++ b/powersensordaemon/key_value_storage.h @@ -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); diff --git a/powersensordaemon/powersensordaemon.c b/powersensordaemon/powersensordaemon.c index 9ef8001..bf03330 100644 --- a/powersensordaemon/powersensordaemon.c +++ b/powersensordaemon/powersensordaemon.c @@ -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;