]> git.mxchange.org Git - flightgear.git/blob - src/Main/fg_io.cxx
Sync with latest JSBSim CVS
[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 <stdlib.h>             // atoi()
27
28 #include STL_STRING
29
30 #include <simgear/debug/logstream.hxx>
31 #include <simgear/io/iochannel.hxx>
32 #include <simgear/io/sg_file.hxx>
33 #include <simgear/io/sg_serial.hxx>
34 #include <simgear/io/sg_socket.hxx>
35 #include <simgear/io/sg_socket_udp.hxx>
36 #include <simgear/math/sg_types.hxx>
37 #include <simgear/timing/timestamp.hxx>
38
39 #include <Network/protocol.hxx>
40 #include <Network/atlas.hxx>
41 #include <Network/garmin.hxx>
42 #include <Network/httpd.hxx>
43 #include <Network/joyclient.hxx>
44 #include <Network/native.hxx>
45 #include <Network/native_ctrls.hxx>
46 #include <Network/native_fdm.hxx>
47 #include <Network/nmea.hxx>
48 #include <Network/props.hxx>
49 #include <Network/pve.hxx>
50 #include <Network/ray.hxx>
51 #include <Network/rul.hxx>
52
53 #include "globals.hxx"
54
55 SG_USING_STD(string);
56
57
58 // define the global I/O channel list
59 io_container global_io_list;
60
61
62 // configure a port based on the config string
63 static FGProtocol *parse_port_config( const string& config )
64 {
65     bool short_circuit = false;
66
67     string::size_type begin, end;
68
69     begin = 0;
70
71     SG_LOG( SG_IO, SG_INFO, "Parse I/O channel request: " << config );
72
73     // determine protocol
74     end = config.find(",", begin);
75     if ( end == string::npos ) {
76         return NULL;            // dummy
77     }
78     
79     string protocol = config.substr(begin, end - begin);
80     begin = end + 1;
81     SG_LOG( SG_IO, SG_INFO, "  protocol = " << protocol );
82
83     FGProtocol *io;
84     if ( protocol == "atlas" ) {
85         FGAtlas *atlas = new FGAtlas;
86         io = atlas;
87     } else if ( protocol == "garmin" ) {
88         FGGarmin *garmin = new FGGarmin;
89         io = garmin;
90     } else if ( protocol == "httpd" ) {
91         // determine port
92         string port = config.substr(begin);
93         FGHttpd *httpd = new FGHttpd( atoi(port.c_str()) );
94         io = httpd;
95         short_circuit = true;
96     } else if ( protocol == "joyclient" ) {
97         FGJoyClient *joyclient = new FGJoyClient;
98         io = joyclient;
99     } else if ( protocol == "native" ) {
100         FGNative *native = new FGNative;
101         io = native;
102     } else if ( protocol == "native_ctrls" ) {
103         FGNativeCtrls *native_ctrls = new FGNativeCtrls;
104         io = native_ctrls;
105     } else if ( protocol == "native_fdm" ) {
106         FGNativeFDM *native_fdm = new FGNativeFDM;
107         io = native_fdm;
108     } else if ( protocol == "nmea" ) {
109         FGNMEA *nmea = new FGNMEA;
110         io = nmea;
111     } else if ( protocol == "props" ) {
112         FGProps *props = new FGProps;
113         io = props;
114     } else if ( protocol == "pve" ) {
115         FGPVE *pve = new FGPVE;
116         io = pve;
117     } else if ( protocol == "ray" ) {
118         FGRAY *ray = new FGRAY;
119         io = ray;
120     } else if ( protocol == "rul" ) {
121         FGRUL *rul = new FGRUL;
122         io = rul;
123     } else {
124         return NULL;
125     }
126
127     if ( ! short_circuit ) {
128         // determine medium
129         end = config.find(",", begin);
130         if ( end == string::npos ) {
131             return NULL;                // dummy
132         }
133     
134         string medium = config.substr(begin, end - begin);
135         begin = end + 1;
136         SG_LOG( SG_IO, SG_INFO, "  medium = " << medium );
137
138         // determine direction
139         end = config.find(",", begin);
140         if ( end == string::npos ) {
141             return NULL;                // dummy
142         }
143     
144         string direction = config.substr(begin, end - begin);
145         begin = end + 1;
146         io->set_direction( direction );
147         SG_LOG( SG_IO, SG_INFO, "  direction = " << direction );
148
149         // determine hertz
150         end = config.find(",", begin);
151         if ( end == string::npos ) {
152             return NULL;                // dummy
153         }
154     
155         string hertz_str = config.substr(begin, end - begin);
156         begin = end + 1;
157         double hertz = atof( hertz_str.c_str() );
158         io->set_hz( hertz );
159         SG_LOG( SG_IO, SG_INFO, "  hertz = " << hertz );
160
161         if ( medium == "serial" ) {
162             // device name
163             end = config.find(",", begin);
164             if ( end == string::npos ) {
165                 return NULL;
166             }
167     
168             string device = config.substr(begin, end - begin);
169             begin = end + 1;
170             SG_LOG( SG_IO, SG_INFO, "  device = " << device );
171
172             // baud
173             string baud = config.substr(begin);
174             SG_LOG( SG_IO, SG_INFO, "  baud = " << baud );
175
176             SGSerial *ch = new SGSerial( device, baud );
177             io->set_io_channel( ch );
178         } else if ( medium == "file" ) {
179             // file name
180             string file = config.substr(begin);
181             SG_LOG( SG_IO, SG_INFO, "  file name = " << file );
182
183             SGFile *ch = new SGFile( file );
184             io->set_io_channel( ch );
185         } else if ( medium == "socket" ) {
186             // hostname
187             end = config.find(",", begin);
188             if ( end == string::npos ) {
189                 return NULL;
190             }
191     
192             string hostname = config.substr(begin, end - begin);
193             begin = end + 1;
194             SG_LOG( SG_IO, SG_INFO, "  hostname = " << hostname );
195
196             // port string
197             end = config.find(",", begin);
198             if ( end == string::npos ) {
199                 return NULL;
200             }
201     
202             string port = config.substr(begin, end - begin);
203             begin = end + 1;
204             SG_LOG( SG_IO, SG_INFO, "  port string = " << port );
205
206             // socket style
207             string style_str = config.substr(begin);
208             SG_LOG( SG_IO, SG_INFO, "  style string = " << style_str );
209             
210             SGSocket *ch = new SGSocket( hostname, port, style_str );
211             io->set_io_channel( ch );
212         }
213     }
214
215     return io;
216 }
217
218
219 // step through the port config streams (from fgOPTIONS) and setup
220 // serial port channels for each
221 void fgIOInit() {
222     // SG_LOG( SG_IO, SG_INFO, "I/O Channel initialization, " << 
223     //         globals->get_channel_options_list()->size() << " requests." );
224
225     FGProtocol *p;
226     string_list *channel_options_list = globals->get_channel_options_list();
227
228     // we could almost do this in a single step except pushing a valid
229     // port onto the port list copies the structure and destroys the
230     // original, which closes the port and frees up the fd ... doh!!!
231
232     // parse the configuration strings and store the results in the
233     // appropriate FGIOChannel structures
234     for ( int i = 0; i < (int)channel_options_list->size(); ++i ) {
235         p = parse_port_config( (*channel_options_list)[i] );
236         if ( p != NULL ) {
237             p->open();
238             global_io_list.push_back( p );
239             if ( !p->is_enabled() ) {
240                 SG_LOG( SG_IO, SG_ALERT, "I/O Channel config failed." );
241                 exit(-1);
242             }
243         } else {
244             SG_LOG( SG_IO, SG_INFO, "I/O Channel parse failed." );
245         }
246     }
247 }
248
249
250 // process any serial port work
251 void fgIOProcess() {
252     FGProtocol *p;
253
254     // cout << "processing I/O channels" << endl;
255
256     static int inited = 0;
257     int interval;
258     static SGTimeStamp last;
259     SGTimeStamp current;
260
261     if ( ! inited ) {
262         inited = 1;
263         last.stamp();
264         interval = 0;
265     } else {
266         current.stamp();
267         interval = current - last;
268         last = current;
269     }
270
271     for ( int i = 0; i < (int)global_io_list.size(); ++i ) {
272         // cout << "  channel = " << i << endl;
273         p = global_io_list[i];
274
275         if ( p->is_enabled() ) {
276             p->dec_count_down( interval );
277             while ( p->get_count_down() < 0 ) {
278                 p->process();
279                 p->dec_count_down( -1000000.0 / p->get_hz() );
280             }
281         }
282     }
283 }
284
285
286 // shutdown all I/O connections
287 void fgIOShutdownAll() {
288     FGProtocol *p;
289
290     // cout << "processing I/O channels" << endl;
291
292     for ( int i = 0; i < (int)global_io_list.size(); ++i ) {
293         // cout << "  channel = " << i << endl;
294         p = global_io_list[i];
295
296         if ( p->is_enabled() ) {
297             p->close();
298         }
299     }
300 }