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