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 struct send_msg_data {
74 int (*finish_handler)(struct nl_msg *, void *);
77 static int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err,
89 static int __finish_handler(struct nl_msg *msg, void *arg)
91 struct send_msg_data *data = arg;
96 if (data->finish_handler)
97 data->finish_handler(msg, data->data);
104 static int ack_handler(struct nl_msg *msg, void *arg)
115 static int __nl_send_msg(struct nl_sock *sock, struct nl_msg *msg,
116 int (*rx_handler)(struct nl_msg *, void *),
117 int (*finish_handler)(struct nl_msg *, void *),
122 struct send_msg_data send_data;
126 cb = nl_cb_alloc(NL_CB_DEFAULT);
130 err = nl_send_auto_complete(sock, msg);
133 near_error("%s", strerror(err));
139 send_data.done = &done;
140 send_data.data = data;
141 send_data.finish_handler = finish_handler;
143 nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, __finish_handler, &send_data);
144 nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err);
145 nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &done);
148 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, rx_handler, data);
150 while (err == 0 && done == 0)
151 nl_recvmsgs(sock, cb);
158 static inline int nl_send_msg(struct nl_sock *sock, struct nl_msg *msg,
159 int (*rx_handler)(struct nl_msg *, void *),
162 return __nl_send_msg(sock, msg, rx_handler, NULL, data);
165 static int get_devices_handler(struct nl_msg *n, void *arg)
167 struct nlmsghdr *nlh = nlmsg_hdr(n);
168 struct nlattr *attrs[NFC_ATTR_MAX + 1];
170 uint32_t idx, protocols;
175 genlmsg_parse(nlh, 0, attrs, NFC_ATTR_MAX, NULL);
177 if (!attrs[NFC_ATTR_DEVICE_INDEX] ||
178 !attrs[NFC_ATTR_DEVICE_NAME] ||
179 !attrs[NFC_ATTR_PROTOCOLS]) {
180 nl_perror(NLE_MISSING_ATTR, "NFC_CMD_GET_DEVICE");
184 idx = nla_get_u32(attrs[NFC_ATTR_DEVICE_INDEX]);
185 name = nla_get_string(attrs[NFC_ATTR_DEVICE_NAME]);
186 protocols = nla_get_u32(attrs[NFC_ATTR_PROTOCOLS]);
188 if (!attrs[NFC_ATTR_DEVICE_POWERED])
191 powered = nla_get_u8(attrs[NFC_ATTR_DEVICE_POWERED]);
193 __near_manager_adapter_add(idx, name, protocols, powered);
198 int __near_netlink_get_adapters(void)
206 if (!nfc_state || nfc_state->nfc_id < 0)
213 hdr = genlmsg_put(msg, NL_AUTO_PID, NL_AUTO_SEQ, nfc_state->nfc_id, 0,
214 NLM_F_DUMP, NFC_CMD_GET_DEVICE, NFC_GENL_VERSION);
220 err = nl_send_msg(nfc_state->cmd_sock, msg, get_devices_handler, NULL);
228 int __near_netlink_start_poll(int idx,
229 uint32_t im_protocols, uint32_t tm_protocols)
235 DBG("IM protos 0x%x TM protos 0x%x", im_protocols, tm_protocols);
241 hdr = genlmsg_put(msg, NL_AUTO_PID, NL_AUTO_SEQ, nfc_state->nfc_id, 0,
242 NLM_F_REQUEST, NFC_CMD_START_POLL, NFC_GENL_VERSION);
245 goto nla_put_failure;
250 NLA_PUT_U32(msg, NFC_ATTR_DEVICE_INDEX, idx);
251 if (im_protocols != 0) {
252 NLA_PUT_U32(msg, NFC_ATTR_IM_PROTOCOLS, im_protocols);
253 NLA_PUT_U32(msg, NFC_ATTR_PROTOCOLS, im_protocols);
255 if (tm_protocols != 0)
256 NLA_PUT_U32(msg, NFC_ATTR_TM_PROTOCOLS, tm_protocols);
258 err = nl_send_msg(nfc_state->cmd_sock, msg, NULL, NULL);
266 int __near_netlink_stop_poll(int idx)
278 hdr = genlmsg_put(msg, NL_AUTO_PID, NL_AUTO_SEQ, nfc_state->nfc_id, 0,
279 NLM_F_REQUEST, NFC_CMD_STOP_POLL, NFC_GENL_VERSION);
282 goto nla_put_failure;
287 NLA_PUT_U32(msg, NFC_ATTR_DEVICE_INDEX, idx);
289 err = nl_send_msg(nfc_state->cmd_sock, msg, NULL, NULL);
297 int __near_netlink_dep_link_up(uint32_t idx, uint32_t target_idx,
298 uint8_t comm_mode, uint8_t rf_mode)
310 hdr = genlmsg_put(msg, NL_AUTO_PID, NL_AUTO_SEQ, nfc_state->nfc_id, 0,
311 NLM_F_REQUEST, NFC_CMD_DEP_LINK_UP, NFC_GENL_VERSION);
314 goto nla_put_failure;
319 NLA_PUT_U32(msg, NFC_ATTR_DEVICE_INDEX, idx);
320 NLA_PUT_U32(msg, NFC_ATTR_TARGET_INDEX, target_idx);
321 NLA_PUT_U8(msg, NFC_ATTR_COMM_MODE, comm_mode);
322 NLA_PUT_U8(msg, NFC_ATTR_RF_MODE, rf_mode);
324 err = nl_send_msg(nfc_state->cmd_sock, msg, NULL, NULL);
332 int __near_netlink_dep_link_down(uint32_t idx)
344 hdr = genlmsg_put(msg, NL_AUTO_PID, NL_AUTO_SEQ, nfc_state->nfc_id, 0,
345 NLM_F_REQUEST, NFC_CMD_DEP_LINK_DOWN, NFC_GENL_VERSION);
348 goto nla_put_failure;
353 NLA_PUT_U32(msg, NFC_ATTR_DEVICE_INDEX, idx);
355 err = nl_send_msg(nfc_state->cmd_sock, msg, NULL, NULL);
363 int __near_netlink_adapter_enable(int idx, bool enable)
377 cmd = NFC_CMD_DEV_UP;
379 cmd = NFC_CMD_DEV_DOWN;
381 hdr = genlmsg_put(msg, NL_AUTO_PID, NL_AUTO_SEQ, nfc_state->nfc_id, 0,
382 NLM_F_REQUEST, cmd, NFC_GENL_VERSION);
385 goto nla_put_failure;
390 NLA_PUT_U32(msg, NFC_ATTR_DEVICE_INDEX, idx);
392 err = nl_send_msg(nfc_state->cmd_sock, msg, NULL, NULL);
401 static int no_seq_check(struct nl_msg *n, void *arg)
408 static int nfc_netlink_event_adapter(struct genlmsghdr *gnlh, bool add)
410 struct nlattr *attrs[NFC_ATTR_MAX + 1];
415 nla_parse(attrs, NFC_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
416 genlmsg_attrlen(gnlh, 0), NULL);
417 if (!attrs[NFC_ATTR_DEVICE_INDEX]) {
418 near_error("Missing device index");
422 idx = nla_get_u32(attrs[NFC_ATTR_DEVICE_INDEX]);
425 (!attrs[NFC_ATTR_DEVICE_NAME] ||
426 !attrs[NFC_ATTR_PROTOCOLS])) {
427 near_error("Missing attributes");
436 name = nla_get_string(attrs[NFC_ATTR_DEVICE_NAME]);
437 protocols = nla_get_u32(attrs[NFC_ATTR_PROTOCOLS]);
438 if (!attrs[NFC_ATTR_DEVICE_POWERED])
441 powered = nla_get_u8(attrs[NFC_ATTR_DEVICE_POWERED]);
443 return __near_manager_adapter_add(idx, name,
446 __near_manager_adapter_remove(idx);
452 static int get_targets_handler(struct nl_msg *n, void *arg)
454 struct nlmsghdr *nlh = nlmsg_hdr(n);
455 struct nlattr *attrs[NFC_ATTR_MAX + 1];
456 uint32_t adapter_idx, target_idx, protocols;
457 uint16_t sens_res = 0;
459 uint8_t nfcid[NFC_MAX_NFCID1_LEN], nfcid_len;
463 genlmsg_parse(nlh, 0, attrs, NFC_ATTR_MAX, NULL);
465 adapter_idx = *((uint32_t *)arg);
466 target_idx = nla_get_u32(attrs[NFC_ATTR_TARGET_INDEX]);
467 protocols = nla_get_u32(attrs[NFC_ATTR_PROTOCOLS]);
469 if (attrs[NFC_ATTR_TARGET_SENS_RES])
471 nla_get_u16(attrs[NFC_ATTR_TARGET_SENS_RES]);
473 if (attrs[NFC_ATTR_TARGET_SEL_RES])
475 nla_get_u16(attrs[NFC_ATTR_TARGET_SEL_RES]);
477 if (attrs[NFC_ATTR_TARGET_NFCID1]) {
478 nfcid_len = nla_len(attrs[NFC_ATTR_TARGET_NFCID1]);
479 if (nfcid_len <= NFC_MAX_NFCID1_LEN)
480 memcpy(nfcid, nla_data(attrs[NFC_ATTR_TARGET_NFCID1]),
486 DBG("target idx %d proto 0x%x sens_res 0x%x sel_res 0x%x NFCID len %d",
487 target_idx, protocols, sens_res, sel_res, nfcid_len);
489 __near_adapter_add_target(adapter_idx, target_idx, protocols,
490 sens_res, sel_res, nfcid, nfcid_len);
495 static int get_targets_finish_handler(struct nl_msg *n, void *arg)
497 uint32_t adapter_idx;
501 adapter_idx = *((uint32_t *)arg);
503 return __near_adapter_get_targets_done(adapter_idx);
506 static int nfc_netlink_event_targets_found(struct genlmsghdr *gnlh)
508 struct nlattr *attr[NFC_ATTR_MAX + 1];
512 uint32_t adapter_idx;
516 nla_parse(attr, NFC_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
517 genlmsg_attrlen(gnlh, 0), NULL);
518 if (!attr[NFC_ATTR_DEVICE_INDEX])
521 adapter_idx = nla_get_u32(attr[NFC_ATTR_DEVICE_INDEX]);
523 DBG("adapter %d", adapter_idx);
529 hdr = genlmsg_put(msg, NL_AUTO_PID, NL_AUTO_SEQ, nfc_state->nfc_id, 0,
530 NLM_F_DUMP, NFC_CMD_GET_TARGET, NFC_GENL_VERSION);
533 goto nla_put_failure;
538 NLA_PUT_U32(msg, NFC_ATTR_DEVICE_INDEX, adapter_idx);
540 err = __nl_send_msg(nfc_state->cmd_sock, msg,
541 get_targets_handler, get_targets_finish_handler,
550 static int nfc_netlink_event_target_lost(struct genlmsghdr *gnlh)
552 struct nlattr *attr[NFC_ATTR_MAX + 1];
553 uint32_t adapter_idx, target_idx;
557 nla_parse(attr, NFC_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
558 genlmsg_attrlen(gnlh, 0), NULL);
560 if (!attr[NFC_ATTR_DEVICE_INDEX])
563 if (!attr[NFC_ATTR_TARGET_INDEX])
566 adapter_idx = nla_get_u32(attr[NFC_ATTR_DEVICE_INDEX]);
567 target_idx = nla_get_u32(attr[NFC_ATTR_TARGET_INDEX]);
569 DBG("adapter %d target %d", adapter_idx, target_idx);
571 return __near_adapter_remove_target(adapter_idx, target_idx);
574 static int nfc_netlink_event_dep_up(struct genlmsghdr *gnlh)
576 struct nlattr *attrs[NFC_ATTR_MAX + 1];
577 uint32_t idx, target_idx = 0;
582 nla_parse(attrs, NFC_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
583 genlmsg_attrlen(gnlh, 0), NULL);
584 if (!attrs[NFC_ATTR_DEVICE_INDEX]) {
585 near_error("Missing device index");
589 if (!attrs[NFC_ATTR_COMM_MODE] ||
590 !attrs[NFC_ATTR_RF_MODE]) {
591 near_error("Missing rf or comm modes");
595 idx = nla_get_u32(attrs[NFC_ATTR_DEVICE_INDEX]);
596 rf_mode = nla_get_u8(attrs[NFC_ATTR_RF_MODE]);
598 if (rf_mode == NFC_RF_INITIATOR) {
599 if (!attrs[NFC_ATTR_TARGET_INDEX]) {
600 near_error("Missing target index");
604 target_idx = nla_get_u32(attrs[NFC_ATTR_TARGET_INDEX]);
606 DBG("%d %d", idx, target_idx);
608 return __near_adapter_set_dep_state(idx, true);
614 static int nfc_netlink_event_dep_down(struct genlmsghdr *gnlh)
616 struct nlattr *attrs[NFC_ATTR_MAX + 1];
621 nla_parse(attrs, NFC_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
622 genlmsg_attrlen(gnlh, 0), NULL);
623 if (!attrs[NFC_ATTR_DEVICE_INDEX]) {
624 near_error("Missing device index");
628 idx = nla_get_u32(attrs[NFC_ATTR_DEVICE_INDEX]);
630 __near_adapter_set_dep_state(idx, false);
635 static int nfc_netlink_event_tm_activated(struct genlmsghdr *gnlh)
637 struct nlattr *attrs[NFC_ATTR_MAX + 1];
642 nla_parse(attrs, NFC_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
643 genlmsg_attrlen(gnlh, 0), NULL);
644 if (!attrs[NFC_ATTR_DEVICE_INDEX]) {
645 near_error("Missing device index");
649 idx = nla_get_u32(attrs[NFC_ATTR_DEVICE_INDEX]);
653 return __near_adapter_add_device(idx, NULL, 0);
656 static int nfc_netlink_event_tm_deactivated(struct genlmsghdr *gnlh)
658 struct nlattr *attrs[NFC_ATTR_MAX + 1];
663 nla_parse(attrs, NFC_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
664 genlmsg_attrlen(gnlh, 0), NULL);
665 if (!attrs[NFC_ATTR_DEVICE_INDEX]) {
666 near_error("Missing device index");
670 idx = nla_get_u32(attrs[NFC_ATTR_DEVICE_INDEX]);
674 return __near_adapter_remove_device(idx);
677 static int nfc_netlink_event(struct nl_msg *n, void *arg)
679 struct sockaddr_nl *src = nlmsg_get_src(n);
680 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(n));
682 DBG("event 0x%x", gnlh->cmd);
685 near_error("WARNING: Wrong netlink message sender %d",
691 case NFC_EVENT_TARGETS_FOUND:
692 DBG("Targets found");
693 nfc_netlink_event_targets_found(gnlh);
695 case NFC_EVENT_TARGET_LOST:
697 nfc_netlink_event_target_lost(gnlh);
699 case NFC_EVENT_DEVICE_ADDED:
700 DBG("Adapter added");
701 nfc_netlink_event_adapter(gnlh, true);
704 case NFC_EVENT_DEVICE_REMOVED:
705 DBG("Adapter removed");
706 nfc_netlink_event_adapter(gnlh, false);
709 case NFC_CMD_DEP_LINK_UP:
710 DBG("DEP link is up");
711 nfc_netlink_event_dep_up(gnlh);
714 case NFC_CMD_DEP_LINK_DOWN:
715 DBG("DEP link is down");
716 nfc_netlink_event_dep_down(gnlh);
719 case NFC_EVENT_TM_ACTIVATED:
720 DBG("Target mode activated");
721 nfc_netlink_event_tm_activated(gnlh);
724 case NFC_EVENT_TM_DEACTIVATED:
725 DBG("Target mode deactivated");
726 nfc_netlink_event_tm_deactivated(gnlh);
734 static gboolean __nfc_netlink_event(GIOChannel *channel,
735 GIOCondition cond, gpointer data)
738 struct nlnfc_state *state = data;
740 if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR))
743 cb = nl_cb_alloc(NL_CB_VERBOSE);
747 nl_cb_set(cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, no_seq_check, NULL);
748 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, nfc_netlink_event, data);
750 nl_recvmsgs(state->event_sock, cb);
757 static int nfc_event_listener(struct nlnfc_state *state)
761 sock = nl_socket_get_fd(state->event_sock);
762 netlink_channel = g_io_channel_unix_new(sock);
763 g_io_channel_set_close_on_unref(netlink_channel, TRUE);
765 g_io_channel_set_encoding(netlink_channel, NULL, NULL);
766 g_io_channel_set_buffered(netlink_channel, FALSE);
768 g_io_add_watch(netlink_channel,
769 G_IO_IN | G_IO_NVAL | G_IO_HUP | G_IO_ERR,
770 __nfc_netlink_event, state);
775 struct handler_args {
780 static int family_handler(struct nl_msg *msg, void *arg)
782 struct handler_args *grp = arg;
783 struct nlattr *tb[CTRL_ATTR_MAX + 1];
784 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
785 struct nlattr *mcgrp;
790 nla_parse(tb, CTRL_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
791 genlmsg_attrlen(gnlh, 0), NULL);
793 if (!tb[CTRL_ATTR_MCAST_GROUPS])
796 nla_for_each_nested(mcgrp, tb[CTRL_ATTR_MCAST_GROUPS], rem_mcgrp) {
797 struct nlattr *tb_mcgrp[CTRL_ATTR_MCAST_GRP_MAX + 1];
799 nla_parse(tb_mcgrp, CTRL_ATTR_MCAST_GRP_MAX,
800 nla_data(mcgrp), nla_len(mcgrp), NULL);
802 if (!tb_mcgrp[CTRL_ATTR_MCAST_GRP_NAME] ||
803 !tb_mcgrp[CTRL_ATTR_MCAST_GRP_ID])
805 if (strncmp(nla_data(tb_mcgrp[CTRL_ATTR_MCAST_GRP_NAME]),
806 grp->group, nla_len(tb_mcgrp[CTRL_ATTR_MCAST_GRP_NAME])))
808 grp->id = nla_get_u32(tb_mcgrp[CTRL_ATTR_MCAST_GRP_ID]);
815 static int nl_get_multicast_id(struct nl_sock *sock, const char *family,
820 struct handler_args grp = {
831 ctrlid = genl_ctrl_resolve(sock, "nlctrl");
833 genlmsg_put(msg, 0, 0, ctrlid, 0, 0, CTRL_CMD_GETFAMILY, 0);
837 NLA_PUT_STRING(msg, CTRL_ATTR_FAMILY_NAME, family);
839 err = nl_send_msg(sock, msg, family_handler, &grp);
841 goto nla_put_failure;
843 DBG("multicast id %d", grp.id);
853 int __near_netlink_init(void)
859 nfc_state = g_try_malloc0(sizeof(struct nlnfc_state));
863 nfc_state->cmd_sock = nl_socket_alloc();
864 if (!nfc_state->cmd_sock) {
865 near_error("Failed to allocate NFC command netlink socket");
870 nfc_state->event_sock = nl_socket_alloc();
871 if (!nfc_state->event_sock) {
872 near_error("Failed to allocate NFC event netlink socket");
874 goto handle_cmd_destroy;
877 if (genl_connect(nfc_state->cmd_sock)) {
878 near_error("Failed to connect to generic netlink");
880 goto handle_event_destroy;
883 if (genl_connect(nfc_state->event_sock)) {
884 near_error("Failed to connect to generic netlink");
886 goto handle_event_destroy;
889 nfc_state->nfc_id = genl_ctrl_resolve(nfc_state->cmd_sock, "nfc");
890 if (nfc_state->nfc_id < 0) {
891 near_error("Unable to find NFC netlink family");
893 goto handle_event_destroy;
896 nfc_state->mcid = nl_get_multicast_id(nfc_state->cmd_sock, NFC_GENL_NAME,
897 NFC_GENL_MCAST_EVENT_NAME);
898 if (nfc_state->mcid <= 0) {
899 near_error("Wrong mcast id %d", nfc_state->mcid);
900 err = nfc_state->mcid;
901 goto handle_event_destroy;
904 err = nl_socket_add_membership(nfc_state->event_sock, nfc_state->mcid);
906 near_error("Error adding nl event socket to membership");
907 goto handle_event_destroy;
910 return nfc_event_listener(nfc_state);
912 handle_event_destroy:
913 nl_socket_free(nfc_state->event_sock);
916 nl_socket_free(nfc_state->cmd_sock);
923 near_error("netlink init failed");
928 void __near_netlink_cleanup(void)
930 if (netlink_channel) {
931 g_io_channel_shutdown(netlink_channel, TRUE, NULL);
932 g_io_channel_unref(netlink_channel);
934 netlink_channel = NULL;
940 nl_socket_free(nfc_state->cmd_sock);
941 nl_socket_free(nfc_state->event_sock);