-
Notifications
You must be signed in to change notification settings - Fork 2
/
x10state.c
355 lines (316 loc) · 9.62 KB
/
x10state.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
/*
* Copyright 2010-2011 Brian Uechi <[email protected]>
*
* This file is part of mochad.
*
* mochad 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.
*
* mochad 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 mochad. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdarg.h>
#include <stdlib.h>
#include <time.h>
#include "global.h"
#include "x10state.h"
#include "decode.h"
/* 16 house codes and 16 unit codes = 256 devices
* For normal (non-security) X10 devices.
*/
typedef unsigned char houseunitaddrs_t[16][16];
/* For X10 security sensors such as MS10A motion sensor and DS10A
* door/window sensor
*/
typedef struct _x10secsensor {
unsigned long secaddr;
time_t lastupdate;
unsigned char secaddr8; /* 0 17 bit addr, 1 8 bit addr */
unsigned char sensorstatus;
} x10secsensor_t;
static x10secsensor_t X10sensors[512];
static unsigned int X10sensorcount = 0;
/* 0x00 = unknown, '1' = ON, '0' = OFF */
static houseunitaddrs_t HouseUnitState;
/* 0x00 = not selected, '1' = selected */
static houseunitaddrs_t HouseUnitSelected;
/* 0..255 dim as set by xdim command */
static houseunitaddrs_t HouseUnitDim;
/* house/unit/func state per house code
* 0 = house/unit code, 1 = house/function */
static int X10protostate[16];
/* HouseUnitSelect and X10protostate are needed because of the following case.
* pl b3 # Select b3
* pl b4 # Select b4
* pl b on # Turn on b3 and b4
*/
void hua_sec_init(void)
{
memset(HouseUnitState, 0, sizeof(HouseUnitState));
memset(HouseUnitSelected, 0, sizeof(HouseUnitSelected));
memset(HouseUnitDim, 0, sizeof(HouseUnitDim));
memset(X10protostate, 0, sizeof(X10protostate));
memset(X10sensors, 0, sizeof(X10sensors));
X10sensorcount = 0;
}
#define issecfunc(x) (((x & 0xF0) == 0x80) || ((x & 0xF0) == 0x00))
/* Remember RF security event */
void hua_sec_event(unsigned char *secaddr, unsigned int funcint,
unsigned int secaddr8)
{
int i;
unsigned long secaddr32;
x10secsensor_t *sen;
secaddr32 = (secaddr[0] << 16) | (secaddr[1] << 8) | secaddr[2] ;
/* dbprintf("secaddr32 %X func %X issecfunc %d\n",
secaddr32, funcint, issecfunc(funcint)); */
for (i = 0; i < X10sensorcount; i++) {
sen = &X10sensors[i];
if (secaddr32 == sen->secaddr) {
sen->sensorstatus = funcint;
//if ((funcint & (1<<7)) == 0) {
sen->lastupdate = time(NULL);
//}
return;
}
}
/* Add new device */
if ((X10sensorcount + 1) >= (sizeof(X10sensors)/sizeof(x10secsensor_t)))
return;
// if (issecfunc(funcint)) {
sen = &X10sensors[X10sensorcount];
sen->secaddr = secaddr32;
sen->secaddr8 = secaddr8;
sen->sensorstatus = funcint;
sen->lastupdate = time(NULL);
X10sensorcount++;
// }
}
static void hua_dbprint(void)
{
int h, u;
char buf[4096];
strcpy(buf, "Selected:");
for (h = 0; h < 16; h++) {
for (u = 0; u < 16; u++) {
if (HouseUnitSelected[h][u])
snprintf(buf+strlen(buf), sizeof(buf)-strlen(buf), "%c%d,", h+'A', u+1);
}
}
strcat(buf, "\n");
dbprintf(buf);
strcpy(buf, "State: ");
for (h = 0; h < 16; h++) {
for (u = 0; u < 16; u++) {
if (HouseUnitState[h][u])
snprintf(buf+strlen(buf), sizeof(buf)-strlen(buf), "%c%d %c,", h+'A', u+1,
HouseUnitState[h][u]);
}
}
strcat(buf, "\n");
dbprintf(buf);
}
static void hua_init(int house)
{
memset(&HouseUnitSelected[house], 0, sizeof(HouseUnitSelected)/16);
}
unsigned char hua_getstatus(int house, int unit)
{
return HouseUnitState[house][unit];
}
unsigned char hua_getstatus_xdim(int house, int unit)
{
return HouseUnitDim[house][unit];
}
void hua_setstatus_xdim(int house, int unit, int xdim)
{
/* dbprintf("%s(%d,%d)\n", __func__, house, xdim); */
hua_init(house);
HouseUnitSelected[house][unit] = '1';
X10protostate[house] = 1;
HouseUnitDim[house][unit] = xdim;
if (xdim > 0)
HouseUnitState[house][unit] = '1';
else
HouseUnitState[house][unit] = '0';
}
void hua_add(int house, int unit)
{
switch (X10protostate[house]) {
case 0:
HouseUnitSelected[house][unit] = '1';
break;
case 1:
hua_init(house);
HouseUnitSelected[house][unit] = '1';
X10protostate[house] = 0;
break;
default:
dbprintf("Invalid state\n");
}
// hua_dbprint();
}
static void hua_func_all(int house, unsigned char func)
{
int u;
X10protostate[house] = 1;
for (u = 0; u < 16; u++) {
HouseUnitState[house][u] = func;
if (func == '1')
HouseUnitDim[house][u] = 63;
else
HouseUnitDim[house][u] = 0;
}
// hua_dbprint();
}
void hua_func_all_on(int house)
{
hua_func_all(house, '1');
}
void hua_func_all_off(int house)
{
hua_func_all(house, '0');
}
static void hua_func(int house, unsigned char func)
{
int u;
/* dbprintf("%s(%d,%d)\n", __func__, house, func); */
X10protostate[house] = 1;
for (u = 0; u < 16; u++) {
if (HouseUnitSelected[house][u]) {
HouseUnitState[house][u] = func;
if (func == '1')
HouseUnitDim[house][u] = 63;
else
HouseUnitDim[house][u] = 0;
}
}
// hua_dbprint();
/* dbprintf("%s exit\n", __func__); */
}
void hua_func_on(int house)
{
hua_func(house, '1');
}
void hua_func_off(int house)
{
hua_func(house, '0');
}
int hua_getstatus_sec(int rf8bitaddr, unsigned long rfaddr)
{
x10secsensor_t *sen;
int sensor;
dbprintf("hua_getstatus_sec(%d,%X)\n", rf8bitaddr, rfaddr);
for (sensor = 0; sensor < X10sensorcount; sensor++) {
sen = &X10sensors[sensor];
dbprintf("hua_getstatus_sec addr8 %d addr %X status %X\n",
sen->secaddr8, sen->secaddr, sen->sensorstatus);
if (rf8bitaddr && sen->secaddr8 && (rfaddr == sen->secaddr)) {
switch (sen->sensorstatus) {
case 0x04: return 1; /* alert */
case 0x84: return 0; /* normal */
}
}
else if (!rf8bitaddr && !sen->secaddr8 && (rfaddr == sen->secaddr)) {
switch (sen->sensorstatus) {
case 0x00:
case 0x01:
case 0x04:
case 0x05:
case 0x0C:
case 0x0D:
return 1; /* alert */
case 0x80:
case 0x81:
case 0x84:
case 0x85:
case 0x8C:
case 0x8D:
return 0; /* normal */
}
}
}
return -1;
}
static int cmpX10sensor(const void *e1, const void *e2)
{
const x10secsensor_t *sen1 = e1, *sen2 = e2;
return sen1->secaddr - sen2->secaddr;
}
void hua_show(int fd)
{
int h, u;
char buf[2048];
int labeldone;
int sensor;
x10secsensor_t *sen;
sockprintf(fd, "Device selected\n");
for (h = 0; h < 16; h++) {
labeldone = 0;
buf[0] = '\0';
for (u = 0; u < 16; u++) {
if (HouseUnitSelected[h][u]) {
if (!labeldone) {
labeldone = 1;
snprintf(buf+strlen(buf), sizeof(buf)-strlen(buf),
"House %c: %d", h+'A', u+1);
}
else
snprintf(buf+strlen(buf), sizeof(buf)-strlen(buf),
",%d", u+1);
}
}
if (labeldone) {
strcat(buf, "\n");
sockprintf(fd, buf, strlen(buf));
}
}
sockprintf(fd, "Device status\n");
for (h = 0; h < 16; h++) {
labeldone = 0;
buf[0] = '\0';
for (u = 0; u < 16; u++) {
if (HouseUnitState[h][u]) {
if (!labeldone) {
labeldone = 1;
snprintf(buf+strlen(buf), sizeof(buf)-strlen(buf),
"House %c: %d=%c", h+'A', u+1, HouseUnitState[h][u]);
}
else
snprintf(buf+strlen(buf), sizeof(buf)-strlen(buf),
",%d=%c", u+1, HouseUnitState[h][u]);
}
}
if (labeldone) {
strcat(buf, "\n");
sockprintf(fd, buf, strlen(buf));
}
}
sockprintf(fd, "Security sensor status\n");
qsort(X10sensors, X10sensorcount, sizeof(X10sensors[0]), cmpX10sensor);
for (sensor = 0; sensor < X10sensorcount; sensor++) {
time_t deltat, mins;
const char *message;
sen = &X10sensors[sensor];
deltat = time(NULL) - sen->lastupdate;
mins = deltat / 60;
deltat = deltat - (mins * 60);
if (sen->secaddr8)
message = findSecRemoteKeyName(sen->sensorstatus);
else
message = findSecEventName(sen->sensorstatus);
sockprintf(fd, "Sensor addr: %06X Last: %02d:%02d %s \n", sen->secaddr,
mins, deltat, message);
}
sockprintf(fd, "End status\n");
}