adapter: Port to the new kernel socket API
authorSamuel Ortiz <sameo@linux.intel.com>
Mon, 9 May 2011 14:23:13 +0000 (16:23 +0200)
committerMarcel Holtmann <marcel@holtmann.org>
Fri, 21 Oct 2011 06:54:03 +0000 (23:54 -0700)
src/adapter.c
src/netlink.c
src/target.c

index 4538bd0..55fc645 100644 (file)
 
 #include <stdio.h>
 #include <stdlib.h>
+#include <unistd.h>
 #include <errno.h>
 
 #include <glib.h>
 
 #include <gdbus.h>
 
-#include <linux/nfc.h>
-
 #include "near.h"
 
 static DBusConnection *connection = NULL;
@@ -51,8 +50,12 @@ struct near_adapter {
 
        GList *target_list;
        struct near_target *active_target;
+       int active_sock;
 };
 
+/* HACK HACK */
+#define AF_NFC 39
+
 static void free_adapter(gpointer data)
 {
        struct near_adapter *adapter = data;
@@ -97,35 +100,35 @@ static void append_protocols(DBusMessageIter *iter, void *user_data)
 
        DBG("protocols 0x%x", adapter->protocols);
 
-       if (adapter->protocols & NFC_PROTO_FELICA) {
+       if (adapter->protocols & NFC_PROTO_FELICA_MASK) {
                str = "Felica";
 
                dbus_message_iter_append_basic(iter,
                                DBUS_TYPE_STRING, &str);
        }
 
-       if (adapter->protocols & NFC_PROTO_MIFARE) {
+       if (adapter->protocols & NFC_PROTO_MIFARE_MASK) {
                str = "MIFARE";
 
                dbus_message_iter_append_basic(iter,
                                DBUS_TYPE_STRING, &str);
        }
 
-       if (adapter->protocols & NFC_PROTO_JEWEL) {
+       if (adapter->protocols & NFC_PROTO_JEWEL_MASK) {
                str = "Jewel";
 
                dbus_message_iter_append_basic(iter,
                                DBUS_TYPE_STRING, &str);
        }
 
-       if (adapter->protocols & NFC_PROTO_ISO14443_4) {
+       if (adapter->protocols & NFC_PROTO_ISO14443_MASK) {
                str = "ISO-DEP";
 
                dbus_message_iter_append_basic(iter,
                                DBUS_TYPE_STRING, &str);
        }
 
-       if (adapter->protocols & NFC_PROTO_NFC_DEP) {
+       if (adapter->protocols & NFC_PROTO_NFC_DEP_MASK) {
                str = "NFC-DEP";
 
                dbus_message_iter_append_basic(iter,
@@ -274,6 +277,8 @@ struct near_adapter * __near_adapter_create(uint32_t idx,
        adapter->idx = idx;
        adapter->protocols = protocols;
        adapter->powered = TRUE;
+       adapter->active_target = NULL;
+       adapter->active_sock = -1;
 
        adapter->path = g_strdup_printf("%s/nfc%d", NFC_PATH, idx);
 
@@ -366,7 +371,8 @@ int __near_adapter_remove_target(uint32_t idx, struct near_target *target)
                return -ENODEV;
 
        if (adapter->active_target == target) {
-               __near_netlink_deactivate_target(idx, target_idx);
+               if (adapter->active_sock != -1)
+                       close(adapter->active_sock);
                adapter->active_target = NULL;
        }
 
@@ -416,7 +422,8 @@ int near_adapter_connect(uint32_t idx, uint32_t target_idx)
        struct near_adapter *adapter;
        struct near_target *target;
        uint32_t protocols;
-       int err;
+       struct sockaddr_nfc addr;
+       int err, sock;
 
        DBG("idx %d", idx);
 
@@ -433,18 +440,29 @@ int near_adapter_connect(uint32_t idx, uint32_t target_idx)
 
        protocols = __near_target_get_protocols(target);
 
-       err = __near_netlink_activate_target(idx, target_idx, protocols);
-       if (err == 0)
-               adapter->active_target = target;
+       sock = socket(AF_NFC, SOCK_SEQPACKET, protocols);
+       if (sock == -1)
+               return sock;
+
+       addr.sa_family = AF_NFC;
+       addr.dev_idx = idx;
+       addr.target_idx = target_idx;
+
+       err = connect(sock, (struct sockaddr *) &addr, sizeof(addr));
+       if (err) {
+               close(sock);
+               return err;
+       }
+
+       adapter->active_target = target;
+       adapter->active_sock = sock;
 
-       return err;
+       return 0;
 }
 
-int near_adapter_disconnect(uint32_t idx, uint32_t target_idx)
+int near_adapter_disconnect(uint32_t idx)
 {
        struct near_adapter *adapter;
-       struct near_target *target;
-       int err;
 
        DBG("idx %d", idx);
 
@@ -452,15 +470,13 @@ int near_adapter_disconnect(uint32_t idx, uint32_t target_idx)
        if (adapter == NULL)
                return -ENODEV;
 
-       target = find_target(adapter, target_idx);
-       if (target == NULL || adapter->active_target == NULL)
+       if (adapter->active_sock == -1)
                return -ENOLINK;
 
-       err = __near_netlink_deactivate_target(idx, target_idx);
-       if (err == 0)
-               adapter->active_target = NULL;
+       close(adapter->active_sock);
+       adapter->active_sock = -1;
 
-       return err;
+       return 0;
 }
 
 int __near_adapter_init(void)
index 2f0217d..fa19758 100644 (file)
@@ -34,8 +34,6 @@
 #include <netlink/genl/family.h>
 #include <netlink/genl/ctrl.h>
 
-#include <linux/nfc.h>
-
 #include "near.h"
 
 struct nlnfc_state {
@@ -242,76 +240,6 @@ nla_put_failure:
        return err;
 }
 
-int __near_netlink_activate_target(uint32_t adapter_idx,
-                                       uint32_t target_idx,
-                                       uint32_t protocol)
-{
-       struct nl_msg *msg;
-       void *hdr;
-       int family, err = 0;
-
-       DBG("");
-
-       msg = nlmsg_alloc();
-       if (msg == NULL)
-               return -ENOMEM;
-
-       family = genl_family_get_id(nfc_state->nlnfc);
-
-       hdr = genlmsg_put(msg, NL_AUTO_PID, NL_AUTO_SEQ, family, 0,
-                       NLM_F_REQUEST, NFC_CMD_ACTIVATE_TARGET,
-                       NFC_GENL_VERSION);
-       if (hdr == NULL) {
-               err = -EINVAL;
-               goto nla_put_failure;
-       }
-
-       NLA_PUT_U32(msg, NFC_ATTR_DEVICE_INDEX, adapter_idx);
-       NLA_PUT_U32(msg, NFC_ATTR_TARGET_INDEX, target_idx);
-       NLA_PUT_U32(msg, NFC_ATTR_PROTOCOLS, protocol);
-
-       err = nl_send_msg(nfc_state->nl_sock, msg, NULL, NULL);
-
-nla_put_failure:
-       nlmsg_free(msg);
-
-       return err;
-}
-
-int __near_netlink_deactivate_target(uint32_t adapter_idx,
-                                       uint32_t target_idx)
-{
-       struct nl_msg *msg;
-       void *hdr;
-       int family, err = 0;
-
-       DBG("");
-
-       msg = nlmsg_alloc();
-       if (msg == NULL)
-               return -ENOMEM;
-
-       family = genl_family_get_id(nfc_state->nlnfc);
-
-       hdr = genlmsg_put(msg, NL_AUTO_PID, NL_AUTO_SEQ, family, 0,
-                       NLM_F_REQUEST, NFC_CMD_DEACTIVATE_TARGET,
-                       NFC_GENL_VERSION);
-       if (hdr == NULL) {
-               err = -EINVAL;
-               goto nla_put_failure;
-       }
-
-       NLA_PUT_U32(msg, NFC_ATTR_DEVICE_INDEX, adapter_idx);
-       NLA_PUT_U32(msg, NFC_ATTR_TARGET_INDEX, target_idx);
-
-       err = nl_send_msg(nfc_state->nl_sock, msg, NULL, NULL);
-
-nla_put_failure:
-       nlmsg_free(msg);
-
-       return err;
-}
-
 static int no_seq_check(struct nl_msg *n, void *arg)
 {
        DBG("");
index c01e9c0..1bb7ff8 100644 (file)
@@ -31,8 +31,6 @@
 
 #include <gdbus.h>
 
-#include <linux/nfc.h>
-
 #include "near.h"
 
 struct near_target {
@@ -95,35 +93,35 @@ static void append_protocols(DBusMessageIter *iter, void *user_data)
 
        DBG("protocols 0x%x", target->protocols);
 
-       if (target->protocols & NFC_PROTO_FELICA) {
+       if (target->protocols & NFC_PROTO_FELICA_MASK) {
                str = "Felica";
 
                dbus_message_iter_append_basic(iter,
                                DBUS_TYPE_STRING, &str);
        }
 
-       if (target->protocols & NFC_PROTO_MIFARE) {
+       if (target->protocols & NFC_PROTO_MIFARE_MASK) {
                str = "MIFARE";
 
                dbus_message_iter_append_basic(iter,
                                DBUS_TYPE_STRING, &str);
        }
 
-       if (target->protocols & NFC_PROTO_JEWEL) {
+       if (target->protocols & NFC_PROTO_JEWEL_MASK) {
                str = "Jewel";
 
                dbus_message_iter_append_basic(iter,
                                DBUS_TYPE_STRING, &str);
        }
 
-       if (target->protocols & NFC_PROTO_ISO14443_4) {
+       if (target->protocols & NFC_PROTO_ISO14443_MASK) {
                str = "ISO-DEP";
 
                dbus_message_iter_append_basic(iter,
                                DBUS_TYPE_STRING, &str);
        }
 
-       if (target->protocols & NFC_PROTO_NFC_DEP) {
+       if (target->protocols & NFC_PROTO_NFC_DEP_MASK) {
                str = "NFC-DEP";
 
                dbus_message_iter_append_basic(iter,
@@ -243,7 +241,8 @@ static GDBusSignalTable target_signals[] = {
        { }
 };
 
-#define NFC_TAG_A (NFC_PROTO_ISO14443_4 | NFC_PROTO_NFC_DEP | NFC_PROTO_MIFARE)
+#define NFC_TAG_A (NFC_PROTO_ISO14443_MASK | NFC_PROTO_NFC_DEP_MASK | \
+                                               NFC_PROTO_MIFARE_MASK)
 #define NFC_TAG_A_TYPE1      0x00
 #define NFC_TAG_A_TYPE4      0x01
 #define NFC_TAG_A_NFC_DEP    0x02