]> git.mxchange.org Git - flightgear.git/blob - src/Main/fg_io.cxx
Added code to put aircraft at the end of the runway closest to the desired
[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 STL_STRING
27
28 #include <simgear/debug/logstream.hxx>
29 #include <simgear/io/iochannel.hxx>
30 #include <simgear/io/sg_file.hxx>
31 #include <simgear/io/sg_serial.hxx>
32 #include <simgear/io/sg_socket.hxx>
33
34 #include <simgear/math/fg_types.hxx>
35
36 #include <Main/options.hxx>
37
38 #include <Network/protocol.hxx>
39 #include <Network/native.hxx>
40 #include <Network/garmin.hxx>
41 #include <Network/nmea.hxx>
42 #include <Network/pve.hxx>
43 #include <Network/ray.hxx>
44 #include <Network/rul.hxx>
45 #include <Network/joyclient.hxx>
46
47 #include <Time/timestamp.hxx>
48
49 FG_USING_STD(string);
50
51
52 // define the global I/O channel list
53 io_container global_io_list;
54
55
56 // configure a port based on the config string
57 static FGProtocol *parse_port_config( const string& config )
58 {
59     string::size_type begin, end;
60
61     begin = 0;
62
63     FG_LOG( FG_IO, FG_INFO, "Parse I/O channel request: " << config );
64
65     // determine protocol
66     end = config.find(",", begin);
67     if ( end == string::npos ) {
68         return NULL;            // dummy
69     }
70     
71     string protocol = config.substr(begin, end - begin);
72     begin = end + 1;
73     FG_LOG( FG_IO, FG_INFO, "  protocol = " << protocol );
74
75     FGProtocol *io;
76     if ( protocol == "native" ) {
77         FGNative *native = new FGNative;
78         io = native;
79     } else if ( protocol == "garmin" ) {
80         FGGarmin *garmin = new FGGarmin;
81         io = garmin;
82     } else if ( protocol == "nmea" ) {
83         FGNMEA *nmea = new FGNMEA;
84         io = nmea;
85     } else if ( protocol == "pve" ) {
86         FGPVE *pve = new FGPVE;
87         io = pve;
88     } else if ( protocol == "ray" ) {
89         FGRAY *ray = new FGRAY;
90         io = ray;
91     } else if ( protocol == "rul" ) {
92         FGRUL *rul = new FGRUL;
93         io = rul;
94     } else if ( protocol == "joyclient" ) {
95         FGJoyClient *joyclient = new FGJoyClient;
96         io = joyclient;
97     } else {
98         return NULL;
99     }
100
101     // determine medium
102     end = config.find(",", begin);
103     if ( end == string::npos ) {
104         return NULL;            // dummy
105     }
106     
107     string medium = config.substr(begin, end - begin);
108     begin = end + 1;
109     FG_LOG( FG_IO, FG_INFO, "  medium = " << medium );
110
111     // determine direction
112     end = config.find(",", begin);
113     if ( end == string::npos ) {
114         return NULL;            // dummy
115     }
116     
117     string direction = config.substr(begin, end - begin);
118     begin = end + 1;
119     io->set_direction( direction );
120     FG_LOG( FG_IO, FG_INFO, "  direction = " << direction );
121
122     // determine hertz
123     end = config.find(",", begin);
124     if ( end == string::npos ) {
125         return NULL;            // dummy
126     }
127     
128     string hertz_str = config.substr(begin, end - begin);
129     begin = end + 1;
130     double hertz = atof( hertz_str.c_str() );
131     io->set_hz( hertz );
132     FG_LOG( FG_IO, FG_INFO, "  hertz = " << hertz );
133
134     if ( medium == "serial" ) {
135         // device name
136         end = config.find(",", begin);
137         if ( end == string::npos ) {
138             return NULL;
139         }
140     
141         string device = config.substr(begin, end - begin);
142         begin = end + 1;
143         FG_LOG( FG_IO, FG_INFO, "  device = " << device );
144
145         // baud
146         string baud = config.substr(begin);
147         FG_LOG( FG_IO, FG_INFO, "  baud = " << baud );
148
149         SGSerial *ch = new SGSerial( device, baud );
150         io->set_io_channel( ch );
151     } else if ( medium == "file" ) {
152         // file name
153         string file = config.substr(begin);
154         FG_LOG( FG_IO, FG_INFO, "  file name = " << file );
155
156         SGFile *ch = new SGFile( file );
157         io->set_io_channel( ch );
158     } else if ( medium == "socket" ) {
159         // hostname
160         end = config.find(",", begin);
161         if ( end == string::npos ) {
162             return NULL;
163         }
164     
165         string hostname = config.substr(begin, end - begin);
166         begin = end + 1;
167         FG_LOG( FG_IO, FG_INFO, "  hostname = " << hostname );
168
169         // port string
170         end = config.find(",", begin);
171         if ( end == string::npos ) {
172             return NULL;
173         }
174     
175         string port = config.substr(begin, end - begin);
176         begin = end + 1;
177         FG_LOG( FG_IO, FG_INFO, "  port string = " << port );
178
179         // socket style
180         string style_str = config.substr(begin);
181         FG_LOG( FG_IO, FG_INFO, "  style string = " << style_str );
182        
183         SGSocket *ch = new SGSocket( hostname, port, style_str );
184         io->set_io_channel( ch );
185     }
186
187     return io;
188 }
189
190
191 // step through the port config streams (from fgOPTIONS) and setup
192 // serial port channels for each
193 void fgIOInit() {
194     FGProtocol *p;
195     string_list channel_options_list = 
196         current_options.get_channel_options_list();
197
198     // we could almost do this in a single step except pushing a valid
199     // port onto the port list copies the structure and destroys the
200     // original, which closes the port and frees up the fd ... doh!!!
201
202     // parse the configuration strings and store the results in the
203     // appropriate FGIOChannel structures
204     for ( int i = 0; i < (int)channel_options_list.size(); ++i ) {
205         p = parse_port_config( channel_options_list[i] );
206         if ( p != NULL ) {
207             p->open();
208             global_io_list.push_back( p );
209             if ( !p->is_enabled() ) {
210                 FG_LOG( FG_IO, FG_INFO, "I/O Channel config failed." );
211             }
212         } else {
213             FG_LOG( FG_IO, FG_INFO, "I/O Channel parse failed." );
214         }
215     }
216 }
217
218
219 // process any serial port work
220 void fgIOProcess() {
221     FGProtocol *p;
222
223     // cout << "processing I/O channels" << endl;
224
225     static int inited = 0;
226     int interval;
227     static FGTimeStamp last;
228     FGTimeStamp current;
229
230     if ( ! inited ) {
231         inited = 1;
232         last.stamp();
233         interval = 0;
234     } else {
235         current.stamp();
236         interval = current - last;
237         last = current;
238     }
239
240     for ( int i = 0; i < (int)global_io_list.size(); ++i ) {
241         // cout << "  channel = " << i << endl;
242         p = global_io_list[i];
243
244         if ( p->is_enabled() ) {
245             p->dec_count_down( interval );
246             while ( p->get_count_down() < 0 ) {
247                 p->process();
248                 p->dec_count_down( -1000000.0 / p->get_hz() );
249             }
250         }
251     }
252 }