]> git.mxchange.org Git - flightgear.git/blobdiff - src/FDM/ExternalNet/ExternalNet.cxx
Set SO_REUSEADDR on incoming UDP socket so other processes on the host
[flightgear.git] / src / FDM / ExternalNet / ExternalNet.cxx
index cde754cfe8bd6baab30538473276e3235aeee0d4..a18b316f42c927ec78a2b9fc9166de26055621a9 100644 (file)
 #  include <netinet/in.h>      // htonl() ntohl()
 #endif
 
+#if defined( linux )
+#  include <sys/types.h>
+#  include <sys/socket.h>
+#endif
 
 // The function htond is defined this way due to the way some
 // processors and OSes treat floating point values.  Some will raise
@@ -62,33 +66,33 @@ static void htond (double &x)
 }
 
 
-static void global2raw( FGRawCtrls *raw ) {
+static void global2net( FGNetCtrls *net ) {
     int i;
 
     // fill in values
     SGPropertyNode * node = fgGetNode("/controls", true);
-    raw->version = FG_RAW_CTRLS_VERSION;
-    raw->aileron = node->getDoubleValue( "aileron" );
-    raw->elevator = node->getDoubleValue( "elevator" );
-    raw->elevator_trim = node->getDoubleValue( "elevator-trim" );
-    raw->rudder = node->getDoubleValue( "rudder" );
-    raw->flaps = node->getDoubleValue( "flaps" );
-    raw->flaps_power
+    net->version = FG_NET_CTRLS_VERSION;
+    net->aileron = node->getDoubleValue( "aileron" );
+    net->elevator = node->getDoubleValue( "elevator" );
+    net->elevator_trim = node->getDoubleValue( "elevator-trim" );
+    net->rudder = node->getDoubleValue( "rudder" );
+    net->flaps = node->getDoubleValue( "flaps" );
+    net->flaps_power
             = node->getDoubleValue( "/systems/electrical/outputs/flaps",
                                     1.0 ) >= 1.0;
-    raw->num_engines = FGRawCtrls::FG_MAX_ENGINES;
-    for ( i = 0; i < FGRawCtrls::FG_MAX_ENGINES; ++i ) {
-       raw->throttle[i] = node->getDoubleValue( "throttle", 0.0 );
-       raw->mixture[i] = node->getDoubleValue( "mixture", 0.0 );
-       raw->fuel_pump_power[i]
+    net->num_engines = FGNetCtrls::FG_MAX_ENGINES;
+    for ( i = 0; i < FGNetCtrls::FG_MAX_ENGINES; ++i ) {
+       net->throttle[i] = node->getDoubleValue( "throttle", 0.0 );
+       net->mixture[i] = node->getDoubleValue( "mixture", 0.0 );
+       net->fuel_pump_power[i]
             = node->getDoubleValue( "/systems/electrical/outputs/fuel-pump",
                                     1.0 ) >= 1.0;
-       raw->prop_advance[i] = node->getDoubleValue( "propeller-pitch", 0.0 );
-       raw->magnetos[i] = node->getIntValue( "magnetos", 0 );
+       net->prop_advance[i] = node->getDoubleValue( "propeller-pitch", 0.0 );
+       net->magnetos[i] = node->getIntValue( "magnetos", 0 );
        if ( i == 0 ) {
          // cout << "Magnetos -> " << node->getIntValue( "magnetos", 0 );
        }
-       raw->starter_power[i]
+       net->starter_power[i]
             = node->getDoubleValue( "/systems/electrical/outputs/starter",
                                     1.0 ) >= 1.0;
        if ( i == 0 ) {
@@ -96,71 +100,91 @@ static void global2raw( FGRawCtrls *raw ) {
          //      << endl;
        }
     }
-    raw->num_tanks = FGRawCtrls::FG_MAX_TANKS;
-    for ( i = 0; i < FGRawCtrls::FG_MAX_TANKS; ++i ) {
+    net->num_tanks = FGNetCtrls::FG_MAX_TANKS;
+    for ( i = 0; i < FGNetCtrls::FG_MAX_TANKS; ++i ) {
         if ( node->getChild("fuel-selector", i) != 0 ) {
-            raw->fuel_selector[i]
+            net->fuel_selector[i]
                 = node->getChild("fuel-selector", i)->getDoubleValue();
         } else {
-            raw->fuel_selector[i] = false;
+            net->fuel_selector[i] = false;
         }
     }
-    raw->num_wheels = FGRawCtrls::FG_MAX_WHEELS;
-    for ( i = 0; i < FGRawCtrls::FG_MAX_WHEELS; ++i ) {
+    net->num_wheels = FGNetCtrls::FG_MAX_WHEELS;
+    for ( i = 0; i < FGNetCtrls::FG_MAX_WHEELS; ++i ) {
         if ( node->getChild("brakes", i) != 0 ) {
-            raw->brake[i]
-                = node->getChild("brakes", i)->getDoubleValue();
+            if ( node->getChild("parking-brake")->getDoubleValue() > 0.0 ) {
+                net->brake[i] = 1.0;
+           } else {
+                net->brake[i]
+                    = node->getChild("brakes", i)->getDoubleValue();
+            }
         } else {
-            raw->brake[i] = 0.0;
+            net->brake[i] = 0.0;
         }
     }
 
     node = fgGetNode("/controls/switches", true);
-    raw->master_bat = node->getChild("master-bat")->getBoolValue();
-    raw->master_alt = node->getChild("master-alt")->getBoolValue();
-    raw->master_avionics = node->getChild("master-avionics")->getBoolValue();
+    net->master_bat = node->getChild("master-bat")->getBoolValue();
+    net->master_alt = node->getChild("master-alt")->getBoolValue();
+    net->master_avionics = node->getChild("master-avionics")->getBoolValue();
+
+    net->wind_speed_kt = fgGetDouble("/environment/wind-speed-kt");
+    net->wind_dir_deg = fgGetDouble("/environment/wind-from-heading-deg");
 
     // cur_fdm_state->get_ground_elev_ft() is what we want ... this
     // reports the altitude of the aircraft.
     // "/environment/ground-elevation-m" reports the ground elevation
     // of the current view point which could change substantially if
     // the user is switching views.
-    raw->hground = cur_fdm_state->get_ground_elev_ft() * SG_FEET_TO_METER;
-    raw->magvar = fgGetDouble("/environment/magnetic-variation-deg");
-    raw->speedup = fgGetInt("/sim/speed-up");
+    net->hground = cur_fdm_state->get_ground_elev_ft() * SG_FEET_TO_METER;
+    net->magvar = fgGetDouble("/environment/magnetic-variation-deg");
+    net->speedup = fgGetInt("/sim/speed-up");
+    net->freeze = 0;
+    if ( fgGetBool("/sim/freeze/master") ) {
+        net->freeze |= 0x01;
+    }
+    if ( fgGetBool("/sim/freeze/position") ) {
+        net->freeze |= 0x02;
+    }
+    if ( fgGetBool("/sim/freeze/fuel") ) {
+        net->freeze |= 0x04;
+    }
 
     // convert to network byte order
-    raw->version = htonl(raw->version);
-    htond(raw->aileron);
-    htond(raw->elevator);
-    htond(raw->elevator_trim);
-    htond(raw->rudder);
-    htond(raw->flaps);
-    raw->flaps_power = htonl(raw->flaps_power);
-    for ( i = 0; i < FGRawCtrls::FG_MAX_ENGINES; ++i ) {
-       htond(raw->throttle[i]);
-       htond(raw->mixture[i]);
-        raw->fuel_pump_power[i] = htonl(raw->fuel_pump_power[i]);
-       htond(raw->prop_advance[i]);
-       raw->magnetos[i] = htonl(raw->magnetos[i]);
-       raw->starter_power[i] = htonl(raw->starter_power[i]);
+    net->version = htonl(net->version);
+    htond(net->aileron);
+    htond(net->elevator);
+    htond(net->elevator_trim);
+    htond(net->rudder);
+    htond(net->flaps);
+    net->flaps_power = htonl(net->flaps_power);
+    for ( i = 0; i < FGNetCtrls::FG_MAX_ENGINES; ++i ) {
+       htond(net->throttle[i]);
+       htond(net->mixture[i]);
+        net->fuel_pump_power[i] = htonl(net->fuel_pump_power[i]);
+       htond(net->prop_advance[i]);
+       net->magnetos[i] = htonl(net->magnetos[i]);
+       net->starter_power[i] = htonl(net->starter_power[i]);
     }
-    raw->num_engines = htonl(raw->num_engines);
-    for ( i = 0; i < FGRawCtrls::FG_MAX_TANKS; ++i ) {
-        raw->fuel_selector[i] = htonl(raw->fuel_selector[i]);
+    net->num_engines = htonl(net->num_engines);
+    for ( i = 0; i < FGNetCtrls::FG_MAX_TANKS; ++i ) {
+        net->fuel_selector[i] = htonl(net->fuel_selector[i]);
     }
-    raw->num_tanks = htonl(raw->num_tanks);
-    for ( i = 0; i < FGRawCtrls::FG_MAX_WHEELS; ++i ) {
-       htond(raw->brake[i]);
+    net->num_tanks = htonl(net->num_tanks);
+    for ( i = 0; i < FGNetCtrls::FG_MAX_WHEELS; ++i ) {
+       htond(net->brake[i]);
     }
-    raw->num_wheels = htonl(raw->num_wheels);
-    raw->gear_handle = htonl(raw->gear_handle);
-    raw->master_bat = htonl(raw->master_bat);
-    raw->master_alt = htonl(raw->master_alt);
-    raw->master_avionics = htonl(raw->master_avionics);
-    htond(raw->hground);
-    htond(raw->magvar);
-    raw->speedup = htonl(raw->speedup);
+    net->num_wheels = htonl(net->num_wheels);
+    net->gear_handle = htonl(net->gear_handle);
+    net->master_bat = htonl(net->master_bat);
+    net->master_alt = htonl(net->master_alt);
+    net->master_avionics = htonl(net->master_avionics);
+    htond(net->wind_speed_kt);
+    htond(net->wind_dir_deg);
+    htond(net->hground);
+    htond(net->magvar);
+    net->speedup = htonl(net->speedup);
+    net->freeze = htonl(net->freeze);
 }
 
 
@@ -185,6 +209,9 @@ static void net2global( FGNetFDM *net ) {
     htond(net->v_north);
     htond(net->v_east);
     htond(net->v_down);
+    htond(net->v_wind_body_north);
+    htond(net->v_wind_body_east);
+    htond(net->v_wind_body_down);
     htond(net->stall_warning);
 
     htond(net->A_X_pilot);
@@ -233,7 +260,11 @@ static void net2global( FGNetFDM *net ) {
         cur_fdm_state->_set_Velocities_Local( net->v_north,
                                               net->v_east,
                                               net->v_down );
-        fgSetDouble( "/sim/aero/alarms/stall-warning", net->stall_warning );
+        cur_fdm_state->_set_Velocities_Wind_Body( net->v_wind_body_north,
+                                                  net->v_wind_body_east,
+                                                  net->v_wind_body_down );
+
+        fgSetDouble( "/sim/alarms/stall-warning", net->stall_warning );
         cur_fdm_state->_set_Accels_Pilot_Body( net->A_X_pilot,
                                               net->A_Y_pilot,
                                               net->A_Z_pilot );
@@ -343,6 +374,15 @@ FGExternalNet::FGExternalNet( double dt, string host, int dop, int dip, int cp )
     // disable blocking
     data_server.setBlocking( false );
 
+#if defined( linux )
+    // set SO_REUSEADDR flag
+    int socket = data_server.getHandle();
+    int one = 1;
+    int result;
+    result = ::setsockopt( socket, SOL_SOCKET, SO_REUSEADDR,
+                           &one, sizeof(one) );
+#endif
+
     // allowed to read from a broadcast addr
     // data_server.setBroadcast( true );
 
@@ -370,39 +410,55 @@ void FGExternalNet::init() {
     // init method first.
     common_init();
 
-    double lon = fgGetDouble( "/position/longitude-deg" );
-    double lat = fgGetDouble( "/position/latitude-deg" );
+    double lon = fgGetDouble( "/sim/presets/longitude-deg" );
+    double lat = fgGetDouble( "/sim/presets/latitude-deg" );
+    double alt = fgGetDouble( "/sim/presets/altitude-ft" );
     double ground = fgGetDouble( "/environment/ground-elevation-m" );
-    double heading = fgGetDouble("/orientation/heading-deg");
+    double heading = fgGetDouble("/sim/presets/heading-deg");
+    double speed = fgGetDouble( "/sim/presets/airspeed-kt" );
 
     char cmd[256];
 
     HTTPClient *http;
     sprintf( cmd, "/longitude-deg?value=%.8f", lon );
     http = new HTTPClient( fdm_host.c_str(), cmd_port, cmd );
-    while ( !http->isDone() ) http->poll(0);
+    while ( !http->isDone(1000000) ) http->poll(0);
     delete http;
 
     sprintf( cmd, "/latitude-deg?value=%.8f", lat );
     http = new HTTPClient( fdm_host.c_str(), cmd_port, cmd );
-    while ( !http->isDone() ) http->poll(0);
+    while ( !http->isDone(1000000) ) http->poll(0);
+    delete http;
+
+    sprintf( cmd, "/altitude-ft?value=%.8f", alt );
+    http = new HTTPClient( fdm_host.c_str(), cmd_port, cmd );
+    while ( !http->isDone(1000000) ) http->poll(0);
     delete http;
 
     sprintf( cmd, "/ground-m?value=%.8f", ground );
     http = new HTTPClient( fdm_host.c_str(), cmd_port, cmd );
-    while ( !http->isDone() ) http->poll(0);
+    while ( !http->isDone(1000000) ) http->poll(0);
+    delete http;
+
+    sprintf( cmd, "/speed-kts?value=%.8f", speed );
+    http = new HTTPClient( fdm_host.c_str(), cmd_port, cmd );
+    while ( !http->isDone(1000000) ) http->poll(0);
     delete http;
 
     sprintf( cmd, "/heading-deg?value=%.8f", heading );
     http = new HTTPClient( fdm_host.c_str(), cmd_port, cmd );
-    while ( !http->isDone() ) http->poll(0);
+    while ( !http->isDone(1000000) ) http->poll(0);
     delete http;
 
     SG_LOG( SG_IO, SG_INFO, "before sending reset command." );
 
-    sprintf( cmd, "/reset?value=ground" );
+    if( fgGetBool("/sim/presets/onground") ) {
+      sprintf( cmd, "/reset?value=ground" );
+    } else {
+      sprintf( cmd, "/reset?value=air" );
+    }
     http = new HTTPClient( fdm_host.c_str(), cmd_port, cmd );
-    while ( !http->isDone() ) http->poll(0);
+    while ( !http->isDone(1000000) ) http->poll(0);
     delete http;
 
     SG_LOG( SG_IO, SG_INFO, "Remote FDM init() finished." );
@@ -419,7 +475,7 @@ void FGExternalNet::update( double dt ) {
 
     // Send control positions to remote fdm
     length = sizeof(ctrls);
-    global2raw( &ctrls );
+    global2net( &ctrls );
     if ( data_client.send( (char *)(& ctrls), length, 0 ) != length ) {
        SG_LOG( SG_IO, SG_DEBUG, "Error writing data." );
     } else {