]> git.mxchange.org Git - flightgear.git/blob - src/Main/fg_io.cxx
Add an initial implementation of the Garmin 400 series "Aviation In" data
[flightgear.git] / src / Main / fg_io.cxx
1 // fg_io.cxx -- higher level I/O channel management routines
2 //
3 // Written by Curtis Olson, started November 1999.
4 //
5 // Copyright (C) 1999  Curtis L. Olson - http://www.flightgear.org/~curt
6 //
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.
11 //
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.
16 //
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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 //
21 // $Id$
22
23 #ifdef HAVE_CONFIG_H
24 #  include "config.h"
25 #endif
26
27 #include <simgear/compiler.h>
28
29 #include <stdlib.h>             // atoi()
30
31 #include STL_STRING
32
33 #include <simgear/debug/logstream.hxx>
34 #include <simgear/io/iochannel.hxx>
35 #include <simgear/io/sg_file.hxx>
36 #include <simgear/io/sg_serial.hxx>
37 #include <simgear/io/sg_socket.hxx>
38 #include <simgear/io/sg_socket_udp.hxx>
39 #include <simgear/math/sg_types.hxx>
40 #include <simgear/timing/timestamp.hxx>
41 #include <simgear/misc/strutils.hxx>
42
43 #include <Network/protocol.hxx>
44 #include <Network/ATC-Main.hxx>
45 #include <Network/atlas.hxx>
46 #include <Network/AV400.hxx>
47 #include <Network/garmin.hxx>
48 #include <Network/httpd.hxx>
49 #ifdef FG_JPEG_SERVER
50 #  include <Network/jpg-httpd.hxx>
51 #endif
52 #include <Network/joyclient.hxx>
53 #include <Network/jsclient.hxx>
54 #include <Network/native.hxx>
55 #include <Network/native_ctrls.hxx>
56 #include <Network/native_fdm.hxx>
57 #include <Network/native_gui.hxx>
58 #include <Network/opengc.hxx>
59 #include <Network/nmea.hxx>
60 #include <Network/props.hxx>
61 #include <Network/pve.hxx>
62 #include <Network/ray.hxx>
63 #include <Network/rul.hxx>
64 #include <Network/generic.hxx>
65 #include <Network/multiplay.hxx>
66
67 #include "globals.hxx"
68 #include "fg_io.hxx"
69
70 SG_USING_STD(string);
71
72
73 FGIO::FGIO()
74 {
75 }
76
77 #include STL_ALGORITHM
78 SG_USING_STD(for_each);
79
80 static void delete_ptr( FGProtocol* p ) { delete p; }
81
82 FGIO::~FGIO()
83 {
84     shutdown_all();
85     for_each( io_channels.begin(), io_channels.end(), delete_ptr );
86 }
87
88
89 // configure a port based on the config string
90 FGProtocol*
91 FGIO::parse_port_config( const string& config )
92 {
93     SG_LOG( SG_IO, SG_INFO, "Parse I/O channel request: " << config );
94
95     vector<string> tokens = simgear::strutils::split( config, "," );
96     if (tokens.empty())
97     {
98         SG_LOG( SG_IO, SG_ALERT,
99                 "Port configuration error: empty config string" );
100         return 0;
101     }
102
103     string protocol = tokens[0];
104     SG_LOG( SG_IO, SG_INFO, "  protocol = " << protocol );
105
106     FGProtocol *io = 0;
107
108     try
109     {
110         if ( protocol == "atcsim" ) {
111             FGATCMain *atcsim = new FGATCMain;
112             atcsim->set_hz( 30 );
113             if ( tokens.size() != 6 ) {
114                 SG_LOG( SG_IO, SG_ALERT, "Usage: --atcsim=[no-]pedals,"
115                         << "input0_config,input1_config,"
116                         << "output0_config,output1_config,file.nas" );
117                 return NULL;
118             }
119             if ( tokens[1] == "no-pedals" ) {
120                 fgSetBool( "/input/atcsim/ignore-pedal-controls", true );
121             } else {
122                 fgSetBool( "/input/atcsim/ignore-pedal-controls", false );
123             }
124             atcsim->set_path_names(tokens[2], tokens[3], tokens[4], tokens[5]);
125             return atcsim;
126         } else if ( protocol == "atlas" ) {
127             FGAtlas *atlas = new FGAtlas;
128             io = atlas;
129         } else if ( protocol == "opengc" ) {
130             // char wait;
131             // printf("Parsed opengc\n"); cin >> wait;
132             FGOpenGC *opengc = new FGOpenGC;
133             io = opengc;
134         } else if ( protocol == "AV400" ) {
135             FGAV400 *av400 = new FGAV400;
136             io = av400;
137         } else if ( protocol == "garmin" ) {
138             FGGarmin *garmin = new FGGarmin;
139             io = garmin;
140         } else if ( protocol == "httpd" ) {
141             // determine port
142             string port = tokens[1];
143             return new FGHttpd( atoi(port.c_str()) );
144 #ifdef FG_JPEG_SERVER
145         } else if ( protocol == "jpg-httpd" ) {
146             // determine port
147             string port = tokens[1];
148             return new FGJpegHttpd( atoi(port.c_str()) );
149 #endif
150         } else if ( protocol == "joyclient" ) {
151             FGJoyClient *joyclient = new FGJoyClient;
152             io = joyclient;
153         } else if ( protocol == "jsclient" ) {
154             FGJsClient *jsclient = new FGJsClient;
155             io = jsclient;
156         } else if ( protocol == "native" ) {
157             FGNative *native = new FGNative;
158             io = native;
159         } else if ( protocol == "native-ctrls" ) {
160             FGNativeCtrls *native_ctrls = new FGNativeCtrls;
161             io = native_ctrls;
162         } else if ( protocol == "native-fdm" ) {
163             FGNativeFDM *native_fdm = new FGNativeFDM;
164             io = native_fdm;
165         } else if ( protocol == "native-gui" ) {
166             FGNativeGUI *net_gui = new FGNativeGUI;
167             io = net_gui;
168         } else if ( protocol == "nmea" ) {
169             FGNMEA *nmea = new FGNMEA;
170             io = nmea;
171         } else if ( protocol == "props" || protocol == "telnet" ) {
172             io = new FGProps( tokens );
173             return io;
174         } else if ( protocol == "pve" ) {
175             FGPVE *pve = new FGPVE;
176             io = pve;
177         } else if ( protocol == "ray" ) {
178             FGRAY *ray = new FGRAY;
179             io = ray;
180         } else if ( protocol == "rul" ) {
181             FGRUL *rul = new FGRUL;
182             io = rul;
183         } else if ( protocol == "generic" ) {
184             int n = 6;
185             if (tokens[1] == "socket")  n++;
186             else if (tokens[1] == "file") n--;
187             FGGeneric *generic = new FGGeneric( tokens[n] );
188             io = generic;
189         } else if ( protocol == "multiplay" ) {\
190             //Determine dir, rate, host & port
191             string dir = tokens[1];
192             string rate = tokens[2];
193             string host = tokens[3];
194             string port = tokens[4];
195             return new FGMultiplay(dir, atoi(rate.c_str()), host, atoi(port.c_str()));
196         } else {
197             return NULL;
198         }
199     }
200     catch (FGProtocolConfigError& err)
201     {
202         SG_LOG( SG_IO, SG_ALERT, "Port configuration error: " << err.what() );
203         delete io;
204         return 0;
205     }
206     
207     if (tokens.size() < 3) {
208       SG_LOG( SG_IO, SG_ALERT, "Incompatible number of network arguments.");
209       return NULL;
210     }
211     string medium = tokens[1];
212     SG_LOG( SG_IO, SG_INFO, "  medium = " << medium );
213
214     string direction = tokens[2];
215     io->set_direction( direction );
216     SG_LOG( SG_IO, SG_INFO, "  direction = " << direction );
217
218     string hertz_str = tokens[3];
219     double hertz = atof( hertz_str.c_str() );
220     io->set_hz( hertz );
221     SG_LOG( SG_IO, SG_INFO, "  hertz = " << hertz );
222
223     if ( medium == "serial" ) {
224         if ( tokens.size() < 5) {
225           SG_LOG( SG_IO, SG_ALERT, "Incompatible number of arguments for serial communications.");
226           return NULL;
227         }
228         // device name
229         string device = tokens[4];
230         SG_LOG( SG_IO, SG_INFO, "  device = " << device );
231
232         // baud
233         string baud = tokens[5];
234         SG_LOG( SG_IO, SG_INFO, "  baud = " << baud );
235
236         SGSerial *ch = new SGSerial( device, baud );
237         io->set_io_channel( ch );
238     } else if ( medium == "file" ) {
239         // file name
240         if ( tokens.size() < 4) {
241           SG_LOG( SG_IO, SG_ALERT, "Incompatible number of arguments for file I/O.");
242           return NULL;
243         }
244           
245         string file = tokens[4];
246         SG_LOG( SG_IO, SG_INFO, "  file name = " << file );
247
248         SGFile *ch = new SGFile( file );
249         io->set_io_channel( ch );
250     } else if ( medium == "socket" ) {
251         if ( tokens.size() < 6) {
252           SG_LOG( SG_IO, SG_ALERT, "Incompatible number of arguments for socket communications.");
253           return NULL;
254         }
255         string hostname = tokens[4];
256         string port = tokens[5];
257         string style = tokens[6];
258
259         SG_LOG( SG_IO, SG_INFO, "  hostname = " << hostname );
260         SG_LOG( SG_IO, SG_INFO, "  port = " << port );
261         SG_LOG( SG_IO, SG_INFO, "  style = " << style );
262
263         io->set_io_channel( new SGSocket( hostname, port, style ) );
264     }
265
266     return io;
267 }
268
269
270 // step through the port config streams (from fgOPTIONS) and setup
271 // serial port channels for each
272 void
273 FGIO::init()
274 {
275     // SG_LOG( SG_IO, SG_INFO, "I/O Channel initialization, " <<
276     //         globals->get_channel_options_list()->size() << " requests." );
277
278     FGProtocol *p;
279
280     // we could almost do this in a single step except pushing a valid
281     // port onto the port list copies the structure and destroys the
282     // original, which closes the port and frees up the fd ... doh!!!
283
284     // parse the configuration strings and store the results in the
285     // appropriate FGIOChannel structures
286     typedef vector<string> container;
287     container::iterator i = globals->get_channel_options_list()->begin();
288     container::iterator end = globals->get_channel_options_list()->end();
289     for (; i != end; ++i )
290     {
291         p = parse_port_config( *i );
292         if ( p != NULL ) {
293             p->open();
294             io_channels.push_back( p );
295             if ( !p->is_enabled() ) {
296                 SG_LOG( SG_IO, SG_ALERT, "I/O Channel config failed." );
297                 exit(-1);
298             }
299         } else {
300             SG_LOG( SG_IO, SG_INFO, "I/O Channel parse failed." );
301         }
302     }
303 }
304
305
306 // process any IO channel work
307 void
308 FGIO::update( double delta_time_sec )
309 {
310     // cout << "processing I/O channels" << endl;
311     // cout << "  Elapsed time = " << delta_time_sec << endl;
312
313     typedef vector< FGProtocol* > container;
314     container::iterator i = io_channels.begin();
315     container::iterator end = io_channels.end();
316     for (; i != end; ++i ) {
317         FGProtocol* p = *i;
318
319         if ( p->is_enabled() ) {
320             p->dec_count_down( delta_time_sec );
321             double dt = 1 / p->get_hz();
322             if ( p->get_count_down() < 0.33 * dt ) {
323               p->process();
324               p->inc_count();
325               while ( p->get_count_down() < 0.33 * dt ) {
326                 p->inc_count_down( dt );
327               }
328               // double ave = elapsed_time / p->get_count();
329               // cout << "  ave rate = " << ave << endl;
330             }
331         }
332     }
333 }
334
335
336 void
337 FGIO::shutdown_all() {
338     FGProtocol *p;
339
340     // cout << "shutting down all I/O channels" << endl;
341
342     typedef vector< FGProtocol* > container;
343     container::iterator i = io_channels.begin();
344     container::iterator end = io_channels.end();
345     for (; i != end; ++i )
346     {
347         p = *i;
348
349         if ( p->is_enabled() ) {
350             p->close();
351         }
352     }
353 }
354
355 void
356 FGIO::bind()
357 {
358 }
359
360 void
361 FGIO::unbind()
362 {
363 }
364