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