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"
41 ChangePilot(FlarmDevice
&flarm
, OperationEnvironment
&env
)
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
, "> ");
52 if (fgets(pilot_name
, 64, stdin
) == NULL
|| strlen(pilot_name
) == 0) {
53 fprintf(stdout
, "Invalid input\n");
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
);
64 fprintf(stdout
, "Operation failed!\n");
71 ChangeCoPilot(FlarmDevice
&flarm
, OperationEnvironment
&env
)
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");
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
);
94 fprintf(stdout
, "Operation failed!\n");
101 ChangePlaneType(FlarmDevice
&flarm
, OperationEnvironment
&env
)
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
, "> ");
112 if (fgets(plane_type
, 64, stdin
) == NULL
|| strlen(plane_type
) == 0) {
113 fprintf(stdout
, "Invalid input\n");
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
);
124 fprintf(stdout
, "Operation failed!\n");
131 ChangeRegistration(FlarmDevice
&flarm
, OperationEnvironment
&env
)
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");
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
);
154 fprintf(stdout
, "Operation failed!\n");
161 ChangeCompetitionId(FlarmDevice
&flarm
, OperationEnvironment
&env
)
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
, "> ");
172 if (fgets(id
, 64, stdin
) == NULL
|| strlen(id
) == 0) {
173 fprintf(stdout
, "Invalid input\n");
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
);
184 fprintf(stdout
, "Operation failed!\n");
191 ChangeCompetitionClass(FlarmDevice
&flarm
, OperationEnvironment
&env
)
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
, "> ");
202 if (fgets(comp_class
, 64, stdin
) == NULL
|| strlen(comp_class
) == 0) {
203 fprintf(stdout
, "Invalid input\n");
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
);
214 fprintf(stdout
, "Operation failed!\n");
221 ChangeRange(FlarmDevice
&flarm
, OperationEnvironment
&env
)
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
, "> ");
233 if (fgets(range
, 64, stdin
) == NULL
|| strlen(range
) == 0) {
234 fprintf(stdout
, "Invalid input\n");
241 num_range
= strtoul(range
, &end_ptr
, 10);
242 if (range
== end_ptr
) {
243 fprintf(stdout
, "Invalid input\n");
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
);
252 fprintf(stdout
, "Operation failed!\n");
259 ChangeBaudRate(FlarmDevice
&flarm
, OperationEnvironment
&env
)
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
, "> ");
271 if (fgets(buffer
, 64, stdin
) == NULL
|| strlen(buffer
) == 0) {
272 fprintf(stdout
, "Invalid input\n");
279 baud_id
= strtoul(buffer
, &end_ptr
, 10);
280 if (end_ptr
== buffer
) {
281 fprintf(stdout
, "Invalid input\n");
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
);
290 fprintf(stdout
, "Operation failed!\n");
299 fprintf(stdout
, "------------------------------------\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");
320 RunUI(FlarmDevice
&flarm
, OperationEnvironment
&env
)
325 fprintf(stdout
, "> ");
328 if (fgets(in
, 20, stdin
) == NULL
|| strlen(in
) == 0) {
329 fprintf(stdout
, "Invalid input\n");
340 ChangePilot(flarm
, env
);
343 ChangeCoPilot(flarm
, env
);
346 ChangePlaneType(flarm
, env
);
349 ChangeRegistration(flarm
, env
);
352 ChangeCompetitionId(flarm
, env
);
355 ChangeCompetitionClass(flarm
, env
);
358 ChangeRange(flarm
, env
);
361 ChangeBaudRate(flarm
, env
);
365 fprintf(stdout
, "Restarting the FLARM ...\n");
370 if (strlen(in
) < 2 || (in
[1] != '+' && in
[1] != '-')) {
371 fprintf(stdout
, "Invalid input\n");
375 fprintf(stdout
, "Changing stealth mode setting ...\n");
376 if (flarm
.SetStealthMode(in
[1] == '+', env
)) {
378 fprintf(stdout
, "Stealth mode enabled\n");
380 fprintf(stdout
, "Stealth mode disabled\n");
382 fprintf(stdout
, "Operation failed!\n");
386 fprintf(stdout
, "Closing Flarm Utils ...\n");
389 fprintf(stdout
, "Invalid input\n");
396 main(int argc
, char **argv
)
398 Args
args(argc
, argv
, "PORT BAUD");
399 const DeviceConfig config
= ParsePortArgs(args
);
402 InitialiseIOThread();
404 Port
*port
= OpenPort(config
, *(DataHandler
*)NULL
);
406 fprintf(stderr
, "Failed to open COM port\n");
410 ConsoleOperationEnvironment env
;
411 FlarmDevice
flarm(*port
);
415 DeinitialiseIOThread();