-
Notifications
You must be signed in to change notification settings - Fork 2
/
osdp_cp.c
1548 lines (1396 loc) · 40.2 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) 2019-2021-2021 Siddharth Chandrasekaran <[email protected]>
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <stdlib.h>
//vk2tds
#define OSDP_EXPORT
#define OSDP_NO_EXPORT
//vk2tds
//#include <utils/disjoint_set.h>
#include "slab.h"
#include "queue.h"
#include "disjoint_set.h"
#include "osdp_common.h"
#include "osdp_file.h"
#define LOG_TAG "CP: "
#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 CMD_MFG_LEN 4 /* variable length command */
#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 REPLY_MFGREP_LEN 4 /* variable length command */
/* CP event requests */
#define CP_REQ_RESTART_SC 0x00000001
#define CP_REQ_EVENT_SEND 0x00000002
enum osdp_cp_error_e {
OSDP_CP_ERR_NONE = 0,
OSDP_CP_ERR_GENERIC = -1,
OSDP_CP_ERR_NO_DATA = -2,
OSDP_CP_ERR_RETRY_CMD = -3,
OSDP_CP_ERR_CAN_YIELD = -4,
OSDP_CP_ERR_INPROG = -5,
OSDP_CP_ERR_UNKNOWN = -6,
};
struct cp_cmd_node {
queue_node_t node;
struct osdp_cmd object;
};
static int cp_cmd_queue_init(struct osdp_pd *pd)
{
if (slab_init(&pd->app_data.slab,
sizeof(struct cp_cmd_node),
pd->app_data.slab_blob,
sizeof(pd->app_data.slab_blob)) < 0) {
LOG_ERR("Failed to initialize command slab");
return -1;
}
queue_init(&pd->cmd_queue);
return 0;
}
static struct osdp_cmd *cp_cmd_alloc(struct osdp_pd *pd)
{
struct cp_cmd_node *n = NULL;
if (slab_alloc(&pd->app_data.slab, (void **)&n)) {
LOG_ERR("Command slab allocation failed");
return NULL;
}
memset(&n->object, 0, sizeof(n->object));
return &n->object;
}
static void cp_cmd_free(struct osdp_pd *pd, struct osdp_cmd *cmd)
{
struct cp_cmd_node *n;
n = CONTAINER_OF(cmd, struct cp_cmd_node, object);
slab_free(&pd->app_data.slab, n);
}
static void cp_cmd_enqueue(struct osdp_pd *pd, struct osdp_cmd *cmd)
{
struct cp_cmd_node *n;
n = CONTAINER_OF(cmd, struct cp_cmd_node, object);
queue_enqueue(&pd->cmd_queue, &n->node);
}
static int cp_cmd_dequeue(struct osdp_pd *pd, struct osdp_cmd **cmd)
{
struct cp_cmd_node *n;
queue_node_t *node;
if (queue_dequeue(&pd->cmd_queue, &node)) {
return -1;
}
n = CONTAINER_OF(node, struct cp_cmd_node, node);
*cmd = &n->object;
return 0;
}
static int cp_channel_acquire(struct osdp_pd *pd, int *owner)
{
int i;
struct osdp *ctx = pd_to_osdp(pd);
if (ctx->channel_lock[pd->idx] == pd->channel.id) {
return 0; /* already acquired! by current PD */
}
assert(ctx->channel_lock[pd->idx] == 0);
for (i = 0; i < NUM_PD(ctx); i++) {
if (ctx->channel_lock[i] == pd->channel.id) {
/* some other PD has locked this channel */
if (owner != NULL) {
*owner = i;
}
return -1;
}
}
ctx->channel_lock[pd->idx] = pd->channel.id;
return 0;
}
static int cp_channel_release(struct osdp_pd *pd)
{
struct osdp *ctx = pd_to_osdp(pd);
if (ctx->channel_lock[pd->idx] != pd->channel.id) {
LOG_ERR("Attempt to release another PD's channel lock");
return -1;
}
ctx->channel_lock[pd->idx] = 0;
return 0;
}
static const char *cp_get_cap_name(int cap)
{
if (cap <= OSDP_PD_CAP_UNUSED || cap >= OSDP_PD_CAP_SENTINEL) {
return NULL;
}
const char *cap_name[] = {
[OSDP_PD_CAP_CONTACT_STATUS_MONITORING] = "ContactStatusMonitoring",
[OSDP_PD_CAP_OUTPUT_CONTROL] = "OutputControl",
[OSDP_PD_CAP_CARD_DATA_FORMAT] = "CardDataFormat",
[OSDP_PD_CAP_READER_LED_CONTROL] = "LEDControl",
[OSDP_PD_CAP_READER_AUDIBLE_OUTPUT] = "AudibleControl",
[OSDP_PD_CAP_READER_TEXT_OUTPUT] = "TextOutput",
[OSDP_PD_CAP_TIME_KEEPING] = "TimeKeeping",
[OSDP_PD_CAP_CHECK_CHARACTER_SUPPORT] = "CheckCharacter",
[OSDP_PD_CAP_COMMUNICATION_SECURITY] = "CommunicationSecurity",
[OSDP_PD_CAP_RECEIVE_BUFFERSIZE] = "ReceiveBufferSize",
[OSDP_PD_CAP_LARGEST_COMBINED_MESSAGE_SIZE] = "CombinedMessageSize",
[OSDP_PD_CAP_SMART_CARD_SUPPORT] = "SmartCard",
[OSDP_PD_CAP_READERS] = "Reader",
[OSDP_PD_CAP_BIOMETRICS] = "Biometric",
};
return cap_name[cap];
}
static inline void assert_buf_len(int need, int have)
{
// __ASSERT(need < have, "OOM at build command: need:%d have:%d",
// need, have);
}
static int cp_build_command(struct osdp_pd *pd, uint8_t *buf, int max_len)
{
struct osdp_cmd *cmd = NULL;
int ret, len = 0;
int data_off = osdp_phy_packet_get_data_offset(pd, buf);
uint8_t *smb = osdp_phy_packet_get_smb(pd, buf);
buf += data_off;
max_len -= data_off;
if (max_len <= 0) {
return OSDP_CP_ERR_GENERIC;
}
switch (pd->cmd_id) {
case CMD_POLL:
assert_buf_len(CMD_POLL_LEN, max_len);
buf[len++] = pd->cmd_id;
break;
case CMD_LSTAT:
assert_buf_len(CMD_LSTAT_LEN, max_len);
buf[len++] = pd->cmd_id;
break;
case CMD_ISTAT:
assert_buf_len(CMD_ISTAT_LEN, max_len);
buf[len++] = pd->cmd_id;
break;
case CMD_OSTAT:
assert_buf_len(CMD_OSTAT_LEN, max_len);
buf[len++] = pd->cmd_id;
break;
case CMD_RSTAT:
assert_buf_len(CMD_RSTAT_LEN, max_len);
buf[len++] = pd->cmd_id;
break;
case CMD_ID:
assert_buf_len(CMD_ID_LEN, max_len);
buf[len++] = pd->cmd_id;
buf[len++] = 0x00;
break;
case CMD_CAP:
assert_buf_len(CMD_CAP_LEN, max_len);
buf[len++] = pd->cmd_id;
buf[len++] = 0x00;
break;
case CMD_OUT:
assert_buf_len(CMD_OUT_LEN, max_len);
cmd = (struct osdp_cmd *)pd->ephemeral_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);
break;
case CMD_LED:
assert_buf_len(CMD_LED_LEN, max_len);
cmd = (struct osdp_cmd *)pd->ephemeral_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;
break;
case CMD_BUZ:
assert_buf_len(CMD_BUZ_LEN, max_len);
cmd = (struct osdp_cmd *)pd->ephemeral_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;
break;
case CMD_TEXT:
cmd = (struct osdp_cmd *)pd->ephemeral_data;
assert_buf_len(CMD_TEXT_LEN + cmd->text.length, max_len);
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;
memcpy(buf + len, cmd->text.data, cmd->text.length);
len += cmd->text.length;
break;
case CMD_COMSET:
assert_buf_len(CMD_COMSET_LEN, max_len);
cmd = (struct osdp_cmd *)pd->ephemeral_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);
break;
case CMD_MFG:
cmd = (struct osdp_cmd *)pd->ephemeral_data;
assert_buf_len(CMD_MFG_LEN + cmd->mfg.length, max_len);
if (cmd->mfg.length > OSDP_CMD_MFG_MAX_DATALEN) {
LOG_ERR("Invalid MFG data length (%d)", cmd->mfg.length);
return OSDP_CP_ERR_GENERIC;
}
buf[len++] = pd->cmd_id;
buf[len++] = BYTE_0(cmd->mfg.vendor_code);
buf[len++] = BYTE_1(cmd->mfg.vendor_code);
buf[len++] = BYTE_2(cmd->mfg.vendor_code);
buf[len++] = cmd->mfg.command;
memcpy(buf + len, cmd->mfg.data, cmd->mfg.length);
len += cmd->mfg.length;
break;
case CMD_ACURXSIZE:
buf[len++] = pd->cmd_id;
buf[len++] = BYTE_0(OSDP_PACKET_BUF_SIZE);
buf[len++] = BYTE_1(OSDP_PACKET_BUF_SIZE);
break;
case CMD_KEEPACTIVE:
buf[len++] = pd->cmd_id;
buf[len++] = BYTE_0(0); // keepalive in ms time LSB
buf[len++] = BYTE_1(0); // keepalive in ms time MSB
break;
case CMD_ABORT:
buf[len++] = pd->cmd_id;
break;
case CMD_FILETRANSFER:
buf[len++] = pd->cmd_id;
ret = osdp_file_cmd_tx_build(pd, buf + len, max_len);
if (ret <= 0) {
return OSDP_CP_ERR_GENERIC;
}
len += ret;
break;
case CMD_KEYSET:
if (!sc_is_active(pd)) {
LOG_ERR("Cannot perform KEYSET without SC!");
return OSDP_CP_ERR_GENERIC;
}
cmd = (struct osdp_cmd *)pd->ephemeral_data;
assert_buf_len(CMD_KEYSET_LEN, max_len);
if (cmd->keyset.length != 16) {
LOG_ERR("Invalid key length");
return OSDP_CP_ERR_GENERIC;
}
buf[len++] = pd->cmd_id;
buf[len++] = 1; /* key type (1: SCBK) */
buf[len++] = 16; /* key length in bytes */
if (cmd->keyset.type == 1) { /* SCBK */
memcpy(buf + len, cmd->keyset.data, 16);
} else if (cmd->keyset.type == 0) { /* master_key */
osdp_compute_scbk(pd, cmd->keyset.data, buf + len);
} else {
LOG_ERR("Unknown key type (%d)", cmd->keyset.type);
return -1;
}
len += 16;
break;
case CMD_CHLNG:
assert_buf_len(CMD_CHLNG_LEN, max_len);
if (smb == NULL) {
LOG_ERR("Invalid secure message block!");
return OSDP_CP_ERR_GENERIC;
}
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;
memcpy(buf + len, pd->sc.cp_random, 8);
len += 8;
break;
case CMD_SCRYPT:
assert_buf_len(CMD_SCRYPT_LEN, max_len);
if (smb == NULL) {
LOG_ERR("Invalid secure message block!");
return OSDP_CP_ERR_GENERIC;
}
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;
memcpy(buf + len, pd->sc.cp_cryptogram, 16);
len += 16;
break;
default:
LOG_ERR("Unknown/Unsupported CMD: %s(%02x)",
osdp_cmd_name(pd->cmd_id), pd->cmd_id);
return OSDP_CP_ERR_GENERIC;
}
if (smb && (smb[1] > SCS_14) && sc_is_active(pd)) {
/**
* 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;
}
if (IS_ENABLED(OSDP_DEBUGFLAG_DATA_TRACE)) { // vk2tds
if (pd->cmd_id != CMD_POLL) {
hexdump(buf + 1, len - 1, "OSDP: CMD: %s(%02x)",
osdp_cmd_name(pd->cmd_id), pd->cmd_id);
}
}
return len;
}
static int cp_decode_response(struct osdp_pd *pd, uint8_t *buf, int len)
{
uint32_t temp32;
struct osdp *ctx = pd_to_osdp(pd);
int i, ret = OSDP_CP_ERR_GENERIC, pos = 0, t1, t2;
struct osdp_event event;
pd->reply_id = buf[pos++];
len--; /* consume reply id from the head */
if (IS_ENABLED(OSDP_DEBUGFLAG_DATA_TRACE)) { // vk2tds
if (pd->cmd_id != CMD_POLL) {
hexdump(buf, len, "OSDP: REPLY: %s(%02x)",
osdp_reply_name(pd->reply_id), pd->reply_id);
}
}
switch (pd->reply_id) {
case REPLY_ACK:
if (len != REPLY_ACK_DATA_LEN) {
break;
}
ret = OSDP_CP_ERR_NONE;
break;
case REPLY_NAK:
if (len != REPLY_NAK_DATA_LEN) {
break;
}
LOG_WRN("PD replied with NAK(%d) for CMD(%02x)",
buf[pos], pd->cmd_id);
ret = OSDP_CP_ERR_NONE;
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 = OSDP_CP_ERR_NONE;
break;
case REPLY_PDCAP:
if ((len % REPLY_PDCAP_ENTITY_LEN) != 0) {
LOG_ERR("PDCAP response length is not a multiple of 3");
return OSDP_CP_ERR_GENERIC;
}
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++];
LOG_DBG("Reports capability '%s' (%d/%d)",
cp_get_cap_name(pd->cap[t1].function_code),
pd->cap[t1].compliance_level,
pd->cap[t1].num_items);
}
/* Get peer RX buffer size */
t1 = OSDP_PD_CAP_RECEIVE_BUFFERSIZE;
if (pd->cap[t1].function_code == t1) {
pd->peer_rx_size = pd->cap[t1].compliance_level;
pd->peer_rx_size |= pd->cap[t1].num_items << 8;
}
/* 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 = OSDP_CP_ERR_NONE;
break;
case REPLY_OSTATR: {
uint32_t status_mask = 0;
int cap_num = OSDP_PD_CAP_OUTPUT_CONTROL;
if (len != pd->cap[cap_num].num_items || len > 32) {
LOG_ERR("Invalid output status report length %d", len);
return OSDP_CP_ERR_GENERIC;
}
for (i = 0; i < len; i++) {
status_mask |= !!buf[pos++] << i;
}
event.type = OSDP_EVENT_STATUS;
event.status.type = OSDP_STATUS_REPORT_OUTPUT;
event.status.nr_entries = len;
event.status.mask = status_mask;
memcpy(pd->ephemeral_data, &event, sizeof(event));
make_request(pd, CP_REQ_EVENT_SEND);
ret = OSDP_CP_ERR_NONE;
break;
}
case REPLY_ISTATR: {
uint32_t status_mask = 0;
int cap_num = OSDP_PD_CAP_CONTACT_STATUS_MONITORING;
if (len != pd->cap[cap_num].num_items || len > 32) {
LOG_ERR("Invalid input status report length %d", len);
return OSDP_CP_ERR_GENERIC;
}
for (i = 0; i < len; i++) {
status_mask |= !!buf[pos++] << i;
}
event.type = OSDP_EVENT_STATUS;
event.status.type = OSDP_STATUS_REPORT_INPUT;
event.status.nr_entries = len;
event.status.mask = status_mask;
memcpy(pd->ephemeral_data, &event, sizeof(event));
make_request(pd, CP_REQ_EVENT_SEND);
ret = OSDP_CP_ERR_NONE;
break;
}
case REPLY_LSTATR:
if (len != REPLY_LSTATR_DATA_LEN) {
break;
}
event.type = OSDP_EVENT_STATUS;
event.status.type = OSDP_STATUS_REPORT_LOCAL;
event.status.nr_entries = 2;
event.status.mask = !!buf[pos++];
event.status.mask = !!buf[pos++] << 1;
memcpy(pd->ephemeral_data, &event, sizeof(event));
make_request(pd, CP_REQ_EVENT_SEND);
ret = OSDP_CP_ERR_NONE;
break;
case REPLY_RSTATR:
if (len != REPLY_RSTATR_DATA_LEN) {
break;
}
event.type = OSDP_EVENT_STATUS;
event.status.type = OSDP_STATUS_REPORT_REMOTE;
event.status.nr_entries = 1;
event.status.mask = !!buf[pos++];
memcpy(pd->ephemeral_data, &event, sizeof(event));
make_request(pd, CP_REQ_EVENT_SEND);
ret = OSDP_CP_ERR_NONE;
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_INF("COMSET responded with ID:%d Baud:%d", t1, temp32);
pd->address = t1;
pd->baud_rate = temp32;
ret = OSDP_CP_ERR_NONE;
break;
case REPLY_KEYPPAD:
if (len < REPLY_KEYPPAD_DATA_LEN) {
break;
}
event.type = OSDP_EVENT_KEYPRESS;
event.keypress.reader_no = buf[pos++];
event.keypress.length = buf[pos++]; /* key length */
if ((len - REPLY_KEYPPAD_DATA_LEN) != event.keypress.length) {
break;
}
memcpy(event.keypress.data, buf + pos, event.keypress.length);
memcpy(pd->ephemeral_data, &event, sizeof(event));
make_request(pd, CP_REQ_EVENT_SEND);
ret = OSDP_CP_ERR_NONE;
break;
case REPLY_RAW:
if (len < REPLY_RAW_DATA_LEN) {
break;
}
event.type = OSDP_EVENT_CARDREAD;
event.cardread.reader_no = buf[pos++];
event.cardread.format = buf[pos++];
event.cardread.length = buf[pos++]; /* bits LSB */
event.cardread.length |= buf[pos++] << 8; /* bits MSB */
event.cardread.direction = 0; /* un-specified */
t1 = (event.cardread.length + 7) / 8; /* len: bytes */
if (t1 != (len - REPLY_RAW_DATA_LEN)) {
break;
}
memcpy(event.cardread.data, buf + pos, t1);
memcpy(pd->ephemeral_data, &event, sizeof(event));
make_request(pd, CP_REQ_EVENT_SEND);
ret = OSDP_CP_ERR_NONE;
break;
case REPLY_FMT:
if (len < REPLY_FMT_DATA_LEN) {
break;
}
event.type = OSDP_EVENT_CARDREAD;
event.cardread.reader_no = buf[pos++];
event.cardread.direction = buf[pos++];
event.cardread.length = buf[pos++];
event.cardread.format = OSDP_CARD_FMT_ASCII;
if (event.cardread.length != (len - REPLY_FMT_DATA_LEN) ||
event.cardread.length > OSDP_EVENT_CARDREAD_MAX_DATALEN) {
break;
}
memcpy(event.cardread.data, buf + pos, event.cardread.length);
memcpy(pd->ephemeral_data, &event, sizeof(event));
make_request(pd, CP_REQ_EVENT_SEND);
ret = OSDP_CP_ERR_NONE;
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;
case REPLY_MFGREP:
if (len < REPLY_MFGREP_LEN) {
break;
}
event.type = OSDP_EVENT_MFGREP;
event.mfgrep.vendor_code = buf[pos++];
event.mfgrep.vendor_code |= buf[pos++] << 8;
event.mfgrep.vendor_code |= buf[pos++] << 16;
event.mfgrep.command = buf[pos++];
event.mfgrep.length = len - REPLY_MFGREP_LEN;
if (event.mfgrep.length > OSDP_EVENT_MFGREP_MAX_DATALEN) {
break;
}
memcpy(event.mfgrep.data, buf + pos, event.mfgrep.length);
memcpy(pd->ephemeral_data, &event, sizeof(event));
make_request(pd, CP_REQ_EVENT_SEND);
ret = OSDP_CP_ERR_NONE;
break;
case REPLY_FTSTAT:
ret = osdp_file_cmd_stat_decode(pd, buf + pos, len);
break;
case REPLY_CCRYPT:
if (sc_is_active(pd) || pd->cmd_id != CMD_CHLNG) {
LOG_EM("Out of order REPLY_CCRYPT; has PD gone rogue?");
break;
}
if (len != REPLY_CCRYPT_DATA_LEN) {
break;
}
memcpy(pd->sc.pd_client_uid, buf + pos, 8);
memcpy(pd->sc.pd_random, buf + pos + 8, 8);
memcpy(pd->sc.pd_cryptogram, buf + pos + 16, 16);
pos += 32;
osdp_compute_session_keys(pd);
if (osdp_verify_pd_cryptogram(pd) != 0) {
LOG_ERR("Failed to verify PD cryptogram");
return OSDP_CP_ERR_GENERIC;
}
ret = OSDP_CP_ERR_NONE;
break;
case REPLY_RMAC_I:
if (sc_is_active(pd) || pd->cmd_id != CMD_SCRYPT) {
LOG_EM("Out of order REPLY_RMAC_I; has PD gone rogue?");
break;
}
if (len != REPLY_RMAC_I_DATA_LEN) {
break;
}
memcpy(pd->sc.r_mac, buf + pos, 16);
ret = OSDP_CP_ERR_NONE;
break;
default:
LOG_WRN("Unknown reply %s(%02x)",
osdp_reply_name(pd->reply_id), pd->reply_id);
return OSDP_CP_ERR_UNKNOWN;
}
if (ret != OSDP_CP_ERR_NONE) {
LOG_ERR("Failed to decode reply %s(%02x) for command %s(%02x)",
osdp_reply_name(pd->reply_id), pd->reply_id,
osdp_cmd_name(pd->cmd_id), pd->cmd_id);
}
if (pd->cmd_id != CMD_POLL) {
LOG_DBG("CMD: %s(%02x) REPLY: %s(%02x)",
osdp_cmd_name(pd->cmd_id), pd->cmd_id,
osdp_reply_name(pd->reply_id), pd->reply_id);
}
return ret;
}
static void do_event_callback(struct osdp_pd *pd)
{
struct osdp *ctx = pd_to_osdp(pd);
if (ctx->event_callback) {
ctx->event_callback(ctx->event_callback_arg, pd->idx,
(struct osdp_event *)pd->ephemeral_data);
}
}
static int cp_build_and_send_packet(struct osdp_pd *pd)
{
int ret, packet_buf_size = get_tx_buf_size(pd);
/* init packet buf with header */
ret = osdp_phy_packet_init(pd, pd->packet_buf, packet_buf_size);
if (ret < 0) {
return OSDP_CP_ERR_GENERIC;
}
pd->packet_buf_len = ret;
/* fill command data */
ret = cp_build_command(pd, pd->packet_buf, packet_buf_size);
if (ret < 0) {
return OSDP_CP_ERR_GENERIC;
}
pd->packet_buf_len += ret;
ret = osdp_phy_send_packet(pd, pd->packet_buf, pd->packet_buf_len,
packet_buf_size);
if (ret < 0) {
return OSDP_CP_ERR_GENERIC;
}
return OSDP_CP_ERR_NONE;
}
static int cp_process_reply(struct osdp_pd *pd)
{
uint8_t *buf;
int err, len;
err = osdp_phy_check_packet(pd);
/* Translate phy error codes to CP errors */
switch (err) {
case OSDP_ERR_PKT_NONE:
break;
case OSDP_ERR_PKT_WAIT:
case OSDP_ERR_PKT_NO_DATA:
return OSDP_CP_ERR_NO_DATA;
case OSDP_ERR_PKT_BUSY:
return OSDP_CP_ERR_RETRY_CMD;
default:
return OSDP_CP_ERR_GENERIC;
}
/* Valid OSDP packet in buffer */
len = osdp_phy_decode_packet(pd, &buf);
if (len <= 0) {
return OSDP_CP_ERR_GENERIC;
}
return cp_decode_response(pd, buf, len);
}
static inline bool cp_sc_should_retry(struct osdp_pd *pd)
{
return (sc_is_capable(pd) && !sc_is_active(pd) &&
osdp_millis_since(pd->sc_tstamp) > OSDP_PD_SC_RETRY_MS);
}
int cp_translate_cmd(struct osdp_pd *pd, struct osdp_cmd *cmd)
{
int cmd_id = -1;
memcpy(pd->ephemeral_data, cmd, sizeof(struct osdp_cmd));
cp_cmd_free(pd, cmd);
cmd = (struct osdp_cmd *)pd->ephemeral_data;
switch (cmd->id) {
case OSDP_CMD_OUTPUT: return CMD_OUT;
case OSDP_CMD_LED: return CMD_LED;
case OSDP_CMD_BUZZER: return CMD_BUZ;
case OSDP_CMD_TEXT: return CMD_TEXT;
case OSDP_CMD_COMSET: return CMD_COMSET;
case OSDP_CMD_MFG: return CMD_MFG;
case OSDP_CMD_STATUS:
switch (cmd->status.type) {
case OSDP_STATUS_REPORT_INPUT: return CMD_ISTAT;
case OSDP_STATUS_REPORT_OUTPUT: return CMD_OSTAT;
case OSDP_STATUS_REPORT_LOCAL: return CMD_LSTAT;
case OSDP_STATUS_REPORT_REMOTE: return CMD_RSTAT;
default: return -1;
}
case OSDP_CMD_KEYSET:
cmd_id = CMD_KEYSET;
if (cmd->keyset.type != 1 || !sc_is_active(pd)) {
cmd_id = -1;
}
return cmd_id;
case OSDP_CMD_FILE_TX:
/**
* This external command is handled as multiple command from
* osdp_file.c and it maintains it's own state. This means we
* should never reach here unless something is wrong.
*/
__fallthrough;
default: BUG();
}
return cmd_id;
}
static void fill_local_keyset_cmd(struct osdp_pd *pd)
{
struct osdp_cmd *cmd = (struct osdp_cmd *)pd->ephemeral_data;
cmd->id = OSDP_CMD_KEYSET;
cmd->keyset.type = 1;
cmd->keyset.length = sizeof(pd->sc.scbk);
memcpy(cmd->keyset.data, pd->sc.scbk, sizeof(pd->sc.scbk));
}
static inline bool cp_phy_running(struct osdp_pd *pd)
{
return (pd->phy_state == OSDP_CP_PHY_STATE_SEND_CMD ||
pd->phy_state == OSDP_CP_PHY_STATE_REPLY_WAIT ||
pd->phy_state == OSDP_CP_PHY_STATE_WAIT);
}
static inline bool cp_phy_kick(struct osdp_pd *pd)
{
if (pd->phy_state == OSDP_CP_PHY_STATE_IDLE) {
pd->phy_state = OSDP_CP_PHY_STATE_SEND_CMD;
return true;
}
return false;
}
static int cp_phy_state_update(struct osdp_pd *pd)
{
int rc, ret = OSDP_CP_ERR_CAN_YIELD;
switch (pd->phy_state) {
case OSDP_CP_PHY_STATE_DONE:
case OSDP_CP_PHY_STATE_IDLE:
ret = OSDP_CP_ERR_NONE;
break;
case OSDP_CP_PHY_STATE_ERR:
ret = OSDP_CP_ERR_GENERIC;
break;
case OSDP_CP_PHY_STATE_WAIT:
if (osdp_millis_since(pd->phy_tstamp) < pd->wait_ms) {
return OSDP_CP_ERR_CAN_YIELD;
}
pd->phy_state = OSDP_CP_PHY_STATE_SEND_CMD;
__fallthrough;
case OSDP_CP_PHY_STATE_SEND_CMD:
/* Check if we have any commands in the queue */
if (cp_build_and_send_packet(pd)) {
LOG_ERR("Failed to build packet for CMD(%d)",
pd->cmd_id);
pd->phy_state = OSDP_CP_PHY_STATE_ERR;
ret = OSDP_CP_ERR_GENERIC;
break;
}
ret = OSDP_CP_ERR_INPROG;
osdp_phy_state_reset(pd, false);
pd->phy_state = OSDP_CP_PHY_STATE_REPLY_WAIT;
pd->phy_tstamp = osdp_millis_now();
break;
case OSDP_CP_PHY_STATE_REPLY_WAIT:
rc = cp_process_reply(pd);
if (rc == OSDP_CP_ERR_NONE) {
pd->tstamp = osdp_millis_now();
if (sc_is_active(pd)) {
pd->sc_tstamp = osdp_millis_now();
}
if (pd->reply_id == REPLY_BUSY) {
pd->phy_tstamp = osdp_millis_now();
pd->wait_ms = OSDP_CMD_RETRY_WAIT_MS;
pd->state = OSDP_CP_PHY_STATE_WAIT;
return OSDP_CP_ERR_CAN_YIELD;
}
pd->phy_state = OSDP_CP_PHY_STATE_DONE;
return OSDP_CP_ERR_NONE;
}
if (rc == OSDP_CP_ERR_UNKNOWN && pd->cmd_id == CMD_POLL &&
ISSET_FLAG(pd, OSDP_FLAG_IGN_UNSOLICITED)) {
if (sc_is_active(pd)) {
pd->sc_tstamp = osdp_millis_now();
}
pd->phy_state = OSDP_CP_PHY_STATE_DONE;
return OSDP_CP_ERR_NONE;
}
if (rc == OSDP_CP_ERR_GENERIC || rc == OSDP_CP_ERR_UNKNOWN ||
osdp_millis_since(pd->phy_tstamp) > OSDP_RESP_TOUT_MS) {
if (rc != OSDP_CP_ERR_GENERIC) {
LOG_ERR("Response timeout for CMD(%02x)",
pd->cmd_id);
}
pd->phy_state = OSDP_CP_PHY_STATE_ERR;
return OSDP_CP_ERR_GENERIC;
}
if (rc == OSDP_CP_ERR_RETRY_CMD) {
pd->phy_state = OSDP_CP_PHY_STATE_DONE;
return OSDP_CP_ERR_NONE;
}
ret = OSDP_CP_ERR_INPROG;
break;
}
return ret;
}
static const char *state_get_name(enum osdp_cp_state_e state)
{
switch (state) {
case OSDP_CP_STATE_INIT: return "ID-Request";
case OSDP_CP_STATE_CAPDET: return "Cap-Detect";
case OSDP_CP_STATE_SC_CHLNG: return "SC-Chlng";
case OSDP_CP_STATE_SC_SCRYPT: return "SC-Scrypt";
case OSDP_CP_STATE_SET_SCBK: return "SC-SetSCBK";
case OSDP_CP_STATE_ONLINE: return "Online";
case OSDP_CP_STATE_OFFLINE: return "Offline";
default:
BUG();
}
}
static int cp_get_online_command(struct osdp_pd *pd)
{
struct osdp_cmd *cmd;
if (cp_cmd_dequeue(pd, &cmd) == 0) {
return cp_translate_cmd(pd, cmd);
}
if (osdp_millis_since(pd->tstamp) > OSDP_PD_POLL_TIMEOUT_MS) {
pd->tstamp = osdp_millis_now();
return CMD_POLL;
}
switch(osdp_get_file_tx_state(pd)) {
case OSDP_FILE_TX_STATE_PENDING: return CMD_FILETRANSFER;
case OSDP_FILE_TX_STATE_ERROR: return CMD_ABORT;
}
return -1;
}
static bool cp_check_online_response(struct osdp_pd *pd)
{
/* Always allow an ACK from the PD; Also, the most common case */
if (pd->reply_id == REPLY_ACK) {
return true;
}
/* A NAK is always an error */
if (pd->reply_id == REPLY_NAK) {
return false;
}
/* Check for known poll responses */
if (pd->cmd_id == CMD_POLL) {
if (pd->reply_id == REPLY_LSTATR ||
pd->reply_id == REPLY_ISTATR ||
pd->reply_id == REPLY_OSTATR ||
pd->reply_id == REPLY_RSTATR ||
pd->reply_id == REPLY_MFGREP ||
pd->reply_id == REPLY_RAW ||
pd->reply_id == REPLY_FMT ||
pd->reply_id == REPLY_KEYPPAD) {
return true;
}
return ISSET_FLAG(pd, OSDP_FLAG_IGN_UNSOLICITED);
}
/* Otherwise, we permit only expected responses */
switch (pd->cmd_id) {
case CMD_FILETRANSFER: return pd->reply_id == REPLY_FTSTAT;
case CMD_COMSET: return pd->reply_id == REPLY_COM;
case CMD_MFG: return pd->reply_id == REPLY_MFGREP;
case CMD_LSTAT: return pd->reply_id == REPLY_LSTATR;
case CMD_ISTAT: return pd->reply_id == REPLY_ISTATR;
case CMD_OSTAT: return pd->reply_id == REPLY_OSTATR;
case CMD_RSTAT: return pd->reply_id == REPLY_RSTATR;