nfctool: Move adapter related functions in one place
authorThierry Escande <thierry.escande@linux.intel.com>
Fri, 22 Feb 2013 18:28:34 +0000 (19:28 +0100)
committerSamuel Ortiz <sameo@linux.intel.com>
Wed, 27 Feb 2013 17:12:10 +0000 (18:12 +0100)
This moves adapter management functions in adapter.c

Makefile.am
tools/nfctool/adapter.c [new file with mode: 0644]
tools/nfctool/adapter.h [new file with mode: 0644]
tools/nfctool/main.c
tools/nfctool/netlink.c
tools/nfctool/nfctool.h

index 6e06be0..dc84433 100644 (file)
@@ -92,6 +92,8 @@ tools_snep_send_LDADD = @GLIB_LIBS@ @DBUS_LIBS@
 
 tools_nfctool_nfctool_SOURCES = tools/nfctool/main.c \
                                        tools/nfctool/nfctool.h \
+                                       tools/nfctool/adapter.h \
+                                       tools/nfctool/adapter.c \
                                        tools/nfctool/netlink.h \
                                        tools/nfctool/netlink.c \
                                        tools/nfctool/sniffer.h \
diff --git a/tools/nfctool/adapter.c b/tools/nfctool/adapter.c
new file mode 100644 (file)
index 0000000..1beb5b1
--- /dev/null
@@ -0,0 +1,213 @@
+/*
+ *
+ *  Near Field Communication nfctool
+ *
+ *  Copyright (C) 2013  Intel Corporation. All rights reserved.
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License version 2 as
+ *  published by the Free Software Foundation.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <string.h>
+#include <sys/socket.h>
+#include <linux/nfc.h>
+#include <glib.h>
+
+#include "adapter.h"
+#include "nfctool.h"
+#include "netlink.h"
+
+static GSList *adapters;
+
+static struct nfc_adapter *selected_adapter;
+
+static void adapter_get_devices(struct nfc_adapter *adapter)
+{
+       if (adapter->rf_mode == NFC_RF_INITIATOR)
+               nl_get_targets(adapter);
+
+       nl_get_params(adapter);
+}
+
+int adapter_all_get_devices(void)
+{
+       int err;
+
+       err = nl_get_devices();
+       if (err)
+               return err;
+
+       g_slist_foreach(adapters, (GFunc)adapter_get_devices, NULL);
+
+       return 0;
+}
+
+static void adapter_print_target(guint32 idx, gchar *type)
+{
+       printf("%s%d ", type, idx);
+}
+
+void adpater_print_targets(struct nfc_adapter *adapter, gchar *prefix)
+{
+       printf("%sTags: [ ", prefix);
+
+       g_slist_foreach(adapter->tags, (GFunc)adapter_print_target, "tag");
+
+       printf("]\n");
+
+       printf("%sDevices: [ ", prefix);
+
+       g_slist_foreach(adapter->devices,
+                       (GFunc)adapter_print_target, "device");
+
+       printf("]\n");
+}
+
+void adapter_print_info(struct nfc_adapter *adapter)
+{
+       gchar *rf_mode_str;
+
+       if (adapter == NULL)
+               return;
+
+       printf("nfc%d:\n", adapter->idx);
+
+       adpater_print_targets(adapter, "          ");
+
+       printf("          Protocols: [ ");
+
+       if (adapter->protocols & NFC_PROTO_FELICA_MASK)
+               printf("Felica ");
+
+       if (adapter->protocols & NFC_PROTO_MIFARE_MASK)
+               printf("MIFARE ");
+
+       if (adapter->protocols & NFC_PROTO_JEWEL_MASK)
+               printf("Jewel ");
+
+       if (adapter->protocols & NFC_PROTO_ISO14443_MASK)
+               printf("ISO-DEP ");
+
+       if (adapter->protocols & NFC_PROTO_NFC_DEP_MASK)
+               printf("NFC-DEP ");
+
+       printf("]\n");
+
+       printf("          Powered: %s\n",
+               adapter->powered ? "Yes" : "No");
+
+       if (adapter->rf_mode == NFC_RF_INITIATOR)
+               rf_mode_str = "Initiator";
+       else if (adapter->rf_mode == NFC_RF_TARGET)
+               rf_mode_str = "Target";
+       else
+               rf_mode_str = "None";
+
+       printf("          RF Mode: %s\n", rf_mode_str);
+
+       printf("          lto: %d\n", adapter->param_lto);
+       printf("          rw: %d\n", adapter->param_rw);
+       printf("          miux: %d\n", adapter->param_miux);
+
+       printf("\n");
+}
+
+void adapter_idx_print_info(guint32 idx)
+{
+       if (idx != INVALID_ADAPTER_IDX)
+               adapter_print_info(adapter_get(idx));
+       else
+               g_slist_foreach(adapters, (GFunc)adapter_print_info, NULL);
+}
+
+static gint adapter_compare_idx(struct nfc_adapter *adapter, guint32 idx)
+{
+       return (gint)adapter->idx - (gint)idx;
+}
+
+struct nfc_adapter *adapter_get(guint32 idx)
+{
+       GSList *elem;
+
+       if (idx == opts.adapter_idx)
+               return selected_adapter;
+
+       elem = g_slist_find_custom(adapters, GINT_TO_POINTER(idx),
+                                  (GCompareFunc)adapter_compare_idx);
+
+       if (elem)
+               return elem->data;
+
+       return NULL;
+}
+
+void adapter_add_target(struct nfc_adapter *adapter, guint8 type, guint32 idx)
+{
+       DBG("adapter_idx: %d, target_type: %d, target_idx: %d",
+           adapter->idx, type, idx);
+
+       if (type == TARGET_TYPE_TAG)
+               adapter->tags = g_slist_append(adapter->tags,
+                                              GINT_TO_POINTER(idx));
+       else
+               adapter->devices = g_slist_append(adapter->devices,
+                                                 GINT_TO_POINTER(idx));
+}
+
+void adapter_free(struct nfc_adapter *adapter)
+{
+       g_slist_free(adapter->tags);
+       g_slist_free(adapter->devices);
+
+       g_free(adapter);
+}
+
+struct nfc_adapter *adapter_add(guint32 idx, guint32 protocols,
+                               guint8 powered, guint8 rf_mode)
+{
+       struct nfc_adapter *adapter;
+
+       adapter = g_malloc0(sizeof(struct nfc_adapter));
+
+       adapter->idx = idx;
+       adapter->protocols = protocols;
+       adapter->powered = powered;
+       adapter->rf_mode = rf_mode;
+
+       if (rf_mode == NFC_RF_TARGET)
+               adapter_add_target(adapter, TARGET_TYPE_DEVICE, 0);
+
+       adapters = g_slist_append(adapters, adapter);
+
+       if (idx == opts.adapter_idx)
+               selected_adapter = adapter;
+
+       return adapter;
+}
+
+void adapter_cleanup(void)
+{
+       g_slist_free_full(adapters, (GDestroyNotify)adapter_free);
+}
+
+int adapter_init(void)
+{
+       adapters = NULL;
+       selected_adapter = NULL;
+
+       return 0;
+}
diff --git a/tools/nfctool/adapter.h b/tools/nfctool/adapter.h
new file mode 100644 (file)
index 0000000..c22ded8
--- /dev/null
@@ -0,0 +1,59 @@
+/*
+ *
+ *  Near Field Communication nfctool
+ *
+ *  Copyright (C) 2013  Intel Corporation. All rights reserved.
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License version 2 as
+ *  published by the Free Software Foundation.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ */
+
+#ifndef __ADAPTER_H
+#define __ADAPTER_H
+
+struct nfc_adapter {
+       guint32 idx;
+       guint32 protocols;
+       guint8 powered;
+       guint8 polling;
+       guint8 rf_mode;
+       GSList *tags;
+       GSList *devices;
+       gint32 param_lto;
+       gint32 param_rw;
+       gint32 param_miux;
+};
+
+int adapter_init(void);
+
+void adapter_cleanup(void);
+
+struct nfc_adapter *adapter_add(guint32 idx, guint32 protocols,
+                               guint8 powered, guint8 rf_mode);
+
+void adapter_free(struct nfc_adapter *adapter);
+
+void adapter_add_target(struct nfc_adapter *adapter, guint8 type, guint32 idx);
+
+struct nfc_adapter *adapter_get(guint32 adapter_idx);
+
+int adapter_all_get_devices(void);
+
+void adapter_idx_print_info(guint32 idx);
+
+void adapter_print_info(struct nfc_adapter *adapter);
+
+void adpater_print_targets(struct nfc_adapter *adapter, gchar *prefix);
+
+#endif /* __ADAPTER_H */
index 1948e7f..1b9a266 100644 (file)
@@ -29,6 +29,7 @@
 #include <near/nfc_copy.h>
 
 #include "nfctool.h"
