libnfc 1.8.0
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 * 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 <string.h>
49#include <signal.h>
50
51#include <nfc/nfc.h>
52
53#include "utils/nfc-utils.h"
54
55#define MAX_FRAME_LEN 264
56
57static nfc_device *pnd;
58static nfc_context *context;
59
60static void stop_dep_communication(int sig)
61{
62 (void) sig;
63 if (pnd != NULL) {
65 } else {
66 nfc_exit(context);
67 exit(EXIT_FAILURE);
68 }
69}
70
71int
72main(int argc, const char *argv[])
73{
74 nfc_target nt;
75 uint8_t abtRx[MAX_FRAME_LEN];
76 uint8_t abtTx[] = "Hello World!";
77
78 if (argc > 1) {
79 printf("Usage: %s\n", argv[0]);
80 exit(EXIT_FAILURE);
81 }
82
83 nfc_init(&context);
84 if (context == NULL) {
85 ERR("Unable to init libnfc (malloc)");
86 exit(EXIT_FAILURE);
87 }
88
89 pnd = nfc_open(context, NULL);
90 if (pnd == NULL) {
91 ERR("Unable to open NFC device.");
92 nfc_exit(context);
93 exit(EXIT_FAILURE);
94 }
95 printf("NFC device: %s\n opened", nfc_device_get_name(pnd));
96
97 signal(SIGINT, stop_dep_communication);
98
99 if (nfc_initiator_init(pnd) < 0) {
100 nfc_perror(pnd, "nfc_initiator_init");
101 nfc_close(pnd);
102 nfc_exit(context);
103 exit(EXIT_FAILURE);
104 }
105
106 if (nfc_initiator_select_dep_target(pnd, NDM_PASSIVE, NBR_212, NULL, &nt, 1000) < 0) {
107 nfc_perror(pnd, "nfc_initiator_select_dep_target");
108 nfc_close(pnd);
109 nfc_exit(context);
110 exit(EXIT_FAILURE);
111 }
112 print_nfc_target(&nt, false);
113
114 printf("Sending: %s\n", abtTx);
115 int res;
116 if ((res = nfc_initiator_transceive_bytes(pnd, abtTx, sizeof(abtTx), abtRx, sizeof(abtRx), 0)) < 0) {
117 nfc_perror(pnd, "nfc_initiator_transceive_bytes");
118 nfc_close(pnd);
119 nfc_exit(context);
120 exit(EXIT_FAILURE);
121 }
122
123 abtRx[res] = 0;
124 printf("Received: %s\n", abtRx);
125
126 if (nfc_initiator_deselect_target(pnd) < 0) {
127 nfc_perror(pnd, "nfc_initiator_deselect_target");
128 nfc_close(pnd);
129 nfc_exit(context);
130 exit(EXIT_FAILURE);
131 }
132
133 nfc_close(pnd);
134 nfc_exit(context);
135 exit(EXIT_SUCCESS);
136}
const char * nfc_device_get_name(nfc_device *pnd)
Returns the device name.
Definition: nfc.c:1209
void nfc_close(nfc_device *pnd)
Close from a NFC device.
Definition: nfc.c:339
nfc_device * nfc_open(nfc_context *context, const nfc_connstring connstring)
Open a NFC device.
Definition: nfc.c:277
int nfc_abort_command(nfc_device *pnd)
Abort current running command.
Definition: nfc.c:1036
void nfc_perror(const nfc_device *pnd, const char *pcString)
Display the last error occured on a nfc_device.
Definition: nfc.c:1183
int nfc_initiator_transceive_bytes(nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx, const size_t szRx, int timeout)
Send data to target then retrieve data from target.
Definition: nfc.c:809
int nfc_initiator_init(nfc_device *pnd)
Initialize NFC device as initiator (reader)
Definition: nfc.c:493
int nfc_initiator_select_dep_target(nfc_device *pnd, const nfc_dep_mode ndm, const nfc_baud_rate nbr, const nfc_dep_info *pndiInitiator, nfc_target *pnt, const int timeout)
Select a target and request active or passive mode for D.E.P. (Data Exchange Protocol)
Definition: nfc.c:702
void nfc_exit(nfc_context *context)
Deinitialize libnfc. Should be called after closing all open devices and before your application term...
Definition: nfc.c:248
void nfc_init(nfc_context **context)
Initialize libnfc. This function must be called before calling any other libnfc function.
Definition: nfc.c:231
Provide some examples shared functions like print, parity calculation, options parsing.
#define ERR(...)
Print a error message.
Definition: nfc-utils.h:85
libnfc interface
NFC library context Struct which contains internal options, references, pointers, etc....
Definition: nfc-internal.h:175
NFC device information.
Definition: nfc-internal.h:190
NFC target structure.
Definition: nfc-types.h:351