8361c02754d3bf631c96fdb6112bf2049dae78e1
[openwrt.git] / package / wprobe / src / user / wprobe-info.c
1 /*
2 * wprobe-test.c: Wireless probe user space test code
3 * Copyright (C) 2008-2009 Felix Fietkau <nbd@openwrt.org>
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 */
15
16 #include <stdio.h>
17 #include <string.h>
18 #include <stdlib.h>
19 #include <inttypes.h>
20 #include <errno.h>
21 #include <stdint.h>
22 #include <getopt.h>
23 #include <stdbool.h>
24 #include <unistd.h>
25 #include <sys/types.h>
26 #include <sys/socket.h>
27 #include <linux/wprobe.h>
28 #include "wprobe.h"
29
30 static const char *
31 wprobe_dump_value(struct wprobe_attribute *attr)
32 {
33 static char buf[128];
34
35 #define HANDLE_TYPE(_type, _format) \
36 case WPROBE_VAL_##_type: \
37 snprintf(buf, sizeof(buf), _format, attr->val._type); \
38 break
39
40 switch(attr->type) {
41 HANDLE_TYPE(S8, "%d");
42 HANDLE_TYPE(S16, "%d");
43 HANDLE_TYPE(S32, "%d");
44 HANDLE_TYPE(S64, "%lld");
45 HANDLE_TYPE(U8, "%d");
46 HANDLE_TYPE(U16, "%d");
47 HANDLE_TYPE(U32, "%d");
48 HANDLE_TYPE(U64, "%lld");
49 case WPROBE_VAL_STRING:
50 /* FIXME: implement this */
51 default:
52 strncpy(buf, "<unknown>", sizeof(buf));
53 break;
54 }
55 if ((attr->flags & WPROBE_F_KEEPSTAT) &&
56 (attr->val.n > 0)) {
57 int len = strlen(buf);
58 snprintf(buf + len, sizeof(buf) - len, " (avg: %.02f; stdev: %.02f, n=%d)", attr->val.avg, attr->val.stdev, attr->val.n);
59 }
60 #undef HANDLE_TYPE
61
62 return buf;
63 }
64
65
66 static void
67 wprobe_dump_data(struct wprobe_iface *dev)
68 {
69 struct wprobe_attribute *attr;
70 struct wprobe_link *link;
71 bool first = true;
72
73 fprintf(stderr, "\n");
74 wprobe_request_data(dev, NULL);
75 list_for_each_entry(attr, &dev->global_attr, list) {
76 fprintf(stderr, (first ?
77 "Global: %s=%s\n" :
78 " %s=%s\n"),
79 attr->name,
80 wprobe_dump_value(attr)
81 );
82 first = false;
83 }
84
85 list_for_each_entry(link, &dev->links, list) {
86 first = true;
87 wprobe_request_data(dev, link->addr);
88 list_for_each_entry(attr, &dev->link_attr, list) {
89 if (first) {
90 fprintf(stderr,
91 "%02x:%02x:%02x:%02x:%02x:%02x: %s=%s\n",
92 link->addr[0], link->addr[1], link->addr[2],
93 link->addr[3], link->addr[4], link->addr[5],
94 attr->name,
95 wprobe_dump_value(attr));
96 first = false;
97 } else {
98 fprintf(stderr,
99 " %s=%s\n",
100 attr->name,
101 wprobe_dump_value(attr));
102 }
103 }
104 }
105 }
106
107 static const char *attr_typestr[] = {
108 [0] = "Unknown",
109 [WPROBE_VAL_STRING] = "String",
110 [WPROBE_VAL_U8] = "Unsigned 8 bit",
111 [WPROBE_VAL_U16] = "Unsigned 16 bit",
112 [WPROBE_VAL_U32] = "Unsigned 32 bit",
113 [WPROBE_VAL_U64] = "Unsigned 64 bit",
114 [WPROBE_VAL_S8] = "Signed 8 bit",
115 [WPROBE_VAL_S16] = "Signed 16 bit",
116 [WPROBE_VAL_S32] = "Signed 32 bit",
117 [WPROBE_VAL_S64] = "Signed 64 bit",
118 };
119
120 static int usage(const char *prog)
121 {
122 fprintf(stderr,
123 "Usage: %s <interface> [options]\n"
124 "\n"
125 "Options:\n"
126 " -c: Only apply configuration\n"
127 " -h: This help text\n"
128 " -i <interval>: Set measurement interval\n"
129 " -m: Run measurement loop\n"
130 "\n"
131 , prog);
132 exit(1);
133 }
134
135 static void show_attributes(struct wprobe_iface *dev)
136 {
137 struct wprobe_attribute *attr;
138 list_for_each_entry(attr, &dev->global_attr, list) {
139 fprintf(stderr, "Global attribute: '%s' (%s)\n",
140 attr->name, attr_typestr[attr->type]);
141 }
142 list_for_each_entry(attr, &dev->link_attr, list) {
143 fprintf(stderr, "Link attribute: '%s' (%s)\n",
144 attr->name, attr_typestr[attr->type]);
145 }
146 }
147
148 static void loop_measurement(struct wprobe_iface *dev)
149 {
150 while (1) {
151 sleep(1);
152 wprobe_update_links(dev);
153 wprobe_dump_data(dev);
154 }
155 }
156
157 int main(int argc, char **argv)
158 {
159 struct wprobe_iface *dev;
160 const char *ifname;
161 const char *prog = argv[0];
162 enum {
163 CMD_NONE,
164 CMD_CONFIG,
165 CMD_MEASURE,
166 } cmd = CMD_NONE;
167 int ch;
168
169 if ((argc < 2) || (argv[1][0] == '-'))
170 return usage(prog);
171
172 ifname = argv[1];
173 dev = wprobe_get_dev(ifname);
174 argv++;
175 argc--;
176
177 if (!dev || (list_empty(&dev->global_attr) &&
178 list_empty(&dev->link_attr))) {
179 fprintf(stderr, "Interface '%s' not found\n", ifname);
180 return -1;
181 }
182
183 while ((ch = getopt(argc, argv, "chi:m")) != -1) {
184 switch(ch) {
185 case 'c':
186 cmd = CMD_CONFIG;
187 break;
188 case 'm':
189 cmd = CMD_MEASURE;
190 break;
191 case 'i':
192 dev->interval = strtoul(optarg, NULL, 10);
193 break;
194 case 'h':
195 default:
196 usage(prog);
197 break;
198 }
199 }
200
201 wprobe_apply_config(dev);
202 if (cmd != CMD_CONFIG)
203 show_attributes(dev);
204 if (cmd == CMD_MEASURE)
205 loop_measurement(dev);
206
207 wprobe_free_dev(dev);
208
209 return 0;
210 }
This page took 0.059827 seconds and 3 git commands to generate.