Revert "Device/Driver/LX: Add small delay after baud rate change"
[xcsoar.git] / test / src / CAI302Tool.cpp
blob433d1d42d364c867cc894d4aee5eade0004ca1a9
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 "Device/Driver/CAI302/Internal.hpp"
25 #include "Profile/DeviceConfig.hpp"
26 #include "Device/Port/Port.hpp"
27 #include "Device/Port/ConfiguredPort.hpp"
28 #include "DebugPort.hpp"
29 #include "OS/Args.hpp"
30 #include "Operation/ConsoleOperationEnvironment.hpp"
31 #include "Util/Macros.hpp"
32 #include "IO/Async/GlobalIOThread.hpp"
34 #include <stdio.h>
36 gcc_pure
37 static int
38 StringBufferLength(const char *buffer, size_t size)
40 const char *z = (const char *)memchr(buffer, 0, size);
41 if (z == NULL)
42 z = buffer + size;
44 while (z > buffer && z[-1] == ' ')
45 --z;
47 return z - buffer;
50 static bool
51 PrintInfo(CAI302Device &device, OperationEnvironment &env)
53 CAI302::GeneralInfo info;
54 if (!device.ReadGeneralInfo(info, env))
55 return false;
57 printf("id: %.*s\n",
58 StringBufferLength(info.id, ARRAY_SIZE(info.id)), info.id);
59 printf("firmward: %.*s (%s)\n",
60 StringBufferLength(info.version, ARRAY_SIZE(info.version)),
61 info.version,
62 info.type == 'F' ? "production"
63 : (info.type == 'P' ? "prototype" : "unknown"));
64 return true;
67 static bool
68 ListPilots(CAI302Device &device, OperationEnvironment &env)
70 std::vector<CAI302::Pilot> pilots;
71 unsigned active;
72 if (!device.ReadPilotList(pilots, active, env))
73 return false;
75 unsigned index = 0;
76 for (auto i = pilots.begin(), end = pilots.end(); i != end; ++i)
77 printf("%u: '%.*s'\n", index++,
78 StringBufferLength(i->name, ARRAY_SIZE(i->name)), i->name);
80 printf("active=%u\n", active);
82 return true;
85 static bool
86 ListNavpoints(CAI302Device &device, OperationEnvironment &env)
88 const int count = device.ReadNavpointCount(env);
89 if (count < 0)
90 return false;
92 printf("count=%u\n", count);
93 for (int i = 0; i < count; ++i) {
94 CAI302::Navpoint n;
95 if (!device.ReadNavpoint(i, n, env))
96 return false;
98 int32_t latitude = FromBE32(n.latitude) - 54000000;
99 char latitude_letter = latitude >= 0 ? 'N' : 'S';
100 if (latitude < 0)
101 latitude = -latitude;
103 int32_t longitude = FromBE32(n.longitude) - 108000000;
104 char longitude_letter = longitude >= 0 ? 'E' : 'W';
105 if (longitude < 0)
106 longitude = -longitude;
108 printf("%u: %u:%02u:%02u%c %u:%02u:%02u%c '%.*s' '%.*s'\n", i,
109 latitude / 10000 / 60,
110 latitude / 10000 % 60,
111 latitude * 6 / 1000 % 60,
112 latitude_letter,
113 longitude / 10000 / 60,
114 longitude / 10000 % 60,
115 longitude * 6 / 1000 % 60,
116 longitude_letter,
117 StringBufferLength(n.name, ARRAY_SIZE(n.name)), n.name,
118 StringBufferLength(n.remark, ARRAY_SIZE(n.remark)), n.remark);
121 return true;
124 static bool
125 RunCommand(CAI302Device &device, const char *command,
126 OperationEnvironment &env)
128 if (strcmp(command, "info") == 0)
129 return PrintInfo(device, env);
130 else if (strcmp(command, "reboot") == 0)
131 return device.Reboot(env);
132 else if (strcmp(command, "poweroff") == 0)
133 return device.PowerOff(env);
134 else if (strcmp(command, "startlogger") == 0)
135 return device.StartLogging(env);
136 else if (strcmp(command, "stoplogger") == 0)
137 return device.StopLogging(env);
138 else if (strcmp(command, "clearlog") == 0)
139 return device.ClearLog(env);
140 else if (strcmp(command, "pilots") == 0)
141 return ListPilots(device, env);
142 else if (strcmp(command, "navpoints") == 0)
143 return ListNavpoints(device, env);
144 else {
145 fprintf(stderr, "Unknown command: %s\n", command);
146 return false;
150 int main(int argc, char **argv)
152 const char *const usage = "PORT BAUD COMMAND\n\n"
153 "Where COMMAND is one of:"
154 "\n\tinfo"
155 "\n\treboot"
156 "\n\tpoweroff"
157 "\n\tstartlogger"
158 "\n\tstoplogger"
159 "\n\tpilots"
160 "\n\tnavpoints"
162 Args args(argc, argv, usage);
163 const DeviceConfig config = ParsePortArgs(args);
165 const char *command = args.ExpectNext();
166 args.ExpectEnd();
168 InitialiseIOThread();
170 Port *port = OpenPort(config, *(DataHandler *)NULL);
171 if (port == NULL) {
172 fprintf(stderr, "Failed to open port\n");
173 return EXIT_FAILURE;
176 ConsoleOperationEnvironment env;
178 CAI302Device device(config, *port);
179 if (!RunCommand(device, command, env)) {
180 fprintf(stderr, "error\n");
181 return EXIT_FAILURE;
184 delete port;
185 DeinitialiseIOThread();
186 return EXIT_SUCCESS;