chip/pn53x: clean up SAMConfiguration usage.

This commit is contained in:
Romuald Conty 2011-06-27 17:12:25 +00:00
parent eb70f3842e
commit e6051ceca4
4 changed files with 48 additions and 82 deletions

View file

@ -30,6 +30,8 @@
/** /**
* @file pn53x-sam.c * @file pn53x-sam.c
* @brief Configures the NFC device to communicate with a SAM (Secure Access Module). * @brief Configures the NFC device to communicate with a SAM (Secure Access Module).
* @note This example requiers a PN532 with SAM connected using S2C interface
* @see PN532 User manual
*/ */
#ifdef HAVE_CONFIG_H #ifdef HAVE_CONFIG_H
@ -39,18 +41,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <time.h> #include <time.h>
#include <unistd.h>
#ifndef _WIN32
// Needed by sleep() under Unix
# include <unistd.h>
# define sleep sleep
# define SUSP_TIME 1 // secs.
#else
// Needed by Sleep() under Windows
# include <winbase.h>
# define sleep Sleep
# define SUSP_TIME 1000 // msecs.
#endif
#include <nfc/nfc.h> #include <nfc/nfc.h>
@ -60,46 +51,6 @@
#define MAX_FRAME_LEN 264 #define MAX_FRAME_LEN 264
#define TIMEOUT 60 // secs. #define TIMEOUT 60 // secs.
#define NORMAL_MODE 1
#define VIRTUAL_CARD_MODE 2
#define WIRED_CARD_MODE 3
#define DUAL_CARD_MODE 4
bool
sam_connection (nfc_device_t * pnd, int mode)
{
byte_t pncmd_sam_config[] = { 0x14, 0x00, 0x00 };
size_t szCmd = 0;
byte_t abtRx[MAX_FRAME_LEN];
size_t szRx = sizeof(abtRx);
pncmd_sam_config[1] = mode;
switch (mode) {
case VIRTUAL_CARD_MODE:
{
// Only the VIRTUAL_CARD_MODE requires 3 bytes.
szCmd = sizeof (pncmd_sam_config);
}
break;
default:
{
szCmd = sizeof (pncmd_sam_config) - 1;
}
break;
}
if (!pn53x_transceive (pnd, pncmd_sam_config, szCmd, abtRx, &szRx)) {
nfc_perror(pnd, "pn53x_transceive");
ERR ("%s %d", "Unable to execute SAMConfiguration command with mode byte:", mode);
return false;
}
return true;
}
void void
wait_one_minute () wait_one_minute ()
{ {
@ -109,7 +60,7 @@ wait_one_minute ()
fflush (stdout); fflush (stdout);
while (secs < TIMEOUT) { while (secs < TIMEOUT) {
sleep (SUSP_TIME); sleep (1);
secs++; secs++;
printf ("."); printf (".");
fflush (stdout); fflush (stdout);
@ -149,25 +100,29 @@ main (int argc, const char *argv[])
// Take user's choice // Take user's choice
char input = getchar (); char input = getchar ();
int mode = input - '0' + 1; int iMode = input - '0' + 1;
printf ("\n"); printf ("\n");
if (mode < VIRTUAL_CARD_MODE || mode > DUAL_CARD_MODE) { if (iMode < 1 || iMode > 3) {
ERR ("%s", "Invalid selection."); ERR ("%s", "Invalid selection.");
return EXIT_FAILURE; exit (EXIT_FAILURE);
} }
pn532_sam_mode mode = iMode;
// Connect with the SAM // Connect with the SAM
sam_connection (pnd, mode); if (!pn53x_SAMConfiguration (pnd, mode)) {
nfc_perror (pnd, "pn53x_SAMConfiguration");
exit (EXIT_FAILURE);
}
switch (mode) { switch (mode) {
case VIRTUAL_CARD_MODE: case PSM_VIRTUAL_CARD:
{ {
// FIXME after the loop the device doesn't respond to host commands...
printf ("Now the SAM is readable for 1 minute from an external reader.\n"); printf ("Now the SAM is readable for 1 minute from an external reader.\n");
wait_one_minute (); wait_one_minute ();
} }
break; break;
case WIRED_CARD_MODE: case PSM_WIRED_CARD:
{ {
nfc_target_t nt; nfc_target_t nt;
@ -187,15 +142,15 @@ main (int argc, const char *argv[])
if (!nfc_initiator_select_passive_target (pnd, nmSAM, NULL, 0, &nt)) { if (!nfc_initiator_select_passive_target (pnd, nmSAM, NULL, 0, &nt)) {
nfc_perror (pnd, "nfc_initiator_select_passive_target"); nfc_perror (pnd, "nfc_initiator_select_passive_target");
ERR ("%s", "Reading of SAM info failed."); ERR ("%s", "Reading of SAM info failed.");
return EXIT_FAILURE; exit (EXIT_FAILURE);
} }
printf ("The following ISO14443A tag (SAM) was found:\n\n"); printf ("The following ISO14443A tag (SAM) was found:\n");
print_nfc_iso14443a_info (nt.nti.nai, true); print_nfc_iso14443a_info (nt.nti.nai, true);
} }
break; break;
case DUAL_CARD_MODE: case PSM_DUAL_CARD:
{ {
byte_t abtRx[MAX_FRAME_LEN]; byte_t abtRx[MAX_FRAME_LEN];
size_t szRx = sizeof(abtRx); size_t szRx = sizeof(abtRx);
@ -224,13 +179,15 @@ main (int argc, const char *argv[])
// wait_one_minute (); // wait_one_minute ();
} }
break; break;
default:
break;
} }
// Disconnect from the SAM // Disconnect from the SAM
sam_connection (pnd, NORMAL_MODE); pn53x_SAMConfiguration (pnd, PSM_NORMAL);
// Disconnect from NFC device // Disconnect from NFC device
nfc_disconnect (pnd); nfc_disconnect (pnd);
return EXIT_SUCCESS; exit (EXIT_SUCCESS);
} }

View file

@ -1861,28 +1861,27 @@ pn53x_SetParameters (nfc_device_t * pnd, const uint8_t ui8Value)
return true; return true;
} }
/*
typedef enum {
NORMAL = 0x01,
VIRTUAL_CARD = 0x02,
WIRED_MODE = 0x03,
DUAL_CARD = 0x04
} pn532_sam_mode;
*/
bool bool
pn53x_SAMConfiguration (nfc_device_t * pnd, const uint8_t ui8Mode) pn53x_SAMConfiguration (nfc_device_t * pnd, const pn532_sam_mode ui8Mode)
{ {
byte_t abtCmd[] = { SAMConfiguration, ui8Mode, 0x00, 0x00 }; byte_t abtCmd[] = { SAMConfiguration, ui8Mode, 0x00, 0x00 };
size_t szCmd = sizeof(abtCmd); size_t szCmd = sizeof(abtCmd);
if (CHIP_DATA(pnd)->type != PN532) {
// This function is not supported by pn531 neither pn533
pnd->iLastError = EDEVNOTSUP;
return false;
}
switch (ui8Mode) { switch (ui8Mode) {
case 0x01: // Normal mode case PSM_NORMAL: // Normal mode
szCmd = 2; szCmd = 2;
break; break;
case 0x02: // Virtual card mode case PSM_VIRTUAL_CARD: // Virtual card mode
case 0x03: // Wired card mode case PSM_WIRED_CARD: // Wired card mode
case 0x04: // Dual card mode case PSM_DUAL_CARD: // Dual card mode
// TODO: Handle these SAM mode // TODO Implement timeout handling
szCmd = 3;
break; break;
default: default:
pnd->iLastError = EINVALARG; pnd->iLastError = EINVALARG;

View file

@ -230,6 +230,13 @@ typedef enum {
PTT_DEP_ACTIVE_424 = 0x82, PTT_DEP_ACTIVE_424 = 0x82,
} pn53x_target_type_t; } pn53x_target_type_t;
typedef enum {
PSM_NORMAL = 0x01,
PSM_VIRTUAL_CARD = 0x02,
PSM_WIRED_CARD = 0x03,
PSM_DUAL_CARD = 0x04
} pn532_sam_mode;
/** /**
* @enum pn53x_target_mode_t * @enum pn53x_target_mode_t
* @brief PN53x target mode enumeration * @brief PN53x target mode enumeration
@ -306,7 +313,7 @@ const char *pn53x_strerror (const nfc_device_t * pnd);
// C wrappers for PN53x commands // C wrappers for PN53x commands
bool pn53x_SetParameters (nfc_device_t * pnd, const uint8_t ui8Value); bool pn53x_SetParameters (nfc_device_t * pnd, const uint8_t ui8Value);
bool pn53x_SAMConfiguration (nfc_device_t * pnd, const uint8_t ui8Mode); bool pn53x_SAMConfiguration (nfc_device_t * pnd, const pn532_sam_mode mode);
bool pn53x_PowerDown (nfc_device_t * pnd); bool pn53x_PowerDown (nfc_device_t * pnd);
bool pn53x_InListPassiveTarget (nfc_device_t * pnd, const pn53x_modulation_t pmInitModulation, bool pn53x_InListPassiveTarget (nfc_device_t * pnd, const pn53x_modulation_t pmInitModulation,
const byte_t szMaxTargets, const byte_t * pbtInitiatorData, const byte_t szMaxTargets, const byte_t * pbtInitiatorData,

View file

@ -93,13 +93,14 @@ pn532_uart_probe (nfc_device_desc_t pnddDevices[], size_t szDevices, size_t * ps
uart_set_speed (sp, PN532_UART_DEFAULT_SPEED); uart_set_speed (sp, PN532_UART_DEFAULT_SPEED);
nfc_device_t *pnd = nfc_device_new (); nfc_device_t *pnd = nfc_device_new ();
pnd->iLastError = 0;
pnd->driver = &pn532_uart_driver; pnd->driver = &pn532_uart_driver;
pnd->driver_data = malloc(sizeof(struct pn532_uart_data)); pnd->driver_data = malloc(sizeof(struct pn532_uart_data));
DRIVER_DATA (pnd)->port = sp; DRIVER_DATA (pnd)->port = sp;
// Alloc and init chip's data // Alloc and init chip's data
pn53x_data_new (pnd, &pn532_uart_io); pn53x_data_new (pnd, &pn532_uart_io);
// SAMConfiguration command if needed to wakeup the chip and pn53x_SAMConfiguration check if the chip is a PN532
CHIP_DATA (pnd)->type = PN532;
// This device starts in LowVBat power mode // This device starts in LowVBat power mode
CHIP_DATA (pnd)->power_mode = LOWVBAT; CHIP_DATA (pnd)->power_mode = LOWVBAT;
@ -166,6 +167,8 @@ pn532_uart_connect (const nfc_device_desc_t * pndd)
DRIVER_DATA(pnd)->port = sp; DRIVER_DATA(pnd)->port = sp;
// Alloc and init chip's data // Alloc and init chip's data
pn53x_data_new (pnd, &pn532_uart_io); pn53x_data_new (pnd, &pn532_uart_io);
// SAMConfiguration command if needed to wakeup the chip and pn53x_SAMConfiguration check if the chip is a PN532
CHIP_DATA (pnd)->type = PN532;
// This device starts in LowVBat mode // This device starts in LowVBat mode
CHIP_DATA(pnd)->power_mode = LOWVBAT; CHIP_DATA(pnd)->power_mode = LOWVBAT;