forked from HeyuX10Automation/heyu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
x10aux.c
2416 lines (2119 loc) · 77.7 KB
/
x10aux.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
/*----------------------------------------------------------------------------+
| |
| RF Auxiliary Input Support for HEYU |
| Copyright 2006, 2008 Charles W. Sullivan |
| |
| |
| 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_STRING_H
#include <string.h>
#endif
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#include <signal.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
#ifdef HAVE_SYSLOG_H
#include <syslog.h>
#endif
#ifdef HAVE_ERRNO_H
#include <errno.h>
#endif
#ifdef HAVE_SYS_ERRNO_H
#include <sys/errno.h>
#endif
#include <ctype.h>
#include "x10.h"
#include "process.h"
#include "oregon.h"
#ifdef pid_t
#define PID_T pid_t
#else
#define PID_T long
#endif
#define W800_LEN 4
#define PACKET_LEN 4
#define BSIZE (100 * PACKET_LEN)
#define HC_MASK 0xF000
#define FUNC_MASK 0x00B8
#define UNIT_MASK 0x0458
#define NOSW_MASK 0x0003
#define OFF_MASK 0x0020
#define OLD_MASK 0x0800
#define NEW_MASK 0x0040
#define STD_MASK (HC_MASK | FUNC_MASK | UNIT_MASK)
#define NOTX10_MASK ~(HC_MASK|FUNC_MASK|UNIT_MASK|OLD_MASK)
extern int tty_aux;
extern int sptty;
extern int tty;
extern int verbose;
extern int heyu_parent;
extern int sxread();
extern int i_am_aux, i_am_relay;
extern PID_T lockpid();
extern int lock_for_write(), munlock();
extern int is_sec_ignored ( unsigned int );
extern void send_showbuffer ( unsigned char *, unsigned char );
extern CONFIG config, *configp;
extern unsigned int rflastaddr[16];
extern unsigned int rftransceive[16][7];
extern unsigned int rfforward[16][7];
static unsigned char rf2hcode[16] = {
0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15
};
#if 0
/* RF Codes for X10 Housecodes (A-P) */
static unsigned int hc2rfcode[16] = {
0x6000, 0x7000, 0x4000, 0x5000,
0x8000, 0x9000, 0xA000, 0xB000,
0xE000, 0xF000, 0xC000, 0xD000,
0x0000, 0x1000, 0x2000, 0x3000,
};
/* Housecode for RF code (upper nybble) */
static char *rf2hc = "MNOPCDABEFGHKLIJ";
#endif
/* RF Codes for X10 _encoded_ unit codes */
static unsigned int rf_unit_code[16] = {
0x0440, 0x0040, 0x0008, 0x0408,
0x0448, 0x0048, 0x0000, 0x0400,
0x0450, 0x0050, 0x0018, 0x0418,
0x0458, 0x0058, 0x0010, 0x0410,
};
/* RF Function Codes: */
/* AllOff, LightsOn, On, Off, */
/* Dim, Bright, LightsOff */
static unsigned int rf_func_code[] = {
0x0080, 0x0090, 0x0000, 0x0020,
0x0098, 0x0088, 0x00a0,
};
char *funclist[7] =
{"alloff", "lightson", "on", "off", "dim", "bright", "lightsoff"};
#if 0
struct rfx_disable_st {
unsigned long bitmap;
unsigned short code;
char *label;
int minlabel;
#endif
struct rfx_disable_st rfx_disable[] = {
{RFXCOM_ARCTECH, 0xF02D, "ARCTECH", 3},
{RFXCOM_OREGON, 0xF043, "OREGON", 3},
{RFXCOM_ATIWONDER, 0xF044, "ATIWONDER", 3},
{RFXCOM_X10, 0xF02F, "X10", 3},
{RFXCOM_KOPPLA, 0xF02E, "KOPPLA", 3},
{RFXCOM_HE_UK, 0xF028, "HE_UK", 4},
{RFXCOM_HE_EU, 0xF047, "HE_EU", 4},
{RFXCOM_VISONIC, 0xF045, "VISONIC", 3},
};
int nrfxdisable = sizeof(rfx_disable) / sizeof(struct rfx_disable_st);
/*-------------------------------------------------------------+
| Check list of valid 4-byte codes other than Standard, |
| Entertainment, and Security codes, e.g., Pan/Tilt |
| Return type code if valid, otherwise RF_NOISEW800. |
| (*** Future ***) |
+-------------------------------------------------------------*/
int is_valid_w800_code( long code )
{
return RF_NOISEW800;
}
/*-------------------------------------------------------------+
| Get the firmware versions of master and slave RFXCOM |
| receiver. (The query code is normally 0xF020) |
+-------------------------------------------------------------*/
int rfxcom_version ( unsigned short code, unsigned char *master, unsigned char *slave )
{
unsigned char inbuf[16];
unsigned char outbuf[4];
int nread;
int ignoret;
outbuf[0] = (code >> 8) & 0xffu;
outbuf[1] = code & 0xffu;
*master = *slave = 0;
ignoret = write(tty_aux, (char *)outbuf, 2);
nread = sxread(tty_aux, inbuf, 4, 1);
if ( nread == 2 ) {
if ( inbuf[0] == 'M' )
*master = inbuf[1];
else if ( inbuf[0] == 'S' )
*slave = inbuf[1];
}
else if ( nread == 4 ) {
if ( inbuf[0] == 'M' )
*master = inbuf[1];
else if ( inbuf[0] == 'S' )
*slave = inbuf[1];
if ( inbuf[2] == 'M' )
*master = inbuf[3];
else if ( inbuf[2] == 'S' )
*slave = inbuf[3];
}
else if ( nread == 1 ) {
syslog(LOG_ERR, "RFXCOM version query returned only a single byte, 0x%02x\n", inbuf[0]);
return 1;
}
else if ( nread == 3 ) {
syslog(LOG_ERR, "RFXCOM version query returned 3 bytes, 0x%02x 0x%02x 0x%02x\n",
inbuf[0], inbuf[1], inbuf[2]);
return 1;
}
else {
syslog(LOG_ERR, "RFXCOM version query returned nothing.\n");
return 1;
}
return 0;
}
/*-------------------------------------------------------------+
| Configure an RFXCOM mode. When sent the 2-byte configure |
| code, e.g., 0xF029 for W800 emulation mode, it should |
| respond with the ack byte, e.g., 0x29. |
+-------------------------------------------------------------*/
unsigned char configure_rfxcom ( unsigned short code )
{
unsigned char inbuf[4];
unsigned char outbuf[4];
int ignoret;
outbuf[0] = (code >> 8) & 0xffu;
outbuf[1] = code & 0xffu;
ignoret = write(tty_aux, (char *)outbuf, 2);
if ( sxread(tty_aux, inbuf, 1, 1) <= 0 )
return 0xffu;
return inbuf[0];
}
/*-------------------------------------------------------------+
| Configure the RF receiver if required. |
+-------------------------------------------------------------*/
int configure_rf_receiver ( unsigned char auxdev )
{
unsigned char inbuf[8], mver = 0xff, sver = 0xff;
unsigned short rtn, crtn;
int is_config;
int j;
char *receiver;
extern int config_tty_dtr_rts ( int, unsigned char );
/* Nothing required for W800RF32 or MR26A */
if ( auxdev == DEV_W800RF32 || auxdev == DEV_MR26A )
return 0;
/* Configuration below is for RFXCOM receivers */
/* Empty the serial port buffers */
while ( sxread(tty_aux, inbuf, 1, 1) > 0 );
/* Get the RFXCOM firmware versions */
rfxcom_version(0xF020, &mver, &sver);
crtn = 0;
if ( auxdev == DEV_RFXCOMVL ) {
/* Variable length mode */
is_config = ((crtn = configure_rfxcom(0xF02C)) == 0x2Cu);
if ( !is_config ) {
syslog(LOG_ERR, "RFXCOM F02C, ack 0x%02x\n", crtn);
}
}
else if ( auxdev == DEV_RFXCOM32 ) {
/* 32 bit (W800) mode */
is_config = ((crtn = configure_rfxcom(0xF029)) == 0x29u);
if ( !is_config ) {
syslog(LOG_ERR, "RFXCOM F029, ack 0x%02x\n", crtn);
}
}
else {
is_config = 0;
}
/* Enable all possible receiving modes */
if ((rtn = configure_rfxcom(0xF02A)) != crtn )
syslog(LOG_ERR, "RFXCOM F02A, ack 0x%02x\n", rtn);
#if 0
/* Disable ARC-Tech RF unless enabled */
if ( (configp->rfxcom_enable & RFXCOM_ARCTECH) == 0 ) {
if ((rtn = configure_rfxcom(0xF02D)) != crtn )
syslog(LOG_ERR, "RFXCOM F02D, ack 0x%02x\n", rtn);
}
/* Disable Oregon RF unless enabled */
if ( (configp->rfxcom_enable & RFXCOM_OREGON) == 0 ) {
if ((rtn = configure_rfxcom(0xF043)) != crtn )
syslog(LOG_ERR, "RFXCOM F043, ack 0x%02x\n", rtn);
}
/* Disable ATI Remote Wonder RF unless enabled */
if ( (configp->rfxcom_enable & RFXCOM_ATIWONDER) == 0 ) {
if ( (rtn = configure_rfxcom(0xF044)) != crtn )
syslog(LOG_ERR, "RFXCOM F044, ack 0x%02x\n", rtn);
}
#endif
#if 0
/* Enable specific modes (DEPRECATED) */
for ( j = 0; j < 3; j++ ) {
if ( !(configp->rfxcom_enable & rfx_disable[j].bitmap) ) {
if ( (rtn = configure_rfxcom(rfx_disable[j].code)) != crtn)
syslog(LOG_ERR, "RFXCOM 0x%4X, ack 0x%02x\n", rfx_disable[j].code, rtn);
}
}
#endif
/* Disable specific modes */
for ( j = 0; j < nrfxdisable /*sizeof(rfx_disable)/sizeof(struct rfx_disable_st)*/; j++ ) {
if ( configp->rfxcom_disable & rfx_disable[j].bitmap ) {
if ( (rtn = configure_rfxcom(rfx_disable[j].code)) != crtn)
// syslog(LOG_ERR, "RFXCOM 0x%4X, ack 0x%02x\n", rfx_disable[j].code, rtn);
syslog(LOG_ERR, "Disable %s failure: bad ack 0x%02x\n", rfx_disable[j].label, rtn);
}
}
receiver =
(auxdev == DEV_RFXCOM32) ? "RFXCOM32" :
(auxdev == DEV_RFXCOMVL) ? "RFXCOMVL" : "UNKNOWN";
if ( is_config ) {
syslog(LOG_ERR, "RFXCOM version M:0x%02x S:0x%02x\n", mver, sver);
syslog(LOG_ERR, "%s configured-\n", receiver);
}
else {
syslog(LOG_ERR, "%s not configured\n", receiver);
return 1;
}
return 0;
}
/*-------------------------------------------------------------+
| Translate Standard X10 RF code into hcode, ucode, and fcode |
+-------------------------------------------------------------*/
int rf_parms (int rfword, unsigned char *hcode, unsigned char *ucode, unsigned char *fcode)
{
unsigned int rem;
int j;
/* Old X10 wireless switch code to new code */
if ( rfword & OLD_MASK ) {
rfword = (rfword & ~OLD_MASK) | NEW_MASK;
}
/* Standardize */
rfword &= STD_MASK;
*hcode = rf2hcode[(rfword & HC_MASK) >> 12];
rem = rfword & ~HC_MASK & ~OFF_MASK;
for ( j = 0; j < 16; j++ ) {
if ( rem == rf_unit_code[j] ) {
*ucode = j;
*fcode = (rfword & OFF_MASK) ? 3 : 2;
return 0;
}
}
*ucode = 0xff;
rem = rfword & 0xff;
for ( j = 0; j < 7; j++ ) {
if ( rem == rf_func_code[j] ) {
*fcode = j;
return 0;
}
}
return 1;
}
/*-------------------------------------------------------------+
| Determine what to do with standard X10 RF signal |
+-------------------------------------------------------------*/
int rf_disposition ( unsigned char hcode, unsigned char ucode,
unsigned char fcode )
{
int retcode = RF_IGNORE;
unsigned int bitmap;
switch ( fcode ) {
case 2 :
case 3 :
bitmap = 1 << ucode;
rflastaddr[hcode] = bitmap;
case 4 :
case 5 :
bitmap = rflastaddr[hcode];
if ( rftransceive[hcode][fcode] & bitmap )
retcode = RF_TRANSCEIVE;
else if ( rfforward[hcode][fcode] & bitmap )
retcode = RF_FORWARD;
else
retcode = RF_IGNORE;
break;
case 0 :
case 1 :
case 6 :
default :
if ( rftransceive[hcode][fcode] )
retcode = RF_TRANSCEIVE;
else if ( rfforward[hcode][fcode] )
retcode = RF_FORWARD;
else
retcode = RF_IGNORE;
break;
}
return retcode;
}
/*----------------------------------------------------------------------------+
| Display a text message in the heyu monitor/state engine logfile |
| Maximum length 80 characters. |
+----------------------------------------------------------------------------*/
int display_aux_message ( char *message )
{
extern int sptty;
int size;
unsigned char buf[88];
char writefilename[PATH_LEN + 1];
int ignoret;
static unsigned char template[7] = {
0xff,0xff,0xff,0,ST_COMMAND,ST_MSG,0};
if ( (size = strlen(message)) > (int)(sizeof(buf) - 8) ) {
fprintf(stderr,
"Log message exceeds %d characters\n", (int)(sizeof(buf) - 8));
return 1;
}
sprintf(writefilename, "%s%s", WRITEFILE, configp->suffix);
if ( lock_for_write() < 0 ) {
syslog(LOG_ERR, "aux unable to lock writefile\n");
return 1;
}
memcpy(buf, template, sizeof(template));
memcpy(buf + 7, message, size + 1);
buf[6] = size + 1;
buf[3] = size + 1 + 3;
ignoret = write(sptty, buf, size + 8);
munlock(writefilename);
return 0;
}
int atp ( int testpoint )
{
char message[80];
sprintf(message, "Aux Testpoint %d", testpoint);
display_aux_message(message);
return 0;
}
/*----------------------------------------------------------------------------+
| Forward variable length data from aux to the heyu monitor/state engine |
+----------------------------------------------------------------------------*/
int forward_variable_aux_data ( unsigned char vl_type, unsigned char *buff, unsigned char length )
{
extern int sptty;
char writefilename[PATH_LEN + 1];
unsigned char sendbuf[80];
int ignoret;
static unsigned char template[15] = {
0xff,0xff,0xff,3,ST_COMMAND,ST_SOURCE,D_AUXRCV,
0xff,0xff,0xff,0,ST_COMMAND,ST_VARIABLE_LEN,0,0};
template[10] = length + 4;
template[13] = vl_type;
template[14] = length;
memcpy(sendbuf, template, sizeof(template));
memcpy(sendbuf + sizeof(template), buff, length);
if (i_am_state || i_am_monitor)
return process_received(&sendbuf[11], sendbuf[10], heyu_parent);
sprintf(writefilename, "%s%s", WRITEFILE, configp->suffix);
if ( lock_for_write() < 0 ) {
syslog(LOG_ERR, "aux unable to lock writefile\n");
return 1;
}
ignoret = write(sptty, sendbuf, sizeof(template) + length);
#if 0
ignoret = write(sptty, template, 15);
ignoret = write(sptty, buff, length);
#endif
munlock(writefilename);
return 0;
}
/*----------------------------------------------------------------------------+
| Forward std data from aux to the heyu monitor/state engine |
+----------------------------------------------------------------------------*/
int forward_standard_aux_data ( unsigned char byte1, unsigned char byte2 )
{
extern int sptty;
char writefilename[PATH_LEN + 1];
int ignoret;
static unsigned char template[13] = {
0xff,0xff,0xff,3,ST_COMMAND,ST_SOURCE,D_AUXRCV,
0xff,0xff,0xff,2,0,0};
template[11] = byte1;
template[12] = byte2;
if (i_am_state || i_am_monitor)
return process_received(&template[11], 2, heyu_parent);
sprintf(writefilename, "%s%s", WRITEFILE, configp->suffix);
if ( lock_for_write() < 0 ) {
syslog(LOG_ERR, "aux unable to lock writefile\n");
return 1;
}
ignoret = write(sptty, template, 13);
munlock(writefilename);
return 0;
}
/*----------------------------------------------------------------------------+
| Send virtual data from aux to the heyu monitor/state engine |
+----------------------------------------------------------------------------*/
int send_virtual_aux_data ( unsigned char address, unsigned char vdata,
unsigned char vtype, unsigned char vidlo, unsigned char vidhi, unsigned char byte2, unsigned char byte3 )
{
extern int sptty;
char writefilename[PATH_LEN + 1];
int ignoret;
static unsigned char template[20] = {
0xff,0xff,0xff,3,ST_COMMAND,ST_SOURCE,D_AUXRCV,
0xff,0xff,0xff,9,ST_COMMAND,ST_VDATA,0,0,0,0,0,0,0};
sprintf(writefilename, "%s%s", WRITEFILE, configp->suffix);
if ( lock_for_write() < 0 ) {
syslog(LOG_ERR, "aux unable to lock writefile\n");
return 1;
}
template[13] = address;
template[14] = vdata;
template[15] = vtype;
template[16] = vidlo;
template[17] = vidhi;
template[18] = byte2;
template[19] = byte3;
ignoret = write(sptty, template, 20);
munlock(writefilename);
return 0;
}
/*----------------------------------------------------------------------------+
| Send virtual data from command line to the heyu monitor/state engine |
| (Similar to the above, but the file lock is already acquired.) |
+----------------------------------------------------------------------------*/
int send_virtual_cmd_data ( unsigned char address, unsigned char vdata,
unsigned char vtype, unsigned char vidlo, unsigned char vidhi, unsigned char byte2, unsigned char byte3 )
{
extern int sptty;
unsigned char source;
int ignoret;
source = heyu_parent;
static unsigned char template[20] = {
0xff,0xff,0xff,3,ST_COMMAND,ST_SOURCE,0,
0xff,0xff,0xff,9,ST_COMMAND,ST_VDATA,0,0,0,0,0,0,0};
template[6] = source;
template[13] = address;
template[14] = vdata;
template[15] = vtype;
template[16] = vidlo;
template[17] = vidhi;
template[18] = byte2;
template[19] = byte3;
if (i_am_state || i_am_monitor) {
process_received(&template[11], 9, source);
return 0;
}
ignoret = write(sptty, template, 20);
return 0;
}
/*----------------------------------------------------------------------------+
| Send buffer of data from aux to the heyu monitor/state engine |
+----------------------------------------------------------------------------*/
int forward_aux_longdata ( unsigned char vtype, unsigned char seq, unsigned char id_high, unsigned char id_low,
unsigned char nbytes, unsigned char buff[])
{
extern int sptty;
char writefilename[PATH_LEN + 1];
unsigned char sendbuf[80];
int ignoret;
static unsigned char template[18] = {
0xff,0xff,0xff,3,ST_COMMAND,ST_SOURCE,D_AUXRCV,
0xff,0xff,0xff,0,ST_COMMAND,ST_LONGVDATA,0, 0,0,0,0};
template[10] = nbytes + 7;
template[13] = vtype;
template[14] = seq;
template[15] = id_high;
template[16] = id_low;
template[17] = nbytes;
memcpy(sendbuf, template, sizeof(template));
memcpy(sendbuf + sizeof(template), buff, nbytes);
if (i_am_state || i_am_monitor)
return process_received(&sendbuf[11], sendbuf[10], heyu_parent);
sprintf(writefilename, "%s%s", WRITEFILE, configp->suffix);
if ( lock_for_write() < 0 ) {
syslog(LOG_ERR, "aux unable to lock writefile\n");
return 1;
}
ignoret = write(sptty, sendbuf, sizeof(template) + nbytes);
munlock(writefilename);
return 0;
}
/*----------------------------------------------------------------------------+
| Forward general aux data. |
| Buffer reference: ST_COMMAND, ST_GENLONG, plus: |
| vtype, subtype, vidhi, vidlo, seq, buflen, buffer |
| [2] [3] [4] [5] [6] [7] [8+] |
+----------------------------------------------------------------------------*/
int forward_gen_longdata ( unsigned char vtype, unsigned char subtype, int nseq,
unsigned char id_high, unsigned char id_low, unsigned char nbytes, unsigned char buff[] )
{
extern int sptty;
char writefilename[PATH_LEN + 1];
unsigned char outbuf[128];
int j;
int ignoret = 0;
static unsigned char template[19] = {
0xff,0xff,0xff,3,ST_COMMAND,ST_SOURCE,D_AUXRCV,
0xff,0xff,0xff,0,ST_COMMAND,ST_GENLONG,0,0,0,0,0,0};
if ( (sizeof(template) + nbytes) > sizeof(outbuf) ) {
syslog(LOG_ERR, "forward_gen_longdata(): input buffer too long\n");
return 1;
}
template[10] = nbytes + 8;
template[13] = vtype;
template[14] = subtype;
template[15] = id_high;
template[16] = id_low;
template[18] = nbytes;
memcpy(outbuf, template, (sizeof(template)));
memcpy(outbuf + (sizeof(template)), buff, nbytes);
if (!i_am_state && !i_am_monitor) {
sprintf(writefilename, "%s%s", WRITEFILE, configp->suffix);
if ( lock_for_write() < 0 ) {
syslog(LOG_ERR, "aux unable to lock writefile\n");
return 1;
}
}
for ( j = 0; j < nseq; j++ ) {
outbuf[17] = (unsigned char)(j + 1);
if (!i_am_state && !i_am_monitor) {
ignoret = write(sptty, (char *)outbuf, (sizeof(template)) + nbytes);
} else {
ignoret = process_received(&outbuf[11], outbuf[10], heyu_parent);
if (ignoret != 0)
break;
}
}
if (!i_am_state && !i_am_monitor) {
munlock(writefilename);
return 0;
} else {
return ignoret;
}
}
/*-------------------------------------------------------------+
| This function forwards Digimax data to the Engine 3 times, |
| with the parity removed and replaced by a sequence number |
| 1-3. (This is so the engine can split the data into three |
| separate functions.) |
+-------------------------------------------------------------*/
int forward_digimax ( unsigned char *buff )
{
int j;
for ( j = 0; j < 3; j++ ) {
forward_aux_longdata(RF_DIGIMAX, j + 1, buff[0], buff[1], 6, buff);
}
return 0;
}
/*-------------------------------------------------------------+
| Forward RF data of type RF_STD (Standard X10 RF remotes) |
| to Heyu engine via the spoolfile. |
+-------------------------------------------------------------*/
int forward_std ( unsigned char hcode, unsigned char ucode,
unsigned char fcode )
{
unsigned char cmdbyte;
switch ( fcode ) {
case 2 :
case 3 :
forward_standard_aux_data(0x04, (hcode << 4 | ucode));
forward_standard_aux_data(0x06, (hcode << 4 | fcode));
break;
case 4 :
case 5 :
cmdbyte = 0x06 | (configp->trans_dim << 3);
forward_standard_aux_data(cmdbyte, (hcode << 4 | fcode));
break;
case 0 :
case 1 :
case 6 :
forward_standard_aux_data(0x06, (hcode << 4 | fcode));
break;
default :
break;
}
return 0;
}
/*-------------------------------------------------------------+
| Transceive RF data of type RF_STD (Standard X10 RF remotes) |
| and send to spoolfile and interface per config. |
+-------------------------------------------------------------*/
int transceive_std( unsigned char hcode, unsigned char ucode,
unsigned char fcode )
{
unsigned char cmdbuf[16];
char writefilename[PATH_LEN + 1];
if (i_am_state || i_am_monitor)
return 0;
if ( configp->device_type & DEV_DUMMY ) {
display_x10state_message("Unable to transceive to TTY dummy");
return 1;
}
sprintf(writefilename, "%s%s", WRITEFILE, configp->suffix);
if ( lock_for_write() < 0 ) {
syslog(LOG_ERR, "aux unable to lock writefile\n");
return 1;
}
switch ( fcode ) {
case 2 : /* On */
case 3 : /* Off */
send_address(hcode, (1 << ucode), NO_SYNC);
cmdbuf[0] = 0x06;
cmdbuf[1] = (hcode << 4) | fcode;
send_command(cmdbuf, 2, 0, NO_SYNC);
break;
case 4 : /* Dim */
case 5 : /* Bright */
cmdbuf[0] = 0x06 | (configp->trans_dim << 3);
cmdbuf[1] = (hcode << 4) | fcode;
send_command(cmdbuf, 2, 0, NO_SYNC);
break;
case 0 : /* AllOff */
case 1 : /* LightsOn */
case 6 : /* LightsOff */
default :
cmdbuf[0] = 0x06;
cmdbuf[1] = (hcode << 4) | fcode;
send_command(cmdbuf, 2, 0, NO_SYNC);
break;
}
munlock(writefilename);
return 0;
}
/*-------------------------------------------------------------+
| Process RF data of type RF_STD (Standard X10 RF remotes) |
| and send to spoolfile and interface per config. |
+-------------------------------------------------------------*/
int proc_type_std( unsigned char hcode, unsigned char ucode,
unsigned char fcode )
{
int dispo;
dispo = rf_disposition(hcode, ucode, fcode);
if ( dispo == RF_TRANSCEIVE )
transceive_std(hcode, ucode, fcode);
else if ( dispo == RF_FORWARD )
forward_std(hcode, ucode, fcode);
return 0;
}
/*-------------------------------------------------------------+
| RFXMeter 4 bit checksum. A valid message returns 0x00. |
| Force a failure if Heyu is not configured for RFXMeters. |
+-------------------------------------------------------------*/
int rfxmeter_checksum ( unsigned char *buf )
{
unsigned int chksum;
chksum = buf[0] + (buf[0] >> 4) + buf[1] + (buf[1] >> 4) +
buf[2] + (buf[2] >> 4) + buf[3] + (buf[3] >> 4) +
buf[4] + (buf[4] >> 4) + buf[5] + (buf[5] >> 4);
// chksum &= 0x0fu;
chksum = (chksum - 0x0fu) & 0x0fu;
#ifdef HAVE_FEATURE_RFXM
return (int)chksum;
#else
return (int)0xffu;
#endif
}
/*-------------------------------------------------------------+
| RFXSensor 4 bit checksum. A valid message returns 0x0f. |
| Force a failure if Heyu is not configured for RFXSensors. |
+-------------------------------------------------------------*/
int rfxsensor_checksum ( unsigned char *buf )
{
unsigned int chksum;
chksum = buf[0] + (buf[0] >> 4) + buf[1] + (buf[1] >> 4) +
buf[2] + (buf[2] >> 4) + buf[3] + (buf[3] >> 4);
chksum = ~chksum & 0x0fu;
// chksum &= 0x0fu;
#ifdef HAVE_FEATURE_RFXS
return (int)chksum;
#else
return (int)0xffu;
#endif
}
/*-------------------------------------------------------------+
| Parity check for 6-byte X10 Security messages in RFXCOM |
| variable length mode. Return 0 for pass, 1 for fail. |
| This is the even parity of byte4 and bit 7 of byte5. | |
| |
| Some RF modules, e.g., the AUX channel in a DS90 Door/ |
| Window sensors, don't reliably transmit a signal with the |
| correct parity bit set, apparently due to a firmware bug. |
| The config directive allows this checking to be bypasssed. |
+-------------------------------------------------------------*/
int security_checksum ( unsigned char byte4, unsigned char byte5 )
{
int j, bitsum = 0;
if ( configp->securid_parity == NO )
return 0;
for ( j = 0; j < 8; j++ )
bitsum += (byte4 >> j) & 0x01;
return ( (bitsum % 2) && (byte5 & 0x80)) ? 0 :
(!(bitsum % 2) && !(byte5 & 0x80)) ? 0 : 1;
}
/*-------------------------------------------------------------+
| Parity check for 6-byte Digimax message in RFXCOM variable |
| length mode. Return 0 for pass, otherwise fail. |
+-------------------------------------------------------------*/
unsigned char digimax_checksum ( unsigned char *buf )
{
unsigned char sum1, sum2;
sum1 = ~((buf[0] >> 4) + (buf[0] & 0x0fu) +
(buf[1] >> 4) + (buf[1] & 0x0fu) +
(buf[2] >> 4) + (buf[2] & 0x0fu)) & 0x0fu;
sum2 = ~((buf[3] >> 4) + (buf[3] & 0x0fu) +
(buf[4] >> 4) + (buf[4] & 0x0fu) +
(buf[5] >> 4) + (buf[5] & 0x0fu)) & 0x0fu;
#ifdef HAVE_FEATURE_DMX
return (sum1 << 4) | sum2;
#else
return 0xffu;
#endif
}
/*-------------------------------------------------------------+
| Alert to an RF Flood |
+-------------------------------------------------------------*/
int send_rf_flood_message( unsigned char vflags )
{
send_virtual_aux_data(0, vflags, RF_FLOOD, 0, 0, 0, 0);
return 0;
}
int write_restart_error ( char *restartfile )
{
FILE *fp;
if ( (fp = fopen(restartfile, "w")) == NULL ) {
syslog(LOG_ERR, "Cannot fopen restartfile\n");
return 1;
}
fprintf(fp, "%s\n", error_message());
fclose(fp);
return 0;
}
#define ABSMAX_IN_A_ROW 1000000