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/math/sg_types.hxx>
36 #include <simgear/timing/timestamp.hxx>
38 #include <Network/protocol.hxx>
39 #include <Network/atlas.hxx>
40 #include <Network/garmin.hxx>
41 #include <Network/httpd.hxx>
42 #include <Network/joyclient.hxx>
43 #include <Network/native.hxx>
44 #include <Network/native_ctrls.hxx>
45 #include <Network/native_fdm.hxx>
46 #include <Network/nmea.hxx>
47 #include <Network/props.hxx>
48 #include <Network/pve.hxx>
49 #include <Network/ray.hxx>
50 #include <Network/rul.hxx>
52 #include "globals.hxx"
57 // define the global I/O channel list
58 io_container global_io_list;
61 // configure a port based on the config string
62 static FGProtocol *parse_port_config( const string& config )
64 bool short_circuit = false;
66 string::size_type begin, end;
70 SG_LOG( SG_IO, SG_INFO, "Parse I/O channel request: " << config );
73 end = config.find(",", begin);
74 if ( end == string::npos ) {
78 string protocol = config.substr(begin, end - begin);
80 SG_LOG( SG_IO, SG_INFO, " protocol = " << protocol );
83 if ( protocol == "atlas" ) {
84 FGAtlas *atlas = new FGAtlas;
86 } else if ( protocol == "garmin" ) {
87 FGGarmin *garmin = new FGGarmin;
89 } else if ( protocol == "httpd" ) {
91 string port = config.substr(begin);
92 FGHttpd *httpd = new FGHttpd( atoi(port.c_str()) );
95 } else if ( protocol == "joyclient" ) {
96 FGJoyClient *joyclient = new FGJoyClient;
98 } else if ( protocol == "native" ) {
99 FGNative *native = new FGNative;
101 } else if ( protocol == "native_ctrls" ) {
102 FGNativeCtrls *native_ctrls = new FGNativeCtrls;
104 } else if ( protocol == "native_fdm" ) {
105 FGNativeFDM *native_fdm = new FGNativeFDM;
107 } else if ( protocol == "nmea" ) {
108 FGNMEA *nmea = new FGNMEA;
110 } else if ( protocol == "props" ) {
111 FGProps *props = new FGProps;
113 } else if ( protocol == "pve" ) {
114 FGPVE *pve = new FGPVE;
116 } else if ( protocol == "ray" ) {
117 FGRAY *ray = new FGRAY;
119 } else if ( protocol == "rul" ) {
120 FGRUL *rul = new FGRUL;
126 if ( ! short_circuit ) {
128 end = config.find(",", begin);
129 if ( end == string::npos ) {
130 return NULL; // dummy
133 string medium = config.substr(begin, end - begin);
135 SG_LOG( SG_IO, SG_INFO, " medium = " << medium );
137 // determine direction
138 end = config.find(",", begin);
139 if ( end == string::npos ) {
140 return NULL; // dummy
143 string direction = config.substr(begin, end - begin);
145 io->set_direction( direction );
146 SG_LOG( SG_IO, SG_INFO, " direction = " << direction );
149 end = config.find(",", begin);
150 if ( end == string::npos ) {
151 return NULL; // dummy
154 string hertz_str = config.substr(begin, end - begin);
156 double hertz = atof( hertz_str.c_str() );
158 SG_LOG( SG_IO, SG_INFO, " hertz = " << hertz );
160 if ( medium == "serial" ) {
162 end = config.find(",", begin);
163 if ( end == string::npos ) {
167 string device = config.substr(begin, end - begin);
169 SG_LOG( SG_IO, SG_INFO, " device = " << device );
172 string baud = config.substr(begin);
173 SG_LOG( SG_IO, SG_INFO, " baud = " << baud );
175 SGSerial *ch = new SGSerial( device, baud );
176 io->set_io_channel( ch );
177 } else if ( medium == "file" ) {
179 string file = config.substr(begin);
180 SG_LOG( SG_IO, SG_INFO, " file name = " << file );
182 SGFile *ch = new SGFile( file );
183 io->set_io_channel( ch );
184 } else if ( medium == "socket" ) {
186 end = config.find(",", begin);
187 if ( end == string::npos ) {
191 string hostname = config.substr(begin, end - begin);
193 SG_LOG( SG_IO, SG_INFO, " hostname = " << hostname );
196 end = config.find(",", begin);
197 if ( end == string::npos ) {
201 string port = config.substr(begin, end - begin);
203 SG_LOG( SG_IO, SG_INFO, " port string = " << port );
206 string style_str = config.substr(begin);
207 SG_LOG( SG_IO, SG_INFO, " style string = " << style_str );
209 SGSocket *ch = new SGSocket( hostname, port, style_str );
210 io->set_io_channel( ch );
218 // step through the port config streams (from fgOPTIONS) and setup
219 // serial port channels for each
221 // SG_LOG( SG_IO, SG_INFO, "I/O Channel initialization, " <<
222 // globals->get_channel_options_list()->size() << " requests." );
225 string_list *channel_options_list = globals->get_channel_options_list();
227 // we could almost do this in a single step except pushing a valid
228 // port onto the port list copies the structure and destroys the
229 // original, which closes the port and frees up the fd ... doh!!!
231 // parse the configuration strings and store the results in the
232 // appropriate FGIOChannel structures
233 for ( int i = 0; i < (int)channel_options_list->size(); ++i ) {
234 p = parse_port_config( (*channel_options_list)[i] );
237 global_io_list.push_back( p );
238 if ( !p->is_enabled() ) {
239 SG_LOG( SG_IO, SG_ALERT, "I/O Channel config failed." );
243 SG_LOG( SG_IO, SG_INFO, "I/O Channel parse failed." );
249 // process any serial port work
253 // cout << "processing I/O channels" << endl;
255 static int inited = 0;
257 static SGTimeStamp last;
266 interval = current - last;
270 for ( int i = 0; i < (int)global_io_list.size(); ++i ) {
271 // cout << " channel = " << i << endl;
272 p = global_io_list[i];
274 if ( p->is_enabled() ) {
275 p->dec_count_down( interval );
276 while ( p->get_count_down() < 0 ) {
278 p->dec_count_down( -1000000.0 / p->get_hz() );
285 // shutdown all I/O connections
286 void fgIOShutdownAll() {
289 // cout << "processing I/O channels" << endl;
291 for ( int i = 0; i < (int)global_io_list.size(); ++i ) {
292 // cout << " channel = " << i << endl;
293 p = global_io_list[i];
295 if ( p->is_enabled() ) {