Skip to content

Commit 7d6d966

Browse files
committed
drivers/*.c, NEWS.adoc: update drivers to use reconnect_trying() with enum arg values for consistent reporting [#3541]
Signed-off-by: Jim Klimov <jimklimov+nut@gmail.com>
1 parent 60278b1 commit 7d6d966

15 files changed

Lines changed: 141 additions & 58 deletions

NEWS.adoc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,8 @@ https://github.com/networkupstools/nut/milestone/13
159159
* Introduced a `reconnect_max_tries` setting for NUT drivers, so they can
160160
be configured to exit after the specified number of reconnection attempts
161161
(so that the OS service management facility can completely recycle the
162-
driver program). [#3541]
162+
driver program). Multiple drivers updated to use the new facility to
163+
consistently report `reconnect_trying()` states. [#3541]
163164

164165
- NUT client libraries:
165166
* Complete support for actions documented in `docs/net-protocol.txt`

drivers/adelsystem_cbi.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
#endif
3535

3636
#define DRIVER_NAME "NUT ADELSYSTEM DC-UPS CB/CBI driver (libmodbus link type: " NUT_MODBUS_LINKTYPE_STR ")"
37-
#define DRIVER_VERSION "0.08"
37+
#define DRIVER_VERSION "0.09"
3838

3939
/* variables */
4040
static modbus_t *mbctx = NULL; /* modbus memory context */
@@ -1330,7 +1330,7 @@ void modbus_reconnect(void)
13301330
int rval;
13311331

13321332
upsdebugx(1, "modbus_reconnect, trying to reconnect to modbus server");
1333-
dstate_setinfo("driver.state", "reconnect.trying");
1333+
reconnect_trying(RECONNECT_TRYING);
13341334

13351335
/* clear current modbus context */
13361336
modbus_close(mbctx);
@@ -1391,5 +1391,5 @@ void modbus_reconnect(void)
13911391
/* #elif (defined NUT_MODBUS_TIMEOUT_ARG_timeval) // some un-castable type in fields */
13921392
#endif /* NUT_MODBUS_TIMEOUT_ARG_* */
13931393

1394-
dstate_setinfo("driver.state", "quiet");
1394+
reconnect_trying(RECONNECT_SUCCESS);
13951395
}

drivers/apc_modbus.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
#endif
4444

4545
#define DRIVER_NAME "NUT APC Modbus driver " DRIVER_NAME_NUT_MODBUS_HAS_USB_WITH_STR " USB support (libmodbus link type: " NUT_MODBUS_LINKTYPE_STR ")"
46-
#define DRIVER_VERSION "0.20"
46+
#define DRIVER_VERSION "0.21"
4747

4848
#if defined NUT_MODBUS_HAS_USB
4949

@@ -955,7 +955,7 @@ static void _apc_modbus_create_reopen_matcher(void)
955955

