3 * neard - Near Field Communication manager
5 * Copyright (C) 2012 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
29 #include <sys/socket.h>
31 #include <linux/socket.h>
32 #include <linux/nfc.h>
34 #include <near/types.h>
36 #include <near/adapter.h>
37 #include <near/device.h>
39 #include <near/ndef.h>
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'
50 #define RECORD_TYPE_WKT_ALTERNATIVE_CARRIER 0x0a
51 #define FRAME_TYPE_OFFSET 3
53 enum loop_stage_flag {
54 STATE_MAIN_NDEF = 0x00,
55 STATE_CFG_RECORD = 0x01,
58 static GHashTable *hr_ndef_hash = NULL;
75 near_bool_t cfg_record_state;
76 near_bool_t in_extra_read;
79 struct hr_push_client {
87 static void free_hr_ndef(gpointer data)
89 struct hr_ndef *ndef = data;
97 static void handover_close(int client_fd, int err)
103 ndef = g_hash_table_lookup(hr_ndef_hash, GINT_TO_POINTER(client_fd));
107 g_hash_table_remove(hr_ndef_hash, GINT_TO_POINTER(client_fd));
110 /* Parse an incoming handover buffer*/
111 static int handover_ndef_parse(int client_fd, struct hr_ndef *ndef)
115 struct near_ndef_message *msg;
119 if ((ndef->ndef == NULL) ||
120 (ndef->cur_ptr < NDEF_HR_MSG_MIN_LENGTH)) {
125 /* call the global parse function */
126 records = near_ndef_parse(ndef->ndef, ndef->cur_ptr);
127 if (records == NULL) {
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)
138 if (strncmp((char *) (ndef->ndef + FRAME_TYPE_OFFSET), "Hr", 2) == 0) {
140 * The first entry on the record list is the Hr record.
141 * We build the Hs based on it.
143 msg = near_ndef_prepare_handover_record("Hs", records->data,
144 NEAR_CARRIER_BLUETOOTH);
150 near_info("Send Hs frame");
151 err = send(client_fd, msg->data, msg->length, MSG_DONTWAIT);
161 near_ndef_records_free(records);
166 near_error("ndef parsing failed %d", err);
168 handover_close(client_fd, 0);
173 static near_bool_t handover_recv_error(void)
175 near_error("%s", strerror(errno));
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,
188 struct hr_ndef *ndef;
192 ndef = g_hash_table_lookup(hr_ndef_hash, GINT_TO_POINTER(client_fd));
194 near_error("hr_ndef should exist");
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);
205 /* Read header bytes */
206 bytes_recv = recv(client_fd, ndef->ndef + ndef->cur_ptr,
207 NDEF_HR_MSG_MIN_LENGTH, MSG_DONTWAIT);
209 return handover_recv_error();
211 /* Now, check the ndef payload size plus header bytes */
212 ndef_size = near_ndef_record_length(ndef->ndef + ndef->cur_ptr,
217 ndef->cur_ptr += bytes_recv;
218 ndef->missing_bytes = ndef_size - bytes_recv;
220 /* Next prepare read to complete the NDEF */
221 ndef->ndef = g_try_realloc(ndef->ndef, ndef->cur_record_len
223 if (ndef->ndef == NULL) {
227 ndef->cur_record_len += ndef_size;
228 ndef->in_extra_read = FALSE;
233 /* Read remaining bytes */
234 bytes_recv = recv(client_fd, ndef->ndef + ndef->cur_ptr,
235 ndef->missing_bytes, MSG_DONTWAIT);
237 return handover_recv_error();
239 ndef->cur_ptr += bytes_recv;
240 ndef->missing_bytes -= bytes_recv;
242 /* Is the NDEF read complete ? */
243 if (ndef->missing_bytes)
244 return TRUE; /* more bytes to come... */
246 ndef->extra_ndef_count--;
247 ndef->in_extra_read = TRUE;
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);
255 /* Process the next NDEF */
259 near_error("Handover read NDEFs failed");
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)
268 struct hr_ndef *ndef;
272 ndef = g_hash_table_lookup(hr_ndef_hash, GINT_TO_POINTER(client_fd));
276 /* Read remaining bytes */
277 bytes_recv = recv(client_fd, ndef->ndef + ndef->cur_ptr,
278 ndef->missing_bytes, MSG_DONTWAIT);
280 return handover_recv_error();
282 ndef->cur_ptr += bytes_recv;
283 ndef->missing_bytes -= bytes_recv;
285 /* Is the ndef "Hr" read complete or should we loop */
286 if (ndef->missing_bytes)
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
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);
300 /* There's still some extra ndefs to read */
301 ndef->extra_ndef_count = extra_ndefs;
303 /* End of Handover message - now process extra records */
304 ndef->in_extra_read = TRUE;
305 ndef->cfg_record_state = TRUE;
310 near_error("Handover read failed");
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)
318 struct hr_ndef *ndef;
322 /* Allocate the ndef structure */
323 ndef = g_try_malloc0(sizeof(struct hr_ndef));
327 /* Allocate and read frame header (6 bytes) */
328 ndef->ndef = g_try_malloc0(NDEF_HR_MSG_MIN_LENGTH);
329 if (ndef->ndef == NULL)
332 /* Initialize default values */
334 ndef->cur_record_len = -1;
335 ndef->adapter_idx = adapter_idx;
336 ndef->target_idx = target_idx;
338 ndef->cfg_record_state = FALSE;
340 g_hash_table_insert(hr_ndef_hash, GINT_TO_POINTER(client_fd), ndef);
342 /* Read header bytes (6) */
343 bytes_recv = recv(client_fd, ndef->ndef,
344 NDEF_HR_MSG_MIN_LENGTH, MSG_DONTWAIT);
346 return handover_recv_error();
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)
353 ndef->cur_ptr += bytes_recv;
354 ndef->missing_bytes = ndef->cur_record_len - bytes_recv;
356 DBG("Handover frame size is %d", ndef->cur_ptr);
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)
372 * This function is a "dispatcher", to read Hr/Hs messages,
373 * and/or additional NDEF messages
375 static near_bool_t handover_read(int client_fd,
376 uint32_t adapter_idx, uint32_t target_idx,
379 struct hr_ndef *ndef;
381 ndef = g_hash_table_lookup(hr_ndef_hash, GINT_TO_POINTER(client_fd));
383 /* First call: allocate and read header bytes */
384 return handover_read_initialize(client_fd, adapter_idx,
388 if (ndef->cfg_record_state == TRUE) {
389 return handover_read_cfg_records(client_fd, adapter_idx,
393 return handover_read_hr(client_fd, adapter_idx, target_idx, cb);
396 static void free_hr_push_client(struct hr_push_client *client, int status)
400 handover_close(client->fd, 0);
403 client->cb(client->adapter_idx, client->target_idx, status);
405 if (client->watch > 0)
406 g_source_remove(client->watch);
411 static gboolean handover_push_event(GIOChannel *channel,
412 GIOCondition condition, gpointer data)
415 struct hr_push_client *client = (struct hr_push_client *) data;
417 DBG("condition 0x%x", condition);
419 if (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
420 near_error("Error with Handover client");
422 free_hr_push_client(client, -EIO);
427 ret = handover_read(client->fd,
428 client->adapter_idx, client->target_idx,
432 free_hr_push_client(client, 0);
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)
443 struct hr_push_client *client;
448 client = g_try_malloc0(sizeof(struct hr_push_client));
452 channel = g_io_channel_unix_new(client_fd);
453 g_io_channel_set_close_on_unref(channel, TRUE);
455 client->fd = client_fd;
456 client->adapter_idx = adapter_idx;
457 client->target_idx = target_idx;
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,
464 g_io_channel_unref(channel);
466 err = send(client_fd, ndef->data, ndef->length, MSG_DONTWAIT);
468 free_hr_push_client(client, err);
469 g_io_channel_unref(channel);
475 struct near_p2p_driver handover_driver = {
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,
484 int handover_init(void)
486 hr_ndef_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
489 return near_p2p_register(&handover_driver);
492 void handover_exit(void)
494 near_p2p_unregister(&handover_driver);
496 g_hash_table_destroy(hr_ndef_hash);