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
43 uint8_t nfcid[NFC_MAX_NFCID1_LEN];
52 DBusMessage *push_msg; /* Push pending message */
55 static DBusConnection *connection = NULL;
57 static GHashTable *device_hash;
59 static GSList *driver_list = NULL;
61 static void free_device(gpointer data)
63 struct near_device *device = data;
66 DBG("device %p", device);
68 for (list = device->records; list; list = list->next) {
69 struct near_ndef_record *record = list->data;
71 __near_ndef_record_free(record);
74 g_list_free(device->records);
80 struct near_device *near_device_get_device(uint32_t adapter_idx,
83 struct near_device *device;
88 path = g_strdup_printf("%s/nfc%d/device%d", NFC_PATH,
89 adapter_idx, target_idx);
93 device = g_hash_table_lookup(device_hash, path);
100 void __near_device_remove(struct near_device *device)
102 char *path = device->path;
104 DBG("path %s", device->path);
106 if (g_hash_table_lookup(device_hash, device->path) == NULL)
109 g_dbus_unregister_interface(connection, device->path,
110 NFC_DEVICE_INTERFACE);
112 g_hash_table_remove(device_hash, path);
115 const char *__near_device_get_path(struct near_device *device)
120 static void append_records(DBusMessageIter *iter, void *user_data)
122 struct near_device *device = user_data;
127 for (list = device->records; list; list = list->next) {
128 struct near_ndef_record *record = list->data;
131 path = __near_ndef_record_get_path(record);
135 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
140 static DBusMessage *get_properties(DBusConnection *conn,
141 DBusMessage *msg, void *data)
143 struct near_device *device = data;
145 DBusMessageIter array, dict;
147 DBG("conn %p", conn);
149 reply = dbus_message_new_method_return(msg);
153 dbus_message_iter_init_append(reply, &array);
155 near_dbus_dict_open(&array, &dict);
157 near_dbus_dict_append_array(&dict, "Records",
158 DBUS_TYPE_OBJECT_PATH, append_records, device);
160 near_dbus_dict_close(&array, &dict);
165 static DBusMessage *set_property(DBusConnection *conn,
166 DBusMessage *msg, void *data)
168 DBG("conn %p", conn);
170 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
173 static void push_cb(uint32_t adapter_idx, uint32_t target_idx, int status)
175 struct near_device *device;
176 DBusConnection *conn;
179 DBG("Push status %d", status);
181 conn = near_dbus_get_connection();
182 device = near_device_get_device(adapter_idx, target_idx);
184 if (conn == NULL || device == NULL)
188 reply = __near_error_failed(device->push_msg, EINVAL);
190 g_dbus_send_message(conn, reply);
192 g_dbus_send_reply(conn, device->push_msg, DBUS_TYPE_INVALID);
195 dbus_message_unref(device->push_msg);
196 device->push_msg = NULL;
199 static char *sn_from_message(DBusMessage *msg)
201 DBusMessageIter iter;
202 DBusMessageIter arr_iter;
206 dbus_message_iter_init(msg, &iter);
207 dbus_message_iter_recurse(&iter, &arr_iter);
209 while (dbus_message_iter_get_arg_type(&arr_iter) !=
211 const char *key, *value;
212 DBusMessageIter ent_iter;
213 DBusMessageIter var_iter;
215 dbus_message_iter_recurse(&arr_iter, &ent_iter);
216 dbus_message_iter_get_basic(&ent_iter, &key);
218 if (g_strcmp0(key, "Type") != 0) {
219 dbus_message_iter_next(&arr_iter);
223 dbus_message_iter_next(&ent_iter);
224 dbus_message_iter_recurse(&ent_iter, &var_iter);
226 switch (dbus_message_iter_get_arg_type(&var_iter)) {
227 case DBUS_TYPE_STRING:
228 dbus_message_iter_get_basic(&var_iter, &value);
230 if (g_strcmp0(value, "Text") == 0)
231 return NEAR_DEVICE_SN_SNEP;
232 else if (g_strcmp0(value, "URI") == 0)
233 return NEAR_DEVICE_SN_SNEP;
234 else if (g_strcmp0(value, "SmartPoster") == 0)
235 return NEAR_DEVICE_SN_SNEP;
236 else if (g_strcmp0(value, "Handover") == 0)
237 return NEAR_DEVICE_SN_HANDOVER;
244 dbus_message_iter_next(&arr_iter);
250 static DBusMessage *push_ndef(DBusConnection *conn,
251 DBusMessage *msg, void *data)
253 struct near_device *device = data;
254 struct near_ndef_message *ndef;
258 DBG("conn %p", conn);
260 if (device->push_msg)
261 return __near_error_in_progress(msg);
263 device->push_msg = dbus_message_ref(msg);
265 service_name = sn_from_message(msg);
266 if (service_name == NULL) {
271 ndef = __ndef_build_from_message(msg);
277 err = __near_device_push(device, ndef, service_name, push_cb);
287 dbus_message_unref(device->push_msg);
288 device->push_msg = NULL;
290 return __near_error_failed(msg, -err);
293 static const GDBusMethodTable device_methods[] = {
294 { GDBUS_METHOD("GetProperties",
295 NULL, GDBUS_ARGS({"properties", "a{sv}"}),
297 { GDBUS_METHOD("SetProperty",
298 GDBUS_ARGS({"name", "s"}, {"value", "v"}),
299 NULL, set_property) },
300 { GDBUS_ASYNC_METHOD("Push", GDBUS_ARGS({"attributes", "a{sv}"}),
305 static const GDBusSignalTable device_signals[] = {
306 { GDBUS_SIGNAL("PropertyChanged",
307 GDBUS_ARGS({"name", "s"}, {"value", "v"})) },
311 int near_device_add_data(uint32_t adapter_idx, uint32_t target_idx,
312 uint8_t *data, size_t data_length)
314 struct near_device *device;
316 device = near_device_get_device(adapter_idx, target_idx);
320 device->data_length = data_length;
321 device->data = g_try_malloc0(data_length);
322 if (device->data == NULL)
326 memcpy(device->data, data, data_length);
331 int near_device_add_records(struct near_device *device, GList *records,
332 near_device_io_cb cb, int status)
335 struct near_ndef_record *record;
338 DBG("records %p", records);
340 for (list = records; list; list = list->next) {
343 path = g_strdup_printf("%s/nfc%d/device%d/record%d",
344 NFC_PATH, device->adapter_idx,
345 device->target_idx, device->n_records);
350 __near_ndef_record_register(record, path);
353 device->records = g_list_append(device->records, record);
356 __near_agent_ndef_parse_records(device->records);
359 cb(device->adapter_idx, device->target_idx, status);
361 g_list_free(records);
366 struct near_device *__near_device_add(uint32_t adapter_idx, uint32_t target_idx,
367 uint8_t *nfcid, uint8_t nfcid_len)
369 struct near_device *device;
372 device = near_device_get_device(adapter_idx, target_idx);
376 device = g_try_malloc0(sizeof(struct near_device));
380 device->path = g_strdup_printf("%s/nfc%d/device%d", NFC_PATH,
381 adapter_idx, target_idx);
382 if (device->path == NULL) {
386 device->adapter_idx = adapter_idx;
387 device->target_idx = target_idx;
388 device->n_records = 0;
390 if (nfcid_len <= NFC_MAX_NFCID1_LEN && nfcid_len > 0) {
391 device->nfcid_len = nfcid_len;
392 memcpy(device->nfcid, nfcid, nfcid_len);
395 path = g_strdup(device->path);
401 g_hash_table_insert(device_hash, path, device);
403 DBG("connection %p", connection);
405 g_dbus_register_interface(connection, device->path,
406 NFC_DEVICE_INTERFACE,
407 device_methods, device_signals,
413 int __near_device_listen(struct near_device *device, near_device_io_cb cb)
419 for (list = driver_list; list; list = list->next) {
420 struct near_device_driver *driver = list->data;
422 return driver->listen(device->adapter_idx, cb);
428 int __near_device_push(struct near_device *device,
429 struct near_ndef_message *ndef, char *service_name,
430 near_device_io_cb cb)
436 if (__near_adapter_get_dep_state(device->adapter_idx) == FALSE) {
437 near_error("DEP link is not established");
441 for (list = driver_list; list; list = list->next) {
442 struct near_device_driver *driver = list->data;
444 return driver->push(device->adapter_idx, device->target_idx,
445 ndef, service_name, cb);
451 static gint cmp_prio(gconstpointer a, gconstpointer b)
453 const struct near_tag_driver *driver1 = a;
454 const struct near_tag_driver *driver2 = b;
456 return driver2->priority - driver1->priority;
459 int near_device_driver_register(struct near_device_driver *driver)
463 if (driver->listen == NULL)
466 driver_list = g_slist_insert_sorted(driver_list, driver, cmp_prio);
468 __near_adapter_listen(driver);
473 void near_device_driver_unregister(struct near_device_driver *driver)
477 driver_list = g_slist_remove(driver_list, driver);
480 int __near_device_init(void)
484 connection = near_dbus_get_connection();
486 device_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
487 g_free, free_device);
492 void __near_device_cleanup(void)
496 g_hash_table_destroy(device_hash);