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;
96 static void handover_close(int client_fd, int err)
102 ndef = g_hash_table_lookup(hr_ndef_hash, GINT_TO_POINTER(client_fd));
106 g_hash_table_remove(hr_ndef_hash, GINT_TO_POINTER(client_fd));
109 /* Parse an incoming handover buffer*/
110 static int handover_ndef_parse(int client_fd, struct hr_ndef *ndef)
114 struct near_ndef_message *msg;
118 if ((ndef->ndef == NULL) ||
119 (ndef->cur_ptr < NDEF_HR_MSG_MIN_LENGTH)) {
124 /* call the global parse function */
125 records = near_ndef_parse(ndef->ndef, ndef->cur_ptr);
126 if (records == NULL) {
132 * If we receive a request, we should reply with a Hs but
133 * if the initial frame is Hs (it means we initiated the
134 * exchange with a Hr), so we have to do some actions (e.g.:
135 * pairing with bluetooth)
137 if (strncmp((char *)(ndef->ndef + FRAME_TYPE_OFFSET), "Hr", 2) == 0) {
139 * The first entry on the record list is the Hr record.
140 * We build the Hs based on it.
142 msg = near_ndef_prepare_handover_record("Hs", records->data,
143 NEAR_CARRIER_BLUETOOTH);
145 near_info("Send Hs frame");
146 err = send(client_fd, msg->data, msg->length, MSG_DONTWAIT);
156 near_ndef_records_free(records);
161 near_error("ndef parsing failed (%d)", err);
163 handover_close(client_fd, 0);
168 static near_bool_t handover_recv_error(void)
170 near_error("%s", strerror(errno));
178 /* Add extra records right after the end of the "Hr" ndef record */
179 static near_bool_t handover_read_cfg_records(int client_fd,
180 uint32_t adapter_idx, uint32_t target_idx,
183 struct hr_ndef *ndef;
187 ndef = g_hash_table_lookup(hr_ndef_hash, GINT_TO_POINTER(client_fd));
189 near_error("hr_ndef should exist !!!");
193 if (ndef->in_extra_read == TRUE) {
194 /* Next prepare read to complete the Hr */
195 ndef->ndef = g_try_realloc(ndef->ndef, ndef->cur_record_len +
196 NDEF_HR_MSG_MIN_LENGTH);
200 /* Read header bytes */
201 bytes_recv = recv(client_fd, ndef->ndef + ndef->cur_ptr,
202 NDEF_HR_MSG_MIN_LENGTH, MSG_DONTWAIT);
204 return handover_recv_error();
206 /* Now, check the ndef payload size plus header bytes */
207 ndef_size = near_ndef_record_length(ndef->ndef + ndef->cur_ptr,
212 ndef->cur_ptr += bytes_recv;
213 ndef->missing_bytes = ndef_size - bytes_recv;
215 /* Next prepare read to complete the NDEF */
216 ndef->ndef = g_try_realloc(ndef->ndef, ndef->cur_record_len
218 if (ndef->ndef == NULL) {
222 ndef->cur_record_len += ndef_size;
223 ndef->in_extra_read = FALSE;
228 /* Read remaining bytes */
229 bytes_recv = recv(client_fd, ndef->ndef + ndef->cur_ptr,
230 ndef->missing_bytes, MSG_DONTWAIT);
232 return handover_recv_error();
234 ndef->cur_ptr += bytes_recv;
235 ndef->missing_bytes -= bytes_recv;
237 /* Is the NDEF read complete ? */
238 if (ndef->missing_bytes)
239 return TRUE; /* more bytes to come... */
241 ndef->extra_ndef_count--;
242 ndef->in_extra_read = TRUE;
244 if (ndef->extra_ndef_count == 0) {
245 /* All the bytes are read so now, parse the frame */
246 handover_ndef_parse(client_fd, ndef);
250 /* Process the next NDEF */
254 near_error("Handover read NDEFs failed");
258 static near_bool_t handover_read_hr(int client_fd,
259 uint32_t adapter_idx, uint32_t target_idx, near_tag_io_cb cb)
263 struct hr_ndef *ndef;
267 ndef = g_hash_table_lookup(hr_ndef_hash, GINT_TO_POINTER(client_fd));
271 /* Read remaining bytes */
272 bytes_recv = recv(client_fd, ndef->ndef + ndef->cur_ptr,
273 ndef->missing_bytes, MSG_DONTWAIT);
275 return handover_recv_error();
277 ndef->cur_ptr += bytes_recv;
278 ndef->missing_bytes -= bytes_recv;
280 /* Is the ndef "Hr" read complete or should we loop */
281 if (ndef->missing_bytes)
285 * The first NDEF frame is read. We now should determine how many
286 * extra records follow the NDEF frame.
287 * We skip the first 6 bytes (Hr header) to jump on the first record
289 extra_ndefs = near_ndef_count_records(ndef->ndef + HR_HEADER_SIZE,
290 ndef->cur_record_len - HR_HEADER_SIZE,
291 RECORD_TYPE_WKT_ALTERNATIVE_CARRIER);
295 /* There's still some extra ndefs to read */
296 ndef->extra_ndef_count = extra_ndefs;
298 /* End of Handover message - now process extra records */
299 ndef->in_extra_read = TRUE;
300 ndef->cfg_record_state = TRUE;
305 near_error("Handover read failed");
309 static near_bool_t handover_read_initialize(int client_fd,
310 uint32_t adapter_idx, uint32_t target_idx, near_tag_io_cb cb)
313 struct hr_ndef *ndef;
317 /* Allocate the ndef structure */
318 ndef = g_try_malloc0(sizeof(struct hr_ndef));
322 /* Allocate and read frame header (6 bytes) */
323 ndef->ndef = g_try_malloc0(NDEF_HR_MSG_MIN_LENGTH);
324 if (ndef->ndef == NULL)
327 /* Initialize default values */
329 ndef->cur_record_len = -1;
330 ndef->adapter_idx = adapter_idx;
331 ndef->target_idx = target_idx;
333 ndef->cfg_record_state = FALSE;
335 g_hash_table_insert(hr_ndef_hash, GINT_TO_POINTER(client_fd), ndef);
337 /* Read header bytes (6) */
338 bytes_recv = recv(client_fd, ndef->ndef,
339 NDEF_HR_MSG_MIN_LENGTH, MSG_DONTWAIT);
341 return handover_recv_error();
343 /* Now, check the ndef payload size plus header bytes */
344 ndef->cur_record_len = near_ndef_record_length(ndef->ndef, bytes_recv);
345 if (ndef->cur_record_len < 0)
348 ndef->cur_ptr += bytes_recv;
349 ndef->missing_bytes = ndef->cur_record_len - bytes_recv;
351 DBG("Handover frame size is %d", ndef->cur_ptr);
353 /* Next prepare read to complete the read */
354 ndef->ndef = g_try_realloc(ndef->ndef, ndef->cur_record_len);
355 if (ndef->ndef == NULL)
367 * This function is a "dispatcher", to read Hr/Hs messages,
368 * and/or additional NDEF messages
370 static near_bool_t handover_read(int client_fd,
371 uint32_t adapter_idx, uint32_t target_idx,
374 struct hr_ndef *ndef;
376 ndef = g_hash_table_lookup(hr_ndef_hash, GINT_TO_POINTER(client_fd));
378 /* First call: allocate and read header bytes*/
379 return handover_read_initialize(client_fd, adapter_idx,
383 if (ndef->cfg_record_state == TRUE) {
384 return handover_read_cfg_records(client_fd, adapter_idx,
388 return handover_read_hr(client_fd, adapter_idx, target_idx, cb);
391 static void free_hr_push_client(struct hr_push_client *client, int status)
395 handover_close(client->fd, 0);
399 client->cb(client->adapter_idx, client->target_idx, status);
401 if (client->watch > 0)
402 g_source_remove(client->watch);
407 static gboolean handover_push_event(GIOChannel *channel,
408 GIOCondition condition, gpointer data)
411 struct hr_push_client *client = (struct hr_push_client *)data;
413 DBG("condition 0x%x", condition);
415 if (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
416 near_error("Error with Handover client");
418 free_hr_push_client(client, -EIO);
423 ret = handover_read(client->fd,
424 client->adapter_idx, client->target_idx,
428 free_hr_push_client(client, 0);
433 static int handover_push(int client_fd,
434 uint32_t adapter_idx, uint32_t target_idx,
435 struct near_ndef_message *ndef,
436 near_device_io_cb cb)
439 struct hr_push_client *client;
444 client = g_try_malloc0(sizeof(struct hr_push_client));
448 channel = g_io_channel_unix_new(client_fd);
449 g_io_channel_set_close_on_unref(channel, TRUE);
451 client->fd = client_fd;
452 client->adapter_idx = adapter_idx;
453 client->target_idx = target_idx;
455 client->watch = g_io_add_watch(channel,
456 G_IO_IN | G_IO_HUP | G_IO_NVAL |
457 G_IO_ERR, handover_push_event,
460 err = send(client_fd, ndef->data, ndef->length, MSG_DONTWAIT);
462 free_hr_push_client(client, err);
467 struct near_p2p_driver handover_driver = {
469 .service_name = NEAR_DEVICE_SN_HANDOVER,
470 .read = handover_read,
471 .push = handover_push,
472 .close = handover_close,
475 int handover_init(void)
477 hr_ndef_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal,
480 return near_p2p_register(&handover_driver);
483 void handover_exit(void)
485 near_p2p_unregister(&handover_driver);
487 g_hash_table_destroy(hr_ndef_hash);