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 <FDM/flight.hxx>
34 #include <Time/tmp.hxx>
35 #include <Main/fg_props.hxx>
36 #include <Main/globals.hxx>
37 #include <Scenery/scenery.hxx>
39 #include "native_gui.hxx"
41 // FreeBSD works better with this included last ... (?)
42 #if defined(WIN32) && !defined(__CYGWIN__)
45 # include <netinet/in.h> // htonl() ntohl()
49 // #define FG_USE_NETWORK_BYTE_ORDER
50 #if defined( FG_USE_NETWORK_BYTE_ORDER )
52 // The function htond is defined this way due to the way some
53 // processors and OSes treat floating point values. Some will raise
54 // an exception whenever a "bad" floating point value is loaded into a
55 // floating point register. Solaris is notorious for this, but then
56 // so is LynxOS on the PowerPC. By translating the data in place,
57 // there is no need to load a FP register with the "corruped" floating
58 // point value. By doing the BIG_ENDIAN test, I can optimize the
59 // routine for big-endian processors so it can be as efficient as
61 static void htond (double &x)
63 if ( sgIsLittleEndian() ) {
67 Double_Overlay = (int *) &x;
68 Holding_Buffer = Double_Overlay [0];
70 Double_Overlay [0] = htonl (Double_Overlay [1]);
71 Double_Overlay [1] = htonl (Holding_Buffer);
76 static void htonf (float &x)
78 if ( sgIsLittleEndian() ) {
82 Float_Overlay = (int *) &x;
83 Holding_Buffer = Float_Overlay [0];
85 Float_Overlay [0] = htonl (Holding_Buffer);
93 FGNativeGUI::FGNativeGUI() {
96 FGNativeGUI::~FGNativeGUI() {
100 // open hailing frequencies
101 bool FGNativeGUI::open() {
102 if ( is_enabled() ) {
103 SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel "
104 << "is already in use, ignoring" );
108 SGIOChannel *io = get_io_channel();
110 if ( ! io->open( get_direction() ) ) {
111 SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
117 cur_fdm_state->_set_Sea_level_radius( SG_EQUATORIAL_RADIUS_FT );
122 void FGProps2NetGUI( FGNetGUI *net ) {
123 static SGPropertyNode *nav_freq
124 = fgGetNode("/instrumentation/nav/frequencies/selected-mhz", true);
125 static SGPropertyNode *nav_target_radial
126 = fgGetNode("/instrumentation/nav/radials/target-radial-deg", true);
127 static SGPropertyNode *nav_inrange
128 = fgGetNode("/instrumentation/nav/in-range", true);
129 static SGPropertyNode *nav_loc
130 = fgGetNode("/instrumentation/nav/nav-loc", true);
131 static SGPropertyNode *nav_gs_dist_signed
132 = fgGetNode("/instrumentation/nav/gs-distance", true);
133 static SGPropertyNode *nav_loc_dist
134 = fgGetNode("/instrumentation/nav/nav-distance", true);
135 static SGPropertyNode *nav_reciprocal_radial
136 = fgGetNode("/instrumentation/nav/radials/reciprocal-radial-deg", true);
137 static SGPropertyNode *nav_gs_deflection
138 = fgGetNode("/instrumentation/nav/gs-needle-deflection", true);
141 // Version sanity checking
142 net->version = FG_NET_GUI_VERSION;
145 net->longitude = cur_fdm_state->get_Longitude();
146 net->latitude = cur_fdm_state->get_Latitude();
147 net->altitude = cur_fdm_state->get_Altitude() * SG_FEET_TO_METER;
148 net->phi = cur_fdm_state->get_Phi();
149 net->theta = cur_fdm_state->get_Theta();
150 net->psi = cur_fdm_state->get_Psi();
153 net->vcas = cur_fdm_state->get_V_calibrated_kts();
154 net->climb_rate = cur_fdm_state->get_Climb_Rate();
157 net->num_tanks = FGNetGUI::FG_MAX_TANKS;
158 for ( i = 0; i < net->num_tanks; ++i ) {
159 SGPropertyNode *node = fgGetNode("/consumables/fuel/tank", i, true);
160 net->fuel_quantity[i] = node->getDoubleValue("level-gal_us");
164 net->cur_time = globals->get_time_params()->get_cur_time();
165 net->warp = globals->get_warp();
166 net->ground_elev = cur_fdm_state->get_Runway_altitude_m();
169 net->tuned_freq = nav_freq->getDoubleValue();
170 net->nav_radial = nav_target_radial->getDoubleValue();
171 net->in_range = nav_inrange->getBoolValue();
173 if ( nav_loc->getBoolValue() ) {
176 = nav_gs_dist_signed->getDoubleValue()
180 net->dist_nm = nav_loc_dist->getDoubleValue()
184 net->course_deviation_deg
185 = nav_reciprocal_radial->getDoubleValue()
186 - nav_target_radial->getDoubleValue();
188 if ( net->course_deviation_deg < -1000.0
189 || net->course_deviation_deg > 1000.0 )
192 net->course_deviation_deg = 0.0;
194 while ( net->course_deviation_deg > 180.0 ) {
195 net->course_deviation_deg -= 360.0;
197 while ( net->course_deviation_deg < -180.0 ) {
198 net->course_deviation_deg += 360.0;
200 if ( fabs(net->course_deviation_deg) > 90.0 )
201 net->course_deviation_deg
202 = ( net->course_deviation_deg<0.0
203 ? -net->course_deviation_deg - 180.0
204 : -net->course_deviation_deg + 180.0 );
206 if ( nav_loc->getBoolValue() ) {
208 net->gs_deviation_deg
209 = nav_gs_deflection->getDoubleValue()
213 net->gs_deviation_deg = -9999.0;
216 #if defined( FG_USE_NETWORK_BYTE_ORDER )
217 // Convert the net buffer to network format
218 net->version = htonl(net->version);
220 htond(net->longitude);
221 htond(net->latitude);
222 htonf(net->altitude);
227 htonf(net->climb_rate);
229 for ( i = 0; i < net->num_tanks; ++i ) {
230 htonf(net->fuel_quantity[i]);
232 net->num_tanks = htonl(net->num_tanks);
234 net->cur_time = htonl( net->cur_time );
235 net->warp = htonl( net->warp );
236 net->ground_elev = htonl( net->ground_elev );
238 htonf(net->tuned_freq);
239 htonf(net->nav_radial);
240 net->in_range = htonl( net->in_range );
242 htonf(net->course_deviation_deg);
243 htonf(net->gs_deviation_deg);
248 void FGNetGUI2Props( FGNetGUI *net ) {
251 #if defined( FG_USE_NETWORK_BYTE_ORDER )
252 // Convert to the net buffer from network format
253 net->version = ntohl(net->version);
255 htond(net->longitude);
256 htond(net->latitude);
257 htonf(net->altitude);
262 htonf(net->climb_rate);
264 net->num_tanks = htonl(net->num_tanks);
265 for ( i = 0; i < net->num_tanks; ++i ) {
266 htonf(net->fuel_quantity[i]);
269 net->cur_time = ntohl(net->cur_time);
270 net->warp = ntohl(net->warp);
271 net->ground_elev = htonl( net->ground_elev );
273 htonf(net->tuned_freq);
274 htonf(net->nav_radial);
275 net->in_range = htonl( net->in_range );
277 htonf(net->course_deviation_deg);
278 htonf(net->gs_deviation_deg);
281 if ( net->version == FG_NET_GUI_VERSION ) {
282 // cout << "pos = " << net->longitude << " " << net->latitude << endl;
283 // cout << "sea level rad = " << cur_fdm_state->get_Sea_level_radius()
285 cur_fdm_state->_updateGeodeticPosition( net->latitude,
288 * SG_METER_TO_FEET );
289 cur_fdm_state->_set_Euler_Angles( net->phi,
293 cur_fdm_state->_set_V_calibrated_kts( net->vcas );
294 cur_fdm_state->_set_Climb_Rate( net->climb_rate );
296 for (i = 0; i < net->num_tanks; ++i ) {
297 SGPropertyNode * node
298 = fgGetNode("/consumables/fuel/tank", i, true);
299 node->setDoubleValue("level-gal_us", net->fuel_quantity[i] );
302 if ( net->cur_time ) {
303 fgSetLong("/sim/time/cur-time-override", net->cur_time);
306 globals->set_warp( net->warp );
309 fgSetDouble( "/instrumentation/nav[0]/frequencies/selected-mhz",
311 fgSetBool( "/instrumentation/nav[0]/in-range", net->in_range );
312 fgSetDouble( "/instrumentation/dme/distance-nm", net->dist_nm );
313 fgSetDouble( "/instrumentation/nav[0]/heading-needle-deflection",
314 net->course_deviation_deg );
315 fgSetDouble( "/instrumentation/nav[0]/gs-needle-deflection",
316 net->gs_deviation_deg );
318 SG_LOG( SG_IO, SG_ALERT,
319 "Error: version mismatch in FGNetNativeGUI2Props()" );
320 SG_LOG( SG_IO, SG_ALERT,
321 "\tread " << net->version << " need " << FG_NET_GUI_VERSION );
322 SG_LOG( SG_IO, SG_ALERT,
323 "\tNeed to upgrade net_fdm.hxx and recompile." );
328 // process work for this port
329 bool FGNativeGUI::process() {
330 SGIOChannel *io = get_io_channel();
331 int length = sizeof(buf);
333 if ( get_direction() == SG_IO_OUT ) {
334 // cout << "size of cur_fdm_state = " << length << endl;
335 FGProps2NetGUI( &buf );
336 if ( ! io->write( (char *)(& buf), length ) ) {
337 SG_LOG( SG_IO, SG_ALERT, "Error writing data." );
340 } else if ( get_direction() == SG_IO_IN ) {
341 if ( io->get_type() == sgFileType ) {
342 if ( io->read( (char *)(& buf), length ) == length ) {
343 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
344 FGNetGUI2Props( &buf );
347 while ( io->read( (char *)(& buf), length ) == length ) {
348 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
349 FGNetGUI2Props( &buf );
359 bool FGNativeGUI::close() {
360 SGIOChannel *io = get_io_channel();
362 set_enabled( false );
364 if ( ! io->close() ) {