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., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include <simgear/compiler.h>
26 #include <stdlib.h> // atoi()
30 #include <simgear/debug/logstream.hxx>
31 #include <simgear/io/iochannel.hxx>
32 #include <simgear/io/sg_file.hxx>
33 #include <simgear/io/sg_serial.hxx>
34 #include <simgear/io/sg_socket.hxx>
35 #include <simgear/io/sg_socket_udp.hxx>
36 #include <simgear/math/sg_types.hxx>
37 #include <simgear/timing/timestamp.hxx>
38 #include <simgear/misc/strutils.hxx>
40 #include <Network/protocol.hxx>
41 #include <Network/atc610x.hxx>
42 #include <Network/atlas.hxx>
43 #include <Network/garmin.hxx>
44 #include <Network/httpd.hxx>
46 # include <Network/jpg-httpd.hxx>
48 #include <Network/joyclient.hxx>
49 #include <Network/jsclient.hxx>
50 #include <Network/native.hxx>
51 #include <Network/native_ctrls.hxx>
52 #include <Network/native_fdm.hxx>
53 #include <Network/native_gui.hxx>
54 #include <Network/opengc.hxx>
55 #include <Network/nmea.hxx>
56 #include <Network/props.hxx>
57 #include <Network/pve.hxx>
58 #include <Network/ray.hxx>
59 #include <Network/rul.hxx>
60 #include <Network/generic.hxx>
63 #include <Network/multiplay.hxx>
66 #include "globals.hxx"
76 #include STL_ALGORITHM
77 SG_USING_STD(for_each);
79 static void delete_ptr( FGProtocol* p ) { delete p; }
84 for_each( io_channels.begin(), io_channels.end(), delete_ptr );
88 // configure a port based on the config string
90 FGIO::parse_port_config( const string& config )
92 SG_LOG( SG_IO, SG_INFO, "Parse I/O channel request: " << config );
94 vector<string> tokens = simgear::strutils::split( config, "," );
97 SG_LOG( SG_IO, SG_ALERT,
98 "Port configuration error: empty config string" );
102 string protocol = tokens[0];
103 SG_LOG( SG_IO, SG_INFO, " protocol = " << protocol );
109 if ( protocol == "atcsim" ) {
110 FGATC610x *atcsim = new FGATC610x;
111 atcsim->set_hz( 30 );
112 if ( tokens.size() != 6 ) {
113 SG_LOG( SG_IO, SG_ALERT, "Usage: --atcsim=[no-]pedals,"
114 << "input0_config,input1_config,"
115 << "output0_config,output1_config,file.nas" );
118 if ( tokens[1] == "no-pedals" ) {
119 fgSetBool( "/input/atcsim/ignore-pedal-controls", true );
121 fgSetBool( "/input/atcsim/ignore-pedal-controls", false );
123 atcsim->set_path_names(tokens[2], tokens[3], tokens[4], tokens[5]);
125 } else if ( protocol == "atlas" ) {
126 FGAtlas *atlas = new FGAtlas;
128 } else if ( protocol == "opengc" ) {
130 // printf("Parsed opengc\n"); cin >> wait;
131 FGOpenGC *opengc = new FGOpenGC;
133 } else if ( protocol == "garmin" ) {
134 FGGarmin *garmin = new FGGarmin;
136 } else if ( protocol == "httpd" ) {
138 string port = tokens[1];
139 return new FGHttpd( atoi(port.c_str()) );
140 #ifdef FG_JPEG_SERVER
141 } else if ( protocol == "jpg-httpd" ) {
143 string port = tokens[1];
144 return new FGJpegHttpd( atoi(port.c_str()) );
146 } else if ( protocol == "joyclient" ) {
147 FGJoyClient *joyclient = new FGJoyClient;
149 } else if ( protocol == "jsclient" ) {
150 FGJsClient *jsclient = new FGJsClient;
152 } else if ( protocol == "native" ) {
153 FGNative *native = new FGNative;
155 } else if ( protocol == "native-ctrls" ) {
156 FGNativeCtrls *native_ctrls = new FGNativeCtrls;
158 } else if ( protocol == "native-fdm" ) {
159 FGNativeFDM *native_fdm = new FGNativeFDM;
161 } else if ( protocol == "native-gui" ) {
162 FGNativeGUI *net_gui = new FGNativeGUI;
164 } else if ( protocol == "nmea" ) {
165 FGNMEA *nmea = new FGNMEA;
167 } else if ( protocol == "props" || protocol == "telnet" ) {
168 io = new FGProps( tokens );
170 } else if ( protocol == "pve" ) {
171 FGPVE *pve = new FGPVE;
173 } else if ( protocol == "ray" ) {
174 FGRAY *ray = new FGRAY;
176 } else if ( protocol == "rul" ) {
177 FGRUL *rul = new FGRUL;
179 } else if ( protocol == "generic" ) {
181 if (tokens[1] == "socket") n++;
182 else if (tokens[1] == "file") n--;
183 FGGeneric *generic = new FGGeneric( tokens[n] );
187 } else if ( protocol == "multiplay" ) {\
188 //Determine dir, rate, host & port
189 string dir = tokens[1];
190 string rate = tokens[2];
191 string host = tokens[3];
192 string port = tokens[4];
193 return new FGMultiplay(dir, atoi(rate.c_str()), host, atoi(port.c_str()));
200 catch (FGProtocolConfigError& err)
202 SG_LOG( SG_IO, SG_ALERT, "Port configuration error: " << err.what() );
207 string medium = tokens[1];
208 SG_LOG( SG_IO, SG_INFO, " medium = " << medium );
210 string direction = tokens[2];
211 io->set_direction( direction );
212 SG_LOG( SG_IO, SG_INFO, " direction = " << direction );
214 string hertz_str = tokens[3];
215 double hertz = atof( hertz_str.c_str() );
217 SG_LOG( SG_IO, SG_INFO, " hertz = " << hertz );
219 if ( medium == "serial" ) {
221 string device = tokens[4];
222 SG_LOG( SG_IO, SG_INFO, " device = " << device );
225 string baud = tokens[5];
226 SG_LOG( SG_IO, SG_INFO, " baud = " << baud );
228 SGSerial *ch = new SGSerial( device, baud );
229 io->set_io_channel( ch );
230 } else if ( medium == "file" ) {
232 string file = tokens[4];
233 SG_LOG( SG_IO, SG_INFO, " file name = " << file );
235 SGFile *ch = new SGFile( file );
236 io->set_io_channel( ch );
237 } else if ( medium == "socket" ) {
238 string hostname = tokens[4];
239 string port = tokens[5];
240 string style = tokens[6];
242 SG_LOG( SG_IO, SG_INFO, " hostname = " << hostname );
243 SG_LOG( SG_IO, SG_INFO, " port = " << port );
244 SG_LOG( SG_IO, SG_INFO, " style = " << style );
246 io->set_io_channel( new SGSocket( hostname, port, style ) );
253 // step through the port config streams (from fgOPTIONS) and setup
254 // serial port channels for each
258 // SG_LOG( SG_IO, SG_INFO, "I/O Channel initialization, " <<
259 // globals->get_channel_options_list()->size() << " requests." );
263 // we could almost do this in a single step except pushing a valid
264 // port onto the port list copies the structure and destroys the
265 // original, which closes the port and frees up the fd ... doh!!!
267 // parse the configuration strings and store the results in the
268 // appropriate FGIOChannel structures
269 typedef vector<string> container;
270 container::iterator i = globals->get_channel_options_list()->begin();
271 container::iterator end = globals->get_channel_options_list()->end();
272 for (; i != end; ++i )
274 p = parse_port_config( *i );
277 io_channels.push_back( p );
278 if ( !p->is_enabled() ) {
279 SG_LOG( SG_IO, SG_ALERT, "I/O Channel config failed." );
283 SG_LOG( SG_IO, SG_INFO, "I/O Channel parse failed." );
289 // process any IO channel work
291 FGIO::update( double delta_time_sec )
293 // cout << "processing I/O channels" << endl;
294 // cout << " Elapsed time = " << delta_time_sec << endl;
296 typedef vector< FGProtocol* > container;
297 container::iterator i = io_channels.begin();
298 container::iterator end = io_channels.end();
299 for (; i != end; ++i ) {
302 if ( p->is_enabled() ) {
303 p->dec_count_down( delta_time_sec );
304 double dt = 1 / p->get_hz();
305 if ( p->get_count_down() < 0.33 * dt ) {
308 while ( p->get_count_down() < 0.33 * dt ) {
309 p->inc_count_down( dt );
311 // double ave = elapsed_time / p->get_count();
312 // cout << " ave rate = " << ave << endl;
320 FGIO::shutdown_all() {
323 // cout << "processing I/O channels" << endl;
325 typedef vector< FGProtocol* > container;
326 container::iterator i = io_channels.begin();
327 container::iterator end = io_channels.end();
328 for (; i != end; ++i )
332 if ( p->is_enabled() ) {