Skip to content

Commit ef02192

Browse files
authored
Merge pull request networkupstools#3288 from Vladdrako/powercom
drivers: powercom-hid: use offdelay/ondelay config options for shutdown commands
2 parents 1c32fbd + 4da001b commit ef02192

File tree

2 files changed

+48
-5
lines changed

2 files changed

+48
-5
lines changed

NEWS.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,11 @@ https://github.com/networkupstools/nut/milestone/12
238238
fixed to use the same mapping helper as other subdrivers. [issue #3246]
239239
* `powercom-hid` subdriver improved with a `powercom_hack_voltage_lkp` to
240240
query `battery.voltage`. [issue #2766]
241+
* `powercom-hid` subdriver now honours configured `offdelay` and `ondelay`
242+
driver options in `ups.conf`, and prefers them (if present) over the
243+
values received from the UPS itself (like `ups.delay.shutdown` and
244+
`ups.delay.start`), to fix implementations of shutdown/stayoff/startup
245+
instant commands. [PR #3288]
241246
* Improved handling of transient `LIBUSB_ERROR_IO` failures during polling.
242247
Some devices (CyberPower, etc.) have firmware bugs causing random I/O
243248
errors on certain HID reports. The driver now skips failing reports and

drivers/powercom-hid.c

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
#include <ctype.h> /* isdigit() */
3030

31-
#define POWERCOM_HID_VERSION "PowerCOM HID 0.73"
31+
#define POWERCOM_HID_VERSION "PowerCOM HID 0.74"
3232
/* FIXME: experimental flag to be put in upsdrv_info */
3333

3434
/* PowerCOM */
@@ -97,11 +97,23 @@ static const char *powercom_startup_fun(double value)
9797
static double powercom_startup_nuf(const char *value)
9898
{
9999
const char *s = dstate_getinfo("ups.delay.start");
100+
const char *cfg = getval("ondelay");
100101
uint32_t val, command;
101102
int iv;
102103

103-
/* Start with seconds "as is" - convert into whole minutes */
104-
iv = atoi(value ? value : s) / 60; /* minutes */
104+
/* Priority: 1) command value, 2) config ondelay, 3) UPS current, 4) default
105+
* Note we start with seconds "as is" - convert into whole minutes for the device protocol
106+
*/
107+
if (value && *value) {
108+
iv = atoi(value) / 60;
109+
} else if (cfg && *cfg) {
110+
iv = atoi(cfg) / 60;
111+
} else if (s && *s) {
112+
iv = atoi(s) / 60;
113+
} else {
114+
iv = 2; /* Default: 2 minutes (120 seconds) */
115+
}
116+
105117
#if (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_PUSH_POP) && ( (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_TYPE_LIMITS) || (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_TAUTOLOGICAL_CONSTANT_OUT_OF_RANGE_COMPARE) )
106118
# pragma GCC diagnostic push
107119
#endif
@@ -165,10 +177,23 @@ static const char *powercom_shutdown_fun(double value)
165177
static double powercom_shutdown_nuf(const char *value)
166178
{
167179
const char *s = dstate_getinfo("ups.delay.shutdown");
180+
const char *cfg = getval("offdelay");
168181
uint16_t val, command;
169182
int iv;
170183

171-
iv = atoi(value ? value : s); /* seconds */
184+
/* Priority: 1) command value, 2) config offdelay, 3) UPS current, 4) default
185+
* Note we start with seconds "as is" - convert into magic numbers for the device protocol below
186+
*/
187+
if (value && *value) {
188+
iv = atoi(value);
189+
} else if (cfg && *cfg) {
190+
iv = atoi(cfg);
191+
} else if (s && *s) {
192+
iv = atoi(s);
193+
} else {
194+
iv = 60; /* Default: 60 seconds */
195+
}
196+
172197
if (iv < 0 || (intmax_t)iv > (intmax_t)UINT16_MAX) {
173198
upsdebugx(0, "%s: value = %d is not in uint16_t range", __func__, iv);
174199
return 0;
@@ -219,11 +244,24 @@ static info_lkp_t powercom_shutdown_info[] = {
219244
static double powercom_stayoff_nuf(const char *value)
220245
{
221246
const char *s = dstate_getinfo("ups.delay.shutdown");
247+
const char *cfg = getval("offdelay");
222248
uint16_t val, command;
223249
int iv;
224250

225251
/* FIXME: Anything for powercom_sdcmd_discrete_delay? */
226-
iv = atoi(value ? value : s);
252+
/* Priority: 1) command value, 2) config offdelay, 3) UPS current, 4) default
253+
* Note we start with seconds "as is" - convert into magic numbers for the device protocol below
254+
*/
255+
if (value && *value) {
256+
iv = atoi(value);
257+
} else if (cfg && *cfg) {
258+
iv = atoi(cfg);
259+
} else if (s && *s) {
260+
iv = atoi(s);
261+
} else {
262+
iv = 60; /* Default: 60 seconds */
263+
}
264+
227265
if (iv < 0 || (intmax_t)iv > (intmax_t)UINT16_MAX) {
228266
upsdebugx(0, "%s: value = %d is not in uint16_t range", __func__, iv);
229267
return 0;

0 commit comments

Comments
 (0)