2 * Frequency conversion utility definitions
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
8 * Copyright 2007 Gerald Combs
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
31 #include "frequency-utils.h"
33 typedef struct freq_cvt_s {
34 guint fmin; /* Minimum frequency in MHz */
35 guint fmax; /* Maximum frequency in MHz */
36 gint cmin; /* Minimum/base channel */
37 gboolean is_bg; /* B/G channel? */
40 #define FREQ_STEP 5 /* MHz. This seems to be consistent, thankfully */
42 /* From "802.11 Wireless Networks: The Definitive Guide", 2nd Ed. by Matthew Gast */
43 static freq_cvt_t freq_cvt[] = {
44 { 2412, 2472, 1, TRUE }, /* Table 12-1, p 257 */
45 { 2484, 2484, 14, TRUE }, /* Table 12-1, p 257 */
46 { 5000, 5995, 0, FALSE }, /* Table 13-1, p 289 */
47 { 4920, 4995, 240, FALSE } /* Table 13-1, p 289 */
50 #define NUM_FREQ_CVT (sizeof(freq_cvt) / sizeof(freq_cvt_t))
51 #define MAX_CHANNEL(fc) ( (gint) ((fc.fmax - fc.fmin) / FREQ_STEP) + fc.cmin )
54 * Get channel number given a Frequency
57 ieee80211_mhz_to_chan(guint freq) {
60 for (i = 0; i < NUM_FREQ_CVT; i++) {
61 if (freq >= freq_cvt[i].fmin && freq <= freq_cvt[i].fmax) {
62 return ((freq - freq_cvt[i].fmin) / FREQ_STEP) + freq_cvt[i].cmin;
69 * Get Frequency given a Channel number
72 ieee80211_chan_to_mhz(gint chan, gboolean is_bg) {
75 for (i = 0; i < NUM_FREQ_CVT; i++) {
76 if (is_bg == freq_cvt[i].is_bg &&
77 chan >= freq_cvt[i].cmin && chan <= MAX_CHANNEL(freq_cvt[i])) {
78 return ((chan - freq_cvt[i].cmin) * FREQ_STEP) + freq_cvt[i].fmin;
85 * Get channel representation string given a Frequency
88 ieee80211_mhz_to_str(guint freq){
89 gint chan = ieee80211_mhz_to_chan(freq);
90 gboolean is_bg = FREQ_IS_BG(freq);
93 return g_strdup_printf("%u", freq);
95 return g_strdup_printf("%u [%s %u]", freq, is_bg ? "BG" : "A",
106 * indent-tabs-mode: nil
109 * ex: set shiftwidth=4 tabstop=8 expandtab:
110 * :indentSize=4:tabSize=8:noTabs=true: