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>
35 #include <Network/protocol.hxx>
36 #include <Network/native.hxx>
37 #include <Network/garmin.hxx>
38 #include <Network/nmea.hxx>
39 #include <Network/props.hxx>
40 #include <Network/pve.hxx>
41 #include <Network/ray.hxx>
42 #include <Network/rul.hxx>
43 #include <Network/joyclient.hxx>
45 #include <Time/timestamp.hxx>
47 #include "globals.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 == "props" ) {
86 FGProps *props = new FGProps;
88 } else if ( protocol == "pve" ) {
89 FGPVE *pve = new FGPVE;
91 } else if ( protocol == "ray" ) {
92 FGRAY *ray = new FGRAY;
94 } else if ( protocol == "rul" ) {
95 FGRUL *rul = new FGRUL;
97 } else if ( protocol == "joyclient" ) {
98 FGJoyClient *joyclient = new FGJoyClient;
105 end = config.find(",", begin);
106 if ( end == string::npos ) {
107 return NULL; // dummy
110 string medium = config.substr(begin, end - begin);
112 FG_LOG( FG_IO, FG_INFO, " medium = " << medium );
114 // determine direction
115 end = config.find(",", begin);
116 if ( end == string::npos ) {
117 return NULL; // dummy
120 string direction = config.substr(begin, end - begin);
122 io->set_direction( direction );
123 FG_LOG( FG_IO, FG_INFO, " direction = " << direction );
126 end = config.find(",", begin);
127 if ( end == string::npos ) {
128 return NULL; // dummy
131 string hertz_str = config.substr(begin, end - begin);
133 double hertz = atof( hertz_str.c_str() );
135 FG_LOG( FG_IO, FG_INFO, " hertz = " << hertz );
137 if ( medium == "serial" ) {
139 end = config.find(",", begin);
140 if ( end == string::npos ) {
144 string device = config.substr(begin, end - begin);
146 FG_LOG( FG_IO, FG_INFO, " device = " << device );
149 string baud = config.substr(begin);
150 FG_LOG( FG_IO, FG_INFO, " baud = " << baud );
152 SGSerial *ch = new SGSerial( device, baud );
153 io->set_io_channel( ch );
154 } else if ( medium == "file" ) {
156 string file = config.substr(begin);
157 FG_LOG( FG_IO, FG_INFO, " file name = " << file );
159 SGFile *ch = new SGFile( file );
160 io->set_io_channel( ch );
161 } else if ( medium == "socket" ) {
163 end = config.find(",", begin);
164 if ( end == string::npos ) {
168 string hostname = config.substr(begin, end - begin);
170 FG_LOG( FG_IO, FG_INFO, " hostname = " << hostname );
173 end = config.find(",", begin);
174 if ( end == string::npos ) {
178 string port = config.substr(begin, end - begin);
180 FG_LOG( FG_IO, FG_INFO, " port string = " << port );
183 string style_str = config.substr(begin);
184 FG_LOG( FG_IO, FG_INFO, " style string = " << style_str );
186 SGSocket *ch = new SGSocket( hostname, port, style_str );
187 io->set_io_channel( ch );
194 // step through the port config streams (from fgOPTIONS) and setup
195 // serial port channels for each
198 string_list channel_options_list =
199 globals->get_options()->get_channel_options_list();
201 // we could almost do this in a single step except pushing a valid
202 // port onto the port list copies the structure and destroys the
203 // original, which closes the port and frees up the fd ... doh!!!
205 // parse the configuration strings and store the results in the
206 // appropriate FGIOChannel structures
207 for ( int i = 0; i < (int)channel_options_list.size(); ++i ) {
208 p = parse_port_config( channel_options_list[i] );
211 global_io_list.push_back( p );
212 if ( !p->is_enabled() ) {
213 FG_LOG( FG_IO, FG_ALERT, "I/O Channel config failed." );
217 FG_LOG( FG_IO, FG_INFO, "I/O Channel parse failed." );
223 // process any serial port work
227 // cout << "processing I/O channels" << endl;
229 static int inited = 0;
231 static FGTimeStamp last;
240 interval = current - last;
244 for ( int i = 0; i < (int)global_io_list.size(); ++i ) {
245 // cout << " channel = " << i << endl;
246 p = global_io_list[i];
248 if ( p->is_enabled() ) {
249 p->dec_count_down( interval );
250 while ( p->get_count_down() < 0 ) {
252 p->dec_count_down( -1000000.0 / p->get_hz() );
259 // shutdown all I/O connections
260 void fgIOShutdownAll() {
263 // cout << "processing I/O channels" << endl;
265 for ( int i = 0; i < (int)global_io_list.size(); ++i ) {
266 // cout << " channel = " << i << endl;
267 p = global_io_list[i];
269 if ( p->is_enabled() ) {