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