forked from darconeous/ge-rs232
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathge-system-node.c
1604 lines (1448 loc) · 43.7 KB
/
ge-system-node.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
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <smcp/assert_macros.h>
#include <stdio.h>
#include "ge-rs232.h"
#include "ge-system-node.h"
#include <string.h>
#include <stdlib.h>
#include <smcp/smcp.h>
#include <smcp/smcp-node.h>
#include <smcp/smcp-pairing.h>
#include <poll.h>
#include <termios.h>
#include <stdarg.h>
#include <fcntl.h>
#define GE_RS232_MAX_ZONES (96)
#define GE_RS232_MAX_PARTITIONS (6)
#define GE_RS232_MAX_SCHEDULES (16)
#define USE_SYSLOG 1
#if USE_SYSLOG
#include <syslog.h>
#endif
#define LOG_LEVEL_EMERGENCY (0)
#define LOG_LEVEL_ALERT (1)
#define LOG_LEVEL_CRITICAL (2)
#define LOG_LEVEL_ERROR (3)
#define LOG_LEVEL_WARNING (4)
#define LOG_LEVEL_NOTICE (5)
#define LOG_LEVEL_INFO (6)
#define LOG_LEVEL_DEBUG (7)
int current_log_level = LOG_LEVEL_INFO;
void log_msg(int level,const char* format, ...) {
va_list ap;
if(level>current_log_level)
return;
va_start(ap, format);
#if USE_SYSLOG
// static bool did_start_syslog;
// if(!did_start_syslog) {
// openlog("ge-rs232",LOG_PERROR|LOG_CONS,LOG_DAEMON);
// did_start_syslog = true;
// }
vsyslog(level,format,ap);
#else
char* formatted = NULL;
struct timeval tp;
gettimeofday(&tp, NULL);
vasprintf(&formatted, format, ap);
if(formatted) {
struct tm tmv;
localtime_r((time_t *)&(tp.tv_sec), &tmv);
fprintf(stderr,"[%d] %04d-%02d-%02d %02d:%02d:%02d%s %s\n",
level,
tmv.tm_year+1900,
tmv.tm_mon,
tmv.tm_mday,
tmv.tm_hour,
tmv.tm_min,
tmv.tm_sec,
tmv.tm_zone,
formatted
);
free(formatted);
}
#endif
}
typedef struct {
smcp_t smcp;
struct smcp_async_response_s async_response;
ge_rs232_status_t status;
smcp_transaction_t transaction;
} panel_response_context;
static smcp_status_t
async_response_ack_handler(int statuscode, panel_response_context* context) {
struct smcp_async_response_s* async_response = &context->async_response;
smcp_finish_async_response(async_response);
free(context);
return SMCP_STATUS_OK;
}
static smcp_status_t
resend_async_response(panel_response_context *context) {
smcp_status_t ret = 0;
struct smcp_async_response_s* async_response = &context->async_response;
if(context->status == GE_RS232_STATUS_OK) {
ret = smcp_outbound_begin_response(COAP_RESULT_204_CHANGED);
} else {
ret = smcp_outbound_begin_response(COAP_RESULT_500_INTERNAL_SERVER_ERROR);
}
require_noerr(ret,bail);
ret = smcp_outbound_set_async_response(async_response);
require_noerr(ret,bail);
if(context->status == GE_RS232_STATUS_NAK)
smcp_outbound_set_content_formatted("NAK");
else if(context->status == GE_RS232_STATUS_TIMEOUT)
smcp_outbound_set_content_formatted("ACK-TIMEOUT");
ret = smcp_outbound_send();
require_noerr(ret,bail);
if(ret) {
assert_printf(
"smcp_outbound_send() returned error %d(%s).\n",
ret,
smcp_status_to_cstr(ret)
);
goto bail;
}
bail:
return ret;
}
void got_panel_response(void* c, ge_rs232_status_t status) {
panel_response_context* context = c;
if(!context)
return;
context->status = status;
context->transaction = NULL;
context->transaction = smcp_transaction_init(
context->transaction,
0,
(void*)&resend_async_response,
(void*)&async_response_ack_handler,
c
);
smcp_transaction_begin(
context->smcp,
context->transaction,
5*1000 // Retry for five seconds.
);
}
static panel_response_context* new_panel_response_context() {
panel_response_context* ret = calloc(sizeof(panel_response_context),1);
smcp_start_async_response(&ret->async_response,0);
ret->smcp = smcp_get_current_instance();
return ret;
}
static const uint8_t refresh_equipment_msg[] = { GE_RS232_ATP_EQUIP_LIST_REQUEST };
static const uint8_t dynamic_data_refresh_msg[] = { GE_RS232_ATP_DYNAMIC_DATA_REFRESH };
ge_rs232_status_t refresh_equipment_list(ge_queue_t qinterface,
void (*finished)(void* context,ge_rs232_status_t status),
void* context
) {
return ge_queue_message(qinterface,refresh_equipment_msg,sizeof(refresh_equipment_msg),finished,context);
}
ge_rs232_status_t dynamic_data_refresh(ge_queue_t qinterface,
void (*finished)(void* context,ge_rs232_status_t status),
void* context
) {
return ge_queue_message(qinterface,dynamic_data_refresh_msg,sizeof(dynamic_data_refresh_msg),finished,context);
}
ge_rs232_status_t send_keypress(ge_queue_t qinterface,uint8_t partition, uint8_t area, char* keys,
void (*finished)(void* context,ge_rs232_status_t status),
void* context
) {
uint8_t msg[50] = {
GE_RS232_ATP_KEYPRESS,
partition, // Partition
area, // Area
};
uint8_t len = 3;
for(;*keys;keys++) {
uint8_t code = 255;
switch(*keys) {
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
code = *keys - '0';
break;
case '*':code = 0x0A;break;
case '#':code = 0x0B;break;
case 'A':case 'a':code = 0x2C;break;
case 'B':case 'b':code = 0x30;break;
case 'C':case 'c':code = 0x2D;break;
case 'D':case 'd':code = 0x33;break;
case 'E':case 'e':code = 0x2E;break;
case 'F':case 'f':code = 0x36;break;
case '[':
if(keys[1]!=0) {
keys++;
code = strtol(keys,&keys,16);
if(*keys!=']') {
log_msg(LOG_LEVEL_WARNING,"send_keypress: '[' without ']'");
return -1;
}
}
break;
default:
log_msg(LOG_LEVEL_WARNING,"send_keypress: bad key code %d '%c'",*keys,*keys);
return -1;
break;
}
if(code==255)
continue;
msg[len++] = code;
}
return ge_queue_message(qinterface,msg,len,finished,context);
}
enum {
PATH_ARM_LEVEL=0,
PATH_ARMED_BY,
PATH_ARM_DATE,
PATH_FS_CHIME,
PATH_FS_ENERGY_SAVER,
PATH_FS_NO_DELAY,
PATH_FS_LATCHKEY,
PATH_FS_SILENT_ARMING,
PATH_FS_QUICK_ARM,
// PATH_TEXT,
PATH_TOUCHPAD_TEXT,
PATH_KEYPRESS,
PATH_REFRESH_EQUIPMENT,
PATH_DDR,
// PATH_ENTRY_DELAY_REMAINING,
// PATH_EXIT_DELAY_REMAINING,
PATH_LIGHT_ALL,
PATH_LIGHT_1,
PATH_LIGHT_2,
PATH_LIGHT_3,
PATH_LIGHT_4,
PATH_LIGHT_5,
PATH_LIGHT_6,
PATH_LIGHT_7,
PATH_LIGHT_8,
PATH_LIGHT_9,
PATH_COUNT,
};
const char* ge_user_to_cstr(char* dest, int user) {
static char static_string[128];
if(!dest)
dest = static_string;
if(user>=230 && user<=237) {
snprintf(dest,sizeof(static_string),"P%d-MASTER-CODE",user-230);
} else if(user>=238 && user<=245) {
snprintf(dest,sizeof(static_string),"P%d-DURESS-CODE",user-238);
} else if(user==246) {
strncpy(dest,"SYSTEM-CODE",sizeof(static_string));
} else if(user==247) {
strncpy(dest,"INSTALLER",sizeof(static_string));
} else if(user==248) {
strncpy(dest,"DEALER",sizeof(static_string));
} else if(user==249) {
strncpy(dest,"AVM-CODE",sizeof(static_string));
} else if(user==250) {
strncpy(dest,"QUICK-ARM",sizeof(static_string));
} else if(user==251) {
strncpy(dest,"KEY-SWITCH",sizeof(static_string));
} else if(user==252) {
strncpy(dest,"SYSTEM",sizeof(static_string));
} else if(user==255) {
strncpy(dest,"AUTOMATION",sizeof(static_string));
} else if(user==65535) {
strncpy(dest,"SYSTEM/KEY-SWITCH",sizeof(static_string));
} else {
snprintf(dest,sizeof(static_string),"USER-%d",user);
}
return dest;
}
static smcp_status_t
partition_node_var_func(
struct ge_partition_s *node,
uint8_t action,
uint8_t path,
char* value
) {
smcp_status_t ret = 0;
if(path>=PATH_COUNT) {
ret = SMCP_STATUS_NOT_FOUND;
} else if(action==SMCP_VAR_GET_KEY) {
static const char* path_names[] = {
"arm-level",
"armed-by",
"arm-date",
"chime",
"energy-saver",
"no-delay",
"latchkey",
"silent-arming",
"quick-arm",
// "text",
"touchpad-text",
"keypress",
"refresh-equipment",
"ddr",
// "entry-delay-remaining",
// "exit-delay-remaining",
"light-all",
"light-1",
"light-2",
"light-3",
"light-4",
"light-5",
"light-6",
"light-7",
"light-8",
"light-9",
};
strcpy(value,path_names[path]);
} else if(action==SMCP_VAR_GET_LF_TITLE) {
if(path==PATH_ARM_LEVEL) {
const char *arm_level[] = {
[0]="ZONE-TEST",
[1]="DISARMED",
[2]="STAY",
[3]="AWAY",
[4]="NIGHT",
[5]="SILENT",
};
if(node->arming_level<sizeof(arm_level)/sizeof(*arm_level)-1) {
strncpy(value,arm_level[node->arming_level],SMCP_VARIABLE_MAX_VALUE_LENGTH);
} else {
ret = SMCP_STATUS_NOT_ALLOWED;
}
} else if(path==PATH_ARMED_BY) {
ge_user_to_cstr(value,node->armed_by);
} else {
ret = SMCP_STATUS_NOT_ALLOWED;
}
} else if(action==SMCP_VAR_GET_MAX_AGE) {
int v = 0;
switch(path) {
case PATH_TOUCHPAD_TEXT:
v = (node->arming_level<=1)?60:240;
break;
case PATH_ARM_LEVEL:
case PATH_ARMED_BY:
case PATH_ARM_DATE:
case PATH_FS_CHIME:
case PATH_FS_ENERGY_SAVER:
case PATH_FS_NO_DELAY:
case PATH_FS_LATCHKEY:
case PATH_FS_SILENT_ARMING:
case PATH_FS_QUICK_ARM:
case PATH_LIGHT_ALL:
case PATH_LIGHT_1:
case PATH_LIGHT_2:
case PATH_LIGHT_3:
case PATH_LIGHT_4:
case PATH_LIGHT_5:
case PATH_LIGHT_6:
case PATH_LIGHT_7:
case PATH_LIGHT_8:
case PATH_LIGHT_9:
v = 3600;
break;
default:
ret = SMCP_STATUS_NOT_ALLOWED;
break;
}
if(v) {
sprintf(value,"%d",v);
} else {
ret = SMCP_STATUS_NOT_ALLOWED;
}
} else if(action==SMCP_VAR_GET_OBSERVABLE) {
switch(path) {
case PATH_TOUCHPAD_TEXT:
case PATH_ARM_LEVEL:
case PATH_ARMED_BY:
case PATH_ARM_DATE:
case PATH_FS_CHIME:
case PATH_FS_ENERGY_SAVER:
case PATH_FS_NO_DELAY:
case PATH_FS_LATCHKEY:
case PATH_FS_SILENT_ARMING:
case PATH_FS_QUICK_ARM:
case PATH_LIGHT_ALL:
case PATH_LIGHT_1:
case PATH_LIGHT_2:
case PATH_LIGHT_3:
case PATH_LIGHT_4:
case PATH_LIGHT_5:
case PATH_LIGHT_6:
case PATH_LIGHT_7:
case PATH_LIGHT_8:
case PATH_LIGHT_9:
ret = SMCP_STATUS_OK;
break;
default:
ret = SMCP_STATUS_NOT_ALLOWED;
break;
}
} else if(action==SMCP_VAR_GET_VALUE) {
if(path==PATH_TOUCHPAD_TEXT) {
// Just send the ascii for now.
int i = 0;
value[0]=0;
strcpy(value,ge_text_to_ascii(node->touchpad_lcd,node->touchpad_lcd_len));
/*
for(;i<node->touchpad_lcd_len;i++) {
const char* str = ge_rs232_text_token_lookup[node->touchpad_lcd[i]];
if(str) {
strlcat(value,str,SMCP_VARIABLE_MAX_VALUE_LENGTH);
}
}
*/
/*
} else if(path==PATH_TEXT) {
// Just send the ascii for now.
int i = 0;
value[0]=0;
for(;i<node->label_len;i++) {
const char* str = ge_rs232_text_token_lookup[node->label[i]];
if(str) {
strlcat(value,str,SMCP_VARIABLE_MAX_VALUE_LENGTH);
}
}
*/
} else {
int v = 0;
if(path==PATH_ARM_LEVEL)
v = node->arming_level;
else if(path>=PATH_LIGHT_ALL && path<=PATH_LIGHT_9)
v = !!(node->light_state & (1<<(path-PATH_LIGHT_ALL)));
else if(path==PATH_ARM_DATE)
v = node->arm_date;
else if(path==PATH_ARMED_BY)
v = node->armed_by;
else if(path==PATH_FS_CHIME)
v = !!(node->feature_state & (1<<0));
else if(path==PATH_FS_ENERGY_SAVER)
v = !!(node->feature_state & (1<<1));
else if(path==PATH_FS_NO_DELAY)
v = !!(node->feature_state & (1<<2));
else if(path==PATH_FS_LATCHKEY)
v = !!(node->feature_state & (1<<3));
else if(path==PATH_FS_SILENT_ARMING)
v = !!(node->feature_state & (1<<4));
else if(path==PATH_FS_QUICK_ARM)
v = !!(node->feature_state & (1<<5));
else
ret = SMCP_STATUS_NOT_ALLOWED;
sprintf(value,"%d",v);
}
} else if(action==SMCP_VAR_SET_VALUE) {
if(path==PATH_ARM_LEVEL) {
struct ge_system_node_s* system_state=(struct ge_system_node_s*)node->node.node.parent;
if((node->arming_level)==atoi(value)) {
// arm level already set!
ret = SMCP_STATUS_OK;
} else {
switch(atoi(value)) {
case 1:
send_keypress(&system_state->qinterface,node->partition_number,0,"5[20]",&got_panel_response,new_panel_response_context());
ret = SMCP_STATUS_ASYNC_RESPONSE;
break;
case 2:
send_keypress(&system_state->qinterface,node->partition_number,0,"5[28]",&got_panel_response,new_panel_response_context());
ret = SMCP_STATUS_ASYNC_RESPONSE;
break;
case 3:
send_keypress(&system_state->qinterface,node->partition_number,0,"5[27]",&got_panel_response,new_panel_response_context());
ret = SMCP_STATUS_ASYNC_RESPONSE;
break;
default:
log_msg(LOG_LEVEL_WARNING,"Bad arming level \"%s\"",value);
return SMCP_STATUS_FAILURE;
}
// ret = smcp_start_async_response(&system_state->async_response,0);
// require_noerr(ret,bail);
// system_state->interface.response_context = system_state;
// system_state->interface.got_response=(void*)&got_panel_response;
// ret = SMCP_STATUS_ASYNC_RESPONSE;
// } else {
// smcp_outbound_drop();
// log_msg(LOG_LEVEL_WARNING,"Too busy to set arming level. Dropping packet.");
// ret = SMCP_STATUS_FAILURE;
}
} else if(path>=PATH_LIGHT_ALL && path<=PATH_LIGHT_9) {
struct ge_system_node_s* system_state=(struct ge_system_node_s*)node->node.node.parent;
char cmd[] = { '[','1','1'-!!atoi(value),']','0'+path-PATH_LIGHT_ALL,0};
if(!!(node->light_state & (1<<(path-PATH_LIGHT_ALL)))==atoi(value)) {
// Light already set!
ret = SMCP_STATUS_OK;
} else if(0== send_keypress(&system_state->qinterface,node->partition_number,0,cmd,&got_panel_response,new_panel_response_context())) {
// ret = smcp_start_async_response(&system_state->async_response,0);
// require_noerr(ret,bail);
// system_state->interface.response_context = system_state;
// system_state->interface.got_response=(void*)&got_panel_response;
ret = SMCP_STATUS_ASYNC_RESPONSE;
} else {
log_msg(LOG_LEVEL_WARNING,"Too busy to change light. Dropping packet.");
smcp_outbound_drop();
ret = SMCP_STATUS_FAILURE;
}
} else if(path==PATH_FS_CHIME) {
struct ge_system_node_s* system_state=(struct ge_system_node_s*)node->node.node.parent;
if((node->feature_state & (1<<0))==atoi(value)) {
// Chime already set!
ret = SMCP_STATUS_OK;
} else if(0== send_keypress(&system_state->qinterface,node->partition_number,0,"71",&got_panel_response,new_panel_response_context())) {
// ret = smcp_start_async_response(&system_state->async_response,0);
// require_noerr(ret,bail);
// system_state->interface.response_context = system_state;
// system_state->interface.got_response=(void*)&got_panel_response;
ret = SMCP_STATUS_ASYNC_RESPONSE;
} else {
log_msg(LOG_LEVEL_WARNING,"Too busy to set chime. Dropping packet.");
smcp_outbound_drop();
ret = SMCP_STATUS_FAILURE;
}
} else if(path==PATH_REFRESH_EQUIPMENT) {
struct ge_system_node_s* system_state=(struct ge_system_node_s*)node->node.node.parent;
if(GE_RS232_STATUS_OK!=ge_queue_message(&system_state->qinterface,refresh_equipment_msg,sizeof(refresh_equipment_msg),&got_panel_response,new_panel_response_context()))
ret = SMCP_STATUS_FAILURE;
else
ret = SMCP_STATUS_ASYNC_RESPONSE;
// if(!system_state->interface.got_response) {
// ret = smcp_start_async_response(&system_state->async_response,0);
// require_noerr(ret,bail);
// system_state->interface.response_context = system_state;
// system_state->interface.got_response=(void*)&got_panel_response;
ret = SMCP_STATUS_ASYNC_RESPONSE;
// }
} else if(path==PATH_KEYPRESS) {
struct ge_system_node_s* system_state=(struct ge_system_node_s*)node->node.node.parent;
if(0 == send_keypress(&system_state->qinterface,node->partition_number,0,value,&got_panel_response,new_panel_response_context())) {
// ret = smcp_start_async_response(&system_state->async_response,0);
// require_noerr(ret,bail);
// system_state->interface.response_context = system_state;
// system_state->interface.got_response=(void*)&got_panel_response;
ret = SMCP_STATUS_ASYNC_RESPONSE;
} else {
log_msg(LOG_LEVEL_WARNING,"Too busy to send keypresses. Dropping packet.");
smcp_outbound_drop();
ret = SMCP_STATUS_FAILURE;
}
} else if(path==PATH_DDR) {
struct ge_system_node_s* system_state=(struct ge_system_node_s*)node->node.node.parent;
if(GE_RS232_STATUS_OK!=ge_queue_message(&system_state->qinterface,dynamic_data_refresh_msg,sizeof(dynamic_data_refresh_msg),&got_panel_response,new_panel_response_context()))
ret = SMCP_STATUS_FAILURE;
else
ret = SMCP_STATUS_ASYNC_RESPONSE;
// if(!system_state->interface.got_response) {
// dynamic_data_refresh(&system_state->interface);
// ret = smcp_start_async_response(&system_state->async_response,0);
// require_noerr(ret,bail);
// system_state->interface.response_context = system_state;
// system_state->interface.got_response=(void*)&got_panel_response;
// ret = SMCP_STATUS_ASYNC_RESPONSE;
// } else {
// log_msg(LOG_LEVEL_WARNING,"Too busy to send dynamic data refresh. Dropping packet.");
// smcp_outbound_drop();
// ret = SMCP_STATUS_FAILURE;
// }
} else {
ret = SMCP_STATUS_NOT_ALLOWED;
}
} else {
ret = SMCP_STATUS_NOT_IMPLEMENTED;
}
bail:
return ret;
}
enum {
PATH_PARTITION=0,
PATH_AREA,
PATH_GROUP,
PATH_TYPE,
PATH_TEXT,
PATH_LAST_TRIPPED,
PATH_STATUS_TRIPPED,
PATH_STATUS_FAULT,
PATH_STATUS_ALARM,
PATH_STATUS_TROUBLE,
PATH_STATUS_BYPASS,
PATH_ZONE_COUNT,
};
static smcp_status_t
zone_node_var_func(
struct ge_zone_s *node,
uint8_t action,
uint8_t path,
char* value
) {
smcp_status_t ret = 0;
if(path>=PATH_ZONE_COUNT) {
ret = SMCP_STATUS_NOT_FOUND;
} else if(action==SMCP_VAR_GET_KEY) {
static const char* path_names[] = {
"pn",
"an",
"gn",
"zt",
"text",
"last-tripped",
"zs.tripped",
"zs.fault",
"zs.alarm",
"zs.trouble",
"zs.bypass",
};
strcpy(value,path_names[path]);
} else if(action==SMCP_VAR_GET_MAX_AGE) {
int v = 0;
switch(path) {
case PATH_PARTITION:
case PATH_AREA:
case PATH_GROUP:
case PATH_TYPE:
case PATH_TEXT:
v = 3600;
break;
case PATH_LAST_TRIPPED:
case PATH_STATUS_TRIPPED:
case PATH_STATUS_FAULT:
case PATH_STATUS_ALARM:
case PATH_STATUS_TROUBLE:
case PATH_STATUS_BYPASS:
v = 60*5;
break;
default:
ret = SMCP_STATUS_NOT_ALLOWED;
break;
}
if(v) {
sprintf(value,"%d",v);
} else {
ret = SMCP_STATUS_NOT_ALLOWED;
}
} else if(action==SMCP_VAR_GET_OBSERVABLE) {
switch(path) {
case PATH_LAST_TRIPPED:
case PATH_STATUS_TRIPPED:
case PATH_STATUS_FAULT:
case PATH_STATUS_ALARM:
case PATH_STATUS_TROUBLE:
case PATH_STATUS_BYPASS:
ret = SMCP_STATUS_OK;
break;
default:
ret = SMCP_STATUS_NOT_ALLOWED;
break;
}
} else if(action==SMCP_VAR_GET_VALUE) {
if(path==PATH_TEXT) {
// Just send the ascii for now.
int i = 0;
value[0]=0;
for(;i<node->label_len;i++) {
const char* str = ge_rs232_text_token_lookup[node->label[i]];
if(str) {
strlcat(value,str,SMCP_VARIABLE_MAX_VALUE_LENGTH);
}
}
} else {
int v = 0;
if(path==PATH_PARTITION)
v = node->partition;
else if(path==PATH_AREA)
v = node->area;
else if(path==PATH_GROUP)
v = node->group;
else if(path==PATH_TYPE)
v = node->type;
else if(path==PATH_LAST_TRIPPED)
v = node->last_tripped;
else if(path==PATH_STATUS_TRIPPED)
v = !!(node->status & GE_RS232_ZONE_STATUS_TRIPPED);
else if(path==PATH_STATUS_TROUBLE)
v = !!(node->status & GE_RS232_ZONE_STATUS_TROUBLE);
else if(path==PATH_STATUS_FAULT)
v = !!(node->status & GE_RS232_ZONE_STATUS_FAULT);
else if(path==PATH_STATUS_BYPASS)
v = !!(node->status & GE_RS232_ZONE_STATUS_BYPASSED);
else if(path==PATH_STATUS_ALARM)
v = !!(node->status & GE_RS232_ZONE_STATUS_ALARM);
sprintf(value,"%d",v);
}
} else if(action==SMCP_VAR_SET_VALUE) {
ret = SMCP_STATUS_NOT_ALLOWED;
} else {
ret = SMCP_STATUS_NOT_IMPLEMENTED;
}
return ret;
}
struct ge_zone_s *
ge_get_zone(struct ge_system_node_s *node,int zonei) {
struct ge_zone_s *zone = NULL;
if(zonei && (zonei<=GE_RS232_MAX_ZONES)) {
zone = &node->zone[zonei-1];
if(!zone->node.node.parent) {
// Bring this zone to life.
char* label = NULL;
asprintf(&label,"zone-%d",zonei);
smcp_variable_node_init(&zone->node,&node->node,label);
zone->node.func = (smcp_variable_node_func)&zone_node_var_func;
zone->zone_number = zonei;
}
}
return zone;
}
struct ge_partition_s *
ge_get_partition(struct ge_system_node_s *node,int partitioni) {
struct ge_partition_s *partition = NULL;
if(partitioni && (partitioni<=GE_RS232_MAX_PARTITIONS)) {
partition = &node->partition[partitioni-1];
if(!partition->node.node.parent) {
// Bring this partition to life.
char* label = NULL;
asprintf(&label,"p-%d",partitioni);
smcp_variable_node_init(&partition->node,&node->node,label);
partition->node.func = (smcp_variable_node_func)&partition_node_var_func;
partition->partition_number = partitioni;
}
}
return partition;
}
bool
should_bypass_gate(void) {
struct tm* local;
time_t t;
time(&t);
local = localtime(&t);
// return 1;
return local->tm_wday==3
&& local->tm_hour>=7
&& local->tm_hour<=12+5;
}
static time_t next_lawn_care_hack_check;
void
lawn_care_hack_check(struct ge_system_node_s *self) {
struct ge_partition_s* partition = ge_get_partition(self,1);
struct ge_zone_s* zone = ge_get_zone(self,18);
ge_rs232_status_t status = ge_rs232_ready_to_send(&self->interface);
if(status == GE_RS232_STATUS_WAIT) {
next_lawn_care_hack_check = time(NULL)+2;
return;
}
static bool did_run;
if(!did_run && partition->arming_level==2 || partition->arming_level==3) {
bool gate_is_bypassed = !!(zone->status&GE_RS232_ZONE_STATUS_BYPASSED);
if(gate_is_bypassed^should_bypass_gate()) {
const char* code = self->system_code;
char bypass_command[40];
if(code[0]) {
// did_run = 1;
snprintf(bypass_command,sizeof(bypass_command),"#%s18",code);
//log_msg(LOG_LEVEL_ERROR,"LAWN HACK KEYPRESS: %s",bypass_command);
//zone->status|=GE_RS232_ZONE_STATUS_BYPASSED;
send_keypress(&self->qinterface,partition->partition_number,0,bypass_command,NULL,NULL);
}
}
} else if(partition->arming_level==1) {
did_run = 0;
}
next_lawn_care_hack_check = time(NULL)+60;
}
ge_rs232_status_t
received_message(struct ge_system_node_s *node, const uint8_t* data, uint8_t len,struct ge_rs232_s* interface) {
static uint8_t last_msg[GE_RS232_MAX_MESSAGE_SIZE];
static uint8_t last_msg_len;
if(data[0]==GE_RS232_PTA_SUBCMD && (
data[1]==GE_RS232_PTA_SUBCMD_SIREN_SYNC
)) {
return GE_RS232_STATUS_OK;
}
if(data[0]==GE_RS232_PTA_SUBCMD &&
data[1]==GE_RS232_PTA_SUBCMD_TOUCHPAD_DISPLAY &&
data[2]!=1
) {
//return GE_RS232_STATUS_OK;
}
if(last_msg_len == len && 0==memcmp(data,last_msg,len)) {
return GE_RS232_STATUS_OK;
}
memcpy(last_msg,data,len);
last_msg_len = len;
if(data[0]==GE_RS232_PTA_AUTOMATION_EVENT_LOST) {
log_msg(LOG_LEVEL_NOTICE,"[AUTOMATION_EVENT_LOST]");
ge_rs232_status_t status = dynamic_data_refresh(&node->qinterface,NULL,NULL);
len=0;
return 0;
} else if(data[0]==GE_RS232_PTA_ZONE_STATUS) {
int zonei = (data[3]<<8)+data[4];
struct ge_zone_s* zone = ge_get_zone(node,zonei);
if(zone) {
if((zone->status^data[5])&GE_RS232_ZONE_STATUS_TRIPPED) {
if((data[5]&GE_RS232_ZONE_STATUS_TRIPPED)) {
zone->last_tripped = time(NULL);
smcp_variable_node_did_change(&zone->node,PATH_LAST_TRIPPED,NULL);
}
smcp_variable_node_did_change(&zone->node,PATH_STATUS_TRIPPED,(data[5]&GE_RS232_ZONE_STATUS_TRIPPED)?"v=1":"v=0");
}
if((zone->status^data[5])&GE_RS232_ZONE_STATUS_FAULT)
smcp_variable_node_did_change(&zone->node,PATH_STATUS_FAULT,(data[5]&GE_RS232_ZONE_STATUS_FAULT)?"v=1":"v=0");
if((zone->status^data[5])&GE_RS232_ZONE_STATUS_TROUBLE)
smcp_variable_node_did_change(&zone->node,PATH_STATUS_TROUBLE,(data[5]&GE_RS232_ZONE_STATUS_TROUBLE)?"v=1":"v=0");
if((zone->status^data[5])&GE_RS232_ZONE_STATUS_ALARM)
smcp_variable_node_did_change(&zone->node,PATH_STATUS_ALARM,(data[5]&GE_RS232_ZONE_STATUS_ALARM)?"v=1":"v=0");
if((zone->status^data[5])&GE_RS232_ZONE_STATUS_BYPASSED)
smcp_variable_node_did_change(&zone->node,PATH_STATUS_BYPASS,(data[5]&GE_RS232_ZONE_STATUS_BYPASSED)?"v=1":"v=0");
zone->partition = data[1];
zone->area = data[2];
zone->status = data[5];
log_msg(LOG_LEVEL_NOTICE,"[ZONE_STATUS] ZONE:%02d STATUS:%s%s%s%s%s TEXT:\"%s\"",
zonei,
data[5]&GE_RS232_ZONE_STATUS_TRIPPED?"T":"-",
data[5]&GE_RS232_ZONE_STATUS_FAULT?"F":"-",
data[5]&GE_RS232_ZONE_STATUS_ALARM?"A":"-",
data[5]&GE_RS232_ZONE_STATUS_TROUBLE?"R":"-",
data[5]&GE_RS232_ZONE_STATUS_BYPASSED?"B":"-",
ge_text_to_ascii_one_line(zone->label,zone->label_len)
);
}
return 0;
len = 0;
} else if(data[0]==GE_RS232_PTA_EQUIP_LIST_ZONE_DATA) {
uint16_t zonei = (data[4]<<8)+data[5];
struct ge_zone_s* zone = ge_get_zone(node,zonei);
if(zone) {
//if((zone->status^data[7])&GE_RS232_ZONE_STATUS_TRIPPED)
// smcp_variable_node_did_change(&zone->node,PATH_STATUS_TRIPPED,NULL);
if((zone->status^data[7])&GE_RS232_ZONE_STATUS_FAULT)
smcp_variable_node_did_change(&zone->node,PATH_STATUS_FAULT,(data[7]&GE_RS232_ZONE_STATUS_FAULT)?"v=1":"v=0");
if((zone->status^data[7])&GE_RS232_ZONE_STATUS_TROUBLE)
smcp_variable_node_did_change(&zone->node,PATH_STATUS_TROUBLE,(data[7]&GE_RS232_ZONE_STATUS_TROUBLE)?"v=1":"v=0");
if((zone->status^data[7])&GE_RS232_ZONE_STATUS_ALARM)
smcp_variable_node_did_change(&zone->node,PATH_STATUS_ALARM,(data[7]&GE_RS232_ZONE_STATUS_ALARM)?"v=1":"v=0");
if((zone->status^data[7])&GE_RS232_ZONE_STATUS_BYPASSED)
smcp_variable_node_did_change(&zone->node,PATH_STATUS_BYPASS,(data[7]&GE_RS232_ZONE_STATUS_BYPASSED)?"v=1":"v=0");
zone->partition = data[1];
zone->area = data[2];
zone->group = data[3];
zone->zone_number = zonei;
zone->type = data[6];
zone->status = data[7];
zone->label_len = len-8;
memcpy(zone->label,data+8,len-8);
}
log_msg(LOG_LEVEL_NOTICE,"[EQUIP_LIST_ZONE_INFO] ZONE:%d PN:%d AREA:%d TYPE:%d GROUP:%d STATUS:%s%s%s%s%s TEXT:\"%s\"",
zonei,
data[1],
data[2],
data[6],
data[3],
"?",
data[7]&GE_RS232_ZONE_STATUS_FAULT?"F":"-",
data[7]&GE_RS232_ZONE_STATUS_ALARM?"A":"-",
data[7]&GE_RS232_ZONE_STATUS_TROUBLE?"R":"-",
data[7]&GE_RS232_ZONE_STATUS_BYPASSED?"B":"-",
ge_text_to_ascii_one_line(data+8,len-8)
);
return 0;
len = 0;
} else if(data[0]==GE_RS232_PTA_EQUIP_LIST_USER_DATA) {
uint16_t user = (data[1]<<8)+data[2];
char code_backing[5];
char* code = code_backing;
if(user == 246) {
code = node->system_code;
} else if(user == 247) {
code = node->installer_code;
} else if(user>=230 && user<=237) {
// Partition master code
} else if(user>=238 && user<=245) {
// Partition duress code
}
if(code) {
code[0] = (data[4]>>4)+'0';
code[1] = (data[4]&0xF)+'0';
code[2] = (data[5]>>4)+'0';
code[3] = (data[5]&0xF)+'0';
code[4] = 0;
// Check for the zero code, which is invalid.
if(strcmp(code,"0000")==0)
code[0] = 0;
#if DEBUG
log_msg(LOG_LEVEL_DEBUG,
"[EQUIP_LIST_USER_DATA] USER:\"%s\"(%d) CODE=\"%s\"",
ge_user_to_cstr(NULL,user),
user,
code
);
#endif
}
} else if(data[0]==GE_RS232_PTA_EQUIP_LIST_SUPERBUS_DEV_DATA) {
log_msg(LOG_LEVEL_INFO,
"[EQUIP_LIST_SUPERBUS_DEV_DATA] PN:%d AREA:%d UNIT-ID:0x%06x UN:%d STATUS:%s",
data[1],
data[2],
(data[3]<<16)+(data[4]<<8)+data[5],
data[7],
data[6]?"FAILURE":"OK"
);
len = 0;
} else if(data[0]==GE_RS232_PTA_EQUIP_LIST_SUPERBUS_CAP_DATA) {
const char* cap_list[] = {
[0x00]="Power Supervision",
[0x01]="Access Control",
[0x02]="Analog Smoke",
[0x03]="Audio Listen-In",
[0x04]="SnapCard Supervision",
[0x05]="Microburst",
[0x06]="Dual Phone Line",
[0x07]="Energy Management",
[0x08]="Input Zones",
[0x09]="Automation",
[0x0A]="Phone Interface",
[0x0B]="Relay Outputs",
[0x0C]="RF Receiver",
[0x0D]="RF Transmitter",
[0x0E]="Parallel Printer",
[0x0F]="UNKNOWN-0x0F",
[0x10]="LED Touchpad",
[0x11]="1-Line/2-Line/BLT Touchpad",
[0x12]="GUI Touchpad",
[0x13]="Voice Evacuation",
[0x14]="Pager",
[0x15]="Downloadable code/data",
[0x16]="JTECH Premise Pager",
[0x17]="Cryptography",
[0x18]="LED Display",
};