2010-05-28 11:09:36 +02:00
|
|
|
/*-
|
2013-03-10 16:15:23 +01:00
|
|
|
* Free/Libre Near Field Communication (NFC) library
|
2012-05-29 02:33:22 +02:00
|
|
|
*
|
2013-03-10 16:15:23 +01:00
|
|
|
* Libnfc historical contributors:
|
|
|
|
* Copyright (C) 2009 Roel Verdult
|
|
|
|
* Copyright (C) 2009-2013 Romuald Conty
|
|
|
|
* Copyright (C) 2010-2012 Romain Tartière
|
|
|
|
* Copyright (C) 2010-2013 Philippe Teuwen
|
|
|
|
* Copyright (C) 2012-2013 Ludovic Rousseau
|
2013-07-17 13:57:56 +02:00
|
|
|
* See AUTHORS file for a more comprehensive list of contributors.
|
2013-03-10 16:15:23 +01:00
|
|
|
* Additional contributors of this file:
|
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-05-28 11:09:36 +02:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2012-05-29 02:33:22 +02:00
|
|
|
/**
|
2010-05-28 11:09:36 +02:00
|
|
|
* @file pn53x-diagnose.c
|
|
|
|
* @brief Small application to diagnose PN53x using dedicated commands
|
|
|
|
*/
|
2011-09-12 16:46:58 +02:00
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif // HAVE_CONFIG_H
|
|
|
|
|
2010-05-28 11:09:36 +02:00
|
|
|
#include <err.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <nfc/nfc.h>
|
|
|
|
|
2011-09-30 13:33:31 +02:00
|
|
|
#include "utils/nfc-utils.h"
|
|
|
|
#include "libnfc/chips/pn53x.h"
|
2010-05-28 11:09:36 +02:00
|
|
|
|
|
|
|
#define MAX_DEVICE_COUNT 16
|
|
|
|
|
2010-09-07 19:51:03 +02:00
|
|
|
int
|
2012-05-29 17:54:36 +02:00
|
|
|
main(int argc, const char *argv[])
|
2010-05-28 11:09:36 +02:00
|
|
|
{
|
2010-09-07 19:51:03 +02:00
|
|
|
size_t i;
|
2013-03-05 19:44:59 +01:00
|
|
|
nfc_device *pnd = NULL;
|
2010-09-07 19:51:03 +02:00
|
|
|
const char *acLibnfcVersion;
|
|
|
|
bool result;
|
2010-05-28 11:09:36 +02:00
|
|
|
|
2011-11-24 11:54:42 +01:00
|
|
|
uint8_t abtRx[PN53x_EXTENDED_FRAME__DATA_MAX_LEN];
|
2011-03-02 16:02:30 +01:00
|
|
|
size_t szRx = sizeof(abtRx);
|
2011-11-24 11:54:42 +01:00
|
|
|
const uint8_t pncmd_diagnose_communication_line_test[] = { Diagnose, 0x00, 0x06, 'l', 'i', 'b', 'n', 'f', 'c' };
|
|
|
|
const uint8_t pncmd_diagnose_rom_test[] = { Diagnose, 0x01 };
|
|
|
|
const uint8_t pncmd_diagnose_ram_test[] = { Diagnose, 0x02 };
|
2010-05-28 11:09:36 +02:00
|
|
|
|
|
|
|
if (argc > 1) {
|
2013-03-05 19:44:59 +01:00
|
|
|
printf("Usage: %s", argv[0]);
|
|
|
|
exit(EXIT_FAILURE);
|
2010-05-28 11:09:36 +02:00
|
|
|
}
|
2012-05-29 02:33:22 +02:00
|
|
|
|
2012-12-04 19:29:57 +01:00
|
|
|
nfc_context *context;
|
|
|
|
nfc_init(&context);
|
2013-03-05 22:24:59 +01:00
|
|
|
if (context == NULL) {
|
|
|
|
ERR("Unable to init libnfc (malloc)");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2012-05-29 02:33:22 +02:00
|
|
|
|
2010-05-28 11:09:36 +02:00
|
|
|
// Display libnfc version
|
2012-05-29 17:54:36 +02:00
|
|
|
acLibnfcVersion = nfc_version();
|
|
|
|
printf("%s uses libnfc %s\n", argv[0], acLibnfcVersion);
|
2010-05-28 11:09:36 +02:00
|
|
|
|
2011-10-17 15:03:56 +02:00
|
|
|
nfc_connstring connstrings[MAX_DEVICE_COUNT];
|
2012-12-04 19:29:57 +01:00
|
|
|
size_t szFound = nfc_list_devices(context, connstrings, MAX_DEVICE_COUNT);
|
2010-05-28 11:09:36 +02:00
|
|
|
|
|
|
|
if (szFound == 0) {
|
2012-05-29 17:54:36 +02:00
|
|
|
printf("No NFC device found.\n");
|
2010-05-28 11:09:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < szFound; i++) {
|
2013-04-05 14:38:03 +02:00
|
|
|
int res = 0;
|
2012-12-04 19:29:57 +01:00
|
|
|
pnd = nfc_open(context, connstrings[i]);
|
2010-05-28 11:09:36 +02:00
|
|
|
|
|
|
|
if (pnd == NULL) {
|
2012-05-29 17:54:36 +02:00
|
|
|
ERR("%s", "Unable to open NFC device.");
|
2013-03-05 19:44:59 +01:00
|
|
|
nfc_exit(context);
|
|
|
|
exit(EXIT_FAILURE);
|
2010-05-28 11:09:36 +02:00
|
|
|
}
|
|
|
|
|
2012-05-29 17:54:36 +02:00
|
|
|
printf("NFC device [%s] opened.\n", nfc_device_get_name(pnd));
|
2010-05-28 11:09:36 +02:00
|
|
|
|
2012-05-29 17:54:36 +02:00
|
|
|
res = pn53x_transceive(pnd, pncmd_diagnose_communication_line_test, sizeof(pncmd_diagnose_communication_line_test), abtRx, szRx, 0);
|
2012-01-09 11:24:00 +01:00
|
|
|
if (res > 0) {
|
|
|
|
szRx = (size_t) res;
|
2011-06-07 22:36:20 +02:00
|
|
|
// Result of Diagnose ping for RC-S360 doesn't contain status byte so we've to handle both cases
|
2012-05-29 17:54:36 +02:00
|
|
|
result = (memcmp(pncmd_diagnose_communication_line_test + 1, abtRx, sizeof(pncmd_diagnose_communication_line_test) - 1) == 0) ||
|
|
|
|
(memcmp(pncmd_diagnose_communication_line_test + 2, abtRx, sizeof(pncmd_diagnose_communication_line_test) - 2) == 0);
|
|
|
|
printf(" Communication line test: %s\n", result ? "OK" : "Failed");
|
2011-04-28 15:26:47 +02:00
|
|
|
} else {
|
2012-05-29 17:54:36 +02:00
|
|
|
nfc_perror(pnd, "pn53x_transceive: cannot diagnose communication line");
|
2010-05-28 11:09:36 +02:00
|
|
|
}
|
|
|
|
|
2012-05-29 17:54:36 +02:00
|
|
|
res = pn53x_transceive(pnd, pncmd_diagnose_rom_test, sizeof(pncmd_diagnose_rom_test), abtRx, szRx, 0);
|
2012-01-09 11:24:00 +01:00
|
|
|
if (res > 0) {
|
|
|
|
szRx = (size_t) res;
|
2010-10-12 16:56:42 +02:00
|
|
|
result = ((szRx == 1) && (abtRx[0] == 0x00));
|
2012-05-29 17:54:36 +02:00
|
|
|
printf(" ROM test: %s\n", result ? "OK" : "Failed");
|
2011-04-28 15:26:47 +02:00
|
|
|
} else {
|
2012-05-29 17:54:36 +02:00
|
|
|
nfc_perror(pnd, "pn53x_transceive: cannot diagnose ROM");
|
2010-05-28 11:09:36 +02:00
|
|
|
}
|
|
|
|
|
2012-05-29 17:54:36 +02:00
|
|
|
res = pn53x_transceive(pnd, pncmd_diagnose_ram_test, sizeof(pncmd_diagnose_ram_test), abtRx, szRx, 0);
|
2012-01-09 11:24:00 +01:00
|
|
|
if (res > 0) {
|
|
|
|
szRx = (size_t) res;
|
2010-10-12 16:56:42 +02:00
|
|
|
result = ((szRx == 1) && (abtRx[0] == 0x00));
|
2012-05-29 17:54:36 +02:00
|
|
|
printf(" RAM test: %s\n", result ? "OK" : "Failed");
|
2011-04-28 15:26:47 +02:00
|
|
|
} else {
|
2012-05-29 17:54:36 +02:00
|
|
|
nfc_perror(pnd, "pn53x_transceive: cannot diagnose RAM");
|
2010-05-28 11:09:36 +02:00
|
|
|
}
|
|
|
|
}
|
2013-03-05 19:44:59 +01:00
|
|
|
nfc_close(pnd);
|
|
|
|
nfc_exit(context);
|
|
|
|
exit(EXIT_SUCCESS);
|
2010-06-26 23:56:32 +02:00
|
|
|
}
|