iochannel.cxx iochannel.hxx \
fg_file.cxx fg_file.hxx \
fg_serial.cxx fg_serial.hxx \
+ fg_socket.cxx fg_socket.hxx \
protocol.cxx protocol.hxx \
garmin.cxx garmin.hxx \
nmea.cxx nmea.hxx \
#include STL_STRING
#include <Debug/logstream.hxx>
-#include <Time/fg_time.hxx>
#include "fg_file.hxx"
}
-// read data from file
-bool FGFile::read( char *buf, int *length ) {
+// read a block of data of specified size
+int FGFile::read( char *buf, int length ) {
+ // read a chunk
+ int result = std::read( fp, buf, length );
+
+ return result;
+}
+
+
+// read a line of data, length is max size of input buffer
+int FGFile::readline( char *buf, int length ) {
// save our current position
int pos = lseek( fp, 0, SEEK_CUR );
// read a chunk
- int result = std::read( fp, buf, FG_MAX_MSG_SIZE );
+ int result = std::read( fp, buf, length );
// find the end of line and reset position
int i;
for ( i = 0; i < result && buf[i] != '\n'; ++i );
if ( buf[i] == '\n' ) {
- *length = i + 1;
+ result = i + 1;
} else {
- *length = i;
+ result = i;
}
- lseek( fp, pos + *length, SEEK_SET );
+ lseek( fp, pos + result, SEEK_SET );
// just in case ...
- buf[ *length ] = '\0';
+ buf[ result ] = '\0';
- return true;
+ return result;
}
// write data to a file
-bool FGFile::write( char *buf, int length ) {
+int FGFile::write( char *buf, int length ) {
int result = std::write( fp, buf, length );
if ( result != length ) {
FG_LOG( FG_IO, FG_ALERT, "Error writing data: " << file_name );
- return false;
}
- return true;
+ return result;
}
// open the file based on specified direction
bool open( FGProtocol::fgProtocolDir dir );
- // read data from file
- bool read( char *buf, int *length );
+ // read a block of data of specified size
+ int read( char *buf, int length );
+
+ // read a line of data, length is max size of input buffer
+ int readline( char *buf, int length );
// write data to a file
- bool write( char *buf, int length );
+ int write( char *buf, int length );
// close file
bool close();
#include <Debug/logstream.hxx>
#include <Aircraft/aircraft.hxx>
-#include <Include/fg_constants.h>
#include <Serial/serial.hxx>
-#include <Time/fg_time.hxx>
#include "fg_serial.hxx"
FG_USING_STD(string);
-FGSerial::FGSerial() {
+FGSerial::FGSerial() :
+ save_len(0)
+{
}
}
-// read data from port
-bool FGSerial::read( char *buf, int *length ) {
- // read a chunk
- *length = port.read_port( buf );
+// Read data from port. If we don't get enough data, save what we did
+// get in the save buffer and return 0. The save buffer will be
+// prepended to subsequent reads until we get as much as is requested.
+
+int FGSerial::read( char *buf, int length ) {
+ int result;
+
+ // read a chunk, keep in the save buffer until we have the
+ // requested amount read
+
+ char *buf_ptr = save_buf + save_len;
+ result = port.read_port( buf_ptr, length - save_len );
- // just in case ...
- buf[ *length ] = '\0';
+ if ( result + save_len == length ) {
+ strncpy( buf, save_buf, length );
+ save_len = 0;
- return true;
+ return length;
+ }
+
+ return 0;
+}
+
+
+// read data from port
+int FGSerial::readline( char *buf, int length ) {
+ int result;
+
+ // read a chunk, keep in the save buffer until we have the
+ // requested amount read
+
+ char *buf_ptr = save_buf + save_len;
+ result = port.read_port( buf_ptr, FG_MAX_MSG_SIZE - save_len );
+ save_len += result;
+
+ // look for the end of line in save_buf
+ int i;
+ for ( i = 0; i < save_len && save_buf[i] != '\n'; ++i );
+ if ( save_buf[i] == '\n' ) {
+ result = i + 1;
+ } else {
+ // no end of line yet
+ return 0;
+ }
+
+ // we found an end of line
+
+ // copy to external buffer
+ strncpy( buf, save_buf, result );
+ buf[result] = '\0';
+ cout << "fg_serial line = " << buf << endl;
+
+ // shift save buffer
+ for ( i = result; i < save_len; ++i ) {
+ save_buf[ i - result ] = save_buf[result];
+ }
+ save_len -= result;
+
+ return result;
}
+
// write data to port
-bool FGSerial::write( char *buf, int length ) {
+int FGSerial::write( char *buf, int length ) {
int result = port.write_port( buf, length );
if ( result != length ) {
FG_LOG( FG_IO, FG_ALERT, "Error writing data: " << device );
- return false;
}
- return true;
+ return result;
}
string baud;
FGSerialPort port;
+ char save_buf[ 2 * FG_MAX_MSG_SIZE ];
+ int save_len;
+
public:
FGSerial();
// open the serial port based on specified direction
bool open( FGProtocol::fgProtocolDir dir );
- // read data from port
- bool read( char *buf, int *length );
+ // read a block of data of specified size
+ int read( char *buf, int length );
+
+ // read a line of data, length is max size of input buffer
+ int readline( char *buf, int length );
- // write data to port
- bool write( char *buf, int length );
+ // write data to a file
+ int write( char *buf, int length );
// close port
bool close();
return false;
}
} else if ( get_direction() == in ) {
- if ( io->read( buf, &length ) ) {
+ if ( io->readline( buf, FG_MAX_MSG_SIZE ) ) {
parse_message();
} else {
FG_LOG( FG_IO, FG_ALERT, "Error reading data." );
return false;
}
- if ( io->read( buf, &length ) ) {
+ if ( io->readline( buf, FG_MAX_MSG_SIZE ) ) {
parse_message();
} else {
FG_LOG( FG_IO, FG_ALERT, "Error reading data." );
// dummy process routine
-bool FGIOChannel::read( char *buf, int *length ) {
- return false;
+int FGIOChannel::read( char *buf, int length ) {
+ return 0;
+}
+
+
+// dummy process routine
+int FGIOChannel::readline( char *buf, int length ) {
+ return 0;
}
// dummy process routine
-bool FGIOChannel::write( char *buf, int length ) {
+int FGIOChannel::write( char *buf, int length ) {
return false;
}
virtual ~FGIOChannel();
virtual bool open( FGProtocol::fgProtocolDir dir );
- virtual bool read( char *buf, int *length );
- virtual bool write( char *buf, int length );
+ virtual int read( char *buf, int length );
+ virtual int readline( char *buf, int length );
+ virtual int write( char *buf, int length );
virtual bool close();
};
return false;
}
} else if ( get_direction() == in ) {
- if ( io->read( buf, &length ) ) {
+ if ( io->readline( buf, FG_MAX_MSG_SIZE ) ) {
+ parse_message();
+ } else {
+ FG_LOG( FG_IO, FG_ALERT, "Error reading data." );
+ return false;
+ }
+ if ( io->readline( buf, FG_MAX_MSG_SIZE ) ) {
parse_message();
} else {
FG_LOG( FG_IO, FG_ALERT, "Error reading data." );