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>
34 #include <simgear/timing/timestamp.hxx>
36 #include <Network/protocol.hxx>
37 #include <Network/native.hxx>
38 #include <Network/garmin.hxx>
39 #include <Network/nmea.hxx>
40 #include <Network/props.hxx>
41 #include <Network/pve.hxx>
42 #include <Network/ray.hxx>
43 #include <Network/rul.hxx>
44 #include <Network/joyclient.hxx>
46 #include "globals.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 == "props" ) {
85 FGProps *props = new FGProps;
87 } else if ( protocol == "pve" ) {
88 FGPVE *pve = new FGPVE;
90 } else if ( protocol == "ray" ) {
91 FGRAY *ray = new FGRAY;
93 } else if ( protocol == "rul" ) {
94 FGRUL *rul = new FGRUL;
96 } else if ( protocol == "joyclient" ) {
97 FGJoyClient *joyclient = new FGJoyClient;
104 end = config.find(",", begin);
105 if ( end == string::npos ) {
106 return NULL; // dummy
109 string medium = config.substr(begin, end - begin);
111 FG_LOG( FG_IO, FG_INFO, " medium = " << medium );
113 // determine direction
114 end = config.find(",", begin);
115 if ( end == string::npos ) {
116 return NULL; // dummy
119 string direction = config.substr(begin, end - begin);
121 io->set_direction( direction );
122 FG_LOG( FG_IO, FG_INFO, " direction = " << direction );
125 end = config.find(",", begin);
126 if ( end == string::npos ) {
127 return NULL; // dummy
130 string hertz_str = config.substr(begin, end - begin);
132 double hertz = atof( hertz_str.c_str() );
134 FG_LOG( FG_IO, FG_INFO, " hertz = " << hertz );
136 if ( medium == "serial" ) {
138 end = config.find(",", begin);
139 if ( end == string::npos ) {
143 string device = config.substr(begin, end - begin);
145 FG_LOG( FG_IO, FG_INFO, " device = " << device );
148 string baud = config.substr(begin);
149 FG_LOG( FG_IO, FG_INFO, " baud = " << baud );
151 SGSerial *ch = new SGSerial( device, baud );
152 io->set_io_channel( ch );
153 } else if ( medium == "file" ) {
155 string file = config.substr(begin);
156 FG_LOG( FG_IO, FG_INFO, " file name = " << file );
158 SGFile *ch = new SGFile( file );
159 io->set_io_channel( ch );
160 } else if ( medium == "socket" ) {
162 end = config.find(",", begin);
163 if ( end == string::npos ) {
167 string hostname = config.substr(begin, end - begin);
169 FG_LOG( FG_IO, FG_INFO, " hostname = " << hostname );
172 end = config.find(",", begin);
173 if ( end == string::npos ) {
177 string port = config.substr(begin, end - begin);
179 FG_LOG( FG_IO, FG_INFO, " port string = " << port );
182 string style_str = config.substr(begin);
183 FG_LOG( FG_IO, FG_INFO, " style string = " << style_str );
185 SGSocket *ch = new SGSocket( hostname, port, style_str );
186 io->set_io_channel( ch );
193 // step through the port config streams (from fgOPTIONS) and setup
194 // serial port channels for each
197 string_list channel_options_list =
198 globals->get_channel_options_list();
200 // we could almost do this in a single step except pushing a valid
201 // port onto the port list copies the structure and destroys the
202 // original, which closes the port and frees up the fd ... doh!!!
204 // parse the configuration strings and store the results in the
205 // appropriate FGIOChannel structures
206 for ( int i = 0; i < (int)channel_options_list.size(); ++i ) {
207 p = parse_port_config( channel_options_list[i] );
210 global_io_list.push_back( p );
211 if ( !p->is_enabled() ) {
212 FG_LOG( FG_IO, FG_ALERT, "I/O Channel config failed." );
216 FG_LOG( FG_IO, FG_INFO, "I/O Channel parse failed." );
222 // process any serial port work
226 // cout << "processing I/O channels" << endl;
228 static int inited = 0;
230 static SGTimeStamp last;
239 interval = current - last;
243 for ( int i = 0; i < (int)global_io_list.size(); ++i ) {
244 // cout << " channel = " << i << endl;
245 p = global_io_list[i];
247 if ( p->is_enabled() ) {
248 p->dec_count_down( interval );
249 while ( p->get_count_down() < 0 ) {
251 p->dec_count_down( -1000000.0 / p->get_hz() );
258 // shutdown all I/O connections
259 void fgIOShutdownAll() {
262 // cout << "processing I/O channels" << endl;
264 for ( int i = 0; i < (int)global_io_list.size(); ++i ) {
265 // cout << " channel = " << i << endl;
266 p = global_io_list[i];
268 if ( p->is_enabled() ) {