-
Notifications
You must be signed in to change notification settings - Fork 6.8k
/
osdp_cp.c
1009 lines (928 loc) · 23.9 KB
/
osdp_cp.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
/*
* Copyright (c) 2020 Siddharth Chandrasekaran <siddharth@embedjournal.com>
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <stdlib.h>
#include <logging/log.h>
LOG_MODULE_DECLARE(osdp, CONFIG_OSDP_LOG_LEVEL);
#include "osdp_common.h"
#define TAG "CP: "
#define OSDP_PD_POLL_TIMEOUT_MS (1000 / CONFIG_OSDP_PD_POLL_RATE)
#define OSDP_CMD_RETRY_WAIT_MS (CONFIG_OSDP_CMD_RETRY_WAIT_SEC * 1000)
#ifdef CONFIG_OSDP_SC_ENABLED
#define OSDP_PD_SC_RETRY_MS (CONFIG_OSDP_SC_RETRY_WAIT_SEC * 1000)
#endif
#define CMD_POLL_LEN 1
#define CMD_LSTAT_LEN 1
#define CMD_ISTAT_LEN 1
#define CMD_OSTAT_LEN 1
#define CMD_RSTAT_LEN 1
#define CMD_ID_LEN 2
#define CMD_CAP_LEN 2
#define CMD_DIAG_LEN 2
#define CMD_OUT_LEN 5
#define CMD_LED_LEN 15
#define CMD_BUZ_LEN 6
#define CMD_TEXT_LEN 7 /* variable length command */
#define CMD_COMSET_LEN 6
#define CMD_KEYSET_LEN 19
#define CMD_CHLNG_LEN 9
#define CMD_SCRYPT_LEN 17
#define REPLY_ACK_DATA_LEN 0
#define REPLY_PDID_DATA_LEN 12
#define REPLY_PDCAP_ENTITY_LEN 3
#define REPLY_LSTATR_DATA_LEN 2
#define REPLY_RSTATR_DATA_LEN 1
#define REPLY_COM_DATA_LEN 5
#define REPLY_NAK_DATA_LEN 1
#define REPLY_CCRYPT_DATA_LEN 32
#define REPLY_RMAC_I_DATA_LEN 16
#define REPLY_KEYPPAD_DATA_LEN 2 /* variable length command */
#define REPLY_RAW_DATA_LEN 4 /* variable length command */
#define REPLY_FMT_DATA_LEN 3 /* variable length command */
#define REPLY_BUSY_DATA_LEN 0
#define OSDP_CP_ERR_GENERIC -1
#define OSDP_CP_ERR_NO_DATA 1
#define OSDP_CP_ERR_RETRY_CMD 2
#define OSDP_CP_ERR_CAN_YIELD 3
#define OSDP_CP_ERR_INPROG 4
int osdp_extract_address(int *address)
{
int pd_offset = 0;
unsigned long addr;
char *tok, *s1, *s2, addr_buf[32 * CONFIG_OSDP_NUM_CONNECTED_PD];
strncpy(addr_buf, CONFIG_OSDP_PD_ADDRESS_LIST, sizeof(addr_buf) - 1);
addr_buf[sizeof(addr_buf) - 1] = '\0';
tok = strtok_r(addr_buf, ", ", &s1);
while (tok && pd_offset < CONFIG_OSDP_NUM_CONNECTED_PD) {
addr = strtoul(tok, &s2, 10);
if (*s2 != '\0') { /* tok must be number-ish */
return -1;
}
address[pd_offset] = addr;
pd_offset++;
tok = strtok_r(NULL, ", ", &s1);
}
return (pd_offset == CONFIG_OSDP_NUM_CONNECTED_PD) ? 0 : -1;
}
/**
* Returns:
* +ve: length of command
* -ve: error
*/
static int cp_build_command(struct osdp_pd *pd, uint8_t *buf, int max_len)
{
struct osdp_cmd *cmd = NULL;
int data_off, i, ret = -1, len = 0;
data_off = osdp_phy_packet_get_data_offset(pd, buf);
#ifdef CONFIG_OSDP_SC_ENABLED
uint8_t *smb = osdp_phy_packet_get_smb(pd, buf);
#endif
buf += data_off;
max_len -= data_off;
if (max_len <= 0) {
return OSDP_CP_ERR_GENERIC;
}
switch (pd->cmd_id) {
case CMD_POLL:
if (max_len < CMD_POLL_LEN) {
break;
}
buf[len++] = pd->cmd_id;
ret = 0;
break;
case CMD_LSTAT:
if (max_len < CMD_LSTAT_LEN) {
break;
}
buf[len++] = pd->cmd_id;
ret = 0;
break;
case CMD_ISTAT:
if (max_len < CMD_ISTAT_LEN) {
break;
}
buf[len++] = pd->cmd_id;
ret = 0;
break;
case CMD_OSTAT:
if (max_len < CMD_OSTAT_LEN) {
break;
}
buf[len++] = pd->cmd_id;
ret = 0;
break;
case CMD_RSTAT:
if (max_len < CMD_RSTAT_LEN) {
break;
}
buf[len++] = pd->cmd_id;
ret = 0;
break;
case CMD_ID:
if (max_len < CMD_ID_LEN) {
break;
}
buf[len++] = pd->cmd_id;
buf[len++] = 0x00;
ret = 0;
break;
case CMD_CAP:
if (max_len < CMD_CAP_LEN) {
break;
}
buf[len++] = pd->cmd_id;
buf[len++] = 0x00;
ret = 0;
break;
case CMD_DIAG:
if (max_len < CMD_DIAG_LEN) {
break;
}
buf[len++] = pd->cmd_id;
buf[len++] = 0x00;
ret = 0;
break;
case CMD_OUT:
if (max_len < CMD_OUT_LEN) {
break;
}
cmd = (struct osdp_cmd *)pd->cmd_data;
buf[len++] = pd->cmd_id;
buf[len++] = cmd->output.output_no;
buf[len++] = cmd->output.control_code;
buf[len++] = BYTE_0(cmd->output.timer_count);
buf[len++] = BYTE_1(cmd->output.timer_count);
ret = 0;
break;
case CMD_LED:
if (max_len < CMD_LED_LEN) {
break;
}
cmd = (struct osdp_cmd *)pd->cmd_data;
buf[len++] = pd->cmd_id;
buf[len++] = cmd->led.reader;
buf[len++] = cmd->led.led_number;
buf[len++] = cmd->led.temporary.control_code;
buf[len++] = cmd->led.temporary.on_count;
buf[len++] = cmd->led.temporary.off_count;
buf[len++] = cmd->led.temporary.on_color;
buf[len++] = cmd->led.temporary.off_color;
buf[len++] = BYTE_0(cmd->led.temporary.timer_count);
buf[len++] = BYTE_1(cmd->led.temporary.timer_count);
buf[len++] = cmd->led.permanent.control_code;
buf[len++] = cmd->led.permanent.on_count;
buf[len++] = cmd->led.permanent.off_count;
buf[len++] = cmd->led.permanent.on_color;
buf[len++] = cmd->led.permanent.off_color;
ret = 0;
break;
case CMD_BUZ:
if (max_len < CMD_BUZ_LEN) {
break;
}
cmd = (struct osdp_cmd *)pd->cmd_data;
buf[len++] = pd->cmd_id;
buf[len++] = cmd->buzzer.reader;
buf[len++] = cmd->buzzer.control_code;
buf[len++] = cmd->buzzer.on_count;
buf[len++] = cmd->buzzer.off_count;
buf[len++] = cmd->buzzer.rep_count;
ret = 0;
break;
case CMD_TEXT:
cmd = (struct osdp_cmd *)pd->cmd_data;
if (max_len < (CMD_TEXT_LEN + cmd->text.length)) {
break;
}
buf[len++] = pd->cmd_id;
buf[len++] = cmd->text.reader;
buf[len++] = cmd->text.control_code;
buf[len++] = cmd->text.temp_time;
buf[len++] = cmd->text.offset_row;
buf[len++] = cmd->text.offset_col;
buf[len++] = cmd->text.length;
for (i = 0; i < cmd->text.length; i++) {
buf[len++] = cmd->text.data[i];
}
ret = 0;
break;
case CMD_COMSET:
if (max_len < CMD_COMSET_LEN) {
break;
}
cmd = (struct osdp_cmd *)pd->cmd_data;
buf[len++] = pd->cmd_id;
buf[len++] = cmd->comset.address;
buf[len++] = BYTE_0(cmd->comset.baud_rate);
buf[len++] = BYTE_1(cmd->comset.baud_rate);
buf[len++] = BYTE_2(cmd->comset.baud_rate);
buf[len++] = BYTE_3(cmd->comset.baud_rate);
ret = 0;
break;
#ifdef CONFIG_OSDP_SC_ENABLED
case CMD_KEYSET:
if (!ISSET_FLAG(pd, PD_FLAG_SC_ACTIVE)) {
LOG_ERR(TAG "Cannot perform KEYSET without SC!");
return -1;
}
if (max_len < CMD_KEYSET_LEN) {
break;
}
buf[len++] = pd->cmd_id;
buf[len++] = 1; /* key type (1: SCBK) */
buf[len++] = 16; /* key length in bytes */
osdp_compute_scbk(pd, buf + len);
len += 16;
ret = 0;
break;
case CMD_CHLNG:
if (smb == NULL || max_len < CMD_CHLNG_LEN) {
break;
}
osdp_fill_random(pd->sc.cp_random, 8);
smb[0] = 3; /* length */
smb[1] = SCS_11; /* type */
smb[2] = ISSET_FLAG(pd, PD_FLAG_SC_USE_SCBKD) ? 0 : 1;
buf[len++] = pd->cmd_id;
for (i = 0; i < 8; i++)
buf[len++] = pd->sc.cp_random[i];
ret = 0;
break;
case CMD_SCRYPT:
if (smb == NULL || max_len < CMD_SCRYPT_LEN) {
break;
}
osdp_compute_cp_cryptogram(pd);
smb[0] = 3; /* length */
smb[1] = SCS_13; /* type */
smb[2] = ISSET_FLAG(pd, PD_FLAG_SC_USE_SCBKD) ? 0 : 1;
buf[len++] = pd->cmd_id;
for (i = 0; i < 16; i++)
buf[len++] = pd->sc.cp_cryptogram[i];
ret = 0;
break;
#endif /* CONFIG_OSDP_SC_ENABLED */
default:
LOG_ERR(TAG "Unknown/Unsupported command %02x", pd->cmd_id);
return OSDP_CP_ERR_GENERIC;
}
#ifdef CONFIG_OSDP_SC_ENABLED
if (smb && (smb[1] > SCS_14) && ISSET_FLAG(pd, PD_FLAG_SC_ACTIVE)) {
/**
* When SC active and current cmd is not a handshake (<= SCS_14)
* then we must set SCS type to 17 if this message has data
* bytes and 15 otherwise.
*/
smb[0] = 2;
smb[1] = (len > 1) ? SCS_17 : SCS_15;
}
#endif /* CONFIG_OSDP_SC_ENABLED */
if (ret < 0) {
LOG_ERR(TAG "Unable to build command %02x", pd->cmd_id);
return OSDP_CP_ERR_GENERIC;
}
return len;
}
static int cp_decode_response(struct osdp_pd *pd, uint8_t *buf, int len)
{
uint32_t temp32;
struct osdp_cp *cp = TO_CTX(pd)->cp;
int i, ret = OSDP_CP_ERR_GENERIC, pos = 0, t1, t2;
if (len < 1) {
LOG_ERR("response must have at least one byte");
return OSDP_CP_ERR_GENERIC;
}
pd->reply_id = buf[pos++];
len--; /* consume reply id from the head */
switch (pd->reply_id) {
case REPLY_ACK:
if (len != REPLY_ACK_DATA_LEN) {
break;
}
ret = 0;
break;
case REPLY_NAK:
if (len != REPLY_NAK_DATA_LEN) {
break;
}
LOG_ERR(TAG "PD replied with NAK code %d", buf[pos]);
ret = 0;
break;
case REPLY_PDID:
if (len != REPLY_PDID_DATA_LEN) {
break;
}
pd->id.vendor_code = buf[pos++];
pd->id.vendor_code |= buf[pos++] << 8;
pd->id.vendor_code |= buf[pos++] << 16;
pd->id.model = buf[pos++];
pd->id.version = buf[pos++];
pd->id.serial_number = buf[pos++];
pd->id.serial_number |= buf[pos++] << 8;
pd->id.serial_number |= buf[pos++] << 16;
pd->id.serial_number |= buf[pos++] << 24;
pd->id.firmware_version = buf[pos++] << 16;
pd->id.firmware_version |= buf[pos++] << 8;
pd->id.firmware_version |= buf[pos++];
ret = 0;
break;
case REPLY_PDCAP:
if ((len % REPLY_PDCAP_ENTITY_LEN) != 0) {
break;
}
while (pos < len) {
t1 = buf[pos++]; /* func_code */
if (t1 > OSDP_PD_CAP_SENTINEL) {
break;
}
pd->cap[t1].function_code = t1;
pd->cap[t1].compliance_level = buf[pos++];
pd->cap[t1].num_items = buf[pos++];
}
/* post-capabilities hooks */
t2 = OSDP_PD_CAP_COMMUNICATION_SECURITY;
if (pd->cap[t2].compliance_level & 0x01) {
SET_FLAG(pd, PD_FLAG_SC_CAPABLE);
} else {
CLEAR_FLAG(pd, PD_FLAG_SC_CAPABLE);
}
ret = 0;
break;
case REPLY_LSTATR:
if (len != REPLY_LSTATR_DATA_LEN) {
break;
}
if (buf[pos++]) {
SET_FLAG(pd, PD_FLAG_TAMPER);
} else {
CLEAR_FLAG(pd, PD_FLAG_TAMPER);
}
if (buf[pos++]) {
SET_FLAG(pd, PD_FLAG_POWER);
} else {
CLEAR_FLAG(pd, PD_FLAG_POWER);
}
ret = 0;
break;
case REPLY_RSTATR:
if (len != REPLY_RSTATR_DATA_LEN) {
break;
}
if (buf[pos++]) {
SET_FLAG(pd, PD_FLAG_R_TAMPER);
} else {
CLEAR_FLAG(pd, PD_FLAG_R_TAMPER);
}
ret = 0;
break;
case REPLY_COM:
if (len != REPLY_COM_DATA_LEN) {
break;
}
t1 = buf[pos++];
temp32 = buf[pos++];
temp32 |= buf[pos++] << 8;
temp32 |= buf[pos++] << 16;
temp32 |= buf[pos++] << 24;
LOG_WRN(TAG "COMSET responded with ID:%d baud:%d", t1, temp32);
pd->address = t1;
pd->baud_rate = temp32;
ret = 0;
break;
case REPLY_KEYPPAD:
if (len < REPLY_KEYPPAD_DATA_LEN) {
break;
}
pos++; /* reader number; skip */
t1 = buf[pos++]; /* key length */
if ((len - REPLY_KEYPPAD_DATA_LEN) != t1) {
break;
}
if (cp->notifier.keypress) {
for (i = 0; i < t1; i++) {
t2 = buf[pos + i]; /* key data */
cp->notifier.keypress(pd->offset, t2);
}
}
ret = 0;
break;
case REPLY_RAW:
if (len < REPLY_RAW_DATA_LEN) {
break;
}
pos++; /* reader number; skip */
t1 = buf[pos++]; /* format */
t2 = buf[pos++]; /* length LSB */
t2 |= buf[pos++] << 8; /* length MSB */
if ((len - REPLY_RAW_DATA_LEN) != t2) {
break;
}
if (cp->notifier.cardread) {
cp->notifier.cardread(pd->offset, t1, buf + pos, t2);
}
ret = 0;
break;
case REPLY_FMT:
if (len < REPLY_FMT_DATA_LEN) {
break;
}
pos++; /* reader number; skip */
pos++; /* skip one byte -- TODO: handle reader direction */
t1 = buf[pos++]; /* Key length */
if ((len - REPLY_FMT_DATA_LEN) != t1) {
break;
}
if (cp->notifier.cardread) {
cp->notifier.cardread(pd->offset, OSDP_CARD_FMT_ASCII,
buf + pos, t1);
}
ret = 0;
break;
case REPLY_BUSY:
/* PD busy; signal upper layer to retry command */
if (len != REPLY_BUSY_DATA_LEN) {
break;
}
ret = OSDP_CP_ERR_RETRY_CMD;
break;
#ifdef CONFIG_OSDP_SC_ENABLED
case REPLY_CCRYPT:
if (len != REPLY_CCRYPT_DATA_LEN) {
break;
}
for (i = 0; i < 8; i++) {
pd->sc.pd_client_uid[i] = buf[pos++];
}
for (i = 0; i < 8; i++) {
pd->sc.pd_random[i] = buf[pos++];
}
for (i = 0; i < 16; i++) {
pd->sc.pd_cryptogram[i] = buf[pos++];
}
osdp_compute_session_keys(TO_CTX(pd));
if (osdp_verify_pd_cryptogram(pd) != 0) {
LOG_ERR(TAG "failed to verify PD_crypt");
return -1;
}
ret = 0;
break;
case REPLY_RMAC_I:
if (len != REPLY_RMAC_I_DATA_LEN) {
break;
}
for (i = 0; i < 16; i++) {
pd->sc.r_mac[i] = buf[pos++];
}
SET_FLAG(pd, PD_FLAG_SC_ACTIVE);
ret = 0;
break;
#endif /* CONFIG_OSDP_SC_ENABLED */
default:
LOG_DBG(TAG "unexpected reply: 0x%02x", pd->reply_id);
return OSDP_CP_ERR_GENERIC;
}
if (ret == OSDP_CP_ERR_GENERIC) {
LOG_ERR(TAG "REPLY %02x for CMD %02x format error!",
pd->cmd_id, pd->reply_id);
return OSDP_CP_ERR_GENERIC;
}
if (pd->cmd_id != CMD_POLL) {
LOG_DBG(TAG "CMD: %02x REPLY: %02x", pd->cmd_id, pd->reply_id);
}
return ret;
}
static int cp_send_command(struct osdp_pd *pd)
{
int ret, len;
/* init packet buf with header */
len = osdp_phy_packet_init(pd, pd->rx_buf, sizeof(pd->rx_buf));
if (len < 0) {
return -1;
}
/* fill command data */
ret = cp_build_command(pd, pd->rx_buf, sizeof(pd->rx_buf));
if (ret < 0) {
return -1;
}
len += ret;
/* finalize packet */
len = osdp_phy_packet_finalize(pd, pd->rx_buf, len, sizeof(pd->rx_buf));
if (len < 0) {
return -1;
}
ret = pd->channel.send(pd->channel.data, pd->rx_buf, len);
if (IS_ENABLED(CONFIG_OSDP_PACKET_TRACE)) {
if (pd->cmd_id != CMD_POLL) {
LOG_DBG(TAG "bytes sent");
osdp_dump(NULL, pd->rx_buf, len);
}
}
return (ret == len) ? 0 : -1;
}
static int cp_process_reply(struct osdp_pd *pd)
{
uint8_t *buf;
int rec_bytes, ret, max_len;
buf = pd->rx_buf + pd->rx_buf_len;
max_len = sizeof(pd->rx_buf) - pd->rx_buf_len;
rec_bytes = pd->channel.recv(pd->channel.data, buf, max_len);
if (rec_bytes <= 0) { /* No data received */
return OSDP_CP_ERR_NO_DATA;
}
pd->rx_buf_len += rec_bytes;
if (IS_ENABLED(CONFIG_OSDP_PACKET_TRACE)) {
if (pd->cmd_id != CMD_POLL) {
LOG_DBG(TAG "bytes received");
osdp_dump(NULL, pd->rx_buf, pd->rx_buf_len);
}
}
/* Valid OSDP packet in buffer */
ret = osdp_phy_decode_packet(pd, pd->rx_buf, pd->rx_buf_len);
if (ret == OSDP_ERR_PKT_FMT) {
return -1; /* fatal errors */
} else if (ret == OSDP_ERR_PKT_WAIT) {
/* rx_buf_len != pkt->len; wait for more data */
return OSDP_CP_ERR_NO_DATA;
} else if (ret == OSDP_ERR_PKT_SKIP) {
/* soft fail - discard this message */
pd->rx_buf_len = 0;
if (pd->channel.flush) {
pd->channel.flush(pd->channel.data);
}
return OSDP_CP_ERR_NO_DATA;
}
pd->rx_buf_len = ret;
return cp_decode_response(pd, pd->rx_buf, pd->rx_buf_len);
}
static void cp_flush_command_queue(struct osdp_pd *pd)
{
struct osdp_cmd *cmd;
while (osdp_cmd_dequeue(pd, &cmd) == 0) {
osdp_cmd_free(pd, cmd);
}
}
static inline void cp_set_offline(struct osdp_pd *pd)
{
CLEAR_FLAG(pd, PD_FLAG_SC_ACTIVE);
pd->state = OSDP_CP_STATE_OFFLINE;
pd->tstamp = osdp_millis_now();
}
static inline void cp_reset_state(struct osdp_pd *pd)
{
pd->state = OSDP_CP_STATE_INIT;
osdp_phy_state_reset(pd);
}
static inline void cp_set_state(struct osdp_pd *pd, enum osdp_cp_state_e state)
{
pd->state = state;
CLEAR_FLAG(pd, PD_FLAG_AWAIT_RESP);
}
static void cp_reset_channel(struct osdp_pd *pd)
{
pd->rx_buf_len = 0;
if (pd->channel.flush) {
pd->channel.flush(pd->channel.data);
}
}
/**
* Note: This method must not dequeue cmd unless it reaches an invalid state.
*/
static int cp_phy_state_update(struct osdp_pd *pd)
{
int ret = OSDP_CP_ERR_INPROG, tmp;
struct osdp_cmd *cmd = NULL;
switch (pd->phy_state) {
case OSDP_CP_PHY_STATE_ERR_WAIT:
ret = OSDP_CP_ERR_GENERIC;
break;
case OSDP_CP_PHY_STATE_IDLE:
if (osdp_cmd_dequeue(pd, &cmd)) {
ret = 0;
break;
}
pd->cmd_id = cmd->id;
memcpy(pd->cmd_data, cmd, sizeof(struct osdp_cmd));
osdp_cmd_free(pd, cmd);
cp_reset_channel(pd);
/* fall-thru */
case OSDP_CP_PHY_STATE_SEND_CMD:
if ((cp_send_command(pd)) < 0) {
LOG_ERR(TAG "send command error");
pd->phy_state = OSDP_CP_PHY_STATE_ERR;
ret = OSDP_CP_ERR_GENERIC;
break;
}
pd->phy_state = OSDP_CP_PHY_STATE_REPLY_WAIT;
pd->rx_buf_len = 0; /* reset buf_len for next use */
pd->phy_tstamp = osdp_millis_now();
break;
case OSDP_CP_PHY_STATE_REPLY_WAIT:
tmp = cp_process_reply(pd);
if (tmp == 0) { /* success */
pd->phy_state = OSDP_CP_PHY_STATE_CLEANUP;
break;
}
if (tmp == OSDP_CP_ERR_RETRY_CMD) {
LOG_INF(TAG "PD busy; retry last command");
pd->phy_tstamp = osdp_millis_now();
pd->phy_state = OSDP_CP_PHY_STATE_WAIT;
ret = 2;
break;
}
if (tmp == OSDP_CP_ERR_GENERIC) {
pd->phy_state = OSDP_CP_PHY_STATE_ERR;
break;
}
if (osdp_millis_since(pd->phy_tstamp) > OSDP_RESP_TOUT_MS) {
LOG_ERR(TAG "CMD: %02x - response timeout", pd->cmd_id);
pd->phy_state = OSDP_CP_PHY_STATE_ERR;
}
break;
case OSDP_CP_PHY_STATE_WAIT:
if (osdp_millis_since(pd->phy_tstamp) < OSDP_CMD_RETRY_WAIT_MS) {
break;
}
pd->phy_state = OSDP_CP_PHY_STATE_IDLE;
break;
case OSDP_CP_PHY_STATE_ERR:
cp_reset_channel(pd);
cp_flush_command_queue(pd);
pd->phy_state = OSDP_CP_PHY_STATE_ERR_WAIT;
ret = OSDP_CP_ERR_GENERIC;
break;
case OSDP_CP_PHY_STATE_CLEANUP:
pd->phy_state = OSDP_CP_PHY_STATE_IDLE;
ret = OSDP_CP_ERR_CAN_YIELD; /* in between commands */
break;
}
return ret;
}
/**
* Returns:
* 0: nothing done
* 1: dispatched
* -1: error
*/
static int cp_cmd_dispatcher(struct osdp_pd *pd, int cmd)
{
struct osdp_cmd *c;
if (ISSET_FLAG(pd, PD_FLAG_AWAIT_RESP)) {
CLEAR_FLAG(pd, PD_FLAG_AWAIT_RESP);
return 0;
}
c = osdp_cmd_alloc(pd);
if (c == NULL) {
return OSDP_CP_ERR_GENERIC;
}
c->id = cmd;
osdp_cmd_enqueue(pd, c);
SET_FLAG(pd, PD_FLAG_AWAIT_RESP);
return OSDP_CP_ERR_INPROG;
}
static int state_update(struct osdp_pd *pd)
{
int phy_state, soft_fail;
phy_state = cp_phy_state_update(pd);
if (phy_state == OSDP_CP_ERR_INPROG ||
phy_state == OSDP_CP_ERR_CAN_YIELD) {
return OSDP_CP_ERR_GENERIC;
}
/* Certain states can fail without causing PD offline */
soft_fail = (pd->state == OSDP_CP_STATE_SC_CHLNG);
/* phy state error -- cleanup */
if (pd->state != OSDP_CP_STATE_OFFLINE &&
phy_state == OSDP_CP_ERR_GENERIC && soft_fail == 0) {
cp_set_offline(pd);
}
/* command queue is empty and last command was successful */
switch (pd->state) {
case OSDP_CP_STATE_ONLINE:
#ifdef CONFIG_OSDP_SC_ENABLED
if (ISSET_FLAG(pd, PD_FLAG_SC_ACTIVE) == false &&
ISSET_FLAG(pd, PD_FLAG_SC_CAPABLE) == true &&
osdp_millis_since(pd->sc_tstamp) > OSDP_PD_SC_RETRY_MS) {
LOG_INF("retry SC after retry timeout");
cp_set_state(pd, OSDP_CP_STATE_SC_INIT);
break;
}
#endif
if (osdp_millis_since(pd->tstamp) < OSDP_PD_POLL_TIMEOUT_MS) {
break;
}
if (cp_cmd_dispatcher(pd, CMD_POLL) == 0) {
pd->tstamp = osdp_millis_now();
}
break;
case OSDP_CP_STATE_OFFLINE:
if (osdp_millis_since(pd->tstamp) > OSDP_CMD_RETRY_WAIT_MS) {
cp_reset_state(pd);
}
break;
case OSDP_CP_STATE_INIT:
cp_set_state(pd, OSDP_CP_STATE_IDREQ);
/* FALLTHRU */
case OSDP_CP_STATE_IDREQ:
if (cp_cmd_dispatcher(pd, CMD_ID) != 0) {
break;
}
if (pd->reply_id != REPLY_PDID) {
cp_set_offline(pd);
}
cp_set_state(pd, OSDP_CP_STATE_CAPDET);
/* FALLTHRU */
case OSDP_CP_STATE_CAPDET:
if (cp_cmd_dispatcher(pd, CMD_CAP) != 0) {
break;
}
if (pd->reply_id != REPLY_PDCAP) {
cp_set_offline(pd);
}
#ifdef CONFIG_OSDP_SC_ENABLED
if (ISSET_FLAG(pd, PD_FLAG_SC_CAPABLE)) {
CLEAR_FLAG(pd, PD_FLAG_SC_SCBKD_DONE);
CLEAR_FLAG(pd, PD_FLAG_SC_USE_SCBKD);
cp_set_state(pd, OSDP_CP_STATE_SC_INIT);
break;
}
#endif /* CONFIG_OSDP_SC_ENABLED */
cp_set_state(pd, OSDP_CP_STATE_ONLINE);
break;
#ifdef CONFIG_OSDP_SC_ENABLED
case OSDP_CP_STATE_SC_INIT:
osdp_sc_init(pd);
cp_set_state(pd, OSDP_CP_STATE_SC_CHLNG);
/* FALLTHRU */
case OSDP_CP_STATE_SC_CHLNG:
if (cp_cmd_dispatcher(pd, CMD_CHLNG) != 0) {
break;
}
if (phy_state < 0) {
if (ISSET_FLAG(pd, PD_FLAG_SC_SCBKD_DONE)) {
LOG_INF(TAG "SC Failed; online without SC");
pd->sc_tstamp = osdp_millis_now();
cp_set_state(pd, OSDP_CP_STATE_ONLINE);
break;
}
SET_FLAG(pd, PD_FLAG_SC_USE_SCBKD);
SET_FLAG(pd, PD_FLAG_SC_SCBKD_DONE);
cp_set_state(pd, OSDP_CP_STATE_SC_INIT);
pd->phy_state = 0; /* soft reset phy state */
LOG_WRN(TAG "SC Failed; retry with SCBK-D");
break;
}
if (pd->reply_id != REPLY_CCRYPT) {
LOG_ERR(TAG "CHLNG failed. Online without SC");
pd->sc_tstamp = osdp_millis_now();
cp_set_state(pd, OSDP_CP_STATE_ONLINE);
break;
}
cp_set_state(pd, OSDP_CP_STATE_SC_SCRYPT);
/* FALLTHRU */
case OSDP_CP_STATE_SC_SCRYPT:
if (cp_cmd_dispatcher(pd, CMD_SCRYPT) != 0) {
break;
}
if (pd->reply_id != REPLY_RMAC_I) {
LOG_ERR(TAG "SCRYPT failed. Online without SC");
pd->sc_tstamp = osdp_millis_now();
cp_set_state(pd, OSDP_CP_STATE_ONLINE);
break;
}
if (ISSET_FLAG(pd, PD_FLAG_SC_USE_SCBKD)) {
LOG_WRN(TAG "SC ACtive with SCBK-D; Set SCBK");
cp_set_state(pd, OSDP_CP_STATE_SET_SCBK);
break;
}
LOG_INF(TAG "SC Active");
pd->sc_tstamp = osdp_millis_now();
cp_set_state(pd, OSDP_CP_STATE_ONLINE);
break;
case OSDP_CP_STATE_SET_SCBK:
if (cp_cmd_dispatcher(pd, CMD_KEYSET) != 0) {
break;
}
if (pd->reply_id == REPLY_NAK) {
LOG_WRN(TAG "Failed to set SCBK; continue with SCBK-D");
cp_set_state(pd, OSDP_CP_STATE_ONLINE);
break;
}
LOG_INF(TAG "SCBK set; restarting SC to verify new SCBK");
CLEAR_FLAG(pd, PD_FLAG_SC_USE_SCBKD);
CLEAR_FLAG(pd, PD_FLAG_SC_ACTIVE);
cp_set_state(pd, OSDP_CP_STATE_SC_INIT);
pd->seq_number = -1;
break;
#endif /* CONFIG_OSDP_SC_ENABLED */
default:
break;
}
return 0;
}
#ifdef CONFIG_OSDP_SC_ENABLED
static int osdp_cp_send_command_keyset(struct osdp_cmd_keyset *cmd)
{
int i;
struct osdp_cmd *p;
struct osdp_pd *pd;
struct osdp *ctx = osdp_get_ctx();
if (osdp_get_sc_status_mask() != PD_MASK(ctx)) {
LOG_WRN(TAG "CMD_KEYSET can be sent only when all PDs are "
"ONLINE and SC_ACTIVE.");
return 1;
}
for (i = 0; i < NUM_PD(ctx); i++) {
pd = TO_PD(ctx, i);
p = osdp_cmd_alloc(pd);
if (p == NULL) {
return -1;
}
p->id = CMD_KEYSET;
memcpy(&p->keyset, &cmd, sizeof(struct osdp_cmd_keyset));
osdp_cmd_enqueue(pd, p);
}
return 0;
}
#endif /* CONFIG_OSDP_SC_ENABLED */
void osdp_update(struct osdp *ctx)
{
int i;
for (i = 0; i < NUM_PD(ctx); i++) {
SET_CURRENT_PD(ctx, i);
state_update(GET_CURRENT_PD(ctx));
}
}
int osdp_setup(struct osdp *ctx, uint8_t *key)
{
ARG_UNUSED(ctx);
ARG_UNUSED(key);
#ifdef CONFIG_OSDP_SC_ENABLED
if (key == NULL) {
LOG_ERR(TAG "Master key cannot be null");
return -1;
}
memcpy(ctx->sc_master_key, key, 16);
#endif
return 0;
}
/* --- Exported Methods --- */
int osdp_cp_set_callback_key_press(int (*cb)(int address, uint8_t key))
{
struct osdp *ctx = osdp_get_ctx();
ctx->cp->notifier.keypress = cb;
return 0;
}
int osdp_cp_set_callback_card_read(
int (*cb)(int address, int format, uint8_t *data, int len))
{
struct osdp *ctx = osdp_get_ctx();
TO_CP(ctx)->notifier.cardread = cb;
return 0;
}
int osdp_cp_send_command(int pd, struct osdp_cmd *cmd)
{
struct osdp *ctx = osdp_get_ctx();
struct osdp_cmd *p;
int cmd_id;
if (pd < 0 || pd >= NUM_PD(ctx)) {
LOG_ERR(TAG "Invalid PD number");
return -1;
}
if (TO_PD(ctx, pd)->state != OSDP_CP_STATE_ONLINE) {
LOG_WRN(TAG "PD not online");
return -1;
}
switch (cmd->id) {
case OSDP_CMD_OUTPUT:
cmd_id = CMD_OUT;
break;
case OSDP_CMD_LED:
cmd_id = CMD_LED;
break;
case OSDP_CMD_BUZZER:
cmd_id = CMD_BUZ;
break;
case OSDP_CMD_TEXT:
cmd_id = CMD_TEXT;
break;
case OSDP_CMD_COMSET:
cmd_id = CMD_COMSET;
break;
#ifdef CONFIG_OSDP_SC_ENABLED
case OSDP_CMD_KEYSET:
return osdp_cp_send_command_keyset(&cmd->keyset);
#endif
default:
LOG_ERR(TAG "Invalid command ID %d", cmd->id);
return -1;
}