libnfc  1.7.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  *
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  strcpy(res->user_defined_devices[0].connstring, envvar);
104  res->user_defined_device_count++;
105  }
106 
107 #endif // ENVVARS
108 
109 #ifdef CONFFILES
110  // Load options from configuration file (ie. /etc/nfc/libnfc.conf)
111  conf_load(res);
112 #endif // CONFFILES
113 
114 #ifdef ENVVARS
115  // Environment variables
116  // Load "intrusive scan" option
117  envvar = getenv("LIBNFC_INTRUSIVE_SCAN");
118  string_as_boolean(envvar, &(res->allow_intrusive_scan));
119 
120  // log level
121  envvar = getenv("LIBNFC_LOG_LEVEL");
122  if (envvar) {
123  res->log_level = atoi(envvar);
124  }
125 #endif // ENVVARS
126 
127  // Initialize log before use it...
128  log_init(res);
129 
130  // Debug context state
131 #if defined DEBUG
132  log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_NONE, "log_level is set to %"PRIu32, res->log_level);
133 #else
134  log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "log_level is set to %"PRIu32, res->log_level);
135 #endif
136  log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "allow_autoscan is set to %s", (res->allow_autoscan) ? "true" : "false");
137  log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "allow_intrusive_scan is set to %s", (res->allow_intrusive_scan) ? "true" : "false");
138 
139  log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "%d device(s) defined by user", res->user_defined_device_count);
140  for (uint32_t i = 0; i < res->user_defined_device_count; i++) {
141  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);
142  }
143  return res;
144 }
145 
146 void
147 nfc_context_free(nfc_context *context)
148 {
149  log_exit();
150  free(context);
151 }
152 
153 void
154 prepare_initiator_data(const nfc_modulation nm, uint8_t **ppbtInitiatorData, size_t *pszInitiatorData)
155 {
156  switch (nm.nmt) {
157  case NMT_ISO14443B: {
158  // Application Family Identifier (AFI) must equals 0x00 in order to wakeup all ISO14443-B PICCs (see ISO/IEC 14443-3)
159  *ppbtInitiatorData = (uint8_t *) "\x00";
160  *pszInitiatorData = 1;
161  }
162  break;
163  case NMT_ISO14443BI: {
164  // APGEN
165  *ppbtInitiatorData = (uint8_t *) "\x01\x0b\x3f\x80";
166  *pszInitiatorData = 4;
167  }
168  break;
169  case NMT_ISO14443B2SR: {
170  // Get_UID
171  *ppbtInitiatorData = (uint8_t *) "\x0b";
172  *pszInitiatorData = 1;
173  }
174  break;
175  case NMT_ISO14443B2CT: {
176  // SELECT-ALL
177  *ppbtInitiatorData = (uint8_t *) "\x9F\xFF\xFF";
178  *pszInitiatorData = 3;
179  }
180  break;
181  case NMT_FELICA: {
182  // polling payload must be present (see ISO/IEC 18092 11.2.2.5)
183  *ppbtInitiatorData = (uint8_t *) "\x00\xff\xff\x01\x00";
184  *pszInitiatorData = 5;
185  }
186  break;
187  case NMT_ISO14443A:
188  case NMT_JEWEL:
189  case NMT_DEP:
190  *ppbtInitiatorData = NULL;
191  *pszInitiatorData = 0;
192  break;
193  }
194 }
195 
196 int
197 connstring_decode(const nfc_connstring connstring, const char *driver_name, const char *bus_name, char **pparam1, char **pparam2)
198 {
199  if (driver_name == NULL) {
200  driver_name = "";
201  }
202  if (bus_name == NULL) {
203  bus_name = "";
204  }
205  int n = strlen(connstring) + 1;
206  char *param0 = malloc(n);
207  if (param0 == NULL) {
208  perror("malloc");
209  return 0;
210  }
211  char *param1 = malloc(n);
212  if (param1 == NULL) {
213  perror("malloc");
214  free(param0);
215  return 0;
216  }
217  char *param2 = malloc(n);
218  if (param2 == NULL) {
219  perror("malloc");
220  free(param0);
221  free(param1);
222  return 0;
223  }
224 
225  char format[32];
226  snprintf(format, sizeof(format), "%%%i[^:]:%%%i[^:]:%%%i[^:]", n - 1, n - 1, n - 1);
227  int res = sscanf(connstring, format, param0, param1, param2);
228 
229  if (res < 1 || ((0 != strcmp(param0, driver_name)) &&
230  (bus_name != NULL) &&
231  (0 != strcmp(param0, bus_name)))) {
232  // Driver name does not match.
233  res = 0;
234  }
235  if (pparam1 != NULL) {
236  if (res < 2) {
237  free(param1);
238  *pparam1 = NULL;
239  } else {
240  *pparam1 = param1;
241  }
242  } else {
243  free(param1);
244  }
245  if (pparam2 != NULL) {
246  if (res < 3) {
247  free(param2);
248  *pparam2 = NULL;
249  } else {
250  *pparam2 = param2;
251  }
252  } else {
253  free(param2);
254  }
255  free(param0);
256  return res;
257 }
258 
Internal defines and macros.
libnfc interface
NFC modulation structure.
Definition: nfc-types.h:319
char nfc_connstring[NFC_BUFSIZE_CONNSTRING]
Definition: nfc-types.h:62
NFC library context Struct which contains internal options, references, pointers, etc...
Definition: nfc-internal.h:175