pn53x-diagnose.c

Go to the documentation of this file.
00001 /*-
00002  * Public platform independent Near Field Communication (NFC) library
00003  * 
00004  * Copyright (C) 2010, Romuald Conty
00005  * 
00006  * This program is free software: you can redistribute it and/or modify it
00007  * under the terms of the GNU Lesser General Public License as published by the
00008  * Free Software Foundation, either version 3 of the License, or (at your
00009  * option) any later version.
00010  * 
00011  * This program is distributed in the hope that it will be useful, but WITHOUT
00012  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00013  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
00014  * more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public License
00017  * along with this program.  If not, see <http://www.gnu.org/licenses/>
00018  */
00019 
00024 #include <err.h>
00025 #include <stdlib.h>
00026 #include <string.h>
00027 
00028 #include <nfc/nfc.h>
00029 #include <nfc/nfc-messages.h>
00030 
00031 #include "nfc-utils.h"
00032 // FIXME: Delete me
00033 #include "chips/pn53x.h"
00034 
00035 #define MAX_DEVICE_COUNT 16
00036 
00037 
00038 int
00039 main (int argc, const char *argv[])
00040 {
00041   size_t  szFound;
00042   size_t  i;
00043   nfc_device_t *pnd;
00044   nfc_device_desc_t *pnddDevices;
00045   const char *acLibnfcVersion;
00046   bool    result;
00047 
00048   byte_t  abtRx[MAX_FRAME_LEN];
00049   size_t  szRxLen;
00050   const byte_t pncmd_diagnose_communication_line_test[] = { 0xD4, 0x00, 0x00, 0x06, 'l', 'i', 'b', 'n', 'f', 'c' };
00051   const byte_t pncmd_diagnose_rom_test[] = { 0xD4, 0x00, 0x01 };
00052   const byte_t pncmd_diagnose_ram_test[] = { 0xD4, 0x00, 0x02 };
00053 
00054   if (argc > 1) {
00055     errx (1, "usage: %s", argv[0]);
00056   }
00057   // Display libnfc version
00058   acLibnfcVersion = nfc_version ();
00059   printf ("%s use libnfc %s\n", argv[0], acLibnfcVersion);
00060 
00061   if (!(pnddDevices = malloc (MAX_DEVICE_COUNT * sizeof (*pnddDevices)))) {
00062     fprintf (stderr, "malloc() failed\n");
00063     return EXIT_FAILURE;
00064   }
00065 
00066   nfc_list_devices (pnddDevices, MAX_DEVICE_COUNT, &szFound);
00067 
00068   if (szFound == 0) {
00069     INFO ("%s", "No device found.");
00070   }
00071 
00072   for (i = 0; i < szFound; i++) {
00073     pnd = nfc_connect (&(pnddDevices[i]));
00074 
00075     if (pnd == NULL) {
00076       ERR ("%s", "Unable to connect to NFC device.");
00077       return EXIT_FAILURE;
00078     }
00079 
00080     printf ("NFC device [%s] connected.\n", pnd->acName);
00081 
00082     // FIXME: Direct call
00083     result =
00084       pn53x_transceive (pnd, pncmd_diagnose_communication_line_test, sizeof (pncmd_diagnose_communication_line_test),
00085                         abtRx, &szRxLen);
00086     if (result) {
00087       result =
00088         (memcmp (pncmd_diagnose_communication_line_test + 2, abtRx, sizeof (pncmd_diagnose_communication_line_test) - 2)
00089          == 0);
00090     }
00091     printf (" Communication line test: %s\n", result ? "OK" : "Failed");
00092 
00093     // FIXME: Direct call
00094     result = pn53x_transceive (pnd, pncmd_diagnose_rom_test, sizeof (pncmd_diagnose_rom_test), abtRx, &szRxLen);
00095     if (result) {
00096       result = ((szRxLen == 1) && (abtRx[0] == 0x00));
00097     }
00098     printf (" ROM test: %s\n", result ? "OK" : "Failed");
00099 
00100     // FIXME: Direct call
00101     result = pn53x_transceive (pnd, pncmd_diagnose_ram_test, sizeof (pncmd_diagnose_ram_test), abtRx, &szRxLen);
00102     if (result) {
00103       result = ((szRxLen == 1) && (abtRx[0] == 0x00));
00104     }
00105     printf (" RAM test: %s\n", result ? "OK" : "Failed");
00106   }
00107 }