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
30 #include <sys/socket.h>
32 #include <linux/socket.h>
33 #include <linux/nfc.h>
35 #include <near/plugin.h>
37 #include <near/types.h>
38 #include <near/adapter.h>
39 #include <near/target.h>
41 #include <near/ndef.h>
44 #define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0]))
47 #define NFC_STATUS_BYTE_LEN 1
49 #define STATUS_WORD_1 1
50 #define STATUS_WORD_2 2
51 #define APDU_HEADER_LEN 5
52 #define APDU_OK 0x9000
53 #define APDU_NOT_FOUND 0x6A82
55 #define T4_ALL_ACCESS 0x00
56 #define T4_READ_ONLY 0xFF
58 #define APDU_STATUS(a) (g_ntohs(*((uint16_t *)(a))))
60 /* Tag Type 4 version ID */
61 static uint8_t iso_appname_v1[] = { 0xd2, 0x76, 0x0, 0x0, 0x85, 0x01, 0x0 };
62 static uint8_t iso_appname_v2[] = { 0xd2, 0x76, 0x0, 0x0, 0x85, 0x01, 0x1 };
65 static uint8_t iso_cc_fileid[] = { 0xe1, 0x03 };
66 #define LEN_ISO_CC_FILEID 2
68 #define LEN_ISO_CC_READ_SIZE 0x0F
70 #define CMD_HEADER_SIZE 5
71 struct type4_cmd { /* iso 7816 */
78 } __attribute__((packed));
80 struct type4_NDEF_file_control_tlv {
81 uint8_t tag ; /* should be 4 */
82 uint8_t len ; /* should be 6 */
84 uint16_t max_ndef_size ;
86 uint8_t write_access ;
87 } __attribute__((packed));
89 struct type4_cc { /* Capability Container */
91 uint8_t mapping_version;
92 uint16_t max_R_apdu_data_size;
93 uint16_t max_C_apdu_data_size;
94 struct type4_NDEF_file_control_tlv tlv_fc ;
96 } __attribute__((packed));
102 struct near_tag *tag;
104 uint16_t r_apdu_max_size;
105 uint16_t c_apdu_max_size;
106 uint16_t max_ndef_size;
107 uint8_t write_access;
108 struct near_ndef_message *ndef;
111 /* ISO functions: This code prepares APDU */
112 static int ISO_send_cmd(uint8_t class,
117 uint8_t cmd_data_length,
121 struct type4_cmd *cmd;
122 struct t4_cookie *in_rcv = in_data;
125 DBG("CLA:x%02x INS:x%02x P1:%02x P2:%02x",
126 class, instruction, param1, param2);
128 cmd = g_try_malloc0(APDU_HEADER_LEN + cmd_data_length);
130 DBG("Mem alloc failed");
136 cmd->instruction = instruction ;
137 cmd->param1 = param1 ;
138 cmd->param2 = param2 ;
139 cmd->data_length = cmd_data_length;
142 memcpy(cmd->data, cmd_data, cmd_data_length);
144 cmd_data_length = 0 ;
146 err = near_adapter_send(in_rcv->adapter_idx, (uint8_t *)cmd,
147 APDU_HEADER_LEN + cmd_data_length , cb, in_rcv);
152 /* On exit, clean memory */
158 /* ISO 7816 command: Select applications or files
159 * p1=0 select by "file id"
160 * P1=4 select by "DF name"
162 static int ISO_Select(uint8_t *filename, uint8_t fnamelen, uint8_t P1,
163 near_recv cb, void *cookie)
169 0xA4, /* INS: Select file */
170 P1, /* P1: select by name */
171 0x00, /* P2: First or only occurence */
172 filename, /* cmd_data */
173 fnamelen, /* uint8_t cmd_data_length*/
178 /* ISO 7816 command: Read binary data from files */
179 static int ISO_ReadBinary(uint16_t offset, uint8_t readsize,
180 near_recv cb, void *cookie)
185 0xB0, /* INS: Select file */
186 (uint8_t)((offset & 0xFF00)>>8),
187 (uint8_t)(offset & 0xFF),
188 0, /* no data send */
189 readsize, /* bytes to read */
194 /* ISO 7816 command: Update data */
195 static int ISO_Update(uint16_t offset, uint8_t nlen,
196 uint8_t *data, near_recv cb, void *cookie)
201 0xD6, /* INS: Select file */
202 (uint8_t)((offset & 0xFF00) >> 8),
203 (uint8_t)(offset & 0xFF),
204 data, /* length of NDEF data */
205 nlen, /* NLEN + NDEF data */
210 static int t4_cookie_release(int err, struct t4_cookie *cookie)
215 if (err < 0 && cookie->cb)
216 cookie->cb(cookie->adapter_idx, cookie->target_idx, err);
219 g_free(cookie->ndef->data);
221 g_free(cookie->ndef);
228 static int data_read_cb(uint8_t *resp, int length, void *data)
230 struct t4_cookie *cookie = data ;
232 uint16_t data_length, length_read, current_length;
233 uint16_t remain_bytes;
239 return t4_cookie_release(err, cookie);
241 if (APDU_STATUS(resp + length - 2) != APDU_OK) {
242 DBG("Fail read_cb SW:x%04x", APDU_STATUS(resp + length - 2));
244 return t4_cookie_release(err, cookie);
247 nfc_data = near_tag_get_data(cookie->tag, (size_t *)&data_length);
249 /* Remove SW1 / SW2 and NFC header */
250 length_read = length - NFC_HEADER_SIZE - 2 ;
251 length = length_read;
253 current_length = cookie->read_data;
255 if (current_length + (length_read) > data_length)
256 length_read = data_length - current_length;
258 memcpy(nfc_data + current_length, resp + NFC_HEADER_SIZE, length_read);
259 if (current_length + length_read == data_length) {
262 near_ndef_parse(cookie->tag, nfc_data, data_length);
263 /* Notify the change */
264 cookie->cb(cookie->adapter_idx, cookie->target_idx, err);
270 cookie->read_data += length ;
271 remain_bytes = (data_length - cookie->read_data);
273 if (remain_bytes >= cookie->r_apdu_max_size)
274 err = ISO_ReadBinary(cookie->read_data + 2,
275 cookie->r_apdu_max_size, data_read_cb, cookie);
277 err = ISO_ReadBinary(cookie->read_data + 2,
278 (uint8_t)remain_bytes, data_read_cb, cookie);
285 return t4_cookie_release(err, cookie);
289 static int t4_readbin_NDEF_ID(uint8_t *resp, int length, void *data)
291 struct t4_cookie *cookie = data ;
292 struct near_tag *tag;
302 if (APDU_STATUS(resp + length - 2) != APDU_OK) {
303 DBG("Fail SW:x%04x", APDU_STATUS(resp + length - 2));
308 /* Add data to the tag */
309 err = near_tag_add_data(cookie->adapter_idx, cookie->target_idx, NULL,
310 g_ntohs(*((uint16_t *)(resp + NFC_STATUS_BYTE_LEN))));
314 tag = near_tag_get_tag(cookie->adapter_idx, cookie->target_idx);
320 near_tag_set_max_ndef_size(tag, cookie->max_ndef_size);
321 near_tag_set_c_apdu_max_size(tag, cookie->c_apdu_max_size);
326 /* Set write conditions */
327 if (cookie->write_access == T4_READ_ONLY)
328 near_tag_set_ro(tag, TRUE);
330 near_tag_set_ro(tag, FALSE);
332 /* TODO: see how we can get the UID value:
333 * near_tag_set_uid(tag, resp + NFC_HEADER_SIZE, 8);
337 err = ISO_ReadBinary(2, cookie->r_apdu_max_size - 2,
338 data_read_cb, cookie);
345 return t4_cookie_release(err, cookie);
348 static int t4_select_NDEF_ID(uint8_t *resp, int length, void *data)
350 struct t4_cookie *cookie = data;
360 /* Check for APDU error */
361 if (APDU_STATUS(resp + STATUS_WORD_1) != APDU_OK) {
362 DBG("Fail SW:x%04x", APDU_STATUS(resp + STATUS_WORD_1));
367 /* Read 0x0f bytes, to grab the NDEF msg length */
368 err = ISO_ReadBinary(0, LEN_ISO_CC_READ_SIZE,
369 t4_readbin_NDEF_ID, cookie);
376 return t4_cookie_release(err, cookie);
380 static int t4_readbin_cc(uint8_t *resp, int length, void *data)
382 struct t4_cookie *cookie = data ;
383 struct type4_cc *read_cc ;
393 /* Check APDU error ( the two last bytes of the resp) */
394 if (APDU_STATUS(resp + length - 2) != APDU_OK) {
395 DBG("Fail SW:x%04x", APDU_STATUS(resp + length - 2));
400 /* -2 for status word and -1 is for NFC first byte... */
401 read_cc = g_try_malloc0(length - 2 - NFC_STATUS_BYTE_LEN);
402 if (read_cc == NULL) {
403 DBG("Mem alloc failed");
408 memcpy(read_cc, &resp[1], length - 2 - NFC_STATUS_BYTE_LEN) ;
410 cookie->r_apdu_max_size = g_ntohs(read_cc->max_R_apdu_data_size) -
412 cookie->c_apdu_max_size = g_ntohs(read_cc->max_C_apdu_data_size);
413 cookie->max_ndef_size = g_ntohs(read_cc->tlv_fc.max_ndef_size);
415 /* TODO 5.1.1 :TLV blocks can be zero, one or more... */
416 /* TODO 5.1.2 :Must ignore proprietary blocks (x05)... */
417 if (read_cc->tlv_fc.tag != 0x4) {
418 DBG("NDEF File Control tag not found !") ;
423 /* save rw conditions */
424 cookie->write_access = read_cc->tlv_fc.write_access;
426 err = ISO_Select((uint8_t *)&read_cc->tlv_fc.file_id,
427 LEN_ISO_CC_FILEID, 0, t4_select_NDEF_ID, cookie);
434 return t4_cookie_release(err, cookie);
437 static int t4_select_cc(uint8_t *resp, int length, void *data)
439 struct t4_cookie *cookie = data;
449 /* Check for APDU error */
450 if (APDU_STATUS(resp + STATUS_WORD_1) != APDU_OK) {
451 DBG("Fail SW:x%04x", APDU_STATUS(resp + STATUS_WORD_1));
456 err = ISO_ReadBinary(0, LEN_ISO_CC_READ_SIZE, t4_readbin_cc, cookie);
463 return t4_cookie_release(err, cookie);
467 static int t4_select_file_by_name_v1(uint8_t *resp, int length, void *data)
469 struct t4_cookie *cookie = data;
479 /* Check for APDU error */
480 if (APDU_STATUS(resp + STATUS_WORD_1) != APDU_OK) {
481 DBG("V1 Fail SW:x%04x", APDU_STATUS(resp + STATUS_WORD_1));
486 if (resp[NFC_STATUS] != 0) {
491 /* Jump to select phase */
492 err = ISO_Select(iso_cc_fileid, LEN_ISO_CC_FILEID, 0,
493 t4_select_cc, cookie);
500 return t4_cookie_release(err, cookie);
504 static int t4_select_file_by_name_v2(uint8_t *resp, int length, void *data)
506 struct t4_cookie *cookie = data;
516 /* Check for APDU error - Not found */
517 if (APDU_STATUS(resp + STATUS_WORD_1) == APDU_NOT_FOUND) {
518 DBG("Fallback to V1");
519 err = ISO_Select(iso_appname_v1, ARRAY_SIZE(iso_appname_v1),
520 0x4, t4_select_file_by_name_v1, cookie);
527 if (APDU_STATUS(resp + STATUS_WORD_1) != APDU_OK) {
528 DBG("V2 Fail SW:x%04x", APDU_STATUS(resp + STATUS_WORD_1));
533 if (resp[NFC_STATUS] != 0) {
538 /* Jump to select phase */
539 err = ISO_Select(iso_cc_fileid, LEN_ISO_CC_FILEID, 0,
540 t4_select_cc, cookie);
547 return t4_cookie_release(err, cookie);
550 static int nfctype4_read_tag(uint32_t adapter_idx,
551 uint32_t target_idx, near_tag_io_cb cb)
553 struct t4_cookie *cookie;
558 cookie = g_try_malloc0(sizeof(struct t4_cookie));
559 if (cookie == NULL) {
564 cookie->adapter_idx = adapter_idx;
565 cookie->target_idx = target_idx;
568 cookie->read_data = 0;;
570 /* Check for V2 type 4 tag */
571 err = ISO_Select(iso_appname_v2, ARRAY_SIZE(iso_appname_v2),
572 0x4, t4_select_file_by_name_v2, cookie);
579 return t4_cookie_release(err, cookie);
582 static int data_write_cb(uint8_t *resp, int length, void *data)
584 struct t4_cookie *cookie = data;
590 return t4_cookie_release(err, cookie);
592 if (APDU_STATUS(resp + length - 2) != APDU_OK) {
593 near_error("write failed SWx%04x",
594 APDU_STATUS(resp + length - 2));
597 return t4_cookie_release(err, cookie);
600 if (cookie->ndef->offset >= cookie->ndef->length) {
602 near_adapter_disconnect(cookie->adapter_idx);
604 return t4_cookie_release(0, cookie);
607 if ((cookie->ndef->length - cookie->ndef->offset) >
608 cookie->c_apdu_max_size) {
609 err = ISO_Update(cookie->ndef->offset,
610 cookie->c_apdu_max_size,
611 cookie->ndef->data + cookie->ndef->offset,
612 data_write_cb, cookie);
613 cookie->ndef->offset += cookie->c_apdu_max_size;
615 err = ISO_Update(cookie->ndef->offset,
616 cookie->ndef->length - cookie->ndef->offset,
617 cookie->ndef->data + cookie->ndef->offset,
618 data_write_cb, cookie);
619 cookie->ndef->offset = cookie->ndef->length;
623 return t4_cookie_release(err, cookie);
628 static int data_write(uint32_t adapter_idx, uint32_t target_idx,
629 struct near_ndef_message *ndef,
630 struct near_tag *tag, near_tag_io_cb cb)
632 struct t4_cookie *cookie;
635 cookie = g_try_malloc0(sizeof(struct t4_cookie));
636 if (cookie == NULL) {
641 cookie->adapter_idx = adapter_idx;
642 cookie->target_idx = target_idx;
645 cookie->read_data = 0;
646 cookie->max_ndef_size = near_tag_get_max_ndef_size(tag);
647 cookie->c_apdu_max_size = near_tag_get_c_apdu_max_size(tag);
650 if (cookie->max_ndef_size < cookie->ndef->length) {
651 near_error("not enough space on tag to write data");
656 if ((cookie->ndef->length - cookie->ndef->offset) >
657 cookie->c_apdu_max_size) {
658 err = ISO_Update(cookie->ndef->offset,
659 cookie->c_apdu_max_size,
661 data_write_cb, cookie);
662 cookie->ndef->offset += cookie->c_apdu_max_size;
664 err = ISO_Update(cookie->ndef->offset,
665 cookie->ndef->length,
667 data_write_cb, cookie);
668 cookie->ndef->offset = cookie->ndef->length;
677 t4_cookie_release(err, cookie);
682 static int nfctype4_write_tag(uint32_t adapter_idx, uint32_t target_idx,
683 struct near_ndef_message *ndef, near_tag_io_cb cb)
685 struct near_tag *tag;
689 if (ndef == NULL || cb == NULL)
692 tag = near_tag_get_tag(adapter_idx, target_idx);
696 if (near_tag_get_ro(tag) == TRUE) {
697 DBG("tag is read-only");
701 return data_write(adapter_idx, target_idx, ndef, tag, cb);
704 static int check_presence(uint8_t *resp, int length, void *data)
706 struct t4_cookie *cookie = data;
715 cookie->cb(cookie->adapter_idx,
716 cookie->target_idx, err);
718 return t4_cookie_release(err, cookie);
721 static int nfctype4_check_presence(uint32_t adapter_idx,
722 uint32_t target_idx, near_tag_io_cb cb)
724 struct t4_cookie *cookie;
729 cookie = g_try_malloc0(sizeof(struct t4_cookie));
730 if (cookie == NULL) {
735 cookie->adapter_idx = adapter_idx;
736 cookie->target_idx = target_idx;
739 cookie->read_data = 0;;
741 /* Check for V2 type 4 tag */
742 err = ISO_Select(iso_appname_v2, ARRAY_SIZE(iso_appname_v2),
743 0x4, check_presence, cookie);
750 return t4_cookie_release(err, cookie);
753 static struct near_tag_driver type4_driver = {
754 .type = NFC_PROTO_ISO14443,
755 .priority = NEAR_TAG_PRIORITY_DEFAULT,
756 .read_tag = nfctype4_read_tag,
757 .add_ndef = nfctype4_write_tag,
758 .check_presence = nfctype4_check_presence,
761 static int nfctype4_init(void)
765 return near_tag_driver_register(&type4_driver);
768 static void nfctype4_exit(void)
772 near_tag_driver_unregister(&type4_driver);
775 NEAR_PLUGIN_DEFINE(nfctype4, "NFC Forum Type 4 tags support", VERSION,
776 NEAR_PLUGIN_PRIORITY_HIGH, nfctype4_init, nfctype4_exit)