-
Notifications
You must be signed in to change notification settings - Fork 27
/
relay_aux.c
427 lines (362 loc) · 12 KB
/
relay_aux.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
/*----------------------------------------------------------------------------+
| |
| HEYU Auxiliary Input Daemon |
| Copyright 2002,2003,2004-2007 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
#ifdef SCO
#define _IBCS2
#endif
#include <stdio.h>
#include <ctype.h>
#include <signal.h>
#ifdef HAVE_ERRNO_H
#include <errno.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#ifdef HAVE_SYSLOG_H
#include <syslog.h>
#endif
#ifdef HAVE_SYS_RESOURCE_H
#include <sys/resource.h>
#endif
#ifdef HAVE_LIMITS_H
#include <limits.h>
#endif
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <time.h>
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#include "x10.h"
#include "process.h"
#ifdef pid_t
#define PID_T pid_t
#else
#define PID_T long
#endif
extern int tty,sptty;
extern int verbose;
extern int i_am_relay;
extern void quit(), error();
extern char x10_tty_aux[PATH_LEN + 1];
extern unsigned char alert_ack[];
extern CONFIG config;
extern CONFIG *configp;
extern char spoolfile[PATH_LEN + 1];
extern int is_ring( void );
int relay_ri_control( void );
extern int i_am_aux;
extern int heyu_parent;
static char saved_auxport[PATH_LEN + 1] = "";
static char saved_suffix[PATH_LEN + 1] = "";
static unsigned char saved_auxdev;
static char auxfilename[PATH_LEN + 1];
unsigned int rflastaddr[16];
unsigned int rftransceive[16][7];
unsigned int rfforward[16][7];
/*---------------------------------------------------------------+
| Set up tables determining whether received standard X10 RF |
| signals are to be transceived, forwarded, or ignored. |
+---------------------------------------------------------------*/
void configure_rf_tables ( void )
{
ALIAS *aliasp;
int hcode;
unsigned int mask;
unsigned long optflags;
int j, k;
for ( hcode = 0; hcode < 16; hcode++ ) {
#if 0
rflastaddr[hcode] = 0;
#endif
rflastaddr[hcode] = 1;
mask = 1 << hcode;
for ( k = 0; k < 7; k++ ) {
if ( configp->transceive & mask )
rftransceive[hcode][k] = 0xffff;
else
rftransceive[hcode][k] = 0;
if ( configp->rfforward & mask )
rfforward[hcode][k] = 0xffff;
else
rfforward[hcode][k] = 0;
}
}
if ( !(aliasp = configp->aliasp) )
return;
j = 0;
while ( aliasp[j].line_no > 0 ) {
hcode = hc2code(aliasp[j].housecode);
optflags = aliasp[j].optflags & (MOPT_TRANSCEIVE | MOPT_RFFORWARD | MOPT_RFIGNORE) ;
if ( optflags == 0 || aliasp[j].modtype < 0 ) {
j++;
continue;
}
for ( k = 2; k < 6; k++ ) {
if ( (aliasp[j].flags & (unsigned long)(1 << k)) == 0 )
continue;
if ( optflags & MOPT_TRANSCEIVE ) {
rftransceive[hcode][k] |= aliasp[j].unitbmap;
rfforward[hcode][k] &= ~aliasp[j].unitbmap;
}
else if ( optflags & MOPT_RFFORWARD ) {
rftransceive[hcode][k] &= ~aliasp[j].unitbmap;
rfforward[hcode][k] |= aliasp[j].unitbmap;
}
else if ( optflags & MOPT_RFIGNORE ) {
rftransceive[hcode][k] &= ~aliasp[j].unitbmap;
rfforward[hcode][k] &= ~aliasp[j].unitbmap;
}
}
j++ ;
}
return ;
}
/*---------------------------------------------------------------+
| Close the aux serial port and unlink the port and daemon lock |
| files. |
+---------------------------------------------------------------*/
void aux_shutdown_port ( char *saved_auxport )
{
char filename[PATH_LEN + 1];
extern int tty_aux;
extern int munlock(char *);
if (tty_aux >= 0 ) {
close(tty_aux);
tty_aux = -1;
}
if ( saved_auxport[0] != '\0' ) {
munlock(saved_auxport);
sprintf(filename, "%s%s", AUXFILE, saved_suffix);
munlock(filename);
}
if ( configp->ttyaux != '\0' ) {
munlock(configp->ttyaux);
sprintf(filename, "%s%s", AUXFILE, configp->suffixaux);
munlock(filename);
}
return;
}
/*---------------------------------------------------------------+
| Aux local setup. |
+---------------------------------------------------------------*/
void aux_local_setup ( void )
{
extern int ttylock(), lock_for_write(), munlock();
extern int aux_baudrate ( void );
extern int setup_tty_aux ( int, int );
int configure_rf_receiver(unsigned char);
extern int tty_aux;
int msglevel = 0;
if ( (saved_auxport[0] != '\0') &&
((strcmp(saved_auxport, configp->ttyaux) != 0) ||
(configp->auxdev != saved_auxdev)) ) {
syslog(LOG_ERR, "Shutting down aux port %s\n", saved_auxport);
aux_shutdown_port(saved_auxport);
saved_auxport[0] = '\0';
saved_auxdev = 0;
msglevel = 1;
millisleep(100);
}
if ( saved_auxport[0] == '\0' ) {
sprintf(auxfilename, "%s%s", AUXFILE, configp->suffixaux);
if ( ttylock(auxfilename) < 0 ) {
aux_shutdown_port(saved_auxport);
syslog(LOG_ERR, "Could not set up the heyu_aux lock-\n");
exit(0);
}
if ( setup_tty_aux(aux_baudrate(), 1) != 0 || tty_aux < 0 ) {
syslog(LOG_ERR, "Unable to open tty_aux\n");
aux_shutdown_port(saved_auxport);
exit(1);
}
if ( msglevel > 0 ) {
syslog(LOG_ERR, "Opening aux port %s\n", configp->ttyaux);
}
}
strncpy2(saved_auxport, configp->ttyaux, PATH_LEN);
strncpy2(saved_suffix, configp->suffixaux, PATH_LEN);
saved_auxdev = configp->auxdev;
configure_rf_tables();
return;
}
void aux_daemon_quit ( int signo )
{
syslog(LOG_ERR, "interrupt received.\n");
aux_shutdown_port(saved_auxport);
exit(0);
}
/*---------------------------------------------------------------+
| Start the daemon for the auxilliary input device. |
+---------------------------------------------------------------*/
int c_start_aux ( char *tty_auxname )
{
long child;
PID_T pid;
int in_sync;
int first_byte;
char spoolfilename[PATH_LEN + 1];
char relayfilename[PATH_LEN + 1];
char writefilename[PATH_LEN + 1];
struct stat file_buf;
extern char *argptr;
extern int ttylock(), lock_for_write(), munlock();
extern int aux_w800(), aux_rfxcomvl(), aux_mr26a();
extern int setup_tty_aux( int, int ), setup_sp_tty();
extern PID_T lockpid( char * );
PID_T was_locked;
int is_idle;
extern int sxread(int, unsigned char *, int, int);
first_byte = 1;
in_sync = 0;
was_locked = (PID_T)0;
is_idle = 0;
if ( !configp->ttyaux ) {
error("Serial port TTY_AUX not specified.");
exit(1);
}
/* set up the file names */
sprintf(spoolfilename, "%s%s", SPOOLFILE, configp->suffix);
sprintf(relayfilename, "%s%s", RELAYFILE, configp->suffix);
sprintf(writefilename, "%s%s", WRITEFILE, configp->suffix);
sprintf(auxfilename, "%s%s", AUXFILE, configp->suffixaux);
spoolfile[0] = '\0';
strcat(spoolfile, SPOOLDIR);
if ( spoolfile[strlen(spoolfile) - 1] != '/' )
strcat(spoolfile, "/");
if ( stat(spoolfile, &file_buf) < 0 ) {
char tmpbuf[sizeof(spoolfile) + 100];
sprintf(tmpbuf, "The directory %s does not exist or is not writable.",
spoolfile);
error(tmpbuf);
}
strcat(spoolfile, spoolfilename);
/* is an aux daemon in place ? */
if ( lockpid(auxfilename) > (PID_T)1) {
if ( verbose )
printf("There was already an aux daemon running (pid = %ld)\n",
(long)lockpid(auxfilename) );
return(-1); /* there was a valid aux relay running */
}
else {
if ( verbose )
printf("Aux lockfile not found - spawning heyu_aux.\n");
/* Spawn an aux process */
child = fork();
if ( child > 0 ) {
sleep(3); /* give child time to set up */
return(1); /* this is parent process */
}
if ( child < 0 ) { /* This is an error */
perror("I could not spawn heyu_aux process");
syslog(LOG_DAEMON | LOG_ERR, "I could not spawn heyu_aux process.\n");
quit();
}
}
/* from this point out, it should be the child. */
close(0);
close(1);
close(2);
strcpy(argptr, "heyu_aux");
pid = setsid(); /* break control terminal affiliation */
openlog( "heyu_aux", 0, LOG_DAEMON);
if ( pid == (PID_T)(-1) ) {
syslog(LOG_ERR, "aux setsid failed--\n");
quit(1);
}
else {
syslog(LOG_ERR, "aux setting up-\n");
}
/* Ok. We're alone now. */
while ( 1 ) {
aux_local_setup();
i_am_relay = 1; /* set flag to inhibit printing */
i_am_aux = 1;
setup_sp_tty();
putenv("HEYU_PARENT=AUXDEV");
heyu_parent = D_AUXDEV;
(void) signal(SIGINT, aux_daemon_quit);
(void) signal(SIGTERM, aux_daemon_quit);
(void) signal(SIGHUP, aux_daemon_quit);
if ( configp->auxdev == DEV_W800RF32 ) {
/* Process loop for data from the W800RF32 */
aux_w800();
}
else if ( configp->auxdev == DEV_RFXCOM32 ) {
/* Process loop for data from the RFXCOM in W800 mode */
aux_w800();
}
else if ( configp->auxdev == DEV_RFXCOMVL ) {
/* Process loop for data from the RFXCOM in variable length mode */
aux_rfxcomvl();
}
else {
/* Process loop for data from the MR26A */
aux_mr26a();
}
} /* End restart loop */
return 0;
}
/*---------------------------------------------------------------------+
| Verify that the aux daemon is running by checking for its lock |
| file. |
+---------------------------------------------------------------------*/
int check_for_aux ( void )
{
struct stat statbuf;
int retcode;
char lockpath[PATH_LEN + 1];
if ( !configp->ttyaux[0] )
return -1;
sprintf(lockpath, "%s/LCK..%s%s", LOCKDIR, AUXFILE, configp->suffixaux);
retcode = stat(lockpath, &statbuf);
return retcode;
}
void cleanup_aux ( void )
{
aux_shutdown_port(saved_auxport);
return;
}