Move zlib version check to wsutil
[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 /*
39  * From IEEE Std 802.11-2012:
40  *
41  *    section 16.4.6.3 "Channel Numbering of operating channels";
42  *
43  *    section 17.4.6.3 "Channel Numbering of operating channels";
44  *
45  *    section 18.3.8.4.2 "Channel numbering";
46  *
47  *    Annex E.
48  *
49  * XXX - Japanese channels 182 through 196 actually have center
50  * frequencies that are off by 2.5 MHz from these values, according
51  * to the IEEE standard, although the table in ARIB STD T-71 version 5.2:
52  *
53  *     http://www.arib.or.jp/english/html/overview/doc/1-STD-T71v5_2.pdf
54  *
55  * section 5.3.8.3.3 doesn't show that.
56  *
57  * XXX - what about the U.S. public safety 4.9 GHz band?
58  *
59  * XXX - what about 802.11ad?
60  */
61 static freq_cvt_t freq_cvt[] = {
62     { 2412, 2472,   1, TRUE },
63     { 2484, 2484,  14, TRUE },
64     { 5000, 5995,   0, FALSE },
65     { 4910, 4980, 182, FALSE }
66 };
67
68 #define NUM_FREQ_CVT (sizeof(freq_cvt) / sizeof(freq_cvt_t))
69 #define MAX_CHANNEL(fc) ( (gint) ((fc.fmax - fc.fmin) / FREQ_STEP) + fc.cmin )
70
71 /*
72  * Get channel number given a Frequency
73  */
74 gint
75 ieee80211_mhz_to_chan(guint freq) {
76     guint i;
77
78     for (i = 0; i < NUM_FREQ_CVT; i++) {
79         if (freq >= freq_cvt[i].fmin && freq <= freq_cvt[i].fmax) {
80             return ((freq - freq_cvt[i].fmin) / FREQ_STEP) + freq_cvt[i].cmin;
81         }
82     }
83     return -1;
84 }
85
86 /*
87  * Get Frequency given a Channel number
88  */
89 guint
90 ieee80211_chan_to_mhz(gint chan, gboolean is_bg) {
91     guint i;
92
93     for (i = 0; i < NUM_FREQ_CVT; i++) {
94         if (is_bg == freq_cvt[i].is_bg &&
95                 chan >= freq_cvt[i].cmin && chan <= MAX_CHANNEL(freq_cvt[i])) {
96             return ((chan - freq_cvt[i].cmin) * FREQ_STEP) + freq_cvt[i].fmin;
97         }
98     }
99     return 0;
100 }
101
102 /*
103  * Get channel representation string given a Frequency
104  */
105 gchar*
106 ieee80211_mhz_to_str(guint freq){
107     gint chan = ieee80211_mhz_to_chan(freq);
108     gboolean is_bg = FREQ_IS_BG(freq);
109
110     if (chan < 0) {
111         return g_strdup_printf("%u", freq);
112     } else {
113         return g_strdup_printf("%u [%s %u]", freq, is_bg ? "BG" : "A",
114             chan);
115     }
116 }
117
118 /*
119  * Editor modelines
120  *
121  * Local Variables:
122  * c-basic-offset: 4
123  * tab-width: 8
124  * indent-tabs-mode: nil
125  * End:
126  *
127  * ex: set shiftwidth=4 tabstop=8 expandtab:
128  * :indentSize=4:tabSize=8:noTabs=true:
129  */