]> git.mxchange.org Git - flightgear.git/blob - src/Network/native_gui.cxx
9471cc588d2dbf89102635329ad3760d84070851
[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 // 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
57 // possible
58 static void htond (double &x)   
59 {
60     if ( sgIsLittleEndian() ) {
61         int    *Double_Overlay;
62         int     Holding_Buffer;
63     
64         Double_Overlay = (int *) &x;
65         Holding_Buffer = Double_Overlay [0];
66     
67         Double_Overlay [0] = htonl (Double_Overlay [1]);
68         Double_Overlay [1] = htonl (Holding_Buffer);
69     } else {
70         return;
71     }
72 }
73 static void htonf (float &x)    
74 {
75     if ( sgIsLittleEndian() ) {
76         int    *Float_Overlay;
77         int     Holding_Buffer;
78     
79         Float_Overlay = (int *) &x;
80         Holding_Buffer = Float_Overlay [0];
81     
82         Float_Overlay [0] = htonl (Holding_Buffer);
83     } else {
84         return;
85     }
86 }
87
88
89 FGNativeGUI::FGNativeGUI() {
90 }
91
92 FGNativeGUI::~FGNativeGUI() {
93 }
94
95
96 // open hailing frequencies
97 bool FGNativeGUI::open() {
98     if ( is_enabled() ) {
99         SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel " 
100                 << "is already in use, ignoring" );
101         return false;
102     }
103
104     SGIOChannel *io = get_io_channel();
105
106     if ( ! io->open( get_direction() ) ) {
107         SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
108         return false;
109     }
110
111     set_enabled( true );
112
113     cur_fdm_state->_set_Sea_level_radius( SG_EQUATORIAL_RADIUS_FT );
114     return true;
115 }
116
117
118 void FGProps2NetGUI( FGNetGUI *net ) {
119     int i;
120
121     // Version sanity checking
122     net->version = FG_NET_GUI_VERSION;
123
124     // Aero parameters
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();
131
132     // Velocities
133     net->vcas = cur_fdm_state->get_V_calibrated_kts();
134     net->climb_rate = cur_fdm_state->get_Climb_Rate();
135
136     // Consumables
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");
141     }
142
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();
146
147     // Approach
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;
154     }
155     while ( net->course_deviation_deg < -180.0 ) {
156         net->course_deviation_deg += 360.0;
157     }
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()
165         / 5.0;
166
167     // Convert the net buffer to network format
168     net->version = htonl(net->version);
169
170     htond(net->longitude);
171     htond(net->latitude);
172     htonf(net->altitude);
173     htonf(net->phi);
174     htonf(net->theta);
175     htonf(net->psi);
176     htonf(net->vcas);
177     htonf(net->climb_rate);
178
179     for ( i = 0; i < net->num_tanks; ++i ) {
180         htonf(net->fuel_quantity[i]);
181     }
182     net->num_tanks = htonl(net->num_tanks);
183
184     net->cur_time = htonl( net->cur_time );
185     net->warp = htonl( net->warp );
186
187     htonf(net->dist_nm);
188     htonf(net->course_deviation_deg);
189     htonf(net->gs_deviation_deg);
190 }
191
192
193 void FGNetGUI2Props( FGNetGUI *net ) {
194     int i;
195
196     // Convert to the net buffer from network format
197     net->version = ntohl(net->version);
198
199     htond(net->longitude);
200     htond(net->latitude);
201     htonf(net->altitude);
202     htonf(net->phi);
203     htonf(net->theta);
204     htonf(net->psi);
205     htonf(net->vcas);
206     htonf(net->climb_rate);
207
208     net->num_tanks = htonl(net->num_tanks);
209     for ( i = 0; i < net->num_tanks; ++i ) {
210         htonf(net->fuel_quantity[i]);
211     }
212
213     net->cur_time = ntohl(net->cur_time);
214     net->warp = ntohl(net->warp);
215
216     htonf(net->dist_nm);
217     htonf(net->course_deviation_deg);
218     htonf(net->gs_deviation_deg);
219
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()
223         //      << endl;
224         cur_fdm_state->_updateGeodeticPosition( net->latitude,
225                                                 net->longitude,
226                                                 net->altitude
227                                                   * SG_METER_TO_FEET );
228         cur_fdm_state->_set_Euler_Angles( net->phi,
229                                           net->theta,
230                                           net->psi );
231
232         cur_fdm_state->_set_V_calibrated_kts( net->vcas );
233         cur_fdm_state->_set_Climb_Rate( net->climb_rate );
234
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] );
239         }
240
241         if ( net->cur_time ) {
242             fgSetLong("/sim/time/cur-time-override", net->cur_time);
243         }
244
245         globals->set_warp( net->warp );
246
247         // Approach
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 );
253     } else {
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." );
260     }
261 }
262
263
264 // process work for this port
265 bool FGNativeGUI::process() {
266     SGIOChannel *io = get_io_channel();
267     int length = sizeof(buf);
268
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." );
274             return false;
275         }
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 );
281             }
282         } else {
283             while ( io->read( (char *)(& buf), length ) == length ) {
284                 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
285                 FGNetGUI2Props( &buf );
286             }
287         }
288     }
289
290     return true;
291 }
292
293
294 // close the channel
295 bool FGNativeGUI::close() {
296     SGIOChannel *io = get_io_channel();
297
298     set_enabled( false );
299
300     if ( ! io->close() ) {
301         return false;
302     }
303
304     return true;
305 }