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/multiplay.hxx>
70 #include <Network/HLA/hla.hxx>
73 #include "globals.hxx"
94 // configure a port based on the config string
96 FGIO::parse_port_config( const string& config )
98 SG_LOG( SG_IO, SG_INFO, "Parse I/O channel request: " << config );
100 string_list tokens = simgear::strutils::split( config, "," );
103 SG_LOG( SG_IO, SG_ALERT,
104 "Port configuration error: empty config string" );
108 string protocol = tokens[0];
109 SG_LOG( SG_IO, SG_INFO, " protocol = " << protocol );
115 if ( protocol == "atcsim" ) {
116 FGATCMain *atcsim = new FGATCMain;
117 atcsim->set_hz( 30 );
118 if ( tokens.size() != 6 ) {
119 SG_LOG( SG_IO, SG_ALERT, "Usage: --atcsim=[no-]pedals,"
120 << "input0_config,input1_config,"
121 << "output0_config,output1_config,file.nas" );
125 if ( tokens[1] == "no-pedals" ) {
126 fgSetBool( "/input/atcsim/ignore-pedal-controls", true );
128 fgSetBool( "/input/atcsim/ignore-pedal-controls", false );
130 atcsim->set_path_names(tokens[2], tokens[3], tokens[4], tokens[5]);
132 } else if ( protocol == "atlas" ) {
133 FGAtlas *atlas = new FGAtlas;
135 } else if ( protocol == "opengc" ) {
137 // printf("Parsed opengc\n"); cin >> wait;
138 FGOpenGC *opengc = new FGOpenGC;
140 } else if ( protocol == "AV400" ) {
141 FGAV400 *av400 = new FGAV400;
143 } else if ( protocol == "AV400Sim" ) {
144 FGAV400Sim *av400sim = new FGAV400Sim;
146 } else if ( protocol == "AV400WSimA" ) {
147 FGAV400WSimA *av400wsima = new FGAV400WSimA;
149 } else if ( protocol == "AV400WSimB" ) {
150 FGAV400WSimB *av400wsimb = new FGAV400WSimB;
152 } else if ( protocol == "garmin" ) {
153 FGGarmin *garmin = new FGGarmin;
155 } else if ( protocol == "httpd" ) {
157 string port = tokens[1];
158 return new FGHttpd( atoi(port.c_str()) );
159 #ifdef FG_JPEG_SERVER
160 } else if ( protocol == "jpg-httpd" ) {
162 string port = tokens[1];
163 return new FGJpegHttpd( atoi(port.c_str()) );
165 } else if ( protocol == "joyclient" ) {
166 FGJoyClient *joyclient = new FGJoyClient;
168 } else if ( protocol == "jsclient" ) {
169 FGJsClient *jsclient = new FGJsClient;
171 } else if ( protocol == "native" ) {
172 FGNative *native = new FGNative;
174 } else if ( protocol == "native-ctrls" ) {
175 FGNativeCtrls *native_ctrls = new FGNativeCtrls;
177 } else if ( protocol == "native-fdm" ) {
178 FGNativeFDM *native_fdm = new FGNativeFDM;
180 } else if ( protocol == "native-gui" ) {
181 FGNativeGUI *net_gui = new FGNativeGUI;
183 } else if ( protocol == "nmea" ) {
184 FGNMEA *nmea = new FGNMEA;
186 } else if ( protocol == "props" || protocol == "telnet" ) {
187 io = new FGProps( tokens );
189 } else if ( protocol == "pve" ) {
190 FGPVE *pve = new FGPVE;
192 } else if ( protocol == "ray" ) {
193 FGRAY *ray = new FGRAY;
195 } else if ( protocol == "rul" ) {
196 FGRUL *rul = new FGRUL;
198 } else if ( protocol == "generic" ) {
200 if (tokens[1] == "socket") {
202 } else if (tokens[1] == "file") {
208 if (configToken >= tokens.size()) {
209 SG_LOG( SG_IO, SG_ALERT, "Not enough tokens passed for the generic protocol.");
213 FGGeneric *generic = new FGGeneric( tokens );
215 } else if ( protocol == "multiplay" ) {
216 if ( tokens.size() != 5 ) {
217 SG_LOG( SG_IO, SG_ALERT, "Ignoring invalid --multiplay option "
218 "(4 arguments expected: --multiplay=dir,hz,hostname,port)" );
221 string dir = tokens[1];
222 string rate = tokens[2];
223 string host = tokens[3];
224 string port = tokens[4];
225 return new FGMultiplay(dir, atoi(rate.c_str()), host, atoi(port.c_str()));
227 } else if ( protocol == "hla" ) {
228 return new FGHLA(tokens);
234 catch (FGProtocolConfigError& err)
236 SG_LOG( SG_IO, SG_ALERT, "Port configuration error: " << err.what() );
241 if (tokens.size() < 3) {
242 SG_LOG( SG_IO, SG_ALERT, "Incompatible number of network arguments.");
245 string medium = tokens[1];
246 SG_LOG( SG_IO, SG_INFO, " medium = " << medium );
248 string direction = tokens[2];
249 io->set_direction( direction );
250 SG_LOG( SG_IO, SG_INFO, " direction = " << direction );
252 string hertz_str = tokens[3];
253 double hertz = atof( hertz_str.c_str() );
255 SG_LOG( SG_IO, SG_INFO, " hertz = " << hertz );
257 if ( medium == "serial" ) {
258 if ( tokens.size() < 5) {
259 SG_LOG( SG_IO, SG_ALERT, "Incompatible number of arguments for serial communications.");
263 string device = tokens[4];
264 SG_LOG( SG_IO, SG_INFO, " device = " << device );
267 string baud = tokens[5];
268 SG_LOG( SG_IO, SG_INFO, " baud = " << baud );
271 SGSerial *ch = new SGSerial( device, baud );
272 io->set_io_channel( ch );
274 if ( protocol == "AV400WSimB" ) {
275 if ( tokens.size() < 7 ) {
276 SG_LOG( SG_IO, SG_ALERT, "Missing second hz for AV400WSimB.");
279 FGAV400WSimB *fgavb = static_cast<FGAV400WSimB*>(io);
280 string hz2_str = tokens[6];
281 double hz2 = atof(hz2_str.c_str());
284 } else if ( medium == "file" ) {
286 if ( tokens.size() < 4) {
287 SG_LOG( SG_IO, SG_ALERT, "Incompatible number of arguments for file I/O.");
291 string file = tokens[4];
292 SG_LOG( SG_IO, SG_INFO, " file name = " << file );
294 if (tokens.size() >= 7 && tokens[6] == "repeat") {
295 if (tokens.size() >= 8) {
296 repeat = atoi(tokens[7].c_str());
297 FGGeneric* generic = dynamic_cast<FGGeneric*>(io);
299 generic->setExitOnError(true);
304 SGFile *ch = new SGFile( file, repeat );
305 io->set_io_channel( ch );
306 } else if ( medium == "socket" ) {
307 if ( tokens.size() < 6) {
308 SG_LOG( SG_IO, SG_ALERT, "Incompatible number of arguments for socket communications.");
311 string hostname = tokens[4];
312 string port = tokens[5];
313 string style = tokens[6];
315 SG_LOG( SG_IO, SG_INFO, " hostname = " << hostname );
316 SG_LOG( SG_IO, SG_INFO, " port = " << port );
317 SG_LOG( SG_IO, SG_INFO, " style = " << style );
319 io->set_io_channel( new SGSocket( hostname, port, style ) );
326 // step through the port config streams (from fgOPTIONS) and setup
327 // serial port channels for each
331 // SG_LOG( SG_IO, SG_INFO, "I/O Channel initialization, " <<
332 // globals->get_channel_options_list()->size() << " requests." );
334 _realDeltaTime = fgGetNode("/sim/time/delta-realtime-sec");
338 // we could almost do this in a single step except pushing a valid
339 // port onto the port list copies the structure and destroys the
340 // original, which closes the port and frees up the fd ... doh!!!
342 // parse the configuration strings and store the results in the
343 // appropriate FGIOChannel structures
344 string_list::iterator i = globals->get_channel_options_list()->begin();
345 string_list::iterator end = globals->get_channel_options_list()->end();
346 for (; i != end; ++i )
348 p = parse_port_config( *i );
351 io_channels.push_back( p );
352 if ( !p->is_enabled() ) {
353 SG_LOG( SG_IO, SG_ALERT, "I/O Channel config failed." );
357 SG_LOG( SG_IO, SG_INFO, "I/O Channel parse failed." );
367 // process any IO channel work
369 FGIO::update( double /* delta_time_sec */ )
371 // use wall-clock, not simulation, delta time, so that network
372 // protocols update when the simulation is paused
373 // see http://code.google.com/p/flightgear-bugs/issues/detail?id=125
374 double delta_time_sec = _realDeltaTime->getDoubleValue();
376 ProtocolVec::iterator i = io_channels.begin();
377 ProtocolVec::iterator end = io_channels.end();
378 for (; i != end; ++i ) {
380 if (!p->is_enabled()) {
384 p->dec_count_down( delta_time_sec );
385 double dt = 1 / p->get_hz();
386 if ( p->get_count_down() < 0.33 * dt ) {
389 while ( p->get_count_down() < 0.33 * dt ) {
390 p->inc_count_down( dt );
392 } // of channel processing
393 } // of io_channels iteration
401 ProtocolVec::iterator i = io_channels.begin();
402 ProtocolVec::iterator end = io_channels.end();
403 for (; i != end; ++i )
406 if ( p->is_enabled() ) {