libnfc  1.4.2
nfc-dep-initiator.c
Go to the documentation of this file.
1 /*-
2  * Public platform independent Near Field Communication (NFC) library examples
3  *
4  * Copyright (C) 2009, Roel Verdult
5  * Copyright (C) 2010, Romuald Conty
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  * 1) Redistributions of source code must retain the above copyright notice,
10  * this list of conditions and the following disclaimer.
11  * 2 )Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
19  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25  * POSSIBILITY OF SUCH DAMAGE.
26  *
27  * Note that this license only applies on the examples, NFC library itself is under LGPL
28  *
29  */
30 
36 #ifdef HAVE_CONFIG_H
37 # include "config.h"
38 #endif // HAVE_CONFIG_H
39 
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 
44 #include <nfc/nfc.h>
45 
46 #include "nfc-utils.h"
47 
48 #define MAX_FRAME_LEN 264
49 
50 int
51 main (int argc, const char *argv[])
52 {
53  nfc_device_t *pnd;
54  nfc_target_t nt;
55  byte_t abtRx[MAX_FRAME_LEN];
56  size_t szRx;
57  byte_t abtTx[] = "Hello World!";
58 
59  if (argc > 1) {
60  printf ("Usage: %s\n", argv[0]);
61  return EXIT_FAILURE;
62  }
63 
64  pnd = nfc_connect (NULL);
65  if (!pnd) {
66  printf("Unable to connect to NFC device.\n");
67  return EXIT_FAILURE;
68  }
69  printf ("Connected to NFC device: %s\n", pnd->acName);
70 
71  if (!nfc_initiator_init (pnd)) {
72  nfc_perror(pnd, "nfc_initiator_init");
73  return EXIT_FAILURE;
74  }
75 
76  if(!nfc_initiator_select_dep_target (pnd, NDM_PASSIVE, NBR_212, NULL, &nt)) {
77  nfc_perror(pnd, "nfc_initiator_select_dep_target");
78  return EXIT_FAILURE;
79  }
80  print_nfc_target (nt, false);
81 
82  printf ("Sending: %s\n", abtTx);
83  if (!nfc_initiator_transceive_bytes (pnd, abtTx, sizeof(abtTx), abtRx, &szRx)) {
84  nfc_perror(pnd, "nfc_initiator_transceive_bytes");
85  return EXIT_FAILURE;
86  }
87 
88  abtRx[szRx] = 0;
89  printf ("Received: %s\n", abtRx);
90 
91  nfc_initiator_deselect_target (pnd);
92  nfc_disconnect (pnd);
93  return EXIT_SUCCESS;
94 }