-
-
Notifications
You must be signed in to change notification settings - Fork 374
/
Copy pathapcsmart-old.c
1503 lines (1152 loc) · 33.7 KB
/
apcsmart-old.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
/*
apcsmart.c - driver for APC smart protocol units (originally "newapc")
Copyright (C) 1999 Russell Kroll <[email protected]>
(C) 2000 Nigel Metheringham <[email protected]>
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
*/
#include "main.h"
#include "serial.h"
#include "apcsmart-old.h"
#define DRIVER_NAME "APC Smart protocol driver"
#define DRIVER_VERSION "2.1"
static upsdrv_info_t table_info = {
"APC command table",
APC_TABLE_VERSION,
NULL,
0,
{ NULL }
};
/* driver description structure */
upsdrv_info_t upsdrv_info = {
DRIVER_NAME,
DRIVER_VERSION,
"Russell Kroll <[email protected]>\n"
"Nigel Metheringham <[email protected]>\n"
"Michal Soltys <[email protected]>",
DRV_STABLE,
{ &table_info, NULL }
};
#define ALT_CABLE_1 "940-0095B"
static int ups_status = 0, quirk_capability_overflow = 0;
static apc_vartab_t *vartab_lookup_char(char cmdchar)
{
int i;
for (i = 0; apc_vartab[i].name != NULL; i++)
if (apc_vartab[i].cmd == cmdchar)
return &apc_vartab[i];
return NULL;
}
static apc_vartab_t *vartab_lookup_name(const char *var)
{
int i;
for (i = 0; apc_vartab[i].name != NULL; i++)
if (!strcasecmp(apc_vartab[i].name, var))
return &apc_vartab[i];
return NULL;
}
/* FUTURE: change to use function pointers */
/* convert APC formatting to NUT formatting */
static const char *convert_data(apc_vartab_t *cmd_entry, const char *upsval)
{
static char tmp[128];
int tval;
switch(cmd_entry->flags & APC_FORMATMASK) {
case APC_F_PERCENT:
case APC_F_VOLT:
case APC_F_AMP:
case APC_F_CELSIUS:
case APC_F_HEX:
case APC_F_DEC:
case APC_F_SECONDS:
case APC_F_LEAVE:
/* no conversion for any of these */
return upsval;
case APC_F_HOURS:
/* convert to seconds */
tval = 60 * 60 * strtol(upsval, NULL, 10);
snprintf(tmp, sizeof(tmp), "%d", tval);
return tmp;
case APC_F_MINUTES:
/* Convert to seconds - NUT standard time measurement */
tval = 60 * strtol(upsval, NULL, 10);
/* Ignore errors - Theres not much we can do */
snprintf(tmp, sizeof(tmp), "%d", tval);
return tmp;
case APC_F_REASON:
switch (upsval[0]) {
case 'R': return "unacceptable utility voltage rate of change";
case 'H': return "high utility voltage";
case 'L': return "low utility voltage";
case 'T': return "line voltage notch or spike";
case 'O': return "no transfers yet since turnon";
case 'S': return "simulated power failure or UPS test";
default: return upsval;
}
}
upslogx(LOG_NOTICE, "Unable to handle conversion of %s", cmd_entry->name);
return upsval;
}
static void ups_status_set(void)
{
status_init();
if (ups_status & APC_STAT_CAL)
status_set("CAL"); /* calibration */
if (ups_status & APC_STAT_TRIM)
status_set("TRIM"); /* SmartTrim */
if (ups_status & APC_STAT_BOOST)
status_set("BOOST"); /* SmartBoost */
if (ups_status & APC_STAT_OL)
status_set("OL"); /* on line */
if (ups_status & APC_STAT_OB)
status_set("OB"); /* on battery */
if (ups_status & APC_STAT_OVER)
status_set("OVER"); /* overload */
if (ups_status & APC_STAT_LB)
status_set("LB"); /* low battery */
if (ups_status & APC_STAT_RB)
status_set("RB"); /* replace batt */
if (ups_status == 0)
status_set("OFF");
status_commit();
}
static void alert_handler(char ch)
{
switch (ch) {
case '!': /* clear OL, set OB */
upsdebugx(4, "alert_handler: OB");
ups_status &= ~APC_STAT_OL;
ups_status |= APC_STAT_OB;
break;
case '$': /* clear OB, set OL */
upsdebugx(4, "alert_handler: OL");
ups_status &= ~APC_STAT_OB;
ups_status |= APC_STAT_OL;
break;
case '%': /* set LB */
upsdebugx(4, "alert_handler: LB");
ups_status |= APC_STAT_LB;
break;
case '+': /* clear LB */
upsdebugx(4, "alert_handler: not LB");
ups_status &= ~APC_STAT_LB;
break;
case '#': /* set RB */
upsdebugx(4, "alert_handler: RB");
ups_status |= APC_STAT_RB;
break;
case '?': /* set OVER */
upsdebugx(4, "alert_handler: OVER");
ups_status |= APC_STAT_OVER;
break;
case '=': /* clear OVER */
upsdebugx(4, "alert_handler: not OVER");
ups_status &= ~APC_STAT_OVER;
break;
default:
upsdebugx(4, "alert_handler got 0x%02x (unhandled)", ch);
break;
}
ups_status_set();
}
static int read_buf(char *buf, size_t buflen)
{
int ret;
ret = ser_get_line_alert(upsfd, buf, buflen, ENDCHAR, POLL_IGNORE,
POLL_ALERT, alert_handler, SER_WAIT_SEC, SER_WAIT_USEC);
if (ret < 1) {
ser_comm_fail("%s", ret ? strerror(errno) : "timeout");
return ret;
}
ser_comm_good();
return ret;
}
static int poll_data(apc_vartab_t *vt)
{
int ret;
char tmp[SMALLBUF];
if ((vt->flags & APC_PRESENT) == 0)
return 1;
upsdebugx(4, "poll_data: %s", vt->name);
ret = ser_send_char(upsfd, vt->cmd);
if (ret != 1) {
upslogx(LOG_ERR, "poll_data: ser_send_char failed");
dstate_datastale();
return 0;
}
if (read_buf(tmp, sizeof(tmp)) < 1) {
dstate_datastale();
return 0;
}
/* no longer supported by the hardware somehow */
if (!strcmp(tmp, "NA")) {
dstate_delinfo(vt->name);
return 1;
}
dstate_setinfo(vt->name, "%s", convert_data(vt, tmp));
dstate_dataok();
return 1;
}
/* check for support or just update a named variable */
static int query_ups(const char *var, int first)
{
int ret;
char temp[256];
const char *ptr;
apc_vartab_t *vt;
vt = vartab_lookup_name(var);
if (!vt) {
upsdebugx(1, "query_ups: unknown variable %s", var);
return 0;
}
/*
* not first run and already known to not be supported ?
*/
if (!first && !(vt->flags & APC_PRESENT))
return 0;
/* empty the input buffer (while allowing the alert handler to run) */
ret = ser_get_line_alert(upsfd, temp, sizeof(temp), ENDCHAR,
POLL_IGNORE, POLL_ALERT, alert_handler, 0, 0);
ret = ser_send_char(upsfd, vt->cmd);
if (ret != 1) {
upslog_with_errno(LOG_ERR, "query_ups: ser_send_char failed");
return 0;
}
ret = ser_get_line_alert(upsfd, temp, sizeof(temp), ENDCHAR,
POLL_IGNORE, POLL_ALERT, alert_handler, SER_WAIT_SEC,
SER_WAIT_USEC);
if ((ret < 1) && (first == 0)) {
ser_comm_fail("%s", ret ? strerror(errno) : "timeout");
return 0;
}
ser_comm_good();
if ((ret < 1) || (!strcmp(temp, "NA"))) /* not supported */
return 0;
vt->flags |= APC_PRESENT;
ptr = convert_data(vt, temp);
dstate_setinfo(vt->name, "%s", ptr);
return 1; /* success */
}
static void do_capabilities(void)
{
const char *ptr, *entptr;
char upsloc, temp[512], cmd, loc, etmp[16], *endtemp;
int nument, entlen, i, matrix, ret, valid;
apc_vartab_t *vt;
upsdebugx(1, "APC - About to get capabilities string");
/* If we can do caps, then we need the Firmware revision which has
the locale descriptor as the last character (ugh)
*/
ptr = dstate_getinfo("ups.firmware");
if (ptr)
upsloc = ptr[strlen(ptr) - 1];
else
upsloc = 0;
/* get capability string */
ret = ser_send_char(upsfd, APC_CAPABILITY); /* ^Z */
if (ret != 1) {
upslog_with_errno(LOG_ERR, "do_capabilities: ser_send_char failed");
return;
}
/* note different IGN set since ^Z returns things like # */
ret = ser_get_line(upsfd, temp, sizeof(temp), ENDCHAR,
MINIGNCHARS, SER_WAIT_SEC, SER_WAIT_USEC);
if ((ret < 1) || (!strcmp(temp, "NA"))) {
/* Early Smart-UPS, not as smart as later ones */
/* This should never happen since we only call
this if the REQ_CAPABILITIES command is supported
*/
upslogx(LOG_ERR, "ERROR: APC cannot do capabilities but said it could!");
return;
}
/* recv always puts a \0 at the end, so this is safe */
/* however it assumes a zero byte cannot be embedded */
endtemp = &temp[0] + strlen(temp);
if (temp[0] != '#') {
upsdebugx(1, "Unrecognized capability start char %c", temp[0]);
upsdebugx(1, "Please report this error [%s]", temp);
upslogx(LOG_ERR, "ERROR: unknown capability start char %c!",
temp[0]);
return;
}
if (temp[1] == '#') { /* Matrix-UPS */
matrix = 1;
ptr = &temp[0];
}
else {
ptr = &temp[1];
matrix = 0;
}
/* command char, location, # of entries, entry length */
while (ptr[0] != '\0') {
if (matrix)
ptr += 2; /* jump over repeating ## */
/* check for idiocy */
if (ptr >= endtemp) {
/* if we expected this, just ignore it */
if (quirk_capability_overflow)
return;
fatalx(EXIT_FAILURE,
"Capability string has overflowed\n"
"Please report this error\n"
"ERROR: capability overflow!"
);
}
cmd = ptr[0];
loc = ptr[1];
nument = ptr[2] - 48;
entlen = ptr[3] - 48;
entptr = &ptr[4];
vt = vartab_lookup_char(cmd);
valid = vt && ((loc == upsloc) || (loc == '4'));
/* mark this as writable */
if (valid) {
upsdebugx(1, "Supported capability: %02x (%c) - %s",
cmd, loc, vt->name);
dstate_setflags(vt->name, ST_FLAG_RW);
/* make sure setvar knows what this is */
vt->flags |= APC_RW | APC_ENUM;
}
for (i = 0; i < nument; i++) {
if (valid) {
snprintf(etmp, entlen + 1, "%s", entptr);
dstate_addenum(vt->name, "%s", convert_data(vt, etmp));
}
entptr += entlen;
}
ptr = entptr;
}
}
static int update_status(void)
{
int ret;
char buf[SMALLBUF];
upsdebugx(4, "update_status");
ser_flush_in(upsfd, IGNCHARS, nut_debug_level);
ret = ser_send_char(upsfd, APC_STATUS);
if (ret != 1) {
upslog_with_errno(LOG_ERR, "update_status: ser_send_char failed");
dstate_datastale();
return 0;
}
ret = read_buf(buf, sizeof(buf));
if ((ret < 1) || (!strcmp(buf, "NA"))) {
dstate_datastale();
return 0;
}
ups_status = strtol(buf, 0, 16) & 0xff;
ups_status_set();
dstate_dataok();
return 1;
}
static void oldapcsetup(void)
{
int ret = 0;
/* really old models ignore REQ_MODEL, so find them first */
ret = query_ups("ups.model", 1);
if (ret != 1) {
/* force the model name */
dstate_setinfo("ups.model", "Smart-UPS");
}
/* see if this might be an old Matrix-UPS instead */
if (query_ups("output.current", 1))
dstate_setinfo("ups.model", "Matrix-UPS");
query_ups("ups.serial", 1);
query_ups("input.voltage", 1); /* This one may fail... no problem */
update_status();
/* If we have come down this path then we dont do capabilities and
other shiny features
*/
}
static void protocol_verify(unsigned char cmd)
{
int i, found;
/* see if it's a variable */
for (i = 0; apc_vartab[i].name != NULL; i++) {
/* 1:1 here, so the first match is the only match */
if (apc_vartab[i].cmd == cmd) {
upsdebugx(3, "UPS supports variable [%s]",
apc_vartab[i].name);
/* load initial data */
apc_vartab[i].flags |= APC_PRESENT;
poll_data(&apc_vartab[i]);
/* handle special data for our two strings */
if (apc_vartab[i].flags & APC_STRING) {
dstate_setflags(apc_vartab[i].name,
ST_FLAG_RW | ST_FLAG_STRING);
dstate_setaux(apc_vartab[i].name, APC_STRLEN);
apc_vartab[i].flags |= APC_RW;
}
return;
}
}
/* check the command list */
/* some cmdchars map onto multiple commands (start and stop) */
found = 0;
for (i = 0; apc_cmdtab[i].name != NULL; i++) {
if (apc_cmdtab[i].cmd == cmd) {
upsdebugx(2, "UPS supports command [%s]",
apc_cmdtab[i].name);
dstate_addcmd(apc_cmdtab[i].name);
apc_cmdtab[i].flags |= APC_PRESENT;
found = 1;
}
}
if (found)
return;
if (isprint(cmd))
upsdebugx(1, "protocol_verify: 0x%02x [%c] unrecognized",
cmd, cmd);
else
upsdebugx(1, "protocol_verify: 0x%02x unrecognized", cmd);
}
/* some hardware is a special case - hotwire the list of cmdchars */
static int firmware_table_lookup(void)
{
int ret;
unsigned int i, j;
char buf[SMALLBUF];
upsdebugx(1, "Attempting firmware lookup using command 'V'");
ret = ser_send_char(upsfd, 'V');
if (ret != 1) {
upslog_with_errno(LOG_ERR, "firmware_table_lookup: ser_send_char failed");
return 0;
}
ret = ser_get_line(upsfd, buf, sizeof(buf), ENDCHAR, IGNCHARS,
SER_WAIT_SEC, SER_WAIT_USEC);
/*
* Some UPSes support both 'V' and 'b'. As 'b' doesn't always return
* firmware version, we attempt that only if 'V' doesn't work.
*/
if ((ret < 1) || (!strcmp(buf, "NA"))) {
upsdebugx(1, "Attempting firmware lookup using command 'b'");
ret = ser_send_char(upsfd, 'b');
if (ret != 1) {
upslog_with_errno(LOG_ERR, "firmware_table_lookup: ser_send_char failed");
return 0;
}
ret = ser_get_line(upsfd, buf, sizeof(buf), ENDCHAR, IGNCHARS,
SER_WAIT_SEC, SER_WAIT_USEC);
if (ret < 1) {
upslog_with_errno(LOG_ERR, "firmware_table_lookup: ser_get_line failed");
return 0;
}
}
upsdebugx(2, "Firmware: [%s]", buf);
/* this will be reworked if we get a lot of these things */
if (!strcmp(buf, "451.2.I")) {
quirk_capability_overflow = 1;
return 0;
}
for (i = 0; compat_tab[i].firmware != NULL; i++) {
if (!strcmp(compat_tab[i].firmware, buf)) {
upsdebugx(2, "Matched - cmdchars: %s",
compat_tab[i].cmdchars);
if (strspn(compat_tab[i].firmware, "05")) {
dstate_setinfo("ups.model", "Matrix-UPS");
} else {
dstate_setinfo("ups.model", "Smart-UPS");
}
/* matched - run the cmdchars from the table */
for (j = 0; j < strlen(compat_tab[i].cmdchars); j++)
protocol_verify(compat_tab[i].cmdchars[j]);
return 1; /* matched */
}
}
upsdebugx(2, "Not found in table - trying normal method");
return 0;
}
static void getbaseinfo(void)
{
unsigned int i;
int ret = 0;
char *alrts, *cmds, temp[512];
/*
* try firmware lookup first; we could start with 'a', but older models
* sometimes return other things than a command set
*/
if (firmware_table_lookup() == 1)
return;
upsdebugx(1, "APC - Attempting to find command set");
/* Initially we ask the UPS what commands it takes
If this fails we are going to need an alternate
strategy - we can deal with that if it happens
*/
ret = ser_send_char(upsfd, APC_CMDSET);
if (ret != 1) {
upslog_with_errno(LOG_ERR, "getbaseinfo: ser_send_char failed");
return;
}
ret = ser_get_line(upsfd, temp, sizeof(temp), ENDCHAR, IGNCHARS,
SER_WAIT_SEC, SER_WAIT_USEC);
if ((ret < 1) || (!strcmp(temp, "NA"))) {
/* We have an old dumb UPS - go to specific code for old stuff */
oldapcsetup();
return;
}
upsdebugx(1, "APC - Parsing out command set");
/* We have the version.alert.cmdchars string
NB the alert chars are normally in IGNCHARS
so will have been pretty much edited out.
You will need to change the ser_get_line above if
you want to check those out too....
*/
alrts = strchr(temp, '.');
if (alrts == NULL) {
fatalx(EXIT_FAILURE, "Unable to split APC version string");
}
*alrts++ = 0;
cmds = strchr(alrts, '.');
if (cmds == NULL) {
fatalx(EXIT_FAILURE, "Unable to find APC command string");
}
*cmds++ = 0;
for (i = 0; i < strlen(cmds); i++)
protocol_verify(cmds[i]);
/* if capabilities are supported, add them here */
if (strchr(cmds, APC_CAPABILITY))
do_capabilities();
upsdebugx(1, "APC - UPS capabilities determined");
}
/* check for calibration status and either start or stop */
static int do_cal(int start)
{
char temp[256];
int tval, ret;
ret = ser_send_char(upsfd, APC_STATUS);
if (ret != 1) {
upslog_with_errno(LOG_ERR, "do_cal: ser_send_char failed");
return STAT_INSTCMD_HANDLED; /* FUTURE: failure */
}
ret = read_buf(temp, sizeof(temp));
/* if we can't check the current calibration status, bail out */
if ((ret < 1) || (!strcmp(temp, "NA")))
return STAT_INSTCMD_HANDLED; /* FUTURE: failure */
tval = strtol(temp, 0, 16);
if (tval & APC_STAT_CAL) { /* calibration currently happening */
if (start == 1) {
/* requested start while calibration still running */
upslogx(LOG_INFO, "Runtime calibration already in progress");
return STAT_INSTCMD_HANDLED; /* FUTURE: failure */
}
/* stop requested */
upslogx(LOG_INFO, "Stopping runtime calibration");
ret = ser_send_char(upsfd, APC_CMD_CALTOGGLE);
if (ret != 1) {
upslog_with_errno(LOG_ERR, "do_cal: ser_send_char failed");
return STAT_INSTCMD_HANDLED; /* FUTURE: failure */
}
ret = read_buf(temp, sizeof(temp));
if ((ret < 1) || (!strcmp(temp, "NA")) || (!strcmp(temp, "NO"))) {
upslogx(LOG_WARNING, "Stop calibration failed: %s",
temp);
return STAT_INSTCMD_HANDLED; /* FUTURE: failure */
}
return STAT_INSTCMD_HANDLED; /* FUTURE: success */
}
/* calibration not happening */
if (start == 0) { /* stop requested */
upslogx(LOG_INFO, "Runtime calibration not occurring");
return STAT_INSTCMD_HANDLED; /* FUTURE: failure */
}
upslogx(LOG_INFO, "Starting runtime calibration");
ret = ser_send_char(upsfd, APC_CMD_CALTOGGLE);
if (ret != 1) {
upslog_with_errno(LOG_ERR, "do_cal: ser_send_char failed");
return STAT_INSTCMD_HANDLED; /* FUTURE: failure */
}
ret = read_buf(temp, sizeof(temp));
if ((ret < 1) || (!strcmp(temp, "NA")) || (!strcmp(temp, "NO"))) {
upslogx(LOG_WARNING, "Start calibration failed: %s", temp);
return STAT_INSTCMD_HANDLED; /* FUTURE: failure */
}
return STAT_INSTCMD_HANDLED; /* FUTURE: success */
}
/* get the UPS talking to us in smart mode */
static int smartmode(void)
{
int ret, tries;
char temp[256];
for (tries = 0; tries < 5; tries++) {
ret = ser_send_char(upsfd, APC_GOSMART);
if (ret != 1) {
upslog_with_errno(LOG_ERR, "smartmode: ser_send_char failed");
return 0;
}
ret = ser_get_line(upsfd, temp, sizeof(temp), ENDCHAR,
IGNCHARS, SER_WAIT_SEC, SER_WAIT_USEC);
if (ret > 0)
if (!strcmp(temp, "SM"))
return 1; /* success */
sleep(1); /* wait before trying again */
/* it failed, so try to bail out of menus on newer units */
ret = ser_send_char(upsfd, 27); /* ESC */
if (ret != 1) {
upslog_with_errno(LOG_ERR, "smartmode: ser_send_char failed");
return 0;
}
/* eat the response (might be NA, might be something else) */
ret = ser_get_line(upsfd, temp, sizeof(temp), ENDCHAR,
IGNCHARS, SER_WAIT_SEC, SER_WAIT_USEC);
}
return 0; /* failure */
}
/*
* all shutdown commands should respond with 'OK' or '*'
*/
static int sdok(void)
{
char temp[16];
ser_get_line(upsfd, temp, sizeof(temp), ENDCHAR, IGNCHARS, SER_WAIT_SEC, SER_WAIT_USEC);
upsdebugx(4, "sdok: got \"%s\"", temp);
if (!strcmp(temp, "*") || !strcmp(temp, "OK")) {
upsdebugx(4, "Last issued shutdown command succeeded");
return 1;
}
upsdebugx(1, "Last issued shutdown command failed");
return 0;
}
/* soft hibernate: S - working only when OB, otherwise ignored */
static int sdcmd_S(int dummy)
{
ser_flush_in(upsfd, IGNCHARS, nut_debug_level);
upsdebugx(1, "Issuing soft hibernate");
ser_send_char(upsfd, APC_CMD_SOFTDOWN);
return sdok();
}
/* soft hibernate, hack version for CS 350 */
static int sdcmd_CS(int tval)
{
upsdebugx(1, "Using CS 350 'force OB' shutdown method");
if (tval & APC_STAT_OL) {
upsdebugx(1, "On-line - forcing OB temporarily");
ser_send_char(upsfd, 'U');
usleep(UPSDELAY);
}
return sdcmd_S(tval);
}
/*
* hard hibernate: @nnn / @nn
* note: works differently for older and new models, see help function for
* detailed info
*/
static int sdcmd_ATn(int cnt)
{
int n = 0, mmax, ret;
const char *strval;
char timer[4];
mmax = cnt == 2 ? 99 : 999;
if ((strval = getval("wugrace"))) {
errno = 0;
n = strtol(strval, NULL, 10);
if (errno || n < 0 || n > mmax)
n = 0;
}
snprintf(timer, sizeof(timer), "%.*d", cnt, n);
ser_flush_in(upsfd, IGNCHARS, nut_debug_level);
upsdebugx(1, "Issuing hard hibernate with %d minutes additional wakeup delay", n*6);
ser_send_char(upsfd, APC_CMD_GRACEDOWN);
usleep(CMDLONGDELAY);
ser_send_pace(upsfd, UPSDELAY, "%s", timer);
ret = sdok();
if (ret || cnt == 3)
return ret;
/*
* "tricky" part - we tried @nn variation and it (unsurprisingly)
* failed; we have to abort the sequence with something bogus to have
* the clean state; newer upses will respond with 'NO', older will be
* silent (YMMV);
*/
ser_send_char(upsfd, APC_CMD_GRACEDOWN);
usleep(UPSDELAY);
ser_flush_in(upsfd, IGNCHARS, nut_debug_level);
return 0;
}
/* shutdown: K - delayed poweroff */
static int sdcmd_K(int dummy)
{
ser_flush_in(upsfd, IGNCHARS, nut_debug_level);
upsdebugx(1, "Issuing delayed poweroff");
ser_send_char(upsfd, APC_CMD_SHUTDOWN);
usleep(CMDLONGDELAY);
ser_send_char(upsfd, APC_CMD_SHUTDOWN);
return sdok();
}
/* shutdown: Z - immediate poweroff */
static int sdcmd_Z(int dummy)
{
ser_flush_in(upsfd, IGNCHARS, nut_debug_level);
upsdebugx(1, "Issuing immediate poweroff");
ser_send_char(upsfd, APC_CMD_OFF);
usleep(CMDLONGDELAY);
ser_send_char(upsfd, APC_CMD_OFF);
return sdok();
}
static int (*sdlist[])(int) = {
sdcmd_S,
sdcmd_ATn, /* for @nnn version */
sdcmd_K,
sdcmd_Z,
sdcmd_CS,
sdcmd_ATn, /* for @nn version */
};
#define SDIDX_S 0
#define SDIDX_AT3N 1
#define SDIDX_K 2
#define SDIDX_Z 3
#define SDIDX_CS 4
#define SDIDX_AT2N 5
#define SDCNT 6
static void upsdrv_shutdown_simple(int status)
{
unsigned int sdtype = 0;
char *strval;
if ((strval = getval("sdtype"))) {
errno = 0;
sdtype = strtol(strval, NULL, 10);
if (errno || sdtype < 0 || sdtype > 6)
sdtype = 0;
}
switch (sdtype) {
case 6: /* hard hibernate */
sdcmd_ATn(3);
break;
case 5: /* "hack nn" hard hibernate */
sdcmd_ATn(2);
break;
case 4: /* special hack for CS 350 and similar models */
sdcmd_CS(status);
break;
case 3: /* delayed poweroff */
sdcmd_K(0);
break;
case 2: /* instant poweroff */
sdcmd_Z(0);
break;
case 1:
/*
* Send a combined set of shutdown commands which can work
* better if the UPS gets power during shutdown process
* Specifically it sends both the soft shutdown 'S' and the
* hard hibernate '@nnn' commands
*/
upsdebugx(1, "UPS - currently %s - sending soft/hard hibernate commands",
(status & APC_STAT_OL) ? "on-line" : "on battery");
/* S works only when OB */
if ((status & APC_STAT_OB) && sdcmd_S(0))
break;
sdcmd_ATn(3);
break;
default:
/*
* Send @nnn or S, depending on OB / OL status
*/
if (status & APC_STAT_OL) /* on line */
sdcmd_ATn(3);
else
sdcmd_S(0);
}
}
static void upsdrv_shutdown_advanced(int status)
{
const char *strval;
const char deforder[] = {48 + SDIDX_S,
48 + SDIDX_AT3N,
48 + SDIDX_K,
48 + SDIDX_Z,
0};
size_t i;
int n;
strval = getval("advorder");
/* sanitize advorder */
if (!strval || !strlen(strval) || strlen(strval) > SDCNT)
strval = deforder;
for (i = 0; i < strlen(strval); i++) {
if (strval[i] - 48 < 0 || strval[i] - 48 >= SDCNT) {
strval = deforder;
break;
}
}