tidy up TODOs

This commit is contained in:
proddy
2024-09-09 17:46:13 +02:00
parent 5fae9872e6
commit 54d8c5ad8f
2 changed files with 294 additions and 294 deletions

View File

@@ -7,32 +7,35 @@
// #undef LOCAL_LOG_LEVEL // #undef LOCAL_LOG_LEVEL
#include "Logging.h" #include "Logging.h"
ModbusClientTCPasync::ModbusClientTCPasync(IPAddress address, uint16_t port, uint16_t queueLimit) : ModbusClientTCPasync::ModbusClientTCPasync(IPAddress address, uint16_t port, uint16_t queueLimit)
ModbusClient(), : ModbusClient()
txQueue(), , txQueue()
rxQueue(), , rxQueue()
MTA_client(), , MTA_client()
MTA_timeout(DEFAULTTIMEOUT), , MTA_timeout(DEFAULTTIMEOUT)
MTA_idleTimeout(DEFAULTIDLETIME), , MTA_idleTimeout(DEFAULTIDLETIME)
MTA_qLimit(queueLimit), , MTA_qLimit(queueLimit)
MTA_maxInflightRequests(queueLimit), , MTA_maxInflightRequests(queueLimit)
MTA_lastActivity(0), , MTA_lastActivity(0)
MTA_state(DISCONNECTED), , MTA_state(DISCONNECTED)
MTA_host(address), , MTA_host(address)
MTA_port(port) , MTA_port(port) {
{
// attach all handlers on async tcp events // attach all handlers on async tcp events
MTA_client.onConnect([](void* i, AsyncClient* c) { (static_cast<ModbusClientTCPasync*>(i))->onConnected(); }, this); MTA_client.onConnect([](void * i, AsyncClient * c) { (static_cast<ModbusClientTCPasync *>(i))->onConnected(); }, this);
MTA_client.onDisconnect([](void* i, AsyncClient* c) { (static_cast<ModbusClientTCPasync*>(i))->onDisconnected(); }, this); MTA_client.onDisconnect([](void * i, AsyncClient * c) { (static_cast<ModbusClientTCPasync *>(i))->onDisconnected(); }, this);
MTA_client.onError([](void* i, AsyncClient* c, int8_t error) { (static_cast<ModbusClientTCPasync*>(i))->onACError(c, error); }, this); MTA_client.onError([](void * i, AsyncClient * c, int8_t error) { (static_cast<ModbusClientTCPasync *>(i))->onACError(c, error); }, this);
// MTA_client.onTimeout([](void* i, AsyncClient* c, uint32_t time) { (static_cast<ModbusClientTCPasync*>(i))->onTimeout(time); }, this); // MTA_client.onTimeout([](void* i, AsyncClient* c, uint32_t time) { (static_cast<ModbusClientTCPasync*>(i))->onTimeout(time); }, this);
// MTA_client.onAck([](void* i, AsyncClient* c, size_t len, uint32_t time) { (static_cast<ModbusClientTCPasync*>(i))->onAck(len, time); }, this); // MTA_client.onAck([](void* i, AsyncClient* c, size_t len, uint32_t time) { (static_cast<ModbusClientTCPasync*>(i))->onAck(len, time); }, this);
MTA_client.onData([](void* i, AsyncClient* c, void* data, size_t len) { (static_cast<ModbusClientTCPasync*>(i))->onPacket(static_cast<uint8_t*>(data), len); }, this); MTA_client.onData([](void * i,
MTA_client.onPoll([](void* i, AsyncClient* c) { (static_cast<ModbusClientTCPasync*>(i))->onPoll(); }, this); AsyncClient * c,
void * data,
size_t len) { (static_cast<ModbusClientTCPasync *>(i))->onPacket(static_cast<uint8_t *>(data), len); },
this);
MTA_client.onPoll([](void * i, AsyncClient * c) { (static_cast<ModbusClientTCPasync *>(i))->onPoll(); }, this);
// disable nagle algorithm ref Modbus spec // disable nagle algorithm ref Modbus spec
MTA_client.setNoDelay(true); MTA_client.setNoDelay(true);
} }
// Destructor: clean up queue, task etc. // Destructor: clean up queue, task etc.
ModbusClientTCPasync::~ModbusClientTCPasync() { ModbusClientTCPasync::~ModbusClientTCPasync() {
@@ -46,7 +49,7 @@ ModbusClientTCPasync::~ModbusClientTCPasync() {
delete txQueue.front(); delete txQueue.front();
txQueue.pop_front(); txQueue.pop_front();
} }
for (auto it = rxQueue.cbegin(); it != rxQueue.cend();/* no increment */) { for (auto it = rxQueue.cbegin(); it != rxQueue.cend(); /* no increment */) {
delete it->second; delete it->second;
it = rxQueue.erase(it); it = rxQueue.erase(it);
} }
@@ -97,8 +100,7 @@ void ModbusClientTCPasync::setMaxInflightRequests(uint32_t maxInflightRequests)
} }
// Remove all pending request from queue // Remove all pending request from queue
void ModbusClientTCPasync::clearQueue() void ModbusClientTCPasync::clearQueue() {
{
LOCK_GUARD(lock1, qLock); LOCK_GUARD(lock1, qLock);
LOCK_GUARD(lock2, sLock); LOCK_GUARD(lock2, sLock);
// Delete all elements from queues // Delete all elements from queues
@@ -151,8 +153,9 @@ bool ModbusClientTCPasync::addToQueue(int32_t token, ModbusMessage request, bool
LOCK_GUARD(lock1, qLock); LOCK_GUARD(lock1, qLock);
if (txQueue.size() + rxQueue.size() < MTA_qLimit) { if (txQueue.size() + rxQueue.size() < MTA_qLimit) {
HEXDUMP_V("Enqueue", request.data(), request.size()); HEXDUMP_V("Enqueue", request.data(), request.size());
RequestEntry *re = new RequestEntry(token, request, syncReq); RequestEntry * re = new RequestEntry(token, request, syncReq);
if (!re) return false; //TODO: proper error returning in case allocation fails if (!re)
return false; // TODO: proper error returning in case allocation fails
// inject proper transactionID // inject proper transactionID
re->head.transactionID = messageCount++; re->head.transactionID = messageCount++;
re->head.len = request.size(); re->head.len = request.size();
@@ -190,7 +193,7 @@ void ModbusClientTCPasync::onDisconnected() {
// empty queue on disconnect, calling errorcode on every waiting request // empty queue on disconnect, calling errorcode on every waiting request
LOCK_GUARD(lock2, qLock); LOCK_GUARD(lock2, qLock);
while (!txQueue.empty()) { while (!txQueue.empty()) {
RequestEntry* r = txQueue.front(); RequestEntry * r = txQueue.front();
if (onError) { if (onError) {
onError(IP_CONNECTION_FAILED, r->token); onError(IP_CONNECTION_FAILED, r->token);
} }
@@ -198,7 +201,7 @@ void ModbusClientTCPasync::onDisconnected() {
txQueue.pop_front(); txQueue.pop_front();
} }
while (!rxQueue.empty()) { while (!rxQueue.empty()) {
RequestEntry *r = rxQueue.begin()->second; RequestEntry * r = rxQueue.begin()->second;
if (onError) { if (onError) {
onError(IP_CONNECTION_FAILED, r->token); onError(IP_CONNECTION_FAILED, r->token);
} }
@@ -208,7 +211,7 @@ void ModbusClientTCPasync::onDisconnected() {
} }
void ModbusClientTCPasync::onACError(AsyncClient* c, int8_t error) { void ModbusClientTCPasync::onACError(AsyncClient * c, int8_t error) {
// onDisconnect will alse be called, so nothing to do here // onDisconnect will alse be called, so nothing to do here
LOG_W("TCP error: %s\n", c->errorToString(error)); LOG_W("TCP error: %s\n", c->errorToString(error));
} }
@@ -222,7 +225,7 @@ void onAck(size_t len, uint32_t time) {
// assuming we don't need this // assuming we don't need this
} }
*/ */
void ModbusClientTCPasync::onPacket(uint8_t* data, size_t length) { void ModbusClientTCPasync::onPacket(uint8_t * data, size_t length) {
LOG_D("packet received (len:%d)\n", length); LOG_D("packet received (len:%d)\n", length);
// reset idle timeout // reset idle timeout
MTA_lastActivity = millis(); MTA_lastActivity = millis();
@@ -231,8 +234,8 @@ void ModbusClientTCPasync::onPacket(uint8_t* data, size_t length) {
LOG_D("parsing (len:%d)\n", length + 1); LOG_D("parsing (len:%d)\n", length + 1);
} }
while (length > 0) { while (length > 0) {
RequestEntry* request = nullptr; RequestEntry * request = nullptr;
ModbusMessage* response = nullptr; ModbusMessage * response = nullptr;
uint16_t transactionID = 0; uint16_t transactionID = 0;
uint16_t protocolID = 0; uint16_t protocolID = 0;
uint16_t messageLength = 0; uint16_t messageLength = 0;
@@ -246,9 +249,7 @@ void ModbusClientTCPasync::onPacket(uint8_t* data, size_t length) {
transactionID = (data[0] << 8) | data[1]; transactionID = (data[0] << 8) | data[1];
protocolID = (data[2] << 8) | data[3]; protocolID = (data[2] << 8) | data[3];
messageLength = (data[4] << 8) | data[5]; messageLength = (data[4] << 8) | data[5];
if (protocolID == 0 && if (protocolID == 0 && length >= (uint32_t)messageLength + 6 && messageLength < 256) {
length >= (uint32_t)messageLength + 6 &&
messageLength < 256) {
response = new ModbusMessage(messageLength); response = new ModbusMessage(messageLength);
response->add(&data[6], messageLength); response->add(&data[6], messageLength);
LOG_D("packet validated (len:%d)\n", messageLength); LOG_D("packet validated (len:%d)\n", messageLength);
@@ -299,7 +300,7 @@ void ModbusClientTCPasync::onPacket(uint8_t* data, size_t length) {
if (request->isSyncRequest) { if (request->isSyncRequest) {
{ {
LOCK_GUARD(sL ,syncRespM); LOCK_GUARD(sL, syncRespM);
syncResponse[request->token] = *response; syncResponse[request->token] = *response;
} }
} else if (onResponse) { } else if (onResponse) {
@@ -335,7 +336,7 @@ void ModbusClientTCPasync::onPoll() {
// next check if timeout has struck for oldest request // next check if timeout has struck for oldest request
if (!rxQueue.empty()) { if (!rxQueue.empty()) {
RequestEntry* request = rxQueue.begin()->second; RequestEntry * request = rxQueue.begin()->second;
if (millis() - request->sentTime > MTA_timeout) { if (millis() - request->sentTime > MTA_timeout) {
LOG_D("request timeouts (now:%lu-sent:%u)\n", millis(), request->sentTime); LOG_D("request timeouts (now:%lu-sent:%u)\n", millis(), request->sentTime);
// oldest element timeouts, call onError and clean up // oldest element timeouts, call onError and clean up
@@ -362,7 +363,7 @@ void ModbusClientTCPasync::handleSendingQueue() {
// by mutex. // by mutex.
// try to send everything we have waiting // try to send everything we have waiting
std::list<RequestEntry*>::iterator it = txQueue.begin(); std::list<RequestEntry *>::iterator it = txQueue.begin();
while (it != txQueue.end()) { while (it != txQueue.end()) {
// get the actual element // get the actual element
if (send(*it)) { if (send(*it)) {
@@ -377,7 +378,7 @@ void ModbusClientTCPasync::handleSendingQueue() {
} }
} }
bool ModbusClientTCPasync::send(RequestEntry* re) { bool ModbusClientTCPasync::send(RequestEntry * re) {
// ATTENTION: This method does not have a lock guard. // ATTENTION: This method does not have a lock guard.
// Calling sites must assure shared resources are protected // Calling sites must assure shared resources are protected
// by mutex. // by mutex.
@@ -391,7 +392,7 @@ bool ModbusClientTCPasync::send(RequestEntry* re) {
// Write TCP header first // Write TCP header first
MTA_client.add(reinterpret_cast<const char *>((const uint8_t *)(re->head)), 6, ASYNC_WRITE_FLAG_COPY); MTA_client.add(reinterpret_cast<const char *>((const uint8_t *)(re->head)), 6, ASYNC_WRITE_FLAG_COPY);
// Request comes next // Request comes next
MTA_client.add(reinterpret_cast<const char*>(re->msg.data()), re->msg.size(), ASYNC_WRITE_FLAG_COPY); MTA_client.add(reinterpret_cast<const char *>(re->msg.data()), re->msg.size(), ASYNC_WRITE_FLAG_COPY);
// done // done
MTA_client.send(); MTA_client.send();
LOG_D("request sent (msgid:%d)\n", re->head.transactionID); LOG_D("request sent (msgid:%d)\n", re->head.transactionID);

View File

@@ -1,6 +1,5 @@
/** /**
* TODO: * TODO: verwendete libs in readme hinzufügen
* - verwendete libs in readme hinzufügen
*/ */
#include "modbus.h" #include "modbus.h"
#include "modbus_entity_parameters.hpp" #include "modbus_entity_parameters.hpp"