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/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" ) {
135 FGSerial *ch = new FGSerial;
136 io->set_io_channel( ch );
139 end = config.find(",", begin);
140 if ( end == string::npos ) {
144 ch->set_device( config.substr(begin, end - begin) );
146 FG_LOG( FG_IO, FG_INFO, " device = " << ch->get_device() );
149 ch->set_baud( config.substr(begin) );
150 FG_LOG( FG_IO, FG_INFO, " baud = " << ch->get_baud() );
152 io->set_io_channel( ch );
153 } else if ( medium == "file" ) {
154 FGFile *ch = new FGFile;
155 io->set_io_channel( ch );
158 ch->set_file_name( config.substr(begin) );
159 FG_LOG( FG_IO, FG_INFO, " file name = " << ch->get_file_name() );
160 } else if ( medium == "socket" ) {
161 FGSocket *ch = new FGSocket;
162 io->set_io_channel( ch );
165 end = config.find(",", begin);
166 if ( end == string::npos ) {
170 ch->set_hostname( config.substr(begin, end - begin) );
172 FG_LOG( FG_IO, FG_INFO, " hostname = " << ch->get_hostname() );
175 ch->set_port_str( config.substr(begin) );
176 FG_LOG( FG_IO, FG_INFO, " port string = " << ch->get_port_str() );
184 // step through the port config streams (from fgOPTIONS) and setup
185 // serial port channels for each
188 string_list channel_options_list =
189 current_options.get_channel_options_list();
191 // we could almost do this in a single step except pushing a valid
192 // port onto the port list copies the structure and destroys the
193 // original, which closes the port and frees up the fd ... doh!!!
195 // parse the configuration strings and store the results in the
196 // appropriate FGIOChannel structures
197 for ( int i = 0; i < (int)channel_options_list.size(); ++i ) {
198 p = parse_port_config( channel_options_list[i] );
201 global_io_list.push_back( p );
202 if ( !p->is_enabled() ) {
203 FG_LOG( FG_IO, FG_INFO, "I/O Channel config failed." );
206 FG_LOG( FG_IO, FG_INFO, "I/O Channel parse failed." );
212 // process any serial port work
216 // cout << "processing I/O channels" << endl;
218 static int inited = 0;
220 static FGTimeStamp last;
229 interval = current - last;
233 for ( int i = 0; i < (int)global_io_list.size(); ++i ) {
234 // cout << " channel = " << i << endl;
235 p = global_io_list[i];
237 if ( p->is_enabled() ) {
238 p->dec_count_down( interval );
239 while ( p->get_count_down() < 0 ) {
241 p->dec_count_down( -1000000.0 / p->get_hz() );