From 13d0bb7b0f1dee99661df040b1d5d99582eae55d Mon Sep 17 00:00:00 2001 From: Romuald Conty Date: Fri, 8 Oct 2010 18:15:00 +0000 Subject: [PATCH] nfcip-*: code clean up, enhance output to understand what happens. --- examples/nfcip-initiator.c | 42 ++++++++++++++--------- examples/nfcip-target.c | 68 ++++++++++++++++++++++---------------- 2 files changed, 66 insertions(+), 44 deletions(-) diff --git a/examples/nfcip-initiator.c b/examples/nfcip-initiator.c index 177930f..34e6773 100644 --- a/examples/nfcip-initiator.c +++ b/examples/nfcip-initiator.c @@ -19,15 +19,15 @@ /** * @file nfcip-initiator.c - * @brief + * @brief Turns the NFC device into a D.E.P. initiator (see NFCIP-1) */ #ifdef HAVE_CONFIG_H # include "config.h" #endif // HAVE_CONFIG_H -#include #include +#include #include #include @@ -38,31 +38,41 @@ main (int argc, const char *argv[]) { nfc_device_t *pnd; nfc_target_info_t ti; - byte_t abtRecv[MAX_FRAME_LEN]; - size_t szRecvBits; + byte_t abtRx[MAX_FRAME_LEN]; + size_t szRx; byte_t send[] = "Hello World!"; if (argc > 1) { - errx (1, "usage: %s", argv[0]); + printf ("Usage: %s\n", argv[0]); + return EXIT_FAILURE; } pnd = nfc_connect (NULL); - if (!pnd || !nfc_initiator_init (pnd) - || !nfc_initiator_select_dep_target (pnd, NM_PASSIVE_DEP, NULL, 0, NULL, 0, NULL, 0, &ti)) { - printf ("unable to connect, initialize, or select the target\n"); - return 1; + if (!pnd) { + printf("Unable to connect to NFC device.\n"); + return EXIT_FAILURE; } - printf ("Sending : %s\n", send); - if (!nfc_initiator_transceive_bytes (pnd, send, strlen ((char *) send), abtRecv, &szRecvBits)) { - printf ("unable to send data\n"); - return 1; + if (!nfc_initiator_init (pnd)) { + nfc_perror(pnd, "nfc_initiator_init"); + return EXIT_FAILURE; } - abtRecv[szRecvBits] = 0; - printf ("Received: %s\n", abtRecv); + if(!nfc_initiator_select_dep_target (pnd, NM_PASSIVE_DEP, NULL, 0, NULL, 0, NULL, 0, &ti)) { + nfc_perror(pnd, "nfc_initiator_select_dep_target"); + return EXIT_FAILURE; + } + + printf ("Sending: %s\n", send); + if (!nfc_initiator_transceive_bytes (pnd, send, strlen ((char *) send), abtRx, &szRx)) { + nfc_perror(pnd, "nfc_initiator_transceive_bytes"); + return EXIT_FAILURE; + } + + abtRx[szRx] = 0; + printf ("Received: %s\n", abtRx); nfc_initiator_deselect_target (pnd); nfc_disconnect (pnd); - return 0; + return EXIT_SUCCESS; } diff --git a/examples/nfcip-target.c b/examples/nfcip-target.c index e255ecb..a8d25f2 100644 --- a/examples/nfcip-target.c +++ b/examples/nfcip-target.c @@ -15,20 +15,20 @@ * * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see - * */ /** * @file nfcip-target.c - * @brief + * @brief Turns the NFC device into a D.E.P. target (see NFCIP-1) */ #ifdef HAVE_CONFIG_H # include "config.h" #endif // HAVE_CONFIG_H -#include #include +#include + #include #define MAX_FRAME_LEN 264 @@ -36,27 +36,30 @@ int main (int argc, const char *argv[]) { - byte_t abtRecv[MAX_FRAME_LEN]; - size_t szRecvBits; + byte_t abtRx[MAX_FRAME_LEN]; + size_t szRx; size_t szDeviceFound; - byte_t send[] = "Hello Mars!"; + byte_t abtTx[] = "Hello Mars!"; nfc_device_t *pnd; #define MAX_DEVICE_COUNT 2 nfc_device_desc_t pnddDevices[MAX_DEVICE_COUNT]; nfc_list_devices (pnddDevices, MAX_DEVICE_COUNT, &szDeviceFound); - //# Little hack to allow using nfcip-initiator & nfcip-target from - //# the same machine: if there is more than one readers connected - //# nfcip-target will connect to the second reader - //# (we hope they're always detected in the same order) + // Little hack to allow using nfcip-initiator & nfcip-target from + // the same machine: if there is more than one readers connected + // nfcip-target will connect to the second reader + // (we hope they're always detected in the same order) if (szDeviceFound == 1) { - pnd = nfc_connect (&(pnddDevices[0])); - } - else if (szDeviceFound > 1) { - pnd = nfc_connect (&(pnddDevices[1])); + pnd = nfc_connect (&(pnddDevices[0])); + } else if (szDeviceFound > 1) { + pnd = nfc_connect (&(pnddDevices[1])); + } else { + printf("No device found."); + return EXIT_FAILURE; } if (argc > 1) { - errx (1, "usage: %s", argv[0]); + printf ("Usage: %s\n", argv[0]); + return EXIT_FAILURE; } // Note: We have to build a "fake" nfc_target_t in order to do exactly the same that was done before the new nfc_target_init() was introduced. @@ -69,24 +72,33 @@ main (int argc, const char *argv[]) .nti.nai.szAtsLen = 0, }; - if (!pnd || !nfc_target_init (pnd, NTM_DEP, nt, abtRecv, &szRecvBits)) { - printf ("unable to connect or initialize\n"); - return 1; + if (!pnd) { + printf("Unable to connect to NFC device.\n"); + return EXIT_FAILURE; + } + printf ("Connected to NFC device: %s\n", pnd->acName); + + printf ("Waiting for initiator request...\n"); + if(!nfc_target_init (pnd, NTM_DEP, nt, abtRx, &szRx)) { + nfc_perror(pnd, "nfc_target_init"); + return EXIT_FAILURE; } - if (!nfc_target_receive_bytes (pnd, abtRecv, &szRecvBits)) { - printf ("unable to receive data\n"); - return 1; + printf("Initiator request received. Waiting for data...\n"); + if (!nfc_target_receive_bytes (pnd, abtRx, &szRx)) { + nfc_perror(pnd, "nfc_target_receive_bytes"); + return EXIT_FAILURE; } - abtRecv[szRecvBits] = 0; - printf ("Received: %s\n", abtRecv); - printf ("Sending : %s\n", send); + abtRx[szRx] = '\0'; + printf ("Received: %s\n", abtRx); - if (!nfc_target_send_bytes (pnd, send, 11)) { - printf ("unable to send data\n"); - return 1; + printf ("Sending: %s\n", abtTx); + if (!nfc_target_send_bytes (pnd, abtTx, sizeof(abtTx))) { + nfc_perror(pnd, "nfc_target_send_bytes"); + return EXIT_FAILURE; } + printf("Data sent.\n"); nfc_disconnect (pnd); - return 0; + return EXIT_SUCCESS; }