]> git.mxchange.org Git - flightgear.git/blob - src/Main/fg_io.cxx
Linux needs stdarg.h for va_args
[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 <cstdlib>             // atoi()
30
31 #include <string>
32 #include <algorithm>
33
34 #include <simgear/debug/logstream.hxx>
35 #include <simgear/io/iochannel.hxx>
36 #include <simgear/io/sg_file.hxx>
37 #include <simgear/io/sg_serial.hxx>
38 #include <simgear/io/sg_socket.hxx>
39 #include <simgear/io/sg_socket_udp.hxx>
40 #include <simgear/math/sg_types.hxx>
41 #include <simgear/timing/timestamp.hxx>
42 #include <simgear/misc/strutils.hxx>
43
44 #include <Network/protocol.hxx>
45 #include <Network/ATC-Main.hxx>
46 #include <Network/atlas.hxx>
47 #include <Network/AV400.hxx>
48 #include <Network/AV400Sim.hxx>
49 #include <Network/AV400WSim.hxx>
50 #include <Network/garmin.hxx>
51 #include <Network/httpd.hxx>
52 #include <Network/igc.hxx>
53 #ifdef FG_JPEG_SERVER
54 #  include <Network/jpg-httpd.hxx>
55 #endif
56 #include <Network/joyclient.hxx>
57 #include <Network/jsclient.hxx>
58 #include <Network/native.hxx>
59 #include <Network/native_ctrls.hxx>
60 #include <Network/native_fdm.hxx>
61 #include <Network/native_gui.hxx>
62 #include <Network/opengc.hxx>
63 #include <Network/nmea.hxx>
64 #include <Network/props.hxx>
65 #include <Network/pve.hxx>
66 #include <Network/ray.hxx>
67 #include <Network/rul.hxx>
68 #include <Network/generic.hxx>
69
70 #if FG_HAVE_HLA
71 #include <Network/HLA/hla.hxx>
72 #endif
73
74 #include "globals.hxx"
75 #include "fg_io.hxx"
76
77 using std::atoi;
78 using std::string;
79
80
81 FGIO::FGIO()
82 {
83 }
84
85
86 FGIO::~FGIO()
87 {
88
89 }
90
91
92 // configure a port based on the config string
93 FGProtocol*
94 FGIO::parse_port_config( const string& config )
95 {
96     SG_LOG( SG_IO, SG_INFO, "Parse I/O channel request: " << config );
97
98     string_list tokens = simgear::strutils::split( config, "," );
99     if (tokens.empty())
100     {
101         SG_LOG( SG_IO, SG_ALERT,
102                 "Port configuration error: empty config string" );
103         return NULL;
104     }
105
106     string protocol = tokens[0];
107     SG_LOG( SG_IO, SG_INFO, "  protocol = " << protocol );
108
109     FGProtocol *io = NULL;
110
111     try
112     {
113         if ( protocol == "atcsim" ) {
114             FGATCMain *atcsim = new FGATCMain;
115             atcsim->set_hz( 30 );
116             if ( tokens.size() != 6 ) {
117                 SG_LOG( SG_IO, SG_ALERT, "Usage: --atcsim=[no-]pedals,"
118                         << "input0_config,input1_config,"
119                         << "output0_config,output1_config,file.nas" );
120                 delete atcsim;
121                 return NULL;
122             }
123             if ( tokens[1] == "no-pedals" ) {
124                 fgSetBool( "/input/atcsim/ignore-pedal-controls", true );
125             } else {
126                 fgSetBool( "/input/atcsim/ignore-pedal-controls", false );
127             }
128             atcsim->set_path_names(tokens[2], tokens[3], tokens[4], tokens[5]);
129             return atcsim;
130         } else if ( protocol == "atlas" ) {
131             FGAtlas *atlas = new FGAtlas;
132             io = atlas;
133         } else if ( protocol == "opengc" ) {
134             FGOpenGC *opengc = new FGOpenGC;
135             io = opengc;
136         } else if ( protocol == "AV400" ) {
137             FGAV400 *av400 = new FGAV400;
138             io = av400;
139         } else if ( protocol == "AV400Sim" ) {
140             FGAV400Sim *av400sim = new FGAV400Sim;
141             io = av400sim;
142         } else if ( protocol == "AV400WSimA" ) {
143             FGAV400WSimA *av400wsima = new FGAV400WSimA;
144             io = av400wsima;
145         } else if ( protocol == "AV400WSimB" ) {
146             FGAV400WSimB *av400wsimb = new FGAV400WSimB;
147             io = av400wsimb;
148         } else if ( protocol == "garmin" ) {
149             FGGarmin *garmin = new FGGarmin;
150             io = garmin;
151         } else if ( protocol == "igc" ) {
152             IGCProtocol *igc = new IGCProtocol;
153             io = igc;
154         } else if ( protocol == "httpd" ) {
155             // determine port
156             string port = tokens[1];
157             return new FGHttpd( atoi(port.c_str()) );
158 #ifdef FG_JPEG_SERVER
159         } else if ( protocol == "jpg-httpd" ) {
160             // determine port
161             string port = tokens[1];
162             return new FGJpegHttpd( atoi(port.c_str()) );
163 #endif
164         } else if ( protocol == "joyclient" ) {
165             FGJoyClient *joyclient = new FGJoyClient;
166             io = joyclient;
167         } else if ( protocol == "jsclient" ) {
168             FGJsClient *jsclient = new FGJsClient;
169             io = jsclient;
170         } else if ( protocol == "native" ) {
171             FGNative *native = new FGNative;
172             io = native;
173         } else if ( protocol == "native-ctrls" ) {
174             FGNativeCtrls *native_ctrls = new FGNativeCtrls;
175             io = native_ctrls;
176         } else if ( protocol == "native-fdm" ) {
177             FGNativeFDM *native_fdm = new FGNativeFDM;
178             io = native_fdm;
179         } else if ( protocol == "native-gui" ) {
180             FGNativeGUI *net_gui = new FGNativeGUI;
181             io = net_gui;
182         } else if ( protocol == "nmea" ) {
183             FGNMEA *nmea = new FGNMEA;
184             io = nmea;
185         } else if ( protocol == "props" || protocol == "telnet" ) {
186             io = new FGProps( tokens );
187             return io;
188         } else if ( protocol == "pve" ) {
189             FGPVE *pve = new FGPVE;
190             io = pve;
191         } else if ( protocol == "ray" ) {
192             FGRAY *ray = new FGRAY;
193             io = ray;
194         } else if ( protocol == "rul" ) {
195             FGRUL *rul = new FGRUL;
196             io = rul;
197         } else if ( protocol == "generic" ) {
198             FGGeneric *generic = new FGGeneric( tokens );
199             if (!generic->getInitOk())
200             {
201                 // failed to initialize (i.e. invalid configuration)
202                 delete generic;
203                 return NULL;
204             }
205             io = generic;
206         } else if ( protocol == "multiplay" ) {
207             if ( tokens.size() != 5 ) {
208                 SG_LOG( SG_IO, SG_ALERT, "Ignoring invalid --multiplay option "
209                         "(4 arguments expected: --multiplay=dir,hz,hostname,port)" );
210                 return NULL;
211             }
212             string dir = tokens[1];
213             int rate = atoi(tokens[2].c_str());
214             string host = tokens[3];
215
216             short port = atoi(tokens[4].c_str());
217
218             // multiplay used to be handled by an FGProtocol, but no longer. This code
219             // retains compatibility with existing command-line syntax
220             fgSetInt("/sim/multiplay/tx-rate-hz", rate);
221             if (dir == "in") {
222                 fgSetInt("/sim/multiplay/rxport", port);
223                 fgSetString("/sim/multiplay/rxhost", host.c_str());
224             } else if (dir == "out") {
225                 fgSetInt("/sim/multiplay/txport", port);
226                 fgSetString("/sim/multiplay/txhost", host.c_str());
227             }
228
229             return NULL;
230         }
231 #if FG_HAVE_HLA
232         else if ( protocol == "hla" ) {
233             return new FGHLA(tokens);
234         }
235         else if ( protocol == "hla-local" ) {
236             // This is just about to bring up some defaults
237             if (tokens.size() != 2) {
238                 SG_LOG( SG_IO, SG_ALERT, "Ignoring invalid --hla-local option "
239                         "(one argument expected: --hla-local=<federationname>" );
240                 return NULL;
241             }
242             tokens.insert(tokens.begin(), "");
243             tokens.insert(tokens.begin(), "60");
244             tokens.insert(tokens.begin(), "bi");
245             tokens.push_back("fg-local.xml");
246             return new FGHLA(tokens);
247         }
248 #endif
249         else {
250             return NULL;
251         }
252     }
253     catch (FGProtocolConfigError& err)
254     {
255         SG_LOG( SG_IO, SG_ALERT, "Port configuration error: " << err.what() );
256         delete io;
257         return NULL;
258     }
259
260     if (tokens.size() < 3) {
261         SG_LOG( SG_IO, SG_ALERT, "Incompatible number of network arguments.");
262         delete io;
263         return NULL;
264     }
265     string medium = tokens[1];
266     SG_LOG( SG_IO, SG_INFO, "  medium = " << medium );
267
268     string direction = tokens[2];
269     io->set_direction( direction );
270     SG_LOG( SG_IO, SG_INFO, "  direction = " << direction );
271
272     string hertz_str = tokens[3];
273     double hertz = atof( hertz_str.c_str() );
274     io->set_hz( hertz );
275     SG_LOG( SG_IO, SG_INFO, "  hertz = " << hertz );
276
277     if ( medium == "serial" ) {
278         if ( tokens.size() < 5) {
279             SG_LOG( SG_IO, SG_ALERT, "Incompatible number of arguments for serial communications.");
280             delete io;
281             return NULL;
282         }
283         // device name
284         string device = tokens[4];
285         SG_LOG( SG_IO, SG_INFO, "  device = " << device );
286
287         // baud
288         string baud = tokens[5];
289         SG_LOG( SG_IO, SG_INFO, "  baud = " << baud );
290
291
292         SGSerial *ch = new SGSerial( device, baud );
293         io->set_io_channel( ch );
294
295         if ( protocol == "AV400WSimB" ) {
296             if ( tokens.size() < 7 ) {
297                 SG_LOG( SG_IO, SG_ALERT, "Missing second hz for AV400WSimB.");
298                 delete io;
299                 return NULL;
300             }
301             FGAV400WSimB *fgavb = static_cast<FGAV400WSimB*>(io);
302             string hz2_str = tokens[6];
303             double hz2 = atof(hz2_str.c_str());
304             fgavb->set_hz2(hz2);
305         }
306     } else if ( medium == "file" ) {
307         // file name
308         if ( tokens.size() < 4) {
309             SG_LOG( SG_IO, SG_ALERT, "Incompatible number of arguments for file I/O.");
310             delete io;
311             return NULL;
312         }
313
314         string file = tokens[4];
315         SG_LOG( SG_IO, SG_INFO, "  file name = " << file );
316         int repeat = 1;
317         if (tokens.size() >= 7 && tokens[6] == "repeat") {
318             if (tokens.size() >= 8) {
319                 repeat = atoi(tokens[7].c_str());
320                 FGGeneric* generic = dynamic_cast<FGGeneric*>(io);
321                 if (generic)
322                     generic->setExitOnError(true);
323             } else {
324                 repeat = -1;
325             }
326         }
327         SGFile *ch = new SGFile( file, repeat );
328         io->set_io_channel( ch );
329     } else if ( medium == "socket" ) {
330         if ( tokens.size() < 6) {
331             SG_LOG( SG_IO, SG_ALERT, "Incompatible number of arguments for socket communications.");
332             delete io;
333             return NULL;
334         }
335         string hostname = tokens[4];
336         string port = tokens[5];
337         string style = tokens[6];
338
339         SG_LOG( SG_IO, SG_INFO, "  hostname = " << hostname );
340         SG_LOG( SG_IO, SG_INFO, "  port = " << port );
341         SG_LOG( SG_IO, SG_INFO, "  style = " << style );
342
343         io->set_io_channel( new SGSocket( hostname, port, style ) );
344     }
345
346     return io;
347 }
348
349
350 // step through the port config streams (from fgOPTIONS) and setup
351 // serial port channels for each
352 void
353 FGIO::init()
354 {
355     // SG_LOG( SG_IO, SG_INFO, "I/O Channel initialization, " <<
356     //         globals->get_channel_options_list()->size() << " requests." );
357
358     _realDeltaTime = fgGetNode("/sim/time/delta-realtime-sec");
359
360     // we could almost do this in a single step except pushing a valid
361     // port onto the port list copies the structure and destroys the
362     // original, which closes the port and frees up the fd ... doh!!!
363
364     string_list::iterator i = globals->get_channel_options_list()->begin();
365     string_list::iterator end = globals->get_channel_options_list()->end();
366     for (; i != end; ++i ) {
367         add_channel( *i );
368     } // of channel options iteration
369 }
370
371 // add another I/O channel
372 void FGIO::add_channel(const string& config)
373 {
374     // parse the configuration string and store the results in the
375     // appropriate FGIOChannel structure
376     FGProtocol *p = parse_port_config( config );
377     if (!p)
378     {
379         return;
380     }
381
382     p->open();
383     if ( !p->is_enabled() ) {
384         SG_LOG( SG_IO, SG_ALERT, "I/O Channel config failed." );
385         delete p;
386         return;
387     }
388
389     io_channels.push_back( p );
390 }
391
392 void
393 FGIO::reinit()
394 {
395 }
396
397 // process any IO channel work
398 void
399 FGIO::update( double /* delta_time_sec */ )
400 {
401     // use wall-clock, not simulation, delta time, so that network
402     // protocols update when the simulation is paused
403     // see http://code.google.com/p/flightgear-bugs/issues/detail?id=125
404     double delta_time_sec = _realDeltaTime->getDoubleValue();
405
406     ProtocolVec::iterator i = io_channels.begin();
407     ProtocolVec::iterator end = io_channels.end();
408     for (; i != end; ++i ) {
409         FGProtocol* p = *i;
410         if (!p->is_enabled()) {
411             continue;
412         }
413
414         p->dec_count_down( delta_time_sec );
415         double dt = 1 / p->get_hz();
416         if ( p->get_count_down() < 0.33 * dt ) {
417             p->process();
418             p->inc_count();
419             while ( p->get_count_down() < 0.33 * dt ) {
420                 p->inc_count_down( dt );
421             }
422         } // of channel processing
423     } // of io_channels iteration
424 }
425
426 void
427 FGIO::shutdown()
428 {
429     ProtocolVec::iterator i = io_channels.begin();
430     ProtocolVec::iterator end = io_channels.end();
431     for (; i != end; ++i )
432     {
433         FGProtocol *p = *i;
434         if ( p->is_enabled() ) {
435             p->close();
436         }
437
438         delete p;
439     }
440
441     io_channels.clear();
442 }
443
444 void
445 FGIO::bind()
446 {
447 }
448
449 void
450 FGIO::unbind()
451 {
452 }