/
hydranfc_cmd_sniff.c
1277 lines (1093 loc) · 35.4 KB
/
hydranfc_cmd_sniff.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
/*
* HydraBus/HydraNFC
*
* Copyright (C) 2014-2015 Benjamin VERNOUX
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdarg.h>
#include <stdio.h> /* sprintf */
#include "ch.h"
#include "hal.h"
#include "mcu.h"
#include "trf797x.h"
#include "types.h"
#include "hydranfc.h"
#include "hydranfc_cmd_sniff_iso14443.h"
#include "hydranfc_cmd_sniff_downsampling.h"
#include "common.h"
#include "microsd.h"
#include "ff.h"
/* INIT_NFC_PROTOCOL */
typedef enum {
ISO14443A = 0,
ISO14443B
} INIT_NFC_PROTOCOL;
filename_t write_filename;
#define TRF7970_DATA_SIZE (384)
#define SPI_RX_DMA_SIZE (4)
uint8_t spi_rx_dma_buf[2][SPI_RX_DMA_SIZE+12] __attribute__ ((aligned (16)));
uint8_t htoa[16] = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
uint8_t tmp_buf[16];
uint8_t irq_no;
volatile int irq_sampling = 0;
static uint32_t old_u32_data, u32_data, old_data_bit;
/*
* SPI1 configuration structure.
* Speed not used in Slave mode, CPHA=0, CPOL=0, 8bits frames, MSb transmitted first.
* The slave select line is internally selected.
*/
static const SPIConfig spi1cfg = {
NULL, /* spicb, */
/* HW dependent part.*/
GPIOA, /* Port */
15, /* Pin */
(SPI_CR1_BR_2), /* CR1 Register */
0 /* CR2 Register */
};
#define CountLeadingZero(x) (__CLZ(x))
#define SWAP32(x) (__REV(x))
uint8_t* sniffer_get_buffer(void)
{
return &g_sbuf[0];
}
uint32_t sniffer_get_buffer_max_size(void)
{
return NB_SBUFFER;
}
uint32_t sniffer_get_size(void)
{
return g_sbuf_idx;
}
void initSPI1(void)
{
uint32_t i;
/* Clear buffer */
for(i=0; i<SPI_RX_DMA_SIZE; i++) {
spi_rx_dma_buf[0][i]=0;
spi_rx_dma_buf[1][i]=0;
}
/*
* Initializes the SPI driver 1.
*/
spiSlaveStart(&SPID1, &spi1cfg);
/* SPI DMA Start using double buffer */
dmaStreamSetMemory0(SPID1.dmarx, spi_rx_dma_buf[0]);
dmaStreamSetMemory1(SPID1.dmarx, spi_rx_dma_buf[1]);
dmaStreamSetTransactionSize(SPID1.dmarx, SPI_RX_DMA_SIZE);
dmaStreamSetMode(SPID1.dmarx, SPID1.rxdmamode | STM32_DMA_CR_MINC |
STM32_DMA_CR_EN);
}
void tprint_str(const char *data, uint32_t size)
{
if (size > 0) {
if (SDU1.config->usbp->state == USB_ACTIVE)
chnWrite((BaseSequentialStream*)&SDU1, (uint8_t *)data, size);
if (SDU2.config->usbp->state == USB_ACTIVE)
chnWrite((BaseSequentialStream*)&SDU2, (uint8_t *)data, size);
}
}
void tprintf(const char *fmt, ...)
{
va_list va_args;
int real_size;
#define TPRINTF_BUFF_SIZE (255)
char tprintf_buff[TPRINTF_BUFF_SIZE+1];
va_start(va_args, fmt);
real_size = vsnprintf(tprintf_buff, TPRINTF_BUFF_SIZE, fmt, va_args);
if (SDU1.config->usbp->state == USB_ACTIVE)
chnWrite((BaseSequentialStream*)&SDU1, (uint8_t *)tprintf_buff, real_size);
if (SDU2.config->usbp->state == USB_ACTIVE)
chnWrite((BaseSequentialStream*)&SDU2, (uint8_t *)tprintf_buff, real_size);
va_end(va_args);
}
/* Stop driver to exit Sniff NFC mode */
void terminate_sniff_nfc(void)
{
spiStop(&SPID1);
}
static void init_sniff_nfc(INIT_NFC_PROTOCOL iso_proto)
{
tprintf("TRF7970A chipset init start\r\n");
/* Init TRF797x */
Trf797xResetFIFO();
Trf797xInitialSettings();
/* ************************************************************* */
/* Configure NFC chipset as ISO14443B (works with ISO14443A too) */
/* Configure Chip Status Register (0x00) to 0x21 (RF output active, 5v operations) */
tmp_buf[0] = CHIP_STATE_CONTROL;
tmp_buf[1] = 0x21;
Trf797xWriteSingle(tmp_buf, 2);
tmp_buf[0] = ISO_CONTROL;
/* Default configure Mode ISO Control Register (0x01) to 0x24 (NFC Card Emulation, Type A) */
tmp_buf[1] = 0x24;
if(iso_proto == ISO14443A)
{
/* Configure Mode ISO Control Register (0x01) to 0x24 (NFC Card Emulation, Type A) */
tmp_buf[1] = 0x24;
}else if(iso_proto == ISO14443B)
{
/* Configure Mode ISO Control Register (0x01) to 0x25 (NFC Card Emulation, Type B) */
tmp_buf[1] = 0x25;
}
Trf797xWriteSingle(tmp_buf, 2);
/* Write Modulator and SYS_CLK Control Register (0x09) (13.56Mhz SYS_CLK and default Clock 3.39Mhz)) */
tmp_buf[0] = MODULATOR_CONTROL;
tmp_buf[1] = 0x11; /* Freq 3.39Mhz */
Trf797xWriteSingle(tmp_buf, 2);
/* Configure Regulator to 0x87 (Auto & 5V) */
tmp_buf[0] = REGULATOR_CONTROL;
tmp_buf[1] = 0x87;
Trf797xWriteSingle(tmp_buf, 2);
/* Configure RX Special Settings
* Bandpass 450 kHz to 1.5 MHz B5=1/Bandpass 100 kHz to 1.5 MHz=B4=1,
* Gain reduction for 5 dB(Can be changed) B2=1&B3=0
* AGC no limit B0=1 */
tmp_buf[0] = RX_SPECIAL_SETTINGS;
tmp_buf[1] = 0x35;
Trf797xWriteSingle(tmp_buf, 2);
/* Configure Test Settings 1 to BIT6/0x40 => MOD Pin becomes receiver subcarrier output (Digital Output for RX/TX) => Used for Sniffer */
tmp_buf[0] = TEST_SETTINGS_1;
tmp_buf[1] = BIT6;
Trf797xWriteSingle(tmp_buf, 2);
Trf797xStopDecoders(); /* Disable Receiver */
Trf797xRunDecoders(); /* Enable Receiver */
tmp_buf[0] = CHIP_STATE_CONTROL;
Trf797xReadSingle(tmp_buf, 1);
tprintf("Chip Status Register(0x00) read=0x%.2lX (shall be 0x21)\r\n", (uint32_t)tmp_buf[0]);
tmp_buf[0] = ISO_CONTROL;
Trf797xReadSingle(tmp_buf, 1);
tprintf("ISO Control Register(0x01) read=0x%.2lX (shall be 0x24 TypeA / 0x25 TypeB)\r\n", (uint32_t)tmp_buf[0]);
tmp_buf[0] = ISO_14443B_OPTIONS;
Trf797xReadSingle(tmp_buf, 1);
tprintf("ISO 14443B Options Register(0x02) read=0x%.2lX\r\n", (uint32_t)tmp_buf[0]);
tmp_buf[0] = ISO_14443A_OPTIONS;
Trf797xReadSingle(tmp_buf, 1);
tprintf("ISO 14443A Options Register(0x03) read=0x%.2lX\r\n", (uint32_t)tmp_buf[0]);
tmp_buf[0] = MODULATOR_CONTROL;
Trf797xReadSingle(tmp_buf, 1);
tprintf("Modulator Control Register(0x09) read=0x%.2lX (shall be 0x11)\r\n", (uint32_t)tmp_buf[0]);
tmp_buf[0] = RX_SPECIAL_SETTINGS;
Trf797xReadSingle(tmp_buf, 1);
tprintf("RX SpecialSettings Register(0x0A) read=0x%.2lX\r\n", (uint32_t)tmp_buf[0]);
tmp_buf[0] = REGULATOR_CONTROL;
Trf797xReadSingle(tmp_buf, 1);
tprintf("Regulator Control Register(0x0B) read=0x%.2lX (shall be 0x87)\r\n", (uint32_t)tmp_buf[0]);
tmp_buf[0] = TEST_SETTINGS_1;
Trf797xReadSingle(tmp_buf, 1);
tprintf("Test Settings Register(0x1A) read=0x%.2lX (shall be 0x40)\r\n", (uint32_t)tmp_buf[0]);
tprintf("TRF7970A chipset init end\r\n");
initSPI1();
tprintf("Init SPI1 end\r\n");
tprintf("TRF7970A sniffer started\r\n");
}
/* Wait DMA Buffer Swap and Read Data
* size shall be less or equal to SPI_RX_DMA_SIZE
* */
__attribute__ ((always_inline)) static inline uint32_t WaitGetDMABuffer(void)
{
static uint32_t buf_no=1; /* Init as Target1 */
uint32_t cr, new_buf_no;
uint32_t val_u32;
while (1) {
cr = (SPID1.dmarx->stream->CR);
if ( (cr&DMA_SxCR_CT)>0 ) {
/* Current DMA memory Target1 */
new_buf_no = 0; /* Read the Target0 */
} else {
/* Current DMA memory Target0 */
new_buf_no = 1; /* Read the Target1 */
}
if (new_buf_no != buf_no) {
buf_no = new_buf_no;
val_u32 = *((uint32_t*)(&spi_rx_dma_buf[buf_no][0]));
return( SWAP32(val_u32) ); /* Swap 32bits Data for Little Endian */
}
if (K4_BUTTON || USER_BUTTON)
break; // ABORT
}
return 0;
}
/* Return 0 if OK else < 0 error code */
int sniff_write_file(uint8_t* buffer, uint32_t size)
{
uint32_t i;
FRESULT err;
FIL FileObject;
uint32_t bytes_written;
if (size == 0) {
return -1;
}
if (is_fs_ready()==FALSE) {
if (mount() != 0) {
return -5;
}
} else {
umount();
if (mount() != 0) {
return -5;
}
}
/* Save data in file */
for(i=0; i<999; i++) {
sprintf(write_filename.filename, "0:nfc_sniff_%ld.txt", i);
err = f_open(&FileObject, write_filename.filename, FA_WRITE | FA_CREATE_NEW);
if (err == FR_OK) {
break;
}
}
if (err == FR_OK) {
err = f_write(&FileObject, buffer, size, (void *)&bytes_written);
if (err != FR_OK) {
f_close(&FileObject);
umount();
return -3;
}
err = f_close(&FileObject);
if (err != FR_OK) {
umount();
return -4;
}
} else {
umount();
return -2;
}
umount();
return 0;
}
/*
Write sniffed data in file and display those data on Terminal if connected.
In case of Write Error(No SDCard, Write error or no data) D5 LED blink quickly
In case of Write OK D5 LED blink quickly
*/
void sniff_log(void)
{
int i;
chSysUnlock();
terminate_sniff_nfc();
D4_OFF;
D5_OFF;
if ( sniff_write_file(sniffer_get_buffer(), sniffer_get_size()) < 0 ) {
/* Display sniffed data */
tprintf("Sniffed data:\r\n");
tprint_str( (char*)sniffer_get_buffer(), sniffer_get_size() );
tprintf("\r\n");
tprintf("\r\n");
write_file_get_last_filename(&write_filename);
tprintf("write_file %s buffer=0x%08LX size=%ld bytes\r\n",
&write_filename.filename[2], sniffer_get_buffer(), sniffer_get_size());
tprintf("write_file() error\r\n", write_filename);
tprintf("\r\n");
/* Error Red LED blink */
for(i=0; i<4; i++) {
D5_ON;
DelayUs(50000);
D5_OFF;
DelayUs(50000);
}
} else {
/* Display sniffed data */
tprintf("Sniffed data:\r\n");
tprint_str( (char*)sniffer_get_buffer(), sniffer_get_size() );
tprintf("\r\n");
write_file_get_last_filename(&write_filename);
tprintf("write_file %s buffer=0x%08LX size=%ld bytes\r\n",
&write_filename.filename[2], sniffer_get_buffer(), sniffer_get_size());
tprintf("write_file() OK\r\n");
/* All is OK Green LED bink */
for(i=0; i<4; i++) {
D4_ON;
DelayUs(50000);
D4_OFF;
DelayUs(50000);
}
}
D4_OFF;
D5_OFF;
}
/* Return TRUE if sniff stopepd by K4, else return FALSE */
__attribute__ ((always_inline)) static inline
bool sniff_wait_data_change_or_exit(void)
{
/* Wait until data change */
while (TRUE) {
u32_data = WaitGetDMABuffer();
/* Search for an edge/data */
if (old_u32_data != u32_data) {
break;
} else {
old_data_bit = (uint32_t)(u32_data&1);
}
if ( (K4_BUTTON) || (USER_BUTTON) ) {
sniff_log();
return TRUE;
}
}
return FALSE;
}
__attribute__ ((always_inline)) static inline
void sniff_write_pcd(void)
{
/* Write output buffer 4 space (data format Miller Modified):
It means Reader/Writer (PCD – Proximity Coupling Device)
*/
uint32_t i, nb_cycles;
uint8_t val;
i = g_sbuf_idx;
g_sbuf[i+0] = '\r';
g_sbuf[i+1] = '\n';
nb_cycles = get_cyclecounter();
val = ((nb_cycles & 0xFF000000) >> 24);
g_sbuf[i+2] = htoa[(val & 0xF0) >> 4];
g_sbuf[i+3] = htoa[(val & 0x0F)];
val = ((nb_cycles & 0x00FF0000) >> 16);
g_sbuf[i+4] = htoa[(val & 0xF0) >> 4];
g_sbuf[i+5] = htoa[(val & 0x0F)];
val = ((nb_cycles & 0x0000FF00) >> 8);
g_sbuf[i+6] = htoa[(val & 0xF0) >> 4];
g_sbuf[i+7] = htoa[(val & 0x0F)];
val = (nb_cycles & 0x000000FF);
g_sbuf[i+8] = htoa[(val & 0xF0) >> 4];
g_sbuf[i+9] = htoa[(val & 0x0F)];
g_sbuf[i+10] = '\t';
g_sbuf[i+11] = 'R';
g_sbuf[i+12] = 'D';
g_sbuf[i+13] = 'R';
g_sbuf[i+14] = '\t';
g_sbuf_idx +=15;
}
__attribute__ ((always_inline)) static inline
void sniff_write_picc(void)
{
/* Write output buffer "TAG"+1 space (data format Manchester):
It means TAG(PICC – Proximity Integrated Circuit Card)
*/
uint32_t i, nb_cycles;
uint8_t val;
i = g_sbuf_idx;
g_sbuf[i+0] = '\r';
g_sbuf[i+1] = '\n';
nb_cycles = get_cyclecounter();
val = ((nb_cycles & 0xFF000000) >> 24);
g_sbuf[i+2] = htoa[(val & 0xF0) >> 4];
g_sbuf[i+3] = htoa[(val & 0x0F)];
val = ((nb_cycles & 0x00FF0000) >> 16);
g_sbuf[i+4] = htoa[(val & 0xF0) >> 4];
g_sbuf[i+5] = htoa[(val & 0x0F)];
val = ((nb_cycles & 0x0000FF00) >> 8);
g_sbuf[i+6] = htoa[(val & 0xF0) >> 4];
g_sbuf[i+7] = htoa[(val & 0x0F)];
val = (nb_cycles & 0x000000FF);
g_sbuf[i+8] = htoa[(val & 0xF0) >> 4];
g_sbuf[i+9] = htoa[(val & 0x0F)];
g_sbuf[i+10] = '\t';
g_sbuf[i+11] = 'T';
g_sbuf[i+12] = 'A';
g_sbuf[i+13] = 'G';
g_sbuf[i+14] = '\t';
g_sbuf_idx +=15;
}
__attribute__ ((always_inline)) static inline
void sniff_write_unknown_protocol(uint8_t data)
{
/* Unknown Protocol */
/* TODO: Detect other coding */
// In Freq of 3.39MHz => 105.9375KHz on 8bits (each bit is 848KHz so 2bits=423.75KHz)
/*
data = (downsample_4x[(f_data>>24)])<<6;
data |= (downsample_4x[((f_data&0x00FF0000)>>16)])<<4;
data |= (downsample_4x[((f_data&0x0000FF00)>>8)])<<2;
data |= (downsample_4x[(f_data&0x000000FF)]);
*/
uint32_t i, nb_cycles;
uint8_t val;
i = g_sbuf_idx;
g_sbuf[i+0] = '\r';
g_sbuf[i+1] = '\n';
nb_cycles = get_cyclecounter();
val = ((nb_cycles & 0xFF000000) >> 24);
g_sbuf[i+2] = htoa[(val & 0xF0) >> 4];
g_sbuf[i+3] = htoa[(val & 0x0F)];
val = ((nb_cycles & 0x00FF0000) >> 16);
g_sbuf[i+4] = htoa[(val & 0xF0) >> 4];
g_sbuf[i+5] = htoa[(val & 0x0F)];
val = ((nb_cycles & 0x0000FF00) >> 8);
g_sbuf[i+6] = htoa[(val & 0xF0) >> 4];
g_sbuf[i+7] = htoa[(val & 0x0F)];
val = (nb_cycles & 0x000000FF);
g_sbuf[i+8] = htoa[(val & 0xF0) >> 4];
g_sbuf[i+9] = htoa[(val & 0x0F)];
g_sbuf[i+10] = '\t';
g_sbuf[i+11] = 'U';
g_sbuf[i+12] = htoa[(data & 0xF0) >> 4];
g_sbuf[i+13] = htoa[(data & 0x0F)];
g_sbuf[i+14] = '\t';
g_sbuf_idx +=15;
}
void sniff_write_raw_protocol(void)
{
/* Raw Protocol */
uint32_t i, nb_cycles;
uint8_t val;
i = g_sbuf_idx;
g_sbuf[i+0] = '\r';
g_sbuf[i+1] = '\n';
nb_cycles = get_cyclecounter();
val = ((nb_cycles & 0xFF000000) >> 24);
g_sbuf[i+2] = htoa[(val & 0xF0) >> 4];
g_sbuf[i+3] = htoa[(val & 0x0F)];
val = ((nb_cycles & 0x00FF0000) >> 16);
g_sbuf[i+4] = htoa[(val & 0xF0) >> 4];
g_sbuf[i+5] = htoa[(val & 0x0F)];
val = ((nb_cycles & 0x0000FF00) >> 8);
g_sbuf[i+6] = htoa[(val & 0xF0) >> 4];
g_sbuf[i+7] = htoa[(val & 0x0F)];
val = (nb_cycles & 0x000000FF);
g_sbuf[i+8] = htoa[(val & 0xF0) >> 4];
g_sbuf[i+9] = htoa[(val & 0x0F)];
g_sbuf[i+10] = '\t';
g_sbuf[i+11] = 'R';
g_sbuf[i+12] = 'A';
g_sbuf[i+13] = 'W';
g_sbuf[i+14] = '\t';
g_sbuf_idx +=15;
}
__attribute__ ((always_inline)) static inline
void sniff_write_eof_protocol(uint32_t timestamp_nb_cycles)
{
uint32_t i, nb_cycles;
uint8_t val;
uint8_t data_buf[1];
uint8_t data;
data_buf[0] = RSSI_LEVELS;
Trf797xReadSingle(data_buf, 1);
data = data_buf[0];
i = g_sbuf_idx;
g_sbuf[i+0] = '\r';
g_sbuf[i+1] = '\n';
nb_cycles = timestamp_nb_cycles;
val = ((nb_cycles & 0xFF000000) >> 24);
g_sbuf[i+2] = htoa[(val & 0xF0) >> 4];
g_sbuf[i+3] = htoa[(val & 0x0F)];
val = ((nb_cycles & 0x00FF0000) >> 16);
g_sbuf[i+4] = htoa[(val & 0xF0) >> 4];
g_sbuf[i+5] = htoa[(val & 0x0F)];
val = ((nb_cycles & 0x0000FF00) >> 8);
g_sbuf[i+6] = htoa[(val & 0xF0) >> 4];
g_sbuf[i+7] = htoa[(val & 0x0F)];
val = (nb_cycles & 0x000000FF);
g_sbuf[i+8] = htoa[(val & 0xF0) >> 4];
g_sbuf[i+9] = htoa[(val & 0x0F)];
g_sbuf[i+10] = '\t';
g_sbuf[i+11] = 'R';
g_sbuf[i+12] = htoa[(data & 0xF0) >> 4];
g_sbuf[i+13] = htoa[(data & 0x0F)];
g_sbuf[i+14] = '\t';
g_sbuf_idx +=15;
}
__attribute__ ((always_inline)) static inline
void sniff_write_8b_ASCII_HEX(uint8_t data, bool add_space)
{
uint32_t i;
i = g_sbuf_idx;
g_sbuf[i+0] = htoa[(data & 0xF0) >> 4];
g_sbuf[i+1] = htoa[(data & 0x0F)];
if (add_space == TRUE) {
g_sbuf[i+2] = ' ';
g_sbuf_idx +=3;
} else {
g_sbuf_idx +=2;
}
}
__attribute__ ((always_inline)) static inline
void sniff_write_Parity_ASCII(uint8_t data)
{
uint32_t i;
i = g_sbuf_idx;
g_sbuf[i+0] = htoa[(data & 0x0F)];
g_sbuf[i+1] = ' ';
g_sbuf_idx +=2;
}
void hydranfc_sniff_14443A(t_hydra_console *con)
{
(void)con;
uint8_t ds_data, tmp_u8_data, tmp_u8_data_nb_bit;
uint32_t f_data, lsh_bit, rsh_bit;
uint32_t rsh_miller_bit, lsh_miller_bit;
uint32_t protocol_found, old_protocol_found; /* 0=Unknown, 1=106kb Miller Modified, 2=106kb Manchester */
uint32_t old_data_counter;
uint32_t nb_data;
tprintf("cmd_nfc_sniff_14443A start TRF7970A configuration as sniffer mode\r\n");
tprintf("Abort/Exit by pressing K4 button\r\n");
init_sniff_nfc(ISO14443A);
tprintf("Starting Sniffer ISO14443-A 106kbps ...\r\n");
/* Wait a bit in order to display all text */
chThdSleepMilliseconds(50);
old_protocol_found = 0;
protocol_found = 0;
g_sbuf_idx = 0;
/* Lock Kernel for sniffer */
chSysLock();
/* Main Loop */
while (TRUE) {
lsh_bit = 0;
rsh_bit = 0;
irq_no = 0;
while (TRUE) {
D4_OFF;
old_data_bit = 0;
f_data = 0;
u32_data = WaitGetDMABuffer();
old_data_bit = (uint32_t)(u32_data&1);
old_u32_data = u32_data;
/* Wait until data change or K4 is pressed to stop/exit */
if (sniff_wait_data_change_or_exit() == TRUE) {
return;
}
/* Log All Data */
TST_ON;
D4_ON;
tmp_u8_data = 0;
tmp_u8_data_nb_bit = 0;
/* Search first edge bit position to synchronize stream */
/* Search an edge on each bit from MSB to LSB */
/* Old bit = 1 so new bit will be 0 => 11111111 10000000 => 00000000 01111111 just need to reverse it to count leading zero */
/* Old bit = 0 so new bit will be 1 => 00000000 01111111 no need to reverse to count leading zero */
lsh_bit = old_data_bit ? (~u32_data) : u32_data;
lsh_bit = CountLeadingZero(lsh_bit);
rsh_bit = 32-lsh_bit;
rsh_miller_bit = 0;
lsh_miller_bit = 32-rsh_miller_bit;
/* Shift data */
f_data = u32_data<<lsh_bit;
/* Next Data */
TST_OFF;
u32_data = WaitGetDMABuffer();
TST_ON;
f_data |= u32_data>>rsh_bit;
/* Todo: Better algorithm to recognize frequency using table and counting number of edge ...
and finaly use majority voting for frequency */
// DownSampling by 4 (input 32bits output 8bits filtered)
// In Freq of 3.39MHz => 105.9375KHz on 8bits (each bit is 848KHz so 2bits=423.75KHz)
ds_data = ((downsample_4x[(f_data>>24)])<<6) |
((downsample_4x[((f_data&0x00FF0000)>>16)])<<4) |
((downsample_4x[((f_data&0x0000FF00)>>8)])<<2) |
(downsample_4x[(f_data&0x000000FF)]);
/* Todo: Find frequency by counting number of consecutive "1" & "0" or the reverse.
* Example0: 1x"1" then 1x"0" => 3.39MHz/2 = Freq 1695KHz
* Example1: 2x"1" then 2x"0" => 3.39MHz/4 = Freq 847.5KHz
* Example2: 4x"1" then 4x"0" => 3.39MHz/8 = Freq 423.75KHz
* Example3: 8x"1" then 8x"0" (8+8) => 3.39MHz/16 = Freq 211.875KHz
* Example4: 16x"1" then 16x"0" (16+16) => 3.39MHz/32 = Freq 105.9375KHz
* Example Miller Modified '0' @~106Khz: 00000000 00111111 11111111 11111111 => 10x"0" then 22x"1" (10+22=32) => 3.39MHz/32 = Freq 105.9375KHz
**/
protocol_found = detected_protocol[ds_data];
switch(protocol_found) {
case MILLER_MODIFIED_106KHZ:
/* Miller Modified@~106Khz Start bit */
old_protocol_found = MILLER_MODIFIED_106KHZ;
sniff_write_pcd();
break;
case MANCHESTER_106KHZ:
/* Manchester@~106Khz Start bit */
old_protocol_found = MANCHESTER_106KHZ;
sniff_write_picc();
break;
default:
/* If previous protocol was Manchester now it should be Miller Modified
(it is a supposition and because Miller modified start after manchester)
*/
if ( MANCHESTER_106KHZ == old_protocol_found ) {
old_protocol_found = MILLER_MODIFIED_106KHZ;
protocol_found = MILLER_MODIFIED_106KHZ;
/* RE Synchronize bit stream to start of bit from (00000000) 11111111 to 00111111 (2 to 3 us at level 0 are not seen) */
/* Nota only first Miller Modified Word does not need this hack because it is well detected it start with (11111111) 00111111 */
// New Word
rsh_miller_bit = 15; /* Between 2 to 3.1us => 7 to 11bits => Average 9bits + 6bits(margin) =< 32-15 = 17 bit */
lsh_miller_bit = 32-rsh_miller_bit;
sniff_write_pcd();
/* Start Bit not included in data buffer */
} else {
old_protocol_found = MILLER_MODIFIED_106KHZ;
protocol_found = MILLER_MODIFIED_106KHZ;
/* RE Synchronize bit stream to start of bit from (00000000) 11111111 to 00111111 (2 to 3 us at level 0 are not seen) */
/* Nota only first Miller Modified Word does not need this hack because it is well detected it start with (11111111) 00111111 */
// New Word
rsh_miller_bit = 15; /* Between 2 to 3.1us => 7 to 11bits => Average 9bits + 6bits(margin) =< 32-15 = 17 bit */
lsh_miller_bit = 32-rsh_miller_bit;
sniff_write_unknown_protocol(ds_data);
}
break;
}
/* Decode Data until end of frame detected */
old_u32_data = f_data;
old_data_counter = 0;
nb_data = 0;
while (1) {
if ( (K4_BUTTON) || (USER_BUTTON) ) {
break;
}
/* New Word */
f_data = u32_data<<lsh_bit;
/* Next Data */
TST_OFF;
u32_data = WaitGetDMABuffer();
TST_ON;
f_data |= u32_data>>rsh_bit;
/* In New Data 32bits */
if (u32_data != old_u32_data) {
old_u32_data = u32_data;
old_data_counter = 0;
} else {
old_u32_data = u32_data;
/* No new data */
if ( (u32_data==0xFFFFFFFF) || (u32_data==0x00000000) ) {
old_data_counter++;
if (old_data_counter>1) {
/* No new data => End Of Frame detected => Wait new data & synchro */
break;
}
} else {
old_data_counter = 0;
}
}
f_data = (f_data>>rsh_miller_bit)|(0xFFFFFFFF<<lsh_miller_bit);
// DownSampling by 4 (input 32bits output 8bits filtered)
// In Freq of 3.39MHz => 105.9375KHz on 8bits (each bit is 848KHz so 2bits=423.75KHz)
ds_data = ((downsample_4x[(f_data>>24)])<<6) |
((downsample_4x[((f_data&0x00FF0000)>>16)])<<4) |
((downsample_4x[((f_data&0x0000FF00)>>8)])<<2) |
(downsample_4x[(f_data&0x000000FF)]);
switch(protocol_found) {
case MILLER_MODIFIED_106KHZ:
if (tmp_u8_data_nb_bit < 8) {
tmp_u8_data |= (miller_modified_106kb[ds_data])<<tmp_u8_data_nb_bit;
tmp_u8_data_nb_bit++;
} else {
nb_data++;
tmp_u8_data_nb_bit=0;
/* Convert Hex to ASCII + Space */
sniff_write_8b_ASCII_HEX(tmp_u8_data, TRUE);
tmp_u8_data=0; /* Parity bit discarded */
}
break;
case MANCHESTER_106KHZ:
if (tmp_u8_data_nb_bit < 8) {
tmp_u8_data |= (manchester_106kb[ds_data])<<tmp_u8_data_nb_bit;
tmp_u8_data_nb_bit++;
} else {
nb_data++;
tmp_u8_data_nb_bit=0;
/* Convert Hex to ASCII + Space */
sniff_write_8b_ASCII_HEX(tmp_u8_data, TRUE);
tmp_u8_data=0; /* Parity bit discarded */
}
break;
default:
/* Unknown protocol */
/* Convert Hex to ASCII */
sniff_write_8b_ASCII_HEX(ds_data, FALSE);
break;
}
/* For safety to avoid potential buffer overflow ... */
if (g_sbuf_idx >= NB_SBUFFER) {
g_sbuf_idx = NB_SBUFFER;
}
}
/* End of Frame detected check if incomplete byte (at least 4bit) is present to write it as output */
if (tmp_u8_data_nb_bit>3) {
/* Convert Hex to ASCII + Space */
sniff_write_8b_ASCII_HEX(tmp_u8_data, FALSE);
}
#if 0
/* Send data if data are available (at least 4bytes) */
if ( g_sbuf_idx >= 4 ) {
chSysUnlock();
tprint_str( "%s\r\n", &g_sbuf[0]);
/* Wait chprintf() end */
chThdSleepMilliseconds(5);
chSysLock();
/* Swap Current Buffer*/
/*
// Clear Index
g_sbuf_idx = 0;
*/
}
#endif
/* For safety to avoid buffer overflow ... */
if (g_sbuf_idx >= NB_SBUFFER) {
g_sbuf_idx = NB_SBUFFER;
}
TST_OFF;
}
} // Main While Loop
}
/* Special version with:
- Parity information for each byte (if total nb bits per frame > 8)
- End Of Frame TimeStamp
*/
void hydranfc_sniff_14443A_dbg(t_hydra_console *con)
{
(void)con;
uint8_t ds_data, tmp_u8_data, tmp_u8_data_nb_bit;
uint32_t f_data, lsh_bit, rsh_bit;
uint32_t rsh_miller_bit, lsh_miller_bit;
uint32_t protocol_found, old_protocol_found; /* 0=Unknown, 1=106kb Miller Modified, 2=106kb Manchester */
uint32_t old_data_counter;
uint32_t nb_data;
uint32_t timestamp_nb_cycles;
tprintf("cmd_nfc_sniff_14443A start TRF7970A configuration as sniffer mode\r\n");
tprintf("Abort/Exit by pressing K4 button\r\n");
init_sniff_nfc(ISO14443A);
tprintf("Starting Sniffer ISO14443-A 106kbps ...\r\n");
/* Wait a bit in order to display all text */
chThdSleepMilliseconds(50);
old_protocol_found = 0;
protocol_found = 0;
g_sbuf_idx = 0;
/* Lock Kernel for sniffer */
chSysLock();
/* Main Loop */
while (TRUE) {
lsh_bit = 0;
rsh_bit = 0;
irq_no = 0;
while (TRUE) {
D4_OFF;
old_data_bit = 0;
f_data = 0;
u32_data = WaitGetDMABuffer();
old_data_bit = (uint32_t)(u32_data&1);
old_u32_data = u32_data;
/* Wait until data change or K4 is pressed to stop/exit */
if (sniff_wait_data_change_or_exit() == TRUE) {
return;
}
/* Log All Data */
TST_ON;
D4_ON;
tmp_u8_data = 0;
tmp_u8_data_nb_bit = 0;
/* Search first edge bit position to synchronize stream */
/* Search an edge on each bit from MSB to LSB */
/* Old bit = 1 so new bit will be 0 => 11111111 10000000 => 00000000 01111111 just need to reverse it to count leading zero */
/* Old bit = 0 so new bit will be 1 => 00000000 01111111 no need to reverse to count leading zero */
lsh_bit = old_data_bit ? (~u32_data) : u32_data;
lsh_bit = CountLeadingZero(lsh_bit);
rsh_bit = 32-lsh_bit;
rsh_miller_bit = 0;
lsh_miller_bit = 32-rsh_miller_bit;
/* Shift data */
f_data = u32_data<<lsh_bit;
/* Next Data */
TST_OFF;
u32_data = WaitGetDMABuffer();
TST_ON;
f_data |= u32_data>>rsh_bit;
/* Todo: Better algorithm to recognize frequency using table and counting number of edge ...
and finaly use majority voting for frequency */
// DownSampling by 4 (input 32bits output 8bits filtered)
// In Freq of 3.39MHz => 105.9375KHz on 8bits (each bit is 848KHz so 2bits=423.75KHz)
ds_data = ((downsample_4x[(f_data>>24)])<<6) |
((downsample_4x[((f_data&0x00FF0000)>>16)])<<4) |
((downsample_4x[((f_data&0x0000FF00)>>8)])<<2) |
(downsample_4x[(f_data&0x000000FF)]);
/* Todo: Find frequency by counting number of consecutive "1" & "0" or the reverse.
* Example0: 1x"1" then 1x"0" => 3.39MHz/2 = Freq 1695KHz
* Example1: 2x"1" then 2x"0" => 3.39MHz/4 = Freq 847.5KHz
* Example2: 4x"1" then 4x"0" => 3.39MHz/8 = Freq 423.75KHz
* Example3: 8x"1" then 8x"0" (8+8) => 3.39MHz/16 = Freq 211.875KHz
* Example4: 16x"1" then 16x"0" (16+16) => 3.39MHz/32 = Freq 105.9375KHz
* Example Miller Modified '0' @~106Khz: 00000000 00111111 11111111 11111111 => 10x"0" then 22x"1" (10+22=32) => 3.39MHz/32 = Freq 105.9375KHz
**/
protocol_found = detected_protocol[ds_data];
switch(protocol_found) {
case MILLER_MODIFIED_106KHZ:
/* Miller Modified@~106Khz Start bit */
old_protocol_found = MILLER_MODIFIED_106KHZ;
sniff_write_pcd();
break;
case MANCHESTER_106KHZ:
/* Manchester@~106Khz Start bit */
old_protocol_found = MANCHESTER_106KHZ;
sniff_write_picc();
break;
default:
/* If previous protocol was Manchester now it should be Miller Modified
(it is a supposition and because Miller modified start after manchester)
*/
if ( MANCHESTER_106KHZ == old_protocol_found ) {
old_protocol_found = MILLER_MODIFIED_106KHZ;
protocol_found = MILLER_MODIFIED_106KHZ;
/* RE Synchronize bit stream to start of bit from (00000000) 11111111 to 00111111 (2 to 3 us at level 0 are not seen) */
/* Nota only first Miller Modified Word does not need this hack because it is well detected it start with (11111111) 00111111 */
// New Word
rsh_miller_bit = 15; /* Between 2 to 3.1us => 7 to 11bits => Average 9bits + 6bits(margin) =< 32-15 = 17 bit */
lsh_miller_bit = 32-rsh_miller_bit;
sniff_write_pcd();
/* Start Bit not included in data buffer */
} else {
old_protocol_found = MILLER_MODIFIED_106KHZ;
protocol_found = MILLER_MODIFIED_106KHZ;
/* RE Synchronize bit stream to start of bit from (00000000) 11111111 to 00111111 (2 to 3 us at level 0 are not seen) */
/* Nota only first Miller Modified Word does not need this hack because it is well detected it start with (11111111) 00111111 */
// New Word
rsh_miller_bit = 15; /* Between 2 to 3.1us => 7 to 11bits => Average 9bits + 6bits(margin) =< 32-15 = 17 bit */
lsh_miller_bit = 32-rsh_miller_bit;
sniff_write_unknown_protocol(ds_data);
}
break;
}
/* Decode Data until end of frame detected */
old_u32_data = f_data;
old_data_counter = 0;
nb_data = 0;
while (1) {
if ( (K4_BUTTON) || (USER_BUTTON) ) {
break;
}