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

Rajkumar Manoharan rmanohar at qca.qualcomm.com
Thu Jul 21 09:24:37 EDT 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 |   36 ++++++++++++++++++++++++++++++++++++
 src/utils/common.h |   11 +++++++++++
 2 files changed, 47 insertions(+), 0 deletions(-)

diff --git a/src/utils/common.c b/src/utils/common.c
index 89eca1c..5c72221 100644
--- a/src/utils/common.c
+++ b/src/utils/common.c
@@ -385,3 +385,39 @@ void * __hide_aliasing_typecast(void *foo)
 {
 	return foo;
 }
+
+enum wpas_band freq_to_channel(int freq, u8 *reg_class, u8 *channel)
+{
+	enum wpas_band band = WPAS_BAND_INVALID;
+	u8 chan = 0, rc;
+
+	if (freq >= 2412 && freq <= 2472) {
+		rc = 81; /* 2.407 GHz, channels 1..13 */
+		chan = (freq - 2407) / 5;
+		band = WPAS_BAND_2GHZ;
+	}
+
+	if (freq == 2484) {
+		rc = 82; /* channel 14 */
+		chan = 14;
+		band = WPAS_BAND_2GHZ;
+	}
+
+	if (freq >= 5180 && freq <= 5240) {
+		rc = 115; /* 5 GHz, channels 36..48 */
+		chan = (freq - 5000) / 5;
+		band = WPAS_BAND_5GHZ;
+	}
+
+	if (freq >= 5745 && freq <= 5805) {
+		rc = 124; /* 5 GHz, channels 149..161 */
+		chan = (freq - 5000) / 5;
+		band = WPAS_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..fd88ddf 100644
--- a/src/utils/common.h
+++ b/src/utils/common.h
@@ -165,6 +165,16 @@ typedef int8_t s8;
 #define WPA_TYPES_DEFINED
 #endif /* !WPA_TYPES_DEFINED */
 
+/**
+ * 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,
+};
 
 /* Define platform specific byte swapping macros */
 
@@ -492,6 +502,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 wpas_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