Skip to content

Commit

Permalink
avoid breaking change
Browse files Browse the repository at this point in the history
* Enable "old" constructors again
  • Loading branch information
bertmelis authored Mar 4, 2023
1 parent 117c9ca commit 5af833c
Show file tree
Hide file tree
Showing 15 changed files with 127 additions and 99 deletions.
1 change: 0 additions & 1 deletion examples/largepayload-esp8266/largepayload-esp8266.ino
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include <ESP8266WiFi.h>
#include <Ticker.h>

#include <espMqttClient.h>

Expand Down
1 change: 0 additions & 1 deletion examples/ota-esp8266/ota-esp8266.ino
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include <ESP8266WiFi.h>
#include <Updater.h>
#include <Ticker.h>

#include <espMqttClient.h>

Expand Down
1 change: 0 additions & 1 deletion examples/simple-esp32/simple-esp32.ino
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include <WiFi.h>
#include <Ticker.h>

#include <espMqttClient.h>

Expand Down
1 change: 0 additions & 1 deletion examples/simple-esp8266/simple-esp8266.ino
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include <ESP8266WiFi.h>
#include <Ticker.h>

#include <espMqttClient.h>

Expand Down
1 change: 0 additions & 1 deletion examples/simpleAsync-esp32/simpleAsync-esp32.ino
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include <WiFi.h>
#include <Ticker.h>

#include <espMqttClientAsync.h>

Expand Down
1 change: 0 additions & 1 deletion examples/simpleAsync-esp8266/simpleAsync-esp8266.ino
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include <ESP8266WiFi.h>
#include <Ticker.h>

#include <espMqttClientAsync.h>

Expand Down
12 changes: 10 additions & 2 deletions examples/tls-esp32/tls-esp32.ino
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include <WiFi.h>
#include <Ticker.h>

#include <espMqttClient.h>

Expand All @@ -16,7 +15,8 @@ const char rootCA[] = \
" add your certificate here \n" \
"-----END CERTIFICATE-----\n";

espMqttClientSecure mqttClient;
espMqttClientSecure mqttClient(espMqttClientTypes::UseInternalTask::NO);
static TaskHandle_t taskHandle;
bool reconnectMqtt = false;
uint32_t lastReconnect = 0;

Expand Down Expand Up @@ -116,6 +116,12 @@ void onMqttPublish(uint16_t packetId) {
Serial.println(packetId);
}

void networkingTask() {
for (;;) {
mqttClient.loop();
}
}

