libnfc  1.7.1
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  * See AUTHORS file for a more comprehensive list of contributors.
11  * Additional contributors of this file:
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions are met:
15  * 1) Redistributions of source code must retain the above copyright notice,
16  * this list of conditions and the following disclaimer.
17  * 2 )Redistributions in binary form must reproduce the above copyright
18  * notice, this list of conditions and the following disclaimer in the
19  * documentation and/or other materials provided with the distribution.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  *
33  * Note that this license only applies on the examples, NFC library itself is under LGPL
34  *
35  */
36 
42 #ifdef HAVE_CONFIG_H
43 # include "config.h"
44 #endif // HAVE_CONFIG_H
45 
46 #include <stdio.h>
47 #include <stdlib.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  uint8_t abtRx[MAX_FRAME_LEN];
74  int szRx;
75  uint8_t abtTx[] = "Hello Mars!";
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 #define MAX_DEVICE_COUNT 2
88  nfc_connstring connstrings[MAX_DEVICE_COUNT];
89  size_t szDeviceFound = nfc_list_devices(context, connstrings, MAX_DEVICE_COUNT);
90  // Little hack to allow using nfc-dep-initiator & nfc-dep-target from
91  // the same machine: if there is more than one readers opened
92  // nfc-dep-target will open the second reader
93  // (we hope they're always detected in the same order)
94  if (szDeviceFound == 1) {
95  pnd = nfc_open(context, connstrings[0]);
96  } else if (szDeviceFound > 1) {
97  pnd = nfc_open(context, connstrings[1]);
98  } else {
99  printf("No device found.\n");
100  nfc_exit(context);
101  exit(EXIT_FAILURE);
102  }
103 
104  nfc_target nt = {
105  .nm = {
106  .nmt = NMT_DEP,
107  .nbr = NBR_UNDEFINED
108  },
109  .nti = {
110  .ndi = {
111  .abtNFCID3 = { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xff, 0x00, 0x00 },
112  .szGB = 4,
113  .abtGB = { 0x12, 0x34, 0x56, 0x78 },
114  .ndm = NDM_UNDEFINED,
115  /* These bytes are not used by nfc_target_init: the chip will provide them automatically to the initiator */
116  .btDID = 0x00,
117  .btBS = 0x00,
118  .btBR = 0x00,
119  .btTO = 0x00,
120  .btPP = 0x01,
121  },
122  },
123  };
124 
125  if (pnd == NULL) {
126  printf("Unable to open NFC device.\n");
127  nfc_exit(context);
128  exit(EXIT_FAILURE);
129  }
130  printf("NFC device: %s opened\n", nfc_device_get_name(pnd));
131 
132  signal(SIGINT, stop_dep_communication);
133 
134  printf("NFC device will now act as: ");
135  print_nfc_target(&nt, false);
136 
137  printf("Waiting for initiator request...\n");
138  if ((szRx = nfc_target_init(pnd, &nt, abtRx, sizeof(abtRx), 0)) < 0) {
139  nfc_perror(pnd, "nfc_target_init");
140  nfc_close(pnd);
141  nfc_exit(context);
142  exit(EXIT_FAILURE);
143  }
144 
145  printf("Initiator request received. Waiting for data...\n");
146  if ((szRx = nfc_target_receive_bytes(pnd, abtRx, sizeof(abtRx), 0)) < 0) {
147  nfc_perror(pnd, "nfc_target_receive_bytes");
148  nfc_close(pnd);
149  nfc_exit(context);
150  exit(EXIT_FAILURE);
151  }
152  abtRx[(size_t) szRx] = '\0';
153  printf("Received: %s\n", abtRx);
154 
155  printf("Sending: %s\n", abtTx);
156  if (nfc_target_send_bytes(pnd, abtTx, sizeof(abtTx), 0) < 0) {
157  nfc_perror(pnd, "nfc_target_send_bytes");
158  nfc_close(pnd);
159  nfc_exit(context);
160  exit(EXIT_FAILURE);
161  }
162  printf("Data sent.\n");
163 
164  nfc_close(pnd);
165  nfc_exit(context);
166  exit(EXIT_SUCCESS);
167 }
nfc_init
void nfc_init(nfc_context **context)
Initialize libnfc. This function must be called before calling any other libnfc function.
Definition: nfc.c:192
nfc_context
NFC library context Struct which contains internal options, references, pointers, etc....
Definition: nfc-internal.h:175
nfc_device
NFC device information.
Definition: nfc-internal.h:190
nfc_target_init
int nfc_target_init(nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, const size_t szRx, int timeout)
Initialize NFC device as an emulated tag.
Definition: nfc.c:933
nfc_exit
void nfc_exit(nfc_context *context)
Deinitialize libnfc. Should be called after closing all open devices and before your application term...
Definition: nfc.c:209
nfc_target
NFC target structure.
Definition: nfc-types.h:328
nfc_connstring
char nfc_connstring[NFC_BUFSIZE_CONNSTRING]
Definition: nfc-types.h:62
nfc_device_get_name
const char * nfc_device_get_name(nfc_device *pnd)
Returns the device name.
Definition: nfc.c:1164
nfc_open
nfc_device * nfc_open(nfc_context *context, const nfc_connstring connstring)
Open a NFC device.
Definition: nfc.c:238
nfc_perror
void nfc_perror(const nfc_device *pnd, const char *pcString)
Display the last error occured on a nfc_device.
Definition: nfc.c:1138
nfc_list_devices
size_t nfc_list_devices(nfc_context *context, nfc_connstring connstrings[], const size_t connstrings_len)
Scan for discoverable supported devices (ie. only available for some drivers)
Definition: nfc.c:317
ERR
#define ERR(...)
Print a error message.
Definition: nfc-utils.h:85
nfc_abort_command
int nfc_abort_command(nfc_device *pnd)
Abort current running command.
Definition: nfc.c:991
nfc_close
void nfc_close(nfc_device *pnd)
Close from a NFC device.
Definition: nfc.c:300
nfc_target_receive_bytes
int nfc_target_receive_bytes(nfc_device *pnd, uint8_t *pbtRx, const size_t szRx, int timeout)
Receive bytes and APDU frames.
Definition: nfc.c:1032
nfc-utils.h
Provide some examples shared functions like print, parity calculation, options parsing.
nfc_target_send_bytes
int nfc_target_send_bytes(nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, int timeout)
Send bytes and APDU frames.
Definition: nfc.c:1012
nfc.h
libnfc interface