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