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