#include "log.h"
#include <stdlib.h>
+#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#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];
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;
}
}
};
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);
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)
{
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);
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;
}