summaryrefslogtreecommitdiff
path: root/openpicc/application/iso14443_sniffer.c
blob: d5cf9c297f5d29acba77b0222790009d5baacd39 (plain)
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
/***************************************************************
 *
 * OpenPICC - ISO 14443 Layer 2 Type A Sniffer
 * Also serves as PoC code for iso14443_layer2a usage
 *
 * Copyright 2008 Henryk Plötz <henryk@ploetzli.ch>
 *
 ***************************************************************

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; version 2.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

*/

#include <FreeRTOS.h>
#include <board.h>
#include <task.h>
#include <errno.h>

#include "openpicc.h"
#include "iso14443_sniffer.h"
#include "iso14443_layer2a.h"
#include "iso14443a_miller.h"
#include "usb_print.h"
#include "cmd.h"

static iso14443_frame rx_frame;

void iso14443_sniffer (void *pvParameters)
{
	(void)pvParameters;
	int res;
	
	/* Delay until USB print etc. are ready */
	vTaskDelay(1000 * portTICK_RATE_MS);
	
	do {
		res = iso14443_layer2a_init(0);
		if(res < 0) {
			usb_print_string("Sniffer: Initialization failed\n\r");
			vTaskDelay(10000 * portTICK_RATE_MS);
		}
	} while(res < 0);
	
	usb_print_string("Waiting for carrier. ");
	while(iso14443_wait_for_carrier(1000 * portTICK_RATE_MS) != 0) {
	}
	usb_print_string("Carrier detected.\n\r");
	
	iso14443_l2a_rx_start();
	while(true) {
		ssc_dma_rx_buffer_t *buffer = 0;
		res = iso14443_receive(NULL, &buffer, 20000 * portTICK_RATE_MS);
		if(res >= 0) {
			DumpStringToUSB("\n\r");
			DumpTimeToUSB(xTaskGetTickCount());
			usb_print_string(": Frame received, consists of ");
			DumpUIntToUSB(res);
			usb_print_string(" transfers (");
			DumpUIntToUSB(buffer->reception_mode->transfersize_ssc);
			usb_print_string(" bits from SSC each)\n\r                  ");
			if(buffer->len_transfers < 200)
				DumpBufferToUSB((char*)buffer->data, (buffer->len_transfers * buffer->reception_mode->transfersize_pdc)/8);
			else {
				DumpBufferToUSB((char*)buffer->data, (200 * buffer->reception_mode->transfersize_pdc)/8);
				usb_print_string("...");
			}
			usb_print_string("\n\r                  ");
			
			iso14443a_decode_miller(&rx_frame, buffer);
			
			usb_print_string("Decodes to ");
			DumpUIntToUSB(rx_frame.numbytes);
			usb_print_string(" bytes and ");
			DumpUIntToUSB(rx_frame.numbits);
			usb_print_string(" bits:  ");
			DumpBufferToUSB((char*)rx_frame.data, rx_frame.numbytes + (rx_frame.numbits+7)/8 );
			usb_print_string("\n\r");
			
			portENTER_CRITICAL();
			buffer->state = FREE;
			portEXIT_CRITICAL();
		} else {
			if(res != -ETIMEDOUT) {
				usb_print_string("Receive error: ");
				switch(res) {
					case -ENETDOWN:   usb_print_string("PLL is not locked or PLL lock lost\n\r"); break;
					case -EBUSY:      usb_print_string("A Tx is currently running or pending, can't receive\n\r"); break;
					case -EALREADY:   usb_print_string("There's already an iso14443_receive() invocation running\n\r"); break;
				}
				vTaskDelay(1000 * portTICK_RATE_MS); // FIXME Proper error handling, e.g. wait for Tx end in case of EBUSY
			} else if(0) {
				DumpStringToUSB("\n\r");
				DumpTimeToUSB(xTaskGetTickCount());
				usb_print_string(": -- Mark --");
			}
		}
	}
}
personal git repositories of Harald Welte. Your mileage may vary