libnfc  1.7.0-rc7
pn53x-diagnose.c
Go to the documentation of this file.
1 /*-
2  * Free/Libre Near Field Communication (NFC) library
3  *
4  * Libnfc historical contributors:
5  * Copyright (C) 2009 Roel Verdult
6  * Copyright (C) 2009-2013 Romuald Conty
7  * Copyright (C) 2010-2012 Romain Tartière
8  * Copyright (C) 2010-2013 Philippe Teuwen
9  * Copyright (C) 2012-2013 Ludovic Rousseau
10  * Additional contributors of this file:
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions are met:
14  * 1) Redistributions of source code must retain the above copyright notice,
15  * this list of conditions and the following disclaimer.
16  * 2 )Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in the
18  * documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  *
32  * Note that this license only applies on the examples, NFC library itself is under LGPL
33  *
34  */
35 
41 #ifdef HAVE_CONFIG_H
42 # include "config.h"
43 #endif // HAVE_CONFIG_H
44 
45 #include <err.h>
46 #include <stdlib.h>
47 #include <string.h>
48 
49 #include <nfc/nfc.h>
50 
51 #include "utils/nfc-utils.h"
52 #include "libnfc/chips/pn53x.h"
53 
54 #define MAX_DEVICE_COUNT 16
55 
56 int
57 main(int argc, const char *argv[])
58 {
59  size_t i;
60  nfc_device *pnd = NULL;
61  const char *acLibnfcVersion;
62  bool result;
63 
64  uint8_t abtRx[PN53x_EXTENDED_FRAME__DATA_MAX_LEN];
65  size_t szRx = sizeof(abtRx);
66  const uint8_t pncmd_diagnose_communication_line_test[] = { Diagnose, 0x00, 0x06, 'l', 'i', 'b', 'n', 'f', 'c' };
67  const uint8_t pncmd_diagnose_rom_test[] = { Diagnose, 0x01 };
68  const uint8_t pncmd_diagnose_ram_test[] = { Diagnose, 0x02 };
69 
70  if (argc > 1) {
71  printf("Usage: %s", argv[0]);
72  exit(EXIT_FAILURE);
73  }
74 
75  nfc_context *context;
76  nfc_init(&context);
77  if (context == NULL) {
78  ERR("Unable to init libnfc (malloc)");
79  exit(EXIT_FAILURE);
80  }
81 
82  // Display libnfc version
83  acLibnfcVersion = nfc_version();
84  printf("%s uses libnfc %s\n", argv[0], acLibnfcVersion);
85 
86  nfc_connstring connstrings[MAX_DEVICE_COUNT];
87  size_t szFound = nfc_list_devices(context, connstrings, MAX_DEVICE_COUNT);
88 
89  if (szFound == 0) {
90  printf("No NFC device found.\n");
91  }
92 
93  for (i = 0; i < szFound; i++) {
94  int res = 0;
95  pnd = nfc_open(context, connstrings[i]);
96 
97  if (pnd == NULL) {
98  ERR("%s", "Unable to open NFC device.");
99  nfc_exit(context);
100  exit(EXIT_FAILURE);
101  }
102 
103  printf("NFC device [%s] opened.\n", nfc_device_get_name(pnd));
104 
105  res = pn53x_transceive(pnd, pncmd_diagnose_communication_line_test, sizeof(pncmd_diagnose_communication_line_test), abtRx, szRx, 0);
106  if (res > 0) {
107  szRx = (size_t) res;
108  // Result of Diagnose ping for RC-S360 doesn't contain status byte so we've to handle both cases
109  result = (memcmp(pncmd_diagnose_communication_line_test + 1, abtRx, sizeof(pncmd_diagnose_communication_line_test) - 1) == 0) ||
110  (memcmp(pncmd_diagnose_communication_line_test + 2, abtRx, sizeof(pncmd_diagnose_communication_line_test) - 2) == 0);
111  printf(" Communication line test: %s\n", result ? "OK" : "Failed");
112  } else {
113  nfc_perror(pnd, "pn53x_transceive: cannot diagnose communication line");
114  }
115 
116  res = pn53x_transceive(pnd, pncmd_diagnose_rom_test, sizeof(pncmd_diagnose_rom_test), abtRx, szRx, 0);
117  if (res > 0) {
118  szRx = (size_t) res;
119  result = ((szRx == 1) && (abtRx[0] == 0x00));
120  printf(" ROM test: %s\n", result ? "OK" : "Failed");
121  } else {
122  nfc_perror(pnd, "pn53x_transceive: cannot diagnose ROM");
123  }
124 
125  res = pn53x_transceive(pnd, pncmd_diagnose_ram_test, sizeof(pncmd_diagnose_ram_test), abtRx, szRx, 0);
126  if (res > 0) {
127  szRx = (size_t) res;
128  result = ((szRx == 1) && (abtRx[0] == 0x00));
129  printf(" RAM test: %s\n", result ? "OK" : "Failed");
130  } else {
131  nfc_perror(pnd, "pn53x_transceive: cannot diagnose RAM");
132  }
133  }
134  nfc_close(pnd);
135  nfc_exit(context);
136  exit(EXIT_SUCCESS);
137 }