libnfc  1.7.1
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  * See AUTHORS file for a more comprehensive list of contributors.
11  * Additional contributors of this file:
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions are met:
15  * 1) Redistributions of source code must retain the above copyright notice,
16  * this list of conditions and the following disclaimer.
17  * 2 )Redistributions in binary form must reproduce the above copyright
18  * notice, this list of conditions and the following disclaimer in the
19  * documentation and/or other materials provided with the distribution.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  *
33  * Note that this license only applies on the examples, NFC library itself is under LGPL
34  *
35  */
36 
42 #ifdef HAVE_CONFIG_H
43 # include "config.h"
44 #endif // HAVE_CONFIG_H
45 
46 #include <err.h>
47 #include <stdlib.h>
48 #include <string.h>
49 
50 #include <nfc/nfc.h>
51 
52 #include "utils/nfc-utils.h"
53 #include "libnfc/chips/pn53x.h"
54 
55 #define MAX_DEVICE_COUNT 16
56 
57 int
58 main(int argc, const char *argv[])
59 {
60  size_t i;
61  nfc_device *pnd = NULL;
62  const char *acLibnfcVersion;
63  bool result;
64 
65  uint8_t abtRx[PN53x_EXTENDED_FRAME__DATA_MAX_LEN];
66  size_t szRx = sizeof(abtRx);
67  const uint8_t pncmd_diagnose_communication_line_test[] = { Diagnose, 0x00, 0x06, 'l', 'i', 'b', 'n', 'f', 'c' };
68  const uint8_t pncmd_diagnose_rom_test[] = { Diagnose, 0x01 };
69  const uint8_t pncmd_diagnose_ram_test[] = { Diagnose, 0x02 };
70 
71  if (argc > 1) {
72  printf("Usage: %s", argv[0]);
73  exit(EXIT_FAILURE);
74  }
75 
76  nfc_context *context;
77  nfc_init(&context);
78  if (context == NULL) {
79  ERR("Unable to init libnfc (malloc)");
80  exit(EXIT_FAILURE);
81  }
82 
83  // Display libnfc version
84  acLibnfcVersion = nfc_version();
85  printf("%s uses libnfc %s\n", argv[0], acLibnfcVersion);
86 
87  nfc_connstring connstrings[MAX_DEVICE_COUNT];
88  size_t szFound = nfc_list_devices(context, connstrings, MAX_DEVICE_COUNT);
89 
90  if (szFound == 0) {
91  printf("No NFC device found.\n");
92  }
93 
94  for (i = 0; i < szFound; i++) {
95  int res = 0;
96  pnd = nfc_open(context, connstrings[i]);
97 
98  if (pnd == NULL) {
99  ERR("%s", "Unable to open NFC device.");
100  nfc_exit(context);
101  exit(EXIT_FAILURE);
102  }
103 
104  printf("NFC device [%s] opened.\n", nfc_device_get_name(pnd));
105 
106  res = pn53x_transceive(pnd, pncmd_diagnose_communication_line_test, sizeof(pncmd_diagnose_communication_line_test), abtRx, szRx, 0);
107  if (res > 0) {
108  szRx = (size_t) res;
109  // Result of Diagnose ping for RC-S360 doesn't contain status byte so we've to handle both cases
110  result = (memcmp(pncmd_diagnose_communication_line_test + 1, abtRx, sizeof(pncmd_diagnose_communication_line_test) - 1) == 0) ||
111  (memcmp(pncmd_diagnose_communication_line_test + 2, abtRx, sizeof(pncmd_diagnose_communication_line_test) - 2) == 0);
112  printf(" Communication line test: %s\n", result ? "OK" : "Failed");
113  } else {
114  nfc_perror(pnd, "pn53x_transceive: cannot diagnose communication line");
115  }
116 
117  res = pn53x_transceive(pnd, pncmd_diagnose_rom_test, sizeof(pncmd_diagnose_rom_test), abtRx, szRx, 0);
118  if (res > 0) {
119  szRx = (size_t) res;
120  result = ((szRx == 1) && (abtRx[0] == 0x00));
121  printf(" ROM test: %s\n", result ? "OK" : "Failed");
122  } else {
123  nfc_perror(pnd, "pn53x_transceive: cannot diagnose ROM");
124  }
125 
126  res = pn53x_transceive(pnd, pncmd_diagnose_ram_test, sizeof(pncmd_diagnose_ram_test), abtRx, szRx, 0);
127  if (res > 0) {
128  szRx = (size_t) res;
129  result = ((szRx == 1) && (abtRx[0] == 0x00));
130  printf(" RAM test: %s\n", result ? "OK" : "Failed");
131  } else {
132  nfc_perror(pnd, "pn53x_transceive: cannot diagnose RAM");
133  }
134  }
135  nfc_close(pnd);
136  nfc_exit(context);
137  exit(EXIT_SUCCESS);
138 }
nfc_init
void nfc_init(nfc_context **context)
Initialize libnfc. This function must be called before calling any other libnfc function.
Definition: nfc.c:192
nfc_context
NFC library context Struct which contains internal options, references, pointers, etc....
Definition: nfc-internal.h:175
nfc_device
NFC device information.
Definition: nfc-internal.h:190
nfc_version
const char * nfc_version(void)
Returns the library version.
Definition: nfc.c:1218
nfc_exit
void nfc_exit(nfc_context *context)
Deinitialize libnfc. Should be called after closing all open devices and before your application term...
Definition: nfc.c:209
nfc_connstring
char nfc_connstring[NFC_BUFSIZE_CONNSTRING]
Definition: nfc-types.h:62
nfc_device_get_name
const char * nfc_device_get_name(nfc_device *pnd)
Returns the device name.
Definition: nfc.c:1164
nfc_open
nfc_device * nfc_open(nfc_context *context, const nfc_connstring connstring)
Open a NFC device.
Definition: nfc.c:238
nfc_perror
void nfc_perror(const nfc_device *pnd, const char *pcString)
Display the last error occured on a nfc_device.
Definition: nfc.c:1138
nfc_list_devices
size_t nfc_list_devices(nfc_context *context, nfc_connstring connstrings[], const size_t connstrings_len)
Scan for discoverable supported devices (ie. only available for some drivers)
Definition: nfc.c:317
ERR
#define ERR(...)
Print a error message.
Definition: nfc-utils.h:85
nfc_close
void nfc_close(nfc_device *pnd)
Close from a NFC device.
Definition: nfc.c:300
nfc-utils.h
Provide some examples shared functions like print, parity calculation, options parsing.
nfc.h
libnfc interface