956956
static int _apc_modbus_reopen(void)
957957
{
958-
dstate_setinfo("driver.state", "reconnect.trying");
958+
reconnect_trying(RECONNECT_TRYING);
959959

960960
if (modbus_connect(modbus_ctx) < 0) {
961961
upslogx(LOG_ERR, "%s: Unable to connect Modbus: %s", __func__, modbus_strerror(errno));
@@ -975,9 +975,10 @@ static int _apc_modbus_reopen(void)
975975

976976
is_open = 1;
977977

978-
dstate_setinfo("driver.state", "reconnect.updateinfo");
978+
reconnect_trying(RECONNECT_UPDATEINFO);
979979
_apc_modbus_read_inventory();
980-
dstate_setinfo("driver.state", "quiet");
980+
981+
reconnect_trying(RECONNECT_SUCCESS);
981982

982983
return 1;
983984
}

drivers/apcsmart.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
#include "apcsmart_tabs.h"
3939

4040
#define DRIVER_NAME "APC Smart protocol driver"
41-
#define DRIVER_VERSION "3.39"
41+
#define DRIVER_VERSION "3.40"
4242

4343
#ifdef WIN32
4444
# ifndef ECANCELED
@@ -2395,7 +2395,7 @@ void upsdrv_updateinfo(void)
23952395
/* become aggressive after a few tries */
23962396
if (!(last_worked % 60)) {
23972397
upslogx(LOG_WARNING, "Trying to reconnect to the UPS");
2398-
dstate_setinfo("driver.state", "reconnect.trying");
2398+
reconnect_trying(RECONNECT_TRYING);
23992399

24002400
upsdebugx(1, "%s: call upsdrv_cleanup", __func__);
24012401
/* dstate_setinfo("driver.state", "cleanup.upsdrv"); */
@@ -2413,11 +2413,11 @@ void upsdrv_updateinfo(void)
24132413
upsdrv_initinfo();
24142414

24152415
upsdebugx(1, "%s: call upsdrv_updateinfo", __func__);
2416-
dstate_setinfo("driver.state", "reconnect.updateinfo");
2416+
reconnect_trying(RECONNECT_UPDATEINFO);
24172417
/* dstate_setinfo("driver.state", "init.updateinfo"); */
24182418
upsdrv_updateinfo();
24192419

2420-
dstate_setinfo("driver.state", "init.quiet");
2420+
reconnect_trying(RECONNECT_SUCCESS);
24212421
}
24222422

24232423
upsdebugx(1, "%s: nudging UPS with 'Y', iteration #%d ...",

drivers/bcmxcp_usb.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#include <unistd.h>
1212

1313
#define SUBDRIVER_NAME "USB communication subdriver"
14-
#define SUBDRIVER_VERSION "0.28"
14+
#define SUBDRIVER_VERSION "0.29"
1515

1616
/* communication driver description structure */
1717
upsdrv_info_t comm_upsdrv_info = {
@@ -385,7 +385,7 @@ void upsdrv_cleanup(void)
385385

386386
void upsdrv_reconnect(void)
387387
{
388-
dstate_setinfo("driver.state", "reconnect.trying");
388+
reconnect_trying(RECONNECT_TRYING);
389389

390390
upsdebugx(4, "==================================================");
391391
upsdebugx(4, "= device has been disconnected, try to reconnect =");
@@ -396,7 +396,12 @@ void upsdrv_reconnect(void)
396396

397397
upsdrv_initups();
398398

399-
dstate_setinfo("driver.state", "quiet");
399+
if (upsdev) {
400+
reconnect_trying(RECONNECT_SUCCESS);
401+
/* dstate_dataok() is called in bcmxcp.c::upsdrv_updateinfo() */
402+
} else {
403+
dstate_datastale();
404+
}
400405
}
401406

402407
/* USB functions */

drivers/blazer_usb.c

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
#endif /* WIN32 */
3838

3939
#define DRIVER_NAME "Megatec/Q1 protocol USB driver"
40-
#define DRIVER_VERSION "0.25"
40+
#define DRIVER_VERSION "0.26"
4141

4242
/* driver description structure */
4343
upsdrv_info_t upsdrv_info = {
@@ -483,21 +483,27 @@ ssize_t blazer_command(const char *cmd, char *buf, size_t buflen)
483483
{
484484
#ifndef TESTING
485485
ssize_t ret;
486+
int reconnecting = (udev == NULL);
486487

487-
if (udev == NULL) {
488-
dstate_setinfo("driver.state", "reconnect.trying");
488+
if (reconnecting) {
489+
reconnect_trying(RECONNECT_TRYING);
489490

490491
ret = usb->open_dev(&udev, &usbdevice, reopen_matcher, NULL);
491492

492493
if (ret < 1) {
494+
/* dstate_datastale/dstate_dataok managed in blazer.c::upsdrv_updateinfo() */
493495
return ret;
494496
}
495497

496-
dstate_setinfo("driver.state", "reconnect.updateinfo");
498+
reconnect_trying(RECONNECT_UPDATEINFO);
497499
}
498500

499501
ret = (*subdriver_command)(cmd, buf, buflen);
500502
if (ret >= 0) {
503+
/* clean read: forget any overflow streak */
504+
if (reconnecting) {
505+
reconnect_trying(RECONNECT_SUCCESS);
506+
}
501507
return ret;
502508
}
503509

@@ -540,6 +546,8 @@ ssize_t blazer_command(const char *cmd, char *buf, size_t buflen)
540546
case LIBUSB_ERROR_NOT_FOUND: /* No such file or directory */
541547
fallthrough_case_reconnect:
542548
/* Uh oh, got to reconnect! */
549+
/* Not accounting just yet with reconnect_trying(RECONNECT_TRYING),
550+
* to avoid off-by-one counter errors */
543551
dstate_setinfo("driver.state", "reconnect.trying");
544552
usb->close_dev(udev);
545553
udev = NULL;
@@ -558,6 +566,14 @@ ssize_t blazer_command(const char *cmd, char *buf, size_t buflen)
558566
break;
559567
}
560568

569+
if (reconnecting) {
570+
/* Success after updateinfo in the bulk of this method body */
571+
upsdebugx(1, "%s: libusb returned %" PRIiSIZE
572+
" which was not classified as a known error, assuming reconnection succeeded",
573+
__func__, ret);
574+
reconnect_trying(RECONNECT_SUCCESS);
575+
}
576+
561577
return ret;
562578
#else /* if TESTING: */
563579
const struct {

drivers/generic_modbus.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
#endif
3232

3333
#define DRIVER_NAME "NUT Generic Modbus driver (libmodbus link type: " NUT_MODBUS_LINKTYPE_STR ")"
34-
#define DRIVER_VERSION "0.10"
34+
#define DRIVER_VERSION "0.11"
3535

3636
/* variables */
3737
static modbus_t *mbctx = NULL; /* modbus memory context */
@@ -1063,7 +1063,7 @@ void modbus_reconnect(void)
10631063
int rval;
10641064

10651065
upsdebugx(2, "modbus_reconnect, trying to reconnect to modbus server");
1066-
dstate_setinfo("driver.state", "reconnect.trying");
1066+
reconnect_trying(RECONNECT_TRYING);
10671067

10681068
/* clear current modbus context */
10691069
modbus_close(mbctx);
@@ -1124,5 +1124,5 @@ void modbus_reconnect(void)
11241124
/* #elif (defined NUT_MODBUS_TIMEOUT_ARG_timeval) // some un-castable type in fields */
11251125
#endif /* NUT_MODBUS_TIMEOUT_ARG_* */
11261126

1127-
dstate_setinfo("driver.state", "quiet");
1127+
reconnect_trying(RECONNECT_SUCCESS);
11281128
}

drivers/nhs_ser.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*
55
* Copyright (C) 2024 Lucas Willian Bocchi <lucas@lucas.inf.br>
66
* Initial Release (as nhs-nut.c)
7-
* Copyright (C) 2024 - 2025 Jim Klimov <jimklimov+nut@gmail.com>
7+
* Copyright (C) 2024 - 2026 Jim Klimov <jimklimov+nut@gmail.com>
88
* Codebase adjusted to NUT standards
99
*
1010
* This program is free software; you can redistribute it and/or modify
@@ -43,7 +43,7 @@
4343
#include <math.h>
4444

4545
#define DRIVER_NAME "NHS Nobreak Drivers"
46-
#define DRIVER_VERSION "0.04"
46+
#define DRIVER_VERSION "0.05"
4747
#define MANUFACTURER "NHS Sistemas Eletronicos LTDA"
4848

4949
#define DEFAULTBAUD 2400
@@ -1706,7 +1706,7 @@ static unsigned int get_numbat(void) {
17061706

17071707
/* Return serial_fd after the reconnection attempt, for easier calls */
17081708
static int reconnect_ups_if_needed(void) {
1709-
/* retries to open port */
1709+
/* retries to open port until we declare "data stale" loudly */
17101710
static unsigned int retries = 0;
17111711

17121712
/* If comms failed earlier, try to resuscitate */
@@ -1715,7 +1715,7 @@ static int reconnect_ups_if_needed(void) {
17151715
__func__, porta);
17161716

17171717
/* Uh oh, got to reconnect! */
1718-
dstate_setinfo("driver.state", "reconnect.trying");
1718+
reconnect_trying(RECONNECT_TRYING);
17191719

17201720
while (serial_fd <= 0) {
17211721
upsdebugx(1, "%s: Trying to reopen serial...", __func__);
@@ -1732,7 +1732,7 @@ static int reconnect_ups_if_needed(void) {
17321732
upslogx(LOG_NOTICE, "Communications with UPS re-established");
17331733
}
17341734
retries = 0;
1735-
dstate_setinfo("driver.state", "quiet");
1735+
reconnect_trying(RECONNECT_SUCCESS);
17361736
} else {
17371737
if (retries == MAXTRIES) {
17381738
upslogx(LOG_WARNING, "Communications with UPS lost: port reopen failed!");

drivers/nutdrv_qx.c

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
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;

drivers/powerman-pdu.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
#include <libpowerman.h> /* pm_err_t and other beasts */
2424

2525
#define DRIVER_NAME "Powerman PDU client driver"
26-
#define DRIVER_VERSION "0.17"
26+
#define DRIVER_VERSION "0.18"
2727

2828
/* driver description structure */
2929
upsdrv_info_t upsdrv_info = {
@@ -218,7 +218,7 @@ static int reconnect_ups(void)
218218
{
219219
pm_err_t rv;
220220

221-
dstate_setinfo("driver.state", "reconnect.trying");
221+
reconnect_trying(RECONNECT_TRYING);
222222

223223
upsdebugx(4, "===================================================");
224224
upsdebugx(4, "= connection lost with Powerman, try to reconnect =");
@@ -228,10 +228,11 @@ static int reconnect_ups(void)
228228
pm_disconnect(pm);
229229

230230
/* Connect to the PowerMan daemon */
231-
if ((rv = pm_connect(device_path, NULL, &pm, 0)) != PM_ESUCCESS)
231+
if ((rv = pm_connect(device_path, NULL, &pm, 0)) != PM_ESUCCESS) {
232+
dstate_datastale();
232233
return 0;
233-
else {
234-
dstate_setinfo("driver.state", "quiet");
234+
} else {
235+
reconnect_trying(RECONNECT_SUCCESS);
235236
upsdebugx(4, "connection restored with Powerman");
236237
return 1;
237238
}

0 commit comments

Comments
 (0)