1 // fg_io.cxx -- higher level I/O channel management routines
3 // Written by Curtis Olson, started November 1999.
5 // Copyright (C) 1999 Curtis L. Olson - http://www.flightgear.org/~curt
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27 #include <simgear/compiler.h>
29 #include <cstdlib> // atoi()
34 #include <simgear/debug/logstream.hxx>
35 #include <simgear/io/iochannel.hxx>
36 #include <simgear/io/sg_file.hxx>
37 #include <simgear/io/sg_serial.hxx>
38 #include <simgear/io/sg_socket.hxx>
39 #include <simgear/io/sg_socket_udp.hxx>
40 #include <simgear/math/sg_types.hxx>
41 #include <simgear/timing/timestamp.hxx>
42 #include <simgear/misc/strutils.hxx>
44 #include <Network/protocol.hxx>
45 #include <Network/ATC-Main.hxx>
46 #include <Network/atlas.hxx>
47 #include <Network/AV400.hxx>
48 #include <Network/AV400Sim.hxx>
49 #include <Network/AV400WSim.hxx>
50 #include <Network/garmin.hxx>
51 #include <Network/httpd.hxx>
53 # include <Network/jpg-httpd.hxx>
55 #include <Network/joyclient.hxx>
56 #include <Network/jsclient.hxx>
57 #include <Network/native.hxx>
58 #include <Network/native_ctrls.hxx>
59 #include <Network/native_fdm.hxx>
60 #include <Network/native_gui.hxx>
61 #include <Network/opengc.hxx>
62 #include <Network/nmea.hxx>
63 #include <Network/props.hxx>
64 #include <Network/pve.hxx>
65 #include <Network/ray.hxx>
66 #include <Network/rul.hxx>
67 #include <Network/generic.hxx>
68 #include <Network/HTTPClient.hxx>
71 #include <Network/HLA/hla.hxx>
74 #include "globals.hxx"
95 // configure a port based on the config string
97 FGIO::parse_port_config( const string& config )
99 SG_LOG( SG_IO, SG_INFO, "Parse I/O channel request: " << config );
101 string_list tokens = simgear::strutils::split( config, "," );
104 SG_LOG( SG_IO, SG_ALERT,
105 "Port configuration error: empty config string" );
109 string protocol = tokens[0];
110 SG_LOG( SG_IO, SG_INFO, " protocol = " << protocol );
116 if ( protocol == "atcsim" ) {
117 FGATCMain *atcsim = new FGATCMain;
118 atcsim->set_hz( 30 );
119 if ( tokens.size() != 6 ) {
120 SG_LOG( SG_IO, SG_ALERT, "Usage: --atcsim=[no-]pedals,"
121 << "input0_config,input1_config,"
122 << "output0_config,output1_config,file.nas" );
126 if ( tokens[1] == "no-pedals" ) {
127 fgSetBool( "/input/atcsim/ignore-pedal-controls", true );
129 fgSetBool( "/input/atcsim/ignore-pedal-controls", false );
131 atcsim->set_path_names(tokens[2], tokens[3], tokens[4], tokens[5]);
133 } else if ( protocol == "atlas" ) {
134 FGAtlas *atlas = new FGAtlas;
136 } else if ( protocol == "opengc" ) {
138 // printf("Parsed opengc\n"); cin >> wait;
139 FGOpenGC *opengc = new FGOpenGC;
141 } else if ( protocol == "AV400" ) {
142 FGAV400 *av400 = new FGAV400;
144 } else if ( protocol == "AV400Sim" ) {
145 FGAV400Sim *av400sim = new FGAV400Sim;
147 } else if ( protocol == "AV400WSimA" ) {
148 FGAV400WSimA *av400wsima = new FGAV400WSimA;
150 } else if ( protocol == "AV400WSimB" ) {
151 FGAV400WSimB *av400wsimb = new FGAV400WSimB;
153 } else if ( protocol == "garmin" ) {
154 FGGarmin *garmin = new FGGarmin;
156 } else if ( protocol == "httpd" ) {
158 string port = tokens[1];
159 return new FGHttpd( atoi(port.c_str()) );
160 #ifdef FG_JPEG_SERVER
161 } else if ( protocol == "jpg-httpd" ) {
163 string port = tokens[1];
164 return new FGJpegHttpd( atoi(port.c_str()) );
166 } else if ( protocol == "joyclient" ) {
167 FGJoyClient *joyclient = new FGJoyClient;
169 } else if ( protocol == "jsclient" ) {
170 FGJsClient *jsclient = new FGJsClient;
172 } else if ( protocol == "native" ) {
173 FGNative *native = new FGNative;
175 } else if ( protocol == "native-ctrls" ) {
176 FGNativeCtrls *native_ctrls = new FGNativeCtrls;
178 } else if ( protocol == "native-fdm" ) {
179 FGNativeFDM *native_fdm = new FGNativeFDM;
181 } else if ( protocol == "native-gui" ) {
182 FGNativeGUI *net_gui = new FGNativeGUI;
184 } else if ( protocol == "nmea" ) {
185 FGNMEA *nmea = new FGNMEA;
187 } else if ( protocol == "props" || protocol == "telnet" ) {
188 io = new FGProps( tokens );
190 } else if ( protocol == "pve" ) {
191 FGPVE *pve = new FGPVE;
193 } else if ( protocol == "ray" ) {
194 FGRAY *ray = new FGRAY;
196 } else if ( protocol == "rul" ) {
197 FGRUL *rul = new FGRUL;
199 } else if ( protocol == "generic" ) {
200 FGGeneric *generic = new FGGeneric( tokens );
201 if (!generic->getInitOk())
203 // failed to initialize (i.e. invalid configuration)
208 } else if ( protocol == "multiplay" ) {
209 if ( tokens.size() != 5 ) {
210 SG_LOG( SG_IO, SG_ALERT, "Ignoring invalid --multiplay option "
211 "(4 arguments expected: --multiplay=dir,hz,hostname,port)" );
214 string dir = tokens[1];
215 int rate = atoi(tokens[2].c_str());
216 string host = tokens[3];
218 short port = atoi(tokens[4].c_str());
220 // multiplay used to be handled by an FGProtocol, but no longer. This code
221 // retains compatability with existing command-line syntax
222 fgSetInt("/sim/multiplay/tx-rate-hz", rate);
224 fgSetInt("/sim/multiplay/rxport", port);
225 fgSetString("/sim/multiplay/rxhost", host.c_str());
226 } else if (dir == "out") {
227 fgSetInt("/sim/multiplay/txport", port);
228 fgSetString("/sim/multiplay/txhost", host.c_str());
233 } else if ( protocol == "hla" ) {
234 return new FGHLA(tokens);
240 catch (FGProtocolConfigError& err)
242 SG_LOG( SG_IO, SG_ALERT, "Port configuration error: " << err.what() );
247 if (tokens.size() < 3) {
248 SG_LOG( SG_IO, SG_ALERT, "Incompatible number of network arguments.");
251 string medium = tokens[1];
252 SG_LOG( SG_IO, SG_INFO, " medium = " << medium );
254 string direction = tokens[2];
255 io->set_direction( direction );
256 SG_LOG( SG_IO, SG_INFO, " direction = " << direction );
258 string hertz_str = tokens[3];
259 double hertz = atof( hertz_str.c_str() );
261 SG_LOG( SG_IO, SG_INFO, " hertz = " << hertz );
263 if ( medium == "serial" ) {
264 if ( tokens.size() < 5) {
265 SG_LOG( SG_IO, SG_ALERT, "Incompatible number of arguments for serial communications.");
269 string device = tokens[4];
270 SG_LOG( SG_IO, SG_INFO, " device = " << device );
273 string baud = tokens[5];
274 SG_LOG( SG_IO, SG_INFO, " baud = " << baud );
277 SGSerial *ch = new SGSerial( device, baud );
278 io->set_io_channel( ch );
280 if ( protocol == "AV400WSimB" ) {
281 if ( tokens.size() < 7 ) {
282 SG_LOG( SG_IO, SG_ALERT, "Missing second hz for AV400WSimB.");
285 FGAV400WSimB *fgavb = static_cast<FGAV400WSimB*>(io);
286 string hz2_str = tokens[6];
287 double hz2 = atof(hz2_str.c_str());
290 } else if ( medium == "file" ) {
292 if ( tokens.size() < 4) {
293 SG_LOG( SG_IO, SG_ALERT, "Incompatible number of arguments for file I/O.");
297 string file = tokens[4];
298 SG_LOG( SG_IO, SG_INFO, " file name = " << file );
300 if (tokens.size() >= 7 && tokens[6] == "repeat") {
301 if (tokens.size() >= 8) {
302 repeat = atoi(tokens[7].c_str());
303 FGGeneric* generic = dynamic_cast<FGGeneric*>(io);
305 generic->setExitOnError(true);
310 SGFile *ch = new SGFile( file, repeat );
311 io->set_io_channel( ch );
312 } else if ( medium == "socket" ) {
313 if ( tokens.size() < 6) {
314 SG_LOG( SG_IO, SG_ALERT, "Incompatible number of arguments for socket communications.");
317 string hostname = tokens[4];
318 string port = tokens[5];
319 string style = tokens[6];
321 SG_LOG( SG_IO, SG_INFO, " hostname = " << hostname );
322 SG_LOG( SG_IO, SG_INFO, " port = " << port );
323 SG_LOG( SG_IO, SG_INFO, " style = " << style );
325 io->set_io_channel( new SGSocket( hostname, port, style ) );
332 // step through the port config streams (from fgOPTIONS) and setup
333 // serial port channels for each
337 // SG_LOG( SG_IO, SG_INFO, "I/O Channel initialization, " <<
338 // globals->get_channel_options_list()->size() << " requests." );
340 _realDeltaTime = fgGetNode("/sim/time/delta-realtime-sec");
344 // we could almost do this in a single step except pushing a valid
345 // port onto the port list copies the structure and destroys the
346 // original, which closes the port and frees up the fd ... doh!!!
348 // parse the configuration strings and store the results in the
349 // appropriate FGIOChannel structures
350 string_list::iterator i = globals->get_channel_options_list()->begin();
351 string_list::iterator end = globals->get_channel_options_list()->end();
352 for (; i != end; ++i ) {
353 p = parse_port_config( *i );
359 if ( !p->is_enabled() ) {
360 SG_LOG( SG_IO, SG_ALERT, "I/O Channel config failed." );
365 io_channels.push_back( p );
366 } // of channel options iteration
374 // process any IO channel work
376 FGIO::update( double /* delta_time_sec */ )
378 if (FGHTTPClient::haveInstance()) {
379 FGHTTPClient::instance()->update();
382 // use wall-clock, not simulation, delta time, so that network
383 // protocols update when the simulation is paused
384 // see http://code.google.com/p/flightgear-bugs/issues/detail?id=125
385 double delta_time_sec = _realDeltaTime->getDoubleValue();
387 ProtocolVec::iterator i = io_channels.begin();
388 ProtocolVec::iterator end = io_channels.end();
389 for (; i != end; ++i ) {
391 if (!p->is_enabled()) {
395 p->dec_count_down( delta_time_sec );
396 double dt = 1 / p->get_hz();
397 if ( p->get_count_down() < 0.33 * dt ) {
400 while ( p->get_count_down() < 0.33 * dt ) {
401 p->inc_count_down( dt );
403 } // of channel processing
404 } // of io_channels iteration
412 ProtocolVec::iterator i = io_channels.begin();
413 ProtocolVec::iterator end = io_channels.end();
414 for (; i != end; ++i )
417 if ( p->is_enabled() ) {