Fix p2p bug and various warnings
[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       3
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                 if (msg == NULL) {
146                         err = -EINVAL;
147                         goto fail;
148                 }
149
150                 near_info("Send Hs frame");
151                 err = send(client_fd, msg->data, msg->length, MSG_DONTWAIT);
152                 if (err >= 0)
153                         err = 0;
154
155                 g_free(msg->data);
156                 g_free(msg);
157         } else {
158                 err = 0;
159         }
160
161         near_ndef_records_free(records);
162
163         return err;
164
165 fail:
166         near_error("ndef parsing failed %d", err);
167
168         handover_close(client_fd, 0);
169
170         return err;
171 }
172
173 static near_bool_t handover_recv_error(void)
174 {
175         near_error("%s", strerror(errno));
176
177         if (errno == EAGAIN)
178                 return TRUE;
179
180         return FALSE;
181 }
182
183 /* Add extra records right after the end of the "Hr" ndef record */
184 static near_bool_t handover_read_cfg_records(int client_fd,
185                                 uint32_t adapter_idx, uint32_t target_idx,
186                                 near_tag_io_cb cb)
187 {
188         struct hr_ndef *ndef;
189         int bytes_recv;
190         int ndef_size;
191
192         ndef = g_hash_table_lookup(hr_ndef_hash, GINT_TO_POINTER(client_fd));
193         if (ndef == NULL) {
194                 near_error("hr_ndef should exist");
195                 return FALSE;
196         }
197
198         if (ndef->in_extra_read == TRUE) {
199                 /* Next prepare read to complete the Hr */
200                 ndef->ndef = g_try_realloc(ndef->ndef, ndef->cur_record_len +
201                                 NDEF_HR_MSG_MIN_LENGTH);
202                 if (ndef == NULL)
203                         return FALSE;
204
205                 /* Read header bytes */
206                 bytes_recv = recv(client_fd, ndef->ndef + ndef->cur_ptr,
207                                 NDEF_HR_MSG_MIN_LENGTH, MSG_DONTWAIT);
208                 if (bytes_recv < 0)
209                         return handover_recv_error();
210
211                 /* Now, check the ndef payload size plus header bytes */
212                 ndef_size = near_ndef_record_length(ndef->ndef + ndef->cur_ptr,
213                                                                 bytes_recv);
214                 if (ndef_size < 0)
215                         goto fail;
216
217                 ndef->cur_ptr += bytes_recv;
218                 ndef->missing_bytes = ndef_size - bytes_recv;
219
220                 /* Next prepare read to complete the NDEF */
221                 ndef->ndef = g_try_realloc(ndef->ndef, ndef->cur_record_len
222                                                                 + ndef_size);
223                 if (ndef->ndef == NULL) {
224                         g_free(ndef);
225                         return FALSE;
226                 }
227                 ndef->cur_record_len += ndef_size;
228                 ndef->in_extra_read = FALSE;
229
230                 return TRUE;
231         }
232
233         /* Read remaining bytes */
234         bytes_recv = recv(client_fd, ndef->ndef + ndef->cur_ptr,
235                                         ndef->missing_bytes, MSG_DONTWAIT);
236         if (bytes_recv < 0)
237                 return handover_recv_error();
238
239         ndef->cur_ptr += bytes_recv;
240         ndef->missing_bytes -= bytes_recv;
241
242         /* Is the NDEF read complete ? */
243         if (ndef->missing_bytes)
244                 return TRUE;    /* more bytes to come... */
245
246         ndef->extra_ndef_count--;
247         ndef->in_extra_read = TRUE;
248
249         if (ndef->extra_ndef_count == 0) {
250                 /* All the bytes are read so now, parse the frame */
251                 handover_ndef_parse(client_fd, ndef);
252                 return FALSE;
253         }
254
255         /* Process the next NDEF */
256         return TRUE;
257
258 fail:
259         near_error("Handover read NDEFs failed");
260         return FALSE;
261 }
262
263 static near_bool_t handover_read_hr(int client_fd,
264                 uint32_t adapter_idx, uint32_t target_idx, near_tag_io_cb cb)
265 {
266         int bytes_recv;
267         int extra_ndefs;
268         struct hr_ndef *ndef;
269
270         DBG("");
271
272         ndef = g_hash_table_lookup(hr_ndef_hash, GINT_TO_POINTER(client_fd));
273         if (ndef == NULL)
274                 return FALSE;
275
276         /* Read remaining bytes */
277         bytes_recv = recv(client_fd, ndef->ndef + ndef->cur_ptr,
278                         ndef->missing_bytes, MSG_DONTWAIT);
279         if (bytes_recv < 0)
280                 return handover_recv_error();
281
282         ndef->cur_ptr += bytes_recv;
283         ndef->missing_bytes -= bytes_recv;
284
285         /* Is the ndef "Hr" read complete or should we loop */
286         if (ndef->missing_bytes)
287                 return TRUE;
288
289         /*
290          * The first NDEF frame is read. We now should determine how many
291          * extra records follow the NDEF frame.
292          * We skip the first 6 bytes (Hr header) to jump on the first record
293          */
294         extra_ndefs = near_ndef_count_records(ndef->ndef + HR_HEADER_SIZE,
295                         ndef->cur_record_len - HR_HEADER_SIZE,
296                         RECORD_TYPE_WKT_ALTERNATIVE_CARRIER);
297         if (extra_ndefs < 0)
298                 goto fail;
299
300         /* There's still some extra ndefs to read */
301         ndef->extra_ndef_count = extra_ndefs;
302
303         /* End of Handover message - now process extra records */
304         ndef->in_extra_read = TRUE;
305         ndef->cfg_record_state = TRUE;
306
307         return TRUE;
308
309 fail:
310         near_error("Handover read failed");
311         return FALSE;
312 }
313
314 static near_bool_t handover_read_initialize(int client_fd,
315                 uint32_t adapter_idx, uint32_t target_idx, near_tag_io_cb cb)
316 {
317         int bytes_recv;
318         struct hr_ndef *ndef;
319
320         DBG("");
321
322         /* Allocate the ndef structure */
323         ndef = g_try_malloc0(sizeof(struct hr_ndef));
324         if (ndef == NULL)
325                 goto fail;
326
327         /* Allocate and read frame header (6 bytes) */
328         ndef->ndef = g_try_malloc0(NDEF_HR_MSG_MIN_LENGTH);
329         if (ndef->ndef == NULL)
330                 goto fail;
331
332         /* Initialize default values */
333         ndef->cur_ptr = 0;
334         ndef->cur_record_len = -1;
335         ndef->adapter_idx = adapter_idx;
336         ndef->target_idx = target_idx;
337         ndef->cb = cb;
338         ndef->cfg_record_state = FALSE;
339
340         g_hash_table_insert(hr_ndef_hash, GINT_TO_POINTER(client_fd), ndef);
341
342         /* Read header bytes (6) */
343         bytes_recv = recv(client_fd, ndef->ndef,
344                                 NDEF_HR_MSG_MIN_LENGTH, MSG_DONTWAIT);
345         if (bytes_recv < 0)
346                 return handover_recv_error();
347
348         /* Now, check the ndef payload size plus header bytes */
349         ndef->cur_record_len = near_ndef_record_length(ndef->ndef, bytes_recv);
350         if (ndef->cur_record_len < 0)
351                 goto fail;
352
353         ndef->cur_ptr += bytes_recv;
354         ndef->missing_bytes = ndef->cur_record_len - bytes_recv;
355
356         DBG("Handover frame size is %d", ndef->cur_ptr);
357
358         /* Next prepare read to complete the read */
359         ndef->ndef = g_try_realloc(ndef->ndef, ndef->cur_record_len);
360         if (ndef->ndef == NULL)
361                 goto fail;
362
363         return TRUE;
364
365 fail:
366         free_hr_ndef(ndef);
367
368         return FALSE;
369 }
370
371 /*
372  * This function is a "dispatcher", to read Hr/Hs messages,
373  * and/or additional NDEF messages
374  */
375 static near_bool_t handover_read(int client_fd,
376                 uint32_t adapter_idx, uint32_t target_idx,
377                 near_tag_io_cb cb)
378 {
379         struct hr_ndef *ndef;
380
381         ndef = g_hash_table_lookup(hr_ndef_hash, GINT_TO_POINTER(client_fd));
382         if (ndef == NULL) {
383                 /* First call: allocate and read header bytes */
384                 return handover_read_initialize(client_fd, adapter_idx,
385                                                 target_idx, cb);
386         }
387
388         if (ndef->cfg_record_state == TRUE) {
389                 return handover_read_cfg_records(client_fd, adapter_idx,
390                                                         target_idx, cb);
391         }
392
393         return handover_read_hr(client_fd, adapter_idx, target_idx, cb);
394 }
395
396 static void free_hr_push_client(struct hr_push_client *client, int status)
397 {
398         DBG("");
399
400         handover_close(client->fd, 0);
401
402         if (client->cb)
403                 client->cb(client->adapter_idx, client->target_idx, status);
404
405         if (client->watch > 0)
406                 g_source_remove(client->watch);
407
408         g_free(client);
409 }
410
411 static gboolean handover_push_event(GIOChannel *channel,
412                                 GIOCondition condition, gpointer data)
413 {
414         near_bool_t ret;
415         struct hr_push_client *client = (struct hr_push_client *) data;
416
417         DBG("condition 0x%x", condition);
418
419         if (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
420                 near_error("Error with Handover client");
421
422                 free_hr_push_client(client, -EIO);
423
424                 return FALSE;
425         }
426
427         ret = handover_read(client->fd,
428                         client->adapter_idx, client->target_idx,
429                         client->cb);
430
431         if (ret == FALSE)
432                 free_hr_push_client(client, 0);
433
434         return ret;
435 }
436
437 static int handover_push(int client_fd,
438                         uint32_t adapter_idx, uint32_t target_idx,
439                         struct near_ndef_message *ndef,
440                         near_device_io_cb cb)
441 {
442         int err;
443         struct hr_push_client *client;
444         GIOChannel *channel;
445
446         DBG("");
447
448         client = g_try_malloc0(sizeof(struct hr_push_client));
449         if (client == NULL)
450                 return -ENOMEM;
451
452         channel = g_io_channel_unix_new(client_fd);
453         g_io_channel_set_close_on_unref(channel, TRUE);
454
455         client->fd = client_fd;
456         client->adapter_idx = adapter_idx;
457         client->target_idx = target_idx;
458         client->cb = cb;
459         client->watch = g_io_add_watch(channel,
460                                         G_IO_IN | G_IO_HUP | G_IO_NVAL |
461                                         G_IO_ERR, handover_push_event,
462                                         (gpointer) client);
463
464         g_io_channel_unref(channel);
465
466         err = send(client_fd, ndef->data, ndef->length, MSG_DONTWAIT);
467         if (err < 0) {
468                 free_hr_push_client(client, err);
469                 g_io_channel_unref(channel);
470         }
471
472         return err;
473 }
474
475 struct near_p2p_driver handover_driver = {
476         .name = "Handover",
477         .service_name = NEAR_DEVICE_SN_HANDOVER,
478         .fallback_service_name = NEAR_DEVICE_SN_SNEP,
479         .read = handover_read,
480         .push = handover_push,
481         .close = handover_close,
482 };
483
484 int handover_init(void)
485 {
486         hr_ndef_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
487                                                         NULL, free_hr_ndef);
488
489         return near_p2p_register(&handover_driver);
490 }
491
492 void handover_exit(void)
493 {
494         near_p2p_unregister(&handover_driver);
495
496         g_hash_table_destroy(hr_ndef_hash);
497         hr_ndef_hash = NULL;
498 }