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 "Util/StaticString.hpp"
25 #include "Math/fixed.hpp"
26 #include "Time/PeriodClock.hpp"
27 #include "OS/SocketDescriptor.hpp"
28 #include "OS/SocketAddress.hpp"
37 int main(int argc
, char **argv
)
39 // Determine on which TCP port to connect to the server
42 fprintf(stderr
, "This program opens a TCP connection to a server which is assumed ");
43 fprintf(stderr
, "to be at 127.0.0.1, and sends artificial FlyNet vario data.\n\n");
44 fprintf(stderr
, "Usage: %s PORT\n", argv
[0]);
45 fprintf(stderr
, "Defaulting to port 4353\n");
51 // Convert IP address to binary form
52 SocketAddress server_address
;
53 if (!server_address
.Lookup("127.0.0.1", tcp_port
, AF_INET
)) {
54 fprintf(stderr
, "Failed to look up address\n");
58 // Create socket for the outgoing connection
59 SocketDescriptor sock
;
60 if (!sock
.CreateTCP()) {
65 // Connect to the specified server
66 if (!sock
.Connect(server_address
))
72 PeriodClock start_clock
;
75 PeriodClock pressure_clock
;
76 PeriodClock battery_clock
;
78 fixed pressure
= fixed(101300);
79 unsigned battery_level
= 11;
81 if (pressure_clock
.CheckUpdate(48)) {
82 NarrowString
<16> sentence
;
84 int elapsed_ms
= start_clock
.Elapsed();
85 fixed elapsed
= fixed(elapsed_ms
) / 1000;
86 fixed vario
= sin(elapsed
/ 3) * cos(elapsed
/ 10) *
87 cos(elapsed
/ 20 + fixed(2)) * 3;
89 fixed pressure_vario
= -vario
* fixed(12.5);
90 fixed delta_pressure
= pressure_vario
* 48 / 1000;
91 pressure
+= delta_pressure
;
94 sentence
.AppendFormat("%08X", uround(pressure
));
97 sock
.Write(sentence
.c_str(), sentence
.length());
100 if (battery_clock
.CheckUpdate(11000)) {
101 NarrowString
<16> sentence
;
104 if (battery_level
<= 10)
105 sentence
.AppendFormat("%X", battery_level
);
110 sock
.Write(sentence
.c_str(), sentence
.length());
112 if (battery_level
== 0)