Skip to content

Commit

Permalink
modbus-tcp-client: Adapt to latest tfmodbustcp
Browse files Browse the repository at this point in the history
  • Loading branch information
photron committed Oct 22, 2024
1 parent 7e760f1 commit 41c6f45
Show file tree
Hide file tree
Showing 10 changed files with 232 additions and 176 deletions.
2 changes: 1 addition & 1 deletion software/energy_manager.ini
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ extends = env:base_esp32_ethernet

lib_deps = ${env:base_esp32_ethernet.lib_deps}
https://github.com/Tinkerforge/WireGuard-ESP32-Arduino#83985ec
https://github.com/Tinkerforge/tfmodbustcp#bad437c
https://github.com/Tinkerforge/tfmodbustcp#7e59917

build_flags = ${env:base_esp32_ethernet.build_flags}
-DMETER_VALUE_HISTORY_VALUE_TYPE=int32_t
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ void GenericModbusTCPClient::read_next()
esp_system_abort("generic_modbus_tcp_client: Unsupported register type to read.");
}

static_cast<TFModbusTCPClient *>(connected_client)->read(data_type, device_address, read_start_address, read_count, target_buffer, 2000000,
static_cast<TFModbusTCPSharedClient *>(connected_client)->read(data_type, device_address, read_start_address, read_count, target_buffer, 2000000,
[this](TFModbusTCPClientTransactionResult result) {
if (result != TFModbusTCPClientTransactionResult::Success) {
logger.printfln("Modbus read failed: %s (%d) client=%p host_name='%s' port=%u device_address=%u start_address=%u register_count=%u",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,146 +17,16 @@
* Boston, MA 02111-1307, USA.
*/

#define EVENT_LOG_PREFIX "modbus_tcp_clnt"

#include "generic_tcp_client_connector.h"

#include "event_log_prefix.h"
#include "module_dependencies.h"

#include "gcc_warnings.h"

#if defined(__GNUC__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wsuggest-final-methods"
#endif

void GenericTCPClientConnector::start_connection()
{
keep_connected = true;

connect_internal();
}

void GenericTCPClientConnector::stop_connection()
{
keep_connected = false;

disconnect_internal();
}

void GenericTCPClientConnector::force_reconnect()
{
stop_connection();
start_connection();
}

void GenericTCPClientConnector::connect_callback_common(TFGenericTCPClientConnectResult result, int error_number)
{
if (result == TFGenericTCPClientConnectResult::Connected) {
logger.printfln_prefixed(event_log_prefix_override, event_log_prefix_override_len,
"Connected to %s:%u", host_name.c_str(), port);

connect_backoff = 1_s;
last_connect_result = TFGenericTCPClientConnectResult::Connected;
last_connect_error_number = 0;
resolve_error_printed = false;

connect_callback();
}
else {
if (last_connect_result != result || last_connect_error_number != error_number) {
if (result == TFGenericTCPClientConnectResult::ResolveFailed) {
if (error_number == EINVAL) {
logger.printfln_prefixed(event_log_prefix_override, event_log_prefix_override_len,
"Could not resolve hostname %s, no DNS server available", host_name.c_str());
}
else if (error_number >= 0) {
logger.printfln_prefixed(event_log_prefix_override, event_log_prefix_override_len,
"Could not resolve hostname %s: %s (%d)",
host_name.c_str(),
strerror(error_number), error_number);
}
else {
logger.printfln_prefixed(event_log_prefix_override, event_log_prefix_override_len,
"Could not resolve hostname %s",
host_name.c_str());
}
}
else if (error_number >= 0) {
logger.printfln_prefixed(event_log_prefix_override, event_log_prefix_override_len,
"Could not connect to %s:%u: %s / %s (%d)",
host_name.c_str(), port,
get_tf_generic_tcp_client_connect_result_name(result),
strerror(error_number), error_number);
}
else {
logger.printfln_prefixed(event_log_prefix_override, event_log_prefix_override_len,
"Could not connect to %s:%u: %s",
host_name.c_str(), port,
get_tf_generic_tcp_client_connect_result_name(result));
}
}

if (result == TFGenericTCPClientConnectResult::ResolveFailed) {
task_scheduler.scheduleOnce([this]() {
if (keep_connected) {
connect_internal();
}
}, 10_s);
}
else {
task_scheduler.scheduleOnce([this]() {
if (keep_connected) {
connect_internal();
}
}, connect_backoff);

connect_backoff += connect_backoff;

if (connect_backoff > 16_s) {
connect_backoff = 16_s;
}
}
}

last_connect_result = result;
last_connect_error_number = error_number;
}

void GenericTCPClientConnector::disconnect_callback_common(TFGenericTCPClientDisconnectReason reason, int error_number)
{
if (reason == TFGenericTCPClientDisconnectReason::Requested) {
logger.printfln_prefixed(event_log_prefix_override, event_log_prefix_override_len,
"Disconnected from %s:%u", host_name.c_str(), port);
}
else if (error_number >= 0) {
logger.printfln_prefixed(event_log_prefix_override, event_log_prefix_override_len,
"Disconnected from %s:%u: %s / %s (%d)",
host_name.c_str(), port,
get_tf_generic_tcp_client_disconnect_reason_name(reason),
strerror(error_number), error_number);
}
else {
logger.printfln_prefixed(event_log_prefix_override, event_log_prefix_override_len,
"Disconnected from %s:%u: %s",
host_name.c_str(), port,
get_tf_generic_tcp_client_disconnect_reason_name(reason));
}

disconnect_callback();

if (keep_connected) {
connect_internal();
}
}

void GenericTCPClientConnector::connect_internal()
{
client->connect(host_name.c_str(), port,
[this](TFGenericTCPClientConnectResult result, int error_number) {
if (result == TFGenericTCPClientConnectResult::Connected) {
connected_client = client;
connected_client = shared_client;
}

connect_callback_common(result, error_number);
Expand All @@ -171,6 +41,7 @@ void GenericTCPClientConnector::connect_internal()
void GenericTCPClientConnector::disconnect_internal()
{
if (connected_client != nullptr) {
connected_client->disconnect();
client->disconnect();
connected_client = nullptr;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,40 +19,18 @@

#pragma once

#include <Arduino.h>
#include <TFGenericTCPClient.h>
#include "tools.h"
#include "generic_tcp_client_connector_base.h"

class GenericTCPClientConnector
class GenericTCPClientConnector : protected GenericTCPClientConnectorBase
{
protected:
GenericTCPClientConnector(const char *event_log_prefix_override_, TFGenericTCPClient *client_) :
event_log_prefix_override(event_log_prefix_override_), event_log_prefix_override_len(strlen(event_log_prefix_override_)), client(client_) {}
virtual ~GenericTCPClientConnector() = default;

void start_connection();
void stop_connection();
void force_reconnect();

void connect_callback_common(TFGenericTCPClientConnectResult result, int error_number);
void disconnect_callback_common(TFGenericTCPClientDisconnectReason reason, int error_number);
GenericTCPClientConnector(const char *event_log_prefix_override_, TFGenericTCPClient *client_, TFGenericTCPSharedClient *shared_client_) :
GenericTCPClientConnectorBase(event_log_prefix_override_), client(client_), shared_client(shared_client_) {}

private:
virtual void connect_internal();
virtual void disconnect_internal();
virtual void connect_callback() = 0;
virtual void disconnect_callback() = 0;

String host_name;
uint16_t port = 0;
TFGenericTCPClient *connected_client = nullptr;

private:
const char *event_log_prefix_override;
size_t event_log_prefix_override_len;
TFGenericTCPClient *client;
bool keep_connected = false;
millis_t connect_backoff = 1_s;
TFGenericTCPClientConnectResult last_connect_result = TFGenericTCPClientConnectResult::Connected;
int last_connect_error_number = 0;
bool resolve_error_printed = false;
TFGenericTCPSharedClient *shared_client;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
/* esp32-firmware
* Copyright (C) 2024 Matthias Bolte <[email protected]>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/

#define EVENT_LOG_PREFIX "modbus_tcp_clnt"

#include "generic_tcp_client_connector_base.h"

#include "event_log_prefix.h"
#include "module_dependencies.h"

#include "gcc_warnings.h"

#if defined(__GNUC__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wsuggest-final-methods"
#endif

void GenericTCPClientConnectorBase::start_connection()
{
keep_connected = true;

connect_internal();
}

void GenericTCPClientConnectorBase::stop_connection()
{
keep_connected = false;

disconnect_internal();
}

void GenericTCPClientConnectorBase::force_reconnect()
{
stop_connection();
start_connection();
}

void GenericTCPClientConnectorBase::connect_callback_common(TFGenericTCPClientConnectResult result, int error_number)
{
if (result == TFGenericTCPClientConnectResult::Connected) {
logger.printfln_prefixed(event_log_prefix_override, event_log_prefix_override_len,
"Connected to %s:%u", host_name.c_str(), port);

connect_backoff = 1_s;
last_connect_result = TFGenericTCPClientConnectResult::Connected;
last_connect_error_number = 0;
resolve_error_printed = false;

connect_callback();
}
else {
if (last_connect_result != result || last_connect_error_number != error_number) {
if (result == TFGenericTCPClientConnectResult::ResolveFailed) {
if (error_number == EINVAL) {
logger.printfln_prefixed(event_log_prefix_override, event_log_prefix_override_len,
"Could not resolve hostname %s, no DNS server available", host_name.c_str());
}
else if (error_number >= 0) {
logger.printfln_prefixed(event_log_prefix_override, event_log_prefix_override_len,
"Could not resolve hostname %s: %s (%d)",
host_name.c_str(),
strerror(error_number), error_number);
}
else {
logger.printfln_prefixed(event_log_prefix_override, event_log_prefix_override_len,
"Could not resolve hostname %s",
host_name.c_str());
}
}
else if (error_number >= 0) {
logger.printfln_prefixed(event_log_prefix_override, event_log_prefix_override_len,
"Could not connect to %s:%u: %s / %s (%d)",
host_name.c_str(), port,
get_tf_generic_tcp_client_connect_result_name(result),
strerror(error_number), error_number);
}
else {
logger.printfln_prefixed(event_log_prefix_override, event_log_prefix_override_len,
"Could not connect to %s:%u: %s",
host_name.c_str(), port,
get_tf_generic_tcp_client_connect_result_name(result));
}
}

if (result == TFGenericTCPClientConnectResult::ResolveFailed) {
task_scheduler.scheduleOnce([this]() {
if (keep_connected) {
connect_internal();
}
}, 10_s);
}
else {
task_scheduler.scheduleOnce([this]() {
if (keep_connected) {
connect_internal();
}
}, connect_backoff);

connect_backoff += connect_backoff;

if (connect_backoff > 16_s) {
connect_backoff = 16_s;
}
}
}

last_connect_result = result;
last_connect_error_number = error_number;
}

void GenericTCPClientConnectorBase::disconnect_callback_common(TFGenericTCPClientDisconnectReason reason, int error_number)
{
if (reason == TFGenericTCPClientDisconnectReason::Requested) {
logger.printfln_prefixed(event_log_prefix_override, event_log_prefix_override_len,
"Disconnected from %s:%u", host_name.c_str(), port);
}
else if (error_number >= 0) {
logger.printfln_prefixed(event_log_prefix_override, event_log_prefix_override_len,
"Disconnected from %s:%u: %s / %s (%d)",
host_name.c_str(), port,
get_tf_generic_tcp_client_disconnect_reason_name(reason),
strerror(error_number), error_number);
}
else {
logger.printfln_prefixed(event_log_prefix_override, event_log_prefix_override_len,
"Disconnected from %s:%u: %s",
host_name.c_str(), port,
get_tf_generic_tcp_client_disconnect_reason_name(reason));
}

disconnect_callback();

if (keep_connected) {
connect_internal();
}
}
Loading

0 comments on commit 41c6f45

Please sign in to comment.