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