refactor NetworkDevice interface to work with new esp mqtt lib
This commit is contained in:
79
networkDevices/ClientSyncEthernet.cpp
Normal file
79
networkDevices/ClientSyncEthernet.cpp
Normal file
@@ -0,0 +1,79 @@
|
||||
/*
|
||||
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()
|
||||
: client() {
|
||||
// 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
|
||||
26
networkDevices/ClientSyncEthernet.h
Normal file
26
networkDevices/ClientSyncEthernet.h
Normal file
@@ -0,0 +1,26 @@
|
||||
#pragma once
|
||||
|
||||
#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
|
||||
|
||||
#include "Transport/Transport.h"
|
||||
#include "EthernetClient.h"
|
||||
|
||||
namespace espMqttClientInternals {
|
||||
|
||||
class ClientSyncEthernet : public Transport {
|
||||
public:
|
||||
ClientSyncEthernet();
|
||||
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
|
||||
@@ -17,8 +17,6 @@ public:
|
||||
: _hostname(hostname)
|
||||
{}
|
||||
|
||||
virtual MqttClientSetup* mqttClient() = 0;
|
||||
|
||||
virtual void initialize() = 0;
|
||||
virtual ReconnectStatus reconnect() = 0;
|
||||
virtual void reconfigure() = 0;
|
||||
@@ -29,6 +27,17 @@ public:
|
||||
virtual bool isConnected() = 0;
|
||||
virtual int8_t signalStrength() = 0;
|
||||
|
||||
virtual void mqttSetClientId(const char* clientId) = 0;
|
||||
virtual void mqttSetCleanSession(bool cleanSession) = 0;
|
||||
virtual uint16_t mqttPublish(const char* topic, uint8_t qos, bool retain, const char* payload) = 0;
|
||||
virtual uint16_t mqttPublish(const char* topic, uint8_t qos, bool retain, const uint8_t* payload, size_t length) = 0;
|
||||
virtual bool mqttConnected() const = 0;
|
||||
virtual void mqttSetServer(const char* host, uint16_t port) = 0;
|
||||
virtual bool mqttConnect() = 0;
|
||||
virtual void mqttSetCredentials(const char* username, const char* password) = 0;
|
||||
virtual void mqttOnMessage(espMqttClientTypes::OnMessageCallback callback) = 0;
|
||||
virtual uint16_t mqttSubscribe(const char* topic, uint8_t qos) = 0;
|
||||
|
||||
protected:
|
||||
const uint16_t _mqttMaxBufferSize = 6144;
|
||||
const String _hostname;
|
||||
|
||||
@@ -38,8 +38,6 @@ void W5500Device::initialize()
|
||||
resetDevice();
|
||||
|
||||
Ethernet.init(ETHERNET_CS_PIN);
|
||||
_ethClient = new EthernetClient();
|
||||
_mqttClient = new espMqttClientEthernet(_ethClient);
|
||||
|
||||
if(_preferences->getBool(preference_mqtt_log_enabled))
|
||||
{
|
||||
@@ -49,7 +47,7 @@ void W5500Device::initialize()
|
||||
String pathStr = _preferences->getString(preference_mqtt_lock_path);
|
||||
pathStr.concat(mqtt_topic_log);
|
||||
strcpy(_path, pathStr.c_str());
|
||||
Log = new MqttLogger(*_mqttClient, _path, MqttLoggerMode::MqttAndSerial);
|
||||
Log = new MqttLogger(this, _path, MqttLoggerMode::MqttAndSerial);
|
||||
}
|
||||
|
||||
reconnect();
|
||||
@@ -185,8 +183,52 @@ int8_t W5500Device::signalStrength()
|
||||
return 127;
|
||||
}
|
||||
|
||||
MqttClientSetup *W5500Device::mqttClient()
|
||||
void W5500Device::mqttSetClientId(const char *clientId)
|
||||
{
|
||||
return _mqttClient;
|
||||
_mqttClient.setClientId(clientId);
|
||||
}
|
||||
|
||||
void W5500Device::mqttSetCleanSession(bool cleanSession)
|
||||
{
|
||||
_mqttClient.setCleanSession(cleanSession);
|
||||
}
|
||||
|
||||
uint16_t W5500Device::mqttPublish(const char *topic, uint8_t qos, bool retain, const char *payload)
|
||||
{
|
||||
return _mqttClient.publish(topic, qos, retain, payload);
|
||||
}
|
||||
|
||||
bool W5500Device::mqttConnected() const
|
||||
{
|
||||
return _mqttClient.connected();
|
||||
}
|
||||
|
||||
void W5500Device::mqttSetServer(const char *host, uint16_t port)
|
||||
{
|
||||
_mqttClient.setServer(host, port);
|
||||
}
|
||||
|
||||
bool W5500Device::mqttConnect()
|
||||
{
|
||||
return _mqttClient.connect();
|
||||
}
|
||||
|
||||
void W5500Device::mqttSetCredentials(const char *username, const char *password)
|
||||
{
|
||||
_mqttClient.setCredentials(username, password);
|
||||
}
|
||||
|
||||
void W5500Device::mqttOnMessage(espMqttClientTypes::OnMessageCallback callback)
|
||||
{
|
||||
_mqttClient.onMessage(callback);
|
||||
}
|
||||
|
||||
uint16_t W5500Device::mqttSubscribe(const char *topic, uint8_t qos)
|
||||
{
|
||||
return _mqttClient.subscribe(topic, qos);
|
||||
}
|
||||
|
||||
uint16_t W5500Device::mqttPublish(const char *topic, uint8_t qos, bool retain, const uint8_t *payload, size_t length)
|
||||
{
|
||||
return _mqttClient.publish(topic, qos, retain, payload, length);
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
#include "NetworkDevice.h"
|
||||
#include "espMqttClient.h"
|
||||
#include "espMqttClientEthernet.h"
|
||||
#include <Ethernet.h>
|
||||
#include <Preferences.h>
|
||||
|
||||
@@ -22,14 +23,31 @@ public:
|
||||
|
||||
int8_t signalStrength() override;
|
||||
|
||||
virtual MqttClientSetup* mqttClient();
|
||||
void mqttSetClientId(const char *clientId) override;
|
||||
|
||||
void mqttSetCleanSession(bool cleanSession) override;
|
||||
|
||||
uint16_t mqttPublish(const char *topic, uint8_t qos, bool retain, const char *payload) override;
|
||||
|
||||
uint16_t mqttPublish(const char *topic, uint8_t qos, bool retain, const uint8_t *payload, size_t length) override;
|
||||
|
||||
bool mqttConnected() const override;
|
||||
|
||||
void mqttSetServer(const char *host, uint16_t port) override;
|
||||
|
||||
bool mqttConnect() override;
|
||||
|
||||
void mqttSetCredentials(const char *username, const char *password) override;
|
||||
|
||||
void mqttOnMessage(espMqttClientTypes::OnMessageCallback callback) override;
|
||||
|
||||
uint16_t mqttSubscribe(const char *topic, uint8_t qos) override;
|
||||
|
||||
private:
|
||||
void resetDevice();
|
||||
void initializeMacAddress(byte* mac);
|
||||
|
||||
EthernetClient* _ethClient = nullptr;
|
||||
MqttClientSetup* _mqttClient = nullptr;
|
||||
espMqttClientEthernet _mqttClient;
|
||||
Preferences* _preferences = nullptr;
|
||||
|
||||
int _maintainResult = 0;
|
||||
|
||||
@@ -18,26 +18,26 @@ WifiDevice::WifiDevice(const String& hostname, Preferences* _preferences)
|
||||
size_t crtLength = _preferences->getString(preference_mqtt_crt,_cert,TLS_CERT_MAX_SIZE);
|
||||
size_t keyLength = _preferences->getString(preference_mqtt_key,_key,TLS_KEY_MAX_SIZE);
|
||||
|
||||
if(caLength > 1) // length is 1 when empty
|
||||
_useEncryption = caLength > 1;
|
||||
|
||||
if(_useEncryption > 1) // length is 1 when empty
|
||||
{
|
||||
Log->println(F("MQTT over TLS."));
|
||||
Log->println(_ca);
|
||||
_wifiClientSecure = new WiFiClientSecure();
|
||||
_wifiClientSecure->setCACert(_ca);
|
||||
_mqttClientSecure = new espMqttClientSecure();
|
||||
_mqttClientSecure->setCACert(_ca);
|
||||
if(crtLength > 1 && keyLength > 1) // length is 1 when empty
|
||||
{
|
||||
Log->println(F("MQTT with client certificate."));
|
||||
Log->println(_cert);
|
||||
Log->println(_key);
|
||||
_wifiClientSecure->setCertificate(_cert);
|
||||
_wifiClientSecure->setPrivateKey(_key);
|
||||
_mqttClientSecure->setCertificate(_cert);
|
||||
_mqttClientSecure->setPrivateKey(_key);
|
||||
}
|
||||
_mqttClient = new espMqttClientSecure(_wifiClientSecure);
|
||||
} else
|
||||
{
|
||||
Log->println(F("MQTT without TLS."));
|
||||
_wifiClient = new WiFiClient();
|
||||
_mqttClient = new espMqttClient(_wifiClient);
|
||||
_mqttClient = new espMqttClient();
|
||||
}
|
||||
|
||||
if(_preferences->getBool(preference_mqtt_log_enabled))
|
||||
@@ -48,7 +48,7 @@ WifiDevice::WifiDevice(const String& hostname, Preferences* _preferences)
|
||||
String pathStr = _preferences->getString(preference_mqtt_lock_path);
|
||||
pathStr.concat(mqtt_topic_log);
|
||||
strcpy(_path, pathStr.c_str());
|
||||
Log = new MqttLogger(*_mqttClient, _path, MqttLoggerMode::MqttAndSerial);
|
||||
Log = new MqttLogger(this, _path, MqttLoggerMode::MqttAndSerial);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -108,12 +108,12 @@ void WifiDevice::reconfigure()
|
||||
|
||||
void WifiDevice::printError()
|
||||
{
|
||||
if(_wifiClientSecure != nullptr)
|
||||
{
|
||||
char lastError[100];
|
||||
_wifiClientSecure->lastError(lastError,100);
|
||||
Log->println(lastError);
|
||||
}
|
||||
// if(_wifiClientSecure != nullptr)
|
||||
// {
|
||||
// char lastError[100];
|
||||
// _wifiClientSecure->lastError(lastError,100);
|
||||
// Log->println(lastError);
|
||||
// }
|
||||
Log->print(F("Free Heap: "));
|
||||
Log->println(ESP.getFreeHeap());
|
||||
}
|
||||
@@ -152,7 +152,122 @@ void WifiDevice::clearRtcInitVar(WiFiManager *)
|
||||
memset(WiFiDevice_reconfdetect, 0, sizeof WiFiDevice_reconfdetect);
|
||||
}
|
||||
|
||||
MqttClientSetup *WifiDevice::mqttClient()
|
||||
void WifiDevice::mqttSetClientId(const char *clientId)
|
||||
{
|
||||
return _mqttClient;
|
||||
if(_useEncryption)
|
||||
{
|
||||
_mqttClientSecure->setClientId(clientId);
|
||||
}
|
||||
else
|
||||
{
|
||||
_mqttClient->setClientId(clientId);
|
||||
}
|
||||
}
|
||||
|
||||
void WifiDevice::mqttSetCleanSession(bool cleanSession)
|
||||
{
|
||||
if(_useEncryption)
|
||||
{
|
||||
_mqttClientSecure->setCleanSession(cleanSession);
|
||||
}
|
||||
else
|
||||
{
|
||||
_mqttClient->setCleanSession(cleanSession);
|
||||
}
|
||||
}
|
||||
|
||||
uint16_t WifiDevice::mqttPublish(const char *topic, uint8_t qos, bool retain, const char *payload)
|
||||
{
|
||||
if(_useEncryption)
|
||||
{
|
||||
return _mqttClientSecure->publish(topic, qos, retain, payload);
|
||||
}
|
||||
else
|
||||
{
|
||||
return _mqttClient->publish(topic, qos, retain, payload);
|
||||
}
|
||||
}
|
||||
|
||||
uint16_t WifiDevice::mqttPublish(const char *topic, uint8_t qos, bool retain, const uint8_t *payload, size_t length)
|
||||
{
|
||||
if(_useEncryption)
|
||||
{
|
||||
return _mqttClientSecure->publish(topic, qos, retain, payload, length);
|
||||
}
|
||||
else
|
||||
{
|
||||
return _mqttClient->publish(topic, qos, retain, payload, length);
|
||||
}
|
||||
}
|
||||
|
||||
bool WifiDevice::mqttConnected() const
|
||||
{
|
||||
if(_useEncryption)
|
||||
{
|
||||
return _mqttClientSecure->connected();
|
||||
}
|
||||
else
|
||||
{
|
||||
return _mqttClient->connected();
|
||||
}
|
||||
}
|
||||
|
||||
void WifiDevice::mqttSetServer(const char *host, uint16_t port)
|
||||
{
|
||||
if(_useEncryption)
|
||||
{
|
||||
_mqttClientSecure->setServer(host, port);
|
||||
}
|
||||
else
|
||||
{
|
||||
_mqttClient->setServer(host, port);
|
||||
}
|
||||
}
|
||||
|
||||
bool WifiDevice::mqttConnect()
|
||||
{
|
||||
if(_useEncryption)
|
||||
{
|
||||
return _mqttClientSecure->connect();
|
||||
}
|
||||
else
|
||||
{
|
||||
return _mqttClient->connect();
|
||||
}
|
||||
}
|
||||
|
||||
void WifiDevice::mqttSetCredentials(const char *username, const char *password)
|
||||
{
|
||||
if(_useEncryption)
|
||||
{
|
||||
_mqttClientSecure->setCredentials(username, password);
|
||||
}
|
||||
else
|
||||
{
|
||||
_mqttClient->setCredentials(username, password);
|
||||
}
|
||||
}
|
||||
|
||||
void WifiDevice::mqttOnMessage(espMqttClientTypes::OnMessageCallback callback)
|
||||
{
|
||||
if(_useEncryption)
|
||||
{
|
||||
_mqttClientSecure->onMessage(callback);
|
||||
}
|
||||
else
|
||||
{
|
||||
_mqttClient->onMessage(callback);
|
||||
}
|
||||
}
|
||||
|
||||
uint16_t WifiDevice::mqttSubscribe(const char *topic, uint8_t qos)
|
||||
{
|
||||
if(_useEncryption)
|
||||
{
|
||||
return _mqttClientSecure->subscribe(topic, qos);
|
||||
}
|
||||
else
|
||||
{
|
||||
return _mqttClient->subscribe(topic, qos);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
#include <Preferences.h>
|
||||
#include "NetworkDevice.h"
|
||||
#include "WiFiManager.h"
|
||||
#include "espMqttClient.h"
|
||||
|
||||
class WifiDevice : public NetworkDevice
|
||||
{
|
||||
@@ -22,7 +23,25 @@ public:
|
||||
|
||||
int8_t signalStrength() override;
|
||||
|
||||
MqttClientSetup *mqttClient() override;
|
||||
void mqttSetClientId(const char *clientId) override;
|
||||
|
||||
void mqttSetCleanSession(bool cleanSession) override;
|
||||
|
||||
uint16_t mqttPublish(const char *topic, uint8_t qos, bool retain, const char *payload) override;
|
||||
|
||||
uint16_t mqttPublish(const char *topic, uint8_t qos, bool retain, const uint8_t *payload, size_t length) override;
|
||||
|
||||
bool mqttConnected() const override;
|
||||
|
||||
void mqttSetServer(const char *host, uint16_t port) override;
|
||||
|
||||
bool mqttConnect() override;
|
||||
|
||||
void mqttSetCredentials(const char *username, const char *password) override;
|
||||
|
||||
void mqttOnMessage(espMqttClientTypes::OnMessageCallback callback) override;
|
||||
|
||||
uint16_t mqttSubscribe(const char *topic, uint8_t qos) override;
|
||||
|
||||
private:
|
||||
static void clearRtcInitVar(WiFiManager*);
|
||||
@@ -30,13 +49,13 @@ private:
|
||||
void onDisconnected();
|
||||
|
||||
WiFiManager _wm;
|
||||
WiFiClient* _wifiClient = nullptr;
|
||||
WiFiClientSecure* _wifiClientSecure = nullptr;
|
||||
MqttClientSetup* _mqttClient = nullptr;
|
||||
// SpiffsCookie _cookie;
|
||||
espMqttClient* _mqttClient = nullptr;
|
||||
espMqttClientSecure* _mqttClientSecure = nullptr;
|
||||
|
||||
bool _restartOnDisconnect = false;
|
||||
bool _startAp = false;
|
||||
char* _path;
|
||||
bool _useEncryption = false;
|
||||
|
||||
char _ca[TLS_CA_MAX_SIZE];
|
||||
char _cert[TLS_CERT_MAX_SIZE];
|
||||
|
||||
8
networkDevices/espMqttClientEthernet.cpp
Normal file
8
networkDevices/espMqttClientEthernet.cpp
Normal file
@@ -0,0 +1,8 @@
|
||||
#include "espMqttClientEthernet.h"
|
||||
|
||||
espMqttClientEthernet::espMqttClientEthernet(uint8_t priority, uint8_t core)
|
||||
: MqttClientSetup(true, priority, core),
|
||||
_client()
|
||||
{
|
||||
_transport = &_client;
|
||||
}
|
||||
20
networkDevices/espMqttClientEthernet.h
Normal file
20
networkDevices/espMqttClientEthernet.h
Normal file
@@ -0,0 +1,20 @@
|
||||
#pragma once
|
||||
|
||||
#include "MqttClientSetup.h"
|
||||
#include "ClientSyncEthernet.h"
|
||||
|
||||
class espMqttClientEthernet : public MqttClientSetup<espMqttClientEthernet> {
|
||||
public:
|
||||
#if defined(ARDUINO_ARCH_ESP32)
|
||||
explicit espMqttClientEthernet(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
|
||||
};
|
||||
Reference in New Issue
Block a user