Add the wireless toolbar.
[metze/wireshark/wip.git] / wsutil / frequency-utils.c
1 /* frequency-utils.c
2  * Frequency conversion utility definitions
3  *
4  * Wireshark - Network traffic analyzer
5  * By Gerald Combs <gerald@wireshark.org>
6  * Copyright 2007 Gerald Combs
7  *
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.
12  *
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.
17  *
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.
21  */
22
23 #include "config.h"
24
25 #include <glib.h>
26
27 #include "frequency-utils.h"
28
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? */
34 } freq_cvt_t;
35
36 #define FREQ_STEP 5     /* MHz. This seems to be consistent, thankfully */
37
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 */
44 };
45
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 )
48
49 /*
50  * Get channel number given a Frequency
51  */
52 gint
53 ieee80211_mhz_to_chan(guint freq) {
54     guint i;
55
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;
59         }
60     }
61     return -1;
62 }
63
64 /*
65  * Get Frequency given a Channel number
66  */
67 guint
68 ieee80211_chan_to_mhz(gint chan, gboolean is_bg) {
69     guint i;
70
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;
75         }
76     }
77     return 0;
78 }
79
80 /*
81  * Get channel representation string given a Frequency
82  */
83 gchar*
84 ieee80211_mhz_to_str(guint freq){
85     gint chan = ieee80211_mhz_to_chan(freq);
86     gboolean is_bg = FREQ_IS_BG(freq);
87
88     if (chan < 0) {
89         return g_strdup_printf("%u", freq);
90     } else {
91         return g_strdup_printf("%u [%s %u]", freq, is_bg ? "BG" : "A",
92             chan);
93     }
94 }
95
96 /*
97  * Editor modelines
98  *
99  * Local Variables:
100  * c-basic-offset: 4
101  * tab-width: 8
102  * indent-tabs-mode: nil
103  * End:
104  *
105  * ex: set shiftwidth=4 tabstop=8 expandtab:
106  * :indentSize=4:tabSize=8:noTabs=true:
107  */