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>
38 #include <simgear/misc/strutils.hxx>
40 #include <Network/protocol.hxx>
41 #include <Network/atc610x.hxx>
42 #include <Network/atlas.hxx>
43 #include <Network/garmin.hxx>
44 #include <Network/httpd.hxx>
46 # include <Network/jpg-httpd.hxx>
48 #include <Network/joyclient.hxx>
49 #include <Network/native.hxx>
50 #include <Network/native_ctrls.hxx>
51 #include <Network/native_fdm.hxx>
52 #include <Network/opengc.hxx>
53 #include <Network/nmea.hxx>
54 #include <Network/props.hxx>
55 #include <Network/pve.hxx>
56 #include <Network/ray.hxx>
57 #include <Network/rul.hxx>
59 #include "globals.hxx"
65 // define the global I/O channel list
66 io_container global_io_list;
69 // configure a port based on the config string
70 static FGProtocol *parse_port_config( const string& config )
72 SG_LOG( SG_IO, SG_INFO, "Parse I/O channel request: " << config );
74 vector<string> tokens = simgear::strutils::split( config, "," );
77 SG_LOG( SG_IO, SG_ALERT,
78 "Port configuration error: empty config string" );
82 string protocol = tokens[0];
83 SG_LOG( SG_IO, SG_INFO, " protocol = " << protocol );
89 if ( protocol == "atc610x" ) {
91 } else if ( protocol == "atlas" ) {
92 FGAtlas *atlas = new FGAtlas;
94 } else if ( protocol == "opengc" ) {
96 // printf("Parsed opengc\n"); cin >> wait;
97 FGOpenGC *opengc = new FGOpenGC;
99 } else if ( protocol == "garmin" ) {
100 FGGarmin *garmin = new FGGarmin;
102 } else if ( protocol == "httpd" ) {
104 string port = tokens[1];
105 return new FGHttpd( atoi(port.c_str()) );
106 #ifdef FG_JPEG_SERVER
107 } else if ( protocol == "jpg-httpd" ) {
109 string port = tokens[1];
110 return new FGJpegHttpd( atoi(port.c_str()) );
112 } else if ( protocol == "joyclient" ) {
113 FGJoyClient *joyclient = new FGJoyClient;
115 } else if ( protocol == "native" ) {
116 FGNative *native = new FGNative;
118 } else if ( protocol == "native_ctrls" ) {
119 FGNativeCtrls *native_ctrls = new FGNativeCtrls;
121 } else if ( protocol == "native_fdm" ) {
122 FGNativeFDM *native_fdm = new FGNativeFDM;
124 } else if ( protocol == "nmea" ) {
125 FGNMEA *nmea = new FGNMEA;
127 } else if ( protocol == "props" || protocol == "telnet" ) {
128 io = new FGProps( tokens );
130 } else if ( protocol == "pve" ) {
131 FGPVE *pve = new FGPVE;
133 } else if ( protocol == "ray" ) {
134 FGRAY *ray = new FGRAY;
136 } else if ( protocol == "rul" ) {
137 FGRUL *rul = new FGRUL;
143 catch (FGProtocolConfigError& err)
145 SG_LOG( SG_IO, SG_ALERT, "Port configuration error: " << err.what() );
150 string medium = tokens[1];
151 SG_LOG( SG_IO, SG_INFO, " medium = " << medium );
153 string direction = tokens[2];
154 io->set_direction( direction );
155 SG_LOG( SG_IO, SG_INFO, " direction = " << direction );
157 string hertz_str = tokens[3];
158 double hertz = atof( hertz_str.c_str() );
160 SG_LOG( SG_IO, SG_INFO, " hertz = " << hertz );
162 if ( medium == "serial" ) {
164 string device = tokens[4];
165 SG_LOG( SG_IO, SG_INFO, " device = " << device );
168 string baud = tokens[5];
169 SG_LOG( SG_IO, SG_INFO, " baud = " << baud );
171 SGSerial *ch = new SGSerial( device, baud );
172 io->set_io_channel( ch );
173 } else if ( medium == "file" ) {
175 string file = tokens[4];
176 SG_LOG( SG_IO, SG_INFO, " file name = " << file );
178 SGFile *ch = new SGFile( file );
179 io->set_io_channel( ch );
180 } else if ( medium == "socket" ) {
181 string hostname = tokens[4];
182 string port = tokens[5];
183 string style = tokens[6];
185 SG_LOG( SG_IO, SG_INFO, " hostname = " << hostname );
186 SG_LOG( SG_IO, SG_INFO, " port = " << port );
187 SG_LOG( SG_IO, SG_INFO, " style = " << style );
189 io->set_io_channel( new SGSocket( hostname, port, style ) );
196 // step through the port config streams (from fgOPTIONS) and setup
197 // serial port channels for each
199 // SG_LOG( SG_IO, SG_INFO, "I/O Channel initialization, " <<
200 // globals->get_channel_options_list()->size() << " requests." );
203 string_list *channel_options_list = globals->get_channel_options_list();
205 // we could almost do this in a single step except pushing a valid
206 // port onto the port list copies the structure and destroys the
207 // original, which closes the port and frees up the fd ... doh!!!
209 // parse the configuration strings and store the results in the
210 // appropriate FGIOChannel structures
211 for ( int i = 0; i < (int)channel_options_list->size(); ++i ) {
212 p = parse_port_config( (*channel_options_list)[i] );
215 global_io_list.push_back( p );
216 if ( !p->is_enabled() ) {
217 SG_LOG( SG_IO, SG_ALERT, "I/O Channel config failed." );
221 SG_LOG( SG_IO, SG_INFO, "I/O Channel parse failed." );
227 // process any serial port work
229 // cout << "processing I/O channels" << endl;
231 static int inited = 0;
233 static SGTimeStamp last;
242 interval = current - last;
246 for ( unsigned int i = 0; i < global_io_list.size(); ++i ) {
247 // cout << " channel = " << i << endl;
248 FGProtocol* p = global_io_list[i];
250 if ( p->is_enabled() ) {
251 p->dec_count_down( interval );
252 while ( p->get_count_down() < 0 ) {
254 p->dec_count_down(int( -1000000.0 / p->get_hz()));
261 // shutdown all I/O connections
262 void fgIOShutdownAll() {
265 // cout << "processing I/O channels" << endl;
267 for ( int i = 0; i < (int)global_io_list.size(); ++i ) {
268 // cout << " channel = " << i << endl;
269 p = global_io_list[i];
271 if ( p->is_enabled() ) {