1999-11-23 05:47:16 +00:00
|
|
|
// native.cxx -- FGFS "Native" protocal class
|
|
|
|
//
|
|
|
|
// Written by Curtis Olson, started November 1999.
|
|
|
|
//
|
2004-11-19 22:10:41 +00:00
|
|
|
// Copyright (C) 1999 Curtis L. Olson - http://www.flightgear.org/~curt
|
1999-11-23 05:47:16 +00:00
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU General Public License as
|
|
|
|
// published by the Free Software Foundation; either version 2 of the
|
|
|
|
// License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful, but
|
|
|
|
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program; if not, write to the Free Software
|
2006-02-21 01:16:04 +00:00
|
|
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
1999-11-23 05:47:16 +00:00
|
|
|
//
|
|
|
|
// $Id$
|
|
|
|
|
|
|
|
|
2000-02-16 23:01:03 +00:00
|
|
|
#include <simgear/debug/logstream.hxx>
|
2000-07-11 20:40:12 +00:00
|
|
|
#include <simgear/io/iochannel.hxx>
|
1999-11-23 05:47:16 +00:00
|
|
|
|
|
|
|
#include "native.hxx"
|
|
|
|
|
|
|
|
|
|
|
|
FGNative::FGNative() {
|
|
|
|
}
|
|
|
|
|
|
|
|
FGNative::~FGNative() {
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// open hailing frequencies
|
|
|
|
bool FGNative::open() {
|
|
|
|
if ( is_enabled() ) {
|
2001-03-24 06:03:11 +00:00
|
|
|
SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel "
|
1999-11-23 05:47:16 +00:00
|
|
|
<< "is already in use, ignoring" );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2000-07-11 20:40:12 +00:00
|
|
|
SGIOChannel *io = get_io_channel();
|
1999-11-23 05:47:16 +00:00
|
|
|
|
|
|
|
if ( ! io->open( get_direction() ) ) {
|
2001-03-24 06:03:11 +00:00
|
|
|
SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." );
|
1999-11-23 05:47:16 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
set_enabled( true );
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// process work for this port
|
|
|
|
bool FGNative::process() {
|
2000-07-11 20:40:12 +00:00
|
|
|
SGIOChannel *io = get_io_channel();
|
1999-11-23 05:47:16 +00:00
|
|
|
int length = sizeof(*cur_fdm_state);
|
|
|
|
|
2000-07-11 20:40:12 +00:00
|
|
|
if ( get_direction() == SG_IO_OUT ) {
|
1999-11-23 05:47:16 +00:00
|
|
|
// cout << "size of cur_fdm_state = " << length << endl;
|
|
|
|
buf = *cur_fdm_state;
|
|
|
|
if ( ! io->write( (char *)(& buf), length ) ) {
|
2001-03-24 06:03:11 +00:00
|
|
|
SG_LOG( SG_IO, SG_ALERT, "Error writing data." );
|
1999-11-23 05:47:16 +00:00
|
|
|
return false;
|
|
|
|
}
|
2000-07-11 20:40:12 +00:00
|
|
|
} else if ( get_direction() == SG_IO_IN ) {
|
|
|
|
if ( io->get_type() == sgFileType ) {
|
2000-06-01 21:13:26 +00:00
|
|
|
if ( io->read( (char *)(& buf), length ) == length ) {
|
2001-03-24 06:03:11 +00:00
|
|
|
SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
|
2000-06-01 21:13:26 +00:00
|
|
|
*cur_fdm_state = buf;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
while ( io->read( (char *)(& buf), length ) == length ) {
|
2001-03-24 06:03:11 +00:00
|
|
|
SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
|
2000-06-01 21:13:26 +00:00
|
|
|
*cur_fdm_state = buf;
|
|
|
|
}
|
1999-11-23 05:47:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// close the channel
|
|
|
|
bool FGNative::close() {
|
2000-07-11 20:40:12 +00:00
|
|
|
SGIOChannel *io = get_io_channel();
|
1999-11-23 05:47:16 +00:00
|
|
|
|
|
|
|
set_enabled( false );
|
|
|
|
|
|
|
|
if ( ! io->close() ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|