]> git.mxchange.org Git - flightgear.git/blob - src/Network/native_gui.cxx
748a500963bd710a7bd6f69d35fadf12c2c84b0a
[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 - curt@flightgear.org
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., 675 Mass Ave, Cambridge, MA 02139, 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 <Cockpit/radiostack.hxx>
34 #include <FDM/flight.hxx>
35 #include <Time/tmp.hxx>
36 #include <Main/fg_props.hxx>
37 #include <Main/globals.hxx>
38 #include <Scenery/scenery.hxx>
39
40 #include "native_gui.hxx"
41
42 // FreeBSD works better with this included last ... (?)
43 #if defined(WIN32) && !defined(__CYGWIN__)
44 #  include <windows.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     cur_fdm_state->_set_Sea_level_radius( SG_EQUATORIAL_RADIUS_FT );
119     return true;
120 }
121
122
123 void FGProps2NetGUI( FGNetGUI *net ) {
124     int i;
125
126     // Version sanity checking
127     net->version = FG_NET_GUI_VERSION;
128
129     // Aero parameters
130     net->longitude = cur_fdm_state->get_Longitude();
131     net->latitude = cur_fdm_state->get_Latitude();
132     net->altitude = cur_fdm_state->get_Altitude() * SG_FEET_TO_METER;
133     net->phi = cur_fdm_state->get_Phi();
134     net->theta = cur_fdm_state->get_Theta();
135     net->psi = cur_fdm_state->get_Psi();
136
137     // Velocities
138     net->vcas = cur_fdm_state->get_V_calibrated_kts();
139     net->climb_rate = cur_fdm_state->get_Climb_Rate();
140
141     // Consumables
142     net->num_tanks = FGNetGUI::FG_MAX_TANKS;
143     for ( i = 0; i < net->num_tanks; ++i ) {
144         SGPropertyNode *node = fgGetNode("/consumables/fuel/tank", i, true);
145         net->fuel_quantity[i] = node->getDoubleValue("level-gal_us");
146     }
147
148     // Environment
149     net->cur_time = globals->get_time_params()->get_cur_time();
150     net->warp = globals->get_warp();
151     net->ground_elev = globals->get_scenery()->get_cur_elev();
152
153     // Approach
154     net->tuned_freq = current_radiostack->get_navcom1()->get_nav_freq();
155     net->nav_radial = current_radiostack->get_navcom1()->get_nav_target_radial();
156     net->in_range = current_radiostack->get_navcom1()->get_nav_inrange();
157
158     if ( current_radiostack->get_navcom1()->get_nav_loc() ) {
159         // is an ILS
160         net->dist_nm
161             = current_radiostack->get_navcom1()->get_nav_gs_dist_signed()
162               * SG_METER_TO_NM;
163     } else {
164         // is a VOR
165         net->dist_nm = current_radiostack->get_navcom1()->get_nav_loc_dist()
166             * SG_METER_TO_NM;
167     }
168
169     net->course_deviation_deg
170         = current_radiostack->get_navcom1()->get_nav_reciprocal_radial()
171         - current_radiostack->get_navcom1()->get_nav_target_radial();
172     while ( net->course_deviation_deg >  180.0 ) {
173         net->course_deviation_deg -= 360.0;
174     }
175     while ( net->course_deviation_deg < -180.0 ) {
176         net->course_deviation_deg += 360.0;
177     }
178     if ( fabs(net->course_deviation_deg) > 90.0 )
179         net->course_deviation_deg
180             = ( net->course_deviation_deg<0.0
181                 ? -net->course_deviation_deg - 180.0
182                 : -net->course_deviation_deg + 180.0 );
183
184     if ( current_radiostack->get_navcom1()->get_nav_loc() ) {
185         // is an ILS
186         net->gs_deviation_deg
187             = current_radiostack->get_navcom1()->get_nav_gs_deflection()
188             / 5.0;
189     } else {
190         // is an ILS
191         net->gs_deviation_deg = -9999.0;
192     }
193
194 #if defined( FG_USE_NETWORK_BYTE_ORDER )
195     // Convert the net buffer to network format
196     net->version = htonl(net->version);
197
198     htond(net->longitude);
199     htond(net->latitude);
200     htonf(net->altitude);
201     htonf(net->phi);
202     htonf(net->theta);
203     htonf(net->psi);
204     htonf(net->vcas);
205     htonf(net->climb_rate);
206
207     for ( i = 0; i < net->num_tanks; ++i ) {
208         htonf(net->fuel_quantity[i]);
209     }
210     net->num_tanks = htonl(net->num_tanks);
211
212     net->cur_time = htonl( net->cur_time );
213     net->warp = htonl( net->warp );
214     net->ground_elev = htonl( net->ground_elev );
215
216     htonf(net->tuned_freq);
217     htonf(net->nav_radial);
218     net->in_range = htonl(net->in_range);
219     htonf(net->dist_nm);
220     htonf(net->course_deviation_deg);
221     htonf(net->gs_deviation_deg);
222 #endif
223 }
224
225
226 void FGNetGUI2Props( FGNetGUI *net ) {
227     int i;
228
229 #if defined( FG_USE_NETWORK_BYTE_ORDER )
230     // Convert to the net buffer from network format
231     net->version = ntohl(net->version);
232
233     htond(net->longitude);
234     htond(net->latitude);
235     htonf(net->altitude);
236     htonf(net->phi);
237     htonf(net->theta);
238     htonf(net->psi);
239     htonf(net->vcas);
240     htonf(net->climb_rate);
241
242     net->num_tanks = htonl(net->num_tanks);
243     for ( i = 0; i < net->num_tanks; ++i ) {
244         htonf(net->fuel_quantity[i]);
245     }
246
247     net->cur_time = ntohl(net->cur_time);
248     net->warp = ntohl(net->warp);
249     net->ground_elev = htonl( net->ground_elev );
250
251     htonf(net->tuned_freq);
252     net->in_range = htonl(net->in_range);
253     htonf(net->dist_nm);
254     htonf(net->course_deviation_deg);
255     htonf(net->gs_deviation_deg);
256 #endif
257
258     if ( net->version == FG_NET_GUI_VERSION ) {
259         // cout << "pos = " << net->longitude << " " << net->latitude << endl;
260         // cout << "sea level rad = " << cur_fdm_state->get_Sea_level_radius()
261         //      << endl;
262         cur_fdm_state->_updateGeodeticPosition( net->latitude,
263                                                 net->longitude,
264                                                 net->altitude
265                                                   * SG_METER_TO_FEET );
266         cur_fdm_state->_set_Euler_Angles( net->phi,
267                                           net->theta,
268                                           net->psi );
269
270         cur_fdm_state->_set_V_calibrated_kts( net->vcas );
271         cur_fdm_state->_set_Climb_Rate( net->climb_rate );
272
273         for (i = 0; i < net->num_tanks; ++i ) {
274             SGPropertyNode * node
275                 = fgGetNode("/consumables/fuel/tank", i, true);
276             node->setDoubleValue("level-gal_us", net->fuel_quantity[i] );
277         }
278
279         if ( net->cur_time ) {
280             fgSetLong("/sim/time/cur-time-override", net->cur_time);
281         }
282
283         globals->set_warp( net->warp );
284         globals->get_scenery()->set_cur_elev( net->ground_elev );
285
286         // Approach
287         fgSetDouble( "/radios/nav[0]/frequencies/selected-mhz",
288                      net->tuned_freq );
289         fgSetBool( "/radios/nav[0]/in-range", net->in_range );
290         fgSetDouble( "/radios/dme/distance-nm", net->dist_nm );
291         fgSetDouble( "/radios/nav[0]/heading-needle-deflection",
292                      net->course_deviation_deg );
293         fgSetDouble( "/radios/nav[0]/gs-needle-deflection",
294                      net->gs_deviation_deg );
295     } else {
296         SG_LOG( SG_IO, SG_ALERT,
297                 "Error: version mismatch in FGNetNativeGUI2Props()" );
298         SG_LOG( SG_IO, SG_ALERT,
299                 "\tread " << net->version << " need " << FG_NET_GUI_VERSION );
300         SG_LOG( SG_IO, SG_ALERT,
301                 "\tNeed to upgrade net_fdm.hxx and recompile." );
302     }
303 }
304
305
306 // process work for this port
307 bool FGNativeGUI::process() {
308     SGIOChannel *io = get_io_channel();
309     int length = sizeof(buf);
310
311     if ( get_direction() == SG_IO_OUT ) {
312         // cout << "size of cur_fdm_state = " << length << endl;
313         FGProps2NetGUI( &buf );
314         if ( ! io->write( (char *)(& buf), length ) ) {
315             SG_LOG( SG_IO, SG_ALERT, "Error writing data." );
316             return false;
317         }
318     } else if ( get_direction() == SG_IO_IN ) {
319         if ( io->get_type() == sgFileType ) {
320             if ( io->read( (char *)(& buf), length ) == length ) {
321                 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
322                 FGNetGUI2Props( &buf );
323             }
324         } else {
325             while ( io->read( (char *)(& buf), length ) == length ) {
326                 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
327                 FGNetGUI2Props( &buf );
328             }
329         }
330     }
331
332     return true;
333 }
334
335
336 // close the channel
337 bool FGNativeGUI::close() {
338     SGIOChannel *io = get_io_channel();
339
340     set_enabled( false );
341
342     if ( ! io->close() ) {
343         return false;
344     }
345
346     return true;
347 }