]> git.mxchange.org Git - flightgear.git/blob - src/Main/fg_io.cxx
Tweaks to go along with changes in SGSocket.
[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         SGSerial *ch = new SGSerial;
136         io->set_io_channel( ch );
137
138         // device name
139         end = config.find(",", begin);
140         if ( end == string::npos ) {
141             return NULL;
142         }
143     
144         ch->set_device( config.substr(begin, end - begin) );
145         begin = end + 1;
146         FG_LOG( FG_IO, FG_INFO, "  device = " << ch->get_device() );
147
148         // baud
149         ch->set_baud( config.substr(begin) );
150         FG_LOG( FG_IO, FG_INFO, "  baud = " << ch->get_baud() );
151
152         io->set_io_channel( ch );
153     } else if ( medium == "file" ) {
154         SGFile *ch = new SGFile;
155         io->set_io_channel( ch );
156
157         // file name
158         ch->set_file_name( config.substr(begin) );
159         FG_LOG( FG_IO, FG_INFO, "  file name = " << ch->get_file_name() );
160     } else if ( medium == "socket" ) {
161         // hostname
162         end = config.find(",", begin);
163         if ( end == string::npos ) {
164             return NULL;
165         }
166     
167         string hostname = config.substr(begin, end - begin);
168         begin = end + 1;
169         FG_LOG( FG_IO, FG_INFO, "  hostname = " << hostname );
170
171         // port string
172         end = config.find(",", begin);
173         if ( end == string::npos ) {
174             return NULL;
175         }
176     
177         string port = config.substr(begin, end - begin);
178         begin = end + 1;
179         FG_LOG( FG_IO, FG_INFO, "  port string = " << port );
180
181         // socket style
182         string style_str = config.substr(begin);
183         FG_LOG( FG_IO, FG_INFO, "  style string = " << style_str );
184        
185         SGSocket *ch = new SGSocket( hostname, port, style_str );
186         io->set_io_channel( ch );
187     }
188
189     return io;
190 }
191
192
193 // step through the port config streams (from fgOPTIONS) and setup
194 // serial port channels for each
195 void fgIOInit() {
196     FGProtocol *p;
197     string_list channel_options_list = 
198         current_options.get_channel_options_list();
199
200     // we could almost do this in a single step except pushing a valid
201     // port onto the port list copies the structure and destroys the
202     // original, which closes the port and frees up the fd ... doh!!!
203
204     // parse the configuration strings and store the results in the
205     // appropriate FGIOChannel structures
206     for ( int i = 0; i < (int)channel_options_list.size(); ++i ) {
207         p = parse_port_config( channel_options_list[i] );
208         if ( p != NULL ) {
209             p->open();
210             global_io_list.push_back( p );
211             if ( !p->is_enabled() ) {
212                 FG_LOG( FG_IO, FG_INFO, "I/O Channel config failed." );
213             }
214         } else {
215             FG_LOG( FG_IO, FG_INFO, "I/O Channel parse failed." );
216         }
217     }
218 }
219
220
221 // process any serial port work
222 void fgIOProcess() {
223     FGProtocol *p;
224
225     // cout << "processing I/O channels" << endl;
226
227     static int inited = 0;
228     int interval;
229     static FGTimeStamp last;
230     FGTimeStamp current;
231
232     if ( ! inited ) {
233         inited = 1;
234         last.stamp();
235         interval = 0;
236     } else {
237         current.stamp();
238         interval = current - last;
239         last = current;
240     }
241
242     for ( int i = 0; i < (int)global_io_list.size(); ++i ) {
243         // cout << "  channel = " << i << endl;
244         p = global_io_list[i];
245
246         if ( p->is_enabled() ) {
247             p->dec_count_down( interval );
248             while ( p->get_count_down() < 0 ) {
249                 p->process();
250                 p->dec_count_down( -1000000.0 / p->get_hz() );
251             }
252         }
253     }
254 }