Expand PMF_FN_* macros.
[netbsd-mini2440.git] / usr.sbin / iwictl / iwictl.c
blob4809e3331aabf4ed6490532fba810143e8f59e33
1 /* $NetBSD: iwictl.c,v 1.7 2006/08/09 11:57:51 skrll Exp $ */
3 /*-
4 * Copyright (c) 2004, 2005
5 * Damien Bergamini <damien.bergamini@free.fr>. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice unmodified, this list of conditions, and the following
12 * disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
30 #include <sys/cdefs.h>
31 __RCSID("$NetBSD: iwictl.c,v 1.7 2006/08/09 11:57:51 skrll Exp $");
33 #include <sys/types.h>
34 #include <sys/ioctl.h>
35 #include <sys/mman.h>
36 #include <sys/socket.h>
37 #include <sys/stat.h>
39 #include <net/if.h>
41 #include <err.h>
42 #include <errno.h>
43 #include <fcntl.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <sysexits.h>
48 #include <unistd.h>
50 #define SIOCGRADIO _IOWR('i', 139, struct ifreq)
51 #define SIOCGTABLE0 _IOWR('i', 140, struct ifreq)
53 static void usage(void) __dead;
54 static int do_req(const char *, unsigned long, void *);
55 static void get_radio_state(const char *);
56 static void get_statistics(const char *);
58 int
59 main(int argc, char **argv)
61 int ch;
62 char *iface = NULL;
63 int noflag = 1, rflag = 0;
65 setprogname(argv[0]);
66 if (argc > 1 && argv[1][0] != '-') {
67 iface = argv[1];
68 optind++;
71 while ((ch = getopt(argc, argv, "i:r")) != -1) {
72 if (ch != 'i')
73 noflag = 0;
75 switch (ch) {
76 case 'i':
77 iface = optarg;
78 break;
80 case 'r':
81 rflag = 1;
82 break;
84 default:
85 usage();
89 if (iface == NULL)
90 usage();
92 if (rflag)
93 get_radio_state(iface);
95 if (noflag)
96 get_statistics(iface);
98 return EX_OK;
101 static void
102 usage(void)
104 (void)fprintf(stderr, "Usage: %s [-i] iface\n"
105 "\t%s [-i] iface -r\n", getprogname(), getprogname());
107 exit(EX_USAGE);
110 static int
111 do_req(const char *iface, unsigned long req, void *data)
113 int s;
114 struct ifreq ifr;
115 int error, serrno;
117 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
118 err(EX_OSERR, "Can't create socket");
120 (void)memset(&ifr, 0, sizeof(ifr));
121 (void)strncpy(ifr.ifr_name, iface, sizeof(ifr.ifr_name));
122 ifr.ifr_data = data;
123 error = ioctl(s, req, &ifr);
124 serrno = errno;
125 (void)close(s);
126 errno = serrno;
128 return error;
131 static void
132 get_radio_state(const char *iface)
134 int radio;
136 if (do_req(iface, SIOCGRADIO, &radio) == -1)
137 err(EX_OSERR, "Can't read radio");
139 (void)printf("Radio is %s\n", radio ? "ON" : "OFF");
142 struct statistic {
143 int index;
144 const char *desc;
147 static const struct statistic tbl[] = {
148 { 1, "Current transmission rate" },
149 { 2, "Fragmentation threshold" },
150 { 3, "RTS threshold" },
151 { 4, "Number of frames submitted for transfer" },
152 { 5, "Number of frames transmitted" },
153 { 6, "Number of unicast frames transmitted" },
154 { 7, "Number of unicast 802.11b frames transmitted at 1Mb/s" },
155 { 8, "Number of unicast 802.11b frames transmitted at 2Mb/s" },
156 { 9, "Number of unicast 802.11b frames transmitted at 5.5Mb/s" },
157 { 10, "Number of unicast 802.11b frames transmitted at 11Mb/s" },
159 { 19, "Number of unicast 802.11g frames transmitted at 1Mb/s" },
160 { 20, "Number of unicast 802.11g frames transmitted at 2Mb/s" },
161 { 21, "Number of unicast 802.11g frames transmitted at 5.5Mb/s" },
162 { 22, "Number of unicast 802.11g frames transmitted at 6Mb/s" },
163 { 23, "Number of unicast 802.11g frames transmitted at 9Mb/s" },
164 { 24, "Number of unicast 802.11g frames transmitted at 11Mb/s" },
165 { 25, "Number of unicast 802.11g frames transmitted at 12Mb/s" },
166 { 26, "Number of unicast 802.11g frames transmitted at 18Mb/s" },
167 { 27, "Number of unicast 802.11g frames transmitted at 24Mb/s" },
168 { 28, "Number of unicast 802.11g frames transmitted at 36Mb/s" },
169 { 29, "Number of unicast 802.11g frames transmitted at 48Mb/s" },
170 { 30, "Number of unicast 802.11g frames transmitted at 54Mb/s" },
171 { 31, "Number of multicast frames transmitted" },
172 { 32, "Number of multicast 802.11b frames transmitted at 1Mb/s" },
173 { 33, "Number of multicast 802.11b frames transmitted at 2Mb/s" },
174 { 34, "Number of multicast 802.11b frames transmitted at 5.5Mb/s" },
175 { 35, "Number of multicast 802.11b frames transmitted at 11Mb/s" },
177 { 44, "Number of multicast 802.11g frames transmitted at 1Mb/s" },
178 { 45, "Number of multicast 802.11g frames transmitted at 2Mb/s" },
179 { 46, "Number of multicast 802.11g frames transmitted at 5.5Mb/s" },
180 { 47, "Number of multicast 802.11g frames transmitted at 6Mb/s" },
181 { 48, "Number of multicast 802.11g frames transmitted at 9Mb/s" },
182 { 49, "Number of multicast 802.11g frames transmitted at 11Mb/s" },
183 { 50, "Number of multicast 802.11g frames transmitted at 12Mb/s" },
184 { 51, "Number of multicast 802.11g frames transmitted at 18Mb/s" },
185 { 52, "Number of multicast 802.11g frames transmitted at 24Mb/s" },
186 { 53, "Number of multicast 802.11g frames transmitted at 36Mb/s" },
187 { 54, "Number of multicast 802.11g frames transmitted at 48Mb/s" },
188 { 55, "Number of multicast 802.11g frames transmitted at 54Mb/s" },
189 { 56, "Number of transmission retries" },
190 { 57, "Number of transmission failures" },
191 { 58, "Number of frames with a bad CRC received" },
193 { 61, "Number of full scans" },
194 { 62, "Number of partial scans" },
196 { 64, "Number of bytes transmitted" },
197 { 65, "Current RSSI" },
198 { 66, "Number of beacons received" },
199 { 67, "Number of beacons missed" },
201 { 0, NULL }
204 static void
205 get_statistics(const char *iface)
207 static u_int32_t stats[256];
208 const struct statistic *st;
210 if (do_req(iface, SIOCGTABLE0, stats) == -1)
211 err(EX_OSERR, "Can't read statistics");
213 for (st = tbl; st->index != 0; st++)
214 (void)printf("%-60s[%u]\n", st->desc, stats[st->index]);