]> git.mxchange.org Git - flightgear.git/blob - src/Network/native_gui.cxx
Logging: less verbose panel loading.
[flightgear.git] / src / Network / native_gui.cxx
1 // native_gui.cxx -- FGFS external gui data export class
2 //
3 // Written by Curtis Olson, started January 2002.
4 //
5 // Copyright (C) 2002  Curtis L. Olson - http://www.flightgear.org/~curt
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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 //
21 // $Id$
22
23
24 #ifdef HAVE_CONFIG_H
25 #  include <config.h>
26 #endif
27
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>
32
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>
38
39 #include "native_gui.hxx"
40
41 // FreeBSD works better with this included last ... (?)
42 #if defined(WIN32) && !defined(__CYGWIN__)
43 #  include <windows.h>
44 #else
45 #  include <netinet/in.h>       // htonl() ntohl()
46 #endif
47
48
49 // #define FG_USE_NETWORK_BYTE_ORDER
50 #if defined( FG_USE_NETWORK_BYTE_ORDER )
51
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
60 // possible
61 static void htond (double &x)   
62 {
63     if ( sgIsLittleEndian() ) {
64         int    *Double_Overlay;
65         int     Holding_Buffer;
66     
67         Double_Overlay = (int *) &x;
68         Holding_Buffer = Double_Overlay [0];
69     
70         Double_Overlay [0] = htonl (Double_Overlay [1]);
71         Double_Overlay [1] = htonl (Holding_Buffer);
72     } else {
73         return;
74     }
75 }
76 static void htonf (float &x)    
77 {
78     if ( sgIsLittleEndian() ) {
79         int    *Float_Overlay;
80         int     Holding_Buffer;
81     
82         Float_Overlay = (int *) &x;
83         Holding_Buffer = Float_Overlay [0];
84     
85         Float_Overlay [0] = htonl (Holding_Buffer);
86     } else {
87         return;
88     }
89 }
90 #endif
91
92
93 FGNativeGUI::FGNativeGUI() {
94 }
95
96 FGNativeGUI::~FGNativeGUI() {
97 }
98
99
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" );
105         return false;
106     }
107
108     SGIOChannel *io = get_io_channel();
109
110     if ( ! io->open( get_direction() ) ) {
111         SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
112         return false;
113     }
114
115     set_enabled( true );
116
117     cur_fdm_state->_set_Sea_level_radius( SG_EQUATORIAL_RADIUS_FT );
118     return true;
119 }
120
121
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);
139     unsigned int i;
140
141     // Version sanity checking
142     net->version = FG_NET_GUI_VERSION;
143
144     // Aero parameters
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();
151
152     // Velocities
153     net->vcas = cur_fdm_state->get_V_calibrated_kts();
154     net->climb_rate = cur_fdm_state->get_Climb_Rate();
155
156     // Consumables
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");
161     }
162
163     // Environment
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();
167
168     // Approach
169     net->tuned_freq = nav_freq->getDoubleValue();
170     net->nav_radial = nav_target_radial->getDoubleValue();
171     net->in_range = nav_inrange->getBoolValue();
172
173     if ( nav_loc->getBoolValue() ) {
174         // is an ILS
175         net->dist_nm
176             = nav_gs_dist_signed->getDoubleValue()
177               * SG_METER_TO_NM;
178     } else {
179         // is a VOR
180         net->dist_nm = nav_loc_dist->getDoubleValue()
181             * SG_METER_TO_NM;
182     }
183
184     net->course_deviation_deg
185         = nav_reciprocal_radial->getDoubleValue()
186         - nav_target_radial->getDoubleValue();
187
188     if ( net->course_deviation_deg < -1000.0 
189          || net->course_deviation_deg > 1000.0 )
190     {
191         // Sanity check ...
192         net->course_deviation_deg = 0.0;
193     }
194     while ( net->course_deviation_deg >  180.0 ) {
195         net->course_deviation_deg -= 360.0;
196     }
197     while ( net->course_deviation_deg < -180.0 ) {
198         net->course_deviation_deg += 360.0;
199     }
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 );
205
206     if ( nav_loc->getBoolValue() ) {
207         // is an ILS
208         net->gs_deviation_deg
209             = nav_gs_deflection->getDoubleValue()
210             / 5.0;
211     } else {
212         // is an ILS
213         net->gs_deviation_deg = -9999.0;
214     }
215
216 #if defined( FG_USE_NETWORK_BYTE_ORDER )
217     // Convert the net buffer to network format
218     net->version = htonl(net->version);
219
220     htond(net->longitude);
221     htond(net->latitude);
222     htonf(net->altitude);
223     htonf(net->phi);
224     htonf(net->theta);
225     htonf(net->psi);
226     htonf(net->vcas);
227     htonf(net->climb_rate);
228
229     for ( i = 0; i < net->num_tanks; ++i ) {
230         htonf(net->fuel_quantity[i]);
231     }
232     net->num_tanks = htonl(net->num_tanks);
233
234     net->cur_time = htonl( net->cur_time );
235     net->warp = htonl( net->warp );
236     net->ground_elev = htonl( net->ground_elev );
237
238     htonf(net->tuned_freq);
239     htonf(net->nav_radial);
240     net->in_range = htonl( net->in_range );
241     htonf(net->dist_nm);
242     htonf(net->course_deviation_deg);
243     htonf(net->gs_deviation_deg);
244 #endif
245 }
246
247
248 void FGNetGUI2Props( FGNetGUI *net ) {
249     unsigned int i;
250
251 #if defined( FG_USE_NETWORK_BYTE_ORDER )
252     // Convert to the net buffer from network format
253     net->version = ntohl(net->version);
254
255     htond(net->longitude);
256     htond(net->latitude);
257     htonf(net->altitude);
258     htonf(net->phi);
259     htonf(net->theta);
260     htonf(net->psi);
261     htonf(net->vcas);
262     htonf(net->climb_rate);
263
264     net->num_tanks = htonl(net->num_tanks);
265     for ( i = 0; i < net->num_tanks; ++i ) {
266         htonf(net->fuel_quantity[i]);
267     }
268
269     net->cur_time = ntohl(net->cur_time);
270     net->warp = ntohl(net->warp);
271     net->ground_elev = htonl( net->ground_elev );
272
273     htonf(net->tuned_freq);
274     htonf(net->nav_radial);
275     net->in_range = htonl( net->in_range );
276     htonf(net->dist_nm);
277     htonf(net->course_deviation_deg);
278     htonf(net->gs_deviation_deg);
279 #endif
280
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()
284         //      << endl;
285         cur_fdm_state->_updateGeodeticPosition( net->latitude,
286                                                 net->longitude,
287                                                 net->altitude
288                                                   * SG_METER_TO_FEET );
289         cur_fdm_state->_set_Euler_Angles( net->phi,
290                                           net->theta,
291                                           net->psi );
292
293         cur_fdm_state->_set_V_calibrated_kts( net->vcas );
294         cur_fdm_state->_set_Climb_Rate( net->climb_rate );
295
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] );
300         }
301
302         if ( net->cur_time ) {
303             fgSetLong("/sim/time/cur-time-override", net->cur_time);
304         }
305
306         globals->set_warp( net->warp );
307
308         // Approach
309         fgSetDouble( "/instrumentation/nav[0]/frequencies/selected-mhz",
310                      net->tuned_freq );
311         fgSetBool( "/instrumentation/nav[0]/in-range", net->in_range );
312         fgSetDouble( "/instrumentation/dme/indicated-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 );
317     } else {
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." );
324     }
325 }
326
327
328 // process work for this port
329 bool FGNativeGUI::process() {
330     SGIOChannel *io = get_io_channel();
331     int length = sizeof(buf);
332
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." );
338             return false;
339         }
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 );
345             }
346         } else {
347             while ( io->read( (char *)(& buf), length ) == length ) {
348                 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
349                 FGNetGUI2Props( &buf );
350             }
351         }
352     }
353
354     return true;
355 }
356
357
358 // close the channel
359 bool FGNativeGUI::close() {
360     SGIOChannel *io = get_io_channel();
361
362     set_enabled( false );
363
364     if ( ! io->close() ) {
365         return false;
366     }
367
368     return true;
369 }