libnfc 1.4.2
nfc-dep-initiator.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 #include <string.h>
00043 
00044 #include <nfc/nfc.h>
00045 
00046 #include "nfc-utils.h"
00047 
00048 #define MAX_FRAME_LEN 264
00049 
00050 int
00051 main (int argc, const char *argv[])
00052 {
00053   nfc_device_t *pnd;
00054   nfc_target_t nt;
00055   byte_t  abtRx[MAX_FRAME_LEN];
00056   size_t  szRx;
00057   byte_t  abtTx[] = "Hello World!";
00058 
00059   if (argc > 1) {
00060     printf ("Usage: %s\n", argv[0]);
00061     return EXIT_FAILURE;
00062   }
00063 
00064   pnd = nfc_connect (NULL);
00065   if (!pnd) {
00066     printf("Unable to connect to NFC device.\n");
00067     return EXIT_FAILURE;
00068   }
00069   printf ("Connected to NFC device: %s\n", pnd->acName);
00070 
00071   if (!nfc_initiator_init (pnd)) {
00072     nfc_perror(pnd, "nfc_initiator_init");
00073     return EXIT_FAILURE;
00074   }
00075 
00076   if(!nfc_initiator_select_dep_target (pnd, NDM_PASSIVE, NBR_212, NULL, &nt)) {
00077     nfc_perror(pnd, "nfc_initiator_select_dep_target");
00078     return EXIT_FAILURE;
00079   }
00080   print_nfc_target (nt, false);
00081 
00082   printf ("Sending: %s\n", abtTx);
00083   if (!nfc_initiator_transceive_bytes (pnd, abtTx, sizeof(abtTx), abtRx, &szRx)) {
00084     nfc_perror(pnd, "nfc_initiator_transceive_bytes");
00085     return EXIT_FAILURE;
00086   }
00087 
00088   abtRx[szRx] = 0;
00089   printf ("Received: %s\n", abtRx);
00090 
00091   nfc_initiator_deselect_target (pnd);
00092   nfc_disconnect (pnd);
00093   return EXIT_SUCCESS;
00094 }