]> git.mxchange.org Git - flightgear.git/blob - src/Network/native_ctrls.cxx
f53dce08f9afb0c08e210145165eafd45da44c3d
[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, bool net_byte_order ) {
102     int i;
103     bool b; 
104     SGPropertyNode * node;
105     SGPropertyNode * tempnode;
106
107     // fill in values
108     node  = fgGetNode("/controls/flight", true);
109     net->version = FG_NET_CTRLS_VERSION;
110     net->aileron = node->getDoubleValue( "aileron" );
111     net->elevator = node->getDoubleValue( "elevator" );
112     net->elevator_trim = node->getDoubleValue( "elevator-trim" );
113     net->rudder = node->getDoubleValue( "rudder" );
114     net->flaps = node->getDoubleValue( "flaps" );
115     node = fgGetNode("/controls", true); 
116     net->flaps_power
117             = node->getDoubleValue( "/systems/electrical/outputs/flaps",
118                                     1.0 ) >= 1.0;
119     net->num_engines = FGNetCtrls::FG_MAX_ENGINES;
120     for ( i = 0; i < FGNetCtrls::FG_MAX_ENGINES; ++i ) {
121         node = fgGetNode("/controls/engines/engine", i );
122         net->throttle[i] = node->getDoubleValue( "throttle", 0.0 );
123         net->mixture[i] = node->getDoubleValue( "mixture", 0.0 );
124         net->prop_advance[i] = node->getDoubleValue( "propeller-pitch", 0.0 );
125         net->magnetos[i] = node->getIntValue( "magnetos", 0 );
126         if ( i == 0 ) {
127           // cout << "Magnetos -> " << node->getIntValue( "magnetos", 0 );
128         }
129         if ( i == 0 ) {
130           // cout << "Starter -> " << node->getIntValue( "starter", false )
131           //      << endl;
132         }
133         node = fgGetNode("/controls", true);
134         net->fuel_pump_power[i]
135             = node->getDoubleValue( "/systems/electrical/outputs/fuel-pump",
136                                     1.0 ) >= 1.0;
137
138         net->starter_power[i]
139             = node->getDoubleValue( "/systems/electrical/outputs/starter",
140                                     1.0 ) >= 1.0;
141     }
142     net->num_tanks = FGNetCtrls::FG_MAX_TANKS;
143     for ( i = 0; i < FGNetCtrls::FG_MAX_TANKS; ++i ) {
144         node = fgGetNode("/controls/fuel/tank", i);
145         if ( node->getChild("fuel-selector") != 0 ) {
146             net->fuel_selector[i]
147                 = node->getChild("fuel-selector")->getBoolValue();
148         } else {
149             net->fuel_selector[i] = false;
150         }
151     }
152     net->num_wheels = FGNetCtrls::FG_MAX_WHEELS;
153     tempnode = fgGetNode("/controls/gear", true);
154     for ( i = 0; i < FGNetCtrls::FG_MAX_WHEELS; ++i ) {
155         node = fgGetNode("/controls/gear/wheel", i);
156         if ( node->getChild("brake") != 0 ) {
157             if ( tempnode->getChild("parking-brake")->getDoubleValue() > 0.0 ) {
158                 net->brake[i] = 1.0;
159            } else {
160                 net->brake[i]
161                     = node->getChild("brake")->getDoubleValue();
162             }
163         } else {
164             net->brake[i] = 0.0;
165         }
166     }
167
168     node = fgGetNode("/controls/switches", true);
169     net->master_bat = node->getChild("master-bat")->getBoolValue();
170     net->master_alt = node->getChild("master-alt")->getBoolValue();
171     net->master_avionics = node->getChild("master-avionics")->getBoolValue();
172
173     net->wind_speed_kt = fgGetDouble("/environment/wind-speed-kt");
174     net->wind_dir_deg = fgGetDouble("/environment/wind-from-heading-deg");
175
176     // cur_fdm_state->get_ground_elev_ft() is what we want ... this
177     // reports the altitude of the aircraft.
178     // "/environment/ground-elevation-m" reports the ground elevation
179     // of the current view point which could change substantially if
180     // the user is switching views.
181     net->hground = cur_fdm_state->get_ground_elev_ft() * SG_FEET_TO_METER;
182     net->magvar = fgGetDouble("/environment/magnetic-variation-deg");
183     net->speedup = fgGetInt("/sim/speed-up");
184     net->freeze = 0;
185     if ( fgGetBool("/sim/freeze/master") ) {
186         net->freeze |= 0x01;
187     }
188     if ( fgGetBool("/sim/freeze/position") ) {
189         net->freeze |= 0x02;
190     }
191     if ( fgGetBool("/sim/freeze/fuel") ) {
192         net->freeze |= 0x04;
193     }
194
195     if ( net_byte_order ) {
196         // convert to network byte order
197         net->version = htonl(net->version);
198         htond(net->aileron);
199         htond(net->elevator);
200         htond(net->elevator_trim);
201         htond(net->rudder);
202         htond(net->flaps);
203         net->flaps_power = htonl(net->flaps_power);
204         for ( i = 0; i < FGNetCtrls::FG_MAX_ENGINES; ++i ) {
205             htond(net->throttle[i]);
206             htond(net->mixture[i]);
207             net->fuel_pump_power[i] = htonl(net->fuel_pump_power[i]);
208             htond(net->prop_advance[i]);
209             net->magnetos[i] = htonl(net->magnetos[i]);
210             net->starter_power[i] = htonl(net->starter_power[i]);
211         }
212         net->num_engines = htonl(net->num_engines);
213         for ( i = 0; i < FGNetCtrls::FG_MAX_TANKS; ++i ) {
214             net->fuel_selector[i] = htonl(net->fuel_selector[i]);
215         }
216         net->num_tanks = htonl(net->num_tanks);
217         for ( i = 0; i < FGNetCtrls::FG_MAX_WHEELS; ++i ) {
218             htond(net->brake[i]);
219         }
220         net->num_wheels = htonl(net->num_wheels);
221         net->gear_handle = htonl(net->gear_handle);
222         net->master_bat = htonl(net->master_bat);
223         net->master_alt = htonl(net->master_alt);
224         net->master_avionics = htonl(net->master_avionics);
225         htond(net->wind_speed_kt);
226         htond(net->wind_dir_deg);
227         htond(net->hground);
228         htond(net->magvar);
229         net->speedup = htonl(net->speedup);
230         net->freeze = htonl(net->freeze);
231     }
232 }
233
234
235 // Update the property tree from the FGNetCtrls structure.
236 void FGNetCtrls2Props( FGNetCtrls *net, bool net_byte_order ) {
237     int i;
238
239     SGPropertyNode * node;
240
241     if ( net_byte_order ) {
242         // convert from network byte order
243         net->version = htonl(net->version);
244         htond(net->aileron);
245         htond(net->elevator);
246         htond(net->elevator_trim);
247         htond(net->rudder);
248         htond(net->flaps);
249         net->flaps_power = htonl(net->flaps_power);
250         net->num_engines = htonl(net->num_engines);
251         for ( i = 0; i < net->num_engines; ++i ) {
252             net->magnetos[i] = htonl(net->magnetos[i]);
253             net->starter_power[i] = htonl(net->starter_power[i]);
254             htond(net->throttle[i]);
255             htond(net->mixture[i]);
256             net->fuel_pump_power[i] = htonl(net->fuel_pump_power[i]);
257             htond(net->prop_advance[i]);
258         }
259         net->num_tanks = htonl(net->num_tanks);
260         for ( i = 0; i < net->num_tanks; ++i ) {
261             net->fuel_selector[i] = htonl(net->fuel_selector[i]);
262         }
263         net->num_wheels = htonl(net->num_wheels);
264         for ( i = 0; i < net->num_wheels; ++i ) {
265             htond(net->brake[i]);
266         }
267         net->gear_handle = htonl(net->gear_handle);
268         net->master_bat = htonl(net->master_bat);
269         net->master_alt = htonl(net->master_alt);
270         net->master_avionics = htonl(net->master_avionics);
271         htond(net->wind_speed_kt);
272         htond(net->wind_dir_deg);
273         htond(net->hground);
274         htond(net->magvar);
275         net->speedup = htonl(net->speedup);
276         net->freeze = htonl(net->freeze);
277     }
278
279     if ( net->version != FG_NET_CTRLS_VERSION ) {
280         SG_LOG( SG_IO, SG_ALERT,
281                 "Version mismatch with raw controls packet format." );
282         SG_LOG( SG_IO, SG_ALERT,
283                 "FlightGear needs version = " << FG_NET_CTRLS_VERSION
284                 << " but is receiving version = "  << net->version );
285     }
286     node = fgGetNode("/controls/flight", true);
287     node->setDoubleValue( "aileron", net->aileron );
288     node->setDoubleValue( "elevator", net->elevator );
289     node->setDoubleValue( "elevator-trim", net->elevator_trim );
290     node->setDoubleValue( "rudder", net->rudder );
291     node->setDoubleValue( "flaps", net->flaps );
292
293     fgSetBool( "/systems/electrical/outputs/flaps", net->flaps_power );
294
295     for ( i = 0; i < FGNetCtrls::FG_MAX_ENGINES; ++i ) {
296         node = fgGetNode("/controls/engines/engine", i);
297         node->getChild( "throttle" )->setDoubleValue( net->throttle[i] );
298         node->getChild( "mixture" )->setDoubleValue( net->mixture[i] );
299         node->getChild( "propeller-pitch" )
300             ->setDoubleValue( net->prop_advance[i] );
301         node->getChild( "magnetos" )->setDoubleValue( net->magnetos[i] );
302     }
303
304     fgSetBool( "/systems/electrical/outputs/fuel-pump",
305                net->fuel_pump_power[0] );
306     fgSetBool( "/systems/electrical/outputs/starter",
307                net->starter_power[0] );
308
309     for ( i = 0; i < FGNetCtrls::FG_MAX_TANKS; ++i ) {
310         node = fgGetNode( "/controls/fuel/tank", i );
311         node->getChild( "fuel-selector" )
312             ->setBoolValue( net->fuel_selector[i] );
313     }
314     for ( i = 0; i < FGNetCtrls::FG_MAX_WHEELS; ++i ) {
315         node = fgGetNode( "/controls/gear/wheel", i );
316         node->getChild( "brake" )->setDoubleValue( net->brake[i] );
317     }
318
319     node = fgGetNode( "/controls/gear", true );
320     node->setBoolValue( "gear-down", net->gear_handle );
321
322     node = fgGetNode( "/controls/switches", true );
323     node->setBoolValue( "master-bat", net->master_bat );
324     node->setBoolValue( "master-alt", net->master_alt );
325     node->setBoolValue( "master-avionics", net->master_avionics );
326     
327     node = fgGetNode( "/environment", true );
328     node->setBoolValue( "wind-speed-kt", net->wind_speed_kt );
329     node->setBoolValue( "wind-from-heading-deg", net->wind_dir_deg );
330     node->setBoolValue( "magnetic-variation-deg", net->magvar );
331
332     // ground elevation ???
333
334     fgSetInt( "/sim/speed-up", net->speedup );
335
336     node = fgGetNode( "/sim/freeze", true );
337     node->setBoolValue( "master", net->freeze & 0x01 );
338     node->setBoolValue( "position", net->freeze & 0x02 );
339     node->setBoolValue( "fuel", net->freeze & 0x04 );
340 }
341
342
343 // process work for this port
344 bool FGNativeCtrls::process() {
345     SGIOChannel *io = get_io_channel();
346     int length = sizeof(FGNetCtrls);
347
348     if ( get_direction() == SG_IO_OUT ) {
349         // cout << "size of cur_fdm_state = " << length << endl;
350
351         FGProps2NetCtrls( &net_ctrls );
352
353         if ( ! io->write( (char *)(& net_ctrls), length ) ) {
354             SG_LOG( SG_IO, SG_ALERT, "Error writing data." );
355             return false;
356         }
357     } else if ( get_direction() == SG_IO_IN ) {
358         if ( io->get_type() == sgFileType ) {
359             if ( io->read( (char *)(& net_ctrls), length ) == length ) {
360                 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
361                 FGNetCtrls2Props( &net_ctrls );
362             }
363         } else {
364             while ( io->read( (char *)(& net_ctrls), length ) == length ) {
365                 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
366                 FGNetCtrls2Props( &net_ctrls );
367             }
368         }
369     }
370
371     return true;
372 }
373
374
375 // close the channel
376 bool FGNativeCtrls::close() {
377     SGIOChannel *io = get_io_channel();
378
379     set_enabled( false );
380
381     if ( ! io->close() ) {
382         return false;
383     }
384
385     return true;
386 }