]> git.mxchange.org Git - flightgear.git/blob - src/Main/fg_io.cxx
Tweaks to protocol names to match recent changes in options.cxx (so they
[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 - curt@flightgear.org
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., 675 Mass Ave, Cambridge, MA 02139, USA.
20 //
21 // $Id$
22
23
24 #include <simgear/compiler.h>
25
26 #include <stdlib.h>             // atoi()
27
28 #include STL_STRING
29
30 #include <simgear/debug/logstream.hxx>
31 #include <simgear/io/iochannel.hxx>
32 #include <simgear/io/sg_file.hxx>
33 #include <simgear/io/sg_serial.hxx>
34 #include <simgear/io/sg_socket.hxx>
35 #include <simgear/io/sg_socket_udp.hxx>
36 #include <simgear/math/sg_types.hxx>
37 #include <simgear/timing/timestamp.hxx>
38 #include <simgear/misc/strutils.hxx>
39
40 #include <Network/protocol.hxx>
41 #include <Network/atc610x.hxx>
42 #include <Network/atlas.hxx>
43 #include <Network/garmin.hxx>
44 #include <Network/httpd.hxx>
45 #ifdef FG_JPEG_SERVER
46 #  include <Network/jpg-httpd.hxx>
47 #endif
48 #include <Network/joyclient.hxx>
49 #include <Network/native.hxx>
50 #include <Network/native_ctrls.hxx>
51 #include <Network/native_fdm.hxx>
52 #include <Network/native_gui.hxx>
53 #include <Network/opengc.hxx>
54 #include <Network/nmea.hxx>
55 #include <Network/props.hxx>
56 #include <Network/pve.hxx>
57 #include <Network/ray.hxx>
58 #include <Network/rul.hxx>
59
60 #include "globals.hxx"
61 #include "fg_io.hxx"
62
63 SG_USING_STD(string);
64
65
66 FGIO::FGIO()
67 {
68 }
69
70 #include STL_ALGORITHM
71 SG_USING_STD(for_each);
72
73 static void delete_ptr( FGProtocol* p ) { delete p; }
74
75 FGIO::~FGIO()
76 {
77     shutdown_all();
78     for_each( io_channels.begin(), io_channels.end(), delete_ptr );
79 }
80
81
82 // configure a port based on the config string
83 FGProtocol*
84 FGIO::parse_port_config( const string& config )
85 {
86     SG_LOG( SG_IO, SG_INFO, "Parse I/O channel request: " << config );
87
88     vector<string> tokens = simgear::strutils::split( config, "," );
89     if (tokens.empty())
90     {
91         SG_LOG( SG_IO, SG_ALERT,
92                 "Port configuration error: empty config string" );
93         return 0;
94     }
95
96     string protocol = tokens[0];
97     SG_LOG( SG_IO, SG_INFO, "  protocol = " << protocol );
98
99     FGProtocol *io = 0;
100
101     try
102     {
103         if ( protocol == "atc610x" ) {
104             FGATC610x *atc610x = new FGATC610x;
105             atc610x->set_hz( 30 );
106             return atc610x;
107         } else if ( protocol == "atlas" ) {
108             FGAtlas *atlas = new FGAtlas;
109             io = atlas;
110         } else if ( protocol == "opengc" ) {
111             // char wait;
112             // printf("Parsed opengc\n"); cin >> wait;
113             FGOpenGC *opengc = new FGOpenGC;
114             io = opengc;
115         } else if ( protocol == "garmin" ) {
116             FGGarmin *garmin = new FGGarmin;
117             io = garmin;
118         } else if ( protocol == "httpd" ) {
119             // determine port
120             string port = tokens[1];
121             return new FGHttpd( atoi(port.c_str()) );
122 #ifdef FG_JPEG_SERVER
123         } else if ( protocol == "jpg-httpd" ) {
124             // determine port
125             string port = tokens[1];
126             return new FGJpegHttpd( atoi(port.c_str()) );
127 #endif
128         } else if ( protocol == "joyclient" ) {
129             FGJoyClient *joyclient = new FGJoyClient;
130             io = joyclient;
131         } else if ( protocol == "native" ) {
132             FGNative *native = new FGNative;
133             io = native;
134         } else if ( protocol == "native-ctrls" ) {
135             FGNativeCtrls *native_ctrls = new FGNativeCtrls;
136             io = native_ctrls;
137         } else if ( protocol == "native-fdm" ) {
138             FGNativeFDM *native_fdm = new FGNativeFDM;
139             io = native_fdm;
140         } else if ( protocol == "native-gui" ) {
141             FGNativeGUI *net_gui = new FGNativeGUI;
142             io = net_gui;
143         } else if ( protocol == "nmea" ) {
144             FGNMEA *nmea = new FGNMEA;
145             io = nmea;
146         } else if ( protocol == "props" || protocol == "telnet" ) {
147             io = new FGProps( tokens );
148             return io;
149         } else if ( protocol == "pve" ) {
150             FGPVE *pve = new FGPVE;
151             io = pve;
152         } else if ( protocol == "ray" ) {
153             FGRAY *ray = new FGRAY;
154             io = ray;
155         } else if ( protocol == "rul" ) {
156             FGRUL *rul = new FGRUL;
157             io = rul;
158         } else {
159             return NULL;
160         }
161     }
162     catch (FGProtocolConfigError& err)
163     {
164         SG_LOG( SG_IO, SG_ALERT, "Port configuration error: " << err.what() );
165         delete io;
166         return 0;
167     }
168
169     string medium = tokens[1];
170     SG_LOG( SG_IO, SG_INFO, "  medium = " << medium );
171
172     string direction = tokens[2];
173     io->set_direction( direction );
174     SG_LOG( SG_IO, SG_INFO, "  direction = " << direction );
175
176     string hertz_str = tokens[3];
177     double hertz = atof( hertz_str.c_str() );
178     io->set_hz( hertz );
179     SG_LOG( SG_IO, SG_INFO, "  hertz = " << hertz );
180
181     if ( medium == "serial" ) {
182         // device name
183         string device = tokens[4];
184         SG_LOG( SG_IO, SG_INFO, "  device = " << device );
185
186         // baud
187         string baud = tokens[5];
188         SG_LOG( SG_IO, SG_INFO, "  baud = " << baud );
189
190         SGSerial *ch = new SGSerial( device, baud );
191         io->set_io_channel( ch );
192     } else if ( medium == "file" ) {
193         // file name
194         string file = tokens[4];
195         SG_LOG( SG_IO, SG_INFO, "  file name = " << file );
196
197         SGFile *ch = new SGFile( file );
198         io->set_io_channel( ch );
199     } else if ( medium == "socket" ) {
200         string hostname = tokens[4];
201         string port = tokens[5];
202         string style = tokens[6];
203
204         SG_LOG( SG_IO, SG_INFO, "  hostname = " << hostname );
205         SG_LOG( SG_IO, SG_INFO, "  port = " << port );
206         SG_LOG( SG_IO, SG_INFO, "  style = " << style );
207             
208         io->set_io_channel( new SGSocket( hostname, port, style ) );
209     }
210
211     return io;
212 }
213
214
215 // step through the port config streams (from fgOPTIONS) and setup
216 // serial port channels for each
217 void
218 FGIO::init()
219 {
220     // SG_LOG( SG_IO, SG_INFO, "I/O Channel initialization, " << 
221     //         globals->get_channel_options_list()->size() << " requests." );
222
223     FGProtocol *p;
224
225     // we could almost do this in a single step except pushing a valid
226     // port onto the port list copies the structure and destroys the
227     // original, which closes the port and frees up the fd ... doh!!!
228
229     // parse the configuration strings and store the results in the
230     // appropriate FGIOChannel structures
231     typedef vector<string> container;
232     container::iterator i = globals->get_channel_options_list()->begin();
233     container::iterator end = globals->get_channel_options_list()->end();
234     for (; i != end; ++i )
235     {
236         p = parse_port_config( *i );
237         if ( p != NULL ) {
238             p->open();
239             io_channels.push_back( p );
240             if ( !p->is_enabled() ) {
241                 SG_LOG( SG_IO, SG_ALERT, "I/O Channel config failed." );
242                 exit(-1);
243             }
244         } else {
245             SG_LOG( SG_IO, SG_INFO, "I/O Channel parse failed." );
246         }
247     }
248 }
249
250
251 // process any serial port work
252 void
253 FGIO::update( double delta_time_sec )
254 {
255     // cout << "processing I/O channels" << endl;
256
257     // SGTimeStamp current_time;
258     // current_time.stamp();
259     // static SGTimeStamp start_time = current_time;
260     // double elapsed_time = (current_time - start_time) / 1000000.0;
261     // cout << " Elapsed time = " << elapsed_time << endl;
262
263     typedef vector< FGProtocol* > container;
264     container::iterator i = io_channels.begin();
265     container::iterator end = io_channels.end();
266     for (; i != end; ++i ) {
267         FGProtocol* p = *i;
268
269         if ( p->is_enabled() ) {
270             p->dec_count_down( delta_time_sec );
271             double dt = 1 / p->get_hz();
272             while ( p->get_count_down() < 0.33 * dt ) {
273                 p->process();
274                 p->inc_count_down( dt );
275                 p->inc_count();
276             }
277             // double ave = elapsed_time / p->get_count();
278             // cout << "  ave rate = " << ave << endl;
279         }
280     }
281 }
282
283
284 void
285 FGIO::shutdown_all() {
286     FGProtocol *p;
287
288     // cout << "processing I/O channels" << endl;
289
290     typedef vector< FGProtocol* > container;
291     container::iterator i = io_channels.begin();
292     container::iterator end = io_channels.end();
293     for (; i != end; ++i )
294     {
295         p = *i;
296
297         if ( p->is_enabled() ) {
298             p->close();
299         }
300     }
301 }
302
303 void
304 FGIO::bind()
305 {
306 }
307
308 void
309 FGIO::unbind()
310 {
311 }
312