initialize tree variables
[metze/wireshark/wip.git] / epan / frequency-utils.c
1 /* frequency-utils.c
2  * Frequency conversion utility definitions
3  *
4  * $Id$
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
8  * Copyright 2007 Gerald Combs
9  *
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.
14  *
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.
19  *
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.
23  */
24
25 #include "config.h"
26
27 #include <glib.h>
28
29 #include "frequency-utils.h"
30
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;
37
38 #define FREQ_STEP 5     /* MHz. This seems to be consistent, thankfully */
39
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 */
46 };
47
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 )
50
51 /*
52  * Get channel number given a Frequency
53  */
54 gint
55 ieee80211_mhz_to_chan(guint freq) {
56     guint i;
57
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;
61         }
62     }
63     return -1;
64 }
65
66 /*
67  * Get Frequency given a Channel number
68  */
69 guint
70 ieee80211_chan_to_mhz(gint chan, gboolean is_bg) {
71     guint i;
72
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;
77         }
78     }
79     return 0;
80 }
81
82 /*
83  * Get channel representation string given a Frequency
84  */
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);
89
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);
95     }
96 }
97
98 /*
99  * Editor modelines
100  *
101  * Local Variables:
102  * c-basic-offset: 4
103  * tab-width: 8
104  * indent-tabs-mode: nil
105  * End:
106  *
107  * ex: set shiftwidth=4 tabstop=8 expandtab:
108  * :indentSize=4:tabSize=8:noTabs=true:
109  */