diff --git a/APRSFrame.h b/APRSFrame.h new file mode 100644 index 0000000..39494eb --- /dev/null +++ b/APRSFrame.h @@ -0,0 +1,36 @@ +/* + * Copyright (C) 2021-2022 by Geoffrey Merck F4FXL / KC3FRA + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +#pragma once + +#include +#include + +// We only support these types for now +enum APRS_FRAME_TYPE { + APFT_UNKNOWN = 0, + APFT_MESSAGE, +}; + +typedef struct { + std::string m_source; + std::string m_dest; + std::vector m_path; + std::string m_body; + APRS_FRAME_TYPE m_type; +} TAPRSFrame; \ No newline at end of file diff --git a/APRSParser.cpp b/APRSParser.cpp new file mode 100644 index 0000000..d3ed20d --- /dev/null +++ b/APRSParser.cpp @@ -0,0 +1,58 @@ +/* + * Copyright (C) 2021-2022 by Geoffrey Merck F4FXL / KC3FRA + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +#include "APRSParser.h" + +bool CAPRSParser::parseFrame(const std::string& frameStr, TAPRSFrame& frame) +{ + frame.m_body.clear(); + frame.m_dest.clear(); + frame.m_path.clear(); + frame.m_source.clear(); + frame.m_type = APFT_UNKNOWN; + + if(!frameStr.empty()) + return false; + + auto pos = frameStr.find_first_of(':'); + if(pos == std::string::npos || pos == frameStr.length() - 1) + return false; + + auto header = frameStr.substr(0, pos); // contains sours, dest and path + auto body = frameStr.substr(pos +1); + + std::vector headerSplits; + boost::split(headerSplits, header, [](char c) { return c == ',' || c == '>';}); + + if(headerSplits.size() < 2) //we need at least source and dest to form a valid frame + return false; + + frame.m_source.assign(headerSplits[0]); + frame.m_dest.assign(headerSplits[1]); + + for(unsigned int i = 2; i < headerSplits.size(); i++) { + frame.m_path.push_back(headerSplits[i]); + } + + frame.m_body.assign(body); + + frame.m_type = body[0] == ':' ? APFT_MESSAGE : APFT_UNKNOWN; + + return true; +} + diff --git a/APRSParser.h b/APRSParser.h new file mode 100644 index 0000000..ede7c2c --- /dev/null +++ b/APRSParser.h @@ -0,0 +1,31 @@ +/* + * Copyright (C) 2021-2022 by Geoffrey Merck F4FXL / KC3FRA + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +#pragma once + +#include +#include + +#include "APRSFrame.h" + +class CAPRSParser +{ +public: + //TODO 2022-01-02 move these to own class ? + static bool parseFrame(const std::string& frameStr, TAPRSFrame& frame); +}; \ No newline at end of file diff --git a/APRSReader.cpp b/APRSReader.cpp new file mode 100644 index 0000000..faec2e8 --- /dev/null +++ b/APRSReader.cpp @@ -0,0 +1,32 @@ +/* + * Copyright (C) 2021-2022 by Geoffrey Merck F4FXL / KC3FRA + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + + +#include "APRSReader.h" +#include "APRSFrame.h" +#include "APRSParser.h" + +CAPRSReader::CAPRSReader() +{ + +} + +bool CAPRSReader::readAprsFrame(const std::string& aprsFrame) +{ + +} \ No newline at end of file diff --git a/APRSReader.h b/APRSReader.h new file mode 100644 index 0000000..56781ba --- /dev/null +++ b/APRSReader.h @@ -0,0 +1,33 @@ +/* + * Copyright (C) 2021-2022 by Geoffrey Merck F4FXL / KC3FRA + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +#pragma once + +#include "ReadAPRSFrameCallback.h" + +class CAPRSReader : public CReadAPRSFrameCallback +{ +public: + CAPRSReader(); + bool readAprsFrame(const std::string& aprsFrame); + +private: + +}; + + diff --git a/APRSWriterThread.cpp b/APRSWriterThread.cpp index 796054c..aa2462b 100644 --- a/APRSWriterThread.cpp +++ b/APRSWriterThread.cpp @@ -43,7 +43,7 @@ m_exit(false), m_connected(false), m_reconnectTimer(1000U), m_tries(0U), -m_APRSReadCallback(NULL), +m_APRSReadCallback(), m_filter(""), m_clientName(FULL_PRODUCT_NAME) { @@ -70,7 +70,7 @@ m_exit(false), m_connected(false), m_reconnectTimer(1000U), m_tries(0U), -m_APRSReadCallback(NULL), +m_APRSReadCallback(), m_filter(filter), m_clientName(clientName) { @@ -88,6 +88,17 @@ m_clientName(clientName) CAPRSWriterThread::~CAPRSWriterThread() { + std::vector callBacksCopy; + callBacksCopy.assign(m_APRSReadCallback.begin(), m_APRSReadCallback.end()); + + m_APRSReadCallback.clear(); + + for(auto cb : callBacksCopy) { + delete cb; + } + + callBacksCopy.clear(); + m_username.clear(); m_password.clear(); } @@ -163,9 +174,11 @@ void* CAPRSWriterThread::Entry() CLog::logDebug("Received APRS Frame : %s", line.c_str()); if(length > 0 && line[0] != '#'//check if we have something and if that something is an APRS frame - && m_APRSReadCallback != NULL)//do we have someone wanting an APRS Frame? + && m_APRSReadCallback.size() > 0U)//do we have someone wanting an APRS Frame? { - m_APRSReadCallback(std::string(line)); + for(auto cb : m_APRSReadCallback) { + cb->readAprsFrame(line); + } } } @@ -193,9 +206,10 @@ void* CAPRSWriterThread::Entry() return NULL; } -void CAPRSWriterThread::setReadAPRSCallback(ReadAPRSFrameCallback cb) +void CAPRSWriterThread::addReadAPRSCallback(CReadAPRSFrameCallback * cb) { - m_APRSReadCallback = cb; + assert(cb != nullptr); + m_APRSReadCallback.push_back(cb); } void CAPRSWriterThread::write(const char* data) diff --git a/APRSWriterThread.h b/APRSWriterThread.h index aa07af2..2e21f9e 100644 --- a/APRSWriterThread.h +++ b/APRSWriterThread.h @@ -19,12 +19,14 @@ #ifndef APRSWriterThread_H #define APRSWriterThread_H +#include + #include "TCPReaderWriterClient.h" #include "RingBuffer.h" #include "Timer.h" #include "Thread.h" +#include "ReadAPRSFrameCallback.h" -typedef void (*ReadAPRSFrameCallback)(const std::string&); class CAPRSWriterThread : public CThread { public: @@ -44,7 +46,7 @@ public: void clock(unsigned int ms); - void setReadAPRSCallback(ReadAPRSFrameCallback cb); + void addReadAPRSCallback(CReadAPRSFrameCallback* cb); private: std::string m_username; @@ -56,7 +58,7 @@ private: bool m_connected; CTimer m_reconnectTimer; unsigned int m_tries; - ReadAPRSFrameCallback m_APRSReadCallback; + std::vector m_APRSReadCallback; std::string m_filter; std::string m_clientName; diff --git a/ReadAPRSFrameCallback.h b/ReadAPRSFrameCallback.h new file mode 100644 index 0000000..1b02dc3 --- /dev/null +++ b/ReadAPRSFrameCallback.h @@ -0,0 +1,28 @@ +/* + * Copyright (C) 2021 by Geoffrey Merck F4FXL / KC3FRA + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +#pragma once + +#include + +class CReadAPRSFrameCallback +{ +public: + virtual ~CReadAPRSFrameCallback(){ } + virtual bool readAprsFrame(const std::string& aprsFrame) = 0; +};