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