PN532 Updates
[hackover2013-badge-firmware.git] / tools / examples / sensors / pn532 / MifareClassic_MemDump / main.c
1 /**************************************************************************/
2 /*!
3 @file main.c
4 @author K. Townsend (microBuilder.eu)
5
6 @section LICENSE
7
8 Software License Agreement (BSD License)
9
10 Copyright (c) 2010, microBuilder SARL
11 All rights reserved.
12
13 Redistribution and use in source and binary forms, with or without
14 modification, are permitted provided that the following conditions are met:
15 1. Redistributions of source code must retain the above copyright
16 notice, this list of conditions and the following disclaimer.
17 2. Redistributions in binary form must reproduce the above copyright
18 notice, this list of conditions and the following disclaimer in the
19 documentation and/or other materials provided with the distribution.
20 3. Neither the name of the copyright holders nor the
21 names of its contributors may be used to endorse or promote products
22 derived from this software without specific prior written permission.
23
24 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
25 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
28 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 */
35 /**************************************************************************/
36 #include <stdio.h>
37
38 #include "projectconfig.h"
39 #include "sysinit.h"
40
41 #include "core/i2c/i2c.h"
42 #include "drivers/sensors/pn532/pn532.h"
43 #include "drivers/sensors/pn532/pn532_bus.h"
44 #include "drivers/sensors/pn532/helpers/pn532_mifare_classic.h"
45 #include "drivers/sensors/pn532/helpers/pn532_mifare_ultralight.h"
46
47 /**************************************************************************/
48 /*!
49 Main program entry point. After reset, normal code execution will
50 begin here.
51
52 Note: CFG_INTERFACE is normally enabled by default. If you wish to
53 enable the blinking LED code in main, you will need to open
54 projectconfig.h, comment out "#define CFG_INTERFACE" and
55 rebuild the project.
56 */
57 /**************************************************************************/
58 int main (void)
59 {
60 #ifdef CFG_INTERFACE
61 //#error "CFG_INTERFACE must be disabled in projectconfig.h for this demo"
62 #endif
63 #if !defined CFG_PRINTF_USBCDC
64 #error "CFG_PRINTF_USBCDC must be enabled in projectconfig.h for this demo"
65 #endif
66
67 // Configure cpu and mandatory peripherals
68 systemInit();
69
70 // Wait a bit for someone to open the USB connection for printf
71 systickDelay(2000);
72
73 // Initialise the PN532
74 pn532Init();
75
76 // To dump an NDEF formatted Mifare Classic Card (formatted using NXP TagWriter on Android
77 // for example), you must use the following authentication keys:
78 //
79 // Sector 0: 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5
80 // Sectors 1..15: 0xd3, 0xf7, 0xd3, 0xf7, 0xd3, 0xf7
81 //
82 // For more information on NDEF see the following document:
83 //
84 // AN1305 - MIFARE Classic as NFC Type MIFARE Classic Tag
85 // http://www.nxp.com/documents/application_note/AN1305.pdf
86
87 // Set this to one for NDEF cards, or 0 for blank factory default cards
88 #define CARDFORMAT_NDEF (0)
89
90 pn532_error_t error;
91 byte_t abtUID[8];
92 byte_t abtBlock[32];
93 #if CARDFORMAT_NDEF == 1
94 byte_t abtAuthKey1[6] = { 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5 }; // Sector 0 of NXP formatter NDEF cards
95 byte_t abtAuthKey2[6] = { 0xd3, 0xf7, 0xd3, 0xf7, 0xd3, 0xf7 }; // All other sectors use standard key (AN1305 p.20, Table 6)
96 #else
97 byte_t abtAuthKey1[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
98 byte_t abtAuthKey2[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
99 #endif
100 size_t szUIDLen;
101
102 // Use the MIFARE Classic Helper to read/write to the tag's EEPROM storage
103 while(1)
104 {
105 printf("Please insert a Mifare Classic 1K or 4K card%s%s", CFG_PRINTF_NEWLINE, CFG_PRINTF_NEWLINE);
106
107 // Wait for any ISO14443A card
108 error = pn532_mifareclassic_WaitForPassiveTarget(abtUID, &szUIDLen);
109 if (!error)
110 {
111 // Mifare classic card found ... cycle through each sector
112 uint8_t block;
113 bool authenticated = false;
114 for (block = 0; block < 64; block++)
115 {
116 // Check if this is a new block so that we can reauthenticate
117 if (is_first_block(block)) authenticated = false;
118 if (!authenticated)
119 {
120 // Start of a new sector ... try to to authenticate
121 printf("-------------------------Sector %02d--------------------------%s", block / 4, CFG_PRINTF_NEWLINE);
122 error = pn532_mifareclassic_AuthenticateBlock (abtUID, szUIDLen, block, PN532_MIFARE_CMD_AUTH_A, block / 4 ? abtAuthKey2 : abtAuthKey1);
123 if (error)
124 {
125 switch(error)
126 {
127 default:
128 printf("Authentication error (0x%02x)%s", error, CFG_PRINTF_NEWLINE);
129 break;
130 }
131 }
132 else
133 {
134 authenticated = true;
135 }
136 }
137 // If we're still not authenticated just skip the block
138 if (!authenticated)
139 {
140 printf("Block %02d: ", block);
141 printf("Unable to authenticate%s", CFG_PRINTF_NEWLINE);
142 }
143 else
144 {
145 // Authenticated ... we should be able to read the block now
146 error = pn532_mifareclassic_ReadDataBlock (block, abtBlock);
147 if (error)
148 {
149 switch(error)
150 {
151 default:
152 printf("Block %02d: ", block);
153 printf("Unable to read this block%s", CFG_PRINTF_NEWLINE);
154 break;
155 }
156 }
157 else
158 {
159 // Read successful
160 printf("Block %02d: ", block);
161 pn532PrintHexChar(abtBlock, 16);
162 }
163 }
164 }
165 }
166 else
167 {
168 switch (error)
169 {
170 case PN532_ERROR_WRONGCARDTYPE:
171 printf("Not a Mifare Classic 1K or 4K card%s", CFG_PRINTF_NEWLINE);
172 break;
173 default:
174 printf("Error establishing passive connection (0x%02x)%s", error, CFG_PRINTF_NEWLINE);
175 break;
176 }
177 }
178
179 // Wait a bit before trying again
180 systickDelay(2000);
181 }
182 }
This page took 0.086843 seconds and 5 git commands to generate.