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