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 __near_adapter_tags_changed(adapter_idx);
236 static void write_cb(uint32_t adapter_idx, uint32_t target_idx, int status)
238 struct near_tag *tag;
239 DBusConnection *conn;
242 DBG("Write status %d", status);
244 conn = near_dbus_get_connection();
245 tag = near_tag_get_tag(adapter_idx, target_idx);
247 if (conn == NULL || tag == NULL)
251 reply = __near_error_failed(tag->write_msg, EINVAL);
253 g_dbus_send_message(conn, reply);
255 g_dbus_send_reply(conn, tag->write_msg, DBUS_TYPE_INVALID);
258 dbus_message_unref(tag->write_msg);
259 tag->write_msg = NULL;
261 near_ndef_records_free(tag->records);
267 __near_tag_read(tag, tag_read_cb);
270 static void format_cb(uint32_t adapter_idx, uint32_t target_idx, int status)
272 struct near_tag *tag;
275 DBG("format status %d", status);
277 tag = near_tag_get_tag(adapter_idx, target_idx);
281 if (tag->write_msg == NULL)
285 err = __near_tag_write(tag, tag->write_ndef,
297 write_cb(tag->adapter_idx, tag->target_idx, err);
300 static DBusMessage *write_ndef(DBusConnection *conn,
301 DBusMessage *msg, void *data)
303 struct near_tag *tag = data;
304 struct near_ndef_message *ndef, *ndef_with_header = NULL;
307 DBG("conn %p", conn);
309 if (tag->readonly == TRUE) {
310 DBG("Read only tag");
311 return __near_error_permission_denied(msg);
315 return __near_error_in_progress(msg);
317 ndef = __ndef_build_from_message(msg);
319 return __near_error_failed(msg, EINVAL);
321 tag->write_msg = dbus_message_ref(msg);
323 /* Add NDEF header information depends upon tag type */
325 case NFC_PROTO_JEWEL:
326 case NFC_PROTO_MIFARE:
327 ndef_with_header = g_try_malloc0(sizeof(
328 struct near_ndef_message));
329 if (ndef_with_header == NULL)
332 ndef_with_header->offset = 0;
333 ndef_with_header->length = ndef->length + 3;
334 ndef_with_header->data = g_try_malloc0(ndef->length + 3);
335 if (ndef_with_header->data == NULL)
338 ndef_with_header->data[0] = TLV_NDEF;
339 ndef_with_header->data[1] = ndef->length;
340 memcpy(ndef_with_header->data + 2, ndef->data, ndef->length);
341 ndef_with_header->data[ndef->length + 2] = TLV_END;
345 case NFC_PROTO_FELICA:
346 ndef_with_header = g_try_malloc0(sizeof(
347 struct near_ndef_message));
348 if (ndef_with_header == NULL)
351 ndef_with_header->offset = 0;
352 ndef_with_header->length = ndef->length;
353 ndef_with_header->data = g_try_malloc0(
354 ndef_with_header->length);
355 if (ndef_with_header->data == NULL)
358 memcpy(ndef_with_header->data, ndef->data, ndef->length);
362 case NFC_PROTO_ISO14443:
363 ndef_with_header = g_try_malloc0(sizeof(
364 struct near_ndef_message));
365 if (ndef_with_header == NULL)
368 ndef_with_header->offset = 0;
369 ndef_with_header->length = ndef->length + 2;
370 ndef_with_header->data = g_try_malloc0(ndef->length + 2);
371 if (ndef_with_header->data == NULL)
374 ndef_with_header->data[0] = (uint8_t)(ndef->length >> 8);
375 ndef_with_header->data[1] = (uint8_t)(ndef->length);
376 memcpy(ndef_with_header->data + 2, ndef->data, ndef->length);
384 return __near_error_failed(msg, EOPNOTSUPP);
390 tag->write_ndef = ndef_with_header;
391 err = __near_tag_write(tag, ndef_with_header, write_cb);
393 g_free(ndef_with_header->data);
394 g_free(ndef_with_header);
396 return __near_error_failed(msg, -err);
399 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
402 dbus_message_unref(tag->write_msg);
403 tag->write_msg = NULL;
405 return __near_error_failed(msg, ENOMEM);
408 static const GDBusMethodTable tag_methods[] = {
409 { GDBUS_METHOD("GetProperties",
410 NULL, GDBUS_ARGS({"properties", "a{sv}"}),
412 { GDBUS_METHOD("SetProperty",
413 GDBUS_ARGS({"name", "s"}, {"value", "v"}),
414 NULL, set_property) },
415 { GDBUS_ASYNC_METHOD("Write", GDBUS_ARGS({"attributes", "a{sv}"}),
420 static const GDBusSignalTable tag_signals[] = {
421 { GDBUS_SIGNAL("PropertyChanged",
422 GDBUS_ARGS({"name", "s"}, {"value", "v"})) },
427 void __near_tag_append_records(struct near_tag *tag, DBusMessageIter *iter)
431 for (list = tag->records; list; list = list->next) {
432 struct near_ndef_record *record = list->data;
435 path = __near_ndef_record_get_path(record);
439 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
444 #define NFC_TAG_A (NFC_PROTO_ISO14443_MASK | NFC_PROTO_NFC_DEP_MASK | \
445 NFC_PROTO_JEWEL_MASK | NFC_PROTO_MIFARE_MASK)
446 #define NFC_TAG_A_TYPE2 0x00
447 #define NFC_TAG_A_TYPE4 0x01
448 #define NFC_TAG_A_NFC_DEP 0x02
449 #define NFC_TAG_A_TYPE4_DEP 0x03
451 #define NFC_TAG_A_SENS_RES_SSD_JEWEL 0x00
452 #define NFC_TAG_A_SENS_RES_PLATCONF_JEWEL 0x0c
454 #define NFC_TAG_A_SEL_PROT(sel_res) (((sel_res) & 0x60) >> 5)
455 #define NFC_TAG_A_SEL_CASCADE(sel_res) (((sel_res) & 0x04) >> 2)
456 #define NFC_TAG_A_SENS_RES_SSD(sens_res) ((sens_res) & 0x001f)
457 #define NFC_TAG_A_SENS_RES_PLATCONF(sens_res) (((sens_res) & 0x0f00) >> 8)
459 static enum near_tag_sub_type get_tag_type2_sub_type(uint8_t sel_res)
463 return NEAR_TAG_NFC_T2_MIFARE_ULTRALIGHT;
465 return NEAR_TAG_NFC_T2_MIFARE_CLASSIC_1K;
467 return NEAR_TAG_NFC_T2_MIFARE_MINI;
469 return NEAR_TAG_NFC_T2_MIFARE_CLASSIC_4K;
471 return NEAR_TAG_NFC_T2_MIFARE_DESFIRE;
473 return NEAR_TAG_NFC_T2_JCOP30;
475 return NEAR_TAG_NFC_T2_MIFARE_4K_EMUL;
477 return NEAR_TAG_NFC_T2_MIFARE_1K_INFINEON;
479 return NEAR_TAG_NFC_T2_MPCOS;
482 return NEAR_TAG_NFC_SUBTYPE_UNKNOWN;
485 static void set_tag_type(struct near_tag *tag,
486 uint16_t sens_res, uint8_t sel_res)
488 uint8_t platconf, ssd, proto;
490 DBG("protocol 0x%x sens_res 0x%x sel_res 0x%x", tag->protocol,
493 switch (tag->protocol) {
494 case NFC_PROTO_JEWEL_MASK:
495 platconf = NFC_TAG_A_SENS_RES_PLATCONF(sens_res);
496 ssd = NFC_TAG_A_SENS_RES_SSD(sens_res);
500 if ((ssd == NFC_TAG_A_SENS_RES_SSD_JEWEL) &&
501 (platconf == NFC_TAG_A_SENS_RES_PLATCONF_JEWEL))
502 tag->type = NFC_PROTO_JEWEL;
505 case NFC_PROTO_MIFARE_MASK:
506 case NFC_PROTO_ISO14443_MASK:
507 proto = NFC_TAG_A_SEL_PROT(sel_res);
509 DBG("proto 0x%x", proto);
512 case NFC_TAG_A_TYPE2:
513 tag->type = NFC_PROTO_MIFARE;
514 tag->sub_type = get_tag_type2_sub_type(sel_res);
516 case NFC_TAG_A_TYPE4:
517 tag->type = NFC_PROTO_ISO14443;
519 case NFC_TAG_A_TYPE4_DEP:
520 tag->type = NFC_PROTO_NFC_DEP;
525 case NFC_PROTO_FELICA_MASK:
526 tag->type = NFC_PROTO_FELICA;
530 tag->type = NFC_PROTO_MAX;
534 DBG("tag type 0x%x", tag->type);
537 static int tag_initialize(struct near_tag *tag,
538 uint32_t adapter_idx, uint32_t target_idx,
540 uint16_t sens_res, uint8_t sel_res,
541 uint8_t *nfcid, uint8_t nfcid_len)
545 tag->path = g_strdup_printf("%s/nfc%d/tag%d", NFC_PATH,
546 adapter_idx, target_idx);
547 if (tag->path == NULL)
549 tag->adapter_idx = adapter_idx;
550 tag->target_idx = target_idx;
551 tag->protocol = protocols;
553 tag->readonly = FALSE;
555 if (nfcid_len <= NFC_MAX_NFCID1_LEN) {
556 tag->nfcid_len = nfcid_len;
557 memcpy(tag->nfcid, nfcid, nfcid_len);
560 set_tag_type(tag, sens_res, sel_res);
565 struct near_tag *__near_tag_add(uint32_t adapter_idx, uint32_t target_idx,
567 uint16_t sens_res, uint8_t sel_res,
568 uint8_t *nfcid, uint8_t nfcid_len)
570 struct near_tag *tag;
573 tag = near_tag_get_tag(adapter_idx, target_idx);
577 tag = g_try_malloc0(sizeof(struct near_tag));
581 if (tag_initialize(tag, adapter_idx, target_idx,
584 nfcid, nfcid_len) < 0) {
589 path = g_strdup(tag->path);
595 g_hash_table_insert(tag_hash, path, tag);
597 DBG("connection %p", connection);
599 g_dbus_register_interface(connection, tag->path,
601 tag_methods, tag_signals,
607 void __near_tag_remove(struct near_tag *tag)
609 char *path = tag->path;
611 DBG("path %s", tag->path);
613 if (g_hash_table_lookup(tag_hash, tag->path) == NULL)
616 g_dbus_unregister_interface(connection, tag->path,
619 g_hash_table_remove(tag_hash, path);
622 const char *__near_tag_get_path(struct near_tag *tag)
627 uint32_t __near_tag_get_type(struct near_tag *tag)
632 enum near_tag_sub_type near_tag_get_subtype(uint32_t adapter_idx,
636 struct near_tag *tag;
638 tag = near_tag_get_tag(adapter_idx, target_idx);
640 return NEAR_TAG_NFC_SUBTYPE_UNKNOWN;
642 return tag->sub_type;
645 uint8_t *near_tag_get_nfcid(uint32_t adapter_idx, uint32_t target_idx,
648 struct near_tag *tag;
651 tag = near_tag_get_tag(adapter_idx, target_idx);
655 nfcid = g_try_malloc0(tag->nfcid_len);
659 memcpy(nfcid, tag->nfcid, tag->nfcid_len);
660 *nfcid_len = tag->nfcid_len;
669 int near_tag_set_nfcid(uint32_t adapter_idx, uint32_t target_idx,
670 uint8_t *nfcid, size_t nfcid_len)
672 struct near_tag *tag;
674 DBG("NFCID len %zd", nfcid_len);
676 tag = near_tag_get_tag(adapter_idx, target_idx);
680 if (tag->nfcid_len > 0)
683 if (nfcid_len > NFC_MAX_NFCID1_LEN)
686 memcpy(tag->nfcid, nfcid, nfcid_len);
687 tag->nfcid_len = nfcid_len;
692 int near_tag_add_data(uint32_t adapter_idx, uint32_t target_idx,
693 uint8_t *data, size_t data_length)
695 struct near_tag *tag;
697 tag = near_tag_get_tag(adapter_idx, target_idx);
701 tag->data_length = data_length;
702 tag->data = g_try_malloc0(data_length);
703 if (tag->data == NULL)
707 memcpy(tag->data, data, data_length);
712 int near_tag_add_records(struct near_tag *tag, GList *records,
713 near_tag_io_cb cb, int status)
716 struct near_ndef_record *record;
719 DBG("records %p", records);
721 for (list = records; list; list = list->next) {
724 path = g_strdup_printf("%s/nfc%d/tag%d/record%d",
725 NFC_PATH, tag->adapter_idx,
726 tag->target_idx, tag->n_records);
731 __near_ndef_record_register(record, path);
734 tag->records = g_list_append(tag->records, record);
737 __near_agent_ndef_parse_records(tag->records);
740 cb(tag->adapter_idx, tag->target_idx, status);
742 g_list_free(records);
747 void near_tag_set_ro(struct near_tag *tag, near_bool_t readonly)
749 tag->readonly = readonly;
752 void near_tag_set_blank(struct near_tag *tag, near_bool_t blank)
757 uint8_t *near_tag_get_data(struct near_tag *tag, size_t *data_length)
759 if (data_length == NULL)
762 *data_length = tag->data_length;
767 uint32_t near_tag_get_adapter_idx(struct near_tag *tag)
769 return tag->adapter_idx;
772 uint32_t near_tag_get_target_idx(struct near_tag *tag)
774 return tag->target_idx;
777 enum near_tag_memory_layout near_tag_get_memory_layout(struct near_tag *tag)
780 return NEAR_TAG_MEMORY_UNKNOWN;
785 void near_tag_set_memory_layout(struct near_tag *tag,
786 enum near_tag_memory_layout layout)
791 tag->layout = layout;
794 void near_tag_set_max_ndef_size(struct near_tag *tag, uint16_t size)
799 tag->t4.max_ndef_size = size;
802 uint16_t near_tag_get_max_ndef_size(struct near_tag *tag)
807 return tag->t4.max_ndef_size;
810 void near_tag_set_c_apdu_max_size(struct near_tag *tag, uint16_t size)
815 tag->t4.c_apdu_max_size = size;
818 uint16_t near_tag_get_c_apdu_max_size(struct near_tag *tag)
823 return tag->t4.c_apdu_max_size;
826 void near_tag_set_idm(struct near_tag *tag, uint8_t *idm, uint8_t len)
828 if (tag == NULL || len > TYPE3_IDM_LEN)
831 memset(tag->t3.IDm, 0, TYPE3_IDM_LEN);
832 memcpy(tag->t3.IDm, idm, len);
835 uint8_t *near_tag_get_idm(struct near_tag *tag, uint8_t *len)
837 if (tag == NULL || len == NULL)
840 *len = TYPE3_IDM_LEN;
844 void near_tag_set_attr_block(struct near_tag *tag, uint8_t *attr, uint8_t len)
846 if (tag == NULL || len > TYPE3_ATTR_BLOCK_SIZE)
849 memset(tag->t3.attr, 0, TYPE3_ATTR_BLOCK_SIZE);
850 memcpy(tag->t3.attr, attr, len);
853 uint8_t *near_tag_get_attr_block(struct near_tag *tag, uint8_t *len)
855 if (tag == NULL || len == NULL)
858 *len = TYPE3_ATTR_BLOCK_SIZE;
862 void near_tag_set_ic_type(struct near_tag *tag, uint8_t ic_type)
867 tag->t3.ic_type = ic_type;
870 uint8_t near_tag_get_ic_type(struct near_tag *tag)
875 return tag->t3.ic_type;
878 static gint cmp_prio(gconstpointer a, gconstpointer b)
880 const struct near_tag_driver *driver1 = a;
881 const struct near_tag_driver *driver2 = b;
883 return driver2->priority - driver1->priority;
886 int near_tag_driver_register(struct near_tag_driver *driver)
890 if (driver->read == NULL)
893 driver_list = g_slist_insert_sorted(driver_list, driver, cmp_prio);
898 void near_tag_driver_unregister(struct near_tag_driver *driver)
902 driver_list = g_slist_remove(driver_list, driver);
905 int __near_tag_read(struct near_tag *tag, near_tag_io_cb cb)
909 DBG("type 0x%x", tag->type);
911 for (list = driver_list; list; list = list->next) {
912 struct near_tag_driver *driver = list->data;
914 DBG("driver type 0x%x", driver->type);
916 if (driver->type == tag->type)
917 return driver->read(tag->adapter_idx, tag->target_idx,
924 int __near_tag_write(struct near_tag *tag,
925 struct near_ndef_message *ndef,
931 DBG("type 0x%x", tag->type);
933 for (list = driver_list; list; list = list->next) {
934 struct near_tag_driver *driver = list->data;
936 DBG("driver type 0x%x", driver->type);
938 if (driver->type == tag->type) {
939 if (tag->blank == TRUE && driver->format != NULL) {
940 DBG("Blank tag detected, formatting");
941 err = driver->format(tag->adapter_idx,
942 tag->target_idx, format_cb);
946 return driver->write(tag->adapter_idx,
947 tag->target_idx, ndef,
956 int __near_tag_check_presence(struct near_tag *tag, near_tag_io_cb cb)
960 DBG("type 0x%x", tag->type);
962 for (list = driver_list; list; list = list->next) {
963 struct near_tag_driver *driver = list->data;
965 DBG("driver type 0x%x", driver->type);
967 if (driver->type == tag->type) {
968 if (driver->check_presence == NULL)
971 return driver->check_presence(tag->adapter_idx, tag->target_idx, cb);
978 static void free_tag(gpointer data)
980 struct near_tag *tag = data;
984 near_ndef_records_free(tag->records);
991 int __near_tag_init(void)
995 connection = near_dbus_get_connection();
997 tag_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
1003 void __near_tag_cleanup(void)
1007 g_hash_table_destroy(tag_hash);