libnfc  1.7.0-rc7
nfc-dep-target.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 <signal.h>
48 
49 #include <nfc/nfc.h>
50 
51 #include "utils/nfc-utils.h"
52 
53 #define MAX_FRAME_LEN 264
54 
55 static nfc_device *pnd;
56 static nfc_context *context;
57 
58 static void stop_dep_communication(int sig)
59 {
60  (void) sig;
61  if (pnd != NULL) {
62  nfc_abort_command(pnd);
63  } else {
64  nfc_exit(context);
65  exit(EXIT_FAILURE);
66  }
67 }
68 
69 int
70 main(int argc, const char *argv[])
71 {
72  uint8_t abtRx[MAX_FRAME_LEN];
73  int szRx;
74  uint8_t abtTx[] = "Hello Mars!";
75 
76  if (argc > 1) {
77  printf("Usage: %s\n", argv[0]);
78  exit(EXIT_FAILURE);
79  }
80 
81  nfc_init(&context);
82  if (context == NULL) {
83  ERR("Unable to init libnfc (malloc)");
84  exit(EXIT_FAILURE);
85  }
86 #define MAX_DEVICE_COUNT 2
87  nfc_connstring connstrings[MAX_DEVICE_COUNT];
88  size_t szDeviceFound = nfc_list_devices(context, connstrings, MAX_DEVICE_COUNT);
89  // Little hack to allow using nfc-dep-initiator & nfc-dep-target from
90  // the same machine: if there is more than one readers opened
91  // nfc-dep-target will open the second reader
92  // (we hope they're always detected in the same order)
93  if (szDeviceFound == 1) {
94  pnd = nfc_open(context, connstrings[0]);
95  } else if (szDeviceFound > 1) {
96  pnd = nfc_open(context, connstrings[1]);
97  } else {
98  printf("No device found.\n");
99  nfc_exit(context);
100  exit(EXIT_FAILURE);
101  }
102 
103  nfc_target nt = {
104  .nm = {
105  .nmt = NMT_DEP,
106  .nbr = NBR_UNDEFINED
107  },
108  .nti = {
109  .ndi = {
110  .abtNFCID3 = { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xff, 0x00, 0x00 },
111  .szGB = 4,
112  .abtGB = { 0x12, 0x34, 0x56, 0x78 },
113  .ndm = NDM_UNDEFINED,
114  /* These bytes are not used by nfc_target_init: the chip will provide them automatically to the initiator */
115  .btDID = 0x00,
116  .btBS = 0x00,
117  .btBR = 0x00,
118  .btTO = 0x00,
119  .btPP = 0x01,
120  },
121  },
122  };
123 
124  if (pnd == NULL) {
125  printf("Unable to open NFC device.\n");
126  nfc_exit(context);
127  exit(EXIT_FAILURE);
128  }
129  printf("NFC device: %s opened\n", nfc_device_get_name(pnd));
130 
131  signal(SIGINT, stop_dep_communication);
132 
133  printf("NFC device will now act as: ");
134  print_nfc_target(&nt, false);
135 
136  printf("Waiting for initiator request...\n");
137  if ((szRx = nfc_target_init(pnd, &nt, abtRx, sizeof(abtRx), 0)) < 0) {
138  nfc_perror(pnd, "nfc_target_init");
139  nfc_close(pnd);
140  nfc_exit(context);
141  exit(EXIT_FAILURE);
142  }
143 
144  printf("Initiator request received. Waiting for data...\n");
145  if ((szRx = nfc_target_receive_bytes(pnd, abtRx, sizeof(abtRx), 0)) < 0) {
146  nfc_perror(pnd, "nfc_target_receive_bytes");
147  nfc_close(pnd);
148  nfc_exit(context);
149  exit(EXIT_FAILURE);
150  }
151  abtRx[(size_t) szRx] = '\0';
152  printf("Received: %s\n", abtRx);
153 
154  printf("Sending: %s\n", abtTx);
155  if (nfc_target_send_bytes(pnd, abtTx, sizeof(abtTx), 0) < 0) {
156  nfc_perror(pnd, "nfc_target_send_bytes");
157  nfc_close(pnd);
158  nfc_exit(context);
159  exit(EXIT_FAILURE);
160  }
161  printf("Data sent.\n");
162 
163  nfc_close(pnd);
164  nfc_exit(context);
165  exit(EXIT_SUCCESS);
166 }