2010-06-15 17:05:40 +02:00
|
|
|
/*-
|
2010-11-17 15:27:11 +01:00
|
|
|
* Public platform independent Near Field Communication (NFC) library examples
|
2012-05-29 02:33:22 +02:00
|
|
|
*
|
2012-10-21 16:09:16 +02:00
|
|
|
* Copyright (C) 2010 Emanuele Bertoldi
|
2012-05-29 02:33:22 +02:00
|
|
|
*
|
2010-11-17 15:27:11 +01:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
* 1) Redistributions of source code must retain the above copyright notice,
|
2012-05-29 02:33:22 +02:00
|
|
|
* this list of conditions and the following disclaimer.
|
2010-11-17 15:27:11 +01:00
|
|
|
* 2 )Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
|
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
2012-05-29 02:33:22 +02:00
|
|
|
*
|
2010-11-17 15:27:11 +01:00
|
|
|
* Note that this license only applies on the examples, NFC library itself is under LGPL
|
2010-06-15 17:05:40 +02:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2010-10-06 11:10:14 +02:00
|
|
|
* @file pn53x-sam.c
|
2010-10-14 13:53:27 +02:00
|
|
|
* @brief Configures the NFC device to communicate with a SAM (Secure Access Module).
|
2011-06-27 19:12:25 +02:00
|
|
|
* @note This example requiers a PN532 with SAM connected using S2C interface
|
|
|
|
* @see PN532 User manual
|
2010-06-15 17:05:40 +02:00
|
|
|
*/
|
2010-09-07 19:51:03 +02:00
|
|
|
|
2010-06-15 17:05:40 +02:00
|
|
|
#ifdef HAVE_CONFIG_H
|
2010-09-07 19:51:03 +02:00
|
|
|
# include "config.h"
|
2010-06-15 17:05:40 +02:00
|
|
|
#endif // HAVE_CONFIG_H
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2010-06-16 13:21:27 +02:00
|
|
|
#include <time.h>
|
2011-06-27 19:12:25 +02:00
|
|
|
#include <unistd.h>
|
2010-06-16 14:12:31 +02:00
|
|
|
|
2010-06-15 17:05:40 +02:00
|
|
|
#include <nfc/nfc.h>
|
2010-10-06 11:03:08 +02:00
|
|
|
|
2011-09-30 13:33:31 +02:00
|
|
|
#include "utils/nfc-utils.h"
|
|
|
|
#include "libnfc/chips/pn53x.h"
|
2010-06-15 17:05:40 +02:00
|
|
|
|
|
|
|
#define MAX_FRAME_LEN 264
|
2010-09-07 19:51:03 +02:00
|
|
|
#define TIMEOUT 60 // secs.
|
2010-06-15 17:05:40 +02:00
|
|
|
|
2012-05-13 14:32:30 +02:00
|
|
|
static void
|
2012-05-29 17:54:36 +02:00
|
|
|
wait_one_minute(void)
|
2010-06-16 13:21:27 +02:00
|
|
|
{
|
2010-09-07 19:51:03 +02:00
|
|
|
int secs = 0;
|
|
|
|
|
2012-05-29 17:54:36 +02:00
|
|
|
printf("|");
|
|
|
|
fflush(stdout);
|
2010-09-07 19:51:03 +02:00
|
|
|
|
|
|
|
while (secs < TIMEOUT) {
|
2012-05-29 17:54:36 +02:00
|
|
|
sleep(1);
|
2010-07-29 12:18:19 +02:00
|
|
|
secs++;
|
2012-05-29 17:54:36 +02:00
|
|
|
printf(".");
|
|
|
|
fflush(stdout);
|
2010-06-16 13:21:27 +02:00
|
|
|
}
|
2010-09-07 19:51:03 +02:00
|
|
|
|
2012-05-29 17:54:36 +02:00
|
|
|
printf("|\n");
|
2010-06-16 13:21:27 +02:00
|
|
|
}
|
|
|
|
|
2010-09-07 19:51:03 +02:00
|
|
|
int
|
2012-05-29 17:54:36 +02:00
|
|
|
main(int argc, const char *argv[])
|
2010-06-16 13:21:27 +02:00
|
|
|
{
|
2010-09-07 19:51:03 +02:00
|
|
|
(void) argc;
|
|
|
|
(void) argv;
|
2012-05-29 02:33:22 +02:00
|
|
|
|
2012-05-21 01:09:21 +02:00
|
|
|
int ret = EXIT_FAILURE;
|
2010-07-24 20:54:50 +02:00
|
|
|
|
2012-05-29 17:54:36 +02:00
|
|
|
nfc_init(NULL);
|
2012-05-29 02:33:22 +02:00
|
|
|
|
2010-06-15 17:05:40 +02:00
|
|
|
// Display libnfc version
|
2012-05-29 17:54:36 +02:00
|
|
|
const char *acLibnfcVersion = nfc_version();
|
|
|
|
printf("%s uses libnfc %s\n", argv[0], acLibnfcVersion);
|
2010-06-15 17:05:40 +02:00
|
|
|
|
2012-01-17 16:21:56 +01:00
|
|
|
// Open using the first available NFC device
|
2012-05-21 01:09:21 +02:00
|
|
|
nfc_device *pnd;
|
2012-05-29 17:54:36 +02:00
|
|
|
pnd = nfc_open(NULL, NULL);
|
2010-06-15 17:05:40 +02:00
|
|
|
|
|
|
|
if (pnd == NULL) {
|
2012-05-29 17:54:36 +02:00
|
|
|
ERR("%s", "Unable to open NFC device.");
|
2010-07-26 10:55:43 +02:00
|
|
|
return EXIT_FAILURE;
|
2010-06-15 17:05:40 +02:00
|
|
|
}
|
|
|
|
|
2012-05-29 17:54:36 +02:00
|
|
|
printf("NFC device: %s opened\n", nfc_device_get_name(pnd));
|
2010-09-07 19:51:03 +02:00
|
|
|
|
2010-06-16 13:21:27 +02:00
|
|
|
// Print the example's menu
|
2012-05-29 17:54:36 +02:00
|
|
|
printf("\nSelect the communication mode:\n");
|
|
|
|
printf("[1] Virtual card mode.\n");
|
|
|
|
printf("[2] Wired card mode.\n");
|
|
|
|
printf("[3] Dual card mode.\n");
|
|
|
|
printf(">> ");
|
2010-09-07 19:51:03 +02:00
|
|
|
|
2010-06-16 13:21:27 +02:00
|
|
|
// Take user's choice
|
2012-05-29 17:54:36 +02:00
|
|
|
char input = getchar();
|
|
|
|
printf("\n");
|
2012-05-21 01:09:21 +02:00
|
|
|
if ((input < '1') || (input > '3')) {
|
2012-05-29 17:54:36 +02:00
|
|
|
ERR("%s", "Invalid selection.");
|
2012-05-21 01:09:21 +02:00
|
|
|
goto error;
|
2010-06-16 13:32:16 +02:00
|
|
|
}
|
2012-05-21 01:09:21 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* '1' -> "Virtual mode" (0x02)
|
|
|
|
* '2' -> "Wired card" (0x03)
|
|
|
|
* '3' -> "Dual card" (0x04)
|
|
|
|
*/
|
|
|
|
int iMode = input - '0' + 0x01;
|
2011-06-27 19:12:25 +02:00
|
|
|
pn532_sam_mode mode = iMode;
|
|
|
|
|
2010-06-16 13:21:27 +02:00
|
|
|
// Connect with the SAM
|
2010-09-07 19:51:03 +02:00
|
|
|
|
|
|
|
switch (mode) {
|
2012-05-29 17:53:43 +02:00
|
|
|
case PSM_VIRTUAL_CARD: {
|
2012-06-04 02:16:28 +02:00
|
|
|
// FIXME Its a private pn53x function
|
|
|
|
if (pn532_SAMConfiguration(pnd, mode, 0) < 0) {
|
|
|
|
nfc_perror(pnd, "pn53x_SAMConfiguration");
|
|
|
|
goto error;
|
|
|
|
}
|
2012-05-29 17:54:36 +02:00
|
|
|
printf("Now the SAM is readable for 1 minute from an external reader.\n");
|
|
|
|
wait_one_minute();
|
2010-06-15 17:05:40 +02:00
|
|
|
}
|
|
|
|
break;
|
2010-09-07 19:51:03 +02:00
|
|
|
|
2012-05-29 17:53:43 +02:00
|
|
|
case PSM_WIRED_CARD: {
|
2012-01-17 16:21:56 +01:00
|
|
|
// Set opened NFC device to initiator mode
|
2012-06-04 02:16:28 +02:00
|
|
|
if (nfc_initiator_init_secure_element(pnd) < 0) {
|
|
|
|
nfc_perror(pnd, "nfc_initiator_init_secure_element");
|
2012-05-21 01:09:21 +02:00
|
|
|
goto error;
|
2012-01-05 11:33:50 +01:00
|
|
|
}
|
2010-06-16 13:40:23 +02:00
|
|
|
|
|
|
|
// Let the reader only try once to find a tag
|
2012-05-29 17:54:36 +02:00
|
|
|
if (nfc_device_set_property_bool(pnd, NP_INFINITE_SELECT, false) < 0) {
|
|
|
|
nfc_perror(pnd, "nfc_device_set_property_bool");
|
2012-05-21 01:09:21 +02:00
|
|
|
goto error;
|
2010-08-18 19:22:13 +02:00
|
|
|
}
|
2010-06-16 13:21:27 +02:00
|
|
|
// Read the SAM's info
|
2011-11-23 16:55:40 +01:00
|
|
|
const nfc_modulation nmSAM = {
|
2010-10-13 19:43:23 +02:00
|
|
|
.nmt = NMT_ISO14443A,
|
|
|
|
.nbr = NBR_106,
|
|
|
|
};
|
2012-06-04 02:16:28 +02:00
|
|
|
nfc_target nt;
|
|
|
|
|
2012-05-21 01:09:21 +02:00
|
|
|
int res;
|
2012-05-29 17:54:36 +02:00
|
|
|
if ((res = nfc_initiator_select_passive_target(pnd, nmSAM, NULL, 0, &nt)) < 0) {
|
|
|
|
nfc_perror(pnd, "nfc_initiator_select_passive_target");
|
2012-05-21 01:09:21 +02:00
|
|
|
goto error;
|
|
|
|
} else if (res == 0) {
|
2012-05-29 17:54:36 +02:00
|
|
|
ERR("No SAM found.");
|
2012-05-21 01:09:21 +02:00
|
|
|
goto error;
|
|
|
|
} else if (res == 1) {
|
2012-05-29 17:54:36 +02:00
|
|
|
printf("The following ISO14443A tag (SAM) was found:\n");
|
2012-09-17 15:47:54 +02:00
|
|
|
print_nfc_target(nt, true);
|
2012-05-21 01:09:21 +02:00
|
|
|
} else {
|
2012-05-29 17:54:36 +02:00
|
|
|
ERR("%s", "More than one ISO14442 tag found as SAM.");
|
2012-05-21 01:09:21 +02:00
|
|
|
goto error;
|
2010-06-16 13:32:16 +02:00
|
|
|
}
|
2010-06-15 17:05:40 +02:00
|
|
|
}
|
|
|
|
break;
|
2010-09-07 19:51:03 +02:00
|
|
|
|
2012-05-29 17:53:43 +02:00
|
|
|
case PSM_DUAL_CARD: {
|
2012-06-04 02:16:28 +02:00
|
|
|
// FIXME Its a private pn53x function
|
|
|
|
if (pn532_SAMConfiguration(pnd, mode, 0) < 0) {
|
|
|
|
nfc_perror(pnd, "pn53x_SAMConfiguration");
|
|
|
|
goto error;
|
|
|
|
}
|
2011-11-24 11:54:42 +01:00
|
|
|
uint8_t abtRx[MAX_FRAME_LEN];
|
2010-09-07 19:51:03 +02:00
|
|
|
|
2011-11-23 16:55:40 +01:00
|
|
|
nfc_target nt = {
|
2011-02-28 10:47:31 +01:00
|
|
|
.nm = {
|
|
|
|
.nmt = NMT_ISO14443A,
|
|
|
|
.nbr = NBR_UNDEFINED,
|
|
|
|
},
|
|
|
|
.nti = {
|
|
|
|
.nai = {
|
|
|
|
.abtAtqa = { 0x04, 0x00 },
|
|
|
|
.abtUid = { 0x08, 0xad, 0xbe, 0xef },
|
|
|
|
.btSak = 0x20,
|
|
|
|
.szUidLen = 4,
|
|
|
|
.szAtsLen = 0,
|
|
|
|
},
|
|
|
|
},
|
2010-10-04 14:46:03 +02:00
|
|
|
};
|
2012-05-29 17:54:36 +02:00
|
|
|
printf("Now both, NFC device (configured as target) and SAM are readables from an external NFC initiator.\n");
|
|
|
|
printf("Please note that NFC device (configured as target) stay in target mode until it receive RATS, ATR_REQ or proprietary command.\n");
|
|
|
|
if (nfc_target_init(pnd, &nt, abtRx, sizeof(abtRx), 0) < 0) {
|
2010-10-06 11:03:08 +02:00
|
|
|
nfc_perror(pnd, "nfc_target_init");
|
2010-06-16 13:21:27 +02:00
|
|
|
return EXIT_FAILURE;
|
2010-10-06 11:03:08 +02:00
|
|
|
}
|
|
|
|
// wait_one_minute ();
|
2010-06-15 17:05:40 +02:00
|
|
|
}
|
|
|
|
break;
|
2012-05-29 17:52:51 +02:00
|
|
|
case PSM_NORMAL:
|
2012-06-04 02:16:28 +02:00
|
|
|
// This should not happend... nothing to do.
|
2012-05-29 17:52:51 +02:00
|
|
|
break;
|
2010-06-15 17:05:40 +02:00
|
|
|
}
|
2012-05-21 01:09:21 +02:00
|
|
|
ret = EXIT_SUCCESS;
|
2010-09-07 19:51:03 +02:00
|
|
|
|
2012-05-21 01:09:21 +02:00
|
|
|
error:
|
2010-07-26 10:55:43 +02:00
|
|
|
// Disconnect from the SAM
|
2012-06-04 02:16:28 +02:00
|
|
|
pn532_SAMConfiguration(pnd, PSM_NORMAL, -1);
|
2010-06-15 17:05:40 +02:00
|
|
|
|
2012-01-17 16:21:56 +01:00
|
|
|
// Close NFC device
|
2012-05-29 17:54:36 +02:00
|
|
|
nfc_close(pnd);
|
|
|
|
nfc_exit(NULL);
|
2010-09-07 19:51:03 +02:00
|
|
|
|
2012-05-29 17:54:36 +02:00
|
|
|
exit(ret);
|
2010-06-15 17:05:40 +02:00
|
|
|
}
|