device: Create the Device D-Bus interface only when the DEP link is up
[platform/upstream/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
65         DBG("device %p", device);
66
67         near_ndef_records_free(device->records);
68
69         g_dbus_unregister_interface(connection, device->path,
70                                                 NFC_DEVICE_INTERFACE);
71
72         g_free(device->path);
73         g_free(device->data);
74         g_free(device);
75 }
76
77 struct near_device *near_device_get_device(uint32_t adapter_idx,
78                                                 uint32_t target_idx)
79 {
80         struct near_device *device;
81         char *path;
82
83         DBG("");
84
85         path = g_strdup_printf("%s/nfc%d/device%d", NFC_PATH,
86                                         adapter_idx, target_idx);
87         if (!path)
88                 return NULL;
89
90         device = g_hash_table_lookup(device_hash, path);
91         g_free(path);
92
93         /* TODO refcount */
94         return device;
95 }
96
97 const char *__near_device_get_path(struct near_device *device)
98 {
99         return device->path;
100 }
101
102 uint32_t __neard_device_get_idx(struct near_device *device)
103 {
104         return device->target_idx;
105 }
106
107 static void push_cb(uint32_t adapter_idx, uint32_t target_idx, int status)
108 {
109         struct near_device *device;
110         DBusConnection *conn;
111         DBusMessage *reply;
112
113         DBG("Push status %d", status);
114
115         conn = near_dbus_get_connection();
116         device = near_device_get_device(adapter_idx, target_idx);
117
118         if (!conn || !device)
119                 return;
120
121         if (status != 0) {
122                 reply = __near_error_failed(device->push_msg, -status);
123                 if (reply)
124                         g_dbus_send_message(conn, reply);
125         } else {
126                 g_dbus_send_reply(conn, device->push_msg, DBUS_TYPE_INVALID);
127         }
128
129         dbus_message_unref(device->push_msg);
130         device->push_msg = NULL;
131 }
132
133 static char *sn_from_message(DBusMessage *msg)
134 {
135         DBusMessageIter iter;
136         DBusMessageIter arr_iter;
137
138         DBG("");
139
140         dbus_message_iter_init(msg, &iter);
141         dbus_message_iter_recurse(&iter, &arr_iter);
142
143         while (dbus_message_iter_get_arg_type(&arr_iter) !=
144                                                 DBUS_TYPE_INVALID) {
145                 const char *key, *value;
146                 DBusMessageIter ent_iter;
147                 DBusMessageIter var_iter;
148
149                 dbus_message_iter_recurse(&arr_iter, &ent_iter);
150                 dbus_message_iter_get_basic(&ent_iter, &key);
151
152                 if (g_strcmp0(key, "Type") != 0) {
153                         dbus_message_iter_next(&arr_iter);
154                         continue;
155                 }
156
157                 dbus_message_iter_next(&ent_iter);
158                 dbus_message_iter_recurse(&ent_iter, &var_iter);
159
160                 switch (dbus_message_iter_get_arg_type(&var_iter)) {
161                 case DBUS_TYPE_STRING:
162                         dbus_message_iter_get_basic(&var_iter, &value);
163
164                         if (g_strcmp0(value, "Text") == 0)
165                                 return NEAR_DEVICE_SN_SNEP;
166                         else if (g_strcmp0(value, "URI") == 0)
167                                 return NEAR_DEVICE_SN_SNEP;
168                         else if (g_strcmp0(value, "SmartPoster") == 0)
169                                 return NEAR_DEVICE_SN_SNEP;
170                         else if (g_strcmp0(value, "Handover") == 0)
171                                 return NEAR_DEVICE_SN_HANDOVER;
172                         else if (g_strcmp0(value, "StaticHandover") == 0)
173                                 return NEAR_DEVICE_SN_HANDOVER;
174                         else if (g_strcmp0(value, "MIME") == 0)
175                                 return NEAR_DEVICE_SN_SNEP;
176                         else
177                                 return NULL;
178
179                         break;
180                 }
181
182                 dbus_message_iter_next(&arr_iter);
183         }
184
185         return NULL;
186 }
187
188 static DBusMessage *push_ndef(DBusConnection *conn,
189                                 DBusMessage *msg, void *data)
190 {
191         struct near_device *device = data;
192         struct near_ndef_message *ndef;
193         char *service_name;
194         int err;
195
196         DBG("conn %p", conn);
197
198         if (device->push_msg)
199                 return __near_error_in_progress(msg);
200
201         device->push_msg = dbus_message_ref(msg);
202
203         service_name = sn_from_message(msg);
204         if (!service_name) {
205                 err = -EINVAL;
206                 goto error;
207         }
208
209         ndef = __ndef_build_from_message(msg);
210         if (!ndef) {
211                 err = -EINVAL;
212                 goto error;
213         }
214
215         err = __near_device_push(device, ndef, service_name, push_cb);
216         if (err < 0)
217                 goto error;
218
219         return NULL;
220
221 error:
222         dbus_message_unref(device->push_msg);
223         device->push_msg = NULL;
224
225         return __near_error_failed(msg, -err);
226 }
227
228 static const GDBusMethodTable device_methods[] = {
229         { GDBUS_ASYNC_METHOD("Push", GDBUS_ARGS({"attributes", "a{sv}"}),
230                                                         NULL, push_ndef) },
231         { },
232 };
233
234 void __near_device_remove(struct near_device *device)
235 {
236         char *path = device->path;
237
238         DBG("path %s", device->path);
239
240         if (device->push_msg)
241                 push_cb(device->adapter_idx, device->target_idx, EIO);
242
243         g_hash_table_remove(device_hash, path);
244 }
245
246 int near_device_add_data(uint32_t adapter_idx, uint32_t target_idx,
247                         uint8_t *data, size_t data_length)
248 {
249         struct near_device *device;
250
251         device = near_device_get_device(adapter_idx, target_idx);
252         if (!device)
253                 return -ENODEV;
254
255         device->data_length = data_length;
256         device->data = g_try_malloc0(data_length);
257         if (!device->data)
258                 return -ENOMEM;
259
260         if (data)
261                 memcpy(device->data, data, data_length);
262
263         return 0;
264 }
265
266 int near_device_add_records(struct near_device *device, GList *records,
267                                 near_device_io_cb cb, int status)
268 {
269         GList *list;
270         struct near_ndef_record *record;
271         char *path;
272
273         DBG("records %p", records);
274
275         near_ndef_records_free(device->records);
276         device->records = NULL;
277
278         for (list = records; list; list = list->next) {
279                 record = list->data;
280
281                 path = g_strdup_printf("%s/nfc%d/device%d/record%d",
282                                         NFC_PATH, device->adapter_idx,
283                                         device->target_idx, device->n_records);
284
285                 if (!path)
286                         continue;
287
288                 __near_ndef_record_register(record, path);
289
290                 device->n_records++;
291                 device->records = g_list_append(device->records, record);
292         }
293
294         __near_agent_ndef_parse_records(device->records);
295
296         if (cb)
297                 cb(device->adapter_idx, device->target_idx, status);
298
299         g_list_free(records);
300
301         return 0;
302 }
303
304 struct near_device *__near_device_add(uint32_t adapter_idx, uint32_t target_idx,
305                                         uint8_t *nfcid, uint8_t nfcid_len)
306 {
307         struct near_device *device;
308         char *path;
309
310         device = near_device_get_device(adapter_idx, target_idx);
311         if (device)
312                 return NULL;
313
314         device = g_try_malloc0(sizeof(struct near_device));
315         if (!device)
316                 return NULL;
317
318         device->path = g_strdup_printf("%s/nfc%d/device%d", NFC_PATH,
319                                         adapter_idx, target_idx);
320         if (!device->path) {
321                 g_free(device);
322                 return NULL;
323         }
324         device->adapter_idx = adapter_idx;
325         device->target_idx = target_idx;
326         device->n_records = 0;
327
328         if (nfcid_len <= NFC_MAX_NFCID1_LEN && nfcid_len > 0) {
329                 device->nfcid_len = nfcid_len;
330                 memcpy(device->nfcid, nfcid, nfcid_len);
331         }
332
333         path = g_strdup(device->path);
334         if (!path) {
335                 g_free(device);
336                 return NULL;
337         }
338
339         g_hash_table_insert(device_hash, path, device);
340
341         return device;
342 }
343
344 bool __near_device_register_interface(struct near_device *device)
345 {
346         DBG("connection %p", connection);
347
348         return g_dbus_register_interface(connection, device->path,
349                                                 NFC_DEVICE_INTERFACE,
350                                                 device_methods, NULL, NULL,
351                                                         device, NULL);
352 }
353
354 int __near_device_listen(struct near_device *device, near_device_io_cb cb)
355 {
356         GSList *list;
357
358         DBG("");
359
360         for (list = driver_list; list; list = list->next) {
361                 struct near_device_driver *driver = list->data;
362
363                 return driver->listen(device->adapter_idx, cb);
364         }
365
366         return 0;
367 }
368
369 int __near_device_push(struct near_device *device,
370                         struct near_ndef_message *ndef, char *service_name,
371                         near_device_io_cb cb)
372 {
373         GSList *list;
374
375         DBG("");
376
377         if (!__near_adapter_get_dep_state(device->adapter_idx)) {
378                 near_error("DEP link is not established");
379                 return -ENOLINK;
380         }
381
382         for (list = driver_list; list; list = list->next) {
383                 struct near_device_driver *driver = list->data;
384
385                 return driver->push(device->adapter_idx, device->target_idx,
386                                         ndef, service_name, cb);
387         }
388
389         return 0;
390 }
391
392 static gint cmp_prio(gconstpointer a, gconstpointer b)
393 {
394         const struct near_tag_driver *driver1 = a;
395         const struct near_tag_driver *driver2 = b;
396
397         return driver2->priority - driver1->priority;
398 }
399
400 int near_device_driver_register(struct near_device_driver *driver)
401 {
402         DBG("");
403
404         if (!driver->listen)
405                 return -EINVAL;
406
407         driver_list = g_slist_insert_sorted(driver_list, driver, cmp_prio);
408
409         __near_adapter_listen(driver);
410
411         return 0;
412 }
413
414 void near_device_driver_unregister(struct near_device_driver *driver)
415 {
416         DBG("");
417
418         driver_list = g_slist_remove(driver_list, driver);
419 }
420
421 int __near_device_init(void)
422 {
423         DBG("");
424
425         connection = near_dbus_get_connection();
426
427         device_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
428                                                 g_free, free_device);
429
430         return 0;
431 }
432
433 void __near_device_cleanup(void)
434 {
435         DBG("");
436
437         g_hash_table_destroy(device_hash);
438         device_hash = NULL;
439 }