1 // native_gui.cxx -- FGFS external gui data export class
3 // Written by Curtis Olson, started January 2002.
5 // Copyright (C) 2002 Curtis L. Olson - http://www.flightgear.org/~curt
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.
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.
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.
28 #include <simgear/debug/logstream.hxx>
29 #include <simgear/io/lowlevel.hxx> // endian tests
30 #include <simgear/io/iochannel.hxx>
31 #include <simgear/timing/sg_time.hxx>
33 #include <Main/fg_props.hxx>
34 #include <Main/globals.hxx>
35 #include <Scenery/scenery.hxx>
36 #include <FDM/flightProperties.hxx>
38 #include "native_gui.hxx"
40 // FreeBSD works better with this included last ... (?)
41 #if defined( _MSC_VER )
43 #elif defined( __MINGW32__ )
44 # include <winsock2.h>
46 # include <netinet/in.h> // htonl() ntohl()
50 // #define FG_USE_NETWORK_BYTE_ORDER
51 #if defined( FG_USE_NETWORK_BYTE_ORDER )
53 // The function htond is defined this way due to the way some
54 // processors and OSes treat floating point values. Some will raise
55 // an exception whenever a "bad" floating point value is loaded into a
56 // floating point register. Solaris is notorious for this, but then
57 // so is LynxOS on the PowerPC. By translating the data in place,
58 // there is no need to load a FP register with the "corruped" floating
59 // point value. By doing the BIG_ENDIAN test, I can optimize the
60 // routine for big-endian processors so it can be as efficient as
62 static void htond (double &x)
64 if ( sgIsLittleEndian() ) {
68 Double_Overlay = (int *) &x;
69 Holding_Buffer = Double_Overlay [0];
71 Double_Overlay [0] = htonl (Double_Overlay [1]);
72 Double_Overlay [1] = htonl (Holding_Buffer);
77 static void htonf (float &x)
79 if ( sgIsLittleEndian() ) {
83 Float_Overlay = (int *) &x;
84 Holding_Buffer = Float_Overlay [0];
86 Float_Overlay [0] = htonl (Holding_Buffer);
94 FGNativeGUI::FGNativeGUI() {
97 FGNativeGUI::~FGNativeGUI() {
101 // open hailing frequencies
102 bool FGNativeGUI::open() {
103 if ( is_enabled() ) {
104 SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel "
105 << "is already in use, ignoring" );
109 SGIOChannel *io = get_io_channel();
111 if ( ! io->open( get_direction() ) ) {
112 SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
118 fgSetDouble("/position/sea-level-radius-ft", SG_EQUATORIAL_RADIUS_FT);
123 void FGProps2NetGUI( FGNetGUI *net ) {
124 static SGPropertyNode *nav_freq
125 = fgGetNode("/instrumentation/nav/frequencies/selected-mhz", true);
126 static SGPropertyNode *nav_target_radial
127 = fgGetNode("/instrumentation/nav/radials/target-radial-deg", true);
128 static SGPropertyNode *nav_inrange
129 = fgGetNode("/instrumentation/nav/in-range", true);
130 static SGPropertyNode *nav_loc
131 = fgGetNode("/instrumentation/nav/nav-loc", true);
132 static SGPropertyNode *nav_gs_dist_signed
133 = fgGetNode("/instrumentation/nav/gs-distance", true);
134 static SGPropertyNode *nav_loc_dist
135 = fgGetNode("/instrumentation/nav/nav-distance", true);
136 static SGPropertyNode *nav_reciprocal_radial
137 = fgGetNode("/instrumentation/nav/radials/reciprocal-radial-deg", true);
138 static SGPropertyNode *nav_gs_deflection
139 = fgGetNode("/instrumentation/nav/gs-needle-deflection", true);
142 static FlightProperties* fdm_state = new FlightProperties;
144 // Version sanity checking
145 net->version = FG_NET_GUI_VERSION;
148 net->longitude = fdm_state->get_Longitude();
149 net->latitude = fdm_state->get_Latitude();
150 net->altitude = fdm_state->get_Altitude() * SG_FEET_TO_METER;
151 net->phi = fdm_state->get_Phi();
152 net->theta = fdm_state->get_Theta();
153 net->psi = fdm_state->get_Psi();
156 net->vcas = fdm_state->get_V_calibrated_kts();
157 net->climb_rate = fdm_state->get_Climb_Rate();
160 net->num_tanks = FGNetGUI::FG_MAX_TANKS;
161 for ( i = 0; i < net->num_tanks; ++i ) {
162 SGPropertyNode *node = fgGetNode("/consumables/fuel/tank", i, true);
163 net->fuel_quantity[i] = node->getDoubleValue("level-gal_us");
167 net->cur_time = globals->get_time_params()->get_cur_time();
168 net->warp = globals->get_warp();
169 net->ground_elev = fdm_state->get_Runway_altitude_m();
172 net->tuned_freq = nav_freq->getDoubleValue();
173 net->nav_radial = nav_target_radial->getDoubleValue();
174 net->in_range = nav_inrange->getBoolValue();
176 if ( nav_loc->getBoolValue() ) {
179 = nav_gs_dist_signed->getDoubleValue()
183 net->dist_nm = nav_loc_dist->getDoubleValue()
187 net->course_deviation_deg
188 = nav_reciprocal_radial->getDoubleValue()
189 - nav_target_radial->getDoubleValue();
191 if ( net->course_deviation_deg < -1000.0
192 || net->course_deviation_deg > 1000.0 )
195 net->course_deviation_deg = 0.0;
197 while ( net->course_deviation_deg > 180.0 ) {
198 net->course_deviation_deg -= 360.0;
200 while ( net->course_deviation_deg < -180.0 ) {
201 net->course_deviation_deg += 360.0;
203 if ( fabs(net->course_deviation_deg) > 90.0 )
204 net->course_deviation_deg
205 = ( net->course_deviation_deg<0.0
206 ? -net->course_deviation_deg - 180.0
207 : -net->course_deviation_deg + 180.0 );
209 if ( nav_loc->getBoolValue() ) {
211 net->gs_deviation_deg
212 = nav_gs_deflection->getDoubleValue()
216 net->gs_deviation_deg = -9999.0;
219 #if defined( FG_USE_NETWORK_BYTE_ORDER )
220 // Convert the net buffer to network format
221 net->version = htonl(net->version);
223 htond(net->longitude);
224 htond(net->latitude);
225 htonf(net->altitude);
230 htonf(net->climb_rate);
232 for ( i = 0; i < net->num_tanks; ++i ) {
233 htonf(net->fuel_quantity[i]);
235 net->num_tanks = htonl(net->num_tanks);
237 net->cur_time = htonl( net->cur_time );
238 net->warp = htonl( net->warp );
239 net->ground_elev = htonl( net->ground_elev );
241 htonf(net->tuned_freq);
242 htonf(net->nav_radial);
243 net->in_range = htonl( net->in_range );
245 htonf(net->course_deviation_deg);
246 htonf(net->gs_deviation_deg);
251 void FGNetGUI2Props( FGNetGUI *net ) {
254 #if defined( FG_USE_NETWORK_BYTE_ORDER )
255 // Convert to the net buffer from network format
256 net->version = ntohl(net->version);
258 htond(net->longitude);
259 htond(net->latitude);
260 htonf(net->altitude);
265 htonf(net->climb_rate);
267 net->num_tanks = htonl(net->num_tanks);
268 for ( i = 0; i < net->num_tanks; ++i ) {
269 htonf(net->fuel_quantity[i]);
272 net->cur_time = ntohl(net->cur_time);
273 net->warp = ntohl(net->warp);
274 net->ground_elev = htonl( net->ground_elev );
276 htonf(net->tuned_freq);
277 htonf(net->nav_radial);
278 net->in_range = htonl( net->in_range );
280 htonf(net->course_deviation_deg);
281 htonf(net->gs_deviation_deg);
284 if ( net->version == FG_NET_GUI_VERSION ) {
285 FlightProperties fdm_state;
287 // cout << "pos = " << net->longitude << " " << net->latitude << endl;
288 // cout << "sea level rad = " << fdm_state->get_Sea_level_radius()
291 fdm_state.set_Latitude(net->latitude);
292 fdm_state.set_Longitude(net->longitude);
293 fdm_state.set_Altitude(net->altitude * SG_METER_TO_FEET);
295 fdm_state.set_Euler_Angles( net->phi,
299 fdm_state.set_V_calibrated_kts( net->vcas );
300 fdm_state.set_Climb_Rate( net->climb_rate );
302 for (i = 0; i < net->num_tanks; ++i ) {
303 SGPropertyNode * node
304 = fgGetNode("/consumables/fuel/tank", i, true);
305 node->setDoubleValue("level-gal_us", net->fuel_quantity[i] );
308 if ( net->cur_time ) {
309 fgSetLong("/sim/time/cur-time-override", net->cur_time);
312 globals->set_warp( net->warp );
315 fgSetDouble( "/instrumentation/nav[0]/frequencies/selected-mhz",
317 fgSetBool( "/instrumentation/nav[0]/in-range", net->in_range );
318 fgSetDouble( "/instrumentation/dme/indicated-distance-nm", net->dist_nm );
319 fgSetDouble( "/instrumentation/nav[0]/heading-needle-deflection",
320 net->course_deviation_deg );
321 fgSetDouble( "/instrumentation/nav[0]/gs-needle-deflection",
322 net->gs_deviation_deg );
324 SG_LOG( SG_IO, SG_ALERT,
325 "Error: version mismatch in FGNetNativeGUI2Props()" );
326 SG_LOG( SG_IO, SG_ALERT,
327 "\tread " << net->version << " need " << FG_NET_GUI_VERSION );
328 SG_LOG( SG_IO, SG_ALERT,
329 "\tNeed to upgrade net_fdm.hxx and recompile." );
334 // process work for this port
335 bool FGNativeGUI::process() {
336 SGIOChannel *io = get_io_channel();
337 int length = sizeof(buf);
339 if ( get_direction() == SG_IO_OUT ) {
340 // cout << "size of fdm_state = " << length << endl;
341 FGProps2NetGUI( &buf );
342 if ( ! io->write( (char *)(& buf), length ) ) {
343 SG_LOG( SG_IO, SG_ALERT, "Error writing data." );
346 } else if ( get_direction() == SG_IO_IN ) {
347 if ( io->get_type() == sgFileType ) {
348 if ( io->read( (char *)(& buf), length ) == length ) {
349 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
350 FGNetGUI2Props( &buf );
353 while ( io->read( (char *)(& buf), length ) == length ) {
354 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
355 FGNetGUI2Props( &buf );
365 bool FGNativeGUI::close() {
366 SGIOChannel *io = get_io_channel();
368 set_enabled( false );
370 if ( ! io->close() ) {