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/math/fg_types.hxx>
31 #include <Main/options.hxx>
33 #include <Network/iochannel.hxx>
34 #include <Network/fg_file.hxx>
35 #include <Network/fg_serial.hxx>
36 #include <Network/fg_socket.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/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 == "rul" ) {
88 FGRUL *rul = new FGRUL;
90 } else if ( protocol == "joyclient" ) {
91 FGJoyClient *joyclient = new FGJoyClient;
98 end = config.find(",", begin);
99 if ( end == string::npos ) {
100 return NULL; // dummy
103 string medium = config.substr(begin, end - begin);
105 FG_LOG( FG_IO, FG_INFO, " medium = " << medium );
107 // determine direction
108 end = config.find(",", begin);
109 if ( end == string::npos ) {
110 return NULL; // dummy
113 string direction = config.substr(begin, end - begin);
115 io->set_direction( direction );
116 FG_LOG( FG_IO, FG_INFO, " direction = " << direction );
119 end = config.find(",", begin);
120 if ( end == string::npos ) {
121 return NULL; // dummy
124 string hertz_str = config.substr(begin, end - begin);
126 double hertz = atof( hertz_str.c_str() );
128 FG_LOG( FG_IO, FG_INFO, " hertz = " << hertz );
130 if ( medium == "serial" ) {
131 FGSerial *ch = new FGSerial;
132 io->set_io_channel( ch );
135 end = config.find(",", begin);
136 if ( end == string::npos ) {
140 ch->set_device( config.substr(begin, end - begin) );
142 FG_LOG( FG_IO, FG_INFO, " device = " << ch->get_device() );
145 ch->set_baud( config.substr(begin) );
146 FG_LOG( FG_IO, FG_INFO, " baud = " << ch->get_baud() );
148 io->set_io_channel( ch );
149 } else if ( medium == "file" ) {
150 FGFile *ch = new FGFile;
151 io->set_io_channel( ch );
154 ch->set_file_name( config.substr(begin) );
155 FG_LOG( FG_IO, FG_INFO, " file name = " << ch->get_file_name() );
156 } else if ( medium == "socket" ) {
157 FGSocket *ch = new FGSocket;
158 io->set_io_channel( ch );
161 end = config.find(",", begin);
162 if ( end == string::npos ) {
166 ch->set_hostname( config.substr(begin, end - begin) );
168 FG_LOG( FG_IO, FG_INFO, " hostname = " << ch->get_hostname() );
171 ch->set_port_str( config.substr(begin) );
172 FG_LOG( FG_IO, FG_INFO, " port string = " << ch->get_port_str() );
180 // step through the port config streams (from fgOPTIONS) and setup
181 // serial port channels for each
184 string_list channel_options_list =
185 current_options.get_channel_options_list();
187 // we could almost do this in a single step except pushing a valid
188 // port onto the port list copies the structure and destroys the
189 // original, which closes the port and frees up the fd ... doh!!!
191 // parse the configuration strings and store the results in the
192 // appropriate FGIOChannel structures
193 for ( int i = 0; i < (int)channel_options_list.size(); ++i ) {
194 p = parse_port_config( channel_options_list[i] );
197 global_io_list.push_back( p );
198 if ( !p->is_enabled() ) {
199 FG_LOG( FG_IO, FG_INFO, "I/O Channel config failed." );
202 FG_LOG( FG_IO, FG_INFO, "I/O Channel parse failed." );
208 // process any serial port work
212 // cout << "processing I/O channels" << endl;
214 static int inited = 0;
216 static FGTimeStamp last;
225 interval = current - last;
229 for ( int i = 0; i < (int)global_io_list.size(); ++i ) {
230 // cout << " channel = " << i << endl;
231 p = global_io_list[i];
233 if ( p->is_enabled() ) {
234 p->dec_count_down( interval );
235 if ( p->get_count_down() < 0 ) {
237 p->set_count_down( 1000000.0 / p->get_hz() );