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/garmin.hxx>
48 #include <Network/httpd.hxx>
50 # include <Network/jpg-httpd.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>
65 #include <Network/multiplay.hxx>
67 #include "globals.hxx"
81 static void delete_ptr( FGProtocol* p ) { delete p; }
86 for_each( io_channels.begin(), io_channels.end(), delete_ptr );
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 vector<string> 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 );
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" ) {
133 // printf("Parsed opengc\n"); cin >> wait;
134 FGOpenGC *opengc = new FGOpenGC;
136 } else if ( protocol == "AV400" ) {
137 FGAV400 *av400 = new FGAV400;
139 } else if ( protocol == "garmin" ) {
140 FGGarmin *garmin = new FGGarmin;
142 } else if ( protocol == "httpd" ) {
144 string port = tokens[1];
145 return new FGHttpd( atoi(port.c_str()) );
146 #ifdef FG_JPEG_SERVER
147 } else if ( protocol == "jpg-httpd" ) {
149 string port = tokens[1];
150 return new FGJpegHttpd( atoi(port.c_str()) );
152 } else if ( protocol == "joyclient" ) {
153 FGJoyClient *joyclient = new FGJoyClient;
155 } else if ( protocol == "jsclient" ) {
156 FGJsClient *jsclient = new FGJsClient;
158 } else if ( protocol == "native" ) {
159 FGNative *native = new FGNative;
161 } else if ( protocol == "native-ctrls" ) {
162 FGNativeCtrls *native_ctrls = new FGNativeCtrls;
164 } else if ( protocol == "native-fdm" ) {
165 FGNativeFDM *native_fdm = new FGNativeFDM;
167 } else if ( protocol == "native-gui" ) {
168 FGNativeGUI *net_gui = new FGNativeGUI;
170 } else if ( protocol == "nmea" ) {
171 FGNMEA *nmea = new FGNMEA;
173 } else if ( protocol == "props" || protocol == "telnet" ) {
174 io = new FGProps( tokens );
176 } else if ( protocol == "pve" ) {
177 FGPVE *pve = new FGPVE;
179 } else if ( protocol == "ray" ) {
180 FGRAY *ray = new FGRAY;
182 } else if ( protocol == "rul" ) {
183 FGRUL *rul = new FGRUL;
185 } else if ( protocol == "generic" ) {
187 if (tokens[1] == "socket")
189 else if (tokens[1] == "file")
193 FGGeneric *generic = new FGGeneric( tokens[configToken] );
195 } else if ( protocol == "multiplay" ) {
196 if ( tokens.size() != 5 ) {
197 SG_LOG( SG_IO, SG_ALERT, "Ignoring invalid --multiplay option "
198 "(4 arguments expected: --multiplay=dir,hz,hostname,port)" );
201 string dir = tokens[1];
202 string rate = tokens[2];
203 string host = tokens[3];
204 string port = tokens[4];
205 return new FGMultiplay(dir, atoi(rate.c_str()), host, atoi(port.c_str()));
210 catch (FGProtocolConfigError& err)
212 SG_LOG( SG_IO, SG_ALERT, "Port configuration error: " << err.what() );
217 if (tokens.size() < 3) {
218 SG_LOG( SG_IO, SG_ALERT, "Incompatible number of network arguments.");
221 string medium = tokens[1];
222 SG_LOG( SG_IO, SG_INFO, " medium = " << medium );
224 string direction = tokens[2];
225 io->set_direction( direction );
226 SG_LOG( SG_IO, SG_INFO, " direction = " << direction );
228 string hertz_str = tokens[3];
229 double hertz = atof( hertz_str.c_str() );
231 SG_LOG( SG_IO, SG_INFO, " hertz = " << hertz );
233 if ( medium == "serial" ) {
234 if ( tokens.size() < 5) {
235 SG_LOG( SG_IO, SG_ALERT, "Incompatible number of arguments for serial communications.");
239 string device = tokens[4];
240 SG_LOG( SG_IO, SG_INFO, " device = " << device );
243 string baud = tokens[5];
244 SG_LOG( SG_IO, SG_INFO, " baud = " << baud );
247 SGSerial *ch = new SGSerial( device, baud );
248 io->set_io_channel( ch );
249 } else if ( medium == "file" ) {
251 if ( tokens.size() < 4) {
252 SG_LOG( SG_IO, SG_ALERT, "Incompatible number of arguments for file I/O.");
256 string file = tokens[4];
257 SG_LOG( SG_IO, SG_INFO, " file name = " << file );
259 if (tokens.size() >= 7 && tokens[6] == "repeat") {
260 if (tokens.size() >= 8) {
261 repeat = atoi(tokens[7].c_str());
262 FGGeneric* generic = dynamic_cast<FGGeneric*>(io);
264 generic->setExitOnError(true);
269 SGFile *ch = new SGFile( file, repeat );
270 io->set_io_channel( ch );
271 } else if ( medium == "socket" ) {
272 if ( tokens.size() < 6) {
273 SG_LOG( SG_IO, SG_ALERT, "Incompatible number of arguments for socket communications.");
276 string hostname = tokens[4];
277 string port = tokens[5];
278 string style = tokens[6];
280 SG_LOG( SG_IO, SG_INFO, " hostname = " << hostname );
281 SG_LOG( SG_IO, SG_INFO, " port = " << port );
282 SG_LOG( SG_IO, SG_INFO, " style = " << style );
284 io->set_io_channel( new SGSocket( hostname, port, style ) );
291 // step through the port config streams (from fgOPTIONS) and setup
292 // serial port channels for each
296 // SG_LOG( SG_IO, SG_INFO, "I/O Channel initialization, " <<
297 // globals->get_channel_options_list()->size() << " requests." );
301 // we could almost do this in a single step except pushing a valid
302 // port onto the port list copies the structure and destroys the
303 // original, which closes the port and frees up the fd ... doh!!!
305 // parse the configuration strings and store the results in the
306 // appropriate FGIOChannel structures
307 typedef vector<string> container;
308 container::iterator i = globals->get_channel_options_list()->begin();
309 container::iterator end = globals->get_channel_options_list()->end();
310 for (; i != end; ++i )
312 p = parse_port_config( *i );
315 io_channels.push_back( p );
316 if ( !p->is_enabled() ) {
317 SG_LOG( SG_IO, SG_ALERT, "I/O Channel config failed." );
321 SG_LOG( SG_IO, SG_INFO, "I/O Channel parse failed." );
327 // process any IO channel work
329 FGIO::update( double delta_time_sec )
331 // cout << "processing I/O channels" << endl;
332 // cout << " Elapsed time = " << delta_time_sec << endl;
334 typedef vector< FGProtocol* > container;
335 container::iterator i = io_channels.begin();
336 container::iterator end = io_channels.end();
337 for (; i != end; ++i ) {
340 if ( p->is_enabled() ) {
341 p->dec_count_down( delta_time_sec );
342 double dt = 1 / p->get_hz();
343 if ( p->get_count_down() < 0.33 * dt ) {
346 while ( p->get_count_down() < 0.33 * dt ) {
347 p->inc_count_down( dt );
349 // double ave = elapsed_time / p->get_count();
350 // cout << " ave rate = " << ave << endl;
358 FGIO::shutdown_all() {
361 // cout << "shutting down all I/O channels" << endl;
363 typedef vector< FGProtocol* > container;
364 container::iterator i = io_channels.begin();
365 container::iterator end = io_channels.end();
366 for (; i != end; ++i )
370 if ( p->is_enabled() ) {