libnfc
1.4.2
|
00001 /*- 00002 * Public platform independent Near Field Communication (NFC) library examples 00003 * 00004 * Copyright (C) 2010, Romuald Conty 00005 * 00006 * Redistribution and use in source and binary forms, with or without 00007 * modification, are permitted provided that the following conditions are met: 00008 * 1) Redistributions of source code must retain the above copyright notice, 00009 * this list of conditions and the following disclaimer. 00010 * 2 )Redistributions in binary form must reproduce the above copyright 00011 * notice, this list of conditions and the following disclaimer in the 00012 * documentation and/or other materials provided with the distribution. 00013 * 00014 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00015 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00016 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00017 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 00018 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00019 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00020 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00021 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00022 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00023 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00024 * POSSIBILITY OF SUCH DAMAGE. 00025 * 00026 * Note that this license only applies on the examples, NFC library itself is under LGPL 00027 * 00028 */ 00029 00034 #include <err.h> 00035 #include <stdlib.h> 00036 #include <string.h> 00037 00038 #include <nfc/nfc.h> 00039 #include <nfc/nfc-messages.h> 00040 00041 #include "nfc-utils.h" 00042 #include "chips/pn53x.h" 00043 00044 #define MAX_DEVICE_COUNT 16 00045 00046 00047 int 00048 main (int argc, const char *argv[]) 00049 { 00050 size_t szFound; 00051 size_t i; 00052 nfc_device_t *pnd; 00053 nfc_device_desc_t *pnddDevices; 00054 const char *acLibnfcVersion; 00055 bool result; 00056 00057 byte_t abtRx[PN53x_EXTENDED_FRAME_MAX_LEN]; 00058 size_t szRx; 00059 const byte_t pncmd_diagnose_communication_line_test[] = { 0xD4, 0x00, 0x00, 0x06, 'l', 'i', 'b', 'n', 'f', 'c' }; 00060 const byte_t pncmd_diagnose_rom_test[] = { 0xD4, 0x00, 0x01 }; 00061 const byte_t pncmd_diagnose_ram_test[] = { 0xD4, 0x00, 0x02 }; 00062 00063 if (argc > 1) { 00064 errx (1, "usage: %s", argv[0]); 00065 } 00066 // Display libnfc version 00067 acLibnfcVersion = nfc_version (); 00068 printf ("%s use libnfc %s\n", argv[0], acLibnfcVersion); 00069 00070 if (!(pnddDevices = malloc (MAX_DEVICE_COUNT * sizeof (*pnddDevices)))) { 00071 fprintf (stderr, "malloc() failed\n"); 00072 return EXIT_FAILURE; 00073 } 00074 00075 nfc_list_devices (pnddDevices, MAX_DEVICE_COUNT, &szFound); 00076 00077 if (szFound == 0) { 00078 printf ("No NFC device found.\n"); 00079 } 00080 00081 for (i = 0; i < szFound; i++) { 00082 pnd = nfc_connect (&(pnddDevices[i])); 00083 00084 if (pnd == NULL) { 00085 ERR ("%s", "Unable to connect to NFC device."); 00086 return EXIT_FAILURE; 00087 } 00088 00089 printf ("NFC device [%s] connected.\n", pnd->acName); 00090 00091 result = pn53x_transceive (pnd, pncmd_diagnose_communication_line_test, sizeof (pncmd_diagnose_communication_line_test), abtRx, &szRx); 00092 if (result) { 00093 result = (memcmp (pncmd_diagnose_communication_line_test + 2, abtRx, sizeof (pncmd_diagnose_communication_line_test) - 2) == 0); 00094 } 00095 printf (" Communication line test: %s\n", result ? "OK" : "Failed"); 00096 00097 result = pn53x_transceive (pnd, pncmd_diagnose_rom_test, sizeof (pncmd_diagnose_rom_test), abtRx, &szRx); 00098 if (result) { 00099 result = ((szRx == 1) && (abtRx[0] == 0x00)); 00100 } 00101 printf (" ROM test: %s\n", result ? "OK" : "Failed"); 00102 00103 result = pn53x_transceive (pnd, pncmd_diagnose_ram_test, sizeof (pncmd_diagnose_ram_test), abtRx, &szRx); 00104 if (result) { 00105 result = ((szRx == 1) && (abtRx[0] == 0x00)); 00106 } 00107 printf (" RAM test: %s\n", result ? "OK" : "Failed"); 00108 } 00109 }