Integrate neard post 0.6 changes - handover code
[profile/ivi/neard.git] / plugins / handover.c
1 /*
2  *
3  *  neard - Near Field Communication manager
4  *
5  *  Copyright (C) 2012  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 <stdint.h>
27 #include <errno.h>
28 #include <string.h>
29 #include <sys/socket.h>
30
31 #include <linux/socket.h>
32 #include <linux/nfc.h>
33
34 #include <near/types.h>
35 #include <near/log.h>
36 #include <near/adapter.h>
37 #include <near/device.h>
38 #include <near/tag.h>
39 #include <near/ndef.h>
40 #include <near/tlv.h>
41
42 #include "p2p.h"
43
44 #define NDEF_HR_MSG_MIN_LENGTH 0x06
45 #define HR_HEADER_SIZE  6               /* header (1) + type len (1) +
46                                         *  payload len (1) + rec type (2) 'Hx'
47                                         *  + version (1)
48                                         */
49
50 #define RECORD_TYPE_WKT_ALTERNATIVE_CARRIER 0x0a
51 #define FRAME_TYPE_OFFSET       4
52
53 enum loop_stage_flag {
54         STATE_MAIN_NDEF         = 0x00,
55         STATE_CFG_RECORD        = 0x01,
56 };
57
58 static GHashTable *hr_ndef_hash = NULL;
59
60 struct extra_ndef {
61         uint8_t *ndef;
62         uint8_t length;
63 };
64
65 struct hr_ndef {
66         uint8_t *ndef;
67         uint16_t cur_ptr;
68         int cur_record_len;
69         int missing_bytes;
70         uint32_t adapter_idx;
71         uint32_t target_idx;
72         near_tag_io_cb cb;
73         int extra_ndef_count;
74         int block_free_size;
75         near_bool_t cfg_record_state;
76         near_bool_t in_extra_read;
77 };
78
79 struct hr_push_client {
80         uint8_t fd;
81         uint32_t adapter_idx;
82         uint32_t target_idx;
83         near_device_io_cb cb;
84         guint watch;
85 };
86
87 static void free_hr_ndef(gpointer data)
88 {
89         struct hr_ndef *ndef = data;
90
91         if (ndef != NULL)
92                 g_free(ndef->ndef);
93
94         g_free(ndef);
95 }
96
97 static void handover_close(int client_fd, int err)
98 {
99         struct hr_ndef *ndef;
100
101         DBG("");
102
103         ndef = g_hash_table_lookup(hr_ndef_hash, GINT_TO_POINTER(client_fd));
104         if (ndef == NULL)
105                 return;
106
107         g_hash_table_remove(hr_ndef_hash, GINT_TO_POINTER(client_fd));
108 }
109
110 /* Parse an incoming handover buffer*/
111 static int handover_ndef_parse(int client_fd, struct hr_ndef *ndef)
112 {
113         int err;
114         GList *records;
115         struct near_ndef_message *msg;
116
117         DBG("");
118
119         if ((ndef->ndef == NULL) ||
120                         (ndef->cur_ptr < NDEF_HR_MSG_MIN_LENGTH)) {
121                 err = -EINVAL;
122                 goto fail;
123         }
124
125         /* call the global parse function */
126         records = near_ndef_parse(ndef->ndef, ndef->cur_ptr);
127         if (records == NULL) {
128                 err = -ENOMEM;
129                 goto fail;
130         }
131
132         /*
133          * If we receive a request, we should reply with a Hs but
134          * if the initial frame is Hs (it means we initiated the
135          * exchange with a Hr), so we have to do some actions (e.g.:
136          * pairing with bluetooth)
137          */
138         if (strncmp((char *) (ndef->ndef + FRAME_TYPE_OFFSET), "Hr", 2) == 0) {
139                 /*
140                  * The first entry on the record list is the Hr record.
141                  * We build the Hs based on it.
142                  */
143                 msg = near_ndef_prepare_handover_record("Hs", records->data,
144                                                         NEAR_CARRIER_BLUETOOTH);
145
146                 near_info("Send Hs frame");
147                 err = send(client_fd, msg->data, msg->length, MSG_DONTWAIT);
148                 if (err >= 0)
149                         err = 0;
150
151                 g_free(msg->data);
152                 g_free(msg);
153         } else {
154                 err = 0;
155         }
156
157         near_ndef_records_free(records);
158
159         return err;
160
161 fail:
162         near_error("ndef parsing failed %d", err);
163
164         handover_close(client_fd, 0);
165
166         return err;
167 }
168
169 static near_bool_t handover_recv_error(void)
170 {
171         near_error("%s", strerror(errno));
172
173         if (errno == EAGAIN)
174                 return TRUE;
175
176         return FALSE;
177 }
178
179 /* Add extra records right after the end of the "Hr" ndef record */
180 static near_bool_t handover_read_cfg_records(int client_fd,
181                                 uint32_t adapter_idx, uint32_t target_idx,
182                                 near_tag_io_cb cb)
183 {
184         struct hr_ndef *ndef;
185         int bytes_recv;
186         int ndef_size;
187
188         ndef = g_hash_table_lookup(hr_ndef_hash, GINT_TO_POINTER(client_fd));
189         if (ndef == NULL) {
190                 near_error("hr_ndef should exist");
191                 return FALSE;
192         }
193
194         if (ndef->in_extra_read == TRUE) {
195                 /* Next prepare read to complete the Hr */
196                 ndef->ndef = g_try_realloc(ndef->ndef, ndef->cur_record_len +
197                                 NDEF_HR_MSG_MIN_LENGTH);
198                 if (ndef == NULL)
199                         return FALSE;
200
201                 /* Read header bytes */
202                 bytes_recv = recv(client_fd, ndef->ndef + ndef->cur_ptr,
203                                 NDEF_HR_MSG_MIN_LENGTH, MSG_DONTWAIT);
204                 if (bytes_recv < 0)
205                         return handover_recv_error();
206
207                 /* Now, check the ndef payload size plus header bytes */
208                 ndef_size = near_ndef_record_length(ndef->ndef + ndef->cur_ptr,
209                                                                 bytes_recv);
210                 if (ndef_size < 0)
211                         goto fail;
212
213                 ndef->cur_ptr += bytes_recv;
214                 ndef->missing_bytes = ndef_size - bytes_recv;
215
216                 /* Next prepare read to complete the NDEF */
217                 ndef->ndef = g_try_realloc(ndef->ndef, ndef->cur_record_len
218                                                                 + ndef_size);
219                 if (ndef->ndef == NULL) {
220                         g_free(ndef);
221                         return FALSE;
222                 }
223                 ndef->cur_record_len += ndef_size;
224                 ndef->in_extra_read = FALSE;
225
226                 return TRUE;
227         }
228
229         /* Read remaining bytes */
230         bytes_recv = recv(client_fd, ndef->ndef + ndef->cur_ptr,
231                                         ndef->missing_bytes, MSG_DONTWAIT);
232         if (bytes_recv < 0)
233                 return handover_recv_error();
234
235         ndef->cur_ptr += bytes_recv;
236         ndef->missing_bytes -= bytes_recv;
237
238         /* Is the NDEF read complete ? */
239         if (ndef->missing_bytes)
240                 return TRUE;    /* more bytes to come... */
241
242         ndef->extra_ndef_count--;
243         ndef->in_extra_read = TRUE;
244
245         if (ndef->extra_ndef_count == 0) {
246                 /* All the bytes are read so now, parse the frame */
247                 handover_ndef_parse(client_fd, ndef);
248                 return FALSE;
249         }
250
251         /* Process the next NDEF */
252         return TRUE;
253
254 fail:
255         near_error("Handover read NDEFs failed");
256         return FALSE;
257 }
258
259 static near_bool_t handover_read_hr(int client_fd,
260                 uint32_t adapter_idx, uint32_t target_idx, near_tag_io_cb cb)
261 {
262         int bytes_recv;
263         int extra_ndefs;
264         struct hr_ndef *ndef;
265
266         DBG("");
267
268         ndef = g_hash_table_lookup(hr_ndef_hash, GINT_TO_POINTER(client_fd));
269         if (ndef == NULL)
270                 return FALSE;
271
272         /* Read remaining bytes */
273         bytes_recv = recv(client_fd, ndef->ndef + ndef->cur_ptr,
274                         ndef->missing_bytes, MSG_DONTWAIT);
275         if (bytes_recv < 0)
276                 return handover_recv_error();
277
278         ndef->cur_ptr += bytes_recv;
279         ndef->missing_bytes -= bytes_recv;
280
281         /* Is the ndef "Hr" read complete or should we loop */
282         if (ndef->missing_bytes)
283                 return TRUE;
284
285         /*
286          * The first NDEF frame is read. We now should determine how many
287          * extra records follow the NDEF frame.
288          * We skip the first 6 bytes (Hr header) to jump on the first record
289          */
290         extra_ndefs = near_ndef_count_records(ndef->ndef + HR_HEADER_SIZE,
291                         ndef->cur_record_len - HR_HEADER_SIZE,
292                         RECORD_TYPE_WKT_ALTERNATIVE_CARRIER);
293         if (extra_ndefs < 0)
294                 goto fail;
295
296         /* There's still some extra ndefs to read */
297         ndef->extra_ndef_count = extra_ndefs;
298
299         /* End of Handover message - now process extra records */
300         ndef->in_extra_read = TRUE;
301         ndef->cfg_record_state = TRUE;
302
303         return TRUE;
304
305 fail:
306         near_error("Handover read failed");
307         return FALSE;
308 }
309
310 static near_bool_t handover_read_initialize(int client_fd,
311                 uint32_t adapter_idx, uint32_t target_idx, near_tag_io_cb cb)
312 {
313         int bytes_recv;
314         struct hr_ndef *ndef;
315
316         DBG("");
317
318         /* Allocate the ndef structure */
319         ndef = g_try_malloc0(sizeof(struct hr_ndef));
320         if (ndef == NULL)
321                 goto fail;
322
323         /* Allocate and read frame header (6 bytes) */
324         ndef->ndef = g_try_malloc0(NDEF_HR_MSG_MIN_LENGTH);
325         if (ndef->ndef == NULL)
326                 goto fail;
327
328         /* Initialize default values */
329         ndef->cur_ptr = 0;
330         ndef->cur_record_len = -1;
331         ndef->adapter_idx = adapter_idx;
332         ndef->target_idx = target_idx;
333         ndef->cb = cb;
334         ndef->cfg_record_state = FALSE;
335
336         g_hash_table_insert(hr_ndef_hash, GINT_TO_POINTER(client_fd), ndef);
337
338         /* Read header bytes (6) */
339         bytes_recv = recv(client_fd, ndef->ndef,
340                                 NDEF_HR_MSG_MIN_LENGTH, MSG_DONTWAIT);
341         if (bytes_recv < 0)
342                 return handover_recv_error();
343
344         /* Now, check the ndef payload size plus header bytes */
345         ndef->cur_record_len = near_ndef_record_length(ndef->ndef, bytes_recv);
346         if (ndef->cur_record_len < 0)
347                 goto fail;
348
349         ndef->cur_ptr += bytes_recv;
350         ndef->missing_bytes = ndef->cur_record_len - bytes_recv;
351
352         DBG("Handover frame size is %d", ndef->cur_ptr);
353
354         /* Next prepare read to complete the read */
355         ndef->ndef = g_try_realloc(ndef->ndef, ndef->cur_record_len);
356         if (ndef->ndef == NULL)
357                 goto fail;
358
359         return TRUE;
360
361 fail:
362         free_hr_ndef(ndef);
363
364         return FALSE;
365 }
366
367 /*
368  * This function is a "dispatcher", to read Hr/Hs messages,
369  * and/or additional NDEF messages
370  */
371 static near_bool_t handover_read(int client_fd,
372                 uint32_t adapter_idx, uint32_t target_idx,
373                 near_tag_io_cb cb)
374 {
375         struct hr_ndef *ndef;
376
377         ndef = g_hash_table_lookup(hr_ndef_hash, GINT_TO_POINTER(client_fd));
378         if (ndef == NULL) {
379                 /* First call: allocate and read header bytes */
380                 return handover_read_initialize(client_fd, adapter_idx,
381                                                 target_idx, cb);
382         }
383
384         if (ndef->cfg_record_state == TRUE) {
385                 return handover_read_cfg_records(client_fd, adapter_idx,
386                                                         target_idx, cb);
387         }
388
389         return handover_read_hr(client_fd, adapter_idx, target_idx, cb);
390 }
391
392 static void free_hr_push_client(struct hr_push_client *client, int status)
393 {
394         DBG("");
395
396         handover_close(client->fd, 0);
397         close(client->fd);
398
399         if (client->cb)
400                 client->cb(client->adapter_idx, client->target_idx, status);
401
402         if (client->watch > 0)
403                 g_source_remove(client->watch);
404
405         g_free(client);
406 }
407
408 static gboolean handover_push_event(GIOChannel *channel,
409                                 GIOCondition condition, gpointer data)
410 {
411         near_bool_t ret;
412         struct hr_push_client *client = (struct hr_push_client *) data;
413
414         DBG("condition 0x%x", condition);
415
416         if (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
417                 near_error("Error with Handover client");
418
419                 free_hr_push_client(client, -EIO);
420
421                 return FALSE;
422         }
423
424         ret = handover_read(client->fd,
425                         client->adapter_idx, client->target_idx,
426                         client->cb);
427
428         if (ret == FALSE)
429                 free_hr_push_client(client, 0);
430
431         return ret;
432 }
433
434 static int handover_push(int client_fd,
435                         uint32_t adapter_idx, uint32_t target_idx,
436                         struct near_ndef_message *ndef,
437                         near_device_io_cb cb)
438 {
439         int err;
440         struct hr_push_client *client;
441         GIOChannel *channel;
442
443         DBG("");
444
445         client = g_try_malloc0(sizeof(struct hr_push_client));
446         if (client == NULL)
447                 return -ENOMEM;
448
449         channel = g_io_channel_unix_new(client_fd);
450         g_io_channel_set_close_on_unref(channel, TRUE);
451
452         client->fd = client_fd;
453         client->adapter_idx = adapter_idx;
454         client->target_idx = target_idx;
455         client->cb = cb;
456         client->watch = g_io_add_watch(channel,
457                                         G_IO_IN | G_IO_HUP | G_IO_NVAL |
458                                         G_IO_ERR, handover_push_event,
459                                         (gpointer) client);
460
461         err = send(client_fd, ndef->data, ndef->length, MSG_DONTWAIT);
462         if (err < 0)
463                 free_hr_push_client(client, err);
464
465         return err;
466 }
467
468 struct near_p2p_driver handover_driver = {
469         .name = "Handover",
470         .service_name = NEAR_DEVICE_SN_HANDOVER,
471         .fallback_service_name = NEAR_DEVICE_SN_SNEP,
472         .read = handover_read,
473         .push = handover_push,
474         .close = handover_close,
475 };
476
477 int handover_init(void)
478 {
479         hr_ndef_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
480                                                         NULL, free_hr_ndef);
481
482         return near_p2p_register(&handover_driver);
483 }
484
485 void handover_exit(void)
486 {
487         near_p2p_unregister(&handover_driver);
488
489         g_hash_table_destroy(hr_ndef_hash);
490         hr_ndef_hash = NULL;
491 }