]> git.decadent.org.uk Git - ap-utils.git/blob - lib/test.c
56b953a41ae5ccf4207189226e9b63d242ac585a
[ap-utils.git] / lib / test.c
1 /*
2  *      test.c from Access Point SNMP Utils for Linux
3  *
4  * Copyright (c) 2002 Roman Festchook <roma at polesye dot net>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License Version 2 from
8  * June 1991 as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * 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 along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  */
20 #include <math.h>
21 #include <string.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include "ap-utils.h"
25
26 #define TEST_MODE _("[T] Test mode: ")
27 #define ANTENNA _("[A] Antenna: ")
28 #define SIGLEVEL _("[S] Signal level: ")
29 #define RATE _("[R] Rate: ")
30 #define FILER _("[F] TxFiler: ")
31 #define COMMAND _("[O] Command: ")
32 #define __HELP _("T - Test mode On/Off; CASRFO - set options; Q - quit to menu")
33
34 extern WINDOW *main_sub;
35 extern short ap_type;
36 extern char *channels[];
37
38 void test()
39 {
40     struct TestModeCommandStruct_ATMEL410 {
41         unsigned char Channel;
42         unsigned char Antenna;
43         unsigned char RegCR31;
44         unsigned char Rate;
45         unsigned char TxFiler;  //US 01 - JPN 02
46         unsigned char Command;  //CMD_CONTINUOUS_TX 0x01, CMD_CONTINUOUS_RX 0x02, CMD_CARRIER_ACCURACY 0x03, CMD_IDLE 0x04
47         unsigned char Res1;
48         unsigned char Res2;
49     } testmode;
50
51     struct TestModeStatistics {
52         unsigned long SuccessFrames;
53         unsigned long FailedFrames;
54     } *teststat = NULL;
55
56     char TestModeOnOff[] =
57         { 0x2B, 0x06, 0x01, 0x04, 0x01, 0x83, 0x1A, 0x01, 0x01, 0x08, 0x01,
58         0x00
59     };
60     char TestModeCommandStruct[] =
61         { 0x2B, 0x06, 0x01, 0x04, 0x01, 0x83, 0x1A, 0x01, 0x01, 0x08, 0x02,
62         0x00
63     };
64     char TestModeStatistics[] =
65         { 0x2B, 0x06, 0x01, 0x04, 0x01, 0x83, 0x1A, 0x01, 0x01, 0x08, 0x09,
66         0x00
67     };
68
69     char message[1024], *antenna[2] =
70         { _("Left"), _("Right") }, *filer[2] = {
71     "EN", "US"}, *rates[4] = {
72     "1", "2", "5.5", "11"};
73     char teston = 2, *commands[4] =
74         { "CONTINUOUS_TX", "CONTINUOUS_RX", "CARRIER_ACCURACY", "IDLE" };
75     varbind varbinds[5];
76
77     if (ap_type == ATMEL12350) {
78 #if 1 /* Temporary kludge */
79         mvwaddstr(main_sub, 1, 0, _("Devices with ATMEL12350 MIB not yet supported."));
80         wrefresh(main_sub);
81         goto exit;
82 #endif
83         TestModeOnOff[5] = 0xE0;
84         TestModeOnOff[6] = 0x3E;
85         TestModeCommandStruct[5] = 0xE0;
86         TestModeCommandStruct[6] = 0x3E;
87         TestModeStatistics[5] = 0xE0;
88         TestModeStatistics[6] = 0x3E;
89     }
90
91     print_top(NULL, _("Test mode"));
92     mvwaddstr(main_sub, 3, 6,
93               _
94               ("Using the \"Test mode\" may cause loss of your current"));
95     mvwaddstr(main_sub, 4, 6, _("configuration."));
96     mvwaddstr(main_sub, 6, 20, _("Do you want to continue? "));
97     wrefresh(main_sub);
98
99     if (help_ysn())
100         goto quit;
101
102     print_help(__HELP);
103
104     sprintf(message, "%s%s", TEST_MODE, OFF);
105     mvwaddstr(main_sub, 0, 0, message);
106
107     mvwaddstr(main_sub, 2, 1, _("Options:"));
108     testmode.Channel = 1;
109     sprintf(message, "%s%02u (%u MHz)", CHANNEL, testmode.Channel,
110             2407 + 5 * testmode.Channel);
111     mvwaddstr(main_sub, 3, 0, message);
112
113     testmode.Antenna = 1;
114     sprintf(message, "%s%s", ANTENNA, antenna[testmode.Antenna - 1]);
115     mvwaddstr(main_sub, 4, 0, message);
116
117     testmode.RegCR31 = 128;
118     sprintf(message, "%s%u", SIGLEVEL, testmode.RegCR31);
119     mvwaddstr(main_sub, 5, 0, message);
120
121     testmode.Rate = 2;
122     sprintf(message, "%s%.1f Mbit/s", RATE, (float) testmode.Rate / 2);
123     mvwaddstr(main_sub, 6, 0, message);
124
125     testmode.TxFiler = 1;
126     sprintf(message, "%s%s", FILER, filer[testmode.TxFiler - 1]);
127     mvwaddstr(main_sub, 7, 0, message);
128
129     testmode.Command = 1;
130     sprintf(message, "%s%s", COMMAND, commands[testmode.Command - 1]);
131     mvwaddstr(main_sub, 8, 0, message);
132
133     mvwaddstr(main_sub, 10, 1, _("Statistics:"));
134     mvwaddstr(main_sub, 11, 0, _("Success Frames: 0 Failed Frames: 0"));
135
136     wrefresh(main_sub);
137     noecho();
138     while (1) {
139         switch (getch()) {
140         case 'Q':
141         case 'q':
142             if (teston == 1) {
143                 teston = 2;
144                 varbinds[0].oid = TestModeOnOff;
145                 varbinds[0].len_oid = sizeof(TestModeOnOff);
146                 varbinds[0].value = &teston;
147                 varbinds[0].len_val = 1;
148                 varbinds[0].type = INT_VALUE;
149                 if (snmp(varbinds, 1, SET) <= 0) {
150                     print_helperr(ERR_RET);
151                     goto exit;
152                 }
153             }
154             goto quit;
155         case 'T':
156         case 't':
157             teston = on_off(0, 1 + strlen(TEST_MODE));
158
159             if (teston == 1) {
160                 clear_main_new(0, 1);
161                 print_menusel(0, 0, TEST_MODE, ON);
162                 varbinds[0].oid = TestModeOnOff;
163                 varbinds[0].len_oid = sizeof(TestModeOnOff);
164                 varbinds[0].value = &teston;
165                 varbinds[0].len_val = 1;
166                 varbinds[0].type = INT_VALUE;
167                 varbinds[1].oid = TestModeCommandStruct;
168                 varbinds[1].len_oid = sizeof(TestModeCommandStruct);
169                 varbinds[1].len_val = sizeof(testmode);
170                 varbinds[1].value = (char *) &testmode;
171                 varbinds[1].type = STRING_VALUE;
172                 if (snmp(varbinds, 2, SET) <= 0) {
173                     print_helperr(ERR_RET);
174                     goto exit;
175                 }
176             } else {
177                 varbinds[0].oid = TestModeStatistics;
178                 varbinds[0].len_oid = sizeof(TestModeStatistics);
179                 varbinds[0].value = TestModeStatistics;
180                 varbinds[0].len_val = 0;
181                 varbinds[0].type = NULL_VALUE;
182
183                 if (snmp(varbinds, 1, GET) <= 0) {
184                     print_helperr(ERR_RET);
185                     getch();
186                     print_help(__HELP);
187                 } else {
188                     teststat =
189                         (struct TestModeStatistics *) malloc(varbinds[0].
190                                                              len_val);
191                     memcpy(teststat, varbinds[0].value,
192                            sizeof(struct TestModeStatistics));
193                     clear_main_new(11, 12);
194                     sprintf(message,
195                             _("Success Frames: %lu Failed Frames: %lu"),
196                             swap4(teststat->SuccessFrames),
197                             swap4(teststat->FailedFrames));
198                     mvwaddstr(main_sub, 11, 0, message);
199                     free(teststat);
200                 }
201                 clear_main_new(0, 1);
202                 sprintf(message, "%s%s", TEST_MODE, OFF);
203                 mvwaddstr(main_sub, 0, 0, message);
204
205                 varbinds[0].oid = TestModeOnOff;
206                 varbinds[0].len_oid = sizeof(TestModeOnOff);
207                 varbinds[0].value = &teston;
208                 varbinds[0].len_val = 1;
209                 varbinds[0].type = INT_VALUE;
210
211                 if (snmp(varbinds, 1, SET) <= 0) {
212                     print_helperr(ERR_RET);
213                     goto exit;
214                 }
215                 wrefresh(main_sub);
216             }
217             continue;
218         case 'C':
219         case 'c':
220             testmode.Channel = menu_choose(3, strlen(CHANNEL), channels,
221                 14) + 1;
222             clear_main_new(3, 4);
223             sprintf(message, "%02u (%u MHz)", testmode.Channel,
224                 2407 + 5 * testmode.Channel);
225             print_menusel(3, 0, CHANNEL, message);
226             continue;
227         case 'R':
228         case 'r':
229             switch (menu_choose(6, strlen(RATE), rates, 4)) {
230             case 0:
231                 testmode.Rate = 2;
232                 break;
233             case 1:
234                 testmode.Rate = 4;
235                 break;
236             case 2:
237                 testmode.Rate = 11;
238                 break;
239             case 3:
240                 testmode.Rate = 22;
241             }
242             clear_main_new(6, 7);
243             sprintf(message, "%.1f Mbit/s", (float) testmode.Rate / 2);
244             print_menusel(6, 0, RATE, message);
245             continue;
246         case 'A':
247         case 'a':
248             testmode.Antenna = menu_choose(4, strlen(ANTENNA), antenna, 2) + 1;
249             clear_main_new(4, 5);
250             print_menusel(4, 0, ANTENNA, antenna[testmode.Antenna - 1]);
251             continue;
252         case 'O':
253         case 'o':
254             testmode.Command = menu_choose(8, strlen(COMMAND), commands, 4) + 1;
255             clear_main_new(8, 9);
256             print_menusel(8, 0, COMMAND, commands[testmode.Command - 1]);
257             continue;
258         case 'F':
259         case 'f':
260             testmode.TxFiler = menu_choose(7, strlen(FILER), filer, 2) + 1;
261             clear_main_new(7, 8);
262             print_menusel(7, 0, FILER, filer[testmode.TxFiler - 1]);
263             continue;
264         case 'S':
265         case 's':
266 //          make_field(5, strlen(SIGLEVEL), 4);
267             get_value(message, 5, strlen(SIGLEVEL), 4, INT_STRING, 0, 255,
268                 __HELP);
269             print_menusel(5, 0, SIGLEVEL, message);
270             testmode.RegCR31 = atoi(message) & 0xFF;
271             continue;
272         default:
273             continue;
274         }
275         break;
276     }
277
278     print_help(ANY_KEY);
279   exit:
280     getch();
281   quit:
282     print_top(NULL, NULL);
283     clear_main(0);
284 }