]> git.mxchange.org Git - flightgear.git/blob - src/Main/fg_io.cxx
Fixes to iochannel parsing which had gotten broke at some point.
[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 #include <simgear/math/sg_types.hxx>
34 #include <simgear/timing/timestamp.hxx>
35
36 #include <Network/protocol.hxx>
37 #include <Network/native.hxx>
38 #include <Network/garmin.hxx>
39 #include <Network/nmea.hxx>
40 #include <Network/props.hxx>
41 #include <Network/pve.hxx>
42 #include <Network/ray.hxx>
43 #include <Network/rul.hxx>
44 #include <Network/joyclient.hxx>
45
46 #include "globals.hxx"
47
48 FG_USING_STD(string);
49
50
51 // define the global I/O channel list
52 io_container global_io_list;
53
54
55 // configure a port based on the config string
56 static FGProtocol *parse_port_config( const string& config )
57 {
58     string::size_type begin, end;
59
60     begin = 0;
61
62     FG_LOG( FG_IO, FG_INFO, "Parse I/O channel request: " << config );
63
64     // determine protocol
65     end = config.find(",", begin);
66     if ( end == string::npos ) {
67         return NULL;            // dummy
68     }
69     
70     string protocol = config.substr(begin, end - begin);
71     begin = end + 1;
72     FG_LOG( FG_IO, FG_INFO, "  protocol = " << protocol );
73
74     FGProtocol *io;
75     if ( protocol == "native" ) {
76         FGNative *native = new FGNative;
77         io = native;
78     } else if ( protocol == "garmin" ) {
79         FGGarmin *garmin = new FGGarmin;
80         io = garmin;
81     } else if ( protocol == "nmea" ) {
82         FGNMEA *nmea = new FGNMEA;
83         io = nmea;
84     } else if ( protocol == "props" ) {
85         FGProps *props = new FGProps;
86         io = props;
87     } else if ( protocol == "pve" ) {
88         FGPVE *pve = new FGPVE;
89         io = pve;
90     } else if ( protocol == "ray" ) {
91         FGRAY *ray = new FGRAY;
92         io = ray;
93     } else if ( protocol == "rul" ) {
94         FGRUL *rul = new FGRUL;
95         io = rul;
96     } else if ( protocol == "joyclient" ) {
97         FGJoyClient *joyclient = new FGJoyClient;
98         io = joyclient;
99     } else {
100         return NULL;
101     }
102
103     // determine medium
104     end = config.find(",", begin);
105     if ( end == string::npos ) {
106         return NULL;            // dummy
107     }
108     
109     string medium = config.substr(begin, end - begin);
110     begin = end + 1;
111     FG_LOG( FG_IO, FG_INFO, "  medium = " << medium );
112
113     // determine direction
114     end = config.find(",", begin);
115     if ( end == string::npos ) {
116         return NULL;            // dummy
117     }
118     
119     string direction = config.substr(begin, end - begin);
120     begin = end + 1;
121     io->set_direction( direction );
122     FG_LOG( FG_IO, FG_INFO, "  direction = " << direction );
123
124     // determine hertz
125     end = config.find(",", begin);
126     if ( end == string::npos ) {
127         return NULL;            // dummy
128     }
129     
130     string hertz_str = config.substr(begin, end - begin);
131     begin = end + 1;
132     double hertz = atof( hertz_str.c_str() );
133     io->set_hz( hertz );
134     FG_LOG( FG_IO, FG_INFO, "  hertz = " << hertz );
135
136     if ( medium == "serial" ) {
137         // device name
138         end = config.find(",", begin);
139         if ( end == string::npos ) {
140             return NULL;
141         }
142     
143         string device = config.substr(begin, end - begin);
144         begin = end + 1;
145         FG_LOG( FG_IO, FG_INFO, "  device = " << device );
146
147         // baud
148         string baud = config.substr(begin);
149         FG_LOG( FG_IO, FG_INFO, "  baud = " << baud );
150
151         SGSerial *ch = new SGSerial( device, baud );
152         io->set_io_channel( ch );
153     } else if ( medium == "file" ) {
154         // file name
155         string file = config.substr(begin);
156         FG_LOG( FG_IO, FG_INFO, "  file name = " << file );
157
158         SGFile *ch = new SGFile( file );
159         io->set_io_channel( ch );
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     // FG_LOG( FG_IO, FG_INFO, "I/O Channel initialization, " << 
197     //         globals->get_channel_options_list()->size() << " requests." );
198
199     FGProtocol *p;
200     string_list *channel_options_list = globals->get_channel_options_list();
201
202     // we could almost do this in a single step except pushing a valid
203     // port onto the port list copies the structure and destroys the
204     // original, which closes the port and frees up the fd ... doh!!!
205
206     // parse the configuration strings and store the results in the
207     // appropriate FGIOChannel structures
208     for ( int i = 0; i < (int)channel_options_list->size(); ++i ) {
209         p = parse_port_config( (*channel_options_list)[i] );
210         if ( p != NULL ) {
211             p->open();
212             global_io_list.push_back( p );
213             if ( !p->is_enabled() ) {
214                 FG_LOG( FG_IO, FG_ALERT, "I/O Channel config failed." );
215                 exit(-1);
216             }
217         } else {
218             FG_LOG( FG_IO, FG_INFO, "I/O Channel parse failed." );
219         }
220     }
221 }
222
223
224 // process any serial port work
225 void fgIOProcess() {
226     FGProtocol *p;
227
228     // cout << "processing I/O channels" << endl;
229
230     static int inited = 0;
231     int interval;
232     static SGTimeStamp last;
233     SGTimeStamp current;
234
235     if ( ! inited ) {
236         inited = 1;
237         last.stamp();
238         interval = 0;
239     } else {
240         current.stamp();
241         interval = current - last;
242         last = current;
243     }
244
245     for ( int i = 0; i < (int)global_io_list.size(); ++i ) {
246         // cout << "  channel = " << i << endl;
247         p = global_io_list[i];
248
249         if ( p->is_enabled() ) {
250             p->dec_count_down( interval );
251             while ( p->get_count_down() < 0 ) {
252                 p->process();
253                 p->dec_count_down( -1000000.0 / p->get_hz() );
254             }
255         }
256     }
257 }
258
259
260 // shutdown all I/O connections
261 void fgIOShutdownAll() {
262     FGProtocol *p;
263
264     // cout << "processing I/O channels" << endl;
265
266     for ( int i = 0; i < (int)global_io_list.size(); ++i ) {
267         // cout << "  channel = " << i << endl;
268         p = global_io_list[i];
269
270         if ( p->is_enabled() ) {
271             p->close();
272         }
273     }
274 }