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>
28 #include <simgear/debug/logstream.hxx>
29 #include <simgear/io/iochannel.hxx>
30 #include <simgear/io/sg_file.hxx>
31 #include <simgear/io/sg_serial.hxx>
32 #include <simgear/io/sg_socket.hxx>
33 #include <simgear/math/sg_types.hxx>
34 #include <simgear/timing/timestamp.hxx>
36 #include <Network/protocol.hxx>
37 #include <Network/native.hxx>
38 #include <Network/garmin.hxx>
39 #include <Network/nmea.hxx>
40 #include <Network/props.hxx>
41 #include <Network/pve.hxx>
42 #include <Network/ray.hxx>
43 #include <Network/rul.hxx>
44 #include <Network/joyclient.hxx>
46 #include "globals.hxx"
51 // define the global I/O channel list
52 io_container global_io_list;
55 // configure a port based on the config string
56 static FGProtocol *parse_port_config( const string& config )
58 string::size_type begin, end;
62 FG_LOG( FG_IO, FG_INFO, "Parse I/O channel request: " << config );
65 end = config.find(",", begin);
66 if ( end == string::npos ) {
70 string protocol = config.substr(begin, end - begin);
72 FG_LOG( FG_IO, FG_INFO, " protocol = " << protocol );
75 if ( protocol == "native" ) {
76 FGNative *native = new FGNative;
78 } else if ( protocol == "garmin" ) {
79 FGGarmin *garmin = new FGGarmin;
81 } else if ( protocol == "nmea" ) {
82 FGNMEA *nmea = new FGNMEA;
84 } else if ( protocol == "props" ) {
85 FGProps *props = new FGProps;
87 } else if ( protocol == "pve" ) {
88 FGPVE *pve = new FGPVE;
90 } else if ( protocol == "ray" ) {
91 FGRAY *ray = new FGRAY;
93 } else if ( protocol == "rul" ) {
94 FGRUL *rul = new FGRUL;
96 } else if ( protocol == "joyclient" ) {
97 FGJoyClient *joyclient = new FGJoyClient;
104 end = config.find(",", begin);
105 if ( end == string::npos ) {
106 return NULL; // dummy
109 string medium = config.substr(begin, end - begin);
111 FG_LOG( FG_IO, FG_INFO, " medium = " << medium );
113 // determine direction
114 end = config.find(",", begin);
115 if ( end == string::npos ) {
116 return NULL; // dummy
119 string direction = config.substr(begin, end - begin);
121 io->set_direction( direction );
122 FG_LOG( FG_IO, FG_INFO, " direction = " << direction );
125 end = config.find(",", begin);
126 if ( end == string::npos ) {
127 return NULL; // dummy
130 string hertz_str = config.substr(begin, end - begin);
132 double hertz = atof( hertz_str.c_str() );
134 FG_LOG( FG_IO, FG_INFO, " hertz = " << hertz );
136 if ( medium == "serial" ) {
138 end = config.find(",", begin);
139 if ( end == string::npos ) {
143 string device = config.substr(begin, end - begin);
145 FG_LOG( FG_IO, FG_INFO, " device = " << device );
148 string baud = config.substr(begin);
149 FG_LOG( FG_IO, FG_INFO, " baud = " << baud );
151 SGSerial *ch = new SGSerial( device, baud );
152 io->set_io_channel( ch );
153 } else if ( medium == "file" ) {
155 string file = config.substr(begin);
156 FG_LOG( FG_IO, FG_INFO, " file name = " << file );
158 SGFile *ch = new SGFile( file );
159 io->set_io_channel( ch );
160 } else if ( medium == "socket" ) {
162 end = config.find(",", begin);
163 if ( end == string::npos ) {
167 string hostname = config.substr(begin, end - begin);
169 FG_LOG( FG_IO, FG_INFO, " hostname = " << hostname );
172 end = config.find(",", begin);
173 if ( end == string::npos ) {
177 string port = config.substr(begin, end - begin);
179 FG_LOG( FG_IO, FG_INFO, " port string = " << port );
182 string style_str = config.substr(begin);
183 FG_LOG( FG_IO, FG_INFO, " style string = " << style_str );
185 SGSocket *ch = new SGSocket( hostname, port, style_str );
186 io->set_io_channel( ch );
193 // step through the port config streams (from fgOPTIONS) and setup
194 // serial port channels for each
196 // FG_LOG( FG_IO, FG_INFO, "I/O Channel initialization, " <<
197 // globals->get_channel_options_list()->size() << " requests." );
200 string_list *channel_options_list = globals->get_channel_options_list();
202 // we could almost do this in a single step except pushing a valid
203 // port onto the port list copies the structure and destroys the
204 // original, which closes the port and frees up the fd ... doh!!!
206 // parse the configuration strings and store the results in the
207 // appropriate FGIOChannel structures
208 for ( int i = 0; i < (int)channel_options_list->size(); ++i ) {
209 p = parse_port_config( (*channel_options_list)[i] );
212 global_io_list.push_back( p );
213 if ( !p->is_enabled() ) {
214 FG_LOG( FG_IO, FG_ALERT, "I/O Channel config failed." );
218 FG_LOG( FG_IO, FG_INFO, "I/O Channel parse failed." );
224 // process any serial port work
228 // cout << "processing I/O channels" << endl;
230 static int inited = 0;
232 static SGTimeStamp last;
241 interval = current - last;
245 for ( int i = 0; i < (int)global_io_list.size(); ++i ) {
246 // cout << " channel = " << i << endl;
247 p = global_io_list[i];
249 if ( p->is_enabled() ) {
250 p->dec_count_down( interval );
251 while ( p->get_count_down() < 0 ) {
253 p->dec_count_down( -1000000.0 / p->get_hz() );
260 // shutdown all I/O connections
261 void fgIOShutdownAll() {
264 // cout << "processing I/O channels" << endl;
266 for ( int i = 0; i < (int)global_io_list.size(); ++i ) {
267 // cout << " channel = " << i << endl;
268 p = global_io_list[i];
270 if ( p->is_enabled() ) {