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 4
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);
146 near_info("Send Hs frame");
147 err = send(client_fd, msg->data, msg->length, MSG_DONTWAIT);
157 near_ndef_records_free(records);
162 near_error("ndef parsing failed %d", err);
164 handover_close(client_fd, 0);
169 static near_bool_t handover_recv_error(void)
171 near_error("%s", strerror(errno));
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,
184 struct hr_ndef *ndef;
188 ndef = g_hash_table_lookup(hr_ndef_hash, GINT_TO_POINTER(client_fd));
190 near_error("hr_ndef should exist");
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);
201 /* Read header bytes */
202 bytes_recv = recv(client_fd, ndef->ndef + ndef->cur_ptr,
203 NDEF_HR_MSG_MIN_LENGTH, MSG_DONTWAIT);
205 return handover_recv_error();
207 /* Now, check the ndef payload size plus header bytes */
208 ndef_size = near_ndef_record_length(ndef->ndef + ndef->cur_ptr,
213 ndef->cur_ptr += bytes_recv;
214 ndef->missing_bytes = ndef_size - bytes_recv;
216 /* Next prepare read to complete the NDEF */
217 ndef->ndef = g_try_realloc(ndef->ndef, ndef->cur_record_len
219 if (ndef->ndef == NULL) {
223 ndef->cur_record_len += ndef_size;
224 ndef->in_extra_read = FALSE;
229 /* Read remaining bytes */
230 bytes_recv = recv(client_fd, ndef->ndef + ndef->cur_ptr,
231 ndef->missing_bytes, MSG_DONTWAIT);
233 return handover_recv_error();
235 ndef->cur_ptr += bytes_recv;
236 ndef->missing_bytes -= bytes_recv;
238 /* Is the NDEF read complete ? */
239 if (ndef->missing_bytes)
240 return TRUE; /* more bytes to come... */
242 ndef->extra_ndef_count--;
243 ndef->in_extra_read = TRUE;
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);
251 /* Process the next NDEF */
255 near_error("Handover read NDEFs failed");
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)
264 struct hr_ndef *ndef;
268 ndef = g_hash_table_lookup(hr_ndef_hash, GINT_TO_POINTER(client_fd));
272 /* Read remaining bytes */
273 bytes_recv = recv(client_fd, ndef->ndef + ndef->cur_ptr,
274 ndef->missing_bytes, MSG_DONTWAIT);
276 return handover_recv_error();
278 ndef->cur_ptr += bytes_recv;
279 ndef->missing_bytes -= bytes_recv;
281 /* Is the ndef "Hr" read complete or should we loop */
282 if (ndef->missing_bytes)
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
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);
296 /* There's still some extra ndefs to read */
297 ndef->extra_ndef_count = extra_ndefs;
299 /* End of Handover message - now process extra records */
300 ndef->in_extra_read = TRUE;
301 ndef->cfg_record_state = TRUE;
306 near_error("Handover read failed");
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)
314 struct hr_ndef *ndef;
318 /* Allocate the ndef structure */
319 ndef = g_try_malloc0(sizeof(struct hr_ndef));
323 /* Allocate and read frame header (6 bytes) */
324 ndef->ndef = g_try_malloc0(NDEF_HR_MSG_MIN_LENGTH);
325 if (ndef->ndef == NULL)
328 /* Initialize default values */
330 ndef->cur_record_len = -1;
331 ndef->adapter_idx = adapter_idx;
332 ndef->target_idx = target_idx;
334 ndef->cfg_record_state = FALSE;
336 g_hash_table_insert(hr_ndef_hash, GINT_TO_POINTER(client_fd), ndef);
338 /* Read header bytes (6) */
339 bytes_recv = recv(client_fd, ndef->ndef,
340 NDEF_HR_MSG_MIN_LENGTH, MSG_DONTWAIT);
342 return handover_recv_error();
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)
349 ndef->cur_ptr += bytes_recv;
350 ndef->missing_bytes = ndef->cur_record_len - bytes_recv;
352 DBG("Handover frame size is %d", ndef->cur_ptr);
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)
368 * This function is a "dispatcher", to read Hr/Hs messages,
369 * and/or additional NDEF messages
371 static near_bool_t handover_read(int client_fd,
372 uint32_t adapter_idx, uint32_t target_idx,
375 struct hr_ndef *ndef;
377 ndef = g_hash_table_lookup(hr_ndef_hash, GINT_TO_POINTER(client_fd));
379 /* First call: allocate and read header bytes */
380 return handover_read_initialize(client_fd, adapter_idx,
384 if (ndef->cfg_record_state == TRUE) {
385 return handover_read_cfg_records(client_fd, adapter_idx,
389 return handover_read_hr(client_fd, adapter_idx, target_idx, cb);
392 static void free_hr_push_client(struct hr_push_client *client, int status)
396 handover_close(client->fd, 0);
400 client->cb(client->adapter_idx, client->target_idx, status);
402 if (client->watch > 0)
403 g_source_remove(client->watch);
408 static gboolean handover_push_event(GIOChannel *channel,
409 GIOCondition condition, gpointer data)
412 struct hr_push_client *client = (struct hr_push_client *) data;
414 DBG("condition 0x%x", condition);
416 if (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
417 near_error("Error with Handover client");
419 free_hr_push_client(client, -EIO);
424 ret = handover_read(client->fd,
425 client->adapter_idx, client->target_idx,
429 free_hr_push_client(client, 0);
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)
440 struct hr_push_client *client;
445 client = g_try_malloc0(sizeof(struct hr_push_client));
449 channel = g_io_channel_unix_new(client_fd);
450 g_io_channel_set_close_on_unref(channel, TRUE);
452 client->fd = client_fd;
453 client->adapter_idx = adapter_idx;
454 client->target_idx = target_idx;
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,
461 err = send(client_fd, ndef->data, ndef->length, MSG_DONTWAIT);
463 free_hr_push_client(client, err);
468 struct near_p2p_driver handover_driver = {
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,
477 int handover_init(void)
479 hr_ndef_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
482 return near_p2p_register(&handover_driver);
485 void handover_exit(void)
487 near_p2p_unregister(&handover_driver);
489 g_hash_table_destroy(hr_ndef_hash);