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 string protocol = tokens[0];
106 SG_LOG( SG_IO, SG_INFO, " protocol = " << protocol );
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" ) {
134 // printf("Parsed opengc\n"); cin >> wait;
135 FGOpenGC *opengc = new FGOpenGC;
137 } else if ( protocol == "AV400" ) {
138 FGAV400 *av400 = new FGAV400;
140 } else if ( protocol == "AV400Sim" ) {
141 FGAV400Sim *av400sim = new FGAV400Sim;
143 } else if ( protocol == "garmin" ) {
144 FGGarmin *garmin = new FGGarmin;
146 } else if ( protocol == "httpd" ) {
148 string port = tokens[1];
149 return new FGHttpd( atoi(port.c_str()) );
150 #ifdef FG_JPEG_SERVER
151 } else if ( protocol == "jpg-httpd" ) {
153 string port = tokens[1];
154 return new FGJpegHttpd( atoi(port.c_str()) );
156 } else if ( protocol == "joyclient" ) {
157 FGJoyClient *joyclient = new FGJoyClient;
159 } else if ( protocol == "jsclient" ) {
160 FGJsClient *jsclient = new FGJsClient;
162 } else if ( protocol == "native" ) {
163 FGNative *native = new FGNative;
165 } else if ( protocol == "native-ctrls" ) {
166 FGNativeCtrls *native_ctrls = new FGNativeCtrls;
168 } else if ( protocol == "native-fdm" ) {
169 FGNativeFDM *native_fdm = new FGNativeFDM;
171 } else if ( protocol == "native-gui" ) {
172 FGNativeGUI *net_gui = new FGNativeGUI;
174 } else if ( protocol == "nmea" ) {
175 FGNMEA *nmea = new FGNMEA;
177 } else if ( protocol == "props" || protocol == "telnet" ) {
178 io = new FGProps( tokens );
180 } else if ( protocol == "pve" ) {
181 FGPVE *pve = new FGPVE;
183 } else if ( protocol == "ray" ) {
184 FGRAY *ray = new FGRAY;
186 } else if ( protocol == "rul" ) {
187 FGRUL *rul = new FGRUL;
189 } else if ( protocol == "generic" ) {
191 if (tokens[1] == "socket") {
193 } else if (tokens[1] == "file") {
199 if (configToken >= tokens.size()) {
200 SG_LOG( SG_IO, SG_ALERT, "Not enough tokens passed for the generic protocol.");
204 FGGeneric *generic = new FGGeneric( tokens );
206 } else if ( protocol == "multiplay" ) {
207 if ( tokens.size() != 5 ) {
208 SG_LOG( SG_IO, SG_ALERT, "Ignoring invalid --multiplay option "
209 "(4 arguments expected: --multiplay=dir,hz,hostname,port)" );
212 string dir = tokens[1];
213 string rate = tokens[2];
214 string host = tokens[3];
215 string port = tokens[4];
216 return new FGMultiplay(dir, atoi(rate.c_str()), host, atoi(port.c_str()));
221 catch (FGProtocolConfigError& err)
223 SG_LOG( SG_IO, SG_ALERT, "Port configuration error: " << err.what() );
228 if (tokens.size() < 3) {
229 SG_LOG( SG_IO, SG_ALERT, "Incompatible number of network arguments.");
232 string medium = tokens[1];
233 SG_LOG( SG_IO, SG_INFO, " medium = " << medium );
235 string direction = tokens[2];
236 io->set_direction( direction );
237 SG_LOG( SG_IO, SG_INFO, " direction = " << direction );
239 string hertz_str = tokens[3];
240 double hertz = atof( hertz_str.c_str() );
242 SG_LOG( SG_IO, SG_INFO, " hertz = " << hertz );
244 if ( medium == "serial" ) {
245 if ( tokens.size() < 5) {
246 SG_LOG( SG_IO, SG_ALERT, "Incompatible number of arguments for serial communications.");
250 string device = tokens[4];
251 SG_LOG( SG_IO, SG_INFO, " device = " << device );
254 string baud = tokens[5];
255 SG_LOG( SG_IO, SG_INFO, " baud = " << baud );
258 SGSerial *ch = new SGSerial( device, baud );
259 io->set_io_channel( ch );
260 } else if ( medium == "file" ) {
262 if ( tokens.size() < 4) {
263 SG_LOG( SG_IO, SG_ALERT, "Incompatible number of arguments for file I/O.");
267 string file = tokens[4];
268 SG_LOG( SG_IO, SG_INFO, " file name = " << file );
270 if (tokens.size() >= 7 && tokens[6] == "repeat") {
271 if (tokens.size() >= 8) {
272 repeat = atoi(tokens[7].c_str());
273 FGGeneric* generic = dynamic_cast<FGGeneric*>(io);
275 generic->setExitOnError(true);
280 SGFile *ch = new SGFile( file, repeat );
281 io->set_io_channel( ch );
282 } else if ( medium == "socket" ) {
283 if ( tokens.size() < 6) {
284 SG_LOG( SG_IO, SG_ALERT, "Incompatible number of arguments for socket communications.");
287 string hostname = tokens[4];
288 string port = tokens[5];
289 string style = tokens[6];
291 SG_LOG( SG_IO, SG_INFO, " hostname = " << hostname );
292 SG_LOG( SG_IO, SG_INFO, " port = " << port );
293 SG_LOG( SG_IO, SG_INFO, " style = " << style );
295 io->set_io_channel( new SGSocket( hostname, port, style ) );
302 // step through the port config streams (from fgOPTIONS) and setup
303 // serial port channels for each
307 // SG_LOG( SG_IO, SG_INFO, "I/O Channel initialization, " <<
308 // globals->get_channel_options_list()->size() << " requests." );
312 // we could almost do this in a single step except pushing a valid
313 // port onto the port list copies the structure and destroys the
314 // original, which closes the port and frees up the fd ... doh!!!
316 // parse the configuration strings and store the results in the
317 // appropriate FGIOChannel structures
318 typedef vector<string> container;
319 container::iterator i = globals->get_channel_options_list()->begin();
320 container::iterator end = globals->get_channel_options_list()->end();
321 for (; i != end; ++i )
323 p = parse_port_config( *i );
326 io_channels.push_back( p );
327 if ( !p->is_enabled() ) {
328 SG_LOG( SG_IO, SG_ALERT, "I/O Channel config failed." );
332 SG_LOG( SG_IO, SG_INFO, "I/O Channel parse failed." );
343 // process any IO channel work
345 FGIO::update( double delta_time_sec )
347 // cout << "processing I/O channels" << endl;
348 // cout << " Elapsed time = " << delta_time_sec << endl;
350 typedef vector< FGProtocol* > container;
351 container::iterator i = io_channels.begin();
352 container::iterator end = io_channels.end();
353 for (; i != end; ++i ) {
356 if ( p->is_enabled() ) {
357 p->dec_count_down( delta_time_sec );
358 double dt = 1 / p->get_hz();
359 if ( p->get_count_down() < 0.33 * dt ) {
362 while ( p->get_count_down() < 0.33 * dt ) {
363 p->inc_count_down( dt );
365 // double ave = elapsed_time / p->get_count();
366 // cout << " ave rate = " << ave << endl;
374 FGIO::shutdown_all() {
377 // cout << "shutting down all I/O channels" << endl;
379 typedef vector< FGProtocol* > container;
380 container::iterator i = io_channels.begin();
381 container::iterator end = io_channels.end();
382 for (; i != end; ++i )
386 if ( p->is_enabled() ) {