1 // native.cxx -- FGFS "Native" protocal class
3 // Written by Curtis Olson, started November 1999.
5 // Copyright (C) 1999 Curtis L. Olson - http://www.flightgear.org/~curt
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.
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.
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.
27 #include <simgear/debug/logstream.hxx>
28 #include <simgear/io/iochannel.hxx>
32 #include <FDM/flight.hxx>
34 FGNative::FGNative() {
37 FGNative::~FGNative() {
41 // open hailing frequencies
42 bool FGNative::open() {
44 SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel "
45 << "is already in use, ignoring" );
49 SGIOChannel *io = get_io_channel();
51 if ( ! io->open( get_direction() ) ) {
52 SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
62 * The design of FGNative requires direct, memcpy access to FGInterface,
63 * unfortunately. Since this is the only remaining place that does, the
64 * extern lives here, rather than a header file.
67 extern FGInterface* evil_global_fdm_state;
69 // process work for this port
70 bool FGNative::process() {
71 SGIOChannel *io = get_io_channel();
72 int length = sizeof(FGInterface);
74 if ( get_direction() == SG_IO_OUT ) {
75 buf = *evil_global_fdm_state;
76 if ( ! io->write( (char *)(& buf), length ) ) {
77 SG_LOG( SG_IO, SG_ALERT, "Error writing data." );
80 } else if ( get_direction() == SG_IO_IN ) {
81 if ( io->get_type() == sgFileType ) {
82 if ( io->read( (char *)(& buf), length ) == length ) {
83 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
84 *evil_global_fdm_state = buf;
87 while ( io->read( (char *)(& buf), length ) == length ) {
88 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
89 *evil_global_fdm_state = buf;
99 bool FGNative::close() {
100 SGIOChannel *io = get_io_channel();
102 set_enabled( false );
104 if ( ! io->close() ) {