Share freq-to-channel conversion function
authorAndrei Otcheretianski <andrei.otcheretianski@intel.com>
Mon, 27 Jul 2015 19:24:18 +0000 (22:24 +0300)
committerJouni Malinen <j@w1.fi>
Mon, 3 Aug 2015 20:55:46 +0000 (23:55 +0300)
Add ieee80211_freq_to_channel_ext() conversion function into
ieee802_11_common.c. This function converts freq to channel and
additionally computes operating class, based on provided HT and VHT
parameters.

Signed-off-by: Andrei Otcheretianski <andrei.otcheretianski@intel.com>
src/common/ieee802_11_common.c
src/common/ieee802_11_common.h
src/p2p/p2p_utils.c

index 063de6a..d07a316 100644 (file)
@@ -568,26 +568,139 @@ int hostapd_config_wmm_ac(struct hostapd_wmm_ac_params wmm_ac_params[],
 
 enum hostapd_hw_mode ieee80211_freq_to_chan(int freq, u8 *channel)
 {
-       enum hostapd_hw_mode mode = NUM_HOSTAPD_MODES;
+       u8 op_class;
+
+       return ieee80211_freq_to_channel_ext(freq, 0, 0, &op_class, channel);
+}
+
+
+/**
+ * ieee80211_freq_to_channel_ext - Convert frequency into channel info
+ * for HT40 and VHT. DFS channels are not covered.
+ * @freq: Frequency (MHz) to convert
+ * @sec_channel: 0 = non-HT40, 1 = sec. channel above, -1 = sec. channel below
+ * @vht: 0 - non-VHT, 1 - 80 MHz
+ * @op_class: Buffer for returning operating class
+ * @channel: Buffer for returning channel number
+ * Returns: hw_mode on success, NUM_HOSTAPD_MODES on failure
+ */
+enum hostapd_hw_mode ieee80211_freq_to_channel_ext(unsigned int freq,
+                                                  int sec_channel, int vht,
+                                                  u8 *op_class, u8 *channel)
+{
+       /* TODO: more operating classes */
+
+       if (sec_channel > 1 || sec_channel < -1)
+               return NUM_HOSTAPD_MODES;
 
        if (freq >= 2412 && freq <= 2472) {
-               mode = HOSTAPD_MODE_IEEE80211G;
+               if ((freq - 2407) % 5)
+                       return NUM_HOSTAPD_MODES;
+
+               if (vht)
+                       return NUM_HOSTAPD_MODES;
+
+               /* 2.407 GHz, channels 1..13 */
+               if (sec_channel == 1)
+                       *op_class = 83;
+               else if (sec_channel == -1)
+                       *op_class = 84;
+               else
+                       *op_class = 81;
+
                *channel = (freq - 2407) / 5;
-       } else if (freq == 2484) {
-               mode = HOSTAPD_MODE_IEEE80211B;
+
+               return HOSTAPD_MODE_IEEE80211G;
+       }
+
+       if (freq == 2484) {
+               if (sec_channel || vht)
+                       return NUM_HOSTAPD_MODES;
+
+               *op_class = 82; /* channel 14 */
                *channel = 14;
-       } else if (freq >= 4900 && freq < 5000) {
-               mode = HOSTAPD_MODE_IEEE80211A;
+
+               return HOSTAPD_MODE_IEEE80211B;
+       }
+
+       if (freq >= 4900 && freq < 5000) {
+               if ((freq - 4000) % 5)
+                       return NUM_HOSTAPD_MODES;
                *channel = (freq - 4000) / 5;
-       } else if (freq >= 5000 && freq < 5900) {
-               mode = HOSTAPD_MODE_IEEE80211A;
+               *op_class = 0; /* TODO */
+               return HOSTAPD_MODE_IEEE80211A;
+       }
+
+       /* 5 GHz, channels 36..48 */
+       if (freq >= 5180 && freq <= 5240) {
+               if ((freq - 5000) % 5)
+                       return NUM_HOSTAPD_MODES;
+
+               if (sec_channel == 1)
+                       *op_class = 116;
+               else if (sec_channel == -1)
+                       *op_class = 117;
+               else if (vht)
+                       *op_class = 128;
+               else
+                       *op_class = 115;
+
                *channel = (freq - 5000) / 5;
-       } else if (freq >= 56160 + 2160 * 1 && freq <= 56160 + 2160 * 4) {
-               mode = HOSTAPD_MODE_IEEE80211AD;
+
+               return HOSTAPD_MODE_IEEE80211A;
+       }
+
+       /* 5 GHz, channels 149..161 */
+       if (freq >= 5745 && freq <= 5805) {
+               if ((freq - 5000) % 5)
+                       return NUM_HOSTAPD_MODES;
+
+               if (sec_channel == 1)
+                       *op_class = 126;
+               else if (sec_channel == -1)
+                       *op_class = 127;
+               else if (vht)
+                       *op_class = 128;
+               else
+                       *op_class = 124;
+
+               *channel = (freq - 5000) / 5;
+
+               return HOSTAPD_MODE_IEEE80211A;
+       }
+
+       /* 5 GHz, channels 149..169 */
+       if (freq >= 5745 && freq <= 5845) {
+               if ((freq - 5000) % 5)
+                       return NUM_HOSTAPD_MODES;
+
+               *op_class = 125;
+
+               *channel = (freq - 5000) / 5;
+
+               return HOSTAPD_MODE_IEEE80211A;
+       }
+
+       if (freq >= 5000 && freq < 5900) {
+               if ((freq - 5000) % 5)
+                       return NUM_HOSTAPD_MODES;
+               *channel = (freq - 5000) / 5;
+               *op_class = 0; /* TODO */
+               return HOSTAPD_MODE_IEEE80211A;
+       }
+
+       /* 56.16 GHz, channel 1..4 */
+       if (freq >= 56160 + 2160 * 1 && freq <= 56160 + 2160 * 4) {
+               if (sec_channel || vht)
+                       return NUM_HOSTAPD_MODES;
+
                *channel = (freq - 56160) / 2160;
+               *op_class = 180;
+
+               return HOSTAPD_MODE_IEEE80211AD;
        }
 
-       return mode;
+       return NUM_HOSTAPD_MODES;
 }
 
 
index 45cf121..55ce022 100644 (file)
@@ -114,6 +114,9 @@ int hostapd_config_wmm_ac(struct hostapd_wmm_ac_params wmm_ac_params[],
                          const char *name, const char *val);
 enum hostapd_hw_mode ieee80211_freq_to_chan(int freq, u8 *channel);
 int ieee80211_chan_to_freq(const char *country, u8 op_class, u8 chan);
+enum hostapd_hw_mode ieee80211_freq_to_channel_ext(unsigned int freq,
+                                                  int sec_channel, int vht,
+                                                  u8 *op_class, u8 *channel);
 int ieee80211_is_dfs(int freq);
 
 int supp_rates_11b_only(struct ieee802_11_elems *elems);
index eee3c5a..e139d03 100644 (file)
@@ -9,6 +9,7 @@
 #include "includes.h"
 
 #include "common.h"
+#include "common/defs.h"
 #include "common/ieee802_11_common.h"
 #include "p2p_i.h"
 
@@ -67,59 +68,11 @@ int p2p_channel_to_freq(int op_class, int channel)
  */
 int p2p_freq_to_channel(unsigned int freq, u8 *op_class, u8 *channel)
 {
-       /* TODO: more operating classes */
-       if (freq >= 2412 && freq <= 2472) {
-               if ((freq - 2407) % 5)
-                       return -1;
-
-               *op_class = 81; /* 2.407 GHz, channels 1..13 */
-               *channel = (freq - 2407) / 5;
-               return 0;
-       }
-
-       if (freq == 2484) {
-               *op_class = 82; /* channel 14 */
-               *channel = 14;
-               return 0;
-       }
-
-       if (freq >= 5180 && freq <= 5240) {
-               if ((freq - 5000) % 5)
-                       return -1;
-
-               *op_class = 115; /* 5 GHz, channels 36..48 */
-               *channel = (freq - 5000) / 5;
-               return 0;
-       }
-
-       if (freq >= 5745 && freq <= 5805) {
-               if ((freq - 5000) % 5)
-                       return -1;
-
-               *op_class = 124; /* 5 GHz, channels 149..161 */
-               *channel = (freq - 5000) / 5;
-               return 0;
-       }
-
-       if (freq >= 5745 && freq <= 5845) {
-               if ((freq - 5000) % 5)
-                       return -1;
-
-               *op_class = 125; /* 5 GHz, channels 149..169 */
-               *channel = (freq - 5000) / 5;
-               return 0;
-       }
-
-       if (freq >= 58320 && freq <= 64800) {
-               if ((freq - 58320) % 2160)
-                       return -1;
-
-               *op_class = 180; /* 60 GHz, channels 1..4 */
-               *channel = (freq - 56160) / 2160;
-               return 0;
-       }
+       if (ieee80211_freq_to_channel_ext(freq, 0, 0, op_class, channel) ==
+           NUM_HOSTAPD_MODES)
+               return -1;
 
-       return -1;
+       return 0;
 }