void setup() {
Serial.begin(115200);
Serial.println();
Expand All @@ -137,6 +143,8 @@ void setup() {
mqttClient.setServer(MQTT_HOST, MQTT_PORT);
mqttClient.setCleanSession(true);

xTaskCreatePinnedToCore((TaskFunction_t)networkingTask, "mqttclienttask", 5120, nullptr, 1, &taskHandle, 0);

connectToWiFi();
}

Expand Down
19 changes: 9 additions & 10 deletions src/MqttClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@ using espMqttClientInternals::PacketType;
using espMqttClientTypes::DisconnectReason;
using espMqttClientTypes::Error;

MqttClient::MqttClient(espMqttClientTypes::UseInternalTask useInternalTask, uint8_t priority, uint8_t core)
#if defined(ARDUINO_ARCH_ESP32)
MqttClient::MqttClient(bool useTask, uint8_t priority, uint8_t core)
: _useTask(useTask)
: _useInternalTask(useInternalTask)
, _transport(nullptr)
#else
MqttClient::MqttClient()
: _transport(nullptr)
#endif
, _onConnectCallback(nullptr)
Expand Down Expand Up @@ -58,19 +57,19 @@ MqttClient::MqttClient()
, _lastServerActivity(0)
, _pingSent(false)
, _disconnectReason(DisconnectReason::TCP_DISCONNECTED)
#if defined(ARDUINO_ARCH_ESP32)
#if ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_INFO
#if defined(ARDUINO_ARCH_ESP32) && ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_INFO
, _highWaterMark(4294967295)
#endif
#endif
{
EMC_GENERATE_CLIENTID(_generatedClientId);
#if defined(ARDUINO_ARCH_ESP32)
_xSemaphore = xSemaphoreCreateMutex();
EMC_SEMAPHORE_GIVE(); // release before first use
if (useTask) {
if (_useInternalTask == espMqttClientTypes::UseInternalTask::YES) {
xTaskCreatePinnedToCore((TaskFunction_t)_loop, "mqttclient", EMC_TASK_STACK_SIZE, this, priority, &_taskHandle, core);
}
#else
(void) useInternalTask;
#endif
_clientId = _generatedClientId;
}
Expand All @@ -80,7 +79,7 @@ MqttClient::~MqttClient() {
_clearQueue(2);
#if defined(ARDUINO_ARCH_ESP32)
vSemaphoreDelete(_xSemaphore);
if (_useTask) {
if (_useInternalTask == espMqttClientTypes::UseInternalTask::YES) {
#if EMC_USE_WATCHDOG
esp_task_wdt_delete(_taskHandle); // not sure if this is really needed
#endif
Expand Down Expand Up @@ -114,7 +113,7 @@ bool MqttClient::connect() {
(uint16_t)(_keepAlive / 1000), // 32b to 16b doesn't overflow because it comes from 16b orignally
_clientId)) {
#if defined(ARDUINO_ARCH_ESP32)
if (_useTask) {
if (_useInternalTask == espMqttClientTypes::UseInternalTask::YES) {
vTaskResume(_taskHandle);
}
#endif
Expand Down Expand Up @@ -197,7 +196,7 @@ void MqttClient::loop() {
switch (_state) {
case State::disconnected:
#if defined(ARDUINO_ARCH_ESP32)
if (_useTask) {
if (_useInternalTask == espMqttClientTypes::UseInternalTask::YES) {
vTaskSuspend(_taskHandle);
}
#endif
Expand Down
8 changes: 2 additions & 6 deletions src/MqttClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,8 @@ class MqttClient {
void loop();

protected:
#if defined(ARDUINO_ARCH_ESP32)
explicit MqttClient(bool useTask, uint8_t priority = 1, uint8_t core = 1);
bool _useTask;
#else
MqttClient();
#endif
explicit MqttClient(espMqttClientTypes::UseInternalTask useInternalTask, uint8_t priority = 1, uint8_t core = 1);
espMqttClientTypes::UseInternalTask _useInternalTask;
espMqttClientInternals::Transport* _transport;

espMqttClientTypes::OnConnectCallback _onConnectCallback;
Expand Down
9 changes: 2 additions & 7 deletions src/MqttClientSetup.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,6 @@ class MqttClientSetup : public MqttClient {
*/

protected:
#if defined(ESP32)
explicit MqttClientSetup(bool useTask, uint8_t priority = 1, uint8_t core = 1)
: MqttClient(useTask, priority, core) {}
#else
MqttClientSetup()
: MqttClient() {}
#endif
explicit MqttClientSetup(espMqttClientTypes::UseInternalTask useInternalTask, uint8_t priority = 1, uint8_t core = 1)
: MqttClient(useInternalTask, priority, core) {}
};
5 changes: 5 additions & 0 deletions src/TypeDefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,9 @@ typedef std::function<void(uint16_t packetId)> OnPublishCallback;
typedef std::function<size_t(uint8_t* data, size_t maxSize, size_t index)> PayloadCallback;
typedef std::function<void(uint16_t packetId, Error error)> OnErrorCallback;

enum class UseInternalTask {
NO = 0,
YES = 1,
};

} // end namespace espMqttClientTypes
97 changes: 62 additions & 35 deletions src/espMqttClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,16 @@ the LICENSE file.

#include "espMqttClient.h"

#if defined(ARDUINO_ARCH_ESP32)
espMqttClient::espMqttClient(bool internalTask, uint8_t priority, uint8_t core)
: MqttClientSetup(internalTask, priority, core)
, _client() {
#else
#if defined(ARDUINO_ARCH_ESP8266)
espMqttClient::espMqttClient()
: _client() {
#endif
: MqttClientSetup(espMqttClientTypes::UseInternalTask::NO)
, _client() {
_transport = &_client;
}

#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
#if defined(ARDUINO_ARCH_ESP32)
espMqttClientSecure::espMqttClientSecure(bool internalTask, uint8_t priority, uint8_t core)
: MqttClientSetup(internalTask, priority, core)
, _client() {
#else
espMqttClientSecure::espMqttClientSecure()
: _client() {
#endif
: MqttClientSetup(espMqttClientTypes::UseInternalTask::NO)
, _client() {
_transport = &_client;
}

Expand All @@ -36,51 +26,88 @@ espMqttClientSecure& espMqttClientSecure::setInsecure() {
return *this;
}

#if defined(ARDUINO_ARCH_ESP32)
espMqttClientSecure& espMqttClientSecure::setCACert(const char* rootCA) {
_client.client.setCACert(rootCA);
espMqttClientSecure& espMqttClientSecure::setFingerprint(const uint8_t fingerprint[20]) {
_client.client.setFingerprint(fingerprint);
return *this;
}

espMqttClientSecure& espMqttClientSecure::setCertificate(const char* clientCa) {
_client.client.setCertificate(clientCa);
espMqttClientSecure& espMqttClientSecure::setTrustAnchors(const X509List *ta) {
_client.client.setTrustAnchors(ta);
return *this;
}

espMqttClientSecure& espMqttClientSecure::setPrivateKey(const char* privateKey) {
_client.client.setPrivateKey(privateKey);
espMqttClientSecure& espMqttClientSecure::setClientRSACert(const X509List *cert, const PrivateKey *sk) {
_client.client.setClientRSACert(cert, sk);
return *this;
}

espMqttClientSecure& espMqttClientSecure::setPreSharedKey(const char* pskIdent, const char* psKey) {
_client.client.setPreSharedKey(pskIdent, psKey);
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);
return *this;
}
#elif defined(ARDUINO_ARCH_ESP8266)
espMqttClientSecure& espMqttClientSecure::setFingerprint(const uint8_t fingerprint[20]) {
_client.client.setFingerprint(fingerprint);

espMqttClientSecure& espMqttClientSecure::setCertStore(CertStoreBase *certStore) {
_client.client.setCertStore(certStore);
return *this;
}
#endif

espMqttClientSecure& espMqttClientSecure::setTrustAnchors(const X509List *ta) {
_client.client.setTrustAnchors(ta);
#if defined(ARDUINO_ARCH_ESP32)
espMqttClient::espMqttClient(espMqttClientTypes::UseInternalTask useInternalTask)
: MqttClientSetup(useInternalTask)
, _client() {
_transport = &_client;
}

espMqttClient::espMqttClient(uint8_t priority, uint8_t core)
: MqttClientSetup(espMqttClientTypes::UseInternalTask::YES, priority, core)
, _client() {
_transport = &_client;
}

espMqttClientSecure::espMqttClientSecure(espMqttClientTypes::UseInternalTask useInternalTask)
: MqttClientSetup(useInternalTask)
, _client() {
_transport = &_client;
}

espMqttClientSecure::espMqttClientSecure(uint8_t priority, uint8_t core)
: MqttClientSetup(espMqttClientTypes::UseInternalTask::YES, priority, core)
, _client() {
_transport = &_client;
}

espMqttClientSecure& espMqttClientSecure::setInsecure() {
_client.client.setInsecure();
return *this;
}

espMqttClientSecure& espMqttClientSecure::setClientRSACert(const X509List *cert, const PrivateKey *sk) {
_client.client.setClientRSACert(cert, sk);
espMqttClientSecure& espMqttClientSecure::setCACert(const char* rootCA) {
_client.client.setCACert(rootCA);
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);
espMqttClientSecure& espMqttClientSecure::setCertificate(const char* clientCa) {
_client.client.setCertificate(clientCa);
return *this;
}

espMqttClientSecure& espMqttClientSecure::setCertStore(CertStoreBase *certStore) {
_client.client.setCertStore(certStore);
espMqttClientSecure& espMqttClientSecure::setPrivateKey(const char* privateKey) {
_client.client.setPrivateKey(privateKey);
return *this;
}

espMqttClientSecure& espMqttClientSecure::setPreSharedKey(const char* pskIdent, const char* psKey) {
_client.client.setPreSharedKey(pskIdent, psKey);
return *this;
}

#endif

#if defined(__linux__)
espMqttClient::espMqttClient()
: MqttClientSetup(espMqttClientTypes::UseInternalTask::NO)
, _client() {
_transport = &_client;
}
#endif
Loading

0 comments on commit 5af833c

Please sign in to comment.