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/atlas.hxx>
38 #include <Network/garmin.hxx>
39 #include <Network/joyclient.hxx>
40 #include <Network/native.hxx>
41 #include <Network/nmea.hxx>
42 #include <Network/props.hxx>
43 #include <Network/pve.hxx>
44 #include <Network/ray.hxx>
45 #include <Network/rul.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 SG_LOG( SG_IO, SG_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 SG_LOG( SG_IO, SG_INFO, " protocol = " << protocol );
76 if ( protocol == "atlas" ) {
77 FGAtlas *atlas = new FGAtlas;
79 } else if ( protocol == "garmin" ) {
80 FGGarmin *garmin = new FGGarmin;
82 } else if ( protocol == "joyclient" ) {
83 FGJoyClient *joyclient = new FGJoyClient;
85 } else if ( protocol == "native" ) {
86 FGNative *native = new FGNative;
88 } else if ( protocol == "nmea" ) {
89 FGNMEA *nmea = new FGNMEA;
91 } else if ( protocol == "props" ) {
92 FGProps *props = new FGProps;
94 } else if ( protocol == "pve" ) {
95 FGPVE *pve = new FGPVE;
97 } else if ( protocol == "ray" ) {
98 FGRAY *ray = new FGRAY;
100 } else if ( protocol == "rul" ) {
101 FGRUL *rul = new FGRUL;
108 end = config.find(",", begin);
109 if ( end == string::npos ) {
110 return NULL; // dummy
113 string medium = config.substr(begin, end - begin);
115 SG_LOG( SG_IO, SG_INFO, " medium = " << medium );
117 // determine direction
118 end = config.find(",", begin);
119 if ( end == string::npos ) {
120 return NULL; // dummy
123 string direction = config.substr(begin, end - begin);
125 io->set_direction( direction );
126 SG_LOG( SG_IO, SG_INFO, " direction = " << direction );
129 end = config.find(",", begin);
130 if ( end == string::npos ) {
131 return NULL; // dummy
134 string hertz_str = config.substr(begin, end - begin);
136 double hertz = atof( hertz_str.c_str() );
138 SG_LOG( SG_IO, SG_INFO, " hertz = " << hertz );
140 if ( medium == "serial" ) {
142 end = config.find(",", begin);
143 if ( end == string::npos ) {
147 string device = config.substr(begin, end - begin);
149 SG_LOG( SG_IO, SG_INFO, " device = " << device );
152 string baud = config.substr(begin);
153 SG_LOG( SG_IO, SG_INFO, " baud = " << baud );
155 SGSerial *ch = new SGSerial( device, baud );
156 io->set_io_channel( ch );
157 } else if ( medium == "file" ) {
159 string file = config.substr(begin);
160 SG_LOG( SG_IO, SG_INFO, " file name = " << file );
162 SGFile *ch = new SGFile( file );
163 io->set_io_channel( ch );
164 } else if ( medium == "socket" ) {
166 end = config.find(",", begin);
167 if ( end == string::npos ) {
171 string hostname = config.substr(begin, end - begin);
173 SG_LOG( SG_IO, SG_INFO, " hostname = " << hostname );
176 end = config.find(",", begin);
177 if ( end == string::npos ) {
181 string port = config.substr(begin, end - begin);
183 SG_LOG( SG_IO, SG_INFO, " port string = " << port );
186 string style_str = config.substr(begin);
187 SG_LOG( SG_IO, SG_INFO, " style string = " << style_str );
189 SGSocket *ch = new SGSocket( hostname, port, style_str );
190 io->set_io_channel( ch );
197 // step through the port config streams (from fgOPTIONS) and setup
198 // serial port channels for each
200 // SG_LOG( SG_IO, SG_INFO, "I/O Channel initialization, " <<
201 // globals->get_channel_options_list()->size() << " requests." );
204 string_list *channel_options_list = globals->get_channel_options_list();
206 // we could almost do this in a single step except pushing a valid
207 // port onto the port list copies the structure and destroys the
208 // original, which closes the port and frees up the fd ... doh!!!
210 // parse the configuration strings and store the results in the
211 // appropriate FGIOChannel structures
212 for ( int i = 0; i < (int)channel_options_list->size(); ++i ) {
213 p = parse_port_config( (*channel_options_list)[i] );
216 global_io_list.push_back( p );
217 if ( !p->is_enabled() ) {
218 SG_LOG( SG_IO, SG_ALERT, "I/O Channel config failed." );
222 SG_LOG( SG_IO, SG_INFO, "I/O Channel parse failed." );
228 // process any serial port work
232 // cout << "processing I/O channels" << endl;
234 static int inited = 0;
236 static SGTimeStamp last;
245 interval = current - last;
249 for ( int i = 0; i < (int)global_io_list.size(); ++i ) {
250 // cout << " channel = " << i << endl;
251 p = global_io_list[i];
253 if ( p->is_enabled() ) {
254 p->dec_count_down( interval );
255 while ( p->get_count_down() < 0 ) {
257 p->dec_count_down( -1000000.0 / p->get_hz() );
264 // shutdown all I/O connections
265 void fgIOShutdownAll() {
268 // cout << "processing I/O channels" << endl;
270 for ( int i = 0; i < (int)global_io_list.size(); ++i ) {
271 // cout << " channel = " << i << endl;
272 p = global_io_list[i];
274 if ( p->is_enabled() ) {