Project

General

Profile

Download (3.1 KB) Statistics
| Branch: | Tag: | Revision:
1 6be69f0d henryk
/***************************************************************
2
 *
3
 * OpenPICC - ISO 14443 Layer 2 Type A Sniffer
4
 * Also serves as PoC code for iso14443_layer2a usage
5
 *
6
 * Copyright 2008 Henryk Plötz <henryk@ploetzli.ch>
7
 *
8
 ***************************************************************
9
10
    This program is free software; you can redistribute it and/or modify
11
    it under the terms of the GNU General Public License as published by
12
    the Free Software Foundation; version 2.
13
14
    This program is distributed in the hope that it will be useful,
15
    but WITHOUT ANY WARRANTY; without even the implied warranty of
16
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
    GNU General Public License for more details.
18
19
    You should have received a copy of the GNU General Public License along
20
    with this program; if not, write to the Free Software Foundation, Inc.,
21
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22
23
*/
24
25
#include <FreeRTOS.h>
26
#include <board.h>
27
#include <task.h>
28
#include <errno.h>
29 e2e37bea henryk
#include <stdlib.h>
30 6be69f0d henryk
31
#include "openpicc.h"
32 ad5b96d5 henryk
#include "ssc_buffer.h"
33
#include "iso14443.h"
34 6be69f0d henryk
#include "iso14443_sniffer.h"
35
#include "iso14443_layer2a.h"
36
#include "iso14443a_miller.h"
37
#include "usb_print.h"
38
#include "cmd.h"
39 ad5b96d5 henryk
#include "led.h"
40 6be69f0d henryk
41 e2e37bea henryk
static iso14443_frame *rx_frame;
42 6be69f0d henryk
43
void iso14443_sniffer (void *pvParameters)
44
{
45
	(void)pvParameters;
46
	int res;
47
	
48
	/* Delay until USB print etc. are ready */
49
	vTaskDelay(1000 * portTICK_RATE_MS);
50
	
51
	do {
52
		res = iso14443_layer2a_init(0);
53
		if(res < 0) {
54
			usb_print_string("Sniffer: Initialization failed\n\r");
55
			vTaskDelay(10000 * portTICK_RATE_MS);
56
		}
57
	} while(res < 0);
58
	
59 e2e37bea henryk
	
60
	//while(1) { static int i=0; vTaskDelay(10*portTICK_RATE_MS); vLedSetBrightness(LED_RED, abs(1000-i)); i=(i+8)%2000; }
61
		
62 6be69f0d henryk
	usb_print_string("Waiting for carrier. ");
63
	while(iso14443_wait_for_carrier(1000 * portTICK_RATE_MS) != 0) {
64
	}
65
	usb_print_string("Carrier detected.\n\r");
66
	
67
	while(true) {
68 e2e37bea henryk
		res = iso14443_receive(NULL, &rx_frame, 20000 * portTICK_RATE_MS);
69 6be69f0d henryk
		if(res >= 0) {
70 b7a11d3b henryk
			//DumpStringToUSB("\n\r");
71 6be69f0d henryk
			DumpTimeToUSB(xTaskGetTickCount());
72
			usb_print_string(": Frame received, consists of ");
73
			
74 e2e37bea henryk
			DumpUIntToUSB(rx_frame->numbytes);
75 6be69f0d henryk
			usb_print_string(" bytes and ");
76 e2e37bea henryk
			DumpUIntToUSB(rx_frame->numbits);
77 6be69f0d henryk
			usb_print_string(" bits:  ");
78 e2e37bea henryk
			DumpBufferToUSB((char*)rx_frame->data, rx_frame->numbytes + (rx_frame->numbits+7)/8 );
79 6be69f0d henryk
			usb_print_string("\n\r");
80
			
81
			portENTER_CRITICAL();
82 e2e37bea henryk
			rx_frame->state = FRAME_FREE;
83 6be69f0d henryk
			portEXIT_CRITICAL();
84
		} else {
85
			if(res != -ETIMEDOUT) {
86
				usb_print_string("Receive error: ");
87
				switch(res) {
88
					case -ENETDOWN:   usb_print_string("PLL is not locked or PLL lock lost\n\r"); break;
89
					case -EBUSY:      usb_print_string("A Tx is currently running or pending, can't receive\n\r"); break;
90
					case -EALREADY:   usb_print_string("There's already an iso14443_receive() invocation running\n\r"); break;
91
				}
92
				vTaskDelay(1000 * portTICK_RATE_MS); // FIXME Proper error handling, e.g. wait for Tx end in case of EBUSY
93
			} else if(0) {
94
				DumpStringToUSB("\n\r");
95
				DumpTimeToUSB(xTaskGetTickCount());
96
				usb_print_string(": -- Mark --");
97
			}
98
		}
99
	}
100
}
Add picture from clipboard (Maximum size: 48.8 MB)