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