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
1
34
diff --git a/src/MqttClient.cpp b/src/MqttClient.cpp
2
- index dc21f74..d4b35c4 100644
35
+ index dc21f74..d524e50 100644
3
36
--- a/src/MqttClient.cpp
4
37
+++ b/src/MqttClient.cpp
5
38
@@ -1,7 +1,7 @@
@@ -11,7 +44,7 @@ index dc21f74..d4b35c4 100644
11
44
For a copy, see <https://opensource.org/licenses/MIT> or
12
45
the LICENSE file.
13
46
*/
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
15
48
#endif
16
49
return 0;
17
50
}
@@ -20,14 +53,15 @@ index dc21f74..d4b35c4 100644
20
53
- if (!_addPacket(packetId, topic, payload, length, qos, retain)) {
21
54
- emc_log_e("Could not create PUBLISH packet");
22
55
+ uint16_t packetId = 0;
23
- + if(pdTRUE == EMC_SEMAPHORE_TAKE() ) {
56
+ + EMC_SEMAPHORE_TAKE_CHECK( ) {
24
57
+ packetId = (qos > 0) ? _getNextPacketId() : 1;
25
58
+ if (!_addPacket(packetId, topic, payload, length, qos, retain)) {
26
59
+ emc_log_e("Could not create PUBLISH packet");
27
60
+ EMC_SEMAPHORE_GIVE();
28
61
+ _onError(packetId, Error::OUT_OF_MEMORY);
29
- + if(pdTRUE == EMC_SEMAPHORE_TAKE())
62
+ + EMC_SEMAPHORE_TAKE_CHECK() {
30
63
+ packetId = 0;
64
+ + }
31
65
+ }
32
66
EMC_SEMAPHORE_GIVE();
33
67
- _onError(packetId, Error::OUT_OF_MEMORY);
@@ -39,7 +73,7 @@ index dc21f74..d4b35c4 100644
39
73
return packetId;
40
74
}
41
75
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
43
77
#endif
44
78
return 0;
45
79
}
@@ -48,14 +82,15 @@ index dc21f74..d4b35c4 100644
48
82
- if (!_addPacket(packetId, topic, callback, length, qos, retain)) {
49
83
- emc_log_e("Could not create PUBLISH packet");
50
84
+ uint16_t packetId = 0;
51
- + if(pdTRUE == EMC_SEMAPHORE_TAKE() ) {
85
+ + EMC_SEMAPHORE_TAKE_CHECK( ) {
52
86
+ packetId = (qos > 0) ? _getNextPacketId() : 1;
53
87
+ if (!_addPacket(packetId, topic, callback, length, qos, retain)) {
54
88
+ emc_log_e("Could not create PUBLISH packet");
55
89
+ EMC_SEMAPHORE_GIVE();
56
90
+ _onError(packetId, Error::OUT_OF_MEMORY);
57
- + if(pdTRUE == EMC_SEMAPHORE_TAKE())
91
+ + EMC_SEMAPHORE_TAKE_CHECK() {
58
92
+ packetId = 0;
93
+ + }
59
94
+ }
60
95
EMC_SEMAPHORE_GIVE();
61
96
- _onError(packetId, Error::OUT_OF_MEMORY);
@@ -67,7 +102,7 @@ index dc21f74..d4b35c4 100644
67
102
return packetId;
68
103
}
69
104
70
- @@ -237,11 +243 ,13 @@ void MqttClient::loop() {
105
+ @@ -237,11 +245 ,13 @@ void MqttClient::loop() {
71
106
case State::connectingMqtt:
72
107
#if EMC_WAIT_FOR_CONNACK
73
108
if (_transport->connected()) {
@@ -76,7 +111,7 @@ index dc21f74..d4b35c4 100644
76
111
- _checkIncoming();
77
112
- _checkPing();
78
113
- EMC_SEMAPHORE_GIVE();
79
- + if(pdTRUE == EMC_SEMAPHORE_TAKE() ) {
114
+ + EMC_SEMAPHORE_TAKE_CHECK( ) {
80
115
+ _sendPacket();
81
116
+ _checkIncoming();
82
117
+ _checkPing();
0 commit comments