-
Notifications
You must be signed in to change notification settings - Fork 2
/
ademco.c
1771 lines (1609 loc) · 59.9 KB
/
ademco.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
/*
* Ademco library
* captainwong
* 2022-6-13 rewrited this C version
*/
#include "ademco.h"
#include <assert.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if ADEMCO_USE_UNIQUE_BUF
static char ADEMCO_BUF_MEMORY_MODIFIER buf[ADEMCO_PACKET_MAX_LEN];
#endif
#define ADEMCO_STRINGIFY(v) ADEMCO_STRINGIFY_HELPER(v)
#define ADEMCO_STRINGIFY_HELPER(v) #v
#define ADEMCO_VERSION_STRING_BASE ADEMCO_STRINGIFY(ADEMCO_VERSION_MAJOR) "." ADEMCO_STRINGIFY(ADEMCO_VERSION_MINOR) "." ADEMCO_STRINGIFY(ADEMCO_VERSION_PATCH)
#if ADEMCO_VERSION_IS_RELEASE
#define ADEMCO_VERSION_STRING ADEMCO_VERSION_STRING_BASE
#else
#define ADEMCO_VERSION_STRING ADEMCO_VERSION_STRING_BASE "-" ADEMCO_VERSION_SUFFIX
#endif
uint32_t ademco_version(void) {
return ADEMCO_VERSION_HEX;
}
#if ADEMCO_ENABLE_TO_STRING
const char* ademco_version_string(void) {
return ADEMCO_VERSION_STRING;
}
#endif
uint8_t ademco_hex2char(uint8_t h) {
h &= 0x0F;
if (h > 9)
return 'A' + h - 10;
else
return '0' + h;
}
uint8_t ademco_char2hex(uint8_t c) {
if ('0' <= c && c <= '9')
return c - '0';
else if ('A' <= c && c <= 'F')
return c - 'A' + 10;
else if ('a' <= c && c <= 'f')
return c - 'a' + 10;
else
return 0xFF;
}
void ademco_print(const ademco_char_t* p, size_t len) {
while (len--) {
if (isprint(*(const uint8_t*)p))
putchar(*p);
else
printf("\\x%02X", *(const uint8_t*)p);
p++;
}
printf("\n");
}
uint8_t ademco_encode_signal_strength(uint8_t strength) {
if (strength > ADEMCO_SIGNAL_STRENGTH_MAX)
strength = ADEMCO_SIGNAL_STRENGTH_MAX;
return bcd_encode(strength);
}
uint8_t ademco_decode_signal_strength(uint8_t bcd_strength) {
uint8_t strength = bcd_decode(bcd_strength);
if (strength > ADEMCO_SIGNAL_STRENGTH_MAX)
strength = ADEMCO_SIGNAL_STRENGTH_MAX;
return strength;
}
bool ademco_is_valid_account(const char* acct) {
size_t ADEMCO_BUF_MEMORY_MODIFIER len = 0;
int ADEMCO_BUF_MEMORY_MODIFIER ishex = 0;
while (*acct && len < ADEMCO_PACKET_ACCT_MAX_LEN) {
if (isdigit(*acct)) {
len++;
} else if (isxdigit(*acct) && len < ADEMCO_PACKET_ACCT_MAC_LEN) {
len++;
ishex = 1;
} else {
return 0;
}
acct++;
}
return *acct == '\0' &&
(ishex ? len == ADEMCO_PACKET_ACCT_MAC_LEN : (len >= ADEMCO_PACKET_ACCT_MIN_LEN));
}
bool ademco_is_valid_password(const char* pwd) {
size_t ADEMCO_BUF_MEMORY_MODIFIER len = 0;
while (*pwd && len < ADEMCO_PACKET_PWD_MAX_LEN) {
if (isdigit(*pwd)) {
len++;
} else {
return false;
}
pwd++;
}
return *pwd == '\0' && len >= ADEMCO_PACKET_PWD_MIN_LEN;
}
bool ademco_is_machine_status_event(ademco_event_t ademco_event) {
return ademco_event == EVENT_ARM_AWAY ||
ademco_event == EVENT_ARM_STAY ||
ademco_event == EVENT_ARM_STAY_1456 ||
ademco_event == EVENT_DISARM;
}
bool ademco_is_machine_type_event(ademco_event_t ademco_event) {
return ademco_event == EVENT_I_AM_NET_MODULE ||
ademco_event == EVENT_I_AM_GPRS ||
ademco_event == EVENT_I_AM_LCD ||
ademco_event == EVENT_I_AM_WIRE ||
ademco_event == EVENT_I_AM_WIFI ||
ademco_event == EVENT_I_AM_3_SECTION ||
ademco_event == EVENT_I_AM_IOT ||
ademco_event == EVENT_I_AM_TRUE_COLOR ||
ademco_event == EVENT_I_AM_GPRS_IOT ||
ademco_event == EVENT_I_AM_GPRS_PHONE ||
ademco_event == EVENT_I_AM_NB ||
ademco_event == EVENT_I_AM_WIFI2;
}
bool ademco_is_event_need_control_source(ademco_event_t ademco_event) {
return ademco_event == EVENT_ARM_AWAY ||
ademco_event == EVENT_ARM_STAY ||
ademco_event == EVENT_ARM_STAY_1456 ||
ademco_event == EVENT_DISARM ||
ademco_event == EVENT_EMERGENCY ||
ademco_event == EVENT_DURESS ||
ademco_event == EVENT_PHONE_USER_CANCLE_ALARM;
}
ademco_event_t ademco_get_exception_event_by_recover_event(ademco_event_t recover_event) {
switch (recover_event) {
case EVENT_DISCONNECT_RECOVER:
return EVENT_DISCONNECT;
case EVENT_LOST_RECOVER:
return EVENT_LOST;
case EVENT_SERIAL_485_RECOVER:
return EVENT_SERIAL_485_DIS;
case EVENT_SUB_MACHINE_SENSOR_RECOVER:
return EVENT_SUB_MACHINE_SENSOR_EXCEPTION;
case EVENT_SUB_MACHINE_POWER_RECOVER:
return EVENT_SUB_MACHINE_POWER_EXCEPTION;
case EVENT_LOW_BATTERY_RECOVER:
return EVENT_LOW_BATTERY;
case EVENT_POWER_EXCEPTION_RECOVER:
return EVENT_POWER_EXCEPTION;
case EVENT_OTHER_EXCEPTION_RECOVER:
return EVENT_OTHER_EXCEPTION;
case EVENT_AC_RECOVER:
return EVENT_AC_BROKEN;
case EVENT_SOLAR_DISTURB_RECOVER:
return EVENT_SOLAR_DISTURB;
case EVENT_BAD_BATTERY_RECOVER:
return EVENT_BAD_BATTERY;
default:
return EVENT_INVALID_EVENT;
}
}
ademco_event_level_t ademco_get_event_level(ademco_event_t ademco_event) {
switch (ademco_event) {
case EVENT_ARM_AWAY:
case EVENT_DISARM:
case EVENT_ARM_STAY:
case EVENT_ARM_STAY_1456:
return EVENT_LEVEL_STATUS;
case EVENT_BAD_BATTERY_RECOVER:
case EVENT_AC_RECOVER:
case EVENT_DISCONNECT_RECOVER:
case EVENT_LOST_RECOVER:
case EVENT_SERIAL_485_RECOVER:
case EVENT_SUB_MACHINE_SENSOR_RECOVER:
case EVENT_SUB_MACHINE_POWER_RECOVER:
case EVENT_LOW_BATTERY_RECOVER:
case EVENT_POWER_EXCEPTION_RECOVER:
case EVENT_OTHER_EXCEPTION_RECOVER:
case EVENT_SOLAR_DISTURB_RECOVER:
return EVENT_LEVEL_EXCEPTION_RECOVER;
case EVENT_AC_BROKEN:
case EVENT_LOW_BATTERY:
case EVENT_BAD_BATTERY:
case EVENT_SOLAR_DISTURB:
case EVENT_DISCONNECT:
case EVENT_LOST:
case EVENT_SUB_MACHINE_SENSOR_EXCEPTION:
case EVENT_SUB_MACHINE_POWER_EXCEPTION:
case EVENT_POWER_EXCEPTION:
case EVENT_OTHER_EXCEPTION:
return EVENT_LEVEL_EXCEPTION;
case EVENT_SERIAL_485_DIS:
case EVENT_BURGLAR:
case EVENT_DURESS:
case EVENT_EMERGENCY:
case EVENT_FIRE:
case EVENT_GAS:
case EVENT_TAMPER:
case EVENT_ZONE_TAMPER:
case EVENT_WATER:
return EVENT_LEVEL_ALARM;
default:
return EVENT_LEVEL_NULL;
}
}
#if ADEMCO_ENABLE_TO_STRING
const char* ademco_event_to_string(ademco_event_t ademco_event) {
switch (ademco_event) {
#define XX(code, name, zh) \
case EVENT_##name: \
return #name;
// ----标准安定宝协议事件----
ADEMCO_STATUS_EVENTS_MAP(XX)
ADEMCO_ALARM_EVENTS_MAP(XX)
ADEMCO_ALARM_RECOVER_EVENTS_MAP(XX)
ADEMCO_EXEPTION_EVENTS_MAP(XX)
ADEMCO_EXEPTION_RECOVER_EVENTS_MAP(XX)
// ---私有事件---------
ADEMCO_HB_EVENTS_MAP(XX)
#undef XX
default:
return "Undefined ademco_event_t";
}
}
#endif
#if ADEMCO_ENABLE_CHINESE
const char* ademco_event_to_string_chinese(ademco_event_t ademco_event) {
switch (ademco_event) {
#define XX(code, name, zh) \
case EVENT_##name: \
return zh;
// ----标准安定宝协议事件----
ADEMCO_STATUS_EVENTS_MAP(XX)
ADEMCO_ALARM_EVENTS_MAP(XX)
ADEMCO_ALARM_RECOVER_EVENTS_MAP(XX)
ADEMCO_EXEPTION_EVENTS_MAP(XX)
ADEMCO_EXEPTION_RECOVER_EVENTS_MAP(XX)
// ---私有事件---------
ADEMCO_HB_EVENTS_MAP(XX)
#undef XX
default:
return "未定义的安定宝事件";
}
}
#endif
#ifndef ADEMCO_USE_EXTERNAL_CRC16
static const uint16_t ADEMCO_CONST_MODIFIER crc16Table[256] = {
/* DEFINE THE FIRST ORDER POLYINOMIAL TABLE */
0x0000, 0xC0C1, 0xC181, 0x0140, 0xC301, 0x03C0, 0x0280, 0xC241, // 0x00
0xC601, 0x06C0, 0x0780, 0xC741, 0x0500, 0xC5C1, 0xC481, 0x0440, // 0x08
0xCC01, 0x0CC0, 0x0D80, 0xCD41, 0x0F00, 0xCFC1, 0xCE81, 0x0E40, // 0x10
0x0A00, 0xCAC1, 0xCB81, 0x0B40, 0xC901, 0x09C0, 0x0880, 0xC841, // 0x18
0xD801, 0x18C0, 0x1980, 0xD941, 0x1B00, 0xDBC1, 0xDA81, 0x1A40, // 0x20
0x1E00, 0xDEC1, 0xDF81, 0x1F40, 0xDD01, 0x1DC0, 0x1C80, 0xDC41, // 0x28
0x1400, 0xD4C1, 0xD581, 0x1540, 0xD701, 0x17C0, 0x1680, 0xD641, // 0x30
0xD201, 0x12C0, 0x1380, 0xD341, 0x1100, 0xD1C1, 0xD081, 0x1040, // 0x38
0xF001, 0x30C0, 0x3180, 0xF141, 0x3300, 0xF3C1, 0xF281, 0x3240, // 0x40
0x3600, 0xF6C1, 0xF781, 0x3740, 0xF501, 0x35C0, 0x3480, 0xF441, // 0x48
0x3C00, 0xFCC1, 0xFD81, 0x3D40, 0xFF01, 0x3FC0, 0x3E80, 0xFE41, // 0x50
0xFA01, 0x3AC0, 0x3B80, 0xFB41, 0x3900, 0xF9C1, 0xF881, 0x3840, // 0x58
0x2800, 0xE8C1, 0xE981, 0x2940, 0xEB01, 0x2BC0, 0x2A80, 0xEA41, // 0x60
0xEE01, 0x2EC0, 0x2F80, 0xEF41, 0x2D00, 0xEDC1, 0xEC81, 0x2C40, // 0x68
0xE401, 0x24C0, 0x2580, 0xE541, 0x2700, 0xE7C1, 0xE681, 0x2640, // 0x70
0x2200, 0xE2C1, 0xE381, 0x2340, 0xE101, 0x21C0, 0x2080, 0xE041, // 0x78
0xA001, 0x60C0, 0x6180, 0xA141, 0x6300, 0xA3C1, 0xA281, 0x6240, // 0x80
0x6600, 0xA6C1, 0xA781, 0x6740, 0xA501, 0x65C0, 0x6480, 0xA441, // 0x88
0x6C00, 0xACC1, 0xAD81, 0x6D40, 0xAF01, 0x6FC0, 0x6E80, 0xAE41, // 0x90
0xAA01, 0x6AC0, 0x6B80, 0xAB41, 0x6900, 0xA9C1, 0xA881, 0x6840, // 0x98
0x7800, 0xB8C1, 0xB981, 0x7940, 0xBB01, 0x7BC0, 0x7A80, 0xBA41, // 0xA0
0xBE01, 0x7EC0, 0x7F80, 0xBF41, 0x7D00, 0xBDC1, 0xBC81, 0x7C40, // 0xA8
0xB401, 0x74C0, 0x7580, 0xB541, 0x7700, 0xB7C1, 0xB681, 0x7640, // 0xB0
0x7200, 0xB2C1, 0xB381, 0x7340, 0xB101, 0x71C0, 0x7080, 0xB041, // 0xB8
0x5000, 0x90C1, 0x9181, 0x5140, 0x9301, 0x53C0, 0x5280, 0x9241, // 0xC0
0x9601, 0x56C0, 0x5780, 0x9741, 0x5500, 0x95C1, 0x9481, 0x5440, // 0xC8
0x9C01, 0x5CC0, 0x5D80, 0x9D41, 0x5F00, 0x9FC1, 0x9E81, 0x5E40, // 0xD0
0x5A00, 0x9AC1, 0x9B81, 0x5B40, 0x9901, 0x59C0, 0x5880, 0x9841, // 0xD8
0x8801, 0x48C0, 0x4980, 0x8941, 0x4B00, 0x8BC1, 0x8A81, 0x4A40, // 0xE0
0x4E00, 0x8EC1, 0x8F81, 0x4F40, 0x8D01, 0x4DC0, 0x4C80, 0x8C41, // 0xE8
0x4400, 0x84C1, 0x8581, 0x4540, 0x8701, 0x47C0, 0x4680, 0x8641, // 0xF0
0x8201, 0x42C0, 0x4380, 0x8341, 0x4100, 0x81C1, 0x8081, 0x4040, // 0xF8
};
uint16_t ademco_crc16(const ademco_char_t ADEMCO_BUF_MEMORY_MODIFIER* buf, size_t len) {
uint16_t ADEMCO_BUF_MEMORY_MODIFIER crc = 0;
while (len--)
crc = (crc >> 8) ^ crc16Table[(crc ^ *buf++) & 0xFF];
return crc;
}
#endif // ADEMCO_USE_EXTERNAL_CRC16
size_t ademco_append_data(ademco_char_t* buf, const char* acct, ademco_id_t ademco_id,
ademco_event_t ademco_event, ademco_gg_t gg, ademco_zone_t zone) {
char* p = (char*)buf;
uint8_t i;
if (ademco_event == EVENT_INVALID_EVENT) {
*p++ = '[';
*p++ = ']';
return ADEMCO_PACKET_DATA_SEGMENT_EMPTY_LEN;
} else {
*p++ = '[';
*p++ = '#';
ademco_id %= ADEMCO_ID_SENTINEL;
if (acct && acct[0] && ademco_id == 0) {
for (i = 0; i < ADEMCO_PACKET_ACCT_MAX_LEN && *acct; i++) {
*p++ = *acct++;
}
} else {
p += ademco_dec_to_hex_str(p, ademco_id, 6);
}
*p++ = '|';
*p++ = (uint8_t)(ademco_event / 1000) + '0';
*p++ = (uint8_t)((ademco_event % 1000) / 100) + '0';
*p++ = (uint8_t)((ademco_event % 100) / 10) + '0';
*p++ = (uint8_t)(ademco_event % 10) + '0';
*p++ = ' ';
if (gg == 0xEE) {
*p++ = 'E';
*p++ = 'E';
} else if (gg == 0xCC) {
*p++ = 'C';
*p++ = 'C';
} else {
*p++ = ((gg / 10) & 0x0F) + '0';
*p++ = ((gg % 10) & 0x0F) + '0';
}
*p++ = ' ';
zone %= ADEMCO_ZONE_SENTINEL;
*p++ = (uint8_t)(zone / 100) + '0';
*p++ = (uint8_t)((zone % 100) / 10) + '0';
*p++ = (uint8_t)(zone % 10) + '0';
*p++ = ']';
// assert(p - (char*)buf == ADEMCO_PACKET_DATA_SEGMENT_FULL_LEN);
*p = '\0'; // for debug convenience
return p - (char*)buf;
}
}
size_t ademco_append_data2(ademco_data_t* ademco_data, const char* acct,
ademco_id_t ademco_id, ademco_event_t ademco_event,
ademco_gg_t gg, ademco_zone_t zone) {
ademco_data->ademco_id = ademco_id;
ademco_data->ademco_event = ademco_event;
ademco_data->gg = gg;
ademco_data->zone = zone;
return ademco_data->raw_len = ademco_append_data(ademco_data->raw,
acct, ademco_id,
ademco_event, gg, zone);
}
size_t ademco_append_data3(ademco_data_t* ademco_data, const char* acct) {
char* p = (char*)ademco_data->raw;
uint8_t i;
if (ademco_data->ademco_event == EVENT_INVALID_EVENT) {
*p++ = '[';
*p++ = ']';
return ademco_data->raw_len = ADEMCO_PACKET_DATA_SEGMENT_EMPTY_LEN;
} else {
*p++ = '[';
*p++ = '#';
ademco_data->ademco_id %= ADEMCO_ID_SENTINEL;
if (acct && acct[0] && ademco_data->ademco_id == 0) {
for (i = 0; i < ADEMCO_PACKET_ACCT_MAX_LEN && *acct; i++) {
*p++ = *acct++;
}
} else {
p += ademco_dec_to_hex_str(p, ademco_data->ademco_id, 6);
}
*p++ = '|';
*p++ = (uint8_t)(ademco_data->ademco_event / 1000) + '0';
*p++ = (uint8_t)((ademco_data->ademco_event % 1000) / 100) + '0';
*p++ = (uint8_t)((ademco_data->ademco_event % 100) / 10) + '0';
*p++ = (uint8_t)(ademco_data->ademco_event % 10) + '0';
*p++ = ' ';
if (ademco_data->gg == 0xEE) {
*p++ = 'E';
*p++ = 'E';
} else if (ademco_data->gg == 0xCC) {
*p++ = 'C';
*p++ = 'C';
} else {
*p++ = ((ademco_data->gg / 10) & 0x0F) + '0';
*p++ = ((ademco_data->gg % 10) & 0x0F) + '0';
}
*p++ = ' ';
ademco_data->zone %= ADEMCO_ZONE_SENTINEL;
*p++ = (uint8_t)(ademco_data->zone / 100) + '0';
*p++ = (uint8_t)((ademco_data->zone % 100) / 10) + '0';
*p++ = (uint8_t)(ademco_data->zone % 10) + '0';
*p++ = ']';
// assert(p - (char*)buf == ADEMCO_PACKET_DATA_SEGMENT_FULL_LEN);
*p = '\0'; // for debug convenience
return ademco_data->raw_len = p - (char*)ademco_data->raw;
}
}
ademco_parse_result_t ademco_parse_data(const ademco_char_t* packet, size_t packet_len,
ademco_data_t* ademco_data,
ademco_parse_error_t* err) {
if (packet_len == ADEMCO_PACKET_DATA_SEGMENT_EMPTY_LEN &&
packet[0] == '[' && packet[1] == ']') {
memset(ademco_data, 0, sizeof(*ademco_data));
strcpy((char*)ademco_data->raw, "[]");
ademco_data->ademco_event = EVENT_INVALID_EVENT;
ademco_data->ademco_id = 0;
ademco_data->gg = 0;
ademco_data->zone = 0;
ademco_data->raw_len = 2;
return ADEMCO_PARSE_RESULT_OK;
} else if (packet_len >= ADEMCO_PACKET_DATA_SEGMENT_FULL_LEN &&
packet[0] == '[' &&
packet[packet_len - 1] == ']') { // [#000000|1400 00 000]
const char* p = packet + 2;
size_t ADEMCO_BUF_MEMORY_MODIFIER acct_len = packet_len - 15;
#if !ADEMCO_USE_UNIQUE_BUF
#if ADEMCO_USE_STATIC_BUF
static
#endif
char ADEMCO_BUF_MEMORY_MODIFIER buf[ADEMCO_PACKET_DATA_SEGMENT_FULL_LEN_MAX] = {0};
#endif
if (acct_len < 6) {
ADEMCO_FILL_PARSE_ERROR(err, p - packet, "acct_len < 6");
return ADEMCO_PARSE_RESULT_ERROR;
} else if (acct_len > ADEMCO_PACKET_ACCT_MAX_LEN) {
ADEMCO_FILL_PARSE_ERROR(err, p - packet, "acct_len > ADEMCO_PACKET_ACCT_MAX_LEN");
return ADEMCO_PARSE_RESULT_ERROR;
}
strncpy(buf, (const char*)p, acct_len);
buf[acct_len] = '\0';
ademco_data->ademco_id = (ademco_id_t)strtoul(buf, NULL, 16);
if (ademco_data->ademco_id > ADEMCO_ID_SENTINEL) {
// ademco_id maybe acct like "861234567890"
ademco_data->ademco_id = ADEMCO_ID_INVALID;
}
p += acct_len;
if (*p++ != '|') {
ADEMCO_FILL_PARSE_ERROR(err, p - packet, "*p++ != '|'");
return ADEMCO_PARSE_RESULT_ERROR;
}
strncpy(buf, (const char*)p, 4);
buf[4] = '\0';
ademco_data->ademco_event = (ademco_event_t)(atoi(buf));
p += 4;
if (*p++ != ' ') {
ADEMCO_FILL_PARSE_ERROR(err, p - packet, "*p++ != ' '");
return ADEMCO_PARSE_RESULT_ERROR;
}
if (*p == 'E' && *(p + 1) == 'E') {
ademco_data->gg = 0xEE;
} else if (*p == 'C' && *(p + 1) == 'C') {
ademco_data->gg = 0xCC;
} else {
ademco_data->gg = (ademco_gg_t)((*p - '0') * 10 + (*(p + 1) - '0'));
}
p += 2;
if (*p++ != ' ') {
ADEMCO_FILL_PARSE_ERROR(err, p - packet, "*p++ != ' '");
return ADEMCO_PARSE_RESULT_ERROR;
}
strncpy(buf, (const char*)p, 3);
buf[3] = '\0';
ademco_data->zone = (ademco_zone_t)atoi(buf);
if (ademco_data->raw != packet) {
memcpy(ademco_data->raw, packet, packet_len);
}
ademco_data->raw_len = packet_len;
return ADEMCO_PARSE_RESULT_OK;
}
ADEMCO_FILL_PARSE_ERROR(err, 0, "dig in source code for more info");
return ADEMCO_PARSE_RESULT_ERROR;
}
size_t ademco_data_to_congwin_fe100(ademco_char_t* fe100, size_t fe100_len,
const ademco_data_t* ademco_data) {
if (ademco_data->raw_len == ADEMCO_PACKET_DATA_SEGMENT_EMPTY_LEN) {
if (fe100_len < 2) return 0;
fe100[0] = '\n';
fe100[1] = '\r';
return 2;
} else if (ademco_data->raw_len >= ADEMCO_PACKET_DATA_SEGMENT_FULL_LEN) {
char* p;
if (fe100_len < CONGWIN_FE100_PACKET_LEN) return 0;
p = fe100;
*p++ = '\n';
*p++ = ' ';
*p++ = '0'; // RG
*p++ = '0';
*p++ = ' ';
*p++ = (uint8_t)(ademco_data->ademco_id / 10000000) + '0';
*p++ = (uint8_t)((ademco_data->ademco_id % 10000000) / 1000000) + '0';
*p++ = (uint8_t)((ademco_data->ademco_id % 1000000) / 100000) + '0';
*p++ = (uint8_t)((ademco_data->ademco_id % 100000) / 10000) + '0';
*p++ = (uint8_t)((ademco_data->ademco_id % 10000) / 1000) + '0';
*p++ = (uint8_t)((ademco_data->ademco_id % 1000) / 100) + '0';
*p++ = (uint8_t)((ademco_data->ademco_id % 100) / 10) + '0';
*p++ = (uint8_t)(ademco_data->ademco_id % 10) + '0';
*p++ = ' '; // 18
*p++ = '1';
*p++ = '8';
*p++ = ' ';
// E for open, R for close
if (ademco_data->ademco_event / 1000 == 1 ||
ademco_data->ademco_event == EVENT_ARM_STAY)
*p++ = 'E';
else
*p++ = 'R';
if (ademco_data->ademco_event == EVENT_OFFLINE) {
*p++ = '3';
*p++ = 'B';
*p++ = '2';
} else if (ademco_data->ademco_event == EVENT_ONLINE) {
*p++ = '3';
*p++ = 'B';
*p++ = '0';
} else {
*p++ = '0' + (uint8_t)((ademco_data->ademco_event % 1000) / 100);
*p++ = '0' + ((ademco_data->ademco_event % 100) / 10);
*p++ = '0' + (ademco_data->ademco_event % 10);
}
*p++ = ' '; // FE100, gg === 0
*p++ = '0';
*p++ = '0';
*p++ = ' ';
if (ademco_is_machine_status_event(ademco_data->ademco_event))
*p++ = 'U'; // user event
else
*p++ = 'C'; // zone event
*p++ = '0' + (uint8_t)((ademco_data->zone % 1000) / 100);
*p++ = '0' + ((ademco_data->zone % 100) / 10);
*p++ = '0' + (ademco_data->zone % 10);
*p++ = ' ';
*p++ = '\r';
return p - fe100;
}
return 0;
}
void ademco_xdata_init(ademco_xdata_t* xdat) {
memset(xdat, 0, sizeof *xdat);
}
bool ademco_xdata_convert(ademco_xdata_t* xdat,
ademco_xdata_length_format_t xlf) {
#if !ADEMCO_USE_UNIQUE_BUF
#if ADEMCO_USE_STATIC_BUF
static
#endif
char ADEMCO_BUF_MEMORY_MODIFIER buf[ADEMCO_PACKET_XDATA_MAX_LEN];
#endif
size_t ADEMCO_BUF_MEMORY_MODIFIER len;
if (!xdat) {
return false;
}
if (xdat->lenghFormat == xlf) {
return false;
}
len = ademco_xdata_get_valid_content_len(xdat);
if (xlf == ADEMCO_XDATA_LENGTH_FMT_TWO_HEX) {
if (len + 6 > ADEMCO_PACKET_XDATA_MAX_LEN)
return false;
xdat->raw[5 + ademco_xdata_get_valid_content_len(xdat)] = '\0';
len = ademco_hex_str_to_array((uint8_t*)buf,
ademco_xdata_get_valid_content_addr(xdat),
0x0F);
ademco_make_xdata(xdat,
ADEMCO_XDATA_LENGTH_FMT_TWO_HEX,
ADEMCO_XDATA_TRANSFORM_AS_IS,
buf, len);
} else {
memcpy(buf, ademco_xdata_get_valid_content_addr(xdat), len);
ademco_make_xdata(xdat,
ADEMCO_XDATA_LENGTH_FMT_FOUR_DECIMAL,
ADEMCO_XDATA_TRANSFORM_TO_ASCII,
buf, len);
}
return true;
}
#ifdef SWIG
static
#endif
const char* ademco_xdata_get_valid_content_addr(const ademco_xdata_t* xdat) {
if (xdat->lenghFormat == ADEMCO_XDATA_LENGTH_FMT_TWO_HEX &&
xdat->raw_len > 4)
return xdat->raw + 3;
else if (xdat->lenghFormat == ADEMCO_XDATA_LENGTH_FMT_FOUR_DECIMAL &&
xdat->raw_len > 6)
return xdat->raw + 5;
else
return NULL;
}
bool ademco_xdata_memeq(const ademco_xdata_t* xdat,
const void* buf, size_t buf_len) {
const void* p;
if (ademco_xdata_get_valid_content_len(xdat) != buf_len) return false;
p = ademco_xdata_get_valid_content_addr(xdat);
if (!p) return 0;
return memcmp(p, buf, buf_len) == 0;
}
size_t ademco_xdata_get_valid_content_len(const ademco_xdata_t* xdat) {
if (!xdat) return 0;
if (xdat->lenghFormat == ADEMCO_XDATA_LENGTH_FMT_TWO_HEX &&
xdat->raw_len > 4)
return xdat->raw_len - 4;
else if (xdat->lenghFormat == ADEMCO_XDATA_LENGTH_FMT_FOUR_DECIMAL &&
xdat->raw_len > 6)
return xdat->raw_len - 6;
else
return 0;
}
size_t ademco_xdata_copy(ademco_xdata_t* dst, const ademco_xdata_t* src) {
if (dst && src && src->raw_len > 0) {
memcpy(dst, src, sizeof(ademco_xdata_t));
return src->raw_len;
}
return 0;
}
bool ademco_make_xdata(ademco_xdata_t* xdat,
ademco_xdata_length_format_t xlf,
ademco_xdata_transform_t xtr,
const ademco_char_t* content, size_t len) {
#if !ADEMCO_USE_UNIQUE_BUF
#if ADEMCO_USE_STATIC_BUF
static
#endif
char ADEMCO_BUF_MEMORY_MODIFIER buf[ADEMCO_PACKET_XDATA_MAX_LEN];
#endif
uint32_t ADEMCO_BUF_MEMORY_MODIFIER translen;
len &= 0xFFFF;
translen = len & 0xFFFF;
if (xtr == ADEMCO_XDATA_TRANSFORM_AS_IS)
memcpy(buf, content, len);
else if (len * 2 < ADEMCO_PACKET_XDATA_MAX_LEN)
translen = ademco_hex_array_to_str(buf,
(const uint8_t*)content, len) &
0xFFFF;
else
return false;
xdat->lenghFormat = xlf;
if (xlf == ADEMCO_XDATA_LENGTH_FMT_TWO_HEX &&
translen + 4 < ADEMCO_PACKET_XDATA_MAX_LEN) {
ademco_char_t* p = xdat->raw;
*p++ = '[';
*p++ = (translen & 0xFFFF) >> 8;
*p++ = (translen & 0xFFFF) & 0xFF;
memcpy(p, buf, translen);
p += translen;
*p++ = ']';
xdat->raw_len = p - xdat->raw;
return true;
} else if (xlf == ADEMCO_XDATA_LENGTH_FMT_FOUR_DECIMAL &&
translen + 6 < ADEMCO_PACKET_XDATA_MAX_LEN) {
ademco_char_t* p = xdat->raw;
*p++ = '[';
p += ademco_dec_to_hex_str(p, translen, 4);
memcpy(p, buf, translen);
p += translen;
*p++ = ']';
xdat->raw_len = p - xdat->raw;
return true;
} else
return false;
}
bool ademco_is_valid_packet_id(const char* standard, const char* id, size_t len) {
while (*standard && len--) {
if (*standard++ != *id++)
return false;
}
return true;
}
ademco_packet_id_t ademco_packet_id_from_string(const char* id, size_t len) {
if (ademco_is_valid_packet_id(ADEMCO_PACKET_ID_NULL, id, len))
return AID_NULL;
if (ademco_is_valid_packet_id(ADEMCO_PACKET_ID_ACK, id, len))
return AID_ACK;
if (ademco_is_valid_packet_id(ADEMCO_PACKET_ID_HB, id, len))
return AID_HB;
if (ademco_is_valid_packet_id(ADEMCO_PACKET_ID_ADM_CID, id, len))
return AID_ADM_CID;
if (ademco_is_valid_packet_id(ADEMCO_PACKET_ID_NAK, id, len))
return AID_NAK;
if (ademco_is_valid_packet_id(ADEMCO_PACKET_ID_DUH, id, len))
return AID_DUH;
if (ademco_is_valid_packet_id(ADEMCO_PACKET_ID_MOD_REG, id, len))
return AID_MOD_REG;
if (ademco_is_valid_packet_id(ADEMCO_PACKET_ID_REG_RSP, id, len))
return AID_REG_RSP;
return AID_INVALID;
}
const char* ademco_packet_id_to_string(ademco_packet_id_t id) {
switch (id) {
case AID_NULL:
return ADEMCO_PACKET_ID_NULL;
case AID_ACK:
return ADEMCO_PACKET_ID_ACK;
case AID_NAK:
return ADEMCO_PACKET_ID_NAK;
case AID_DUH:
return ADEMCO_PACKET_ID_DUH;
case AID_HB:
return ADEMCO_PACKET_ID_HB;
case AID_ADM_CID:
return ADEMCO_PACKET_ID_ADM_CID;
case AID_MOD_REG:
return ADEMCO_PACKET_ID_MOD_REG;
case AID_REG_RSP:
return ADEMCO_PACKET_ID_REG_RSP;
default:
return NULL;
}
}
static void getNowTimestamp(char* buf) {
time_t ADEMCO_BUF_MEMORY_MODIFIER now = time(NULL);
#if ADEMCO_DISABLE_GMTIME
struct tm ADEMCO_BUF_MEMORY_MODIFIER tm = {0};
_localtime_r(&now, &tm);
#else
struct tm ADEMCO_BUF_MEMORY_MODIFIER tm = *localtime(&now);
#endif // ADEMCO_DISABLE_GMTIME
tm.tm_mon += 1;
tm.tm_year += 1900;
*buf++ = '_';
*buf++ = (uint8_t)(tm.tm_hour / 10) + '0';
*buf++ = (uint8_t)(tm.tm_hour % 10) + '0';
*buf++ = ':';
*buf++ = (uint8_t)(tm.tm_min / 10) + '0';
*buf++ = (uint8_t)(tm.tm_min % 10) + '0';
*buf++ = ':';
*buf++ = (uint8_t)(tm.tm_sec / 10) + '0';
*buf++ = (uint8_t)(tm.tm_sec % 10) + '0';
*buf++ = ',';
*buf++ = (uint8_t)(tm.tm_mon / 10) + '0';
*buf++ = (uint8_t)(tm.tm_mon % 10) + '0';
*buf++ = '-';
*buf++ = (uint8_t)(tm.tm_mday / 10) + '0';
*buf++ = (uint8_t)(tm.tm_mday % 10) + '0';
*buf++ = '-';
*buf++ = (uint8_t)(tm.tm_year / 1000) + '0';
*buf++ = (uint8_t)((tm.tm_year % 1000) / 100) + '0';
*buf++ = (uint8_t)((tm.tm_year % 100) / 10) + '0';
*buf++ = (uint8_t)(tm.tm_year % 10) + '0';
*buf = '\0';
}
static void getGmtTimestamp(char* buf) {
#ifdef ADEMCO_DISABLE_GMTIME
getNowTimestamp(buf);
#else
time_t ADEMCO_BUF_MEMORY_MODIFIER now = time(NULL);
struct tm ADEMCO_BUF_MEMORY_MODIFIER tm = *gmtime(&now);
tm.tm_mon += 1;
tm.tm_year += 1900;
*buf++ = '_';
*buf++ = (uint8_t)(tm.tm_hour / 10) + '0';
*buf++ = (uint8_t)(tm.tm_hour % 10) + '0';
*buf++ = ':';
*buf++ = (uint8_t)(tm.tm_min / 10) + '0';
*buf++ = (uint8_t)(tm.tm_min % 10) + '0';
*buf++ = ':';
*buf++ = (uint8_t)(tm.tm_sec / 10) + '0';
*buf++ = (uint8_t)(tm.tm_sec % 10) + '0';
*buf++ = ',';
*buf++ = (uint8_t)(tm.tm_mon / 10) + '0';
*buf++ = (uint8_t)(tm.tm_mon % 10) + '0';
*buf++ = '-';
*buf++ = (uint8_t)(tm.tm_mday / 10) + '0';
*buf++ = (uint8_t)(tm.tm_mday % 10) + '0';
*buf++ = '-';
*buf++ = (uint8_t)(tm.tm_year / 1000) + '0';
*buf++ = (uint8_t)((tm.tm_year % 1000) / 100) + '0';
*buf++ = (uint8_t)((tm.tm_year % 100) / 10) + '0';
*buf++ = (uint8_t)(tm.tm_year % 10) + '0';
*buf = '\0';
#endif
}
size_t ademco_make_empty_data_packet(ademco_char_t* dst_buff, size_t len,
const char* id, uint16_t seq,
const char* acct, ademco_id_t ademco_id) {
#if !ADEMCO_USE_UNIQUE_BUF
#if ADEMCO_USE_STATIC_BUF
static
#endif
char ADEMCO_BUF_MEMORY_MODIFIER buf[ADEMCO_PACKET_MAX_LEN];
#endif
char ADEMCO_BUF_MEMORY_MODIFIER* p = buf;
char ADEMCO_BUF_MEMORY_MODIFIER* pcrc = buf + 1;
char ADEMCO_BUF_MEMORY_MODIFIER* plen = buf + 5;
char ADEMCO_BUF_MEMORY_MODIFIER* pid = buf + 9;
uint16_t ADEMCO_BUF_MEMORY_MODIFIER crc;
uint16_t ADEMCO_BUF_MEMORY_MODIFIER packet_len, ademco_len;
uint8_t i;
buf[0] = ADEMCO_PACKET_PREFIX;
memcpy(pid, id, strlen(id));
p = pid + strlen(id);
seq %= 10000;
*p++ = (uint8_t)(seq / 1000) + '0';
*p++ = (uint8_t)((seq % 1000) / 100) + '0';
*p++ = (uint8_t)((seq % 100) / 10) + '0';
*p++ = (uint8_t)(seq % 10) + '0';
memcpy(p, ADEMCO_RRCVR_DEFAULT, strlen(ADEMCO_RRCVR_DEFAULT));
p += strlen(ADEMCO_RRCVR_DEFAULT);
memcpy(p, ADEMCO_LPREF_DEFAULT, strlen(ADEMCO_LPREF_DEFAULT));
p += strlen(ADEMCO_LPREF_DEFAULT);
*p++ = '#';
if (acct && acct[0]) {
for (i = 0; i < ADEMCO_PACKET_ACCT_MAX_LEN && *acct; i++) {
*p++ = *acct++;
}
} else {
p += ademco_dec_to_hex_str(p, ademco_id, 6);
}
*p++ = '[';
*p++ = ']';
getNowTimestamp((char*)p);
p += ADEMCO_PACKET_TIMESTAMP_LEN;
*p++ = ADEMCO_PACKET_SUFIX;
packet_len = (p - buf) & 0xFFFF;
// \n crc len \r
ademco_len = packet_len - 1 - 4 - 4 - 1;
ademco_dec_to_hex_str(plen, ademco_len, 4);
crc = ademco_crc16(pid, ademco_len);
ademco_dec_to_hex_str(pcrc, crc, 4);
if (dst_buff != NULL && len > packet_len) {
memcpy(dst_buff, buf, packet_len);
dst_buff[packet_len] = '\0'; // for debug convenience
return packet_len;
} else
return 0;
}
size_t ademco_make_adm_empty_data_packet(ademco_char_t* dst_buff, size_t len,
const char* id, uint16_t seq,
const char* acct, ademco_id_t ademco_id) {
#if !ADEMCO_USE_UNIQUE_BUF
#if ADEMCO_USE_STATIC_BUF
static
#endif
char ADEMCO_BUF_MEMORY_MODIFIER buf[ADEMCO_PACKET_MAX_LEN];
#endif
char ADEMCO_BUF_MEMORY_MODIFIER* p = buf;
char ADEMCO_BUF_MEMORY_MODIFIER* pcrc = buf + 1;
char ADEMCO_BUF_MEMORY_MODIFIER* plen = buf + 5;
char ADEMCO_BUF_MEMORY_MODIFIER* pid = buf + 9;
uint16_t ADEMCO_BUF_MEMORY_MODIFIER crc;
uint16_t ADEMCO_BUF_MEMORY_MODIFIER packet_len, ademco_len;
uint8_t i;
buf[0] = ADEMCO_PACKET_PREFIX;
memcpy(pid, id, strlen(id));
p = pid + strlen(id);
seq %= 10000;
*p++ = (uint8_t)(seq / 1000) + '0';
*p++ = (uint8_t)((seq % 1000) / 100) + '0';
*p++ = (uint8_t)((seq % 100) / 10) + '0';
*p++ = (uint8_t)(seq % 10) + '0';
p += 4;
memcpy(p, ADEMCO_RRCVR_DEFAULT, strlen(ADEMCO_RRCVR_DEFAULT));
p += strlen(ADEMCO_RRCVR_DEFAULT);
memcpy(p, ADEMCO_LPREF_DEFAULT, strlen(ADEMCO_LPREF_DEFAULT));
p += strlen(ADEMCO_LPREF_DEFAULT);
*p++ = '#';
if (acct && acct[0]) {
for (i = 0; i < ADEMCO_PACKET_ACCT_MAX_LEN && *acct; i++) {
*p++ = *acct++;
}
} else {
p += ademco_dec_to_hex_str(p, ademco_id, 6);
}
*p++ = '[';
*p++ = ']';
getGmtTimestamp((char*)p);
p += ADEMCO_PACKET_TIMESTAMP_LEN;
*p++ = ADEMCO_PACKET_SUFIX;
packet_len = (p - buf) & 0xFFFF;
// \n crc len \r
ademco_len = packet_len - 1 - 4 - 4 - 1;
ademco_dec_to_hex_str(plen, ademco_len, 4);
crc = ademco_crc16(pid, ademco_len);
ademco_dec_to_hex_str(pcrc, crc, 4);
if (dst_buff != NULL && len > packet_len) {
memcpy(dst_buff, buf, packet_len);
dst_buff[packet_len] = '\0'; // for debug convenience
return packet_len;
} else
return 0;
}
size_t ademco_make_null_packet(ademco_char_t* buf, size_t len,
uint16_t seq, const char* acct,
ademco_id_t ademco_id) {
return ademco_make_empty_data_packet(buf, len,
ADEMCO_PACKET_ID_NULL,
seq, acct, ademco_id);
}
size_t ademco_make_ack_packet(ademco_char_t* buf, size_t len,
uint16_t seq, const char* acct,
ademco_id_t ademco_id) {
return ademco_make_empty_data_packet(buf, len,
ADEMCO_PACKET_ID_ACK,
seq, acct, ademco_id);
}
size_t ademco_make_nak_packet(ademco_char_t* buf, size_t len,
uint16_t seq, const char* acct,
ademco_id_t ademco_id) {
return ademco_make_empty_data_packet(buf, len,
ADEMCO_PACKET_ID_NAK,
seq, acct, ademco_id);
}
size_t ademco_make_hb_packet(ademco_char_t* dst_buff, size_t len,
uint16_t seq, const char* acct,
ademco_id_t ademco_id, ademco_event_t ademco_event,
ademco_gg_t gg, ademco_zone_t zone,
const ademco_xdata_t* xdat) {
#if !ADEMCO_USE_UNIQUE_BUF
#if ADEMCO_USE_STATIC_BUF