]> git.mxchange.org Git - flightgear.git/blob - src/FDM/ExternalPipe/ExternalPipe.cxx
Curt:
[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 #ifdef HAVE_MKFIFO
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 #ifdef HAVE_MKFIFO
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     if ( pd1 == -1 ) {
73         SG_LOG( SG_IO, SG_ALERT, "Unable to open named pipe: " << fifo_name_1 );
74         valid = false;
75     }
76     pd2 = open( fifo_name_2.c_str(), O_RDWR );
77     if ( pd2 == -1 ) {
78         SG_LOG( SG_IO, SG_ALERT, "Unable to open named pipe: " << fifo_name_2 );
79         valid = false;
80     }
81 #endif
82 }
83
84
85 FGExternalPipe::~FGExternalPipe() {
86     delete [] buf;
87
88     SG_LOG( SG_IO, SG_INFO, "Closing up the ExternalPipe." );
89     
90 #ifdef HAVE_MKFIFO
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 #endif
104 }
105
106
107 // Initialize the ExternalPipe flight model, dt is the time increment
108 // for each subsequent iteration through the EOM
109 void FGExternalPipe::init() {
110     // Explicitly call the superclass's
111     // init method first.
112     common_init();
113
114     double lon = fgGetDouble( "/sim/presets/longitude-deg" );
115     double lat = fgGetDouble( "/sim/presets/latitude-deg" );
116     double alt = fgGetDouble( "/sim/presets/altitude-ft" );
117     double ground = fgGetDouble( "/environment/ground-elevation-m" );
118     double heading = fgGetDouble("/sim/presets/heading-deg");
119     double speed = fgGetDouble( "/sim/presets/airspeed-kt" );
120
121 #ifdef HAVE_MKFIFO
122
123     char cmd[256];
124     int result;
125
126     sprintf( cmd, "1longitude-deg=%.8f", lon );
127     result = write( pd1, cmd, strlen(cmd) );
128     if ( result == -1 ) {
129         SG_LOG( SG_IO, SG_ALERT, "Write error to named pipe: " << fifo_name_1 );
130     }
131
132     sprintf( cmd, "1latitude-deg=%.8f", lat );
133     result = ::write( pd1, cmd, strlen(cmd) );
134     if ( result == -1 ) {
135         SG_LOG( SG_IO, SG_ALERT, "Write error to named pipe: " << fifo_name_1 );
136     }
137
138     sprintf( cmd, "1altitude-ft=%.8f", alt );
139     result = 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, "1ground-m=%.8f", ground );
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, "1speed-kts=%.8f", speed );
151     result = 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, "1heading-deg=%.8f", heading );
157     result = 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     SG_LOG( SG_IO, SG_INFO, "before sending reset command." );
163
164     if( fgGetBool("/sim/presets/onground") ) {
165       sprintf( cmd, "1reset=ground" );
166     } else {
167       sprintf( cmd, "1reset=air" );
168     }
169     result = 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, "Remote FDM init() finished." );
175 #endif
176 }
177
178
179 // Run an iteration of the EOM.
180 void FGExternalPipe::update( double dt ) {
181 #ifdef HAVE_MKFIFO
182     // SG_LOG( SG_IO, SG_INFO, "Start FGExternalPipe::udpate()" );
183
184     int length;
185     int result;
186
187     if ( is_suspended() ) {
188         return;
189     }
190
191     int iterations = _calc_multiloop(dt);
192
193     // Send control positions to remote fdm
194     length = sizeof(ctrls);
195     FGProps2NetCtrls( &ctrls, true, false );
196     char *ptr = buf;
197     *ptr = '2';
198     ptr++;
199     *((int *)ptr) = iterations;
200     ptr += sizeof(int);
201     memcpy( ptr, (char *)(&ctrls), length );
202     result = write( pd1, buf, length + 1 );
203     if ( result == -1 ) {
204         SG_LOG( SG_IO, SG_ALERT, "Write error to named pipe: "
205                 << fifo_name_1 );
206     }
207
208     length = sizeof(fdm);
209     result = read( pd2, (char *)(& fdm), length );
210     if ( result == -1 ) {
211         SG_LOG( SG_IO, SG_ALERT, "Read error from named pipe: "
212                 << fifo_name_2 );
213     }
214     FGNetFDM2Props( &fdm, false );
215 #endif
216 }