FlightGear next
native_fdm.cxx
Go to the documentation of this file.
1// native_fdm.cxx -- FGFS "Native" flight dynamics protocal class
2//
3// Written by Curtis Olson, started September 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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/timing/sg_time.hxx>
31
32#if FG_HAVE_DDS
33#include <simgear/io/SGDataDistributionService.hxx>
34#endif
35
37#include <Main/fg_props.hxx>
38#include <Main/globals.hxx>
39#include <Scenery/scenery.hxx>
40
41#include "native_structs.hxx"
42#include "native_fdm.hxx"
43
44// FreeBSD works better with this included last ... (?)
45#if defined( _MSC_VER )
46# include <windows.h>
47#elif defined( __MINGW32__ )
48# include <winsock2.h>
49#else
50# include <netinet/in.h> // htonl() ntohl()
51#endif
52
53// open hailing frequencies
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 FG_HAVE_DDS
64 if ( io->get_type() == sgDDSType ) {
65 SG_DDS_Topic *dds = static_cast<SG_DDS_Topic*>(io);
66 dds->setup("FG_DDS_FDM", &FG_DDS_FDM_desc, sizeof(FG_DDS_FDM));
67 }
68#endif
69
70 if ( ! io->open( get_direction() ) ) {
71 SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
72 return false;
73 }
74
75 set_enabled( true );
76
77 // Is this really needed here ????
78 fgSetDouble("/position/sea-level-radius-ft", SG_EQUATORIAL_RADIUS_FT);
79
80 return true;
81}
82
83// process work for this port
85 SGIOChannel *io = get_io_channel();
86 int length;
87 char *buf;
88
89 if ( io->get_type() == sgDDSType ) {
90 buf = reinterpret_cast<char*>(&fdm.dds);
91 length = sizeof(FG_DDS_FDM);
92 } else {
93 buf = reinterpret_cast<char*>(&fdm.net);
94 length = sizeof(FGNetFDM);
95 }
96
97 if ( get_direction() == SG_IO_OUT ) {
98
99 if ( io->get_type() == sgDDSType ) {
100 FGProps2FDM( globals->get_props(), &fdm.dds );
101 } else {
102 FGProps2FDM( globals->get_props(), &fdm.net );
103 }
104
105 if ( ! io->write( buf, length ) ) {
106 SG_LOG( SG_IO, SG_ALERT, "Error writing data." );
107 return false;
108 }
109 } else if ( get_direction() == SG_IO_IN ) {
110 if ( io->get_type() == sgFileType ) {
111 if ( io->read( buf, length ) == length ) {
112 SG_LOG( SG_IO, SG_INFO, "Success reading data." );
113 FGFDM2Props( globals->get_props(), &fdm.net );
114 }
115 } else if ( io->get_type() == sgDDSType ) {
116 while ( io->read( buf, length ) == length ) {
117 SG_LOG( SG_IO, SG_INFO, " Success reading data." );
118 FGFDM2Props( globals->get_props(), &fdm.dds );
119 }
120 } else {
121 while ( io->read( buf, length ) == length ) {
122 SG_LOG( SG_IO, SG_INFO, " Success reading data." );
123 FGFDM2Props( globals->get_props(), &fdm.net );
124 }
125 }
126 }
127
128 return true;
129}
130
131// close the channel
133 SGIOChannel *io = get_io_channel();
134
135 set_enabled( false );
136
137 if ( ! io->close() ) {
138 return false;
139 }
140
141 return true;
142}
FG_DDS_FDM dds
bool process()
SGProtocolDir get_direction() const
Definition protocol.hxx:65
SGIOChannel * get_io_channel() const
Definition protocol.hxx:90
void set_enabled(const bool b)
Definition protocol.hxx:88
bool is_enabled() const
Definition protocol.hxx:87
const dds_topic_descriptor_t FG_DDS_FDM_desc
Definition dds_fdm.c:85
FGGlobals * globals
Definition globals.cxx:142
FGNetFDM FG_DDS_FDM
void FGProps2FDM(SGPropertyNode *props, T *net, bool net_byte_order=true)
void FGFDM2Props(SGPropertyNode *props, T *net, bool net_byte_order=true)
bool fgSetDouble(const char *name, double defaultValue)
Set a double value for a property.
Definition proptest.cpp:31