3 * neard - Near Field Communication manager
5 * Copyright 2007, 2008 Johannes Berg <johannes@sipsolutions.net>
6 * Copyright (C) 2011 Instituto Nokia de Tecnologia
7 * Copyright (C) 2011 Intel Corporation. All rights reserved.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
32 #include <netlink/netlink.h>
33 #include <netlink/genl/genl.h>
34 #include <netlink/genl/family.h>
35 #include <netlink/genl/ctrl.h>
39 #ifdef NEED_LIBNL_COMPAT
40 #define nl_sock nl_handle
42 static inline struct nl_handle *nl_socket_alloc(void)
44 return nl_handle_alloc();
47 static inline void nl_socket_free(struct nl_sock *h)
52 #define NLE_MISSING_ATTR 14
54 static inline void __nl_perror(int error, const char *s)
58 #define nl_perror __nl_perror
62 struct nl_sock *cmd_sock;
63 struct nl_sock *event_sock;
68 static struct nlnfc_state *nfc_state;
69 static GIOChannel *netlink_channel = NULL;
71 static int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err,
83 static int finish_handler(struct nl_msg *msg, void *arg)
94 static int ack_handler(struct nl_msg *msg, void *arg)
105 static int nl_send_msg(struct nl_sock *sock, struct nl_msg *msg,
106 int (*rx_handler)(struct nl_msg *, void *),
114 cb = nl_cb_alloc(NL_CB_DEFAULT);
118 err = nl_send_auto_complete(sock, msg);
121 near_error("%s", strerror(err));
128 nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err);
129 nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &done);
130 nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &done);
132 if (rx_handler != NULL)
133 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, rx_handler, data);
135 while (err == 0 && done == 0)
136 nl_recvmsgs(sock, cb);
144 static int get_devices_handler(struct nl_msg *n, void *arg)
146 struct nlmsghdr *nlh = nlmsg_hdr(n);
147 struct nlattr *attrs[NFC_ATTR_MAX + 1];
149 uint32_t idx, protocols;
154 genlmsg_parse(nlh, 0, attrs, NFC_ATTR_MAX, NULL);
156 if (attrs[NFC_ATTR_DEVICE_INDEX] == NULL ||
157 attrs[NFC_ATTR_DEVICE_NAME] == NULL ||
158 attrs[NFC_ATTR_PROTOCOLS] == NULL) {
159 nl_perror(NLE_MISSING_ATTR, "NFC_CMD_GET_DEVICE");
164 idx = nla_get_u32(attrs[NFC_ATTR_DEVICE_INDEX]);
165 name = nla_get_string(attrs[NFC_ATTR_DEVICE_NAME]);
166 protocols = nla_get_u32(attrs[NFC_ATTR_PROTOCOLS]);
168 if (attrs[NFC_ATTR_DEVICE_POWERED] == NULL)
171 powered = nla_get_u8(attrs[NFC_ATTR_DEVICE_POWERED]);
173 __near_manager_adapter_add(idx, name, protocols, powered);
178 int __near_netlink_get_adapters(void)
186 if (nfc_state == NULL || nfc_state->nfc_id < 0)
193 hdr = genlmsg_put(msg, NL_AUTO_PID, NL_AUTO_SEQ, nfc_state->nfc_id, 0,
194 NLM_F_DUMP, NFC_CMD_GET_DEVICE, NFC_GENL_VERSION);
200 err = nl_send_msg(nfc_state->cmd_sock, msg, get_devices_handler, NULL);
208 int __near_netlink_start_poll(int idx,
209 uint32_t im_protocols, uint32_t tm_protocols)
215 DBG("IM protos 0x%x TM protos 0x%x", im_protocols, tm_protocols);
221 hdr = genlmsg_put(msg, NL_AUTO_PID, NL_AUTO_SEQ, nfc_state->nfc_id, 0,
222 NLM_F_REQUEST, NFC_CMD_START_POLL, NFC_GENL_VERSION);
225 goto nla_put_failure;
230 NLA_PUT_U32(msg, NFC_ATTR_DEVICE_INDEX, idx);
231 if (im_protocols != 0) {
232 NLA_PUT_U32(msg, NFC_ATTR_IM_PROTOCOLS, im_protocols);
233 NLA_PUT_U32(msg, NFC_ATTR_PROTOCOLS, im_protocols);
235 if (tm_protocols != 0)
236 NLA_PUT_U32(msg, NFC_ATTR_TM_PROTOCOLS, tm_protocols);
238 err = nl_send_msg(nfc_state->cmd_sock, msg, NULL, NULL);
247 int __near_netlink_stop_poll(int idx)
259 hdr = genlmsg_put(msg, NL_AUTO_PID, NL_AUTO_SEQ, nfc_state->nfc_id, 0,
260 NLM_F_REQUEST, NFC_CMD_STOP_POLL, NFC_GENL_VERSION);
263 goto nla_put_failure;
268 NLA_PUT_U32(msg, NFC_ATTR_DEVICE_INDEX, idx);
270 err = nl_send_msg(nfc_state->cmd_sock, msg, NULL, NULL);
278 int __near_netlink_dep_link_up(uint32_t idx, uint32_t target_idx,
279 uint8_t comm_mode, uint8_t rf_mode)
291 hdr = genlmsg_put(msg, NL_AUTO_PID, NL_AUTO_SEQ, nfc_state->nfc_id, 0,
292 NLM_F_REQUEST, NFC_CMD_DEP_LINK_UP, NFC_GENL_VERSION);
295 goto nla_put_failure;
300 NLA_PUT_U32(msg, NFC_ATTR_DEVICE_INDEX, idx);
301 NLA_PUT_U32(msg, NFC_ATTR_TARGET_INDEX, target_idx);
302 NLA_PUT_U8(msg, NFC_ATTR_COMM_MODE, comm_mode);
303 NLA_PUT_U8(msg, NFC_ATTR_RF_MODE, rf_mode);
305 err = nl_send_msg(nfc_state->cmd_sock, msg, NULL, NULL);
313 int __near_netlink_dep_link_down(uint32_t idx)
325 hdr = genlmsg_put(msg, NL_AUTO_PID, NL_AUTO_SEQ, nfc_state->nfc_id, 0,
326 NLM_F_REQUEST, NFC_CMD_DEP_LINK_DOWN, NFC_GENL_VERSION);
329 goto nla_put_failure;
334 NLA_PUT_U32(msg, NFC_ATTR_DEVICE_INDEX, idx);
336 err = nl_send_msg(nfc_state->cmd_sock, msg, NULL, NULL);
344 int __near_netlink_adapter_enable(int idx, near_bool_t enable)
358 cmd = NFC_CMD_DEV_UP;
360 cmd = NFC_CMD_DEV_DOWN;
362 hdr = genlmsg_put(msg, NL_AUTO_PID, NL_AUTO_SEQ, nfc_state->nfc_id, 0,
363 NLM_F_REQUEST, cmd, NFC_GENL_VERSION);
366 goto nla_put_failure;
371 NLA_PUT_U32(msg, NFC_ATTR_DEVICE_INDEX, idx);
373 err = nl_send_msg(nfc_state->cmd_sock, msg, NULL, NULL);
382 static int no_seq_check(struct nl_msg *n, void *arg)
389 static int nfc_netlink_event_adapter(struct genlmsghdr *gnlh, near_bool_t add)
391 struct nlattr *attrs[NFC_ATTR_MAX + 1];
396 nla_parse(attrs, NFC_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
397 genlmsg_attrlen(gnlh, 0), NULL);
398 if (attrs[NFC_ATTR_DEVICE_INDEX] == NULL) {
399 near_error("Missing device index");
403 idx = nla_get_u32(attrs[NFC_ATTR_DEVICE_INDEX]);
406 (attrs[NFC_ATTR_DEVICE_NAME] == NULL ||
407 attrs[NFC_ATTR_PROTOCOLS] == NULL)) {
408 near_error("Missing attributes");
417 name = nla_get_string(attrs[NFC_ATTR_DEVICE_NAME]);
418 protocols = nla_get_u32(attrs[NFC_ATTR_PROTOCOLS]);
419 if (attrs[NFC_ATTR_DEVICE_POWERED] == NULL)
422 powered = nla_get_u8(attrs[NFC_ATTR_DEVICE_POWERED]);
424 return __near_manager_adapter_add(idx, name,
427 __near_manager_adapter_remove(idx);
433 static int get_targets_handler(struct nl_msg *n, void *arg)
435 struct nlmsghdr *nlh = nlmsg_hdr(n);
436 struct nlattr *attrs[NFC_ATTR_MAX + 1];
437 uint32_t adapter_idx, target_idx, protocols;
438 uint16_t sens_res = 0;
440 uint8_t nfcid[NFC_MAX_NFCID1_LEN], nfcid_len;
444 genlmsg_parse(nlh, 0, attrs, NFC_ATTR_MAX, NULL);
446 adapter_idx = *((uint32_t *)arg);
447 target_idx = nla_get_u32(attrs[NFC_ATTR_TARGET_INDEX]);
448 protocols = nla_get_u32(attrs[NFC_ATTR_PROTOCOLS]);
450 if (attrs[NFC_ATTR_TARGET_SENS_RES] != NULL)
452 nla_get_u16(attrs[NFC_ATTR_TARGET_SENS_RES]);
454 if (attrs[NFC_ATTR_TARGET_SEL_RES] != NULL)
456 nla_get_u16(attrs[NFC_ATTR_TARGET_SEL_RES]);
458 if (attrs[NFC_ATTR_TARGET_NFCID1] != NULL) {
459 nfcid_len = nla_len(attrs[NFC_ATTR_TARGET_NFCID1]);
460 if (nfcid_len <= NFC_MAX_NFCID1_LEN)
461 memcpy(nfcid, nla_data(attrs[NFC_ATTR_TARGET_NFCID1]),
467 DBG("target idx %d proto 0x%x sens_res 0x%x sel_res 0x%x NFCID len %d",
468 target_idx, protocols, sens_res, sel_res, nfcid_len);
470 __near_adapter_add_target(adapter_idx, target_idx, protocols,
471 sens_res, sel_res, nfcid, nfcid_len);
476 static int nfc_netlink_event_targets_found(struct genlmsghdr *gnlh)
478 struct nlattr *attr[NFC_ATTR_MAX + 1];
482 uint32_t adapter_idx;
486 nla_parse(attr, NFC_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
487 genlmsg_attrlen(gnlh, 0), NULL);
488 if (attr[NFC_ATTR_DEVICE_INDEX] == NULL)
491 adapter_idx = nla_get_u32(attr[NFC_ATTR_DEVICE_INDEX]);
493 DBG("adapter %d", adapter_idx);
499 hdr = genlmsg_put(msg, NL_AUTO_PID, NL_AUTO_SEQ, nfc_state->nfc_id, 0,
500 NLM_F_DUMP, NFC_CMD_GET_TARGET, NFC_GENL_VERSION);
503 goto nla_put_failure;
508 NLA_PUT_U32(msg, NFC_ATTR_DEVICE_INDEX, adapter_idx);
510 err = nl_send_msg(nfc_state->cmd_sock, msg,
511 get_targets_handler, &adapter_idx);
519 static int nfc_netlink_event_target_lost(struct genlmsghdr *gnlh)
521 struct nlattr *attr[NFC_ATTR_MAX + 1];
522 uint32_t adapter_idx, target_idx;
526 nla_parse(attr, NFC_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
527 genlmsg_attrlen(gnlh, 0), NULL);
529 if (attr[NFC_ATTR_DEVICE_INDEX] == NULL)
532 if (attr[NFC_ATTR_TARGET_INDEX] == NULL)
535 adapter_idx = nla_get_u32(attr[NFC_ATTR_DEVICE_INDEX]);
536 target_idx = nla_get_u32(attr[NFC_ATTR_TARGET_INDEX]);
538 DBG("adapter %d target %d", adapter_idx, target_idx);
540 return __near_adapter_remove_target(adapter_idx, target_idx);
543 static int nfc_netlink_event_dep_up(struct genlmsghdr *gnlh)
545 struct nlattr *attrs[NFC_ATTR_MAX + 1];
546 uint32_t idx, target_idx = 0;
551 nla_parse(attrs, NFC_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
552 genlmsg_attrlen(gnlh, 0), NULL);
553 if (attrs[NFC_ATTR_DEVICE_INDEX] == NULL) {
554 near_error("Missing device index");
558 if (attrs[NFC_ATTR_COMM_MODE] == NULL ||
559 attrs[NFC_ATTR_RF_MODE] == NULL) {
560 near_error("Missing rf or comm modes");
564 idx = nla_get_u32(attrs[NFC_ATTR_DEVICE_INDEX]);
565 rf_mode = nla_get_u8(attrs[NFC_ATTR_RF_MODE]);
567 if (rf_mode == NFC_RF_INITIATOR) {
568 if (attrs[NFC_ATTR_TARGET_INDEX] == NULL) {
569 near_error("Missing target index");
573 target_idx = nla_get_u32(attrs[NFC_ATTR_TARGET_INDEX]);
575 DBG("%d %d", idx, target_idx);
577 return __near_adapter_set_dep_state(idx, TRUE);
583 static int nfc_netlink_event_dep_down(struct genlmsghdr *gnlh)
585 struct nlattr *attrs[NFC_ATTR_MAX + 1];
590 nla_parse(attrs, NFC_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
591 genlmsg_attrlen(gnlh, 0), NULL);
592 if (attrs[NFC_ATTR_DEVICE_INDEX] == NULL) {
593 near_error("Missing device index");
597 idx = nla_get_u32(attrs[NFC_ATTR_DEVICE_INDEX]);
599 __near_adapter_set_dep_state(idx, FALSE);
604 static int nfc_netlink_event_tm_activated(struct genlmsghdr *gnlh)
606 struct nlattr *attrs[NFC_ATTR_MAX + 1];
611 nla_parse(attrs, NFC_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
612 genlmsg_attrlen(gnlh, 0), NULL);
613 if (attrs[NFC_ATTR_DEVICE_INDEX] == NULL) {
614 near_error("Missing device index");
618 idx = nla_get_u32(attrs[NFC_ATTR_DEVICE_INDEX]);
622 return __near_adapter_add_device(idx, NULL, 0);
625 static int nfc_netlink_event_tm_deactivated(struct genlmsghdr *gnlh)
627 struct nlattr *attrs[NFC_ATTR_MAX + 1];
632 nla_parse(attrs, NFC_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
633 genlmsg_attrlen(gnlh, 0), NULL);
634 if (attrs[NFC_ATTR_DEVICE_INDEX] == NULL) {
635 near_error("Missing device index");
639 idx = nla_get_u32(attrs[NFC_ATTR_DEVICE_INDEX]);
643 return __near_adapter_remove_device(idx);
646 static int nfc_netlink_event(struct nl_msg *n, void *arg)
648 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(n));
650 DBG("event 0x%x", gnlh->cmd);
653 case NFC_EVENT_TARGETS_FOUND:
654 DBG("Targets found");
655 nfc_netlink_event_targets_found(gnlh);
657 case NFC_EVENT_TARGET_LOST:
659 nfc_netlink_event_target_lost(gnlh);
661 case NFC_EVENT_DEVICE_ADDED:
662 DBG("Adapter added");
663 nfc_netlink_event_adapter(gnlh, TRUE);
666 case NFC_EVENT_DEVICE_REMOVED:
667 DBG("Adapter removed");
668 nfc_netlink_event_adapter(gnlh, FALSE);
671 case NFC_CMD_DEP_LINK_UP:
672 DBG("DEP link is up");
673 nfc_netlink_event_dep_up(gnlh);
676 case NFC_CMD_DEP_LINK_DOWN:
677 DBG("DEP link is down");
678 nfc_netlink_event_dep_down(gnlh);
681 case NFC_EVENT_TM_ACTIVATED:
682 DBG("Target mode activated");
683 nfc_netlink_event_tm_activated(gnlh);
686 case NFC_EVENT_TM_DEACTIVATED:
687 DBG("Target mode deactivated");
688 nfc_netlink_event_tm_deactivated(gnlh);
696 static gboolean __nfc_netlink_event(GIOChannel *channel,
697 GIOCondition cond, gpointer data)
700 struct nlnfc_state *state = data;
702 if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR))
705 cb = nl_cb_alloc(NL_CB_VERBOSE);
709 nl_cb_set(cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, no_seq_check, NULL);
710 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, nfc_netlink_event, data);
712 nl_recvmsgs(state->event_sock, cb);
719 static int nfc_event_listener(struct nlnfc_state *state)
723 sock = nl_socket_get_fd(state->event_sock);
724 netlink_channel = g_io_channel_unix_new(sock);
725 g_io_channel_set_close_on_unref(netlink_channel, TRUE);
727 g_io_channel_set_encoding(netlink_channel, NULL, NULL);
728 g_io_channel_set_buffered(netlink_channel, FALSE);
730 g_io_add_watch(netlink_channel,
731 G_IO_IN | G_IO_NVAL | G_IO_HUP | G_IO_ERR,
732 __nfc_netlink_event, state);
737 struct handler_args {
742 static int family_handler(struct nl_msg *msg, void *arg)
744 struct handler_args *grp = arg;
745 struct nlattr *tb[CTRL_ATTR_MAX + 1];
746 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
747 struct nlattr *mcgrp;
752 nla_parse(tb, CTRL_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
753 genlmsg_attrlen(gnlh, 0), NULL);
755 if (!tb[CTRL_ATTR_MCAST_GROUPS])
758 nla_for_each_nested(mcgrp, tb[CTRL_ATTR_MCAST_GROUPS], rem_mcgrp) {
759 struct nlattr *tb_mcgrp[CTRL_ATTR_MCAST_GRP_MAX + 1];
761 nla_parse(tb_mcgrp, CTRL_ATTR_MCAST_GRP_MAX,
762 nla_data(mcgrp), nla_len(mcgrp), NULL);
764 if (!tb_mcgrp[CTRL_ATTR_MCAST_GRP_NAME] ||
765 !tb_mcgrp[CTRL_ATTR_MCAST_GRP_ID])
767 if (strncmp(nla_data(tb_mcgrp[CTRL_ATTR_MCAST_GRP_NAME]),
768 grp->group, nla_len(tb_mcgrp[CTRL_ATTR_MCAST_GRP_NAME])))
770 grp->id = nla_get_u32(tb_mcgrp[CTRL_ATTR_MCAST_GRP_ID]);
777 static int nl_get_multicast_id(struct nl_sock *sock, const char *family,
782 struct handler_args grp = {
793 ctrlid = genl_ctrl_resolve(sock, "nlctrl");
795 genlmsg_put(msg, 0, 0, ctrlid, 0,
796 0, CTRL_CMD_GETFAMILY, 0);
800 NLA_PUT_STRING(msg, CTRL_ATTR_FAMILY_NAME, family);
802 err = nl_send_msg(sock, msg, family_handler, &grp);
804 goto nla_put_failure;
806 DBG("multicast id %d", grp.id);
816 int __near_netlink_init(void)
822 nfc_state = g_try_malloc0(sizeof(struct nlnfc_state));
823 if (nfc_state == NULL)
826 nfc_state->cmd_sock = nl_socket_alloc();
827 if (nfc_state->cmd_sock == NULL) {
828 near_error("Failed to allocate NFC command netlink socket");
833 nfc_state->event_sock = nl_socket_alloc();
834 if (nfc_state->event_sock == NULL) {
835 near_error("Failed to allocate NFC event netlink socket");
837 goto handle_cmd_destroy;
840 if (genl_connect(nfc_state->cmd_sock)) {
841 near_error("Failed to connect to generic netlink");
843 goto handle_event_destroy;
846 if (genl_connect(nfc_state->event_sock)) {
847 near_error("Failed to connect to generic netlink");
849 goto handle_event_destroy;
852 nfc_state->nfc_id = genl_ctrl_resolve(nfc_state->cmd_sock, "nfc");
853 if (nfc_state->nfc_id < 0) {
854 near_error("Unable to find NFC netlink family");
856 goto handle_event_destroy;
859 nfc_state->mcid = nl_get_multicast_id(nfc_state->cmd_sock, NFC_GENL_NAME,
860 NFC_GENL_MCAST_EVENT_NAME);
861 if (nfc_state->mcid <= 0) {
862 near_error("Wrong mcast id %d", nfc_state->mcid);
863 err = nfc_state->mcid;
864 goto handle_event_destroy;
867 err = nl_socket_add_membership(nfc_state->event_sock, nfc_state->mcid);
869 near_error("Error adding nl event socket to membership");
870 goto handle_event_destroy;
873 return nfc_event_listener(nfc_state);
875 handle_event_destroy:
876 nl_socket_free(nfc_state->event_sock);
879 nl_socket_free(nfc_state->cmd_sock);
886 near_error("netlink init failed");
891 void __near_netlink_cleanup(void)
893 if (netlink_channel != NULL) {
894 g_io_channel_shutdown(netlink_channel, TRUE, NULL);
895 g_io_channel_unref(netlink_channel);
897 netlink_channel = NULL;
900 if (nfc_state == NULL)
903 nl_socket_free(nfc_state->cmd_sock);
904 nl_socket_free(nfc_state->event_sock);