android/GlueIOIOPort: fix spurious errors after IOIO baud rate change
[xcsoar.git] / test / src / RunSkyLinesTracking.cpp
blobadd4bcf88212655541207fdbc92ed0322b34854b
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 "Tracking/SkyLines/Client.hpp"
25 #include "NMEA/Info.hpp"
26 #include "OS/Args.hpp"
27 #include "Util/NumberParser.hpp"
28 #include "Util/StringUtil.hpp"
29 #include "DebugReplay.hpp"
31 #ifdef HAVE_SKYLINES_TRACKING_HANDLER
32 #include "IO/Async/GlobalIOThread.hpp"
34 class Handler : public SkyLinesTracking::Handler {
35 Mutex mutex;
36 Cond cond;
38 bool done;
40 public:
41 Handler():done(false) {}
43 bool Wait(unsigned timeout_ms=5000) {
44 const ScopeLock protect(mutex);
45 return done || (cond.Wait(mutex, timeout_ms) && done);
48 protected:
49 void Done() {
50 const ScopeLock protect(mutex);
51 done = true;
52 cond.Broadcast();
55 public:
56 virtual void OnAck(unsigned id) override {
57 printf("received ack %u\n", id);
58 Done();
61 virtual void OnTraffic(unsigned pilot_id, unsigned time_of_day_ms,
62 const GeoPoint &location, int altitude) override {
63 BrokenTime time = BrokenTime::FromSecondOfDay(time_of_day_ms / 1000);
65 printf("received traffic pilot=%u time=%02u:%02u:%02u location=%f/%f altitude=%d\n",
66 pilot_id, time.hour, time.minute, time.second,
67 (double)location.longitude.Degrees(),
68 (double)location.latitude.Degrees(),
69 altitude);
70 Done();
74 #endif
76 int
77 main(int argc, char *argv[])
79 Args args(argc, argv, "HOST KEY");
80 const char *host = args.ExpectNext();
81 const char *key = args.ExpectNext();
83 SocketAddress address;
84 if (!address.Lookup(host, "5597", SOCK_DGRAM)) {
85 fprintf(stderr, "Failed to look up: %s\n", host);
86 return EXIT_FAILURE;
89 #ifdef HAVE_SKYLINES_TRACKING_HANDLER
90 InitialiseIOThread();
91 #endif
93 SkyLinesTracking::Client client;
95 #ifdef HAVE_SKYLINES_TRACKING_HANDLER
96 client.SetIOThread(io_thread);
98 Handler handler;
99 client.SetHandler(&handler);
100 #endif
102 client.SetKey(ParseUint64(key, NULL, 16));
103 if (!client.Open(address)) {
104 fprintf(stderr, "Failed to create client\n");
105 return EXIT_FAILURE;
108 if (args.IsEmpty() || StringIsEqual(args.PeekNext(), "fix")) {
109 NMEAInfo basic;
110 basic.Reset();
111 basic.UpdateClock();
112 basic.time = fixed(1);
113 basic.time_available.Update(basic.clock);
115 return client.SendFix(basic) ? EXIT_SUCCESS : EXIT_FAILURE;
116 } else if (StringIsEqual(args.PeekNext(), "ping")) {
117 client.SendPing(1);
119 #ifdef HAVE_SKYLINES_TRACKING_HANDLER
120 handler.Wait();
121 #endif
122 } else if (StringIsEqual(args.PeekNext(), "traffic")) {
123 client.SendTrafficRequest(true, true);
125 #ifdef HAVE_SKYLINES_TRACKING_HANDLER
126 handler.Wait();
127 #endif
128 } else {
129 DebugReplay *replay = CreateDebugReplay(args);
130 if (replay == NULL)
131 return EXIT_FAILURE;
133 while (replay->Next()) {
134 client.SendFix(replay->Basic());
135 usleep(100000);
139 #ifdef HAVE_SKYLINES_TRACKING_HANDLER
140 client.Close();
141 DeinitialiseIOThread();
142 #endif
144 return EXIT_SUCCESS;