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/native.hxx>
50 #include <Network/native_ctrls.hxx>
51 #include <Network/native_fdm.hxx>
52 #include <Network/opengc.hxx>
53 #include <Network/nmea.hxx>
54 #include <Network/props.hxx>
55 #include <Network/pve.hxx>
56 #include <Network/ray.hxx>
57 #include <Network/rul.hxx>
59 #include "globals.hxx"
69 #include STL_ALGORITHM
70 SG_USING_STD(for_each);
72 static void delete_ptr( FGProtocol* p ) { delete p; }
77 for_each( io_channels.begin(), io_channels.end(), delete_ptr );
81 // configure a port based on the config string
83 FGIO::parse_port_config( const string& config )
85 SG_LOG( SG_IO, SG_INFO, "Parse I/O channel request: " << config );
87 vector<string> tokens = simgear::strutils::split( config, "," );
90 SG_LOG( SG_IO, SG_ALERT,
91 "Port configuration error: empty config string" );
95 string protocol = tokens[0];
96 SG_LOG( SG_IO, SG_INFO, " protocol = " << protocol );
102 if ( protocol == "atc610x" ) {
103 FGATC610x *atc610x = new FGATC610x;
104 atc610x->set_hz( 30 );
106 } else if ( protocol == "atlas" ) {
107 FGAtlas *atlas = new FGAtlas;
109 } else if ( protocol == "opengc" ) {
111 // printf("Parsed opengc\n"); cin >> wait;
112 FGOpenGC *opengc = new FGOpenGC;
114 } else if ( protocol == "garmin" ) {
115 FGGarmin *garmin = new FGGarmin;
117 } else if ( protocol == "httpd" ) {
119 string port = tokens[1];
120 return new FGHttpd( atoi(port.c_str()) );
121 #ifdef FG_JPEG_SERVER
122 } else if ( protocol == "jpg-httpd" ) {
124 string port = tokens[1];
125 return new FGJpegHttpd( atoi(port.c_str()) );
127 } else if ( protocol == "joyclient" ) {
128 FGJoyClient *joyclient = new FGJoyClient;
130 } else if ( protocol == "native" ) {
131 FGNative *native = new FGNative;
133 } else if ( protocol == "native_ctrls" ) {
134 FGNativeCtrls *native_ctrls = new FGNativeCtrls;
136 } else if ( protocol == "native_fdm" ) {
137 FGNativeFDM *native_fdm = new FGNativeFDM;
139 } else if ( protocol == "nmea" ) {
140 FGNMEA *nmea = new FGNMEA;
142 } else if ( protocol == "props" || protocol == "telnet" ) {
143 io = new FGProps( tokens );
145 } else if ( protocol == "pve" ) {
146 FGPVE *pve = new FGPVE;
148 } else if ( protocol == "ray" ) {
149 FGRAY *ray = new FGRAY;
151 } else if ( protocol == "rul" ) {
152 FGRUL *rul = new FGRUL;
158 catch (FGProtocolConfigError& err)
160 SG_LOG( SG_IO, SG_ALERT, "Port configuration error: " << err.what() );
165 string medium = tokens[1];
166 SG_LOG( SG_IO, SG_INFO, " medium = " << medium );
168 string direction = tokens[2];
169 io->set_direction( direction );
170 SG_LOG( SG_IO, SG_INFO, " direction = " << direction );
172 string hertz_str = tokens[3];
173 double hertz = atof( hertz_str.c_str() );
175 SG_LOG( SG_IO, SG_INFO, " hertz = " << hertz );
177 if ( medium == "serial" ) {
179 string device = tokens[4];
180 SG_LOG( SG_IO, SG_INFO, " device = " << device );
183 string baud = tokens[5];
184 SG_LOG( SG_IO, SG_INFO, " baud = " << baud );
186 SGSerial *ch = new SGSerial( device, baud );
187 io->set_io_channel( ch );
188 } else if ( medium == "file" ) {
190 string file = tokens[4];
191 SG_LOG( SG_IO, SG_INFO, " file name = " << file );
193 SGFile *ch = new SGFile( file );
194 io->set_io_channel( ch );
195 } else if ( medium == "socket" ) {
196 string hostname = tokens[4];
197 string port = tokens[5];
198 string style = tokens[6];
200 SG_LOG( SG_IO, SG_INFO, " hostname = " << hostname );
201 SG_LOG( SG_IO, SG_INFO, " port = " << port );
202 SG_LOG( SG_IO, SG_INFO, " style = " << style );
204 io->set_io_channel( new SGSocket( hostname, port, style ) );
211 // step through the port config streams (from fgOPTIONS) and setup
212 // serial port channels for each
216 // SG_LOG( SG_IO, SG_INFO, "I/O Channel initialization, " <<
217 // globals->get_channel_options_list()->size() << " requests." );
221 // we could almost do this in a single step except pushing a valid
222 // port onto the port list copies the structure and destroys the
223 // original, which closes the port and frees up the fd ... doh!!!
225 // parse the configuration strings and store the results in the
226 // appropriate FGIOChannel structures
227 typedef vector<string> container;
228 container::iterator i = globals->get_channel_options_list()->begin();
229 container::iterator end = globals->get_channel_options_list()->end();
230 for (; i != end; ++i )
232 p = parse_port_config( *i );
235 io_channels.push_back( p );
236 if ( !p->is_enabled() ) {
237 SG_LOG( SG_IO, SG_ALERT, "I/O Channel config failed." );
241 SG_LOG( SG_IO, SG_INFO, "I/O Channel parse failed." );
247 // process any serial port work
249 FGIO::update( double delta_time_sec )
251 // cout << "processing I/O channels" << endl;
253 // SGTimeStamp current_time;
254 // current_time.stamp();
255 // static SGTimeStamp start_time = current_time;
256 // double elapsed_time = (current_time - start_time) / 1000000.0;
257 // cout << " Elapsed time = " << elapsed_time << endl;
259 typedef vector< FGProtocol* > container;
260 container::iterator i = io_channels.begin();
261 container::iterator end = io_channels.end();
262 for (; i != end; ++i ) {
265 if ( p->is_enabled() ) {
266 p->dec_count_down( delta_time_sec );
267 double dt = 1 / p->get_hz();
268 while ( p->get_count_down() < 0.33 * dt ) {
270 p->inc_count_down( dt );
273 // double ave = elapsed_time / p->get_count();
274 // cout << " ave rate = " << ave << endl;
281 FGIO::shutdown_all() {
284 // cout << "processing I/O channels" << endl;
286 typedef vector< FGProtocol* > container;
287 container::iterator i = io_channels.begin();
288 container::iterator end = io_channels.end();
289 for (; i != end; ++i )
293 if ( p->is_enabled() ) {