From a6afa899228cdd6a42a77bd5f75471871c2ef026 Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Tue, 25 Mar 2014 17:57:04 +0000 Subject: moved heartbeat client to new paradigm diff --git a/heartbeatclient.c b/heartbeatclient.c index 0cc03b7..3fe8423 100644 --- a/heartbeatclient.c +++ b/heartbeatclient.c @@ -33,15 +33,8 @@ #include "daemon.h" #include "utils.h" -int process_cmd(const char* cmd, int fd, cmd_t **cmd_q, client_t* client_lst, options_t* opt) +int process_serial(read_buffer_t* buffer, int serial_fd, int led_pipe_fd, bool *state) { - return 0; -} - -int process_serial(read_buffer_t* buffer, int serial_fd, u_int32_t* time, int led_pipe_fd) -{ - log_printf(DEBUG, "received heartbeat data at %d 1/10 s", *time); - int ret = 0; struct timeval tv; fd_set fds; @@ -72,8 +65,7 @@ int process_serial(read_buffer_t* buffer, int serial_fd, u_int32_t* time, int le buffer->buf[buffer->offset-1] = 0; if(strlen((char *)(buffer->buf))) { - log_printf(INFO, "heartbeat: '%s'", buffer->buf); - *time = 0; + *state = TRUE; if(led_pipe_fd >= 0) { char buf = '1'; ret = write(led_pipe_fd, &buf, 1); @@ -124,7 +116,9 @@ int main_loop(int serial_fd, int cmd_fd, int led_pipe_fd, options_t* opt) return_value = 0; } - int active = 1; + bool state = FALSE; + send_string(cmd_fd, "heartbeat 0\n"); + read_buffer_t serial_buffer; serial_buffer.offset = 0; struct timeval timeout; @@ -143,15 +137,13 @@ int main_loop(int serial_fd, int cmd_fd, int led_pipe_fd, options_t* opt) if(ret == -1) continue; if(!ret) { - if(active == 0) - continue; - time++; if(time >= opt->timeout_) { - log_printf(WARNING, "timeout reached, switching to standby"); - send_string(cmd_fd, "mode standby\n"); - active = 0; - log_printf(NOTICE, "deactivated timeout"); + if(state == TRUE) { + log_printf(WARNING, "heartbeat timeout"); + send_string(cmd_fd, "heartbeat 0\n"); + state = FALSE; + } time = 0; } continue; @@ -161,18 +153,18 @@ int main_loop(int serial_fd, int cmd_fd, int led_pipe_fd, options_t* opt) if(signal_handle()) return_value = 1; - if(reset_flag) { - active = 1; - time = 0; - reset_flag = 0; - log_printf(NOTICE, "reactivated timeout, switching to master"); - send_string(cmd_fd, "mode master\n"); - } - if(FD_ISSET(serial_fd, &tmpfds)) { - return_value = process_serial(&serial_buffer, serial_fd, &time, led_pipe_fd); + bool old_state = state; + return_value = process_serial(&serial_buffer, serial_fd, led_pipe_fd, &state); if(return_value) break; + if(state) { + if(!old_state) { + log_printf(WARNING, "heartbeat returned"); + send_string(cmd_fd, "heartbeat 1\n"); + } + time = 0; + } } if(FD_ISSET(cmd_fd, &tmpfds)) { @@ -197,6 +189,7 @@ int led_process(int pipe_fd, int led_fd) char buf; int ret; + log_printf(INFO, "led_process: starting up"); for(;;) { ret = read(pipe_fd, &buf, 1); if(ret == -1 && errno == EAGAIN) @@ -228,6 +221,7 @@ int led_process(int pipe_fd, int led_fd) break; } } + log_printf(INFO, "led_process: stopped"); return -1; } -- cgit v0.10.2