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