diff --git a/README.md b/README.md index 950015db..5eff0ec4 100644 --- a/README.md +++ b/README.md @@ -401,7 +401,7 @@ class Custom_API_Implementation : public IAPI_Implementation { // Nothing to do } - void Set_Client_Callbacks(Callback::function subscribe_api_callback, Callback::function send_json_callback, Callback::function send_json_string_callback, Callback::function subscribe_topic_callback, Callback::function unsubscribe_topic_callback, Callback::function get_size_callback, Callback::function set_buffer_size_callback, Callback::function get_request_id_callback) override { + void Set_Client_Callbacks(Callback::function subscribe_api_callback, Callback::function send_json_callback, Callback::function send_json_string_callback, Callback::function subscribe_topic_callback, Callback::function unsubscribe_topic_callback, Callback::function get_receive_size_callback, Callback::function get_send_size_callback, Callback::function set_buffer_size_callback, Callback::function get_request_id_callback) override { // Nothing to do } }; diff --git a/examples/0000-arduino_send_telemetry/0000-arduino_send_telemetry.ino b/examples/0000-arduino_send_telemetry/0000-arduino_send_telemetry.ino index 74282f37..ee94d59a 100644 --- a/examples/0000-arduino_send_telemetry/0000-arduino_send_telemetry.ino +++ b/examples/0000-arduino_send_telemetry/0000-arduino_send_telemetry.ino @@ -28,7 +28,8 @@ constexpr uint16_t THINGSBOARD_PORT = 1883U; // Maximum size packets will ever be sent or received by the underlying MQTT client, // if the size is to small messages might not be sent or received messages will be discarded -constexpr uint16_t MAX_MESSAGE_SIZE = 128U; +constexpr uint16_t MAX_MESSAGE_SEND_SIZE = 128U; +constexpr uint16_t MAX_MESSAGE_RECEIVE_SIZE = 128U; // Baud rate for the debugging serial connection // If the Serial output is mangled, ensure to change the monitor speed accordingly to this variable @@ -46,8 +47,8 @@ SoftwareSerial soft(2U, 3U); // RX, TX WiFiEspClient espClient; // Initalize the Mqtt client instance Arduino_MQTT_Client mqttClient(espClient); -// Initialize ThingsBoard instance with the maximum needed buffer size -ThingsBoard tb(mqttClient, MAX_MESSAGE_SIZE); +// Initialize ThingsBoard instance with the maximum needed buffer sizes +ThingsBoard tb(mqttClient, MAX_MESSAGE_RECEIVE_SIZE, MAX_MESSAGE_SEND_SIZE); /// @brief Initalizes WiFi connection, diff --git a/examples/0001-arduino_send_batch/0001-arduino_send_batch.ino b/examples/0001-arduino_send_batch/0001-arduino_send_batch.ino index abdb22d2..4aa0c062 100644 --- a/examples/0001-arduino_send_batch/0001-arduino_send_batch.ino +++ b/examples/0001-arduino_send_batch/0001-arduino_send_batch.ino @@ -28,7 +28,8 @@ constexpr uint16_t THINGSBOARD_PORT = 1883U; // Maximum size packets will ever be sent or received by the underlying MQTT client, // if the size is to small messages might not be sent or received messages will be discarded -constexpr uint16_t MAX_MESSAGE_SIZE = 128U; +constexpr uint16_t MAX_MESSAGE_SEND_SIZE = 128U; +constexpr uint16_t MAX_MESSAGE_RECEIVE_SIZE = 128U; // Baud rate for the debugging serial connection // If the Serial output is mangled, ensure to change the monitor speed accordingly to this variable @@ -49,8 +50,8 @@ SoftwareSerial soft(2U, 3U); // RX, TX WiFiEspClient espClient; // Initalize the Mqtt client instance Arduino_MQTT_Client mqttClient(espClient); -// Initialize ThingsBoard instance with the maximum needed buffer size -ThingsBoard tb(mqttClient, MAX_MESSAGE_SIZE); +// Initialize ThingsBoard instance with the maximum needed buffer sizes +ThingsBoard tb(mqttClient, MAX_MESSAGE_RECEIVE_SIZE, MAX_MESSAGE_SEND_SIZE); /// @brief Initalizes WiFi connection, diff --git a/examples/0002-arduino_rpc/0002-arduino_rpc.ino b/examples/0002-arduino_rpc/0002-arduino_rpc.ino index f6851001..d93e7ad3 100644 --- a/examples/0002-arduino_rpc/0002-arduino_rpc.ino +++ b/examples/0002-arduino_rpc/0002-arduino_rpc.ino @@ -29,7 +29,8 @@ constexpr uint16_t THINGSBOARD_PORT = 1883U; // Maximum size packets will ever be sent or received by the underlying MQTT client, // if the size is to small messages might not be sent or received messages will be discarded -constexpr uint16_t MAX_MESSAGE_SIZE = 128U; +constexpr uint16_t MAX_MESSAGE_SEND_SIZE = 128U; +constexpr uint16_t MAX_MESSAGE_RECEIVE_SIZE = 128U; // Baud rate for the debugging serial connection // If the Serial output is mangled, ensure to change the monitor speed accordingly to this variable @@ -56,8 +57,9 @@ Server_Side_RPC rpc; IAPI_Implementation* apis[1U] = { &rpc }; -// Initialize ThingsBoard instance with the maximum needed buffer size -ThingsBoard tb(mqttClient, MAX_MESSAGE_SIZE, Default_Max_Stack_Size, apis + 0U, apis + 1U); +// Initialize ThingsBoard instance with the maximum needed buffer sizes +ThingsBoard tb(mqttClient, MAX_MESSAGE_RECEIVE_SIZE, MAX_MESSAGE_SEND_SIZE, Default_Max_Stack_Size, apis + 0U, apis + 1U); + // Statuses for subscribing to rpc bool subscribed = false; diff --git a/examples/0003-esp8266_esp32_send_data/0003-esp8266_esp32_send_data.ino b/examples/0003-esp8266_esp32_send_data/0003-esp8266_esp32_send_data.ino index c31cf1c8..b46974c6 100644 --- a/examples/0003-esp8266_esp32_send_data/0003-esp8266_esp32_send_data.ino +++ b/examples/0003-esp8266_esp32_send_data/0003-esp8266_esp32_send_data.ino @@ -70,7 +70,8 @@ constexpr uint16_t THINGSBOARD_PORT = 1883U; // Maximum size packets will ever be sent or received by the underlying MQTT client, // if the size is to small messages might not be sent or received messages will be discarded -constexpr uint16_t MAX_MESSAGE_SIZE = 128U; +constexpr uint16_t MAX_MESSAGE_SEND_SIZE = 128U; +constexpr uint16_t MAX_MESSAGE_RECEIVE_SIZE = 128U; // Baud rate for the debugging serial connection // If the Serial output is mangled, ensure to change the monitor speed accordingly to this variable @@ -136,7 +137,7 @@ Arduino_MQTT_Client mqttClient(espClient); // Initialize used apis const std::array apis = {}; // Initialize ThingsBoard instance with the maximum needed buffer size -ThingsBoard tb(mqttClient, MAX_MESSAGE_SIZE, Default_Max_Stack_Size, apis); +ThingsBoard tb(mqttClient, MAX_MESSAGE_RECEIVE_SIZE, MAX_MESSAGE_SEND_SIZE, Default_Max_Stack_Size, apis); #endif diff --git a/examples/0004-arduino-sim900_send_telemetry/0004-arduino-sim900_send_telemetry.ino b/examples/0004-arduino-sim900_send_telemetry/0004-arduino-sim900_send_telemetry.ino index b3eb5551..4dd95228 100644 --- a/examples/0004-arduino-sim900_send_telemetry/0004-arduino-sim900_send_telemetry.ino +++ b/examples/0004-arduino-sim900_send_telemetry/0004-arduino-sim900_send_telemetry.ino @@ -41,7 +41,8 @@ constexpr uint16_t THINGSBOARD_PORT = 1883U; // Maximum size packets will ever be sent or received by the underlying MQTT client, // if the size is to small messages might not be sent or received messages will be discarded -constexpr uint16_t MAX_MESSAGE_SIZE = 128U; +constexpr uint16_t MAX_MESSAGE_SEND_SIZE = 128U; +constexpr uint16_t MAX_MESSAGE_RECEIVE_SIZE = 128U; // Baud rate for the debugging serial connection // If the Serial output is mangled, ensure to change the monitor speed accordingly to this variable @@ -65,7 +66,7 @@ TinyGsmClient client(modem); Arduino_MQTT_Client mqttClient(client); // Initialize ThingsBoard instance with the maximum needed buffer size -ThingsBoard tb(mqttClient, MAX_MESSAGE_SIZE); +ThingsBoard tb(mqttClient, MAX_MESSAGE_RECEIVE_SIZE, MAX_MESSAGE_SEND_SIZE); // Set to true, if modem is connected bool modemConnected = false; diff --git a/examples/0006-esp8266_esp32_process_shared_attribute_update/0006-esp8266_esp32_process_shared_attribute_update.ino b/examples/0006-esp8266_esp32_process_shared_attribute_update/0006-esp8266_esp32_process_shared_attribute_update.ino index c3c628dd..68090197 100644 --- a/examples/0006-esp8266_esp32_process_shared_attribute_update/0006-esp8266_esp32_process_shared_attribute_update.ino +++ b/examples/0006-esp8266_esp32_process_shared_attribute_update/0006-esp8266_esp32_process_shared_attribute_update.ino @@ -39,7 +39,8 @@ constexpr uint16_t THINGSBOARD_PORT = 1883U; // Maximum size packets will ever be sent or received by the underlying MQTT client, // if the size is to small messages might not be sent or received messages will be discarded -constexpr uint16_t MAX_MESSAGE_SIZE = 128U; +constexpr uint16_t MAX_MESSAGE_SEND_SIZE = 128U; +constexpr uint16_t MAX_MESSAGE_RECEIVE_SIZE = 128U; // Baud rate for the debugging serial connection // If the Serial output is mangled, ensure to change the monitor speed accordingly to this variable @@ -110,7 +111,7 @@ const std::array apis = { &shared_update }; // Initialize ThingsBoard instance with the maximum needed buffer size -ThingsBoard tb(mqttClient, MAX_MESSAGE_SIZE, Default_Max_Stack_Size, apis); +ThingsBoard tb(mqttClient, MAX_MESSAGE_RECEIVE_SIZE, MAX_MESSAGE_SEND_SIZE, Default_Max_Stack_Size, apis); // Statuses for subscribing to shared attributes bool subscribed = false; diff --git a/examples/0007-esp8266_esp32_claim_device/0007-esp8266_esp32_claim_device.ino b/examples/0007-esp8266_esp32_claim_device/0007-esp8266_esp32_claim_device.ino index 9eaf6163..60d1e593 100644 --- a/examples/0007-esp8266_esp32_claim_device/0007-esp8266_esp32_claim_device.ino +++ b/examples/0007-esp8266_esp32_claim_device/0007-esp8266_esp32_claim_device.ino @@ -43,7 +43,8 @@ constexpr uint16_t THINGSBOARD_PORT = 1883U; // Maximum size packets will ever be sent or received by the underlying MQTT client, // if the size is to small messages might not be sent or received messages will be discarded -constexpr uint16_t MAX_MESSAGE_SIZE = 128U; +constexpr uint16_t MAX_MESSAGE_SEND_SIZE = 128U; +constexpr uint16_t MAX_MESSAGE_RECEIVE_SIZE = 128U; // Baud rate for the debugging serial connection // If the Serial output is mangled, ensure to change the monitor speed accordingly to this variable @@ -108,7 +109,7 @@ WiFiClient espClient; // Initalize the Mqtt client instance Arduino_MQTT_Client mqttClient(espClient); // Initialize ThingsBoard instance with the maximum needed buffer size -ThingsBoard tb(mqttClient, MAX_MESSAGE_SIZE); +ThingsBoard tb(mqttClient, MAX_MESSAGE_RECEIVE_SIZE, MAX_MESSAGE_SEND_SIZE); // Statuses for claiming bool claimingRequestSent = false; diff --git a/examples/0008-esp8266_esp32_provision_device/0008-esp8266_esp32_provision_device.ino b/examples/0008-esp8266_esp32_provision_device/0008-esp8266_esp32_provision_device.ino index 5b3acd39..83fb8f5b 100644 --- a/examples/0008-esp8266_esp32_provision_device/0008-esp8266_esp32_provision_device.ino +++ b/examples/0008-esp8266_esp32_provision_device/0008-esp8266_esp32_provision_device.ino @@ -39,7 +39,8 @@ constexpr uint16_t THINGSBOARD_PORT = 1883U; // Maximum size packets will ever be sent or received by the underlying MQTT client, // if the size is to small messages might not be sent or received messages will be discarded -constexpr uint16_t MAX_MESSAGE_SIZE = 256U; +constexpr uint16_t MAX_MESSAGE_SEND_SIZE = 256U; +constexpr uint16_t MAX_MESSAGE_RECEIVE_SIZE = 256U; // Baud rate for the debugging serial connection // If the Serial output is mangled, ensure to change the monitor speed accordingly to this variable @@ -120,7 +121,7 @@ const std::array apis = { &prov }; // Initialize ThingsBoard instance with the maximum needed buffer size -ThingsBoard tb(mqttClient, MAX_MESSAGE_SIZE, Default_Max_Stack_Size, apis); +ThingsBoard tb(mqttClient, MAX_MESSAGE_RECEIVE_SIZE, MAX_MESSAGE_SEND_SIZE, Default_Max_Stack_Size, apis); uint32_t previous_processing_time = 0U; diff --git a/examples/0009-esp8266_esp32_process_OTA_MQTT/0009-esp8266_esp32_process_OTA_MQTT.ino b/examples/0009-esp8266_esp32_process_OTA_MQTT/0009-esp8266_esp32_process_OTA_MQTT.ino index 7717edc5..a5f45526 100644 --- a/examples/0009-esp8266_esp32_process_OTA_MQTT/0009-esp8266_esp32_process_OTA_MQTT.ino +++ b/examples/0009-esp8266_esp32_process_OTA_MQTT/0009-esp8266_esp32_process_OTA_MQTT.ino @@ -68,7 +68,8 @@ constexpr uint16_t THINGSBOARD_PORT = 1883U; // Maximum size packets will ever be sent or received by the underlying MQTT client, // if the size is to small messages might not be sent or received messages will be discarded -constexpr uint16_t MAX_MESSAGE_SIZE = 512U; +constexpr uint16_t MAX_MESSAGE_SEND_SIZE = 512U; +constexpr uint16_t MAX_MESSAGE_RECEIVE_SIZE = 512U; // Baud rate for the debugging serial connection // If the Serial output is mangled, ensure to change the monitor speed accordingly to this variable @@ -127,7 +128,7 @@ const std::array apis = { &ota }; // Initialize ThingsBoard instance with the maximum needed buffer size -ThingsBoard tb(mqttClient, MAX_MESSAGE_SIZE, Default_Max_Stack_Size, apis); +ThingsBoard tb(mqttClient, MAX_MESSAGE_RECEIVE_SIZE, MAX_MESSAGE_SEND_SIZE, Default_Max_Stack_Size, apis); // Initalize the Updater client instance used to flash binary to flash memory #ifdef ESP8266 Arduino_ESP8266_Updater updater; diff --git a/examples/0010-esp8266_esp32_rpc/0010-esp8266_esp32_rpc.ino b/examples/0010-esp8266_esp32_rpc/0010-esp8266_esp32_rpc.ino index f2dc26b4..11bedec3 100644 --- a/examples/0010-esp8266_esp32_rpc/0010-esp8266_esp32_rpc.ino +++ b/examples/0010-esp8266_esp32_rpc/0010-esp8266_esp32_rpc.ino @@ -39,7 +39,8 @@ constexpr uint16_t THINGSBOARD_PORT = 1883U; // Maximum size packets will ever be sent or received by the underlying MQTT client, // if the size is to small messages might not be sent or received messages will be discarded -constexpr uint16_t MAX_MESSAGE_SIZE = 256U; +constexpr uint16_t MAX_MESSAGE_SEND_SIZE = 256U; +constexpr uint16_t MAX_MESSAGE_RECEIVE_SIZE = 256U; // Baud rate for the debugging serial connection. // If the Serial output is mangled, ensure to change the monitor speed accordingly to this variable @@ -106,7 +107,7 @@ const std::array apis = { &rpc }; // Initialize ThingsBoard instance with the maximum needed buffer size -ThingsBoard tb(mqttClient, MAX_MESSAGE_SIZE, Default_Max_Stack_Size, apis); +ThingsBoard tb(mqttClient, MAX_MESSAGE_RECEIVE_SIZE, MAX_MESSAGE_SEND_SIZE, Default_Max_Stack_Size, apis); // Statuses for subscribing to rpc bool subscribed = false; diff --git a/examples/0011-esp8266_esp32_subscribe_OTA_MQTT/0011-esp8266_esp32_subscribe_OTA_MQTT.ino b/examples/0011-esp8266_esp32_subscribe_OTA_MQTT/0011-esp8266_esp32_subscribe_OTA_MQTT.ino index 4f4b0aba..1a211453 100644 --- a/examples/0011-esp8266_esp32_subscribe_OTA_MQTT/0011-esp8266_esp32_subscribe_OTA_MQTT.ino +++ b/examples/0011-esp8266_esp32_subscribe_OTA_MQTT/0011-esp8266_esp32_subscribe_OTA_MQTT.ino @@ -63,7 +63,8 @@ constexpr uint16_t THINGSBOARD_PORT = 1883U; // Maximum size packets will ever be sent or received by the underlying MQTT client, // if the size is to small messages might not be sent or received messages will be discarded -constexpr uint16_t MAX_MESSAGE_SIZE = 512U; +constexpr uint16_t MAX_MESSAGE_SEND_SIZE = 512U; +constexpr uint16_t MAX_MESSAGE_RECEIVE_SIZE = 512U; // Baud rate for the debugging serial connection // If the Serial output is mangled, ensure to change the monitor speed accordingly to this variable @@ -122,7 +123,7 @@ const std::array apis = { &ota }; // Initialize ThingsBoard instance with the maximum needed buffer size -ThingsBoard tb(mqttClient, MAX_MESSAGE_SIZE, Default_Max_Stack_Size, apis); +ThingsBoard tb(mqttClient, MAX_MESSAGE_RECEIVE_SIZE, MAX_MESSAGE_SEND_SIZE, Default_Max_Stack_Size, apis); // Initalize the Updater client instance used to flash binary to flash memory #ifdef ESP8266 Arduino_ESP8266_Updater updater; diff --git a/examples/0012-esp8266_esp32_request_shared_attribute/0012-esp8266_esp32_request_shared_attribute.ino b/examples/0012-esp8266_esp32_request_shared_attribute/0012-esp8266_esp32_request_shared_attribute.ino index 14aebeac..1f384f66 100644 --- a/examples/0012-esp8266_esp32_request_shared_attribute/0012-esp8266_esp32_request_shared_attribute.ino +++ b/examples/0012-esp8266_esp32_request_shared_attribute/0012-esp8266_esp32_request_shared_attribute.ino @@ -39,7 +39,8 @@ constexpr uint16_t THINGSBOARD_PORT = 1883U; // Maximum size packets will ever be sent or received by the underlying MQTT client, // if the size is to small messages might not be sent or received messages will be discarded -constexpr uint16_t MAX_MESSAGE_SIZE = 256U; +constexpr uint16_t MAX_MESSAGE_SEND_SIZE = 256U; +constexpr uint16_t MAX_MESSAGE_RECEIVE_SIZE = 256U; // Baud rate for the debugging serial connection // If the Serial output is mangled, ensure to change the monitor speed accordingly to this variable @@ -111,7 +112,7 @@ const std::array apis = { &attr_request }; // Initialize ThingsBoard instance with the maximum needed buffer size -ThingsBoard tb(mqttClient, MAX_MESSAGE_SIZE, Default_Max_Stack_Size, apis); +ThingsBoard tb(mqttClient, MAX_MESSAGE_RECEIVE_SIZE, MAX_MESSAGE_SEND_SIZE, Default_Max_Stack_Size, apis); // Statuses for requesting of attributes bool requestedClient = false; diff --git a/examples/0013-esp8266_esp32_request_rpc/0013-esp8266_esp32_request_rpc.ino b/examples/0013-esp8266_esp32_request_rpc/0013-esp8266_esp32_request_rpc.ino index f1c0261f..430a1da4 100644 --- a/examples/0013-esp8266_esp32_request_rpc/0013-esp8266_esp32_request_rpc.ino +++ b/examples/0013-esp8266_esp32_request_rpc/0013-esp8266_esp32_request_rpc.ino @@ -39,7 +39,8 @@ constexpr uint16_t THINGSBOARD_PORT = 1883U; // Maximum size packets will ever be sent or received by the underlying MQTT client, // if the size is to small messages might not be sent or received messages will be discarded -constexpr uint16_t MAX_MESSAGE_SIZE = 256U; +constexpr uint16_t MAX_MESSAGE_SEND_SIZE = 256U; +constexpr uint16_t MAX_MESSAGE_RECEIVE_SIZE = 256U; // Baud rate for the debugging serial connection. // If the Serial output is mangled, ensure to change the monitor speed accordingly to this variable @@ -103,7 +104,7 @@ const std::array apis = { &rpc_request }; // Initialize ThingsBoard instance with the maximum needed buffer size -ThingsBoard tb(mqttClient, MAX_MESSAGE_SIZE, Default_Max_Stack_Size, apis); +ThingsBoard tb(mqttClient, MAX_MESSAGE_RECEIVE_SIZE, MAX_MESSAGE_SEND_SIZE, Default_Max_Stack_Size, apis); // Statuses for subscribing to rpc bool subscribed = false; diff --git a/examples/0014-espressif_esp32_send_data/main/0014-espressif_esp32_send_data.cpp b/examples/0014-espressif_esp32_send_data/main/0014-espressif_esp32_send_data.cpp index 5559c6aa..c8c21484 100644 --- a/examples/0014-espressif_esp32_send_data/main/0014-espressif_esp32_send_data.cpp +++ b/examples/0014-espressif_esp32_send_data/main/0014-espressif_esp32_send_data.cpp @@ -38,7 +38,8 @@ constexpr uint16_t THINGSBOARD_PORT = 1883U; // Maximum size packets will ever be sent or received by the underlying MQTT client, // if the size is to small messages might not be sent or received messages will be discarded -constexpr uint16_t MAX_MESSAGE_SIZE = 128U; +constexpr uint16_t MAX_MESSAGE_SEND_SIZE = 128U; +constexpr uint16_t MAX_MESSAGE_RECEIVE_SIZE = 128U; #if ENCRYPTED // See https://comodosslstore.com/resources/what-is-a-root-ca-certificate-and-how-do-i-download-it/ @@ -85,7 +86,7 @@ constexpr char HUMIDITY_KEY[] = "humidity"; // Initalize the Mqtt client instance Espressif_MQTT_Client<> mqttClient; // Initialize ThingsBoard instance with the maximum needed buffer size -ThingsBoard tb(mqttClient, MAX_MESSAGE_SIZE); +ThingsBoard tb(mqttClient, MAX_MESSAGE_RECEIVE_SIZE, MAX_MESSAGE_SEND_SIZE); // Status for successfully connecting to the given WiFi bool wifi_connected = false; diff --git a/examples/0015-espressif_esp32_process_OTA_MQTT/main/0015-espressif_esp32_process_OTA_MQTT.cpp b/examples/0015-espressif_esp32_process_OTA_MQTT/main/0015-espressif_esp32_process_OTA_MQTT.cpp index 83565465..ed32077c 100644 --- a/examples/0015-espressif_esp32_process_OTA_MQTT/main/0015-espressif_esp32_process_OTA_MQTT.cpp +++ b/examples/0015-espressif_esp32_process_OTA_MQTT/main/0015-espressif_esp32_process_OTA_MQTT.cpp @@ -57,7 +57,8 @@ constexpr uint16_t THINGSBOARD_PORT = 1883U; // and a little bit more for the topic we received the message on. // This has to be done at least until the issue https://github.com/espressif/esp-mqtt/issues/267 has been fixed in the esp-mqtt client, // or if an older version of the esp-mqtt client is used that does not include the possible fixes to the aforementioned issue yet. -constexpr uint16_t MAX_MESSAGE_SIZE = FIRMWARE_PACKET_SIZE + 50U; +constexpr uint16_t MAX_MESSAGE_SEND_SIZE = FIRMWARE_PACKET_SIZE + 50U; +constexpr uint16_t MAX_MESSAGE_RECEIVE_SIZE = FIRMWARE_PACKET_SIZE + 50U; #if ENCRYPTED // See https://comodosslstore.com/resources/what-is-a-root-ca-certificate-and-how-do-i-download-it/ @@ -108,7 +109,7 @@ const std::array apis = { &ota }; // Initialize ThingsBoard instance with the maximum needed buffer size -ThingsBoard tb(mqttClient, MAX_MESSAGE_SIZE, Default_Max_Stack_Size, apis); +ThingsBoard tb(mqttClient, MAX_MESSAGE_RECEIVE_SIZE, MAX_MESSAGE_SEND_SIZE, Default_Max_Stack_Size, apis); // Initalize the Updater client instance used to flash binary to flash memory SDCard_Updater<> updater(UPDAT_FILE_PATH); diff --git a/examples/0016-espressif_esp32_rpc/main/0016-espressif_esp32_rpc.cpp b/examples/0016-espressif_esp32_rpc/main/0016-espressif_esp32_rpc.cpp index 2108f7b1..a804d24d 100644 --- a/examples/0016-espressif_esp32_rpc/main/0016-espressif_esp32_rpc.cpp +++ b/examples/0016-espressif_esp32_rpc/main/0016-espressif_esp32_rpc.cpp @@ -37,7 +37,8 @@ constexpr uint16_t THINGSBOARD_PORT = 1883U; // Maximum size packets will ever be sent or received by the underlying MQTT client, // if the size is to small messages might not be sent or received messages will be discarded -constexpr uint16_t MAX_MESSAGE_SIZE = 256U; +constexpr uint16_t MAX_MESSAGE_SEND_SIZE = 256U; +constexpr uint16_t MAX_MESSAGE_RECEIVE_SIZE = 256U; #if ENCRYPTED // See https://comodosslstore.com/resources/what-is-a-root-ca-certificate-and-how-do-i-download-it/ @@ -94,7 +95,7 @@ const std::array apis = { &rpc }; // Initialize ThingsBoard instance with the maximum needed buffer size -ThingsBoard tb(mqttClient, MAX_MESSAGE_SIZE, Default_Max_Stack_Size, apis); +ThingsBoard tb(mqttClient, MAX_MESSAGE_RECEIVE_SIZE, MAX_MESSAGE_SEND_SIZE, Default_Max_Stack_Size, apis); // Status for successfully connecting to the given WiFi bool wifi_connected = false; diff --git a/examples/0017-espressif_esp32_process_shared_attribute_update/main/0017-espressif_esp32_process_shared_attribute_update.cpp b/examples/0017-espressif_esp32_process_shared_attribute_update/main/0017-espressif_esp32_process_shared_attribute_update.cpp index 04593c29..f4232e0b 100644 --- a/examples/0017-espressif_esp32_process_shared_attribute_update/main/0017-espressif_esp32_process_shared_attribute_update.cpp +++ b/examples/0017-espressif_esp32_process_shared_attribute_update/main/0017-espressif_esp32_process_shared_attribute_update.cpp @@ -37,7 +37,8 @@ constexpr uint16_t THINGSBOARD_PORT = 1883U; // Maximum size packets will ever be sent or received by the underlying MQTT client, // if the size is to small messages might not be sent or received messages will be discarded -constexpr uint16_t MAX_MESSAGE_SIZE = 128U; +constexpr uint16_t MAX_MESSAGE_SEND_SIZE = 128U; +constexpr uint16_t MAX_MESSAGE_RECEIVE_SIZE = 128U; // Maximum amount of attributs we can request or subscribe, has to be set both in the ThingsBoard template list and Attribute_Request_Callback template list // and should be the same as the amount of variables in the passed array. If it is less not all variables will be requested or subscribed @@ -96,7 +97,7 @@ const std::array apis = { &shared_update }; // Initialize ThingsBoard instance with the maximum needed buffer size -ThingsBoard tb(mqttClient, MAX_MESSAGE_SIZE, Default_Max_Stack_Size, apis); +ThingsBoard tb(mqttClient, MAX_MESSAGE_RECEIVE_SIZE, MAX_MESSAGE_SEND_SIZE, Default_Max_Stack_Size, apis); // Status for successfully connecting to the given WiFi bool wifi_connected = false; diff --git a/examples/0018-espressif_esp32_provision_device/main/0018-espressif_esp32_provision_device.cpp b/examples/0018-espressif_esp32_provision_device/main/0018-espressif_esp32_provision_device.cpp index 8f8aac11..f5914b72 100644 --- a/examples/0018-espressif_esp32_provision_device/main/0018-espressif_esp32_provision_device.cpp +++ b/examples/0018-espressif_esp32_provision_device/main/0018-espressif_esp32_provision_device.cpp @@ -42,7 +42,8 @@ constexpr uint16_t THINGSBOARD_PORT = 1883U; // Maximum size packets will ever be sent or received by the underlying MQTT client, // if the size is to small messages might not be sent or received messages will be discarded -constexpr uint16_t MAX_MESSAGE_SIZE = 256U; +constexpr uint16_t MAX_MESSAGE_SEND_SIZE = 256U; +constexpr uint16_t MAX_MESSAGE_RECEIVE_SIZE = 256U; #if ENCRYPTED // See https://comodosslstore.com/resources/what-is-a-root-ca-certificate-and-how-do-i-download-it/ @@ -113,7 +114,7 @@ const std::array apis = { &prov }; // Initialize ThingsBoard instance with the maximum needed buffer size -ThingsBoard tb(mqttClient, MAX_MESSAGE_SIZE, Default_Max_Stack_Size, apis); +ThingsBoard tb(mqttClient, MAX_MESSAGE_RECEIVE_SIZE, MAX_MESSAGE_SEND_SIZE, Default_Max_Stack_Size, apis); uint32_t previous_processing_time = 0U; diff --git a/examples/0019-esp8266_esp32_send_attributes/0019-esp8266_esp32_send_attributes.ino b/examples/0019-esp8266_esp32_send_attributes/0019-esp8266_esp32_send_attributes.ino index d9ca0947..939751cb 100644 --- a/examples/0019-esp8266_esp32_send_attributes/0019-esp8266_esp32_send_attributes.ino +++ b/examples/0019-esp8266_esp32_send_attributes/0019-esp8266_esp32_send_attributes.ino @@ -70,7 +70,8 @@ constexpr uint16_t THINGSBOARD_PORT = 1883U; // Maximum size packets will ever be sent or received by the underlying MQTT client, // if the size is to small messages might not be sent or received messages will be discarded -constexpr uint16_t MAX_MESSAGE_SIZE = 128U; +constexpr uint16_t MAX_MESSAGE_SEND_SIZE = 128U; +constexpr uint16_t MAX_MESSAGE_RECEIVE_SIZE = 128U; // Baud rate for the debugging serial connection // If the Serial output is mangled, ensure to change the monitor speed accordingly to this variable @@ -137,7 +138,7 @@ Arduino_MQTT_Client mqttClient(espClient); // Initialize used apis const std::array apis = {}; // Initialize ThingsBoard instance with the maximum needed buffer size -ThingsBoard tb(mqttClient, MAX_MESSAGE_SIZE, Default_Max_Stack_Size, apis); +ThingsBoard tb(mqttClient, MAX_MESSAGE_RECEIVE_SIZE, MAX_MESSAGE_SEND_SIZE, Default_Max_Stack_Size, apis); #endif diff --git a/src/Arduino_MQTT_Client.cpp b/src/Arduino_MQTT_Client.cpp index 8bc92df4..3afc7988 100644 --- a/src/Arduino_MQTT_Client.cpp +++ b/src/Arduino_MQTT_Client.cpp @@ -22,12 +22,16 @@ void Arduino_MQTT_Client::set_connect_callback(Callback::function callback m_connected_callback.Set_Callback(callback); } -bool Arduino_MQTT_Client::set_buffer_size(uint16_t buffer_size) { - return m_mqtt_client.setBufferSize(buffer_size); +bool Arduino_MQTT_Client::set_buffer_size(uint16_t receive_buffer_size, uint16_t send_buffer_size) { + return m_mqtt_client.setBufferSize(receive_buffer_size, send_buffer_size); } -uint16_t Arduino_MQTT_Client::get_buffer_size() { - return m_mqtt_client.getBufferSize(); +uint16_t Arduino_MQTT_Client::get_receive_buffer_size() { + return m_mqtt_client.getReceiveBufferSize(); +} + +uint16_t Arduino_MQTT_Client::get_send_buffer_size() { + return m_mqtt_client.getSendBufferSize(); } void Arduino_MQTT_Client::set_server(char const * domain, uint16_t port) { diff --git a/src/Arduino_MQTT_Client.h b/src/Arduino_MQTT_Client.h index 5de9ea3b..b1d5356a 100644 --- a/src/Arduino_MQTT_Client.h +++ b/src/Arduino_MQTT_Client.h @@ -31,9 +31,11 @@ class Arduino_MQTT_Client : public IMQTT_Client { void set_connect_callback(Callback::function callback) override; - bool set_buffer_size(uint16_t buffer_size) override; + bool set_buffer_size(uint16_t receive_buffer_size, uint16_t send_buffer_size) override; - uint16_t get_buffer_size() override; + uint16_t get_receive_buffer_size() override; + + uint16_t get_send_buffer_size() override; void set_server(char const * domain, uint16_t port) override; diff --git a/src/Attribute_Request.h b/src/Attribute_Request.h index fc8155c0..3b3137d3 100644 --- a/src/Attribute_Request.h +++ b/src/Attribute_Request.h @@ -158,7 +158,7 @@ class Attribute_Request : public IAPI_Implementation { // Nothing to do } - void Set_Client_Callbacks(Callback::function subscribe_api_callback, Callback::function send_json_callback, Callback::function send_json_string_callback, Callback::function subscribe_topic_callback, Callback::function unsubscribe_topic_callback, Callback::function get_size_callback, Callback::function set_buffer_size_callback, Callback::function get_request_id_callback) override { + void Set_Client_Callbacks(Callback::function subscribe_api_callback, Callback::function send_json_callback, Callback::function send_json_string_callback, Callback::function subscribe_topic_callback, Callback::function unsubscribe_topic_callback, Callback::function get_receive_size_callback, Callback::function get_send_size_callback, Callback::function set_buffer_size_callback, Callback::function get_request_id_callback) override { m_send_json_callback.Set_Callback(send_json_callback); m_subscribe_topic_callback.Set_Callback(subscribe_topic_callback); m_unsubscribe_topic_callback.Set_Callback(unsubscribe_topic_callback); diff --git a/src/Client_Side_RPC.h b/src/Client_Side_RPC.h index 33c7d52b..f16eb8a5 100644 --- a/src/Client_Side_RPC.h +++ b/src/Client_Side_RPC.h @@ -170,7 +170,7 @@ class Client_Side_RPC : public IAPI_Implementation { // Nothing to do } - void Set_Client_Callbacks(Callback::function subscribe_api_callback, Callback::function send_json_callback, Callback::function send_json_string_callback, Callback::function subscribe_topic_callback, Callback::function unsubscribe_topic_callback, Callback::function get_size_callback, Callback::function set_buffer_size_callback, Callback::function get_request_id_callback) override { + void Set_Client_Callbacks(Callback::function subscribe_api_callback, Callback::function send_json_callback, Callback::function send_json_string_callback, Callback::function subscribe_topic_callback, Callback::function unsubscribe_topic_callback, Callback::function get_receive_size_callback, Callback::function get_send_size_callback, Callback::function set_buffer_size_callback, Callback::function get_request_id_callback) override { m_send_json_callback.Set_Callback(send_json_callback); m_subscribe_topic_callback.Set_Callback(subscribe_topic_callback); m_unsubscribe_topic_callback.Set_Callback(unsubscribe_topic_callback); diff --git a/src/Espressif_MQTT_Client.h b/src/Espressif_MQTT_Client.h index ba821b9b..f9db1f9e 100644 --- a/src/Espressif_MQTT_Client.h +++ b/src/Espressif_MQTT_Client.h @@ -218,11 +218,13 @@ class Espressif_MQTT_Client : public IMQTT_Client { m_connected_callback.Set_Callback(callback); } - bool set_buffer_size(uint16_t buffer_size) override { + bool set_buffer_size(uint16_t receive_buffer_size, uint16_t send_buffer_size) override { #if ESP_IDF_VERSION_MAJOR < 5 - m_mqtt_configuration.buffer_size = buffer_size; + m_mqtt_configuration.buffer_size = receive_buffer_size; + m_mqtt_configuration.out_buffer_size = send_buffer_size; #else - m_mqtt_configuration.buffer.size = buffer_size; + m_mqtt_configuration.buffer.size = receive_buffer_size; + m_mqtt_configuration.buffer.out_size = send_buffer_size; #endif // ESP_IDF_VERSION_MAJOR < 5 // Calls esp_mqtt_set_config(), which should adjust the underlying mqtt client to the changed values. @@ -234,7 +236,7 @@ class Espressif_MQTT_Client : public IMQTT_Client { return update_configuration(); } - uint16_t get_buffer_size() override { + uint16_t get_receive_buffer_size() override { #if ESP_IDF_VERSION_MAJOR < 5 return m_mqtt_configuration.buffer_size; #else @@ -242,6 +244,14 @@ class Espressif_MQTT_Client : public IMQTT_Client { #endif // ESP_IDF_VERSION_MAJOR < 5 } + uint16_t get_send_buffer_size() override { +#if ESP_IDF_VERSION_MAJOR < 5 + return m_mqtt_configuration.out_buffer_size; +#else + return m_mqtt_configuration.buffer.out_size; +#endif // ESP_IDF_VERSION_MAJOR < 5 + } + void set_server(char const * domain, uint16_t port) override { #if ESP_IDF_VERSION_MAJOR < 5 m_mqtt_configuration.host = domain; @@ -420,7 +430,7 @@ class Espressif_MQTT_Client : public IMQTT_Client { // Check wheter the given message has not bee received completly, but instead would be received in multiple chunks, // if it were we discard the message because receiving a message over multiple chunks is currently not supported if (event->data_len != event->total_data_len) { - Logger::printfln(MQTT_DATA_EXCEEDS_BUFFER, event->total_data_len, get_buffer_size()); + Logger::printfln(MQTT_DATA_EXCEEDS_BUFFER, event->total_data_len, get_receive_buffer_size()); break; } // Topic is not null terminated, to fix this issue we copy the topic string. diff --git a/src/IAPI_Implementation.h b/src/IAPI_Implementation.h index 0a6c40e9..74228f20 100644 --- a/src/IAPI_Implementation.h +++ b/src/IAPI_Implementation.h @@ -94,10 +94,11 @@ class IAPI_Implementation { /// @param send_json_string_callback Method which allows to send arbitrary JSON string payload, points to Send_Json_String per default /// @param subscribe_topic_callback Method which allows to subscribe to arbitrary topics, points to m_client.subscribe per default /// @param unsubscribe_topic_callback Method which allows to unsubscribe from arbitrary topics, points to m_client.unsubscribe per default - /// @param get_size_callback Method which allows to get the current underlying size of the buffer, points to m_client.get_buffer_size per default + /// @param get_receive_size_callback Method which allows to get the current underlying receive size of the buffer, points to m_client.get_receive_buffer_size per default + /// @param get_send_size_callback Method which allows to get the current underlying send size of the buffer, points to m_client.get_send_buffer_size per default /// @param set_buffer_size_callback Method which allows to set the current underlying size of the buffer, points to m_client.set_buffer_size per default /// @param get_request_id_callback Method which allows to get the current request id as a mutable reference, points to getRequestID per default - virtual void Set_Client_Callbacks(Callback::function subscribe_api_callback, Callback::function send_json_callback, Callback::function send_json_string_callback, Callback::function subscribe_topic_callback, Callback::function unsubscribe_topic_callback, Callback::function get_size_callback, Callback::function set_buffer_size_callback, Callback::function get_request_id_callback) = 0; + virtual void Set_Client_Callbacks(Callback::function subscribe_api_callback, Callback::function send_json_callback, Callback::function send_json_string_callback, Callback::function subscribe_topic_callback, Callback::function unsubscribe_topic_callback, Callback::function get_receive_size_callback, Callback::function get_send_size_callback, Callback::function set_buffer_size_callback, Callback::function get_request_id_callback) = 0; }; #endif // IAPI_Implementation_h diff --git a/src/IMQTT_Client.h b/src/IMQTT_Client.h index 467f3cc2..0182c3b8 100644 --- a/src/IMQTT_Client.h +++ b/src/IMQTT_Client.h @@ -44,15 +44,24 @@ class IMQTT_Client { /// @brief Changes the size of the buffer for sent and received MQTT messages, /// using a bigger value than uint16_t for passing the buffer size does not make any sense because the maximum message size received /// or sent by MQTT can never be bigger than 64K, because it relies on TCP and the TCP size limit also uses a uint16_t internally for the size parameter - /// @param buffer_size Maximum amount of data that can be either received or sent via MQTT at once, - /// expected behaviour is that, if bigger packets are received they are discarded and a warning is printed to the console - /// and if we attempt to send data that is bigger, it will simply not be sent and a message is printed to the console instead - /// @return Whether allocating the needed memory for the given buffer_size was successful or not - virtual bool set_buffer_size(uint16_t buffer_size) = 0; + /// @param receive_buffer_size Maximum amount of data that can be received via MQTT at once, + /// expected behaviour is that, if bigger packets are received they are discarded and a warning is printed to the console. + /// Should be big enough to hold the biggest response that is expected to be ever received by the device at once. + /// @param send_buffer_size Maximum amount of data that can be sent via MQTT at once, + /// expected behaviour is that, if we attempt to send data that is bigger, it will simply not be sent and a message is printed to the console instead. + /// Should be big enough to hold the biggest request that is expected to be ever sent by the device at once. + /// Alternatively it is possible if THINGSBOARD_ENABLE_STREAM_UTILS is enabled, requires using the Arduino framework and simply installing StreamUtils (https://github.com/bblanchon/ArduinoStreamUtils) library, + /// to only set the value of this paramter to the same value as the buffering_size passed to the constructor + enough memory to hold the topic and MQTT Header ~= 20 bytes + /// @return Whether allocating the needed memory for the given buffer sizes was successful or not + virtual bool set_buffer_size(uint16_t receive_buffer_size, uint16_t send_buffer_size) = 0; + + /// @brief Gets the previously set size of the internal buffer size for sent MQTT data + /// @return Internal size of the buffer + virtual uint16_t get_receive_buffer_size() = 0; - /// @brief Gets the previously set size of the internal buffer size for sent and received MQTT + /// @brief Gets the previously set size of the internal buffer size for received MQTT data /// @return Internal size of the buffer - virtual uint16_t get_buffer_size() = 0; + virtual uint16_t get_send_buffer_size() = 0; /// @brief Configures the server and port that the client should connect to MQTT over, /// should be called atleast once before calling connect() so it is clear which server to connect too diff --git a/src/OTA_Firmware_Update.h b/src/OTA_Firmware_Update.h index 97fd6ebb..e8095e6b 100644 --- a/src/OTA_Firmware_Update.h +++ b/src/OTA_Firmware_Update.h @@ -58,7 +58,8 @@ class OTA_Firmware_Update : public IAPI_Implementation { , m_send_json_string_callback() , m_subscribe_topic_callback() , m_unsubscribe_topic_callback() - , m_get_size_callback() + , m_get_receive_size_callback() + , m_get_send_size_callback() , m_set_buffer_size_callback() , m_get_request_id_callback() , m_fw_callback() @@ -216,13 +217,14 @@ class OTA_Firmware_Update : public IAPI_Implementation { m_subscribe_api_callback.Call_Callback(m_fw_attribute_request); } - void Set_Client_Callbacks(Callback::function subscribe_api_callback, Callback::function send_json_callback, Callback::function send_json_string_callback, Callback::function subscribe_topic_callback, Callback::function unsubscribe_topic_callback, Callback::function get_size_callback, Callback::function set_buffer_size_callback, Callback::function get_request_id_callback) override { + void Set_Client_Callbacks(Callback::function subscribe_api_callback, Callback::function send_json_callback, Callback::function send_json_string_callback, Callback::function subscribe_topic_callback, Callback::function unsubscribe_topic_callback, Callback::function get_receive_size_callback, Callback::function get_send_size_callback, Callback::function set_buffer_size_callback, Callback::function get_request_id_callback) override { m_subscribe_api_callback.Set_Callback(subscribe_api_callback); m_send_json_callback.Set_Callback(send_json_callback); m_send_json_string_callback.Set_Callback(send_json_string_callback); m_subscribe_topic_callback.Set_Callback(subscribe_topic_callback); m_unsubscribe_topic_callback.Set_Callback(unsubscribe_topic_callback); - m_get_size_callback.Set_Callback(get_size_callback); + m_get_receive_size_callback.Set_Callback(get_receive_size_callback); + m_get_send_size_callback.Set_Callback(get_send_size_callback); m_set_buffer_size_callback.Set_Callback(set_buffer_size_callback); m_get_request_id_callback.Set_Callback(get_request_id_callback); } @@ -277,7 +279,7 @@ class OTA_Firmware_Update : public IAPI_Implementation { // to allow to receive ota chunck packets that might be much bigger than the normal // buffer size would allow, therefore we return to the previous value to decrease overall memory usage if (m_changed_buffer_size) { - (void)m_set_buffer_size_callback.Call_Callback(m_previous_buffer_size); + (void)m_set_buffer_size_callback.Call_Callback(m_previous_buffer_size, m_get_send_size_callback.Call_Callback()); } // Reset now not needed private member variables m_fw_callback = OTA_Update_Callback(); @@ -394,11 +396,11 @@ class OTA_Firmware_Update : public IAPI_Implementation { const uint16_t& chunk_size = m_fw_callback.Get_Chunk_Size(); // Get the previous buffer size and cache it so the previous settings can be restored. - m_previous_buffer_size = m_get_size_callback.Call_Callback(); + m_previous_buffer_size = m_get_receive_size_callback.Call_Callback(); m_changed_buffer_size = m_previous_buffer_size < (chunk_size + 50U); // Increase size of receive buffer - if (m_changed_buffer_size && !m_set_buffer_size_callback.Call_Callback(chunk_size + 50U)) { + if (m_changed_buffer_size && !m_set_buffer_size_callback.Call_Callback(chunk_size + 50U, m_get_send_size_callback.Call_Callback())) { Logger::printfln(NOT_ENOUGH_RAM); Firmware_Send_State(FW_STATE_FAILED, NOT_ENOUGH_RAM); m_fw_callback.Call_Callback(false); @@ -455,8 +457,9 @@ class OTA_Firmware_Update : public IAPI_Implementation { Callback m_send_json_string_callback = {}; // Send json string callback Callback m_subscribe_topic_callback = {}; // Subscribe mqtt topic client callback Callback m_unsubscribe_topic_callback = {}; // Unubscribe mqtt topic client callback - Callback m_get_size_callback = {}; // Get client buffer size callback - Callback m_set_buffer_size_callback = {}; // Set client buffer size callback + Callback m_get_receive_size_callback = {}; // Get client receive buffer size callback + Callback m_get_send_size_callback = {}; // Get client send buffer size callback + Callback m_set_buffer_size_callback = {}; // Set client buffer size callback Callback m_get_request_id_callback = {}; // Get internal request id callback OTA_Update_Callback m_fw_callback = {}; // OTA update response callback diff --git a/src/Provision.h b/src/Provision.h index 8ba142c3..ed028eab 100644 --- a/src/Provision.h +++ b/src/Provision.h @@ -129,7 +129,7 @@ class Provision : public IAPI_Implementation { // Nothing to do } - void Set_Client_Callbacks(Callback::function subscribe_api_callback, Callback::function send_json_callback, Callback::function send_json_string_callback, Callback::function subscribe_topic_callback, Callback::function unsubscribe_topic_callback, Callback::function get_size_callback, Callback::function set_buffer_size_callback, Callback::function get_request_id_callback) override { + void Set_Client_Callbacks(Callback::function subscribe_api_callback, Callback::function send_json_callback, Callback::function send_json_string_callback, Callback::function subscribe_topic_callback, Callback::function unsubscribe_topic_callback, Callback::function get_receive_size_callback, Callback::function get_send_size_callback, Callback::function set_buffer_size_callback, Callback::function get_request_id_callback) override { m_send_json_callback.Set_Callback(send_json_callback); m_subscribe_topic_callback.Set_Callback(subscribe_topic_callback); m_unsubscribe_topic_callback.Set_Callback(unsubscribe_topic_callback); diff --git a/src/Server_Side_RPC.h b/src/Server_Side_RPC.h index 5da04c35..3f3c2003 100644 --- a/src/Server_Side_RPC.h +++ b/src/Server_Side_RPC.h @@ -197,7 +197,7 @@ class Server_Side_RPC : public IAPI_Implementation { // Nothing to do } - void Set_Client_Callbacks(Callback::function subscribe_api_callback, Callback::function send_json_callback, Callback::function send_json_string_callback, Callback::function subscribe_topic_callback, Callback::function unsubscribe_topic_callback, Callback::function get_size_callback, Callback::function set_buffer_size_callback, Callback::function get_request_id_callback) override { + void Set_Client_Callbacks(Callback::function subscribe_api_callback, Callback::function send_json_callback, Callback::function send_json_string_callback, Callback::function subscribe_topic_callback, Callback::function unsubscribe_topic_callback, Callback::function get_receive_size_callback, Callback::function get_send_size_callback, Callback::function set_buffer_size_callback, Callback::function get_request_id_callback) override { m_send_json_callback.Set_Callback(send_json_callback); m_subscribe_topic_callback.Set_Callback(subscribe_topic_callback); m_unsubscribe_topic_callback.Set_Callback(unsubscribe_topic_callback); diff --git a/src/Shared_Attribute_Update.h b/src/Shared_Attribute_Update.h index 76b5a34f..89b3ec66 100644 --- a/src/Shared_Attribute_Update.h +++ b/src/Shared_Attribute_Update.h @@ -186,7 +186,7 @@ class Shared_Attribute_Update : public IAPI_Implementation { // Nothing to do } - void Set_Client_Callbacks(Callback::function subscribe_api_callback, Callback::function send_json_callback, Callback::function send_json_string_callback, Callback::function subscribe_topic_callback, Callback::function unsubscribe_topic_callback, Callback::function get_size_callback, Callback::function set_buffer_size_callback, Callback::function get_request_id_callback) override { + void Set_Client_Callbacks(Callback::function subscribe_api_callback, Callback::function send_json_callback, Callback::function send_json_string_callback, Callback::function subscribe_topic_callback, Callback::function unsubscribe_topic_callback, Callback::function get_receive_size_callback, Callback::function get_send_size_callback, Callback::function set_buffer_size_callback, Callback::function get_request_id_callback) override { m_subscribe_topic_callback.Set_Callback(subscribe_topic_callback); m_unsubscribe_topic_callback.Set_Callback(unsubscribe_topic_callback); } diff --git a/src/ThingsBoard.h b/src/ThingsBoard.h index 83f6787f..1862d151 100644 --- a/src/ThingsBoard.h +++ b/src/ThingsBoard.h @@ -18,7 +18,7 @@ uint16_t constexpr DEFAULT_MQTT_PORT = 1883U; char constexpr PROV_ACCESS_TOKEN[] = "provision"; // Log messages. char constexpr UNABLE_TO_DE_SERIALIZE_JSON[] = "Unable to de-serialize received json data with error (DeserializationError::%s)"; -char constexpr INVALID_BUFFER_SIZE[] = "Buffer size (%u) to small for the given payloads size (%u), increase with setBufferSize accordingly or set THINGSBOARD_ENABLE_STREAM_UTILS to 1 before including ThingsBoard"; +char constexpr INVALID_BUFFER_SIZE[] = "Send buffer size (%u) to small for the given payloads size (%u), increase with setBufferSize accordingly or install the StreamUtils library"; char constexpr UNABLE_TO_ALLOCATE_BUFFER[] = "Allocating memory for the internal MQTT buffer failed"; char constexpr MAX_ENDPOINTS_AMOUNT_TEMPLATE_NAME[] = "MaxEndpointsAmount"; #if THINGSBOARD_ENABLE_DYNAMIC @@ -71,18 +71,14 @@ class ThingsBoardSized { /// and to the end of the data container (last element + 1) and then every element between those iteratos will be copied, in the same order as in the original data container /// @tparam ...Args Holds the multiple arguments that will simply be forwarded to the Array or Vector (THINGSBOARD_ENABLE_DYNAMIC) constructor and therefore allow to use every overloaded vector constructor without having to implement them /// @param client MQTT Client implementation that should be used to establish the connection to ThingsBoard - /// @param buffer_size Maximum amount of data that can be either received or sent to ThingsBoard at once, if bigger packets are received they are discarded - /// and if we attempt to send data that is bigger, it will not be sent, the internal value can be changed later at any time with the setBufferSize() method - /// alternatively setting THINGSBOARD_ENABLE_STREAM_UTILS to 1 allows to send arbitrary size payloads if that is done the internal buffer of the MQTT Client implementation - /// can be theoretically set to only be as big as the biggest message we should every receive from ThingsBoard, - /// this will mean though that all messages are sent over the StreamUtils library as long as they are bigger than the internal buffer, + /// @param receive_buffer_size Maximum amount of data that can be received by this device at once, if bigger packets are received they are discarded and the update lost instead, default = Default_Payload_Size (64) + /// @param send_buffer_size Maximum amount of data that can be sent from this device at once. If we attempt to send data that is bigger, it will not be sent instead. + /// Alternatively setting THINGSBOARD_ENABLE_STREAM_UTILS to 1 allows to send arbitrary size payloads if that is done the internal buffer of the MQTT Client implementation + /// can be theoretically set the value as big as the buffering_size passed to the constructor + enough memory to hold the topic and MQTT Header ~= 20 bytes. + /// This will mean though that all messages are sent over the StreamUtils library as long as they are bigger than the internal buffer, /// which needs more time than sending a message directly but has the advantage of requiring less memory. - /// So if that is a problem on the board it might be useful to enable the THINGSBOARD_ENABLE_STREAM_UTILS option - /// and decrease the internal buffer size of the mqtt client to what is needed to receive all MQTT messages, - /// that size can vary but if all ThingsBoard features are used a buffer size of 256 bytes should suffice for receiving most responses. - /// If the aforementioned feature is not enabled the buffer size might need to be much bigger though, - /// but in that case if a message was too big to be sent the user will be informed with a message to the Logger. - /// The aforementioned options can only be enabled if Arduino is used to build this library, because the StreamUtils library requires it, default = Default_Payload_Size (64) + /// So if the available heap memory is a problem on the board it might be useful to enable the THINGSBOARD_ENABLE_STREAM_UTILS option. + /// This can be done by simply using Arduino as the framework and installing the StreamUtils (https://github.com/bblanchon/ArduinoStreamUtils) library, default = Default_Payload_Size (64) /// @param max_stack_size Maximum amount of bytes we want to allocate on the stack, default = Default_Max_Stack_Size (1024) /// @param ...args Arguments that will be forwarded into the overloaded Array or Vector (THINGSBOARD_ENABLE_DYNAMIC) constructor template @@ -95,7 +91,7 @@ class ThingsBoardSized { /// especially because attempting to allocate too much memory, will cause the allocation to fail, which is checked. But if the failure of that heap allocation is subscribed for example with the heap_caps_register_failed_alloc_callback method on the ESP32, /// then that subscribed callback will be called and could theoretically restart the device. To circumvent that we can simply set the size of this variable to a value that should never be exceeded by a non malicious json payload, received by attribute requests, shared attribute updates, server-side or client-side rpc. /// If this safety feature is not required, because the heap allocation failure callback is not subscribed, then the value of the variable can simply be kept as 0, which means we will not check the received payload for its size before the allocation happens, default = Default_Max_Response_Size (0) - ThingsBoardSized(IMQTT_Client & client, uint16_t buffer_size = Default_Payload_Size, size_t const & max_stack_size = Default_Max_Stack_Size, size_t const & buffering_size = Default_Buffering_Size, size_t const & max_response_size = Default_Max_Response_Size, Args const &... args) + ThingsBoardSized(IMQTT_Client & client, uint16_t receive_buffer_size = Default_Payload_Size, uint16_t send_buffer_size = Default_Payload_Size, size_t const & max_stack_size = Default_Max_Stack_Size, size_t const & buffering_size = Default_Buffering_Size, size_t const & max_response_size = Default_Max_Response_Size, Args const &... args) #else /// @param max_response_size Maximum amount of bytes allocated for the interal JsonDocument structure that holds the received payload. /// Size is calculated automatically from certain characters in the received payload (',', '{', '[') but if we receive a malicious payload that contains these symbols in a string {"example":",,,,,,..."}. @@ -103,14 +99,14 @@ class ThingsBoardSized { /// especially because attempting to allocate too much memory, will cause the allocation to fail, which is checked. But if the failure of that heap allocation is subscribed for example with the heap_caps_register_failed_alloc_callback method on the ESP32, /// then that subscribed callback will be called and could theoretically restart the device. To circumvent that we can simply set the size of this variable to a value that should never be exceeded by a non malicious json payload, received by attribute requests, shared attribute updates, server-side or client-side rpc. /// If this safety feature is not required, because the heap allocation failure callback is not subscribed, then the value of the variable can simply be kept as 0, which means we will not check the received payload for its size before the allocation happens, default = Default_Max_Response_Size (0) - ThingsBoardSized(IMQTT_Client & client, uint16_t buffer_size = Default_Payload_Size, size_t const & max_stack_size = Default_Max_Stack_Size, size_t const & max_response_size = Default_Max_Response_Size, Args const &... args) + ThingsBoardSized(IMQTT_Client & client, uint16_t receive_buffer_size = Default_Payload_Size, uint16_t send_buffer_size = Default_Payload_Size, size_t const & max_stack_size = Default_Max_Stack_Size, size_t const & max_response_size = Default_Max_Response_Size, Args const &... args) #endif // THINGSBOARD_ENABLE_STREAM_UTILS #else #if THINGSBOARD_ENABLE_STREAM_UTILS /// @param buffering_size Amount of bytes allocated to speed up serialization, default = Default_Buffering_Size (64) - ThingsBoardSized(IMQTT_Client & client, uint16_t buffer_size = Default_Payload_Size, size_t const & max_stack_size = Default_Max_Stack_Size, size_t const & buffering_size = Default_Buffering_Size, Args const &... args) + ThingsBoardSized(IMQTT_Client & client, uint16_t receive_buffer_size = Default_Payload_Size, uint16_t send_buffer_size = Default_Payload_Size, size_t const & max_stack_size = Default_Max_Stack_Size, size_t const & buffering_size = Default_Buffering_Size, Args const &... args) #else - ThingsBoardSized(IMQTT_Client & client, uint16_t buffer_size = Default_Payload_Size, size_t const & max_stack_size = Default_Max_Stack_Size, Args const &... args) + ThingsBoardSized(IMQTT_Client & client, uint16_t receive_buffer_size = Default_Payload_Size, uint16_t send_buffer_size = Default_Payload_Size, size_t const & max_stack_size = Default_Max_Stack_Size, Args const &... args) #endif // THINGSBOARD_ENABLE_STREAM_UTILS #endif // THINGSBOARD_ENABLE_DYNAMIC : m_client(client) @@ -128,13 +124,13 @@ class ThingsBoardSized { continue; } #if THINGSBOARD_ENABLE_STL - api->Set_Client_Callbacks(std::bind(&ThingsBoardSized::Subscribe_API_Implementation, this, std::placeholders::_1), std::bind(&ThingsBoardSized::Send_Json, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3), std::bind(&ThingsBoardSized::Send_Json_String, this, std::placeholders::_1, std::placeholders::_2), std::bind(&ThingsBoardSized::clientSubscribe, this, std::placeholders::_1), std::bind(&ThingsBoardSized::clientUnsubscribe, this, std::placeholders::_1), std::bind(&ThingsBoardSized::getClientBufferSize, this), std::bind(&ThingsBoardSized::setBufferSize, this, std::placeholders::_1), std::bind(&ThingsBoardSized::getRequestID, this)); + api->Set_Client_Callbacks(std::bind(&ThingsBoardSized::Subscribe_API_Implementation, this, std::placeholders::_1), std::bind(&ThingsBoardSized::Send_Json, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3), std::bind(&ThingsBoardSized::Send_Json_String, this, std::placeholders::_1, std::placeholders::_2), std::bind(&ThingsBoardSized::clientSubscribe, this, std::placeholders::_1), std::bind(&ThingsBoardSized::clientUnsubscribe, this, std::placeholders::_1), std::bind(&ThingsBoardSized::getClientReceiveBufferSize, this), std::bind(&ThingsBoardSized::getClientSendBufferSize, this), std::bind(&ThingsBoardSized::setBufferSize, this, std::placeholders::_1, std::placeholders::_2), std::bind(&ThingsBoardSized::getRequestID, this)); #else - api->Set_Client_Callbacks(ThingsBoardSized::staticSubscribeImplementation, ThingsBoardSized::staticSendJson, ThingsBoardSized::staticSendJsonString, ThingsBoardSized::staticClientSubscribe, ThingsBoardSized::staticClientUnsubscribe, ThingsBoardSized::staticGetClientBufferSize, ThingsBoardSized::staticSetBufferSize, ThingsBoardSized::staticGetRequestID); + api->Set_Client_Callbacks(ThingsBoardSized::staticSubscribeImplementation, ThingsBoardSized::staticSendJson, ThingsBoardSized::staticSendJsonString, ThingsBoardSized::staticClientSubscribe, ThingsBoardSized::staticClientUnsubscribe, ThingsBoardSized::staticGetClientReceiveBufferSize, ThingsBoardSized::staticGetClientSendBufferSize, ThingsBoardSized::staticSetBufferSize, ThingsBoardSized::staticGetRequestID); #endif // THINGSBOARD_ENABLE_STL api->Initialize(); } - (void)setBufferSize(buffer_size); + (void)setBufferSize(receive_buffer_size, send_buffer_size); // Initialize callback. #if THINGSBOARD_ENABLE_STL m_client.set_data_callback(std::bind(&ThingsBoardSized::onMQTTMessage, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)); @@ -183,22 +179,21 @@ class ThingsBoardSized { } #endif // THINGSBOARD_ENABLE_DYNAMIC - /// @brief Sets the size of the buffer for the underlying network client that will be used to establish the connection to ThingsBoard - /// @param buffer_size Maximum amount of data that can be either received or sent to ThingsBoard at once, if bigger packets are received they are discarded - /// and if we attempt to send data that is bigger, it will not be sent, the internal value can be changed later at any time with the setBufferSize() method - /// alternatively setting THINGSBOARD_ENABLE_STREAM_UTILS to 1 allows to send arbitrary size payloads if that is done the internal buffer of the MQTT Client implementation - /// can be theoretically set to only be as big as the biggest message we should every receive from ThingsBoard, - /// this will mean though that all messages are sent over the StreamUtils library as long as they are bigger than the internal buffer, + /// @brief Sets the size of the buffer for the underlying network client that will be used to establish the connection to ThingsBoard. + /// The internal values can be changed later again, at any time with the setBufferSize() method. Is split into two arguments, because it allows seperating the buffer that received data from the one that sends data. + /// This makes it possible to optimize the memory used and to handle received data without copying it, while sending data in between. + /// Meaning it is possible to read the values in callback functions even after you send more data from this device. + /// @param receive_buffer_size Maximum amount of data that can be received by this device at once, if bigger packets are received they are discarded and the update lost instead + /// @param send_buffer_size Maximum amount of data that can be sent from this device at once. If we attempt to send data that is bigger, it will not be sent instead. + /// Alternatively setting THINGSBOARD_ENABLE_STREAM_UTILS to 1 allows to send arbitrary size payloads if that is done the internal buffer of the MQTT Client implementation + /// can be theoretically set the value as big as the buffering_size passed to the constructor + enough memory to hold the topic and MQTT Header ~= 20 bytes. + /// This will mean though that all messages are sent over the StreamUtils library as long as they are bigger than the internal buffer, /// which needs more time than sending a message directly but has the advantage of requiring less memory. - /// So if that is a problem on the board it might be useful to enable the THINGSBOARD_ENABLE_STREAM_UTILS option - /// and decrease the internal buffer size of the mqtt client to what is needed to receive all MQTT messages, - /// that size can vary but if all ThingsBoard features are used a buffer size of 256 bytes should suffice for receiving most responses. - /// If the aforementioned feature is not enabled the buffer size might need to be much bigger though, - /// but in that case if a message was too big to be sent the user will be informed with a message to the logger implementation. - /// The aforementioned options can only be enabled if Arduino is used to build this library, because the StreamUtils library requires it - /// @return Whether allocating the needed memory for the given buffer size was successful or not - bool setBufferSize(uint16_t buffer_size) { - bool const result = m_client.set_buffer_size(buffer_size); + /// So if the available heap memory is a problem on the board it might be useful to enable the THINGSBOARD_ENABLE_STREAM_UTILS option. + /// This can be done by simply using Arduino as the framework and installing the StreamUtils (https://github.com/bblanchon/ArduinoStreamUtils) library + /// @return Whether allocating the needed memory for the given buffer sizes was successful or not + bool setBufferSize(uint16_t receive_buffer_size, uint16_t send_buffer_size) { + bool const result = m_client.set_buffer_size(receive_buffer_size, send_buffer_size); if (!result) { Logger::printfln(UNABLE_TO_ALLOCATE_BUFFER); } @@ -335,11 +330,11 @@ class ThingsBoardSized { return false; } - uint16_t currentBufferSize = m_client.get_buffer_size(); + uint16_t current_send_buffer_size = m_client.get_send_buffer_size(); size_t const json_size = strlen(json); - if (currentBufferSize < json_size) { - Logger::printfln(INVALID_BUFFER_SIZE, currentBufferSize, json_size); + if (current_send_buffer_size < json_size) { + Logger::printfln(INVALID_BUFFER_SIZE, current_send_buffer_size, json_size); return false; } @@ -360,9 +355,9 @@ class ThingsBoardSized { } #endif // !THINGSBOARD_ENABLE_DYNAMIC #if THINGSBOARD_ENABLE_STL - api.Set_Client_Callbacks(std::bind(&ThingsBoardSized::Subscribe_API_Implementation, this, std::placeholders::_1), std::bind(&ThingsBoardSized::Send_Json, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3), std::bind(&ThingsBoardSized::Send_Json_String, this, std::placeholders::_1, std::placeholders::_2), std::bind(&ThingsBoardSized::clientSubscribe, this, std::placeholders::_1), std::bind(&ThingsBoardSized::clientUnsubscribe, this, std::placeholders::_1), std::bind(&ThingsBoardSized::getClientBufferSize, this), std::bind(&ThingsBoardSized::setBufferSize, this, std::placeholders::_1), std::bind(&ThingsBoardSized::getRequestID, this)); + api.Set_Client_Callbacks(std::bind(&ThingsBoardSized::Subscribe_API_Implementation, this, std::placeholders::_1), std::bind(&ThingsBoardSized::Send_Json, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3), std::bind(&ThingsBoardSized::Send_Json_String, this, std::placeholders::_1, std::placeholders::_2), std::bind(&ThingsBoardSized::clientSubscribe, this, std::placeholders::_1), std::bind(&ThingsBoardSized::clientUnsubscribe, this, std::placeholders::_1), std::bind(&ThingsBoardSized::getClientReceiveBufferSize, this), std::bind(&ThingsBoardSized::getClientSendBufferSize, this), std::bind(&ThingsBoardSized::setBufferSize, this, std::placeholders::_1, std::placeholders::_2), std::bind(&ThingsBoardSized::getRequestID, this)); #else - api.Set_Client_Callbacks(ThingsBoardSized::staticSubscribeImplementation, ThingsBoardSized::staticSendJson, ThingsBoardSized::staticSendJsonString, ThingsBoardSized::staticClientSubscribe, ThingsBoardSized::staticClientUnsubscribe, ThingsBoardSized::staticGetClientBufferSize, ThingsBoardSized::staticSetBufferSize, ThingsBoardSized::staticGetRequestID); + api.Set_Client_Callbacks(ThingsBoardSized::staticSubscribeImplementation, ThingsBoardSized::staticSendJson, ThingsBoardSized::staticSendJsonString, ThingsBoardSized::staticClientSubscribe, ThingsBoardSized::staticClientUnsubscribe, ThingsBoardSized::staticGetClientReceiveBufferSize, ThingsBoardSized::staticGetClientSendBufferSize, ThingsBoardSized::staticSetBufferSize, ThingsBoardSized::staticGetRequestID); #endif // THINGSBOARD_ENABLE_STL api.Initialize(); m_api_implementations.push_back(&api); @@ -391,9 +386,9 @@ class ThingsBoardSized { continue; } #if THINGSBOARD_ENABLE_STL - api->Set_Client_Callbacks(std::bind(&ThingsBoardSized::Subscribe_API_Implementation, this, std::placeholders::_1), std::bind(&ThingsBoardSized::Send_Json, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3), std::bind(&ThingsBoardSized::Send_Json_String, this, std::placeholders::_1, std::placeholders::_2), std::bind(&ThingsBoardSized::clientSubscribe, this, std::placeholders::_1), std::bind(&ThingsBoardSized::clientUnsubscribe, this, std::placeholders::_1), std::bind(&ThingsBoardSized::getClientBufferSize, this), std::bind(&ThingsBoardSized::setBufferSize, this, std::placeholders::_1), std::bind(&ThingsBoardSized::getRequestID, this)); + api->Set_Client_Callbacks(std::bind(&ThingsBoardSized::Subscribe_API_Implementation, this, std::placeholders::_1), std::bind(&ThingsBoardSized::Send_Json, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3), std::bind(&ThingsBoardSized::Send_Json_String, this, std::placeholders::_1, std::placeholders::_2), std::bind(&ThingsBoardSized::clientSubscribe, this, std::placeholders::_1), std::bind(&ThingsBoardSized::clientUnsubscribe, this, std::placeholders::_1), std::bind(&ThingsBoardSized::getClientReceiveBufferSize, this), std::bind(&ThingsBoardSized::getClientSendBufferSize, this), std::bind(&ThingsBoardSized::setBufferSize, this, std::placeholders::_1, std::placeholders::_2), std::bind(&ThingsBoardSized::getRequestID, this)); #else - api->Set_Client_Callbacks(ThingsBoardSized::staticSubscribeImplementation, ThingsBoardSized::staticSendJson, ThingsBoardSized::staticSendJsonString, ThingsBoardSized::staticClientSubscribe, ThingsBoardSized::staticClientUnsubscribe, ThingsBoardSized::staticGetClientBufferSize, ThingsBoardSized::staticSetBufferSize, ThingsBoardSized::staticGetRequestID); + api->Set_Client_Callbacks(ThingsBoardSized::staticSubscribeImplementation, ThingsBoardSized::staticSendJson, ThingsBoardSized::staticSendJsonString, ThingsBoardSized::staticClientSubscribe, ThingsBoardSized::staticClientUnsubscribe, ThingsBoardSized::staticGetClientReceiveBufferSize, ThingsBoardSized::staticGetClientSendBufferSize, ThingsBoardSized::staticSetBufferSize, ThingsBoardSized::staticGetRequestID); #endif // THINGSBOARD_ENABLE_STL api->Initialize(); } @@ -562,10 +557,16 @@ class ThingsBoardSized { return m_max_stack; } - /// @brief Returns the current buffer size of the underlying client interface - /// @return Current internal buffer size - uint16_t getClientBufferSize() { - return m_client.get_buffer_size(); + /// @brief Returns the current receive buffer size of the underlying client interface + /// @return Current internal send buffer size + uint16_t getClientReceiveBufferSize() { + return m_client.get_receive_buffer_size(); + } + + /// @brief Returns the current send buffer size of the underlying client interface + /// @return Current internal receive buffer size + uint16_t getClientSendBufferSize() { + return m_client.get_send_buffer_size(); } /// @brief Subscribes the given topic with the underlying client interface @@ -874,18 +875,25 @@ class ThingsBoardSized { return m_subscribedInstance->getRequestID(); } - static uint16_t staticGetClientBufferSize() { + static uint16_t staticGetClientReceiveBufferSize() { + if (m_subscribedInstance == nullptr) { + return 0U; + } + return m_subscribedInstance->getClientReceiveBufferSize(); + } + + static uint16_t staticGetClientSendBufferSize() { if (m_subscribedInstance == nullptr) { return 0U; } - return m_subscribedInstance->getClientBufferSize(); + return m_subscribedInstance->getClientSendBufferSize(); } - static bool staticSetBufferSize(uint16_t buffer_size) { + static bool staticSetBufferSize(uint16_t receive_buffer_size, uint16_t send_buffer_size) { if (m_subscribedInstance == nullptr) { return false; } - return m_subscribedInstance->setBufferSize(buffer_size); + return m_subscribedInstance->setBufferSize(receive_buffer_size, send_buffer_size); } // PubSub client cannot call a instanced method when message arrives on subscribed topic.