+#include "adapter.h"
 #include "netlink.h"
 #include "sniffer.h"
 
 #define LLCP_MAX_RW   0x0f
 #define LLCP_MAX_MIUX 0x7ff
 
-GSList *adapters = NULL;
-
 static GMainLoop *main_loop = NULL;
 
-static gint nfctool_compare_adapter_idx(struct nfc_adapter *adapter,
-                                                               guint32 idx)
-{
-       if (adapter->idx < idx)
-               return -1;
-
-       if (adapter->idx > idx)
-               return 1;
-
-       return 0;
-}
-
-static struct nfc_adapter *nfctool_find_adapter(guint32 adapter_idx)
-{
-       GSList *elem;
-
-       elem = g_slist_find_custom(adapters, GINT_TO_POINTER(adapter_idx),
-                               (GCompareFunc)nfctool_compare_adapter_idx);
-
-       if (elem)
-               return elem->data;
-
-       return NULL;
-}
-
-static void nfctool_print_target(guint32 idx, gchar *type)
-{
-       printf("%s%d ", type, idx);
-}
-
-static void nfctool_print_targets(struct nfc_adapter *adapter, gchar *prefix)
-{
-       printf("%sTags: [ ", prefix);
-
-       g_slist_foreach(adapter->tags, (GFunc)nfctool_print_target, "tag");
-
-       printf("]\n");
-
-       printf("%sDevices: [ ", prefix);
-
-       g_slist_foreach(adapter->devices,
-                       (GFunc)nfctool_print_target, "device");
-
-       printf("]\n");
-}
-
-static void nfctool_print_adapter_info(struct nfc_adapter *adapter)
-{
-       gchar *rf_mode_str;
-
-       printf("nfc%d:\n", adapter->idx);
-
-       nfctool_print_targets(adapter, "          ");
-
-       printf("          Protocols: [ ");
-
-       if (adapter->protocols & NFC_PROTO_FELICA_MASK)
-               printf("Felica ");
-
-       if (adapter->protocols & NFC_PROTO_MIFARE_MASK)
-               printf("MIFARE ");
-
-       if (adapter->protocols & NFC_PROTO_JEWEL_MASK)
-               printf("Jewel ");
-
-       if (adapter->protocols & NFC_PROTO_ISO14443_MASK)
-               printf("ISO-DEP ");
-
-       if (adapter->protocols & NFC_PROTO_NFC_DEP_MASK)
-               printf("NFC-DEP ");
-
-       printf("]\n");
-
-       printf("          Powered: %s\n",
-               adapter->powered ? "Yes" : "No");
-
-       if (adapter->rf_mode == NFC_RF_INITIATOR)
-               rf_mode_str = "Initiator";
-       else if (adapter->rf_mode == NFC_RF_TARGET)
-               rf_mode_str = "Target";
-       else
-               rf_mode_str = "None";
-
-       printf("          RF Mode: %s\n", rf_mode_str);
-
-       printf("          lto: %d\n", adapter->param_lto);
-       printf("          rw: %d\n", adapter->param_rw);
-       printf("          miux: %d\n", adapter->param_miux);
-
-       printf("\n");
-}
-
-static void nfctool_list_adapter(struct nfc_adapter *adapter, guint32 idx)
-{
-       if (idx == INVALID_ADAPTER_IDX || idx == adapter->idx)
-               nfctool_print_adapter_info(adapter);
-}
-
-static void nfctool_list_adapters(void)
-{
-       g_slist_foreach(adapters, (GFunc)nfctool_list_adapter,
-                                       GINT_TO_POINTER(opts.adapter_idx));
-}
-
-static void nfctool_adapter_free(struct nfc_adapter *adapter)
-{
-       g_slist_free(adapter->tags);
-       g_slist_free(adapter->devices);
-
-       g_free(adapter);
-}
-
 static gchar *nfctool_poll_mode_str(int mode)
 {
        if (mode == POLLING_MODE_TARGET)
@@ -169,7 +56,7 @@ static int nfctool_start_poll(void)
 
        struct nfc_adapter *adapter;
 
-       adapter = nfctool_find_adapter(opts.adapter_idx);
+       adapter = adapter_get(opts.adapter_idx);
 
        if (adapter == NULL) {
                print_error("Invalid adapter index: %d", opts.adapter_idx);
@@ -196,33 +83,12 @@ static int nfctool_start_poll(void)
        return err;
 }
 
-static void nfctool_get_device(struct nfc_adapter *adapter)
-{
-       if (adapter->rf_mode == NFC_RF_INITIATOR)
-               nl_get_targets(adapter);
-
-       nl_get_params(adapter);
-}
-
-static int nfctool_get_devices(void)
-{
-       int err;
-
-       err = nl_get_devices();
-       if (err)
-               return err;
-
-       g_slist_foreach(adapters, (GFunc)nfctool_get_device, NULL);
-
-       return 0;
-}
-
 static int nfctool_set_params(void)
 {
        struct nfc_adapter *adapter;
        int err;
 
-       adapter = nfctool_find_adapter(opts.adapter_idx);
+       adapter = adapter_get(opts.adapter_idx);
        if (!adapter)
                return -ENODEV;
 
@@ -234,7 +100,7 @@ static int nfctool_set_params(void)
 
        nl_get_params(adapter);
 
-       nfctool_print_adapter_info(adapter);
+       adapter_print_info(adapter);
 
 exit:
        return err;
@@ -265,7 +131,7 @@ static int nfctool_targets_found(guint32 adapter_idx)
        if (adapter_idx == INVALID_ADAPTER_IDX)
                return -ENODEV;
 
-       adapter = nfctool_find_adapter(adapter_idx);
+       adapter = adapter_get(adapter_idx);
 
        if (adapter == NULL)
                return -ENODEV;
@@ -277,7 +143,7 @@ static int nfctool_targets_found(guint32 adapter_idx)
        }
 
        printf("Targets found for nfc%d\n", adapter_idx);
-       nfctool_print_targets(adapter, "  ");
+       adpater_print_targets(adapter, "  ");
 
        if (adapter->polling) {
                g_slist_foreach(adapter->devices,
@@ -575,17 +441,19 @@ int main(int argc, char **argv)
        if (err)
                goto exit_err;
 
+       adapter_init();
+
        if (opts.need_netlink) {
                err = nl_init(nfc_event_cb);
                if (err)
                        goto exit_err;
 
-               err = nfctool_get_devices();
+               err = adapter_all_get_devices();
                if (err)
                        goto exit_err;
 
                if (opts.list && !opts.set_param)
-                       nfctool_list_adapters();
+                       adapter_idx_print_info(opts.adapter_idx);
 
                if (opts.set_param) {
                        err = nfctool_set_params();
@@ -618,7 +486,7 @@ int main(int argc, char **argv)
 exit_err:
        nfctool_main_loop_clean();
 
-       g_slist_free_full(adapters, (GDestroyNotify)nfctool_adapter_free);
+       adapter_cleanup();
 
        nl_cleanup();
 
index cded411..edaefef 100644 (file)
@@ -37,6 +37,7 @@
 
 #include "nfctool.h"
 #include "netlink.h"
+#include "adapter.h"
 
 #ifdef NEED_LIBNL_COMPAT
 #define nl_sock nl_handle
@@ -78,20 +79,6 @@ static GIOChannel *nl_gio_channel = NULL;
 
 static nfc_event_cb_t nfc_event_cb = NULL;
 
-static void adapter_add_target(struct nfc_adapter *adapter,
-                               guint8 type, guint32 idx)
-{
-       DBG("adapter_idx: %d, target_type: %d, target_idx: %d", adapter->idx,
-                                                               type, idx);
-
-       if (type == TARGET_TYPE_TAG)
-               adapter->tags = g_slist_append(adapter->tags,
-                                              GINT_TO_POINTER(idx));
-       else
-               adapter->devices = g_slist_append(adapter->devices,
-                                                 GINT_TO_POINTER(idx));
-}
-
 static int nl_error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err,
                         void *arg)
 {
@@ -428,7 +415,6 @@ static int nl_get_devices_handler(struct nl_msg *n, void *arg)
        guint32 idx, protocols = 0;
        guint8 powered = 0;
        guint8 rf_mode = NFC_RF_NONE;
-       struct nfc_adapter *adapter;
 
        DBG("");
 
@@ -450,17 +436,7 @@ static int nl_get_devices_handler(struct nl_msg *n, void *arg)
        if (attrs[NFC_ATTR_DEVICE_POWERED] != NULL)
                powered = nla_get_u8(attrs[NFC_ATTR_DEVICE_POWERED]);
 
-       adapter = g_malloc0(sizeof(struct nfc_adapter));
-
-       adapter->idx = idx;
-       adapter->protocols = protocols;
-       adapter->powered = powered;
-       adapter->rf_mode = rf_mode;
-
-       if (rf_mode == NFC_RF_TARGET)
-               adapter_add_target(adapter, TARGET_TYPE_DEVICE, 0);
-
-       adapters = g_slist_append(adapters, adapter);
+       adapter_add(idx, protocols, powered, rf_mode);
 
        return NL_SKIP;
 }
index 40c6cec..f9e2918 100644 (file)
 #define SNIFFER_SHOW_TIMESTAMP_DELTA   1
 #define SNIFFER_SHOW_TIMESTAMP_ABS     2
 
-struct nfc_target {
-       guint32 idx;
-       guint8 type;
-};
-
-struct nfc_adapter {
-       guint32 idx;
-       guint32 protocols;
-       guint8 powered;
-       guint8 polling;
-       guint8 rf_mode;
-       GSList *tags;
-       GSList *devices;
-       gint32 param_lto;
-       gint32 param_rw;
-       gint32 param_miux;
-};
-
 struct nfctool_options {
        gboolean list;
        gboolean poll;
@@ -81,6 +63,5 @@ struct nfctool_options {
 
 extern struct nfctool_options opts;
 
-extern GSList *adapters;
 
 #endif /* __NFCTOOL_H */