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,103 +13,104 @@ the LICENSE file.
#include "MqttClient.h"
template <typename T>
class MqttClientSetup : public MqttClient {
public:
void setKeepAlive(uint16_t keepAlive) {
_keepAlive = keepAlive * 1000; // s to ms conversion, will also do 16 to 32 bit conversion
public:
T& setKeepAlive(uint16_t keepAlive) {
_keepAlive = keepAlive * 1000; // s to ms conversion, will also do 16 to 32 bit conversion
return static_cast<T&>(*this);
}
T& setClientId(const char* clientId) {
_clientId = clientId;
return static_cast<T&>(*this);
}
T& setCleanSession(bool cleanSession) {
_cleanSession = cleanSession;
return static_cast<T&>(*this);
}
T& setCredentials(const char* username, const char* password) {
_username = username;
_password = password;
return static_cast<T&>(*this);
}
T& setWill(const char* topic, uint8_t qos, bool retain, const uint8_t* payload, size_t length) {
_willTopic = topic;
_willQos = qos;
_willRetain = retain;
_willPayload = payload;
if (!_willPayload) {
_willPayloadLength = 0;
} else {
_willPayloadLength = length;
}
return static_cast<T&>(*this);
}
void setClientId(const char* clientId) {
_clientId = clientId;
T& setWill(const char* topic, uint8_t qos, bool retain, const char* payload) {
return setWill(topic, qos, retain, reinterpret_cast<const uint8_t*>(payload), strlen(payload));
}
}
T& setServer(IPAddress ip, uint16_t port) {
_ip = ip;
_port = port;
_useIp = true;
return static_cast<T&>(*this);
}
void setCleanSession(bool cleanSession) {
_cleanSession = cleanSession;
T& setServer(const char* host, uint16_t port) {
_host = host;
_port = port;
_useIp = false;
return static_cast<T&>(*this);
}
}
T& onConnect(espMqttClientTypes::OnConnectCallback callback) {
_onConnectCallback = callback;
return static_cast<T&>(*this);
}
void setCredentials(const char* username, const char* password) {
_username = username;
_password = password;
T& onDisconnect(espMqttClientTypes::OnDisconnectCallback callback) {
_onDisconnectCallback = callback;
return static_cast<T&>(*this);
}
}
T& onSubscribe(espMqttClientTypes::OnSubscribeCallback callback) {
_onSubscribeCallback = callback;
return static_cast<T&>(*this);
}
void setWill(const char* topic, uint8_t qos, bool retain, const uint8_t* payload, size_t length) {
_willTopic = topic;
_willQos = qos;
_willRetain = retain;
_willPayload = payload;
if (!_willPayload) {
_willPayloadLength = 0;
} else {
_willPayloadLength = length;
}
T& onUnsubscribe(espMqttClientTypes::OnUnsubscribeCallback callback) {
_onUnsubscribeCallback = callback;
return static_cast<T&>(*this);
}
}
T& onMessage(espMqttClientTypes::OnMessageCallback callback) {
_onMessageCallback = callback;
return static_cast<T&>(*this);
}
void setWill(const char* topic, uint8_t qos, bool retain, const char* payload) {
return setWill(topic, qos, retain, reinterpret_cast<const uint8_t*>(payload), strlen(payload));
}
T& onPublish(espMqttClientTypes::OnPublishCallback callback) {
_onPublishCallback = callback;
return static_cast<T&>(*this);
}
void setServer(IPAddress ip, uint16_t port) {
_ip = ip;
_port = port;
_useIp = true;
/*
T& onError(espMqttClientTypes::OnErrorCallback callback) {
_onErrorCallback = callback;
return static_cast<T&>(*this);
}
*/
}
void setServer(const char* host, uint16_t port) {
_host = host;
_port = port;
_useIp = false;
}
void onConnect(espMqttClientTypes::OnConnectCallback callback) {
_onConnectCallback = callback;
}
void onDisconnect(espMqttClientTypes::OnDisconnectCallback callback) {
_onDisconnectCallback = callback;
}
void onSubscribe(espMqttClientTypes::OnSubscribeCallback callback) {
_onSubscribeCallback = callback;
}
void onUnsubscribe(espMqttClientTypes::OnUnsubscribeCallback callback) {
_onUnsubscribeCallback = callback;
}
void onMessage(espMqttClientTypes::OnMessageCallback callback) {
_onMessageCallback = callback;
}
void onPublish(espMqttClientTypes::OnPublishCallback callback) {
_onPublishCallback = callback;
}
/*
void onError(espMqttClientTypes::OnErrorCallback callback) {
_onErrorCallback = callback;
}
*/
protected:
protected:
#if defined(ESP32)
explicit MqttClientSetup(bool useTask, uint8_t priority = 1, uint8_t core = 1)
: MqttClient(useTask, priority, core) {}
explicit MqttClientSetup(bool useTask, uint8_t priority = 1, uint8_t core = 1)
: MqttClient(useTask, priority, core) {}
#else
MqttClientSetup()
MqttClientSetup()
: MqttClient() {}
#endif
};

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

View File

@@ -9,9 +9,9 @@ the LICENSE file.
#include "espMqttClient.h"
#if defined(ARDUINO_ARCH_ESP32)
espMqttClient::espMqttClient(WiFiClient* wiFiClient, uint8_t priority, uint8_t core)
espMqttClient::espMqttClient(uint8_t priority, uint8_t core)
: MqttClientSetup(true, priority, core)
, _client(wiFiClient) {
, _client() {
#else
espMqttClient::espMqttClient()
: _client() {
@@ -21,9 +21,9 @@ espMqttClient::espMqttClient()
#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
#if defined(ARDUINO_ARCH_ESP32)
espMqttClientSecure::espMqttClientSecure(WiFiClientSecure* wiFiClient, uint8_t priority, uint8_t core)
espMqttClientSecure::espMqttClientSecure(uint8_t priority, uint8_t core)
: MqttClientSetup(priority, core)
, _client(wiFiClient) {
, _client() {
#else
espMqttClientSecure::espMqttClientSecure()
: _client() {
@@ -32,66 +32,55 @@ espMqttClientSecure::espMqttClientSecure()
}
espMqttClientSecure& espMqttClientSecure::setInsecure() {
_client.client->setInsecure();
_client.client.setInsecure();
return *this;
}
#if defined(ARDUINO_ARCH_ESP32)
espMqttClientSecure& espMqttClientSecure::setCACert(const char* rootCA) {
_client.client->setCACert(rootCA);
_client.client.setCACert(rootCA);
return *this;
}
espMqttClientSecure& espMqttClientSecure::setCertificate(const char* clientCa) {
_client.client->setCertificate(clientCa);
_client.client.setCertificate(clientCa);
return *this;
}
espMqttClientSecure& espMqttClientSecure::setPrivateKey(const char* privateKey) {
_client.client->setPrivateKey(privateKey);
_client.client.setPrivateKey(privateKey);
return *this;
}
espMqttClientSecure& espMqttClientSecure::setPreSharedKey(const char* pskIdent, const char* psKey) {
_client.client->setPreSharedKey(pskIdent, psKey);
_client.client.setPreSharedKey(pskIdent, psKey);
return *this;
}
#elif defined(ARDUINO_ARCH_ESP8266)
espMqttClientSecure& espMqttClientSecure::setFingerprint(const uint8_t fingerprint[20]) {
_client.client->setFingerprint(fingerprint);
_client.client.setFingerprint(fingerprint);
return *this;
}
espMqttClientSecure& espMqttClientSecure::setTrustAnchors(const X509List *ta) {
_client.client->setTrustAnchors(ta);
_client.client.setTrustAnchors(ta);
return *this;
}
espMqttClientSecure& espMqttClientSecure::setClientRSACert(const X509List *cert, const PrivateKey *sk) {
_client.client->setClientRSACert(cert, sk);
_client.client.setClientRSACert(cert, sk);
return *this;
}
espMqttClientSecure& espMqttClientSecure::setClientECCert(const X509List *cert, const PrivateKey *sk, unsigned allowed_usages, unsigned cert_issuer_key_type) {
_client.client->setClientECCert(cert, sk, allowed_usages, cert_issuer_key_type);
_client.client.setClientECCert(cert, sk, allowed_usages, cert_issuer_key_type);
return *this;
}
espMqttClientSecure& espMqttClientSecure::setCertStore(CertStoreBase *certStore) {
_client.client->setCertStore(certStore);
_client.client.setCertStore(certStore);
return *this;
}
#endif
#endif
#if defined(ARDUINO_ARCH_ESP32)
espMqttClientEthernet::espMqttClientEthernet(EthernetClient* ethernetClient, uint8_t priority, uint8_t core)
: MqttClientSetup(true, priority, core)
, _client(ethernetClient) {
#else
espMqttClientEthernet::espMqttClientEthernet()
: _client() {
#endif
_transport = &_client;
}

View File

@@ -19,64 +19,47 @@ the LICENSE file.
#endif
#include "MqttClientSetup.h"
#include "Transport/ClientSyncEthernet.h"
class espMqttClient : public MqttClientSetup {
public:
class espMqttClient : public MqttClientSetup<espMqttClient> {
public:
#if defined(ARDUINO_ARCH_ESP32)
explicit espMqttClient(WiFiClient* wiFiClient, uint8_t priority = 1, uint8_t core = 1);
explicit espMqttClient(uint8_t priority = 1, uint8_t core = 1);
#else
espMqttClient();
espMqttClient();
#endif
protected:
protected:
#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
espMqttClientInternals::ClientSync _client;
espMqttClientInternals::ClientSync _client;
#elif defined(__linux__)
espMqttClientInternals::ClientPosix _client;
espMqttClientInternals::ClientPosix _client;
#endif
};
#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
class espMqttClientSecure : public MqttClientSetup {
public:
#if defined(ARDUINO_ARCH_ESP32)
explicit espMqttClientSecure(WiFiClientSecure* wiFiClient, uint8_t priority = 1, uint8_t core = 1);
#else
espMqttClientSecure();
#endif
espMqttClientSecure& setInsecure();
#if defined(ARDUINO_ARCH_ESP32)
espMqttClientSecure& setCACert(const char* rootCA);
espMqttClientSecure& setCertificate(const char* clientCa);
espMqttClientSecure& setPrivateKey(const char* privateKey);
espMqttClientSecure& setPreSharedKey(const char* pskIdent, const char* psKey);
#else
espMqttClientSecure& setFingerprint(const uint8_t fingerprint[20]);
class espMqttClientSecure : public MqttClientSetup<espMqttClientSecure> {
public:
#if defined(ARDUINO_ARCH_ESP32)
explicit espMqttClientSecure(uint8_t priority = 1, uint8_t core = 1);
#else
espMqttClientSecure();
#endif
espMqttClientSecure& setInsecure();
#if defined(ARDUINO_ARCH_ESP32)
espMqttClientSecure& setCACert(const char* rootCA);
espMqttClientSecure& setCertificate(const char* clientCa);
espMqttClientSecure& setPrivateKey(const char* privateKey);
espMqttClientSecure& setPreSharedKey(const char* pskIdent, const char* psKey);
#else
espMqttClientSecure& setFingerprint(const uint8_t fingerprint[20]);
espMqttClientSecure& setTrustAnchors(const X509List *ta);
espMqttClientSecure& setClientRSACert(const X509List *cert, const PrivateKey *sk);
espMqttClientSecure& setClientECCert(const X509List *cert, const PrivateKey *sk, unsigned allowed_usages, unsigned cert_issuer_key_type);
espMqttClientSecure& setCertStore(CertStoreBase *certStore);
#endif
#endif
protected:
espMqttClientInternals::ClientSecureSync _client;
};
class espMqttClientEthernet : public MqttClientSetup {
public:
#if defined(ARDUINO_ARCH_ESP32)
explicit espMqttClientEthernet(EthernetClient* ethernetClient, uint8_t priority = 1, uint8_t core = 1);
#else
espMqttClient();
#endif
protected:
#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
espMqttClientInternals::ClientSyncEthernet _client;
#elif defined(__linux__)
espMqttClientInternals::ClientPosix _client;
#endif
protected:
espMqttClientInternals::ClientSecureSync _client;
};
#endif

View File

@@ -17,7 +17,7 @@ the LICENSE file.
#include "MqttClientSetup.h"
class espMqttClientAsync : public MqttClientSetup {
class espMqttClientAsync : public MqttClientSetup<espMqttClientAsync> {
public:
#if defined(ARDUINO_ARCH_ESP32)
explicit espMqttClientAsync(uint8_t priority = 1, uint8_t core = 1);