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.
25 * This program tries to connect to a TCP server at the localhost at
26 * port 4353 and feeds NMEA data read from stdin to it.
29 #include "OS/SocketDescriptor.hpp"
30 #include "OS/SocketAddress.hpp"
39 int main(int argc
, char **argv
)
41 // Determine on which TCP port to connect to the server
44 fprintf(stderr
, "This program opens a TCP connection to a server which is assumed ");
45 fprintf(stderr
, "to be at 127.0.0.1, and sends NMEA data which is read from stdin.\n\n");
46 fprintf(stderr
, "Usage: %s PORT\n", argv
[0]);
47 fprintf(stderr
, "Defaulting to port 4353\n");
53 // Convert IP address to binary form
54 SocketAddress server_address
;
55 if (!server_address
.Lookup("127.0.0.1", tcp_port
, AF_INET
)) {
56 fprintf(stderr
, "Failed to look up address\n");
60 // Create socket for the outgoing connection
61 SocketDescriptor sock
;
62 if (!sock
.CreateTCP()) {
67 // Connect to the specified server
68 if (!sock
.Connect(server_address
)) {
77 useconds_t sleep_acc
= 0;
79 // Number of characters sent
81 // Number of sentence packages sent
84 while (fgets(line
, sizeof(line
), stdin
) != NULL
) {
87 long tsleep
= l
*1e6
/(baudrate
/10);
91 if (memcmp(line
, "$GP", 3) == 0 &&
92 (memcmp(line
+ 3, "GGA", 3) == 0 ||
93 memcmp(line
+ 3, "RMC", 3) == 0) &&
95 strncmp(stamp
, line
+ 7, sizeof(stamp
)) != 0) {
96 /* the time stamp has changed - sleep for one second */
97 usleep(1e6
-sleep_acc
);
98 strncpy(stamp
, line
+ 7, sizeof(stamp
));
108 printf(">>>> Av %ld\n", c_count
/l_count
);