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