]> git.mxchange.org Git - flightgear.git/blob - src/Main/fg_io.cxx
Renamed fg_types.hxx -> sg_types.hxx
[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/pve.hxx>
42 #include <Network/ray.hxx>
43 #include <Network/rul.hxx>
44 #include <Network/joyclient.hxx>
45
46 #include <Time/timestamp.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 == "pve" ) {
85         FGPVE *pve = new FGPVE;
86         io = pve;
87     } else if ( protocol == "ray" ) {
88         FGRAY *ray = new FGRAY;
89         io = ray;
90     } else if ( protocol == "rul" ) {
91         FGRUL *rul = new FGRUL;
92         io = rul;
93     } else if ( protocol == "joyclient" ) {
94         FGJoyClient *joyclient = new FGJoyClient;
95         io = joyclient;
96     } else {
97         return NULL;
98     }
99
100     // determine medium
101     end = config.find(",", begin);
102     if ( end == string::npos ) {
103         return NULL;            // dummy
104     }
105     
106     string medium = config.substr(begin, end - begin);
107     begin = end + 1;
108     FG_LOG( FG_IO, FG_INFO, "  medium = " << medium );
109
110     // determine direction
111     end = config.find(",", begin);
112     if ( end == string::npos ) {
113         return NULL;            // dummy
114     }
115     
116     string direction = config.substr(begin, end - begin);
117     begin = end + 1;
118     io->set_direction( direction );
119     FG_LOG( FG_IO, FG_INFO, "  direction = " << direction );
120
121     // determine hertz
122     end = config.find(",", begin);
123     if ( end == string::npos ) {
124         return NULL;            // dummy
125     }
126     
127     string hertz_str = config.substr(begin, end - begin);
128     begin = end + 1;
129     double hertz = atof( hertz_str.c_str() );
130     io->set_hz( hertz );
131     FG_LOG( FG_IO, FG_INFO, "  hertz = " << hertz );
132
133     if ( medium == "serial" ) {
134         // device name
135         end = config.find(",", begin);
136         if ( end == string::npos ) {
137             return NULL;
138         }
139     
140         string device = config.substr(begin, end - begin);
141         begin = end + 1;
142         FG_LOG( FG_IO, FG_INFO, "  device = " << device );
143
144         // baud
145         string baud = config.substr(begin);
146         FG_LOG( FG_IO, FG_INFO, "  baud = " << baud );
147
148         SGSerial *ch = new SGSerial( device, baud );
149         io->set_io_channel( ch );
150     } else if ( medium == "file" ) {
151         // file name
152         string file = config.substr(begin);
153         FG_LOG( FG_IO, FG_INFO, "  file name = " << file );
154
155         SGFile *ch = new SGFile( file );
156         io->set_io_channel( ch );
157     } else if ( medium == "socket" ) {
158         // hostname
159         end = config.find(",", begin);
160         if ( end == string::npos ) {
161             return NULL;
162         }
163     
164         string hostname = config.substr(begin, end - begin);
165         begin = end + 1;
166         FG_LOG( FG_IO, FG_INFO, "  hostname = " << hostname );
167
168         // port string
169         end = config.find(",", begin);
170         if ( end == string::npos ) {
171             return NULL;
172         }
173     
174         string port = config.substr(begin, end - begin);
175         begin = end + 1;
176         FG_LOG( FG_IO, FG_INFO, "  port string = " << port );
177
178         // socket style
179         string style_str = config.substr(begin);
180         FG_LOG( FG_IO, FG_INFO, "  style string = " << style_str );
181        
182         SGSocket *ch = new SGSocket( hostname, port, style_str );
183         io->set_io_channel( ch );
184     }
185
186     return io;
187 }
188
189
190 // step through the port config streams (from fgOPTIONS) and setup
191 // serial port channels for each
192 void fgIOInit() {
193     FGProtocol *p;
194     string_list channel_options_list = 
195         current_options.get_channel_options_list();
196
197     // we could almost do this in a single step except pushing a valid
198     // port onto the port list copies the structure and destroys the
199     // original, which closes the port and frees up the fd ... doh!!!
200
201     // parse the configuration strings and store the results in the
202     // appropriate FGIOChannel structures
203     for ( int i = 0; i < (int)channel_options_list.size(); ++i ) {
204         p = parse_port_config( channel_options_list[i] );
205         if ( p != NULL ) {
206             p->open();
207             global_io_list.push_back( p );
208             if ( !p->is_enabled() ) {
209                 FG_LOG( FG_IO, FG_INFO, "I/O Channel config failed." );
210             }
211         } else {
212             FG_LOG( FG_IO, FG_INFO, "I/O Channel parse failed." );
213         }
214     }
215 }
216
217
218 // process any serial port work
219 void fgIOProcess() {
220     FGProtocol *p;
221
222     // cout << "processing I/O channels" << endl;
223
224     static int inited = 0;
225     int interval;
226     static FGTimeStamp last;
227     FGTimeStamp current;
228
229     if ( ! inited ) {
230         inited = 1;
231         last.stamp();
232         interval = 0;
233     } else {
234         current.stamp();
235         interval = current - last;
236         last = current;
237     }
238
239     for ( int i = 0; i < (int)global_io_list.size(); ++i ) {
240         // cout << "  channel = " << i << endl;
241         p = global_io_list[i];
242
243         if ( p->is_enabled() ) {
244             p->dec_count_down( interval );
245             while ( p->get_count_down() < 0 ) {
246                 p->process();
247                 p->dec_count_down( -1000000.0 / p->get_hz() );
248             }
249         }
250     }
251 }