]> git.mxchange.org Git - flightgear.git/blob - src/Main/fg_io.cxx
981a6f9e3e2902e09aa308c616f732710dcdad6b
[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 STL_STRING
27
28 #include <simgear/debug/logstream.hxx>
29 #include <simgear/math/fg_types.hxx>
30
31 #include <Main/options.hxx>
32
33 #include <Network/iochannel.hxx>
34 #include <Network/fg_file.hxx>
35 #include <Network/fg_serial.hxx>
36 #include <Network/fg_socket.hxx>
37
38 #include <Network/protocol.hxx>
39 #include <Network/native.hxx>
40 #include <Network/garmin.hxx>
41 #include <Network/nmea.hxx>
42 #include <Network/pve.hxx>
43 #include <Network/rul.hxx>
44 #include <Network/joyclient.hxx>
45
46 #include <Time/timestamp.hxx>
47
48 FG_USING_STD(string);
49
50
51 // define the global I/O channel list
52 io_container global_io_list;
53
54
55 // configure a port based on the config string
56 static FGProtocol *parse_port_config( const string& config )
57 {
58     string::size_type begin, end;
59
60     begin = 0;
61
62     FG_LOG( FG_IO, FG_INFO, "Parse I/O channel request: " << config );
63
64     // determine protocol
65     end = config.find(",", begin);
66     if ( end == string::npos ) {
67         return NULL;            // dummy
68     }
69     
70     string protocol = config.substr(begin, end - begin);
71     begin = end + 1;
72     FG_LOG( FG_IO, FG_INFO, "  protocol = " << protocol );
73
74     FGProtocol *io;
75     if ( protocol == "native" ) {
76         FGNative *native = new FGNative;
77         io = native;
78     } else if ( protocol == "garmin" ) {
79         FGGarmin *garmin = new FGGarmin;
80         io = garmin;
81     } else if ( protocol == "nmea" ) {
82         FGNMEA *nmea = new FGNMEA;
83         io = nmea;
84     } else if ( protocol == "pve" ) {
85         FGPVE *pve = new FGPVE;
86         io = pve;
87     } else if ( protocol == "rul" ) {
88         FGRUL *rul = new FGRUL;
89         io = rul;
90     } else if ( protocol == "joyclient" ) {
91         FGJoyClient *joyclient = new FGJoyClient;
92         io = joyclient;
93     } else {
94         return NULL;
95     }
96
97     // determine medium
98     end = config.find(",", begin);
99     if ( end == string::npos ) {
100         return NULL;            // dummy
101     }
102     
103     string medium = config.substr(begin, end - begin);
104     begin = end + 1;
105     FG_LOG( FG_IO, FG_INFO, "  medium = " << medium );
106
107     // determine direction
108     end = config.find(",", begin);
109     if ( end == string::npos ) {
110         return NULL;            // dummy
111     }
112     
113     string direction = config.substr(begin, end - begin);
114     begin = end + 1;
115     io->set_direction( direction );
116     FG_LOG( FG_IO, FG_INFO, "  direction = " << direction );
117
118     // determine hertz
119     end = config.find(",", begin);
120     if ( end == string::npos ) {
121         return NULL;            // dummy
122     }
123     
124     string hertz_str = config.substr(begin, end - begin);
125     begin = end + 1;
126     double hertz = atof( hertz_str.c_str() );
127     io->set_hz( hertz );
128     FG_LOG( FG_IO, FG_INFO, "  hertz = " << hertz );
129
130     if ( medium == "serial" ) {
131         FGSerial *ch = new FGSerial;
132         io->set_io_channel( ch );
133
134         // device name
135         end = config.find(",", begin);
136         if ( end == string::npos ) {
137             return NULL;
138         }
139     
140         ch->set_device( config.substr(begin, end - begin) );
141         begin = end + 1;
142         FG_LOG( FG_IO, FG_INFO, "  device = " << ch->get_device() );
143
144         // baud
145         ch->set_baud( config.substr(begin) );
146         FG_LOG( FG_IO, FG_INFO, "  baud = " << ch->get_baud() );
147
148         io->set_io_channel( ch );
149     } else if ( medium == "file" ) {
150         FGFile *ch = new FGFile;
151         io->set_io_channel( ch );
152
153         // file name
154         ch->set_file_name( config.substr(begin) );
155         FG_LOG( FG_IO, FG_INFO, "  file name = " << ch->get_file_name() );
156     } else if ( medium == "socket" ) {
157         FGSocket *ch = new FGSocket;
158         io->set_io_channel( ch );
159
160         // hostname
161         end = config.find(",", begin);
162         if ( end == string::npos ) {
163             return NULL;
164         }
165     
166         ch->set_hostname( config.substr(begin, end - begin) );
167         begin = end + 1;
168         FG_LOG( FG_IO, FG_INFO, "  hostname = " << ch->get_hostname() );
169
170         // port
171         ch->set_port_str( config.substr(begin) );
172         FG_LOG( FG_IO, FG_INFO, "  port string = " << ch->get_port_str() );
173
174     }
175
176     return io;
177 }
178
179
180 // step through the port config streams (from fgOPTIONS) and setup
181 // serial port channels for each
182 void fgIOInit() {
183     FGProtocol *p;
184     string_list channel_options_list = 
185         current_options.get_channel_options_list();
186
187     // we could almost do this in a single step except pushing a valid
188     // port onto the port list copies the structure and destroys the
189     // original, which closes the port and frees up the fd ... doh!!!
190
191     // parse the configuration strings and store the results in the
192     // appropriate FGIOChannel structures
193     for ( int i = 0; i < (int)channel_options_list.size(); ++i ) {
194         p = parse_port_config( channel_options_list[i] );
195         if ( p != NULL ) {
196             p->open();
197             global_io_list.push_back( p );
198             if ( !p->is_enabled() ) {
199                 FG_LOG( FG_IO, FG_INFO, "I/O Channel config failed." );
200             }
201         } else {
202             FG_LOG( FG_IO, FG_INFO, "I/O Channel parse failed." );
203         }
204     }
205 }
206
207
208 // process any serial port work
209 void fgIOProcess() {
210     FGProtocol *p;
211
212     // cout << "processing I/O channels" << endl;
213
214     static int inited = 0;
215     int interval;
216     static FGTimeStamp last;
217     FGTimeStamp current;
218
219     if ( ! inited ) {
220         inited = 1;
221         last.stamp();
222         interval = 0;
223     } else {
224         current.stamp();
225         interval = current - last;
226         last = current;
227     }
228
229     for ( int i = 0; i < (int)global_io_list.size(); ++i ) {
230         // cout << "  channel = " << i << endl;
231         p = global_io_list[i];
232
233         if ( p->is_enabled() ) {
234             p->dec_count_down( interval );
235             if ( p->get_count_down() < 0 ) {
236                 p->process();
237                 p->set_count_down( 1000000.0 / p->get_hz() );
238             }
239         }
240     }
241 }