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>
45 #include <Time/timestamp.hxx>
50 // define the global I/O channel list
51 io_container global_io_list;
54 // configure a port based on the config string
55 static FGProtocol *parse_port_config( const string& config )
57 string::size_type begin, end;
61 FG_LOG( FG_IO, FG_INFO, "Parse I/O channel request: " << config );
64 end = config.find(",", begin);
65 if ( end == string::npos ) {
69 string protocol = config.substr(begin, end - begin);
71 FG_LOG( FG_IO, FG_INFO, " protocol = " << protocol );
74 if ( protocol == "native" ) {
75 FGNative *native = new FGNative;
77 } else if ( protocol == "garmin" ) {
78 FGGarmin *garmin = new FGGarmin;
80 } else if ( protocol == "nmea" ) {
81 FGNMEA *nmea = new FGNMEA;
83 } else if ( protocol == "pve" ) {
84 FGPVE *pve = new FGPVE;
86 } else if ( protocol == "rul" ) {
87 FGRUL *rul = new FGRUL;
94 end = config.find(",", begin);
95 if ( end == string::npos ) {
99 string medium = config.substr(begin, end - begin);
101 FG_LOG( FG_IO, FG_INFO, " medium = " << medium );
103 // determine direction
104 end = config.find(",", begin);
105 if ( end == string::npos ) {
106 return NULL; // dummy
109 string direction = config.substr(begin, end - begin);
111 io->set_direction( direction );
112 FG_LOG( FG_IO, FG_INFO, " direction = " << direction );
115 end = config.find(",", begin);
116 if ( end == string::npos ) {
117 return NULL; // dummy
120 string hertz_str = config.substr(begin, end - begin);
122 double hertz = atof( hertz_str.c_str() );
124 FG_LOG( FG_IO, FG_INFO, " hertz = " << hertz );
126 if ( medium == "serial" ) {
127 FGSerial *ch = new FGSerial;
128 io->set_io_channel( ch );
131 end = config.find(",", begin);
132 if ( end == string::npos ) {
136 ch->set_device( config.substr(begin, end - begin) );
138 FG_LOG( FG_IO, FG_INFO, " device = " << ch->get_device() );
141 ch->set_baud( config.substr(begin) );
142 FG_LOG( FG_IO, FG_INFO, " baud = " << ch->get_baud() );
144 io->set_io_channel( ch );
145 } else if ( medium == "file" ) {
146 FGFile *ch = new FGFile;
147 io->set_io_channel( ch );
150 ch->set_file_name( config.substr(begin) );
151 FG_LOG( FG_IO, FG_INFO, " file name = " << ch->get_file_name() );
152 } else if ( medium == "socket" ) {
153 FGSocket *ch = new FGSocket;
154 io->set_io_channel( ch );
157 end = config.find(",", begin);
158 if ( end == string::npos ) {
162 ch->set_hostname( config.substr(begin, end - begin) );
164 FG_LOG( FG_IO, FG_INFO, " hostname = " << ch->get_hostname() );
167 ch->set_port_str( config.substr(begin) );
168 FG_LOG( FG_IO, FG_INFO, " port string = " << ch->get_port_str() );
176 // step through the port config streams (from fgOPTIONS) and setup
177 // serial port channels for each
180 string_list channel_options_list =
181 current_options.get_channel_options_list();
183 // we could almost do this in a single step except pushing a valid
184 // port onto the port list copies the structure and destroys the
185 // original, which closes the port and frees up the fd ... doh!!!
187 // parse the configuration strings and store the results in the
188 // appropriate FGIOChannel structures
189 for ( int i = 0; i < (int)channel_options_list.size(); ++i ) {
190 p = parse_port_config( channel_options_list[i] );
193 global_io_list.push_back( p );
194 if ( !p->is_enabled() ) {
195 FG_LOG( FG_IO, FG_INFO, "I/O Channel config failed." );
198 FG_LOG( FG_IO, FG_INFO, "I/O Channel parse failed." );
204 // process any serial port work
208 // cout << "processing I/O channels" << endl;
210 static int inited = 0;
212 static FGTimeStamp last;
221 interval = current - last;
225 for ( int i = 0; i < (int)global_io_list.size(); ++i ) {
226 // cout << " channel = " << i << endl;
227 p = global_io_list[i];
229 if ( p->is_enabled() ) {
230 p->dec_count_down( interval );
231 if ( p->get_count_down() < 0 ) {
233 p->set_count_down( 1000000.0 / p->get_hz() );