forked from openwrt/mt76
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
207 lines (161 loc) · 3.83 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// SPDX-License-Identifier: ISC
/* Copyright (C) 2020 Felix Fietkau <[email protected]> */
#define _GNU_SOURCE
#include <sys/types.h>
#include <sys/uio.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <unistd.h>
#include <stdio.h>
#include <stdbool.h>
#include <errno.h>
#include <poll.h>
#include <fcntl.h>
#include <signal.h>
#include "mt76-test.h"
struct unl unl;
static uint32_t tm_changed[DIV_ROUND_UP(NUM_MT76_TM_ATTRS, 32)];
static const char *progname;
static int phy_lookup_idx(const char *name)
{
char buf[128];
FILE *f;
int len;
snprintf(buf, sizeof(buf), "/sys/class/ieee80211/%s/index", name);
f = fopen(buf, "r");
if (!f)
return -1;
len = fread(buf, 1, sizeof(buf) - 1, f);
fclose(f);
if (!len)
return -1;
buf[len] = 0;
return atoi(buf);
}
void usage(void)
{
static const char *const commands[] = {
"set <var>=<val> [...]",
"dump [stats]",
"eeprom file",
"eeprom set <addr>=<val> [...]",
"eeprom changes",
"eeprom reset",
};
int i;
fprintf(stderr, "Usage:\n");
for (i = 0; i < ARRAY_SIZE(commands); i++)
printf(" %s phyX %s\n", progname, commands[i]);
exit(1);
}
static int mt76_dump_cb(struct nl_msg *msg, void *arg)
{
struct nlattr *attr;
attr = unl_find_attr(&unl, msg, NL80211_ATTR_TESTDATA);
if (!attr) {
fprintf(stderr, "Testdata attribute not found\n");
return NL_SKIP;
}
msg_field.print(&msg_field, attr);
return NL_SKIP;
}
static int mt76_dump(int phy, int argc, char **argv)
{
struct nl_msg *msg;
void *data;
msg = unl_genl_msg(&unl, NL80211_CMD_TESTMODE, true);
nla_put_u32(msg, NL80211_ATTR_WIPHY, phy);
data = nla_nest_start(msg, NL80211_ATTR_TESTDATA);
for (; argc > 0; argc--, argv++) {
if (!strcmp(argv[0], "stats"))
nla_put_flag(msg, MT76_TM_ATTR_STATS);
}
nla_nest_end(msg, data);
unl_genl_request(&unl, msg, mt76_dump_cb, NULL);
return 0;
}
static inline void tm_set_changed(uint32_t id)
{
tm_changed[id / 32] |= (1U << (id % 32));
}
static inline bool tm_is_changed(uint32_t id)
{
return tm_changed[id / 32] & (1U << (id % 32));
}
static int mt76_set(int phy, int argc, char **argv)
{
const struct tm_field *fields = msg_field.fields;
struct nl_msg *msg;
void *data;
int i, ret;
if (argc < 1)
return 1;
msg = unl_genl_msg(&unl, NL80211_CMD_TESTMODE, false);
nla_put_u32(msg, NL80211_ATTR_WIPHY, phy);
data = nla_nest_start(msg, NL80211_ATTR_TESTDATA);
for (; argc > 0; argc--, argv++) {
char *name = argv[0];
char *val = strchr(name, '=');
if (!val) {
fprintf(stderr, "Invalid argument: %s\n", name);
return 1;
}
*(val++) = 0;
for (i = 0; i < msg_field.len; i++) {
if (!fields[i].parse)
continue;
if (!strcmp(fields[i].name, name))
break;
}
if (i == msg_field.len) {
fprintf(stderr, "Unknown field: %s\n", name);
return 1;
}
if (tm_is_changed(i)) {
fprintf(stderr, "Duplicate field '%s'\n", name);
return 1;
}
if (!fields[i].parse(&fields[i], i, msg, val))
return 1;
tm_set_changed(i);
}
nla_nest_end(msg, data);
ret = unl_genl_request(&unl, msg, NULL, NULL);
if (ret)
fprintf(stderr, "nl80211 call failed: %s\n", strerror(-ret));
return ret;
}
int main(int argc, char **argv)
{
const char *cmd, *phyname;
int phy;
int ret = 0;
progname = argv[0];
if (argc < 3)
usage();
if (unl_genl_init(&unl, "nl80211") < 0) {
fprintf(stderr, "Failed to connect to nl80211\n");
return 2;
}
phyname = argv[1];
phy = phy_lookup_idx(phyname);
if (phy < 0) {
fprintf(stderr, "Could not find phy '%s'\n", argv[1]);
return 2;
}
cmd = argv[2];
argv += 3;
argc -= 3;
if (!strcmp(cmd, "dump"))
ret = mt76_dump(phy, argc, argv);
else if (!strcmp(cmd, "set"))
ret = mt76_set(phy, argc, argv);
else if (!strcmp(cmd, "eeprom"))
ret = mt76_eeprom(phy, argc, argv);
else if (!strcmp(cmd, "fwlog"))
ret = mt76_fwlog(phyname, argc, argv);
else
usage();
unl_free(&unl);
return ret;
}