libnfc  1.7.0-rc7
nfc-dep-initiator.c
Go to the documentation of this file.
1 /*-
2  * Free/Libre Near Field Communication (NFC) library
3  *
4  * Libnfc historical contributors:
5  * Copyright (C) 2009 Roel Verdult
6  * Copyright (C) 2009-2013 Romuald Conty
7  * Copyright (C) 2010-2012 Romain Tartière
8  * Copyright (C) 2010-2013 Philippe Teuwen
9  * Copyright (C) 2012-2013 Ludovic Rousseau
10  * Additional contributors of this file:
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions are met:
14  * 1) Redistributions of source code must retain the above copyright notice,
15  * this list of conditions and the following disclaimer.
16  * 2 )Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in the
18  * documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  *
32  * Note that this license only applies on the examples, NFC library itself is under LGPL
33  *
34  */
35 
41 #ifdef HAVE_CONFIG_H
42 # include "config.h"
43 #endif // HAVE_CONFIG_H
44 
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <signal.h>
49 
50 #include <nfc/nfc.h>
51 
52 #include "utils/nfc-utils.h"
53 
54 #define MAX_FRAME_LEN 264
55 
56 static nfc_device *pnd;
57 static nfc_context *context;
58 
59 static void stop_dep_communication(int sig)
60 {
61  (void) sig;
62  if (pnd != NULL) {
63  nfc_abort_command(pnd);
64  } else {
65  nfc_exit(context);
66  exit(EXIT_FAILURE);
67  }
68 }
69 
70 int
71 main(int argc, const char *argv[])
72 {
73  nfc_target nt;
74  uint8_t abtRx[MAX_FRAME_LEN];
75  uint8_t abtTx[] = "Hello World!";
76 
77  if (argc > 1) {
78  printf("Usage: %s\n", argv[0]);
79  exit(EXIT_FAILURE);
80  }
81 
82  nfc_init(&context);
83  if (context == NULL) {
84  ERR("Unable to init libnfc (malloc)");
85  exit(EXIT_FAILURE);
86  }
87 
88  pnd = nfc_open(context, NULL);
89  if (pnd == NULL) {
90  ERR("Unable to open NFC device.");
91  nfc_exit(context);
92  exit(EXIT_FAILURE);
93  }
94  printf("NFC device: %s\n opened", nfc_device_get_name(pnd));
95 
96  signal(SIGINT, stop_dep_communication);
97 
98  if (nfc_initiator_init(pnd) < 0) {
99  nfc_perror(pnd, "nfc_initiator_init");
100  nfc_close(pnd);
101  nfc_exit(context);
102  exit(EXIT_FAILURE);
103  }
104 
105  if (nfc_initiator_select_dep_target(pnd, NDM_PASSIVE, NBR_212, NULL, &nt, 1000) < 0) {
106  nfc_perror(pnd, "nfc_initiator_select_dep_target");
107  nfc_close(pnd);
108  nfc_exit(context);
109  exit(EXIT_FAILURE);
110  }
111  print_nfc_target(&nt, false);
112 
113  printf("Sending: %s\n", abtTx);
114  int res;
115  if ((res = nfc_initiator_transceive_bytes(pnd, abtTx, sizeof(abtTx), abtRx, sizeof(abtRx), 0)) < 0) {
116  nfc_perror(pnd, "nfc_initiator_transceive_bytes");
117  nfc_close(pnd);
118  nfc_exit(context);
119  exit(EXIT_FAILURE);
120  }
121 
122  abtRx[res] = 0;
123  printf("Received: %s\n", abtRx);
124 
125  if (nfc_initiator_deselect_target(pnd) < 0) {
126  nfc_perror(pnd, "nfc_initiator_deselect_target");
127  nfc_close(pnd);
128  nfc_exit(context);
129  exit(EXIT_FAILURE);
130  }
131 
132  nfc_close(pnd);
133  nfc_exit(context);
134  exit(EXIT_SUCCESS);
135 }