libnfc  1.7.1
nfc-internal.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  * This program is free software: you can redistribute it and/or modify it
14  * under the terms of the GNU Lesser General Public License as published by the
15  * Free Software Foundation, either version 3 of the License, or (at your
16  * option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful, but WITHOUT
19  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
21  * more details.
22  *
23  * You should have received a copy of the GNU Lesser General Public License
24  * along with this program. If not, see <http://www.gnu.org/licenses/>
25  */
26 
32 #include <nfc/nfc.h>
33 #include "nfc-internal.h"
34 
35 #ifdef HAVE_CONFIG_H
36 #include "config.h"
37 #endif
38 
39 #ifdef CONFFILES
40 #include "conf.h"
41 #endif
42 
43 #include <stdlib.h>
44 #include <string.h>
45 #include <inttypes.h>
46 
47 #define LOG_GROUP NFC_LOG_GROUP_GENERAL
48 #define LOG_CATEGORY "libnfc.general"
49 
50 void
51 string_as_boolean(const char *s, bool *value)
52 {
53  if (s) {
54  if (!(*value)) {
55  if ((strcmp(s, "yes") == 0) ||
56  (strcmp(s, "true") == 0) ||
57  (strcmp(s, "1") == 0)) {
58  *value = true;
59  return;
60  }
61  } else {
62  if ((strcmp(s, "no") == 0) ||
63  (strcmp(s, "false") == 0) ||
64  (strcmp(s, "0") == 0)) {
65  *value = false;
66  return;
67  }
68  }
69  }
70 }
71 
73 nfc_context_new(void)
74 {
75  nfc_context *res = malloc(sizeof(*res));
76 
77  if (!res) {
78  return NULL;
79  }
80 
81  // Set default context values
82  res->allow_autoscan = true;
83  res->allow_intrusive_scan = false;
84 #ifdef DEBUG
85  res->log_level = 3;
86 #else
87  res->log_level = 1;
88 #endif
89 
90  // Clear user defined devices array
91  for (int i = 0; i < MAX_USER_DEFINED_DEVICES; i++) {
92  strcpy(res->user_defined_devices[i].name, "");
93  strcpy(res->user_defined_devices[i].connstring, "");
94  res->user_defined_devices[i].optional = false;
95  }
96  res->user_defined_device_count = 0;
97 
98 #ifdef ENVVARS
99  // Load user defined device from environment variable at first
100  char *envvar = getenv("LIBNFC_DEFAULT_DEVICE");
101  if (envvar) {
102  strcpy(res->user_defined_devices[0].name, "user defined default device");
103  strncpy(res->user_defined_devices[0].connstring, envvar, NFC_BUFSIZE_CONNSTRING);
104  res->user_defined_devices[0].connstring[NFC_BUFSIZE_CONNSTRING - 1] = '\0';
105  res->user_defined_device_count++;
106  }
107 
108 #endif // ENVVARS
109 
110 #ifdef CONFFILES
111  // Load options from configuration file (ie. /etc/nfc/libnfc.conf)
112  conf_load(res);
113 #endif // CONFFILES
114 
115 #ifdef ENVVARS
116  // Environment variables
117 
118  // Load user defined device from environment variable as the only reader
119  envvar = getenv("LIBNFC_DEVICE");
120  if (envvar) {
121  strcpy(res->user_defined_devices[0].name, "user defined device");
122  strncpy(res->user_defined_devices[0].connstring, envvar, NFC_BUFSIZE_CONNSTRING);
123  res->user_defined_devices[0].connstring[NFC_BUFSIZE_CONNSTRING - 1] = '\0';
124  res->user_defined_device_count = 1;
125  }
126 
127  // Load "auto scan" option
128  envvar = getenv("LIBNFC_AUTO_SCAN");
129  string_as_boolean(envvar, &(res->allow_autoscan));
130 
131  // Load "intrusive scan" option
132  envvar = getenv("LIBNFC_INTRUSIVE_SCAN");
133  string_as_boolean(envvar, &(res->allow_intrusive_scan));
134 
135  // log level
136  envvar = getenv("LIBNFC_LOG_LEVEL");
137  if (envvar) {
138  res->log_level = atoi(envvar);
139  }
140 #endif // ENVVARS
141 
142  // Initialize log before use it...
143  log_init(res);
144 
145  // Debug context state
146 #if defined DEBUG
147  log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_NONE, "log_level is set to %"PRIu32, res->log_level);
148 #else
149  log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "log_level is set to %"PRIu32, res->log_level);
150 #endif
151  log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "allow_autoscan is set to %s", (res->allow_autoscan) ? "true" : "false");
152  log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "allow_intrusive_scan is set to %s", (res->allow_intrusive_scan) ? "true" : "false");
153 
154  log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "%d device(s) defined by user", res->user_defined_device_count);
155  for (uint32_t i = 0; i < res->user_defined_device_count; i++) {
156  log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, " #%d name: \"%s\", connstring: \"%s\"", i, res->user_defined_devices[i].name, res->user_defined_devices[i].connstring);
157  }
158  return res;
159 }
160 
161 void
162 nfc_context_free(nfc_context *context)
163 {
164  log_exit();
165  free(context);
166 }
167 
168 void
169 prepare_initiator_data(const nfc_modulation nm, uint8_t **ppbtInitiatorData, size_t *pszInitiatorData)
170 {
171  switch (nm.nmt) {
172  case NMT_ISO14443B: {
173  // Application Family Identifier (AFI) must equals 0x00 in order to wakeup all ISO14443-B PICCs (see ISO/IEC 14443-3)
174  *ppbtInitiatorData = (uint8_t *) "\x00";
175  *pszInitiatorData = 1;
176  }
177  break;
178  case NMT_ISO14443BI: {
179  // APGEN
180  *ppbtInitiatorData = (uint8_t *) "\x01\x0b\x3f\x80";
181  *pszInitiatorData = 4;
182  }
183  break;
184  case NMT_ISO14443B2SR: {
185  // Get_UID
186  *ppbtInitiatorData = (uint8_t *) "\x0b";
187  *pszInitiatorData = 1;
188  }
189  break;
190  case NMT_ISO14443B2CT: {
191  // SELECT-ALL
192  *ppbtInitiatorData = (uint8_t *) "\x9F\xFF\xFF";
193  *pszInitiatorData = 3;
194  }
195  break;
196  case NMT_FELICA: {
197  // polling payload must be present (see ISO/IEC 18092 11.2.2.5)
198  *ppbtInitiatorData = (uint8_t *) "\x00\xff\xff\x01\x00";
199  *pszInitiatorData = 5;
200  }
201  break;
202  case NMT_ISO14443A:
203  case NMT_JEWEL:
204  case NMT_DEP:
205  *ppbtInitiatorData = NULL;
206  *pszInitiatorData = 0;
207  break;
208  }
209 }
210 
211 int
212 connstring_decode(const nfc_connstring connstring, const char *driver_name, const char *bus_name, char **pparam1, char **pparam2)
213 {
214  if (driver_name == NULL) {
215  driver_name = "";
216  }
217  if (bus_name == NULL) {
218  bus_name = "";
219  }
220  int n = strlen(connstring) + 1;
221  char *param0 = malloc(n);
222  if (param0 == NULL) {
223  perror("malloc");
224  return 0;
225  }
226  char *param1 = malloc(n);
227  if (param1 == NULL) {
228  perror("malloc");
229  free(param0);
230  return 0;
231  }
232  char *param2 = malloc(n);
233  if (param2 == NULL) {
234  perror("malloc");
235  free(param0);
236  free(param1);
237  return 0;
238  }
239 
240  char format[32];
241  snprintf(format, sizeof(format), "%%%i[^:]:%%%i[^:]:%%%i[^:]", n - 1, n - 1, n - 1);
242  int res = sscanf(connstring, format, param0, param1, param2);
243 
244  if (res < 1 || ((0 != strcmp(param0, driver_name)) &&
245  (bus_name != NULL) &&
246  (0 != strcmp(param0, bus_name)))) {
247  // Driver name does not match.
248  res = 0;
249  }
250  if (pparam1 != NULL) {
251  if (res < 2) {
252  free(param1);
253  *pparam1 = NULL;
254  } else {
255  *pparam1 = param1;
256  }
257  } else {
258  free(param1);
259  }
260  if (pparam2 != NULL) {
261  if (res < 3) {
262  free(param2);
263  *pparam2 = NULL;
264  } else {
265  *pparam2 = param2;
266  }
267  } else {
268  free(param2);
269  }
270  free(param0);
271  return res;
272 }
273 
nfc_context
NFC library context Struct which contains internal options, references, pointers, etc....
Definition: nfc-internal.h:175
nfc_modulation
NFC modulation structure.
Definition: nfc-types.h:319
nfc_connstring
char nfc_connstring[NFC_BUFSIZE_CONNSTRING]
Definition: nfc-types.h:62
nfc-internal.h
Internal defines and macros.
nfc.h
libnfc interface