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