-
Notifications
You must be signed in to change notification settings - Fork 27
/
setclock.c
385 lines (320 loc) · 9.67 KB
/
setclock.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
/*
* Copyright 1996, 1997, 1998, 1999, 2002 by Daniel B. Suthers,
* Pleasanton Ca. 94588 USA
* E-MAIL [email protected]
*
*/
/*
* Original code written for the cp290 series by Larry Campbell,
* 73 Concord Street, Maynard MA 01754 USA. (maynard!campbell)
*/
/*
* Code modified for Heyu ver 2 by Charles Sullivan
*/
/*
* 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
#include <time.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include "x10.h"
#ifdef HAVE_SYSLOG_H
#include <syslog.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#include <ctype.h>
#include "process.h"
extern int tty;
extern int sptty;
extern char *wday_name[7];
extern char *month_name[12];
extern int i_am_relay;
extern int x10_housecode;
extern int verbose;
extern unsigned int jul();
extern CONFIG config;
extern CONFIG *configp;
/* ARGSUSED */
int c_setclock ( int argc, char *argv[] )
{
unsigned char data[9];
unsigned char buf[3];
char msgbuf[1000];
unsigned int n;
unsigned int clear;
struct tm *tp;
time_t dtime;
extern int usage(), xwrite(), xread(), exread(), sxread(), check4poll(), get_status();
extern void wait_next_tick();
extern struct tm *legal_to_cm11a();
int code;
unsigned char cksum;
int ignoret;
if ( invalidate_for_cm10a() != 0 )
return -1;
/* Argc == 1 means use system time.
* Argc == 2 means parse a date from the input or reset the clock.
*/
if ( argc < 1 || argc > 3 ) {
if ( argc != 7 )
usage(E_2MANY);
}
clear = 0;
if ( i_am_relay != 1 )
wait_next_tick(); /* System clock rolls over to next second */
dtime = time(NULL);
tp = localtime(&dtime);
if ( argc == 2 ) {
if ( strcmp( "reset", argv[1]) == 0 ) {
clear = 1;
}
}
if ( argc == 3 ) {
/* must be date information. Load it in the structure
* and then remake the structure with the right time.
* (In Heyu 1, entered data was taken to be Standard Time
* but is now taken to be Legal Time.)
*/
if ( sscanf(argv[2], "%2d%2d%2d%2d%2d", &tp->tm_mon, &tp->tm_mday,
&tp->tm_hour, &tp->tm_min, &tp->tm_sec) != 5 ) {
printf(" Bad argument. Expected %s %s MMDDhhmmss\n", argv[0], argv[1]);
return(-1);
}
tp->tm_mon -= 1; /* tm month is 0 relative */
tp->tm_isdst = -1;
dtime = mktime(tp); /* reset day of week, julian */
if ( dtime < 1 ) {
printf("An error was encountered parsing the input %s\n", argv[2]);
return(-1);
}
}
/*
* Translate Legal Time into CM11a clock time per the
* X10 Record File, if it exists, or to Standard Time
* if it doesn't.
*/
tp = legal_to_cm11a( &dtime );
data[0] = 0x9b; /* CM11A timer download code */
data[1] = tp->tm_sec ;
data[2] = tp->tm_min + (((tp->tm_hour) %2) * 60 ) ; /* minutes 0 - 119 */
data[3] = tp->tm_hour / 2 ; /* hour / 2 0 - 11 */
data[4] = tp->tm_yday % 256 ; /* mantisa of julian date */
data[5] = ((tp->tm_yday / 256 ) << 7); /* radix of julian date */
data[5] |= (0x01 << (tp->tm_wday)); /* bits 0-6 = single bit mask day */
/* of week ( smtwtfs ) */
data[6] = (x10_housecode << 4);
data[6] |= clear;
if ( verbose ) {
sprintf(msgbuf, "would send %02x %02x %02x %02x %02x %02x %02x\n",
data[0], data[1], data[2], data[3], data[4], data[5],
data[6]);
if ( i_am_relay == 1 )
syslog(LOG_ERR, "%s", msgbuf);
else
fprintf(stderr, "%s", msgbuf);
}
/* Reset the timed macro execution timestamp */
set_macro_timestamp(0);
cksum = checksum(data + 1, 6);
code = 0;
if ( i_am_relay ) {
ignoret = write(tty, (char *) data, 7);
n = sxread(tty, buf, 1, 1);
}
else {
xwrite(tty, (char *) data, 7);
n = exread(sptty, buf, 1, 1);
}
if ( n != 1 )
code |= 0x10;
else if ( buf[0] != cksum )
code |= 0x01;
if ( i_am_relay ) {
ignoret = write(tty, "\0", 1); /* WRMI */
n = sxread(tty, buf, 1, 1);
}
else {
xwrite(tty, "\0", 1); /* WRMI */
n = exread(sptty, buf, 1, 1);
}
if ( n != 1 )
code |= 0x20;
else if ( buf[0] != 0x55 )
code |= 0x02;
if ( argc == 2 || argc == 3 ) {
check4poll(0,1); /* zero means to discard data */
get_status();
if ( clear == 0) {
if ( i_am_relay != 1 )
fprintf(stderr, "CM11A clock set to %s, Day %03d, %02d:%02d:%02d (Standard Time)\n",
wday_name[tp->tm_wday], tp->tm_yday, tp->tm_hour, tp->tm_min, tp->tm_sec);
}
else {
if ( i_am_relay != 1 )
printf("CM11A house code set to %c\n", code2hc( x10_housecode));
}
}
return(0);
}
/* Set clock without regard to the x10record file */
int c_setclockraw ( int argc, char *argv[] )
{
unsigned char data[9];
unsigned char buf[3];
char msgbuf[1000];
unsigned int n;
unsigned int clear;
struct tm *tp;
time_t dtime;
int was_tm_isdst;
extern int usage(), xwrite(), xread(), exread(), sxread(), check4poll(), get_status();
extern void wait_next_tick();
int code, nwrite;
unsigned char cksum;
if ( invalidate_for_cm10a() != 0 )
return 1;
/* Argc == 1 means use system time.
* Argc == 2 means parse a date from the input or reset the clock.
*/
if ( argc < 1 || argc > 3 ) {
if ( argc != 7 )
usage(E_2MANY);
}
clear = 0;
if ( i_am_relay != 1 )
wait_next_tick(); /* System clock rolls over to next second */
dtime = time(NULL);
tp = localtime(&dtime);
if ( argc == 2 ) {
if( strcmp( "reset", argv[1]) == 0 ) {
clear = 1;
}
}
if ( argc == 3) {
/* must be date information. Load it in the structure
* and then remake the structure with the right time.
*/
if ( sscanf(argv[2], "%2d%2d%2d%2d%2d", &tp->tm_mon, &tp->tm_mday,
&tp->tm_hour, &tp->tm_min, &tp->tm_sec) != 5) {
printf(" Bad argument. Expected %s %s MMDDhhmmss\n", argv[0], argv[1]);
return(-1);
}
tp->tm_mon -= 1; /* tm month is 0 relative */
was_tm_isdst = tp->tm_isdst; /* save the current DST flag */
dtime = mktime(tp); /* reset day of week, julian */
if ( dtime < 1 ) {
printf("An error was encountered parsing the input %s\n", argv[2]);
return(-1);
}
/* we want absolute time, but mktime will reset the hour as necessary,
* to match daylight savings.
* we undo that here.
*/
if ( tp->tm_isdst != was_tm_isdst ) {
if ( was_tm_isdst == 0 ) {
dtime -= 3600; /* subtract an hour*/
}
else {
dtime += 3600; /* add an hour*/
}
tp = localtime(&dtime);
}
}
data[0] = 0x9b; /* CM11A timer download code */
data[1] = tp->tm_sec ;
data[2] = tp->tm_min + (((tp->tm_hour) %2) * 60 ) ; /* minutes 0 - 119 */
data[3] = tp->tm_hour / 2 ; /* hour / 2 0 - 11 */
data[4] = tp->tm_yday % 256 ; /* mantisa of julian date */
data[5] = ((tp->tm_yday / 256 ) << 7); /* radix of julian date */
data[5] |= (0x01 << (tp->tm_wday)); /* bits 0-6 = single bit mask day */
/* of week ( smtwtfs ) */
data[6] = (x10_housecode << 4);
data[6] |= clear;
if ( verbose ) {
sprintf(msgbuf, "would send %02x %02x %02x %02x %02x %02x %02x\n",
data[0], data[1], data[2], data[3], data[4], data[5],
data[6]);
if ( i_am_relay == 1 )
syslog(LOG_ERR, "%s", msgbuf);
else
fprintf(stderr, "%s", msgbuf);
}
cksum = checksum(data + 1, 6);
code = 0;
nwrite = xwrite(tty, (char *) data, 7);
if ( verbose & !i_am_relay )
fprintf(stderr, "%d bytes written.\n", nwrite);
if ( i_am_relay )
n = sxread(tty, buf, 1, 1);
else
n = exread(sptty, buf, 1, 1);
if ( n != 1 )
code |= 0x10;
else if ( buf[0] != cksum )
code |= 0x01;
xwrite(tty, "\0", 1); /* WRMI */
if ( i_am_relay )
n = sxread(tty, buf, 1, 1);
else
n = exread(sptty, buf, 1, 1);
if ( n != 1 )
code |= 0x20;
else if ( buf[0] != 0x55 )
code |= 0x02;
if ( argc == 2 || argc == 3 ) {
check4poll(0,1); /* zero means to discard data */
get_status();
if( clear == 0) {
if ( i_am_relay != 1 )
fprintf(stderr, "CM11A clock set to %s, %d:%02d\n",
wday_name[tp->tm_wday], tp->tm_hour, tp->tm_min);
}
else {
if ( i_am_relay != 1 )
(void) printf("CM11A house code set to %c\n", code2hc( x10_housecode));
}
}
return(0);
}
/* Stop power-fail update polling without */
/* resetting clock. (For clock testing purposes) */
int c_stop_cm11a_poll ( int argc, char *argv[] )
{
unsigned char data[3];
extern int xwrite(), check4poll();
int ignoret;
data[0] = 0x9b;
if ( i_am_relay ) {
ignoret = write(tty, (char *)data, 1);
}
else {
xwrite(tty, (char *)data, 1);
}
if ( !i_am_relay )
check4poll(0,1); /* zero means to discard data */
/* Allow CM11A to recover */
millisleep(SETCLOCK_DELAY);
return 0;
}