Added a readline(buf) vs. read(buf,len)
This commit is contained in:
parent
24caa82715
commit
4f294285fe
9 changed files with 121 additions and 41 deletions
|
@ -4,6 +4,7 @@ libNetwork_a_SOURCES = \
|
||||||
iochannel.cxx iochannel.hxx \
|
iochannel.cxx iochannel.hxx \
|
||||||
fg_file.cxx fg_file.hxx \
|
fg_file.cxx fg_file.hxx \
|
||||||
fg_serial.cxx fg_serial.hxx \
|
fg_serial.cxx fg_serial.hxx \
|
||||||
|
fg_socket.cxx fg_socket.hxx \
|
||||||
protocol.cxx protocol.hxx \
|
protocol.cxx protocol.hxx \
|
||||||
garmin.cxx garmin.hxx \
|
garmin.cxx garmin.hxx \
|
||||||
nmea.cxx nmea.hxx \
|
nmea.cxx nmea.hxx \
|
||||||
|
|
|
@ -26,7 +26,6 @@
|
||||||
#include STL_STRING
|
#include STL_STRING
|
||||||
|
|
||||||
#include <Debug/logstream.hxx>
|
#include <Debug/logstream.hxx>
|
||||||
#include <Time/fg_time.hxx>
|
|
||||||
|
|
||||||
#include "fg_file.hxx"
|
#include "fg_file.hxx"
|
||||||
|
|
||||||
|
@ -64,40 +63,48 @@ bool FGFile::open( FGProtocol::fgProtocolDir dir ) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// read data from file
|
// read a block of data of specified size
|
||||||
bool FGFile::read( char *buf, int *length ) {
|
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
|
// save our current position
|
||||||
int pos = lseek( fp, 0, SEEK_CUR );
|
int pos = lseek( fp, 0, SEEK_CUR );
|
||||||
|
|
||||||
// read a chunk
|
// 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
|
// find the end of line and reset position
|
||||||
int i;
|
int i;
|
||||||
for ( i = 0; i < result && buf[i] != '\n'; ++i );
|
for ( i = 0; i < result && buf[i] != '\n'; ++i );
|
||||||
if ( buf[i] == '\n' ) {
|
if ( buf[i] == '\n' ) {
|
||||||
*length = i + 1;
|
result = i + 1;
|
||||||
} else {
|
} else {
|
||||||
*length = i;
|
result = i;
|
||||||
}
|
}
|
||||||
lseek( fp, pos + *length, SEEK_SET );
|
lseek( fp, pos + result, SEEK_SET );
|
||||||
|
|
||||||
// just in case ...
|
// just in case ...
|
||||||
buf[ *length ] = '\0';
|
buf[ result ] = '\0';
|
||||||
|
|
||||||
return true;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// write data to a file
|
// 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 );
|
int result = std::write( fp, buf, length );
|
||||||
if ( result != length ) {
|
if ( result != length ) {
|
||||||
FG_LOG( FG_IO, FG_ALERT, "Error writing data: " << file_name );
|
FG_LOG( FG_IO, FG_ALERT, "Error writing data: " << file_name );
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -57,11 +57,14 @@ public:
|
||||||
// open the file based on specified direction
|
// open the file based on specified direction
|
||||||
bool open( FGProtocol::fgProtocolDir dir );
|
bool open( FGProtocol::fgProtocolDir dir );
|
||||||
|
|
||||||
// read data from file
|
// read a block of data of specified size
|
||||||
bool read( char *buf, int *length );
|
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
|
// write data to a file
|
||||||
bool write( char *buf, int length );
|
int write( char *buf, int length );
|
||||||
|
|
||||||
// close file
|
// close file
|
||||||
bool close();
|
bool close();
|
||||||
|
|
|
@ -27,16 +27,16 @@
|
||||||
|
|
||||||
#include <Debug/logstream.hxx>
|
#include <Debug/logstream.hxx>
|
||||||
#include <Aircraft/aircraft.hxx>
|
#include <Aircraft/aircraft.hxx>
|
||||||
#include <Include/fg_constants.h>
|
|
||||||
#include <Serial/serial.hxx>
|
#include <Serial/serial.hxx>
|
||||||
#include <Time/fg_time.hxx>
|
|
||||||
|
|
||||||
#include "fg_serial.hxx"
|
#include "fg_serial.hxx"
|
||||||
|
|
||||||
FG_USING_STD(string);
|
FG_USING_STD(string);
|
||||||
|
|
||||||
|
|
||||||
FGSerial::FGSerial() {
|
FGSerial::FGSerial() :
|
||||||
|
save_len(0)
|
||||||
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -62,27 +62,77 @@ bool FGSerial::open( FGProtocol::fgProtocolDir dir ) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// read data from port
|
// Read data from port. If we don't get enough data, save what we did
|
||||||
bool FGSerial::read( char *buf, int *length ) {
|
// get in the save buffer and return 0. The save buffer will be
|
||||||
// read a chunk
|
// prepended to subsequent reads until we get as much as is requested.
|
||||||
*length = port.read_port( buf );
|
|
||||||
|
|
||||||
// just in case ...
|
|
||||||
buf[ *length ] = '\0';
|
|
||||||
|
|
||||||
return true;
|
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 );
|
||||||
|
|
||||||
|
if ( result + save_len == length ) {
|
||||||
|
strncpy( buf, save_buf, length );
|
||||||
|
save_len = 0;
|
||||||
|
|
||||||
|
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
|
// 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 );
|
int result = port.write_port( buf, length );
|
||||||
|
|
||||||
if ( result != length ) {
|
if ( result != length ) {
|
||||||
FG_LOG( FG_IO, FG_ALERT, "Error writing data: " << device );
|
FG_LOG( FG_IO, FG_ALERT, "Error writing data: " << device );
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -53,6 +53,9 @@ class FGSerial : public FGIOChannel {
|
||||||
string baud;
|
string baud;
|
||||||
FGSerialPort port;
|
FGSerialPort port;
|
||||||
|
|
||||||
|
char save_buf[ 2 * FG_MAX_MSG_SIZE ];
|
||||||
|
int save_len;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
FGSerial();
|
FGSerial();
|
||||||
|
@ -61,11 +64,14 @@ public:
|
||||||
// open the serial port based on specified direction
|
// open the serial port based on specified direction
|
||||||
bool open( FGProtocol::fgProtocolDir dir );
|
bool open( FGProtocol::fgProtocolDir dir );
|
||||||
|
|
||||||
// read data from port
|
// read a block of data of specified size
|
||||||
bool read( char *buf, int *length );
|
int read( char *buf, int length );
|
||||||
|
|
||||||
// write data to port
|
// read a line of data, length is max size of input buffer
|
||||||
bool write( char *buf, int length );
|
int readline( char *buf, int length );
|
||||||
|
|
||||||
|
// write data to a file
|
||||||
|
int write( char *buf, int length );
|
||||||
|
|
||||||
// close port
|
// close port
|
||||||
bool close();
|
bool close();
|
||||||
|
|
|
@ -373,13 +373,13 @@ bool FGGarmin::process() {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
} else if ( get_direction() == in ) {
|
} else if ( get_direction() == in ) {
|
||||||
if ( io->read( buf, &length ) ) {
|
if ( io->readline( buf, FG_MAX_MSG_SIZE ) ) {
|
||||||
parse_message();
|
parse_message();
|
||||||
} else {
|
} else {
|
||||||
FG_LOG( FG_IO, FG_ALERT, "Error reading data." );
|
FG_LOG( FG_IO, FG_ALERT, "Error reading data." );
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if ( io->read( buf, &length ) ) {
|
if ( io->readline( buf, FG_MAX_MSG_SIZE ) ) {
|
||||||
parse_message();
|
parse_message();
|
||||||
} else {
|
} else {
|
||||||
FG_LOG( FG_IO, FG_ALERT, "Error reading data." );
|
FG_LOG( FG_IO, FG_ALERT, "Error reading data." );
|
||||||
|
|
|
@ -45,13 +45,19 @@ bool FGIOChannel::open( FGProtocol::fgProtocolDir dir ) {
|
||||||
|
|
||||||
|
|
||||||
// dummy process routine
|
// dummy process routine
|
||||||
bool FGIOChannel::read( char *buf, int *length ) {
|
int FGIOChannel::read( char *buf, int length ) {
|
||||||
return false;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// dummy process routine
|
// dummy process routine
|
||||||
bool FGIOChannel::write( char *buf, int length ) {
|
int FGIOChannel::readline( char *buf, int length ) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// dummy process routine
|
||||||
|
int FGIOChannel::write( char *buf, int length ) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -48,8 +48,9 @@ public:
|
||||||
virtual ~FGIOChannel();
|
virtual ~FGIOChannel();
|
||||||
|
|
||||||
virtual bool open( FGProtocol::fgProtocolDir dir );
|
virtual bool open( FGProtocol::fgProtocolDir dir );
|
||||||
virtual bool read( char *buf, int *length );
|
virtual int read( char *buf, int length );
|
||||||
virtual bool write( char *buf, int length );
|
virtual int readline( char *buf, int length );
|
||||||
|
virtual int write( char *buf, int length );
|
||||||
virtual bool close();
|
virtual bool close();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -476,7 +476,13 @@ bool FGNMEA::process() {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
} else if ( get_direction() == in ) {
|
} 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();
|
parse_message();
|
||||||
} else {
|
} else {
|
||||||
FG_LOG( FG_IO, FG_ALERT, "Error reading data." );
|
FG_LOG( FG_IO, FG_ALERT, "Error reading data." );
|
||||||
|
|
Loading…
Add table
Reference in a new issue