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