117117#include <ctype.h>
118118
119119#define DRIVER_NAME "Tripp-Lite SmartUPS driver"
120- #define DRIVER_VERSION "0.99 "
120+ #define DRIVER_VERSION "0.100 "
121121
122122/* driver description structure */
123123upsdrv_info_t upsdrv_info = {
@@ -135,6 +135,8 @@ static unsigned int offdelay = DEFAULT_OFFDELAY;
135135static unsigned int startdelay = DEFAULT_STARTDELAY ;
136136static unsigned int bootdelay = DEFAULT_BOOTDELAY ;
137137
138+ static int reconnect_ups (void );
139+
138140static long hex2d (char * start , unsigned int len )
139141{
140142 char buf [32 ];
@@ -405,6 +407,23 @@ void upsdrv_shutdown(void)
405407 set_exit_flag (ret == STAT_INSTCMD_HANDLED ? EF_EXIT_SUCCESS : EF_EXIT_FAILURE );
406408}
407409
410+ static int reconnect_ups (void )
411+ {
412+ reconnect_trying (RECONNECT_TRYING );
413+ upslogx (LOG_WARNING , "Communications with UPS lost; attempting to re-establish the connection" );
414+
415+ upsdrv_cleanup ();
416+ upsdrv_initups ();
417+ if (INVALID_FD_SER (upsfd ))
418+ return 0 ;
419+
420+ reconnect_trying (RECONNECT_UPDATEINFO );
421+ upsdrv_initinfo ();
422+
423+ reconnect_trying (RECONNECT_SUCCESS );
424+ return 1 ;
425+ }
426+
408427void upsdrv_updateinfo (void )
409428{
410429 static int numfails ;
@@ -420,7 +439,10 @@ void upsdrv_updateinfo(void)
420439 ++ numfails ;
421440 if (numfails > MAXTRIES ) {
422441 ser_comm_fail ("Data command failed: [%" PRIiSIZE "] bytes != 21 bytes." , len );
423- dstate_datastale ();
442+ if (!reconnect_ups ()) {
443+ dstate_datastale ();
444+ }
445+ numfails = 0 ;
424446 }
425447 return ;
426448 }
@@ -441,7 +463,10 @@ void upsdrv_updateinfo(void)
441463 if (numfails > MAXTRIES ) {
442464 ser_comm_fail ("Data out of bounds: [%0ld,%3d,%3ld,%02.2f]" ,
443465 volt , temp , load , freq );
444- dstate_datastale ();
466+ if (!reconnect_ups ()) {
467+ dstate_datastale ();
468+ }
469+ numfails = 0 ;
445470 }
446471 return ;
447472 }
@@ -452,7 +477,10 @@ void upsdrv_updateinfo(void)
452477 ++ numfails ;
453478 if (numfails > MAXTRIES ) {
454479 ser_comm_fail ("Battery voltage out of bounds: [%02.1f]" , bv );
455- dstate_datastale ();
480+ if (!reconnect_ups ()) {
481+ dstate_datastale ();
482+ }
483+ numfails = 0 ;
456484 }
457485 return ;
458486 }
@@ -463,7 +491,10 @@ void upsdrv_updateinfo(void)
463491 ++ numfails ;
464492 if (numfails > MAXTRIES ) {
465493 ser_comm_fail ("InVoltMax out of bounds: [%ld]" , vmax );
466- dstate_datastale ();
494+ if (!reconnect_ups ()) {
495+ dstate_datastale ();
496+ }
497+ numfails = 0 ;
467498 }
468499 return ;
469500 }
@@ -474,7 +505,10 @@ void upsdrv_updateinfo(void)
474505 ++ numfails ;
475506 if (numfails > MAXTRIES ) {
476507 ser_comm_fail ("InVoltMin out of bounds: [%ld]" , vmin );
477- dstate_datastale ();
508+ if (!reconnect_ups ()) {
509+ dstate_datastale ();
510+ }
511+ numfails = 0 ;
478512 }
479513 return ;
480514 }
@@ -486,23 +520,32 @@ void upsdrv_updateinfo(void)
486520 ++ numfails ;
487521 if (numfails > MAXTRIES ) {
488522 ser_comm_fail ("Self test is out of range: [%ld]" , stest );
489- dstate_datastale ();
523+ if (!reconnect_ups ()) {
524+ dstate_datastale ();
525+ }
526+ numfails = 0 ;
490527 }
491528 return ;
492529 }
493530 if (errno == EINVAL ) {
494531 ++ numfails ;
495532 if (numfails > MAXTRIES ) {
496533 ser_comm_fail ("Self test returned non-numeric data." );
497- dstate_datastale ();
534+ if (!reconnect_ups ()) {
535+ dstate_datastale ();
536+ }
537+ numfails = 0 ;
498538 }
499539 return ;
500540 }
501541 if (stest > 3 || stest < 0 ) {
502542 ++ numfails ;
503543 if (numfails > MAXTRIES ) {
504544 ser_comm_fail ("Self test out of bounds: [%ld]" , stest );
505- dstate_datastale ();
545+ if (!reconnect_ups ()) {
546+ dstate_datastale ();
547+ }
548+ numfails = 0 ;
506549 }
507550 return ;
508551 }
@@ -644,6 +687,13 @@ void upsdrv_initups(void)
644687 char * val ;
645688
646689 upsfd = ser_open (device_path );
690+
691+ if (INVALID_FD_SER (upsfd )) {
692+ upslogx (LOG_WARNING , "%s: failed to open %s" ,
693+ __func__ , device_path );
694+ /* \todo: Deal with the failure */
695+ }
696+
647697 ser_set_speed (upsfd , device_path , B2400 );
648698
649699 if ((val = getval ("offdelay" ))) {
@@ -665,6 +715,10 @@ void upsdrv_initups(void)
665715
666716void upsdrv_cleanup (void )
667717{
668- ser_close (upsfd , device_path );
718+ upsdebugx (1 , "%s: begin" , __func__ );
719+ if (VALID_FD_SER (upsfd )) {
720+ ser_close (upsfd , device_path );
721+ upsfd = ERROR_FD_SER ; /* invalidate the closed upsfd */
722+ }
723+ upsdebugx (1 , "%s: end" , __func__ );
669724}
670-
0 commit comments