2010-11-17 15:27:11 +01:00
|
|
|
/*-
|
|
|
|
* Public platform independent Near Field Communication (NFC) library examples
|
|
|
|
*
|
|
|
|
* Copyright (C) 2010, Romuald Conty
|
|
|
|
*
|
|
|
|
* 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,
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* Note that this license only applies on the examples, NFC library itself is under LGPL
|
|
|
|
*
|
|
|
|
*/
|
2010-10-14 13:53:27 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @file pn53x-tamashell.c
|
|
|
|
* @brief Configures the NFC device to communicate with a SAM (Secure Access Module).
|
|
|
|
*/
|
|
|
|
|
2010-10-04 12:24:45 +02:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif // HAVE_CONFIG_H
|
2010-10-03 01:35:24 +02:00
|
|
|
|
2010-10-07 14:59:12 +02:00
|
|
|
# define _GNU_SOURCE // for getline on system with glibc < 2.10
|
|
|
|
# define _POSIX_C_SOURCE 200809L // for getline on system with glibc >= 2.10
|
|
|
|
# include <stdio.h>
|
2011-02-03 18:17:29 +01:00
|
|
|
#if defined(HAVE_READLINE)
|
|
|
|
# include <readline/readline.h>
|
|
|
|
# include <readline/history.h>
|
2010-10-03 01:35:24 +02:00
|
|
|
#endif //HAVE_READLINE
|
2010-10-04 12:24:45 +02:00
|
|
|
|
2010-10-07 14:59:12 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
2011-04-08 21:50:09 +02:00
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
// Needed by sleep() under Unix
|
|
|
|
# include <unistd.h>
|
|
|
|
# define sleep usleep
|
|
|
|
# define SUSP_TIME 1000 // usecs.
|
|
|
|
#else
|
|
|
|
// Needed by Sleep() under Windows
|
|
|
|
# include <winbase.h>
|
|
|
|
# define sleep Sleep
|
|
|
|
# define SUSP_TIME 1 // msecs.
|
|
|
|
#endif
|
|
|
|
|
2010-10-07 14:59:12 +02:00
|
|
|
|
2010-10-03 01:35:24 +02:00
|
|
|
#include <nfc/nfc.h>
|
|
|
|
|
2011-09-30 13:33:31 +02:00
|
|
|
#include "utils/nfc-utils.h"
|
|
|
|
#include "libnfc/chips/pn53x.h"
|
2010-10-04 12:24:45 +02:00
|
|
|
|
2010-10-03 01:35:24 +02:00
|
|
|
#define MAX_FRAME_LEN 264
|
|
|
|
|
|
|
|
int main(int argc, const char* argv[])
|
|
|
|
{
|
2011-11-25 16:21:10 +01:00
|
|
|
nfc_device *pnd;
|
2011-11-24 11:54:42 +01:00
|
|
|
uint8_t abtRx[MAX_FRAME_LEN];
|
|
|
|
uint8_t abtTx[MAX_FRAME_LEN];
|
2011-03-02 16:02:30 +01:00
|
|
|
size_t szRx = sizeof(abtRx);
|
2010-10-12 16:56:42 +02:00
|
|
|
size_t szTx;
|
2011-02-03 18:17:29 +01:00
|
|
|
extern FILE* stdin;
|
|
|
|
FILE* input = NULL;
|
|
|
|
|
|
|
|
if (argc >= 2) {
|
2012-01-20 10:17:38 +01:00
|
|
|
if((input = fopen(argv[1], "r"))==NULL) {
|
2011-02-03 18:17:29 +01:00
|
|
|
ERR ("%s", "Cannot open file.");
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
2010-10-03 01:35:24 +02:00
|
|
|
|
2012-01-18 17:22:06 +01:00
|
|
|
nfc_init (NULL);
|
2012-01-18 12:09:01 +01:00
|
|
|
|
2010-10-03 01:35:24 +02:00
|
|
|
// Try to open the NFC reader
|
2012-01-18 17:22:06 +01:00
|
|
|
pnd = nfc_open(NULL, NULL);
|
2010-10-03 01:35:24 +02:00
|
|
|
|
|
|
|
if (pnd == NULL) {
|
2012-01-17 16:21:56 +01:00
|
|
|
ERR ("%s", "Unable to open NFC device.");
|
2010-10-03 01:35:24 +02:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2012-01-17 16:21:56 +01:00
|
|
|
printf ("NFC reader: %s opened\n", nfc_device_get_name (pnd));
|
2012-01-05 11:33:50 +01:00
|
|
|
if (nfc_initiator_init (pnd) < 0) {
|
|
|
|
nfc_perror (pnd, "nfc_initiator_init");
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
2010-10-03 01:35:24 +02:00
|
|
|
|
2011-11-25 16:21:10 +01:00
|
|
|
char *cmd;
|
|
|
|
char *prompt = "> ";
|
2010-10-03 01:35:24 +02:00
|
|
|
while(1) {
|
2012-01-20 10:17:38 +01:00
|
|
|
int offset = 0;
|
2010-10-03 01:35:24 +02:00
|
|
|
#if defined(HAVE_READLINE)
|
2012-01-20 10:17:38 +01:00
|
|
|
if (input == NULL) { // means we use stdin
|
|
|
|
cmd = readline(prompt);
|
2011-02-03 18:17:29 +01:00
|
|
|
// NULL if ctrl-d
|
2012-01-20 10:17:38 +01:00
|
|
|
if (cmd == NULL) {
|
2011-02-03 18:17:29 +01:00
|
|
|
printf("Bye!\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
add_history(cmd);
|
|
|
|
} else {
|
|
|
|
#endif //HAVE_READLINE
|
2011-02-10 15:29:29 +01:00
|
|
|
size_t n = 255;
|
2011-11-25 16:21:10 +01:00
|
|
|
char *ret = NULL;
|
2011-02-10 15:29:29 +01:00
|
|
|
cmd = malloc(n);
|
2011-02-03 18:17:29 +01:00
|
|
|
printf("%s", prompt);
|
|
|
|
fflush(0);
|
|
|
|
if (input != NULL) {
|
2011-02-10 15:29:29 +01:00
|
|
|
ret = fgets(cmd, n, input);
|
2011-02-03 18:17:29 +01:00
|
|
|
} else {
|
2011-02-10 15:29:29 +01:00
|
|
|
ret = fgets(cmd, n, stdin);
|
2011-02-03 18:17:29 +01:00
|
|
|
}
|
2011-02-10 15:29:29 +01:00
|
|
|
if (ret == NULL || strlen(cmd) <= 0) {
|
2011-02-03 18:17:29 +01:00
|
|
|
printf("Bye!\n");
|
|
|
|
free(cmd);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// FIXME print only if read from redirected stdin (i.e. script)
|
|
|
|
printf("%s", cmd);
|
|
|
|
#if defined(HAVE_READLINE)
|
2010-10-03 01:35:24 +02:00
|
|
|
}
|
|
|
|
#endif //HAVE_READLINE
|
2012-01-20 10:17:38 +01:00
|
|
|
if (cmd[0] == 'q') {
|
2010-10-03 01:35:24 +02:00
|
|
|
printf("Bye!\n");
|
|
|
|
free(cmd);
|
|
|
|
break;
|
|
|
|
}
|
2012-01-20 10:17:38 +01:00
|
|
|
if (cmd[0] == 'p') {
|
|
|
|
int s = 0;
|
2011-02-18 23:05:36 +01:00
|
|
|
offset++;
|
|
|
|
while (isspace(cmd[offset])) {
|
|
|
|
offset++;
|
|
|
|
}
|
2012-01-20 10:17:38 +01:00
|
|
|
sscanf(cmd + offset, "%d", &s);
|
2011-04-08 21:50:09 +02:00
|
|
|
printf("Pause for %i msecs\n", s);
|
2011-02-18 23:05:36 +01:00
|
|
|
if (s>0) {
|
2011-04-08 21:50:09 +02:00
|
|
|
sleep(s * SUSP_TIME);
|
2011-02-18 23:05:36 +01:00
|
|
|
}
|
|
|
|
free(cmd);
|
|
|
|
continue;
|
|
|
|
}
|
2010-10-12 16:56:42 +02:00
|
|
|
szTx = 0;
|
2012-01-20 10:17:38 +01:00
|
|
|
for(int i = 0; i < MAX_FRAME_LEN - 10; i++) {
|
2010-10-03 01:35:24 +02:00
|
|
|
int size;
|
2011-11-24 11:54:42 +01:00
|
|
|
uint8_t byte;
|
2010-10-03 01:35:24 +02:00
|
|
|
while (isspace(cmd[offset])) {
|
|
|
|
offset++;
|
|
|
|
}
|
2010-10-07 14:59:12 +02:00
|
|
|
size = sscanf(cmd+offset, "%2x", (unsigned int*)&byte);
|
2012-01-20 10:17:38 +01:00
|
|
|
if (size < 1 ) {
|
2010-10-03 01:35:24 +02:00
|
|
|
break;
|
|
|
|
}
|
2011-03-09 15:45:22 +01:00
|
|
|
abtTx[i] = byte;
|
2010-10-12 16:56:42 +02:00
|
|
|
szTx++;
|
2010-10-03 01:35:24 +02:00
|
|
|
if (cmd[offset+1] == 0) { // if last hex was only 1 symbol
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
offset += 2;
|
|
|
|
}
|
|
|
|
|
2010-10-12 16:56:42 +02:00
|
|
|
if ((int)szTx < 1) {
|
2010-10-03 01:35:24 +02:00
|
|
|
free(cmd);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
printf("Tx: ");
|
2011-11-24 11:54:42 +01:00
|
|
|
print_hex((uint8_t*)abtTx,szTx);
|
2010-10-03 01:35:24 +02:00
|
|
|
|
2011-03-10 15:43:59 +01:00
|
|
|
szRx = sizeof(abtRx);
|
2012-01-09 11:24:00 +01:00
|
|
|
int res = 0;
|
|
|
|
if ((res = pn53x_transceive (pnd, abtTx, szTx, abtRx, szRx, 0)) < 0) {
|
2010-10-03 01:35:24 +02:00
|
|
|
free(cmd);
|
|
|
|
nfc_perror (pnd, "Rx");
|
|
|
|
continue;
|
|
|
|
}
|
2012-01-09 11:24:00 +01:00
|
|
|
szRx = (size_t) res;
|
2010-10-03 01:35:24 +02:00
|
|
|
|
|
|
|
printf("Rx: ");
|
2010-10-12 16:56:42 +02:00
|
|
|
print_hex(abtRx, szRx);
|
2010-10-03 01:35:24 +02:00
|
|
|
free(cmd);
|
|
|
|
}
|
|
|
|
|
2011-02-03 18:17:29 +01:00
|
|
|
if (input != NULL) {
|
|
|
|
fclose(input);
|
|
|
|
}
|
2012-01-17 15:52:39 +01:00
|
|
|
nfc_close(pnd);
|
2012-01-18 17:22:06 +01:00
|
|
|
nfc_exit (NULL);
|
2010-10-03 01:35:24 +02:00
|
|
|
return 1;
|
|
|
|
}
|