regen pidl all: rm epan/dissectors/pidl/*-stamp; pushd epan/dissectors/pidl/ && make...
[wireshark-sm.git] / wsutil / 802_11-utils.c
blob2ad5da81a49af2c3c5ce0386f2ff9463b46439f9
1 /* 802_11-utils.c
2 * 802.11 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-or-later
9 */
11 #include "config.h"
12 #include "802_11-utils.h"
13 #include <wsutil/array.h>
15 typedef struct freq_cvt_s {
16 unsigned fmin; /* Minimum frequency in MHz */
17 unsigned fmax; /* Maximum frequency in MHz */
18 int cmin; /* Minimum/base channel */
19 bool is_bg; /* B/G channel? */
20 } freq_cvt_t;
22 #define FREQ_STEP 5 /* MHz. This seems to be consistent, thankfully */
25 * XXX - Japanese channels 182 through 196 actually have center
26 * frequencies that are off by 2.5 MHz from these values, according
27 * to the IEEE standard, although the table in ARIB STD T-71 version 5.2:
29 * http://www.arib.or.jp/english/html/overview/doc/1-STD-T71v5_2.pdf
31 * section 5.3.8.3.3 doesn't show that.
33 * XXX - what about the U.S. public safety 4.9 GHz band?
35 * XXX - what about 802.11ad?
37 static freq_cvt_t freq_cvt[] = {
38 { 2412, 2472, 1, true }, /* IEEE Std 802.11-2020: Section 15.4.4.3 and Annex E */
39 { 2484, 2484, 14, true }, /* IEEE Std 802.11-2020: Section 15.4.4.3 and Annex E */
40 { 5000, 5925, 0, false }, /* IEEE Std 802.11-2020: Annex E */
41 { 5950, 7125, 0, false }, /* IEEE Std 802.11ax-2021: Annex E */
42 { 4910, 4980, 182, false },
45 #define NUM_FREQ_CVT array_length(freq_cvt)
46 #define MAX_CHANNEL(fc) ( (int) ((fc.fmax - fc.fmin) / FREQ_STEP) + fc.cmin )
49 * Get channel number given a Frequency
51 int
52 ieee80211_mhz_to_chan(unsigned freq) {
53 unsigned i;
55 for (i = 0; i < NUM_FREQ_CVT; i++) {
56 if (freq >= freq_cvt[i].fmin && freq <= freq_cvt[i].fmax) {
57 return ((freq - freq_cvt[i].fmin) / FREQ_STEP) + freq_cvt[i].cmin;
60 return -1;
64 * Get Frequency given a Channel number
66 * XXX - Because channel numbering schemes for 2.4 and 5 overlap with 6 GHz,
67 * this function may not return the correct channel. For example, the frequency
68 * for channel 1 in 2.4 GHz band is 2412 MHz, while the frequency for channel 1
69 * in the 6 GHz band is 5955 MHz. To resolve this problem, this function needs
70 * to take a starting frequency to convert channel to frequencies correctly.
71 * Unfortunately, this is not possible in some cases, so for now, the order on
72 * which frequency ranges are defined will favor 2.4 and 5 GHz over 6 GHz.
74 unsigned
75 ieee80211_chan_to_mhz(int chan, bool is_bg) {
76 unsigned i;
78 for (i = 0; i < NUM_FREQ_CVT; i++) {
79 if (is_bg == freq_cvt[i].is_bg &&
80 chan >= freq_cvt[i].cmin && chan <= MAX_CHANNEL(freq_cvt[i])) {
81 return ((chan - freq_cvt[i].cmin) * FREQ_STEP) + freq_cvt[i].fmin;
84 return 0;
88 * Get Frequency given a Channel number and band.
90 unsigned
91 ieee80211_chan_band_to_mhz(int chan, bool is_bg, bool is_6ghz) {
92 unsigned i;
94 int start_idx = 0;
95 if (is_6ghz) {
96 start_idx = 3;
98 for (i = start_idx; i < NUM_FREQ_CVT; i++) {
99 if (is_bg == freq_cvt[i].is_bg &&
100 chan >= freq_cvt[i].cmin && chan <= MAX_CHANNEL(freq_cvt[i])) {
101 return ((chan - freq_cvt[i].cmin) * FREQ_STEP) + freq_cvt[i].fmin;
104 return 0;
108 * Get channel representation string given a Frequency
110 char*
111 ieee80211_mhz_to_str(unsigned freq){
112 int chan = ieee80211_mhz_to_chan(freq);
113 const char* band;
114 if (FREQ_IS_BG(freq)) {
115 band = "2.4 GHz";
116 } else if (FREQ_IS_6G(freq)) {
117 band = "6 GHz";
118 } else {
119 band = "5 GHz";
122 if (chan < 0) {
123 return ws_strdup_printf("%u", freq);
124 } else {
125 return ws_strdup_printf("%u [%s %u]", freq, band,
126 chan);
131 * Editor modelines
133 * Local Variables:
134 * c-basic-offset: 4
135 * tab-width: 8
136 * indent-tabs-mode: nil
137 * End:
139 * ex: set shiftwidth=4 tabstop=8 expandtab:
140 * :indentSize=4:tabSize=8:noTabs=true: