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 - curt@flightgear.org
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" );
117 if ( tokens[1] == "no-pedals" ) {
118 fgSetBool( "/input/atcsim/ignore-pedal-controls", true );
120 fgSetBool( "/input/atcsim/ignore-pedal-controls", false );
122 atcsim->set_path_names(tokens[2], tokens[3], tokens[4], tokens[5]);
124 } else if ( protocol == "atlas" ) {
125 FGAtlas *atlas = new FGAtlas;
127 } else if ( protocol == "opengc" ) {
129 // printf("Parsed opengc\n"); cin >> wait;
130 FGOpenGC *opengc = new FGOpenGC;
132 } else if ( protocol == "garmin" ) {
133 FGGarmin *garmin = new FGGarmin;
135 } else if ( protocol == "httpd" ) {
137 string port = tokens[1];
138 return new FGHttpd( atoi(port.c_str()) );
139 #ifdef FG_JPEG_SERVER
140 } else if ( protocol == "jpg-httpd" ) {
142 string port = tokens[1];
143 return new FGJpegHttpd( atoi(port.c_str()) );
145 } else if ( protocol == "joyclient" ) {
146 FGJoyClient *joyclient = new FGJoyClient;
148 } else if ( protocol == "jsclient" ) {
149 FGJsClient *jsclient = new FGJsClient;
151 } else if ( protocol == "native" ) {
152 FGNative *native = new FGNative;
154 } else if ( protocol == "native-ctrls" ) {
155 FGNativeCtrls *native_ctrls = new FGNativeCtrls;
157 } else if ( protocol == "native-fdm" ) {
158 FGNativeFDM *native_fdm = new FGNativeFDM;
160 } else if ( protocol == "native-gui" ) {
161 FGNativeGUI *net_gui = new FGNativeGUI;
163 } else if ( protocol == "nmea" ) {
164 FGNMEA *nmea = new FGNMEA;
166 } else if ( protocol == "props" || protocol == "telnet" ) {
167 io = new FGProps( tokens );
169 } else if ( protocol == "pve" ) {
170 FGPVE *pve = new FGPVE;
172 } else if ( protocol == "ray" ) {
173 FGRAY *ray = new FGRAY;
175 } else if ( protocol == "rul" ) {
176 FGRUL *rul = new FGRUL;
178 } else if ( protocol == "generic" ) {
180 if (tokens[1] == "socket") n++;
181 else if (tokens[1] == "file") n--;
182 FGGeneric *generic = new FGGeneric( tokens[n] );
186 } else if ( protocol == "multiplay" ) {\
187 //Determine dir, rate, host & port
188 string dir = tokens[1];
189 string rate = tokens[2];
190 string host = tokens[3];
191 string port = tokens[4];
192 return new FGMultiplay(dir, atoi(rate.c_str()), host, atoi(port.c_str()));
199 catch (FGProtocolConfigError& err)
201 SG_LOG( SG_IO, SG_ALERT, "Port configuration error: " << err.what() );
206 string medium = tokens[1];
207 SG_LOG( SG_IO, SG_INFO, " medium = " << medium );
209 string direction = tokens[2];
210 io->set_direction( direction );
211 SG_LOG( SG_IO, SG_INFO, " direction = " << direction );
213 string hertz_str = tokens[3];
214 double hertz = atof( hertz_str.c_str() );
216 SG_LOG( SG_IO, SG_INFO, " hertz = " << hertz );
218 if ( medium == "serial" ) {
220 string device = tokens[4];
221 SG_LOG( SG_IO, SG_INFO, " device = " << device );
224 string baud = tokens[5];
225 SG_LOG( SG_IO, SG_INFO, " baud = " << baud );
227 SGSerial *ch = new SGSerial( device, baud );
228 io->set_io_channel( ch );
229 } else if ( medium == "file" ) {
231 string file = tokens[4];
232 SG_LOG( SG_IO, SG_INFO, " file name = " << file );
234 SGFile *ch = new SGFile( file );
235 io->set_io_channel( ch );
236 } else if ( medium == "socket" ) {
237 string hostname = tokens[4];
238 string port = tokens[5];
239 string style = tokens[6];
241 SG_LOG( SG_IO, SG_INFO, " hostname = " << hostname );
242 SG_LOG( SG_IO, SG_INFO, " port = " << port );
243 SG_LOG( SG_IO, SG_INFO, " style = " << style );
245 io->set_io_channel( new SGSocket( hostname, port, style ) );
252 // step through the port config streams (from fgOPTIONS) and setup
253 // serial port channels for each
257 // SG_LOG( SG_IO, SG_INFO, "I/O Channel initialization, " <<
258 // globals->get_channel_options_list()->size() << " requests." );
262 // we could almost do this in a single step except pushing a valid
263 // port onto the port list copies the structure and destroys the
264 // original, which closes the port and frees up the fd ... doh!!!
266 // parse the configuration strings and store the results in the
267 // appropriate FGIOChannel structures
268 typedef vector<string> container;
269 container::iterator i = globals->get_channel_options_list()->begin();
270 container::iterator end = globals->get_channel_options_list()->end();
271 for (; i != end; ++i )
273 p = parse_port_config( *i );
276 io_channels.push_back( p );
277 if ( !p->is_enabled() ) {
278 SG_LOG( SG_IO, SG_ALERT, "I/O Channel config failed." );
282 SG_LOG( SG_IO, SG_INFO, "I/O Channel parse failed." );
288 // process any IO channel work
290 FGIO::update( double delta_time_sec )
292 // cout << "processing I/O channels" << endl;
293 // cout << " Elapsed time = " << delta_time_sec << endl;
295 typedef vector< FGProtocol* > container;
296 container::iterator i = io_channels.begin();
297 container::iterator end = io_channels.end();
298 for (; i != end; ++i ) {
301 if ( p->is_enabled() ) {
302 p->dec_count_down( delta_time_sec );
303 double dt = 1 / p->get_hz();
304 if ( p->get_count_down() < 0.33 * dt ) {
307 while ( p->get_count_down() < 0.33 * dt ) {
308 p->inc_count_down( dt );
310 // double ave = elapsed_time / p->get_count();
311 // cout << " ave rate = " << ave << endl;
319 FGIO::shutdown_all() {
322 // cout << "processing I/O channels" << endl;
324 typedef vector< FGProtocol* > container;
325 container::iterator i = io_channels.begin();
326 container::iterator end = io_channels.end();
327 for (; i != end; ++i )
331 if ( p->is_enabled() ) {