android/GlueIOIOPort: fix spurious errors after IOIO baud rate change
[xcsoar.git] / test / src / RunFlarmUtils.cpp
blob96c59c37e16d61f75f7869c0076fe1c4954acc50
1 /*
2 Copyright_License {
4 XCSoar Glide Computer - http://www.xcsoar.org/
5 Copyright (C) 2000-2013 The XCSoar Project
6 A detailed list of copyright holders can be found in the file "AUTHORS".
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 #include "DebugPort.hpp"
25 #include "Device/Port/Port.hpp"
26 #include "Device/Port/ConfiguredPort.hpp"
27 #include "Device/Driver.hpp"
28 #include "Device/Register.hpp"
29 #include "Device/Parser.hpp"
30 #include "Device/Driver/FLARM/Device.hpp"
31 #include "OS/Args.hpp"
32 #include "Profile/DeviceConfig.hpp"
33 #include "Util/StringUtil.hpp"
34 #include "Util/ConvertString.hpp"
35 #include "Operation/ConsoleOperationEnvironment.hpp"
36 #include "IO/Async/GlobalIOThread.hpp"
38 #include <stdio.h>
40 static void
41 ChangePilot(FlarmDevice &flarm, OperationEnvironment &env)
43 while (true) {
44 TCHAR old_pilot_name[64];
45 if (flarm.GetPilot(old_pilot_name, 64, env))
46 _tprintf(_T("Old pilot name: \"%s\"\n"), old_pilot_name);
48 fprintf(stdout, "Please enter the new pilot name:\n");
49 fprintf(stdout, "> ");
51 char pilot_name[64];
52 if (fgets(pilot_name, 64, stdin) == NULL || strlen(pilot_name) == 0) {
53 fprintf(stdout, "Invalid input\n");
54 continue;
57 TrimRight(pilot_name);
58 fprintf(stdout, "Setting pilot name to \"%s\" ...\n", pilot_name);
60 const UTF8ToWideConverter value(pilot_name);
61 if (flarm.SetPilot(value, env))
62 fprintf(stdout, "Pilot name set to \"%s\"\n", pilot_name);
63 else
64 fprintf(stdout, "Operation failed!\n");
66 return;
70 static void
71 ChangeCoPilot(FlarmDevice &flarm, OperationEnvironment &env)
73 while (true) {
74 TCHAR old_copilot_name[64];
75 if (flarm.GetCoPilot(old_copilot_name, 64, env))
76 _tprintf(_T("Old copilot name: \"%s\"\n"), old_copilot_name);
78 fprintf(stdout, "Please enter the new copilot name:\n");
79 fprintf(stdout, "> ");
81 char copilot_name[64];
82 if (fgets(copilot_name, 64, stdin) == NULL || strlen(copilot_name) == 0) {
83 fprintf(stdout, "Invalid input\n");
84 continue;
87 TrimRight(copilot_name);
88 fprintf(stdout, "Setting copilot name to \"%s\" ...\n", copilot_name);
90 const UTF8ToWideConverter value(copilot_name);
91 if (flarm.SetCoPilot(value, env))
92 fprintf(stdout, "CoPilot name set to \"%s\"\n", copilot_name);
93 else
94 fprintf(stdout, "Operation failed!\n");
96 return;
100 static void
101 ChangePlaneType(FlarmDevice &flarm, OperationEnvironment &env)
103 while (true) {
104 TCHAR old_plane_type[64];
105 if (flarm.GetPlaneType(old_plane_type, 64, env))
106 _tprintf(_T("Old plane type: \"%s\"\n"), old_plane_type);
108 fprintf(stdout, "Please enter the new plane type:\n");
109 fprintf(stdout, "> ");
111 char plane_type[64];
112 if (fgets(plane_type, 64, stdin) == NULL || strlen(plane_type) == 0) {
113 fprintf(stdout, "Invalid input\n");
114 continue;
117 TrimRight(plane_type);
118 fprintf(stdout, "Setting plane type to \"%s\" ...\n", plane_type);
120 const UTF8ToWideConverter value(plane_type);
121 if (flarm.SetPlaneType(value, env))
122 fprintf(stdout, "Plane type set to \"%s\"\n", plane_type);
123 else
124 fprintf(stdout, "Operation failed!\n");
126 return;
130 static void
131 ChangeRegistration(FlarmDevice &flarm, OperationEnvironment &env)
133 while (true) {
134 TCHAR old_registration[64];
135 if (flarm.GetPlaneRegistration(old_registration, 64, env))
136 _tprintf(_T("Old plane registratio: \"%s\"\n"), old_registration);
138 fprintf(stdout, "Please enter the new plane registration:\n");
139 fprintf(stdout, "> ");
141 char registration[64];
142 if (fgets(registration, 64, stdin) == NULL || strlen(registration) == 0) {
143 fprintf(stdout, "Invalid input\n");
144 continue;
147 TrimRight(registration);
148 fprintf(stdout, "Setting plane registration to \"%s\" ...\n", registration);
150 const UTF8ToWideConverter value(registration);
151 if (flarm.SetPlaneRegistration(value, env))
152 fprintf(stdout, "Plane registration set to \"%s\"\n", registration);
153 else
154 fprintf(stdout, "Operation failed!\n");
156 return;
160 static void
161 ChangeCompetitionId(FlarmDevice &flarm, OperationEnvironment &env)
163 while (true) {
164 TCHAR old_id[64];
165 if (flarm.GetCompetitionId(old_id, 64, env))
166 _tprintf(_T("Old competition id: \"%s\"\n"), old_id);
168 fprintf(stdout, "Please enter the new competition id:\n");
169 fprintf(stdout, "> ");
171 char id[64];
172 if (fgets(id, 64, stdin) == NULL || strlen(id) == 0) {
173 fprintf(stdout, "Invalid input\n");
174 continue;
177 TrimRight(id);
178 fprintf(stdout, "Setting competition id to \"%s\" ...\n", id);
180 const UTF8ToWideConverter value(id);
181 if (flarm.SetCompetitionId(value, env))
182 fprintf(stdout, "competition id set to \"%s\"\n", id);
183 else
184 fprintf(stdout, "Operation failed!\n");
186 return;
190 static void
191 ChangeCompetitionClass(FlarmDevice &flarm, OperationEnvironment &env)
193 while (true) {
194 TCHAR old_comp_class[64];
195 if (flarm.GetCompetitionClass(old_comp_class, 64, env))
196 _tprintf(_T("Old competition class: \"%s\"\n"), old_comp_class);
198 fprintf(stdout, "Please enter the new competition class:\n");
199 fprintf(stdout, "> ");
201 char comp_class[64];
202 if (fgets(comp_class, 64, stdin) == NULL || strlen(comp_class) == 0) {
203 fprintf(stdout, "Invalid input\n");
204 continue;
207 TrimRight(comp_class);
208 fprintf(stdout, "Setting competition class to \"%s\" ...\n", comp_class);
210 const UTF8ToWideConverter value(comp_class);
211 if (flarm.SetCompetitionClass(value, env))
212 fprintf(stdout, "Competition class set to \"%s\"\n", comp_class);
213 else
214 fprintf(stdout, "Operation failed!\n");
216 return;
220 static void
221 ChangeRange(FlarmDevice &flarm, OperationEnvironment &env)
223 while (true) {
224 unsigned num_range;
226 if (flarm.GetRange(num_range, env))
227 printf("Old range setting: \"%d\"\n", num_range);
229 fprintf(stdout, "Please enter the range setting (2000-25500):\n");
230 fprintf(stdout, "> ");
232 char range[64];
233 if (fgets(range, 64, stdin) == NULL || strlen(range) == 0) {
234 fprintf(stdout, "Invalid input\n");
235 continue;
238 TrimRight(range);
240 char *end_ptr;
241 num_range = strtoul(range, &end_ptr, 10);
242 if (range == end_ptr) {
243 fprintf(stdout, "Invalid input\n");
244 continue;
247 fprintf(stdout, "Setting range to \"%d\" ...\n", num_range);
249 if (flarm.SetRange(num_range, env))
250 fprintf(stdout, "Range set to \"%d\"\n", num_range);
251 else
252 fprintf(stdout, "Operation failed!\n");
254 return;
258 static void
259 ChangeBaudRate(FlarmDevice &flarm, OperationEnvironment &env)
261 while (true) {
262 unsigned baud_id;
264 if (flarm.GetBaudRate(baud_id, env))
265 printf("Old baud rate setting: \"%d\"\n", baud_id);
267 fprintf(stdout, "Please enter the baud rate setting (2000-25500):\n");
268 fprintf(stdout, "> ");
270 char buffer[64];
271 if (fgets(buffer, 64, stdin) == NULL || strlen(buffer) == 0) {
272 fprintf(stdout, "Invalid input\n");
273 continue;
276 TrimRight(buffer);
278 char *end_ptr;
279 baud_id = strtoul(buffer, &end_ptr, 10);
280 if (end_ptr == buffer) {
281 fprintf(stdout, "Invalid input\n");
282 continue;
285 fprintf(stdout, "Setting baud rate to \"%d\" ...\n", baud_id);
287 if (flarm.SetBaudRate(baud_id, env))
288 fprintf(stdout, "BaudRate set to \"%d\"\n", baud_id);
289 else
290 fprintf(stdout, "Operation failed!\n");
292 return;
296 static void
297 WriteMenu()
299 fprintf(stdout, "------------------------------------\n"
300 "Flarm Utils Menu\n"
301 "------------------------------------\n"
302 "Press any of the following commands:\n\n"
303 "h: Display this menu\n"
304 "1: Change pilot name\n"
305 "2: Change copilot name\n"
306 "3: Change plane type\n"
307 "4: Change plane registration\n"
308 "5: Change competition id\n"
309 "6: Change competition class\n"
310 "7: Change receiving range\n"
311 "8: Change baud rate\n"
312 "r: Restart the FLARM\n"
313 "s+: Enable the stealth mode\n"
314 "s-: Disable the stealth mode\n"
315 "q: Quit this application\n"
316 "------------------------------------\n");
319 static void
320 RunUI(FlarmDevice &flarm, OperationEnvironment &env)
322 WriteMenu();
324 while (true) {
325 fprintf(stdout, "> ");
327 char in[20];
328 if (fgets(in, 20, stdin) == NULL || strlen(in) == 0) {
329 fprintf(stdout, "Invalid input\n");
330 continue;
333 switch (in[0]) {
334 case '?':
335 case 'h':
336 case 'H':
337 WriteMenu();
338 break;
339 case '1':
340 ChangePilot(flarm, env);
341 break;
342 case '2':
343 ChangeCoPilot(flarm, env);
344 break;
345 case '3':
346 ChangePlaneType(flarm, env);
347 break;
348 case '4':
349 ChangeRegistration(flarm, env);
350 break;
351 case '5':
352 ChangeCompetitionId(flarm, env);
353 break;
354 case '6':
355 ChangeCompetitionClass(flarm, env);
356 break;
357 case '7':
358 ChangeRange(flarm, env);
359 break;
360 case '8':
361 ChangeBaudRate(flarm, env);
362 break;
363 case 'r':
364 case 'R':
365 fprintf(stdout, "Restarting the FLARM ...\n");
366 flarm.Restart(env);
367 break;
368 case 's':
369 case 'S':
370 if (strlen(in) < 2 || (in[1] != '+' && in[1] != '-')) {
371 fprintf(stdout, "Invalid input\n");
372 break;
375 fprintf(stdout, "Changing stealth mode setting ...\n");
376 if (flarm.SetStealthMode(in[1] == '+', env)) {
377 if (in[1] == '+')
378 fprintf(stdout, "Stealth mode enabled\n");
379 else
380 fprintf(stdout, "Stealth mode disabled\n");
381 } else
382 fprintf(stdout, "Operation failed!\n");
383 break;
384 case 'q':
385 case 'Q':
386 fprintf(stdout, "Closing Flarm Utils ...\n");
387 return;
388 default:
389 fprintf(stdout, "Invalid input\n");
390 break;
396 main(int argc, char **argv)
398 Args args(argc, argv, "PORT BAUD");
399 const DeviceConfig config = ParsePortArgs(args);
400 args.ExpectEnd();
402 InitialiseIOThread();
404 Port *port = OpenPort(config, *(DataHandler *)NULL);
405 if (port == NULL) {
406 fprintf(stderr, "Failed to open COM port\n");
407 return EXIT_FAILURE;
410 ConsoleOperationEnvironment env;
411 FlarmDevice flarm(*port);
412 RunUI(flarm, env);
414 delete port;
415 DeinitialiseIOThread();
417 return EXIT_SUCCESS;