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