[RFC 2/3] wpa_supplicant: Add freq_to_channel utility function

Rajkumar Manoharan rmanohar
Sat Jul 16 23:06:06 PDT 2011


This patch defines freq_to_channel utility function that returns
regulatory class, channel number and band for the given frequency.

Signed-off-by: Rajkumar Manoharan <rmanohar at qca.qualcomm.com>
---
 src/drivers/nl80211_copy.h |   10 ----------
 src/utils/common.c         |   37 +++++++++++++++++++++++++++++++++++++
 src/utils/common.h         |   10 ++++++++++
 3 files changed, 47 insertions(+), 10 deletions(-)

diff --git a/src/drivers/nl80211_copy.h b/src/drivers/nl80211_copy.h
index 8cb025a..2bd78cf 100644
--- a/src/drivers/nl80211_copy.h
+++ b/src/drivers/nl80211_copy.h
@@ -2125,16 +2125,6 @@ enum nl80211_tx_rate_attributes {
 	NL80211_TXRATE_MAX = __NL80211_TXRATE_AFTER_LAST - 1
 };
 
-/**
- * enum nl80211_band - Frequency band
- * @NL80211_BAND_2GHZ: 2.4 GHz ISM band
- * @NL80211_BAND_5GHZ: around 5 GHz band (4.9 - 5.7 GHz)
- */
-enum nl80211_band {
-	NL80211_BAND_2GHZ,
-	NL80211_BAND_5GHZ,
-};
-
 enum nl80211_ps_state {
 	NL80211_PS_DISABLED,
 	NL80211_PS_ENABLED,
diff --git a/src/utils/common.c b/src/utils/common.c
index 89eca1c..fe65726 100644
--- a/src/utils/common.c
+++ b/src/utils/common.c
@@ -385,3 +385,40 @@ void * __hide_aliasing_typecast(void *foo)
 {
 	return foo;
 }
+
+enum nl80211_band freq_to_channel(int freq, u8 *reg_class,
+				  u8 *channel)
+{
+	enum nl80211_band band;
+	u8 chan = 0, rc;
+
+	if (freq >= 2412 && freq <= 2472) {
+		rc = 81; /* 2.407 GHz, channels 1..13 */
+		chan = (freq - 2407) / 5;
+		band = NL80211_BAND_2GHZ;
+	}
+
+	if (freq == 2484) {
+		rc = 82; /* channel 14 */
+		chan = 14;
+		band = NL80211_BAND_2GHZ;
+	}
+
+	if (freq >= 5180 && freq <= 5240) {
+		rc = 115; /* 5 GHz, channels 36..48 */
+		chan = (freq - 5000) / 5;
+		band = NL80211_BAND_5GHZ;
+	}
+
+	if (freq >= 5745 && freq <= 5805) {
+		rc = 124; /* 5 GHz, channels 149..161 */
+		chan = (freq - 5000) / 5;
+		band = NL80211_BAND_5GHZ;
+	}
+	if (reg_class)
+		*reg_class = rc;
+	if (channel)
+		*channel = chan;
+
+	return band;
+}
diff --git a/src/utils/common.h b/src/utils/common.h
index 14ab297..6c45936 100644
--- a/src/utils/common.h
+++ b/src/utils/common.h
@@ -165,6 +165,15 @@ typedef int8_t s8;
 #define WPA_TYPES_DEFINED
 #endif /* !WPA_TYPES_DEFINED */
 
+/**
+ * enum nl80211_band - Frequency band
+ * @NL80211_BAND_2GHZ: 2.4 GHz ISM band
+ * @NL80211_BAND_5GHZ: around 5 GHz band (4.9 - 5.7 GHz)
+ */
+enum nl80211_band {
+	NL80211_BAND_2GHZ,
+	NL80211_BAND_5GHZ,
+};
 
 /* Define platform specific byte swapping macros */
 
@@ -492,6 +501,7 @@ static inline int is_broadcast_ether_addr(const u8 *a)
 void * __hide_aliasing_typecast(void *foo);
 #define aliasing_hide_typecast(a,t) (t *) __hide_aliasing_typecast((a))
 
+enum nl80211_band freq_to_channel(int freq, u8 *reg_class, u8 *channel);
 #ifdef CONFIG_VALGRIND
 #include <valgrind/memcheck.h>
 #define WPA_MEM_DEFINED(ptr, len) VALGRIND_MAKE_MEM_DEFINED((ptr), (len))
-- 
1.7.6




More information about the Hostap mailing list