-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathcm17a.c
469 lines (400 loc) · 14.2 KB
/
cm17a.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
/*----------------------------------------------------------------------------+
| |
| HEYU CM17A "Firecracker" Support |
| Copyright 2005, 2006 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 HAVE_FEATURE_CM17A /* Compile only if configured for CM17A */
#include <stdio.h>
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#include <ctype.h>
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#include "x10.h"
#include "process.h"
#ifdef SCO
#define _SVID3 /* required for correct termio handling */
#undef _IBCS2 /* conflicts with SVID3 */
#endif
#ifdef TIME_WITH_SYS_TIME
# include <sys/time.h>
# include <time.h>
#else
# ifdef HAVE_SYS_TIME_H
# include <sys/time.h>
# else
# include <time.h>
# endif
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <signal.h>
#ifdef HAVE_ASM_IOCTLS_H
#include <asm/ioctls.h>
#endif
#ifdef HAVE_LINUX_SERIAL_REG_H
#include <linux/serial_reg.h>
#endif
#ifdef HAVE_LINUX_SERIAL_H
#include <linux/serial.h>
#endif
#ifdef HAVE_SYS_IOCTL_H
#include <sys/ioctl.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_SYSLOG_H
#include <syslog.h>
#endif
#ifdef HAVE_SYS_TERMIOS_H
#include <sys/termios.h>
#endif
#ifdef HAVE_SYS_TERMIO_H
#include <sys/termio.h>
#endif
#ifdef HAVE_TERMIOS_H
#include <termios.h>
#endif
#ifdef HAVE_TERMIO_H
#include <termio.h>
#endif
#ifdef HAVE_SYS_IOCTL_H
#include <sys/ioctl.h>
#endif
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
extern int verbose;
extern int sptty;
extern int tty;
extern int ttylock(), munlock();
extern int i_am_relay;
extern CONFIG config;
extern CONFIG *configp;
#define FC_STANDBY (TIOCM_RTS | TIOCM_DTR)
#define FC_RESET ~FC_STANDBY
#define FC_ONE TIOCM_RTS
#define FC_ZERO TIOCM_DTR
#define HC_MASK 0xF000
#define FUNC_MASK 0x00B8
#define UNIT_MASK 0x0458
#define NOSW_MASK 0x0003
#define OLD_MASK 0x0800
#define NOTX10_MASK ~(HC_MASK|FUNC_MASK|UNIT_MASK|NOSW_MASK|OLD_MASK)
/* The CM17A will normally repeat its RF code */
/* transmission 5 times (configp->def_rf_bursts) */
/* at intervals of about 110 milliseconds */
/* (configp->rf_burst_spacing). */
/* By resetting the unit in one of the intervals */
/* between transmissions the number of bursts */
/* can be set from 1 through 5. */
/* It appears there is an effective window of */
/* about 40-50 milliseconds at the end of an */
/* interval when the reset will cut off */
/* subsequent bursts without interfering with */
/* the current burst. */
/* Time the reset at OFFSET milliseconds before */
/* the start of the next interval. */
#define OFFSET 10
/* On many systems the nanosleep() function will */
/* have a minimum resolution of 10 milliseconds */
/* and a minimum time of 20 milliseconds. */
/* The tweak (configp->rf_timer_tweak) attempts to */
/* compensate for this to keep within the */
/* effective window. */
/* RF Codes for X10 Housecodes (A-P) */
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";
/* RF Codes for X10 _encoded_ unit codes */
unsigned int rf_unit_code[16] = {
0x0440, 0x0040, 0x0008, 0x0408,
0x0448, 0x0048, 0x0000, 0x0400,
0x0450, 0x0050, 0x0018, 0x0418,
0x0458, 0x0058, 0x0010, 0x0410,
};
/* RF Function names for monitor */
char *rf_func_name[] = {
"fAllOff", "fLightsOn", "fOn", "fOff",
"fDim", "fBright", "fLightsOff",
"fdimbo", "freset", "fArb"
};
/* RF Function Codes: */
/* AllOff, LightsOn, On, Off, */
/* Dim, Bright, LightsOff */
unsigned int rf_func_code[] = {
0x0080, 0x0090, 0x0000, 0x0020,
0x0098, 0x0088, 0x00a0,
0x0000, 0x0000, 0x0000
};
/* Undocumented "No Switch" bits for TM751 and RR501 */
/* Transceivers. If set, the built-in appliance module */
/* relay will not respond to the On, Off, and AllOff RF */
/* signals, and will just quietly transceive them. */
unsigned int rf_nosw_code[] = {
0x0001, 0x0000, 0x0002, 0x0002,
0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000,
};
static unsigned long cm17a_loopcount;
static volatile unsigned long countdown;
static volatile int dummy = 1;
/*----------------------------------------------------------------------------+
| Microsecond timer using timing loop. |
+----------------------------------------------------------------------------*/
static void looptimer ( void )
{
countdown = cm17a_loopcount;
while (dummy && countdown--);
return;
}
/*----------------------------------------------------------------------------+
| Microsecond timer using standard timing functions. These don't have |
| sufficient resolution for fast RF mode in most Unix-like kernels. |
+----------------------------------------------------------------------------*/
static void stdtimer ( void )
{
long microsec = configp->cm17a_bit_delay;
#ifdef HAVE_NSLEEP
struct timestruc_t tspec;
tspec.tv_sec = microsec / 1000000L;
tspec.tv_nsec = 1000L * (microsec % 1000000L);
while ( nsleep( &tspec, &tspec ) == -1 );
return;
#else
#ifndef HAVE_NANOSLEEP
struct timeval tspec;
tspec.tv_sec = microsec / 1000000;
tspec.tv_usec = microsec % 1000000;
while ( usleep(tspec.tv_usec) == -1 );
#else
struct timespec tspec;
tspec.tv_sec = microsec / 1000000L;
tspec.tv_nsec = 1000L * (microsec % 1000000L);
while ( nanosleep( &tspec, &tspec ) == -1 );
#endif /* HAVE_NANOSLEEP */
return;
#endif
}
/*----------------------------------------------------------------------------+
| Delay after normal CM17A command. |
+----------------------------------------------------------------------------*/
void rf_post_delay(void)
{
millisleep(configp->rf_post_delay);
return;
}
/*----------------------------------------------------------------------------+
| Delay after 'farb' CM17A command |
+----------------------------------------------------------------------------*/
void rf_farb_delay(void)
{
millisleep(configp->rf_farb_delay);
return;
}
/*----------------------------------------------------------------------------+
| Delay after 'farw' CM17A command |
+----------------------------------------------------------------------------*/
void rf_farw_delay(void)
{
millisleep(configp->rf_farw_delay);
return;
}
/*----------------------------------------------------------------------------+
| Delay after 'flux' CM17A command |
+----------------------------------------------------------------------------*/
void rf_flux_delay( long msec )
{
millisleep(msec);
return;
}
/*----------------------------------------------------------------------------+
| Reset CM17A to the power-up state - with no log message. |
+----------------------------------------------------------------------------*/
int reset_cm17a_quiet ( void )
{
int status, retcode;
retcode = ioctl(tty, TIOCMGET, &status);
status &= FC_RESET;
retcode |= ioctl(tty, TIOCMSET, &status);
millisleep(10);
retcode = ioctl(tty, TIOCMGET, &status);
status = (status & FC_RESET) | FC_STANDBY;
retcode |= ioctl(tty, TIOCMSET, &status);
millisleep(500);
return retcode;
}
/*----------------------------------------------------------------------------+
| Reset CM17A to the power-up state. |
+----------------------------------------------------------------------------*/
int reset_cm17a( void )
{
send_x10state_command(ST_RESETRF, 0);
return reset_cm17a_quiet();
}
/*----------------------------------------------------------------------------+
| Actuate the CM17A by appropriately toggling the RTS and DTR lines. |
+----------------------------------------------------------------------------*/
int write_cm17a( unsigned int rfword, int bursts, unsigned char rfmode )
{
unsigned char buffer[16];
unsigned char data, mask;
int status, signal;
int i, j, k, retcode, groups, presleep;
void (*delay)();
if ( rfmode == RF_FAST && configp->timer_loopcount > 0 ) {
cm17a_loopcount =
configp->cm17a_bit_delay * (configp->timer_loopcount / 1000000L);
delay = looptimer;
}
else {
delay = stdtimer;
}
if ( rfmode == RF_FAST ) {
groups = (bursts - 1) / (int)configp->def_rf_bursts + 1;
bursts = (bursts - 1) % (int)configp->def_rf_bursts + 1;
presleep = (configp->rf_burst_spacing * configp->def_rf_bursts)
- OFFSET - configp->rf_timer_tweak;
}
else {
groups = 1;
bursts = min(bursts, (int)configp->def_rf_bursts);
presleep = 0;
}
buffer[0] = 0xD5;
buffer[1] = 0xAA;
buffer[2] = (rfword & 0xff00) >> 8;
buffer[3] = rfword & 0xff;
buffer[4] = 0xAD;
retcode = ioctl(tty, TIOCMGET, &status);
status |= FC_STANDBY;
retcode |= ioctl(tty, TIOCMSET, &status);
delay();
for ( i = 0; i < groups; i++ ) {
if ( i > 0 )
millisleep( presleep );
for ( j = 0; j < 5; j++ ) {
data = buffer[j];
mask = 0x80;
for ( k = 0; k < 8; k++ ) {
delay(); /* Put delay at beginning of loop */
signal = (data & mask) ? FC_ONE : FC_ZERO ;
#if 0
status &= FC_RESET;
status |= signal;
#endif
status = (status & FC_RESET) | signal;
retcode |= ioctl(tty, TIOCMSET, &status);
delay();
#if 0
status |= FC_STANDBY;
#endif
status = (status & FC_RESET) | FC_STANDBY;
retcode |= ioctl(tty, TIOCMSET, &status);
mask = mask >> 1;
}
}
}
/* Timed reset here limits the number of RF bursts */
if ( bursts > 0 ) {
millisleep((configp->rf_burst_spacing * bursts)
- OFFSET - configp->rf_timer_tweak);
status &= FC_RESET;
retcode |= ioctl(tty, TIOCMSET, &status);
millisleep(10);
status |= FC_STANDBY;
retcode |= ioctl(tty, TIOCMSET, &status);
millisleep(10);
}
return retcode;
}
/*----------------------------------------------------------------------------+
|
+----------------------------------------------------------------------------*/
void xlate_rf( unsigned char type, char **fname, unsigned int rfword,
char *hcp, int *unitp, unsigned char *nosw )
{
int j;
*fname = rf_func_name[type];
if ( type == 9 )
return;
*hcp = rf2hc[(rfword & HC_MASK) >> 12];
*nosw = rfword & NOSW_MASK;
rfword &= UNIT_MASK;
*unitp = 0;
for ( j = 0; j < 16; j++ ) {
if ( rfword == rf_unit_code[j] ) {
*unitp = code2unit(j);
break;
}
}
return;
}
/*----------------------------------------------------------------------------+
| Display the CM17A command in the monitor/logfile |
| |
| *** This really doesn't work very well, especially with multiple RF |
| commands. The uncertain delay between transmission of the RF and the |
| reporting of the received power line signal by the CM11A often results in |
| the RF signals not properly interleaved with the resulting power line |
| signals. It can be corrected only by setting an unreasonably long |
| post-delay. |
+----------------------------------------------------------------------------*/
int display_rf_xmit ( unsigned char type, unsigned int rfword, int bursts )
{
extern int sptty;
int ignoret;
static unsigned char template[10] = {
0xff,0xff,0xff,6,ST_COMMAND,ST_XMITRF,0,0,0,0};
if ( configp->disp_rf_xmit == NO )
return 0;
template[6] = type;
template[7] = (rfword & 0xFF00) >> 8;
template[8] = rfword & 0xFF;
template[9] = (unsigned char)bursts;
ignoret = write(sptty, template, 10);
return 0;
}
#else /* Stubs */
void xlate_rf( unsigned char type, char **fname, unsigned int rfword,
char *hcp, int *unitp, unsigned char *nosw ) {}
#endif /* End of HAVE_FEATURE_CM17A code */