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()
33 #include <simgear/debug/logstream.hxx>
34 #include <simgear/io/iochannel.hxx>
35 #include <simgear/io/sg_file.hxx>
36 #include <simgear/io/sg_serial.hxx>
37 #include <simgear/io/sg_socket.hxx>
38 #include <simgear/io/sg_socket_udp.hxx>
39 #include <simgear/math/sg_types.hxx>
40 #include <simgear/timing/timestamp.hxx>
41 #include <simgear/misc/strutils.hxx>
43 #include <Network/protocol.hxx>
44 #include <Network/ATC-Main.hxx>
45 #include <Network/atlas.hxx>
46 #include <Network/AV400.hxx>
47 #include <Network/AV400Sim.hxx>
48 #include <Network/garmin.hxx>
49 #include <Network/httpd.hxx>
51 # include <Network/jpg-httpd.hxx>
53 #include <Network/joyclient.hxx>
54 #include <Network/jsclient.hxx>
55 #include <Network/native.hxx>
56 #include <Network/native_ctrls.hxx>
57 #include <Network/native_fdm.hxx>
58 #include <Network/native_gui.hxx>
59 #include <Network/opengc.hxx>
60 #include <Network/nmea.hxx>
61 #include <Network/props.hxx>
62 #include <Network/pve.hxx>
63 #include <Network/ray.hxx>
64 #include <Network/rul.hxx>
65 #include <Network/generic.hxx>
66 #include <Network/multiplay.hxx>
68 #include "globals.hxx"
82 static void delete_ptr( FGProtocol* p ) { delete p; }
87 for_each( io_channels.begin(), io_channels.end(), delete_ptr );
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 vector<string> tokens = simgear::strutils::split( config, "," );
100 SG_LOG( SG_IO, SG_ALERT,
101 "Port configuration error: empty config string" );
105 unsigned num_tokens = tokens.size();
106 string protocol = tokens[0];
107 SG_LOG( SG_IO, SG_INFO, " protocol = " << protocol );
113 if ( protocol == "atcsim" ) {
114 FGATCMain *atcsim = new FGATCMain;
115 atcsim->set_hz( 30 );
116 if ( tokens.size() != 6 ) {
117 SG_LOG( SG_IO, SG_ALERT, "Usage: --atcsim=[no-]pedals,"
118 << "input0_config,input1_config,"
119 << "output0_config,output1_config,file.nas" );
123 if ( tokens[1] == "no-pedals" ) {
124 fgSetBool( "/input/atcsim/ignore-pedal-controls", true );
126 fgSetBool( "/input/atcsim/ignore-pedal-controls", false );
128 atcsim->set_path_names(tokens[2], tokens[3], tokens[4], tokens[5]);
130 } else if ( protocol == "atlas" ) {
131 FGAtlas *atlas = new FGAtlas;
133 } else if ( protocol == "opengc" ) {
135 // printf("Parsed opengc\n"); cin >> wait;
136 FGOpenGC *opengc = new FGOpenGC;
138 } else if ( protocol == "AV400" ) {
139 FGAV400 *av400 = new FGAV400;
141 } else if ( protocol == "AV400Sim" ) {
142 FGAV400Sim *av400sim = new FGAV400Sim;
144 } else if ( protocol == "garmin" ) {
145 FGGarmin *garmin = new FGGarmin;
147 } else if ( protocol == "httpd" ) {
149 string port = tokens[1];
150 return new FGHttpd( atoi(port.c_str()) );
151 #ifdef FG_JPEG_SERVER
152 } else if ( protocol == "jpg-httpd" ) {
154 string port = tokens[1];
155 return new FGJpegHttpd( atoi(port.c_str()) );
157 } else if ( protocol == "joyclient" ) {
158 FGJoyClient *joyclient = new FGJoyClient;
160 } else if ( protocol == "jsclient" ) {
161 FGJsClient *jsclient = new FGJsClient;
163 } else if ( protocol == "native" ) {
164 FGNative *native = new FGNative;
166 } else if ( protocol == "native-ctrls" ) {
167 FGNativeCtrls *native_ctrls = new FGNativeCtrls;
169 } else if ( protocol == "native-fdm" ) {
170 FGNativeFDM *native_fdm = new FGNativeFDM;
172 } else if ( protocol == "native-gui" ) {
173 FGNativeGUI *net_gui = new FGNativeGUI;
175 } else if ( protocol == "nmea" ) {
176 FGNMEA *nmea = new FGNMEA;
178 } else if ( protocol == "props" || protocol == "telnet" ) {
179 io = new FGProps( tokens );
181 } else if ( protocol == "pve" ) {
182 FGPVE *pve = new FGPVE;
184 } else if ( protocol == "ray" ) {
185 FGRAY *ray = new FGRAY;
187 } else if ( protocol == "rul" ) {
188 FGRUL *rul = new FGRUL;
190 } else if ( protocol == "generic" ) {
192 if (tokens[1] == "socket")
194 else if (tokens[1] == "file")
198 if (configToken < num_tokens)
200 FGGeneric *generic = new FGGeneric( tokens[configToken] );
205 SG_LOG( SG_IO, SG_ALERT, "Not enough tokens passed for the generic protocol.");
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 string rate = tokens[2];
216 string host = tokens[3];
217 string port = tokens[4];
218 return new FGMultiplay(dir, atoi(rate.c_str()), host, atoi(port.c_str()));
223 catch (FGProtocolConfigError& err)
225 SG_LOG( SG_IO, SG_ALERT, "Port configuration error: " << err.what() );
230 if (tokens.size() < 3) {
231 SG_LOG( SG_IO, SG_ALERT, "Incompatible number of network arguments.");
234 string medium = tokens[1];
235 SG_LOG( SG_IO, SG_INFO, " medium = " << medium );
237 string direction = tokens[2];
238 io->set_direction( direction );
239 SG_LOG( SG_IO, SG_INFO, " direction = " << direction );
241 string hertz_str = tokens[3];
242 double hertz = atof( hertz_str.c_str() );
244 SG_LOG( SG_IO, SG_INFO, " hertz = " << hertz );
246 if ( medium == "serial" ) {
247 if ( tokens.size() < 5) {
248 SG_LOG( SG_IO, SG_ALERT, "Incompatible number of arguments for serial communications.");
252 string device = tokens[4];
253 SG_LOG( SG_IO, SG_INFO, " device = " << device );
256 string baud = tokens[5];
257 SG_LOG( SG_IO, SG_INFO, " baud = " << baud );
260 SGSerial *ch = new SGSerial( device, baud );
261 io->set_io_channel( ch );
262 } else if ( medium == "file" ) {
264 if ( tokens.size() < 4) {
265 SG_LOG( SG_IO, SG_ALERT, "Incompatible number of arguments for file I/O.");
269 string file = tokens[4];
270 SG_LOG( SG_IO, SG_INFO, " file name = " << file );
272 if (tokens.size() >= 7 && tokens[6] == "repeat") {
273 if (tokens.size() >= 8) {
274 repeat = atoi(tokens[7].c_str());
275 FGGeneric* generic = dynamic_cast<FGGeneric*>(io);
277 generic->setExitOnError(true);
282 SGFile *ch = new SGFile( file, repeat );
283 io->set_io_channel( ch );
284 } else if ( medium == "socket" ) {
285 if ( tokens.size() < 6) {
286 SG_LOG( SG_IO, SG_ALERT, "Incompatible number of arguments for socket communications.");
289 string hostname = tokens[4];
290 string port = tokens[5];
291 string style = tokens[6];
293 SG_LOG( SG_IO, SG_INFO, " hostname = " << hostname );
294 SG_LOG( SG_IO, SG_INFO, " port = " << port );
295 SG_LOG( SG_IO, SG_INFO, " style = " << style );
297 io->set_io_channel( new SGSocket( hostname, port, style ) );
304 // step through the port config streams (from fgOPTIONS) and setup
305 // serial port channels for each
309 // SG_LOG( SG_IO, SG_INFO, "I/O Channel initialization, " <<
310 // globals->get_channel_options_list()->size() << " requests." );
314 // we could almost do this in a single step except pushing a valid
315 // port onto the port list copies the structure and destroys the
316 // original, which closes the port and frees up the fd ... doh!!!
318 // parse the configuration strings and store the results in the
319 // appropriate FGIOChannel structures
320 typedef vector<string> container;
321 container::iterator i = globals->get_channel_options_list()->begin();
322 container::iterator end = globals->get_channel_options_list()->end();
323 for (; i != end; ++i )
325 p = parse_port_config( *i );
328 io_channels.push_back( p );
329 if ( !p->is_enabled() ) {
330 SG_LOG( SG_IO, SG_ALERT, "I/O Channel config failed." );
334 SG_LOG( SG_IO, SG_INFO, "I/O Channel parse failed." );
340 // process any IO channel work
342 FGIO::update( double delta_time_sec )
344 // cout << "processing I/O channels" << endl;
345 // cout << " Elapsed time = " << delta_time_sec << endl;
347 typedef vector< FGProtocol* > container;
348 container::iterator i = io_channels.begin();
349 container::iterator end = io_channels.end();
350 for (; i != end; ++i ) {
353 if ( p->is_enabled() ) {
354 p->dec_count_down( delta_time_sec );
355 double dt = 1 / p->get_hz();
356 if ( p->get_count_down() < 0.33 * dt ) {
359 while ( p->get_count_down() < 0.33 * dt ) {
360 p->inc_count_down( dt );
362 // double ave = elapsed_time / p->get_count();
363 // cout << " ave rate = " << ave << endl;
371 FGIO::shutdown_all() {
374 // cout << "shutting down all I/O channels" << endl;
376 typedef vector< FGProtocol* > container;
377 container::iterator i = io_channels.begin();
378 container::iterator end = io_channels.end();
379 for (; i != end; ++i )
383 if ( p->is_enabled() ) {