move mqtt loop into network task

This commit is contained in:
technyon
2023-02-26 10:26:55 +01:00
parent c5bd325ed5
commit 8b647e8b8e
12 changed files with 129 additions and 17 deletions

View File

@@ -66,10 +66,10 @@ class MqttClient {
void clearQueue(bool all = false); // Not MQTT compliant and may cause unpredictable results when `all` = true!
const char* getClientId() const;
#if defined(ARDUINO_ARCH_ESP32)
void loop();
protected:
#endif
void loop();
#if defined(ARDUINO_ARCH_ESP32)
explicit MqttClient(bool useTask, uint8_t priority = 1, uint8_t core = 1);
bool _useTask;

View File

@@ -66,3 +66,69 @@ void espMqttClientAsync::onPollCb(void* a, AsyncClient* c) {
}
#endif
#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
#if defined(ARDUINO_ARCH_ESP32)
espMqttClientSecureAsync::espMqttClientSecureAsync(uint8_t priority, uint8_t core)
: MqttClientSetup(false, priority, core)
, _client() {
#else
espMqttClientSecure::espMqttClientSecure()
: _client() {
#endif
_transport = &_client;
}
espMqttClientSecureAsync& espMqttClientSecureAsync::setInsecure() {
_client.client.setInsecure();
return *this;
}
#if defined(ARDUINO_ARCH_ESP32)
espMqttClientSecureAsync& espMqttClientSecureAsync::setCACert(const char* rootCA) {
_client.client.setCACert(rootCA);
return *this;
}
espMqttClientSecureAsync& espMqttClientSecureAsync::setCertificate(const char* clientCa) {
_client.client.setCertificate(clientCa);
return *this;
}
espMqttClientSecureAsync& espMqttClientSecureAsync::setPrivateKey(const char* privateKey) {
_client.client.setPrivateKey(privateKey);
return *this;
}
espMqttClientSecureAsync& espMqttClientSecureAsync::setPreSharedKey(const char* pskIdent, const char* psKey) {
_client.client.setPreSharedKey(pskIdent, psKey);
return *this;
}
#elif defined(ARDUINO_ARCH_ESP8266)
espMqttClientSecureAsync& espMqttClientSecureAsync::setFingerprint(const uint8_t fingerprint[20]) {
_client.client.setFingerprint(fingerprint);
return *this;
}
espMqttClientSecureAsync& espMqttClientSecureAsync::setTrustAnchors(const X509List *ta) {
_client.client.setTrustAnchors(ta);
return *this;
}
espMqttClientSecureAsync& espMqttClientSecureAsync::setClientRSACert(const X509List *cert, const PrivateKey *sk) {
_client.client.setClientRSACert(cert, sk);
return *this;
}
espMqttClientSecureAsync& espMqttClientSecureAsync::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);
return *this;
}
espMqttClientSecureAsync& espMqttClientSecureAsync::setCertStore(CertStoreBase *certStore) {
_client.client.setCertStore(certStore);
return *this;
}
#endif
#endif

View File

@@ -16,6 +16,7 @@ the LICENSE file.
#include "Transport/ClientAsync.h"
#include "MqttClientSetup.h"
#include "Transport/ClientSecureSync.h"
class espMqttClientAsync : public MqttClientSetup<espMqttClientAsync> {
public:
@@ -38,3 +39,32 @@ class espMqttClientAsync : public MqttClientSetup<espMqttClientAsync> {
};
#endif
#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
class espMqttClientSecureAsync : public MqttClientSetup<espMqttClientSecureAsync> {
public:
#if defined(ARDUINO_ARCH_ESP32)
explicit espMqttClientSecureAsync(uint8_t priority = 1, uint8_t core = 1);
#else
espMqttClientSecure();
#endif
espMqttClientSecureAsync& setInsecure();
#if defined(ARDUINO_ARCH_ESP32)
espMqttClientSecureAsync& setCACert(const char* rootCA);
espMqttClientSecureAsync& setCertificate(const char* clientCa);
espMqttClientSecureAsync& setPrivateKey(const char* privateKey);
espMqttClientSecureAsync& 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
protected:
espMqttClientInternals::ClientSecureSync _client;
};
#endif