forked from nfc-tools/libnfc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_dep_passive.c
277 lines (224 loc) · 12.4 KB
/
test_dep_passive.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#include <cutter.h>
#include <pthread.h>
#include <signal.h>
#include <unistd.h>
#include "nfc/nfc.h"
void test_dep_passive(void);
#define INITIATOR 0
#define TARGET 1
pthread_t threads[2];
nfc_context *context;
nfc_connstring connstrings[2];
nfc_device *devices[2];
intptr_t result[2];
static void
abort_test_by_keypress(int sig)
{
(void) sig;
printf("\033[0;1;31mSIGINT\033[0m");
nfc_abort_command(devices[INITIATOR]);
nfc_abort_command(devices[TARGET]);
}
void
cut_setup(void)
{
nfc_init(&context);
size_t n = nfc_list_devices(context, connstrings, 2);
if (n < 2) {
cut_omit("At least two NFC devices must be plugged-in to run this test");
}
devices[TARGET] = nfc_open(context, connstrings[TARGET]);
devices[INITIATOR] = nfc_open(context, connstrings[INITIATOR]);
signal(SIGINT, abort_test_by_keypress);
}
void
cut_teardown(void)
{
nfc_close(devices[TARGET]);
nfc_close(devices[INITIATOR]);
nfc_exit(context);
}
struct thread_data {
nfc_device *device;
void *cut_test_context;
};
static void *
target_thread(void *arg)
{
intptr_t thread_res = 0;
nfc_device *device = ((struct thread_data *) arg)->device;
cut_set_current_test_context(((struct thread_data *) arg)->cut_test_context);
printf("=========== TARGET %s =========\n", nfc_device_get_name(device));
nfc_target nt = {
.nm = {
.nmt = NMT_DEP,
.nbr = NBR_UNDEFINED
},
.nti = {
.ndi = {
.abtNFCID3 = { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA },
.szGB = 4,
.abtGB = { 0x12, 0x34, 0x56, 0x78 },
.ndm = NDM_PASSIVE,
/* These bytes are not used by nfc_target_init: the chip will provide them automatically to the initiator */
.btDID = 0x00,
.btBS = 0x00,
.btBR = 0x00,
.btTO = 0x00,
.btPP = 0x01,
},
},
};
uint8_t abtRx[1024];
size_t szRx = sizeof(abtRx);
int res = nfc_target_init(device, &nt, abtRx, szRx, 0);
cut_assert_operator_int(res, >, 0, cut_message("Can't initialize NFC device as target: %s", nfc_strerror(device)));
if (res < 0) { thread_res = -1; return (void *) thread_res; }
// First pass
res = nfc_target_receive_bytes(device, abtRx, sizeof(abtRx), 500);
cut_assert_operator_int(res, >, 0, cut_message("Can't receive bytes from initiator: %s", nfc_strerror(device)));
const uint8_t abtAttRx[] = "Hello DEP target!";
cut_assert_equal_memory(abtAttRx, sizeof(abtAttRx), abtRx, res, cut_message("Invalid received data"));
if (res <= 0) { thread_res = -1; return (void *) thread_res; }
const uint8_t abtTx[] = "Hello DEP initiator!";
res = nfc_target_send_bytes(device, abtTx, sizeof(abtTx), 500);
cut_assert_operator_int(res, >, 0, cut_message("Can't send bytes to initiator: %s", nfc_strerror(device)));
if (res <= 0) { thread_res = -1; return (void *) thread_res; }
// Second pass
res = nfc_target_receive_bytes(device, abtRx, sizeof(abtRx), 500);
cut_assert_operator_int(res, >, 0, cut_message("Can't receive bytes from initiator: %s", nfc_strerror(device)));
cut_assert_equal_memory(abtAttRx, sizeof(abtAttRx), abtRx, res, cut_message("Invalid received data"));
if (res <= 0) { thread_res = -1; return (void *) thread_res; }
res = nfc_target_send_bytes(device, abtTx, sizeof(abtTx), 500);
cut_assert_operator_int(res, >, 0, cut_message("Can't send bytes to initiator: %s", nfc_strerror(device)));
if (res <= 0) { thread_res = -1; return (void *) thread_res; }
// Third pass
res = nfc_target_receive_bytes(device, abtRx, sizeof(abtRx), 500);
cut_assert_operator_int(res, >, 0, cut_message("Can't receive bytes from initiator: %s", nfc_strerror(device)));
cut_assert_equal_memory(abtAttRx, sizeof(abtAttRx), abtRx, res, cut_message("Invalid received data"));
if (res <= 0) { thread_res = -1; return (void *) thread_res; }
res = nfc_target_send_bytes(device, abtTx, sizeof(abtTx), 500);
cut_assert_operator_int(res, >, 0, cut_message("Can't send bytes to initiator: %s", nfc_strerror(device)));
if (res <= 0) { thread_res = -1; return (void *) thread_res; }
// Fourth pass
res = nfc_target_receive_bytes(device, abtRx, sizeof(abtRx), 500);
cut_assert_operator_int(res, >, 0, cut_message("Can't receive bytes from initiator: %s", nfc_strerror(device)));
cut_assert_equal_memory(abtAttRx, sizeof(abtAttRx), abtRx, res, cut_message("Invalid received data"));
if (res <= 0) { thread_res = -1; return (void *) thread_res; }
res = nfc_target_send_bytes(device, abtTx, sizeof(abtTx), 500);
cut_assert_operator_int(res, >, 0, cut_message("Can't send bytes to initiator: %s", nfc_strerror(device)));
if (res <= 0) { thread_res = -1; return (void *) thread_res; }
return (void *) thread_res;
}
static void *
initiator_thread(void *arg)
{
intptr_t thread_res = 0;
nfc_device *device = ((struct thread_data *) arg)->device;
cut_set_current_test_context(((struct thread_data *) arg)->cut_test_context);
/*
* Wait some time for the other thread to initialise NFC device as target
*/
sleep(1);
printf("=========== INITIATOR %s =========\n", nfc_device_get_name(device));
int res = nfc_initiator_init(device);
cut_assert_equal_int(0, res, cut_message("Can't initialize NFC device as initiator: %s", nfc_strerror(device)));
if (res < 0) { thread_res = -1; return (void *) thread_res; }
nfc_target nt;
// Passive mode / 106Kbps
printf("=========== INITIATOR %s (Passive mode / 106Kbps) =========\n", nfc_device_get_name(device));
res = nfc_initiator_select_dep_target(device, NDM_PASSIVE, NBR_106, NULL, &nt, 5000);
cut_assert_operator_int(res, >, 0, cut_message("Can't select any DEP target: %s", nfc_strerror(device)));
cut_assert_equal_int(NMT_DEP, nt.nm.nmt, cut_message("Invalid target modulation"));
cut_assert_equal_int(NBR_106, nt.nm.nbr, cut_message("Invalid target baud rate"));
cut_assert_equal_memory("\x11\x22\x33\x44\x55\x66\x77\x88\x99\xAA", 10, nt.nti.ndi.abtNFCID3, 10, cut_message("Invalid target NFCID3"));
cut_assert_equal_int(NDM_PASSIVE, nt.nti.ndi.ndm, cut_message("Invalid target DEP mode"));
cut_assert_equal_memory("\x12\x34\x56\x78", 4, nt.nti.ndi.abtGB, nt.nti.ndi.szGB, cut_message("Invalid target general bytes"));
if (res <= 0) { thread_res = -1; return (void *) thread_res; }
const uint8_t abtTx[] = "Hello DEP target!";
uint8_t abtRx[1024];
res = nfc_initiator_transceive_bytes(device, abtTx, sizeof(abtTx), abtRx, sizeof(abtRx), 500);
cut_assert_operator_int(res, >=, 0, cut_message("Can't transceive bytes to target: %s", nfc_strerror(device)));
const uint8_t abtAttRx[] = "Hello DEP initiator!";
cut_assert_equal_memory(abtAttRx, sizeof(abtAttRx), abtRx, res, cut_message("Invalid received data"));
if (res < 0) { thread_res = -1; return (void *) thread_res; }
res = nfc_initiator_deselect_target(device);
cut_assert_operator_int(res, >=, 0, cut_message("Can't deselect target: %s", nfc_strerror(device)));
if (res < 0) { thread_res = -1; return (void *) thread_res; }
// Passive mode / 212Kbps (second pass)
printf("=========== INITIATOR %s (Passive mode / 212Kbps) =========\n", nfc_device_get_name(device));
res = nfc_initiator_select_dep_target(device, NDM_PASSIVE, NBR_212, NULL, &nt, 1000);
cut_assert_operator_int(res, >, 0, cut_message("Can't select any DEP target: %s", nfc_strerror(device)));
cut_assert_equal_int(NMT_DEP, nt.nm.nmt, cut_message("Invalid target modulation"));
cut_assert_equal_int(NBR_212, nt.nm.nbr, cut_message("Invalid target baud rate"));
cut_assert_equal_memory("\x11\x22\x33\x44\x55\x66\x77\x88\x99\xAA", 10, nt.nti.ndi.abtNFCID3, 10, cut_message("Invalid target NFCID3"));
cut_assert_equal_int(NDM_PASSIVE, nt.nti.ndi.ndm, cut_message("Invalid target DEP mode"));
cut_assert_equal_memory("\x12\x34\x56\x78", 4, nt.nti.ndi.abtGB, nt.nti.ndi.szGB, cut_message("Invalid target general bytes"));
if (res <= 0) { thread_res = -1; return (void *) thread_res; }
res = nfc_initiator_transceive_bytes(device, abtTx, sizeof(abtTx), abtRx, sizeof(abtRx), 1000);
cut_assert_operator_int(res, >=, 0, cut_message("Can't transceive bytes to target: %s", nfc_strerror(device)));
cut_assert_equal_memory(abtAttRx, sizeof(abtAttRx), abtRx, res, cut_message("Invalid received data"));
if (res < 0) { thread_res = -1; return (void *) thread_res; }
res = nfc_initiator_deselect_target(device);
cut_assert_operator_int(res, >=, 0, cut_message("Can't deselect target: %s", nfc_strerror(device)));
if (res < 0) { thread_res = -1; return (void *) thread_res; }
// Passive mode / 212Kbps
printf("=========== INITIATOR %s (Passive mode / 212Kbps, second pass) =========\n", nfc_device_get_name(device));
res = nfc_initiator_select_dep_target(device, NDM_PASSIVE, NBR_212, NULL, &nt, 1000);
cut_assert_operator_int(res, >, 0, cut_message("Can't select any DEP target: %s", nfc_strerror(device)));
cut_assert_equal_int(NMT_DEP, nt.nm.nmt, cut_message("Invalid target modulation"));
cut_assert_equal_int(NBR_212, nt.nm.nbr, cut_message("Invalid target baud rate"));
cut_assert_equal_memory("\x11\x22\x33\x44\x55\x66\x77\x88\x99\xAA", 10, nt.nti.ndi.abtNFCID3, 10, cut_message("Invalid target NFCID3"));
cut_assert_equal_int(NDM_PASSIVE, nt.nti.ndi.ndm, cut_message("Invalid target DEP mode"));
cut_assert_equal_memory("\x12\x34\x56\x78", 4, nt.nti.ndi.abtGB, nt.nti.ndi.szGB, cut_message("Invalid target general bytes"));
if (res <= 0) { thread_res = -1; return (void *) thread_res; }
res = nfc_initiator_transceive_bytes(device, abtTx, sizeof(abtTx), abtRx, sizeof(abtRx), 5000);
cut_assert_operator_int(res, >=, 0, cut_message("Can't transceive bytes to target: %s", nfc_strerror(device)));
cut_assert_equal_memory(abtAttRx, sizeof(abtAttRx), abtRx, res, cut_message("Invalid received data"));
if (res < 0) { thread_res = -1; return (void *) thread_res; }
res = nfc_initiator_deselect_target(device);
cut_assert_operator_int(res, >=, 0, cut_message("Can't deselect target: %s", nfc_strerror(device)));
if (res < 0) { thread_res = -1; return (void *) thread_res; }
// Passive mode / 424Kbps
printf("=========== INITIATOR %s (Passive mode / 424Kbps) =========\n", nfc_device_get_name(device));
res = nfc_initiator_select_dep_target(device, NDM_PASSIVE, NBR_424, NULL, &nt, 1000);
cut_assert_operator_int(res, >, 0, cut_message("Can't select any DEP target: %s", nfc_strerror(device)));
cut_assert_equal_int(NMT_DEP, nt.nm.nmt, cut_message("Invalid target modulation"));
cut_assert_equal_int(NBR_424, nt.nm.nbr, cut_message("Invalid target baud rate"));
cut_assert_equal_memory("\x11\x22\x33\x44\x55\x66\x77\x88\x99\xAA", 10, nt.nti.ndi.abtNFCID3, 10, cut_message("Invalid target NFCID3"));
cut_assert_equal_int(NDM_PASSIVE, nt.nti.ndi.ndm, cut_message("Invalid target DEP mode"));
cut_assert_equal_memory("\x12\x34\x56\x78", 4, nt.nti.ndi.abtGB, nt.nti.ndi.szGB, cut_message("Invalid target general bytes"));
if (res <= 0) { thread_res = -1; return (void *) thread_res; }
res = nfc_initiator_transceive_bytes(device, abtTx, sizeof(abtTx), abtRx, sizeof(abtRx), 5000);
cut_assert_operator_int(res, >=, 0, cut_message("Can't transceive bytes to target: %s", nfc_strerror(device)));
cut_assert_equal_memory(abtAttRx, sizeof(abtAttRx), abtRx, res, cut_message("Invalid received data"));
if (res < 0) { thread_res = -1; return (void *) thread_res; }
res = nfc_initiator_deselect_target(device);
cut_assert_operator_int(res, >=, 0, cut_message("Can't deselect target: %s", nfc_strerror(device)));
if (res < 0) { thread_res = -1; return (void *) thread_res; }
return (void *) thread_res;
}
void
test_dep_passive(void)
{
int res;
CutTestContext *test_context = cut_get_current_test_context();
struct thread_data target_data = {
.device = devices[TARGET],
.cut_test_context = test_context,
};
if ((res = pthread_create(&(threads[TARGET]), NULL, target_thread, &target_data)))
cut_fail("pthread_create() returned %d", res);
struct thread_data initiator_data = {
.device = devices[INITIATOR],
.cut_test_context = test_context,
};
if ((res = pthread_create(&(threads[INITIATOR]), NULL, initiator_thread, &initiator_data)))
cut_fail("pthread_create() returned %d", res);
if ((res = pthread_join(threads[INITIATOR], (void *) &result[INITIATOR])))
cut_fail("pthread_join() returned %d", res);
if ((res = pthread_join(threads[TARGET], (void *) &result[TARGET])))
cut_fail("pthread_join() returned %d", res);
cut_assert_equal_int(0, result[INITIATOR], cut_message("Unexpected initiator return code"));
cut_assert_equal_int(0, result[TARGET], cut_message("Unexpected target return code"));
}