intermediate: restructure serial handling

This commit is contained in:
andreas 2024-11-01 17:01:44 +01:00
parent 0ddc0d055d
commit 56ec7a0406
4 changed files with 32 additions and 18 deletions

View File

@ -83,7 +83,7 @@ public:
};
template<typename T>
class SerialWrapper : public GwChannelList::SerialWrapperBase{
class SerialWrapper : public GwSerial::SerialWrapperBase{
private:
template<class C>
void beginImpl(C *s,unsigned long baud, uint32_t config=SERIAL_8N1, int8_t rxPin=-1, int8_t txPin=-1){}
@ -184,7 +184,7 @@ void GwChannelList::addSerial(int id, int rx, int tx, int type){
}
LOG_DEBUG(GwLog::ERROR,"invalid serial config with id %d",id);
}
void GwChannelList::addSerial(GwChannelList::SerialWrapperBase *stream,int type,int rx,int tx){
void GwChannelList::addSerial(GwSerial::SerialWrapperBase *stream,int type,int rx,int tx){
const char *mode=nullptr;
switch (type)
{
@ -207,7 +207,7 @@ void GwChannelList::addSerial(GwChannelList::SerialWrapperBase *stream,int type,
}
addSerial(stream,mode,rx,tx);
}
void GwChannelList::addSerial(GwChannelList::SerialWrapperBase *serialStream,const String &mode,int rx,int tx){
void GwChannelList::addSerial(GwSerial::SerialWrapperBase *serialStream,const String &mode,int rx,int tx){
int id=serialStream->getId();
for (auto &&it:theChannels){
if (it->isOwnSource(id)){
@ -251,7 +251,7 @@ void GwChannelList::addSerial(GwChannelList::SerialWrapperBase *serialStream,con
LOG_DEBUG(GwLog::DEBUG,"serial set up: mode=%s,rx=%d,canRead=%d,tx=%d,canWrite=%d",
mode.c_str(),rx,(int)canRead,tx,(int)canWrite);
serialStream->begin(logger,config->getInt(param->baud,115200),SERIAL_8N1,rx,tx);
GwSerial *serial = new GwSerial(logger, serialStream->getStream(), id, canRead);
GwSerial *serial = new GwSerial(logger, serialStream, canRead);
LOG_DEBUG(GwLog::LOG, "starting serial %d ", id);
GwChannel *channel = new GwChannel(logger, param->name, id);
channel->setImpl(serial);
@ -303,8 +303,9 @@ void GwChannelList::begin(bool fallbackSerial){
GwChannel *channel=NULL;
//usb
if (! fallbackSerial){
GwSerial *usb=new GwSerial(NULL,&USBSerial,USB_CHANNEL_ID);
USBSerial.begin(config->getInt(config->usbBaud));
GwSerial::SerialWrapperBase *usbWrapper=new SerialWrapper<decltype(USBSerial)>(&USBSerial,USB_CHANNEL_ID);
usbWrapper->begin(NULL,config->getInt(config->usbBaud));
GwSerial *usb=new GwSerial(NULL,usbWrapper);
logger->setWriter(new GwSerialLog(usb,config->getBool(config->usbActisense),getFlushTimeout(USBSerial)));
logger->prefix="GWSERIAL:";
channel=new GwChannel(logger,"USB",USB_CHANNEL_ID);

View File

@ -8,6 +8,7 @@
#include "GWConfig.h"
#include "GwJsonDocument.h"
#include "GwApi.h"
#include "GwSerial.h"
#include <HardwareSerial.h>
//NMEA message channels
@ -23,12 +24,6 @@ class GwSocketServer;
class GwTcpClient;
class GwChannelList{
private:
class SerialWrapperBase{
public:
virtual void begin(GwLog* logger,unsigned long baud, uint32_t config=SERIAL_8N1, int8_t rxPin=-1, int8_t txPin=-1)=0;
virtual Stream *getStream()=0;
virtual int getId()=0;
};
GwLog *logger;
GwConfigHandler *config;
typedef std::vector<GwChannel *> ChannelList;
@ -36,8 +31,8 @@ class GwChannelList{
std::map<int,String> modes;
GwSocketServer *sockets;
GwTcpClient *client;
void addSerial(SerialWrapperBase *stream,const String &mode,int rx,int tx);
void addSerial(SerialWrapperBase *stream,int type,int rx,int tx);
void addSerial(GwSerial::SerialWrapperBase *stream,const String &mode,int rx,int tx);
void addSerial(GwSerial::SerialWrapperBase *stream,int type,int rx,int tx);
public:
void addSerial(int id, int rx, int tx, int type);
GwChannelList(GwLog *logger, GwConfigHandler *config);

View File

@ -40,10 +40,10 @@ class GwSerialStream: public Stream{
GwSerial::GwSerial(GwLog *logger, Stream *s, int id,bool allowRead):serial(s)
GwSerial::GwSerial(GwLog *logger, GwSerial::SerialWrapperBase *s, bool allowRead):serial(s)
{
LOG_DEBUG(GwLog::DEBUG,"creating GwSerial %p id %d",this,id);
this->id=id;
this->id=s->getId();
this->logger = logger;
String bufName="Ser(";
bufName+=String(id);

View File

@ -16,11 +16,27 @@ class GwSerial : public GwChannelInterface{
int id=-1;
int overflows=0;
size_t enqueue(const uint8_t *data, size_t len,bool partial=false);
Stream *serial;
bool availableWrite=false; //if this is false we will wait for availabkleWrite until we flush again
public:
class SerialWrapperBase{
public:
virtual void begin(GwLog* logger,unsigned long baud, uint32_t config=SERIAL_8N1, int8_t rxPin=-1, int8_t txPin=-1)=0;
virtual int getId()=0;
virtual int available(){return getStream()->available();}
size_t readBytes(uint8_t *buffer, size_t length){
return getStream()->readBytes(buffer,length);
}
virtual int availableForWrite(void){
return getStream()->availableForWrite();
}
size_t write(const uint8_t *buffer, size_t size){
return getStream()->write(buffer,size);
}
private:
virtual Stream *getStream()=0;
};
static const int bufferSize=200;
GwSerial(GwLog *logger,Stream *stream,int id,bool allowRead=true);
GwSerial(GwLog *logger,SerialWrapperBase *stream,bool allowRead=true);
~GwSerial();
bool isInitialized();
virtual size_t sendToClients(const char *buf,int sourceId,bool partial=false);
@ -30,5 +46,7 @@ class GwSerial : public GwChannelInterface{
virtual Stream *getStream(bool partialWrites);
bool getAvailableWrite(){return availableWrite;}
friend GwSerialStream;
private:
SerialWrapperBase *serial;
};
#endif