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