[PATCH 2/4] wpa_supplicant: Add freq_to_channel utility function

Rajkumar Manoharan rmanohar
Thu Jul 21 06:24:37 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/utils/common.c |   44 ++++++++++++++++++++++++++++++++++++++++++++
 src/utils/common.h |   13 +++++++++++++
 2 files changed, 57 insertions(+), 0 deletions(-)

diff --git a/src/utils/common.c b/src/utils/common.c
index 89eca1c..02e8c8d 100644
--- a/src/utils/common.c
+++ b/src/utils/common.c
@@ -385,3 +385,47 @@ void * __hide_aliasing_typecast(void *foo)
 {
 	return foo;
 }
+
+
+/**
+ * freq_to_channel - Convert frequency into channel info
+ * @op_class: Buffer for returning operating class
+ * @channel: Buffer for returning channel number
+ * Returns: Band (2 or 5 GHz) or WPAS_BAND_INVALID if the specified frequency
+ *	is unknown
+ */
+enum wpas_band freq_to_channel(int freq, u8 *op_class, u8 *channel)
+{
+	enum wpas_band band = WPAS_BAND_INVALID;
+	u8 chan = 0, oc;
+
+	if (freq >= 2412 && freq <= 2472) {
+		oc = 81; /* 2.407 GHz, channels 1..13 */
+		chan = (freq - 2407) / 5;
+		band = WPAS_BAND_2GHZ;
+	}
+
+	if (freq == 2484) {
+		oc = 82; /* channel 14 */
+		chan = 14;
+		band = WPAS_BAND_2GHZ;
+	}
+
+	if (freq >= 5180 && freq <= 5240) {
+		oc = 115; /* 5 GHz, channels 36..48 */
+		chan = (freq - 5000) / 5;
+		band = WPAS_BAND_5GHZ;
+	}
+
+	if (freq >= 5745 && freq <= 5805) {
+		oc = 124; /* 5 GHz, channels 149..161 */
+		chan = (freq - 5000) / 5;
+		band = WPAS_BAND_5GHZ;
+	}
+	if (op_class)
+		*op_class = oc;
+	if (channel)
+		*channel = chan;
+
+	return band;
+}
diff --git a/src/utils/common.h b/src/utils/common.h
index 14ab297..acc2ef1 100644
--- a/src/utils/common.h
+++ b/src/utils/common.h
@@ -499,4 +499,17 @@ void * __hide_aliasing_typecast(void *foo);
 #define WPA_MEM_DEFINED(ptr, len) do { } while (0)
 #endif /* CONFIG_VALGRIND */
 
+/**
+ * enum wpas_band - Frequency band
+ * @WPAS_BAND_2GHZ: 2.4 GHz ISM band
+ * @WPAS_BAND_5GHZ: around 5 GHz band (4.9 - 5.7 GHz)
+ */
+enum wpas_band {
+	WPAS_BAND_2GHZ,
+	WPAS_BAND_5GHZ,
+	WPAS_BAND_INVALID
+};
+
+enum wpas_band freq_to_channel(int freq, u8 *op_class, u8 *channel);
+
 #endif /* COMMON_H */
-- 
1.7.4.1





More information about the Hostap mailing list