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