libnfc  1.4.2
pn53x.c
1 /*-
2  * Public platform independent Near Field Communication (NFC) library
3  *
4  * Copyright (C) 2009, Roel Verdult, Romuald Conty
5  * Copyright (C) 2010, Roel Verdult, Romuald Conty, Romain Tartière
6  *
7  * This program is free software: you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation, either version 3 of the License, or (at your
10  * option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15  * more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>
19  */
20 
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif // HAVE_CONFIG_H
29 
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <stdlib.h>
34 
35 #include <nfc/nfc.h>
36 #include <nfc/nfc-messages.h>
37 
38 #include "pn53x.h"
39 #include "../mirror-subr.h"
40 
41 #ifdef _WIN32
42 # include "../../contrib/windows.h"
43 #endif
44 
45 #include <sys/param.h>
46 
47 // PN53X configuration
48 const byte_t pncmd_get_firmware_version[2] = { 0xD4, 0x02 };
49 const byte_t pncmd_get_general_status[2] = { 0xD4, 0x04 };
50 const byte_t pncmd_get_register[4] = { 0xD4, 0x06 };
51 const byte_t pncmd_set_register[5] = { 0xD4, 0x08 };
52 const byte_t pncmd_set_parameters[3] = { 0xD4, 0x12 };
53 const byte_t pncmd_rf_configure[14] = { 0xD4, 0x32 };
54 
55 // Reader
56 const byte_t pncmd_initiator_list_passive[264] = { 0xD4, 0x4A };
57 const byte_t pncmd_initiator_jump_for_dep[68] = { 0xD4, 0x56 };
58 const byte_t pncmd_initiator_select[3] = { 0xD4, 0x54 };
59 const byte_t pncmd_initiator_deselect[3] = { 0xD4, 0x44, 0x00 };
60 const byte_t pncmd_initiator_release[3] = { 0xD4, 0x52, 0x00 };
61 const byte_t pncmd_initiator_set_baud_rate[5] = { 0xD4, 0x4E };
62 const byte_t pncmd_initiator_exchange_data[265] = { 0xD4, 0x40 };
63 const byte_t pncmd_initiator_exchange_raw_data[266] = { 0xD4, 0x42 };
64 const byte_t pncmd_initiator_auto_poll[5] = { 0xD4, 0x60 };
65 
66 // Target
67 const byte_t pncmd_target_get_data[2] = { 0xD4, 0x86 };
68 const byte_t pncmd_target_set_data[264] = { 0xD4, 0x8E };
69 const byte_t pncmd_target_init[2] = { 0xD4, 0x8C };
70 //Example of default values for PN532 or PN533:
71 //const byte_t pncmd_target_init[39] = { 0xD4, 0x8C, 0x00, 0x08, 0x00, 0x12, 0x34, 0x56, 0x40, 0x01, 0xFE, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xFF, 0xFF, 0xAA, 0x99, 0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11, 0x00, 0x00 };
72 const byte_t pncmd_target_virtual_card[4] = { 0xD4, 0x14 };
73 const byte_t pncmd_target_get_initiator_command[2] = { 0xD4, 0x88 };
74 const byte_t pncmd_target_response_to_initiator[264] = { 0xD4, 0x90 };
75 const byte_t pncmd_target_get_status[2] = { 0xD4, 0x8A };
76 
77 static const byte_t pn53x_ack_frame[] = { 0x00, 0x00, 0xff, 0x00, 0xff, 0x00 };
78 static const byte_t pn53x_nack_frame[] = { 0x00, 0x00, 0xff, 0xff, 0x00, 0x00 };
79 static const byte_t pn53x_error_frame[] = { 0x00, 0x00, 0xff, 0x01, 0xff, 0x7f, 0x81, 0x00 };
80 
81 /* prototypes */
82 const nfc_modulation_t pn53x_ptt_to_nm( const pn53x_target_type_t ptt );
83 const pn53x_modulation_t pn53x_nm_to_pm(const nfc_modulation_t nm);
84 const pn53x_target_type_t pn53x_nm_to_ptt(const nfc_modulation_t nm);
85 
86 bool
87 pn53x_init(nfc_device_t * pnd)
88 {
89  // CRC handling is enabled by default
90  pnd->bCrc = true;
91  // Parity handling is enabled by default
92  pnd->bPar = true;
93 
94  // Reset the ending transmission bits register, it is unknown what the last tranmission used there
95  pnd->ui8TxBits = 0;
96  if (!pn53x_set_reg (pnd, REG_CIU_BIT_FRAMING, SYMBOL_TX_LAST_BITS, 0x00)) {
97  return false;
98  }
99 
100  // We can't read these parameters, so we set a default config by using the SetParameters wrapper
101  // Note: pn53x_SetParameters() will save the sent value in pnd->ui8Parameters cache
102  if(!pn53x_SetParameters(pnd, PARAM_AUTO_ATR_RES | PARAM_AUTO_RATS)) {
103  return false;
104  }
105 
106  char abtFirmwareText[18];
107  if (!pn53x_get_firmware_version (pnd, abtFirmwareText)) {
108  return false;
109  }
110 
111  // Add the firmware revision to the device name
112  char *pcName;
113  pcName = strdup (pnd->acName);
114  snprintf (pnd->acName, DEVICE_NAME_LENGTH - 1, "%s - %s", pcName, abtFirmwareText);
115  free (pcName);
116  return true;
117 }
118 
119 bool
120 pn53x_check_ack_frame_callback (nfc_device_t * pnd, const byte_t * pbtRxFrame, const size_t szRxFrameLen)
121 {
122  if (szRxFrameLen >= sizeof (pn53x_ack_frame)) {
123  if (0 == memcmp (pbtRxFrame, pn53x_ack_frame, sizeof (pn53x_ack_frame))) {
124  // DBG ("%s", "PN53x ACKed");
125  return true;
126  } else if (0 == memcmp (pbtRxFrame, pn53x_nack_frame, sizeof (pn53x_nack_frame))) {
127  DBG ("%s", "PN53x NACKed");
128  // TODO Try to recover when PN53x NACKs !
129  // A counter could allow the command to be sent again (e.g. max 3 times)
130  pnd->iLastError = DENACK;
131  return false;
132  }
133  }
134  pnd->iLastError = DEACKMISMATCH;
135  ERR ("%s", "Unexpected PN53x reply!");
136 #if defined(DEBUG)
137  // coredump so that we can have a backtrace about how this code was reached.
138  abort ();
139 #endif
140  return false;
141 }
142 
143 bool
144 pn53x_check_error_frame_callback (nfc_device_t * pnd, const byte_t * pbtRxFrame, const size_t szRxFrameLen)
145 {
146  if (szRxFrameLen >= sizeof (pn53x_error_frame)) {
147  if (0 == memcmp (pbtRxFrame, pn53x_error_frame, sizeof (pn53x_error_frame))) {
148  DBG ("%s", "PN53x sent an error frame");
149  pnd->iLastError = DEISERRFRAME;
150  return false;
151  }
152  }
153 
154  return true;
155 }
156 
157 #define PN53x_REPLY_FRAME_MAX_LEN (PN53x_EXTENDED_FRAME_MAX_LEN + PN53x_EXTENDED_FRAME_OVERHEAD + sizeof(pn53x_ack_frame))
158 bool
159 pn53x_transceive (nfc_device_t * pnd, const byte_t * pbtTx, const size_t szTx, byte_t * pbtRx, size_t * pszRx)
160 {
161  byte_t abtRx[PN53x_REPLY_FRAME_MAX_LEN];
162  size_t szRx = PN53x_EXTENDED_FRAME_MAX_LEN;
163 
164  // Check if receiving buffers are available, if not, replace them
165  if (!pszRx || !pbtRx) {
166  pbtRx = abtRx;
167  pszRx = &szRx;
168  }
169 
170 #if defined(DEBUG)
171  if(*pszRx > PN53x_EXTENDED_FRAME_MAX_LEN) {
172  DBG( "Expected reply bytes count (*pszRx=%zu) is greater than MAX (PN53x_EXTENDED_FRAME_MAX_LEN=%d)", *pszRx, PN53x_EXTENDED_FRAME_MAX_LEN );
173  *pszRx=MIN(*pszRx, PN53x_EXTENDED_FRAME_MAX_LEN);
174 // abort();
175  }
176 #endif
177 
178  *pszRx += sizeof(pn53x_ack_frame) + PN53x_EXTENDED_FRAME_OVERHEAD;
179 
180  // Call the transceive callback function of the current device
181  if (!pnd->pdc->transceive (pnd, pbtTx, szTx, pbtRx, pszRx))
182  return false;
183  // TODO Put all these hex-coded command behind a human-readable #define (1.6.x)
184  // Should be proceed while we will fix Issue 110 (Rework the way that pn53x commands are built)
185  switch (pbtTx[1]) {
186  case 0x16: // PowerDown
187  case 0x40: // InDataExchange
188  case 0x42: // InCommunicateThru
189  case 0x44: // InDeselect
190  case 0x46: // InJumpForPSL
191  case 0x4e: // InPSL
192  case 0x50: // InATR
193  case 0x52: // InRelease
194  case 0x54: // InSelect
195  case 0x56: // InJumpForDEP
196  case 0x86: // TgGetData
197  case 0x88: // TgGetInitiatorCommand
198  case 0x8e: // TgSetData
199  case 0x90: // TgResponseToInitiator
200  case 0x92: // TgSetGeneralBytes
201  case 0x94: // TgSetMetaData
202  pnd->iLastError = pbtRx[0] & 0x3f;
203  break;
204  default:
205  pnd->iLastError = 0;
206  }
207  if (pnd->nc == NC_PN533) {
208  if ((pbtTx[1] == 0x06) // ReadRegister
209  || (pbtTx[1] == 0x08)) { // WriteRegister
210  // PN533 prepends its answer by a status byte
211  pnd->iLastError = pbtRx[0] & 0x3f;
212  }
213  }
214  return (0 == pnd->iLastError);
215 }
216 
217 bool
218 pn53x_get_reg (nfc_device_t * pnd, uint16_t ui16Reg, uint8_t * ui8Value)
219 {
220  byte_t abtCmd[sizeof (pncmd_get_register)];
221  memcpy (abtCmd, pncmd_get_register, sizeof (pncmd_get_register));
222 
223  abtCmd[2] = ui16Reg >> 8;
224  abtCmd[3] = ui16Reg & 0xff;
225 
226  byte_t abtRegValue[2];
227  size_t szValueLen = 3 + PN53x_NORMAL_FRAME_OVERHEAD;
228  if (pn53x_transceive (pnd, abtCmd, sizeof (pncmd_get_register), abtRegValue, &szValueLen)) {
229  if (pnd->nc == NC_PN533) {
230  // PN533 prepends its answer by a status byte
231  if (abtRegValue[0] == 0) { // 0x00
232  *ui8Value = abtRegValue[1];
233  } else {
234  return false;
235  }
236  } else {
237  *ui8Value = abtRegValue[0];
238  }
239  return true;
240  }
241  return false;
242 }
243 
244 bool
245 pn53x_set_reg (nfc_device_t * pnd, uint16_t ui16Reg, uint8_t ui8SymbolMask, uint8_t ui8Value)
246 {
247  uint8_t ui8Current;
248  byte_t abtCmd[sizeof (pncmd_set_register)];
249  memcpy (abtCmd, pncmd_set_register, sizeof (pncmd_set_register));
250 
251  abtCmd[2] = ui16Reg >> 8;
252  abtCmd[3] = ui16Reg & 0xff;
253  if (ui8SymbolMask != 0xff) {
254  if (!pn53x_get_reg (pnd, ui16Reg, &ui8Current))
255  return false;
256  abtCmd[4] = ui8Value | (ui8Current & (~ui8SymbolMask));
257  return (abtCmd[4] != ui8Current) ? pn53x_transceive (pnd, abtCmd, sizeof (pncmd_set_register), NULL, NULL) : true;
258  } else {
259  abtCmd[4] = ui8Value;
260  return pn53x_transceive (pnd, abtCmd, sizeof (pncmd_set_register), NULL, NULL);
261  }
262 }
263 
264 bool
265 pn53x_set_parameter (nfc_device_t * pnd, const uint8_t ui8Parameter, const bool bEnable)
266 {
267  uint8_t ui8Value = (bEnable) ? (pnd->ui8Parameters | ui8Parameter) : (pnd->ui8Parameters & ~(ui8Parameter));
268  if (ui8Value != pnd->ui8Parameters) {
269  return pn53x_SetParameters(pnd, ui8Value);
270  }
271  return true;
272 }
273 
274 bool
275 pn53x_SetParameters (nfc_device_t * pnd, const uint8_t ui8Value)
276 {
277  byte_t abtCmd[sizeof (pncmd_set_parameters)];
278  memcpy (abtCmd, pncmd_set_parameters, sizeof (pncmd_set_parameters));
279 
280  abtCmd[2] = ui8Value;
281  if(!pn53x_transceive (pnd, abtCmd, sizeof (pncmd_set_parameters), NULL, NULL)) {
282  return false;
283  }
284  // We save last parameters in register cache
285  pnd->ui8Parameters = ui8Value;
286  return true;
287 }
288 
289 bool
290 pn53x_set_tx_bits (nfc_device_t * pnd, const uint8_t ui8Bits)
291 {
292  // Test if we need to update the transmission bits register setting
293  if (pnd->ui8TxBits != ui8Bits) {
294  // Set the amount of transmission bits in the PN53X chip register
295  if (!pn53x_set_reg (pnd, REG_CIU_BIT_FRAMING, SYMBOL_TX_LAST_BITS, ui8Bits))
296  return false;
297 
298  // Store the new setting
299  ((nfc_device_t *) pnd)->ui8TxBits = ui8Bits;
300  }
301  return true;
302 }
303 
304 bool
305 pn53x_wrap_frame (const byte_t * pbtTx, const size_t szTxBits, const byte_t * pbtTxPar,
306  byte_t * pbtFrame, size_t * pszFrameBits)
307 {
308  byte_t btFrame;
309  byte_t btData;
310  uint32_t uiBitPos;
311  uint32_t uiDataPos = 0;
312  size_t szBitsLeft = szTxBits;
313 
314  // Make sure we should frame at least something
315  if (szBitsLeft == 0)
316  return false;
317 
318  // Handle a short response (1byte) as a special case
319  if (szBitsLeft < 9) {
320  *pbtFrame = *pbtTx;
321  *pszFrameBits = szTxBits;
322  return true;
323  }
324  // We start by calculating the frame length in bits
325  *pszFrameBits = szTxBits + (szTxBits / 8);
326 
327  // Parse the data bytes and add the parity bits
328  // This is really a sensitive process, mirror the frame bytes and append parity bits
329  // buffer = mirror(frame-byte) + parity + mirror(frame-byte) + parity + ...
330  // split "buffer" up in segments of 8 bits again and mirror them
331  // air-bytes = mirror(buffer-byte) + mirror(buffer-byte) + mirror(buffer-byte) + ..
332  while (true) {
333  // Reset the temporary frame byte;
334  btFrame = 0;
335 
336  for (uiBitPos = 0; uiBitPos < 8; uiBitPos++) {
337  // Copy as much data that fits in the frame byte
338  btData = mirror (pbtTx[uiDataPos]);
339  btFrame |= (btData >> uiBitPos);
340  // Save this frame byte
341  *pbtFrame = mirror (btFrame);
342  // Set the remaining bits of the date in the new frame byte and append the parity bit
343  btFrame = (btData << (8 - uiBitPos));
344  btFrame |= ((pbtTxPar[uiDataPos] & 0x01) << (7 - uiBitPos));
345  // Backup the frame bits we have so far
346  pbtFrame++;
347  *pbtFrame = mirror (btFrame);
348  // Increase the data (without parity bit) position
349  uiDataPos++;
350  // Test if we are done
351  if (szBitsLeft < 9)
352  return true;
353  szBitsLeft -= 8;
354  }
355  // Every 8 data bytes we lose one frame byte to the parities
356  pbtFrame++;
357  }
358 }
359 
360 bool
361 pn53x_unwrap_frame (const byte_t * pbtFrame, const size_t szFrameBits, byte_t * pbtRx, size_t * pszRxBits,
362  byte_t * pbtRxPar)
363 {
364  byte_t btFrame;
365  byte_t btData;
366  uint8_t uiBitPos;
367  uint32_t uiDataPos = 0;
368  byte_t *pbtFramePos = (byte_t *) pbtFrame;
369  size_t szBitsLeft = szFrameBits;
370 
371  // Make sure we should frame at least something
372  if (szBitsLeft == 0)
373  return false;
374 
375  // Handle a short response (1byte) as a special case
376  if (szBitsLeft < 9) {
377  *pbtRx = *pbtFrame;
378  *pszRxBits = szFrameBits;
379  return true;
380  }
381  // Calculate the data length in bits
382  *pszRxBits = szFrameBits - (szFrameBits / 9);
383 
384  // Parse the frame bytes, remove the parity bits and store them in the parity array
385  // This process is the reverse of WrapFrame(), look there for more info
386  while (true) {
387  for (uiBitPos = 0; uiBitPos < 8; uiBitPos++) {
388  btFrame = mirror (pbtFramePos[uiDataPos]);
389  btData = (btFrame << uiBitPos);
390  btFrame = mirror (pbtFramePos[uiDataPos + 1]);
391  btData |= (btFrame >> (8 - uiBitPos));
392  pbtRx[uiDataPos] = mirror (btData);
393  if (pbtRxPar != NULL)
394  pbtRxPar[uiDataPos] = ((btFrame >> (7 - uiBitPos)) & 0x01);
395  // Increase the data (without parity bit) position
396  uiDataPos++;
397  // Test if we are done
398  if (szBitsLeft < 9)
399  return true;
400  szBitsLeft -= 9;
401  }
402  // Every 8 data bytes we lose one frame byte to the parities
403  pbtFramePos++;
404  }
405 }
406 
407 bool
408 pn53x_decode_target_data (const byte_t * pbtRawData, size_t szRawData, nfc_chip_t nc, nfc_modulation_type_t nmt,
409  nfc_target_info_t * pnti)
410 {
411  uint8_t szAttribRes;
412 
413  switch (nmt) {
414  case NMT_ISO14443A:
415  // We skip the first byte: its the target number (Tg)
416  pbtRawData++;
417 
418  // Somehow they switched the lower and upper ATQA bytes around for the PN531 chipset
419  if (nc == NC_PN531) {
420  pnti->nai.abtAtqa[1] = *(pbtRawData++);
421  pnti->nai.abtAtqa[0] = *(pbtRawData++);
422  } else {
423  pnti->nai.abtAtqa[0] = *(pbtRawData++);
424  pnti->nai.abtAtqa[1] = *(pbtRawData++);
425  }
426  pnti->nai.btSak = *(pbtRawData++);
427  // Copy the NFCID1
428  pnti->nai.szUidLen = *(pbtRawData++);
429  memcpy (pnti->nai.abtUid, pbtRawData, pnti->nai.szUidLen);
430  pbtRawData += pnti->nai.szUidLen;
431 
432  // Did we received an optional ATS (Smardcard ATR)
433  if (szRawData > (pnti->nai.szUidLen + 5)) {
434  pnti->nai.szAtsLen = ((*(pbtRawData++)) - 1); // In pbtRawData, ATS Length byte is counted in ATS Frame.
435  memcpy (pnti->nai.abtAts, pbtRawData, pnti->nai.szAtsLen);
436  } else {
437  pnti->nai.szAtsLen = 0;
438  }
439 
440  // Strip CT (Cascade Tag) to retrieve and store the _real_ UID
441  // (e.g. 0x8801020304050607 is in fact 0x01020304050607)
442  if ((pnti->nai.szUidLen == 8) && (pnti->nai.abtUid[0] == 0x88)) {
443  pnti->nai.szUidLen = 7;
444  memmove (pnti->nai.abtUid, pnti->nai.abtUid + 1, 7);
445  } else if ((pnti->nai.szUidLen == 12) && (pnti->nai.abtUid[0] == 0x88) && (pnti->nai.abtUid[4] == 0x88)) {
446  pnti->nai.szUidLen = 10;
447  memmove (pnti->nai.abtUid, pnti->nai.abtUid + 1, 3);
448  memmove (pnti->nai.abtUid + 3, pnti->nai.abtUid + 5, 7);
449  }
450  break;
451 
452  case NMT_ISO14443B:
453  // We skip the first byte: its the target number (Tg)
454  pbtRawData++;
455 
456  // Now we are in ATQB, we skip the first ATQB byte always equal to 0x50
457  pbtRawData++;
458 
459  // Store the PUPI (Pseudo-Unique PICC Identifier)
460  memcpy (pnti->nbi.abtPupi, pbtRawData, 4);
461  pbtRawData += 4;
462 
463  // Store the Application Data
464  memcpy (pnti->nbi.abtApplicationData, pbtRawData, 4);
465  pbtRawData += 4;
466 
467  // Store the Protocol Info
468  memcpy (pnti->nbi.abtProtocolInfo, pbtRawData, 3);
469  pbtRawData += 3;
470 
471  // We leave the ATQB field, we now enter in Card IDentifier
472  szAttribRes = *(pbtRawData++);
473  if (szAttribRes) {
474  pnti->nbi.ui8CardIdentifier = *(pbtRawData++);
475  }
476  break;
477 
478  case NMT_FELICA:
479  // We skip the first byte: its the target number (Tg)
480  pbtRawData++;
481 
482  // Store the mandatory info
483  pnti->nfi.szLen = *(pbtRawData++);
484  pnti->nfi.btResCode = *(pbtRawData++);
485  // Copy the NFCID2t
486  memcpy (pnti->nfi.abtId, pbtRawData, 8);
487  pbtRawData += 8;
488  // Copy the felica padding
489  memcpy (pnti->nfi.abtPad, pbtRawData, 8);
490  pbtRawData += 8;
491  // Test if the System code (SYST_CODE) is available
492  if (pnti->nfi.szLen > 18) {
493  memcpy (pnti->nfi.abtSysCode, pbtRawData, 2);
494  }
495  break;
496  case NMT_JEWEL:
497  // We skip the first byte: its the target number (Tg)
498  pbtRawData++;
499 
500  // Store the mandatory info
501  memcpy (pnti->nji.btSensRes, pbtRawData, 2);
502  pbtRawData += 2;
503  memcpy (pnti->nji.btId, pbtRawData, 4);
504  break;
505  default:
506  return false;
507  break;
508  }
509  return true;
510 }
511 
512 bool
513 pn53x_initiator_select_passive_target (nfc_device_t * pnd,
514  const nfc_modulation_t nm,
515  const byte_t * pbtInitData, const size_t szInitData,
516  nfc_target_t * pnt)
517 {
518  size_t szTargetsData;
519  byte_t abtTargetsData[PN53x_EXTENDED_FRAME_MAX_LEN];
520 
521  const pn53x_modulation_t pm = pn53x_nm_to_pm(nm);
522  if (PM_UNDEFINED == pm) {
523  pnd->iLastError = DENOTSUP;
524  return false;
525  }
526  if (!pn53x_InListPassiveTarget (pnd, pm, 1, pbtInitData, szInitData, abtTargetsData, &szTargetsData))
527  return false;
528 
529  // Make sure one tag has been found, the PN53X returns 0x00 if none was available
530  if (abtTargetsData[0] == 0)
531  return false;
532 
533  // Is a tag info struct available
534  if (pnt) {
535  pnt->nm = nm;
536  // Fill the tag info struct with the values corresponding to this init modulation
537  if (!pn53x_decode_target_data (abtTargetsData + 1, szTargetsData - 1, pnd->nc, nm.nmt, &(pnt->nti))) {
538  return false;
539  }
540  }
541  return true;
542 }
543 
544 bool
545 pn53x_initiator_poll_targets (nfc_device_t * pnd,
546  const nfc_modulation_t * pnmModulations, const size_t szModulations,
547  const byte_t btPollNr, const byte_t btPeriod,
548  nfc_target_t * pntTargets, size_t * pszTargetFound)
549 {
550  size_t szTargetTypes = 0;
551  pn53x_target_type_t apttTargetTypes[32];
552  for (size_t n=0; n<szModulations; n++) {
553  const pn53x_target_type_t ptt = pn53x_nm_to_ptt(pnmModulations[n]);
554  if (PTT_UNDEFINED == ptt) {
555  pnd->iLastError = DENOTSUP;
556  return false;
557  }
558  apttTargetTypes[szTargetTypes] = ptt;
559  if ((pnd->bAutoIso14443_4) && (ptt == PTT_MIFARE)) { // Hack to have ATS
560  apttTargetTypes[szTargetTypes] = PTT_ISO14443_4A_106;
561  szTargetTypes++;
562  apttTargetTypes[szTargetTypes] = PTT_MIFARE;
563  }
564  szTargetTypes++;
565  }
566 
567  return pn53x_InAutoPoll (pnd, apttTargetTypes, szTargetTypes, btPollNr, btPeriod, pntTargets, pszTargetFound);
568 }
569 
570 
585 bool
586 pn53x_InListPassiveTarget (nfc_device_t * pnd,
587  const pn53x_modulation_t pmInitModulation, const byte_t szMaxTargets,
588  const byte_t * pbtInitiatorData, const size_t szInitiatorData,
589  byte_t * pbtTargetsData, size_t * pszTargetsData)
590 {
591  size_t szRx;
592  byte_t abtCmd[sizeof (pncmd_initiator_list_passive)];
593  memcpy (abtCmd, pncmd_initiator_list_passive, sizeof (pncmd_initiator_list_passive));
594 
595  abtCmd[2] = szMaxTargets; // MaxTg
596 
597  // XXX Is there is a better way to do handle supported modulations ?
598  switch(pmInitModulation) {
599  case PM_ISO14443A_106:
600  case PM_FELICA_212:
601  case PM_FELICA_424:
602  // all gone fine.
603  break;
604  case PM_ISO14443B_106:
605  if (!(pnd->btSupportByte & SUPPORT_ISO14443B)) {
606  // Eg. Some PN532 doesn't support type B!
607  pnd->iLastError = DENOTSUP;
608  return false;
609  }
610  break;
611  case PM_JEWEL_106:
612  if(pnd->nc == NC_PN531) {
613  // These modulations are not supported by pn531
614  pnd->iLastError = DENOTSUP;
615  return false;
616  }
617  break;
618  case PM_ISO14443B_212:
619  case PM_ISO14443B_424:
620  case PM_ISO14443B_847:
621  if((pnd->nc != NC_PN533) || (!(pnd->btSupportByte & SUPPORT_ISO14443B))) {
622  // These modulations are not supported by pn531 neither pn532
623  pnd->iLastError = DENOTSUP;
624  return false;
625  }
626  break;
627  default:
628  pnd->iLastError = DENOTSUP;
629  return false;
630  }
631  abtCmd[3] = pmInitModulation; // BrTy, the type of init modulation used for polling a passive tag
632 
633  // Set the optional initiator data (used for Felica, ISO14443B, Topaz Polling or for ISO14443A selecting a specific UID).
634  if (pbtInitiatorData)
635  memcpy (abtCmd + 4, pbtInitiatorData, szInitiatorData);
636 
637  // Try to find a tag, call the tranceive callback function of the current device
638  szRx = PN53x_EXTENDED_FRAME_MAX_LEN;
639  if (pn53x_transceive (pnd, abtCmd, 4 + szInitiatorData, pbtTargetsData, &szRx)) {
640  *pszTargetsData = szRx;
641  return true;
642  } else {
643  return false;
644  }
645 }
646 
647 bool
648 pn53x_InDeselect (nfc_device_t * pnd, const uint8_t ui8Target)
649 {
650  byte_t abtCmd[sizeof (pncmd_initiator_deselect)];
651  memcpy (abtCmd, pncmd_initiator_deselect, sizeof (pncmd_initiator_deselect));
652  abtCmd[2] = ui8Target;
653 
654  return (pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, NULL));
655 }
656 
657 bool
658 pn53x_InRelease (nfc_device_t * pnd, const uint8_t ui8Target)
659 {
660  byte_t abtCmd[sizeof (pncmd_initiator_release)];
661  memcpy (abtCmd, pncmd_initiator_release, sizeof (pncmd_initiator_release));
662  abtCmd[2] = ui8Target;
663 
664  return (pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, NULL));
665 }
666 
667 bool
668 pn53x_InAutoPoll (nfc_device_t * pnd,
669  const pn53x_target_type_t * ppttTargetTypes, const size_t szTargetTypes,
670  const byte_t btPollNr, const byte_t btPeriod, nfc_target_t * pntTargets, size_t * pszTargetFound)
671 {
672  size_t szTxInAutoPoll,
673  n;
674  byte_t abtRx[PN53x_EXTENDED_FRAME_MAX_LEN];
675  size_t szRx = PN53x_EXTENDED_FRAME_MAX_LEN;
676  bool res;
677  byte_t *pbtTxInAutoPoll;
678 
679  if (pnd->nc != NC_PN532) {
680  // This function is not supported by pn531 neither pn533
681  pnd->iLastError = DENOTSUP;
682  return false;
683  }
684  // InAutoPoll frame looks like this { 0xd4, 0x60, 0x0f, 0x01, 0x00 } => { direction, command, pollnr, period, types... }
685  szTxInAutoPoll = 4 + szTargetTypes;
686  pbtTxInAutoPoll = malloc (szTxInAutoPoll);
687  pbtTxInAutoPoll[0] = 0xd4;
688  pbtTxInAutoPoll[1] = 0x60;
689  pbtTxInAutoPoll[2] = btPollNr;
690  pbtTxInAutoPoll[3] = btPeriod;
691  for (n = 0; n < szTargetTypes; n++) {
692  pbtTxInAutoPoll[4 + n] = ppttTargetTypes[n];
693  }
694 
695  szRx = PN53x_EXTENDED_FRAME_MAX_LEN;
696  res = pn53x_transceive (pnd, pbtTxInAutoPoll, szTxInAutoPoll, abtRx, &szRx);
697 
698  if ((szRx == 0) || (res == false)) {
699  return false;
700  } else {
701  *pszTargetFound = abtRx[0];
702  if (*pszTargetFound) {
703  uint8_t ln;
704  byte_t *pbt = abtRx + 1;
705  /* 1st target */
706  // Target type
707  pn53x_target_type_t ptt = *(pbt++);
708  pntTargets[0].nm = pn53x_ptt_to_nm(ptt);
709  // AutoPollTargetData length
710  ln = *(pbt++);
711  pn53x_decode_target_data (pbt, ln, pnd->nc, pntTargets[0].nm.nmt, &(pntTargets[0].nti));
712  pbt += ln;
713 
714  if (abtRx[0] > 1) {
715  /* 2nd target */
716  // Target type
717  ptt = *(pbt++);
718  pntTargets[1].nm = pn53x_ptt_to_nm(ptt);
719  // AutoPollTargetData length
720  ln = *(pbt++);
721  pn53x_decode_target_data (pbt, ln, pnd->nc, pntTargets[1].nm.nmt, &(pntTargets[1].nti));
722  }
723  }
724  }
725  return true;
726 }
727 
728 static struct sErrorMessage {
729  int iErrorCode;
730  const char *pcErrorMsg;
731 } sErrorMessages[] = {
732  /* Chip-level errors */
733  { 0x00, "Success" },
734  { ETIMEOUT, "Timeout" }, // Time Out, the target has not answered
735  { ECRC, "CRC Error" }, // A CRC error has been detected by the CIU
736  { EPARITY, "Parity Error" }, // A Parity error has been detected by the CIU
737  { EBITCOUNT, "Erroneous Bit Count" }, // During an anti-collision/select operation (ISO/IEC14443-3 Type A and ISO/IEC18092 106 kbps passive mode), an erroneous Bit Count has been detected
738  { EFRAMING, "Framing Error" }, // Framing error during MIFARE operation
739  { EBITCOLL, "Bit-collision" }, // An abnormal bit-collision has been detected during bit wise anti-collision at 106 kbps
740  { ESMALLBUF, "Communication Buffer Too Small" }, // Communication buffer size insufficient
741  { EBUFOVF, "Buffer Overflow" }, // RF Buffer overflow has been detected by the CIU (bit BufferOvfl of the register CIU_Error)
742  { ERFTIMEOUT, "RF Timeout" }, // In active communication mode, the RF field has not been switched on in time by the counterpart (as defined in NFCIP-1 standard)
743  { ERFPROTO, "RF Protocol Error" }, // RF Protocol error (see PN53x manual)
744  { EOVHEAT, "Chip Overheating" }, // Temperature error: the internal temperature sensor has detected overheating, and therefore has automatically switched off the antenna drivers
745  { EINBUFOVF, "Internal Buffer overflow."}, // Internal buffer overflow
746  { EINVPARAM, "Invalid Parameter"}, // Invalid parameter (range, format, …)
747  /* DEP Errors */
748  { EDEPUNKCMD, "Unknown DEP Command" },
749  /* MIFARE */
750  { EMFAUTH, "Mifare Authentication Error" },
751  /* */
752  { EINVRXFRAM, "Invalid Received Frame" }, // DEP Protocol, Mifare or ISO/IEC14443-4: The data format does not match to the specification.
753  { ENSECNOTSUPP, "NFC Secure not supported" }, // Target or Initiator does not support NFC Secure
754  { EBCC, "Wrong UID Check Byte (BCC)" }, // ISO/IEC14443-3: UID Check byte is wrong
755  { EDEPINVSTATE, "Invalid DEP State" }, // DEP Protocol: Invalid device state, the system is in a state which does not allow the operation
756  { EOPNOTALL, "Operation Not Allowed" }, // Operation not allowed in this configuration (host controller interface)
757  { ECMD, "Command Not Acceptable" }, // Command is not acceptable due to the current context
758  { ETGREL, "Target Released" }, // Target have been released by initiator
759  // FIXME: Errors can be grouped (DEP-related, MIFARE-related, ISO14443B-related, etc.)
760  // Purposal: Use prefix/suffix to identify them
761  { ECID, "Card ID Mismatch" }, // ISO14443 type B: Card ID mismatch, meaning that the expected card has been exchanged with another one.
762  { ECDISCARDED, "Card Discarded" }, // ISO/IEC14443 type B: the card previously activated has disappeared.
763  { ENFCID3, "NFCID3 Mismatch" },
764  { EOVCURRENT, "Over Current" },
765  { ENAD, "NAD Missing in DEP Frame" },
766  /* Software level errors */
767  { ETGUIDNOTSUP, "Target UID not supported" }, // In target mode, PN53x only support 4 bytes UID and the first byte must start with 0x08
768  /* Driver-level errors */
769  { DENACK, "Received NACK" },
770  { DEACKMISMATCH, "Expected ACK/NACK" },
771  { DEISERRFRAME, "Received an error frame" },
772  // TODO: Move me in more generic code for libnfc 1.6
773  // FIXME: Driver-errors and Device-errors have the same prefix (DE*)
774  // eg. DENACK means Driver Error NACK while DEIO means Device Error I/O
775  { DEINVAL, "Invalid argument" },
776  { DEIO, "Input/output error" },
777  { DETIMEOUT, "Operation timed-out" },
778  { DENOTSUP, "Operation not supported" }
779 };
780 
781 const char *
782 pn53x_strerror (const nfc_device_t * pnd)
783 {
784  const char *pcRes = "Unknown error";
785  size_t i;
786 
787  for (i = 0; i < (sizeof (sErrorMessages) / sizeof (struct sErrorMessage)); i++) {
788  if (sErrorMessages[i].iErrorCode == pnd->iLastError) {
789  pcRes = sErrorMessages[i].pcErrorMsg;
790  break;
791  }
792  }
793 
794  return pcRes;
795 }
796 
797 bool
798 pn53x_get_firmware_version (nfc_device_t * pnd, char abtFirmwareText[18])
799 {
800  byte_t abtFw[4];
801  size_t szFwLen = sizeof (abtFw);
802  if (!pn53x_transceive (pnd, pncmd_get_firmware_version, 2, abtFw, &szFwLen)) {
803  // Failed to get firmware revision??, whatever...let's disconnect and clean up and return err
804  pnd->pdc->disconnect (pnd);
805  return false;
806  }
807  // Convert firmware info in text, PN531 gives 2 bytes info, but PN532 and PN533 gives 4
808  switch (pnd->nc) {
809  case NC_PN531:
810  snprintf (abtFirmwareText, 18, "PN531 v%d.%d", abtFw[0], abtFw[1]);
811  pnd->btSupportByte = SUPPORT_ISO14443A | SUPPORT_ISO18092;
812  break;
813  case NC_PN532:
814  snprintf (abtFirmwareText, 18, "PN532 v%d.%d (0x%02x)", abtFw[1], abtFw[2], abtFw[3]);
815  pnd->btSupportByte = abtFw[3];
816  break;
817  case NC_PN533:
818  snprintf (abtFirmwareText, 18, "PN533 v%d.%d (0x%02x)", abtFw[1], abtFw[2], abtFw[3]);
819  pnd->btSupportByte = abtFw[3];
820  break;
821  }
822  // Be sure to have a null end of string
823  abtFirmwareText[17] = '\0';
824  return true;
825 }
826 
827 bool
828 pn53x_configure (nfc_device_t * pnd, const nfc_device_option_t ndo, const bool bEnable)
829 {
830  byte_t btValue;
831  byte_t abtCmd[sizeof (pncmd_rf_configure)];
832 
833  memcpy (abtCmd, pncmd_rf_configure, sizeof (pncmd_rf_configure));
834 
835  // Make sure we are dealing with a active device
836  if (!pnd->bActive)
837  return false;
838 
839  switch (ndo) {
840  case NDO_HANDLE_CRC:
841  // Enable or disable automatic receiving/sending of CRC bytes
842  // TX and RX are both represented by the symbol 0x80
843  btValue = (bEnable) ? 0x80 : 0x00;
844  if (!pn53x_set_reg (pnd, REG_CIU_TX_MODE, SYMBOL_TX_CRC_ENABLE, btValue))
845  return false;
846  if (!pn53x_set_reg (pnd, REG_CIU_RX_MODE, SYMBOL_RX_CRC_ENABLE, btValue))
847  return false;
848  pnd->bCrc = bEnable;
849  break;
850 
851  case NDO_HANDLE_PARITY:
852  // Handle parity bit by PN53X chip or parse it as data bit
853  btValue = (bEnable) ? 0x00 : SYMBOL_PARITY_DISABLE;
854  if (!pn53x_set_reg (pnd, REG_CIU_MANUAL_RCV, SYMBOL_PARITY_DISABLE, btValue))
855  return false;
856  pnd->bPar = bEnable;
857  break;
858 
859  case NDO_EASY_FRAMING:
860  pnd->bEasyFraming = bEnable;
861  break;
862 
863  case NDO_ACTIVATE_FIELD:
864  abtCmd[2] = RFCI_FIELD;
865  abtCmd[3] = (bEnable) ? 1 : 0;
866  if (!pn53x_transceive (pnd, abtCmd, 4, NULL, NULL))
867  return false;
868  break;
869 
871  btValue = (bEnable) ? SYMBOL_MF_CRYPTO1_ON : 0x00;
872  if (!pn53x_set_reg (pnd, REG_CIU_STATUS2, SYMBOL_MF_CRYPTO1_ON, btValue))
873  return false;
874  break;
875 
876  case NDO_INFINITE_SELECT:
877  // TODO Made some research around this point:
878  // timings could be tweak better than this, and maybe we can tweak timings
879  // to "gain" a sort-of hardware polling (ie. like PN532 does)
880 
881  // Retry format: 0x00 means only 1 try, 0xff means infinite
882  abtCmd[2] = RFCI_RETRY_SELECT;
883  abtCmd[3] = (bEnable) ? 0xff : 0x00; // MxRtyATR, default: active = 0xff, passive = 0x02
884  abtCmd[4] = (bEnable) ? 0xff : 0x00; // MxRtyPSL, default: 0x01
885  abtCmd[5] = (bEnable) ? 0xff : 0x00; // MxRtyPassiveActivation, default: 0xff
886  if (!pn53x_transceive (pnd, abtCmd, 6, NULL, NULL))
887  return false;
888  break;
889 
891  btValue = (bEnable) ? SYMBOL_RX_NO_ERROR : 0x00;
892  if (!pn53x_set_reg (pnd, REG_CIU_RX_MODE, SYMBOL_RX_NO_ERROR, btValue))
893  return false;
894  break;
895 
897  btValue = (bEnable) ? SYMBOL_RX_MULTIPLE : 0x00;
898  if (!pn53x_set_reg (pnd, REG_CIU_RX_MODE, SYMBOL_RX_MULTIPLE, btValue))
899  return false;
900  return true;
901  break;
902 
903  case NDO_AUTO_ISO14443_4:
904  // TODO Cache activated/disactivated options
905  pnd->bAutoIso14443_4 = bEnable;
906  return pn53x_set_parameter(pnd, PARAM_AUTO_RATS, bEnable);
907  break;
908 
910  if(!bEnable) {
911  // Nothing to do
912  return true;
913  }
914  // Force pn53x to be in ISO14443-A mode
915  if (!pn53x_set_reg (pnd, REG_CIU_TX_MODE, SYMBOL_TX_FRAMING, 0x00)) {
916  return false;
917  }
918  if (!pn53x_set_reg (pnd, REG_CIU_RX_MODE, SYMBOL_RX_FRAMING, 0x00)) {
919  return false;
920  }
921  return true;
922  break;
923  }
924 
925  // When we reach this, the configuration is completed and succesful
926  return true;
927 }
928 
929 bool
930 pn53x_initiator_select_dep_target(nfc_device_t * pnd,
931  const nfc_dep_mode_t ndm, const nfc_baud_rate_t nbr,
932  const nfc_dep_info_t * pndiInitiator,
933  nfc_target_t * pnt)
934 {
935  const byte_t abtPassiveInitiatorData[5] = { 0x00, 0xff, 0xff, 0x00, 0x00 }; // Only for 212/424 kpbs: First 4 bytes shall be set like this according to NFCIP-1, last byte is TSN (Time Slot Number)
936  const byte_t * pbtPassiveInitiatorData = NULL;
937 
938  switch (nbr) {
939  case NBR_212:
940  case NBR_424:
941  // Only use this predefined bytes array when we are at 212/424kbps
942  pbtPassiveInitiatorData = abtPassiveInitiatorData;
943  break;
944 
945  default:
946  // Nothing to do
947  break;
948  }
949 
950  if (pndiInitiator) {
951  return pn53x_InJumpForDEP (pnd, ndm, nbr, pbtPassiveInitiatorData, pndiInitiator->abtNFCID3, pndiInitiator->abtGB, pndiInitiator->szGB, pnt);
952  } else {
953  return pn53x_InJumpForDEP (pnd, ndm, nbr, pbtPassiveInitiatorData, NULL, NULL, 0, pnt);
954  }
955 }
956 
967 bool
968 pn53x_InJumpForDEP (nfc_device_t * pnd,
969  const nfc_dep_mode_t ndm,
970  const nfc_baud_rate_t nbr,
971  const byte_t * pbtPassiveInitiatorData,
972  const byte_t * pbtNFCID3i,
973  const byte_t * pbtGBi, const size_t szGBi,
974  nfc_target_t * pnt)
975 {
976  byte_t abtRx[PN53x_EXTENDED_FRAME_MAX_LEN];
977  size_t szRx;
978  size_t offset;
979  byte_t abtCmd[sizeof (pncmd_initiator_jump_for_dep)];
980 
981  memcpy (abtCmd, pncmd_initiator_jump_for_dep, sizeof (pncmd_initiator_jump_for_dep));
982 
983  offset = 5; // 2 bytes for command, 1 byte for DEP mode (Active/Passive), 1 byte for baud rate, 1 byte for following parameters flag
984  abtCmd[2] = (ndm == NDM_ACTIVE) ? 0x01 : 0x00;
985 
986  switch (nbr) {
987  case NBR_106:
988  abtCmd[3] = 0x00; // baud rate is 106 kbps
989  break;
990  case NBR_212:
991  abtCmd[3] = 0x01; // baud rate is 212 kbps
992  break;
993  case NBR_424:
994  abtCmd[3] = 0x02; // baud rate is 424 kbps
995  break;
996  case NBR_847:
997  case NBR_UNDEFINED:
998  // XXX Maybe we should put a "syntax error" or sth like that
999  pnd->iLastError = DENOTSUP;
1000  return false;
1001  break;
1002  }
1003 
1004  if (pbtPassiveInitiatorData && (ndm == NDM_PASSIVE)) { /* can't have passive initiator data when using active mode */
1005  switch (nbr) {
1006  case NBR_106:
1007  abtCmd[4] |= 0x01;
1008  memcpy (abtCmd + offset, pbtPassiveInitiatorData, 4);
1009  offset += 4;
1010  break;
1011  case NBR_212:
1012  case NBR_424:
1013  abtCmd[4] |= 0x01;
1014  memcpy (abtCmd + offset, pbtPassiveInitiatorData, 5);
1015  offset += 5;
1016  break;
1017  case NBR_847:
1018  case NBR_UNDEFINED:
1019  // XXX Maybe we should put a "syntax error" or sth like that
1020  pnd->iLastError = DENOTSUP;
1021  return false;
1022  break;
1023  }
1024  }
1025 
1026  if (pbtNFCID3i) {
1027  abtCmd[4] |= 0x02;
1028  memcpy (abtCmd + offset, pbtNFCID3i, 10);
1029  offset += 10;
1030  }
1031 
1032  if (szGBi && pbtGBi) {
1033  abtCmd[4] |= 0x04;
1034  memcpy (abtCmd + offset, pbtGBi, szGBi);
1035  offset += szGBi;
1036  }
1037  // Try to find a target, call the transceive callback function of the current device
1038  if (!pn53x_transceive (pnd, abtCmd, offset, abtRx, &szRx))
1039  return false;
1040 
1041  // Make sure one target has been found, the PN53X returns 0x00 if none was available
1042  if (abtRx[1] != 1)
1043  return false;
1044 
1045  // Is a target struct available
1046  if (pnt) {
1047  pnt->nm.nmt = NMT_DEP;
1048  pnt->nm.nbr = nbr;
1049  memcpy (pnt->nti.ndi.abtNFCID3, abtRx + 2, 10);
1050  pnt->nti.ndi.btDID = abtRx[12];
1051  pnt->nti.ndi.btBS = abtRx[13];
1052  pnt->nti.ndi.btBR = abtRx[14];
1053  pnt->nti.ndi.btTO = abtRx[15];
1054  pnt->nti.ndi.btPP = abtRx[16];
1055  if(szRx > 17) {
1056  pnt->nti.ndi.szGB = szRx - 17;
1057  memcpy (pnt->nti.ndi.abtGB, abtRx + 17, pnt->nti.ndi.szGB);
1058  } else {
1059  pnt->nti.ndi.szGB = 0;
1060  }
1061  }
1062  return true;
1063 }
1064 
1065 bool
1066 pn53x_initiator_transceive_bits (nfc_device_t * pnd, const byte_t * pbtTx, const size_t szTxBits,
1067  const byte_t * pbtTxPar, byte_t * pbtRx, size_t * pszRxBits, byte_t * pbtRxPar)
1068 {
1069  byte_t abtRx[PN53x_EXTENDED_FRAME_MAX_LEN];
1070  size_t szRx = PN53x_EXTENDED_FRAME_MAX_LEN;
1071  size_t szFrameBits = 0;
1072  size_t szFrameBytes = 0;
1073  uint8_t ui8rcc;
1074  uint8_t ui8Bits = 0;
1075  byte_t abtCmd[sizeof (pncmd_initiator_exchange_raw_data)];
1076 
1077  memcpy (abtCmd, pncmd_initiator_exchange_raw_data, sizeof (pncmd_initiator_exchange_raw_data));
1078 
1079  // Check if we should prepare the parity bits ourself
1080  if (!pnd->bPar) {
1081  // Convert data with parity to a frame
1082  pn53x_wrap_frame (pbtTx, szTxBits, pbtTxPar, abtCmd + 2, &szFrameBits);
1083  } else {
1084  szFrameBits = szTxBits;
1085  }
1086 
1087  // Retrieve the leading bits
1088  ui8Bits = szFrameBits % 8;
1089 
1090  // Get the amount of frame bytes + optional (1 byte if there are leading bits)
1091  szFrameBytes = (szFrameBits / 8) + ((ui8Bits == 0) ? 0 : 1);
1092 
1093  // When the parity is handled before us, we just copy the data
1094  if (pnd->bPar)
1095  memcpy (abtCmd + 2, pbtTx, szFrameBytes);
1096 
1097  // Set the amount of transmission bits in the PN53X chip register
1098  if (!pn53x_set_tx_bits (pnd, ui8Bits))
1099  return false;
1100 
1101  // Send the frame to the PN53X chip and get the answer
1102  // We have to give the amount of bytes + (the two command bytes 0xD4, 0x42)
1103  if (!pn53x_transceive (pnd, abtCmd, szFrameBytes + 2, abtRx, &szRx))
1104  return false;
1105 
1106  // Get the last bit-count that is stored in the received byte
1107  if (!pn53x_get_reg (pnd, REG_CIU_CONTROL, &ui8rcc))
1108  return false;
1109  ui8Bits = ui8rcc & SYMBOL_RX_LAST_BITS;
1110 
1111  // Recover the real frame length in bits
1112  szFrameBits = ((szRx - 1 - ((ui8Bits == 0) ? 0 : 1)) * 8) + ui8Bits;
1113 
1114  // Ignore the status byte from the PN53X here, it was checked earlier in pn53x_transceive()
1115  // Check if we should recover the parity bits ourself
1116  if (!pnd->bPar) {
1117  // Unwrap the response frame
1118  pn53x_unwrap_frame (abtRx + 1, szFrameBits, pbtRx, pszRxBits, pbtRxPar);
1119  } else {
1120  // Save the received bits
1121  *pszRxBits = szFrameBits;
1122  // Copy the received bytes
1123  memcpy (pbtRx, abtRx + 1, szRx - 1);
1124  }
1125 
1126  // Everything went successful
1127  return true;
1128 }
1129 
1130 bool
1131 pn53x_initiator_transceive_bytes (nfc_device_t * pnd, const byte_t * pbtTx, const size_t szTx, byte_t * pbtRx,
1132  size_t * pszRx)
1133 {
1134  byte_t abtRx[PN53x_EXTENDED_FRAME_MAX_LEN];
1135  size_t szRx = PN53x_EXTENDED_FRAME_MAX_LEN;
1136  size_t szExtraTxLen;
1137  byte_t abtCmd[sizeof (pncmd_initiator_exchange_raw_data)];
1138 
1139  // We can not just send bytes without parity if while the PN53X expects we handled them
1140  if (!pnd->bPar)
1141  return false;
1142 
1143  // Copy the data into the command frame
1144  if (pnd->bEasyFraming) {
1145  memcpy (abtCmd, pncmd_initiator_exchange_data, sizeof (pncmd_initiator_exchange_data));
1146  abtCmd[2] = 1; /* target number */
1147  memcpy (abtCmd + 3, pbtTx, szTx);
1148  szExtraTxLen = 3;
1149  } else {
1150  memcpy (abtCmd, pncmd_initiator_exchange_raw_data, sizeof (pncmd_initiator_exchange_raw_data));
1151  memcpy (abtCmd + 2, pbtTx, szTx);
1152  szExtraTxLen = 2;
1153  }
1154 
1155  // To transfer command frames bytes we can not have any leading bits, reset this to zero
1156  if (!pn53x_set_tx_bits (pnd, 0))
1157  return false;
1158 
1159  // Send the frame to the PN53X chip and get the answer
1160  // We have to give the amount of bytes + (the two command bytes 0xD4, 0x42)
1161  if (!pn53x_transceive (pnd, abtCmd, szTx + szExtraTxLen, abtRx, &szRx))
1162  return false;
1163 
1164  // Save the received byte count
1165  *pszRx = szRx - 1;
1166 
1167  // Copy the received bytes
1168  memcpy (pbtRx, abtRx + 1, *pszRx);
1169 
1170  // Everything went successful
1171  return true;
1172 }
1173 
1174 #define SAK_ISO14443_4_COMPLIANT 0x20
1175 bool
1176 pn53x_target_init (nfc_device_t * pnd, nfc_target_t * pnt, byte_t * pbtRx, size_t * pszRx)
1177 {
1178  // Save the current configuration settings
1179  bool bCrc = pnd->bCrc;
1180  bool bPar = pnd->bPar;
1181 
1183  switch (pnt->nm.nmt) {
1184  case NMT_ISO14443A:
1185  ptm = PTM_PASSIVE_ONLY;
1186  if ((pnt->nti.nai.abtUid[0] != 0x08) || (pnt->nti.nai.szUidLen != 4)) {
1187  pnd->iLastError = ETGUIDNOTSUP;
1188  return false;
1189  }
1190  pn53x_set_parameter(pnd, PARAM_AUTO_ATR_RES, false);
1191  if (pnd->nc == NC_PN532) { // We have a PN532
1192  if ((pnt->nti.nai.btSak & SAK_ISO14443_4_COMPLIANT) && (pnd->bAutoIso14443_4)) {
1193  // We have a ISO14443-4 tag to emulate and NDO_AUTO_14443_4A option is enabled
1194  ptm |= PTM_ISO14443_4_PICC_ONLY; // We add ISO14443-4 restriction
1195  pn53x_set_parameter(pnd, PARAM_14443_4_PICC, true);
1196  } else {
1197  pn53x_set_parameter(pnd, PARAM_14443_4_PICC, false);
1198  }
1199  }
1200  break;
1201  case NMT_FELICA:
1202  ptm = PTM_PASSIVE_ONLY;
1203  break;
1204  case NMT_DEP:
1205  pn53x_set_parameter(pnd, PARAM_AUTO_ATR_RES, true);
1206  ptm = PTM_DEP_ONLY;
1207  if (pnt->nti.ndi.ndm == NDM_PASSIVE) {
1208  ptm |= PTM_PASSIVE_ONLY; // We add passive mode restriction
1209  }
1210  break;
1211  case NMT_ISO14443B:
1212  case NMT_JEWEL:
1213  pnd->iLastError = DENOTSUP;
1214  return false;
1215  break;
1216  }
1217 
1218  // Make sure the CRC & parity are handled by the device, this is needed for target_init to work properly
1219  if (!bCrc)
1220  pn53x_configure ((nfc_device_t *) pnd, NDO_HANDLE_CRC, true);
1221  if (!bPar)
1222  pn53x_configure ((nfc_device_t *) pnd, NDO_HANDLE_PARITY, true);
1223 
1224  // Let the PN53X be activated by the RF level detector from power down mode
1225  if (!pn53x_set_reg (pnd, REG_CIU_TX_AUTO, SYMBOL_INITIAL_RF_ON, 0x04))
1226  return false;
1227 
1228  byte_t abtMifareParams[6];
1229  byte_t * pbtMifareParams = NULL;
1230  byte_t * pbtTkt = NULL;
1231  size_t szTkt = 0;
1232 
1233  byte_t abtFeliCaParams[18];
1234  byte_t * pbtFeliCaParams = NULL;
1235 
1236  const byte_t * pbtNFCID3t = NULL;
1237  const byte_t * pbtGBt = NULL;
1238  size_t szGBt = 0;
1239 
1240  switch(pnt->nm.nmt) {
1241  case NMT_ISO14443A: {
1242  // Set ATQA (SENS_RES)
1243  abtMifareParams[0] = pnt->nti.nai.abtAtqa[1];
1244  abtMifareParams[1] = pnt->nti.nai.abtAtqa[0];
1245  // Set UID
1246  // Note: in this mode we can only emulate a single size (4 bytes) UID where the first is hard-wired by PN53x as 0x08
1247  abtMifareParams[2] = pnt->nti.nai.abtUid[1];
1248  abtMifareParams[3] = pnt->nti.nai.abtUid[2];
1249  abtMifareParams[4] = pnt->nti.nai.abtUid[3];
1250  // Set SAK (SEL_RES)
1251  abtMifareParams[5] = pnt->nti.nai.btSak;
1252 
1253  pbtMifareParams = abtMifareParams;
1254 
1255  // Historical Bytes
1256  pbtTkt = iso14443a_locate_historical_bytes (pnt->nti.nai.abtAts, pnt->nti.nai.szAtsLen, &szTkt);
1257  }
1258  break;
1259 
1260  case NMT_FELICA:
1261  // Set NFCID2t
1262  memcpy(abtFeliCaParams, pnt->nti.nfi.abtId, 8);
1263  // Set PAD
1264  memcpy(abtFeliCaParams+8, pnt->nti.nfi.abtPad, 8);
1265  // Set SystemCode
1266  memcpy(abtFeliCaParams+16, pnt->nti.nfi.abtSysCode, 2);
1267  pbtFeliCaParams = abtFeliCaParams;
1268  break;
1269 
1270  case NMT_DEP:
1271  // Set NFCID3
1272  pbtNFCID3t = pnt->nti.ndi.abtNFCID3;
1273  // Set General Bytes, if relevant
1274  szGBt = pnt->nti.ndi.szGB;
1275  if (szGBt) pbtGBt = pnt->nti.ndi.abtGB;
1276  break;
1277  case NMT_ISO14443B:
1278  case NMT_JEWEL:
1279  pnd->iLastError = DENOTSUP;
1280  return false;
1281  break;
1282  }
1283 
1284  bool targetActivated = false;
1285  while (!targetActivated) {
1286  nfc_modulation_t nm;
1287  nfc_dep_mode_t ndm = NDM_UNDEFINED;
1288  byte_t btActivatedMode;
1289 
1290  nm.nbr = NBR_UNDEFINED;
1291 
1292  if(!pn53x_TgInitAsTarget(pnd, ptm, pbtMifareParams, pbtTkt, szTkt, pbtFeliCaParams, pbtNFCID3t, pbtGBt, szGBt, pbtRx, pszRx, &btActivatedMode)) {
1293  return false;
1294  }
1295 
1296  // Decode activated "mode"
1297  switch(btActivatedMode & 0x70) { // Baud rate
1298  case 0x00: // 106kbps
1299  nm.nbr = NBR_106;
1300  break;
1301  case 0x10: // 212kbps
1302  nm.nbr = NBR_212;
1303  break;
1304  case 0x20: // 424kbps
1305  nm.nbr = NBR_424;
1306  break;
1307  };
1308 
1309  if (btActivatedMode & 0x04) { // D.E.P.
1310  nm.nmt = NMT_DEP;
1311  if ((btActivatedMode & 0x03) == 0x01) { // Active mode
1312  ndm = NDM_ACTIVE;
1313  } else { // Passive mode
1314  ndm = NDM_PASSIVE;
1315  }
1316  } else { // Not D.E.P.
1317  if ((btActivatedMode & 0x03) == 0x00) { // MIFARE
1318  nm.nmt = NMT_ISO14443A;
1319  } else if ((btActivatedMode & 0x03) == 0x02) { // FeliCa
1320  nm.nmt = NMT_FELICA;
1321  }
1322  }
1323 
1324  if(pnt->nm.nmt == nm.nmt) { // Actual activation have the right modulation type
1325  if ((pnt->nm.nbr == NBR_UNDEFINED) || (pnt->nm.nbr == nm.nbr)) { // Have the right baud rate (or undefined)
1326  if ((pnt->nm.nmt != NMT_DEP) || (pnt->nti.ndi.ndm == NDM_UNDEFINED) || (pnt->nti.ndi.ndm == ndm)) { // Have the right DEP mode (or is not a DEP)
1327  targetActivated = true;
1328  }
1329  }
1330  }
1331 
1332  if (targetActivated) {
1333  pnt->nm.nbr = nm.nbr; // Update baud rate
1334  if (pnt->nm.nmt == NMT_DEP) {
1335  pnt->nti.ndi.ndm = ndm; // Update DEP mode
1336  }
1337  }
1338  }
1339 
1340  // Restore the CRC & parity setting to the original value (if needed)
1341  if (!bCrc)
1342  pn53x_configure ((nfc_device_t *) pnd, NDO_HANDLE_CRC, false);
1343  if (!bPar)
1344  pn53x_configure ((nfc_device_t *) pnd, NDO_HANDLE_PARITY, false);
1345 
1346  return true;
1347 }
1348 
1349 bool
1350 pn53x_TgInitAsTarget (nfc_device_t * pnd, pn53x_target_mode_t ptm,
1351  const byte_t * pbtMifareParams,
1352  const byte_t * pbtTkt, size_t szTkt,
1353  const byte_t * pbtFeliCaParams,
1354  const byte_t * pbtNFCID3t, const byte_t * pbtGBt, const size_t szGBt,
1355  byte_t * pbtRx, size_t * pszRx, byte_t * pbtModeByte)
1356 {
1357  byte_t abtRx[PN53x_EXTENDED_FRAME_MAX_LEN];
1358  size_t szRx;
1359  byte_t abtCmd[39 + 47 + 48]; // Worst case: 39-byte base, 47 bytes max. for General Bytes, 48 bytes max. for Historical Bytes
1360  size_t szOptionalBytes = 0;
1361 
1362  memcpy (abtCmd, pncmd_target_init, sizeof (pncmd_target_init));
1363 
1364  // Clear the target init struct, reset to all zeros
1365  memset (abtCmd + sizeof (pncmd_target_init), 0x00, sizeof (abtCmd) - sizeof (pncmd_target_init));
1366 
1367  // Store the target mode in the initialization params
1368  abtCmd[2] = ptm;
1369 
1370  // MIFARE part
1371  if (pbtMifareParams) {
1372  memcpy (abtCmd+3, pbtMifareParams, 6);
1373  }
1374  // FeliCa part
1375  if (pbtFeliCaParams) {
1376  memcpy (abtCmd+9, pbtFeliCaParams, 18);
1377  }
1378  // DEP part
1379  if (pbtNFCID3t) {
1380  memcpy(abtCmd+27, pbtNFCID3t, 10);
1381  }
1382  // General Bytes (ISO/IEC 18092)
1383  if (pnd->nc == NC_PN531) {
1384  if (szGBt) {
1385  memcpy (abtCmd+37, pbtGBt, szGBt);
1386  szOptionalBytes = szGBt;
1387  }
1388  } else {
1389  abtCmd[37] = (byte_t)(szGBt);
1390  if (szGBt) {
1391  memcpy (abtCmd+38, pbtGBt, szGBt);
1392  }
1393  szOptionalBytes = szGBt + 1;
1394  }
1395  // Historical bytes (ISO/IEC 14443-4)
1396  if (pnd->nc != NC_PN531) { // PN531 does not handle Historical Bytes
1397  abtCmd[37+szOptionalBytes] = (byte_t)(szTkt);
1398  if (szTkt) {
1399  memcpy (abtCmd+38+szOptionalBytes, pbtTkt, szTkt);
1400  }
1401  szOptionalBytes += szTkt + 1;
1402  }
1403 
1404  // Request the initialization as a target
1405  szRx = PN53x_EXTENDED_FRAME_MAX_LEN;
1406 
1407  if (!pn53x_transceive (pnd, abtCmd, 37 + szOptionalBytes, abtRx, &szRx))
1408  return false;
1409 
1410  // Note: the first byte is skip:
1411  // its the "mode" byte which contains baudrate, DEP and Framing type (Mifare, active or FeliCa) datas.
1412  if(pbtModeByte) {
1413  *pbtModeByte = abtRx[0];
1414  }
1415 
1416  // Save the received byte count
1417  *pszRx = szRx - 1;
1418  // Copy the received bytes
1419  memcpy (pbtRx, abtRx + 1, *pszRx);
1420 
1421  return true;
1422 }
1423 
1424 bool
1425 pn53x_target_receive_bits (nfc_device_t * pnd, byte_t * pbtRx, size_t * pszRxBits, byte_t * pbtRxPar)
1426 {
1427  byte_t abtRx[PN53x_EXTENDED_FRAME_MAX_LEN];
1428  size_t szRx;
1429  size_t szFrameBits;
1430  uint8_t ui8rcc;
1431  uint8_t ui8Bits;
1432 
1433  // Try to gather a received frame from the reader
1434  if (!pn53x_transceive (pnd, pncmd_target_get_initiator_command, 2, abtRx, &szRx))
1435  return false;
1436 
1437  // Get the last bit-count that is stored in the received byte
1438  if (!pn53x_get_reg (pnd, REG_CIU_CONTROL, &ui8rcc))
1439  return false;
1440  ui8Bits = ui8rcc & SYMBOL_RX_LAST_BITS;
1441 
1442  // Recover the real frame length in bits
1443  szFrameBits = ((szRx - 1 - ((ui8Bits == 0) ? 0 : 1)) * 8) + ui8Bits;
1444 
1445  // Ignore the status byte from the PN53X here, it was checked earlier in pn53x_transceive()
1446  // Check if we should recover the parity bits ourself
1447  if (!pnd->bPar) {
1448  // Unwrap the response frame
1449  pn53x_unwrap_frame (abtRx + 1, szFrameBits, pbtRx, pszRxBits, pbtRxPar);
1450  } else {
1451  // Save the received bits
1452  *pszRxBits = szFrameBits;
1453  // Copy the received bytes
1454  memcpy (pbtRx, abtRx + 1, szRx - 1);
1455  }
1456  // Everyting seems ok, return true
1457  return true;
1458 }
1459 
1460 bool
1461 pn53x_target_receive_bytes (nfc_device_t * pnd, byte_t * pbtRx, size_t * pszRx)
1462 {
1463  byte_t const *pbtTx;
1464  byte_t abtRx[PN53x_EXTENDED_FRAME_MAX_LEN];
1465  size_t szRx;
1466 
1467  if (pnd->bEasyFraming) {
1468  pbtTx = pncmd_target_get_data;
1469  } else {
1470  pbtTx = pncmd_target_get_initiator_command;
1471  }
1472 
1473  // Try to gather a received frame from the reader
1474  if (!pn53x_transceive (pnd, pbtTx, 2, abtRx, &szRx))
1475  return false;
1476 
1477  // Save the received byte count
1478  *pszRx = szRx - 1;
1479 
1480  // Copy the received bytes
1481  memcpy (pbtRx, abtRx + 1, *pszRx);
1482 
1483  // Everyting seems ok, return true
1484  return true;
1485 }
1486 
1487 bool
1488 pn53x_target_send_bits (nfc_device_t * pnd, const byte_t * pbtTx, const size_t szTxBits, const byte_t * pbtTxPar)
1489 {
1490  size_t szFrameBits = 0;
1491  size_t szFrameBytes = 0;
1492  uint8_t ui8Bits = 0;
1493  byte_t abtCmd[sizeof (pncmd_target_response_to_initiator)];
1494 
1495  memcpy (abtCmd, pncmd_target_response_to_initiator, sizeof (pncmd_target_response_to_initiator));
1496 
1497  // Check if we should prepare the parity bits ourself
1498  if (!pnd->bPar) {
1499  // Convert data with parity to a frame
1500  pn53x_wrap_frame (pbtTx, szTxBits, pbtTxPar, abtCmd + 2, &szFrameBits);
1501  } else {
1502  szFrameBits = szTxBits;
1503  }
1504 
1505  // Retrieve the leading bits
1506  ui8Bits = szFrameBits % 8;
1507 
1508  // Get the amount of frame bytes + optional (1 byte if there are leading bits)
1509  szFrameBytes = (szFrameBits / 8) + ((ui8Bits == 0) ? 0 : 1);
1510 
1511  // When the parity is handled before us, we just copy the data
1512  if (pnd->bPar)
1513  memcpy (abtCmd + 2, pbtTx, szFrameBytes);
1514 
1515  // Set the amount of transmission bits in the PN53X chip register
1516  if (!pn53x_set_tx_bits (pnd, ui8Bits))
1517  return false;
1518 
1519  // Try to send the bits to the reader
1520  if (!pn53x_transceive (pnd, abtCmd, szFrameBytes + 2, NULL, NULL))
1521  return false;
1522 
1523  // Everyting seems ok, return true
1524  return true;
1525 }
1526 
1527 bool
1528 pn53x_target_send_bytes (nfc_device_t * pnd, const byte_t * pbtTx, const size_t szTx)
1529 {
1530  byte_t abtCmd[MAX (sizeof (pncmd_target_response_to_initiator), sizeof (pncmd_target_set_data))];
1531 
1532 
1533  // We can not just send bytes without parity if while the PN53X expects we handled them
1534  if (!pnd->bPar)
1535  return false;
1536 
1537  if (pnd->bEasyFraming) {
1538  memcpy (abtCmd, pncmd_target_set_data, sizeof (pncmd_target_set_data));
1539  } else {
1540  memcpy (abtCmd, pncmd_target_response_to_initiator, sizeof (pncmd_target_response_to_initiator));
1541  }
1542 
1543  // Copy the data into the command frame
1544  memcpy (abtCmd + 2, pbtTx, szTx);
1545 
1546  // Try to send the bits to the reader
1547  if (!pn53x_transceive (pnd, abtCmd, szTx + 2, NULL, NULL))
1548  return false;
1549 
1550  // Everyting seems ok, return true
1551  return true;
1552 }
1553 
1554 const pn53x_modulation_t
1555 pn53x_nm_to_pm(const nfc_modulation_t nm)
1556 {
1557  switch(nm.nmt) {
1558  case NMT_ISO14443A:
1559  return PM_ISO14443A_106;
1560  break;
1561 
1562  case NMT_ISO14443B:
1563  switch(nm.nbr) {
1564  case NBR_106:
1565  return PM_ISO14443B_106;
1566  break;
1567  case NBR_212:
1568  return PM_ISO14443B_212;
1569  break;
1570  case NBR_424:
1571  return PM_ISO14443B_424;
1572  break;
1573  case NBR_847:
1574  return PM_ISO14443B_847;
1575  break;
1576  case NBR_UNDEFINED:
1577  // Nothing to do...
1578  break;
1579  }
1580  break;
1581 
1582  case NMT_JEWEL:
1583  return PM_JEWEL_106;
1584  break;
1585 
1586  case NMT_FELICA:
1587  switch(nm.nbr) {
1588  case NBR_212:
1589  return PM_FELICA_212;
1590  break;
1591  case NBR_424:
1592  return PM_FELICA_424;
1593  break;
1594  case NBR_106:
1595  case NBR_847:
1596  case NBR_UNDEFINED:
1597  // Nothing to do...
1598  break;
1599  }
1600  break;
1601  case NMT_DEP:
1602  // Nothing to do...
1603  break;
1604  }
1605  return PM_UNDEFINED;
1606 }
1607 
1608 const nfc_modulation_t
1609 pn53x_ptt_to_nm( const pn53x_target_type_t ptt )
1610 {
1611  switch (ptt) {
1615  case PTT_UNDEFINED:
1616  // XXX This should not happend, how handle it cleanly ?
1617  break;
1618 
1619  case PTT_MIFARE:
1620  case PTT_ISO14443_4A_106:
1621  return (const nfc_modulation_t){ .nmt = NMT_ISO14443A, .nbr = NBR_106 };
1622  break;
1623 
1624  case PTT_ISO14443_4B_106:
1626  return (const nfc_modulation_t){ .nmt = NMT_ISO14443B, .nbr = NBR_106 };
1627  break;
1628 
1629  case PTT_JEWEL_106:
1630  return (const nfc_modulation_t){ .nmt = NMT_JEWEL, .nbr = NBR_106 };
1631  break;
1632 
1633  case PTT_FELICA_212:
1634  return (const nfc_modulation_t){ .nmt = NMT_FELICA, .nbr = NBR_212 };
1635  break;
1636  case PTT_FELICA_424:
1637  return (const nfc_modulation_t){ .nmt = NMT_FELICA, .nbr = NBR_424 };
1638  break;
1639 
1640  case PTT_DEP_PASSIVE_106:
1641  case PTT_DEP_ACTIVE_106:
1642  return (const nfc_modulation_t){ .nmt = NMT_DEP, .nbr = NBR_106 };
1643  break;
1644  case PTT_DEP_PASSIVE_212:
1645  case PTT_DEP_ACTIVE_212:
1646  return (const nfc_modulation_t){ .nmt = NMT_DEP, .nbr = NBR_212 };
1647  break;
1648  case PTT_DEP_PASSIVE_424:
1649  case PTT_DEP_ACTIVE_424:
1650  return (const nfc_modulation_t){ .nmt = NMT_DEP, .nbr = NBR_424 };
1651  break;
1652  }
1653  // We should never be here, this line silent compilation warning
1654  return (const nfc_modulation_t){ .nmt = NMT_ISO14443A, .nbr = NBR_106 };
1655 }
1656 
1657 const pn53x_target_type_t
1658 pn53x_nm_to_ptt(const nfc_modulation_t nm)
1659 {
1660  switch(nm.nmt) {
1661  case NMT_ISO14443A:
1662  return PTT_MIFARE;
1663  // return PTT_ISO14443_4A_106;
1664  break;
1665 
1666  case NMT_ISO14443B:
1667  switch(nm.nbr) {
1668  case NBR_106:
1669  return PTT_ISO14443_4B_106;
1670  break;
1671  case NBR_UNDEFINED:
1672  case NBR_212:
1673  case NBR_424:
1674  case NBR_847:
1675  // Nothing to do...
1676  break;
1677  }
1678  break;
1679 
1680  case NMT_JEWEL:
1681  return PTT_JEWEL_106;
1682  break;
1683 
1684  case NMT_FELICA:
1685  switch(nm.nbr) {
1686  case NBR_212:
1687  return PTT_FELICA_212;
1688  break;
1689  case NBR_424:
1690  return PTT_FELICA_424;
1691  break;
1692  case NBR_UNDEFINED:
1693  case NBR_106:
1694  case NBR_847:
1695  // Nothing to do...
1696  break;
1697  }
1698  break;
1699 
1700  case NMT_DEP:
1701  // Nothing to do...
1702  break;
1703  }
1704  return PTT_UNDEFINED;
1705 }
1706