summaryrefslogtreecommitdiff
path: root/openpicc/application/iso14443a_pretender.c
blob: 6f62330b80e03f52d822dd9c2eaaeb47098dcdb5 (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
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
/***************************************************************
 *
 * OpenPICC - ISO 14443 Layer 3 response pretender, 
 * proof of concept
 *
 * 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 <stdlib.h>

#include "openpicc.h"
#include "ssc_buffer.h"
#include "iso14443.h"
#include "iso14443_layer2a.h"
#include "iso14443a_pretender.h"
#include "iso14443a_manchester.h"
#include "usb_print.h"
#include "cmd.h"
#include "led.h"

static const iso14443_frame ATQA_FRAME = {
	TYPE_A,
	{{STANDARD_FRAME, PARITY, ISO14443A_LAST_BIT_NONE}},
	2,
	0, 0,
	{4, 0},
	{}
};

static ssc_dma_tx_buffer_t tx_buffer;

static void fast_receive_callback(ssc_dma_rx_buffer_t *buffer, u_int8_t in_irq)
{
	(void)buffer; (void)in_irq;
	unsigned int buffy=0;
	u_int32_t cv = *AT91C_TC2_CV;
	
	const iso14443_frame *tx_frame = NULL;
	int fdt = 0;
	
	switch(buffer->len_transfers) {
	case 3: case 4: /* REQA (7 bits) */
		tx_frame = &ATQA_FRAME;
		fdt = 1172;
		break;
	case 6: case 7: /* ANTICOL (2 bytes) */
	case 22: /* SELECT (9 bytes) */
		
		break;
	case 577:
		usb_print_string_f("f", 0); 
		buffy=(unsigned int)buffer;
		break;
	}
	
	/* Add some extra room to the fdt for testing */
	fdt += 3*128;
	
	int ret = 0;
	if(tx_frame != NULL) {
		tx_buffer.source = (void*)tx_frame;
		tx_buffer.len = sizeof(tx_buffer.data);
		ret = manchester_encode(tx_buffer.data,
				tx_buffer.len,
				tx_frame);
		if(ret >= 0) {
			tx_buffer.state = FULL;
			tx_buffer.len = ret;
			if(	iso14443_transmit(&tx_buffer, fdt, 1, 0) < 0) {
				tx_buffer.state = FREE;
				usb_print_string_f("Tx failed ", 0);
			}
		} else tx_buffer.state = FREE;

	}
	
	u_int32_t cv2 = *AT91C_TC2_CV;
	int old=usb_print_set_default_flush(0);
	DumpUIntToUSB(cv);
	DumpStringToUSB(":");
	DumpUIntToUSB(cv2);
	if(ret<0) {
		DumpStringToUSB("!");
		DumpUIntToUSB(-ret);
	}
	if(buffy!=0) {
		DumpStringToUSB("§");
		DumpUIntToUSB(buffy);
	}
	usb_print_set_default_flush(old);
	usb_print_string_f("%", 0);
}

void iso14443a_pretender (void *pvParameters)
{
	(void)pvParameters;
	int res;
	
	/* Delay until USB print etc. are ready */
	int i;
	for(i=0; i<=1000; i++) {
		vLedSetBrightness(LED_RED, i);
		vLedSetBrightness(LED_GREEN, i);
		vTaskDelay(1*portTICK_RATE_MS);
	}
	
	for(i=0; i<=1000; i+=4) {
		vLedSetBrightness(LED_GREEN, 1000-i);
		vTaskDelay(1*portTICK_RATE_MS);
	}
	
	do {
		res = iso14443_layer2a_init(1);
		if(res < 0) {
			usb_print_string("Pretender: Initialization failed\n\r");
			for(i=0; i<=10000; i++) {
				vLedSetBrightness(LED_RED, abs(1000-(i%2000)));
				vTaskDelay(1*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");
	
	for(i=0; i<=1000; i+=4) {
		vLedSetBrightness(LED_RED, 1000-i);
		vTaskDelay(1*portTICK_RATE_MS);
	}
	
	vTaskDelay(250*portTICK_RATE_MS);
	vLedSetGreen(0);
	vLedSetRed(0);
	
	int current_detected = 0, last_detected = 0;
	portTickType last_switched = 0;
#define DETECTED_14443A_3 1
#define DETECTED_14443A_4 2
#define DETECTED_MIFARE 4
	
	while(true) {
		ssc_dma_rx_buffer_t *buffer = 0;
		res = iso14443_receive(fast_receive_callback, &buffer, 1000 * portTICK_RATE_MS);
		if(res >= 0) {
			DumpUIntToUSB(buffer->len_transfers);
			DumpStringToUSB("\n\r");
			
			switch(buffer->len_transfers) {
			case 3: case 4: /* REQA (7 bits) */
				current_detected |= DETECTED_14443A_3;
				break;
			case 6: case 7: /* ANTICOL (2 bytes) */
			case 22: /* SELECT (9 bytes) */
				current_detected |= DETECTED_14443A_3;
				break;
			case 10: case 11: /* AUTH1A (or any other four byte frame) */
			case 19: case 20: /* AUTH2 (8 bytes) */
				current_detected |= DETECTED_MIFARE;
				break;
			}
			
			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 --");
			}
		}
		
		if( last_switched < xTaskGetTickCount()-(1000*portTICK_RATE_MS) ) {
			last_detected = current_detected;
			current_detected = 0;
			last_switched = xTaskGetTickCount();
		}
		
		if(last_detected & (DETECTED_14443A_3 | DETECTED_14443A_4 | DETECTED_MIFARE)) {
			if(last_detected & DETECTED_MIFARE) {
				vLedSetGreen(0);
				vLedSetRed(1);
			} else if(last_detected & DETECTED_14443A_4) {
				vLedSetGreen(1);
				vLedSetRed(0);
			} else {
				vLedSetGreen(1);
				vLedSetRed(1);
			}
		} else {
			vLedSetGreen(0);
			vLedSetRed(0);
		}

	}
}
personal git repositories of Harald Welte. Your mileage may vary