This repository has been archived by the owner on Feb 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
nxprd.go
1864 lines (1656 loc) · 56.3 KB
/
nxprd.go
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
// Package nxprd is the main package of the GO wrapper for NXP's reader library.
// All NFC functionality is included in this package.
package nxprd
/*
#cgo CFLAGS: -I ${SRCDIR}/nxp/nxprdlib/NxpRdLib/intfs -I ${SRCDIR}/nxp/nxprdlib/NxpRdLib/types -I ${SRCDIR}/nxp/linux/comps/phPlatform/src/Posix -I ${SRCDIR}/nxp/linux/shared -I ${SRCDIR}/nxp/linux/comps/phOsal/src/Posix
#cgo LDFLAGS: -L ${SRCDIR}/nxp/build/linux -lNxpRdLibLinuxPN512 -lrt
//#define DEBUG
#include <phhwConfig.h>
#include <ph_Status.h>
#include <phPlatform.h>
#include <phbalReg.h>
#include <phpalI14443p3a.h>
#include <phpalI14443p4.h>
#include <phpalI14443p3b.h>
#include <phpalI14443p4a.h>
#include <phpalI18092mPI.h>
#include <phpalSli15693.h>
#include <phpalI18000p3m3.h>
#include <phpalMifare.h>
#include <phpalFelica.h>
#include <phalI18000p3m3.h>
#include <phalT1T.h>
#include <phalFelica.h>
#include <phalMfc.h>
#include <phalMful.h>
#include <phacDiscLoop.h>
#include <phKeyStore.h>
#define LISTEN_PHASE_TIME_MS 50
#define NUMBER_OF_KEYENTRIES 2
#define NUMBER_OF_KEYVERSIONPAIRS 2
#define NUMBER_OF_KUCENTRIES 1
#define DATA_BUFFER_LEN 16
#define MFC_BLOCK_DATA_SIZE 16
// SAK codes
#define sak_ul 0x00
#define sak_ulc 0x00
#define sak_mini 0x09
#define sak_mfc_1k 0x08
#define sak_mfc_4k 0x18
#define sak_mfp_2k_sl1 0x08
#define sak_mfp_4k_sl1 0x18
#define sak_mfp_2k_sl2 0x10
#define sak_mfp_4k_sl2 0x11
#define sak_mfp_2k_sl3 0x20
#define sak_mfp_4k_sl3 0x20
#define sak_desfire 0x20
#define sak_jcop 0x28
#define sak_layer4 0x20
// ATQ codes
#define atqa_ul 0x4400
#define atqa_ulc 0x4400
#define atqa_mfc 0x0200
#define atqa_mfp_s 0x0400
#define atqa_mfp_x 0x4200
#define atqa_desfire 0x4403
#define atqa_jcop 0x0400
#define atqa_mini 0x0400
#define atqa_nPA 0x0800
// MIFARE cards
#define mifare_ultralight 0x01
#define mifare_ultralight_c 0x02
#define mifare_classic 0x03
#define mifare_classic_1k 0x04
#define mifare_classic_4k 0x05
#define mifare_plus 0x06
#define mifare_plus_2k_sl1 0x07
#define mifare_plus_4k_sl1 0x08
#define mifare_plus_2k_sl2 0x09
#define mifare_plus_4k_sl2 0x0A
#define mifare_plus_2k_sl3 0x0B
#define mifare_plus_4k_sl3 0x0C
#define mifare_desfire 0x0D
#define jcop 0x0F
#define mifare_mini 0x10
#define nPA 0x11
// Used by GO wrapper
typedef enum DiscoverResult {
DR_FOUND,
DR_UNKNOWN,
DR_NOT_FOUND
} DiscoverResult_t;
// Used by GO wrapper
typedef enum TagType
{
TAT_1 = 1,
TAT_2,
TAT_3,
TAT_4A,
TAT_P2P,
TAT_NFC_DEP_4A,
TAT_UNDEFINED
} TagType_t;
// Used by GO wrapper
typedef enum TechType
{
TET_A = 1,
TET_B,
TET_F,
TET_V_15693_T5T,
TET_18000p3m3_EPCGen2,
TET_UNDEFINED
} TechType_t;
// Used by GO wrapper
typedef struct NFCParams
{
int sak;
uint8_t atq[255];
uint8_t atqSize;
uint8_t uid[255];
uint8_t uidSize;
TechType_t techType;
TagType_t tagType;
} NFCParams_t;
NFCParams_t nfcParams;
phbalReg_Stub_DataParams_t sBalReader;
phPlatform_DataParams_t sPlatform;
phhalHw_Nfc_Ic_DataParams_t sHal_Nfc_Ic;
uint8_t bHalBufferTx[256];
uint8_t bHalBufferRx[256];
uint8_t mfulDataBuffer[PHAL_MFUL_READ_BLOCK_LENGTH];
void *pHal;
phpalI14443p3a_Sw_DataParams_t spalI14443p3a;
phpalI14443p4a_Sw_DataParams_t spalI14443p4a;
phpalI14443p3b_Sw_DataParams_t spalI14443p3b;
phpalI14443p4_Sw_DataParams_t spalI14443p4;
phpalFelica_Sw_DataParams_t spalFelica;
phpalI18092mPI_Sw_DataParams_t spalI18092mPI;
phpalMifare_Sw_DataParams_t spalMifare;
phacDiscLoop_Sw_DataParams_t sDiscLoop;
phalMfc_Sw_DataParams_t salMfc;
phalMful_Sw_DataParams_t salMfu;
phalT1T_Sw_DataParams_t alT1T;
#ifndef NXPBUILD__PHHAL_HW_RC523
phpalSli15693_Sw_DataParams_t spalSli15693;
phalI18000p3m3_Sw_DataParams_t salI18000p3m3;
phpalI18000p3m3_Sw_DataParams_t spalI18000p3m3;
#endif
phKeyStore_Sw_DataParams_t sSwkeyStore;
phKeyStore_Sw_KeyEntry_t sKeyEntries[NUMBER_OF_KEYENTRIES];
phKeyStore_Sw_KUCEntry_t sKUCEntries[NUMBER_OF_KUCENTRIES];
phKeyStore_Sw_KeyVersionPair_t sKeyVersionPairs[NUMBER_OF_KEYVERSIONPAIRS * NUMBER_OF_KEYENTRIES];
uint8_t bDataBuffer[DATA_BUFFER_LEN];
uint8_t bSak;
uint16_t wAtqa;
const uint8_t GI[] = { 0x46,0x66,0x6D,
0x01,0x01,0x10,
0x03,0x02,0x00,0x01,
0x04,0x01,0xF1
};
static uint8_t aData[50];
#ifndef NXPBUILD__PHHAL_HW_RC663
static uint8_t sens_res[2] = {0x04, 0x00};
static uint8_t nfc_id1[3] = {0xA1, 0xA2, 0xA3};
static uint8_t sel_res = 0x40;
static uint8_t nfc_id3 = 0xFA;
static uint8_t poll_res[18] = {0x01, 0xFE, 0xB2, 0xB3, 0xB4, 0xB5,
0xB6, 0xB7, 0xC0, 0xC1, 0xC2, 0xC3,
0xC4, 0xC5, 0xC6, 0xC7, 0x23, 0x45 };
#endif
static uint16_t bSavePollTechCfg = 0;
uint8_t Key[6] = {0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU};
uint8_t Original_Key[6] = {0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU, 0xFFU};
// Used in Discover and Discover_Init functions
uint16_t wTagsDetected = 0;
uint16_t wNumberOfTags = 0;
uint16_t wEntryPoint;
#define DETECT_ERROR 1
#if DETECT_ERROR
#define DEBUG_ERROR_PRINT(x) x
#define PRINT_INFO(...) DEBUG_PRINTF(__VA_ARGS__)
#else
#define DEBUG_ERROR_PRINT(x)
#define PRINT_INFO(...)
#endif
static void PRINT_BUFF(uint8_t *pBuff, uint8_t num)
{
uint32_t i;
for(i = 0; i < num; i++)
{
DEBUG_PRINTF(" %02X",pBuff[i]);
}
}
static void PrintTagInfo(phacDiscLoop_Sw_DataParams_t *pDataParams, uint16_t wNumberOfTags, uint16_t wTagsDetected)
{
uint8_t bIndex;
uint8_t bTagType;
if (PHAC_DISCLOOP_CHECK_ANDMASK(wTagsDetected, PHAC_DISCLOOP_POS_BIT_MASK_A))
{
if(pDataParams->sTypeATargetInfo.bT1TFlag)
{
DEBUG_PRINTF("\tTechnology : Type A");
DEBUG_PRINTF ("\n\t\tUID :");
PRINT_BUFF( pDataParams->sTypeATargetInfo.aTypeA_I3P3[0].aUid,
pDataParams->sTypeATargetInfo.aTypeA_I3P3[0].bUidSize);
DEBUG_PRINTF ("\n\t\tSAK : 0x%02x",pDataParams->sTypeATargetInfo.aTypeA_I3P3[0].aSak);
DEBUG_PRINTF ("\n\t\tType: Type 1 Tag\n");
}
else
{
DEBUG_PRINTF("\tTechnology : Type A");
for(bIndex = 0; bIndex < wNumberOfTags; bIndex++)
{
DEBUG_PRINTF ("\n\t\tCard: %d",bIndex + 1);
DEBUG_PRINTF ("\n\t\tUID :");
PRINT_BUFF( pDataParams->sTypeATargetInfo.aTypeA_I3P3[bIndex].aUid,
pDataParams->sTypeATargetInfo.aTypeA_I3P3[bIndex].bUidSize);
DEBUG_PRINTF ("\n\t\tSAK : 0x%02x",pDataParams->sTypeATargetInfo.aTypeA_I3P3[bIndex].aSak);
if ((pDataParams->sTypeATargetInfo.aTypeA_I3P3[bIndex].aSak & (uint8_t) ~0xFB) == 0)
{
// Bit b3 is set to zero, [Digital] 4.8.2
// Mask out all other bits except for b7 and b6
bTagType = (pDataParams->sTypeATargetInfo.aTypeA_I3P3[bIndex].aSak & 0x60);
bTagType = bTagType >> 5;
switch(bTagType)
{
case PHAC_DISCLOOP_TYPEA_TYPE2_TAG_CONFIG_MASK:
DEBUG_PRINTF ("\n\t\tType: Type 2 Tag\n");
break;
case PHAC_DISCLOOP_TYPEA_TYPE4A_TAG_CONFIG_MASK:
DEBUG_PRINTF ("\n\t\tType: Type 4A Tag\n");
break;
case PHAC_DISCLOOP_TYPEA_TYPE_NFC_DEP_TAG_CONFIG_MASK:
DEBUG_PRINTF ("\n\t\tType: P2P\n");
break;
case PHAC_DISCLOOP_TYPEA_TYPE_NFC_DEP_TYPE4A_TAG_CONFIG_MASK:
DEBUG_PRINTF ("\n\t\tType: Type NFC_DEP and 4A Tag\n");
break;
default:
break;
}
}
}
}
}
if (PHAC_DISCLOOP_CHECK_ANDMASK(wTagsDetected, PHAC_DISCLOOP_POS_BIT_MASK_B))
{
DEBUG_PRINTF("\tTechnology : Type B");
// Loop through all the Type B tags detected and print the Pupi
for (bIndex = 0; bIndex < wNumberOfTags; bIndex++)
{
DEBUG_PRINTF ("\n\t\tCard: %d",bIndex + 1);
DEBUG_PRINTF ("\n\t\tUID :");
// PUPI Length is always 4 bytes
PRINT_BUFF( pDataParams->sTypeBTargetInfo.aTypeB_I3P3[bIndex].aPupi, 0x04);
}
DEBUG_PRINTF("\n");
}
if( PHAC_DISCLOOP_CHECK_ANDMASK(wTagsDetected, PHAC_DISCLOOP_POS_BIT_MASK_F212) ||
PHAC_DISCLOOP_CHECK_ANDMASK(wTagsDetected, PHAC_DISCLOOP_POS_BIT_MASK_F424))
{
DEBUG_PRINTF("\tTechnology : Type F");
// Loop through all the type F tags and print the IDm
for (bIndex = 0; bIndex < wNumberOfTags; bIndex++)
{
DEBUG_PRINTF ("\n\t\tCard: %d",bIndex + 1);
DEBUG_PRINTF ("\n\t\tUID :");
PRINT_BUFF( pDataParams->sTypeFTargetInfo.aTypeFTag[bIndex].aIDmPMm,
PHAC_DISCLOOP_FELICA_IDM_LENGTH );
if ((pDataParams->sTypeFTargetInfo.aTypeFTag[bIndex].aIDmPMm[0] == 0x01) &&
(pDataParams->sTypeFTargetInfo.aTypeFTag[bIndex].aIDmPMm[1] == 0xFE))
{
// This is Type F tag with P2P capabilities
DEBUG_PRINTF ("\n\t\tType: P2P");
}
else
{
// This is Type F T3T tag
DEBUG_PRINTF ("\n\t\tType: Type 3 Tag");
}
if(pDataParams->sTypeFTargetInfo.aTypeFTag[bIndex].bBaud != PHAC_DISCLOOP_CON_BITR_212)
{
DEBUG_PRINTF ("\n\t\tBit Rate: 424\n");
}
else
{
DEBUG_PRINTF ("\n\t\tBit Rate: 212\n");
}
}
}
#ifndef NXPBUILD__PHHAL_HW_RC523
if (PHAC_DISCLOOP_CHECK_ANDMASK(wTagsDetected, PHAC_DISCLOOP_POS_BIT_MASK_V))
{
DEBUG_PRINTF("\tTechnology : Type V / ISO 15693 / T5T");
// Loop through all the Type V tags detected and print the UIDs
for (bIndex = 0; bIndex < wNumberOfTags; bIndex++)
{
DEBUG_PRINTF ("\n\t\tCard: %d",bIndex + 1);
DEBUG_PRINTF ("\n\t\tUID :");
PRINT_BUFF( pDataParams->sTypeVTargetInfo.aTypeV[bIndex].aUid, 0x08);
}
DEBUG_PRINTF("\n");
}
if (PHAC_DISCLOOP_CHECK_ANDMASK(wTagsDetected, PHAC_DISCLOOP_POS_BIT_MASK_18000P3M3))
{
DEBUG_PRINTF("\tTechnology : ISO 18000p3m3 / EPC Gen2");
// Loop through all the 18000p3m3 tags detected and print the UII
for (bIndex = 0; bIndex < wNumberOfTags; bIndex++)
{
DEBUG_PRINTF("\n\t\tCard: %d",bIndex + 1);
DEBUG_PRINTF("\n\t\tUII :");
PRINT_BUFF(
pDataParams->sI18000p3m3TargetInfo.aI18000p3m3[bIndex].aUii,
(pDataParams->sI18000p3m3TargetInfo.aI18000p3m3[bIndex].wUiiLength / 8));
}
DEBUG_PRINTF("\n");
}
#endif
}
#if DETECT_ERROR
static void PrintErrorInfo(phStatus_t wStatus)
{
DEBUG_PRINTF("\n ErrorInfo Comp:");
switch(wStatus & 0xFF00)
{
case PH_COMP_BAL:
DEBUG_PRINTF("\t PH_COMP_BAL");
break;
case PH_COMP_HAL:
DEBUG_PRINTF("\t PH_COMP_HAL");
break;
case PH_COMP_PAL_ISO14443P3A:
DEBUG_PRINTF("\t PH_COMP_PAL_ISO14443P3A");
break;
case PH_COMP_PAL_ISO14443P3B:
DEBUG_PRINTF("\t PH_COMP_PAL_ISO14443P3B");
break;
case PH_COMP_PAL_ISO14443P4A:
DEBUG_PRINTF("\t PH_COMP_PAL_ISO14443P4A");
break;
case PH_COMP_PAL_ISO14443P4:
DEBUG_PRINTF("\t PH_COMP_PAL_ISO14443P4");
break;
case PH_COMP_PAL_FELICA:
DEBUG_PRINTF("\t PH_COMP_PAL_FELICA");
break;
case PH_COMP_PAL_EPCUID:
DEBUG_PRINTF("\t PH_COMP_PAL_EPCUID");
break;
case PH_COMP_PAL_SLI15693:
DEBUG_PRINTF("\t PH_COMP_PAL_SLI15693");
break;
case PH_COMP_PAL_I18000P3M3:
DEBUG_PRINTF("\t PH_COMP_PAL_I18000P3M3");
break;
case PH_COMP_PAL_I18092MPI:
DEBUG_PRINTF("\t PH_COMP_PAL_I18092MPI");
break;
case PH_COMP_PAL_I18092MT:
DEBUG_PRINTF("\t PH_COMP_PAL_I18092MT");
break;
case PH_COMP_PAL_GENERALTARGET:
DEBUG_PRINTF("\t PH_COMP_PAL_GENERALTARGET");
break;
case PH_COMP_PAL_I14443P4MC:
DEBUG_PRINTF("\t PH_COMP_PAL_I14443P4MC");
break;
case PH_COMP_AC_DISCLOOP:
DEBUG_PRINTF("\t PH_COMP_AC_DISCLOOP");
break;
case PH_COMP_OSAL:
DEBUG_PRINTF("\t PH_COMP_PAL_I14443P4MC");
break;
default:
DEBUG_PRINTF("\t 0x%x",(wStatus & PH_COMPID_MASK));
break;
}
DEBUG_PRINTF("\t type:");
switch(wStatus & PH_ERR_MASK)
{
case PH_ERR_SUCCESS_INCOMPLETE_BYTE:
DEBUG_PRINTF("\t PH_ERR_SUCCESS_INCOMPLETE_BYTE");
break;
case PH_ERR_IO_TIMEOUT:
DEBUG_PRINTF("\t PH_ERR_IO_TIMEOUT");
break;
case PH_ERR_INTEGRITY_ERROR:
DEBUG_PRINTF("\t PH_ERR_INTEGRITY_ERROR");
break;
case PH_ERR_COLLISION_ERROR:
DEBUG_PRINTF("\t PH_ERR_COLLISION_ERROR");
break;
case PH_ERR_BUFFER_OVERFLOW:
DEBUG_PRINTF("\t PH_ERR_BUFFER_OVERFLOW");
break;
case PH_ERR_FRAMING_ERROR:
DEBUG_PRINTF("\t PH_ERR_FRAMING_ERROR");
break;
case PH_ERR_PROTOCOL_ERROR:
DEBUG_PRINTF("\t PH_ERR_PROTOCOL_ERROR");
break;
case PH_ERR_RF_ERROR:
DEBUG_PRINTF("\t PH_ERR_RF_ERROR");
break;
case PH_ERR_EXT_RF_ERROR:
DEBUG_PRINTF("\t PH_ERR_EXT_RF_ERROR");
break;
case PH_ERR_NOISE_ERROR:
DEBUG_PRINTF("\t PH_ERR_NOISE_ERROR");
break;
case PH_ERR_ABORTED:
DEBUG_PRINTF("\t PH_ERR_ABORTED");
break;
//case PH_ERR_RF_TURNOFF:
// DEBUG_PRINTF("\t PH_ERR_RF_TURNOFF");
// break;
case PH_ERR_INTERNAL_ERROR:
DEBUG_PRINTF("\t PH_ERR_INTERNAL_ERROR");
break;
case PH_ERR_INVALID_DATA_PARAMS:
DEBUG_PRINTF("\t PH_ERR_INVALID_DATA_PARAMS");
break;
case PH_ERR_INVALID_PARAMETER:
DEBUG_PRINTF("\t PH_ERR_INVALID_PARAMETER");
break;
case PH_ERR_PARAMETER_OVERFLOW:
DEBUG_PRINTF("\t PH_ERR_PARAMETER_OVERFLOW");
break;
case PH_ERR_UNSUPPORTED_PARAMETER:
DEBUG_PRINTF("\t PH_ERR_UNSUPPORTED_PARAMETER");
break;
case PH_ERR_OSAL_ERROR:
DEBUG_PRINTF("\t PH_ERR_OSAL_ERROR");
break;
case PHAC_DISCLOOP_LPCD_NO_TECH_DETECTED:
DEBUG_PRINTF("\t PHAC_DISCLOOP_LPCD_NO_TECH_DETECTED");
break;
case PHAC_DISCLOOP_COLLISION_PENDING:
DEBUG_PRINTF("\t PHAC_DISCLOOP_COLLISION_PENDING");
break;
default:
DEBUG_PRINTF("\t 0x%x",(wStatus & PH_ERR_MASK));
break;
}
}
#endif
// Print technology being resolved
void PrintTechnology(uint8_t TechType)
{
switch(TechType)
{
case PHAC_DISCLOOP_POS_BIT_MASK_A:
DEBUG_PRINTF ("\tResolving Type A... \n");
break;
case PHAC_DISCLOOP_POS_BIT_MASK_B:
DEBUG_PRINTF ("\tResolving Type B... \n");
break;
case PHAC_DISCLOOP_POS_BIT_MASK_F212:
DEBUG_PRINTF ("\tResolving Type F with baud rate 212... \n");
break;
case PHAC_DISCLOOP_POS_BIT_MASK_F424:
DEBUG_PRINTF ("\tResolving Type F with baud rate 424... \n");
break;
case PHAC_DISCLOOP_POS_BIT_MASK_V:
DEBUG_PRINTF ("\tResolving Type V... \n");
break;
default:
break;
}
}
phStatus_t NfcRdLibInit(void) {
phStatus_t status;
status = phbalReg_Stub_Init(
&sBalReader,
sizeof(phbalReg_Stub_DataParams_t));
CHECK_STATUS(status);
status = phPlatform_Init(&sPlatform);
CHECK_SUCCESS(status);
status = phOsal_Event_Init();
CHECK_STATUS(status);
Set_Interrupt();
#ifdef NXPBUILD__PHHAL_HW_PN5180
status = phbalReg_SetConfig(
&sBalReader,
PHBAL_REG_CONFIG_HAL_HW_TYPE,
PHBAL_REG_HAL_HW_PN5180);
#endif
#ifdef NXPBUILD__PHHAL_HW_RC523
status = phbalReg_SetConfig(
&sBalReader,
PHBAL_REG_CONFIG_HAL_HW_TYPE,
PHBAL_REG_HAL_HW_RC523);
#endif
#ifdef NXPBUILD__PHHAL_HW_RC663
status = phbalReg_SetConfig(
&sBalReader,
PHBAL_REG_CONFIG_HAL_HW_TYPE,
PHBAL_REG_HAL_HW_RC663);
#endif
CHECK_STATUS(status);
status = phbalReg_SetPort(
&sBalReader,
SPI_CONFIG);
CHECK_STATUS(status);
status = phbalReg_OpenPort(&sBalReader);
CHECK_STATUS(status);
status = phhalHw_Nfc_IC_Init(
&sHal_Nfc_Ic,
sizeof(phhalHw_Nfc_Ic_DataParams_t),
&sBalReader,
0,
bHalBufferTx,
sizeof(bHalBufferTx),
bHalBufferRx,
sizeof(bHalBufferRx));
sHal_Nfc_Ic.sHal.bBalConnectionType = PHHAL_HW_BAL_CONNECTION_SPI;
Configure_Device(&sHal_Nfc_Ic);
pHal = &sHal_Nfc_Ic.sHal;
status = phpalI14443p3a_Sw_Init(
&spalI14443p3a,
sizeof(phpalI14443p3a_Sw_DataParams_t),
&sHal_Nfc_Ic.sHal);
CHECK_STATUS(status);
status = phpalI14443p4a_Sw_Init(
&spalI14443p4a,
sizeof(phpalI14443p4a_Sw_DataParams_t),
&sHal_Nfc_Ic.sHal);
CHECK_STATUS(status);
status = phpalI14443p4_Sw_Init(
&spalI14443p4,
sizeof(phpalI14443p4_Sw_DataParams_t),
&sHal_Nfc_Ic.sHal);
CHECK_STATUS(status);
status = phpalI14443p3b_Sw_Init(
&spalI14443p3b,
sizeof(spalI14443p3b),
&sHal_Nfc_Ic.sHal);
CHECK_STATUS(status);
status = phpalFelica_Sw_Init(
&spalFelica,
sizeof(phpalFelica_Sw_DataParams_t),
&sHal_Nfc_Ic.sHal);
CHECK_STATUS(status);
status = phpalI18092mPI_Sw_Init(
&spalI18092mPI,
sizeof(phpalI18092mPI_Sw_DataParams_t),
pHal);
CHECK_STATUS(status);
status = phpalMifare_Sw_Init(
&spalMifare,
sizeof(phpalMifare_Sw_DataParams_t),
&sHal_Nfc_Ic.sHal,
&spalI14443p4);
CHECK_STATUS(status);
#ifndef NXPBUILD__PHHAL_HW_RC523
status = phpalI18000p3m3_Sw_Init(
&spalI18000p3m3,
sizeof(phpalI18000p3m3_Sw_DataParams_t),
pHal);
CHECK_STATUS(status);
status = phalI18000p3m3_Sw_Init(
&salI18000p3m3,
sizeof(phalI18000p3m3_Sw_DataParams_t),
&spalI18000p3m3);
CHECK_STATUS(status);
status = phpalSli15693_Sw_Init(
&spalSli15693,
sizeof(phpalSli15693_Sw_DataParams_t),
pHal);
CHECK_STATUS(status);
#endif
status = phalT1T_Sw_Init(
&alT1T,
sizeof(phalT1T_Sw_DataParams_t),
&spalI14443p3a);
CHECK_STATUS(status);
status = phpalMifare_Sw_Init(&spalMifare, sizeof(phpalMifare_Sw_DataParams_t), &sHal_Nfc_Ic.sHal, NULL);
CHECK_STATUS(status);
// Initialize the keystore component
// Not used at the moment
status = phKeyStore_Sw_Init(
&sSwkeyStore,
sizeof(phKeyStore_Sw_DataParams_t),
&sKeyEntries[0],
NUMBER_OF_KEYENTRIES,
&sKeyVersionPairs[0],
NUMBER_OF_KEYVERSIONPAIRS,
&sKUCEntries[0],
NUMBER_OF_KUCENTRIES);
CHECK_SUCCESS(status);
status = phalMful_Sw_Init(&salMfu, sizeof(phalMful_Sw_DataParams_t), &spalMifare, NULL, NULL, NULL);
CHECK_STATUS(status);
status = phacDiscLoop_Sw_Init(
&sDiscLoop,
sizeof(phacDiscLoop_Sw_DataParams_t),
&sHal_Nfc_Ic.sHal);
CHECK_STATUS(status);
#ifdef NXPBUILD__PHHAL_HW_RC523
status = phhalHw_Rc523_SetListenParameters(
&sHal_Nfc_Ic.sHal,
&sens_res[0],
&nfc_id1[0],
sel_res,
&poll_res[0],
nfc_id3);
CHECK_SUCCESS(status);
#endif
#ifdef NXPBUILD__PHHAL_HW_PN5180
status = phhalHw_Pn5180_SetListenParameters(
&sHal_Nfc_Ic.sHal,
&sens_res[0],
&nfc_id1[0],
sel_res,
&poll_res[0],
nfc_id3);
CHECK_SUCCESS(status);
#endif
sDiscLoop.pPal1443p3aDataParams = &spalI14443p3a;
sDiscLoop.pPal1443p3bDataParams = &spalI14443p3b;
sDiscLoop.pPal1443p4aDataParams = &spalI14443p4a;
sDiscLoop.pPal14443p4DataParams = &spalI14443p4;
#ifndef NXPBUILD__PHHAL_HW_RC523
sDiscLoop.pPal18000p3m3DataParams = &spalI18000p3m3;
sDiscLoop.pAl18000p3m3DataParams = &salI18000p3m3;
sDiscLoop.pPalSli15693DataParams = &spalSli15693;
#endif
sDiscLoop.pPal18092mPIDataParams = &spalI18092mPI;
sDiscLoop.pPalFelicaDataParams = &spalFelica;
sDiscLoop.pAlT1TDataParams = &alT1T;
sDiscLoop.pHalDataParams = &sHal_Nfc_Ic.sHal;
sDiscLoop.sTypeATargetInfo.sTypeA_P2P.pGi = (uint8_t *)GI;
sDiscLoop.sTypeATargetInfo.sTypeA_P2P.bGiLength = sizeof(GI);
sDiscLoop.sTypeFTargetInfo.sTypeF_P2P.pGi = (uint8_t *)GI;
sDiscLoop.sTypeFTargetInfo.sTypeF_P2P.bGiLength = sizeof(GI);
sDiscLoop.sTypeATargetInfo.sTypeA_P2P.pAtrRes = aData;
sDiscLoop.sTypeFTargetInfo.sTypeF_P2P.pAtrRes = aData;
sDiscLoop.sTypeATargetInfo.sTypeA_I3P4.pAts = aData;
return PH_ERR_SUCCESS;
}
#ifdef NXPBUILD__PHHAL_HW_RC663
phStatus_t ConfigureLPCD(void)
{
phStatus_t status;
uint8_t bValueI;
uint8_t bValueQ;
status = phhalHw_Rc663_Cmd_Lpcd_GetConfig(pHal, &bValueI, &bValueQ);
CHECK_STATUS(status);
status = phhalHw_Rc663_Cmd_Lpcd_SetConfig(
pHal,
PHHAL_HW_RC663_CMD_LPCD_MODE_POWERDOWN,
bValueI,
bValueQ,
1,
100);
status = phacDiscLoop_SetConfig(&sDiscLoop, PHAC_DISCLOOP_CONFIG_ENABLE_LPCD, PH_ON);
CHECK_STATUS(status);
return status;
}
#endif
#ifdef NXPBUILD__PHHAL_HW_PN5180
phStatus_t ConfigureLPCD(void)
{
phStatus_t status;
uint16_t wConfig = PHHAL_HW_CONFIG_LPCD_REF;
uint16_t wValue;
status = phhalHw_Pn5180_Int_LPCD_GetConfig(pHal, wConfig, &wValue);
CHECK_STATUS(status);
wValue = PHHAL_HW_PN5180_LPCD_MODE_POWERDOWN;
wConfig = PHHAL_HW_CONFIG_LPCD_MODE;
status = phhalHw_Pn5180_Int_LPCD_SetConfig(
pHal,
wConfig,
wValue
);
status = phacDiscLoop_SetConfig(&sDiscLoop, PHAC_DISCLOOP_CONFIG_ENABLE_LPCD, PH_ON);
CHECK_STATUS(status);
return status;
}
#endif
void Discover_Init() {
void *pDataParams = &sDiscLoop;
phStatus_t status = PHAC_DISCLOOP_LPCD_NO_TECH_DETECTED;
status = phacDiscLoop_GetConfig(pDataParams, PHAC_DISCLOOP_CONFIG_PAS_POLL_TECH_CFG, &bSavePollTechCfg);
CHECK_STATUS(status);
#ifdef NXPBUILD__PHHAL_HW_RC523
wEntryPoint = PHAC_DISCLOOP_ENTRY_POINT_LISTEN;
#else
wEntryPoint = PHAC_DISCLOOP_ENTRY_POINT_POLL;
status = PHAC_DISCLOOP_LPCD_NO_TECH_DETECTED;
#endif
}
DiscoverResult_t Discover()
{
phStatus_t status = PHAC_DISCLOOP_LPCD_NO_TECH_DETECTED;
void *pDataParams = &sDiscLoop;
uint8_t bIndex;
uint16_t wValue;
#ifndef NXPBUILD__PHHAL_HW_RC523
if((status & PH_ERR_MASK) == PHAC_DISCLOOP_LPCD_NO_TECH_DETECTED)
{
status = ConfigureLPCD();
CHECK_STATUS(status);
}
#endif
status = phacDiscLoop_SetConfig(pDataParams, PHAC_DISCLOOP_CONFIG_NEXT_POLL_STATE, PHAC_DISCLOOP_POLL_STATE_DETECTION);
CHECK_STATUS(status);
status = phacDiscLoop_SetConfig(pDataParams, PHAC_DISCLOOP_CONFIG_PAS_POLL_TECH_CFG, bSavePollTechCfg);
CHECK_STATUS(status);
status = phhalHw_FieldOff(pHal);
CHECK_STATUS(status);
status = phacDiscLoop_Run(pDataParams, wEntryPoint);
if((status & PH_ERR_MASK) == PHAC_DISCLOOP_MULTI_TECH_DETECTED)
{
DEBUG_PRINTF (" \n Multiple technology detected: \n");
status = phacDiscLoop_GetConfig(pDataParams, PHAC_DISCLOOP_CONFIG_TECH_DETECTED, &wTagsDetected);
CHECK_STATUS(status);
if(PHAC_DISCLOOP_CHECK_ANDMASK(wTagsDetected, PHAC_DISCLOOP_POS_BIT_MASK_A))
{
DEBUG_PRINTF (" \tType A detected... \n");
}
if(PHAC_DISCLOOP_CHECK_ANDMASK(wTagsDetected, PHAC_DISCLOOP_POS_BIT_MASK_B))
{
DEBUG_PRINTF (" \tType B detected... \n");
}
if(PHAC_DISCLOOP_CHECK_ANDMASK(wTagsDetected, PHAC_DISCLOOP_POS_BIT_MASK_F212))
{
DEBUG_PRINTF (" \tType F detected with baud rate 212... \n");
}
if(PHAC_DISCLOOP_CHECK_ANDMASK(wTagsDetected, PHAC_DISCLOOP_POS_BIT_MASK_F424))
{
DEBUG_PRINTF (" \tType F detected with baud rate 424... \n");
}
#ifndef NXPBUILD__PHHAL_HW_RC523
if(PHAC_DISCLOOP_CHECK_ANDMASK(wTagsDetected, PHAC_DISCLOOP_POS_BIT_MASK_V))
{
DEBUG_PRINTF(" \tType V / ISO 15693 / T5T detected... \n");
}
#endif
for(bIndex = 0; bIndex < PHAC_DISCLOOP_PASS_POLL_MAX_TECHS_SUPPORTED; bIndex++)
{
if(PHAC_DISCLOOP_CHECK_ANDMASK(wTagsDetected, (1 << bIndex)))
{
status = phacDiscLoop_SetConfig(pDataParams, PHAC_DISCLOOP_CONFIG_PAS_POLL_TECH_CFG, (1 << bIndex));
CHECK_STATUS(status);
break;
}
}
//Print the technology resolved
#ifdef DEBUG
PrintTechnology((1 << bIndex));
#endif
// Set Discovery Poll State to collision resolution
status = phacDiscLoop_SetConfig(pDataParams, PHAC_DISCLOOP_CONFIG_NEXT_POLL_STATE, PHAC_DISCLOOP_POLL_STATE_COLLISION_RESOLUTION);
CHECK_STATUS(status);
// Restart discovery loop in poll mode from collision resolution phase
status = phacDiscLoop_Run(pDataParams, wEntryPoint);
}
if((status & PH_ERR_MASK) == PHAC_DISCLOOP_MULTI_DEVICES_RESOLVED)
{
// Get Detected Technology Type
status = phacDiscLoop_GetConfig(&sDiscLoop, PHAC_DISCLOOP_CONFIG_TECH_DETECTED, &wTagsDetected);
CHECK_STATUS(status);
// Get number of tags detected
status = phacDiscLoop_GetConfig(&sDiscLoop, PHAC_DISCLOOP_CONFIG_NR_TAGS_FOUND, &wNumberOfTags);
CHECK_STATUS(status);
DEBUG_PRINTF (" \n Multiple cards resolved: %d cards \n",wNumberOfTags);
#ifdef DEBUG
PrintTagInfo(pDataParams, wNumberOfTags, wTagsDetected);
#endif
if(wNumberOfTags > 1)
{
// Get 1st Detected Technology and Activate device at index 0
for(bIndex = 0; bIndex < PHAC_DISCLOOP_PASS_POLL_MAX_TECHS_SUPPORTED; bIndex++)
{
if(PHAC_DISCLOOP_CHECK_ANDMASK(wTagsDetected, (1 << bIndex)))
{
DEBUG_PRINTF("\t Activating one card...\n");
status = phacDiscLoop_ActivateCard(pDataParams, bIndex, 0);
break;
}
}
if(((status & PH_ERR_MASK) == PHAC_DISCLOOP_DEVICE_ACTIVATED) ||
((status & PH_ERR_MASK) == PHAC_DISCLOOP_PASSIVE_TARGET_ACTIVATED) ||
((status & PH_ERR_MASK) == PHAC_DISCLOOP_MERGED_SEL_RES_FOUND))
{
// Get Detected Technology Type
status = phacDiscLoop_GetConfig(&sDiscLoop, PHAC_DISCLOOP_CONFIG_TECH_DETECTED, &wTagsDetected);
CHECK_STATUS(status);
#ifdef DEBUG
PrintTagInfo(pDataParams, 0x01, wTagsDetected);
#endif
#ifdef NXPBUILD__PHHAL_HW_RC523
wEntryPoint = PHAC_DISCLOOP_ENTRY_POINT_LISTEN;
#endif
// Change to DR_FOUND? How to reproduce multiple cards resolved?
return DR_UNKNOWN;
}
else
{
PRINT_INFO("\t\tCard activation failed...\n");
}
}
}
else if (((status & PH_ERR_MASK) == PHAC_DISCLOOP_NO_TECH_DETECTED) ||
((status & PH_ERR_MASK) == PHAC_DISCLOOP_NO_DEVICE_RESOLVED))
{
// Switch to LISTEN Mode
#ifdef NXPBUILD__PHHAL_HW_RC523
wEntryPoint = PHAC_DISCLOOP_ENTRY_POINT_LISTEN;
#endif
}
else if((status & PH_ERR_MASK) == PHAC_DISCLOOP_EXTERNAL_RFON)
{
// If external RF is detected during POLL, return back so that the application
// can restart the loop in LISTEN mode
#ifdef NXPBUILD__PHHAL_HW_RC523
wEntryPoint = PHAC_DISCLOOP_ENTRY_POINT_LISTEN;
#endif
}
else if((status & PH_ERR_MASK) == PHAC_DISCLOOP_EXTERNAL_RFOFF)
{
// Enters here if in the target/card mode and external RF is not available
// Wait for LISTEN timeout till an external RF is detected.
// Application may choose to go into standby at this point.
#ifdef NXPBUILD__PHHAL_HW_RC523
status = phOsal_Event_Consume(E_PH_OSAL_EVT_RF, E_PH_OSAL_EVT_SRC_ISR);
CHECK_STATUS(status);
status = phhalHw_SetConfig(pHal, PHHAL_HW_CONFIG_RFON_INTERRUPT, PH_ON);
CHECK_STATUS(status);
status = phOsal_Event_WaitAny(E_PH_OSAL_EVT_RF, LISTEN_PHASE_TIME_MS , NULL);
if((status & PH_ERR_MASK) == PH_ERR_IO_TIMEOUT)
{
// With RC523 board we're always landing here after the first call
// to this function. Listen mode not really supported by RC523?
wEntryPoint = PHAC_DISCLOOP_ENTRY_POINT_POLL;
}
else
{
wEntryPoint = PHAC_DISCLOOP_ENTRY_POINT_LISTEN;
}
#endif
}
else if((status & PH_ERR_MASK) == PHAC_DISCLOOP_ACTIVATED_BY_PEER)
{
DEBUG_PRINTF (" \n Device activated in listen mode... \n");
#ifdef NXPBUILD__PHHAL_HW_RC523
// On successful activated by Peer, switch to LISTEN mode
wEntryPoint = PHAC_DISCLOOP_ENTRY_POINT_LISTEN;
#endif
}
else if((status & PH_ERR_MASK) == PHAC_DISCLOOP_ACTIVE_TARGET_ACTIVATED)
{
DEBUG_PRINTF (" \n Active target detected... \n");
#ifdef NXPBUILD__PHHAL_HW_RC523
// Target Activated successful, switch to LISTEN mode
wEntryPoint = PHAC_DISCLOOP_ENTRY_POINT_LISTEN;
#endif
}
else if((status & PH_ERR_MASK) == PHAC_DISCLOOP_PASSIVE_TARGET_ACTIVATED)
{
DEBUG_PRINTF (" \n Passive target detected... \n");
// Get Detected Technology Type
status = phacDiscLoop_GetConfig(&sDiscLoop, PHAC_DISCLOOP_CONFIG_TECH_DETECTED, &wTagsDetected);
CHECK_STATUS(status);
#ifdef DEBUG
PrintTagInfo(pDataParams, 1, wTagsDetected);
#endif
#ifdef NXPBUILD__PHHAL_HW_RC523
// Target Activated successful, switch to LISTEN mode
wEntryPoint = PHAC_DISCLOOP_ENTRY_POINT_LISTEN;
#endif
}
else if((status & PH_ERR_MASK) == PHAC_DISCLOOP_MERGED_SEL_RES_FOUND)
{
DEBUG_PRINTF (" \n Device having T4T and NFC-DEP support detected... \n");
// Get Detected Technology Type
status = phacDiscLoop_GetConfig(&sDiscLoop, PHAC_DISCLOOP_CONFIG_TECH_DETECTED, &wTagsDetected);
CHECK_STATUS(status);
#ifdef DEBUG
PrintTagInfo(pDataParams, 1, wTagsDetected);
#endif
#ifdef NXPBUILD__PHHAL_HW_RC523
// Switch to LISTEN mode
wEntryPoint = PHAC_DISCLOOP_ENTRY_POINT_LISTEN;
#endif
}