Witness: add pidl output
[wireshark-wip.git] / epan / frequency-utils.c
blob3cd27e3a52863229519130afc8e69d2e0b423989
1 /* frequency-utils.c
2 * Frequency conversion utility definitions
4 * $Id$
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 #include "config.h"
27 #include <glib.h>
29 #include "frequency-utils.h"
31 typedef struct freq_cvt_s {
32 guint fmin; /* Minimum frequency in MHz */
33 guint fmax; /* Maximum frequency in MHz */
34 gint cmin; /* Minimum/base channel */
35 gboolean is_bg; /* B/G channel? */
36 } freq_cvt_t;
38 #define FREQ_STEP 5 /* MHz. This seems to be consistent, thankfully */
40 /* From "802.11 Wireless Networks: The Definitive Guide", 2nd Ed. by Matthew Gast */
41 static freq_cvt_t freq_cvt[] = {
42 { 2412, 2472, 1, TRUE }, /* Table 12-1, p 257 */
43 { 2484, 2484, 14, TRUE }, /* Table 12-1, p 257 */
44 { 5000, 5995, 0, FALSE }, /* Table 13-1, p 289 */
45 { 4920, 4995, 240, FALSE } /* Table 13-1, p 289 */
48 #define NUM_FREQ_CVT (sizeof(freq_cvt) / sizeof(freq_cvt_t))
49 #define MAX_CHANNEL(fc) ( (gint) ((fc.fmax - fc.fmin) / FREQ_STEP) + fc.cmin )
52 * Get channel number given a Frequency
54 gint
55 ieee80211_mhz_to_chan(guint freq) {
56 guint i;
58 for (i = 0; i < NUM_FREQ_CVT; i++) {
59 if (freq >= freq_cvt[i].fmin && freq <= freq_cvt[i].fmax) {
60 return ((freq - freq_cvt[i].fmin) / FREQ_STEP) + freq_cvt[i].cmin;
63 return -1;
67 * Get Frequency given a Channel number
69 guint
70 ieee80211_chan_to_mhz(gint chan, gboolean is_bg) {
71 guint i;
73 for (i = 0; i < NUM_FREQ_CVT; i++) {
74 if (is_bg == freq_cvt[i].is_bg &&
75 chan >= freq_cvt[i].cmin && chan <= MAX_CHANNEL(freq_cvt[i])) {
76 return ((chan - freq_cvt[i].cmin) * FREQ_STEP) + freq_cvt[i].fmin;
79 return 0;
83 * Get channel representation string given a Frequency
85 gchar*
86 ieee80211_mhz_to_str(guint freq){
87 gint chan = ieee80211_mhz_to_chan(freq);
88 gboolean is_bg = FREQ_IS_BG(freq);
90 if (chan < 0) {
91 return g_strdup_printf("%u", freq);
92 } else {
93 return g_strdup_printf("%u [%s %u]", freq, is_bg ? "BG" : "A",
94 chan);
99 * Editor modelines
101 * Local Variables:
102 * c-basic-offset: 4
103 * tab-width: 8
104 * indent-tabs-mode: nil
105 * End:
107 * ex: set shiftwidth=4 tabstop=8 expandtab:
108 * :indentSize=4:tabSize=8:noTabs=true: