forked from HeyuX10Automation/heyu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxpl.c
688 lines (573 loc) · 18.1 KB
/
xpl.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
/*
* xPL interface for Heyu
*
* Copyright (C) 2010, Janusz Krzysztofik <[email protected]>
*
*
* 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_LIBXPL
#include <poll.h>
#include <stdio.h>
#ifdef HAVE_SYSLOG_H
#include <syslog.h>
#endif
#ifdef HAVE_XPL_H
#include <xPL.h>
#endif
#include "x10.h"
#include "process.h"
#include "x10state.h"
static void xpl_log_basic(xPL_ServicePtr service, String type, String text)
{
static xPL_MessagePtr message;
if (!message) {
message = xPL_createBroadcastMessage(service,
xPL_MESSAGE_TRIGGER);
xPL_setSchema(message, "log", "basic");
} else {
xPL_clearMessageNamedValues(message);
}
fprintf(stderr, "%s %s log.basic %s: %s\n", datstrf(),
xPL_getServiceDeviceID(service), type, text);
fflush(stderr);
xPL_addMessageNamedValue(message, "type", type);
xPL_addMessageNamedValue(message, "text", text);
xPL_sendServiceMessage(service, message);
}
/*
* xpl_x10_security(): translate name=value pairs of an xPL message of type
* x10.security into a list of heyu sec_emu command line
* arguments and pass them to the sec_encode() function
* as if originated from signal_src.
*/
static void xpl_x10_security(xPL_ServicePtr service, xPL_MessagePtr message,
xPL_ObjectPtr user_value)
{
String command = xPL_getMessageNamedValue(message, "command");
String device = xPL_getMessageNamedValue(message, "device");
String type = xPL_getMessageNamedValue(message, "type");
String tamper = xPL_getMessageNamedValue(message, "tamper");
String delay = xPL_getMessageNamedValue(message, "delay");
String low_battery = xPL_getMessageNamedValue(message, "low-battery");
/*
* Fixed strings declared below will be strlower()ized unconditionally
* when passed to sec_encode(), so store them as statics, not constants.
*/
static char lobat[] = "lobat", sectamper[] = "sectamper", arm[] = "arm";
static char alert[] = "alert", clear[] = "clear";
static char sdawn[] = "sdawn", sdusk[] = "sdusk";
int argc = 3, typec, i, modtype = -1, err = 0;
unsigned int *signal_src = user_value;
struct xlate_vdata_st xlate_vdata;
String tmpptr, argv[6], *typev;
unsigned long ident;
char buf[80];
if (!command || !device) {
store_error_message("missing mandatory \"command\" or \"device\" argument");
goto bad;
}
ident = strtol(device, &tmpptr, 0);
if (*tmpptr != '\0') {
snprintf(buf, sizeof(buf), "invalid device ID '%s'", device);
store_error_message(buf);
goto bad;
}
/*
* Exception: handle RFXCOM receiver provided 24-bit IDs
* Note that we are not able to detect 24-bit 0x00hhhh values
*/
if (ident & 0xff0000)
ident = ((ident & 0xff0000) >> 16) | ((ident & 0xffff) << 8);
/* adaptation of x10.security commands / flags to Heyu sec_emu syntax */
if (tamper) {
command = sectamper;
} else if (xPL_strcmpIgnoreCase(command, "normal") == 0) {
command = clear;
} else if (xPL_strcmpIgnoreCase(command, "motion") == 0) {
command = alert;
} else if (xPL_strncmpIgnoreCase(command, "arm-", 4) == 0) {
argv[argc++] = arm;
command += 2;
memcpy(command, "sw", 2);
} else if (xPL_strncmpIgnoreCase(command, "lights-", 7) == 0) {
memcpy(command, "slights", 7);
} else if (xPL_strcmpIgnoreCase(command, "light") == 0) {
command = sdawn;
} else if (xPL_strcmpIgnoreCase(command, "dark") == 0) {
command = sdusk;
}
argv[argc++] = command;
if (low_battery && xPL_strcmpIgnoreCase(low_battery, "true") == 0)
argv[argc++] = lobat;
if (delay) {
snprintf(buf, sizeof(buf), "sw%s", delay);
argv[argc++] = buf;
}
if (!type) {
/* use module ID to retreive its type if known to Heyu */
unsigned char ucode;
int index = id2index(RF_SEC, ident, &ucode);
if (index >= 0) {
ALIAS *aliasp = &configp->aliasp[index];
modtype = aliasp->modtype;
if (modtype >= 0) {
memset(&xlate_vdata, '\0',
sizeof(struct xlate_vdata_st));
xlate_vdata.identp = &ident;
err = alt_parent_call(sec_encode,
signal_src - source_type,
argc, argv, &xlate_vdata,
aliasp->xlate_func, modtype);
if (err)
goto bad;
return;
}
}
snprintf(buf, sizeof(buf),
"alias for module ID '%s' not found, couldn't determine module type",
device);
store_error_message(buf);
goto bad;
}
/* Handle RFXCOM receiver provided multi-value "type" arguments */
tokenize(strdup(type), ",", &typec, &typev);
for (i = 0; i < typec; i++) {
if (*typev[i] == '\0')
continue;
/*
* Work around RFXCOM optional type SD90 false
* selection caused by a missing delay flag
*/
if (!delay && typec > 1 && strcmp(typev[i], "sd90") == 0) {
err = 1;
continue;
}
modtype = lookup_module_type(typev[i]);
if (modtype < 0)
continue;
memset(&xlate_vdata, '\0', sizeof(struct xlate_vdata_st));
xlate_vdata.identp = &ident;
err = alt_parent_call(sec_encode, signal_src - source_type,
argc, argv, &xlate_vdata,
module_xlate_func(modtype), modtype);
if (!err)
goto done;
}
if (!err) {
snprintf(buf, sizeof(buf), "unsupported module type '%s'",
type);
store_error_message(buf);
err = 1;
goto done;
}
if (delay)
goto done;
/* Guess potentially RFXCOM missing delay flag and retry for not SD90 */
tmpptr = strstr(type, "sd90");
if (!tmpptr)
strncpy(buf, "swmax", sizeof(buf));
else if (typec > 1)
strncpy(buf, "swmin", sizeof(buf));
else
goto done;
argv[argc++] = buf;
for (i = 0; i < typec; i++) {
if (*typev[i] == '\0')
continue;
if (strcmp(typev[i], "sd90") == 0)
continue;
modtype = lookup_module_type(typev[i]);
if (modtype < 0)
continue;
memset(&xlate_vdata, '\0', sizeof(struct xlate_vdata_st));
xlate_vdata.identp = &ident;
err = alt_parent_call(sec_encode, signal_src - source_type,
argc, argv, &xlate_vdata,
module_xlate_func(modtype), modtype);
if (!err)
goto done;
}
/*
* If the workaround failed, drop the guessed
* delay argument and try the SD90 if suggested
*/
if (tmpptr) {
argc--;
for (i = 0; i < typec; i++)
if (strcmp(typev[i], "sd90") == 0)
break;
modtype = lookup_module_type(typev[i]);
memset(&xlate_vdata, '\0', sizeof(struct xlate_vdata_st));
xlate_vdata.identp = &ident;
err = alt_parent_call(sec_encode, signal_src - source_type,
argc, argv, &xlate_vdata,
module_xlate_func(modtype), modtype);
}
done:
if (!err && typec > 1) {
fprintf(stderr,
"%s %s x10.security: module type \"%s\" selected out of '%s'\n",
datstrf(), xPL_getServiceDeviceID(service),
typev[i], type);
fflush(stderr);
}
free(typev[0]);
free(typev);
if (err) {
bad:
add_error_prefix("x10.security: ");
xpl_log_basic(service, "inf", error_message());
}
}
/*
* xpl_x10_basic(): translate name=value pairs of an xPL message of type
* x10.basic into a list of heyu command line arguments
* and pass them to the direct_command() function as if
* originated from signal_src.
*/
static void xpl_x10_basic(xPL_ServicePtr service, xPL_MessagePtr message,
xPL_ObjectPtr user_value)
{
String command = xPL_getMessageNamedValue(message, "command");
String device = xPL_getMessageNamedValue(message, "device");
String house = xPL_getMessageNamedValue(message, "house");
String level = xPL_getMessageNamedValue(message, "level");
String data1 = xPL_getMessageNamedValue(message, "data1");
String data2 = xPL_getMessageNamedValue(message, "data2");
unsigned int *signal_src = user_value;
int argc = 0, dimlevel, err;
static char _h[3] = "-a";
String argv[5];
if ((!command && !device) || (!device && !house) || (device && house) ||
(data1 && !data2) || (!data1 && data2) ||
(level && data1) || (!command && (level || data1))) {
store_error_message("missing or conflicting name=value data");
goto bad;
}
/* RFXCOM missing unit code workaround */
if (device && strlen(device) == 1 && *signal_src == RCVA) {
_h[1] = device[0];
device = _h;
}
if (command)
argv[argc++] = command;
else
argv[argc++] = "address";
if (data2)
argv[argc++] = data2;
if (device)
argv[argc++] = device;
else
argv[argc++] = house;
if (level) {
if (!xPL_strToInt(level, &dimlevel))
goto bad;
dimlevel = dimlevel * 21 / 100 + 1;
argv[argc++] = xPL_intToStr(dimlevel);
} else if (data1) {
argv[argc++] = data1;
} else if (*signal_src == RCVA && command &&
(xPL_strcmpIgnoreCase(command, "dim") == 0 ||
xPL_strcmpIgnoreCase(command, "bright") == 0)) {
/* RFXCOM missing dim/bright level workaround */
argv[argc++] = "1";
}
err = alt_parent_call(direct_command, signal_src - source_type, argc,
argv, CMD_RUN);
if (err) {
bad:
add_error_prefix("x10.basic: ");
xpl_log_basic(service, "inf", error_message());
}
}
static xPL_ObjectPtr xpl_select_msg_handler(String class, String type)
{
xPL_ObjectPtr handler = NULL;
/* Schema based message handler selection logic */
if (xPL_strcmpIgnoreCase(class, "x10") == 0) {
if (xPL_strcmpIgnoreCase(type, "basic") == 0)
handler = xpl_x10_basic;
else if (xPL_strcmpIgnoreCase(type, "security") == 0)
handler = xpl_x10_security;
}
/* End of message handler selection logic */
return handler;
}
static void xpl_reconf_listeners(xPL_ServicePtr service,
xPL_ObjectPtr signal_src)
{
Bool enabled = xPL_isServiceEnabled(service);
int count = xPL_getServiceConfigValueCount(service, "listener");
if (enabled)
xPL_setServiceEnabled(service, FALSE);
if (service->listenerCount > 2)
service->listenerCount = 2;
while (count > 0) {
String value = xPL_getServiceConfigValueAt(service, "listener",
--count);
String msg_type, schema_class, schema_type = NULL;
xPL_MessageType message_type;
xPL_ObjectPtr handler;
if (!value)
continue;
msg_type = strdup(value);
if (!msg_type) {
fprintf(stderr, "xpl_reconf_listeners(): out of memory, skipping listener %s\n",
value);
continue;
}
schema_class = strchr(msg_type, '.');
if (schema_class) {
*(schema_class++) = '\0';
schema_type = strchr(schema_class, '.');
}
if (schema_type)
*(schema_type++) = '\0';
if (strcmp(msg_type, "*") == 0) {
message_type = xPL_MESSAGE_ANY;
} else if (xPL_strcmpIgnoreCase(msg_type, "xpl-cmnd") == 0) {
message_type = xPL_MESSAGE_COMMAND;
} else if (xPL_strcmpIgnoreCase(msg_type, "xpl-stat") == 0) {
message_type = xPL_MESSAGE_STATUS;
} else if (xPL_strcmpIgnoreCase(msg_type, "xpl-trig") == 0) {
message_type = xPL_MESSAGE_TRIGGER;
} else {
fprintf(stderr, "xpl_reconf_listeners(): bad message type %s, skipping listener %s\n",
msg_type, value);
goto free;
}
handler = xpl_select_msg_handler(schema_class, schema_type);
if (handler)
xPL_addServiceListener(service, handler, message_type,
schema_class, schema_type, signal_src);
else
fprintf(stderr, "xpl_reconf_listeners(): schema %s.%s not supported, skipping listener %s\n",
schema_class, schema_type, value);
free:
free(msg_type);
}
if (enabled)
xPL_setServiceEnabled(service, TRUE);
}
#define xpl_add_configurable_array(service, config) \
xPL_addServiceConfigurable(service, config, xPL_CONFIG_OPTIONAL, 255)
static xPL_ObjectPtr xpl_setup_subservice(xPL_ServicePtr service,
xPL_ObjectPtr *user_value)
{
if (!xPL_isServiceConfigured(service)) {
xPL_ServiceFilterPtr fp = malloc(sizeof(xPL_ServiceFilter));
unsigned int *signal_src = *user_value;
if (fp) {
service->messageFilterList = fp;
service->filterAllocCount = 1;
memset(fp, 0, sizeof(xPL_ServiceFilter));
}
xpl_add_configurable_array(service, "listener");
switch (*signal_src) {
case RCVA:
xPL_addServiceConfigValue(service, "listener",
"xpl-trig.x10.security");
xPL_addServiceConfigValue(service, "listener",
"xpl-trig.x10.basic");
if (fp) {
xPL_addServiceConfigValue(service, "filter",
"xpl-trig.rfxcom.lan.*.*.*");
fp->matchOnMessageType = xPL_MESSAGE_TRIGGER;
fp->matchOnVendor = strdup("rfxcom");
fp->matchOnDeviceID = strdup("lan");
service->filterCount = 1;
}
break;
}
}
return xpl_reconf_listeners;
}
#define xpl_setup_service(name, instance, type, user_value) \
_xpl_setup_service(name, instance, xpl_setup_##type, user_value)
/*
* _xpl_setup_service() - a helper function for setting up xPL services.
* name the service name,
* configure optional initial configuration routine for the service;
* should accept a pointer to the service structure, and a pointer
* to an optional extra argument intended to be passed to the
* service reconfigure handler; may return a pointer to the
* service reconfiguration handler, if applicable,
* user_value optional extra argument passed to the service reconfiguration
* handler; can be modified by the configure routine;
* return value TRUE - success, FALSE - failure.
*/
static Bool _xpl_setup_service(String name, String instance,
xPL_ObjectPtr (*configure)(xPL_ServicePtr, xPL_ObjectPtr *),
xPL_ObjectPtr user_value)
{
xPL_ServicePtr service;
int instance_len = strlen(instance);
String config_file = malloc(strlen(XPLCONFDIR) + strlen("/") +
strlen(name) + instance_len + strlen(".xpl") + 1);
if (!config_file) {
fprintf(stderr, "xpl_setup_service(%s): out of memory\n", name);
return FALSE;
}
sprintf(config_file, "%s/%s%s.xpl", XPLCONFDIR, name, instance);
service = xPL_createConfigurableService("heyu", name, config_file);
free(config_file);
if (!service) {
fprintf(stderr, "xpl_setup_service(%s): create failed\n", name);
return FALSE;
}
if (!xPL_isServiceConfigured(service) && instance_len) {
/*
* For XML Plugin Files for xPL enabled Heyu to work as
* expected, we have to use a closed set of xPL device IDs,
* even if running several Heyu or xPL subservice instances.
* Then, the only way to distinguish among them after created
* and before configured for the first time is adjusting their
* default instance ID to match the instance argument provided.
*/
String instance_id = xPL_getServiceInstanceID(service);
int id_len = strlen(instance_id);
if (instance_len > id_len)
instance_len = id_len;
strncpy(instance_id + (id_len - instance_len), instance,
instance_len);
xPL_setServiceInstanceID(service, instance_id);
}
xPL_setServiceVersion(service, VERSION);
if (configure) {
/* Do custom startup configuration */
void (*config_handler)(xPL_ServicePtr, xPL_ObjectPtr) =
configure(service, &user_value);
if (config_handler) {
/* Set up custom runtime reconfiguration */
xPL_addServiceConfigChangedListener(service,
config_handler, user_value);
config_handler(service, user_value);
}
}
xPL_setServiceEnabled(service, TRUE);
return TRUE;
}
static xPL_ObjectPtr xpl_signal_src(String name)
{
int i;
for (i = D_CMDLINE; i <= D_AUXRCV; i++) {
if (xPL_strcmpIgnoreCase(name, (String) source_name[i]) != 0)
continue;
switch (i) {
static int err = -1;
case D_AUXRCV:
if (!(i_am_state || i_am_monitor))
goto err;
configure_rf_tables();
break;
default:
return NULL;
err:
fprintf(stderr, "xpl_signal_src(): signal source %s not valid for this Heyu daemon, skipping\n",
name);
return &err;
}
return (xPL_ObjectPtr) &source_type[i];
}
return NULL;
}
static void xpl_reconf_subservices(xPL_ServicePtr service,
xPL_ObjectPtr user_value)
{
int count = xPL_getServiceCount();
while (count > 1) {
xPL_ServicePtr subservice = xPL_getServiceAt(--count);
if (subservice)
xPL_releaseService(subservice);
}
count = xPL_getServiceConfigValueCount(service, "subService");
while (count > 0) {
String instance = xPL_getServiceConfigValueAt(service,
"subService", --count);
xPL_ObjectPtr signal_src;
static char name[5];
if (!instance)
continue;
/*
* Split the user defined subservice name
* into a 4 character Heyu signal source name
* and an optional instance modifier part.
*/
strncpy(name, instance, 4);
instance += strlen(name);
/* Use Heyu instance as a last resort */
if (!strlen(instance))
instance = optptr->dispsub;
signal_src = xpl_signal_src(name);
if (signal_src == NULL)
fprintf(stderr, "xpl_reconf_subservices(): service name %s not supported, skipping\n",
name);
else if (*((int *)signal_src) >= 0)
xpl_setup_service(name, instance, subservice,
signal_src);
}
}
static xPL_ObjectPtr xpl_setup_metaservice(xPL_ServicePtr service,
xPL_ObjectPtr *user_value)
{
void (*setup_io)(xPL_ServicePtr) = *user_value;
if (setup_io)
setup_io(service);
if (!xPL_isServiceConfigured(service)) {
xpl_add_configurable_array(service, "subService");
if ((i_am_state || i_am_monitor) && !configp->auxdev) {
xPL_addServiceConfigValue(service, "subService",
(String) source_name[D_AUXRCV]);
}
}
return xpl_reconf_subservices;
}
/*
* xpl_init(): initialize the xPL layer and set up xPL service(s)
* based on the Heyu daemon specific i_am_* flags.
*/
Bool xpl_init(void)
{
String name = NULL;
xPL_ObjectPtr setup_io = NULL;
/* Primary xPL service name and io_setup selection logic goes here */
if (i_am_state) {
name = "engine";
} else if (i_am_monitor) {
name = "monitor";
}
/* End of primary xPL service name and io_setup selection logic */
if (!name) {
fprintf(stderr, "xpl_init(): unsupported Heyu daemon\n");
return FALSE;
}
if (!xPL_initialize(xPL_getParsedConnectionType())) {
fprintf(stderr, "xpl_init(): unable to initialize xPL\n");
return FALSE;
}
/*
* Take an optional xPL service instance ID
* modifier from the Heyu daemon instace number
*/
if (xpl_setup_service(name, optptr->dispsub, metaservice, setup_io))
return TRUE;
xPL_shutdown();
return FALSE;
}
#endif