libnfc 1.8.0
nfc-barcode.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) 2017 Philippe Teuwen
6 * See AUTHORS file for a more comprehensive list of contributors.
7 * Additional contributors of this file:
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are met:
11 * 1) Redistributions of source code must retain the above copyright notice,
12 * this list of conditions and the following disclaimer.
13 * 2 )Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
28 *
29 * Note that this license only applies on the examples, NFC library itself is under LGPL
30 *
31 */
32
37
38#ifdef HAVE_CONFIG_H
39# include "config.h"
40#endif // HAVE_CONFIG_H
41
42#include <stdio.h>
43#include <stdlib.h>
44#include <stddef.h>
45#include <stdint.h>
46#include <stdbool.h>
47#include <string.h>
48
49#include <nfc/nfc.h>
50
51#include "nfc-utils.h"
52
53#define MAX_FRAME_LEN 264
54
55static nfc_device *pnd;
56
57bool verbose = false;
58
59static void
60print_usage(char *argv[])
61{
62 printf("Usage: %s [OPTIONS]\n", argv[0]);
63 printf("Options:\n");
64 printf("\t-h\tHelp. Print this message.\n");
65 printf("\t-q\tVerbose mode.\n");
66}
67
68
69static bool
70decode_barcode(uint8_t *pbtBarcode, const size_t szBarcode)
71{
72 if (verbose) {
73 printf("Manufacturer ID field: %02X\n", pbtBarcode[0]);
74 switch (pbtBarcode[0]) {
75 case 0xb7:
76 printf("Manufacturer: Thinfilm\n");
77 break;
78 default:
79 printf("Manufacturer: unknown\n");
80 break;
81 }
82 }
83 if (verbose) {
84 printf("Data Format Field: %02X\n", pbtBarcode[1]);
85 }
86 switch (pbtBarcode[1]) {
87 case 0:
88 printf("Data Format Field: Reserved for allocation by tag manufacturer\n");
89 return false;
90 break;
91 case 1:
92 case 2:
93 case 3:
94 case 4:
95 switch (pbtBarcode[1]) {
96 case 1:
97 printf("http://www.");
98 break;
99 case 2:
100 printf("https://www.");
101 break;
102 case 3:
103 printf("http://");
104 break;
105 case 4:
106 printf("https://");
107 break;
108 }
109 for (uint8_t i = 2; i < 15; i++) {
110 if ((pbtBarcode[i] == 0xfe) || (i == 14)) {
111 pbtBarcode[i] = '\n';
112 pbtBarcode[i + 1] = 0;
113 break;
114 }
115 }
116 printf("%s", (char *)pbtBarcode + 2);
117 break;
118 case 5:
119 printf("EPC: ");
120 for (uint8_t i = 0; i < 12; i++) {
121 printf("%02x", pbtBarcode[i + 2]);
122 }
123 printf("\n");
124 break;
125 default:
126 printf("Data Format Field: unknown (%02X)\n", pbtBarcode[1]);
127 printf("Data:");
128 for (uint8_t i = 2; i < szBarcode - 2; i++) {
129 printf("%02x", pbtBarcode[i]);
130 }
131 printf("\n");
132 break;
133 }
134 return true;
135}
136
137int
138main(int argc, char *argv[])
139{
140 int arg;
141
142 // Get commandline options
143 for (arg = 1; arg < argc; arg++) {
144 if (0 == strcmp(argv[arg], "-h")) {
145 print_usage(argv);
146 exit(EXIT_SUCCESS);
147 } else if (0 == strcmp(argv[arg], "-v")) {
148 verbose = true;
149 } else {
150 ERR("%s is not supported option.", argv[arg]);
151 print_usage(argv);
152 exit(EXIT_FAILURE);
153 }
154 }
155
156 nfc_context *context;
157 nfc_init(&context);
158 if (context == NULL) {
159 ERR("Unable to init libnfc (malloc)");
160 exit(EXIT_FAILURE);
161 }
162
163 // Try to open the NFC reader
164 pnd = nfc_open(context, NULL);
165
166 if (pnd == NULL) {
167 ERR("Error opening NFC reader");
168 nfc_exit(context);
169 exit(EXIT_FAILURE);
170 }
171
172 // Initialise NFC device as "initiator"
173 if (nfc_initiator_init(pnd) < 0) {
174 nfc_perror(pnd, "nfc_initiator_init");
175 nfc_close(pnd);
176 nfc_exit(context);
177 exit(EXIT_FAILURE);
178 }
179
180 printf("NFC reader: %s opened\n\n", nfc_device_get_name(pnd));
181
183 nm.nmt = NMT_BARCODE;
184 nm.nbr = NBR_106;
185 nfc_target ant[1];
186 // List NFC Barcode targets
187 if ((nfc_initiator_list_passive_targets(pnd, nm, ant, 1)) == 1) {
188 if (verbose) {
189 for (uint8_t i = 0; i < (&ant[0])->nti.nti.szDataLen; i++) {
190 printf("%02x", (&ant[0])->nti.nti.abtData[i]);
191 }
192 printf("\n");
193 }
194 decode_barcode((&ant[0])->nti.nti.abtData, (&ant[0])->nti.nti.szDataLen);
195 }
196 nfc_close(pnd);
197 nfc_exit(context);
198 exit(EXIT_SUCCESS);
199}
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
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_init(nfc_device *pnd)
Initialize NFC device as initiator (reader)
Definition nfc.c:493
int nfc_initiator_list_passive_targets(nfc_device *pnd, const nfc_modulation nm, nfc_target ant[], const size_t szTargets)
List passive or emulated tags.
Definition nfc.c:605
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....
NFC device information.
NFC modulation structure.
Definition nfc-types.h:342
NFC target structure.
Definition nfc-types.h:351