]> git.mxchange.org Git - flightgear.git/blob - src/Main/fg_io.cxx
b3304d651bbbce61f1edb333a1bfafc3bdea8c2c
[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
35 #include <Main/options.hxx>
36
37 #include <Network/protocol.hxx>
38 #include <Network/native.hxx>
39 #include <Network/garmin.hxx>
40 #include <Network/nmea.hxx>
41 #include <Network/props.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 == "props" ) {
86         FGProps *props = new FGProps;
87         io = props;
88     } else if ( protocol == "pve" ) {
89         FGPVE *pve = new FGPVE;
90         io = pve;
91     } else if ( protocol == "ray" ) {
92         FGRAY *ray = new FGRAY;
93         io = ray;
94     } else if ( protocol == "rul" ) {
95         FGRUL *rul = new FGRUL;
96         io = rul;
97     } else if ( protocol == "joyclient" ) {
98         FGJoyClient *joyclient = new FGJoyClient;
99         io = joyclient;
100     } else {
101         return NULL;
102     }
103
104     // determine medium
105     end = config.find(",", begin);
106     if ( end == string::npos ) {
107         return NULL;            // dummy
108     }
109     
110     string medium = config.substr(begin, end - begin);
111     begin = end + 1;
112     FG_LOG( FG_IO, FG_INFO, "  medium = " << medium );
113
114     // determine direction
115     end = config.find(",", begin);
116     if ( end == string::npos ) {
117         return NULL;            // dummy
118     }
119     
120     string direction = config.substr(begin, end - begin);
121     begin = end + 1;
122     io->set_direction( direction );
123     FG_LOG( FG_IO, FG_INFO, "  direction = " << direction );
124
125     // determine hertz
126     end = config.find(",", begin);
127     if ( end == string::npos ) {
128         return NULL;            // dummy
129     }
130     
131     string hertz_str = config.substr(begin, end - begin);
132     begin = end + 1;
133     double hertz = atof( hertz_str.c_str() );
134     io->set_hz( hertz );
135     FG_LOG( FG_IO, FG_INFO, "  hertz = " << hertz );
136
137     if ( medium == "serial" ) {
138         // device name
139         end = config.find(",", begin);
140         if ( end == string::npos ) {
141             return NULL;
142         }
143     
144         string device = config.substr(begin, end - begin);
145         begin = end + 1;
146         FG_LOG( FG_IO, FG_INFO, "  device = " << device );
147
148         // baud
149         string baud = config.substr(begin);
150         FG_LOG( FG_IO, FG_INFO, "  baud = " << baud );
151
152         SGSerial *ch = new SGSerial( device, baud );
153         io->set_io_channel( ch );
154     } else if ( medium == "file" ) {
155         // file name
156         string file = config.substr(begin);
157         FG_LOG( FG_IO, FG_INFO, "  file name = " << file );
158
159         SGFile *ch = new SGFile( file );
160         io->set_io_channel( ch );
161     } else if ( medium == "socket" ) {
162         // hostname
163         end = config.find(",", begin);
164         if ( end == string::npos ) {
165             return NULL;
166         }
167     
168         string hostname = config.substr(begin, end - begin);
169         begin = end + 1;
170         FG_LOG( FG_IO, FG_INFO, "  hostname = " << hostname );
171
172         // port string
173         end = config.find(",", begin);
174         if ( end == string::npos ) {
175             return NULL;
176         }
177     
178         string port = config.substr(begin, end - begin);
179         begin = end + 1;
180         FG_LOG( FG_IO, FG_INFO, "  port string = " << port );
181
182         // socket style
183         string style_str = config.substr(begin);
184         FG_LOG( FG_IO, FG_INFO, "  style string = " << style_str );
185        
186         SGSocket *ch = new SGSocket( hostname, port, style_str );
187         io->set_io_channel( ch );
188     }
189
190     return io;
191 }
192
193
194 // step through the port config streams (from fgOPTIONS) and setup
195 // serial port channels for each
196 void fgIOInit() {
197     FGProtocol *p;
198     string_list channel_options_list = 
199         current_options.get_channel_options_list();
200
201     // we could almost do this in a single step except pushing a valid
202     // port onto the port list copies the structure and destroys the
203     // original, which closes the port and frees up the fd ... doh!!!
204
205     // parse the configuration strings and store the results in the
206     // appropriate FGIOChannel structures
207     for ( int i = 0; i < (int)channel_options_list.size(); ++i ) {
208         p = parse_port_config( channel_options_list[i] );
209         if ( p != NULL ) {
210             p->open();
211             global_io_list.push_back( p );
212             if ( !p->is_enabled() ) {
213                 FG_LOG( FG_IO, FG_INFO, "I/O Channel config failed." );
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 FGTimeStamp last;
231     FGTimeStamp 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 }