From b24f42d00affeff8f837d4eddf3fe90677458b4f Mon Sep 17 00:00:00 2001 From: Peter Buchegger Date: Fri, 21 May 2021 22:41:30 +0200 Subject: [PATCH] remove smart pointer in APRS IS Task --- lib/APRS-IS/APRS-IS.cpp | 88 +++++++++++++++++++++++++++++++++++++++++ lib/APRS-IS/APRS-IS.h | 34 ++++++++++++++++ platformio.ini | 1 - src/TaskAprsIs.cpp | 11 +++--- src/TaskAprsIs.h | 2 +- 5 files changed, 128 insertions(+), 8 deletions(-) create mode 100644 lib/APRS-IS/APRS-IS.cpp create mode 100644 lib/APRS-IS/APRS-IS.h diff --git a/lib/APRS-IS/APRS-IS.cpp b/lib/APRS-IS/APRS-IS.cpp new file mode 100644 index 0000000..6988ac9 --- /dev/null +++ b/lib/APRS-IS/APRS-IS.cpp @@ -0,0 +1,88 @@ +#include "APRS-IS.h" +#include + +void APRS_IS::setup(const String &user, const String &passcode, const String &tool_name, const String &version) { + _user = user; + _passcode = passcode; + _tool_name = tool_name; + _version = version; +} + +bool APRS_IS::connect(const String &server, const int port) { + const String login = "user " + _user + " pass " + _passcode + " vers " + _tool_name + " " + _version + "\n\r"; + return _connect(server, port, login); +} + +bool APRS_IS::connect(const String &server, const int port, const String &filter) { + const String login = "user " + _user + " pass " + _passcode + " vers " + _tool_name + " " + _version + " filter " + filter + "\n\r"; + return _connect(server, port, login); +} + +bool APRS_IS::_connect(const String &server, const int port, const String &login_line) { + if (!_client.connect(server.c_str(), port)) { + logPrintlnE("Something went wrong on connecting! Is the server reachable?"); + return false; + } + sendMessage(login_line); + while (true) { + String line = _client.readStringUntil('\n'); + if (line.indexOf("logresp") != -1) { + if (line.indexOf("unverified") == -1) { + return true; + } else { + logPrintlnE("User can not be verified with passcode!"); + return false; + } + } + } + return true; +} + +bool APRS_IS::connected() { + return _client.connected(); +} + +bool APRS_IS::sendMessage(const String &message) { + if (!connected()) { + return false; + } + _client.println(message); + return true; +} + +bool APRS_IS::sendMessage(const std::shared_ptr message) { + if (!connected()) { + return false; + } + _client.println(message->encode() + "\n"); + return true; +} + +int APRS_IS::available() { + return _client.available(); +} + +String APRS_IS::getMessage() { + String line; + if (_client.available() > 0) { + line = _client.readStringUntil('\n'); + } + return line; +} + +std::shared_ptr APRS_IS::getAPRSMessage() { + String line; + if (_client.available() > 0) { + line = _client.readStringUntil('\n'); + } + if (line.startsWith("#")) { + logPrintlnD(line); + return 0; + } + if (line.length() == 0) { + return 0; + } + std::shared_ptr msg = std::shared_ptr(new APRSMessage()); + msg->decode(line); + return msg; +} diff --git a/lib/APRS-IS/APRS-IS.h b/lib/APRS-IS/APRS-IS.h new file mode 100644 index 0000000..00d27ca --- /dev/null +++ b/lib/APRS-IS/APRS-IS.h @@ -0,0 +1,34 @@ + +#ifndef APRS_IS_Lib_h_ +#define APRS_IS_Lib_h_ + +#include +#include + +class APRS_IS { +public: + void setup(const String &user, const String &passcode, const String &tool_name, const String &version); + + bool connect(const String &server, const int port); + bool connect(const String &server, const int port, const String &filter); + bool connected(); + + bool sendMessage(const String &message); + bool sendMessage(const std::shared_ptr message); + + int available(); + + String getMessage(); + std::shared_ptr getAPRSMessage(); + +private: + String _user; + String _passcode; + String _tool_name; + String _version; + WiFiClient _client; + + bool _connect(const String &server, const int port, const String &login_line); +}; + +#endif diff --git a/platformio.ini b/platformio.ini index f8d8be4..d553213 100644 --- a/platformio.ini +++ b/platformio.ini @@ -11,7 +11,6 @@ lib_deps = bblanchon/ArduinoJson @ 6.17.0 lewisxhe/AXP202X_Library @ 1.1.2 peterus/APRS-Decoder-Lib @ 0.0.6 - peterus/APRS-IS-Lib @ 0.0.8 peterus/esp-logger @ 0.0.1 peterus/ESP-FTP-Server-Lib @ 0.9.5 check_tool = cppcheck diff --git a/src/TaskAprsIs.cpp b/src/TaskAprsIs.cpp index 43526fe..d2fe06d 100644 --- a/src/TaskAprsIs.cpp +++ b/src/TaskAprsIs.cpp @@ -11,8 +11,7 @@ AprsIsTask::~AprsIsTask() { } bool AprsIsTask::setup(System &system) { - _aprs_is = std::shared_ptr(new APRS_IS(system.getUserConfig()->callsign, system.getUserConfig()->aprs_is.passcode, "ESP32-APRS-IS", "0.2")); - + _aprs_is.setup(system.getUserConfig()->callsign, system.getUserConfig()->aprs_is.passcode, "ESP32-APRS-IS", "0.2"); return true; } @@ -20,7 +19,7 @@ bool AprsIsTask::loop(System &system) { if (!system.isWifiEthConnected()) { return false; } - if (!_aprs_is->connected()) { + if (!_aprs_is.connected()) { if (!connect(system)) { _stateInfo = "not connected"; _state = Error; @@ -31,11 +30,11 @@ bool AprsIsTask::loop(System &system) { return false; } - _aprs_is->getAPRSMessage(); + _aprs_is.getAPRSMessage(); if (!_toAprsIs.empty()) { std::shared_ptr msg = _toAprsIs.getElement(); - _aprs_is->sendMessage(msg); + _aprs_is.sendMessage(msg); } return true; @@ -46,7 +45,7 @@ bool AprsIsTask::connect(System &system) { logPrintI(system.getUserConfig()->aprs_is.server); logPrintI(" on port: "); logPrintlnI(String(system.getUserConfig()->aprs_is.port)); - if (!_aprs_is->connect(system.getUserConfig()->aprs_is.server, system.getUserConfig()->aprs_is.port)) { + if (!_aprs_is.connect(system.getUserConfig()->aprs_is.server, system.getUserConfig()->aprs_is.port)) { logPrintlnE("Connection failed."); return false; } diff --git a/src/TaskAprsIs.h b/src/TaskAprsIs.h index 2b1955a..30679db 100644 --- a/src/TaskAprsIs.h +++ b/src/TaskAprsIs.h @@ -15,7 +15,7 @@ public: virtual bool loop(System &system) override; private: - std::shared_ptr _aprs_is; + APRS_IS _aprs_is; TaskQueue> &_toAprsIs;