xref: /freebsd/tools/tools/net80211/wlantxtime/wlantxtime.c (revision 1d386b48a555f61cb7325543adbbb5c3f3407a66)
1 /*-
2  * Copyright (c) 2007-2009 Sam Leffler, Errno Consulting
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 
26 #include <sys/cdefs.h>
27 /*
28  * IEEE 802.11 PHY-related support.
29  */
30 
31 #include <sys/param.h>
32 
33 #include <net/if_llc.h>
34 
35 #include <net80211/_ieee80211.h>
36 #include <net80211/ieee80211.h>
37 
38 #define	IEEE80211_F_SHPREAMBLE	0x00040000	/* STATUS: use short preamble */
39 
40 #include <err.h>
41 #include <stdio.h>
42 #include <stdarg.h>
43 #include <stdlib.h>
44 #include <strings.h>
45 #include <unistd.h>
46 
47 struct ieee80211_rate_table {
48 	int		rateCount;		/* NB: for proper padding */
49 	uint8_t		rateCodeToIndex[256];	/* back mapping */
50 	struct {
51 		uint8_t		phy;		/* CCK/OFDM/TURBO */
52 		uint32_t	rateKbps;	/* transfer rate in kbs */
53 		uint8_t		shortPreamble;	/* mask for enabling short
54 						 * preamble in CCK rate code */
55 		uint8_t		dot11Rate;	/* value for supported rates
56 						 * info element of MLME */
57 		uint8_t		ctlRateIndex;	/* index of next lower basic
58 						 * rate; used for dur. calcs */
59 		uint16_t	lpAckDuration;	/* long preamble ACK dur. */
60 		uint16_t	spAckDuration;	/* short preamble ACK dur. */
61 	} info[32];
62 };
63 
64 uint16_t
65 ieee80211_compute_duration(const struct ieee80211_rate_table *rt,
66 	uint32_t frameLen, uint16_t rate, int isShortPreamble);
67 
68 #define	KASSERT(c, msg) do {			\
69 	if (!(c)) {				\
70 		printf msg;			\
71 		putchar('\n');			\
72 		exit(-1);			\
73 	}					\
74 } while (0)
75 
76 static void
panic(const char * fmt,...)77 panic(const char *fmt, ...)
78 {
79 	va_list ap;
80 
81 	va_start(ap, fmt);
82 	vprintf(fmt, ap);
83 	va_end(ap);
84 	exit(-1);
85 }
86 
87 /* shorthands to compact tables for readability */
88 #define	OFDM	IEEE80211_T_OFDM
89 #define	CCK	IEEE80211_T_CCK
90 #define	TURBO	IEEE80211_T_TURBO
91 #define	HALF	IEEE80211_T_OFDM_HALF
92 #define	QUART	IEEE80211_T_OFDM_QUARTER
93 #define	PBCC	(IEEE80211_T_OFDM_QUARTER+1)		/* XXX */
94 #define	B(r)	(0x80 | r)
95 #define	Mb(x)	(x*1000)
96 
97 static struct ieee80211_rate_table ieee80211_11b_table = {
98     .rateCount = 4,		/* XXX no PBCC */
99     .info = {
100 /*                                   short            ctrl  */
101 /*                                Preamble  dot11Rate Rate */
102      [0] = { .phy = CCK,     1000,    0x00,      B(2),   0 },/*   1 Mb */
103      [1] = { .phy = CCK,     2000,    0x04,      B(4),   1 },/*   2 Mb */
104      [2] = { .phy = CCK,     5500,    0x04,     B(11),   1 },/* 5.5 Mb */
105      [3] = { .phy = CCK,    11000,    0x04,     B(22),   1 },/*  11 Mb */
106      [4] = { .phy = PBCC,   22000,    0x04,        44,   3 } /*  22 Mb */
107     },
108 };
109 
110 static struct ieee80211_rate_table ieee80211_11g_table = {
111     .rateCount = 12,
112     .info = {
113 /*                                   short            ctrl  */
114 /*                                Preamble  dot11Rate Rate */
115      [0] = { .phy = CCK,     1000,    0x00,      B(2),   0 },
116      [1] = { .phy = CCK,     2000,    0x04,      B(4),   1 },
117      [2] = { .phy = CCK,     5500,    0x04,     B(11),   2 },
118      [3] = { .phy = CCK,    11000,    0x04,     B(22),   3 },
119      [4] = { .phy = OFDM,    6000,    0x00,        12,   4 },
120      [5] = { .phy = OFDM,    9000,    0x00,        18,   4 },
121      [6] = { .phy = OFDM,   12000,    0x00,        24,   6 },
122      [7] = { .phy = OFDM,   18000,    0x00,        36,   6 },
123      [8] = { .phy = OFDM,   24000,    0x00,        48,   8 },
124      [9] = { .phy = OFDM,   36000,    0x00,        72,   8 },
125     [10] = { .phy = OFDM,   48000,    0x00,        96,   8 },
126     [11] = { .phy = OFDM,   54000,    0x00,       108,   8 }
127     },
128 };
129 
130 static struct ieee80211_rate_table ieee80211_11a_table = {
131     .rateCount = 8,
132     .info = {
133 /*                                   short            ctrl  */
134 /*                                Preamble  dot11Rate Rate */
135      [0] = { .phy = OFDM,    6000,    0x00,     B(12),   0 },
136      [1] = { .phy = OFDM,    9000,    0x00,        18,   0 },
137      [2] = { .phy = OFDM,   12000,    0x00,     B(24),   2 },
138      [3] = { .phy = OFDM,   18000,    0x00,        36,   2 },
139      [4] = { .phy = OFDM,   24000,    0x00,     B(48),   4 },
140      [5] = { .phy = OFDM,   36000,    0x00,        72,   4 },
141      [6] = { .phy = OFDM,   48000,    0x00,        96,   4 },
142      [7] = { .phy = OFDM,   54000,    0x00,       108,   4 }
143     },
144 };
145 
146 static struct ieee80211_rate_table ieee80211_half_table = {
147     .rateCount = 8,
148     .info = {
149 /*                                   short            ctrl  */
150 /*                                Preamble  dot11Rate Rate */
151      [0] = { .phy = HALF,    3000,    0x00,      B(6),   0 },
152      [1] = { .phy = HALF,    4500,    0x00,         9,   0 },
153      [2] = { .phy = HALF,    6000,    0x00,     B(12),   2 },
154      [3] = { .phy = HALF,    9000,    0x00,        18,   2 },
155      [4] = { .phy = HALF,   12000,    0x00,     B(24),   4 },
156      [5] = { .phy = HALF,   18000,    0x00,        36,   4 },
157      [6] = { .phy = HALF,   24000,    0x00,        48,   4 },
158      [7] = { .phy = HALF,   27000,    0x00,        54,   4 }
159     },
160 };
161 
162 static struct ieee80211_rate_table ieee80211_quarter_table = {
163     .rateCount = 8,
164     .info = {
165 /*                                   short            ctrl  */
166 /*                                Preamble  dot11Rate Rate */
167      [0] = { .phy = QUART,   1500,    0x00,      B(3),   0 },
168      [1] = { .phy = QUART,   2250,    0x00,         4,   0 },
169      [2] = { .phy = QUART,   3000,    0x00,      B(9),   2 },
170      [3] = { .phy = QUART,   4500,    0x00,         9,   2 },
171      [4] = { .phy = QUART,   6000,    0x00,     B(12),   4 },
172      [5] = { .phy = QUART,   9000,    0x00,        18,   4 },
173      [6] = { .phy = QUART,  12000,    0x00,        24,   4 },
174      [7] = { .phy = QUART,  13500,    0x00,        27,   4 }
175     },
176 };
177 
178 static struct ieee80211_rate_table ieee80211_turbog_table = {
179     .rateCount = 7,
180     .info = {
181 /*                                   short            ctrl  */
182 /*                                Preamble  dot11Rate Rate */
183      [0] = { .phy = TURBO,   12000,   0x00,     B(12),   0 },
184      [1] = { .phy = TURBO,   24000,   0x00,     B(24),   1 },
185      [2] = { .phy = TURBO,   36000,   0x00,        36,   1 },
186      [3] = { .phy = TURBO,   48000,   0x00,     B(48),   3 },
187      [4] = { .phy = TURBO,   72000,   0x00,        72,   3 },
188      [5] = { .phy = TURBO,   96000,   0x00,        96,   3 },
189      [6] = { .phy = TURBO,  108000,   0x00,       108,   3 }
190     },
191 };
192 
193 static struct ieee80211_rate_table ieee80211_turboa_table = {
194     .rateCount = 8,
195     .info = {
196 /*                                   short            ctrl  */
197 /*                                Preamble  dot11Rate Rate */
198      [0] = { .phy = TURBO,   12000,   0x00,     B(12),   0 },
199      [1] = { .phy = TURBO,   18000,   0x00,        18,   0 },
200      [2] = { .phy = TURBO,   24000,   0x00,     B(24),   2 },
201      [3] = { .phy = TURBO,   36000,   0x00,        36,   2 },
202      [4] = { .phy = TURBO,   48000,   0x00,     B(48),   4 },
203      [5] = { .phy = TURBO,   72000,   0x00,        72,   4 },
204      [6] = { .phy = TURBO,   96000,   0x00,        96,   4 },
205      [7] = { .phy = TURBO,  108000,   0x00,       108,   4 }
206     },
207 };
208 
209 #undef	Mb
210 #undef	B
211 #undef	OFDM
212 #undef	CCK
213 #undef	TURBO
214 #undef	XR
215 
216 /*
217  * Setup a rate table's reverse lookup table and fill in
218  * ack durations.  The reverse lookup tables are assumed
219  * to be initialized to zero (or at least the first entry).
220  * We use this as a key that indicates whether or not
221  * we've previously setup the reverse lookup table.
222  *
223  * XXX not reentrant, but shouldn't matter
224  */
225 static void
ieee80211_setup_ratetable(struct ieee80211_rate_table * rt)226 ieee80211_setup_ratetable(struct ieee80211_rate_table *rt)
227 {
228 #define	WLAN_CTRL_FRAME_SIZE \
229 	(sizeof(struct ieee80211_frame_ack) + IEEE80211_CRC_LEN)
230 
231 	int i;
232 
233 	for (i = 0; i < nitems(rt->rateCodeToIndex); i++)
234 		rt->rateCodeToIndex[i] = (uint8_t) -1;
235 	for (i = 0; i < rt->rateCount; i++) {
236 		uint8_t code = rt->info[i].dot11Rate;
237 		uint8_t cix = rt->info[i].ctlRateIndex;
238 		uint8_t ctl_rate = rt->info[cix].dot11Rate;
239 
240 		rt->rateCodeToIndex[code] = i;
241 		if (code & IEEE80211_RATE_BASIC) {
242 			/*
243 			 * Map w/o basic rate bit too.
244 			 */
245 			code &= IEEE80211_RATE_VAL;
246 			rt->rateCodeToIndex[code] = i;
247 		}
248 
249 		/*
250 		 * XXX for 11g the control rate to use for 5.5 and 11 Mb/s
251 		 *     depends on whether they are marked as basic rates;
252 		 *     the static tables are setup with an 11b-compatible
253 		 *     2Mb/s rate which will work but is suboptimal
254 		 *
255 		 * NB: Control rate is always less than or equal to the
256 		 *     current rate, so control rate's reverse lookup entry
257 		 *     has been installed and following call is safe.
258 		 */
259 		rt->info[i].lpAckDuration = ieee80211_compute_duration(rt,
260 			WLAN_CTRL_FRAME_SIZE, ctl_rate, 0);
261 		rt->info[i].spAckDuration = ieee80211_compute_duration(rt,
262 			WLAN_CTRL_FRAME_SIZE, ctl_rate, IEEE80211_F_SHPREAMBLE);
263 	}
264 
265 #undef WLAN_CTRL_FRAME_SIZE
266 }
267 
268 /* Setup all rate tables */
269 static void
ieee80211_phy_init(void)270 ieee80211_phy_init(void)
271 {
272 	static struct ieee80211_rate_table * const ratetables[] = {
273 		&ieee80211_half_table,
274 		&ieee80211_quarter_table,
275 		&ieee80211_11a_table,
276 		&ieee80211_11g_table,
277 		&ieee80211_turbog_table,
278 		&ieee80211_turboa_table,
279 		&ieee80211_turboa_table,
280 		&ieee80211_11a_table,
281 		&ieee80211_11g_table,
282 		&ieee80211_11b_table
283 	};
284 	unsigned int i;
285 
286 	for (i = 0; i < nitems(ratetables); ++i)
287 		ieee80211_setup_ratetable(ratetables[i]);
288 
289 }
290 #define CCK_SIFS_TIME		10
291 #define CCK_PREAMBLE_BITS	144
292 #define CCK_PLCP_BITS		48
293 
294 #define OFDM_SIFS_TIME		16
295 #define OFDM_PREAMBLE_TIME	20
296 #define OFDM_PLCP_BITS		22
297 #define OFDM_SYMBOL_TIME	4
298 
299 #define OFDM_HALF_SIFS_TIME	32
300 #define OFDM_HALF_PREAMBLE_TIME	40
301 #define OFDM_HALF_PLCP_BITS	22
302 #define OFDM_HALF_SYMBOL_TIME	8
303 
304 #define OFDM_QUARTER_SIFS_TIME 		64
305 #define OFDM_QUARTER_PREAMBLE_TIME	80
306 #define OFDM_QUARTER_PLCP_BITS		22
307 #define OFDM_QUARTER_SYMBOL_TIME	16
308 
309 #define TURBO_SIFS_TIME		8
310 #define TURBO_PREAMBLE_TIME	14
311 #define TURBO_PLCP_BITS		22
312 #define TURBO_SYMBOL_TIME	4
313 
314 #define	HT_L_STF	8
315 #define	HT_L_LTF	8
316 #define	HT_L_SIG	4
317 #define	HT_SIG		8
318 #define	HT_STF		4
319 #define	HT_LTF(n)	((n) * 4)
320 
321 /*
322  * Compute the time to transmit a frame of length frameLen bytes
323  * using the specified rate, phy, and short preamble setting.
324  * SIFS is included.
325  */
326 uint16_t
ieee80211_compute_duration(const struct ieee80211_rate_table * rt,uint32_t frameLen,uint16_t rate,int isShortPreamble)327 ieee80211_compute_duration(const struct ieee80211_rate_table *rt,
328 	uint32_t frameLen, uint16_t rate, int isShortPreamble)
329 {
330 	uint8_t rix = rt->rateCodeToIndex[rate];
331 	uint32_t bitsPerSymbol, numBits, numSymbols, phyTime, txTime;
332 	uint32_t kbps;
333 
334 	KASSERT(rix != (uint8_t)-1, ("rate %d has no info", rate));
335 	kbps = rt->info[rix].rateKbps;
336 	if (kbps == 0)			/* XXX bandaid for channel changes */
337 		return 0;
338 
339 	switch (rt->info[rix].phy) {
340 	case IEEE80211_T_CCK:
341 		phyTime		= CCK_PREAMBLE_BITS + CCK_PLCP_BITS;
342 		if (isShortPreamble && rt->info[rix].shortPreamble)
343 			phyTime >>= 1;
344 		numBits		= frameLen << 3;
345 		txTime		= CCK_SIFS_TIME + phyTime
346 				+ ((numBits * 1000)/kbps);
347 		break;
348 	case IEEE80211_T_OFDM:
349 		bitsPerSymbol	= (kbps * OFDM_SYMBOL_TIME) / 1000;
350 		KASSERT(bitsPerSymbol != 0, ("full rate bps"));
351 
352 		numBits		= OFDM_PLCP_BITS + (frameLen << 3);
353 		numSymbols	= howmany(numBits, bitsPerSymbol);
354 		txTime		= OFDM_SIFS_TIME
355 				+ OFDM_PREAMBLE_TIME
356 				+ (numSymbols * OFDM_SYMBOL_TIME);
357 		break;
358 	case IEEE80211_T_OFDM_HALF:
359 		bitsPerSymbol	= (kbps * OFDM_HALF_SYMBOL_TIME) / 1000;
360 		KASSERT(bitsPerSymbol != 0, ("1/4 rate bps"));
361 
362 		numBits		= OFDM_PLCP_BITS + (frameLen << 3);
363 		numSymbols	= howmany(numBits, bitsPerSymbol);
364 		txTime		= OFDM_HALF_SIFS_TIME
365 				+ OFDM_HALF_PREAMBLE_TIME
366 				+ (numSymbols * OFDM_HALF_SYMBOL_TIME);
367 		break;
368 	case IEEE80211_T_OFDM_QUARTER:
369 		bitsPerSymbol	= (kbps * OFDM_QUARTER_SYMBOL_TIME) / 1000;
370 		KASSERT(bitsPerSymbol != 0, ("1/2 rate bps"));
371 
372 		numBits		= OFDM_PLCP_BITS + (frameLen << 3);
373 		numSymbols	= howmany(numBits, bitsPerSymbol);
374 		txTime		= OFDM_QUARTER_SIFS_TIME
375 				+ OFDM_QUARTER_PREAMBLE_TIME
376 				+ (numSymbols * OFDM_QUARTER_SYMBOL_TIME);
377 		break;
378 	case IEEE80211_T_TURBO:
379 		/* we still save OFDM rates in kbps - so double them */
380 		bitsPerSymbol = ((kbps << 1) * TURBO_SYMBOL_TIME) / 1000;
381 		KASSERT(bitsPerSymbol != 0, ("turbo bps"));
382 
383 		numBits       = TURBO_PLCP_BITS + (frameLen << 3);
384 		numSymbols    = howmany(numBits, bitsPerSymbol);
385 		txTime        = TURBO_SIFS_TIME + TURBO_PREAMBLE_TIME
386 			      + (numSymbols * TURBO_SYMBOL_TIME);
387 		break;
388 	default:
389 		panic("%s: unknown phy %u (rate %u)\n", __func__,
390 		      rt->info[rix].phy, rate);
391 		break;
392 	}
393 	return txTime;
394 }
395 
396 uint32_t
ieee80211_compute_duration_ht(const struct ieee80211_rate_table * rt,uint32_t frameLen,uint16_t rate,int streams,int isht40,int isShortGI)397 ieee80211_compute_duration_ht(const struct ieee80211_rate_table *rt,
398 	uint32_t frameLen, uint16_t rate,
399 	int streams, int isht40, int isShortGI)
400 {
401 	static const uint16_t ht20_bps[16] = {
402 	    26, 52, 78, 104, 156, 208, 234, 260,
403 	    52, 104, 156, 208, 312, 416, 468, 520
404 	};
405 	static const uint16_t ht40_bps[16] = {
406 	    54, 108, 162, 216, 324, 432, 486, 540,
407 	    108, 216, 324, 432, 648, 864, 972, 1080,
408 	};
409 	uint32_t bitsPerSymbol, numBits, numSymbols, txTime;
410 
411 	KASSERT(rate & IEEE80211_RATE_MCS, ("not mcs %d", rate));
412 	KASSERT((rate &~ IEEE80211_RATE_MCS) < 16, ("bad mcs 0x%x", rate));
413 
414 	if (isht40)
415 		bitsPerSymbol = ht40_bps[rate & 0xf];
416 	else
417 		bitsPerSymbol = ht20_bps[rate & 0xf];
418 	numBits = OFDM_PLCP_BITS + (frameLen << 3);
419 	numSymbols = howmany(numBits, bitsPerSymbol);
420 	if (isShortGI)
421 		txTime = ((numSymbols * 18) + 4) / 5;	/* 3.6us */
422 	else
423 		txTime = numSymbols * 4;		/* 4us */
424 	return txTime + HT_L_STF + HT_L_LTF +
425 	    HT_L_SIG + HT_SIG + HT_STF + HT_LTF(streams);
426 }
427 
428 static const struct ieee80211_rate_table *
mode2table(const char * mode)429 mode2table(const char *mode)
430 {
431 	if (strcasecmp(mode, "half") == 0)
432 		return &ieee80211_half_table;
433 	else if (strcasecmp(mode, "quarter") == 0)
434 		return &ieee80211_quarter_table;
435 	else if (strcasecmp(mode, "hta") == 0)
436 		return &ieee80211_11a_table;	/* XXX */
437 	else if (strcasecmp(mode, "htg") == 0)
438 		return &ieee80211_11g_table;	/* XXX */
439 	else if (strcasecmp(mode, "108g") == 0)
440 		return &ieee80211_turbog_table;
441 	else if (strcasecmp(mode, "sturbo") == 0)
442 		return &ieee80211_turboa_table;
443 	else if (strcasecmp(mode, "turbo") == 0)
444 		return &ieee80211_turboa_table;
445 	else if (strcasecmp(mode, "11a") == 0)
446 		return &ieee80211_11a_table;
447 	else if (strcasecmp(mode, "11g") == 0)
448 		return &ieee80211_11g_table;
449 	else if (strcasecmp(mode, "11b") == 0)
450 		return &ieee80211_11b_table;
451 	else
452 		return NULL;
453 }
454 
455 const char *
srate(int rate)456 srate(int rate)
457 {
458 	static char buf[32];
459 	if (rate & 1)
460 		snprintf(buf, sizeof(buf), "%u.5", rate/2);
461 	else
462 		snprintf(buf, sizeof(buf), "%u", rate/2);
463 	return buf;
464 }
465 
466 static int
checkpreamble(const struct ieee80211_rate_table * rt,uint8_t rix,int isShortPreamble,int verbose)467 checkpreamble(const struct ieee80211_rate_table *rt, uint8_t rix,
468 	int isShortPreamble, int verbose)
469 {
470 	if (isShortPreamble) {
471 		if (rt->info[rix].phy != IEEE80211_T_CCK) {
472 			if (verbose)
473 				warnx("short preamble not meaningful, ignored");
474 			isShortPreamble = 0;
475 		} else if (!rt->info[rix].shortPreamble) {
476 			if (verbose)
477 				warnx("short preamble not meaningful with "
478 				    "rate %s, ignored",
479 				    srate(rt->info[rix].dot11Rate &~ IEEE80211_RATE_BASIC));
480 			isShortPreamble = 0;
481 		}
482 	}
483 	return isShortPreamble;
484 }
485 
486 static void
usage(const char * progname)487 usage(const char *progname)
488 {
489 	fprintf(stderr, "usage: %s [-a] [-l framelen] [-m mode] [-r rate] [-s]\n",
490 	    progname);
491 	fprintf(stderr, "-a             display calculations for all possible rates\n");
492 	fprintf(stderr, "-l framelen    length in bytes of 802.11 payload (default 1536)\n");
493 	fprintf(stderr, "-m 11a         calculate for 11a channel\n");
494 	fprintf(stderr, "-m 11b         calculate for 11b channel\n");
495 	fprintf(stderr, "-m 11g         calculate for 11g channel (default)\n");
496 	fprintf(stderr, "-m half        calculate for 1/2 width channel\n");
497 	fprintf(stderr, "-m quarter     calculate for 1/4 width channel\n");
498 	fprintf(stderr, "-m 108g        calculate for dynamic turbo 11g channel\n");
499 	fprintf(stderr, "-m sturbo      calculate for static turbo channel\n");
500 	fprintf(stderr, "-m turbo       calculate for dynamic turbo 11a channel\n");
501 	fprintf(stderr, "-r rate        IEEE rate code (default 54)\n");
502 	fprintf(stderr, "-s             short preamble (default long)\n");
503 	exit(0);
504 }
505 
506 int
main(int argc,char * argv[])507 main(int argc, char *argv[])
508 {
509 	const struct ieee80211_rate_table *rt;
510 	const char *mode;
511 	uint32_t frameLen;
512 	uint16_t rate;
513 	uint16_t time;
514 	uint8_t rix;
515 	int ch, allrates, isShortPreamble, isShort;
516 	float frate;
517 
518 	ieee80211_phy_init();
519 
520 	mode = "11g";
521 	isShortPreamble = 0;
522 	frameLen = 1500
523 		 + sizeof(struct ieee80211_frame)
524 		 + LLC_SNAPFRAMELEN
525 		 + IEEE80211_CRC_LEN
526 		 ;
527 	rate = 2*54;
528 	allrates = 0;
529 	while ((ch = getopt(argc, argv, "al:m:r:s")) != -1) {
530 		switch (ch) {
531 		case 'a':
532 			allrates = 1;
533 			break;
534 		case 'l':
535 			frameLen = strtoul(optarg, NULL, 0);
536 			break;
537 		case 'm':
538 			mode = optarg;
539 			break;
540 		case 'r':
541 			frate = atof(optarg);
542 			rate = (int) 2*frate;
543 			break;
544 		case 's':
545 			isShortPreamble = 1;
546 			break;
547 		default:
548 			usage(argv[0]);
549 			break;
550 		}
551 	}
552 	rt = mode2table(mode);
553 	if (rt == NULL)
554 		errx(-1, "unknown mode %s", mode);
555 	if (!allrates) {
556 		rix = rt->rateCodeToIndex[rate];
557 		if (rix == (uint8_t) -1)
558 			errx(-1, "rate %s not valid for mode %s", srate(rate), mode);
559 		isShort = checkpreamble(rt, rix, isShortPreamble, 1);
560 
561 		time = ieee80211_compute_duration(rt, frameLen, rate, isShort);
562 		printf("%u usec to send %u bytes @ %s Mb/s, %s preamble\n",
563 		    time, frameLen, srate(rate),
564 		    isShort ? "short" : "long");
565 	} else {
566 		for (rix = 0; rix < rt->rateCount; rix++) {
567 			rate = rt->info[rix].dot11Rate &~ IEEE80211_RATE_BASIC;
568 			isShort = checkpreamble(rt, rix, isShortPreamble, 0);
569 			time = ieee80211_compute_duration(rt, frameLen, rate,
570 			    isShort);
571 			printf("%u usec to send %u bytes @ %s Mb/s, %s preamble\n",
572 			    time, frameLen, srate(rate),
573 			    isShort ? "short" : "long");
574 		}
575 	}
576 	return 0;
577 }
578