From c5069977a49cbb7ded08e73d8f63c55dc81f4cb3 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Tue, 24 Jul 2018 16:59:03 +0200 Subject: [PATCH 01/91] MQTT: Integrate esp-mqtt library into idf added docs and tests for mqtt library, small fixes (removed warnings, option for custom outbox, websocket bug fixed for longer transports). refactored to use common tcp_transport component, support for CMake build system. Closes #2108 --- components/mqtt/CMakeLists.txt | 7 +++++++ components/mqtt/Kconfig | 1 + components/mqtt/component.mk | 4 ++++ components/mqtt/esp-mqtt | 1 + 4 files changed, 13 insertions(+) create mode 100644 components/mqtt/CMakeLists.txt create mode 100644 components/mqtt/Kconfig create mode 100644 components/mqtt/component.mk create mode 160000 components/mqtt/esp-mqtt diff --git a/components/mqtt/CMakeLists.txt b/components/mqtt/CMakeLists.txt new file mode 100644 index 0000000..ba5375c --- /dev/null +++ b/components/mqtt/CMakeLists.txt @@ -0,0 +1,7 @@ +set(COMPONENT_ADD_INCLUDEDIRS esp-mqtt/include) +set(COMPONENT_PRIV_INCLUDEDIRS "esp-mqtt/lib/include") +set(COMPONENT_SRCDIRS esp-mqtt esp-mqtt/lib) + +set(COMPONENT_REQUIRES lwip nghttp mbedtls tcp_transport) + +register_component() diff --git a/components/mqtt/Kconfig b/components/mqtt/Kconfig new file mode 100644 index 0000000..273a2da --- /dev/null +++ b/components/mqtt/Kconfig @@ -0,0 +1 @@ +source "$IDF_PATH/components/mqtt/esp-mqtt/Kconfig.included" diff --git a/components/mqtt/component.mk b/components/mqtt/component.mk new file mode 100644 index 0000000..19e4980 --- /dev/null +++ b/components/mqtt/component.mk @@ -0,0 +1,4 @@ +COMPONENT_SUBMODULES += esp-mqtt +COMPONENT_ADD_INCLUDEDIRS := esp-mqtt/include +COMPONENT_SRCDIRS := esp-mqtt esp-mqtt/lib +COMPONENT_PRIV_INCLUDEDIRS := esp-mqtt/lib/include diff --git a/components/mqtt/esp-mqtt b/components/mqtt/esp-mqtt new file mode 160000 index 0000000..abaab2a --- /dev/null +++ b/components/mqtt/esp-mqtt @@ -0,0 +1 @@ +Subproject commit abaab2abccc019aa57f5b9afaf57f0d49f7b1b6f From 24b656ca4b7a72c099d30689e5963c743e68cf64 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Thu, 13 Sep 2018 11:36:23 +0200 Subject: [PATCH 02/91] MQTT: Moved Kconfig from esp-mqtt submodule to esp-idf to support docs genration in RTD --- components/mqtt/Kconfig | 103 ++++++++++++++++++++++++++++++++++++++- components/mqtt/esp-mqtt | 2 +- 2 files changed, 103 insertions(+), 2 deletions(-) diff --git a/components/mqtt/Kconfig b/components/mqtt/Kconfig index 273a2da..323cc39 100644 --- a/components/mqtt/Kconfig +++ b/components/mqtt/Kconfig @@ -1 +1,102 @@ -source "$IDF_PATH/components/mqtt/esp-mqtt/Kconfig.included" +menu "ESP-MQTT Configurations" + +config MQTT_PROTOCOL_311 + bool "Enable MQTT protocol 3.1.1" + default y + help + If not, this library will use MQTT protocol 3.1 + +config MQTT_TRANSPORT_SSL + bool "Enable MQTT over SSL" + default y + help + Enable MQTT transport over SSL with mbedtls + +config MQTT_TRANSPORT_WEBSOCKET + bool "Enable MQTT over Websocket" + default y + help + Enable MQTT transport over Websocket. + +config MQTT_TRANSPORT_WEBSOCKET_SECURE + bool "Enable MQTT over Websocket Secure" + default y + depends on MQTT_TRANSPORT_WEBSOCKET + depends on MQTT_TRANSPORT_SSL + help + Enable MQTT transport over Websocket Secure. + +config MQTT_USE_CUSTOM_CONFIG + bool "MQTT Using custom configurations" + default n + help + Custom MQTT configurations. + +config MQTT_TCP_DEFAULT_PORT + int "Default MQTT over TCP port" + default 1883 + depends on MQTT_USE_CUSTOM_CONFIG + help + Default MQTT over TCP port + +config MQTT_SSL_DEFAULT_PORT + int "Default MQTT over SSL port" + default 8883 + depends on MQTT_USE_CUSTOM_CONFIG + depends on MQTT_TRANSPORT_SSL + help + Default MQTT over SSL port + +config MQTT_WS_DEFAULT_PORT + int "Default MQTT over Websocket port" + default 80 + depends on MQTT_USE_CUSTOM_CONFIG + depends on MQTT_TRANSPORT_WEBSOCKET + help + Default MQTT over Websocket port + +config MQTT_WSS_DEFAULT_PORT + int "Default MQTT over Websocket Secure port" + default 443 + depends on MQTT_USE_CUSTOM_CONFIG + depends on MQTT_TRANSPORT_WEBSOCKET + depends on MQTT_TRANSPORT_WEBSOCKET_SECURE + help + Default MQTT over Websocket Secure port + +config MQTT_BUFFER_SIZE + int "Default MQTT Buffer Size" + default 1024 + depends on MQTT_USE_CUSTOM_CONFIG + help + This buffer size using for both transmit and receive + +config MQTT_TASK_STACK_SIZE + int "MQTT task stack size" + default 6144 + depends on MQTT_USE_CUSTOM_CONFIG + help + MQTT task stack size + +config MQTT_TASK_CORE_SELECTION_ENABLED + bool "Enable MQTT task core selection" + default false + help + This will enable core selection + +choice + depends on MQTT_TASK_CORE_SELECTION_ENABLED + prompt "Core to use ?" + config MQTT_USE_CORE_0 + bool "Core 0" + config MQTT_USE_CORE_1 + bool "Core 1" + endchoice + +config MQTT_CUSTOM_OUTBOX + bool "Enable custom outbox implementation" + default n + help + Set to true if a specific implementation of message outbox is needed (e.g. persistant outbox in NVM or similar). + +endmenu diff --git a/components/mqtt/esp-mqtt b/components/mqtt/esp-mqtt index abaab2a..bcb38e4 160000 --- a/components/mqtt/esp-mqtt +++ b/components/mqtt/esp-mqtt @@ -1 +1 @@ -Subproject commit abaab2abccc019aa57f5b9afaf57f0d49f7b1b6f +Subproject commit bcb38e45f521085f997439a8b6c4ead34dff9043 From 20dda12b887e1f71909759c741e86169696a895e Mon Sep 17 00:00:00 2001 From: Renz Bagaporo Date: Wed, 19 Sep 2018 16:48:12 +0800 Subject: [PATCH 03/91] mqtt: list files manually in component cmake file --- components/mqtt/CMakeLists.txt | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/components/mqtt/CMakeLists.txt b/components/mqtt/CMakeLists.txt index ba5375c..27cf1e2 100644 --- a/components/mqtt/CMakeLists.txt +++ b/components/mqtt/CMakeLists.txt @@ -1,6 +1,10 @@ set(COMPONENT_ADD_INCLUDEDIRS esp-mqtt/include) set(COMPONENT_PRIV_INCLUDEDIRS "esp-mqtt/lib/include") -set(COMPONENT_SRCDIRS esp-mqtt esp-mqtt/lib) +set(COMPONENT_SRCS "esp-mqtt/mqtt_client.c" + "esp-mqtt/lib/mqtt_msg.c" + "esp-mqtt/lib/mqtt_outbox.c" + "esp-mqtt/lib/platform_esp32_idf.c" + "esp-mqtt/lib/transport_ws.c") set(COMPONENT_REQUIRES lwip nghttp mbedtls tcp_transport) From 1d49ac324916a991540d434e2f629e78648961d1 Mon Sep 17 00:00:00 2001 From: Anton Maklakov Date: Tue, 18 Sep 2018 14:16:19 +0800 Subject: [PATCH 04/91] mqtt: silence a format warning --- components/mqtt/CMakeLists.txt | 8 ++++++++ components/mqtt/component.mk | 4 ++++ 2 files changed, 12 insertions(+) diff --git a/components/mqtt/CMakeLists.txt b/components/mqtt/CMakeLists.txt index 27cf1e2..d100f80 100644 --- a/components/mqtt/CMakeLists.txt +++ b/components/mqtt/CMakeLists.txt @@ -9,3 +9,11 @@ set(COMPONENT_SRCS "esp-mqtt/mqtt_client.c" set(COMPONENT_REQUIRES lwip nghttp mbedtls tcp_transport) register_component() + +if(GCC_NOT_5_2_0) + # Temporary suppress "format-overflow" warning until we are fixed in esp-mqtt repo + set_source_files_properties( + esp-mqtt/lib/transport_ws.c + PROPERTIES COMPILE_FLAGS + -Wno-format-overflow) +endif() diff --git a/components/mqtt/component.mk b/components/mqtt/component.mk index 19e4980..7c77159 100644 --- a/components/mqtt/component.mk +++ b/components/mqtt/component.mk @@ -2,3 +2,7 @@ COMPONENT_SUBMODULES += esp-mqtt COMPONENT_ADD_INCLUDEDIRS := esp-mqtt/include COMPONENT_SRCDIRS := esp-mqtt esp-mqtt/lib COMPONENT_PRIV_INCLUDEDIRS := esp-mqtt/lib/include + +ifeq ($(GCC_NOT_5_2_0), 1) +esp-mqtt/lib/transport_ws.o: CFLAGS += -Wno-format-overflow +endif From 78233fe7de0918612f1df76d5eb9c73a2f612ad9 Mon Sep 17 00:00:00 2001 From: Angus Gratton Date: Wed, 19 Sep 2018 16:10:46 +1000 Subject: [PATCH 05/91] doc: Re-add summaries of what children each menu item has Slightly different to the original version of this, but same goal. --- components/mqtt/Kconfig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/mqtt/Kconfig b/components/mqtt/Kconfig index 323cc39..624b286 100644 --- a/components/mqtt/Kconfig +++ b/components/mqtt/Kconfig @@ -84,7 +84,7 @@ config MQTT_TASK_CORE_SELECTION_ENABLED help This will enable core selection -choice +choice MQTT_TASK_CORE_SELECTION depends on MQTT_TASK_CORE_SELECTION_ENABLED prompt "Core to use ?" config MQTT_USE_CORE_0 From 71d6861d6a82c9eedbb9cfb8665a5d222eaf64bc Mon Sep 17 00:00:00 2001 From: David Cermak Date: Tue, 25 Sep 2018 10:34:04 +0200 Subject: [PATCH 06/91] tcp_transport: renamed transport related header files to esp_ prefixed to avoid collisions tcp_transport component used public header files such as 'transport.h', etc. which are too generic and might collide with user or user libraries headers This change closes #2417 --- components/mqtt/CMakeLists.txt | 11 +---------- components/mqtt/esp-mqtt | 2 +- 2 files changed, 2 insertions(+), 11 deletions(-) diff --git a/components/mqtt/CMakeLists.txt b/components/mqtt/CMakeLists.txt index d100f80..85f63cc 100644 --- a/components/mqtt/CMakeLists.txt +++ b/components/mqtt/CMakeLists.txt @@ -3,17 +3,8 @@ set(COMPONENT_PRIV_INCLUDEDIRS "esp-mqtt/lib/include") set(COMPONENT_SRCS "esp-mqtt/mqtt_client.c" "esp-mqtt/lib/mqtt_msg.c" "esp-mqtt/lib/mqtt_outbox.c" - "esp-mqtt/lib/platform_esp32_idf.c" - "esp-mqtt/lib/transport_ws.c") + "esp-mqtt/lib/platform_esp32_idf.c") set(COMPONENT_REQUIRES lwip nghttp mbedtls tcp_transport) register_component() - -if(GCC_NOT_5_2_0) - # Temporary suppress "format-overflow" warning until we are fixed in esp-mqtt repo - set_source_files_properties( - esp-mqtt/lib/transport_ws.c - PROPERTIES COMPILE_FLAGS - -Wno-format-overflow) -endif() diff --git a/components/mqtt/esp-mqtt b/components/mqtt/esp-mqtt index bcb38e4..ca893e2 160000 --- a/components/mqtt/esp-mqtt +++ b/components/mqtt/esp-mqtt @@ -1 +1 @@ -Subproject commit bcb38e45f521085f997439a8b6c4ead34dff9043 +Subproject commit ca893e2c23a104cead0bb756a11af7b80464c2d4 From 229167f48aa6ef7467766c06c7faa969fae3f36f Mon Sep 17 00:00:00 2001 From: David Cermak Date: Wed, 26 Sep 2018 11:56:47 +0200 Subject: [PATCH 07/91] tcp_transport: renamed possibly generic function names to be esp_ prefixed and not to colide with user namespace --- components/mqtt/esp-mqtt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/mqtt/esp-mqtt b/components/mqtt/esp-mqtt index ca893e2..fe3ac2a 160000 --- a/components/mqtt/esp-mqtt +++ b/components/mqtt/esp-mqtt @@ -1 +1 @@ -Subproject commit ca893e2c23a104cead0bb756a11af7b80464c2d4 +Subproject commit fe3ac2af1b708f44e710f35c4731584a3f69acd5 From a31ede754f06cc5eed0f64e5a7a3dbd7af4ac5ba Mon Sep 17 00:00:00 2001 From: David Cermak Date: Tue, 2 Oct 2018 15:19:46 +0200 Subject: [PATCH 08/91] tcp_transport: transport set handle refactoring, web socket client name updated --- components/mqtt/esp-mqtt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/mqtt/esp-mqtt b/components/mqtt/esp-mqtt index fe3ac2a..85ee406 160000 --- a/components/mqtt/esp-mqtt +++ b/components/mqtt/esp-mqtt @@ -1 +1 @@ -Subproject commit fe3ac2af1b708f44e710f35c4731584a3f69acd5 +Subproject commit 85ee406d03fd84f5613c6dead1ea653e384b9559 From 14e9df14ff44eb013ec1d5d7cc16653ac3ef5f92 Mon Sep 17 00:00:00 2001 From: Anton Maklakov Date: Sat, 13 Oct 2018 16:26:11 +0800 Subject: [PATCH 09/91] tcp_transport: Remove the ignore warning because we had idf/esp-idf!3359 --- components/mqtt/component.mk | 4 ---- 1 file changed, 4 deletions(-) diff --git a/components/mqtt/component.mk b/components/mqtt/component.mk index 7c77159..19e4980 100644 --- a/components/mqtt/component.mk +++ b/components/mqtt/component.mk @@ -2,7 +2,3 @@ COMPONENT_SUBMODULES += esp-mqtt COMPONENT_ADD_INCLUDEDIRS := esp-mqtt/include COMPONENT_SRCDIRS := esp-mqtt esp-mqtt/lib COMPONENT_PRIV_INCLUDEDIRS := esp-mqtt/lib/include - -ifeq ($(GCC_NOT_5_2_0), 1) -esp-mqtt/lib/transport_ws.o: CFLAGS += -Wno-format-overflow -endif From 60e39865e6e5c15326d41fd99942525a2ed10953 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Thu, 11 Oct 2018 17:34:09 +0200 Subject: [PATCH 10/91] mqtt: ssl mutual authentication example added per PR from github, corrected cmake build, updated per idf style Merges https://github.com/espressif/esp-idf/pull/2490 --- components/mqtt/esp-mqtt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/mqtt/esp-mqtt b/components/mqtt/esp-mqtt index 85ee406..a7b1cea 160000 --- a/components/mqtt/esp-mqtt +++ b/components/mqtt/esp-mqtt @@ -1 +1 @@ -Subproject commit 85ee406d03fd84f5613c6dead1ea653e384b9559 +Subproject commit a7b1cea5b3e246298607a8c64447765297626f36 From 7437dce403c46739aadb2247d18b08fe6f7c045e Mon Sep 17 00:00:00 2001 From: David Cermak Date: Fri, 16 Nov 2018 10:42:58 +0000 Subject: [PATCH 11/91] mqtt: support for BEFORE_CONNECT event in idf Updated examples to use new event id, idf to use mqtt with fixed retained, oversized messages --- components/mqtt/esp-mqtt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/mqtt/esp-mqtt b/components/mqtt/esp-mqtt index a7b1cea..f08f3b6 160000 --- a/components/mqtt/esp-mqtt +++ b/components/mqtt/esp-mqtt @@ -1 +1 @@ -Subproject commit a7b1cea5b3e246298607a8c64447765297626f36 +Subproject commit f08f3b678717865234637164a29ed3a63e756ca7 From 9726859fa384df6aa3871de48e5f0023c6e5c6df Mon Sep 17 00:00:00 2001 From: Roland Dobai Date: Fri, 25 Jan 2019 17:10:53 +0100 Subject: [PATCH 12/91] Correct Kconfigs according to the coding style --- components/mqtt/Kconfig | 173 ++++++++++++++++++++-------------------- 1 file changed, 87 insertions(+), 86 deletions(-) diff --git a/components/mqtt/Kconfig b/components/mqtt/Kconfig index 624b286..de79975 100644 --- a/components/mqtt/Kconfig +++ b/components/mqtt/Kconfig @@ -1,102 +1,103 @@ menu "ESP-MQTT Configurations" -config MQTT_PROTOCOL_311 - bool "Enable MQTT protocol 3.1.1" - default y - help - If not, this library will use MQTT protocol 3.1 + config MQTT_PROTOCOL_311 + bool "Enable MQTT protocol 3.1.1" + default y + help + If not, this library will use MQTT protocol 3.1 -config MQTT_TRANSPORT_SSL - bool "Enable MQTT over SSL" - default y - help - Enable MQTT transport over SSL with mbedtls + config MQTT_TRANSPORT_SSL + bool "Enable MQTT over SSL" + default y + help + Enable MQTT transport over SSL with mbedtls -config MQTT_TRANSPORT_WEBSOCKET - bool "Enable MQTT over Websocket" - default y - help - Enable MQTT transport over Websocket. + config MQTT_TRANSPORT_WEBSOCKET + bool "Enable MQTT over Websocket" + default y + help + Enable MQTT transport over Websocket. -config MQTT_TRANSPORT_WEBSOCKET_SECURE - bool "Enable MQTT over Websocket Secure" - default y - depends on MQTT_TRANSPORT_WEBSOCKET - depends on MQTT_TRANSPORT_SSL - help - Enable MQTT transport over Websocket Secure. + config MQTT_TRANSPORT_WEBSOCKET_SECURE + bool "Enable MQTT over Websocket Secure" + default y + depends on MQTT_TRANSPORT_WEBSOCKET + depends on MQTT_TRANSPORT_SSL + help + Enable MQTT transport over Websocket Secure. -config MQTT_USE_CUSTOM_CONFIG - bool "MQTT Using custom configurations" - default n - help - Custom MQTT configurations. + config MQTT_USE_CUSTOM_CONFIG + bool "MQTT Using custom configurations" + default n + help + Custom MQTT configurations. -config MQTT_TCP_DEFAULT_PORT - int "Default MQTT over TCP port" - default 1883 - depends on MQTT_USE_CUSTOM_CONFIG - help - Default MQTT over TCP port + config MQTT_TCP_DEFAULT_PORT + int "Default MQTT over TCP port" + default 1883 + depends on MQTT_USE_CUSTOM_CONFIG + help + Default MQTT over TCP port -config MQTT_SSL_DEFAULT_PORT - int "Default MQTT over SSL port" - default 8883 - depends on MQTT_USE_CUSTOM_CONFIG - depends on MQTT_TRANSPORT_SSL - help - Default MQTT over SSL port - -config MQTT_WS_DEFAULT_PORT - int "Default MQTT over Websocket port" - default 80 - depends on MQTT_USE_CUSTOM_CONFIG - depends on MQTT_TRANSPORT_WEBSOCKET - help - Default MQTT over Websocket port + config MQTT_SSL_DEFAULT_PORT + int "Default MQTT over SSL port" + default 8883 + depends on MQTT_USE_CUSTOM_CONFIG + depends on MQTT_TRANSPORT_SSL + help + Default MQTT over SSL port -config MQTT_WSS_DEFAULT_PORT - int "Default MQTT over Websocket Secure port" - default 443 - depends on MQTT_USE_CUSTOM_CONFIG - depends on MQTT_TRANSPORT_WEBSOCKET - depends on MQTT_TRANSPORT_WEBSOCKET_SECURE - help - Default MQTT over Websocket Secure port + config MQTT_WS_DEFAULT_PORT + int "Default MQTT over Websocket port" + default 80 + depends on MQTT_USE_CUSTOM_CONFIG + depends on MQTT_TRANSPORT_WEBSOCKET + help + Default MQTT over Websocket port -config MQTT_BUFFER_SIZE - int "Default MQTT Buffer Size" - default 1024 - depends on MQTT_USE_CUSTOM_CONFIG - help - This buffer size using for both transmit and receive + config MQTT_WSS_DEFAULT_PORT + int "Default MQTT over Websocket Secure port" + default 443 + depends on MQTT_USE_CUSTOM_CONFIG + depends on MQTT_TRANSPORT_WEBSOCKET + depends on MQTT_TRANSPORT_WEBSOCKET_SECURE + help + Default MQTT over Websocket Secure port -config MQTT_TASK_STACK_SIZE - int "MQTT task stack size" - default 6144 - depends on MQTT_USE_CUSTOM_CONFIG - help - MQTT task stack size + config MQTT_BUFFER_SIZE + int "Default MQTT Buffer Size" + default 1024 + depends on MQTT_USE_CUSTOM_CONFIG + help + This buffer size using for both transmit and receive -config MQTT_TASK_CORE_SELECTION_ENABLED - bool "Enable MQTT task core selection" - default false - help - This will enable core selection + config MQTT_TASK_STACK_SIZE + int "MQTT task stack size" + default 6144 + depends on MQTT_USE_CUSTOM_CONFIG + help + MQTT task stack size -choice MQTT_TASK_CORE_SELECTION - depends on MQTT_TASK_CORE_SELECTION_ENABLED - prompt "Core to use ?" - config MQTT_USE_CORE_0 - bool "Core 0" - config MQTT_USE_CORE_1 - bool "Core 1" - endchoice + config MQTT_TASK_CORE_SELECTION_ENABLED + bool "Enable MQTT task core selection" + default false + help + This will enable core selection -config MQTT_CUSTOM_OUTBOX - bool "Enable custom outbox implementation" - default n - help - Set to true if a specific implementation of message outbox is needed (e.g. persistant outbox in NVM or similar). + choice MQTT_TASK_CORE_SELECTION + depends on MQTT_TASK_CORE_SELECTION_ENABLED + prompt "Core to use ?" + config MQTT_USE_CORE_0 + bool "Core 0" + config MQTT_USE_CORE_1 + bool "Core 1" + endchoice + + config MQTT_CUSTOM_OUTBOX + bool "Enable custom outbox implementation" + default n + help + Set to true if a specific implementation of message outbox is needed (e.g. persistant outbox in NVM or + similar). endmenu From e19b9aa2df785b121e40b0c58f52a1bf6c0de617 Mon Sep 17 00:00:00 2001 From: morris Date: Thu, 14 Mar 2019 17:29:32 +0800 Subject: [PATCH 13/91] separate rom from esp32 component to esp_rom 1. separate rom include files and linkscript to esp_rom 2. modefiy "include rom/xxx.h" to "include esp32/rom/xxx.h" 3. Forward compatible 4. update mqtt --- components/mqtt/esp-mqtt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/mqtt/esp-mqtt b/components/mqtt/esp-mqtt index f08f3b6..39118d5 160000 --- a/components/mqtt/esp-mqtt +++ b/components/mqtt/esp-mqtt @@ -1 +1 @@ -Subproject commit f08f3b678717865234637164a29ed3a63e756ca7 +Subproject commit 39118d5182f804edc01ed030c8ba137a9c7f388e From 6b70e14236a32acfe6ffc6bf9d12259780194f93 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Thu, 31 Jan 2019 17:09:34 +0100 Subject: [PATCH 14/91] mqtt tests: adding weekend test for mqtt library to exercise publishing/receiving different data and references esp-mqtt commits to pass these tests testing conditions: transports (tcp, ssl, ws..) qos (0, 1, 2) short repeated messages (packed packets) oversized messages (fragmented packets) publish from a different thread Closes https://github.com/espressif/esp-idf/issues/2870 by means of including commit 815623dfe5a0e41fa0e51ab4e336feb3eaa5ba15 from esp-mqtt Closes https://github.com/espressif/esp-idf/issues/2975 by means of including commit 752953dc3be007cca4255b66a35d3087e61f6a54 from esp-mqtt Closes https://github.com/espressif/esp-idf/issues/2850 by means of including commits df455d2a5fe562dd1b8351da99a1d6d82b66eff3 17fd713bced4f2d00df7ed664ed82a7d108ab317 from esp-mqtt --- components/mqtt/Kconfig | 8 + components/mqtt/weekend_test/config.yml | 3 + components/mqtt/weekend_test/env.yml | 0 .../mqtt/weekend_test/mqtt_publish_test.py | 190 ++++++++++++++++++ 4 files changed, 201 insertions(+) create mode 100644 components/mqtt/weekend_test/config.yml create mode 100644 components/mqtt/weekend_test/env.yml create mode 100644 components/mqtt/weekend_test/mqtt_publish_test.py diff --git a/components/mqtt/Kconfig b/components/mqtt/Kconfig index de79975..ec05dc6 100644 --- a/components/mqtt/Kconfig +++ b/components/mqtt/Kconfig @@ -78,6 +78,14 @@ menu "ESP-MQTT Configurations" help MQTT task stack size + config MQTT_DISABLE_API_LOCKS + bool "Disable API locks" + default n + depends on MQTT_USE_CUSTOM_CONFIG + help + Default config employs API locks to protect internal structures. It is possible to disable + these locks if the user code doesn't access MQTT API from multiple concurrent tasks + config MQTT_TASK_CORE_SELECTION_ENABLED bool "Enable MQTT task core selection" default false diff --git a/components/mqtt/weekend_test/config.yml b/components/mqtt/weekend_test/config.yml new file mode 100644 index 0000000..8acf214 --- /dev/null +++ b/components/mqtt/weekend_test/config.yml @@ -0,0 +1,3 @@ +CaseConfig: +- name: test_weekend_mqtt_publish + diff --git a/components/mqtt/weekend_test/env.yml b/components/mqtt/weekend_test/env.yml new file mode 100644 index 0000000..e69de29 diff --git a/components/mqtt/weekend_test/mqtt_publish_test.py b/components/mqtt/weekend_test/mqtt_publish_test.py new file mode 100644 index 0000000..474bcd8 --- /dev/null +++ b/components/mqtt/weekend_test/mqtt_publish_test.py @@ -0,0 +1,190 @@ +from __future__ import print_function +from __future__ import unicode_literals +from builtins import str +import re +import os +import sys +import ssl +import paho.mqtt.client as mqtt +from threading import Thread, Event +import time +import string +import random + +try: + import IDF +except ImportError: + # this is a test case write with tiny-test-fw. + # to run test cases outside tiny-test-fw, + # we need to set environment variable `TEST_FW_PATH`, + # then get and insert `TEST_FW_PATH` to sys path before import FW module + test_fw_path = os.getenv("TEST_FW_PATH") + if test_fw_path and test_fw_path not in sys.path: + sys.path.insert(0, test_fw_path) + import IDF + +import DUT + + +event_client_connected = Event() +event_stop_client = Event() +event_client_received_correct = Event() +message_log = "" +broker_host = {} +broker_port = {} +expected_data = "" +subscribe_topic = "" +publish_topic = "" +expected_count = 0 + + +# The callback for when the client receives a CONNACK response from the server. +def on_connect(client, userdata, flags, rc): + print("Connected with result code " + str(rc)) + event_client_connected.set() + client.subscribe("/topic/qos0") + + +def mqtt_client_task(client): + while not event_stop_client.is_set(): + client.loop() + + +def get_host_port_from_dut(dut1, config_option): + value = re.search(r'\:\/\/([^:]+)\:([0-9]+)', dut1.app.get_sdkconfig()[config_option]) + return value.group(1), int(value.group(2)) + + +# The callback for when a PUBLISH message is received from the server. +def on_message(client, userdata, msg): + global message_log + global expected_count + payload = msg.payload.decode() + if payload == expected_data: + expected_count += 1 + print("[{}] Received...".format(msg.mid)) + message_log += "Received data:" + msg.topic + " " + payload + "\n" + + +def test_single_config(dut, transport, qos, repeat, published): + global expected_count + global expected_data + global message_log + sample_string = ''.join(random.choice(string.ascii_uppercase + string.ascii_lowercase + string.digits) for _ in range(16)) + print("PUBLISH TEST: transport:{}, qos:{}, sequence:{}, sample msg:{}".format(transport, qos, published, sample_string)) + event_client_connected.clear() + expected_count = 0 + message_log = "" + expected_data = sample_string * repeat + client = None + try: + if transport in ["ws", "wss"]: + client = mqtt.Client(transport="websockets") + client.ws_set_options(path="/ws", headers=None) + else: + client = mqtt.Client() + client.on_connect = on_connect + client.on_message = on_message + if transport in ["ssl", "wss"]: + client.tls_set(None, None, None, cert_reqs=ssl.CERT_NONE, tls_version=ssl.PROTOCOL_TLSv1, ciphers=None) + client.tls_insecure_set(True) + print("Connecting...") + client.connect(broker_host[transport], broker_port[transport], 60) + except Exception: + print("ENV_TEST_FAILURE: Unexpected error while connecting to broker {}: {}:".format(broker_host[transport], sys.exc_info()[0])) + raise + # Starting a py-client in a separate thread + thread1 = Thread(target=mqtt_client_task, args=(client,)) + thread1.start() + print("Connecting py-client to broker {}:{}...".format(broker_host[transport], broker_port[transport])) + if not event_client_connected.wait(timeout=30): + raise ValueError("ENV_TEST_FAILURE: Test script cannot connect to broker: {}".format(broker_host[transport])) + client.subscribe(subscribe_topic, qos) + dut.write("{} {} {} {} {}".format(transport, sample_string, repeat, published, qos), eol="\n") + try: + # waiting till subscribed to defined topic + dut.expect(re.compile(r"MQTT_EVENT_SUBSCRIBED"), timeout=30) + for i in range(published): + client.publish(publish_topic, sample_string * repeat, qos) + print("Publishing...") + print("Checking esp-client received msg published from py-client...") + dut.expect(re.compile(r"Correct pattern received exactly x times"), timeout=60) + start = time.time() + while expected_count < published and time.time() - start <= 60: + time.sleep(1) + # Note: tolerate that messages qos=1 to be received more than once + if expected_count == published or (expected_count > published and qos == 1): + print("All data received from ESP32...") + else: + raise ValueError("Not all data received from ESP32: Expected:{}x{}, Received:{}x{}".format(expected_data, published, message_log, expected_count)) + finally: + event_stop_client.set() + thread1.join() + client.disconnect() + event_stop_client.clear() + + +@IDF.idf_example_test(env_tag="Example_WIFI") +def test_weekend_mqtt_publish(env, extra_data): + # Using broker url dictionary for different transport + global broker_host + global broker_port + global publish_topic + global subscribe_topic + """ + steps: | + 1. join AP and connects to ssl broker + 2. Test connects a client to the same broker + 3. Test evaluates python client received correct qos0 message + 4. Test ESP32 client received correct qos0 message + """ + dut1 = env.get_dut("mqtt_publish", "examples/protocols/mqtt/publish_test") + # check and log bin size + binary_file = os.path.join(dut1.app.binary_path, "mqtt_publish.bin") + bin_size = os.path.getsize(binary_file) + IDF.log_performance("mqtt_publish_bin_size", "{}KB" + .format(bin_size // 1024)) + IDF.check_performance("mqtt_publish_size", bin_size // 1024) + # Look for host:port in sdkconfig + try: + # python client subscribes to the topic to which esp client publishes and vice versa + publish_topic = dut1.app.get_sdkconfig()["CONFIG_SUBSCIBE_TOPIC"].replace('"','') + subscribe_topic = dut1.app.get_sdkconfig()["CONFIG_PUBLISH_TOPIC"].replace('"','') + broker_host["ssl"], broker_port["ssl"] = get_host_port_from_dut(dut1, "CONFIG_BROKER_SSL_URI") + broker_host["tcp"], broker_port["tcp"] = get_host_port_from_dut(dut1, "CONFIG_BROKER_TCP_URI") + broker_host["ws"], broker_port["ws"] = get_host_port_from_dut(dut1, "CONFIG_BROKER_WS_URI") + broker_host["wss"], broker_port["wss"] = get_host_port_from_dut(dut1, "CONFIG_BROKER_WSS_URI") + except Exception: + print('ENV_TEST_FAILURE: Cannot find broker url in sdkconfig') + raise + dut1.start_app() + try: + ip_address = dut1.expect(re.compile(r" sta ip: ([^,]+),"), timeout=30) + print("Connected to AP with IP: {}".format(ip_address)) + except DUT.ExpectTimeout: + print('ENV_TEST_FAILURE: Cannot connect to AP') + raise + for qos in [0, 1, 2]: + for transport in ["tcp", "ssl", "ws", "wss"]: + # decide on broker what level of test will pass (local broker works the best) + if broker_host[transport].startswith("192.168"): + # medium size, medium repeated + test_single_config(dut1, transport, qos, 5, 50) + # long data + test_single_config(dut1, transport, qos, 1000, 10) + # short data, many repeats + test_single_config(dut1, transport, qos, 2, 200) + elif transport in ["ws", "wss"]: + # more relaxed criteria for websockets! + test_single_config(dut1, transport, qos, 2, 5) + test_single_config(dut1, transport, qos, 50, 1) + test_single_config(dut1, transport, qos, 10, 20) + else: + # common configuration should be good for most public mosquittos + test_single_config(dut1, transport, qos, 5, 10) + test_single_config(dut1, transport, qos, 500, 3) + test_single_config(dut1, transport, qos, 1, 50) + + +if __name__ == '__main__': + test_weekend_mqtt_publish() From 6809fcb2df3cc3f1c9998e584c08f8132f6c6bf6 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Tue, 30 Apr 2019 11:41:39 +0200 Subject: [PATCH 15/91] tcp_transport: modified ws_read to read payload directly to the read buffer and separately from header bytes Previous version read all data to the buffer including header which reduced maximum payload read. This version uses a local array to receive header and reads payload bytes to the buffer --- components/mqtt/esp-mqtt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/mqtt/esp-mqtt b/components/mqtt/esp-mqtt index 39118d5..18b6f2c 160000 --- a/components/mqtt/esp-mqtt +++ b/components/mqtt/esp-mqtt @@ -1 +1 @@ -Subproject commit 39118d5182f804edc01ed030c8ba137a9c7f388e +Subproject commit 18b6f2c58231728805ff813d46021d07ed023dcb From 7f77ad063d81e8722483c3a85b43b4e60415b1f7 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Mon, 6 May 2019 16:14:48 +0200 Subject: [PATCH 16/91] mqtt_tests: add weekend test for sending and receiving empty payload messages, update config options per new naming convetions --- .../mqtt/weekend_test/mqtt_publish_test.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/components/mqtt/weekend_test/mqtt_publish_test.py b/components/mqtt/weekend_test/mqtt_publish_test.py index 474bcd8..9263f3a 100644 --- a/components/mqtt/weekend_test/mqtt_publish_test.py +++ b/components/mqtt/weekend_test/mqtt_publish_test.py @@ -71,11 +71,11 @@ def test_single_config(dut, transport, qos, repeat, published): global expected_data global message_log sample_string = ''.join(random.choice(string.ascii_uppercase + string.ascii_lowercase + string.digits) for _ in range(16)) - print("PUBLISH TEST: transport:{}, qos:{}, sequence:{}, sample msg:{}".format(transport, qos, published, sample_string)) event_client_connected.clear() expected_count = 0 message_log = "" expected_data = sample_string * repeat + print("PUBLISH TEST: transport:{}, qos:{}, sequence:{}, sample msg:'{}'".format(transport, qos, published, expected_data)) client = None try: if transport in ["ws", "wss"]: @@ -148,12 +148,12 @@ def test_weekend_mqtt_publish(env, extra_data): # Look for host:port in sdkconfig try: # python client subscribes to the topic to which esp client publishes and vice versa - publish_topic = dut1.app.get_sdkconfig()["CONFIG_SUBSCIBE_TOPIC"].replace('"','') - subscribe_topic = dut1.app.get_sdkconfig()["CONFIG_PUBLISH_TOPIC"].replace('"','') - broker_host["ssl"], broker_port["ssl"] = get_host_port_from_dut(dut1, "CONFIG_BROKER_SSL_URI") - broker_host["tcp"], broker_port["tcp"] = get_host_port_from_dut(dut1, "CONFIG_BROKER_TCP_URI") - broker_host["ws"], broker_port["ws"] = get_host_port_from_dut(dut1, "CONFIG_BROKER_WS_URI") - broker_host["wss"], broker_port["wss"] = get_host_port_from_dut(dut1, "CONFIG_BROKER_WSS_URI") + publish_topic = dut1.app.get_sdkconfig()["CONFIG_EXAMPLE_SUBSCIBE_TOPIC"].replace('"','') + subscribe_topic = dut1.app.get_sdkconfig()["CONFIG_EXAMPLE_PUBLISH_TOPIC"].replace('"','') + broker_host["ssl"], broker_port["ssl"] = get_host_port_from_dut(dut1, "CONFIG_EXAMPLE_BROKER_SSL_URI") + broker_host["tcp"], broker_port["tcp"] = get_host_port_from_dut(dut1, "CONFIG_EXAMPLE_BROKER_TCP_URI") + broker_host["ws"], broker_port["ws"] = get_host_port_from_dut(dut1, "CONFIG_EXAMPLE_BROKER_WS_URI") + broker_host["wss"], broker_port["wss"] = get_host_port_from_dut(dut1, "CONFIG_EXAMPLE_BROKER_WSS_URI") except Exception: print('ENV_TEST_FAILURE: Cannot find broker url in sdkconfig') raise @@ -166,8 +166,10 @@ def test_weekend_mqtt_publish(env, extra_data): raise for qos in [0, 1, 2]: for transport in ["tcp", "ssl", "ws", "wss"]: + # simple test with empty message + test_single_config(dut1, transport, qos, 0, 5) # decide on broker what level of test will pass (local broker works the best) - if broker_host[transport].startswith("192.168"): + if broker_host[transport].startswith("192.168") and qos < 1: # medium size, medium repeated test_single_config(dut1, transport, qos, 5, 50) # long data From 3a2e82ec0f15b62141e45bb1f97073587c0532b7 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Wed, 17 Apr 2019 15:56:59 +0200 Subject: [PATCH 17/91] mqtt: added support for esp event loop, updating examples to register and use event loop handler --- components/mqtt/esp-mqtt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/mqtt/esp-mqtt b/components/mqtt/esp-mqtt index 18b6f2c..e205913 160000 --- a/components/mqtt/esp-mqtt +++ b/components/mqtt/esp-mqtt @@ -1 +1 @@ -Subproject commit 18b6f2c58231728805ff813d46021d07ed023dcb +Subproject commit e205913b2cf3eff20b1f8ced7c76cf03533f130b From 332fadfea620dbafd55deaa35e44fa58ed91be11 Mon Sep 17 00:00:00 2001 From: Tuan Date: Thu, 20 Jun 2019 15:37:40 +0800 Subject: [PATCH 18/91] esp_websocket_client: Add websocket client component Closes https://github.com/espressif/esp-idf/issues/2829 --- components/mqtt/esp-mqtt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/mqtt/esp-mqtt b/components/mqtt/esp-mqtt index e205913..11f8846 160000 --- a/components/mqtt/esp-mqtt +++ b/components/mqtt/esp-mqtt @@ -1 +1 @@ -Subproject commit e205913b2cf3eff20b1f8ced7c76cf03533f130b +Subproject commit 11f884623bd32cb4269f24f47847f5d046da93f5 From 63b3f29ffc417fb32053b3b9a35101052c0f2563 Mon Sep 17 00:00:00 2001 From: Renz Christian Bagaporo Date: Sun, 28 Apr 2019 15:38:23 +0800 Subject: [PATCH 19/91] components: use new component registration api --- components/mqtt/CMakeLists.txt | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/components/mqtt/CMakeLists.txt b/components/mqtt/CMakeLists.txt index 85f63cc..1c30e3a 100644 --- a/components/mqtt/CMakeLists.txt +++ b/components/mqtt/CMakeLists.txt @@ -1,10 +1,7 @@ -set(COMPONENT_ADD_INCLUDEDIRS esp-mqtt/include) -set(COMPONENT_PRIV_INCLUDEDIRS "esp-mqtt/lib/include") -set(COMPONENT_SRCS "esp-mqtt/mqtt_client.c" - "esp-mqtt/lib/mqtt_msg.c" - "esp-mqtt/lib/mqtt_outbox.c" - "esp-mqtt/lib/platform_esp32_idf.c") - -set(COMPONENT_REQUIRES lwip nghttp mbedtls tcp_transport) - -register_component() +idf_component_register(SRCS "esp-mqtt/mqtt_client.c" + "esp-mqtt/lib/mqtt_msg.c" + "esp-mqtt/lib/mqtt_outbox.c" + "esp-mqtt/lib/platform_esp32_idf.c" + INCLUDE_DIRS esp-mqtt/include + PRIV_INCLUDE_DIRS "esp-mqtt/lib/include" + REQUIRES lwip nghttp mbedtls tcp_transport) From a903e5e3a962243f379fbd7b2d728a69cfaa0313 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Tue, 9 Apr 2019 16:08:05 +0200 Subject: [PATCH 20/91] esp-tls: capturing specific errors to be available in tcp_transport and then in application code --- components/mqtt/esp-mqtt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/mqtt/esp-mqtt b/components/mqtt/esp-mqtt index 11f8846..65bf225 160000 --- a/components/mqtt/esp-mqtt +++ b/components/mqtt/esp-mqtt @@ -1 +1 @@ -Subproject commit 11f884623bd32cb4269f24f47847f5d046da93f5 +Subproject commit 65bf2255d74c79e3c6abe3e3f791ba8d42efdee3 From 49296d851d49764f26b12c0f209801abbbdbfc28 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Tue, 16 Apr 2019 11:58:38 +0200 Subject: [PATCH 21/91] esp-tls: extending error handle to contain error descriptors with last mbedtls failure and latest certificate verification result flags, reworked tcp_transport to use this error handle --- components/mqtt/esp-mqtt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/mqtt/esp-mqtt b/components/mqtt/esp-mqtt index 65bf225..0cc4077 160000 --- a/components/mqtt/esp-mqtt +++ b/components/mqtt/esp-mqtt @@ -1 +1 @@ -Subproject commit 65bf2255d74c79e3c6abe3e3f791ba8d42efdee3 +Subproject commit 0cc4077bd3e10bb93456ff2785309ec9237a5906 From bef4fce9c114c19598f150e9137e24b530a37a0d Mon Sep 17 00:00:00 2001 From: David Cermak Date: Wed, 17 Jul 2019 20:52:53 +0200 Subject: [PATCH 22/91] mqtt: referenced esp-mqtt master to close disconnection issues and fix static analysis warnings closes https://github.com/espressif/esp-idf/issues/3619 including mqtt commit https://github.com/espressif/esp-mqtt/commit/7223302deb3ffefe60cc645c6d792fd5e4d6259c closes https://github.com/espressif/esp-idf/issues/3215 including mqtt commit https://github.com/espressif/esp-mqtt/commit/caf5007b99df985b330683dfe2fa454c94633018 --- components/mqtt/esp-mqtt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/mqtt/esp-mqtt b/components/mqtt/esp-mqtt index 0cc4077..dc37d3a 160000 --- a/components/mqtt/esp-mqtt +++ b/components/mqtt/esp-mqtt @@ -1 +1 @@ -Subproject commit 0cc4077bd3e10bb93456ff2785309ec9237a5906 +Subproject commit dc37d3a065f345a7358b8ff4553db0baceeb8ad6 From 5a50fc0812192050790054a6f7a1789f75e06cd6 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Thu, 23 May 2019 21:48:08 +0200 Subject: [PATCH 23/91] esp_tls: enable psk verification mode, added mqtt example using psk authentication --- components/mqtt/esp-mqtt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/mqtt/esp-mqtt b/components/mqtt/esp-mqtt index dc37d3a..117eef2 160000 --- a/components/mqtt/esp-mqtt +++ b/components/mqtt/esp-mqtt @@ -1 +1 @@ -Subproject commit dc37d3a065f345a7358b8ff4553db0baceeb8ad6 +Subproject commit 117eef2dad54e0f9e25b3005fcfc18e7695ff29e From 25f423d69bc53c58d9a682031a08d4af0f0504e0 Mon Sep 17 00:00:00 2001 From: "Michael (XIAO Xufeng)" Date: Thu, 29 Aug 2019 17:54:14 +0800 Subject: [PATCH 24/91] ci: limit example test to ESP32s --- components/mqtt/weekend_test/mqtt_publish_test.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/components/mqtt/weekend_test/mqtt_publish_test.py b/components/mqtt/weekend_test/mqtt_publish_test.py index 9263f3a..f3d081b 100644 --- a/components/mqtt/weekend_test/mqtt_publish_test.py +++ b/components/mqtt/weekend_test/mqtt_publish_test.py @@ -13,6 +13,7 @@ import random try: import IDF + from IDF.IDFDUT import ESP32DUT except ImportError: # this is a test case write with tiny-test-fw. # to run test cases outside tiny-test-fw, @@ -138,7 +139,7 @@ def test_weekend_mqtt_publish(env, extra_data): 3. Test evaluates python client received correct qos0 message 4. Test ESP32 client received correct qos0 message """ - dut1 = env.get_dut("mqtt_publish", "examples/protocols/mqtt/publish_test") + dut1 = env.get_dut("mqtt_publish", "examples/protocols/mqtt/publish_test", dut_class=ESP32DUT) # check and log bin size binary_file = os.path.join(dut1.app.binary_path, "mqtt_publish.bin") bin_size = os.path.getsize(binary_file) From b4ba09ce38af662240143623ce6b21540f127a45 Mon Sep 17 00:00:00 2001 From: liu zhifu Date: Fri, 5 Jul 2019 16:58:04 +0800 Subject: [PATCH 25/91] esp_wifi: wifi support new event mechanism 1. WiFi support new event mechanism 2. Update examples to use new event mechanism --- components/mqtt/esp-mqtt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/mqtt/esp-mqtt b/components/mqtt/esp-mqtt index 117eef2..fb3d210 160000 --- a/components/mqtt/esp-mqtt +++ b/components/mqtt/esp-mqtt @@ -1 +1 @@ -Subproject commit 117eef2dad54e0f9e25b3005fcfc18e7695ff29e +Subproject commit fb3d2107cdac440d84f2fab81ea9b5217aa4ba1f From 37e511bef14d723d943e0e15228f17df8ea79715 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Fri, 27 Sep 2019 14:44:55 +0200 Subject: [PATCH 26/91] ci: fix weekend test confguration update per latest refactoring of grouping tests --- .../mqtt/weekend_test/{config.yml => test_weekend_mqtt_.yml} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename components/mqtt/weekend_test/{config.yml => test_weekend_mqtt_.yml} (100%) diff --git a/components/mqtt/weekend_test/config.yml b/components/mqtt/weekend_test/test_weekend_mqtt_.yml similarity index 100% rename from components/mqtt/weekend_test/config.yml rename to components/mqtt/weekend_test/test_weekend_mqtt_.yml From 6cadfe519bbbfc9f1f576eff2ac7ae2a46f20224 Mon Sep 17 00:00:00 2001 From: Marius Vikhammer Date: Fri, 27 Sep 2019 09:35:26 +0800 Subject: [PATCH 27/91] MQTT: update default broker URL for examples The MQTT broker URL used as default in the examples has ceased operation. All examples and documention have been updated to point to the new domain mqtt.eclipse.org. This also required an update of the python example test scripts to use TLS 1.2 --- components/mqtt/weekend_test/mqtt_publish_test.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/components/mqtt/weekend_test/mqtt_publish_test.py b/components/mqtt/weekend_test/mqtt_publish_test.py index f3d081b..55e0407 100644 --- a/components/mqtt/weekend_test/mqtt_publish_test.py +++ b/components/mqtt/weekend_test/mqtt_publish_test.py @@ -81,13 +81,12 @@ def test_single_config(dut, transport, qos, repeat, published): try: if transport in ["ws", "wss"]: client = mqtt.Client(transport="websockets") - client.ws_set_options(path="/ws", headers=None) else: client = mqtt.Client() client.on_connect = on_connect client.on_message = on_message if transport in ["ssl", "wss"]: - client.tls_set(None, None, None, cert_reqs=ssl.CERT_NONE, tls_version=ssl.PROTOCOL_TLSv1, ciphers=None) + client.tls_set(None, None, None, cert_reqs=ssl.CERT_NONE, tls_version=ssl.PROTOCOL_TLSv1_2, ciphers=None) client.tls_insecure_set(True) print("Connecting...") client.connect(broker_host[transport], broker_port[transport], 60) From 62f3dc4705161e45a04ecf0f6c1013c753a3caf8 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Wed, 9 Oct 2019 11:03:26 +0200 Subject: [PATCH 28/91] mqtt: updated to latest version to include latest fixes, support for global CA store, extended error structure to receive mqtt specific errors. updated idf ssl example to use this error struct https://github.com/espressif/esp-mqtt/issues/135 --- components/mqtt/esp-mqtt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/mqtt/esp-mqtt b/components/mqtt/esp-mqtt index fb3d210..e3b013e 160000 --- a/components/mqtt/esp-mqtt +++ b/components/mqtt/esp-mqtt @@ -1 +1 @@ -Subproject commit fb3d2107cdac440d84f2fab81ea9b5217aa4ba1f +Subproject commit e3b013e2db58124ea68cf7c8f44a8cba6e1572b7 From 8a24bebbc082d39e7db332ff9b9f8e3788206850 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Wed, 4 Dec 2019 14:21:29 +0100 Subject: [PATCH 29/91] ci: updated mqtt weekend test for qemu support Added default sdkconfig for qemu build for the mqtt publish example, Added environment configuration for running the same test on target or in qemu Updated missing example tests per latest ttfw refactoring --- .../mqtt/weekend_test/mqtt_publish_test.py | 34 +++++++------------ .../weekend_test/test_weekend_mqtt_qemu.yml | 7 ++++ 2 files changed, 20 insertions(+), 21 deletions(-) create mode 100644 components/mqtt/weekend_test/test_weekend_mqtt_qemu.yml diff --git a/components/mqtt/weekend_test/mqtt_publish_test.py b/components/mqtt/weekend_test/mqtt_publish_test.py index 55e0407..32ae338 100644 --- a/components/mqtt/weekend_test/mqtt_publish_test.py +++ b/components/mqtt/weekend_test/mqtt_publish_test.py @@ -11,20 +11,8 @@ import time import string import random -try: - import IDF - from IDF.IDFDUT import ESP32DUT -except ImportError: - # this is a test case write with tiny-test-fw. - # to run test cases outside tiny-test-fw, - # we need to set environment variable `TEST_FW_PATH`, - # then get and insert `TEST_FW_PATH` to sys path before import FW module - test_fw_path = os.getenv("TEST_FW_PATH") - if test_fw_path and test_fw_path not in sys.path: - sys.path.insert(0, test_fw_path) - import IDF - -import DUT +from tiny_test_fw import DUT +import ttfw_idf event_client_connected = Event() @@ -53,6 +41,8 @@ def mqtt_client_task(client): def get_host_port_from_dut(dut1, config_option): value = re.search(r'\:\/\/([^:]+)\:([0-9]+)', dut1.app.get_sdkconfig()[config_option]) + if value is None: + return None, None return value.group(1), int(value.group(2)) @@ -124,7 +114,7 @@ def test_single_config(dut, transport, qos, repeat, published): event_stop_client.clear() -@IDF.idf_example_test(env_tag="Example_WIFI") +@ttfw_idf.idf_example_test(env_tag="Example_WIFI") def test_weekend_mqtt_publish(env, extra_data): # Using broker url dictionary for different transport global broker_host @@ -138,13 +128,12 @@ def test_weekend_mqtt_publish(env, extra_data): 3. Test evaluates python client received correct qos0 message 4. Test ESP32 client received correct qos0 message """ - dut1 = env.get_dut("mqtt_publish", "examples/protocols/mqtt/publish_test", dut_class=ESP32DUT) + dut1 = env.get_dut("mqtt_publish", "examples/protocols/mqtt/publish_test") # check and log bin size binary_file = os.path.join(dut1.app.binary_path, "mqtt_publish.bin") bin_size = os.path.getsize(binary_file) - IDF.log_performance("mqtt_publish_bin_size", "{}KB" - .format(bin_size // 1024)) - IDF.check_performance("mqtt_publish_size", bin_size // 1024) + ttfw_idf.log_performance("mqtt_publish_bin_size", "{}KB".format(bin_size // 1024)) + ttfw_idf.check_performance("mqtt_publish_size", bin_size // 1024) # Look for host:port in sdkconfig try: # python client subscribes to the topic to which esp client publishes and vice versa @@ -159,13 +148,16 @@ def test_weekend_mqtt_publish(env, extra_data): raise dut1.start_app() try: - ip_address = dut1.expect(re.compile(r" sta ip: ([^,]+),"), timeout=30) + ip_address = dut1.expect(re.compile(r" IPv4 address: ([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)"), timeout=30) print("Connected to AP with IP: {}".format(ip_address)) except DUT.ExpectTimeout: print('ENV_TEST_FAILURE: Cannot connect to AP') raise for qos in [0, 1, 2]: for transport in ["tcp", "ssl", "ws", "wss"]: + if broker_host[transport] is None: + print('Skipping transport: {}...'.format(transport)) + continue # simple test with empty message test_single_config(dut1, transport, qos, 0, 5) # decide on broker what level of test will pass (local broker works the best) @@ -189,4 +181,4 @@ def test_weekend_mqtt_publish(env, extra_data): if __name__ == '__main__': - test_weekend_mqtt_publish() + test_weekend_mqtt_publish(dut=ttfw_idf.ESP32QEMUDUT if sys.argv[1:] == ['qemu'] else ttfw_idf.ESP32DUT) diff --git a/components/mqtt/weekend_test/test_weekend_mqtt_qemu.yml b/components/mqtt/weekend_test/test_weekend_mqtt_qemu.yml new file mode 100644 index 0000000..e4a72ec --- /dev/null +++ b/components/mqtt/weekend_test/test_weekend_mqtt_qemu.yml @@ -0,0 +1,7 @@ +CaseConfig: +- name: test_weekend_mqtt_publish + overwrite: + dut: + class: ESP32QEMUDUT + package: ttfw_idf + From 0178bafba882932fb693fc4c93f28b4546fdb391 Mon Sep 17 00:00:00 2001 From: Marius Vikhammer Date: Mon, 6 Jan 2020 18:14:06 +0800 Subject: [PATCH 30/91] mqtt: update submodule to point to latest commit. Adds bugfixes for: - Too early publishing - Potential mutex memory leak - CI related issues. - Wait for entire connack message - Event loop not getting cleaned up Adds support for ALPN, configurable reconnect time, QEMU CI tests and password protected client key. MQTT MR: https://gitlab.espressif.cn:6688/espressif/esp-mqtt/merge_requests/46 Closes IDF-1162 Closes https://github.com/espressif/esp-mqtt/issues/137 MQTT MR: https://gitlab.espressif.cn:6688/espressif/esp-mqtt/merge_requests/47 Closes IDF-1126 MQTT MR: https://gitlab.espressif.cn:6688/espressif/esp-mqtt/merge_requests/48 Closes IDFGH-2197 Closes https://github.com/espressif/esp-idf/issues/4349 Closes https://github.com/espressif/esp-mqtt/issues/140 MQTT MR: https://gitlab.espressif.cn:6688/espressif/esp-mqtt/merge_requests/48 Closes IDFGH-2235 Closes https://github.com/espressif/esp-idf/issues/4384 MQTT MR: https://gitlab.espressif.cn:6688/espressif/esp-mqtt/merge_requests/49 Closes https://github.com/espressif/esp-idf/issues/4433 Closes IDFGH-2293 MQTT MR: https://gitlab.espressif.cn:6688/espressif/esp-mqtt/merge_requests/50 Closes FCS-254 MQTT MR: https://gitlab.espressif.cn:6688/espressif/esp-mqtt/merge_requests/53 Closes FCS-267 --- components/mqtt/esp-mqtt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/mqtt/esp-mqtt b/components/mqtt/esp-mqtt index e3b013e..86fc8b7 160000 --- a/components/mqtt/esp-mqtt +++ b/components/mqtt/esp-mqtt @@ -1 +1 @@ -Subproject commit e3b013e2db58124ea68cf7c8f44a8cba6e1572b7 +Subproject commit 86fc8b7584f7f3aebf422843d84a26655e485fbe From 5be3f7b5d41d5950aa5d598ca7c6afae79087a9d Mon Sep 17 00:00:00 2001 From: David Cermak Date: Sun, 19 Jan 2020 21:24:28 +0100 Subject: [PATCH 31/91] mqtt: add basic set of unit tests --- components/mqtt/esp-mqtt | 2 +- components/mqtt/test/CMakeLists.txt | 2 + components/mqtt/test/component.mk | 4 ++ components/mqtt/test/test_mqtt.c | 70 +++++++++++++++++++++++++++++ 4 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 components/mqtt/test/CMakeLists.txt create mode 100644 components/mqtt/test/component.mk create mode 100644 components/mqtt/test/test_mqtt.c diff --git a/components/mqtt/esp-mqtt b/components/mqtt/esp-mqtt index 86fc8b7..9e20c7a 160000 --- a/components/mqtt/esp-mqtt +++ b/components/mqtt/esp-mqtt @@ -1 +1 @@ -Subproject commit 86fc8b7584f7f3aebf422843d84a26655e485fbe +Subproject commit 9e20c7ae3d6951cab3ed8dc66a0467da5bf37f16 diff --git a/components/mqtt/test/CMakeLists.txt b/components/mqtt/test/CMakeLists.txt new file mode 100644 index 0000000..867c584 --- /dev/null +++ b/components/mqtt/test/CMakeLists.txt @@ -0,0 +1,2 @@ +idf_component_register(SRC_DIRS "." + PRIV_REQUIRES unity test_utils mqtt nvs_flash app_update) \ No newline at end of file diff --git a/components/mqtt/test/component.mk b/components/mqtt/test/component.mk new file mode 100644 index 0000000..5be8734 --- /dev/null +++ b/components/mqtt/test/component.mk @@ -0,0 +1,4 @@ +# +#Component Makefile +# +COMPONENT_ADD_LDFLAGS = -Wl,--whole-archive -l$(COMPONENT_NAME) -Wl,--no-whole-archive \ No newline at end of file diff --git a/components/mqtt/test/test_mqtt.c b/components/mqtt/test/test_mqtt.c new file mode 100644 index 0000000..12cd177 --- /dev/null +++ b/components/mqtt/test/test_mqtt.c @@ -0,0 +1,70 @@ +#include "test_utils.h" +#include "mqtt_client.h" +#include "unity.h" +#include +#include "nvs_flash.h" +#include "esp_ota_ops.h" + +static void test_leak_setup(const char * file, long line) +{ + uint8_t mac[6]; + struct timeval te; + gettimeofday(&te, NULL); // get current time + esp_read_mac(mac, ESP_MAC_WIFI_STA); + printf("%s:%ld: time=%ld.%lds, mac:" MACSTR "\n", file, line, te.tv_sec, te.tv_usec, MAC2STR(mac)); + unity_reset_leak_checks(); +} + +static const char* this_bin_addr(void) +{ + spi_flash_mmap_handle_t out_handle; + const void *binary_address; + const esp_partition_t* partition = esp_ota_get_running_partition(); + esp_partition_mmap(partition, 0, partition->size, SPI_FLASH_MMAP_DATA, &binary_address, &out_handle); + return binary_address; +} + +TEST_CASE("mqtt init with invalid url", "[mqtt][leaks=0]") +{ + test_leak_setup(__FILE__, __LINE__); + const esp_mqtt_client_config_t mqtt_cfg = { + .uri = "INVALID", + }; + esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg); + TEST_ASSERT_EQUAL(NULL, client ); +} + +TEST_CASE("mqtt init and deinit", "[mqtt][leaks=0]") +{ + test_leak_setup(__FILE__, __LINE__); + const esp_mqtt_client_config_t mqtt_cfg = { + // no connection takes place, but the uri has to be valid for init() to succeed + .uri = "mqtts://localhost:8883", + }; + esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg); + TEST_ASSERT_NOT_EQUAL(NULL, client ); + esp_mqtt_client_destroy(client); +} + +TEST_CASE("mqtt enqueue and destroy outbox", "[mqtt][leaks=0]") +{ + const char * bin_addr = this_bin_addr(); + test_leak_setup(__FILE__, __LINE__); + const int messages = 20; + const int size = 2000; + const esp_mqtt_client_config_t mqtt_cfg = { + // no connection takes place, but the uri has to be valid for init() to succeed + .uri = "mqtts://localhost:8883", + }; + esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg); + TEST_ASSERT_NOT_EQUAL(NULL, client ); + int bytes_before = esp_get_free_heap_size(); + for (int i=0; i Date: Tue, 21 Jan 2020 16:44:02 +0100 Subject: [PATCH 32/91] mqtt: example test to check connection with different ssl parameters --- components/mqtt/weekend_test/mqtt_publish_test.py | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/components/mqtt/weekend_test/mqtt_publish_test.py b/components/mqtt/weekend_test/mqtt_publish_test.py index 32ae338..e018ebf 100644 --- a/components/mqtt/weekend_test/mqtt_publish_test.py +++ b/components/mqtt/weekend_test/mqtt_publish_test.py @@ -2,7 +2,6 @@ from __future__ import print_function from __future__ import unicode_literals from builtins import str import re -import os import sys import ssl import paho.mqtt.client as mqtt @@ -128,12 +127,7 @@ def test_weekend_mqtt_publish(env, extra_data): 3. Test evaluates python client received correct qos0 message 4. Test ESP32 client received correct qos0 message """ - dut1 = env.get_dut("mqtt_publish", "examples/protocols/mqtt/publish_test") - # check and log bin size - binary_file = os.path.join(dut1.app.binary_path, "mqtt_publish.bin") - bin_size = os.path.getsize(binary_file) - ttfw_idf.log_performance("mqtt_publish_bin_size", "{}KB".format(bin_size // 1024)) - ttfw_idf.check_performance("mqtt_publish_size", bin_size // 1024) + dut1 = env.get_dut("mqtt_publish_connect_test", "examples/protocols/mqtt/publish_connect_test") # Look for host:port in sdkconfig try: # python client subscribes to the topic to which esp client publishes and vice versa From 5ba36d511ff818bf53f2f37fc6e4cedaceedddaf Mon Sep 17 00:00:00 2001 From: michael Date: Tue, 11 Feb 2020 10:41:47 +0800 Subject: [PATCH 33/91] ci: disable failed cases for s2 temporarily --- components/mqtt/test/test_mqtt.c | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/components/mqtt/test/test_mqtt.c b/components/mqtt/test/test_mqtt.c index 12cd177..7e6ed29 100644 --- a/components/mqtt/test/test_mqtt.c +++ b/components/mqtt/test/test_mqtt.c @@ -15,15 +15,6 @@ static void test_leak_setup(const char * file, long line) unity_reset_leak_checks(); } -static const char* this_bin_addr(void) -{ - spi_flash_mmap_handle_t out_handle; - const void *binary_address; - const esp_partition_t* partition = esp_ota_get_running_partition(); - esp_partition_mmap(partition, 0, partition->size, SPI_FLASH_MMAP_DATA, &binary_address, &out_handle); - return binary_address; -} - TEST_CASE("mqtt init with invalid url", "[mqtt][leaks=0]") { test_leak_setup(__FILE__, __LINE__); @@ -38,7 +29,7 @@ TEST_CASE("mqtt init and deinit", "[mqtt][leaks=0]") { test_leak_setup(__FILE__, __LINE__); const esp_mqtt_client_config_t mqtt_cfg = { - // no connection takes place, but the uri has to be valid for init() to succeed + // no connection takes place, but the uri has to be valid for init() to succeed .uri = "mqtts://localhost:8883", }; esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg); @@ -46,6 +37,16 @@ TEST_CASE("mqtt init and deinit", "[mqtt][leaks=0]") esp_mqtt_client_destroy(client); } +#if !TEMPORARY_DISABLED_FOR_TARGETS(ESP32S2) +static const char* this_bin_addr(void) +{ + spi_flash_mmap_handle_t out_handle; + const void *binary_address; + const esp_partition_t* partition = esp_ota_get_running_partition(); + esp_partition_mmap(partition, 0, partition->size, SPI_FLASH_MMAP_DATA, &binary_address, &out_handle); + return binary_address; +} + TEST_CASE("mqtt enqueue and destroy outbox", "[mqtt][leaks=0]") { const char * bin_addr = this_bin_addr(); @@ -53,7 +54,7 @@ TEST_CASE("mqtt enqueue and destroy outbox", "[mqtt][leaks=0]") const int messages = 20; const int size = 2000; const esp_mqtt_client_config_t mqtt_cfg = { - // no connection takes place, but the uri has to be valid for init() to succeed + // no connection takes place, but the uri has to be valid for init() to succeed .uri = "mqtts://localhost:8883", }; esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg); @@ -67,4 +68,5 @@ TEST_CASE("mqtt enqueue and destroy outbox", "[mqtt][leaks=0]") TEST_ASSERT_GREATER_OR_EQUAL(messages*size, bytes_before - bytes_after); esp_mqtt_client_destroy(client); -} \ No newline at end of file +} +#endif //!TEMPORARY_DISABLED_FOR_TARGETS(ESP32S2) From 605352c1c50760596d8750cfafc53dd78a4dcb15 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Thu, 20 Feb 2020 12:52:59 +0100 Subject: [PATCH 34/91] MQTT: Reference latest mqtt addressing c++ build and qos1/2 resend Closes https://github.com/espressif/esp-idf/issues/4787 --- components/mqtt/esp-mqtt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/mqtt/esp-mqtt b/components/mqtt/esp-mqtt index 9e20c7a..8a1e1a5 160000 --- a/components/mqtt/esp-mqtt +++ b/components/mqtt/esp-mqtt @@ -1 +1 @@ -Subproject commit 9e20c7ae3d6951cab3ed8dc66a0467da5bf37f16 +Subproject commit 8a1e1a5a9ffce89313aaa209375e1def893a4c73 From 29ce3c58692001ee12d66ebeea7df978646bbe3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Br=C3=A9livet?= Date: Tue, 17 Mar 2020 14:03:03 +0100 Subject: [PATCH 35/91] esp_mqtt: add option to configure mqtt task priority. Merges https://github.com/espressif/esp-idf/pull/4947 --- components/mqtt/Kconfig | 7 +++++++ components/mqtt/esp-mqtt | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/components/mqtt/Kconfig b/components/mqtt/Kconfig index ec05dc6..e0d5f31 100644 --- a/components/mqtt/Kconfig +++ b/components/mqtt/Kconfig @@ -86,6 +86,13 @@ menu "ESP-MQTT Configurations" Default config employs API locks to protect internal structures. It is possible to disable these locks if the user code doesn't access MQTT API from multiple concurrent tasks + config MQTT_TASK_PRIORITY + int "MQTT task priority" + default 5 + depends on MQTT_USE_CUSTOM_CONFIG + help + MQTT task priority. Higher number denotes higher priority. + config MQTT_TASK_CORE_SELECTION_ENABLED bool "Enable MQTT task core selection" default false diff --git a/components/mqtt/esp-mqtt b/components/mqtt/esp-mqtt index 8a1e1a5..d8e2081 160000 --- a/components/mqtt/esp-mqtt +++ b/components/mqtt/esp-mqtt @@ -1 +1 @@ -Subproject commit 8a1e1a5a9ffce89313aaa209375e1def893a4c73 +Subproject commit d8e20813326cb6c9fe0303fc5ba1813ccca53065 From 9db10b5b7bc9695adb48166f9490813df6e15675 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Thu, 19 Mar 2020 21:14:33 +0100 Subject: [PATCH 36/91] mqtt: reenable outbox unit tests for esp32s2 --- components/mqtt/test/test_mqtt.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/components/mqtt/test/test_mqtt.c b/components/mqtt/test/test_mqtt.c index 7e6ed29..1cf0618 100644 --- a/components/mqtt/test/test_mqtt.c +++ b/components/mqtt/test/test_mqtt.c @@ -37,7 +37,6 @@ TEST_CASE("mqtt init and deinit", "[mqtt][leaks=0]") esp_mqtt_client_destroy(client); } -#if !TEMPORARY_DISABLED_FOR_TARGETS(ESP32S2) static const char* this_bin_addr(void) { spi_flash_mmap_handle_t out_handle; @@ -69,4 +68,3 @@ TEST_CASE("mqtt enqueue and destroy outbox", "[mqtt][leaks=0]") esp_mqtt_client_destroy(client); } -#endif //!TEMPORARY_DISABLED_FOR_TARGETS(ESP32S2) From cb782cae1e427aae376f2e6afa6c6b26e91f413d Mon Sep 17 00:00:00 2001 From: David Cermak Date: Fri, 3 Apr 2020 16:43:55 +0200 Subject: [PATCH 37/91] mqtt-tests: rename tests to match the actual group --- components/mqtt/weekend_test/mqtt_publish_test.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/components/mqtt/weekend_test/mqtt_publish_test.py b/components/mqtt/weekend_test/mqtt_publish_test.py index e018ebf..e5a2f76 100644 --- a/components/mqtt/weekend_test/mqtt_publish_test.py +++ b/components/mqtt/weekend_test/mqtt_publish_test.py @@ -113,7 +113,7 @@ def test_single_config(dut, transport, qos, repeat, published): event_stop_client.clear() -@ttfw_idf.idf_example_test(env_tag="Example_WIFI") +@ttfw_idf.idf_custom_test(env_tag="Example_WIFI") def test_weekend_mqtt_publish(env, extra_data): # Using broker url dictionary for different transport global broker_host @@ -127,7 +127,7 @@ def test_weekend_mqtt_publish(env, extra_data): 3. Test evaluates python client received correct qos0 message 4. Test ESP32 client received correct qos0 message """ - dut1 = env.get_dut("mqtt_publish_connect_test", "examples/protocols/mqtt/publish_connect_test") + dut1 = env.get_dut("mqtt_publish_connect_test", "tools/test_apps/protocols/mqtt/publish_connect_test") # Look for host:port in sdkconfig try: # python client subscribes to the topic to which esp client publishes and vice versa From 9b79fe3d003bb4ff4d8a964f9f6df6b0e7ec0b25 Mon Sep 17 00:00:00 2001 From: Piyush Shah Date: Wed, 1 Apr 2020 22:51:38 +0530 Subject: [PATCH 38/91] esp_mqtt_abort_connection: Fixed an issue which could result in a race condition and subsequent crash --- components/mqtt/esp-mqtt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/mqtt/esp-mqtt b/components/mqtt/esp-mqtt index d8e2081..50dc010 160000 --- a/components/mqtt/esp-mqtt +++ b/components/mqtt/esp-mqtt @@ -1 +1 @@ -Subproject commit d8e20813326cb6c9fe0303fc5ba1813ccca53065 +Subproject commit 50dc010b97ed876657f1713cba7208b843a355ec From 691e7bca887683943b0d42193ef5b0c28e21df69 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Tue, 28 Apr 2020 16:30:28 +0200 Subject: [PATCH 39/91] mqtt: clenaup logs and docs esp_mqtt: Change an error print to use ESP_LOGE instead of ESP_LOGI Move Sending MQTT connect message log from Info to Debug level docs: Makes clear that publish API could block Change the message printed after MQTT connection failure Closes https://github.com/espressif/esp-idf/issues/5077 (by means of referencing commit https://github.com/espressif/esp-mqtt/commit/615aeae0c2fbf062269e19065054b8b3d087cd8e) --- components/mqtt/esp-mqtt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/mqtt/esp-mqtt b/components/mqtt/esp-mqtt index 50dc010..6bc94ad 160000 --- a/components/mqtt/esp-mqtt +++ b/components/mqtt/esp-mqtt @@ -1 +1 @@ -Subproject commit 50dc010b97ed876657f1713cba7208b843a355ec +Subproject commit 6bc94add892437d0fd50f26bfabe78c646648c13 From 8b3d3db10b2fde3d3ef8977686895c41551c7c78 Mon Sep 17 00:00:00 2001 From: Marius Vikhammer Date: Tue, 18 Aug 2020 15:51:14 +0800 Subject: [PATCH 40/91] MQTT: Update submodule reference SSL: add config option for skipping common name check esp-mqtt commit: https://github.com/espressif/esp-mqtt/commit/5e8950e681e5419d85e0ffc0d72a78cdeb894fd7) Closes https://github.com/espressif/esp-mqtt/issues/158 SSL: add support for tls with secure element (ATECC608A) esp-mqtt commit: https://github.com/espressif/esp-mqtt/commit/a7ff9afa3f82a10433bd0dfab8225e315b6f7213) Closes https://github.com/espressif/esp-mqtt/issues/156 Websocket: Allow the query part of the uri to be a part of the path esp-mqtt commit: https://github.com/espressif/esp-mqtt/commit/40b06deb10d27219c66ddb2e1a628caf0fc2547f) Closes https://github.com/espressif/esp-mqtt/issues/161 Config: Add check for consistency between config settings esp-mqtt commit: https://github.com/espressif/esp-mqtt/commit/8a412c147de5cce4b2c2a612915755cf551a1675) Add IDF version check for secure element feature esp-mqtt commit: https://github.com/espressif/esp-mqtt/commit/db4bce01ab6d8e4ad81e59995def48b5728d9b36) Fix esp_mqtt_client_stop deadlock esp-mqtt commit: https://github.com/espressif/esp-mqtt/commit/5e17dcaeb257f847386478119935ecd360f47377) Closes https://github.com/espressif/esp-mqtt/issues/163 Add dispatch error event for read errors esp-mqtt commit: https://github.com/espressif/esp-mqtt/commit/d4aaec08ff13b5a599f114cba8ada684901c7e54 Closes https://github.com/espressif/esp-idf/issues/5704 Cleanup expired messages when offline esp-mqtt commit: https://github.com/espressif/esp-mqtt/commit/bdadd77c6e24382234c1960600ac2bdf4a6ace9d Closes https://github.com/espressif/esp-idf/issues/5668 esp_mqtt_client_publish now returns msg id for QoS > 0 when offline esp-mqtt commit: https://github.com/espressif/esp-mqtt/commit/f7325bfa10c7cacabb76e58fcd2e1622a9e1b589 Add support for Digital Signature through ESP-TLS esp-mqtt commit: https://github.com/espressif/esp-mqtt/commit/7d8e59de0062ff9312c5e97b4a409d43271f5f81 --- components/mqtt/esp-mqtt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/mqtt/esp-mqtt b/components/mqtt/esp-mqtt index 6bc94ad..01594bf 160000 --- a/components/mqtt/esp-mqtt +++ b/components/mqtt/esp-mqtt @@ -1 +1 @@ -Subproject commit 6bc94add892437d0fd50f26bfabe78c646648c13 +Subproject commit 01594bf118ae502b5a0ead040446f2be75d26223 From 25b661c8eae6e3cb20f7b147897ff773fb8a0bf2 Mon Sep 17 00:00:00 2001 From: Marius Vikhammer Date: Mon, 10 Aug 2020 16:43:23 +0800 Subject: [PATCH 41/91] MQTT: add configurable msg expired timeout --- components/mqtt/Kconfig | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/components/mqtt/Kconfig b/components/mqtt/Kconfig index e0d5f31..fc38978 100644 --- a/components/mqtt/Kconfig +++ b/components/mqtt/Kconfig @@ -115,4 +115,11 @@ menu "ESP-MQTT Configurations" Set to true if a specific implementation of message outbox is needed (e.g. persistant outbox in NVM or similar). + config MQTT_OUTBOX_EXPIRED_TIMEOUT_MS + int "Outbox message expired timeout[ms]" + default 300000 + depends on MQTT_USE_CUSTOM_CONFIG + help + Messages which stays in the outbox longer than this value before being published will be discarded. + endmenu From 87d698cbc7dcb001db664c209847f803de279ced Mon Sep 17 00:00:00 2001 From: Jakob Hasse Date: Wed, 5 Aug 2020 10:16:32 +0800 Subject: [PATCH 42/91] cmock: added cmock as component * changing dependencies from unity->cmock * added component.mk and Makefile.projbuild * ignore test dir in gen_esp_err_to_name.py * added some brief introduction of CMock in IDF --- components/mqtt/test/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/mqtt/test/CMakeLists.txt b/components/mqtt/test/CMakeLists.txt index 867c584..bbe7825 100644 --- a/components/mqtt/test/CMakeLists.txt +++ b/components/mqtt/test/CMakeLists.txt @@ -1,2 +1,2 @@ idf_component_register(SRC_DIRS "." - PRIV_REQUIRES unity test_utils mqtt nvs_flash app_update) \ No newline at end of file + PRIV_REQUIRES cmock test_utils mqtt nvs_flash app_update) \ No newline at end of file From 6c55757f70ff34e12f46d5729d20b17d78763dce Mon Sep 17 00:00:00 2001 From: Angus Gratton Date: Tue, 10 Nov 2020 18:40:01 +1100 Subject: [PATCH 43/91] Whitespace: Automated whitespace fixes (large commit) Apply the pre-commit hook whitespace fixes to all files in the repo. (Line endings, blank lines at end of file, trailing whitespace) --- components/mqtt/test/CMakeLists.txt | 2 +- components/mqtt/test/component.mk | 2 +- components/mqtt/weekend_test/test_weekend_mqtt_.yml | 1 - components/mqtt/weekend_test/test_weekend_mqtt_qemu.yml | 1 - 4 files changed, 2 insertions(+), 4 deletions(-) diff --git a/components/mqtt/test/CMakeLists.txt b/components/mqtt/test/CMakeLists.txt index bbe7825..a86bbd0 100644 --- a/components/mqtt/test/CMakeLists.txt +++ b/components/mqtt/test/CMakeLists.txt @@ -1,2 +1,2 @@ idf_component_register(SRC_DIRS "." - PRIV_REQUIRES cmock test_utils mqtt nvs_flash app_update) \ No newline at end of file + PRIV_REQUIRES cmock test_utils mqtt nvs_flash app_update) diff --git a/components/mqtt/test/component.mk b/components/mqtt/test/component.mk index 5be8734..8c6eb51 100644 --- a/components/mqtt/test/component.mk +++ b/components/mqtt/test/component.mk @@ -1,4 +1,4 @@ # #Component Makefile # -COMPONENT_ADD_LDFLAGS = -Wl,--whole-archive -l$(COMPONENT_NAME) -Wl,--no-whole-archive \ No newline at end of file +COMPONENT_ADD_LDFLAGS = -Wl,--whole-archive -l$(COMPONENT_NAME) -Wl,--no-whole-archive diff --git a/components/mqtt/weekend_test/test_weekend_mqtt_.yml b/components/mqtt/weekend_test/test_weekend_mqtt_.yml index 8acf214..ae00e35 100644 --- a/components/mqtt/weekend_test/test_weekend_mqtt_.yml +++ b/components/mqtt/weekend_test/test_weekend_mqtt_.yml @@ -1,3 +1,2 @@ CaseConfig: - name: test_weekend_mqtt_publish - diff --git a/components/mqtt/weekend_test/test_weekend_mqtt_qemu.yml b/components/mqtt/weekend_test/test_weekend_mqtt_qemu.yml index e4a72ec..bd6f0d6 100644 --- a/components/mqtt/weekend_test/test_weekend_mqtt_qemu.yml +++ b/components/mqtt/weekend_test/test_weekend_mqtt_qemu.yml @@ -4,4 +4,3 @@ CaseConfig: dut: class: ESP32QEMUDUT package: ttfw_idf - From 5facf78673d049b7642f659a1fdf5882ebfcd0ed Mon Sep 17 00:00:00 2001 From: Axel Lin Date: Fri, 6 Nov 2020 20:42:38 +0800 Subject: [PATCH 44/91] MQTT: Restore default MQTT_OUTBOX_EXPIRED_TIMEOUT_MS to 30 sec The OUTBOX_EXPIRED_TIMEOUT_MS was 30*1000 in original esp-mqtt code. Don't change the default OUTBOX_EXPIRED_TIMEOUT_MS without good reason, which may has impact on memory usage for existing applications. Fixes: e931168ed695 ("MQTT: add configurable msg expired timeout") Signed-off-by: Axel Lin --- components/mqtt/Kconfig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/mqtt/Kconfig b/components/mqtt/Kconfig index fc38978..7ce41d9 100644 --- a/components/mqtt/Kconfig +++ b/components/mqtt/Kconfig @@ -117,7 +117,7 @@ menu "ESP-MQTT Configurations" config MQTT_OUTBOX_EXPIRED_TIMEOUT_MS int "Outbox message expired timeout[ms]" - default 300000 + default 30000 depends on MQTT_USE_CUSTOM_CONFIG help Messages which stays in the outbox longer than this value before being published will be discarded. From b342444746a0b8afefc206bbbdc8cf37c0820d88 Mon Sep 17 00:00:00 2001 From: Angus Gratton Date: Fri, 6 Nov 2020 15:03:03 +1100 Subject: [PATCH 45/91] riscv: Add new arch-level component Changes come from internal branch commit a6723fc --- components/mqtt/esp-mqtt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/mqtt/esp-mqtt b/components/mqtt/esp-mqtt index 01594bf..b9db8d9 160000 --- a/components/mqtt/esp-mqtt +++ b/components/mqtt/esp-mqtt @@ -1 +1 @@ -Subproject commit 01594bf118ae502b5a0ead040446f2be75d26223 +Subproject commit b9db8d90204c7f9a23165630fd74ad621516c0c7 From 10bba73d21a06ef5ba85bcf6039b813d424f9784 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Mon, 21 Sep 2020 15:12:22 +0200 Subject: [PATCH 46/91] MQTT: Update submodule reference: config, error handle, minor fixes Updates esp-mqtt reference to include fixes below related mainly to: * configuration update (disable keepalive, network timeout) * minor fixes (size_t for all sizes, unbalanced lock, api for outbox-size) * extended error handle to include socket's errno Closes https://github.com/espressif/esp-idf/issues/5906 Config: Added config value to disable keepalive mechanism esp-mqtt commit: https://github.com/espressif/esp-mqtt/commit/8562437c8a50754998f7b5484773851fd1c42388 Related https://github.com/espressif/esp-mqtt/issues/179 Added esp_mqtt_client_get_outbox_size API esp-mqtt commit: https://github.com/espressif/esp-mqtt/commit/0a1d9d0300335ca98dd1fed8d2ec2411145877b0 Related https://github.com/espressif/esp-mqtt/pull/178 mqtt_outbox: Removed unused retry_count field from outbox_item_t esp-mqtt commit: https://github.com/espressif/esp-mqtt/commit/673086e13a7ff141929ddc9739da3d197f8a5720 config: Fixed typo for configuring OUTBOX_EXPIRED_TIMEOUT_MS esp-mqtt commit: https://github.com/espressif/esp-mqtt/commit/259baaec9671ea3c32afa3c27a1196fce5646974 Fixed missing MQTT_API_UNLOCK in esp_mqtt_client_stop error path esp-mqtt commit: https://github.com/espressif/esp-mqtt/commit/845c2a3a1e112af5dbe5f3a9ee8f6adb92a03757 Related https://github.com/espressif/esp-mqtt/issues/173 Related https://github.com/espressif/esp-mqtt/pull/174 Extended mqtt error handle to capture transport's socket errno (IDF v4.3+) esp-mqtt commit: https://github.com/espressif/esp-mqtt/commit/23c8e1ecf5681d60032f8405990116a589290b57 Config: Added configuration value to set network timeout esp-mqtt commit: https://github.com/espressif/esp-mqtt/commit/a03228ac4639eeb324af99dcded44a1f4113d3c3 Related https://github.com/espressif/esp-mqtt/pull/166 Used size_t for all lengths to allow for other architectures esp-mqtt commit: https://github.com/espressif/esp-mqtt/commit/b9db8d90204c7f9a23165630fd74ad621516c0c7 --- components/mqtt/esp-mqtt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/mqtt/esp-mqtt b/components/mqtt/esp-mqtt index b9db8d9..da850b0 160000 --- a/components/mqtt/esp-mqtt +++ b/components/mqtt/esp-mqtt @@ -1 +1 @@ -Subproject commit b9db8d90204c7f9a23165630fd74ad621516c0c7 +Subproject commit da850b0add1e71b3659bfac5d797cc834dc3e89b From 6756e62387e732e99305578999b4705cc379e6a5 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Tue, 15 Dec 2020 19:45:34 +0100 Subject: [PATCH 47/91] MQTT: Update submodule reference to support new config modes * Queueing publish messages to outbox when the client is not connected (default=off -> messages are queued if disconnected) * Use of incremental msg-id instead of random id (default=off -> msg-id uses platform_random()) * Posting a new event-id if a queued message gets deleted from the outbox (default=off -> events are not posted) Detailed description of included `esp-mqtt` changes (da850b0add1e71b3659bfac5d797cc834dc3e89b...9ea804e0ab5368d5ab53ae2301a5fec9d1f12f1a) * mqtt: Remove unused mqtt_header_state_t - esp-mqtt commit: https://github.com/espressif/esp-mqtt/commit/b7158a4aea69e95210a6dec5d1636f52a1795dbb - esp-mqtt MR: espressif/esp-mqtt!84 - Merges https://github.com/espressif/esp-mqtt/pull/180 * Cleanup public include dirs - esp-mqtt commit: https://github.com/espressif/esp-mqtt/commit/f65d5d05db9b984065079e622e0adeac4c4c7db7 - esp-mqtt MR: espressif/esp-mqtt!85 * Config: Add a new option to use incremental message id - esp-mqtt commit: https://github.com/espressif/esp-mqtt/commit/8bb4a26f46b9bb0b00f66d61671833a184fc7afa - esp-mqtt MR: espressif/esp-mqtt!85 - Closes https://github.com/espressif/esp-mqtt/issues/176 * Publish: Add new API to enqueue qos>0 messages - esp-mqtt commit: https://github.com/espressif/esp-mqtt/commit/dc7fd5c0b1c132dc0e3c265db6bc2ac886f0f0b2 - esp-mqtt MR: espressif/esp-mqtt!85 - Closes https://github.com/espressif/esp-mqtt/issues/155 * Config: Add a new option to disable publishing when disconnected - esp-mqtt commit: https://github.com/espressif/esp-mqtt/commit/f44dcb1c26e47cc96eef379810a343246d8c265b - esp-mqtt MR: espressif/esp-mqtt!85 - Related https://github.com/espressif/esp-mqtt/issues/177 * Events: Add new event to report deleted messages from outbox - esp-mqtt commit: https://github.com/espressif/esp-mqtt/commit/2e35d4d4d53a9a4d74c475de228e8c78f69a51bd - esp-mqtt MR: espressif/esp-mqtt!85 * Publish: Allow for qos=0 messages to be stored using esp_mqtt_client_enqueue() - esp-mqtt commit: https://github.com/espressif/esp-mqtt/commit/e2de0f3e3eb4d4afd4fdb5c2860a96a2ab8b1e21 - esp-mqtt MR: espressif/esp-mqtt!85 --- components/mqtt/esp-mqtt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/mqtt/esp-mqtt b/components/mqtt/esp-mqtt index da850b0..9ea804e 160000 --- a/components/mqtt/esp-mqtt +++ b/components/mqtt/esp-mqtt @@ -1 +1 @@ -Subproject commit da850b0add1e71b3659bfac5d797cc834dc3e89b +Subproject commit 9ea804e0ab5368d5ab53ae2301a5fec9d1f12f1a From 2c3363e23a6c0deacc784bc30abad000592ba2fc Mon Sep 17 00:00:00 2001 From: David Cermak Date: Tue, 15 Dec 2020 15:48:08 +0100 Subject: [PATCH 48/91] MQTT: Add new config modes (outbox related, incremental id) --- components/mqtt/Kconfig | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/components/mqtt/Kconfig b/components/mqtt/Kconfig index 7ce41d9..3fc4ee6 100644 --- a/components/mqtt/Kconfig +++ b/components/mqtt/Kconfig @@ -26,6 +26,30 @@ menu "ESP-MQTT Configurations" help Enable MQTT transport over Websocket Secure. + config MQTT_MSG_ID_INCREMENTAL + bool "Use Incremental Message Id" + default n + help + Set this to true for the message id (2.3.1 Packet Identifier) to be generated + as an incremental number rather then a random value (used by default) + + config MQTT_SKIP_PUBLISH_IF_DISCONNECTED + bool "Skip publish if disconnected" + default n + help + Set this to true to avoid publishing (enqueueing messages) if the client is disconnected. + The MQTT client tries to publish all messages by default, even in the disconnected state + (where the qos1 and qos2 packets are stored in the internal outbox to be published later) + The MQTT_SKIP_PUBLISH_IF_DISCONNECTED option allows applications to override this behaviour + and not enqueue publish packets in the disconnected state. + + config MQTT_REPORT_DELETED_MESSAGES + bool "Report deleted messages" + default n + help + Set this to true to post events for all messages which were deleted from the outbox + before being correctly sent and confirmed. + config MQTT_USE_CUSTOM_CONFIG bool "MQTT Using custom configurations" default n From 40fdf32793da3df7140dc80f017a46682c027faf Mon Sep 17 00:00:00 2001 From: David Cermak Date: Tue, 15 Dec 2020 16:21:29 +0100 Subject: [PATCH 49/91] ci: Extend the MQTT weekend test to check mqtt-enqueue api --- .../mqtt/weekend_test/mqtt_publish_test.py | 55 ++++++++++--------- 1 file changed, 28 insertions(+), 27 deletions(-) diff --git a/components/mqtt/weekend_test/mqtt_publish_test.py b/components/mqtt/weekend_test/mqtt_publish_test.py index e5a2f76..fd6ec4f 100644 --- a/components/mqtt/weekend_test/mqtt_publish_test.py +++ b/components/mqtt/weekend_test/mqtt_publish_test.py @@ -56,7 +56,7 @@ def on_message(client, userdata, msg): message_log += "Received data:" + msg.topic + " " + payload + "\n" -def test_single_config(dut, transport, qos, repeat, published): +def test_single_config(dut, transport, qos, repeat, published, queue = 0): global expected_count global expected_data global message_log @@ -65,7 +65,7 @@ def test_single_config(dut, transport, qos, repeat, published): expected_count = 0 message_log = "" expected_data = sample_string * repeat - print("PUBLISH TEST: transport:{}, qos:{}, sequence:{}, sample msg:'{}'".format(transport, qos, published, expected_data)) + print("PUBLISH TEST: transport:{}, qos:{}, sequence:{}, enqueue:{}, sample msg:'{}'".format(transport, qos, published, queue, expected_data)) client = None try: if transport in ["ws", "wss"]: @@ -89,7 +89,7 @@ def test_single_config(dut, transport, qos, repeat, published): if not event_client_connected.wait(timeout=30): raise ValueError("ENV_TEST_FAILURE: Test script cannot connect to broker: {}".format(broker_host[transport])) client.subscribe(subscribe_topic, qos) - dut.write("{} {} {} {} {}".format(transport, sample_string, repeat, published, qos), eol="\n") + dut.write("{} {} {} {} {} {}".format(transport, sample_string, repeat, published, qos, queue), eol="\n") try: # waiting till subscribed to defined topic dut.expect(re.compile(r"MQTT_EVENT_SUBSCRIBED"), timeout=30) @@ -105,7 +105,7 @@ def test_single_config(dut, transport, qos, repeat, published): if expected_count == published or (expected_count > published and qos == 1): print("All data received from ESP32...") else: - raise ValueError("Not all data received from ESP32: Expected:{}x{}, Received:{}x{}".format(expected_data, published, message_log, expected_count)) + raise ValueError("Not all data received from ESP32: Expected:{}x{}, Received:{}x{}".format(expected_count, published, expected_data, message_log)) finally: event_stop_client.set() thread1.join() @@ -149,29 +149,30 @@ def test_weekend_mqtt_publish(env, extra_data): raise for qos in [0, 1, 2]: for transport in ["tcp", "ssl", "ws", "wss"]: - if broker_host[transport] is None: - print('Skipping transport: {}...'.format(transport)) - continue - # simple test with empty message - test_single_config(dut1, transport, qos, 0, 5) - # decide on broker what level of test will pass (local broker works the best) - if broker_host[transport].startswith("192.168") and qos < 1: - # medium size, medium repeated - test_single_config(dut1, transport, qos, 5, 50) - # long data - test_single_config(dut1, transport, qos, 1000, 10) - # short data, many repeats - test_single_config(dut1, transport, qos, 2, 200) - elif transport in ["ws", "wss"]: - # more relaxed criteria for websockets! - test_single_config(dut1, transport, qos, 2, 5) - test_single_config(dut1, transport, qos, 50, 1) - test_single_config(dut1, transport, qos, 10, 20) - else: - # common configuration should be good for most public mosquittos - test_single_config(dut1, transport, qos, 5, 10) - test_single_config(dut1, transport, qos, 500, 3) - test_single_config(dut1, transport, qos, 1, 50) + for q in [0, 1]: + if broker_host[transport] is None: + print('Skipping transport: {}...'.format(transport)) + continue + # simple test with empty message + test_single_config(dut1, transport, qos, 0, 5, q) + # decide on broker what level of test will pass (local broker works the best) + if broker_host[transport].startswith("192.168") and qos > 0 and q == 0: + # medium size, medium repeated + test_single_config(dut1, transport, qos, 5, 50, q) + # long data + test_single_config(dut1, transport, qos, 1000, 10, q) + # short data, many repeats + test_single_config(dut1, transport, qos, 2, 200, q) + elif transport in ["ws", "wss"]: + # more relaxed criteria for websockets! + test_single_config(dut1, transport, qos, 2, 5, q) + test_single_config(dut1, transport, qos, 50, 1, q) + test_single_config(dut1, transport, qos, 10, 20, q) + else: + # common configuration should be good for most public mosquittos + test_single_config(dut1, transport, qos, 5, 10, q) + test_single_config(dut1, transport, qos, 500, 3, q) + test_single_config(dut1, transport, qos, 1, 50, q) if __name__ == '__main__': From 3883bde0b012d267aacd65d32da1f3a6b8862024 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Tue, 15 Dec 2020 19:33:25 +0100 Subject: [PATCH 50/91] ci: Add MQTT publish test to standard test apps --- components/mqtt/weekend_test/mqtt_publish_test.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/components/mqtt/weekend_test/mqtt_publish_test.py b/components/mqtt/weekend_test/mqtt_publish_test.py index fd6ec4f..bdc2e63 100644 --- a/components/mqtt/weekend_test/mqtt_publish_test.py +++ b/components/mqtt/weekend_test/mqtt_publish_test.py @@ -56,7 +56,7 @@ def on_message(client, userdata, msg): message_log += "Received data:" + msg.topic + " " + payload + "\n" -def test_single_config(dut, transport, qos, repeat, published, queue = 0): +def test_single_config(dut, transport, qos, repeat, published, queue=0): global expected_count global expected_data global message_log @@ -89,7 +89,7 @@ def test_single_config(dut, transport, qos, repeat, published, queue = 0): if not event_client_connected.wait(timeout=30): raise ValueError("ENV_TEST_FAILURE: Test script cannot connect to broker: {}".format(broker_host[transport])) client.subscribe(subscribe_topic, qos) - dut.write("{} {} {} {} {} {}".format(transport, sample_string, repeat, published, qos, queue), eol="\n") + dut.write(' '.join(str(x) for x in (transport, sample_string, repeat, published, qos, queue)), eol="\n") try: # waiting till subscribed to defined topic dut.expect(re.compile(r"MQTT_EVENT_SUBSCRIBED"), timeout=30) From f072c9f75311e0f25ed5c6eb6668a240fbfc76c1 Mon Sep 17 00:00:00 2001 From: Fu Hanxi Date: Tue, 26 Jan 2021 10:49:01 +0800 Subject: [PATCH 51/91] style: format python files with isort and double-quote-string-fixer --- .../mqtt/weekend_test/mqtt_publish_test.py | 95 +++++++++---------- 1 file changed, 47 insertions(+), 48 deletions(-) diff --git a/components/mqtt/weekend_test/mqtt_publish_test.py b/components/mqtt/weekend_test/mqtt_publish_test.py index bdc2e63..c9ea51c 100644 --- a/components/mqtt/weekend_test/mqtt_publish_test.py +++ b/components/mqtt/weekend_test/mqtt_publish_test.py @@ -1,36 +1,35 @@ -from __future__ import print_function -from __future__ import unicode_literals -from builtins import str -import re -import sys -import ssl -import paho.mqtt.client as mqtt -from threading import Thread, Event -import time -import string +from __future__ import print_function, unicode_literals + import random +import re +import ssl +import string +import sys +import time +from builtins import str +from threading import Event, Thread -from tiny_test_fw import DUT +import paho.mqtt.client as mqtt import ttfw_idf - +from tiny_test_fw import DUT event_client_connected = Event() event_stop_client = Event() event_client_received_correct = Event() -message_log = "" +message_log = '' broker_host = {} broker_port = {} -expected_data = "" -subscribe_topic = "" -publish_topic = "" +expected_data = '' +subscribe_topic = '' +publish_topic = '' expected_count = 0 # The callback for when the client receives a CONNACK response from the server. def on_connect(client, userdata, flags, rc): - print("Connected with result code " + str(rc)) + print('Connected with result code ' + str(rc)) event_client_connected.set() - client.subscribe("/topic/qos0") + client.subscribe('/topic/qos0') def mqtt_client_task(client): @@ -52,8 +51,8 @@ def on_message(client, userdata, msg): payload = msg.payload.decode() if payload == expected_data: expected_count += 1 - print("[{}] Received...".format(msg.mid)) - message_log += "Received data:" + msg.topic + " " + payload + "\n" + print('[{}] Received...'.format(msg.mid)) + message_log += 'Received data:' + msg.topic + ' ' + payload + '\n' def test_single_config(dut, transport, qos, repeat, published, queue=0): @@ -63,49 +62,49 @@ def test_single_config(dut, transport, qos, repeat, published, queue=0): sample_string = ''.join(random.choice(string.ascii_uppercase + string.ascii_lowercase + string.digits) for _ in range(16)) event_client_connected.clear() expected_count = 0 - message_log = "" + message_log = '' expected_data = sample_string * repeat print("PUBLISH TEST: transport:{}, qos:{}, sequence:{}, enqueue:{}, sample msg:'{}'".format(transport, qos, published, queue, expected_data)) client = None try: - if transport in ["ws", "wss"]: - client = mqtt.Client(transport="websockets") + if transport in ['ws', 'wss']: + client = mqtt.Client(transport='websockets') else: client = mqtt.Client() client.on_connect = on_connect client.on_message = on_message - if transport in ["ssl", "wss"]: + if transport in ['ssl', 'wss']: client.tls_set(None, None, None, cert_reqs=ssl.CERT_NONE, tls_version=ssl.PROTOCOL_TLSv1_2, ciphers=None) client.tls_insecure_set(True) - print("Connecting...") + print('Connecting...') client.connect(broker_host[transport], broker_port[transport], 60) except Exception: - print("ENV_TEST_FAILURE: Unexpected error while connecting to broker {}: {}:".format(broker_host[transport], sys.exc_info()[0])) + print('ENV_TEST_FAILURE: Unexpected error while connecting to broker {}: {}:'.format(broker_host[transport], sys.exc_info()[0])) raise # Starting a py-client in a separate thread thread1 = Thread(target=mqtt_client_task, args=(client,)) thread1.start() - print("Connecting py-client to broker {}:{}...".format(broker_host[transport], broker_port[transport])) + print('Connecting py-client to broker {}:{}...'.format(broker_host[transport], broker_port[transport])) if not event_client_connected.wait(timeout=30): - raise ValueError("ENV_TEST_FAILURE: Test script cannot connect to broker: {}".format(broker_host[transport])) + raise ValueError('ENV_TEST_FAILURE: Test script cannot connect to broker: {}'.format(broker_host[transport])) client.subscribe(subscribe_topic, qos) - dut.write(' '.join(str(x) for x in (transport, sample_string, repeat, published, qos, queue)), eol="\n") + dut.write(' '.join(str(x) for x in (transport, sample_string, repeat, published, qos, queue)), eol='\n') try: # waiting till subscribed to defined topic - dut.expect(re.compile(r"MQTT_EVENT_SUBSCRIBED"), timeout=30) + dut.expect(re.compile(r'MQTT_EVENT_SUBSCRIBED'), timeout=30) for i in range(published): client.publish(publish_topic, sample_string * repeat, qos) - print("Publishing...") - print("Checking esp-client received msg published from py-client...") - dut.expect(re.compile(r"Correct pattern received exactly x times"), timeout=60) + print('Publishing...') + print('Checking esp-client received msg published from py-client...') + dut.expect(re.compile(r'Correct pattern received exactly x times'), timeout=60) start = time.time() while expected_count < published and time.time() - start <= 60: time.sleep(1) # Note: tolerate that messages qos=1 to be received more than once if expected_count == published or (expected_count > published and qos == 1): - print("All data received from ESP32...") + print('All data received from ESP32...') else: - raise ValueError("Not all data received from ESP32: Expected:{}x{}, Received:{}x{}".format(expected_count, published, expected_data, message_log)) + raise ValueError('Not all data received from ESP32: Expected:{}x{}, Received:{}x{}'.format(expected_count, published, expected_data, message_log)) finally: event_stop_client.set() thread1.join() @@ -113,7 +112,7 @@ def test_single_config(dut, transport, qos, repeat, published, queue=0): event_stop_client.clear() -@ttfw_idf.idf_custom_test(env_tag="Example_WIFI") +@ttfw_idf.idf_custom_test(env_tag='Example_WIFI') def test_weekend_mqtt_publish(env, extra_data): # Using broker url dictionary for different transport global broker_host @@ -127,28 +126,28 @@ def test_weekend_mqtt_publish(env, extra_data): 3. Test evaluates python client received correct qos0 message 4. Test ESP32 client received correct qos0 message """ - dut1 = env.get_dut("mqtt_publish_connect_test", "tools/test_apps/protocols/mqtt/publish_connect_test") + dut1 = env.get_dut('mqtt_publish_connect_test', 'tools/test_apps/protocols/mqtt/publish_connect_test') # Look for host:port in sdkconfig try: # python client subscribes to the topic to which esp client publishes and vice versa - publish_topic = dut1.app.get_sdkconfig()["CONFIG_EXAMPLE_SUBSCIBE_TOPIC"].replace('"','') - subscribe_topic = dut1.app.get_sdkconfig()["CONFIG_EXAMPLE_PUBLISH_TOPIC"].replace('"','') - broker_host["ssl"], broker_port["ssl"] = get_host_port_from_dut(dut1, "CONFIG_EXAMPLE_BROKER_SSL_URI") - broker_host["tcp"], broker_port["tcp"] = get_host_port_from_dut(dut1, "CONFIG_EXAMPLE_BROKER_TCP_URI") - broker_host["ws"], broker_port["ws"] = get_host_port_from_dut(dut1, "CONFIG_EXAMPLE_BROKER_WS_URI") - broker_host["wss"], broker_port["wss"] = get_host_port_from_dut(dut1, "CONFIG_EXAMPLE_BROKER_WSS_URI") + publish_topic = dut1.app.get_sdkconfig()['CONFIG_EXAMPLE_SUBSCIBE_TOPIC'].replace('"','') + subscribe_topic = dut1.app.get_sdkconfig()['CONFIG_EXAMPLE_PUBLISH_TOPIC'].replace('"','') + broker_host['ssl'], broker_port['ssl'] = get_host_port_from_dut(dut1, 'CONFIG_EXAMPLE_BROKER_SSL_URI') + broker_host['tcp'], broker_port['tcp'] = get_host_port_from_dut(dut1, 'CONFIG_EXAMPLE_BROKER_TCP_URI') + broker_host['ws'], broker_port['ws'] = get_host_port_from_dut(dut1, 'CONFIG_EXAMPLE_BROKER_WS_URI') + broker_host['wss'], broker_port['wss'] = get_host_port_from_dut(dut1, 'CONFIG_EXAMPLE_BROKER_WSS_URI') except Exception: print('ENV_TEST_FAILURE: Cannot find broker url in sdkconfig') raise dut1.start_app() try: - ip_address = dut1.expect(re.compile(r" IPv4 address: ([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)"), timeout=30) - print("Connected to AP with IP: {}".format(ip_address)) + ip_address = dut1.expect(re.compile(r' IPv4 address: ([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)'), timeout=30) + print('Connected to AP with IP: {}'.format(ip_address)) except DUT.ExpectTimeout: print('ENV_TEST_FAILURE: Cannot connect to AP') raise for qos in [0, 1, 2]: - for transport in ["tcp", "ssl", "ws", "wss"]: + for transport in ['tcp', 'ssl', 'ws', 'wss']: for q in [0, 1]: if broker_host[transport] is None: print('Skipping transport: {}...'.format(transport)) @@ -156,14 +155,14 @@ def test_weekend_mqtt_publish(env, extra_data): # simple test with empty message test_single_config(dut1, transport, qos, 0, 5, q) # decide on broker what level of test will pass (local broker works the best) - if broker_host[transport].startswith("192.168") and qos > 0 and q == 0: + if broker_host[transport].startswith('192.168') and qos > 0 and q == 0: # medium size, medium repeated test_single_config(dut1, transport, qos, 5, 50, q) # long data test_single_config(dut1, transport, qos, 1000, 10, q) # short data, many repeats test_single_config(dut1, transport, qos, 2, 200, q) - elif transport in ["ws", "wss"]: + elif transport in ['ws', 'wss']: # more relaxed criteria for websockets! test_single_config(dut1, transport, qos, 2, 5, q) test_single_config(dut1, transport, qos, 50, 1, q) From f2f30baec0eb944873e2f32f7f883778bb202311 Mon Sep 17 00:00:00 2001 From: Angus Gratton Date: Thu, 18 Feb 2021 15:36:16 +1100 Subject: [PATCH 52/91] esp-mqtt: Remove __FILE__ macro from error logs --- components/mqtt/esp-mqtt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/mqtt/esp-mqtt b/components/mqtt/esp-mqtt index 9ea804e..9fdf7b6 160000 --- a/components/mqtt/esp-mqtt +++ b/components/mqtt/esp-mqtt @@ -1 +1 @@ -Subproject commit 9ea804e0ab5368d5ab53ae2301a5fec9d1f12f1a +Subproject commit 9fdf7b61385633075d5c3b84803f2dd0578d7869 From 128be3f402f5b427aa96ef3d65f40cd13661c4df Mon Sep 17 00:00:00 2001 From: David Cermak Date: Thu, 10 Jun 2021 09:09:14 +0200 Subject: [PATCH 53/91] mqtt: Moved weekend tests to test apps --- components/mqtt/weekend_test/env.yml | 0 .../mqtt/weekend_test/mqtt_publish_test.py | 178 ------------------ .../mqtt/weekend_test/test_weekend_mqtt_.yml | 2 - .../weekend_test/test_weekend_mqtt_qemu.yml | 6 - 4 files changed, 186 deletions(-) delete mode 100644 components/mqtt/weekend_test/env.yml delete mode 100644 components/mqtt/weekend_test/mqtt_publish_test.py delete mode 100644 components/mqtt/weekend_test/test_weekend_mqtt_.yml delete mode 100644 components/mqtt/weekend_test/test_weekend_mqtt_qemu.yml diff --git a/components/mqtt/weekend_test/env.yml b/components/mqtt/weekend_test/env.yml deleted file mode 100644 index e69de29..0000000 diff --git a/components/mqtt/weekend_test/mqtt_publish_test.py b/components/mqtt/weekend_test/mqtt_publish_test.py deleted file mode 100644 index c9ea51c..0000000 --- a/components/mqtt/weekend_test/mqtt_publish_test.py +++ /dev/null @@ -1,178 +0,0 @@ -from __future__ import print_function, unicode_literals - -import random -import re -import ssl -import string -import sys -import time -from builtins import str -from threading import Event, Thread - -import paho.mqtt.client as mqtt -import ttfw_idf -from tiny_test_fw import DUT - -event_client_connected = Event() -event_stop_client = Event() -event_client_received_correct = Event() -message_log = '' -broker_host = {} -broker_port = {} -expected_data = '' -subscribe_topic = '' -publish_topic = '' -expected_count = 0 - - -# The callback for when the client receives a CONNACK response from the server. -def on_connect(client, userdata, flags, rc): - print('Connected with result code ' + str(rc)) - event_client_connected.set() - client.subscribe('/topic/qos0') - - -def mqtt_client_task(client): - while not event_stop_client.is_set(): - client.loop() - - -def get_host_port_from_dut(dut1, config_option): - value = re.search(r'\:\/\/([^:]+)\:([0-9]+)', dut1.app.get_sdkconfig()[config_option]) - if value is None: - return None, None - return value.group(1), int(value.group(2)) - - -# The callback for when a PUBLISH message is received from the server. -def on_message(client, userdata, msg): - global message_log - global expected_count - payload = msg.payload.decode() - if payload == expected_data: - expected_count += 1 - print('[{}] Received...'.format(msg.mid)) - message_log += 'Received data:' + msg.topic + ' ' + payload + '\n' - - -def test_single_config(dut, transport, qos, repeat, published, queue=0): - global expected_count - global expected_data - global message_log - sample_string = ''.join(random.choice(string.ascii_uppercase + string.ascii_lowercase + string.digits) for _ in range(16)) - event_client_connected.clear() - expected_count = 0 - message_log = '' - expected_data = sample_string * repeat - print("PUBLISH TEST: transport:{}, qos:{}, sequence:{}, enqueue:{}, sample msg:'{}'".format(transport, qos, published, queue, expected_data)) - client = None - try: - if transport in ['ws', 'wss']: - client = mqtt.Client(transport='websockets') - else: - client = mqtt.Client() - client.on_connect = on_connect - client.on_message = on_message - if transport in ['ssl', 'wss']: - client.tls_set(None, None, None, cert_reqs=ssl.CERT_NONE, tls_version=ssl.PROTOCOL_TLSv1_2, ciphers=None) - client.tls_insecure_set(True) - print('Connecting...') - client.connect(broker_host[transport], broker_port[transport], 60) - except Exception: - print('ENV_TEST_FAILURE: Unexpected error while connecting to broker {}: {}:'.format(broker_host[transport], sys.exc_info()[0])) - raise - # Starting a py-client in a separate thread - thread1 = Thread(target=mqtt_client_task, args=(client,)) - thread1.start() - print('Connecting py-client to broker {}:{}...'.format(broker_host[transport], broker_port[transport])) - if not event_client_connected.wait(timeout=30): - raise ValueError('ENV_TEST_FAILURE: Test script cannot connect to broker: {}'.format(broker_host[transport])) - client.subscribe(subscribe_topic, qos) - dut.write(' '.join(str(x) for x in (transport, sample_string, repeat, published, qos, queue)), eol='\n') - try: - # waiting till subscribed to defined topic - dut.expect(re.compile(r'MQTT_EVENT_SUBSCRIBED'), timeout=30) - for i in range(published): - client.publish(publish_topic, sample_string * repeat, qos) - print('Publishing...') - print('Checking esp-client received msg published from py-client...') - dut.expect(re.compile(r'Correct pattern received exactly x times'), timeout=60) - start = time.time() - while expected_count < published and time.time() - start <= 60: - time.sleep(1) - # Note: tolerate that messages qos=1 to be received more than once - if expected_count == published or (expected_count > published and qos == 1): - print('All data received from ESP32...') - else: - raise ValueError('Not all data received from ESP32: Expected:{}x{}, Received:{}x{}'.format(expected_count, published, expected_data, message_log)) - finally: - event_stop_client.set() - thread1.join() - client.disconnect() - event_stop_client.clear() - - -@ttfw_idf.idf_custom_test(env_tag='Example_WIFI') -def test_weekend_mqtt_publish(env, extra_data): - # Using broker url dictionary for different transport - global broker_host - global broker_port - global publish_topic - global subscribe_topic - """ - steps: | - 1. join AP and connects to ssl broker - 2. Test connects a client to the same broker - 3. Test evaluates python client received correct qos0 message - 4. Test ESP32 client received correct qos0 message - """ - dut1 = env.get_dut('mqtt_publish_connect_test', 'tools/test_apps/protocols/mqtt/publish_connect_test') - # Look for host:port in sdkconfig - try: - # python client subscribes to the topic to which esp client publishes and vice versa - publish_topic = dut1.app.get_sdkconfig()['CONFIG_EXAMPLE_SUBSCIBE_TOPIC'].replace('"','') - subscribe_topic = dut1.app.get_sdkconfig()['CONFIG_EXAMPLE_PUBLISH_TOPIC'].replace('"','') - broker_host['ssl'], broker_port['ssl'] = get_host_port_from_dut(dut1, 'CONFIG_EXAMPLE_BROKER_SSL_URI') - broker_host['tcp'], broker_port['tcp'] = get_host_port_from_dut(dut1, 'CONFIG_EXAMPLE_BROKER_TCP_URI') - broker_host['ws'], broker_port['ws'] = get_host_port_from_dut(dut1, 'CONFIG_EXAMPLE_BROKER_WS_URI') - broker_host['wss'], broker_port['wss'] = get_host_port_from_dut(dut1, 'CONFIG_EXAMPLE_BROKER_WSS_URI') - except Exception: - print('ENV_TEST_FAILURE: Cannot find broker url in sdkconfig') - raise - dut1.start_app() - try: - ip_address = dut1.expect(re.compile(r' IPv4 address: ([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)'), timeout=30) - print('Connected to AP with IP: {}'.format(ip_address)) - except DUT.ExpectTimeout: - print('ENV_TEST_FAILURE: Cannot connect to AP') - raise - for qos in [0, 1, 2]: - for transport in ['tcp', 'ssl', 'ws', 'wss']: - for q in [0, 1]: - if broker_host[transport] is None: - print('Skipping transport: {}...'.format(transport)) - continue - # simple test with empty message - test_single_config(dut1, transport, qos, 0, 5, q) - # decide on broker what level of test will pass (local broker works the best) - if broker_host[transport].startswith('192.168') and qos > 0 and q == 0: - # medium size, medium repeated - test_single_config(dut1, transport, qos, 5, 50, q) - # long data - test_single_config(dut1, transport, qos, 1000, 10, q) - # short data, many repeats - test_single_config(dut1, transport, qos, 2, 200, q) - elif transport in ['ws', 'wss']: - # more relaxed criteria for websockets! - test_single_config(dut1, transport, qos, 2, 5, q) - test_single_config(dut1, transport, qos, 50, 1, q) - test_single_config(dut1, transport, qos, 10, 20, q) - else: - # common configuration should be good for most public mosquittos - test_single_config(dut1, transport, qos, 5, 10, q) - test_single_config(dut1, transport, qos, 500, 3, q) - test_single_config(dut1, transport, qos, 1, 50, q) - - -if __name__ == '__main__': - test_weekend_mqtt_publish(dut=ttfw_idf.ESP32QEMUDUT if sys.argv[1:] == ['qemu'] else ttfw_idf.ESP32DUT) diff --git a/components/mqtt/weekend_test/test_weekend_mqtt_.yml b/components/mqtt/weekend_test/test_weekend_mqtt_.yml deleted file mode 100644 index ae00e35..0000000 --- a/components/mqtt/weekend_test/test_weekend_mqtt_.yml +++ /dev/null @@ -1,2 +0,0 @@ -CaseConfig: -- name: test_weekend_mqtt_publish diff --git a/components/mqtt/weekend_test/test_weekend_mqtt_qemu.yml b/components/mqtt/weekend_test/test_weekend_mqtt_qemu.yml deleted file mode 100644 index bd6f0d6..0000000 --- a/components/mqtt/weekend_test/test_weekend_mqtt_qemu.yml +++ /dev/null @@ -1,6 +0,0 @@ -CaseConfig: -- name: test_weekend_mqtt_publish - overwrite: - dut: - class: ESP32QEMUDUT - package: ttfw_idf From 0fda7d7062e6264f5a635c3fc236d02ff6356bb4 Mon Sep 17 00:00:00 2001 From: liuhan Date: Fri, 16 Apr 2021 17:23:18 +0800 Subject: [PATCH 54/91] transport: Add CONFI_WS_TRANSPORT for optimize the code size --- components/mqtt/Kconfig | 1 + 1 file changed, 1 insertion(+) diff --git a/components/mqtt/Kconfig b/components/mqtt/Kconfig index 3fc4ee6..9a06e43 100644 --- a/components/mqtt/Kconfig +++ b/components/mqtt/Kconfig @@ -15,6 +15,7 @@ menu "ESP-MQTT Configurations" config MQTT_TRANSPORT_WEBSOCKET bool "Enable MQTT over Websocket" default y + depends on WS_TRANSPORT help Enable MQTT transport over Websocket. From a8b0f474ec3fb387b74dfaf4736816c6d58fc438 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Wed, 23 Jun 2021 14:30:24 +0200 Subject: [PATCH 55/91] MQTT: Support for certificate bundle; Client clean-up * Closes https://github.com/espressif/esp-idf/issues/7040 * Merges https://github.com/espressif/esp-idf/pull/7041 * Update submodule: git log --oneline 9fdf7b61385633075d5c3b84803f2dd0578d7869..f10321a53b53a146ee299cfecc320b89c0cf6611 Detailed description of the changes: * Remove unnecessary parentheses - esp-mqtt commit: https://github.com/espressif/esp-mqtt/commit/db1353390489a17f57b4cbd7a9014cd960c75877 - esp-mqtt MR: espressif/esp-mqtt!101 * outbox: Cleanup all items when connection closes - esp-mqtt commit: https://github.com/espressif/esp-mqtt/commit/1a94efe8b9e1b16c53eaa5c95e73e1c1337abbf5 - esp-mqtt MR: espressif/esp-mqtt!104 * Outbox: Removes unnecessary calls to outbox_set_pending - esp-mqtt commit: https://github.com/espressif/esp-mqtt/commit/36a39904044765c7ec6803d9d9c3aa2d777d96fa - esp-mqtt MR: espressif/esp-mqtt!105 * MQTT: Makes abort connection function void. - esp-mqtt commit: https://github.com/espressif/esp-mqtt/commit/67553aba45062c973aa7f2a9de6b05c0a852b840 - esp-mqtt MR: espressif/esp-mqtt!106 * Client: Removes unused defines - esp-mqtt commit: https://github.com/espressif/esp-mqtt/commit/eec6f0e17de36ad15cba365b6cc5d57fa24af17f - esp-mqtt MR: espressif/esp-mqtt!100 - Closes https://github.com/espressif/esp-mqtt/issues/194 * Config: Added support for certificate bundle - esp-mqtt commit: https://github.com/espressif/esp-mqtt/commit/06157be118e162e66d15bfc98b144fb5160a272b - esp-mqtt MR: espressif/esp-mqtt!98 - Closes https://github.com/espressif/esp-mqtt/issues/190 * Config: Adds missing field at config struct (path field) - esp-mqtt commit: https://github.com/espressif/esp-mqtt/commit/5b27d1896ed355571fbc9145617e05354d634726 - esp-mqtt MR: espressif/esp-mqtt!96 * Client: Add support for partial transport writes - esp-mqtt commit: https://github.com/espressif/esp-mqtt/commit/d8c9c7a9e75d82eaa766c27454e3a2282eb001cf - esp-mqtt MR: espressif/esp-mqtt!99 - Partially addresses https://github.com/espressif/esp-idf/issues/6940 * Client: Add support for Retain flag in messages posted by events - esp-mqtt commit: https://github.com/espressif/esp-mqtt/commit/a00a3134c67657949ab7ab5317133a49a855fe30 - esp-mqtt MR: espressif/esp-mqtt!99 - Closes https://github.com/espressif/esp-mqtt/issues/193 * esp-mqtt: Added nullchecks for public APIs - esp-mqtt commit: https://github.com/espressif/esp-mqtt/commit/2f57985c0b7337af19989baca37b89ad0d07ff7a - esp-mqtt MR: espressif/esp-mqtt!94 - Closes https://github.com/espressif/esp-mqtt/issues/185 * esp-mqtt: Reduce the includes used in all files - esp-mqtt commit: https://github.com/espressif/esp-mqtt/commit/87fcce72c951b3f413aaddbd13606e379f0c744a - esp-mqtt MR: espressif/esp-mqtt!93 * mqtt_outbox: Use STAILQ_FOREACH for outbox_delete_single_expired - esp-mqtt commit: https://github.com/espressif/esp-mqtt/commit/ff8e64839af0fa464932ce1383723e4a20d3488f - esp-mqtt MR: espressif/esp-mqtt!97 - Merges https://github.com/espressif/esp-mqtt/pull/187 * Client: Add optimize for depend on ssl - esp-mqtt commit: https://github.com/espressif/esp-mqtt/commit/8f3cac8c368d3c6518a8f68c9a659811ae0582f6 - esp-mqtt MR: espressif/esp-mqtt!95 --- components/mqtt/esp-mqtt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/mqtt/esp-mqtt b/components/mqtt/esp-mqtt index 9fdf7b6..f10321a 160000 --- a/components/mqtt/esp-mqtt +++ b/components/mqtt/esp-mqtt @@ -1 +1 @@ -Subproject commit 9fdf7b61385633075d5c3b84803f2dd0578d7869 +Subproject commit f10321a53b53a146ee299cfecc320b89c0cf6611 From 93158d5d1819da943f296daeec0f2f73a71f313a Mon Sep 17 00:00:00 2001 From: Euripedes Rocha Filho Date: Tue, 16 Feb 2021 15:47:10 +0000 Subject: [PATCH 56/91] mqtt: Adds host tests for mqtt client --- components/mqtt/CMakeLists.txt | 132 +++++++++++- components/mqtt/host_test/CMakeLists.txt | 6 + components/mqtt/host_test/README.md | 27 +++ components/mqtt/host_test/main/CMakeLists.txt | 3 + .../mqtt/host_test/main/test_mqtt_client.cpp | 109 ++++++++++ components/mqtt/host_test/mocks/config.yaml | 23 ++ .../mocks/include/freertos/FreeRTOSConfig.h | 133 ++++++++++++ .../mocks/include/freertos/portmacro.h | 199 ++++++++++++++++++ .../host_test/mocks/include/machine/endian.h | 2 + .../mqtt/host_test/mocks/include/sys/queue.h | 66 ++++++ components/mqtt/host_test/sdkconfig.defaults | 6 + 11 files changed, 705 insertions(+), 1 deletion(-) create mode 100644 components/mqtt/host_test/CMakeLists.txt create mode 100644 components/mqtt/host_test/README.md create mode 100644 components/mqtt/host_test/main/CMakeLists.txt create mode 100644 components/mqtt/host_test/main/test_mqtt_client.cpp create mode 100644 components/mqtt/host_test/mocks/config.yaml create mode 100644 components/mqtt/host_test/mocks/include/freertos/FreeRTOSConfig.h create mode 100644 components/mqtt/host_test/mocks/include/freertos/portmacro.h create mode 100644 components/mqtt/host_test/mocks/include/machine/endian.h create mode 100644 components/mqtt/host_test/mocks/include/sys/queue.h create mode 100644 components/mqtt/host_test/sdkconfig.defaults diff --git a/components/mqtt/CMakeLists.txt b/components/mqtt/CMakeLists.txt index 1c30e3a..e88c582 100644 --- a/components/mqtt/CMakeLists.txt +++ b/components/mqtt/CMakeLists.txt @@ -4,4 +4,134 @@ idf_component_register(SRCS "esp-mqtt/mqtt_client.c" "esp-mqtt/lib/platform_esp32_idf.c" INCLUDE_DIRS esp-mqtt/include PRIV_INCLUDE_DIRS "esp-mqtt/lib/include" - REQUIRES lwip nghttp mbedtls tcp_transport) + ) + +if(TEST_BUILD) +message(STATUS "building MOCKS") +idf_component_get_property(tcp_transport_dir tcp_transport COMPONENT_DIR) +idf_component_get_property(esp_hw_support_dir esp_hw_support COMPONENT_DIR) +idf_component_get_property(esp_event_dir esp_event COMPONENT_DIR) +idf_component_get_property(log_dir log COMPONENT_DIR) +idf_component_get_property(freertos_dir freertos COMPONENT_DIR) +idf_component_get_property(nghttp_dir nghttp COMPONENT_DIR) +idf_component_get_property(esp_wifi_dir esp_wifi COMPONENT_DIR) +idf_component_get_property(esp_hw_support_dir esp_hw_support COMPONENT_DIR) +idf_component_get_property(esp_tls_dir esp-tls COMPONENT_DIR) +idf_component_get_property(esp_netif_dir esp_netif COMPONENT_DIR) +idf_component_get_property(esp_common_dir esp_common COMPONENT_DIR) +idf_component_get_property(esp_rom_dir esp_rom COMPONENT_DIR) +idf_component_get_property(esp_system_dir esp_system COMPONENT_DIR) +idf_component_get_property(mbedtls_dir mbedtls COMPONENT_DIR) + + idf_component_get_property(cmock_lib cmock COMPONENT_LIB) + set(IDF_PATH $ENV{IDF_PATH}) + set(CMOCK_DIR "${IDF_PATH}/components/cmock/CMock") + set(MOCK_GEN_DIR "${CMAKE_CURRENT_BINARY_DIR}/mocks") + set(ENV{UNITY_DIR} "$ENV{IDF_PATH}/components/cmock/CMock") + file(MAKE_DIRECTORY ${MOCK_GEN_DIR}) + + set(MOCK_OUTPUT + "${MOCK_GEN_DIR}/Mockesp_transport.c" "${MOCK_GEN_DIR}/Mockesp_transport.h" + "${MOCK_GEN_DIR}/Mockesp_transport_ssl.c" "${MOCK_GEN_DIR}/Mockesp_transport_ssl.h" + "${MOCK_GEN_DIR}/Mockesp_transport_ws.c" "${MOCK_GEN_DIR}/Mockesp_transport_ws.h" + "${MOCK_GEN_DIR}/Mockesp_transport_tcp.c" "${MOCK_GEN_DIR}/Mockesp_transport_tcp.h" + "${MOCK_GEN_DIR}/Mockesp_event.c" "${MOCK_GEN_DIR}/Mockesp_event.h" + "${MOCK_GEN_DIR}/Mockesp_mac.c" "${MOCK_GEN_DIR}/Mockesp_mac.h" + "${MOCK_GEN_DIR}/Mockesp_random.c" "${MOCK_GEN_DIR}/Mockesp_random.h" + "${MOCK_GEN_DIR}/Mockesp_system.c" "${MOCK_GEN_DIR}/Mockesp_system.h" + "${MOCK_GEN_DIR}/Mockesp_tls.c" "${MOCK_GEN_DIR}/Mockesp_tls.h" + "${MOCK_GEN_DIR}/Mockevent_groups.c" "${MOCK_GEN_DIR}/Mockevent_groups.h" + "${MOCK_GEN_DIR}/Mockqueue.c" "${MOCK_GEN_DIR}/Mockqueue.h" + "${MOCK_GEN_DIR}/Mocktask.c" "${MOCK_GEN_DIR}/Mocktask.h" + "${MOCK_GEN_DIR}/Mockesp_log.c" "${MOCK_GEN_DIR}/Mockesp_log.h" + "${MOCK_GEN_DIR}/Mockhttp_parser.c" "${MOCK_GEN_DIR}/Mockhttp_parser.h" + ) + + set(HEADERS_TO_MOCK + ${tcp_transport_dir}/include/esp_transport_tcp.h + ${tcp_transport_dir}/include/esp_transport_ws.h + ${tcp_transport_dir}/include/esp_transport_ssl.h + ${tcp_transport_dir}/include/esp_transport.h + ${esp_event_dir}/include/esp_event.h + ${esp_hw_support_dir}/include/esp_mac.h + ${esp_hw_support_dir}/include/esp_random.h + ${esp_system_dir}/include/esp_system.h + ${esp_tls_dir}/esp_tls.h + ${freertos_dir}/include/freertos/queue.h + ${freertos_dir}/include/freertos/task.h + ${freertos_dir}/include/freertos/event_groups.h + ${log_dir}/include/esp_log.h + ${nghttp_dir}/port/include/http_parser.h + ) + + set(srcs + ${MOCK_GEN_DIR}/Mockesp_transport.c + ${MOCK_GEN_DIR}/Mockesp_transport_ws.c + ${MOCK_GEN_DIR}/Mockesp_transport_ssl.c + ${MOCK_GEN_DIR}/Mockesp_transport_tcp.c + ${MOCK_GEN_DIR}/Mockesp_transport_tcp.c + ${MOCK_GEN_DIR}/Mockesp_event.c + ${MOCK_GEN_DIR}/Mockesp_mac.c + ${MOCK_GEN_DIR}/Mockesp_random.c + ${MOCK_GEN_DIR}/Mockesp_system.c + ${MOCK_GEN_DIR}/Mockesp_tls.c + ${MOCK_GEN_DIR}/Mockesp_log.c + ${MOCK_GEN_DIR}/Mockhttp_parser.c + ${MOCK_GEN_DIR}/Mockevent_groups.c + ${MOCK_GEN_DIR}/Mockqueue.c + ${MOCK_GEN_DIR}/Mocktask.c + ) + + add_custom_command( + OUTPUT ruby_found SYMBOLIC + COMMAND "ruby" "-v" + COMMENT "Try to find ruby. If this fails, you need to install ruby" + ) + + add_custom_command( + OUTPUT ${MOCK_OUTPUT} + DEPENDS ruby_found + COMMAND ${CMAKE_COMMAND} -E env "UNITY_DIR=${IDF_PATH}/components/unity/unity" + ruby + ${CMOCK_DIR}/lib/cmock.rb + -o${CMAKE_CURRENT_SOURCE_DIR}/host_test/mocks/config.yaml + ${HEADERS_TO_MOCK} + ) + + add_library(mocks ${srcs}) + target_include_directories(mocks PUBLIC + ${CMAKE_CURRENT_SOURCE_DIR}/host_test/mocks/include + ${tcp_transport_dir}/include + ${esp_tls_dir} + ${freertos_dir}/include + ${esp_event_dir}/include + ${esp_system_dir}/include + ${esp_common_dir}/include + ${esp_wifi_dir}/include + ${esp_hw_support_dir}/include + ${esp_netif_dir}/include + ${log_dir}/include + ${esp_rom_dir}/include + ${mbedtls_dir}/port/include + ${nghttp_dir}/port/include + ${mbedtls_dir}/mbedtls/include + ${freertos_dir}/include/freertos + esp-mqtt/lib/include + ${MOCK_GEN_DIR} + ) + target_link_libraries(mocks PUBLIC ${cmock_lib}) + target_compile_definitions(mocks PUBLIC + CONFIG_LOG_MAXIMUM_LEVEL=5 + CONFIG_LOG_DEFAULT_LEVEL=3 + CONFIG_ESP_TLS_USING_MBEDTLS + CONFIG_ESP_TLS_SERVER + CONFIG_LOG_TIMESTAMP_SOURCE_RTOS) + target_link_options(${COMPONENT_LIB} INTERFACE -fsanitize=address) + + target_link_libraries(${COMPONENT_LIB} PUBLIC mocks) + +else() + idf_component_get_property(nghttp_lib nghttp COMPONENT_LIB) + idf_component_get_property(tcp_transport_lib tcp_transport COMPONENT_LIB) + target_link_libraries(${COMPONENT_LIB} PUBLIC ${nghttp_lib} ${tcp_transport_lib}) +endif() diff --git a/components/mqtt/host_test/CMakeLists.txt b/components/mqtt/host_test/CMakeLists.txt new file mode 100644 index 0000000..312ad1e --- /dev/null +++ b/components/mqtt/host_test/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.5) + +include($ENV{IDF_PATH}/tools/cmake/project.cmake) +set(COMPONENTS main) +option(TEST_BUILD "" ON) +project(host_mqtt_client_test) diff --git a/components/mqtt/host_test/README.md b/components/mqtt/host_test/README.md new file mode 100644 index 0000000..071cd94 --- /dev/null +++ b/components/mqtt/host_test/README.md @@ -0,0 +1,27 @@ +# Description + +This directory contains test code for the mqtt client that runs on host. + +Tests are written using [Catch2](https://github.com/catchorg/Catch2) test framework + +# Build + +Tests build regularly like an idf project. + +``` +idf.py build +``` + +# Run + +The build produces an executable in the build folder. + +Just run: + +``` +./build/host_mqtt_client_test.elf +``` + +The test executable have some options provided by the test framework. + + diff --git a/components/mqtt/host_test/main/CMakeLists.txt b/components/mqtt/host_test/main/CMakeLists.txt new file mode 100644 index 0000000..c89e9bf --- /dev/null +++ b/components/mqtt/host_test/main/CMakeLists.txt @@ -0,0 +1,3 @@ +idf_component_register(SRCS "test_mqtt_client.cpp" + INCLUDE_DIRS "$ENV{IDF_PATH}/tools/catch" + REQUIRES cmock mqtt) diff --git a/components/mqtt/host_test/main/test_mqtt_client.cpp b/components/mqtt/host_test/main/test_mqtt_client.cpp new file mode 100644 index 0000000..51ef9cf --- /dev/null +++ b/components/mqtt/host_test/main/test_mqtt_client.cpp @@ -0,0 +1,109 @@ +#define CATCH_CONFIG_MAIN // This tells the catch header to generate a main +#include "catch.hpp" +#include "mqtt_client.h" + +extern "C" { +#include "Mockesp_event.h" +#include "Mockesp_log.h" +#include "Mockesp_system.h" +#include "Mockesp_mac.h" +#include "Mockesp_transport.h" +#include "Mockesp_transport_ssl.h" +#include "Mockesp_transport_tcp.h" +#include "Mockesp_transport_ws.h" +#include "Mockevent_groups.h" +#include "Mockhttp_parser.h" +#include "Mockqueue.h" +#include "Mocktask.h" + + /* + * The following functions are not directly called but the generation of them + * from cmock is broken, so we need to define them here. + */ + BaseType_t xQueueTakeMutexRecursive(QueueHandle_t xMutex, + TickType_t xTicksToWait) + { + return 0; + } + BaseType_t xQueueGiveMutexRecursive(QueueHandle_t xMutex) + { + return 0; + } +} + +struct ClientInitializedFixture { + esp_mqtt_client_handle_t client; + ClientInitializedFixture() + { + TEST_PROTECT(); + int mtx; + int transport_list; + int transport; + int event_group; + uint8_t mac[] = {0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55}; + esp_log_write_Ignore(); + xQueueCreateMutex_ExpectAnyArgsAndReturn( + reinterpret_cast(&mtx)); + xEventGroupCreate_IgnoreAndReturn(reinterpret_cast(&event_group)); + esp_log_timestamp_IgnoreAndReturn(0); + esp_transport_list_init_IgnoreAndReturn(reinterpret_cast(&transport_list)); + esp_transport_tcp_init_IgnoreAndReturn(reinterpret_cast(&transport)); + esp_transport_ssl_init_IgnoreAndReturn(reinterpret_cast(&transport)); + esp_transport_ws_init_IgnoreAndReturn(reinterpret_cast(&transport)); + esp_transport_ws_set_subprotocol_IgnoreAndReturn(ESP_OK); + esp_transport_list_add_IgnoreAndReturn(ESP_OK); + esp_transport_set_default_port_IgnoreAndReturn(ESP_OK); + http_parser_parse_url_IgnoreAndReturn(0); + http_parser_url_init_ExpectAnyArgs(); + esp_event_loop_create_IgnoreAndReturn(ESP_OK); + esp_read_mac_IgnoreAndReturn(ESP_OK); + esp_read_mac_ReturnThruPtr_mac(mac); + esp_transport_list_destroy_IgnoreAndReturn(ESP_OK); + vEventGroupDelete_Ignore(); + vQueueDelete_Ignore(); + + esp_mqtt_client_config_t config{}; + client = esp_mqtt_client_init(&config); + } + ~ClientInitializedFixture() + { + esp_mqtt_client_destroy(client); + } +}; +TEST_CASE_METHOD(ClientInitializedFixture, "Client set uri") +{ + struct http_parser_url ret_uri; + SECTION("User set a correct URI") { + http_parser_parse_url_StopIgnore(); + http_parser_parse_url_ExpectAnyArgsAndReturn(0); + http_parser_parse_url_ReturnThruPtr_u(&ret_uri); + auto res = esp_mqtt_client_set_uri(client, " "); + REQUIRE(res == ESP_OK); + } + SECTION("Incorrect URI from user") { + http_parser_parse_url_StopIgnore(); + http_parser_parse_url_ExpectAnyArgsAndReturn(1); + http_parser_parse_url_ReturnThruPtr_u(&ret_uri); + auto res = esp_mqtt_client_set_uri(client, " "); + REQUIRE(res == ESP_FAIL); + } +} +TEST_CASE_METHOD(ClientInitializedFixture, "Client Start") +{ + SECTION("Successful start") { + xTaskCreatePinnedToCore_ExpectAnyArgsAndReturn(pdTRUE); + auto res = esp_mqtt_client_start(client); + REQUIRE(res == ESP_OK); + } + SECTION("Failed on initialization") { + xTaskCreatePinnedToCore_ExpectAnyArgsAndReturn(pdFALSE); + auto res = esp_mqtt_client_start(nullptr); + REQUIRE(res == ESP_ERR_INVALID_ARG); + } + SECTION("Client already started") {} + SECTION("Failed to start task") { + xTaskCreatePinnedToCore_ExpectAnyArgsAndReturn(pdFALSE); + auto res = esp_mqtt_client_start(client); + REQUIRE(res == ESP_FAIL); + } +} diff --git a/components/mqtt/host_test/mocks/config.yaml b/components/mqtt/host_test/mocks/config.yaml new file mode 100644 index 0000000..8fc80e9 --- /dev/null +++ b/components/mqtt/host_test/mocks/config.yaml @@ -0,0 +1,23 @@ + :cmock: + :plugins: + - expect + - expect_any_args + - return_thru_ptr + - ignore + - array + - callback + :includes_h_pre_orig_header: + - FreeRTOS.h + - net/if.h + :strippables: + - '(?:__attribute__\s*\(+.*?\)+)' + - '(?:vQueueAddToRegistry\s*\(+.*?\)+)' + - '(?:vQueueUnregisterQueue\s*\(+.*?\)+)' + - '(?:pcQueueGetName\s*\(+.*?\)+)' + - '(?:xQueueTakeMutexRecursive\s*\(+.*?\)+)' + - '(?:xQueueGiveMutexRecursive\s*\(+.*?\)+)' + - '(?:vTaskSetThreadLocalStoragePointerAndDelCallback\s*\(+.*?\)+)' + - '(?:esp_log_writev\s*\(+.*?\)+)' + - '(?:esp_restart\s*\(+.*?\)+)' + - '(?:esp_system_abort\s*\(+.*?\)+)' + - PRIVILEGED_FUNCTION diff --git a/components/mqtt/host_test/mocks/include/freertos/FreeRTOSConfig.h b/components/mqtt/host_test/mocks/include/freertos/FreeRTOSConfig.h new file mode 100644 index 0000000..0492f81 --- /dev/null +++ b/components/mqtt/host_test/mocks/include/freertos/FreeRTOSConfig.h @@ -0,0 +1,133 @@ +/* + FreeRTOS V8.2.0 - Copyright (C) 2015 Real Time Engineers Ltd. + All rights reserved + + VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION. + + This file is part of the FreeRTOS distribution. + + FreeRTOS is free software; you can redistribute it and/or modify it under + the terms of the GNU General Public License (version 2) as published by the + Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception. + + *************************************************************************** + >>! NOTE: The modification to the GPL is included to allow you to !<< + >>! distribute a combined work that includes FreeRTOS without being !<< + >>! obliged to provide the source code for proprietary components !<< + >>! outside of the FreeRTOS kernel. !<< + *************************************************************************** + + FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + FOR A PARTICULAR PURPOSE. Full license text is available on the following + link: http://www.freertos.org/a00114.html + + *************************************************************************** + * * + * FreeRTOS provides completely free yet professionally developed, * + * robust, strictly quality controlled, supported, and cross * + * platform software that is more than just the market leader, it * + * is the industry's de facto standard. * + * * + * Help yourself get started quickly while simultaneously helping * + * to support the FreeRTOS project by purchasing a FreeRTOS * + * tutorial book, reference manual, or both: * + * http://www.FreeRTOS.org/Documentation * + * * + *************************************************************************** + + http://www.FreeRTOS.org/FAQHelp.html - Having a problem? Start by reading + the FAQ page "My application does not run, what could be wrong?". Have you + defined configASSERT()? + + http://www.FreeRTOS.org/support - In return for receiving this top quality + embedded software for free we request you assist our global community by + participating in the support forum. + + http://www.FreeRTOS.org/training - Investing in training allows your team to + be as productive as possible as early as possible. Now you can receive + FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers + Ltd, and the world's leading authority on the world's leading RTOS. + + http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products, + including FreeRTOS+Trace - an indispensable productivity tool, a DOS + compatible FAT file system, and our tiny thread aware UDP/IP stack. + + http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate. + Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS. + + http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High + Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS + licenses offer ticketed support, indemnification and commercial middleware. + + http://www.SafeRTOS.com - High Integrity Systems also provide a safety + engineered and independently SIL3 certified version for use in safety and + mission critical applications that require provable dependability. + + 1 tab == 4 spaces! +*/ + +#ifndef FREERTOS_CONFIG_H +#define FREERTOS_CONFIG_H + +#include "esp_attr.h" + +/*----------------------------------------------------------- + * Application specific definitions. + * + * These definitions should be adjusted for your particular hardware and + * application requirements. + * + * THESE PARAMETERS ARE DESCRIBED WITHIN THE 'CONFIGURATION' SECTION OF THE + * FreeRTOS API DOCUMENTATION AVAILABLE ON THE FreeRTOS.org WEB SITE. + * + * See http://www.freertos.org/a00110.html. + *----------------------------------------------------------*/ + +#define configUSE_PREEMPTION 1 +#define configUSE_IDLE_HOOK 1 +#define configUSE_TICK_HOOK 1 +#define configTICK_RATE_HZ ((TickType_t)1000) +#define configMINIMAL_STACK_SIZE ((unsigned short)256) /* This can be made smaller if required. */ +#define configTOTAL_HEAP_SIZE ((size_t)(32 * 1024)) +#define configMAX_TASK_NAME_LEN (16) +#define configUSE_TRACE_FACILITY 1 +#define configUSE_16_BIT_TICKS 1 +#define configIDLE_SHOULD_YIELD 1 +#define configUSE_CO_ROUTINES 1 +#define configUSE_MUTEXES 1 +#define configUSE_COUNTING_SEMAPHORES 1 +#define configUSE_ALTERNATIVE_API 0 +#define configUSE_RECURSIVE_MUTEXES 1 +#define configCHECK_FOR_STACK_OVERFLOW 0 /* Do not use this option on the PC port. */ +#define configUSE_APPLICATION_TASK_TAG 1 +#define configQUEUE_REGISTRY_SIZE 0 + +#define configMAX_PRIORITIES (10) +#define configMAX_CO_ROUTINE_PRIORITIES (2) + +/* Set the following definitions to 1 to include the API function, or zero +to exclude the API function. */ + +#define INCLUDE_vTaskPrioritySet 1 +#define INCLUDE_uxTaskPriorityGet 1 +#define INCLUDE_vTaskDelete 1 +#define INCLUDE_vTaskCleanUpResources 1 +#define INCLUDE_vTaskSuspend 1 +#define INCLUDE_vTaskDelayUntil 1 +#define INCLUDE_vTaskDelay 1 +#define INCLUDE_uxTaskGetStackHighWaterMark 0 /* Do not use this option on the PC port. */ + +/* This demo makes use of one or more example stats formatting functions. These +format the raw data provided by the uxTaskGetSystemState() function in to human +readable ASCII form. See the notes in the implementation of vTaskList() within +FreeRTOS/Source/tasks.c for limitations. */ +#define configUSE_STATS_FORMATTING_FUNCTIONS 1 + +/* An example "task switched in" hook macro definition. */ +#define traceTASK_SWITCHED_IN() xTaskCallApplicationTaskHook(NULL, (void*)0xabcd) + +extern void vMainQueueSendPassed(void); +#define traceQUEUE_SEND(pxQueue) vMainQueueSendPassed() + +#endif /* FREERTOS_CONFIG_H */ diff --git a/components/mqtt/host_test/mocks/include/freertos/portmacro.h b/components/mqtt/host_test/mocks/include/freertos/portmacro.h new file mode 100644 index 0000000..67ce5f0 --- /dev/null +++ b/components/mqtt/host_test/mocks/include/freertos/portmacro.h @@ -0,0 +1,199 @@ +/* + * FreeRTOS Kernel V10.2.1 + * Copyright (C) 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of + * the Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + * http://www.FreeRTOS.org + * http://aws.amazon.com/freertos + * + * 1 tab == 4 spaces! + */ +#ifndef PORTMACRO_H +#define PORTMACRO_H + +#include +#include +#include +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +#ifndef __ASSEMBLER__ + +/*----------------------------------------------------------- + * Port specific definitions. + * + * The settings in this file configure FreeRTOS correctly for the + * given hardware and compiler. + * + * These settings should not be altered. + *----------------------------------------------------------- + */ + +/* Type definitions. */ +#define portCHAR uint8_t +#define portFLOAT float +#define portDOUBLE double +#define portLONG int32_t +#define portSHORT int16_t +#define portSTACK_TYPE uint8_t +#define portBASE_TYPE int +// interrupt module will mask interrupt with priority less than threshold +#define RVHAL_EXCM_LEVEL 4 + +typedef portSTACK_TYPE StackType_t; +typedef portBASE_TYPE BaseType_t; +typedef unsigned portBASE_TYPE UBaseType_t; + +#if (configUSE_16_BIT_TICKS == 1) +typedef uint16_t TickType_t; +#define portMAX_DELAY (TickType_t)0xffff +#else +typedef uint32_t TickType_t; +#define portMAX_DELAY (TickType_t)0xffffffffUL +#endif +/*------------------------------------------------------*/ + +/* Architecture specifics. */ +#define portSTACK_GROWTH (-1) +#define portTICK_PERIOD_MS ((TickType_t)(1000 / configTICK_RATE_HZ)) +#define portBYTE_ALIGNMENT 16 +/*-----------------------------------------------------------*/ + +#define portCRITICAL_NESTING_IN_TCB 0 + +/* + * Send an interrupt to another core in order to make the task running + * on it yield for a higher-priority task. + */ +void vPortYieldOtherCore(BaseType_t coreid); + +/* + Callback to set a watchpoint on the end of the stack. Called every context switch to change the stack + watchpoint around. + */ +void vPortSetStackWatchpoint(void *pxStackStart); + +/* + * Returns true if the current core is in ISR context; low prio ISR, med prio ISR or timer tick ISR. High prio ISRs + * aren't detected here, but they normally cannot call C code, so that should not be an issue anyway. + */ +BaseType_t xPortInIsrContext(void); + +/* + * This function will be called in High prio ISRs. Returns true if the current core was in ISR context + * before calling into high prio ISR context. + */ +BaseType_t xPortInterruptedFromISRContext(void); + +/* "mux" data structure (spinlock) */ +typedef struct { + /* owner field values: + * 0 - Uninitialized (invalid) + * portMUX_FREE_VAL - Mux is free, can be locked by either CPU + * CORE_ID_REGVAL_PRO / CORE_ID_REGVAL_APP - Mux is locked to the particular core + * + * + * Any value other than portMUX_FREE_VAL, CORE_ID_REGVAL_PRO, CORE_ID_REGVAL_APP indicates corruption + */ + uint32_t owner; + /* count field: + * If mux is unlocked, count should be zero. + * If mux is locked, count is non-zero & represents the number of recursive locks on the mux. + */ + uint32_t count; +#ifdef CONFIG_FREERTOS_PORTMUX_DEBUG + const char *lastLockedFn; + int lastLockedLine; +#endif +} portMUX_TYPE; + +#define portMUX_FREE_VAL SPINLOCK_FREE + +/* Special constants for vPortCPUAcquireMutexTimeout() */ +#define portMUX_NO_TIMEOUT SPINLOCK_WAIT_FOREVER /* When passed for 'timeout_cycles', spin forever if necessary */ +#define portMUX_TRY_LOCK SPINLOCK_NO_WAIT /* Try to acquire the spinlock a single time only */ + +// Keep this in sync with the portMUX_TYPE struct definition please. +#ifndef CONFIG_FREERTOS_PORTMUX_DEBUG +#define portMUX_INITIALIZER_UNLOCKED \ + { .owner = portMUX_FREE_VAL, .count = 0, } +#else +#define portMUX_INITIALIZER_UNLOCKED \ + { .owner = portMUX_FREE_VAL, .count = 0, .lastLockedFn = "(never locked)", .lastLockedLine = -1 } +#endif + +/* Scheduler utilities. */ +extern void vPortYield(void); +extern void vPortYieldFromISR(void); + +#define portYIELD() vPortYield() +#define portYIELD_FROM_ISR() vPortYieldFromISR() + +/* Yielding within an API call (when interrupts are off), means the yield should be delayed + until interrupts are re-enabled. + To do this, we use the "cross-core" interrupt as a trigger to yield on this core when interrupts are re-enabled.This + is the same interrupt & code path which is used to trigger a yield between CPUs, although in this case the yield is + happening on the same CPU. +*/ +#define portYIELD_WITHIN_API() portYIELD() +/*-----------------------------------------------------------*/ + +/* Critical section management. */ +extern int vPortSetInterruptMask(void); +extern void vPortClearInterruptMask(int); + +void vPortCPUInitializeMutex(portMUX_TYPE *mux); +void vPortCPUAcquireMutex(portMUX_TYPE *mux); +bool vPortCPUAcquireMutexTimeout(portMUX_TYPE *mux, int timeout_cycles); +void vPortCPUReleaseMutex(portMUX_TYPE *mux); + +extern void vPortEnterCritical(void); +extern void vPortExitCritical(void); + +extern void esp_vApplicationIdleHook(void); +extern void esp_vApplicationTickHook(void); + +#ifndef CONFIG_FREERTOS_LEGACY_HOOKS +#define vApplicationIdleHook esp_vApplicationIdleHook +#define vApplicationTickHook esp_vApplicationTickHook +#endif /* !CONFIG_FREERTOS_LEGACY_HOOKS */ + +/* Task function macros as described on the FreeRTOS.org WEB site. */ +#define portTASK_FUNCTION_PROTO(vFunction, pvParameters) void vFunction(void* pvParameters) +#define portTASK_FUNCTION(vFunction, pvParameters) void vFunction(void* pvParameters) + +void vApplicationSleep(TickType_t xExpectedIdleTime); +#define portSUPPRESS_TICKS_AND_SLEEP(idleTime) vApplicationSleep(idleTime) + +#define portNOP() //__asm volatile ( " nop " ) + +#define portVALID_TCB_MEM(ptr) // esp_ptr_byte_accessible(ptr) +#define portVALID_STACK_MEM(ptr) // esp_ptr_byte_accessible(ptr) + +#endif //__ASSEMBLER__ + +#ifdef __cplusplus +} +#endif + +#endif /* PORTMACRO_H */ diff --git a/components/mqtt/host_test/mocks/include/machine/endian.h b/components/mqtt/host_test/mocks/include/machine/endian.h new file mode 100644 index 0000000..228316d --- /dev/null +++ b/components/mqtt/host_test/mocks/include/machine/endian.h @@ -0,0 +1,2 @@ +#pragma once +#include_next diff --git a/components/mqtt/host_test/mocks/include/sys/queue.h b/components/mqtt/host_test/mocks/include/sys/queue.h new file mode 100644 index 0000000..5ec7fec --- /dev/null +++ b/components/mqtt/host_test/mocks/include/sys/queue.h @@ -0,0 +1,66 @@ +#pragma once + +/* Implementation from BSD headers*/ +#define QMD_SAVELINK(name, link) void **name = (void *)&(link) +#define TRASHIT(x) do {(x) = (void *)-1;} while (0) +#define STAILQ_NEXT(elm, field) ((elm)->field.stqe_next) + +#define STAILQ_FIRST(head) ((head)->stqh_first) + +#define STAILQ_HEAD(name, type) \ +struct name { \ + struct type *stqh_first;/* first element */ \ + struct type **stqh_last;/* addr of last next element */ \ +} + +#define STAILQ_ENTRY(type) \ +struct { \ + struct type *stqe_next; /* next element */ \ +} + +#define STAILQ_INSERT_TAIL(head, elm, field) do { \ + STAILQ_NEXT((elm), field) = NULL; \ + *(head)->stqh_last = (elm); \ + (head)->stqh_last = &STAILQ_NEXT((elm), field); \ +} while (0) + +#define STAILQ_INIT(head) do { \ + STAILQ_FIRST((head)) = NULL; \ + (head)->stqh_last = &STAILQ_FIRST((head)); \ +} while (0) + +#define STAILQ_FOREACH(var, head, field) \ + for((var) = STAILQ_FIRST((head)); \ + (var); \ + (var) = STAILQ_NEXT((var), field)) + +#define STAILQ_FOREACH_SAFE(var, head, field, tvar) \ + for ((var) = STAILQ_FIRST((head)); \ + (var) && ((tvar) = STAILQ_NEXT((var), field), 1); \ + (var) = (tvar)) + +#define STAILQ_REMOVE_AFTER(head, elm, field) do { \ + if ((STAILQ_NEXT(elm, field) = \ + STAILQ_NEXT(STAILQ_NEXT(elm, field), field)) == NULL) \ + (head)->stqh_last = &STAILQ_NEXT((elm), field); \ +} while (0) + +#define STAILQ_REMOVE_HEAD(head, field) do { \ + if ((STAILQ_FIRST((head)) = \ + STAILQ_NEXT(STAILQ_FIRST((head)), field)) == NULL) \ + (head)->stqh_last = &STAILQ_FIRST((head)); \ +} while (0) + +#define STAILQ_REMOVE(head, elm, type, field) do { \ + QMD_SAVELINK(oldnext, (elm)->field.stqe_next); \ + if (STAILQ_FIRST((head)) == (elm)) { \ + STAILQ_REMOVE_HEAD((head), field); \ + } \ + else { \ + struct type *curelm = STAILQ_FIRST((head)); \ + while (STAILQ_NEXT(curelm, field) != (elm)) \ + curelm = STAILQ_NEXT(curelm, field); \ + STAILQ_REMOVE_AFTER(head, curelm, field); \ + } \ + TRASHIT(*oldnext); \ +} while (0) diff --git a/components/mqtt/host_test/sdkconfig.defaults b/components/mqtt/host_test/sdkconfig.defaults new file mode 100644 index 0000000..c126429 --- /dev/null +++ b/components/mqtt/host_test/sdkconfig.defaults @@ -0,0 +1,6 @@ +CONFIG_IDF_TARGET="linux" +CONFIG_COMPILER_CXX_EXCEPTIONS=y +CONFIG_COMPILER_CXX_RTTI=y +CONFIG_COMPILER_CXX_EXCEPTIONS_EMG_POOL_SIZE=0 +CONFIG_COMPILER_STACK_CHECK_NONE=y +CONFIG_UNITY_ENABLE_IDF_TEST_RUNNER=n From 382caedbbbf967b8019f30fafd8cc5f95ab0a64b Mon Sep 17 00:00:00 2001 From: Zim Kalinowski Date: Tue, 24 Aug 2021 12:06:21 +0800 Subject: [PATCH 57/91] upgrade freertos version and history --- components/mqtt/host_test/mocks/include/freertos/portmacro.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/mqtt/host_test/mocks/include/freertos/portmacro.h b/components/mqtt/host_test/mocks/include/freertos/portmacro.h index 67ce5f0..1ff7b7c 100644 --- a/components/mqtt/host_test/mocks/include/freertos/portmacro.h +++ b/components/mqtt/host_test/mocks/include/freertos/portmacro.h @@ -1,5 +1,5 @@ /* - * FreeRTOS Kernel V10.2.1 + * FreeRTOS Kernel V10.4.3 * Copyright (C) 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. * * Permission is hereby granted, free of charge, to any person obtaining a copy of From c03353333ba04921913e7ba23205e9d5b83729e1 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Wed, 22 Sep 2021 13:28:50 +0200 Subject: [PATCH 58/91] MQTT: Fix build if CONFIG_WS_TRANSPORT is not set and client cleanups Updated MQTT submodule: git log --oneline f10321a53b53a146ee299cfecc320b89c0cf6611...89894bd0c611b1392967fe90bb49682eba858383 * Fix build issue if cert bundle disabled * Fix build issue if ws transport disabled * Add config to set retransmission interval Detailed description of the changes (https://github.com/espressif/esp-mqtt/compare/f10321a53b53a146ee299cfecc320b89c0cf6611...89894bd0c611b1392967fe90bb49682eba858383): * Added config option to configure custom retransmission interval - See merge request espressif/esp-mqtt!110 - esp_mqtt commit https://github.com/espressif/esp-mqtt/commit/1b009c840b78a14f59b6ada70d36aca9ad6303f0 - Related https://github.com/espressif/esp-mqtt/pull/199 * Configuration conflicts were verified, logged but not reported to the user. - See merge request espressif/esp-mqtt!102 - esp_mqtt commit https://github.com/espressif/esp-mqtt/commit/88f4b8ed50af6989efba3f87a14882b276e2deb7 * Fixed build issue if cert bundle disabled - See merge request espressif/esp-mqtt!109 - esp_mqtt commit https://github.com/espressif/esp-mqtt/commit/4a89bff610b86f534f67d032a3e1549d8c4da95f - esp_mqtt commit https://github.com/espressif/esp-mqtt/commit/1b719805756f60140312f8e314f732c0a177f74f - esp_mqtt commit https://github.com/espressif/esp-mqtt/commit/5b3c81ee482c815e2602f5d16adb0e197b9e3695 - Related https://github.com/espressif/esp-mqtt/pull/198 - Related https://github.com/espressif/esp-idf/issues/7535 * Removes unnecessary outbox_cleanup - This function were used on old version to handle QoS 2 messages. It's no longer necessary in current implementation. - See merge request espressif/esp-mqtt!108 - esp_mqtt commit https://github.com/espressif/esp-mqtt/commit/ebef896b00c1e0b48107a074f786096479b8c9ee * Fixed return an error when fail to enqueue - The functions that enqueue messages didn't had a return for the handler, with this the error was only logged instead of returned whichmay cause the user to have an ID for a message that was not published. - See merge request espressif/esp-mqtt!103 - esp_mqtt commit https://github.com/espressif/esp-mqtt/commit/7471177fe77aea8c0d85d8008867aa8d6ff57424 * CI: Use qemu image based on esp-env:v4.4-1 - Replaced the temporary qemu image with the official qemu:v4.4-1-20210517 derived from the esp-env:v4.4-1 test environment - See merge request espressif/esp-mqtt!107 - esp_mqtt commit https://github.com/espressif/esp-mqtt/commit/231b2749628b59df997750e82d6470d68735798b Closes https://github.com/espressif/esp-idf/issues/7535 --- components/mqtt/esp-mqtt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/mqtt/esp-mqtt b/components/mqtt/esp-mqtt index f10321a..89894bd 160000 --- a/components/mqtt/esp-mqtt +++ b/components/mqtt/esp-mqtt @@ -1 +1 @@ -Subproject commit f10321a53b53a146ee299cfecc320b89c0cf6611 +Subproject commit 89894bd0c611b1392967fe90bb49682eba858383 From 3c764f0fc1e458f2e9d10534a7acaab1e58b4e58 Mon Sep 17 00:00:00 2001 From: Sudeep Mohanty Date: Mon, 20 Sep 2021 11:37:03 +0530 Subject: [PATCH 59/91] freertos: update freertos folder structure to match upstream The following changes have been made: 1. All FreeRTOS kernel source files are now placed in the freertos/FreeRTOS-Kernel folder to match with the upstream folder structure. 2. All kernel include files are now placed in freertos/FreeRTOS-Kernel/include. 3. All port files are now placed in freertos/FreeRTOS-Kernel/portable. 4. All additions/customizations are placed in freertos/esp_additions. 5. All other miscellaneous files (README, License files etc.) are moved to freertos/FreeRTOS-Kernel folder to match with the upstream. 6. Updated esp-cryptoauthlib to latest commit to resolve FreeRTOS include dependencies. Signed-off-by: Sudeep Mohanty --- components/mqtt/CMakeLists.txt | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/components/mqtt/CMakeLists.txt b/components/mqtt/CMakeLists.txt index e88c582..d97d3a6 100644 --- a/components/mqtt/CMakeLists.txt +++ b/components/mqtt/CMakeLists.txt @@ -57,9 +57,9 @@ idf_component_get_property(mbedtls_dir mbedtls COMPONENT_DIR) ${esp_hw_support_dir}/include/esp_random.h ${esp_system_dir}/include/esp_system.h ${esp_tls_dir}/esp_tls.h - ${freertos_dir}/include/freertos/queue.h - ${freertos_dir}/include/freertos/task.h - ${freertos_dir}/include/freertos/event_groups.h + ${freertos_dir}/FreeRTOS-Kernel/include/freertos/queue.h + ${freertos_dir}/FreeRTOS-Kernel/include/freertos/task.h + ${freertos_dir}/FreeRTOS-Kernel/include/freertos/event_groups.h ${log_dir}/include/esp_log.h ${nghttp_dir}/port/include/http_parser.h ) @@ -103,7 +103,7 @@ idf_component_get_property(mbedtls_dir mbedtls COMPONENT_DIR) ${CMAKE_CURRENT_SOURCE_DIR}/host_test/mocks/include ${tcp_transport_dir}/include ${esp_tls_dir} - ${freertos_dir}/include + ${freertos_dir}/FreeRTOS-Kernel/include ${esp_event_dir}/include ${esp_system_dir}/include ${esp_common_dir}/include @@ -115,7 +115,7 @@ idf_component_get_property(mbedtls_dir mbedtls COMPONENT_DIR) ${mbedtls_dir}/port/include ${nghttp_dir}/port/include ${mbedtls_dir}/mbedtls/include - ${freertos_dir}/include/freertos + ${freertos_dir}/FreeRTOS-Kernel/include/freertos esp-mqtt/lib/include ${MOCK_GEN_DIR} ) From b4474ac16dcc4e2561a53cb604131cdc03d40677 Mon Sep 17 00:00:00 2001 From: Roland Dobai Date: Fri, 5 Nov 2021 15:38:25 +0100 Subject: [PATCH 60/91] Build & config: Remove leftover files from the unsupported "make" build system --- components/mqtt/component.mk | 4 ---- components/mqtt/test/component.mk | 4 ---- 2 files changed, 8 deletions(-) delete mode 100644 components/mqtt/component.mk delete mode 100644 components/mqtt/test/component.mk diff --git a/components/mqtt/component.mk b/components/mqtt/component.mk deleted file mode 100644 index 19e4980..0000000 --- a/components/mqtt/component.mk +++ /dev/null @@ -1,4 +0,0 @@ -COMPONENT_SUBMODULES += esp-mqtt -COMPONENT_ADD_INCLUDEDIRS := esp-mqtt/include -COMPONENT_SRCDIRS := esp-mqtt esp-mqtt/lib -COMPONENT_PRIV_INCLUDEDIRS := esp-mqtt/lib/include diff --git a/components/mqtt/test/component.mk b/components/mqtt/test/component.mk deleted file mode 100644 index 8c6eb51..0000000 --- a/components/mqtt/test/component.mk +++ /dev/null @@ -1,4 +0,0 @@ -# -#Component Makefile -# -COMPONENT_ADD_LDFLAGS = -Wl,--whole-archive -l$(COMPONENT_NAME) -Wl,--no-whole-archive From ff8a93585f60043d2b12c21301531789957fe169 Mon Sep 17 00:00:00 2001 From: Darian Leung Date: Sat, 30 Oct 2021 00:48:19 +0800 Subject: [PATCH 61/91] freertos: Add portTRY_ENTRY_CRITICAL() and deprecate legacy spinlock fucntions Add TRY_ENTRY_CRITICAL() API to all for timeouts when entering critical sections. The following port API were added: - portTRY_ENTER_CRITICAL() - portTRY_ENTER_CRITICAL_ISR() - portTRY_ENTER_CRITICAL_SAFE() Deprecated legacy spinlock API in favor of spinlock.h. The following API were deprecated: - vPortCPUInitializeMutex() - vPortCPUAcquireMutex() - vPortCPUAcquireMutexTimeout() - vPortCPUReleaseMutex() Other Changes: - Added portMUX_INITIALIZE() to replace vPortCPUInitializeMutex() - The assembly of the critical section functions ends up being about 50 instructions longer, thus the spinlock test pass threshold had to be increased to account for the extra runtime. Closes https://github.com/espressif/esp-idf/issues/5301 --- .../host_test/mocks/include/freertos/portmacro.h | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/components/mqtt/host_test/mocks/include/freertos/portmacro.h b/components/mqtt/host_test/mocks/include/freertos/portmacro.h index 1ff7b7c..a68d3c4 100644 --- a/components/mqtt/host_test/mocks/include/freertos/portmacro.h +++ b/components/mqtt/host_test/mocks/include/freertos/portmacro.h @@ -121,10 +121,6 @@ typedef struct { * If mux is locked, count is non-zero & represents the number of recursive locks on the mux. */ uint32_t count; -#ifdef CONFIG_FREERTOS_PORTMUX_DEBUG - const char *lastLockedFn; - int lastLockedLine; -#endif } portMUX_TYPE; #define portMUX_FREE_VAL SPINLOCK_FREE @@ -134,13 +130,8 @@ typedef struct { #define portMUX_TRY_LOCK SPINLOCK_NO_WAIT /* Try to acquire the spinlock a single time only */ // Keep this in sync with the portMUX_TYPE struct definition please. -#ifndef CONFIG_FREERTOS_PORTMUX_DEBUG #define portMUX_INITIALIZER_UNLOCKED \ { .owner = portMUX_FREE_VAL, .count = 0, } -#else -#define portMUX_INITIALIZER_UNLOCKED \ - { .owner = portMUX_FREE_VAL, .count = 0, .lastLockedFn = "(never locked)", .lastLockedLine = -1 } -#endif /* Scheduler utilities. */ extern void vPortYield(void); @@ -162,11 +153,6 @@ extern void vPortYieldFromISR(void); extern int vPortSetInterruptMask(void); extern void vPortClearInterruptMask(int); -void vPortCPUInitializeMutex(portMUX_TYPE *mux); -void vPortCPUAcquireMutex(portMUX_TYPE *mux); -bool vPortCPUAcquireMutexTimeout(portMUX_TYPE *mux, int timeout_cycles); -void vPortCPUReleaseMutex(portMUX_TYPE *mux); - extern void vPortEnterCritical(void); extern void vPortExitCritical(void); From 74882b18f0182ad89ad792e3412468038ecabacd Mon Sep 17 00:00:00 2001 From: David Cermak Date: Mon, 4 Oct 2021 10:24:31 +0200 Subject: [PATCH 62/91] MQTT: Add more unit tests with actual broker --- components/mqtt/test/Kconfig | 9 ++ components/mqtt/test/connection.c | 138 ++++++++++++++++++++++++++++++ components/mqtt/test/test_mqtt.c | 54 +++++++++++- 3 files changed, 199 insertions(+), 2 deletions(-) create mode 100644 components/mqtt/test/Kconfig create mode 100644 components/mqtt/test/connection.c diff --git a/components/mqtt/test/Kconfig b/components/mqtt/test/Kconfig new file mode 100644 index 0000000..e006f92 --- /dev/null +++ b/components/mqtt/test/Kconfig @@ -0,0 +1,9 @@ +menu "ESP-MQTT Unit Test Config" + + config MQTT_TEST_BROKER_URI + string "URI of the test broker" + default "mqtt://mqtt.eclipseprojects.io" + help + URL of an mqtt broker which this test connects to. + +endmenu diff --git a/components/mqtt/test/connection.c b/components/mqtt/test/connection.c new file mode 100644 index 0000000..213d6b3 --- /dev/null +++ b/components/mqtt/test/connection.c @@ -0,0 +1,138 @@ +/* + * SPDX-FileCopyrightText: 2021 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ +#include "freertos/FreeRTOS.h" +#include "freertos/event_groups.h" +#include "unity.h" +#include "esp_event.h" +#include "esp_eth.h" +#include "esp_log.h" + +#if SOC_EMAC_SUPPORTED +#define ETH_START_BIT BIT(0) +#define ETH_STOP_BIT BIT(1) +#define ETH_CONNECT_BIT BIT(2) +#define ETH_GOT_IP_BIT BIT(3) +#define ETH_STOP_TIMEOUT_MS (10000) +#define ETH_GET_IP_TIMEOUT_MS (60000) + +static const char *TAG = "esp32_eth_test_fixture"; + +/** Event handler for Ethernet events */ +static void eth_event_handler(void *arg, esp_event_base_t event_base, + int32_t event_id, void *event_data) +{ + EventGroupHandle_t eth_event_group = (EventGroupHandle_t)arg; + switch (event_id) { + case ETHERNET_EVENT_CONNECTED: + xEventGroupSetBits(eth_event_group, ETH_CONNECT_BIT); + ESP_LOGI(TAG, "Ethernet Link Up"); + break; + case ETHERNET_EVENT_DISCONNECTED: + ESP_LOGI(TAG, "Ethernet Link Down"); + break; + case ETHERNET_EVENT_START: + xEventGroupSetBits(eth_event_group, ETH_START_BIT); + ESP_LOGI(TAG, "Ethernet Started"); + break; + case ETHERNET_EVENT_STOP: + xEventGroupSetBits(eth_event_group, ETH_STOP_BIT); + ESP_LOGI(TAG, "Ethernet Stopped"); + break; + default: + break; + } +} + +/** Event handler for IP_EVENT_ETH_GOT_IP */ +static void got_ip_event_handler(void *arg, esp_event_base_t event_base, + int32_t event_id, void *event_data) +{ + EventGroupHandle_t eth_event_group = (EventGroupHandle_t)arg; + ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data; + const esp_netif_ip_info_t *ip_info = &event->ip_info; + ESP_LOGI(TAG, "Ethernet Got IP Address"); + ESP_LOGI(TAG, "~~~~~~~~~~~"); + ESP_LOGI(TAG, "ETHIP:" IPSTR, IP2STR(&ip_info->ip)); + ESP_LOGI(TAG, "ETHMASK:" IPSTR, IP2STR(&ip_info->netmask)); + ESP_LOGI(TAG, "ETHGW:" IPSTR, IP2STR(&ip_info->gw)); + ESP_LOGI(TAG, "~~~~~~~~~~~"); + xEventGroupSetBits(eth_event_group, ETH_GOT_IP_BIT); +} + +static esp_err_t test_uninstall_driver(esp_eth_handle_t eth_hdl, uint32_t ms_to_wait) +{ + int i = 0; + ms_to_wait += 100; + for (i = 0; i < ms_to_wait / 100; i++) { + vTaskDelay(pdMS_TO_TICKS(100)); + if (esp_eth_driver_uninstall(eth_hdl) == ESP_OK) { + break; + } + } + if (i < ms_to_wait / 10) { + return ESP_OK; + } else { + return ESP_FAIL; + } +} +static EventGroupHandle_t eth_event_group; +static esp_netif_t *eth_netif; +static esp_eth_mac_t *mac; +static esp_eth_phy_t *phy; +static esp_eth_handle_t eth_handle = NULL; +static esp_eth_netif_glue_handle_t glue; + +void eth_test_fixture_connect(void) +{ + EventBits_t bits; + eth_event_group = xEventGroupCreate(); + TEST_ASSERT(eth_event_group != NULL); + TEST_ESP_OK(esp_event_loop_create_default()); + // create TCP/IP netif + esp_netif_config_t netif_cfg = ESP_NETIF_DEFAULT_ETH(); + eth_netif = esp_netif_new(&netif_cfg); + + eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG(); + mac = esp_eth_mac_new_esp32(&mac_config); + eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG(); + phy = esp_eth_phy_new_ip101(&phy_config); + esp_eth_config_t eth_config = ETH_DEFAULT_CONFIG(mac, phy); + + // install Ethernet driver + TEST_ESP_OK(esp_eth_driver_install(ð_config, ð_handle)); + // combine driver with netif + glue = esp_eth_new_netif_glue(eth_handle); + TEST_ESP_OK(esp_netif_attach(eth_netif, glue)); + // register user defined event handers + TEST_ESP_OK(esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, ð_event_handler, eth_event_group)); + TEST_ESP_OK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &got_ip_event_handler, eth_event_group)); + // start Ethernet driver + TEST_ESP_OK(esp_eth_start(eth_handle)); + /* wait for IP lease */ + bits = xEventGroupWaitBits(eth_event_group, ETH_GOT_IP_BIT, true, true, pdMS_TO_TICKS(ETH_GET_IP_TIMEOUT_MS)); + TEST_ASSERT((bits & ETH_GOT_IP_BIT) == ETH_GOT_IP_BIT); +} + +void eth_test_fixture_deinit(void) +{ + EventBits_t bits; + // stop Ethernet driver + TEST_ESP_OK(esp_eth_stop(eth_handle)); + /* wait for connection stop */ + bits = xEventGroupWaitBits(eth_event_group, ETH_STOP_BIT, true, true, pdMS_TO_TICKS(ETH_STOP_TIMEOUT_MS)); + TEST_ASSERT((bits & ETH_STOP_BIT) == ETH_STOP_BIT); + TEST_ESP_OK(esp_eth_del_netif_glue(glue)); + /* driver should be uninstalled within 2 seconds */ + TEST_ESP_OK(test_uninstall_driver(eth_handle, 2000)); + TEST_ESP_OK(phy->del(phy)); + TEST_ESP_OK(mac->del(mac)); + TEST_ESP_OK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_ETH_GOT_IP, got_ip_event_handler)); + TEST_ESP_OK(esp_event_handler_unregister(ETH_EVENT, ESP_EVENT_ANY_ID, eth_event_handler)); + esp_netif_destroy(eth_netif); + TEST_ESP_OK(esp_event_loop_delete_default()); + vEventGroupDelete(eth_event_group); +} +#endif // SOC_EMAC_SUPPORTED diff --git a/components/mqtt/test/test_mqtt.c b/components/mqtt/test/test_mqtt.c index 1cf0618..5cc0e47 100644 --- a/components/mqtt/test/test_mqtt.c +++ b/components/mqtt/test/test_mqtt.c @@ -1,9 +1,13 @@ +#include +#include "freertos/FreeRTOS.h" +#include "freertos/event_groups.h" +#include "unity.h" #include "test_utils.h" #include "mqtt_client.h" -#include "unity.h" -#include #include "nvs_flash.h" #include "esp_ota_ops.h" +#include "sdkconfig.h" +#include "soc/soc_caps.h" static void test_leak_setup(const char * file, long line) { @@ -68,3 +72,49 @@ TEST_CASE("mqtt enqueue and destroy outbox", "[mqtt][leaks=0]") esp_mqtt_client_destroy(client); } + +#if SOC_EMAC_SUPPORTED + +void eth_test_fixture_connect(void); +void eth_test_fixture_deinit(void); + +static const int CONNECT_BIT = BIT0; +static const int DISCONNECT_BIT = BIT1; + +static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data) +{ + EventGroupHandle_t *event_group = handler_args; + switch ((esp_mqtt_event_id_t)event_id) { + case MQTT_EVENT_CONNECTED: + xEventGroupSetBits(*event_group, CONNECT_BIT); + break; + case MQTT_EVENT_DISCONNECTED: + xEventGroupSetBits(*event_group, DISCONNECT_BIT); + break; + default: + break; + } +} + +TEST_CASE("connect disconnect", "[mqtt][test_env=UT_T2_Ethernet]") +{ + test_leak_setup(__FILE__, __LINE__); + test_case_uses_tcpip(); + eth_test_fixture_connect(); + const int TEST_CONNECT_TIMEOUT = 10000; + const esp_mqtt_client_config_t mqtt_cfg = { + // no connection takes place, but the uri has to be valid for init() to succeed + .uri = CONFIG_MQTT_TEST_BROKER_URI, + }; + EventGroupHandle_t event_group = xEventGroupCreate(); + esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg); + TEST_ASSERT_NOT_EQUAL(NULL, client ); + esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, &event_group); + TEST_ASSERT_EQUAL(ESP_OK, esp_mqtt_client_start(client)); + TEST_ASSERT_TRUE(xEventGroupWaitBits(event_group, CONNECT_BIT, pdTRUE, pdTRUE, pdMS_TO_TICKS(TEST_CONNECT_TIMEOUT)) & CONNECT_BIT); + esp_mqtt_client_disconnect(client); + TEST_ASSERT_TRUE(xEventGroupWaitBits(event_group, DISCONNECT_BIT, pdTRUE, pdTRUE, pdMS_TO_TICKS(TEST_CONNECT_TIMEOUT)) & DISCONNECT_BIT); + esp_mqtt_client_destroy(client); + eth_test_fixture_deinit(); +} +#endif // SOC_EMAC_SUPPORTED From 4afe71b1321cfade6e9ce7c401b610a4f9f6ce89 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Wed, 6 Oct 2021 17:52:22 +0200 Subject: [PATCH 63/91] MQTT: Add more tests --- components/mqtt/test/test_mqtt.c | 55 ++--- .../mqtt/test/test_mqtt_client_broker.c | 226 ++++++++++++++++++ .../mqtt/test/test_mqtt_client_broker.h | 50 ++++ .../{connection.c => test_mqtt_connection.c} | 62 ++--- components/mqtt/test/test_mqtt_connection.h | 18 ++ 5 files changed, 341 insertions(+), 70 deletions(-) create mode 100644 components/mqtt/test/test_mqtt_client_broker.c create mode 100644 components/mqtt/test/test_mqtt_client_broker.h rename components/mqtt/test/{connection.c => test_mqtt_connection.c} (70%) create mode 100644 components/mqtt/test/test_mqtt_connection.h diff --git a/components/mqtt/test/test_mqtt.c b/components/mqtt/test/test_mqtt.c index 5cc0e47..ede3b31 100644 --- a/components/mqtt/test/test_mqtt.c +++ b/components/mqtt/test/test_mqtt.c @@ -7,7 +7,8 @@ #include "nvs_flash.h" #include "esp_ota_ops.h" #include "sdkconfig.h" -#include "soc/soc_caps.h" +#include "test_mqtt_client_broker.h" +#include "test_mqtt_connection.h" static void test_leak_setup(const char * file, long line) { @@ -74,47 +75,19 @@ TEST_CASE("mqtt enqueue and destroy outbox", "[mqtt][leaks=0]") } #if SOC_EMAC_SUPPORTED - -void eth_test_fixture_connect(void); -void eth_test_fixture_deinit(void); - -static const int CONNECT_BIT = BIT0; -static const int DISCONNECT_BIT = BIT1; - -static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data) +/** + * This test cases uses ethernet kit, so build and use it only if EMAC supported + */ +TEST_CASE("mqtt broker tests", "[mqtt][test_env=UT_T2_Ethernet]") { - EventGroupHandle_t *event_group = handler_args; - switch ((esp_mqtt_event_id_t)event_id) { - case MQTT_EVENT_CONNECTED: - xEventGroupSetBits(*event_group, CONNECT_BIT); - break; - case MQTT_EVENT_DISCONNECTED: - xEventGroupSetBits(*event_group, DISCONNECT_BIT); - break; - default: - break; - } -} - -TEST_CASE("connect disconnect", "[mqtt][test_env=UT_T2_Ethernet]") -{ - test_leak_setup(__FILE__, __LINE__); test_case_uses_tcpip(); - eth_test_fixture_connect(); - const int TEST_CONNECT_TIMEOUT = 10000; - const esp_mqtt_client_config_t mqtt_cfg = { - // no connection takes place, but the uri has to be valid for init() to succeed - .uri = CONFIG_MQTT_TEST_BROKER_URI, - }; - EventGroupHandle_t event_group = xEventGroupCreate(); - esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg); - TEST_ASSERT_NOT_EQUAL(NULL, client ); - esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, &event_group); - TEST_ASSERT_EQUAL(ESP_OK, esp_mqtt_client_start(client)); - TEST_ASSERT_TRUE(xEventGroupWaitBits(event_group, CONNECT_BIT, pdTRUE, pdTRUE, pdMS_TO_TICKS(TEST_CONNECT_TIMEOUT)) & CONNECT_BIT); - esp_mqtt_client_disconnect(client); - TEST_ASSERT_TRUE(xEventGroupWaitBits(event_group, DISCONNECT_BIT, pdTRUE, pdTRUE, pdMS_TO_TICKS(TEST_CONNECT_TIMEOUT)) & DISCONNECT_BIT); - esp_mqtt_client_destroy(client); - eth_test_fixture_deinit(); + connect_test_fixture_setup(); + + RUN_MQTT_BROKER_TEST(mqtt_connect_disconnect); + RUN_MQTT_BROKER_TEST(mqtt_subscribe_publish); + RUN_MQTT_BROKER_TEST(mqtt_lwt_clean_disconnect); + RUN_MQTT_BROKER_TEST(mqtt_subscribe_payload); + + connect_test_fixture_teardown(); } #endif // SOC_EMAC_SUPPORTED diff --git a/components/mqtt/test/test_mqtt_client_broker.c b/components/mqtt/test/test_mqtt_client_broker.c new file mode 100644 index 0000000..167a007 --- /dev/null +++ b/components/mqtt/test/test_mqtt_client_broker.c @@ -0,0 +1,226 @@ +/* + * SPDX-FileCopyrightText: 2021 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ +#include "freertos/FreeRTOS.h" +#include "freertos/event_groups.h" +#include "mqtt_client.h" +#include "esp_log.h" + +#define WAIT_FOR_EVENT(event) \ + TEST_ASSERT_TRUE(xEventGroupWaitBits(s_event_group, event, pdTRUE, pdTRUE, pdMS_TO_TICKS(COMMON_OPERATION_TIMEOUT)) & event); + +#define TEST_ASSERT_TRUE(condition) TEST_ASSERT_TRUE_LINE(condition, __LINE__) +#define TEST_ASSERT_TRUE_LINE(condition, line) \ + do { \ + if (!(condition)) { \ + ESP_LOGE("test_mqtt_client_broker.c", \ + "Assertion failed in line %d", line); \ + return false; \ + } \ + } while(0) + + +static const int COMMON_OPERATION_TIMEOUT = 10000; +static const int CONNECT_BIT = BIT0; +static const int DISCONNECT_BIT = BIT1; +static const int DATA_BIT = BIT2; + +static EventGroupHandle_t s_event_group; + +static char* append_mac(const char* string) +{ + uint8_t mac[6]; + char *id_string = NULL; + esp_read_mac(mac, ESP_MAC_WIFI_STA); + asprintf(&id_string, "%s_%02x%02X%02X", string, mac[3], mac[4], mac[5]); + return id_string; +} + +static void mqtt_data_handler_qos(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data) +{ + if (event_id == MQTT_EVENT_DATA) { + esp_mqtt_event_handle_t event = event_data; + int * qos = handler_args; + *qos = event->qos; + xEventGroupSetBits(s_event_group, DATA_BIT); + } +} + +static void mqtt_data_handler_lwt(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data) +{ + if (event_id == MQTT_EVENT_DATA) { + esp_mqtt_event_handle_t event = event_data; + ESP_LOGI("mqtt-lwt", "MQTT_EVENT_DATA"); + ESP_LOGI("mqtt-lwt", "TOPIC=%.*s", event->topic_len, event->topic); + ESP_LOGI("mqtt-lwt", "DATA=%.*s", event->data_len, event->data); + if (strncmp(event->data, "no-lwt", event->data_len) == 0) { + // no lwt, just to indicate the test has finished + xEventGroupSetBits(s_event_group, DATA_BIT); + } else { + // count up any potential lwt message + int * count = handler_args; + *count = *count + 1; + ESP_LOGE("mqtt-lwt", "count=%d", *count); + } + } +} + +static void mqtt_data_handler_subscribe(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data) +{ + if (event_id == MQTT_EVENT_SUBSCRIBED) { + esp_mqtt_event_handle_t event = event_data; + ESP_LOGI("mqtt-subscribe", "MQTT_EVENT_SUBSCRIBED, data size=%d", event->data_len); + int * sub_payload = handler_args; + if (event->data_len == 1) { + ESP_LOGI("mqtt-subscribe", "DATA=%d", *(uint8_t*)event->data); + *sub_payload = *(uint8_t*)event->data; + } + xEventGroupSetBits(s_event_group, DATA_BIT); + } +} + + +static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data) +{ + switch ((esp_mqtt_event_id_t)event_id) { + case MQTT_EVENT_CONNECTED: + xEventGroupSetBits(s_event_group, CONNECT_BIT); + break; + + case MQTT_EVENT_DISCONNECTED: + xEventGroupSetBits(s_event_group, DISCONNECT_BIT); + break; + default: + break; + } +} + +bool mqtt_connect_disconnect(void) +{ + const esp_mqtt_client_config_t mqtt_cfg = { + .uri = CONFIG_MQTT_TEST_BROKER_URI, + .disable_auto_reconnect = true, + }; + s_event_group = xEventGroupCreate(); + esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg); + TEST_ASSERT_TRUE(NULL != client ); + esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, NULL); + TEST_ASSERT_TRUE(ESP_OK == esp_mqtt_client_start(client)); + WAIT_FOR_EVENT(CONNECT_BIT); + esp_mqtt_client_disconnect(client); + WAIT_FOR_EVENT(DISCONNECT_BIT); + esp_mqtt_client_reconnect(client); + WAIT_FOR_EVENT(CONNECT_BIT); + esp_mqtt_client_destroy(client); + vEventGroupDelete(s_event_group); + return true; +} + +bool mqtt_subscribe_publish(void) +{ + const esp_mqtt_client_config_t mqtt_cfg = { + .uri = CONFIG_MQTT_TEST_BROKER_URI, + }; + char* topic = append_mac("topic"); + TEST_ASSERT_TRUE(NULL != topic); + s_event_group = xEventGroupCreate(); + esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg); + TEST_ASSERT_TRUE(NULL != client ); + esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, NULL); + TEST_ASSERT_TRUE(ESP_OK == esp_mqtt_client_start(client)); + WAIT_FOR_EVENT(CONNECT_BIT); + int qos = -1; + esp_mqtt_client_register_event(client, MQTT_EVENT_DATA, mqtt_data_handler_qos, &qos); + TEST_ASSERT_TRUE(esp_mqtt_client_subscribe(client, topic, 2) != -1); + TEST_ASSERT_TRUE(esp_mqtt_client_publish(client, topic, "message", 0, 2, 0) != -1); + WAIT_FOR_EVENT(DATA_BIT); + TEST_ASSERT_TRUE(qos == 2); + TEST_ASSERT_TRUE(esp_mqtt_client_publish(client, topic, "message", 0, 1, 0) != -1); + WAIT_FOR_EVENT(DATA_BIT); + TEST_ASSERT_TRUE(qos == 1); + esp_mqtt_client_destroy(client); + vEventGroupDelete(s_event_group); + free(topic); + return true; +} + +bool mqtt_lwt_clean_disconnect(void) +{ + char* lwt = append_mac("lwt"); + TEST_ASSERT_TRUE(lwt); + const esp_mqtt_client_config_t mqtt_cfg1 = { + .uri = CONFIG_MQTT_TEST_BROKER_URI, + .set_null_client_id = true, + .lwt_topic = lwt, + .lwt_msg = "lwt_msg" + }; + const esp_mqtt_client_config_t mqtt_cfg2 = { + .uri = CONFIG_MQTT_TEST_BROKER_URI, + .set_null_client_id = true, + .lwt_topic = lwt, + .lwt_msg = "lwt_msg" + }; + s_event_group = xEventGroupCreate(); + + esp_mqtt_client_handle_t client1 = esp_mqtt_client_init(&mqtt_cfg1); + esp_mqtt_client_handle_t client2 = esp_mqtt_client_init(&mqtt_cfg2); + TEST_ASSERT_TRUE(NULL != client1 && NULL != client2 ); + esp_mqtt_client_register_event(client1, ESP_EVENT_ANY_ID, mqtt_event_handler, NULL); + esp_mqtt_client_register_event(client2, ESP_EVENT_ANY_ID, mqtt_event_handler, NULL); + TEST_ASSERT_TRUE(esp_mqtt_client_start(client1) == ESP_OK); + WAIT_FOR_EVENT(CONNECT_BIT); + TEST_ASSERT_TRUE(esp_mqtt_client_start(client2) == ESP_OK); + WAIT_FOR_EVENT(CONNECT_BIT); + int counter = 0; + esp_mqtt_client_register_event(client1, MQTT_EVENT_DATA, mqtt_data_handler_lwt, &counter); + esp_mqtt_client_register_event(client2, MQTT_EVENT_DATA, mqtt_data_handler_lwt, &counter); + TEST_ASSERT_TRUE(esp_mqtt_client_subscribe(client1, lwt, 0) != -1); + TEST_ASSERT_TRUE(esp_mqtt_client_subscribe(client2, lwt, 0) != -1); + esp_mqtt_client_disconnect(client1); + WAIT_FOR_EVENT(DISCONNECT_BIT); + esp_mqtt_client_reconnect(client1); + WAIT_FOR_EVENT(CONNECT_BIT); + TEST_ASSERT_TRUE(esp_mqtt_client_subscribe(client1, lwt, 0) != -1); + esp_mqtt_client_stop(client2); + esp_mqtt_client_start(client2); + WAIT_FOR_EVENT(CONNECT_BIT); + TEST_ASSERT_TRUE(esp_mqtt_client_subscribe(client2, lwt, 0) != -1); + TEST_ASSERT_TRUE(esp_mqtt_client_publish(client1, lwt, "no-lwt", 0, 0, 0) != -1); + WAIT_FOR_EVENT(DATA_BIT); + TEST_ASSERT_TRUE(counter == 0); + esp_mqtt_client_destroy(client1); + esp_mqtt_client_destroy(client2); + vEventGroupDelete(s_event_group); + free(lwt); + return true; +} + +bool mqtt_subscribe_payload(void) +{ + const esp_mqtt_client_config_t mqtt_cfg = { + .uri = CONFIG_MQTT_TEST_BROKER_URI, + .disable_auto_reconnect = true, + }; + char* topic = append_mac("topic"); + TEST_ASSERT_TRUE(NULL != topic); + s_event_group = xEventGroupCreate(); + esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg); + TEST_ASSERT_TRUE(NULL != client ); + esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, NULL); + TEST_ASSERT_TRUE(ESP_OK == esp_mqtt_client_start(client)); + WAIT_FOR_EVENT(CONNECT_BIT); + int qos_payload = -1; + esp_mqtt_client_register_event(client, MQTT_EVENT_SUBSCRIBED, mqtt_data_handler_subscribe, &qos_payload); + TEST_ASSERT_TRUE(esp_mqtt_client_subscribe(client, topic, 2) != -1); + WAIT_FOR_EVENT(DATA_BIT); + TEST_ASSERT_TRUE(qos_payload == 2); + TEST_ASSERT_TRUE(esp_mqtt_client_subscribe(client, topic, 0) != -1); + WAIT_FOR_EVENT(DATA_BIT); + TEST_ASSERT_TRUE(qos_payload == 0); + esp_mqtt_client_destroy(client); + vEventGroupDelete(s_event_group); + free(topic); + return true; +} diff --git a/components/mqtt/test/test_mqtt_client_broker.h b/components/mqtt/test/test_mqtt_client_broker.h new file mode 100644 index 0000000..6bfd5d8 --- /dev/null +++ b/components/mqtt/test/test_mqtt_client_broker.h @@ -0,0 +1,50 @@ +/* + * SPDX-FileCopyrightText: 2021 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ +#pragma once +#include "esp_log.h" + +/** + * @brief MQTT client-broker tests are not implemented as separate test cases + * due to time consuming connection setup/teardown. + * This utility macro is used to run functional cases as MQTT tests + * and evaluate as separate assertions in one "mqtt broker tests" test case. + */ +#define RUN_MQTT_BROKER_TEST(test_name) \ + do { \ + ESP_LOGI("mqtt_test", "Running test:" #test_name "()"); \ + TEST_ASSERT_TRUE_MESSAGE(test_name(), "Mqtt test failed: " #test_name "() "); \ + ESP_LOGI("mqtt_test", "Test:" #test_name "() passed "); \ + } while(0) + + +/** + * @brief This module contains mqtt test cases interacting the client with a (real) broker + */ + +/** + * @brief The client subscribes and publishes on the same topic + * and verifies the received published qos in the event + */ +bool mqtt_subscribe_publish(void); + +/** + * @brief The client connects, disconnects and reconnects. + * Tests basic client state transitions + */ +bool mqtt_connect_disconnect(void); + +/** + * @brief Two clients with defined lwt connect and subscribe to lwt topic. + * This test verifies that no lwt is send when each of the client disconnects. + * (we expect a clean disconnection, so no last-will being sent) + */ +bool mqtt_lwt_clean_disconnect(void); + +/** + * @brief The client subscribes to a topic with certain qos + * and verifies the qos in SUBACK message from the broker. + */ +bool mqtt_subscribe_payload(void); diff --git a/components/mqtt/test/connection.c b/components/mqtt/test/test_mqtt_connection.c similarity index 70% rename from components/mqtt/test/connection.c rename to components/mqtt/test/test_mqtt_connection.c index 213d6b3..bf3f09a 100644 --- a/components/mqtt/test/connection.c +++ b/components/mqtt/test/test_mqtt_connection.c @@ -10,6 +10,7 @@ #include "esp_eth.h" #include "esp_log.h" + #if SOC_EMAC_SUPPORTED #define ETH_START_BIT BIT(0) #define ETH_STOP_BIT BIT(1) @@ -18,7 +19,15 @@ #define ETH_STOP_TIMEOUT_MS (10000) #define ETH_GET_IP_TIMEOUT_MS (60000) + static const char *TAG = "esp32_eth_test_fixture"; +static EventGroupHandle_t s_eth_event_group = NULL; +static esp_netif_t *s_eth_netif = NULL; +static esp_eth_mac_t *s_mac = NULL; +static esp_eth_phy_t *s_phy = NULL; +static esp_eth_handle_t s_eth_handle = NULL; +static esp_eth_netif_glue_handle_t s_eth_glue = NULL; + /** Event handler for Ethernet events */ static void eth_event_handler(void *arg, esp_event_base_t event_base, @@ -78,61 +87,56 @@ static esp_err_t test_uninstall_driver(esp_eth_handle_t eth_hdl, uint32_t ms_to_ return ESP_FAIL; } } -static EventGroupHandle_t eth_event_group; -static esp_netif_t *eth_netif; -static esp_eth_mac_t *mac; -static esp_eth_phy_t *phy; -static esp_eth_handle_t eth_handle = NULL; -static esp_eth_netif_glue_handle_t glue; -void eth_test_fixture_connect(void) + +void connect_test_fixture_setup(void) { EventBits_t bits; - eth_event_group = xEventGroupCreate(); - TEST_ASSERT(eth_event_group != NULL); + s_eth_event_group = xEventGroupCreate(); + TEST_ASSERT(s_eth_event_group != NULL); TEST_ESP_OK(esp_event_loop_create_default()); // create TCP/IP netif esp_netif_config_t netif_cfg = ESP_NETIF_DEFAULT_ETH(); - eth_netif = esp_netif_new(&netif_cfg); + s_eth_netif = esp_netif_new(&netif_cfg); eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG(); - mac = esp_eth_mac_new_esp32(&mac_config); + s_mac = esp_eth_mac_new_esp32(&mac_config); eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG(); - phy = esp_eth_phy_new_ip101(&phy_config); - esp_eth_config_t eth_config = ETH_DEFAULT_CONFIG(mac, phy); + s_phy = esp_eth_phy_new_ip101(&phy_config); + esp_eth_config_t eth_config = ETH_DEFAULT_CONFIG(s_mac, s_phy); // install Ethernet driver - TEST_ESP_OK(esp_eth_driver_install(ð_config, ð_handle)); + TEST_ESP_OK(esp_eth_driver_install(ð_config, &s_eth_handle)); // combine driver with netif - glue = esp_eth_new_netif_glue(eth_handle); - TEST_ESP_OK(esp_netif_attach(eth_netif, glue)); + s_eth_glue = esp_eth_new_netif_glue(s_eth_handle); + TEST_ESP_OK(esp_netif_attach(s_eth_netif, s_eth_glue)); // register user defined event handers - TEST_ESP_OK(esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, ð_event_handler, eth_event_group)); - TEST_ESP_OK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &got_ip_event_handler, eth_event_group)); + TEST_ESP_OK(esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, ð_event_handler, s_eth_event_group)); + TEST_ESP_OK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &got_ip_event_handler, s_eth_event_group)); // start Ethernet driver - TEST_ESP_OK(esp_eth_start(eth_handle)); + TEST_ESP_OK(esp_eth_start(s_eth_handle)); /* wait for IP lease */ - bits = xEventGroupWaitBits(eth_event_group, ETH_GOT_IP_BIT, true, true, pdMS_TO_TICKS(ETH_GET_IP_TIMEOUT_MS)); + bits = xEventGroupWaitBits(s_eth_event_group, ETH_GOT_IP_BIT, true, true, pdMS_TO_TICKS(ETH_GET_IP_TIMEOUT_MS)); TEST_ASSERT((bits & ETH_GOT_IP_BIT) == ETH_GOT_IP_BIT); } -void eth_test_fixture_deinit(void) +void connect_test_fixture_teardown(void) { EventBits_t bits; // stop Ethernet driver - TEST_ESP_OK(esp_eth_stop(eth_handle)); + TEST_ESP_OK(esp_eth_stop(s_eth_handle)); /* wait for connection stop */ - bits = xEventGroupWaitBits(eth_event_group, ETH_STOP_BIT, true, true, pdMS_TO_TICKS(ETH_STOP_TIMEOUT_MS)); + bits = xEventGroupWaitBits(s_eth_event_group, ETH_STOP_BIT, true, true, pdMS_TO_TICKS(ETH_STOP_TIMEOUT_MS)); TEST_ASSERT((bits & ETH_STOP_BIT) == ETH_STOP_BIT); - TEST_ESP_OK(esp_eth_del_netif_glue(glue)); + TEST_ESP_OK(esp_eth_del_netif_glue(s_eth_glue)); /* driver should be uninstalled within 2 seconds */ - TEST_ESP_OK(test_uninstall_driver(eth_handle, 2000)); - TEST_ESP_OK(phy->del(phy)); - TEST_ESP_OK(mac->del(mac)); + TEST_ESP_OK(test_uninstall_driver(s_eth_handle, 2000)); + TEST_ESP_OK(s_phy->del(s_phy)); + TEST_ESP_OK(s_mac->del(s_mac)); TEST_ESP_OK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_ETH_GOT_IP, got_ip_event_handler)); TEST_ESP_OK(esp_event_handler_unregister(ETH_EVENT, ESP_EVENT_ANY_ID, eth_event_handler)); - esp_netif_destroy(eth_netif); + esp_netif_destroy(s_eth_netif); TEST_ESP_OK(esp_event_loop_delete_default()); - vEventGroupDelete(eth_event_group); + vEventGroupDelete(s_eth_event_group); } #endif // SOC_EMAC_SUPPORTED diff --git a/components/mqtt/test/test_mqtt_connection.h b/components/mqtt/test/test_mqtt_connection.h new file mode 100644 index 0000000..5322ddf --- /dev/null +++ b/components/mqtt/test/test_mqtt_connection.h @@ -0,0 +1,18 @@ +/* + * SPDX-FileCopyrightText: 2021 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ +#pragma once +#include "soc/soc_caps.h" + +/** + * Connection test fixture setup, so we expect the broker is available + * on network + */ +void connect_test_fixture_setup(void); + +/** + * Cleans up the connection + */ +void connect_test_fixture_teardown(void); From 1766fd4d5b35ea09d8cbc9bf30eaa80d56594361 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Fri, 26 Nov 2021 13:35:35 +0100 Subject: [PATCH 64/91] MQTT: Fix disconnect/reconnect, Adds empty client id, ... Updated MQTT submodule: git log --oneline 89894bd0c611b1392967fe90bb49682eba858383...b86d42c130ac64a916ce6cf299d99f9756692394 * Added support for client with empty id * Fixed user requested disconnect to correctly send MQTT disconnection message * Fixed reconnection request with disabled autoreconnect * Added qos and dup flags to data events * Added Support for suback massage payload in mqtt events Detailed description of the changes (https://github.com/espressif/esp-mqtt/compare/89894bd0c611b1392967fe90bb49682eba858383...b86d42c130ac64a916ce6cf299d99f9756692394): * Adds the possibility of client with empty id - See merge request esp-mqtt!114 - esp_mqtt commit https://github.com/espressif/esp-mqtt/commit/09287a11565fdd26f5c0deb2960566dff676509b - esp_mqtt commit https://github.com/espressif/esp-mqtt/commit/1fd50dd2cbbe9a7847d8e99d2d802c17122a0be9 - Related IDF-4124 * Client: Disconnect/Reconnect improvements - See merge request esp-mqtt!113 - esp_mqtt commit https://github.com/espressif/esp-mqtt/commit/3f05b1aedc4da048d3a41657339e77dfc0bfe0a8 - esp_mqtt commit https://github.com/espressif/esp-mqtt/commit/86e40f86152a522b179b5dcc1dbbd0b288ee8321 - Related https://github.com/espressif/esp-mqtt/issues/206 - Related https://github.com/espressif/esp-mqtt/issues/208 * Events: Support qos/dup flags and suback payload in mqtt events (GitHub PR) - See merge request esp-mqtt!112 - esp_mqtt commit https://github.com/espressif/esp-mqtt/commit/de47f1c3415d38509d9fe3436dc6d35886556954 - esp_mqtt commit https://github.com/espressif/esp-mqtt/commit/e1d5a9402f08a21e3ca755afbbcd8d99be681902 - Related https://github.com/espressif/esp-mqtt/issues/200 - Related https://github.com/espressif/esp-mqtt/pull/203 --- components/mqtt/esp-mqtt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/mqtt/esp-mqtt b/components/mqtt/esp-mqtt index 89894bd..b86d42c 160000 --- a/components/mqtt/esp-mqtt +++ b/components/mqtt/esp-mqtt @@ -1 +1 @@ -Subproject commit 89894bd0c611b1392967fe90bb49682eba858383 +Subproject commit b86d42c130ac64a916ce6cf299d99f9756692394 From 174c6781de0506e29bf981db3c351537aeb54127 Mon Sep 17 00:00:00 2001 From: Jakob Hasse Date: Thu, 18 Nov 2021 14:27:30 +0800 Subject: [PATCH 65/91] refactor (test_utils)!: separate file for memory check functions Memory check (leaks and heap tracing) functions for unit tests now have a separate file now and are renamed for more consistency. BREAKING CHANGE: renamed memory check function names which may be used in unit tests outside IDF. --- components/mqtt/test/test_mqtt.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/components/mqtt/test/test_mqtt.c b/components/mqtt/test/test_mqtt.c index ede3b31..e103b59 100644 --- a/components/mqtt/test/test_mqtt.c +++ b/components/mqtt/test/test_mqtt.c @@ -1,8 +1,21 @@ +/* + * SPDX-FileCopyrightText: 2021 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Unlicense OR CC0-1.0 + * + * This test code is in the Public Domain (or CC0 licensed, at your option.) + * + * Unless required by applicable law or agreed to in writing, this + * software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. + */ + #include #include "freertos/FreeRTOS.h" #include "freertos/event_groups.h" #include "unity.h" #include "test_utils.h" +#include "memory_checks.h" #include "mqtt_client.h" #include "nvs_flash.h" #include "esp_ota_ops.h" @@ -17,7 +30,7 @@ static void test_leak_setup(const char * file, long line) gettimeofday(&te, NULL); // get current time esp_read_mac(mac, ESP_MAC_WIFI_STA); printf("%s:%ld: time=%ld.%lds, mac:" MACSTR "\n", file, line, te.tv_sec, te.tv_usec, MAC2STR(mac)); - unity_reset_leak_checks(); + test_utils_record_free_mem(); } TEST_CASE("mqtt init with invalid url", "[mqtt][leaks=0]") From 47ac29b1e2945ccd3d5f883f100929727f868d84 Mon Sep 17 00:00:00 2001 From: Mahavir Jain Date: Fri, 31 Dec 2021 13:51:38 +0530 Subject: [PATCH 66/91] mqtt: replace nghttp with http_parser references --- components/mqtt/CMakeLists.txt | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/components/mqtt/CMakeLists.txt b/components/mqtt/CMakeLists.txt index d97d3a6..ac832e1 100644 --- a/components/mqtt/CMakeLists.txt +++ b/components/mqtt/CMakeLists.txt @@ -13,7 +13,7 @@ idf_component_get_property(esp_hw_support_dir esp_hw_support COMPONENT_DIR) idf_component_get_property(esp_event_dir esp_event COMPONENT_DIR) idf_component_get_property(log_dir log COMPONENT_DIR) idf_component_get_property(freertos_dir freertos COMPONENT_DIR) -idf_component_get_property(nghttp_dir nghttp COMPONENT_DIR) +idf_component_get_property(http_parser_dir http_parser COMPONENT_DIR) idf_component_get_property(esp_wifi_dir esp_wifi COMPONENT_DIR) idf_component_get_property(esp_hw_support_dir esp_hw_support COMPONENT_DIR) idf_component_get_property(esp_tls_dir esp-tls COMPONENT_DIR) @@ -61,7 +61,7 @@ idf_component_get_property(mbedtls_dir mbedtls COMPONENT_DIR) ${freertos_dir}/FreeRTOS-Kernel/include/freertos/task.h ${freertos_dir}/FreeRTOS-Kernel/include/freertos/event_groups.h ${log_dir}/include/esp_log.h - ${nghttp_dir}/port/include/http_parser.h + ${http_parser_dir}/http_parser.h ) set(srcs @@ -113,7 +113,7 @@ idf_component_get_property(mbedtls_dir mbedtls COMPONENT_DIR) ${log_dir}/include ${esp_rom_dir}/include ${mbedtls_dir}/port/include - ${nghttp_dir}/port/include + ${http_parser_dir} ${mbedtls_dir}/mbedtls/include ${freertos_dir}/FreeRTOS-Kernel/include/freertos esp-mqtt/lib/include @@ -131,7 +131,7 @@ idf_component_get_property(mbedtls_dir mbedtls COMPONENT_DIR) target_link_libraries(${COMPONENT_LIB} PUBLIC mocks) else() - idf_component_get_property(nghttp_lib nghttp COMPONENT_LIB) + idf_component_get_property(http_parser_lib http_parser COMPONENT_LIB) idf_component_get_property(tcp_transport_lib tcp_transport COMPONENT_LIB) - target_link_libraries(${COMPONENT_LIB} PUBLIC ${nghttp_lib} ${tcp_transport_lib}) + target_link_libraries(${COMPONENT_LIB} PUBLIC ${http_parser_lib} ${tcp_transport_lib}) endif() From f8cb9f7286d3db3e1287f5024c7521411638e8ce Mon Sep 17 00:00:00 2001 From: David Cermak Date: Mon, 17 Jan 2022 10:47:59 +0100 Subject: [PATCH 67/91] mqtt: Add docs on MQTT_CUSTOM_OUTBOX implentation Closes https://github.com/espressif/esp-mqtt/issues/217 --- components/mqtt/Kconfig | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/components/mqtt/Kconfig b/components/mqtt/Kconfig index 9a06e43..fd1d4a6 100644 --- a/components/mqtt/Kconfig +++ b/components/mqtt/Kconfig @@ -137,8 +137,12 @@ menu "ESP-MQTT Configurations" bool "Enable custom outbox implementation" default n help - Set to true if a specific implementation of message outbox is needed (e.g. persistant outbox in NVM or + Set to true if a specific implementation of message outbox is needed (e.g. persistent outbox in NVM or similar). + Note: Implementation of the custom outbox must be added to the mqtt component. These CMake commands + could be used to append the custom implementation to lib-mqtt sources: + idf_component_get_property(mqtt mqtt COMPONENT_LIB) + set_property(TARGET ${mqtt} PROPERTY SOURCES ${PROJECT_DIR}/custom_outbox.c APPEND) config MQTT_OUTBOX_EXPIRED_TIMEOUT_MS int "Outbox message expired timeout[ms]" From 71d92362759fcc224d8d9ee8f9cb316d8eb83dbb Mon Sep 17 00:00:00 2001 From: David Cermak Date: Thu, 20 Jan 2022 09:42:21 +0100 Subject: [PATCH 68/91] mqtt: Fix sending log data; dup flag after queue * Fix sending mqtt message longer than Tx buffer size * Fix enqueue API to send data with correct dup flag * Update submodule: git log --oneline b86d42c130ac64a916ce6cf299d99f9756692394..985078affa8a2d2b56b87c8e6455252850f895c6 Detailed description of the changes: * Isolate IDF env for v4.4 and v5.0(master) - See merge request espressif/esp-mqtt!111 - ci: Isolate IDF env for v4.4 and v5.0(master) (espressif/esp-mqtt@4c5a65c) * Client: Remove usage of legacy FreeRTOS types - See merge request espressif/esp-mqtt!120 - ci: Fix build issues with IDF-4.4 against master (espressif/esp-mqtt@c28a56d) - See commit https://github.com/espressif/esp-mqtt/commit/6ef98d6 * mqtt_client: Fix mqtt send long data error - See merge request espressif/esp-mqtt!117 - Closes https://github.com/espressif/esp-mqtt/issues/214 - See commit https://github.com/espressif/esp-mqtt/commit/372b323 * Client: Fix use esp_mqtt_client_enqueue API to send data, data dup flag will be set 1 - See merge request espressif/esp-mqtt!116 - See commit https://github.com/espressif/esp-mqtt/commit/df8dc92 --- components/mqtt/esp-mqtt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/mqtt/esp-mqtt b/components/mqtt/esp-mqtt index b86d42c..985078a 160000 --- a/components/mqtt/esp-mqtt +++ b/components/mqtt/esp-mqtt @@ -1 +1 @@ -Subproject commit b86d42c130ac64a916ce6cf299d99f9756692394 +Subproject commit 985078affa8a2d2b56b87c8e6455252850f895c6 From 23e5c5a02c6d63d3010209b0eecd61ed403f7dab Mon Sep 17 00:00:00 2001 From: David Cermak Date: Mon, 24 Jan 2022 15:40:11 +0100 Subject: [PATCH 69/91] esp_eth: Update esp32's EMAC API to decouple driver and vendor config --- components/mqtt/test/test_mqtt_connection.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/components/mqtt/test/test_mqtt_connection.c b/components/mqtt/test/test_mqtt_connection.c index bf3f09a..6598b98 100644 --- a/components/mqtt/test/test_mqtt_connection.c +++ b/components/mqtt/test/test_mqtt_connection.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2021 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -100,7 +100,8 @@ void connect_test_fixture_setup(void) s_eth_netif = esp_netif_new(&netif_cfg); eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG(); - s_mac = esp_eth_mac_new_esp32(&mac_config); + eth_esp32_emac_config_t esp32_emac_config = ETH_ESP32_EMAC_DEFAULT_CONFIG(); + s_mac = esp_eth_mac_new_esp32(&esp32_emac_config, &mac_config); eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG(); s_phy = esp_eth_phy_new_ip101(&phy_config); esp_eth_config_t eth_config = ETH_DEFAULT_CONFIG(s_mac, s_phy); From fcd548daabf772e2eaf1472fb1b5243c1061fcf7 Mon Sep 17 00:00:00 2001 From: Darian Leung Date: Thu, 17 Feb 2022 16:40:52 +0800 Subject: [PATCH 70/91] freertos: Remove legacy hooks This commit refactors the legacy hooks as follows: - Removed CONFIG_FREERTOS_LEGACY_HOOKS - FreeRTOS hooks are now enabled via: - CONFIG_FREERTOS_USE_IDLE_HOOK - CONFIG_FREERTOS_USE_TICK_HOOK - Update IDF hooks documentation --- .../mqtt/host_test/mocks/include/freertos/portmacro.h | 8 -------- 1 file changed, 8 deletions(-) diff --git a/components/mqtt/host_test/mocks/include/freertos/portmacro.h b/components/mqtt/host_test/mocks/include/freertos/portmacro.h index a68d3c4..23c3d6e 100644 --- a/components/mqtt/host_test/mocks/include/freertos/portmacro.h +++ b/components/mqtt/host_test/mocks/include/freertos/portmacro.h @@ -156,14 +156,6 @@ extern void vPortClearInterruptMask(int); extern void vPortEnterCritical(void); extern void vPortExitCritical(void); -extern void esp_vApplicationIdleHook(void); -extern void esp_vApplicationTickHook(void); - -#ifndef CONFIG_FREERTOS_LEGACY_HOOKS -#define vApplicationIdleHook esp_vApplicationIdleHook -#define vApplicationTickHook esp_vApplicationTickHook -#endif /* !CONFIG_FREERTOS_LEGACY_HOOKS */ - /* Task function macros as described on the FreeRTOS.org WEB site. */ #define portTASK_FUNCTION_PROTO(vFunction, pvParameters) void vFunction(void* pvParameters) #define portTASK_FUNCTION(vFunction, pvParameters) void vFunction(void* pvParameters) From a840bcf2743e1e8bed7dc0fce30758421d96f767 Mon Sep 17 00:00:00 2001 From: Sudeep Mohanty Date: Wed, 12 Jan 2022 12:23:47 +0530 Subject: [PATCH 71/91] esp_hw_support/esp_system: Re-evaluate header inclusions and include directories This commit updates the visibility of various header files and cleans up some unnecessary inclusions. Also, this commit removes certain header include paths which were maintained for backward compatibility. --- components/mqtt/esp-mqtt | 2 +- components/mqtt/test/test_mqtt.c | 3 ++- components/mqtt/test/test_mqtt_client_broker.c | 3 ++- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/components/mqtt/esp-mqtt b/components/mqtt/esp-mqtt index 985078a..0eda78e 160000 --- a/components/mqtt/esp-mqtt +++ b/components/mqtt/esp-mqtt @@ -1 +1 @@ -Subproject commit 985078affa8a2d2b56b87c8e6455252850f895c6 +Subproject commit 0eda78e9b7cf008cd91aeccd734e2db195c6dd22 diff --git a/components/mqtt/test/test_mqtt.c b/components/mqtt/test/test_mqtt.c index e103b59..f2c27b0 100644 --- a/components/mqtt/test/test_mqtt.c +++ b/components/mqtt/test/test_mqtt.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2021 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Unlicense OR CC0-1.0 * @@ -22,6 +22,7 @@ #include "sdkconfig.h" #include "test_mqtt_client_broker.h" #include "test_mqtt_connection.h" +#include "esp_mac.h" static void test_leak_setup(const char * file, long line) { diff --git a/components/mqtt/test/test_mqtt_client_broker.c b/components/mqtt/test/test_mqtt_client_broker.c index 167a007..cb0f3ae 100644 --- a/components/mqtt/test/test_mqtt_client_broker.c +++ b/components/mqtt/test/test_mqtt_client_broker.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2021 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -7,6 +7,7 @@ #include "freertos/event_groups.h" #include "mqtt_client.h" #include "esp_log.h" +#include "esp_mac.h" #define WAIT_FOR_EVENT(event) \ TEST_ASSERT_TRUE(xEventGroupWaitBits(s_event_group, event, pdTRUE, pdTRUE, pdMS_TO_TICKS(COMMON_OPERATION_TIMEOUT)) & event); From 933da9efed09e26cb97bb3e7efe86c4ef31bc891 Mon Sep 17 00:00:00 2001 From: Sagar Bijwe Date: Thu, 2 Dec 2021 15:49:28 +0530 Subject: [PATCH 72/91] Remove legacy system event framework. --- components/mqtt/test/test_mqtt_connection.c | 1 + 1 file changed, 1 insertion(+) diff --git a/components/mqtt/test/test_mqtt_connection.c b/components/mqtt/test/test_mqtt_connection.c index 6598b98..0e82003 100644 --- a/components/mqtt/test/test_mqtt_connection.c +++ b/components/mqtt/test/test_mqtt_connection.c @@ -7,6 +7,7 @@ #include "freertos/event_groups.h" #include "unity.h" #include "esp_event.h" +#include "esp_netif.h" #include "esp_eth.h" #include "esp_log.h" From 8dc8e5ae0b34e5ae19f9e785e887f2681bbb37ef Mon Sep 17 00:00:00 2001 From: Laukik Hase Date: Thu, 10 Mar 2022 14:26:37 +0530 Subject: [PATCH 73/91] kconfig: Changed default values of bool configs - Some bool configs were using default values true and false, instead of y and n. --- components/mqtt/Kconfig | 1 - 1 file changed, 1 deletion(-) diff --git a/components/mqtt/Kconfig b/components/mqtt/Kconfig index fd1d4a6..06f06a1 100644 --- a/components/mqtt/Kconfig +++ b/components/mqtt/Kconfig @@ -120,7 +120,6 @@ menu "ESP-MQTT Configurations" config MQTT_TASK_CORE_SELECTION_ENABLED bool "Enable MQTT task core selection" - default false help This will enable core selection From a88eaebad98ebe1e19e310b32eec43e634a3610f Mon Sep 17 00:00:00 2001 From: Anton Maklakov Date: Fri, 4 Mar 2022 11:29:47 +0700 Subject: [PATCH 74/91] esp-mqtt: fix warnings --- components/mqtt/esp-mqtt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/mqtt/esp-mqtt b/components/mqtt/esp-mqtt index 0eda78e..4874bab 160000 --- a/components/mqtt/esp-mqtt +++ b/components/mqtt/esp-mqtt @@ -1 +1 @@ -Subproject commit 0eda78e9b7cf008cd91aeccd734e2db195c6dd22 +Subproject commit 4874bab35659bd2301e65fd849f6559d7380d4f1 From 77e41479d700659e15a5eae38b3e0443875c2414 Mon Sep 17 00:00:00 2001 From: Anton Maklakov Date: Tue, 22 Feb 2022 12:42:56 +0700 Subject: [PATCH 75/91] components: correct printf() placeholder for time_t Using C99 %jd, https://en.cppreference.com/w/c/chrono/time_t --- components/mqtt/test/test_mqtt.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/mqtt/test/test_mqtt.c b/components/mqtt/test/test_mqtt.c index f2c27b0..7a0f938 100644 --- a/components/mqtt/test/test_mqtt.c +++ b/components/mqtt/test/test_mqtt.c @@ -30,7 +30,7 @@ static void test_leak_setup(const char * file, long line) struct timeval te; gettimeofday(&te, NULL); // get current time esp_read_mac(mac, ESP_MAC_WIFI_STA); - printf("%s:%ld: time=%ld.%lds, mac:" MACSTR "\n", file, line, te.tv_sec, te.tv_usec, MAC2STR(mac)); + printf("%s:%ld: time=%jd.%lds, mac:" MACSTR "\n", file, line, (intmax_t)te.tv_sec, te.tv_usec, MAC2STR(mac)); test_utils_record_free_mem(); } From 0a507f12056982c9ffb226616c8d26ca19b3ce6b Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Thu, 14 Apr 2022 20:03:56 +0200 Subject: [PATCH 76/91] build system: remove lwip from common requirements lwip was added to common requirements list to provide "sys/socket.h" header to all components without additional requirements specified. However, lwip pulls in a lot of dependencies on other components. This commit removes lwip from common requirements to reduce the number of components in G1-only apps. To compensate for this removal, the following changes are made: - newlib (which is a common requirement) has a public dependency on lwip if lwip is present in the build. This ensures that sys/socket.h is available as long as lwip component is included into the build. - lwip is now a public requirement of esp-tls since esp_tls.h includes sys/socket.h header. - lwip is now a public requirement o esp_http_client because sys/socket.h is included from esp_http_client.h - lwip is now a private requirement of esp_wifi for "smartconfig_ack" - lwip is now a private requirement of mqtt for socket functions - lwip is now a public requirement of tcp_transport because esp_transport_tcp.h includes sys/socket.h header. - mbedtls checks if lwip component is present in the build. If yes, net_sockets.c is added to the build, along with the dependency on lwip. Previously lwip was a public requirement of mbedtls unconditionally. system/g1_components test app is updated to reflect the changes Default public dependencies of a component before and after this change, except common requirements: - esp_timer (public dependency of freertos) - bootloader_support (public dependency of esp_hw_support) - vfs (public dependency of lwip) - esp_wifi (public dependency of lwip) - esp_event (public dependency of esp_wifi) - esp_netif (public dependency of esp_event) - esp_eth (public dependency of esp_netif) - esp_phy (public dependency of esp_wifi) After: - esp_timer (public dependency of freertos) - bootloader_support (public dependency of esp_hw_support) Altogether, the following components have been always added as public requirements to all other components, and are not added now ([breaking-change]): - lwip - vfs - esp_wifi - esp_event - esp_netif - esp_eth - esp_phy Application components now need to explicitly declare dependencies on these components. --- components/mqtt/CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/components/mqtt/CMakeLists.txt b/components/mqtt/CMakeLists.txt index ac832e1..004389a 100644 --- a/components/mqtt/CMakeLists.txt +++ b/components/mqtt/CMakeLists.txt @@ -4,6 +4,7 @@ idf_component_register(SRCS "esp-mqtt/mqtt_client.c" "esp-mqtt/lib/platform_esp32_idf.c" INCLUDE_DIRS esp-mqtt/include PRIV_INCLUDE_DIRS "esp-mqtt/lib/include" + PRIV_REQUIRES lwip ) if(TEST_BUILD) From 5539c83c0a734ea161bb1957bcf224ea4530456b Mon Sep 17 00:00:00 2001 From: David Cermak Date: Fri, 13 May 2022 10:48:28 +0200 Subject: [PATCH 77/91] mqtt: Fix incorrect reads on error; Update ping processing * Update submodule: git log --oneline 4874bab35659bd2301e65fd849f6559d7380d4f1..64f88b4412ea6649dbf207a07370c2617160d044 Detailed description of the changes: * Fix documentation of config struct - See merge request espressif/esp-mqtt!129 - See commit https://github.com/espressif/esp-mqtt/commit/e31834c * Changes the moment we update keepalive_tick. - See merge request espressif/esp-mqtt!127 - See commit https://github.com/espressif/esp-mqtt/commit/2c2e6f3 * MQTT: Fix signature matching for some integer values - See merge request espressif/esp-mqtt!128 - Closes https://github.com/espressif/esp-idf/issues/8482 - MQTT: Fix signature matching for integer values (espressif/esp-mqtt@6b794e4) * Make the mqtt submodule logging tags lower case - See merge request espressif/esp-mqtt!122 - See commit https://github.com/espressif/esp-mqtt/commit/fb3184c Closes https://github.com/espressif/esp-idf/issues/8482 Closes https://github.com/espressif/esp-idf/issues/8550 --- components/mqtt/esp-mqtt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/mqtt/esp-mqtt b/components/mqtt/esp-mqtt index 4874bab..64f88b4 160000 --- a/components/mqtt/esp-mqtt +++ b/components/mqtt/esp-mqtt @@ -1 +1 @@ -Subproject commit 4874bab35659bd2301e65fd849f6559d7380d4f1 +Subproject commit 64f88b4412ea6649dbf207a07370c2617160d044 From b80d4d468ca3d507265863786542b1311422d7e8 Mon Sep 17 00:00:00 2001 From: Djordje Nedic Date: Fri, 27 May 2022 10:10:51 +0200 Subject: [PATCH 78/91] tools: Increase the minimal supported CMake version to 3.16 This updates the minimal supported version of CMake to 3.16, which in turn enables us to use more CMake features and have a cleaner build system. This is the version that provides most new features and also the one we use in our latest docker image for CI. --- components/mqtt/host_test/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/mqtt/host_test/CMakeLists.txt b/components/mqtt/host_test/CMakeLists.txt index 312ad1e..947809f 100644 --- a/components/mqtt/host_test/CMakeLists.txt +++ b/components/mqtt/host_test/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.5) +cmake_minimum_required(VERSION 3.16) include($ENV{IDF_PATH}/tools/cmake/project.cmake) set(COMPONENTS main) From 62c5545f2d27105e9803a822c05394a94cda6afd Mon Sep 17 00:00:00 2001 From: David Cermak Date: Mon, 2 May 2022 15:47:05 +0200 Subject: [PATCH 79/91] esp-netif: Make dependency on esp-eth optional * esp-netif to optionally depend on esp-eth (only for l2tap config) * esp_eth.h now includes the original ethernet header and the ethernet-netif glue layer * Updated examples and test to explicitely use esp-eth dependency if needed --- components/mqtt/test/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/mqtt/test/CMakeLists.txt b/components/mqtt/test/CMakeLists.txt index a86bbd0..a5b7606 100644 --- a/components/mqtt/test/CMakeLists.txt +++ b/components/mqtt/test/CMakeLists.txt @@ -1,2 +1,2 @@ idf_component_register(SRC_DIRS "." - PRIV_REQUIRES cmock test_utils mqtt nvs_flash app_update) + PRIV_REQUIRES cmock test_utils mqtt nvs_flash app_update esp_eth) From 610db7b9348ee35001fb9ce6ead178f4c21cd9ee Mon Sep 17 00:00:00 2001 From: David Cermak Date: Fri, 13 May 2022 16:55:22 +0200 Subject: [PATCH 80/91] mqtt: Fix and add mqtt host test to CI --- components/mqtt/CMakeLists.txt | 21 ++++--------------- components/mqtt/host_test/CMakeLists.txt | 2 ++ .../mqtt/host_test/main/test_mqtt_client.cpp | 15 ++++++------- components/mqtt/host_test/mocks/config.yaml | 2 ++ .../mocks/include/local_FreeRTOS_config.h | 6 ++++++ 5 files changed, 20 insertions(+), 26 deletions(-) create mode 100644 components/mqtt/host_test/mocks/include/local_FreeRTOS_config.h diff --git a/components/mqtt/CMakeLists.txt b/components/mqtt/CMakeLists.txt index 004389a..ae35aef 100644 --- a/components/mqtt/CMakeLists.txt +++ b/components/mqtt/CMakeLists.txt @@ -3,9 +3,7 @@ idf_component_register(SRCS "esp-mqtt/mqtt_client.c" "esp-mqtt/lib/mqtt_outbox.c" "esp-mqtt/lib/platform_esp32_idf.c" INCLUDE_DIRS esp-mqtt/include - PRIV_INCLUDE_DIRS "esp-mqtt/lib/include" - PRIV_REQUIRES lwip - ) + PRIV_INCLUDE_DIRS "esp-mqtt/lib/include") if(TEST_BUILD) message(STATUS "building MOCKS") @@ -13,10 +11,9 @@ idf_component_get_property(tcp_transport_dir tcp_transport COMPONENT_DIR) idf_component_get_property(esp_hw_support_dir esp_hw_support COMPONENT_DIR) idf_component_get_property(esp_event_dir esp_event COMPONENT_DIR) idf_component_get_property(log_dir log COMPONENT_DIR) -idf_component_get_property(freertos_dir freertos COMPONENT_DIR) +idf_component_get_property(freertos_dir freertos COMPONENT_OVERRIDEN_DIR) idf_component_get_property(http_parser_dir http_parser COMPONENT_DIR) idf_component_get_property(esp_wifi_dir esp_wifi COMPONENT_DIR) -idf_component_get_property(esp_hw_support_dir esp_hw_support COMPONENT_DIR) idf_component_get_property(esp_tls_dir esp-tls COMPONENT_DIR) idf_component_get_property(esp_netif_dir esp_netif COMPONENT_DIR) idf_component_get_property(esp_common_dir esp_common COMPONENT_DIR) @@ -56,10 +53,6 @@ idf_component_get_property(mbedtls_dir mbedtls COMPONENT_DIR) ${esp_event_dir}/include/esp_event.h ${esp_hw_support_dir}/include/esp_mac.h ${esp_hw_support_dir}/include/esp_random.h - ${esp_system_dir}/include/esp_system.h - ${esp_tls_dir}/esp_tls.h - ${freertos_dir}/FreeRTOS-Kernel/include/freertos/queue.h - ${freertos_dir}/FreeRTOS-Kernel/include/freertos/task.h ${freertos_dir}/FreeRTOS-Kernel/include/freertos/event_groups.h ${log_dir}/include/esp_log.h ${http_parser_dir}/http_parser.h @@ -74,13 +67,9 @@ idf_component_get_property(mbedtls_dir mbedtls COMPONENT_DIR) ${MOCK_GEN_DIR}/Mockesp_event.c ${MOCK_GEN_DIR}/Mockesp_mac.c ${MOCK_GEN_DIR}/Mockesp_random.c - ${MOCK_GEN_DIR}/Mockesp_system.c - ${MOCK_GEN_DIR}/Mockesp_tls.c ${MOCK_GEN_DIR}/Mockesp_log.c ${MOCK_GEN_DIR}/Mockhttp_parser.c ${MOCK_GEN_DIR}/Mockevent_groups.c - ${MOCK_GEN_DIR}/Mockqueue.c - ${MOCK_GEN_DIR}/Mocktask.c ) add_custom_command( @@ -122,10 +111,6 @@ idf_component_get_property(mbedtls_dir mbedtls COMPONENT_DIR) ) target_link_libraries(mocks PUBLIC ${cmock_lib}) target_compile_definitions(mocks PUBLIC - CONFIG_LOG_MAXIMUM_LEVEL=5 - CONFIG_LOG_DEFAULT_LEVEL=3 - CONFIG_ESP_TLS_USING_MBEDTLS - CONFIG_ESP_TLS_SERVER CONFIG_LOG_TIMESTAMP_SOURCE_RTOS) target_link_options(${COMPONENT_LIB} INTERFACE -fsanitize=address) @@ -134,5 +119,7 @@ idf_component_get_property(mbedtls_dir mbedtls COMPONENT_DIR) else() idf_component_get_property(http_parser_lib http_parser COMPONENT_LIB) idf_component_get_property(tcp_transport_lib tcp_transport COMPONENT_LIB) + idf_component_get_property(lwip_lib lwip COMPONENT_LIB) target_link_libraries(${COMPONENT_LIB} PUBLIC ${http_parser_lib} ${tcp_transport_lib}) + target_link_libraries(${COMPONENT_LIB} PRIVATE ${lwip_lib}) endif() diff --git a/components/mqtt/host_test/CMakeLists.txt b/components/mqtt/host_test/CMakeLists.txt index 947809f..fedde21 100644 --- a/components/mqtt/host_test/CMakeLists.txt +++ b/components/mqtt/host_test/CMakeLists.txt @@ -2,5 +2,7 @@ cmake_minimum_required(VERSION 3.16) include($ENV{IDF_PATH}/tools/cmake/project.cmake) set(COMPONENTS main) +list(APPEND EXTRA_COMPONENT_DIRS "$ENV{IDF_PATH}/tools/mocks/freertos/") + option(TEST_BUILD "" ON) project(host_mqtt_client_test) diff --git a/components/mqtt/host_test/main/test_mqtt_client.cpp b/components/mqtt/host_test/main/test_mqtt_client.cpp index 51ef9cf..58dd134 100644 --- a/components/mqtt/host_test/main/test_mqtt_client.cpp +++ b/components/mqtt/host_test/main/test_mqtt_client.cpp @@ -1,11 +1,9 @@ #define CATCH_CONFIG_MAIN // This tells the catch header to generate a main #include "catch.hpp" -#include "mqtt_client.h" extern "C" { #include "Mockesp_event.h" #include "Mockesp_log.h" -#include "Mockesp_system.h" #include "Mockesp_mac.h" #include "Mockesp_transport.h" #include "Mockesp_transport_ssl.h" @@ -20,17 +18,14 @@ extern "C" { * The following functions are not directly called but the generation of them * from cmock is broken, so we need to define them here. */ - BaseType_t xQueueTakeMutexRecursive(QueueHandle_t xMutex, - TickType_t xTicksToWait) + esp_err_t esp_tls_get_and_clear_last_error(esp_tls_error_handle_t h, int *esp_tls_code, int *esp_tls_flags) { - return 0; - } - BaseType_t xQueueGiveMutexRecursive(QueueHandle_t xMutex) - { - return 0; + return ESP_OK; } } +#include "mqtt_client.h" + struct ClientInitializedFixture { esp_mqtt_client_handle_t client; ClientInitializedFixture() @@ -42,6 +37,8 @@ struct ClientInitializedFixture { int event_group; uint8_t mac[] = {0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55}; esp_log_write_Ignore(); + xQueueTakeMutexRecursive_CMockIgnoreAndReturn(0, true); + xQueueGiveMutexRecursive_CMockIgnoreAndReturn(0, true); xQueueCreateMutex_ExpectAnyArgsAndReturn( reinterpret_cast(&mtx)); xEventGroupCreate_IgnoreAndReturn(reinterpret_cast(&event_group)); diff --git a/components/mqtt/host_test/mocks/config.yaml b/components/mqtt/host_test/mocks/config.yaml index 8fc80e9..cdaab2a 100644 --- a/components/mqtt/host_test/mocks/config.yaml +++ b/components/mqtt/host_test/mocks/config.yaml @@ -7,6 +7,8 @@ - array - callback :includes_h_pre_orig_header: + - local_FreeRTOS_config.h + - esp_attr.h - FreeRTOS.h - net/if.h :strippables: diff --git a/components/mqtt/host_test/mocks/include/local_FreeRTOS_config.h b/components/mqtt/host_test/mocks/include/local_FreeRTOS_config.h new file mode 100644 index 0000000..e7e1014 --- /dev/null +++ b/components/mqtt/host_test/mocks/include/local_FreeRTOS_config.h @@ -0,0 +1,6 @@ +/* + * SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ +#define configUSE_TRACE_FACILITY 1 From d372bbf09e10601ba3b440dea3bccb46bfb660f9 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Wed, 8 Jun 2022 20:07:04 +0200 Subject: [PATCH 81/91] mqtt: Fix client_enqueue(len=0), Improve transport memory * Update submodule: git log --oneline 64f88b4412ea6649dbf207a07370c2617160d044..a21c387d6280260894981c22494017c893d505b9 Detailed description of the changes: * mqtt_client: Added checks for cleanly-closed connection and timeout - See merge request espressif/esp-mqtt!118 - Added checks for cleanly-closed connection and timeout (espressif/esp-mqtt@e05d873) * mqtt_client: fix esp_mqtt_client_enqueue for len=0 (GitHub PR) - See merge request espressif/esp-mqtt!135 - mqtt_client: fix esp_mqtt_client_enqueue for len=0 (espressif/esp-mqtt@69b6493) * Fix implicit malloc/free inclusion - See merge request espressif/esp-mqtt!134 - See commit https://github.com/espressif/esp-mqtt/commit/9299f54 * feat(mqtt): Optimize mqtt transport list and remove unused transport - See merge request espressif/esp-mqtt!131 - See commit https://github.com/espressif/esp-mqtt/commit/647e0ef * Fix WSS default port selection through menuconfig. - See merge request espressif/esp-mqtt!132 - - Closes https://github.com/espressif/esp-mqtt/issues/223 - See commit https://github.com/espressif/esp-mqtt/commit/f6caaff --- components/mqtt/esp-mqtt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/mqtt/esp-mqtt b/components/mqtt/esp-mqtt index 64f88b4..a21c387 160000 --- a/components/mqtt/esp-mqtt +++ b/components/mqtt/esp-mqtt @@ -1 +1 @@ -Subproject commit 64f88b4412ea6649dbf207a07370c2617160d044 +Subproject commit a21c387d6280260894981c22494017c893d505b9 From bc30b9a8e33885d354994c32d7d3abcfb6cb91c9 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Thu, 9 Jun 2022 11:08:45 +0200 Subject: [PATCH 82/91] mqtt: Update tests to start with valid transport --- .../mqtt/host_test/main/test_mqtt_client.cpp | 20 +++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/components/mqtt/host_test/main/test_mqtt_client.cpp b/components/mqtt/host_test/main/test_mqtt_client.cpp index 58dd134..a28cbc2 100644 --- a/components/mqtt/host_test/main/test_mqtt_client.cpp +++ b/components/mqtt/host_test/main/test_mqtt_client.cpp @@ -69,7 +69,11 @@ struct ClientInitializedFixture { }; TEST_CASE_METHOD(ClientInitializedFixture, "Client set uri") { - struct http_parser_url ret_uri; + struct http_parser_url ret_uri = { + .field_set = 1, + .port = 0, + .field_data = { { 0, 1} } + }; SECTION("User set a correct URI") { http_parser_parse_url_StopIgnore(); http_parser_parse_url_ExpectAnyArgsAndReturn(0); @@ -88,8 +92,20 @@ TEST_CASE_METHOD(ClientInitializedFixture, "Client set uri") TEST_CASE_METHOD(ClientInitializedFixture, "Client Start") { SECTION("Successful start") { + esp_mqtt_client_config_t config{}; + config.uri = "mqtt://1.1.1.1"; + struct http_parser_url ret_uri = { + .field_set = 1 | (1<<1), + .port = 0, + .field_data = { { 0, 4 } /*mqtt*/, { 7, 1 } } // at least *scheme* and *host* + }; + http_parser_parse_url_StopIgnore(); + http_parser_parse_url_ExpectAnyArgsAndReturn(0); + http_parser_parse_url_ReturnThruPtr_u(&ret_uri); xTaskCreatePinnedToCore_ExpectAnyArgsAndReturn(pdTRUE); - auto res = esp_mqtt_client_start(client); + auto res = esp_mqtt_set_config(client, &config); + REQUIRE(res == ESP_OK); + res = esp_mqtt_client_start(client); REQUIRE(res == ESP_OK); } SECTION("Failed on initialization") { From 411ea65ec7c8669fd4ea8f60979d2c5d6e4fd7a7 Mon Sep 17 00:00:00 2001 From: Euripedes Rocha Date: Tue, 7 Jun 2022 08:10:38 -0300 Subject: [PATCH 83/91] [MQTT] - Adds esp-timer as dependency and change version. - Current time is now from esp_timer. --- components/mqtt/CMakeLists.txt | 4 +++- components/mqtt/esp-mqtt | 2 +- components/mqtt/host_test/CMakeLists.txt | 4 +++- components/mqtt/host_test/main/CMakeLists.txt | 2 +- components/mqtt/host_test/main/test_mqtt_client.cpp | 8 +++++--- 5 files changed, 13 insertions(+), 7 deletions(-) diff --git a/components/mqtt/CMakeLists.txt b/components/mqtt/CMakeLists.txt index ae35aef..2e57327 100644 --- a/components/mqtt/CMakeLists.txt +++ b/components/mqtt/CMakeLists.txt @@ -3,7 +3,9 @@ idf_component_register(SRCS "esp-mqtt/mqtt_client.c" "esp-mqtt/lib/mqtt_outbox.c" "esp-mqtt/lib/platform_esp32_idf.c" INCLUDE_DIRS esp-mqtt/include - PRIV_INCLUDE_DIRS "esp-mqtt/lib/include") + PRIV_INCLUDE_DIRS "esp-mqtt/lib/include" + PRIV_REQUIRES esp_timer + ) if(TEST_BUILD) message(STATUS "building MOCKS") diff --git a/components/mqtt/esp-mqtt b/components/mqtt/esp-mqtt index a21c387..89e5c60 160000 --- a/components/mqtt/esp-mqtt +++ b/components/mqtt/esp-mqtt @@ -1 +1 @@ -Subproject commit a21c387d6280260894981c22494017c893d505b9 +Subproject commit 89e5c6014f8dbcfcd98af35fb507ca7b96ac8aee diff --git a/components/mqtt/host_test/CMakeLists.txt b/components/mqtt/host_test/CMakeLists.txt index fedde21..7137910 100644 --- a/components/mqtt/host_test/CMakeLists.txt +++ b/components/mqtt/host_test/CMakeLists.txt @@ -2,7 +2,9 @@ cmake_minimum_required(VERSION 3.16) include($ENV{IDF_PATH}/tools/cmake/project.cmake) set(COMPONENTS main) -list(APPEND EXTRA_COMPONENT_DIRS "$ENV{IDF_PATH}/tools/mocks/freertos/") +list(APPEND EXTRA_COMPONENT_DIRS "$ENV{IDF_PATH}/tools/mocks/freertos/" + "$ENV{IDF_PATH}/tools/mocks/esp_timer/" + ) option(TEST_BUILD "" ON) project(host_mqtt_client_test) diff --git a/components/mqtt/host_test/main/CMakeLists.txt b/components/mqtt/host_test/main/CMakeLists.txt index c89e9bf..90692f6 100644 --- a/components/mqtt/host_test/main/CMakeLists.txt +++ b/components/mqtt/host_test/main/CMakeLists.txt @@ -1,3 +1,3 @@ idf_component_register(SRCS "test_mqtt_client.cpp" INCLUDE_DIRS "$ENV{IDF_PATH}/tools/catch" - REQUIRES cmock mqtt) + REQUIRES cmock mqtt esp_timer) diff --git a/components/mqtt/host_test/main/test_mqtt_client.cpp b/components/mqtt/host_test/main/test_mqtt_client.cpp index a28cbc2..e0c0160 100644 --- a/components/mqtt/host_test/main/test_mqtt_client.cpp +++ b/components/mqtt/host_test/main/test_mqtt_client.cpp @@ -13,6 +13,7 @@ extern "C" { #include "Mockhttp_parser.h" #include "Mockqueue.h" #include "Mocktask.h" +#include "Mockesp_timer.h" /* * The following functions are not directly called but the generation of them @@ -30,15 +31,16 @@ struct ClientInitializedFixture { esp_mqtt_client_handle_t client; ClientInitializedFixture() { - TEST_PROTECT(); + [[maybe_unused]] auto protect = TEST_PROTECT(); int mtx; int transport_list; int transport; int event_group; uint8_t mac[] = {0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55}; esp_log_write_Ignore(); - xQueueTakeMutexRecursive_CMockIgnoreAndReturn(0, true); - xQueueGiveMutexRecursive_CMockIgnoreAndReturn(0, true); + esp_timer_get_time_IgnoreAndReturn(0); + xQueueTakeMutexRecursive_IgnoreAndReturn(true); + xQueueGiveMutexRecursive_IgnoreAndReturn(true); xQueueCreateMutex_ExpectAnyArgsAndReturn( reinterpret_cast(&mtx)); xEventGroupCreate_IgnoreAndReturn(reinterpret_cast(&event_group)); From d4ee31f627341069a634704b8e34ab9a691c93d8 Mon Sep 17 00:00:00 2001 From: Fu Hanxi Date: Wed, 13 Jul 2022 10:41:36 +0800 Subject: [PATCH 84/91] docs: changes docs supported targets tables --- components/mqtt/host_test/README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/components/mqtt/host_test/README.md b/components/mqtt/host_test/README.md index 071cd94..a62087a 100644 --- a/components/mqtt/host_test/README.md +++ b/components/mqtt/host_test/README.md @@ -1,3 +1,6 @@ +| Supported Targets | Linux | +| ----------------- | ----- | + # Description This directory contains test code for the mqtt client that runs on host. From 1e598b071bea9fee9db479675e02c381f0c92536 Mon Sep 17 00:00:00 2001 From: Euripedes Rocha Date: Mon, 13 Jun 2022 07:59:11 -0300 Subject: [PATCH 85/91] [MQTT] - Updates esp_mqtt configuration struct - Layered config struct - Fix examples. --- components/mqtt/esp-mqtt | 2 +- .../mqtt/host_test/main/test_mqtt_client.cpp | 2 +- components/mqtt/test/test_mqtt.c | 6 ++--- .../mqtt/test/test_mqtt_client_broker.c | 26 +++++++++---------- 4 files changed, 18 insertions(+), 18 deletions(-) diff --git a/components/mqtt/esp-mqtt b/components/mqtt/esp-mqtt index 89e5c60..ae53d79 160000 --- a/components/mqtt/esp-mqtt +++ b/components/mqtt/esp-mqtt @@ -1 +1 @@ -Subproject commit 89e5c6014f8dbcfcd98af35fb507ca7b96ac8aee +Subproject commit ae53d799da294f03ef65c33e88fa33648e638134 diff --git a/components/mqtt/host_test/main/test_mqtt_client.cpp b/components/mqtt/host_test/main/test_mqtt_client.cpp index e0c0160..4979c19 100644 --- a/components/mqtt/host_test/main/test_mqtt_client.cpp +++ b/components/mqtt/host_test/main/test_mqtt_client.cpp @@ -95,7 +95,7 @@ TEST_CASE_METHOD(ClientInitializedFixture, "Client Start") { SECTION("Successful start") { esp_mqtt_client_config_t config{}; - config.uri = "mqtt://1.1.1.1"; + config.broker.address.uri = "mqtt://1.1.1.1"; struct http_parser_url ret_uri = { .field_set = 1 | (1<<1), .port = 0, diff --git a/components/mqtt/test/test_mqtt.c b/components/mqtt/test/test_mqtt.c index 7a0f938..2494d03 100644 --- a/components/mqtt/test/test_mqtt.c +++ b/components/mqtt/test/test_mqtt.c @@ -38,7 +38,7 @@ TEST_CASE("mqtt init with invalid url", "[mqtt][leaks=0]") { test_leak_setup(__FILE__, __LINE__); const esp_mqtt_client_config_t mqtt_cfg = { - .uri = "INVALID", + .broker.address.uri = "INVALID", }; esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg); TEST_ASSERT_EQUAL(NULL, client ); @@ -49,7 +49,7 @@ TEST_CASE("mqtt init and deinit", "[mqtt][leaks=0]") test_leak_setup(__FILE__, __LINE__); const esp_mqtt_client_config_t mqtt_cfg = { // no connection takes place, but the uri has to be valid for init() to succeed - .uri = "mqtts://localhost:8883", + .broker.address.uri = "mqtts://localhost:8883", }; esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg); TEST_ASSERT_NOT_EQUAL(NULL, client ); @@ -73,7 +73,7 @@ TEST_CASE("mqtt enqueue and destroy outbox", "[mqtt][leaks=0]") const int size = 2000; const esp_mqtt_client_config_t mqtt_cfg = { // no connection takes place, but the uri has to be valid for init() to succeed - .uri = "mqtts://localhost:8883", + .broker.address.uri = "mqtts://localhost:8883", }; esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg); TEST_ASSERT_NOT_EQUAL(NULL, client ); diff --git a/components/mqtt/test/test_mqtt_client_broker.c b/components/mqtt/test/test_mqtt_client_broker.c index cb0f3ae..05fca43 100644 --- a/components/mqtt/test/test_mqtt_client_broker.c +++ b/components/mqtt/test/test_mqtt_client_broker.c @@ -101,8 +101,8 @@ static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_ bool mqtt_connect_disconnect(void) { const esp_mqtt_client_config_t mqtt_cfg = { - .uri = CONFIG_MQTT_TEST_BROKER_URI, - .disable_auto_reconnect = true, + .broker.address.uri = CONFIG_MQTT_TEST_BROKER_URI, + .network.disable_auto_reconnect = true, }; s_event_group = xEventGroupCreate(); esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg); @@ -122,7 +122,7 @@ bool mqtt_connect_disconnect(void) bool mqtt_subscribe_publish(void) { const esp_mqtt_client_config_t mqtt_cfg = { - .uri = CONFIG_MQTT_TEST_BROKER_URI, + .broker.address.uri = CONFIG_MQTT_TEST_BROKER_URI, }; char* topic = append_mac("topic"); TEST_ASSERT_TRUE(NULL != topic); @@ -152,16 +152,16 @@ bool mqtt_lwt_clean_disconnect(void) char* lwt = append_mac("lwt"); TEST_ASSERT_TRUE(lwt); const esp_mqtt_client_config_t mqtt_cfg1 = { - .uri = CONFIG_MQTT_TEST_BROKER_URI, - .set_null_client_id = true, - .lwt_topic = lwt, - .lwt_msg = "lwt_msg" + .broker.address.uri = CONFIG_MQTT_TEST_BROKER_URI, + .credentials.set_null_client_id = true, + .session.last_will.topic = lwt, + .session.last_will.msg = "lwt_msg" }; const esp_mqtt_client_config_t mqtt_cfg2 = { - .uri = CONFIG_MQTT_TEST_BROKER_URI, - .set_null_client_id = true, - .lwt_topic = lwt, - .lwt_msg = "lwt_msg" + .broker.address.uri = CONFIG_MQTT_TEST_BROKER_URI, + .credentials.set_null_client_id = true, + .session.last_will.topic = lwt, + .session.last_will.msg = "lwt_msg" }; s_event_group = xEventGroupCreate(); @@ -201,8 +201,8 @@ bool mqtt_lwt_clean_disconnect(void) bool mqtt_subscribe_payload(void) { const esp_mqtt_client_config_t mqtt_cfg = { - .uri = CONFIG_MQTT_TEST_BROKER_URI, - .disable_auto_reconnect = true, + .broker.address.uri = CONFIG_MQTT_TEST_BROKER_URI, + .network.disable_auto_reconnect = true, }; char* topic = append_mac("topic"); TEST_ASSERT_TRUE(NULL != topic); From 94ecca207341b3c5b47f717ce0ed3d48b0f7d086 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Wed, 11 May 2022 16:01:48 +0200 Subject: [PATCH 86/91] esp_netif/lwip: Fix deps cycles to "lwip -> esp_netif -> phy-drivers" Fix dependency tree so that lwip doesn't depend on any specific network interface component. Network interface drivers shall depend on esp_netif. esp_netif shall depend on lwip (but not on any specific interface driver) -- it optionally depends on vfs and esp_eth (need ethernet header for L2/bridge mode) --- components/mqtt/CMakeLists.txt | 7 ++----- components/mqtt/test/CMakeLists.txt | 2 +- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/components/mqtt/CMakeLists.txt b/components/mqtt/CMakeLists.txt index 2e57327..629aca8 100644 --- a/components/mqtt/CMakeLists.txt +++ b/components/mqtt/CMakeLists.txt @@ -119,9 +119,6 @@ idf_component_get_property(mbedtls_dir mbedtls COMPONENT_DIR) target_link_libraries(${COMPONENT_LIB} PUBLIC mocks) else() - idf_component_get_property(http_parser_lib http_parser COMPONENT_LIB) - idf_component_get_property(tcp_transport_lib tcp_transport COMPONENT_LIB) - idf_component_get_property(lwip_lib lwip COMPONENT_LIB) - target_link_libraries(${COMPONENT_LIB} PUBLIC ${http_parser_lib} ${tcp_transport_lib}) - target_link_libraries(${COMPONENT_LIB} PRIVATE ${lwip_lib}) + idf_component_optional_requires(PUBLIC esp_event tcp_transport) + idf_component_optional_requires(PRIVATE http_parser) endif() diff --git a/components/mqtt/test/CMakeLists.txt b/components/mqtt/test/CMakeLists.txt index a5b7606..4910339 100644 --- a/components/mqtt/test/CMakeLists.txt +++ b/components/mqtt/test/CMakeLists.txt @@ -1,2 +1,2 @@ idf_component_register(SRC_DIRS "." - PRIV_REQUIRES cmock test_utils mqtt nvs_flash app_update esp_eth) + PRIV_REQUIRES cmock test_utils mqtt nvs_flash app_update esp_eth esp_netif) From 0b98c6324701e145988db5429d7711cfb232a457 Mon Sep 17 00:00:00 2001 From: yuanjm Date: Thu, 3 Mar 2022 15:40:03 +0800 Subject: [PATCH 87/91] mqtt: Add mqtt5 Kconfig --- components/mqtt/CMakeLists.txt | 11 +++++++---- components/mqtt/Kconfig | 6 ++++++ 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/components/mqtt/CMakeLists.txt b/components/mqtt/CMakeLists.txt index 629aca8..fda5220 100644 --- a/components/mqtt/CMakeLists.txt +++ b/components/mqtt/CMakeLists.txt @@ -1,7 +1,10 @@ -idf_component_register(SRCS "esp-mqtt/mqtt_client.c" - "esp-mqtt/lib/mqtt_msg.c" - "esp-mqtt/lib/mqtt_outbox.c" - "esp-mqtt/lib/platform_esp32_idf.c" +set(srcs esp-mqtt/mqtt_client.c esp-mqtt/lib/mqtt_msg.c esp-mqtt/lib/mqtt_outbox.c esp-mqtt/lib/platform_esp32_idf.c) + +if(CONFIG_MQTT_PROTOCOL_5) + list(APPEND srcs esp-mqtt/lib/mqtt5_msg.c esp-mqtt/mqtt5_client.c) +endif() + +idf_component_register(SRCS "${srcs}" INCLUDE_DIRS esp-mqtt/include PRIV_INCLUDE_DIRS "esp-mqtt/lib/include" PRIV_REQUIRES esp_timer diff --git a/components/mqtt/Kconfig b/components/mqtt/Kconfig index 06f06a1..4db6e15 100644 --- a/components/mqtt/Kconfig +++ b/components/mqtt/Kconfig @@ -6,6 +6,12 @@ menu "ESP-MQTT Configurations" help If not, this library will use MQTT protocol 3.1 + config MQTT_PROTOCOL_5 + bool "Enable MQTT protocol 5.0" + default n + help + If not, this library will not support MQTT 5.0 + config MQTT_TRANSPORT_SSL bool "Enable MQTT over SSL" default y From 01d0f3465475638a7f418e770cf2ce44edd0df15 Mon Sep 17 00:00:00 2001 From: yuanjm Date: Thu, 3 Mar 2022 15:41:07 +0800 Subject: [PATCH 88/91] test: Add mqtt5 unit-test --- components/mqtt/test/CMakeLists.txt | 8 +- components/mqtt/test/Kconfig | 5 + components/mqtt/test/test_mqtt5.c | 151 ++++++++++ .../mqtt/test/test_mqtt5_client_broker.c | 285 ++++++++++++++++++ .../mqtt/test/test_mqtt5_client_broker.h | 51 ++++ 5 files changed, 499 insertions(+), 1 deletion(-) create mode 100644 components/mqtt/test/test_mqtt5.c create mode 100644 components/mqtt/test/test_mqtt5_client_broker.c create mode 100644 components/mqtt/test/test_mqtt5_client_broker.h diff --git a/components/mqtt/test/CMakeLists.txt b/components/mqtt/test/CMakeLists.txt index 4910339..9ab0dfe 100644 --- a/components/mqtt/test/CMakeLists.txt +++ b/components/mqtt/test/CMakeLists.txt @@ -1,2 +1,8 @@ -idf_component_register(SRC_DIRS "." +set(srcs test_mqtt_client_broker.c test_mqtt_connection.c test_mqtt.c) + +if(CONFIG_MQTT_PROTOCOL_5) + list(APPEND srcs test_mqtt5_client_broker.c test_mqtt5.c) +endif() + +idf_component_register(SRCS "${srcs}" PRIV_REQUIRES cmock test_utils mqtt nvs_flash app_update esp_eth esp_netif) diff --git a/components/mqtt/test/Kconfig b/components/mqtt/test/Kconfig index e006f92..30b2c10 100644 --- a/components/mqtt/test/Kconfig +++ b/components/mqtt/test/Kconfig @@ -6,4 +6,9 @@ menu "ESP-MQTT Unit Test Config" help URL of an mqtt broker which this test connects to. + config MQTT5_TEST_BROKER_URI + string "URI of the test broker" + default "mqtt://mqtt.eclipseprojects.io" + help + URL of an mqtt broker which this test connects to. endmenu diff --git a/components/mqtt/test/test_mqtt5.c b/components/mqtt/test/test_mqtt5.c new file mode 100644 index 0000000..01c33e7 --- /dev/null +++ b/components/mqtt/test/test_mqtt5.c @@ -0,0 +1,151 @@ +/* + * SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include "freertos/FreeRTOS.h" +#include "freertos/event_groups.h" +#include "unity.h" +#include "test_utils.h" +#include "memory_checks.h" +#include "mqtt_client.h" +#include "nvs_flash.h" +#include "esp_ota_ops.h" +#include "sdkconfig.h" +#include "test_mqtt5_client_broker.h" +#include "test_mqtt_connection.h" +#include "esp_mac.h" + +static esp_mqtt5_user_property_item_t user_property_arr[3] = { + {"board", "esp32"}, + {"u", "user"}, + {"p", "password"} +}; + +static void test_leak_setup(const char * file, long line) +{ + uint8_t mac[6]; + struct timeval te; + gettimeofday(&te, NULL); // get current time + esp_read_mac(mac, ESP_MAC_WIFI_STA); + printf("%s:%ld: time=%ld.%lds, mac:" MACSTR "\n", file, line, te.tv_sec, te.tv_usec, MAC2STR(mac)); + test_utils_record_free_mem(); +} + +TEST_CASE("mqtt5 init with invalid url", "[mqtt5][leaks=0]") +{ + test_leak_setup(__FILE__, __LINE__); + const esp_mqtt_client_config_t mqtt5_cfg = { + .broker.address.uri = "INVALID", + .session.protocol_ver = MQTT_PROTOCOL_V_5, + }; + esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt5_cfg); + TEST_ASSERT_EQUAL(NULL, client ); +} + +TEST_CASE("mqtt5 init and deinit", "[mqtt5][leaks=0]") +{ + test_leak_setup(__FILE__, __LINE__); + const esp_mqtt_client_config_t mqtt5_cfg = { + // no connection takes place, but the uri has to be valid for init() to succeed + .broker.address.uri = "mqtts://localhost:8883", + .session.protocol_ver = MQTT_PROTOCOL_V_5, + .credentials.username = "123", + .credentials.authentication.password = "456", + .session.last_will.topic = "/topic/will", + .session.last_will.msg = "i will leave", + .session.last_will.msg_len = 12, + .session.last_will.qos = 1, + .session.last_will.retain = true, + }; + esp_mqtt5_connection_property_config_t connect_property = { + .session_expiry_interval = 10, + .maximum_packet_size = 1024, + .receive_maximum = 65535, + .topic_alias_maximum = 2, + .request_resp_info = true, + .request_problem_info = true, + .will_delay_interval = 10, + .payload_format_indicator = true, + .message_expiry_interval = 10, + .content_type = "json", + .response_topic = "/test/response", + .correlation_data = "123456", + .correlation_data_len = 6, + }; + + esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt5_cfg); + esp_mqtt5_client_set_user_property(&connect_property.user_property, user_property_arr, 3); + esp_mqtt5_client_set_user_property(&connect_property.will_user_property, user_property_arr, 3); + esp_mqtt5_client_set_connect_property(client, &connect_property); + esp_mqtt5_client_delete_user_property(connect_property.user_property); + esp_mqtt5_client_delete_user_property(connect_property.will_user_property); + TEST_ASSERT_NOT_EQUAL(NULL, client ); + esp_mqtt_client_destroy(client); +} + +static const char* this_bin_addr(void) +{ + spi_flash_mmap_handle_t out_handle; + const void *binary_address; + const esp_partition_t* partition = esp_ota_get_running_partition(); + esp_partition_mmap(partition, 0, partition->size, SPI_FLASH_MMAP_DATA, &binary_address, &out_handle); + return binary_address; +} + +TEST_CASE("mqtt5 enqueue and destroy outbox", "[mqtt5][leaks=0]") +{ + const char * bin_addr = this_bin_addr(); + test_leak_setup(__FILE__, __LINE__); + const int messages = 20; + const int size = 2000; + const esp_mqtt_client_config_t mqtt5_cfg = { + // no connection takes place, but the uri has to be valid for init() to succeed + .broker.address.uri = "mqtts://localhost:8883", + .session.protocol_ver = MQTT_PROTOCOL_V_5, + }; + esp_mqtt5_publish_property_config_t publish_property = { + .payload_format_indicator = 1, + .message_expiry_interval = 1000, + .topic_alias = 0, + .response_topic = "/topic/test/response", + .correlation_data = "123456", + .correlation_data_len = 6, + .content_type = "json", + }; + esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt5_cfg); + TEST_ASSERT_NOT_EQUAL(NULL, client ); + int bytes_before = esp_get_free_heap_size(); + for (int i = 0; i < messages; i ++) { + esp_mqtt5_client_set_user_property(&publish_property.user_property, user_property_arr, 3); + esp_mqtt5_client_set_publish_property(client, &publish_property); + esp_mqtt_client_publish(client, "test", bin_addr, size, 1, 0); + esp_mqtt5_client_delete_user_property(publish_property.user_property); + publish_property.user_property = NULL; + } + int bytes_after = esp_get_free_heap_size(); + // check that outbox allocated all messages on heap + TEST_ASSERT_GREATER_OR_EQUAL(messages*size, bytes_before - bytes_after); + + esp_mqtt_client_destroy(client); +} + +#if SOC_EMAC_SUPPORTED +/** + * This test cases uses ethernet kit, so build and use it only if EMAC supported + */ +TEST_CASE("mqtt5 broker tests", "[mqtt5][test_env=UT_T2_Ethernet]") +{ + test_case_uses_tcpip(); + connect_test_fixture_setup(); + + RUN_MQTT5_BROKER_TEST(mqtt5_connect_disconnect); + RUN_MQTT5_BROKER_TEST(mqtt5_subscribe_publish); + RUN_MQTT5_BROKER_TEST(mqtt5_lwt_clean_disconnect); + RUN_MQTT5_BROKER_TEST(mqtt5_subscribe_payload); + + connect_test_fixture_teardown(); +} +#endif // SOC_EMAC_SUPPORTED diff --git a/components/mqtt/test/test_mqtt5_client_broker.c b/components/mqtt/test/test_mqtt5_client_broker.c new file mode 100644 index 0000000..ced44e9 --- /dev/null +++ b/components/mqtt/test/test_mqtt5_client_broker.c @@ -0,0 +1,285 @@ +/* + * SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include "freertos/FreeRTOS.h" +#include "freertos/event_groups.h" +#include "mqtt_client.h" +#include "esp_log.h" +#include "esp_mac.h" + +#define WAIT_FOR_EVENT(event) \ + TEST_ASSERT_TRUE(xEventGroupWaitBits(s_event_group, event, pdTRUE, pdTRUE, pdMS_TO_TICKS(COMMON_OPERATION_TIMEOUT)) & event); + +#define TEST_ASSERT_TRUE(condition) TEST_ASSERT_TRUE_LINE(condition, __LINE__) +#define TEST_ASSERT_TRUE_LINE(condition, line) \ + do { \ + if (!(condition)) { \ + ESP_LOGE("test_mqtt5_client_broker.c", \ + "Assertion failed in line %d", line); \ + return false; \ + } \ + } while(0) + + +static const int COMMON_OPERATION_TIMEOUT = 10000; +static const int CONNECT_BIT = BIT0; +static const int DISCONNECT_BIT = BIT1; +static const int DATA_BIT = BIT2; + +static EventGroupHandle_t s_event_group; + +static esp_mqtt5_user_property_item_t user_property_arr[3] = { + {"board", "esp32"}, + {"u", "user"}, + {"p", "password"} +}; + +static char* append_mac(const char* string) +{ + uint8_t mac[6]; + char *id_string = NULL; + esp_read_mac(mac, ESP_MAC_WIFI_STA); + asprintf(&id_string, "%s_%02x%02X%02X", string, mac[3], mac[4], mac[5]); + return id_string; +} + +static void mqtt5_data_handler_qos(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data) +{ + if (event_id == MQTT_EVENT_DATA) { + esp_mqtt_event_handle_t event = event_data; + int * qos = handler_args; + *qos = event->qos; + xEventGroupSetBits(s_event_group, DATA_BIT); + } +} + +static void mqtt5_data_handler_lwt(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data) +{ + if (event_id == MQTT_EVENT_DATA) { + esp_mqtt_event_handle_t event = event_data; + ESP_LOGI("mqtt-lwt", "MQTT_EVENT_DATA"); + ESP_LOGI("mqtt-lwt", "TOPIC=%.*s", event->topic_len, event->topic); + ESP_LOGI("mqtt-lwt", "DATA=%.*s", event->data_len, event->data); + if (strncmp(event->data, "no-lwt", event->data_len) == 0) { + // no lwt, just to indicate the test has finished + xEventGroupSetBits(s_event_group, DATA_BIT); + } else { + // count up any potential lwt message + int * count = handler_args; + *count = *count + 1; + ESP_LOGE("mqtt5-lwt", "count=%d", *count); + } + } +} + +static void mqtt5_data_handler_subscribe(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data) +{ + if (event_id == MQTT_EVENT_SUBSCRIBED) { + esp_mqtt_event_handle_t event = event_data; + ESP_LOGI("mqtt5-subscribe", "MQTT_EVENT_SUBSCRIBED, data size=%d", event->data_len); + int * sub_payload = handler_args; + if (event->data_len == 1) { + ESP_LOGI("mqtt5-subscribe", "DATA=%d", *(uint8_t*)event->data); + *sub_payload = *(uint8_t*)event->data; + } + xEventGroupSetBits(s_event_group, DATA_BIT); + } +} + + +static void mqtt5_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data) +{ + switch ((esp_mqtt_event_id_t)event_id) { + case MQTT_EVENT_CONNECTED: + xEventGroupSetBits(s_event_group, CONNECT_BIT); + break; + + case MQTT_EVENT_DISCONNECTED: + xEventGroupSetBits(s_event_group, DISCONNECT_BIT); + break; + default: + break; + } +} + +bool mqtt5_connect_disconnect(void) +{ + const esp_mqtt_client_config_t mqtt5_cfg = { + .broker.address.uri = CONFIG_MQTT5_TEST_BROKER_URI, + .network.disable_auto_reconnect = true, + .session.protocol_ver = MQTT_PROTOCOL_V_5, + }; + esp_mqtt5_connection_property_config_t connect_property = { + .session_expiry_interval = 10, + .maximum_packet_size = 1024, + .receive_maximum = 65535, + .topic_alias_maximum = 2, + .request_resp_info = true, + .request_problem_info = true, + }; + esp_mqtt5_disconnect_property_config_t disconnect_property = { + .session_expiry_interval = 10, + .disconnect_reason = 0, + }; + s_event_group = xEventGroupCreate(); + esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt5_cfg); + TEST_ASSERT_TRUE(NULL != client ); + esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt5_event_handler, NULL); + TEST_ASSERT_TRUE(ESP_OK == esp_mqtt5_client_set_user_property(&connect_property.user_property, user_property_arr, 3)); + TEST_ASSERT_TRUE(ESP_OK == esp_mqtt5_client_set_connect_property(client, &connect_property)); + esp_mqtt5_client_delete_user_property(connect_property.user_property); + TEST_ASSERT_TRUE(ESP_OK == esp_mqtt_client_start(client)); + WAIT_FOR_EVENT(CONNECT_BIT); + TEST_ASSERT_TRUE(ESP_OK == esp_mqtt5_client_set_user_property(&disconnect_property.user_property, user_property_arr, 3)); + TEST_ASSERT_TRUE(ESP_OK == esp_mqtt5_client_set_disconnect_property(client, &disconnect_property)); + esp_mqtt5_client_delete_user_property(disconnect_property.user_property); + esp_mqtt_client_disconnect(client); + WAIT_FOR_EVENT(DISCONNECT_BIT); + esp_mqtt_client_reconnect(client); + WAIT_FOR_EVENT(CONNECT_BIT); + esp_mqtt_client_destroy(client); + vEventGroupDelete(s_event_group); + return true; +} + +bool mqtt5_subscribe_publish(void) +{ + const esp_mqtt_client_config_t mqtt5_cfg = { + .broker.address.uri = CONFIG_MQTT5_TEST_BROKER_URI, + .session.protocol_ver = MQTT_PROTOCOL_V_5, + }; + esp_mqtt5_publish_property_config_t publish_property = { + .payload_format_indicator = 1, + .message_expiry_interval = 1000, + .topic_alias = 1, + .response_topic = "/topic/test/response", + .correlation_data = "123456", + .correlation_data_len = 6, + .content_type = "json", + }; + esp_mqtt5_subscribe_property_config_t subscribe_property = { + .subscribe_id = 25555, + .no_local_flag = false, + .retain_as_published_flag = true, + .retain_handle = 0, + }; + char* topic = append_mac("topic"); + TEST_ASSERT_TRUE(NULL != topic); + s_event_group = xEventGroupCreate(); + esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt5_cfg); + TEST_ASSERT_TRUE(NULL != client ); + esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt5_event_handler, NULL); + TEST_ASSERT_TRUE(ESP_OK == esp_mqtt_client_start(client)); + WAIT_FOR_EVENT(CONNECT_BIT); + int qos = -1; + esp_mqtt_client_register_event(client, MQTT_EVENT_DATA, mqtt5_data_handler_qos, &qos); + TEST_ASSERT_TRUE(ESP_OK == esp_mqtt5_client_set_subscribe_property(client, &subscribe_property)); + TEST_ASSERT_TRUE(esp_mqtt_client_subscribe(client, topic, 2) != -1); + TEST_ASSERT_TRUE(ESP_OK == esp_mqtt5_client_set_publish_property(client, &publish_property)); + TEST_ASSERT_TRUE(esp_mqtt_client_publish(client, topic, "message", 0, 2, 0) != -1); + WAIT_FOR_EVENT(DATA_BIT); + TEST_ASSERT_TRUE(qos == 2); + TEST_ASSERT_TRUE(esp_mqtt_client_publish(client, topic, "message", 0, 1, 0) != -1); + WAIT_FOR_EVENT(DATA_BIT); + TEST_ASSERT_TRUE(qos == 1); + esp_mqtt_client_destroy(client); + vEventGroupDelete(s_event_group); + free(topic); + return true; +} + +bool mqtt5_lwt_clean_disconnect(void) +{ + char* lwt = append_mac("lwt"); + TEST_ASSERT_TRUE(lwt); + const esp_mqtt_client_config_t mqtt5_cfg1 = { + .broker.address.uri = CONFIG_MQTT5_TEST_BROKER_URI, + .credentials.set_null_client_id = true, + .session.last_will.topic = lwt, + .session.last_will.msg = "lwt_msg", + .session.protocol_ver = MQTT_PROTOCOL_V_5, + }; + const esp_mqtt_client_config_t mqtt5_cfg2 = { + .broker.address.uri = CONFIG_MQTT5_TEST_BROKER_URI, + .credentials.set_null_client_id = true, + .session.last_will.topic = lwt, + .session.last_will.msg = "lwt_msg", + .session.protocol_ver = MQTT_PROTOCOL_V_5, + }; + esp_mqtt5_connection_property_config_t connect_property = { + .will_delay_interval = 10, + .payload_format_indicator = true, + .message_expiry_interval = 10, + .content_type = "json", + .response_topic = "/test/response", + .correlation_data = "123456", + .correlation_data_len = 6, + }; + s_event_group = xEventGroupCreate(); + + esp_mqtt_client_handle_t client1 = esp_mqtt_client_init(&mqtt5_cfg1); + esp_mqtt_client_handle_t client2 = esp_mqtt_client_init(&mqtt5_cfg2); + TEST_ASSERT_TRUE(NULL != client1 && NULL != client2 ); + esp_mqtt_client_register_event(client1, ESP_EVENT_ANY_ID, mqtt5_event_handler, NULL); + esp_mqtt_client_register_event(client2, ESP_EVENT_ANY_ID, mqtt5_event_handler, NULL); + TEST_ASSERT_TRUE(ESP_OK == esp_mqtt5_client_set_connect_property(client1, &connect_property)); + TEST_ASSERT_TRUE(ESP_OK == esp_mqtt5_client_set_connect_property(client2, &connect_property)); + TEST_ASSERT_TRUE(esp_mqtt_client_start(client1) == ESP_OK); + WAIT_FOR_EVENT(CONNECT_BIT); + TEST_ASSERT_TRUE(esp_mqtt_client_start(client2) == ESP_OK); + WAIT_FOR_EVENT(CONNECT_BIT); + int counter = 0; + esp_mqtt_client_register_event(client1, MQTT_EVENT_DATA, mqtt5_data_handler_lwt, &counter); + esp_mqtt_client_register_event(client2, MQTT_EVENT_DATA, mqtt5_data_handler_lwt, &counter); + TEST_ASSERT_TRUE(esp_mqtt_client_subscribe(client1, lwt, 0) != -1); + TEST_ASSERT_TRUE(esp_mqtt_client_subscribe(client2, lwt, 0) != -1); + esp_mqtt_client_disconnect(client1); + WAIT_FOR_EVENT(DISCONNECT_BIT); + esp_mqtt_client_reconnect(client1); + WAIT_FOR_EVENT(CONNECT_BIT); + TEST_ASSERT_TRUE(esp_mqtt_client_subscribe(client1, lwt, 0) != -1); + esp_mqtt_client_stop(client2); + esp_mqtt_client_start(client2); + WAIT_FOR_EVENT(CONNECT_BIT); + TEST_ASSERT_TRUE(esp_mqtt_client_subscribe(client2, lwt, 0) != -1); + TEST_ASSERT_TRUE(esp_mqtt_client_publish(client1, lwt, "no-lwt", 0, 0, 0) != -1); + WAIT_FOR_EVENT(DATA_BIT); + TEST_ASSERT_TRUE(counter == 0); + esp_mqtt_client_destroy(client1); + esp_mqtt_client_destroy(client2); + vEventGroupDelete(s_event_group); + free(lwt); + return true; +} + +bool mqtt5_subscribe_payload(void) +{ + const esp_mqtt_client_config_t mqtt5_cfg = { + .broker.address.uri = CONFIG_MQTT5_TEST_BROKER_URI, + .network.disable_auto_reconnect = true, + .session.protocol_ver = MQTT_PROTOCOL_V_5, + }; + char* topic = append_mac("topic"); + TEST_ASSERT_TRUE(NULL != topic); + s_event_group = xEventGroupCreate(); + esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt5_cfg); + TEST_ASSERT_TRUE(NULL != client ); + esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt5_event_handler, NULL); + TEST_ASSERT_TRUE(ESP_OK == esp_mqtt_client_start(client)); + WAIT_FOR_EVENT(CONNECT_BIT); + int qos_payload = -1; + esp_mqtt_client_register_event(client, MQTT_EVENT_SUBSCRIBED, mqtt5_data_handler_subscribe, &qos_payload); + TEST_ASSERT_TRUE(esp_mqtt_client_subscribe(client, topic, 2) != -1); + WAIT_FOR_EVENT(DATA_BIT); + TEST_ASSERT_TRUE(qos_payload == 2); + TEST_ASSERT_TRUE(esp_mqtt_client_subscribe(client, topic, 0) != -1); + WAIT_FOR_EVENT(DATA_BIT); + TEST_ASSERT_TRUE(qos_payload == 0); + esp_mqtt_client_destroy(client); + vEventGroupDelete(s_event_group); + free(topic); + return true; +} diff --git a/components/mqtt/test/test_mqtt5_client_broker.h b/components/mqtt/test/test_mqtt5_client_broker.h new file mode 100644 index 0000000..52b6ab8 --- /dev/null +++ b/components/mqtt/test/test_mqtt5_client_broker.h @@ -0,0 +1,51 @@ +/* + * SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#pragma once +#include "esp_log.h" + +/** + * @brief MQTT5 client-broker tests are not implemented as separate test cases + * due to time consuming connection setup/teardown. + * This utility macro is used to run functional cases as MQTT tests + * and evaluate as separate assertions in one "mqtt5 broker tests" test case. + */ +#define RUN_MQTT5_BROKER_TEST(test_name) \ + do { \ + ESP_LOGI("mqtt5_test", "Running test:" #test_name "()"); \ + TEST_ASSERT_TRUE_MESSAGE(test_name(), "Mqtt5 test failed: " #test_name "() "); \ + ESP_LOGI("mqtt5_test", "Test:" #test_name "() passed "); \ + } while(0) + + +/** + * @brief This module contains mqtt5 test cases interacting the client with a (real) broker + */ + +/** + * @brief The client subscribes and publishes on the same topic + * and verifies the received published qos in the event + */ +bool mqtt5_subscribe_publish(void); + +/** + * @brief The client connects, disconnects and reconnects. + * Tests basic client state transitions + */ +bool mqtt5_connect_disconnect(void); + +/** + * @brief Two clients with defined lwt connect and subscribe to lwt topic. + * This test verifies that no lwt is send when each of the client disconnects. + * (we expect a clean disconnection, so no last-will being sent) + */ +bool mqtt5_lwt_clean_disconnect(void); + +/** + * @brief The client subscribes to a topic with certain qos + * and verifies the qos in SUBACK message from the broker. + */ +bool mqtt5_subscribe_payload(void); From ee9b52b7d27863ef4bfba08f60e31a76fc109b2b Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Mon, 31 Jan 2022 19:45:31 +0100 Subject: [PATCH 89/91] build system: re-add -Wno-format as private flag for some components --- components/mqtt/CMakeLists.txt | 1 + components/mqtt/test/CMakeLists.txt | 1 + 2 files changed, 2 insertions(+) diff --git a/components/mqtt/CMakeLists.txt b/components/mqtt/CMakeLists.txt index fda5220..15aea3d 100644 --- a/components/mqtt/CMakeLists.txt +++ b/components/mqtt/CMakeLists.txt @@ -9,6 +9,7 @@ idf_component_register(SRCS "${srcs}" PRIV_INCLUDE_DIRS "esp-mqtt/lib/include" PRIV_REQUIRES esp_timer ) +target_compile_options(${COMPONENT_LIB} PRIVATE "-Wno-format") if(TEST_BUILD) message(STATUS "building MOCKS") diff --git a/components/mqtt/test/CMakeLists.txt b/components/mqtt/test/CMakeLists.txt index 9ab0dfe..616d362 100644 --- a/components/mqtt/test/CMakeLists.txt +++ b/components/mqtt/test/CMakeLists.txt @@ -6,3 +6,4 @@ endif() idf_component_register(SRCS "${srcs}" PRIV_REQUIRES cmock test_utils mqtt nvs_flash app_update esp_eth esp_netif) + target_compile_options(${COMPONENT_LIB} PRIVATE "-Wno-format") From f27cb47c0f6f3a922c8e939d435d1987dff49136 Mon Sep 17 00:00:00 2001 From: Euripedes Rocha Date: Mon, 5 Sep 2022 15:46:10 +0200 Subject: [PATCH 90/91] [MQTT] Simplifies MQTT CMakeLists file - Uses mocks from tools instead of creating them. - Move host based definition to test code. --- components/mqtt/CMakeLists.txt | 119 +----------------- components/mqtt/host_test/CMakeLists.txt | 10 +- components/mqtt/host_test/main/CMakeLists.txt | 2 +- .../mqtt/host_test/main/test_mqtt_client.cpp | 3 - .../mocks/include/local_FreeRTOS_config.h | 6 - components/mqtt/test/test_mqtt.c | 1 + 6 files changed, 12 insertions(+), 129 deletions(-) delete mode 100644 components/mqtt/host_test/mocks/include/local_FreeRTOS_config.h diff --git a/components/mqtt/CMakeLists.txt b/components/mqtt/CMakeLists.txt index 15aea3d..29aa349 100644 --- a/components/mqtt/CMakeLists.txt +++ b/components/mqtt/CMakeLists.txt @@ -7,122 +7,7 @@ endif() idf_component_register(SRCS "${srcs}" INCLUDE_DIRS esp-mqtt/include PRIV_INCLUDE_DIRS "esp-mqtt/lib/include" - PRIV_REQUIRES esp_timer + REQUIRES esp_event tcp_transport + PRIV_REQUIRES esp_timer http_parser esp_hw_support ) target_compile_options(${COMPONENT_LIB} PRIVATE "-Wno-format") - -if(TEST_BUILD) -message(STATUS "building MOCKS") -idf_component_get_property(tcp_transport_dir tcp_transport COMPONENT_DIR) -idf_component_get_property(esp_hw_support_dir esp_hw_support COMPONENT_DIR) -idf_component_get_property(esp_event_dir esp_event COMPONENT_DIR) -idf_component_get_property(log_dir log COMPONENT_DIR) -idf_component_get_property(freertos_dir freertos COMPONENT_OVERRIDEN_DIR) -idf_component_get_property(http_parser_dir http_parser COMPONENT_DIR) -idf_component_get_property(esp_wifi_dir esp_wifi COMPONENT_DIR) -idf_component_get_property(esp_tls_dir esp-tls COMPONENT_DIR) -idf_component_get_property(esp_netif_dir esp_netif COMPONENT_DIR) -idf_component_get_property(esp_common_dir esp_common COMPONENT_DIR) -idf_component_get_property(esp_rom_dir esp_rom COMPONENT_DIR) -idf_component_get_property(esp_system_dir esp_system COMPONENT_DIR) -idf_component_get_property(mbedtls_dir mbedtls COMPONENT_DIR) - - idf_component_get_property(cmock_lib cmock COMPONENT_LIB) - set(IDF_PATH $ENV{IDF_PATH}) - set(CMOCK_DIR "${IDF_PATH}/components/cmock/CMock") - set(MOCK_GEN_DIR "${CMAKE_CURRENT_BINARY_DIR}/mocks") - set(ENV{UNITY_DIR} "$ENV{IDF_PATH}/components/cmock/CMock") - file(MAKE_DIRECTORY ${MOCK_GEN_DIR}) - - set(MOCK_OUTPUT - "${MOCK_GEN_DIR}/Mockesp_transport.c" "${MOCK_GEN_DIR}/Mockesp_transport.h" - "${MOCK_GEN_DIR}/Mockesp_transport_ssl.c" "${MOCK_GEN_DIR}/Mockesp_transport_ssl.h" - "${MOCK_GEN_DIR}/Mockesp_transport_ws.c" "${MOCK_GEN_DIR}/Mockesp_transport_ws.h" - "${MOCK_GEN_DIR}/Mockesp_transport_tcp.c" "${MOCK_GEN_DIR}/Mockesp_transport_tcp.h" - "${MOCK_GEN_DIR}/Mockesp_event.c" "${MOCK_GEN_DIR}/Mockesp_event.h" - "${MOCK_GEN_DIR}/Mockesp_mac.c" "${MOCK_GEN_DIR}/Mockesp_mac.h" - "${MOCK_GEN_DIR}/Mockesp_random.c" "${MOCK_GEN_DIR}/Mockesp_random.h" - "${MOCK_GEN_DIR}/Mockesp_system.c" "${MOCK_GEN_DIR}/Mockesp_system.h" - "${MOCK_GEN_DIR}/Mockesp_tls.c" "${MOCK_GEN_DIR}/Mockesp_tls.h" - "${MOCK_GEN_DIR}/Mockevent_groups.c" "${MOCK_GEN_DIR}/Mockevent_groups.h" - "${MOCK_GEN_DIR}/Mockqueue.c" "${MOCK_GEN_DIR}/Mockqueue.h" - "${MOCK_GEN_DIR}/Mocktask.c" "${MOCK_GEN_DIR}/Mocktask.h" - "${MOCK_GEN_DIR}/Mockesp_log.c" "${MOCK_GEN_DIR}/Mockesp_log.h" - "${MOCK_GEN_DIR}/Mockhttp_parser.c" "${MOCK_GEN_DIR}/Mockhttp_parser.h" - ) - - set(HEADERS_TO_MOCK - ${tcp_transport_dir}/include/esp_transport_tcp.h - ${tcp_transport_dir}/include/esp_transport_ws.h - ${tcp_transport_dir}/include/esp_transport_ssl.h - ${tcp_transport_dir}/include/esp_transport.h - ${esp_event_dir}/include/esp_event.h - ${esp_hw_support_dir}/include/esp_mac.h - ${esp_hw_support_dir}/include/esp_random.h - ${freertos_dir}/FreeRTOS-Kernel/include/freertos/event_groups.h - ${log_dir}/include/esp_log.h - ${http_parser_dir}/http_parser.h - ) - - set(srcs - ${MOCK_GEN_DIR}/Mockesp_transport.c - ${MOCK_GEN_DIR}/Mockesp_transport_ws.c - ${MOCK_GEN_DIR}/Mockesp_transport_ssl.c - ${MOCK_GEN_DIR}/Mockesp_transport_tcp.c - ${MOCK_GEN_DIR}/Mockesp_transport_tcp.c - ${MOCK_GEN_DIR}/Mockesp_event.c - ${MOCK_GEN_DIR}/Mockesp_mac.c - ${MOCK_GEN_DIR}/Mockesp_random.c - ${MOCK_GEN_DIR}/Mockesp_log.c - ${MOCK_GEN_DIR}/Mockhttp_parser.c - ${MOCK_GEN_DIR}/Mockevent_groups.c - ) - - add_custom_command( - OUTPUT ruby_found SYMBOLIC - COMMAND "ruby" "-v" - COMMENT "Try to find ruby. If this fails, you need to install ruby" - ) - - add_custom_command( - OUTPUT ${MOCK_OUTPUT} - DEPENDS ruby_found - COMMAND ${CMAKE_COMMAND} -E env "UNITY_DIR=${IDF_PATH}/components/unity/unity" - ruby - ${CMOCK_DIR}/lib/cmock.rb - -o${CMAKE_CURRENT_SOURCE_DIR}/host_test/mocks/config.yaml - ${HEADERS_TO_MOCK} - ) - - add_library(mocks ${srcs}) - target_include_directories(mocks PUBLIC - ${CMAKE_CURRENT_SOURCE_DIR}/host_test/mocks/include - ${tcp_transport_dir}/include - ${esp_tls_dir} - ${freertos_dir}/FreeRTOS-Kernel/include - ${esp_event_dir}/include - ${esp_system_dir}/include - ${esp_common_dir}/include - ${esp_wifi_dir}/include - ${esp_hw_support_dir}/include - ${esp_netif_dir}/include - ${log_dir}/include - ${esp_rom_dir}/include - ${mbedtls_dir}/port/include - ${http_parser_dir} - ${mbedtls_dir}/mbedtls/include - ${freertos_dir}/FreeRTOS-Kernel/include/freertos - esp-mqtt/lib/include - ${MOCK_GEN_DIR} - ) - target_link_libraries(mocks PUBLIC ${cmock_lib}) - target_compile_definitions(mocks PUBLIC - CONFIG_LOG_TIMESTAMP_SOURCE_RTOS) - target_link_options(${COMPONENT_LIB} INTERFACE -fsanitize=address) - - target_link_libraries(${COMPONENT_LIB} PUBLIC mocks) - -else() - idf_component_optional_requires(PUBLIC esp_event tcp_transport) - idf_component_optional_requires(PRIVATE http_parser) -endif() diff --git a/components/mqtt/host_test/CMakeLists.txt b/components/mqtt/host_test/CMakeLists.txt index 7137910..d1c1ac6 100644 --- a/components/mqtt/host_test/CMakeLists.txt +++ b/components/mqtt/host_test/CMakeLists.txt @@ -2,9 +2,15 @@ cmake_minimum_required(VERSION 3.16) include($ENV{IDF_PATH}/tools/cmake/project.cmake) set(COMPONENTS main) -list(APPEND EXTRA_COMPONENT_DIRS "$ENV{IDF_PATH}/tools/mocks/freertos/" +list(APPEND EXTRA_COMPONENT_DIRS + "$ENV{IDF_PATH}/tools/mocks/esp_hw_support/" + "$ENV{IDF_PATH}/tools/mocks/freertos/" "$ENV{IDF_PATH}/tools/mocks/esp_timer/" + "$ENV{IDF_PATH}/tools/mocks/esp_event/" + "$ENV{IDF_PATH}/tools/mocks/lwip/" + "$ENV{IDF_PATH}/tools/mocks/esp-tls/" + "$ENV{IDF_PATH}/tools/mocks/http_parser/" + "$ENV{IDF_PATH}/tools/mocks/tcp_transport/" ) -option(TEST_BUILD "" ON) project(host_mqtt_client_test) diff --git a/components/mqtt/host_test/main/CMakeLists.txt b/components/mqtt/host_test/main/CMakeLists.txt index 90692f6..6c4a9c7 100644 --- a/components/mqtt/host_test/main/CMakeLists.txt +++ b/components/mqtt/host_test/main/CMakeLists.txt @@ -1,3 +1,3 @@ idf_component_register(SRCS "test_mqtt_client.cpp" INCLUDE_DIRS "$ENV{IDF_PATH}/tools/catch" - REQUIRES cmock mqtt esp_timer) + REQUIRES cmock mqtt esp_timer esp_hw_support http_parser log) diff --git a/components/mqtt/host_test/main/test_mqtt_client.cpp b/components/mqtt/host_test/main/test_mqtt_client.cpp index 4979c19..8ebbf1e 100644 --- a/components/mqtt/host_test/main/test_mqtt_client.cpp +++ b/components/mqtt/host_test/main/test_mqtt_client.cpp @@ -3,7 +3,6 @@ extern "C" { #include "Mockesp_event.h" -#include "Mockesp_log.h" #include "Mockesp_mac.h" #include "Mockesp_transport.h" #include "Mockesp_transport_ssl.h" @@ -37,14 +36,12 @@ struct ClientInitializedFixture { int transport; int event_group; uint8_t mac[] = {0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55}; - esp_log_write_Ignore(); esp_timer_get_time_IgnoreAndReturn(0); xQueueTakeMutexRecursive_IgnoreAndReturn(true); xQueueGiveMutexRecursive_IgnoreAndReturn(true); xQueueCreateMutex_ExpectAnyArgsAndReturn( reinterpret_cast(&mtx)); xEventGroupCreate_IgnoreAndReturn(reinterpret_cast(&event_group)); - esp_log_timestamp_IgnoreAndReturn(0); esp_transport_list_init_IgnoreAndReturn(reinterpret_cast(&transport_list)); esp_transport_tcp_init_IgnoreAndReturn(reinterpret_cast(&transport)); esp_transport_ssl_init_IgnoreAndReturn(reinterpret_cast(&transport)); diff --git a/components/mqtt/host_test/mocks/include/local_FreeRTOS_config.h b/components/mqtt/host_test/mocks/include/local_FreeRTOS_config.h deleted file mode 100644 index e7e1014..0000000 --- a/components/mqtt/host_test/mocks/include/local_FreeRTOS_config.h +++ /dev/null @@ -1,6 +0,0 @@ -/* - * SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD - * - * SPDX-License-Identifier: Apache-2.0 - */ -#define configUSE_TRACE_FACILITY 1 diff --git a/components/mqtt/test/test_mqtt.c b/components/mqtt/test/test_mqtt.c index 2494d03..d997bce 100644 --- a/components/mqtt/test/test_mqtt.c +++ b/components/mqtt/test/test_mqtt.c @@ -104,4 +104,5 @@ TEST_CASE("mqtt broker tests", "[mqtt][test_env=UT_T2_Ethernet]") connect_test_fixture_teardown(); } + #endif // SOC_EMAC_SUPPORTED From 159b1638b283bdd5aa6dc69ffcc9f580961d120c Mon Sep 17 00:00:00 2001 From: Euripedes Rocha Date: Mon, 17 Oct 2022 12:21:52 +0200 Subject: [PATCH 91/91] Reorganize mqtt build structure - Integrate build definitions from idf - Changes CMakeLists to allow import - Added host test from idf - Added test code from idf --- CMakeLists.txt | 18 ++ components/mqtt/Kconfig => Kconfig | 7 + components/mqtt/CMakeLists.txt | 13 - components/mqtt/esp-mqtt | 1 - components/mqtt/host_test/mocks/config.yaml | 25 -- .../mocks/include/freertos/FreeRTOSConfig.h | 133 -------- .../mocks/include/freertos/portmacro.h | 177 ----------- .../host_test/mocks/include/machine/endian.h | 2 - components/mqtt/test/CMakeLists.txt | 9 - components/mqtt/test/Kconfig | 14 - components/mqtt/test/test_mqtt.c | 108 ------- components/mqtt/test/test_mqtt5.c | 151 ---------- .../mqtt/test/test_mqtt5_client_broker.c | 285 ------------------ .../mqtt/test/test_mqtt5_client_broker.h | 51 ---- .../mqtt/test/test_mqtt_client_broker.c | 227 -------------- .../mqtt/test/test_mqtt_client_broker.h | 50 --- components/mqtt/test/test_mqtt_connection.c | 144 --------- components/mqtt/test/test_mqtt_connection.h | 18 -- .../host_test => host_test}/CMakeLists.txt | 1 + .../mqtt/host_test => host_test}/README.md | 0 .../main/CMakeLists.txt | 0 .../main/test_mqtt_client.cpp | 5 + host_test/mocks/heap/CMakeLists.txt | 4 + host_test/mocks/heap/heap_mock.c | 11 + .../mocks/include/sys/queue.h | 0 .../sdkconfig.defaults | 0 idf_component.yml | 5 + mqtt_client.c | 5 + 28 files changed, 56 insertions(+), 1408 deletions(-) create mode 100644 CMakeLists.txt rename components/mqtt/Kconfig => Kconfig (96%) delete mode 100644 components/mqtt/CMakeLists.txt delete mode 160000 components/mqtt/esp-mqtt delete mode 100644 components/mqtt/host_test/mocks/config.yaml delete mode 100644 components/mqtt/host_test/mocks/include/freertos/FreeRTOSConfig.h delete mode 100644 components/mqtt/host_test/mocks/include/freertos/portmacro.h delete mode 100644 components/mqtt/host_test/mocks/include/machine/endian.h delete mode 100644 components/mqtt/test/CMakeLists.txt delete mode 100644 components/mqtt/test/Kconfig delete mode 100644 components/mqtt/test/test_mqtt.c delete mode 100644 components/mqtt/test/test_mqtt5.c delete mode 100644 components/mqtt/test/test_mqtt5_client_broker.c delete mode 100644 components/mqtt/test/test_mqtt5_client_broker.h delete mode 100644 components/mqtt/test/test_mqtt_client_broker.c delete mode 100644 components/mqtt/test/test_mqtt_client_broker.h delete mode 100644 components/mqtt/test/test_mqtt_connection.c delete mode 100644 components/mqtt/test/test_mqtt_connection.h rename {components/mqtt/host_test => host_test}/CMakeLists.txt (94%) rename {components/mqtt/host_test => host_test}/README.md (100%) rename {components/mqtt/host_test => host_test}/main/CMakeLists.txt (100%) rename {components/mqtt/host_test => host_test}/main/test_mqtt_client.cpp (97%) create mode 100644 host_test/mocks/heap/CMakeLists.txt create mode 100644 host_test/mocks/heap/heap_mock.c rename {components/mqtt/host_test => host_test}/mocks/include/sys/queue.h (100%) rename {components/mqtt/host_test => host_test}/sdkconfig.defaults (100%) create mode 100644 idf_component.yml diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..1fce7fb --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,18 @@ +set(srcs mqtt_client.c lib/mqtt_msg.c lib/mqtt_outbox.c lib/platform_esp32_idf.c) + +if(CONFIG_MQTT_PROTOCOL_5) + list(APPEND srcs lib/mqtt5_msg.c mqtt5_client.c) +endif() + +list(TRANSFORM srcs PREPEND ${CMAKE_CURRENT_LIST_DIR}/) +idf_component_register(SRCS "${srcs}" + INCLUDE_DIRS ${CMAKE_CURRENT_LIST_DIR}/include + PRIV_INCLUDE_DIRS ${CMAKE_CURRENT_LIST_DIR}/lib/include + REQUIRES esp_event tcp_transport + PRIV_REQUIRES esp_timer http_parser esp_hw_support heap + KCONFIG ${CMAKE_CURRENT_LIST_DIR}/Kconfig + ) +target_compile_options(${COMPONENT_LIB} PRIVATE "-Wno-format") + + + diff --git a/components/mqtt/Kconfig b/Kconfig similarity index 96% rename from components/mqtt/Kconfig rename to Kconfig index 4db6e15..113bac7 100644 --- a/components/mqtt/Kconfig +++ b/Kconfig @@ -124,6 +124,13 @@ menu "ESP-MQTT Configurations" help MQTT task priority. Higher number denotes higher priority. + config MQTT_EVENT_QUEUE_SIZE + int "Number of queued events." + default 1 + depends on MQTT_USE_CUSTOM_CONFIG + help + A value higher than 1 enables multiple queued events. + config MQTT_TASK_CORE_SELECTION_ENABLED bool "Enable MQTT task core selection" help diff --git a/components/mqtt/CMakeLists.txt b/components/mqtt/CMakeLists.txt deleted file mode 100644 index 29aa349..0000000 --- a/components/mqtt/CMakeLists.txt +++ /dev/null @@ -1,13 +0,0 @@ -set(srcs esp-mqtt/mqtt_client.c esp-mqtt/lib/mqtt_msg.c esp-mqtt/lib/mqtt_outbox.c esp-mqtt/lib/platform_esp32_idf.c) - -if(CONFIG_MQTT_PROTOCOL_5) - list(APPEND srcs esp-mqtt/lib/mqtt5_msg.c esp-mqtt/mqtt5_client.c) -endif() - -idf_component_register(SRCS "${srcs}" - INCLUDE_DIRS esp-mqtt/include - PRIV_INCLUDE_DIRS "esp-mqtt/lib/include" - REQUIRES esp_event tcp_transport - PRIV_REQUIRES esp_timer http_parser esp_hw_support - ) -target_compile_options(${COMPONENT_LIB} PRIVATE "-Wno-format") diff --git a/components/mqtt/esp-mqtt b/components/mqtt/esp-mqtt deleted file mode 160000 index ae53d79..0000000 --- a/components/mqtt/esp-mqtt +++ /dev/null @@ -1 +0,0 @@ -Subproject commit ae53d799da294f03ef65c33e88fa33648e638134 diff --git a/components/mqtt/host_test/mocks/config.yaml b/components/mqtt/host_test/mocks/config.yaml deleted file mode 100644 index cdaab2a..0000000 --- a/components/mqtt/host_test/mocks/config.yaml +++ /dev/null @@ -1,25 +0,0 @@ - :cmock: - :plugins: - - expect - - expect_any_args - - return_thru_ptr - - ignore - - array - - callback - :includes_h_pre_orig_header: - - local_FreeRTOS_config.h - - esp_attr.h - - FreeRTOS.h - - net/if.h - :strippables: - - '(?:__attribute__\s*\(+.*?\)+)' - - '(?:vQueueAddToRegistry\s*\(+.*?\)+)' - - '(?:vQueueUnregisterQueue\s*\(+.*?\)+)' - - '(?:pcQueueGetName\s*\(+.*?\)+)' - - '(?:xQueueTakeMutexRecursive\s*\(+.*?\)+)' - - '(?:xQueueGiveMutexRecursive\s*\(+.*?\)+)' - - '(?:vTaskSetThreadLocalStoragePointerAndDelCallback\s*\(+.*?\)+)' - - '(?:esp_log_writev\s*\(+.*?\)+)' - - '(?:esp_restart\s*\(+.*?\)+)' - - '(?:esp_system_abort\s*\(+.*?\)+)' - - PRIVILEGED_FUNCTION diff --git a/components/mqtt/host_test/mocks/include/freertos/FreeRTOSConfig.h b/components/mqtt/host_test/mocks/include/freertos/FreeRTOSConfig.h deleted file mode 100644 index 0492f81..0000000 --- a/components/mqtt/host_test/mocks/include/freertos/FreeRTOSConfig.h +++ /dev/null @@ -1,133 +0,0 @@ -/* - FreeRTOS V8.2.0 - Copyright (C) 2015 Real Time Engineers Ltd. - All rights reserved - - VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION. - - This file is part of the FreeRTOS distribution. - - FreeRTOS is free software; you can redistribute it and/or modify it under - the terms of the GNU General Public License (version 2) as published by the - Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception. - - *************************************************************************** - >>! NOTE: The modification to the GPL is included to allow you to !<< - >>! distribute a combined work that includes FreeRTOS without being !<< - >>! obliged to provide the source code for proprietary components !<< - >>! outside of the FreeRTOS kernel. !<< - *************************************************************************** - - FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY - WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS - FOR A PARTICULAR PURPOSE. Full license text is available on the following - link: http://www.freertos.org/a00114.html - - *************************************************************************** - * * - * FreeRTOS provides completely free yet professionally developed, * - * robust, strictly quality controlled, supported, and cross * - * platform software that is more than just the market leader, it * - * is the industry's de facto standard. * - * * - * Help yourself get started quickly while simultaneously helping * - * to support the FreeRTOS project by purchasing a FreeRTOS * - * tutorial book, reference manual, or both: * - * http://www.FreeRTOS.org/Documentation * - * * - *************************************************************************** - - http://www.FreeRTOS.org/FAQHelp.html - Having a problem? Start by reading - the FAQ page "My application does not run, what could be wrong?". Have you - defined configASSERT()? - - http://www.FreeRTOS.org/support - In return for receiving this top quality - embedded software for free we request you assist our global community by - participating in the support forum. - - http://www.FreeRTOS.org/training - Investing in training allows your team to - be as productive as possible as early as possible. Now you can receive - FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers - Ltd, and the world's leading authority on the world's leading RTOS. - - http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products, - including FreeRTOS+Trace - an indispensable productivity tool, a DOS - compatible FAT file system, and our tiny thread aware UDP/IP stack. - - http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate. - Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS. - - http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High - Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS - licenses offer ticketed support, indemnification and commercial middleware. - - http://www.SafeRTOS.com - High Integrity Systems also provide a safety - engineered and independently SIL3 certified version for use in safety and - mission critical applications that require provable dependability. - - 1 tab == 4 spaces! -*/ - -#ifndef FREERTOS_CONFIG_H -#define FREERTOS_CONFIG_H - -#include "esp_attr.h" - -/*----------------------------------------------------------- - * Application specific definitions. - * - * These definitions should be adjusted for your particular hardware and - * application requirements. - * - * THESE PARAMETERS ARE DESCRIBED WITHIN THE 'CONFIGURATION' SECTION OF THE - * FreeRTOS API DOCUMENTATION AVAILABLE ON THE FreeRTOS.org WEB SITE. - * - * See http://www.freertos.org/a00110.html. - *----------------------------------------------------------*/ - -#define configUSE_PREEMPTION 1 -#define configUSE_IDLE_HOOK 1 -#define configUSE_TICK_HOOK 1 -#define configTICK_RATE_HZ ((TickType_t)1000) -#define configMINIMAL_STACK_SIZE ((unsigned short)256) /* This can be made smaller if required. */ -#define configTOTAL_HEAP_SIZE ((size_t)(32 * 1024)) -#define configMAX_TASK_NAME_LEN (16) -#define configUSE_TRACE_FACILITY 1 -#define configUSE_16_BIT_TICKS 1 -#define configIDLE_SHOULD_YIELD 1 -#define configUSE_CO_ROUTINES 1 -#define configUSE_MUTEXES 1 -#define configUSE_COUNTING_SEMAPHORES 1 -#define configUSE_ALTERNATIVE_API 0 -#define configUSE_RECURSIVE_MUTEXES 1 -#define configCHECK_FOR_STACK_OVERFLOW 0 /* Do not use this option on the PC port. */ -#define configUSE_APPLICATION_TASK_TAG 1 -#define configQUEUE_REGISTRY_SIZE 0 - -#define configMAX_PRIORITIES (10) -#define configMAX_CO_ROUTINE_PRIORITIES (2) - -/* Set the following definitions to 1 to include the API function, or zero -to exclude the API function. */ - -#define INCLUDE_vTaskPrioritySet 1 -#define INCLUDE_uxTaskPriorityGet 1 -#define INCLUDE_vTaskDelete 1 -#define INCLUDE_vTaskCleanUpResources 1 -#define INCLUDE_vTaskSuspend 1 -#define INCLUDE_vTaskDelayUntil 1 -#define INCLUDE_vTaskDelay 1 -#define INCLUDE_uxTaskGetStackHighWaterMark 0 /* Do not use this option on the PC port. */ - -/* This demo makes use of one or more example stats formatting functions. These -format the raw data provided by the uxTaskGetSystemState() function in to human -readable ASCII form. See the notes in the implementation of vTaskList() within -FreeRTOS/Source/tasks.c for limitations. */ -#define configUSE_STATS_FORMATTING_FUNCTIONS 1 - -/* An example "task switched in" hook macro definition. */ -#define traceTASK_SWITCHED_IN() xTaskCallApplicationTaskHook(NULL, (void*)0xabcd) - -extern void vMainQueueSendPassed(void); -#define traceQUEUE_SEND(pxQueue) vMainQueueSendPassed() - -#endif /* FREERTOS_CONFIG_H */ diff --git a/components/mqtt/host_test/mocks/include/freertos/portmacro.h b/components/mqtt/host_test/mocks/include/freertos/portmacro.h deleted file mode 100644 index 23c3d6e..0000000 --- a/components/mqtt/host_test/mocks/include/freertos/portmacro.h +++ /dev/null @@ -1,177 +0,0 @@ -/* - * FreeRTOS Kernel V10.4.3 - * Copyright (C) 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. - * - * Permission is hereby granted, free of charge, to any person obtaining a copy of - * this software and associated documentation files (the "Software"), to deal in - * the Software without restriction, including without limitation the rights to - * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of - * the Software, and to permit persons to whom the Software is furnished to do so, - * subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS - * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR - * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER - * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN - * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - * - * http://www.FreeRTOS.org - * http://aws.amazon.com/freertos - * - * 1 tab == 4 spaces! - */ -#ifndef PORTMACRO_H -#define PORTMACRO_H - -#include -#include -#include -#include -#include - -#ifdef __cplusplus -extern "C" { -#endif - -#ifndef __ASSEMBLER__ - -/*----------------------------------------------------------- - * Port specific definitions. - * - * The settings in this file configure FreeRTOS correctly for the - * given hardware and compiler. - * - * These settings should not be altered. - *----------------------------------------------------------- - */ - -/* Type definitions. */ -#define portCHAR uint8_t -#define portFLOAT float -#define portDOUBLE double -#define portLONG int32_t -#define portSHORT int16_t -#define portSTACK_TYPE uint8_t -#define portBASE_TYPE int -// interrupt module will mask interrupt with priority less than threshold -#define RVHAL_EXCM_LEVEL 4 - -typedef portSTACK_TYPE StackType_t; -typedef portBASE_TYPE BaseType_t; -typedef unsigned portBASE_TYPE UBaseType_t; - -#if (configUSE_16_BIT_TICKS == 1) -typedef uint16_t TickType_t; -#define portMAX_DELAY (TickType_t)0xffff -#else -typedef uint32_t TickType_t; -#define portMAX_DELAY (TickType_t)0xffffffffUL -#endif -/*------------------------------------------------------*/ - -/* Architecture specifics. */ -#define portSTACK_GROWTH (-1) -#define portTICK_PERIOD_MS ((TickType_t)(1000 / configTICK_RATE_HZ)) -#define portBYTE_ALIGNMENT 16 -/*-----------------------------------------------------------*/ - -#define portCRITICAL_NESTING_IN_TCB 0 - -/* - * Send an interrupt to another core in order to make the task running - * on it yield for a higher-priority task. - */ -void vPortYieldOtherCore(BaseType_t coreid); - -/* - Callback to set a watchpoint on the end of the stack. Called every context switch to change the stack - watchpoint around. - */ -void vPortSetStackWatchpoint(void *pxStackStart); - -/* - * Returns true if the current core is in ISR context; low prio ISR, med prio ISR or timer tick ISR. High prio ISRs - * aren't detected here, but they normally cannot call C code, so that should not be an issue anyway. - */ -BaseType_t xPortInIsrContext(void); - -/* - * This function will be called in High prio ISRs. Returns true if the current core was in ISR context - * before calling into high prio ISR context. - */ -BaseType_t xPortInterruptedFromISRContext(void); - -/* "mux" data structure (spinlock) */ -typedef struct { - /* owner field values: - * 0 - Uninitialized (invalid) - * portMUX_FREE_VAL - Mux is free, can be locked by either CPU - * CORE_ID_REGVAL_PRO / CORE_ID_REGVAL_APP - Mux is locked to the particular core - * - * - * Any value other than portMUX_FREE_VAL, CORE_ID_REGVAL_PRO, CORE_ID_REGVAL_APP indicates corruption - */ - uint32_t owner; - /* count field: - * If mux is unlocked, count should be zero. - * If mux is locked, count is non-zero & represents the number of recursive locks on the mux. - */ - uint32_t count; -} portMUX_TYPE; - -#define portMUX_FREE_VAL SPINLOCK_FREE - -/* Special constants for vPortCPUAcquireMutexTimeout() */ -#define portMUX_NO_TIMEOUT SPINLOCK_WAIT_FOREVER /* When passed for 'timeout_cycles', spin forever if necessary */ -#define portMUX_TRY_LOCK SPINLOCK_NO_WAIT /* Try to acquire the spinlock a single time only */ - -// Keep this in sync with the portMUX_TYPE struct definition please. -#define portMUX_INITIALIZER_UNLOCKED \ - { .owner = portMUX_FREE_VAL, .count = 0, } - -/* Scheduler utilities. */ -extern void vPortYield(void); -extern void vPortYieldFromISR(void); - -#define portYIELD() vPortYield() -#define portYIELD_FROM_ISR() vPortYieldFromISR() - -/* Yielding within an API call (when interrupts are off), means the yield should be delayed - until interrupts are re-enabled. - To do this, we use the "cross-core" interrupt as a trigger to yield on this core when interrupts are re-enabled.This - is the same interrupt & code path which is used to trigger a yield between CPUs, although in this case the yield is - happening on the same CPU. -*/ -#define portYIELD_WITHIN_API() portYIELD() -/*-----------------------------------------------------------*/ - -/* Critical section management. */ -extern int vPortSetInterruptMask(void); -extern void vPortClearInterruptMask(int); - -extern void vPortEnterCritical(void); -extern void vPortExitCritical(void); - -/* Task function macros as described on the FreeRTOS.org WEB site. */ -#define portTASK_FUNCTION_PROTO(vFunction, pvParameters) void vFunction(void* pvParameters) -#define portTASK_FUNCTION(vFunction, pvParameters) void vFunction(void* pvParameters) - -void vApplicationSleep(TickType_t xExpectedIdleTime); -#define portSUPPRESS_TICKS_AND_SLEEP(idleTime) vApplicationSleep(idleTime) - -#define portNOP() //__asm volatile ( " nop " ) - -#define portVALID_TCB_MEM(ptr) // esp_ptr_byte_accessible(ptr) -#define portVALID_STACK_MEM(ptr) // esp_ptr_byte_accessible(ptr) - -#endif //__ASSEMBLER__ - -#ifdef __cplusplus -} -#endif - -#endif /* PORTMACRO_H */ diff --git a/components/mqtt/host_test/mocks/include/machine/endian.h b/components/mqtt/host_test/mocks/include/machine/endian.h deleted file mode 100644 index 228316d..0000000 --- a/components/mqtt/host_test/mocks/include/machine/endian.h +++ /dev/null @@ -1,2 +0,0 @@ -#pragma once -#include_next diff --git a/components/mqtt/test/CMakeLists.txt b/components/mqtt/test/CMakeLists.txt deleted file mode 100644 index 616d362..0000000 --- a/components/mqtt/test/CMakeLists.txt +++ /dev/null @@ -1,9 +0,0 @@ -set(srcs test_mqtt_client_broker.c test_mqtt_connection.c test_mqtt.c) - -if(CONFIG_MQTT_PROTOCOL_5) - list(APPEND srcs test_mqtt5_client_broker.c test_mqtt5.c) -endif() - -idf_component_register(SRCS "${srcs}" - PRIV_REQUIRES cmock test_utils mqtt nvs_flash app_update esp_eth esp_netif) - target_compile_options(${COMPONENT_LIB} PRIVATE "-Wno-format") diff --git a/components/mqtt/test/Kconfig b/components/mqtt/test/Kconfig deleted file mode 100644 index 30b2c10..0000000 --- a/components/mqtt/test/Kconfig +++ /dev/null @@ -1,14 +0,0 @@ -menu "ESP-MQTT Unit Test Config" - - config MQTT_TEST_BROKER_URI - string "URI of the test broker" - default "mqtt://mqtt.eclipseprojects.io" - help - URL of an mqtt broker which this test connects to. - - config MQTT5_TEST_BROKER_URI - string "URI of the test broker" - default "mqtt://mqtt.eclipseprojects.io" - help - URL of an mqtt broker which this test connects to. -endmenu diff --git a/components/mqtt/test/test_mqtt.c b/components/mqtt/test/test_mqtt.c deleted file mode 100644 index d997bce..0000000 --- a/components/mqtt/test/test_mqtt.c +++ /dev/null @@ -1,108 +0,0 @@ -/* - * SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD - * - * SPDX-License-Identifier: Unlicense OR CC0-1.0 - * - * This test code is in the Public Domain (or CC0 licensed, at your option.) - * - * Unless required by applicable law or agreed to in writing, this - * software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. - */ - -#include -#include "freertos/FreeRTOS.h" -#include "freertos/event_groups.h" -#include "unity.h" -#include "test_utils.h" -#include "memory_checks.h" -#include "mqtt_client.h" -#include "nvs_flash.h" -#include "esp_ota_ops.h" -#include "sdkconfig.h" -#include "test_mqtt_client_broker.h" -#include "test_mqtt_connection.h" -#include "esp_mac.h" - -static void test_leak_setup(const char * file, long line) -{ - uint8_t mac[6]; - struct timeval te; - gettimeofday(&te, NULL); // get current time - esp_read_mac(mac, ESP_MAC_WIFI_STA); - printf("%s:%ld: time=%jd.%lds, mac:" MACSTR "\n", file, line, (intmax_t)te.tv_sec, te.tv_usec, MAC2STR(mac)); - test_utils_record_free_mem(); -} - -TEST_CASE("mqtt init with invalid url", "[mqtt][leaks=0]") -{ - test_leak_setup(__FILE__, __LINE__); - const esp_mqtt_client_config_t mqtt_cfg = { - .broker.address.uri = "INVALID", - }; - esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg); - TEST_ASSERT_EQUAL(NULL, client ); -} - -TEST_CASE("mqtt init and deinit", "[mqtt][leaks=0]") -{ - test_leak_setup(__FILE__, __LINE__); - const esp_mqtt_client_config_t mqtt_cfg = { - // no connection takes place, but the uri has to be valid for init() to succeed - .broker.address.uri = "mqtts://localhost:8883", - }; - esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg); - TEST_ASSERT_NOT_EQUAL(NULL, client ); - esp_mqtt_client_destroy(client); -} - -static const char* this_bin_addr(void) -{ - spi_flash_mmap_handle_t out_handle; - const void *binary_address; - const esp_partition_t* partition = esp_ota_get_running_partition(); - esp_partition_mmap(partition, 0, partition->size, SPI_FLASH_MMAP_DATA, &binary_address, &out_handle); - return binary_address; -} - -TEST_CASE("mqtt enqueue and destroy outbox", "[mqtt][leaks=0]") -{ - const char * bin_addr = this_bin_addr(); - test_leak_setup(__FILE__, __LINE__); - const int messages = 20; - const int size = 2000; - const esp_mqtt_client_config_t mqtt_cfg = { - // no connection takes place, but the uri has to be valid for init() to succeed - .broker.address.uri = "mqtts://localhost:8883", - }; - esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg); - TEST_ASSERT_NOT_EQUAL(NULL, client ); - int bytes_before = esp_get_free_heap_size(); - for (int i=0; i -#include "freertos/FreeRTOS.h" -#include "freertos/event_groups.h" -#include "unity.h" -#include "test_utils.h" -#include "memory_checks.h" -#include "mqtt_client.h" -#include "nvs_flash.h" -#include "esp_ota_ops.h" -#include "sdkconfig.h" -#include "test_mqtt5_client_broker.h" -#include "test_mqtt_connection.h" -#include "esp_mac.h" - -static esp_mqtt5_user_property_item_t user_property_arr[3] = { - {"board", "esp32"}, - {"u", "user"}, - {"p", "password"} -}; - -static void test_leak_setup(const char * file, long line) -{ - uint8_t mac[6]; - struct timeval te; - gettimeofday(&te, NULL); // get current time - esp_read_mac(mac, ESP_MAC_WIFI_STA); - printf("%s:%ld: time=%ld.%lds, mac:" MACSTR "\n", file, line, te.tv_sec, te.tv_usec, MAC2STR(mac)); - test_utils_record_free_mem(); -} - -TEST_CASE("mqtt5 init with invalid url", "[mqtt5][leaks=0]") -{ - test_leak_setup(__FILE__, __LINE__); - const esp_mqtt_client_config_t mqtt5_cfg = { - .broker.address.uri = "INVALID", - .session.protocol_ver = MQTT_PROTOCOL_V_5, - }; - esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt5_cfg); - TEST_ASSERT_EQUAL(NULL, client ); -} - -TEST_CASE("mqtt5 init and deinit", "[mqtt5][leaks=0]") -{ - test_leak_setup(__FILE__, __LINE__); - const esp_mqtt_client_config_t mqtt5_cfg = { - // no connection takes place, but the uri has to be valid for init() to succeed - .broker.address.uri = "mqtts://localhost:8883", - .session.protocol_ver = MQTT_PROTOCOL_V_5, - .credentials.username = "123", - .credentials.authentication.password = "456", - .session.last_will.topic = "/topic/will", - .session.last_will.msg = "i will leave", - .session.last_will.msg_len = 12, - .session.last_will.qos = 1, - .session.last_will.retain = true, - }; - esp_mqtt5_connection_property_config_t connect_property = { - .session_expiry_interval = 10, - .maximum_packet_size = 1024, - .receive_maximum = 65535, - .topic_alias_maximum = 2, - .request_resp_info = true, - .request_problem_info = true, - .will_delay_interval = 10, - .payload_format_indicator = true, - .message_expiry_interval = 10, - .content_type = "json", - .response_topic = "/test/response", - .correlation_data = "123456", - .correlation_data_len = 6, - }; - - esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt5_cfg); - esp_mqtt5_client_set_user_property(&connect_property.user_property, user_property_arr, 3); - esp_mqtt5_client_set_user_property(&connect_property.will_user_property, user_property_arr, 3); - esp_mqtt5_client_set_connect_property(client, &connect_property); - esp_mqtt5_client_delete_user_property(connect_property.user_property); - esp_mqtt5_client_delete_user_property(connect_property.will_user_property); - TEST_ASSERT_NOT_EQUAL(NULL, client ); - esp_mqtt_client_destroy(client); -} - -static const char* this_bin_addr(void) -{ - spi_flash_mmap_handle_t out_handle; - const void *binary_address; - const esp_partition_t* partition = esp_ota_get_running_partition(); - esp_partition_mmap(partition, 0, partition->size, SPI_FLASH_MMAP_DATA, &binary_address, &out_handle); - return binary_address; -} - -TEST_CASE("mqtt5 enqueue and destroy outbox", "[mqtt5][leaks=0]") -{ - const char * bin_addr = this_bin_addr(); - test_leak_setup(__FILE__, __LINE__); - const int messages = 20; - const int size = 2000; - const esp_mqtt_client_config_t mqtt5_cfg = { - // no connection takes place, but the uri has to be valid for init() to succeed - .broker.address.uri = "mqtts://localhost:8883", - .session.protocol_ver = MQTT_PROTOCOL_V_5, - }; - esp_mqtt5_publish_property_config_t publish_property = { - .payload_format_indicator = 1, - .message_expiry_interval = 1000, - .topic_alias = 0, - .response_topic = "/topic/test/response", - .correlation_data = "123456", - .correlation_data_len = 6, - .content_type = "json", - }; - esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt5_cfg); - TEST_ASSERT_NOT_EQUAL(NULL, client ); - int bytes_before = esp_get_free_heap_size(); - for (int i = 0; i < messages; i ++) { - esp_mqtt5_client_set_user_property(&publish_property.user_property, user_property_arr, 3); - esp_mqtt5_client_set_publish_property(client, &publish_property); - esp_mqtt_client_publish(client, "test", bin_addr, size, 1, 0); - esp_mqtt5_client_delete_user_property(publish_property.user_property); - publish_property.user_property = NULL; - } - int bytes_after = esp_get_free_heap_size(); - // check that outbox allocated all messages on heap - TEST_ASSERT_GREATER_OR_EQUAL(messages*size, bytes_before - bytes_after); - - esp_mqtt_client_destroy(client); -} - -#if SOC_EMAC_SUPPORTED -/** - * This test cases uses ethernet kit, so build and use it only if EMAC supported - */ -TEST_CASE("mqtt5 broker tests", "[mqtt5][test_env=UT_T2_Ethernet]") -{ - test_case_uses_tcpip(); - connect_test_fixture_setup(); - - RUN_MQTT5_BROKER_TEST(mqtt5_connect_disconnect); - RUN_MQTT5_BROKER_TEST(mqtt5_subscribe_publish); - RUN_MQTT5_BROKER_TEST(mqtt5_lwt_clean_disconnect); - RUN_MQTT5_BROKER_TEST(mqtt5_subscribe_payload); - - connect_test_fixture_teardown(); -} -#endif // SOC_EMAC_SUPPORTED diff --git a/components/mqtt/test/test_mqtt5_client_broker.c b/components/mqtt/test/test_mqtt5_client_broker.c deleted file mode 100644 index ced44e9..0000000 --- a/components/mqtt/test/test_mqtt5_client_broker.c +++ /dev/null @@ -1,285 +0,0 @@ -/* - * SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD - * - * SPDX-License-Identifier: Apache-2.0 - */ - -#include "freertos/FreeRTOS.h" -#include "freertos/event_groups.h" -#include "mqtt_client.h" -#include "esp_log.h" -#include "esp_mac.h" - -#define WAIT_FOR_EVENT(event) \ - TEST_ASSERT_TRUE(xEventGroupWaitBits(s_event_group, event, pdTRUE, pdTRUE, pdMS_TO_TICKS(COMMON_OPERATION_TIMEOUT)) & event); - -#define TEST_ASSERT_TRUE(condition) TEST_ASSERT_TRUE_LINE(condition, __LINE__) -#define TEST_ASSERT_TRUE_LINE(condition, line) \ - do { \ - if (!(condition)) { \ - ESP_LOGE("test_mqtt5_client_broker.c", \ - "Assertion failed in line %d", line); \ - return false; \ - } \ - } while(0) - - -static const int COMMON_OPERATION_TIMEOUT = 10000; -static const int CONNECT_BIT = BIT0; -static const int DISCONNECT_BIT = BIT1; -static const int DATA_BIT = BIT2; - -static EventGroupHandle_t s_event_group; - -static esp_mqtt5_user_property_item_t user_property_arr[3] = { - {"board", "esp32"}, - {"u", "user"}, - {"p", "password"} -}; - -static char* append_mac(const char* string) -{ - uint8_t mac[6]; - char *id_string = NULL; - esp_read_mac(mac, ESP_MAC_WIFI_STA); - asprintf(&id_string, "%s_%02x%02X%02X", string, mac[3], mac[4], mac[5]); - return id_string; -} - -static void mqtt5_data_handler_qos(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data) -{ - if (event_id == MQTT_EVENT_DATA) { - esp_mqtt_event_handle_t event = event_data; - int * qos = handler_args; - *qos = event->qos; - xEventGroupSetBits(s_event_group, DATA_BIT); - } -} - -static void mqtt5_data_handler_lwt(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data) -{ - if (event_id == MQTT_EVENT_DATA) { - esp_mqtt_event_handle_t event = event_data; - ESP_LOGI("mqtt-lwt", "MQTT_EVENT_DATA"); - ESP_LOGI("mqtt-lwt", "TOPIC=%.*s", event->topic_len, event->topic); - ESP_LOGI("mqtt-lwt", "DATA=%.*s", event->data_len, event->data); - if (strncmp(event->data, "no-lwt", event->data_len) == 0) { - // no lwt, just to indicate the test has finished - xEventGroupSetBits(s_event_group, DATA_BIT); - } else { - // count up any potential lwt message - int * count = handler_args; - *count = *count + 1; - ESP_LOGE("mqtt5-lwt", "count=%d", *count); - } - } -} - -static void mqtt5_data_handler_subscribe(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data) -{ - if (event_id == MQTT_EVENT_SUBSCRIBED) { - esp_mqtt_event_handle_t event = event_data; - ESP_LOGI("mqtt5-subscribe", "MQTT_EVENT_SUBSCRIBED, data size=%d", event->data_len); - int * sub_payload = handler_args; - if (event->data_len == 1) { - ESP_LOGI("mqtt5-subscribe", "DATA=%d", *(uint8_t*)event->data); - *sub_payload = *(uint8_t*)event->data; - } - xEventGroupSetBits(s_event_group, DATA_BIT); - } -} - - -static void mqtt5_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data) -{ - switch ((esp_mqtt_event_id_t)event_id) { - case MQTT_EVENT_CONNECTED: - xEventGroupSetBits(s_event_group, CONNECT_BIT); - break; - - case MQTT_EVENT_DISCONNECTED: - xEventGroupSetBits(s_event_group, DISCONNECT_BIT); - break; - default: - break; - } -} - -bool mqtt5_connect_disconnect(void) -{ - const esp_mqtt_client_config_t mqtt5_cfg = { - .broker.address.uri = CONFIG_MQTT5_TEST_BROKER_URI, - .network.disable_auto_reconnect = true, - .session.protocol_ver = MQTT_PROTOCOL_V_5, - }; - esp_mqtt5_connection_property_config_t connect_property = { - .session_expiry_interval = 10, - .maximum_packet_size = 1024, - .receive_maximum = 65535, - .topic_alias_maximum = 2, - .request_resp_info = true, - .request_problem_info = true, - }; - esp_mqtt5_disconnect_property_config_t disconnect_property = { - .session_expiry_interval = 10, - .disconnect_reason = 0, - }; - s_event_group = xEventGroupCreate(); - esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt5_cfg); - TEST_ASSERT_TRUE(NULL != client ); - esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt5_event_handler, NULL); - TEST_ASSERT_TRUE(ESP_OK == esp_mqtt5_client_set_user_property(&connect_property.user_property, user_property_arr, 3)); - TEST_ASSERT_TRUE(ESP_OK == esp_mqtt5_client_set_connect_property(client, &connect_property)); - esp_mqtt5_client_delete_user_property(connect_property.user_property); - TEST_ASSERT_TRUE(ESP_OK == esp_mqtt_client_start(client)); - WAIT_FOR_EVENT(CONNECT_BIT); - TEST_ASSERT_TRUE(ESP_OK == esp_mqtt5_client_set_user_property(&disconnect_property.user_property, user_property_arr, 3)); - TEST_ASSERT_TRUE(ESP_OK == esp_mqtt5_client_set_disconnect_property(client, &disconnect_property)); - esp_mqtt5_client_delete_user_property(disconnect_property.user_property); - esp_mqtt_client_disconnect(client); - WAIT_FOR_EVENT(DISCONNECT_BIT); - esp_mqtt_client_reconnect(client); - WAIT_FOR_EVENT(CONNECT_BIT); - esp_mqtt_client_destroy(client); - vEventGroupDelete(s_event_group); - return true; -} - -bool mqtt5_subscribe_publish(void) -{ - const esp_mqtt_client_config_t mqtt5_cfg = { - .broker.address.uri = CONFIG_MQTT5_TEST_BROKER_URI, - .session.protocol_ver = MQTT_PROTOCOL_V_5, - }; - esp_mqtt5_publish_property_config_t publish_property = { - .payload_format_indicator = 1, - .message_expiry_interval = 1000, - .topic_alias = 1, - .response_topic = "/topic/test/response", - .correlation_data = "123456", - .correlation_data_len = 6, - .content_type = "json", - }; - esp_mqtt5_subscribe_property_config_t subscribe_property = { - .subscribe_id = 25555, - .no_local_flag = false, - .retain_as_published_flag = true, - .retain_handle = 0, - }; - char* topic = append_mac("topic"); - TEST_ASSERT_TRUE(NULL != topic); - s_event_group = xEventGroupCreate(); - esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt5_cfg); - TEST_ASSERT_TRUE(NULL != client ); - esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt5_event_handler, NULL); - TEST_ASSERT_TRUE(ESP_OK == esp_mqtt_client_start(client)); - WAIT_FOR_EVENT(CONNECT_BIT); - int qos = -1; - esp_mqtt_client_register_event(client, MQTT_EVENT_DATA, mqtt5_data_handler_qos, &qos); - TEST_ASSERT_TRUE(ESP_OK == esp_mqtt5_client_set_subscribe_property(client, &subscribe_property)); - TEST_ASSERT_TRUE(esp_mqtt_client_subscribe(client, topic, 2) != -1); - TEST_ASSERT_TRUE(ESP_OK == esp_mqtt5_client_set_publish_property(client, &publish_property)); - TEST_ASSERT_TRUE(esp_mqtt_client_publish(client, topic, "message", 0, 2, 0) != -1); - WAIT_FOR_EVENT(DATA_BIT); - TEST_ASSERT_TRUE(qos == 2); - TEST_ASSERT_TRUE(esp_mqtt_client_publish(client, topic, "message", 0, 1, 0) != -1); - WAIT_FOR_EVENT(DATA_BIT); - TEST_ASSERT_TRUE(qos == 1); - esp_mqtt_client_destroy(client); - vEventGroupDelete(s_event_group); - free(topic); - return true; -} - -bool mqtt5_lwt_clean_disconnect(void) -{ - char* lwt = append_mac("lwt"); - TEST_ASSERT_TRUE(lwt); - const esp_mqtt_client_config_t mqtt5_cfg1 = { - .broker.address.uri = CONFIG_MQTT5_TEST_BROKER_URI, - .credentials.set_null_client_id = true, - .session.last_will.topic = lwt, - .session.last_will.msg = "lwt_msg", - .session.protocol_ver = MQTT_PROTOCOL_V_5, - }; - const esp_mqtt_client_config_t mqtt5_cfg2 = { - .broker.address.uri = CONFIG_MQTT5_TEST_BROKER_URI, - .credentials.set_null_client_id = true, - .session.last_will.topic = lwt, - .session.last_will.msg = "lwt_msg", - .session.protocol_ver = MQTT_PROTOCOL_V_5, - }; - esp_mqtt5_connection_property_config_t connect_property = { - .will_delay_interval = 10, - .payload_format_indicator = true, - .message_expiry_interval = 10, - .content_type = "json", - .response_topic = "/test/response", - .correlation_data = "123456", - .correlation_data_len = 6, - }; - s_event_group = xEventGroupCreate(); - - esp_mqtt_client_handle_t client1 = esp_mqtt_client_init(&mqtt5_cfg1); - esp_mqtt_client_handle_t client2 = esp_mqtt_client_init(&mqtt5_cfg2); - TEST_ASSERT_TRUE(NULL != client1 && NULL != client2 ); - esp_mqtt_client_register_event(client1, ESP_EVENT_ANY_ID, mqtt5_event_handler, NULL); - esp_mqtt_client_register_event(client2, ESP_EVENT_ANY_ID, mqtt5_event_handler, NULL); - TEST_ASSERT_TRUE(ESP_OK == esp_mqtt5_client_set_connect_property(client1, &connect_property)); - TEST_ASSERT_TRUE(ESP_OK == esp_mqtt5_client_set_connect_property(client2, &connect_property)); - TEST_ASSERT_TRUE(esp_mqtt_client_start(client1) == ESP_OK); - WAIT_FOR_EVENT(CONNECT_BIT); - TEST_ASSERT_TRUE(esp_mqtt_client_start(client2) == ESP_OK); - WAIT_FOR_EVENT(CONNECT_BIT); - int counter = 0; - esp_mqtt_client_register_event(client1, MQTT_EVENT_DATA, mqtt5_data_handler_lwt, &counter); - esp_mqtt_client_register_event(client2, MQTT_EVENT_DATA, mqtt5_data_handler_lwt, &counter); - TEST_ASSERT_TRUE(esp_mqtt_client_subscribe(client1, lwt, 0) != -1); - TEST_ASSERT_TRUE(esp_mqtt_client_subscribe(client2, lwt, 0) != -1); - esp_mqtt_client_disconnect(client1); - WAIT_FOR_EVENT(DISCONNECT_BIT); - esp_mqtt_client_reconnect(client1); - WAIT_FOR_EVENT(CONNECT_BIT); - TEST_ASSERT_TRUE(esp_mqtt_client_subscribe(client1, lwt, 0) != -1); - esp_mqtt_client_stop(client2); - esp_mqtt_client_start(client2); - WAIT_FOR_EVENT(CONNECT_BIT); - TEST_ASSERT_TRUE(esp_mqtt_client_subscribe(client2, lwt, 0) != -1); - TEST_ASSERT_TRUE(esp_mqtt_client_publish(client1, lwt, "no-lwt", 0, 0, 0) != -1); - WAIT_FOR_EVENT(DATA_BIT); - TEST_ASSERT_TRUE(counter == 0); - esp_mqtt_client_destroy(client1); - esp_mqtt_client_destroy(client2); - vEventGroupDelete(s_event_group); - free(lwt); - return true; -} - -bool mqtt5_subscribe_payload(void) -{ - const esp_mqtt_client_config_t mqtt5_cfg = { - .broker.address.uri = CONFIG_MQTT5_TEST_BROKER_URI, - .network.disable_auto_reconnect = true, - .session.protocol_ver = MQTT_PROTOCOL_V_5, - }; - char* topic = append_mac("topic"); - TEST_ASSERT_TRUE(NULL != topic); - s_event_group = xEventGroupCreate(); - esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt5_cfg); - TEST_ASSERT_TRUE(NULL != client ); - esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt5_event_handler, NULL); - TEST_ASSERT_TRUE(ESP_OK == esp_mqtt_client_start(client)); - WAIT_FOR_EVENT(CONNECT_BIT); - int qos_payload = -1; - esp_mqtt_client_register_event(client, MQTT_EVENT_SUBSCRIBED, mqtt5_data_handler_subscribe, &qos_payload); - TEST_ASSERT_TRUE(esp_mqtt_client_subscribe(client, topic, 2) != -1); - WAIT_FOR_EVENT(DATA_BIT); - TEST_ASSERT_TRUE(qos_payload == 2); - TEST_ASSERT_TRUE(esp_mqtt_client_subscribe(client, topic, 0) != -1); - WAIT_FOR_EVENT(DATA_BIT); - TEST_ASSERT_TRUE(qos_payload == 0); - esp_mqtt_client_destroy(client); - vEventGroupDelete(s_event_group); - free(topic); - return true; -} diff --git a/components/mqtt/test/test_mqtt5_client_broker.h b/components/mqtt/test/test_mqtt5_client_broker.h deleted file mode 100644 index 52b6ab8..0000000 --- a/components/mqtt/test/test_mqtt5_client_broker.h +++ /dev/null @@ -1,51 +0,0 @@ -/* - * SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD - * - * SPDX-License-Identifier: Apache-2.0 - */ - -#pragma once -#include "esp_log.h" - -/** - * @brief MQTT5 client-broker tests are not implemented as separate test cases - * due to time consuming connection setup/teardown. - * This utility macro is used to run functional cases as MQTT tests - * and evaluate as separate assertions in one "mqtt5 broker tests" test case. - */ -#define RUN_MQTT5_BROKER_TEST(test_name) \ - do { \ - ESP_LOGI("mqtt5_test", "Running test:" #test_name "()"); \ - TEST_ASSERT_TRUE_MESSAGE(test_name(), "Mqtt5 test failed: " #test_name "() "); \ - ESP_LOGI("mqtt5_test", "Test:" #test_name "() passed "); \ - } while(0) - - -/** - * @brief This module contains mqtt5 test cases interacting the client with a (real) broker - */ - -/** - * @brief The client subscribes and publishes on the same topic - * and verifies the received published qos in the event - */ -bool mqtt5_subscribe_publish(void); - -/** - * @brief The client connects, disconnects and reconnects. - * Tests basic client state transitions - */ -bool mqtt5_connect_disconnect(void); - -/** - * @brief Two clients with defined lwt connect and subscribe to lwt topic. - * This test verifies that no lwt is send when each of the client disconnects. - * (we expect a clean disconnection, so no last-will being sent) - */ -bool mqtt5_lwt_clean_disconnect(void); - -/** - * @brief The client subscribes to a topic with certain qos - * and verifies the qos in SUBACK message from the broker. - */ -bool mqtt5_subscribe_payload(void); diff --git a/components/mqtt/test/test_mqtt_client_broker.c b/components/mqtt/test/test_mqtt_client_broker.c deleted file mode 100644 index 05fca43..0000000 --- a/components/mqtt/test/test_mqtt_client_broker.c +++ /dev/null @@ -1,227 +0,0 @@ -/* - * SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD - * - * SPDX-License-Identifier: Apache-2.0 - */ -#include "freertos/FreeRTOS.h" -#include "freertos/event_groups.h" -#include "mqtt_client.h" -#include "esp_log.h" -#include "esp_mac.h" - -#define WAIT_FOR_EVENT(event) \ - TEST_ASSERT_TRUE(xEventGroupWaitBits(s_event_group, event, pdTRUE, pdTRUE, pdMS_TO_TICKS(COMMON_OPERATION_TIMEOUT)) & event); - -#define TEST_ASSERT_TRUE(condition) TEST_ASSERT_TRUE_LINE(condition, __LINE__) -#define TEST_ASSERT_TRUE_LINE(condition, line) \ - do { \ - if (!(condition)) { \ - ESP_LOGE("test_mqtt_client_broker.c", \ - "Assertion failed in line %d", line); \ - return false; \ - } \ - } while(0) - - -static const int COMMON_OPERATION_TIMEOUT = 10000; -static const int CONNECT_BIT = BIT0; -static const int DISCONNECT_BIT = BIT1; -static const int DATA_BIT = BIT2; - -static EventGroupHandle_t s_event_group; - -static char* append_mac(const char* string) -{ - uint8_t mac[6]; - char *id_string = NULL; - esp_read_mac(mac, ESP_MAC_WIFI_STA); - asprintf(&id_string, "%s_%02x%02X%02X", string, mac[3], mac[4], mac[5]); - return id_string; -} - -static void mqtt_data_handler_qos(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data) -{ - if (event_id == MQTT_EVENT_DATA) { - esp_mqtt_event_handle_t event = event_data; - int * qos = handler_args; - *qos = event->qos; - xEventGroupSetBits(s_event_group, DATA_BIT); - } -} - -static void mqtt_data_handler_lwt(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data) -{ - if (event_id == MQTT_EVENT_DATA) { - esp_mqtt_event_handle_t event = event_data; - ESP_LOGI("mqtt-lwt", "MQTT_EVENT_DATA"); - ESP_LOGI("mqtt-lwt", "TOPIC=%.*s", event->topic_len, event->topic); - ESP_LOGI("mqtt-lwt", "DATA=%.*s", event->data_len, event->data); - if (strncmp(event->data, "no-lwt", event->data_len) == 0) { - // no lwt, just to indicate the test has finished - xEventGroupSetBits(s_event_group, DATA_BIT); - } else { - // count up any potential lwt message - int * count = handler_args; - *count = *count + 1; - ESP_LOGE("mqtt-lwt", "count=%d", *count); - } - } -} - -static void mqtt_data_handler_subscribe(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data) -{ - if (event_id == MQTT_EVENT_SUBSCRIBED) { - esp_mqtt_event_handle_t event = event_data; - ESP_LOGI("mqtt-subscribe", "MQTT_EVENT_SUBSCRIBED, data size=%d", event->data_len); - int * sub_payload = handler_args; - if (event->data_len == 1) { - ESP_LOGI("mqtt-subscribe", "DATA=%d", *(uint8_t*)event->data); - *sub_payload = *(uint8_t*)event->data; - } - xEventGroupSetBits(s_event_group, DATA_BIT); - } -} - - -static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data) -{ - switch ((esp_mqtt_event_id_t)event_id) { - case MQTT_EVENT_CONNECTED: - xEventGroupSetBits(s_event_group, CONNECT_BIT); - break; - - case MQTT_EVENT_DISCONNECTED: - xEventGroupSetBits(s_event_group, DISCONNECT_BIT); - break; - default: - break; - } -} - -bool mqtt_connect_disconnect(void) -{ - const esp_mqtt_client_config_t mqtt_cfg = { - .broker.address.uri = CONFIG_MQTT_TEST_BROKER_URI, - .network.disable_auto_reconnect = true, - }; - s_event_group = xEventGroupCreate(); - esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg); - TEST_ASSERT_TRUE(NULL != client ); - esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, NULL); - TEST_ASSERT_TRUE(ESP_OK == esp_mqtt_client_start(client)); - WAIT_FOR_EVENT(CONNECT_BIT); - esp_mqtt_client_disconnect(client); - WAIT_FOR_EVENT(DISCONNECT_BIT); - esp_mqtt_client_reconnect(client); - WAIT_FOR_EVENT(CONNECT_BIT); - esp_mqtt_client_destroy(client); - vEventGroupDelete(s_event_group); - return true; -} - -bool mqtt_subscribe_publish(void) -{ - const esp_mqtt_client_config_t mqtt_cfg = { - .broker.address.uri = CONFIG_MQTT_TEST_BROKER_URI, - }; - char* topic = append_mac("topic"); - TEST_ASSERT_TRUE(NULL != topic); - s_event_group = xEventGroupCreate(); - esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg); - TEST_ASSERT_TRUE(NULL != client ); - esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, NULL); - TEST_ASSERT_TRUE(ESP_OK == esp_mqtt_client_start(client)); - WAIT_FOR_EVENT(CONNECT_BIT); - int qos = -1; - esp_mqtt_client_register_event(client, MQTT_EVENT_DATA, mqtt_data_handler_qos, &qos); - TEST_ASSERT_TRUE(esp_mqtt_client_subscribe(client, topic, 2) != -1); - TEST_ASSERT_TRUE(esp_mqtt_client_publish(client, topic, "message", 0, 2, 0) != -1); - WAIT_FOR_EVENT(DATA_BIT); - TEST_ASSERT_TRUE(qos == 2); - TEST_ASSERT_TRUE(esp_mqtt_client_publish(client, topic, "message", 0, 1, 0) != -1); - WAIT_FOR_EVENT(DATA_BIT); - TEST_ASSERT_TRUE(qos == 1); - esp_mqtt_client_destroy(client); - vEventGroupDelete(s_event_group); - free(topic); - return true; -} - -bool mqtt_lwt_clean_disconnect(void) -{ - char* lwt = append_mac("lwt"); - TEST_ASSERT_TRUE(lwt); - const esp_mqtt_client_config_t mqtt_cfg1 = { - .broker.address.uri = CONFIG_MQTT_TEST_BROKER_URI, - .credentials.set_null_client_id = true, - .session.last_will.topic = lwt, - .session.last_will.msg = "lwt_msg" - }; - const esp_mqtt_client_config_t mqtt_cfg2 = { - .broker.address.uri = CONFIG_MQTT_TEST_BROKER_URI, - .credentials.set_null_client_id = true, - .session.last_will.topic = lwt, - .session.last_will.msg = "lwt_msg" - }; - s_event_group = xEventGroupCreate(); - - esp_mqtt_client_handle_t client1 = esp_mqtt_client_init(&mqtt_cfg1); - esp_mqtt_client_handle_t client2 = esp_mqtt_client_init(&mqtt_cfg2); - TEST_ASSERT_TRUE(NULL != client1 && NULL != client2 ); - esp_mqtt_client_register_event(client1, ESP_EVENT_ANY_ID, mqtt_event_handler, NULL); - esp_mqtt_client_register_event(client2, ESP_EVENT_ANY_ID, mqtt_event_handler, NULL); - TEST_ASSERT_TRUE(esp_mqtt_client_start(client1) == ESP_OK); - WAIT_FOR_EVENT(CONNECT_BIT); - TEST_ASSERT_TRUE(esp_mqtt_client_start(client2) == ESP_OK); - WAIT_FOR_EVENT(CONNECT_BIT); - int counter = 0; - esp_mqtt_client_register_event(client1, MQTT_EVENT_DATA, mqtt_data_handler_lwt, &counter); - esp_mqtt_client_register_event(client2, MQTT_EVENT_DATA, mqtt_data_handler_lwt, &counter); - TEST_ASSERT_TRUE(esp_mqtt_client_subscribe(client1, lwt, 0) != -1); - TEST_ASSERT_TRUE(esp_mqtt_client_subscribe(client2, lwt, 0) != -1); - esp_mqtt_client_disconnect(client1); - WAIT_FOR_EVENT(DISCONNECT_BIT); - esp_mqtt_client_reconnect(client1); - WAIT_FOR_EVENT(CONNECT_BIT); - TEST_ASSERT_TRUE(esp_mqtt_client_subscribe(client1, lwt, 0) != -1); - esp_mqtt_client_stop(client2); - esp_mqtt_client_start(client2); - WAIT_FOR_EVENT(CONNECT_BIT); - TEST_ASSERT_TRUE(esp_mqtt_client_subscribe(client2, lwt, 0) != -1); - TEST_ASSERT_TRUE(esp_mqtt_client_publish(client1, lwt, "no-lwt", 0, 0, 0) != -1); - WAIT_FOR_EVENT(DATA_BIT); - TEST_ASSERT_TRUE(counter == 0); - esp_mqtt_client_destroy(client1); - esp_mqtt_client_destroy(client2); - vEventGroupDelete(s_event_group); - free(lwt); - return true; -} - -bool mqtt_subscribe_payload(void) -{ - const esp_mqtt_client_config_t mqtt_cfg = { - .broker.address.uri = CONFIG_MQTT_TEST_BROKER_URI, - .network.disable_auto_reconnect = true, - }; - char* topic = append_mac("topic"); - TEST_ASSERT_TRUE(NULL != topic); - s_event_group = xEventGroupCreate(); - esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg); - TEST_ASSERT_TRUE(NULL != client ); - esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, NULL); - TEST_ASSERT_TRUE(ESP_OK == esp_mqtt_client_start(client)); - WAIT_FOR_EVENT(CONNECT_BIT); - int qos_payload = -1; - esp_mqtt_client_register_event(client, MQTT_EVENT_SUBSCRIBED, mqtt_data_handler_subscribe, &qos_payload); - TEST_ASSERT_TRUE(esp_mqtt_client_subscribe(client, topic, 2) != -1); - WAIT_FOR_EVENT(DATA_BIT); - TEST_ASSERT_TRUE(qos_payload == 2); - TEST_ASSERT_TRUE(esp_mqtt_client_subscribe(client, topic, 0) != -1); - WAIT_FOR_EVENT(DATA_BIT); - TEST_ASSERT_TRUE(qos_payload == 0); - esp_mqtt_client_destroy(client); - vEventGroupDelete(s_event_group); - free(topic); - return true; -} diff --git a/components/mqtt/test/test_mqtt_client_broker.h b/components/mqtt/test/test_mqtt_client_broker.h deleted file mode 100644 index 6bfd5d8..0000000 --- a/components/mqtt/test/test_mqtt_client_broker.h +++ /dev/null @@ -1,50 +0,0 @@ -/* - * SPDX-FileCopyrightText: 2021 Espressif Systems (Shanghai) CO LTD - * - * SPDX-License-Identifier: Apache-2.0 - */ -#pragma once -#include "esp_log.h" - -/** - * @brief MQTT client-broker tests are not implemented as separate test cases - * due to time consuming connection setup/teardown. - * This utility macro is used to run functional cases as MQTT tests - * and evaluate as separate assertions in one "mqtt broker tests" test case. - */ -#define RUN_MQTT_BROKER_TEST(test_name) \ - do { \ - ESP_LOGI("mqtt_test", "Running test:" #test_name "()"); \ - TEST_ASSERT_TRUE_MESSAGE(test_name(), "Mqtt test failed: " #test_name "() "); \ - ESP_LOGI("mqtt_test", "Test:" #test_name "() passed "); \ - } while(0) - - -/** - * @brief This module contains mqtt test cases interacting the client with a (real) broker - */ - -/** - * @brief The client subscribes and publishes on the same topic - * and verifies the received published qos in the event - */ -bool mqtt_subscribe_publish(void); - -/** - * @brief The client connects, disconnects and reconnects. - * Tests basic client state transitions - */ -bool mqtt_connect_disconnect(void); - -/** - * @brief Two clients with defined lwt connect and subscribe to lwt topic. - * This test verifies that no lwt is send when each of the client disconnects. - * (we expect a clean disconnection, so no last-will being sent) - */ -bool mqtt_lwt_clean_disconnect(void); - -/** - * @brief The client subscribes to a topic with certain qos - * and verifies the qos in SUBACK message from the broker. - */ -bool mqtt_subscribe_payload(void); diff --git a/components/mqtt/test/test_mqtt_connection.c b/components/mqtt/test/test_mqtt_connection.c deleted file mode 100644 index 0e82003..0000000 --- a/components/mqtt/test/test_mqtt_connection.c +++ /dev/null @@ -1,144 +0,0 @@ -/* - * SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD - * - * SPDX-License-Identifier: Apache-2.0 - */ -#include "freertos/FreeRTOS.h" -#include "freertos/event_groups.h" -#include "unity.h" -#include "esp_event.h" -#include "esp_netif.h" -#include "esp_eth.h" -#include "esp_log.h" - - -#if SOC_EMAC_SUPPORTED -#define ETH_START_BIT BIT(0) -#define ETH_STOP_BIT BIT(1) -#define ETH_CONNECT_BIT BIT(2) -#define ETH_GOT_IP_BIT BIT(3) -#define ETH_STOP_TIMEOUT_MS (10000) -#define ETH_GET_IP_TIMEOUT_MS (60000) - - -static const char *TAG = "esp32_eth_test_fixture"; -static EventGroupHandle_t s_eth_event_group = NULL; -static esp_netif_t *s_eth_netif = NULL; -static esp_eth_mac_t *s_mac = NULL; -static esp_eth_phy_t *s_phy = NULL; -static esp_eth_handle_t s_eth_handle = NULL; -static esp_eth_netif_glue_handle_t s_eth_glue = NULL; - - -/** Event handler for Ethernet events */ -static void eth_event_handler(void *arg, esp_event_base_t event_base, - int32_t event_id, void *event_data) -{ - EventGroupHandle_t eth_event_group = (EventGroupHandle_t)arg; - switch (event_id) { - case ETHERNET_EVENT_CONNECTED: - xEventGroupSetBits(eth_event_group, ETH_CONNECT_BIT); - ESP_LOGI(TAG, "Ethernet Link Up"); - break; - case ETHERNET_EVENT_DISCONNECTED: - ESP_LOGI(TAG, "Ethernet Link Down"); - break; - case ETHERNET_EVENT_START: - xEventGroupSetBits(eth_event_group, ETH_START_BIT); - ESP_LOGI(TAG, "Ethernet Started"); - break; - case ETHERNET_EVENT_STOP: - xEventGroupSetBits(eth_event_group, ETH_STOP_BIT); - ESP_LOGI(TAG, "Ethernet Stopped"); - break; - default: - break; - } -} - -/** Event handler for IP_EVENT_ETH_GOT_IP */ -static void got_ip_event_handler(void *arg, esp_event_base_t event_base, - int32_t event_id, void *event_data) -{ - EventGroupHandle_t eth_event_group = (EventGroupHandle_t)arg; - ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data; - const esp_netif_ip_info_t *ip_info = &event->ip_info; - ESP_LOGI(TAG, "Ethernet Got IP Address"); - ESP_LOGI(TAG, "~~~~~~~~~~~"); - ESP_LOGI(TAG, "ETHIP:" IPSTR, IP2STR(&ip_info->ip)); - ESP_LOGI(TAG, "ETHMASK:" IPSTR, IP2STR(&ip_info->netmask)); - ESP_LOGI(TAG, "ETHGW:" IPSTR, IP2STR(&ip_info->gw)); - ESP_LOGI(TAG, "~~~~~~~~~~~"); - xEventGroupSetBits(eth_event_group, ETH_GOT_IP_BIT); -} - -static esp_err_t test_uninstall_driver(esp_eth_handle_t eth_hdl, uint32_t ms_to_wait) -{ - int i = 0; - ms_to_wait += 100; - for (i = 0; i < ms_to_wait / 100; i++) { - vTaskDelay(pdMS_TO_TICKS(100)); - if (esp_eth_driver_uninstall(eth_hdl) == ESP_OK) { - break; - } - } - if (i < ms_to_wait / 10) { - return ESP_OK; - } else { - return ESP_FAIL; - } -} - - -void connect_test_fixture_setup(void) -{ - EventBits_t bits; - s_eth_event_group = xEventGroupCreate(); - TEST_ASSERT(s_eth_event_group != NULL); - TEST_ESP_OK(esp_event_loop_create_default()); - // create TCP/IP netif - esp_netif_config_t netif_cfg = ESP_NETIF_DEFAULT_ETH(); - s_eth_netif = esp_netif_new(&netif_cfg); - - eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG(); - eth_esp32_emac_config_t esp32_emac_config = ETH_ESP32_EMAC_DEFAULT_CONFIG(); - s_mac = esp_eth_mac_new_esp32(&esp32_emac_config, &mac_config); - eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG(); - s_phy = esp_eth_phy_new_ip101(&phy_config); - esp_eth_config_t eth_config = ETH_DEFAULT_CONFIG(s_mac, s_phy); - - // install Ethernet driver - TEST_ESP_OK(esp_eth_driver_install(ð_config, &s_eth_handle)); - // combine driver with netif - s_eth_glue = esp_eth_new_netif_glue(s_eth_handle); - TEST_ESP_OK(esp_netif_attach(s_eth_netif, s_eth_glue)); - // register user defined event handers - TEST_ESP_OK(esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, ð_event_handler, s_eth_event_group)); - TEST_ESP_OK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &got_ip_event_handler, s_eth_event_group)); - // start Ethernet driver - TEST_ESP_OK(esp_eth_start(s_eth_handle)); - /* wait for IP lease */ - bits = xEventGroupWaitBits(s_eth_event_group, ETH_GOT_IP_BIT, true, true, pdMS_TO_TICKS(ETH_GET_IP_TIMEOUT_MS)); - TEST_ASSERT((bits & ETH_GOT_IP_BIT) == ETH_GOT_IP_BIT); -} - -void connect_test_fixture_teardown(void) -{ - EventBits_t bits; - // stop Ethernet driver - TEST_ESP_OK(esp_eth_stop(s_eth_handle)); - /* wait for connection stop */ - bits = xEventGroupWaitBits(s_eth_event_group, ETH_STOP_BIT, true, true, pdMS_TO_TICKS(ETH_STOP_TIMEOUT_MS)); - TEST_ASSERT((bits & ETH_STOP_BIT) == ETH_STOP_BIT); - TEST_ESP_OK(esp_eth_del_netif_glue(s_eth_glue)); - /* driver should be uninstalled within 2 seconds */ - TEST_ESP_OK(test_uninstall_driver(s_eth_handle, 2000)); - TEST_ESP_OK(s_phy->del(s_phy)); - TEST_ESP_OK(s_mac->del(s_mac)); - TEST_ESP_OK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_ETH_GOT_IP, got_ip_event_handler)); - TEST_ESP_OK(esp_event_handler_unregister(ETH_EVENT, ESP_EVENT_ANY_ID, eth_event_handler)); - esp_netif_destroy(s_eth_netif); - TEST_ESP_OK(esp_event_loop_delete_default()); - vEventGroupDelete(s_eth_event_group); -} -#endif // SOC_EMAC_SUPPORTED diff --git a/components/mqtt/test/test_mqtt_connection.h b/components/mqtt/test/test_mqtt_connection.h deleted file mode 100644 index 5322ddf..0000000 --- a/components/mqtt/test/test_mqtt_connection.h +++ /dev/null @@ -1,18 +0,0 @@ -/* - * SPDX-FileCopyrightText: 2021 Espressif Systems (Shanghai) CO LTD - * - * SPDX-License-Identifier: Apache-2.0 - */ -#pragma once -#include "soc/soc_caps.h" - -/** - * Connection test fixture setup, so we expect the broker is available - * on network - */ -void connect_test_fixture_setup(void); - -/** - * Cleans up the connection - */ -void connect_test_fixture_teardown(void); diff --git a/components/mqtt/host_test/CMakeLists.txt b/host_test/CMakeLists.txt similarity index 94% rename from components/mqtt/host_test/CMakeLists.txt rename to host_test/CMakeLists.txt index d1c1ac6..db98733 100644 --- a/components/mqtt/host_test/CMakeLists.txt +++ b/host_test/CMakeLists.txt @@ -3,6 +3,7 @@ cmake_minimum_required(VERSION 3.16) include($ENV{IDF_PATH}/tools/cmake/project.cmake) set(COMPONENTS main) list(APPEND EXTRA_COMPONENT_DIRS + "mocks/heap/" "$ENV{IDF_PATH}/tools/mocks/esp_hw_support/" "$ENV{IDF_PATH}/tools/mocks/freertos/" "$ENV{IDF_PATH}/tools/mocks/esp_timer/" diff --git a/components/mqtt/host_test/README.md b/host_test/README.md similarity index 100% rename from components/mqtt/host_test/README.md rename to host_test/README.md diff --git a/components/mqtt/host_test/main/CMakeLists.txt b/host_test/main/CMakeLists.txt similarity index 100% rename from components/mqtt/host_test/main/CMakeLists.txt rename to host_test/main/CMakeLists.txt diff --git a/components/mqtt/host_test/main/test_mqtt_client.cpp b/host_test/main/test_mqtt_client.cpp similarity index 97% rename from components/mqtt/host_test/main/test_mqtt_client.cpp rename to host_test/main/test_mqtt_client.cpp index 8ebbf1e..37dd34e 100644 --- a/components/mqtt/host_test/main/test_mqtt_client.cpp +++ b/host_test/main/test_mqtt_client.cpp @@ -1,3 +1,8 @@ +/* + * SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ #define CATCH_CONFIG_MAIN // This tells the catch header to generate a main #include "catch.hpp" diff --git a/host_test/mocks/heap/CMakeLists.txt b/host_test/mocks/heap/CMakeLists.txt new file mode 100644 index 0000000..0a0c8a6 --- /dev/null +++ b/host_test/mocks/heap/CMakeLists.txt @@ -0,0 +1,4 @@ +idf_component_get_property(original_heap_dir heap COMPONENT_OVERRIDEN_DIR) + +idf_component_register(SRCS heap_mock.c + INCLUDE_DIRS "${original_heap_dir}/include") diff --git a/host_test/mocks/heap/heap_mock.c b/host_test/mocks/heap/heap_mock.c new file mode 100644 index 0000000..6bc3b35 --- /dev/null +++ b/host_test/mocks/heap/heap_mock.c @@ -0,0 +1,11 @@ +#include "esp_heap_caps.h" +#include +#include + + + +void *heap_caps_calloc(size_t n, size_t size, uint32_t caps) { + (void)caps; + return calloc(n, size); + +} diff --git a/components/mqtt/host_test/mocks/include/sys/queue.h b/host_test/mocks/include/sys/queue.h similarity index 100% rename from components/mqtt/host_test/mocks/include/sys/queue.h rename to host_test/mocks/include/sys/queue.h diff --git a/components/mqtt/host_test/sdkconfig.defaults b/host_test/sdkconfig.defaults similarity index 100% rename from components/mqtt/host_test/sdkconfig.defaults rename to host_test/sdkconfig.defaults diff --git a/idf_component.yml b/idf_component.yml new file mode 100644 index 0000000..23cec31 --- /dev/null +++ b/idf_component.yml @@ -0,0 +1,5 @@ +version: "1.0.0" +description: esp-mqtt +dependencies: + idf: + version: ">=5.0" diff --git a/mqtt_client.c b/mqtt_client.c index d732d95..3a2563c 100644 --- a/mqtt_client.c +++ b/mqtt_client.c @@ -235,6 +235,10 @@ esp_mqtt_set_transport_failed: /* Checks if the user supplied config values are internally consistent */ static esp_err_t esp_mqtt_check_cfg_conflict(const mqtt_config_storage_t *cfg, const esp_mqtt_client_config_t *user_cfg) { + if(cfg == NULL || user_cfg == NULL) { + ESP_LOGE(TAG, "Invalid configuration"); + return ESP_ERR_INVALID_ARG; + } esp_err_t ret = ESP_OK; bool ssl_cfg_enabled = cfg->use_global_ca_store || cfg->cacert_buf || cfg->clientcert_buf || cfg->psk_hint_key || cfg->alpn_protos; @@ -517,6 +521,7 @@ esp_err_t esp_mqtt_set_config(esp_mqtt_client_handle_t client, const esp_mqtt_cl if (config->broker.address.transport) { free(client->config->scheme); + client->config->scheme = NULL; if (config->broker.address.transport == MQTT_TRANSPORT_OVER_TCP) { client->config->scheme = create_string(MQTT_OVER_TCP_SCHEME, strlen(MQTT_OVER_TCP_SCHEME)); ESP_MEM_CHECK(TAG, client->config->scheme, goto _mqtt_set_config_failed);