remove smart pointer in APRS IS Task

pull/90/head
Peter Buchegger 2021-05-21 22:41:30 +02:00
rodzic d96e6cebc1
commit b24f42d00a
5 zmienionych plików z 128 dodań i 8 usunięć

Wyświetl plik

@ -0,0 +1,88 @@
#include "APRS-IS.h"
#include <logger.h>
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<APRSMessage> 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<APRSMessage> 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<APRSMessage> msg = std::shared_ptr<APRSMessage>(new APRSMessage());
msg->decode(line);
return msg;
}

Wyświetl plik

@ -0,0 +1,34 @@
#ifndef APRS_IS_Lib_h_
#define APRS_IS_Lib_h_
#include <APRS-Decoder.h>
#include <WiFi.h>
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<APRSMessage> message);
int available();
String getMessage();
std::shared_ptr<APRSMessage> 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

Wyświetl plik

@ -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

Wyświetl plik

@ -11,8 +11,7 @@ AprsIsTask::~AprsIsTask() {
}
bool AprsIsTask::setup(System &system) {
_aprs_is = std::shared_ptr<APRS_IS>(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<APRSMessage> 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;
}

Wyświetl plik

@ -15,7 +15,7 @@ public:
virtual bool loop(System &system) override;
private:
std::shared_ptr<APRS_IS> _aprs_is;
APRS_IS _aprs_is;
TaskQueue<std::shared_ptr<APRSMessage>> &_toAprsIs;