1161200Ssam/*-
2186904Ssam * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
3161200Ssam * All rights reserved.
4161200Ssam *
5161200Ssam * Redistribution and use in source and binary forms, with or without
6161200Ssam * modification, are permitted provided that the following conditions
7161200Ssam * are met:
8161200Ssam * 1. Redistributions of source code must retain the above copyright
9161200Ssam *    notice, this list of conditions and the following disclaimer,
10161200Ssam *    without modification.
11161200Ssam * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12161200Ssam *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
13161200Ssam *    redistribution must be conditioned upon including a substantially
14161200Ssam *    similar Disclaimer requirement for further binary redistribution.
15161200Ssam *
16161200Ssam * NO WARRANTY
17161200Ssam * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18161200Ssam * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19161200Ssam * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
20161200Ssam * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21161200Ssam * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
22161200Ssam * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23161200Ssam * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24161200Ssam * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25161200Ssam * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26161200Ssam * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27161200Ssam * THE POSSIBILITY OF SUCH DAMAGES.
28161200Ssam *
29161200Ssam * $FreeBSD$
30161200Ssam */
31161200Ssam
32161200Ssam/*
33161200Ssam * Simple Atheros-specific tool to inspect and monitor network traffic
34161200Ssam * statistics.
35161200Ssam *
36189274Ssam *	athstats [-i interface] [-bz] [-l] [-o fmtstring] [interval]
37161200Ssam *
38161200Ssam * (default interface is ath0).  If interval is specified a rolling output
39161200Ssam * a la netstat -i is displayed every interval seconds.  The format of
40161200Ssam * the rolling display can be controlled a la ps.  The -l option will
41161200Ssam * print a list of all possible statistics for use with the -o option.
42161200Ssam */
43161200Ssam
44287297Srodrigc#include <sys/param.h>
45287297Srodrigc
46287297Srodrigc#include <err.h>
47287297Srodrigc#include <signal.h>
48161200Ssam#include <stdio.h>
49174571Ssam#include <stdlib.h>
50287297Srodrigc#include <string.h>
51161200Ssam#include <unistd.h>
52161200Ssam
53161200Ssam#include "athstats.h"
54161200Ssam
55184371Ssamstatic struct {
56184371Ssam	const char *tag;
57184371Ssam	const char *fmt;
58184371Ssam} tags[] = {
59184371Ssam  { "default",
60184371Ssam    "input,output,altrate,short,long,xretry,crcerr,crypt,phyerr,rssi,rate"
61184371Ssam  },
62184371Ssam  { "ani",
63184371Ssam    "avgbrssi,avgrssi,avgtxrssi,NI,SI,step,owsd,cwst,NI+,NI-,SI+,SI-,OFDM,CCK,LISTEN"
64184371Ssam  },
65186904Ssam  { "tdma",
66186904Ssam    "input,output,bexmit,tdmau,tdmadj,crcerr,phyerr,phytor,rssi,noise,rate"
67186904Ssam  },
68184371Ssam};
69161200Ssam
70184371Ssamstatic const char *
71184371Ssamgetfmt(const char *tag)
72184371Ssam{
73184371Ssam	int i;
74287297Srodrigc	for (i = 0; i < nitems(tags); i++)
75184371Ssam		if (strcasecmp(tags[i].tag, tag) == 0)
76184371Ssam			return tags[i].fmt;
77188205Ssam	return tag;
78184371Ssam}
79184371Ssam
80161200Ssamstatic int signalled;
81161200Ssam
82161200Ssamstatic void
83161200Ssamcatchalarm(int signo __unused)
84161200Ssam{
85161200Ssam	signalled = 1;
86161200Ssam}
87161200Ssam
88161200Ssamint
89161200Ssammain(int argc, char *argv[])
90161200Ssam{
91161200Ssam	struct athstatfoo *wf;
92174571Ssam	const char *ifname;
93189274Ssam	int c, banner = 1;
94161200Ssam
95174571Ssam	ifname = getenv("ATH");
96174571Ssam	if (ifname == NULL)
97295363Sadrian		ifname = ATH_DEFAULT;
98184371Ssam	wf = athstats_new(ifname, getfmt("default"));
99189274Ssam	while ((c = getopt(argc, argv, "bi:lo:z")) != -1) {
100161200Ssam		switch (c) {
101189274Ssam		case 'b':
102189274Ssam			banner = 0;
103189274Ssam			break;
104161200Ssam		case 'i':
105161200Ssam			wf->setifname(wf, optarg);
106161200Ssam			break;
107161200Ssam		case 'l':
108161200Ssam			wf->print_fields(wf, stdout);
109161200Ssam			return 0;
110161200Ssam		case 'o':
111184371Ssam			wf->setfmt(wf, getfmt(optarg));
112161200Ssam			break;
113188560Ssam		case 'z':
114188560Ssam			wf->zerostats(wf);
115188560Ssam			break;
116161200Ssam		default:
117188560Ssam			errx(-1, "usage: %s [-a] [-i ifname] [-l] [-o fmt] [-z] [interval]\n", argv[0]);
118161200Ssam			/*NOTREACHED*/
119161200Ssam		}
120161200Ssam	}
121161200Ssam	argc -= optind;
122161200Ssam	argv += optind;
123161200Ssam
124161200Ssam	if (argc > 0) {
125161200Ssam		u_long interval = strtoul(argv[0], NULL, 0);
126161200Ssam		int line, omask;
127161200Ssam
128161200Ssam		if (interval < 1)
129161200Ssam			interval = 1;
130161200Ssam		signal(SIGALRM, catchalarm);
131161200Ssam		signalled = 0;
132161200Ssam		alarm(interval);
133161200Ssam	banner:
134189274Ssam		if (banner)
135189274Ssam			wf->print_header(wf, stdout);
136161200Ssam		line = 0;
137161200Ssam	loop:
138161200Ssam		if (line != 0) {
139161200Ssam			wf->collect_cur(wf);
140161200Ssam			wf->print_current(wf, stdout);
141161200Ssam			wf->update_tot(wf);
142161200Ssam		} else {
143161200Ssam			wf->collect_tot(wf);
144161200Ssam			wf->print_total(wf, stdout);
145161200Ssam		}
146161200Ssam		fflush(stdout);
147161200Ssam		omask = sigblock(sigmask(SIGALRM));
148161200Ssam		if (!signalled)
149161200Ssam			sigpause(0);
150161200Ssam		sigsetmask(omask);
151161200Ssam		signalled = 0;
152161200Ssam		alarm(interval);
153161200Ssam		line++;
154161200Ssam		if (line == 21)		/* XXX tty line count */
155161200Ssam			goto banner;
156161200Ssam		else
157161200Ssam			goto loop;
158161200Ssam		/*NOTREACHED*/
159161200Ssam	} else {
160161200Ssam		wf->collect_tot(wf);
161161200Ssam		wf->print_verbose(wf, stdout);
162161200Ssam	}
163161200Ssam	return 0;
164161200Ssam}
165