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