-
Notifications
You must be signed in to change notification settings - Fork 4
/
br.c
673 lines (557 loc) · 18.1 KB
/
br.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
/*
*
* br (BottleRocket)
*
* Control software for the X10 FireCracker wireless computer
* interface kit.
*
* (c) 1999 Ashley Clark ([email protected]) and Tymm Twillman ([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.
*
*/
#define VERSION "0.05b3"
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <unistd.h>
#include <sys/ioctl.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/time.h>
#include <stdlib.h>
#include <ctype.h>
#include <limits.h>
#include <string.h>
#ifdef HAVE_ERRNO_H
#include <errno.h>
#endif
#ifdef HAVE_FEATURES_H
#include <features.h>
#endif
#ifdef HAVE_GETOPT_LONG
#include <getopt.h>
#endif
#include "br.h"
#include "br_cmd.h"
#include "br_cmd_engine.h"
int Verbose = 0;
char *MyName;
void (*saved_br_error_handler)(char *, char *);
void usage()
{
fprintf(stderr, "BottleRocket version %s\n", VERSION);
fprintf(stderr, "\n");
fprintf(stderr, "Usage: %s [<options>][<housecode>(<list>) "
"<native command> ...]\n\n", MyName);
fprintf(stderr, " Options:\n");
#ifdef HAVE_GETOPT_LONG
fprintf(stderr, " -v, --verbose\t\t\tadd v's to increase verbosity\n");
fprintf(stderr, " -x, --port=PORT\t\tset port to use\n");
fprintf(stderr, " -c, --house=[A-P]\t\tuse alternate house code "
"(default \"A\")\n");
fprintf(stderr, " -n, --on=LIST\t\t\tturn on devices in LIST\n");
fprintf(stderr, " -f, --off=LIST\t\tturn off devices in LIST\n");
fprintf(stderr, " -N, --ON\t\t\tturn on all devices in housecode\n");
fprintf(stderr, " -F, --OFF\t\t\tturn off all devices in housecode\n");
fprintf(stderr, " -d, --dim=LEVEL[,LIST]\tdim devices in housecode to "
" relative LEVEL\n");
fprintf(stderr, " -B, --lamps_on\t\tturn all lamps in housecode on\n");
fprintf(stderr, " -D, --lamps_off\t\tturn all lamps in housecode off\n");
fprintf(stderr, " -r, --repeat=NUM\t\trepeat commands NUM times "
"(0 = ~ forever)\n");
fprintf(stderr, " -h, --help\t\t\tthis help\n\n");
#else
fprintf(stderr, " -v\tverbose (add v's to increase verbosity)\n");
fprintf(stderr, " -x\tset port to use\n");
fprintf(stderr, " -c\tuse alternate house code (default \"A\")\n");
fprintf(stderr, " -n\tturn on devices\n");
fprintf(stderr, " -f\tturn off devices\n");
fprintf(stderr, " -N\tturn all devices in housecode on\n");
fprintf(stderr, " -F\tturn all devices in housecode off\n");
fprintf(stderr, " -d\tdim devices in housecode to relative dimlevel\n");
fprintf(stderr, " -B\tturn all lamps in housecode on\n");
fprintf(stderr, " -D\tturn all lamps in housecode off\n");
fprintf(stderr, " -r\trepeat commands <repeats> times (0 basically "
"means don't stop)\n");
fprintf(stderr, " -h\tthis help\n\n");
#endif
fprintf(stderr, "<list>\t\tis a comma separated list of devices "
"(no spaces),\n");
fprintf(stderr, "\t\teach ranging from 1 to 16\n");
fprintf(stderr, "<dimlevel>\tis an integer from %d to %d "
"(0 means no change)\n", -DIMRANGE, DIMRANGE);
fprintf(stderr, "<housecode>\tis a letter between A and P\n");
fprintf(stderr, "<native cmd>\tis one of ON, OFF, DIM, BRIGHT, "
"ALL_ON, ALL_OFF,\n");
fprintf(stderr, "\t\tLAMPS_ON or LAMPS_OFF\n\n");
fprintf(stderr, "For native commands, <list> should only be specified "
"for ON or OFF.\n\n");
}
void my_br_error_handler(char *where, char *problem)
{
int tmperrno = errno;
char *tmpwhere;
/*
* Don't print out possibly confusing error info
* unless in verbose mode
*/
tmpwhere = (Verbose ? where:NULL);
fprintf(stderr, "%s: ", MyName);
errno = tmperrno;
(*saved_br_error_handler)(tmpwhere, problem);
}
int checkimmutableport(char *port_source)
{
/*
* Check to see if the user is allowed to specify an alternate serial port
*/
if (!ISSETID())
return 0;
errno = EPERM;
br_error("checkimmutableport", "You are not authorized to change the X10 port!");
fprintf(stderr, "\tPort specified %s\n", port_source);
return -1;
}
int add_dimcmd(br_control_info *cinfo, br_unit_list *units, int dim_level)
{
/*
* Turn info into stuff usable by the command engine and add it to the
* list of things to do.
*/
register int i;
int index = 0;
int dev;
int house;
int cmd;
cmd = ((dim_level < 0) ? DIM:BRIGHT);
dim_level = (dim_level < 0) ? -dim_level:dim_level;
if (br_get_num_units(units)) {
while ((dev = br_get_ul_device(units, index)) != -1) {
house = br_get_ul_house(units, index);
if (br_add_cmd(cinfo, ON, house, dev) < 0)
return -1;
for (i = 0; i < dim_level; i++) {
if (br_add_cmd(cinfo, cmd, house, 0) < 0)
return -1;
}
index++;
}
} else {
for (i = 0; i < dim_level; i++) {
if (br_add_cmd(cinfo, cmd, br_default_house, 0) < 0)
return -1;
}
}
return 0;
}
int gethouse(char *house)
{
/*
* Grab a house code from the command line
*/
char *end;
int c;
c = br_strtohc(house, &end);
if ((c < 0) || (*end != '\0')) {
errno = EINVAL;
br_error("gethouse", "House code must be in range [A-P]");
return -1;
}
return c;
}
int getunits(char *list, br_unit_list **units)
{
/*
* Get a list of devices for an operation to be performed on from
* the command line
*/
char *end;
if (*units == NULL) {
if ((*units = br_new_unit_list()) == NULL)
return -1;
}
if ((br_strtoul(list, *units, &end) < 0)
|| (*end != '\0'))
{
br_free_unit_list(*units);
errno = EINVAL;
br_error("getunits", "Devices must be in the range of [1-16], housecodes [A-P]");
*units = NULL;
return -1;
}
return 0;
}
int getdim(char *list, br_unit_list **units, int *dim)
{
/*
* Get devices that should be dimmed from the command line, and how
* much to dim them
*/
char *end;
*dim = strtol(list, &end, 0);
while (isspace(*end))
end++;
if (*end == ',')
end++;
if (*end) {
if (getunits(end, units) < 0)
return -1; /* error already printed */
} else {
if (*units)
br_free_unit_list(*units);
*units = NULL;
}
/*
* May have more dimlevels when I get a chance to play with variable
* dimming
*/
if ((*dim < -DIMRANGE) || (*dim > DIMRANGE))
{
if (*units)
br_free_unit_list(*units);
*units = NULL;
errno = EINVAL;
br_error("getdim", "Invalid dim level");
return -1;
}
return 0;
}
int open_port(br_control_info *cinfo, char *port)
{
/*
* Open the serial port that a FireCracker device is (we expect) on
*/
int fd;
if (Verbose >= 2)
printf("%s: Opening serial port %s.\n", MyName, port);
/*
* Oh, yeah. Don't need O_RDWR for ioctls. This is safer.
*/
if ((fd = open(port, O_RDONLY | O_NONBLOCK)) < 0) {
br_error("open_port", "Unable to open serial port");
return -1;
}
/*
* If we end up with a reserved fd, don't mess with it. Just to make sure.
*/
if (!SAFE_FILENO(fd)) {
close(fd);
errno = EBADF;
return -1;
}
return fd;
}
int close_port(int fd)
{
/*
* Close the serial port when we're done using it
*/
if (Verbose >= 2)
printf("%s: Closing serial port.\n", MyName);
close(fd);
return 0;
}
int native_getunits(char *arg, br_unit_list **units)
{
/*
* Get units to be accessed from the command line in native BottleRocket style
*/
if (strlen(arg) < 2) {
errno = EINVAL;
br_error("native_getunits", "No units specified");
return -1;
}
if (getunits(arg, units) < 0)
return -1; /* error already printed */
return 0;
}
int native_getcmd(char *arg)
{
/*
* Convert a native BottleRocket command to the appropriate token
*/
if (!strcasecmp(arg, "ON"))
return ON;
if (!strcasecmp(arg, "OFF"))
return OFF;
if (!strcasecmp(arg, "DIM"))
return DIM;
if (!strcasecmp(arg, "BRIGHT"))
return BRIGHT;
if (!strcasecmp(arg, "ALL_ON"))
return ALL_ON;
if (!strcasecmp(arg, "ALL_OFF"))
return ALL_OFF;
if (!strcasecmp(arg, "LAMPS_ON"))
return ALL_LAMPS_ON;
if (!strcasecmp(arg, "LAMPS_OFF"))
return ALL_LAMPS_OFF;
errno = EINVAL;
br_error("native_getcmd", "Native br commands are ON, OFF, DIM,\n\t"
"BRIGHT, ALL_ON, ALL_OFF, LAMPS_ON or LAMPS_OFF");
errno = EINVAL;
return -1;
}
int native_cmdline(br_control_info *cinfo, int argc, char *argv[], int optind)
{
/*
* Get arguments from the command line in native BottleRocket style
*/
int cmd;
int i;
int house;
br_unit_list *units = NULL;
if (argc - optind < 2) {
usage();
errno = EINVAL;
return -1;
}
/*
* Two arguments at a time; device and command
*/
for (i = optind; i < argc - 1; i += 2) {
cmd = native_getcmd(argv[i + 1]);
switch(cmd) {
case ON:
if (native_getunits(argv[i], &units) < 0)
return -1;
if (br_add_ul_cmd(cinfo, ON, units) < 0)
return -1;
break;
case OFF:
if (native_getunits(argv[i], &units) < 0)
return -1;
if (br_add_ul_cmd(cinfo, OFF, units) < 0)
return -1;
break;
case DIM:
if ((house = gethouse(argv[i])) < 0)
return -1;
if (br_add_cmd(cinfo, DIM, house, 0) < 0)
return -1;
break;
case BRIGHT:
if ((house = gethouse(argv[i])) < 0)
return -1;
if (br_add_cmd(cinfo, BRIGHT, house, 0) < 0)
return -1;
break;
case ALL_ON:
if ((house = gethouse(argv[i])) < 0)
return -1;
if (br_add_cmd(cinfo, ALL_ON, house, 0) < 0)
return -1;
break;
case ALL_OFF:
if ((house = gethouse(argv[i])) < 0)
return -1;
if (br_add_cmd(cinfo, ALL_OFF, house, 0) < 0)
return -1;
break;
case ALL_LAMPS_ON:
if ((house = gethouse(argv[i])) < 0)
return -1;
if (br_add_cmd(cinfo, ALL_LAMPS_ON, house, 0) < 0)
return -1;
break;
case ALL_LAMPS_OFF:
if ((house = gethouse(argv[i])) < 0)
return -1;
if (br_add_cmd(cinfo, ALL_LAMPS_OFF, house, 0) < 0)
return -1;
break;
default:
errno = EINVAL;
return -1;
}
}
if (i != argc) {
usage();
errno = EINVAL;
return -1;
}
if (units)
br_free_unit_list(units);
return 0;
}
int main(int argc, char **argv)
{
char *port_source = "at compile time";
char *tmp_port;
char *port = X10_PORTNAME;
int opt;
int house = 0;
int repeat;
int dimlevel = 0;
int fd;
br_control_info *cinfo = NULL;
br_unit_list *units = NULL;
#ifdef HAVE_GETOPT_LONG
int opt_index;
static struct option long_options[] = {
{"help", no_argument, 0, 'h'},
{"port", required_argument, 0, 'x'},
{"repeat", required_argument, 0, 'r'},
{"on", required_argument, 0, 'n'},
{"off", required_argument, 0, 'f'},
{"ON", no_argument, 0, 'N'},
{"OFF", no_argument, 0, 'F'},
{"dim", required_argument, 0, 'd'},
{"lamps_on", no_argument, 0, 'B'},
{"lamps_off", no_argument, 0, 'D'},
{"inverse", no_argument, 0, 'i'},
{"house", required_argument, 0, 'c'},
{"verbose", no_argument, 0, 'v'},
{"pause", no_argument, 0, 'p'},
{0, 0, 0, 0}
};
#endif
#define OPT_STRING "x:hvr:ic:n:Nf:Fd:BDp"
/*
* Jimmy in the local error handler that hides the
* function where the error occurred (to keep from
* being confusing) and also prints the program name
*/
saved_br_error_handler = br_error_handler;
br_error_handler = my_br_error_handler;
MyName = argv[0];
/*
* This is where we store all of the info to be passed to the
* command engine.
*/
if ((cinfo = br_new_control_info()) == NULL)
exit(errno);
if ((tmp_port = getenv("X10_PORTNAME"))) {
port_source = "in the environment variable X10_PORTNAME";
if (checkimmutableport(port_source)) {
exit(errno);
} else {
port = tmp_port;
}
}
#ifdef HAVE_GETOPT_LONG
while ((opt = getopt_long(argc, argv, OPT_STRING, long_options,
&opt_index)) != -1)
{
#else
while ((opt = getopt(argc, argv, OPT_STRING)) != -1) {
#endif
switch (opt) {
case 'x': /* Set the port */
port_source = "on the command line";
if (checkimmutableport(port_source) < 0)
exit(errno);
port = optarg;
break;
case 'r': /* Repeat */
repeat = atoi(optarg);
if (!repeat && !isdigit(*optarg)) {
errno = EINVAL;
br_error(NULL, "Invalid repeat value");
exit(errno);
}
cinfo->repeat = (repeat ? repeat:INT_MAX);
break;
case 'v': /* Verbose */
if (Verbose++ == 10)
fprintf(stderr, "\nGet a LIFE already. "
"I've got enough v's, thanks.\n\n");
if (Verbose >= 4)
br_verbose = Verbose - 3;
break;
case 'i':
cinfo->inverse++; /* no this isn't documented.
* your free gift for reading the source.
*/
break;
case 'c': /* Set housecode */
if ((br_default_house = gethouse(optarg)) < 0)
exit(errno);
house = br_default_house;
break;
case 'n': /* Device(s) on */
if (getunits(optarg, &units) < 0)
exit(errno);
if (br_add_ul_cmd(cinfo, ON, units) < 0)
exit(errno);
break;
case 'N': /* All on */
if (br_add_cmd(cinfo, ALL_ON, house, 0) < 0)
exit(errno);
break;
case 'f': /* Device(s) off */
if (getunits(optarg, &units) < 0)
exit(errno);
if (br_add_ul_cmd(cinfo, OFF, units) < 0)
exit(errno);
break;
case 'F': /* All off */
if (br_add_cmd(cinfo, ALL_OFF, house, 0) < 0)
exit(errno);
break;
case 'd': /* Dim (/bright) */
if (getdim(optarg, &units, &dimlevel) < 0)
exit(errno);
if (add_dimcmd(cinfo, units, dimlevel) < 0)
exit(errno);
break;
case 'B': /* All lamps on */
if (br_add_cmd(cinfo, ALL_LAMPS_ON, house, 0) < 0)
exit(errno);
break;
case 'D': /* All lamps off */
if (br_add_cmd(cinfo, ALL_LAMPS_OFF, house, 0) < 0)
exit(errno);
break;
case 'p':
if (br_add_cmd(cinfo, PAUSE, 0, 0) < 0)
exit(errno);
break;
case 'h': /* Help */
usage();
exit(0);
default: /* Someone messed up. */
usage();
exit(EINVAL);
}
}
if (argc > optind) {
/*
* Must be using the native BottleRocket command line...
*/
if (native_cmdline(cinfo, argc, argv, optind) < 0)
exit(errno);
}
if (!br_get_num_commands(cinfo)) {
usage();
exit(EINVAL);
}
if ((fd = open_port(cinfo, port)) < 0)
exit(errno);
if (Verbose >= 2)
printf("%s: Executing %d commands\n", MyName,
br_get_num_commands(cinfo));
if (br_execute(fd, cinfo) < 0)
exit(errno);
if (close_port(fd) < 0)
exit(errno);
if (Verbose >= 3)
printf("%s: Cleaning up...\n", MyName);
br_free_unit_list(units);
br_free_control_info(cinfo);
return 0;
}