4a201e8cf368983bd8be2c7ac12d18a5d4c9822f
[profile/ivi/neard.git] / src / device.c
1 /*
2  *
3  *  neard - Near Field Communication manager
4  *
5  *  Copyright (C) 2011  Intel Corporation. All rights reserved.
6  *
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.
10  *
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.
15  *
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
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <errno.h>
29 #include <string.h>
30
31 #include <glib.h>
32
33 #include <gdbus.h>
34
35 #include "near.h"
36
37 struct near_device {
38         char *path;
39
40         uint32_t adapter_idx;
41         uint32_t target_idx;
42
43         uint8_t nfcid[NFC_MAX_NFCID1_LEN];
44         uint8_t nfcid_len;
45
46         size_t data_length;
47         uint8_t *data;
48
49         uint32_t n_records;
50         GList *records;
51
52         DBusMessage *push_msg; /* Push pending message */
53 };
54
55 static DBusConnection *connection = NULL;
56
57 static GHashTable *device_hash;
58
59 static GSList *driver_list = NULL;
60
61 static void free_device(gpointer data)
62 {
63         struct near_device *device = data;
64         GList *list;
65
66         DBG("device %p", device);
67
68         for (list = device->records; list; list = list->next) {
69                 struct near_ndef_record *record = list->data;
70
71                 __near_ndef_record_free(record);
72         }
73
74         g_list_free(device->records);
75         g_free(device->path);
76         g_free(device->data);
77         g_free(device);
78 }
79
80 struct near_device *near_device_get_device(uint32_t adapter_idx,
81                                                 uint32_t target_idx)
82 {
83         struct near_device *device;
84         char *path;
85
86         DBG("");
87
88         path = g_strdup_printf("%s/nfc%d/device%d", NFC_PATH,
89                                         adapter_idx, target_idx);
90         if (path == NULL)
91                 return NULL;
92
93         device = g_hash_table_lookup(device_hash, path);
94         g_free(path);
95
96         /* TODO refcount */
97         return device;
98 }
99
100 void __near_device_remove(struct near_device *device)
101 {
102         char *path = device->path;
103
104         DBG("path %s", device->path);
105
106         if (g_hash_table_lookup(device_hash, device->path) == NULL)
107                 return;
108
109         g_dbus_unregister_interface(connection, device->path,
110                                                 NFC_DEVICE_INTERFACE);
111
112         g_hash_table_remove(device_hash, path);
113 }
114
115 const char *__near_device_get_path(struct near_device *device)
116 {
117         return device->path;
118 }
119
120 static void append_records(DBusMessageIter *iter, void *user_data)
121 {
122         struct near_device *device = user_data;
123         GList *list;
124
125         DBG("");
126
127         for (list = device->records; list; list = list->next) {
128                 struct near_ndef_record *record = list->data;
129                 char *path;
130
131                 path = __near_ndef_record_get_path(record);
132                 if (path == NULL)
133                         continue;
134
135                 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
136                                                         &path);
137         }
138 }
139
140 static DBusMessage *get_properties(DBusConnection *conn,
141                                         DBusMessage *msg, void *data)
142 {
143         struct near_device *device = data;
144         DBusMessage *reply;
145         DBusMessageIter array, dict;
146
147         DBG("conn %p", conn);
148
149         reply = dbus_message_new_method_return(msg);
150         if (reply == NULL)
151                 return NULL;
152
153         dbus_message_iter_init_append(reply, &array);
154
155         near_dbus_dict_open(&array, &dict);
156
157         near_dbus_dict_append_array(&dict, "Records",
158                                 DBUS_TYPE_OBJECT_PATH, append_records, device);
159
160         near_dbus_dict_close(&array, &dict);
161
162         return reply;
163 }
164
165 static DBusMessage *set_property(DBusConnection *conn,
166                                         DBusMessage *msg, void *data)
167 {
168         DBG("conn %p", conn);
169
170         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
171 }
172
173 static void push_cb(uint32_t adapter_idx, uint32_t target_idx, int status)
174 {
175         struct near_device *device;
176         DBusConnection *conn;
177         DBusMessage *reply;
178
179         DBG("Push status %d", status);
180
181         conn = near_dbus_get_connection();
182         device = near_device_get_device(adapter_idx, target_idx);
183
184         if (conn == NULL || device == NULL)
185                 return;
186
187         if (status != 0) {
188                 reply = __near_error_failed(device->push_msg, EINVAL);
189                 if (reply != NULL)
190                         g_dbus_send_message(conn, reply);
191         } else {
192                 g_dbus_send_reply(conn, device->push_msg, DBUS_TYPE_INVALID);
193         }
194
195         dbus_message_unref(device->push_msg);
196         device->push_msg = NULL;
197 }
198
199 static char *sn_from_message(DBusMessage *msg)
200 {
201         DBusMessageIter iter;
202         DBusMessageIter arr_iter;
203
204         DBG("");
205
206         dbus_message_iter_init(msg, &iter);
207         dbus_message_iter_recurse(&iter, &arr_iter);
208
209         while (dbus_message_iter_get_arg_type(&arr_iter) !=
210                                                 DBUS_TYPE_INVALID) {
211                 const char *key, *value;
212                 DBusMessageIter ent_iter;
213                 DBusMessageIter var_iter;
214
215                 dbus_message_iter_recurse(&arr_iter, &ent_iter);
216                 dbus_message_iter_get_basic(&ent_iter, &key);
217
218                 if (g_strcmp0(key, "Type") != 0) {
219                         dbus_message_iter_next(&arr_iter);
220                         continue;
221                 }
222
223                 dbus_message_iter_next(&ent_iter);
224                 dbus_message_iter_recurse(&ent_iter, &var_iter);
225
226                 switch (dbus_message_iter_get_arg_type(&var_iter)) {
227                 case DBUS_TYPE_STRING:
228                         dbus_message_iter_get_basic(&var_iter, &value);
229
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;
238                         else
239                                 return NULL;
240
241                         break;
242                 }
243
244                 dbus_message_iter_next(&arr_iter);
245         }
246
247         return NULL;
248 }
249
250 static DBusMessage *push_ndef(DBusConnection *conn,
251                                 DBusMessage *msg, void *data)
252 {
253         struct near_device *device = data;
254         struct near_ndef_message *ndef;
255         char *service_name;
256         int err;
257
258         DBG("conn %p", conn);
259
260         if (device->push_msg)
261                 return __near_error_in_progress(msg);
262
263         device->push_msg = dbus_message_ref(msg);
264
265         service_name = sn_from_message(msg);
266         if (service_name == NULL) {
267                 err = -EINVAL;
268                 goto error;
269         }
270
271         ndef = __ndef_build_from_message(msg);
272         if (ndef == NULL) {
273                 err = -EINVAL;
274                 goto error;
275         }
276
277         err = __near_device_push(device, ndef, service_name, push_cb);
278         if (err < 0)
279                 goto error;
280
281         g_free(ndef);
282         g_free(ndef->data);
283
284         return NULL;
285
286 error:
287         dbus_message_unref(device->push_msg);
288         device->push_msg = NULL;
289
290         return __near_error_failed(msg, -err);
291 }
292
293 static const GDBusMethodTable device_methods[] = {
294         { GDBUS_METHOD("GetProperties",
295                                 NULL, GDBUS_ARGS({"properties", "a{sv}"}),
296                                 get_properties) },
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}"}),
301                                                         NULL, push_ndef) },
302         { },
303 };
304
305 static const GDBusSignalTable device_signals[] = {
306         { GDBUS_SIGNAL("PropertyChanged",
307                                 GDBUS_ARGS({"name", "s"}, {"value", "v"})) },
308         { }
309 };
310
311 int near_device_add_data(uint32_t adapter_idx, uint32_t target_idx,
312                         uint8_t *data, size_t data_length)
313 {
314         struct near_device *device;
315
316         device = near_device_get_device(adapter_idx, target_idx);
317         if (device == NULL)
318                 return -ENODEV;
319
320         device->data_length = data_length;
321         device->data = g_try_malloc0(data_length);
322         if (device->data == NULL)
323                 return -ENOMEM;
324
325         if (data != NULL)
326                 memcpy(device->data, data, data_length);
327
328         return 0;
329 }
330
331 int near_device_add_records(struct near_device *device, GList *records,
332                                 near_device_io_cb cb, int status)
333 {
334         GList *list;
335         struct near_ndef_record *record;
336         char *path;
337
338         DBG("records %p", records);
339
340         for (list = records; list; list = list->next) {
341                 record = list->data;
342
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);
346
347                 if (path == NULL)
348                         continue;
349
350                 __near_ndef_record_register(record, path);
351
352                 device->n_records++;
353                 device->records = g_list_append(device->records, record);
354         }
355
356         __near_agent_ndef_parse_records(device->records);
357
358         if (cb != NULL)
359                 cb(device->adapter_idx, device->target_idx, status);
360
361         g_list_free(records);
362
363         return 0;
364 }
365
366 struct near_device *__near_device_add(uint32_t adapter_idx, uint32_t target_idx,
367                                         uint8_t *nfcid, uint8_t nfcid_len)
368 {
369         struct near_device *device;
370         char *path;
371
372         device = near_device_get_device(adapter_idx, target_idx);
373         if (device != NULL)
374                 return NULL;
375
376         device = g_try_malloc0(sizeof(struct near_device));
377         if (device == NULL)
378                 return NULL;
379
380         device->path = g_strdup_printf("%s/nfc%d/device%d", NFC_PATH,
381                                         adapter_idx, target_idx);
382         if (device->path == NULL) {
383                 g_free(device);
384                 return NULL;
385         }
386         device->adapter_idx = adapter_idx;
387         device->target_idx = target_idx;
388         device->n_records = 0;
389
390         if (nfcid_len <= NFC_MAX_NFCID1_LEN && nfcid_len > 0) {
391                 device->nfcid_len = nfcid_len;
392                 memcpy(device->nfcid, nfcid, nfcid_len);
393         }
394
395         path = g_strdup(device->path);
396         if (path == NULL) {
397                 g_free(device);
398                 return NULL;
399         }
400
401         g_hash_table_insert(device_hash, path, device);
402
403         DBG("connection %p", connection);
404
405         g_dbus_register_interface(connection, device->path,
406                                         NFC_DEVICE_INTERFACE,
407                                         device_methods, device_signals,
408                                                         NULL, device, NULL);
409
410         return device;
411 }
412
413 int __near_device_listen(struct near_device *device, near_device_io_cb cb)
414 {
415         GSList *list;
416
417         DBG("");
418
419         for (list = driver_list; list; list = list->next) {
420                 struct near_device_driver *driver = list->data;
421
422                 return driver->listen(device->adapter_idx, cb);
423         }
424
425         return 0;
426 }
427
428 int __near_device_push(struct near_device *device,
429                         struct near_ndef_message *ndef, char *service_name,
430                         near_device_io_cb cb)
431 {
432         GSList *list;
433
434         DBG("");
435
436         if (__near_adapter_get_dep_state(device->adapter_idx) == FALSE) {
437                 near_error("DEP link is not established");
438                 return -ENOLINK;
439         }
440
441         for (list = driver_list; list; list = list->next) {
442                 struct near_device_driver *driver = list->data;
443
444                 return driver->push(device->adapter_idx, device->target_idx,
445                                         ndef, service_name, cb);
446         }
447
448         return 0;
449 }
450
451 static gint cmp_prio(gconstpointer a, gconstpointer b)
452 {
453         const struct near_tag_driver *driver1 = a;
454         const struct near_tag_driver *driver2 = b;
455
456         return driver2->priority - driver1->priority;
457 }
458
459 int near_device_driver_register(struct near_device_driver *driver)
460 {
461         DBG("");
462
463         if (driver->listen == NULL)
464                 return -EINVAL;
465
466         driver_list = g_slist_insert_sorted(driver_list, driver, cmp_prio);
467
468         __near_adapter_listen(driver);
469
470         return 0;
471 }
472
473 void near_device_driver_unregister(struct near_device_driver *driver)
474 {
475         DBG("");
476
477         driver_list = g_slist_remove(driver_list, driver);
478 }
479
480 int __near_device_init(void)
481 {
482         DBG("");
483
484         connection = near_dbus_get_connection();
485
486         device_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
487                                                 g_free, free_device);
488
489         return 0;
490 }
491
492 void __near_device_cleanup(void)
493 {
494         DBG("");
495
496         g_hash_table_destroy(device_hash);
497         device_hash = NULL;
498 }