]> git.mxchange.org Git - flightgear.git/blob - src/Network/native_fdm.cxx
c9d6df7967c11ecc8afa18630107588cc998abad
[flightgear.git] / src / Network / native_fdm.cxx
1 // native_fdm.cxx -- FGFS "Native" flight dynamics protocal class
2 //
3 // Written by Curtis Olson, started September 2001.
4 //
5 // Copyright (C) 2001  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
32 #include <FDM/flight.hxx>
33 #include <Main/globals.hxx>
34
35 #include "native_fdm.hxx"
36
37 // FreeBSD works better with this included last ... (?)
38 #if defined(WIN32) && !defined(__CYGWIN__)
39 #  include <windows.h>
40 #else
41 #  include <netinet/in.h>       // htonl() ntohl()
42 #endif
43
44
45 // The function htond is defined this way due to the way some
46 // processors and OSes treat floating point values.  Some will raise
47 // an exception whenever a "bad" floating point value is loaded into a
48 // floating point register.  Solaris is notorious for this, but then
49 // so is LynxOS on the PowerPC.  By translating the data in place,
50 // there is no need to load a FP register with the "corruped" floating
51 // point value.  By doing the BIG_ENDIAN test, I can optimize the
52 // routine for big-endian processors so it can be as efficient as
53 // possible
54 static void htond (double &x)   
55 {
56     if ( sgIsLittleEndian() ) {
57         int    *Double_Overlay;
58         int     Holding_Buffer;
59     
60         Double_Overlay = (int *) &x;
61         Holding_Buffer = Double_Overlay [0];
62     
63         Double_Overlay [0] = htonl (Double_Overlay [1]);
64         Double_Overlay [1] = htonl (Holding_Buffer);
65     } else {
66         return;
67     }
68 }
69
70
71 FGNativeFDM::FGNativeFDM() {
72 }
73
74 FGNativeFDM::~FGNativeFDM() {
75 }
76
77
78 // open hailing frequencies
79 bool FGNativeFDM::open() {
80     if ( is_enabled() ) {
81         SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel " 
82                 << "is already in use, ignoring" );
83         return false;
84     }
85
86     SGIOChannel *io = get_io_channel();
87
88     if ( ! io->open( get_direction() ) ) {
89         SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
90         return false;
91     }
92
93     set_enabled( true );
94
95     cur_fdm_state->_set_Sea_level_radius( SG_EQUATORIAL_RADIUS_FT );
96     return true;
97 }
98
99
100 static void global2net( const FGInterface *global, FGNetFDM *net ) {
101     net->version = FG_NET_FDM_VERSION;
102
103     // positions
104     net->longitude = cur_fdm_state->get_Longitude();
105     net->latitude = cur_fdm_state->get_Latitude();
106     net->altitude = cur_fdm_state->get_Altitude() * SG_FEET_TO_METER;
107     net->phi = cur_fdm_state->get_Phi();
108     net->theta = cur_fdm_state->get_Theta();
109     net->psi = cur_fdm_state->get_Psi();
110
111     // velocities
112     net->vcas = cur_fdm_state->get_V_calibrated_kts();
113     net->climb_rate = cur_fdm_state->get_Climb_Rate();
114
115     // time
116     net->cur_time = globals->get_time_params()->get_cur_time();
117     net->warp = globals->get_warp();
118
119     // Convert the net buffer to network format
120     net->version = htonl(net->version);
121     htond(net->longitude);
122     htond(net->latitude);
123     htond(net->altitude);
124     htond(net->phi);
125     htond(net->theta);
126     htond(net->psi);
127     htond(net->vcas);
128     htond(net->climb_rate);
129     net->cur_time = htonl( net->cur_time );
130     net->warp = htonl( net->warp );
131 }
132
133
134 static void net2global( FGNetFDM *net, FGInterface *global ) {
135
136     // Convert to the net buffer from network format
137     net->version = htonl(net->version);
138     htond(net->longitude);
139     htond(net->latitude);
140     htond(net->altitude);
141     htond(net->phi);
142     htond(net->theta);
143     htond(net->psi);
144     htond(net->vcas);
145     htond(net->climb_rate);
146     net->cur_time = htonl(net->cur_time);
147     net->warp = htonl(net->warp);
148
149     if ( net->version == FG_NET_FDM_VERSION ) {
150         // cout << "pos = " << net->longitude << " " << net->latitude << endl;
151         // cout << "sea level rad = " << cur_fdm_state->get_Sea_level_radius() << endl;
152         cur_fdm_state->_updateGeodeticPosition( net->latitude,
153                                                 net->longitude,
154                                                 net->altitude
155                                                   * SG_METER_TO_FEET );
156         cur_fdm_state->_set_Euler_Angles( net->phi,
157                                           net->theta,
158                                           net->psi );
159         cur_fdm_state->_set_V_calibrated_kts( net->vcas );
160         cur_fdm_state->_set_Climb_Rate( net->climb_rate );
161
162         globals->set_warp( net->warp );
163     } else {
164         SG_LOG( SG_IO, SG_ALERT, "Error: version mismatch in net2global()" );
165         SG_LOG( SG_IO, SG_ALERT,
166                 "\tsomeone needs to upgrade net_fdm.hxx and recompile." );
167     }
168 }
169
170
171 // process work for this port
172 bool FGNativeFDM::process() {
173     SGIOChannel *io = get_io_channel();
174     int length = sizeof(buf);
175
176     if ( get_direction() == SG_IO_OUT ) {
177         // cout << "size of cur_fdm_state = " << length << endl;
178         global2net( cur_fdm_state, &buf );
179         if ( ! io->write( (char *)(& buf), length ) ) {
180             SG_LOG( SG_IO, SG_ALERT, "Error writing data." );
181             return false;
182         }
183     } else if ( get_direction() == SG_IO_IN ) {
184         if ( io->get_type() == sgFileType ) {
185             if ( io->read( (char *)(& buf), length ) == length ) {
186                 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
187                 net2global( &buf, cur_fdm_state );
188             }
189         } else {
190             while ( io->read( (char *)(& buf), length ) == length ) {
191                 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
192                 net2global( &buf, cur_fdm_state );
193             }
194         }
195     }
196
197     return true;
198 }
199
200
201 // close the channel
202 bool FGNativeFDM::close() {
203     SGIOChannel *io = get_io_channel();
204
205     set_enabled( false );
206
207     if ( ! io->close() ) {
208         return false;
209     }
210
211     return true;
212 }