Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 30 Jul 2012 01:42:22 +0000 (UTC)
From:      Adrian Chadd <adrian@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r238893 - head/tools/tools/ath/athratestats
Message-ID:  <201207300142.q6U1gMgY033755@svn.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: adrian
Date: Mon Jul 30 01:42:22 2012
New Revision: 238893
URL: http://svn.freebsd.org/changeset/base/238893

Log:
  Break out the parsing code from main() and modularise things a little.
  
  The eventual aim will be to support listing "one" and "all" stations for
  the given ath device.

Modified:
  head/tools/tools/ath/athratestats/main.c

Modified: head/tools/tools/ath/athratestats/main.c
==============================================================================
--- head/tools/tools/ath/athratestats/main.c	Sun Jul 29 20:04:09 2012	(r238892)
+++ head/tools/tools/ath/athratestats/main.c	Mon Jul 30 01:42:22 2012	(r238893)
@@ -170,18 +170,69 @@ ath_rate_ioctl(struct ath_ratestats *r)
 		err(1, "ioctl");
 }
 
-int
-main(int argc, char *argv[])
+static int
+rate_node_stats(struct ath_ratestats *r, struct ether_addr *e)
 {
-	struct ath_ratestats r;
-	struct ether_addr *e;
-	uint8_t *buf;
 	struct ath_rateioctl_tlv *av;
 	struct sample_node *sn = NULL;
 	struct ath_rateioctl_rt *rt = NULL;
+	int error = 0;
+	uint8_t *buf = r->re.buf;
+
+	/*
+	 * For now, hard-code the TLV order and contents.  Ew!
+	 */
+	av = (struct ath_rateioctl_tlv *) buf;
+	if (av->tlv_id != ATH_RATE_TLV_RATETABLE) {
+		fprintf(stderr, "unexpected rate control TLV (got 0x%x, "
+		    "expected 0x%x\n",
+		    av->tlv_id,
+		    ATH_RATE_TLV_RATETABLE);
+		exit(127);
+	}
+	if (av->tlv_len != sizeof(struct ath_rateioctl_rt)) {
+		fprintf(stderr, "unexpected TLV len (got %d bytes, "
+		    "expected %d bytes\n",
+		    av->tlv_len,
+		    sizeof(struct ath_rateioctl_rt));
+		exit(127);
+	}
+	rt = (void *) (buf + sizeof(struct ath_rateioctl_tlv));
+
+	/* Next */
+	av = (void *) (buf + sizeof(struct ath_rateioctl_tlv) +
+	    sizeof(struct ath_rateioctl_rt));
+	if (av->tlv_id != ATH_RATE_TLV_SAMPLENODE) {
+		fprintf(stderr, "unexpected rate control TLV (got 0x%x, "
+		    "expected 0x%x\n",
+		    av->tlv_id,
+		    ATH_RATE_TLV_SAMPLENODE);
+		exit(127);
+	}
+	if (av->tlv_len != sizeof(struct sample_node)) {
+		fprintf(stderr, "unexpected TLV len (got %d bytes, "
+		    "expected %d bytes\n",
+		    av->tlv_len,
+		    sizeof(struct sample_node));
+		exit(127);
+	}
+	sn = (void *) (buf + sizeof(struct ath_rateioctl_tlv) +
+	    sizeof(struct ath_rateioctl_rt) +
+	    sizeof(struct ath_rateioctl_tlv));
+
+	ath_sample_stats(r, rt, sn);
+}
+
+
+int
+main(int argc, char *argv[])
+{
 	char const *ifname = NULL, *macaddr = NULL;
 	int c;
 	int do_all = 0;
+	struct ether_addr *e;
+	struct ath_ratestats r;
+	uint8_t *buf;
 
 	ifname = getenv("ATH");
 	if (ifname == NULL)
@@ -207,18 +258,6 @@ main(int argc, char *argv[])
 		}
 	}
 
-	buf = calloc(1, STATS_BUF_SIZE);
-	if (buf == NULL)
-		err(1, "calloc");
-
-	bzero(&r, sizeof(r));
-	r.s = socket(AF_INET, SOCK_DGRAM, 0);
-	if (r.s < 0) {
-		err(1, "socket");
-	}
-	/* XXX error check */
-	ath_setifname(&r, ifname);
-
 	if (macaddr == NULL) {
 		errx(1, "%s: macaddress wasn't supplied and no -a given\n",
 		    argv[0]);
@@ -228,53 +267,40 @@ main(int argc, char *argv[])
 	if (e == NULL)
 		err(1, "ether_aton");
 
+	bzero(&r, sizeof(r));
+
+	/*
+	 * Persistent buffer for each lookup
+	 */
+	buf = malloc(STATS_BUF_SIZE);
+	if (buf == NULL)
+		err(1, "calloc");
+
 	r.re.buf = buf;
 	r.re.len = STATS_BUF_SIZE;
 
-	ath_setsta(&r, e->octet);
-	ath_rate_ioctl(&r);
+	r.s = socket(AF_INET, SOCK_DGRAM, 0);
+	if (r.s < 0) {
+		err(1, "socket");
+	}
+	/* XXX error check */
+	ath_setifname(&r, ifname);
+
+	/* Zero the buffer before it's passed in */
+	memset(buf, '\0', STATS_BUF_SIZE);
 
 	/*
-	 * For now, hard-code the TLV order and contents.  Ew!
+	 * Set the station address for this lookup.
 	 */
-	av = (struct ath_rateioctl_tlv *) buf;
-	if (av->tlv_id != ATH_RATE_TLV_RATETABLE) {
-		fprintf(stderr, "unexpected rate control TLV (got 0x%x, "
-		    "expected 0x%x\n",
-		    av->tlv_id,
-		    ATH_RATE_TLV_RATETABLE);
-		exit(127);
-	}
-	if (av->tlv_len != sizeof(struct ath_rateioctl_rt)) {
-		fprintf(stderr, "unexpected TLV len (got %d bytes, "
-		    "expected %d bytes\n",
-		    av->tlv_len,
-		    sizeof(struct ath_rateioctl_rt));
-		exit(127);
-	}
-	rt = (void *) (buf + sizeof(struct ath_rateioctl_tlv));
+	ath_setsta(&r, e->octet);
 
-	/* Next */
-	av = (void *) (buf + sizeof(struct ath_rateioctl_tlv) +
-	    sizeof(struct ath_rateioctl_rt));
-	if (av->tlv_id != ATH_RATE_TLV_SAMPLENODE) {
-		fprintf(stderr, "unexpected rate control TLV (got 0x%x, "
-		    "expected 0x%x\n",
-		    av->tlv_id,
-		    ATH_RATE_TLV_SAMPLENODE);
-		exit(127);
-	}
-	if (av->tlv_len != sizeof(struct sample_node)) {
-		fprintf(stderr, "unexpected TLV len (got %d bytes, "
-		    "expected %d bytes\n",
-		    av->tlv_len,
-		    sizeof(struct sample_node));
-		exit(127);
-	}
-	sn = (void *) (buf + sizeof(struct ath_rateioctl_tlv) +
-	    sizeof(struct ath_rateioctl_rt) +
-	    sizeof(struct ath_rateioctl_tlv));
+	/*
+	 * Fetch the data from the driver.
+	 */
+	ath_rate_ioctl(&r);
 
-	ath_sample_stats(&r, rt, sn);
+	/*
+	 * Decode and parse statistics.
+	 */
+	rate_node_stats(&r, e);
 }
-



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201207300142.q6U1gMgY033755>