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

View File

@ -8,6 +8,7 @@
#include "GWConfig.h" #include "GWConfig.h"
#include "GwJsonDocument.h" #include "GwJsonDocument.h"
#include "GwApi.h" #include "GwApi.h"
#include "GwSerial.h"
#include <HardwareSerial.h> #include <HardwareSerial.h>
//NMEA message channels //NMEA message channels
@ -23,12 +24,6 @@ class GwSocketServer;
class GwTcpClient; class GwTcpClient;
class GwChannelList{ class GwChannelList{
private: 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; GwLog *logger;
GwConfigHandler *config; GwConfigHandler *config;
typedef std::vector<GwChannel *> ChannelList; typedef std::vector<GwChannel *> ChannelList;
@ -36,8 +31,8 @@ class GwChannelList{
std::map<int,String> modes; std::map<int,String> modes;
GwSocketServer *sockets; GwSocketServer *sockets;
GwTcpClient *client; GwTcpClient *client;
void addSerial(SerialWrapperBase *stream,const String &mode,int rx,int tx); void addSerial(GwSerial::SerialWrapperBase *stream,const String &mode,int rx,int tx);
void addSerial(SerialWrapperBase *stream,int type,int rx,int tx); void addSerial(GwSerial::SerialWrapperBase *stream,int type,int rx,int tx);
public: public:
void addSerial(int id, int rx, int tx, int type); void addSerial(int id, int rx, int tx, int type);
GwChannelList(GwLog *logger, GwConfigHandler *config); 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); LOG_DEBUG(GwLog::DEBUG,"creating GwSerial %p id %d",this,id);
this->id=id; this->id=s->getId();
this->logger = logger; this->logger = logger;
String bufName="Ser("; String bufName="Ser(";
bufName+=String(id); bufName+=String(id);

View File

@ -16,11 +16,27 @@ class GwSerial : public GwChannelInterface{
int id=-1; int id=-1;
int overflows=0; int overflows=0;
size_t enqueue(const uint8_t *data, size_t len,bool partial=false); 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 bool availableWrite=false; //if this is false we will wait for availabkleWrite until we flush again
public: 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; static const int bufferSize=200;
GwSerial(GwLog *logger,Stream *stream,int id,bool allowRead=true); GwSerial(GwLog *logger,SerialWrapperBase *stream,bool allowRead=true);
~GwSerial(); ~GwSerial();
bool isInitialized(); bool isInitialized();
virtual size_t sendToClients(const char *buf,int sourceId,bool partial=false); 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); virtual Stream *getStream(bool partialWrites);
bool getAvailableWrite(){return availableWrite;} bool getAvailableWrite(){return availableWrite;}
friend GwSerialStream; friend GwSerialStream;
private:
SerialWrapperBase *serial;
}; };
#endif #endif