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>
26 #include <stdlib.h> // atoi()
30 #include <simgear/debug/logstream.hxx>
31 #include <simgear/io/iochannel.hxx>
32 #include <simgear/io/sg_file.hxx>
33 #include <simgear/io/sg_serial.hxx>
34 #include <simgear/io/sg_socket.hxx>
35 #include <simgear/io/sg_socket_udp.hxx>
36 #include <simgear/math/sg_types.hxx>
37 #include <simgear/timing/timestamp.hxx>
39 #include <Network/protocol.hxx>
40 #include <Network/atlas.hxx>
41 #include <Network/garmin.hxx>
42 #include <Network/httpd.hxx>
43 #include <Network/joyclient.hxx>
44 #include <Network/native.hxx>
45 #include <Network/native_ctrls.hxx>
46 #include <Network/native_fdm.hxx>
47 #include <Network/nmea.hxx>
48 #include <Network/props.hxx>
49 #include <Network/pve.hxx>
50 #include <Network/ray.hxx>
51 #include <Network/rul.hxx>
53 #include "globals.hxx"
58 // define the global I/O channel list
59 io_container global_io_list;
62 // configure a port based on the config string
63 static FGProtocol *parse_port_config( const string& config )
65 bool short_circuit = false;
67 string::size_type begin, end;
71 SG_LOG( SG_IO, SG_INFO, "Parse I/O channel request: " << config );
74 end = config.find(",", begin);
75 if ( end == string::npos ) {
79 string protocol = config.substr(begin, end - begin);
81 SG_LOG( SG_IO, SG_INFO, " protocol = " << protocol );
84 if ( protocol == "atlas" ) {
85 FGAtlas *atlas = new FGAtlas;
87 } else if ( protocol == "garmin" ) {
88 FGGarmin *garmin = new FGGarmin;
90 } else if ( protocol == "httpd" ) {
92 string port = config.substr(begin);
93 FGHttpd *httpd = new FGHttpd( atoi(port.c_str()) );
96 } else if ( protocol == "joyclient" ) {
97 FGJoyClient *joyclient = new FGJoyClient;
99 } else if ( protocol == "native" ) {
100 FGNative *native = new FGNative;
102 } else if ( protocol == "native_ctrls" ) {
103 FGNativeCtrls *native_ctrls = new FGNativeCtrls;
105 } else if ( protocol == "native_fdm" ) {
106 FGNativeFDM *native_fdm = new FGNativeFDM;
108 } else if ( protocol == "nmea" ) {
109 FGNMEA *nmea = new FGNMEA;
111 } else if ( protocol == "props" ) {
112 FGProps *props = new FGProps;
114 } else if ( protocol == "pve" ) {
115 FGPVE *pve = new FGPVE;
117 } else if ( protocol == "ray" ) {
118 FGRAY *ray = new FGRAY;
120 } else if ( protocol == "rul" ) {
121 FGRUL *rul = new FGRUL;
127 if ( ! short_circuit ) {
129 end = config.find(",", begin);
130 if ( end == string::npos ) {
131 return NULL; // dummy
134 string medium = config.substr(begin, end - begin);
136 SG_LOG( SG_IO, SG_INFO, " medium = " << medium );
138 // determine direction
139 end = config.find(",", begin);
140 if ( end == string::npos ) {
141 return NULL; // dummy
144 string direction = config.substr(begin, end - begin);
146 io->set_direction( direction );
147 SG_LOG( SG_IO, SG_INFO, " direction = " << direction );
150 end = config.find(",", begin);
151 if ( end == string::npos ) {
152 return NULL; // dummy
155 string hertz_str = config.substr(begin, end - begin);
157 double hertz = atof( hertz_str.c_str() );
159 SG_LOG( SG_IO, SG_INFO, " hertz = " << hertz );
161 if ( medium == "serial" ) {
163 end = config.find(",", begin);
164 if ( end == string::npos ) {
168 string device = config.substr(begin, end - begin);
170 SG_LOG( SG_IO, SG_INFO, " device = " << device );
173 string baud = config.substr(begin);
174 SG_LOG( SG_IO, SG_INFO, " baud = " << baud );
176 SGSerial *ch = new SGSerial( device, baud );
177 io->set_io_channel( ch );
178 } else if ( medium == "file" ) {
180 string file = config.substr(begin);
181 SG_LOG( SG_IO, SG_INFO, " file name = " << file );
183 SGFile *ch = new SGFile( file );
184 io->set_io_channel( ch );
185 } else if ( medium == "socket" ) {
187 end = config.find(",", begin);
188 if ( end == string::npos ) {
192 string hostname = config.substr(begin, end - begin);
194 SG_LOG( SG_IO, SG_INFO, " hostname = " << hostname );
197 end = config.find(",", begin);
198 if ( end == string::npos ) {
202 string port = config.substr(begin, end - begin);
204 SG_LOG( SG_IO, SG_INFO, " port string = " << port );
207 string style_str = config.substr(begin);
208 SG_LOG( SG_IO, SG_INFO, " style string = " << style_str );
210 SGSocket *ch = new SGSocket( hostname, port, style_str );
211 io->set_io_channel( ch );
219 // step through the port config streams (from fgOPTIONS) and setup
220 // serial port channels for each
222 // SG_LOG( SG_IO, SG_INFO, "I/O Channel initialization, " <<
223 // globals->get_channel_options_list()->size() << " requests." );
226 string_list *channel_options_list = globals->get_channel_options_list();
228 // we could almost do this in a single step except pushing a valid
229 // port onto the port list copies the structure and destroys the
230 // original, which closes the port and frees up the fd ... doh!!!
232 // parse the configuration strings and store the results in the
233 // appropriate FGIOChannel structures
234 for ( int i = 0; i < (int)channel_options_list->size(); ++i ) {
235 p = parse_port_config( (*channel_options_list)[i] );
238 global_io_list.push_back( p );
239 if ( !p->is_enabled() ) {
240 SG_LOG( SG_IO, SG_ALERT, "I/O Channel config failed." );
244 SG_LOG( SG_IO, SG_INFO, "I/O Channel parse failed." );
250 // process any serial port work
254 // cout << "processing I/O channels" << endl;
256 static int inited = 0;
258 static SGTimeStamp last;
267 interval = current - last;
271 for ( int i = 0; i < (int)global_io_list.size(); ++i ) {
272 // cout << " channel = " << i << endl;
273 p = global_io_list[i];
275 if ( p->is_enabled() ) {
276 p->dec_count_down( interval );
277 while ( p->get_count_down() < 0 ) {
279 p->dec_count_down( -1000000.0 / p->get_hz() );
286 // shutdown all I/O connections
287 void fgIOShutdownAll() {
290 // cout << "processing I/O channels" << endl;
292 for ( int i = 0; i < (int)global_io_list.size(); ++i ) {
293 // cout << " channel = " << i << endl;
294 p = global_io_list[i];
296 if ( p->is_enabled() ) {