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