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