]> git.mxchange.org Git - flightgear.git/blob - src/Network/native_gui.cxx
Fix crashes (activating the route-manager) with a default GPS.
[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 <Main/fg_props.hxx>
34 #include <Main/globals.hxx>
35 #include <Scenery/scenery.hxx>
36 #include <FDM/flightProperties.hxx>
37
38 #include "native_gui.hxx"
39
40 // FreeBSD works better with this included last ... (?)
41 #if defined( _MSC_VER )
42 #  include <windows.h>
43 #elif defined( __MINGW32__ )
44 #  include <winsock2.h>
45 #else
46 #  include <netinet/in.h>       // htonl() ntohl()
47 #endif
48
49
50 // #define FG_USE_NETWORK_BYTE_ORDER
51 #if defined( FG_USE_NETWORK_BYTE_ORDER )
52
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
61 // possible
62 static void htond (double &x)   
63 {
64     if ( sgIsLittleEndian() ) {
65         int    *Double_Overlay;
66         int     Holding_Buffer;
67     
68         Double_Overlay = (int *) &x;
69         Holding_Buffer = Double_Overlay [0];
70     
71         Double_Overlay [0] = htonl (Double_Overlay [1]);
72         Double_Overlay [1] = htonl (Holding_Buffer);
73     } else {
74         return;
75     }
76 }
77 static void htonf (float &x)    
78 {
79     if ( sgIsLittleEndian() ) {
80         int    *Float_Overlay;
81         int     Holding_Buffer;
82     
83         Float_Overlay = (int *) &x;
84         Holding_Buffer = Float_Overlay [0];
85     
86         Float_Overlay [0] = htonl (Holding_Buffer);
87     } else {
88         return;
89     }
90 }
91 #endif
92
93
94 FGNativeGUI::FGNativeGUI() {
95 }
96
97 FGNativeGUI::~FGNativeGUI() {
98 }
99
100
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" );
106         return false;
107     }
108
109     SGIOChannel *io = get_io_channel();
110
111     if ( ! io->open( get_direction() ) ) {
112         SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
113         return false;
114     }
115
116     set_enabled( true );
117
118     fgSetDouble("/position/sea-level-radius-ft", SG_EQUATORIAL_RADIUS_FT);
119     return true;
120 }
121
122
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);
140     unsigned int i;
141
142     static FlightProperties* fdm_state = new FlightProperties;
143
144     // Version sanity checking
145     net->version = FG_NET_GUI_VERSION;
146
147     // Aero parameters
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();
154
155     // Velocities
156     net->vcas = fdm_state->get_V_calibrated_kts();
157     net->climb_rate = fdm_state->get_Climb_Rate();
158
159     // Consumables
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");
164     }
165
166     // Environment
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();
170
171     // Approach
172     net->tuned_freq = nav_freq->getDoubleValue();
173     net->nav_radial = nav_target_radial->getDoubleValue();
174     net->in_range = nav_inrange->getBoolValue();
175
176     if ( nav_loc->getBoolValue() ) {
177         // is an ILS
178         net->dist_nm
179             = nav_gs_dist_signed->getDoubleValue()
180               * SG_METER_TO_NM;
181     } else {
182         // is a VOR
183         net->dist_nm = nav_loc_dist->getDoubleValue()
184             * SG_METER_TO_NM;
185     }
186
187     net->course_deviation_deg
188         = nav_reciprocal_radial->getDoubleValue()
189         - nav_target_radial->getDoubleValue();
190
191     if ( net->course_deviation_deg < -1000.0 
192          || net->course_deviation_deg > 1000.0 )
193     {
194         // Sanity check ...
195         net->course_deviation_deg = 0.0;
196     }
197     while ( net->course_deviation_deg >  180.0 ) {
198         net->course_deviation_deg -= 360.0;
199     }
200     while ( net->course_deviation_deg < -180.0 ) {
201         net->course_deviation_deg += 360.0;
202     }
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 );
208
209     if ( nav_loc->getBoolValue() ) {
210         // is an ILS
211         net->gs_deviation_deg
212             = nav_gs_deflection->getDoubleValue()
213             / 5.0;
214     } else {
215         // is an ILS
216         net->gs_deviation_deg = -9999.0;
217     }
218
219 #if defined( FG_USE_NETWORK_BYTE_ORDER )
220     // Convert the net buffer to network format
221     net->version = htonl(net->version);
222
223     htond(net->longitude);
224     htond(net->latitude);
225     htonf(net->altitude);
226     htonf(net->phi);
227     htonf(net->theta);
228     htonf(net->psi);
229     htonf(net->vcas);
230     htonf(net->climb_rate);
231
232     for ( i = 0; i < net->num_tanks; ++i ) {
233         htonf(net->fuel_quantity[i]);
234     }
235     net->num_tanks = htonl(net->num_tanks);
236
237     net->cur_time = htonl( net->cur_time );
238     net->warp = htonl( net->warp );
239     net->ground_elev = htonl( net->ground_elev );
240
241     htonf(net->tuned_freq);
242     htonf(net->nav_radial);
243     net->in_range = htonl( net->in_range );
244     htonf(net->dist_nm);
245     htonf(net->course_deviation_deg);
246     htonf(net->gs_deviation_deg);
247 #endif
248 }
249
250
251 void FGNetGUI2Props( FGNetGUI *net ) {
252     unsigned int i;
253
254 #if defined( FG_USE_NETWORK_BYTE_ORDER )
255     // Convert to the net buffer from network format
256     net->version = ntohl(net->version);
257
258     htond(net->longitude);
259     htond(net->latitude);
260     htonf(net->altitude);
261     htonf(net->phi);
262     htonf(net->theta);
263     htonf(net->psi);
264     htonf(net->vcas);
265     htonf(net->climb_rate);
266
267     net->num_tanks = htonl(net->num_tanks);
268     for ( i = 0; i < net->num_tanks; ++i ) {
269         htonf(net->fuel_quantity[i]);
270     }
271
272     net->cur_time = ntohl(net->cur_time);
273     net->warp = ntohl(net->warp);
274     net->ground_elev = htonl( net->ground_elev );
275
276     htonf(net->tuned_freq);
277     htonf(net->nav_radial);
278     net->in_range = htonl( net->in_range );
279     htonf(net->dist_nm);
280     htonf(net->course_deviation_deg);
281     htonf(net->gs_deviation_deg);
282 #endif
283
284     if ( net->version == FG_NET_GUI_VERSION ) {
285         FlightProperties fdm_state;
286         
287         // cout << "pos = " << net->longitude << " " << net->latitude << endl;
288         // cout << "sea level rad = " << fdm_state->get_Sea_level_radius()
289         //      << endl;
290   
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);
294
295         fdm_state.set_Euler_Angles( net->phi,
296                                           net->theta,
297                                           net->psi );
298
299         fdm_state.set_V_calibrated_kts( net->vcas );
300         fdm_state.set_Climb_Rate( net->climb_rate );
301
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] );
306         }
307
308         if ( net->cur_time ) {
309             fgSetLong("/sim/time/cur-time-override", net->cur_time);
310         }
311
312         globals->set_warp( net->warp );
313
314         // Approach
315         fgSetDouble( "/instrumentation/nav[0]/frequencies/selected-mhz",
316                      net->tuned_freq );
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 );
323     } else {
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." );
330     }
331 }
332
333
334 // process work for this port
335 bool FGNativeGUI::process() {
336     SGIOChannel *io = get_io_channel();
337     int length = sizeof(buf);
338
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." );
344             return false;
345         }
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 );
351             }
352         } else {
353             while ( io->read( (char *)(& buf), length ) == length ) {
354                 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
355                 FGNetGUI2Props( &buf );
356             }
357         }
358     }
359
360     return true;
361 }
362
363
364 // close the channel
365 bool FGNativeGUI::close() {
366     SGIOChannel *io = get_io_channel();
367
368     set_enabled( false );
369
370     if ( ! io->close() ) {
371         return false;
372     }
373
374     return true;
375 }