#12 Add formater and start using it

pull/32/head
Geoffrey Merck 4 years ago
parent c7e5b5a655
commit 0cc9f43bb8

@ -0,0 +1,54 @@
/*
* 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 <boost/algorithm/string.hpp>
#include "APRSFormater.h"
#include "Log.h"
#include "StringUtils.h"
bool CAPRSFormater::frameToString(std::string& output, CAPRSFrame& frame)
{
// make sur we have the minimal stuff to build a correct aprs string
if(frame.getSource().empty()
|| frame.getDestination().empty()
|| frame.getBody().empty()) {
CLog::logWarning("Invalid APRS frame, missing source, destination or body");
return false;
}
// std::string path(frame.getPath().size() > 0U ? "," : "");
// for(auto pathItem : frame.getPath()) {
// path.append(boost::trim_copy(pathItem)).push_back(',');
// }
// boost::trim_right_if(path, [](char c){ return c == ','; });
auto path = boost::join_if(frame.getPath(), ",", [](std::string s) { return !string_is_blank_or_empty(s); });
CStringUtils::string_format_in_place(output, "%s>%s%s%s:%s",
frame.getSource().c_str(),
frame.getDestination().c_str(),
path.empty() ? "" : ",",
path.c_str(),
frame.getBody().c_str());
return true;
}

@ -0,0 +1,27 @@
/*
* 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 "APRSFrame.h"
class CAPRSFormater
{
public:
static bool frameToString(std::string& output, CAPRSFrame& frame);
};

@ -22,15 +22,17 @@ CAPRSFrame::CAPRSFrame() :
m_source(), m_source(),
m_destination(), m_destination(),
m_path(), m_path(),
m_body(),
m_type(APFT_UNKNOWN) m_type(APFT_UNKNOWN)
{ {
} }
CAPRSFrame::CAPRSFrame(const std::string& source, const std::string& destination, const std::vector<std::string>& path, APRS_FRAME_TYPE type) : CAPRSFrame::CAPRSFrame(const std::string& source, const std::string& destination, const std::vector<std::string>& path, const std::string& body, APRS_FRAME_TYPE type) :
m_source(source), m_source(source),
m_destination(destination), m_destination(destination),
m_path(), m_path(),
m_body(body),
m_type(type) m_type(type)
{ {
m_path.assign(path.begin(), path.end()); m_path.assign(path.begin(), path.end());

@ -25,12 +25,14 @@
enum APRS_FRAME_TYPE { enum APRS_FRAME_TYPE {
APFT_UNKNOWN = 0, APFT_UNKNOWN = 0,
APFT_MESSAGE, APFT_MESSAGE,
APFT_POSITION,
APFT_NMEA,
}; };
class CAPRSFrame { class CAPRSFrame {
public: public:
CAPRSFrame(); CAPRSFrame();
CAPRSFrame(const std::string& source, const std::string& destination, const std::vector<std::string>& path, APRS_FRAME_TYPE type); CAPRSFrame(const std::string& source, const std::string& destination, const std::vector<std::string>& path, const std::string& body, APRS_FRAME_TYPE type);
void clear(); void clear();
std::string& getSource() { return m_source; } std::string& getSource() { return m_source; }

@ -20,9 +20,13 @@
#include "Log.h" #include "Log.h"
bool CAPRSParser::parseFrame(const std::string& frameStr, CAPRSFrame& frame) bool CAPRSParser::parseFrame(const std::string& frameStr, CAPRSFrame& frame)
{
return parseFrame(frameStr, frame, false);
}
bool CAPRSParser::parseFrame(const std::string& frameStr, CAPRSFrame& frame, bool doNotEnforceFrameType)
{ {
frame.clear(); frame.clear();
bool ret = false; bool ret = doNotEnforceFrameType;
if(!frameStr.empty()) { if(!frameStr.empty()) {
auto pos = frameStr.find_first_of(':'); auto pos = frameStr.find_first_of(':');
@ -44,12 +48,14 @@ bool CAPRSParser::parseFrame(const std::string& frameStr, CAPRSFrame& frame)
frame.getBody().assign(body); frame.getBody().assign(body);
setFrameType(frame); if(!doNotEnforceFrameType) {
if(frame.getType() == APFT_UNKNOWN) { setFrameType(frame);
CLog::logInfo("Invalid or unsupported APRS frame : %s", frameStr); if(frame.getType() == APFT_UNKNOWN) {
} CLog::logInfo("Invalid or unsupported APRS frame : %s", frameStr);
else { }
ret = true; else {
ret = true;
}
} }
} }
} }
@ -71,7 +77,18 @@ void CAPRSParser::setFrameType(CAPRSFrame& frame)
[](char c){ return c == ' ' || c == '-' || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9'); })) [](char c){ return c == ' ' || c == '-' || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9'); }))
type = APFT_MESSAGE; type = APFT_MESSAGE;
break; break;
// case '!':
// if(body.length() >= 2U && body[1] == '!') {
// // This is ultimeter 200 weather station
// type = APFT_UNKNOWN;
// break;
// } else {
// type = APFT_POSITION;
// }
// case '=':
// case '/':
// case '@':
// break;
default: default:
break; break;
} }

@ -27,6 +27,7 @@ class CAPRSParser
{ {
public: public:
static bool parseFrame(const std::string& frameStr, CAPRSFrame& frame); static bool parseFrame(const std::string& frameStr, CAPRSFrame& frame);
static bool parseFrame(const std::string& frameStr, CAPRSFrame& frame, bool doNotEnforceFrameType);
private: private:
static void setFrameType(CAPRSFrame& frame); static void setFrameType(CAPRSFrame& frame);

@ -21,6 +21,7 @@
#include <boost/algorithm/string.hpp> #include <boost/algorithm/string.hpp>
#include <cmath> #include <cmath>
#include <cassert> #include <cassert>
#include <algorithm>
#include "StringUtils.h" #include "StringUtils.h"
#include "Log.h" #include "Log.h"
@ -28,6 +29,9 @@
#include "DStarDefines.h" #include "DStarDefines.h"
#include "Defs.h" #include "Defs.h"
#include "Log.h" #include "Log.h"
#include "APRSFrame.h"
#include "APRSParser.h"
#include "APRSFormater.h"
CAPRSWriter::CAPRSWriter(const std::string& hostname, unsigned int port, const std::string& gateway, const std::string& password, const std::string& address) : CAPRSWriter::CAPRSWriter(const std::string& hostname, unsigned int port, const std::string& gateway, const std::string& password, const std::string& address) :
m_thread(NULL), m_thread(NULL),
@ -127,26 +131,24 @@ void CAPRSWriter::writeData(const std::string& callsign, const CAMBEData& data)
unsigned int length = collector->getData(SLOW_DATA_TYPE_GPS, buffer, 400U); unsigned int length = collector->getData(SLOW_DATA_TYPE_GPS, buffer, 400U);
std::string text((char*)buffer, length); std::string text((char*)buffer, length);
auto n = text.find(':'); CAPRSFrame frame;
if (n == std::string::npos) { if(!CAPRSParser::parseFrame(text, frame, true)) {
collector->reset(); collector->reset();
CLog::logWarning("Failed to parse DPRS Frame : %s", text.c_str());
return; return;
} }
std::string header = text.substr(0, n);
std::string body = text.substr(n + 1U);
// If we already have a q-construct, don't send it on // If we already have a q-construct, don't send it on
n = header.find('q'); if(std::any_of(frame.getPath().begin(), frame.getPath().end(), [] (std::string s) { return !s.empty() && s[0] == 'q'; })) {
if (n != std::string::npos) CLog::logWarning("DPRS Frame already has q construct, not forwarding to APRS-IS: %s", text.c_str());
return; return;
}
// Remove the trailing \r frame.getPath().push_back("qAR");
n = body.find('\r'); frame.getPath().push_back(CStringUtils::string_format("%s-%s", entry->getCallsign().c_str(), entry->getBand().c_str()));
if (n != std::string::npos)
body = body.substr(0, n); std::string output ;
CAPRSFormater::frameToString(output, frame);
std::string output = CStringUtils::string_format("%s,qAR,%s-%s:%s", header.c_str(), entry->getCallsign().c_str(), entry->getBand().c_str(), body.c_str());
char ascii[500U]; char ascii[500U];
::memset(ascii, 0x00, 500U); ::memset(ascii, 0x00, 500U);

@ -21,8 +21,11 @@
#include <memory> #include <memory>
#include <string> #include <string>
#include <stdexcept> #include <stdexcept>
#include <algorithm>
#define string_right(s,l) (s.substr(s.length() - l, l)) #define string_right(s,l) (s.substr(s.length() - l, l))
#define string_is_blank(s) (std::all_of(s.begin(), s.end(), [](char c) { return c == ' '; }))
#define string_is_blank_or_empty(s) (s.empty() || std::all_of(s.begin(), s.end(), [](char c) { return c == ' '; }))
class CStringUtils { class CStringUtils {
public: public:

@ -0,0 +1,115 @@
/*
* 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 <gtest/gtest.h>
#include "../../APRSFormater.h"
class APRSFormater_frameToString : public ::testing::Test {
};
TEST_F(APRSFormater_frameToString, EmptyFrame) {
CAPRSFrame aprsFrame("", "", { }, "", APFT_UNKNOWN);
std::string output("This should be left untouched if the test succeeds");
bool retVal = CAPRSFormater::frameToString(output, aprsFrame);
EXPECT_FALSE(retVal);
EXPECT_STREQ(output.c_str(), "This should be left untouched if the test succeeds");
}
TEST_F(APRSFormater_frameToString, SourceOnly) {
CAPRSFrame aprsFrame("N0CALL", "", { }, "", APFT_UNKNOWN);
std::string output("This should be left untouched if the test succeeds");
bool retVal = CAPRSFormater::frameToString(output, aprsFrame);
EXPECT_FALSE(retVal);
EXPECT_STREQ(output.c_str(), "This should be left untouched if the test succeeds");
}
TEST_F(APRSFormater_frameToString, DestinationOnly) {
CAPRSFrame aprsFrame("", "APRS", { }, "", APFT_UNKNOWN);
std::string output("This should be left untouched if the test succeeds");
bool retVal = CAPRSFormater::frameToString(output, aprsFrame);
EXPECT_FALSE(retVal);
EXPECT_STREQ(output.c_str(), "This should be left untouched if the test succeeds");
}
TEST_F(APRSFormater_frameToString, PathOnly) {
CAPRSFrame aprsFrame("", "", { "WIDE1-1, WIDE2-1" }, "", APFT_UNKNOWN);
std::string output("This should be left untouched if the test succeeds");
bool retVal = CAPRSFormater::frameToString(output, aprsFrame);
EXPECT_FALSE(retVal);
EXPECT_STREQ(output.c_str(), "This should be left untouched if the test succeeds");
}
TEST_F(APRSFormater_frameToString, BodyOnly) {
CAPRSFrame aprsFrame("", "", { }, "Lorem Ipsum", APFT_UNKNOWN);
std::string output("This should be left untouched if the test succeeds");
bool retVal = CAPRSFormater::frameToString(output, aprsFrame);
EXPECT_FALSE(retVal);
EXPECT_STREQ(output.c_str(), "This should be left untouched if the test succeeds");
}
TEST_F(APRSFormater_frameToString, CorrectWithoutPath) {
CAPRSFrame aprsFrame("N0CALL", "APRS", { }, "Lorem Ipsum", APFT_UNKNOWN);
std::string output("This should NOT be left untouched if the test succeeds");
bool retVal = CAPRSFormater::frameToString(output, aprsFrame);
EXPECT_TRUE(retVal);
EXPECT_STREQ(output.c_str(), "N0CALL>APRS:Lorem Ipsum");
}
TEST_F(APRSFormater_frameToString, CorrectWithPath) {
CAPRSFrame aprsFrame("N0CALL", "APRS", { "WIDE1-1", "WIDE2-2" }, "Lorem Ipsum", APFT_UNKNOWN);
std::string output("This should be left untouched if the test succeeds");
bool retVal = CAPRSFormater::frameToString(output, aprsFrame);
EXPECT_TRUE(retVal);
EXPECT_STREQ(output.c_str(), "N0CALL>APRS,WIDE1-1,WIDE2-2:Lorem Ipsum");
}
TEST_F(APRSFormater_frameToString, WithSomeEmptyPath) {
CAPRSFrame aprsFrame("N0CALL", "APRS", { "WIDE1-1", "", "WIDE2-2" }, "Lorem Ipsum", APFT_UNKNOWN);
std::string output("This should be left untouched if the test succeeds");
bool retVal = CAPRSFormater::frameToString(output, aprsFrame);
EXPECT_TRUE(retVal);
EXPECT_STREQ(output.c_str(), "N0CALL>APRS,WIDE1-1,WIDE2-2:Lorem Ipsum");
}
TEST_F(APRSFormater_frameToString, WithSomeBlankPath) {
CAPRSFrame aprsFrame("N0CALL", "APRS", { "WIDE1-1", "", "WIDE2-2" }, "Lorem Ipsum", APFT_UNKNOWN);
std::string output("This should be left untouched if the test succeeds");
bool retVal = CAPRSFormater::frameToString(output, aprsFrame);
EXPECT_TRUE(retVal);
EXPECT_STREQ(output.c_str(), "N0CALL>APRS,WIDE1-1,WIDE2-2:Lorem Ipsum");
}

@ -91,7 +91,7 @@ TEST_F(APRSParser_parseAPRSFrame, CorrectMessageFrameWithoutDigipeater) {
EXPECT_EQ(aprsFrame.getPath().size(), 0); EXPECT_EQ(aprsFrame.getPath().size(), 0);
} }
TEST_F(APRSParser_parseAPRSFrame, InvalideMessageFrame) { TEST_F(APRSParser_parseAPRSFrame, InvalidMessageFrame) {
CAPRSFrame aprsFrame; CAPRSFrame aprsFrame;
bool retVal = CAPRSParser::parseFrame("N0CALL>APRS::F4ABC&@#$:Test Message", aprsFrame); bool retVal = CAPRSParser::parseFrame("N0CALL>APRS::F4ABC&@#$:Test Message", aprsFrame);
@ -102,4 +102,35 @@ TEST_F(APRSParser_parseAPRSFrame, InvalideMessageFrame) {
EXPECT_STRCASEEQ(aprsFrame.getSource().c_str(), ""); EXPECT_STRCASEEQ(aprsFrame.getSource().c_str(), "");
EXPECT_EQ(aprsFrame.getType(), APFT_UNKNOWN); EXPECT_EQ(aprsFrame.getType(), APFT_UNKNOWN);
EXPECT_EQ(aprsFrame.getPath().size(), 0U); EXPECT_EQ(aprsFrame.getPath().size(), 0U);
}
TEST_F(APRSParser_parseAPRSFrame, ValidFrameDoNotEnforceType) {
CAPRSFrame aprsFrame;
bool retVal = CAPRSParser::parseFrame("N0CALL>APRS,WIDE1-1,WIDE2-2:Lorem Ipsum", aprsFrame, true);
EXPECT_TRUE(retVal);
EXPECT_STRCASEEQ(aprsFrame.getBody().c_str(), "Lorem Ipsum");
EXPECT_STRCASEEQ(aprsFrame.getDestination().c_str(), "APRS");
EXPECT_STRCASEEQ(aprsFrame.getSource().c_str(), "N0CALL");
EXPECT_EQ(aprsFrame.getType(), APFT_UNKNOWN);
EXPECT_EQ(aprsFrame.getPath().size(), 2);
EXPECT_STREQ(aprsFrame.getPath()[0].c_str(), "WIDE1-1");
EXPECT_STREQ(aprsFrame.getPath()[1].c_str(), "WIDE2-2");
}
//
TEST_F(APRSParser_parseAPRSFrame, ID51) {
CAPRSFrame aprsFrame;
bool retVal = CAPRSParser::parseFrame("F4FXL-8>API51,DSTAR:!1234.51N/12345.42E[/A=000886QRV DStar\r\r\n", aprsFrame, true);
EXPECT_TRUE(retVal);
EXPECT_STRCASEEQ(aprsFrame.getBody().c_str(), "!4849.51N/00736.42E[/A=000886QRV DStar\r\r\n");
EXPECT_STRCASEEQ(aprsFrame.getDestination().c_str(), "API51");
EXPECT_STRCASEEQ(aprsFrame.getSource().c_str(), "F4FXL-8");
EXPECT_EQ(aprsFrame.getType(), APFT_UNKNOWN);
EXPECT_EQ(aprsFrame.getPath().size(), 1);
EXPECT_STREQ(aprsFrame.getPath()[0].c_str(), "DSTAR");
} }
Loading…
Cancel
Save

Powered by TurnKey Linux.