-
-
Notifications
You must be signed in to change notification settings - Fork 333
/
usbhid-ups.c
2088 lines (1788 loc) · 58.2 KB
/
usbhid-ups.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* usbhid-ups.c - Driver for USB and serial (MGE SHUT) HID UPS units
*
* Copyright (C)
* 2003-2022 Arnaud Quette <arnaud.quette@gmail.com>
* 2005 John Stamp <kinsayder@hotmail.com>
* 2005-2006 Peter Selinger <selinger@users.sourceforge.net>
* 2007-2009 Arjen de Korte <adkorte-guest@alioth.debian.org>
* 2016 Eaton / Arnaud Quette <ArnaudQuette@Eaton.com>
*
* This program was sponsored by MGE UPS SYSTEMS, and now Eaton
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* TODO list:
* - set ST_FLAG_RW according to HIDData_t->Attribute (ATTR_DATA_CST-ATTR_NVOL_VOL)
*/
#define DRIVER_NAME "Generic HID driver"
#define DRIVER_VERSION "0.52"
#define HU_VAR_WAITBEFORERECONNECT "waitbeforereconnect"
#include "main.h" /* Must be first, includes "config.h" */
#include "nut_stdint.h"
#include "libhid.h"
#include "usbhid-ups.h"
#include "hidparser.h"
#include "hidtypes.h"
#include "common.h"
#ifdef WIN32
#include "wincompat.h"
#endif
/* include all known subdrivers */
#include "mge-hid.h"
#if !((defined SHUT_MODE) && SHUT_MODE)
/* explore stub goes first, others alphabetically */
#include "explore-hid.h"
#include "apc-hid.h"
#include "arduino-hid.h"
#include "belkin-hid.h"
#include "cps-hid.h"
#include "delta_ups-hid.h"
#include "ever-hid.h"
#include "idowell-hid.h"
#include "legrand-hid.h"
#include "liebert-hid.h"
#include "openups-hid.h"
#include "powercom-hid.h"
#include "powervar-hid.h"
#include "salicru-hid.h"
#include "tripplite-hid.h"
#endif /* !SHUT_MODE => USB */
/* Reference list of available subdrivers */
static subdriver_t *subdriver_list[] = {
#if !((defined SHUT_MODE) && SHUT_MODE)
&explore_subdriver,
#endif /* !SHUT_MODE => USB */
/* mge-hid.c supports both SHUT and USB */
&mge_subdriver,
#if !((defined SHUT_MODE) && SHUT_MODE)
&apc_subdriver,
&arduino_subdriver,
&belkin_subdriver,
&cps_subdriver,
&delta_ups_subdriver,
&ever_subdriver,
&idowell_subdriver,
&legrand_subdriver,
&liebert_subdriver,
&openups_subdriver,
&powercom_subdriver,
&powervar_subdriver,
&salicru_subdriver,
&tripplite_subdriver,
#endif /* !SHUT_MODE => USB */
NULL
};
upsdrv_info_t upsdrv_info = {
DRIVER_NAME,
DRIVER_VERSION,
"Arnaud Quette <arnaud.quette@gmail.com>\n" \
"Peter Selinger <selinger@users.sourceforge.net>\n" \
"Arjen de Korte <adkorte-guest@alioth.debian.org>\n" \
"John Stamp <kinsayder@hotmail.com>",
/*FIXME: link the subdrivers? do the same as for the mibs! */
#if !((defined SHUT_MODE) && SHUT_MODE)
DRV_STABLE,
#else /* SHUT_MODE */
DRV_EXPERIMENTAL,
#endif /* SHUT_MODE / USB */
{ &comm_upsdrv_info, NULL }
};
/* Data walk modes */
typedef enum {
HU_WALKMODE_INIT = 0,
HU_WALKMODE_QUICK_UPDATE,
HU_WALKMODE_FULL_UPDATE
} walkmode_t;
/* pointer to the active subdriver object (changed in callback() function) */
static subdriver_t *subdriver = NULL;
/* Global vars */
static HIDDevice_t *hd = NULL;
static HIDDevice_t curDevice = { 0x0000, 0x0000, NULL, NULL, NULL, NULL, 0, NULL
#if (defined WITH_USB_BUSPORT) && (WITH_USB_BUSPORT)
, NULL
#endif
};
static HIDDeviceMatcher_t *subdriver_matcher = NULL;
#if !((defined SHUT_MODE) && SHUT_MODE)
static HIDDeviceMatcher_t *exact_matcher = NULL;
static HIDDeviceMatcher_t *regex_matcher = NULL;
#endif /* !SHUT_MODE => USB */
static int pollfreq = DEFAULT_POLLFREQ;
static unsigned ups_status = 0;
static bool_t data_has_changed = FALSE; /* for SEMI_STATIC data polling */
#ifndef SUN_LIBUSB
bool_t use_interrupt_pipe = TRUE;
#else
bool_t use_interrupt_pipe = FALSE;
#endif
static size_t interrupt_pipe_EIO_count = 0; /* How many times we had I/O errors since last reconnect? */
static time_t lastpoll; /* Timestamp the last polling */
hid_dev_handle_t udev = HID_DEV_HANDLE_CLOSED;
/**
* CyberPower UT series sometime need a bit of help deciding their online status.
* This quirk is to enable the special handling of OL & DISCHRG at the same time
* as being OB (on battery power/no mains power). Enabled by device config flag.
*/
static int onlinedischarge = 0;
/**
* Some UPS models (e.g. APC were seen to do so) report OL & DISCHRG when they
* are in calibration mode. This usually happens after a few seconds reporting
* an "OFF" state as well, while the hardware is switching to on-battery mode.
*/
static int onlinedischarge_calibration = 0;
/* support functions */
static hid_info_t *find_nut_info(const char *varname);
static hid_info_t *find_hid_info(const HIDData_t *hiddata);
static const char *hu_find_infoval(info_lkp_t *hid2info, const double value);
static long hu_find_valinfo(info_lkp_t *hid2info, const char* value);
static void process_boolean_info(const char *nutvalue);
static void ups_alarm_set(void);
static void ups_status_set(void);
static bool_t hid_ups_walk(walkmode_t mode);
static int reconnect_ups(void);
static int ups_infoval_set(hid_info_t *item, double value);
static int callback(hid_dev_handle_t argudev, HIDDevice_t *arghd,
usb_ctrl_charbuf rdbuf, usb_ctrl_charbufsize rdlen);
#ifdef DEBUG
static double interval(void);
#endif
/* global variables */
HIDDesc_t *pDesc = NULL; /* parsed Report Descriptor */
reportbuf_t *reportbuf = NULL; /* buffer for most recent reports */
int disable_fix_report_desc = 0; /* by default we apply fix-ups for broken USB encoding, etc. */
/* --------------------------------------------------------------- */
/* Struct & data for boolean processing */
/* --------------------------------------------------------------- */
/* Note: this structure holds internal status info, directly as
collected from the hardware; not yet converted to official NUT
status or alarms */
typedef struct {
const char *status_str; /* ups status string */
const unsigned int status_mask; /* ups status mask */
} status_lkp_t;
static status_lkp_t status_info[] = {
/* map internal status strings to bit masks */
{ "online", STATUS(ONLINE) },
{ "dischrg", STATUS(DISCHRG) },
{ "chrg", STATUS(CHRG) },
{ "lowbatt", STATUS(LOWBATT) },
{ "overload", STATUS(OVERLOAD) },
{ "replacebatt", STATUS(REPLACEBATT) },
{ "shutdownimm", STATUS(SHUTDOWNIMM) },
{ "trim", STATUS(TRIM) },
{ "boost", STATUS(BOOST) },
{ "bypassauto", STATUS(BYPASSAUTO) },
{ "bypassman", STATUS(BYPASSMAN) },
{ "off", STATUS(OFF) },
{ "cal", STATUS(CALIB) },
{ "overheat", STATUS(OVERHEAT) },
{ "commfault", STATUS(COMMFAULT) },
{ "depleted", STATUS(DEPLETED) },
{ "timelimitexp", STATUS(TIMELIMITEXP) },
{ "fullycharged", STATUS(FULLYCHARGED) },
{ "awaitingpower", STATUS(AWAITINGPOWER) },
{ "fanfail", STATUS(FANFAIL) },
{ "nobattery", STATUS(NOBATTERY) },
{ "battvoltlo", STATUS(BATTVOLTLO) },
{ "battvolthi", STATUS(BATTVOLTHI) },
{ "chargerfail", STATUS(CHARGERFAIL) },
{ "vrange", STATUS(VRANGE) },
{ "frange", STATUS(FRANGE) },
{ NULL, 0 },
};
/* ---------------------------------------------------------------------- */
/* value lookup tables and generic lookup functions */
/* Actual value lookup tables => should be fine for all Mfrs (TODO: validate it!) */
/* the purpose of the following status conversions is to collect
information, not to interpret it. The function
process_boolean_info() remembers these values by updating the global
variable ups_status. Interpretation happens in ups_status_set,
where they are converted to standard NUT status strings. Notice
that the below conversions do not yield standard NUT status
strings; this in indicated being in lower-case characters.
The reason to separate the collection of information from its
interpretation is that not each report received from the UPS may
contain all the status flags, so they must be stored
somewhere. Also, there can be more than one status flag triggering
a certain condition (e.g. a certain UPS might have variables
low_battery, shutdown_imminent, timelimit_exceeded, and each of
these would trigger the NUT status LB. But we have to ensure that
these variables don't unset each other, so they are remembered
separately) */
info_lkp_t online_info[] = {
{ 1, "online", NULL, NULL },
{ 0, "!online", NULL, NULL },
{ 0, NULL, NULL, NULL }
};
info_lkp_t discharging_info[] = {
{ 1, "dischrg", NULL, NULL },
{ 0, "!dischrg", NULL, NULL },
{ 0, NULL, NULL, NULL }
};
info_lkp_t charging_info[] = {
{ 1, "chrg", NULL, NULL },
{ 0, "!chrg", NULL, NULL },
{ 0, NULL, NULL, NULL }
};
info_lkp_t lowbatt_info[] = {
{ 1, "lowbatt", NULL, NULL },
{ 0, "!lowbatt", NULL, NULL },
{ 0, NULL, NULL, NULL }
};
info_lkp_t overload_info[] = {
{ 1, "overload", NULL, NULL },
{ 0, "!overload", NULL, NULL },
{ 0, NULL, NULL, NULL }
};
info_lkp_t replacebatt_info[] = {
{ 1, "replacebatt", NULL, NULL },
{ 0, "!replacebatt", NULL, NULL },
{ 0, NULL, NULL, NULL }
};
info_lkp_t trim_info[] = {
{ 1, "trim", NULL, NULL },
{ 0, "!trim", NULL, NULL },
{ 0, NULL, NULL, NULL }
};
info_lkp_t boost_info[] = {
{ 1, "boost", NULL, NULL },
{ 0, "!boost", NULL, NULL },
{ 0, NULL, NULL, NULL }
};
info_lkp_t bypass_auto_info[] = {
{ 1, "bypassauto", NULL, NULL },
{ 0, "!bypassauto", NULL, NULL },
{ 0, NULL, NULL, NULL }
};
info_lkp_t bypass_manual_info[] = {
{ 1, "bypassman", NULL, NULL },
{ 0, "!bypassman", NULL, NULL },
{ 0, NULL, NULL, NULL }
};
/* note: this value is reverted (0=set, 1=not set). We report "being
off" rather than "being on", so that devices that don't implement
this variable are "on" by default */
info_lkp_t off_info[] = {
{ 0, "off", NULL, NULL },
{ 1, "!off", NULL, NULL },
{ 0, NULL, NULL, NULL }
};
info_lkp_t calibration_info[] = {
{ 1, "cal", NULL, NULL },
{ 0, "!cal", NULL, NULL },
{ 0, NULL, NULL, NULL }
};
/* note: this value is reverted (0=set, 1=not set). We report "battery
not installed" rather than "battery installed", so that devices
that don't implement this variable have a battery by default */
info_lkp_t nobattery_info[] = {
{ 1, "!nobattery", NULL, NULL },
{ 0, "nobattery", NULL, NULL },
{ 0, NULL, NULL, NULL }
};
info_lkp_t fanfail_info[] = {
{ 1, "fanfail", NULL, NULL },
{ 0, "!fanfail", NULL, NULL },
{ 0, NULL, NULL, NULL }
};
info_lkp_t shutdownimm_info[] = {
{ 1, "shutdownimm", NULL, NULL },
{ 0, "!shutdownimm", NULL, NULL },
{ 0, NULL, NULL, NULL }
};
info_lkp_t overheat_info[] = {
{ 1, "overheat", NULL, NULL },
{ 0, "!overheat", NULL, NULL },
{ 0, NULL, NULL, NULL }
};
info_lkp_t awaitingpower_info[] = {
{ 1, "awaitingpower", NULL, NULL },
{ 0, "!awaitingpower", NULL, NULL },
{ 0, NULL, NULL, NULL }
};
info_lkp_t commfault_info[] = {
{ 1, "commfault", NULL, NULL },
{ 0, "!commfault", NULL, NULL },
{ 0, NULL, NULL, NULL }
};
info_lkp_t timelimitexpired_info[] = {
{ 1, "timelimitexp", NULL, NULL },
{ 0, "!timelimitexp", NULL, NULL },
{ 0, NULL, NULL, NULL }
};
info_lkp_t battvoltlo_info[] = {
{ 1, "battvoltlo", NULL, NULL },
{ 0, "!battvoltlo", NULL, NULL },
{ 0, NULL, NULL, NULL }
};
info_lkp_t battvolthi_info[] = {
{ 1, "battvolthi", NULL, NULL },
{ 0, "!battvolthi", NULL, NULL },
{ 0, NULL, NULL, NULL }
};
info_lkp_t chargerfail_info[] = {
{ 1, "chargerfail", NULL, NULL },
{ 0, "!chargerfail", NULL, NULL },
{ 0, NULL, NULL, NULL }
};
info_lkp_t fullycharged_info[] = { /* used by CyberPower and TrippLite */
{ 1, "fullycharged", NULL, NULL },
{ 0, "!fullycharged", NULL, NULL },
{ 0, NULL, NULL, NULL }
};
info_lkp_t depleted_info[] = {
{ 1, "depleted", NULL, NULL },
{ 0, "!depleted", NULL, NULL },
{ 0, NULL, NULL, NULL }
};
info_lkp_t vrange_info[] = {
{ 0, "!vrange", NULL, NULL },
{ 1, "vrange", NULL, NULL },
{ 0, NULL, NULL, NULL }
};
info_lkp_t frange_info[] = {
{ 0, "!frange", NULL, NULL },
{ 1, "frange", NULL, NULL },
{ 0, NULL, NULL, NULL }
};
info_lkp_t test_write_info[] = {
{ 0, "No test", NULL, NULL },
{ 1, "Quick test", NULL, NULL },
{ 2, "Deep test", NULL, NULL },
{ 3, "Abort test", NULL, NULL },
{ 0, NULL, NULL, NULL }
};
info_lkp_t test_read_info[] = {
{ 1, "Done and passed", NULL, NULL },
{ 2, "Done and warning", NULL, NULL },
{ 3, "Done and error", NULL, NULL },
{ 4, "Aborted", NULL, NULL },
{ 5, "In progress", NULL, NULL },
{ 6, "No test initiated", NULL, NULL },
{ 7, "Test scheduled", NULL, NULL },
{ 0, NULL, NULL, NULL }
};
info_lkp_t beeper_info[] = {
{ 1, "disabled", NULL, NULL },
{ 2, "enabled", NULL, NULL },
{ 3, "muted", NULL, NULL },
{ 0, NULL, NULL, NULL }
};
info_lkp_t yes_no_info[] = {
{ 0, "no", NULL, NULL },
{ 1, "yes", NULL, NULL },
{ 0, NULL, NULL, NULL }
};
info_lkp_t on_off_info[] = {
{ 0, "off", NULL, NULL },
{ 1, "on", NULL, NULL },
{ 0, NULL, NULL, NULL }
};
/* returns statically allocated string - must not use it again before
done with result! */
static const char *date_conversion_fun(double value)
{
/* Per spec https://www.usb.org/sites/default/files/pdcv11.pdf (page 38):
* 4.2.6 Battery Settings -> ManufacturerDate
* The date the pack was manufactured in a packed integer.
* The date is packed in the following fashion:
* (year – 1980)*512 + month*32 + day.
*/
static char buf[32];
long year, month, day;
if ((long)value == 0) {
return "not set";
}
/* TOTHINK: About the comment below...
* Does bit-shift keep the negativeness on all architectures?
*/
/* negative value represents pre-1980 date: */
year = 1980 + ((long)value >> 9);
month = ((long)value >> 5) & 0x0f;
day = (long)value & 0x1f;
snprintf(buf, sizeof(buf), "%04ld/%02ld/%02ld", year, month, day);
return buf;
}
static double date_conversion_reverse(const char* date_string)
{
long year, month, day;
long date;
sscanf(date_string, "%04ld/%02ld/%02ld", &year, &month, &day);
if(year - 1980 > 127 || month > 12 || day > 31)
return 0;
date = (year - 1980) << 9;
date += month << 5;
date += day;
return (double) date;
}
info_lkp_t date_conversion[] = {
{ 0, NULL, date_conversion_fun, date_conversion_reverse }
};
/* returns statically allocated string - must not use it again before
done with result! */
static const char *hex_conversion_fun(double value)
{
static char buf[20];
snprintf(buf, sizeof(buf), "%08lx", (long)value);
return buf;
}
/* FIXME? Do we need an inverse "nuf()" here? */
info_lkp_t hex_conversion[] = {
{ 0, NULL, hex_conversion_fun, NULL }
};
/* returns statically allocated string - must not use it again before
done with result! */
static const char *stringid_conversion_fun(double value)
{
static char buf[20];
return HIDGetIndexString(udev, (int)value, buf, sizeof(buf));
}
/* FIXME? Do we need an inverse "nuf()" here? */
info_lkp_t stringid_conversion[] = {
{ 0, NULL, stringid_conversion_fun, NULL }
};
/* returns statically allocated string - must not use it again before
done with result! */
static const char *divide_by_10_conversion_fun(double value)
{
static char buf[20];
snprintf(buf, sizeof(buf), "%0.1f", value * 0.1);
return buf;
}
/* FIXME? Do we need an inverse "nuf()" here? */
info_lkp_t divide_by_10_conversion[] = {
{ 0, NULL, divide_by_10_conversion_fun, NULL }
};
/* returns statically allocated string - must not use it again before
done with result! */
static const char *kelvin_celsius_conversion_fun(double value)
{
static char buf[20];
/* check if the value is in the Kelvin range, to
* detect buggy value (already expressed in °C), as found
* on some HP implementation */
if ((value >= 273) && (value <= 373)) {
/* the value is indeed in °K */
snprintf(buf, sizeof(buf), "%.1f", value - 273.15);
}
else {
/* else, this is actually °C, not °K! */
snprintf(buf, sizeof(buf), "%.1f", value);
}
return buf;
}
/* FIXME? Do we need an inverse "nuf()" here? */
info_lkp_t kelvin_celsius_conversion[] = {
{ 0, NULL, kelvin_celsius_conversion_fun, NULL }
};
static subdriver_t *match_function_subdriver_name(int fatal_mismatch) {
char *subdrv = getval("subdriver");
subdriver_t *info = NULL;
/* Pick up the subdriver name if set explicitly */
if (subdrv) {
int i, flag_HAVE_LIBREGEX = 0;
#if (defined HAVE_LIBREGEX && HAVE_LIBREGEX)
int res;
size_t len;
regex_t *regex_ptr = NULL;
flag_HAVE_LIBREGEX = 1;
#endif
upsdebugx(2,
"%s: matching a subdriver by explicit "
"name%s: '%s'...",
__func__,
flag_HAVE_LIBREGEX ? "/regex" : "",
subdrv);
/* First try exact match for strings like "TrippLite HID 0.85"
* Not likely to hit (due to versions etc.), but worth a try :)
*/
for (i=0; subdriver_list[i] != NULL; i++) {
if (strcmp_null(subdrv, subdriver_list[i]->name) == 0) {
info = subdriver_list[i];
goto found;
}
}
#if (defined HAVE_LIBREGEX && HAVE_LIBREGEX)
/* Then try a case-insensitive regex like "tripplite.*"
* if so provided by caller */
upsdebugx(2, "%s: retry matching by regex 'as is'", __func__);
res = compile_regex(®ex_ptr, subdrv, REG_ICASE | REG_EXTENDED);
if (res == 0 && regex_ptr != NULL) {
for (i=0; subdriver_list[i] != NULL; i++) {
res = match_regex(regex_ptr, subdriver_list[i]->name);
if (res == 1) {
free(regex_ptr);
info = subdriver_list[i];
goto found;
}
}
}
if (regex_ptr) {
free(regex_ptr);
regex_ptr = NULL;
}
/* Then try a case-insensitive regex like "tripplite.*"
* with automatically added ".*" */
len = strlen(subdrv);
if (
(len < 3 || (subdrv[len-2] != '.' && subdrv[len-1] != '*'))
&& len < (LARGEBUF-3)
) {
char buf[LARGEBUF];
upsdebugx(2, "%s: retry matching by regex with added '.*'", __func__);
snprintf(buf, sizeof(buf), "%s.*", subdrv);
res = compile_regex(®ex_ptr, buf, REG_ICASE | REG_EXTENDED);
if (res == 0 && regex_ptr != NULL) {
for (i=0; subdriver_list[i] != NULL; i++) {
res = match_regex(regex_ptr, subdriver_list[i]->name);
if (res == 1) {
free(regex_ptr);
info = subdriver_list[i];
goto found;
}
}
}
if (regex_ptr) {
free(regex_ptr);
regex_ptr = NULL;
}
}
#endif /* HAVE_LIBREGEX */
if (fatal_mismatch) {
fatalx(EXIT_FAILURE,
"Configuration requested subdriver '%s' but none matched",
subdrv);
} else {
upslogx(LOG_WARNING,
"Configuration requested subdriver '%s' but none matched; "
"will try USB matching by other fields",
subdrv);
}
}
/* No match (and non-fatal mismatch mode), or no
* "subdriver" was specified in configuration */
return NULL;
found:
upsdebugx(2, "%s: found a match: %s", __func__, info->name);
if (!getval("vendorid") || !getval("productid")) {
if (fatal_mismatch) {
fatalx(EXIT_FAILURE,
"When specifying a subdriver, "
"'vendorid' and 'productid' "
"are mandatory.");
} else {
upslogx(LOG_WARNING,
"When specifying a subdriver, "
"'vendorid' and 'productid' "
"are highly recommended.");
}
}
return info;
}
/*!
* subdriver matcher: only useful for USB mode
* as SHUT is only supported by MGE UPS SYSTEMS units
*/
#if !((defined SHUT_MODE) && SHUT_MODE)
static int match_function_subdriver(HIDDevice_t *d, void *privdata) {
int i;
NUT_UNUSED_VARIABLE(privdata);
if (match_function_subdriver_name(1)) {
/* This driver can handle this device. Guessing so... */
return 1;
}
upsdebugx(2, "%s (non-SHUT mode): matching a device...", __func__);
for (i=0; subdriver_list[i] != NULL; i++) {
if (subdriver_list[i]->claim(d)) {
return 1;
}
}
upsdebugx(2, "%s (non-SHUT mode): failed to match a subdriver "
"to vendor and/or product ID",
__func__);
return 0;
}
static HIDDeviceMatcher_t subdriver_matcher_struct = {
match_function_subdriver,
NULL,
NULL
};
#endif /* !SHUT_MODE => USB */
/* ---------------------------------------------
* driver functions implementations
* --------------------------------------------- */
/* process instant command and take action. */
int instcmd(const char *cmdname, const char *extradata)
{
hid_info_t *hidups_item;
const char *val;
double value;
if (!strcasecmp(cmdname, "beeper.off")) {
/* compatibility mode for old command */
upslogx(LOG_WARNING,
"The 'beeper.off' command has been "
"renamed to 'beeper.disable'");
return instcmd("beeper.disable", NULL);
}
if (!strcasecmp(cmdname, "beeper.on")) {
/* compatibility mode for old command */
upslogx(LOG_WARNING,
"The 'beeper.on' command has been "
"renamed to 'beeper.enable'");
return instcmd("beeper.enable", NULL);
}
upsdebugx(1, "instcmd(%s, %s)",
cmdname,
extradata ? extradata : "[NULL]");
/* Retrieve and check netvar & item_path */
hidups_item = find_nut_info(cmdname);
/* Check for fallback if not found */
if (hidups_item == NULL) {
upsdebugx(3, "%s: cmdname '%s' not found; "
"checking for alternatives",
__func__, cmdname);
if (!strcasecmp(cmdname, "load.on")) {
return instcmd("load.on.delay", "0");
}
if (!strcasecmp(cmdname, "load.off")) {
return instcmd("load.off.delay", "0");
}
if (!strcasecmp(cmdname, "shutdown.return")) {
int ret;
/* Ensure "ups.start.auto" is set to "yes",
* if supported */
if (dstate_getinfo("ups.start.auto")) {
setvar("ups.start.auto", "yes");
}
ret = instcmd("load.on.delay", dstate_getinfo("ups.delay.start"));
if (ret != STAT_INSTCMD_HANDLED) {
return ret;
}
/* Some UPS's (e.g. TrippLive AVR750U w/ 3024 protocol) don't accept
* commands that arrive too rapidly, so add this arbitary wait,
* which has proven to be long enough to avoid this problem in practice */
usleep(125000);
return instcmd("load.off.delay", dstate_getinfo("ups.delay.shutdown"));
}
if (!strcasecmp(cmdname, "shutdown.stayoff")) {
int ret;
/* Ensure "ups.start.auto" is set to "no", if supported */
if (dstate_getinfo("ups.start.auto")) {
setvar("ups.start.auto", "no");
}
ret = instcmd("load.on.delay", "-1");
if (ret != STAT_INSTCMD_HANDLED) {
return ret;
}
/* Some UPS's (e.g. TrippLive AVR750U w/ 3024 protocol) don't accept
* commands that arrive too rapidly, so add this arbitary wait,
* which has proven to be long enough to avoid this problem in practice */
usleep(125000);
return instcmd("load.off.delay", dstate_getinfo("ups.delay.shutdown"));
}
upsdebugx(2, "instcmd: info element unavailable %s\n", cmdname);
return STAT_INSTCMD_INVALID;
}
upsdebugx(3, "%s: using Path '%s'",
__func__,
(hidups_item->hidpath ? hidups_item->hidpath : "[NULL]")
);
/* Check if the item is an instant command */
if (!(hidups_item->hidflags & HU_TYPE_CMD)) {
upsdebugx(2, "instcmd: %s is not an instant command\n", cmdname);
return STAT_INSTCMD_INVALID;
}
/* If extradata is empty, use the default value from the HID-to-NUT table */
val = extradata ? extradata : hidups_item->dfl;
/* Lookup the new value if needed */
if (hidups_item->hid2info != NULL) {
value = hu_find_valinfo(hidups_item->hid2info, val);
} else {
value = atol(val);
}
/* Actual variable setting */
if (HIDSetDataValue(udev, hidups_item->hiddata, value) == 1) {
upsdebugx(3, "instcmd: SUCCEED\n");
/* Set the status so that SEMI_STATIC vars are polled */
data_has_changed = TRUE;
return STAT_INSTCMD_HANDLED;
}
upsdebugx(3, "instcmd: FAILED\n"); /* TODO: HANDLED but FAILED, not UNKNOWN! */
return STAT_INSTCMD_FAILED;
}
/* set r/w variable to a value. */
int setvar(const char *varname, const char *val)
{
hid_info_t *hidups_item;
double value;
upsdebugx(1, "setvar(%s, %s)", varname, val);
/* retrieve and check netvar & item_path */
hidups_item = find_nut_info(varname);
if (hidups_item == NULL) {
upsdebugx(2, "setvar: info element unavailable %s\n", varname);
return STAT_SET_UNKNOWN;
}
/* Checking item writability and HID Path */
if (!(hidups_item->info_flags & ST_FLAG_RW)) {
upsdebugx(2, "setvar: not writable %s\n", varname);
return STAT_SET_UNKNOWN;
}
/* handle server side variable */
if (hidups_item->hidflags & HU_FLAG_ABSENT) {
upsdebugx(2, "setvar: setting server side variable %s\n", varname);
dstate_setinfo(hidups_item->info_type, "%s", val);
return STAT_SET_HANDLED;
}
/* HU_FLAG_ABSENT is the only case of HID Path == NULL */
if (hidups_item->hidpath == NULL) {
upsdebugx(2, "setvar: ID Path is NULL for %s\n", varname);
return STAT_SET_UNKNOWN;
}
/* Lookup the new value if needed */
if (hidups_item->hid2info != NULL) {
value = hu_find_valinfo(hidups_item->hid2info, val);
} else {
value = atol(val);
}
/* Actual variable setting */
if (HIDSetDataValue(udev, hidups_item->hiddata, value) == 1) {
upsdebugx(5, "setvar: SUCCEED\n");
/* Set the status so that SEMI_STATIC vars are polled */
data_has_changed = TRUE;
return STAT_SET_HANDLED;
}
upsdebugx(3, "setvar: FAILED\n"); /* FIXME: HANDLED but FAILED, not UNKNOWN! */
return STAT_SET_UNKNOWN;
}
void upsdrv_shutdown(void)
{
upsdebugx(1, "upsdrv_shutdown...");
/* Try to shutdown with delay */
if (instcmd("shutdown.return", NULL) == STAT_INSTCMD_HANDLED) {
/* Shutdown successful */
return;
}
/* If the above doesn't work, try shutdown.reboot */
if (instcmd("shutdown.reboot", NULL) == STAT_INSTCMD_HANDLED) {
/* Shutdown successful */
return;
}
/* If the above doesn't work, try load.off.delay */
if (instcmd("load.off.delay", NULL) == STAT_INSTCMD_HANDLED) {
/* Shutdown successful */
return;
}
upslogx(LOG_ERR, "Shutdown failed!");
set_exit_flag(-1);
}
void upsdrv_help(void)
{
size_t i;
printf("\nAcceptable values for 'subdriver' via -x or ups.conf "
"in this driver (exact names here, case-insensitive "
"sub-strings may be used, as well as regular expressions): ");
for (i = 0; subdriver_list[i] != NULL; i++) {
if (i>0)
printf(", ");
printf("\"%s\"", subdriver_list[i]->name);
}
printf("\n\n");
printf("Read The Fine Manual ('man 8 usbhid-ups')\n");
}
void upsdrv_makevartable(void)
{
char temp [MAX_STRING_SIZE];
upsdebugx(1, "upsdrv_makevartable...");
snprintf(temp, sizeof(temp),
"Set low battery level, in %% (default=%s)",
DEFAULT_LOWBATT);
addvar (VAR_VALUE, HU_VAR_LOWBATT, temp);
snprintf(temp, sizeof(temp),
"Set shutdown delay, in seconds (default=%s)",
DEFAULT_OFFDELAY);
addvar(VAR_VALUE, HU_VAR_OFFDELAY, temp);
snprintf(temp, sizeof(temp),
"Set startup delay, in seconds (default=%s)",
DEFAULT_ONDELAY);
addvar(VAR_VALUE, HU_VAR_ONDELAY, temp);
snprintf(temp, sizeof(temp),
"Set polling frequency, in seconds, to reduce data flow (default=%d)",
DEFAULT_POLLFREQ);
addvar(VAR_VALUE, HU_VAR_POLLFREQ, temp);
addvar(VAR_FLAG, "pollonly", "Don't use interrupt pipe, only use polling");
addvar(VAR_FLAG, "onlinedischarge",
"Set to treat discharging while online as being offline");
addvar(VAR_FLAG, "onlinedischarge_calibration",
"Set to treat discharging while online as doing calibration");
addvar(VAR_FLAG, "disable_fix_report_desc",
"Set to disable fix-ups for broken USB encoding, etc. which we apply by default on certain vendors/products");
#if !((defined SHUT_MODE) && SHUT_MODE)
addvar(VAR_VALUE, "subdriver", "Explicit USB HID subdriver selection");
/* allow -x vendor=X, vendorid=X, product=X, productid=X, serial=X */
nut_usb_addvars();
addvar(VAR_FLAG, "explore",
"Diagnostic matching of unsupported UPS");
addvar(VAR_FLAG, "maxreport",
"Activate tweak for buggy APC Back-UPS firmware");
addvar(VAR_FLAG, "interruptonly",
"Don't use polling, only use interrupt pipe");
addvar(VAR_VALUE, "interruptsize",
"Number of bytes to read from interrupt pipe");
addvar(VAR_VALUE, HU_VAR_WAITBEFORERECONNECT,
"Seconds to wait before trying to reconnect");
#else /* SHUT_MODE */
addvar(VAR_VALUE, "notification",
"Set notification type (ignored, only for backward compatibility)");
#endif /* SHUT_MODE / USB */
}
#define MAX_EVENT_NUM 32
void upsdrv_updateinfo(void)
{
hid_info_t *item;
HIDData_t *event[MAX_EVENT_NUM], *found_data;
int i, evtCount;
double value;
time_t now;
upsdebugx(1, "upsdrv_updateinfo...");
time(&now);
/* check for device availability to set datastale! */
if (hd == NULL) {
/* don't flood reconnection attempts */
if (now < (lastpoll + poll_interval)) {
return;
}
upsdebugx(1, "Got to reconnect!");
if (use_interrupt_pipe == TRUE && interrupt_pipe_EIO_count > 0) {