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