From Tyson Key:
[obnox/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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
23  */
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #include <glib.h>
30
31 #include "frequency-utils.h"
32
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? */
38 } freq_cvt_t;
39
40 #define FREQ_STEP 5     /* MHz. This seems to be consistent, thankfully */
41
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 */
48 };
49
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 )
52
53 /*
54  * Get channel number given a Frequency
55  */
56 gint
57 ieee80211_mhz_to_chan(guint freq) {
58     guint i;
59
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;
63         }
64     }
65     return -1;
66 }
67
68 /*
69  * Get Frequency given a Channel number
70  */
71 guint
72 ieee80211_chan_to_mhz(gint chan, gboolean is_bg) {
73     guint i;
74
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;
79         }
80     }
81     return 0;
82 }
83
84 /*
85  * Get channel representation string given a Frequency
86  */
87 gchar*
88 ieee80211_mhz_to_str(guint freq){
89     gint chan = ieee80211_mhz_to_chan(freq);
90     gboolean is_bg = FREQ_IS_BG(freq);
91
92     if (chan < 0) {
93         return g_strdup_printf("%u", freq);
94     } else {
95         return g_strdup_printf("%u [%s %u]", freq, is_bg ? "BG" : "A",
96             chan);
97     }
98 }
99
100 /*
101  * Editor modelines
102  *
103  * Local Variables:
104  * c-basic-offset: 4
105  * tab-width: 8
106  * indent-tabs-mode: nil
107  * End:
108  *
109  * ex: set shiftwidth=4 tabstop=8 expandtab:
110  * :indentSize=4:tabSize=8:noTabs=true:
111  */