2 * Frequency conversion utility definitions
4 * Wireshark - Network traffic analyzer
5 * By Gerald Combs <gerald@wireshark.org>
6 * Copyright 2007 Gerald Combs
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include "frequency-utils.h"
29 typedef struct freq_cvt_s {
30 guint fmin; /* Minimum frequency in MHz */
31 guint fmax; /* Maximum frequency in MHz */
32 gint cmin; /* Minimum/base channel */
33 gboolean is_bg; /* B/G channel? */
36 #define FREQ_STEP 5 /* MHz. This seems to be consistent, thankfully */
39 * From IEEE Std 802.11-2012:
41 * section 16.4.6.3 "Channel Numbering of operating channels";
43 * section 17.4.6.3 "Channel Numbering of operating channels";
45 * section 18.3.8.4.2 "Channel numbering";
49 * XXX - Japanese channels 182 through 196 actually have center
50 * frequencies that are off by 2.5 MHz from these values, according
51 * to the IEEE standard, although the table in ARIB STD T-71 version 5.2:
53 * http://www.arib.or.jp/english/html/overview/doc/1-STD-T71v5_2.pdf
55 * section 5.3.8.3.3 doesn't show that.
57 * XXX - what about the U.S. public safety 4.9 GHz band?
59 * XXX - what about 802.11ad?
61 static freq_cvt_t freq_cvt[] = {
62 { 2412, 2472, 1, TRUE },
63 { 2484, 2484, 14, TRUE },
64 { 5000, 5995, 0, FALSE },
65 { 4910, 4980, 182, FALSE }
68 #define NUM_FREQ_CVT (sizeof(freq_cvt) / sizeof(freq_cvt_t))
69 #define MAX_CHANNEL(fc) ( (gint) ((fc.fmax - fc.fmin) / FREQ_STEP) + fc.cmin )
72 * Get channel number given a Frequency
75 ieee80211_mhz_to_chan(guint freq) {
78 for (i = 0; i < NUM_FREQ_CVT; i++) {
79 if (freq >= freq_cvt[i].fmin && freq <= freq_cvt[i].fmax) {
80 return ((freq - freq_cvt[i].fmin) / FREQ_STEP) + freq_cvt[i].cmin;
87 * Get Frequency given a Channel number
90 ieee80211_chan_to_mhz(gint chan, gboolean is_bg) {
93 for (i = 0; i < NUM_FREQ_CVT; i++) {
94 if (is_bg == freq_cvt[i].is_bg &&
95 chan >= freq_cvt[i].cmin && chan <= MAX_CHANNEL(freq_cvt[i])) {
96 return ((chan - freq_cvt[i].cmin) * FREQ_STEP) + freq_cvt[i].fmin;
103 * Get channel representation string given a Frequency
106 ieee80211_mhz_to_str(guint freq){
107 gint chan = ieee80211_mhz_to_chan(freq);
108 gboolean is_bg = FREQ_IS_BG(freq);
111 return g_strdup_printf("%u", freq);
113 return g_strdup_printf("%u [%s %u]", freq, is_bg ? "BG" : "A",
124 * indent-tabs-mode: nil
127 * ex: set shiftwidth=4 tabstop=8 expandtab:
128 * :indentSize=4:tabSize=8:noTabs=true: