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 <stdlib.h> // 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"
77 #include STL_ALGORITHM
78 SG_USING_STD(for_each);
80 static void delete_ptr( FGProtocol* p ) { delete p; }
85 for_each( io_channels.begin(), io_channels.end(), delete_ptr );
89 // configure a port based on the config string
91 FGIO::parse_port_config( const string& config )
93 SG_LOG( SG_IO, SG_INFO, "Parse I/O channel request: " << config );
95 vector<string> tokens = simgear::strutils::split( config, "," );
98 SG_LOG( SG_IO, SG_ALERT,
99 "Port configuration error: empty config string" );
103 string protocol = tokens[0];
104 SG_LOG( SG_IO, SG_INFO, " protocol = " << protocol );
110 if ( protocol == "atcsim" ) {
111 FGATCMain *atcsim = new FGATCMain;
112 atcsim->set_hz( 30 );
113 if ( tokens.size() != 6 ) {
114 SG_LOG( SG_IO, SG_ALERT, "Usage: --atcsim=[no-]pedals,"
115 << "input0_config,input1_config,"
116 << "output0_config,output1_config,file.nas" );
120 if ( tokens[1] == "no-pedals" ) {
121 fgSetBool( "/input/atcsim/ignore-pedal-controls", true );
123 fgSetBool( "/input/atcsim/ignore-pedal-controls", false );
125 atcsim->set_path_names(tokens[2], tokens[3], tokens[4], tokens[5]);
127 } else if ( protocol == "atlas" ) {
128 FGAtlas *atlas = new FGAtlas;
130 } else if ( protocol == "opengc" ) {
132 // printf("Parsed opengc\n"); cin >> wait;
133 FGOpenGC *opengc = new FGOpenGC;
135 } else if ( protocol == "AV400" ) {
136 FGAV400 *av400 = new FGAV400;
138 } else if ( protocol == "garmin" ) {
139 FGGarmin *garmin = new FGGarmin;
141 } else if ( protocol == "httpd" ) {
143 string port = tokens[1];
144 return new FGHttpd( atoi(port.c_str()) );
145 #ifdef FG_JPEG_SERVER
146 } else if ( protocol == "jpg-httpd" ) {
148 string port = tokens[1];
149 return new FGJpegHttpd( atoi(port.c_str()) );
151 } else if ( protocol == "joyclient" ) {
152 FGJoyClient *joyclient = new FGJoyClient;
154 } else if ( protocol == "jsclient" ) {
155 FGJsClient *jsclient = new FGJsClient;
157 } else if ( protocol == "native" ) {
158 FGNative *native = new FGNative;
160 } else if ( protocol == "native-ctrls" ) {
161 FGNativeCtrls *native_ctrls = new FGNativeCtrls;
163 } else if ( protocol == "native-fdm" ) {
164 FGNativeFDM *native_fdm = new FGNativeFDM;
166 } else if ( protocol == "native-gui" ) {
167 FGNativeGUI *net_gui = new FGNativeGUI;
169 } else if ( protocol == "nmea" ) {
170 FGNMEA *nmea = new FGNMEA;
172 } else if ( protocol == "props" || protocol == "telnet" ) {
173 io = new FGProps( tokens );
175 } else if ( protocol == "pve" ) {
176 FGPVE *pve = new FGPVE;
178 } else if ( protocol == "ray" ) {
179 FGRAY *ray = new FGRAY;
181 } else if ( protocol == "rul" ) {
182 FGRUL *rul = new FGRUL;
184 } else if ( protocol == "generic" ) {
186 if (tokens[1] == "socket") n++;
187 else if (tokens[1] == "file") n--;
188 FGGeneric *generic = new FGGeneric( tokens[n] );
190 } else if ( protocol == "multiplay" ) {\
191 //Determine dir, rate, host & port
192 string dir = tokens[1];
193 string rate = tokens[2];
194 string host = tokens[3];
195 string port = tokens[4];
196 return new FGMultiplay(dir, atoi(rate.c_str()), host, atoi(port.c_str()));
201 catch (FGProtocolConfigError& err)
203 SG_LOG( SG_IO, SG_ALERT, "Port configuration error: " << err.what() );
208 if (tokens.size() < 3) {
209 SG_LOG( SG_IO, SG_ALERT, "Incompatible number of network arguments.");
212 string medium = tokens[1];
213 SG_LOG( SG_IO, SG_INFO, " medium = " << medium );
215 string direction = tokens[2];
216 io->set_direction( direction );
217 SG_LOG( SG_IO, SG_INFO, " direction = " << direction );
219 string hertz_str = tokens[3];
220 double hertz = atof( hertz_str.c_str() );
222 SG_LOG( SG_IO, SG_INFO, " hertz = " << hertz );
224 if ( medium == "serial" ) {
225 if ( tokens.size() < 5) {
226 SG_LOG( SG_IO, SG_ALERT, "Incompatible number of arguments for serial communications.");
230 string device = tokens[4];
231 SG_LOG( SG_IO, SG_INFO, " device = " << device );
234 string baud = tokens[5];
235 SG_LOG( SG_IO, SG_INFO, " baud = " << baud );
237 SGSerial *ch = new SGSerial( device, baud );
238 io->set_io_channel( ch );
239 } else if ( medium == "file" ) {
241 if ( tokens.size() < 4) {
242 SG_LOG( SG_IO, SG_ALERT, "Incompatible number of arguments for file I/O.");
246 string file = tokens[4];
247 SG_LOG( SG_IO, SG_INFO, " file name = " << file );
249 SGFile *ch = new SGFile( file );
250 io->set_io_channel( ch );
251 } else if ( medium == "socket" ) {
252 if ( tokens.size() < 6) {
253 SG_LOG( SG_IO, SG_ALERT, "Incompatible number of arguments for socket communications.");
256 string hostname = tokens[4];
257 string port = tokens[5];
258 string style = tokens[6];
260 SG_LOG( SG_IO, SG_INFO, " hostname = " << hostname );
261 SG_LOG( SG_IO, SG_INFO, " port = " << port );
262 SG_LOG( SG_IO, SG_INFO, " style = " << style );
264 io->set_io_channel( new SGSocket( hostname, port, style ) );
271 // step through the port config streams (from fgOPTIONS) and setup
272 // serial port channels for each
276 // SG_LOG( SG_IO, SG_INFO, "I/O Channel initialization, " <<
277 // globals->get_channel_options_list()->size() << " requests." );
281 // we could almost do this in a single step except pushing a valid
282 // port onto the port list copies the structure and destroys the
283 // original, which closes the port and frees up the fd ... doh!!!
285 // parse the configuration strings and store the results in the
286 // appropriate FGIOChannel structures
287 typedef vector<string> container;
288 container::iterator i = globals->get_channel_options_list()->begin();
289 container::iterator end = globals->get_channel_options_list()->end();
290 for (; i != end; ++i )
292 p = parse_port_config( *i );
295 io_channels.push_back( p );
296 if ( !p->is_enabled() ) {
297 SG_LOG( SG_IO, SG_ALERT, "I/O Channel config failed." );
301 SG_LOG( SG_IO, SG_INFO, "I/O Channel parse failed." );
307 // process any IO channel work
309 FGIO::update( double delta_time_sec )
311 // cout << "processing I/O channels" << endl;
312 // cout << " Elapsed time = " << delta_time_sec << endl;
314 typedef vector< FGProtocol* > container;
315 container::iterator i = io_channels.begin();
316 container::iterator end = io_channels.end();
317 for (; i != end; ++i ) {
320 if ( p->is_enabled() ) {
321 p->dec_count_down( delta_time_sec );
322 double dt = 1 / p->get_hz();
323 if ( p->get_count_down() < 0.33 * dt ) {
326 while ( p->get_count_down() < 0.33 * dt ) {
327 p->inc_count_down( dt );
329 // double ave = elapsed_time / p->get_count();
330 // cout << " ave rate = " << ave << endl;
338 FGIO::shutdown_all() {
341 // cout << "shutting down all I/O channels" << endl;
343 typedef vector< FGProtocol* > container;
344 container::iterator i = io_channels.begin();
345 container::iterator end = io_channels.end();
346 for (; i != end; ++i )
350 if ( p->is_enabled() ) {