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"
60 SG_USING_NAMESPACE(std);
64 // define the global I/O channel list
65 io_container global_io_list;
68 // configure a port based on the config string
69 static FGProtocol *parse_port_config( const string& config )
71 bool short_circuit = false;
73 string::size_type begin, end;
77 SG_LOG( SG_IO, SG_INFO, "Parse I/O channel request: " << config );
80 end = config.find(",", begin);
81 if ( end == string::npos ) {
85 string protocol = config.substr(begin, end - begin);
87 SG_LOG( SG_IO, SG_INFO, " protocol = " << protocol );
90 if ( protocol == "atc610x" ) {
91 FGATC610x *atc610x = new FGATC610x;
94 cout << "here ..." << endl;
95 } else if ( protocol == "atlas" ) {
96 FGAtlas *atlas = new FGAtlas;
98 } else if ( protocol == "opengc" ) {
100 // printf("Parsed opengc\n"); cin >> wait;
101 FGOpenGC *opengc = new FGOpenGC;
103 } else if ( protocol == "garmin" ) {
104 FGGarmin *garmin = new FGGarmin;
106 } else if ( protocol == "httpd" ) {
108 string port = config.substr(begin);
109 FGHttpd *httpd = new FGHttpd( atoi(port.c_str()) );
111 short_circuit = true;
112 #ifdef FG_JPEG_SERVER
113 } else if ( protocol == "jpg-httpd" ) {
115 string port = config.substr(begin);
116 FGJpegHttpd *jpeg_httpd = new FGJpegHttpd( atoi(port.c_str()) );
118 short_circuit = true;
120 } else if ( protocol == "joyclient" ) {
121 FGJoyClient *joyclient = new FGJoyClient;
123 } else if ( protocol == "native" ) {
124 FGNative *native = new FGNative;
126 } else if ( protocol == "native_ctrls" ) {
127 FGNativeCtrls *native_ctrls = new FGNativeCtrls;
129 } else if ( protocol == "native_fdm" ) {
130 FGNativeFDM *native_fdm = new FGNativeFDM;
132 } else if ( protocol == "nmea" ) {
133 FGNMEA *nmea = new FGNMEA;
135 } else if ( protocol == "props" ) {
136 FGProps *props = new FGProps;
138 } else if ( protocol == "pve" ) {
139 FGPVE *pve = new FGPVE;
141 } else if ( protocol == "ray" ) {
142 FGRAY *ray = new FGRAY;
144 } else if ( protocol == "rul" ) {
145 FGRUL *rul = new FGRUL;
151 if ( ! short_circuit ) {
153 end = config.find(",", begin);
154 if ( end == string::npos ) {
155 return NULL; // dummy
158 string medium = config.substr(begin, end - begin);
160 SG_LOG( SG_IO, SG_INFO, " medium = " << medium );
162 // determine direction
163 end = config.find(",", begin);
164 if ( end == string::npos ) {
165 return NULL; // dummy
168 string direction = config.substr(begin, end - begin);
170 io->set_direction( direction );
171 SG_LOG( SG_IO, SG_INFO, " direction = " << direction );
174 end = config.find(",", begin);
175 if ( end == string::npos ) {
176 return NULL; // dummy
179 string hertz_str = config.substr(begin, end - begin);
181 double hertz = atof( hertz_str.c_str() );
183 SG_LOG( SG_IO, SG_INFO, " hertz = " << hertz );
185 if ( medium == "serial" ) {
187 end = config.find(",", begin);
188 if ( end == string::npos ) {
192 string device = config.substr(begin, end - begin);
194 SG_LOG( SG_IO, SG_INFO, " device = " << device );
197 string baud = config.substr(begin);
198 SG_LOG( SG_IO, SG_INFO, " baud = " << baud );
200 SGSerial *ch = new SGSerial( device, baud );
201 io->set_io_channel( ch );
202 } else if ( medium == "file" ) {
204 string file = config.substr(begin);
205 SG_LOG( SG_IO, SG_INFO, " file name = " << file );
207 SGFile *ch = new SGFile( file );
208 io->set_io_channel( ch );
209 } else if ( medium == "socket" ) {
211 end = config.find(",", begin);
212 if ( end == string::npos ) {
216 string hostname = config.substr(begin, end - begin);
218 SG_LOG( SG_IO, SG_INFO, " hostname = " << hostname );
221 end = config.find(",", begin);
222 if ( end == string::npos ) {
226 string port = config.substr(begin, end - begin);
228 SG_LOG( SG_IO, SG_INFO, " port string = " << port );
231 string style_str = config.substr(begin);
232 SG_LOG( SG_IO, SG_INFO, " style string = " << style_str );
234 SGSocket *ch = new SGSocket( hostname, port, style_str );
235 io->set_io_channel( ch );
243 // step through the port config streams (from fgOPTIONS) and setup
244 // serial port channels for each
246 // SG_LOG( SG_IO, SG_INFO, "I/O Channel initialization, " <<
247 // globals->get_channel_options_list()->size() << " requests." );
250 string_list *channel_options_list = globals->get_channel_options_list();
252 // we could almost do this in a single step except pushing a valid
253 // port onto the port list copies the structure and destroys the
254 // original, which closes the port and frees up the fd ... doh!!!
256 // parse the configuration strings and store the results in the
257 // appropriate FGIOChannel structures
258 for ( int i = 0; i < (int)channel_options_list->size(); ++i ) {
259 p = parse_port_config( (*channel_options_list)[i] );
262 global_io_list.push_back( p );
263 if ( !p->is_enabled() ) {
264 SG_LOG( SG_IO, SG_ALERT, "I/O Channel config failed." );
268 SG_LOG( SG_IO, SG_INFO, "I/O Channel parse failed." );
274 // process any serial port work
278 // cout << "processing I/O channels" << endl;
280 static int inited = 0;
282 static SGTimeStamp last;
291 interval = current - last;
295 for ( int i = 0; i < (int)global_io_list.size(); ++i ) {
296 // cout << " channel = " << i << endl;
297 p = global_io_list[i];
299 if ( p->is_enabled() ) {
300 p->dec_count_down( interval );
301 while ( p->get_count_down() < 0 ) {
303 p->dec_count_down(int( -1000000.0 / p->get_hz()));
310 // shutdown all I/O connections
311 void fgIOShutdownAll() {
314 // cout << "processing I/O channels" << endl;
316 for ( int i = 0; i < (int)global_io_list.size(); ++i ) {
317 // cout << " channel = " << i << endl;
318 p = global_io_list[i];
320 if ( p->is_enabled() ) {