From 2d06acd6ac6850712f4a9b2c344f2016f8f1774c Mon Sep 17 00:00:00 2001
From: Felix Fietkau <nbd@openwrt.org>
Date: Wed, 21 May 2014 12:33:10 +0000
Subject: [PATCH] iwinfo: keep an array of backends, reduce the number of
 ifdefs and hardcoded strcmp calls

Signed-off-by: Felix Fietkau <nbd@openwrt.org>

SVN-Revision: 40809
---
 .../network/utils/iwinfo/src/include/iwinfo.h |  3 +
 .../utils/iwinfo/src/include/iwinfo/madwifi.h |  2 +
 .../utils/iwinfo/src/include/iwinfo/nl80211.h |  2 +
 .../utils/iwinfo/src/include/iwinfo/wext.h    |  2 +
 .../utils/iwinfo/src/include/iwinfo/wl.h      |  2 +
 package/network/utils/iwinfo/src/iwinfo_lib.c | 76 ++++++-------------
 6 files changed, 35 insertions(+), 52 deletions(-)

diff --git a/package/network/utils/iwinfo/src/include/iwinfo.h b/package/network/utils/iwinfo/src/include/iwinfo.h
index ebea319d10..1302d159b9 100644
--- a/package/network/utils/iwinfo/src/include/iwinfo.h
+++ b/package/network/utils/iwinfo/src/include/iwinfo.h
@@ -148,6 +148,9 @@ extern const struct iwinfo_iso3166_label IWINFO_ISO3166_NAMES[];
 
 
 struct iwinfo_ops {
+	const char *name;
+
+	int (*probe)(const char *ifname);
 	int (*mode)(const char *, int *);
 	int (*channel)(const char *, int *);
 	int (*frequency)(const char *, int *);
diff --git a/package/network/utils/iwinfo/src/include/iwinfo/madwifi.h b/package/network/utils/iwinfo/src/include/iwinfo/madwifi.h
index 3662d9f95f..0de9fea6e3 100644
--- a/package/network/utils/iwinfo/src/include/iwinfo/madwifi.h
+++ b/package/network/utils/iwinfo/src/include/iwinfo/madwifi.h
@@ -54,6 +54,8 @@ int madwifi_get_hardware_name(const char *ifname, char *buf);
 void madwifi_close(void);
 
 static const struct iwinfo_ops madwifi_ops = {
+	.name             = "madwifi",
+	.probe            = madwifi_probe,
 	.channel          = madwifi_get_channel,
 	.frequency        = madwifi_get_frequency,
 	.frequency_offset = madwifi_get_frequency_offset,
diff --git a/package/network/utils/iwinfo/src/include/iwinfo/nl80211.h b/package/network/utils/iwinfo/src/include/iwinfo/nl80211.h
index 0611feed3c..97f8c7de63 100644
--- a/package/network/utils/iwinfo/src/include/iwinfo/nl80211.h
+++ b/package/network/utils/iwinfo/src/include/iwinfo/nl80211.h
@@ -96,6 +96,8 @@ int nl80211_get_hardware_name(const char *ifname, char *buf);
 void nl80211_close(void);
 
 static const struct iwinfo_ops nl80211_ops = {
+	.name             = "nl80211",
+	.probe            = nl80211_probe,
 	.channel          = nl80211_get_channel,
 	.frequency        = nl80211_get_frequency,
 	.frequency_offset = nl80211_get_frequency_offset,
diff --git a/package/network/utils/iwinfo/src/include/iwinfo/wext.h b/package/network/utils/iwinfo/src/include/iwinfo/wext.h
index e84f6a6f92..22016c69ac 100644
--- a/package/network/utils/iwinfo/src/include/iwinfo/wext.h
+++ b/package/network/utils/iwinfo/src/include/iwinfo/wext.h
@@ -55,6 +55,8 @@ int wext_get_hardware_name(const char *ifname, char *buf);
 void wext_close(void);
 
 static const struct iwinfo_ops wext_ops = {
+	.name             = "wext",
+	.probe            = wext_probe,
 	.channel          = wext_get_channel,
 	.frequency        = wext_get_frequency,
 	.frequency_offset = wext_get_frequency_offset,
diff --git a/package/network/utils/iwinfo/src/include/iwinfo/wl.h b/package/network/utils/iwinfo/src/include/iwinfo/wl.h
index 077a51bbd4..535a43dc37 100644
--- a/package/network/utils/iwinfo/src/include/iwinfo/wl.h
+++ b/package/network/utils/iwinfo/src/include/iwinfo/wl.h
@@ -55,6 +55,8 @@ int wl_get_hardware_name(const char *ifname, char *buf);
 void wl_close(void);
 
 static const struct iwinfo_ops wl_ops = {
+	.name             = "wl",
+	.probe            = wl_probe,
 	.channel          = wl_get_channel,
 	.frequency        = wl_get_frequency,
 	.frequency_offset = wl_get_frequency_offset,
diff --git a/package/network/utils/iwinfo/src/iwinfo_lib.c b/package/network/utils/iwinfo/src/iwinfo_lib.c
index df1f450cc5..4ae8f591c9 100644
--- a/package/network/utils/iwinfo/src/iwinfo_lib.c
+++ b/package/network/utils/iwinfo/src/iwinfo_lib.c
@@ -313,77 +313,49 @@ const struct iwinfo_iso3166_label IWINFO_ISO3166_NAMES[] = {
 	{ 0,               "" }
 };
 
+#ifndef ARRAY_SIZE
+#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
+#endif
 
-const char * iwinfo_type(const char *ifname)
-{
+static const struct iwinfo_ops *backends[] = {
 #ifdef USE_NL80211
-	if (nl80211_probe(ifname))
-		return "nl80211";
-	else
+	&nl80211_ops,
 #endif
-
 #ifdef USE_MADWIFI
-	if (madwifi_probe(ifname))
-		return "madwifi";
-	else
+	&madwifi_ops,
 #endif
-
 #ifdef USE_WL
-	if (wl_probe(ifname))
-		return "wl";
-	else
+	&wl_ops,
 #endif
+	&wext_ops,
+};
 
-	if (wext_probe(ifname))
-		return "wext";
+const char * iwinfo_type(const char *ifname)
+{
+	const struct iwinfo_ops *ops = iwinfo_backend(ifname);
+	if (!ops)
+		return NULL;
 
-	return NULL;
+	return ops->name;
 }
 
 const struct iwinfo_ops * iwinfo_backend(const char *ifname)
 {
-	const char *type;
-	struct iwinfo_ops *ops;
+	int i;
 
-	type = iwinfo_type(ifname);
-	if (!type)
-		return NULL;
-
-#ifdef USE_NL80211
-	if (!strcmp(type, "nl80211"))
-		return &nl80211_ops;
-	else
-#endif
-
-#ifdef USE_MADWIFI
-	if (!strcmp(type, "madwifi"))
-		return &madwifi_ops;
-	else
-#endif
-
-#ifdef USE_WL
-	if (!strcmp(type, "wl"))
-		return &wl_ops;
-	else
-#endif
-
-	if (!strcmp(type, "wext"))
-		return &wext_ops;
+	for (i = 0; i < ARRAY_SIZE(backends); i++)
+		if (backends[i]->probe(ifname))
+			return backends[i];
 
 	return NULL;
 }
 
 void iwinfo_finish(void)
 {
-#ifdef USE_WL
-	wl_close();
-#endif
-#ifdef USE_MADWIFI
-	madwifi_close();
-#endif
-#ifdef USE_NL80211
-	nl80211_close();
-#endif
-	wext_close();
+	int i;
+
+	for (i = 0; i < ARRAY_SIZE(backends); i++)
+		backends[i]->close();
+
 	iwinfo_close();
 }
-- 
2.30.2