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/atlas.hxx>
41 #include <Network/garmin.hxx>
42 #include <Network/httpd.hxx>
43 #include <Network/joyclient.hxx>
44 #include <Network/native.hxx>
45 #include <Network/native_ctrls.hxx>
46 #include <Network/native_fdm.hxx>
47 #include <Network/opengc.hxx>
48 #include <Network/nmea.hxx>
49 #include <Network/props.hxx>
50 #include <Network/pve.hxx>
51 #include <Network/ray.hxx>
52 #include <Network/rul.hxx>
54 #include "globals.hxx"
59 // define the global I/O channel list
60 io_container global_io_list;
63 // configure a port based on the config string
64 static FGProtocol *parse_port_config( const string& config )
66 bool short_circuit = false;
68 string::size_type begin, end;
72 SG_LOG( SG_IO, SG_INFO, "Parse I/O channel request: " << config );
75 end = config.find(",", begin);
76 if ( end == string::npos ) {
80 string protocol = config.substr(begin, end - begin);
82 SG_LOG( SG_IO, SG_INFO, " protocol = " << protocol );
85 if ( protocol == "atlas" ) {
86 FGAtlas *atlas = new FGAtlas;
88 } else if ( protocol == "opengc" ) {
90 // printf("Parsed opengc\n"); cin >> wait;
91 FGOpenGC *opengc = new FGOpenGC;
93 } else if ( protocol == "garmin" ) {
94 FGGarmin *garmin = new FGGarmin;
96 } else if ( protocol == "httpd" ) {
98 string port = config.substr(begin);
99 FGHttpd *httpd = new FGHttpd( atoi(port.c_str()) );
101 short_circuit = true;
102 } else if ( protocol == "joyclient" ) {
103 FGJoyClient *joyclient = new FGJoyClient;
105 } else if ( protocol == "native" ) {
106 FGNative *native = new FGNative;
108 } else if ( protocol == "native_ctrls" ) {
109 FGNativeCtrls *native_ctrls = new FGNativeCtrls;
111 } else if ( protocol == "native_fdm" ) {
112 FGNativeFDM *native_fdm = new FGNativeFDM;
114 } else if ( protocol == "nmea" ) {
115 FGNMEA *nmea = new FGNMEA;
117 } else if ( protocol == "props" ) {
118 FGProps *props = new FGProps;
120 } else if ( protocol == "pve" ) {
121 FGPVE *pve = new FGPVE;
123 } else if ( protocol == "ray" ) {
124 FGRAY *ray = new FGRAY;
126 } else if ( protocol == "rul" ) {
127 FGRUL *rul = new FGRUL;
133 if ( ! short_circuit ) {
135 end = config.find(",", begin);
136 if ( end == string::npos ) {
137 return NULL; // dummy
140 string medium = config.substr(begin, end - begin);
142 SG_LOG( SG_IO, SG_INFO, " medium = " << medium );
144 // determine direction
145 end = config.find(",", begin);
146 if ( end == string::npos ) {
147 return NULL; // dummy
150 string direction = config.substr(begin, end - begin);
152 io->set_direction( direction );
153 SG_LOG( SG_IO, SG_INFO, " direction = " << direction );
156 end = config.find(",", begin);
157 if ( end == string::npos ) {
158 return NULL; // dummy
161 string hertz_str = config.substr(begin, end - begin);
163 double hertz = atof( hertz_str.c_str() );
165 SG_LOG( SG_IO, SG_INFO, " hertz = " << hertz );
167 if ( medium == "serial" ) {
169 end = config.find(",", begin);
170 if ( end == string::npos ) {
174 string device = config.substr(begin, end - begin);
176 SG_LOG( SG_IO, SG_INFO, " device = " << device );
179 string baud = config.substr(begin);
180 SG_LOG( SG_IO, SG_INFO, " baud = " << baud );
182 SGSerial *ch = new SGSerial( device, baud );
183 io->set_io_channel( ch );
184 } else if ( medium == "file" ) {
186 string file = config.substr(begin);
187 SG_LOG( SG_IO, SG_INFO, " file name = " << file );
189 SGFile *ch = new SGFile( file );
190 io->set_io_channel( ch );
191 } else if ( medium == "socket" ) {
193 end = config.find(",", begin);
194 if ( end == string::npos ) {
198 string hostname = config.substr(begin, end - begin);
200 SG_LOG( SG_IO, SG_INFO, " hostname = " << hostname );
203 end = config.find(",", begin);
204 if ( end == string::npos ) {
208 string port = config.substr(begin, end - begin);
210 SG_LOG( SG_IO, SG_INFO, " port string = " << port );
213 string style_str = config.substr(begin);
214 SG_LOG( SG_IO, SG_INFO, " style string = " << style_str );
216 SGSocket *ch = new SGSocket( hostname, port, style_str );
217 io->set_io_channel( ch );
225 // step through the port config streams (from fgOPTIONS) and setup
226 // serial port channels for each
228 // SG_LOG( SG_IO, SG_INFO, "I/O Channel initialization, " <<
229 // globals->get_channel_options_list()->size() << " requests." );
232 string_list *channel_options_list = globals->get_channel_options_list();
234 // we could almost do this in a single step except pushing a valid
235 // port onto the port list copies the structure and destroys the
236 // original, which closes the port and frees up the fd ... doh!!!
238 // parse the configuration strings and store the results in the
239 // appropriate FGIOChannel structures
240 for ( int i = 0; i < (int)channel_options_list->size(); ++i ) {
241 p = parse_port_config( (*channel_options_list)[i] );
244 global_io_list.push_back( p );
245 if ( !p->is_enabled() ) {
246 SG_LOG( SG_IO, SG_ALERT, "I/O Channel config failed." );
250 SG_LOG( SG_IO, SG_INFO, "I/O Channel parse failed." );
256 // process any serial port work
260 // cout << "processing I/O channels" << endl;
262 static int inited = 0;
264 static SGTimeStamp last;
273 interval = current - last;
277 for ( int i = 0; i < (int)global_io_list.size(); ++i ) {
278 // cout << " channel = " << i << endl;
279 p = global_io_list[i];
281 if ( p->is_enabled() ) {
282 p->dec_count_down( interval );
283 while ( p->get_count_down() < 0 ) {
285 p->dec_count_down( -1000000.0 / p->get_hz() );
292 // shutdown all I/O connections
293 void fgIOShutdownAll() {
296 // cout << "processing I/O channels" << endl;
298 for ( int i = 0; i < (int)global_io_list.size(); ++i ) {
299 // cout << " channel = " << i << endl;
300 p = global_io_list[i];
302 if ( p->is_enabled() ) {