From: Christian Pointner Date: Sun, 7 Mar 2010 20:29:18 +0000 (+0000) Subject: autosample sends actual command now X-Git-Url: https://git.realraum.at/?a=commitdiff_plain;h=c62c39411cd07099da6ca5998e98b3123d15095c;p=svn42.git autosample sends actual command now --- diff --git a/powersensordaemon/autosample.c b/powersensordaemon/autosample.c index 69b9184..b79e1b9 100644 --- a/powersensordaemon/autosample.c +++ b/powersensordaemon/autosample.c @@ -25,6 +25,7 @@ #include "log.h" #include +#include #include #include #include @@ -33,6 +34,40 @@ #include "autosample.h" +int send_sample_cmd(int fd, const char* device_name) +{ + if(!device_name) + return -1; + + char* buf; + int len = asprintf(&buf, "sample %s\n", device_name); + if(len <= 0) + return len; + int offset = 0; + int ret; + for(;;) { + ret = write(fd, &buf[offset], len - offset); + if(ret < 0) { + if(errno != EINTR) { + free(buf); + return ret; + } + + ret = 0; + } + + offset += ret; + if(offset+1 >= len) + break; + } + free(buf); + + if(ret > 0) + return 0; + + return ret; +} + int start_autosample_process(options_t* opt) { int pipefd[2]; @@ -112,7 +147,7 @@ int autosample_process(options_t *opt, int pipefd) 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 + send_sample_cmd(pipefd, devices[i].device_name_); devices[i].cnt_ = 0; } } diff --git a/powersensordaemon/autosample.h b/powersensordaemon/autosample.h index 396b130..4ea195e 100644 --- a/powersensordaemon/autosample.h +++ b/powersensordaemon/autosample.h @@ -31,6 +31,7 @@ struct autosample_device_struct { }; typedef struct autosample_device_struct autosample_device_t; +int send_sample_cmd(int fd, const char* device_name); int start_autosample_process(options_t* opt); int autosample_process(options_t *opt, int pipefd); diff --git a/powersensordaemon/powersensordaemon.c b/powersensordaemon/powersensordaemon.c index 0c79e94..1aee114 100644 --- a/powersensordaemon/powersensordaemon.c +++ b/powersensordaemon/powersensordaemon.c @@ -76,6 +76,25 @@ int init_command_socket(const char* path) return fd; } +void clear_fd(int fd) +{ + fd_set fds; + struct timeval tv; + FD_ZERO(&fds); + FD_SET(fd, &fds); + tv.tv_sec = 0; + tv.tv_usec = 50000; + for(;;) { + int ret = select(fd+1, &fds, NULL, NULL, &tv); + if(ret > 0) { + char buffer[100]; + ret = read(fd, buffer, sizeof(buffer)); + } + else + break; + } +} + int send_command(int tty_fd, cmd_t* cmd) { if(!cmd) @@ -390,6 +409,9 @@ int main_loop(int tty_fd, int cmd_listen_fd, int autosample_fd, options_t* opt) { log_printf(NOTICE, "entering main loop"); + clear_fd(tty_fd); + clear_fd(autosample_fd); + fd_set readfds, tmpfds; FD_ZERO(&readfds); FD_SET(tty_fd, &readfds); @@ -540,21 +562,7 @@ int setup_tty(int fd) return ret; } - fd_set fds; - struct timeval tv; - FD_ZERO(&fds); - FD_SET(fd, &fds); - tv.tv_sec = 0; - tv.tv_usec = 50000; - for(;;) { - ret = select(fd+1, &fds, NULL, NULL, &tv); - if(ret > 0) { - char buffer[100]; - ret = read(fd, buffer, sizeof(buffer)); - } - else - break; - } + clear_fd(fd); return 0; }