]> git.mxchange.org Git - flightgear.git/blob - src/Network/native_ctrls.cxx
404fc994a9c92dcc52fe66e23194eea752b904d9
[flightgear.git] / src / Network / native_ctrls.cxx
1 // native_ctrls.cxx -- FGFS "Native" controls I/O class
2 //
3 // Written by Curtis Olson, started July 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/iochannel.hxx>
30 #include <simgear/io/lowlevel.hxx> // endian tests
31
32 #include <FDM/flight.hxx>
33 #include <Main/fg_props.hxx>
34 #include <Scenery/scenery.hxx>  // ground elevation
35
36 #include "native_ctrls.hxx"
37
38 // FreeBSD works better with this included last ... (?)
39 #if defined(WIN32) && !defined(__CYGWIN__)
40 #  include <windows.h>
41 #else
42 #  include <netinet/in.h>       // htonl() ntohl()
43 #endif
44
45
46 FGNativeCtrls::FGNativeCtrls() {
47 }
48
49 FGNativeCtrls::~FGNativeCtrls() {
50 }
51
52
53 // open hailing frequencies
54 bool FGNativeCtrls::open() {
55     if ( is_enabled() ) {
56         SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel " 
57                 << "is already in use, ignoring" );
58         return false;
59     }
60
61     SGIOChannel *io = get_io_channel();
62
63     if ( ! io->open( get_direction() ) ) {
64         SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
65         return false;
66     }
67
68     set_enabled( true );
69
70     return true;
71 }
72
73
74 // The function htond is defined this way due to the way some
75 // processors and OSes treat floating point values.  Some will raise
76 // an exception whenever a "bad" floating point value is loaded into a
77 // floating point register.  Solaris is notorious for this, but then
78 // so is LynxOS on the PowerPC.  By translating the data in place,
79 // there is no need to load a FP register with the "corruped" floating
80 // point value.  By doing the BIG_ENDIAN test, I can optimize the
81 // routine for big-endian processors so it can be as efficient as
82 // possible
83 static void htond (double &x)   
84 {
85     if ( sgIsLittleEndian() ) {
86         int    *Double_Overlay;
87         int     Holding_Buffer;
88     
89         Double_Overlay = (int *) &x;
90         Holding_Buffer = Double_Overlay [0];
91     
92         Double_Overlay [0] = htonl (Double_Overlay [1]);
93         Double_Overlay [1] = htonl (Holding_Buffer);
94     } else {
95         return;
96     }
97 }
98
99
100 // Populate the FGNetCtrls structure from the property tree.
101 void FGProps2NetCtrls( FGNetCtrls *net ) {
102     int i;
103
104     SGPropertyNode * node = fgGetNode("/controls", true);
105
106     // fill in values
107     net->version = FG_NET_CTRLS_VERSION;
108     net->aileron = node->getDoubleValue( "aileron" );
109     net->elevator = node->getDoubleValue( "elevator" );
110     net->elevator_trim = node->getDoubleValue( "elevator-trim" );
111     net->rudder = node->getDoubleValue( "rudder" );
112     net->flaps = node->getDoubleValue( "flaps" );
113     net->flaps_power
114             = node->getDoubleValue( "/systems/electrical/outputs/flaps",
115                                     1.0 ) >= 1.0;
116     net->num_engines = FGNetCtrls::FG_MAX_ENGINES;
117     for ( i = 0; i < FGNetCtrls::FG_MAX_ENGINES; ++i ) {
118         net->throttle[i] = node->getDoubleValue( "throttle", 0.0 );
119         net->mixture[i] = node->getDoubleValue( "mixture", 0.0 );
120         net->fuel_pump_power[i]
121             = node->getDoubleValue( "/systems/electrical/outputs/fuel-pump",
122                                     1.0 ) >= 1.0;
123         net->prop_advance[i] = node->getDoubleValue( "propeller-pitch", 0.0 );
124         net->magnetos[i] = node->getIntValue( "magnetos", 0 );
125         if ( i == 0 ) {
126           // cout << "Magnetos -> " << node->getIntValue( "magnetos", 0 );
127         }
128         net->starter_power[i]
129             = node->getDoubleValue( "/systems/electrical/outputs/starter",
130                                     1.0 ) >= 1.0;
131         if ( i == 0 ) {
132           // cout << " Starter -> " << node->getIntValue( "stater", false )
133           //      << endl;
134         }
135     }
136     net->num_tanks = FGNetCtrls::FG_MAX_TANKS;
137     for ( i = 0; i < FGNetCtrls::FG_MAX_TANKS; ++i ) {
138         if ( node->getChild("fuel-selector", i) != 0 ) {
139             net->fuel_selector[i]
140                 = node->getChild("fuel-selector", i)->getDoubleValue();
141         } else {
142             net->fuel_selector[i] = false;
143         }
144     }
145     net->num_wheels = FGNetCtrls::FG_MAX_WHEELS;
146     for ( i = 0; i < FGNetCtrls::FG_MAX_WHEELS; ++i ) {
147         if ( node->getChild("brakes", i) != 0 ) {
148             if ( node->getChild("parking-brake")->getDoubleValue() > 0.0 ) {
149                 net->brake[i] = 1.0;
150            } else {
151                 net->brake[i]
152                     = node->getChild("brakes", i)->getDoubleValue();
153             }
154         } else {
155             net->brake[i] = 0.0;
156         }
157     }
158
159     node = fgGetNode("/controls/switches", true);
160     net->master_bat = node->getChild("master-bat")->getBoolValue();
161     net->master_alt = node->getChild("master-alt")->getBoolValue();
162     net->master_avionics = node->getChild("master-avionics")->getBoolValue();
163
164     net->wind_speed_kt = fgGetDouble("/environment/wind-speed-kt");
165     net->wind_dir_deg = fgGetDouble("/environment/wind-from-heading-deg");
166
167     // cur_fdm_state->get_ground_elev_ft() is what we want ... this
168     // reports the altitude of the aircraft.
169     // "/environment/ground-elevation-m" reports the ground elevation
170     // of the current view point which could change substantially if
171     // the user is switching views.
172     net->hground = cur_fdm_state->get_ground_elev_ft() * SG_FEET_TO_METER;
173     net->magvar = fgGetDouble("/environment/magnetic-variation-deg");
174     net->speedup = fgGetInt("/sim/speed-up");
175     net->freeze = 0;
176     if ( fgGetBool("/sim/freeze/master") ) {
177         net->freeze |= 0x01;
178     }
179     if ( fgGetBool("/sim/freeze/position") ) {
180         net->freeze |= 0x02;
181     }
182     if ( fgGetBool("/sim/freeze/fuel") ) {
183         net->freeze |= 0x04;
184     }
185
186     // convert to network byte order
187     net->version = htonl(net->version);
188     htond(net->aileron);
189     htond(net->elevator);
190     htond(net->elevator_trim);
191     htond(net->rudder);
192     htond(net->flaps);
193     net->flaps_power = htonl(net->flaps_power);
194     for ( i = 0; i < FGNetCtrls::FG_MAX_ENGINES; ++i ) {
195         htond(net->throttle[i]);
196         htond(net->mixture[i]);
197         net->fuel_pump_power[i] = htonl(net->fuel_pump_power[i]);
198         htond(net->prop_advance[i]);
199         net->magnetos[i] = htonl(net->magnetos[i]);
200         net->starter_power[i] = htonl(net->starter_power[i]);
201     }
202     net->num_engines = htonl(net->num_engines);
203     for ( i = 0; i < FGNetCtrls::FG_MAX_TANKS; ++i ) {
204         net->fuel_selector[i] = htonl(net->fuel_selector[i]);
205     }
206     net->num_tanks = htonl(net->num_tanks);
207     for ( i = 0; i < FGNetCtrls::FG_MAX_WHEELS; ++i ) {
208         htond(net->brake[i]);
209     }
210     net->num_wheels = htonl(net->num_wheels);
211     net->gear_handle = htonl(net->gear_handle);
212     net->master_bat = htonl(net->master_bat);
213     net->master_alt = htonl(net->master_alt);
214     net->master_avionics = htonl(net->master_avionics);
215     htond(net->wind_speed_kt);
216     htond(net->wind_dir_deg);
217     htond(net->hground);
218     htond(net->magvar);
219     net->speedup = htonl(net->speedup);
220     net->freeze = htonl(net->freeze);
221 }
222
223
224 // Update the property tree from the FGNetCtrls structure.
225 void FGNetCtrls2Props( FGNetCtrls *net ) {
226     int i;
227
228     SGPropertyNode * node = fgGetNode("/controls", true);
229
230     // convert from network byte order
231     net->version = htonl(net->version);
232     htond(net->aileron);
233     htond(net->elevator);
234     htond(net->elevator_trim);
235     htond(net->rudder);
236     htond(net->flaps);
237     net->flaps_power = htonl(net->flaps_power);
238     net->num_engines = htonl(net->num_engines);
239     for ( i = 0; i < net->num_engines; ++i ) {
240         net->magnetos[i] = htonl(net->magnetos[i]);
241         net->starter_power[i] = htonl(net->starter_power[i]);
242         htond(net->throttle[i]);
243         htond(net->mixture[i]);
244         net->fuel_pump_power[i] = htonl(net->fuel_pump_power[i]);
245         htond(net->prop_advance[i]);
246     }
247     net->num_tanks = htonl(net->num_tanks);
248     for ( i = 0; i < net->num_tanks; ++i ) {
249         net->fuel_selector[i] = htonl(net->fuel_selector[i]);
250     }
251     net->num_wheels = htonl(net->num_wheels);
252     for ( i = 0; i < net->num_wheels; ++i ) {
253         htond(net->brake[i]);
254     }
255     net->gear_handle = htonl(net->gear_handle);
256     net->master_bat = htonl(net->master_bat);
257     net->master_alt = htonl(net->master_alt);
258     net->master_avionics = htonl(net->master_avionics);
259     htond(net->wind_speed_kt);
260     htond(net->wind_dir_deg);
261     htond(net->hground);
262     htond(net->magvar);
263     net->speedup = htonl(net->speedup);
264     net->freeze = htonl(net->freeze);
265
266     if ( net->version != FG_NET_CTRLS_VERSION ) {
267         SG_LOG( SG_IO, SG_ALERT,
268                 "Version mismatch with raw controls packet format." );
269         SG_LOG( SG_IO, SG_ALERT,
270                 "FlightGear needs version = " << FG_NET_CTRLS_VERSION
271                 << " but is receiving version = "  << net->version );
272     }
273
274     node->setDoubleValue( "aileron", net->aileron );
275     node->setDoubleValue( "elevator", net->elevator );
276     node->setDoubleValue( "elevator-trim", net->elevator_trim );
277     node->setDoubleValue( "rudder", net->rudder );
278     node->setDoubleValue( "flaps", net->flaps );
279
280     fgSetBool( "/systems/electrical/outputs/flaps", net->flaps_power );
281
282     for ( i = 0; i < FGNetCtrls::FG_MAX_ENGINES; ++i ) {
283         node->getChild( "throttle", i )->setDoubleValue( net->throttle[i] );
284         node->getChild( "mixture", i )->setDoubleValue( net->mixture[i] );
285         node->getChild( "propeller-pitch", i )
286             ->setDoubleValue( net->prop_advance[i] );
287         node->getChild( "magnetos", i )->setDoubleValue( net->magnetos[i] );
288     }
289
290     fgSetBool( "/systems/electrical/outputs/fuel-pump",
291                net->fuel_pump_power[0] );
292     fgSetBool( "/systems/electrical/outputs/starter",
293                net->starter_power[0] );
294
295     for ( i = 0; i < FGNetCtrls::FG_MAX_TANKS; ++i ) {
296         node->getChild( "fuel-selector", i )
297             ->setBoolValue( net->fuel_selector[i] );
298     }
299     for ( i = 0; i < FGNetCtrls::FG_MAX_WHEELS; ++i ) {
300         node->getChild( "brakes", i )->setDoubleValue( net->brake[i] );
301     }
302
303     node->setBoolValue( "gear-down", net->gear_handle );
304
305     node = fgGetNode( "/controls/switches", true );
306     node->setBoolValue( "master-bat", net->master_bat );
307     node->setBoolValue( "master-alt", net->master_alt );
308     node->setBoolValue( "master-avionics", net->master_avionics );
309     
310     node = fgGetNode( "/environment", true );
311     node->setBoolValue( "wind-speed-kt", net->wind_speed_kt );
312     node->setBoolValue( "wind-from-heading-deg", net->wind_dir_deg );
313     node->setBoolValue( "magnetic-variation-deg", net->magvar );
314
315     // ground elevation ???
316
317     fgSetInt( "/sim/speed-up", net->speedup );
318
319     node = fgGetNode( "/sim/freeze", true );
320     node->setBoolValue( "master", net->freeze & 0x01 );
321     node->setBoolValue( "position", net->freeze & 0x02 );
322     node->setBoolValue( "fuel", net->freeze & 0x04 );
323 }
324
325
326 // process work for this port
327 bool FGNativeCtrls::process() {
328     SGIOChannel *io = get_io_channel();
329     int length = sizeof(FGNetCtrls);
330
331     if ( get_direction() == SG_IO_OUT ) {
332         // cout << "size of cur_fdm_state = " << length << endl;
333
334         FGProps2NetCtrls( &net_ctrls );
335
336         if ( ! io->write( (char *)(& net_ctrls), length ) ) {
337             SG_LOG( SG_IO, SG_ALERT, "Error writing data." );
338             return false;
339         }
340     } else if ( get_direction() == SG_IO_IN ) {
341         if ( io->get_type() == sgFileType ) {
342             if ( io->read( (char *)(& net_ctrls), length ) == length ) {
343                 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
344                 FGNetCtrls2Props( &net_ctrls );
345             }
346         } else {
347             while ( io->read( (char *)(& net_ctrls), length ) == length ) {
348                 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
349                 FGNetCtrls2Props( &net_ctrls );
350             }
351         }
352     }
353
354     return true;
355 }
356
357
358 // close the channel
359 bool FGNativeCtrls::close() {
360     SGIOChannel *io = get_io_channel();
361
362     set_enabled( false );
363
364     if ( ! io->close() ) {
365         return false;
366     }
367
368     return true;
369 }