]> git.mxchange.org Git - flightgear.git/blob - src/FDM/ExternalPipe/ExternalPipe.cxx
bdad4bd213c11067593ffb4d687069609e9ae065
[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     SG_LOG( SG_IO, SG_INFO, "Closing up the ExternalPipe." );
93     
94 #if defined( HAVE_SYS_TYPES_H ) && defined( HAVE_SYS_STAT_H )
95     // close
96     int result;
97     result = close( pd1 );
98     if ( result == -1 ) {
99         SG_LOG( SG_IO, SG_ALERT, "Unable to close named pipe: "
100                 << fifo_name_1 );
101     }
102     result = close( pd2 );
103     if ( result == -1 ) {
104         SG_LOG( SG_IO, SG_ALERT, "Unable to close named pipe: "
105                 << fifo_name_2 );
106     }
107 #endif
108 }
109
110
111 // Initialize the ExternalPipe flight model, dt is the time increment
112 // for each subsequent iteration through the EOM
113 void FGExternalPipe::init() {
114     // Explicitly call the superclass's
115     // init method first.
116     common_init();
117
118     double lon = fgGetDouble( "/sim/presets/longitude-deg" );
119     double lat = fgGetDouble( "/sim/presets/latitude-deg" );
120     double alt = fgGetDouble( "/sim/presets/altitude-ft" );
121     double ground = fgGetDouble( "/environment/ground-elevation-m" );
122     double heading = fgGetDouble("/sim/presets/heading-deg");
123     double speed = fgGetDouble( "/sim/presets/airspeed-kt" );
124
125 #if defined( HAVE_SYS_TYPES_H ) && defined( HAVE_SYS_STAT_H )
126
127     char cmd[256];
128     int result;
129
130     sprintf( cmd, "1longitude-deg=%.8f", lon );
131     result = std::write( pd1, cmd, strlen(cmd) );
132     if ( result == -1 ) {
133         SG_LOG( SG_IO, SG_ALERT, "Write error to named pipe: " << fifo_name_1 );
134     }
135
136     sprintf( cmd, "1latitude-deg=%.8f", lat );
137     result = ::write( pd1, cmd, strlen(cmd) );
138     if ( result == -1 ) {
139         SG_LOG( SG_IO, SG_ALERT, "Write error to named pipe: " << fifo_name_1 );
140     }
141
142     sprintf( cmd, "1altitude-ft=%.8f", alt );
143     result = std::write( pd1, cmd, strlen(cmd) );
144     if ( result == -1 ) {
145         SG_LOG( SG_IO, SG_ALERT, "Write error to named pipe: " << fifo_name_1 );
146     }
147
148     sprintf( cmd, "1ground-m=%.8f", ground );
149     result = std::write( pd1, cmd, strlen(cmd) );
150     if ( result == -1 ) {
151         SG_LOG( SG_IO, SG_ALERT, "Write error to named pipe: " << fifo_name_1 );
152     }
153
154     sprintf( cmd, "1speed-kts=%.8f", speed );
155     result = std::write( pd1, cmd, strlen(cmd) );
156     if ( result == -1 ) {
157         SG_LOG( SG_IO, SG_ALERT, "Write error to named pipe: " << fifo_name_1 );
158     }
159
160     sprintf( cmd, "1heading-deg=%.8f", heading );
161     result = std::write( pd1, cmd, strlen(cmd) );
162     if ( result == -1 ) {
163         SG_LOG( SG_IO, SG_ALERT, "Write error to named pipe: " << fifo_name_1 );
164     }
165
166     SG_LOG( SG_IO, SG_INFO, "before sending reset command." );
167
168     if( fgGetBool("/sim/presets/onground") ) {
169       sprintf( cmd, "1reset=ground" );
170     } else {
171       sprintf( cmd, "1reset=air" );
172     }
173     result = std::write( pd1, cmd, strlen(cmd) );
174     if ( result == -1 ) {
175         SG_LOG( SG_IO, SG_ALERT, "Write error to named pipe: " << fifo_name_1 );
176     }
177
178     SG_LOG( SG_IO, SG_INFO, "Remote FDM init() finished." );
179 #endif
180 }
181
182
183 // Run an iteration of the EOM.
184 void FGExternalPipe::update( double dt ) {
185 #if defined( HAVE_SYS_TYPES_H ) && defined( HAVE_SYS_STAT_H )
186     // SG_LOG( SG_IO, SG_INFO, "Start FGExternalPipe::udpate()" );
187
188     int length;
189     int result;
190
191     if ( is_suspended() ) {
192         return;
193     }
194
195     int iterations = _calc_multiloop(dt);
196
197     // Send control positions to remote fdm
198     length = sizeof(ctrls);
199     FGProps2NetCtrls( &ctrls, false );
200     char *ptr = buf;
201     *ptr = '2';
202     ptr++;
203     *((int *)ptr) = iterations;
204     ptr += sizeof(int);
205     memcpy( ptr, (char *)(&ctrls), length );
206     result = std::write( pd1, buf, length + 1 );
207     if ( result == -1 ) {
208         SG_LOG( SG_IO, SG_ALERT, "Write error to named pipe: "
209                 << fifo_name_1 );
210     }
211     // cout << "  wrote to pipe" << endl;
212
213     length = sizeof(fdm);
214     result = std::read( pd2, (char *)(& fdm), length );
215     if ( result == -1 ) {
216         SG_LOG( SG_IO, SG_ALERT, "Read error from named pipe: "
217                 << fifo_name_2 );
218     }
219     FGNetFDM2Props( &fdm, false );
220     // cout << "read from pipe" << endl;
221
222     // SG_LOG( SG_IO, SG_INFO, "  End FGExternalPipe::udpate()" );
223 #endif
224 }