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
#include "Logging.h"
ModbusClientTCPasync::ModbusClientTCPasync(IPAddress address, uint16_t port, uint16_t queueLimit) :
ModbusClient(),
txQueue(),
rxQueue(),
MTA_client(),
MTA_timeout(DEFAULTTIMEOUT),
MTA_idleTimeout(DEFAULTIDLETIME),
MTA_qLimit(queueLimit),
MTA_maxInflightRequests(queueLimit),
MTA_lastActivity(0),
MTA_state(DISCONNECTED),
MTA_host(address),
MTA_port(port)
{
ModbusClientTCPasync::ModbusClientTCPasync(IPAddress address, uint16_t port, uint16_t queueLimit)
: ModbusClient()
, txQueue()
, rxQueue()
, MTA_client()
, MTA_timeout(DEFAULTTIMEOUT)
, MTA_idleTimeout(DEFAULTIDLETIME)
, MTA_qLimit(queueLimit)
, MTA_maxInflightRequests(queueLimit)
, MTA_lastActivity(0)
, MTA_state(DISCONNECTED)
, MTA_host(address)
, MTA_port(port) {
// attach all handlers on async tcp events
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.onError([](void* i, AsyncClient* c, int8_t error) { (static_cast<ModbusClientTCPasync*>(i))->onACError(c, error); }, 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.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.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.onPoll([](void* i, AsyncClient* c) { (static_cast<ModbusClientTCPasync*>(i))->onPoll(); }, 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.onPoll([](void * i, AsyncClient * c) { (static_cast<ModbusClientTCPasync *>(i))->onPoll(); }, this);
// disable nagle algorithm ref Modbus spec
MTA_client.setNoDelay(true);
}
}
// Destructor: clean up queue, task etc.
ModbusClientTCPasync::~ModbusClientTCPasync() {
@@ -46,7 +49,7 @@ ModbusClientTCPasync::~ModbusClientTCPasync() {
delete txQueue.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;
it = rxQueue.erase(it);
}
@@ -97,8 +100,7 @@ void ModbusClientTCPasync::setMaxInflightRequests(uint32_t maxInflightRequests)
}
// Remove all pending request from queue
void ModbusClientTCPasync::clearQueue()
{
void ModbusClientTCPasync::clearQueue() {
LOCK_GUARD(lock1, qLock);
LOCK_GUARD(lock2, sLock);
// Delete all elements from queues
@@ -151,8 +153,9 @@ bool ModbusClientTCPasync::addToQueue(int32_t token, ModbusMessage request, bool
LOCK_GUARD(lock1, qLock);
if (txQueue.size() + rxQueue.size() < MTA_qLimit) {
HEXDUMP_V("Enqueue", request.data(), request.size());
RequestEntry *re = new RequestEntry(token, request, syncReq);
if (!re) return false; //TODO: proper error returning in case allocation fails
RequestEntry * re = new RequestEntry(token, request, syncReq);
if (!re)
return false; // TODO: proper error returning in case allocation fails
// inject proper transactionID
re->head.transactionID = messageCount++;
re->head.len = request.size();
@@ -190,7 +193,7 @@ void ModbusClientTCPasync::onDisconnected() {
// empty queue on disconnect, calling errorcode on every waiting request
LOCK_GUARD(lock2, qLock);
while (!txQueue.empty()) {
RequestEntry* r = txQueue.front();
RequestEntry * r = txQueue.front();
if (onError) {
onError(IP_CONNECTION_FAILED, r->token);
}
@@ -198,7 +201,7 @@ void ModbusClientTCPasync::onDisconnected() {
txQueue.pop_front();
}
while (!rxQueue.empty()) {
RequestEntry *r = rxQueue.begin()->second;
RequestEntry * r = rxQueue.begin()->second;
if (onError) {
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
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
}
*/
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);
// reset idle timeout
MTA_lastActivity = millis();
@@ -231,8 +234,8 @@ void ModbusClientTCPasync::onPacket(uint8_t* data, size_t length) {
LOG_D("parsing (len:%d)\n", length + 1);
}
while (length > 0) {
RequestEntry* request = nullptr;
ModbusMessage* response = nullptr;
RequestEntry * request = nullptr;
ModbusMessage * response = nullptr;
uint16_t transactionID = 0;
uint16_t protocolID = 0;
uint16_t messageLength = 0;
@@ -246,9 +249,7 @@ void ModbusClientTCPasync::onPacket(uint8_t* data, size_t length) {
transactionID = (data[0] << 8) | data[1];
protocolID = (data[2] << 8) | data[3];
messageLength = (data[4] << 8) | data[5];
if (protocolID == 0 &&
length >= (uint32_t)messageLength + 6 &&
messageLength < 256) {
if (protocolID == 0 && length >= (uint32_t)messageLength + 6 && messageLength < 256) {
response = new ModbusMessage(messageLength);
response->add(&data[6], 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) {
{
LOCK_GUARD(sL ,syncRespM);
LOCK_GUARD(sL, syncRespM);
syncResponse[request->token] = *response;
}
} else if (onResponse) {
@@ -335,7 +336,7 @@ void ModbusClientTCPasync::onPoll() {
// next check if timeout has struck for oldest request
if (!rxQueue.empty()) {
RequestEntry* request = rxQueue.begin()->second;
RequestEntry * request = rxQueue.begin()->second;
if (millis() - request->sentTime > MTA_timeout) {
LOG_D("request timeouts (now:%lu-sent:%u)\n", millis(), request->sentTime);
// oldest element timeouts, call onError and clean up
@@ -362,7 +363,7 @@ void ModbusClientTCPasync::handleSendingQueue() {
// by mutex.
// 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()) {
// get the actual element
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.
// Calling sites must assure shared resources are protected
// by mutex.
@@ -391,7 +392,7 @@ bool ModbusClientTCPasync::send(RequestEntry* re) {
// Write TCP header first
MTA_client.add(reinterpret_cast<const char *>((const uint8_t *)(re->head)), 6, ASYNC_WRITE_FLAG_COPY);
// 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
MTA_client.send();
LOG_D("request sent (msgid:%d)\n", re->head.transactionID);

View File

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