*** upsguru.c Sat Jun 21 10:09:12 2003 --- src/upsguru.c Mon Oct 6 17:27:01 2003 *************** *** 51,56 **** --- 51,58 ---- static struct init_request telinit; static char *psname; static char *tty; + char shutcommand[9]={0}; + int timeout=-1; static char log_string[LOG_LEN]; static ups_status_t status; static FILE *status_file = NULL; *************** *** 59,65 **** void *cleanup(int signal); void log_it(int priority, char *msg); void reset_tty(void); ! int poll_ups(void); void output_status(FILE *stream); void report_status(void); int process_status(char *status_string); --- 61,67 ---- void *cleanup(int signal); void log_it(int priority, char *msg); void reset_tty(void); ! int poll_ups(char *command); void output_status(FILE *stream); void report_status(void); int process_status(char *status_string); *************** *** 70,81 **** pid_t pid; ups_status_t last_status; psname = argv[0]; ! if (argc != 2) { ! fprintf(stderr, "usage: upsguru tty\n"); exit(EXIT_FAILURE); } tty = argv[1]; switch (pid = fork()) { case 0: /* child */ --- 72,97 ---- pid_t pid; ups_status_t last_status; psname = argv[0]; ! if (argc < 2) { ! fprintf(stderr, "usage: upsguru tty [timeout]\n"); exit(EXIT_FAILURE); } tty = argv[1]; + if (argc > 2) + { + timeout=atoi(argv[2]); + if (timeout < 1 || timeout > 99) + { + fprintf(stderr, "Incorrect timeout: must be 1-99\n"); + exit(EXIT_FAILURE); + } + strcpy(shutcommand,"S"); + if (strlen(argv[2]) < 2) strcat(shutcommand,"0"); + strcat(shutcommand,argv[2]); + strcat(shutcommand,"R0001\r"); + } + switch (pid = fork()) { case 0: /* child */ *************** *** 115,120 **** --- 131,137 ---- snprintf(log_string, LOG_LEN - 1, "UPS status available in %s\n", POWER_STATUS); log_it(LOG_INFO, log_string); + reset_tty(); while(run) *************** *** 122,128 **** #ifdef DEBUG fprintf(stderr, "Polling UPS...\n"); #endif /* DEBUG */ ! poll_ups(); #ifdef DEBUG fprintf(stderr, "UPS polled\n"); #endif /* DEBUG */ --- 139,145 ---- #ifdef DEBUG fprintf(stderr, "Polling UPS...\n"); #endif /* DEBUG */ ! poll_ups(REQUEST); #ifdef DEBUG fprintf(stderr, "UPS polled\n"); #endif /* DEBUG */ *************** *** 204,212 **** #endif /* DEBUG */ } ! int poll_ups(void) { ! const char *command = REQUEST; char status[80]; int bytes_written; --- 221,229 ---- #endif /* DEBUG */ } ! int poll_ups(char *command) { ! // const char *command = REQUEST; char status[80]; int bytes_written; *************** *** 214,220 **** bytes_written = write_serial(command, strlen(command)); ! if(bytes_written > 0) { #ifdef DEBUG fprintf(stderr, "Read serial..."); --- 231,237 ---- bytes_written = write_serial(command, strlen(command)); ! if(bytes_written > 0 && command[0]!='S') { #ifdef DEBUG fprintf(stderr, "Read serial..."); *************** *** 276,281 **** --- 293,305 ---- snprintf(log_string, LOG_LEN - 1, "Failed to notify init of INIT_CMD_POWERFAILNOW\n"); log_it(LOG_CRIT, log_string); } + else + if (shutcommand[0]=='S') { + poll_ups(shutcommand);poll_ups(REQUEST); + snprintf(log_string, LOG_LEN - 1, "Warning! UPS will be SHUTDOWN after %ld minutes\n", timeout); + log_it(LOG_INFO, log_string); + } + /* Give up and die gracefully, there's no hope :( */ run = 0; return; *************** *** 294,309 **** snprintf(log_string, LOG_LEN - 1, "Failed to notify init of INIT_CMD_POWERFAIL\n"); log_it(LOG_CRIT, log_string); } } } else { if(status.in_powerfail) ! { status.in_powerfail = 0; time_down = time(NULL) - fail_time; - snprintf(log_string, LOG_LEN - 1, "Mains power has returned\n"); - log_it(LOG_CRIT, log_string); if (time_down >= 60) { --- 318,342 ---- snprintf(log_string, LOG_LEN - 1, "Failed to notify init of INIT_CMD_POWERFAIL\n"); log_it(LOG_CRIT, log_string); } + else + if (shutcommand[0]=='S') { + poll_ups(shutcommand);poll_ups(REQUEST); + snprintf(log_string, LOG_LEN - 1, "Warning! UPS will be SHUTDOWN after %ld minutes\n", timeout); + log_it(LOG_INFO, log_string); + } } } else { if(status.in_powerfail) ! { ! if (shutcommand[0]=='S') { ! reset_tty(); ! snprintf(log_string, LOG_LEN - 1, "UPS shuttdown cancelled\n"); ! log_it(LOG_CRIT, log_string); ! } status.in_powerfail = 0; time_down = time(NULL) - fail_time; if (time_down >= 60) {