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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
|
/*
* neard - Near Field Communication manager
*
* Copyright (C) 2013 Intel Corporation. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include <glib/gprintf.h>
#include "src/near.h"
#include "include/ndef.h"
enum record_type {
RECORD_TYPE_WKT_SMART_POSTER = 0x01,
RECORD_TYPE_WKT_URI = 0x02,
RECORD_TYPE_WKT_TEXT = 0x03,
RECORD_TYPE_WKT_SIZE = 0x04,
RECORD_TYPE_WKT_TYPE = 0x05,
RECORD_TYPE_WKT_ACTION = 0x06,
RECORD_TYPE_WKT_HANDOVER_REQUEST = 0x07,
RECORD_TYPE_WKT_HANDOVER_SELECT = 0x08,
RECORD_TYPE_WKT_HANDOVER_CARRIER = 0x09,
RECORD_TYPE_WKT_ALTERNATIVE_CARRIER = 0x0a,
RECORD_TYPE_WKT_COLLISION_RESOLUTION = 0x0b,
RECORD_TYPE_WKT_ERROR = 0x0c,
RECORD_TYPE_MIME_TYPE = 0x0d,
RECORD_TYPE_EXT_AAR = 0x0e,
RECORD_TYPE_UNKNOWN = 0xfe,
RECORD_TYPE_ERROR = 0xff
};
struct near_ndef_record_header {
uint8_t mb;
uint8_t me;
uint8_t cf;
uint8_t sr;
uint8_t il;
uint8_t tnf;
uint8_t il_length;
uint8_t *il_field;
uint32_t payload_len;
uint32_t offset;
uint8_t type_len;
enum record_type rec_type;
char *type_name;
uint32_t header_len;
};
struct near_ndef_text_payload {
char *encoding;
char *language_code;
char *data;
};
struct near_ndef_uri_payload {
uint8_t identifier;
uint32_t field_length;
uint8_t *field;
};
struct near_ndef_sp_payload {
struct near_ndef_uri_payload *uri;
uint8_t number_of_title_records;
struct near_ndef_text_payload **title_records;
uint32_t size; /* from Size record*/
char *type; /* from Type record*/
char *action;
};
struct near_ndef_aar_payload {
char *package;
};
struct near_ndef_record {
char *path;
struct near_ndef_record_header *header;
/* specific payloads */
struct near_ndef_text_payload *text;
struct near_ndef_uri_payload *uri;
struct near_ndef_sp_payload *sp;
struct near_ndef_mime_payload *mime;
struct near_ndef_ho_payload *ho; /* handover payload */
struct near_ndef_aar_payload *aar;
char *type;
uint8_t *data;
size_t data_len;
};
struct near_ndef_ho_payload {
uint8_t version; /* version id */
uint16_t collision_record; /* collision record */
uint8_t number_of_ac_payloads; /* At least 1 ac is needed */
struct near_ndef_ac_payload **ac_payloads;
/* Optional records */
uint16_t *err_record; /* not NULL if present */
uint8_t number_of_cfg_payloads; /* extra NDEF records */
struct near_ndef_mime_payload **cfg_payloads;
};
struct near_ndef_ac_payload {
enum carrier_power_state cps; /* carrier power state */
uint8_t cdr_len; /* carrier data reference length: 0x01 */
uint8_t *cdr; /* carrier data reference */
uint8_t adata_refcount; /* auxiliary data reference count */
/* !: if adata_refcount == 0, then there's no data reference */
uint16_t **adata; /* auxiliary data reference */
};
/* http://www.intel.com URI NDEF */
static uint8_t uri[] = {0xd1, 0x1, 0xa, 0x55, 0x1, 0x69, 0x6e, 0x74,
0x65, 0x6c, 0x2e, 0x63, 0x6f, 0x6d};
/* 'hello' - UTF-8 - en-US Text NDEF */
static uint8_t text[] = {0xd1, 0x1, 0xb, 0x54, 0x5, 0x65, 0x6e, 0x2d,
0x55, 0x53, 0x68, 0x65, 0x6c, 0x6c, 0x6f};
/* Smart poster with a http://intel.com URI record */
static uint8_t single_sp[] = {0xd1, 0x2, 0xe, 0x53, 0x70, 0xd1, 0x1, 0xa,
0x55, 0x3, 0x69, 0x6e, 0x74, 0x65, 0x6c, 0x2e,
0x63, 0x6f, 0x6d};
/* Smart poster with a http://intel.com URI record and a 'Intel' title */
static uint8_t title_sp[] = {0xd1, 0x2, 0x1a, 0x53, 0x70, 0x91, 0x1, 0xa,
0x55, 0x3, 0x69, 0x6e, 0x74, 0x65, 0x6c, 0x2e,
0x63, 0x6f, 0x6d, 0x51, 0x1, 0x8, 0x54, 0x2,
0x65, 0x6e, 0x49, 0x6e, 0x74, 0x65, 0x6c};
/* AAR record with a "com.example.aar" package name */
static uint8_t aar[] = {0xd4, 0xf, 0xf, 0x61, 0x6e, 0x64, 0x72, 0x6f, 0x69,
0x64, 0x2e, 0x63, 0x6f, 0x6d, 0x3a, 0x70, 0x6b, 0x67,
0x63, 0x6f, 0x6d, 0x2e, 0x65, 0x78, 0x61, 0x6d, 0x70,
0x6c, 0x65, 0x2e, 0x61, 0x61, 0x72};
/* Sample Bluetooth Handover Select Message on an NFC Forum Tag */
static uint8_t ho_hs_bt[] = {0x91, 0x02, 0x0A, 0x48, 0x73, 0x12, 0xD1, 0x02,
0x04, 0x61, 0x63, 0x03, 0x01, 0x30, 0x00, 0x5A,
0x20, 0x1F, 0x01, 0x61, 0x70, 0x70, 0x6C, 0x69,
0x63, 0x61, 0x74, 0x69, 0x6F, 0x6E, 0x2F, 0x76,
0x6E, 0x64, 0x2E, 0x62, 0x6C, 0x75, 0x65, 0x74,
0x6F, 0x6F, 0x74, 0x68, 0x2E, 0x65, 0x70, 0x2E,
0x6F, 0x6F, 0x62, 0x30, 0x1F, 0x00, 0x03, 0x07,
0x80, 0x88, 0xbf, 0x01, 0x04, 0x0D, 0x80, 0x06,
0x04, 0x05, 0x03, 0x18, 0x11, 0x23, 0x11, 0x0B,
0x09, 0x44, 0x65, 0x79, 0x69, 0x63, 0x65, 0x4e,
0x61, 0x6d, 0x65};
static void test_ndef_free_record(struct near_ndef_record *record)
{
g_free(record->header);
g_free(record->type);
g_free(record->data);
g_free(record);
}
static void test_ndef_uri(void)
{
GList *records;
struct near_ndef_record *record;
records = near_ndef_parse_msg(uri, sizeof(uri), NULL);
g_assert(records);
g_assert(g_list_length(records) == 1);
record = (struct near_ndef_record *)(records->data);
g_assert(record->header->rec_type == RECORD_TYPE_WKT_URI);
g_assert(record->header->mb == 1);
g_assert(record->header->me == 1);
g_assert(record->uri);
g_assert(record->uri->field_length == strlen("intel.com"));
g_assert(strncmp((char *) record->uri->field, "intel.com",
record->uri->field_length) == 0);
if (g_test_verbose())
g_print("NDEF URI field: %s\n", record->uri->field);
g_free(record->uri->field);
g_free(record->uri);
test_ndef_free_record(record);
}
static void test_ndef_text(void)
{
GList *records;
struct near_ndef_record *record;
records = near_ndef_parse_msg(text, sizeof(text), NULL);
g_assert(records);
g_assert(g_list_length(records) == 1);
record = (struct near_ndef_record *)(records->data);
g_assert(record->header->rec_type == RECORD_TYPE_WKT_TEXT);
g_assert(record->header->mb == 1);
g_assert(record->header->me == 1);
g_assert(record->text);
g_assert(strcmp(record->text->data, "hello") == 0);
g_assert(strcmp(record->text->encoding, "UTF-8") == 0);
g_assert(strcmp(record->text->language_code, "en-US") == 0);
if (g_test_verbose()) {
g_print("NDEF Text data: %s\n", record->text->data);
g_print("NDEF Text Encoding: %s\n", record->text->encoding);
g_print("NDEF Text Language: %s\n",
record->text->language_code);
}
g_free(record->text->data);
g_free(record->text->encoding);
g_free(record->text->language_code);
g_free(record->text);
test_ndef_free_record(record);
}
static void test_ndef_single_sp(void)
{
GList *records;
struct near_ndef_record *record;
struct near_ndef_uri_payload *uri;
records = near_ndef_parse_msg(single_sp, sizeof(single_sp), NULL);
g_assert(records);
g_assert(g_list_length(records) == 1);
record = (struct near_ndef_record *) records->data;
g_assert(record->header->rec_type == RECORD_TYPE_WKT_SMART_POSTER);
g_assert(record->header->mb == 1);
g_assert(record->header->me == 1);
g_assert(record->sp);
g_assert(record->sp->number_of_title_records == 0);
g_assert(!record->sp->type);
g_assert(!record->sp->action);
g_assert(record->sp->size == 0);
g_assert(record->sp->uri);
uri = (struct near_ndef_uri_payload *) record->sp->uri;
g_assert(uri->field_length == strlen("intel.com"));
g_assert(strncmp((char *) uri->field, "intel.com",
uri->field_length) == 0);
if (g_test_verbose())
g_print("NDEF SP URI field: %.*s\n", uri->field_length,
(char *) uri->field);
g_free(uri->field);
g_free(uri);
g_free(record->sp);
test_ndef_free_record(record);
}
static void test_ndef_title_sp(void)
{
GList *records;
struct near_ndef_record *record;
struct near_ndef_uri_payload *uri;
struct near_ndef_text_payload *text;
records = near_ndef_parse_msg(title_sp, sizeof(title_sp), NULL);
g_assert(records);
g_assert(g_list_length(records) == 1);
record = (struct near_ndef_record *) records->data;
g_assert(record->header->rec_type == RECORD_TYPE_WKT_SMART_POSTER);
g_assert(record->header->mb == 1);
g_assert(record->header->me == 1);
g_assert(record->sp);
g_assert(record->sp->number_of_title_records == 1);
g_assert(!record->sp->type);
g_assert(!record->sp->action);
g_assert(record->sp->size == 0);
g_assert(record->sp->uri);
g_assert(record->sp->title_records[0]);
uri = (struct near_ndef_uri_payload *) record->sp->uri;
text = (struct near_ndef_text_payload *) record->sp->title_records[0];
g_assert(uri->field_length == strlen("intel.com"));
g_assert(strncmp((char *) uri->field, "intel.com",
uri->field_length) == 0);
if (g_test_verbose())
g_print("NDEF SP URI field: %.*s\n", uri->field_length,
(char *) uri->field);
g_assert(strcmp(text->data, "Intel") == 0);
g_assert(strcmp(text->encoding, "UTF-8") == 0);
g_assert(strcmp(text->language_code, "en") == 0);
if (g_test_verbose()) {
g_print("NDEF SP Title data: %s\n", text->data);
g_print("NDEF SP Title Encoding: %s\n", text->encoding);
g_print("NDEF SP Title Language: %s\n", text->language_code);
}
g_free(uri->field);
g_free(uri);
g_free(text->data);
g_free(text->encoding);
g_free(text->language_code);
g_free(text);
g_free(record->sp);
test_ndef_free_record(record);
}
static void test_ndef_aar(void)
{
GList *records;
struct near_ndef_record *record;
records = near_ndef_parse_msg(aar, sizeof(aar), NULL);
g_assert(records);
g_assert(g_list_length(records) == 1);
record = (struct near_ndef_record *)(records->data);
g_assert(record->header->rec_type == RECORD_TYPE_EXT_AAR);
g_assert(record->header->mb == 1);
g_assert(record->header->me == 1);
g_assert(record->aar);
g_assert(record->aar->package);
g_assert(strcmp((char *) record->aar->package, "com.example.aar") == 0);
if (g_test_verbose())
g_print("NDEF AAR package: %s\n", record->aar->package);
g_free(record->aar->package);
g_free(record->aar);
test_ndef_free_record(record);
}
static void test_ndef_ho_hs_bt(void)
{
GList *records;
struct near_ndef_record *record;
struct near_ndef_ho_payload *ho;
struct near_ndef_ac_payload *ac;
/* TODO
* It is not possible to run HO code as unit test now as this code does
* both parsing and acting (eg. trying to access HO agent) in same run.
* Don't enable this test until it is fixed.
*/
return;
records = near_ndef_parse_msg(ho_hs_bt, sizeof(ho_hs_bt), NULL);
g_assert(records);
g_assert(g_list_length(records) == 2);
record = records->data;
ho = record->ho;
g_assert(ho->number_of_ac_payloads == 1);
ac = ho->ac_payloads[0];
g_assert(ac->cdr_len == 1);
g_assert(memcmp(ac->cdr, "0", ac->cdr_len) == 0);
records = g_list_next(records);
record = records->data;
g_assert(strcmp(record->type, BT_MIME_STRING_2_1) == 0);
}
int main(int argc, char **argv)
{
g_test_init(&argc, &argv, NULL);
g_test_add_func("/testNDEF-parse/Test URI NDEF", test_ndef_uri);
g_test_add_func("/testNDEF-parse/Test Text NDEF", test_ndef_text);
g_test_add_func("/testNDEF-parse/Test Single record SmartPoster NDEF",
test_ndef_single_sp);
g_test_add_func("/testNDEF-parse/Test Title record SmartPoster NDEF",
test_ndef_title_sp);
g_test_add_func("/testNDEF-parse/Android Application Record NDEF",
test_ndef_aar);
g_test_add_func("/testNDEF-parse/Test Handover Select NDEF",
test_ndef_ho_hs_bt);
return g_test_run();
}
|