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>
70 #include <Network/HLA/hla.hxx>
73 #include "globals.hxx"
91 // configure a port based on the config string
93 FGIO::parse_port_config( const string& config )
95 SG_LOG( SG_IO, SG_INFO, "Parse I/O channel request: " << config );
97 string_list tokens = simgear::strutils::split( config, "," );
100 SG_LOG( SG_IO, SG_ALERT,
101 "Port configuration error: empty config string" );
105 string protocol = tokens[0];
106 SG_LOG( SG_IO, SG_INFO, " protocol = " << protocol );
108 FGProtocol *io = NULL;
112 if ( protocol == "atcsim" ) {
113 FGATCMain *atcsim = new FGATCMain;
114 atcsim->set_hz( 30 );
115 if ( tokens.size() != 6 ) {
116 SG_LOG( SG_IO, SG_ALERT, "Usage: --atcsim=[no-]pedals,"
117 << "input0_config,input1_config,"
118 << "output0_config,output1_config,file.nas" );
122 if ( tokens[1] == "no-pedals" ) {
123 fgSetBool( "/input/atcsim/ignore-pedal-controls", true );
125 fgSetBool( "/input/atcsim/ignore-pedal-controls", false );
127 atcsim->set_path_names(tokens[2], tokens[3], tokens[4], tokens[5]);
129 } else if ( protocol == "atlas" ) {
130 FGAtlas *atlas = new FGAtlas;
132 } else if ( protocol == "opengc" ) {
133 FGOpenGC *opengc = new FGOpenGC;
135 } else if ( protocol == "AV400" ) {
136 FGAV400 *av400 = new FGAV400;
138 } else if ( protocol == "AV400Sim" ) {
139 FGAV400Sim *av400sim = new FGAV400Sim;
141 } else if ( protocol == "AV400WSimA" ) {
142 FGAV400WSimA *av400wsima = new FGAV400WSimA;
144 } else if ( protocol == "AV400WSimB" ) {
145 FGAV400WSimB *av400wsimb = new FGAV400WSimB;
147 } else if ( protocol == "garmin" ) {
148 FGGarmin *garmin = new FGGarmin;
150 } else if ( protocol == "httpd" ) {
152 string port = tokens[1];
153 return new FGHttpd( atoi(port.c_str()) );
154 #ifdef FG_JPEG_SERVER
155 } else if ( protocol == "jpg-httpd" ) {
157 string port = tokens[1];
158 return new FGJpegHttpd( atoi(port.c_str()) );
160 } else if ( protocol == "joyclient" ) {
161 FGJoyClient *joyclient = new FGJoyClient;
163 } else if ( protocol == "jsclient" ) {
164 FGJsClient *jsclient = new FGJsClient;
166 } else if ( protocol == "native" ) {
167 FGNative *native = new FGNative;
169 } else if ( protocol == "native-ctrls" ) {
170 FGNativeCtrls *native_ctrls = new FGNativeCtrls;
172 } else if ( protocol == "native-fdm" ) {
173 FGNativeFDM *native_fdm = new FGNativeFDM;
175 } else if ( protocol == "native-gui" ) {
176 FGNativeGUI *net_gui = new FGNativeGUI;
178 } else if ( protocol == "nmea" ) {
179 FGNMEA *nmea = new FGNMEA;
181 } else if ( protocol == "props" || protocol == "telnet" ) {
182 io = new FGProps( tokens );
184 } else if ( protocol == "pve" ) {
185 FGPVE *pve = new FGPVE;
187 } else if ( protocol == "ray" ) {
188 FGRAY *ray = new FGRAY;
190 } else if ( protocol == "rul" ) {
191 FGRUL *rul = new FGRUL;
193 } else if ( protocol == "generic" ) {
194 FGGeneric *generic = new FGGeneric( tokens );
195 if (!generic->getInitOk())
197 // failed to initialize (i.e. invalid configuration)
202 } else if ( protocol == "multiplay" ) {
203 if ( tokens.size() != 5 ) {
204 SG_LOG( SG_IO, SG_ALERT, "Ignoring invalid --multiplay option "
205 "(4 arguments expected: --multiplay=dir,hz,hostname,port)" );
208 string dir = tokens[1];
209 int rate = atoi(tokens[2].c_str());
210 string host = tokens[3];
212 short port = atoi(tokens[4].c_str());
214 // multiplay used to be handled by an FGProtocol, but no longer. This code
215 // retains compatibility with existing command-line syntax
216 fgSetInt("/sim/multiplay/tx-rate-hz", rate);
218 fgSetInt("/sim/multiplay/rxport", port);
219 fgSetString("/sim/multiplay/rxhost", host.c_str());
220 } else if (dir == "out") {
221 fgSetInt("/sim/multiplay/txport", port);
222 fgSetString("/sim/multiplay/txhost", host.c_str());
228 else if ( protocol == "hla" ) {
229 return new FGHLA(tokens);
231 else if ( protocol == "hla-local" ) {
232 // This is just about to bring up some defaults
233 if (tokens.size() != 2) {
234 SG_LOG( SG_IO, SG_ALERT, "Ignoring invalid --hla-local option "
235 "(one argument expected: --hla-local=<federationname>" );
238 tokens.insert(tokens.begin(), "");
239 tokens.insert(tokens.begin(), "60");
240 tokens.insert(tokens.begin(), "bi");
241 tokens.push_back("fg-local.xml");
242 return new FGHLA(tokens);
249 catch (FGProtocolConfigError& err)
251 SG_LOG( SG_IO, SG_ALERT, "Port configuration error: " << err.what() );
256 if (tokens.size() < 3) {
257 SG_LOG( SG_IO, SG_ALERT, "Incompatible number of network arguments.");
261 string medium = tokens[1];
262 SG_LOG( SG_IO, SG_INFO, " medium = " << medium );
264 string direction = tokens[2];
265 io->set_direction( direction );
266 SG_LOG( SG_IO, SG_INFO, " direction = " << direction );
268 string hertz_str = tokens[3];
269 double hertz = atof( hertz_str.c_str() );
271 SG_LOG( SG_IO, SG_INFO, " hertz = " << hertz );
273 if ( medium == "serial" ) {
274 if ( tokens.size() < 5) {
275 SG_LOG( SG_IO, SG_ALERT, "Incompatible number of arguments for serial communications.");
280 string device = tokens[4];
281 SG_LOG( SG_IO, SG_INFO, " device = " << device );
284 string baud = tokens[5];
285 SG_LOG( SG_IO, SG_INFO, " baud = " << baud );
288 SGSerial *ch = new SGSerial( device, baud );
289 io->set_io_channel( ch );
291 if ( protocol == "AV400WSimB" ) {
292 if ( tokens.size() < 7 ) {
293 SG_LOG( SG_IO, SG_ALERT, "Missing second hz for AV400WSimB.");
297 FGAV400WSimB *fgavb = static_cast<FGAV400WSimB*>(io);
298 string hz2_str = tokens[6];
299 double hz2 = atof(hz2_str.c_str());
302 } else if ( medium == "file" ) {
304 if ( tokens.size() < 4) {
305 SG_LOG( SG_IO, SG_ALERT, "Incompatible number of arguments for file I/O.");
310 string file = tokens[4];
311 SG_LOG( SG_IO, SG_INFO, " file name = " << file );
313 if (tokens.size() >= 7 && tokens[6] == "repeat") {
314 if (tokens.size() >= 8) {
315 repeat = atoi(tokens[7].c_str());
316 FGGeneric* generic = dynamic_cast<FGGeneric*>(io);
318 generic->setExitOnError(true);
323 SGFile *ch = new SGFile( file, repeat );
324 io->set_io_channel( ch );
325 } else if ( medium == "socket" ) {
326 if ( tokens.size() < 6) {
327 SG_LOG( SG_IO, SG_ALERT, "Incompatible number of arguments for socket communications.");
331 string hostname = tokens[4];
332 string port = tokens[5];
333 string style = tokens[6];
335 SG_LOG( SG_IO, SG_INFO, " hostname = " << hostname );
336 SG_LOG( SG_IO, SG_INFO, " port = " << port );
337 SG_LOG( SG_IO, SG_INFO, " style = " << style );
339 io->set_io_channel( new SGSocket( hostname, port, style ) );
346 // step through the port config streams (from fgOPTIONS) and setup
347 // serial port channels for each
351 // SG_LOG( SG_IO, SG_INFO, "I/O Channel initialization, " <<
352 // globals->get_channel_options_list()->size() << " requests." );
354 _realDeltaTime = fgGetNode("/sim/time/delta-realtime-sec");
356 // we could almost do this in a single step except pushing a valid
357 // port onto the port list copies the structure and destroys the
358 // original, which closes the port and frees up the fd ... doh!!!
360 string_list::iterator i = globals->get_channel_options_list()->begin();
361 string_list::iterator end = globals->get_channel_options_list()->end();
362 for (; i != end; ++i ) {
364 } // of channel options iteration
367 // add another I/O channel
368 void FGIO::add_channel(const string& config)
370 // parse the configuration string and store the results in the
371 // appropriate FGIOChannel structure
372 FGProtocol *p = parse_port_config( config );
379 if ( !p->is_enabled() ) {
380 SG_LOG( SG_IO, SG_ALERT, "I/O Channel config failed." );
385 io_channels.push_back( p );
393 // process any IO channel work
395 FGIO::update( double /* delta_time_sec */ )
397 // use wall-clock, not simulation, delta time, so that network
398 // protocols update when the simulation is paused
399 // see http://code.google.com/p/flightgear-bugs/issues/detail?id=125
400 double delta_time_sec = _realDeltaTime->getDoubleValue();
402 ProtocolVec::iterator i = io_channels.begin();
403 ProtocolVec::iterator end = io_channels.end();
404 for (; i != end; ++i ) {
406 if (!p->is_enabled()) {
410 p->dec_count_down( delta_time_sec );
411 double dt = 1 / p->get_hz();
412 if ( p->get_count_down() < 0.33 * dt ) {
415 while ( p->get_count_down() < 0.33 * dt ) {
416 p->inc_count_down( dt );
418 } // of channel processing
419 } // of io_channels iteration
425 ProtocolVec::iterator i = io_channels.begin();
426 ProtocolVec::iterator end = io_channels.end();
427 for (; i != end; ++i )
430 if ( p->is_enabled() ) {