Renderer, ...: use PixelRect::GetCenter()
[xcsoar.git] / src / Device / Driver / Volkslogger / Logger.cpp
blobc54dc693257dd396210a60751bcb8024781a8a76
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 "Internal.hpp"
25 #include "Protocol.hpp"
26 #include "Device/Port/Port.hpp"
27 #include "Operation/Operation.hpp"
28 #include "vlconv.h"
29 #include "grecord.h"
31 #include <algorithm>
33 /**
34 * Sizes of VL memory regions
36 static constexpr size_t VLAPI_LOG_MEMSIZE = 81920L;
38 static bool
39 ParseDate(const tm &time,BrokenDate &date)
41 date.year = time.tm_year + 1900;
42 date.month = time.tm_mon + 1;
43 date.day = time.tm_mday;
45 return date.Plausible();
48 static bool
49 ParseTime(const tm &time,BrokenTime &btime)
51 btime.hour = time.tm_hour;
52 btime.minute = time.tm_min;
53 btime.second = time.tm_sec;
55 return btime.Plausible();
58 static bool
59 ConvertDirectoryToRecordedFlightList(const std::vector<DIRENTRY> &dir,
60 RecordedFlightList &flight_list)
62 RecordedFlightInfo flight_info;
63 for (unsigned i=0; (i < dir.size()) && !flight_list.full(); i++) {
64 const DIRENTRY &flight = dir[i];
66 * Only show logs with a takeoff detected
68 if (flight.takeoff == 1) {
69 if (!ParseDate(flight.firsttime, flight_info.date) ||
70 !ParseTime(flight.firsttime, flight_info.start_time) ||
71 !ParseTime(flight.lasttime, flight_info.end_time) )
72 return false;
73 flight_info.internal.volkslogger = i;
74 flight_list.append(flight_info);
77 return true;
80 static bool
81 ReadFlightListInner(Port &port,
82 RecordedFlightList &flight_list,
83 OperationEnvironment &env)
85 env.SetProgressRange(10);
86 if (!Volkslogger::ConnectAndFlush(port, env, 20000))
87 return false;
88 env.SetProgressPosition(3);
90 uint8_t dirbuffer[VLAPI_LOG_MEMSIZE];
91 int data_length = Volkslogger::ReadFlightList(port, env,
92 dirbuffer, sizeof(dirbuffer));
93 if (data_length <= 0)
94 return data_length == 0;
96 std::vector<DIRENTRY> directory;
97 if (!conv_dir(directory, dirbuffer, data_length))
98 return false;
100 if (directory.empty())
101 return true;
103 env.SetProgressPosition(8);
104 if (!ConvertDirectoryToRecordedFlightList(directory, flight_list))
105 return false;
106 env.SetProgressPosition(10);
108 return true;
111 static bool
112 DownloadFlightInner(Port &port, unsigned bulkrate,
113 const RecordedFlightInfo &flight,
114 const TCHAR *path,
115 OperationEnvironment &env)
117 if (!Volkslogger::ConnectAndFlush(port, env, 20000))
118 return false;
120 uint8_t logbuffer[VLAPI_LOG_MEMSIZE];
121 const size_t length = Volkslogger::ReadFlight(port, bulkrate, env,
122 flight.internal.volkslogger,
123 true,
124 logbuffer, sizeof(logbuffer));
125 if (length == 0)
126 return false;
128 FILE *outfile = _tfopen(path, _T("wt"));
129 if (outfile == nullptr)
130 return false;
132 size_t r = convert_gcs(0, outfile, logbuffer, length, true);
133 if (r == 0) {
134 fclose(outfile);
135 return false;
138 print_g_record(outfile, // output to stdout
139 logbuffer, // binary file is in buffer
140 r // length of binary file to include
142 return true;
145 bool
146 VolksloggerDevice::ReadFlightList(RecordedFlightList &flight_list,
147 OperationEnvironment &env)
149 port.StopRxThread();
151 // change to IO mode baud rate
152 unsigned old_baud_rate = port.GetBaudrate();
153 if (old_baud_rate == 9600)
154 old_baud_rate = 0;
155 else if (old_baud_rate != 0 && !port.SetBaudrate(9600))
156 return false;
158 bool success = ReadFlightListInner(port, flight_list, env);
160 // restore baudrate
161 if (old_baud_rate != 0)
162 port.SetBaudrate(old_baud_rate);
164 return success;
167 bool
168 VolksloggerDevice::DownloadFlight(const RecordedFlightInfo &flight,
169 const TCHAR *path,
170 OperationEnvironment &env)
172 port.StopRxThread();
174 // change to IO mode baud rate
175 unsigned old_baud_rate = port.GetBaudrate();
176 if (old_baud_rate == 9600)
177 old_baud_rate = 0;
178 else if (old_baud_rate != 0 && !port.SetBaudrate(9600))
179 return false;
181 bool success = DownloadFlightInner(port, bulkrate,
182 flight, path, env);
183 // restore baudrate
184 if (old_baud_rate != 0)
185 port.SetBaudrate(old_baud_rate);
187 return success;