]> git.mxchange.org Git - flightgear.git/blob - src/Network/ATC-Main.cxx
Add missing include files needed by the new math code under windows
[flightgear.git] / src / Network / ATC-Main.cxx
1 // ATC-Main.cxx -- FGFS interface to ATC hardware
2 //
3 // Written by Curtis Olson, started January 2002
4 //
5 // Copyright (C) 2002  Curtis L. Olson - http://www.flightgear.org/~curt
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 #ifdef HAVE_CONFIG_H
25 #  include <config.h>
26 #endif
27
28 #include <simgear/compiler.h>
29
30 #include <stdlib.h>             // atoi() atof() abs()
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <fcntl.h>
34 #include <stdio.h>              //snprintf
35 #if defined( _MSC_VER ) || defined(__MINGW32__)
36 #  include <io.h>                 //lseek, read, write
37 #endif
38
39 #include STL_STRING
40
41 #include <plib/ul.h>
42
43 #include <simgear/debug/logstream.hxx>
44 #include <simgear/io/iochannel.hxx>
45 #include <simgear/math/sg_types.hxx>
46 #include <simgear/misc/sg_path.hxx>
47 #include <simgear/props/props.hxx>
48
49 #include <Scripting/NasalSys.hxx>
50 #include <Main/fg_props.hxx>
51 #include <Main/globals.hxx>
52
53 #include "ATC-Main.hxx"
54
55 SG_USING_STD(string);
56
57
58 // Lock the ATC hardware
59 static int fgATCMainLock( int fd ) {
60     // rewind
61     lseek( fd, 0, SEEK_SET );
62
63     char tmp[2];
64     int result = read( fd, tmp, 1 );
65     if ( result != 1 ) {
66         SG_LOG( SG_IO, SG_DEBUG, "Lock failed" );
67     }
68
69     return result;
70 }
71
72
73 // Write a radios command
74 static int fgATCMainRelease( int fd ) {
75     // rewind
76     lseek( fd, 0, SEEK_SET );
77
78     char tmp[2];
79     tmp[0] = tmp[1] = 0;
80     int result = write( fd, tmp, 1 );
81
82     if ( result != 1 ) {
83         SG_LOG( SG_IO, SG_DEBUG, "Release failed" );
84     }
85
86     return result;
87 }
88
89
90 void FGATCMain::init_config() {
91 #if defined( unix ) || defined( __CYGWIN__ )
92     // Next check home directory for .fgfsrc.hostname file
93     char *envp = ::getenv( "HOME" );
94     if ( envp != NULL ) {
95         SGPath atcsim_config( envp );
96         atcsim_config.append( ".fgfs-atc610x.xml" );
97         readProperties( atcsim_config.str(), globals->get_props() );
98     }
99 #endif
100 }
101
102
103 // Open and initialize ATC hardware
104 bool FGATCMain::open() {
105     if ( is_enabled() ) {
106         SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel " 
107                 << "is already in use, ignoring" );
108         return false;
109     }
110
111     SG_LOG( SG_IO, SG_ALERT,
112             "Initializing ATC hardware, please wait ..." );
113
114     // This loads the config parameters generated by "simcal"
115     init_config();
116
117     /////////////////////////////////////////////////////////////////////
118     // Open the /proc files
119     /////////////////////////////////////////////////////////////////////
120
121     string lock0_file = "/proc/atcflightsim/board0/lock";
122     string lock1_file = "/proc/atcflightsim/board1/lock";
123
124     lock0_fd = ::open( lock0_file.c_str(), O_RDWR );
125     if ( lock0_fd == -1 ) {
126         SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
127         char msg[256];
128         snprintf( msg, 256, "Error opening %s", lock0_file.c_str() );
129         perror( msg );
130         exit( -1 );
131     }
132
133     lock1_fd = ::open( lock1_file.c_str(), O_RDWR );
134     if ( lock1_fd == -1 ) {
135         SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
136         char msg[256];
137         snprintf( msg, 256, "Error opening %s", lock1_file.c_str() );
138         perror( msg );
139         exit( -1 );
140     }
141
142     if ( input0_path.str().length() ) {
143         input0 = new FGATCInput( 0, input0_path );
144         input0->open();
145     }
146     if ( input1_path.str().length() ) {
147         input1 = new FGATCInput( 1, input1_path );
148         input1->open();
149     }
150     if ( output0_path.str().length() ) {
151         output0 = new FGATCOutput( 0, output0_path );
152         output0->open( lock0_fd );
153     }
154     if ( output1_path.str().length() ) {
155         output1 = new FGATCOutput( 1, output1_path );
156         output1->open( lock1_fd );
157     }
158
159     set_hz( 30 );               // default to processing requests @ 30Hz
160     set_enabled( true );
161
162     /////////////////////////////////////////////////////////////////////
163     // Finished initing hardware
164     /////////////////////////////////////////////////////////////////////
165
166     SG_LOG( SG_IO, SG_ALERT,
167             "Done initializing ATC hardware." );
168
169     return true;
170 }
171
172
173 bool FGATCMain::process() {
174     // cout << "Main::process()\n";
175
176     bool board0_locked = false;
177     bool board1_locked = false;
178
179     if ( input0 != NULL || output0 != NULL ) {
180         // Lock board0 if we have a configuration for it
181         if ( fgATCMainLock( lock0_fd ) > 0 ) {
182             board0_locked = true;
183         }
184     }
185
186     if ( input1 != NULL || output1 != NULL ) {
187         // Lock board1 if we have a configuration for it
188         if ( fgATCMainLock( lock1_fd ) > 0 ) {
189             board1_locked = true;
190         }
191     }
192
193     // cout << "  locks: ";
194     // if ( board0_locked ) { cout << "board0 "; }
195     // if ( board1_locked ) { cout << "board1 "; }
196     // cout << endl;
197
198     // process the ATC inputs
199     if ( input0 != NULL && board0_locked ) {
200         input0->process();
201     }
202     if ( input1 != NULL && board1_locked ) {
203         input1->process();
204     }
205
206     // run our custom nasal script.  This is a layer above the raw
207     // hardware inputs.  It handles situations where there isn't a
208     // direct 1-1 linear mapping between ATC functionality and FG
209     // functionality, and handles situations where FG expects more
210     // functionality from the interface than the ATC hardware can
211     // directly provide.
212
213     FGNasalSys *n = (FGNasalSys*)globals->get_subsystem("nasal");
214     bool result = n->parseAndRun( "atcsim.update()" );
215     if ( !result ) {
216         SG_LOG( SG_GENERAL, SG_ALERT, "Nasal: atcsim.update() failed!" );
217     }
218
219     // process the ATC outputs
220     if ( output0 != NULL && board0_locked ) {
221         output0->process();
222     }
223     if ( output1 != NULL && board1_locked ) {
224         output1->process();
225     }
226
227     if ( board0_locked ) {
228         fgATCMainRelease( lock0_fd );
229     }
230     if ( board1_locked ) {
231         fgATCMainRelease( lock1_fd );
232     }
233
234     return true;
235 }
236
237
238 bool FGATCMain::close() {
239     cout << "FGATCMain::close()" << endl;
240
241     int result;
242
243     if ( input0 != NULL ) {
244         input0->close();
245     }
246     if ( input1 != NULL ) {
247         input1->close();
248     }
249     if ( output0 != NULL ) {
250         output0->close();
251     }
252     if ( output1 != NULL ) {
253         output1->close();
254     }
255
256     result = ::close( lock0_fd );
257     if ( result == -1 ) {
258         SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
259         char msg[256];
260         snprintf( msg, 256, "Error closing lock0_fd" );
261         perror( msg );
262         exit( -1 );
263     }
264
265     result = ::close( lock1_fd );
266     if ( result == -1 ) {
267         SG_LOG( SG_IO, SG_ALERT, "errno = " << errno );
268         char msg[256];
269         snprintf( msg, 256, "Error closing lock1_fd" );
270         perror( msg );
271         exit( -1 );
272     }
273
274     return true;
275 }