libnfc  1.4.2
pn53x-diagnose.c
Go to the documentation of this file.
1 /*-
2  * Public platform independent Near Field Communication (NFC) library examples
3  *
4  * Copyright (C) 2010, Romuald Conty
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  * 1) Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * 2 )Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
18  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24  * POSSIBILITY OF SUCH DAMAGE.
25  *
26  * Note that this license only applies on the examples, NFC library itself is under LGPL
27  *
28  */
29 
34 #include <err.h>
35 #include <stdlib.h>
36 #include <string.h>
37 
38 #include <nfc/nfc.h>
39 #include <nfc/nfc-messages.h>
40 
41 #include "nfc-utils.h"
42 #include "chips/pn53x.h"
43 
44 #define MAX_DEVICE_COUNT 16
45 
46 
47 int
48 main (int argc, const char *argv[])
49 {
50  size_t szFound;
51  size_t i;
52  nfc_device_t *pnd;
53  nfc_device_desc_t *pnddDevices;
54  const char *acLibnfcVersion;
55  bool result;
56 
57  byte_t abtRx[PN53x_EXTENDED_FRAME_MAX_LEN];
58  size_t szRx;
59  const byte_t pncmd_diagnose_communication_line_test[] = { 0xD4, 0x00, 0x00, 0x06, 'l', 'i', 'b', 'n', 'f', 'c' };
60  const byte_t pncmd_diagnose_rom_test[] = { 0xD4, 0x00, 0x01 };
61  const byte_t pncmd_diagnose_ram_test[] = { 0xD4, 0x00, 0x02 };
62 
63  if (argc > 1) {
64  errx (1, "usage: %s", argv[0]);
65  }
66  // Display libnfc version
67  acLibnfcVersion = nfc_version ();
68  printf ("%s use libnfc %s\n", argv[0], acLibnfcVersion);
69 
70  if (!(pnddDevices = malloc (MAX_DEVICE_COUNT * sizeof (*pnddDevices)))) {
71  fprintf (stderr, "malloc() failed\n");
72  return EXIT_FAILURE;
73  }
74 
75  nfc_list_devices (pnddDevices, MAX_DEVICE_COUNT, &szFound);
76 
77  if (szFound == 0) {
78  printf ("No NFC device found.\n");
79  }
80 
81  for (i = 0; i < szFound; i++) {
82  pnd = nfc_connect (&(pnddDevices[i]));
83 
84  if (pnd == NULL) {
85  ERR ("%s", "Unable to connect to NFC device.");
86  return EXIT_FAILURE;
87  }
88 
89  printf ("NFC device [%s] connected.\n", pnd->acName);
90 
91  result = pn53x_transceive (pnd, pncmd_diagnose_communication_line_test, sizeof (pncmd_diagnose_communication_line_test), abtRx, &szRx);
92  if (result) {
93  result = (memcmp (pncmd_diagnose_communication_line_test + 2, abtRx, sizeof (pncmd_diagnose_communication_line_test) - 2) == 0);
94  }
95  printf (" Communication line test: %s\n", result ? "OK" : "Failed");
96 
97  result = pn53x_transceive (pnd, pncmd_diagnose_rom_test, sizeof (pncmd_diagnose_rom_test), abtRx, &szRx);
98  if (result) {
99  result = ((szRx == 1) && (abtRx[0] == 0x00));
100  }
101  printf (" ROM test: %s\n", result ? "OK" : "Failed");
102 
103  result = pn53x_transceive (pnd, pncmd_diagnose_ram_test, sizeof (pncmd_diagnose_ram_test), abtRx, &szRx);
104  if (result) {
105  result = ((szRx == 1) && (abtRx[0] == 0x00));
106  }
107  printf (" RAM test: %s\n", result ? "OK" : "Failed");
108  }
109 }