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>
35 #include <Main/options.hxx>
37 #include <Network/protocol.hxx>
38 #include <Network/native.hxx>
39 #include <Network/garmin.hxx>
40 #include <Network/nmea.hxx>
41 #include <Network/pve.hxx>
42 #include <Network/ray.hxx>
43 #include <Network/rul.hxx>
44 #include <Network/joyclient.hxx>
46 #include <Time/timestamp.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 == "pve" ) {
85 FGPVE *pve = new FGPVE;
87 } else if ( protocol == "ray" ) {
88 FGRAY *ray = new FGRAY;
90 } else if ( protocol == "rul" ) {
91 FGRUL *rul = new FGRUL;
93 } else if ( protocol == "joyclient" ) {
94 FGJoyClient *joyclient = new FGJoyClient;
101 end = config.find(",", begin);
102 if ( end == string::npos ) {
103 return NULL; // dummy
106 string medium = config.substr(begin, end - begin);
108 FG_LOG( FG_IO, FG_INFO, " medium = " << medium );
110 // determine direction
111 end = config.find(",", begin);
112 if ( end == string::npos ) {
113 return NULL; // dummy
116 string direction = config.substr(begin, end - begin);
118 io->set_direction( direction );
119 FG_LOG( FG_IO, FG_INFO, " direction = " << direction );
122 end = config.find(",", begin);
123 if ( end == string::npos ) {
124 return NULL; // dummy
127 string hertz_str = config.substr(begin, end - begin);
129 double hertz = atof( hertz_str.c_str() );
131 FG_LOG( FG_IO, FG_INFO, " hertz = " << hertz );
133 if ( medium == "serial" ) {
135 end = config.find(",", begin);
136 if ( end == string::npos ) {
140 string device = config.substr(begin, end - begin);
142 FG_LOG( FG_IO, FG_INFO, " device = " << device );
145 string baud = config.substr(begin);
146 FG_LOG( FG_IO, FG_INFO, " baud = " << baud );
148 SGSerial *ch = new SGSerial( device, baud );
149 io->set_io_channel( ch );
150 } else if ( medium == "file" ) {
152 string file = config.substr(begin);
153 FG_LOG( FG_IO, FG_INFO, " file name = " << file );
155 SGFile *ch = new SGFile( file );
156 io->set_io_channel( ch );
157 } else if ( medium == "socket" ) {
159 end = config.find(",", begin);
160 if ( end == string::npos ) {
164 string hostname = config.substr(begin, end - begin);
166 FG_LOG( FG_IO, FG_INFO, " hostname = " << hostname );
169 end = config.find(",", begin);
170 if ( end == string::npos ) {
174 string port = config.substr(begin, end - begin);
176 FG_LOG( FG_IO, FG_INFO, " port string = " << port );
179 string style_str = config.substr(begin);
180 FG_LOG( FG_IO, FG_INFO, " style string = " << style_str );
182 SGSocket *ch = new SGSocket( hostname, port, style_str );
183 io->set_io_channel( ch );
190 // step through the port config streams (from fgOPTIONS) and setup
191 // serial port channels for each
194 string_list channel_options_list =
195 current_options.get_channel_options_list();
197 // we could almost do this in a single step except pushing a valid
198 // port onto the port list copies the structure and destroys the
199 // original, which closes the port and frees up the fd ... doh!!!
201 // parse the configuration strings and store the results in the
202 // appropriate FGIOChannel structures
203 for ( int i = 0; i < (int)channel_options_list.size(); ++i ) {
204 p = parse_port_config( channel_options_list[i] );
207 global_io_list.push_back( p );
208 if ( !p->is_enabled() ) {
209 FG_LOG( FG_IO, FG_INFO, "I/O Channel config failed." );
212 FG_LOG( FG_IO, FG_INFO, "I/O Channel parse failed." );
218 // process any serial port work
222 // cout << "processing I/O channels" << endl;
224 static int inited = 0;
226 static FGTimeStamp last;
235 interval = current - last;
239 for ( int i = 0; i < (int)global_io_list.size(); ++i ) {
240 // cout << " channel = " << i << endl;
241 p = global_io_list[i];
243 if ( p->is_enabled() ) {
244 p->dec_count_down( interval );
245 while ( p->get_count_down() < 0 ) {
247 p->dec_count_down( -1000000.0 / p->get_hz() );