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 // The function htond is defined this way due to the way some
50 // processors and OSes treat floating point values. Some will raise
51 // an exception whenever a "bad" floating point value is loaded into a
52 // floating point register. Solaris is notorious for this, but then
53 // so is LynxOS on the PowerPC. By translating the data in place,
54 // there is no need to load a FP register with the "corruped" floating
55 // point value. By doing the BIG_ENDIAN test, I can optimize the
56 // routine for big-endian processors so it can be as efficient as
58 static void htond (double &x)
60 if ( sgIsLittleEndian() ) {
64 Double_Overlay = (int *) &x;
65 Holding_Buffer = Double_Overlay [0];
67 Double_Overlay [0] = htonl (Double_Overlay [1]);
68 Double_Overlay [1] = htonl (Holding_Buffer);
73 static void htonf (float &x)
75 if ( sgIsLittleEndian() ) {
79 Float_Overlay = (int *) &x;
80 Holding_Buffer = Float_Overlay [0];
82 Float_Overlay [0] = htonl (Holding_Buffer);
89 FGNativeGUI::FGNativeGUI() {
92 FGNativeGUI::~FGNativeGUI() {
96 // open hailing frequencies
97 bool FGNativeGUI::open() {
99 SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel "
100 << "is already in use, ignoring" );
104 SGIOChannel *io = get_io_channel();
106 if ( ! io->open( get_direction() ) ) {
107 SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
113 cur_fdm_state->_set_Sea_level_radius( SG_EQUATORIAL_RADIUS_FT );
118 void FGProps2NetGUI( FGNetGUI *net ) {
121 // Version sanity checking
122 net->version = FG_NET_GUI_VERSION;
125 net->longitude = cur_fdm_state->get_Longitude();
126 net->latitude = cur_fdm_state->get_Latitude();
127 net->altitude = cur_fdm_state->get_Altitude() * SG_FEET_TO_METER;
128 net->phi = cur_fdm_state->get_Phi();
129 net->theta = cur_fdm_state->get_Theta();
130 net->psi = cur_fdm_state->get_Psi();
133 net->vcas = cur_fdm_state->get_V_calibrated_kts();
134 net->climb_rate = cur_fdm_state->get_Climb_Rate();
137 net->num_tanks = FGNetGUI::FG_MAX_TANKS;
138 for ( i = 0; i < net->num_tanks; ++i ) {
139 SGPropertyNode *node = fgGetNode("/consumables/fuel/tank", i, true);
140 net->fuel_quantity[i] = node->getDoubleValue("level-gal_us");
143 // the following really aren't used in this context
144 net->cur_time = globals->get_time_params()->get_cur_time();
145 net->warp = globals->get_warp();
148 net->dist_nm = current_radiostack->get_dme()->get_dist();
149 net->course_deviation_deg
150 = current_radiostack->get_navcom1()->get_nav_heading()
151 - current_radiostack->get_navcom1()->get_nav_radial();
152 while ( net->course_deviation_deg > 180.0 ) {
153 net->course_deviation_deg -= 360.0;
155 while ( net->course_deviation_deg < -180.0 ) {
156 net->course_deviation_deg += 360.0;
158 if ( fabs(net->course_deviation_deg) > 90.0 )
159 net->course_deviation_deg
160 = ( net->course_deviation_deg<0.0
161 ? -net->course_deviation_deg - 180.0
162 : -net->course_deviation_deg + 180.0 );
163 net->gs_deviation_deg
164 = current_radiostack->get_navcom1()->get_nav_gs_needle_deflection()
167 // Convert the net buffer to network format
168 net->version = htonl(net->version);
170 htond(net->longitude);
171 htond(net->latitude);
172 htonf(net->altitude);
177 htonf(net->climb_rate);
179 for ( i = 0; i < net->num_tanks; ++i ) {
180 htonf(net->fuel_quantity[i]);
182 net->num_tanks = htonl(net->num_tanks);
184 net->cur_time = htonl( net->cur_time );
185 net->warp = htonl( net->warp );
188 htonf(net->course_deviation_deg);
189 htonf(net->gs_deviation_deg);
193 void FGNetGUI2Props( FGNetGUI *net ) {
196 // Convert to the net buffer from network format
197 net->version = ntohl(net->version);
199 htond(net->longitude);
200 htond(net->latitude);
201 htonf(net->altitude);
206 htonf(net->climb_rate);
208 net->num_tanks = htonl(net->num_tanks);
209 for ( i = 0; i < net->num_tanks; ++i ) {
210 htonf(net->fuel_quantity[i]);
213 net->cur_time = ntohl(net->cur_time);
214 net->warp = ntohl(net->warp);
217 htonf(net->course_deviation_deg);
218 htonf(net->gs_deviation_deg);
220 if ( net->version == FG_NET_GUI_VERSION ) {
221 // cout << "pos = " << net->longitude << " " << net->latitude << endl;
222 // cout << "sea level rad = " << cur_fdm_state->get_Sea_level_radius()
224 cur_fdm_state->_updateGeodeticPosition( net->latitude,
227 * SG_METER_TO_FEET );
228 cur_fdm_state->_set_Euler_Angles( net->phi,
232 cur_fdm_state->_set_V_calibrated_kts( net->vcas );
233 cur_fdm_state->_set_Climb_Rate( net->climb_rate );
235 for (i = 0; i < net->num_tanks; ++i ) {
236 SGPropertyNode * node
237 = fgGetNode("/consumables/fuel/tank", i, true);
238 node->setDoubleValue("level-gal_us", net->fuel_quantity[i] );
241 if ( net->cur_time ) {
242 fgSetLong("/sim/time/cur-time-override", net->cur_time);
245 globals->set_warp( net->warp );
248 fgSetDouble( "/radios/dme/distance-nm", net->dist_nm );
249 fgSetDouble( "/radios/nav[0]/heading-needle-deflection",
250 net->course_deviation_deg );
251 fgSetDouble( "/radios/nav[0]/gs-needle-deflection",
252 net->gs_deviation_deg );
254 SG_LOG( SG_IO, SG_ALERT,
255 "Error: version mismatch in FGNetNativeGUI2Props()" );
256 SG_LOG( SG_IO, SG_ALERT,
257 "\tread " << net->version << " need " << FG_NET_GUI_VERSION );
258 SG_LOG( SG_IO, SG_ALERT,
259 "\tNeed to upgrade net_fdm.hxx and recompile." );
264 // process work for this port
265 bool FGNativeGUI::process() {
266 SGIOChannel *io = get_io_channel();
267 int length = sizeof(buf);
269 if ( get_direction() == SG_IO_OUT ) {
270 // cout << "size of cur_fdm_state = " << length << endl;
271 FGProps2NetGUI( &buf );
272 if ( ! io->write( (char *)(& buf), length ) ) {
273 SG_LOG( SG_IO, SG_ALERT, "Error writing data." );
276 } else if ( get_direction() == SG_IO_IN ) {
277 if ( io->get_type() == sgFileType ) {
278 if ( io->read( (char *)(& buf), length ) == length ) {
279 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
280 FGNetGUI2Props( &buf );
283 while ( io->read( (char *)(& buf), length ) == length ) {
284 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
285 FGNetGUI2Props( &buf );
295 bool FGNativeGUI::close() {
296 SGIOChannel *io = get_io_channel();
298 set_enabled( false );
300 if ( ! io->close() ) {