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>
52 #include <Network/igc.hxx>
53 #include <Network/jpg-httpd.hxx>
54 #include <Network/joyclient.hxx>
55 #include <Network/jsclient.hxx>
56 #include <Network/native.hxx>
57 #include <Network/native_ctrls.hxx>
58 #include <Network/native_fdm.hxx>
59 #include <Network/native_gui.hxx>
60 #include <Network/opengc.hxx>
61 #include <Network/nmea.hxx>
62 #include <Network/props.hxx>
63 #include <Network/pve.hxx>
64 #include <Network/ray.hxx>
65 #include <Network/rul.hxx>
66 #include <Network/generic.hxx>
69 #include <Network/HLA/hla.hxx>
72 #include "globals.hxx"
90 // configure a port based on the config string
92 FGIO::parse_port_config( const string& config )
94 SG_LOG( SG_IO, SG_INFO, "Parse I/O channel request: " << config );
96 string_list tokens = simgear::strutils::split( config, "," );
99 SG_LOG( SG_IO, SG_ALERT,
100 "Port configuration error: empty config string" );
104 string protocol = tokens[0];
105 SG_LOG( SG_IO, SG_INFO, " protocol = " << protocol );
107 FGProtocol *io = NULL;
111 if ( protocol == "atcsim" ) {
112 FGATCMain *atcsim = new FGATCMain;
113 atcsim->set_hz( 30 );
114 if ( tokens.size() != 6 ) {
115 SG_LOG( SG_IO, SG_ALERT, "Usage: --atcsim=[no-]pedals,"
116 << "input0_config,input1_config,"
117 << "output0_config,output1_config,file.nas" );
121 if ( tokens[1] == "no-pedals" ) {
122 fgSetBool( "/input/atcsim/ignore-pedal-controls", true );
124 fgSetBool( "/input/atcsim/ignore-pedal-controls", false );
126 atcsim->set_path_names(tokens[2], tokens[3], tokens[4], tokens[5]);
128 } else if ( protocol == "atlas" ) {
129 FGAtlas *atlas = new FGAtlas;
131 } else if ( protocol == "opengc" ) {
132 FGOpenGC *opengc = new FGOpenGC;
134 } else if ( protocol == "AV400" ) {
135 FGAV400 *av400 = new FGAV400;
137 } else if ( protocol == "AV400Sim" ) {
138 FGAV400Sim *av400sim = new FGAV400Sim;
140 } else if ( protocol == "AV400WSimA" ) {
141 FGAV400WSimA *av400wsima = new FGAV400WSimA;
143 } else if ( protocol == "AV400WSimB" ) {
144 FGAV400WSimB *av400wsimb = new FGAV400WSimB;
146 } else if ( protocol == "garmin" ) {
147 FGGarmin *garmin = new FGGarmin;
149 } else if ( protocol == "igc" ) {
150 IGCProtocol *igc = new IGCProtocol;
152 } else if ( protocol == "httpd" ) {
154 string port = tokens[1];
155 return new FGHttpd( atoi(port.c_str()) );
156 } else if ( protocol == "jpg-httpd" ) {
158 int port = simgear::strutils::to_int(tokens[1]);
159 int frameHz = 8; // maximum frame rate
160 string type = "jpeg";
162 if (tokens.size() > 2) {
163 frameHz = simgear::strutils::to_int(tokens[2]);
166 if (tokens.size() > 3) {
170 return new FGJpegHttpd(port, frameHz, type);
171 } else if ( protocol == "joyclient" ) {
172 FGJoyClient *joyclient = new FGJoyClient;
174 } else if ( protocol == "jsclient" ) {
175 FGJsClient *jsclient = new FGJsClient;
177 } else if ( protocol == "native" ) {
178 FGNative *native = new FGNative;
180 } else if ( protocol == "native-ctrls" ) {
181 FGNativeCtrls *native_ctrls = new FGNativeCtrls;
183 } else if ( protocol == "native-fdm" ) {
184 FGNativeFDM *native_fdm = new FGNativeFDM;
186 } else if ( protocol == "native-gui" ) {
187 FGNativeGUI *net_gui = new FGNativeGUI;
189 } else if ( protocol == "nmea" ) {
190 FGNMEA *nmea = new FGNMEA;
192 } else if ( protocol == "props" || protocol == "telnet" ) {
193 io = new FGProps( tokens );
195 } else if ( protocol == "pve" ) {
196 FGPVE *pve = new FGPVE;
198 } else if ( protocol == "ray" ) {
199 FGRAY *ray = new FGRAY;
201 } else if ( protocol == "rul" ) {
202 FGRUL *rul = new FGRUL;
204 } else if ( protocol == "generic" ) {
205 FGGeneric *generic = new FGGeneric( tokens );
206 if (!generic->getInitOk())
208 // failed to initialize (i.e. invalid configuration)
213 } else if ( protocol == "multiplay" ) {
214 if ( tokens.size() != 5 ) {
215 SG_LOG( SG_IO, SG_ALERT, "Ignoring invalid --multiplay option "
216 "(4 arguments expected: --multiplay=dir,hz,hostname,port)" );
219 string dir = tokens[1];
220 int rate = atoi(tokens[2].c_str());
221 string host = tokens[3];
223 short port = atoi(tokens[4].c_str());
225 // multiplay used to be handled by an FGProtocol, but no longer. This code
226 // retains compatibility with existing command-line syntax
227 fgSetInt("/sim/multiplay/tx-rate-hz", rate);
229 fgSetInt("/sim/multiplay/rxport", port);
230 fgSetString("/sim/multiplay/rxhost", host.c_str());
231 } else if (dir == "out") {
232 fgSetInt("/sim/multiplay/txport", port);
233 fgSetString("/sim/multiplay/txhost", host.c_str());
239 else if ( protocol == "hla" ) {
240 return new FGHLA(tokens);
242 else if ( protocol == "hla-local" ) {
243 // This is just about to bring up some defaults
244 if (tokens.size() != 2) {
245 SG_LOG( SG_IO, SG_ALERT, "Ignoring invalid --hla-local option "
246 "(one argument expected: --hla-local=<federationname>" );
249 tokens.insert(tokens.begin(), "");
250 tokens.insert(tokens.begin(), "60");
251 tokens.insert(tokens.begin(), "bi");
252 tokens.push_back("fg-local.xml");
253 return new FGHLA(tokens);
260 catch (FGProtocolConfigError& err)
262 SG_LOG( SG_IO, SG_ALERT, "Port configuration error: " << err.what() );
267 if (tokens.size() < 3) {
268 SG_LOG( SG_IO, SG_ALERT, "Incompatible number of network arguments.");
272 string medium = tokens[1];
273 SG_LOG( SG_IO, SG_INFO, " medium = " << medium );
275 string direction = tokens[2];
276 io->set_direction( direction );
277 SG_LOG( SG_IO, SG_INFO, " direction = " << direction );
279 string hertz_str = tokens[3];
280 double hertz = atof( hertz_str.c_str() );
282 SG_LOG( SG_IO, SG_INFO, " hertz = " << hertz );
284 if ( medium == "serial" ) {
285 if ( tokens.size() < 5) {
286 SG_LOG( SG_IO, SG_ALERT, "Incompatible number of arguments for serial communications.");
291 string device = tokens[4];
292 SG_LOG( SG_IO, SG_INFO, " device = " << device );
295 string baud = tokens[5];
296 SG_LOG( SG_IO, SG_INFO, " baud = " << baud );
299 SGSerial *ch = new SGSerial( device, baud );
300 io->set_io_channel( ch );
302 if ( protocol == "AV400WSimB" ) {
303 if ( tokens.size() < 7 ) {
304 SG_LOG( SG_IO, SG_ALERT, "Missing second hz for AV400WSimB.");
308 FGAV400WSimB *fgavb = static_cast<FGAV400WSimB*>(io);
309 string hz2_str = tokens[6];
310 double hz2 = atof(hz2_str.c_str());
313 } else if ( medium == "file" ) {
315 if ( tokens.size() < 4) {
316 SG_LOG( SG_IO, SG_ALERT, "Incompatible number of arguments for file I/O.");
321 string file = tokens[4];
322 SG_LOG( SG_IO, SG_INFO, " file name = " << file );
324 if (tokens.size() >= 7 && tokens[6] == "repeat") {
325 if (tokens.size() >= 8) {
326 repeat = atoi(tokens[7].c_str());
327 FGGeneric* generic = dynamic_cast<FGGeneric*>(io);
329 generic->setExitOnError(true);
334 SGFile *ch = new SGFile( file, repeat );
335 io->set_io_channel( ch );
336 } else if ( medium == "socket" ) {
337 if ( tokens.size() < 6) {
338 SG_LOG( SG_IO, SG_ALERT, "Incompatible number of arguments for socket communications.");
342 string hostname = tokens[4];
343 string port = tokens[5];
344 string style = tokens[6];
346 SG_LOG( SG_IO, SG_INFO, " hostname = " << hostname );
347 SG_LOG( SG_IO, SG_INFO, " port = " << port );
348 SG_LOG( SG_IO, SG_INFO, " style = " << style );
350 io->set_io_channel( new SGSocket( hostname, port, style ) );
357 // step through the port config streams (from fgOPTIONS) and setup
358 // serial port channels for each
362 // SG_LOG( SG_IO, SG_INFO, "I/O Channel initialization, " <<
363 // globals->get_channel_options_list()->size() << " requests." );
365 _realDeltaTime = fgGetNode("/sim/time/delta-realtime-sec");
367 // we could almost do this in a single step except pushing a valid
368 // port onto the port list copies the structure and destroys the
369 // original, which closes the port and frees up the fd ... doh!!!
371 string_list::iterator i = globals->get_channel_options_list()->begin();
372 string_list::iterator end = globals->get_channel_options_list()->end();
373 for (; i != end; ++i ) {
375 } // of channel options iteration
378 // add another I/O channel
379 void FGIO::add_channel(const string& config)
381 // parse the configuration string and store the results in the
382 // appropriate FGIOChannel structure
383 FGProtocol *p = parse_port_config( config );
390 if ( !p->is_enabled() ) {
391 SG_LOG( SG_IO, SG_ALERT, "I/O Channel config failed." );
396 io_channels.push_back( p );
404 // process any IO channel work
406 FGIO::update( double /* delta_time_sec */ )
408 // use wall-clock, not simulation, delta time, so that network
409 // protocols update when the simulation is paused
410 // see http://code.google.com/p/flightgear-bugs/issues/detail?id=125
411 double delta_time_sec = _realDeltaTime->getDoubleValue();
413 ProtocolVec::iterator i = io_channels.begin();
414 ProtocolVec::iterator end = io_channels.end();
415 for (; i != end; ++i ) {
417 if (!p->is_enabled()) {
421 p->dec_count_down( delta_time_sec );
422 double dt = 1 / p->get_hz();
423 if ( p->get_count_down() < 0.33 * dt ) {
426 while ( p->get_count_down() < 0.33 * dt ) {
427 p->inc_count_down( dt );
429 } // of channel processing
430 } // of io_channels iteration
436 ProtocolVec::iterator i = io_channels.begin();
437 ProtocolVec::iterator end = io_channels.end();
438 for (; i != end; ++i )
441 if ( p->is_enabled() ) {