]> git.mxchange.org Git - flightgear.git/blob - src/Network/native_ctrls.cxx
NavDisplay - custom symbol support enabled.
[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->speedbrake = node->getDoubleValue( "speedbrake" );
121     net->spoilers = node->getDoubleValue( "spoilers" );
122     net->flaps_power
123         = fgGetDouble( "/systems/electrical/outputs/flaps", 1.0 ) >= 1.0;
124     net->flap_motor_ok = node->getBoolValue( "flaps-serviceable" );
125
126     net->num_engines = FGNetCtrls::FG_MAX_ENGINES;
127     for ( i = 0; i < FGNetCtrls::FG_MAX_ENGINES; ++i ) {
128         // Controls
129         node = fgGetNode("/controls/engines/engine", i );
130         fuelpump = fgGetNode("/systems/electrical/outputs/fuel-pump", i );
131
132         tempnode = node->getChild("starter");
133         if ( tempnode != NULL ) {
134             net->starter_power[i] = ( tempnode->getDoubleValue() >= 1.0 );
135         }
136         tempnode = node->getChild("master-bat");
137         if ( tempnode != NULL ) {
138             net->master_bat[i] = tempnode->getBoolValue();
139         }
140         tempnode = node->getChild("master-alt");
141         if ( tempnode != NULL ) {
142             net->master_alt[i] = tempnode->getBoolValue();
143         }
144
145         net->throttle[i] = node->getDoubleValue( "throttle", 0.0 );
146         net->mixture[i] = node->getDoubleValue( "mixture", 0.0 );
147         net->prop_advance[i] = node->getDoubleValue( "propeller-pitch", 0.0 );
148         net->condition[i] = node->getDoubleValue( "condition", 0.0 );
149         net->magnetos[i] = node->getIntValue( "magnetos", 0 );
150         if ( i == 0 ) {
151           // cout << "Magnetos -> " << node->getIntValue( "magnetos", 0 );
152         }
153         if ( i == 0 ) {
154           // cout << "Starter -> " << node->getIntValue( "starter", false )
155           //      << endl;
156         }
157
158         if ( fuelpump != NULL ) {
159             net->fuel_pump_power[i] = ( fuelpump->getDoubleValue() >= 1.0 );
160         } else {
161             net->fuel_pump_power[i] = 0;
162         }
163
164         // Faults
165         SGPropertyNode *faults = node->getChild( "faults", 0, true );
166         net->engine_ok[i] = faults->getBoolValue( "serviceable", true );
167         net->mag_left_ok[i]
168           = faults->getBoolValue( "left-magneto-serviceable", true );
169         net->mag_right_ok[i]
170           = faults->getBoolValue( "right-magneto-serviceable", true);
171         net->spark_plugs_ok[i]
172           = faults->getBoolValue( "spark-plugs-serviceable", true );
173         net->oil_press_status[i]
174           = faults->getIntValue( "oil-pressure-status", 0 );
175         net->fuel_pump_ok[i]
176           = faults->getBoolValue( "fuel-pump-serviceable", true );
177     }
178     net->num_tanks = FGNetCtrls::FG_MAX_TANKS;
179     for ( i = 0; i < FGNetCtrls::FG_MAX_TANKS; ++i ) {
180         node = fgGetNode("/controls/fuel/tank", i);
181         if ( node->getChild("fuel_selector") != 0 ) {
182             net->fuel_selector[i]
183                 = node->getChild("fuel_selector")->getBoolValue();
184         } else {
185             net->fuel_selector[i] = false;
186         }
187     }
188     node = fgGetNode("/controls/gear", true);
189     net->brake_left = node->getChild("brake-left")->getDoubleValue();
190     net->brake_right = node->getChild("brake-right")->getDoubleValue();
191     net->copilot_brake_left
192         = node->getChild("copilot-brake-left")->getDoubleValue();
193     net->copilot_brake_right
194         = node->getChild("copilot-brake-right")->getDoubleValue();
195     net->brake_parking = node->getChild("brake-parking")->getDoubleValue();
196
197     net->gear_handle = fgGetBool( "/controls/gear/gear-down" );
198
199     net->master_avionics = fgGetBool("/controls/switches/master-avionics");
200
201     net->wind_speed_kt = fgGetDouble("/environment/wind-speed-kt");
202     net->wind_dir_deg = fgGetDouble("/environment/wind-from-heading-deg");
203     net->turbulence_norm =
204         fgGetDouble("/environment/turbulence/magnitude-norm");
205
206     net->temp_c = fgGetDouble("/environment/temperature-degc");
207     net->press_inhg = fgGetDouble("/environment/pressure-sea-level-inhg");
208
209     net->hground = fgGetDouble("/position/ground-elev-m");
210     net->magvar = fgGetDouble("/environment/magnetic-variation-deg");
211
212     net->icing = fgGetBool("/hazards/icing/wing");
213
214     net->speedup = fgGetInt("/sim/speed-up");
215     net->freeze = 0;
216     if ( honor_freezes ) {
217         if ( fgGetBool("/sim/freeze/master") ) {
218             net->freeze |= 0x01;
219         }
220         if ( fgGetBool("/sim/freeze/position") ) {
221             net->freeze |= 0x02;
222         }
223         if ( fgGetBool("/sim/freeze/fuel") ) {
224             net->freeze |= 0x04;
225         }
226     }
227
228     if ( net_byte_order ) {
229         // convert to network byte order
230         net->version = htonl(net->version);
231         htond(net->aileron);
232         htond(net->elevator);
233         htond(net->rudder);
234         htond(net->aileron_trim);
235         htond(net->elevator_trim);
236         htond(net->rudder_trim);
237         htond(net->flaps);
238         htond(net->speedbrake);
239         htond(net->spoilers);
240         net->flaps_power = htonl(net->flaps_power);
241         net->flap_motor_ok = htonl(net->flap_motor_ok);
242
243         net->num_engines = htonl(net->num_engines);
244         for ( i = 0; i < FGNetCtrls::FG_MAX_ENGINES; ++i ) {
245             net->master_bat[i] = htonl(net->master_bat[i]);
246             net->master_alt[i] = htonl(net->master_alt[i]);
247             net->magnetos[i] = htonl(net->magnetos[i]);
248             net->starter_power[i] = htonl(net->starter_power[i]);
249             htond(net->throttle[i]);
250             htond(net->mixture[i]);
251             net->fuel_pump_power[i] = htonl(net->fuel_pump_power[i]);
252             htond(net->prop_advance[i]);
253             htond(net->condition[i]);
254             net->engine_ok[i] = htonl(net->engine_ok[i]);
255             net->mag_left_ok[i] = htonl(net->mag_left_ok[i]);
256             net->mag_right_ok[i] = htonl(net->mag_right_ok[i]);
257             net->spark_plugs_ok[i] = htonl(net->spark_plugs_ok[i]);
258             net->oil_press_status[i] = htonl(net->oil_press_status[i]);
259             net->fuel_pump_ok[i] = htonl(net->fuel_pump_ok[i]);
260         }
261
262         net->num_tanks = htonl(net->num_tanks);
263         for ( i = 0; i < FGNetCtrls::FG_MAX_TANKS; ++i ) {
264             net->fuel_selector[i] = htonl(net->fuel_selector[i]);
265         }
266
267         net->cross_feed = htonl(net->cross_feed);
268         htond(net->brake_left);
269         htond(net->brake_right);
270         htond(net->copilot_brake_left);
271         htond(net->copilot_brake_right);
272         htond(net->brake_parking);
273         net->gear_handle = htonl(net->gear_handle);
274         net->master_avionics = htonl(net->master_avionics);
275         htond(net->wind_speed_kt);
276         htond(net->wind_dir_deg);
277         htond(net->turbulence_norm);
278         htond(net->temp_c);
279         htond(net->press_inhg);
280         htond(net->hground);
281         htond(net->magvar);
282         net->icing = htonl(net->icing);
283         net->speedup = htonl(net->speedup);
284         net->freeze = htonl(net->freeze);
285     }
286 }
287
288
289 // Update the property tree from the FGNetCtrls structure.
290 void FGNetCtrls2Props( FGNetCtrls *net, bool honor_freezes,
291                        bool net_byte_order )
292 {
293     int i;
294
295     SGPropertyNode * node;
296
297     if ( net_byte_order ) {
298         // convert from network byte order
299         net->version = htonl(net->version);
300         htond(net->aileron);
301         htond(net->elevator);
302         htond(net->rudder);
303         htond(net->aileron_trim);
304         htond(net->elevator_trim);
305         htond(net->rudder_trim);
306         htond(net->flaps);
307         htond(net->speedbrake);
308         htond(net->spoilers);
309         net->flaps_power = htonl(net->flaps_power);
310         net->flap_motor_ok = htonl(net->flap_motor_ok);
311
312         net->num_engines = htonl(net->num_engines);
313         for ( i = 0; i < (int)net->num_engines; ++i ) {
314             net->master_bat[i] = htonl(net->master_bat[i]);
315             net->master_alt[i] = htonl(net->master_alt[i]);
316             net->magnetos[i] = htonl(net->magnetos[i]);
317             net->starter_power[i] = htonl(net->starter_power[i]);
318             htond(net->throttle[i]);
319             htond(net->mixture[i]);
320             net->fuel_pump_power[i] = htonl(net->fuel_pump_power[i]);
321             htond(net->prop_advance[i]);
322             htond(net->condition[i]);
323             net->engine_ok[i] = htonl(net->engine_ok[i]);
324             net->mag_left_ok[i] = htonl(net->mag_left_ok[i]);
325             net->mag_right_ok[i] = htonl(net->mag_right_ok[i]);
326             net->spark_plugs_ok[i] = htonl(net->spark_plugs_ok[i]);
327             net->oil_press_status[i] = htonl(net->oil_press_status[i]);
328             net->fuel_pump_ok[i] = htonl(net->fuel_pump_ok[i]);
329         }
330
331         net->num_tanks = htonl(net->num_tanks);
332         for ( i = 0; i < FGNetCtrls::FG_MAX_TANKS; ++i ) {
333             net->fuel_selector[i] = htonl(net->fuel_selector[i]);
334         }
335
336         net->cross_feed = htonl(net->cross_feed);
337         htond(net->brake_left);
338         htond(net->brake_right);
339         htond(net->copilot_brake_left);
340         htond(net->copilot_brake_right);
341         htond(net->brake_parking);
342         net->gear_handle = htonl(net->gear_handle);
343         net->master_avionics = htonl(net->master_avionics);
344         htond(net->wind_speed_kt);
345         htond(net->wind_dir_deg);
346         htond(net->turbulence_norm);
347         htond(net->temp_c);
348         htond(net->press_inhg);
349         htond(net->hground);
350         htond(net->magvar);
351         net->icing = htonl(net->icing);
352         net->speedup = htonl(net->speedup);
353         net->freeze = htonl(net->freeze);
354     }
355
356     if ( net->version != FG_NET_CTRLS_VERSION ) {
357         SG_LOG( SG_IO, SG_ALERT,
358                 "Version mismatch with raw controls packet format." );
359         SG_LOG( SG_IO, SG_ALERT,
360                 "FlightGear needs version = " << FG_NET_CTRLS_VERSION
361                 << " but is receiving version = "  << net->version );
362     }
363     node = fgGetNode("/controls/flight", true);
364     node->setDoubleValue( "aileron", net->aileron );
365     node->setDoubleValue( "elevator", net->elevator );
366     node->setDoubleValue( "rudder", net->rudder );
367     node->setDoubleValue( "aileron-trim", net->aileron_trim );
368     node->setDoubleValue( "elevator-trim", net->elevator_trim );
369     node->setDoubleValue( "rudder-trim", net->rudder_trim );
370     node->setDoubleValue( "flaps", net->flaps );
371     node->setDoubleValue( "speedbrake", net->speedbrake );  //JWW
372     // or
373     node->setDoubleValue( "spoilers", net->spoilers );  //JWW
374 //    cout << "NET->Spoilers: " << net->spoilers << endl;
375     fgSetBool( "/systems/electrical/outputs/flaps", net->flaps_power > 0 );
376     node->setBoolValue( "flaps-serviceable", net->flap_motor_ok > 0 );
377
378     for ( i = 0; i < FGNetCtrls::FG_MAX_ENGINES; ++i ) {
379         // Controls
380         node = fgGetNode("/controls/engines/engine", i);
381         node->getChild( "throttle" )->setDoubleValue( net->throttle[i] );
382         node->getChild( "mixture" )->setDoubleValue( net->mixture[i] );
383         node->getChild( "propeller-pitch" )
384             ->setDoubleValue( net->prop_advance[i] );
385         node->getChild( "condition" )
386             ->setDoubleValue( net->condition[i] );
387         node->getChild( "magnetos" )->setDoubleValue( net->magnetos[i] );
388         node->getChild( "starter" )->setDoubleValue( net->starter_power[i] );
389         node->getChild( "feed_tank" )->setIntValue( net->feed_tank_to[i] );
390         node->getChild( "reverser" )->setBoolValue( net->reverse[i] > 0 );
391         // Faults
392         SGPropertyNode *faults = node->getNode( "faults", true );
393         faults->setBoolValue( "serviceable", net->engine_ok[i] > 0 );
394         faults->setBoolValue( "left-magneto-serviceable",
395                               net->mag_left_ok[i] > 0 );
396         faults->setBoolValue( "right-magneto-serviceable",
397                               net->mag_right_ok[i] > 0);
398         faults->setBoolValue( "spark-plugs-serviceable",
399                               net->spark_plugs_ok[i] > 0);
400         faults->setIntValue( "oil-pressure-status", net->oil_press_status[i] );
401         faults->setBoolValue( "fuel-pump-serviceable", net->fuel_pump_ok[i] > 0);
402     }
403
404     fgSetBool( "/systems/electrical/outputs/fuel-pump",
405                net->fuel_pump_power[0] > 0);
406
407     for ( i = 0; i < FGNetCtrls::FG_MAX_TANKS; ++i ) {
408         node = fgGetNode( "/controls/fuel/tank", i );
409         node->getChild( "fuel_selector" )
410             ->setBoolValue( net->fuel_selector[i] > 0 );
411 //        node->getChild( "to_tank" )->xfer_tank( i, net->xfer_to[i] );
412     }
413     node = fgGetNode( "/controls/gear" );
414     if ( node != NULL ) {
415         node->getChild( "brake-left" )->setDoubleValue( net->brake_left );
416         node->getChild( "brake-right" )->setDoubleValue( net->brake_right );
417         node->getChild( "copilot-brake-left" )
418             ->setDoubleValue( net->copilot_brake_left );
419         node->getChild( "copilot-brake-right" )
420             ->setDoubleValue( net->copilot_brake_right );
421         node->getChild( "brake-parking" )->setDoubleValue( net->brake_parking );
422     }
423
424     node = fgGetNode( "/controls/gear", true );
425     node->setBoolValue( "gear-down", net->gear_handle > 0 );
426 //    node->setDoubleValue( "brake-parking", net->brake_parking );
427 //    node->setDoubleValue( net->brake_left );
428 //    node->setDoubleValue( net->brake_right );
429
430     node = fgGetNode( "/controls/switches", true );
431     node->setBoolValue( "master-bat", net->master_bat > 0 );
432     node->setBoolValue( "master-alt", net->master_alt > 0);
433     node->setBoolValue( "master-avionics", net->master_avionics > 0);
434     
435     node = fgGetNode( "/environment", true );
436     node->setDoubleValue( "wind-speed-kt", net->wind_speed_kt );
437     node->setDoubleValue( "wind-from-heading-deg", net->wind_dir_deg );
438     node->setDoubleValue( "turbulence/magnitude-norm", net->turbulence_norm );
439     node->setDoubleValue( "magnetic-variation-deg", net->magvar );
440
441     node->setDoubleValue( "/environment/temperature-degc",
442                           net->temp_c );
443     node->setDoubleValue( "/environment/pressure-sea-level-inhg",
444                           net->press_inhg );
445
446     // ground elevation ???
447
448     fgSetDouble("/hazards/icing/wing", net->icing);
449     
450     node = fgGetNode( "/radios", true );
451     node->setDoubleValue( "comm/frequencies/selected-mhz[0]", net->comm_1 );
452     node->setDoubleValue( "nav/frequencies/selected-mhz[0]", net->nav_1 );
453     node->setDoubleValue( "nav[1]/frequencies/selected-mhz[0]", net->nav_2 );
454
455     fgSetDouble( "/sim/speed-up", net->speedup );
456
457     if ( honor_freezes ) {
458         node = fgGetNode( "/sim/freeze", true );
459         node->setBoolValue( "master", (net->freeze & 0x01) > 0 );
460         node->setBoolValue( "position", (net->freeze & 0x02) > 0 );
461         node->setBoolValue( "fuel", (net->freeze & 0x04) > 0 );
462     }
463 }
464
465
466 // process work for this port
467 bool FGNativeCtrls::process() {
468     SGIOChannel *io = get_io_channel();
469     int length = sizeof(FGNetCtrls);
470
471     if ( get_direction() == SG_IO_OUT ) {
472
473         FGProps2NetCtrls( &net_ctrls, true, true );
474
475         if ( ! io->write( (char *)(& net_ctrls), length ) ) {
476             SG_LOG( SG_IO, SG_ALERT, "Error writing data." );
477             return false;
478         }
479     } else if ( get_direction() == SG_IO_IN ) {
480         if ( io->get_type() == sgFileType ) {
481             if ( io->read( (char *)(& net_ctrls), length ) == length ) {
482                 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
483                 FGNetCtrls2Props( &net_ctrls, true, true );
484             }
485         } else {
486             while ( io->read( (char *)(& net_ctrls), length ) == length ) {
487                 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
488                 FGNetCtrls2Props( &net_ctrls, true, true );
489             }
490         }
491     }
492
493     return true;
494 }
495
496
497 // close the channel
498 bool FGNativeCtrls::close() {
499     SGIOChannel *io = get_io_channel();
500
501     set_enabled( false );
502
503     if ( ! io->close() ) {
504         return false;
505     }
506
507     return true;
508 }