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
37 #define TYPE3_IDM_LEN 8
38 #define TYPE3_ATTR_BLOCK_SIZE 16
48 enum near_tag_sub_type sub_type;
49 enum near_tag_memory_layout layout;
52 uint8_t nfcid[NFC_MAX_NFCID1_LEN];
62 /* Tag specific structures */
64 uint8_t IDm[TYPE3_IDM_LEN];
65 uint8_t attr[TYPE3_ATTR_BLOCK_SIZE];
70 uint16_t max_ndef_size;
71 uint16_t c_apdu_max_size;
74 DBusMessage *write_msg; /* Pending write message */
75 struct near_ndef_message *write_ndef;
78 static DBusConnection *connection = NULL;
80 static GHashTable *tag_hash;
82 static GSList *driver_list = NULL;
84 struct near_tag *near_tag_get_tag(uint32_t adapter_idx, uint32_t target_idx)
89 path = g_strdup_printf("%s/nfc%d/tag%d", NFC_PATH,
90 adapter_idx, target_idx);
94 tag = g_hash_table_lookup(tag_hash, path);
101 static void append_records(DBusMessageIter *iter, void *user_data)
103 struct near_tag *tag = user_data;
108 for (list = tag->records; list; list = list->next) {
109 struct near_ndef_record *record = list->data;
112 path = __near_ndef_record_get_path(record);
116 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
121 static const char *type_string(struct near_tag *tag)
125 DBG("type 0x%x", tag->type);
128 case NFC_PROTO_JEWEL:
132 case NFC_PROTO_MIFARE:
136 case NFC_PROTO_FELICA:
140 case NFC_PROTO_ISO14443:
146 near_error("Unknown tag type 0x%x", tag->type);
153 static const char *protocol_string(struct near_tag *tag)
155 const char *protocol;
157 DBG("protocol 0x%x", tag->protocol);
159 switch (tag->protocol) {
160 case NFC_PROTO_FELICA_MASK:
164 case NFC_PROTO_MIFARE_MASK:
168 case NFC_PROTO_JEWEL_MASK:
172 case NFC_PROTO_ISO14443_MASK:
173 protocol = "ISO-DEP";
177 near_error("Unknown tag protocol 0x%x", tag->protocol);
184 static DBusMessage *get_properties(DBusConnection *conn,
185 DBusMessage *msg, void *data)
187 struct near_tag *tag = data;
188 const char *protocol, *type;
190 DBusMessageIter array, dict;
192 DBG("conn %p", conn);
194 reply = dbus_message_new_method_return(msg);
198 dbus_message_iter_init_append(reply, &array);
200 near_dbus_dict_open(&array, &dict);
202 type = type_string(tag);
204 near_dbus_dict_append_basic(&dict, "Type",
205 DBUS_TYPE_STRING, &type);
207 protocol = protocol_string(tag);
208 if (protocol != NULL)
209 near_dbus_dict_append_basic(&dict, "Protocol",
210 DBUS_TYPE_STRING, &protocol);
212 near_dbus_dict_append_basic(&dict, "ReadOnly",
213 DBUS_TYPE_BOOLEAN, &tag->readonly);
215 near_dbus_dict_append_array(&dict, "Records",
216 DBUS_TYPE_OBJECT_PATH, append_records, tag);
218 near_dbus_dict_close(&array, &dict);
223 static DBusMessage *set_property(DBusConnection *conn,
224 DBusMessage *msg, void *data)
226 DBG("conn %p", conn);
228 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
231 static void tag_read_cb(uint32_t adapter_idx, uint32_t target_idx, int status)
233 struct near_tag *tag;
235 tag = near_tag_get_tag(adapter_idx, target_idx);
240 dbus_message_unref(tag->write_msg);
241 tag->write_msg = NULL;
243 __near_adapter_start_check_presence(adapter_idx, target_idx);
245 __near_adapter_tags_changed(adapter_idx);
248 static void write_cb(uint32_t adapter_idx, uint32_t target_idx, int status)
250 struct near_tag *tag;
251 DBusConnection *conn;
254 DBG("Write status %d", status);
256 tag = near_tag_get_tag(adapter_idx, target_idx);
260 conn = near_dbus_get_connection();
265 reply = __near_error_failed(tag->write_msg, EINVAL);
267 g_dbus_send_message(conn, reply);
269 g_dbus_send_reply(conn, tag->write_msg, DBUS_TYPE_INVALID);
272 near_ndef_records_free(tag->records);
280 * If writing succeeded,
281 * check presence will be restored after reading
283 __near_tag_read(tag, tag_read_cb);
288 dbus_message_unref(tag->write_msg);
289 tag->write_msg = NULL;
291 __near_adapter_start_check_presence(tag->adapter_idx, tag->target_idx);
294 static void format_cb(uint32_t adapter_idx, uint32_t target_idx, int status)
296 struct near_tag *tag;
299 DBG("format status %d", status);
301 tag = near_tag_get_tag(adapter_idx, target_idx);
305 if (tag->write_msg == NULL)
309 err = __near_tag_write(tag, tag->write_ndef,
321 write_cb(tag->adapter_idx, tag->target_idx, err);
324 static DBusMessage *write_ndef(DBusConnection *conn,
325 DBusMessage *msg, void *data)
327 struct near_tag *tag = data;
328 struct near_ndef_message *ndef, *ndef_with_header = NULL;
329 int tlv_len_size, err;
331 DBG("conn %p", conn);
333 if (tag->readonly == TRUE) {
334 DBG("Read only tag");
335 return __near_error_permission_denied(msg);
339 return __near_error_in_progress(msg);
341 ndef = __ndef_build_from_message(msg);
343 return __near_error_failed(msg, EINVAL);
345 tag->write_msg = dbus_message_ref(msg);
347 /* Add NDEF header information depends upon tag type */
349 case NFC_PROTO_JEWEL:
350 case NFC_PROTO_MIFARE:
351 if (ndef->length < 0xff)
356 ndef_with_header = g_try_malloc0(sizeof(
357 struct near_ndef_message));
358 if (ndef_with_header == NULL)
361 ndef_with_header->offset = 0;
362 ndef_with_header->length = ndef->length + tlv_len_size;
363 ndef_with_header->data =
364 g_try_malloc0(ndef->length + tlv_len_size);
365 if (ndef_with_header->data == NULL)
368 ndef_with_header->data[0] = TLV_NDEF;
370 if (ndef->length < 0xff) {
371 ndef_with_header->data[1] = ndef->length;
373 ndef_with_header->data[1] = 0xff;
374 ndef_with_header->data[2] =
375 (uint8_t)(ndef->length >> 8);
376 ndef_with_header->data[3] = (uint8_t)(ndef->length);
379 memcpy(ndef_with_header->data + tlv_len_size - 1, ndef->data,
381 ndef_with_header->data[ndef->length + tlv_len_size - 1] =
385 case NFC_PROTO_FELICA:
386 ndef_with_header = g_try_malloc0(sizeof(
387 struct near_ndef_message));
388 if (ndef_with_header == NULL)
391 ndef_with_header->offset = 0;
392 ndef_with_header->length = ndef->length;
393 ndef_with_header->data = g_try_malloc0(
394 ndef_with_header->length);
395 if (ndef_with_header->data == NULL)
398 memcpy(ndef_with_header->data, ndef->data, ndef->length);
402 case NFC_PROTO_ISO14443:
403 ndef_with_header = g_try_malloc0(sizeof(
404 struct near_ndef_message));
405 if (ndef_with_header == NULL)
408 ndef_with_header->offset = 0;
409 ndef_with_header->length = ndef->length + 2;
410 ndef_with_header->data = g_try_malloc0(ndef->length + 2);
411 if (ndef_with_header->data == NULL)
414 ndef_with_header->data[0] = (uint8_t)(ndef->length >> 8);
415 ndef_with_header->data[1] = (uint8_t)(ndef->length);
416 memcpy(ndef_with_header->data + 2, ndef->data, ndef->length);
424 return __near_error_failed(msg, EOPNOTSUPP);
430 tag->write_ndef = ndef_with_header;
431 err = __near_tag_write(tag, ndef_with_header, write_cb);
433 g_free(ndef_with_header->data);
434 g_free(ndef_with_header);
436 return __near_error_failed(msg, -err);
439 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
442 dbus_message_unref(tag->write_msg);
443 tag->write_msg = NULL;
445 return __near_error_failed(msg, ENOMEM);
448 static DBusMessage *get_raw_ndef(DBusConnection *conn,
449 DBusMessage *msg, void *data)
451 struct near_tag *tag = data;
453 DBusMessageIter iter, array;
457 reply = dbus_message_new_method_return(msg);
461 dbus_message_iter_init_append(reply, &iter);
463 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
464 DBUS_TYPE_BYTE_AS_STRING,
467 __near_ndef_append_records(&array, tag->records);
469 dbus_message_iter_close_container(&iter, &array);
474 static const GDBusMethodTable tag_methods[] = {
475 { GDBUS_METHOD("GetProperties",
476 NULL, GDBUS_ARGS({"properties", "a{sv}"}),
478 { GDBUS_METHOD("SetProperty",
479 GDBUS_ARGS({"name", "s"}, {"value", "v"}),
480 NULL, set_property) },
481 { GDBUS_ASYNC_METHOD("Write", GDBUS_ARGS({"attributes", "a{sv}"}),
483 { GDBUS_METHOD("GetRawNDEF",
484 NULL, GDBUS_ARGS({"NDEF", "ay"}),
489 static const GDBusSignalTable tag_signals[] = {
490 { GDBUS_SIGNAL("PropertyChanged",
491 GDBUS_ARGS({"name", "s"}, {"value", "v"})) },
496 void __near_tag_append_records(struct near_tag *tag, DBusMessageIter *iter)
500 for (list = tag->records; list; list = list->next) {
501 struct near_ndef_record *record = list->data;
504 path = __near_ndef_record_get_path(record);
508 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
513 #define NFC_TAG_A (NFC_PROTO_ISO14443_MASK | NFC_PROTO_NFC_DEP_MASK | \
514 NFC_PROTO_JEWEL_MASK | NFC_PROTO_MIFARE_MASK)
515 #define NFC_TAG_A_TYPE2 0x00
516 #define NFC_TAG_A_TYPE4 0x01
517 #define NFC_TAG_A_NFC_DEP 0x02
518 #define NFC_TAG_A_TYPE4_DEP 0x03
520 #define NFC_TAG_A_SENS_RES_SSD_JEWEL 0x00
521 #define NFC_TAG_A_SENS_RES_PLATCONF_JEWEL 0x0c
523 #define NFC_TAG_A_SEL_PROT(sel_res) (((sel_res) & 0x60) >> 5)
524 #define NFC_TAG_A_SEL_CASCADE(sel_res) (((sel_res) & 0x04) >> 2)
525 #define NFC_TAG_A_SENS_RES_SSD(sens_res) ((sens_res) & 0x001f)
526 #define NFC_TAG_A_SENS_RES_PLATCONF(sens_res) (((sens_res) & 0x0f00) >> 8)
528 static enum near_tag_sub_type get_tag_type2_sub_type(uint8_t sel_res)
532 return NEAR_TAG_NFC_T2_MIFARE_ULTRALIGHT;
534 return NEAR_TAG_NFC_T2_MIFARE_CLASSIC_1K;
536 return NEAR_TAG_NFC_T2_MIFARE_MINI;
538 return NEAR_TAG_NFC_T2_MIFARE_CLASSIC_4K;
540 return NEAR_TAG_NFC_T2_MIFARE_DESFIRE;
542 return NEAR_TAG_NFC_T2_JCOP30;
544 return NEAR_TAG_NFC_T2_MIFARE_4K_EMUL;
546 return NEAR_TAG_NFC_T2_MIFARE_1K_INFINEON;
548 return NEAR_TAG_NFC_T2_MPCOS;
551 return NEAR_TAG_NFC_SUBTYPE_UNKNOWN;
554 static void set_tag_type(struct near_tag *tag,
555 uint16_t sens_res, uint8_t sel_res)
557 uint8_t platconf, ssd, proto;
559 DBG("protocol 0x%x sens_res 0x%x sel_res 0x%x", tag->protocol,
562 switch (tag->protocol) {
563 case NFC_PROTO_JEWEL_MASK:
564 platconf = NFC_TAG_A_SENS_RES_PLATCONF(sens_res);
565 ssd = NFC_TAG_A_SENS_RES_SSD(sens_res);
569 if ((ssd == NFC_TAG_A_SENS_RES_SSD_JEWEL) &&
570 (platconf == NFC_TAG_A_SENS_RES_PLATCONF_JEWEL))
571 tag->type = NFC_PROTO_JEWEL;
574 case NFC_PROTO_MIFARE_MASK:
575 case NFC_PROTO_ISO14443_MASK:
576 proto = NFC_TAG_A_SEL_PROT(sel_res);
578 DBG("proto 0x%x", proto);
581 case NFC_TAG_A_TYPE2:
582 tag->type = NFC_PROTO_MIFARE;
583 tag->sub_type = get_tag_type2_sub_type(sel_res);
585 case NFC_TAG_A_TYPE4:
586 tag->type = NFC_PROTO_ISO14443;
588 case NFC_TAG_A_TYPE4_DEP:
589 tag->type = NFC_PROTO_NFC_DEP;
594 case NFC_PROTO_FELICA_MASK:
595 tag->type = NFC_PROTO_FELICA;
599 tag->type = NFC_PROTO_MAX;
603 DBG("tag type 0x%x", tag->type);
606 static int tag_initialize(struct near_tag *tag,
607 uint32_t adapter_idx, uint32_t target_idx,
609 uint16_t sens_res, uint8_t sel_res,
610 uint8_t *nfcid, uint8_t nfcid_len)
614 tag->path = g_strdup_printf("%s/nfc%d/tag%d", NFC_PATH,
615 adapter_idx, target_idx);
616 if (tag->path == NULL)
618 tag->adapter_idx = adapter_idx;
619 tag->target_idx = target_idx;
620 tag->protocol = protocols;
622 tag->readonly = FALSE;
624 if (nfcid_len <= NFC_MAX_NFCID1_LEN) {
625 tag->nfcid_len = nfcid_len;
626 memcpy(tag->nfcid, nfcid, nfcid_len);
629 set_tag_type(tag, sens_res, sel_res);
634 struct near_tag *__near_tag_add(uint32_t adapter_idx, uint32_t target_idx,
636 uint16_t sens_res, uint8_t sel_res,
637 uint8_t *nfcid, uint8_t nfcid_len)
639 struct near_tag *tag;
642 tag = near_tag_get_tag(adapter_idx, target_idx);
646 tag = g_try_malloc0(sizeof(struct near_tag));
650 if (tag_initialize(tag, adapter_idx, target_idx,
653 nfcid, nfcid_len) < 0) {
658 path = g_strdup(tag->path);
664 g_hash_table_insert(tag_hash, path, tag);
666 DBG("connection %p", connection);
668 g_dbus_register_interface(connection, tag->path,
670 tag_methods, tag_signals,
676 void __near_tag_remove(struct near_tag *tag)
678 char *path = tag->path;
680 DBG("path %s", tag->path);
682 if (g_hash_table_lookup(tag_hash, tag->path) == NULL)
685 g_dbus_unregister_interface(connection, tag->path,
688 g_hash_table_remove(tag_hash, path);
691 const char *__near_tag_get_path(struct near_tag *tag)
696 uint32_t __near_tag_get_type(struct near_tag *tag)
701 enum near_tag_sub_type near_tag_get_subtype(uint32_t adapter_idx,
705 struct near_tag *tag;
707 tag = near_tag_get_tag(adapter_idx, target_idx);
709 return NEAR_TAG_NFC_SUBTYPE_UNKNOWN;
711 return tag->sub_type;
714 uint8_t *near_tag_get_nfcid(uint32_t adapter_idx, uint32_t target_idx,
717 struct near_tag *tag;
720 tag = near_tag_get_tag(adapter_idx, target_idx);
724 nfcid = g_try_malloc0(tag->nfcid_len);
728 memcpy(nfcid, tag->nfcid, tag->nfcid_len);
729 *nfcid_len = tag->nfcid_len;
738 int near_tag_set_nfcid(uint32_t adapter_idx, uint32_t target_idx,
739 uint8_t *nfcid, size_t nfcid_len)
741 struct near_tag *tag;
743 DBG("NFCID len %zd", nfcid_len);
745 tag = near_tag_get_tag(adapter_idx, target_idx);
749 if (tag->nfcid_len > 0)
752 if (nfcid_len > NFC_MAX_NFCID1_LEN)
755 memcpy(tag->nfcid, nfcid, nfcid_len);
756 tag->nfcid_len = nfcid_len;
761 int near_tag_add_data(uint32_t adapter_idx, uint32_t target_idx,
762 uint8_t *data, size_t data_length)
764 struct near_tag *tag;
766 tag = near_tag_get_tag(adapter_idx, target_idx);
770 tag->data_length = data_length;
771 tag->data = g_try_malloc0(data_length);
772 if (tag->data == NULL)
776 memcpy(tag->data, data, data_length);
781 int near_tag_add_records(struct near_tag *tag, GList *records,
782 near_tag_io_cb cb, int status)
785 struct near_ndef_record *record;
788 DBG("records %p", records);
790 for (list = records; list; list = list->next) {
793 path = g_strdup_printf("%s/nfc%d/tag%d/record%d",
794 NFC_PATH, tag->adapter_idx,
795 tag->target_idx, tag->n_records);
800 __near_ndef_record_register(record, path);
803 tag->records = g_list_append(tag->records, record);
806 __near_agent_ndef_parse_records(tag->records);
809 cb(tag->adapter_idx, tag->target_idx, status);
811 g_list_free(records);
816 void near_tag_set_ro(struct near_tag *tag, near_bool_t readonly)
818 tag->readonly = readonly;
821 void near_tag_set_blank(struct near_tag *tag, near_bool_t blank)
826 near_bool_t near_tag_get_blank(struct near_tag *tag)
831 uint8_t *near_tag_get_data(struct near_tag *tag, size_t *data_length)
833 if (data_length == NULL)
836 *data_length = tag->data_length;
841 uint32_t near_tag_get_adapter_idx(struct near_tag *tag)
843 return tag->adapter_idx;
846 uint32_t near_tag_get_target_idx(struct near_tag *tag)
848 return tag->target_idx;
851 enum near_tag_memory_layout near_tag_get_memory_layout(struct near_tag *tag)
854 return NEAR_TAG_MEMORY_UNKNOWN;
859 void near_tag_set_memory_layout(struct near_tag *tag,
860 enum near_tag_memory_layout layout)
865 tag->layout = layout;
868 void near_tag_set_max_ndef_size(struct near_tag *tag, uint16_t size)
873 tag->t4.max_ndef_size = size;
876 uint16_t near_tag_get_max_ndef_size(struct near_tag *tag)
881 return tag->t4.max_ndef_size;
884 void near_tag_set_c_apdu_max_size(struct near_tag *tag, uint16_t size)
889 tag->t4.c_apdu_max_size = size;
892 uint16_t near_tag_get_c_apdu_max_size(struct near_tag *tag)
897 return tag->t4.c_apdu_max_size;
900 void near_tag_set_idm(struct near_tag *tag, uint8_t *idm, uint8_t len)
902 if (tag == NULL || len > TYPE3_IDM_LEN)
905 memset(tag->t3.IDm, 0, TYPE3_IDM_LEN);
906 memcpy(tag->t3.IDm, idm, len);
909 uint8_t *near_tag_get_idm(struct near_tag *tag, uint8_t *len)
911 if (tag == NULL || len == NULL)
914 *len = TYPE3_IDM_LEN;
918 void near_tag_set_attr_block(struct near_tag *tag, uint8_t *attr, uint8_t len)
920 if (tag == NULL || len > TYPE3_ATTR_BLOCK_SIZE)
923 memset(tag->t3.attr, 0, TYPE3_ATTR_BLOCK_SIZE);
924 memcpy(tag->t3.attr, attr, len);
927 uint8_t *near_tag_get_attr_block(struct near_tag *tag, uint8_t *len)
929 if (tag == NULL || len == NULL)
932 *len = TYPE3_ATTR_BLOCK_SIZE;
936 void near_tag_set_ic_type(struct near_tag *tag, uint8_t ic_type)
941 tag->t3.ic_type = ic_type;
944 uint8_t near_tag_get_ic_type(struct near_tag *tag)
949 return tag->t3.ic_type;
952 static gint cmp_prio(gconstpointer a, gconstpointer b)
954 const struct near_tag_driver *driver1 = a;
955 const struct near_tag_driver *driver2 = b;
957 return driver2->priority - driver1->priority;
960 int near_tag_driver_register(struct near_tag_driver *driver)
964 if (driver->read == NULL)
967 driver_list = g_slist_insert_sorted(driver_list, driver, cmp_prio);
972 void near_tag_driver_unregister(struct near_tag_driver *driver)
976 driver_list = g_slist_remove(driver_list, driver);
979 int __near_tag_read(struct near_tag *tag, near_tag_io_cb cb)
983 DBG("type 0x%x", tag->type);
985 /* Stop check presence while reading */
986 __near_adapter_stop_check_presence(tag->adapter_idx, tag->target_idx);
988 for (list = driver_list; list; list = list->next) {
989 struct near_tag_driver *driver = list->data;
991 DBG("driver type 0x%x", driver->type);
993 if (driver->type == tag->type)
994 return driver->read(tag->adapter_idx, tag->target_idx,
1001 int __near_tag_write(struct near_tag *tag,
1002 struct near_ndef_message *ndef,
1008 DBG("type 0x%x", tag->type);
1010 for (list = driver_list; list; list = list->next) {
1011 struct near_tag_driver *driver = list->data;
1013 DBG("driver type 0x%x", driver->type);
1015 if (driver->type == tag->type) {
1016 /* Stop check presence while writing */
1017 __near_adapter_stop_check_presence(tag->adapter_idx,
1020 if (tag->blank == TRUE && driver->format != NULL) {
1021 DBG("Blank tag detected, formatting");
1022 err = driver->format(tag->adapter_idx,
1023 tag->target_idx, format_cb);
1025 err = driver->write(tag->adapter_idx,
1026 tag->target_idx, ndef,
1038 __near_adapter_start_check_presence(tag->adapter_idx,
1044 int __near_tag_check_presence(struct near_tag *tag, near_tag_io_cb cb)
1048 DBG("type 0x%x", tag->type);
1050 for (list = driver_list; list; list = list->next) {
1051 struct near_tag_driver *driver = list->data;
1053 DBG("driver type 0x%x", driver->type);
1055 if (driver->type == tag->type) {
1056 if (driver->check_presence == NULL)
1059 return driver->check_presence(tag->adapter_idx, tag->target_idx, cb);
1066 static void free_tag(gpointer data)
1068 struct near_tag *tag = data;
1072 near_ndef_records_free(tag->records);
1079 int __near_tag_init(void)
1083 connection = near_dbus_get_connection();
1085 tag_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
1091 void __near_tag_cleanup(void)
1095 g_hash_table_destroy(tag_hash);