libnfc 1.4.2
nfc-dep-target.c
Go to the documentation of this file.
00001 /*-
00002  * Public platform independent Near Field Communication (NFC) library examples
00003  * 
00004  * Copyright (C) 2009, Roel Verdult
00005  * Copyright (C) 2010, Romuald Conty
00006  * 
00007  * Redistribution and use in source and binary forms, with or without
00008  * modification, are permitted provided that the following conditions are met:
00009  *  1) Redistributions of source code must retain the above copyright notice,
00010  *  this list of conditions and the following disclaimer. 
00011  *  2 )Redistributions in binary form must reproduce the above copyright
00012  *  notice, this list of conditions and the following disclaimer in the
00013  *  documentation and/or other materials provided with the distribution.
00014  *
00015  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00016  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00017  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00018  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
00019  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00020  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00021  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00022  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00023  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00024  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00025  * POSSIBILITY OF SUCH DAMAGE.
00026  * 
00027  * Note that this license only applies on the examples, NFC library itself is under LGPL
00028  *
00029  */
00030 
00036 #ifdef HAVE_CONFIG_H
00037 #  include "config.h"
00038 #endif // HAVE_CONFIG_H
00039 
00040 #include <stdio.h>
00041 #include <stdlib.h>
00042 
00043 #include <nfc/nfc.h>
00044 
00045 #include "nfc-utils.h"
00046 
00047 #define MAX_FRAME_LEN 264
00048 
00049 int
00050 main (int argc, const char *argv[])
00051 {
00052   byte_t  abtRx[MAX_FRAME_LEN];
00053   size_t  szRx;
00054   size_t  szDeviceFound;
00055   byte_t  abtTx[] = "Hello Mars!";
00056   nfc_device_t *pnd;
00057   #define MAX_DEVICE_COUNT 2
00058   nfc_device_desc_t pnddDevices[MAX_DEVICE_COUNT];
00059   nfc_list_devices (pnddDevices, MAX_DEVICE_COUNT, &szDeviceFound);
00060   // Little hack to allow using nfc-dep-initiator & nfc-dep-target from
00061   // the same machine: if there is more than one readers connected
00062   // nfc-dep-target will connect to the second reader
00063   // (we hope they're always detected in the same order)
00064   if (szDeviceFound == 1) {
00065     pnd = nfc_connect (&(pnddDevices[0]));
00066   } else if (szDeviceFound > 1) {
00067     pnd = nfc_connect (&(pnddDevices[1]));
00068   } else {
00069     printf("No device found.");
00070     return EXIT_FAILURE;
00071   }
00072 
00073   if (argc > 1) {
00074     printf ("Usage: %s\n", argv[0]);
00075     return EXIT_FAILURE;
00076   }
00077 
00078   nfc_target_t nt = {
00079     .nm.nmt = NMT_DEP,
00080     .nm.nbr = NBR_UNDEFINED, // Will be updated by nfc_target_init
00081     .nti.ndi.abtNFCID3 = { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xff, 0x00, 0x00 },
00082     .nti.ndi.szGB = 4,
00083     .nti.ndi.abtGB = { 0x12, 0x34, 0x56, 0x78 },
00084     /* These bytes are not used by nfc_target_init: the chip will provide them automatically to the initiator */
00085     .nti.ndi.btDID = 0x00,
00086     .nti.ndi.btBS = 0x00,
00087     .nti.ndi.btBR = 0x00,
00088     .nti.ndi.btTO = 0x00,
00089     .nti.ndi.btPP = 0x01,
00090   };
00091 
00092   if (!pnd) {
00093     printf("Unable to connect to NFC device.\n");
00094     return EXIT_FAILURE;
00095   }
00096   printf ("Connected to NFC device: %s\n", pnd->acName);
00097 
00098   printf ("NFC device will now act as: ");
00099   print_nfc_target (nt, false);
00100 
00101   printf ("Waiting for initiator request...\n");
00102   if(!nfc_target_init (pnd, &nt, abtRx, &szRx)) {
00103     nfc_perror(pnd, "nfc_target_init");
00104     return EXIT_FAILURE;
00105   }
00106 
00107   printf("Initiator request received. Waiting for data...\n");
00108   if (!nfc_target_receive_bytes (pnd, abtRx, &szRx)) {
00109     nfc_perror(pnd, "nfc_target_receive_bytes");
00110     return EXIT_FAILURE;
00111   }
00112   abtRx[szRx] = '\0';
00113   printf ("Received: %s\n", abtRx);
00114 
00115   printf ("Sending: %s\n", abtTx);
00116   if (!nfc_target_send_bytes (pnd, abtTx, sizeof(abtTx))) {
00117     nfc_perror(pnd, "nfc_target_send_bytes");
00118     return EXIT_FAILURE;
00119   }
00120   printf("Data sent.\n");
00121 
00122   nfc_disconnect (pnd);
00123   return EXIT_SUCCESS;
00124 }