refactor NetworkDevice interface to work with new esp mqtt lib

This commit is contained in:
technyon
2023-01-28 17:17:46 +01:00
parent a6010a7f49
commit 5853b0dc0e
24 changed files with 493 additions and 287 deletions

View File

@@ -13,61 +13,61 @@ the LICENSE file.
namespace espMqttClientInternals {
ClientSecureSync::ClientSecureSync(WiFiClientSecure* wiFiClient)
: client(wiFiClient) {
ClientSecureSync::ClientSecureSync()
: client() {
// empty
}
bool ClientSecureSync::connect(IPAddress ip, uint16_t port) {
bool ret = client->connect(ip, port); // implicit conversion of return code int --> bool
bool ret = client.connect(ip, port); // implicit conversion of return code int --> bool
if (ret) {
#if defined(ARDUINO_ARCH_ESP8266)
client->setNoDelay(true);
client.setNoDelay(true);
#elif defined(ARDUINO_ARCH_ESP32)
// Set TCP option directly to bypass lack of working setNoDelay for WiFiClientSecure
int val = true;
client->setSocketOption(IPPROTO_TCP, TCP_NODELAY, &val, sizeof(int));
client.setSocketOption(IPPROTO_TCP, TCP_NODELAY, &val, sizeof(int));
#endif
}
return ret;
}
bool ClientSecureSync::connect(const char* host, uint16_t port) {
bool ret = client->connect(host, port); // implicit conversion of return code int --> bool
bool ret = client.connect(host, port); // implicit conversion of return code int --> bool
if (ret) {
#if defined(ARDUINO_ARCH_ESP8266)
client->setNoDelay(true);
client.setNoDelay(true);
#elif defined(ARDUINO_ARCH_ESP32)
// Set TCP option directly to bypass lack of working setNoDelay for WiFiClientSecure
int val = true;
client->setSocketOption(IPPROTO_TCP, TCP_NODELAY, &val, sizeof(int));
client.setSocketOption(IPPROTO_TCP, TCP_NODELAY, &val, sizeof(int));
#endif
}
return ret;
}
size_t ClientSecureSync::write(const uint8_t* buf, size_t size) {
return client->write(buf, size);
return client.write(buf, size);
}
int ClientSecureSync::available() {
return client->available();
return client.available();
}
int ClientSecureSync::read(uint8_t* buf, size_t size) {
return client->read(buf, size);
return client.read(buf, size);
}
void ClientSecureSync::stop() {
client->stop();
client.stop();
}
bool ClientSecureSync::connected() {
return client->connected();
return client.connected();
}
bool ClientSecureSync::disconnected() {
return !client->connected();
return !client.connected();
}
} // namespace espMqttClientInternals

View File

@@ -18,7 +18,7 @@ namespace espMqttClientInternals {
class ClientSecureSync : public Transport {
public:
ClientSecureSync(WiFiClientSecure* wiFiClient);
ClientSecureSync();
bool connect(IPAddress ip, uint16_t port) override;
bool connect(const char* host, uint16_t port) override;
size_t write(const uint8_t* buf, size_t size) override;
@@ -27,7 +27,7 @@ class ClientSecureSync : public Transport {
void stop() override;
bool connected() override;
bool disconnected() override;
WiFiClientSecure* client;
WiFiClientSecure client;
};
} // namespace espMqttClientInternals

View File

@@ -13,61 +13,61 @@ the LICENSE file.
namespace espMqttClientInternals {
ClientSync::ClientSync(WiFiClient* wiFiClient)
: client(wiFiClient) {
ClientSync::ClientSync()
: client() {
// empty
}
bool ClientSync::connect(IPAddress ip, uint16_t port) {
bool ret = client->connect(ip, port); // implicit conversion of return code int --> bool
bool ret = client.connect(ip, port); // implicit conversion of return code int --> bool
if (ret) {
#if defined(ARDUINO_ARCH_ESP8266)
client->setNoDelay(true);
client.setNoDelay(true);
#elif defined(ARDUINO_ARCH_ESP32)
// Set TCP option directly to bypass lack of working setNoDelay for WiFiClientSecure (for consistency also here)
int val = true;
client->setSocketOption(IPPROTO_TCP, TCP_NODELAY, &val, sizeof(int));
client.setSocketOption(IPPROTO_TCP, TCP_NODELAY, &val, sizeof(int));
#endif
}
return ret;
}
bool ClientSync::connect(const char* host, uint16_t port) {
bool ret = client->connect(host, port); // implicit conversion of return code int --> bool
bool ret = client.connect(host, port); // implicit conversion of return code int --> bool
if (ret) {
#if defined(ARDUINO_ARCH_ESP8266)
client->setNoDelay(true);
client.setNoDelay(true);
#elif defined(ARDUINO_ARCH_ESP32)
// Set TCP option directly to bypass lack of working setNoDelay for WiFiClientSecure (for consistency also here)
int val = true;
client->setSocketOption(IPPROTO_TCP, TCP_NODELAY, &val, sizeof(int));
client.setSocketOption(IPPROTO_TCP, TCP_NODELAY, &val, sizeof(int));
#endif
}
return ret;
}
size_t ClientSync::write(const uint8_t* buf, size_t size) {
return client->write(buf, size);
return client.write(buf, size);
}
int ClientSync::available() {
return client->available();
return client.available();
}
int ClientSync::read(uint8_t* buf, size_t size) {
return client->read(buf, size);
return client.read(buf, size);
}
void ClientSync::stop() {
client->stop();
client.stop();
}
bool ClientSync::connected() {
return client->connected();
return client.connected();
}
bool ClientSync::disconnected() {
return !client->connected();
return !client.connected();
}
} // namespace espMqttClientInternals

View File

@@ -18,7 +18,7 @@ namespace espMqttClientInternals {
class ClientSync : public Transport {
public:
ClientSync(WiFiClient* wiFiClient);
ClientSync();
bool connect(IPAddress ip, uint16_t port) override;
bool connect(const char* host, uint16_t port) override;
size_t write(const uint8_t* buf, size_t size) override;
@@ -27,7 +27,7 @@ class ClientSync : public Transport {
void stop() override;
bool connected() override;
bool disconnected() override;
WiFiClient* client;
WiFiClient client;
};
} // namespace espMqttClientInternals

View File

@@ -1,79 +0,0 @@
/*
Copyright (c) 2022 Bert Melis. All rights reserved.
This work is licensed under the terms of the MIT license.
For a copy, see <https://opensource.org/licenses/MIT> or
the LICENSE file.
*/
#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
#include "ClientSyncEthernet.h"
#include <lwip/sockets.h> // socket options
namespace espMqttClientInternals {
ClientSyncEthernet::ClientSyncEthernet(EthernetClient* ethernetClient)
: client(ethernetClient) {
// empty
}
bool ClientSyncEthernet::connect(IPAddress ip, uint16_t port) {
bool ret = client->connect(ip, port); // implicit conversion of return code int --> bool
if (ret) {
#if defined(ARDUINO_ARCH_ESP8266)
client->setNoDelay(true);
#elif defined(ARDUINO_ARCH_ESP32)
// Set TCP option directly to bypass lack of working setNoDelay for WiFiClientSecure (for consistency also here)
int val = true;
// TODO
// client->setSocketOption(IPPROTO_TCP, TCP_NODELAY, &val, sizeof(int));
#endif
}
return ret;
}
bool ClientSyncEthernet::connect(const char* host, uint16_t port) {
bool ret = client->connect(host, port); // implicit conversion of return code int --> bool
if (ret) {
#if defined(ARDUINO_ARCH_ESP8266)
client->setNoDelay(true);
#elif defined(ARDUINO_ARCH_ESP32)
// Set TCP option directly to bypass lack of working setNoDelay for WiFiClientSecure (for consistency also here)
int val = true;
// TODO
// client->setSocketOption(IPPROTO_TCP, TCP_NODELAY, &val, sizeof(int));
#endif
}
return ret;
}
size_t ClientSyncEthernet::write(const uint8_t* buf, size_t size) {
return client->write(buf, size);
}
int ClientSyncEthernet::available() {
return client->available();
}
int ClientSyncEthernet::read(uint8_t* buf, size_t size) {
return client->read(buf, size);
}
void ClientSyncEthernet::stop() {
client->stop();
}
bool ClientSyncEthernet::connected() {
return client->connected();
}
bool ClientSyncEthernet::disconnected() {
return !client->connected();
}
} // namespace espMqttClientInternals
#endif

View File

@@ -1,26 +0,0 @@
#pragma once
#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
#include "Transport.h"
#include "EthernetClient.h"
namespace espMqttClientInternals {
class ClientSyncEthernet : public Transport {
public:
ClientSyncEthernet(EthernetClient* ethernetClient);
bool connect(IPAddress ip, uint16_t port) override;
bool connect(const char* host, uint16_t port) override;
size_t write(const uint8_t* buf, size_t size) override;
int available() override;
int read(uint8_t* buf, size_t size) override;
void stop() override;
bool connected() override;
bool disconnected() override;
EthernetClient* client;
};
} // namespace espMqttClientInternals
#endif