3 * neard - Near Field Communication manager
5 * Copyright (C) 2011 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
29 #include <sys/socket.h>
31 #include <linux/socket.h>
32 #include <linux/nfc.h>
34 #include <near/plugin.h>
36 #include <near/types.h>
37 #include <near/adapter.h>
38 #include <near/target.h>
40 #include <near/ndef.h>
43 extern int mifare_read_tag(uint32_t adapter_idx, uint32_t target_idx,
44 near_tag_io_cb cb, enum near_tag_sub_type tgt_subtype);
47 #define CMD_WRITE 0xA2
52 #define META_BLOCK_START 0
53 #define DATA_BLOCK_START 4
54 #define TYPE2_MAGIC 0xe1
56 #define TAG_DATA_CC(data) ((data) + 12)
57 #define TAG_DATA_LENGTH(cc) ((cc)[2] * 8)
58 #define TAG_DATA_NFC(cc) ((cc)[0] & TYPE2_MAGIC)
60 #define TYPE2_NOWRITE_ACCESS 0x0F
61 #define TAG_T2_WRITE_FLAG(cc) ((cc)[3] & TYPE2_NOWRITE_ACCESS)
63 #define NDEF_MAX_SIZE 0x30
68 uint8_t data[BLOCK_SIZE];
69 } __attribute__((packed));
73 uint16_t current_block;
82 uint8_t current_block;
83 struct near_ndef_message *ndef;
87 static void t2_cookie_release(struct t2_cookie *cookie)
93 g_free(cookie->ndef->data);
100 static int data_recv(uint8_t *resp, int length, void *data)
102 struct type2_tag *tag = data;
103 struct type2_cmd cmd;
105 uint16_t current_length, length_read, data_length;
106 uint32_t adapter_idx;
117 nfc_data = near_tag_get_data(tag->tag, (size_t *)&data_length);
118 adapter_idx = near_tag_get_adapter_idx(tag->tag);
120 length_read = length - NFC_HEADER_SIZE;
121 current_length = tag->current_block * BLOCK_SIZE;
122 if (current_length + length - NFC_HEADER_SIZE > data_length)
123 length_read = data_length - current_length;
125 memcpy(nfc_data + current_length, resp + NFC_HEADER_SIZE, length_read);
127 if (current_length + length_read == data_length) {
128 /* TODO parse tag->data for NDEFS, and notify target.c */
129 tag->current_block = 0;
133 near_tlv_parse(tag->tag, tag->cb);
140 read_blocks = length / BLOCK_SIZE;
141 tag->current_block += read_blocks;
144 cmd.block = DATA_BLOCK_START + tag->current_block;
146 DBG("adapter %d", adapter_idx);
148 return near_adapter_send(adapter_idx,
149 (uint8_t *)&cmd, sizeof(cmd),
153 static int data_read(struct type2_tag *tag)
155 struct type2_cmd cmd;
156 uint32_t adapter_idx;
160 tag->current_block = 0;
163 cmd.block = DATA_BLOCK_START;
165 adapter_idx = near_tag_get_adapter_idx(tag->tag);
167 return near_adapter_send(adapter_idx,
168 (uint8_t *)&cmd, sizeof(cmd),
172 static int meta_recv(uint8_t *resp, int length, void *data)
174 struct t2_cookie *cookie = data;
175 struct near_tag *tag;
176 struct type2_tag *t2_tag;
192 cc = TAG_DATA_CC(resp + NFC_HEADER_SIZE);
194 if (TAG_DATA_NFC(cc) == 0) {
199 err = near_tag_add_data(cookie->adapter_idx, cookie->target_idx,
200 NULL, TAG_DATA_LENGTH(cc));
204 tag = near_tag_get_tag(cookie->adapter_idx, cookie->target_idx);
210 t2_tag = g_try_malloc0(sizeof(struct type2_tag));
211 if (t2_tag == NULL) {
216 t2_tag->adapter_idx = cookie->adapter_idx;
217 t2_tag->cb = cookie->cb;
220 /* Set the ReadWrite flag */
221 if (TAG_T2_WRITE_FLAG(cc) == TYPE2_NOWRITE_ACCESS)
222 near_tag_set_ro(tag, TRUE);
224 near_tag_set_ro(tag, FALSE);
226 near_tag_set_memory_layout(tag, NEAR_TAG_MEMORY_STATIC);
228 err = data_read(t2_tag);
232 t2_cookie_release(cookie);
237 if (err < 0 && cookie->cb)
238 cookie->cb(cookie->adapter_idx, cookie->target_idx, err);
240 t2_cookie_release(cookie);
245 static int nfctype2_read_meta(uint32_t adapter_idx, uint32_t target_idx,
248 struct type2_cmd cmd;
249 struct t2_cookie *cookie;
254 cmd.block = META_BLOCK_START;
256 cookie = g_try_malloc0(sizeof(struct t2_cookie));
257 cookie->adapter_idx = adapter_idx;
258 cookie->target_idx = target_idx;
261 return near_adapter_send(adapter_idx, (uint8_t *)&cmd, sizeof(cmd),
265 static int nfctype2_read_tag(uint32_t adapter_idx,
266 uint32_t target_idx, near_tag_io_cb cb)
269 enum near_tag_sub_type tgt_subtype;
273 tgt_subtype = near_tag_get_subtype(adapter_idx, target_idx);
275 switch (tgt_subtype) {
276 case NEAR_TAG_NFC_T2_MIFARE_ULTRALIGHT:
277 err = nfctype2_read_meta(adapter_idx, target_idx, cb);
280 /* Specific Mifare read access */
281 case NEAR_TAG_NFC_T2_MIFARE_CLASSIC_1K:
282 case NEAR_TAG_NFC_T2_MIFARE_CLASSIC_4K:
283 err= mifare_read_tag( adapter_idx, target_idx,
288 DBG("Unknown TAG Type 2 subtype (%d)", tgt_subtype);
296 static int data_write_resp(uint8_t *resp, int length, void *data)
299 struct t2_cookie *cookie = data;
300 struct type2_cmd cmd;
304 if (length < 0 || resp[0] != 0) {
309 if (cookie->ndef->offset > cookie->ndef->length) {
313 cookie->cb(cookie->adapter_idx, cookie->target_idx, 0);
315 t2_cookie_release(cookie);
321 cmd.block = cookie->current_block;
322 cookie->current_block++;
324 if ((cookie->ndef->offset + BLOCK_SIZE) <
325 cookie->ndef->length) {
326 memcpy(cmd.data, cookie->ndef->data +
327 cookie->ndef->offset, BLOCK_SIZE);
328 cookie->ndef->offset += BLOCK_SIZE;
330 memcpy(cmd.data, cookie->ndef->data + cookie->ndef->offset,
331 cookie->ndef->length - cookie->ndef->offset);
332 cookie->ndef->offset = cookie->ndef->length + 1;
335 err = near_adapter_send(cookie->adapter_idx, (uint8_t *)&cmd,
336 sizeof(cmd), data_write_resp, cookie);
345 if (err < 0 && cookie->cb)
346 cookie->cb(cookie->adapter_idx, cookie->target_idx, err);
348 t2_cookie_release(cookie);
353 static int data_write(uint32_t adapter_idx, uint32_t target_idx,
354 struct near_ndef_message *ndef,
357 struct type2_cmd cmd;
358 struct t2_cookie *cookie;
363 cookie = g_try_malloc0(sizeof(struct t2_cookie));
364 cookie->adapter_idx = adapter_idx;
365 cookie->target_idx = target_idx;
366 cookie->current_block = DATA_BLOCK_START;
371 cmd.block = cookie->current_block;
372 memcpy(cmd.data, cookie->ndef->data, BLOCK_SIZE);
373 cookie->ndef->offset += BLOCK_SIZE;
374 cookie->current_block++;
376 err = near_adapter_send(cookie->adapter_idx, (uint8_t *)&cmd,
377 sizeof(cmd), data_write_resp, cookie);
385 t2_cookie_release(cookie);
390 static int nfctype2_write_tag(uint32_t adapter_idx, uint32_t target_idx,
391 struct near_ndef_message *ndef,
394 struct near_tag *tag;
395 enum near_tag_sub_type tgt_subtype;
399 if (ndef == NULL || cb == NULL)
402 tag = near_tag_get_tag(adapter_idx, target_idx);
406 if (near_tag_get_ro(tag) == TRUE) {
407 DBG("tag is read-only");
411 tgt_subtype = near_tag_get_subtype(adapter_idx, target_idx);
413 if (tgt_subtype != NEAR_TAG_NFC_T2_MIFARE_ULTRALIGHT) {
414 DBG("Unknown Tag Type 2 subtype (%d)", tgt_subtype);
418 /* This check is valid for only static tags.
419 * Max data length on Type 2 Tag including TLV's is NDEF_MAX_SIZE */
420 if (near_tag_get_memory_layout(tag) == NEAR_TAG_MEMORY_STATIC) {
421 if ((ndef->length + 3) > NDEF_MAX_SIZE) {
422 near_error("not enough space on tag");
427 return data_write(adapter_idx, target_idx, ndef, cb);
430 static int check_presence(uint8_t *resp, int length, void *data)
432 struct t2_cookie *cookie = data;
441 cookie->cb(cookie->adapter_idx,
442 cookie->target_idx, err);
444 t2_cookie_release(cookie);
449 static int nfctype2_check_presence(uint32_t adapter_idx, uint32_t target_idx,
452 struct type2_cmd cmd;
453 struct t2_cookie *cookie;
459 cmd.block = META_BLOCK_START;
461 cookie = g_try_malloc0(sizeof(struct t2_cookie));
465 cookie->adapter_idx = adapter_idx;
466 cookie->target_idx = target_idx;
469 err = near_adapter_send(adapter_idx, (uint8_t *)&cmd, sizeof(cmd),
470 check_presence, cookie);
477 t2_cookie_release(cookie);
482 static struct near_tag_driver type2_driver = {
483 .type = NFC_PROTO_MIFARE,
484 .priority = NEAR_TAG_PRIORITY_DEFAULT,
485 .read_tag = nfctype2_read_tag,
486 .add_ndef = nfctype2_write_tag,
487 .check_presence = nfctype2_check_presence,
490 static int nfctype2_init(void)
494 return near_tag_driver_register(&type2_driver);
497 static void nfctype2_exit(void)
501 near_tag_driver_unregister(&type2_driver);
504 NEAR_PLUGIN_DEFINE(nfctype2, "NFC Forum Type 2 tags support", VERSION,
505 NEAR_PLUGIN_PRIORITY_HIGH, nfctype2_init, nfctype2_exit)