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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
|
/*
* wattsup - Program for controlling the Watts Up? Pro Device
*
*
* Copyright (c) 2005 Patrick Mochel
*
* This program is released under the GPLv2
*
*
* Compiled with:
*
* gcc -O2 -Wall -o wattsup wattsup.c
*
*/
#define _GNU_SOURCE
#include<stdio.h>
#include<stdlib.h>
#include<stdarg.h>
#include<string.h>
#include<errno.h>
#include<unistd.h>
#include<fcntl.h>
#include<termios.h>
#include<ctype.h>
#include<getopt.h>
#include<signal.h>
#include<time.h>
#include<sys/stat.h>
static const char * wu_version = "0.02";
#define ARRAY_SIZE(array) (sizeof(array) / sizeof(array[0]))
static const char * prog_name = "wattsup";
static const char * sysfs_path_start = "/sys/class/tty";
static char * wu_device = "ttyUSB0";
static int wu_fd = 0;
static int wu_count = 0;
static int wu_debug = 0;
static char *wu_delim = ", ";
static int wu_final = 0;
static int wu_interval = 1;
static int wu_label = 0;
static int wu_newline = 0;
static int wu_suppress = 0;
static int wu_localtime = 0;
static int wu_gmtime = 0;
static int wu_info_all = 0;
static int wu_no_data = 0;
static int wu_set_only = 0;
#define wu_strlen 256
#define wu_num_fields 18
#define wu_param_len 16
struct wu_packet {
unsigned int cmd;
unsigned int sub_cmd;
unsigned int count;
char buf[wu_strlen];
int len;
char * field[wu_num_fields];
char * label[wu_num_fields];
};
struct wu_data {
unsigned int watts;
unsigned int volts;
unsigned int amps;
unsigned int watt_hours;
unsigned int cost;
unsigned int mo_kWh;
unsigned int mo_cost;
unsigned int max_watts;
unsigned int max_volts;
unsigned int max_amps;
unsigned int min_watts;
unsigned int min_volts;
unsigned int min_amps;
unsigned int power_factor;
unsigned int duty_cycle;
unsigned int power_cycle;
};
struct wu_options {
char * longopt;
int shortopt;
int param;
int flag;
char * value;
char * descr;
char * option;
char * format;
int (*show)(int dev_fd);
int (*store)(int dev_fd);
};
enum {
wu_option_help = 0,
wu_option_version,
wu_option_debug,
wu_option_count,
wu_option_final,
wu_option_delim,
wu_option_newline,
wu_option_localtime,
wu_option_gmtime,
wu_option_label,
wu_option_suppress,
wu_option_cal,
wu_option_header,
wu_option_interval,
wu_option_mode,
wu_option_user,
wu_option_info_all,
wu_option_no_data,
wu_option_set_only,
};
static char * wu_option_value(unsigned int index);
enum {
wu_field_watts = 0,
wu_field_volts,
wu_field_amps,
wu_field_watt_hours,
wu_field_cost,
wu_field_mo_kwh,
wu_field_mo_cost,
wu_field_max_watts,
wu_field_max_volts,
wu_field_max_amps,
wu_field_min_watts,
wu_field_min_volts,
wu_field_min_amps,
wu_field_power_factor,
wu_field_duty_cycle,
wu_field_power_cycle,
};
struct wu_field {
unsigned int enable;
char * name;
char * descr;
};
static struct wu_field wu_fields[wu_num_fields] = {
[wu_field_watts] = {
.name = "watts",
.descr = "Watt Consumption",
},
[wu_field_min_watts] = {
.name = "min-watts",
.descr = "Minimum Watts Consumed",
},
[wu_field_max_watts] = {
.name = "max-watts",
.descr = "Maxium Watts Consumed",
},
[wu_field_volts] = {
.name = "volts",
.descr = "Volts Consumption",
},
[wu_field_min_volts] = {
.name = "max-volts",
.descr = "Minimum Volts Consumed",
},
[wu_field_max_volts] = {
.name = "min-volts",
.descr = "Maximum Volts Consumed",
},
[wu_field_amps] = {
.name = "amps",
.descr = "Amp Consumption",
},
[wu_field_min_amps] = {
.name = "min-amps",
.descr = "Minimum Amps Consumed",
},
[wu_field_max_amps] = {
.name = "max-amps",
.descr = "Maximum Amps Consumed",
},
[wu_field_watt_hours] = {
.name = "kwh",
.descr = "Average KWH",
},
[wu_field_mo_kwh] = {
.name = "mo-kwh",
.descr = "Average monthly KWH",
},
[wu_field_cost] = {
.name = "cost",
.descr = "Cost per watt",
},
[wu_field_mo_cost] = {
.name = "mo-cost",
.descr = "Monthly Cost",
},
[wu_field_power_factor] = {
.name = "power-factor",
.descr = "Ratio of Watts vs. Volt Amps",
},
[wu_field_duty_cycle] = {
.name = "duty-cycle",
.descr = "Percent of the Time On vs. Time Off",
},
[wu_field_power_cycle] = {
.name = "power-cycle",
.descr = "Indication of power cycle",
},
};
static void msg_start(const char * fmt, ...)
{
va_list(ap);
va_start(ap, fmt);
vprintf(fmt, ap);
va_end(ap);
}
static void msg_end(void)
{
printf("\n");
}
static void msg(const char * fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vprintf(fmt, ap);
va_end(ap);
}
static void dbg(const char * fmt, ...)
{
va_list ap;
if (wu_debug) {
va_start(ap, fmt);
msg_start("%s: [debug] ", prog_name);
vprintf(fmt, ap);
msg_end();
va_end(ap);
}
}
static void err(const char * fmt, ...)
{
va_list ap;
va_start(ap, fmt);
fprintf(stderr, "%s: [error] ", prog_name);
vfprintf(stderr, fmt, ap);
fprintf(stderr, "\n");
va_end(ap);
}
static void perr(const char * fmt, ...)
{
char buf[1024];
int n;
va_list ap;
va_start(ap, fmt);
n = sprintf(buf, "%s: [error] ", prog_name);
vsnprintf(buf + n, sizeof(buf) - n, fmt, ap);
perror(buf);
va_end(ap);
}
static int ret_err(int err)
{
errno = err;
return -1;
}
static void print_packet(struct wu_packet * p, char * str)
{
int i;
if (!wu_suppress)
msg_start("Watts Up? %s\n", str);
for (i = 0; i< p->count; i++) {
if (i)
msg("%s", wu_newline ? "\n" : wu_delim);
if (wu_label)
msg("[%s] ", p->label[i]);
msg(p->field[i]);
}
msg_end();
}
static void print_time(void)
{
time_t t;
struct tm * tm;
if (wu_localtime || wu_gmtime) {
time(&t);
if (wu_localtime)
tm = localtime(&t);
else
tm = gmtime(&t);
msg("[%02d:%02d:%02d] ",
tm->tm_hour, tm->tm_min, tm->tm_sec);
}
}
static void print_packet_filter(struct wu_packet * p,
int (*filter_ok)(struct wu_packet * p, int i, char * str))
{
char buf[256];
int printed;
int i;
print_time();
for (i = 0, printed = 0; i< p->count; i++) {
if (!filter_ok(p, i, buf))
continue;
if (printed++)
msg("%s", wu_newline ? "\n" : wu_delim);
if (wu_label)
msg("[%s] ", p->label[i]);
msg(buf);
}
msg_end();
}
/*
* Device should be something like "ttyS0"
*/
static int open_device(char * device_name, int * dev_fd)
{
struct stat s;
int ret;
int cur_fd;
cur_fd = open(".", O_RDONLY);
if (cur_fd< 0) {
perr("Could not open current directory.");
return cur_fd;
}
ret = chdir(sysfs_path_start);
if (ret) {
perr(sysfs_path_start);
return ret;
}
/*
* First, check if /sys/class/tty/<name>/ exists.
*/
dbg("Checking sysfs path: %s/%s", sysfs_path_start, device_name);
ret = stat(device_name,&s);
if (ret< 0) {
perr(device_name);
goto Done;
}
if (!S_ISDIR(s.st_mode)) {
errno = -ENOTDIR;
err("%s is not a TTY device.", device_name);
goto Done;
}
dbg("%s is a registered TTY device", device_name);
fchdir(cur_fd);
/*
* Check if device node exists and is writable
*/
chdir("/dev");
ret = stat(device_name,&s);
if (ret< 0) {
perr("/dev/%s (device node)", device_name);
goto Done;
}
if (!S_ISCHR(s.st_mode)) {
errno = -ENOTTY;
ret = -1;
err("%s is not a TTY character device.", device_name);
goto Done;
}
dbg("%s has a device node", device_name);
ret = access(device_name, R_OK | W_OK);
if (ret) {
perr("%s: Not writable?", device_name);
goto Done;
}
ret = open(device_name, O_RDWR | O_NONBLOCK);
if (ret< 0) {
perr("Could not open %s");
goto Done;
}
*dev_fd = ret;
ret = 0;
Done:
fchdir(cur_fd);
close(cur_fd);
return ret;
}
static int setup_serial_device(int dev_fd)
{
struct termios t;
int ret;
ret = tcgetattr(dev_fd,&t);
if (ret)
return ret;
cfmakeraw(&t);
cfsetispeed(&t, B115200);
cfsetospeed(&t, B115200);
tcflush(dev_fd, TCIFLUSH);
t.c_iflag |= IGNPAR;
t.c_cflag&= ~CSTOPB;
ret = tcsetattr(dev_fd, TCSANOW,&t);
if (ret) {
perr("setting terminal attributes");
return ret;
}
return 0;
}
static int wu_write(int fd, struct wu_packet * p)
{
int ret;
int n;
int i;
char * s = p->buf;
memset(p->buf, 0, sizeof(p->buf));
n = sprintf(p->buf, "#%c,%c,%d", p->cmd, p->sub_cmd, p->count);
p->len = n;
s = p->buf + n;
for (i = 0; i< p->count; i++) {
if ((p->len + strlen(p->field[i]) + 4)>= sizeof(p->buf)) {
err("Overflowed command string");
return ret_err(EOVERFLOW);
}
n = sprintf(s, ",%s", p->field[i]);
s += n;
p->len += n;
}
p->buf[p->len++] = ';';
dbg("Writing '%s' (strlen = %d) (len = %d) to device",
p->buf, strlen(p->buf), p->len);
ret = write(fd, p->buf, p->len);
if (ret != p->len)
perr("Writing to device");
return ret>= 0 ? 0 : ret;
}
static void dump_packet(struct wu_packet * p)
{
int i;
dbg("Packet - Command '%c' %d parameters", p->cmd, p->count);
for (i = 0; i< p->count; i++)
dbg("[%2d] [%20s] = \"%s\"", i, p->label[i], p->field[i]);
}
static int parse_packet(struct wu_packet * p)
{
char * s, *next;
int i;
p->buf[p->len] = '\0';
dbg("Parsing Packet, Raw buffer is (%d bytes) [%s]",
p->len, p->buf);
s = p->buf;
/*
* First character should be '#'
*/
if (s) {
s = strchr(s, '#');
if (s)
s++;
else {
dbg("Invalid packet");
return ret_err(EFAULT);
}
} else {
dbg("Invalid packet");
return ret_err(EFAULT);
}
/*
* Command character is first
*/
next = strchr(s, ',');
if (next) {
p->cmd = *s;
s = ++next;
} else {
dbg("Invalid Command field [%s]", s);
return ret_err(EFAULT);
}
/*
* Next character is the subcommand, and should be '-'
* Though, it doesn't matter, because we just
* discard it anyway.
*/
next = strchr(s, ',');
if (next) {
p->sub_cmd = *s;
s = ++next;
} else {
dbg("Invalid 2nd field");
return ret_err(EFAULT);
}
/*
* Next is the number of parameters,
* which should always be> 0.
*/
next = strchr(s, ',');
if (next) {
*next++ = '\0';
p->count = atoi(s);
s = next;
} else {
dbg("Couldn't determine number of parameters");
return ret_err(EFAULT);
}
dbg("Have %d parameter%s (cmd = '%c')",
p->count, p->count> 1 ? "s" : "", p->cmd);
/*
* Now, we loop over the rest of the string,
* storing a pointer to each in p->field[].
*
* The last character was originally a ';', but may have been
* overwritten with a '\0', so we make sure to catch
* that when converting the last parameter.
*/
for (i = 0; i< p->count; i++) {
next = strpbrk(s, ",;");
if (next) {
*next++ = '\0';
} else {
if (i< (p->count - 1)) {
dbg("Malformed parameter string [%s]", s);
return ret_err(EFAULT);
}
}
/*
* Skip leading white space in fields
*/
while (isspace(*s))
s++;
p->field[i] = s;
s = next;
}
dump_packet(p);
return 0;
}
static int wu_read(int fd, struct wu_packet * p)
{
fd_set read_fd;
struct timeval tv;
int ret;
FD_ZERO(&read_fd);
FD_SET(fd,&read_fd);
tv.tv_sec = 2;
tv.tv_usec = 0;
ret = select(fd + 1,&read_fd, NULL, NULL,&tv);
if (ret< 0) {
perr("select on terminal device");
return ret;
} else if (ret> 0) {
ret = read(fd, p->buf, wu_strlen);
if (ret< 0) {
perr("Reading from device");
return ret;
}
p->len = ret;
} else {
dbg("Device timed out while reading");
return ret_err(ETIME);
}
return parse_packet(p);
}
static int wu_show_header(int fd)
{
struct wu_packet p = {
.cmd = 'H',
.sub_cmd = 'R',
.count = 0,
.label = {
[0] = "watts header",
[1] = "volts header",
[2] = "amps header",
[3] = "kWh header",
[4] = "cost header",
[5] = "mo. kWh header",
[6] = "mo. cost header",
[7] = "max watts header",
[8] = "max volts header",
[9] = "max amps header",
[10] = "min watts header",
[11] = "min volts header",
[12] = "min amps header",
[13] = "power factor header",
[14] = "duty cycle header",
[15] = "power cycle header",
}
};
int ret;
ret = wu_write(fd,&p);
if (ret) {
perr("Requesting header strings");
return ret;
}
sleep(1);
ret = wu_read(fd,&p);
if (ret) {
perr("Reading header strings");
return ret;
}
print_packet(&p, "Header Record");
return 0;
}
static int wu_show_cal(int fd)
{
struct wu_packet p = {
.cmd = 'F',
.sub_cmd = 'R',
.count = 0,
.label = {
[0] = "flags",
[1] = "sample count",
[2] = "volts gain",
[3] = "volts bias",
[4] = "amps gain",
[5] = "amps bias",
[6] = "amps offset",
[7] = "low amps gain",
[8] = "low amps bias",
[9] = "low amps offset",
[10] = "watts gain",
[11] = "watts offset",
[12] = "low watts gain",
[13] = "low watts offset",
},
};
int ret;
ret = wu_write(fd,&p);
if (ret) {
perr("Requesting calibration parameters");
return ret;
}
sleep(1);
ret = wu_read(fd,&p);
if (ret) {
perr("Reading header strings");
return ret;
}
print_packet(&p, "Calibration Settings");
return 0;
}
static int wu_start_log(void)
{
struct wu_packet p = {
.cmd = 'L',
.sub_cmd = 'W',
.count = 3,
.field = {
[0] = "E",
[1] = "1",
[2] = "1",
},
};
int ret;
/*
* Start up logging
*/
ret = wu_write(wu_fd,&p);
if (!ret)
sleep(1);
else {
perr("Starting External Logging");
return ret;
}
return ret;
}
static int wu_stop_log(void)
{
struct wu_packet p = {
.cmd = 'L',
.sub_cmd = 'R',
.count = 0,
.label = {
[0] = "time stamp",
[1] = "interval",
},
};
int ret;
/*
* Stop logging and read time stamp.
*/
ret = wu_write(wu_fd,&p);
if (ret) {
perr("Stopping External Logging");
return ret;
}
sleep(1);
ret = wu_read(wu_fd,&p);
if (ret) {
perr("Reading final time stamp");
return ret;
}
if (wu_final)
print_packet(&p, "Final Time Stamp and Interval");
return ret;
}
static int filter_data(struct wu_packet * p, int i, char * buf)
{
if (i< wu_num_fields) {
if (wu_fields[i].enable) {
double val = strtod(p->field[i], NULL);
snprintf(buf, 256, "%.1f", val / 10.0);
return 1;
}
}
return 0;
}
static int wu_clear(int fd)
{
struct wu_packet p = {
.cmd = 'R',
.sub_cmd = 'W',
.count = 0,
};
int ret;
/*
* Clear the memory
*/
ret = wu_write(fd,&p);
if (ret)
perr("Clearing memory");
else
sleep(2);
/*
* Dummy read
*/
wu_read(fd,&p);
return ret;
}
static int wu_read_data(int fd)
{
struct wu_packet p = {
.label = {
[0] = "watts",
[1] = "volts",
[2] = "amps",
[3] = "watt hours",
[4] = "cost",
[5] = "mo. kWh",
[6] = "mo. cost",
[7] = "max watts",
[8] = "max volts",
[9] = "max amps",
[10] = "min watts",
[11] = "min volts",
[12] = "min amps",
[13] = "power factor",
[14] = "duty cycle",
[15] = "power cycle",
},
};
int num_read = 0;
int retry = 0;
int ret;
int i;
static const int wu_max_retry = 2;
i = 0;
while (1) {
ret = wu_read(fd,&p);
if (ret) {
if (++retry< wu_max_retry) {
dbg("Bad record back, retrying\n");
sleep(wu_interval);
continue;
} else if (retry == wu_max_retry) {
dbg("Still couldn't get a good record, resetting\n");
wu_stop_log();
wu_clear(fd);
wu_start_log();
num_read = 0;
sleep(wu_interval);
continue;
}
perr("Blech. Giving up on read");
break;
} else if (retry)
retry = 0;
dbg("[%d] ", num_read);
num_read++;
print_packet_filter(&p, filter_data);
if (wu_count&& (++i == wu_count))
break;
sleep(wu_interval);
}
return 0;
}
static int wu_show_interval(int fd)
{
struct wu_packet p = {
.cmd = 'S',
.sub_cmd = 'R',
.count = 0,
.label = {
[0] = "reserved",
[1] = "interval",
},
};
int ret;
ret = wu_write(fd,&p);
if (ret) {
perr("Requesting interval");
return ret;
}
sleep(1);
ret = wu_read(fd,&p);
if (ret) {
perr("Reading interval");
return ret;
}
print_packet(&p, "Interval Settings");
return 0;
}
static int wu_write_interval(int fd, unsigned int seconds,
unsigned int interval)
{
char str_seconds[wu_param_len];
char str_interval[wu_param_len];
struct wu_packet p = {
.cmd = 'S',
.sub_cmd = 'W',
.count = 2,
.field = {
[0] = str_seconds,
[1] = str_interval,
},
};
int ret;
snprintf(str_seconds, wu_param_len, "%ud", seconds);
snprintf(str_interval, wu_param_len, "%ud", interval);
ret = wu_write(fd,&p);
if (ret) {
perr("Setting Sampling Interval");
return ret;
}
sleep(1);
return 0;
}
static int wu_store_interval(int fd)
{
char * s = wu_option_value(wu_option_interval);
char * end;
wu_interval = strtol(s,&end, 0);
if (*end) {
err("Invalid interval: %s", s);
return ret_err(EINVAL);
}
return wu_write_interval(fd, 1, wu_interval);
}
static int wu_show_mode(int fd)
{
struct wu_packet p = {
.cmd = 'M',
.sub_cmd = 'R',
.count = 0,
.label = {
[0] = "display mode",
},
};
int ret;
ret = wu_write(fd,&p);
if (ret) {
perr("Requesting device display mode");
return ret;
}
ret = wu_read(fd,&p);
if (ret) {
perr("Reaing device display mode");
return ret;
}
dump_packet(&p);
return ret;
}
static int wu_write_mode(int fd, int mode)
{
char str_mode[wu_param_len];
struct wu_packet p = {
.cmd = 'M',
.sub_cmd = 'W',
.count = 1,
.field = {
[0] = str_mode,
},
};
int ret;
snprintf(str_mode, wu_param_len, "%ud", mode);
ret = wu_write(fd,&p);
if (ret)
perr("Setting device display mode");
else
sleep(1);
return ret;
}
static int wu_store_mode(int fd)
{
char * s = wu_option_value(wu_option_mode);
char * end;
unsigned int mode;
mode = strtol(s,&end, 0);
if (*end) {
err("Invalid mode: %s", s);
return ret_err(EINVAL);
}
return wu_write_mode(fd, mode);
}
static int wu_show_user(int fd)
{
struct wu_packet p = {
.cmd = 'U',
.sub_cmd = 'R',
.count = 0,
.label = {
[0] = "cost per kWh",
[1] = "2nd tier cost",
[2] = "2nd tier threshold",
[3] = "duty cycle threshold",
},
};
int ret;
ret = wu_write(fd,&p);
if (ret) {
perr("Requesting user parameters");
return ret;
}
sleep(1);
ret = wu_read(fd,&p);
if (ret) {
perr("Reading user parameters");
return ret;
}
print_packet(&p, "User Settings");
return 0;
}
static int wu_write_user(int fd, unsigned int kwh_cost,
unsigned int second_tier_cost,
unsigned int second_tier_threshold,
unsigned int duty_cycle_threshold)
{
char str_kwh_cost[wu_param_len];
char str_2nd_tier_cost[wu_param_len];
char str_2nd_tier_threshold[wu_param_len];
char str_duty_cycle_threshold[wu_param_len];
struct wu_packet p = {
.cmd = 'U',
.sub_cmd = 'R',
.count = 0,
.label = {
[0] = str_kwh_cost,
[1] = str_2nd_tier_cost,
[2] = str_2nd_tier_threshold,
[3] = str_duty_cycle_threshold,
},
};
int ret;
snprintf(str_kwh_cost, wu_param_len, "%ud", kwh_cost);
snprintf(str_2nd_tier_cost, wu_param_len, "%ud",
second_tier_cost);
snprintf(str_2nd_tier_threshold, wu_param_len, "%ud",
second_tier_threshold);
snprintf(str_duty_cycle_threshold, wu_param_len, "%ud",
duty_cycle_threshold);
ret = wu_write(fd,&p);
if (ret)
perr("Writing user parameters");
else
sleep(1);
return ret;
}
static int wu_store_user(int fd)
{
unsigned int kwh_cost;
unsigned int second_tier_cost;
unsigned int second_tier_threshold;
unsigned int duty_cycle_threshold;
char * buf = wu_option_value(wu_option_user);
char * s = buf;
char * next;
if (!buf) {
err("No user parameters?");
return ret_err(EINVAL);
}
kwh_cost = strtoul(s,&next, 0);
if (next == s) {
err("Incomplete user parameters");
return ret_err(EINVAL);
}
s = next;
while (s&& !isdigit(*s))
s++;
if (!s) {
err("Incomplete user parameters");
return ret_err(EINVAL);
}
second_tier_cost = strtoul(s,&next, 0);
if (next == s) {
err("Incomplete user parameters");
return ret_err(EINVAL);
}
s = next;
while (s&& !isdigit(*s))
s++;
if (!s) {
err("Incomplete user parameters");
return ret_err(EINVAL);
}
second_tier_threshold = strtoul(s,&next, 0);
if (next == s) {
err("Incomplete user parameters");
return ret_err(EINVAL);
}
s = next;
while (s&& !isdigit(*s))
s++;
if (!s) {
err("Incomplete user parameters");
return ret_err(EINVAL);
}
duty_cycle_threshold = strtoul(s,&next, 0);
if (next == s) {
err("Incomplete user parameters");
return ret_err(EINVAL);
}
s = next;
while (s&& !isdigit(*s))
s++;
if (!s) {
err("Incomplete user parameters");
return ret_err(EINVAL);
}
return wu_write_user(fd, kwh_cost, second_tier_cost,
second_tier_threshold, duty_cycle_threshold);
}
static void enable_field(char * name)
{
int i;
for (i = 0; i< wu_num_fields; i++) {
if (!strcasecmp(wu_fields[i].name, name)) {
wu_fields[i].enable = 1;
break;
}
}
}
static void enable_all_fields(void)
{
int i;
for (i = 0; i< wu_num_fields; i++)
wu_fields[i].enable = 1;
}
static int wu_show_help(int);
static int wu_show_version(int);
static int wu_store_count(int unused)
{
char * s = wu_option_value(wu_option_count);
char * end;
if (s) {
wu_count = strtol(s,&end, 0);
if (*end) {
err("Bad count field");
return ret_err(EINVAL);
}
}
return 0;
}
static int wu_store_debug(int unused)
{
wu_debug = 1;
return 0;
}
static int wu_store_delim(int unused)
{
char * s = wu_option_value(wu_option_delim);
if (s)
wu_delim = s;
return 0;
}
static int wu_store_final(int unused)
{
wu_final = 1;
return 0;
}
static int wu_store_label(int unused)
{
wu_label = 1;
return 0;
}
static int wu_store_newline(int unused)
{
wu_newline = 1;
return 0;
}
static int wu_store_suppress(int unused)
{
wu_suppress = 1;
return 0;
}
static int wu_store_localtime(int unused)
{
wu_localtime = 1;
return 0;
}
static int wu_store_gmtime(int unused)
{
wu_gmtime = 1;
return 0;
}
static int wu_store_info_all(int unused)
{
wu_info_all = 1;
return 0;
}
static int wu_store_no_data(int unused)
{
wu_no_data = 1;
return 0;
}
static int wu_store_set_only(int unused)
{
wu_set_only = 1;
return 0;
}
/**
* wu_options - command line options and their associated flags
*
*/
static struct wu_options wu_options[] = {
/*
* Help!
*/
[wu_option_help] = {
.longopt = "help",
.shortopt = 'h',
.param = 0,
.descr = "Display help text and exit",
.show = wu_show_help,
},
[wu_option_version] = {
.longopt = "version",
.shortopt = 'V',
.param = 0,
.descr = "Display version information and exit",
.show = wu_show_version,
},
/*
* Modifies the output for all other options
*/
[wu_option_debug] = {
.longopt = "debug",
.shortopt = 'd',
.param = 0,
.descr = "Print out debugging messages",
.store = wu_store_debug,
},
/*
* For data reading..
*/
[wu_option_count] = {
.longopt = "count",
.shortopt = 'c',
.param = 1,
.descr = "Specify number of data samples",
.option = "<n>",
.store = wu_store_count,
},
[wu_option_final] = {
.longopt = "final",
.shortopt = 'z',
.param = 0,
.descr = "Print final interval information",
.store = wu_store_final,
},
/*
* Modifies output for each option (most relevant for data)
*/
[wu_option_delim] = {
.longopt = "delim",
.shortopt = 'f',
.param = 1,
.descr = "Set field delimiter (default \", \")",
.option = "<str>",
.store = wu_store_delim,
},
[wu_option_newline] = {
.longopt = "newline",
.shortopt = 'n',
.param = 0,
.descr = "Use '\\n' as delimter instead",
.store = wu_store_newline,
},
[wu_option_localtime] = {
.longopt = "localtime",
.shortopt = 't',
.param = 0,
.descr = "Print localtime with each data reading",
.store = wu_store_localtime,
},
[wu_option_gmtime] = {
.longopt = "gmtime",
.shortopt = 'g',
.param = 0,
.descr = "Print GMT time with each data reading",
.store = wu_store_gmtime,
},
[wu_option_label] = {
.longopt = "label",
.shortopt = 'l',
.param = 0,
.descr = "Show labels of each field",
.store = wu_store_label,
},
/*
* Relevant for each of the fields below
*/
[wu_option_suppress] = {
.longopt = "suppress",
.shortopt = 's',
.param = 0,
.descr = "Suppress printing of the field description",
.store = wu_store_suppress,
},
/*
* These options print values from the device and exit.
*/
[wu_option_cal] = {
.longopt = "calibrate",
.shortopt = 'b',
.param = 0,
.descr = "Print calibration parameters",
.show = wu_show_cal,
},
[wu_option_header] = {
.longopt = "header",
.shortopt = 'r',
.param = 0,
.descr = "Print data field names (as read from device)",
.show = wu_show_header,
},
/*
* These options have an optional parameter.
* W/o that parameter, they print values from the device.
* W/ that parameter, they set that option and read data.
*
* Except when the 'set-only' parameter is used, then the
* parameters are set, then re-read and printed.
*/
[wu_option_interval] = {
.longopt = "interval",
.shortopt = 'i',
.param = 2,
.descr = "Get/Set sampling interval",
.option = "<n>",
.show = wu_show_interval,
.store = wu_store_interval,
},
[wu_option_mode] = {
.longopt = "mode",
.shortopt = 'm',
.param = 2,
.descr = "Get/Set display mode",
.option = "<n>",
.show = wu_show_mode,
.store = wu_store_mode,
},
[wu_option_user] = {
.longopt = "user",
.shortopt = 'u',
.param = 2,
.descr = "Get/Set user parameters",
.option = "<str>",
.format = "<cost per kwh>,<2nd tier cost>,"
"<2nd tier threshold>,"
"<duty cycle threshold>",
.show = wu_show_user,
.store = wu_store_user,
},
[wu_option_info_all] = {
.longopt = "show-all",
.shortopt = 'a',
.param = 0,
.descr = "Show all device parameters",
.store = wu_store_info_all,
},
[wu_option_no_data] = {
.longopt = "no-data",
.shortopt = 'N',
.param = 0,
.descr = "Don't read any data (just read device info)",
.store = wu_store_no_data,
},
[wu_option_set_only] = {
.longopt = "set-only",
.shortopt = 'S',
.param = 0,
.descr = "Set parameters only (don't read them back)",
.store = wu_store_set_only,
},
};
#define wu_num_options ARRAY_SIZE(wu_options)
static int wu_show_version(int unused)
{
printf("%s Version %s\n", prog_name, wu_version);
return 0;
}
static int wu_show_help(int unused)
{
int i;
int n;
wu_show_version(unused);
printf(" A program for interfacing with the Watts Up? Power Meter\n");
printf("\n");
printf("Usage: %s [<options> ... ]<device> [<values> ... ]\n",
prog_name);
printf("\n");
printf("<device> is the serial port the device is connected at.\n");
printf("\n");
printf("<options> are any of the following:\n");
for (i = 0; i< wu_num_options; i++) {
n = printf(" -%c", wu_options[i].shortopt);
if (wu_options[i].param == 0)
n = printf(" ");
else if (wu_options[i].param == 1)
n = printf(" %s", wu_options[i].option);
else if (wu_options[i].param == 2)
n = printf(" [%s]", wu_options[i].option);
n += printf("%*c| ", n - 12, ' ');
n += printf("--%s", wu_options[i].longopt);
if (wu_options[i].param == 0)
n += printf(" ");
else if (wu_options[i].param == 1)
n += printf("=%s", wu_options[i].option);
else if (wu_options[i].param == 2)
n += printf("[=%s]", wu_options[i].option);
printf("%*c%s\n",
40 - n, ' ', wu_options[i].descr);
}
printf("\n");
printf("<value> specifies which of these to print out (default: ALL)\n");
for (i = 0; i< wu_num_fields; i++) {
printf("%12s -- %s\n", wu_fields[i].name, wu_fields[i].descr);
}
printf("\n");
return 0;
}
static char * wu_option_value(unsigned int index)
{
return (index< wu_num_options) ? wu_options[index].value : NULL;
}
static int wu_check_option_show(int index)
{
/*
* Return 1 if we need to print something out for
* a particular option.
*/
if (index< wu_num_options) {
if (wu_options[index].flag) {
return 1;
}
}
return 0;
}
static int wu_check_option_store(int index)
{
/*
* Return a 1 if this option is set.
*/
if (index< wu_num_options) {
if (wu_options[index].flag) {
return 1;
}
}
return 0;
}
static int wu_show(int index, int dev_fd)
{
if (wu_options[index].show)
return wu_options[index].show(dev_fd);
return 0;
}
/*
* Check if the option is set, and call its method if so.
* Return whether or not we did anything..
*/
static int wu_check_show(int index, int dev_fd)
{
if (wu_check_option_show(index)) {
wu_show(index, dev_fd);
return 1;
}
return 0;
}
/*
* Check if the option is set and if so, call it
* Return the value from the ->store() method.
*/
static int wu_check_store(int index, int dev_fd)
{
if (wu_check_option_store(index)) {
if (wu_options[index].store)
return wu_options[index].store(dev_fd);
}
return 0;
}
static void make_longopt(struct option * l)
{
int i;
for (i = 0; i< wu_num_options; i++) {
l[i].name = wu_options[i].longopt;
l[i].has_arg = wu_options[i].param;
l[i].flag =&wu_options[i].flag;
l[i].val = 0;
}
}
static void make_shortopt(char * str)
{
int i;
char * s = str;
for (i = 0; i< wu_num_options; i++) {
*s++ = wu_options[i].shortopt;
if (wu_options[i].param)
*s++ = wu_options[i].param == 1 ? ':' : ';';
}
}
static void enable_short_option(int c, char * arg)
{
int i;
/*
* Friggin' getopt_long() will return the
* character if we get a short option (e.g. '-h'),
* instead of returning 0 like it does when it
* gets a long option (e.g. "--help"). Ugh.
*/
for (i = 0; i< wu_num_options; i++) {
if (wu_options[i].shortopt == c) {
wu_options[i].flag = 1;
if (arg)
wu_options[i].value = strdup(arg);
break;
}
}
}
static int parse_args(int argc, char ** argv)
{
struct option longopts[wu_num_options + 1] = { };
char shortopts[wu_num_options * 2] = "";
make_longopt(longopts);
make_shortopt(shortopts);
while (1) {
int c;
int index;
c = getopt_long(argc, argv, shortopts,
longopts,&index);
if (c == -1)
break;
switch (c) {
case 0:
wu_options[index].flag = 1;
if (optarg)
wu_options[index].value = strdup(optarg);
printf("long option: val = %c, optarg = %s\n",
wu_options[index].shortopt, optarg);
break;
case '?':
err("Bad parameter");
return ret_err(EINVAL);
break;
default:
enable_short_option(c, optarg);
break;
}
}
/*
* Check for help request now and bail after
* printing it, if it's set.
*/
if (wu_check_show(wu_option_help, 0))
exit(0);
if (wu_check_show(wu_option_version, 0))
exit(0);
/*
* Fields to print out
*/
if (optind< argc) {
int i;
wu_device = argv[optind++];
if (optind< argc) {
for (i = optind; i< argc; i++)
enable_field(argv[i]);
} else
enable_all_fields();
} else {
wu_show(wu_option_help, 0);
return ret_err(EINVAL);
}
return 0;
}
int main(int argc, char ** argv)
{
int ret;
int fd = 0;
ret = parse_args(argc, argv);
if (ret)
return 0;
/*
* Try to enable debugging early
*/
if ((ret = wu_check_store(wu_option_debug, 0)))
goto Close;
ret = open_device(wu_device,&fd);
if (ret)
return ret;
dbg("%s: Open for business", wu_device);
ret = setup_serial_device(fd);
if (ret)
goto Close;
wu_clear(fd);
wu_fd = fd;
/*
* Set delimeter before we print out any fields.
*/
if ((ret = wu_check_store(wu_option_delim, fd)))
goto Close;
/*
* Ditto for 'label' and 'newline' flags.
*/
if ((ret = wu_check_store(wu_option_label, fd)))
goto Close;
if ((ret = wu_check_store(wu_option_newline, fd)))
goto Close;
if ((ret = wu_check_store(wu_option_suppress, fd)))
goto Close;
if ((ret = wu_check_store(wu_option_localtime, fd)))
goto Close;
if ((ret = wu_check_store(wu_option_gmtime, fd)))
goto Close;
if ((ret = wu_check_store(wu_option_set_only, fd)))
goto Close;
if ((ret = wu_check_store(wu_option_no_data, fd)))
goto Close;
if ((ret = wu_check_store(wu_option_info_all, fd)))
goto Close;
/*
* Options to set device parameters.
*/
if ((ret = wu_check_store(wu_option_interval, fd)))
goto Close;
if ((ret = wu_check_store(wu_option_mode, fd)))
goto Close;
if ((ret = wu_check_store(wu_option_user, fd)))
goto Close;
/*
* Check for options to print device info
*/
if (wu_info_all) {
wu_show(wu_option_cal, fd);
wu_show(wu_option_header, fd);
wu_show(wu_option_interval, fd);
wu_show(wu_option_mode, fd);
wu_show(wu_option_user, fd);
} else {
wu_check_show(wu_option_cal, fd);
wu_check_show(wu_option_header, fd);
if (!wu_set_only) {
wu_check_show(wu_option_interval, fd);
wu_check_show(wu_option_mode, fd);
wu_check_show(wu_option_user, fd);
}
}
if (!wu_no_data) {
if ((ret = wu_check_store(wu_option_count, fd)))
goto Close;
if ((ret = wu_check_store(wu_option_final, fd)))
goto Close;
if ((ret = wu_start_log()))
goto Close;
wu_read_data(fd);
wu_stop_log();
}
Close:
close(fd);
return ret;
}
|