libnfc  1.4.2
nfc-dep-target.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 
43 #include <nfc/nfc.h>
44 
45 #include "nfc-utils.h"
46 
47 #define MAX_FRAME_LEN 264
48 
49 int
50 main (int argc, const char *argv[])
51 {
52  byte_t abtRx[MAX_FRAME_LEN];
53  size_t szRx;
54  size_t szDeviceFound;
55  byte_t abtTx[] = "Hello Mars!";
56  nfc_device_t *pnd;
57  #define MAX_DEVICE_COUNT 2
58  nfc_device_desc_t pnddDevices[MAX_DEVICE_COUNT];
59  nfc_list_devices (pnddDevices, MAX_DEVICE_COUNT, &szDeviceFound);
60  // Little hack to allow using nfc-dep-initiator & nfc-dep-target from
61  // the same machine: if there is more than one readers connected
62  // nfc-dep-target will connect to the second reader
63  // (we hope they're always detected in the same order)
64  if (szDeviceFound == 1) {
65  pnd = nfc_connect (&(pnddDevices[0]));
66  } else if (szDeviceFound > 1) {
67  pnd = nfc_connect (&(pnddDevices[1]));
68  } else {
69  printf("No device found.");
70  return EXIT_FAILURE;
71  }
72 
73  if (argc > 1) {
74  printf ("Usage: %s\n", argv[0]);
75  return EXIT_FAILURE;
76  }
77 
78  nfc_target_t nt = {
79  .nm.nmt = NMT_DEP,
80  .nm.nbr = NBR_UNDEFINED, // Will be updated by nfc_target_init
81  .nti.ndi.abtNFCID3 = { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xff, 0x00, 0x00 },
82  .nti.ndi.szGB = 4,
83  .nti.ndi.abtGB = { 0x12, 0x34, 0x56, 0x78 },
84  /* These bytes are not used by nfc_target_init: the chip will provide them automatically to the initiator */
85  .nti.ndi.btDID = 0x00,
86  .nti.ndi.btBS = 0x00,
87  .nti.ndi.btBR = 0x00,
88  .nti.ndi.btTO = 0x00,
89  .nti.ndi.btPP = 0x01,
90  };
91 
92  if (!pnd) {
93  printf("Unable to connect to NFC device.\n");
94  return EXIT_FAILURE;
95  }
96  printf ("Connected to NFC device: %s\n", pnd->acName);
97 
98  printf ("NFC device will now act as: ");
99  print_nfc_target (nt, false);
100 
101  printf ("Waiting for initiator request...\n");
102  if(!nfc_target_init (pnd, &nt, abtRx, &szRx)) {
103  nfc_perror(pnd, "nfc_target_init");
104  return EXIT_FAILURE;
105  }
106 
107  printf("Initiator request received. Waiting for data...\n");
108  if (!nfc_target_receive_bytes (pnd, abtRx, &szRx)) {
109  nfc_perror(pnd, "nfc_target_receive_bytes");
110  return EXIT_FAILURE;
111  }
112  abtRx[szRx] = '\0';
113  printf ("Received: %s\n", abtRx);
114 
115  printf ("Sending: %s\n", abtTx);
116  if (!nfc_target_send_bytes (pnd, abtTx, sizeof(abtTx))) {
117  nfc_perror(pnd, "nfc_target_send_bytes");
118  return EXIT_FAILURE;
119  }
120  printf("Data sent.\n");
121 
122  nfc_disconnect (pnd);
123  return EXIT_SUCCESS;
124 }