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/telnet.hxx>
56 #include <Network/pve.hxx>
57 #include <Network/ray.hxx>
58 #include <Network/rul.hxx>
60 #include "globals.hxx"
62 SG_USING_NAMESPACE(std);
66 // define the global I/O channel list
67 io_container global_io_list;
70 // configure a port based on the config string
71 static FGProtocol *parse_port_config( const string& config )
73 SG_LOG( SG_IO, SG_INFO, "Parse I/O channel request: " << config );
75 vector<string> tokens = simgear::strutils::split( config, "," );
78 SG_LOG( SG_IO, SG_ALERT,
79 "Port configuration error: empty config string" );
83 string protocol = tokens[0];
84 SG_LOG( SG_IO, SG_INFO, " protocol = " << protocol );
90 if ( protocol == "atc610x" ) {
92 } else if ( protocol == "atlas" ) {
93 FGAtlas *atlas = new FGAtlas;
95 } else if ( protocol == "opengc" ) {
97 // printf("Parsed opengc\n"); cin >> wait;
98 FGOpenGC *opengc = new FGOpenGC;
100 } else if ( protocol == "garmin" ) {
101 FGGarmin *garmin = new FGGarmin;
103 } else if ( protocol == "httpd" ) {
105 string port = tokens[1];
106 return new FGHttpd( atoi(port.c_str()) );
107 #ifdef FG_JPEG_SERVER
108 } else if ( protocol == "jpg-httpd" ) {
110 string port = tokens[1];
111 return new FGJpegHttpd( atoi(port.c_str()) );
113 } else if ( protocol == "joyclient" ) {
114 FGJoyClient *joyclient = new FGJoyClient;
116 } else if ( protocol == "native" ) {
117 FGNative *native = new FGNative;
119 } else if ( protocol == "native_ctrls" ) {
120 FGNativeCtrls *native_ctrls = new FGNativeCtrls;
122 } else if ( protocol == "native_fdm" ) {
123 FGNativeFDM *native_fdm = new FGNativeFDM;
125 } else if ( protocol == "nmea" ) {
126 FGNMEA *nmea = new FGNMEA;
128 } else if ( protocol == "props" ) {
130 } else if ( protocol == "telnet" ) {
131 io = new FGTelnet( tokens );
133 } else if ( protocol == "pve" ) {
134 FGPVE *pve = new FGPVE;
136 } else if ( protocol == "ray" ) {
137 FGRAY *ray = new FGRAY;
139 } else if ( protocol == "rul" ) {
140 FGRUL *rul = new FGRUL;
146 catch (FGProtocolConfigError& err)
148 SG_LOG( SG_IO, SG_ALERT, "Port configuration error: " << err.what() );
153 string medium = tokens[1];
154 SG_LOG( SG_IO, SG_INFO, " medium = " << medium );
156 string direction = tokens[2];
157 io->set_direction( direction );
158 SG_LOG( SG_IO, SG_INFO, " direction = " << direction );
160 string hertz_str = tokens[3];
161 double hertz = atof( hertz_str.c_str() );
163 SG_LOG( SG_IO, SG_INFO, " hertz = " << hertz );
165 if ( medium == "serial" ) {
167 string device = tokens[4];
168 SG_LOG( SG_IO, SG_INFO, " device = " << device );
171 string baud = tokens[5];
172 SG_LOG( SG_IO, SG_INFO, " baud = " << baud );
174 SGSerial *ch = new SGSerial( device, baud );
175 io->set_io_channel( ch );
176 } else if ( medium == "file" ) {
178 string file = tokens[4];
179 SG_LOG( SG_IO, SG_INFO, " file name = " << file );
181 SGFile *ch = new SGFile( file );
182 io->set_io_channel( ch );
183 } else if ( medium == "socket" ) {
184 string hostname = tokens[4];
185 string port = tokens[5];
186 string style = tokens[6];
188 SG_LOG( SG_IO, SG_INFO, " hostname = " << hostname );
189 SG_LOG( SG_IO, SG_INFO, " port = " << port );
190 SG_LOG( SG_IO, SG_INFO, " style = " << style );
192 io->set_io_channel( new SGSocket( hostname, port, style ) );
199 // step through the port config streams (from fgOPTIONS) and setup
200 // serial port channels for each
202 // SG_LOG( SG_IO, SG_INFO, "I/O Channel initialization, " <<
203 // globals->get_channel_options_list()->size() << " requests." );
206 string_list *channel_options_list = globals->get_channel_options_list();
208 // we could almost do this in a single step except pushing a valid
209 // port onto the port list copies the structure and destroys the
210 // original, which closes the port and frees up the fd ... doh!!!
212 // parse the configuration strings and store the results in the
213 // appropriate FGIOChannel structures
214 for ( int i = 0; i < (int)channel_options_list->size(); ++i ) {
215 p = parse_port_config( (*channel_options_list)[i] );
218 global_io_list.push_back( p );
219 if ( !p->is_enabled() ) {
220 SG_LOG( SG_IO, SG_ALERT, "I/O Channel config failed." );
224 SG_LOG( SG_IO, SG_INFO, "I/O Channel parse failed." );
230 // 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 ( unsigned int i = 0; i < global_io_list.size(); ++i ) {
250 // cout << " channel = " << i << endl;
251 FGProtocol* 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(int( -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() ) {