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