PN532 customized

Fork of PN532 by Components

Committer:
stanvn
Date:
Wed Feb 10 08:41:44 2016 +0000
Revision:
8:30bba738e292
Parent:
7:a26fa6ef10eb
Child:
9:70abe4844114
Don't know why i have to commit again

Who changed what in which revision?

UserRevisionLine numberNew contents of line
yihui 1:b8cab5222fd0 1 /**************************************************************************/
yihui 1:b8cab5222fd0 2 /*!
yihui 1:b8cab5222fd0 3 @file PN532.cpp
yihui 3:4189a10038e6 4 @author Adafruit Industries & Seeed Studio
yihui 3:4189a10038e6 5 @license BSD
yihui 1:b8cab5222fd0 6 */
yihui 1:b8cab5222fd0 7 /**************************************************************************/
yihui 0:9c6b9280c0e1 8
yihui 3:4189a10038e6 9 #include "PN532.h"
yihui 3:4189a10038e6 10 #include "PN532_debug.h"
yihui 0:9c6b9280c0e1 11 #include <string.h>
yihui 0:9c6b9280c0e1 12
yihui 4:0774b8298eb8 13 #ifndef ARDUINO
yihui 4:0774b8298eb8 14 #include <stdio.h>
stanvn 7:a26fa6ef10eb 15 #include <stdlib.h>
stanvn 7:a26fa6ef10eb 16
yihui 4:0774b8298eb8 17 #endif
yihui 4:0774b8298eb8 18
yihui 0:9c6b9280c0e1 19 #define HAL(func) (_interface->func)
yihui 0:9c6b9280c0e1 20
yihui 0:9c6b9280c0e1 21 PN532::PN532(PN532Interface &interface)
yihui 0:9c6b9280c0e1 22 {
yihui 1:b8cab5222fd0 23 _interface = &interface;
yihui 0:9c6b9280c0e1 24 }
yihui 0:9c6b9280c0e1 25
yihui 0:9c6b9280c0e1 26 /**************************************************************************/
yihui 1:b8cab5222fd0 27 /*!
yihui 0:9c6b9280c0e1 28 @brief Setups the HW
yihui 0:9c6b9280c0e1 29 */
yihui 0:9c6b9280c0e1 30 /**************************************************************************/
yihui 0:9c6b9280c0e1 31 void PN532::begin()
yihui 0:9c6b9280c0e1 32 {
yihui 0:9c6b9280c0e1 33 HAL(begin)();
yihui 0:9c6b9280c0e1 34 HAL(wakeup)();
yihui 0:9c6b9280c0e1 35 }
yihui 1:b8cab5222fd0 36
yihui 0:9c6b9280c0e1 37 /**************************************************************************/
yihui 1:b8cab5222fd0 38 /*!
yihui 0:9c6b9280c0e1 39 @brief Prints a hexadecimal value in plain characters
yihui 0:9c6b9280c0e1 40
yihui 0:9c6b9280c0e1 41 @param data Pointer to the uint8_t data
yihui 0:9c6b9280c0e1 42 @param numBytes Data length in bytes
yihui 0:9c6b9280c0e1 43 */
yihui 0:9c6b9280c0e1 44 /**************************************************************************/
yihui 1:b8cab5222fd0 45 void PN532::PrintHex(const uint8_t *data, const uint32_t numBytes)
yihui 0:9c6b9280c0e1 46 {
yihui 3:4189a10038e6 47 #ifdef ARDUINO
yihui 1:b8cab5222fd0 48 for (uint8_t i = 0; i < numBytes; i++) {
yihui 3:4189a10038e6 49 if (data[i] < 0x10) {
yihui 3:4189a10038e6 50 Serial.print(" 0");
yihui 3:4189a10038e6 51 } else {
yihui 3:4189a10038e6 52 Serial.print(' ');
yihui 3:4189a10038e6 53 }
yihui 3:4189a10038e6 54 Serial.print(data[i], HEX);
yihui 1:b8cab5222fd0 55 }
yihui 3:4189a10038e6 56 Serial.println("");
yihui 3:4189a10038e6 57 #else
yihui 3:4189a10038e6 58 for (uint8_t i = 0; i < numBytes; i++) {
yihui 3:4189a10038e6 59 printf(" %2X", data[i]);
yihui 3:4189a10038e6 60 }
yihui 3:4189a10038e6 61 printf("\n");
yihui 3:4189a10038e6 62 #endif
yihui 0:9c6b9280c0e1 63 }
yihui 0:9c6b9280c0e1 64
yihui 0:9c6b9280c0e1 65 /**************************************************************************/
yihui 1:b8cab5222fd0 66 /*!
yihui 0:9c6b9280c0e1 67 @brief Prints a hexadecimal value in plain characters, along with
yihui 0:9c6b9280c0e1 68 the char equivalents in the following format
yihui 0:9c6b9280c0e1 69
yihui 0:9c6b9280c0e1 70 00 00 00 00 00 00 ......
yihui 0:9c6b9280c0e1 71
yihui 1:b8cab5222fd0 72 @param data Pointer to the data
yihui 0:9c6b9280c0e1 73 @param numBytes Data length in bytes
yihui 0:9c6b9280c0e1 74 */
yihui 0:9c6b9280c0e1 75 /**************************************************************************/
yihui 1:b8cab5222fd0 76 void PN532::PrintHexChar(const uint8_t *data, const uint32_t numBytes)
yihui 0:9c6b9280c0e1 77 {
yihui 3:4189a10038e6 78 #ifdef ARDUINO
yihui 1:b8cab5222fd0 79 for (uint8_t i = 0; i < numBytes; i++) {
yihui 3:4189a10038e6 80 if (data[i] < 0x10) {
yihui 3:4189a10038e6 81 Serial.print(" 0");
yihui 3:4189a10038e6 82 } else {
yihui 3:4189a10038e6 83 Serial.print(' ');
yihui 3:4189a10038e6 84 }
yihui 3:4189a10038e6 85 Serial.print(data[i], HEX);
yihui 3:4189a10038e6 86 }
yihui 3:4189a10038e6 87 Serial.print(" ");
yihui 3:4189a10038e6 88 for (uint8_t i = 0; i < numBytes; i++) {
yihui 3:4189a10038e6 89 char c = data[i];
yihui 3:4189a10038e6 90 if (c <= 0x1f || c > 0x7f) {
yihui 3:4189a10038e6 91 Serial.print('.');
yihui 3:4189a10038e6 92 } else {
yihui 3:4189a10038e6 93 Serial.print(c);
yihui 3:4189a10038e6 94 }
yihui 1:b8cab5222fd0 95 }
yihui 3:4189a10038e6 96 Serial.println("");
yihui 3:4189a10038e6 97 #else
yihui 3:4189a10038e6 98 for (uint8_t i = 0; i < numBytes; i++) {
yihui 3:4189a10038e6 99 printf(" %2X", data[i]);
yihui 3:4189a10038e6 100 }
yihui 3:4189a10038e6 101 printf(" ");
yihui 3:4189a10038e6 102 for (uint8_t i = 0; i < numBytes; i++) {
yihui 3:4189a10038e6 103 char c = data[i];
yihui 3:4189a10038e6 104 if (c <= 0x1f || c > 0x7f) {
yihui 3:4189a10038e6 105 printf(".");
yihui 3:4189a10038e6 106 } else {
yihui 3:4189a10038e6 107 printf("%c", c);
yihui 3:4189a10038e6 108 }
yihui 3:4189a10038e6 109 printf("\n");
yihui 3:4189a10038e6 110 }
yihui 3:4189a10038e6 111 #endif
yihui 0:9c6b9280c0e1 112 }
yihui 1:b8cab5222fd0 113
yihui 0:9c6b9280c0e1 114 /**************************************************************************/
yihui 1:b8cab5222fd0 115 /*!
yihui 0:9c6b9280c0e1 116 @brief Checks the firmware version of the PN5xx chip
yihui 0:9c6b9280c0e1 117
yihui 0:9c6b9280c0e1 118 @returns The chip's firmware version and ID
yihui 0:9c6b9280c0e1 119 */
yihui 0:9c6b9280c0e1 120 /**************************************************************************/
yihui 1:b8cab5222fd0 121 uint32_t PN532::getFirmwareVersion(void)
yihui 1:b8cab5222fd0 122 {
yihui 1:b8cab5222fd0 123 uint32_t response;
yihui 0:9c6b9280c0e1 124
yihui 1:b8cab5222fd0 125 pn532_packetbuffer[0] = PN532_COMMAND_GETFIRMWAREVERSION;
yihui 1:b8cab5222fd0 126
yihui 1:b8cab5222fd0 127 if (HAL(writeCommand)(pn532_packetbuffer, 1)) {
yihui 1:b8cab5222fd0 128 return 0;
yihui 1:b8cab5222fd0 129 }
yihui 0:9c6b9280c0e1 130
yihui 1:b8cab5222fd0 131 // read data packet
yihui 1:b8cab5222fd0 132 int16_t status = HAL(readResponse)(pn532_packetbuffer, sizeof(pn532_packetbuffer));
yihui 1:b8cab5222fd0 133 if (0 > status) {
yihui 1:b8cab5222fd0 134 return 0;
yihui 1:b8cab5222fd0 135 }
yihui 0:9c6b9280c0e1 136
yihui 1:b8cab5222fd0 137 response = pn532_packetbuffer[0];
yihui 1:b8cab5222fd0 138 response <<= 8;
yihui 1:b8cab5222fd0 139 response |= pn532_packetbuffer[1];
yihui 1:b8cab5222fd0 140 response <<= 8;
yihui 1:b8cab5222fd0 141 response |= pn532_packetbuffer[2];
yihui 1:b8cab5222fd0 142 response <<= 8;
yihui 1:b8cab5222fd0 143 response |= pn532_packetbuffer[3];
yihui 1:b8cab5222fd0 144
yihui 1:b8cab5222fd0 145 return response;
yihui 0:9c6b9280c0e1 146 }
yihui 0:9c6b9280c0e1 147
yihui 0:9c6b9280c0e1 148
yihui 0:9c6b9280c0e1 149 /**************************************************************************/
yihui 1:b8cab5222fd0 150 /*!
yihui 0:9c6b9280c0e1 151 Writes an 8-bit value that sets the state of the PN532's GPIO pins
yihui 1:b8cab5222fd0 152
yihui 0:9c6b9280c0e1 153 @warning This function is provided exclusively for board testing and
yihui 0:9c6b9280c0e1 154 is dangerous since it will throw an error if any pin other
yihui 0:9c6b9280c0e1 155 than the ones marked "Can be used as GPIO" are modified! All
yihui 0:9c6b9280c0e1 156 pins that can not be used as GPIO should ALWAYS be left high
yihui 0:9c6b9280c0e1 157 (value = 1) or the system will become unstable and a HW reset
yihui 0:9c6b9280c0e1 158 will be required to recover the PN532.
yihui 1:b8cab5222fd0 159
yihui 0:9c6b9280c0e1 160 pinState[0] = P30 Can be used as GPIO
yihui 0:9c6b9280c0e1 161 pinState[1] = P31 Can be used as GPIO
yihui 0:9c6b9280c0e1 162 pinState[2] = P32 *** RESERVED (Must be 1!) ***
yihui 0:9c6b9280c0e1 163 pinState[3] = P33 Can be used as GPIO
yihui 0:9c6b9280c0e1 164 pinState[4] = P34 *** RESERVED (Must be 1!) ***
yihui 0:9c6b9280c0e1 165 pinState[5] = P35 Can be used as GPIO
yihui 1:b8cab5222fd0 166
yihui 0:9c6b9280c0e1 167 @returns 1 if everything executed properly, 0 for an error
yihui 0:9c6b9280c0e1 168 */
yihui 0:9c6b9280c0e1 169 /**************************************************************************/
yihui 1:b8cab5222fd0 170 bool PN532::writeGPIO(uint8_t pinstate)
yihui 1:b8cab5222fd0 171 {
yihui 1:b8cab5222fd0 172 // Make sure pinstate does not try to toggle P32 or P34
yihui 1:b8cab5222fd0 173 pinstate |= (1 << PN532_GPIO_P32) | (1 << PN532_GPIO_P34);
yihui 1:b8cab5222fd0 174
yihui 1:b8cab5222fd0 175 // Fill command buffer
yihui 1:b8cab5222fd0 176 pn532_packetbuffer[0] = PN532_COMMAND_WRITEGPIO;
yihui 1:b8cab5222fd0 177 pn532_packetbuffer[1] = PN532_GPIO_VALIDATIONBIT | pinstate; // P3 Pins
yihui 1:b8cab5222fd0 178 pn532_packetbuffer[2] = 0x00; // P7 GPIO Pins (not used ... taken by I2C)
yihui 0:9c6b9280c0e1 179
yihui 1:b8cab5222fd0 180 DMSG("Writing P3 GPIO: ");
yihui 1:b8cab5222fd0 181 DMSG_HEX(pn532_packetbuffer[1]);
yihui 1:b8cab5222fd0 182 DMSG("\n");
yihui 0:9c6b9280c0e1 183
yihui 1:b8cab5222fd0 184 // Send the WRITEGPIO command (0x0E)
yihui 1:b8cab5222fd0 185 if (HAL(writeCommand)(pn532_packetbuffer, 3))
yihui 1:b8cab5222fd0 186 return 0;
yihui 0:9c6b9280c0e1 187
yihui 1:b8cab5222fd0 188 return (0 < HAL(readResponse)(pn532_packetbuffer, sizeof(pn532_packetbuffer)));
yihui 0:9c6b9280c0e1 189 }
yihui 0:9c6b9280c0e1 190
yihui 0:9c6b9280c0e1 191 /**************************************************************************/
yihui 1:b8cab5222fd0 192 /*!
yihui 0:9c6b9280c0e1 193 Reads the state of the PN532's GPIO pins
yihui 1:b8cab5222fd0 194
yihui 0:9c6b9280c0e1 195 @returns An 8-bit value containing the pin state where:
yihui 1:b8cab5222fd0 196
yihui 1:b8cab5222fd0 197 pinState[0] = P30
yihui 1:b8cab5222fd0 198 pinState[1] = P31
yihui 1:b8cab5222fd0 199 pinState[2] = P32
yihui 1:b8cab5222fd0 200 pinState[3] = P33
yihui 1:b8cab5222fd0 201 pinState[4] = P34
yihui 1:b8cab5222fd0 202 pinState[5] = P35
yihui 0:9c6b9280c0e1 203 */
yihui 0:9c6b9280c0e1 204 /**************************************************************************/
yihui 1:b8cab5222fd0 205 uint8_t PN532::readGPIO(void)
yihui 1:b8cab5222fd0 206 {
yihui 1:b8cab5222fd0 207 pn532_packetbuffer[0] = PN532_COMMAND_READGPIO;
yihui 0:9c6b9280c0e1 208
yihui 1:b8cab5222fd0 209 // Send the READGPIO command (0x0C)
yihui 1:b8cab5222fd0 210 if (HAL(writeCommand)(pn532_packetbuffer, 1))
yihui 1:b8cab5222fd0 211 return 0x0;
yihui 1:b8cab5222fd0 212
yihui 1:b8cab5222fd0 213 HAL(readResponse)(pn532_packetbuffer, sizeof(pn532_packetbuffer));
yihui 1:b8cab5222fd0 214
yihui 1:b8cab5222fd0 215 /* READGPIO response without prefix and suffix should be in the following format:
yihui 0:9c6b9280c0e1 216
yihui 1:b8cab5222fd0 217 byte Description
yihui 1:b8cab5222fd0 218 ------------- ------------------------------------------
yihui 1:b8cab5222fd0 219 b0 P3 GPIO Pins
yihui 1:b8cab5222fd0 220 b1 P7 GPIO Pins (not used ... taken by I2C)
yihui 1:b8cab5222fd0 221 b2 Interface Mode Pins (not used ... bus select pins)
yihui 1:b8cab5222fd0 222 */
yihui 0:9c6b9280c0e1 223
yihui 0:9c6b9280c0e1 224
yihui 1:b8cab5222fd0 225 DMSG("P3 GPIO: "); DMSG_HEX(pn532_packetbuffer[7]);
yihui 1:b8cab5222fd0 226 DMSG("P7 GPIO: "); DMSG_HEX(pn532_packetbuffer[8]);
yihui 1:b8cab5222fd0 227 DMSG("I0I1 GPIO: "); DMSG_HEX(pn532_packetbuffer[9]);
yihui 1:b8cab5222fd0 228 DMSG("\n");
yihui 1:b8cab5222fd0 229
yihui 1:b8cab5222fd0 230 return pn532_packetbuffer[0];
yihui 0:9c6b9280c0e1 231 }
yihui 0:9c6b9280c0e1 232
yihui 0:9c6b9280c0e1 233 /**************************************************************************/
yihui 1:b8cab5222fd0 234 /*!
yihui 0:9c6b9280c0e1 235 @brief Configures the SAM (Secure Access Module)
yihui 0:9c6b9280c0e1 236 */
yihui 0:9c6b9280c0e1 237 /**************************************************************************/
yihui 1:b8cab5222fd0 238 bool PN532::SAMConfig(void)
yihui 1:b8cab5222fd0 239 {
yihui 1:b8cab5222fd0 240 pn532_packetbuffer[0] = PN532_COMMAND_SAMCONFIGURATION;
yihui 1:b8cab5222fd0 241 pn532_packetbuffer[1] = 0x01; // normal mode;
yihui 1:b8cab5222fd0 242 pn532_packetbuffer[2] = 0x14; // timeout 50ms * 20 = 1 second
yihui 1:b8cab5222fd0 243 pn532_packetbuffer[3] = 0x01; // use IRQ pin!
yihui 0:9c6b9280c0e1 244
yihui 3:4189a10038e6 245 DMSG("SAMConfig\n");
yihui 3:4189a10038e6 246
yihui 1:b8cab5222fd0 247 if (HAL(writeCommand)(pn532_packetbuffer, 4))
yihui 1:b8cab5222fd0 248 return false;
yihui 1:b8cab5222fd0 249
yihui 1:b8cab5222fd0 250 return (0 < HAL(readResponse)(pn532_packetbuffer, sizeof(pn532_packetbuffer)));
yihui 0:9c6b9280c0e1 251 }
yihui 0:9c6b9280c0e1 252
yihui 0:9c6b9280c0e1 253 /**************************************************************************/
yihui 1:b8cab5222fd0 254 /*!
yihui 0:9c6b9280c0e1 255 Sets the MxRtyPassiveActivation uint8_t of the RFConfiguration register
yihui 1:b8cab5222fd0 256
yihui 0:9c6b9280c0e1 257 @param maxRetries 0xFF to wait forever, 0x00..0xFE to timeout
yihui 0:9c6b9280c0e1 258 after mxRetries
yihui 1:b8cab5222fd0 259
yihui 0:9c6b9280c0e1 260 @returns 1 if everything executed properly, 0 for an error
yihui 0:9c6b9280c0e1 261 */
yihui 0:9c6b9280c0e1 262 /**************************************************************************/
yihui 1:b8cab5222fd0 263 bool PN532::setPassiveActivationRetries(uint8_t maxRetries)
yihui 1:b8cab5222fd0 264 {
yihui 1:b8cab5222fd0 265 pn532_packetbuffer[0] = PN532_COMMAND_RFCONFIGURATION;
yihui 1:b8cab5222fd0 266 pn532_packetbuffer[1] = 5; // Config item 5 (MaxRetries)
yihui 1:b8cab5222fd0 267 pn532_packetbuffer[2] = 0xFF; // MxRtyATR (default = 0xFF)
yihui 1:b8cab5222fd0 268 pn532_packetbuffer[3] = 0x01; // MxRtyPSL (default = 0x01)
yihui 1:b8cab5222fd0 269 pn532_packetbuffer[4] = maxRetries;
yihui 1:b8cab5222fd0 270
yihui 1:b8cab5222fd0 271 if (HAL(writeCommand)(pn532_packetbuffer, 5))
yihui 1:b8cab5222fd0 272 return 0x0; // no ACK
yihui 1:b8cab5222fd0 273
yihui 3:4189a10038e6 274 return (0 < HAL(readResponse)(pn532_packetbuffer, sizeof(pn532_packetbuffer)));
yihui 0:9c6b9280c0e1 275 }
yihui 0:9c6b9280c0e1 276
yihui 0:9c6b9280c0e1 277 /***** ISO14443A Commands ******/
yihui 0:9c6b9280c0e1 278
yihui 0:9c6b9280c0e1 279 /**************************************************************************/
yihui 1:b8cab5222fd0 280 /*!
yihui 0:9c6b9280c0e1 281 Waits for an ISO14443A target to enter the field
yihui 1:b8cab5222fd0 282
yihui 0:9c6b9280c0e1 283 @param cardBaudRate Baud rate of the card
yihui 0:9c6b9280c0e1 284 @param uid Pointer to the array that will be populated
yihui 0:9c6b9280c0e1 285 with the card's UID (up to 7 bytes)
yihui 0:9c6b9280c0e1 286 @param uidLength Pointer to the variable that will hold the
yihui 0:9c6b9280c0e1 287 length of the card's UID.
yihui 1:b8cab5222fd0 288
yihui 0:9c6b9280c0e1 289 @returns 1 if everything executed properly, 0 for an error
yihui 0:9c6b9280c0e1 290 */
yihui 0:9c6b9280c0e1 291 /**************************************************************************/
yihui 2:f618fb2169c4 292 bool PN532::readPassiveTargetID(uint8_t cardbaudrate, uint8_t *uid, uint8_t *uidLength, uint16_t timeout)
yihui 1:b8cab5222fd0 293 {
yihui 1:b8cab5222fd0 294 pn532_packetbuffer[0] = PN532_COMMAND_INLISTPASSIVETARGET;
yihui 1:b8cab5222fd0 295 pn532_packetbuffer[1] = 1; // max 1 cards at once (we can set this to 2 later)
yihui 1:b8cab5222fd0 296 pn532_packetbuffer[2] = cardbaudrate;
yihui 1:b8cab5222fd0 297
yihui 1:b8cab5222fd0 298 if (HAL(writeCommand)(pn532_packetbuffer, 3)) {
yihui 1:b8cab5222fd0 299 return 0x0; // command failed
yihui 1:b8cab5222fd0 300 }
yihui 1:b8cab5222fd0 301
yihui 1:b8cab5222fd0 302 // read data packet
yihui 2:f618fb2169c4 303 if (HAL(readResponse)(pn532_packetbuffer, sizeof(pn532_packetbuffer), timeout) < 0) {
yihui 2:f618fb2169c4 304 return 0x0;
yihui 2:f618fb2169c4 305 }
yihui 1:b8cab5222fd0 306
yihui 1:b8cab5222fd0 307 // check some basic stuff
yihui 1:b8cab5222fd0 308 /* ISO14443A card response should be in the following format:
yihui 0:9c6b9280c0e1 309
yihui 1:b8cab5222fd0 310 byte Description
yihui 1:b8cab5222fd0 311 ------------- ------------------------------------------
yihui 1:b8cab5222fd0 312 b0 Tags Found
yihui 1:b8cab5222fd0 313 b1 Tag Number (only one used in this example)
yihui 1:b8cab5222fd0 314 b2..3 SENS_RES
yihui 1:b8cab5222fd0 315 b4 SEL_RES
yihui 1:b8cab5222fd0 316 b5 NFCID Length
yihui 1:b8cab5222fd0 317 b6..NFCIDLen NFCID
yihui 1:b8cab5222fd0 318 */
yihui 1:b8cab5222fd0 319
yihui 1:b8cab5222fd0 320 if (pn532_packetbuffer[0] != 1)
yihui 1:b8cab5222fd0 321 return 0;
yihui 0:9c6b9280c0e1 322
yihui 1:b8cab5222fd0 323 uint16_t sens_res = pn532_packetbuffer[2];
yihui 1:b8cab5222fd0 324 sens_res <<= 8;
yihui 1:b8cab5222fd0 325 sens_res |= pn532_packetbuffer[3];
yihui 1:b8cab5222fd0 326
yihui 1:b8cab5222fd0 327 DMSG("ATQA: 0x"); DMSG_HEX(sens_res);
yihui 1:b8cab5222fd0 328 DMSG("SAK: 0x"); DMSG_HEX(pn532_packetbuffer[4]);
yihui 1:b8cab5222fd0 329 DMSG("\n");
stanvn 7:a26fa6ef10eb 330 printf("ATQA: ");
stanvn 7:a26fa6ef10eb 331 for(int i = 0; i < 8; i++){
stanvn 7:a26fa6ef10eb 332 printf("%02x ",pn532_packetbuffer[i]);
stanvn 7:a26fa6ef10eb 333 }
yihui 1:b8cab5222fd0 334 /* Card appears to be Mifare Classic */
yihui 1:b8cab5222fd0 335 *uidLength = pn532_packetbuffer[5];
yihui 0:9c6b9280c0e1 336
yihui 1:b8cab5222fd0 337 for (uint8_t i = 0; i < pn532_packetbuffer[5]; i++) {
yihui 1:b8cab5222fd0 338 uid[i] = pn532_packetbuffer[6 + i];
yihui 1:b8cab5222fd0 339 }
yihui 1:b8cab5222fd0 340
yihui 1:b8cab5222fd0 341 return 1;
yihui 0:9c6b9280c0e1 342 }
yihui 0:9c6b9280c0e1 343
yihui 0:9c6b9280c0e1 344
stanvn 7:a26fa6ef10eb 345 /***** ISO14443A Commands ******/
stanvn 7:a26fa6ef10eb 346 // UPDATED FROM readPassiveTargetID with SAK bit
stanvn 7:a26fa6ef10eb 347 /**************************************************************************/
stanvn 7:a26fa6ef10eb 348 /*!
stanvn 7:a26fa6ef10eb 349 Waits for an ISO14443A target to enter the field
stanvn 7:a26fa6ef10eb 350
stanvn 7:a26fa6ef10eb 351 @param cardBaudRate Baud rate of the card
stanvn 7:a26fa6ef10eb 352 @param uid Pointer to the array that will be populated
stanvn 7:a26fa6ef10eb 353 with the card's UID (up to 7 bytes)
stanvn 7:a26fa6ef10eb 354 @param uidLength Pointer to the variable that will hold the
stanvn 7:a26fa6ef10eb 355 length of the card's UID.
stanvn 7:a26fa6ef10eb 356 @param sak Pointer to the variable that will hold the
stanvn 7:a26fa6ef10eb 357 SAK bit
stanvn 7:a26fa6ef10eb 358
stanvn 7:a26fa6ef10eb 359 @returns 1 if everything executed properly, 0 for an error
stanvn 7:a26fa6ef10eb 360 */
stanvn 7:a26fa6ef10eb 361 /**************************************************************************/
stanvn 8:30bba738e292 362 // TODO: add a RQTB command to detect a type-b card
stanvn 7:a26fa6ef10eb 363 bool PN532::readPassiveTarget(uint8_t cardbaudrate, uint8_t *uid, uint8_t *uidLength, uint8_t *sak, uint16_t timeout)
stanvn 7:a26fa6ef10eb 364 {
stanvn 7:a26fa6ef10eb 365 pn532_packetbuffer[0] = PN532_COMMAND_INLISTPASSIVETARGET;
stanvn 7:a26fa6ef10eb 366 pn532_packetbuffer[1] = 1; // max 1 cards at once (we can set this to 2 later)
stanvn 7:a26fa6ef10eb 367 pn532_packetbuffer[2] = cardbaudrate;
stanvn 7:a26fa6ef10eb 368
stanvn 7:a26fa6ef10eb 369 if (HAL(writeCommand)(pn532_packetbuffer, 3)) {
stanvn 7:a26fa6ef10eb 370 return 0x0; // command failed
stanvn 7:a26fa6ef10eb 371 }
stanvn 7:a26fa6ef10eb 372
stanvn 7:a26fa6ef10eb 373 // read data packet
stanvn 7:a26fa6ef10eb 374 if (HAL(readResponse)(pn532_packetbuffer, sizeof(pn532_packetbuffer), timeout) < 0) {
stanvn 7:a26fa6ef10eb 375 return 0x0;
stanvn 7:a26fa6ef10eb 376 }
stanvn 7:a26fa6ef10eb 377
stanvn 7:a26fa6ef10eb 378 // check some basic stuff
stanvn 7:a26fa6ef10eb 379 /* ISO14443A card response should be in the following format:
stanvn 7:a26fa6ef10eb 380
stanvn 7:a26fa6ef10eb 381 byte Description
stanvn 7:a26fa6ef10eb 382 ------------- ------------------------------------------
stanvn 7:a26fa6ef10eb 383 b0 Tags Found
stanvn 7:a26fa6ef10eb 384 b1 Tag Number (only one used in this example)
stanvn 7:a26fa6ef10eb 385 b2..3 SENS_RES
stanvn 7:a26fa6ef10eb 386 b4 SEL_RES
stanvn 7:a26fa6ef10eb 387 b5 NFCID Length
stanvn 7:a26fa6ef10eb 388 b6..NFCIDLen NFCID
stanvn 7:a26fa6ef10eb 389 */
stanvn 7:a26fa6ef10eb 390
stanvn 7:a26fa6ef10eb 391 if (pn532_packetbuffer[0] != 1)
stanvn 7:a26fa6ef10eb 392 return 0;
stanvn 7:a26fa6ef10eb 393
stanvn 7:a26fa6ef10eb 394 uint16_t sens_res = pn532_packetbuffer[2];
stanvn 7:a26fa6ef10eb 395 sens_res <<= 8;
stanvn 7:a26fa6ef10eb 396 sens_res |= pn532_packetbuffer[3];
stanvn 7:a26fa6ef10eb 397
stanvn 7:a26fa6ef10eb 398 DMSG("ATQA: 0x"); DMSG_HEX(sens_res);
stanvn 7:a26fa6ef10eb 399 DMSG("SAK: 0x"); DMSG_HEX(pn532_packetbuffer[4]);
stanvn 7:a26fa6ef10eb 400 DMSG("\n");
stanvn 7:a26fa6ef10eb 401 *sak = pn532_packetbuffer[4];
stanvn 7:a26fa6ef10eb 402 /* Card appears to be Mifare Classic */
stanvn 7:a26fa6ef10eb 403 *uidLength = pn532_packetbuffer[5];
stanvn 7:a26fa6ef10eb 404
stanvn 7:a26fa6ef10eb 405 for (uint8_t i = 0; i < pn532_packetbuffer[5]; i++) {
stanvn 7:a26fa6ef10eb 406 uid[i] = pn532_packetbuffer[6 + i];
stanvn 7:a26fa6ef10eb 407 }
stanvn 7:a26fa6ef10eb 408
stanvn 7:a26fa6ef10eb 409 return 1;
stanvn 7:a26fa6ef10eb 410 }
stanvn 7:a26fa6ef10eb 411
yihui 0:9c6b9280c0e1 412 /***** Mifare Classic Functions ******/
yihui 0:9c6b9280c0e1 413
yihui 0:9c6b9280c0e1 414 /**************************************************************************/
yihui 1:b8cab5222fd0 415 /*!
yihui 0:9c6b9280c0e1 416 Indicates whether the specified block number is the first block
yihui 0:9c6b9280c0e1 417 in the sector (block 0 relative to the current sector)
yihui 0:9c6b9280c0e1 418 */
yihui 0:9c6b9280c0e1 419 /**************************************************************************/
yihui 0:9c6b9280c0e1 420 bool PN532::mifareclassic_IsFirstBlock (uint32_t uiBlock)
yihui 0:9c6b9280c0e1 421 {
yihui 1:b8cab5222fd0 422 // Test if we are in the small or big sectors
yihui 1:b8cab5222fd0 423 if (uiBlock < 128)
yihui 1:b8cab5222fd0 424 return ((uiBlock) % 4 == 0);
yihui 1:b8cab5222fd0 425 else
yihui 1:b8cab5222fd0 426 return ((uiBlock) % 16 == 0);
yihui 0:9c6b9280c0e1 427 }
yihui 0:9c6b9280c0e1 428
yihui 0:9c6b9280c0e1 429 /**************************************************************************/
yihui 1:b8cab5222fd0 430 /*!
yihui 0:9c6b9280c0e1 431 Indicates whether the specified block number is the sector trailer
yihui 0:9c6b9280c0e1 432 */
yihui 0:9c6b9280c0e1 433 /**************************************************************************/
yihui 0:9c6b9280c0e1 434 bool PN532::mifareclassic_IsTrailerBlock (uint32_t uiBlock)
yihui 0:9c6b9280c0e1 435 {
yihui 1:b8cab5222fd0 436 // Test if we are in the small or big sectors
yihui 1:b8cab5222fd0 437 if (uiBlock < 128)
yihui 1:b8cab5222fd0 438 return ((uiBlock + 1) % 4 == 0);
yihui 1:b8cab5222fd0 439 else
yihui 1:b8cab5222fd0 440 return ((uiBlock + 1) % 16 == 0);
yihui 0:9c6b9280c0e1 441 }
yihui 0:9c6b9280c0e1 442
yihui 0:9c6b9280c0e1 443 /**************************************************************************/
yihui 1:b8cab5222fd0 444 /*!
yihui 0:9c6b9280c0e1 445 Tries to authenticate a block of memory on a MIFARE card using the
yihui 0:9c6b9280c0e1 446 INDATAEXCHANGE command. See section 7.3.8 of the PN532 User Manual
yihui 0:9c6b9280c0e1 447 for more information on sending MIFARE and other commands.
yihui 0:9c6b9280c0e1 448
yihui 1:b8cab5222fd0 449 @param uid Pointer to a byte array containing the card UID
yihui 0:9c6b9280c0e1 450 @param uidLen The length (in bytes) of the card's UID (Should
yihui 0:9c6b9280c0e1 451 be 4 for MIFARE Classic)
yihui 0:9c6b9280c0e1 452 @param blockNumber The block number to authenticate. (0..63 for
yihui 0:9c6b9280c0e1 453 1KB cards, and 0..255 for 4KB cards).
yihui 0:9c6b9280c0e1 454 @param keyNumber Which key type to use during authentication
yihui 0:9c6b9280c0e1 455 (0 = MIFARE_CMD_AUTH_A, 1 = MIFARE_CMD_AUTH_B)
yihui 1:b8cab5222fd0 456 @param keyData Pointer to a byte array containing the 6 bytes
yihui 0:9c6b9280c0e1 457 key value
yihui 1:b8cab5222fd0 458
yihui 0:9c6b9280c0e1 459 @returns 1 if everything executed properly, 0 for an error
yihui 0:9c6b9280c0e1 460 */
yihui 0:9c6b9280c0e1 461 /**************************************************************************/
yihui 1:b8cab5222fd0 462 uint8_t PN532::mifareclassic_AuthenticateBlock (uint8_t *uid, uint8_t uidLen, uint32_t blockNumber, uint8_t keyNumber, uint8_t *keyData)
yihui 0:9c6b9280c0e1 463 {
yihui 1:b8cab5222fd0 464 uint8_t i;
yihui 1:b8cab5222fd0 465
yihui 1:b8cab5222fd0 466 // Hang on to the key and uid data
yihui 1:b8cab5222fd0 467 memcpy (_key, keyData, 6);
yihui 1:b8cab5222fd0 468 memcpy (_uid, uid, uidLen);
yihui 1:b8cab5222fd0 469 _uidLen = uidLen;
yihui 0:9c6b9280c0e1 470
yihui 1:b8cab5222fd0 471 // Prepare the authentication command //
yihui 1:b8cab5222fd0 472 pn532_packetbuffer[0] = PN532_COMMAND_INDATAEXCHANGE; /* Data Exchange Header */
yihui 1:b8cab5222fd0 473 pn532_packetbuffer[1] = 1; /* Max card numbers */
yihui 1:b8cab5222fd0 474 pn532_packetbuffer[2] = (keyNumber) ? MIFARE_CMD_AUTH_B : MIFARE_CMD_AUTH_A;
yihui 1:b8cab5222fd0 475 pn532_packetbuffer[3] = blockNumber; /* Block Number (1K = 0..63, 4K = 0..255 */
yihui 1:b8cab5222fd0 476 memcpy (pn532_packetbuffer + 4, _key, 6);
yihui 1:b8cab5222fd0 477 for (i = 0; i < _uidLen; i++) {
yihui 1:b8cab5222fd0 478 pn532_packetbuffer[10 + i] = _uid[i]; /* 4 bytes card ID */
yihui 1:b8cab5222fd0 479 }
yihui 1:b8cab5222fd0 480
yihui 1:b8cab5222fd0 481 if (HAL(writeCommand)(pn532_packetbuffer, 10 + _uidLen))
yihui 1:b8cab5222fd0 482 return 0;
yihui 0:9c6b9280c0e1 483
yihui 1:b8cab5222fd0 484 // Read the response packet
yihui 1:b8cab5222fd0 485 HAL(readResponse)(pn532_packetbuffer, sizeof(pn532_packetbuffer));
yihui 0:9c6b9280c0e1 486
yihui 1:b8cab5222fd0 487 // Check if the response is valid and we are authenticated???
yihui 1:b8cab5222fd0 488 // for an auth success it should be bytes 5-7: 0xD5 0x41 0x00
yihui 1:b8cab5222fd0 489 // Mifare auth error is technically byte 7: 0x14 but anything other and 0x00 is not good
yihui 1:b8cab5222fd0 490 if (pn532_packetbuffer[0] != 0x00) {
yihui 1:b8cab5222fd0 491 DMSG("Authentification failed\n");
yihui 1:b8cab5222fd0 492 return 0;
yihui 1:b8cab5222fd0 493 }
yihui 1:b8cab5222fd0 494
yihui 1:b8cab5222fd0 495 return 1;
yihui 0:9c6b9280c0e1 496 }
yihui 0:9c6b9280c0e1 497
yihui 0:9c6b9280c0e1 498 /**************************************************************************/
yihui 1:b8cab5222fd0 499 /*!
yihui 1:b8cab5222fd0 500 Tries to read an entire 16-bytes data block at the specified block
yihui 0:9c6b9280c0e1 501 address.
yihui 0:9c6b9280c0e1 502
yihui 0:9c6b9280c0e1 503 @param blockNumber The block number to authenticate. (0..63 for
yihui 0:9c6b9280c0e1 504 1KB cards, and 0..255 for 4KB cards).
yihui 1:b8cab5222fd0 505 @param data Pointer to the byte array that will hold the
yihui 0:9c6b9280c0e1 506 retrieved data (if any)
yihui 1:b8cab5222fd0 507
yihui 0:9c6b9280c0e1 508 @returns 1 if everything executed properly, 0 for an error
yihui 0:9c6b9280c0e1 509 */
yihui 0:9c6b9280c0e1 510 /**************************************************************************/
yihui 1:b8cab5222fd0 511 uint8_t PN532::mifareclassic_ReadDataBlock (uint8_t blockNumber, uint8_t *data)
yihui 0:9c6b9280c0e1 512 {
yihui 1:b8cab5222fd0 513 DMSG("Trying to read 16 bytes from block ");
yihui 1:b8cab5222fd0 514 DMSG_INT(blockNumber);
yihui 1:b8cab5222fd0 515
yihui 1:b8cab5222fd0 516 /* Prepare the command */
yihui 1:b8cab5222fd0 517 pn532_packetbuffer[0] = PN532_COMMAND_INDATAEXCHANGE;
yihui 1:b8cab5222fd0 518 pn532_packetbuffer[1] = 1; /* Card number */
yihui 1:b8cab5222fd0 519 pn532_packetbuffer[2] = MIFARE_CMD_READ; /* Mifare Read command = 0x30 */
yihui 1:b8cab5222fd0 520 pn532_packetbuffer[3] = blockNumber; /* Block Number (0..63 for 1K, 0..255 for 4K) */
yihui 0:9c6b9280c0e1 521
yihui 1:b8cab5222fd0 522 /* Send the command */
yihui 1:b8cab5222fd0 523 if (HAL(writeCommand)(pn532_packetbuffer, 4)) {
yihui 1:b8cab5222fd0 524 return 0;
yihui 1:b8cab5222fd0 525 }
yihui 0:9c6b9280c0e1 526
yihui 1:b8cab5222fd0 527 /* Read the response packet */
yihui 1:b8cab5222fd0 528 HAL(readResponse)(pn532_packetbuffer, sizeof(pn532_packetbuffer));
yihui 0:9c6b9280c0e1 529
yihui 1:b8cab5222fd0 530 /* If byte 8 isn't 0x00 we probably have an error */
yihui 1:b8cab5222fd0 531 if (pn532_packetbuffer[0] != 0x00) {
yihui 1:b8cab5222fd0 532 return 0;
yihui 1:b8cab5222fd0 533 }
yihui 0:9c6b9280c0e1 534
yihui 1:b8cab5222fd0 535 /* Copy the 16 data bytes to the output buffer */
yihui 1:b8cab5222fd0 536 /* Block content starts at byte 9 of a valid response */
yihui 1:b8cab5222fd0 537 memcpy (data, pn532_packetbuffer + 1, 16);
yihui 1:b8cab5222fd0 538
yihui 1:b8cab5222fd0 539 return 1;
yihui 0:9c6b9280c0e1 540 }
yihui 0:9c6b9280c0e1 541
yihui 0:9c6b9280c0e1 542 /**************************************************************************/
yihui 1:b8cab5222fd0 543 /*!
yihui 1:b8cab5222fd0 544 Tries to write an entire 16-bytes data block at the specified block
yihui 0:9c6b9280c0e1 545 address.
yihui 0:9c6b9280c0e1 546
yihui 0:9c6b9280c0e1 547 @param blockNumber The block number to authenticate. (0..63 for
yihui 0:9c6b9280c0e1 548 1KB cards, and 0..255 for 4KB cards).
yihui 1:b8cab5222fd0 549 @param data The byte array that contains the data to write.
yihui 1:b8cab5222fd0 550
yihui 0:9c6b9280c0e1 551 @returns 1 if everything executed properly, 0 for an error
yihui 0:9c6b9280c0e1 552 */
yihui 0:9c6b9280c0e1 553 /**************************************************************************/
yihui 1:b8cab5222fd0 554 uint8_t PN532::mifareclassic_WriteDataBlock (uint8_t blockNumber, uint8_t *data)
yihui 0:9c6b9280c0e1 555 {
yihui 1:b8cab5222fd0 556 /* Prepare the first command */
yihui 1:b8cab5222fd0 557 pn532_packetbuffer[0] = PN532_COMMAND_INDATAEXCHANGE;
yihui 1:b8cab5222fd0 558 pn532_packetbuffer[1] = 1; /* Card number */
yihui 1:b8cab5222fd0 559 pn532_packetbuffer[2] = MIFARE_CMD_WRITE; /* Mifare Write command = 0xA0 */
yihui 1:b8cab5222fd0 560 pn532_packetbuffer[3] = blockNumber; /* Block Number (0..63 for 1K, 0..255 for 4K) */
yihui 1:b8cab5222fd0 561 memcpy (pn532_packetbuffer + 4, data, 16); /* Data Payload */
yihui 0:9c6b9280c0e1 562
yihui 1:b8cab5222fd0 563 /* Send the command */
yihui 1:b8cab5222fd0 564 if (HAL(writeCommand)(pn532_packetbuffer, 20)) {
yihui 1:b8cab5222fd0 565 return 0;
yihui 1:b8cab5222fd0 566 }
yihui 1:b8cab5222fd0 567
yihui 1:b8cab5222fd0 568 /* Read the response packet */
yihui 1:b8cab5222fd0 569 return (0 < HAL(readResponse)(pn532_packetbuffer, sizeof(pn532_packetbuffer)));
yihui 0:9c6b9280c0e1 570 }
yihui 0:9c6b9280c0e1 571
yihui 0:9c6b9280c0e1 572 /**************************************************************************/
yihui 1:b8cab5222fd0 573 /*!
yihui 1:b8cab5222fd0 574 Formats a Mifare Classic card to store NDEF Records
yihui 1:b8cab5222fd0 575
yihui 0:9c6b9280c0e1 576 @returns 1 if everything executed properly, 0 for an error
yihui 0:9c6b9280c0e1 577 */
yihui 0:9c6b9280c0e1 578 /**************************************************************************/
yihui 0:9c6b9280c0e1 579 uint8_t PN532::mifareclassic_FormatNDEF (void)
yihui 0:9c6b9280c0e1 580 {
yihui 1:b8cab5222fd0 581 uint8_t sectorbuffer1[16] = {0x14, 0x01, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1};
yihui 1:b8cab5222fd0 582 uint8_t sectorbuffer2[16] = {0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1};
yihui 1:b8cab5222fd0 583 uint8_t sectorbuffer3[16] = {0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0x78, 0x77, 0x88, 0xC1, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
yihui 1:b8cab5222fd0 584
yihui 1:b8cab5222fd0 585 // Note 0xA0 0xA1 0xA2 0xA3 0xA4 0xA5 must be used for key A
yihui 1:b8cab5222fd0 586 // for the MAD sector in NDEF records (sector 0)
yihui 0:9c6b9280c0e1 587
yihui 1:b8cab5222fd0 588 // Write block 1 and 2 to the card
yihui 1:b8cab5222fd0 589 if (!(mifareclassic_WriteDataBlock (1, sectorbuffer1)))
yihui 1:b8cab5222fd0 590 return 0;
yihui 1:b8cab5222fd0 591 if (!(mifareclassic_WriteDataBlock (2, sectorbuffer2)))
yihui 1:b8cab5222fd0 592 return 0;
yihui 1:b8cab5222fd0 593 // Write key A and access rights card
yihui 1:b8cab5222fd0 594 if (!(mifareclassic_WriteDataBlock (3, sectorbuffer3)))
yihui 1:b8cab5222fd0 595 return 0;
yihui 0:9c6b9280c0e1 596
yihui 1:b8cab5222fd0 597 // Seems that everything was OK (?!)
yihui 1:b8cab5222fd0 598 return 1;
yihui 0:9c6b9280c0e1 599 }
yihui 0:9c6b9280c0e1 600
yihui 0:9c6b9280c0e1 601 /**************************************************************************/
yihui 1:b8cab5222fd0 602 /*!
yihui 0:9c6b9280c0e1 603 Writes an NDEF URI Record to the specified sector (1..15)
yihui 1:b8cab5222fd0 604
yihui 0:9c6b9280c0e1 605 Note that this function assumes that the Mifare Classic card is
yihui 0:9c6b9280c0e1 606 already formatted to work as an "NFC Forum Tag" and uses a MAD1
yihui 0:9c6b9280c0e1 607 file system. You can use the NXP TagWriter app on Android to
yihui 0:9c6b9280c0e1 608 properly format cards for this.
yihui 0:9c6b9280c0e1 609
yihui 0:9c6b9280c0e1 610 @param sectorNumber The sector that the URI record should be written
yihui 0:9c6b9280c0e1 611 to (can be 1..15 for a 1K card)
yihui 1:b8cab5222fd0 612 @param uriIdentifier The uri identifier code (0 = none, 0x01 =
yihui 0:9c6b9280c0e1 613 "http://www.", etc.)
yihui 0:9c6b9280c0e1 614 @param url The uri text to write (max 38 characters).
yihui 1:b8cab5222fd0 615
yihui 0:9c6b9280c0e1 616 @returns 1 if everything executed properly, 0 for an error
yihui 0:9c6b9280c0e1 617 */
yihui 0:9c6b9280c0e1 618 /**************************************************************************/
yihui 1:b8cab5222fd0 619 uint8_t PN532::mifareclassic_WriteNDEFURI (uint8_t sectorNumber, uint8_t uriIdentifier, const char *url)
yihui 0:9c6b9280c0e1 620 {
yihui 1:b8cab5222fd0 621 // Figure out how long the string is
yihui 1:b8cab5222fd0 622 uint8_t len = strlen(url);
yihui 1:b8cab5222fd0 623
yihui 1:b8cab5222fd0 624 // Make sure we're within a 1K limit for the sector number
yihui 1:b8cab5222fd0 625 if ((sectorNumber < 1) || (sectorNumber > 15))
yihui 1:b8cab5222fd0 626 return 0;
yihui 1:b8cab5222fd0 627
yihui 1:b8cab5222fd0 628 // Make sure the URI payload is between 1 and 38 chars
yihui 1:b8cab5222fd0 629 if ((len < 1) || (len > 38))
yihui 1:b8cab5222fd0 630 return 0;
yihui 1:b8cab5222fd0 631
yihui 1:b8cab5222fd0 632 // Note 0xD3 0xF7 0xD3 0xF7 0xD3 0xF7 must be used for key A
yihui 1:b8cab5222fd0 633 // in NDEF records
yihui 0:9c6b9280c0e1 634
yihui 1:b8cab5222fd0 635 // Setup the sector buffer (w/pre-formatted TLV wrapper and NDEF message)
yihui 1:b8cab5222fd0 636 uint8_t sectorbuffer1[16] = {0x00, 0x00, 0x03, len + 5, 0xD1, 0x01, len + 1, 0x55, uriIdentifier, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
yihui 1:b8cab5222fd0 637 uint8_t sectorbuffer2[16] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
yihui 1:b8cab5222fd0 638 uint8_t sectorbuffer3[16] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
yihui 1:b8cab5222fd0 639 uint8_t sectorbuffer4[16] = {0xD3, 0xF7, 0xD3, 0xF7, 0xD3, 0xF7, 0x7F, 0x07, 0x88, 0x40, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
yihui 1:b8cab5222fd0 640 if (len <= 6) {
yihui 1:b8cab5222fd0 641 // Unlikely we'll get a url this short, but why not ...
yihui 1:b8cab5222fd0 642 memcpy (sectorbuffer1 + 9, url, len);
yihui 1:b8cab5222fd0 643 sectorbuffer1[len + 9] = 0xFE;
yihui 1:b8cab5222fd0 644 } else if (len == 7) {
yihui 1:b8cab5222fd0 645 // 0xFE needs to be wrapped around to next block
yihui 1:b8cab5222fd0 646 memcpy (sectorbuffer1 + 9, url, len);
yihui 1:b8cab5222fd0 647 sectorbuffer2[0] = 0xFE;
yihui 1:b8cab5222fd0 648 } else if ((len > 7) || (len <= 22)) {
yihui 1:b8cab5222fd0 649 // Url fits in two blocks
yihui 1:b8cab5222fd0 650 memcpy (sectorbuffer1 + 9, url, 7);
yihui 1:b8cab5222fd0 651 memcpy (sectorbuffer2, url + 7, len - 7);
yihui 1:b8cab5222fd0 652 sectorbuffer2[len - 7] = 0xFE;
yihui 1:b8cab5222fd0 653 } else if (len == 23) {
yihui 1:b8cab5222fd0 654 // 0xFE needs to be wrapped around to final block
yihui 1:b8cab5222fd0 655 memcpy (sectorbuffer1 + 9, url, 7);
yihui 1:b8cab5222fd0 656 memcpy (sectorbuffer2, url + 7, len - 7);
yihui 1:b8cab5222fd0 657 sectorbuffer3[0] = 0xFE;
yihui 1:b8cab5222fd0 658 } else {
yihui 1:b8cab5222fd0 659 // Url fits in three blocks
yihui 1:b8cab5222fd0 660 memcpy (sectorbuffer1 + 9, url, 7);
yihui 1:b8cab5222fd0 661 memcpy (sectorbuffer2, url + 7, 16);
yihui 1:b8cab5222fd0 662 memcpy (sectorbuffer3, url + 23, len - 24);
yihui 1:b8cab5222fd0 663 sectorbuffer3[len - 22] = 0xFE;
yihui 1:b8cab5222fd0 664 }
yihui 1:b8cab5222fd0 665
yihui 1:b8cab5222fd0 666 // Now write all three blocks back to the card
yihui 1:b8cab5222fd0 667 if (!(mifareclassic_WriteDataBlock (sectorNumber * 4, sectorbuffer1)))
yihui 1:b8cab5222fd0 668 return 0;
yihui 1:b8cab5222fd0 669 if (!(mifareclassic_WriteDataBlock ((sectorNumber * 4) + 1, sectorbuffer2)))
yihui 1:b8cab5222fd0 670 return 0;
yihui 1:b8cab5222fd0 671 if (!(mifareclassic_WriteDataBlock ((sectorNumber * 4) + 2, sectorbuffer3)))
yihui 1:b8cab5222fd0 672 return 0;
yihui 1:b8cab5222fd0 673 if (!(mifareclassic_WriteDataBlock ((sectorNumber * 4) + 3, sectorbuffer4)))
yihui 1:b8cab5222fd0 674 return 0;
yihui 1:b8cab5222fd0 675
yihui 1:b8cab5222fd0 676 // Seems that everything was OK (?!)
yihui 1:b8cab5222fd0 677 return 1;
yihui 0:9c6b9280c0e1 678 }
yihui 0:9c6b9280c0e1 679
yihui 0:9c6b9280c0e1 680 /***** Mifare Ultralight Functions ******/
yihui 0:9c6b9280c0e1 681
yihui 0:9c6b9280c0e1 682 /**************************************************************************/
yihui 1:b8cab5222fd0 683 /*!
yihui 1:b8cab5222fd0 684 Tries to read an entire 4-bytes page at the specified address.
yihui 0:9c6b9280c0e1 685
yihui 0:9c6b9280c0e1 686 @param page The page number (0..63 in most cases)
yihui 1:b8cab5222fd0 687 @param buffer Pointer to the byte array that will hold the
yihui 0:9c6b9280c0e1 688 retrieved data (if any)
yihui 0:9c6b9280c0e1 689 */
yihui 0:9c6b9280c0e1 690 /**************************************************************************/
yihui 1:b8cab5222fd0 691 uint8_t PN532::mifareultralight_ReadPage (uint8_t page, uint8_t *buffer)
yihui 0:9c6b9280c0e1 692 {
yihui 1:b8cab5222fd0 693 if (page >= 64) {
yihui 1:b8cab5222fd0 694 DMSG("Page value out of range\n");
yihui 1:b8cab5222fd0 695 return 0;
yihui 1:b8cab5222fd0 696 }
yihui 0:9c6b9280c0e1 697
yihui 1:b8cab5222fd0 698 /* Prepare the command */
yihui 1:b8cab5222fd0 699 pn532_packetbuffer[0] = PN532_COMMAND_INDATAEXCHANGE;
yihui 1:b8cab5222fd0 700 pn532_packetbuffer[1] = 1; /* Card number */
yihui 1:b8cab5222fd0 701 pn532_packetbuffer[2] = MIFARE_CMD_READ; /* Mifare Read command = 0x30 */
yihui 1:b8cab5222fd0 702 pn532_packetbuffer[3] = page; /* Page Number (0..63 in most cases) */
yihui 1:b8cab5222fd0 703
yihui 1:b8cab5222fd0 704 /* Send the command */
yihui 1:b8cab5222fd0 705 if (HAL(writeCommand)(pn532_packetbuffer, 4)) {
yihui 1:b8cab5222fd0 706 return 0;
yihui 1:b8cab5222fd0 707 }
yihui 0:9c6b9280c0e1 708
yihui 1:b8cab5222fd0 709 /* Read the response packet */
yihui 1:b8cab5222fd0 710 HAL(readResponse)(pn532_packetbuffer, sizeof(pn532_packetbuffer));
yihui 0:9c6b9280c0e1 711
yihui 1:b8cab5222fd0 712 /* If byte 8 isn't 0x00 we probably have an error */
yihui 1:b8cab5222fd0 713 if (pn532_packetbuffer[0] == 0x00) {
yihui 1:b8cab5222fd0 714 /* Copy the 4 data bytes to the output buffer */
yihui 1:b8cab5222fd0 715 /* Block content starts at byte 9 of a valid response */
yihui 1:b8cab5222fd0 716 /* Note that the command actually reads 16 bytes or 4 */
yihui 1:b8cab5222fd0 717 /* pages at a time ... we simply discard the last 12 */
yihui 1:b8cab5222fd0 718 /* bytes */
yihui 1:b8cab5222fd0 719 memcpy (buffer, pn532_packetbuffer + 1, 4);
yihui 1:b8cab5222fd0 720 } else {
yihui 1:b8cab5222fd0 721 return 0;
yihui 1:b8cab5222fd0 722 }
yihui 0:9c6b9280c0e1 723
yihui 1:b8cab5222fd0 724 // Return OK signal
yihui 1:b8cab5222fd0 725 return 1;
yihui 0:9c6b9280c0e1 726 }
yihui 0:9c6b9280c0e1 727
yihui 0:9c6b9280c0e1 728 /**************************************************************************/
yihui 1:b8cab5222fd0 729 /*!
yihui 0:9c6b9280c0e1 730 @brief Exchanges an APDU with the currently inlisted peer
yihui 0:9c6b9280c0e1 731
yihui 0:9c6b9280c0e1 732 @param send Pointer to data to send
yihui 0:9c6b9280c0e1 733 @param sendLength Length of the data to send
yihui 0:9c6b9280c0e1 734 @param response Pointer to response data
yihui 0:9c6b9280c0e1 735 @param responseLength Pointer to the response data length
yihui 0:9c6b9280c0e1 736 */
yihui 0:9c6b9280c0e1 737 /**************************************************************************/
yihui 1:b8cab5222fd0 738 bool PN532::inDataExchange(uint8_t *send, uint8_t sendLength, uint8_t *response, uint8_t *responseLength)
yihui 1:b8cab5222fd0 739 {
screamer 6:26c1b3b6c192 740 //uint8_t i;
yihui 0:9c6b9280c0e1 741
yihui 1:b8cab5222fd0 742 pn532_packetbuffer[0] = 0x40; // PN532_COMMAND_INDATAEXCHANGE;
yihui 1:b8cab5222fd0 743 pn532_packetbuffer[1] = inListedTag;
yihui 1:b8cab5222fd0 744
yihui 3:4189a10038e6 745 if (HAL(writeCommand)(pn532_packetbuffer, 2, send, sendLength)) {
yihui 1:b8cab5222fd0 746 return false;
yihui 1:b8cab5222fd0 747 }
yihui 0:9c6b9280c0e1 748
yihui 3:4189a10038e6 749 int16_t status = HAL(readResponse)(response, *responseLength, 1000);
yihui 1:b8cab5222fd0 750 if (status < 0) {
yihui 1:b8cab5222fd0 751 return false;
yihui 1:b8cab5222fd0 752 }
yihui 1:b8cab5222fd0 753
yihui 3:4189a10038e6 754 if ((response[0] & 0x3f) != 0) {
yihui 1:b8cab5222fd0 755 DMSG("Status code indicates an error\n");
yihui 1:b8cab5222fd0 756 return false;
yihui 1:b8cab5222fd0 757 }
yihui 0:9c6b9280c0e1 758
yihui 3:4189a10038e6 759 uint8_t length = status;
yihui 1:b8cab5222fd0 760 length -= 1;
yihui 1:b8cab5222fd0 761
yihui 1:b8cab5222fd0 762 if (length > *responseLength) {
yihui 1:b8cab5222fd0 763 length = *responseLength; // silent truncation...
yihui 1:b8cab5222fd0 764 }
yihui 1:b8cab5222fd0 765
yihui 3:4189a10038e6 766 for (uint8_t i = 0; i < length; i++) {
yihui 3:4189a10038e6 767 response[i] = response[i + 1];
yihui 1:b8cab5222fd0 768 }
yihui 1:b8cab5222fd0 769 *responseLength = length;
yihui 1:b8cab5222fd0 770
yihui 1:b8cab5222fd0 771 return true;
yihui 0:9c6b9280c0e1 772 }
yihui 0:9c6b9280c0e1 773
yihui 0:9c6b9280c0e1 774 /**************************************************************************/
yihui 1:b8cab5222fd0 775 /*!
yihui 0:9c6b9280c0e1 776 @brief 'InLists' a passive target. PN532 acting as reader/initiator,
yihui 0:9c6b9280c0e1 777 peer acting as card/responder.
yihui 0:9c6b9280c0e1 778 */
yihui 0:9c6b9280c0e1 779 /**************************************************************************/
yihui 1:b8cab5222fd0 780 bool PN532::inListPassiveTarget()
yihui 1:b8cab5222fd0 781 {
yihui 1:b8cab5222fd0 782 pn532_packetbuffer[0] = PN532_COMMAND_INLISTPASSIVETARGET;
yihui 1:b8cab5222fd0 783 pn532_packetbuffer[1] = 1;
yihui 1:b8cab5222fd0 784 pn532_packetbuffer[2] = 0;
yihui 0:9c6b9280c0e1 785
yihui 1:b8cab5222fd0 786 DMSG("inList passive target\n");
yihui 1:b8cab5222fd0 787
yihui 1:b8cab5222fd0 788 if (HAL(writeCommand)(pn532_packetbuffer, 3)) {
yihui 1:b8cab5222fd0 789 return false;
yihui 1:b8cab5222fd0 790 }
yihui 0:9c6b9280c0e1 791
yihui 1:b8cab5222fd0 792 int16_t status = HAL(readResponse)(pn532_packetbuffer, sizeof(pn532_packetbuffer), 30000);
yihui 1:b8cab5222fd0 793 if (status < 0) {
yihui 1:b8cab5222fd0 794 return false;
yihui 1:b8cab5222fd0 795 }
yihui 0:9c6b9280c0e1 796
yihui 1:b8cab5222fd0 797 if (pn532_packetbuffer[0] != 1) {
yihui 1:b8cab5222fd0 798 return false;
yihui 1:b8cab5222fd0 799 }
yihui 1:b8cab5222fd0 800
yihui 1:b8cab5222fd0 801 inListedTag = pn532_packetbuffer[1];
yihui 1:b8cab5222fd0 802
yihui 1:b8cab5222fd0 803 return true;
yihui 0:9c6b9280c0e1 804 }
yihui 0:9c6b9280c0e1 805
yihui 3:4189a10038e6 806 int8_t PN532::tgInitAsTarget(const uint8_t* command, const uint8_t len, const uint16_t timeout){
yihui 3:4189a10038e6 807
yihui 3:4189a10038e6 808 int8_t status = HAL(writeCommand)(command, len);
yihui 3:4189a10038e6 809 if (status < 0) {
yihui 3:4189a10038e6 810 return -1;
yihui 3:4189a10038e6 811 }
yihui 3:4189a10038e6 812
yihui 3:4189a10038e6 813 status = HAL(readResponse)(pn532_packetbuffer, sizeof(pn532_packetbuffer), timeout);
yihui 3:4189a10038e6 814 if (status > 0) {
yihui 3:4189a10038e6 815 return 1;
yihui 3:4189a10038e6 816 } else if (PN532_TIMEOUT == status) {
yihui 3:4189a10038e6 817 return 0;
yihui 3:4189a10038e6 818 } else {
yihui 3:4189a10038e6 819 return -2;
yihui 3:4189a10038e6 820 }
yihui 3:4189a10038e6 821 }
yihui 3:4189a10038e6 822
yihui 0:9c6b9280c0e1 823 /**
yihui 0:9c6b9280c0e1 824 * Peer to Peer
yihui 0:9c6b9280c0e1 825 */
yihui 3:4189a10038e6 826 int8_t PN532::tgInitAsTarget(uint16_t timeout)
yihui 0:9c6b9280c0e1 827 {
yihui 3:4189a10038e6 828 const uint8_t command[] = {
yihui 1:b8cab5222fd0 829 PN532_COMMAND_TGINITASTARGET,
yihui 1:b8cab5222fd0 830 0,
yihui 1:b8cab5222fd0 831 0x00, 0x00, //SENS_RES
yihui 1:b8cab5222fd0 832 0x00, 0x00, 0x00, //NFCID1
yihui 1:b8cab5222fd0 833 0x40, //SEL_RES
yihui 0:9c6b9280c0e1 834
yihui 1:b8cab5222fd0 835 0x01, 0xFE, 0x0F, 0xBB, 0xBA, 0xA6, 0xC9, 0x89, // POL_RES
yihui 1:b8cab5222fd0 836 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
yihui 1:b8cab5222fd0 837 0xFF, 0xFF,
yihui 0:9c6b9280c0e1 838
yihui 1:b8cab5222fd0 839 0x01, 0xFE, 0x0F, 0xBB, 0xBA, 0xA6, 0xC9, 0x89, 0x00, 0x00, //NFCID3t: Change this to desired value
yihui 0:9c6b9280c0e1 840
yihui 1:b8cab5222fd0 841 0x06, 0x46, 0x66, 0x6D, 0x01, 0x01, 0x10, 0x00// LLCP magic number and version parameter
yihui 1:b8cab5222fd0 842 };
yihui 3:4189a10038e6 843 return tgInitAsTarget(command, sizeof(command), timeout);
yihui 3:4189a10038e6 844 }
yihui 1:b8cab5222fd0 845
yihui 3:4189a10038e6 846 int16_t PN532::tgGetData(uint8_t *buf, uint8_t len)
yihui 3:4189a10038e6 847 {
yihui 3:4189a10038e6 848 buf[0] = PN532_COMMAND_TGGETDATA;
yihui 3:4189a10038e6 849
yihui 3:4189a10038e6 850 if (HAL(writeCommand)(buf, 1)) {
yihui 3:4189a10038e6 851 return -1;
yihui 3:4189a10038e6 852 }
yihui 3:4189a10038e6 853
yihui 3:4189a10038e6 854 int16_t status = HAL(readResponse)(buf, len, 3000);
yihui 3:4189a10038e6 855 if (0 >= status) {
yihui 0:9c6b9280c0e1 856 return status;
yihui 0:9c6b9280c0e1 857 }
yihui 1:b8cab5222fd0 858
yihui 3:4189a10038e6 859 uint16_t length = status - 1;
yihui 1:b8cab5222fd0 860
yihui 1:b8cab5222fd0 861
yihui 3:4189a10038e6 862 if (buf[0] != 0) {
yihui 0:9c6b9280c0e1 863 DMSG("status is not ok\n");
yihui 0:9c6b9280c0e1 864 return -5;
yihui 0:9c6b9280c0e1 865 }
yihui 1:b8cab5222fd0 866
yihui 3:4189a10038e6 867 for (uint8_t i = 0; i < length; i++) {
yihui 3:4189a10038e6 868 buf[i] = buf[i + 1];
yihui 3:4189a10038e6 869 }
yihui 0:9c6b9280c0e1 870
yihui 3:4189a10038e6 871 return length;
yihui 0:9c6b9280c0e1 872 }
yihui 0:9c6b9280c0e1 873
yihui 3:4189a10038e6 874 bool PN532::tgSetData(const uint8_t *header, uint8_t hlen, const uint8_t *body, uint8_t blen)
yihui 0:9c6b9280c0e1 875 {
yihui 3:4189a10038e6 876 if (hlen > (sizeof(pn532_packetbuffer) - 1)) {
yihui 0:9c6b9280c0e1 877 return false;
yihui 0:9c6b9280c0e1 878 }
yihui 1:b8cab5222fd0 879
yihui 3:4189a10038e6 880 for (int8_t i = hlen - 1; i >= 0; i--){
yihui 3:4189a10038e6 881 pn532_packetbuffer[i + 1] = header[i];
yihui 3:4189a10038e6 882 }
yihui 3:4189a10038e6 883 pn532_packetbuffer[0] = PN532_COMMAND_TGSETDATA;
yihui 1:b8cab5222fd0 884
yihui 3:4189a10038e6 885 if (HAL(writeCommand)(pn532_packetbuffer, hlen + 1, body, blen)) {
yihui 0:9c6b9280c0e1 886 return false;
yihui 0:9c6b9280c0e1 887 }
yihui 1:b8cab5222fd0 888
yihui 0:9c6b9280c0e1 889 if (0 > HAL(readResponse)(pn532_packetbuffer, sizeof(pn532_packetbuffer), 3000)) {
yihui 0:9c6b9280c0e1 890 return false;
yihui 0:9c6b9280c0e1 891 }
yihui 1:b8cab5222fd0 892
yihui 0:9c6b9280c0e1 893 if (0 != pn532_packetbuffer[0]) {
yihui 0:9c6b9280c0e1 894 return false;
yihui 0:9c6b9280c0e1 895 }
yihui 1:b8cab5222fd0 896
yihui 0:9c6b9280c0e1 897 return true;
yihui 0:9c6b9280c0e1 898 }
yihui 3:4189a10038e6 899
yihui 3:4189a10038e6 900 int16_t PN532::inRelease(const uint8_t relevantTarget){
yihui 3:4189a10038e6 901
yihui 3:4189a10038e6 902 pn532_packetbuffer[0] = PN532_COMMAND_INRELEASE;
yihui 3:4189a10038e6 903 pn532_packetbuffer[1] = relevantTarget;
yihui 3:4189a10038e6 904
yihui 3:4189a10038e6 905 if (HAL(writeCommand)(pn532_packetbuffer, 2)) {
yihui 3:4189a10038e6 906 return 0;
yihui 3:4189a10038e6 907 }
yihui 3:4189a10038e6 908
yihui 3:4189a10038e6 909 // read data packet
yihui 3:4189a10038e6 910 return HAL(readResponse)(pn532_packetbuffer, sizeof(pn532_packetbuffer));
yihui 3:4189a10038e6 911 }
yihui 3:4189a10038e6 912
yihui 3:4189a10038e6 913