5858# define DRIVER_NAME "Generic Q* Serial driver"
5959#endif /* QX_USB */
6060
61- #define DRIVER_VERSION "0.52 "
61+ #define DRIVER_VERSION "0.53 "
6262
6363#ifdef QX_SERIAL
6464# include "serial.h"
@@ -3908,6 +3908,7 @@ static ssize_t qx_command(const char *cmd, size_t cmdlen, char *buf, size_t bufl
39083908 /* Persists across calls; only consecutive overflows accumulate (any clean
39093909 * read zeroes it, see the switch on `ret` below). */
39103910 static int overflow_tries = 0 ;
3911+ int reconnecting = (udev == NULL );
39113912# endif
39123913#endif
39133914
@@ -3927,22 +3928,25 @@ static ssize_t qx_command(const char *cmd, size_t cmdlen, char *buf, size_t bufl
39273928 if (is_usb ) {
39283929# endif /* QX_SERIAL (&& QX_USB)*/
39293930
3930- if (udev == NULL ) {
3931- dstate_setinfo ( "driver.state" , "reconnect.trying" );
3931+ if (reconnecting ) {
3932+ reconnect_trying ( RECONNECT_TRYING );
39323933
39333934 ret = usb -> open_dev (& udev , & usbdevice , reopen_matcher , NULL );
39343935
39353936 if (ret < 1 ) {
39363937 return ret ;
39373938 }
39383939
3939- dstate_setinfo ( "driver.state" , "reconnect.updateinfo" );
3940+ reconnect_trying ( RECONNECT_UPDATEINFO );
39403941 }
39413942
39423943 ret = (* subdriver_command )(cmd , cmdlen , buf , buflen );
39433944
39443945 if (ret >= 0 ) {
39453946 overflow_tries = 0 ; /* clean read: forget any overflow streak */
3947+ if (reconnecting ) {
3948+ reconnect_trying (RECONNECT_SUCCESS );
3949+ }
39463950 return ret ;
39473951 }
39483952
@@ -3999,6 +4003,8 @@ static ssize_t qx_command(const char *cmd, size_t cmdlen, char *buf, size_t bufl
39994003 case LIBUSB_ERROR_NOT_FOUND : /* No such file or directory */
40004004 fallthrough_case_reconnect :
40014005 /* Uh oh, got to reconnect! */
4006+ /* Not accounting just yet with reconnect_trying(RECONNECT_TRYING),
4007+ * to avoid off-by-one counter errors */
40024008 dstate_setinfo ("driver.state" , "reconnect.trying" );
40034009 usb -> close_dev (udev );
40044010 udev = NULL ;
@@ -4031,6 +4037,14 @@ static ssize_t qx_command(const char *cmd, size_t cmdlen, char *buf, size_t bufl
40314037 break ;
40324038 }
40334039
4040+ if (reconnecting ) {
4041+ /* Success after updateinfo in the bulk of this method body */
4042+ upsdebugx (1 , "%s: libusb returned %" PRIiSIZE
4043+ " which was not classified as a known error, assuming reconnection succeeded" ,
4044+ __func__ , ret );
4045+ reconnect_trying (RECONNECT_SUCCESS );
4046+ }
4047+
40344048# ifdef QX_SERIAL
40354049 /* Communication: serial */
40364050 } else { /* !is_usb */
@@ -4045,6 +4059,7 @@ static ssize_t qx_command(const char *cmd, size_t cmdlen, char *buf, size_t bufl
40454059 ret = ser_send_buf (upsfd , cmd , cmdlen );
40464060
40474061 if (ret <= 0 ) {
4062+ /* TOTHINK: Is any special reconnect logic/tracking needed? */
40484063 upsdebugx (3 , "send: %s (%" PRIiSIZE ")" ,
40494064 ret ? strerror (errno ) : "timeout" , ret );
40504065 return ret ;
0 commit comments