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 void append_properties(DBusMessageIter *iter, struct near_tag *tag)
186 DBusMessageIter dict;
187 dbus_bool_t readonly;
188 const char *protocol, *type;
190 near_dbus_dict_open(iter, &dict);
192 type = type_string(tag);
194 near_dbus_dict_append_basic(&dict, "Type",
195 DBUS_TYPE_STRING, &type);
197 protocol = protocol_string(tag);
199 near_dbus_dict_append_basic(&dict, "Protocol",
200 DBUS_TYPE_STRING, &protocol);
202 readonly = tag->readonly;
203 near_dbus_dict_append_basic(&dict, "ReadOnly",
204 DBUS_TYPE_BOOLEAN, &readonly);
206 near_dbus_dict_append_array(&dict, "Records",
207 DBUS_TYPE_OBJECT_PATH, append_records, tag);
209 near_dbus_dict_close(iter, &dict);
212 static DBusMessage *get_properties(DBusConnection *conn,
213 DBusMessage *msg, void *data)
215 struct near_tag *tag = data;
217 DBusMessageIter array;
219 DBG("conn %p", conn);
221 reply = dbus_message_new_method_return(msg);
225 dbus_message_iter_init_append(reply, &array);
227 append_properties(&array, tag);
232 static DBusMessage *set_property(DBusConnection *conn,
233 DBusMessage *msg, void *data)
235 DBG("conn %p", conn);
237 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
240 void __near_tag_found_signal(struct near_adapter *adapter,
241 struct near_tag *tag)
245 DBusMessageIter iter;
247 path = __near_adapter_get_path(adapter);
251 signal = dbus_message_new_signal(path, NFC_ADAPTER_INTERFACE,
256 dbus_message_iter_init_append(signal, &iter);
257 dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH,
259 append_properties(&iter, tag);
261 dbus_connection_send(connection, signal, NULL);
262 dbus_message_unref(signal);
265 void __near_tag_lost_signal(struct near_adapter *adapter, struct near_tag *tag)
269 path = __near_adapter_get_path(adapter);
273 g_dbus_emit_signal(connection, path, NFC_ADAPTER_INTERFACE,
274 "TagLost", DBUS_TYPE_OBJECT_PATH, &tag->path,
278 static void tag_read_cb(uint32_t adapter_idx, uint32_t target_idx, int status)
280 struct near_tag *tag;
282 tag = near_tag_get_tag(adapter_idx, target_idx);
287 dbus_message_unref(tag->write_msg);
288 tag->write_msg = NULL;
290 __near_adapter_start_check_presence(adapter_idx, target_idx);
292 __near_adapter_tags_changed(adapter_idx);
295 static void write_cb(uint32_t adapter_idx, uint32_t target_idx, int status)
297 struct near_tag *tag;
298 DBusConnection *conn;
301 DBG("Write status %d", status);
303 tag = near_tag_get_tag(adapter_idx, target_idx);
307 conn = near_dbus_get_connection();
312 reply = __near_error_failed(tag->write_msg, EINVAL);
314 g_dbus_send_message(conn, reply);
316 g_dbus_send_reply(conn, tag->write_msg, DBUS_TYPE_INVALID);
319 near_ndef_records_free(tag->records);
327 * If writing succeeded,
328 * check presence will be restored after reading
330 __near_tag_read(tag, tag_read_cb);
335 dbus_message_unref(tag->write_msg);
336 tag->write_msg = NULL;
338 __near_adapter_start_check_presence(tag->adapter_idx, tag->target_idx);
341 static void format_cb(uint32_t adapter_idx, uint32_t target_idx, int status)
343 struct near_tag *tag;
346 DBG("format status %d", status);
348 tag = near_tag_get_tag(adapter_idx, target_idx);
356 err = __near_tag_write(tag, tag->write_ndef,
368 write_cb(tag->adapter_idx, tag->target_idx, err);
371 static DBusMessage *write_ndef(DBusConnection *conn,
372 DBusMessage *msg, void *data)
374 struct near_tag *tag = data;
375 struct near_ndef_message *ndef, *ndef_with_header = NULL;
376 int tlv_len_size, err;
378 DBG("conn %p", conn);
381 DBG("Read only tag");
382 return __near_error_permission_denied(msg);
386 return __near_error_in_progress(msg);
388 ndef = __ndef_build_from_message(msg);
390 return __near_error_failed(msg, EINVAL);
392 tag->write_msg = dbus_message_ref(msg);
394 /* Add NDEF header information depends upon tag type */
396 case NFC_PROTO_JEWEL:
397 case NFC_PROTO_MIFARE:
398 if (ndef->length < 0xff)
403 ndef_with_header = g_try_malloc0(sizeof(
404 struct near_ndef_message));
405 if (!ndef_with_header)
408 ndef_with_header->offset = 0;
409 ndef_with_header->length = ndef->length + tlv_len_size;
410 ndef_with_header->data =
411 g_try_malloc0(ndef->length + tlv_len_size);
412 if (!ndef_with_header->data)
415 ndef_with_header->data[0] = TLV_NDEF;
417 if (ndef->length < 0xff) {
418 ndef_with_header->data[1] = ndef->length;
420 ndef_with_header->data[1] = 0xff;
421 ndef_with_header->data[2] =
422 (uint8_t)(ndef->length >> 8);
423 ndef_with_header->data[3] = (uint8_t)(ndef->length);
426 memcpy(ndef_with_header->data + tlv_len_size - 1, ndef->data,
428 ndef_with_header->data[ndef->length + tlv_len_size - 1] =
432 case NFC_PROTO_FELICA:
433 ndef_with_header = g_try_malloc0(sizeof(
434 struct near_ndef_message));
435 if (!ndef_with_header)
438 ndef_with_header->offset = 0;
439 ndef_with_header->length = ndef->length;
440 ndef_with_header->data = g_try_malloc0(
441 ndef_with_header->length);
442 if (!ndef_with_header->data)
445 memcpy(ndef_with_header->data, ndef->data, ndef->length);
449 case NFC_PROTO_ISO14443:
450 ndef_with_header = g_try_malloc0(sizeof(
451 struct near_ndef_message));
452 if (!ndef_with_header)
455 ndef_with_header->offset = 0;
456 ndef_with_header->length = ndef->length + 2;
457 ndef_with_header->data = g_try_malloc0(ndef->length + 2);
458 if (!ndef_with_header->data)
461 ndef_with_header->data[0] = (uint8_t)(ndef->length >> 8);
462 ndef_with_header->data[1] = (uint8_t)(ndef->length);
463 memcpy(ndef_with_header->data + 2, ndef->data, ndef->length);
471 return __near_error_failed(msg, EOPNOTSUPP);
477 tag->write_ndef = ndef_with_header;
478 err = __near_tag_write(tag, ndef_with_header, write_cb);
480 g_free(ndef_with_header->data);
481 g_free(ndef_with_header);
483 return __near_error_failed(msg, -err);
486 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
489 dbus_message_unref(tag->write_msg);
490 tag->write_msg = NULL;
492 return __near_error_failed(msg, ENOMEM);
495 static const GDBusMethodTable tag_methods[] = {
496 { GDBUS_METHOD("GetProperties",
497 NULL, GDBUS_ARGS({"properties", "a{sv}"}),
499 { GDBUS_METHOD("SetProperty",
500 GDBUS_ARGS({"name", "s"}, {"value", "v"}),
501 NULL, set_property) },
502 { GDBUS_ASYNC_METHOD("Write", GDBUS_ARGS({"attributes", "a{sv}"}),
507 static const GDBusSignalTable tag_signals[] = {
508 { GDBUS_SIGNAL("PropertyChanged",
509 GDBUS_ARGS({"name", "s"}, {"value", "v"})) },
514 void __near_tag_append_records(struct near_tag *tag, DBusMessageIter *iter)
518 for (list = tag->records; list; list = list->next) {
519 struct near_ndef_record *record = list->data;
522 path = __near_ndef_record_get_path(record);
526 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
531 #define NFC_TAG_A (NFC_PROTO_ISO14443_MASK | NFC_PROTO_NFC_DEP_MASK | \
532 NFC_PROTO_JEWEL_MASK | NFC_PROTO_MIFARE_MASK)
533 #define NFC_TAG_A_TYPE2 0x00
534 #define NFC_TAG_A_TYPE4 0x01
535 #define NFC_TAG_A_NFC_DEP 0x02
536 #define NFC_TAG_A_TYPE4_DEP 0x03
538 #define NFC_TAG_A_SENS_RES_SSD_JEWEL 0x00
539 #define NFC_TAG_A_SENS_RES_PLATCONF_JEWEL 0x0c
541 #define NFC_TAG_A_SEL_PROT(sel_res) (((sel_res) & 0x60) >> 5)
542 #define NFC_TAG_A_SEL_CASCADE(sel_res) (((sel_res) & 0x04) >> 2)
543 #define NFC_TAG_A_SENS_RES_SSD(sens_res) ((sens_res) & 0x001f)
544 #define NFC_TAG_A_SENS_RES_PLATCONF(sens_res) (((sens_res) & 0x0f00) >> 8)
546 static enum near_tag_sub_type get_tag_type2_sub_type(uint8_t sel_res)
550 return NEAR_TAG_NFC_T2_MIFARE_ULTRALIGHT;
552 return NEAR_TAG_NFC_T2_MIFARE_CLASSIC_1K;
554 return NEAR_TAG_NFC_T2_MIFARE_MINI;
556 return NEAR_TAG_NFC_T2_MIFARE_CLASSIC_4K;
558 return NEAR_TAG_NFC_T2_MIFARE_DESFIRE;
560 return NEAR_TAG_NFC_T2_JCOP30;
562 return NEAR_TAG_NFC_T2_MIFARE_4K_EMUL;
564 return NEAR_TAG_NFC_T2_MIFARE_1K_INFINEON;
566 return NEAR_TAG_NFC_T2_MPCOS;
569 return NEAR_TAG_NFC_SUBTYPE_UNKNOWN;
572 static void set_tag_type(struct near_tag *tag,
573 uint16_t sens_res, uint8_t sel_res)
575 uint8_t platconf, ssd, proto;
577 DBG("protocol 0x%x sens_res 0x%x sel_res 0x%x", tag->protocol,
580 switch (tag->protocol) {
581 case NFC_PROTO_JEWEL_MASK:
582 platconf = NFC_TAG_A_SENS_RES_PLATCONF(sens_res);
583 ssd = NFC_TAG_A_SENS_RES_SSD(sens_res);
587 if ((ssd == NFC_TAG_A_SENS_RES_SSD_JEWEL) &&
588 (platconf == NFC_TAG_A_SENS_RES_PLATCONF_JEWEL))
589 tag->type = NFC_PROTO_JEWEL;
592 case NFC_PROTO_MIFARE_MASK:
593 case NFC_PROTO_ISO14443_MASK:
594 proto = NFC_TAG_A_SEL_PROT(sel_res);
596 DBG("proto 0x%x", proto);
599 case NFC_TAG_A_TYPE2:
600 tag->type = NFC_PROTO_MIFARE;
601 tag->sub_type = get_tag_type2_sub_type(sel_res);
603 case NFC_TAG_A_TYPE4:
604 tag->type = NFC_PROTO_ISO14443;
606 case NFC_TAG_A_TYPE4_DEP:
607 tag->type = NFC_PROTO_NFC_DEP;
612 case NFC_PROTO_FELICA_MASK:
613 tag->type = NFC_PROTO_FELICA;
616 case NFC_PROTO_ISO14443_B_MASK:
617 tag->type = NFC_PROTO_ISO14443_B;
621 tag->type = NFC_PROTO_MAX;
625 DBG("tag type 0x%x", tag->type);
628 static int tag_initialize(struct near_tag *tag,
629 uint32_t adapter_idx, uint32_t target_idx,
631 uint16_t sens_res, uint8_t sel_res,
632 uint8_t *nfcid, uint8_t nfcid_len)
636 tag->path = g_strdup_printf("%s/nfc%d/tag%d", NFC_PATH,
637 adapter_idx, target_idx);
640 tag->adapter_idx = adapter_idx;
641 tag->target_idx = target_idx;
642 tag->protocol = protocols;
644 tag->readonly = false;
646 if (nfcid_len <= NFC_MAX_NFCID1_LEN) {
647 tag->nfcid_len = nfcid_len;
648 memcpy(tag->nfcid, nfcid, nfcid_len);
651 set_tag_type(tag, sens_res, sel_res);
656 struct near_tag *__near_tag_add(uint32_t adapter_idx, uint32_t target_idx,
658 uint16_t sens_res, uint8_t sel_res,
659 uint8_t *nfcid, uint8_t nfcid_len)
661 struct near_tag *tag;
664 tag = near_tag_get_tag(adapter_idx, target_idx);
668 tag = g_try_malloc0(sizeof(struct near_tag));
672 if (tag_initialize(tag, adapter_idx, target_idx,
675 nfcid, nfcid_len) < 0) {
680 path = g_strdup(tag->path);
686 g_hash_table_insert(tag_hash, path, tag);
688 DBG("connection %p", connection);
690 g_dbus_register_interface(connection, tag->path,
692 tag_methods, tag_signals,
698 void __near_tag_remove(struct near_tag *tag)
700 char *path = tag->path;
702 DBG("path %s", tag->path);
704 if (!g_hash_table_lookup(tag_hash, tag->path))
707 g_dbus_unregister_interface(connection, tag->path,
710 g_hash_table_remove(tag_hash, path);
713 const char *__near_tag_get_path(struct near_tag *tag)
718 uint32_t __near_tag_get_type(struct near_tag *tag)
723 enum near_tag_sub_type near_tag_get_subtype(uint32_t adapter_idx,
727 struct near_tag *tag;
729 tag = near_tag_get_tag(adapter_idx, target_idx);
731 return NEAR_TAG_NFC_SUBTYPE_UNKNOWN;
733 return tag->sub_type;
736 uint8_t *near_tag_get_nfcid(uint32_t adapter_idx, uint32_t target_idx,
739 struct near_tag *tag;
742 tag = near_tag_get_tag(adapter_idx, target_idx);
746 nfcid = g_try_malloc0(tag->nfcid_len);
750 memcpy(nfcid, tag->nfcid, tag->nfcid_len);
751 *nfcid_len = tag->nfcid_len;
760 int near_tag_set_nfcid(uint32_t adapter_idx, uint32_t target_idx,
761 uint8_t *nfcid, size_t nfcid_len)
763 struct near_tag *tag;
765 DBG("NFCID len %zd", nfcid_len);
767 tag = near_tag_get_tag(adapter_idx, target_idx);
771 if (tag->nfcid_len > 0)
774 if (nfcid_len > NFC_MAX_NFCID1_LEN)
777 memcpy(tag->nfcid, nfcid, nfcid_len);
778 tag->nfcid_len = nfcid_len;
783 int near_tag_add_data(uint32_t adapter_idx, uint32_t target_idx,
784 uint8_t *data, size_t data_length)
786 struct near_tag *tag;
788 tag = near_tag_get_tag(adapter_idx, target_idx);
792 tag->data_length = data_length;
793 tag->data = g_try_malloc0(data_length);
798 memcpy(tag->data, data, data_length);
803 int near_tag_add_records(struct near_tag *tag, GList *records,
804 near_tag_io_cb cb, int status)
807 struct near_ndef_record *record;
810 DBG("records %p", records);
812 for (list = records; list; list = list->next) {
815 path = g_strdup_printf("%s/nfc%d/tag%d/record%d",
816 NFC_PATH, tag->adapter_idx,
817 tag->target_idx, tag->n_records);
822 __near_ndef_record_register(record, path);
825 tag->records = g_list_append(tag->records, record);
828 __near_agent_ndef_parse_records(tag->records);
830 near_dbus_property_changed_array(tag->path,
831 NFC_TAG_INTERFACE, "Records",
832 DBUS_TYPE_OBJECT_PATH, append_records,
836 cb(tag->adapter_idx, tag->target_idx, status);
838 g_list_free(records);
843 void near_tag_set_ro(struct near_tag *tag, bool readonly)
845 tag->readonly = readonly;
848 void near_tag_set_blank(struct near_tag *tag, bool blank)
853 bool near_tag_get_blank(struct near_tag *tag)
858 uint8_t *near_tag_get_data(struct near_tag *tag, size_t *data_length)
863 *data_length = tag->data_length;
868 size_t near_tag_get_data_length(struct near_tag *tag)
870 return tag->data_length;
873 uint32_t near_tag_get_adapter_idx(struct near_tag *tag)
875 return tag->adapter_idx;
878 uint32_t near_tag_get_target_idx(struct near_tag *tag)
880 return tag->target_idx;
883 enum near_tag_memory_layout near_tag_get_memory_layout(struct near_tag *tag)
886 return NEAR_TAG_MEMORY_UNKNOWN;
891 void near_tag_set_memory_layout(struct near_tag *tag,
892 enum near_tag_memory_layout layout)
897 tag->layout = layout;
900 void near_tag_set_max_ndef_size(struct near_tag *tag, uint16_t size)
905 tag->t4.max_ndef_size = size;
908 uint16_t near_tag_get_max_ndef_size(struct near_tag *tag)
913 return tag->t4.max_ndef_size;
916 void near_tag_set_c_apdu_max_size(struct near_tag *tag, uint16_t size)
921 tag->t4.c_apdu_max_size = size;
924 uint16_t near_tag_get_c_apdu_max_size(struct near_tag *tag)
929 return tag->t4.c_apdu_max_size;
932 void near_tag_set_idm(struct near_tag *tag, uint8_t *idm, uint8_t len)
934 if (!tag || len > TYPE3_IDM_LEN)
937 memset(tag->t3.IDm, 0, TYPE3_IDM_LEN);
938 memcpy(tag->t3.IDm, idm, len);
941 uint8_t *near_tag_get_idm(struct near_tag *tag, uint8_t *len)
946 *len = TYPE3_IDM_LEN;
950 void near_tag_set_attr_block(struct near_tag *tag, uint8_t *attr, uint8_t len)
952 if (!tag || len > TYPE3_ATTR_BLOCK_SIZE)
955 memset(tag->t3.attr, 0, TYPE3_ATTR_BLOCK_SIZE);
956 memcpy(tag->t3.attr, attr, len);
959 uint8_t *near_tag_get_attr_block(struct near_tag *tag, uint8_t *len)
964 *len = TYPE3_ATTR_BLOCK_SIZE;
968 void near_tag_set_ic_type(struct near_tag *tag, uint8_t ic_type)
973 tag->t3.ic_type = ic_type;
976 uint8_t near_tag_get_ic_type(struct near_tag *tag)
981 return tag->t3.ic_type;
984 static gint cmp_prio(gconstpointer a, gconstpointer b)
986 const struct near_tag_driver *driver1 = a;
987 const struct near_tag_driver *driver2 = b;
989 return driver2->priority - driver1->priority;
992 int near_tag_driver_register(struct near_tag_driver *driver)
999 driver_list = g_slist_insert_sorted(driver_list, driver, cmp_prio);
1004 void near_tag_driver_unregister(struct near_tag_driver *driver)
1008 driver_list = g_slist_remove(driver_list, driver);
1011 int __near_tag_read(struct near_tag *tag, near_tag_io_cb cb)
1015 DBG("type 0x%x", tag->type);
1017 /* Stop check presence while reading */
1018 __near_adapter_stop_check_presence(tag->adapter_idx, tag->target_idx);
1020 for (list = driver_list; list; list = list->next) {
1021 struct near_tag_driver *driver = list->data;
1023 DBG("driver type 0x%x", driver->type);
1025 if (driver->type == tag->type)
1026 return driver->read(tag->adapter_idx, tag->target_idx,
1033 int __near_tag_write(struct near_tag *tag,
1034 struct near_ndef_message *ndef,
1040 DBG("type 0x%x", tag->type);
1042 for (list = driver_list; list; list = list->next) {
1043 struct near_tag_driver *driver = list->data;
1045 DBG("driver type 0x%x", driver->type);
1047 if (driver->type == tag->type) {
1048 /* Stop check presence while writing */
1049 __near_adapter_stop_check_presence(tag->adapter_idx,
1052 if (tag->blank && driver->format) {
1053 DBG("Blank tag detected, formatting");
1054 err = driver->format(tag->adapter_idx,
1055 tag->target_idx, format_cb);
1057 err = driver->write(tag->adapter_idx,
1058 tag->target_idx, ndef,
1070 __near_adapter_start_check_presence(tag->adapter_idx,
1076 int __near_tag_check_presence(struct near_tag *tag, near_tag_io_cb cb)
1080 DBG("type 0x%x", tag->type);
1082 for (list = driver_list; list; list = list->next) {
1083 struct near_tag_driver *driver = list->data;
1085 DBG("driver type 0x%x", driver->type);
1087 if (driver->type == tag->type) {
1088 if (!driver->check_presence)
1091 return driver->check_presence(tag->adapter_idx, tag->target_idx, cb);
1098 static void free_tag(gpointer data)
1100 struct near_tag *tag = data;
1104 near_ndef_records_free(tag->records);
1111 int __near_tag_init(void)
1115 connection = near_dbus_get_connection();
1117 tag_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
1123 void __near_tag_cleanup(void)
1127 g_hash_table_destroy(tag_hash);