-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathpoll.c
1178 lines (1090 loc) · 56.3 KB
/
poll.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
/*----------------------------------------------------------------------------+
| |
| Enhanced HEYU Functionality |
| |
| Enhancements copyright 2004-2008 Charles W. Sullivan |
| |
| Changes for use with CM11A copyright 1996, 1997 Daniel B. Suthers, |
| Pleasanton Ca, 94588 USA |
| |
| Copyright 1986 by Larry Campbell, 73 Concord Street, Maynard MA 01754 USA |
| (maynard!campbell). |
| |
| John Chmielewski (tesla!jlc until 9/1/86, then rogue!jlc) assisted |
| by doing the System V port and adding some nice features. Thanks! |
| |
| |
| As used herein, HEYU is a trademark of Daniel B. Suthers. |
| X10, CM11A, and ActiveHome are trademarks of X-10 (USA) Inc. |
| The author is not affiliated with either entity. |
| |
| Charles W. Sullivan |
| Co-author and Maintainer |
| Greensboro, North Carolina |
| Email ID: cwsulliv01 |
| Email domain: -at- heyu -dot- org |
| |
+----------------------------------------------------------------------------*/
/*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <ctype.h>
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#ifdef HAVE_STRINGS_H
#include <strings.h>
#endif
#include <time.h>
#include <signal.h>
#include "x10.h"
#ifdef HAVE_SYSLOG_H
#include <syslog.h>
#endif
#include "process.h"
#include "x10state.h"
#include "oregon.h"
/* msf - added for glibc/rh 5.0 */
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
extern int sptty;
extern CONFIG config;
extern CONFIG *configp;
extern struct x10global_st x10global;
extern x10hcode_t *x10state;
extern int i_am_monitor, i_am_state, i_am_rfxmeter;
extern int verbose;
extern int xread(int, unsigned char *, int, int);
extern int is_ring ( void );
extern int reread_config ( void );
unsigned char bootflag = R_NOTATSTART;
/*----------------------------------------------------------------------------+
| Determine the maximum number of bytes for a received function available in |
| the buffer. Arguments lenbyte and fmapbyte are the first two bytes from |
| the buffer, representing the length of the buffer and a bitmapped function |
| indicator. The return value is determined by the lesser the length of the |
| buffer or the distance to the next indicated function from the function |
| indicator byte. |
+----------------------------------------------------------------------------*/
int max_funclen ( unsigned char lenbyte, unsigned char fmapbyte, int pos )
{
int k, maxlen;
if ( !(fmapbyte & (1 << pos)) || lenbyte > 9 )
return 0;
maxlen = 1;
for ( k = pos + 1; k < (int)(lenbyte - 1); k++ ) {
if ( !(fmapbyte & (1 << k)) )
maxlen++;
else
break;
}
return maxlen;
}
/*
* The CM11A sends data back to the computer whenever it sees a command
* come in over the AC buss. This should (theoretically) allow the compuer
* to track the status of all modules. Upon startup, this program should
* check for a poll before anything else.
*
* Check for a poll (0x5a) from the CM11A, If we get one within a
* second, we should send 0xc3 to tell it that we are ready to read
* it's output.
*
* If the showdata flag is set, we print. Otherwise we just eat the output.
*/
extern char *datstrf(void);
unsigned int signal_source;
extern FILE *fdsout;
extern FILE *fdserr;
extern FILE *fprfxo;
extern FILE *fprfxe;
int check4poll( int showdata, int timeout )
{
static int lastunit;
static char lasthc = '_';
int temperat, fdata;
int n, i, j;
int to_read;
char hc;
unsigned char predim, xcmd, xtype, xdata, subunit;
int unit;
int macro_report;
char *func;
int funcbits;
static int wasflag = 0;
unsigned char buf[128];
char outbuf[256];
unsigned char snochange;
extern char *b2s();
off_t f_offset;
unsigned int mac_addr, bitmap;
extern void acknowledge_hail();
extern int special_func;
extern int display_expanded_macro();
int ichksum;
int identify_sent(unsigned char *, int, unsigned char *);
char *translate_other(unsigned char *, int, unsigned char *);
extern char *translate_sent(unsigned char *, int, int *);
extern char *translate_rf_sent(unsigned char *, int *);
#if 0
extern int find_powerfail_launcher( unsigned char );
extern int find_rfflood_launcher( void );
extern int find_lockup_launcher( void );
#endif
extern int find_powerfail_scripts ( unsigned char );
extern int find_rfflood_scripts ( void );
extern int find_lockup_scripts ( void );
extern int set_globsec_flags( unsigned char );
extern char *display_armed_status ( void );
extern int clear_tamper_flags ( void );
extern int engine_local_setup ( int );
extern char *translate_rfxtype_message ( unsigned char * );
extern char *translate_rfxsensor_ident ( unsigned char * );
extern char *display_variable_aux_data ( unsigned char * );
extern int set_counter ( int, unsigned short, unsigned char );
extern char *translate_counter_action ( unsigned char * );
extern char *translate_kaku ( unsigned char *, unsigned char *, int * );
extern char *translate_visonic ( unsigned char *, unsigned char *, int * );
extern char *display_binbuffer ( unsigned char * );
int launch_script_cmd ( unsigned char * );
int launchp = -1;
char maclabel[MACRO_LEN + 1];
int macfound;
unsigned int squelch;
unsigned char hcode;
static int chksum_alert = -1;
extern char *funclabel[18];
static unsigned char newbuf[8], hexaddr = 0;
static char *send_prefix = "sndc";
static unsigned char waitflag = 0;
int sent_type;
unsigned char tag;
int maxlen;
unsigned char level;
static unsigned char defer_dim;
char minibuf[32];
char *transp;
mode_t oldumask;
char *sp;
static char *rcs_status[] = {
"System_mode = OFF", "System_mode = HEAT", "System_mode = COOL", "System_mode = AUTO",
"Fan = ON", "Fan = OFF", "Setback = ON", "Setback = OFF", "Temp Change", "Setpoint Change",
"Outside Temp", "Heat Setpoint", "Cooling Setpoint",
};
static unsigned int source_type[] = {SNDC, SNDS, SNDP, SNDA, RCVA};
static char *source_name[] = {"sndc", "snds", "sndp", "snda", "rcva"};
if ( i_am_state ) {
/* Redirect output to the Heyu log file */
oldumask = umask((mode_t)configp->log_umask);
fdsout = freopen(configp->logfile, "a", stdout);
fdserr = freopen(configp->logfile, "a", stderr);
umask(oldumask);
}
/* Note: When invoked as the normal 'heyu monitor' (i_am_monitor), */
/* fdsout and fdserr will have been set to stdout and stderr. */
/* But when invoked as 'heyu monitor rfxmeter' (i_am_rfxmeter), */
/* they will have been redirected to /dev/null so the only output */
/* to that xterm will be RFXMeter data via file pointer fprfxo. */
unit = -1;
hc = '\0';
func = "" ;
if ( timeout == 0 ) { /* only do a read if there is data */
if( sptty < 0 ) {
return 0;
}
f_offset = lseek(sptty, 0, SEEK_CUR);
if ( f_offset == lseek(sptty, 0, SEEK_END) ) {
return 0;
}
lseek(sptty, f_offset, SEEK_SET);
timeout = 1;
}
n = xread(sptty, buf, 1, timeout);
if ( n != 0 ) {
/* Three 0xff bytes in a row indicate that I sent a transmission to
* the CM11, I.E a command. The byte following the 3 0xFF bytes
* indicates how long the sent command was. We need to suck these
* off the stack if we are monitoring.
*/
while ( buf[0] == 0xff ) { /* CM11A may be responding to a transmission */
if ( ++wasflag == 3 ) {
wasflag = 0;
n = xread(sptty, buf, 1, timeout); /* length of xmit */
if( n != 1 ) {
return(0);
}
if ( buf[0] == 0xff && chksum_alert == 0xff ) {
/*
* We have received four 0xff bytes in a row,
* looks like the first one was the checksum expected,
* so drop one of them and loop one more time.
*/
chksum_alert = -1;
wasflag = 2;
continue;
}
n = buf[0];
if ( n < 127 ) {
unsigned char chksum;
n = xread(sptty, buf, n, timeout);
/* Identify the type of sent command from the leading */
/* byte and the length of the buffer. */
sent_type = identify_sent(buf, n, &chksum);
if ( sent_type == SENT_STCMD ) {
/* A command for the monitor/state process */
if ( buf[1] == ST_SOURCE ) {
signal_source = source_type[buf[2]];
send_prefix = source_name[buf[2]];
}
else if ( buf[1] == ST_TESTPOINT && i_am_monitor ) {
fprintf(fdsout, "%s Testpoint %d\n", datstrf(), buf[2]);
}
else if ( buf[1] == ST_LAUNCH && i_am_state ) {
configp->script_ctrl = buf[2];
fprintf(fdsout, "%s Scripts %s\n", datstrf(), (buf[2] == ENABLE ? "enabled" : "disabled"));
}
else if ( buf[1] == ST_INIT_ALL && i_am_state ) {
x10state_init_all();
write_x10state_file();
}
else if ( buf[1] == ST_INIT && i_am_state ) {
x10state_init_old(buf[2]);
write_x10state_file();
}
else if ( buf[1] == ST_INIT_OTHERS && i_am_state ) {
x10state_init_others();
write_x10state_file();
}
else if ( buf[1] == ST_WRITE && i_am_state ) {
write_x10state_file();
}
else if ( buf[1] == ST_EXIT && i_am_state ) {
write_x10state_file();
unlock_state_engine();
exit(0);
}
else if ( buf[1] == ST_RESETRF && (i_am_state || i_am_monitor) ) {
fprintf(fdsout, "%s Reset CM17A\n", datstrf());
}
else if ( buf[1] == ST_BUSYWAIT && (i_am_state || i_am_monitor) ) {
waitflag = buf[2];
if ( configp->auto_wait == 0 ) {
if ( waitflag > 0 )
fprintf(fdsout, "%s Wait macro completion.\n", datstrf());
else
fprintf(fdsout, "%s Wait timeout.\n", datstrf());
}
}
else if ( buf[1] == ST_CHKSUM ) {
chksum_alert = buf[2];
}
else if ( buf[1] == ST_REWIND ) {
for ( j = 0; j < 10; j++ ) {
if ( lseek(sptty, (off_t)0, SEEK_END) <= (off_t)(SPOOLFILE_ABSMIN/2) )
break;
sleep(1);
}
fprintf(fdsout, "%s Spoolfile exceeded max size and was rewound.\n", datstrf());
}
else if ( buf[1] == ST_SHOWBYTE && (i_am_state || i_am_monitor) ) {
fprintf(fdsout, "%s Byte value = 0x%02x\n", datstrf(), buf[2]);
}
else if ( buf[1] == ST_RESTART && (i_am_state || i_am_monitor || i_am_rfxmeter) ) {
reread_config();
if ( i_am_state ) {
syslog(LOG_ERR, "engine reconfiguration-\n");
engine_local_setup(E_RESTART);
fprintf(fdsout, "%s Engine reconfiguration\n", datstrf());
fflush(fdsout);
// syslog(LOG_ERR, "engine reconfiguration-\n");
}
else if ( i_am_rfxmeter ) {
fprintf(fprfxo, "RFXMeter monitor reconfiguration\n");
fflush(fprfxo);
}
else {
fprintf(fdsout, "%s Monitor reconfiguration\n", datstrf());
fflush(fdsout);
}
}
else if ( buf[1] == ST_SECFLAGS && (i_am_state || i_am_monitor)) {
if ( buf[2] != 0 ) {
warn_zone_faults(fdsout, datstrf());
}
set_globsec_flags(buf[2]);
write_x10state_file();
fprintf(fdsout, "%s %s\n", datstrf(), display_armed_status());
fflush(fdsout);
}
else if ( buf[1] == ST_CLRTIMERS && (i_am_state || i_am_monitor)) {
reset_user_timers();
write_x10state_file();
fprintf(fdsout, "%s Reset timers 1-16\n", datstrf());
fflush(fdsout);
}
else if ( buf[1] == ST_CLRTAMPER && (i_am_state || i_am_monitor)) {
clear_tamper_flags();
write_x10state_file();
fprintf(fdsout, "%s Clear tamper flags\n", datstrf());
fflush(fdsout);
}
}
else if ( sent_type == SENT_WRMI ) {
/* WRMI - Ignore */
}
else if ( sent_type == SENT_ADDR ) {
/* An address */
if ( *(transp = translate_sent(buf, n, &launchp)) )
fprintf(fdsout, "%s %s %s\n", datstrf(), send_prefix, transp);
if (signal_source != RCVA)
chksum_alert = chksum;
if ( launchp >= 0 && i_am_state) {
launch_scripts(&launchp);
}
}
else if ( sent_type == SENT_FUNC ) {
/* Standard function */
if ( *(transp = translate_sent(buf, n, &launchp)) )
fprintf(fdsout, "%s %s %s\n", datstrf(), send_prefix, transp);
if (signal_source != RCVA)
chksum_alert = chksum;
if ( launchp >= 0 && i_am_state) {
launch_scripts(&launchp);
}
}
else if ( sent_type == SENT_EXTFUNC ) {
/* Extended function */
if ( *(transp = translate_sent(buf, n, &launchp)) )
fprintf(fdsout,"%s %s %s\n", datstrf(), send_prefix, transp);
chksum_alert = chksum;
if ( launchp >= 0 && i_am_state) {
launch_scripts(&launchp);
}
}
else if ( sent_type == SENT_RF ) {
/* CM17A command */
fprintf(fdsout,"%s %s %s\n", datstrf(), "xmtf", translate_rf_sent(buf, &launchp));
if ( launchp >= 0 && i_am_state ) {
launch_scripts(&launchp);
}
}
else if ( sent_type == SENT_FLAGS && i_am_state ) {
update_flags(buf);
write_x10state_file();
}
else if ( sent_type == SENT_FLAGS32 && i_am_state ) {
update_flags_32(buf);
write_x10state_file();
}
else if ( sent_type == SENT_VDATA ) {
strcpy(outbuf, translate_virtual(buf, 9 /*8*/, &snochange, &launchp));
if ( *outbuf && !(snochange && config.hide_unchanged == YES && launchp < 0) )
fprintf(fdsout,"%s %s %s\n", datstrf(), send_prefix, outbuf);
if ( i_am_state && launchp >= 0 ) {
launch_scripts(&launchp);
}
}
else if ( sent_type == SENT_GENLONG ) {
strcpy(outbuf, translate_gen_longdata(buf, &snochange, &launchp));
if ( *outbuf && !(snochange && config.hide_unchanged == YES && launchp < 0) )
fprintf(fdsout,"%s %s %s\n", datstrf(), send_prefix, outbuf);
if ( i_am_state && launchp >= 0 ) {
launch_scripts(&launchp);
}
}
else if ( sent_type == SENT_ORE_EMU ) {
strcpy(outbuf, translate_ore_emu(buf, &snochange, &launchp));
if ( *outbuf && !(snochange && config.hide_unchanged == YES && launchp < 0) )
fprintf(fdsout,"%s %s %s\n", datstrf(), send_prefix, outbuf);
if ( i_am_state && launchp >= 0 ) {
launch_scripts(&launchp);
}
}
else if ( sent_type == SENT_KAKU ) {
strcpy(outbuf, translate_kaku(buf, &snochange, &launchp));
if ( *outbuf && !(snochange && config.hide_unchanged == YES && launchp < 0) )
fprintf(fdsout,"%s", outbuf);
if ( i_am_state && launchp >= 0 ) {
launch_scripts(&launchp);
}
}
else if ( sent_type == SENT_VISONIC ) {
strcpy(outbuf, translate_visonic(buf, &snochange, &launchp));
if ( *outbuf && !(snochange && config.hide_unchanged == YES && launchp < 0) )
fprintf(fdsout,"%s", outbuf);
if ( i_am_state && launchp >= 0 ) {
launch_scripts(&launchp);
}
}
else if ( sent_type == SENT_LONGVDATA ) {
if ( i_am_rfxmeter ) {
/* This and only this data gets sent to the special */
/* 'heyu monitor rfxmeter' window */
strcpy(outbuf, translate_long_virtual(buf, &snochange, &launchp) );
if ( (sp = strchr(outbuf, '\n')) != NULL )
*sp = '\0';
fprintf(fprfxo, "%s\n", outbuf);
fflush(fprfxo);
}
else {
/* This and all other data are not sent to the rfxmeter window */
strcpy(outbuf, translate_long_virtual(buf, &snochange, &launchp) );
if ( *outbuf && !(snochange && config.hide_unchanged == YES && launchp < 0) ) {
if ( (sp = strchr(outbuf, '\n')) != NULL ) {
*sp = '\0';
fprintf(fdsout,"%s %s %s\n", datstrf(), send_prefix, outbuf);
fprintf(fdsout,"%s %s\n", datstrf(), sp + 1);
}
else {
fprintf(fdsout,"%s %s %s\n", datstrf(), send_prefix, outbuf);
}
}
if ( i_am_state && launchp >= 0 ) {
launch_scripts(&launchp);
}
}
}
else if ( sent_type == SENT_COUNTER && (i_am_state || i_am_monitor)) {
set_counter(buf[2] | (buf[3] << 8), buf[4] | (buf[5] << 8), buf[6]);
write_x10state_file();
fprintf(fdsout, "%s %s\n", datstrf(), translate_counter_action(buf));
fflush(fdsout);
}
else if ( sent_type == SENT_CLRSTATUS && i_am_state ) {
clear_statusflags(buf[2], (buf[3] << 8 | buf[4]));
write_x10state_file();
}
else if ( sent_type == SENT_MESSAGE && (i_am_monitor || i_am_state) ) {
fprintf(fdsout, "%s %s\n", datstrf(), buf + 3);
}
else if ( sent_type == SENT_PFAIL && (i_am_state || i_am_monitor) ) {
bootflag = buf[2];
if ( bootflag & R_ATSTART )
fprintf(fdsout, "%s Powerfail signal received at startup.\n", datstrf());
else
fprintf(fdsout, "%s Powerfail signal received.\n", datstrf());
if ( i_am_state && (launchp = find_powerfail_scripts(bootflag)) >= 0 )
launch_scripts(&launchp);
}
else if ( sent_type == SENT_RFFLOOD && (i_am_state || i_am_monitor) ) {
fprintf(fdsout, "%s RF_Flood signal received.\n", datstrf());
if ( i_am_state && (launchp = find_rfflood_scripts()) >= 0 )
launch_scripts(&launchp);
}
else if ( sent_type == SENT_LOCKUP && (i_am_state || i_am_monitor) ) {
fprintf(fdsout, "%s Interface lockup signal received, reason %02x.\n", datstrf(), buf[2]);
if ( i_am_state && (launchp = find_lockup_scripts()) >= 0 )
launch_scripts(&launchp);
}
else if ( sent_type == SENT_SETTIMER && (i_am_state || i_am_monitor) ) {
fprintf(fdsout, "%s %s\n", datstrf(), translate_settimer_message(buf));
// if ( i_am_state )
// write_x10state_file();
}
else if ( sent_type == SENT_RFXTYPE && (i_am_state || i_am_monitor) ) {
fprintf(fdsout, "%s %s %s\n", datstrf(), send_prefix, translate_rfxtype_message(buf));
}
else if ( sent_type == SENT_RFXSERIAL && (i_am_state || i_am_monitor) ) {
fprintf(fdsout, "%s %s %s\n", datstrf(), send_prefix, translate_rfxsensor_ident(buf));
}
else if ( sent_type == SENT_RFVARIABLE && (i_am_state || i_am_monitor) ) {
fprintf(fdsout, "%s %s %s\n", datstrf(), send_prefix, display_variable_aux_data(buf));
}
else if ( sent_type == SENT_SCRIPT && (i_am_state) ) {
launch_script_cmd(buf);
}
else if ( sent_type == SENT_INITSTATE && (i_am_state) ) {
x10state_init(buf[2], buf[3] | (buf[4] << 8));
}
else if ( sent_type == SENT_SHOWBUFFER && i_am_monitor ) {
fprintf(fdsout, "%s %s\n", datstrf(), display_binbuffer(buf + 2));
}
else if ( sent_type == SENT_OTHER ) {
/* Other command */
if ( *(transp = translate_other(buf, n, &chksum)) )
fprintf(fdsout, "%s %s\n", datstrf(), transp);
chksum_alert = chksum;
launchp = -1;
}
}
else {
n = xread(sptty, buf, n - 127, timeout);
}
fflush(fdsout);
return(0);
}
else
n = xread(sptty, buf, 1, timeout);
if( n != 1 ) {
return(0);
}
} /* end of while */
/* print out the saved 0xff from the buffer */
if ( buf[0] != 0xff && wasflag > 0 ) {
if (chksum_alert == 0xff) {
/* a single 0xff was apparently a checksum */
chksum_alert = -1;
wasflag--;
}
n = buf[0];
for(i = 0; i < wasflag;i++)
buf[i] = 0xff;
buf[i] = n;
n = wasflag + 1;
wasflag = 0;
}
if ( buf[0] == chksum_alert ) {
/* this is the checksum expected, just drop it */
chksum_alert = -1;
return 0;
}
if (chksum_alert >= 0) {
/*
* Instead of the checksum expected, we've received something else.
* Don't expect that checksum to appear any longer. Either the
* interface didn't respond with a checksum, busy with an incomming
* transmission or a macro, or we are completely out of sync.
*/
chksum_alert = -1;
}
macro_report = 0;
if ( buf[0] == 0x5a ) {
/* CM11A has polling info for me */
/* The xread is executed a second time on failure because the
dim commands may be tieing up the CM11.
*/
n = xread(sptty, buf, 1, 2); /* get the buffer size */
if ( n == 0)
n = xread(sptty, buf, 1, 2); /* get the buffer size */
if ( n > 0 ) {
/* We have a byte count */
to_read = buf[0]; /* number of bytes to read */
if ( to_read == 0x5a ) {
/* Darn. Another polling indicator */
timeout = 2;
return ( check4poll(showdata, timeout) );
}
else if ( to_read == 0x5b ) { /* Macro report coming in */
to_read = 2;
macro_report = 1;
}
if ( to_read > (int)sizeof(buf) ) {
if( verbose )
fprintf(fdserr, "Polling read size exceeds buffer");
return(-1);
}
n = xread(sptty, buf, to_read , timeout);
if ( n != to_read ) {
fprintf(fdsout, "Poll only got %d of %d bytes\n",
n, to_read);
fflush(fdsout);
return 0;
}
if ( showdata > 0 && (verbose != 0) )
fprintf(fdsout, "I received a poll %d bytes long.\n", n);
if ( macro_report == 1 ) { /* CM11A is reporting a macro execution.*/
if ( showdata ) {
mac_addr = ((buf[0] & 0x07u) << 8) + buf[1];
/* Get macro label (or "Unknown") and saved image checksum */
macfound = lookup_macro(mac_addr, maclabel, &ichksum);
if ( buf[0] & 0x70u ) {
/* Macro was executed by a Trigger */
if ( macfound == 1 && ichksum != -1 && loadcheck_image(ichksum) ) {
/* Determine and display the inferred trigger. */
/* Launch a script if warranted. */
signal_source = RCVT;
tag = (buf[0] & 0x70u) >> 4;
display_trigger(datstrf(), mac_addr, tag);
/* Announce the execution of the macro */
fprintf(fdsout, "%s Trigger executed macro : %s, address 0x%03x\n",
datstrf(), maclabel, mac_addr);
/* Decipher and display macro elements from image file. */
/* Launch script(s) when warranted. */
signal_source = SNDT;
expand_macro(datstrf(), mac_addr, TRIGGER_EXEC);
}
else {
fprintf(fdsout, "%s Trigger executed macro : %s, address 0x%03x\n",
datstrf(), maclabel, mac_addr);
}
}
else {
/* Macro was executed by a Timer */
set_macro_timestamp(time(NULL));
if ( macfound == 1 && ichksum != -1 && loadcheck_image(ichksum) ) {
fprintf(fdsout, "%s Timer executed macro : %s, address 0x%03x\n",
datstrf(), maclabel, mac_addr);
/* Decipher and display macro elements from image file. */
/* Launch script(s) when warranted. */
signal_source = SNDM;
expand_macro(datstrf(), mac_addr, TIMER_EXEC);
}
else {
fprintf(fdsout, "%s Timer executed macro : %s, address 0x%03x\n",
datstrf(), maclabel, mac_addr);
}
}
}
}
else {
signal_source = RCVI;
#if 0
if ( showdata ) {
for ( i = 1; i < to_read; i++)
fprintf(fdsout, " %02x", buf[i]);
fprintf(fdsout, "\n");
}
#endif
for ( i = 1; i < to_read; i++) {
xcmd = xdata = subunit = predim = level = 0;
if ( defer_dim &&
( (funcbits = (newbuf[0] & 0x0Fu)) == DIM || funcbits == BRIGHT) ) {
/* Finish handling a deferred Dim or Bright function */
newbuf[1] = buf[i];
signal_source = RCVI;
x10state_update_func(newbuf, &launchp);
hc = code2hc((newbuf[0] & 0xF0u) >> 4);
level = buf[i];
defer_dim = 0;
newbuf[0] = 0;
if( showdata ) {
fprintf(fdsout, "%s rcvi func %12s : hc %c %s %%%02.0f [%d]\n",
datstrf(), funclabel[funcbits], hc,
((funcbits == DIM) ? "dim" : "bright"),
100.*(float)level/210., level);
}
}
else if ( (buf[0] & (0x01 << (i-1))) != 0) {
/* A function has been received */
signal_source = RCVI;
/* Determine the maximum number of bytes */
/* available in the buffer for the function */
maxlen = max_funclen(to_read, buf[0], (i-1));
hc = code2hc( ((buf[i] & 0xF0u) >> 4) );
funcbits = buf[i] & 0x0Fu;
func = funclabel[buf[i] & 0x0Fu];
if ( funcbits == DIM || funcbits == BRIGHT ) {
if ( maxlen > 1 ) {
/* We have the dim level - handle it here */
x10state_update_func(buf + i, &launchp);
level = buf[i+1];
defer_dim = 0;
i++;
}
else {
/* Otherwise, defer until we've got the dim level */
newbuf[0] = buf[i];
defer_dim = 1;
}
}
else if ( funcbits == PRESET1 || funcbits == PRESET2) {
func = "Preset";
x10state_update_func(buf + i, &launchp);
predim = rev_low_nybble((buf[i] & 0xF0u) >> 4) + 1;
if ( funcbits == PRESET2 )
predim += 16;
fdata = predim;
}
else if ( funcbits == EXTCODE ) {
if ( maxlen >= 4 ) {
/* Buffer looks OK. (Sometimes it gets */
/* garbled when ext code functions are */
/* received by a firmware 1 interface.) */
unit = code2unit(buf[i+1] & 0x0fu);
subunit = (buf[i+1] & 0xf0u) >> 4;
bitmap = 1 << (buf[i+1] & 0x0fu);
xdata = buf[i+2];
xcmd = buf[i+3];
xtype = (xcmd & 0xf0u) >> 4;
if ( xtype == 3 ) {
x10state_update_ext3func(buf + i, &launchp);
}
#ifdef HAVE_FEATURE_EXT0
else if ( xtype == 0 ) {
x10state_update_ext0func(buf + i, &launchp);
}
#endif
else {
x10state_update_extotherfunc(buf + i, &launchp);
}
i += 3;
}
else {
/* Garbled buffer - dump it */
unit = 0;
bitmap = 0;
xdata = 0;
xcmd = 0xff;
i = to_read;
}
}
else {
x10state_update_func(buf + i, &launchp);
}
if ( showdata ) {
hcode = hc2code(hc);
squelch = x10state[hcode].squelch;
if ( squelch != 0 && (x10state[hcode].lastactive & squelch) == 0 ) {
fprintf(fdsout, "%s rcvi addr unit %3d : hu %c%-2d (%s)\n",
datstrf(), code2unit(single_bmap_unit(squelch)),
hc, code2unit(single_bmap_unit(squelch)), lookup_label(hc, squelch) );
x10state[hcode].squelch = squelch = 0;
}
else if ( squelch != 0 && ((squelch & x10state[hcode].state[ChgState]) || launchp >= 0) ) {
fprintf(fdsout, "%s rcvi addr unit %3d : hu %c%-2d (%s)\n",
datstrf(), code2unit(single_bmap_unit(squelch)),
hc, code2unit(single_bmap_unit(squelch)), lookup_label(hc, squelch) );
x10state[hcode].squelch = squelch = 0;
}
if ( (funcbits == DIM || funcbits == BRIGHT) && squelch == 0 ) {
if ( !defer_dim )
fprintf(fdsout, "%s rcvi func %12s : hc %c %s %%%02.0f [%d]\n",
datstrf(), funclabel[funcbits], toupper((int)hc),
((funcbits == DIM) ? "dim" : "bright"),
100.*(float)level/210., level);
}
else if ( (funcbits == PRESET1 || funcbits == PRESET2) && squelch == 0 ) {
fprintf(fdsout, "%s rcvi func %12s : level %d\n",
datstrf(), func, predim);
if ( i_am_monitor &&
configp->rcs_temperature & (1 << hc2code(lasthc)) ) {
/* Special funcs for RCS compatible thermometers */
if ( lastunit > 10 ) {
temperat = -60 + (predim - 1) + 32 * (lastunit - 11);
sprintf(minibuf, "Temperature = %d", temperat);
fprintf(fdsout, "%s %-22s : hu %c0 (%s)\n",
datstrf(), minibuf, toupper((int)lasthc), lookup_label(lasthc, 0));
}
else if ( lastunit == 6 && predim < sizeof(rcs_status) / sizeof(*rcs_status) + 1 ) {
fprintf(fdsout, "%s %-22s : hu %c0 (%s)\n",
datstrf(), rcs_status[predim - 1], toupper((int)lasthc),
lookup_label(lasthc, 0));
}
}
}
else if ( funcbits == EXTCODE && squelch == 0 ) {
char tmp[10], stmp[16];
sprintf(tmp, "%d", unit);
bitmap = 1 << unit2code(unit);
if ( subunit == 0 )
stmp[0] = '\0';
else
sprintf(stmp, " subunit %02d", subunit);
if ( xcmd == 0x31 ) {
if ( xdata & 0xc0u ) {
fprintf(fdsout,
"%s rcvi func xPreset : hu %c%-2d%s level %d ramp %d (%s)\n",
datstrf(), hc, unit, stmp, xdata & 0x3f, (xdata & 0xc0) >> 6,
lookup_label(hc, bitmap));
}
else {
fprintf(fdsout,
"%s rcvi func xPreset : hu %c%-2d%s level %d (%s)\n",
datstrf(), hc, unit, stmp, xdata, lookup_label(hc, bitmap));
}
}
else if ( xcmd == 0x3b )
fprintf(fdsout, "%s rcvi func xConfig : hc %c mode=%d\n",
datstrf(), hc, xdata);
else if ( xcmd == 0x33 )
fprintf(fdsout, "%s rcvi func xAllOn : hc %c\n",
datstrf(), hc);
else if ( xcmd == 0x34 )
fprintf(fdsout, "%s rcvi func xAllOff : hc %c\n",
datstrf(), hc);
else if ( xcmd == 0x37 ) {
if ( (xdata & 0x30u) == 0 ) {
fprintf(fdsout, "%s rcvi func xStatusReq : hu %c%-2d%s (%s)\n",
datstrf(), hc, unit, stmp, lookup_label(hc, bitmap));
}
else if ( (xdata & 0x30u) == 0x10 ) {
fprintf(fdsout, "%s rcvi func xPowerUp : hu %c%-2d%s (%s)\n",
datstrf(), hc, unit, stmp, lookup_label(hc, bitmap));
}
else if ( (xdata & 0x30u) == 0x20 ) {
fprintf(fdsout, "%s rcvi func xGrpStatus : hu %c%-2d%s group %d (%s)\n",
datstrf(), hc, unit, stmp, (xdata & 0xc0u) >> 6, lookup_label(hc, bitmap));
}
else {
fprintf(fdsout, "%s rcvi func xGrpStatus : hu %c%-2d%s group %d.%-2d (%s)\n",
datstrf(), hc, unit, stmp, (xdata & 0xc0u) >> 6, (xdata & 0x0fu) + 1, lookup_label(hc, bitmap));
}
}
else if ( xcmd == 0x38 ) {
if ( xdata & 0x40u )
fprintf(fdsout, "%s rcvi func xStatusAck : hu %c%-2d%s Switch %-3s %s (%s)\n",
datstrf(), hc, unit, stmp, ((xdata & 0x3fu) ? "On " : "Off"),
((xdata & 0x80u) ? "LoadOK" : "NoLoad"),
lookup_label(hc, bitmap));
else
fprintf(fdsout, "%s rcvi func xStatusAck : hu %c%-2d%s Lamp level %d, %s (%s)\n",
datstrf(), hc, unit, stmp, xdata & 0x3fu,
((xdata & 0x80u) ? "BulbOK" : "NoBulb"),
lookup_label(hc, bitmap));
}
else if ( xcmd == 0x30 ) {
if ( (xdata & 0x20u) == 0 ) {
fprintf(fdsout, "%s rcvi func xGrpAdd : hu %c%-2d%s group %d (%s)\n",
datstrf(), hc, unit, stmp, (xdata & 0xc0u) >> 6, lookup_label(hc, bitmap));
}
else {
fprintf(fdsout, "%s rcvi func xGrpAdd : hu %c%-2d%s group %d.%-2d (%s)\n",
datstrf(), hc, unit, stmp, (xdata & 0xc0u) >> 6, (xdata & 0x0fu) + 1, lookup_label(hc, bitmap));
}
}
else if ( xcmd == 0x32 ) {
fprintf(fdsout, "%s rcvi func xGrpAddLvl : hu %c%-2d%s group %d level %d (%s)\n",
datstrf(), hc, unit, stmp, (xdata & 0xc0u) >> 6, xdata & 0x3fu, lookup_label(hc, bitmap));
}
else if ( xcmd == 0x35 ) {
if ( (xdata & 0x30) == 0 ) {
fprintf(fdsout, "%s rcvi func xGrpRem : hu %c%-2d%s group(s) %s (%s)\n",
datstrf(), hc, unit, stmp, linmap2list(xdata & 0x0f), lookup_label(hc, bitmap));
}
else {
fprintf(fdsout, "%s rcvi func xGrpRemAll : hc %c group(s) %s\n",
datstrf(), hc, linmap2list(xdata & 0x0f));
}
}
else if ( xcmd == 0x36 ) {
if ( (xdata & 0x30u) == 0 ) {
fprintf(fdsout, "%s rcvi func xGrpExec : hc %c group %d\n",
datstrf(), hc, (xdata & 0xc0u) >> 6);
}
else if ( (xdata & 0x30u) == 0x20u ) {
fprintf(fdsout, "%s rcvi func xGrpExec : hc %c group %d.%-2d\n",
datstrf(), hc, (xdata & 0xc0u) >> 6, (xdata & 0x0fu) + 1);
}
else if ( (xdata & 0x30u) == 0x10u ) {
fprintf(fdsout, "%s rcvi func xGrpOff : hc %c group %d\n",
datstrf(), hc, (xdata & 0xc0u) >> 6);
}
else {
fprintf(fdsout, "%s rcvi func xGrpOff : hc %c group %d.%-2d\n",
datstrf(), hc, (xdata & 0xc0u) >> 6, (xdata & 0x0fu) + 1);
}
}
else if ( xcmd == 0x39 ) {
fprintf(fdsout, "%s rcvi func xGrpAck : hu %c%-2d%s group %d level %d (%s)\n",
datstrf(), hc, unit, stmp, (xdata & 0xc0) >> 6, xdata & 0x3fu, lookup_label(hc, bitmap));
}
else if ( xcmd == 0x3A ) {
fprintf(fdsout, "%s rcvi func xGrpNack : hu %c%-2d%s group %d (%s)\n",
datstrf(), hc, unit, stmp, (xdata & 0xc0) >> 6, lookup_label(hc, bitmap));
}
else if ( xcmd == 0x3C ) {
if ( (xdata & 0x30u) == 0 ) {
fprintf(fdsout, "%s rcvi func xGrpDim : hc %c group %d\n",
datstrf(), hc, (xdata & 0xc0u) >> 6);
}
else if ( (xdata & 0x30u) == 0x10 ) {
fprintf(fdsout, "%s rcvi func xGrpBright : hc %c group %d\n",
datstrf(), hc, (xdata & 0xc0u) >> 6);
}
else if ( (xdata & 0x30u) == 0x20 ) {
fprintf(fdsout, "%s rcvi func xGrpDim : hc %c group %d.%-2d\n",
datstrf(), hc, (xdata & 0xc0u) >> 6, (xdata & 0x0fu) + 1);
}
else {
fprintf(fdsout, "%s rcvi func xGrpBright : hc %c group %d.%-2d\n",
datstrf(), hc, (xdata & 0xc0u) >> 6, (xdata & 0x0fu) + 1);
}
}
else if ( xcmd == 0x01 )
fprintf(fdsout, "%s rcvi func shOpenLim : hu %c%-2d%s level %d (%s)\n",
datstrf(), hc, unit, stmp, xdata, lookup_label(hc, bitmap));
else if ( xcmd == 0x03 )