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"
38 StringBufferLength(const char *buffer
, size_t size
)
40 const char *z
= (const char *)memchr(buffer
, 0, size
);
44 while (z
> buffer
&& z
[-1] == ' ')
51 PrintInfo(CAI302Device
&device
, OperationEnvironment
&env
)
53 CAI302::GeneralInfo info
;
54 if (!device
.ReadGeneralInfo(info
, env
))
58 StringBufferLength(info
.id
, ARRAY_SIZE(info
.id
)), info
.id
);
59 printf("firmward: %.*s (%s)\n",
60 StringBufferLength(info
.version
, ARRAY_SIZE(info
.version
)),
62 info
.type
== 'F' ? "production"
63 : (info
.type
== 'P' ? "prototype" : "unknown"));
68 ListPilots(CAI302Device
&device
, OperationEnvironment
&env
)
70 std::vector
<CAI302::Pilot
> pilots
;
72 if (!device
.ReadPilotList(pilots
, active
, env
))
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
);
86 ListNavpoints(CAI302Device
&device
, OperationEnvironment
&env
)
88 const int count
= device
.ReadNavpointCount(env
);
92 printf("count=%u\n", count
);
93 for (int i
= 0; i
< count
; ++i
) {
95 if (!device
.ReadNavpoint(i
, n
, env
))
98 int32_t latitude
= FromBE32(n
.latitude
) - 54000000;
99 char latitude_letter
= latitude
>= 0 ? 'N' : 'S';
101 latitude
= -latitude
;
103 int32_t longitude
= FromBE32(n
.longitude
) - 108000000;
104 char longitude_letter
= longitude
>= 0 ? 'E' : 'W';
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,
113 longitude
/ 10000 / 60,
114 longitude
/ 10000 % 60,
115 longitude
* 6 / 1000 % 60,
117 StringBufferLength(n
.name
, ARRAY_SIZE(n
.name
)), n
.name
,
118 StringBufferLength(n
.remark
, ARRAY_SIZE(n
.remark
)), n
.remark
);
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
);
145 fprintf(stderr
, "Unknown command: %s\n", command
);
150 int main(int argc
, char **argv
)
152 const char *const usage
= "PORT BAUD COMMAND\n\n"
153 "Where COMMAND is one of:"
162 Args
args(argc
, argv
, usage
);
163 const DeviceConfig config
= ParsePortArgs(args
);
165 const char *command
= args
.ExpectNext();
168 InitialiseIOThread();
170 Port
*port
= OpenPort(config
, *(DataHandler
*)NULL
);
172 fprintf(stderr
, "Failed to open port\n");
176 ConsoleOperationEnvironment env
;
178 CAI302Device
device(config
, *port
);
179 if (!RunCommand(device
, command
, env
)) {
180 fprintf(stderr
, "error\n");
185 DeinitialiseIOThread();