forked from networkupstools/nut
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmge-hid.c
More file actions
2387 lines (2110 loc) · 99.4 KB
/
mge-hid.c
File metadata and controls
2387 lines (2110 loc) · 99.4 KB
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
/* mge-hid.c - data to monitor Eaton / MGE HID (USB and serial) devices
*
* Copyright (C)
* 2003 - 2015 Arnaud Quette <arnaud.quette@free.fr>
* 2015 - 2024 Eaton / Arnaud Quette <ArnaudQuette@Eaton.com>
* 2020 - 2025 Jim Klimov <jimklimov+nut@gmail.com>
* 2024 - 2025 "DaRK AnGeL" <28630321+masterwishx@users.noreply.github.com>
*
* Sponsored by MGE UPS SYSTEMS <http://www.mgeups.com>
*
* 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:
* - better processing of FW info:
* * some models (HP R5000) include firmware.aux (00.01.0021;00.01.00)
* * other (9130) need more processing (0128 => 1.28)
* ...
* - better handling of input.transfer.* (need dstate_addrange)
* - outlet management logic (Ie, for outlet.X.load.{on,off}.delay
* => use outlet.X.delay.{start,stop}
*/
#include "main.h" /* for getval() */
#include "usbhid-ups.h"
#include "mge-hid.h"
#include "nut_float.h"
#include "timehead.h"
#ifdef WIN32
# include "wincompat.h"
# ifndef LDOUBLE
# ifdef HAVE_LONG_DOUBLE
# define LDOUBLE long double
# else
# define LDOUBLE double
# endif
# endif
#endif
#define MGE_HID_VERSION "MGE HID 1.54"
/* (prev. MGE Office Protection Systems, prev. MGE UPS SYSTEMS) */
/* Eaton */
#define MGE_VENDORID 0x0463
/* Dell */
#define DELL_VENDORID 0x047c
/* Powerware */
#define POWERWARE_VENDORID 0x0592
/* Hewlett Packard */
#define HP_VENDORID 0x03f0
/* AEG */
#define AEG_VENDORID 0x2b2d
/* Note that normally this VID is handled by Liebert/Phoenixtec HID mapping,
* here it is just for for AEG PROTECT NAS devices: */
/* Phoenixtec Power Co., Ltd */
#define PHOENIXTEC 0x06da
/* IBM */
#define IBM_VENDORID 0x04b3
/* KSTAR under Berkeley Varitronics Systems ID */
#define KSTAR_VENDORID 0x09d6
#if !((defined SHUT_MODE) && SHUT_MODE)
#include "usb-common.h"
/* USB IDs device table */
static usb_device_id_t mge_usb_device_table[] = {
/* various models */
{ USB_DEVICE(MGE_VENDORID, 0x0001), NULL },
{ USB_DEVICE(MGE_VENDORID, 0xffff), NULL },
/* various models */
{ USB_DEVICE(DELL_VENDORID, 0xffff), NULL },
/* PW 9140 */
{ USB_DEVICE(POWERWARE_VENDORID, 0x0004), NULL },
/* R/T3000 */
{ USB_DEVICE(HP_VENDORID, 0x1fe5), NULL },
/* R/T3000 */
{ USB_DEVICE(HP_VENDORID, 0x1fe6), NULL },
/* various models */
{ USB_DEVICE(HP_VENDORID, 0x1fe7), NULL },
{ USB_DEVICE(HP_VENDORID, 0x1fe8), NULL },
/* PROTECT B / NAS */
{ USB_DEVICE(AEG_VENDORID, 0xffff), NULL },
{ USB_DEVICE(PHOENIXTEC, 0xffff), NULL },
/* 6000 VA LCD 4U Rack UPS; 5396-1Kx */
{ USB_DEVICE(IBM_VENDORID, 0x0001), NULL },
/* MasterPower MF-UPS650VA under KSTAR vendorid (can also be under MGE)
* MicroPower models were also reported */
{ USB_DEVICE(KSTAR_VENDORID, 0x0001), NULL },
/* Terminating entry */
{ 0, 0, NULL }
};
#endif /* !SHUT_MODE => USB */
typedef enum {
MGE_DEFAULT_OFFLINE = 0,
MGE_PEGASUS = 0x100,
MGE_3S = 0x110,
/* All offline models have type value < 200! */
MGE_DEFAULT = 0x200, /* for line-interactive and online models */
MGE_EVOLUTION = 0x300, /* MGE Evolution series */
MGE_EVOLUTION_650,
MGE_EVOLUTION_850,
MGE_EVOLUTION_1150,
MGE_EVOLUTION_S_1250,
MGE_EVOLUTION_1550,
MGE_EVOLUTION_S_1750,
MGE_EVOLUTION_2000,
MGE_EVOLUTION_S_2500,
MGE_EVOLUTION_S_3000,
MGE_PULSAR_M = 0x400, /* MGE Pulsar M series */
MGE_PULSAR_M_2200,
MGE_PULSAR_M_3000,
MGE_PULSAR_M_3000_XL,
EATON_5P = 0x500, /* Eaton 5P / 5PX / 5SC series; possibly 5S also */
EATON_9E = 0x900 /* Eaton 9E entry-level / 9SX / 9PX series */
} models_type_t;
/* Default to line-interactive or online (ie, not offline).
* This is then overridden for offline, through mge_model_names */
static models_type_t mge_type = MGE_DEFAULT;
/* Countries definition, for region specific settings and features */
typedef enum {
COUNTRY_UNKNOWN = -1,
COUNTRY_EUROPE = 0,
COUNTRY_US,
/* Special European models, which also supports 200 / 208 V */
COUNTRY_EUROPE_208,
COUNTRY_WORLDWIDE,
COUNTRY_AUSTRALIA,
} country_code_t;
static int country_code = COUNTRY_UNKNOWN;
static char mge_scratch_buf[20];
/* ABM - Advanced Battery Monitoring
***********************************
* Synthesis table
* HID data | Charger in ABM mode | Charger in Constant mode | Error | Good |
* UPS.BatterySystem.Charger.ABMEnable | 1 | 0 | | |
* UPS.PowerSummary.PresentStatus.ACPresent | On utility | On battery | On utility | On battery | | |
* Charger ABM mode | Charging | Floating | Resting | Discharging | Disabled | Disabled | | |
* UPS.BatterySystem.Charger.Mode | 1 | 3 | 4 | 2 | 6 | 6 | | |
* UPS.BatterySystem.Charger.Status | 1 | 2 | 3 | 4 | 6 | 6 | 0 | 1 |
* UPS.PowerSummary.PresentStatus.Charging | 1 | 1 | 1 | 0 | 1 | 0 | | |
* UPS.PowerSummary.PresentStatus.Discharging | 0 | 0 | 0 | 1 | 0 | 1 | | |
*
* Notes (from David G. Miller) to understand ABM status:
*
* When supporting ABM, when a UPS powers up or returns from battery, or
* ends the ABM rest mode, it enters charge mode.
* Some UPSs run a different charger reference voltage during charge mode
* but all the newer models should not be doing that, but basically once
* the battery voltage reaches the charger reference level
* (should be 2.3 volts/cell) = 13.8v battery(6 cells), the charger is considered in float mode.
* Some UPSs will not annunciate float mode until the charger power starts falling from
* the maximum level indicating the battery is truly at the float voltage or in float mode.
* The %charge level is based on battery voltage and the charge mode timer
* (should be 48 hours) and some UPSs add in a value that's related to charger
* power output. So you can have UPS that enters float mode with anywhere
* from 80% or greater battery capacity.
* float mode is not important from the software's perspective, it's there to
* help determine if the charger is advancing correctly.
* So in float mode, the charger is charging the battery, so by definition you
* can assert the CHRG flag in NUT when in "float" mode or "charge" mode.
* When in "rest" mode the charger is not delivering anything to the battery,
* but it will when the ABM cycle(28 days) ends, or a battery discharge occurs
* and utility returns. This is when the ABM status should be "resting".
* If a battery failure is detected that disables the charger, it should be
* reporting "off" in the ABM charger status.
* Of course when delivering load power from the battery, the ABM status is
* discharging.
*
* UPS.BatterySystem.Charger.ChargerType:
*
* This HID path has been seen to denote the charger type of the UPS.
* At present it is unclear whether deductions about ABM being enabled
* should be made from it, even though it seems to be changing when a
* UPS that is ABM-capable is manually enabling and disabling its ABM.
* For example, the Eaton 5P850I has been seen with a ChargerType of 0
* when having ABM disabled, and ChargerType of 4 when ABM was enabled.
* According to the known values, however, the ChargerType should have
* either remained the same (if only to display general charger ability)
* or changed to ChargerType of 5 (for constant charge mode) - and not 0:
*
* { 0, "None", NULL, NULL },
* { 1, "Extended (CLA)", NULL, NULL },
* { 2, "Large extension", NULL, NULL },
* { 3, "Extra large extension (XL)", NULL, NULL },
* { 4, "ABM", NULL, NULL },
* { 5, "Constant Charge (CC)", NULL, NULL },
*
* For this reason, ABM being enabled remains controlled only through the
* one HID path known to be consistent: UPS.BatterySystem.Charger.ABMEnable
*/
#define ABM_UNKNOWN -1
#define ABM_DISABLED 0
#define ABM_ENABLED 1
/* Define internal flags to process the different ABM paths as seen in HID:
* ABM_PATH_STATUS --> UPS.BatterySystem.Charger.Status
* ABM_PATH_MODE --> UPS.BatterySystem.Charger.Mode */
#define ABM_PATH_UNKNOWN -1
#define ABM_PATH_STATUS 0
#define ABM_PATH_MODE 1
/* Internal flag to process battery status (CHRG/DISCHRG) and ABM */
static int advanced_battery_monitoring = ABM_UNKNOWN;
/* Internal flag to process the different ABM paths as seen in HID */
static int advanced_battery_path = ABM_PATH_UNKNOWN;
/* TODO: Lifted from strptime.c... maybe should externalize the fallback?
* NOTE: HAVE_DECL_* are always defined, 0 or 1. Many other flags are not.
*/
#if ! HAVE_DECL_ROUND
# ifndef WIN32
static long round (double value)
# else
static long round (LDOUBLE value)
# endif
{
long intpart;
intpart = (long)value;
value = value - intpart;
if (value >= 0.5)
intpart++;
return intpart;
}
#endif /* HAVE_DECL_ROUND */
/* Used to store internally if ABM is enabled or not */
static const char *eaton_abm_enabled_fun(double value)
{
int abm_enabled_value = (int)value;
switch (abm_enabled_value)
{
case ABM_DISABLED:
advanced_battery_monitoring = ABM_DISABLED;
upsdebugx(2, "ABM status is: disabled (%i)", abm_enabled_value);
break;
case ABM_ENABLED:
advanced_battery_monitoring = ABM_ENABLED;
upsdebugx(2, "ABM status is: enabled (%i)", abm_enabled_value);
break;
default:
advanced_battery_monitoring = ABM_UNKNOWN;
upsdebugx(2, "ABM status is: unknown (%i)", abm_enabled_value);
break;
}
/* Return NULL, not to get the value published! */
return NULL;
}
static info_lkp_t eaton_abm_enabled_info[] = {
{ 0, "dummy", eaton_abm_enabled_fun, NULL },
{ 0, NULL, NULL, NULL }
};
/* ABM Path: UPS.BatterySystem.Charger.Mode (battery.charger.mode.status) */
static const char *eaton_abm_path_mode_fun(double value)
{
int abm_path_mode_value = (int)value;
/* If unknown/disabled ABM, reset ABM path to give UPS a chance to use another once re-enabled */
if (advanced_battery_monitoring == ABM_UNKNOWN || advanced_battery_monitoring == ABM_DISABLED) {
advanced_battery_path = ABM_PATH_UNKNOWN;
return NULL;
}
/* If ABM_ENABLED and not yet initialized set ABM path to ABM_PATH_MODE */
if (advanced_battery_monitoring == ABM_ENABLED && advanced_battery_path == ABM_PATH_UNKNOWN)
{
advanced_battery_path = ABM_PATH_MODE;
upsdebugx(2, "Set ABM path to: ABM_PATH_MODE (%i)", abm_path_mode_value);
}
/* Return NULL, not to get the value published! */
return NULL;
}
static info_lkp_t eaton_abm_path_mode_info[] = {
{ 0, "dummy", eaton_abm_path_mode_fun, NULL },
{ 0, NULL, NULL, NULL }
};
/* ABM Path: UPS.BatterySystem.Charger.Status (battery.charger.type.status) */
static const char *eaton_abm_path_status_fun(double value)
{
int abm_path_status_value = (int)value;
/* If unknown/disabled ABM, reset ABM path to give UPS a chance to use another once re-enabled */
if (advanced_battery_monitoring == ABM_UNKNOWN || advanced_battery_monitoring == ABM_DISABLED) {
advanced_battery_path = ABM_PATH_UNKNOWN;
return NULL;
}
/* If ABM_ENABLED and not yet initialized set ABM path to ABM_PATH_STATUS */
if (advanced_battery_monitoring == ABM_ENABLED && advanced_battery_path == ABM_PATH_UNKNOWN)
{
advanced_battery_path = ABM_PATH_STATUS;
upsdebugx(2, "Set ABM path to: ABM_PATH_STATUS (%i)", abm_path_status_value);
}
/* Return NULL, not to get the value published! */
return NULL;
}
static info_lkp_t eaton_abm_path_status_info[] = {
{ 0, "dummy", eaton_abm_path_status_fun, NULL },
{ 0, NULL, NULL, NULL }
};
/* Note 1: This point will need more clarification! */
# if 0
/* Used to store internally if ABM is enabled or not (for legacy units) */
static const char *eaton_abm_enabled_legacy_fun(double value)
{
advanced_battery_monitoring = value;
upsdebugx(2, "ABM is %s (legacy data)", (advanced_battery_monitoring==1)?"enabled":"disabled");
/* Return NULL, not to get the value published! */
return NULL;
}
static info_lkp_t eaton_abm_enabled_legacy_info[] = {
{ 0, "dummy", eaton_abm_enabled_legacy_fun, NULL },
{ 0, NULL, NULL, NULL }
};
#endif /* if 0 */
/* Used to process ABM status text (for battery.charger.status) */
static const char *eaton_abm_status_fun(double value)
{
int abm_status_value = (int)value;
/* Don't process if ABM is unknown or disabled */
if (advanced_battery_monitoring == ABM_UNKNOWN || advanced_battery_monitoring == ABM_DISABLED) {
/* Clear any previously published data, in case
* the user has switched off ABM */
dstate_delinfo("battery.charger.status");
return NULL;
}
/* ABM Path: UPS.BatterySystem.Charger.Status (battery.charger.type.status)
* as more recent and seen with 9 series devices (and possibly others): */
if (advanced_battery_path == ABM_PATH_STATUS)
{
switch (abm_status_value)
{
case 1:
snprintf(mge_scratch_buf, sizeof(mge_scratch_buf), "%s", "charging");
break;
case 2:
snprintf(mge_scratch_buf, sizeof(mge_scratch_buf), "%s", "floating");
break;
case 3:
snprintf(mge_scratch_buf, sizeof(mge_scratch_buf), "%s", "resting");
break;
case 4:
snprintf(mge_scratch_buf, sizeof(mge_scratch_buf), "%s", "discharging");
break;
case 6: /* ABM Charger Disabled */
snprintf(mge_scratch_buf, sizeof(mge_scratch_buf), "%s", "off");
break;
case 5: /* Undefined - ABM is not activated */
default:
/* Return NULL, not to get the value published! */
return NULL;
}
}
/* ABM Path: UPS.BatterySystem.Charger.Mode (battery.charger.mode.status)
* as more common and for any other ABM paths not (yet) recognized: */
else
{
switch (abm_status_value)
{
case 1:
snprintf(mge_scratch_buf, sizeof(mge_scratch_buf), "%s", "charging");
break;
case 2:
snprintf(mge_scratch_buf, sizeof(mge_scratch_buf), "%s", "discharging");
break;
case 3:
snprintf(mge_scratch_buf, sizeof(mge_scratch_buf), "%s", "floating");
break;
case 4:
snprintf(mge_scratch_buf, sizeof(mge_scratch_buf), "%s", "resting");
break;
case 6: /* ABM Charger Disabled */
snprintf(mge_scratch_buf, sizeof(mge_scratch_buf), "%s", "off");
break;
case 5: /* Undefined - ABM is not activated */
default:
/* Return NULL, not to get the value published! */
return NULL;
}
}
upsdebugx(2, "ABM charger status is: %s (%i)", mge_scratch_buf, abm_status_value);
return mge_scratch_buf;
}
static info_lkp_t eaton_abm_status_info[] = {
{ 1, "dummy", eaton_abm_status_fun, NULL },
{ 0, NULL, NULL, NULL }
};
static info_lkp_t eaton_charger_type_info[] = {
{ 0, "None", NULL, NULL },
{ 1, "Extended (CLA)", NULL, NULL },
{ 2, "Large extension", NULL, NULL },
{ 3, "Extra large extension (XL)", NULL, NULL },
{ 4, "ABM", NULL, NULL },
{ 5, "Constant Charge (CC)", NULL, NULL },
{ 0, NULL, NULL, NULL }
};
/* Used to process ABM status flags, for ups.status (CHRG/DISCHRG) */
static const char *eaton_abm_chrg_dischrg_fun(double value)
{
int abm_chrg_dischrg_value = (int)value;
/* Don't process if ABM is unknown or disabled */
if (advanced_battery_monitoring == ABM_UNKNOWN || advanced_battery_monitoring == ABM_DISABLED)
return NULL;
/* ABM Path: UPS.BatterySystem.Charger.Status (battery.charger.type.status)
* as more recent and seen with 9 series devices (and possibly others): */
if (advanced_battery_path == ABM_PATH_STATUS)
{
switch (abm_chrg_dischrg_value)
{
case 1: /* charging status */
snprintf(mge_scratch_buf, sizeof(mge_scratch_buf), "%s", "chrg");
break;
case 2: /* floating status */
snprintf(mge_scratch_buf, sizeof(mge_scratch_buf), "%s", "chrg");
break;
case 4: /* discharging status */
snprintf(mge_scratch_buf, sizeof(mge_scratch_buf), "%s", "dischrg");
break;
case 6: /* ABM Charger Disabled */
case 3: /* resting, nothing to publish! (?) */
case 5: /* Undefined - ABM is not activated */
default:
/* Return NULL, not to get the value published! */
return NULL;
}
}
/* ABM Path: UPS.BatterySystem.Charger.Mode (battery.charger.mode.status)
* as more common and for any other ABM paths not (yet) recognized: */
else
{
switch (abm_chrg_dischrg_value)
{
case 1: /* charging status */
snprintf(mge_scratch_buf, sizeof(mge_scratch_buf), "%s", "chrg");
break;
case 3: /* floating status */
snprintf(mge_scratch_buf, sizeof(mge_scratch_buf), "%s", "chrg");
break;
case 2: /* discharging status */
snprintf(mge_scratch_buf, sizeof(mge_scratch_buf), "%s", "dischrg");
break;
case 6: /* ABM Charger Disabled */
case 4: /* resting, nothing to publish! (?) */
case 5: /* Undefined - ABM is not activated */
default:
/* Return NULL, not to get the value published! */
return NULL;
}
}
upsdebugx(2, "ABM charger flag is: %s (%i)", mge_scratch_buf, abm_chrg_dischrg_value);
return mge_scratch_buf;
}
static info_lkp_t eaton_abm_chrg_dischrg_info[] = {
{ 1, "dummy", eaton_abm_chrg_dischrg_fun, NULL },
{ 0, NULL, NULL, NULL }
};
/* ABM also implies that standard CHRG/DISCHRG are processed according
* to weither ABM is enabled or not...
* If ABM is disabled, we publish these legacy status
* Otherwise, we don't publish on ups.status, but only battery.charger.status */
/* FIXME: we may prefer to publish the CHRG/DISCHRG status
* on battery.charger.status?! */
static const char *eaton_abm_check_dischrg_fun(double value)
{
if (advanced_battery_monitoring == ABM_UNKNOWN || advanced_battery_monitoring == ABM_DISABLED)
{
if (d_equal(value, 1)) {
snprintf(mge_scratch_buf, sizeof(mge_scratch_buf), "%s", "dischrg");
}
else {
snprintf(mge_scratch_buf, sizeof(mge_scratch_buf), "%s", "!dischrg");
}
}
else {
/* Else, ABM is enabled, we should return NULL,
* not to get the value published!
* However, clear flags that would persist in case of prior
* publication in ABM-disabled mode */
snprintf(mge_scratch_buf, sizeof(mge_scratch_buf), "%s", "!dischrg");
}
return mge_scratch_buf;
}
static info_lkp_t eaton_discharging_info[] = {
{ 1, "dummy", eaton_abm_check_dischrg_fun, NULL },
{ 0, NULL, NULL, NULL }
};
static const char *eaton_abm_check_chrg_fun(double value)
{
if (advanced_battery_monitoring == ABM_UNKNOWN || advanced_battery_monitoring == ABM_DISABLED)
{
if (d_equal(value, 1)) {
snprintf(mge_scratch_buf, sizeof(mge_scratch_buf), "%s", "chrg");
}
else {
snprintf(mge_scratch_buf, sizeof(mge_scratch_buf), "%s", "!chrg");
}
}
else {
/* Else, ABM is enabled, we should return NULL,
* not to get the value published!
* However, clear flags that would persist in case of prior
* publication in ABM-disabled mode */
snprintf(mge_scratch_buf, sizeof(mge_scratch_buf), "%s", "!chrg");
}
return mge_scratch_buf;
}
static info_lkp_t eaton_charging_info[] = {
{ 1, "dummy", eaton_abm_check_chrg_fun, NULL },
{ 0, NULL, NULL, NULL }
};
/* The HID path 'UPS.PowerSummary.Time' reports Unix time (ie the number of
* seconds since 1970-01-01 00:00:00. This has to be split between ups.date and
* ups.time */
static const char *mge_date_conversion_fun(double value)
{
time_t sec = value;
struct tm tmbuf;
if (strftime(mge_scratch_buf, sizeof(mge_scratch_buf), "%Y/%m/%d", localtime_r(&sec, &tmbuf)) == 10) {
return mge_scratch_buf;
}
upsdebugx(3, "%s: can't compute date %g", __func__, value);
return NULL;
}
static const char *mge_time_conversion_fun(double value)
{
time_t sec = value;
struct tm tmbuf;
if (strftime(mge_scratch_buf, sizeof(mge_scratch_buf), "%H:%M:%S", localtime_r(&sec, &tmbuf)) == 8) {
return mge_scratch_buf;
}
upsdebugx(3, "%s: can't compute time %g", __func__, value);
return NULL;
}
#ifdef HAVE_STRPTIME
/* Conversion back retrieve ups.time to build the full unix time */
static double mge_date_conversion_nuf(const char *value)
{
struct tm mge_tm;
/* build a full value (date) + time string */
snprintf(mge_scratch_buf, sizeof(mge_scratch_buf), "%s %s", value, dstate_getinfo("ups.time"));
if (strptime(mge_scratch_buf, "%Y/%m/%d %H:%M:%S", &mge_tm) != NULL) {
return mktime(&mge_tm);
}
upsdebugx(3, "%s: can't compute date %s", __func__, value);
return 0;
}
/* Conversion back retrieve ups.date to build the full unix time */
static double mge_time_conversion_nuf(const char *value)
{
struct tm mge_tm;
/* build a full date + value (time) string */
snprintf(mge_scratch_buf, sizeof(mge_scratch_buf), "%s %s", dstate_getinfo("ups.date"), value);
if (strptime(mge_scratch_buf, "%Y/%m/%d %H:%M:%S", &mge_tm) != NULL) {
return mktime(&mge_tm);
}
upsdebugx(3, "%s: can't compute time %s", __func__, value);
return 0;
}
static info_lkp_t mge_date_conversion[] = {
{ 0, NULL, mge_date_conversion_fun, mge_date_conversion_nuf },
{ 0, NULL, NULL, NULL }
};
static info_lkp_t mge_time_conversion[] = {
{ 0, NULL, mge_time_conversion_fun, mge_time_conversion_nuf },
{ 0, NULL, NULL, NULL }
};
#else
static info_lkp_t mge_date_conversion[] = {
{ 0, NULL, mge_date_conversion_fun, NULL },
{ 0, NULL, NULL, NULL }
};
static info_lkp_t mge_time_conversion[] = {
{ 0, NULL, mge_time_conversion_fun, NULL },
{ 0, NULL, NULL, NULL }
};
#endif /* HAVE_STRPTIME */
/* The HID path 'UPS.PowerSummary.ConfigVoltage' only reports
* 'battery.voltage.nominal' for specific UPS series.
* Ignore the value for other series (default behavior).
*/
static const char *mge_battery_voltage_nominal_fun(double value)
{
switch (mge_type & 0xFF00) /* Ignore model byte */
{
case MGE_EVOLUTION:
if (mge_type == MGE_EVOLUTION_650) {
value = 12.0;
}
break;
case MGE_PULSAR_M:
case EATON_5P:
case EATON_9E: /* Presumably per https://github.com/networkupstools/nut/issues/1925#issuecomment-1562342854 */
break;
default:
return NULL;
}
snprintf(mge_scratch_buf, sizeof(mge_scratch_buf), "%.0f", value);
return mge_scratch_buf;
}
static info_lkp_t mge_battery_voltage_nominal[] = {
{ 0, NULL, mge_battery_voltage_nominal_fun, NULL },
{ 0, NULL, NULL, NULL }
};
/* The HID path 'UPS.PowerSummary.Voltage' only reports
* 'battery.voltage' for specific UPS series.
* Ignore the value for other series (default behavior).
*/
static const char *mge_battery_voltage_fun(double value)
{
switch (mge_type & 0xFF00) /* Ignore model byte */
{
case MGE_EVOLUTION:
case MGE_PULSAR_M:
case EATON_5P:
case EATON_9E: /* Presumably per https://github.com/networkupstools/nut/issues/1925#issuecomment-1562342854 */
break;
default:
return NULL;
}
snprintf(mge_scratch_buf, sizeof(mge_scratch_buf), "%.1f", value);
return mge_scratch_buf;
}
static info_lkp_t mge_battery_voltage[] = {
{ 0, NULL, mge_battery_voltage_fun, NULL },
{ 0, NULL, NULL, NULL }
};
static const char *mge_powerfactor_conversion_fun(double value)
{
snprintf(mge_scratch_buf, sizeof(mge_scratch_buf), "%.2f", value / 100);
return mge_scratch_buf;
}
static info_lkp_t mge_powerfactor_conversion[] = {
{ 0, NULL, mge_powerfactor_conversion_fun, NULL },
{ 0, NULL, NULL, NULL }
};
static const char *mge_battery_capacity_fun(double value)
{
snprintf(mge_scratch_buf, sizeof(mge_scratch_buf), "%.2f", value / 3600);
return mge_scratch_buf;
}
static info_lkp_t mge_battery_capacity[] = {
{ 0, NULL, mge_battery_capacity_fun, NULL },
{ 0, NULL, NULL, NULL }
};
static info_lkp_t eaton_enable_disable_info[] = {
{ 0, "disabled", NULL, NULL },
{ 1, "enabled", NULL, NULL },
{ 0, NULL, NULL, NULL }
};
static info_lkp_t mge_upstype_conversion[] = {
{ 1, "offline / line interactive", NULL, NULL },
{ 2, "online", NULL, NULL },
{ 3, "online - unitary/parallel", NULL, NULL },
{ 4, "online - parallel with hot standy", NULL, NULL },
{ 5, "online - hot standby redundancy", NULL, NULL },
{ 0, NULL, NULL, NULL }
};
static info_lkp_t mge_sensitivity_info[] = {
{ 0, "normal", NULL, NULL },
{ 1, "high", NULL, NULL },
{ 2, "low", NULL, NULL },
{ 0, NULL, NULL, NULL }
};
static info_lkp_t mge_emergency_stop[] = {
{ 1, "Emergency stop!", NULL, NULL },
{ 0, NULL, NULL, NULL }
};
static info_lkp_t mge_wiring_fault[] = {
{ 1, "Wiring fault!", NULL, NULL },
{ 0, NULL, NULL, NULL }
};
static info_lkp_t mge_config_failure[] = {
{ 1, "Fatal EEPROM fault!", NULL, NULL },
{ 0, NULL, NULL, NULL }
};
static info_lkp_t mge_inverter_volthi[] = {
{ 1, "Inverter AC voltage too high!", NULL, NULL },
{ 0, NULL, NULL, NULL }
};
static info_lkp_t mge_inverter_voltlo[] = {
{ 1, "Inverter AC voltage too low!", NULL, NULL },
{ 0, NULL, NULL, NULL }
};
static info_lkp_t mge_short_circuit[] = {
{ 1, "Output short circuit!", NULL, NULL },
{ 0, NULL, NULL, NULL }
};
static info_lkp_t mge_onbatt_info[] = {
{ 1, "!online", NULL, NULL },
{ 0, "online", NULL, NULL },
{ 0, NULL, NULL, NULL }
};
/* allow limiting to ups.model = Protection Station, Ellipse Eco
* and 3S (US 750 and AUS 700 only!) */
static const char *eaton_check_pegasus_fun(double value)
{
switch (mge_type & 0xFF00) /* Ignore model byte */
{
case MGE_PEGASUS:
break;
case MGE_3S:
/* Only consider non European models */
if (country_code != COUNTRY_EUROPE)
break;
return NULL;
default:
return NULL;
}
snprintf(mge_scratch_buf, sizeof(mge_scratch_buf), "%.0f", value);
return mge_scratch_buf;
}
static info_lkp_t pegasus_threshold_info[] = {
{ 10, "10", eaton_check_pegasus_fun, NULL },
{ 25, "25", eaton_check_pegasus_fun, NULL },
{ 60, "60", eaton_check_pegasus_fun, NULL },
{ 0, NULL, NULL, NULL }
};
/* allow limiting standard yes/no info (here, to enable ECO mode) to
* ups.model = Protection Station, Ellipse Eco and 3S (US 750 and AUS 700 only!)
* this allows to enable special flags used in hid_info_t entries (Ie RW) */
static const char *pegasus_yes_no_info_fun(double value)
{
switch (mge_type & 0xFF00) /* Ignore model byte */
{
case MGE_PEGASUS:
break;
case MGE_3S:
/* Only consider non European models */
if (country_code != COUNTRY_EUROPE)
break;
return NULL;
default:
return NULL;
}
return (d_equal(value, 0)) ? "no" : "yes";
}
/* Conversion back of yes/no info */
static double pegasus_yes_no_info_nuf(const char *value)
{
switch (mge_type & 0xFF00) /* Ignore model byte */
{
case MGE_PEGASUS:
break;
case MGE_3S:
/* Only consider non European models */
if (country_code != COUNTRY_EUROPE)
break;
return 0;
default:
return 0;
}
if (!strncmp(value, "yes", 3))
return 1;
else
return 0;
}
static info_lkp_t pegasus_yes_no_info[] = {
{ 0, "no", pegasus_yes_no_info_fun, pegasus_yes_no_info_nuf },
{ 1, "yes", pegasus_yes_no_info_fun, pegasus_yes_no_info_nuf },
{ 0, NULL, NULL, NULL }
};
static info_lkp_t outlet_eco_yes_no_info[] = {
{ 0, "The outlet is not ECO controlled", NULL, NULL },
{ 1, "The outlet is ECO controlled", NULL, NULL },
{ 0, NULL, NULL, NULL }
};
/* Function to check if the current High Efficiency (aka ECO) mode voltage/frequency is within the configured limits */
static const char *eaton_input_eco_mode_check_range(double value)
{
double bypass_voltage;
double eco_low_transfer;
double eco_high_transfer;
double out_voltage_nominal;
double out_frequency_nominal;
double bypass_frequency;
double frequency_range_transfer;
double lower_frequency_limit;
double upper_frequency_limit;
double lower_voltage_limit;
double upper_voltage_limit;
/* Get the ECO mode voltage/frequency and transfer points */
const char *bypass_voltage_str = dstate_getinfo("input.bypass.voltage");
const char *eco_low_transfer_str = dstate_getinfo("input.transfer.eco.low");
const char *eco_high_transfer_str = dstate_getinfo("input.transfer.eco.high");
const char *out_voltage_nominal_str = dstate_getinfo("output.voltage.nominal");
const char *out_frequency_nominal_str = dstate_getinfo("output.frequency.nominal");
const char *frequency_range_transfer_str = dstate_getinfo("input.transfer.frequency.eco.range");
const char *bypass_frequency_str = dstate_getinfo("input.bypass.frequency");
NUT_UNUSED_VARIABLE(value);
if (bypass_voltage_str == NULL || bypass_frequency_str == NULL
|| out_voltage_nominal_str == NULL || out_frequency_nominal_str == NULL
) {
upsdebugx(2, "%s: Failed to get values: "
"input.bypass.voltage = %s, "
"input.bypass.frequency = %s, "
"output.voltage.nominal = %s, "
"output.frequency.nominal = %s",
__func__,
NUT_STRARG(bypass_voltage_str),
NUT_STRARG(bypass_frequency_str),
NUT_STRARG(out_voltage_nominal_str),
NUT_STRARG(out_frequency_nominal_str));
/* Disable ECO mode switching, do not enter ECO mode */
dstate_setinfo("input.eco.switchable", "normal");
upsdebugx(1, "%s: Disable ECO mode due to missing input/output variables.", __func__);
return NULL;
}
/* In case we don't have ECO transfer limit variables
* but still have ability to enter Bypass/ECO modes,
* will use default limits later in code.
* Possibly reported by debug log for 9SX1000i https://github.com/networkupstools/nut/issues/2685
*/
if (eco_low_transfer_str == NULL || eco_high_transfer_str == NULL
|| frequency_range_transfer_str == NULL
) {
upsdebugx(2, "%s: Failed to get values: "
"input.transfer.eco.low = %s, "
"input.transfer.eco.high = %s, "
"input.transfer.frequency.eco.range = %s",
__func__,
NUT_STRARG(eco_low_transfer_str),
NUT_STRARG(eco_high_transfer_str),
NUT_STRARG(frequency_range_transfer_str));
/* Do not return NULL here, we will use default values for limits */
}
str_to_double(bypass_voltage_str, &bypass_voltage, 10);
str_to_double(eco_low_transfer_str, &eco_low_transfer, 10);
str_to_double(eco_high_transfer_str, &eco_high_transfer, 10);
str_to_double(out_voltage_nominal_str, &out_voltage_nominal, 10);
str_to_double(out_frequency_nominal_str, &out_frequency_nominal, 10);
str_to_double(frequency_range_transfer_str, &frequency_range_transfer, 10);
str_to_double(bypass_frequency_str, &bypass_frequency, 10);
/* Default values if user-defined limits are not available or out of range
* 5% below nominal output voltage
* 5% above nominal output voltage
* 5% below/above output frequency nominal
*/
/* Set the frequency limit */
if (frequency_range_transfer > 0) {
lower_frequency_limit = out_frequency_nominal - (out_frequency_nominal / 100 * frequency_range_transfer);
upper_frequency_limit = out_frequency_nominal + (out_frequency_nominal / 100 * frequency_range_transfer);
} else {
/* Set default values if user-defined limits are not available or out of range */
lower_frequency_limit = out_frequency_nominal - (out_frequency_nominal / 100 * 5);
upper_frequency_limit = out_frequency_nominal + (out_frequency_nominal / 100 * 5);
}
/* Set the voltage limit */
if (eco_low_transfer > 0 && eco_high_transfer > 0) {
lower_voltage_limit = eco_low_transfer;
upper_voltage_limit = eco_high_transfer;
} else {
/* Set default values if user-defined limits are not available or out of range */
lower_voltage_limit = out_voltage_nominal * 0.95;
upper_voltage_limit = out_voltage_nominal * 1.05;
}
/* Check if limits are within valid range */
if ((bypass_voltage >= lower_voltage_limit && bypass_voltage <= upper_voltage_limit)
&& (bypass_frequency >= lower_frequency_limit && bypass_frequency <= upper_frequency_limit)
) {
upsdebugx(1, "%s: Entering ECO mode due to input conditions being within the transfer limits.", __func__);
buzzmode_set("vendor:mge-hid:ECO");
return "ECO"; /* Enter ECO mode */
} else {
/* Condensed debug messages for out of range voltage and frequency */
if (bypass_voltage < lower_voltage_limit || bypass_voltage > upper_voltage_limit) {
upsdebugx(1, "Input Bypass voltage is outside ECO transfer limits: %.1f V", bypass_voltage);
}
if (bypass_frequency < lower_frequency_limit || bypass_frequency > upper_frequency_limit) {
upsdebugx(1, "Input Bypass frequency is outside ECO transfer limits: %.1f Hz", bypass_frequency);
}
/* Disable ECO mode switching, do not enter ECO mode */
dstate_setinfo("input.eco.switchable", "normal");