Home | History | Annotate | Line # | Download | only in cnwctl
cnwctl.c revision 1.2
      1 /*	$NetBSD: cnwctl.c,v 1.2 1999/11/29 12:55:08 itojun Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1997 Berkeley Software Design, Inc.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that this notice is retained,
      9  * the conditions in the following notices are met, and terms applying
     10  * to contributors in the following notices also apply to Berkeley
     11  * Software Design, Inc.
     12  *
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *      This product includes software developed by
     21  *	Berkeley Software Design, Inc.
     22  * 4. Neither the name of the Berkeley Software Design, Inc. nor the names
     23  *    of its contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY BERKELEY SOFTWARE DESIGN, INC. ``AS IS'' AND
     27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29  * ARE DISCLAIMED.  IN NO EVENT SHALL BERKELEY SOFTWARE DESIGN, INC. BE LIABLE
     30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36  * SUCH DAMAGE.
     37  *
     38  *      PAO2 Id: cnwctl.c,v 1.1.1.1 1997/12/11 14:46:06 itojun Exp
     39  */
     40 
     41 #include <sys/types.h>
     42 #include <sys/param.h>
     43 #include <sys/cdefs.h>
     44 #include <sys/errno.h>
     45 #include <sys/socket.h>
     46 #include <sys/ioctl.h>
     47 
     48 #include <net/if.h>
     49 
     50 #include <dev/pcmcia/if_cnwioctl.h>
     51 
     52 #include <err.h>
     53 #include <stdio.h>
     54 #include <stdlib.h>
     55 #include <string.h>
     56 #include <time.h>
     57 #include <unistd.h>
     58 
     59 int
     60 main(int argc, char **argv)
     61 {
     62 	int c, domain, i, key, rate, sflag, Sflag, skt;
     63 	char *e, *interface;
     64 	struct ifreq ifr;
     65         struct cnwistats cnwis, onwis;
     66         struct cnwstatus cnws;
     67 	struct ttysize ts;
     68 
     69 	domain = -1;
     70 	key = -1;
     71 	Sflag = sflag = 0;
     72 	rate = 0;
     73 	interface = "cnw0";
     74 
     75 	while ((c = getopt(argc, argv, "d:i:k:sS")) != EOF)
     76 		switch (c) {
     77 		case 'd':
     78 			domain = strtol(optarg, &e, 0);
     79 			if (e == optarg || *e || domain & ~ 0x1ff)
     80 				errx(1, "%s: invalid domain", optarg);
     81 			break;
     82 		case 'i':
     83 			interface = optarg;
     84 			break;
     85 		case 'k':
     86 			key = strtol(optarg, &e, 0);
     87 			if (e == optarg || *e || key & ~ 0xffff)
     88 				errx(1, "%s: invalid scramble key", optarg);
     89 			break;
     90 		case 'S':
     91 			++Sflag;
     92 			break;
     93 		case 's':
     94 			++sflag;
     95 			break;
     96 		default: usage:
     97 			fprintf(stderr, "usage: cnwctl [-i interface] [-d domain] [-k key] [-sS [rate]]\n");
     98 			exit(1);
     99 		}
    100 
    101 	switch (argc - optind) {
    102 	case 0:
    103 		break;
    104 	case 1:
    105 		if (sflag == 0 && Sflag == 0)
    106 			goto usage;
    107 		if (sflag && Sflag)
    108 			errx(1, "only one of -s and -S may be specified with a rate");
    109 		rate = strtol(argv[optind], &e, 0);
    110 		if (e == optarg || *e || rate < 1)
    111 			errx(1, "%s: invalid rate", optarg);
    112 		break;
    113 	default:
    114 		goto usage;
    115 	}
    116 
    117         if ((skt = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
    118                 err(1, "socket(AF_INET, SOCK_DGRAM)");
    119 
    120 	if (key >= 0) {
    121 		memset(&ifr, 0, sizeof(ifr));
    122 		strncpy(ifr.ifr_name, interface, sizeof(ifr.ifr_name));
    123 		ifr.ifr_key = key;
    124 		if (ioctl(skt, SIOCSCNWKEY, (caddr_t)&ifr) < 0)
    125 			err(1, "SIOCSCNWKEY");
    126 	}
    127 
    128 	if (domain >= 0) {
    129 		memset(&ifr, 0, sizeof(ifr));
    130 		strncpy(ifr.ifr_name, interface, sizeof(ifr.ifr_name));
    131 		ifr.ifr_domain = domain;
    132 		if (ioctl(skt, SIOCSCNWDOMAIN, (caddr_t)&ifr) < 0)
    133 			err(1, "SIOCSCNWDOMAIN");
    134 	}
    135 
    136 	if (sflag == 0 && Sflag == 0)
    137 		exit (0);
    138 
    139 	if (Sflag) {
    140         	memset(&cnws, 0, sizeof(cnws));
    141 		strncpy(cnws.ifr.ifr_name, interface,
    142 		    sizeof(cnws.ifr.ifr_name));
    143 		if (ioctl(skt, SIOCGCNWSTATUS, (caddr_t)&cnws) < 0)
    144 			err(1, "SIOCGCNWSTATUS");
    145 	}
    146 
    147 	if (sflag) {
    148 		memset(&cnwis, 0, sizeof(cnwis));
    149 		strncpy(cnwis.ifr.ifr_name, interface,
    150 		    sizeof(cnwis.ifr.ifr_name));
    151 		if (ioctl(skt, SIOCGCNWSTATS, (caddr_t)&cnwis) < 0)
    152 			err(1, "SIOCGCNWSTATS");
    153 		cnwis.stats.nws_txretries[0] = 0;
    154 
    155 		for (i = 1; i < 16; ++i)
    156 			cnwis.stats.nws_txretries[0] +=
    157 			    cnwis.stats.nws_txretries[i] * i;
    158 	}
    159 
    160 	if (rate == 0 && sflag) {
    161 		memset(&ifr, 0, sizeof(ifr));
    162 		strncpy(ifr.ifr_name, interface, sizeof(ifr.ifr_name));
    163 		if (ioctl(skt, SIOCGCNWDOMAIN, (caddr_t)&ifr) < 0)
    164 			err(1, "SIOCGCNWDOMAIN");
    165 		printf("%s:\n     0x%03x domain\n", interface, ifr.ifr_domain);
    166 		printf("%qu rx\n", cnwis.stats.nws_rx);
    167 		printf("%qu rxoverflow\n", cnwis.stats.nws_rxoverflow);
    168 		printf("%qu rxoverrun\n", cnwis.stats.nws_rxoverrun);
    169 		printf("%qu rxcrcerror\n", cnwis.stats.nws_rxcrcerror);
    170 		printf("%qu rxframe\n", cnwis.stats.nws_rxframe);
    171 		printf("%qu rxerrors\n", cnwis.stats.nws_rxerrors);
    172 		printf("%qu rxavail\n", cnwis.stats.nws_rxavail);
    173 
    174 		printf("%qu tx\n", cnwis.stats.nws_tx);
    175 		printf("%qu txokay\n", cnwis.stats.nws_txokay);
    176 		printf("%qu txabort\n", cnwis.stats.nws_txabort);
    177 		printf("%qu txlostcd\n", cnwis.stats.nws_txlostcd);
    178 		printf("%qu txerrors\n", cnwis.stats.nws_txerrors);
    179 		printf("%qu txretries\n", cnwis.stats.nws_txretries[0]);
    180 		for (i = 1; i < 16; ++i)
    181 			if (cnwis.stats.nws_txretries[i])
    182 				printf("%10s %qu %dx retries\n", "",
    183 				    cnwis.stats.nws_txretries[i], i);
    184 	}
    185 
    186 	if (rate == 0 && Sflag) {
    187 		printf("      0x%02x link integrity field\n",
    188 		    cnws.data[0x4e]);
    189 		printf("      0x%02x connection quality\n",
    190 		    cnws.data[0x54]);
    191 		printf("      0x%02x spu\n",
    192 		    cnws.data[0x55]);
    193 		printf("      0x%02x link quality\n",
    194 		    cnws.data[0x56]);
    195 		printf("      0x%02x hhc\n",
    196 		    cnws.data[0x58]);
    197 		printf("      0x%02x mhs\n",
    198 		    cnws.data[0x6b]);
    199 		printf(" %04x %04x revision\n",
    200 		    *(u_short *)&cnws.data[0x66], *(u_short *)&cnws.data[0x68]);
    201 		printf("        %c%c id\n",
    202 		    cnws.data[0x6e], cnws.data[0x6f]);
    203 	}
    204 
    205 	if (rate == 0)
    206 		exit (0);
    207 
    208 	if (ioctl(0, TIOCGWINSZ, &ts) < 0)
    209 		ts.ts_lines = 24;
    210 	c = 0;
    211 
    212 	if (Sflag) for (;;) {
    213 		if (c-- == 0) {
    214 			printf("lif  cq spu  lq hhc mhs\n");
    215 			c = ts.ts_lines - 3;
    216 		}
    217 		printf(" %02x  %02x  %02x  %02x  %02x  %02x\n",
    218                     cnws.data[0x4e],
    219                     cnws.data[0x54],
    220                     cnws.data[0x55],
    221                     cnws.data[0x56],
    222                     cnws.data[0x58],
    223 		    cnws.data[0x6b]);
    224 		fflush(stdout);
    225 		if (ioctl(skt, SIOCGCNWSTATUS, (caddr_t)&cnws) < 0)
    226 			err(1, "SIOCGCNWSTATUS");
    227 		sleep (rate);
    228 	}
    229 
    230 	for (;;) {
    231 		if (c-- == 0) {
    232 			printf("%10s %10s %10s %10s %10s %10s\n",
    233 			    "tx-request", "tx-okay", "tx-error", "tx-retry",
    234 			    "rx", "rx-error");
    235 			c = ts.ts_lines - 3;
    236 			memset(&onwis, 0, sizeof(onwis));
    237 		}
    238 		printf("%10qu ", cnwis.stats.nws_tx - onwis.stats.nws_tx);
    239 		printf("%10qu ", cnwis.stats.nws_txokay - onwis.stats.nws_txokay);
    240 		printf("%10qu ", cnwis.stats.nws_txerrors - onwis.stats.nws_txerrors);
    241 		printf("%10qu ", cnwis.stats.nws_txretries[0] - onwis.stats.nws_txretries[0]);
    242 		printf("%10qu ", cnwis.stats.nws_rx - onwis.stats.nws_rx);
    243 		printf("%10qu\n", cnwis.stats.nws_rxerrors - onwis.stats.nws_rxerrors);
    244 		fflush(stdout);
    245 		sleep (rate);
    246 		onwis = cnwis;
    247 
    248 		if (ioctl(skt, SIOCGCNWSTATS, (caddr_t)&cnwis) < 0)
    249 			err(1, "SIOCGCNWSTATS");
    250 		cnwis.stats.nws_txretries[0] = 0;
    251 		for (i = 1; i < 16; ++i)
    252 			cnwis.stats.nws_txretries[0] +=
    253 			    cnwis.stats.nws_txretries[i] * i;
    254 	}
    255 }
    256