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 <Include/compiler.h>
28 #include <Debug/logstream.hxx>
29 #include <Include/fg_types.hxx>
30 #include <Main/options.hxx>
32 #include <Network/iochannel.hxx>
33 #include <Network/fg_file.hxx>
34 #include <Network/fg_serial.hxx>
35 #include <Network/fg_socket.hxx>
37 #include <Network/protocol.hxx>
38 #include <Network/garmin.hxx>
39 #include <Network/nmea.hxx>
40 #include <Network/pve.hxx>
41 #include <Network/rul.hxx>
43 #include <Time/timestamp.hxx>
48 // define the global I/O channel list
49 io_container global_io_list;
52 // configure a port based on the config string
53 static FGProtocol *parse_port_config( const string& config )
55 string::size_type begin, end;
59 FG_LOG( FG_IO, FG_INFO, "Parse I/O channel request: " << config );
62 end = config.find(",", begin);
63 if ( end == string::npos ) {
67 string protocol = config.substr(begin, end - begin);
69 FG_LOG( FG_IO, FG_INFO, " protocol = " << protocol );
72 if ( protocol == "garmin" ) {
73 FGGarmin *garmin = new FGGarmin;
75 } else if ( protocol == "nmea" ) {
76 FGNMEA *nmea = new FGNMEA;
78 } else if ( protocol == "pve" ) {
79 FGPVE *pve = new FGPVE;
81 } else if ( protocol == "rul" ) {
82 FGRUL *rul = new FGRUL;
89 end = config.find(",", begin);
90 if ( end == string::npos ) {
94 string medium = config.substr(begin, end - begin);
96 FG_LOG( FG_IO, FG_INFO, " medium = " << medium );
98 // determine direction
99 end = config.find(",", begin);
100 if ( end == string::npos ) {
101 return NULL; // dummy
104 string direction = config.substr(begin, end - begin);
106 io->set_direction( direction );
107 FG_LOG( FG_IO, FG_INFO, " direction = " << direction );
110 end = config.find(",", begin);
111 if ( end == string::npos ) {
112 return NULL; // dummy
115 string hertz_str = config.substr(begin, end - begin);
117 double hertz = atof( hertz_str.c_str() );
119 FG_LOG( FG_IO, FG_INFO, " hertz = " << hertz );
121 if ( medium == "serial" ) {
122 FGSerial *ch = new FGSerial;
123 io->set_io_channel( ch );
126 end = config.find(",", begin);
127 if ( end == string::npos ) {
131 ch->set_device( config.substr(begin, end - begin) );
133 FG_LOG( FG_IO, FG_INFO, " device = " << ch->get_device() );
136 ch->set_baud( config.substr(begin) );
137 FG_LOG( FG_IO, FG_INFO, " baud = " << ch->get_baud() );
139 io->set_io_channel( ch );
140 } else if ( medium == "file" ) {
141 FGFile *ch = new FGFile;
142 io->set_io_channel( ch );
145 ch->set_file_name( config.substr(begin) );
146 FG_LOG( FG_IO, FG_INFO, " file name = " << ch->get_file_name() );
147 } else if ( medium == "socket" ) {
148 FGSocket *ch = new FGSocket;
149 io->set_io_channel( ch );
152 end = config.find(",", begin);
153 if ( end == string::npos ) {
157 ch->set_hostname( config.substr(begin, end - begin) );
159 FG_LOG( FG_IO, FG_INFO, " hostname = " << ch->get_hostname() );
162 ch->set_port_str( config.substr(begin) );
163 FG_LOG( FG_IO, FG_INFO, " port string = " << ch->get_port_str() );
171 // step through the port config streams (from fgOPTIONS) and setup
172 // serial port channels for each
175 string_list channel_options_list =
176 current_options.get_channel_options_list();
178 // we could almost do this in a single step except pushing a valid
179 // port onto the port list copies the structure and destroys the
180 // original, which closes the port and frees up the fd ... doh!!!
182 // parse the configuration strings and store the results in the
183 // appropriate FGIOChannel structures
184 for ( int i = 0; i < (int)channel_options_list.size(); ++i ) {
185 p = parse_port_config( channel_options_list[i] );
188 global_io_list.push_back( p );
189 if ( !p->is_enabled() ) {
190 FG_LOG( FG_IO, FG_INFO, "I/O Channel config failed." );
193 FG_LOG( FG_IO, FG_INFO, "I/O Channel parse failed." );
199 // process any serial port work
203 // cout << "processing I/O channels" << endl;
205 static int inited = 0;
207 static FGTimeStamp last;
216 interval = current - last;
220 for ( int i = 0; i < (int)global_io_list.size(); ++i ) {
221 // cout << " channel = " << i << endl;
222 p = global_io_list[i];
224 if ( p->is_enabled() ) {
225 p->dec_count_down( interval );
226 if ( p->get_count_down() < 0 ) {
228 p->set_count_down( 1000000.0 / p->get_hz() );