1
0
mirror of https://github.com/thooge/esp32-nmea2000-obp60.git synced 2025-12-16 15:33:05 +01:00

intermediate: restructure buffer handling

This commit is contained in:
wellenvogel
2021-12-01 22:55:37 +01:00
parent d8950c4eb4
commit 9dcb98bb51
7 changed files with 119 additions and 54 deletions

View File

@@ -4,6 +4,7 @@
#include <stdint.h>
#include "GwLog.h"
class GwBuffer;
class GwBufferWriter{
public:
int id=0; //can be set be users
@@ -12,11 +13,19 @@ class GwBufferWriter{
virtual ~GwBufferWriter(){};
};
class GwMessageFetcher{
public:
int id=0;
virtual bool handleBuffer(GwBuffer *buffer)=0;
virtual size_t fetchMessageToBuffer(GwBuffer *gwbuffer,uint8_t *buffer, size_t bufferLen,char delimiter);
};
/**
* an implementation of a
* buffer to safely inserte data if it fits
* and to write out data if possible
*/
typedef size_t (*GwBufferHandleFunction)(uint8_t *buffer, size_t len, void *param);
class GwBuffer{
public:
static const size_t TX_BUFFER_SIZE=1620; // app. 20 NMEA messages
@@ -36,25 +45,26 @@ class GwBuffer{
}
GwLog *logger;
void lp(const char *fkt,int p=0);
/**
* find the first occurance of x in the buffer, -1 if not found
*/
int findChar(char x);
public:
GwBuffer(GwLog *logger,size_t bufferSize);
~GwBuffer();
void reset();
void reset(String reason="");
size_t freeSpace();
size_t usedSpace();
size_t addData(const uint8_t *data,size_t len,bool addPartial=false);
size_t fillData(int maxLen,GwBufferHandleFunction handler, void *param);
int read();
int peek();
size_t fetchData(int maxLen,GwBufferHandleFunction handler, void *param);
/**
* write some data to the buffer writer
* return an error if the buffer writer returned < 0
*/
WriteStatus fetchData(GwBufferWriter *writer, int maxLen=-1,bool errorIf0 = true);
/**
* find the first occurance of x in the buffer, -1 if not found
*/
int findChar(char x);
WriteStatus fetchMessage(GwBufferWriter *writer,char delimiter,bool emptyIfFull=true);
};