Examples and utils are now updated to use the requiered nfc_context
Romuald Conty
11 years ago
148 | 148 | } |
149 | 149 | } |
150 | 150 | |
151 | nfc_init(NULL); | |
151 | nfc_context *context; | |
152 | nfc_init(&context); | |
152 | 153 | |
153 | 154 | // Try to open the NFC reader |
154 | pnd = nfc_open(NULL, NULL); | |
155 | pnd = nfc_open(context, NULL); | |
155 | 156 | |
156 | 157 | if (!pnd) { |
157 | 158 | printf("Error opening NFC reader\n"); |
186 | 187 | if (!transmit_bits(abtReqa, 7)) { |
187 | 188 | printf("Error: No tag available\n"); |
188 | 189 | nfc_close(pnd); |
189 | nfc_exit(NULL); | |
190 | return 1; | |
190 | nfc_exit(context); | |
191 | return EXIT_FAILURE; | |
191 | 192 | } |
192 | 193 | memcpy(abtAtqa, abtRx, 2); |
193 | 194 | |
316 | 317 | } |
317 | 318 | |
318 | 319 | nfc_close(pnd); |
319 | nfc_exit(NULL); | |
320 | return 0; | |
320 | nfc_exit(context); | |
321 | return EXIT_SUCCESS; | |
321 | 322 | } |
70 | 70 | return EXIT_FAILURE; |
71 | 71 | } |
72 | 72 | |
73 | nfc_init(NULL); | |
73 | nfc_context *context; | |
74 | nfc_init(&context); | |
74 | 75 | |
75 | pnd = nfc_open(NULL, NULL); | |
76 | pnd = nfc_open(context, NULL); | |
76 | 77 | if (!pnd) { |
77 | 78 | printf("Unable to open NFC device.\n"); |
78 | 79 | return EXIT_FAILURE; |
109 | 110 | |
110 | 111 | error: |
111 | 112 | nfc_close(pnd); |
112 | nfc_exit(NULL); | |
113 | nfc_exit(context); | |
113 | 114 | return EXIT_SUCCESS; |
114 | 115 | } |
63 | 63 | uint8_t abtRx[MAX_FRAME_LEN]; |
64 | 64 | int szRx; |
65 | 65 | uint8_t abtTx[] = "Hello Mars!"; |
66 | ||
67 | nfc_context *context; | |
68 | nfc_init(&context); | |
66 | 69 | #define MAX_DEVICE_COUNT 2 |
67 | 70 | nfc_connstring connstrings[MAX_DEVICE_COUNT]; |
68 | size_t szDeviceFound = nfc_list_devices(NULL, connstrings, MAX_DEVICE_COUNT); | |
71 | size_t szDeviceFound = nfc_list_devices(context, connstrings, MAX_DEVICE_COUNT); | |
69 | 72 | // Little hack to allow using nfc-dep-initiator & nfc-dep-target from |
70 | 73 | // the same machine: if there is more than one readers opened |
71 | 74 | // nfc-dep-target will open the second reader |
72 | 75 | // (we hope they're always detected in the same order) |
73 | nfc_init(NULL); | |
74 | 76 | if (szDeviceFound == 1) { |
75 | pnd = nfc_open(NULL, connstrings[0]); | |
77 | pnd = nfc_open(context, connstrings[0]); | |
76 | 78 | } else if (szDeviceFound > 1) { |
77 | pnd = nfc_open(NULL, connstrings[1]); | |
79 | pnd = nfc_open(context, connstrings[1]); | |
78 | 80 | } else { |
79 | 81 | printf("No device found.\n"); |
80 | 82 | return EXIT_FAILURE; |
140 | 142 | |
141 | 143 | error: |
142 | 144 | nfc_close(pnd); |
143 | nfc_exit(NULL); | |
145 | nfc_exit(context); | |
144 | 146 | return EXIT_SUCCESS; |
145 | 147 | } |
183 | 183 | }; |
184 | 184 | |
185 | 185 | signal(SIGINT, stop_emulation); |
186 | nfc_init(NULL); | |
187 | pnd = nfc_open(NULL, NULL); | |
186 | ||
187 | nfc_context *context; | |
188 | nfc_init(&context); | |
189 | pnd = nfc_open(context, NULL); | |
188 | 190 | |
189 | 191 | if (pnd == NULL) { |
190 | 192 | ERR("Unable to open NFC device"); |
199 | 201 | } |
200 | 202 | |
201 | 203 | nfc_close(pnd); |
202 | nfc_exit(NULL); | |
204 | nfc_exit(context); | |
203 | 205 | |
204 | 206 | exit(EXIT_SUCCESS); |
205 | 207 | |
207 | 209 | if (pnd) { |
208 | 210 | nfc_perror(pnd, argv[0]); |
209 | 211 | nfc_close(pnd); |
210 | nfc_exit(NULL); | |
212 | nfc_exit(context); | |
211 | 213 | } |
212 | 214 | } |
55 | 55 | |
56 | 56 | static uint8_t abtRx[MAX_FRAME_LEN]; |
57 | 57 | static int szRx; |
58 | static nfc_context *context; | |
58 | 59 | static nfc_device *pnd; |
59 | 60 | static bool quiet_output = false; |
60 | 61 | static bool init_mfc_auth = false; |
67 | 68 | if (pnd != NULL) { |
68 | 69 | nfc_close(pnd); |
69 | 70 | } |
70 | nfc_exit(NULL); | |
71 | nfc_exit(context); | |
71 | 72 | exit(EXIT_FAILURE); |
72 | 73 | } |
73 | 74 | |
180 | 181 | signal(SIGINT, intr_hdlr); |
181 | 182 | #endif |
182 | 183 | |
183 | nfc_init(NULL); | |
184 | nfc_init(&context); | |
184 | 185 | |
185 | 186 | // Try to open the NFC reader |
186 | pnd = nfc_open(NULL, NULL); | |
187 | pnd = nfc_open(context, NULL); | |
187 | 188 | |
188 | 189 | // Display libnfc version |
189 | 190 | acLibnfcVersion = nfc_version(); |
269 | 270 | } |
270 | 271 | |
271 | 272 | nfc_close(pnd); |
272 | nfc_exit(NULL); | |
273 | nfc_exit(context); | |
273 | 274 | exit(EXIT_SUCCESS); |
274 | 275 | } |
275 | 276 |
125 | 125 | signal(SIGINT, intr_hdlr); |
126 | 126 | #endif |
127 | 127 | |
128 | nfc_init(NULL); | |
128 | nfc_context *context; | |
129 | nfc_init(&context); | |
129 | 130 | |
130 | 131 | // Try to open the NFC device |
131 | pnd = nfc_open(NULL, NULL); | |
132 | pnd = nfc_open(context, NULL); | |
132 | 133 | |
133 | 134 | if (pnd == NULL) { |
134 | 135 | printf("Unable to open NFC device\n"); |
219 | 220 | } |
220 | 221 | } |
221 | 222 | nfc_close(pnd); |
222 | nfc_exit(NULL); | |
223 | nfc_exit(context); | |
223 | 224 | exit(EXIT_SUCCESS); |
224 | 225 | |
225 | 226 | error: |
226 | 227 | nfc_close(pnd); |
227 | nfc_exit(NULL); | |
228 | nfc_exit(context); | |
228 | 229 | exit(EXIT_FAILURE); |
229 | 230 | } |
176 | 176 | } |
177 | 177 | } |
178 | 178 | |
179 | nfc_init(NULL); | |
179 | nfc_context *context; | |
180 | nfc_init(&context); | |
180 | 181 | |
181 | 182 | // Try to open the NFC reader |
182 | pnd = nfc_open(NULL, NULL); | |
183 | pnd = nfc_open(context, NULL); | |
183 | 184 | |
184 | 185 | if (!pnd) { |
185 | 186 | printf("Error opening NFC reader\n"); |
214 | 215 | if (!transmit_bits(abtReqa, 7)) { |
215 | 216 | printf("Error: No tag available\n"); |
216 | 217 | nfc_close(pnd); |
217 | nfc_exit(NULL); | |
218 | nfc_exit(context); | |
218 | 219 | return 1; |
219 | 220 | } |
220 | 221 | memcpy(abtAtqa, abtRx, 2); |
353 | 354 | |
354 | 355 | |
355 | 356 | nfc_close(pnd); |
356 | nfc_exit(NULL); | |
357 | return 0; | |
357 | nfc_exit(context); | |
358 | return EXIT_SUCCESS; | |
358 | 359 | } |
102 | 102 | nfc_target nt; |
103 | 103 | int res = 0; |
104 | 104 | |
105 | nfc_init(NULL); | |
105 | nfc_context *context; | |
106 | nfc_init(&context); | |
106 | 107 | |
107 | pnd = nfc_open(NULL, NULL); | |
108 | pnd = nfc_open(context, NULL); | |
108 | 109 | |
109 | 110 | if (pnd == NULL) { |
110 | 111 | ERR("%s", "Unable to open NFC device."); |
121 | 122 | if ((res = nfc_initiator_poll_target(pnd, nmModulations, szModulations, uiPollNr, uiPeriod, &nt)) < 0) { |
122 | 123 | nfc_perror(pnd, "nfc_initiator_poll_target"); |
123 | 124 | nfc_close(pnd); |
124 | nfc_exit(NULL); | |
125 | nfc_exit(context); | |
125 | 126 | exit(EXIT_FAILURE); |
126 | 127 | } |
127 | 128 | |
131 | 132 | printf("No target found.\n"); |
132 | 133 | } |
133 | 134 | nfc_close(pnd); |
134 | nfc_exit(NULL); | |
135 | nfc_exit(context); | |
135 | 136 | exit(EXIT_SUCCESS); |
136 | 137 | } |
107 | 107 | signal(SIGINT, intr_hdlr); |
108 | 108 | #endif |
109 | 109 | |
110 | nfc_context *context; | |
111 | nfc_init(&context); | |
110 | 112 | nfc_connstring connstrings[MAX_DEVICE_COUNT]; |
111 | 113 | // List available devices |
112 | size_t szFound = nfc_list_devices(NULL, connstrings, MAX_DEVICE_COUNT); | |
114 | size_t szFound = nfc_list_devices(context, connstrings, MAX_DEVICE_COUNT); | |
113 | 115 | |
114 | 116 | if (szFound < 2) { |
115 | 117 | ERR("%zd device found but two opened devices are needed to relay NFC.", szFound); |
116 | 118 | return EXIT_FAILURE; |
117 | 119 | } |
118 | 120 | |
119 | nfc_init(NULL); | |
120 | ||
121 | 121 | // Try to open the NFC emulator device |
122 | pndTag = nfc_open(NULL, connstrings[0]); | |
122 | pndTag = nfc_open(context, connstrings[0]); | |
123 | 123 | if (pndTag == NULL) { |
124 | 124 | printf("Error opening NFC emulator device\n"); |
125 | 125 | return EXIT_FAILURE; |
151 | 151 | if ((szReaderRxBits = nfc_target_init(pndTag, &nt, abtReaderRx, sizeof(abtReaderRx), 0)) < 0) { |
152 | 152 | ERR("%s", "Initialization of NFC emulator failed"); |
153 | 153 | nfc_close(pndTag); |
154 | nfc_exit(NULL); | |
154 | nfc_exit(context); | |
155 | 155 | return EXIT_FAILURE; |
156 | 156 | } |
157 | 157 | printf("%s", "Configuring emulator settings..."); |
163 | 163 | printf("%s", "Done, emulated tag is initialized"); |
164 | 164 | |
165 | 165 | // Try to open the NFC reader |
166 | pndReader = nfc_open(NULL, connstrings[1]); | |
166 | pndReader = nfc_open(context, connstrings[1]); | |
167 | 167 | |
168 | 168 | printf("NFC reader device: %s opened", nfc_device_get_name(pndReader)); |
169 | 169 | printf("%s", "Configuring NFC reader settings..."); |
221 | 221 | |
222 | 222 | nfc_close(pndTag); |
223 | 223 | nfc_close(pndReader); |
224 | nfc_exit(NULL); | |
224 | nfc_exit(context); | |
225 | 225 | exit(EXIT_SUCCESS); |
226 | 226 | } |
65 | 65 | errx(1, "usage: %s", argv[0]); |
66 | 66 | } |
67 | 67 | |
68 | nfc_init(NULL); | |
68 | nfc_context *context; | |
69 | nfc_init(&context); | |
69 | 70 | |
70 | 71 | // Display libnfc version |
71 | 72 | acLibnfcVersion = nfc_version(); |
72 | 73 | printf("%s uses libnfc %s\n", argv[0], acLibnfcVersion); |
73 | 74 | |
74 | 75 | nfc_connstring connstrings[MAX_DEVICE_COUNT]; |
75 | size_t szFound = nfc_list_devices(NULL, connstrings, MAX_DEVICE_COUNT); | |
76 | size_t szFound = nfc_list_devices(context, connstrings, MAX_DEVICE_COUNT); | |
76 | 77 | |
77 | 78 | if (szFound == 0) { |
78 | 79 | printf("No NFC device found.\n"); |
79 | 80 | } |
80 | 81 | |
81 | 82 | for (i = 0; i < szFound; i++) { |
82 | pnd = nfc_open(NULL, connstrings[i]); | |
83 | pnd = nfc_open(context, connstrings[i]); | |
83 | 84 | |
84 | 85 | if (pnd == NULL) { |
85 | 86 | ERR("%s", "Unable to open NFC device."); |
76 | 76 | |
77 | 77 | int ret = EXIT_FAILURE; |
78 | 78 | |
79 | nfc_init(NULL); | |
79 | nfc_context *context; | |
80 | nfc_init(&context); | |
80 | 81 | |
81 | 82 | // Display libnfc version |
82 | 83 | const char *acLibnfcVersion = nfc_version(); |
84 | 85 | |
85 | 86 | // Open using the first available NFC device |
86 | 87 | nfc_device *pnd; |
87 | pnd = nfc_open(NULL, NULL); | |
88 | pnd = nfc_open(context, NULL); | |
88 | 89 | |
89 | 90 | if (pnd == NULL) { |
90 | 91 | ERR("%s", "Unable to open NFC device."); |
210 | 211 | |
211 | 212 | // Close NFC device |
212 | 213 | nfc_close(pnd); |
213 | nfc_exit(NULL); | |
214 | nfc_exit(context); | |
214 | 215 | |
215 | 216 | exit(ret); |
216 | 217 | } |
82 | 82 | } |
83 | 83 | } |
84 | 84 | |
85 | nfc_init(NULL); | |
85 | nfc_context *context; | |
86 | nfc_init(&context); | |
86 | 87 | |
87 | 88 | // Try to open the NFC reader |
88 | pnd = nfc_open(NULL, NULL); | |
89 | pnd = nfc_open(context, NULL); | |
89 | 90 | |
90 | 91 | if (pnd == NULL) { |
91 | 92 | ERR("%s", "Unable to open NFC device."); |
196 | 197 | fclose(input); |
197 | 198 | } |
198 | 199 | nfc_close(pnd); |
199 | nfc_exit(NULL); | |
200 | return 1; | |
200 | nfc_exit(context); | |
201 | return EXIT_SUCCESS; | |
201 | 202 | } |
57 | 57 | |
58 | 58 | # include <nfc/nfc-types.h> |
59 | 59 | |
60 | # ifndef __has_attribute | |
61 | # define __has_attribute(x) 0 | |
62 | # endif | |
63 | ||
64 | # if __has_attribute(nonnull) || defined(__GNUC__) | |
65 | # define __has_attribute_nonnull 1 | |
66 | # endif | |
67 | ||
68 | # if __has_attribute_nonnull | |
69 | # define ATTRIBUTE_NONNULL( param ) __attribute__((nonnull (param))) | |
70 | # else | |
71 | # define ATTRIBUTE_NONNULL( param ) | |
72 | # endif | |
73 | ||
60 | 74 | # ifdef __cplusplus |
61 | 75 | extern "C" { |
62 | 76 | # endif // __cplusplus |
63 | 77 | |
64 | 78 | /* Library initialization/deinitialization */ |
65 | NFC_EXPORT void nfc_init(nfc_context **context); | |
66 | NFC_EXPORT void nfc_exit(nfc_context *context); | |
79 | NFC_EXPORT void nfc_init(nfc_context **context) ATTRIBUTE_NONNULL(1); | |
80 | NFC_EXPORT void nfc_exit(nfc_context *context) ATTRIBUTE_NONNULL(1); | |
67 | 81 | |
68 | 82 | /* NFC Device/Hardware manipulation */ |
69 | NFC_EXPORT nfc_device *nfc_open(nfc_context *context, const nfc_connstring connstring); | |
83 | NFC_EXPORT nfc_device *nfc_open(nfc_context *context, const nfc_connstring connstring) ATTRIBUTE_NONNULL(1); | |
70 | 84 | NFC_EXPORT void nfc_close(nfc_device *pnd); |
71 | 85 | NFC_EXPORT int nfc_abort_command(nfc_device *pnd); |
72 | NFC_EXPORT size_t nfc_list_devices(nfc_context *context, nfc_connstring connstrings[], size_t connstrings_len); | |
86 | NFC_EXPORT size_t nfc_list_devices(nfc_context *context, nfc_connstring connstrings[], size_t connstrings_len) ATTRIBUTE_NONNULL(1); | |
73 | 87 | NFC_EXPORT int nfc_idle(nfc_device *pnd); |
74 | 88 | |
75 | 89 | /* NFC initiator: act as "reader" */ |
365 | 365 | } |
366 | 366 | } |
367 | 367 | |
368 | nfc_init(NULL); | |
368 | nfc_context *context; | |
369 | nfc_init(&context); | |
369 | 370 | |
370 | 371 | // Try to open the NFC reader |
371 | pnd = nfc_open(NULL, NULL); | |
372 | pnd = nfc_open(context, NULL); | |
372 | 373 | |
373 | 374 | if (pnd == NULL) { |
374 | 375 | ERR("Unable to open NFC device"); |
392 | 393 | } |
393 | 394 | } |
394 | 395 | |
395 | nfc_exit(NULL); | |
396 | nfc_exit(context); | |
396 | 397 | exit(EXIT_SUCCESS); |
397 | 398 | } |
252 | 252 | nfc_close(pnd); |
253 | 253 | } |
254 | 254 | |
255 | nfc_exit(NULL); | |
256 | return 0; | |
255 | nfc_exit(context); | |
256 | return EXIT_SUCCESS; | |
257 | 257 | } |
52 | 52 | #include "mifare.h" |
53 | 53 | #include "nfc-utils.h" |
54 | 54 | |
55 | static nfc_context *context; | |
55 | 56 | static nfc_device *pnd; |
56 | 57 | static nfc_target nt; |
57 | 58 | static mifare_param mp; |
507 | 508 | } |
508 | 509 | // printf("Successfully opened required files\n"); |
509 | 510 | |
510 | nfc_init(NULL); | |
511 | nfc_init(&context); | |
511 | 512 | |
512 | 513 | // Try to open the NFC reader |
513 | pnd = nfc_open(NULL, NULL); | |
514 | pnd = nfc_open(context, NULL); | |
514 | 515 | if (pnd == NULL) { |
515 | 516 | printf("Error opening NFC reader\n"); |
516 | 517 | exit(EXIT_FAILURE); |
535 | 536 | if (nfc_initiator_select_passive_target(pnd, nmMifare, NULL, 0, &nt) < 0) { |
536 | 537 | printf("Error: no tag was found\n"); |
537 | 538 | nfc_close(pnd); |
538 | nfc_exit(NULL); | |
539 | nfc_exit(context); | |
539 | 540 | exit(EXIT_FAILURE); |
540 | 541 | } |
541 | 542 | // Test if we are dealing with a MIFARE compatible tag |
595 | 596 | break; |
596 | 597 | }; |
597 | 598 | |
598 | nfc_exit(NULL); | |
599 | nfc_exit(context); | |
599 | 600 | exit(EXIT_SUCCESS); |
600 | 601 | } |
203 | 203 | } |
204 | 204 | DBG("Successfully opened the dump file\n"); |
205 | 205 | |
206 | nfc_init(NULL); | |
206 | nfc_context *context; | |
207 | nfc_init(&context); | |
207 | 208 | |
208 | 209 | // Try to open the NFC device |
209 | pnd = nfc_open(NULL, NULL); | |
210 | pnd = nfc_open(context, NULL); | |
210 | 211 | if (pnd == NULL) { |
211 | 212 | ERR("Error opening NFC device\n"); |
212 | 213 | return 1; |
229 | 230 | if (nfc_initiator_select_passive_target(pnd, nmMifare, NULL, 0, &nt) < 0) { |
230 | 231 | ERR("no tag was found\n"); |
231 | 232 | nfc_close(pnd); |
232 | nfc_exit(NULL); | |
233 | return 1; | |
233 | nfc_exit(context); | |
234 | return EXIT_FAILURE; | |
234 | 235 | } |
235 | 236 | // Test if we are dealing with a MIFARE compatible tag |
236 | 237 | |
237 | 238 | if (nt.nti.nai.abtAtqa[1] != 0x44) { |
238 | 239 | ERR("tag is not a MIFARE Ultralight card\n"); |
239 | 240 | nfc_close(pnd); |
240 | nfc_exit(NULL); | |
241 | nfc_exit(context); | |
241 | 242 | return EXIT_FAILURE; |
242 | 243 | } |
243 | 244 | // Get the info from the current tag |
269 | 270 | } |
270 | 271 | |
271 | 272 | nfc_close(pnd); |
272 | nfc_exit(NULL); | |
273 | nfc_exit(context); | |
273 | 274 | return EXIT_SUCCESS; |
274 | 275 | } |
191 | 191 | } |
192 | 192 | } |
193 | 193 | |
194 | nfc_init(NULL); | |
195 | ||
196 | pnd = nfc_open(NULL, NULL); | |
194 | nfc_context *context; | |
195 | nfc_init(&context); | |
196 | ||
197 | pnd = nfc_open(context, NULL); | |
197 | 198 | |
198 | 199 | if (pnd == NULL) { |
199 | 200 | ERR("Unable to open NFC device"); |
313 | 314 | if (pnd) { |
314 | 315 | nfc_close(pnd); |
315 | 316 | } |
316 | nfc_exit(NULL); | |
317 | nfc_exit(context); | |
317 | 318 | exit(error); |
318 | 319 | } |
194 | 194 | signal(SIGINT, intr_hdlr); |
195 | 195 | #endif |
196 | 196 | |
197 | nfc_init(NULL); | |
197 | nfc_context *context; | |
198 | nfc_init(&context); | |
198 | 199 | |
199 | 200 | nfc_connstring connstrings[MAX_DEVICE_COUNT]; |
200 | 201 | // List available devices |
201 | size_t szFound = nfc_list_devices(NULL, connstrings, MAX_DEVICE_COUNT); | |
202 | size_t szFound = nfc_list_devices(context, connstrings, MAX_DEVICE_COUNT); | |
202 | 203 | |
203 | 204 | if (initiator_only_mode || target_only_mode) { |
204 | 205 | if (szFound < 1) { |
221 | 222 | // if there is more than one readers opened we open the second reader |
222 | 223 | // (we hope they're always detected in the same order) |
223 | 224 | if ((szFound == 1) || swap_devices) { |
224 | pndInitiator = nfc_open(NULL, connstrings[0]); | |
225 | } else { | |
226 | pndInitiator = nfc_open(NULL, connstrings[1]); | |
225 | pndInitiator = nfc_open(context, connstrings[0]); | |
226 | } else { | |
227 | pndInitiator = nfc_open(context, connstrings[1]); | |
227 | 228 | } |
228 | 229 | |
229 | 230 | if (!pndInitiator) { |
236 | 237 | if (nfc_initiator_init(pndInitiator) < 0) { |
237 | 238 | printf("Error: fail initializing initiator\n"); |
238 | 239 | nfc_close(pndInitiator); |
239 | nfc_exit(NULL); | |
240 | nfc_exit(context); | |
240 | 241 | exit(EXIT_FAILURE); |
241 | 242 | } |
242 | 243 | |
248 | 249 | if (nfc_initiator_select_passive_target(pndInitiator, nm, NULL, 0, &ntRealTarget) < 0) { |
249 | 250 | printf("Error: no tag was found\n"); |
250 | 251 | nfc_close(pndInitiator); |
251 | nfc_exit(NULL); | |
252 | nfc_exit(context); | |
252 | 253 | exit(EXIT_FAILURE); |
253 | 254 | } |
254 | 255 | |
258 | 259 | if (print_hex_fd4(ntRealTarget.nti.nai.abtUid, ntRealTarget.nti.nai.szUidLen, "UID") != EXIT_SUCCESS) { |
259 | 260 | fprintf(stderr, "Error while printing UID to FD4\n"); |
260 | 261 | nfc_close(pndInitiator); |
261 | nfc_exit(NULL); | |
262 | nfc_exit(context); | |
262 | 263 | exit(EXIT_FAILURE); |
263 | 264 | } |
264 | 265 | if (print_hex_fd4(ntRealTarget.nti.nai.abtAtqa, 2, "ATQA") != EXIT_SUCCESS) { |
265 | 266 | fprintf(stderr, "Error while printing ATQA to FD4\n"); |
266 | 267 | nfc_close(pndInitiator); |
267 | nfc_exit(NULL); | |
268 | nfc_exit(context); | |
268 | 269 | exit(EXIT_FAILURE); |
269 | 270 | } |
270 | 271 | if (print_hex_fd4(&(ntRealTarget.nti.nai.btSak), 1, "SAK") != EXIT_SUCCESS) { |
271 | 272 | fprintf(stderr, "Error while printing SAK to FD4\n"); |
272 | 273 | nfc_close(pndInitiator); |
273 | nfc_exit(NULL); | |
274 | nfc_exit(context); | |
274 | 275 | exit(EXIT_FAILURE); |
275 | 276 | } |
276 | 277 | if (print_hex_fd4(ntRealTarget.nti.nai.abtAts, ntRealTarget.nti.nai.szAtsLen, "ATS") != EXIT_SUCCESS) { |
277 | 278 | fprintf(stderr, "Error while printing ATS to FD4\n"); |
278 | 279 | nfc_close(pndInitiator); |
279 | nfc_exit(NULL); | |
280 | nfc_exit(context); | |
280 | 281 | exit(EXIT_FAILURE); |
281 | 282 | } |
282 | 283 | } |
300 | 301 | if (scan_hex_fd3(ntEmulatedTarget.nti.nai.abtUid, &(ntEmulatedTarget.nti.nai.szUidLen), "UID") != EXIT_SUCCESS) { |
301 | 302 | fprintf(stderr, "Error while scanning UID from FD3\n"); |
302 | 303 | nfc_close(pndInitiator); |
303 | nfc_exit(NULL); | |
304 | nfc_exit(context); | |
304 | 305 | exit(EXIT_FAILURE); |
305 | 306 | } |
306 | 307 | if (scan_hex_fd3(ntEmulatedTarget.nti.nai.abtAtqa, &foo, "ATQA") != EXIT_SUCCESS) { |
311 | 312 | if (scan_hex_fd3(&(ntEmulatedTarget.nti.nai.btSak), &foo, "SAK") != EXIT_SUCCESS) { |
312 | 313 | fprintf(stderr, "Error while scanning SAK from FD3\n"); |
313 | 314 | nfc_close(pndInitiator); |
314 | nfc_exit(NULL); | |
315 | nfc_exit(context); | |
315 | 316 | exit(EXIT_FAILURE); |
316 | 317 | } |
317 | 318 | if (scan_hex_fd3(ntEmulatedTarget.nti.nai.abtAts, &(ntEmulatedTarget.nti.nai.szAtsLen), "ATS") != EXIT_SUCCESS) { |
318 | 319 | fprintf(stderr, "Error while scanning ATS from FD3\n"); |
319 | 320 | nfc_close(pndInitiator); |
320 | nfc_exit(NULL); | |
321 | nfc_exit(context); | |
321 | 322 | exit(EXIT_FAILURE); |
322 | 323 | } |
323 | 324 | } else { |
357 | 358 | |
358 | 359 | // Try to open the NFC emulator device |
359 | 360 | if (swap_devices) { |
360 | pndTarget = nfc_open(NULL, connstrings[1]); | |
361 | } else { | |
362 | pndTarget = nfc_open(NULL, connstrings[0]); | |
361 | pndTarget = nfc_open(context, connstrings[1]); | |
362 | } else { | |
363 | pndTarget = nfc_open(context, connstrings[0]); | |
363 | 364 | } |
364 | 365 | if (pndTarget == NULL) { |
365 | 366 | printf("Error opening NFC emulator device\n"); |
366 | 367 | if (!target_only_mode) { |
367 | 368 | nfc_close(pndInitiator); |
368 | 369 | } |
369 | nfc_exit(NULL); | |
370 | nfc_exit(context); | |
370 | 371 | return EXIT_FAILURE; |
371 | 372 | } |
372 | 373 | |
378 | 379 | nfc_close(pndInitiator); |
379 | 380 | } |
380 | 381 | nfc_close(pndTarget); |
381 | nfc_exit(NULL); | |
382 | nfc_exit(context); | |
382 | 383 | exit(EXIT_FAILURE); |
383 | 384 | } |
384 | 385 | printf("%s\n", "Done, relaying frames now!"); |
395 | 396 | nfc_close(pndInitiator); |
396 | 397 | } |
397 | 398 | nfc_close(pndTarget); |
398 | nfc_exit(NULL); | |
399 | nfc_exit(context); | |
399 | 400 | exit(EXIT_FAILURE); |
400 | 401 | } |
401 | 402 | szCapduLen = (size_t) res; |
403 | 404 | if (print_hex_fd4(abtCapdu, szCapduLen, "C-APDU") != EXIT_SUCCESS) { |
404 | 405 | fprintf(stderr, "Error while printing C-APDU to FD4\n"); |
405 | 406 | nfc_close(pndTarget); |
406 | nfc_exit(NULL); | |
407 | nfc_exit(context); | |
407 | 408 | exit(EXIT_FAILURE); |
408 | 409 | } |
409 | 410 | } |
411 | 412 | if (scan_hex_fd3(abtCapdu, &szCapduLen, "C-APDU") != EXIT_SUCCESS) { |
412 | 413 | fprintf(stderr, "Error while scanning C-APDU from FD3\n"); |
413 | 414 | nfc_close(pndInitiator); |
414 | nfc_exit(NULL); | |
415 | nfc_exit(context); | |
415 | 416 | exit(EXIT_FAILURE); |
416 | 417 | } |
417 | 418 | } |
433 | 434 | if (scan_hex_fd3(abtRapdu, &szRapduLen, "R-APDU") != EXIT_SUCCESS) { |
434 | 435 | fprintf(stderr, "Error while scanning R-APDU from FD3\n"); |
435 | 436 | nfc_close(pndTarget); |
436 | nfc_exit(NULL); | |
437 | nfc_exit(context); | |
437 | 438 | exit(EXIT_FAILURE); |
438 | 439 | } |
439 | 440 | ret = true; |
460 | 461 | } |
461 | 462 | if (!initiator_only_mode) { |
462 | 463 | nfc_close(pndTarget); |
463 | nfc_exit(NULL); | |
464 | nfc_exit(context); | |
464 | 465 | } |
465 | nfc_exit(NULL); | |
466 | nfc_exit(context); | |
466 | 467 | exit(EXIT_FAILURE); |
467 | 468 | } |
468 | 469 | } else { |
469 | 470 | if (print_hex_fd4(abtRapdu, szRapduLen, "R-APDU") != EXIT_SUCCESS) { |
470 | 471 | fprintf(stderr, "Error while printing R-APDU to FD4\n"); |
471 | 472 | nfc_close(pndInitiator); |
472 | nfc_exit(NULL); | |
473 | nfc_exit(context); | |
473 | 474 | exit(EXIT_FAILURE); |
474 | 475 | } |
475 | 476 | } |
482 | 483 | if (!initiator_only_mode) { |
483 | 484 | nfc_close(pndTarget); |
484 | 485 | } |
485 | nfc_exit(NULL); | |
486 | nfc_exit(context); | |
486 | 487 | exit(EXIT_SUCCESS); |
487 | 488 | } |
488 | 489 |
75 | 75 | size_t i; |
76 | 76 | bool verbose = false; |
77 | 77 | |
78 | nfc_init(NULL); | |
78 | nfc_context *context; | |
79 | nfc_init(&context); | |
79 | 80 | |
80 | 81 | // Display libnfc version |
81 | 82 | acLibnfcVersion = nfc_version(); |
104 | 105 | #endif |
105 | 106 | |
106 | 107 | nfc_connstring connstrings[MAX_DEVICE_COUNT]; |
107 | size_t szDeviceFound = nfc_list_devices(NULL, connstrings, MAX_DEVICE_COUNT); | |
108 | size_t szDeviceFound = nfc_list_devices(context, connstrings, MAX_DEVICE_COUNT); | |
108 | 109 | |
109 | 110 | int res = EXIT_FAILURE; |
110 | 111 | if (szDeviceFound == 0) { |
115 | 116 | printf("%d NFC device(s) found:\n", (int)szDeviceFound); |
116 | 117 | char *strinfo = NULL; |
117 | 118 | for (i = 0; i < szDeviceFound; i++) { |
118 | pnd = nfc_open(NULL, connstrings[i]); | |
119 | pnd = nfc_open(context, connstrings[i]); | |
119 | 120 | if (pnd != NULL) { |
120 | 121 | printf("- %s:\n %s\n", nfc_device_get_name(pnd), nfc_device_get_connstring(pnd)); |
121 | 122 | if (verbose) { |
132 | 133 | res = EXIT_SUCCESS; |
133 | 134 | |
134 | 135 | bye: |
135 | nfc_exit(NULL); | |
136 | nfc_exit(context); | |
136 | 137 | return res; |
137 | 138 | } |