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>
34 #include <simgear/math/fg_types.hxx>
36 #include <Main/options.hxx>
38 #include <Network/protocol.hxx>
39 #include <Network/native.hxx>
40 #include <Network/garmin.hxx>
41 #include <Network/nmea.hxx>
42 #include <Network/pve.hxx>
43 #include <Network/ray.hxx>
44 #include <Network/rul.hxx>
45 #include <Network/joyclient.hxx>
47 #include <Time/timestamp.hxx>
52 // define the global I/O channel list
53 io_container global_io_list;
56 // configure a port based on the config string
57 static FGProtocol *parse_port_config( const string& config )
59 string::size_type begin, end;
63 FG_LOG( FG_IO, FG_INFO, "Parse I/O channel request: " << config );
66 end = config.find(",", begin);
67 if ( end == string::npos ) {
71 string protocol = config.substr(begin, end - begin);
73 FG_LOG( FG_IO, FG_INFO, " protocol = " << protocol );
76 if ( protocol == "native" ) {
77 FGNative *native = new FGNative;
79 } else if ( protocol == "garmin" ) {
80 FGGarmin *garmin = new FGGarmin;
82 } else if ( protocol == "nmea" ) {
83 FGNMEA *nmea = new FGNMEA;
85 } else if ( protocol == "pve" ) {
86 FGPVE *pve = new FGPVE;
88 } else if ( protocol == "ray" ) {
89 FGRAY *ray = new FGRAY;
91 } else if ( protocol == "rul" ) {
92 FGRUL *rul = new FGRUL;
94 } else if ( protocol == "joyclient" ) {
95 FGJoyClient *joyclient = new FGJoyClient;
102 end = config.find(",", begin);
103 if ( end == string::npos ) {
104 return NULL; // dummy
107 string medium = config.substr(begin, end - begin);
109 FG_LOG( FG_IO, FG_INFO, " medium = " << medium );
111 // determine direction
112 end = config.find(",", begin);
113 if ( end == string::npos ) {
114 return NULL; // dummy
117 string direction = config.substr(begin, end - begin);
119 io->set_direction( direction );
120 FG_LOG( FG_IO, FG_INFO, " direction = " << direction );
123 end = config.find(",", begin);
124 if ( end == string::npos ) {
125 return NULL; // dummy
128 string hertz_str = config.substr(begin, end - begin);
130 double hertz = atof( hertz_str.c_str() );
132 FG_LOG( FG_IO, FG_INFO, " hertz = " << hertz );
134 if ( medium == "serial" ) {
136 end = config.find(",", begin);
137 if ( end == string::npos ) {
141 string device = config.substr(begin, end - begin);
143 FG_LOG( FG_IO, FG_INFO, " device = " << device );
146 string baud = config.substr(begin);
147 FG_LOG( FG_IO, FG_INFO, " baud = " << baud );
149 SGSerial *ch = new SGSerial( device, baud );
150 io->set_io_channel( ch );
151 } else if ( medium == "file" ) {
153 string file = config.substr(begin);
154 FG_LOG( FG_IO, FG_INFO, " file name = " << file );
156 SGFile *ch = new SGFile( file );
157 io->set_io_channel( ch );
158 } else if ( medium == "socket" ) {
160 end = config.find(",", begin);
161 if ( end == string::npos ) {
165 string hostname = config.substr(begin, end - begin);
167 FG_LOG( FG_IO, FG_INFO, " hostname = " << hostname );
170 end = config.find(",", begin);
171 if ( end == string::npos ) {
175 string port = config.substr(begin, end - begin);
177 FG_LOG( FG_IO, FG_INFO, " port string = " << port );
180 string style_str = config.substr(begin);
181 FG_LOG( FG_IO, FG_INFO, " style string = " << style_str );
183 SGSocket *ch = new SGSocket( hostname, port, style_str );
184 io->set_io_channel( ch );
191 // step through the port config streams (from fgOPTIONS) and setup
192 // serial port channels for each
195 string_list channel_options_list =
196 current_options.get_channel_options_list();
198 // we could almost do this in a single step except pushing a valid
199 // port onto the port list copies the structure and destroys the
200 // original, which closes the port and frees up the fd ... doh!!!
202 // parse the configuration strings and store the results in the
203 // appropriate FGIOChannel structures
204 for ( int i = 0; i < (int)channel_options_list.size(); ++i ) {
205 p = parse_port_config( channel_options_list[i] );
208 global_io_list.push_back( p );
209 if ( !p->is_enabled() ) {
210 FG_LOG( FG_IO, FG_INFO, "I/O Channel config failed." );
213 FG_LOG( FG_IO, FG_INFO, "I/O Channel parse failed." );
219 // process any serial port work
223 // cout << "processing I/O channels" << endl;
225 static int inited = 0;
227 static FGTimeStamp last;
236 interval = current - last;
240 for ( int i = 0; i < (int)global_io_list.size(); ++i ) {
241 // cout << " channel = " << i << endl;
242 p = global_io_list[i];
244 if ( p->is_enabled() ) {
245 p->dec_count_down( interval );
246 while ( p->get_count_down() < 0 ) {
248 p->dec_count_down( -1000000.0 / p->get_hz() );