Skip to content

Commit

Permalink
mqtt: MQTT_BUFFER_MAX_SIZE
Browse files Browse the repository at this point in the history
  • Loading branch information
mcspr committed Mar 15, 2020
1 parent 6c44fbb commit 067cf20
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 24 deletions.
12 changes: 12 additions & 0 deletions code/espurna/config/dependencies.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@
#endif

#if THERMOSTAT_SUPPORT
#undef MQTT_USE_JSON
#define MQTT_USE_JSON 1 // Thermostat depends on group messages in a JSON body
#undef RELAY_SUPPORT
#define RELAY_SUPPORT 1 // Thermostat depends on switches
#endif
Expand Down Expand Up @@ -195,3 +197,13 @@
#undef BUTTON1_LNGCLICK
#define BUTTON1_LNGCLICK BUTTON_ACTION_TOGGLE
#endif

//------------------------------------------------------------------------------
// We should always set MQTT_MAX_PACKET_SIZE
//

#if MQTT_LIBRARY == MQTT_LIBRARY_PUBSUBCLIENT
#if not defined(MQTT_MAX_PACKET_SIZE)
#warning "MQTT_MAX_PACKET_SIZE should be set in `build_flags = ...` of the environment! Default value is used instead."
#endif
#endif
15 changes: 6 additions & 9 deletions code/espurna/config/general.h
Original file line number Diff line number Diff line change
Expand Up @@ -1079,15 +1079,8 @@
#define MQTT_SKIP_TIME 1000 // Skip messages for 1 second anter connection
#endif


#if THERMOSTAT_SUPPORT == 1
#ifndef MQTT_USE_JSON
#define MQTT_USE_JSON 1 // Group messages in a JSON body
#endif
#else
#ifndef MQTT_USE_JSON
#define MQTT_USE_JSON 0 // Don't group messages in a JSON body (default)
#endif
#ifndef MQTT_USE_JSON
#define MQTT_USE_JSON 0 // Don't group messages in a JSON body by default
#endif

#ifndef MQTT_USE_JSON_DELAY
Expand All @@ -1098,6 +1091,10 @@
#define MQTT_QUEUE_MAX_SIZE 20 // Size of the MQTT queue when MQTT_USE_JSON is enabled
#endif

#ifndef MQTT_BUFFER_MAX_SIZE
#define MQTT_BUFFER_MAX_SIZE 1024 // Size of the MQTT payload buffer for MQTT_MESSAGE_EVENT. Large messages will only be available via MQTT_MESSAGE_RAW_EVENT.
// Note: When using MQTT_LIBRARY_PUBSUBCLIENT, MQTT_MAX_PACKET_SIZE should not be more than this value.
#endif

// These are the properties that will be sent when useJson is true
#ifndef MQTT_ENQUEUE_IP
Expand Down
28 changes: 14 additions & 14 deletions code/espurna/mqtt.ino
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ Updated secure client support by Niek van der Maas < mail at niekvandermaas dot
#endif // SECURE_CLIENT != SECURE_CLIENT_NONE

#if MQTT_LIBRARY == MQTT_LIBRARY_ARDUINOMQTT
#ifdef MQTT_MAX_PACKET_SIZE
MQTTClient _mqtt(MQTT_MAX_PACKET_SIZE);
#else
MQTTClient _mqtt;
#endif

MQTTClient _mqtt(MQTT_BUFFER_MAX_SIZE);

#elif MQTT_LIBRARY == MQTT_LIBRARY_PUBSUBCLIENT
PubSubClient _mqtt;

PubSubClient _mqtt;

#endif

#endif // MQTT_LIBRARY == MQTT_ASYNCMQTTCLIENT
Expand Down Expand Up @@ -427,7 +427,7 @@ void _mqttInfo() {
#else
"DISABLED"
#endif
", Buffer size " _MQTT_INFO_STR(MQTT_MAX_PACKET_SIZE) " bytes"
", Buffer size " _MQTT_INFO_STR(MQTT_BUFFER_MAX_SIZE) " bytes"
"\n"
));
#undef _MQTT_INFO_STR
Expand Down Expand Up @@ -620,18 +620,18 @@ bool _mqttMaybeSkipRetained(char* topic) {

#if MQTT_LIBRARY == MQTT_LIBRARY_ASYNCMQTTCLIENT

// MQTT Broker can sometimes send messages in bulk. Even when message size is less than MQTT_MAX_PACKET_SIZE, we *could*
// MQTT Broker can sometimes send messages in bulk. Even when message size is less than MQTT_BUFFER_MAX_SIZE, we *could*
// receive a message with `len != total`, this requiring buffering of the received data. Prepare a static memory to store the
// data until `(len + index) == total`.
// TODO: One pending issue is streaming arbitrary data (e.g. binary, for OTA). We always set '\0' and expect text data.
// In that case, there could be MQTT_MESSAGE_PARTIAL_EVENT and this callback only trigger on small messages.
// TODO: One pending issue is streaming arbitrary data (e.g. binary, for OTA). We always set '\0' and API consumer expects C-String.
// In that case, there could be MQTT_MESSAGE_RAW_EVENT and this callback only trigger on small messages.

void _mqttOnMessageAsync(char* topic, char* payload, AsyncMqttClientMessageProperties, size_t len, size_t index, size_t total) {

if (!len || (len >= MQTT_MAX_PACKET_SIZE) || (total >= MQTT_MAX_PACKET_SIZE)) return;
if (!len || (len > MQTT_BUFFER_MAX_SIZE) || (total > MQTT_BUFFER_MAX_SIZE)) return;
if (_mqttMaybeSkipRetained(topic)) return;

static char message[MQTT_MAX_PACKET_SIZE] = {0};
static char message[((MQTT_BUFFER_MAX_SIZE + 1) + 31) & -32] = {0};
memmove(message + index, (char *) payload, len);

// Not done yet
Expand All @@ -654,10 +654,10 @@ void _mqttOnMessageAsync(char* topic, char* payload, AsyncMqttClientMessagePrope

void _mqttOnMessage(char* topic, char* payload, unsigned int len) {

if (!len || (len >= MQTT_MAX_PACKET_SIZE)) return;
if (!len || (len > MQTT_BUFFER_MAX_SIZE)) return;
if (_mqttMaybeSkipRetained(topic)) return;

static char message[MQTT_MAX_PACKET_SIZE] = {0};
static char message[((MQTT_BUFFER_MAX_SIZE + 1) + 31) & -32] = {0};
memmove(message, (char *) payload, len);
message[len] = '\0';

Expand Down
2 changes: 1 addition & 1 deletion code/platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ board_1m = esp01_1m
board_2m = esp_wroom_02
board_4m = esp12e

build_flags = -g -w -DMQTT_MAX_PACKET_SIZE=1024 -DNO_GLOBAL_EEPROM -DPIO_FRAMEWORK_ARDUINO_LWIP2_HIGHER_BANDWIDTH
build_flags = -g -w -DNO_GLOBAL_EEPROM -DPIO_FRAMEWORK_ARDUINO_LWIP2_HIGHER_BANDWIDTH
build_flags_512k = ${common.build_flags} -Wl,-Teagle.flash.512k0m1s.ld
build_flags_1m0m = ${common.build_flags} -Wl,-Teagle.flash.1m0m1s.ld
build_flags_2m1m = ${common.build_flags} -Wl,-Teagle.flash.2m1m4s.ld
Expand Down

0 comments on commit 067cf20

Please sign in to comment.