]> git.mxchange.org Git - flightgear.git/blob - src/FDM/ExternalPipe/ExternalPipe.cxx
da73ca06921826d8e1c5afd999dd0af1d14ad484
[flightgear.git] / src / FDM / ExternalPipe / ExternalPipe.cxx
1 // ExternalPipe.hxx -- a "pipe" interface to an external flight dynamics model
2 //
3 // Written by Curtis Olson, started March 2003.
4 //
5 // Copyright (C) 2003  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 #ifdef HAVE_CONFIG_H
24 #  include <config.h>
25 #endif
26
27 #if defined( HAVE_SYS_TYPES_H ) && defined( HAVE_SYS_STAT_H )
28 #  include <sys/types.h>        // mkfifo() open() umask()
29 #  include <sys/stat.h>         // mkfifo() open() umask()
30 #  include <fcntl.h>            // open()
31 #  include <unistd.h>           // unlink()
32 #endif
33
34 #include <simgear/debug/logstream.hxx>
35 #include <simgear/io/lowlevel.hxx> // endian tests
36
37 #include <Main/fg_props.hxx>
38 #include <Network/native_ctrls.hxx>
39 #include <Network/native_fdm.hxx>
40
41 #include "ExternalPipe.hxx"
42
43
44 FGExternalPipe::FGExternalPipe( double dt, string name ) {
45     valid = true;
46
47     buf = new char[sizeof(ctrls) + 1];
48
49     cout << "dt = " << dt << endl;
50
51 #if defined( HAVE_SYS_TYPES_H ) && defined( HAVE_SYS_STAT_H )
52     fifo_name_1 = name + "1";
53     fifo_name_2 = name + "2";
54
55     SG_LOG( SG_IO, SG_ALERT, "ExternalPipe Inited with " << name );
56
57     // Make the named pipe
58     umask(0);
59     int result;
60     result = mkfifo( fifo_name_1.c_str(), 0644 );
61     if ( result == -1 ) {
62         SG_LOG( SG_IO, SG_ALERT, "Unable to create named pipe: "
63                 << fifo_name_1 );
64         valid = false;
65     }
66     result = mkfifo( fifo_name_2.c_str(), 0644 );
67     if ( result == -1 ) {
68         SG_LOG( SG_IO, SG_ALERT, "Unable to create named pipe: "
69                 << fifo_name_2 );
70         valid = false;
71     }
72
73     pd1 = open( fifo_name_1.c_str(), O_RDWR );
74     cout << "pd1 = " << pd1 << endl;
75     if ( pd1 == -1 ) {
76         SG_LOG( SG_IO, SG_ALERT, "Unable to open named pipe: " << fifo_name_1 );
77         valid = false;
78     }
79     pd2 = open( fifo_name_2.c_str(), O_RDWR );
80     cout << "pd2 = " << pd2 << endl;
81     if ( pd2 == -1 ) {
82         SG_LOG( SG_IO, SG_ALERT, "Unable to open named pipe: " << fifo_name_2 );
83         valid = false;
84     }
85 #endif
86 }
87
88
89 FGExternalPipe::~FGExternalPipe() {
90     delete [] buf;
91
92 #if defined( HAVE_SYS_TYPES_H ) && defined( HAVE_SYS_STAT_H )
93     // close
94     int result;
95     result = close( pd1 );
96     if ( result == -1 ) {
97         SG_LOG( SG_IO, SG_ALERT, "Unable to close named pipe: "
98                 << fifo_name_1 );
99     }
100     result = close( pd2 );
101     if ( result == -1 ) {
102         SG_LOG( SG_IO, SG_ALERT, "Unable to close named pipe: "
103                 << fifo_name_2 );
104     }
105
106     // remove the file system entry
107     result = unlink( fifo_name_1.c_str() );
108     if ( result == -1 ) {
109         SG_LOG( SG_IO, SG_ALERT, "Unable to remove named pipe: "
110                 << fifo_name_1 );
111     }
112     result = unlink( fifo_name_2.c_str() );
113     if ( result == -1 ) {
114         SG_LOG( SG_IO, SG_ALERT, "Unable to remove named pipe: "
115                 << fifo_name_2 );
116     }
117 #endif
118 }
119
120
121 // Initialize the ExternalPipe flight model, dt is the time increment
122 // for each subsequent iteration through the EOM
123 void FGExternalPipe::init() {
124     // Explicitly call the superclass's
125     // init method first.
126     common_init();
127
128     double lon = fgGetDouble( "/sim/presets/longitude-deg" );
129     double lat = fgGetDouble( "/sim/presets/latitude-deg" );
130     double alt = fgGetDouble( "/sim/presets/altitude-ft" );
131     double ground = fgGetDouble( "/environment/ground-elevation-m" );
132     double heading = fgGetDouble("/sim/presets/heading-deg");
133     double speed = fgGetDouble( "/sim/presets/airspeed-kt" );
134
135 #if defined( HAVE_SYS_TYPES_H ) && defined( HAVE_SYS_STAT_H )
136
137     char cmd[256];
138     int result;
139
140     sprintf( cmd, "1longitude-deg=%.8f", lon );
141     result = std::write( pd1, cmd, strlen(cmd) );
142     if ( result == -1 ) {
143         SG_LOG( SG_IO, SG_ALERT, "Write error to named pipe: " << fifo_name_1 );
144     }
145
146     sprintf( cmd, "1latitude-deg=%.8f", lat );
147     result = ::write( pd1, cmd, strlen(cmd) );
148     if ( result == -1 ) {
149         SG_LOG( SG_IO, SG_ALERT, "Write error to named pipe: " << fifo_name_1 );
150     }
151
152     sprintf( cmd, "1altitude-ft=%.8f", alt );
153     result = std::write( pd1, cmd, strlen(cmd) );
154     if ( result == -1 ) {
155         SG_LOG( SG_IO, SG_ALERT, "Write error to named pipe: " << fifo_name_1 );
156     }
157
158     sprintf( cmd, "1ground-m=%.8f", ground );
159     result = std::write( pd1, cmd, strlen(cmd) );
160     if ( result == -1 ) {
161         SG_LOG( SG_IO, SG_ALERT, "Write error to named pipe: " << fifo_name_1 );
162     }
163
164     sprintf( cmd, "1speed-kts=%.8f", speed );
165     result = std::write( pd1, cmd, strlen(cmd) );
166     if ( result == -1 ) {
167         SG_LOG( SG_IO, SG_ALERT, "Write error to named pipe: " << fifo_name_1 );
168     }
169
170     sprintf( cmd, "1heading-deg=%.8f", heading );
171     result = std::write( pd1, cmd, strlen(cmd) );
172     if ( result == -1 ) {
173         SG_LOG( SG_IO, SG_ALERT, "Write error to named pipe: " << fifo_name_1 );
174     }
175
176     SG_LOG( SG_IO, SG_INFO, "before sending reset command." );
177
178     if( fgGetBool("/sim/presets/onground") ) {
179       sprintf( cmd, "1reset=ground" );
180     } else {
181       sprintf( cmd, "1reset=air" );
182     }
183     result = std::write( pd1, cmd, strlen(cmd) );
184     if ( result == -1 ) {
185         SG_LOG( SG_IO, SG_ALERT, "Write error to named pipe: " << fifo_name_1 );
186     }
187
188     SG_LOG( SG_IO, SG_INFO, "Remote FDM init() finished." );
189 #endif
190 }
191
192
193 // Run an iteration of the EOM.
194 void FGExternalPipe::update( double dt ) {
195 #if defined( HAVE_SYS_TYPES_H ) && defined( HAVE_SYS_STAT_H )
196     int length;
197     int result;
198
199     if ( is_suspended() ) {
200         return;
201     }
202
203     int iterations = _calc_multiloop(dt);
204
205     // Send control positions to remote fdm
206     length = sizeof(ctrls);
207     FGProps2NetCtrls( &ctrls, false );
208     char *ptr = buf;
209     *ptr = '2';
210     ptr++;
211     *((int *)ptr) = iterations;
212     ptr += sizeof(int);
213     memcpy( ptr, (char *)(&ctrls), length );
214     result = std::write( pd1, buf, length + 1 );
215     if ( result == -1 ) {
216         SG_LOG( SG_IO, SG_ALERT, "Write error to named pipe: "
217                 << fifo_name_1 );
218     }
219     // cout << "wrote to pipe" << endl;
220
221     // Read next set of FDM data (blocking enabled to maintain 'sync')
222     length = sizeof(fdm);
223     result = std::read( pd2, (char *)(& fdm), length );
224     if ( result == -1 ) {
225         SG_LOG( SG_IO, SG_ALERT, "Read error from named pipe: "
226                 << fifo_name_2 );
227     }
228     FGNetFDM2Props( &fdm, false );
229     // cout << "read from pipe" << endl;
230 #endif
231 }