Skip to content

Commit 78cbd77

Browse files
committed
0.8.152
fix ESP8266 compile
1 parent 76899a6 commit 78cbd77

File tree

1 file changed

+44
-9
lines changed

1 file changed

+44
-9
lines changed

patches/espMqttClientSemaphore.patch

+44-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,38 @@
1+
diff --git a/src/Helpers.h b/src/Helpers.h
2+
index 05ab136..50b4c2f 100644
3+
--- a/src/Helpers.h
4+
+++ b/src/Helpers.h
5+
@@ -1,7 +1,7 @@
6+
/*
7+
Copyright (c) 2022 Bert Melis. All rights reserved.
8+
9+
-This work is licensed under the terms of the MIT license.
10+
+This work is licensed under the terms of the MIT license.
11+
For a copy, see <https://opensource.org/licenses/MIT> or
12+
the LICENSE file.
13+
*/
14+
@@ -13,6 +13,7 @@ the LICENSE file.
15+
#include "freertos/FreeRTOS.h"
16+
#include "freertos/task.h"
17+
#include "esp_task_wdt.h"
18+
+ #define EMC_SEMAPHORE_TAKE_CHECK() if(pdTRUE == xSemaphoreTake(_xSemaphore, portMAX_DELAY))
19+
#define EMC_SEMAPHORE_TAKE() xSemaphoreTake(_xSemaphore, portMAX_DELAY)
20+
#define EMC_SEMAPHORE_GIVE() xSemaphoreGive(_xSemaphore)
21+
#define EMC_GET_FREE_MEMORY() std::max(ESP.getMaxAllocHeap(), ESP.getMaxAllocPsram())
22+
@@ -25,9 +26,11 @@ the LICENSE file.
23+
// _xSemaphore defined as std::atomic<bool>
24+
#define EMC_SEMAPHORE_TAKE() while (_xSemaphore) { /*ESP.wdtFeed();*/ } _xSemaphore = true
25+
#define EMC_SEMAPHORE_GIVE() _xSemaphore = false
26+
+ #define EMC_SEMAPHORE_TAKE_CHECK() EMC_SEMAPHORE_TAKE
27+
#else
28+
#define EMC_SEMAPHORE_TAKE()
29+
#define EMC_SEMAPHORE_GIVE()
30+
+ #define EMC_SEMAPHORE_TAKE_CHECK()
31+
#endif
32+
#define EMC_GET_FREE_MEMORY() ESP.getMaxFreeBlockSize()
33+
// no need to yield for ESP8266, the Arduino framework does this internally
134
diff --git a/src/MqttClient.cpp b/src/MqttClient.cpp
2-
index dc21f74..d4b35c4 100644
35+
index dc21f74..d524e50 100644
336
--- a/src/MqttClient.cpp
437
+++ b/src/MqttClient.cpp
538
@@ -1,7 +1,7 @@
@@ -11,7 +44,7 @@ index dc21f74..d4b35c4 100644
1144
For a copy, see <https://opensource.org/licenses/MIT> or
1245
the LICENSE file.
1346
*/
14-
@@ -148,16 +148,19 @@ uint16_t MqttClient::publish(const char* topic, uint8_t qos, bool retain, const
47+
@@ -148,16 +148,20 @@ uint16_t MqttClient::publish(const char* topic, uint8_t qos, bool retain, const
1548
#endif
1649
return 0;
1750
}
@@ -20,14 +53,15 @@ index dc21f74..d4b35c4 100644
2053
- if (!_addPacket(packetId, topic, payload, length, qos, retain)) {
2154
- emc_log_e("Could not create PUBLISH packet");
2255
+ uint16_t packetId = 0;
23-
+ if(pdTRUE == EMC_SEMAPHORE_TAKE()) {
56+
+ EMC_SEMAPHORE_TAKE_CHECK() {
2457
+ packetId = (qos > 0) ? _getNextPacketId() : 1;
2558
+ if (!_addPacket(packetId, topic, payload, length, qos, retain)) {
2659
+ emc_log_e("Could not create PUBLISH packet");
2760
+ EMC_SEMAPHORE_GIVE();
2861
+ _onError(packetId, Error::OUT_OF_MEMORY);
29-
+ if(pdTRUE == EMC_SEMAPHORE_TAKE())
62+
+ EMC_SEMAPHORE_TAKE_CHECK() {
3063
+ packetId = 0;
64+
+ }
3165
+ }
3266
EMC_SEMAPHORE_GIVE();
3367
- _onError(packetId, Error::OUT_OF_MEMORY);
@@ -39,7 +73,7 @@ index dc21f74..d4b35c4 100644
3973
return packetId;
4074
}
4175

42-
@@ -174,16 +177,19 @@ uint16_t MqttClient::publish(const char* topic, uint8_t qos, bool retain, espMqt
76+
@@ -174,16 +178,20 @@ uint16_t MqttClient::publish(const char* topic, uint8_t qos, bool retain, espMqt
4377
#endif
4478
return 0;
4579
}
@@ -48,14 +82,15 @@ index dc21f74..d4b35c4 100644
4882
- if (!_addPacket(packetId, topic, callback, length, qos, retain)) {
4983
- emc_log_e("Could not create PUBLISH packet");
5084
+ uint16_t packetId = 0;
51-
+ if(pdTRUE == EMC_SEMAPHORE_TAKE()) {
85+
+ EMC_SEMAPHORE_TAKE_CHECK() {
5286
+ packetId = (qos > 0) ? _getNextPacketId() : 1;
5387
+ if (!_addPacket(packetId, topic, callback, length, qos, retain)) {
5488
+ emc_log_e("Could not create PUBLISH packet");
5589
+ EMC_SEMAPHORE_GIVE();
5690
+ _onError(packetId, Error::OUT_OF_MEMORY);
57-
+ if(pdTRUE == EMC_SEMAPHORE_TAKE())
91+
+ EMC_SEMAPHORE_TAKE_CHECK() {
5892
+ packetId = 0;
93+
+ }
5994
+ }
6095
EMC_SEMAPHORE_GIVE();
6196
- _onError(packetId, Error::OUT_OF_MEMORY);
@@ -67,7 +102,7 @@ index dc21f74..d4b35c4 100644
67102
return packetId;
68103
}
69104

70-
@@ -237,11 +243,13 @@ void MqttClient::loop() {
105+
@@ -237,11 +245,13 @@ void MqttClient::loop() {
71106
case State::connectingMqtt:
72107
#if EMC_WAIT_FOR_CONNACK
73108
if (_transport->connected()) {
@@ -76,7 +111,7 @@ index dc21f74..d4b35c4 100644
76111
- _checkIncoming();
77112
- _checkPing();
78113
- EMC_SEMAPHORE_GIVE();
79-
+ if(pdTRUE == EMC_SEMAPHORE_TAKE()) {
114+
+ EMC_SEMAPHORE_TAKE_CHECK() {
80115
+ _sendPacket();
81116
+ _checkIncoming();
82117
+ _checkPing();

0 commit comments

Comments
 (0)