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 - curt@flightgear.org
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., 675 Mass Ave, Cambridge, MA 02139, 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 <Cockpit/radiostack.hxx>
34 #include <FDM/flight.hxx>
35 #include <Time/tmp.hxx>
36 #include <Main/fg_props.hxx>
37 #include <Main/globals.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 ) {
125 // Version sanity checking
126 net->version = FG_NET_GUI_VERSION;
129 net->longitude = cur_fdm_state->get_Longitude();
130 net->latitude = cur_fdm_state->get_Latitude();
131 net->altitude = cur_fdm_state->get_Altitude() * SG_FEET_TO_METER;
132 net->phi = cur_fdm_state->get_Phi();
133 net->theta = cur_fdm_state->get_Theta();
134 net->psi = cur_fdm_state->get_Psi();
137 net->vcas = cur_fdm_state->get_V_calibrated_kts();
138 net->climb_rate = cur_fdm_state->get_Climb_Rate();
141 net->num_tanks = FGNetGUI::FG_MAX_TANKS;
142 for ( i = 0; i < net->num_tanks; ++i ) {
143 SGPropertyNode *node = fgGetNode("/consumables/fuel/tank", i, true);
144 net->fuel_quantity[i] = node->getDoubleValue("level-gal_us");
147 // the following really aren't used in this context
148 net->cur_time = globals->get_time_params()->get_cur_time();
149 net->warp = globals->get_warp();
152 net->dist_nm = current_radiostack->get_navcom1()->get_nav_gs_dist()
154 net->course_deviation_deg
155 = current_radiostack->get_navcom1()->get_nav_heading()
156 - current_radiostack->get_navcom1()->get_nav_radial();
157 while ( net->course_deviation_deg > 180.0 ) {
158 net->course_deviation_deg -= 360.0;
160 while ( net->course_deviation_deg < -180.0 ) {
161 net->course_deviation_deg += 360.0;
163 if ( fabs(net->course_deviation_deg) > 90.0 )
164 net->course_deviation_deg
165 = ( net->course_deviation_deg<0.0
166 ? -net->course_deviation_deg - 180.0
167 : -net->course_deviation_deg + 180.0 );
168 net->gs_deviation_deg
169 = current_radiostack->get_navcom1()->get_nav_gs_needle_deflection()
172 #if defined( FG_USE_NETWORK_BYTE_ORDER )
173 // Convert the net buffer to network format
174 net->version = htonl(net->version);
176 htond(net->longitude);
177 htond(net->latitude);
178 htonf(net->altitude);
183 htonf(net->climb_rate);
185 for ( i = 0; i < net->num_tanks; ++i ) {
186 htonf(net->fuel_quantity[i]);
188 net->num_tanks = htonl(net->num_tanks);
190 net->cur_time = htonl( net->cur_time );
191 net->warp = htonl( net->warp );
194 htonf(net->course_deviation_deg);
195 htonf(net->gs_deviation_deg);
200 void FGNetGUI2Props( FGNetGUI *net ) {
203 #if defined( FG_USE_NETWORK_BYTE_ORDER )
204 // Convert to the net buffer from network format
205 net->version = ntohl(net->version);
207 htond(net->longitude);
208 htond(net->latitude);
209 htonf(net->altitude);
214 htonf(net->climb_rate);
216 net->num_tanks = htonl(net->num_tanks);
217 for ( i = 0; i < net->num_tanks; ++i ) {
218 htonf(net->fuel_quantity[i]);
221 net->cur_time = ntohl(net->cur_time);
222 net->warp = ntohl(net->warp);
225 htonf(net->course_deviation_deg);
226 htonf(net->gs_deviation_deg);
229 if ( net->version == FG_NET_GUI_VERSION ) {
230 // cout << "pos = " << net->longitude << " " << net->latitude << endl;
231 // cout << "sea level rad = " << cur_fdm_state->get_Sea_level_radius()
233 cur_fdm_state->_updateGeodeticPosition( net->latitude,
236 * SG_METER_TO_FEET );
237 cur_fdm_state->_set_Euler_Angles( net->phi,
241 cur_fdm_state->_set_V_calibrated_kts( net->vcas );
242 cur_fdm_state->_set_Climb_Rate( net->climb_rate );
244 for (i = 0; i < net->num_tanks; ++i ) {
245 SGPropertyNode * node
246 = fgGetNode("/consumables/fuel/tank", i, true);
247 node->setDoubleValue("level-gal_us", net->fuel_quantity[i] );
250 if ( net->cur_time ) {
251 fgSetLong("/sim/time/cur-time-override", net->cur_time);
254 globals->set_warp( net->warp );
257 fgSetDouble( "/radios/dme/distance-nm", net->dist_nm );
258 fgSetDouble( "/radios/nav[0]/heading-needle-deflection",
259 net->course_deviation_deg );
260 fgSetDouble( "/radios/nav[0]/gs-needle-deflection",
261 net->gs_deviation_deg );
263 SG_LOG( SG_IO, SG_ALERT,
264 "Error: version mismatch in FGNetNativeGUI2Props()" );
265 SG_LOG( SG_IO, SG_ALERT,
266 "\tread " << net->version << " need " << FG_NET_GUI_VERSION );
267 SG_LOG( SG_IO, SG_ALERT,
268 "\tNeed to upgrade net_fdm.hxx and recompile." );
273 // process work for this port
274 bool FGNativeGUI::process() {
275 SGIOChannel *io = get_io_channel();
276 int length = sizeof(buf);
278 if ( get_direction() == SG_IO_OUT ) {
279 // cout << "size of cur_fdm_state = " << length << endl;
280 FGProps2NetGUI( &buf );
281 if ( ! io->write( (char *)(& buf), length ) ) {
282 SG_LOG( SG_IO, SG_ALERT, "Error writing data." );
285 } else if ( get_direction() == SG_IO_IN ) {
286 if ( io->get_type() == sgFileType ) {
287 if ( io->read( (char *)(& buf), length ) == length ) {
288 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
289 FGNetGUI2Props( &buf );
292 while ( io->read( (char *)(& buf), length ) == length ) {
293 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
294 FGNetGUI2Props( &buf );
304 bool FGNativeGUI::close() {
305 SGIOChannel *io = get_io_channel();
307 set_enabled( false );
309 if ( ! io->close() ) {