From bf24462606f32226099680522eb44ac12c274291 Mon Sep 17 00:00:00 2001 From: Bernhard Tittelbach Date: Tue, 16 Mar 2010 02:44:40 +0000 Subject: [PATCH] SampleDevice Reverse Lookup --- powersensordaemon/key_value_storage.c | 19 +++++++++++++++++++ powersensordaemon/key_value_storage.h | 2 ++ powersensordaemon/options.c | 1 - powersensordaemon/powersensordaemon.c | 32 +++++++++++++++++++++++++++----- 4 files changed, 48 insertions(+), 6 deletions(-) diff --git a/powersensordaemon/key_value_storage.c b/powersensordaemon/key_value_storage.c index 118be99..db3c19b 100644 --- a/powersensordaemon/key_value_storage.c +++ b/powersensordaemon/key_value_storage.c @@ -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) diff --git a/powersensordaemon/key_value_storage.h b/powersensordaemon/key_value_storage.h index 9fa701c..bec26f8 100644 --- a/powersensordaemon/key_value_storage.h +++ b/powersensordaemon/key_value_storage.h @@ -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); diff --git a/powersensordaemon/options.c b/powersensordaemon/options.c index 4d64b3e..51aabd9 100644 --- a/powersensordaemon/options.c +++ b/powersensordaemon/options.c @@ -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; diff --git a/powersensordaemon/powersensordaemon.c b/powersensordaemon/powersensordaemon.c index 8b12743..9ef8001 100644 --- a/powersensordaemon/powersensordaemon.c +++ b/powersensordaemon/powersensordaemon.c @@ -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; } -- 1.7.10.4