953591dd2d79327a76f812d8f69e3e3b94b306b6
[openwrt.git] / package / openwrt / wlc / wlc.c
1 /*
2  * wl - tool for configuring the Broadcom Wireless Network Adapter
3  * Copyright (C) 2005 Felix Fietkau <nbd@vd-s.ath.cx>
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  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18  */
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23
24 #include <typedefs.h>
25 #include <wlutils.h>
26
27 int _argc;
28 char **_argv;
29
30 void qtxpwr() {
31         int i = 0;
32                 
33         if (_argc > 3) {
34                 if (_argv[3][0] == '@') {
35                         i = atoi(_argv[3] + 1);
36                         i |= 0x80000000;
37                 } else {
38                         i = atoi(_argv[3]);
39                 }
40                 wl_set_val(_argv[1], "qtxpower", &i, sizeof(i));
41         } else {
42                 wl_get_val(_argv[1], "qtxpower", &i, sizeof(i));
43                 if ((i & 0x80000000) == 0x80000000)
44                         printf("@");
45                 printf("%d\n", i & 0x7ffffff);
46         }
47 }
48
49
50 struct wl_config {
51         char *name, *desc;
52         enum {
53                 INT,
54                 BOOL,
55                 OTHER
56         } type;
57         int get, set;
58         int r1, r2;
59         void *handler;
60 };
61
62 struct wl_config commands[] = {
63         {"txpwr", "transmit power (in mW)", INT, WLC_GET_TXPWR, WLC_SET_TXPWR, 0, 255, NULL},
64         {"qtxpwr", "\ttransmit power (in qdbm)\n\t\t\t\"@\" before value means regulatory override", OTHER, 0, 0, 0, 0, qtxpwr},
65         {"promisc", "promiscuous mode", BOOL, WLC_GET_PROMISC, WLC_SET_PROMISC, 0, 0, NULL},
66         {"monitor", "monitor mode", BOOL, WLC_GET_MONITOR, WLC_SET_MONITOR, 0, 0, NULL},
67         {"passive", "passive mode", BOOL, WLC_GET_PASSIVE, WLC_SET_PASSIVE, 0, 0, NULL},
68         {"ap", "access point mode (0 = STA, 1 = AP)", BOOL, WLC_GET_AP, WLC_SET_AP, 0, 0, NULL},
69         {"infra", "infrastructure mode (0 = IBSS, 1 = Infra BSS)", BOOL, WLC_GET_INFRA, WLC_SET_INFRA, 0, 0, NULL},
70         {"antdiv", "rx antenna diversity (0 = antenna 0, 1 = antenna 1, 3 = auto select)", INT, WLC_GET_ANTDIV, WLC_SET_ANTDIV, 0, 3, NULL},
71         {"txant", "set tx antenna (0 = antenna 0, 1 = antenna 1, 3 = rx antenna)", INT, WLC_GET_TXANT, WLC_SET_TXANT, 0, 3, NULL},
72         {"wet", "wireless ethernet bridging mode", BOOL, WLC_GET_WET, WLC_SET_WET, 0, 0, NULL},
73         {"channel", "set channel", INT, WLC_GET_CHANNEL, WLC_SET_CHANNEL, 1, 14, NULL},
74         {NULL, NULL, 0, 0, 0, 0, 0, NULL}
75 };
76
77 void set_int(char *name, int ioctl, int value, int r1, int r2)
78 {
79         if ((value >= r1) || (value <= r2)) {
80                 wl_ioctl(name, ioctl, &value, sizeof(value));
81         } else
82                 fprintf(stderr, "invalid value.\n");
83 }
84
85 void get_int(char *name, int ioctl)
86 {
87         int value = 0;
88         wl_ioctl(name, ioctl, &value, sizeof(value));
89         printf("%d\n", value);
90 }
91
92 int main(int argc, char **argv)
93 {
94         struct wl_config *cmd; 
95         int i;
96
97         _argc = argc;
98         _argv = argv;
99
100         if (argc < 3) {
101                 fprintf(stderr, "Usage: %s <interface> <command> [...]\n\n", argv[0]);
102                 fprintf(stderr, "Commands:\n\n");
103                 
104                 cmd = commands;
105                 while (cmd->name != NULL) {
106                         fprintf(stderr, "\t%s", cmd->name);
107                         switch (cmd->type) {
108                                 case INT:
109                                         fprintf(stderr, " [%d-%d]", cmd->r1, cmd->r2);
110                                         break;
111                                 case BOOL:
112                                         fprintf(stderr, " [0|1]");
113                                         cmd->r1 = 0;
114                                         cmd->r2 = 1;
115                                         
116                         }
117                         fprintf(stderr, "\t%s\n", cmd->desc);
118                         cmd++;
119                 }
120                 fprintf(stderr, "\n");
121                 
122                 return -1;
123         }
124
125         if (wl_probe(argv[1]) < 0) {
126                 fprintf(stderr, "No broadcom extensions detected on interface %s\n", argv[1]);
127                 return -1;
128         }
129
130         if ((argc > 4) && (strcmp(argv[2], "ioctl") == 0)) {
131                 if (strcmp(argv[3], "int") == 0) {
132                         int ioctl = atoi(argv[4]);
133                         if (argc > 5)
134                                 i = atoi(argv[5]);
135                         else
136                                 i = 0;
137                         fprintf(stderr, "ioctl = 0x%x (%d)\nold value = 0x%x (%d)\n", ioctl, ioctl, i, i);
138                         wl_ioctl(argv[1], ioctl, &i, sizeof(i));
139                         fprintf(stderr, "new value = 0x%x (%d)\n", i, i);
140                 } else if (strcmp(argv[3], "intval") == 0) {
141                         i = 0;
142                         fprintf(stderr, "var = \"%s\"\nold value = 0x%x (%d)\n", argv[4], i, i);
143                         if (argc > 5) {
144                                 i = atoi(argv[5]);
145                                 wl_set_val(argv[1], argv[4], &i, sizeof(i));
146                         } else {
147                                 wl_get_val(argv[1], argv[4], &i, sizeof(i));
148                         }
149                         fprintf(stderr, "new value = 0x%x (%d)\n", i, i);
150                 }
151                 return 0;
152         }
153         cmd = commands;
154         while (cmd->name != NULL) {
155                 if (strcmp(argv[2], cmd->name) == 0) {
156                         switch (cmd->type) {
157                                 case INT:
158                                 case BOOL:
159                                         if (argc < 4) {
160                                                 get_int(argv[1], cmd->get);
161                                         } else {
162                                                 set_int(argv[1], cmd->set, atoi(argv[3]), cmd->r1, cmd->r2);
163                                         }
164                                 break;
165                                 case OTHER: {
166                                         void (*handler)(void) = cmd->handler;
167                                         handler();
168                                 }
169                                 break;
170                         }
171                 }
172                 cmd++;
173         }
174         
175         
176         return 0;
177 }