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/igc.hxx>
52 #include <Network/joyclient.hxx>
53 #include <Network/jsclient.hxx>
54 #include <Network/native.hxx>
55 #include <Network/native_ctrls.hxx>
56 #include <Network/native_fdm.hxx>
57 #include <Network/native_gui.hxx>
58 #include <Network/opengc.hxx>
59 #include <Network/nmea.hxx>
60 #include <Network/props.hxx>
61 #include <Network/pve.hxx>
62 #include <Network/ray.hxx>
63 #include <Network/rul.hxx>
64 #include <Network/generic.hxx>
67 #include <Network/HLA/hla.hxx>
70 #include "globals.hxx"
88 // configure a port based on the config string
90 FGIO::parse_port_config( const string& config )
92 SG_LOG( SG_IO, SG_INFO, "Parse I/O channel request: " << config );
94 string_list tokens = simgear::strutils::split( config, "," );
97 SG_LOG( SG_IO, SG_ALERT,
98 "Port configuration error: empty config string" );
102 string protocol = tokens[0];
103 SG_LOG( SG_IO, SG_INFO, " protocol = " << protocol );
105 FGProtocol *io = NULL;
109 if ( protocol == "atcsim" ) {
110 FGATCMain *atcsim = new FGATCMain;
111 atcsim->set_hz( 30 );
112 if ( tokens.size() != 6 ) {
113 SG_LOG( SG_IO, SG_ALERT, "Usage: --atcsim=[no-]pedals,"
114 << "input0_config,input1_config,"
115 << "output0_config,output1_config,file.nas" );
119 if ( tokens[1] == "no-pedals" ) {
120 fgSetBool( "/input/atcsim/ignore-pedal-controls", true );
122 fgSetBool( "/input/atcsim/ignore-pedal-controls", false );
124 atcsim->set_path_names(tokens[2], tokens[3], tokens[4], tokens[5]);
126 } else if ( protocol == "atlas" ) {
127 FGAtlas *atlas = new FGAtlas;
129 } else if ( protocol == "opengc" ) {
130 FGOpenGC *opengc = new FGOpenGC;
132 } else if ( protocol == "AV400" ) {
133 FGAV400 *av400 = new FGAV400;
135 } else if ( protocol == "AV400Sim" ) {
136 FGAV400Sim *av400sim = new FGAV400Sim;
138 } else if ( protocol == "AV400WSimA" ) {
139 FGAV400WSimA *av400wsima = new FGAV400WSimA;
141 } else if ( protocol == "AV400WSimB" ) {
142 FGAV400WSimB *av400wsimb = new FGAV400WSimB;
144 } else if ( protocol == "garmin" ) {
145 FGGarmin *garmin = new FGGarmin;
147 } else if ( protocol == "igc" ) {
148 IGCProtocol *igc = new IGCProtocol;
150 } else if ( protocol == "joyclient" ) {
151 FGJoyClient *joyclient = new FGJoyClient;
153 } else if ( protocol == "jsclient" ) {
154 FGJsClient *jsclient = new FGJsClient;
156 } else if ( protocol == "native" ) {
157 FGNative *native = new FGNative;
159 } else if ( protocol == "native-ctrls" ) {
160 FGNativeCtrls *native_ctrls = new FGNativeCtrls;
162 } else if ( protocol == "native-fdm" ) {
163 FGNativeFDM *native_fdm = new FGNativeFDM;
165 } else if ( protocol == "native-gui" ) {
166 FGNativeGUI *net_gui = new FGNativeGUI;
168 } else if ( protocol == "nmea" ) {
169 FGNMEA *nmea = new FGNMEA;
171 } else if ( protocol == "props" || protocol == "telnet" ) {
172 io = new FGProps( tokens );
174 } else if ( protocol == "pve" ) {
175 FGPVE *pve = new FGPVE;
177 } else if ( protocol == "ray" ) {
178 FGRAY *ray = new FGRAY;
180 } else if ( protocol == "rul" ) {
181 FGRUL *rul = new FGRUL;
183 } else if ( protocol == "generic" ) {
184 FGGeneric *generic = new FGGeneric( tokens );
185 if (!generic->getInitOk())
187 // failed to initialize (i.e. invalid configuration)
192 } else if ( protocol == "multiplay" ) {
193 if ( tokens.size() != 5 ) {
194 SG_LOG( SG_IO, SG_ALERT, "Ignoring invalid --multiplay option "
195 "(4 arguments expected: --multiplay=dir,hz,hostname,port)" );
198 string dir = tokens[1];
199 int rate = atoi(tokens[2].c_str());
200 string host = tokens[3];
202 short port = atoi(tokens[4].c_str());
204 // multiplay used to be handled by an FGProtocol, but no longer. This code
205 // retains compatibility with existing command-line syntax
206 fgSetInt("/sim/multiplay/tx-rate-hz", rate);
208 fgSetInt("/sim/multiplay/rxport", port);
209 fgSetString("/sim/multiplay/rxhost", host.c_str());
210 } else if (dir == "out") {
211 fgSetInt("/sim/multiplay/txport", port);
212 fgSetString("/sim/multiplay/txhost", host.c_str());
218 else if ( protocol == "hla" ) {
219 return new FGHLA(tokens);
221 else if ( protocol == "hla-local" ) {
222 // This is just about to bring up some defaults
223 if (tokens.size() != 2) {
224 SG_LOG( SG_IO, SG_ALERT, "Ignoring invalid --hla-local option "
225 "(one argument expected: --hla-local=<federationname>" );
228 tokens.insert(tokens.begin(), "");
229 tokens.insert(tokens.begin(), "60");
230 tokens.insert(tokens.begin(), "bi");
231 tokens.push_back("fg-local.xml");
232 return new FGHLA(tokens);
239 catch (FGProtocolConfigError& err)
241 SG_LOG( SG_IO, SG_ALERT, "Port configuration error: " << err.what() );
246 if (tokens.size() < 3) {
247 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.");
270 string device = tokens[4];
271 SG_LOG( SG_IO, SG_INFO, " device = " << device );
274 string baud = tokens[5];
275 SG_LOG( SG_IO, SG_INFO, " baud = " << baud );
278 SGSerial *ch = new SGSerial( device, baud );
279 io->set_io_channel( ch );
281 if ( protocol == "AV400WSimB" ) {
282 if ( tokens.size() < 7 ) {
283 SG_LOG( SG_IO, SG_ALERT, "Missing second hz for AV400WSimB.");
287 FGAV400WSimB *fgavb = static_cast<FGAV400WSimB*>(io);
288 string hz2_str = tokens[6];
289 double hz2 = atof(hz2_str.c_str());
292 } else if ( medium == "file" ) {
294 if ( tokens.size() < 4) {
295 SG_LOG( SG_IO, SG_ALERT, "Incompatible number of arguments for file I/O.");
300 string file = tokens[4];
301 SG_LOG( SG_IO, SG_INFO, " file name = " << file );
303 if (tokens.size() >= 7 && tokens[6] == "repeat") {
304 if (tokens.size() >= 8) {
305 repeat = atoi(tokens[7].c_str());
306 FGGeneric* generic = dynamic_cast<FGGeneric*>(io);
308 generic->setExitOnError(true);
313 SGFile *ch = new SGFile( file, repeat );
314 io->set_io_channel( ch );
315 } else if ( medium == "socket" ) {
316 if ( tokens.size() < 6) {
317 SG_LOG( SG_IO, SG_ALERT, "Incompatible number of arguments for socket communications.");
321 string hostname = tokens[4];
322 string port = tokens[5];
323 string style = tokens[6];
325 SG_LOG( SG_IO, SG_INFO, " hostname = " << hostname );
326 SG_LOG( SG_IO, SG_INFO, " port = " << port );
327 SG_LOG( SG_IO, SG_INFO, " style = " << style );
329 io->set_io_channel( new SGSocket( hostname, port, style ) );
336 // step through the port config streams (from fgOPTIONS) and setup
337 // serial port channels for each
341 // SG_LOG( SG_IO, SG_INFO, "I/O Channel initialization, " <<
342 // globals->get_channel_options_list()->size() << " requests." );
344 _realDeltaTime = fgGetNode("/sim/time/delta-realtime-sec");
346 // we could almost do this in a single step except pushing a valid
347 // port onto the port list copies the structure and destroys the
348 // original, which closes the port and frees up the fd ... doh!!!
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 ) {
354 } // of channel options iteration
357 // add another I/O channel
358 void FGIO::add_channel(const string& config)
360 // parse the configuration string and store the results in the
361 // appropriate FGIOChannel structure
362 FGProtocol *p = parse_port_config( config );
369 if ( !p->is_enabled() ) {
370 SG_LOG( SG_IO, SG_ALERT, "I/O Channel config failed." );
375 io_channels.push_back( p );
383 // process any IO channel work
385 FGIO::update( double /* delta_time_sec */ )
387 // use wall-clock, not simulation, delta time, so that network
388 // protocols update when the simulation is paused
389 // see http://code.google.com/p/flightgear-bugs/issues/detail?id=125
390 double delta_time_sec = _realDeltaTime->getDoubleValue();
392 ProtocolVec::iterator i = io_channels.begin();
393 ProtocolVec::iterator end = io_channels.end();
394 for (; i != end; ++i ) {
396 if (!p->is_enabled()) {
400 p->dec_count_down( delta_time_sec );
401 double dt = 1 / p->get_hz();
402 if ( p->get_count_down() < 0.33 * dt ) {
405 while ( p->get_count_down() < 0.33 * dt ) {
406 p->inc_count_down( dt );
408 } // of channel processing
409 } // of io_channels iteration
415 ProtocolVec::iterator i = io_channels.begin();
416 ProtocolVec::iterator end = io_channels.end();
417 for (; i != end; ++i )
420 if ( p->is_enabled() ) {