diff options
Diffstat (limited to 'switchctl.c')
-rw-r--r-- | switchctl.c | 52 |
1 files changed, 47 insertions, 5 deletions
diff --git a/switchctl.c b/switchctl.c index 9650674..d7a7002 100644 --- a/switchctl.c +++ b/switchctl.c @@ -299,14 +299,13 @@ int process_cmd(const char* cmd, int fd, cmd_t **cmd_q, client_t* client_lst, op return 0; } -int process_switch(int switch_fd) +int process_switch(read_buffer_t* buffer, int switch_fd, cmd_t **cmd_q, client_t* client_lst) { int ret = 0; struct timeval tv; fd_set fds; FD_ZERO(&fds); FD_SET(switch_fd, &fds); - char buf[1]; for(;;) { tv.tv_sec = 0; @@ -317,13 +316,53 @@ int process_switch(int switch_fd) else if(ret < 0) return ret; - ret = read(switch_fd, buf, 1); + ret = read(switch_fd, &buffer->buf[buffer->offset], 1); if(!ret) return 2; if(ret == -1 && errno == EAGAIN) return 0; else if(ret < 0) break; + + if(buffer->buf[buffer->offset] == '\n') { + buffer->buf[buffer->offset] = 0; + + if(buffer->offset > 0 && buffer->buf[buffer->offset-1] == '\r') + buffer->buf[buffer->offset-1] = 0; + + log_printf(NOTICE, "switch-firmware: %s", buffer->buf); + + int cmd_fd = -1; + if(cmd_q && (*cmd_q)) { + cmd_fd = (*cmd_q)->fd; + send_response(cmd_fd, buffer->buf); + } + + if(!strncmp(buffer->buf, "S0L", 3)) { + client_t* client; + int listener_cnt = 0; + for(client = client_lst; client; client = client->next) + if(client->status_listener && client->fd != cmd_fd) { + send_response(client->fd, buffer->buf); + listener_cnt++; + } + log_printf(DEBUG, "sent status to %d additional listeners", listener_cnt); + } + + if((!strncmp(buffer->buf, "RRR", 3)) || + (!strncmp(buffer->buf, "EEE", 3))) + cmd_pop(cmd_q); + + buffer->offset = 0; + return 0; + } + + buffer->offset++; + if(buffer->offset >= sizeof(buffer->buf)) { + log_printf(DEBUG, "string too long (fd=%d)", switch_fd); + buffer->offset = 0; + return 0; + } } return ret; @@ -341,6 +380,9 @@ int main_loop(int switch_fd, int cmd_listen_fd, options_t* opt) cmd_t* cmd_q = NULL; client_t* client_lst = NULL; + read_buffer_t switch_buffer; + switch_buffer.offset = 0; + int sig_fd = signal_init(); if(sig_fd < 0) return -1; @@ -379,7 +421,7 @@ int main_loop(int switch_fd, int cmd_listen_fd, options_t* opt) } if(FD_ISSET(switch_fd, &tmpfds)) { - return_value = process_switch(switch_fd); + return_value = process_switch(&switch_buffer, switch_fd, &cmd_q, client_lst); if(return_value) break; } @@ -532,7 +574,7 @@ int main(int argc, char* argv[]) if(switch_fd < 0) ret = 2; else { - ret = setup_tty(switch_fd, B9600); + ret = setup_tty(switch_fd, B19200); if(ret) ret = 2; else |