]> git.mxchange.org Git - flightgear.git/blob - src/Network/native_ctrls.cxx
Trivial cleanup commit, to test continuous integration server.
[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 - http://www.flightgear.org/~curt
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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 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 <Main/fg_props.hxx>
33 #include <Scenery/scenery.hxx>  // ground elevation
34
35 #include "native_ctrls.hxx"
36
37 // FreeBSD works better with this included last ... (?)
38 #if defined( _MSC_VER )
39 #  include <windows.h>
40 #elif defined( __MINGW32__ )
41 #  include <winsock2.h>
42 #else
43 #  include <netinet/in.h>       // htonl() ntohl()
44 #endif
45
46
47 FGNativeCtrls::FGNativeCtrls() {
48 }
49
50 FGNativeCtrls::~FGNativeCtrls() {
51 }
52
53
54 // open hailing frequencies
55 bool FGNativeCtrls::open() {
56     if ( is_enabled() ) {
57         SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel " 
58                 << "is already in use, ignoring" );
59         return false;
60     }
61
62     SGIOChannel *io = get_io_channel();
63
64     if ( ! io->open( get_direction() ) ) {
65         SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
66         return false;
67     }
68
69     set_enabled( true );
70
71     return true;
72 }
73
74
75 // The function htond is defined this way due to the way some
76 // processors and OSes treat floating point values.  Some will raise
77 // an exception whenever a "bad" floating point value is loaded into a
78 // floating point register.  Solaris is notorious for this, but then
79 // so is LynxOS on the PowerPC.  By translating the data in place,
80 // there is no need to load a FP register with the "corruped" floating
81 // point value.  By doing the BIG_ENDIAN test, I can optimize the
82 // routine for big-endian processors so it can be as efficient as
83 // possible
84 static void htond (double &x)   
85 {
86     if ( sgIsLittleEndian() ) {
87         int    *Double_Overlay;
88         int     Holding_Buffer;
89     
90         Double_Overlay = (int *) &x;
91         Holding_Buffer = Double_Overlay [0];
92     
93         Double_Overlay [0] = htonl (Double_Overlay [1]);
94         Double_Overlay [1] = htonl (Holding_Buffer);
95     } else {
96         return;
97     }
98 }
99
100
101 // Populate the FGNetCtrls structure from the property tree.
102 void FGProps2NetCtrls( FGNetCtrls *net, bool honor_freezes,
103                        bool net_byte_order )
104 {
105     int i;
106     SGPropertyNode *node;
107     SGPropertyNode *fuelpump;
108     SGPropertyNode *tempnode;
109
110     // fill in values
111     node  = fgGetNode("/controls/flight", true);
112     net->version = FG_NET_CTRLS_VERSION;
113     net->aileron = node->getDoubleValue( "aileron" );
114     net->elevator = node->getDoubleValue( "elevator" );
115     net->rudder = node->getDoubleValue( "rudder" );
116     net->aileron_trim = node->getDoubleValue( "aileron-trim" );
117     net->elevator_trim = node->getDoubleValue( "elevator-trim" );
118     net->rudder_trim = node->getDoubleValue( "rudder-trim" );
119     net->flaps = node->getDoubleValue( "flaps" );
120     net->flaps_power
121         = fgGetDouble( "/systems/electrical/outputs/flaps", 1.0 ) >= 1.0;
122     net->flap_motor_ok = node->getBoolValue( "flaps-serviceable" );
123
124     net->num_engines = FGNetCtrls::FG_MAX_ENGINES;
125     for ( i = 0; i < FGNetCtrls::FG_MAX_ENGINES; ++i ) {
126         // Controls
127         node = fgGetNode("/controls/engines/engine", i );
128         fuelpump = fgGetNode("/systems/electrical/outputs/fuel-pump", i );
129
130         tempnode = node->getChild("starter");
131         if ( tempnode != NULL ) {
132             net->starter_power[i] = ( tempnode->getDoubleValue() >= 1.0 );
133         }
134         tempnode = node->getChild("master-bat");
135         if ( tempnode != NULL ) {
136             net->master_bat[i] = tempnode->getBoolValue();
137         }
138         tempnode = node->getChild("master-alt");
139         if ( tempnode != NULL ) {
140             net->master_alt[i] = tempnode->getBoolValue();
141         }
142
143         net->throttle[i] = node->getDoubleValue( "throttle", 0.0 );
144         net->mixture[i] = node->getDoubleValue( "mixture", 0.0 );
145         net->prop_advance[i] = node->getDoubleValue( "propeller-pitch", 0.0 );
146         net->condition[i] = node->getDoubleValue( "condition", 0.0 );
147         net->magnetos[i] = node->getIntValue( "magnetos", 0 );
148         if ( i == 0 ) {
149           // cout << "Magnetos -> " << node->getIntValue( "magnetos", 0 );
150         }
151         if ( i == 0 ) {
152           // cout << "Starter -> " << node->getIntValue( "starter", false )
153           //      << endl;
154         }
155
156         if ( fuelpump != NULL ) {
157             net->fuel_pump_power[i] = ( fuelpump->getDoubleValue() >= 1.0 );
158         } else {
159             net->fuel_pump_power[i] = 0;
160         }
161
162         // Faults
163         SGPropertyNode *faults = node->getChild( "faults", 0, true );
164         net->engine_ok[i] = faults->getBoolValue( "serviceable", true );
165         net->mag_left_ok[i]
166           = faults->getBoolValue( "left-magneto-serviceable", true );
167         net->mag_right_ok[i]
168           = faults->getBoolValue( "right-magneto-serviceable", true);
169         net->spark_plugs_ok[i]
170           = faults->getBoolValue( "spark-plugs-serviceable", true );
171         net->oil_press_status[i]
172           = faults->getIntValue( "oil-pressure-status", 0 );
173         net->fuel_pump_ok[i]
174           = faults->getBoolValue( "fuel-pump-serviceable", true );
175     }
176     net->num_tanks = FGNetCtrls::FG_MAX_TANKS;
177     for ( i = 0; i < FGNetCtrls::FG_MAX_TANKS; ++i ) {
178         node = fgGetNode("/controls/fuel/tank", i);
179         if ( node->getChild("fuel_selector") != 0 ) {
180             net->fuel_selector[i]
181                 = node->getChild("fuel_selector")->getBoolValue();
182         } else {
183             net->fuel_selector[i] = false;
184         }
185     }
186     node = fgGetNode("/controls/gear", true);
187     net->brake_left = node->getChild("brake-left")->getDoubleValue();
188     net->brake_right = node->getChild("brake-right")->getDoubleValue();
189     net->copilot_brake_left
190         = node->getChild("copilot-brake-left")->getDoubleValue();
191     net->copilot_brake_right
192         = node->getChild("copilot-brake-right")->getDoubleValue();
193     net->brake_parking = node->getChild("brake-parking")->getDoubleValue();
194
195     net->gear_handle = fgGetBool( "/controls/gear/gear-down" );
196
197     net->master_avionics = fgGetBool("/controls/switches/master-avionics");
198
199     net->wind_speed_kt = fgGetDouble("/environment/wind-speed-kt");
200     net->wind_dir_deg = fgGetDouble("/environment/wind-from-heading-deg");
201     net->turbulence_norm =
202         fgGetDouble("/environment/turbulence/magnitude-norm");
203
204     net->temp_c = fgGetDouble("/environment/temperature-degc");
205     net->press_inhg = fgGetDouble("/environment/pressure-sea-level-inhg");
206
207     net->hground = fgGetDouble("/position/ground-elev-m");
208     net->magvar = fgGetDouble("/environment/magnetic-variation-deg");
209
210     net->icing = fgGetBool("/hazards/icing/wing");
211
212     net->speedup = fgGetInt("/sim/speed-up");
213     net->freeze = 0;
214     if ( honor_freezes ) {
215         if ( fgGetBool("/sim/freeze/master") ) {
216             net->freeze |= 0x01;
217         }
218         if ( fgGetBool("/sim/freeze/position") ) {
219             net->freeze |= 0x02;
220         }
221         if ( fgGetBool("/sim/freeze/fuel") ) {
222             net->freeze |= 0x04;
223         }
224     }
225
226     if ( net_byte_order ) {
227         // convert to network byte order
228         net->version = htonl(net->version);
229         htond(net->aileron);
230         htond(net->elevator);
231         htond(net->rudder);
232         htond(net->aileron_trim);
233         htond(net->elevator_trim);
234         htond(net->rudder_trim);
235         htond(net->flaps);
236         net->flaps_power = htonl(net->flaps_power);
237         net->flap_motor_ok = htonl(net->flap_motor_ok);
238
239         net->num_engines = htonl(net->num_engines);
240         for ( i = 0; i < FGNetCtrls::FG_MAX_ENGINES; ++i ) {
241             net->master_bat[i] = htonl(net->master_bat[i]);
242             net->master_alt[i] = htonl(net->master_alt[i]);
243             net->magnetos[i] = htonl(net->magnetos[i]);
244             net->starter_power[i] = htonl(net->starter_power[i]);
245             htond(net->throttle[i]);
246             htond(net->mixture[i]);
247             net->fuel_pump_power[i] = htonl(net->fuel_pump_power[i]);
248             htond(net->prop_advance[i]);
249             htond(net->condition[i]);
250             net->engine_ok[i] = htonl(net->engine_ok[i]);
251             net->mag_left_ok[i] = htonl(net->mag_left_ok[i]);
252             net->mag_right_ok[i] = htonl(net->mag_right_ok[i]);
253             net->spark_plugs_ok[i] = htonl(net->spark_plugs_ok[i]);
254             net->oil_press_status[i] = htonl(net->oil_press_status[i]);
255             net->fuel_pump_ok[i] = htonl(net->fuel_pump_ok[i]);
256         }
257
258         net->num_tanks = htonl(net->num_tanks);
259         for ( i = 0; i < FGNetCtrls::FG_MAX_TANKS; ++i ) {
260             net->fuel_selector[i] = htonl(net->fuel_selector[i]);
261         }
262
263         net->cross_feed = htonl(net->cross_feed);
264         htond(net->brake_left);
265         htond(net->brake_right);
266         htond(net->copilot_brake_left);
267         htond(net->copilot_brake_right);
268         htond(net->brake_parking);
269         net->gear_handle = htonl(net->gear_handle);
270         net->master_avionics = htonl(net->master_avionics);
271         htond(net->wind_speed_kt);
272         htond(net->wind_dir_deg);
273         htond(net->turbulence_norm);
274         htond(net->temp_c);
275         htond(net->press_inhg);
276         htond(net->hground);
277         htond(net->magvar);
278         net->icing = htonl(net->icing);
279         net->speedup = htonl(net->speedup);
280         net->freeze = htonl(net->freeze);
281     }
282 }
283
284
285 // Update the property tree from the FGNetCtrls structure.
286 void FGNetCtrls2Props( FGNetCtrls *net, bool honor_freezes,
287                        bool net_byte_order )
288 {
289     int i;
290
291     SGPropertyNode * node;
292
293     if ( net_byte_order ) {
294         // convert from network byte order
295         net->version = htonl(net->version);
296         htond(net->aileron);
297         htond(net->elevator);
298         htond(net->rudder);
299         htond(net->aileron_trim);
300         htond(net->elevator_trim);
301         htond(net->rudder_trim);
302         htond(net->flaps);
303         net->flaps_power = htonl(net->flaps_power);
304         net->flap_motor_ok = htonl(net->flap_motor_ok);
305
306         net->num_engines = htonl(net->num_engines);
307         for ( i = 0; i < (int)net->num_engines; ++i ) {
308             net->master_bat[i] = htonl(net->master_bat[i]);
309             net->master_alt[i] = htonl(net->master_alt[i]);
310             net->magnetos[i] = htonl(net->magnetos[i]);
311             net->starter_power[i] = htonl(net->starter_power[i]);
312             htond(net->throttle[i]);
313             htond(net->mixture[i]);
314             net->fuel_pump_power[i] = htonl(net->fuel_pump_power[i]);
315             htond(net->prop_advance[i]);
316             htond(net->condition[i]);
317             net->engine_ok[i] = htonl(net->engine_ok[i]);
318             net->mag_left_ok[i] = htonl(net->mag_left_ok[i]);
319             net->mag_right_ok[i] = htonl(net->mag_right_ok[i]);
320             net->spark_plugs_ok[i] = htonl(net->spark_plugs_ok[i]);
321             net->oil_press_status[i] = htonl(net->oil_press_status[i]);
322             net->fuel_pump_ok[i] = htonl(net->fuel_pump_ok[i]);
323         }
324
325         net->num_tanks = htonl(net->num_tanks);
326         for ( i = 0; i < FGNetCtrls::FG_MAX_TANKS; ++i ) {
327             net->fuel_selector[i] = htonl(net->fuel_selector[i]);
328         }
329
330         net->cross_feed = htonl(net->cross_feed);
331         htond(net->brake_left);
332         htond(net->brake_right);
333         htond(net->copilot_brake_left);
334         htond(net->copilot_brake_right);
335         htond(net->brake_parking);
336         net->gear_handle = htonl(net->gear_handle);
337         net->master_avionics = htonl(net->master_avionics);
338         htond(net->wind_speed_kt);
339         htond(net->wind_dir_deg);
340         htond(net->turbulence_norm);
341         htond(net->temp_c);
342         htond(net->press_inhg);
343         htond(net->hground);
344         htond(net->magvar);
345         net->icing = htonl(net->icing);
346         net->speedup = htonl(net->speedup);
347         net->freeze = htonl(net->freeze);
348     }
349
350     if ( net->version != FG_NET_CTRLS_VERSION ) {
351         SG_LOG( SG_IO, SG_ALERT,
352                 "Version mismatch with raw controls packet format." );
353         SG_LOG( SG_IO, SG_ALERT,
354                 "FlightGear needs version = " << FG_NET_CTRLS_VERSION
355                 << " but is receiving version = "  << net->version );
356     }
357     node = fgGetNode("/controls/flight", true);
358     node->setDoubleValue( "aileron", net->aileron );
359     node->setDoubleValue( "elevator", net->elevator );
360     node->setDoubleValue( "rudder", net->rudder );
361     node->setDoubleValue( "aileron-trim", net->aileron_trim );
362     node->setDoubleValue( "elevator-trim", net->elevator_trim );
363     node->setDoubleValue( "rudder-trim", net->rudder_trim );
364     node->setDoubleValue( "flaps", net->flaps );
365     node->setDoubleValue( "speedbrake", net->speedbrake );  //JWW
366     // or
367     node->setDoubleValue( "spoilers", net->spoilers );  //JWW
368 //    cout << "NET->Spoilers: " << net->spoilers << endl;
369     fgSetBool( "/systems/electrical/outputs/flaps", net->flaps_power );
370     node->setBoolValue( "flaps-serviceable", net->flap_motor_ok );
371
372     for ( i = 0; i < FGNetCtrls::FG_MAX_ENGINES; ++i ) {
373         // Controls
374         node = fgGetNode("/controls/engines/engine", i);
375         node->getChild( "throttle" )->setDoubleValue( net->throttle[i] );
376         node->getChild( "mixture" )->setDoubleValue( net->mixture[i] );
377         node->getChild( "propeller-pitch" )
378             ->setDoubleValue( net->prop_advance[i] );
379         node->getChild( "condition" )
380             ->setDoubleValue( net->condition[i] );
381         node->getChild( "magnetos" )->setDoubleValue( net->magnetos[i] );
382         node->getChild( "starter" )->setDoubleValue( net->starter_power[i] );
383         node->getChild( "feed_tank" )->setIntValue( net->feed_tank_to[i] );
384         node->getChild( "reverser" )->setBoolValue( net->reverse[i] );
385         // Faults
386         SGPropertyNode *faults = node->getNode( "faults", true );
387         faults->setBoolValue( "serviceable", net->engine_ok[i] );
388         faults->setBoolValue( "left-magneto-serviceable",
389                               net->mag_left_ok[i] );
390         faults->setBoolValue( "right-magneto-serviceable",
391                               net->mag_right_ok[i]);
392         faults->setBoolValue( "spark-plugs-serviceable",
393                               net->spark_plugs_ok[i] );
394         faults->setIntValue( "oil-pressure-status", net->oil_press_status[i] );
395         faults->setBoolValue( "fuel-pump-serviceable", net->fuel_pump_ok[i] );
396     }
397
398     fgSetBool( "/systems/electrical/outputs/fuel-pump",
399                net->fuel_pump_power[0] );
400
401     for ( i = 0; i < FGNetCtrls::FG_MAX_TANKS; ++i ) {
402         node = fgGetNode( "/controls/fuel/tank", i );
403         node->getChild( "fuel_selector" )
404             ->setBoolValue( net->fuel_selector[i] );
405 //        node->getChild( "to_tank" )->xfer_tank( i, net->xfer_to[i] );
406     }
407     node = fgGetNode( "/controls/gear" );
408     if ( node != NULL ) {
409         node->getChild( "brake-left" )->setDoubleValue( net->brake_left );
410         node->getChild( "brake-right" )->setDoubleValue( net->brake_right );
411         node->getChild( "copilot-brake-left" )
412             ->setDoubleValue( net->copilot_brake_left );
413         node->getChild( "copilot-brake-right" )
414             ->setDoubleValue( net->copilot_brake_right );
415         node->getChild( "brake-parking" )->setDoubleValue( net->brake_parking );
416     }
417
418     node = fgGetNode( "/controls/gear", true );
419     node->setBoolValue( "gear-down", net->gear_handle );
420 //    node->setDoubleValue( "brake-parking", net->brake_parking );
421 //    node->setDoubleValue( net->brake_left );
422 //    node->setDoubleValue( net->brake_right );
423
424     node = fgGetNode( "/controls/switches", true );
425     node->setBoolValue( "master-bat", net->master_bat );
426     node->setBoolValue( "master-alt", net->master_alt );
427     node->setBoolValue( "master-avionics", net->master_avionics );
428     
429     node = fgGetNode( "/environment", true );
430     node->setDoubleValue( "wind-speed-kt", net->wind_speed_kt );
431     node->setDoubleValue( "wind-from-heading-deg", net->wind_dir_deg );
432     node->setDoubleValue( "turbulence/magnitude-norm", net->turbulence_norm );
433     node->setDoubleValue( "magnetic-variation-deg", net->magvar );
434
435     node->setDoubleValue( "/environment/temperature-degc",
436                           net->temp_c );
437     node->setDoubleValue( "/environment/pressure-sea-level-inhg",
438                           net->press_inhg );
439
440     // ground elevation ???
441
442     fgSetDouble("/hazards/icing/wing", net->icing);
443     
444     node = fgGetNode( "/radios", true );
445     node->setDoubleValue( "comm/frequencies/selected-mhz[0]", net->comm_1 );
446     node->setDoubleValue( "nav/frequencies/selected-mhz[0]", net->nav_1 );
447     node->setDoubleValue( "nav[1]/frequencies/selected-mhz[0]", net->nav_2 );
448
449     fgSetInt( "/sim/speed-up", net->speedup );
450
451     if ( honor_freezes ) {
452         node = fgGetNode( "/sim/freeze", true );
453         node->setBoolValue( "master", net->freeze & 0x01 );
454         node->setBoolValue( "position", net->freeze & 0x02 );
455         node->setBoolValue( "fuel", net->freeze & 0x04 );
456     }
457 }
458
459
460 // process work for this port
461 bool FGNativeCtrls::process() {
462     SGIOChannel *io = get_io_channel();
463     int length = sizeof(FGNetCtrls);
464
465     if ( get_direction() == SG_IO_OUT ) {
466
467         FGProps2NetCtrls( &net_ctrls, true, true );
468
469         if ( ! io->write( (char *)(& net_ctrls), length ) ) {
470             SG_LOG( SG_IO, SG_ALERT, "Error writing data." );
471             return false;
472         }
473     } else if ( get_direction() == SG_IO_IN ) {
474         if ( io->get_type() == sgFileType ) {
475             if ( io->read( (char *)(& net_ctrls), length ) == length ) {
476                 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
477                 FGNetCtrls2Props( &net_ctrls, true, true );
478             }
479         } else {
480             while ( io->read( (char *)(& net_ctrls), length ) == length ) {
481                 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
482                 FGNetCtrls2Props( &net_ctrls, true, true );
483             }
484         }
485     }
486
487     return true;
488 }
489
490
491 // close the channel
492 bool FGNativeCtrls::close() {
493     SGIOChannel *io = get_io_channel();
494
495     set_enabled( false );
496
497     if ( ! io->close() ) {
498         return false;
499     }
500
501     return true;
502 }