]> git.mxchange.org Git - flightgear.git/blob - src/Main/fg_io.cxx
Minor tweaks.
[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     FGProtocol *p;
197     string_list channel_options_list = 
198         globals->get_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_ALERT, "I/O Channel config failed." );
213                 exit(-1);
214             }
215         } else {
216             FG_LOG( FG_IO, FG_INFO, "I/O Channel parse failed." );
217         }
218     }
219 }
220
221
222 // process any serial port work
223 void fgIOProcess() {
224     FGProtocol *p;
225
226     // cout << "processing I/O channels" << endl;
227
228     static int inited = 0;
229     int interval;
230     static SGTimeStamp last;
231     SGTimeStamp current;
232
233     if ( ! inited ) {
234         inited = 1;
235         last.stamp();
236         interval = 0;
237     } else {
238         current.stamp();
239         interval = current - last;
240         last = current;
241     }
242
243     for ( int i = 0; i < (int)global_io_list.size(); ++i ) {
244         // cout << "  channel = " << i << endl;
245         p = global_io_list[i];
246
247         if ( p->is_enabled() ) {
248             p->dec_count_down( interval );
249             while ( p->get_count_down() < 0 ) {
250                 p->process();
251                 p->dec_count_down( -1000000.0 / p->get_hz() );
252             }
253         }
254     }
255 }
256
257
258 // shutdown all I/O connections
259 void fgIOShutdownAll() {
260     FGProtocol *p;
261
262     // cout << "processing I/O channels" << endl;
263
264     for ( int i = 0; i < (int)global_io_list.size(); ++i ) {
265         // cout << "  channel = " << i << endl;
266         p = global_io_list[i];
267
268         if ( p->is_enabled() ) {
269             p->close();
270         }
271     }
272 }