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>
39 #include <Network/protocol.hxx>
40 #include <Network/atc610x.hxx>
41 #include <Network/atlas.hxx>
42 #include <Network/garmin.hxx>
43 #include <Network/httpd.hxx>
45 # include <Network/jpg-httpd.hxx>
47 #include <Network/joyclient.hxx>
48 #include <Network/native.hxx>
49 #include <Network/native_ctrls.hxx>
50 #include <Network/native_fdm.hxx>
51 #include <Network/opengc.hxx>
52 #include <Network/nmea.hxx>
53 #include <Network/props.hxx>
54 #include <Network/pve.hxx>
55 #include <Network/ray.hxx>
56 #include <Network/rul.hxx>
58 #include "globals.hxx"
63 // define the global I/O channel list
64 io_container global_io_list;
67 // configure a port based on the config string
68 static FGProtocol *parse_port_config( const string& config )
70 bool short_circuit = false;
72 string::size_type begin, end;
76 SG_LOG( SG_IO, SG_INFO, "Parse I/O channel request: " << config );
79 end = config.find(",", begin);
80 if ( end == string::npos ) {
84 string protocol = config.substr(begin, end - begin);
86 SG_LOG( SG_IO, SG_INFO, " protocol = " << protocol );
89 if ( protocol == "atc610x" ) {
90 FGATC610x *atc610x = new FGATC610x;
93 cout << "here ..." << endl;
94 } else if ( protocol == "atlas" ) {
95 FGAtlas *atlas = new FGAtlas;
97 } else if ( protocol == "opengc" ) {
99 // printf("Parsed opengc\n"); cin >> wait;
100 FGOpenGC *opengc = new FGOpenGC;
102 } else if ( protocol == "garmin" ) {
103 FGGarmin *garmin = new FGGarmin;
105 } else if ( protocol == "httpd" ) {
107 string port = config.substr(begin);
108 FGHttpd *httpd = new FGHttpd( atoi(port.c_str()) );
110 short_circuit = true;
111 #ifdef FG_JPEG_SERVER
112 } else if ( protocol == "jpg-httpd" ) {
114 string port = config.substr(begin);
115 FGJpegHttpd *jpeg_httpd = new FGJpegHttpd( atoi(port.c_str()) );
117 short_circuit = true;
119 } else if ( protocol == "joyclient" ) {
120 FGJoyClient *joyclient = new FGJoyClient;
122 } else if ( protocol == "native" ) {
123 FGNative *native = new FGNative;
125 } else if ( protocol == "native_ctrls" ) {
126 FGNativeCtrls *native_ctrls = new FGNativeCtrls;
128 } else if ( protocol == "native_fdm" ) {
129 FGNativeFDM *native_fdm = new FGNativeFDM;
131 } else if ( protocol == "nmea" ) {
132 FGNMEA *nmea = new FGNMEA;
134 } else if ( protocol == "props" ) {
135 FGProps *props = new FGProps;
137 } else if ( protocol == "pve" ) {
138 FGPVE *pve = new FGPVE;
140 } else if ( protocol == "ray" ) {
141 FGRAY *ray = new FGRAY;
143 } else if ( protocol == "rul" ) {
144 FGRUL *rul = new FGRUL;
150 if ( ! short_circuit ) {
152 end = config.find(",", begin);
153 if ( end == string::npos ) {
154 return NULL; // dummy
157 string medium = config.substr(begin, end - begin);
159 SG_LOG( SG_IO, SG_INFO, " medium = " << medium );
161 // determine direction
162 end = config.find(",", begin);
163 if ( end == string::npos ) {
164 return NULL; // dummy
167 string direction = config.substr(begin, end - begin);
169 io->set_direction( direction );
170 SG_LOG( SG_IO, SG_INFO, " direction = " << direction );
173 end = config.find(",", begin);
174 if ( end == string::npos ) {
175 return NULL; // dummy
178 string hertz_str = config.substr(begin, end - begin);
180 double hertz = atof( hertz_str.c_str() );
182 SG_LOG( SG_IO, SG_INFO, " hertz = " << hertz );
184 if ( medium == "serial" ) {
186 end = config.find(",", begin);
187 if ( end == string::npos ) {
191 string device = config.substr(begin, end - begin);
193 SG_LOG( SG_IO, SG_INFO, " device = " << device );
196 string baud = config.substr(begin);
197 SG_LOG( SG_IO, SG_INFO, " baud = " << baud );
199 SGSerial *ch = new SGSerial( device, baud );
200 io->set_io_channel( ch );
201 } else if ( medium == "file" ) {
203 string file = config.substr(begin);
204 SG_LOG( SG_IO, SG_INFO, " file name = " << file );
206 SGFile *ch = new SGFile( file );
207 io->set_io_channel( ch );
208 } else if ( medium == "socket" ) {
210 end = config.find(",", begin);
211 if ( end == string::npos ) {
215 string hostname = config.substr(begin, end - begin);
217 SG_LOG( SG_IO, SG_INFO, " hostname = " << hostname );
220 end = config.find(",", begin);
221 if ( end == string::npos ) {
225 string port = config.substr(begin, end - begin);
227 SG_LOG( SG_IO, SG_INFO, " port string = " << port );
230 string style_str = config.substr(begin);
231 SG_LOG( SG_IO, SG_INFO, " style string = " << style_str );
233 SGSocket *ch = new SGSocket( hostname, port, style_str );
234 io->set_io_channel( ch );
242 // step through the port config streams (from fgOPTIONS) and setup
243 // serial port channels for each
245 // SG_LOG( SG_IO, SG_INFO, "I/O Channel initialization, " <<
246 // globals->get_channel_options_list()->size() << " requests." );
249 string_list *channel_options_list = globals->get_channel_options_list();
251 // we could almost do this in a single step except pushing a valid
252 // port onto the port list copies the structure and destroys the
253 // original, which closes the port and frees up the fd ... doh!!!
255 // parse the configuration strings and store the results in the
256 // appropriate FGIOChannel structures
257 for ( int i = 0; i < (int)channel_options_list->size(); ++i ) {
258 p = parse_port_config( (*channel_options_list)[i] );
261 global_io_list.push_back( p );
262 if ( !p->is_enabled() ) {
263 SG_LOG( SG_IO, SG_ALERT, "I/O Channel config failed." );
267 SG_LOG( SG_IO, SG_INFO, "I/O Channel parse failed." );
273 // process any serial port work
277 // cout << "processing I/O channels" << endl;
279 static int inited = 0;
281 static SGTimeStamp last;
290 interval = current - last;
294 for ( int i = 0; i < (int)global_io_list.size(); ++i ) {
295 // cout << " channel = " << i << endl;
296 p = global_io_list[i];
298 if ( p->is_enabled() ) {
299 p->dec_count_down( interval );
300 while ( p->get_count_down() < 0 ) {
302 p->dec_count_down( -1000000.0 / p->get_hz() );
309 // shutdown all I/O connections
310 void fgIOShutdownAll() {
313 // cout << "processing I/O channels" << endl;
315 for ( int i = 0; i < (int)global_io_list.size(); ++i ) {
316 // cout << " channel = " << i << endl;
317 p = global_io_list[i];
319 if ( p->is_enabled() ) {