2 * Frequency conversion utility definitions
4 * Wireshark - Network traffic analyzer
5 * By Gerald Combs <gerald@wireshark.org>
6 * Copyright 2007 Gerald Combs
8 * SPDX-License-Identifier: GPL-2.0+
15 #include "frequency-utils.h"
17 typedef struct freq_cvt_s {
18 guint fmin; /* Minimum frequency in MHz */
19 guint fmax; /* Maximum frequency in MHz */
20 gint cmin; /* Minimum/base channel */
21 gboolean is_bg; /* B/G channel? */
24 #define FREQ_STEP 5 /* MHz. This seems to be consistent, thankfully */
27 * From IEEE Std 802.11-2012:
29 * section 16.4.6.3 "Channel Numbering of operating channels";
31 * section 17.4.6.3 "Channel Numbering of operating channels";
33 * section 18.3.8.4.2 "Channel numbering";
37 * XXX - Japanese channels 182 through 196 actually have center
38 * frequencies that are off by 2.5 MHz from these values, according
39 * to the IEEE standard, although the table in ARIB STD T-71 version 5.2:
41 * http://www.arib.or.jp/english/html/overview/doc/1-STD-T71v5_2.pdf
43 * section 5.3.8.3.3 doesn't show that.
45 * XXX - what about the U.S. public safety 4.9 GHz band?
47 * XXX - what about 802.11ad?
49 static freq_cvt_t freq_cvt[] = {
50 { 2412, 2472, 1, TRUE },
51 { 2484, 2484, 14, TRUE },
52 { 5000, 5995, 0, FALSE },
53 { 4910, 4980, 182, FALSE }
56 #define NUM_FREQ_CVT (sizeof(freq_cvt) / sizeof(freq_cvt_t))
57 #define MAX_CHANNEL(fc) ( (gint) ((fc.fmax - fc.fmin) / FREQ_STEP) + fc.cmin )
60 * Get channel number given a Frequency
63 ieee80211_mhz_to_chan(guint freq) {
66 for (i = 0; i < NUM_FREQ_CVT; i++) {
67 if (freq >= freq_cvt[i].fmin && freq <= freq_cvt[i].fmax) {
68 return ((freq - freq_cvt[i].fmin) / FREQ_STEP) + freq_cvt[i].cmin;
75 * Get Frequency given a Channel number
78 ieee80211_chan_to_mhz(gint chan, gboolean is_bg) {
81 for (i = 0; i < NUM_FREQ_CVT; i++) {
82 if (is_bg == freq_cvt[i].is_bg &&
83 chan >= freq_cvt[i].cmin && chan <= MAX_CHANNEL(freq_cvt[i])) {
84 return ((chan - freq_cvt[i].cmin) * FREQ_STEP) + freq_cvt[i].fmin;
91 * Get channel representation string given a Frequency
94 ieee80211_mhz_to_str(guint freq){
95 gint chan = ieee80211_mhz_to_chan(freq);
96 gboolean is_bg = FREQ_IS_BG(freq);
99 return g_strdup_printf("%u", freq);
101 return g_strdup_printf("%u [%s %u]", freq, is_bg ? "BG" : "A",
112 * indent-tabs-mode: nil
115 * ex: set shiftwidth=4 tabstop=8 expandtab:
116 * :indentSize=4:tabSize=8:noTabs=true: