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 */
38 /* From "802.11 Wireless Networks: The Definitive Guide", 2nd Ed. by Matthew Gast */
39 static freq_cvt_t freq_cvt[] = {
40 { 2412, 2472, 1, TRUE }, /* Table 12-1, p 257 */
41 { 2484, 2484, 14, TRUE }, /* Table 12-1, p 257 */
42 { 5000, 5995, 0, FALSE }, /* Table 13-1, p 289 */
43 { 4920, 4995, 240, FALSE } /* Table 13-1, p 289 */
46 #define NUM_FREQ_CVT (sizeof(freq_cvt) / sizeof(freq_cvt_t))
47 #define MAX_CHANNEL(fc) ( (gint) ((fc.fmax - fc.fmin) / FREQ_STEP) + fc.cmin )
50 * Get channel number given a Frequency
53 ieee80211_mhz_to_chan(guint freq) {
56 for (i = 0; i < NUM_FREQ_CVT; i++) {
57 if (freq >= freq_cvt[i].fmin && freq <= freq_cvt[i].fmax) {
58 return ((freq - freq_cvt[i].fmin) / FREQ_STEP) + freq_cvt[i].cmin;
65 * Get Frequency given a Channel number
68 ieee80211_chan_to_mhz(gint chan, gboolean is_bg) {
71 for (i = 0; i < NUM_FREQ_CVT; i++) {
72 if (is_bg == freq_cvt[i].is_bg &&
73 chan >= freq_cvt[i].cmin && chan <= MAX_CHANNEL(freq_cvt[i])) {
74 return ((chan - freq_cvt[i].cmin) * FREQ_STEP) + freq_cvt[i].fmin;
81 * Get channel representation string given a Frequency
84 ieee80211_mhz_to_str(guint freq){
85 gint chan = ieee80211_mhz_to_chan(freq);
86 gboolean is_bg = FREQ_IS_BG(freq);
89 return g_strdup_printf("%u", freq);
91 return g_strdup_printf("%u [%s %u]", freq, is_bg ? "BG" : "A",
102 * indent-tabs-mode: nil
105 * ex: set shiftwidth=4 tabstop=8 expandtab:
106 * :indentSize=4:tabSize=8:noTabs=true: