From ab9698a3b4045713c1ab6c9670820dd8643f867c Mon Sep 17 00:00:00 2001 From: David Cermak Date: Tue, 24 Jul 2018 16:59:03 +0200 Subject: [PATCH 001/231] 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/Kconfig | 1 + components/mqtt/component.mk | 4 + docs/en/api-reference/protocols/mqtt.rst | 112 ++++++++++++ docs/zh_CN/api-reference/protocols/mqtt.rst | 1 + examples/protocols/mqtt/ssl/CMakeLists.txt | 9 + examples/protocols/mqtt/ssl/Makefile | 7 + examples/protocols/mqtt/ssl/README.md | 67 +++++++ .../protocols/mqtt/ssl/main/CMakeLists.txt | 4 + .../protocols/mqtt/ssl/main/Kconfig.projbuild | 15 ++ examples/protocols/mqtt/ssl/main/app_main.c | 149 +++++++++++++++ examples/protocols/mqtt/ssl/main/component.mk | 1 + .../mqtt/ssl/main/iot_eclipse_org.pem | 27 +++ .../mqtt/ssl/mqtt_ssl_example_test.py | 132 ++++++++++++++ examples/protocols/mqtt/tcp/CMakeLists.txt | 7 + examples/protocols/mqtt/tcp/Makefile | 7 + examples/protocols/mqtt/tcp/README.md | 61 +++++++ .../protocols/mqtt/tcp/main/CMakeLists.txt | 4 + .../protocols/mqtt/tcp/main/Kconfig.projbuild | 25 +++ examples/protocols/mqtt/tcp/main/app_main.c | 171 ++++++++++++++++++ examples/protocols/mqtt/tcp/main/component.mk | 0 .../mqtt/tcp/mqtt_tcp_example_test.py | 101 +++++++++++ examples/protocols/mqtt/tcp/sdkconfig | 1 + .../protocols/mqtt/tcp/sdkconfig.defaults | 7 + examples/protocols/mqtt/ws/CMakeLists.txt | 7 + examples/protocols/mqtt/ws/Makefile | 7 + examples/protocols/mqtt/ws/README.md | 62 +++++++ .../protocols/mqtt/ws/main/CMakeLists.txt | 4 + .../protocols/mqtt/ws/main/Kconfig.projbuild | 15 ++ examples/protocols/mqtt/ws/main/app_main.c | 144 +++++++++++++++ examples/protocols/mqtt/ws/main/component.mk | 0 .../protocols/mqtt/ws/mqtt_ws_example_test.py | 128 +++++++++++++ examples/protocols/mqtt/wss/CMakeLists.txt | 9 + examples/protocols/mqtt/wss/Makefile | 7 + examples/protocols/mqtt/wss/README.md | 69 +++++++ .../protocols/mqtt/wss/main/CMakeLists.txt | 4 + .../protocols/mqtt/wss/main/Kconfig.projbuild | 15 ++ examples/protocols/mqtt/wss/main/app_main.c | 148 +++++++++++++++ examples/protocols/mqtt/wss/main/component.mk | 1 + .../mqtt/wss/main/iot_eclipse_org.pem | 27 +++ .../mqtt/wss/mqtt_wss_example_test.py | 132 ++++++++++++++ 40 files changed, 1692 insertions(+) create mode 100644 components/mqtt/Kconfig create mode 100644 components/mqtt/component.mk create mode 100644 docs/en/api-reference/protocols/mqtt.rst create mode 100644 docs/zh_CN/api-reference/protocols/mqtt.rst create mode 100644 examples/protocols/mqtt/ssl/CMakeLists.txt create mode 100644 examples/protocols/mqtt/ssl/Makefile create mode 100644 examples/protocols/mqtt/ssl/README.md create mode 100644 examples/protocols/mqtt/ssl/main/CMakeLists.txt create mode 100644 examples/protocols/mqtt/ssl/main/Kconfig.projbuild create mode 100644 examples/protocols/mqtt/ssl/main/app_main.c create mode 100644 examples/protocols/mqtt/ssl/main/component.mk create mode 100644 examples/protocols/mqtt/ssl/main/iot_eclipse_org.pem create mode 100644 examples/protocols/mqtt/ssl/mqtt_ssl_example_test.py create mode 100644 examples/protocols/mqtt/tcp/CMakeLists.txt create mode 100644 examples/protocols/mqtt/tcp/Makefile create mode 100644 examples/protocols/mqtt/tcp/README.md create mode 100644 examples/protocols/mqtt/tcp/main/CMakeLists.txt create mode 100644 examples/protocols/mqtt/tcp/main/Kconfig.projbuild create mode 100644 examples/protocols/mqtt/tcp/main/app_main.c create mode 100644 examples/protocols/mqtt/tcp/main/component.mk create mode 100644 examples/protocols/mqtt/tcp/mqtt_tcp_example_test.py create mode 100644 examples/protocols/mqtt/tcp/sdkconfig create mode 100644 examples/protocols/mqtt/tcp/sdkconfig.defaults create mode 100644 examples/protocols/mqtt/ws/CMakeLists.txt create mode 100644 examples/protocols/mqtt/ws/Makefile create mode 100644 examples/protocols/mqtt/ws/README.md create mode 100644 examples/protocols/mqtt/ws/main/CMakeLists.txt create mode 100644 examples/protocols/mqtt/ws/main/Kconfig.projbuild create mode 100644 examples/protocols/mqtt/ws/main/app_main.c create mode 100644 examples/protocols/mqtt/ws/main/component.mk create mode 100644 examples/protocols/mqtt/ws/mqtt_ws_example_test.py create mode 100644 examples/protocols/mqtt/wss/CMakeLists.txt create mode 100644 examples/protocols/mqtt/wss/Makefile create mode 100644 examples/protocols/mqtt/wss/README.md create mode 100644 examples/protocols/mqtt/wss/main/CMakeLists.txt create mode 100644 examples/protocols/mqtt/wss/main/Kconfig.projbuild create mode 100644 examples/protocols/mqtt/wss/main/app_main.c create mode 100644 examples/protocols/mqtt/wss/main/component.mk create mode 100644 examples/protocols/mqtt/wss/main/iot_eclipse_org.pem create mode 100644 examples/protocols/mqtt/wss/mqtt_wss_example_test.py 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/docs/en/api-reference/protocols/mqtt.rst b/docs/en/api-reference/protocols/mqtt.rst new file mode 100644 index 0000000..83dece2 --- /dev/null +++ b/docs/en/api-reference/protocols/mqtt.rst @@ -0,0 +1,112 @@ +ESP-MQTT +======== + +Overview +-------- + +ESP-MQTT is an implementation of MQTT protocol client (MQTT is a lightweight publish/subscribe messaging protocol). + + +Features +-------- + * supports MQTT over TCP, SSL with mbedtls, MQTT over Websocket, MQTT over Websocket Secure. + * Easy to setup with URI + * Multiple instances (Multiple clients in one application) + * Support subscribing, publishing, authentication, will messages, keep alive pings and all 3 QoS levels (it should be a fully functional client). + + +Application Example +------------------- + + * :example:`protocols/mqtt/tcp`: MQTT over tcp, defalut port 1883 + * :example:`protocols/mqtt/ssl`: MQTT over tcp, defalut port 8883 + * :example:`protocols/mqtt/ws`: MQTT over Websocket, defalut port 80 + * :example:`protocols/mqtt/wss`: MQTT over Websocket Secure, defalut port 443 + + +Configuration +------------- +URI +^^^ + +- Curently support ``mqtt``, ``mqtts``, ``ws``, ``wss`` schemes +- MQTT over TCP samples: + + - ``mqtt://iot.eclipse.org``: MQTT over TCP, default port 1883: + - ``mqtt://iot.eclipse.org:1884`` MQTT over TCP, port 1884: + - ``mqtt://username:password@iot.eclipse.org:1884`` MQTT over TCP, + port 1884, with username and password + +- MQTT over SSL samples: + + - ``mqtts://iot.eclipse.org``: MQTT over SSL, port 8883 + - ``mqtts://iot.eclipse.org:8884``: MQTT over SSL, port 8884 + +- MQTT over Websocket samples: + + - ``ws://iot.eclipse.org:80/ws`` + +- MQTT over Websocket Secure samples: + + - ``wss://iot.eclipse.org:443/ws`` + +- Minimal configurations: + +.. code:: c + + const esp_mqtt_client_config_t mqtt_cfg = { + .uri = "mqtt://iot.eclipse.org", + .event_handle = mqtt_event_handler, + // .user_context = (void *)your_context + }; + +- If there are any options related to the URI in + ``esp_mqtt_client_config_t``, the option defined by the URI will be + overridden. Sample: + +.. code:: c + + const esp_mqtt_client_config_t mqtt_cfg = { + .uri = "mqtt://iot.eclipse.org:1234", + .event_handle = mqtt_event_handler, + .port = 4567, + }; + //MQTT client will connect to iot.eclipse.org using port 4567 + +SSL +^^^ + +- Get certificate from server, example: ``iot.eclipse.org`` + ``openssl s_client -showcerts -connect iot.eclipse.org:8883 /dev/null|openssl x509 -outform PEM >iot_eclipse_org.pem`` +- Check the sample application: ``examples/mqtt_ssl`` +- Configuration: + +.. code:: cpp + + const esp_mqtt_client_config_t mqtt_cfg = { + .uri = "mqtts://iot.eclipse.org:8883", + .event_handle = mqtt_event_handler, + .cert_pem = (const char *)iot_eclipse_org_pem_start, + }; + +For more options on ``esp_mqtt_client_config_t``, please refer to API reference below + +Change settings in ``menuconfig`` +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +:: + make menuconfig + -> Component config -> ESP-MQTT Configuration + + +- :envvar:`CONFIG_MQTT_PROTOCOL_311`: Enables 3.1.1 version of MQTT protocol + +- :envvar:`MQTT_TRANSPORT_%TRANSPORT%`: Enables specific MQTT transport layer, such as SSL, WEBSOCKET, WEBSOCKET_SECURE + +- :envvar:`MQTT_CUSTOM_OUTBOX`: Disables default implementation of mqtt_outbox, so a specific implementaion can be supplied + + +API Reference +------------- + +.. include:: /_build/inc/mqtt_client.inc \ No newline at end of file diff --git a/docs/zh_CN/api-reference/protocols/mqtt.rst b/docs/zh_CN/api-reference/protocols/mqtt.rst new file mode 100644 index 0000000..4f5ad7d --- /dev/null +++ b/docs/zh_CN/api-reference/protocols/mqtt.rst @@ -0,0 +1 @@ +.. include:: ../../../en/api-reference/protocols/mqtt.rst \ No newline at end of file diff --git a/examples/protocols/mqtt/ssl/CMakeLists.txt b/examples/protocols/mqtt/ssl/CMakeLists.txt new file mode 100644 index 0000000..13bdd8c --- /dev/null +++ b/examples/protocols/mqtt/ssl/CMakeLists.txt @@ -0,0 +1,9 @@ +# The following four lines of boilerplate have to be in your project's CMakeLists +# in this exact order for cmake to work correctly +cmake_minimum_required(VERSION 3.5) + +include($ENV{IDF_PATH}/tools/cmake/project.cmake) + +project(mqtt_ssl) + +target_add_binary_data(mqtt_ssl.elf "main/iot_eclipse_org.pem" TEXT) diff --git a/examples/protocols/mqtt/ssl/Makefile b/examples/protocols/mqtt/ssl/Makefile new file mode 100644 index 0000000..bae0d73 --- /dev/null +++ b/examples/protocols/mqtt/ssl/Makefile @@ -0,0 +1,7 @@ +# +# This is a project Makefile. It is assumed the directory this Makefile resides in is a +# project subdirectory. +# +PROJECT_NAME := mqtt_ssl + +include $(IDF_PATH)/make/project.mk diff --git a/examples/protocols/mqtt/ssl/README.md b/examples/protocols/mqtt/ssl/README.md new file mode 100644 index 0000000..3d369bd --- /dev/null +++ b/examples/protocols/mqtt/ssl/README.md @@ -0,0 +1,67 @@ +# ESP-MQTT SSL Sample application + +(See the README.md file in the upper level 'examples' directory for more information about examples.) + +This example connects to the broker iot.eclipse.org using ssl transport and as a demonstration subscribes/unsubscribes and send a message on certain topic. + +It uses ESP-MQTT library which implements mqtt client to connect to mqtt broker. + +## How to use example + +### Hardware Required + +This example can be executed on any ESP32 board, the only required interface is WiFi and connection to internet. + +### Configure the project + +``` +make menuconfig +``` + +* Set serial port under Serial Flasher Options. + +* Set ssid and password for the board to connect to AP. + +Note how to create a PEM certificate for iot.eclipse.org: +``` +openssl s_client -showcerts -connect iot.eclipse.org:8883 /dev/null|openssl x509 -outform PEM >iot_eclipse_org.pem +``` + +### Build and Flash + +Build the project and flash it to the board, then run monitor tool to view serial output: + +``` +make -j4 flash monitor +``` + +(To exit the serial monitor, type ``Ctrl-]``.) + +See the Getting Started Guide for full steps to configure and use ESP-IDF to build projects. + +## Example Output + +``` +I (3714) event: sta ip: 192.168.0.139, mask: 255.255.255.0, gw: 192.168.0.2 +I (3714) system_api: Base MAC address is not set, read default base MAC address from BLK0 of EFUSE +I (3964) MQTT_CLIENT: Sending MQTT CONNECT message, type: 1, id: 0000 +I (4164) MQTTS_EXAMPLE: MQTT_EVENT_CONNECTED +I (4174) MQTTS_EXAMPLE: sent publish successful, msg_id=41464 +I (4174) MQTTS_EXAMPLE: sent subscribe successful, msg_id=17886 +I (4174) MQTTS_EXAMPLE: sent subscribe successful, msg_id=42970 +I (4184) MQTTS_EXAMPLE: sent unsubscribe successful, msg_id=50241 +I (4314) MQTTS_EXAMPLE: MQTT_EVENT_PUBLISHED, msg_id=41464 +I (4484) MQTTS_EXAMPLE: MQTT_EVENT_SUBSCRIBED, msg_id=17886 +I (4484) MQTTS_EXAMPLE: sent publish successful, msg_id=0 +I (4684) MQTTS_EXAMPLE: MQTT_EVENT_SUBSCRIBED, msg_id=42970 +I (4684) MQTTS_EXAMPLE: sent publish successful, msg_id=0 +I (4884) MQTT_CLIENT: deliver_publish, message_length_read=19, message_length=19 +I (4884) MQTTS_EXAMPLE: MQTT_EVENT_DATA +TOPIC=/topic/qos0 +DATA=data +I (5194) MQTT_CLIENT: deliver_publish, message_length_read=19, message_length=19 +I (5194) MQTTS_EXAMPLE: MQTT_EVENT_DATA +TOPIC=/topic/qos0 +DATA=data +``` + diff --git a/examples/protocols/mqtt/ssl/main/CMakeLists.txt b/examples/protocols/mqtt/ssl/main/CMakeLists.txt new file mode 100644 index 0000000..6b03500 --- /dev/null +++ b/examples/protocols/mqtt/ssl/main/CMakeLists.txt @@ -0,0 +1,4 @@ +set(COMPONENT_SRCS "app_main.c") +set(COMPONENT_ADD_INCLUDEDIRS ".") + +register_component() diff --git a/examples/protocols/mqtt/ssl/main/Kconfig.projbuild b/examples/protocols/mqtt/ssl/main/Kconfig.projbuild new file mode 100644 index 0000000..176d8fb --- /dev/null +++ b/examples/protocols/mqtt/ssl/main/Kconfig.projbuild @@ -0,0 +1,15 @@ +menu "Example Configuration" + +config WIFI_SSID + string "WiFi SSID" + default "myssid" + help + SSID (network name) for the example to connect to. + +config WIFI_PASSWORD + string "WiFi Password" + default "mypassword" + help + WiFi password (WPA or WPA2) for the example to use. + +endmenu diff --git a/examples/protocols/mqtt/ssl/main/app_main.c b/examples/protocols/mqtt/ssl/main/app_main.c new file mode 100644 index 0000000..83f0b8a --- /dev/null +++ b/examples/protocols/mqtt/ssl/main/app_main.c @@ -0,0 +1,149 @@ +#include +#include +#include +#include +#include "esp_wifi.h" +#include "esp_system.h" +#include "nvs_flash.h" +#include "esp_event_loop.h" + +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "freertos/semphr.h" +#include "freertos/queue.h" +#include "freertos/event_groups.h" + +#include "lwip/sockets.h" +#include "lwip/dns.h" +#include "lwip/netdb.h" + +#include "esp_log.h" +#include "mqtt_client.h" + +static const char *TAG = "MQTTS_EXAMPLE"; + +static EventGroupHandle_t wifi_event_group; +const static int CONNECTED_BIT = BIT0; + + + +static esp_err_t wifi_event_handler(void *ctx, system_event_t *event) +{ + switch (event->event_id) { + case SYSTEM_EVENT_STA_START: + esp_wifi_connect(); + break; + case SYSTEM_EVENT_STA_GOT_IP: + xEventGroupSetBits(wifi_event_group, CONNECTED_BIT); + + break; + case SYSTEM_EVENT_STA_DISCONNECTED: + esp_wifi_connect(); + xEventGroupClearBits(wifi_event_group, CONNECTED_BIT); + break; + default: + break; + } + return ESP_OK; +} + +static void wifi_init(void) +{ + tcpip_adapter_init(); + wifi_event_group = xEventGroupCreate(); + ESP_ERROR_CHECK(esp_event_loop_init(wifi_event_handler, NULL)); + wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); + ESP_ERROR_CHECK(esp_wifi_init(&cfg)); + ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM)); + wifi_config_t wifi_config = { + .sta = { + .ssid = CONFIG_WIFI_SSID, + .password = CONFIG_WIFI_PASSWORD, + }, + }; + ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); + ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config)); + ESP_LOGI(TAG, "start the WIFI SSID:[%s]", CONFIG_WIFI_SSID); + ESP_ERROR_CHECK(esp_wifi_start()); + ESP_LOGI(TAG, "Waiting for wifi"); + xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT, false, true, portMAX_DELAY); +} + +extern const uint8_t iot_eclipse_org_pem_start[] asm("_binary_iot_eclipse_org_pem_start"); +extern const uint8_t iot_eclipse_org_pem_end[] asm("_binary_iot_eclipse_org_pem_end"); + +static esp_err_t mqtt_event_handler(esp_mqtt_event_handle_t event) +{ + esp_mqtt_client_handle_t client = event->client; + int msg_id; + // your_context_t *context = event->context; + switch (event->event_id) { + case MQTT_EVENT_CONNECTED: + ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED"); + msg_id = esp_mqtt_client_subscribe(client, "/topic/qos0", 0); + ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id); + + msg_id = esp_mqtt_client_subscribe(client, "/topic/qos1", 1); + ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id); + + msg_id = esp_mqtt_client_unsubscribe(client, "/topic/qos1"); + ESP_LOGI(TAG, "sent unsubscribe successful, msg_id=%d", msg_id); + break; + case MQTT_EVENT_DISCONNECTED: + ESP_LOGI(TAG, "MQTT_EVENT_DISCONNECTED"); + break; + + case MQTT_EVENT_SUBSCRIBED: + ESP_LOGI(TAG, "MQTT_EVENT_SUBSCRIBED, msg_id=%d", event->msg_id); + msg_id = esp_mqtt_client_publish(client, "/topic/qos0", "data", 0, 0, 0); + ESP_LOGI(TAG, "sent publish successful, msg_id=%d", msg_id); + break; + case MQTT_EVENT_UNSUBSCRIBED: + ESP_LOGI(TAG, "MQTT_EVENT_UNSUBSCRIBED, msg_id=%d", event->msg_id); + break; + case MQTT_EVENT_PUBLISHED: + ESP_LOGI(TAG, "MQTT_EVENT_PUBLISHED, msg_id=%d", event->msg_id); + break; + case MQTT_EVENT_DATA: + ESP_LOGI(TAG, "MQTT_EVENT_DATA"); + printf("TOPIC=%.*s\r\n", event->topic_len, event->topic); + printf("DATA=%.*s\r\n", event->data_len, event->data); + break; + case MQTT_EVENT_ERROR: + ESP_LOGI(TAG, "MQTT_EVENT_ERROR"); + break; + } + return ESP_OK; +} + +static void mqtt_app_start(void) +{ + const esp_mqtt_client_config_t mqtt_cfg = { + .uri = "mqtts://iot.eclipse.org:8883", + .event_handle = mqtt_event_handler, + .cert_pem = (const char *)iot_eclipse_org_pem_start, + }; + + ESP_LOGI(TAG, "[APP] Free memory: %d bytes", esp_get_free_heap_size()); + esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg); + esp_mqtt_client_start(client); +} + +void app_main() +{ + ESP_LOGI(TAG, "[APP] Startup.."); + ESP_LOGI(TAG, "[APP] Free memory: %d bytes", esp_get_free_heap_size()); + ESP_LOGI(TAG, "[APP] IDF version: %s", esp_get_idf_version()); + + esp_log_level_set("*", ESP_LOG_INFO); + esp_log_level_set("MQTT_CLIENT", ESP_LOG_VERBOSE); + esp_log_level_set("TRANSPORT_TCP", ESP_LOG_VERBOSE); + esp_log_level_set("TRANSPORT_SSL", ESP_LOG_VERBOSE); + esp_log_level_set("TRANSPORT", ESP_LOG_VERBOSE); + esp_log_level_set("OUTBOX", ESP_LOG_VERBOSE); + + nvs_flash_init(); + wifi_init(); + mqtt_app_start(); + +} diff --git a/examples/protocols/mqtt/ssl/main/component.mk b/examples/protocols/mqtt/ssl/main/component.mk new file mode 100644 index 0000000..797c4a1 --- /dev/null +++ b/examples/protocols/mqtt/ssl/main/component.mk @@ -0,0 +1 @@ +COMPONENT_EMBED_TXTFILES := iot_eclipse_org.pem diff --git a/examples/protocols/mqtt/ssl/main/iot_eclipse_org.pem b/examples/protocols/mqtt/ssl/main/iot_eclipse_org.pem new file mode 100644 index 0000000..edb593b --- /dev/null +++ b/examples/protocols/mqtt/ssl/main/iot_eclipse_org.pem @@ -0,0 +1,27 @@ +-----BEGIN CERTIFICATE----- +MIIEkjCCA3qgAwIBAgIQCgFBQgAAAVOFc2oLheynCDANBgkqhkiG9w0BAQsFADA/ +MSQwIgYDVQQKExtEaWdpdGFsIFNpZ25hdHVyZSBUcnVzdCBDby4xFzAVBgNVBAMT +DkRTVCBSb290IENBIFgzMB4XDTE2MDMxNzE2NDA0NloXDTIxMDMxNzE2NDA0Nlow +SjELMAkGA1UEBhMCVVMxFjAUBgNVBAoTDUxldCdzIEVuY3J5cHQxIzAhBgNVBAMT +GkxldCdzIEVuY3J5cHQgQXV0aG9yaXR5IFgzMIIBIjANBgkqhkiG9w0BAQEFAAOC +AQ8AMIIBCgKCAQEAnNMM8FrlLke3cl03g7NoYzDq1zUmGSXhvb418XCSL7e4S0EF +q6meNQhY7LEqxGiHC6PjdeTm86dicbp5gWAf15Gan/PQeGdxyGkOlZHP/uaZ6WA8 +SMx+yk13EiSdRxta67nsHjcAHJyse6cF6s5K671B5TaYucv9bTyWaN8jKkKQDIZ0 +Z8h/pZq4UmEUEz9l6YKHy9v6Dlb2honzhT+Xhq+w3Brvaw2VFn3EK6BlspkENnWA +a6xK8xuQSXgvopZPKiAlKQTGdMDQMc2PMTiVFrqoM7hD8bEfwzB/onkxEz0tNvjj +/PIzark5McWvxI0NHWQWM6r6hCm21AvA2H3DkwIDAQABo4IBfTCCAXkwEgYDVR0T +AQH/BAgwBgEB/wIBADAOBgNVHQ8BAf8EBAMCAYYwfwYIKwYBBQUHAQEEczBxMDIG +CCsGAQUFBzABhiZodHRwOi8vaXNyZy50cnVzdGlkLm9jc3AuaWRlbnRydXN0LmNv +bTA7BggrBgEFBQcwAoYvaHR0cDovL2FwcHMuaWRlbnRydXN0LmNvbS9yb290cy9k +c3Ryb290Y2F4My5wN2MwHwYDVR0jBBgwFoAUxKexpHsscfrb4UuQdf/EFWCFiRAw +VAYDVR0gBE0wSzAIBgZngQwBAgEwPwYLKwYBBAGC3xMBAQEwMDAuBggrBgEFBQcC +ARYiaHR0cDovL2Nwcy5yb290LXgxLmxldHNlbmNyeXB0Lm9yZzA8BgNVHR8ENTAz +MDGgL6AthitodHRwOi8vY3JsLmlkZW50cnVzdC5jb20vRFNUUk9PVENBWDNDUkwu +Y3JsMB0GA1UdDgQWBBSoSmpjBH3duubRObemRWXv86jsoTANBgkqhkiG9w0BAQsF +AAOCAQEA3TPXEfNjWDjdGBX7CVW+dla5cEilaUcne8IkCJLxWh9KEik3JHRRHGJo +uM2VcGfl96S8TihRzZvoroed6ti6WqEBmtzw3Wodatg+VyOeph4EYpr/1wXKtx8/ +wApIvJSwtmVi4MFU5aMqrSDE6ea73Mj2tcMyo5jMd6jmeWUHK8so/joWUoHOUgwu +X4Po1QYz+3dszkDqMp4fklxBwXRsW10KXzPMTZ+sOPAveyxindmjkW8lGy+QsRlG +PfZ+G6Z6h7mjem0Y+iWlkYcV4PIWL1iwBi8saCbGS5jN2p8M+X+Q7UNKEkROb3N6 +KOqkqm57TH2H3eDJAkSnh6/DNFu0Qg== +-----END CERTIFICATE----- \ No newline at end of file diff --git a/examples/protocols/mqtt/ssl/mqtt_ssl_example_test.py b/examples/protocols/mqtt/ssl/mqtt_ssl_example_test.py new file mode 100644 index 0000000..c5ae31a --- /dev/null +++ b/examples/protocols/mqtt/ssl/mqtt_ssl_example_test.py @@ -0,0 +1,132 @@ +import re +import os +import sys +import time +import socket +import imp +import ssl + +use_mqtt_client_sketch = False + +try: + imp.find_module('paho') + import paho.mqtt.client as mqtt + # Make things with supposed existing module +except ImportError: + use_mqtt_client_sketch = True + pass + +global g_recv_topic +global g_recv_data + +g_recv_data="" + +# This is only a workaround for running mqtt client with 'hardcoded' data using plain socket interface +def mqtt_client_sketch(): + global g_recv_topic + global g_recv_data + connect_msg = bytearray([0x10, 0x0c, 00, 0x04, 0x4d, 0x51, 0x54, 0x54, 0x04, 0x02, 00, 0x3c, 00, 00]) + send_qos0_msg = bytearray([ 0x30, 0x1a, 0x00, 0x0b, 0x2f, 0x74, 0x6f, 0x70, 0x69, 0x63, 0x2f, 0x71, 0x6f, 0x73, 0x30, 0x64, 0x61, 0x74, 0x61, 0x5f, 0x74, 0x6f, 0x5f, 0x65, 0x73, 0x70, 0x33, 0x32]) + subscribe_qos0 = bytearray([ 0x82, 0x10, 0x00, 0x01, 0x00, 0x0b, 0x2f, 0x74, 0x6f, 0x70, 0x69, 0x63, 0x2f, 0x71, 0x6f, 0x73, 0x30, 0x00] ) + client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + client.settimeout(30) + cli = ssl.wrap_socket(client) + cli.connect(("iot.eclipse.org", 8883)) + cli.send(connect_msg) + data = cli.recv(1024) + print("Connect ack received {}".format(data)) + cli.send(subscribe_qos0) + data = cli.recv(1024) + print("Subscibe ack received {}".format(data)) + start = time.time() + while (time.time() - start) <= 20: + data = cli.recv(1024) + print("Data received {}".format(data[-17:])) + if data[-15:] == "/topic/qos0data": + g_recv_topic = data[-15:][:11] + g_recv_data = data[-4:] + cli.send(send_qos0_msg) + data = cli.recv(1024) + print("data ack received {}".format(data)) + break + cli.close() + +# 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)) + client.subscribe("/topic/qos0") + +# The callback for when a PUBLISH message is received from the server. +def on_message(client, userdata, msg): + global g_recv_topic + global g_recv_data + if g_recv_data == "" and msg.payload == "data": + client.publish("/topic/qos0", "data_to_esp32") + g_recv_topic = msg.topic + g_recv_data = msg.payload + print(msg.topic+" "+str(msg.payload)) + +# 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 TinyFW +import IDF + + + + +@IDF.idf_example_test(env_tag="Example_WIFI") +def test_examples_protocol_mqtt_ssl(env, extra_data): + global g_recv_topic + global g_recv_data + """ + 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_ssl", "examples/protocols/mqtt/ssl") + # check and log bin size + binary_file = os.path.join(dut1.app.binary_path, "mqtt_ssl.bin") + bin_size = os.path.getsize(binary_file) + IDF.log_performance("mqtt_ssl_bin_size", "{}KB".format(bin_size//1024)) + IDF.check_performance("mqtt_ssl_size", bin_size//1024) + # 1. start test + dut1.start_app() + # 2. Test connects to a broker + if use_mqtt_client_sketch: + mqtt_client_sketch() + else: + client = mqtt.Client() + client.on_connect = on_connect + client.on_message = on_message + 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("iot.eclipse.org", 8883, 60) + print "...done" + print "Start Looping..." + start = time.time() + while (time.time() - start) <= 20: + client.loop() + print "...done" + # 3. check the message received back from the server + if g_recv_topic == "/topic/qos0" and g_recv_data == "data" : + print("PASS: Received correct message") + else: + print("Failure!") + raise ValueError('Wrong data received topic: {}, data:{}'.format(g_recv_topic, g_recv_data)) + # 4. check that the esp32 client received data sent by this python client + dut1.expect(re.compile(r"DATA=data_to_esp32"), timeout=30) + +if __name__ == '__main__': + test_examples_protocol_mqtt_ssl() diff --git a/examples/protocols/mqtt/tcp/CMakeLists.txt b/examples/protocols/mqtt/tcp/CMakeLists.txt new file mode 100644 index 0000000..678d787 --- /dev/null +++ b/examples/protocols/mqtt/tcp/CMakeLists.txt @@ -0,0 +1,7 @@ +# The following four lines of boilerplate have to be in your project's CMakeLists +# in this exact order for cmake to work correctly +cmake_minimum_required(VERSION 3.5) + +include($ENV{IDF_PATH}/tools/cmake/project.cmake) + +project(mqtt_tcp) \ No newline at end of file diff --git a/examples/protocols/mqtt/tcp/Makefile b/examples/protocols/mqtt/tcp/Makefile new file mode 100644 index 0000000..cd53fdb --- /dev/null +++ b/examples/protocols/mqtt/tcp/Makefile @@ -0,0 +1,7 @@ +# +# This is a project Makefile. It is assumed the directory this Makefile resides in is a +# project subdirectory. +# +PROJECT_NAME := mqtt_tcp + +include $(IDF_PATH)/make/project.mk diff --git a/examples/protocols/mqtt/tcp/README.md b/examples/protocols/mqtt/tcp/README.md new file mode 100644 index 0000000..2fda240 --- /dev/null +++ b/examples/protocols/mqtt/tcp/README.md @@ -0,0 +1,61 @@ +# ESP-MQTT sample application +(See the README.md file in the upper level 'examples' directory for more information about examples.) + +This example connects to the broker URI selected using `make menuconfig` (using mqtt tcp transport) and as a demonstration subscribes/unsubscribes and send a message on certain topic. +Note: If the URI equals `FROM_STDIN` then the broker address is read from stdin upon application startup (used for testing) + +It uses ESP-MQTT library which implements mqtt client to connect to mqtt broker. + +## How to use example + +### Hardware Required + +This example can be executed on any ESP32 board, the only required interface is WiFi and connection to internet. + +### Configure the project + +``` +make menuconfig +``` + +* Set serial port under Serial Flasher Options. + +* Set ssid and password for the board to connect to AP. + +### Build and Flash + +Build the project and flash it to the board, then run monitor tool to view serial output: + +``` +make -j4 flash monitor +``` + +(To exit the serial monitor, type ``Ctrl-]``.) + +See the Getting Started Guide for full steps to configure and use ESP-IDF to build projects. + +## Example Output + +``` +I (3714) event: sta ip: 192.168.0.139, mask: 255.255.255.0, gw: 192.168.0.2 +I (3714) system_api: Base MAC address is not set, read default base MAC address from BLK0 of EFUSE +I (3964) MQTT_CLIENT: Sending MQTT CONNECT message, type: 1, id: 0000 +I (4164) MQTT_EXAMPLE: MQTT_EVENT_CONNECTED +I (4174) MQTT_EXAMPLE: sent publish successful, msg_id=41464 +I (4174) MQTT_EXAMPLE: sent subscribe successful, msg_id=17886 +I (4174) MQTT_EXAMPLE: sent subscribe successful, msg_id=42970 +I (4184) MQTT_EXAMPLE: sent unsubscribe successful, msg_id=50241 +I (4314) MQTT_EXAMPLE: MQTT_EVENT_PUBLISHED, msg_id=41464 +I (4484) MQTT_EXAMPLE: MQTT_EVENT_SUBSCRIBED, msg_id=17886 +I (4484) MQTT_EXAMPLE: sent publish successful, msg_id=0 +I (4684) MQTT_EXAMPLE: MQTT_EVENT_SUBSCRIBED, msg_id=42970 +I (4684) MQTT_EXAMPLE: sent publish successful, msg_id=0 +I (4884) MQTT_CLIENT: deliver_publish, message_length_read=19, message_length=19 +I (4884) MQTT_EXAMPLE: MQTT_EVENT_DATA +TOPIC=/topic/qos0 +DATA=data +I (5194) MQTT_CLIENT: deliver_publish, message_length_read=19, message_length=19 +I (5194) MQTT_EXAMPLE: MQTT_EVENT_DATA +TOPIC=/topic/qos0 +DATA=data +``` diff --git a/examples/protocols/mqtt/tcp/main/CMakeLists.txt b/examples/protocols/mqtt/tcp/main/CMakeLists.txt new file mode 100644 index 0000000..6b03500 --- /dev/null +++ b/examples/protocols/mqtt/tcp/main/CMakeLists.txt @@ -0,0 +1,4 @@ +set(COMPONENT_SRCS "app_main.c") +set(COMPONENT_ADD_INCLUDEDIRS ".") + +register_component() diff --git a/examples/protocols/mqtt/tcp/main/Kconfig.projbuild b/examples/protocols/mqtt/tcp/main/Kconfig.projbuild new file mode 100644 index 0000000..bb4194d --- /dev/null +++ b/examples/protocols/mqtt/tcp/main/Kconfig.projbuild @@ -0,0 +1,25 @@ +menu "Example Configuration" + +config WIFI_SSID + string "WiFi SSID" + default "myssid" + help + SSID (network name) for the example to connect to. + +config WIFI_PASSWORD + string "WiFi Password" + default "mypassword" + help + WiFi password (WPA or WPA2) for the example to use. + +config BROKER_URL + string "Broker URL" + default "mqtt://iot.eclipse.org" + help + URL of the broker to connect to + +config BROKER_URL_FROM_STDIN + bool + default y if BROKER_URL = "FROM_STDIN" + +endmenu diff --git a/examples/protocols/mqtt/tcp/main/app_main.c b/examples/protocols/mqtt/tcp/main/app_main.c new file mode 100644 index 0000000..0d294bd --- /dev/null +++ b/examples/protocols/mqtt/tcp/main/app_main.c @@ -0,0 +1,171 @@ +#include +#include +#include +#include +#include "esp_wifi.h" +#include "esp_system.h" +#include "nvs_flash.h" +#include "esp_event_loop.h" + +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "freertos/semphr.h" +#include "freertos/queue.h" +#include "freertos/event_groups.h" + +#include "lwip/sockets.h" +#include "lwip/dns.h" +#include "lwip/netdb.h" + +#include "esp_log.h" +#include "mqtt_client.h" + +static const char *TAG = "MQTT_EXAMPLE"; + +static EventGroupHandle_t wifi_event_group; +const static int CONNECTED_BIT = BIT0; + + +static esp_err_t mqtt_event_handler(esp_mqtt_event_handle_t event) +{ + esp_mqtt_client_handle_t client = event->client; + int msg_id; + // your_context_t *context = event->context; + switch (event->event_id) { + case MQTT_EVENT_CONNECTED: + ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED"); + msg_id = esp_mqtt_client_publish(client, "/topic/qos1", "data_3", 0, 1, 0); + ESP_LOGI(TAG, "sent publish successful, msg_id=%d", msg_id); + + msg_id = esp_mqtt_client_subscribe(client, "/topic/qos0", 0); + ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id); + + msg_id = esp_mqtt_client_subscribe(client, "/topic/qos1", 1); + ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id); + + msg_id = esp_mqtt_client_unsubscribe(client, "/topic/qos1"); + ESP_LOGI(TAG, "sent unsubscribe successful, msg_id=%d", msg_id); + break; + case MQTT_EVENT_DISCONNECTED: + ESP_LOGI(TAG, "MQTT_EVENT_DISCONNECTED"); + break; + + case MQTT_EVENT_SUBSCRIBED: + ESP_LOGI(TAG, "MQTT_EVENT_SUBSCRIBED, msg_id=%d", event->msg_id); + msg_id = esp_mqtt_client_publish(client, "/topic/qos0", "data", 0, 0, 0); + ESP_LOGI(TAG, "sent publish successful, msg_id=%d", msg_id); + break; + case MQTT_EVENT_UNSUBSCRIBED: + ESP_LOGI(TAG, "MQTT_EVENT_UNSUBSCRIBED, msg_id=%d", event->msg_id); + break; + case MQTT_EVENT_PUBLISHED: + ESP_LOGI(TAG, "MQTT_EVENT_PUBLISHED, msg_id=%d", event->msg_id); + break; + case MQTT_EVENT_DATA: + ESP_LOGI(TAG, "MQTT_EVENT_DATA"); + printf("TOPIC=%.*s\r\n", event->topic_len, event->topic); + printf("DATA=%.*s\r\n", event->data_len, event->data); + break; + case MQTT_EVENT_ERROR: + ESP_LOGI(TAG, "MQTT_EVENT_ERROR"); + break; + } + return ESP_OK; +} + +static esp_err_t wifi_event_handler(void *ctx, system_event_t *event) +{ + switch (event->event_id) { + case SYSTEM_EVENT_STA_START: + esp_wifi_connect(); + break; + case SYSTEM_EVENT_STA_GOT_IP: + xEventGroupSetBits(wifi_event_group, CONNECTED_BIT); + + break; + case SYSTEM_EVENT_STA_DISCONNECTED: + esp_wifi_connect(); + xEventGroupClearBits(wifi_event_group, CONNECTED_BIT); + break; + default: + break; + } + return ESP_OK; +} + +static void wifi_init(void) +{ + tcpip_adapter_init(); + wifi_event_group = xEventGroupCreate(); + ESP_ERROR_CHECK(esp_event_loop_init(wifi_event_handler, NULL)); + wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); + ESP_ERROR_CHECK(esp_wifi_init(&cfg)); + ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM)); + wifi_config_t wifi_config = { + .sta = { + .ssid = CONFIG_WIFI_SSID, + .password = CONFIG_WIFI_PASSWORD, + }, + }; + ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); + ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config)); + ESP_LOGI(TAG, "start the WIFI SSID:[%s]", CONFIG_WIFI_SSID); + ESP_ERROR_CHECK(esp_wifi_start()); + ESP_LOGI(TAG, "Waiting for wifi"); + xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT, false, true, portMAX_DELAY); +} + +static void mqtt_app_start(void) +{ + esp_mqtt_client_config_t mqtt_cfg = { + .uri = CONFIG_BROKER_URL, + .event_handle = mqtt_event_handler, + // .user_context = (void *)your_context + }; + +#if CONFIG_BROKER_URL_FROM_STDIN + char line[128]; + + if (strcmp(mqtt_cfg.uri, "FROM_STDIN") == 0) { + int count = 0; + printf("Please enter url of mqtt broker\n"); + while (count < 128) { + int c = fgetc(stdin); + if (c == '\n') { + line[count] = '\0'; + break; + } else if (c > 0 && c < 127) { + line[count] = c; + ++count; + } + vTaskDelay(10 / portTICK_PERIOD_MS); + } + mqtt_cfg.uri = line; + printf("Broker url: %s\n", line); + } else { + ESP_LOGE(TAG, "Configuration mismatch: wrong broker url"); + abort(); + } +#endif /* CONFIG_BROKER_URL_FROM_STDIN */ + + esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg); + esp_mqtt_client_start(client); +} + +void app_main() +{ + ESP_LOGI(TAG, "[APP] Startup.."); + ESP_LOGI(TAG, "[APP] Free memory: %d bytes", esp_get_free_heap_size()); + ESP_LOGI(TAG, "[APP] IDF version: %s", esp_get_idf_version()); + + esp_log_level_set("*", ESP_LOG_INFO); + esp_log_level_set("MQTT_CLIENT", ESP_LOG_VERBOSE); + esp_log_level_set("TRANSPORT_TCP", ESP_LOG_VERBOSE); + esp_log_level_set("TRANSPORT_SSL", ESP_LOG_VERBOSE); + esp_log_level_set("TRANSPORT", ESP_LOG_VERBOSE); + esp_log_level_set("OUTBOX", ESP_LOG_VERBOSE); + + nvs_flash_init(); + wifi_init(); + mqtt_app_start(); +} diff --git a/examples/protocols/mqtt/tcp/main/component.mk b/examples/protocols/mqtt/tcp/main/component.mk new file mode 100644 index 0000000..e69de29 diff --git a/examples/protocols/mqtt/tcp/mqtt_tcp_example_test.py b/examples/protocols/mqtt/tcp/mqtt_tcp_example_test.py new file mode 100644 index 0000000..c1bf42d --- /dev/null +++ b/examples/protocols/mqtt/tcp/mqtt_tcp_example_test.py @@ -0,0 +1,101 @@ +import re +import os +import sys +from socket import * +from threading import Thread +import time + +global msgid + +def get_my_ip(): + s1 = socket(AF_INET, SOCK_DGRAM) + s1.connect(("8.8.8.8", 80)) + my_ip = s1.getsockname()[0] + s1.close() + return my_ip + +def mqqt_server_sketch(my_ip, port): + global msgid + print("Starting the server on {}".format(my_ip)) + s=socket(AF_INET, SOCK_STREAM) + s.settimeout(60) + s.bind((my_ip, port)) + s.listen(1) + q,addr=s.accept() + q.settimeout(30) + print("connection accepted") + # q.send(g_msg_to_client) + data = q.recv(1024) + # check if received initial empty message + print("received from client {}".format(data)) + data = bytearray([0x20, 0x02, 0x00, 0x00]) + q.send(data) + # try to receive qos1 + data = q.recv(1024) + msgid = ord(data[15])*256+ord(data[16]) + print("received from client {}, msgid: {}".format(data, msgid)) + data = bytearray([0x40, 0x02, data[15], data[16]]) + q.send(data) + time.sleep(5) + s.close() + print("server closed") + +# 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 TinyFW +import IDF + + + + +@IDF.idf_example_test(env_tag="Example_WIFI") +def test_examples_protocol_mqtt_qos1(env, extra_data): + global msgid + """ + steps: (QoS1: Happy flow) + 1. start the broker broker (with correctly sending ACK) + 2. DUT client connects to a broker and publishes qos1 message + 3. Test evaluates that qos1 message is queued and removed from queued after ACK received + 4. Test the broker received the same message id evaluated in step 3 + """ + dut1 = env.get_dut("mqtt_tcp", "examples/protocols/mqtt/tcp") + # check and log bin size + binary_file = os.path.join(dut1.app.binary_path, "mqtt_tcp.bin") + bin_size = os.path.getsize(binary_file) + IDF.log_performance("mqtt_tcp_bin_size", "{}KB".format(bin_size//1024)) + IDF.check_performance("mqtt_tcp_size", bin_size//1024) + # 1. start mqtt broker sketch + host_ip = get_my_ip() + thread1 = Thread(target = mqqt_server_sketch, args = (host_ip,1883)) + thread1.start() + # 2. start the dut test and wait till client gets IP address + dut1.start_app() + # waiting for getting the IP address + data = dut1.expect(re.compile(r" sta ip: ([^,]+),"), timeout=30) + # time.sleep(15) + print ("writing to device: {}".format("mqtt://" + host_ip + "\n")) + dut1.write("mqtt://" + host_ip + "\n") + thread1.join() + print ("Message id received from server: {}".format(msgid)) + # 3. check the message id was enqueued and then deleted + msgid_enqueued = dut1.expect(re.compile(r"OUTBOX: ENQUEUE msgid=([0-9]+)"), timeout=30) + # expect_txt="OUTBOX: ENQUEUE msgid=" + str(msgid) + # dut1.expect(re.compile(expect_txt), timeout=30) + msgid_deleted = dut1.expect(re.compile(r"OUTBOX: DELETED msgid=([0-9]+)"), timeout=30) + # expect_txt="OUTBOX: DELETED msgid=" + str(msgid) + # dut1.expect(re.compile(expect_txt), timeout=30) + # 4. check the msgid of received data are the same as that of enqueued and deleted from outbox + if (msgid_enqueued[0] == str(msgid) and msgid_deleted[0] == str(msgid)): + print("PASS: Received correct msg id") + else: + print("Failure!") + raise ValueError('Mismatch of msgid: received: {}, enqueued {}, deleted {}'.format(msgid, msgid_enqueued, msgid_deleted)) + +if __name__ == '__main__': + test_examples_protocol_mqtt_qos1() diff --git a/examples/protocols/mqtt/tcp/sdkconfig b/examples/protocols/mqtt/tcp/sdkconfig new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/examples/protocols/mqtt/tcp/sdkconfig @@ -0,0 +1 @@ + diff --git a/examples/protocols/mqtt/tcp/sdkconfig.defaults b/examples/protocols/mqtt/tcp/sdkconfig.defaults new file mode 100644 index 0000000..51c8139 --- /dev/null +++ b/examples/protocols/mqtt/tcp/sdkconfig.defaults @@ -0,0 +1,7 @@ +CONFIG_BROKER_URL="FROM_STDIN" +CONFIG_LOG_DEFAULT_LEVEL_NONE= +CONFIG_LOG_DEFAULT_LEVEL_ERROR= +CONFIG_LOG_DEFAULT_LEVEL_WARN= +CONFIG_LOG_DEFAULT_LEVEL_INFO= +CONFIG_LOG_DEFAULT_LEVEL_DEBUG=y +CONFIG_LOG_DEFAULT_LEVEL_VERBOSE= diff --git a/examples/protocols/mqtt/ws/CMakeLists.txt b/examples/protocols/mqtt/ws/CMakeLists.txt new file mode 100644 index 0000000..58a2135 --- /dev/null +++ b/examples/protocols/mqtt/ws/CMakeLists.txt @@ -0,0 +1,7 @@ +# The following four lines of boilerplate have to be in your project's CMakeLists +# in this exact order for cmake to work correctly +cmake_minimum_required(VERSION 3.5) + +include($ENV{IDF_PATH}/tools/cmake/project.cmake) + +project(mqtt_websocket) \ No newline at end of file diff --git a/examples/protocols/mqtt/ws/Makefile b/examples/protocols/mqtt/ws/Makefile new file mode 100644 index 0000000..668719b --- /dev/null +++ b/examples/protocols/mqtt/ws/Makefile @@ -0,0 +1,7 @@ +# +# This is a project Makefile. It is assumed the directory this Makefile resides in is a +# project subdirectory. +# +PROJECT_NAME := mqtt_websocket + +include $(IDF_PATH)/make/project.mk diff --git a/examples/protocols/mqtt/ws/README.md b/examples/protocols/mqtt/ws/README.md new file mode 100644 index 0000000..619519d --- /dev/null +++ b/examples/protocols/mqtt/ws/README.md @@ -0,0 +1,62 @@ +# ESP-MQTT MQTT over Websocket + +(See the README.md file in the upper level 'examples' directory for more information about examples.) + +This example connects to the broker iot.eclipse.org over web sockets as a demonstration subscribes/unsubscribes and send a message on certain topic. + +It uses ESP-MQTT library which implements mqtt client to connect to mqtt broker. + +## How to use example + +### Hardware Required + +This example can be executed on any ESP32 board, the only required interface is WiFi and connection to internet. + +### Configure the project + +``` +make menuconfig +``` + +* Set serial port under Serial Flasher Options. + +* Set ssid and password for the board to connect to AP. + +### Build and Flash + +Build the project and flash it to the board, then run monitor tool to view serial output: + +``` +make -j4 flash monitor +``` + +(To exit the serial monitor, type ``Ctrl-]``.) + +See the Getting Started Guide for full steps to configure and use ESP-IDF to build projects. + +## Example Output + +``` +I (3714) event: sta ip: 192.168.0.139, mask: 255.255.255.0, gw: 192.168.0.2 +I (3714) system_api: Base MAC address is not set, read default base MAC address from BLK0 of EFUSE +I (3964) MQTT_CLIENT: Sending MQTT CONNECT message, type: 1, id: 0000 +I (4164) MQTTWS_EXAMPLE: MQTT_EVENT_CONNECTED +I (4174) MQTTWS_EXAMPLE: sent publish successful, msg_id=41464 +I (4174) MQTTWS_EXAMPLE: sent subscribe successful, msg_id=17886 +I (4174) MQTTWS_EXAMPLE: sent subscribe successful, msg_id=42970 +I (4184) MQTTWS_EXAMPLE: sent unsubscribe successful, msg_id=50241 +I (4314) MQTTWS_EXAMPLE: MQTT_EVENT_PUBLISHED, msg_id=41464 +I (4484) MQTTWS_EXAMPLE: MQTT_EVENT_SUBSCRIBED, msg_id=17886 +I (4484) MQTTWS_EXAMPLE: sent publish successful, msg_id=0 +I (4684) MQTTWS_EXAMPLE: MQTT_EVENT_SUBSCRIBED, msg_id=42970 +I (4684) MQTTWS_EXAMPLE: sent publish successful, msg_id=0 +I (4884) MQTT_CLIENT: deliver_publish, message_length_read=19, message_length=19 +I (4884) MQTTWS_EXAMPLE: MQTT_EVENT_DATA +TOPIC=/topic/qos0 +DATA=data +I (5194) MQTT_CLIENT: deliver_publish, message_length_read=19, message_length=19 +I (5194) MQTTWS_EXAMPLE: MQTT_EVENT_DATA +TOPIC=/topic/qos0 +DATA=data +``` + diff --git a/examples/protocols/mqtt/ws/main/CMakeLists.txt b/examples/protocols/mqtt/ws/main/CMakeLists.txt new file mode 100644 index 0000000..6b03500 --- /dev/null +++ b/examples/protocols/mqtt/ws/main/CMakeLists.txt @@ -0,0 +1,4 @@ +set(COMPONENT_SRCS "app_main.c") +set(COMPONENT_ADD_INCLUDEDIRS ".") + +register_component() diff --git a/examples/protocols/mqtt/ws/main/Kconfig.projbuild b/examples/protocols/mqtt/ws/main/Kconfig.projbuild new file mode 100644 index 0000000..176d8fb --- /dev/null +++ b/examples/protocols/mqtt/ws/main/Kconfig.projbuild @@ -0,0 +1,15 @@ +menu "Example Configuration" + +config WIFI_SSID + string "WiFi SSID" + default "myssid" + help + SSID (network name) for the example to connect to. + +config WIFI_PASSWORD + string "WiFi Password" + default "mypassword" + help + WiFi password (WPA or WPA2) for the example to use. + +endmenu diff --git a/examples/protocols/mqtt/ws/main/app_main.c b/examples/protocols/mqtt/ws/main/app_main.c new file mode 100644 index 0000000..cdb0f0a --- /dev/null +++ b/examples/protocols/mqtt/ws/main/app_main.c @@ -0,0 +1,144 @@ +#include +#include +#include +#include +#include "esp_wifi.h" +#include "esp_system.h" +#include "nvs_flash.h" +#include "esp_event_loop.h" + +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "freertos/semphr.h" +#include "freertos/queue.h" +#include "freertos/event_groups.h" + +#include "lwip/sockets.h" +#include "lwip/dns.h" +#include "lwip/netdb.h" + +#include "esp_log.h" +#include "mqtt_client.h" + +static const char *TAG = "MQTTWS_EXAMPLE"; + +static EventGroupHandle_t wifi_event_group; +const static int CONNECTED_BIT = BIT0; + + +static esp_err_t mqtt_event_handler(esp_mqtt_event_handle_t event) +{ + esp_mqtt_client_handle_t client = event->client; + int msg_id; + // your_context_t *context = event->context; + switch (event->event_id) { + case MQTT_EVENT_CONNECTED: + ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED"); + msg_id = esp_mqtt_client_subscribe(client, "/topic/qos0", 0); + ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id); + + msg_id = esp_mqtt_client_subscribe(client, "/topic/qos1", 1); + ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id); + + msg_id = esp_mqtt_client_unsubscribe(client, "/topic/qos1"); + ESP_LOGI(TAG, "sent unsubscribe successful, msg_id=%d", msg_id); + break; + case MQTT_EVENT_DISCONNECTED: + ESP_LOGI(TAG, "MQTT_EVENT_DISCONNECTED"); + break; + + case MQTT_EVENT_SUBSCRIBED: + ESP_LOGI(TAG, "MQTT_EVENT_SUBSCRIBED, msg_id=%d", event->msg_id); + msg_id = esp_mqtt_client_publish(client, "/topic/qos0", "data", 0, 0, 0); + ESP_LOGI(TAG, "sent publish successful, msg_id=%d", msg_id); + break; + case MQTT_EVENT_UNSUBSCRIBED: + ESP_LOGI(TAG, "MQTT_EVENT_UNSUBSCRIBED, msg_id=%d", event->msg_id); + break; + case MQTT_EVENT_PUBLISHED: + ESP_LOGI(TAG, "MQTT_EVENT_PUBLISHED, msg_id=%d", event->msg_id); + break; + case MQTT_EVENT_DATA: + ESP_LOGI(TAG, "MQTT_EVENT_DATA"); + printf("TOPIC=%.*s\r\n", event->topic_len, event->topic); + printf("DATA=%.*s\r\n", event->data_len, event->data); + break; + case MQTT_EVENT_ERROR: + ESP_LOGI(TAG, "MQTT_EVENT_ERROR"); + break; + } + return ESP_OK; +} + +static esp_err_t wifi_event_handler(void *ctx, system_event_t *event) +{ + switch (event->event_id) { + case SYSTEM_EVENT_STA_START: + esp_wifi_connect(); + break; + case SYSTEM_EVENT_STA_GOT_IP: + xEventGroupSetBits(wifi_event_group, CONNECTED_BIT); + + break; + case SYSTEM_EVENT_STA_DISCONNECTED: + esp_wifi_connect(); + xEventGroupClearBits(wifi_event_group, CONNECTED_BIT); + break; + default: + break; + } + return ESP_OK; +} + +static void wifi_init(void) +{ + tcpip_adapter_init(); + wifi_event_group = xEventGroupCreate(); + ESP_ERROR_CHECK(esp_event_loop_init(wifi_event_handler, NULL)); + wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); + ESP_ERROR_CHECK(esp_wifi_init(&cfg)); + ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM)); + wifi_config_t wifi_config = { + .sta = { + .ssid = CONFIG_WIFI_SSID, + .password = CONFIG_WIFI_PASSWORD, + }, + }; + ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); + ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config)); + ESP_LOGI(TAG, "start the WIFI SSID:[%s]", CONFIG_WIFI_SSID); + ESP_ERROR_CHECK(esp_wifi_start()); + ESP_LOGI(TAG, "Waiting for wifi"); + xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT, false, true, portMAX_DELAY); +} + +static void mqtt_app_start(void) +{ + const esp_mqtt_client_config_t mqtt_cfg = { + .uri = "ws://iot.eclipse.org:80/ws", + .event_handle = mqtt_event_handler, + // .user_context = (void *)your_context + }; + + esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg); + esp_mqtt_client_start(client); +} + +void app_main() +{ + ESP_LOGI(TAG, "[APP] Startup.."); + ESP_LOGI(TAG, "[APP] Free memory: %d bytes", esp_get_free_heap_size()); + ESP_LOGI(TAG, "[APP] IDF version: %s", esp_get_idf_version()); + + esp_log_level_set("*", ESP_LOG_INFO); + esp_log_level_set("MQTT_CLIENT", ESP_LOG_VERBOSE); + esp_log_level_set("TRANSPORT_TCP", ESP_LOG_VERBOSE); + esp_log_level_set("TRANSPORT_SSL", ESP_LOG_VERBOSE); + esp_log_level_set("TRANSPORT_WS", ESP_LOG_VERBOSE); + esp_log_level_set("TRANSPORT", ESP_LOG_VERBOSE); + esp_log_level_set("OUTBOX", ESP_LOG_VERBOSE); + + nvs_flash_init(); + wifi_init(); + mqtt_app_start(); +} diff --git a/examples/protocols/mqtt/ws/main/component.mk b/examples/protocols/mqtt/ws/main/component.mk new file mode 100644 index 0000000..e69de29 diff --git a/examples/protocols/mqtt/ws/mqtt_ws_example_test.py b/examples/protocols/mqtt/ws/mqtt_ws_example_test.py new file mode 100644 index 0000000..f00e4dd --- /dev/null +++ b/examples/protocols/mqtt/ws/mqtt_ws_example_test.py @@ -0,0 +1,128 @@ +import re +import os +import sys +import time +import socket +import imp + +use_mqtt_client_sketch = False + +try: + imp.find_module('paho') + import paho.mqtt.client as mqtt + # Make things with supposed existing module +except ImportError: + use_mqtt_client_sketch = True + pass + +global g_recv_topic +global g_recv_data + +g_recv_data="" + +# This is only a workaround for running mqtt client with 'hardcoded' data using plain socket interface +def mqtt_client_sketch(): + global g_recv_topic + global g_recv_data + http_connect = bytearray([ 0x47, 0x45, 0x54, 0x20, 0x2f, 0x77, 0x73, 0x20, 0x48, 0x54, 0x54, 0x50, 0x2f, 0x31, 0x2e, 0x31, 0x0d, 0x0a, 0x4f, 0x72, 0x69, 0x67, 0x69, 0x6e, 0x3a, 0x20, 0x68, 0x74, 0x74, 0x70, 0x73, 0x3a, 0x2f, 0x2f, 0x69, 0x6f, 0x74, 0x2e, 0x65, 0x63, 0x6c, 0x69, 0x70, 0x73, 0x65, 0x2e, 0x6f, 0x72, 0x67, 0x3a, 0x38, 0x30, 0x0d, 0x0a, 0x55, 0x70, 0x67, 0x72, 0x61, 0x64, 0x65, 0x3a, 0x20, 0x77, 0x65, 0x62, 0x73, 0x6f, 0x63, 0x6b, 0x65, 0x74, 0x0d, 0x0a, 0x53, 0x65, 0x63, 0x2d, 0x57, 0x65, 0x62, 0x73, 0x6f, 0x63, 0x6b, 0x65, 0x74, 0x2d, 0x56, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x3a, 0x20, 0x31, 0x33, 0x0d, 0x0a, 0x53, 0x65, 0x63, 0x2d, 0x57, 0x65, 0x62, 0x73, 0x6f, 0x63, 0x6b, 0x65, 0x74, 0x2d, 0x50, 0x72, 0x6f, 0x74, 0x6f, 0x63, 0x6f, 0x6c, 0x3a, 0x20, 0x6d, 0x71, 0x74, 0x74, 0x0d, 0x0a, 0x43, 0x6f, 0x6e, 0x6e, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x3a, 0x20, 0x55, 0x70, 0x67, 0x72, 0x61, 0x64, 0x65, 0x0d, 0x0a, 0x53, 0x65, 0x63, 0x2d, 0x57, 0x65, 0x62, 0x53, 0x6f, 0x63, 0x6b, 0x65, 0x74, 0x2d, 0x4b, 0x65, 0x79, 0x3a, 0x20, 0x6c, 0x35, 0x61, 0x50, 0x41, 0x64, 0x6d, 0x4a, 0x52, 0x65, 0x32, 0x79, 0x55, 0x42, 0x79, 0x68, 0x37, 0x35, 0x72, 0x58, 0x68, 0x51, 0x3d, 0x3d, 0x0d, 0x0a, 0x48, 0x6f, 0x73, 0x74, 0x3a, 0x20, 0x69, 0x6f, 0x74, 0x2e, 0x65, 0x63, 0x6c, 0x69, 0x70, 0x73, 0x65, 0x2e, 0x6f, 0x72, 0x67, 0x3a, 0x38, 0x30, 0x0d, 0x0a, 0x0d, 0x0a]) + connect_msg = bytearray([0x82, 0x8e, 0x82, 0x1a, 0xe6, 0x22, 0x92, 0x16, 0xe6, 0x26, 0xcf, 0x4b, 0xb2, 0x76, 0x86, 0x18, 0xe6, 0x1e, 0x82, 0x1a]) + send_qos0_msg = bytearray([ 0x82, 0x9c, 0x44, 0x78, 0xdf, 0x8e, 0x74, 0x62, 0xdf, 0x85, 0x6b, 0x0c, 0xb0, 0xfe, 0x2d, 0x1b, 0xf0, 0xff, 0x2b, 0x0b, 0xef, 0xea, 0x25, 0x0c, 0xbe, 0xd1, 0x30, 0x17, 0x80, 0xeb, 0x37, 0x08, 0xec, 0xbc ]) + subscribe_qos0 = bytearray([ 0x82, 0x92, 0x8e, 0x31, 0x8c, 0x4a, 0x0c, 0x21, 0x8c, 0x4b, 0x8e, 0x3a, 0xa3, 0x3e, 0xe1, 0x41, 0xe5, 0x29, 0xa1, 0x40, 0xe3, 0x39, 0xbe, 0x31] ) + cli = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + cli.settimeout(30) + cli.connect(("iot.eclipse.org", 80)) + cli.send(http_connect) + cli.send(connect_msg) + data = cli.recv(1024) + print("Connect ack received {}".format(data)) + cli.send(subscribe_qos0) + data = cli.recv(1024) + print("Subscibe ack received {}".format(data)) + start = time.time() + while (time.time() - start) <= 20: + data = cli.recv(1024) + print("Data received {}".format(data[-17:])) + if data[-15:] == "/topic/qos0data": + g_recv_topic = data[-15:][:11] + g_recv_data = data[-4:] + cli.send(send_qos0_msg) + data = cli.recv(1024) + print("data ack received {}".format(data)) + break + cli.close() + +# 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)) + client.subscribe("/topic/qos0") + +# The callback for when a PUBLISH message is received from the server. +def on_message(client, userdata, msg): + global g_recv_topic + global g_recv_data + if g_recv_data == "" and msg.payload == "data": + client.publish("/topic/qos0", "data_to_esp32") + g_recv_topic = msg.topic + g_recv_data = msg.payload + print(msg.topic+" "+str(msg.payload)) + +# 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 TinyFW +import IDF + + + + +@IDF.idf_example_test(env_tag="Example_WIFI") +def test_examples_protocol_mqtt_ws(env, extra_data): + global g_recv_topic + global g_recv_data + """ + steps: | + 1. join AP and connects to ws broker + 2. Test connects a client to the same broker + 3. Test evaluates it received correct qos0 message + 4. Test ESP32 client received correct qos0 message + """ + dut1 = env.get_dut("mqtt_websocket", "examples/protocols/mqtt/ws") + # check and log bin size + binary_file = os.path.join(dut1.app.binary_path, "mqtt_websocket.bin") + bin_size = os.path.getsize(binary_file) + IDF.log_performance("mqtt_websocket_bin_size", "{}KB".format(bin_size//1024)) + IDF.check_performance("mqtt_websocket_size", bin_size//1024) + # 1. start test + dut1.start_app() + # 2. Test connects to a broker + if use_mqtt_client_sketch: + mqtt_client_sketch() + else: + client = mqtt.Client(transport="websockets") + client.on_connect = on_connect + client.on_message = on_message + client.ws_set_options(path="/ws", headers=None) + print "Connecting..." + client.connect("iot.eclipse.org", 80, 60) + print "...done" + print "Start Looping..." + start = time.time() + while (time.time() - start) <= 20: + client.loop() + print "...done" + # 3. check the message received back from the server + if g_recv_topic == "/topic/qos0" and g_recv_data == "data" : + print("PASS: Received correct message") + else: + print("Failure!") + raise ValueError('Wrong data received topic: {}, data:{}'.format(g_recv_topic, g_recv_data)) + # 4. check that the esp32 client received data sent by this python client + dut1.expect(re.compile(r"DATA=data_to_esp32"), timeout=30) + +if __name__ == '__main__': + test_examples_protocol_mqtt_ws() diff --git a/examples/protocols/mqtt/wss/CMakeLists.txt b/examples/protocols/mqtt/wss/CMakeLists.txt new file mode 100644 index 0000000..7ba5e62 --- /dev/null +++ b/examples/protocols/mqtt/wss/CMakeLists.txt @@ -0,0 +1,9 @@ +# The following four lines of boilerplate have to be in your project's CMakeLists +# in this exact order for cmake to work correctly +cmake_minimum_required(VERSION 3.5) + +include($ENV{IDF_PATH}/tools/cmake/project.cmake) + +project(mqtt_websocket_secure) + +target_add_binary_data(mqtt_websocket_secure.elf "main/iot_eclipse_org.pem" TEXT) diff --git a/examples/protocols/mqtt/wss/Makefile b/examples/protocols/mqtt/wss/Makefile new file mode 100644 index 0000000..27047d0 --- /dev/null +++ b/examples/protocols/mqtt/wss/Makefile @@ -0,0 +1,7 @@ +# +# This is a project Makefile. It is assumed the directory this Makefile resides in is a +# project subdirectory. +# +PROJECT_NAME := mqtt_websocket_secure + +include $(IDF_PATH)/make/project.mk diff --git a/examples/protocols/mqtt/wss/README.md b/examples/protocols/mqtt/wss/README.md new file mode 100644 index 0000000..43d829c --- /dev/null +++ b/examples/protocols/mqtt/wss/README.md @@ -0,0 +1,69 @@ +# ESP-MQTT MQTT over WSS Sample application +(See the README.md file in the upper level 'examples' directory for more information about examples.) + +This example connects to the broker iot.eclipse.org over secure websockets and as a demonstration subscribes/unsubscribes and send a message on certain topic. + +It uses ESP-MQTT library which implements mqtt client to connect to mqtt broker. + +## How to use example + +### Hardware Required + +This example can be executed on any ESP32 board, the only required interface is WiFi and connection to internet. + +### Configure the project + +``` +make menuconfig +``` + +* Set serial port under Serial Flasher Options. + +* Set ssid and password for the board to connect to AP. + +Note how to create a PEM certificate for iot.eclipse.org: + +``` +openssl s_client -showcerts -connect iot.eclipse.org:8883 /dev/null|openssl x509 -outform PEM >iot_eclipse_org.pem +``` + +### Build and Flash + +Build the project and flash it to the board, then run monitor tool to view serial output: + +``` +make -j4 flash monitor +``` + +(To exit the serial monitor, type ``Ctrl-]``.) + +See the Getting Started Guide for full steps to configure and use ESP-IDF to build projects. + +## Example Output + +``` +I (3714) event: sta ip: 192.168.0.139, mask: 255.255.255.0, gw: 192.168.0.2 +I (3714) system_api: Base MAC address is not set, read default base MAC address from BLK0 of EFUSE +I (3964) MQTT_CLIENT: Sending MQTT CONNECT message, type: 1, id: 0000 +I (4164) MQTTWSS_EXAMPLE: MQTT_EVENT_CONNECTED +I (4174) MQTTWSS_EXAMPLE: sent publish successful, msg_id=41464 +I (4174) MQTTWSS_EXAMPLE: sent subscribe successful, msg_id=17886 +I (4174) MQTTWSS_EXAMPLE: sent subscribe successful, msg_id=42970 +I (4184) MQTTWSS_EXAMPLE: sent unsubscribe successful, msg_id=50241 +I (4314) MQTTWSS_EXAMPLE: MQTT_EVENT_PUBLISHED, msg_id=41464 +I (4484) MQTTWSS_EXAMPLE: MQTT_EVENT_SUBSCRIBED, msg_id=17886 +I (4484) MQTTWSS_EXAMPLE: sent publish successful, msg_id=0 +I (4684) MQTTWSS_EXAMPLE: MQTT_EVENT_SUBSCRIBED, msg_id=42970 +I (4684) MQTTWSS_EXAMPLE: sent publish successful, msg_id=0 +I (4884) MQTT_CLIENT: deliver_publish, message_length_read=19, message_length=19 +I (4884) MQTTWSS_EXAMPLE: MQTT_EVENT_DATA +TOPIC=/topic/qos0 +DATA=data +I (5194) MQTT_CLIENT: deliver_publish, message_length_read=19, message_length=19 +I (5194) MQTTWSS_EXAMPLE: MQTT_EVENT_DATA +TOPIC=/topic/qos0 +DATA=data +``` + + + diff --git a/examples/protocols/mqtt/wss/main/CMakeLists.txt b/examples/protocols/mqtt/wss/main/CMakeLists.txt new file mode 100644 index 0000000..6b03500 --- /dev/null +++ b/examples/protocols/mqtt/wss/main/CMakeLists.txt @@ -0,0 +1,4 @@ +set(COMPONENT_SRCS "app_main.c") +set(COMPONENT_ADD_INCLUDEDIRS ".") + +register_component() diff --git a/examples/protocols/mqtt/wss/main/Kconfig.projbuild b/examples/protocols/mqtt/wss/main/Kconfig.projbuild new file mode 100644 index 0000000..176d8fb --- /dev/null +++ b/examples/protocols/mqtt/wss/main/Kconfig.projbuild @@ -0,0 +1,15 @@ +menu "Example Configuration" + +config WIFI_SSID + string "WiFi SSID" + default "myssid" + help + SSID (network name) for the example to connect to. + +config WIFI_PASSWORD + string "WiFi Password" + default "mypassword" + help + WiFi password (WPA or WPA2) for the example to use. + +endmenu diff --git a/examples/protocols/mqtt/wss/main/app_main.c b/examples/protocols/mqtt/wss/main/app_main.c new file mode 100644 index 0000000..bc1d045 --- /dev/null +++ b/examples/protocols/mqtt/wss/main/app_main.c @@ -0,0 +1,148 @@ +#include +#include +#include +#include +#include "esp_wifi.h" +#include "esp_system.h" +#include "nvs_flash.h" +#include "esp_event_loop.h" + +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "freertos/semphr.h" +#include "freertos/queue.h" +#include "freertos/event_groups.h" + +#include "lwip/sockets.h" +#include "lwip/dns.h" +#include "lwip/netdb.h" + +#include "esp_log.h" +#include "mqtt_client.h" + +static const char *TAG = "MQTTWSS_EXAMPLE"; + +static EventGroupHandle_t wifi_event_group; +const static int CONNECTED_BIT = BIT0; + + + +static esp_err_t wifi_event_handler(void *ctx, system_event_t *event) +{ + switch (event->event_id) { + case SYSTEM_EVENT_STA_START: + esp_wifi_connect(); + break; + case SYSTEM_EVENT_STA_GOT_IP: + xEventGroupSetBits(wifi_event_group, CONNECTED_BIT); + + break; + case SYSTEM_EVENT_STA_DISCONNECTED: + esp_wifi_connect(); + xEventGroupClearBits(wifi_event_group, CONNECTED_BIT); + break; + default: + break; + } + return ESP_OK; +} + +static void wifi_init(void) +{ + tcpip_adapter_init(); + wifi_event_group = xEventGroupCreate(); + ESP_ERROR_CHECK(esp_event_loop_init(wifi_event_handler, NULL)); + wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); + ESP_ERROR_CHECK(esp_wifi_init(&cfg)); + ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM)); + wifi_config_t wifi_config = { + .sta = { + .ssid = CONFIG_WIFI_SSID, + .password = CONFIG_WIFI_PASSWORD, + }, + }; + ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); + ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config)); + ESP_LOGI(TAG, "start the WIFI SSID:[%s]", CONFIG_WIFI_SSID); + ESP_ERROR_CHECK(esp_wifi_start()); + ESP_LOGI(TAG, "Waiting for wifi"); + xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT, false, true, portMAX_DELAY); +} + +extern const uint8_t iot_eclipse_org_pem_start[] asm("_binary_iot_eclipse_org_pem_start"); +extern const uint8_t iot_eclipse_org_pem_end[] asm("_binary_iot_eclipse_org_pem_end"); + +static esp_err_t mqtt_event_handler(esp_mqtt_event_handle_t event) +{ + esp_mqtt_client_handle_t client = event->client; + int msg_id; + // your_context_t *context = event->context; + switch (event->event_id) { + case MQTT_EVENT_CONNECTED: + ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED"); + msg_id = esp_mqtt_client_subscribe(client, "/topic/qos0", 0); + ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id); + + msg_id = esp_mqtt_client_subscribe(client, "/topic/qos1", 1); + ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id); + + msg_id = esp_mqtt_client_unsubscribe(client, "/topic/qos1"); + ESP_LOGI(TAG, "sent unsubscribe successful, msg_id=%d", msg_id); + break; + case MQTT_EVENT_DISCONNECTED: + ESP_LOGI(TAG, "MQTT_EVENT_DISCONNECTED"); + break; + + case MQTT_EVENT_SUBSCRIBED: + ESP_LOGI(TAG, "MQTT_EVENT_SUBSCRIBED, msg_id=%d", event->msg_id); + msg_id = esp_mqtt_client_publish(client, "/topic/qos0", "data", 0, 0, 0); + ESP_LOGI(TAG, "sent publish successful, msg_id=%d", msg_id); + break; + case MQTT_EVENT_UNSUBSCRIBED: + ESP_LOGI(TAG, "MQTT_EVENT_UNSUBSCRIBED, msg_id=%d", event->msg_id); + break; + case MQTT_EVENT_PUBLISHED: + ESP_LOGI(TAG, "MQTT_EVENT_PUBLISHED, msg_id=%d", event->msg_id); + break; + case MQTT_EVENT_DATA: + ESP_LOGI(TAG, "MQTT_EVENT_DATA"); + printf("TOPIC=%.*s\r\n", event->topic_len, event->topic); + printf("DATA=%.*s\r\n", event->data_len, event->data); + break; + case MQTT_EVENT_ERROR: + ESP_LOGI(TAG, "MQTT_EVENT_ERROR"); + break; + } + return ESP_OK; +} + +static void mqtt_app_start(void) +{ + const esp_mqtt_client_config_t mqtt_cfg = { + .uri = "wss://iot.eclipse.org:443/ws", + .event_handle = mqtt_event_handler, + .cert_pem = (const char *)iot_eclipse_org_pem_start, + }; + + ESP_LOGI(TAG, "[APP] Free memory: %d bytes", esp_get_free_heap_size()); + esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg); + esp_mqtt_client_start(client); +} + +void app_main() +{ + ESP_LOGI(TAG, "[APP] Startup.."); + ESP_LOGI(TAG, "[APP] Free memory: %d bytes", esp_get_free_heap_size()); + ESP_LOGI(TAG, "[APP] IDF version: %s", esp_get_idf_version()); + + esp_log_level_set("*", ESP_LOG_INFO); + esp_log_level_set("MQTT_CLIENT", ESP_LOG_VERBOSE); + esp_log_level_set("TRANSPORT_TCP", ESP_LOG_VERBOSE); + esp_log_level_set("TRANSPORT_SSL", ESP_LOG_VERBOSE); + esp_log_level_set("TRANSPORT", ESP_LOG_VERBOSE); + esp_log_level_set("OUTBOX", ESP_LOG_VERBOSE); + + nvs_flash_init(); + wifi_init(); + mqtt_app_start(); +} diff --git a/examples/protocols/mqtt/wss/main/component.mk b/examples/protocols/mqtt/wss/main/component.mk new file mode 100644 index 0000000..797c4a1 --- /dev/null +++ b/examples/protocols/mqtt/wss/main/component.mk @@ -0,0 +1 @@ +COMPONENT_EMBED_TXTFILES := iot_eclipse_org.pem diff --git a/examples/protocols/mqtt/wss/main/iot_eclipse_org.pem b/examples/protocols/mqtt/wss/main/iot_eclipse_org.pem new file mode 100644 index 0000000..edb593b --- /dev/null +++ b/examples/protocols/mqtt/wss/main/iot_eclipse_org.pem @@ -0,0 +1,27 @@ +-----BEGIN CERTIFICATE----- +MIIEkjCCA3qgAwIBAgIQCgFBQgAAAVOFc2oLheynCDANBgkqhkiG9w0BAQsFADA/ +MSQwIgYDVQQKExtEaWdpdGFsIFNpZ25hdHVyZSBUcnVzdCBDby4xFzAVBgNVBAMT +DkRTVCBSb290IENBIFgzMB4XDTE2MDMxNzE2NDA0NloXDTIxMDMxNzE2NDA0Nlow +SjELMAkGA1UEBhMCVVMxFjAUBgNVBAoTDUxldCdzIEVuY3J5cHQxIzAhBgNVBAMT +GkxldCdzIEVuY3J5cHQgQXV0aG9yaXR5IFgzMIIBIjANBgkqhkiG9w0BAQEFAAOC +AQ8AMIIBCgKCAQEAnNMM8FrlLke3cl03g7NoYzDq1zUmGSXhvb418XCSL7e4S0EF +q6meNQhY7LEqxGiHC6PjdeTm86dicbp5gWAf15Gan/PQeGdxyGkOlZHP/uaZ6WA8 +SMx+yk13EiSdRxta67nsHjcAHJyse6cF6s5K671B5TaYucv9bTyWaN8jKkKQDIZ0 +Z8h/pZq4UmEUEz9l6YKHy9v6Dlb2honzhT+Xhq+w3Brvaw2VFn3EK6BlspkENnWA +a6xK8xuQSXgvopZPKiAlKQTGdMDQMc2PMTiVFrqoM7hD8bEfwzB/onkxEz0tNvjj +/PIzark5McWvxI0NHWQWM6r6hCm21AvA2H3DkwIDAQABo4IBfTCCAXkwEgYDVR0T +AQH/BAgwBgEB/wIBADAOBgNVHQ8BAf8EBAMCAYYwfwYIKwYBBQUHAQEEczBxMDIG +CCsGAQUFBzABhiZodHRwOi8vaXNyZy50cnVzdGlkLm9jc3AuaWRlbnRydXN0LmNv +bTA7BggrBgEFBQcwAoYvaHR0cDovL2FwcHMuaWRlbnRydXN0LmNvbS9yb290cy9k +c3Ryb290Y2F4My5wN2MwHwYDVR0jBBgwFoAUxKexpHsscfrb4UuQdf/EFWCFiRAw +VAYDVR0gBE0wSzAIBgZngQwBAgEwPwYLKwYBBAGC3xMBAQEwMDAuBggrBgEFBQcC +ARYiaHR0cDovL2Nwcy5yb290LXgxLmxldHNlbmNyeXB0Lm9yZzA8BgNVHR8ENTAz +MDGgL6AthitodHRwOi8vY3JsLmlkZW50cnVzdC5jb20vRFNUUk9PVENBWDNDUkwu +Y3JsMB0GA1UdDgQWBBSoSmpjBH3duubRObemRWXv86jsoTANBgkqhkiG9w0BAQsF +AAOCAQEA3TPXEfNjWDjdGBX7CVW+dla5cEilaUcne8IkCJLxWh9KEik3JHRRHGJo +uM2VcGfl96S8TihRzZvoroed6ti6WqEBmtzw3Wodatg+VyOeph4EYpr/1wXKtx8/ +wApIvJSwtmVi4MFU5aMqrSDE6ea73Mj2tcMyo5jMd6jmeWUHK8so/joWUoHOUgwu +X4Po1QYz+3dszkDqMp4fklxBwXRsW10KXzPMTZ+sOPAveyxindmjkW8lGy+QsRlG +PfZ+G6Z6h7mjem0Y+iWlkYcV4PIWL1iwBi8saCbGS5jN2p8M+X+Q7UNKEkROb3N6 +KOqkqm57TH2H3eDJAkSnh6/DNFu0Qg== +-----END CERTIFICATE----- \ No newline at end of file diff --git a/examples/protocols/mqtt/wss/mqtt_wss_example_test.py b/examples/protocols/mqtt/wss/mqtt_wss_example_test.py new file mode 100644 index 0000000..9c153cf --- /dev/null +++ b/examples/protocols/mqtt/wss/mqtt_wss_example_test.py @@ -0,0 +1,132 @@ +import re +import os +import sys +import time +import socket +import imp +import ssl + +use_mqtt_client_sketch = False + +try: + imp.find_module('paho') + import paho.mqtt.client as mqtt + # Make things with supposed existing module +except ImportError: + use_mqtt_client_sketch = True + pass + +global g_recv_topic +global g_recv_data + +g_recv_data="" + +# This is only a workaround for running mqtt client with 'hardcoded' data using plain socket interface +def mqtt_client_sketch(): + global g_recv_topic + global g_recv_data + http_connect = bytearray([ 0x47, 0x45, 0x54, 0x20, 0x2f, 0x77, 0x73, 0x20, 0x48, 0x54, 0x54, 0x50, 0x2f, 0x31, 0x2e, 0x31, 0x0d, 0x0a, 0x4f, 0x72, 0x69, 0x67, 0x69, 0x6e, 0x3a, 0x20, 0x68, 0x74, 0x74, 0x70, 0x73, 0x3a, 0x2f, 0x2f, 0x69, 0x6f, 0x74, 0x2e, 0x65, 0x63, 0x6c, 0x69, 0x70, 0x73, 0x65, 0x2e, 0x6f, 0x72, 0x67, 0x3a, 0x38, 0x30, 0x0d, 0x0a, 0x55, 0x70, 0x67, 0x72, 0x61, 0x64, 0x65, 0x3a, 0x20, 0x77, 0x65, 0x62, 0x73, 0x6f, 0x63, 0x6b, 0x65, 0x74, 0x0d, 0x0a, 0x53, 0x65, 0x63, 0x2d, 0x57, 0x65, 0x62, 0x73, 0x6f, 0x63, 0x6b, 0x65, 0x74, 0x2d, 0x56, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x3a, 0x20, 0x31, 0x33, 0x0d, 0x0a, 0x53, 0x65, 0x63, 0x2d, 0x57, 0x65, 0x62, 0x73, 0x6f, 0x63, 0x6b, 0x65, 0x74, 0x2d, 0x50, 0x72, 0x6f, 0x74, 0x6f, 0x63, 0x6f, 0x6c, 0x3a, 0x20, 0x6d, 0x71, 0x74, 0x74, 0x0d, 0x0a, 0x43, 0x6f, 0x6e, 0x6e, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x3a, 0x20, 0x55, 0x70, 0x67, 0x72, 0x61, 0x64, 0x65, 0x0d, 0x0a, 0x53, 0x65, 0x63, 0x2d, 0x57, 0x65, 0x62, 0x53, 0x6f, 0x63, 0x6b, 0x65, 0x74, 0x2d, 0x4b, 0x65, 0x79, 0x3a, 0x20, 0x6c, 0x35, 0x61, 0x50, 0x41, 0x64, 0x6d, 0x4a, 0x52, 0x65, 0x32, 0x79, 0x55, 0x42, 0x79, 0x68, 0x37, 0x35, 0x72, 0x58, 0x68, 0x51, 0x3d, 0x3d, 0x0d, 0x0a, 0x48, 0x6f, 0x73, 0x74, 0x3a, 0x20, 0x69, 0x6f, 0x74, 0x2e, 0x65, 0x63, 0x6c, 0x69, 0x70, 0x73, 0x65, 0x2e, 0x6f, 0x72, 0x67, 0x3a, 0x38, 0x30, 0x0d, 0x0a, 0x0d, 0x0a]) + connect_msg = bytearray([0x82, 0x8e, 0x82, 0x1a, 0xe6, 0x22, 0x92, 0x16, 0xe6, 0x26, 0xcf, 0x4b, 0xb2, 0x76, 0x86, 0x18, 0xe6, 0x1e, 0x82, 0x1a]) + send_qos0_msg = bytearray([ 0x82, 0x9c, 0x44, 0x78, 0xdf, 0x8e, 0x74, 0x62, 0xdf, 0x85, 0x6b, 0x0c, 0xb0, 0xfe, 0x2d, 0x1b, 0xf0, 0xff, 0x2b, 0x0b, 0xef, 0xea, 0x25, 0x0c, 0xbe, 0xd1, 0x30, 0x17, 0x80, 0xeb, 0x37, 0x08, 0xec, 0xbc ]) + subscribe_qos0 = bytearray([ 0x82, 0x92, 0x8e, 0x31, 0x8c, 0x4a, 0x0c, 0x21, 0x8c, 0x4b, 0x8e, 0x3a, 0xa3, 0x3e, 0xe1, 0x41, 0xe5, 0x29, 0xa1, 0x40, 0xe3, 0x39, 0xbe, 0x31] ) + client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + client.settimeout(30) + cli = ssl.wrap_socket(client) + cli.connect(("iot.eclipse.org", 443)) + cli.send(http_connect) + cli.send(connect_msg) + data = cli.recv(1024) + print("Connect ack received {}".format(data)) + cli.send(subscribe_qos0) + data = cli.recv(1024) + print("Subscibe ack received {}".format(data)) + start = time.time() + while (time.time() - start) <= 20: + data = cli.recv(1024) + print("Data received {}".format(data[-17:])) + if data[-15:] == "/topic/qos0data": + g_recv_topic = data[-15:][:11] + g_recv_data = data[-4:] + cli.send(send_qos0_msg) + data = cli.recv(1024) + print("data ack received {}".format(data)) + break + cli.close() + +# 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)) + client.subscribe("/topic/qos0") + +# The callback for when a PUBLISH message is received from the server. +def on_message(client, userdata, msg): + global g_recv_topic + global g_recv_data + if g_recv_data == "" and msg.payload == "data": + client.publish("/topic/qos0", "data_to_esp32") + g_recv_topic = msg.topic + g_recv_data = msg.payload + print(msg.topic+" "+str(msg.payload)) + +# 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 TinyFW +import IDF + + + + +@IDF.idf_example_test(env_tag="Example_WIFI") +def test_examples_protocol_mqtt_wss(env, extra_data): + global g_recv_topic + global g_recv_data + """ + steps: | + 1. join AP and connects to wss broker + 2. Test connects a client to the same broker + 3. Test evaluates it received correct qos0 message + 4. Test ESP32 client received correct qos0 message + """ + dut1 = env.get_dut("mqtt_websocket_secure", "examples/protocols/mqtt/wss") + # check and log bin size + binary_file = os.path.join(dut1.app.binary_path, "mqtt_websocket_secure.bin") + bin_size = os.path.getsize(binary_file) + IDF.log_performance("mqtt_websocket_secure_bin_size", "{}KB".format(bin_size//1024)) + IDF.check_performance("mqtt_websocket_secure_size", bin_size//1024) + # 1. start test + dut1.start_app() + # 2. Test connects to a broker + if use_mqtt_client_sketch: + mqtt_client_sketch() + else: + client = mqtt.Client(transport="websockets") + client.on_connect = on_connect + client.on_message = on_message + client.tls_set(None, + None, + None, cert_reqs=ssl.CERT_NONE, tls_version=ssl.PROTOCOL_TLSv1, ciphers=None) + print "Connecting..." + client.connect("iot.eclipse.org", 443, 60) + print "...done" + print "Start Looping..." + start = time.time() + while (time.time() - start) <= 20: + client.loop() + print "...done" + # 3. check the message received back from the server + if g_recv_topic == "/topic/qos0" and g_recv_data == "data" : + print("PASS: Received correct message") + else: + print("Failure!") + raise ValueError('Wrong data received topic: {}, data:{}'.format(g_recv_topic, g_recv_data)) + # 4. check that the esp32 client received data sent by this python client + dut1.expect(re.compile(r"DATA=data_to_esp32"), timeout=30) + +if __name__ == '__main__': + test_examples_protocol_mqtt_wss() From e5e56341becd0c593a66ed946f7c145d16602d06 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Thu, 13 Sep 2018 11:36:23 +0200 Subject: [PATCH 002/231] MQTT: Moved Kconfig from esp-mqtt submodule to esp-idf to support docs genration in RTD --- components/mqtt/Kconfig | 103 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 102 insertions(+), 1 deletion(-) 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 From 7f2bf4ae2f3436a28390cbd2baa3af14e8ee08fe Mon Sep 17 00:00:00 2001 From: Anton Maklakov Date: Tue, 18 Sep 2018 14:16:19 +0800 Subject: [PATCH 003/231] mqtt: silence a format warning --- components/mqtt/component.mk | 4 ++++ 1 file changed, 4 insertions(+) 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 08efd1fd0815e1771e7d79a7edba71c2ec6c6aec Mon Sep 17 00:00:00 2001 From: Angus Gratton Date: Wed, 19 Sep 2018 15:29:09 +1000 Subject: [PATCH 004/231] doc: Replace :envvar: config links with :ref: --- docs/en/api-reference/protocols/mqtt.rst | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/en/api-reference/protocols/mqtt.rst b/docs/en/api-reference/protocols/mqtt.rst index 83dece2..c5a22aa 100644 --- a/docs/en/api-reference/protocols/mqtt.rst +++ b/docs/en/api-reference/protocols/mqtt.rst @@ -99,14 +99,14 @@ Change settings in ``menuconfig`` -> Component config -> ESP-MQTT Configuration -- :envvar:`CONFIG_MQTT_PROTOCOL_311`: Enables 3.1.1 version of MQTT protocol +- :ref:`CONFIG_MQTT_PROTOCOL_311`: Enables 3.1.1 version of MQTT protocol -- :envvar:`MQTT_TRANSPORT_%TRANSPORT%`: Enables specific MQTT transport layer, such as SSL, WEBSOCKET, WEBSOCKET_SECURE +- :ref:`CONFIG_MQTT_TRANSPORT_SSL`, :ref:`CONFIG_MQTT_TRANSPORT_WEBSOCKET`: Enables specific MQTT transport layer, such as SSL, WEBSOCKET, WEBSOCKET_SECURE -- :envvar:`MQTT_CUSTOM_OUTBOX`: Disables default implementation of mqtt_outbox, so a specific implementaion can be supplied +- :ref:`CONFIG_MQTT_CUSTOM_OUTBOX`: Disables default implementation of mqtt_outbox, so a specific implementaion can be supplied API Reference ------------- -.. include:: /_build/inc/mqtt_client.inc \ No newline at end of file +.. include:: /_build/inc/mqtt_client.inc From 7bcafbc9f482ef71b5a99cc6d193db4be6ef9c2b Mon Sep 17 00:00:00 2001 From: Angus Gratton Date: Wed, 19 Sep 2018 16:10:46 +1000 Subject: [PATCH 005/231] 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 984e21831700b68e427b28b78655d946a451abe2 Mon Sep 17 00:00:00 2001 From: Anton Maklakov Date: Sat, 13 Oct 2018 16:26:11 +0800 Subject: [PATCH 006/231] 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 bab63dcee936953f581dab93868427b44b8c1c07 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Thu, 11 Oct 2018 17:34:09 +0200 Subject: [PATCH 007/231] 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 --- .../mqtt/ssl_mutual_auth/CMakeLists.txt | 10 ++ .../protocols/mqtt/ssl_mutual_auth/Makefile | 7 + .../protocols/mqtt/ssl_mutual_auth/README.md | 81 ++++++++++ .../mqtt/ssl_mutual_auth/main/CMakeLists.txt | 4 + .../ssl_mutual_auth/main/Kconfig.projbuild | 15 ++ .../mqtt/ssl_mutual_auth/main/app_main.c | 152 ++++++++++++++++++ .../mqtt/ssl_mutual_auth/main/client.crt | 1 + .../mqtt/ssl_mutual_auth/main/client.key | 1 + .../mqtt/ssl_mutual_auth/main/component.mk | 1 + 9 files changed, 272 insertions(+) create mode 100644 examples/protocols/mqtt/ssl_mutual_auth/CMakeLists.txt create mode 100644 examples/protocols/mqtt/ssl_mutual_auth/Makefile create mode 100644 examples/protocols/mqtt/ssl_mutual_auth/README.md create mode 100644 examples/protocols/mqtt/ssl_mutual_auth/main/CMakeLists.txt create mode 100644 examples/protocols/mqtt/ssl_mutual_auth/main/Kconfig.projbuild create mode 100644 examples/protocols/mqtt/ssl_mutual_auth/main/app_main.c create mode 100644 examples/protocols/mqtt/ssl_mutual_auth/main/client.crt create mode 100644 examples/protocols/mqtt/ssl_mutual_auth/main/client.key create mode 100644 examples/protocols/mqtt/ssl_mutual_auth/main/component.mk diff --git a/examples/protocols/mqtt/ssl_mutual_auth/CMakeLists.txt b/examples/protocols/mqtt/ssl_mutual_auth/CMakeLists.txt new file mode 100644 index 0000000..84bf375 --- /dev/null +++ b/examples/protocols/mqtt/ssl_mutual_auth/CMakeLists.txt @@ -0,0 +1,10 @@ +# The following four lines of boilerplate have to be in your project's CMakeLists +# in this exact order for cmake to work correctly +cmake_minimum_required(VERSION 3.5) + +include($ENV{IDF_PATH}/tools/cmake/project.cmake) + +project(mqtt_ssl_mutual_auth) + +target_add_binary_data(${CMAKE_PROJECT_NAME}.elf "main/client.crt" TEXT) +target_add_binary_data(${CMAKE_PROJECT_NAME}.elf "main/client.key" TEXT) diff --git a/examples/protocols/mqtt/ssl_mutual_auth/Makefile b/examples/protocols/mqtt/ssl_mutual_auth/Makefile new file mode 100644 index 0000000..cfc04f8 --- /dev/null +++ b/examples/protocols/mqtt/ssl_mutual_auth/Makefile @@ -0,0 +1,7 @@ +# +# This is a project Makefile. It is assumed the directory this Makefile resides in is a +# project subdirectory. +# +PROJECT_NAME := mqtt_ssl_mutual_auth + +include $(IDF_PATH)/make/project.mk diff --git a/examples/protocols/mqtt/ssl_mutual_auth/README.md b/examples/protocols/mqtt/ssl_mutual_auth/README.md new file mode 100644 index 0000000..7ffe537 --- /dev/null +++ b/examples/protocols/mqtt/ssl_mutual_auth/README.md @@ -0,0 +1,81 @@ +# ESP-MQTT SSL Sample application (mutual authentication) + +(See the README.md file in the upper level 'examples' directory for more information about examples.) + +This example connects to the broker test.mosquitto.org using ssl transport with client certificate and as a demonstration subscribes/unsubscribes and send a message on certain topic. + +It uses ESP-MQTT library which implements mqtt client to connect to mqtt broker. + +## How to use example + +### Hardware Required + +This example can be executed on any ESP32 board, the only required interface is WiFi and connection to internet. + +### Configure the project + +``` +make menuconfig +``` + +* Set serial port under Serial Flasher Options. + +* Set ssid and password for the board to connect to AP. + +* Generate your client keys and certificate + +Navigate to the main directory + +``` +cd main +``` + +Generate a client key and a CSR. When you are generating the CSR, do not use the default values. At a minimum, the CSR must include the Country, Organisation and Common Name fields. + +``` +openssl genrsa -out client.key +openssl req -out client.csr -key client.key -new +``` + +Paste the generated CSR in the [Mosquitto test certificate signer](https://test.mosquitto.org/ssl/index.php), click Submit and copy the downloaded `client.crt` in the `main` directory. + +Please note, that the supplied files `client.crt` and `client.key` in the `main` directory are only placeholders for your client certificate and key (i.e. the example "as is" would compile but would not connect to the broker) + +### Build and Flash + +Build the project and flash it to the board, then run monitor tool to view serial output: + +``` +make -j4 flash monitor +``` + +(To exit the serial monitor, type ``Ctrl-]``.) + +See the Getting Started Guide for full steps to configure and use ESP-IDF to build projects. + +## Example Output + +``` +I (3714) event: sta ip: 192.168.0.139, mask: 255.255.255.0, gw: 192.168.0.2 +I (3714) system_api: Base MAC address is not set, read default base MAC address from BLK0 of EFUSE +I (3964) MQTT_CLIENT: Sending MQTT CONNECT message, type: 1, id: 0000 +I (4164) MQTTS_EXAMPLE: MQTT_EVENT_CONNECTED +I (4174) MQTTS_EXAMPLE: sent publish successful, msg_id=41464 +I (4174) MQTTS_EXAMPLE: sent subscribe successful, msg_id=17886 +I (4174) MQTTS_EXAMPLE: sent subscribe successful, msg_id=42970 +I (4184) MQTTS_EXAMPLE: sent unsubscribe successful, msg_id=50241 +I (4314) MQTTS_EXAMPLE: MQTT_EVENT_PUBLISHED, msg_id=41464 +I (4484) MQTTS_EXAMPLE: MQTT_EVENT_SUBSCRIBED, msg_id=17886 +I (4484) MQTTS_EXAMPLE: sent publish successful, msg_id=0 +I (4684) MQTTS_EXAMPLE: MQTT_EVENT_SUBSCRIBED, msg_id=42970 +I (4684) MQTTS_EXAMPLE: sent publish successful, msg_id=0 +I (4884) MQTT_CLIENT: deliver_publish, message_length_read=19, message_length=19 +I (4884) MQTTS_EXAMPLE: MQTT_EVENT_DATA +TOPIC=/topic/qos0 +DATA=data +I (5194) MQTT_CLIENT: deliver_publish, message_length_read=19, message_length=19 +I (5194) MQTTS_EXAMPLE: MQTT_EVENT_DATA +TOPIC=/topic/qos0 +DATA=data +``` + diff --git a/examples/protocols/mqtt/ssl_mutual_auth/main/CMakeLists.txt b/examples/protocols/mqtt/ssl_mutual_auth/main/CMakeLists.txt new file mode 100644 index 0000000..6b03500 --- /dev/null +++ b/examples/protocols/mqtt/ssl_mutual_auth/main/CMakeLists.txt @@ -0,0 +1,4 @@ +set(COMPONENT_SRCS "app_main.c") +set(COMPONENT_ADD_INCLUDEDIRS ".") + +register_component() diff --git a/examples/protocols/mqtt/ssl_mutual_auth/main/Kconfig.projbuild b/examples/protocols/mqtt/ssl_mutual_auth/main/Kconfig.projbuild new file mode 100644 index 0000000..176d8fb --- /dev/null +++ b/examples/protocols/mqtt/ssl_mutual_auth/main/Kconfig.projbuild @@ -0,0 +1,15 @@ +menu "Example Configuration" + +config WIFI_SSID + string "WiFi SSID" + default "myssid" + help + SSID (network name) for the example to connect to. + +config WIFI_PASSWORD + string "WiFi Password" + default "mypassword" + help + WiFi password (WPA or WPA2) for the example to use. + +endmenu diff --git a/examples/protocols/mqtt/ssl_mutual_auth/main/app_main.c b/examples/protocols/mqtt/ssl_mutual_auth/main/app_main.c new file mode 100644 index 0000000..2ebce48 --- /dev/null +++ b/examples/protocols/mqtt/ssl_mutual_auth/main/app_main.c @@ -0,0 +1,152 @@ +#include +#include +#include +#include +#include "esp_wifi.h" +#include "esp_system.h" +#include "nvs_flash.h" +#include "esp_event_loop.h" + +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "freertos/semphr.h" +#include "freertos/queue.h" +#include "freertos/event_groups.h" + +#include "lwip/sockets.h" +#include "lwip/dns.h" +#include "lwip/netdb.h" + +#include "esp_log.h" +#include "mqtt_client.h" + +static const char *TAG = "MQTTS_EXAMPLE"; + +static EventGroupHandle_t wifi_event_group; +const static int CONNECTED_BIT = BIT0; + + + +static esp_err_t wifi_event_handler(void *ctx, system_event_t *event) +{ + switch (event->event_id) { + case SYSTEM_EVENT_STA_START: + esp_wifi_connect(); + break; + case SYSTEM_EVENT_STA_GOT_IP: + xEventGroupSetBits(wifi_event_group, CONNECTED_BIT); + + break; + case SYSTEM_EVENT_STA_DISCONNECTED: + esp_wifi_connect(); + xEventGroupClearBits(wifi_event_group, CONNECTED_BIT); + break; + default: + break; + } + return ESP_OK; +} + +static void wifi_init(void) +{ + tcpip_adapter_init(); + wifi_event_group = xEventGroupCreate(); + ESP_ERROR_CHECK(esp_event_loop_init(wifi_event_handler, NULL)); + wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); + ESP_ERROR_CHECK(esp_wifi_init(&cfg)); + ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM)); + wifi_config_t wifi_config = { + .sta = { + .ssid = CONFIG_WIFI_SSID, + .password = CONFIG_WIFI_PASSWORD, + }, + }; + ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); + ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config)); + ESP_LOGI(TAG, "start the WIFI SSID:[%s]", CONFIG_WIFI_SSID); + ESP_ERROR_CHECK(esp_wifi_start()); + ESP_LOGI(TAG, "Waiting for wifi"); + xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT, false, true, portMAX_DELAY); +} + +extern const uint8_t client_cert_pem_start[] asm("_binary_client_crt_start"); +extern const uint8_t client_cert_pem_end[] asm("_binary_client_crt_end"); +extern const uint8_t client_key_pem_start[] asm("_binary_client_key_start"); +extern const uint8_t client_key_pem_end[] asm("_binary_client_key_end"); + +static esp_err_t mqtt_event_handler(esp_mqtt_event_handle_t event) +{ + esp_mqtt_client_handle_t client = event->client; + int msg_id; + // your_context_t *context = event->context; + switch (event->event_id) { + case MQTT_EVENT_CONNECTED: + ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED"); + msg_id = esp_mqtt_client_subscribe(client, "/topic/qos0", 0); + ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id); + + msg_id = esp_mqtt_client_subscribe(client, "/topic/qos1", 1); + ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id); + + msg_id = esp_mqtt_client_unsubscribe(client, "/topic/qos1"); + ESP_LOGI(TAG, "sent unsubscribe successful, msg_id=%d", msg_id); + break; + case MQTT_EVENT_DISCONNECTED: + ESP_LOGI(TAG, "MQTT_EVENT_DISCONNECTED"); + break; + + case MQTT_EVENT_SUBSCRIBED: + ESP_LOGI(TAG, "MQTT_EVENT_SUBSCRIBED, msg_id=%d", event->msg_id); + msg_id = esp_mqtt_client_publish(client, "/topic/qos0", "data", 0, 0, 0); + ESP_LOGI(TAG, "sent publish successful, msg_id=%d", msg_id); + break; + case MQTT_EVENT_UNSUBSCRIBED: + ESP_LOGI(TAG, "MQTT_EVENT_UNSUBSCRIBED, msg_id=%d", event->msg_id); + break; + case MQTT_EVENT_PUBLISHED: + ESP_LOGI(TAG, "MQTT_EVENT_PUBLISHED, msg_id=%d", event->msg_id); + break; + case MQTT_EVENT_DATA: + ESP_LOGI(TAG, "MQTT_EVENT_DATA"); + printf("TOPIC=%.*s\r\n", event->topic_len, event->topic); + printf("DATA=%.*s\r\n", event->data_len, event->data); + break; + case MQTT_EVENT_ERROR: + ESP_LOGI(TAG, "MQTT_EVENT_ERROR"); + break; + } + return ESP_OK; +} + +static void mqtt_app_start(void) +{ + const esp_mqtt_client_config_t mqtt_cfg = { + .uri = "mqtts://test.mosquitto.org:8884", + .event_handle = mqtt_event_handler, + .client_cert_pem = (const char *)client_cert_pem_start, + .client_key_pem = (const char *)client_key_pem_start, + }; + + ESP_LOGI(TAG, "[APP] Free memory: %d bytes", esp_get_free_heap_size()); + esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg); + esp_mqtt_client_start(client); +} + +void app_main() +{ + ESP_LOGI(TAG, "[APP] Startup.."); + ESP_LOGI(TAG, "[APP] Free memory: %d bytes", esp_get_free_heap_size()); + ESP_LOGI(TAG, "[APP] IDF version: %s", esp_get_idf_version()); + + esp_log_level_set("*", ESP_LOG_INFO); + esp_log_level_set("MQTT_CLIENT", ESP_LOG_VERBOSE); + esp_log_level_set("TRANSPORT_TCP", ESP_LOG_VERBOSE); + esp_log_level_set("TRANSPORT_SSL", ESP_LOG_VERBOSE); + esp_log_level_set("TRANSPORT", ESP_LOG_VERBOSE); + esp_log_level_set("OUTBOX", ESP_LOG_VERBOSE); + + nvs_flash_init(); + wifi_init(); + mqtt_app_start(); + +} diff --git a/examples/protocols/mqtt/ssl_mutual_auth/main/client.crt b/examples/protocols/mqtt/ssl_mutual_auth/main/client.crt new file mode 100644 index 0000000..7a3074b --- /dev/null +++ b/examples/protocols/mqtt/ssl_mutual_auth/main/client.crt @@ -0,0 +1 @@ +Please paste your client certificate here (follow instructions in README.md) diff --git a/examples/protocols/mqtt/ssl_mutual_auth/main/client.key b/examples/protocols/mqtt/ssl_mutual_auth/main/client.key new file mode 100644 index 0000000..a956f85 --- /dev/null +++ b/examples/protocols/mqtt/ssl_mutual_auth/main/client.key @@ -0,0 +1 @@ +Please paste here your client key (follow instructions in README.md) diff --git a/examples/protocols/mqtt/ssl_mutual_auth/main/component.mk b/examples/protocols/mqtt/ssl_mutual_auth/main/component.mk new file mode 100644 index 0000000..01adda5 --- /dev/null +++ b/examples/protocols/mqtt/ssl_mutual_auth/main/component.mk @@ -0,0 +1 @@ +COMPONENT_EMBED_TXTFILES := client.crt client.key From 1d008bf4edc427ce0b23dbe767f2ec644263a41a Mon Sep 17 00:00:00 2001 From: David Cermak Date: Thu, 27 Sep 2018 11:46:13 +0200 Subject: [PATCH 008/231] mqtt: example tests refactored to provide descriptive failures, addapted to python23, removed workarround for launching without mqtt-paho package --- .../mqtt/ssl/mqtt_ssl_example_test.py | 92 +++++++----------- .../mqtt/tcp/mqtt_tcp_example_test.py | 42 ++++---- .../protocols/mqtt/ws/mqtt_ws_example_test.py | 96 +++++++------------ .../mqtt/wss/mqtt_wss_example_test.py | 94 +++++++----------- 4 files changed, 125 insertions(+), 199 deletions(-) diff --git a/examples/protocols/mqtt/ssl/mqtt_ssl_example_test.py b/examples/protocols/mqtt/ssl/mqtt_ssl_example_test.py index c5ae31a..be6d1e3 100644 --- a/examples/protocols/mqtt/ssl/mqtt_ssl_example_test.py +++ b/examples/protocols/mqtt/ssl/mqtt_ssl_example_test.py @@ -5,66 +5,29 @@ import time import socket import imp import ssl - -use_mqtt_client_sketch = False - -try: - imp.find_module('paho') - import paho.mqtt.client as mqtt - # Make things with supposed existing module -except ImportError: - use_mqtt_client_sketch = True - pass - -global g_recv_topic -global g_recv_data +import paho.mqtt.client as mqtt g_recv_data="" - -# This is only a workaround for running mqtt client with 'hardcoded' data using plain socket interface -def mqtt_client_sketch(): - global g_recv_topic - global g_recv_data - connect_msg = bytearray([0x10, 0x0c, 00, 0x04, 0x4d, 0x51, 0x54, 0x54, 0x04, 0x02, 00, 0x3c, 00, 00]) - send_qos0_msg = bytearray([ 0x30, 0x1a, 0x00, 0x0b, 0x2f, 0x74, 0x6f, 0x70, 0x69, 0x63, 0x2f, 0x71, 0x6f, 0x73, 0x30, 0x64, 0x61, 0x74, 0x61, 0x5f, 0x74, 0x6f, 0x5f, 0x65, 0x73, 0x70, 0x33, 0x32]) - subscribe_qos0 = bytearray([ 0x82, 0x10, 0x00, 0x01, 0x00, 0x0b, 0x2f, 0x74, 0x6f, 0x70, 0x69, 0x63, 0x2f, 0x71, 0x6f, 0x73, 0x30, 0x00] ) - client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - client.settimeout(30) - cli = ssl.wrap_socket(client) - cli.connect(("iot.eclipse.org", 8883)) - cli.send(connect_msg) - data = cli.recv(1024) - print("Connect ack received {}".format(data)) - cli.send(subscribe_qos0) - data = cli.recv(1024) - print("Subscibe ack received {}".format(data)) - start = time.time() - while (time.time() - start) <= 20: - data = cli.recv(1024) - print("Data received {}".format(data[-17:])) - if data[-15:] == "/topic/qos0data": - g_recv_topic = data[-15:][:11] - g_recv_data = data[-4:] - cli.send(send_qos0_msg) - data = cli.recv(1024) - print("data ack received {}".format(data)) - break - cli.close() +g_recv_topic="" +g_broker_connected=0 # The callback for when the client receives a CONNACK response from the server. def on_connect(client, userdata, flags, rc): + global g_broker_connected print("Connected with result code "+str(rc)) + g_broker_connected = 1 client.subscribe("/topic/qos0") # The callback for when a PUBLISH message is received from the server. def on_message(client, userdata, msg): global g_recv_topic global g_recv_data - if g_recv_data == "" and msg.payload == "data": + payload = msg.payload.decode() + if g_recv_data == "" and payload == "data": client.publish("/topic/qos0", "data_to_esp32") g_recv_topic = msg.topic - g_recv_data = msg.payload - print(msg.topic+" "+str(msg.payload)) + g_recv_data = payload + print(msg.topic+" "+str(payload)) # this is a test case write with tiny-test-fw. # to run test cases outside tiny-test-fw, @@ -76,7 +39,7 @@ if test_fw_path and test_fw_path not in sys.path: import TinyFW import IDF - +import DUT @@ -84,6 +47,8 @@ import IDF def test_examples_protocol_mqtt_ssl(env, extra_data): global g_recv_topic global g_recv_data + global g_broker_connected + broker_url="iot.eclipse.org" """ steps: | 1. join AP and connects to ssl broker @@ -97,12 +62,13 @@ def test_examples_protocol_mqtt_ssl(env, extra_data): bin_size = os.path.getsize(binary_file) IDF.log_performance("mqtt_ssl_bin_size", "{}KB".format(bin_size//1024)) IDF.check_performance("mqtt_ssl_size", bin_size//1024) - # 1. start test + # 1. start test (and check the environment is healthy) dut1.start_app() + client = None # 2. Test connects to a broker - if use_mqtt_client_sketch: - mqtt_client_sketch() - else: + try: + ip_address = dut1.expect(re.compile(r" sta ip: ([^,]+),"), timeout=30) + print("Connected to AP with IP: {}".format(ip_address)) client = mqtt.Client() client.on_connect = on_connect client.on_message = on_message @@ -110,15 +76,21 @@ def test_examples_protocol_mqtt_ssl(env, extra_data): None, None, cert_reqs=ssl.CERT_NONE, tls_version=ssl.PROTOCOL_TLSv1, ciphers=None) client.tls_insecure_set(True) - - print "Connecting..." - client.connect("iot.eclipse.org", 8883, 60) - print "...done" - print "Start Looping..." - start = time.time() - while (time.time() - start) <= 20: - client.loop() - print "...done" + print("Connecting...") + client.connect(broker_url, 8883, 60) + print("...done") + except DUT.ExpectTimeout: + raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP') + except: + print("ENV_TEST_FAILURE: Unexpected error while connecting to broker {}: {}:".format(broker_url, sys.exc_info()[0])) + raise + print("Start Looping...") + start = time.time() + while (time.time() - start) <= 20: + client.loop() + print("...done") + if g_broker_connected == 0: + raise ValueError('ENV_TEST_FAILURE: Test script cannot connect to broker: {}'.format(broker_url)) # 3. check the message received back from the server if g_recv_topic == "/topic/qos0" and g_recv_data == "data" : print("PASS: Received correct message") diff --git a/examples/protocols/mqtt/tcp/mqtt_tcp_example_test.py b/examples/protocols/mqtt/tcp/mqtt_tcp_example_test.py index c1bf42d..e2568d5 100644 --- a/examples/protocols/mqtt/tcp/mqtt_tcp_example_test.py +++ b/examples/protocols/mqtt/tcp/mqtt_tcp_example_test.py @@ -3,9 +3,10 @@ import os import sys from socket import * from threading import Thread +import struct import time -global msgid +msgid=-1 def get_my_ip(): s1 = socket(AF_INET, SOCK_DGRAM) @@ -17,14 +18,20 @@ def get_my_ip(): def mqqt_server_sketch(my_ip, port): global msgid print("Starting the server on {}".format(my_ip)) - s=socket(AF_INET, SOCK_STREAM) - s.settimeout(60) - s.bind((my_ip, port)) - s.listen(1) - q,addr=s.accept() - q.settimeout(30) - print("connection accepted") - # q.send(g_msg_to_client) + s = None + try: + s=socket(AF_INET, SOCK_STREAM) + s.settimeout(60) + s.bind((my_ip, port)) + s.listen(1) + q,addr=s.accept() + q.settimeout(30) + print("connection accepted") + except: + print("Local server on {}:{} listening/accepting failure: {}" + "Possibly check permissions or firewall settings" + "to accept connections on this address".format(my_ip, port, sys.exc_info()[0])) + raise data = q.recv(1024) # check if received initial empty message print("received from client {}".format(data)) @@ -32,7 +39,7 @@ def mqqt_server_sketch(my_ip, port): q.send(data) # try to receive qos1 data = q.recv(1024) - msgid = ord(data[15])*256+ord(data[16]) + msgid = struct.unpack(">H", data[15:17])[0] print("received from client {}, msgid: {}".format(data, msgid)) data = bytearray([0x40, 0x02, data[15], data[16]]) q.send(data) @@ -50,6 +57,7 @@ if test_fw_path and test_fw_path not in sys.path: import TinyFW import IDF +import DUT @@ -75,21 +83,21 @@ def test_examples_protocol_mqtt_qos1(env, extra_data): thread1 = Thread(target = mqqt_server_sketch, args = (host_ip,1883)) thread1.start() # 2. start the dut test and wait till client gets IP address - dut1.start_app() + dut1.start_app() # waiting for getting the IP address - data = dut1.expect(re.compile(r" sta ip: ([^,]+),"), timeout=30) - # time.sleep(15) + try: + ip_address = dut1.expect(re.compile(r" sta ip: ([^,]+),"), timeout=30) + print("Connected to AP with IP: {}".format(ip_address)) + except DUT.ExpectTimeout: + raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP') + print ("writing to device: {}".format("mqtt://" + host_ip + "\n")) dut1.write("mqtt://" + host_ip + "\n") thread1.join() print ("Message id received from server: {}".format(msgid)) # 3. check the message id was enqueued and then deleted msgid_enqueued = dut1.expect(re.compile(r"OUTBOX: ENQUEUE msgid=([0-9]+)"), timeout=30) - # expect_txt="OUTBOX: ENQUEUE msgid=" + str(msgid) - # dut1.expect(re.compile(expect_txt), timeout=30) msgid_deleted = dut1.expect(re.compile(r"OUTBOX: DELETED msgid=([0-9]+)"), timeout=30) - # expect_txt="OUTBOX: DELETED msgid=" + str(msgid) - # dut1.expect(re.compile(expect_txt), timeout=30) # 4. check the msgid of received data are the same as that of enqueued and deleted from outbox if (msgid_enqueued[0] == str(msgid) and msgid_deleted[0] == str(msgid)): print("PASS: Received correct msg id") diff --git a/examples/protocols/mqtt/ws/mqtt_ws_example_test.py b/examples/protocols/mqtt/ws/mqtt_ws_example_test.py index f00e4dd..276f1d3 100644 --- a/examples/protocols/mqtt/ws/mqtt_ws_example_test.py +++ b/examples/protocols/mqtt/ws/mqtt_ws_example_test.py @@ -1,70 +1,35 @@ +from __future__ import print_function +from __future__ import unicode_literals +from builtins import str import re import os import sys import time import socket import imp - -use_mqtt_client_sketch = False - -try: - imp.find_module('paho') - import paho.mqtt.client as mqtt - # Make things with supposed existing module -except ImportError: - use_mqtt_client_sketch = True - pass - -global g_recv_topic -global g_recv_data +import paho.mqtt.client as mqtt g_recv_data="" - -# This is only a workaround for running mqtt client with 'hardcoded' data using plain socket interface -def mqtt_client_sketch(): - global g_recv_topic - global g_recv_data - http_connect = bytearray([ 0x47, 0x45, 0x54, 0x20, 0x2f, 0x77, 0x73, 0x20, 0x48, 0x54, 0x54, 0x50, 0x2f, 0x31, 0x2e, 0x31, 0x0d, 0x0a, 0x4f, 0x72, 0x69, 0x67, 0x69, 0x6e, 0x3a, 0x20, 0x68, 0x74, 0x74, 0x70, 0x73, 0x3a, 0x2f, 0x2f, 0x69, 0x6f, 0x74, 0x2e, 0x65, 0x63, 0x6c, 0x69, 0x70, 0x73, 0x65, 0x2e, 0x6f, 0x72, 0x67, 0x3a, 0x38, 0x30, 0x0d, 0x0a, 0x55, 0x70, 0x67, 0x72, 0x61, 0x64, 0x65, 0x3a, 0x20, 0x77, 0x65, 0x62, 0x73, 0x6f, 0x63, 0x6b, 0x65, 0x74, 0x0d, 0x0a, 0x53, 0x65, 0x63, 0x2d, 0x57, 0x65, 0x62, 0x73, 0x6f, 0x63, 0x6b, 0x65, 0x74, 0x2d, 0x56, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x3a, 0x20, 0x31, 0x33, 0x0d, 0x0a, 0x53, 0x65, 0x63, 0x2d, 0x57, 0x65, 0x62, 0x73, 0x6f, 0x63, 0x6b, 0x65, 0x74, 0x2d, 0x50, 0x72, 0x6f, 0x74, 0x6f, 0x63, 0x6f, 0x6c, 0x3a, 0x20, 0x6d, 0x71, 0x74, 0x74, 0x0d, 0x0a, 0x43, 0x6f, 0x6e, 0x6e, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x3a, 0x20, 0x55, 0x70, 0x67, 0x72, 0x61, 0x64, 0x65, 0x0d, 0x0a, 0x53, 0x65, 0x63, 0x2d, 0x57, 0x65, 0x62, 0x53, 0x6f, 0x63, 0x6b, 0x65, 0x74, 0x2d, 0x4b, 0x65, 0x79, 0x3a, 0x20, 0x6c, 0x35, 0x61, 0x50, 0x41, 0x64, 0x6d, 0x4a, 0x52, 0x65, 0x32, 0x79, 0x55, 0x42, 0x79, 0x68, 0x37, 0x35, 0x72, 0x58, 0x68, 0x51, 0x3d, 0x3d, 0x0d, 0x0a, 0x48, 0x6f, 0x73, 0x74, 0x3a, 0x20, 0x69, 0x6f, 0x74, 0x2e, 0x65, 0x63, 0x6c, 0x69, 0x70, 0x73, 0x65, 0x2e, 0x6f, 0x72, 0x67, 0x3a, 0x38, 0x30, 0x0d, 0x0a, 0x0d, 0x0a]) - connect_msg = bytearray([0x82, 0x8e, 0x82, 0x1a, 0xe6, 0x22, 0x92, 0x16, 0xe6, 0x26, 0xcf, 0x4b, 0xb2, 0x76, 0x86, 0x18, 0xe6, 0x1e, 0x82, 0x1a]) - send_qos0_msg = bytearray([ 0x82, 0x9c, 0x44, 0x78, 0xdf, 0x8e, 0x74, 0x62, 0xdf, 0x85, 0x6b, 0x0c, 0xb0, 0xfe, 0x2d, 0x1b, 0xf0, 0xff, 0x2b, 0x0b, 0xef, 0xea, 0x25, 0x0c, 0xbe, 0xd1, 0x30, 0x17, 0x80, 0xeb, 0x37, 0x08, 0xec, 0xbc ]) - subscribe_qos0 = bytearray([ 0x82, 0x92, 0x8e, 0x31, 0x8c, 0x4a, 0x0c, 0x21, 0x8c, 0x4b, 0x8e, 0x3a, 0xa3, 0x3e, 0xe1, 0x41, 0xe5, 0x29, 0xa1, 0x40, 0xe3, 0x39, 0xbe, 0x31] ) - cli = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - cli.settimeout(30) - cli.connect(("iot.eclipse.org", 80)) - cli.send(http_connect) - cli.send(connect_msg) - data = cli.recv(1024) - print("Connect ack received {}".format(data)) - cli.send(subscribe_qos0) - data = cli.recv(1024) - print("Subscibe ack received {}".format(data)) - start = time.time() - while (time.time() - start) <= 20: - data = cli.recv(1024) - print("Data received {}".format(data[-17:])) - if data[-15:] == "/topic/qos0data": - g_recv_topic = data[-15:][:11] - g_recv_data = data[-4:] - cli.send(send_qos0_msg) - data = cli.recv(1024) - print("data ack received {}".format(data)) - break - cli.close() +g_recv_topic="" +g_broker_connected=0 # The callback for when the client receives a CONNACK response from the server. def on_connect(client, userdata, flags, rc): + global g_broker_connected print("Connected with result code "+str(rc)) + g_broker_connected = 1 client.subscribe("/topic/qos0") # The callback for when a PUBLISH message is received from the server. def on_message(client, userdata, msg): global g_recv_topic global g_recv_data - if g_recv_data == "" and msg.payload == "data": + payload = msg.payload.decode() + if g_recv_data == "" and payload == "data": client.publish("/topic/qos0", "data_to_esp32") g_recv_topic = msg.topic - g_recv_data = msg.payload - print(msg.topic+" "+str(msg.payload)) + g_recv_data = payload + print(msg.topic+" "+payload) # this is a test case write with tiny-test-fw. # to run test cases outside tiny-test-fw, @@ -76,14 +41,15 @@ if test_fw_path and test_fw_path not in sys.path: import TinyFW import IDF - - +import DUT @IDF.idf_example_test(env_tag="Example_WIFI") def test_examples_protocol_mqtt_ws(env, extra_data): global g_recv_topic global g_recv_data + global g_broker_connected + broker_url="iot.eclipse.org" """ steps: | 1. join AP and connects to ws broker @@ -97,24 +63,32 @@ def test_examples_protocol_mqtt_ws(env, extra_data): bin_size = os.path.getsize(binary_file) IDF.log_performance("mqtt_websocket_bin_size", "{}KB".format(bin_size//1024)) IDF.check_performance("mqtt_websocket_size", bin_size//1024) - # 1. start test + # 1. start test (and check the environment is healthy) dut1.start_app() + client = None # 2. Test connects to a broker - if use_mqtt_client_sketch: - mqtt_client_sketch() - else: + try: + ip_address = dut1.expect(re.compile(r" sta ip: ([^,]+),"), timeout=30) + print("Connected to AP with IP: {}".format(ip_address)) client = mqtt.Client(transport="websockets") client.on_connect = on_connect client.on_message = on_message client.ws_set_options(path="/ws", headers=None) - print "Connecting..." - client.connect("iot.eclipse.org", 80, 60) - print "...done" - print "Start Looping..." - start = time.time() - while (time.time() - start) <= 20: - client.loop() - print "...done" + print("Connecting...") + client.connect(broker_url, 80, 60) + print("...done") + except DUT.ExpectTimeout: + raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP') + except: + print("ENV_TEST_FAILURE: Unexpected error while connecting to broker {}: {}:".format(broker_url, sys.exc_info()[0])) + raise + print("Start Looping...") + start = time.time() + while (time.time() - start) <= 20: + client.loop() + print("...done") + if g_broker_connected == 0: + raise ValueError('ENV_TEST_FAILURE: Test script cannot connect to broker: {}'.format(broker_url)) # 3. check the message received back from the server if g_recv_topic == "/topic/qos0" and g_recv_data == "data" : print("PASS: Received correct message") diff --git a/examples/protocols/mqtt/wss/mqtt_wss_example_test.py b/examples/protocols/mqtt/wss/mqtt_wss_example_test.py index 9c153cf..ade43c1 100644 --- a/examples/protocols/mqtt/wss/mqtt_wss_example_test.py +++ b/examples/protocols/mqtt/wss/mqtt_wss_example_test.py @@ -1,3 +1,4 @@ +from __future__ import unicode_literals import re import os import sys @@ -5,68 +6,29 @@ import time import socket import imp import ssl - -use_mqtt_client_sketch = False - -try: - imp.find_module('paho') - import paho.mqtt.client as mqtt - # Make things with supposed existing module -except ImportError: - use_mqtt_client_sketch = True - pass - -global g_recv_topic -global g_recv_data +import paho.mqtt.client as mqtt g_recv_data="" - -# This is only a workaround for running mqtt client with 'hardcoded' data using plain socket interface -def mqtt_client_sketch(): - global g_recv_topic - global g_recv_data - http_connect = bytearray([ 0x47, 0x45, 0x54, 0x20, 0x2f, 0x77, 0x73, 0x20, 0x48, 0x54, 0x54, 0x50, 0x2f, 0x31, 0x2e, 0x31, 0x0d, 0x0a, 0x4f, 0x72, 0x69, 0x67, 0x69, 0x6e, 0x3a, 0x20, 0x68, 0x74, 0x74, 0x70, 0x73, 0x3a, 0x2f, 0x2f, 0x69, 0x6f, 0x74, 0x2e, 0x65, 0x63, 0x6c, 0x69, 0x70, 0x73, 0x65, 0x2e, 0x6f, 0x72, 0x67, 0x3a, 0x38, 0x30, 0x0d, 0x0a, 0x55, 0x70, 0x67, 0x72, 0x61, 0x64, 0x65, 0x3a, 0x20, 0x77, 0x65, 0x62, 0x73, 0x6f, 0x63, 0x6b, 0x65, 0x74, 0x0d, 0x0a, 0x53, 0x65, 0x63, 0x2d, 0x57, 0x65, 0x62, 0x73, 0x6f, 0x63, 0x6b, 0x65, 0x74, 0x2d, 0x56, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x3a, 0x20, 0x31, 0x33, 0x0d, 0x0a, 0x53, 0x65, 0x63, 0x2d, 0x57, 0x65, 0x62, 0x73, 0x6f, 0x63, 0x6b, 0x65, 0x74, 0x2d, 0x50, 0x72, 0x6f, 0x74, 0x6f, 0x63, 0x6f, 0x6c, 0x3a, 0x20, 0x6d, 0x71, 0x74, 0x74, 0x0d, 0x0a, 0x43, 0x6f, 0x6e, 0x6e, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x3a, 0x20, 0x55, 0x70, 0x67, 0x72, 0x61, 0x64, 0x65, 0x0d, 0x0a, 0x53, 0x65, 0x63, 0x2d, 0x57, 0x65, 0x62, 0x53, 0x6f, 0x63, 0x6b, 0x65, 0x74, 0x2d, 0x4b, 0x65, 0x79, 0x3a, 0x20, 0x6c, 0x35, 0x61, 0x50, 0x41, 0x64, 0x6d, 0x4a, 0x52, 0x65, 0x32, 0x79, 0x55, 0x42, 0x79, 0x68, 0x37, 0x35, 0x72, 0x58, 0x68, 0x51, 0x3d, 0x3d, 0x0d, 0x0a, 0x48, 0x6f, 0x73, 0x74, 0x3a, 0x20, 0x69, 0x6f, 0x74, 0x2e, 0x65, 0x63, 0x6c, 0x69, 0x70, 0x73, 0x65, 0x2e, 0x6f, 0x72, 0x67, 0x3a, 0x38, 0x30, 0x0d, 0x0a, 0x0d, 0x0a]) - connect_msg = bytearray([0x82, 0x8e, 0x82, 0x1a, 0xe6, 0x22, 0x92, 0x16, 0xe6, 0x26, 0xcf, 0x4b, 0xb2, 0x76, 0x86, 0x18, 0xe6, 0x1e, 0x82, 0x1a]) - send_qos0_msg = bytearray([ 0x82, 0x9c, 0x44, 0x78, 0xdf, 0x8e, 0x74, 0x62, 0xdf, 0x85, 0x6b, 0x0c, 0xb0, 0xfe, 0x2d, 0x1b, 0xf0, 0xff, 0x2b, 0x0b, 0xef, 0xea, 0x25, 0x0c, 0xbe, 0xd1, 0x30, 0x17, 0x80, 0xeb, 0x37, 0x08, 0xec, 0xbc ]) - subscribe_qos0 = bytearray([ 0x82, 0x92, 0x8e, 0x31, 0x8c, 0x4a, 0x0c, 0x21, 0x8c, 0x4b, 0x8e, 0x3a, 0xa3, 0x3e, 0xe1, 0x41, 0xe5, 0x29, 0xa1, 0x40, 0xe3, 0x39, 0xbe, 0x31] ) - client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - client.settimeout(30) - cli = ssl.wrap_socket(client) - cli.connect(("iot.eclipse.org", 443)) - cli.send(http_connect) - cli.send(connect_msg) - data = cli.recv(1024) - print("Connect ack received {}".format(data)) - cli.send(subscribe_qos0) - data = cli.recv(1024) - print("Subscibe ack received {}".format(data)) - start = time.time() - while (time.time() - start) <= 20: - data = cli.recv(1024) - print("Data received {}".format(data[-17:])) - if data[-15:] == "/topic/qos0data": - g_recv_topic = data[-15:][:11] - g_recv_data = data[-4:] - cli.send(send_qos0_msg) - data = cli.recv(1024) - print("data ack received {}".format(data)) - break - cli.close() +g_recv_topic="" +g_broker_connected=0 # The callback for when the client receives a CONNACK response from the server. def on_connect(client, userdata, flags, rc): + global g_broker_connected print("Connected with result code "+str(rc)) + g_broker_connected = 1 client.subscribe("/topic/qos0") # The callback for when a PUBLISH message is received from the server. def on_message(client, userdata, msg): global g_recv_topic global g_recv_data - if g_recv_data == "" and msg.payload == "data": + payload = msg.payload.decode() + if g_recv_data == "" and payload == "data": client.publish("/topic/qos0", "data_to_esp32") g_recv_topic = msg.topic - g_recv_data = msg.payload - print(msg.topic+" "+str(msg.payload)) + g_recv_data = payload + print(msg.topic+" "+str(payload)) # this is a test case write with tiny-test-fw. # to run test cases outside tiny-test-fw, @@ -78,7 +40,7 @@ if test_fw_path and test_fw_path not in sys.path: import TinyFW import IDF - +import DUT @@ -86,6 +48,8 @@ import IDF def test_examples_protocol_mqtt_wss(env, extra_data): global g_recv_topic global g_recv_data + global g_broker_connected + broker_url="iot.eclipse.org" """ steps: | 1. join AP and connects to wss broker @@ -99,26 +63,34 @@ def test_examples_protocol_mqtt_wss(env, extra_data): bin_size = os.path.getsize(binary_file) IDF.log_performance("mqtt_websocket_secure_bin_size", "{}KB".format(bin_size//1024)) IDF.check_performance("mqtt_websocket_secure_size", bin_size//1024) - # 1. start test + # 1. start test (and check the environment is healthy) dut1.start_app() + client = None # 2. Test connects to a broker - if use_mqtt_client_sketch: - mqtt_client_sketch() - else: + try: + ip_address = dut1.expect(re.compile(r" sta ip: ([^,]+),"), timeout=30) + print("Connected to AP with IP: {}".format(ip_address)) client = mqtt.Client(transport="websockets") client.on_connect = on_connect client.on_message = on_message client.tls_set(None, None, None, cert_reqs=ssl.CERT_NONE, tls_version=ssl.PROTOCOL_TLSv1, ciphers=None) - print "Connecting..." - client.connect("iot.eclipse.org", 443, 60) - print "...done" - print "Start Looping..." - start = time.time() - while (time.time() - start) <= 20: - client.loop() - print "...done" + print("Connecting...") + client.connect(broker_url, 443, 60) + print("...done") + except DUT.ExpectTimeout: + raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP') + except: + print("ENV_TEST_FAILURE: Unexpected error while connecting to broker {}: {}:".format(broker_url, sys.exc_info()[0])) + raise + print("Start Looping...") + start = time.time() + while (time.time() - start) <= 20: + client.loop() + print("...done") + if g_broker_connected == 0: + raise ValueError('ENV_TEST_FAILURE: Test script cannot connect to broker: {}'.format(broker_url)) # 3. check the message received back from the server if g_recv_topic == "/topic/qos0" and g_recv_data == "data" : print("PASS: Received correct message") From f7720ff277329623e9993019bf18c01c2066dfd5 Mon Sep 17 00:00:00 2001 From: Roland Dobai Date: Tue, 4 Dec 2018 08:32:48 +0100 Subject: [PATCH 009/231] examples: Fix Python coding style --- .../mqtt/ssl/mqtt_ssl_example_test.py | 58 +++++++++--------- .../mqtt/tcp/mqtt_tcp_example_test.py | 60 ++++++++++--------- .../protocols/mqtt/ws/mqtt_ws_example_test.py | 54 +++++++++-------- .../mqtt/wss/mqtt_wss_example_test.py | 58 +++++++++--------- 4 files changed, 121 insertions(+), 109 deletions(-) diff --git a/examples/protocols/mqtt/ssl/mqtt_ssl_example_test.py b/examples/protocols/mqtt/ssl/mqtt_ssl_example_test.py index be6d1e3..5f47580 100644 --- a/examples/protocols/mqtt/ssl/mqtt_ssl_example_test.py +++ b/examples/protocols/mqtt/ssl/mqtt_ssl_example_test.py @@ -2,45 +2,46 @@ import re import os import sys import time -import socket -import imp import ssl import paho.mqtt.client as mqtt -g_recv_data="" -g_recv_topic="" -g_broker_connected=0 +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 + +g_recv_data = "" +g_recv_topic = "" +g_broker_connected = 0 + # The callback for when the client receives a CONNACK response from the server. def on_connect(client, userdata, flags, rc): global g_broker_connected - print("Connected with result code "+str(rc)) + print("Connected with result code " + str(rc)) g_broker_connected = 1 client.subscribe("/topic/qos0") + # The callback for when a PUBLISH message is received from the server. def on_message(client, userdata, msg): global g_recv_topic global g_recv_data payload = msg.payload.decode() - if g_recv_data == "" and payload == "data": + if g_recv_data == "" and payload == "data": client.publish("/topic/qos0", "data_to_esp32") g_recv_topic = msg.topic g_recv_data = payload - print(msg.topic+" "+str(payload)) - -# 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 TinyFW -import IDF -import DUT - + print(msg.topic + " " + str(payload)) @IDF.idf_example_test(env_tag="Example_WIFI") @@ -48,7 +49,7 @@ def test_examples_protocol_mqtt_ssl(env, extra_data): global g_recv_topic global g_recv_data global g_broker_connected - broker_url="iot.eclipse.org" + broker_url = "iot.eclipse.org" """ steps: | 1. join AP and connects to ssl broker @@ -60,8 +61,8 @@ def test_examples_protocol_mqtt_ssl(env, extra_data): # check and log bin size binary_file = os.path.join(dut1.app.binary_path, "mqtt_ssl.bin") bin_size = os.path.getsize(binary_file) - IDF.log_performance("mqtt_ssl_bin_size", "{}KB".format(bin_size//1024)) - IDF.check_performance("mqtt_ssl_size", bin_size//1024) + IDF.log_performance("mqtt_ssl_bin_size", "{}KB".format(bin_size // 1024)) + IDF.check_performance("mqtt_ssl_size", bin_size // 1024) # 1. start test (and check the environment is healthy) dut1.start_app() client = None @@ -73,15 +74,15 @@ def test_examples_protocol_mqtt_ssl(env, extra_data): client.on_connect = on_connect client.on_message = on_message client.tls_set(None, - None, - None, cert_reqs=ssl.CERT_NONE, tls_version=ssl.PROTOCOL_TLSv1, ciphers=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_url, 8883, 60) print("...done") except DUT.ExpectTimeout: raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP') - except: + except Exception: print("ENV_TEST_FAILURE: Unexpected error while connecting to broker {}: {}:".format(broker_url, sys.exc_info()[0])) raise print("Start Looping...") @@ -92,7 +93,7 @@ def test_examples_protocol_mqtt_ssl(env, extra_data): if g_broker_connected == 0: raise ValueError('ENV_TEST_FAILURE: Test script cannot connect to broker: {}'.format(broker_url)) # 3. check the message received back from the server - if g_recv_topic == "/topic/qos0" and g_recv_data == "data" : + if g_recv_topic == "/topic/qos0" and g_recv_data == "data": print("PASS: Received correct message") else: print("Failure!") @@ -100,5 +101,6 @@ def test_examples_protocol_mqtt_ssl(env, extra_data): # 4. check that the esp32 client received data sent by this python client dut1.expect(re.compile(r"DATA=data_to_esp32"), timeout=30) + if __name__ == '__main__': test_examples_protocol_mqtt_ssl() diff --git a/examples/protocols/mqtt/tcp/mqtt_tcp_example_test.py b/examples/protocols/mqtt/tcp/mqtt_tcp_example_test.py index e2568d5..05e5142 100644 --- a/examples/protocols/mqtt/tcp/mqtt_tcp_example_test.py +++ b/examples/protocols/mqtt/tcp/mqtt_tcp_example_test.py @@ -1,36 +1,53 @@ import re import os import sys -from socket import * +import socket from threading import Thread import struct import time -msgid=-1 + +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 + +msgid = -1 + def get_my_ip(): - s1 = socket(AF_INET, SOCK_DGRAM) + s1 = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s1.connect(("8.8.8.8", 80)) my_ip = s1.getsockname()[0] s1.close() return my_ip + def mqqt_server_sketch(my_ip, port): global msgid print("Starting the server on {}".format(my_ip)) s = None try: - s=socket(AF_INET, SOCK_STREAM) + s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.settimeout(60) s.bind((my_ip, port)) s.listen(1) - q,addr=s.accept() + q,addr = s.accept() q.settimeout(30) print("connection accepted") - except: + except Exception: print("Local server on {}:{} listening/accepting failure: {}" - "Possibly check permissions or firewall settings" - "to accept connections on this address".format(my_ip, port, sys.exc_info()[0])) + "Possibly check permissions or firewall settings" + "to accept connections on this address".format(my_ip, port, sys.exc_info()[0])) raise data = q.recv(1024) # check if received initial empty message @@ -47,20 +64,6 @@ def mqqt_server_sketch(my_ip, port): s.close() print("server closed") -# 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 TinyFW -import IDF -import DUT - - - @IDF.idf_example_test(env_tag="Example_WIFI") def test_examples_protocol_mqtt_qos1(env, extra_data): @@ -76,14 +79,14 @@ def test_examples_protocol_mqtt_qos1(env, extra_data): # check and log bin size binary_file = os.path.join(dut1.app.binary_path, "mqtt_tcp.bin") bin_size = os.path.getsize(binary_file) - IDF.log_performance("mqtt_tcp_bin_size", "{}KB".format(bin_size//1024)) - IDF.check_performance("mqtt_tcp_size", bin_size//1024) + IDF.log_performance("mqtt_tcp_bin_size", "{}KB".format(bin_size // 1024)) + IDF.check_performance("mqtt_tcp_size", bin_size // 1024) # 1. start mqtt broker sketch host_ip = get_my_ip() - thread1 = Thread(target = mqqt_server_sketch, args = (host_ip,1883)) + thread1 = Thread(target=mqqt_server_sketch, args=(host_ip,1883)) thread1.start() # 2. start the dut test and wait till client gets IP address - dut1.start_app() + dut1.start_app() # waiting for getting the IP address try: ip_address = dut1.expect(re.compile(r" sta ip: ([^,]+),"), timeout=30) @@ -91,10 +94,10 @@ def test_examples_protocol_mqtt_qos1(env, extra_data): except DUT.ExpectTimeout: raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP') - print ("writing to device: {}".format("mqtt://" + host_ip + "\n")) + print("writing to device: {}".format("mqtt://" + host_ip + "\n")) dut1.write("mqtt://" + host_ip + "\n") thread1.join() - print ("Message id received from server: {}".format(msgid)) + print("Message id received from server: {}".format(msgid)) # 3. check the message id was enqueued and then deleted msgid_enqueued = dut1.expect(re.compile(r"OUTBOX: ENQUEUE msgid=([0-9]+)"), timeout=30) msgid_deleted = dut1.expect(re.compile(r"OUTBOX: DELETED msgid=([0-9]+)"), timeout=30) @@ -105,5 +108,6 @@ def test_examples_protocol_mqtt_qos1(env, extra_data): print("Failure!") raise ValueError('Mismatch of msgid: received: {}, enqueued {}, deleted {}'.format(msgid, msgid_enqueued, msgid_deleted)) + if __name__ == '__main__': test_examples_protocol_mqtt_qos1() diff --git a/examples/protocols/mqtt/ws/mqtt_ws_example_test.py b/examples/protocols/mqtt/ws/mqtt_ws_example_test.py index 276f1d3..3567cae 100644 --- a/examples/protocols/mqtt/ws/mqtt_ws_example_test.py +++ b/examples/protocols/mqtt/ws/mqtt_ws_example_test.py @@ -5,43 +5,46 @@ import re import os import sys import time -import socket -import imp import paho.mqtt.client as mqtt -g_recv_data="" -g_recv_topic="" -g_broker_connected=0 +try: + import IDF +except Exception: + # 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 + + +g_recv_data = "" +g_recv_topic = "" +g_broker_connected = 0 + # The callback for when the client receives a CONNACK response from the server. def on_connect(client, userdata, flags, rc): global g_broker_connected - print("Connected with result code "+str(rc)) + print("Connected with result code " + str(rc)) g_broker_connected = 1 client.subscribe("/topic/qos0") + # The callback for when a PUBLISH message is received from the server. def on_message(client, userdata, msg): global g_recv_topic global g_recv_data payload = msg.payload.decode() - if g_recv_data == "" and payload == "data": + if g_recv_data == "" and payload == "data": client.publish("/topic/qos0", "data_to_esp32") g_recv_topic = msg.topic g_recv_data = payload - print(msg.topic+" "+payload) - -# 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 TinyFW -import IDF -import DUT + print(msg.topic + " " + payload) @IDF.idf_example_test(env_tag="Example_WIFI") @@ -49,7 +52,7 @@ def test_examples_protocol_mqtt_ws(env, extra_data): global g_recv_topic global g_recv_data global g_broker_connected - broker_url="iot.eclipse.org" + broker_url = "iot.eclipse.org" """ steps: | 1. join AP and connects to ws broker @@ -61,8 +64,8 @@ def test_examples_protocol_mqtt_ws(env, extra_data): # check and log bin size binary_file = os.path.join(dut1.app.binary_path, "mqtt_websocket.bin") bin_size = os.path.getsize(binary_file) - IDF.log_performance("mqtt_websocket_bin_size", "{}KB".format(bin_size//1024)) - IDF.check_performance("mqtt_websocket_size", bin_size//1024) + IDF.log_performance("mqtt_websocket_bin_size", "{}KB".format(bin_size // 1024)) + IDF.check_performance("mqtt_websocket_size", bin_size // 1024) # 1. start test (and check the environment is healthy) dut1.start_app() client = None @@ -79,7 +82,7 @@ def test_examples_protocol_mqtt_ws(env, extra_data): print("...done") except DUT.ExpectTimeout: raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP') - except: + except Exception: print("ENV_TEST_FAILURE: Unexpected error while connecting to broker {}: {}:".format(broker_url, sys.exc_info()[0])) raise print("Start Looping...") @@ -90,7 +93,7 @@ def test_examples_protocol_mqtt_ws(env, extra_data): if g_broker_connected == 0: raise ValueError('ENV_TEST_FAILURE: Test script cannot connect to broker: {}'.format(broker_url)) # 3. check the message received back from the server - if g_recv_topic == "/topic/qos0" and g_recv_data == "data" : + if g_recv_topic == "/topic/qos0" and g_recv_data == "data": print("PASS: Received correct message") else: print("Failure!") @@ -98,5 +101,6 @@ def test_examples_protocol_mqtt_ws(env, extra_data): # 4. check that the esp32 client received data sent by this python client dut1.expect(re.compile(r"DATA=data_to_esp32"), timeout=30) + if __name__ == '__main__': test_examples_protocol_mqtt_ws() diff --git a/examples/protocols/mqtt/wss/mqtt_wss_example_test.py b/examples/protocols/mqtt/wss/mqtt_wss_example_test.py index ade43c1..b1515ae 100644 --- a/examples/protocols/mqtt/wss/mqtt_wss_example_test.py +++ b/examples/protocols/mqtt/wss/mqtt_wss_example_test.py @@ -3,45 +3,46 @@ import re import os import sys import time -import socket -import imp import ssl import paho.mqtt.client as mqtt -g_recv_data="" -g_recv_topic="" -g_broker_connected=0 +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 + +g_recv_data = "" +g_recv_topic = "" +g_broker_connected = 0 + # The callback for when the client receives a CONNACK response from the server. def on_connect(client, userdata, flags, rc): global g_broker_connected - print("Connected with result code "+str(rc)) + print("Connected with result code " + str(rc)) g_broker_connected = 1 client.subscribe("/topic/qos0") + # The callback for when a PUBLISH message is received from the server. def on_message(client, userdata, msg): global g_recv_topic global g_recv_data payload = msg.payload.decode() - if g_recv_data == "" and payload == "data": + if g_recv_data == "" and payload == "data": client.publish("/topic/qos0", "data_to_esp32") g_recv_topic = msg.topic g_recv_data = payload - print(msg.topic+" "+str(payload)) - -# 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 TinyFW -import IDF -import DUT - + print(msg.topic + " " + str(payload)) @IDF.idf_example_test(env_tag="Example_WIFI") @@ -49,7 +50,7 @@ def test_examples_protocol_mqtt_wss(env, extra_data): global g_recv_topic global g_recv_data global g_broker_connected - broker_url="iot.eclipse.org" + broker_url = "iot.eclipse.org" """ steps: | 1. join AP and connects to wss broker @@ -61,8 +62,8 @@ def test_examples_protocol_mqtt_wss(env, extra_data): # check and log bin size binary_file = os.path.join(dut1.app.binary_path, "mqtt_websocket_secure.bin") bin_size = os.path.getsize(binary_file) - IDF.log_performance("mqtt_websocket_secure_bin_size", "{}KB".format(bin_size//1024)) - IDF.check_performance("mqtt_websocket_secure_size", bin_size//1024) + IDF.log_performance("mqtt_websocket_secure_bin_size", "{}KB".format(bin_size // 1024)) + IDF.check_performance("mqtt_websocket_secure_size", bin_size // 1024) # 1. start test (and check the environment is healthy) dut1.start_app() client = None @@ -74,14 +75,14 @@ def test_examples_protocol_mqtt_wss(env, extra_data): client.on_connect = on_connect client.on_message = on_message client.tls_set(None, - None, - None, cert_reqs=ssl.CERT_NONE, tls_version=ssl.PROTOCOL_TLSv1, ciphers=None) + None, + None, cert_reqs=ssl.CERT_NONE, tls_version=ssl.PROTOCOL_TLSv1, ciphers=None) print("Connecting...") client.connect(broker_url, 443, 60) print("...done") except DUT.ExpectTimeout: raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP') - except: + except Exception: print("ENV_TEST_FAILURE: Unexpected error while connecting to broker {}: {}:".format(broker_url, sys.exc_info()[0])) raise print("Start Looping...") @@ -92,7 +93,7 @@ def test_examples_protocol_mqtt_wss(env, extra_data): if g_broker_connected == 0: raise ValueError('ENV_TEST_FAILURE: Test script cannot connect to broker: {}'.format(broker_url)) # 3. check the message received back from the server - if g_recv_topic == "/topic/qos0" and g_recv_data == "data" : + if g_recv_topic == "/topic/qos0" and g_recv_data == "data": print("PASS: Received correct message") else: print("Failure!") @@ -100,5 +101,6 @@ def test_examples_protocol_mqtt_wss(env, extra_data): # 4. check that the esp32 client received data sent by this python client dut1.expect(re.compile(r"DATA=data_to_esp32"), timeout=30) + if __name__ == '__main__': test_examples_protocol_mqtt_wss() From 28aabd712b09751f9a773298dc854969261223b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Noe=C3=ABl=20Moeskops?= Date: Sun, 2 Dec 2018 16:37:27 +0100 Subject: [PATCH 010/231] Typo fix in mqtt.rst. changed 'defalut' to 'default' Merges https://github.com/espressif/esp-idf/pull/2780 --- docs/en/api-reference/protocols/mqtt.rst | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/en/api-reference/protocols/mqtt.rst b/docs/en/api-reference/protocols/mqtt.rst index c5a22aa..3fa366b 100644 --- a/docs/en/api-reference/protocols/mqtt.rst +++ b/docs/en/api-reference/protocols/mqtt.rst @@ -18,10 +18,10 @@ Features Application Example ------------------- - * :example:`protocols/mqtt/tcp`: MQTT over tcp, defalut port 1883 - * :example:`protocols/mqtt/ssl`: MQTT over tcp, defalut port 8883 - * :example:`protocols/mqtt/ws`: MQTT over Websocket, defalut port 80 - * :example:`protocols/mqtt/wss`: MQTT over Websocket Secure, defalut port 443 + * :example:`protocols/mqtt/tcp`: MQTT over tcp, default port 1883 + * :example:`protocols/mqtt/ssl`: MQTT over tcp, default port 8883 + * :example:`protocols/mqtt/ws`: MQTT over Websocket, default port 80 + * :example:`protocols/mqtt/wss`: MQTT over Websocket Secure, default port 443 Configuration From 4b49b806f8ebc4bb841ccc8306c03ebc1f35438d Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Mon, 10 Dec 2018 13:26:40 +0800 Subject: [PATCH 011/231] ci, examples: use sdkconfig.ci as an extra defaults file, if present - Allows placing CI-specific settings into sdkconfig.ci file - Allows substituting environment variables in sdkconfig.ci --- examples/protocols/mqtt/tcp/sdkconfig | 1 - examples/protocols/mqtt/tcp/sdkconfig.ci | 2 ++ examples/protocols/mqtt/tcp/sdkconfig.defaults | 7 ------- 3 files changed, 2 insertions(+), 8 deletions(-) delete mode 100644 examples/protocols/mqtt/tcp/sdkconfig create mode 100644 examples/protocols/mqtt/tcp/sdkconfig.ci delete mode 100644 examples/protocols/mqtt/tcp/sdkconfig.defaults diff --git a/examples/protocols/mqtt/tcp/sdkconfig b/examples/protocols/mqtt/tcp/sdkconfig deleted file mode 100644 index 8b13789..0000000 --- a/examples/protocols/mqtt/tcp/sdkconfig +++ /dev/null @@ -1 +0,0 @@ - diff --git a/examples/protocols/mqtt/tcp/sdkconfig.ci b/examples/protocols/mqtt/tcp/sdkconfig.ci new file mode 100644 index 0000000..09ca8f3 --- /dev/null +++ b/examples/protocols/mqtt/tcp/sdkconfig.ci @@ -0,0 +1,2 @@ +CONFIG_LOG_DEFAULT_LEVEL_DEBUG=y +CONFIG_BROKER_URL="FROM_STDIN" diff --git a/examples/protocols/mqtt/tcp/sdkconfig.defaults b/examples/protocols/mqtt/tcp/sdkconfig.defaults deleted file mode 100644 index 51c8139..0000000 --- a/examples/protocols/mqtt/tcp/sdkconfig.defaults +++ /dev/null @@ -1,7 +0,0 @@ -CONFIG_BROKER_URL="FROM_STDIN" -CONFIG_LOG_DEFAULT_LEVEL_NONE= -CONFIG_LOG_DEFAULT_LEVEL_ERROR= -CONFIG_LOG_DEFAULT_LEVEL_WARN= -CONFIG_LOG_DEFAULT_LEVEL_INFO= -CONFIG_LOG_DEFAULT_LEVEL_DEBUG=y -CONFIG_LOG_DEFAULT_LEVEL_VERBOSE= From 6b3ba4d55a38aaa586d8e296970b78f2ac52636f Mon Sep 17 00:00:00 2001 From: David Cermak Date: Fri, 16 Nov 2018 10:42:58 +0000 Subject: [PATCH 012/231] mqtt: support for BEFORE_CONNECT event in idf Updated examples to use new event id, idf to use mqtt with fixed retained, oversized messages --- examples/protocols/mqtt/ssl/main/app_main.c | 3 +++ examples/protocols/mqtt/ssl_mutual_auth/main/app_main.c | 3 +++ examples/protocols/mqtt/tcp/main/app_main.c | 3 +++ examples/protocols/mqtt/ws/main/app_main.c | 3 +++ examples/protocols/mqtt/wss/main/app_main.c | 3 +++ 5 files changed, 15 insertions(+) diff --git a/examples/protocols/mqtt/ssl/main/app_main.c b/examples/protocols/mqtt/ssl/main/app_main.c index 83f0b8a..3be4d57 100644 --- a/examples/protocols/mqtt/ssl/main/app_main.c +++ b/examples/protocols/mqtt/ssl/main/app_main.c @@ -112,6 +112,9 @@ static esp_err_t mqtt_event_handler(esp_mqtt_event_handle_t event) case MQTT_EVENT_ERROR: ESP_LOGI(TAG, "MQTT_EVENT_ERROR"); break; + default: + ESP_LOGI(TAG, "Other event id:%d", event->event_id); + break; } return ESP_OK; } diff --git a/examples/protocols/mqtt/ssl_mutual_auth/main/app_main.c b/examples/protocols/mqtt/ssl_mutual_auth/main/app_main.c index 2ebce48..62b9700 100644 --- a/examples/protocols/mqtt/ssl_mutual_auth/main/app_main.c +++ b/examples/protocols/mqtt/ssl_mutual_auth/main/app_main.c @@ -114,6 +114,9 @@ static esp_err_t mqtt_event_handler(esp_mqtt_event_handle_t event) case MQTT_EVENT_ERROR: ESP_LOGI(TAG, "MQTT_EVENT_ERROR"); break; + default: + ESP_LOGI(TAG, "Other event id:%d", event->event_id); + break; } return ESP_OK; } diff --git a/examples/protocols/mqtt/tcp/main/app_main.c b/examples/protocols/mqtt/tcp/main/app_main.c index 0d294bd..ce1640a 100644 --- a/examples/protocols/mqtt/tcp/main/app_main.c +++ b/examples/protocols/mqtt/tcp/main/app_main.c @@ -69,6 +69,9 @@ static esp_err_t mqtt_event_handler(esp_mqtt_event_handle_t event) case MQTT_EVENT_ERROR: ESP_LOGI(TAG, "MQTT_EVENT_ERROR"); break; + default: + ESP_LOGI(TAG, "Other event id:%d", event->event_id); + break; } return ESP_OK; } diff --git a/examples/protocols/mqtt/ws/main/app_main.c b/examples/protocols/mqtt/ws/main/app_main.c index cdb0f0a..bcaf5c8 100644 --- a/examples/protocols/mqtt/ws/main/app_main.c +++ b/examples/protocols/mqtt/ws/main/app_main.c @@ -66,6 +66,9 @@ static esp_err_t mqtt_event_handler(esp_mqtt_event_handle_t event) case MQTT_EVENT_ERROR: ESP_LOGI(TAG, "MQTT_EVENT_ERROR"); break; + default: + ESP_LOGI(TAG, "Other event id:%d", event->event_id); + break; } return ESP_OK; } diff --git a/examples/protocols/mqtt/wss/main/app_main.c b/examples/protocols/mqtt/wss/main/app_main.c index bc1d045..1203117 100644 --- a/examples/protocols/mqtt/wss/main/app_main.c +++ b/examples/protocols/mqtt/wss/main/app_main.c @@ -112,6 +112,9 @@ static esp_err_t mqtt_event_handler(esp_mqtt_event_handle_t event) case MQTT_EVENT_ERROR: ESP_LOGI(TAG, "MQTT_EVENT_ERROR"); break; + default: + ESP_LOGI(TAG, "Other event id:%d", event->event_id); + break; } return ESP_OK; } From e38e60f77ee7fb2bad8edec61cb0cd3af34ba3e0 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Fri, 7 Dec 2018 15:15:34 +0100 Subject: [PATCH 013/231] mqtt tests: connect to local broker when running in CI to make the tests more reliable --- .../protocols/mqtt/ssl/main/Kconfig.projbuild | 16 +++ examples/protocols/mqtt/ssl/main/app_main.c | 9 +- .../mqtt/ssl/mqtt_ssl_example_test.py | 100 ++++++++++-------- examples/protocols/mqtt/ssl/sdkconfig.ci | 2 + .../protocols/mqtt/tcp/main/Kconfig.projbuild | 4 +- .../protocols/mqtt/ws/main/Kconfig.projbuild | 6 ++ examples/protocols/mqtt/ws/main/app_main.c | 2 +- .../protocols/mqtt/ws/mqtt_ws_example_test.py | 94 ++++++++-------- examples/protocols/mqtt/ws/sdkconfig.ci | 1 + .../protocols/mqtt/wss/main/Kconfig.projbuild | 16 +++ examples/protocols/mqtt/wss/main/app_main.c | 8 +- .../mqtt/wss/mqtt_wss_example_test.py | 95 ++++++++++------- examples/protocols/mqtt/wss/sdkconfig.ci | 3 + 13 files changed, 224 insertions(+), 132 deletions(-) create mode 100644 examples/protocols/mqtt/ssl/sdkconfig.ci create mode 100644 examples/protocols/mqtt/ws/sdkconfig.ci create mode 100644 examples/protocols/mqtt/wss/sdkconfig.ci diff --git a/examples/protocols/mqtt/ssl/main/Kconfig.projbuild b/examples/protocols/mqtt/ssl/main/Kconfig.projbuild index 176d8fb..8b4eda4 100644 --- a/examples/protocols/mqtt/ssl/main/Kconfig.projbuild +++ b/examples/protocols/mqtt/ssl/main/Kconfig.projbuild @@ -12,4 +12,20 @@ config WIFI_PASSWORD help WiFi password (WPA or WPA2) for the example to use. +config BROKER_URI + string "Broker URL" + default "mqtts://iot.eclipse.org:8883" + help + URL of an mqtt broker which this example connects to. + +config BROKER_CERTIFICATE_OVERRIDE + string "Broker certificate override" + default "" + help + Please leave empty if broker certificate included from a textfile; otherwise fill in a base64 part of PEM format certificate + +config BROKER_CERTIFICATE_OVERRIDDEN + bool + default y if BROKER_CERTIFICATE_OVERRIDE != "" + endmenu diff --git a/examples/protocols/mqtt/ssl/main/app_main.c b/examples/protocols/mqtt/ssl/main/app_main.c index 3be4d57..86697a0 100644 --- a/examples/protocols/mqtt/ssl/main/app_main.c +++ b/examples/protocols/mqtt/ssl/main/app_main.c @@ -69,7 +69,11 @@ static void wifi_init(void) xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT, false, true, portMAX_DELAY); } -extern const uint8_t iot_eclipse_org_pem_start[] asm("_binary_iot_eclipse_org_pem_start"); +#if CONFIG_BROKER_CERTIFICATE_OVERRIDDEN == 1 +static const uint8_t iot_eclipse_org_pem_start[] = "-----BEGIN CERTIFICATE-----\n" CONFIG_BROKER_CERTIFICATE_OVERRIDE "\n-----END CERTIFICATE-----"; +#else +extern const uint8_t iot_eclipse_org_pem_start[] asm("_binary_iot_eclipse_org_pem_start"); +#endif extern const uint8_t iot_eclipse_org_pem_end[] asm("_binary_iot_eclipse_org_pem_end"); static esp_err_t mqtt_event_handler(esp_mqtt_event_handle_t event) @@ -119,10 +123,11 @@ static esp_err_t mqtt_event_handler(esp_mqtt_event_handle_t event) return ESP_OK; } + static void mqtt_app_start(void) { const esp_mqtt_client_config_t mqtt_cfg = { - .uri = "mqtts://iot.eclipse.org:8883", + .uri = CONFIG_BROKER_URI, .event_handle = mqtt_event_handler, .cert_pem = (const char *)iot_eclipse_org_pem_start, }; diff --git a/examples/protocols/mqtt/ssl/mqtt_ssl_example_test.py b/examples/protocols/mqtt/ssl/mqtt_ssl_example_test.py index 5f47580..7e66482 100644 --- a/examples/protocols/mqtt/ssl/mqtt_ssl_example_test.py +++ b/examples/protocols/mqtt/ssl/mqtt_ssl_example_test.py @@ -1,9 +1,13 @@ +from __future__ import print_function +from __future__ import unicode_literals +from builtins import str import re import os import sys -import time import ssl import paho.mqtt.client as mqtt +from threading import Thread, Event + try: import IDF @@ -19,37 +23,40 @@ except ImportError: import DUT -g_recv_data = "" -g_recv_topic = "" -g_broker_connected = 0 + +event_client_connected = Event() +event_stop_client = Event() +event_client_received_correct = Event() +message_log = "" # The callback for when the client receives a CONNACK response from the server. def on_connect(client, userdata, flags, rc): - global g_broker_connected print("Connected with result code " + str(rc)) - g_broker_connected = 1 + event_client_connected.set() client.subscribe("/topic/qos0") +def mqtt_client_task(client): + while not event_stop_client.is_set(): + client.loop() + + # The callback for when a PUBLISH message is received from the server. def on_message(client, userdata, msg): - global g_recv_topic - global g_recv_data + global message_log payload = msg.payload.decode() - if g_recv_data == "" and payload == "data": + if not event_client_received_correct.is_set() and payload == "data": client.publish("/topic/qos0", "data_to_esp32") - g_recv_topic = msg.topic - g_recv_data = payload - print(msg.topic + " " + str(payload)) + if msg.topic == "/topic/qos0" and payload == "data": + event_client_received_correct.set() + message_log += "Received data:" + msg.topic + " " + payload + "\n" @IDF.idf_example_test(env_tag="Example_WIFI") def test_examples_protocol_mqtt_ssl(env, extra_data): - global g_recv_topic - global g_recv_data - global g_broker_connected - broker_url = "iot.eclipse.org" + broker_url = "" + broker_port = 0 """ steps: | 1. join AP and connects to ssl broker @@ -61,15 +68,20 @@ def test_examples_protocol_mqtt_ssl(env, extra_data): # check and log bin size binary_file = os.path.join(dut1.app.binary_path, "mqtt_ssl.bin") bin_size = os.path.getsize(binary_file) - IDF.log_performance("mqtt_ssl_bin_size", "{}KB".format(bin_size // 1024)) + IDF.log_performance("mqtt_ssl_bin_size", "{}KB" + .format(bin_size // 1024)) IDF.check_performance("mqtt_ssl_size", bin_size // 1024) - # 1. start test (and check the environment is healthy) - dut1.start_app() - client = None - # 2. Test connects to a broker + # Look for host:port in sdkconfig + try: + value = re.search(r'\:\/\/([^:]+)\:([0-9]+)', dut1.app.get_sdkconfig()["CONFIG_BROKER_URI"]) + broker_url = value.group(1) + broker_port = int(value.group(2)) + except Exception: + print('ENV_TEST_FAILURE: Cannot find broker url in sdkconfig') + raise + client = None + # 1. Test connects to a broker try: - ip_address = dut1.expect(re.compile(r" sta ip: ([^,]+),"), timeout=30) - print("Connected to AP with IP: {}".format(ip_address)) client = mqtt.Client() client.on_connect = on_connect client.on_message = on_message @@ -78,28 +90,32 @@ def test_examples_protocol_mqtt_ssl(env, extra_data): None, cert_reqs=ssl.CERT_NONE, tls_version=ssl.PROTOCOL_TLSv1, ciphers=None) client.tls_insecure_set(True) print("Connecting...") - client.connect(broker_url, 8883, 60) - print("...done") - except DUT.ExpectTimeout: - raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP') + client.connect(broker_url, broker_port, 60) except Exception: print("ENV_TEST_FAILURE: Unexpected error while connecting to broker {}: {}:".format(broker_url, sys.exc_info()[0])) raise - print("Start Looping...") - start = time.time() - while (time.time() - start) <= 20: - client.loop() - print("...done") - if g_broker_connected == 0: - raise ValueError('ENV_TEST_FAILURE: Test script cannot connect to broker: {}'.format(broker_url)) - # 3. check the message received back from the server - if g_recv_topic == "/topic/qos0" and g_recv_data == "data": - print("PASS: Received correct message") - else: - print("Failure!") - raise ValueError('Wrong data received topic: {}, data:{}'.format(g_recv_topic, g_recv_data)) - # 4. check that the esp32 client received data sent by this python client - dut1.expect(re.compile(r"DATA=data_to_esp32"), timeout=30) + # Starting a py-client in a separate thread + thread1 = Thread(target=mqtt_client_task, args=(client,)) + thread1.start() + try: + print("Connecting py-client to broker {}:{}...".format(broker_url, broker_port)) + if not event_client_connected.wait(timeout=30): + raise ValueError("ENV_TEST_FAILURE: Test script cannot connect to broker: {}".format(broker_url)) + 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 + print("Checking py-client received msg published from esp...") + if not event_client_received_correct.wait(timeout=30): + raise ValueError('Wrong data received, msg log: {}'.format(message_log)) + print("Checking esp-client received msg published from py-client...") + dut1.expect(re.compile(r"DATA=data_to_esp32"), timeout=30) + finally: + event_stop_client.set() + thread1.join() if __name__ == '__main__': diff --git a/examples/protocols/mqtt/ssl/sdkconfig.ci b/examples/protocols/mqtt/ssl/sdkconfig.ci new file mode 100644 index 0000000..ce328a6 --- /dev/null +++ b/examples/protocols/mqtt/ssl/sdkconfig.ci @@ -0,0 +1,2 @@ +CONFIG_BROKER_URI="mqtts://${EXAMPLE_MQTT_BROKER_SSL}" +CONFIG_BROKER_CERTIFICATE_OVERRIDE="${EXAMPLE_MQTT_BROKER_CERTIFICATE}" diff --git a/examples/protocols/mqtt/tcp/main/Kconfig.projbuild b/examples/protocols/mqtt/tcp/main/Kconfig.projbuild index bb4194d..71f95ea 100644 --- a/examples/protocols/mqtt/tcp/main/Kconfig.projbuild +++ b/examples/protocols/mqtt/tcp/main/Kconfig.projbuild @@ -19,7 +19,7 @@ config BROKER_URL URL of the broker to connect to config BROKER_URL_FROM_STDIN - bool - default y if BROKER_URL = "FROM_STDIN" + bool + default y if BROKER_URL = "FROM_STDIN" endmenu diff --git a/examples/protocols/mqtt/ws/main/Kconfig.projbuild b/examples/protocols/mqtt/ws/main/Kconfig.projbuild index 176d8fb..5223e88 100644 --- a/examples/protocols/mqtt/ws/main/Kconfig.projbuild +++ b/examples/protocols/mqtt/ws/main/Kconfig.projbuild @@ -12,4 +12,10 @@ config WIFI_PASSWORD help WiFi password (WPA or WPA2) for the example to use. +config BROKER_URI + string "Broker URL" + default "ws://iot.eclipse.org:80/ws" + help + URL of an mqtt broker which this example connects to. + endmenu diff --git a/examples/protocols/mqtt/ws/main/app_main.c b/examples/protocols/mqtt/ws/main/app_main.c index bcaf5c8..024be48 100644 --- a/examples/protocols/mqtt/ws/main/app_main.c +++ b/examples/protocols/mqtt/ws/main/app_main.c @@ -118,7 +118,7 @@ static void wifi_init(void) static void mqtt_app_start(void) { const esp_mqtt_client_config_t mqtt_cfg = { - .uri = "ws://iot.eclipse.org:80/ws", + .uri = CONFIG_BROKER_URI, .event_handle = mqtt_event_handler, // .user_context = (void *)your_context }; diff --git a/examples/protocols/mqtt/ws/mqtt_ws_example_test.py b/examples/protocols/mqtt/ws/mqtt_ws_example_test.py index 3567cae..0578876 100644 --- a/examples/protocols/mqtt/ws/mqtt_ws_example_test.py +++ b/examples/protocols/mqtt/ws/mqtt_ws_example_test.py @@ -4,8 +4,9 @@ from builtins import str import re import os import sys -import time import paho.mqtt.client as mqtt +from threading import Thread, Event + try: import IDF @@ -21,38 +22,39 @@ except Exception: import DUT - -g_recv_data = "" -g_recv_topic = "" -g_broker_connected = 0 +event_client_connected = Event() +event_stop_client = Event() +event_client_received_correct = Event() +message_log = "" # The callback for when the client receives a CONNACK response from the server. def on_connect(client, userdata, flags, rc): - global g_broker_connected print("Connected with result code " + str(rc)) - g_broker_connected = 1 + event_client_connected.set() client.subscribe("/topic/qos0") +def mqtt_client_task(client): + while not event_stop_client.is_set(): + client.loop() + + # The callback for when a PUBLISH message is received from the server. def on_message(client, userdata, msg): - global g_recv_topic - global g_recv_data + global message_log payload = msg.payload.decode() - if g_recv_data == "" and payload == "data": + if not event_client_received_correct.is_set() and payload == "data": client.publish("/topic/qos0", "data_to_esp32") - g_recv_topic = msg.topic - g_recv_data = payload - print(msg.topic + " " + payload) + if msg.topic == "/topic/qos0" and payload == "data": + event_client_received_correct.set() + message_log += "Received data:" + msg.topic + " " + payload + "\n" @IDF.idf_example_test(env_tag="Example_WIFI") def test_examples_protocol_mqtt_ws(env, extra_data): - global g_recv_topic - global g_recv_data - global g_broker_connected - broker_url = "iot.eclipse.org" + broker_url = "" + broker_port = 0 """ steps: | 1. join AP and connects to ws broker @@ -66,40 +68,48 @@ def test_examples_protocol_mqtt_ws(env, extra_data): bin_size = os.path.getsize(binary_file) IDF.log_performance("mqtt_websocket_bin_size", "{}KB".format(bin_size // 1024)) IDF.check_performance("mqtt_websocket_size", bin_size // 1024) - # 1. start test (and check the environment is healthy) - dut1.start_app() - client = None - # 2. Test connects to a broker + # Look for host:port in sdkconfig + try: + value = re.search(r'\:\/\/([^:]+)\:([0-9]+)', dut1.app.get_sdkconfig()["CONFIG_BROKER_URI"]) + broker_url = value.group(1) + broker_port = int(value.group(2)) + except Exception: + print('ENV_TEST_FAILURE: Cannot find broker url in sdkconfig') + raise + client = None + # 1. Test connects to a broker try: - ip_address = dut1.expect(re.compile(r" sta ip: ([^,]+),"), timeout=30) - print("Connected to AP with IP: {}".format(ip_address)) client = mqtt.Client(transport="websockets") client.on_connect = on_connect client.on_message = on_message client.ws_set_options(path="/ws", headers=None) print("Connecting...") - client.connect(broker_url, 80, 60) - print("...done") - except DUT.ExpectTimeout: - raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP') + client.connect(broker_url, broker_port, 60) except Exception: print("ENV_TEST_FAILURE: Unexpected error while connecting to broker {}: {}:".format(broker_url, sys.exc_info()[0])) raise - print("Start Looping...") - start = time.time() - while (time.time() - start) <= 20: - client.loop() - print("...done") - if g_broker_connected == 0: - raise ValueError('ENV_TEST_FAILURE: Test script cannot connect to broker: {}'.format(broker_url)) - # 3. check the message received back from the server - if g_recv_topic == "/topic/qos0" and g_recv_data == "data": - print("PASS: Received correct message") - else: - print("Failure!") - raise ValueError('Wrong data received topic: {}, data:{}'.format(g_recv_topic, g_recv_data)) - # 4. check that the esp32 client received data sent by this python client - dut1.expect(re.compile(r"DATA=data_to_esp32"), timeout=30) + # Starting a py-client in a separate thread + thread1 = Thread(target=mqtt_client_task, args=(client,)) + thread1.start() + try: + print("Connecting py-client to broker {}:{}...".format(broker_url, broker_port)) + if not event_client_connected.wait(timeout=30): + raise ValueError("ENV_TEST_FAILURE: Test script cannot connect to broker: {}".format(broker_url)) + 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 + print("Checking py-client received msg published from esp...") + if not event_client_received_correct.wait(timeout=30): + raise ValueError('Wrong data received, msg log: {}'.format(message_log)) + print("Checking esp-client received msg published from py-client...") + dut1.expect(re.compile(r"DATA=data_to_esp32"), timeout=30) + finally: + event_stop_client.set() + thread1.join() if __name__ == '__main__': diff --git a/examples/protocols/mqtt/ws/sdkconfig.ci b/examples/protocols/mqtt/ws/sdkconfig.ci new file mode 100644 index 0000000..4f14eef --- /dev/null +++ b/examples/protocols/mqtt/ws/sdkconfig.ci @@ -0,0 +1 @@ +CONFIG_BROKER_URI="ws://${EXAMPLE_MQTT_BROKER_WS}/ws" diff --git a/examples/protocols/mqtt/wss/main/Kconfig.projbuild b/examples/protocols/mqtt/wss/main/Kconfig.projbuild index 176d8fb..964d436 100644 --- a/examples/protocols/mqtt/wss/main/Kconfig.projbuild +++ b/examples/protocols/mqtt/wss/main/Kconfig.projbuild @@ -12,4 +12,20 @@ config WIFI_PASSWORD help WiFi password (WPA or WPA2) for the example to use. +config BROKER_URI + string "Broker URL" + default "wss://iot.eclipse.org:443/ws" + help + URL of an mqtt broker which this example connects to. + +config BROKER_CERTIFICATE_OVERRIDE + string "Server certificate override" + default "" + help + Please leave empty if server certificate included from a textfile; otherwise fill in a base64 part of PEM format certificate + +config BROKER_CERTIFICATE_OVERRIDDEN + bool + default y if BROKER_CERTIFICATE_OVERRIDE != "" + endmenu diff --git a/examples/protocols/mqtt/wss/main/app_main.c b/examples/protocols/mqtt/wss/main/app_main.c index 1203117..5059549 100644 --- a/examples/protocols/mqtt/wss/main/app_main.c +++ b/examples/protocols/mqtt/wss/main/app_main.c @@ -69,7 +69,11 @@ static void wifi_init(void) xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT, false, true, portMAX_DELAY); } -extern const uint8_t iot_eclipse_org_pem_start[] asm("_binary_iot_eclipse_org_pem_start"); +#if CONFIG_BROKER_CERTIFICATE_OVERRIDDEN == 1 +static const uint8_t iot_eclipse_org_pem_start[] = "-----BEGIN CERTIFICATE-----\n" CONFIG_BROKER_CERTIFICATE_OVERRIDE "\n-----END CERTIFICATE-----"; +#else +extern const uint8_t iot_eclipse_org_pem_start[] asm("_binary_iot_eclipse_org_pem_start"); +#endif extern const uint8_t iot_eclipse_org_pem_end[] asm("_binary_iot_eclipse_org_pem_end"); static esp_err_t mqtt_event_handler(esp_mqtt_event_handle_t event) @@ -122,7 +126,7 @@ static esp_err_t mqtt_event_handler(esp_mqtt_event_handle_t event) static void mqtt_app_start(void) { const esp_mqtt_client_config_t mqtt_cfg = { - .uri = "wss://iot.eclipse.org:443/ws", + .uri = CONFIG_BROKER_URI, .event_handle = mqtt_event_handler, .cert_pem = (const char *)iot_eclipse_org_pem_start, }; diff --git a/examples/protocols/mqtt/wss/mqtt_wss_example_test.py b/examples/protocols/mqtt/wss/mqtt_wss_example_test.py index b1515ae..58ce560 100644 --- a/examples/protocols/mqtt/wss/mqtt_wss_example_test.py +++ b/examples/protocols/mqtt/wss/mqtt_wss_example_test.py @@ -1,10 +1,13 @@ from __future__ import unicode_literals +from __future__ import unicode_literals +from builtins import str import re import os import sys -import time import ssl import paho.mqtt.client as mqtt +from threading import Thread, Event + try: import IDF @@ -20,37 +23,39 @@ except ImportError: import DUT -g_recv_data = "" -g_recv_topic = "" -g_broker_connected = 0 +event_client_connected = Event() +event_stop_client = Event() +event_client_received_correct = Event() +message_log = "" # The callback for when the client receives a CONNACK response from the server. def on_connect(client, userdata, flags, rc): - global g_broker_connected print("Connected with result code " + str(rc)) - g_broker_connected = 1 + event_client_connected.set() client.subscribe("/topic/qos0") +def mqtt_client_task(client): + while not event_stop_client.is_set(): + client.loop() + + # The callback for when a PUBLISH message is received from the server. def on_message(client, userdata, msg): - global g_recv_topic - global g_recv_data + global message_log payload = msg.payload.decode() - if g_recv_data == "" and payload == "data": + if not event_client_received_correct.is_set() and payload == "data": client.publish("/topic/qos0", "data_to_esp32") - g_recv_topic = msg.topic - g_recv_data = payload - print(msg.topic + " " + str(payload)) + if msg.topic == "/topic/qos0" and payload == "data": + event_client_received_correct.set() + message_log += "Received data:" + msg.topic + " " + payload + "\n" @IDF.idf_example_test(env_tag="Example_WIFI") def test_examples_protocol_mqtt_wss(env, extra_data): - global g_recv_topic - global g_recv_data - global g_broker_connected - broker_url = "iot.eclipse.org" + broker_url = "" + broker_port = 0 """ steps: | 1. join AP and connects to wss broker @@ -64,13 +69,17 @@ def test_examples_protocol_mqtt_wss(env, extra_data): bin_size = os.path.getsize(binary_file) IDF.log_performance("mqtt_websocket_secure_bin_size", "{}KB".format(bin_size // 1024)) IDF.check_performance("mqtt_websocket_secure_size", bin_size // 1024) - # 1. start test (and check the environment is healthy) - dut1.start_app() - client = None - # 2. Test connects to a broker + # Look for host:port in sdkconfig + try: + value = re.search(r'\:\/\/([^:]+)\:([0-9]+)', dut1.app.get_sdkconfig()["CONFIG_BROKER_URI"]) + broker_url = value.group(1) + broker_port = int(value.group(2)) + except Exception: + print('ENV_TEST_FAILURE: Cannot find broker url in sdkconfig') + raise + client = None + # 1. Test connects to a broker try: - ip_address = dut1.expect(re.compile(r" sta ip: ([^,]+),"), timeout=30) - print("Connected to AP with IP: {}".format(ip_address)) client = mqtt.Client(transport="websockets") client.on_connect = on_connect client.on_message = on_message @@ -78,28 +87,32 @@ def test_examples_protocol_mqtt_wss(env, extra_data): None, None, cert_reqs=ssl.CERT_NONE, tls_version=ssl.PROTOCOL_TLSv1, ciphers=None) print("Connecting...") - client.connect(broker_url, 443, 60) - print("...done") - except DUT.ExpectTimeout: - raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP') + client.connect(broker_url, broker_port, 60) except Exception: print("ENV_TEST_FAILURE: Unexpected error while connecting to broker {}: {}:".format(broker_url, sys.exc_info()[0])) raise - print("Start Looping...") - start = time.time() - while (time.time() - start) <= 20: - client.loop() - print("...done") - if g_broker_connected == 0: - raise ValueError('ENV_TEST_FAILURE: Test script cannot connect to broker: {}'.format(broker_url)) - # 3. check the message received back from the server - if g_recv_topic == "/topic/qos0" and g_recv_data == "data": - print("PASS: Received correct message") - else: - print("Failure!") - raise ValueError('Wrong data received topic: {}, data:{}'.format(g_recv_topic, g_recv_data)) - # 4. check that the esp32 client received data sent by this python client - dut1.expect(re.compile(r"DATA=data_to_esp32"), timeout=30) + # Starting a py-client in a separate thread + thread1 = Thread(target=mqtt_client_task, args=(client,)) + thread1.start() + try: + print("Connecting py-client to broker {}:{}...".format(broker_url, broker_port)) + if not event_client_connected.wait(timeout=30): + raise ValueError("ENV_TEST_FAILURE: Test script cannot connect to broker: {}".format(broker_url)) + 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 + print("Checking py-client received msg published from esp...") + if not event_client_received_correct.wait(timeout=30): + raise ValueError('Wrong data received, msg log: {}'.format(message_log)) + print("Checking esp-client received msg published from py-client...") + dut1.expect(re.compile(r"DATA=data_to_esp32"), timeout=30) + finally: + event_stop_client.set() + thread1.join() if __name__ == '__main__': diff --git a/examples/protocols/mqtt/wss/sdkconfig.ci b/examples/protocols/mqtt/wss/sdkconfig.ci new file mode 100644 index 0000000..d0dd492 --- /dev/null +++ b/examples/protocols/mqtt/wss/sdkconfig.ci @@ -0,0 +1,3 @@ +CONFIG_BROKER_URI="wss://${EXAMPLE_MQTT_BROKER_WSS}/ws" +CONFIG_BROKER_CERTIFICATE_OVERRIDE="${EXAMPLE_MQTT_BROKER_CERTIFICATE}" + From 79e16da5c4529edb085aebac098b3cd46a201456 Mon Sep 17 00:00:00 2001 From: Roland Dobai Date: Fri, 25 Jan 2019 17:10:53 +0100 Subject: [PATCH 014/231] Correct Kconfigs according to the coding style --- components/mqtt/Kconfig | 173 +++++++++--------- .../protocols/mqtt/ssl/main/Kconfig.projbuild | 47 ++--- .../ssl_mutual_auth/main/Kconfig.projbuild | 20 +- .../protocols/mqtt/tcp/main/Kconfig.projbuild | 36 ++-- .../protocols/mqtt/ws/main/Kconfig.projbuild | 30 +-- .../protocols/mqtt/wss/main/Kconfig.projbuild | 47 ++--- 6 files changed, 178 insertions(+), 175 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 diff --git a/examples/protocols/mqtt/ssl/main/Kconfig.projbuild b/examples/protocols/mqtt/ssl/main/Kconfig.projbuild index 8b4eda4..7bd1fcb 100644 --- a/examples/protocols/mqtt/ssl/main/Kconfig.projbuild +++ b/examples/protocols/mqtt/ssl/main/Kconfig.projbuild @@ -1,31 +1,32 @@ menu "Example Configuration" -config WIFI_SSID - string "WiFi SSID" - default "myssid" - help - SSID (network name) for the example to connect to. + config WIFI_SSID + string "WiFi SSID" + default "myssid" + help + SSID (network name) for the example to connect to. -config WIFI_PASSWORD - string "WiFi Password" - default "mypassword" - help - WiFi password (WPA or WPA2) for the example to use. + config WIFI_PASSWORD + string "WiFi Password" + default "mypassword" + help + WiFi password (WPA or WPA2) for the example to use. -config BROKER_URI - string "Broker URL" - default "mqtts://iot.eclipse.org:8883" - help - URL of an mqtt broker which this example connects to. + config BROKER_URI + string "Broker URL" + default "mqtts://iot.eclipse.org:8883" + help + URL of an mqtt broker which this example connects to. -config BROKER_CERTIFICATE_OVERRIDE - string "Broker certificate override" - default "" - help - Please leave empty if broker certificate included from a textfile; otherwise fill in a base64 part of PEM format certificate + config BROKER_CERTIFICATE_OVERRIDE + string "Broker certificate override" + default "" + help + Please leave empty if broker certificate included from a textfile; otherwise fill in a base64 part of PEM + format certificate -config BROKER_CERTIFICATE_OVERRIDDEN - bool - default y if BROKER_CERTIFICATE_OVERRIDE != "" + config BROKER_CERTIFICATE_OVERRIDDEN + bool + default y if BROKER_CERTIFICATE_OVERRIDE != "" endmenu diff --git a/examples/protocols/mqtt/ssl_mutual_auth/main/Kconfig.projbuild b/examples/protocols/mqtt/ssl_mutual_auth/main/Kconfig.projbuild index 176d8fb..ed4e691 100644 --- a/examples/protocols/mqtt/ssl_mutual_auth/main/Kconfig.projbuild +++ b/examples/protocols/mqtt/ssl_mutual_auth/main/Kconfig.projbuild @@ -1,15 +1,15 @@ menu "Example Configuration" -config WIFI_SSID - string "WiFi SSID" - default "myssid" - help - SSID (network name) for the example to connect to. + config WIFI_SSID + string "WiFi SSID" + default "myssid" + help + SSID (network name) for the example to connect to. -config WIFI_PASSWORD - string "WiFi Password" - default "mypassword" - help - WiFi password (WPA or WPA2) for the example to use. + config WIFI_PASSWORD + string "WiFi Password" + default "mypassword" + help + WiFi password (WPA or WPA2) for the example to use. endmenu diff --git a/examples/protocols/mqtt/tcp/main/Kconfig.projbuild b/examples/protocols/mqtt/tcp/main/Kconfig.projbuild index 71f95ea..f5c519b 100644 --- a/examples/protocols/mqtt/tcp/main/Kconfig.projbuild +++ b/examples/protocols/mqtt/tcp/main/Kconfig.projbuild @@ -1,25 +1,25 @@ menu "Example Configuration" -config WIFI_SSID - string "WiFi SSID" - default "myssid" - help - SSID (network name) for the example to connect to. + config WIFI_SSID + string "WiFi SSID" + default "myssid" + help + SSID (network name) for the example to connect to. -config WIFI_PASSWORD - string "WiFi Password" - default "mypassword" - help - WiFi password (WPA or WPA2) for the example to use. + config WIFI_PASSWORD + string "WiFi Password" + default "mypassword" + help + WiFi password (WPA or WPA2) for the example to use. -config BROKER_URL - string "Broker URL" - default "mqtt://iot.eclipse.org" - help - URL of the broker to connect to + config BROKER_URL + string "Broker URL" + default "mqtt://iot.eclipse.org" + help + URL of the broker to connect to -config BROKER_URL_FROM_STDIN - bool - default y if BROKER_URL = "FROM_STDIN" + config BROKER_URL_FROM_STDIN + bool + default y if BROKER_URL = "FROM_STDIN" endmenu diff --git a/examples/protocols/mqtt/ws/main/Kconfig.projbuild b/examples/protocols/mqtt/ws/main/Kconfig.projbuild index 5223e88..9e47709 100644 --- a/examples/protocols/mqtt/ws/main/Kconfig.projbuild +++ b/examples/protocols/mqtt/ws/main/Kconfig.projbuild @@ -1,21 +1,21 @@ menu "Example Configuration" -config WIFI_SSID - string "WiFi SSID" - default "myssid" - help - SSID (network name) for the example to connect to. + config WIFI_SSID + string "WiFi SSID" + default "myssid" + help + SSID (network name) for the example to connect to. -config WIFI_PASSWORD - string "WiFi Password" - default "mypassword" - help - WiFi password (WPA or WPA2) for the example to use. + config WIFI_PASSWORD + string "WiFi Password" + default "mypassword" + help + WiFi password (WPA or WPA2) for the example to use. -config BROKER_URI - string "Broker URL" - default "ws://iot.eclipse.org:80/ws" - help - URL of an mqtt broker which this example connects to. + config BROKER_URI + string "Broker URL" + default "ws://iot.eclipse.org:80/ws" + help + URL of an mqtt broker which this example connects to. endmenu diff --git a/examples/protocols/mqtt/wss/main/Kconfig.projbuild b/examples/protocols/mqtt/wss/main/Kconfig.projbuild index 964d436..a161740 100644 --- a/examples/protocols/mqtt/wss/main/Kconfig.projbuild +++ b/examples/protocols/mqtt/wss/main/Kconfig.projbuild @@ -1,31 +1,32 @@ menu "Example Configuration" -config WIFI_SSID - string "WiFi SSID" - default "myssid" - help - SSID (network name) for the example to connect to. + config WIFI_SSID + string "WiFi SSID" + default "myssid" + help + SSID (network name) for the example to connect to. -config WIFI_PASSWORD - string "WiFi Password" - default "mypassword" - help - WiFi password (WPA or WPA2) for the example to use. + config WIFI_PASSWORD + string "WiFi Password" + default "mypassword" + help + WiFi password (WPA or WPA2) for the example to use. -config BROKER_URI - string "Broker URL" - default "wss://iot.eclipse.org:443/ws" - help - URL of an mqtt broker which this example connects to. + config BROKER_URI + string "Broker URL" + default "wss://iot.eclipse.org:443/ws" + help + URL of an mqtt broker which this example connects to. -config BROKER_CERTIFICATE_OVERRIDE - string "Server certificate override" - default "" - help - Please leave empty if server certificate included from a textfile; otherwise fill in a base64 part of PEM format certificate + config BROKER_CERTIFICATE_OVERRIDE + string "Server certificate override" + default "" + help + Please leave empty if server certificate included from a textfile; otherwise fill in a base64 part of PEM + format certificate -config BROKER_CERTIFICATE_OVERRIDDEN - bool - default y if BROKER_CERTIFICATE_OVERRIDE != "" + config BROKER_CERTIFICATE_OVERRIDDEN + bool + default y if BROKER_CERTIFICATE_OVERRIDE != "" endmenu From 202acd4d6d9e9ca82c9362ebad769e88086919b0 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Thu, 31 Jan 2019 17:09:34 +0100 Subject: [PATCH 015/231] 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 +++++++++++++ .../mqtt/publish_test/CMakeLists.txt | 9 + examples/protocols/mqtt/publish_test/Makefile | 7 + .../protocols/mqtt/publish_test/README.md | 91 ++++++ .../mqtt/publish_test/main/CMakeLists.txt | 4 + .../mqtt/publish_test/main/Kconfig.projbuild | 62 +++++ .../mqtt/publish_test/main/component.mk | 1 + .../publish_test/main/iot_eclipse_org.pem | 27 ++ .../mqtt/publish_test/main/publish_test.c | 258 ++++++++++++++++++ .../protocols/mqtt/publish_test/sdkconfig.ci | 5 + 13 files changed, 665 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 create mode 100644 examples/protocols/mqtt/publish_test/CMakeLists.txt create mode 100644 examples/protocols/mqtt/publish_test/Makefile create mode 100644 examples/protocols/mqtt/publish_test/README.md create mode 100644 examples/protocols/mqtt/publish_test/main/CMakeLists.txt create mode 100644 examples/protocols/mqtt/publish_test/main/Kconfig.projbuild create mode 100644 examples/protocols/mqtt/publish_test/main/component.mk create mode 100644 examples/protocols/mqtt/publish_test/main/iot_eclipse_org.pem create mode 100644 examples/protocols/mqtt/publish_test/main/publish_test.c create mode 100644 examples/protocols/mqtt/publish_test/sdkconfig.ci 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() diff --git a/examples/protocols/mqtt/publish_test/CMakeLists.txt b/examples/protocols/mqtt/publish_test/CMakeLists.txt new file mode 100644 index 0000000..75f1619 --- /dev/null +++ b/examples/protocols/mqtt/publish_test/CMakeLists.txt @@ -0,0 +1,9 @@ +# The following four lines of boilerplate have to be in your project's CMakeLists +# in this exact order for cmake to work correctly +cmake_minimum_required(VERSION 3.5) + +include($ENV{IDF_PATH}/tools/cmake/project.cmake) + +project(mqtt_publish) + +target_add_binary_data(mqtt_publish.elf "main/iot_eclipse_org.pem" TEXT) diff --git a/examples/protocols/mqtt/publish_test/Makefile b/examples/protocols/mqtt/publish_test/Makefile new file mode 100644 index 0000000..d1341ae --- /dev/null +++ b/examples/protocols/mqtt/publish_test/Makefile @@ -0,0 +1,7 @@ +# +# This is a project Makefile. It is assumed the directory this Makefile resides in is a +# project subdirectory. +# +PROJECT_NAME := mqtt_publish + +include $(IDF_PATH)/make/project.mk diff --git a/examples/protocols/mqtt/publish_test/README.md b/examples/protocols/mqtt/publish_test/README.md new file mode 100644 index 0000000..bc4e02c --- /dev/null +++ b/examples/protocols/mqtt/publish_test/README.md @@ -0,0 +1,91 @@ +# ESP-MQTT advanced published test +(See the README.md file in the upper level 'examples' directory for more information about examples.) + +Main purpose of this example is to test the MQTT library to correctly publish and receive messages (of different size and sequences) over different transports. +It is possible to run this example manually without any test to exercise how the MQTT library deals with + +- reception of fragmented messages +- runtime updates of URI + +## How to use example + +This example waits for user input to provide these parameters: +- transport: string parameter, one of: tcp, ssl, ws, wss +- pattern: sample string to be transmitted as message +- pattern repeats: number of repeats of pattern in one MQTT message +- repeated: number of repeats ESP32 publishes the message, also ESP32 expects to receive the same message the same number of repeats +- qos: number specifying qos, one of: 0, 1, 2 + +### Hardware Required + +This example can be executed on any ESP32 board, the only required interface is WiFi and connection to internet. + +### Configure the project + +``` +make menuconfig +``` + +* Set serial port under Serial Flasher Options. + +* Set ssid and password for the board to connect to AP. + +* Set brokers for all 4 transports (TCP, SSL, WS, WSS), also set certificate if needed + +* Set topics for publishing from and to ESP32 + +### Build and Flash + +Build the project and flash it to the board, then run monitor tool to view serial output: + +``` +make -j4 flash monitor +``` + +(To exit the serial monitor, type ``Ctrl-]``.) + +See the Getting Started Guide for full steps to configure and use ESP-IDF to build projects. + +## Example Output + +``` +I (4730) event: sta ip: 192.168.0.125, mask: 255.255.255.0, gw: 192.168.0.2 +I (4730) PUBLISH_TEST: [APP] Free memory: 236728 bytes +I (4730) system_api: Base MAC address is not set, read default base MAC address from BLK0 of EFUSE +D (4740) MQTT_CLIENT: MQTT client_id=ESP32_09885C +I (31360) PUBLISH_TEST: PATTERN:1234 REPEATED:10 PUBLISHED:10 +``` +- User enters "tcp 1234 10 10 1" +``` +EXPECTED STRING 1234123412341234123412341234123412341234, SIZE:40 +W (31360) MQTT_CLIENT: Client asked to stop, but was not started +I (31360) PUBLISH_TEST: [TCP transport] Startup.. +D (31370) MQTT_CLIENT: Core selection disabled +I (31370) PUBLISH_TEST: Note free memory: 224652 bytes +I (31370) PUBLISH_TEST: Other event id:7 +D (31390) MQTT_CLIENT: Transport connected to mqtt://192.168.0.163:1883 +I (31400) MQTT_CLIENT: Sending MQTT CONNECT message, type: 1, id: 0000 +D (31410) MQTT_CLIENT: Connected +I (31410) PUBLISH_TEST: MQTT_EVENT_CONNECTED +D (31410) MQTT_CLIENT: mqtt_enqueue id: 31184, type=8 successful +D (31410) OUTBOX: ENQUEUE msgid=31184, msg_type=8, len=20, size=20 +D (31420) MQTT_CLIENT: Sent subscribe topic=/xxx.topic123, id: 31184, type=8 successful +I (31430) PUBLISH_TEST: sent subscribe successful, msg_id=31184 +D (31440) MQTT_CLIENT: mqtt_enqueue id: 16584, type=3 successful +D (31440) OUTBOX: ENQUEUE msgid=16584, msg_type=3, len=59, size=79 +I (31450) PUBLISH_TEST: [16584] Publishing... +D (31450) MQTT_CLIENT: msg_type=9, msg_id=31184 +D (31460) MQTT_CLIENT: pending_id=16584, pending_msg_count = 2 +D (31460) OUTBOX: DELETED msgid=31184, msg_type=8, remain size=59 +D (31470) MQTT_CLIENT: Subscribe successful +I (31470) PUBLISH_TEST: MQTT_EVENT_SUBSCRIBED, msg_id=31184 +D (31480) MQTT_CLIENT: msg_type=4, msg_id=16584 +D (31480) MQTT_CLIENT: pending_id=16584, pending_msg_count = 1 +D (31490) OUTBOX: DELETED msgid=16584, msg_type=3, remain size=0 +D (31500) MQTT_CLIENT: received MQTT_MSG_TYPE_PUBACK, finish QoS1 publish +I (31500) PUBLISH_TEST: MQTT_EVENT_PUBLISHED, msg_id=16584 +D (31510) MQTT_CLIENT: mqtt_enqueue id: 44615, type=3 successful +D (31520) OUTBOX: ENQUEUE msgid=44615, msg_type=3, len=59, size=59 +I (31530) PUBLISH_TEST: [44615] Publishing... +... +``` diff --git a/examples/protocols/mqtt/publish_test/main/CMakeLists.txt b/examples/protocols/mqtt/publish_test/main/CMakeLists.txt new file mode 100644 index 0000000..c3074a7 --- /dev/null +++ b/examples/protocols/mqtt/publish_test/main/CMakeLists.txt @@ -0,0 +1,4 @@ +set(COMPONENT_SRCS "publish_test.c") +set(COMPONENT_ADD_INCLUDEDIRS ".") + +register_component() diff --git a/examples/protocols/mqtt/publish_test/main/Kconfig.projbuild b/examples/protocols/mqtt/publish_test/main/Kconfig.projbuild new file mode 100644 index 0000000..3d534a4 --- /dev/null +++ b/examples/protocols/mqtt/publish_test/main/Kconfig.projbuild @@ -0,0 +1,62 @@ +menu "Example Configuration" + + config WIFI_SSID + string "WiFi SSID" + default "myssid" + help + SSID (network name) for the example to connect to. + + config WIFI_PASSWORD + string "WiFi Password" + default "mypassword" + help + WiFi password (WPA or WPA2) for the example to use. + + config BROKER_SSL_URI + string "Broker SSL URL" + default "mqtts://iot.eclipse.org:8883" + help + URL of an mqtt broker for ssl transport + + config BROKER_TCP_URI + string "Broker TCP URL" + default "mqtt://iot.eclipse.org:1883" + help + URL of an mqtt broker for tcp transport + + config BROKER_WS_URI + string "Broker WS URL" + default "ws://iot.eclipse.org:80/ws" + help + URL of an mqtt broker for ws transport + + config BROKER_WSS_URI + string "Broker WSS URL" + default "wss://iot.eclipse.org:443/ws" + help + URL of an mqtt broker for wss transport + + config PUBLISH_TOPIC + string "publish topic" + default "/topic/publish/esp2py" + help + topic to which esp32 client publishes + + config SUBSCIBE_TOPIC + string "subscribe topic" + default "/topic/subscribe/py2esp" + help + topic to which esp32 client subsribes (and expects data) + + config BROKER_CERTIFICATE_OVERRIDE + string "Broker certificate override" + default "" + help + Please leave empty if broker certificate included from a textfile; otherwise fill in a base64 part of PEM + format certificate + + config BROKER_CERTIFICATE_OVERRIDDEN + bool + default y if BROKER_CERTIFICATE_OVERRIDE != "" + +endmenu diff --git a/examples/protocols/mqtt/publish_test/main/component.mk b/examples/protocols/mqtt/publish_test/main/component.mk new file mode 100644 index 0000000..797c4a1 --- /dev/null +++ b/examples/protocols/mqtt/publish_test/main/component.mk @@ -0,0 +1 @@ +COMPONENT_EMBED_TXTFILES := iot_eclipse_org.pem diff --git a/examples/protocols/mqtt/publish_test/main/iot_eclipse_org.pem b/examples/protocols/mqtt/publish_test/main/iot_eclipse_org.pem new file mode 100644 index 0000000..edb593b --- /dev/null +++ b/examples/protocols/mqtt/publish_test/main/iot_eclipse_org.pem @@ -0,0 +1,27 @@ +-----BEGIN CERTIFICATE----- +MIIEkjCCA3qgAwIBAgIQCgFBQgAAAVOFc2oLheynCDANBgkqhkiG9w0BAQsFADA/ +MSQwIgYDVQQKExtEaWdpdGFsIFNpZ25hdHVyZSBUcnVzdCBDby4xFzAVBgNVBAMT +DkRTVCBSb290IENBIFgzMB4XDTE2MDMxNzE2NDA0NloXDTIxMDMxNzE2NDA0Nlow +SjELMAkGA1UEBhMCVVMxFjAUBgNVBAoTDUxldCdzIEVuY3J5cHQxIzAhBgNVBAMT +GkxldCdzIEVuY3J5cHQgQXV0aG9yaXR5IFgzMIIBIjANBgkqhkiG9w0BAQEFAAOC +AQ8AMIIBCgKCAQEAnNMM8FrlLke3cl03g7NoYzDq1zUmGSXhvb418XCSL7e4S0EF +q6meNQhY7LEqxGiHC6PjdeTm86dicbp5gWAf15Gan/PQeGdxyGkOlZHP/uaZ6WA8 +SMx+yk13EiSdRxta67nsHjcAHJyse6cF6s5K671B5TaYucv9bTyWaN8jKkKQDIZ0 +Z8h/pZq4UmEUEz9l6YKHy9v6Dlb2honzhT+Xhq+w3Brvaw2VFn3EK6BlspkENnWA +a6xK8xuQSXgvopZPKiAlKQTGdMDQMc2PMTiVFrqoM7hD8bEfwzB/onkxEz0tNvjj +/PIzark5McWvxI0NHWQWM6r6hCm21AvA2H3DkwIDAQABo4IBfTCCAXkwEgYDVR0T +AQH/BAgwBgEB/wIBADAOBgNVHQ8BAf8EBAMCAYYwfwYIKwYBBQUHAQEEczBxMDIG +CCsGAQUFBzABhiZodHRwOi8vaXNyZy50cnVzdGlkLm9jc3AuaWRlbnRydXN0LmNv +bTA7BggrBgEFBQcwAoYvaHR0cDovL2FwcHMuaWRlbnRydXN0LmNvbS9yb290cy9k +c3Ryb290Y2F4My5wN2MwHwYDVR0jBBgwFoAUxKexpHsscfrb4UuQdf/EFWCFiRAw +VAYDVR0gBE0wSzAIBgZngQwBAgEwPwYLKwYBBAGC3xMBAQEwMDAuBggrBgEFBQcC +ARYiaHR0cDovL2Nwcy5yb290LXgxLmxldHNlbmNyeXB0Lm9yZzA8BgNVHR8ENTAz +MDGgL6AthitodHRwOi8vY3JsLmlkZW50cnVzdC5jb20vRFNUUk9PVENBWDNDUkwu +Y3JsMB0GA1UdDgQWBBSoSmpjBH3duubRObemRWXv86jsoTANBgkqhkiG9w0BAQsF +AAOCAQEA3TPXEfNjWDjdGBX7CVW+dla5cEilaUcne8IkCJLxWh9KEik3JHRRHGJo +uM2VcGfl96S8TihRzZvoroed6ti6WqEBmtzw3Wodatg+VyOeph4EYpr/1wXKtx8/ +wApIvJSwtmVi4MFU5aMqrSDE6ea73Mj2tcMyo5jMd6jmeWUHK8so/joWUoHOUgwu +X4Po1QYz+3dszkDqMp4fklxBwXRsW10KXzPMTZ+sOPAveyxindmjkW8lGy+QsRlG +PfZ+G6Z6h7mjem0Y+iWlkYcV4PIWL1iwBi8saCbGS5jN2p8M+X+Q7UNKEkROb3N6 +KOqkqm57TH2H3eDJAkSnh6/DNFu0Qg== +-----END CERTIFICATE----- \ No newline at end of file diff --git a/examples/protocols/mqtt/publish_test/main/publish_test.c b/examples/protocols/mqtt/publish_test/main/publish_test.c new file mode 100644 index 0000000..6507318 --- /dev/null +++ b/examples/protocols/mqtt/publish_test/main/publish_test.c @@ -0,0 +1,258 @@ +/* MQTT publish test + + This example 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 +#include +#include +#include "esp_wifi.h" +#include "esp_system.h" +#include "nvs_flash.h" +#include "esp_event_loop.h" + +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "freertos/semphr.h" +#include "freertos/queue.h" +#include "freertos/event_groups.h" + +#include "lwip/sockets.h" +#include "lwip/dns.h" +#include "lwip/netdb.h" + +#include "esp_log.h" +#include "mqtt_client.h" + +static const char *TAG = "PUBLISH_TEST"; + +static EventGroupHandle_t wifi_event_group; +static EventGroupHandle_t mqtt_event_group; +const static int CONNECTED_BIT = BIT0; + +static esp_mqtt_client_handle_t mqtt_client = NULL; + +static char *expected_data = NULL; +static char *actual_data = NULL; +static size_t expected_size = 0; +static size_t expected_published = 0; +static size_t actual_published = 0; +static int qos_test = 0; + +static esp_err_t wifi_event_handler(void *ctx, system_event_t *event) +{ + switch (event->event_id) { + case SYSTEM_EVENT_STA_START: + esp_wifi_connect(); + break; + case SYSTEM_EVENT_STA_GOT_IP: + xEventGroupSetBits(wifi_event_group, CONNECTED_BIT); + + break; + case SYSTEM_EVENT_STA_DISCONNECTED: + esp_wifi_connect(); + xEventGroupClearBits(wifi_event_group, CONNECTED_BIT); + break; + default: + break; + } + return ESP_OK; +} + +static void wifi_init(void) +{ + tcpip_adapter_init(); + wifi_event_group = xEventGroupCreate(); + ESP_ERROR_CHECK(esp_event_loop_init(wifi_event_handler, NULL)); + wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); + ESP_ERROR_CHECK(esp_wifi_init(&cfg)); + ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM)); + wifi_config_t wifi_config = { + .sta = { + .ssid = CONFIG_WIFI_SSID, + .password = CONFIG_WIFI_PASSWORD, + }, + }; + ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); + ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config)); + ESP_LOGI(TAG, "start the WIFI SSID:[%s]", CONFIG_WIFI_SSID); + ESP_ERROR_CHECK(esp_wifi_start()); + ESP_LOGI(TAG, "Waiting for wifi"); + xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT, false, true, portMAX_DELAY); +} + +#if CONFIG_BROKER_CERTIFICATE_OVERRIDDEN == 1 +static const uint8_t iot_eclipse_org_pem_start[] = "-----BEGIN CERTIFICATE-----\n" CONFIG_BROKER_CERTIFICATE_OVERRIDE "\n-----END CERTIFICATE-----"; +#else +extern const uint8_t iot_eclipse_org_pem_start[] asm("_binary_iot_eclipse_org_pem_start"); +#endif +extern const uint8_t iot_eclipse_org_pem_end[] asm("_binary_iot_eclipse_org_pem_end"); + +static esp_err_t mqtt_event_handler(esp_mqtt_event_handle_t event) +{ + esp_mqtt_client_handle_t client = event->client; + static int msg_id = 0; + static int actual_len = 0; + // your_context_t *context = event->context; + switch (event->event_id) { + case MQTT_EVENT_CONNECTED: + ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED"); + xEventGroupSetBits(mqtt_event_group, CONNECTED_BIT); + msg_id = esp_mqtt_client_subscribe(client, CONFIG_SUBSCIBE_TOPIC, qos_test); + ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id); + + break; + case MQTT_EVENT_DISCONNECTED: + ESP_LOGI(TAG, "MQTT_EVENT_DISCONNECTED"); + break; + + case MQTT_EVENT_SUBSCRIBED: + ESP_LOGI(TAG, "MQTT_EVENT_SUBSCRIBED, msg_id=%d", event->msg_id); + break; + case MQTT_EVENT_UNSUBSCRIBED: + ESP_LOGI(TAG, "MQTT_EVENT_UNSUBSCRIBED, msg_id=%d", event->msg_id); + break; + case MQTT_EVENT_PUBLISHED: + ESP_LOGI(TAG, "MQTT_EVENT_PUBLISHED, msg_id=%d", event->msg_id); + break; + case MQTT_EVENT_DATA: + ESP_LOGI(TAG, "MQTT_EVENT_DATA"); + printf("TOPIC=%.*s\r\n", event->topic_len, event->topic); + printf("DATA=%.*s\r\n", event->data_len, event->data); + printf("ID=%d, total_len=%d, data_len=%d, current_data_offset=%d\n", event->msg_id, event->total_data_len, event->data_len, event->current_data_offset); + if (event->topic) { + actual_len = event->data_len; + msg_id = event->msg_id; + } else { + actual_len += event->data_len; + // check consisency with msg_id across multiple data events for single msg + if (msg_id != event->msg_id) { + ESP_LOGI(TAG, "Wrong msg_id in chunked message %d != %d", msg_id, event->msg_id); + abort(); + } + } + memcpy(actual_data + event->current_data_offset, event->data, event->data_len); + if (actual_len == event->total_data_len) { + if (0 == memcmp(actual_data, expected_data, expected_size)) { + printf("OK!"); + memset(actual_data, 0, expected_size); + actual_published ++; + if (actual_published == expected_published) { + printf("Correct pattern received exactly x times\n"); + ESP_LOGI(TAG, "Test finished correctly!"); + } + } else { + printf("FAILED!"); + abort(); + } + } + break; + case MQTT_EVENT_ERROR: + ESP_LOGI(TAG, "MQTT_EVENT_ERROR"); + break; + default: + ESP_LOGI(TAG, "Other event id:%d", event->event_id); + break; + } + return ESP_OK; +} + + +static void mqtt_app_start(void) +{ + mqtt_event_group = xEventGroupCreate(); + const esp_mqtt_client_config_t mqtt_cfg = { + .event_handle = mqtt_event_handler, + .cert_pem = (const char *)iot_eclipse_org_pem_start, + }; + + ESP_LOGI(TAG, "[APP] Free memory: %d bytes", esp_get_free_heap_size()); + mqtt_client = esp_mqtt_client_init(&mqtt_cfg); +} + +static void get_string(char *line, size_t size) +{ + + int count = 0; + while (count < size) { + int c = fgetc(stdin); + if (c == '\n') { + line[count] = '\0'; + break; + } else if (c > 0 && c < 127) { + line[count] = c; + ++count; + } + vTaskDelay(10 / portTICK_PERIOD_MS); + } +} + +void app_main() +{ + char line[256]; + char pattern[32]; + char transport[32]; + int repeat = 0; + + ESP_LOGI(TAG, "[APP] Free memory: %d bytes", esp_get_free_heap_size()); + ESP_LOGI(TAG, "[APP] IDF version: %s", esp_get_idf_version()); + + esp_log_level_set("*", ESP_LOG_INFO); + esp_log_level_set("MQTT_CLIENT", ESP_LOG_VERBOSE); + esp_log_level_set("TRANSPORT_TCP", ESP_LOG_VERBOSE); + esp_log_level_set("TRANSPORT_SSL", ESP_LOG_VERBOSE); + esp_log_level_set("TRANSPORT", ESP_LOG_VERBOSE); + esp_log_level_set("OUTBOX", ESP_LOG_VERBOSE); + + nvs_flash_init(); + wifi_init(); + mqtt_app_start(); + + while (1) { + get_string(line, sizeof(line)); + sscanf(line, "%s %s %d %d %d", transport, pattern, &repeat, &expected_published, &qos_test); + ESP_LOGI(TAG, "PATTERN:%s REPEATED:%d PUBLISHED:%d\n", pattern, repeat, expected_published); + int pattern_size = strlen(pattern); + free(expected_data); + free(actual_data); + actual_published = 0; + expected_size = pattern_size * repeat; + expected_data = malloc(expected_size); + actual_data = malloc(expected_size); + for (int i = 0; i < repeat; i++) { + memcpy(expected_data + i * pattern_size, pattern, pattern_size); + } + printf("EXPECTED STRING %.*s, SIZE:%d\n", expected_size, expected_data, expected_size); + esp_mqtt_client_stop(mqtt_client); + + if (0 == strcmp(transport, "tcp")) { + ESP_LOGI(TAG, "[TCP transport] Startup.."); + esp_mqtt_client_set_uri(mqtt_client, CONFIG_BROKER_TCP_URI); + } else if (0 == strcmp(transport, "ssl")) { + ESP_LOGI(TAG, "[SSL transport] Startup.."); + esp_mqtt_client_set_uri(mqtt_client, CONFIG_BROKER_SSL_URI); + } else if (0 == strcmp(transport, "ws")) { + ESP_LOGI(TAG, "[WS transport] Startup.."); + esp_mqtt_client_set_uri(mqtt_client, CONFIG_BROKER_WS_URI); + } else if (0 == strcmp(transport, "wss")) { + ESP_LOGI(TAG, "[WSS transport] Startup.."); + esp_mqtt_client_set_uri(mqtt_client, CONFIG_BROKER_WSS_URI); + } else { + ESP_LOGE(TAG, "Unexpected transport"); + abort(); + } + xEventGroupClearBits(mqtt_event_group, CONNECTED_BIT); + esp_mqtt_client_start(mqtt_client); + ESP_LOGI(TAG, "Note free memory: %d bytes", esp_get_free_heap_size()); + xEventGroupWaitBits(mqtt_event_group, CONNECTED_BIT, false, true, portMAX_DELAY); + + for (int i = 0; i < expected_published; i++) { + int msg_id = esp_mqtt_client_publish(mqtt_client, CONFIG_PUBLISH_TOPIC, expected_data, expected_size, qos_test, 0); + ESP_LOGI(TAG, "[%d] Publishing...", msg_id); + } + } +} diff --git a/examples/protocols/mqtt/publish_test/sdkconfig.ci b/examples/protocols/mqtt/publish_test/sdkconfig.ci new file mode 100644 index 0000000..00db776 --- /dev/null +++ b/examples/protocols/mqtt/publish_test/sdkconfig.ci @@ -0,0 +1,5 @@ +CONFIG_BROKER_SSL_URI="mqtts://${EXAMPLE_MQTT_BROKER_SSL}" +CONFIG_BROKER_TCP_URI="mqtt://${EXAMPLE_MQTT_BROKER_TCP}" +CONFIG_BROKER_WS_URI="ws://${EXAMPLE_MQTT_BROKER_WS}/ws" +CONFIG_BROKER_WSS_URI="wss://${EXAMPLE_MQTT_BROKER_WSS}/ws" +CONFIG_BROKER_CERTIFICATE_OVERRIDE="${EXAMPLE_MQTT_BROKER_CERTIFICATE}" From ad3c88c867782d5cd36a063dc1a9f72395fd2e99 Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Wed, 21 Nov 2018 00:42:03 +0800 Subject: [PATCH 016/231] examples/protocols/mqtt: use common network component --- .../mqtt/publish_test/CMakeLists.txt | 4 + examples/protocols/mqtt/publish_test/Makefile | 2 + .../protocols/mqtt/publish_test/README.md | 12 +-- .../mqtt/publish_test/main/Kconfig.projbuild | 12 --- .../mqtt/publish_test/main/publish_test.c | 58 ++++----------- examples/protocols/mqtt/ssl/CMakeLists.txt | 5 +- examples/protocols/mqtt/ssl/Makefile | 2 + examples/protocols/mqtt/ssl/README.md | 10 +-- .../protocols/mqtt/ssl/main/Kconfig.projbuild | 12 --- examples/protocols/mqtt/ssl/main/app_main.c | 73 ++++++------------- .../mqtt/ssl_mutual_auth/CMakeLists.txt | 5 +- .../protocols/mqtt/ssl_mutual_auth/Makefile | 2 + .../protocols/mqtt/ssl_mutual_auth/README.md | 10 +-- .../ssl_mutual_auth/main/Kconfig.projbuild | 15 ---- .../mqtt/ssl_mutual_auth/main/app_main.c | 73 ++++++------------- examples/protocols/mqtt/tcp/CMakeLists.txt | 5 +- examples/protocols/mqtt/tcp/Makefile | 2 + examples/protocols/mqtt/tcp/README.md | 10 +-- .../protocols/mqtt/tcp/main/Kconfig.projbuild | 12 --- examples/protocols/mqtt/tcp/main/app_main.c | 71 ++++++------------ examples/protocols/mqtt/ws/CMakeLists.txt | 7 +- examples/protocols/mqtt/ws/Makefile | 2 + examples/protocols/mqtt/ws/README.md | 10 +-- .../protocols/mqtt/ws/main/Kconfig.projbuild | 12 --- examples/protocols/mqtt/ws/main/app_main.c | 69 ++++++------------ examples/protocols/mqtt/wss/CMakeLists.txt | 5 +- examples/protocols/mqtt/wss/Makefile | 2 + examples/protocols/mqtt/wss/README.md | 10 +-- .../protocols/mqtt/wss/main/Kconfig.projbuild | 12 --- examples/protocols/mqtt/wss/main/app_main.c | 71 ++++++------------ 30 files changed, 175 insertions(+), 420 deletions(-) delete mode 100644 examples/protocols/mqtt/ssl_mutual_auth/main/Kconfig.projbuild diff --git a/examples/protocols/mqtt/publish_test/CMakeLists.txt b/examples/protocols/mqtt/publish_test/CMakeLists.txt index 75f1619..2203d06 100644 --- a/examples/protocols/mqtt/publish_test/CMakeLists.txt +++ b/examples/protocols/mqtt/publish_test/CMakeLists.txt @@ -2,6 +2,10 @@ # in this exact order for cmake to work correctly cmake_minimum_required(VERSION 3.5) +# (Not part of the boilerplate) +# This example uses an extra component for common functions such as Wi-Fi and Ethernet connection. +set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common) + include($ENV{IDF_PATH}/tools/cmake/project.cmake) project(mqtt_publish) diff --git a/examples/protocols/mqtt/publish_test/Makefile b/examples/protocols/mqtt/publish_test/Makefile index d1341ae..7d552c3 100644 --- a/examples/protocols/mqtt/publish_test/Makefile +++ b/examples/protocols/mqtt/publish_test/Makefile @@ -4,4 +4,6 @@ # PROJECT_NAME := mqtt_publish +EXTRA_COMPONENT_DIRS = $(IDF_PATH)/examples/common_components/protocol_examples_common + include $(IDF_PATH)/make/project.mk diff --git a/examples/protocols/mqtt/publish_test/README.md b/examples/protocols/mqtt/publish_test/README.md index bc4e02c..bdc2bc8 100644 --- a/examples/protocols/mqtt/publish_test/README.md +++ b/examples/protocols/mqtt/publish_test/README.md @@ -22,16 +22,10 @@ This example can be executed on any ESP32 board, the only required interface is ### Configure the project -``` -make menuconfig -``` - -* Set serial port under Serial Flasher Options. - -* Set ssid and password for the board to connect to AP. - +* Run `make menuconfig` (or `idf.py menuconfig` if using CMake build system) +* Configure Wi-Fi or Ethernet under "Example Connection Configuration" menu. See "Establishing Wi-Fi or Ethernet Connection" section in [examples/protocols/README.md](../../README.md) for more details. +* When using Make build system, set `Default serial port` under `Serial flasher config`. * Set brokers for all 4 transports (TCP, SSL, WS, WSS), also set certificate if needed - * Set topics for publishing from and to ESP32 ### Build and Flash diff --git a/examples/protocols/mqtt/publish_test/main/Kconfig.projbuild b/examples/protocols/mqtt/publish_test/main/Kconfig.projbuild index 3d534a4..66d3cb0 100644 --- a/examples/protocols/mqtt/publish_test/main/Kconfig.projbuild +++ b/examples/protocols/mqtt/publish_test/main/Kconfig.projbuild @@ -1,17 +1,5 @@ menu "Example Configuration" - config WIFI_SSID - string "WiFi SSID" - default "myssid" - help - SSID (network name) for the example to connect to. - - config WIFI_PASSWORD - string "WiFi Password" - default "mypassword" - help - WiFi password (WPA or WPA2) for the example to use. - config BROKER_SSL_URI string "Broker SSL URL" default "mqtts://iot.eclipse.org:8883" diff --git a/examples/protocols/mqtt/publish_test/main/publish_test.c b/examples/protocols/mqtt/publish_test/main/publish_test.c index 6507318..99ef19d 100644 --- a/examples/protocols/mqtt/publish_test/main/publish_test.c +++ b/examples/protocols/mqtt/publish_test/main/publish_test.c @@ -13,7 +13,9 @@ #include "esp_wifi.h" #include "esp_system.h" #include "nvs_flash.h" -#include "esp_event_loop.h" +#include "esp_event.h" +#include "tcpip_adapter.h" +#include "protocol_examples_common.h" #include "freertos/FreeRTOS.h" #include "freertos/task.h" @@ -30,7 +32,6 @@ static const char *TAG = "PUBLISH_TEST"; -static EventGroupHandle_t wifi_event_group; static EventGroupHandle_t mqtt_event_group; const static int CONNECTED_BIT = BIT0; @@ -43,47 +44,6 @@ static size_t expected_published = 0; static size_t actual_published = 0; static int qos_test = 0; -static esp_err_t wifi_event_handler(void *ctx, system_event_t *event) -{ - switch (event->event_id) { - case SYSTEM_EVENT_STA_START: - esp_wifi_connect(); - break; - case SYSTEM_EVENT_STA_GOT_IP: - xEventGroupSetBits(wifi_event_group, CONNECTED_BIT); - - break; - case SYSTEM_EVENT_STA_DISCONNECTED: - esp_wifi_connect(); - xEventGroupClearBits(wifi_event_group, CONNECTED_BIT); - break; - default: - break; - } - return ESP_OK; -} - -static void wifi_init(void) -{ - tcpip_adapter_init(); - wifi_event_group = xEventGroupCreate(); - ESP_ERROR_CHECK(esp_event_loop_init(wifi_event_handler, NULL)); - wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); - ESP_ERROR_CHECK(esp_wifi_init(&cfg)); - ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM)); - wifi_config_t wifi_config = { - .sta = { - .ssid = CONFIG_WIFI_SSID, - .password = CONFIG_WIFI_PASSWORD, - }, - }; - ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); - ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config)); - ESP_LOGI(TAG, "start the WIFI SSID:[%s]", CONFIG_WIFI_SSID); - ESP_ERROR_CHECK(esp_wifi_start()); - ESP_LOGI(TAG, "Waiting for wifi"); - xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT, false, true, portMAX_DELAY); -} #if CONFIG_BROKER_CERTIFICATE_OVERRIDDEN == 1 static const uint8_t iot_eclipse_org_pem_start[] = "-----BEGIN CERTIFICATE-----\n" CONFIG_BROKER_CERTIFICATE_OVERRIDE "\n-----END CERTIFICATE-----"; @@ -208,8 +168,16 @@ void app_main() esp_log_level_set("TRANSPORT", ESP_LOG_VERBOSE); esp_log_level_set("OUTBOX", ESP_LOG_VERBOSE); - nvs_flash_init(); - wifi_init(); + ESP_ERROR_CHECK(nvs_flash_init()); + tcpip_adapter_init(); + ESP_ERROR_CHECK(esp_event_loop_create_default()); + + /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig. + * Read "Establishing Wi-Fi or Ethernet Connection" section in + * examples/protocols/README.md for more information about this function. + */ + ESP_ERROR_CHECK(example_connect()); + mqtt_app_start(); while (1) { diff --git a/examples/protocols/mqtt/ssl/CMakeLists.txt b/examples/protocols/mqtt/ssl/CMakeLists.txt index 13bdd8c..a7b2c44 100644 --- a/examples/protocols/mqtt/ssl/CMakeLists.txt +++ b/examples/protocols/mqtt/ssl/CMakeLists.txt @@ -2,8 +2,11 @@ # in this exact order for cmake to work correctly cmake_minimum_required(VERSION 3.5) -include($ENV{IDF_PATH}/tools/cmake/project.cmake) +# (Not part of the boilerplate) +# This example uses an extra component for common functions such as Wi-Fi and Ethernet connection. +set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common) +include($ENV{IDF_PATH}/tools/cmake/project.cmake) project(mqtt_ssl) target_add_binary_data(mqtt_ssl.elf "main/iot_eclipse_org.pem" TEXT) diff --git a/examples/protocols/mqtt/ssl/Makefile b/examples/protocols/mqtt/ssl/Makefile index bae0d73..efe7f7d 100644 --- a/examples/protocols/mqtt/ssl/Makefile +++ b/examples/protocols/mqtt/ssl/Makefile @@ -4,4 +4,6 @@ # PROJECT_NAME := mqtt_ssl +EXTRA_COMPONENT_DIRS = $(IDF_PATH)/examples/common_components/protocol_examples_common + include $(IDF_PATH)/make/project.mk diff --git a/examples/protocols/mqtt/ssl/README.md b/examples/protocols/mqtt/ssl/README.md index 3d369bd..929257d 100644 --- a/examples/protocols/mqtt/ssl/README.md +++ b/examples/protocols/mqtt/ssl/README.md @@ -14,13 +14,9 @@ This example can be executed on any ESP32 board, the only required interface is ### Configure the project -``` -make menuconfig -``` - -* Set serial port under Serial Flasher Options. - -* Set ssid and password for the board to connect to AP. +* Run `make menuconfig` (or `idf.py menuconfig` if using CMake build system) +* Configure Wi-Fi or Ethernet under "Example Connection Configuration" menu. See "Establishing Wi-Fi or Ethernet Connection" section in [examples/protocols/README.md](../../README.md) for more details. +* When using Make build system, set `Default serial port` under `Serial flasher config`. Note how to create a PEM certificate for iot.eclipse.org: ``` diff --git a/examples/protocols/mqtt/ssl/main/Kconfig.projbuild b/examples/protocols/mqtt/ssl/main/Kconfig.projbuild index 7bd1fcb..e318733 100644 --- a/examples/protocols/mqtt/ssl/main/Kconfig.projbuild +++ b/examples/protocols/mqtt/ssl/main/Kconfig.projbuild @@ -1,17 +1,5 @@ menu "Example Configuration" - config WIFI_SSID - string "WiFi SSID" - default "myssid" - help - SSID (network name) for the example to connect to. - - config WIFI_PASSWORD - string "WiFi Password" - default "mypassword" - help - WiFi password (WPA or WPA2) for the example to use. - config BROKER_URI string "Broker URL" default "mqtts://iot.eclipse.org:8883" diff --git a/examples/protocols/mqtt/ssl/main/app_main.c b/examples/protocols/mqtt/ssl/main/app_main.c index 86697a0..88e1a0c 100644 --- a/examples/protocols/mqtt/ssl/main/app_main.c +++ b/examples/protocols/mqtt/ssl/main/app_main.c @@ -1,3 +1,12 @@ +/* MQTT over SSL Example + + This example 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 #include @@ -5,13 +14,14 @@ #include "esp_wifi.h" #include "esp_system.h" #include "nvs_flash.h" -#include "esp_event_loop.h" +#include "esp_event.h" +#include "tcpip_adapter.h" +#include "protocol_examples_common.h" #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "freertos/semphr.h" #include "freertos/queue.h" -#include "freertos/event_groups.h" #include "lwip/sockets.h" #include "lwip/dns.h" @@ -22,52 +32,6 @@ static const char *TAG = "MQTTS_EXAMPLE"; -static EventGroupHandle_t wifi_event_group; -const static int CONNECTED_BIT = BIT0; - - - -static esp_err_t wifi_event_handler(void *ctx, system_event_t *event) -{ - switch (event->event_id) { - case SYSTEM_EVENT_STA_START: - esp_wifi_connect(); - break; - case SYSTEM_EVENT_STA_GOT_IP: - xEventGroupSetBits(wifi_event_group, CONNECTED_BIT); - - break; - case SYSTEM_EVENT_STA_DISCONNECTED: - esp_wifi_connect(); - xEventGroupClearBits(wifi_event_group, CONNECTED_BIT); - break; - default: - break; - } - return ESP_OK; -} - -static void wifi_init(void) -{ - tcpip_adapter_init(); - wifi_event_group = xEventGroupCreate(); - ESP_ERROR_CHECK(esp_event_loop_init(wifi_event_handler, NULL)); - wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); - ESP_ERROR_CHECK(esp_wifi_init(&cfg)); - ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM)); - wifi_config_t wifi_config = { - .sta = { - .ssid = CONFIG_WIFI_SSID, - .password = CONFIG_WIFI_PASSWORD, - }, - }; - ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); - ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config)); - ESP_LOGI(TAG, "start the WIFI SSID:[%s]", CONFIG_WIFI_SSID); - ESP_ERROR_CHECK(esp_wifi_start()); - ESP_LOGI(TAG, "Waiting for wifi"); - xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT, false, true, portMAX_DELAY); -} #if CONFIG_BROKER_CERTIFICATE_OVERRIDDEN == 1 static const uint8_t iot_eclipse_org_pem_start[] = "-----BEGIN CERTIFICATE-----\n" CONFIG_BROKER_CERTIFICATE_OVERRIDE "\n-----END CERTIFICATE-----"; @@ -150,8 +114,15 @@ void app_main() esp_log_level_set("TRANSPORT", ESP_LOG_VERBOSE); esp_log_level_set("OUTBOX", ESP_LOG_VERBOSE); - nvs_flash_init(); - wifi_init(); - mqtt_app_start(); + ESP_ERROR_CHECK(nvs_flash_init()); + tcpip_adapter_init(); + ESP_ERROR_CHECK(esp_event_loop_create_default()); + /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig. + * Read "Establishing Wi-Fi or Ethernet Connection" section in + * examples/protocols/README.md for more information about this function. + */ + ESP_ERROR_CHECK(example_connect()); + + mqtt_app_start(); } diff --git a/examples/protocols/mqtt/ssl_mutual_auth/CMakeLists.txt b/examples/protocols/mqtt/ssl_mutual_auth/CMakeLists.txt index 84bf375..472a3ca 100644 --- a/examples/protocols/mqtt/ssl_mutual_auth/CMakeLists.txt +++ b/examples/protocols/mqtt/ssl_mutual_auth/CMakeLists.txt @@ -2,8 +2,11 @@ # in this exact order for cmake to work correctly cmake_minimum_required(VERSION 3.5) -include($ENV{IDF_PATH}/tools/cmake/project.cmake) +# (Not part of the boilerplate) +# This example uses an extra component for common functions such as Wi-Fi and Ethernet connection. +set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common) +include($ENV{IDF_PATH}/tools/cmake/project.cmake) project(mqtt_ssl_mutual_auth) target_add_binary_data(${CMAKE_PROJECT_NAME}.elf "main/client.crt" TEXT) diff --git a/examples/protocols/mqtt/ssl_mutual_auth/Makefile b/examples/protocols/mqtt/ssl_mutual_auth/Makefile index cfc04f8..1394f99 100644 --- a/examples/protocols/mqtt/ssl_mutual_auth/Makefile +++ b/examples/protocols/mqtt/ssl_mutual_auth/Makefile @@ -4,4 +4,6 @@ # PROJECT_NAME := mqtt_ssl_mutual_auth +EXTRA_COMPONENT_DIRS = $(IDF_PATH)/examples/common_components/protocol_examples_common + include $(IDF_PATH)/make/project.mk diff --git a/examples/protocols/mqtt/ssl_mutual_auth/README.md b/examples/protocols/mqtt/ssl_mutual_auth/README.md index 7ffe537..763e267 100644 --- a/examples/protocols/mqtt/ssl_mutual_auth/README.md +++ b/examples/protocols/mqtt/ssl_mutual_auth/README.md @@ -14,13 +14,9 @@ This example can be executed on any ESP32 board, the only required interface is ### Configure the project -``` -make menuconfig -``` - -* Set serial port under Serial Flasher Options. - -* Set ssid and password for the board to connect to AP. +* Run `make menuconfig` (or `idf.py menuconfig` if using CMake build system) +* Configure Wi-Fi or Ethernet under "Example Connection Configuration" menu. See "Establishing Wi-Fi or Ethernet Connection" section in [examples/protocols/README.md](../../README.md) for more details. +* When using Make build system, set `Default serial port` under `Serial flasher config`. * Generate your client keys and certificate diff --git a/examples/protocols/mqtt/ssl_mutual_auth/main/Kconfig.projbuild b/examples/protocols/mqtt/ssl_mutual_auth/main/Kconfig.projbuild deleted file mode 100644 index ed4e691..0000000 --- a/examples/protocols/mqtt/ssl_mutual_auth/main/Kconfig.projbuild +++ /dev/null @@ -1,15 +0,0 @@ -menu "Example Configuration" - - config WIFI_SSID - string "WiFi SSID" - default "myssid" - help - SSID (network name) for the example to connect to. - - config WIFI_PASSWORD - string "WiFi Password" - default "mypassword" - help - WiFi password (WPA or WPA2) for the example to use. - -endmenu diff --git a/examples/protocols/mqtt/ssl_mutual_auth/main/app_main.c b/examples/protocols/mqtt/ssl_mutual_auth/main/app_main.c index 62b9700..21c76ff 100644 --- a/examples/protocols/mqtt/ssl_mutual_auth/main/app_main.c +++ b/examples/protocols/mqtt/ssl_mutual_auth/main/app_main.c @@ -1,3 +1,11 @@ +/* MQTT Mutual Authentication Example + + This example 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 #include @@ -5,13 +13,14 @@ #include "esp_wifi.h" #include "esp_system.h" #include "nvs_flash.h" -#include "esp_event_loop.h" +#include "esp_event.h" +#include "tcpip_adapter.h" +#include "protocol_examples_common.h" #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "freertos/semphr.h" #include "freertos/queue.h" -#include "freertos/event_groups.h" #include "lwip/sockets.h" #include "lwip/dns.h" @@ -22,53 +31,6 @@ static const char *TAG = "MQTTS_EXAMPLE"; -static EventGroupHandle_t wifi_event_group; -const static int CONNECTED_BIT = BIT0; - - - -static esp_err_t wifi_event_handler(void *ctx, system_event_t *event) -{ - switch (event->event_id) { - case SYSTEM_EVENT_STA_START: - esp_wifi_connect(); - break; - case SYSTEM_EVENT_STA_GOT_IP: - xEventGroupSetBits(wifi_event_group, CONNECTED_BIT); - - break; - case SYSTEM_EVENT_STA_DISCONNECTED: - esp_wifi_connect(); - xEventGroupClearBits(wifi_event_group, CONNECTED_BIT); - break; - default: - break; - } - return ESP_OK; -} - -static void wifi_init(void) -{ - tcpip_adapter_init(); - wifi_event_group = xEventGroupCreate(); - ESP_ERROR_CHECK(esp_event_loop_init(wifi_event_handler, NULL)); - wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); - ESP_ERROR_CHECK(esp_wifi_init(&cfg)); - ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM)); - wifi_config_t wifi_config = { - .sta = { - .ssid = CONFIG_WIFI_SSID, - .password = CONFIG_WIFI_PASSWORD, - }, - }; - ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); - ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config)); - ESP_LOGI(TAG, "start the WIFI SSID:[%s]", CONFIG_WIFI_SSID); - ESP_ERROR_CHECK(esp_wifi_start()); - ESP_LOGI(TAG, "Waiting for wifi"); - xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT, false, true, portMAX_DELAY); -} - extern const uint8_t client_cert_pem_start[] asm("_binary_client_crt_start"); extern const uint8_t client_cert_pem_end[] asm("_binary_client_crt_end"); extern const uint8_t client_key_pem_start[] asm("_binary_client_key_start"); @@ -148,8 +110,15 @@ void app_main() esp_log_level_set("TRANSPORT", ESP_LOG_VERBOSE); esp_log_level_set("OUTBOX", ESP_LOG_VERBOSE); - nvs_flash_init(); - wifi_init(); - mqtt_app_start(); + ESP_ERROR_CHECK(nvs_flash_init()); + tcpip_adapter_init(); + ESP_ERROR_CHECK(esp_event_loop_create_default()); + /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig. + * Read "Establishing Wi-Fi or Ethernet Connection" section in + * examples/protocols/README.md for more information about this function. + */ + ESP_ERROR_CHECK(example_connect()); + + mqtt_app_start(); } diff --git a/examples/protocols/mqtt/tcp/CMakeLists.txt b/examples/protocols/mqtt/tcp/CMakeLists.txt index 678d787..5a70e7a 100644 --- a/examples/protocols/mqtt/tcp/CMakeLists.txt +++ b/examples/protocols/mqtt/tcp/CMakeLists.txt @@ -2,6 +2,9 @@ # in this exact order for cmake to work correctly cmake_minimum_required(VERSION 3.5) -include($ENV{IDF_PATH}/tools/cmake/project.cmake) +# (Not part of the boilerplate) +# This example uses an extra component for common functions such as Wi-Fi and Ethernet connection. +set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common) +include($ENV{IDF_PATH}/tools/cmake/project.cmake) project(mqtt_tcp) \ No newline at end of file diff --git a/examples/protocols/mqtt/tcp/Makefile b/examples/protocols/mqtt/tcp/Makefile index cd53fdb..5302765 100644 --- a/examples/protocols/mqtt/tcp/Makefile +++ b/examples/protocols/mqtt/tcp/Makefile @@ -4,4 +4,6 @@ # PROJECT_NAME := mqtt_tcp +EXTRA_COMPONENT_DIRS = $(IDF_PATH)/examples/common_components/protocol_examples_common + include $(IDF_PATH)/make/project.mk diff --git a/examples/protocols/mqtt/tcp/README.md b/examples/protocols/mqtt/tcp/README.md index 2fda240..aea6630 100644 --- a/examples/protocols/mqtt/tcp/README.md +++ b/examples/protocols/mqtt/tcp/README.md @@ -14,13 +14,9 @@ This example can be executed on any ESP32 board, the only required interface is ### Configure the project -``` -make menuconfig -``` - -* Set serial port under Serial Flasher Options. - -* Set ssid and password for the board to connect to AP. +* Run `make menuconfig` (or `idf.py menuconfig` if using CMake build system) +* Configure Wi-Fi or Ethernet under "Example Connection Configuration" menu. See "Establishing Wi-Fi or Ethernet Connection" section in [examples/protocols/README.md](../../README.md) for more details. +* When using Make build system, set `Default serial port` under `Serial flasher config`. ### Build and Flash diff --git a/examples/protocols/mqtt/tcp/main/Kconfig.projbuild b/examples/protocols/mqtt/tcp/main/Kconfig.projbuild index f5c519b..fbd4810 100644 --- a/examples/protocols/mqtt/tcp/main/Kconfig.projbuild +++ b/examples/protocols/mqtt/tcp/main/Kconfig.projbuild @@ -1,17 +1,5 @@ menu "Example Configuration" - config WIFI_SSID - string "WiFi SSID" - default "myssid" - help - SSID (network name) for the example to connect to. - - config WIFI_PASSWORD - string "WiFi Password" - default "mypassword" - help - WiFi password (WPA or WPA2) for the example to use. - config BROKER_URL string "Broker URL" default "mqtt://iot.eclipse.org" diff --git a/examples/protocols/mqtt/tcp/main/app_main.c b/examples/protocols/mqtt/tcp/main/app_main.c index ce1640a..1863a01 100644 --- a/examples/protocols/mqtt/tcp/main/app_main.c +++ b/examples/protocols/mqtt/tcp/main/app_main.c @@ -1,3 +1,12 @@ +/* MQTT (over TCP) Example + + This example 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 #include @@ -5,13 +14,14 @@ #include "esp_wifi.h" #include "esp_system.h" #include "nvs_flash.h" -#include "esp_event_loop.h" +#include "esp_event.h" +#include "tcpip_adapter.h" +#include "protocol_examples_common.h" #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "freertos/semphr.h" #include "freertos/queue.h" -#include "freertos/event_groups.h" #include "lwip/sockets.h" #include "lwip/dns.h" @@ -22,9 +32,6 @@ static const char *TAG = "MQTT_EXAMPLE"; -static EventGroupHandle_t wifi_event_group; -const static int CONNECTED_BIT = BIT0; - static esp_err_t mqtt_event_handler(esp_mqtt_event_handle_t event) { @@ -76,48 +83,6 @@ static esp_err_t mqtt_event_handler(esp_mqtt_event_handle_t event) return ESP_OK; } -static esp_err_t wifi_event_handler(void *ctx, system_event_t *event) -{ - switch (event->event_id) { - case SYSTEM_EVENT_STA_START: - esp_wifi_connect(); - break; - case SYSTEM_EVENT_STA_GOT_IP: - xEventGroupSetBits(wifi_event_group, CONNECTED_BIT); - - break; - case SYSTEM_EVENT_STA_DISCONNECTED: - esp_wifi_connect(); - xEventGroupClearBits(wifi_event_group, CONNECTED_BIT); - break; - default: - break; - } - return ESP_OK; -} - -static void wifi_init(void) -{ - tcpip_adapter_init(); - wifi_event_group = xEventGroupCreate(); - ESP_ERROR_CHECK(esp_event_loop_init(wifi_event_handler, NULL)); - wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); - ESP_ERROR_CHECK(esp_wifi_init(&cfg)); - ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM)); - wifi_config_t wifi_config = { - .sta = { - .ssid = CONFIG_WIFI_SSID, - .password = CONFIG_WIFI_PASSWORD, - }, - }; - ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); - ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config)); - ESP_LOGI(TAG, "start the WIFI SSID:[%s]", CONFIG_WIFI_SSID); - ESP_ERROR_CHECK(esp_wifi_start()); - ESP_LOGI(TAG, "Waiting for wifi"); - xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT, false, true, portMAX_DELAY); -} - static void mqtt_app_start(void) { esp_mqtt_client_config_t mqtt_cfg = { @@ -168,7 +133,15 @@ void app_main() esp_log_level_set("TRANSPORT", ESP_LOG_VERBOSE); esp_log_level_set("OUTBOX", ESP_LOG_VERBOSE); - nvs_flash_init(); - wifi_init(); + ESP_ERROR_CHECK(nvs_flash_init()); + tcpip_adapter_init(); + ESP_ERROR_CHECK(esp_event_loop_create_default()); + + /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig. + * Read "Establishing Wi-Fi or Ethernet Connection" section in + * examples/protocols/README.md for more information about this function. + */ + ESP_ERROR_CHECK(example_connect()); + mqtt_app_start(); } diff --git a/examples/protocols/mqtt/ws/CMakeLists.txt b/examples/protocols/mqtt/ws/CMakeLists.txt index 58a2135..f049006 100644 --- a/examples/protocols/mqtt/ws/CMakeLists.txt +++ b/examples/protocols/mqtt/ws/CMakeLists.txt @@ -2,6 +2,9 @@ # in this exact order for cmake to work correctly cmake_minimum_required(VERSION 3.5) -include($ENV{IDF_PATH}/tools/cmake/project.cmake) +# (Not part of the boilerplate) +# This example uses an extra component for common functions such as Wi-Fi and Ethernet connection. +set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common) -project(mqtt_websocket) \ No newline at end of file +include($ENV{IDF_PATH}/tools/cmake/project.cmake) +project(mqtt_websocket) diff --git a/examples/protocols/mqtt/ws/Makefile b/examples/protocols/mqtt/ws/Makefile index 668719b..2b97fa4 100644 --- a/examples/protocols/mqtt/ws/Makefile +++ b/examples/protocols/mqtt/ws/Makefile @@ -4,4 +4,6 @@ # PROJECT_NAME := mqtt_websocket +EXTRA_COMPONENT_DIRS = $(IDF_PATH)/examples/common_components/protocol_examples_common + include $(IDF_PATH)/make/project.mk diff --git a/examples/protocols/mqtt/ws/README.md b/examples/protocols/mqtt/ws/README.md index 619519d..efe2efc 100644 --- a/examples/protocols/mqtt/ws/README.md +++ b/examples/protocols/mqtt/ws/README.md @@ -14,13 +14,9 @@ This example can be executed on any ESP32 board, the only required interface is ### Configure the project -``` -make menuconfig -``` - -* Set serial port under Serial Flasher Options. - -* Set ssid and password for the board to connect to AP. +* Run `make menuconfig` (or `idf.py menuconfig` if using CMake build system) +* Configure Wi-Fi or Ethernet under "Example Connection Configuration" menu. See "Establishing Wi-Fi or Ethernet Connection" section in [examples/protocols/README.md](../../README.md) for more details. +* When using Make build system, set `Default serial port` under `Serial flasher config`. ### Build and Flash diff --git a/examples/protocols/mqtt/ws/main/Kconfig.projbuild b/examples/protocols/mqtt/ws/main/Kconfig.projbuild index 9e47709..46f3ad5 100644 --- a/examples/protocols/mqtt/ws/main/Kconfig.projbuild +++ b/examples/protocols/mqtt/ws/main/Kconfig.projbuild @@ -1,17 +1,5 @@ menu "Example Configuration" - config WIFI_SSID - string "WiFi SSID" - default "myssid" - help - SSID (network name) for the example to connect to. - - config WIFI_PASSWORD - string "WiFi Password" - default "mypassword" - help - WiFi password (WPA or WPA2) for the example to use. - config BROKER_URI string "Broker URL" default "ws://iot.eclipse.org:80/ws" diff --git a/examples/protocols/mqtt/ws/main/app_main.c b/examples/protocols/mqtt/ws/main/app_main.c index 024be48..0387e96 100644 --- a/examples/protocols/mqtt/ws/main/app_main.c +++ b/examples/protocols/mqtt/ws/main/app_main.c @@ -1,3 +1,11 @@ +/* MQTT over Websockets Example + + This example 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 #include @@ -5,13 +13,14 @@ #include "esp_wifi.h" #include "esp_system.h" #include "nvs_flash.h" -#include "esp_event_loop.h" +#include "esp_event.h" +#include "tcpip_adapter.h" +#include "protocol_examples_common.h" #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "freertos/semphr.h" #include "freertos/queue.h" -#include "freertos/event_groups.h" #include "lwip/sockets.h" #include "lwip/dns.h" @@ -22,9 +31,6 @@ static const char *TAG = "MQTTWS_EXAMPLE"; -static EventGroupHandle_t wifi_event_group; -const static int CONNECTED_BIT = BIT0; - static esp_err_t mqtt_event_handler(esp_mqtt_event_handle_t event) { @@ -73,47 +79,6 @@ static esp_err_t mqtt_event_handler(esp_mqtt_event_handle_t event) return ESP_OK; } -static esp_err_t wifi_event_handler(void *ctx, system_event_t *event) -{ - switch (event->event_id) { - case SYSTEM_EVENT_STA_START: - esp_wifi_connect(); - break; - case SYSTEM_EVENT_STA_GOT_IP: - xEventGroupSetBits(wifi_event_group, CONNECTED_BIT); - - break; - case SYSTEM_EVENT_STA_DISCONNECTED: - esp_wifi_connect(); - xEventGroupClearBits(wifi_event_group, CONNECTED_BIT); - break; - default: - break; - } - return ESP_OK; -} - -static void wifi_init(void) -{ - tcpip_adapter_init(); - wifi_event_group = xEventGroupCreate(); - ESP_ERROR_CHECK(esp_event_loop_init(wifi_event_handler, NULL)); - wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); - ESP_ERROR_CHECK(esp_wifi_init(&cfg)); - ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM)); - wifi_config_t wifi_config = { - .sta = { - .ssid = CONFIG_WIFI_SSID, - .password = CONFIG_WIFI_PASSWORD, - }, - }; - ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); - ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config)); - ESP_LOGI(TAG, "start the WIFI SSID:[%s]", CONFIG_WIFI_SSID); - ESP_ERROR_CHECK(esp_wifi_start()); - ESP_LOGI(TAG, "Waiting for wifi"); - xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT, false, true, portMAX_DELAY); -} static void mqtt_app_start(void) { @@ -141,7 +106,15 @@ void app_main() esp_log_level_set("TRANSPORT", ESP_LOG_VERBOSE); esp_log_level_set("OUTBOX", ESP_LOG_VERBOSE); - nvs_flash_init(); - wifi_init(); + ESP_ERROR_CHECK(nvs_flash_init()); + tcpip_adapter_init(); + ESP_ERROR_CHECK(esp_event_loop_create_default()); + + /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig. + * Read "Establishing Wi-Fi or Ethernet Connection" section in + * examples/protocols/README.md for more information about this function. + */ + ESP_ERROR_CHECK(example_connect()); + mqtt_app_start(); } diff --git a/examples/protocols/mqtt/wss/CMakeLists.txt b/examples/protocols/mqtt/wss/CMakeLists.txt index 7ba5e62..3e2d5fc 100644 --- a/examples/protocols/mqtt/wss/CMakeLists.txt +++ b/examples/protocols/mqtt/wss/CMakeLists.txt @@ -2,8 +2,11 @@ # in this exact order for cmake to work correctly cmake_minimum_required(VERSION 3.5) -include($ENV{IDF_PATH}/tools/cmake/project.cmake) +# (Not part of the boilerplate) +# This example uses an extra component for common functions such as Wi-Fi and Ethernet connection. +set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common) +include($ENV{IDF_PATH}/tools/cmake/project.cmake) project(mqtt_websocket_secure) target_add_binary_data(mqtt_websocket_secure.elf "main/iot_eclipse_org.pem" TEXT) diff --git a/examples/protocols/mqtt/wss/Makefile b/examples/protocols/mqtt/wss/Makefile index 27047d0..50ed13d 100644 --- a/examples/protocols/mqtt/wss/Makefile +++ b/examples/protocols/mqtt/wss/Makefile @@ -4,4 +4,6 @@ # PROJECT_NAME := mqtt_websocket_secure +EXTRA_COMPONENT_DIRS = $(IDF_PATH)/examples/common_components/protocol_examples_common + include $(IDF_PATH)/make/project.mk diff --git a/examples/protocols/mqtt/wss/README.md b/examples/protocols/mqtt/wss/README.md index 43d829c..9433986 100644 --- a/examples/protocols/mqtt/wss/README.md +++ b/examples/protocols/mqtt/wss/README.md @@ -13,13 +13,9 @@ This example can be executed on any ESP32 board, the only required interface is ### Configure the project -``` -make menuconfig -``` - -* Set serial port under Serial Flasher Options. - -* Set ssid and password for the board to connect to AP. +* Run `make menuconfig` (or `idf.py menuconfig` if using CMake build system) +* Configure Wi-Fi or Ethernet under "Example Connection Configuration" menu. See "Establishing Wi-Fi or Ethernet Connection" section in [examples/protocols/README.md](../../README.md) for more details. +* When using Make build system, set `Default serial port` under `Serial flasher config`. Note how to create a PEM certificate for iot.eclipse.org: diff --git a/examples/protocols/mqtt/wss/main/Kconfig.projbuild b/examples/protocols/mqtt/wss/main/Kconfig.projbuild index a161740..d3f7bf5 100644 --- a/examples/protocols/mqtt/wss/main/Kconfig.projbuild +++ b/examples/protocols/mqtt/wss/main/Kconfig.projbuild @@ -1,17 +1,5 @@ menu "Example Configuration" - config WIFI_SSID - string "WiFi SSID" - default "myssid" - help - SSID (network name) for the example to connect to. - - config WIFI_PASSWORD - string "WiFi Password" - default "mypassword" - help - WiFi password (WPA or WPA2) for the example to use. - config BROKER_URI string "Broker URL" default "wss://iot.eclipse.org:443/ws" diff --git a/examples/protocols/mqtt/wss/main/app_main.c b/examples/protocols/mqtt/wss/main/app_main.c index 5059549..cbf8631 100644 --- a/examples/protocols/mqtt/wss/main/app_main.c +++ b/examples/protocols/mqtt/wss/main/app_main.c @@ -1,3 +1,11 @@ +/* MQTT over Secure Websockets Example + + This example 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 #include @@ -5,13 +13,14 @@ #include "esp_wifi.h" #include "esp_system.h" #include "nvs_flash.h" -#include "esp_event_loop.h" +#include "esp_event.h" +#include "tcpip_adapter.h" +#include "protocol_examples_common.h" #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "freertos/semphr.h" #include "freertos/queue.h" -#include "freertos/event_groups.h" #include "lwip/sockets.h" #include "lwip/dns.h" @@ -22,52 +31,6 @@ static const char *TAG = "MQTTWSS_EXAMPLE"; -static EventGroupHandle_t wifi_event_group; -const static int CONNECTED_BIT = BIT0; - - - -static esp_err_t wifi_event_handler(void *ctx, system_event_t *event) -{ - switch (event->event_id) { - case SYSTEM_EVENT_STA_START: - esp_wifi_connect(); - break; - case SYSTEM_EVENT_STA_GOT_IP: - xEventGroupSetBits(wifi_event_group, CONNECTED_BIT); - - break; - case SYSTEM_EVENT_STA_DISCONNECTED: - esp_wifi_connect(); - xEventGroupClearBits(wifi_event_group, CONNECTED_BIT); - break; - default: - break; - } - return ESP_OK; -} - -static void wifi_init(void) -{ - tcpip_adapter_init(); - wifi_event_group = xEventGroupCreate(); - ESP_ERROR_CHECK(esp_event_loop_init(wifi_event_handler, NULL)); - wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); - ESP_ERROR_CHECK(esp_wifi_init(&cfg)); - ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM)); - wifi_config_t wifi_config = { - .sta = { - .ssid = CONFIG_WIFI_SSID, - .password = CONFIG_WIFI_PASSWORD, - }, - }; - ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); - ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config)); - ESP_LOGI(TAG, "start the WIFI SSID:[%s]", CONFIG_WIFI_SSID); - ESP_ERROR_CHECK(esp_wifi_start()); - ESP_LOGI(TAG, "Waiting for wifi"); - xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT, false, true, portMAX_DELAY); -} #if CONFIG_BROKER_CERTIFICATE_OVERRIDDEN == 1 static const uint8_t iot_eclipse_org_pem_start[] = "-----BEGIN CERTIFICATE-----\n" CONFIG_BROKER_CERTIFICATE_OVERRIDE "\n-----END CERTIFICATE-----"; @@ -149,7 +112,15 @@ void app_main() esp_log_level_set("TRANSPORT", ESP_LOG_VERBOSE); esp_log_level_set("OUTBOX", ESP_LOG_VERBOSE); - nvs_flash_init(); - wifi_init(); + ESP_ERROR_CHECK(nvs_flash_init()); + tcpip_adapter_init(); + ESP_ERROR_CHECK(esp_event_loop_create_default()); + + /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig. + * Read "Establishing Wi-Fi or Ethernet Connection" section in + * examples/protocols/README.md for more information about this function. + */ + ESP_ERROR_CHECK(example_connect()); + mqtt_app_start(); } From 4624326cda77cc2d6c594dc3129d4489a7a423ce Mon Sep 17 00:00:00 2001 From: Roland Dobai Date: Thu, 9 May 2019 16:43:06 +0200 Subject: [PATCH 017/231] Rename Kconfig options (examples) --- .../mqtt/publish_test/main/Kconfig.projbuild | 18 +++++++++--------- .../mqtt/publish_test/main/publish_test.c | 16 ++++++++-------- .../protocols/mqtt/publish_test/sdkconfig.ci | 10 +++++----- 3 files changed, 22 insertions(+), 22 deletions(-) diff --git a/examples/protocols/mqtt/publish_test/main/Kconfig.projbuild b/examples/protocols/mqtt/publish_test/main/Kconfig.projbuild index 66d3cb0..8360e57 100644 --- a/examples/protocols/mqtt/publish_test/main/Kconfig.projbuild +++ b/examples/protocols/mqtt/publish_test/main/Kconfig.projbuild @@ -1,50 +1,50 @@ menu "Example Configuration" - config BROKER_SSL_URI + config EXAMPLE_BROKER_SSL_URI string "Broker SSL URL" default "mqtts://iot.eclipse.org:8883" help URL of an mqtt broker for ssl transport - config BROKER_TCP_URI + config EXAMPLE_BROKER_TCP_URI string "Broker TCP URL" default "mqtt://iot.eclipse.org:1883" help URL of an mqtt broker for tcp transport - config BROKER_WS_URI + config EXAMPLE_BROKER_WS_URI string "Broker WS URL" default "ws://iot.eclipse.org:80/ws" help URL of an mqtt broker for ws transport - config BROKER_WSS_URI + config EXAMPLE_BROKER_WSS_URI string "Broker WSS URL" default "wss://iot.eclipse.org:443/ws" help URL of an mqtt broker for wss transport - config PUBLISH_TOPIC + config EXAMPLE_PUBLISH_TOPIC string "publish topic" default "/topic/publish/esp2py" help topic to which esp32 client publishes - config SUBSCIBE_TOPIC + config EXAMPLE_SUBSCIBE_TOPIC string "subscribe topic" default "/topic/subscribe/py2esp" help topic to which esp32 client subsribes (and expects data) - config BROKER_CERTIFICATE_OVERRIDE + config EXAMPLE_BROKER_CERTIFICATE_OVERRIDE string "Broker certificate override" default "" help Please leave empty if broker certificate included from a textfile; otherwise fill in a base64 part of PEM format certificate - config BROKER_CERTIFICATE_OVERRIDDEN + config EXAMPLE_BROKER_CERTIFICATE_OVERRIDDEN bool - default y if BROKER_CERTIFICATE_OVERRIDE != "" + default y if EXAMPLE_BROKER_CERTIFICATE_OVERRIDE != "" endmenu diff --git a/examples/protocols/mqtt/publish_test/main/publish_test.c b/examples/protocols/mqtt/publish_test/main/publish_test.c index 99ef19d..15cd2b0 100644 --- a/examples/protocols/mqtt/publish_test/main/publish_test.c +++ b/examples/protocols/mqtt/publish_test/main/publish_test.c @@ -45,8 +45,8 @@ static size_t actual_published = 0; static int qos_test = 0; -#if CONFIG_BROKER_CERTIFICATE_OVERRIDDEN == 1 -static const uint8_t iot_eclipse_org_pem_start[] = "-----BEGIN CERTIFICATE-----\n" CONFIG_BROKER_CERTIFICATE_OVERRIDE "\n-----END CERTIFICATE-----"; +#if CONFIG_EXAMPLE_BROKER_CERTIFICATE_OVERRIDDEN == 1 +static const uint8_t iot_eclipse_org_pem_start[] = "-----BEGIN CERTIFICATE-----\n" CONFIG_EXAMPLE_BROKER_CERTIFICATE_OVERRIDE "\n-----END CERTIFICATE-----"; #else extern const uint8_t iot_eclipse_org_pem_start[] asm("_binary_iot_eclipse_org_pem_start"); #endif @@ -62,7 +62,7 @@ static esp_err_t mqtt_event_handler(esp_mqtt_event_handle_t event) case MQTT_EVENT_CONNECTED: ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED"); xEventGroupSetBits(mqtt_event_group, CONNECTED_BIT); - msg_id = esp_mqtt_client_subscribe(client, CONFIG_SUBSCIBE_TOPIC, qos_test); + msg_id = esp_mqtt_client_subscribe(client, CONFIG_EXAMPLE_SUBSCIBE_TOPIC, qos_test); ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id); break; @@ -199,16 +199,16 @@ void app_main() if (0 == strcmp(transport, "tcp")) { ESP_LOGI(TAG, "[TCP transport] Startup.."); - esp_mqtt_client_set_uri(mqtt_client, CONFIG_BROKER_TCP_URI); + esp_mqtt_client_set_uri(mqtt_client, CONFIG_EXAMPLE_BROKER_TCP_URI); } else if (0 == strcmp(transport, "ssl")) { ESP_LOGI(TAG, "[SSL transport] Startup.."); - esp_mqtt_client_set_uri(mqtt_client, CONFIG_BROKER_SSL_URI); + esp_mqtt_client_set_uri(mqtt_client, CONFIG_EXAMPLE_BROKER_SSL_URI); } else if (0 == strcmp(transport, "ws")) { ESP_LOGI(TAG, "[WS transport] Startup.."); - esp_mqtt_client_set_uri(mqtt_client, CONFIG_BROKER_WS_URI); + esp_mqtt_client_set_uri(mqtt_client, CONFIG_EXAMPLE_BROKER_WS_URI); } else if (0 == strcmp(transport, "wss")) { ESP_LOGI(TAG, "[WSS transport] Startup.."); - esp_mqtt_client_set_uri(mqtt_client, CONFIG_BROKER_WSS_URI); + esp_mqtt_client_set_uri(mqtt_client, CONFIG_EXAMPLE_BROKER_WSS_URI); } else { ESP_LOGE(TAG, "Unexpected transport"); abort(); @@ -219,7 +219,7 @@ void app_main() xEventGroupWaitBits(mqtt_event_group, CONNECTED_BIT, false, true, portMAX_DELAY); for (int i = 0; i < expected_published; i++) { - int msg_id = esp_mqtt_client_publish(mqtt_client, CONFIG_PUBLISH_TOPIC, expected_data, expected_size, qos_test, 0); + int msg_id = esp_mqtt_client_publish(mqtt_client, CONFIG_EXAMPLE_PUBLISH_TOPIC, expected_data, expected_size, qos_test, 0); ESP_LOGI(TAG, "[%d] Publishing...", msg_id); } } diff --git a/examples/protocols/mqtt/publish_test/sdkconfig.ci b/examples/protocols/mqtt/publish_test/sdkconfig.ci index 00db776..4ff3a8d 100644 --- a/examples/protocols/mqtt/publish_test/sdkconfig.ci +++ b/examples/protocols/mqtt/publish_test/sdkconfig.ci @@ -1,5 +1,5 @@ -CONFIG_BROKER_SSL_URI="mqtts://${EXAMPLE_MQTT_BROKER_SSL}" -CONFIG_BROKER_TCP_URI="mqtt://${EXAMPLE_MQTT_BROKER_TCP}" -CONFIG_BROKER_WS_URI="ws://${EXAMPLE_MQTT_BROKER_WS}/ws" -CONFIG_BROKER_WSS_URI="wss://${EXAMPLE_MQTT_BROKER_WSS}/ws" -CONFIG_BROKER_CERTIFICATE_OVERRIDE="${EXAMPLE_MQTT_BROKER_CERTIFICATE}" +CONFIG_EXAMPLE_BROKER_SSL_URI="mqtts://${EXAMPLE_MQTT_BROKER_SSL}" +CONFIG_EXAMPLE_BROKER_TCP_URI="mqtt://${EXAMPLE_MQTT_BROKER_TCP}" +CONFIG_EXAMPLE_BROKER_WS_URI="ws://${EXAMPLE_MQTT_BROKER_WS}/ws" +CONFIG_EXAMPLE_BROKER_WSS_URI="wss://${EXAMPLE_MQTT_BROKER_WSS}/ws" +CONFIG_EXAMPLE_BROKER_CERTIFICATE_OVERRIDE="${EXAMPLE_MQTT_BROKER_CERTIFICATE}" From b09c4daf0d8a042cd47fc5284f1135f04fe07c85 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Mon, 6 May 2019 16:14:48 +0200 Subject: [PATCH 018/231] 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 8c299a21d3be96f9d8da1f857428c7a965869a81 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Wed, 17 Apr 2019 15:56:59 +0200 Subject: [PATCH 019/231] mqtt: added support for esp event loop, updating examples to register and use event loop handler --- docs/en/api-reference/protocols/mqtt.rst | 7 +++++-- examples/protocols/mqtt/ssl/main/app_main.c | 9 +++++++-- examples/protocols/mqtt/tcp/main/app_main.c | 12 ++++++++---- examples/protocols/mqtt/ws/main/app_main.c | 10 +++++++--- examples/protocols/mqtt/wss/main/app_main.c | 11 +++++++++-- 5 files changed, 36 insertions(+), 13 deletions(-) diff --git a/docs/en/api-reference/protocols/mqtt.rst b/docs/en/api-reference/protocols/mqtt.rst index 3fa366b..053159e 100644 --- a/docs/en/api-reference/protocols/mqtt.rst +++ b/docs/en/api-reference/protocols/mqtt.rst @@ -56,9 +56,13 @@ URI const esp_mqtt_client_config_t mqtt_cfg = { .uri = "mqtt://iot.eclipse.org", - .event_handle = mqtt_event_handler, // .user_context = (void *)your_context }; + esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg); + esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, client); + esp_mqtt_client_start(client); + +- Note: By default mqtt client uses event loop library to post related mqtt events (connected, subsribed, published, etc.) - If there are any options related to the URI in ``esp_mqtt_client_config_t``, the option defined by the URI will be @@ -68,7 +72,6 @@ URI const esp_mqtt_client_config_t mqtt_cfg = { .uri = "mqtt://iot.eclipse.org:1234", - .event_handle = mqtt_event_handler, .port = 4567, }; //MQTT client will connect to iot.eclipse.org using port 4567 diff --git a/examples/protocols/mqtt/ssl/main/app_main.c b/examples/protocols/mqtt/ssl/main/app_main.c index 88e1a0c..090e802 100644 --- a/examples/protocols/mqtt/ssl/main/app_main.c +++ b/examples/protocols/mqtt/ssl/main/app_main.c @@ -40,7 +40,7 @@ extern const uint8_t iot_eclipse_org_pem_start[] asm("_binary_iot_eclipse_org_ #endif extern const uint8_t iot_eclipse_org_pem_end[] asm("_binary_iot_eclipse_org_pem_end"); -static esp_err_t mqtt_event_handler(esp_mqtt_event_handle_t event) +static esp_err_t mqtt_event_handler_cb(esp_mqtt_event_handle_t event) { esp_mqtt_client_handle_t client = event->client; int msg_id; @@ -87,17 +87,21 @@ static esp_err_t mqtt_event_handler(esp_mqtt_event_handle_t event) return ESP_OK; } +static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data) { + ESP_LOGD(TAG, "Event dispatched from event loop base=%s, event_id=%d", base, event_id); + mqtt_event_handler_cb(event_data); +} static void mqtt_app_start(void) { const esp_mqtt_client_config_t mqtt_cfg = { .uri = CONFIG_BROKER_URI, - .event_handle = mqtt_event_handler, .cert_pem = (const char *)iot_eclipse_org_pem_start, }; ESP_LOGI(TAG, "[APP] Free memory: %d bytes", esp_get_free_heap_size()); esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg); + esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, client); esp_mqtt_client_start(client); } @@ -109,6 +113,7 @@ void app_main() esp_log_level_set("*", ESP_LOG_INFO); esp_log_level_set("MQTT_CLIENT", ESP_LOG_VERBOSE); + esp_log_level_set("MQTT_EXAMPLE", ESP_LOG_VERBOSE); esp_log_level_set("TRANSPORT_TCP", ESP_LOG_VERBOSE); esp_log_level_set("TRANSPORT_SSL", ESP_LOG_VERBOSE); esp_log_level_set("TRANSPORT", ESP_LOG_VERBOSE); diff --git a/examples/protocols/mqtt/tcp/main/app_main.c b/examples/protocols/mqtt/tcp/main/app_main.c index 1863a01..9e24a37 100644 --- a/examples/protocols/mqtt/tcp/main/app_main.c +++ b/examples/protocols/mqtt/tcp/main/app_main.c @@ -33,7 +33,7 @@ static const char *TAG = "MQTT_EXAMPLE"; -static esp_err_t mqtt_event_handler(esp_mqtt_event_handle_t event) +static esp_err_t mqtt_event_handler_cb(esp_mqtt_event_handle_t event) { esp_mqtt_client_handle_t client = event->client; int msg_id; @@ -83,14 +83,16 @@ static esp_err_t mqtt_event_handler(esp_mqtt_event_handle_t event) return ESP_OK; } +static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data) { + ESP_LOGD(TAG, "Event dispatched from event loop base=%s, event_id=%d", base, event_id); + mqtt_event_handler_cb(event_data); +} + static void mqtt_app_start(void) { esp_mqtt_client_config_t mqtt_cfg = { .uri = CONFIG_BROKER_URL, - .event_handle = mqtt_event_handler, - // .user_context = (void *)your_context }; - #if CONFIG_BROKER_URL_FROM_STDIN char line[128]; @@ -117,6 +119,7 @@ static void mqtt_app_start(void) #endif /* CONFIG_BROKER_URL_FROM_STDIN */ esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg); + esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, client); esp_mqtt_client_start(client); } @@ -128,6 +131,7 @@ void app_main() esp_log_level_set("*", ESP_LOG_INFO); esp_log_level_set("MQTT_CLIENT", ESP_LOG_VERBOSE); + esp_log_level_set("MQTT_EXAMPLE", ESP_LOG_VERBOSE); esp_log_level_set("TRANSPORT_TCP", ESP_LOG_VERBOSE); esp_log_level_set("TRANSPORT_SSL", ESP_LOG_VERBOSE); esp_log_level_set("TRANSPORT", ESP_LOG_VERBOSE); diff --git a/examples/protocols/mqtt/ws/main/app_main.c b/examples/protocols/mqtt/ws/main/app_main.c index 0387e96..cb45759 100644 --- a/examples/protocols/mqtt/ws/main/app_main.c +++ b/examples/protocols/mqtt/ws/main/app_main.c @@ -32,7 +32,7 @@ static const char *TAG = "MQTTWS_EXAMPLE"; -static esp_err_t mqtt_event_handler(esp_mqtt_event_handle_t event) +static esp_err_t mqtt_event_handler_cb(esp_mqtt_event_handle_t event) { esp_mqtt_client_handle_t client = event->client; int msg_id; @@ -79,16 +79,19 @@ static esp_err_t mqtt_event_handler(esp_mqtt_event_handle_t event) return ESP_OK; } +static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data) { + ESP_LOGD(TAG, "Event dispatched from event loop base=%s, event_id=%d", base, event_id); + mqtt_event_handler_cb(event_data); +} static void mqtt_app_start(void) { const esp_mqtt_client_config_t mqtt_cfg = { .uri = CONFIG_BROKER_URI, - .event_handle = mqtt_event_handler, - // .user_context = (void *)your_context }; esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg); + esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, client); esp_mqtt_client_start(client); } @@ -100,6 +103,7 @@ void app_main() esp_log_level_set("*", ESP_LOG_INFO); esp_log_level_set("MQTT_CLIENT", ESP_LOG_VERBOSE); + esp_log_level_set("MQTT_EXAMPLE", ESP_LOG_VERBOSE); esp_log_level_set("TRANSPORT_TCP", ESP_LOG_VERBOSE); esp_log_level_set("TRANSPORT_SSL", ESP_LOG_VERBOSE); esp_log_level_set("TRANSPORT_WS", ESP_LOG_VERBOSE); diff --git a/examples/protocols/mqtt/wss/main/app_main.c b/examples/protocols/mqtt/wss/main/app_main.c index cbf8631..dca752b 100644 --- a/examples/protocols/mqtt/wss/main/app_main.c +++ b/examples/protocols/mqtt/wss/main/app_main.c @@ -39,7 +39,7 @@ extern const uint8_t iot_eclipse_org_pem_start[] asm("_binary_iot_eclipse_org_ #endif extern const uint8_t iot_eclipse_org_pem_end[] asm("_binary_iot_eclipse_org_pem_end"); -static esp_err_t mqtt_event_handler(esp_mqtt_event_handle_t event) +static esp_err_t mqtt_event_handler_cb(esp_mqtt_event_handle_t event) { esp_mqtt_client_handle_t client = event->client; int msg_id; @@ -86,16 +86,22 @@ static esp_err_t mqtt_event_handler(esp_mqtt_event_handle_t event) return ESP_OK; } +static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data) { + ESP_LOGD(TAG, "Event dispatched from event loop base=%s, event_id=%d", base, event_id); + mqtt_event_handler_cb(event_data); +} + static void mqtt_app_start(void) { const esp_mqtt_client_config_t mqtt_cfg = { .uri = CONFIG_BROKER_URI, - .event_handle = mqtt_event_handler, .cert_pem = (const char *)iot_eclipse_org_pem_start, }; ESP_LOGI(TAG, "[APP] Free memory: %d bytes", esp_get_free_heap_size()); esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg); + esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, client); + esp_mqtt_client_start(client); } @@ -107,6 +113,7 @@ void app_main() esp_log_level_set("*", ESP_LOG_INFO); esp_log_level_set("MQTT_CLIENT", ESP_LOG_VERBOSE); + esp_log_level_set("MQTT_EXAMPLE", ESP_LOG_VERBOSE); esp_log_level_set("TRANSPORT_TCP", ESP_LOG_VERBOSE); esp_log_level_set("TRANSPORT_SSL", ESP_LOG_VERBOSE); esp_log_level_set("TRANSPORT", ESP_LOG_VERBOSE); From 0ff7c4ee21d8c34a844037a9188f9fcdee5b4b7d Mon Sep 17 00:00:00 2001 From: "Michael (XIAO Xufeng)" Date: Mon, 8 Jul 2019 09:16:06 +0800 Subject: [PATCH 020/231] ci: support to build esp32s2beta simple examples --- examples/protocols/mqtt/publish_test/CMakeLists.txt | 1 + examples/protocols/mqtt/ssl/CMakeLists.txt | 1 + examples/protocols/mqtt/ssl_mutual_auth/CMakeLists.txt | 1 + examples/protocols/mqtt/tcp/CMakeLists.txt | 1 + examples/protocols/mqtt/ws/CMakeLists.txt | 1 + examples/protocols/mqtt/wss/CMakeLists.txt | 1 + 6 files changed, 6 insertions(+) diff --git a/examples/protocols/mqtt/publish_test/CMakeLists.txt b/examples/protocols/mqtt/publish_test/CMakeLists.txt index 2203d06..e494bbb 100644 --- a/examples/protocols/mqtt/publish_test/CMakeLists.txt +++ b/examples/protocols/mqtt/publish_test/CMakeLists.txt @@ -6,6 +6,7 @@ cmake_minimum_required(VERSION 3.5) # This example uses an extra component for common functions such as Wi-Fi and Ethernet connection. set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common) +set(SUPPORTED_TARGETS esp32) include($ENV{IDF_PATH}/tools/cmake/project.cmake) project(mqtt_publish) diff --git a/examples/protocols/mqtt/ssl/CMakeLists.txt b/examples/protocols/mqtt/ssl/CMakeLists.txt index a7b2c44..a265066 100644 --- a/examples/protocols/mqtt/ssl/CMakeLists.txt +++ b/examples/protocols/mqtt/ssl/CMakeLists.txt @@ -6,6 +6,7 @@ cmake_minimum_required(VERSION 3.5) # This example uses an extra component for common functions such as Wi-Fi and Ethernet connection. set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common) +set(SUPPORTED_TARGETS esp32) include($ENV{IDF_PATH}/tools/cmake/project.cmake) project(mqtt_ssl) diff --git a/examples/protocols/mqtt/ssl_mutual_auth/CMakeLists.txt b/examples/protocols/mqtt/ssl_mutual_auth/CMakeLists.txt index 472a3ca..cd3de74 100644 --- a/examples/protocols/mqtt/ssl_mutual_auth/CMakeLists.txt +++ b/examples/protocols/mqtt/ssl_mutual_auth/CMakeLists.txt @@ -6,6 +6,7 @@ cmake_minimum_required(VERSION 3.5) # This example uses an extra component for common functions such as Wi-Fi and Ethernet connection. set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common) +set(SUPPORTED_TARGETS esp32) include($ENV{IDF_PATH}/tools/cmake/project.cmake) project(mqtt_ssl_mutual_auth) diff --git a/examples/protocols/mqtt/tcp/CMakeLists.txt b/examples/protocols/mqtt/tcp/CMakeLists.txt index 5a70e7a..25e9bc3 100644 --- a/examples/protocols/mqtt/tcp/CMakeLists.txt +++ b/examples/protocols/mqtt/tcp/CMakeLists.txt @@ -6,5 +6,6 @@ cmake_minimum_required(VERSION 3.5) # This example uses an extra component for common functions such as Wi-Fi and Ethernet connection. set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common) +set(SUPPORTED_TARGETS esp32) include($ENV{IDF_PATH}/tools/cmake/project.cmake) project(mqtt_tcp) \ No newline at end of file diff --git a/examples/protocols/mqtt/ws/CMakeLists.txt b/examples/protocols/mqtt/ws/CMakeLists.txt index f049006..6b7b93d 100644 --- a/examples/protocols/mqtt/ws/CMakeLists.txt +++ b/examples/protocols/mqtt/ws/CMakeLists.txt @@ -6,5 +6,6 @@ cmake_minimum_required(VERSION 3.5) # This example uses an extra component for common functions such as Wi-Fi and Ethernet connection. set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common) +set(SUPPORTED_TARGETS esp32) include($ENV{IDF_PATH}/tools/cmake/project.cmake) project(mqtt_websocket) diff --git a/examples/protocols/mqtt/wss/CMakeLists.txt b/examples/protocols/mqtt/wss/CMakeLists.txt index 3e2d5fc..6519f02 100644 --- a/examples/protocols/mqtt/wss/CMakeLists.txt +++ b/examples/protocols/mqtt/wss/CMakeLists.txt @@ -6,6 +6,7 @@ cmake_minimum_required(VERSION 3.5) # This example uses an extra component for common functions such as Wi-Fi and Ethernet connection. set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common) +set(SUPPORTED_TARGETS esp32) include($ENV{IDF_PATH}/tools/cmake/project.cmake) project(mqtt_websocket_secure) From 0e49903b2fe2d3a3ff5c5e25e61f04474e5eb972 Mon Sep 17 00:00:00 2001 From: Renz Christian Bagaporo Date: Sun, 28 Apr 2019 15:38:46 +0800 Subject: [PATCH 021/231] examples: use new component registration api --- examples/protocols/mqtt/publish_test/main/CMakeLists.txt | 6 ++---- examples/protocols/mqtt/ssl/main/CMakeLists.txt | 6 ++---- examples/protocols/mqtt/ssl_mutual_auth/main/CMakeLists.txt | 6 ++---- examples/protocols/mqtt/tcp/main/CMakeLists.txt | 6 ++---- examples/protocols/mqtt/ws/main/CMakeLists.txt | 6 ++---- examples/protocols/mqtt/wss/main/CMakeLists.txt | 6 ++---- 6 files changed, 12 insertions(+), 24 deletions(-) diff --git a/examples/protocols/mqtt/publish_test/main/CMakeLists.txt b/examples/protocols/mqtt/publish_test/main/CMakeLists.txt index c3074a7..67c4c7b 100644 --- a/examples/protocols/mqtt/publish_test/main/CMakeLists.txt +++ b/examples/protocols/mqtt/publish_test/main/CMakeLists.txt @@ -1,4 +1,2 @@ -set(COMPONENT_SRCS "publish_test.c") -set(COMPONENT_ADD_INCLUDEDIRS ".") - -register_component() +idf_component_register(SRCS "publish_test.c" + INCLUDE_DIRS ".") \ No newline at end of file diff --git a/examples/protocols/mqtt/ssl/main/CMakeLists.txt b/examples/protocols/mqtt/ssl/main/CMakeLists.txt index 6b03500..e739255 100644 --- a/examples/protocols/mqtt/ssl/main/CMakeLists.txt +++ b/examples/protocols/mqtt/ssl/main/CMakeLists.txt @@ -1,4 +1,2 @@ -set(COMPONENT_SRCS "app_main.c") -set(COMPONENT_ADD_INCLUDEDIRS ".") - -register_component() +idf_component_register(SRCS "app_main.c" + INCLUDE_DIRS ".") \ No newline at end of file diff --git a/examples/protocols/mqtt/ssl_mutual_auth/main/CMakeLists.txt b/examples/protocols/mqtt/ssl_mutual_auth/main/CMakeLists.txt index 6b03500..e739255 100644 --- a/examples/protocols/mqtt/ssl_mutual_auth/main/CMakeLists.txt +++ b/examples/protocols/mqtt/ssl_mutual_auth/main/CMakeLists.txt @@ -1,4 +1,2 @@ -set(COMPONENT_SRCS "app_main.c") -set(COMPONENT_ADD_INCLUDEDIRS ".") - -register_component() +idf_component_register(SRCS "app_main.c" + INCLUDE_DIRS ".") \ No newline at end of file diff --git a/examples/protocols/mqtt/tcp/main/CMakeLists.txt b/examples/protocols/mqtt/tcp/main/CMakeLists.txt index 6b03500..e739255 100644 --- a/examples/protocols/mqtt/tcp/main/CMakeLists.txt +++ b/examples/protocols/mqtt/tcp/main/CMakeLists.txt @@ -1,4 +1,2 @@ -set(COMPONENT_SRCS "app_main.c") -set(COMPONENT_ADD_INCLUDEDIRS ".") - -register_component() +idf_component_register(SRCS "app_main.c" + INCLUDE_DIRS ".") \ No newline at end of file diff --git a/examples/protocols/mqtt/ws/main/CMakeLists.txt b/examples/protocols/mqtt/ws/main/CMakeLists.txt index 6b03500..e739255 100644 --- a/examples/protocols/mqtt/ws/main/CMakeLists.txt +++ b/examples/protocols/mqtt/ws/main/CMakeLists.txt @@ -1,4 +1,2 @@ -set(COMPONENT_SRCS "app_main.c") -set(COMPONENT_ADD_INCLUDEDIRS ".") - -register_component() +idf_component_register(SRCS "app_main.c" + INCLUDE_DIRS ".") \ No newline at end of file diff --git a/examples/protocols/mqtt/wss/main/CMakeLists.txt b/examples/protocols/mqtt/wss/main/CMakeLists.txt index 6b03500..e739255 100644 --- a/examples/protocols/mqtt/wss/main/CMakeLists.txt +++ b/examples/protocols/mqtt/wss/main/CMakeLists.txt @@ -1,4 +1,2 @@ -set(COMPONENT_SRCS "app_main.c") -set(COMPONENT_ADD_INCLUDEDIRS ".") - -register_component() +idf_component_register(SRCS "app_main.c" + INCLUDE_DIRS ".") \ No newline at end of file From 948eea4a26cdc3b6979739374e96c6e2e8f4526d Mon Sep 17 00:00:00 2001 From: David Cermak Date: Tue, 9 Apr 2019 16:08:05 +0200 Subject: [PATCH 022/231] esp-tls: capturing specific errors to be available in tcp_transport and then in application code --- examples/protocols/mqtt/ssl/main/app_main.c | 1 + 1 file changed, 1 insertion(+) diff --git a/examples/protocols/mqtt/ssl/main/app_main.c b/examples/protocols/mqtt/ssl/main/app_main.c index 090e802..fa50e16 100644 --- a/examples/protocols/mqtt/ssl/main/app_main.c +++ b/examples/protocols/mqtt/ssl/main/app_main.c @@ -79,6 +79,7 @@ static esp_err_t mqtt_event_handler_cb(esp_mqtt_event_handle_t event) break; case MQTT_EVENT_ERROR: ESP_LOGI(TAG, "MQTT_EVENT_ERROR"); + ESP_LOGI(TAG, "Error code: 0x%x", event->last_err); break; default: ESP_LOGI(TAG, "Other event id:%d", event->event_id); From f70dafe18b967112f99732dc4be7f7ea0dd8757d Mon Sep 17 00:00:00 2001 From: David Cermak Date: Tue, 16 Apr 2019 11:58:38 +0200 Subject: [PATCH 023/231] 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 --- examples/protocols/mqtt/ssl/main/app_main.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/examples/protocols/mqtt/ssl/main/app_main.c b/examples/protocols/mqtt/ssl/main/app_main.c index fa50e16..e819c8f 100644 --- a/examples/protocols/mqtt/ssl/main/app_main.c +++ b/examples/protocols/mqtt/ssl/main/app_main.c @@ -29,6 +29,7 @@ #include "esp_log.h" #include "mqtt_client.h" +#include "esp_tls.h" static const char *TAG = "MQTTS_EXAMPLE"; @@ -79,7 +80,10 @@ static esp_err_t mqtt_event_handler_cb(esp_mqtt_event_handle_t event) break; case MQTT_EVENT_ERROR: ESP_LOGI(TAG, "MQTT_EVENT_ERROR"); - ESP_LOGI(TAG, "Error code: 0x%x", event->last_err); + int mbedtls_err = 0; + esp_err_t err = esp_tls_get_and_clear_last_error(event->error_handle, &mbedtls_err, NULL); + ESP_LOGI(TAG, "Last esp error code: 0x%x", err); + ESP_LOGI(TAG, "Last mbedtls failure: 0x%x", mbedtls_err); break; default: ESP_LOGI(TAG, "Other event id:%d", event->event_id); From 0872e4ec683f63918c00adf7f5c33789b93c05be Mon Sep 17 00:00:00 2001 From: Angus Gratton Date: Sun, 23 Jun 2019 11:54:31 +1000 Subject: [PATCH 024/231] build system: Use CMake-based build system as default when describing commands --- docs/en/api-reference/protocols/mqtt.rst | 8 ++++---- examples/protocols/mqtt/publish_test/README.md | 2 +- examples/protocols/mqtt/ssl/README.md | 2 +- examples/protocols/mqtt/ssl_mutual_auth/README.md | 2 +- examples/protocols/mqtt/tcp/README.md | 2 +- examples/protocols/mqtt/ws/README.md | 2 +- examples/protocols/mqtt/wss/README.md | 2 +- 7 files changed, 10 insertions(+), 10 deletions(-) diff --git a/docs/en/api-reference/protocols/mqtt.rst b/docs/en/api-reference/protocols/mqtt.rst index 053159e..cb95675 100644 --- a/docs/en/api-reference/protocols/mqtt.rst +++ b/docs/en/api-reference/protocols/mqtt.rst @@ -94,12 +94,12 @@ SSL For more options on ``esp_mqtt_client_config_t``, please refer to API reference below -Change settings in ``menuconfig`` -^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +Change settings in Project Configuration Menu +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ :: - make menuconfig - -> Component config -> ESP-MQTT Configuration + idf.py menuconfig + -> Component config -> ESP-MQTT Configuration - :ref:`CONFIG_MQTT_PROTOCOL_311`: Enables 3.1.1 version of MQTT protocol diff --git a/examples/protocols/mqtt/publish_test/README.md b/examples/protocols/mqtt/publish_test/README.md index bdc2bc8..d3db836 100644 --- a/examples/protocols/mqtt/publish_test/README.md +++ b/examples/protocols/mqtt/publish_test/README.md @@ -22,7 +22,7 @@ This example can be executed on any ESP32 board, the only required interface is ### Configure the project -* Run `make menuconfig` (or `idf.py menuconfig` if using CMake build system) +* Open the project configuration menu (`idf.py menuconfig`) * Configure Wi-Fi or Ethernet under "Example Connection Configuration" menu. See "Establishing Wi-Fi or Ethernet Connection" section in [examples/protocols/README.md](../../README.md) for more details. * When using Make build system, set `Default serial port` under `Serial flasher config`. * Set brokers for all 4 transports (TCP, SSL, WS, WSS), also set certificate if needed diff --git a/examples/protocols/mqtt/ssl/README.md b/examples/protocols/mqtt/ssl/README.md index 929257d..5925c40 100644 --- a/examples/protocols/mqtt/ssl/README.md +++ b/examples/protocols/mqtt/ssl/README.md @@ -14,7 +14,7 @@ This example can be executed on any ESP32 board, the only required interface is ### Configure the project -* Run `make menuconfig` (or `idf.py menuconfig` if using CMake build system) +* Open the project configuration menu (`idf.py menuconfig`) * Configure Wi-Fi or Ethernet under "Example Connection Configuration" menu. See "Establishing Wi-Fi or Ethernet Connection" section in [examples/protocols/README.md](../../README.md) for more details. * When using Make build system, set `Default serial port` under `Serial flasher config`. diff --git a/examples/protocols/mqtt/ssl_mutual_auth/README.md b/examples/protocols/mqtt/ssl_mutual_auth/README.md index 763e267..9b20b37 100644 --- a/examples/protocols/mqtt/ssl_mutual_auth/README.md +++ b/examples/protocols/mqtt/ssl_mutual_auth/README.md @@ -14,7 +14,7 @@ This example can be executed on any ESP32 board, the only required interface is ### Configure the project -* Run `make menuconfig` (or `idf.py menuconfig` if using CMake build system) +* Open the project configuration menu (`idf.py menuconfig`) * Configure Wi-Fi or Ethernet under "Example Connection Configuration" menu. See "Establishing Wi-Fi or Ethernet Connection" section in [examples/protocols/README.md](../../README.md) for more details. * When using Make build system, set `Default serial port` under `Serial flasher config`. diff --git a/examples/protocols/mqtt/tcp/README.md b/examples/protocols/mqtt/tcp/README.md index aea6630..7247a7a 100644 --- a/examples/protocols/mqtt/tcp/README.md +++ b/examples/protocols/mqtt/tcp/README.md @@ -14,7 +14,7 @@ This example can be executed on any ESP32 board, the only required interface is ### Configure the project -* Run `make menuconfig` (or `idf.py menuconfig` if using CMake build system) +* Open the project configuration menu (`idf.py menuconfig`) * Configure Wi-Fi or Ethernet under "Example Connection Configuration" menu. See "Establishing Wi-Fi or Ethernet Connection" section in [examples/protocols/README.md](../../README.md) for more details. * When using Make build system, set `Default serial port` under `Serial flasher config`. diff --git a/examples/protocols/mqtt/ws/README.md b/examples/protocols/mqtt/ws/README.md index efe2efc..236521f 100644 --- a/examples/protocols/mqtt/ws/README.md +++ b/examples/protocols/mqtt/ws/README.md @@ -14,7 +14,7 @@ This example can be executed on any ESP32 board, the only required interface is ### Configure the project -* Run `make menuconfig` (or `idf.py menuconfig` if using CMake build system) +* Open the project configuration menu (`idf.py menuconfig`) * Configure Wi-Fi or Ethernet under "Example Connection Configuration" menu. See "Establishing Wi-Fi or Ethernet Connection" section in [examples/protocols/README.md](../../README.md) for more details. * When using Make build system, set `Default serial port` under `Serial flasher config`. diff --git a/examples/protocols/mqtt/wss/README.md b/examples/protocols/mqtt/wss/README.md index 9433986..453d52f 100644 --- a/examples/protocols/mqtt/wss/README.md +++ b/examples/protocols/mqtt/wss/README.md @@ -13,7 +13,7 @@ This example can be executed on any ESP32 board, the only required interface is ### Configure the project -* Run `make menuconfig` (or `idf.py menuconfig` if using CMake build system) +* Open the project configuration menu (`idf.py menuconfig`) * Configure Wi-Fi or Ethernet under "Example Connection Configuration" menu. See "Establishing Wi-Fi or Ethernet Connection" section in [examples/protocols/README.md](../../README.md) for more details. * When using Make build system, set `Default serial port` under `Serial flasher config`. From bf6652687349d5e20cb8e0755a50e7575bac21e3 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Wed, 12 Jun 2019 14:55:58 +0200 Subject: [PATCH 025/231] mqtt: update example documentation on generating a certificate for ssl authentication to hosts Closes https://github.com/espressif/esp-idf/issues/3593 --- examples/protocols/mqtt/ssl/README.md | 9 +++++++-- examples/protocols/mqtt/ssl_mutual_auth/README.md | 1 + examples/protocols/mqtt/tcp/README.md | 2 ++ examples/protocols/mqtt/ws/README.md | 1 + examples/protocols/mqtt/wss/README.md | 8 +++++++- 5 files changed, 18 insertions(+), 3 deletions(-) diff --git a/examples/protocols/mqtt/ssl/README.md b/examples/protocols/mqtt/ssl/README.md index 5925c40..db711fd 100644 --- a/examples/protocols/mqtt/ssl/README.md +++ b/examples/protocols/mqtt/ssl/README.md @@ -3,6 +3,7 @@ (See the README.md file in the upper level 'examples' directory for more information about examples.) This example connects to the broker iot.eclipse.org using ssl transport and as a demonstration subscribes/unsubscribes and send a message on certain topic. +(Please note that the public broker is maintained by the community so may not be always available, for details please see this [disclaimer](https://iot.eclipse.org/getting-started/#sandboxes)) It uses ESP-MQTT library which implements mqtt client to connect to mqtt broker. @@ -18,10 +19,14 @@ This example can be executed on any ESP32 board, the only required interface is * Configure Wi-Fi or Ethernet under "Example Connection Configuration" menu. See "Establishing Wi-Fi or Ethernet Connection" section in [examples/protocols/README.md](../../README.md) for more details. * When using Make build system, set `Default serial port` under `Serial flasher config`. -Note how to create a PEM certificate for iot.eclipse.org: +PEM certificate for this example could be extracted from an openssl `s_client` command connecting to iot.eclipse.org. +In case a host operating system has `openssl` and `sed` packages installed, one could execute the following command to download and save the root certificate to a file (Note for Windows users: Both Linux like environment or Windows native packages may be used). ``` -openssl s_client -showcerts -connect iot.eclipse.org:8883 /dev/null|openssl x509 -outform PEM >iot_eclipse_org.pem +echo "" | openssl s_client -showcerts -connect iot.eclipse.org:8883 | sed -n "1,/Root/d; /BEGIN/,/END/p" | openssl x509 -outform PEM >iot_eclipse_org.pem ``` +Please note that this is not a general command for downloading a root certificate for an arbitrary host; +this command works with iot.eclipse.org as the site provides root certificate in the chain, which then could be extracted +with text operation. ### Build and Flash diff --git a/examples/protocols/mqtt/ssl_mutual_auth/README.md b/examples/protocols/mqtt/ssl_mutual_auth/README.md index 9b20b37..bf5b57d 100644 --- a/examples/protocols/mqtt/ssl_mutual_auth/README.md +++ b/examples/protocols/mqtt/ssl_mutual_auth/README.md @@ -3,6 +3,7 @@ (See the README.md file in the upper level 'examples' directory for more information about examples.) This example connects to the broker test.mosquitto.org using ssl transport with client certificate and as a demonstration subscribes/unsubscribes and send a message on certain topic. +(Please note that the public broker is maintained by the community so may not be always available, for details please visit http://test.mosquitto.org) It uses ESP-MQTT library which implements mqtt client to connect to mqtt broker. diff --git a/examples/protocols/mqtt/tcp/README.md b/examples/protocols/mqtt/tcp/README.md index 7247a7a..7920a75 100644 --- a/examples/protocols/mqtt/tcp/README.md +++ b/examples/protocols/mqtt/tcp/README.md @@ -2,6 +2,8 @@ (See the README.md file in the upper level 'examples' directory for more information about examples.) This example connects to the broker URI selected using `make menuconfig` (using mqtt tcp transport) and as a demonstration subscribes/unsubscribes and send a message on certain topic. +(Please note that the public broker is maintained by the community so may not be always available, for details please see this [disclaimer](https://iot.eclipse.org/getting-started/#sandboxes)) + Note: If the URI equals `FROM_STDIN` then the broker address is read from stdin upon application startup (used for testing) It uses ESP-MQTT library which implements mqtt client to connect to mqtt broker. diff --git a/examples/protocols/mqtt/ws/README.md b/examples/protocols/mqtt/ws/README.md index 236521f..d0eff6b 100644 --- a/examples/protocols/mqtt/ws/README.md +++ b/examples/protocols/mqtt/ws/README.md @@ -3,6 +3,7 @@ (See the README.md file in the upper level 'examples' directory for more information about examples.) This example connects to the broker iot.eclipse.org over web sockets as a demonstration subscribes/unsubscribes and send a message on certain topic. +(Please note that the public broker is maintained by the community so may not be always available, for details please see this [disclaimer](https://iot.eclipse.org/getting-started/#sandboxes)) It uses ESP-MQTT library which implements mqtt client to connect to mqtt broker. diff --git a/examples/protocols/mqtt/wss/README.md b/examples/protocols/mqtt/wss/README.md index 453d52f..1877979 100644 --- a/examples/protocols/mqtt/wss/README.md +++ b/examples/protocols/mqtt/wss/README.md @@ -2,6 +2,7 @@ (See the README.md file in the upper level 'examples' directory for more information about examples.) This example connects to the broker iot.eclipse.org over secure websockets and as a demonstration subscribes/unsubscribes and send a message on certain topic. +(Please note that the public broker is maintained by the community so may not be always available, for details please see this [disclaimer](https://iot.eclipse.org/getting-started/#sandboxes)) It uses ESP-MQTT library which implements mqtt client to connect to mqtt broker. @@ -19,9 +20,14 @@ This example can be executed on any ESP32 board, the only required interface is Note how to create a PEM certificate for iot.eclipse.org: +PEM certificate for this example could be extracted from an openssl `s_client` command connecting to iot.eclipse.org. +In case a host operating system has `openssl` and `sed` packages installed, one could execute the following command to download and save the root certificate to a file (Note for Windows users: Both Linux like environment or Windows native packages may be used). ``` -openssl s_client -showcerts -connect iot.eclipse.org:8883 /dev/null|openssl x509 -outform PEM >iot_eclipse_org.pem +echo "" | openssl s_client -showcerts -connect iot.eclipse.org:443 | sed -n "1,/Root/d; /BEGIN/,/END/p" | openssl x509 -outform PEM >iot_eclipse_org.pem ``` +Please note that this is not a general command for downloading a root certificate for an arbitrary host; +this command works with iot.eclipse.org as the site provides root certificate in the chain, which then could be extracted +with text operation. ### Build and Flash From 49360c1a9325c59b9afd79978e65db54c1cf65f1 Mon Sep 17 00:00:00 2001 From: Mahavir Jain Date: Fri, 2 Aug 2019 09:01:20 +0530 Subject: [PATCH 026/231] examples: change default build instructions in docs to CMake --- examples/protocols/mqtt/publish_test/README.md | 2 +- examples/protocols/mqtt/ssl/README.md | 2 +- examples/protocols/mqtt/ssl_mutual_auth/README.md | 2 +- examples/protocols/mqtt/tcp/README.md | 4 ++-- examples/protocols/mqtt/ws/README.md | 2 +- examples/protocols/mqtt/wss/README.md | 2 +- 6 files changed, 7 insertions(+), 7 deletions(-) diff --git a/examples/protocols/mqtt/publish_test/README.md b/examples/protocols/mqtt/publish_test/README.md index d3db836..ee377d5 100644 --- a/examples/protocols/mqtt/publish_test/README.md +++ b/examples/protocols/mqtt/publish_test/README.md @@ -33,7 +33,7 @@ This example can be executed on any ESP32 board, the only required interface is Build the project and flash it to the board, then run monitor tool to view serial output: ``` -make -j4 flash monitor +idf.py -p PORT flash monitor ``` (To exit the serial monitor, type ``Ctrl-]``.) diff --git a/examples/protocols/mqtt/ssl/README.md b/examples/protocols/mqtt/ssl/README.md index db711fd..e269372 100644 --- a/examples/protocols/mqtt/ssl/README.md +++ b/examples/protocols/mqtt/ssl/README.md @@ -33,7 +33,7 @@ with text operation. Build the project and flash it to the board, then run monitor tool to view serial output: ``` -make -j4 flash monitor +idf.py -p PORT flash monitor ``` (To exit the serial monitor, type ``Ctrl-]``.) diff --git a/examples/protocols/mqtt/ssl_mutual_auth/README.md b/examples/protocols/mqtt/ssl_mutual_auth/README.md index bf5b57d..0696a4e 100644 --- a/examples/protocols/mqtt/ssl_mutual_auth/README.md +++ b/examples/protocols/mqtt/ssl_mutual_auth/README.md @@ -43,7 +43,7 @@ Please note, that the supplied files `client.crt` and `client.key` in the `main` Build the project and flash it to the board, then run monitor tool to view serial output: ``` -make -j4 flash monitor +idf.py -p PORT flash monitor ``` (To exit the serial monitor, type ``Ctrl-]``.) diff --git a/examples/protocols/mqtt/tcp/README.md b/examples/protocols/mqtt/tcp/README.md index 7920a75..7fe788c 100644 --- a/examples/protocols/mqtt/tcp/README.md +++ b/examples/protocols/mqtt/tcp/README.md @@ -1,7 +1,7 @@ # ESP-MQTT sample application (See the README.md file in the upper level 'examples' directory for more information about examples.) -This example connects to the broker URI selected using `make menuconfig` (using mqtt tcp transport) and as a demonstration subscribes/unsubscribes and send a message on certain topic. +This example connects to the broker URI selected using `idf.py menuconfig` (using mqtt tcp transport) and as a demonstration subscribes/unsubscribes and send a message on certain topic. (Please note that the public broker is maintained by the community so may not be always available, for details please see this [disclaimer](https://iot.eclipse.org/getting-started/#sandboxes)) Note: If the URI equals `FROM_STDIN` then the broker address is read from stdin upon application startup (used for testing) @@ -25,7 +25,7 @@ This example can be executed on any ESP32 board, the only required interface is Build the project and flash it to the board, then run monitor tool to view serial output: ``` -make -j4 flash monitor +idf.py -p PORT flash monitor ``` (To exit the serial monitor, type ``Ctrl-]``.) diff --git a/examples/protocols/mqtt/ws/README.md b/examples/protocols/mqtt/ws/README.md index d0eff6b..c0526b0 100644 --- a/examples/protocols/mqtt/ws/README.md +++ b/examples/protocols/mqtt/ws/README.md @@ -24,7 +24,7 @@ This example can be executed on any ESP32 board, the only required interface is Build the project and flash it to the board, then run monitor tool to view serial output: ``` -make -j4 flash monitor +idf.py -p PORT flash monitor ``` (To exit the serial monitor, type ``Ctrl-]``.) diff --git a/examples/protocols/mqtt/wss/README.md b/examples/protocols/mqtt/wss/README.md index 1877979..9851d54 100644 --- a/examples/protocols/mqtt/wss/README.md +++ b/examples/protocols/mqtt/wss/README.md @@ -34,7 +34,7 @@ with text operation. Build the project and flash it to the board, then run monitor tool to view serial output: ``` -make -j4 flash monitor +idf.py -p PORT flash monitor ``` (To exit the serial monitor, type ``Ctrl-]``.) From 898d58b747b1969ead1699c5c0a57afe698a7a93 Mon Sep 17 00:00:00 2001 From: Anton Maklakov Date: Tue, 16 Jul 2019 16:33:30 +0700 Subject: [PATCH 027/231] tools: Mass fixing of empty prototypes (for -Wstrict-prototypes) --- examples/protocols/mqtt/publish_test/main/publish_test.c | 2 +- examples/protocols/mqtt/ssl/main/app_main.c | 2 +- examples/protocols/mqtt/ssl_mutual_auth/main/app_main.c | 2 +- examples/protocols/mqtt/tcp/main/app_main.c | 2 +- examples/protocols/mqtt/ws/main/app_main.c | 2 +- examples/protocols/mqtt/wss/main/app_main.c | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) diff --git a/examples/protocols/mqtt/publish_test/main/publish_test.c b/examples/protocols/mqtt/publish_test/main/publish_test.c index 15cd2b0..9183bbb 100644 --- a/examples/protocols/mqtt/publish_test/main/publish_test.c +++ b/examples/protocols/mqtt/publish_test/main/publish_test.c @@ -151,7 +151,7 @@ static void get_string(char *line, size_t size) } } -void app_main() +void app_main(void) { char line[256]; char pattern[32]; diff --git a/examples/protocols/mqtt/ssl/main/app_main.c b/examples/protocols/mqtt/ssl/main/app_main.c index e819c8f..fd0a49d 100644 --- a/examples/protocols/mqtt/ssl/main/app_main.c +++ b/examples/protocols/mqtt/ssl/main/app_main.c @@ -110,7 +110,7 @@ static void mqtt_app_start(void) esp_mqtt_client_start(client); } -void app_main() +void app_main(void) { ESP_LOGI(TAG, "[APP] Startup.."); ESP_LOGI(TAG, "[APP] Free memory: %d bytes", esp_get_free_heap_size()); diff --git a/examples/protocols/mqtt/ssl_mutual_auth/main/app_main.c b/examples/protocols/mqtt/ssl_mutual_auth/main/app_main.c index 21c76ff..5c6eab7 100644 --- a/examples/protocols/mqtt/ssl_mutual_auth/main/app_main.c +++ b/examples/protocols/mqtt/ssl_mutual_auth/main/app_main.c @@ -97,7 +97,7 @@ static void mqtt_app_start(void) esp_mqtt_client_start(client); } -void app_main() +void app_main(void) { ESP_LOGI(TAG, "[APP] Startup.."); ESP_LOGI(TAG, "[APP] Free memory: %d bytes", esp_get_free_heap_size()); diff --git a/examples/protocols/mqtt/tcp/main/app_main.c b/examples/protocols/mqtt/tcp/main/app_main.c index 9e24a37..7acd685 100644 --- a/examples/protocols/mqtt/tcp/main/app_main.c +++ b/examples/protocols/mqtt/tcp/main/app_main.c @@ -123,7 +123,7 @@ static void mqtt_app_start(void) esp_mqtt_client_start(client); } -void app_main() +void app_main(void) { ESP_LOGI(TAG, "[APP] Startup.."); ESP_LOGI(TAG, "[APP] Free memory: %d bytes", esp_get_free_heap_size()); diff --git a/examples/protocols/mqtt/ws/main/app_main.c b/examples/protocols/mqtt/ws/main/app_main.c index cb45759..cacea73 100644 --- a/examples/protocols/mqtt/ws/main/app_main.c +++ b/examples/protocols/mqtt/ws/main/app_main.c @@ -95,7 +95,7 @@ static void mqtt_app_start(void) esp_mqtt_client_start(client); } -void app_main() +void app_main(void) { ESP_LOGI(TAG, "[APP] Startup.."); ESP_LOGI(TAG, "[APP] Free memory: %d bytes", esp_get_free_heap_size()); diff --git a/examples/protocols/mqtt/wss/main/app_main.c b/examples/protocols/mqtt/wss/main/app_main.c index dca752b..a9de244 100644 --- a/examples/protocols/mqtt/wss/main/app_main.c +++ b/examples/protocols/mqtt/wss/main/app_main.c @@ -105,7 +105,7 @@ static void mqtt_app_start(void) esp_mqtt_client_start(client); } -void app_main() +void app_main(void) { ESP_LOGI(TAG, "[APP] Startup.."); ESP_LOGI(TAG, "[APP] Free memory: %d bytes", esp_get_free_heap_size()); From feb7bb6f8ae21bd5a28d13d20be48eb29a6fa4ee Mon Sep 17 00:00:00 2001 From: David Cermak Date: Thu, 23 May 2019 21:48:08 +0200 Subject: [PATCH 028/231] esp_tls: enable psk verification mode, added mqtt example using psk authentication --- .../protocols/mqtt/ssl_psk/CMakeLists.txt | 10 ++ examples/protocols/mqtt/ssl_psk/Makefile | 9 ++ examples/protocols/mqtt/ssl_psk/README.md | 76 ++++++++++ .../mqtt/ssl_psk/main/CMakeLists.txt | 4 + .../protocols/mqtt/ssl_psk/main/app_main.c | 141 ++++++++++++++++++ .../protocols/mqtt/ssl_psk/main/component.mk | 0 .../protocols/mqtt/ssl_psk/sdkconfig.defaults | 1 + 7 files changed, 241 insertions(+) create mode 100644 examples/protocols/mqtt/ssl_psk/CMakeLists.txt create mode 100644 examples/protocols/mqtt/ssl_psk/Makefile create mode 100644 examples/protocols/mqtt/ssl_psk/README.md create mode 100644 examples/protocols/mqtt/ssl_psk/main/CMakeLists.txt create mode 100644 examples/protocols/mqtt/ssl_psk/main/app_main.c create mode 100644 examples/protocols/mqtt/ssl_psk/main/component.mk create mode 100644 examples/protocols/mqtt/ssl_psk/sdkconfig.defaults diff --git a/examples/protocols/mqtt/ssl_psk/CMakeLists.txt b/examples/protocols/mqtt/ssl_psk/CMakeLists.txt new file mode 100644 index 0000000..77934fa --- /dev/null +++ b/examples/protocols/mqtt/ssl_psk/CMakeLists.txt @@ -0,0 +1,10 @@ +# The following four lines of boilerplate have to be in your project's CMakeLists +# in this exact order for cmake to work correctly +cmake_minimum_required(VERSION 3.5) + +# (Not part of the boilerplate) +# This example uses an extra component for common functions such as Wi-Fi and Ethernet connection. +set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common) + +include($ENV{IDF_PATH}/tools/cmake/project.cmake) +project(mqtt_ssl_psk) diff --git a/examples/protocols/mqtt/ssl_psk/Makefile b/examples/protocols/mqtt/ssl_psk/Makefile new file mode 100644 index 0000000..24bec17 --- /dev/null +++ b/examples/protocols/mqtt/ssl_psk/Makefile @@ -0,0 +1,9 @@ +# +# This is a project Makefile. It is assumed the directory this Makefile resides in is a +# project subdirectory. +# +PROJECT_NAME := mqtt_ssl_psk + +EXTRA_COMPONENT_DIRS = $(IDF_PATH)/examples/common_components/protocol_examples_common + +include $(IDF_PATH)/make/project.mk diff --git a/examples/protocols/mqtt/ssl_psk/README.md b/examples/protocols/mqtt/ssl_psk/README.md new file mode 100644 index 0000000..c46688f --- /dev/null +++ b/examples/protocols/mqtt/ssl_psk/README.md @@ -0,0 +1,76 @@ +# ESP-MQTT SSL example with PSK verification + +(See the README.md file in the upper level 'examples' directory for more information about examples.) + +This example connects to a local broker configured to PSK authentication + +## How to use example + +### Hardware Required + +This example can be executed on any ESP32 board, the only required interface is WiFi (or ethernet) to connect to a MQTT +broker with preconfigured PSK verification method. + +#### Mosquitto settings +In case of using mosquitto broker, here is how to enable PSK authentication in `mosquitto.config`, +``` +psk_hint hint +psk_file path_to_your_psk_file +allow_anonymous true +``` +Note: Last line enables anonymous mode, as this example does not use mqtt username and password. + +PSK file then has to contain pairs of hints and keys, as shown below: +``` +hint:BAD123 +``` + +Important note: Keys are stored as text hexadecimal values in PSK file, while the example code stores key as plain binary +as required by MQTT API. (See the example source for details: `"BAD123" -> 0xBA, 0xD1, 0x23`) + +### Configure the project + +* Run `make menuconfig` (or `idf.py menuconfig` if using CMake build system) +* Configure Wi-Fi or Ethernet under "Example Connection Configuration" menu. See "Establishing Wi-Fi or Ethernet Connection" section in [examples/protocols/README.md](../../README.md) for more details. +* When using Make build system, set `Default serial port` under `Serial flasher config`. + +### Build and Flash + + +(To exit the serial monitor, type ``Ctrl-]``.) + +See the Getting Started Guide for full steps to configure and use ESP-IDF to build projects. + +## Example Output + +``` +I (2160) example_connect: Ethernet Link Up +I (4650) example_connect: Connected to Ethernet +I (4650) example_connect: IPv4 address: 192.168.0.1 +I (4650) MQTTS_EXAMPLE: [APP] Free memory: 244792 bytes +I (4660) system_api: Base MAC address is not set, read default base MAC address from BLK0 of EFUSE +D (4670) MQTT_CLIENT: MQTT client_id=ESP32_c6B4F8 +D (4680) MQTT_CLIENT: Core selection disabled +I (4680) MQTTS_EXAMPLE: Other event id:7 +D (4680) esp-tls: host:192.168.0.2: strlen 13 +D (4700) esp-tls: ssl psk authentication +D (4700) esp-tls: handshake in progress... +D (4720) MQTT_CLIENT: Transport connected to mqtts://192.168.0.2:8883 +I (4720) MQTT_CLIENT: Sending MQTT CONNECT message, type: 1, id: 0000 +D (4720) MQTT_CLIENT: mqtt_message_receive: first byte: 0x20 +D (4730) MQTT_CLIENT: mqtt_message_receive: read "remaining length" byte: 0x2 +D (4730) MQTT_CLIENT: mqtt_message_receive: total message length: 4 (already read: 2) +D (4740) MQTT_CLIENT: mqtt_message_receive: read_len=2 +D (4750) MQTT_CLIENT: mqtt_message_receive: transport_read():4 4 +D (4750) MQTT_CLIENT: Connected +I (4760) MQTTS_EXAMPLE: MQTT_EVENT_CONNECTED +D (4760) MQTT_CLIENT: mqtt_enqueue id: 4837, type=8 successful +D (4770) OUTBOX: ENQUEUE msgid=4837, msg_type=8, len=18, size=18 +D (4770) MQTT_CLIENT: Sent subscribe topic=/topic/qos0, id: 4837, type=8 successful +I (4780) MQTTS_EXAMPLE: sent subscribe successful, msg_id=4837 +D (4790) MQTT_CLIENT: mqtt_enqueue id: 58982, type=8 successful +D (4790) OUTBOX: ENQUEUE msgid=58982, msg_type=8, len=18, size=36 +D (4800) MQTT_CLIENT: Sent subscribe topic=/topic/qos1, id: 58982, type=8 successful +I (4810) MQTTS_EXAMPLE: sent subscribe successful, msg_id=58982 +``` + diff --git a/examples/protocols/mqtt/ssl_psk/main/CMakeLists.txt b/examples/protocols/mqtt/ssl_psk/main/CMakeLists.txt new file mode 100644 index 0000000..6b03500 --- /dev/null +++ b/examples/protocols/mqtt/ssl_psk/main/CMakeLists.txt @@ -0,0 +1,4 @@ +set(COMPONENT_SRCS "app_main.c") +set(COMPONENT_ADD_INCLUDEDIRS ".") + +register_component() diff --git a/examples/protocols/mqtt/ssl_psk/main/app_main.c b/examples/protocols/mqtt/ssl_psk/main/app_main.c new file mode 100644 index 0000000..7a08d17 --- /dev/null +++ b/examples/protocols/mqtt/ssl_psk/main/app_main.c @@ -0,0 +1,141 @@ +/* MQTT over SSL Example + + This example 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 +#include +#include +#include "esp_wifi.h" +#include "esp_system.h" +#include "nvs_flash.h" +#include "esp_event.h" +#include "tcpip_adapter.h" +#include "protocol_examples_common.h" + +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "freertos/semphr.h" +#include "freertos/queue.h" + +#include "lwip/sockets.h" +#include "lwip/dns.h" +#include "lwip/netdb.h" + +#include "esp_log.h" +#include "mqtt_client.h" +#include "esp_tls.h" + +/* + * Add here URI of mqtt broker which supports PSK authentication + */ +#define EXAMPLE_BROKER_URI "mqtts://192.168.0.2" + +static const char *TAG = "MQTTS_EXAMPLE"; + +/* + * Define psk key and hint as defined in mqtt broker + * example for mosquitto server, content of psk_file: + * hint:BAD123 + * + */ +static const uint8_t s_key[] = { 0xBA, 0xD1, 0x23 }; + +static const psk_hint_key_t psk_hint_key = { + .key = s_key, + .key_size = sizeof(s_key), + .hint = "hint" + }; + +static esp_err_t mqtt_event_handler(esp_mqtt_event_handle_t event) +{ + esp_mqtt_client_handle_t client = event->client; + int msg_id; + // your_context_t *context = event->context; + switch (event->event_id) { + case MQTT_EVENT_CONNECTED: + ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED"); + msg_id = esp_mqtt_client_subscribe(client, "/topic/qos0", 0); + ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id); + + msg_id = esp_mqtt_client_subscribe(client, "/topic/qos1", 1); + ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id); + + msg_id = esp_mqtt_client_unsubscribe(client, "/topic/qos1"); + ESP_LOGI(TAG, "sent unsubscribe successful, msg_id=%d", msg_id); + break; + case MQTT_EVENT_DISCONNECTED: + ESP_LOGI(TAG, "MQTT_EVENT_DISCONNECTED"); + break; + + case MQTT_EVENT_SUBSCRIBED: + ESP_LOGI(TAG, "MQTT_EVENT_SUBSCRIBED, msg_id=%d", event->msg_id); + msg_id = esp_mqtt_client_publish(client, "/topic/qos0", "data", 0, 0, 0); + ESP_LOGI(TAG, "sent publish successful, msg_id=%d", msg_id); + break; + case MQTT_EVENT_UNSUBSCRIBED: + ESP_LOGI(TAG, "MQTT_EVENT_UNSUBSCRIBED, msg_id=%d", event->msg_id); + break; + case MQTT_EVENT_PUBLISHED: + ESP_LOGI(TAG, "MQTT_EVENT_PUBLISHED, msg_id=%d", event->msg_id); + break; + case MQTT_EVENT_DATA: + ESP_LOGI(TAG, "MQTT_EVENT_DATA"); + printf("TOPIC=%.*s\r\n", event->topic_len, event->topic); + printf("DATA=%.*s\r\n", event->data_len, event->data); + break; + case MQTT_EVENT_ERROR: + ESP_LOGI(TAG, "MQTT_EVENT_ERROR"); + break; + default: + ESP_LOGI(TAG, "Other event id:%d", event->event_id); + break; + } + return ESP_OK; +} + + +static void mqtt_app_start(void) +{ + const esp_mqtt_client_config_t mqtt_cfg = { + .uri = EXAMPLE_BROKER_URI, + .event_handle = mqtt_event_handler, + .psk_hint_key = &psk_hint_key, + }; + + ESP_LOGI(TAG, "[APP] Free memory: %d bytes", esp_get_free_heap_size()); + esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg); + esp_mqtt_client_start(client); +} + +void app_main(void) +{ + ESP_LOGI(TAG, "[APP] Startup.."); + ESP_LOGI(TAG, "[APP] Free memory: %d bytes", esp_get_free_heap_size()); + ESP_LOGI(TAG, "[APP] IDF version: %s", esp_get_idf_version()); + + esp_log_level_set("*", ESP_LOG_INFO); + esp_log_level_set("MQTT_CLIENT", ESP_LOG_VERBOSE); + esp_log_level_set("TRANSPORT_TCP", ESP_LOG_VERBOSE); + esp_log_level_set("TRANSPORT_SSL", ESP_LOG_VERBOSE); + esp_log_level_set("esp-tls", ESP_LOG_VERBOSE); + esp_log_level_set("TRANSPORT", ESP_LOG_VERBOSE); + esp_log_level_set("OUTBOX", ESP_LOG_VERBOSE); + + ESP_ERROR_CHECK(nvs_flash_init()); + tcpip_adapter_init(); + ESP_ERROR_CHECK(esp_event_loop_create_default()); + + /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig. + * Read "Establishing Wi-Fi or Ethernet Connection" section in + * examples/protocols/README.md for more information about this function. + */ + ESP_ERROR_CHECK(example_connect()); + + mqtt_app_start(); +} diff --git a/examples/protocols/mqtt/ssl_psk/main/component.mk b/examples/protocols/mqtt/ssl_psk/main/component.mk new file mode 100644 index 0000000..e69de29 diff --git a/examples/protocols/mqtt/ssl_psk/sdkconfig.defaults b/examples/protocols/mqtt/ssl_psk/sdkconfig.defaults new file mode 100644 index 0000000..1df83e8 --- /dev/null +++ b/examples/protocols/mqtt/ssl_psk/sdkconfig.defaults @@ -0,0 +1 @@ +CONFIG_ESP_TLS_PSK_VERIFICATION=y From e6df783596a3c2e6c3a0ff9d5782761fefa2d783 Mon Sep 17 00:00:00 2001 From: "Michael (XIAO Xufeng)" Date: Thu, 29 Aug 2019 17:54:14 +0800 Subject: [PATCH 029/231] ci: limit example test to ESP32s --- components/mqtt/weekend_test/mqtt_publish_test.py | 3 ++- examples/protocols/mqtt/ssl/mqtt_ssl_example_test.py | 3 ++- examples/protocols/mqtt/tcp/mqtt_tcp_example_test.py | 3 ++- examples/protocols/mqtt/ws/mqtt_ws_example_test.py | 3 ++- examples/protocols/mqtt/wss/mqtt_wss_example_test.py | 3 ++- 5 files changed, 10 insertions(+), 5 deletions(-) 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) diff --git a/examples/protocols/mqtt/ssl/mqtt_ssl_example_test.py b/examples/protocols/mqtt/ssl/mqtt_ssl_example_test.py index 7e66482..87931e8 100644 --- a/examples/protocols/mqtt/ssl/mqtt_ssl_example_test.py +++ b/examples/protocols/mqtt/ssl/mqtt_ssl_example_test.py @@ -11,6 +11,7 @@ from threading import Thread, Event 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, @@ -64,7 +65,7 @@ def test_examples_protocol_mqtt_ssl(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_ssl", "examples/protocols/mqtt/ssl") + dut1 = env.get_dut("mqtt_ssl", "examples/protocols/mqtt/ssl", dut_class=ESP32DUT) # check and log bin size binary_file = os.path.join(dut1.app.binary_path, "mqtt_ssl.bin") bin_size = os.path.getsize(binary_file) diff --git a/examples/protocols/mqtt/tcp/mqtt_tcp_example_test.py b/examples/protocols/mqtt/tcp/mqtt_tcp_example_test.py index 05e5142..4e39705 100644 --- a/examples/protocols/mqtt/tcp/mqtt_tcp_example_test.py +++ b/examples/protocols/mqtt/tcp/mqtt_tcp_example_test.py @@ -9,6 +9,7 @@ import time 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, @@ -75,7 +76,7 @@ def test_examples_protocol_mqtt_qos1(env, extra_data): 3. Test evaluates that qos1 message is queued and removed from queued after ACK received 4. Test the broker received the same message id evaluated in step 3 """ - dut1 = env.get_dut("mqtt_tcp", "examples/protocols/mqtt/tcp") + dut1 = env.get_dut("mqtt_tcp", "examples/protocols/mqtt/tcp", dut_class=ESP32DUT) # check and log bin size binary_file = os.path.join(dut1.app.binary_path, "mqtt_tcp.bin") bin_size = os.path.getsize(binary_file) diff --git a/examples/protocols/mqtt/ws/mqtt_ws_example_test.py b/examples/protocols/mqtt/ws/mqtt_ws_example_test.py index 0578876..85074c2 100644 --- a/examples/protocols/mqtt/ws/mqtt_ws_example_test.py +++ b/examples/protocols/mqtt/ws/mqtt_ws_example_test.py @@ -10,6 +10,7 @@ from threading import Thread, Event try: import IDF + from IDF.IDFDUT import ESP32DUT except Exception: # this is a test case write with tiny-test-fw. # to run test cases outside tiny-test-fw, @@ -62,7 +63,7 @@ def test_examples_protocol_mqtt_ws(env, extra_data): 3. Test evaluates it received correct qos0 message 4. Test ESP32 client received correct qos0 message """ - dut1 = env.get_dut("mqtt_websocket", "examples/protocols/mqtt/ws") + dut1 = env.get_dut("mqtt_websocket", "examples/protocols/mqtt/ws", dut_class=ESP32DUT) # check and log bin size binary_file = os.path.join(dut1.app.binary_path, "mqtt_websocket.bin") bin_size = os.path.getsize(binary_file) diff --git a/examples/protocols/mqtt/wss/mqtt_wss_example_test.py b/examples/protocols/mqtt/wss/mqtt_wss_example_test.py index 58ce560..5e0e143 100644 --- a/examples/protocols/mqtt/wss/mqtt_wss_example_test.py +++ b/examples/protocols/mqtt/wss/mqtt_wss_example_test.py @@ -11,6 +11,7 @@ from threading import Thread, Event 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, @@ -63,7 +64,7 @@ def test_examples_protocol_mqtt_wss(env, extra_data): 3. Test evaluates it received correct qos0 message 4. Test ESP32 client received correct qos0 message """ - dut1 = env.get_dut("mqtt_websocket_secure", "examples/protocols/mqtt/wss") + dut1 = env.get_dut("mqtt_websocket_secure", "examples/protocols/mqtt/wss", dut_class=ESP32DUT) # check and log bin size binary_file = os.path.join(dut1.app.binary_path, "mqtt_websocket_secure.bin") bin_size = os.path.getsize(binary_file) From 43bc4ca6224ebf9cb9c4c51e175e7565cb045758 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Fri, 27 Sep 2019 14:44:55 +0200 Subject: [PATCH 030/231] 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 90f4b153a5ed9ae4d85536d30915dfba94b5c395 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Wed, 2 Oct 2019 16:22:50 +0200 Subject: [PATCH 031/231] update mqtt weekend test to use mbedtls asymetric buffer --- examples/protocols/mqtt/publish_test/sdkconfig.ci | 3 +++ 1 file changed, 3 insertions(+) diff --git a/examples/protocols/mqtt/publish_test/sdkconfig.ci b/examples/protocols/mqtt/publish_test/sdkconfig.ci index 4ff3a8d..34319fc 100644 --- a/examples/protocols/mqtt/publish_test/sdkconfig.ci +++ b/examples/protocols/mqtt/publish_test/sdkconfig.ci @@ -3,3 +3,6 @@ CONFIG_EXAMPLE_BROKER_TCP_URI="mqtt://${EXAMPLE_MQTT_BROKER_TCP}" CONFIG_EXAMPLE_BROKER_WS_URI="ws://${EXAMPLE_MQTT_BROKER_WS}/ws" CONFIG_EXAMPLE_BROKER_WSS_URI="wss://${EXAMPLE_MQTT_BROKER_WSS}/ws" CONFIG_EXAMPLE_BROKER_CERTIFICATE_OVERRIDE="${EXAMPLE_MQTT_BROKER_CERTIFICATE}" +CONFIG_MBEDTLS_ASYMMETRIC_CONTENT_LEN=y +CONFIG_MBEDTLS_SSL_IN_CONTENT_LEN=16384 +CONFIG_MBEDTLS_SSL_OUT_CONTENT_LEN=16384 \ No newline at end of file From d67b4ec8b88cca1577486546a39d4b40ebab72d9 Mon Sep 17 00:00:00 2001 From: Marius Vikhammer Date: Fri, 27 Sep 2019 09:35:26 +0800 Subject: [PATCH 032/231] 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 --- .../mqtt/weekend_test/mqtt_publish_test.py | 3 +- docs/en/api-reference/protocols/mqtt.rst | 28 +++++++++---------- .../mqtt/publish_test/CMakeLists.txt | 2 +- .../mqtt/publish_test/main/Kconfig.projbuild | 8 +++--- .../mqtt/publish_test/main/component.mk | 2 +- ...t_eclipse_org.pem => mqtt_eclipse_org.pem} | 0 .../mqtt/publish_test/main/publish_test.c | 8 +++--- examples/protocols/mqtt/ssl/CMakeLists.txt | 2 +- examples/protocols/mqtt/ssl/README.md | 8 +++--- .../protocols/mqtt/ssl/main/Kconfig.projbuild | 2 +- examples/protocols/mqtt/ssl/main/app_main.c | 8 +++--- examples/protocols/mqtt/ssl/main/component.mk | 2 +- ...t_eclipse_org.pem => mqtt_eclipse_org.pem} | 0 .../mqtt/ssl/mqtt_ssl_example_test.py | 2 +- .../protocols/mqtt/tcp/main/Kconfig.projbuild | 2 +- examples/protocols/mqtt/ws/README.md | 2 +- .../protocols/mqtt/ws/main/Kconfig.projbuild | 2 +- .../protocols/mqtt/ws/mqtt_ws_example_test.py | 1 - examples/protocols/mqtt/wss/CMakeLists.txt | 2 +- examples/protocols/mqtt/wss/README.md | 10 +++---- .../protocols/mqtt/wss/main/Kconfig.projbuild | 2 +- examples/protocols/mqtt/wss/main/app_main.c | 8 +++--- examples/protocols/mqtt/wss/main/component.mk | 2 +- ...t_eclipse_org.pem => mqtt_eclipse_org.pem} | 0 .../mqtt/wss/mqtt_wss_example_test.py | 2 +- 25 files changed, 53 insertions(+), 55 deletions(-) rename examples/protocols/mqtt/publish_test/main/{iot_eclipse_org.pem => mqtt_eclipse_org.pem} (100%) rename examples/protocols/mqtt/ssl/main/{iot_eclipse_org.pem => mqtt_eclipse_org.pem} (100%) rename examples/protocols/mqtt/wss/main/{iot_eclipse_org.pem => mqtt_eclipse_org.pem} (100%) 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) diff --git a/docs/en/api-reference/protocols/mqtt.rst b/docs/en/api-reference/protocols/mqtt.rst index cb95675..80d6270 100644 --- a/docs/en/api-reference/protocols/mqtt.rst +++ b/docs/en/api-reference/protocols/mqtt.rst @@ -32,30 +32,30 @@ URI - Curently support ``mqtt``, ``mqtts``, ``ws``, ``wss`` schemes - MQTT over TCP samples: - - ``mqtt://iot.eclipse.org``: MQTT over TCP, default port 1883: - - ``mqtt://iot.eclipse.org:1884`` MQTT over TCP, port 1884: - - ``mqtt://username:password@iot.eclipse.org:1884`` MQTT over TCP, + - ``mqtt://mqtt.eclipse.org``: MQTT over TCP, default port 1883: + - ``mqtt://mqtt.eclipse.org:1884`` MQTT over TCP, port 1884: + - ``mqtt://username:password@mqtt.eclipse.org:1884`` MQTT over TCP, port 1884, with username and password - MQTT over SSL samples: - - ``mqtts://iot.eclipse.org``: MQTT over SSL, port 8883 - - ``mqtts://iot.eclipse.org:8884``: MQTT over SSL, port 8884 + - ``mqtts://mqtt.eclipse.org``: MQTT over SSL, port 8883 + - ``mqtts://mqtt.eclipse.org:8884``: MQTT over SSL, port 8884 - MQTT over Websocket samples: - - ``ws://iot.eclipse.org:80/ws`` + - ``ws://mqtt.eclipse.org:80/mqtt`` - MQTT over Websocket Secure samples: - - ``wss://iot.eclipse.org:443/ws`` + - ``wss://mqtt.eclipse.org:443/mqtt`` - Minimal configurations: .. code:: c const esp_mqtt_client_config_t mqtt_cfg = { - .uri = "mqtt://iot.eclipse.org", + .uri = "mqtt://mqtt.eclipse.org", // .user_context = (void *)your_context }; esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg); @@ -71,25 +71,25 @@ URI .. code:: c const esp_mqtt_client_config_t mqtt_cfg = { - .uri = "mqtt://iot.eclipse.org:1234", + .uri = "mqtt://mqtt.eclipse.org:1234", .port = 4567, }; - //MQTT client will connect to iot.eclipse.org using port 4567 + //MQTT client will connect to mqtt.eclipse.org using port 4567 SSL ^^^ -- Get certificate from server, example: ``iot.eclipse.org`` - ``openssl s_client -showcerts -connect iot.eclipse.org:8883 /dev/null|openssl x509 -outform PEM >iot_eclipse_org.pem`` +- Get certificate from server, example: ``mqtt.eclipse.org`` + ``openssl s_client -showcerts -connect mqtt.eclipse.org:8883 /dev/null|openssl x509 -outform PEM >mqtt_eclipse_org.pem`` - Check the sample application: ``examples/mqtt_ssl`` - Configuration: .. code:: cpp const esp_mqtt_client_config_t mqtt_cfg = { - .uri = "mqtts://iot.eclipse.org:8883", + .uri = "mqtts://mqtt.eclipse.org:8883", .event_handle = mqtt_event_handler, - .cert_pem = (const char *)iot_eclipse_org_pem_start, + .cert_pem = (const char *)mqtt_eclipse_org_pem_start, }; For more options on ``esp_mqtt_client_config_t``, please refer to API reference below diff --git a/examples/protocols/mqtt/publish_test/CMakeLists.txt b/examples/protocols/mqtt/publish_test/CMakeLists.txt index e494bbb..c7d6c8a 100644 --- a/examples/protocols/mqtt/publish_test/CMakeLists.txt +++ b/examples/protocols/mqtt/publish_test/CMakeLists.txt @@ -11,4 +11,4 @@ include($ENV{IDF_PATH}/tools/cmake/project.cmake) project(mqtt_publish) -target_add_binary_data(mqtt_publish.elf "main/iot_eclipse_org.pem" TEXT) +target_add_binary_data(mqtt_publish.elf "main/mqtt_eclipse_org.pem" TEXT) diff --git a/examples/protocols/mqtt/publish_test/main/Kconfig.projbuild b/examples/protocols/mqtt/publish_test/main/Kconfig.projbuild index 8360e57..bf41974 100644 --- a/examples/protocols/mqtt/publish_test/main/Kconfig.projbuild +++ b/examples/protocols/mqtt/publish_test/main/Kconfig.projbuild @@ -2,25 +2,25 @@ menu "Example Configuration" config EXAMPLE_BROKER_SSL_URI string "Broker SSL URL" - default "mqtts://iot.eclipse.org:8883" + default "mqtts://mqtt.eclipse.org:8883" help URL of an mqtt broker for ssl transport config EXAMPLE_BROKER_TCP_URI string "Broker TCP URL" - default "mqtt://iot.eclipse.org:1883" + default "mqtt://mqtt.eclipse.org:1883" help URL of an mqtt broker for tcp transport config EXAMPLE_BROKER_WS_URI string "Broker WS URL" - default "ws://iot.eclipse.org:80/ws" + default "ws://mqtt.eclipse.org:80/mqtt" help URL of an mqtt broker for ws transport config EXAMPLE_BROKER_WSS_URI string "Broker WSS URL" - default "wss://iot.eclipse.org:443/ws" + default "wss://mqtt.eclipse.org:443/mqtt" help URL of an mqtt broker for wss transport diff --git a/examples/protocols/mqtt/publish_test/main/component.mk b/examples/protocols/mqtt/publish_test/main/component.mk index 797c4a1..597752f 100644 --- a/examples/protocols/mqtt/publish_test/main/component.mk +++ b/examples/protocols/mqtt/publish_test/main/component.mk @@ -1 +1 @@ -COMPONENT_EMBED_TXTFILES := iot_eclipse_org.pem +COMPONENT_EMBED_TXTFILES := mqtt_eclipse_org.pem diff --git a/examples/protocols/mqtt/publish_test/main/iot_eclipse_org.pem b/examples/protocols/mqtt/publish_test/main/mqtt_eclipse_org.pem similarity index 100% rename from examples/protocols/mqtt/publish_test/main/iot_eclipse_org.pem rename to examples/protocols/mqtt/publish_test/main/mqtt_eclipse_org.pem diff --git a/examples/protocols/mqtt/publish_test/main/publish_test.c b/examples/protocols/mqtt/publish_test/main/publish_test.c index 9183bbb..918f9a1 100644 --- a/examples/protocols/mqtt/publish_test/main/publish_test.c +++ b/examples/protocols/mqtt/publish_test/main/publish_test.c @@ -46,11 +46,11 @@ static int qos_test = 0; #if CONFIG_EXAMPLE_BROKER_CERTIFICATE_OVERRIDDEN == 1 -static const uint8_t iot_eclipse_org_pem_start[] = "-----BEGIN CERTIFICATE-----\n" CONFIG_EXAMPLE_BROKER_CERTIFICATE_OVERRIDE "\n-----END CERTIFICATE-----"; +static const uint8_t mqtt_eclipse_org_pem_start[] = "-----BEGIN CERTIFICATE-----\n" CONFIG_EXAMPLE_BROKER_CERTIFICATE_OVERRIDE "\n-----END CERTIFICATE-----"; #else -extern const uint8_t iot_eclipse_org_pem_start[] asm("_binary_iot_eclipse_org_pem_start"); +extern const uint8_t mqtt_eclipse_org_pem_start[] asm("_binary_mqtt_eclipse_org_pem_start"); #endif -extern const uint8_t iot_eclipse_org_pem_end[] asm("_binary_iot_eclipse_org_pem_end"); +extern const uint8_t mqtt_eclipse_org_pem_end[] asm("_binary_mqtt_eclipse_org_pem_end"); static esp_err_t mqtt_event_handler(esp_mqtt_event_handle_t event) { @@ -127,7 +127,7 @@ static void mqtt_app_start(void) mqtt_event_group = xEventGroupCreate(); const esp_mqtt_client_config_t mqtt_cfg = { .event_handle = mqtt_event_handler, - .cert_pem = (const char *)iot_eclipse_org_pem_start, + .cert_pem = (const char *)mqtt_eclipse_org_pem_start, }; ESP_LOGI(TAG, "[APP] Free memory: %d bytes", esp_get_free_heap_size()); diff --git a/examples/protocols/mqtt/ssl/CMakeLists.txt b/examples/protocols/mqtt/ssl/CMakeLists.txt index a265066..517c022 100644 --- a/examples/protocols/mqtt/ssl/CMakeLists.txt +++ b/examples/protocols/mqtt/ssl/CMakeLists.txt @@ -10,4 +10,4 @@ set(SUPPORTED_TARGETS esp32) include($ENV{IDF_PATH}/tools/cmake/project.cmake) project(mqtt_ssl) -target_add_binary_data(mqtt_ssl.elf "main/iot_eclipse_org.pem" TEXT) +target_add_binary_data(mqtt_ssl.elf "main/mqtt_eclipse_org.pem" TEXT) diff --git a/examples/protocols/mqtt/ssl/README.md b/examples/protocols/mqtt/ssl/README.md index e269372..aa96a68 100644 --- a/examples/protocols/mqtt/ssl/README.md +++ b/examples/protocols/mqtt/ssl/README.md @@ -2,7 +2,7 @@ (See the README.md file in the upper level 'examples' directory for more information about examples.) -This example connects to the broker iot.eclipse.org using ssl transport and as a demonstration subscribes/unsubscribes and send a message on certain topic. +This example connects to the broker mqtt.eclipse.org using ssl transport and as a demonstration subscribes/unsubscribes and send a message on certain topic. (Please note that the public broker is maintained by the community so may not be always available, for details please see this [disclaimer](https://iot.eclipse.org/getting-started/#sandboxes)) It uses ESP-MQTT library which implements mqtt client to connect to mqtt broker. @@ -19,13 +19,13 @@ This example can be executed on any ESP32 board, the only required interface is * Configure Wi-Fi or Ethernet under "Example Connection Configuration" menu. See "Establishing Wi-Fi or Ethernet Connection" section in [examples/protocols/README.md](../../README.md) for more details. * When using Make build system, set `Default serial port` under `Serial flasher config`. -PEM certificate for this example could be extracted from an openssl `s_client` command connecting to iot.eclipse.org. +PEM certificate for this example could be extracted from an openssl `s_client` command connecting to mqtt.eclipse.org. In case a host operating system has `openssl` and `sed` packages installed, one could execute the following command to download and save the root certificate to a file (Note for Windows users: Both Linux like environment or Windows native packages may be used). ``` -echo "" | openssl s_client -showcerts -connect iot.eclipse.org:8883 | sed -n "1,/Root/d; /BEGIN/,/END/p" | openssl x509 -outform PEM >iot_eclipse_org.pem +echo "" | openssl s_client -showcerts -connect mqtt.eclipse.org:8883 | sed -n "1,/Root/d; /BEGIN/,/END/p" | openssl x509 -outform PEM >mqtt_eclipse_org.pem ``` Please note that this is not a general command for downloading a root certificate for an arbitrary host; -this command works with iot.eclipse.org as the site provides root certificate in the chain, which then could be extracted +this command works with mqtt.eclipse.org as the site provides root certificate in the chain, which then could be extracted with text operation. ### Build and Flash diff --git a/examples/protocols/mqtt/ssl/main/Kconfig.projbuild b/examples/protocols/mqtt/ssl/main/Kconfig.projbuild index e318733..3c44294 100644 --- a/examples/protocols/mqtt/ssl/main/Kconfig.projbuild +++ b/examples/protocols/mqtt/ssl/main/Kconfig.projbuild @@ -2,7 +2,7 @@ menu "Example Configuration" config BROKER_URI string "Broker URL" - default "mqtts://iot.eclipse.org:8883" + default "mqtts://mqtt.eclipse.org:8883" help URL of an mqtt broker which this example connects to. diff --git a/examples/protocols/mqtt/ssl/main/app_main.c b/examples/protocols/mqtt/ssl/main/app_main.c index fd0a49d..cf0df58 100644 --- a/examples/protocols/mqtt/ssl/main/app_main.c +++ b/examples/protocols/mqtt/ssl/main/app_main.c @@ -35,11 +35,11 @@ static const char *TAG = "MQTTS_EXAMPLE"; #if CONFIG_BROKER_CERTIFICATE_OVERRIDDEN == 1 -static const uint8_t iot_eclipse_org_pem_start[] = "-----BEGIN CERTIFICATE-----\n" CONFIG_BROKER_CERTIFICATE_OVERRIDE "\n-----END CERTIFICATE-----"; +static const uint8_t mqtt_eclipse_org_pem_start[] = "-----BEGIN CERTIFICATE-----\n" CONFIG_BROKER_CERTIFICATE_OVERRIDE "\n-----END CERTIFICATE-----"; #else -extern const uint8_t iot_eclipse_org_pem_start[] asm("_binary_iot_eclipse_org_pem_start"); +extern const uint8_t mqtt_eclipse_org_pem_start[] asm("_binary_mqtt_eclipse_org_pem_start"); #endif -extern const uint8_t iot_eclipse_org_pem_end[] asm("_binary_iot_eclipse_org_pem_end"); +extern const uint8_t mqtt_eclipse_org_pem_end[] asm("_binary_mqtt_eclipse_org_pem_end"); static esp_err_t mqtt_event_handler_cb(esp_mqtt_event_handle_t event) { @@ -101,7 +101,7 @@ static void mqtt_app_start(void) { const esp_mqtt_client_config_t mqtt_cfg = { .uri = CONFIG_BROKER_URI, - .cert_pem = (const char *)iot_eclipse_org_pem_start, + .cert_pem = (const char *)mqtt_eclipse_org_pem_start, }; ESP_LOGI(TAG, "[APP] Free memory: %d bytes", esp_get_free_heap_size()); diff --git a/examples/protocols/mqtt/ssl/main/component.mk b/examples/protocols/mqtt/ssl/main/component.mk index 797c4a1..597752f 100644 --- a/examples/protocols/mqtt/ssl/main/component.mk +++ b/examples/protocols/mqtt/ssl/main/component.mk @@ -1 +1 @@ -COMPONENT_EMBED_TXTFILES := iot_eclipse_org.pem +COMPONENT_EMBED_TXTFILES := mqtt_eclipse_org.pem diff --git a/examples/protocols/mqtt/ssl/main/iot_eclipse_org.pem b/examples/protocols/mqtt/ssl/main/mqtt_eclipse_org.pem similarity index 100% rename from examples/protocols/mqtt/ssl/main/iot_eclipse_org.pem rename to examples/protocols/mqtt/ssl/main/mqtt_eclipse_org.pem diff --git a/examples/protocols/mqtt/ssl/mqtt_ssl_example_test.py b/examples/protocols/mqtt/ssl/mqtt_ssl_example_test.py index 87931e8..6c255c6 100644 --- a/examples/protocols/mqtt/ssl/mqtt_ssl_example_test.py +++ b/examples/protocols/mqtt/ssl/mqtt_ssl_example_test.py @@ -88,7 +88,7 @@ def test_examples_protocol_mqtt_ssl(env, extra_data): client.on_message = on_message client.tls_set(None, None, - None, cert_reqs=ssl.CERT_NONE, tls_version=ssl.PROTOCOL_TLSv1, ciphers=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_url, broker_port, 60) diff --git a/examples/protocols/mqtt/tcp/main/Kconfig.projbuild b/examples/protocols/mqtt/tcp/main/Kconfig.projbuild index fbd4810..fc6e8a1 100644 --- a/examples/protocols/mqtt/tcp/main/Kconfig.projbuild +++ b/examples/protocols/mqtt/tcp/main/Kconfig.projbuild @@ -2,7 +2,7 @@ menu "Example Configuration" config BROKER_URL string "Broker URL" - default "mqtt://iot.eclipse.org" + default "mqtt://mqtt.eclipse.org" help URL of the broker to connect to diff --git a/examples/protocols/mqtt/ws/README.md b/examples/protocols/mqtt/ws/README.md index c0526b0..3b2a05a 100644 --- a/examples/protocols/mqtt/ws/README.md +++ b/examples/protocols/mqtt/ws/README.md @@ -2,7 +2,7 @@ (See the README.md file in the upper level 'examples' directory for more information about examples.) -This example connects to the broker iot.eclipse.org over web sockets as a demonstration subscribes/unsubscribes and send a message on certain topic. +This example connects to the broker mqtt.eclipse.org over web sockets as a demonstration subscribes/unsubscribes and send a message on certain topic. (Please note that the public broker is maintained by the community so may not be always available, for details please see this [disclaimer](https://iot.eclipse.org/getting-started/#sandboxes)) It uses ESP-MQTT library which implements mqtt client to connect to mqtt broker. diff --git a/examples/protocols/mqtt/ws/main/Kconfig.projbuild b/examples/protocols/mqtt/ws/main/Kconfig.projbuild index 46f3ad5..c298f5d 100644 --- a/examples/protocols/mqtt/ws/main/Kconfig.projbuild +++ b/examples/protocols/mqtt/ws/main/Kconfig.projbuild @@ -2,7 +2,7 @@ menu "Example Configuration" config BROKER_URI string "Broker URL" - default "ws://iot.eclipse.org:80/ws" + default "ws://mqtt.eclipse.org:80/mqtt" help URL of an mqtt broker which this example connects to. diff --git a/examples/protocols/mqtt/ws/mqtt_ws_example_test.py b/examples/protocols/mqtt/ws/mqtt_ws_example_test.py index 85074c2..19ed395 100644 --- a/examples/protocols/mqtt/ws/mqtt_ws_example_test.py +++ b/examples/protocols/mqtt/ws/mqtt_ws_example_test.py @@ -83,7 +83,6 @@ def test_examples_protocol_mqtt_ws(env, extra_data): client = mqtt.Client(transport="websockets") client.on_connect = on_connect client.on_message = on_message - client.ws_set_options(path="/ws", headers=None) print("Connecting...") client.connect(broker_url, broker_port, 60) except Exception: diff --git a/examples/protocols/mqtt/wss/CMakeLists.txt b/examples/protocols/mqtt/wss/CMakeLists.txt index 6519f02..96fe21d 100644 --- a/examples/protocols/mqtt/wss/CMakeLists.txt +++ b/examples/protocols/mqtt/wss/CMakeLists.txt @@ -10,4 +10,4 @@ set(SUPPORTED_TARGETS esp32) include($ENV{IDF_PATH}/tools/cmake/project.cmake) project(mqtt_websocket_secure) -target_add_binary_data(mqtt_websocket_secure.elf "main/iot_eclipse_org.pem" TEXT) +target_add_binary_data(mqtt_websocket_secure.elf "main/mqtt_eclipse_org.pem" TEXT) diff --git a/examples/protocols/mqtt/wss/README.md b/examples/protocols/mqtt/wss/README.md index 9851d54..c07477f 100644 --- a/examples/protocols/mqtt/wss/README.md +++ b/examples/protocols/mqtt/wss/README.md @@ -1,7 +1,7 @@ # ESP-MQTT MQTT over WSS Sample application (See the README.md file in the upper level 'examples' directory for more information about examples.) -This example connects to the broker iot.eclipse.org over secure websockets and as a demonstration subscribes/unsubscribes and send a message on certain topic. +This example connects to the broker mqtt.eclipse.org over secure websockets and as a demonstration subscribes/unsubscribes and send a message on certain topic. (Please note that the public broker is maintained by the community so may not be always available, for details please see this [disclaimer](https://iot.eclipse.org/getting-started/#sandboxes)) It uses ESP-MQTT library which implements mqtt client to connect to mqtt broker. @@ -18,15 +18,15 @@ This example can be executed on any ESP32 board, the only required interface is * Configure Wi-Fi or Ethernet under "Example Connection Configuration" menu. See "Establishing Wi-Fi or Ethernet Connection" section in [examples/protocols/README.md](../../README.md) for more details. * When using Make build system, set `Default serial port` under `Serial flasher config`. -Note how to create a PEM certificate for iot.eclipse.org: +Note how to create a PEM certificate for mqtt.eclipse.org: -PEM certificate for this example could be extracted from an openssl `s_client` command connecting to iot.eclipse.org. +PEM certificate for this example could be extracted from an openssl `s_client` command connecting to mqtt.eclipse.org. In case a host operating system has `openssl` and `sed` packages installed, one could execute the following command to download and save the root certificate to a file (Note for Windows users: Both Linux like environment or Windows native packages may be used). ``` -echo "" | openssl s_client -showcerts -connect iot.eclipse.org:443 | sed -n "1,/Root/d; /BEGIN/,/END/p" | openssl x509 -outform PEM >iot_eclipse_org.pem +echo "" | openssl s_client -showcerts -connect mqtt.eclipse.org:443 | sed -n "1,/Root/d; /BEGIN/,/END/p" | openssl x509 -outform PEM >mqtt_eclipse_org.pem ``` Please note that this is not a general command for downloading a root certificate for an arbitrary host; -this command works with iot.eclipse.org as the site provides root certificate in the chain, which then could be extracted +this command works with mqtt.eclipse.org as the site provides root certificate in the chain, which then could be extracted with text operation. ### Build and Flash diff --git a/examples/protocols/mqtt/wss/main/Kconfig.projbuild b/examples/protocols/mqtt/wss/main/Kconfig.projbuild index d3f7bf5..5e43c89 100644 --- a/examples/protocols/mqtt/wss/main/Kconfig.projbuild +++ b/examples/protocols/mqtt/wss/main/Kconfig.projbuild @@ -2,7 +2,7 @@ menu "Example Configuration" config BROKER_URI string "Broker URL" - default "wss://iot.eclipse.org:443/ws" + default "wss://mqtt.eclipse.org:443/mqtt" help URL of an mqtt broker which this example connects to. diff --git a/examples/protocols/mqtt/wss/main/app_main.c b/examples/protocols/mqtt/wss/main/app_main.c index a9de244..529e960 100644 --- a/examples/protocols/mqtt/wss/main/app_main.c +++ b/examples/protocols/mqtt/wss/main/app_main.c @@ -33,11 +33,11 @@ static const char *TAG = "MQTTWSS_EXAMPLE"; #if CONFIG_BROKER_CERTIFICATE_OVERRIDDEN == 1 -static const uint8_t iot_eclipse_org_pem_start[] = "-----BEGIN CERTIFICATE-----\n" CONFIG_BROKER_CERTIFICATE_OVERRIDE "\n-----END CERTIFICATE-----"; +static const uint8_t mqtt_eclipse_org_pem_start[] = "-----BEGIN CERTIFICATE-----\n" CONFIG_BROKER_CERTIFICATE_OVERRIDE "\n-----END CERTIFICATE-----"; #else -extern const uint8_t iot_eclipse_org_pem_start[] asm("_binary_iot_eclipse_org_pem_start"); +extern const uint8_t mqtt_eclipse_org_pem_start[] asm("_binary_mqtt_eclipse_org_pem_start"); #endif -extern const uint8_t iot_eclipse_org_pem_end[] asm("_binary_iot_eclipse_org_pem_end"); +extern const uint8_t mqtt_eclipse_org_pem_end[] asm("_binary_mqtt_eclipse_org_pem_end"); static esp_err_t mqtt_event_handler_cb(esp_mqtt_event_handle_t event) { @@ -95,7 +95,7 @@ static void mqtt_app_start(void) { const esp_mqtt_client_config_t mqtt_cfg = { .uri = CONFIG_BROKER_URI, - .cert_pem = (const char *)iot_eclipse_org_pem_start, + .cert_pem = (const char *)mqtt_eclipse_org_pem_start, }; ESP_LOGI(TAG, "[APP] Free memory: %d bytes", esp_get_free_heap_size()); diff --git a/examples/protocols/mqtt/wss/main/component.mk b/examples/protocols/mqtt/wss/main/component.mk index 797c4a1..597752f 100644 --- a/examples/protocols/mqtt/wss/main/component.mk +++ b/examples/protocols/mqtt/wss/main/component.mk @@ -1 +1 @@ -COMPONENT_EMBED_TXTFILES := iot_eclipse_org.pem +COMPONENT_EMBED_TXTFILES := mqtt_eclipse_org.pem diff --git a/examples/protocols/mqtt/wss/main/iot_eclipse_org.pem b/examples/protocols/mqtt/wss/main/mqtt_eclipse_org.pem similarity index 100% rename from examples/protocols/mqtt/wss/main/iot_eclipse_org.pem rename to examples/protocols/mqtt/wss/main/mqtt_eclipse_org.pem diff --git a/examples/protocols/mqtt/wss/mqtt_wss_example_test.py b/examples/protocols/mqtt/wss/mqtt_wss_example_test.py index 5e0e143..486d65d 100644 --- a/examples/protocols/mqtt/wss/mqtt_wss_example_test.py +++ b/examples/protocols/mqtt/wss/mqtt_wss_example_test.py @@ -86,7 +86,7 @@ def test_examples_protocol_mqtt_wss(env, extra_data): client.on_message = on_message client.tls_set(None, None, - None, cert_reqs=ssl.CERT_NONE, tls_version=ssl.PROTOCOL_TLSv1, ciphers=None) + None, cert_reqs=ssl.CERT_NONE, tls_version=ssl.PROTOCOL_TLSv1_2, ciphers=None) print("Connecting...") client.connect(broker_url, broker_port, 60) except Exception: From c70dbbff8fbf1b2e5f82df1dfff5a72a837e2a06 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Wed, 9 Oct 2019 11:03:26 +0200 Subject: [PATCH 033/231] 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 --- examples/protocols/mqtt/ssl/main/app_main.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/examples/protocols/mqtt/ssl/main/app_main.c b/examples/protocols/mqtt/ssl/main/app_main.c index cf0df58..355819c 100644 --- a/examples/protocols/mqtt/ssl/main/app_main.c +++ b/examples/protocols/mqtt/ssl/main/app_main.c @@ -80,10 +80,14 @@ static esp_err_t mqtt_event_handler_cb(esp_mqtt_event_handle_t event) break; case MQTT_EVENT_ERROR: ESP_LOGI(TAG, "MQTT_EVENT_ERROR"); - int mbedtls_err = 0; - esp_err_t err = esp_tls_get_and_clear_last_error(event->error_handle, &mbedtls_err, NULL); - ESP_LOGI(TAG, "Last esp error code: 0x%x", err); - ESP_LOGI(TAG, "Last mbedtls failure: 0x%x", mbedtls_err); + if (event->error_handle->error_type == MQTT_ERROR_TYPE_ESP_TLS) { + ESP_LOGI(TAG, "Last error code reported from esp-tls: 0x%x", event->error_handle->esp_tls_last_esp_err); + ESP_LOGI(TAG, "Last tls stack error number: 0x%x", event->error_handle->esp_tls_stack_err); + } else if (event->error_handle->error_type == MQTT_ERROR_TYPE_CONNECTION_REFUSED) { + ESP_LOGI(TAG, "Connection refused error: 0x%x", event->error_handle->connect_return_code); + } else { + ESP_LOGW(TAG, "Unknown error type: 0x%x", event->error_handle->error_type); + } break; default: ESP_LOGI(TAG, "Other event id:%d", event->event_id); From bc2c8ffb4ff8275442a76d86e0118ad2a409699c Mon Sep 17 00:00:00 2001 From: David Cermak Date: Thu, 17 Oct 2019 10:57:04 +0200 Subject: [PATCH 034/231] ci: mqtt ssl test updated to output larger data in form of binary partition Testing with predefined buffer sizes for MQTT as well as mbedTLS to exercise fragmenting outgoing data on both layers. Example test compares received partition with the actual binary file. --- examples/protocols/mqtt/ssl/main/app_main.c | 30 ++++++++++++------- .../mqtt/ssl/mqtt_ssl_example_test.py | 28 +++++++++++++++-- examples/protocols/mqtt/ssl/sdkconfig.ci | 10 +++++++ 3 files changed, 56 insertions(+), 12 deletions(-) diff --git a/examples/protocols/mqtt/ssl/main/app_main.c b/examples/protocols/mqtt/ssl/main/app_main.c index 355819c..0074654 100644 --- a/examples/protocols/mqtt/ssl/main/app_main.c +++ b/examples/protocols/mqtt/ssl/main/app_main.c @@ -11,25 +11,16 @@ #include #include #include -#include "esp_wifi.h" #include "esp_system.h" #include "nvs_flash.h" #include "esp_event.h" #include "tcpip_adapter.h" #include "protocol_examples_common.h" -#include "freertos/FreeRTOS.h" -#include "freertos/task.h" -#include "freertos/semphr.h" -#include "freertos/queue.h" - -#include "lwip/sockets.h" -#include "lwip/dns.h" -#include "lwip/netdb.h" - #include "esp_log.h" #include "mqtt_client.h" #include "esp_tls.h" +#include "esp_ota_ops.h" static const char *TAG = "MQTTS_EXAMPLE"; @@ -41,6 +32,20 @@ extern const uint8_t mqtt_eclipse_org_pem_start[] asm("_binary_mqtt_eclipse_or #endif extern const uint8_t mqtt_eclipse_org_pem_end[] asm("_binary_mqtt_eclipse_org_pem_end"); +// +// Note: this function is for testing purposes only publishing the entire active partition +// (to be checked against the original binary) +// +static void send_binary(esp_mqtt_client_handle_t client) +{ + 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); + int msg_id = esp_mqtt_client_publish(client, "/topic/binary", binary_address, partition->size, 0, 0); + ESP_LOGI(TAG, "binary sent with msg_id=%d", msg_id); +} + static esp_err_t mqtt_event_handler_cb(esp_mqtt_event_handle_t event) { esp_mqtt_client_handle_t client = event->client; @@ -77,6 +82,10 @@ static esp_err_t mqtt_event_handler_cb(esp_mqtt_event_handle_t event) ESP_LOGI(TAG, "MQTT_EVENT_DATA"); printf("TOPIC=%.*s\r\n", event->topic_len, event->topic); printf("DATA=%.*s\r\n", event->data_len, event->data); + if (strncmp(event->data, "send binary please", event->data_len) == 0) { + ESP_LOGI(TAG, "Sending the binary"); + send_binary(client); + } break; case MQTT_EVENT_ERROR: ESP_LOGI(TAG, "MQTT_EVENT_ERROR"); @@ -121,6 +130,7 @@ void app_main(void) ESP_LOGI(TAG, "[APP] IDF version: %s", esp_get_idf_version()); esp_log_level_set("*", ESP_LOG_INFO); + esp_log_level_set("esp-tls", ESP_LOG_VERBOSE); esp_log_level_set("MQTT_CLIENT", ESP_LOG_VERBOSE); esp_log_level_set("MQTT_EXAMPLE", ESP_LOG_VERBOSE); esp_log_level_set("TRANSPORT_TCP", ESP_LOG_VERBOSE); diff --git a/examples/protocols/mqtt/ssl/mqtt_ssl_example_test.py b/examples/protocols/mqtt/ssl/mqtt_ssl_example_test.py index 6c255c6..43d4cec 100644 --- a/examples/protocols/mqtt/ssl/mqtt_ssl_example_test.py +++ b/examples/protocols/mqtt/ssl/mqtt_ssl_example_test.py @@ -28,6 +28,7 @@ import DUT event_client_connected = Event() event_stop_client = Event() event_client_received_correct = Event() +event_client_received_binary = Event() message_log = "" @@ -46,9 +47,27 @@ def mqtt_client_task(client): # The callback for when a PUBLISH message is received from the server. def on_message(client, userdata, msg): global message_log + global event_client_received_correct + global event_client_received_binary + if msg.topic == "/topic/binary": + binary = userdata + size = os.path.getsize(binary) + print("Receiving binary from esp and comparing with {}, size {}...".format(binary, size)) + with open(binary, "rb") as f: + bin = f.read() + if bin == msg.payload[:size]: + print("...matches!") + event_client_received_binary.set() + return + else: + recv_binary = binary + ".received" + with open(recv_binary, "w") as fw: + fw.write(msg.payload) + raise ValueError('Received binary (saved as: {}) does not match the original file: {}'.format(recv_binary, binary)) payload = msg.payload.decode() if not event_client_received_correct.is_set() and payload == "data": - client.publish("/topic/qos0", "data_to_esp32") + client.subscribe("/topic/binary") + client.publish("/topic/qos0", "send binary please") if msg.topic == "/topic/qos0" and payload == "data": event_client_received_correct.set() message_log += "Received data:" + msg.topic + " " + payload + "\n" @@ -64,6 +83,7 @@ def test_examples_protocol_mqtt_ssl(env, extra_data): 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 + 5. Test python client receives binary data from running partition and compares it with the binary """ dut1 = env.get_dut("mqtt_ssl", "examples/protocols/mqtt/ssl", dut_class=ESP32DUT) # check and log bin size @@ -86,6 +106,7 @@ def test_examples_protocol_mqtt_ssl(env, extra_data): client = mqtt.Client() client.on_connect = on_connect client.on_message = on_message + client.user_data_set(binary_file) client.tls_set(None, None, None, cert_reqs=ssl.CERT_NONE, tls_version=ssl.PROTOCOL_TLSv1_2, ciphers=None) @@ -113,7 +134,10 @@ def test_examples_protocol_mqtt_ssl(env, extra_data): if not event_client_received_correct.wait(timeout=30): raise ValueError('Wrong data received, msg log: {}'.format(message_log)) print("Checking esp-client received msg published from py-client...") - dut1.expect(re.compile(r"DATA=data_to_esp32"), timeout=30) + dut1.expect(re.compile(r"DATA=send binary please"), timeout=30) + print("Receiving binary data from running partition...") + if not event_client_received_binary.wait(timeout=30): + raise ValueError('Binary not received within timeout') finally: event_stop_client.set() thread1.join() diff --git a/examples/protocols/mqtt/ssl/sdkconfig.ci b/examples/protocols/mqtt/ssl/sdkconfig.ci index ce328a6..b3557c2 100644 --- a/examples/protocols/mqtt/ssl/sdkconfig.ci +++ b/examples/protocols/mqtt/ssl/sdkconfig.ci @@ -1,2 +1,12 @@ CONFIG_BROKER_URI="mqtts://${EXAMPLE_MQTT_BROKER_SSL}" CONFIG_BROKER_CERTIFICATE_OVERRIDE="${EXAMPLE_MQTT_BROKER_CERTIFICATE}" +CONFIG_MQTT_USE_CUSTOM_CONFIG=y +CONFIG_MQTT_TCP_DEFAULT_PORT=1883 +CONFIG_MQTT_SSL_DEFAULT_PORT=8883 +CONFIG_MQTT_WS_DEFAULT_PORT=80 +CONFIG_MQTT_WSS_DEFAULT_PORT=443 +CONFIG_MQTT_BUFFER_SIZE=16384 +CONFIG_MQTT_TASK_STACK_SIZE=6144 +CONFIG_MBEDTLS_ASYMMETRIC_CONTENT_LEN=y +CONFIG_MBEDTLS_SSL_IN_CONTENT_LEN=16384 +CONFIG_MBEDTLS_SSL_OUT_CONTENT_LEN=4096 From 358b4b1919b49ba8a23ec0bbd83668be29761e91 Mon Sep 17 00:00:00 2001 From: Marius Vikhammer Date: Thu, 31 Oct 2019 17:01:07 +0800 Subject: [PATCH 035/231] protocol examples: enable protocol examples for esp32sbeta Closes IDF-1027 --- examples/protocols/mqtt/publish_test/CMakeLists.txt | 1 - examples/protocols/mqtt/ssl/CMakeLists.txt | 1 - examples/protocols/mqtt/ssl_mutual_auth/CMakeLists.txt | 1 - examples/protocols/mqtt/tcp/CMakeLists.txt | 1 - examples/protocols/mqtt/ws/CMakeLists.txt | 1 - examples/protocols/mqtt/wss/CMakeLists.txt | 1 - 6 files changed, 6 deletions(-) diff --git a/examples/protocols/mqtt/publish_test/CMakeLists.txt b/examples/protocols/mqtt/publish_test/CMakeLists.txt index c7d6c8a..4a46af3 100644 --- a/examples/protocols/mqtt/publish_test/CMakeLists.txt +++ b/examples/protocols/mqtt/publish_test/CMakeLists.txt @@ -6,7 +6,6 @@ cmake_minimum_required(VERSION 3.5) # This example uses an extra component for common functions such as Wi-Fi and Ethernet connection. set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common) -set(SUPPORTED_TARGETS esp32) include($ENV{IDF_PATH}/tools/cmake/project.cmake) project(mqtt_publish) diff --git a/examples/protocols/mqtt/ssl/CMakeLists.txt b/examples/protocols/mqtt/ssl/CMakeLists.txt index 517c022..2b78a84 100644 --- a/examples/protocols/mqtt/ssl/CMakeLists.txt +++ b/examples/protocols/mqtt/ssl/CMakeLists.txt @@ -6,7 +6,6 @@ cmake_minimum_required(VERSION 3.5) # This example uses an extra component for common functions such as Wi-Fi and Ethernet connection. set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common) -set(SUPPORTED_TARGETS esp32) include($ENV{IDF_PATH}/tools/cmake/project.cmake) project(mqtt_ssl) diff --git a/examples/protocols/mqtt/ssl_mutual_auth/CMakeLists.txt b/examples/protocols/mqtt/ssl_mutual_auth/CMakeLists.txt index cd3de74..472a3ca 100644 --- a/examples/protocols/mqtt/ssl_mutual_auth/CMakeLists.txt +++ b/examples/protocols/mqtt/ssl_mutual_auth/CMakeLists.txt @@ -6,7 +6,6 @@ cmake_minimum_required(VERSION 3.5) # This example uses an extra component for common functions such as Wi-Fi and Ethernet connection. set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common) -set(SUPPORTED_TARGETS esp32) include($ENV{IDF_PATH}/tools/cmake/project.cmake) project(mqtt_ssl_mutual_auth) diff --git a/examples/protocols/mqtt/tcp/CMakeLists.txt b/examples/protocols/mqtt/tcp/CMakeLists.txt index 25e9bc3..5a70e7a 100644 --- a/examples/protocols/mqtt/tcp/CMakeLists.txt +++ b/examples/protocols/mqtt/tcp/CMakeLists.txt @@ -6,6 +6,5 @@ cmake_minimum_required(VERSION 3.5) # This example uses an extra component for common functions such as Wi-Fi and Ethernet connection. set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common) -set(SUPPORTED_TARGETS esp32) include($ENV{IDF_PATH}/tools/cmake/project.cmake) project(mqtt_tcp) \ No newline at end of file diff --git a/examples/protocols/mqtt/ws/CMakeLists.txt b/examples/protocols/mqtt/ws/CMakeLists.txt index 6b7b93d..f049006 100644 --- a/examples/protocols/mqtt/ws/CMakeLists.txt +++ b/examples/protocols/mqtt/ws/CMakeLists.txt @@ -6,6 +6,5 @@ cmake_minimum_required(VERSION 3.5) # This example uses an extra component for common functions such as Wi-Fi and Ethernet connection. set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common) -set(SUPPORTED_TARGETS esp32) include($ENV{IDF_PATH}/tools/cmake/project.cmake) project(mqtt_websocket) diff --git a/examples/protocols/mqtt/wss/CMakeLists.txt b/examples/protocols/mqtt/wss/CMakeLists.txt index 96fe21d..2350bd8 100644 --- a/examples/protocols/mqtt/wss/CMakeLists.txt +++ b/examples/protocols/mqtt/wss/CMakeLists.txt @@ -6,7 +6,6 @@ cmake_minimum_required(VERSION 3.5) # This example uses an extra component for common functions such as Wi-Fi and Ethernet connection. set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common) -set(SUPPORTED_TARGETS esp32) include($ENV{IDF_PATH}/tools/cmake/project.cmake) project(mqtt_websocket_secure) From 29a89b19618f7aa5c2bcec8c33344a539a6c1dbc Mon Sep 17 00:00:00 2001 From: David Cermak Date: Sat, 31 Aug 2019 16:19:21 +0200 Subject: [PATCH 036/231] examples: protocol examples which use common connection component updated to use esp_netif_init instead of tcpip_adapter in initialization code --- examples/protocols/mqtt/publish_test/main/publish_test.c | 4 ++-- examples/protocols/mqtt/ssl/main/app_main.c | 4 ++-- examples/protocols/mqtt/ssl_mutual_auth/main/app_main.c | 4 ++-- examples/protocols/mqtt/tcp/main/app_main.c | 4 ++-- examples/protocols/mqtt/ws/main/app_main.c | 4 ++-- examples/protocols/mqtt/wss/main/app_main.c | 4 ++-- 6 files changed, 12 insertions(+), 12 deletions(-) diff --git a/examples/protocols/mqtt/publish_test/main/publish_test.c b/examples/protocols/mqtt/publish_test/main/publish_test.c index 918f9a1..2a20d1a 100644 --- a/examples/protocols/mqtt/publish_test/main/publish_test.c +++ b/examples/protocols/mqtt/publish_test/main/publish_test.c @@ -14,7 +14,7 @@ #include "esp_system.h" #include "nvs_flash.h" #include "esp_event.h" -#include "tcpip_adapter.h" +#include "esp_netif.h" #include "protocol_examples_common.h" #include "freertos/FreeRTOS.h" @@ -169,7 +169,7 @@ void app_main(void) esp_log_level_set("OUTBOX", ESP_LOG_VERBOSE); ESP_ERROR_CHECK(nvs_flash_init()); - tcpip_adapter_init(); + esp_netif_init(); ESP_ERROR_CHECK(esp_event_loop_create_default()); /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig. diff --git a/examples/protocols/mqtt/ssl/main/app_main.c b/examples/protocols/mqtt/ssl/main/app_main.c index 0074654..f8e8573 100644 --- a/examples/protocols/mqtt/ssl/main/app_main.c +++ b/examples/protocols/mqtt/ssl/main/app_main.c @@ -14,7 +14,7 @@ #include "esp_system.h" #include "nvs_flash.h" #include "esp_event.h" -#include "tcpip_adapter.h" +#include "esp_netif.h" #include "protocol_examples_common.h" #include "esp_log.h" @@ -139,7 +139,7 @@ void app_main(void) esp_log_level_set("OUTBOX", ESP_LOG_VERBOSE); ESP_ERROR_CHECK(nvs_flash_init()); - tcpip_adapter_init(); + esp_netif_init(); ESP_ERROR_CHECK(esp_event_loop_create_default()); /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig. diff --git a/examples/protocols/mqtt/ssl_mutual_auth/main/app_main.c b/examples/protocols/mqtt/ssl_mutual_auth/main/app_main.c index 5c6eab7..858f58a 100644 --- a/examples/protocols/mqtt/ssl_mutual_auth/main/app_main.c +++ b/examples/protocols/mqtt/ssl_mutual_auth/main/app_main.c @@ -14,7 +14,7 @@ #include "esp_system.h" #include "nvs_flash.h" #include "esp_event.h" -#include "tcpip_adapter.h" +#include "esp_netif.h" #include "protocol_examples_common.h" #include "freertos/FreeRTOS.h" @@ -111,7 +111,7 @@ void app_main(void) esp_log_level_set("OUTBOX", ESP_LOG_VERBOSE); ESP_ERROR_CHECK(nvs_flash_init()); - tcpip_adapter_init(); + esp_netif_init(); ESP_ERROR_CHECK(esp_event_loop_create_default()); /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig. diff --git a/examples/protocols/mqtt/tcp/main/app_main.c b/examples/protocols/mqtt/tcp/main/app_main.c index 7acd685..d4b631e 100644 --- a/examples/protocols/mqtt/tcp/main/app_main.c +++ b/examples/protocols/mqtt/tcp/main/app_main.c @@ -15,7 +15,7 @@ #include "esp_system.h" #include "nvs_flash.h" #include "esp_event.h" -#include "tcpip_adapter.h" +#include "esp_netif.h" #include "protocol_examples_common.h" #include "freertos/FreeRTOS.h" @@ -138,7 +138,7 @@ void app_main(void) esp_log_level_set("OUTBOX", ESP_LOG_VERBOSE); ESP_ERROR_CHECK(nvs_flash_init()); - tcpip_adapter_init(); + esp_netif_init(); ESP_ERROR_CHECK(esp_event_loop_create_default()); /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig. diff --git a/examples/protocols/mqtt/ws/main/app_main.c b/examples/protocols/mqtt/ws/main/app_main.c index cacea73..5af121f 100644 --- a/examples/protocols/mqtt/ws/main/app_main.c +++ b/examples/protocols/mqtt/ws/main/app_main.c @@ -14,7 +14,7 @@ #include "esp_system.h" #include "nvs_flash.h" #include "esp_event.h" -#include "tcpip_adapter.h" +#include "esp_netif.h" #include "protocol_examples_common.h" #include "freertos/FreeRTOS.h" @@ -111,7 +111,7 @@ void app_main(void) esp_log_level_set("OUTBOX", ESP_LOG_VERBOSE); ESP_ERROR_CHECK(nvs_flash_init()); - tcpip_adapter_init(); + esp_netif_init(); ESP_ERROR_CHECK(esp_event_loop_create_default()); /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig. diff --git a/examples/protocols/mqtt/wss/main/app_main.c b/examples/protocols/mqtt/wss/main/app_main.c index 529e960..29b5de1 100644 --- a/examples/protocols/mqtt/wss/main/app_main.c +++ b/examples/protocols/mqtt/wss/main/app_main.c @@ -14,7 +14,7 @@ #include "esp_system.h" #include "nvs_flash.h" #include "esp_event.h" -#include "tcpip_adapter.h" +#include "esp_netif.h" #include "protocol_examples_common.h" #include "freertos/FreeRTOS.h" @@ -120,7 +120,7 @@ void app_main(void) esp_log_level_set("OUTBOX", ESP_LOG_VERBOSE); ESP_ERROR_CHECK(nvs_flash_init()); - tcpip_adapter_init(); + esp_netif_init(); ESP_ERROR_CHECK(esp_event_loop_create_default()); /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig. From 6efd355e1e2142e7edbdae2626952e8239cd646a Mon Sep 17 00:00:00 2001 From: David Cermak Date: Sun, 1 Sep 2019 18:25:23 +0200 Subject: [PATCH 037/231] examples: modify other examples and tests to use esp_netif instead of tcpip_adapter --- examples/protocols/mqtt/ssl_psk/main/app_main.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/protocols/mqtt/ssl_psk/main/app_main.c b/examples/protocols/mqtt/ssl_psk/main/app_main.c index 7a08d17..48e73f8 100644 --- a/examples/protocols/mqtt/ssl_psk/main/app_main.c +++ b/examples/protocols/mqtt/ssl_psk/main/app_main.c @@ -15,7 +15,7 @@ #include "esp_system.h" #include "nvs_flash.h" #include "esp_event.h" -#include "tcpip_adapter.h" +#include "esp_netif.h" #include "protocol_examples_common.h" #include "freertos/FreeRTOS.h" @@ -128,7 +128,7 @@ void app_main(void) esp_log_level_set("OUTBOX", ESP_LOG_VERBOSE); ESP_ERROR_CHECK(nvs_flash_init()); - tcpip_adapter_init(); + esp_netif_init(); ESP_ERROR_CHECK(esp_event_loop_create_default()); /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig. From 1d09afe527b66992257cc0f8919cf5fc6e5de294 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Fri, 13 Sep 2019 15:44:23 +0200 Subject: [PATCH 038/231] esp_netif: include tcpip_adapter legacy header by default to provide *implicit* compatiblity --- examples/protocols/mqtt/publish_test/sdkconfig.ci | 3 ++- examples/protocols/mqtt/ssl/sdkconfig.ci | 1 + examples/protocols/mqtt/tcp/sdkconfig.ci | 1 + examples/protocols/mqtt/ws/sdkconfig.ci | 1 + examples/protocols/mqtt/wss/sdkconfig.ci | 2 +- 5 files changed, 6 insertions(+), 2 deletions(-) diff --git a/examples/protocols/mqtt/publish_test/sdkconfig.ci b/examples/protocols/mqtt/publish_test/sdkconfig.ci index 34319fc..3ae0049 100644 --- a/examples/protocols/mqtt/publish_test/sdkconfig.ci +++ b/examples/protocols/mqtt/publish_test/sdkconfig.ci @@ -5,4 +5,5 @@ CONFIG_EXAMPLE_BROKER_WSS_URI="wss://${EXAMPLE_MQTT_BROKER_WSS}/ws" CONFIG_EXAMPLE_BROKER_CERTIFICATE_OVERRIDE="${EXAMPLE_MQTT_BROKER_CERTIFICATE}" CONFIG_MBEDTLS_ASYMMETRIC_CONTENT_LEN=y CONFIG_MBEDTLS_SSL_IN_CONTENT_LEN=16384 -CONFIG_MBEDTLS_SSL_OUT_CONTENT_LEN=16384 \ No newline at end of file +CONFIG_MBEDTLS_SSL_OUT_CONTENT_LEN=16384 +CONFIG_ESP_NETIF_TCPIP_ADAPTER_COMPATIBLE_LAYER=n diff --git a/examples/protocols/mqtt/ssl/sdkconfig.ci b/examples/protocols/mqtt/ssl/sdkconfig.ci index b3557c2..716fb5b 100644 --- a/examples/protocols/mqtt/ssl/sdkconfig.ci +++ b/examples/protocols/mqtt/ssl/sdkconfig.ci @@ -10,3 +10,4 @@ CONFIG_MQTT_TASK_STACK_SIZE=6144 CONFIG_MBEDTLS_ASYMMETRIC_CONTENT_LEN=y CONFIG_MBEDTLS_SSL_IN_CONTENT_LEN=16384 CONFIG_MBEDTLS_SSL_OUT_CONTENT_LEN=4096 +CONFIG_ESP_NETIF_TCPIP_ADAPTER_COMPATIBLE_LAYER=n diff --git a/examples/protocols/mqtt/tcp/sdkconfig.ci b/examples/protocols/mqtt/tcp/sdkconfig.ci index 09ca8f3..43a5f63 100644 --- a/examples/protocols/mqtt/tcp/sdkconfig.ci +++ b/examples/protocols/mqtt/tcp/sdkconfig.ci @@ -1,2 +1,3 @@ CONFIG_LOG_DEFAULT_LEVEL_DEBUG=y CONFIG_BROKER_URL="FROM_STDIN" +CONFIG_ESP_NETIF_TCPIP_ADAPTER_COMPATIBLE_LAYER=n diff --git a/examples/protocols/mqtt/ws/sdkconfig.ci b/examples/protocols/mqtt/ws/sdkconfig.ci index 4f14eef..caa4f0c 100644 --- a/examples/protocols/mqtt/ws/sdkconfig.ci +++ b/examples/protocols/mqtt/ws/sdkconfig.ci @@ -1 +1,2 @@ CONFIG_BROKER_URI="ws://${EXAMPLE_MQTT_BROKER_WS}/ws" +CONFIG_ESP_NETIF_TCPIP_ADAPTER_COMPATIBLE_LAYER=n diff --git a/examples/protocols/mqtt/wss/sdkconfig.ci b/examples/protocols/mqtt/wss/sdkconfig.ci index d0dd492..d088025 100644 --- a/examples/protocols/mqtt/wss/sdkconfig.ci +++ b/examples/protocols/mqtt/wss/sdkconfig.ci @@ -1,3 +1,3 @@ CONFIG_BROKER_URI="wss://${EXAMPLE_MQTT_BROKER_WSS}/ws" CONFIG_BROKER_CERTIFICATE_OVERRIDE="${EXAMPLE_MQTT_BROKER_CERTIFICATE}" - +CONFIG_ESP_NETIF_TCPIP_ADAPTER_COMPATIBLE_LAYER=n From 274a258b0292e039df8317152c17d80fca3ed3fb Mon Sep 17 00:00:00 2001 From: He Yin Ling Date: Wed, 27 Nov 2019 11:58:07 +0800 Subject: [PATCH 039/231] test: update example and unit tests with new import roles: tiny_test_fw is a python package now. import it using normal way. --- .../mqtt/ssl/mqtt_ssl_example_test.py | 27 +++++-------------- .../mqtt/tcp/mqtt_tcp_example_test.py | 25 +++++------------ .../protocols/mqtt/ws/mqtt_ws_example_test.py | 25 +++++------------ .../mqtt/wss/mqtt_wss_example_test.py | 24 +++++------------ 4 files changed, 25 insertions(+), 76 deletions(-) diff --git a/examples/protocols/mqtt/ssl/mqtt_ssl_example_test.py b/examples/protocols/mqtt/ssl/mqtt_ssl_example_test.py index 43d4cec..a392b2c 100644 --- a/examples/protocols/mqtt/ssl/mqtt_ssl_example_test.py +++ b/examples/protocols/mqtt/ssl/mqtt_ssl_example_test.py @@ -8,21 +8,8 @@ import ssl import paho.mqtt.client as mqtt from threading import Thread, Event - -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() @@ -73,7 +60,7 @@ def on_message(client, userdata, msg): message_log += "Received data:" + msg.topic + " " + payload + "\n" -@IDF.idf_example_test(env_tag="Example_WIFI") +@ttfw_idf.idf_example_test(env_tag="Example_WIFI") def test_examples_protocol_mqtt_ssl(env, extra_data): broker_url = "" broker_port = 0 @@ -85,13 +72,13 @@ def test_examples_protocol_mqtt_ssl(env, extra_data): 4. Test ESP32 client received correct qos0 message 5. Test python client receives binary data from running partition and compares it with the binary """ - dut1 = env.get_dut("mqtt_ssl", "examples/protocols/mqtt/ssl", dut_class=ESP32DUT) + dut1 = env.get_dut("mqtt_ssl", "examples/protocols/mqtt/ssl", dut_class=ttfw_idf.ESP32DUT) # check and log bin size binary_file = os.path.join(dut1.app.binary_path, "mqtt_ssl.bin") bin_size = os.path.getsize(binary_file) - IDF.log_performance("mqtt_ssl_bin_size", "{}KB" - .format(bin_size // 1024)) - IDF.check_performance("mqtt_ssl_size", bin_size // 1024) + ttfw_idf.log_performance("mqtt_ssl_bin_size", "{}KB" + .format(bin_size // 1024)) + ttfw_idf.check_performance("mqtt_ssl_size", bin_size // 1024) # Look for host:port in sdkconfig try: value = re.search(r'\:\/\/([^:]+)\:([0-9]+)', dut1.app.get_sdkconfig()["CONFIG_BROKER_URI"]) diff --git a/examples/protocols/mqtt/tcp/mqtt_tcp_example_test.py b/examples/protocols/mqtt/tcp/mqtt_tcp_example_test.py index 4e39705..2d8dbe3 100644 --- a/examples/protocols/mqtt/tcp/mqtt_tcp_example_test.py +++ b/examples/protocols/mqtt/tcp/mqtt_tcp_example_test.py @@ -6,21 +6,8 @@ from threading import Thread import struct import time - -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 msgid = -1 @@ -66,7 +53,7 @@ def mqqt_server_sketch(my_ip, port): print("server closed") -@IDF.idf_example_test(env_tag="Example_WIFI") +@ttfw_idf.idf_example_test(env_tag="Example_WIFI") def test_examples_protocol_mqtt_qos1(env, extra_data): global msgid """ @@ -76,12 +63,12 @@ def test_examples_protocol_mqtt_qos1(env, extra_data): 3. Test evaluates that qos1 message is queued and removed from queued after ACK received 4. Test the broker received the same message id evaluated in step 3 """ - dut1 = env.get_dut("mqtt_tcp", "examples/protocols/mqtt/tcp", dut_class=ESP32DUT) + dut1 = env.get_dut("mqtt_tcp", "examples/protocols/mqtt/tcp", dut_class=ttfw_idf.ESP32DUT) # check and log bin size binary_file = os.path.join(dut1.app.binary_path, "mqtt_tcp.bin") bin_size = os.path.getsize(binary_file) - IDF.log_performance("mqtt_tcp_bin_size", "{}KB".format(bin_size // 1024)) - IDF.check_performance("mqtt_tcp_size", bin_size // 1024) + ttfw_idf.log_performance("mqtt_tcp_bin_size", "{}KB".format(bin_size // 1024)) + ttfw_idf.check_performance("mqtt_tcp_size", bin_size // 1024) # 1. start mqtt broker sketch host_ip = get_my_ip() thread1 = Thread(target=mqqt_server_sketch, args=(host_ip,1883)) diff --git a/examples/protocols/mqtt/ws/mqtt_ws_example_test.py b/examples/protocols/mqtt/ws/mqtt_ws_example_test.py index 19ed395..cf40c9a 100644 --- a/examples/protocols/mqtt/ws/mqtt_ws_example_test.py +++ b/examples/protocols/mqtt/ws/mqtt_ws_example_test.py @@ -7,21 +7,8 @@ import sys import paho.mqtt.client as mqtt from threading import Thread, Event - -try: - import IDF - from IDF.IDFDUT import ESP32DUT -except Exception: - # 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() event_stop_client = Event() @@ -52,7 +39,7 @@ def on_message(client, userdata, msg): message_log += "Received data:" + msg.topic + " " + payload + "\n" -@IDF.idf_example_test(env_tag="Example_WIFI") +@ttfw_idf.idf_example_test(env_tag="Example_WIFI") def test_examples_protocol_mqtt_ws(env, extra_data): broker_url = "" broker_port = 0 @@ -63,12 +50,12 @@ def test_examples_protocol_mqtt_ws(env, extra_data): 3. Test evaluates it received correct qos0 message 4. Test ESP32 client received correct qos0 message """ - dut1 = env.get_dut("mqtt_websocket", "examples/protocols/mqtt/ws", dut_class=ESP32DUT) + dut1 = env.get_dut("mqtt_websocket", "examples/protocols/mqtt/ws", dut_class=ttfw_idf.ESP32DUT) # check and log bin size binary_file = os.path.join(dut1.app.binary_path, "mqtt_websocket.bin") bin_size = os.path.getsize(binary_file) - IDF.log_performance("mqtt_websocket_bin_size", "{}KB".format(bin_size // 1024)) - IDF.check_performance("mqtt_websocket_size", bin_size // 1024) + ttfw_idf.log_performance("mqtt_websocket_bin_size", "{}KB".format(bin_size // 1024)) + ttfw_idf.check_performance("mqtt_websocket_size", bin_size // 1024) # Look for host:port in sdkconfig try: value = re.search(r'\:\/\/([^:]+)\:([0-9]+)', dut1.app.get_sdkconfig()["CONFIG_BROKER_URI"]) diff --git a/examples/protocols/mqtt/wss/mqtt_wss_example_test.py b/examples/protocols/mqtt/wss/mqtt_wss_example_test.py index 486d65d..a4c4e6c 100644 --- a/examples/protocols/mqtt/wss/mqtt_wss_example_test.py +++ b/examples/protocols/mqtt/wss/mqtt_wss_example_test.py @@ -8,21 +8,9 @@ import ssl import paho.mqtt.client as mqtt from threading import Thread, Event +from tiny_test_fw import DUT +import ttfw_idf -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 event_client_connected = Event() event_stop_client = Event() @@ -53,7 +41,7 @@ def on_message(client, userdata, msg): message_log += "Received data:" + msg.topic + " " + payload + "\n" -@IDF.idf_example_test(env_tag="Example_WIFI") +@ttfw_idf.idf_example_test(env_tag="Example_WIFI") def test_examples_protocol_mqtt_wss(env, extra_data): broker_url = "" broker_port = 0 @@ -64,12 +52,12 @@ def test_examples_protocol_mqtt_wss(env, extra_data): 3. Test evaluates it received correct qos0 message 4. Test ESP32 client received correct qos0 message """ - dut1 = env.get_dut("mqtt_websocket_secure", "examples/protocols/mqtt/wss", dut_class=ESP32DUT) + dut1 = env.get_dut("mqtt_websocket_secure", "examples/protocols/mqtt/wss", dut_class=ttfw_idf.ESP32DUT) # check and log bin size binary_file = os.path.join(dut1.app.binary_path, "mqtt_websocket_secure.bin") bin_size = os.path.getsize(binary_file) - IDF.log_performance("mqtt_websocket_secure_bin_size", "{}KB".format(bin_size // 1024)) - IDF.check_performance("mqtt_websocket_secure_size", bin_size // 1024) + ttfw_idf.log_performance("mqtt_websocket_secure_bin_size", "{}KB".format(bin_size // 1024)) + ttfw_idf.check_performance("mqtt_websocket_secure_size", bin_size // 1024) # Look for host:port in sdkconfig try: value = re.search(r'\:\/\/([^:]+)\:([0-9]+)', dut1.app.get_sdkconfig()["CONFIG_BROKER_URI"]) From 6b4a3b37593ac684031885616d8cacb7e4926f8f Mon Sep 17 00:00:00 2001 From: David Cermak Date: Wed, 4 Dec 2019 14:21:29 +0100 Subject: [PATCH 040/231] 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 ++++ .../mqtt/publish_test/sdkconfig.qemu | 19 +++++++++++ 3 files changed, 39 insertions(+), 21 deletions(-) create mode 100644 components/mqtt/weekend_test/test_weekend_mqtt_qemu.yml create mode 100644 examples/protocols/mqtt/publish_test/sdkconfig.qemu 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 + diff --git a/examples/protocols/mqtt/publish_test/sdkconfig.qemu b/examples/protocols/mqtt/publish_test/sdkconfig.qemu new file mode 100644 index 0000000..e0d289d --- /dev/null +++ b/examples/protocols/mqtt/publish_test/sdkconfig.qemu @@ -0,0 +1,19 @@ +CONFIG_IDF_TARGET_ESP32=y +CONFIG_EXAMPLE_USE_OPENETH=y +CONFIG_ETH_USE_OPENETH=y +CONFIG_ETH_OPENETH_DMA_RX_BUFFER_NUM=4 +CONFIG_ETH_OPENETH_DMA_TX_BUFFER_NUM=1 +CONFIG_EXAMPLE_CONNECT_ETHERNET=y +CONFIG_LOG_DEFAULT_LEVEL_DEBUG=y +CONFIG_ESPTOOLPY_FLASHMODE_DOUT=y +CONFIG_MBEDTLS_ASYMMETRIC_CONTENT_LEN=y +CONFIG_MBEDTLS_SSL_IN_CONTENT_LEN=16384 +CONFIG_MBEDTLS_SSL_OUT_CONTENT_LEN=16384 +CONFIG_EXAMPLE_BROKER_SSL_URI="mqtts://${EXAMPLE_MQTT_BROKER_SSL}" +CONFIG_EXAMPLE_BROKER_TCP_URI="mqtt://${EXAMPLE_MQTT_BROKER_TCP}" +CONFIG_EXAMPLE_BROKER_WS_URI="ws://${EXAMPLE_MQTT_BROKER_WS}/ws" +CONFIG_EXAMPLE_BROKER_WSS_URI="wss://${EXAMPLE_MQTT_BROKER_WSS}/ws" +CONFIG_EXAMPLE_BROKER_CERTIFICATE_OVERRIDE="${EXAMPLE_MQTT_BROKER_CERTIFICATE}" +CONFIG_MBEDTLS_HARDWARE_AES=n +CONFIG_MBEDTLS_HARDWARE_MPI=n +CONFIG_MBEDTLS_HARDWARE_SHA=n From 5488fe31ba2236397d08d6e1305569092765928f Mon Sep 17 00:00:00 2001 From: David Cermak Date: Fri, 29 Nov 2019 10:54:02 +0100 Subject: [PATCH 041/231] esp_netif, examples: esp_netif_init() moved into ESP_ERROR_CHECK() esp_netif_init() returns standard esp_err_t error code (unlike tcpip_adapter init), so shall be checked for the return value Also to make the initialization code more consistent. --- examples/protocols/mqtt/publish_test/main/publish_test.c | 2 +- examples/protocols/mqtt/ssl/main/app_main.c | 2 +- examples/protocols/mqtt/ssl_mutual_auth/main/app_main.c | 2 +- examples/protocols/mqtt/ssl_psk/main/app_main.c | 2 +- examples/protocols/mqtt/tcp/main/app_main.c | 2 +- examples/protocols/mqtt/ws/main/app_main.c | 2 +- examples/protocols/mqtt/wss/main/app_main.c | 2 +- 7 files changed, 7 insertions(+), 7 deletions(-) diff --git a/examples/protocols/mqtt/publish_test/main/publish_test.c b/examples/protocols/mqtt/publish_test/main/publish_test.c index 2a20d1a..b6b7e53 100644 --- a/examples/protocols/mqtt/publish_test/main/publish_test.c +++ b/examples/protocols/mqtt/publish_test/main/publish_test.c @@ -169,7 +169,7 @@ void app_main(void) esp_log_level_set("OUTBOX", ESP_LOG_VERBOSE); ESP_ERROR_CHECK(nvs_flash_init()); - esp_netif_init(); + ESP_ERROR_CHECK(esp_netif_init()); ESP_ERROR_CHECK(esp_event_loop_create_default()); /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig. diff --git a/examples/protocols/mqtt/ssl/main/app_main.c b/examples/protocols/mqtt/ssl/main/app_main.c index f8e8573..1afb9c0 100644 --- a/examples/protocols/mqtt/ssl/main/app_main.c +++ b/examples/protocols/mqtt/ssl/main/app_main.c @@ -139,7 +139,7 @@ void app_main(void) esp_log_level_set("OUTBOX", ESP_LOG_VERBOSE); ESP_ERROR_CHECK(nvs_flash_init()); - esp_netif_init(); + ESP_ERROR_CHECK(esp_netif_init()); ESP_ERROR_CHECK(esp_event_loop_create_default()); /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig. diff --git a/examples/protocols/mqtt/ssl_mutual_auth/main/app_main.c b/examples/protocols/mqtt/ssl_mutual_auth/main/app_main.c index 858f58a..6c620af 100644 --- a/examples/protocols/mqtt/ssl_mutual_auth/main/app_main.c +++ b/examples/protocols/mqtt/ssl_mutual_auth/main/app_main.c @@ -111,7 +111,7 @@ void app_main(void) esp_log_level_set("OUTBOX", ESP_LOG_VERBOSE); ESP_ERROR_CHECK(nvs_flash_init()); - esp_netif_init(); + ESP_ERROR_CHECK(esp_netif_init()); ESP_ERROR_CHECK(esp_event_loop_create_default()); /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig. diff --git a/examples/protocols/mqtt/ssl_psk/main/app_main.c b/examples/protocols/mqtt/ssl_psk/main/app_main.c index 48e73f8..3ca754c 100644 --- a/examples/protocols/mqtt/ssl_psk/main/app_main.c +++ b/examples/protocols/mqtt/ssl_psk/main/app_main.c @@ -128,7 +128,7 @@ void app_main(void) esp_log_level_set("OUTBOX", ESP_LOG_VERBOSE); ESP_ERROR_CHECK(nvs_flash_init()); - esp_netif_init(); + ESP_ERROR_CHECK(esp_netif_init()); ESP_ERROR_CHECK(esp_event_loop_create_default()); /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig. diff --git a/examples/protocols/mqtt/tcp/main/app_main.c b/examples/protocols/mqtt/tcp/main/app_main.c index d4b631e..2c2abbe 100644 --- a/examples/protocols/mqtt/tcp/main/app_main.c +++ b/examples/protocols/mqtt/tcp/main/app_main.c @@ -138,7 +138,7 @@ void app_main(void) esp_log_level_set("OUTBOX", ESP_LOG_VERBOSE); ESP_ERROR_CHECK(nvs_flash_init()); - esp_netif_init(); + ESP_ERROR_CHECK(esp_netif_init()); ESP_ERROR_CHECK(esp_event_loop_create_default()); /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig. diff --git a/examples/protocols/mqtt/ws/main/app_main.c b/examples/protocols/mqtt/ws/main/app_main.c index 5af121f..96b29e3 100644 --- a/examples/protocols/mqtt/ws/main/app_main.c +++ b/examples/protocols/mqtt/ws/main/app_main.c @@ -111,7 +111,7 @@ void app_main(void) esp_log_level_set("OUTBOX", ESP_LOG_VERBOSE); ESP_ERROR_CHECK(nvs_flash_init()); - esp_netif_init(); + ESP_ERROR_CHECK(esp_netif_init()); ESP_ERROR_CHECK(esp_event_loop_create_default()); /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig. diff --git a/examples/protocols/mqtt/wss/main/app_main.c b/examples/protocols/mqtt/wss/main/app_main.c index 29b5de1..61b7b44 100644 --- a/examples/protocols/mqtt/wss/main/app_main.c +++ b/examples/protocols/mqtt/wss/main/app_main.c @@ -120,7 +120,7 @@ void app_main(void) esp_log_level_set("OUTBOX", ESP_LOG_VERBOSE); ESP_ERROR_CHECK(nvs_flash_init()); - esp_netif_init(); + ESP_ERROR_CHECK(esp_netif_init()); ESP_ERROR_CHECK(esp_event_loop_create_default()); /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig. From 1f2405289e893bb86cf003a4e0b396acce27734f Mon Sep 17 00:00:00 2001 From: Marius Vikhammer Date: Tue, 26 Nov 2019 12:29:09 +0800 Subject: [PATCH 042/231] doc/mqtt: add event description and more details about configuration to API reference --- docs/en/api-reference/protocols/mqtt.rst | 84 ++++++++++++++++++------ 1 file changed, 65 insertions(+), 19 deletions(-) diff --git a/docs/en/api-reference/protocols/mqtt.rst b/docs/en/api-reference/protocols/mqtt.rst index 80d6270..8bbacb1 100644 --- a/docs/en/api-reference/protocols/mqtt.rst +++ b/docs/en/api-reference/protocols/mqtt.rst @@ -9,10 +9,10 @@ ESP-MQTT is an implementation of MQTT protocol client (MQTT is a lightweight pub Features -------- - * supports MQTT over TCP, SSL with mbedtls, MQTT over Websocket, MQTT over Websocket Secure. + * Supports MQTT over TCP, SSL with mbedtls, MQTT over Websocket, MQTT over Websocket Secure. * Easy to setup with URI * Multiple instances (Multiple clients in one application) - * Support subscribing, publishing, authentication, will messages, keep alive pings and all 3 QoS levels (it should be a fully functional client). + * Support subscribing, publishing, authentication, last will messages, keep alive pings and all 3 QoS levels (it should be a fully functional client). Application Example @@ -20,6 +20,7 @@ Application Example * :example:`protocols/mqtt/tcp`: MQTT over tcp, default port 1883 * :example:`protocols/mqtt/ssl`: MQTT over tcp, default port 8883 + * :example:`protocols/mqtt/ssl_psk`: MQTT over tcp using pre-shared keys for authentication, default port 8883 * :example:`protocols/mqtt/ws`: MQTT over Websocket, default port 80 * :example:`protocols/mqtt/wss`: MQTT over Websocket Secure, default port 443 @@ -62,19 +63,8 @@ URI esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, client); esp_mqtt_client_start(client); -- Note: By default mqtt client uses event loop library to post related mqtt events (connected, subsribed, published, etc.) +- Note: By default mqtt client uses event loop library to post related mqtt events (connected, subscribed, published, etc.) -- If there are any options related to the URI in - ``esp_mqtt_client_config_t``, the option defined by the URI will be - overridden. Sample: - -.. code:: c - - const esp_mqtt_client_config_t mqtt_cfg = { - .uri = "mqtt://mqtt.eclipse.org:1234", - .port = 4567, - }; - //MQTT client will connect to mqtt.eclipse.org using port 4567 SSL ^^^ @@ -84,7 +74,7 @@ SSL - Check the sample application: ``examples/mqtt_ssl`` - Configuration: -.. code:: cpp +.. code:: c const esp_mqtt_client_config_t mqtt_cfg = { .uri = "mqtts://mqtt.eclipse.org:8883", @@ -92,15 +82,56 @@ SSL .cert_pem = (const char *)mqtt_eclipse_org_pem_start, }; +If the certificate is not null-terminated then ``cert_len`` should also be set. +Other SSL related configuration parameters are: + + * ``use_global_ca_store``: use the global certificate store to verify server certificate, see ``esp-tls.h`` for more information + * ``client_cert_pem``: pointer to certificate data in PEM or DER format for SSL mutual authentication, default is NULL, not required if mutual authentication is not needed. + * ``client_cert_len``: length of the buffer pointed to by client_cert_pem. May be 0 for null-terminated pem. + * ``client_key_pem``: pointer to private key data in PEM or DER format for SSL mutual authentication, default is NULL, not required if mutual authentication is not needed. + * ``client_key_len``: length of the buffer pointed to by client_key_pem. May be 0 for null-terminated pem. + * ``psk_hint_key``: pointer to PSK struct defined in esp_tls.h to enable PSK authentication (as alternative to certificate verification). If not NULL and server/client certificates are NULL, PSK is enabled + * ``alpn_protos``: NULL-terminated list of protocols to be used for ALPN. + +Last Will and Testament +^^^^^^^^^^^^^^^^^^^^^^^ +MQTT allows for a last will and testament (LWT) message to notify other clients when a client ungracefully disconnects. This is configured by the following fields +in the ``esp_mqtt_client_config_t``-struct. + + * ``lwt_topic``: pointer to the LWT message topic + * ``lwt_msg``: pointer to the LWT message + * ``lwt_msg_len``: length of the LWT message, required if ``lwt_msg`` is not null-terminated + * ``lwt_qos``: quality of service for the LWT message + * ``lwt_retain``: specifies the retain flag of the LWT message + +Other Configuration Parameters +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + * ``disable_clean_session``: determines the clean session flag for the connect message, defaults to a clean session + * ``keepalive``: determines how many seconds the client will wait for a ping response before disconnecting, default is 120 seconds. + * ``disable_auto_reconnect``: enable to stop the client from reconnecting to server after errors or disconnects + * ``user_context``: custom context that will be passed to the event handler + * ``task_prio``: MQTT task priority, defaults to 5 + * ``task_stack``: MQTT task stack size, defaults to 6144 bytes, setting this will override setting from menuconfig + * ``buffer_size``: size of MQTT send/receive buffer, default is 1024 bytes + * ``username``: pointer to the username used for connecting to the broker + * ``password``: pointer to the password used for connecting to the broker + * ``client_id``: pointer to the client id, defaults to ``ESP32_%CHIPID%`` where %CHIPID% are the last 3 bytes of MAC address in hex format + * ``host``: MQTT broker domain (ipv4 as string), setting the uri will override this + * ``port``: MQTT broker port, specifying the port in the uri will override this + * ``transport``: sets the transport protocol, setting the uri will override this + * ``refresh_connection_after_ms``: refresh connection after this value (in milliseconds) + * ``event_handle``: handle for MQTT events as a callback in legacy mode + * ``event_loop_handle``: handle for MQTT event loop library + + + For more options on ``esp_mqtt_client_config_t``, please refer to API reference below Change settings in Project Configuration Menu ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +The settings for MQTT can be found using ``idf.py menuconfig``, under Component config -> ESP-MQTT Configuration -:: - idf.py menuconfig - -> Component config -> ESP-MQTT Configuration - +The following settings are available: - :ref:`CONFIG_MQTT_PROTOCOL_311`: Enables 3.1.1 version of MQTT protocol @@ -109,6 +140,21 @@ Change settings in Project Configuration Menu - :ref:`CONFIG_MQTT_CUSTOM_OUTBOX`: Disables default implementation of mqtt_outbox, so a specific implementaion can be supplied +Events +------ +The following events may be posted by the MQTT client: + +* ``MQTT_EVENT_BEFORE_CONNECT``: The client is initialized and about to start connecting to the broker. +* ``MQTT_EVENT_CONNECTED``: The client has successfully established a connection to the broker. The client is now ready to send and receive data. +* ``MQTT_EVENT_DISCONNECTED``: The client has aborted the connection due to being unable to read or write data, e.g. because the server is unavailable. +* ``MQTT_EVENT_SUBSCRIBED``: The broker has acknowledged the client's subscribe request. The event data will contain the message ID of the subscribe message. +* ``MQTT_EVENT_UNSUBSCRIBED``: The broker has acknowledged the client's unsubscribe request. The event data will contain the message ID of the unsubscribe message. +* ``MQTT_EVENT_PUBLISHED``: The broker has acknowledged the client's publish message. This will only be posted for Quality of Service level 1 and 2, as level 0 does not use acknowledgements. The event data will contain the message ID of the publish message. +* ``MQTT_EVENT_DATA``: The client has received a publish message. The event data contains: message ID, name of the topic it was published to, received data and its length. For data that exceeds the internal buffer multiple `MQTT_EVENT_DATA` will be posted and `current_data_offset` and `total_data_len` from event data updated to keep track of the fragmented message. +* ``MQTT_EVENT_ERROR``: The client has encountered an error. `esp_mqtt_error_type_t` from `error_handle` in the event data can be used to further determine the type of the error. The type of error will determine which parts of the `error_handle` struct is filled. + + + API Reference ------------- From 084451f9e2cae984bddced0bdad3a149a4b813e4 Mon Sep 17 00:00:00 2001 From: Angus Gratton Date: Wed, 13 Nov 2019 11:46:16 +0800 Subject: [PATCH 043/231] docs: add new top-level docs builder that builds docs for a single chip --- docs/en/api-reference/protocols/mqtt.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/en/api-reference/protocols/mqtt.rst b/docs/en/api-reference/protocols/mqtt.rst index 8bbacb1..47021b6 100644 --- a/docs/en/api-reference/protocols/mqtt.rst +++ b/docs/en/api-reference/protocols/mqtt.rst @@ -158,4 +158,4 @@ The following events may be posted by the MQTT client: API Reference ------------- -.. include:: /_build/inc/mqtt_client.inc +.. include-build-file:: inc/mqtt_client.inc From d52f5e084a168d6e17b64e43304aa3962d19f65b Mon Sep 17 00:00:00 2001 From: David Cermak Date: Sun, 19 Jan 2020 21:24:28 +0100 Subject: [PATCH 044/231] mqtt: add basic set of unit tests --- components/mqtt/test/CMakeLists.txt | 2 + components/mqtt/test/component.mk | 4 ++ components/mqtt/test/test_mqtt.c | 70 +++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+) 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/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 045/231] mqtt: example test to check connection with different ssl parameters --- .../mqtt/weekend_test/mqtt_publish_test.py | 8 +- .../mqtt/publish_connect_test/CMakeLists.txt | 19 ++ .../Makefile | 2 +- .../README.md | 2 +- .../mqtt/publish_connect_test/ca.crt | 22 ++ .../mqtt/publish_connect_test/ca.der | Bin 0 -> 919 bytes .../mqtt/publish_connect_test/ca.key | 27 ++ .../mqtt/publish_connect_test/client_inv.crt | 19 ++ .../publish_connect_test/client_no_pwd.key | 27 ++ .../mqtt/publish_connect_test/client_pwd.crt | 18 ++ .../mqtt/publish_connect_test/client_pwd.key | 30 +++ .../mqtt/publish_connect_test/example_test.py | 230 +++++++++++++++++ .../publish_connect_test/main/CMakeLists.txt | 2 + .../main/Kconfig.projbuild | 35 +++ .../publish_connect_test/main/component.mk | 1 + .../publish_connect_test/main/connect_test.c | 237 ++++++++++++++++++ .../main/mqtt_eclipse_org.pem | 0 .../main/publish_connect_test.c | 76 ++++++ .../publish_connect_test/main/publish_test.c | 175 +++++++++++++ .../sdkconfig.ci | 0 .../sdkconfig.qemu | 0 .../mqtt/publish_connect_test/server.key | 27 ++ .../mqtt/publish_test/CMakeLists.txt | 13 - .../mqtt/publish_test/main/CMakeLists.txt | 2 - .../mqtt/publish_test/main/component.mk | 1 - .../mqtt/publish_test/main/publish_test.c | 226 ----------------- 26 files changed, 948 insertions(+), 251 deletions(-) create mode 100644 examples/protocols/mqtt/publish_connect_test/CMakeLists.txt rename examples/protocols/mqtt/{publish_test => publish_connect_test}/Makefile (85%) rename examples/protocols/mqtt/{publish_test => publish_connect_test}/README.md (98%) create mode 100644 examples/protocols/mqtt/publish_connect_test/ca.crt create mode 100644 examples/protocols/mqtt/publish_connect_test/ca.der create mode 100644 examples/protocols/mqtt/publish_connect_test/ca.key create mode 100644 examples/protocols/mqtt/publish_connect_test/client_inv.crt create mode 100644 examples/protocols/mqtt/publish_connect_test/client_no_pwd.key create mode 100644 examples/protocols/mqtt/publish_connect_test/client_pwd.crt create mode 100644 examples/protocols/mqtt/publish_connect_test/client_pwd.key create mode 100644 examples/protocols/mqtt/publish_connect_test/example_test.py create mode 100644 examples/protocols/mqtt/publish_connect_test/main/CMakeLists.txt rename examples/protocols/mqtt/{publish_test => publish_connect_test}/main/Kconfig.projbuild (63%) create mode 100644 examples/protocols/mqtt/publish_connect_test/main/component.mk create mode 100644 examples/protocols/mqtt/publish_connect_test/main/connect_test.c rename examples/protocols/mqtt/{publish_test => publish_connect_test}/main/mqtt_eclipse_org.pem (100%) create mode 100644 examples/protocols/mqtt/publish_connect_test/main/publish_connect_test.c create mode 100644 examples/protocols/mqtt/publish_connect_test/main/publish_test.c rename examples/protocols/mqtt/{publish_test => publish_connect_test}/sdkconfig.ci (100%) rename examples/protocols/mqtt/{publish_test => publish_connect_test}/sdkconfig.qemu (100%) create mode 100644 examples/protocols/mqtt/publish_connect_test/server.key delete mode 100644 examples/protocols/mqtt/publish_test/CMakeLists.txt delete mode 100644 examples/protocols/mqtt/publish_test/main/CMakeLists.txt delete mode 100644 examples/protocols/mqtt/publish_test/main/component.mk delete mode 100644 examples/protocols/mqtt/publish_test/main/publish_test.c 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 diff --git a/examples/protocols/mqtt/publish_connect_test/CMakeLists.txt b/examples/protocols/mqtt/publish_connect_test/CMakeLists.txt new file mode 100644 index 0000000..6d1ef5a --- /dev/null +++ b/examples/protocols/mqtt/publish_connect_test/CMakeLists.txt @@ -0,0 +1,19 @@ +# The following four lines of boilerplate have to be in your project's CMakeLists +# in this exact order for cmake to work correctly +cmake_minimum_required(VERSION 3.5) + +# (Not part of the boilerplate) +# This example uses an extra component for common functions such as Wi-Fi and Ethernet connection. +set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common) + +include($ENV{IDF_PATH}/tools/cmake/project.cmake) + +project(mqtt_publish_connect_test) + +target_add_binary_data(mqtt_publish_connect_test.elf "main/mqtt_eclipse_org.pem" TEXT) +target_add_binary_data(mqtt_publish_connect_test.elf "ca.crt" TEXT) +target_add_binary_data(mqtt_publish_connect_test.elf "ca.der" TEXT) +target_add_binary_data(mqtt_publish_connect_test.elf "client_pwd.key" TEXT) +target_add_binary_data(mqtt_publish_connect_test.elf "client_pwd.crt" TEXT) +target_add_binary_data(mqtt_publish_connect_test.elf "client_no_pwd.key" TEXT) +target_add_binary_data(mqtt_publish_connect_test.elf "client_inv.crt" TEXT) diff --git a/examples/protocols/mqtt/publish_test/Makefile b/examples/protocols/mqtt/publish_connect_test/Makefile similarity index 85% rename from examples/protocols/mqtt/publish_test/Makefile rename to examples/protocols/mqtt/publish_connect_test/Makefile index 7d552c3..49c193d 100644 --- a/examples/protocols/mqtt/publish_test/Makefile +++ b/examples/protocols/mqtt/publish_connect_test/Makefile @@ -2,7 +2,7 @@ # This is a project Makefile. It is assumed the directory this Makefile resides in is a # project subdirectory. # -PROJECT_NAME := mqtt_publish +PROJECT_NAME := mqtt_publish_connect_test EXTRA_COMPONENT_DIRS = $(IDF_PATH)/examples/common_components/protocol_examples_common diff --git a/examples/protocols/mqtt/publish_test/README.md b/examples/protocols/mqtt/publish_connect_test/README.md similarity index 98% rename from examples/protocols/mqtt/publish_test/README.md rename to examples/protocols/mqtt/publish_connect_test/README.md index ee377d5..2147420 100644 --- a/examples/protocols/mqtt/publish_test/README.md +++ b/examples/protocols/mqtt/publish_connect_test/README.md @@ -1,4 +1,4 @@ -# ESP-MQTT advanced published test +# ESP-MQTT advanced publish and connect test project (See the README.md file in the upper level 'examples' directory for more information about examples.) Main purpose of this example is to test the MQTT library to correctly publish and receive messages (of different size and sequences) over different transports. diff --git a/examples/protocols/mqtt/publish_connect_test/ca.crt b/examples/protocols/mqtt/publish_connect_test/ca.crt new file mode 100644 index 0000000..894f295 --- /dev/null +++ b/examples/protocols/mqtt/publish_connect_test/ca.crt @@ -0,0 +1,22 @@ +-----BEGIN CERTIFICATE----- +MIIDkzCCAnugAwIBAgIUNI5wldYysh6rtCzYmda6H414aRswDQYJKoZIhvcNAQEL +BQAwWTELMAkGA1UEBhMCQVUxEzARBgNVBAgMClNvbWUtU3RhdGUxITAfBgNVBAoM +GEludGVybmV0IFdpZGdpdHMgUHR5IEx0ZDESMBAGA1UEAwwJRXNwcmVzc2lmMB4X +DTIwMDEyMTA5MDk0NloXDTI1MDEyMDA5MDk0NlowWTELMAkGA1UEBhMCQVUxEzAR +BgNVBAgMClNvbWUtU3RhdGUxITAfBgNVBAoMGEludGVybmV0IFdpZGdpdHMgUHR5 +IEx0ZDESMBAGA1UEAwwJRXNwcmVzc2lmMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8A +MIIBCgKCAQEAyadSpRnIQBVbEAsbpkrKrOMlBOMIUmA8AfNyOYPLfv0Oa5lBiMAV +3OQDu5tYyFYKwkCUqq65iAm50fPbSH71w1tkja6nZ1yAIM+TvpMlM/WiFGrhY+Tc +kAcLcKUJyPxrv/glzoVslbqUgIhuhCSKA8uk1+ILcn3nWzPcbcowLx31+AHeZj8h +bIAdj6vjqxMCFStp4IcA+ikmCk75LCN4vkkifdkebb/ZDNYCZZhpCBnCHyFAjPc4 +7C+FDVGT3/UUeeTy+Mtn+MqUAhB+W0sPDm1n2h59D4Z/MFm0hl6GQCAKeMJPzssU +BBsRm6zoyPQ4VTqG0uwfNNbORyIfKONMUwIDAQABo1MwUTAdBgNVHQ4EFgQUGYLV +EkgWzxjpltE6texha7zZVxowHwYDVR0jBBgwFoAUGYLVEkgWzxjpltE6texha7zZ +VxowDwYDVR0TAQH/BAUwAwEB/zANBgkqhkiG9w0BAQsFAAOCAQEAb2EF4Zg2XWNb +eZHnzupCDd9jAhwPqkt7F1OXvxJa/RFUSB9+2izGvikGGhuKY4f0iLuqF+bhExD9 +sapDcdFO2Suh4J3onbwEvmKvsv56K3xhapYg8WwPofpkVirnkwFjpQXGzrYxPujg +BPmSy3psQrhvOr/WH7SefJv2qr4ikaugfE+3enY4PL+C1dSQAuNo1QGgWsZIu0c8 +TZybNZ13vNVMA+tgj2CM8FR3Etaabwtu3TTcAnO7aoBTix/bLBTuZoczhN8/MhG3 +GylmDzFI8a6aKxQL3Fi4PsM82hRKWu3gfs39sR1Ci4V22v8uO5EWBPK0QZvDSc1a +KwwxI4zA0w== +-----END CERTIFICATE----- diff --git a/examples/protocols/mqtt/publish_connect_test/ca.der b/examples/protocols/mqtt/publish_connect_test/ca.der new file mode 100644 index 0000000000000000000000000000000000000000..c5c3304e23747ca8143d9d92365c99ac752799ff GIT binary patch literal 919 zcmXqLVxDZ!#8kb2nTe5!NyMbDVCpraO>(QZ=-iljZI^s+MW(a?FB_*;n@8JsUPeZ4 zRtAGeLv903Hs(+kHen{mP(xt@K@f+7hbuTgH&r*dB(WsbP|-jhB*?`h;h9>9lVs zq7a^$lAc*otPoIAso+zRVkl%F08+xt!|7UFP?TC+oS9}IC(dhRU|?uuXkckzX<`;7 z&TC|9U}$6j<k210{FgG#sGZ-{6axpbAGBTW89<)^Qgo9|b0JroquTyIt ztFk=i2uiSF{9I(&e7f#0U-nGLjsv21o-ps89dROz>yX2gRqJ+kaPGYL`L;*h*Tc~% zz3Y~z$22IMpS*9fs`1xFB3TcUpWKJ1{dw8gbKC~#FA@;`T4ghg6#_L>(bzF34>wOx86Z*uLNyOO-dW1nCqW=00a#lZ%F z2C{6-p|X4|Vk{z(O;?3H#Li2+oOaP_>zl;vJvYOpz!5L2%pzeR)_`3BKS;kYBjbM- zRs&{6#{bB{&J7NBMuz-E)`v69Vw0mQCq6&-%8B=WGLsDdD(`CX;OYB?qW%hoc*xh? z(mA$IlTAvxE4lqk$L>|)&mIa3{N1?9x$vUjP3?sb=DwJ_hh<;V`c40;v}+QxrYU^P z;a~VGB~0u2WX9yBtjEr6Gqij0faT|;(^WZ6JMyjeUz6W5uV(hQRr{1Cu3k{%zrCu= z!e)Qd)hiR29%o!-To854W4F7F@0{7DbIbQ!^g|KNvwt_rI(4^} c-TJR*JyDG1(-z0shds|mY4aE=_Z+wk03*6t4gdfE literal 0 HcmV?d00001 diff --git a/examples/protocols/mqtt/publish_connect_test/ca.key b/examples/protocols/mqtt/publish_connect_test/ca.key new file mode 100644 index 0000000..d39ad7c --- /dev/null +++ b/examples/protocols/mqtt/publish_connect_test/ca.key @@ -0,0 +1,27 @@ +-----BEGIN RSA PRIVATE KEY----- +MIIEowIBAAKCAQEAyadSpRnIQBVbEAsbpkrKrOMlBOMIUmA8AfNyOYPLfv0Oa5lB +iMAV3OQDu5tYyFYKwkCUqq65iAm50fPbSH71w1tkja6nZ1yAIM+TvpMlM/WiFGrh +Y+TckAcLcKUJyPxrv/glzoVslbqUgIhuhCSKA8uk1+ILcn3nWzPcbcowLx31+AHe +Zj8hbIAdj6vjqxMCFStp4IcA+ikmCk75LCN4vkkifdkebb/ZDNYCZZhpCBnCHyFA +jPc47C+FDVGT3/UUeeTy+Mtn+MqUAhB+W0sPDm1n2h59D4Z/MFm0hl6GQCAKeMJP +zssUBBsRm6zoyPQ4VTqG0uwfNNbORyIfKONMUwIDAQABAoIBAQDEKH7NWcohBGUj +sxp/ZcvH5+FP4qVqtHBLGYyohBsE+Zb4dgl4xBnAWRGEgrYXkxM+KOI1MmgJ/CQF +JujNmarqEVI8PIRdmG6O/D1lKfALnkq+/8Umji87745iUji1iU4rXHEydznMYMYq +TgzrgDu9O3CsDBhElFLktgsbxY2flhRiQ9s/MJTXlfkHYVMbzB+WzbgwglZmzRRB +V9P7GDc1RPs3iUuab2BC0ajWSVWPCIE+WRQ8OTxeSz/Trp0S1y1WtxdMUDhg6wIe +xbTCYF6L6CRjdnnAiaFZuW+ECaeLOyy8sOTtzxIU/8in++x3+CJBaMLsvAG1e2K9 +7OLzz4KZAoGBAObHebhxfC9ue3hGC5IbE7BOaXBBdyHboXvajJKtyqpIDq7QLk2j +ktTuatigrzyCA8GgJUZCuOeWe/JhuzpFAsXN/lVs7ugw/6vQ+RGtJZkxzsYQDQjw +/3f4uWevsj3b28idxdMgstsw12a92pmH3TtKu7mMX2jJhYSu3wqbnCj/AoGBAN+w +/6nH4jLhSobunV+JT3YxwsMdyffLcdwXTcwjJrwGLwNvhPfAGtCCXidneYELCwCF +TbemR5VZadZrCdURWqwyHw8YMHBfN1hArD3MKqck+XK5Spxxr3pfNHvU3YIKWI1l +2h5sKoaPNUTQerLsJh9XG/zyc2Nl88hlFZucTGitAoGAPnKf518eIZ+d3Y/mtaSK +EV1Gfs/YMttBuUgMXeyVOLrC7l77CJtIskHJu9DPWmo8Jfr12ytW6aP46j+z8DKY +a3owZmFRzJbluFKV80iNMgUeVM4nGNJN7uLpGLucWczSjljTHSxt+Y4f23doXb88 +CD1SywTHFI3jiWHgjPhKq3UCgYATBZUoFeRJWVkiEkZ1qlKEhYS/XNgg5z7+bUjj +VBXmJUx4KVKQUti05HEnPqhZbdv4pl1OgahSrfDPF/kVEk24mOaFPRRZae9l5NIA +y0zRO9auh80tcoluiYwH/7j6ZvDSzVd4ANC48pKgEG5uqqAvSBQMNX3UdQX/A4GL +4wWoXQKBgBqtbOYpsoAbLTNV4e7AMZCCKlPYFvRDALbO2E/8MjdaIqBWQSiZBkQ6 +ALFYQaKJOwiLVXVv5B1FHz8MblqDOIaUpaujoA4NxdhYFLh2ui3fzuGTdGiPzOry +QHcIGifGSef45KnrF1LGvGvLMzX7Jx3xnAFOblJikN10wt1MLdNG +-----END RSA PRIVATE KEY----- diff --git a/examples/protocols/mqtt/publish_connect_test/client_inv.crt b/examples/protocols/mqtt/publish_connect_test/client_inv.crt new file mode 100644 index 0000000..e5042f3 --- /dev/null +++ b/examples/protocols/mqtt/publish_connect_test/client_inv.crt @@ -0,0 +1,19 @@ +-----BEGIN CERTIFICATE----- +MIIDGTCCAgECFBhepE4kbe+jKn9Qa4Fq7o73PL/AMA0GCSqGSIb3DQEBCwUAMIGD +MQswCQYDVQQGEwJDWjEOMAwGA1UECAwFQ3plY2gxDTALBgNVBAcMBEJybm8xEjAQ +BgNVBAoMCUVzcHJlc3NpZjELMAkGA1UECwwCU1cxFDASBgNVBAMMCzE5Mi4xNjgu +Mi4xMR4wHAYJKoZIhvcNAQkBFg9kYXZpZEBjZXJtYWsuY3owHhcNMjAwMTIyMTAy +MDM5WhcNMjkxMTMwMTAyMDM5WjAOMQwwCgYDVQQDDANlc3AwggEiMA0GCSqGSIb3 +DQEBAQUAA4IBDwAwggEKAoIBAQCtphtVYl80g2ldpmNY0NKcmabqWKGRC5PmabDU +wrqErvyOko68fpfw24bO2ndUyXdIgWtf4mbAFmIKajrNrACzMlDtfcZM2uYgJZdn +CYu/NNfDaFEb/ZyHIgkmVskLNxtv8QFTfaNCKK5Fhzz0iaFJnFAuQVX3kXIgOEN2 +8EmNxP53Eb9FeauVX+PBHhwJcGBruvWLPz8bfybHl51b80lCfddbow3z1zqGLESh +AzGc1j/5AEgEgL+iP3QX4qoLKYBgs/olv3rYor8I7oCYfsIpFCcvr/LEoaVYtCJP +PxbGu6KPx3p+r3MjaDStevdhLmzrKr35KczzTpUdOU3wgJA/AgMBAAEwDQYJKoZI +hvcNAQELBQADggEBABVwInNI7NPozIKXsshp6hfM0hchrAozYRT5sZSwusxQJ80t +Xhva0+RnBn3uaYG2YWULu+4QSgzjJ9vdRsFysuNN+7YpihwZrFHwvoRWes5C4ERE +Mzh5xeoZT1VNdz72KHvBytBUwb+h5FZQpfZ9O3yr2wBVHsi4d7/X5LVEmTxLte89 +sxoMGmSbqTKj7/UOfGZjBi2IuiHs1xOZxaZWW7RTgJiz9VGrhSpwb4j6G55HDhYA +4YODy3+epyVXAHVuy4zosQ8CCgdZgN0exB7pEwVQ21zIuyzrcQgQFPGQFbkXH0ow +1mgONA6PZ9YUZftJBmqBZoTVPLLQuE1aKbhBTAA= +-----END CERTIFICATE----- diff --git a/examples/protocols/mqtt/publish_connect_test/client_no_pwd.key b/examples/protocols/mqtt/publish_connect_test/client_no_pwd.key new file mode 100644 index 0000000..0d9d016 --- /dev/null +++ b/examples/protocols/mqtt/publish_connect_test/client_no_pwd.key @@ -0,0 +1,27 @@ +-----BEGIN RSA PRIVATE KEY----- +MIIEogIBAAKCAQEAraYbVWJfNINpXaZjWNDSnJmm6lihkQuT5mmw1MK6hK78jpKO +vH6X8NuGztp3VMl3SIFrX+JmwBZiCmo6zawAszJQ7X3GTNrmICWXZwmLvzTXw2hR +G/2chyIJJlbJCzcbb/EBU32jQiiuRYc89ImhSZxQLkFV95FyIDhDdvBJjcT+dxG/ +RXmrlV/jwR4cCXBga7r1iz8/G38mx5edW/NJQn3XW6MN89c6hixEoQMxnNY/+QBI +BIC/oj90F+KqCymAYLP6Jb962KK/CO6AmH7CKRQnL6/yxKGlWLQiTz8Wxruij8d6 +fq9zI2g0rXr3YS5s6yq9+SnM806VHTlN8ICQPwIDAQABAoIBAA4G8wJMtgAZ9XL5 +M+FCzSCVUORxUGvVEZd1RjGJoWOCdyhVMm6Lk16DfTpMb4NL2vTib3gJY990b2sD +9cgTcuMG1363wEMJE7nZD4flP4KslBlW3eZy8CgCWdbc/9SGGRNL1p2V8pAvlRRM +vmHKlFrL47Y41ObwutVbdievdWGcPBdF/sjpzskemVrEIPg7knHJ+MyxuIsjyaLV +Xdf3RR8yqtRggl+EP6m7JP4mbdlb/key8JTOHhe135JbuIwjUKTvm7UI+r9HwrDF +/4ysd6uEqAq4vD73VJ+7UR1LSWJ1hqoVyNP96QWPsReID/PB8TxbCPNOMX8WKz7U +WXOVomECgYEA1+54rF8QT/cpuqxR3ChbPaRGfwform0/nXZY7Xna4SqifQ2/gd6S +I97cktKCQzsnxqxrq8lV8kBlVBnqYWib+546WNjez32DUTsSu/0cIat649izbhw/ +a9piOttup3OSAmc6LZGgPtLB/Jhupl9F25SbwAxnYyUUSomqK4zSOvkCgYEAzd8O +MNZUGHREjD43UuoM+qyMa1vanZk+0mcNt6cL4MTKVHNyEPdCOg01QcAGTH5pumbs +mxU4yEPGn6RylQuXDi1UwFBsPLQTDRCnbjWkYBSNY3qKD0MlN+UIQ6/zUMaYHVL8 +xEfvJKxMiMdU4FhgDqoCTsK7BjnJ/bzgEdrHevcCgYAS7pOh+UvC1xbPiSA8P0WQ +qACOTrE16do0AhZV6+Mm7sgEUtpBlrQVdQq9zLsjDeK05pUiIKrqbH712rfUBon2 +i67t70XJx2VmD9napZx7zz8dDvjcZJmi6SjHpEmVYOqiT06ohCYam/vqG6tH5v6G +/AaT1gKSjMO0rVFAND6ScQKBgF459ZjMwHjg3m8CGvhMP9yMFUkeJZV0iphqqpCg +WINsDt9QZ6j0Qs+nM/UAGuHwChxS94CT2gVvX/25mug1AdJvVRcguCmgkges07VR +wAZp4bziXUZXCTXoEjxI0CjsfLsPPLnp4r76TZ1c/rAgQvbzQVMjNc7HrHgCdtw1 +MpBJAoGAEdeW0t5kHrHHYlFqwUHYnwD6DbNl4TzTumZrI+uEkrH1EAQ4BpIk5Gwx +OOWw8m+x2AMtBk+XcElMGZbX98msB2KcHbQMuxLF3xbCB5ihm6sKxk0HLvf2kygP +k+DYz710yKq9zRwoGOeM52oTrA4hIpQKBnpjCXg9QigD0yoWOjI= +-----END RSA PRIVATE KEY----- diff --git a/examples/protocols/mqtt/publish_connect_test/client_pwd.crt b/examples/protocols/mqtt/publish_connect_test/client_pwd.crt new file mode 100644 index 0000000..50c291a --- /dev/null +++ b/examples/protocols/mqtt/publish_connect_test/client_pwd.crt @@ -0,0 +1,18 @@ +-----BEGIN CERTIFICATE----- +MIIC7jCCAdYCFDLr5uidhcbJKmiKv+FoJ2lyh2ZkMA0GCSqGSIb3DQEBCwUAMFkx +CzAJBgNVBAYTAkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEwHwYDVQQKDBhJbnRl +cm5ldCBXaWRnaXRzIFB0eSBMdGQxEjAQBgNVBAMMCUVzcHJlc3NpZjAeFw0yMDAx +MjIxMDA1MThaFw0yOTExMzAxMDA1MThaMA4xDDAKBgNVBAMMA2VzcDCCASIwDQYJ +KoZIhvcNAQEBBQADggEPADCCAQoCggEBAK2mG1ViXzSDaV2mY1jQ0pyZpupYoZEL +k+ZpsNTCuoSu/I6Sjrx+l/Dbhs7ad1TJd0iBa1/iZsAWYgpqOs2sALMyUO19xkza +5iAll2cJi78018NoURv9nIciCSZWyQs3G2/xAVN9o0IorkWHPPSJoUmcUC5BVfeR +ciA4Q3bwSY3E/ncRv0V5q5Vf48EeHAlwYGu69Ys/Pxt/JseXnVvzSUJ911ujDfPX +OoYsRKEDMZzWP/kASASAv6I/dBfiqgspgGCz+iW/etiivwjugJh+wikUJy+v8sSh +pVi0Ik8/Fsa7oo/Hen6vcyNoNK1692EubOsqvfkpzPNOlR05TfCAkD8CAwEAATAN +BgkqhkiG9w0BAQsFAAOCAQEAxRyJBo1GP5K5BFvJDeXcAJjUbzFUqY4pUsbodkIZ +NJj69ZulxARISKsqaVhjR0m4CXZMRVhimRUJJBnjgTYidWF/xsbxgmEoURqV9SAL +puYMOm3t9Fi/81iQSzvT6ZLq5bQmxCFpZ6jl+nM28KqqldhRJApgXLOvc9i/N1FW +7ceZPw33vfzKxk91rVl9vv+wBLS/jeF++6wscIeFW+HjDj2F84JEENltoEpQz4Y1 +VWYJ2G5Fu0MaxObwMrEqIxKAq7X96pyIocO6vINg0UWNp3kbPBSzD1i5FLqx96fQ +kfykJJN1kUsrJk1PskxnWq2vckdSv+jLOiF7hoHRG7+69A== +-----END CERTIFICATE----- diff --git a/examples/protocols/mqtt/publish_connect_test/client_pwd.key b/examples/protocols/mqtt/publish_connect_test/client_pwd.key new file mode 100644 index 0000000..ce98682 --- /dev/null +++ b/examples/protocols/mqtt/publish_connect_test/client_pwd.key @@ -0,0 +1,30 @@ +-----BEGIN RSA PRIVATE KEY----- +Proc-Type: 4,ENCRYPTED +DEK-Info: AES-256-CBC,6FFF58C7A652F8E0D7E31F0CCAA27E98 + +ZUeJ8iCLa7bDf8xQsL3NtDK/oR2z8EWapoqGru/zOvxAeHWoLdMko1bouMkEXn+h +g4GigyXo9f3jdyFKeqf9TtXonL6vguWczFc+81ynE5w3iqqcmdP8SD8k3o07lBc/ +hfRIwsg4Ywda3luQBWT63LIx7GnQjHRjK/KkPkFkYG6LDwshWyf1L7NxRMCrkejA +3B7KqlRTEia9xR0WH//ZGOdipp2MSyUGC46BgXJQ1rDJo1D+3NUTZ8RmMkqZNmJK +z3HMMflB4khmh2HkdM10VaErpiphitP52aTbGu/aschDOvSHFYMNf916u8UlyGQJ +4m/pgSz+aAfReGRDqCcojkAf/BR4+n7QYfFtxjJ5P4ul4ZnhB30synknet6+9bKU +Ht1SkavyJmruW2sfIcvPadLVm+xROnGr3ZWs82BGdPPu4wIYRWBn4S7huELXSSC2 +qLCTWeLB+pV//+9E9EmrvwsTJBNMCkjAmmTdmofm8UisAGsZlvyIDJVlpUsUKOoU +cHGc2x9hj5OhgK++0JOY2ftowFX69FNRYNYHB5Vw6/5Teo3eGxVYoMeosADo7pXa +j17g7dywDLRBZuTfIkFcYyfRV4A/xcagZJ8m/rnSDphGHYkM1d+MoXAPhj6A+iAa +2qZkPyYdd0OkWvCY42F2xsjxzYjmxvkkNLDiYa7zj3ofb/1mqdNc8/k2AbFObX4v +LenDFyqmzeYfzHcJ2Z4edwx1TFFMlHb+knuYjSzcBceWoQ5nOi+uipgZVJEEKC8o +vTUBeaDnRblNIGlvuWnZFN0OHV1BduF5TyKUh2YGBw/X4qgCgrnCR2I8HGZMkUNe +UTDF9vpxnDke5BEhzkwb2R34Kvn7ac1p4NDdXOrCwEqZTt93FgQTgOuklPb39CcZ +yQHfl1Vr0EgfOlcE+ngbUrU93/qsEADXyjM8D4ZGB5+tQZGWlz9nc8PqldC3txux +Hce4bk98hOgZUy/fEozE6fODxBJCuIi2ab/6WU8j62UAIumw7fvgti+zor6eJdas +QCS+UVj5fJtYc5E/7Urih7Ixkltu0atqR5p81xn8U94/6KGYUCcsaZ7UZnWfD4dF +0qEkdHravD82Z38eBpt/kqIE7bHs3orvt7WSCBYZQtkdQysYy+lXEAwF3eIpCqAW +EcdHZ10zENvSHfzot1yK2rk9uuD+KqyK80xCSh9auLBSdJHCNeMtaozpCg5jLhYu +mW4zSQA4awuD58k/+xAnOzXtNCPg6TPQ7GNzX/33W9R2ceRtkbNmd68YEmkZ7tZo +s/3DAuxjid5W+wbsJHBvnUQh9g5BMF18k0SYS3bFPy62eycQziGfEQIwcgaHqAt5 +SX4URYqDLN0k+68CAQfm2JAS8kOBdo9TAKsluThp/uRC1oh3DW/D0xctDAApDbdd +oNnK44XSYfMsBaGjm+NFQYTndFDpSd4+eQcVWAaXTdyRGMC6jbc9XijdzJnnKbEP +M38+5dUaZFPyONieb7VfciH1WKM0GfxHAFxpiEgXd0dwS8Jqq9sLY6drKZP5+b9a +9KMXI3jBLVqPg9yvx8E7THjF9ihsheVhEcJ6Nywhopab9Et+wvFa9upl2TJ2xGIj +-----END RSA PRIVATE KEY----- diff --git a/examples/protocols/mqtt/publish_connect_test/example_test.py b/examples/protocols/mqtt/publish_connect_test/example_test.py new file mode 100644 index 0000000..ccc3e06 --- /dev/null +++ b/examples/protocols/mqtt/publish_connect_test/example_test.py @@ -0,0 +1,230 @@ +from __future__ import print_function +from __future__ import unicode_literals +import re +import os +import socket +import select +import subprocess +from threading import Thread, Event +import ttfw_idf +import ssl + + +def _path(f): + return os.path.join(os.path.dirname(os.path.realpath(__file__)),f) + + +def set_server_cert_cn(ip): + arg_list = [ + ['openssl', 'req', '-out', _path('srv.csr'), '-key', _path('server.key'),'-subj', "/CN={}".format(ip), '-new'], + ['openssl', 'x509', '-req', '-in', _path('srv.csr'), '-CA', _path('ca.crt'), + '-CAkey', _path('ca.key'), '-CAcreateserial', '-out', _path('srv.crt'), '-days', '360']] + for args in arg_list: + if subprocess.check_call(args) != 0: + raise("openssl command {} failed".format(args)) + + +def get_my_ip(): + s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + try: + # doesn't even have to be reachable + s.connect(('10.255.255.255', 1)) + IP = s.getsockname()[0] + except Exception: + IP = '127.0.0.1' + finally: + s.close() + return IP + + +# Simple server for mqtt over TLS connection +class TlsServer: + + def __init__(self, port, client_cert=False, refuse_connection=False, use_alpn=False): + self.port = port + self.socket = socket.socket() + self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) + self.socket.settimeout(10.0) + self.shutdown = Event() + self.client_cert = client_cert + self.refuse_connection = refuse_connection + self.ssl_error = None + self.use_alpn = use_alpn + self.negotiated_protocol = None + + def __enter__(self): + try: + self.socket.bind(('', self.port)) + except socket.error as e: + print("Bind failed:{}".format(e)) + raise + + self.socket.listen(1) + self.server_thread = Thread(target=self.run_server) + self.server_thread.start() + + return self + + def __exit__(self, exc_type, exc_value, traceback): + self.shutdown.set() + self.server_thread.join() + self.socket.close() + if (self.conn is not None): + self.conn.close() + + def get_last_ssl_error(self): + return self.ssl_error + + def get_negotiated_protocol(self): + return self.negotiated_protocol + + def run_server(self): + context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH) + if self.client_cert: + context.verify_mode = ssl.CERT_REQUIRED + context.load_verify_locations(cafile=_path("ca.crt")) + context.load_cert_chain(certfile=_path("srv.crt"), keyfile=_path("server.key")) + if self.use_alpn: + context.set_alpn_protocols(["mymqtt", "http/1.1"]) + self.socket = context.wrap_socket(self.socket, server_side=True) + try: + self.conn, address = self.socket.accept() # accept new connection + self.socket.settimeout(10.0) + print(" - connection from: {}".format(address)) + if self.use_alpn: + self.negotiated_protocol = self.conn.selected_alpn_protocol() + print(" - negotiated_protocol: {}".format(self.negotiated_protocol)) + self.handle_conn() + except ssl.SSLError as e: + self.conn = None + self.ssl_error = str(e) + print(" - SSLError: {}".format(str(e))) + + def handle_conn(self): + while not self.shutdown.is_set(): + r,w,e = select.select([self.conn], [], [], 1) + try: + if self.conn in r: + self.process_mqtt_connect() + + except socket.error as err: + print(" - error: {}".format(err)) + raise + + def process_mqtt_connect(self): + try: + data = bytearray(self.conn.recv(1024)) + message = ''.join(format(x, '02x') for x in data) + if message[0:16] == '101800044d515454': + if self.refuse_connection is False: + print(" - received mqtt connect, sending ACK") + self.conn.send(bytearray.fromhex("20020000")) + else: + # injecting connection not authorized error + print(" - received mqtt connect, sending NAK") + self.conn.send(bytearray.fromhex("20020005")) + else: + raise Exception(" - error process_mqtt_connect unexpected connect received: {}".format(message)) + finally: + # stop the server after the connect message in happy flow, or if any exception occur + self.shutdown.set() + + +@ttfw_idf.idf_example_test(env_tag="Example_WIFI") +def test_examples_protocol_mqtt_publish_connect(env, extra_data): + """ + steps: + 1. join AP + 2. connect to uri specified in the config + 3. send and receive data + """ + dut1 = env.get_dut("mqtt_publish_connect_test", "examples/protocols/mqtt/publish_connect_test", dut_class=ttfw_idf.ESP32DUT) + # check and log bin size + binary_file = os.path.join(dut1.app.binary_path, "mqtt_publish_connect_test.bin") + bin_size = os.path.getsize(binary_file) + ttfw_idf.log_performance("mqtt_publish_connect_test_bin_size", "{}KB".format(bin_size // 1024)) + ttfw_idf.check_performance("mqtt_publish_connect_test_bin_size_vin_size", bin_size // 1024) + # Look for test case symbolic names + cases = {} + try: + for i in ["CONFIG_EXAMPLE_CONNECT_CASE_NO_CERT", + "CONFIG_EXAMPLE_CONNECT_CASE_SERVER_CERT", + "CONFIG_EXAMPLE_CONNECT_CASE_MUTUAL_AUTH", + "CONFIG_EXAMPLE_CONNECT_CASE_INVALID_SERVER_CERT", + "CONFIG_EXAMPLE_CONNECT_CASE_SERVER_DER_CERT", + "CONFIG_EXAMPLE_CONNECT_CASE_MUTUAL_AUTH_KEY_PWD", + "CONFIG_EXAMPLE_CONNECT_CASE_MUTUAL_AUTH_BAD_CRT", + "CONFIG_EXAMPLE_CONNECT_CASE_NO_CERT_ALPN"]: + cases[i] = dut1.app.get_sdkconfig()[i] + except Exception: + print('ENV_TEST_FAILURE: Some mandatory test case not found in sdkconfig') + raise + + dut1.start_app() + esp_ip = dut1.expect(re.compile(r" IPv4 address: ([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)"), timeout=30) + print("Got IP={}".format(esp_ip[0])) + # + # start connection test + ip = get_my_ip() + set_server_cert_cn(ip) + server_port = 2222 + + def start_case(case, desc): + print("Starting {}: {}".format(case, desc)) + case_id = cases[case] + dut1.write("conn {} {} {}".format(ip, server_port, case_id)) + dut1.expect("Test case:{} started".format(case_id)) + return case_id + + for case in ["CONFIG_EXAMPLE_CONNECT_CASE_NO_CERT", "CONFIG_EXAMPLE_CONNECT_CASE_SERVER_CERT", "CONFIG_EXAMPLE_CONNECT_CASE_SERVER_DER_CERT"]: + # All these cases connect to the server with no server verification or with server only verification + with TlsServer(server_port): + test_nr = start_case(case, "default server - expect to connect normally") + dut1.expect("MQTT_EVENT_CONNECTED: Test={}".format(test_nr), timeout=30) + with TlsServer(server_port, refuse_connection=True): + test_nr = start_case(case, "ssl shall connect, but mqtt sends connect refusal") + dut1.expect("MQTT_EVENT_ERROR: Test={}".format(test_nr), timeout=30) + dut1.expect("MQTT ERROR: 0x5") # expecting 0x5 ... connection not authorized error + with TlsServer(server_port, client_cert=True) as s: + test_nr = start_case(case, "server with client verification - handshake error since client presents no client certificate") + dut1.expect("MQTT_EVENT_ERROR: Test={}".format(test_nr), timeout=30) + dut1.expect("ESP-TLS ERROR: 0x8010") # expect ... handshake error (PEER_DID_NOT_RETURN_A_CERTIFICATE) + if "PEER_DID_NOT_RETURN_A_CERTIFICATE" not in s.get_last_ssl_error(): + raise("Unexpected ssl error from the server {}".format(s.get_last_ssl_error())) + + for case in ["CONFIG_EXAMPLE_CONNECT_CASE_MUTUAL_AUTH", "CONFIG_EXAMPLE_CONNECT_CASE_MUTUAL_AUTH_KEY_PWD"]: + # These cases connect to server with both server and client verification (client key might be password protected) + with TlsServer(server_port, client_cert=True): + test_nr = start_case(case, "server with client verification - expect to connect normally") + dut1.expect("MQTT_EVENT_CONNECTED: Test={}".format(test_nr), timeout=30) + + case = "CONFIG_EXAMPLE_CONNECT_CASE_INVALID_SERVER_CERT" + with TlsServer(server_port) as s: + test_nr = start_case(case, "invalid server certificate on default server - expect ssl handshake error") + dut1.expect("MQTT_EVENT_ERROR: Test={}".format(test_nr), timeout=30) + dut1.expect("ESP-TLS ERROR: 0x8010") # expect ... handshake error (TLSV1_ALERT_UNKNOWN_CA) + if "alert unknown ca" not in s.get_last_ssl_error(): + raise Exception("Unexpected ssl error from the server {}".format(s.get_last_ssl_error())) + + case = "CONFIG_EXAMPLE_CONNECT_CASE_MUTUAL_AUTH_BAD_CRT" + with TlsServer(server_port, client_cert=True) as s: + test_nr = start_case(case, "Invalid client certificate on server with client verification - expect ssl handshake error") + dut1.expect("MQTT_EVENT_ERROR: Test={}".format(test_nr), timeout=30) + dut1.expect("ESP-TLS ERROR: 0x8010") # expect ... handshake error (CERTIFICATE_VERIFY_FAILED) + if "CERTIFICATE_VERIFY_FAILED" not in s.get_last_ssl_error(): + raise Exception("Unexpected ssl error from the server {}".format(s.get_last_ssl_error())) + + for case in ["CONFIG_EXAMPLE_CONNECT_CASE_NO_CERT", "CONFIG_EXAMPLE_CONNECT_CASE_NO_CERT_ALPN"]: + with TlsServer(server_port, use_alpn=True) as s: + test_nr = start_case(case, "server with alpn - expect connect, check resolved protocol") + dut1.expect("MQTT_EVENT_CONNECTED: Test={}".format(test_nr), timeout=30) + if case == "CONFIG_EXAMPLE_CONNECT_CASE_NO_CERT" and s.get_negotiated_protocol() is None: + print(" - client with alpn off, no negotiated protocol: OK") + elif case == "CONFIG_EXAMPLE_CONNECT_CASE_NO_CERT_ALPN" and s.get_negotiated_protocol() == "mymqtt": + print(" - client with alpn on, negotiated protocol resolved: OK") + else: + raise Exception("Unexpected negotiated protocol {}".format(s.get_negotiated_protocol())) + + +if __name__ == '__main__': + test_examples_protocol_mqtt_publish_connect() diff --git a/examples/protocols/mqtt/publish_connect_test/main/CMakeLists.txt b/examples/protocols/mqtt/publish_connect_test/main/CMakeLists.txt new file mode 100644 index 0000000..85e306a --- /dev/null +++ b/examples/protocols/mqtt/publish_connect_test/main/CMakeLists.txt @@ -0,0 +1,2 @@ +idf_component_register(SRCS "publish_test.c" "connect_test.c" "publish_connect_test.c" + INCLUDE_DIRS ".") \ No newline at end of file diff --git a/examples/protocols/mqtt/publish_test/main/Kconfig.projbuild b/examples/protocols/mqtt/publish_connect_test/main/Kconfig.projbuild similarity index 63% rename from examples/protocols/mqtt/publish_test/main/Kconfig.projbuild rename to examples/protocols/mqtt/publish_connect_test/main/Kconfig.projbuild index bf41974..874db25 100644 --- a/examples/protocols/mqtt/publish_test/main/Kconfig.projbuild +++ b/examples/protocols/mqtt/publish_connect_test/main/Kconfig.projbuild @@ -47,4 +47,39 @@ menu "Example Configuration" bool default y if EXAMPLE_BROKER_CERTIFICATE_OVERRIDE != "" + config EXAMPLE_CONNECT_CASE_NO_CERT + # Note: All the below config values (EXAMPLE_CONNECT_CASE...) are hidden and + # used to give symbolic names to test cases, which are then referenced from both + # the embedded C code as well as the test counterpart in python + int + default 1 + + config EXAMPLE_CONNECT_CASE_SERVER_CERT + int + default 2 + + config EXAMPLE_CONNECT_CASE_MUTUAL_AUTH + int + default 3 + + config EXAMPLE_CONNECT_CASE_INVALID_SERVER_CERT + int + default 4 + + config EXAMPLE_CONNECT_CASE_SERVER_DER_CERT + int + default 5 + + config EXAMPLE_CONNECT_CASE_MUTUAL_AUTH_KEY_PWD + int + default 6 + + config EXAMPLE_CONNECT_CASE_MUTUAL_AUTH_BAD_CRT + int + default 7 + + config EXAMPLE_CONNECT_CASE_NO_CERT_ALPN + int + default 8 + endmenu diff --git a/examples/protocols/mqtt/publish_connect_test/main/component.mk b/examples/protocols/mqtt/publish_connect_test/main/component.mk new file mode 100644 index 0000000..ba6d986 --- /dev/null +++ b/examples/protocols/mqtt/publish_connect_test/main/component.mk @@ -0,0 +1 @@ +COMPONENT_EMBED_TXTFILES := mqtt_eclipse_org.pem ../ca.crt ../ca.der ../client_pwd.key ../client_pwd.crt ../client_no_pwd.key ../client_inv.crt diff --git a/examples/protocols/mqtt/publish_connect_test/main/connect_test.c b/examples/protocols/mqtt/publish_connect_test/main/connect_test.c new file mode 100644 index 0000000..7cee6bd --- /dev/null +++ b/examples/protocols/mqtt/publish_connect_test/main/connect_test.c @@ -0,0 +1,237 @@ +/* MQTT connect test + + This example 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 "esp_netif.h" + +#include "esp_log.h" +#include "mqtt_client.h" +#include "esp_tls.h" + +#if (!defined(CONFIG_EXAMPLE_CONNECT_CASE_NO_CERT)) || \ + (!defined(CONFIG_EXAMPLE_CONNECT_CASE_SERVER_CERT)) || \ + (!defined(CONFIG_EXAMPLE_CONNECT_CASE_MUTUAL_AUTH)) || \ + (!defined(CONFIG_EXAMPLE_CONNECT_CASE_INVALID_SERVER_CERT)) || \ + (!defined(CONFIG_EXAMPLE_CONNECT_CASE_SERVER_DER_CERT)) || \ + (!defined(CONFIG_EXAMPLE_CONNECT_CASE_MUTUAL_AUTH_KEY_PWD)) || \ + (!defined(CONFIG_EXAMPLE_CONNECT_CASE_MUTUAL_AUTH_BAD_CRT)) || \ + (!defined(CONFIG_EXAMPLE_CONNECT_CASE_NO_CERT_ALPN)) +#error "Some mandatory test case not defined!" +#endif + +extern const uint8_t ca_local_crt[] asm("_binary_ca_crt_start"); +extern const uint8_t ca_der_start[] asm("_binary_ca_der_start"); +extern const uint8_t ca_der_end[] asm("_binary_ca_der_end"); +extern const uint8_t client_pwd_crt[] asm("_binary_client_pwd_crt_start"); +extern const uint8_t client_pwd_key[] asm("_binary_client_pwd_key_start"); +extern const uint8_t client_inv_crt[] asm("_binary_client_inv_crt_start"); +extern const uint8_t client_no_pwd_key[] asm("_binary_client_no_pwd_key_start"); + +static const char *TAG = "CONNECT_TEST"; +static esp_mqtt_client_handle_t mqtt_client = NULL; +static int running_test_case = 0; + +static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data) +{ + esp_mqtt_event_handle_t event = event_data; + ESP_LOGD(TAG, "Event: %d, Test case: %d", event->event_id, running_test_case); + switch (event->event_id) { + case MQTT_EVENT_CONNECTED: + ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED: Test=%d", running_test_case); + break; + case MQTT_EVENT_ERROR: + ESP_LOGI(TAG, "MQTT_EVENT_ERROR: Test=%d", running_test_case); + if (event->error_handle->error_type == MQTT_ERROR_TYPE_ESP_TLS) { + ESP_LOGI(TAG, "ESP-TLS ERROR: 0x%x", event->error_handle->esp_tls_last_esp_err); + } else if (event->error_handle->error_type == MQTT_ERROR_TYPE_CONNECTION_REFUSED) { + ESP_LOGI(TAG, "MQTT ERROR: 0x%x", event->error_handle->connect_return_code); + } else { + ESP_LOGW(TAG, "Unknown error type: 0x%x", event->error_handle->error_type); + } + break; + default: + ESP_LOGI(TAG, "Other event id:%d", event->event_id); + break; + } +} + +static void create_client(void) +{ + const esp_mqtt_client_config_t mqtt_cfg = { + .uri = "mqtts://127.0.0.1:1234" + }; + esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg); + esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, client); + mqtt_client = client; + esp_mqtt_client_start(client); +} + +static void connect_no_certs(const char *host, const int port) +{ + char uri[64]; + sprintf(uri, "mqtts://%s:%d", host, port); + const esp_mqtt_client_config_t mqtt_cfg = { + .uri = uri + }; + esp_mqtt_set_config(mqtt_client, &mqtt_cfg); + esp_mqtt_client_disconnect(mqtt_client); + esp_mqtt_client_reconnect(mqtt_client); +} + +static void connect_with_client_key_password(const char *host, const int port) +{ + char uri[64]; + sprintf(uri, "mqtts://%s:%d", host, port); + const esp_mqtt_client_config_t mqtt_cfg = { + .uri = uri, + .cert_pem = (const char *)ca_local_crt, + .client_cert_pem = (const char *)client_pwd_crt, + .client_key_pem = (const char *)client_pwd_key, + .clientkey_password = "esp32", + .clientkey_password_len = 5 + }; + esp_mqtt_set_config(mqtt_client, &mqtt_cfg); + esp_mqtt_client_disconnect(mqtt_client); + esp_mqtt_client_reconnect(mqtt_client); +} + +static void connect_with_server_der_cert(const char *host, const int port) +{ + char uri[64]; + sprintf(uri, "mqtts://%s:%d", host, port); + const esp_mqtt_client_config_t mqtt_cfg = { + .uri = uri, + .cert_pem = (const char *)ca_der_start, + .cert_len = ca_der_end - ca_der_start, + .client_cert_pem = "NULL", + .client_key_pem = "NULL" + }; + esp_mqtt_set_config(mqtt_client, &mqtt_cfg); + esp_mqtt_client_disconnect(mqtt_client); + esp_mqtt_client_reconnect(mqtt_client); +} + +static void connect_with_wrong_server_cert(const char *host, const int port) +{ + char uri[64]; + sprintf(uri, "mqtts://%s:%d", host, port); + const esp_mqtt_client_config_t mqtt_cfg = { + .uri = uri, + .cert_pem = (const char *)client_pwd_crt, + .client_cert_pem = "NULL", + .client_key_pem = "NULL" + }; + esp_mqtt_set_config(mqtt_client, &mqtt_cfg); + esp_mqtt_client_disconnect(mqtt_client); + esp_mqtt_client_reconnect(mqtt_client); +} + +static void connect_with_server_cert(const char *host, const int port) +{ + char uri[64]; + sprintf(uri, "mqtts://%s:%d", host, port); + const esp_mqtt_client_config_t mqtt_cfg = { + .uri = uri, + .cert_pem = (const char *)ca_local_crt, + }; + esp_mqtt_set_config(mqtt_client, &mqtt_cfg); + esp_mqtt_client_disconnect(mqtt_client); + esp_mqtt_client_reconnect(mqtt_client); +} + +static void connect_with_server_client_certs(const char *host, const int port) +{ + char uri[64]; + sprintf(uri, "mqtts://%s:%d", host, port); + const esp_mqtt_client_config_t mqtt_cfg = { + .uri = uri, + .cert_pem = (const char *)ca_local_crt, + .client_cert_pem = (const char *)client_pwd_crt, + .client_key_pem = (const char *)client_no_pwd_key + }; + esp_mqtt_set_config(mqtt_client, &mqtt_cfg); + esp_mqtt_client_disconnect(mqtt_client); + esp_mqtt_client_reconnect(mqtt_client); +} + +static void connect_with_invalid_client_certs(const char *host, const int port) +{ + char uri[64]; + sprintf(uri, "mqtts://%s:%d", host, port); + const esp_mqtt_client_config_t mqtt_cfg = { + .uri = uri, + .cert_pem = (const char *)ca_local_crt, + .client_cert_pem = (const char *)client_inv_crt, + .client_key_pem = (const char *)client_no_pwd_key + }; + esp_mqtt_set_config(mqtt_client, &mqtt_cfg); + esp_mqtt_client_disconnect(mqtt_client); + esp_mqtt_client_reconnect(mqtt_client); +} + +static void connect_with_alpn(const char *host, const int port) +{ + char uri[64]; + const char *alpn_protos[] = { "mymqtt", NULL }; + sprintf(uri, "mqtts://%s:%d", host, port); + const esp_mqtt_client_config_t mqtt_cfg = { + .uri = uri, + .alpn_protos = alpn_protos + }; + esp_mqtt_set_config(mqtt_client, &mqtt_cfg); + esp_mqtt_client_disconnect(mqtt_client); + esp_mqtt_client_reconnect(mqtt_client); +} + +void connection_test(const char* line) +{ + char test_type[32]; + char host[32]; + int port; + int test_case; + + sscanf(line, "%s %s %d %d", test_type, host, &port, &test_case); + if (mqtt_client == NULL) { + create_client(); + } + ESP_LOGI(TAG, "CASE:%d, connecting to mqtts://%s:%d ", test_case, host, port); + running_test_case = test_case; + switch (test_case) { + case CONFIG_EXAMPLE_CONNECT_CASE_NO_CERT: + connect_no_certs(host, port); + break; + case CONFIG_EXAMPLE_CONNECT_CASE_SERVER_CERT: + connect_with_server_cert(host, port); + break; + case CONFIG_EXAMPLE_CONNECT_CASE_MUTUAL_AUTH: + connect_with_server_client_certs(host, port); + break; + case CONFIG_EXAMPLE_CONNECT_CASE_INVALID_SERVER_CERT: + connect_with_wrong_server_cert(host, port); + break; + case CONFIG_EXAMPLE_CONNECT_CASE_SERVER_DER_CERT: + connect_with_server_der_cert(host, port); + break; + case CONFIG_EXAMPLE_CONNECT_CASE_MUTUAL_AUTH_KEY_PWD: + connect_with_client_key_password(host, port); + break; + case CONFIG_EXAMPLE_CONNECT_CASE_MUTUAL_AUTH_BAD_CRT: + connect_with_invalid_client_certs(host, port); + break; + case CONFIG_EXAMPLE_CONNECT_CASE_NO_CERT_ALPN: + connect_with_alpn(host, port); + break; + default: + ESP_LOGE(TAG, "Unknown test case %d ", test_case); + break; + } + ESP_LOGI(TAG, "Test case:%d started", test_case); +} + + diff --git a/examples/protocols/mqtt/publish_test/main/mqtt_eclipse_org.pem b/examples/protocols/mqtt/publish_connect_test/main/mqtt_eclipse_org.pem similarity index 100% rename from examples/protocols/mqtt/publish_test/main/mqtt_eclipse_org.pem rename to examples/protocols/mqtt/publish_connect_test/main/mqtt_eclipse_org.pem diff --git a/examples/protocols/mqtt/publish_connect_test/main/publish_connect_test.c b/examples/protocols/mqtt/publish_connect_test/main/publish_connect_test.c new file mode 100644 index 0000000..fa7336c --- /dev/null +++ b/examples/protocols/mqtt/publish_connect_test/main/publish_connect_test.c @@ -0,0 +1,76 @@ +/* MQTT publish-connect test + + This example 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 +#include +#include "esp_system.h" +#include "nvs_flash.h" +#include "esp_event.h" +#include "esp_netif.h" +#include "protocol_examples_common.h" +#include "esp_log.h" + +static const char *TAG = "PUBLISH_CONNECT_TEST"; + +void connection_test(const char* line); +void publish_test(const char* line); + +static void get_string(char *line, size_t size) +{ + int count = 0; + while (count < size) { + int c = fgetc(stdin); + if (c == '\n') { + line[count] = '\0'; + break; + } else if (c > 0 && c < 127) { + line[count] = c; + ++count; + } + vTaskDelay(10 / portTICK_PERIOD_MS); + } +} + +void app_main(void) +{ + char line[256]; + + ESP_LOGI(TAG, "[APP] Free memory: %d bytes", esp_get_free_heap_size()); + ESP_LOGI(TAG, "[APP] IDF version: %s", esp_get_idf_version()); + + esp_log_level_set("*", ESP_LOG_INFO); + esp_log_level_set("MQTT_CLIENT", ESP_LOG_VERBOSE); + esp_log_level_set("TRANSPORT_TCP", ESP_LOG_VERBOSE); + esp_log_level_set("TRANSPORT_SSL", ESP_LOG_VERBOSE); + esp_log_level_set("TRANSPORT", ESP_LOG_VERBOSE); + esp_log_level_set("OUTBOX", ESP_LOG_VERBOSE); + + ESP_ERROR_CHECK(nvs_flash_init()); + ESP_ERROR_CHECK(esp_netif_init()); + ESP_ERROR_CHECK(esp_event_loop_create_default()); + + /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig. + * Read "Establishing Wi-Fi or Ethernet Connection" section in + * examples/protocols/README.md for more information about this function. + */ + ESP_ERROR_CHECK(example_connect()); + + while (1) { + get_string(line, sizeof(line)); + if (memcmp(line, "conn", 4) == 0) { + // line starting with "conn" indicate connection tests + connection_test(line); + get_string(line, sizeof(line)); + continue; + } else { + publish_test(line); + } + } + +} \ No newline at end of file diff --git a/examples/protocols/mqtt/publish_connect_test/main/publish_test.c b/examples/protocols/mqtt/publish_connect_test/main/publish_test.c new file mode 100644 index 0000000..c78a84e --- /dev/null +++ b/examples/protocols/mqtt/publish_connect_test/main/publish_test.c @@ -0,0 +1,175 @@ +/* MQTT publish test + + This example 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 +#include +#include +#include "esp_system.h" +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "freertos/event_groups.h" + +#include "esp_log.h" +#include "mqtt_client.h" + +static const char *TAG = "PUBLISH_TEST"; + +static EventGroupHandle_t mqtt_event_group; +const static int CONNECTED_BIT = BIT0; + +static esp_mqtt_client_handle_t mqtt_client = NULL; + +static char *expected_data = NULL; +static char *actual_data = NULL; +static size_t expected_size = 0; +static size_t expected_published = 0; +static size_t actual_published = 0; +static int qos_test = 0; + + #if CONFIG_EXAMPLE_BROKER_CERTIFICATE_OVERRIDDEN == 1 + static const uint8_t mqtt_eclipse_org_pem_start[] = "-----BEGIN CERTIFICATE-----\n" CONFIG_EXAMPLE_BROKER_CERTIFICATE_OVERRIDE "\n-----END CERTIFICATE-----"; + #else + extern const uint8_t mqtt_eclipse_org_pem_start[] asm("_binary_mqtt_eclipse_org_pem_start"); + #endif + extern const uint8_t mqtt_eclipse_org_pem_end[] asm("_binary_mqtt_eclipse_org_pem_end"); + +static esp_err_t mqtt_event_handler(esp_mqtt_event_handle_t event) +{ + esp_mqtt_client_handle_t client = event->client; + static int msg_id = 0; + static int actual_len = 0; + // your_context_t *context = event->context; + switch (event->event_id) { + case MQTT_EVENT_CONNECTED: + ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED"); + xEventGroupSetBits(mqtt_event_group, CONNECTED_BIT); + msg_id = esp_mqtt_client_subscribe(client, CONFIG_EXAMPLE_SUBSCIBE_TOPIC, qos_test); + ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id); + + break; + case MQTT_EVENT_DISCONNECTED: + ESP_LOGI(TAG, "MQTT_EVENT_DISCONNECTED"); + break; + + case MQTT_EVENT_SUBSCRIBED: + ESP_LOGI(TAG, "MQTT_EVENT_SUBSCRIBED, msg_id=%d", event->msg_id); + break; + case MQTT_EVENT_UNSUBSCRIBED: + ESP_LOGI(TAG, "MQTT_EVENT_UNSUBSCRIBED, msg_id=%d", event->msg_id); + break; + case MQTT_EVENT_PUBLISHED: + ESP_LOGI(TAG, "MQTT_EVENT_PUBLISHED, msg_id=%d", event->msg_id); + break; + case MQTT_EVENT_DATA: + ESP_LOGI(TAG, "MQTT_EVENT_DATA"); + printf("TOPIC=%.*s\r\n", event->topic_len, event->topic); + printf("DATA=%.*s\r\n", event->data_len, event->data); + printf("ID=%d, total_len=%d, data_len=%d, current_data_offset=%d\n", event->msg_id, event->total_data_len, event->data_len, event->current_data_offset); + if (event->topic) { + actual_len = event->data_len; + msg_id = event->msg_id; + } else { + actual_len += event->data_len; + // check consisency with msg_id across multiple data events for single msg + if (msg_id != event->msg_id) { + ESP_LOGI(TAG, "Wrong msg_id in chunked message %d != %d", msg_id, event->msg_id); + abort(); + } + } + memcpy(actual_data + event->current_data_offset, event->data, event->data_len); + if (actual_len == event->total_data_len) { + if (0 == memcmp(actual_data, expected_data, expected_size)) { + printf("OK!"); + memset(actual_data, 0, expected_size); + actual_published ++; + if (actual_published == expected_published) { + printf("Correct pattern received exactly x times\n"); + ESP_LOGI(TAG, "Test finished correctly!"); + } + } else { + printf("FAILED!"); + abort(); + } + } + break; + case MQTT_EVENT_ERROR: + ESP_LOGI(TAG, "MQTT_EVENT_ERROR"); + break; + default: + ESP_LOGI(TAG, "Other event id:%d", event->event_id); + break; + } + return ESP_OK; +} + + +static void mqtt_app_start(void) +{ + mqtt_event_group = xEventGroupCreate(); + const esp_mqtt_client_config_t mqtt_cfg = { + .event_handle = mqtt_event_handler, + .cert_pem = (const char *)mqtt_eclipse_org_pem_start, + }; + + ESP_LOGI(TAG, "[APP] Free memory: %d bytes", esp_get_free_heap_size()); + mqtt_client = esp_mqtt_client_init(&mqtt_cfg); +} + +void publish_test(const char* line) +{ + char pattern[32]; + char transport[32]; + int repeat = 0; + static bool is_mqtt_init = false; + + if (!is_mqtt_init) { + mqtt_app_start(); + is_mqtt_init = true; + } + sscanf(line, "%s %s %d %d %d", transport, pattern, &repeat, &expected_published, &qos_test); + ESP_LOGI(TAG, "PATTERN:%s REPEATED:%d PUBLISHED:%d\n", pattern, repeat, expected_published); + int pattern_size = strlen(pattern); + free(expected_data); + free(actual_data); + actual_published = 0; + expected_size = pattern_size * repeat; + expected_data = malloc(expected_size); + actual_data = malloc(expected_size); + for (int i = 0; i < repeat; i++) { + memcpy(expected_data + i * pattern_size, pattern, pattern_size); + } + printf("EXPECTED STRING %.*s, SIZE:%d\n", expected_size, expected_data, expected_size); + esp_mqtt_client_stop(mqtt_client); + + if (0 == strcmp(transport, "tcp")) { + ESP_LOGI(TAG, "[TCP transport] Startup.."); + esp_mqtt_client_set_uri(mqtt_client, CONFIG_EXAMPLE_BROKER_TCP_URI); + } else if (0 == strcmp(transport, "ssl")) { + ESP_LOGI(TAG, "[SSL transport] Startup.."); + esp_mqtt_client_set_uri(mqtt_client, CONFIG_EXAMPLE_BROKER_SSL_URI); + } else if (0 == strcmp(transport, "ws")) { + ESP_LOGI(TAG, "[WS transport] Startup.."); + esp_mqtt_client_set_uri(mqtt_client, CONFIG_EXAMPLE_BROKER_WS_URI); + } else if (0 == strcmp(transport, "wss")) { + ESP_LOGI(TAG, "[WSS transport] Startup.."); + esp_mqtt_client_set_uri(mqtt_client, CONFIG_EXAMPLE_BROKER_WSS_URI); + } else { + ESP_LOGE(TAG, "Unexpected transport"); + abort(); + } + xEventGroupClearBits(mqtt_event_group, CONNECTED_BIT); + esp_mqtt_client_start(mqtt_client); + ESP_LOGI(TAG, "Note free memory: %d bytes", esp_get_free_heap_size()); + xEventGroupWaitBits(mqtt_event_group, CONNECTED_BIT, false, true, portMAX_DELAY); + + for (int i = 0; i < expected_published; i++) { + int msg_id = esp_mqtt_client_publish(mqtt_client, CONFIG_EXAMPLE_PUBLISH_TOPIC, expected_data, expected_size, qos_test, 0); + ESP_LOGI(TAG, "[%d] Publishing...", msg_id); + } +} diff --git a/examples/protocols/mqtt/publish_test/sdkconfig.ci b/examples/protocols/mqtt/publish_connect_test/sdkconfig.ci similarity index 100% rename from examples/protocols/mqtt/publish_test/sdkconfig.ci rename to examples/protocols/mqtt/publish_connect_test/sdkconfig.ci diff --git a/examples/protocols/mqtt/publish_test/sdkconfig.qemu b/examples/protocols/mqtt/publish_connect_test/sdkconfig.qemu similarity index 100% rename from examples/protocols/mqtt/publish_test/sdkconfig.qemu rename to examples/protocols/mqtt/publish_connect_test/sdkconfig.qemu diff --git a/examples/protocols/mqtt/publish_connect_test/server.key b/examples/protocols/mqtt/publish_connect_test/server.key new file mode 100644 index 0000000..2a4d650 --- /dev/null +++ b/examples/protocols/mqtt/publish_connect_test/server.key @@ -0,0 +1,27 @@ +-----BEGIN RSA PRIVATE KEY----- +MIIEogIBAAKCAQEAlUCywNhVv4RO2y9h/XGKZ1azzk3jzHpSBzIGO9LoiA8trC/p +1ykGaUfYPJllYK4HMhC4fUyE3J7tVL2Eskzl26LNPLbEoaBWZM9NhV3iA1/1EtOu +p6umLx+y3sDfvK35YAOUbjdAlBfhnJ4r8h7oTsxl3J5jZ18zgjJnJi2NEFq/yTpO +MiwHLWPjy25fDFixfV9UzSvbgt1JaGPmC7c4QkhHzjyp0+ikuvRIw0p9BBNeqBV2 +da3qBMB5FtodUJTAz6o6OKWbTalLjQi6C1H6z9TnY7IrJBUOy/FWkQH/sEsLdscD +hHa1Dz2oT203QjhzyOSfnNF95D/1MdNcMt6l0wIDAQABAoIBAC1JJTOoMFRc48RT +myrYQYNbZlEphv3q+2qdfhC2zMFDwbrmCtCy7PQSzYSNkpoEE8DYG/JAvmtmeWJl +4pZrCK9ctWM/nWfhC3WpBL97nfEiM20T94F+bn0L5Cz8XqaULv839th+QUTt/hGU +WIctY5VNJXcMQ+MAmtNdUbjex1d3iuxiKHUo4nDoZ8digKFNdtdP5B5nlMq5chCL +mxNRcsGsx2dDAxbGUapdTVPWHPJKpLOBoSkluDsfd2KZADFU2R1SJpAX9+RYh3HM +5FTUdHTUaISxbKkgeDKlEM0lqk2TtGUwCyEj098ewi7Wzsu9w60IplPPUJx5FRG6 +jp3wzLkCgYEAxKp5T20rf/7ysX7x053I7VCjDXUxAaWOEj1uS3AhOkl0NaZg7Di+ +y53fWNkcHdkt2n2LqMt/43UgMYq3TVVcq2eunPNF11e1bJw8CjDafwDs4omwwyVn +lYhPuB4dK2OAib+vU5Zqpp0kZMoxk2MZVgon8z+s8DW/zmB6aFqAWeUCgYEAwkhC +OgmXKMdjOCVy5t2f5UbY8Y9rV3w8eUATuJ47MMwLr4pGYnKoEn9JB4ltWrHv/u5S +fOv3tIrrCEvnCoCbOILwCsY5LqTNXgqova8FB6RpMUQCzhDd8LHuvdHv0WMnMzX1 +3PKuqwh8JS55m4WqZRhzr5BFKG4fHPVs4IcaJVcCgYAzzCaJSdqUKqTnJOUydDNQ +ddWMHNqccWs62J0tF0pZHLGT089hSAzQejMyJnSmU+Ykzr4y5e44DUg+ZCelIZ93 +saYmxlgVwI8THQ8fLADQRIEfpV4996MRmkZM2vmZzOo03Zyi6lIKsga82Rg3lnk8 +1Q3ynknBNpbfF0AGLhfyFQKBgBYlxJ73HutAJ5hr9HhLBYJOnEaVUehMOlycKGNg +bmD2sdJWEgYBChXpurqIORYguLo4EuE4ySkkuPxeIr14wbkkfBbOWBBwKxUwY+IT +xKAFZxR9q1AwbgyVTCEJgKw/AGX/HcMNS0omEnjunmBTUYRq0C1QZgHg490aQUor +PJjLAoGAevzdTpFlVeuKeYh1oDubGO1LinyXpBv7fPFjl+zu4AVbjojcU6yC4OO6 +QvqopE6SyAECKy8kAOFcESPsGc9Lta2XUvI203z7pIVlNVEcJ0+90mQh3Mn1U46l +sZ49PdRvNwNb5wvkh1UqNsMlGFbRlzMbIk45ou4311kCobowZek= +-----END RSA PRIVATE KEY----- diff --git a/examples/protocols/mqtt/publish_test/CMakeLists.txt b/examples/protocols/mqtt/publish_test/CMakeLists.txt deleted file mode 100644 index 4a46af3..0000000 --- a/examples/protocols/mqtt/publish_test/CMakeLists.txt +++ /dev/null @@ -1,13 +0,0 @@ -# The following four lines of boilerplate have to be in your project's CMakeLists -# in this exact order for cmake to work correctly -cmake_minimum_required(VERSION 3.5) - -# (Not part of the boilerplate) -# This example uses an extra component for common functions such as Wi-Fi and Ethernet connection. -set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common) - -include($ENV{IDF_PATH}/tools/cmake/project.cmake) - -project(mqtt_publish) - -target_add_binary_data(mqtt_publish.elf "main/mqtt_eclipse_org.pem" TEXT) diff --git a/examples/protocols/mqtt/publish_test/main/CMakeLists.txt b/examples/protocols/mqtt/publish_test/main/CMakeLists.txt deleted file mode 100644 index 67c4c7b..0000000 --- a/examples/protocols/mqtt/publish_test/main/CMakeLists.txt +++ /dev/null @@ -1,2 +0,0 @@ -idf_component_register(SRCS "publish_test.c" - INCLUDE_DIRS ".") \ No newline at end of file diff --git a/examples/protocols/mqtt/publish_test/main/component.mk b/examples/protocols/mqtt/publish_test/main/component.mk deleted file mode 100644 index 597752f..0000000 --- a/examples/protocols/mqtt/publish_test/main/component.mk +++ /dev/null @@ -1 +0,0 @@ -COMPONENT_EMBED_TXTFILES := mqtt_eclipse_org.pem diff --git a/examples/protocols/mqtt/publish_test/main/publish_test.c b/examples/protocols/mqtt/publish_test/main/publish_test.c deleted file mode 100644 index b6b7e53..0000000 --- a/examples/protocols/mqtt/publish_test/main/publish_test.c +++ /dev/null @@ -1,226 +0,0 @@ -/* MQTT publish test - - This example 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 -#include -#include -#include "esp_wifi.h" -#include "esp_system.h" -#include "nvs_flash.h" -#include "esp_event.h" -#include "esp_netif.h" -#include "protocol_examples_common.h" - -#include "freertos/FreeRTOS.h" -#include "freertos/task.h" -#include "freertos/semphr.h" -#include "freertos/queue.h" -#include "freertos/event_groups.h" - -#include "lwip/sockets.h" -#include "lwip/dns.h" -#include "lwip/netdb.h" - -#include "esp_log.h" -#include "mqtt_client.h" - -static const char *TAG = "PUBLISH_TEST"; - -static EventGroupHandle_t mqtt_event_group; -const static int CONNECTED_BIT = BIT0; - -static esp_mqtt_client_handle_t mqtt_client = NULL; - -static char *expected_data = NULL; -static char *actual_data = NULL; -static size_t expected_size = 0; -static size_t expected_published = 0; -static size_t actual_published = 0; -static int qos_test = 0; - - -#if CONFIG_EXAMPLE_BROKER_CERTIFICATE_OVERRIDDEN == 1 -static const uint8_t mqtt_eclipse_org_pem_start[] = "-----BEGIN CERTIFICATE-----\n" CONFIG_EXAMPLE_BROKER_CERTIFICATE_OVERRIDE "\n-----END CERTIFICATE-----"; -#else -extern const uint8_t mqtt_eclipse_org_pem_start[] asm("_binary_mqtt_eclipse_org_pem_start"); -#endif -extern const uint8_t mqtt_eclipse_org_pem_end[] asm("_binary_mqtt_eclipse_org_pem_end"); - -static esp_err_t mqtt_event_handler(esp_mqtt_event_handle_t event) -{ - esp_mqtt_client_handle_t client = event->client; - static int msg_id = 0; - static int actual_len = 0; - // your_context_t *context = event->context; - switch (event->event_id) { - case MQTT_EVENT_CONNECTED: - ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED"); - xEventGroupSetBits(mqtt_event_group, CONNECTED_BIT); - msg_id = esp_mqtt_client_subscribe(client, CONFIG_EXAMPLE_SUBSCIBE_TOPIC, qos_test); - ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id); - - break; - case MQTT_EVENT_DISCONNECTED: - ESP_LOGI(TAG, "MQTT_EVENT_DISCONNECTED"); - break; - - case MQTT_EVENT_SUBSCRIBED: - ESP_LOGI(TAG, "MQTT_EVENT_SUBSCRIBED, msg_id=%d", event->msg_id); - break; - case MQTT_EVENT_UNSUBSCRIBED: - ESP_LOGI(TAG, "MQTT_EVENT_UNSUBSCRIBED, msg_id=%d", event->msg_id); - break; - case MQTT_EVENT_PUBLISHED: - ESP_LOGI(TAG, "MQTT_EVENT_PUBLISHED, msg_id=%d", event->msg_id); - break; - case MQTT_EVENT_DATA: - ESP_LOGI(TAG, "MQTT_EVENT_DATA"); - printf("TOPIC=%.*s\r\n", event->topic_len, event->topic); - printf("DATA=%.*s\r\n", event->data_len, event->data); - printf("ID=%d, total_len=%d, data_len=%d, current_data_offset=%d\n", event->msg_id, event->total_data_len, event->data_len, event->current_data_offset); - if (event->topic) { - actual_len = event->data_len; - msg_id = event->msg_id; - } else { - actual_len += event->data_len; - // check consisency with msg_id across multiple data events for single msg - if (msg_id != event->msg_id) { - ESP_LOGI(TAG, "Wrong msg_id in chunked message %d != %d", msg_id, event->msg_id); - abort(); - } - } - memcpy(actual_data + event->current_data_offset, event->data, event->data_len); - if (actual_len == event->total_data_len) { - if (0 == memcmp(actual_data, expected_data, expected_size)) { - printf("OK!"); - memset(actual_data, 0, expected_size); - actual_published ++; - if (actual_published == expected_published) { - printf("Correct pattern received exactly x times\n"); - ESP_LOGI(TAG, "Test finished correctly!"); - } - } else { - printf("FAILED!"); - abort(); - } - } - break; - case MQTT_EVENT_ERROR: - ESP_LOGI(TAG, "MQTT_EVENT_ERROR"); - break; - default: - ESP_LOGI(TAG, "Other event id:%d", event->event_id); - break; - } - return ESP_OK; -} - - -static void mqtt_app_start(void) -{ - mqtt_event_group = xEventGroupCreate(); - const esp_mqtt_client_config_t mqtt_cfg = { - .event_handle = mqtt_event_handler, - .cert_pem = (const char *)mqtt_eclipse_org_pem_start, - }; - - ESP_LOGI(TAG, "[APP] Free memory: %d bytes", esp_get_free_heap_size()); - mqtt_client = esp_mqtt_client_init(&mqtt_cfg); -} - -static void get_string(char *line, size_t size) -{ - - int count = 0; - while (count < size) { - int c = fgetc(stdin); - if (c == '\n') { - line[count] = '\0'; - break; - } else if (c > 0 && c < 127) { - line[count] = c; - ++count; - } - vTaskDelay(10 / portTICK_PERIOD_MS); - } -} - -void app_main(void) -{ - char line[256]; - char pattern[32]; - char transport[32]; - int repeat = 0; - - ESP_LOGI(TAG, "[APP] Free memory: %d bytes", esp_get_free_heap_size()); - ESP_LOGI(TAG, "[APP] IDF version: %s", esp_get_idf_version()); - - esp_log_level_set("*", ESP_LOG_INFO); - esp_log_level_set("MQTT_CLIENT", ESP_LOG_VERBOSE); - esp_log_level_set("TRANSPORT_TCP", ESP_LOG_VERBOSE); - esp_log_level_set("TRANSPORT_SSL", ESP_LOG_VERBOSE); - esp_log_level_set("TRANSPORT", ESP_LOG_VERBOSE); - esp_log_level_set("OUTBOX", ESP_LOG_VERBOSE); - - ESP_ERROR_CHECK(nvs_flash_init()); - ESP_ERROR_CHECK(esp_netif_init()); - ESP_ERROR_CHECK(esp_event_loop_create_default()); - - /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig. - * Read "Establishing Wi-Fi or Ethernet Connection" section in - * examples/protocols/README.md for more information about this function. - */ - ESP_ERROR_CHECK(example_connect()); - - mqtt_app_start(); - - while (1) { - get_string(line, sizeof(line)); - sscanf(line, "%s %s %d %d %d", transport, pattern, &repeat, &expected_published, &qos_test); - ESP_LOGI(TAG, "PATTERN:%s REPEATED:%d PUBLISHED:%d\n", pattern, repeat, expected_published); - int pattern_size = strlen(pattern); - free(expected_data); - free(actual_data); - actual_published = 0; - expected_size = pattern_size * repeat; - expected_data = malloc(expected_size); - actual_data = malloc(expected_size); - for (int i = 0; i < repeat; i++) { - memcpy(expected_data + i * pattern_size, pattern, pattern_size); - } - printf("EXPECTED STRING %.*s, SIZE:%d\n", expected_size, expected_data, expected_size); - esp_mqtt_client_stop(mqtt_client); - - if (0 == strcmp(transport, "tcp")) { - ESP_LOGI(TAG, "[TCP transport] Startup.."); - esp_mqtt_client_set_uri(mqtt_client, CONFIG_EXAMPLE_BROKER_TCP_URI); - } else if (0 == strcmp(transport, "ssl")) { - ESP_LOGI(TAG, "[SSL transport] Startup.."); - esp_mqtt_client_set_uri(mqtt_client, CONFIG_EXAMPLE_BROKER_SSL_URI); - } else if (0 == strcmp(transport, "ws")) { - ESP_LOGI(TAG, "[WS transport] Startup.."); - esp_mqtt_client_set_uri(mqtt_client, CONFIG_EXAMPLE_BROKER_WS_URI); - } else if (0 == strcmp(transport, "wss")) { - ESP_LOGI(TAG, "[WSS transport] Startup.."); - esp_mqtt_client_set_uri(mqtt_client, CONFIG_EXAMPLE_BROKER_WSS_URI); - } else { - ESP_LOGE(TAG, "Unexpected transport"); - abort(); - } - xEventGroupClearBits(mqtt_event_group, CONNECTED_BIT); - esp_mqtt_client_start(mqtt_client); - ESP_LOGI(TAG, "Note free memory: %d bytes", esp_get_free_heap_size()); - xEventGroupWaitBits(mqtt_event_group, CONNECTED_BIT, false, true, portMAX_DELAY); - - for (int i = 0; i < expected_published; i++) { - int msg_id = esp_mqtt_client_publish(mqtt_client, CONFIG_EXAMPLE_PUBLISH_TOPIC, expected_data, expected_size, qos_test, 0); - ESP_LOGI(TAG, "[%d] Publishing...", msg_id); - } - } -} From 1d52a2c3c682f13f1d45e09a5c03a55435ec8230 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Fri, 7 Feb 2020 11:10:04 +0100 Subject: [PATCH 046/231] mqtt-tests: migrate the publish-connection test from example-test to test-apps --- .../mqtt/publish_connect_test/README.md | 85 ------------------ .../mqtt/publish_connect_test/CMakeLists.txt | 0 .../mqtt/publish_connect_test/Makefile | 0 .../mqtt/publish_connect_test/README.md | 22 +++++ .../mqtt/publish_connect_test/app_test.py | 4 +- .../mqtt/publish_connect_test/ca.crt | 0 .../mqtt/publish_connect_test/ca.der | Bin .../mqtt/publish_connect_test/ca.key | 0 .../mqtt/publish_connect_test/client_inv.crt | 0 .../publish_connect_test/client_no_pwd.key | 0 .../mqtt/publish_connect_test/client_pwd.crt | 0 .../mqtt/publish_connect_test/client_pwd.key | 0 .../publish_connect_test/main/CMakeLists.txt | 0 .../main/Kconfig.projbuild | 0 .../publish_connect_test/main/component.mk | 0 .../publish_connect_test/main/connect_test.c | 0 .../main/mqtt_eclipse_org.pem | 0 .../main/publish_connect_test.c | 0 .../publish_connect_test/main/publish_test.c | 0 .../publish_connect_test/sdkconfig.ci.default | 0 .../mqtt/publish_connect_test/sdkconfig.qemu | 0 .../mqtt/publish_connect_test/server.key | 0 22 files changed, 24 insertions(+), 87 deletions(-) delete mode 100644 examples/protocols/mqtt/publish_connect_test/README.md rename {examples => tools/test_apps}/protocols/mqtt/publish_connect_test/CMakeLists.txt (100%) rename {examples => tools/test_apps}/protocols/mqtt/publish_connect_test/Makefile (100%) create mode 100644 tools/test_apps/protocols/mqtt/publish_connect_test/README.md rename examples/protocols/mqtt/publish_connect_test/example_test.py => tools/test_apps/protocols/mqtt/publish_connect_test/app_test.py (98%) rename {examples => tools/test_apps}/protocols/mqtt/publish_connect_test/ca.crt (100%) rename {examples => tools/test_apps}/protocols/mqtt/publish_connect_test/ca.der (100%) rename {examples => tools/test_apps}/protocols/mqtt/publish_connect_test/ca.key (100%) rename {examples => tools/test_apps}/protocols/mqtt/publish_connect_test/client_inv.crt (100%) rename {examples => tools/test_apps}/protocols/mqtt/publish_connect_test/client_no_pwd.key (100%) rename {examples => tools/test_apps}/protocols/mqtt/publish_connect_test/client_pwd.crt (100%) rename {examples => tools/test_apps}/protocols/mqtt/publish_connect_test/client_pwd.key (100%) rename {examples => tools/test_apps}/protocols/mqtt/publish_connect_test/main/CMakeLists.txt (100%) rename {examples => tools/test_apps}/protocols/mqtt/publish_connect_test/main/Kconfig.projbuild (100%) rename {examples => tools/test_apps}/protocols/mqtt/publish_connect_test/main/component.mk (100%) rename {examples => tools/test_apps}/protocols/mqtt/publish_connect_test/main/connect_test.c (100%) rename {examples => tools/test_apps}/protocols/mqtt/publish_connect_test/main/mqtt_eclipse_org.pem (100%) rename {examples => tools/test_apps}/protocols/mqtt/publish_connect_test/main/publish_connect_test.c (100%) rename {examples => tools/test_apps}/protocols/mqtt/publish_connect_test/main/publish_test.c (100%) rename examples/protocols/mqtt/publish_connect_test/sdkconfig.ci => tools/test_apps/protocols/mqtt/publish_connect_test/sdkconfig.ci.default (100%) rename {examples => tools/test_apps}/protocols/mqtt/publish_connect_test/sdkconfig.qemu (100%) rename {examples => tools/test_apps}/protocols/mqtt/publish_connect_test/server.key (100%) diff --git a/examples/protocols/mqtt/publish_connect_test/README.md b/examples/protocols/mqtt/publish_connect_test/README.md deleted file mode 100644 index 2147420..0000000 --- a/examples/protocols/mqtt/publish_connect_test/README.md +++ /dev/null @@ -1,85 +0,0 @@ -# ESP-MQTT advanced publish and connect test project -(See the README.md file in the upper level 'examples' directory for more information about examples.) - -Main purpose of this example is to test the MQTT library to correctly publish and receive messages (of different size and sequences) over different transports. -It is possible to run this example manually without any test to exercise how the MQTT library deals with - -- reception of fragmented messages -- runtime updates of URI - -## How to use example - -This example waits for user input to provide these parameters: -- transport: string parameter, one of: tcp, ssl, ws, wss -- pattern: sample string to be transmitted as message -- pattern repeats: number of repeats of pattern in one MQTT message -- repeated: number of repeats ESP32 publishes the message, also ESP32 expects to receive the same message the same number of repeats -- qos: number specifying qos, one of: 0, 1, 2 - -### Hardware Required - -This example can be executed on any ESP32 board, the only required interface is WiFi and connection to internet. - -### Configure the project - -* Open the project configuration menu (`idf.py menuconfig`) -* Configure Wi-Fi or Ethernet under "Example Connection Configuration" menu. See "Establishing Wi-Fi or Ethernet Connection" section in [examples/protocols/README.md](../../README.md) for more details. -* When using Make build system, set `Default serial port` under `Serial flasher config`. -* Set brokers for all 4 transports (TCP, SSL, WS, WSS), also set certificate if needed -* Set topics for publishing from and to ESP32 - -### Build and Flash - -Build the project and flash it to the board, then run monitor tool to view serial output: - -``` -idf.py -p PORT flash monitor -``` - -(To exit the serial monitor, type ``Ctrl-]``.) - -See the Getting Started Guide for full steps to configure and use ESP-IDF to build projects. - -## Example Output - -``` -I (4730) event: sta ip: 192.168.0.125, mask: 255.255.255.0, gw: 192.168.0.2 -I (4730) PUBLISH_TEST: [APP] Free memory: 236728 bytes -I (4730) system_api: Base MAC address is not set, read default base MAC address from BLK0 of EFUSE -D (4740) MQTT_CLIENT: MQTT client_id=ESP32_09885C -I (31360) PUBLISH_TEST: PATTERN:1234 REPEATED:10 PUBLISHED:10 -``` -- User enters "tcp 1234 10 10 1" -``` -EXPECTED STRING 1234123412341234123412341234123412341234, SIZE:40 -W (31360) MQTT_CLIENT: Client asked to stop, but was not started -I (31360) PUBLISH_TEST: [TCP transport] Startup.. -D (31370) MQTT_CLIENT: Core selection disabled -I (31370) PUBLISH_TEST: Note free memory: 224652 bytes -I (31370) PUBLISH_TEST: Other event id:7 -D (31390) MQTT_CLIENT: Transport connected to mqtt://192.168.0.163:1883 -I (31400) MQTT_CLIENT: Sending MQTT CONNECT message, type: 1, id: 0000 -D (31410) MQTT_CLIENT: Connected -I (31410) PUBLISH_TEST: MQTT_EVENT_CONNECTED -D (31410) MQTT_CLIENT: mqtt_enqueue id: 31184, type=8 successful -D (31410) OUTBOX: ENQUEUE msgid=31184, msg_type=8, len=20, size=20 -D (31420) MQTT_CLIENT: Sent subscribe topic=/xxx.topic123, id: 31184, type=8 successful -I (31430) PUBLISH_TEST: sent subscribe successful, msg_id=31184 -D (31440) MQTT_CLIENT: mqtt_enqueue id: 16584, type=3 successful -D (31440) OUTBOX: ENQUEUE msgid=16584, msg_type=3, len=59, size=79 -I (31450) PUBLISH_TEST: [16584] Publishing... -D (31450) MQTT_CLIENT: msg_type=9, msg_id=31184 -D (31460) MQTT_CLIENT: pending_id=16584, pending_msg_count = 2 -D (31460) OUTBOX: DELETED msgid=31184, msg_type=8, remain size=59 -D (31470) MQTT_CLIENT: Subscribe successful -I (31470) PUBLISH_TEST: MQTT_EVENT_SUBSCRIBED, msg_id=31184 -D (31480) MQTT_CLIENT: msg_type=4, msg_id=16584 -D (31480) MQTT_CLIENT: pending_id=16584, pending_msg_count = 1 -D (31490) OUTBOX: DELETED msgid=16584, msg_type=3, remain size=0 -D (31500) MQTT_CLIENT: received MQTT_MSG_TYPE_PUBACK, finish QoS1 publish -I (31500) PUBLISH_TEST: MQTT_EVENT_PUBLISHED, msg_id=16584 -D (31510) MQTT_CLIENT: mqtt_enqueue id: 44615, type=3 successful -D (31520) OUTBOX: ENQUEUE msgid=44615, msg_type=3, len=59, size=59 -I (31530) PUBLISH_TEST: [44615] Publishing... -... -``` diff --git a/examples/protocols/mqtt/publish_connect_test/CMakeLists.txt b/tools/test_apps/protocols/mqtt/publish_connect_test/CMakeLists.txt similarity index 100% rename from examples/protocols/mqtt/publish_connect_test/CMakeLists.txt rename to tools/test_apps/protocols/mqtt/publish_connect_test/CMakeLists.txt diff --git a/examples/protocols/mqtt/publish_connect_test/Makefile b/tools/test_apps/protocols/mqtt/publish_connect_test/Makefile similarity index 100% rename from examples/protocols/mqtt/publish_connect_test/Makefile rename to tools/test_apps/protocols/mqtt/publish_connect_test/Makefile diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/README.md b/tools/test_apps/protocols/mqtt/publish_connect_test/README.md new file mode 100644 index 0000000..fe866d1 --- /dev/null +++ b/tools/test_apps/protocols/mqtt/publish_connect_test/README.md @@ -0,0 +1,22 @@ +# ESP-MQTT advanced publish and connect test project + +Main purpose of this application is to test the MQTT library to correctly publish and receive messages (of different size and sequences) over different transports. +It is possible to run this example manually without any test to exercise how the MQTT library deals with + +- reception of fragmented messages +- runtime updates of URI + +## Runtime settings + +This app waits for user input to provide these parameters: +- test-type: "conn" if connection test (host, port, test-case number) +- publish test: + * transport: string parameter, one of: tcp, ssl, ws, wss + * pattern: sample string to be transmitted as message + * pattern repeats: number of repeats of pattern in one MQTT message + * repeated: number of repeats ESP32 publishes the message, also ESP32 expects to receive the same message the same number of repeats + * qos: number specifying qos, one of: 0, 1, 2 + +## Hardware Required + +This test-app can be executed on any ESP32 board, the only required interface is WiFi and connection to a local network, then depending on the test either a mqtt test broker or a tls server. diff --git a/examples/protocols/mqtt/publish_connect_test/example_test.py b/tools/test_apps/protocols/mqtt/publish_connect_test/app_test.py similarity index 98% rename from examples/protocols/mqtt/publish_connect_test/example_test.py rename to tools/test_apps/protocols/mqtt/publish_connect_test/app_test.py index ccc3e06..83f85c3 100644 --- a/examples/protocols/mqtt/publish_connect_test/example_test.py +++ b/tools/test_apps/protocols/mqtt/publish_connect_test/app_test.py @@ -130,7 +130,7 @@ class TlsServer: self.shutdown.set() -@ttfw_idf.idf_example_test(env_tag="Example_WIFI") +@ttfw_idf.idf_custom_test(env_tag="Example_WIFI", group="test-apps") def test_examples_protocol_mqtt_publish_connect(env, extra_data): """ steps: @@ -138,7 +138,7 @@ def test_examples_protocol_mqtt_publish_connect(env, extra_data): 2. connect to uri specified in the config 3. send and receive data """ - dut1 = env.get_dut("mqtt_publish_connect_test", "examples/protocols/mqtt/publish_connect_test", dut_class=ttfw_idf.ESP32DUT) + dut1 = env.get_dut("mqtt_publish_connect_test", "tools/test_apps/protocols/mqtt/publish_connect_test", dut_class=ttfw_idf.ESP32DUT) # check and log bin size binary_file = os.path.join(dut1.app.binary_path, "mqtt_publish_connect_test.bin") bin_size = os.path.getsize(binary_file) diff --git a/examples/protocols/mqtt/publish_connect_test/ca.crt b/tools/test_apps/protocols/mqtt/publish_connect_test/ca.crt similarity index 100% rename from examples/protocols/mqtt/publish_connect_test/ca.crt rename to tools/test_apps/protocols/mqtt/publish_connect_test/ca.crt diff --git a/examples/protocols/mqtt/publish_connect_test/ca.der b/tools/test_apps/protocols/mqtt/publish_connect_test/ca.der similarity index 100% rename from examples/protocols/mqtt/publish_connect_test/ca.der rename to tools/test_apps/protocols/mqtt/publish_connect_test/ca.der diff --git a/examples/protocols/mqtt/publish_connect_test/ca.key b/tools/test_apps/protocols/mqtt/publish_connect_test/ca.key similarity index 100% rename from examples/protocols/mqtt/publish_connect_test/ca.key rename to tools/test_apps/protocols/mqtt/publish_connect_test/ca.key diff --git a/examples/protocols/mqtt/publish_connect_test/client_inv.crt b/tools/test_apps/protocols/mqtt/publish_connect_test/client_inv.crt similarity index 100% rename from examples/protocols/mqtt/publish_connect_test/client_inv.crt rename to tools/test_apps/protocols/mqtt/publish_connect_test/client_inv.crt diff --git a/examples/protocols/mqtt/publish_connect_test/client_no_pwd.key b/tools/test_apps/protocols/mqtt/publish_connect_test/client_no_pwd.key similarity index 100% rename from examples/protocols/mqtt/publish_connect_test/client_no_pwd.key rename to tools/test_apps/protocols/mqtt/publish_connect_test/client_no_pwd.key diff --git a/examples/protocols/mqtt/publish_connect_test/client_pwd.crt b/tools/test_apps/protocols/mqtt/publish_connect_test/client_pwd.crt similarity index 100% rename from examples/protocols/mqtt/publish_connect_test/client_pwd.crt rename to tools/test_apps/protocols/mqtt/publish_connect_test/client_pwd.crt diff --git a/examples/protocols/mqtt/publish_connect_test/client_pwd.key b/tools/test_apps/protocols/mqtt/publish_connect_test/client_pwd.key similarity index 100% rename from examples/protocols/mqtt/publish_connect_test/client_pwd.key rename to tools/test_apps/protocols/mqtt/publish_connect_test/client_pwd.key diff --git a/examples/protocols/mqtt/publish_connect_test/main/CMakeLists.txt b/tools/test_apps/protocols/mqtt/publish_connect_test/main/CMakeLists.txt similarity index 100% rename from examples/protocols/mqtt/publish_connect_test/main/CMakeLists.txt rename to tools/test_apps/protocols/mqtt/publish_connect_test/main/CMakeLists.txt diff --git a/examples/protocols/mqtt/publish_connect_test/main/Kconfig.projbuild b/tools/test_apps/protocols/mqtt/publish_connect_test/main/Kconfig.projbuild similarity index 100% rename from examples/protocols/mqtt/publish_connect_test/main/Kconfig.projbuild rename to tools/test_apps/protocols/mqtt/publish_connect_test/main/Kconfig.projbuild diff --git a/examples/protocols/mqtt/publish_connect_test/main/component.mk b/tools/test_apps/protocols/mqtt/publish_connect_test/main/component.mk similarity index 100% rename from examples/protocols/mqtt/publish_connect_test/main/component.mk rename to tools/test_apps/protocols/mqtt/publish_connect_test/main/component.mk diff --git a/examples/protocols/mqtt/publish_connect_test/main/connect_test.c b/tools/test_apps/protocols/mqtt/publish_connect_test/main/connect_test.c similarity index 100% rename from examples/protocols/mqtt/publish_connect_test/main/connect_test.c rename to tools/test_apps/protocols/mqtt/publish_connect_test/main/connect_test.c diff --git a/examples/protocols/mqtt/publish_connect_test/main/mqtt_eclipse_org.pem b/tools/test_apps/protocols/mqtt/publish_connect_test/main/mqtt_eclipse_org.pem similarity index 100% rename from examples/protocols/mqtt/publish_connect_test/main/mqtt_eclipse_org.pem rename to tools/test_apps/protocols/mqtt/publish_connect_test/main/mqtt_eclipse_org.pem diff --git a/examples/protocols/mqtt/publish_connect_test/main/publish_connect_test.c b/tools/test_apps/protocols/mqtt/publish_connect_test/main/publish_connect_test.c similarity index 100% rename from examples/protocols/mqtt/publish_connect_test/main/publish_connect_test.c rename to tools/test_apps/protocols/mqtt/publish_connect_test/main/publish_connect_test.c diff --git a/examples/protocols/mqtt/publish_connect_test/main/publish_test.c b/tools/test_apps/protocols/mqtt/publish_connect_test/main/publish_test.c similarity index 100% rename from examples/protocols/mqtt/publish_connect_test/main/publish_test.c rename to tools/test_apps/protocols/mqtt/publish_connect_test/main/publish_test.c diff --git a/examples/protocols/mqtt/publish_connect_test/sdkconfig.ci b/tools/test_apps/protocols/mqtt/publish_connect_test/sdkconfig.ci.default similarity index 100% rename from examples/protocols/mqtt/publish_connect_test/sdkconfig.ci rename to tools/test_apps/protocols/mqtt/publish_connect_test/sdkconfig.ci.default diff --git a/examples/protocols/mqtt/publish_connect_test/sdkconfig.qemu b/tools/test_apps/protocols/mqtt/publish_connect_test/sdkconfig.qemu similarity index 100% rename from examples/protocols/mqtt/publish_connect_test/sdkconfig.qemu rename to tools/test_apps/protocols/mqtt/publish_connect_test/sdkconfig.qemu diff --git a/examples/protocols/mqtt/publish_connect_test/server.key b/tools/test_apps/protocols/mqtt/publish_connect_test/server.key similarity index 100% rename from examples/protocols/mqtt/publish_connect_test/server.key rename to tools/test_apps/protocols/mqtt/publish_connect_test/server.key From 84f104bc7114853f3c0c4b261236df11b60652d5 Mon Sep 17 00:00:00 2001 From: michael Date: Tue, 11 Feb 2020 10:41:47 +0800 Subject: [PATCH 047/231] 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 999261dac5b10ff29a36085410a769b78224d046 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Thu, 20 Feb 2020 12:45:25 +0100 Subject: [PATCH 048/231] MQTT: Add build only test for using mqtt API from C++ --- .../protocols/mqtt/build_test/CMakeLists.txt | 10 ++ .../protocols/mqtt/build_test/README.md | 3 + .../mqtt/build_test/main/CMakeLists.txt | 2 + .../mqtt/build_test/main/mqtt_app.cpp | 101 ++++++++++++++++++ 4 files changed, 116 insertions(+) create mode 100644 tools/test_apps/protocols/mqtt/build_test/CMakeLists.txt create mode 100644 tools/test_apps/protocols/mqtt/build_test/README.md create mode 100644 tools/test_apps/protocols/mqtt/build_test/main/CMakeLists.txt create mode 100644 tools/test_apps/protocols/mqtt/build_test/main/mqtt_app.cpp diff --git a/tools/test_apps/protocols/mqtt/build_test/CMakeLists.txt b/tools/test_apps/protocols/mqtt/build_test/CMakeLists.txt new file mode 100644 index 0000000..5a70e7a --- /dev/null +++ b/tools/test_apps/protocols/mqtt/build_test/CMakeLists.txt @@ -0,0 +1,10 @@ +# The following four lines of boilerplate have to be in your project's CMakeLists +# in this exact order for cmake to work correctly +cmake_minimum_required(VERSION 3.5) + +# (Not part of the boilerplate) +# This example uses an extra component for common functions such as Wi-Fi and Ethernet connection. +set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common) + +include($ENV{IDF_PATH}/tools/cmake/project.cmake) +project(mqtt_tcp) \ No newline at end of file diff --git a/tools/test_apps/protocols/mqtt/build_test/README.md b/tools/test_apps/protocols/mqtt/build_test/README.md new file mode 100644 index 0000000..210877c --- /dev/null +++ b/tools/test_apps/protocols/mqtt/build_test/README.md @@ -0,0 +1,3 @@ +# Build only test for C++ + +This test app ensures that calling all mqtt-client API could be called from C++ diff --git a/tools/test_apps/protocols/mqtt/build_test/main/CMakeLists.txt b/tools/test_apps/protocols/mqtt/build_test/main/CMakeLists.txt new file mode 100644 index 0000000..9782a04 --- /dev/null +++ b/tools/test_apps/protocols/mqtt/build_test/main/CMakeLists.txt @@ -0,0 +1,2 @@ +idf_component_register(SRCS "mqtt_app.cpp" + INCLUDE_DIRS ".") \ No newline at end of file diff --git a/tools/test_apps/protocols/mqtt/build_test/main/mqtt_app.cpp b/tools/test_apps/protocols/mqtt/build_test/main/mqtt_app.cpp new file mode 100644 index 0000000..4a3f8a5 --- /dev/null +++ b/tools/test_apps/protocols/mqtt/build_test/main/mqtt_app.cpp @@ -0,0 +1,101 @@ +/* Build only example to check mqtt client API from C++ + + This example 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 "esp_system.h" +#include "nvs_flash.h" +#include "esp_event.h" +#include "esp_netif.h" +#include "esp_log.h" +#include "mqtt_client.h" + +static const char *TAG = "MQTT_EXAMPLE"; + + +static esp_err_t mqtt_event_handler_cb(esp_mqtt_event_handle_t event) +{ + switch (event->event_id) { + case MQTT_EVENT_CONNECTED: + ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED"); + break; + case MQTT_EVENT_DISCONNECTED: + ESP_LOGI(TAG, "MQTT_EVENT_DISCONNECTED"); + break; + + case MQTT_EVENT_SUBSCRIBED: + ESP_LOGI(TAG, "MQTT_EVENT_SUBSCRIBED, msg_id=%d", event->msg_id); + break; + case MQTT_EVENT_UNSUBSCRIBED: + ESP_LOGI(TAG, "MQTT_EVENT_UNSUBSCRIBED, msg_id=%d", event->msg_id); + break; + case MQTT_EVENT_PUBLISHED: + ESP_LOGI(TAG, "MQTT_EVENT_PUBLISHED, msg_id=%d", event->msg_id); + break; + case MQTT_EVENT_DATA: + ESP_LOGI(TAG, "MQTT_EVENT_DATA"); + break; + case MQTT_EVENT_ERROR: + ESP_LOGI(TAG, "MQTT_EVENT_ERROR"); + break; + default: + ESP_LOGI(TAG, "Other event id:%d", event->event_id); + break; + } + return ESP_OK; +} + +static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data) { + ESP_LOGD(TAG, "Event dispatched from event loop base=%s, event_id=%d", base, event_id); + mqtt_event_handler_cb((esp_mqtt_event_handle_t)event_data); +} + +static void mqtt_app_start(void) +{ + esp_mqtt_client_config_t mqtt_cfg = { }; + + esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg); + esp_mqtt_client_register_event(client, MQTT_EVENT_ANY, mqtt_event_handler, client); + esp_mqtt_client_start(client); + esp_mqtt_set_config(client, &mqtt_cfg); + int msg_id = esp_mqtt_client_publish(client, "/topic/qos1", "data", 0, 0, 0); + ESP_LOGI(TAG, "mqtt api returned %d", msg_id); + msg_id = esp_mqtt_client_subscribe(client, "/topic/qos0", 0); + ESP_LOGI(TAG, "mqtt api returned %d", msg_id); + msg_id = esp_mqtt_client_subscribe(client, "/topic/qos1", 1); + ESP_LOGI(TAG, "mqtt api returned %d", msg_id); + msg_id = esp_mqtt_client_unsubscribe(client, "/topic/qos1"); + ESP_LOGI(TAG, "mqtt api returned %d", msg_id); + esp_err_t err = esp_mqtt_client_set_uri(client, "mqtt://localhost:1883"); + ESP_LOGI(TAG, "mqtt api returned %d", err); + err = esp_mqtt_client_reconnect(client); + err = esp_mqtt_client_disconnect(client); + err = esp_mqtt_client_stop(client); + err = esp_mqtt_client_destroy(client); +} + +extern "C" void app_main(void) +{ + ESP_LOGI(TAG, "[APP] Startup.."); + ESP_LOGI(TAG, "[APP] Free memory: %d bytes", esp_get_free_heap_size()); + ESP_LOGI(TAG, "[APP] IDF version: %s", esp_get_idf_version()); + + esp_log_level_set("*", ESP_LOG_INFO); + esp_log_level_set("MQTT_CLIENT", ESP_LOG_VERBOSE); + esp_log_level_set("MQTT_EXAMPLE", ESP_LOG_VERBOSE); + esp_log_level_set("TRANSPORT_TCP", ESP_LOG_VERBOSE); + esp_log_level_set("TRANSPORT_SSL", ESP_LOG_VERBOSE); + esp_log_level_set("TRANSPORT", ESP_LOG_VERBOSE); + esp_log_level_set("OUTBOX", ESP_LOG_VERBOSE); + + ESP_ERROR_CHECK(nvs_flash_init()); + ESP_ERROR_CHECK(esp_netif_init()); + ESP_ERROR_CHECK(esp_event_loop_create_default()); + + mqtt_app_start(); +} From 3cf32eda758efce955a4864466af086ae1fa539b Mon Sep 17 00:00:00 2001 From: Roland Dobai Date: Fri, 28 Feb 2020 16:12:11 +0100 Subject: [PATCH 049/231] Add multi-target support for performance tests --- examples/protocols/mqtt/ssl/mqtt_ssl_example_test.py | 2 +- examples/protocols/mqtt/tcp/mqtt_tcp_example_test.py | 2 +- examples/protocols/mqtt/ws/mqtt_ws_example_test.py | 2 +- examples/protocols/mqtt/wss/mqtt_wss_example_test.py | 2 +- tools/test_apps/protocols/mqtt/publish_connect_test/app_test.py | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/examples/protocols/mqtt/ssl/mqtt_ssl_example_test.py b/examples/protocols/mqtt/ssl/mqtt_ssl_example_test.py index a392b2c..bfd9de6 100644 --- a/examples/protocols/mqtt/ssl/mqtt_ssl_example_test.py +++ b/examples/protocols/mqtt/ssl/mqtt_ssl_example_test.py @@ -78,7 +78,7 @@ def test_examples_protocol_mqtt_ssl(env, extra_data): bin_size = os.path.getsize(binary_file) ttfw_idf.log_performance("mqtt_ssl_bin_size", "{}KB" .format(bin_size // 1024)) - ttfw_idf.check_performance("mqtt_ssl_size", bin_size // 1024) + ttfw_idf.check_performance("mqtt_ssl_size", bin_size // 1024, dut1.TARGET) # Look for host:port in sdkconfig try: value = re.search(r'\:\/\/([^:]+)\:([0-9]+)', dut1.app.get_sdkconfig()["CONFIG_BROKER_URI"]) diff --git a/examples/protocols/mqtt/tcp/mqtt_tcp_example_test.py b/examples/protocols/mqtt/tcp/mqtt_tcp_example_test.py index 2d8dbe3..3c62adc 100644 --- a/examples/protocols/mqtt/tcp/mqtt_tcp_example_test.py +++ b/examples/protocols/mqtt/tcp/mqtt_tcp_example_test.py @@ -68,7 +68,7 @@ def test_examples_protocol_mqtt_qos1(env, extra_data): binary_file = os.path.join(dut1.app.binary_path, "mqtt_tcp.bin") bin_size = os.path.getsize(binary_file) ttfw_idf.log_performance("mqtt_tcp_bin_size", "{}KB".format(bin_size // 1024)) - ttfw_idf.check_performance("mqtt_tcp_size", bin_size // 1024) + ttfw_idf.check_performance("mqtt_tcp_size", bin_size // 1024, dut1.TARGET) # 1. start mqtt broker sketch host_ip = get_my_ip() thread1 = Thread(target=mqqt_server_sketch, args=(host_ip,1883)) diff --git a/examples/protocols/mqtt/ws/mqtt_ws_example_test.py b/examples/protocols/mqtt/ws/mqtt_ws_example_test.py index cf40c9a..fa24a1b 100644 --- a/examples/protocols/mqtt/ws/mqtt_ws_example_test.py +++ b/examples/protocols/mqtt/ws/mqtt_ws_example_test.py @@ -55,7 +55,7 @@ def test_examples_protocol_mqtt_ws(env, extra_data): binary_file = os.path.join(dut1.app.binary_path, "mqtt_websocket.bin") bin_size = os.path.getsize(binary_file) ttfw_idf.log_performance("mqtt_websocket_bin_size", "{}KB".format(bin_size // 1024)) - ttfw_idf.check_performance("mqtt_websocket_size", bin_size // 1024) + ttfw_idf.check_performance("mqtt_websocket_size", bin_size // 1024, dut1.TARGET) # Look for host:port in sdkconfig try: value = re.search(r'\:\/\/([^:]+)\:([0-9]+)', dut1.app.get_sdkconfig()["CONFIG_BROKER_URI"]) diff --git a/examples/protocols/mqtt/wss/mqtt_wss_example_test.py b/examples/protocols/mqtt/wss/mqtt_wss_example_test.py index a4c4e6c..75f3d39 100644 --- a/examples/protocols/mqtt/wss/mqtt_wss_example_test.py +++ b/examples/protocols/mqtt/wss/mqtt_wss_example_test.py @@ -57,7 +57,7 @@ def test_examples_protocol_mqtt_wss(env, extra_data): binary_file = os.path.join(dut1.app.binary_path, "mqtt_websocket_secure.bin") bin_size = os.path.getsize(binary_file) ttfw_idf.log_performance("mqtt_websocket_secure_bin_size", "{}KB".format(bin_size // 1024)) - ttfw_idf.check_performance("mqtt_websocket_secure_size", bin_size // 1024) + ttfw_idf.check_performance("mqtt_websocket_secure_size", bin_size // 1024, dut1.TARGET) # Look for host:port in sdkconfig try: value = re.search(r'\:\/\/([^:]+)\:([0-9]+)', dut1.app.get_sdkconfig()["CONFIG_BROKER_URI"]) diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/app_test.py b/tools/test_apps/protocols/mqtt/publish_connect_test/app_test.py index 83f85c3..3dc2a41 100644 --- a/tools/test_apps/protocols/mqtt/publish_connect_test/app_test.py +++ b/tools/test_apps/protocols/mqtt/publish_connect_test/app_test.py @@ -143,7 +143,7 @@ def test_examples_protocol_mqtt_publish_connect(env, extra_data): binary_file = os.path.join(dut1.app.binary_path, "mqtt_publish_connect_test.bin") bin_size = os.path.getsize(binary_file) ttfw_idf.log_performance("mqtt_publish_connect_test_bin_size", "{}KB".format(bin_size // 1024)) - ttfw_idf.check_performance("mqtt_publish_connect_test_bin_size_vin_size", bin_size // 1024) + ttfw_idf.check_performance("mqtt_publish_connect_test_bin_size_vin_size", bin_size // 1024, dut1.TARGET) # Look for test case symbolic names cases = {} try: From 14be739764054ec19f9b34234961a954f3f86c2b 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 050/231] esp_mqtt: add option to configure mqtt task priority. Merges https://github.com/espressif/esp-idf/pull/4947 --- components/mqtt/Kconfig | 7 +++++++ 1 file changed, 7 insertions(+) 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 From 1ece84aa33c5cd330efad8c8996f4baf979e1899 Mon Sep 17 00:00:00 2001 From: Renz Bagaporo Date: Sun, 22 Mar 2020 17:34:18 +0800 Subject: [PATCH 051/231] test, examples: use new component registration function --- examples/protocols/mqtt/ssl_psk/main/CMakeLists.txt | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/examples/protocols/mqtt/ssl_psk/main/CMakeLists.txt b/examples/protocols/mqtt/ssl_psk/main/CMakeLists.txt index 6b03500..61fac40 100644 --- a/examples/protocols/mqtt/ssl_psk/main/CMakeLists.txt +++ b/examples/protocols/mqtt/ssl_psk/main/CMakeLists.txt @@ -1,4 +1,2 @@ -set(COMPONENT_SRCS "app_main.c") -set(COMPONENT_ADD_INCLUDEDIRS ".") - -register_component() +idf_component_register(SRCS "app_main.c" + INCLUDE_DIRS ".") From ef9a2db815a5fe4c71894d6ed24240a83a7c47bf Mon Sep 17 00:00:00 2001 From: David Cermak Date: Thu, 19 Mar 2020 21:14:33 +0100 Subject: [PATCH 052/231] 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 d0f8eb10f3f30531d27dcbfeb57e49d26f015038 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Fri, 3 Apr 2020 16:43:55 +0200 Subject: [PATCH 053/231] mqtt-tests: rename tests to match the actual group --- components/mqtt/weekend_test/mqtt_publish_test.py | 4 ++-- .../test_apps/protocols/mqtt/publish_connect_test/app_test.py | 4 ++-- 2 files changed, 4 insertions(+), 4 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 diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/app_test.py b/tools/test_apps/protocols/mqtt/publish_connect_test/app_test.py index 3dc2a41..8418775 100644 --- a/tools/test_apps/protocols/mqtt/publish_connect_test/app_test.py +++ b/tools/test_apps/protocols/mqtt/publish_connect_test/app_test.py @@ -131,7 +131,7 @@ class TlsServer: @ttfw_idf.idf_custom_test(env_tag="Example_WIFI", group="test-apps") -def test_examples_protocol_mqtt_publish_connect(env, extra_data): +def test_app_protocol_mqtt_publish_connect(env, extra_data): """ steps: 1. join AP @@ -227,4 +227,4 @@ def test_examples_protocol_mqtt_publish_connect(env, extra_data): if __name__ == '__main__': - test_examples_protocol_mqtt_publish_connect() + test_app_protocol_mqtt_publish_connect() From 28a139c454c63836d6bf2f68a9511589c7881277 Mon Sep 17 00:00:00 2001 From: Marius Vikhammer Date: Tue, 11 Aug 2020 17:36:24 +0800 Subject: [PATCH 054/231] MQTT: add server side authentication to mutual auth example --- .../mqtt/ssl_mutual_auth/CMakeLists.txt | 1 + .../protocols/mqtt/ssl_mutual_auth/README.md | 2 ++ .../mqtt/ssl_mutual_auth/main/app_main.c | 3 +++ .../mqtt/ssl_mutual_auth/main/component.mk | 2 +- .../ssl_mutual_auth/main/mosquitto.org.crt | 24 +++++++++++++++++++ 5 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 examples/protocols/mqtt/ssl_mutual_auth/main/mosquitto.org.crt diff --git a/examples/protocols/mqtt/ssl_mutual_auth/CMakeLists.txt b/examples/protocols/mqtt/ssl_mutual_auth/CMakeLists.txt index 472a3ca..c392535 100644 --- a/examples/protocols/mqtt/ssl_mutual_auth/CMakeLists.txt +++ b/examples/protocols/mqtt/ssl_mutual_auth/CMakeLists.txt @@ -11,3 +11,4 @@ project(mqtt_ssl_mutual_auth) target_add_binary_data(${CMAKE_PROJECT_NAME}.elf "main/client.crt" TEXT) target_add_binary_data(${CMAKE_PROJECT_NAME}.elf "main/client.key" TEXT) +target_add_binary_data(${CMAKE_PROJECT_NAME}.elf "main/mosquitto.org.crt" TEXT) diff --git a/examples/protocols/mqtt/ssl_mutual_auth/README.md b/examples/protocols/mqtt/ssl_mutual_auth/README.md index 0696a4e..50472d6 100644 --- a/examples/protocols/mqtt/ssl_mutual_auth/README.md +++ b/examples/protocols/mqtt/ssl_mutual_auth/README.md @@ -38,6 +38,8 @@ Paste the generated CSR in the [Mosquitto test certificate signer](https://test. Please note, that the supplied files `client.crt` and `client.key` in the `main` directory are only placeholders for your client certificate and key (i.e. the example "as is" would compile but would not connect to the broker) +The server certificate `mosquitto.org.crt` can be downloaded in pem format from [mosquitto.org.crt](https://test.mosquitto.org/ssl/mosquitto.org.crt). + ### Build and Flash Build the project and flash it to the board, then run monitor tool to view serial output: diff --git a/examples/protocols/mqtt/ssl_mutual_auth/main/app_main.c b/examples/protocols/mqtt/ssl_mutual_auth/main/app_main.c index 6c620af..8c63b77 100644 --- a/examples/protocols/mqtt/ssl_mutual_auth/main/app_main.c +++ b/examples/protocols/mqtt/ssl_mutual_auth/main/app_main.c @@ -35,6 +35,8 @@ extern const uint8_t client_cert_pem_start[] asm("_binary_client_crt_start"); extern const uint8_t client_cert_pem_end[] asm("_binary_client_crt_end"); extern const uint8_t client_key_pem_start[] asm("_binary_client_key_start"); extern const uint8_t client_key_pem_end[] asm("_binary_client_key_end"); +extern const uint8_t server_cert_pem_start[] asm("_binary_mosquitto_org_crt_start"); +extern const uint8_t server_cert_pem_end[] asm("_binary_mosquitto_org_crt_end"); static esp_err_t mqtt_event_handler(esp_mqtt_event_handle_t event) { @@ -90,6 +92,7 @@ static void mqtt_app_start(void) .event_handle = mqtt_event_handler, .client_cert_pem = (const char *)client_cert_pem_start, .client_key_pem = (const char *)client_key_pem_start, + .cert_pem = (const char *)server_cert_pem_start, }; ESP_LOGI(TAG, "[APP] Free memory: %d bytes", esp_get_free_heap_size()); diff --git a/examples/protocols/mqtt/ssl_mutual_auth/main/component.mk b/examples/protocols/mqtt/ssl_mutual_auth/main/component.mk index 01adda5..aaed44c 100644 --- a/examples/protocols/mqtt/ssl_mutual_auth/main/component.mk +++ b/examples/protocols/mqtt/ssl_mutual_auth/main/component.mk @@ -1 +1 @@ -COMPONENT_EMBED_TXTFILES := client.crt client.key +COMPONENT_EMBED_TXTFILES := client.crt client.key mosquitto.org.crt diff --git a/examples/protocols/mqtt/ssl_mutual_auth/main/mosquitto.org.crt b/examples/protocols/mqtt/ssl_mutual_auth/main/mosquitto.org.crt new file mode 100644 index 0000000..e76dbd8 --- /dev/null +++ b/examples/protocols/mqtt/ssl_mutual_auth/main/mosquitto.org.crt @@ -0,0 +1,24 @@ +-----BEGIN CERTIFICATE----- +MIIEAzCCAuugAwIBAgIUBY1hlCGvdj4NhBXkZ/uLUZNILAwwDQYJKoZIhvcNAQEL +BQAwgZAxCzAJBgNVBAYTAkdCMRcwFQYDVQQIDA5Vbml0ZWQgS2luZ2RvbTEOMAwG +A1UEBwwFRGVyYnkxEjAQBgNVBAoMCU1vc3F1aXR0bzELMAkGA1UECwwCQ0ExFjAU +BgNVBAMMDW1vc3F1aXR0by5vcmcxHzAdBgkqhkiG9w0BCQEWEHJvZ2VyQGF0Y2hv +by5vcmcwHhcNMjAwNjA5MTEwNjM5WhcNMzAwNjA3MTEwNjM5WjCBkDELMAkGA1UE +BhMCR0IxFzAVBgNVBAgMDlVuaXRlZCBLaW5nZG9tMQ4wDAYDVQQHDAVEZXJieTES +MBAGA1UECgwJTW9zcXVpdHRvMQswCQYDVQQLDAJDQTEWMBQGA1UEAwwNbW9zcXVp +dHRvLm9yZzEfMB0GCSqGSIb3DQEJARYQcm9nZXJAYXRjaG9vLm9yZzCCASIwDQYJ +KoZIhvcNAQEBBQADggEPADCCAQoCggEBAME0HKmIzfTOwkKLT3THHe+ObdizamPg +UZmD64Tf3zJdNeYGYn4CEXbyP6fy3tWc8S2boW6dzrH8SdFf9uo320GJA9B7U1FW +Te3xda/Lm3JFfaHjkWw7jBwcauQZjpGINHapHRlpiCZsquAthOgxW9SgDgYlGzEA +s06pkEFiMw+qDfLo/sxFKB6vQlFekMeCymjLCbNwPJyqyhFmPWwio/PDMruBTzPH +3cioBnrJWKXc3OjXdLGFJOfj7pP0j/dr2LH72eSvv3PQQFl90CZPFhrCUcRHSSxo +E6yjGOdnz7f6PveLIB574kQORwt8ePn0yidrTC1ictikED3nHYhMUOUCAwEAAaNT +MFEwHQYDVR0OBBYEFPVV6xBUFPiGKDyo5V3+Hbh4N9YSMB8GA1UdIwQYMBaAFPVV +6xBUFPiGKDyo5V3+Hbh4N9YSMA8GA1UdEwEB/wQFMAMBAf8wDQYJKoZIhvcNAQEL +BQADggEBAGa9kS21N70ThM6/Hj9D7mbVxKLBjVWe2TPsGfbl3rEDfZ+OKRZ2j6AC +6r7jb4TZO3dzF2p6dgbrlU71Y/4K0TdzIjRj3cQ3KSm41JvUQ0hZ/c04iGDg/xWf ++pp58nfPAYwuerruPNWmlStWAXf0UTqRtg4hQDWBuUFDJTuWuuBvEXudz74eh/wK +sMwfu1HFvjy5Z0iMDU8PUDepjVolOCue9ashlS4EB5IECdSR2TItnAIiIwimx839 +LdUdRudafMu5T5Xma182OC0/u/xRlEm+tvKGGmfFcN0piqVl8OrSPBgIlb+1IKJE +m/XriWr/Cq4h/JfB7NTsezVslgkBaoU= +-----END CERTIFICATE----- From 33f7fdf23b56522b08488108228178355b98c3ca Mon Sep 17 00:00:00 2001 From: Marius Vikhammer Date: Mon, 10 Aug 2020 16:43:23 +0800 Subject: [PATCH 055/231] 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 4b6dedee6a9755a5e67218906c8788ecc38c1cdb Mon Sep 17 00:00:00 2001 From: Jakob Hasse Date: Wed, 5 Aug 2020 10:16:32 +0800 Subject: [PATCH 056/231] 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 20d4b4fa59fb1ac79cfe83d27aa3f741ae9466e4 Mon Sep 17 00:00:00 2001 From: Aditya Patwardhan Date: Fri, 31 Jul 2020 17:12:28 +0530 Subject: [PATCH 057/231] esp32s2/esp_ds: Added mqtt example for TLS using Digital Signature --- examples/protocols/mqtt/ssl_ds/CMakeLists.txt | 13 + examples/protocols/mqtt/ssl_ds/README.md | 131 ++++++++ .../protocols/mqtt/ssl_ds/configure_ds.py | 314 ++++++++++++++++++ .../protocols/mqtt/ssl_ds/main/CMakeLists.txt | 3 + .../protocols/mqtt/ssl_ds/main/app_main.c | 212 ++++++++++++ .../protocols/mqtt/ssl_ds/main/client.crt | 1 + .../mqtt/ssl_ds/main/mosquitto.org.crt | 25 ++ examples/protocols/mqtt/ssl_ds/partitions.csv | 7 + .../protocols/mqtt/ssl_ds/sdkconfig.defaults | 2 + 9 files changed, 708 insertions(+) create mode 100644 examples/protocols/mqtt/ssl_ds/CMakeLists.txt create mode 100644 examples/protocols/mqtt/ssl_ds/README.md create mode 100644 examples/protocols/mqtt/ssl_ds/configure_ds.py create mode 100644 examples/protocols/mqtt/ssl_ds/main/CMakeLists.txt create mode 100644 examples/protocols/mqtt/ssl_ds/main/app_main.c create mode 100644 examples/protocols/mqtt/ssl_ds/main/client.crt create mode 100644 examples/protocols/mqtt/ssl_ds/main/mosquitto.org.crt create mode 100644 examples/protocols/mqtt/ssl_ds/partitions.csv create mode 100644 examples/protocols/mqtt/ssl_ds/sdkconfig.defaults diff --git a/examples/protocols/mqtt/ssl_ds/CMakeLists.txt b/examples/protocols/mqtt/ssl_ds/CMakeLists.txt new file mode 100644 index 0000000..76c1d63 --- /dev/null +++ b/examples/protocols/mqtt/ssl_ds/CMakeLists.txt @@ -0,0 +1,13 @@ +# The following four lines of boilerplate have to be in your project's CMakeLists +# in this exact order for cmake to work correctly +cmake_minimum_required(VERSION 3.5) + +# (Not part of the boilerplate) +# This example uses an extra component for common functions such as Wi-Fi and Ethernet connection. +set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common) + +include($ENV{IDF_PATH}/tools/cmake/project.cmake) +project(mqtt_ssl_ds) + +target_add_binary_data(${CMAKE_PROJECT_NAME}.elf "main/client.crt" TEXT) +target_add_binary_data(${CMAKE_PROJECT_NAME}.elf "main/mosquitto.org.crt" TEXT) diff --git a/examples/protocols/mqtt/ssl_ds/README.md b/examples/protocols/mqtt/ssl_ds/README.md new file mode 100644 index 0000000..85515ba --- /dev/null +++ b/examples/protocols/mqtt/ssl_ds/README.md @@ -0,0 +1,131 @@ +| Supported Targets | ESP32-S2 | +# ESP-MQTT SSL Mutual Authentication with Digital Signature +(See the README.md file in the upper level 'examples' directory for more information about examples.) + +Espressif's ESP32-S2 MCU has a built-in Digital Signature (DS) Peripheral, which provides hardware acceleration for RSA signature. More details can be found at [Digital Signature with ESP-TLS](https://docs.espressif.com/projects/esp-idf/en/latest/esp32s2/api-reference/protocols/esp_tls.html#digital-signature-with-esp-tls). + +This example connects to the broker test.mosquitto.org using ssl transport with client certificate(RSA) and as a demonstration subscribes/unsubscribes and sends a message on certain topic.The RSA signature operation required in the ssl connection is performed with help of the Digital Signature (DS) peripheral. +(Please note that the public broker is maintained by the community so may not be always available, for details please visit http://test.mosquitto.org) + +It uses ESP-MQTT library which implements mqtt client to connect to mqtt broker. +## How to use example + +### Hardware Required + +This example can be executed on any ESP32-S2 board (which has a built-in DS peripheral), the only required interface is WiFi and connection to internet. + +### Configure the project + +#### 1) Selecting the target +As the project is to be built for the target ESP32-S2, it should be selected with the following command +``` +idf.py set-target esp32s2 +``` +More detials can be found at [Selecting the target](https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-guides/build-system.html#selecting-the-target). + +#### 2) Generate your client key and certificate + +Navigate to the main directory + +``` +cd main +``` + +Generate a client key and a CSR. When you are generating the CSR, do not use the default values. At a minimum, the CSR must include the Country, Organisation and Common Name fields. + +``` +openssl genrsa -out client.key +openssl req -out client.csr -key client.key -new +``` + +Paste the generated CSR in the [Mosquitto test certificate signer](https://test.mosquitto.org/ssl/index.php), click Submit and copy the downloaded `client.crt` in the `main` directory. + +Please note, that the supplied file `client.crt` in the `main` directory is only a placeholder for your client certificate (i.e. the example "as is" would compile but would not connect to the broker) + +#### 3) Configure the DS peripheral + +* The DS peripheral can be configured with the python script [configure_ds.py](README.md#configure_ds-py) by executing the following command + +``` + python configure_ds.py --port /* USB COM port */ --private_key /* RSA priv key */ +``` + +In the command USB COM port is nothing but the serial port to which the ESP32-S2 chip is connected. see +[check serial port](https://docs.espressif.com/projects/esp-idf/en/latest/esp32/get-started/establish-serial-connection.html#check-port-on-windows) for more details. +RSA private key is nothing but the client private key ( RSA ) generated in Step 2. + +#### 4) Connection cofiguration +* Open the project configuration menu (`idf.py menuconfig`) +* Configure Wi-Fi or Ethernet under "Example Connection Configuration" menu. See "Establishing Wi-Fi or Ethernet Connection" section in [examples/protocols/README.md](../../README.md) for more details. +* When using Make build system, set `Default serial port` under `Serial flasher config`. + +### Build and Flash + +Build the project and flash it to the board, then run monitor tool to view serial output: + +``` +idf.py -p PORT flash monitor +``` + +(To exit the serial monitor, type ``Ctrl-]``.) + +See the Getting Started Guide for full steps to configure and use ESP-IDF to build projects. + +## Example Output + +``` +I (3714) event: sta ip: 192.168.0.139, mask: 255.255.255.0, gw: 192.168.0.2 +I (3714) system_api: Base MAC address is not set, read default base MAC address from BLK0 of EFUSE +I (3964) MQTT_CLIENT: Sending MQTT CONNECT message, type: 1, id: 0000 +I (4164) MQTTS_EXAMPLE: MQTT_EVENT_CONNECTED +I (4174) MQTTS_EXAMPLE: sent publish successful, msg_id=41464 +I (4174) MQTTS_EXAMPLE: sent subscribe successful, msg_id=17886 +I (4174) MQTTS_EXAMPLE: sent subscribe successful, msg_id=42970 +I (4184) MQTTS_EXAMPLE: sent unsubscribe successful, msg_id=50241 +I (4314) MQTTS_EXAMPLE: MQTT_EVENT_PUBLISHED, msg_id=41464 +I (4484) MQTTS_EXAMPLE: MQTT_EVENT_SUBSCRIBED, msg_id=17886 +I (4484) MQTTS_EXAMPLE: sent publish successful, msg_id=0 +I (4684) MQTTS_EXAMPLE: MQTT_EVENT_SUBSCRIBED, msg_id=42970 +I (4684) MQTTS_EXAMPLE: sent publish successful, msg_id=0 +I (4884) MQTT_CLIENT: deliver_publish, message_length_read=19, message_length=19 +I (4884) MQTTS_EXAMPLE: MQTT_EVENT_DATA +TOPIC=/topic/qos0 +DATA=data +I (5194) MQTT_CLIENT: deliver_publish, message_length_read=19, message_length=19 +I (5194) MQTTS_EXAMPLE: MQTT_EVENT_DATA +TOPIC=/topic/qos0 +DATA=data +``` + + +### configure_ds.py +The script [configure_ds.py](./configure_ds.py) is used for configuring the DS peripheral on the ESP32-S2 SoC. The steps in the script are based on technical details of certain operations in the Digital Signature calculation, which can be found at Digital Signature Section of [ESP32-S2 TRM](https://www.espressif.com/sites/default/files/documentation/esp32-s2_technical_reference_manual_en.pdf) + +The configuration script performs the following steps - + +1. Take the client private key ( RSA key ) as input. + (*required parameter for the script) +can be provided with +``` + python configure_ds.py --private-key /* path to client (rsa) prv key */ +``` + +2. Randomly Calculate the `HMAC_KEY` and the `initialization vector`(IV).Then calculate the encrypted private key parameters from client private key (step i) and newly generated parameters. These encrypted private key parameters are required for the DS peripheral to perform the Digital Signature operation. + +3. Store `HMAC_KEY` in one of the efuse key blocks (in the hardware). + The ID of the efuse key block ( should be in range 1-5) can be provided with the following option. ( default value of 1 is used if not provided), +``` + python configure_ds.py --efuse_key_id /* key id in range 1-5 */ --burn_key +``` + +Currently for development purposes, the `HMAC_KEY` is stored in the efuse key block without read protection so that read operation can be performed on the same key block. +> You can burn (write) a key on an efuse key block only once.Please use a different block ID, if you want to use a different `HMAC_KEY` for the DS operation. + +4. Create an NVS partition of the name `pre_prov.csv` (in `esp_ds_data` folder) which contains the required encrypted private key parameters. A bin file of the nvs partition (`pre_prov.bin`) is also created and is flashed on the device. As we have added a custom partition, the example is set to use the custom partition table by adding the required option in `sdkconfig.defaults`. + +5. (optional) The script can be made to print the summary of the efuse on the chip by providing the following option.When this option is enabled, no other operations in the script are performed. +``` + python configure_ds.py --summary +``` + +> A list of all the supported options in the script can be obtained by executing `python configure_ds.py --help`. diff --git a/examples/protocols/mqtt/ssl_ds/configure_ds.py b/examples/protocols/mqtt/ssl_ds/configure_ds.py new file mode 100644 index 0000000..6f075ee --- /dev/null +++ b/examples/protocols/mqtt/ssl_ds/configure_ds.py @@ -0,0 +1,314 @@ +#!/usr/bin/env python +# Copyright 2020 Espressif Systems (Shanghai) Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +import argparse +import os +import sys +import hashlib +import hmac +import struct +from cryptography.hazmat.backends import default_backend +from cryptography.hazmat.primitives import serialization +from cryptography.hazmat.primitives.asymmetric import rsa +from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes +from cryptography.utils import int_to_bytes + +try: + import esptool + import espefuse +except ImportError: + idf_path = os.getenv("IDF_PATH") + if not idf_path or not os.path.exists(idf_path): + raise Exception("IDF_PATH not found") + sys.path.insert(0, os.path.join(idf_path, "components", "esptool_py", "esptool")) + import esptool + import espefuse +try: + import nvs_partition_gen as nvs_gen +except ImportError: + idf_path = os.getenv("IDF_PATH") + if not idf_path or not os.path.exists(idf_path): + raise Exception("IDF_PATH not found") + sys.path.insert(0, os.path.join(idf_path, "components", "nvs_flash", "nvs_partition_generator")) + import nvs_partition_gen as nvs_gen + + +esp_ds_data_dir = 'esp_ds_data' +# hmac_key_file is generated when HMAC_KEY is calculated, it is used when burning HMAC_KEY to efuse +hmac_key_file = esp_ds_data_dir + '/hmac_key.bin' +# csv and bin filenames are default filenames for nvs partition files created with this script +csv_filename = esp_ds_data_dir + '/pre_prov.csv' +bin_filename = esp_ds_data_dir + '/pre_prov.bin' + + +def load_privatekey(key_file_path, password=None): + key_file = open(key_file_path, 'rb') + key = key_file.read() + key_file.close() + return serialization.load_pem_private_key(key, password=password, backend=default_backend()) + + +def number_as_bytes(number, pad_bits=None): + """ + Given a number, format as a little endian array of bytes + """ + result = int_to_bytes(number)[::-1] + while pad_bits is not None and len(result) < (pad_bits // 8): + result += b'\x00' + return result + + +def calculate_ds_parameters(privkey, priv_key_pass): + private_key = load_privatekey(privkey, priv_key_pass) + if not isinstance(private_key, rsa.RSAPrivateKey): + print("Only RSA private keys are supported") + sys.exit(-1) + + priv_numbers = private_key.private_numbers() + pub_numbers = private_key.public_key().public_numbers() + Y = priv_numbers.d + M = pub_numbers.n + key_size = private_key.key_size + supported_key_size = [1024, 2048, 3072, 4096] + if key_size not in supported_key_size: + print("Key size not supported, supported sizes are" + str(supported_key_size)) + sys.exit(-1) + + hmac_key = os.urandom(32) + with open(hmac_key_file, 'wb') as key_file: + key_file.write(hmac_key) + + iv = os.urandom(16) + + rr = 1 << (key_size * 2) + rinv = rr % pub_numbers.n + mprime = - rsa._modinv(M, 1 << 32) + mprime &= 0xFFFFFFFF + length = key_size // 32 - 1 + + aes_key = hmac.HMAC(hmac_key, b"\xFF" * 32, hashlib.sha256).digest() + + md_in = number_as_bytes(Y, 4096) + \ + number_as_bytes(M, 4096) + \ + number_as_bytes(rinv, 4096) + \ + struct.pack(" +#include +#include +#include +#include "esp_wifi.h" +#include "esp_system.h" +#include "nvs_flash.h" +#include "esp_event.h" +#include "esp_netif.h" +#include "protocol_examples_common.h" + +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "freertos/semphr.h" +#include "freertos/queue.h" + +#include "lwip/sockets.h" +#include "lwip/dns.h" +#include "lwip/netdb.h" + +#include "esp_log.h" +#include "mqtt_client.h" + +/* pre_prov - name of partition containing encrypted prv key parameters ( It is set as such to synchronize it with the pre provisioning service */ +#define NVS_PARTITION_NAME "pre_prov" +/* esp_ds_ns - namespace used for defining values in esp_ds_nvs */ +#define NVS_NAMESPACE "esp_ds_ns" +/* esp_ds_key_id - efuse key block id where 256 bit key is stored, which will be read by + * DS module to perform DS operation */ +#define NVS_EFUSE_KEY_ID "esp_ds_key_id" +/* esp_ds_rsa_len - length of RSA private key (in bits) which is encrypted */ +#define NVS_RSA_LEN "esp_ds_rsa_len" +/* following entries denote key(ASCII string) for particular value in key-value pair of esp_ds_nvs (which are defined in esp_ds_ns) */ +/* ciphertext_c - encrypted RSA private key, see ESP32-S2 Techincal Reference Manual for more details */ +#define NVS_CIPHER_C "esp_ds_c" +/* initialization vector (iv) - 256 bit value used to encrypt RSA private key (to generate ciphertext_c) */ +#define NVS_IV "esp_ds_iv" +static const char *TAG = "MQTTS_EXAMPLE"; + +extern const uint8_t client_cert_pem_start[] asm("_binary_client_crt_start"); +extern const uint8_t client_cert_pem_end[] asm("_binary_client_crt_end"); +extern const uint8_t server_cert_pem_start[] asm("_binary_mosquitto_org_crt_start"); +extern const uint8_t server_cert_pem_end[] asm("_binary_mosquitto_org_crt_end"); + +static esp_err_t mqtt_event_handler(esp_mqtt_event_handle_t event) +{ + esp_mqtt_client_handle_t client = event->client; + int msg_id; + // your_context_t *context = event->context; + switch (event->event_id) { + case MQTT_EVENT_CONNECTED: + ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED"); + msg_id = esp_mqtt_client_subscribe(client, "/topic/qos0", 0); + ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id); + + msg_id = esp_mqtt_client_subscribe(client, "/topic/qos1", 1); + ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id); + + msg_id = esp_mqtt_client_unsubscribe(client, "/topic/qos1"); + ESP_LOGI(TAG, "sent unsubscribe successful, msg_id=%d", msg_id); + break; + case MQTT_EVENT_DISCONNECTED: + ESP_LOGI(TAG, "MQTT_EVENT_DISCONNECTED"); + break; + + case MQTT_EVENT_SUBSCRIBED: + ESP_LOGI(TAG, "MQTT_EVENT_SUBSCRIBED, msg_id=%d", event->msg_id); + msg_id = esp_mqtt_client_publish(client, "/topic/qos0", "data", 0, 0, 0); + ESP_LOGI(TAG, "sent publish successful, msg_id=%d", msg_id); + break; + case MQTT_EVENT_UNSUBSCRIBED: + ESP_LOGI(TAG, "MQTT_EVENT_UNSUBSCRIBED, msg_id=%d", event->msg_id); + break; + case MQTT_EVENT_PUBLISHED: + ESP_LOGI(TAG, "MQTT_EVENT_PUBLISHED, msg_id=%d", event->msg_id); + break; + case MQTT_EVENT_DATA: + ESP_LOGI(TAG, "MQTT_EVENT_DATA"); + printf("TOPIC=%.*s\r\n", event->topic_len, event->topic); + printf("DATA=%.*s\r\n", event->data_len, event->data); + break; + case MQTT_EVENT_ERROR: + ESP_LOGI(TAG, "MQTT_EVENT_ERROR"); + break; + default: + ESP_LOGI(TAG, "Other event id:%d", event->event_id); + break; + } + return ESP_OK; +} + +void *esp_read_ds_data_from_nvs(void) +{ + esp_ds_data_ctx_t *ds_data_ctx; + ds_data_ctx = (esp_ds_data_ctx_t *)malloc(sizeof(esp_ds_data_ctx_t)); + if (ds_data_ctx == NULL) { + ESP_LOGE(TAG, "Error in allocating memory for esp_ds_data_context"); + goto exit; + } + + ds_data_ctx->esp_ds_data = (esp_ds_data_t *)calloc(1, sizeof(esp_ds_data_t)); + if (ds_data_ctx->esp_ds_data == NULL) { + ESP_LOGE(TAG, "Could not allocate memory for DS data handle "); + goto exit; + } + + nvs_handle_t esp_ds_nvs_handle; + esp_err_t esp_ret; + esp_ret = nvs_flash_init_partition(NVS_PARTITION_NAME); + if (esp_ret != ESP_OK) { + ESP_LOGE(TAG, "Error in esp_ds_nvs partition init, returned %02x", esp_ret); + goto exit; + } + + esp_ret = nvs_open_from_partition(NVS_PARTITION_NAME, NVS_NAMESPACE, + NVS_READONLY, &esp_ds_nvs_handle); + if (esp_ret != ESP_OK) { + ESP_LOGE(TAG, "Error in esp_ds_nvs partition open, returned %02x", esp_ret); + goto exit; + } + + esp_ret = nvs_get_u8(esp_ds_nvs_handle, NVS_EFUSE_KEY_ID, &ds_data_ctx->efuse_key_id); + if (esp_ret != ESP_OK) { + ESP_LOGE(TAG, "Error in efuse_key_id value from nvs, returned %02x", esp_ret); + goto exit; + } + + esp_ret = nvs_get_u16(esp_ds_nvs_handle, NVS_RSA_LEN, &ds_data_ctx->rsa_length_bits); + if (esp_ret != ESP_OK) { + ESP_LOGE(TAG, "Error in reading rsa key length value from nvs, returned %02x", esp_ret); + goto exit; + } + + size_t blob_length = ESP_DS_C_LEN; + esp_ret = nvs_get_blob(esp_ds_nvs_handle, NVS_CIPHER_C, (void *)(ds_data_ctx->esp_ds_data->c), &blob_length); + if ((esp_ret != ESP_OK) || (blob_length != ESP_DS_C_LEN)) { + ESP_LOGE(TAG, "Error in reading initialization vector value from nvs,bytes_read = %d, returned %02x", blob_length, esp_ret); + goto exit; + } + + blob_length = ESP_DS_IV_LEN; + esp_ret = nvs_get_blob(esp_ds_nvs_handle, NVS_IV, (void *)(ds_data_ctx->esp_ds_data->iv), &blob_length); + if ((esp_ret != ESP_OK) || (blob_length != ESP_DS_IV_LEN)) { + ESP_LOGE(TAG, "Error in reading initialization vector value from nvs,bytes_read = %d, returned %02x", blob_length, esp_ret); + goto exit; + } + + return (void *)ds_data_ctx; +exit: + if (ds_data_ctx != NULL) { + free(ds_data_ctx->esp_ds_data); + } + free(ds_data_ctx); + return NULL; +} + +static void mqtt_app_start(void) +{ + + /* The context is used by the DS peripheral, should not be freed */ + void *ds_data = esp_read_ds_data_from_nvs(); + if (ds_data == NULL) { + ESP_LOGE(TAG, "Error in reading DS data from NVS"); + vTaskDelete(NULL); + } + const esp_mqtt_client_config_t mqtt_cfg = { + .uri = "mqtts://test.mosquitto.org:8884", + .event_handle = mqtt_event_handler, + .cert_pem = (const char *)server_cert_pem_start, + .client_cert_pem = (const char *)client_cert_pem_start, + .client_key_pem = NULL, + .ds_data = ds_data, + }; + + ESP_LOGI(TAG, "[APP] Free memory: %d bytes", esp_get_free_heap_size()); + esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg); + esp_mqtt_client_start(client); +} + +void app_main(void) +{ + ESP_LOGI(TAG, "[APP] Startup.."); + ESP_LOGI(TAG, "[APP] Free memory: %d bytes", esp_get_free_heap_size()); + ESP_LOGI(TAG, "[APP] IDF version: %s", esp_get_idf_version()); + + esp_log_level_set("*", ESP_LOG_INFO); + esp_log_level_set("MQTT_CLIENT", ESP_LOG_VERBOSE); + esp_log_level_set("TRANSPORT_TCP", ESP_LOG_VERBOSE); + esp_log_level_set("TRANSPORT_SSL", ESP_LOG_VERBOSE); + esp_log_level_set("TRANSPORT", ESP_LOG_VERBOSE); + esp_log_level_set("OUTBOX", ESP_LOG_VERBOSE); + + ESP_ERROR_CHECK(nvs_flash_init()); + ESP_ERROR_CHECK(esp_netif_init()); + ESP_ERROR_CHECK(esp_event_loop_create_default()); + + /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig. + * Read "Establishing Wi-Fi or Ethernet Connection" section in + * examples/protocols/README.md for more information about this function. + */ + ESP_ERROR_CHECK(example_connect()); + + mqtt_app_start(); +} diff --git a/examples/protocols/mqtt/ssl_ds/main/client.crt b/examples/protocols/mqtt/ssl_ds/main/client.crt new file mode 100644 index 0000000..7a3074b --- /dev/null +++ b/examples/protocols/mqtt/ssl_ds/main/client.crt @@ -0,0 +1 @@ +Please paste your client certificate here (follow instructions in README.md) diff --git a/examples/protocols/mqtt/ssl_ds/main/mosquitto.org.crt b/examples/protocols/mqtt/ssl_ds/main/mosquitto.org.crt new file mode 100644 index 0000000..8a3d44f --- /dev/null +++ b/examples/protocols/mqtt/ssl_ds/main/mosquitto.org.crt @@ -0,0 +1,25 @@ +-----BEGIN CERTIFICATE----- +MIIEAzCCAuugAwIBAgIUBY1hlCGvdj4NhBXkZ/uLUZNILAwwDQYJKoZIhvcNAQEL +BQAwgZAxCzAJBgNVBAYTAkdCMRcwFQYDVQQIDA5Vbml0ZWQgS2luZ2RvbTEOMAwG +A1UEBwwFRGVyYnkxEjAQBgNVBAoMCU1vc3F1aXR0bzELMAkGA1UECwwCQ0ExFjAU +BgNVBAMMDW1vc3F1aXR0by5vcmcxHzAdBgkqhkiG9w0BCQEWEHJvZ2VyQGF0Y2hv +by5vcmcwHhcNMjAwNjA5MTEwNjM5WhcNMzAwNjA3MTEwNjM5WjCBkDELMAkGA1UE +BhMCR0IxFzAVBgNVBAgMDlVuaXRlZCBLaW5nZG9tMQ4wDAYDVQQHDAVEZXJieTES +MBAGA1UECgwJTW9zcXVpdHRvMQswCQYDVQQLDAJDQTEWMBQGA1UEAwwNbW9zcXVp +dHRvLm9yZzEfMB0GCSqGSIb3DQEJARYQcm9nZXJAYXRjaG9vLm9yZzCCASIwDQYJ +KoZIhvcNAQEBBQADggEPADCCAQoCggEBAME0HKmIzfTOwkKLT3THHe+ObdizamPg +UZmD64Tf3zJdNeYGYn4CEXbyP6fy3tWc8S2boW6dzrH8SdFf9uo320GJA9B7U1FW +Te3xda/Lm3JFfaHjkWw7jBwcauQZjpGINHapHRlpiCZsquAthOgxW9SgDgYlGzEA +s06pkEFiMw+qDfLo/sxFKB6vQlFekMeCymjLCbNwPJyqyhFmPWwio/PDMruBTzPH +3cioBnrJWKXc3OjXdLGFJOfj7pP0j/dr2LH72eSvv3PQQFl90CZPFhrCUcRHSSxo +E6yjGOdnz7f6PveLIB574kQORwt8ePn0yidrTC1ictikED3nHYhMUOUCAwEAAaNT +MFEwHQYDVR0OBBYEFPVV6xBUFPiGKDyo5V3+Hbh4N9YSMB8GA1UdIwQYMBaAFPVV +6xBUFPiGKDyo5V3+Hbh4N9YSMA8GA1UdEwEB/wQFMAMBAf8wDQYJKoZIhvcNAQEL +BQADggEBAGa9kS21N70ThM6/Hj9D7mbVxKLBjVWe2TPsGfbl3rEDfZ+OKRZ2j6AC +6r7jb4TZO3dzF2p6dgbrlU71Y/4K0TdzIjRj3cQ3KSm41JvUQ0hZ/c04iGDg/xWf ++pp58nfPAYwuerruPNWmlStWAXf0UTqRtg4hQDWBuUFDJTuWuuBvEXudz74eh/wK +sMwfu1HFvjy5Z0iMDU8PUDepjVolOCue9ashlS4EB5IECdSR2TItnAIiIwimx839 +LdUdRudafMu5T5Xma182OC0/u/xRlEm+tvKGGmfFcN0piqVl8OrSPBgIlb+1IKJE +m/XriWr/Cq4h/JfB7NTsezVslgkBaoU= +-----END CERTIFICATE----- +--- diff --git a/examples/protocols/mqtt/ssl_ds/partitions.csv b/examples/protocols/mqtt/ssl_ds/partitions.csv new file mode 100644 index 0000000..45a8469 --- /dev/null +++ b/examples/protocols/mqtt/ssl_ds/partitions.csv @@ -0,0 +1,7 @@ +# ESP-IDF Partition Table +# Name, Type, SubType, Offset, Size, Flags +nvs,data,nvs,0x9000,24K, +phy_init,data,phy,0xf000,4K, +pre_prov,data,nvs,0x10000,0x3000, +factory,app,factory,0x20000,1M, + diff --git a/examples/protocols/mqtt/ssl_ds/sdkconfig.defaults b/examples/protocols/mqtt/ssl_ds/sdkconfig.defaults new file mode 100644 index 0000000..5e7b3e3 --- /dev/null +++ b/examples/protocols/mqtt/ssl_ds/sdkconfig.defaults @@ -0,0 +1,2 @@ +CONFIG_PARTITION_TABLE_CUSTOM=y + From b934cb2943217d82c969b9fd0bae04555c3903f6 Mon Sep 17 00:00:00 2001 From: Aditya Patwardhan Date: Mon, 19 Oct 2020 08:44:05 +0530 Subject: [PATCH 058/231] esp32s2-ssl_ds: Use CLI interface for espefuse in configure_ds.py * make the project CMakelists to flash the external partition "pre_prov" * Remove the dependancy of configure_ds.py on esptool.py * Updated README * Fix the help menu --- examples/protocols/mqtt/ssl_ds/CMakeLists.txt | 7 + examples/protocols/mqtt/ssl_ds/README.md | 2 +- .../protocols/mqtt/ssl_ds/configure_ds.py | 127 +++--------------- 3 files changed, 25 insertions(+), 111 deletions(-) diff --git a/examples/protocols/mqtt/ssl_ds/CMakeLists.txt b/examples/protocols/mqtt/ssl_ds/CMakeLists.txt index 76c1d63..50de6d0 100644 --- a/examples/protocols/mqtt/ssl_ds/CMakeLists.txt +++ b/examples/protocols/mqtt/ssl_ds/CMakeLists.txt @@ -9,5 +9,12 @@ set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_exam include($ENV{IDF_PATH}/tools/cmake/project.cmake) project(mqtt_ssl_ds) +# Flash the custom partition named `pre_prov`. +set (partition pre_prov) +idf_build_get_property(project_dir PROJECT_DIR) +set(image_file ${project_dir}/esp_ds_data/${partition}.bin) +partition_table_get_partition_info(offset "--partition-name ${partition}" "offset") +esptool_py_flash_target_image(flash "${partition}" "${offset}" "${image_file}") + target_add_binary_data(${CMAKE_PROJECT_NAME}.elf "main/client.crt" TEXT) target_add_binary_data(${CMAKE_PROJECT_NAME}.elf "main/mosquitto.org.crt" TEXT) diff --git a/examples/protocols/mqtt/ssl_ds/README.md b/examples/protocols/mqtt/ssl_ds/README.md index 85515ba..01c9758 100644 --- a/examples/protocols/mqtt/ssl_ds/README.md +++ b/examples/protocols/mqtt/ssl_ds/README.md @@ -115,7 +115,7 @@ can be provided with 3. Store `HMAC_KEY` in one of the efuse key blocks (in the hardware). The ID of the efuse key block ( should be in range 1-5) can be provided with the following option. ( default value of 1 is used if not provided), ``` - python configure_ds.py --efuse_key_id /* key id in range 1-5 */ --burn_key + python configure_ds.py --efuse_key_id /* key id in range 1-5 */ ``` Currently for development purposes, the `HMAC_KEY` is stored in the efuse key block without read protection so that read operation can be performed on the same key block. diff --git a/examples/protocols/mqtt/ssl_ds/configure_ds.py b/examples/protocols/mqtt/ssl_ds/configure_ds.py index 6f075ee..2d4f9af 100644 --- a/examples/protocols/mqtt/ssl_ds/configure_ds.py +++ b/examples/protocols/mqtt/ssl_ds/configure_ds.py @@ -23,16 +23,6 @@ from cryptography.hazmat.primitives.asymmetric import rsa from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes from cryptography.utils import int_to_bytes -try: - import esptool - import espefuse -except ImportError: - idf_path = os.getenv("IDF_PATH") - if not idf_path or not os.path.exists(idf_path): - raise Exception("IDF_PATH not found") - sys.path.insert(0, os.path.join(idf_path, "components", "esptool_py", "esptool")) - import esptool - import espefuse try: import nvs_partition_gen as nvs_gen except ImportError: @@ -122,68 +112,14 @@ def calculate_ds_parameters(privkey, priv_key_pass): return c, iv, key_size -class DefineArgs(object): - def __init__(self, attributes): - for key, value in attributes.items(): - self.__setattr__(key, value) +def efuse_summary(args): + os.system("python $IDF_PATH/components/esptool_py/esptool/espefuse.py --chip esp32s2 -p %s summary" % (args.port)) -def efuse_summary(esp,args): - efuses, _efuse_operations = espefuse.get_efuses(esp, esp.CHIP_NAME, False, False, False) - - summary_args = DefineArgs({ - 'baud': 115200, - 'before': 'default_reset', - 'chip': esp.CHIP_NAME, - 'debug': False, - 'do_not_confirm': False, - 'file': sys.stdout, - 'mode':'w', - 'encding': 'utf-8', - 'format': 'summary', - 'operation': 'summary', - 'port':args.port, - }) - - print("\n\n\n\t---SUMMARY START---\n") - espefuse.summary(esp, efuses, summary_args) - print("\n\t---SUMMARY END---\n\n") - - -def efuse_burn_key(esp, args): - - efuses, efuse_operations = espefuse.get_efuses(esp, esp.CHIP_NAME, False, False, False) - - if args.efuse_key_id is None: - print("efuse Key id cannot be None") - sys.exit(-1) - - key_file = open(hmac_key_file, 'rb') - # First element of _KEYBLOCKS is config data so add offset of 1 - key_block = efuses._KEYBLOCKS[args.efuse_key_id + 1][0] - burn_key_args = DefineArgs({ - 'baud': 115200, - 'before': 'default_reset', - 'chip': esp.CHIP_NAME, - 'debug': False, - 'do_not_confirm': False, - 'block': [key_block], - 'keyfile': [key_file], - 'keypurpose': ['HMAC_DOWN_DIGITAL_SIGNATURE'], - 'operation': 'burn_key', - 'force_write_always': False, - 'no_read_protect': True, - 'no_write_protect': False, - 'port': args.port, - - }) - - try: - efuse_operations.burn_key(esp, efuses, burn_key_args, None) - key_file.close() - except esptool.FatalError: - print("\nERROR: The provided key block already contains previously burned key, please use a different key block ID") - sys.exit(-1) +def efuse_burn_key(args): + os.system("python $IDF_PATH/components/esptool_py/esptool/espefuse.py --chip esp32s2 -p %s burn_key" + "%s %s HMAC_DOWN_DIGITAL_SIGNATURE --no-read-protect" + % ((args.port), ("BLOCK_KEY" + str(args.efuse_key_id)), (hmac_key_file))) def generate_csv_file(c, iv, hmac_key_id, key_size, csv_file): @@ -197,6 +133,12 @@ def generate_csv_file(c, iv, hmac_key_id, key_size, csv_file): f.write("esp_ds_rsa_len,data,u16,%d\n" % (key_size)) +class DefineArgs(object): + def __init__(self, attributes): + for key, value in attributes.items(): + self.__setattr__(key, value) + + def generate_nvs_partition(input_filename, output_filename): nvs_args = DefineArgs({ @@ -211,45 +153,16 @@ def generate_nvs_partition(input_filename, output_filename): nvs_gen.generate(nvs_args, is_encr_enabled=False, encr_key=None) -def flash_nvs_partition(bin_path, addr, esp): - esp.connect() - print(bin_path) - abs_bin_path = os.path.dirname(os.path.abspath(__file__)) + '/' + bin_path - print(abs_bin_path) - if (os.path.exists(abs_bin_path) is False): - print("NVS partition not found") - sys.exit(-1) - - with open(bin_path, 'rb') as nvs_file: - - flash_file = [(addr, nvs_file)] - - flash_args = DefineArgs({ - 'flash_size': '4MB', - 'flash_mode': 'qio', - 'flash_freq': '80m', - 'addr_filename': flash_file, - 'no_stub': False, - 'compress': False, - 'verify': False, - 'encrypt': False, - 'erase_all': False, - }) - - esp.change_baud(baud=921600) - esptool.write_flash(esp, flash_args) - - def main(): - parser = argparse.ArgumentParser(description='''Provision the ESPWROOM32SE device with - device_certificate and signer_certificate required for TLS authentication''') + parser = argparse.ArgumentParser(description='''Genereate an nvs partition containing the DS private key parameters from the client private key, + Generate an HMAC key and burn it in the desired efuse key block (required for Digital Signature)''') parser.add_argument( '--private-key', dest='privkey', default='main/client.key', metavar='relative/path/to/client-priv-key', - help='relative path(from secure_cert_mfg.py) to signer certificate private key') + help='relative path to client private key') parser.add_argument( "--pwd", '--password', @@ -283,13 +196,8 @@ def main(): args = parser.parse_args() - esp = esptool.ESPLoader.detect_chip(args.port,baud=115200) - if (esp.CHIP_NAME != 'ESP32-S2'): - print("Only ESP32S2 chip is supported") - sys.exit(-1) - if args.summary is not False: - efuse_summary(esp, args) + efuse_summary(args) sys.exit(0) if (os.path.exists(esp_ds_data_dir) is False): @@ -303,11 +211,10 @@ def main(): print("overwriting previous encrypted private key data, as you have provided \"--overwrite\" option") c, iv, key_size = calculate_ds_parameters(args.privkey, args.priv_key_pass) - efuse_burn_key(esp, args) + efuse_burn_key(args) generate_csv_file(c, iv, args.efuse_key_id, key_size, csv_filename) generate_nvs_partition(csv_filename, bin_filename) - flash_nvs_partition(bin_filename, 0x10000, esp) if __name__ == "__main__": From ddb5e81a9843fdae3e89d880d41fe5b08aa1fba5 Mon Sep 17 00:00:00 2001 From: morris Date: Tue, 20 Oct 2020 18:08:15 +0800 Subject: [PATCH 059/231] ci: add more build test for esp32-s3 --- tools/test_apps/protocols/mqtt/build_test/README.md | 3 +++ tools/test_apps/protocols/mqtt/publish_connect_test/README.md | 3 +++ 2 files changed, 6 insertions(+) diff --git a/tools/test_apps/protocols/mqtt/build_test/README.md b/tools/test_apps/protocols/mqtt/build_test/README.md index 210877c..d4a8422 100644 --- a/tools/test_apps/protocols/mqtt/build_test/README.md +++ b/tools/test_apps/protocols/mqtt/build_test/README.md @@ -1,3 +1,6 @@ +| Supported Targets | ESP32 | ESP32-S2 | +| ----------------- | ----- | -------- | + # Build only test for C++ This test app ensures that calling all mqtt-client API could be called from C++ diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/README.md b/tools/test_apps/protocols/mqtt/publish_connect_test/README.md index fe866d1..b52d36e 100644 --- a/tools/test_apps/protocols/mqtt/publish_connect_test/README.md +++ b/tools/test_apps/protocols/mqtt/publish_connect_test/README.md @@ -1,3 +1,6 @@ +| Supported Targets | ESP32 | ESP32-S2 | +| ----------------- | ----- | -------- | + # ESP-MQTT advanced publish and connect test project Main purpose of this application is to test the MQTT library to correctly publish and receive messages (of different size and sequences) over different transports. From d24c85e78318b4c768a9a4b22721ef6c84804ac4 Mon Sep 17 00:00:00 2001 From: Angus Gratton Date: Tue, 10 Nov 2020 17:51:08 +1100 Subject: [PATCH 060/231] cmake: Apply cmakelint fixes --- examples/protocols/mqtt/ssl_ds/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/protocols/mqtt/ssl_ds/CMakeLists.txt b/examples/protocols/mqtt/ssl_ds/CMakeLists.txt index 50de6d0..974fc2d 100644 --- a/examples/protocols/mqtt/ssl_ds/CMakeLists.txt +++ b/examples/protocols/mqtt/ssl_ds/CMakeLists.txt @@ -10,7 +10,7 @@ include($ENV{IDF_PATH}/tools/cmake/project.cmake) project(mqtt_ssl_ds) # Flash the custom partition named `pre_prov`. -set (partition pre_prov) +set(partition pre_prov) idf_build_get_property(project_dir PROJECT_DIR) set(image_file ${project_dir}/esp_ds_data/${partition}.bin) partition_table_get_partition_info(offset "--partition-name ${partition}" "offset") From ebd802fa2cbd18c6251fedafbbb7efdce9d0abf6 Mon Sep 17 00:00:00 2001 From: Angus Gratton Date: Tue, 10 Nov 2020 18:40:01 +1100 Subject: [PATCH 061/231] 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 - examples/protocols/mqtt/ssl/main/CMakeLists.txt | 2 +- examples/protocols/mqtt/ssl/main/mqtt_eclipse_org.pem | 2 +- examples/protocols/mqtt/ssl_ds/main/CMakeLists.txt | 1 - examples/protocols/mqtt/ssl_ds/partitions.csv | 1 - examples/protocols/mqtt/ssl_ds/sdkconfig.defaults | 1 - examples/protocols/mqtt/ssl_mutual_auth/main/CMakeLists.txt | 2 +- examples/protocols/mqtt/tcp/CMakeLists.txt | 2 +- examples/protocols/mqtt/tcp/main/CMakeLists.txt | 2 +- examples/protocols/mqtt/ws/main/CMakeLists.txt | 2 +- examples/protocols/mqtt/wss/main/CMakeLists.txt | 2 +- examples/protocols/mqtt/wss/main/mqtt_eclipse_org.pem | 2 +- tools/test_apps/protocols/mqtt/build_test/CMakeLists.txt | 2 +- tools/test_apps/protocols/mqtt/build_test/main/CMakeLists.txt | 2 +- .../protocols/mqtt/publish_connect_test/main/CMakeLists.txt | 2 +- .../protocols/mqtt/publish_connect_test/main/connect_test.c | 2 -- .../mqtt/publish_connect_test/main/mqtt_eclipse_org.pem | 2 +- .../mqtt/publish_connect_test/main/publish_connect_test.c | 2 +- 21 files changed, 15 insertions(+), 22 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 - diff --git a/examples/protocols/mqtt/ssl/main/CMakeLists.txt b/examples/protocols/mqtt/ssl/main/CMakeLists.txt index e739255..61fac40 100644 --- a/examples/protocols/mqtt/ssl/main/CMakeLists.txt +++ b/examples/protocols/mqtt/ssl/main/CMakeLists.txt @@ -1,2 +1,2 @@ idf_component_register(SRCS "app_main.c" - INCLUDE_DIRS ".") \ No newline at end of file + INCLUDE_DIRS ".") diff --git a/examples/protocols/mqtt/ssl/main/mqtt_eclipse_org.pem b/examples/protocols/mqtt/ssl/main/mqtt_eclipse_org.pem index edb593b..0002462 100644 --- a/examples/protocols/mqtt/ssl/main/mqtt_eclipse_org.pem +++ b/examples/protocols/mqtt/ssl/main/mqtt_eclipse_org.pem @@ -24,4 +24,4 @@ wApIvJSwtmVi4MFU5aMqrSDE6ea73Mj2tcMyo5jMd6jmeWUHK8so/joWUoHOUgwu X4Po1QYz+3dszkDqMp4fklxBwXRsW10KXzPMTZ+sOPAveyxindmjkW8lGy+QsRlG PfZ+G6Z6h7mjem0Y+iWlkYcV4PIWL1iwBi8saCbGS5jN2p8M+X+Q7UNKEkROb3N6 KOqkqm57TH2H3eDJAkSnh6/DNFu0Qg== ------END CERTIFICATE----- \ No newline at end of file +-----END CERTIFICATE----- diff --git a/examples/protocols/mqtt/ssl_ds/main/CMakeLists.txt b/examples/protocols/mqtt/ssl_ds/main/CMakeLists.txt index 8899494..61fac40 100644 --- a/examples/protocols/mqtt/ssl_ds/main/CMakeLists.txt +++ b/examples/protocols/mqtt/ssl_ds/main/CMakeLists.txt @@ -1,3 +1,2 @@ idf_component_register(SRCS "app_main.c" INCLUDE_DIRS ".") - diff --git a/examples/protocols/mqtt/ssl_ds/partitions.csv b/examples/protocols/mqtt/ssl_ds/partitions.csv index 45a8469..2e1b23d 100644 --- a/examples/protocols/mqtt/ssl_ds/partitions.csv +++ b/examples/protocols/mqtt/ssl_ds/partitions.csv @@ -4,4 +4,3 @@ nvs,data,nvs,0x9000,24K, phy_init,data,phy,0xf000,4K, pre_prov,data,nvs,0x10000,0x3000, factory,app,factory,0x20000,1M, - diff --git a/examples/protocols/mqtt/ssl_ds/sdkconfig.defaults b/examples/protocols/mqtt/ssl_ds/sdkconfig.defaults index 5e7b3e3..4b0421e 100644 --- a/examples/protocols/mqtt/ssl_ds/sdkconfig.defaults +++ b/examples/protocols/mqtt/ssl_ds/sdkconfig.defaults @@ -1,2 +1 @@ CONFIG_PARTITION_TABLE_CUSTOM=y - diff --git a/examples/protocols/mqtt/ssl_mutual_auth/main/CMakeLists.txt b/examples/protocols/mqtt/ssl_mutual_auth/main/CMakeLists.txt index e739255..61fac40 100644 --- a/examples/protocols/mqtt/ssl_mutual_auth/main/CMakeLists.txt +++ b/examples/protocols/mqtt/ssl_mutual_auth/main/CMakeLists.txt @@ -1,2 +1,2 @@ idf_component_register(SRCS "app_main.c" - INCLUDE_DIRS ".") \ No newline at end of file + INCLUDE_DIRS ".") diff --git a/examples/protocols/mqtt/tcp/CMakeLists.txt b/examples/protocols/mqtt/tcp/CMakeLists.txt index 5a70e7a..3dd15c0 100644 --- a/examples/protocols/mqtt/tcp/CMakeLists.txt +++ b/examples/protocols/mqtt/tcp/CMakeLists.txt @@ -7,4 +7,4 @@ cmake_minimum_required(VERSION 3.5) set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common) include($ENV{IDF_PATH}/tools/cmake/project.cmake) -project(mqtt_tcp) \ No newline at end of file +project(mqtt_tcp) diff --git a/examples/protocols/mqtt/tcp/main/CMakeLists.txt b/examples/protocols/mqtt/tcp/main/CMakeLists.txt index e739255..61fac40 100644 --- a/examples/protocols/mqtt/tcp/main/CMakeLists.txt +++ b/examples/protocols/mqtt/tcp/main/CMakeLists.txt @@ -1,2 +1,2 @@ idf_component_register(SRCS "app_main.c" - INCLUDE_DIRS ".") \ No newline at end of file + INCLUDE_DIRS ".") diff --git a/examples/protocols/mqtt/ws/main/CMakeLists.txt b/examples/protocols/mqtt/ws/main/CMakeLists.txt index e739255..61fac40 100644 --- a/examples/protocols/mqtt/ws/main/CMakeLists.txt +++ b/examples/protocols/mqtt/ws/main/CMakeLists.txt @@ -1,2 +1,2 @@ idf_component_register(SRCS "app_main.c" - INCLUDE_DIRS ".") \ No newline at end of file + INCLUDE_DIRS ".") diff --git a/examples/protocols/mqtt/wss/main/CMakeLists.txt b/examples/protocols/mqtt/wss/main/CMakeLists.txt index e739255..61fac40 100644 --- a/examples/protocols/mqtt/wss/main/CMakeLists.txt +++ b/examples/protocols/mqtt/wss/main/CMakeLists.txt @@ -1,2 +1,2 @@ idf_component_register(SRCS "app_main.c" - INCLUDE_DIRS ".") \ No newline at end of file + INCLUDE_DIRS ".") diff --git a/examples/protocols/mqtt/wss/main/mqtt_eclipse_org.pem b/examples/protocols/mqtt/wss/main/mqtt_eclipse_org.pem index edb593b..0002462 100644 --- a/examples/protocols/mqtt/wss/main/mqtt_eclipse_org.pem +++ b/examples/protocols/mqtt/wss/main/mqtt_eclipse_org.pem @@ -24,4 +24,4 @@ wApIvJSwtmVi4MFU5aMqrSDE6ea73Mj2tcMyo5jMd6jmeWUHK8so/joWUoHOUgwu X4Po1QYz+3dszkDqMp4fklxBwXRsW10KXzPMTZ+sOPAveyxindmjkW8lGy+QsRlG PfZ+G6Z6h7mjem0Y+iWlkYcV4PIWL1iwBi8saCbGS5jN2p8M+X+Q7UNKEkROb3N6 KOqkqm57TH2H3eDJAkSnh6/DNFu0Qg== ------END CERTIFICATE----- \ No newline at end of file +-----END CERTIFICATE----- diff --git a/tools/test_apps/protocols/mqtt/build_test/CMakeLists.txt b/tools/test_apps/protocols/mqtt/build_test/CMakeLists.txt index 5a70e7a..3dd15c0 100644 --- a/tools/test_apps/protocols/mqtt/build_test/CMakeLists.txt +++ b/tools/test_apps/protocols/mqtt/build_test/CMakeLists.txt @@ -7,4 +7,4 @@ cmake_minimum_required(VERSION 3.5) set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common) include($ENV{IDF_PATH}/tools/cmake/project.cmake) -project(mqtt_tcp) \ No newline at end of file +project(mqtt_tcp) diff --git a/tools/test_apps/protocols/mqtt/build_test/main/CMakeLists.txt b/tools/test_apps/protocols/mqtt/build_test/main/CMakeLists.txt index 9782a04..fabd82e 100644 --- a/tools/test_apps/protocols/mqtt/build_test/main/CMakeLists.txt +++ b/tools/test_apps/protocols/mqtt/build_test/main/CMakeLists.txt @@ -1,2 +1,2 @@ idf_component_register(SRCS "mqtt_app.cpp" - INCLUDE_DIRS ".") \ No newline at end of file + INCLUDE_DIRS ".") diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/main/CMakeLists.txt b/tools/test_apps/protocols/mqtt/publish_connect_test/main/CMakeLists.txt index 85e306a..d03e617 100644 --- a/tools/test_apps/protocols/mqtt/publish_connect_test/main/CMakeLists.txt +++ b/tools/test_apps/protocols/mqtt/publish_connect_test/main/CMakeLists.txt @@ -1,2 +1,2 @@ idf_component_register(SRCS "publish_test.c" "connect_test.c" "publish_connect_test.c" - INCLUDE_DIRS ".") \ No newline at end of file + INCLUDE_DIRS ".") diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/main/connect_test.c b/tools/test_apps/protocols/mqtt/publish_connect_test/main/connect_test.c index 7cee6bd..a5817eb 100644 --- a/tools/test_apps/protocols/mqtt/publish_connect_test/main/connect_test.c +++ b/tools/test_apps/protocols/mqtt/publish_connect_test/main/connect_test.c @@ -233,5 +233,3 @@ void connection_test(const char* line) } ESP_LOGI(TAG, "Test case:%d started", test_case); } - - diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/main/mqtt_eclipse_org.pem b/tools/test_apps/protocols/mqtt/publish_connect_test/main/mqtt_eclipse_org.pem index edb593b..0002462 100644 --- a/tools/test_apps/protocols/mqtt/publish_connect_test/main/mqtt_eclipse_org.pem +++ b/tools/test_apps/protocols/mqtt/publish_connect_test/main/mqtt_eclipse_org.pem @@ -24,4 +24,4 @@ wApIvJSwtmVi4MFU5aMqrSDE6ea73Mj2tcMyo5jMd6jmeWUHK8so/joWUoHOUgwu X4Po1QYz+3dszkDqMp4fklxBwXRsW10KXzPMTZ+sOPAveyxindmjkW8lGy+QsRlG PfZ+G6Z6h7mjem0Y+iWlkYcV4PIWL1iwBi8saCbGS5jN2p8M+X+Q7UNKEkROb3N6 KOqkqm57TH2H3eDJAkSnh6/DNFu0Qg== ------END CERTIFICATE----- \ No newline at end of file +-----END CERTIFICATE----- diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/main/publish_connect_test.c b/tools/test_apps/protocols/mqtt/publish_connect_test/main/publish_connect_test.c index fa7336c..a7a2504 100644 --- a/tools/test_apps/protocols/mqtt/publish_connect_test/main/publish_connect_test.c +++ b/tools/test_apps/protocols/mqtt/publish_connect_test/main/publish_connect_test.c @@ -73,4 +73,4 @@ void app_main(void) } } -} \ No newline at end of file +} From 121b7c7b1384de1fe70539ea8c8510fc6af8d20b Mon Sep 17 00:00:00 2001 From: Axel Lin Date: Fri, 6 Nov 2020 20:42:38 +0800 Subject: [PATCH 062/231] 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: 8e50d4b3967f ("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 caae6c45827c0f1fe0c34100c41f29f052c71227 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Tue, 3 Nov 2020 08:23:51 +0100 Subject: [PATCH 063/231] examples: make mqtt tcp example to report tcp-transport errno --- examples/protocols/mqtt/ssl/main/app_main.c | 4 +++- examples/protocols/mqtt/tcp/main/app_main.c | 14 ++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/examples/protocols/mqtt/ssl/main/app_main.c b/examples/protocols/mqtt/ssl/main/app_main.c index 1afb9c0..1c15a29 100644 --- a/examples/protocols/mqtt/ssl/main/app_main.c +++ b/examples/protocols/mqtt/ssl/main/app_main.c @@ -89,9 +89,11 @@ static esp_err_t mqtt_event_handler_cb(esp_mqtt_event_handle_t event) break; case MQTT_EVENT_ERROR: ESP_LOGI(TAG, "MQTT_EVENT_ERROR"); - if (event->error_handle->error_type == MQTT_ERROR_TYPE_ESP_TLS) { + if (event->error_handle->error_type == MQTT_ERROR_TYPE_TCP_TRANSPORT) { ESP_LOGI(TAG, "Last error code reported from esp-tls: 0x%x", event->error_handle->esp_tls_last_esp_err); ESP_LOGI(TAG, "Last tls stack error number: 0x%x", event->error_handle->esp_tls_stack_err); + ESP_LOGI(TAG, "Last captured errno : %d (%s)", event->error_handle->esp_transport_sock_errno, + strerror(event->error_handle->esp_transport_sock_errno)); } else if (event->error_handle->error_type == MQTT_ERROR_TYPE_CONNECTION_REFUSED) { ESP_LOGI(TAG, "Connection refused error: 0x%x", event->error_handle->connect_return_code); } else { diff --git a/examples/protocols/mqtt/tcp/main/app_main.c b/examples/protocols/mqtt/tcp/main/app_main.c index 2c2abbe..f23e774 100644 --- a/examples/protocols/mqtt/tcp/main/app_main.c +++ b/examples/protocols/mqtt/tcp/main/app_main.c @@ -33,6 +33,13 @@ static const char *TAG = "MQTT_EXAMPLE"; +static void log_error_if_nonzero(const char * message, int error_code) +{ + if (error_code != 0) { + ESP_LOGE(TAG, "Last error %s: 0x%x", message, error_code); + } +} + static esp_err_t mqtt_event_handler_cb(esp_mqtt_event_handle_t event) { esp_mqtt_client_handle_t client = event->client; @@ -75,6 +82,13 @@ static esp_err_t mqtt_event_handler_cb(esp_mqtt_event_handle_t event) break; case MQTT_EVENT_ERROR: ESP_LOGI(TAG, "MQTT_EVENT_ERROR"); + if (event->error_handle->error_type == MQTT_ERROR_TYPE_TCP_TRANSPORT) { + log_error_if_nonzero("reported from esp-tls", event->error_handle->esp_tls_last_esp_err); + log_error_if_nonzero("reported from tls stack", event->error_handle->esp_tls_stack_err); + log_error_if_nonzero("captured as transport's socket errno", event->error_handle->esp_transport_sock_errno); + ESP_LOGI(TAG, "Last errno string (%s)", strerror(event->error_handle->esp_transport_sock_errno)); + + } break; default: ESP_LOGI(TAG, "Other event id:%d", event->event_id); From 75dd50ef3f37ed4fd6736b214ed4268e279af8e3 Mon Sep 17 00:00:00 2001 From: He Yin Ling Date: Tue, 24 Nov 2020 16:40:15 +0800 Subject: [PATCH 064/231] test: remove fake binary size check in example test: the binary size check in example test was removed long time ago. Now we have updated ttfw_idf to raise exception when performance standard is not found. These fake performance check will be regarded as error. Remove them now. --- examples/protocols/mqtt/ssl/mqtt_ssl_example_test.py | 1 - examples/protocols/mqtt/tcp/mqtt_tcp_example_test.py | 1 - examples/protocols/mqtt/ws/mqtt_ws_example_test.py | 1 - examples/protocols/mqtt/wss/mqtt_wss_example_test.py | 1 - 4 files changed, 4 deletions(-) diff --git a/examples/protocols/mqtt/ssl/mqtt_ssl_example_test.py b/examples/protocols/mqtt/ssl/mqtt_ssl_example_test.py index bfd9de6..2fddd57 100644 --- a/examples/protocols/mqtt/ssl/mqtt_ssl_example_test.py +++ b/examples/protocols/mqtt/ssl/mqtt_ssl_example_test.py @@ -78,7 +78,6 @@ def test_examples_protocol_mqtt_ssl(env, extra_data): bin_size = os.path.getsize(binary_file) ttfw_idf.log_performance("mqtt_ssl_bin_size", "{}KB" .format(bin_size // 1024)) - ttfw_idf.check_performance("mqtt_ssl_size", bin_size // 1024, dut1.TARGET) # Look for host:port in sdkconfig try: value = re.search(r'\:\/\/([^:]+)\:([0-9]+)', dut1.app.get_sdkconfig()["CONFIG_BROKER_URI"]) diff --git a/examples/protocols/mqtt/tcp/mqtt_tcp_example_test.py b/examples/protocols/mqtt/tcp/mqtt_tcp_example_test.py index 3c62adc..6159481 100644 --- a/examples/protocols/mqtt/tcp/mqtt_tcp_example_test.py +++ b/examples/protocols/mqtt/tcp/mqtt_tcp_example_test.py @@ -68,7 +68,6 @@ def test_examples_protocol_mqtt_qos1(env, extra_data): binary_file = os.path.join(dut1.app.binary_path, "mqtt_tcp.bin") bin_size = os.path.getsize(binary_file) ttfw_idf.log_performance("mqtt_tcp_bin_size", "{}KB".format(bin_size // 1024)) - ttfw_idf.check_performance("mqtt_tcp_size", bin_size // 1024, dut1.TARGET) # 1. start mqtt broker sketch host_ip = get_my_ip() thread1 = Thread(target=mqqt_server_sketch, args=(host_ip,1883)) diff --git a/examples/protocols/mqtt/ws/mqtt_ws_example_test.py b/examples/protocols/mqtt/ws/mqtt_ws_example_test.py index fa24a1b..fdf1e2c 100644 --- a/examples/protocols/mqtt/ws/mqtt_ws_example_test.py +++ b/examples/protocols/mqtt/ws/mqtt_ws_example_test.py @@ -55,7 +55,6 @@ def test_examples_protocol_mqtt_ws(env, extra_data): binary_file = os.path.join(dut1.app.binary_path, "mqtt_websocket.bin") bin_size = os.path.getsize(binary_file) ttfw_idf.log_performance("mqtt_websocket_bin_size", "{}KB".format(bin_size // 1024)) - ttfw_idf.check_performance("mqtt_websocket_size", bin_size // 1024, dut1.TARGET) # Look for host:port in sdkconfig try: value = re.search(r'\:\/\/([^:]+)\:([0-9]+)', dut1.app.get_sdkconfig()["CONFIG_BROKER_URI"]) diff --git a/examples/protocols/mqtt/wss/mqtt_wss_example_test.py b/examples/protocols/mqtt/wss/mqtt_wss_example_test.py index 75f3d39..0bc1999 100644 --- a/examples/protocols/mqtt/wss/mqtt_wss_example_test.py +++ b/examples/protocols/mqtt/wss/mqtt_wss_example_test.py @@ -57,7 +57,6 @@ def test_examples_protocol_mqtt_wss(env, extra_data): binary_file = os.path.join(dut1.app.binary_path, "mqtt_websocket_secure.bin") bin_size = os.path.getsize(binary_file) ttfw_idf.log_performance("mqtt_websocket_secure_bin_size", "{}KB".format(bin_size // 1024)) - ttfw_idf.check_performance("mqtt_websocket_secure_size", bin_size // 1024, dut1.TARGET) # Look for host:port in sdkconfig try: value = re.search(r'\:\/\/([^:]+)\:([0-9]+)', dut1.app.get_sdkconfig()["CONFIG_BROKER_URI"]) From c8fa8e1cb084e05f787e322be6fd34a087c014c6 Mon Sep 17 00:00:00 2001 From: Aditya Patwardhan Date: Wed, 25 Nov 2020 13:04:04 +0530 Subject: [PATCH 065/231] Fixed typo in configure_ds.py script commmand. --- examples/protocols/mqtt/ssl_ds/configure_ds.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/protocols/mqtt/ssl_ds/configure_ds.py b/examples/protocols/mqtt/ssl_ds/configure_ds.py index 2d4f9af..2611c2c 100644 --- a/examples/protocols/mqtt/ssl_ds/configure_ds.py +++ b/examples/protocols/mqtt/ssl_ds/configure_ds.py @@ -117,7 +117,7 @@ def efuse_summary(args): def efuse_burn_key(args): - os.system("python $IDF_PATH/components/esptool_py/esptool/espefuse.py --chip esp32s2 -p %s burn_key" + os.system("python $IDF_PATH/components/esptool_py/esptool/espefuse.py --chip esp32s2 -p %s burn_key " "%s %s HMAC_DOWN_DIGITAL_SIGNATURE --no-read-protect" % ((args.port), ("BLOCK_KEY" + str(args.efuse_key_id)), (hmac_key_file))) From 0ba76d9de83436c893492f9ee048ee554d04d8b2 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Wed, 25 Nov 2020 21:21:03 +0100 Subject: [PATCH 066/231] ci: Removed remaining binary size checks in app-tests Also removes unused references to http-request app size in idf_performance.h --- tools/test_apps/protocols/mqtt/publish_connect_test/app_test.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/app_test.py b/tools/test_apps/protocols/mqtt/publish_connect_test/app_test.py index 8418775..6b4b6fe 100644 --- a/tools/test_apps/protocols/mqtt/publish_connect_test/app_test.py +++ b/tools/test_apps/protocols/mqtt/publish_connect_test/app_test.py @@ -143,7 +143,6 @@ def test_app_protocol_mqtt_publish_connect(env, extra_data): binary_file = os.path.join(dut1.app.binary_path, "mqtt_publish_connect_test.bin") bin_size = os.path.getsize(binary_file) ttfw_idf.log_performance("mqtt_publish_connect_test_bin_size", "{}KB".format(bin_size // 1024)) - ttfw_idf.check_performance("mqtt_publish_connect_test_bin_size_vin_size", bin_size // 1024, dut1.TARGET) # Look for test case symbolic names cases = {} try: From 8f5ebf26f41b46a524886b66308b405eadc01f47 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Tue, 15 Dec 2020 15:48:08 +0100 Subject: [PATCH 067/231] 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 7faa1a99fe1c6a9716d33b55a111d44427e2f07e Mon Sep 17 00:00:00 2001 From: David Cermak Date: Tue, 15 Dec 2020 16:21:29 +0100 Subject: [PATCH 068/231] ci: Extend the MQTT weekend test to check mqtt-enqueue api --- .../mqtt/weekend_test/mqtt_publish_test.py | 55 ++++++++++--------- .../publish_connect_test/main/publish_test.c | 10 +++- 2 files changed, 36 insertions(+), 29 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__': diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/main/publish_test.c b/tools/test_apps/protocols/mqtt/publish_connect_test/main/publish_test.c index c78a84e..d4863e5 100644 --- a/tools/test_apps/protocols/mqtt/publish_connect_test/main/publish_test.c +++ b/tools/test_apps/protocols/mqtt/publish_connect_test/main/publish_test.c @@ -126,13 +126,14 @@ void publish_test(const char* line) char pattern[32]; char transport[32]; int repeat = 0; + int enqueue = 0; static bool is_mqtt_init = false; if (!is_mqtt_init) { mqtt_app_start(); is_mqtt_init = true; } - sscanf(line, "%s %s %d %d %d", transport, pattern, &repeat, &expected_published, &qos_test); + sscanf(line, "%s %s %d %d %d %d", transport, pattern, &repeat, &expected_published, &qos_test, &enqueue); ESP_LOGI(TAG, "PATTERN:%s REPEATED:%d PUBLISHED:%d\n", pattern, repeat, expected_published); int pattern_size = strlen(pattern); free(expected_data); @@ -169,7 +170,12 @@ void publish_test(const char* line) xEventGroupWaitBits(mqtt_event_group, CONNECTED_BIT, false, true, portMAX_DELAY); for (int i = 0; i < expected_published; i++) { - int msg_id = esp_mqtt_client_publish(mqtt_client, CONFIG_EXAMPLE_PUBLISH_TOPIC, expected_data, expected_size, qos_test, 0); + int msg_id; + if (enqueue) { + msg_id = esp_mqtt_client_enqueue(mqtt_client, CONFIG_EXAMPLE_PUBLISH_TOPIC, expected_data, expected_size, qos_test, 0, true); + } else { + msg_id = esp_mqtt_client_publish(mqtt_client, CONFIG_EXAMPLE_PUBLISH_TOPIC, expected_data, expected_size, qos_test, 0); + } ESP_LOGI(TAG, "[%d] Publishing...", msg_id); } } From b3550c0fa2ea1aeeaa8ec0295a078b239634ff7b Mon Sep 17 00:00:00 2001 From: David Cermak Date: Tue, 15 Dec 2020 19:33:25 +0100 Subject: [PATCH 069/231] ci: Add MQTT publish test to standard test apps --- .../mqtt/weekend_test/mqtt_publish_test.py | 4 +- .../mqtt/publish_connect_test/app_test.py | 163 +++++++++++++++++- 2 files changed, 156 insertions(+), 11 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) diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/app_test.py b/tools/test_apps/protocols/mqtt/publish_connect_test/app_test.py index 6b4b6fe..78cc962 100644 --- a/tools/test_apps/protocols/mqtt/publish_connect_test/app_test.py +++ b/tools/test_apps/protocols/mqtt/publish_connect_test/app_test.py @@ -8,6 +8,11 @@ import subprocess from threading import Thread, Event import ttfw_idf import ssl +import paho.mqtt.client as mqtt +import string +import random + +DEFAULT_MSG_SIZE = 16 def _path(f): @@ -37,6 +42,109 @@ def get_my_ip(): return IP +# Publisher class creating a python client to send/receive published data from esp-mqtt client +class MqttPublisher: + + def __init__(self, dut, transport, qos, repeat, published, queue, publish_cfg, log_details=False): + # instance variables used as parameters of the publish test + self.event_stop_client = Event() + self.sample_string = ''.join(random.choice(string.ascii_uppercase + string.ascii_lowercase + string.digits) for _ in range(DEFAULT_MSG_SIZE)) + self.client = None + self.dut = dut + self.log_details = log_details + self.repeat = repeat + self.publish_cfg = publish_cfg + self.publish_cfg["qos"] = qos + self.publish_cfg["queue"] = queue + self.publish_cfg["transport"] = transport + # static variables used to pass options to and from static callbacks of paho-mqtt client + MqttPublisher.event_client_connected = Event() + MqttPublisher.event_client_got_all = Event() + MqttPublisher.published = published + MqttPublisher.event_client_connected.clear() + MqttPublisher.event_client_got_all.clear() + MqttPublisher.expected_data = self.sample_string * self.repeat + + def print_details(self, text): + if self.log_details: + print(text) + + def mqtt_client_task(self, client): + while not self.event_stop_client.is_set(): + client.loop() + + # The callback for when the client receives a CONNACK response from the server (needs to be static) + @staticmethod + def on_connect(_client, _userdata, _flags, _rc): + MqttPublisher.event_client_connected.set() + + # The callback for when a PUBLISH message is received from the server (needs to be static) + @staticmethod + def on_message(client, userdata, msg): + payload = msg.payload.decode() + if payload == MqttPublisher.expected_data: + userdata += 1 + client.user_data_set(userdata) + if userdata == MqttPublisher.published: + MqttPublisher.event_client_got_all.set() + + def __enter__(self): + + qos = self.publish_cfg["qos"] + queue = self.publish_cfg["queue"] + transport = self.publish_cfg["transport"] + broker_host = self.publish_cfg["broker_host_" + transport] + broker_port = self.publish_cfg["broker_port_" + transport] + + # Start the test + self.print_details("PUBLISH TEST: transport:{}, qos:{}, sequence:{}, enqueue:{}, sample msg:'{}'" + .format(transport, qos, MqttPublisher.published, queue, MqttPublisher.expected_data)) + + try: + if transport in ["ws", "wss"]: + self.client = mqtt.Client(transport="websockets") + else: + self.client = mqtt.Client() + self.client.on_connect = MqttPublisher.on_connect + self.client.on_message = MqttPublisher.on_message + self.client.user_data_set(0) + + if transport in ["ssl", "wss"]: + self.client.tls_set(None, None, None, cert_reqs=ssl.CERT_NONE, tls_version=ssl.PROTOCOL_TLSv1_2, ciphers=None) + self.client.tls_insecure_set(True) + self.print_details("Connecting...") + self.client.connect(broker_host, broker_port, 60) + except Exception: + self.print_details("ENV_TEST_FAILURE: Unexpected error while connecting to broker {}".format(broker_host)) + raise + # Starting a py-client in a separate thread + thread1 = Thread(target=self.mqtt_client_task, args=(self.client,)) + thread1.start() + self.print_details("Connecting py-client to broker {}:{}...".format(broker_host, broker_port)) + if not MqttPublisher.event_client_connected.wait(timeout=30): + raise ValueError("ENV_TEST_FAILURE: Test script cannot connect to broker: {}".format(broker_host)) + self.client.subscribe(self.publish_cfg["subscribe_topic"], qos) + self.dut.write(' '.join(str(x) for x in (transport, self.sample_string, self.repeat, MqttPublisher.published, qos, queue)), eol="\n") + try: + # waiting till subscribed to defined topic + self.dut.expect(re.compile(r"MQTT_EVENT_SUBSCRIBED"), timeout=30) + for _ in range(MqttPublisher.published): + self.client.publish(self.publish_cfg["publish_topic"], self.sample_string * self.repeat, qos) + self.print_details("Publishing...") + self.print_details("Checking esp-client received msg published from py-client...") + self.dut.expect(re.compile(r"Correct pattern received exactly x times"), timeout=60) + if not MqttPublisher.event_client_got_all.wait(timeout=60): + raise ValueError("Not all data received from ESP32") + print(" - all data received from ESP32") + finally: + self.event_stop_client.set() + thread1.join() + + def __exit__(self, exc_type, exc_value, traceback): + self.client.disconnect() + self.event_stop_client.clear() + + # Simple server for mqtt over TLS connection class TlsServer: @@ -143,9 +251,18 @@ def test_app_protocol_mqtt_publish_connect(env, extra_data): binary_file = os.path.join(dut1.app.binary_path, "mqtt_publish_connect_test.bin") bin_size = os.path.getsize(binary_file) ttfw_idf.log_performance("mqtt_publish_connect_test_bin_size", "{}KB".format(bin_size // 1024)) - # Look for test case symbolic names + + # Look for test case symbolic names and publish configs cases = {} + publish_cfg = {} try: + 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)) + + # Get connection test cases configuration: symbolic names for test cases for i in ["CONFIG_EXAMPLE_CONNECT_CASE_NO_CERT", "CONFIG_EXAMPLE_CONNECT_CASE_SERVER_CERT", "CONFIG_EXAMPLE_CONNECT_CASE_MUTUAL_AUTH", @@ -155,6 +272,14 @@ def test_app_protocol_mqtt_publish_connect(env, extra_data): "CONFIG_EXAMPLE_CONNECT_CASE_MUTUAL_AUTH_BAD_CRT", "CONFIG_EXAMPLE_CONNECT_CASE_NO_CERT_ALPN"]: cases[i] = dut1.app.get_sdkconfig()[i] + # Get publish test configuration + publish_cfg["publish_topic"] = dut1.app.get_sdkconfig()["CONFIG_EXAMPLE_SUBSCIBE_TOPIC"].replace('"','') + publish_cfg["subscribe_topic"] = dut1.app.get_sdkconfig()["CONFIG_EXAMPLE_PUBLISH_TOPIC"].replace('"','') + publish_cfg["broker_host_ssl"], publish_cfg["broker_port_ssl"] = get_host_port_from_dut(dut1, "CONFIG_EXAMPLE_BROKER_SSL_URI") + publish_cfg["broker_host_tcp"], publish_cfg["broker_port_tcp"] = get_host_port_from_dut(dut1, "CONFIG_EXAMPLE_BROKER_TCP_URI") + publish_cfg["broker_host_ws"], publish_cfg["broker_port_ws"] = get_host_port_from_dut(dut1, "CONFIG_EXAMPLE_BROKER_WS_URI") + publish_cfg["broker_host_wss"], publish_cfg["broker_port_wss"] = get_host_port_from_dut(dut1, "CONFIG_EXAMPLE_BROKER_WSS_URI") + except Exception: print('ENV_TEST_FAILURE: Some mandatory test case not found in sdkconfig') raise @@ -162,13 +287,14 @@ def test_app_protocol_mqtt_publish_connect(env, extra_data): dut1.start_app() esp_ip = dut1.expect(re.compile(r" IPv4 address: ([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)"), timeout=30) print("Got IP={}".format(esp_ip[0])) + # # start connection test ip = get_my_ip() set_server_cert_cn(ip) server_port = 2222 - def start_case(case, desc): + def start_connection_case(case, desc): print("Starting {}: {}".format(case, desc)) case_id = cases[case] dut1.write("conn {} {} {}".format(ip, server_port, case_id)) @@ -178,14 +304,14 @@ def test_app_protocol_mqtt_publish_connect(env, extra_data): for case in ["CONFIG_EXAMPLE_CONNECT_CASE_NO_CERT", "CONFIG_EXAMPLE_CONNECT_CASE_SERVER_CERT", "CONFIG_EXAMPLE_CONNECT_CASE_SERVER_DER_CERT"]: # All these cases connect to the server with no server verification or with server only verification with TlsServer(server_port): - test_nr = start_case(case, "default server - expect to connect normally") + test_nr = start_connection_case(case, "default server - expect to connect normally") dut1.expect("MQTT_EVENT_CONNECTED: Test={}".format(test_nr), timeout=30) with TlsServer(server_port, refuse_connection=True): - test_nr = start_case(case, "ssl shall connect, but mqtt sends connect refusal") + test_nr = start_connection_case(case, "ssl shall connect, but mqtt sends connect refusal") dut1.expect("MQTT_EVENT_ERROR: Test={}".format(test_nr), timeout=30) dut1.expect("MQTT ERROR: 0x5") # expecting 0x5 ... connection not authorized error with TlsServer(server_port, client_cert=True) as s: - test_nr = start_case(case, "server with client verification - handshake error since client presents no client certificate") + test_nr = start_connection_case(case, "server with client verification - handshake error since client presents no client certificate") dut1.expect("MQTT_EVENT_ERROR: Test={}".format(test_nr), timeout=30) dut1.expect("ESP-TLS ERROR: 0x8010") # expect ... handshake error (PEER_DID_NOT_RETURN_A_CERTIFICATE) if "PEER_DID_NOT_RETURN_A_CERTIFICATE" not in s.get_last_ssl_error(): @@ -194,12 +320,12 @@ def test_app_protocol_mqtt_publish_connect(env, extra_data): for case in ["CONFIG_EXAMPLE_CONNECT_CASE_MUTUAL_AUTH", "CONFIG_EXAMPLE_CONNECT_CASE_MUTUAL_AUTH_KEY_PWD"]: # These cases connect to server with both server and client verification (client key might be password protected) with TlsServer(server_port, client_cert=True): - test_nr = start_case(case, "server with client verification - expect to connect normally") + test_nr = start_connection_case(case, "server with client verification - expect to connect normally") dut1.expect("MQTT_EVENT_CONNECTED: Test={}".format(test_nr), timeout=30) case = "CONFIG_EXAMPLE_CONNECT_CASE_INVALID_SERVER_CERT" with TlsServer(server_port) as s: - test_nr = start_case(case, "invalid server certificate on default server - expect ssl handshake error") + test_nr = start_connection_case(case, "invalid server certificate on default server - expect ssl handshake error") dut1.expect("MQTT_EVENT_ERROR: Test={}".format(test_nr), timeout=30) dut1.expect("ESP-TLS ERROR: 0x8010") # expect ... handshake error (TLSV1_ALERT_UNKNOWN_CA) if "alert unknown ca" not in s.get_last_ssl_error(): @@ -207,7 +333,7 @@ def test_app_protocol_mqtt_publish_connect(env, extra_data): case = "CONFIG_EXAMPLE_CONNECT_CASE_MUTUAL_AUTH_BAD_CRT" with TlsServer(server_port, client_cert=True) as s: - test_nr = start_case(case, "Invalid client certificate on server with client verification - expect ssl handshake error") + test_nr = start_connection_case(case, "Invalid client certificate on server with client verification - expect ssl handshake error") dut1.expect("MQTT_EVENT_ERROR: Test={}".format(test_nr), timeout=30) dut1.expect("ESP-TLS ERROR: 0x8010") # expect ... handshake error (CERTIFICATE_VERIFY_FAILED) if "CERTIFICATE_VERIFY_FAILED" not in s.get_last_ssl_error(): @@ -215,7 +341,7 @@ def test_app_protocol_mqtt_publish_connect(env, extra_data): for case in ["CONFIG_EXAMPLE_CONNECT_CASE_NO_CERT", "CONFIG_EXAMPLE_CONNECT_CASE_NO_CERT_ALPN"]: with TlsServer(server_port, use_alpn=True) as s: - test_nr = start_case(case, "server with alpn - expect connect, check resolved protocol") + test_nr = start_connection_case(case, "server with alpn - expect connect, check resolved protocol") dut1.expect("MQTT_EVENT_CONNECTED: Test={}".format(test_nr), timeout=30) if case == "CONFIG_EXAMPLE_CONNECT_CASE_NO_CERT" and s.get_negotiated_protocol() is None: print(" - client with alpn off, no negotiated protocol: OK") @@ -224,6 +350,25 @@ def test_app_protocol_mqtt_publish_connect(env, extra_data): else: raise Exception("Unexpected negotiated protocol {}".format(s.get_negotiated_protocol())) + # + # start publish tests + def start_publish_case(transport, qos, repeat, published, queue): + print("Starting Publish test: transport:{}, qos:{}, nr_of_msgs:{}, msg_size:{}, enqueue:{}" + .format(transport, qos, published, repeat * DEFAULT_MSG_SIZE, queue)) + with MqttPublisher(dut1, transport, qos, repeat, published, queue, publish_cfg): + pass + + for qos in [0, 1, 2]: + for transport in ["tcp", "ssl", "ws", "wss"]: + for q in [0, 1]: + if publish_cfg["broker_host_" + transport] is None: + print('Skipping transport: {}...'.format(transport)) + continue + start_publish_case(transport, qos, 0, 5, q) + start_publish_case(transport, qos, 2, 5, q) + start_publish_case(transport, qos, 50, 1, q) + start_publish_case(transport, qos, 10, 20, q) + if __name__ == '__main__': test_app_protocol_mqtt_publish_connect() From ee05381ef54867e96d76fd466fdd27906fc6bc94 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Wed, 16 Dec 2020 11:13:06 +0100 Subject: [PATCH 070/231] Examples: MQTT DS example to use ds_peripheral API --- examples/protocols/mqtt/ssl_ds/main/app_main.c | 1 + 1 file changed, 1 insertion(+) diff --git a/examples/protocols/mqtt/ssl_ds/main/app_main.c b/examples/protocols/mqtt/ssl_ds/main/app_main.c index 8c8f262..c4fb87b 100644 --- a/examples/protocols/mqtt/ssl_ds/main/app_main.c +++ b/examples/protocols/mqtt/ssl_ds/main/app_main.c @@ -28,6 +28,7 @@ #include "esp_log.h" #include "mqtt_client.h" +#include "rsa_sign_alt.h" /* pre_prov - name of partition containing encrypted prv key parameters ( It is set as such to synchronize it with the pre provisioning service */ #define NVS_PARTITION_NAME "pre_prov" From cc72be6b5761f3905d1b37e1a9027132d562a058 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Tue, 5 Jan 2021 18:54:19 +0100 Subject: [PATCH 071/231] ci/mqtt: Made MQTT test app tls insecure capable This is needed for testing different connection modes in the mqtt library, specifically: * test case CONFIG_EXAMPLE_CONNECT_CASE_NO_CERT: default server - expect to connect normally --- .../protocols/mqtt/publish_connect_test/sdkconfig.ci.default | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/sdkconfig.ci.default b/tools/test_apps/protocols/mqtt/publish_connect_test/sdkconfig.ci.default index 3ae0049..fbfe696 100644 --- a/tools/test_apps/protocols/mqtt/publish_connect_test/sdkconfig.ci.default +++ b/tools/test_apps/protocols/mqtt/publish_connect_test/sdkconfig.ci.default @@ -7,3 +7,5 @@ CONFIG_MBEDTLS_ASYMMETRIC_CONTENT_LEN=y CONFIG_MBEDTLS_SSL_IN_CONTENT_LEN=16384 CONFIG_MBEDTLS_SSL_OUT_CONTENT_LEN=16384 CONFIG_ESP_NETIF_TCPIP_ADAPTER_COMPATIBLE_LAYER=n +CONFIG_ESP_TLS_INSECURE=y +CONFIG_ESP_TLS_SKIP_SERVER_CERT_VERIFY=y From 3f2c411e3785f389b27ef72b57655a11ac128a56 Mon Sep 17 00:00:00 2001 From: Euripedes Rocha Filho Date: Tue, 8 Dec 2020 18:13:39 +0000 Subject: [PATCH 072/231] examples/mqtt : Fix ssl mutual auth to use event loop The example for mqtt ssl mutual authentication is now using the event loop. Thi modification makes it uniform with all the other mqtt examples. --- examples/protocols/mqtt/ssl_mutual_auth/main/app_main.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/examples/protocols/mqtt/ssl_mutual_auth/main/app_main.c b/examples/protocols/mqtt/ssl_mutual_auth/main/app_main.c index 8c63b77..f5eb769 100644 --- a/examples/protocols/mqtt/ssl_mutual_auth/main/app_main.c +++ b/examples/protocols/mqtt/ssl_mutual_auth/main/app_main.c @@ -38,7 +38,7 @@ extern const uint8_t client_key_pem_end[] asm("_binary_client_key_end"); extern const uint8_t server_cert_pem_start[] asm("_binary_mosquitto_org_crt_start"); extern const uint8_t server_cert_pem_end[] asm("_binary_mosquitto_org_crt_end"); -static esp_err_t mqtt_event_handler(esp_mqtt_event_handle_t event) +static esp_err_t mqtt_event_handler_cb(esp_mqtt_event_handle_t event) { esp_mqtt_client_handle_t client = event->client; int msg_id; @@ -85,11 +85,15 @@ static esp_err_t mqtt_event_handler(esp_mqtt_event_handle_t event) return ESP_OK; } +static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data) { + ESP_LOGD(TAG, "Event dispatched from event loop base=%s, event_id=%d", base, event_id); + mqtt_event_handler_cb(event_data); +} + static void mqtt_app_start(void) { const esp_mqtt_client_config_t mqtt_cfg = { .uri = "mqtts://test.mosquitto.org:8884", - .event_handle = mqtt_event_handler, .client_cert_pem = (const char *)client_cert_pem_start, .client_key_pem = (const char *)client_key_pem_start, .cert_pem = (const char *)server_cert_pem_start, @@ -97,6 +101,7 @@ static void mqtt_app_start(void) ESP_LOGI(TAG, "[APP] Free memory: %d bytes", esp_get_free_heap_size()); esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg); + esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, client); esp_mqtt_client_start(client); } From a3f9fea41f9ad3a662edae4c0b9c733e7c369087 Mon Sep 17 00:00:00 2001 From: Aditya Patwardhan Date: Thu, 24 Dec 2020 20:24:38 +0530 Subject: [PATCH 073/231] esp32s2/configure_ds.py: The script now reads and parses the efuse summary of the esp32-s2 chip. * Added option to read and use previously burned efuse keys * Added option for the user to save the DS parameters for * development purpose. * Updated required documentation. --- examples/protocols/mqtt/ssl_ds/README.md | 25 ++- .../protocols/mqtt/ssl_ds/configure_ds.py | 187 +++++++++++++++--- 2 files changed, 177 insertions(+), 35 deletions(-) diff --git a/examples/protocols/mqtt/ssl_ds/README.md b/examples/protocols/mqtt/ssl_ds/README.md index 01c9758..a41d9a7 100644 --- a/examples/protocols/mqtt/ssl_ds/README.md +++ b/examples/protocols/mqtt/ssl_ds/README.md @@ -110,22 +110,35 @@ can be provided with python configure_ds.py --private-key /* path to client (rsa) prv key */ ``` -2. Randomly Calculate the `HMAC_KEY` and the `initialization vector`(IV).Then calculate the encrypted private key parameters from client private key (step i) and newly generated parameters. These encrypted private key parameters are required for the DS peripheral to perform the Digital Signature operation. +2. Randomly Calculate the `HMAC_KEY` and the `initialization vector`(IV). Then calculate the encrypted private key parameters from client private key (step i) and newly generated parameters. These encrypted private key parameters are required for the DS peripheral to perform the Digital Signature operation. -3. Store `HMAC_KEY` in one of the efuse key blocks (in the hardware). - The ID of the efuse key block ( should be in range 1-5) can be provided with the following option. ( default value of 1 is used if not provided), +3. Store the `HMAC_KEY` in one of the efuse key blocks (in the hardware). + The ID of the efuse key block ( should be in range 1-5) can be provided with the following option. (default value of 1 is used if not provided), ``` python configure_ds.py --efuse_key_id /* key id in range 1-5 */ ``` Currently for development purposes, the `HMAC_KEY` is stored in the efuse key block without read protection so that read operation can be performed on the same key block. -> You can burn (write) a key on an efuse key block only once.Please use a different block ID, if you want to use a different `HMAC_KEY` for the DS operation. +> You can burn (write) a key on an efuse key block only once. Please use a different key block ID if you want to use a different `HMAC_KEY` for the DS operation. -4. Create an NVS partition of the name `pre_prov.csv` (in `esp_ds_data` folder) which contains the required encrypted private key parameters. A bin file of the nvs partition (`pre_prov.bin`) is also created and is flashed on the device. As we have added a custom partition, the example is set to use the custom partition table by adding the required option in `sdkconfig.defaults`. +4. Create an NVS partition of the name `pre_prov.csv` (in `esp_ds_data` folder) which contains the required encrypted private key parameters. A bin file of the nvs partition (`pre_prov.bin`) is also created. As we have added a custom partition, the example is set to use the custom partition table by adding the required option in `sdkconfig.defaults`. -5. (optional) The script can be made to print the summary of the efuse on the chip by providing the following option.When this option is enabled, no other operations in the script are performed. +5. (optional) The script can be made to print the summary of the efuse on the chip by providing the following option. When this option is enabled, no other operations in the script are performed. ``` python configure_ds.py --summary ``` +6. (optional) If the user wants to keep the encrypted private key data and the randomly generated `HMAC_KEY` on the host machine for testing purpose. The following option may be used. +``` + python configure_ds.py --keep_ds_data_on_host +``` + The respective files will be stored in the `esp_ds_data` folder which is generated by the script in the same directory. The contents of the `esp_ds_data` folder may be overwritten when the `configure_ds.py` script is executed again. + +7. (optional) If the user wants to use the script for production usecase then this option can be used. +Currently for development purpose, the script disables the read protection on the efuse key block by default. +In case of a production usecase it is recommeneded to enable the read protection for the efuse key block. It can be done by providing following option along with other required options: +``` + python configure_ds.py --production +``` + > A list of all the supported options in the script can be obtained by executing `python configure_ds.py --help`. diff --git a/examples/protocols/mqtt/ssl_ds/configure_ds.py b/examples/protocols/mqtt/ssl_ds/configure_ds.py index 2611c2c..42a6b79 100644 --- a/examples/protocols/mqtt/ssl_ds/configure_ds.py +++ b/examples/protocols/mqtt/ssl_ds/configure_ds.py @@ -17,6 +17,8 @@ import sys import hashlib import hmac import struct +import subprocess +import json from cryptography.hazmat.backends import default_backend from cryptography.hazmat.primitives import serialization from cryptography.hazmat.primitives.asymmetric import rsa @@ -32,6 +34,8 @@ except ImportError: sys.path.insert(0, os.path.join(idf_path, "components", "nvs_flash", "nvs_partition_generator")) import nvs_partition_gen as nvs_gen +# Check python version is proper or not to avoid script failure +assert sys.version_info >= (3, 6, 0), "Python version too low." esp_ds_data_dir = 'esp_ds_data' # hmac_key_file is generated when HMAC_KEY is calculated, it is used when burning HMAC_KEY to efuse @@ -39,6 +43,23 @@ hmac_key_file = esp_ds_data_dir + '/hmac_key.bin' # csv and bin filenames are default filenames for nvs partition files created with this script csv_filename = esp_ds_data_dir + '/pre_prov.csv' bin_filename = esp_ds_data_dir + '/pre_prov.bin' +expected_json_path = os.path.join('build', 'config', 'sdkconfig.json') +# Targets supported by the script +supported_targets = {'esp32s2'} + + +# @return +# on success idf_target - value of the IDF_TARGET read from build/config/sdkconfig.json +# on failure None +def get_idf_target(): + if os.path.exists(expected_json_path): + sdkconfig = json.load(open(expected_json_path)) + idf_target_read = sdkconfig['IDF_TARGET'] + return idf_target_read + else: + print("ERROR: IDF_TARGET has not been set for the supported targets," + "\nplase execute command \"idf.py set-target {TARGET}\" in the example directory") + return None def load_privatekey(key_file_path, password=None): @@ -58,11 +79,14 @@ def number_as_bytes(number, pad_bits=None): return result -def calculate_ds_parameters(privkey, priv_key_pass): +def calculate_ds_parameters(privkey, priv_key_pass, hmac_key): private_key = load_privatekey(privkey, priv_key_pass) if not isinstance(private_key, rsa.RSAPrivateKey): print("Only RSA private keys are supported") sys.exit(-1) + if hmac_key is None: + print("hmac_key cannot be None") + sys.exit(-2) priv_numbers = private_key.private_numbers() pub_numbers = private_key.public_key().public_numbers() @@ -74,10 +98,6 @@ def calculate_ds_parameters(privkey, priv_key_pass): print("Key size not supported, supported sizes are" + str(supported_key_size)) sys.exit(-1) - hmac_key = os.urandom(32) - with open(hmac_key_file, 'wb') as key_file: - key_file.write(hmac_key) - iv = os.urandom(16) rr = 1 << (key_size * 2) @@ -112,14 +132,22 @@ def calculate_ds_parameters(privkey, priv_key_pass): return c, iv, key_size -def efuse_summary(args): - os.system("python $IDF_PATH/components/esptool_py/esptool/espefuse.py --chip esp32s2 -p %s summary" % (args.port)) +def efuse_summary(args, idf_target): + os.system("python $IDF_PATH/components/esptool_py/esptool/espefuse.py --chip {0} -p {1} summary".format(idf_target, (args.port))) -def efuse_burn_key(args): - os.system("python $IDF_PATH/components/esptool_py/esptool/espefuse.py --chip esp32s2 -p %s burn_key " - "%s %s HMAC_DOWN_DIGITAL_SIGNATURE --no-read-protect" - % ((args.port), ("BLOCK_KEY" + str(args.efuse_key_id)), (hmac_key_file))) +def efuse_burn_key(args, idf_target): + # In case of a development (default) usecase we disable the read protection. + key_block_status = '--no-read-protect' + + if args.production is True: + # Whitespace character will have no additional effect on the command and + # read protection will be enabled as the default behaviour of the command + key_block_status = ' ' + + os.system("python $IDF_PATH/components/esptool_py/esptool/espefuse.py --chip {0} -p {1} burn_key " + "{2} {3} HMAC_DOWN_DIGITAL_SIGNATURE {4}" + .format((idf_target), (args.port), ("BLOCK_KEY" + str(args.efuse_key_id)), (hmac_key_file), (key_block_status))) def generate_csv_file(c, iv, hmac_key_id, key_size, csv_file): @@ -153,14 +181,98 @@ def generate_nvs_partition(input_filename, output_filename): nvs_gen.generate(nvs_args, is_encr_enabled=False, encr_key=None) +def get_efuse_summary_json(args, idf_target): + _efuse_summary = None + try: + _efuse_summary = subprocess.check_output(("python $IDF_PATH/components/esptool_py/esptool/espefuse.py " + "--chip {0} -p {1} summary --format json".format(idf_target, (args.port))), shell=True) + except subprocess.CalledProcessError as e: + print((e.output).decode('UTF-8')) + sys.exit(-1) + + _efuse_summary = _efuse_summary.decode('UTF-8') + # Remove everything before actual json data from efuse_summary command output. + _efuse_summary = _efuse_summary[_efuse_summary.find('{'):] + try: + _efuse_summary_json = json.loads(_efuse_summary) + except json.JSONDecodeError: + print('ERROR: failed to parse the json output') + sys.exit(-1) + return _efuse_summary_json + + +def configure_efuse_key_block(args, idf_target): + efuse_summary_json = get_efuse_summary_json(args, idf_target) + key_blk = 'BLOCK_KEY' + str(args.efuse_key_id) + key_purpose = 'KEY_PURPOSE_' + str(args.efuse_key_id) + + kb_writeable = efuse_summary_json[key_blk]['writeable'] + kb_readable = efuse_summary_json[key_blk]['readable'] + hmac_key_read = None + + # If the efuse key block is writable (empty) then generate and write + # the new hmac key and check again + # If the efuse key block is not writable (already contains a key) then check if it is redable + if kb_writeable is True: + print('Provided key block is writable\n Generating new key and burning it in the efuse..\n') + + new_hmac_key = os.urandom(32) + with open(hmac_key_file, 'wb') as key_file: + key_file.write(new_hmac_key) + # Burn efuse key + efuse_burn_key(args, idf_target) + # Read fresh summary of the efuse to read the key value from efuse. + # If the key read from efuse matches with the key generated + # on host then burn_key operation was successfull + new_efuse_summary_json = get_efuse_summary_json(args, idf_target) + hmac_key_read = new_efuse_summary_json[key_blk]['value'] + hmac_key_read = bytes.fromhex(hmac_key_read) + if new_hmac_key == hmac_key_read: + print('Key was successfully written in the efuse') + else: + print('Error in burning hmac key to efuse ,\nPlease execute the script again using a different key id') + else: + # If the efuse key block is redable, then read the key from efuse block and use it for encrypting the RSA private key parameters. + # If the efuse key block is not redable or it has key purpose set to a different + # value than "HMAC_DOWN_DIGITAL_SIGNATURE" then we cannot use it for DS operation + if kb_readable is True: + if efuse_summary_json[key_purpose]['value'] == 'HMAC_DOWN_DIGITAL_SIGNATURE': + print("Provided efuse key block already contains a key with key_purpose=HMAC_DOWN_DIGITAL_SIGNATURE," + "\nusing the same key for encrypting private key data...\n") + hmac_key_read = efuse_summary_json[key_blk]['value'] + hmac_key_read = bytes.fromhex(hmac_key_read) + if args.keep_ds_data is True: + with open(hmac_key_file, 'wb') as key_file: + key_file.write(new_hmac_key) + else: + print("Provided efuse key block contains a key with key purpose different" + "than HMAC_DOWN_DIGITAL_SIGNATURE,\nplease execute the script again with a different value of the efuse key id.") + sys.exit(0) + else: + print('Provided efuse key block is not readable and writeable,\nplease execute the script again with a different value of the efuse key id.') + sys.exit(0) + + # Return the hmac key read from the efuse + return hmac_key_read + + +def cleanup(args): + if args.keep_ds_data is False: + if os.path.exists(hmac_key_file): + os.remove(hmac_key_file) + if os.path.exists(csv_filename): + os.remove(csv_filename) + + def main(): - parser = argparse.ArgumentParser(description='''Genereate an nvs partition containing the DS private key parameters from the client private key, - Generate an HMAC key and burn it in the desired efuse key block (required for Digital Signature)''') + parser = argparse.ArgumentParser(description='''Generate an HMAC key and burn it in the desired efuse key block (required for Digital Signature), + Generates an NVS partition containing the encrypted private key parameters from the client private key. + ''') parser.add_argument( '--private-key', dest='privkey', - default='main/client.key', + default='client.key', metavar='relative/path/to/client-priv-key', help='relative path to client private key') @@ -173,7 +285,7 @@ def main(): parser.add_argument( '--summary', dest='summary',action='store_true', - help='Provide this option to print efuse summary the chip') + help='Provide this option to print efuse summary of the chip') parser.add_argument( '--efuse_key_id', @@ -187,34 +299,51 @@ def main(): dest='port', metavar='[port]', required=True, - help='UART com port to which ESP device is connected') + help='UART com port to which the ESP device is connected') parser.add_argument( - '--overwrite', - dest='overwrite', action='store_true', - help='Overwrite previously generated keys') + '--keep_ds_data_on_host','-keep_ds_data', + dest='keep_ds_data', action='store_true', + help='Keep encrypted private key data and key on host machine for testing purpose') + + parser.add_argument( + '--production', '-prod', + dest='production', action='store_true', + help='Enable production configurations. e.g.keep efuse key block read protection enabled') args = parser.parse_args() + idf_target = get_idf_target() + if idf_target not in supported_targets: + if idf_target is not None: + print('ERROR: The script does not support the target %s' % idf_target) + sys.exit(-1) + idf_target = str(idf_target) + if args.summary is not False: - efuse_summary(args) + efuse_summary(args, idf_target) sys.exit(0) + if (os.path.exists(args.privkey) is False): + print('ERROR: The provided private key file does not exist') + sys.exit(-1) + if (os.path.exists(esp_ds_data_dir) is False): os.makedirs(esp_ds_data_dir) - else: - if (args.overwrite is False): - print("WARNING: previous ecrypted private key data exists.\nIf you want to overwrite," - " please execute your command with providing \"--overwrite\" option") - sys.exit(0) - else: - print("overwriting previous encrypted private key data, as you have provided \"--overwrite\" option") - c, iv, key_size = calculate_ds_parameters(args.privkey, args.priv_key_pass) - efuse_burn_key(args) + # Burn hmac_key on the efuse block (if it is empty) or read it + # from the efuse block (if the efuse block already contains a key). + hmac_key_read = configure_efuse_key_block(args, idf_target) + if hmac_key_read is None: + sys.exit(-1) + # Calculate the encrypted private key data along with all other parameters + c, iv, key_size = calculate_ds_parameters(args.privkey, args.priv_key_pass, hmac_key_read) + + # Generate csv file for the DS data and generate an NVS partition. generate_csv_file(c, iv, args.efuse_key_id, key_size, csv_filename) generate_nvs_partition(csv_filename, bin_filename) + cleanup(args) if __name__ == "__main__": From a25f7b73e4fea894f5d03dc1393688bf440f2cb4 Mon Sep 17 00:00:00 2001 From: Aditya Patwardhan Date: Mon, 11 Jan 2021 10:26:31 +0530 Subject: [PATCH 074/231] configure_ds.py: Add function documentation for improving redability. --- examples/protocols/mqtt/ssl_ds/README.md | 2 +- .../protocols/mqtt/ssl_ds/configure_ds.py | 63 ++++++++++++++----- 2 files changed, 49 insertions(+), 16 deletions(-) diff --git a/examples/protocols/mqtt/ssl_ds/README.md b/examples/protocols/mqtt/ssl_ds/README.md index a41d9a7..d3f8cdb 100644 --- a/examples/protocols/mqtt/ssl_ds/README.md +++ b/examples/protocols/mqtt/ssl_ds/README.md @@ -47,7 +47,7 @@ Please note, that the supplied file `client.crt` in the `main` directory is only * The DS peripheral can be configured with the python script [configure_ds.py](README.md#configure_ds-py) by executing the following command ``` - python configure_ds.py --port /* USB COM port */ --private_key /* RSA priv key */ + python configure_ds.py --port /* USB COM port */ --private-key /* RSA priv key */ ``` In the command USB COM port is nothing but the serial port to which the ESP32-S2 chip is connected. see diff --git a/examples/protocols/mqtt/ssl_ds/configure_ds.py b/examples/protocols/mqtt/ssl_ds/configure_ds.py index 42a6b79..0d5b294 100644 --- a/examples/protocols/mqtt/ssl_ds/configure_ds.py +++ b/examples/protocols/mqtt/ssl_ds/configure_ds.py @@ -79,13 +79,24 @@ def number_as_bytes(number, pad_bits=None): return result +# @return +# c : ciphertext_c +# iv : initialization vector +# key_size : key size of the RSA private key in bytes. +# @input +# privkey : path to the RSA private key +# priv_key_pass : path to the RSA privaete key password +# hmac_key : HMAC key value ( to calculate DS params) +# @info +# The function calculates the encrypted private key parameters. +# Consult the DS documentation (available for the ESP32-S2) in the esp-idf programming guide for more details about the variables and calculations. def calculate_ds_parameters(privkey, priv_key_pass, hmac_key): private_key = load_privatekey(privkey, priv_key_pass) if not isinstance(private_key, rsa.RSAPrivateKey): - print("Only RSA private keys are supported") + print("ERROR: Only RSA private keys are supported") sys.exit(-1) if hmac_key is None: - print("hmac_key cannot be None") + print("ERROR: hmac_key cannot be None") sys.exit(-2) priv_numbers = private_key.private_numbers() @@ -132,10 +143,14 @@ def calculate_ds_parameters(privkey, priv_key_pass, hmac_key): return c, iv, key_size +# @info +# The function makes use of the "espefuse.py" script to read the efuse summary def efuse_summary(args, idf_target): os.system("python $IDF_PATH/components/esptool_py/esptool/espefuse.py --chip {0} -p {1} summary".format(idf_target, (args.port))) +# @info +# The function makes use of the "espefuse.py" script to burn the HMAC key on the efuse. def efuse_burn_key(args, idf_target): # In case of a development (default) usecase we disable the read protection. key_block_status = '--no-read-protect' @@ -150,6 +165,9 @@ def efuse_burn_key(args, idf_target): .format((idf_target), (args.port), ("BLOCK_KEY" + str(args.efuse_key_id)), (hmac_key_file), (key_block_status))) +# @info +# Generate a custom csv file of encrypted private key parameters. +# The csv file is required by the nvs_partition_generator utility to create the nvs partition. def generate_csv_file(c, iv, hmac_key_id, key_size, csv_file): with open(csv_file, 'wt', encoding='utf8') as f: @@ -167,6 +185,9 @@ class DefineArgs(object): self.__setattr__(key, value) +# @info +# This function uses the nvs_partition_generater utility +# to generate the nvs partition of the encrypted private key parameters. def generate_nvs_partition(input_filename, output_filename): nvs_args = DefineArgs({ @@ -181,6 +202,8 @@ def generate_nvs_partition(input_filename, output_filename): nvs_gen.generate(nvs_args, is_encr_enabled=False, encr_key=None) +# @return +# The json formatted summary of the efuse. def get_efuse_summary_json(args, idf_target): _efuse_summary = None try: @@ -201,6 +224,13 @@ def get_efuse_summary_json(args, idf_target): return _efuse_summary_json +# @return +# on success: 256 bit HMAC key present in the given key_block (args.efuse_key_id) +# on failure: None +# @info +# This function configures the provided efuse key_block. +# If the provided efuse key_block is empty the function generates a new HMAC key and burns it in the efuse key_block. +# If the key_block already contains a key the function reads the key from the efuse key_block def configure_efuse_key_block(args, idf_target): efuse_summary_json = get_efuse_summary_json(args, idf_target) key_blk = 'BLOCK_KEY' + str(args.efuse_key_id) @@ -214,7 +244,7 @@ def configure_efuse_key_block(args, idf_target): # the new hmac key and check again # If the efuse key block is not writable (already contains a key) then check if it is redable if kb_writeable is True: - print('Provided key block is writable\n Generating new key and burning it in the efuse..\n') + print('Provided key block (KEY BLOCK %1d) is writable\n Generating a new key and burning it in the efuse..\n' % (args.efuse_key_id)) new_hmac_key = os.urandom(32) with open(hmac_key_file, 'wb') as key_file: @@ -228,32 +258,35 @@ def configure_efuse_key_block(args, idf_target): hmac_key_read = new_efuse_summary_json[key_blk]['value'] hmac_key_read = bytes.fromhex(hmac_key_read) if new_hmac_key == hmac_key_read: - print('Key was successfully written in the efuse') + print('Key was successfully written to the efuse (KEY BLOCK %1d)' % (args.efuse_key_id)) else: - print('Error in burning hmac key to efuse ,\nPlease execute the script again using a different key id') + print("ERROR: Failed to burn the hmac key to efuse (KEY BLOCK %1d)," + "\nPlease execute the script again using a different key id" % (args.efuse_key_id)) + return None else: # If the efuse key block is redable, then read the key from efuse block and use it for encrypting the RSA private key parameters. # If the efuse key block is not redable or it has key purpose set to a different # value than "HMAC_DOWN_DIGITAL_SIGNATURE" then we cannot use it for DS operation if kb_readable is True: if efuse_summary_json[key_purpose]['value'] == 'HMAC_DOWN_DIGITAL_SIGNATURE': - print("Provided efuse key block already contains a key with key_purpose=HMAC_DOWN_DIGITAL_SIGNATURE," - "\nusing the same key for encrypting private key data...\n") + print("Provided efuse key block (KEY BLOCK %1d) already contains a key with key_purpose=HMAC_DOWN_DIGITAL_SIGNATURE," + "\nusing the same key for encrypting the private key data...\n" % (args.efuse_key_id)) hmac_key_read = efuse_summary_json[key_blk]['value'] hmac_key_read = bytes.fromhex(hmac_key_read) if args.keep_ds_data is True: with open(hmac_key_file, 'wb') as key_file: - key_file.write(new_hmac_key) + key_file.write(hmac_key_read) else: - print("Provided efuse key block contains a key with key purpose different" - "than HMAC_DOWN_DIGITAL_SIGNATURE,\nplease execute the script again with a different value of the efuse key id.") - sys.exit(0) + print("ERROR: Provided efuse key block ((KEY BLOCK %1d)) contains a key with key purpose different" + "than HMAC_DOWN_DIGITAL_SIGNATURE,\nplease execute the script again with a different value of the efuse key id." % (args.efuse_key_id)) + return None else: - print('Provided efuse key block is not readable and writeable,\nplease execute the script again with a different value of the efuse key id.') - sys.exit(0) + print("ERROR: Provided efuse key block (KEY BLOCK %1d) is not readable and writeable," + "\nplease execute the script again with a different value of the efuse key id." % (args.efuse_key_id)) + return None - # Return the hmac key read from the efuse - return hmac_key_read + # Return the hmac key read from the efuse + return hmac_key_read def cleanup(args): From e64379783b4e1495dce32b5cf00be3d9d1817427 Mon Sep 17 00:00:00 2001 From: Aditya Patwardhan Date: Sun, 17 Jan 2021 21:38:34 +0530 Subject: [PATCH 075/231] esp32c3/Digital Signature: mbedtls integration through ESP-TLS --- examples/protocols/mqtt/ssl_ds/README.md | 14 +++--- .../protocols/mqtt/ssl_ds/configure_ds.py | 50 ++++++++++++------- .../protocols/mqtt/ssl_ds/main/CMakeLists.txt | 3 +- .../protocols/mqtt/ssl_ds/main/app_main.c | 12 ++--- 4 files changed, 46 insertions(+), 33 deletions(-) diff --git a/examples/protocols/mqtt/ssl_ds/README.md b/examples/protocols/mqtt/ssl_ds/README.md index d3f8cdb..20a06d0 100644 --- a/examples/protocols/mqtt/ssl_ds/README.md +++ b/examples/protocols/mqtt/ssl_ds/README.md @@ -1,8 +1,8 @@ -| Supported Targets | ESP32-S2 | +| Supported Targets | ESP32-S2 | ESP32-C3 | # ESP-MQTT SSL Mutual Authentication with Digital Signature (See the README.md file in the upper level 'examples' directory for more information about examples.) -Espressif's ESP32-S2 MCU has a built-in Digital Signature (DS) Peripheral, which provides hardware acceleration for RSA signature. More details can be found at [Digital Signature with ESP-TLS](https://docs.espressif.com/projects/esp-idf/en/latest/esp32s2/api-reference/protocols/esp_tls.html#digital-signature-with-esp-tls). +Espressif's ESP32-S2 and ESP32-C3 MCU have a built-in Digital Signature (DS) Peripheral, which provides hardware acceleration for RSA signature. More details can be found at [Digital Signature with ESP-TLS](https://docs.espressif.com/projects/esp-idf/en/latest/esp32s2/api-reference/protocols/esp_tls.html#digital-signature-with-esp-tls). This example connects to the broker test.mosquitto.org using ssl transport with client certificate(RSA) and as a demonstration subscribes/unsubscribes and sends a message on certain topic.The RSA signature operation required in the ssl connection is performed with help of the Digital Signature (DS) peripheral. (Please note that the public broker is maintained by the community so may not be always available, for details please visit http://test.mosquitto.org) @@ -12,14 +12,14 @@ It uses ESP-MQTT library which implements mqtt client to connect to mqtt broker. ### Hardware Required -This example can be executed on any ESP32-S2 board (which has a built-in DS peripheral), the only required interface is WiFi and connection to internet. +This example can be executed on any ESP32-S2, ESP32-C3 board (which has a built-in DS peripheral), the only required interface is WiFi and connection to internet. ### Configure the project #### 1) Selecting the target -As the project is to be built for the target ESP32-S2, it should be selected with the following command +As the project is to be built for the target ESP32-S2, ESP32-C3 it should be selected with the following command ``` -idf.py set-target esp32s2 +idf.py set-target /* target */ ``` More detials can be found at [Selecting the target](https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-guides/build-system.html#selecting-the-target). @@ -50,7 +50,7 @@ Please note, that the supplied file `client.crt` in the `main` directory is only python configure_ds.py --port /* USB COM port */ --private-key /* RSA priv key */ ``` -In the command USB COM port is nothing but the serial port to which the ESP32-S2 chip is connected. see +In the command USB COM port is nothing but the serial port to which the ESP chip is connected. see [check serial port](https://docs.espressif.com/projects/esp-idf/en/latest/esp32/get-started/establish-serial-connection.html#check-port-on-windows) for more details. RSA private key is nothing but the client private key ( RSA ) generated in Step 2. @@ -99,7 +99,7 @@ DATA=data ### configure_ds.py -The script [configure_ds.py](./configure_ds.py) is used for configuring the DS peripheral on the ESP32-S2 SoC. The steps in the script are based on technical details of certain operations in the Digital Signature calculation, which can be found at Digital Signature Section of [ESP32-S2 TRM](https://www.espressif.com/sites/default/files/documentation/esp32-s2_technical_reference_manual_en.pdf) +The script [configure_ds.py](./configure_ds.py) is used for configuring the DS peripheral on the ESP32-S2/ESP32-C3 SoC. The steps in the script are based on technical details of certain operations in the Digital Signature calculation, which can be found at Digital Signature Section of [ESP32-S2 TRM](https://www.espressif.com/sites/default/files/documentation/esp32-s2_technical_reference_manual_en.pdf) The configuration script performs the following steps - diff --git a/examples/protocols/mqtt/ssl_ds/configure_ds.py b/examples/protocols/mqtt/ssl_ds/configure_ds.py index 0d5b294..374309a 100644 --- a/examples/protocols/mqtt/ssl_ds/configure_ds.py +++ b/examples/protocols/mqtt/ssl_ds/configure_ds.py @@ -12,13 +12,14 @@ # See the License for the specific language governing permissions and # limitations under the License. import argparse -import os -import sys import hashlib import hmac +import json +import os import struct import subprocess -import json +import sys + from cryptography.hazmat.backends import default_backend from cryptography.hazmat.primitives import serialization from cryptography.hazmat.primitives.asymmetric import rsa @@ -45,7 +46,8 @@ csv_filename = esp_ds_data_dir + '/pre_prov.csv' bin_filename = esp_ds_data_dir + '/pre_prov.bin' expected_json_path = os.path.join('build', 'config', 'sdkconfig.json') # Targets supported by the script -supported_targets = {'esp32s2'} +supported_targets = {'esp32s2', 'esp32c3'} +supported_key_size = {'esp32s2':[1024, 2048, 3072, 4096], 'esp32c3':[1024, 2048, 3072]} # @return @@ -87,10 +89,11 @@ def number_as_bytes(number, pad_bits=None): # privkey : path to the RSA private key # priv_key_pass : path to the RSA privaete key password # hmac_key : HMAC key value ( to calculate DS params) +# idf_target : The target chip for the script (e.g. esp32s2, esp32c3) # @info # The function calculates the encrypted private key parameters. # Consult the DS documentation (available for the ESP32-S2) in the esp-idf programming guide for more details about the variables and calculations. -def calculate_ds_parameters(privkey, priv_key_pass, hmac_key): +def calculate_ds_parameters(privkey, priv_key_pass, hmac_key, idf_target): private_key = load_privatekey(privkey, priv_key_pass) if not isinstance(private_key, rsa.RSAPrivateKey): print("ERROR: Only RSA private keys are supported") @@ -104,9 +107,9 @@ def calculate_ds_parameters(privkey, priv_key_pass, hmac_key): Y = priv_numbers.d M = pub_numbers.n key_size = private_key.key_size - supported_key_size = [1024, 2048, 3072, 4096] - if key_size not in supported_key_size: - print("Key size not supported, supported sizes are" + str(supported_key_size)) + if key_size not in supported_key_size[idf_target]: + print("ERROR: Private key size {0} not supported for the target {1},\nthe supported key sizes are {2}" + .format(key_size, idf_target, str(supported_key_size[idf_target]))) sys.exit(-1) iv = os.urandom(16) @@ -117,25 +120,34 @@ def calculate_ds_parameters(privkey, priv_key_pass, hmac_key): mprime &= 0xFFFFFFFF length = key_size // 32 - 1 + # get max supported key size for the respective target + max_len = max(supported_key_size[idf_target]) aes_key = hmac.HMAC(hmac_key, b"\xFF" * 32, hashlib.sha256).digest() - md_in = number_as_bytes(Y, 4096) + \ - number_as_bytes(M, 4096) + \ - number_as_bytes(rinv, 4096) + \ + md_in = number_as_bytes(Y, max_len) + \ + number_as_bytes(M, max_len) + \ + number_as_bytes(rinv, max_len) + \ struct.pack("efuse_key_id); if (esp_ret != ESP_OK) { - ESP_LOGE(TAG, "Error in efuse_key_id value from nvs, returned %02x", esp_ret); + ESP_LOGE(TAG, "Error in efuse_key_id value from nvs,\nreturned %02x (%s)", esp_ret, esp_err_to_name(esp_ret)); goto exit; } esp_ret = nvs_get_u16(esp_ds_nvs_handle, NVS_RSA_LEN, &ds_data_ctx->rsa_length_bits); if (esp_ret != ESP_OK) { - ESP_LOGE(TAG, "Error in reading rsa key length value from nvs, returned %02x", esp_ret); + ESP_LOGE(TAG, "Error in reading rsa key length value from nvs,\nreturned %02x (%s)", esp_ret, esp_err_to_name(esp_ret)); goto exit; } size_t blob_length = ESP_DS_C_LEN; esp_ret = nvs_get_blob(esp_ds_nvs_handle, NVS_CIPHER_C, (void *)(ds_data_ctx->esp_ds_data->c), &blob_length); if ((esp_ret != ESP_OK) || (blob_length != ESP_DS_C_LEN)) { - ESP_LOGE(TAG, "Error in reading initialization vector value from nvs,bytes_read = %d, returned %02x", blob_length, esp_ret); + ESP_LOGE(TAG, "Error in reading ciphertext_c value from nvs,bytes_read = %d,\nreturned %02x (%s)", blob_length, esp_ret, esp_err_to_name(esp_ret)); goto exit; } blob_length = ESP_DS_IV_LEN; esp_ret = nvs_get_blob(esp_ds_nvs_handle, NVS_IV, (void *)(ds_data_ctx->esp_ds_data->iv), &blob_length); if ((esp_ret != ESP_OK) || (blob_length != ESP_DS_IV_LEN)) { - ESP_LOGE(TAG, "Error in reading initialization vector value from nvs,bytes_read = %d, returned %02x", blob_length, esp_ret); + ESP_LOGE(TAG, "Error in reading initialization vector value from nvs,bytes_read = %d,\nreturned %02x (%s)", blob_length, esp_ret, esp_err_to_name(esp_ret)); goto exit; } From 13f6635a51a62d98b1afd9db8596201324a5b2fe Mon Sep 17 00:00:00 2001 From: Aditya Patwardhan Date: Fri, 22 Jan 2021 12:11:04 +0530 Subject: [PATCH 076/231] configure_ds.py: Fix double quoted strings ( pre_hook check ) --- .../protocols/mqtt/ssl_ds/configure_ds.py | 68 +++++++++---------- 1 file changed, 34 insertions(+), 34 deletions(-) diff --git a/examples/protocols/mqtt/ssl_ds/configure_ds.py b/examples/protocols/mqtt/ssl_ds/configure_ds.py index 374309a..b718052 100644 --- a/examples/protocols/mqtt/ssl_ds/configure_ds.py +++ b/examples/protocols/mqtt/ssl_ds/configure_ds.py @@ -29,14 +29,14 @@ from cryptography.utils import int_to_bytes try: import nvs_partition_gen as nvs_gen except ImportError: - idf_path = os.getenv("IDF_PATH") + idf_path = os.getenv('IDF_PATH') if not idf_path or not os.path.exists(idf_path): - raise Exception("IDF_PATH not found") - sys.path.insert(0, os.path.join(idf_path, "components", "nvs_flash", "nvs_partition_generator")) + raise Exception('IDF_PATH not found') + sys.path.insert(0, os.path.join(idf_path, 'components', 'nvs_flash', 'nvs_partition_generator')) import nvs_partition_gen as nvs_gen # Check python version is proper or not to avoid script failure -assert sys.version_info >= (3, 6, 0), "Python version too low." +assert sys.version_info >= (3, 6, 0), 'Python version too low.' esp_ds_data_dir = 'esp_ds_data' # hmac_key_file is generated when HMAC_KEY is calculated, it is used when burning HMAC_KEY to efuse @@ -59,7 +59,7 @@ def get_idf_target(): idf_target_read = sdkconfig['IDF_TARGET'] return idf_target_read else: - print("ERROR: IDF_TARGET has not been set for the supported targets," + print('ERROR: IDF_TARGET has not been set for the supported targets,' "\nplase execute command \"idf.py set-target {TARGET}\" in the example directory") return None @@ -96,10 +96,10 @@ def number_as_bytes(number, pad_bits=None): def calculate_ds_parameters(privkey, priv_key_pass, hmac_key, idf_target): private_key = load_privatekey(privkey, priv_key_pass) if not isinstance(private_key, rsa.RSAPrivateKey): - print("ERROR: Only RSA private keys are supported") + print('ERROR: Only RSA private keys are supported') sys.exit(-1) if hmac_key is None: - print("ERROR: hmac_key cannot be None") + print('ERROR: hmac_key cannot be None') sys.exit(-2) priv_numbers = private_key.private_numbers() @@ -108,7 +108,7 @@ def calculate_ds_parameters(privkey, priv_key_pass, hmac_key, idf_target): M = pub_numbers.n key_size = private_key.key_size if key_size not in supported_key_size[idf_target]: - print("ERROR: Private key size {0} not supported for the target {1},\nthe supported key sizes are {2}" + print('ERROR: Private key size {0} not supported for the target {1},\nthe supported key sizes are {2}' .format(key_size, idf_target, str(supported_key_size[idf_target]))) sys.exit(-1) @@ -122,12 +122,12 @@ def calculate_ds_parameters(privkey, priv_key_pass, hmac_key, idf_target): # get max supported key size for the respective target max_len = max(supported_key_size[idf_target]) - aes_key = hmac.HMAC(hmac_key, b"\xFF" * 32, hashlib.sha256).digest() + aes_key = hmac.HMAC(hmac_key, b'\xFF' * 32, hashlib.sha256).digest() md_in = number_as_bytes(Y, max_len) + \ number_as_bytes(M, max_len) + \ number_as_bytes(rinv, max_len) + \ - struct.pack(" Date: Tue, 26 Jan 2021 10:49:01 +0800 Subject: [PATCH 077/231] style: format python files with isort and double-quote-string-fixer --- .../mqtt/weekend_test/mqtt_publish_test.py | 95 ++++---- .../mqtt/ssl/mqtt_ssl_example_test.py | 77 +++--- .../mqtt/tcp/mqtt_tcp_example_test.py | 54 ++--- .../protocols/mqtt/ws/mqtt_ws_example_test.py | 64 ++--- .../mqtt/wss/mqtt_wss_example_test.py | 61 +++-- .../mqtt/publish_connect_test/app_test.py | 221 +++++++++--------- 6 files changed, 285 insertions(+), 287 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) diff --git a/examples/protocols/mqtt/ssl/mqtt_ssl_example_test.py b/examples/protocols/mqtt/ssl/mqtt_ssl_example_test.py index 2fddd57..5c77c7b 100644 --- a/examples/protocols/mqtt/ssl/mqtt_ssl_example_test.py +++ b/examples/protocols/mqtt/ssl/mqtt_ssl_example_test.py @@ -1,29 +1,28 @@ -from __future__ import print_function -from __future__ import unicode_literals -from builtins import str -import re +from __future__ import print_function, unicode_literals + import os -import sys +import re import ssl +import sys +from builtins import str +from threading import Event, Thread + import paho.mqtt.client as mqtt -from threading import Thread, Event - -from tiny_test_fw import DUT import ttfw_idf - +from tiny_test_fw import DUT event_client_connected = Event() event_stop_client = Event() event_client_received_correct = Event() event_client_received_binary = Event() -message_log = "" +message_log = '' # 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): @@ -36,33 +35,33 @@ def on_message(client, userdata, msg): global message_log global event_client_received_correct global event_client_received_binary - if msg.topic == "/topic/binary": + if msg.topic == '/topic/binary': binary = userdata size = os.path.getsize(binary) - print("Receiving binary from esp and comparing with {}, size {}...".format(binary, size)) - with open(binary, "rb") as f: + print('Receiving binary from esp and comparing with {}, size {}...'.format(binary, size)) + with open(binary, 'rb') as f: bin = f.read() if bin == msg.payload[:size]: - print("...matches!") + print('...matches!') event_client_received_binary.set() return else: - recv_binary = binary + ".received" - with open(recv_binary, "w") as fw: + recv_binary = binary + '.received' + with open(recv_binary, 'w') as fw: fw.write(msg.payload) raise ValueError('Received binary (saved as: {}) does not match the original file: {}'.format(recv_binary, binary)) payload = msg.payload.decode() - if not event_client_received_correct.is_set() and payload == "data": - client.subscribe("/topic/binary") - client.publish("/topic/qos0", "send binary please") - if msg.topic == "/topic/qos0" and payload == "data": + if not event_client_received_correct.is_set() and payload == 'data': + client.subscribe('/topic/binary') + client.publish('/topic/qos0', 'send binary please') + if msg.topic == '/topic/qos0' and payload == 'data': event_client_received_correct.set() - message_log += "Received data:" + msg.topic + " " + payload + "\n" + message_log += 'Received data:' + msg.topic + ' ' + payload + '\n' -@ttfw_idf.idf_example_test(env_tag="Example_WIFI") +@ttfw_idf.idf_example_test(env_tag='Example_WIFI') def test_examples_protocol_mqtt_ssl(env, extra_data): - broker_url = "" + broker_url = '' broker_port = 0 """ steps: | @@ -72,15 +71,15 @@ def test_examples_protocol_mqtt_ssl(env, extra_data): 4. Test ESP32 client received correct qos0 message 5. Test python client receives binary data from running partition and compares it with the binary """ - dut1 = env.get_dut("mqtt_ssl", "examples/protocols/mqtt/ssl", dut_class=ttfw_idf.ESP32DUT) + dut1 = env.get_dut('mqtt_ssl', 'examples/protocols/mqtt/ssl', dut_class=ttfw_idf.ESP32DUT) # check and log bin size - binary_file = os.path.join(dut1.app.binary_path, "mqtt_ssl.bin") + binary_file = os.path.join(dut1.app.binary_path, 'mqtt_ssl.bin') bin_size = os.path.getsize(binary_file) - ttfw_idf.log_performance("mqtt_ssl_bin_size", "{}KB" + ttfw_idf.log_performance('mqtt_ssl_bin_size', '{}KB' .format(bin_size // 1024)) # Look for host:port in sdkconfig try: - value = re.search(r'\:\/\/([^:]+)\:([0-9]+)', dut1.app.get_sdkconfig()["CONFIG_BROKER_URI"]) + value = re.search(r'\:\/\/([^:]+)\:([0-9]+)', dut1.app.get_sdkconfig()['CONFIG_BROKER_URI']) broker_url = value.group(1) broker_port = int(value.group(2)) except Exception: @@ -97,31 +96,31 @@ def test_examples_protocol_mqtt_ssl(env, extra_data): 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_url, broker_port, 60) except Exception: - print("ENV_TEST_FAILURE: Unexpected error while connecting to broker {}: {}:".format(broker_url, sys.exc_info()[0])) + print('ENV_TEST_FAILURE: Unexpected error while connecting to broker {}: {}:'.format(broker_url, sys.exc_info()[0])) raise # Starting a py-client in a separate thread thread1 = Thread(target=mqtt_client_task, args=(client,)) thread1.start() try: - print("Connecting py-client to broker {}:{}...".format(broker_url, broker_port)) + print('Connecting py-client to broker {}:{}...'.format(broker_url, broker_port)) if not event_client_connected.wait(timeout=30): - raise ValueError("ENV_TEST_FAILURE: Test script cannot connect to broker: {}".format(broker_url)) + raise ValueError('ENV_TEST_FAILURE: Test script cannot connect to broker: {}'.format(broker_url)) dut1.start_app() try: - ip_address = dut1.expect(re.compile(r" sta ip: ([^,]+),"), timeout=30) - print("Connected to AP with IP: {}".format(ip_address)) + 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 - print("Checking py-client received msg published from esp...") + print('Checking py-client received msg published from esp...') if not event_client_received_correct.wait(timeout=30): raise ValueError('Wrong data received, msg log: {}'.format(message_log)) - print("Checking esp-client received msg published from py-client...") - dut1.expect(re.compile(r"DATA=send binary please"), timeout=30) - print("Receiving binary data from running partition...") + print('Checking esp-client received msg published from py-client...') + dut1.expect(re.compile(r'DATA=send binary please'), timeout=30) + print('Receiving binary data from running partition...') if not event_client_received_binary.wait(timeout=30): raise ValueError('Binary not received within timeout') finally: diff --git a/examples/protocols/mqtt/tcp/mqtt_tcp_example_test.py b/examples/protocols/mqtt/tcp/mqtt_tcp_example_test.py index 6159481..7acb1dc 100644 --- a/examples/protocols/mqtt/tcp/mqtt_tcp_example_test.py +++ b/examples/protocols/mqtt/tcp/mqtt_tcp_example_test.py @@ -1,20 +1,20 @@ -import re import os -import sys +import re import socket -from threading import Thread import struct +import sys import time +from threading import Thread -from tiny_test_fw import DUT import ttfw_idf +from tiny_test_fw import DUT msgid = -1 def get_my_ip(): s1 = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) - s1.connect(("8.8.8.8", 80)) + s1.connect(('8.8.8.8', 80)) my_ip = s1.getsockname()[0] s1.close() return my_ip @@ -22,7 +22,7 @@ def get_my_ip(): def mqqt_server_sketch(my_ip, port): global msgid - print("Starting the server on {}".format(my_ip)) + print('Starting the server on {}'.format(my_ip)) s = None try: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) @@ -31,29 +31,29 @@ def mqqt_server_sketch(my_ip, port): s.listen(1) q,addr = s.accept() q.settimeout(30) - print("connection accepted") + print('connection accepted') except Exception: - print("Local server on {}:{} listening/accepting failure: {}" - "Possibly check permissions or firewall settings" - "to accept connections on this address".format(my_ip, port, sys.exc_info()[0])) + print('Local server on {}:{} listening/accepting failure: {}' + 'Possibly check permissions or firewall settings' + 'to accept connections on this address'.format(my_ip, port, sys.exc_info()[0])) raise data = q.recv(1024) # check if received initial empty message - print("received from client {}".format(data)) + print('received from client {}'.format(data)) data = bytearray([0x20, 0x02, 0x00, 0x00]) q.send(data) # try to receive qos1 data = q.recv(1024) - msgid = struct.unpack(">H", data[15:17])[0] - print("received from client {}, msgid: {}".format(data, msgid)) + msgid = struct.unpack('>H', data[15:17])[0] + print('received from client {}, msgid: {}'.format(data, msgid)) data = bytearray([0x40, 0x02, data[15], data[16]]) q.send(data) time.sleep(5) s.close() - print("server closed") + print('server closed') -@ttfw_idf.idf_example_test(env_tag="Example_WIFI") +@ttfw_idf.idf_example_test(env_tag='Example_WIFI') def test_examples_protocol_mqtt_qos1(env, extra_data): global msgid """ @@ -63,11 +63,11 @@ def test_examples_protocol_mqtt_qos1(env, extra_data): 3. Test evaluates that qos1 message is queued and removed from queued after ACK received 4. Test the broker received the same message id evaluated in step 3 """ - dut1 = env.get_dut("mqtt_tcp", "examples/protocols/mqtt/tcp", dut_class=ttfw_idf.ESP32DUT) + dut1 = env.get_dut('mqtt_tcp', 'examples/protocols/mqtt/tcp', dut_class=ttfw_idf.ESP32DUT) # check and log bin size - binary_file = os.path.join(dut1.app.binary_path, "mqtt_tcp.bin") + binary_file = os.path.join(dut1.app.binary_path, 'mqtt_tcp.bin') bin_size = os.path.getsize(binary_file) - ttfw_idf.log_performance("mqtt_tcp_bin_size", "{}KB".format(bin_size // 1024)) + ttfw_idf.log_performance('mqtt_tcp_bin_size', '{}KB'.format(bin_size // 1024)) # 1. start mqtt broker sketch host_ip = get_my_ip() thread1 = Thread(target=mqqt_server_sketch, args=(host_ip,1883)) @@ -76,23 +76,23 @@ def test_examples_protocol_mqtt_qos1(env, extra_data): dut1.start_app() # waiting for getting the IP address try: - ip_address = dut1.expect(re.compile(r" sta ip: ([^,]+),"), timeout=30) - print("Connected to AP with IP: {}".format(ip_address)) + ip_address = dut1.expect(re.compile(r' sta ip: ([^,]+),'), timeout=30) + print('Connected to AP with IP: {}'.format(ip_address)) except DUT.ExpectTimeout: raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP') - print("writing to device: {}".format("mqtt://" + host_ip + "\n")) - dut1.write("mqtt://" + host_ip + "\n") + print('writing to device: {}'.format('mqtt://' + host_ip + '\n')) + dut1.write('mqtt://' + host_ip + '\n') thread1.join() - print("Message id received from server: {}".format(msgid)) + print('Message id received from server: {}'.format(msgid)) # 3. check the message id was enqueued and then deleted - msgid_enqueued = dut1.expect(re.compile(r"OUTBOX: ENQUEUE msgid=([0-9]+)"), timeout=30) - msgid_deleted = dut1.expect(re.compile(r"OUTBOX: DELETED msgid=([0-9]+)"), timeout=30) + msgid_enqueued = dut1.expect(re.compile(r'OUTBOX: ENQUEUE msgid=([0-9]+)'), timeout=30) + msgid_deleted = dut1.expect(re.compile(r'OUTBOX: DELETED msgid=([0-9]+)'), timeout=30) # 4. check the msgid of received data are the same as that of enqueued and deleted from outbox if (msgid_enqueued[0] == str(msgid) and msgid_deleted[0] == str(msgid)): - print("PASS: Received correct msg id") + print('PASS: Received correct msg id') else: - print("Failure!") + print('Failure!') raise ValueError('Mismatch of msgid: received: {}, enqueued {}, deleted {}'.format(msgid, msgid_enqueued, msgid_deleted)) diff --git a/examples/protocols/mqtt/ws/mqtt_ws_example_test.py b/examples/protocols/mqtt/ws/mqtt_ws_example_test.py index fdf1e2c..69c5dc9 100644 --- a/examples/protocols/mqtt/ws/mqtt_ws_example_test.py +++ b/examples/protocols/mqtt/ws/mqtt_ws_example_test.py @@ -1,26 +1,26 @@ -from __future__ import print_function -from __future__ import unicode_literals -from builtins import str -import re -import os -import sys -import paho.mqtt.client as mqtt -from threading import Thread, Event +from __future__ import print_function, unicode_literals -from tiny_test_fw import DUT +import os +import re +import sys +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 = "" +message_log = '' # 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): @@ -32,16 +32,16 @@ def mqtt_client_task(client): def on_message(client, userdata, msg): global message_log payload = msg.payload.decode() - if not event_client_received_correct.is_set() and payload == "data": - client.publish("/topic/qos0", "data_to_esp32") - if msg.topic == "/topic/qos0" and payload == "data": + if not event_client_received_correct.is_set() and payload == 'data': + client.publish('/topic/qos0', 'data_to_esp32') + if msg.topic == '/topic/qos0' and payload == 'data': event_client_received_correct.set() - message_log += "Received data:" + msg.topic + " " + payload + "\n" + message_log += 'Received data:' + msg.topic + ' ' + payload + '\n' -@ttfw_idf.idf_example_test(env_tag="Example_WIFI") +@ttfw_idf.idf_example_test(env_tag='Example_WIFI') def test_examples_protocol_mqtt_ws(env, extra_data): - broker_url = "" + broker_url = '' broker_port = 0 """ steps: | @@ -50,14 +50,14 @@ def test_examples_protocol_mqtt_ws(env, extra_data): 3. Test evaluates it received correct qos0 message 4. Test ESP32 client received correct qos0 message """ - dut1 = env.get_dut("mqtt_websocket", "examples/protocols/mqtt/ws", dut_class=ttfw_idf.ESP32DUT) + dut1 = env.get_dut('mqtt_websocket', 'examples/protocols/mqtt/ws', dut_class=ttfw_idf.ESP32DUT) # check and log bin size - binary_file = os.path.join(dut1.app.binary_path, "mqtt_websocket.bin") + binary_file = os.path.join(dut1.app.binary_path, 'mqtt_websocket.bin') bin_size = os.path.getsize(binary_file) - ttfw_idf.log_performance("mqtt_websocket_bin_size", "{}KB".format(bin_size // 1024)) + ttfw_idf.log_performance('mqtt_websocket_bin_size', '{}KB'.format(bin_size // 1024)) # Look for host:port in sdkconfig try: - value = re.search(r'\:\/\/([^:]+)\:([0-9]+)', dut1.app.get_sdkconfig()["CONFIG_BROKER_URI"]) + value = re.search(r'\:\/\/([^:]+)\:([0-9]+)', dut1.app.get_sdkconfig()['CONFIG_BROKER_URI']) broker_url = value.group(1) broker_port = int(value.group(2)) except Exception: @@ -66,33 +66,33 @@ def test_examples_protocol_mqtt_ws(env, extra_data): client = None # 1. Test connects to a broker try: - client = mqtt.Client(transport="websockets") + client = mqtt.Client(transport='websockets') client.on_connect = on_connect client.on_message = on_message - print("Connecting...") + print('Connecting...') client.connect(broker_url, broker_port, 60) except Exception: - print("ENV_TEST_FAILURE: Unexpected error while connecting to broker {}: {}:".format(broker_url, sys.exc_info()[0])) + print('ENV_TEST_FAILURE: Unexpected error while connecting to broker {}: {}:'.format(broker_url, sys.exc_info()[0])) raise # Starting a py-client in a separate thread thread1 = Thread(target=mqtt_client_task, args=(client,)) thread1.start() try: - print("Connecting py-client to broker {}:{}...".format(broker_url, broker_port)) + print('Connecting py-client to broker {}:{}...'.format(broker_url, broker_port)) if not event_client_connected.wait(timeout=30): - raise ValueError("ENV_TEST_FAILURE: Test script cannot connect to broker: {}".format(broker_url)) + raise ValueError('ENV_TEST_FAILURE: Test script cannot connect to broker: {}'.format(broker_url)) dut1.start_app() try: - ip_address = dut1.expect(re.compile(r" sta ip: ([^,]+),"), timeout=30) - print("Connected to AP with IP: {}".format(ip_address)) + 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 - print("Checking py-client received msg published from esp...") + print('Checking py-client received msg published from esp...') if not event_client_received_correct.wait(timeout=30): raise ValueError('Wrong data received, msg log: {}'.format(message_log)) - print("Checking esp-client received msg published from py-client...") - dut1.expect(re.compile(r"DATA=data_to_esp32"), timeout=30) + print('Checking esp-client received msg published from py-client...') + dut1.expect(re.compile(r'DATA=data_to_esp32'), timeout=30) finally: event_stop_client.set() thread1.join() diff --git a/examples/protocols/mqtt/wss/mqtt_wss_example_test.py b/examples/protocols/mqtt/wss/mqtt_wss_example_test.py index 0bc1999..5e2fa8a 100644 --- a/examples/protocols/mqtt/wss/mqtt_wss_example_test.py +++ b/examples/protocols/mqtt/wss/mqtt_wss_example_test.py @@ -1,28 +1,27 @@ from __future__ import unicode_literals -from __future__ import unicode_literals -from builtins import str -import re + import os -import sys +import re import ssl +import sys +from builtins import str +from threading import Event, Thread + import paho.mqtt.client as mqtt -from threading import Thread, Event - -from tiny_test_fw import DUT 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 = '' # 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): @@ -34,16 +33,16 @@ def mqtt_client_task(client): def on_message(client, userdata, msg): global message_log payload = msg.payload.decode() - if not event_client_received_correct.is_set() and payload == "data": - client.publish("/topic/qos0", "data_to_esp32") - if msg.topic == "/topic/qos0" and payload == "data": + if not event_client_received_correct.is_set() and payload == 'data': + client.publish('/topic/qos0', 'data_to_esp32') + if msg.topic == '/topic/qos0' and payload == 'data': event_client_received_correct.set() - message_log += "Received data:" + msg.topic + " " + payload + "\n" + message_log += 'Received data:' + msg.topic + ' ' + payload + '\n' -@ttfw_idf.idf_example_test(env_tag="Example_WIFI") +@ttfw_idf.idf_example_test(env_tag='Example_WIFI') def test_examples_protocol_mqtt_wss(env, extra_data): - broker_url = "" + broker_url = '' broker_port = 0 """ steps: | @@ -52,14 +51,14 @@ def test_examples_protocol_mqtt_wss(env, extra_data): 3. Test evaluates it received correct qos0 message 4. Test ESP32 client received correct qos0 message """ - dut1 = env.get_dut("mqtt_websocket_secure", "examples/protocols/mqtt/wss", dut_class=ttfw_idf.ESP32DUT) + dut1 = env.get_dut('mqtt_websocket_secure', 'examples/protocols/mqtt/wss', dut_class=ttfw_idf.ESP32DUT) # check and log bin size - binary_file = os.path.join(dut1.app.binary_path, "mqtt_websocket_secure.bin") + binary_file = os.path.join(dut1.app.binary_path, 'mqtt_websocket_secure.bin') bin_size = os.path.getsize(binary_file) - ttfw_idf.log_performance("mqtt_websocket_secure_bin_size", "{}KB".format(bin_size // 1024)) + ttfw_idf.log_performance('mqtt_websocket_secure_bin_size', '{}KB'.format(bin_size // 1024)) # Look for host:port in sdkconfig try: - value = re.search(r'\:\/\/([^:]+)\:([0-9]+)', dut1.app.get_sdkconfig()["CONFIG_BROKER_URI"]) + value = re.search(r'\:\/\/([^:]+)\:([0-9]+)', dut1.app.get_sdkconfig()['CONFIG_BROKER_URI']) broker_url = value.group(1) broker_port = int(value.group(2)) except Exception: @@ -68,36 +67,36 @@ def test_examples_protocol_mqtt_wss(env, extra_data): client = None # 1. Test connects to a broker try: - client = mqtt.Client(transport="websockets") + client = mqtt.Client(transport='websockets') client.on_connect = on_connect client.on_message = on_message client.tls_set(None, None, None, cert_reqs=ssl.CERT_NONE, tls_version=ssl.PROTOCOL_TLSv1_2, ciphers=None) - print("Connecting...") + print('Connecting...') client.connect(broker_url, broker_port, 60) except Exception: - print("ENV_TEST_FAILURE: Unexpected error while connecting to broker {}: {}:".format(broker_url, sys.exc_info()[0])) + print('ENV_TEST_FAILURE: Unexpected error while connecting to broker {}: {}:'.format(broker_url, sys.exc_info()[0])) raise # Starting a py-client in a separate thread thread1 = Thread(target=mqtt_client_task, args=(client,)) thread1.start() try: - print("Connecting py-client to broker {}:{}...".format(broker_url, broker_port)) + print('Connecting py-client to broker {}:{}...'.format(broker_url, broker_port)) if not event_client_connected.wait(timeout=30): - raise ValueError("ENV_TEST_FAILURE: Test script cannot connect to broker: {}".format(broker_url)) + raise ValueError('ENV_TEST_FAILURE: Test script cannot connect to broker: {}'.format(broker_url)) dut1.start_app() try: - ip_address = dut1.expect(re.compile(r" sta ip: ([^,]+),"), timeout=30) - print("Connected to AP with IP: {}".format(ip_address)) + 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 - print("Checking py-client received msg published from esp...") + print('Checking py-client received msg published from esp...') if not event_client_received_correct.wait(timeout=30): raise ValueError('Wrong data received, msg log: {}'.format(message_log)) - print("Checking esp-client received msg published from py-client...") - dut1.expect(re.compile(r"DATA=data_to_esp32"), timeout=30) + print('Checking esp-client received msg published from py-client...') + dut1.expect(re.compile(r'DATA=data_to_esp32'), timeout=30) finally: event_stop_client.set() thread1.join() diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/app_test.py b/tools/test_apps/protocols/mqtt/publish_connect_test/app_test.py index 78cc962..2cb19f5 100644 --- a/tools/test_apps/protocols/mqtt/publish_connect_test/app_test.py +++ b/tools/test_apps/protocols/mqtt/publish_connect_test/app_test.py @@ -1,16 +1,17 @@ -from __future__ import print_function -from __future__ import unicode_literals -import re +from __future__ import print_function, unicode_literals + import os -import socket -import select -import subprocess -from threading import Thread, Event -import ttfw_idf -import ssl -import paho.mqtt.client as mqtt -import string import random +import re +import select +import socket +import ssl +import string +import subprocess +from threading import Event, Thread + +import paho.mqtt.client as mqtt +import ttfw_idf DEFAULT_MSG_SIZE = 16 @@ -21,12 +22,12 @@ def _path(f): def set_server_cert_cn(ip): arg_list = [ - ['openssl', 'req', '-out', _path('srv.csr'), '-key', _path('server.key'),'-subj', "/CN={}".format(ip), '-new'], + ['openssl', 'req', '-out', _path('srv.csr'), '-key', _path('server.key'),'-subj', '/CN={}'.format(ip), '-new'], ['openssl', 'x509', '-req', '-in', _path('srv.csr'), '-CA', _path('ca.crt'), '-CAkey', _path('ca.key'), '-CAcreateserial', '-out', _path('srv.crt'), '-days', '360']] for args in arg_list: if subprocess.check_call(args) != 0: - raise("openssl command {} failed".format(args)) + raise('openssl command {} failed'.format(args)) def get_my_ip(): @@ -54,9 +55,9 @@ class MqttPublisher: self.log_details = log_details self.repeat = repeat self.publish_cfg = publish_cfg - self.publish_cfg["qos"] = qos - self.publish_cfg["queue"] = queue - self.publish_cfg["transport"] = transport + self.publish_cfg['qos'] = qos + self.publish_cfg['queue'] = queue + self.publish_cfg['transport'] = transport # static variables used to pass options to and from static callbacks of paho-mqtt client MqttPublisher.event_client_connected = Event() MqttPublisher.event_client_got_all = Event() @@ -90,52 +91,52 @@ class MqttPublisher: def __enter__(self): - qos = self.publish_cfg["qos"] - queue = self.publish_cfg["queue"] - transport = self.publish_cfg["transport"] - broker_host = self.publish_cfg["broker_host_" + transport] - broker_port = self.publish_cfg["broker_port_" + transport] + qos = self.publish_cfg['qos'] + queue = self.publish_cfg['queue'] + transport = self.publish_cfg['transport'] + broker_host = self.publish_cfg['broker_host_' + transport] + broker_port = self.publish_cfg['broker_port_' + transport] # Start the test self.print_details("PUBLISH TEST: transport:{}, qos:{}, sequence:{}, enqueue:{}, sample msg:'{}'" .format(transport, qos, MqttPublisher.published, queue, MqttPublisher.expected_data)) try: - if transport in ["ws", "wss"]: - self.client = mqtt.Client(transport="websockets") + if transport in ['ws', 'wss']: + self.client = mqtt.Client(transport='websockets') else: self.client = mqtt.Client() self.client.on_connect = MqttPublisher.on_connect self.client.on_message = MqttPublisher.on_message self.client.user_data_set(0) - if transport in ["ssl", "wss"]: + if transport in ['ssl', 'wss']: self.client.tls_set(None, None, None, cert_reqs=ssl.CERT_NONE, tls_version=ssl.PROTOCOL_TLSv1_2, ciphers=None) self.client.tls_insecure_set(True) - self.print_details("Connecting...") + self.print_details('Connecting...') self.client.connect(broker_host, broker_port, 60) except Exception: - self.print_details("ENV_TEST_FAILURE: Unexpected error while connecting to broker {}".format(broker_host)) + self.print_details('ENV_TEST_FAILURE: Unexpected error while connecting to broker {}'.format(broker_host)) raise # Starting a py-client in a separate thread thread1 = Thread(target=self.mqtt_client_task, args=(self.client,)) thread1.start() - self.print_details("Connecting py-client to broker {}:{}...".format(broker_host, broker_port)) + self.print_details('Connecting py-client to broker {}:{}...'.format(broker_host, broker_port)) if not MqttPublisher.event_client_connected.wait(timeout=30): - raise ValueError("ENV_TEST_FAILURE: Test script cannot connect to broker: {}".format(broker_host)) - self.client.subscribe(self.publish_cfg["subscribe_topic"], qos) - self.dut.write(' '.join(str(x) for x in (transport, self.sample_string, self.repeat, MqttPublisher.published, qos, queue)), eol="\n") + raise ValueError('ENV_TEST_FAILURE: Test script cannot connect to broker: {}'.format(broker_host)) + self.client.subscribe(self.publish_cfg['subscribe_topic'], qos) + self.dut.write(' '.join(str(x) for x in (transport, self.sample_string, self.repeat, MqttPublisher.published, qos, queue)), eol='\n') try: # waiting till subscribed to defined topic - self.dut.expect(re.compile(r"MQTT_EVENT_SUBSCRIBED"), timeout=30) + self.dut.expect(re.compile(r'MQTT_EVENT_SUBSCRIBED'), timeout=30) for _ in range(MqttPublisher.published): - self.client.publish(self.publish_cfg["publish_topic"], self.sample_string * self.repeat, qos) - self.print_details("Publishing...") - self.print_details("Checking esp-client received msg published from py-client...") - self.dut.expect(re.compile(r"Correct pattern received exactly x times"), timeout=60) + self.client.publish(self.publish_cfg['publish_topic'], self.sample_string * self.repeat, qos) + self.print_details('Publishing...') + self.print_details('Checking esp-client received msg published from py-client...') + self.dut.expect(re.compile(r'Correct pattern received exactly x times'), timeout=60) if not MqttPublisher.event_client_got_all.wait(timeout=60): - raise ValueError("Not all data received from ESP32") - print(" - all data received from ESP32") + raise ValueError('Not all data received from ESP32') + print(' - all data received from ESP32') finally: self.event_stop_client.set() thread1.join() @@ -164,7 +165,7 @@ class TlsServer: try: self.socket.bind(('', self.port)) except socket.error as e: - print("Bind failed:{}".format(e)) + print('Bind failed:{}'.format(e)) raise self.socket.listen(1) @@ -190,23 +191,23 @@ class TlsServer: context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH) if self.client_cert: context.verify_mode = ssl.CERT_REQUIRED - context.load_verify_locations(cafile=_path("ca.crt")) - context.load_cert_chain(certfile=_path("srv.crt"), keyfile=_path("server.key")) + context.load_verify_locations(cafile=_path('ca.crt')) + context.load_cert_chain(certfile=_path('srv.crt'), keyfile=_path('server.key')) if self.use_alpn: - context.set_alpn_protocols(["mymqtt", "http/1.1"]) + context.set_alpn_protocols(['mymqtt', 'http/1.1']) self.socket = context.wrap_socket(self.socket, server_side=True) try: self.conn, address = self.socket.accept() # accept new connection self.socket.settimeout(10.0) - print(" - connection from: {}".format(address)) + print(' - connection from: {}'.format(address)) if self.use_alpn: self.negotiated_protocol = self.conn.selected_alpn_protocol() - print(" - negotiated_protocol: {}".format(self.negotiated_protocol)) + print(' - negotiated_protocol: {}'.format(self.negotiated_protocol)) self.handle_conn() except ssl.SSLError as e: self.conn = None self.ssl_error = str(e) - print(" - SSLError: {}".format(str(e))) + print(' - SSLError: {}'.format(str(e))) def handle_conn(self): while not self.shutdown.is_set(): @@ -216,7 +217,7 @@ class TlsServer: self.process_mqtt_connect() except socket.error as err: - print(" - error: {}".format(err)) + print(' - error: {}'.format(err)) raise def process_mqtt_connect(self): @@ -225,20 +226,20 @@ class TlsServer: message = ''.join(format(x, '02x') for x in data) if message[0:16] == '101800044d515454': if self.refuse_connection is False: - print(" - received mqtt connect, sending ACK") - self.conn.send(bytearray.fromhex("20020000")) + print(' - received mqtt connect, sending ACK') + self.conn.send(bytearray.fromhex('20020000')) else: # injecting connection not authorized error - print(" - received mqtt connect, sending NAK") - self.conn.send(bytearray.fromhex("20020005")) + print(' - received mqtt connect, sending NAK') + self.conn.send(bytearray.fromhex('20020005')) else: - raise Exception(" - error process_mqtt_connect unexpected connect received: {}".format(message)) + raise Exception(' - error process_mqtt_connect unexpected connect received: {}'.format(message)) finally: # stop the server after the connect message in happy flow, or if any exception occur self.shutdown.set() -@ttfw_idf.idf_custom_test(env_tag="Example_WIFI", group="test-apps") +@ttfw_idf.idf_custom_test(env_tag='Example_WIFI', group='test-apps') def test_app_protocol_mqtt_publish_connect(env, extra_data): """ steps: @@ -246,11 +247,11 @@ def test_app_protocol_mqtt_publish_connect(env, extra_data): 2. connect to uri specified in the config 3. send and receive data """ - dut1 = env.get_dut("mqtt_publish_connect_test", "tools/test_apps/protocols/mqtt/publish_connect_test", dut_class=ttfw_idf.ESP32DUT) + dut1 = env.get_dut('mqtt_publish_connect_test', 'tools/test_apps/protocols/mqtt/publish_connect_test', dut_class=ttfw_idf.ESP32DUT) # check and log bin size - binary_file = os.path.join(dut1.app.binary_path, "mqtt_publish_connect_test.bin") + binary_file = os.path.join(dut1.app.binary_path, 'mqtt_publish_connect_test.bin') bin_size = os.path.getsize(binary_file) - ttfw_idf.log_performance("mqtt_publish_connect_test_bin_size", "{}KB".format(bin_size // 1024)) + ttfw_idf.log_performance('mqtt_publish_connect_test_bin_size', '{}KB'.format(bin_size // 1024)) # Look for test case symbolic names and publish configs cases = {} @@ -263,30 +264,30 @@ def test_app_protocol_mqtt_publish_connect(env, extra_data): return value.group(1), int(value.group(2)) # Get connection test cases configuration: symbolic names for test cases - for i in ["CONFIG_EXAMPLE_CONNECT_CASE_NO_CERT", - "CONFIG_EXAMPLE_CONNECT_CASE_SERVER_CERT", - "CONFIG_EXAMPLE_CONNECT_CASE_MUTUAL_AUTH", - "CONFIG_EXAMPLE_CONNECT_CASE_INVALID_SERVER_CERT", - "CONFIG_EXAMPLE_CONNECT_CASE_SERVER_DER_CERT", - "CONFIG_EXAMPLE_CONNECT_CASE_MUTUAL_AUTH_KEY_PWD", - "CONFIG_EXAMPLE_CONNECT_CASE_MUTUAL_AUTH_BAD_CRT", - "CONFIG_EXAMPLE_CONNECT_CASE_NO_CERT_ALPN"]: + for i in ['CONFIG_EXAMPLE_CONNECT_CASE_NO_CERT', + 'CONFIG_EXAMPLE_CONNECT_CASE_SERVER_CERT', + 'CONFIG_EXAMPLE_CONNECT_CASE_MUTUAL_AUTH', + 'CONFIG_EXAMPLE_CONNECT_CASE_INVALID_SERVER_CERT', + 'CONFIG_EXAMPLE_CONNECT_CASE_SERVER_DER_CERT', + 'CONFIG_EXAMPLE_CONNECT_CASE_MUTUAL_AUTH_KEY_PWD', + 'CONFIG_EXAMPLE_CONNECT_CASE_MUTUAL_AUTH_BAD_CRT', + 'CONFIG_EXAMPLE_CONNECT_CASE_NO_CERT_ALPN']: cases[i] = dut1.app.get_sdkconfig()[i] # Get publish test configuration - publish_cfg["publish_topic"] = dut1.app.get_sdkconfig()["CONFIG_EXAMPLE_SUBSCIBE_TOPIC"].replace('"','') - publish_cfg["subscribe_topic"] = dut1.app.get_sdkconfig()["CONFIG_EXAMPLE_PUBLISH_TOPIC"].replace('"','') - publish_cfg["broker_host_ssl"], publish_cfg["broker_port_ssl"] = get_host_port_from_dut(dut1, "CONFIG_EXAMPLE_BROKER_SSL_URI") - publish_cfg["broker_host_tcp"], publish_cfg["broker_port_tcp"] = get_host_port_from_dut(dut1, "CONFIG_EXAMPLE_BROKER_TCP_URI") - publish_cfg["broker_host_ws"], publish_cfg["broker_port_ws"] = get_host_port_from_dut(dut1, "CONFIG_EXAMPLE_BROKER_WS_URI") - publish_cfg["broker_host_wss"], publish_cfg["broker_port_wss"] = get_host_port_from_dut(dut1, "CONFIG_EXAMPLE_BROKER_WSS_URI") + publish_cfg['publish_topic'] = dut1.app.get_sdkconfig()['CONFIG_EXAMPLE_SUBSCIBE_TOPIC'].replace('"','') + publish_cfg['subscribe_topic'] = dut1.app.get_sdkconfig()['CONFIG_EXAMPLE_PUBLISH_TOPIC'].replace('"','') + publish_cfg['broker_host_ssl'], publish_cfg['broker_port_ssl'] = get_host_port_from_dut(dut1, 'CONFIG_EXAMPLE_BROKER_SSL_URI') + publish_cfg['broker_host_tcp'], publish_cfg['broker_port_tcp'] = get_host_port_from_dut(dut1, 'CONFIG_EXAMPLE_BROKER_TCP_URI') + publish_cfg['broker_host_ws'], publish_cfg['broker_port_ws'] = get_host_port_from_dut(dut1, 'CONFIG_EXAMPLE_BROKER_WS_URI') + publish_cfg['broker_host_wss'], publish_cfg['broker_port_wss'] = get_host_port_from_dut(dut1, 'CONFIG_EXAMPLE_BROKER_WSS_URI') except Exception: print('ENV_TEST_FAILURE: Some mandatory test case not found in sdkconfig') raise dut1.start_app() - esp_ip = dut1.expect(re.compile(r" IPv4 address: ([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)"), timeout=30) - print("Got IP={}".format(esp_ip[0])) + esp_ip = dut1.expect(re.compile(r' IPv4 address: ([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)'), timeout=30) + print('Got IP={}'.format(esp_ip[0])) # # start connection test @@ -295,73 +296,73 @@ def test_app_protocol_mqtt_publish_connect(env, extra_data): server_port = 2222 def start_connection_case(case, desc): - print("Starting {}: {}".format(case, desc)) + print('Starting {}: {}'.format(case, desc)) case_id = cases[case] - dut1.write("conn {} {} {}".format(ip, server_port, case_id)) - dut1.expect("Test case:{} started".format(case_id)) + dut1.write('conn {} {} {}'.format(ip, server_port, case_id)) + dut1.expect('Test case:{} started'.format(case_id)) return case_id - for case in ["CONFIG_EXAMPLE_CONNECT_CASE_NO_CERT", "CONFIG_EXAMPLE_CONNECT_CASE_SERVER_CERT", "CONFIG_EXAMPLE_CONNECT_CASE_SERVER_DER_CERT"]: + for case in ['CONFIG_EXAMPLE_CONNECT_CASE_NO_CERT', 'CONFIG_EXAMPLE_CONNECT_CASE_SERVER_CERT', 'CONFIG_EXAMPLE_CONNECT_CASE_SERVER_DER_CERT']: # All these cases connect to the server with no server verification or with server only verification with TlsServer(server_port): - test_nr = start_connection_case(case, "default server - expect to connect normally") - dut1.expect("MQTT_EVENT_CONNECTED: Test={}".format(test_nr), timeout=30) + test_nr = start_connection_case(case, 'default server - expect to connect normally') + dut1.expect('MQTT_EVENT_CONNECTED: Test={}'.format(test_nr), timeout=30) with TlsServer(server_port, refuse_connection=True): - test_nr = start_connection_case(case, "ssl shall connect, but mqtt sends connect refusal") - dut1.expect("MQTT_EVENT_ERROR: Test={}".format(test_nr), timeout=30) - dut1.expect("MQTT ERROR: 0x5") # expecting 0x5 ... connection not authorized error + test_nr = start_connection_case(case, 'ssl shall connect, but mqtt sends connect refusal') + dut1.expect('MQTT_EVENT_ERROR: Test={}'.format(test_nr), timeout=30) + dut1.expect('MQTT ERROR: 0x5') # expecting 0x5 ... connection not authorized error with TlsServer(server_port, client_cert=True) as s: - test_nr = start_connection_case(case, "server with client verification - handshake error since client presents no client certificate") - dut1.expect("MQTT_EVENT_ERROR: Test={}".format(test_nr), timeout=30) - dut1.expect("ESP-TLS ERROR: 0x8010") # expect ... handshake error (PEER_DID_NOT_RETURN_A_CERTIFICATE) - if "PEER_DID_NOT_RETURN_A_CERTIFICATE" not in s.get_last_ssl_error(): - raise("Unexpected ssl error from the server {}".format(s.get_last_ssl_error())) + test_nr = start_connection_case(case, 'server with client verification - handshake error since client presents no client certificate') + dut1.expect('MQTT_EVENT_ERROR: Test={}'.format(test_nr), timeout=30) + dut1.expect('ESP-TLS ERROR: 0x8010') # expect ... handshake error (PEER_DID_NOT_RETURN_A_CERTIFICATE) + if 'PEER_DID_NOT_RETURN_A_CERTIFICATE' not in s.get_last_ssl_error(): + raise('Unexpected ssl error from the server {}'.format(s.get_last_ssl_error())) - for case in ["CONFIG_EXAMPLE_CONNECT_CASE_MUTUAL_AUTH", "CONFIG_EXAMPLE_CONNECT_CASE_MUTUAL_AUTH_KEY_PWD"]: + for case in ['CONFIG_EXAMPLE_CONNECT_CASE_MUTUAL_AUTH', 'CONFIG_EXAMPLE_CONNECT_CASE_MUTUAL_AUTH_KEY_PWD']: # These cases connect to server with both server and client verification (client key might be password protected) with TlsServer(server_port, client_cert=True): - test_nr = start_connection_case(case, "server with client verification - expect to connect normally") - dut1.expect("MQTT_EVENT_CONNECTED: Test={}".format(test_nr), timeout=30) + test_nr = start_connection_case(case, 'server with client verification - expect to connect normally') + dut1.expect('MQTT_EVENT_CONNECTED: Test={}'.format(test_nr), timeout=30) - case = "CONFIG_EXAMPLE_CONNECT_CASE_INVALID_SERVER_CERT" + case = 'CONFIG_EXAMPLE_CONNECT_CASE_INVALID_SERVER_CERT' with TlsServer(server_port) as s: - test_nr = start_connection_case(case, "invalid server certificate on default server - expect ssl handshake error") - dut1.expect("MQTT_EVENT_ERROR: Test={}".format(test_nr), timeout=30) - dut1.expect("ESP-TLS ERROR: 0x8010") # expect ... handshake error (TLSV1_ALERT_UNKNOWN_CA) - if "alert unknown ca" not in s.get_last_ssl_error(): - raise Exception("Unexpected ssl error from the server {}".format(s.get_last_ssl_error())) + test_nr = start_connection_case(case, 'invalid server certificate on default server - expect ssl handshake error') + dut1.expect('MQTT_EVENT_ERROR: Test={}'.format(test_nr), timeout=30) + dut1.expect('ESP-TLS ERROR: 0x8010') # expect ... handshake error (TLSV1_ALERT_UNKNOWN_CA) + if 'alert unknown ca' not in s.get_last_ssl_error(): + raise Exception('Unexpected ssl error from the server {}'.format(s.get_last_ssl_error())) - case = "CONFIG_EXAMPLE_CONNECT_CASE_MUTUAL_AUTH_BAD_CRT" + case = 'CONFIG_EXAMPLE_CONNECT_CASE_MUTUAL_AUTH_BAD_CRT' with TlsServer(server_port, client_cert=True) as s: - test_nr = start_connection_case(case, "Invalid client certificate on server with client verification - expect ssl handshake error") - dut1.expect("MQTT_EVENT_ERROR: Test={}".format(test_nr), timeout=30) - dut1.expect("ESP-TLS ERROR: 0x8010") # expect ... handshake error (CERTIFICATE_VERIFY_FAILED) - if "CERTIFICATE_VERIFY_FAILED" not in s.get_last_ssl_error(): - raise Exception("Unexpected ssl error from the server {}".format(s.get_last_ssl_error())) + test_nr = start_connection_case(case, 'Invalid client certificate on server with client verification - expect ssl handshake error') + dut1.expect('MQTT_EVENT_ERROR: Test={}'.format(test_nr), timeout=30) + dut1.expect('ESP-TLS ERROR: 0x8010') # expect ... handshake error (CERTIFICATE_VERIFY_FAILED) + if 'CERTIFICATE_VERIFY_FAILED' not in s.get_last_ssl_error(): + raise Exception('Unexpected ssl error from the server {}'.format(s.get_last_ssl_error())) - for case in ["CONFIG_EXAMPLE_CONNECT_CASE_NO_CERT", "CONFIG_EXAMPLE_CONNECT_CASE_NO_CERT_ALPN"]: + for case in ['CONFIG_EXAMPLE_CONNECT_CASE_NO_CERT', 'CONFIG_EXAMPLE_CONNECT_CASE_NO_CERT_ALPN']: with TlsServer(server_port, use_alpn=True) as s: - test_nr = start_connection_case(case, "server with alpn - expect connect, check resolved protocol") - dut1.expect("MQTT_EVENT_CONNECTED: Test={}".format(test_nr), timeout=30) - if case == "CONFIG_EXAMPLE_CONNECT_CASE_NO_CERT" and s.get_negotiated_protocol() is None: - print(" - client with alpn off, no negotiated protocol: OK") - elif case == "CONFIG_EXAMPLE_CONNECT_CASE_NO_CERT_ALPN" and s.get_negotiated_protocol() == "mymqtt": - print(" - client with alpn on, negotiated protocol resolved: OK") + test_nr = start_connection_case(case, 'server with alpn - expect connect, check resolved protocol') + dut1.expect('MQTT_EVENT_CONNECTED: Test={}'.format(test_nr), timeout=30) + if case == 'CONFIG_EXAMPLE_CONNECT_CASE_NO_CERT' and s.get_negotiated_protocol() is None: + print(' - client with alpn off, no negotiated protocol: OK') + elif case == 'CONFIG_EXAMPLE_CONNECT_CASE_NO_CERT_ALPN' and s.get_negotiated_protocol() == 'mymqtt': + print(' - client with alpn on, negotiated protocol resolved: OK') else: - raise Exception("Unexpected negotiated protocol {}".format(s.get_negotiated_protocol())) + raise Exception('Unexpected negotiated protocol {}'.format(s.get_negotiated_protocol())) # # start publish tests def start_publish_case(transport, qos, repeat, published, queue): - print("Starting Publish test: transport:{}, qos:{}, nr_of_msgs:{}, msg_size:{}, enqueue:{}" + print('Starting Publish test: transport:{}, qos:{}, nr_of_msgs:{}, msg_size:{}, enqueue:{}' .format(transport, qos, published, repeat * DEFAULT_MSG_SIZE, queue)) with MqttPublisher(dut1, transport, qos, repeat, published, queue, publish_cfg): pass 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 publish_cfg["broker_host_" + transport] is None: + if publish_cfg['broker_host_' + transport] is None: print('Skipping transport: {}...'.format(transport)) continue start_publish_case(transport, qos, 0, 5, q) From edde84b37768743d6f547fe8b81966ee5560458a Mon Sep 17 00:00:00 2001 From: David Cermak Date: Tue, 26 Jan 2021 11:52:36 +0100 Subject: [PATCH 078/231] ci: Make the mqtt example test to send only portion of the partition This makes the test faster and more robust in very a busy WiFi environment (our CI) and still exercises the scenario of fragmenting the message on both mqtt and ssl levels (binary size to send > mqtt buffer size) --- .../protocols/mqtt/ssl/main/Kconfig.projbuild | 6 +++++ examples/protocols/mqtt/ssl/main/app_main.c | 7 ++++-- .../mqtt/ssl/mqtt_ssl_example_test.py | 22 +++++++++---------- 3 files changed, 22 insertions(+), 13 deletions(-) diff --git a/examples/protocols/mqtt/ssl/main/Kconfig.projbuild b/examples/protocols/mqtt/ssl/main/Kconfig.projbuild index 3c44294..b1acbc5 100644 --- a/examples/protocols/mqtt/ssl/main/Kconfig.projbuild +++ b/examples/protocols/mqtt/ssl/main/Kconfig.projbuild @@ -17,4 +17,10 @@ menu "Example Configuration" bool default y if BROKER_CERTIFICATE_OVERRIDE != "" + config BROKER_BIN_SIZE_TO_SEND + # This option is not visible and is used only to set parameters for example tests + # Here we configure the data size to send and to be expected in the python script + int + default 20000 + endmenu diff --git a/examples/protocols/mqtt/ssl/main/app_main.c b/examples/protocols/mqtt/ssl/main/app_main.c index 1c15a29..85a4ee7 100644 --- a/examples/protocols/mqtt/ssl/main/app_main.c +++ b/examples/protocols/mqtt/ssl/main/app_main.c @@ -21,6 +21,7 @@ #include "mqtt_client.h" #include "esp_tls.h" #include "esp_ota_ops.h" +#include static const char *TAG = "MQTTS_EXAMPLE"; @@ -33,7 +34,7 @@ extern const uint8_t mqtt_eclipse_org_pem_start[] asm("_binary_mqtt_eclipse_or extern const uint8_t mqtt_eclipse_org_pem_end[] asm("_binary_mqtt_eclipse_org_pem_end"); // -// Note: this function is for testing purposes only publishing the entire active partition +// Note: this function is for testing purposes only publishing part of the active partition // (to be checked against the original binary) // static void send_binary(esp_mqtt_client_handle_t client) @@ -42,7 +43,9 @@ static void send_binary(esp_mqtt_client_handle_t client) 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); - int msg_id = esp_mqtt_client_publish(client, "/topic/binary", binary_address, partition->size, 0, 0); + // sending only the configured portion of the partition (if it's less than the partition size) + int binary_size = MIN(CONFIG_BROKER_BIN_SIZE_TO_SEND,partition->size); + int msg_id = esp_mqtt_client_publish(client, "/topic/binary", binary_address, binary_size, 0, 0); ESP_LOGI(TAG, "binary sent with msg_id=%d", msg_id); } diff --git a/examples/protocols/mqtt/ssl/mqtt_ssl_example_test.py b/examples/protocols/mqtt/ssl/mqtt_ssl_example_test.py index 5c77c7b..34bd2c7 100644 --- a/examples/protocols/mqtt/ssl/mqtt_ssl_example_test.py +++ b/examples/protocols/mqtt/ssl/mqtt_ssl_example_test.py @@ -36,20 +36,19 @@ def on_message(client, userdata, msg): global event_client_received_correct global event_client_received_binary if msg.topic == '/topic/binary': - binary = userdata - size = os.path.getsize(binary) - print('Receiving binary from esp and comparing with {}, size {}...'.format(binary, size)) + binary, bin_size = userdata + print('Receiving binary from esp and comparing with {}, size {}...'.format(binary, bin_size)) with open(binary, 'rb') as f: bin = f.read() - if bin == msg.payload[:size]: + if bin[:bin_size] == msg.payload[:bin_size]: print('...matches!') event_client_received_binary.set() return - else: - recv_binary = binary + '.received' - with open(recv_binary, 'w') as fw: - fw.write(msg.payload) - raise ValueError('Received binary (saved as: {}) does not match the original file: {}'.format(recv_binary, binary)) + recv_binary = binary + '.received' + with open(recv_binary, 'w') as fw: + fw.write(msg.payload) + raise ValueError('Received binary (saved as: {}) does not match the original file: {}'.format(recv_binary, binary)) + payload = msg.payload.decode() if not event_client_received_correct.is_set() and payload == 'data': client.subscribe('/topic/binary') @@ -64,7 +63,7 @@ def test_examples_protocol_mqtt_ssl(env, extra_data): broker_url = '' broker_port = 0 """ - steps: | + 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 @@ -82,6 +81,7 @@ def test_examples_protocol_mqtt_ssl(env, extra_data): value = re.search(r'\:\/\/([^:]+)\:([0-9]+)', dut1.app.get_sdkconfig()['CONFIG_BROKER_URI']) broker_url = value.group(1) broker_port = int(value.group(2)) + bin_size = min(int(dut1.app.get_sdkconfig()['CONFIG_BROKER_BIN_SIZE_TO_SEND']), bin_size) except Exception: print('ENV_TEST_FAILURE: Cannot find broker url in sdkconfig') raise @@ -91,7 +91,7 @@ def test_examples_protocol_mqtt_ssl(env, extra_data): client = mqtt.Client() client.on_connect = on_connect client.on_message = on_message - client.user_data_set(binary_file) + client.user_data_set((binary_file, bin_size)) client.tls_set(None, None, None, cert_reqs=ssl.CERT_NONE, tls_version=ssl.PROTOCOL_TLSv1_2, ciphers=None) From 3148f4c5dd585ef3f69f352a3156ed1b55814708 Mon Sep 17 00:00:00 2001 From: Marius Vikhammer Date: Tue, 15 Dec 2020 11:00:02 +0800 Subject: [PATCH 079/231] CI: enable example builds for C3 Enables building C3 examples in CI. Fixes related warnings/errors and disables examples that cannot run. --- tools/test_apps/protocols/mqtt/build_test/README.md | 5 +---- .../test_apps/protocols/mqtt/publish_connect_test/README.md | 5 +---- 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/tools/test_apps/protocols/mqtt/build_test/README.md b/tools/test_apps/protocols/mqtt/build_test/README.md index d4a8422..70fb332 100644 --- a/tools/test_apps/protocols/mqtt/build_test/README.md +++ b/tools/test_apps/protocols/mqtt/build_test/README.md @@ -1,6 +1,3 @@ -| Supported Targets | ESP32 | ESP32-S2 | -| ----------------- | ----- | -------- | - # Build only test for C++ -This test app ensures that calling all mqtt-client API could be called from C++ +This test app ensures that calling all mqtt-client API could be called from C++ diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/README.md b/tools/test_apps/protocols/mqtt/publish_connect_test/README.md index b52d36e..2d926b2 100644 --- a/tools/test_apps/protocols/mqtt/publish_connect_test/README.md +++ b/tools/test_apps/protocols/mqtt/publish_connect_test/README.md @@ -1,9 +1,6 @@ -| Supported Targets | ESP32 | ESP32-S2 | -| ----------------- | ----- | -------- | - # ESP-MQTT advanced publish and connect test project -Main purpose of this application is to test the MQTT library to correctly publish and receive messages (of different size and sequences) over different transports. +Main purpose of this application is to test the MQTT library to correctly publish and receive messages (of different size and sequences) over different transports. It is possible to run this example manually without any test to exercise how the MQTT library deals with - reception of fragmented messages From 4471cd2158f124f0cf94301a6cb66d1566141c8f Mon Sep 17 00:00:00 2001 From: Euripedes Rocha Filho Date: Fri, 22 Jan 2021 15:19:20 +0000 Subject: [PATCH 080/231] examples/mqtt: Cleanup on examples - Removes extra callback function, the code from the callback is integrated into the event handler code. - Clarify usage of handler args, pass NULL as callback data instead of client, to avoid user confusion, and add documentation on usage of handler_args. --- examples/protocols/mqtt/ssl/main/app_main.c | 124 +++++++++--------- .../mqtt/ssl_mutual_auth/main/app_main.c | 112 +++++++++------- examples/protocols/mqtt/tcp/main/app_main.c | 112 ++++++++-------- examples/protocols/mqtt/ws/main/app_main.c | 115 +++++++++------- examples/protocols/mqtt/wss/main/app_main.c | 75 ++++++----- 5 files changed, 297 insertions(+), 241 deletions(-) diff --git a/examples/protocols/mqtt/ssl/main/app_main.c b/examples/protocols/mqtt/ssl/main/app_main.c index 85a4ee7..0f4d26a 100644 --- a/examples/protocols/mqtt/ssl/main/app_main.c +++ b/examples/protocols/mqtt/ssl/main/app_main.c @@ -41,7 +41,7 @@ static void send_binary(esp_mqtt_client_handle_t client) { spi_flash_mmap_handle_t out_handle; const void *binary_address; - const esp_partition_t* partition = esp_ota_get_running_partition(); + 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); // sending only the configured portion of the partition (if it's less than the partition size) int binary_size = MIN(CONFIG_BROKER_BIN_SIZE_TO_SEND,partition->size); @@ -49,70 +49,75 @@ static void send_binary(esp_mqtt_client_handle_t client) ESP_LOGI(TAG, "binary sent with msg_id=%d", msg_id); } -static esp_err_t mqtt_event_handler_cb(esp_mqtt_event_handle_t event) +/* + * @brief Event handler registered to receive MQTT events + * + * This function is called by the MQTT client event loop. + * + * @param handler_args user data registered to the event. + * @param base Event base for the handler(always MQTT Base in this example). + * @param event_id The id for the received event. + * @param event_data The data for the event, esp_mqtt_event_handle_t. + */ +static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data) { + ESP_LOGD(TAG, "Event dispatched from event loop base=%s, event_id=%d", base, event_id); + esp_mqtt_event_handle_t event = event_data; esp_mqtt_client_handle_t client = event->client; int msg_id; - // your_context_t *context = event->context; - switch (event->event_id) { - case MQTT_EVENT_CONNECTED: - ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED"); - msg_id = esp_mqtt_client_subscribe(client, "/topic/qos0", 0); - ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id); + switch ((esp_mqtt_event_id_t)event_id) { + case MQTT_EVENT_CONNECTED: + ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED"); + msg_id = esp_mqtt_client_subscribe(client, "/topic/qos0", 0); + ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id); - msg_id = esp_mqtt_client_subscribe(client, "/topic/qos1", 1); - ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id); + msg_id = esp_mqtt_client_subscribe(client, "/topic/qos1", 1); + ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id); - msg_id = esp_mqtt_client_unsubscribe(client, "/topic/qos1"); - ESP_LOGI(TAG, "sent unsubscribe successful, msg_id=%d", msg_id); - break; - case MQTT_EVENT_DISCONNECTED: - ESP_LOGI(TAG, "MQTT_EVENT_DISCONNECTED"); - break; + msg_id = esp_mqtt_client_unsubscribe(client, "/topic/qos1"); + ESP_LOGI(TAG, "sent unsubscribe successful, msg_id=%d", msg_id); + break; + case MQTT_EVENT_DISCONNECTED: + ESP_LOGI(TAG, "MQTT_EVENT_DISCONNECTED"); + break; - case MQTT_EVENT_SUBSCRIBED: - ESP_LOGI(TAG, "MQTT_EVENT_SUBSCRIBED, msg_id=%d", event->msg_id); - msg_id = esp_mqtt_client_publish(client, "/topic/qos0", "data", 0, 0, 0); - ESP_LOGI(TAG, "sent publish successful, msg_id=%d", msg_id); - break; - case MQTT_EVENT_UNSUBSCRIBED: - ESP_LOGI(TAG, "MQTT_EVENT_UNSUBSCRIBED, msg_id=%d", event->msg_id); - break; - case MQTT_EVENT_PUBLISHED: - ESP_LOGI(TAG, "MQTT_EVENT_PUBLISHED, msg_id=%d", event->msg_id); - break; - case MQTT_EVENT_DATA: - ESP_LOGI(TAG, "MQTT_EVENT_DATA"); - printf("TOPIC=%.*s\r\n", event->topic_len, event->topic); - printf("DATA=%.*s\r\n", event->data_len, event->data); - if (strncmp(event->data, "send binary please", event->data_len) == 0) { - ESP_LOGI(TAG, "Sending the binary"); - send_binary(client); - } - break; - case MQTT_EVENT_ERROR: - ESP_LOGI(TAG, "MQTT_EVENT_ERROR"); - if (event->error_handle->error_type == MQTT_ERROR_TYPE_TCP_TRANSPORT) { - ESP_LOGI(TAG, "Last error code reported from esp-tls: 0x%x", event->error_handle->esp_tls_last_esp_err); - ESP_LOGI(TAG, "Last tls stack error number: 0x%x", event->error_handle->esp_tls_stack_err); - ESP_LOGI(TAG, "Last captured errno : %d (%s)", event->error_handle->esp_transport_sock_errno, - strerror(event->error_handle->esp_transport_sock_errno)); - } else if (event->error_handle->error_type == MQTT_ERROR_TYPE_CONNECTION_REFUSED) { - ESP_LOGI(TAG, "Connection refused error: 0x%x", event->error_handle->connect_return_code); - } else { - ESP_LOGW(TAG, "Unknown error type: 0x%x", event->error_handle->error_type); - } - break; - default: - ESP_LOGI(TAG, "Other event id:%d", event->event_id); - break; + case MQTT_EVENT_SUBSCRIBED: + ESP_LOGI(TAG, "MQTT_EVENT_SUBSCRIBED, msg_id=%d", event->msg_id); + msg_id = esp_mqtt_client_publish(client, "/topic/qos0", "data", 0, 0, 0); + ESP_LOGI(TAG, "sent publish successful, msg_id=%d", msg_id); + break; + case MQTT_EVENT_UNSUBSCRIBED: + ESP_LOGI(TAG, "MQTT_EVENT_UNSUBSCRIBED, msg_id=%d", event->msg_id); + break; + case MQTT_EVENT_PUBLISHED: + ESP_LOGI(TAG, "MQTT_EVENT_PUBLISHED, msg_id=%d", event->msg_id); + break; + case MQTT_EVENT_DATA: + ESP_LOGI(TAG, "MQTT_EVENT_DATA"); + printf("TOPIC=%.*s\r\n", event->topic_len, event->topic); + printf("DATA=%.*s\r\n", event->data_len, event->data); + if (strncmp(event->data, "send binary please", event->data_len) == 0) { + ESP_LOGI(TAG, "Sending the binary"); + send_binary(client); + } + break; + case MQTT_EVENT_ERROR: + ESP_LOGI(TAG, "MQTT_EVENT_ERROR"); + if (event->error_handle->error_type == MQTT_ERROR_TYPE_TCP_TRANSPORT) { + ESP_LOGI(TAG, "Last error code reported from esp-tls: 0x%x", event->error_handle->esp_tls_last_esp_err); + ESP_LOGI(TAG, "Last tls stack error number: 0x%x", event->error_handle->esp_tls_stack_err); + ESP_LOGI(TAG, "Last captured errno : %d (%s)", event->error_handle->esp_transport_sock_errno, + strerror(event->error_handle->esp_transport_sock_errno)); + } else if (event->error_handle->error_type == MQTT_ERROR_TYPE_CONNECTION_REFUSED) { + ESP_LOGI(TAG, "Connection refused error: 0x%x", event->error_handle->connect_return_code); + } else { + ESP_LOGW(TAG, "Unknown error type: 0x%x", event->error_handle->error_type); + } + break; + default: + ESP_LOGI(TAG, "Other event id:%d", event->event_id); + break; } - return ESP_OK; -} - -static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data) { - ESP_LOGD(TAG, "Event dispatched from event loop base=%s, event_id=%d", base, event_id); - mqtt_event_handler_cb(event_data); } static void mqtt_app_start(void) @@ -124,7 +129,8 @@ static void mqtt_app_start(void) ESP_LOGI(TAG, "[APP] Free memory: %d bytes", esp_get_free_heap_size()); esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg); - esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, client); + /* The last argument may be used to pass data to the event handler, in this example mqtt_event_handler */ + esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, NULL); esp_mqtt_client_start(client); } diff --git a/examples/protocols/mqtt/ssl_mutual_auth/main/app_main.c b/examples/protocols/mqtt/ssl_mutual_auth/main/app_main.c index f5eb769..a5add84 100644 --- a/examples/protocols/mqtt/ssl_mutual_auth/main/app_main.c +++ b/examples/protocols/mqtt/ssl_mutual_auth/main/app_main.c @@ -38,56 +38,75 @@ extern const uint8_t client_key_pem_end[] asm("_binary_client_key_end"); extern const uint8_t server_cert_pem_start[] asm("_binary_mosquitto_org_crt_start"); extern const uint8_t server_cert_pem_end[] asm("_binary_mosquitto_org_crt_end"); -static esp_err_t mqtt_event_handler_cb(esp_mqtt_event_handle_t event) +static void log_error_if_nonzero(const char *message, int error_code) { - esp_mqtt_client_handle_t client = event->client; - int msg_id; - // your_context_t *context = event->context; - switch (event->event_id) { - case MQTT_EVENT_CONNECTED: - ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED"); - msg_id = esp_mqtt_client_subscribe(client, "/topic/qos0", 0); - ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id); - - msg_id = esp_mqtt_client_subscribe(client, "/topic/qos1", 1); - ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id); - - msg_id = esp_mqtt_client_unsubscribe(client, "/topic/qos1"); - ESP_LOGI(TAG, "sent unsubscribe successful, msg_id=%d", msg_id); - break; - case MQTT_EVENT_DISCONNECTED: - ESP_LOGI(TAG, "MQTT_EVENT_DISCONNECTED"); - break; - - case MQTT_EVENT_SUBSCRIBED: - ESP_LOGI(TAG, "MQTT_EVENT_SUBSCRIBED, msg_id=%d", event->msg_id); - msg_id = esp_mqtt_client_publish(client, "/topic/qos0", "data", 0, 0, 0); - ESP_LOGI(TAG, "sent publish successful, msg_id=%d", msg_id); - break; - case MQTT_EVENT_UNSUBSCRIBED: - ESP_LOGI(TAG, "MQTT_EVENT_UNSUBSCRIBED, msg_id=%d", event->msg_id); - break; - case MQTT_EVENT_PUBLISHED: - ESP_LOGI(TAG, "MQTT_EVENT_PUBLISHED, msg_id=%d", event->msg_id); - break; - case MQTT_EVENT_DATA: - ESP_LOGI(TAG, "MQTT_EVENT_DATA"); - printf("TOPIC=%.*s\r\n", event->topic_len, event->topic); - printf("DATA=%.*s\r\n", event->data_len, event->data); - break; - case MQTT_EVENT_ERROR: - ESP_LOGI(TAG, "MQTT_EVENT_ERROR"); - break; - default: - ESP_LOGI(TAG, "Other event id:%d", event->event_id); - break; + if (error_code != 0) { + ESP_LOGE(TAG, "Last error %s: 0x%x", message, error_code); } - return ESP_OK; } -static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data) { +/* + * @brief Event handler registered to receive MQTT events + * + * This function is called by the MQTT client event loop. + * + * @param handler_args user data registered to the event. + * @param base Event base for the handler(always MQTT Base in this example). + * @param event_id The id for the received event. + * @param event_data The data for the event, esp_mqtt_event_handle_t. + */ +static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data) +{ ESP_LOGD(TAG, "Event dispatched from event loop base=%s, event_id=%d", base, event_id); - mqtt_event_handler_cb(event_data); + esp_mqtt_event_handle_t event = event_data; + esp_mqtt_client_handle_t client = event->client; + int msg_id; + switch ((esp_mqtt_event_id_t)event_id) { + case MQTT_EVENT_CONNECTED: + ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED"); + msg_id = esp_mqtt_client_subscribe(client, "/topic/qos0", 0); + ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id); + + msg_id = esp_mqtt_client_subscribe(client, "/topic/qos1", 1); + ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id); + + msg_id = esp_mqtt_client_unsubscribe(client, "/topic/qos1"); + ESP_LOGI(TAG, "sent unsubscribe successful, msg_id=%d", msg_id); + break; + case MQTT_EVENT_DISCONNECTED: + ESP_LOGI(TAG, "MQTT_EVENT_DISCONNECTED"); + break; + + case MQTT_EVENT_SUBSCRIBED: + ESP_LOGI(TAG, "MQTT_EVENT_SUBSCRIBED, msg_id=%d", event->msg_id); + msg_id = esp_mqtt_client_publish(client, "/topic/qos0", "data", 0, 0, 0); + ESP_LOGI(TAG, "sent publish successful, msg_id=%d", msg_id); + break; + case MQTT_EVENT_UNSUBSCRIBED: + ESP_LOGI(TAG, "MQTT_EVENT_UNSUBSCRIBED, msg_id=%d", event->msg_id); + break; + case MQTT_EVENT_PUBLISHED: + ESP_LOGI(TAG, "MQTT_EVENT_PUBLISHED, msg_id=%d", event->msg_id); + break; + case MQTT_EVENT_DATA: + ESP_LOGI(TAG, "MQTT_EVENT_DATA"); + printf("TOPIC=%.*s\r\n", event->topic_len, event->topic); + printf("DATA=%.*s\r\n", event->data_len, event->data); + break; + case MQTT_EVENT_ERROR: + ESP_LOGI(TAG, "MQTT_EVENT_ERROR"); + if (event->error_handle->error_type == MQTT_ERROR_TYPE_TCP_TRANSPORT) { + log_error_if_nonzero("reported from esp-tls", event->error_handle->esp_tls_last_esp_err); + log_error_if_nonzero("reported from tls stack", event->error_handle->esp_tls_stack_err); + log_error_if_nonzero("captured as transport's socket errno", event->error_handle->esp_transport_sock_errno); + ESP_LOGI(TAG, "Last errno string (%s)", strerror(event->error_handle->esp_transport_sock_errno)); + + } + break; + default: + ESP_LOGI(TAG, "Other event id:%d", event->event_id); + break; + } } static void mqtt_app_start(void) @@ -101,7 +120,8 @@ static void mqtt_app_start(void) ESP_LOGI(TAG, "[APP] Free memory: %d bytes", esp_get_free_heap_size()); esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg); - esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, client); + /* The last argument may be used to pass data to the event handler, in this example mqtt_event_handler */ + esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, NULL); esp_mqtt_client_start(client); } diff --git a/examples/protocols/mqtt/tcp/main/app_main.c b/examples/protocols/mqtt/tcp/main/app_main.c index f23e774..3f63ce4 100644 --- a/examples/protocols/mqtt/tcp/main/app_main.c +++ b/examples/protocols/mqtt/tcp/main/app_main.c @@ -33,73 +33,78 @@ static const char *TAG = "MQTT_EXAMPLE"; -static void log_error_if_nonzero(const char * message, int error_code) +static void log_error_if_nonzero(const char *message, int error_code) { if (error_code != 0) { ESP_LOGE(TAG, "Last error %s: 0x%x", message, error_code); } } -static esp_err_t mqtt_event_handler_cb(esp_mqtt_event_handle_t event) +/* + * @brief Event handler registered to receive MQTT events + * + * This function is called by the MQTT client event loop. + * + * @param handler_args user data registered to the event. + * @param base Event base for the handler(always MQTT Base in this example). + * @param event_id The id for the received event. + * @param event_data The data for the event, esp_mqtt_event_handle_t. + */ +static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data) { + ESP_LOGD(TAG, "Event dispatched from event loop base=%s, event_id=%d", base, event_id); + esp_mqtt_event_handle_t event = event_data; esp_mqtt_client_handle_t client = event->client; int msg_id; - // your_context_t *context = event->context; - switch (event->event_id) { - case MQTT_EVENT_CONNECTED: - ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED"); - msg_id = esp_mqtt_client_publish(client, "/topic/qos1", "data_3", 0, 1, 0); - ESP_LOGI(TAG, "sent publish successful, msg_id=%d", msg_id); + switch ((esp_mqtt_event_id_t)event_id) { + case MQTT_EVENT_CONNECTED: + ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED"); + msg_id = esp_mqtt_client_publish(client, "/topic/qos1", "data_3", 0, 1, 0); + ESP_LOGI(TAG, "sent publish successful, msg_id=%d", msg_id); - msg_id = esp_mqtt_client_subscribe(client, "/topic/qos0", 0); - ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id); + msg_id = esp_mqtt_client_subscribe(client, "/topic/qos0", 0); + ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id); - msg_id = esp_mqtt_client_subscribe(client, "/topic/qos1", 1); - ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id); + msg_id = esp_mqtt_client_subscribe(client, "/topic/qos1", 1); + ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id); - msg_id = esp_mqtt_client_unsubscribe(client, "/topic/qos1"); - ESP_LOGI(TAG, "sent unsubscribe successful, msg_id=%d", msg_id); - break; - case MQTT_EVENT_DISCONNECTED: - ESP_LOGI(TAG, "MQTT_EVENT_DISCONNECTED"); - break; + msg_id = esp_mqtt_client_unsubscribe(client, "/topic/qos1"); + ESP_LOGI(TAG, "sent unsubscribe successful, msg_id=%d", msg_id); + break; + case MQTT_EVENT_DISCONNECTED: + ESP_LOGI(TAG, "MQTT_EVENT_DISCONNECTED"); + break; - case MQTT_EVENT_SUBSCRIBED: - ESP_LOGI(TAG, "MQTT_EVENT_SUBSCRIBED, msg_id=%d", event->msg_id); - msg_id = esp_mqtt_client_publish(client, "/topic/qos0", "data", 0, 0, 0); - ESP_LOGI(TAG, "sent publish successful, msg_id=%d", msg_id); - break; - case MQTT_EVENT_UNSUBSCRIBED: - ESP_LOGI(TAG, "MQTT_EVENT_UNSUBSCRIBED, msg_id=%d", event->msg_id); - break; - case MQTT_EVENT_PUBLISHED: - ESP_LOGI(TAG, "MQTT_EVENT_PUBLISHED, msg_id=%d", event->msg_id); - break; - case MQTT_EVENT_DATA: - ESP_LOGI(TAG, "MQTT_EVENT_DATA"); - printf("TOPIC=%.*s\r\n", event->topic_len, event->topic); - printf("DATA=%.*s\r\n", event->data_len, event->data); - break; - case MQTT_EVENT_ERROR: - ESP_LOGI(TAG, "MQTT_EVENT_ERROR"); - if (event->error_handle->error_type == MQTT_ERROR_TYPE_TCP_TRANSPORT) { - log_error_if_nonzero("reported from esp-tls", event->error_handle->esp_tls_last_esp_err); - log_error_if_nonzero("reported from tls stack", event->error_handle->esp_tls_stack_err); - log_error_if_nonzero("captured as transport's socket errno", event->error_handle->esp_transport_sock_errno); - ESP_LOGI(TAG, "Last errno string (%s)", strerror(event->error_handle->esp_transport_sock_errno)); + case MQTT_EVENT_SUBSCRIBED: + ESP_LOGI(TAG, "MQTT_EVENT_SUBSCRIBED, msg_id=%d", event->msg_id); + msg_id = esp_mqtt_client_publish(client, "/topic/qos0", "data", 0, 0, 0); + ESP_LOGI(TAG, "sent publish successful, msg_id=%d", msg_id); + break; + case MQTT_EVENT_UNSUBSCRIBED: + ESP_LOGI(TAG, "MQTT_EVENT_UNSUBSCRIBED, msg_id=%d", event->msg_id); + break; + case MQTT_EVENT_PUBLISHED: + ESP_LOGI(TAG, "MQTT_EVENT_PUBLISHED, msg_id=%d", event->msg_id); + break; + case MQTT_EVENT_DATA: + ESP_LOGI(TAG, "MQTT_EVENT_DATA"); + printf("TOPIC=%.*s\r\n", event->topic_len, event->topic); + printf("DATA=%.*s\r\n", event->data_len, event->data); + break; + case MQTT_EVENT_ERROR: + ESP_LOGI(TAG, "MQTT_EVENT_ERROR"); + if (event->error_handle->error_type == MQTT_ERROR_TYPE_TCP_TRANSPORT) { + log_error_if_nonzero("reported from esp-tls", event->error_handle->esp_tls_last_esp_err); + log_error_if_nonzero("reported from tls stack", event->error_handle->esp_tls_stack_err); + log_error_if_nonzero("captured as transport's socket errno", event->error_handle->esp_transport_sock_errno); + ESP_LOGI(TAG, "Last errno string (%s)", strerror(event->error_handle->esp_transport_sock_errno)); - } - break; - default: - ESP_LOGI(TAG, "Other event id:%d", event->event_id); - break; + } + break; + default: + ESP_LOGI(TAG, "Other event id:%d", event->event_id); + break; } - return ESP_OK; -} - -static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data) { - ESP_LOGD(TAG, "Event dispatched from event loop base=%s, event_id=%d", base, event_id); - mqtt_event_handler_cb(event_data); } static void mqtt_app_start(void) @@ -133,7 +138,8 @@ static void mqtt_app_start(void) #endif /* CONFIG_BROKER_URL_FROM_STDIN */ esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg); - esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, client); + /* The last argument may be used to pass data to the event handler, in this example mqtt_event_handler */ + esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, NULL); esp_mqtt_client_start(client); } diff --git a/examples/protocols/mqtt/ws/main/app_main.c b/examples/protocols/mqtt/ws/main/app_main.c index 96b29e3..09a81b5 100644 --- a/examples/protocols/mqtt/ws/main/app_main.c +++ b/examples/protocols/mqtt/ws/main/app_main.c @@ -31,57 +31,77 @@ static const char *TAG = "MQTTWS_EXAMPLE"; - -static esp_err_t mqtt_event_handler_cb(esp_mqtt_event_handle_t event) +static void log_error_if_nonzero(const char *message, int error_code) { - esp_mqtt_client_handle_t client = event->client; - int msg_id; - // your_context_t *context = event->context; - switch (event->event_id) { - case MQTT_EVENT_CONNECTED: - ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED"); - msg_id = esp_mqtt_client_subscribe(client, "/topic/qos0", 0); - ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id); - - msg_id = esp_mqtt_client_subscribe(client, "/topic/qos1", 1); - ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id); - - msg_id = esp_mqtt_client_unsubscribe(client, "/topic/qos1"); - ESP_LOGI(TAG, "sent unsubscribe successful, msg_id=%d", msg_id); - break; - case MQTT_EVENT_DISCONNECTED: - ESP_LOGI(TAG, "MQTT_EVENT_DISCONNECTED"); - break; - - case MQTT_EVENT_SUBSCRIBED: - ESP_LOGI(TAG, "MQTT_EVENT_SUBSCRIBED, msg_id=%d", event->msg_id); - msg_id = esp_mqtt_client_publish(client, "/topic/qos0", "data", 0, 0, 0); - ESP_LOGI(TAG, "sent publish successful, msg_id=%d", msg_id); - break; - case MQTT_EVENT_UNSUBSCRIBED: - ESP_LOGI(TAG, "MQTT_EVENT_UNSUBSCRIBED, msg_id=%d", event->msg_id); - break; - case MQTT_EVENT_PUBLISHED: - ESP_LOGI(TAG, "MQTT_EVENT_PUBLISHED, msg_id=%d", event->msg_id); - break; - case MQTT_EVENT_DATA: - ESP_LOGI(TAG, "MQTT_EVENT_DATA"); - printf("TOPIC=%.*s\r\n", event->topic_len, event->topic); - printf("DATA=%.*s\r\n", event->data_len, event->data); - break; - case MQTT_EVENT_ERROR: - ESP_LOGI(TAG, "MQTT_EVENT_ERROR"); - break; - default: - ESP_LOGI(TAG, "Other event id:%d", event->event_id); - break; + if (error_code != 0) { + ESP_LOGE(TAG, "Last error %s: 0x%x", message, error_code); } - return ESP_OK; } -static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data) { +/* + * @brief Event handler registered to receive MQTT events + * + * This function is called by the MQTT client event loop. + * + * @param handler_args user data registered to the event. + * @param base Event base for the handler(always MQTT Base in this example). + * @param event_id The id for the received event. + * @param event_data The data for the event, esp_mqtt_event_handle_t. + */ +static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data) +{ ESP_LOGD(TAG, "Event dispatched from event loop base=%s, event_id=%d", base, event_id); - mqtt_event_handler_cb(event_data); + esp_mqtt_event_handle_t event = event_data; + esp_mqtt_client_handle_t client = event->client; + int msg_id; + switch ((esp_mqtt_event_id_t)event_id) { + case MQTT_EVENT_CONNECTED: + ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED"); + msg_id = esp_mqtt_client_publish(client, "/topic/qos1", "data_3", 0, 1, 0); + ESP_LOGI(TAG, "sent publish successful, msg_id=%d", msg_id); + msg_id = esp_mqtt_client_subscribe(client, "/topic/qos0", 0); + ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id); + + msg_id = esp_mqtt_client_subscribe(client, "/topic/qos1", 1); + ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id); + + msg_id = esp_mqtt_client_unsubscribe(client, "/topic/qos1"); + ESP_LOGI(TAG, "sent unsubscribe successful, msg_id=%d", msg_id); + break; + case MQTT_EVENT_DISCONNECTED: + ESP_LOGI(TAG, "MQTT_EVENT_DISCONNECTED"); + break; + + case MQTT_EVENT_SUBSCRIBED: + ESP_LOGI(TAG, "MQTT_EVENT_SUBSCRIBED, msg_id=%d", event->msg_id); + msg_id = esp_mqtt_client_publish(client, "/topic/qos0", "data", 0, 0, 0); + ESP_LOGI(TAG, "sent publish successful, msg_id=%d", msg_id); + break; + case MQTT_EVENT_UNSUBSCRIBED: + ESP_LOGI(TAG, "MQTT_EVENT_UNSUBSCRIBED, msg_id=%d", event->msg_id); + break; + case MQTT_EVENT_PUBLISHED: + ESP_LOGI(TAG, "MQTT_EVENT_PUBLISHED, msg_id=%d", event->msg_id); + break; + case MQTT_EVENT_DATA: + ESP_LOGI(TAG, "MQTT_EVENT_DATA"); + printf("TOPIC=%.*s\r\n", event->topic_len, event->topic); + printf("DATA=%.*s\r\n", event->data_len, event->data); + break; + case MQTT_EVENT_ERROR: + ESP_LOGI(TAG, "MQTT_EVENT_ERROR"); + if (event->error_handle->error_type == MQTT_ERROR_TYPE_TCP_TRANSPORT) { + log_error_if_nonzero("reported from esp-tls", event->error_handle->esp_tls_last_esp_err); + log_error_if_nonzero("reported from tls stack", event->error_handle->esp_tls_stack_err); + log_error_if_nonzero("captured as transport's socket errno", event->error_handle->esp_transport_sock_errno); + ESP_LOGI(TAG, "Last errno string (%s)", strerror(event->error_handle->esp_transport_sock_errno)); + + } + break; + default: + ESP_LOGI(TAG, "Other event id:%d", event->event_id); + break; + } } static void mqtt_app_start(void) @@ -91,7 +111,8 @@ static void mqtt_app_start(void) }; esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg); - esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, client); + /* The last argument may be used to pass data to the event handler, in this example mqtt_event_handler */ + esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, NULL); esp_mqtt_client_start(client); } diff --git a/examples/protocols/mqtt/wss/main/app_main.c b/examples/protocols/mqtt/wss/main/app_main.c index 61b7b44..3c56890 100644 --- a/examples/protocols/mqtt/wss/main/app_main.c +++ b/examples/protocols/mqtt/wss/main/app_main.c @@ -45,48 +45,50 @@ static esp_err_t mqtt_event_handler_cb(esp_mqtt_event_handle_t event) int msg_id; // your_context_t *context = event->context; switch (event->event_id) { - case MQTT_EVENT_CONNECTED: - ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED"); - msg_id = esp_mqtt_client_subscribe(client, "/topic/qos0", 0); - ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id); + case MQTT_EVENT_CONNECTED: + ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED"); + msg_id = esp_mqtt_client_subscribe(client, "/topic/qos0", 0); + ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id); - msg_id = esp_mqtt_client_subscribe(client, "/topic/qos1", 1); - ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id); + msg_id = esp_mqtt_client_subscribe(client, "/topic/qos1", 1); + ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id); - msg_id = esp_mqtt_client_unsubscribe(client, "/topic/qos1"); - ESP_LOGI(TAG, "sent unsubscribe successful, msg_id=%d", msg_id); - break; - case MQTT_EVENT_DISCONNECTED: - ESP_LOGI(TAG, "MQTT_EVENT_DISCONNECTED"); - break; + msg_id = esp_mqtt_client_unsubscribe(client, "/topic/qos1"); + ESP_LOGI(TAG, "sent unsubscribe successful, msg_id=%d", msg_id); + break; + case MQTT_EVENT_DISCONNECTED: + ESP_LOGI(TAG, "MQTT_EVENT_DISCONNECTED"); + break; - case MQTT_EVENT_SUBSCRIBED: - ESP_LOGI(TAG, "MQTT_EVENT_SUBSCRIBED, msg_id=%d", event->msg_id); - msg_id = esp_mqtt_client_publish(client, "/topic/qos0", "data", 0, 0, 0); - ESP_LOGI(TAG, "sent publish successful, msg_id=%d", msg_id); - break; - case MQTT_EVENT_UNSUBSCRIBED: - ESP_LOGI(TAG, "MQTT_EVENT_UNSUBSCRIBED, msg_id=%d", event->msg_id); - break; - case MQTT_EVENT_PUBLISHED: - ESP_LOGI(TAG, "MQTT_EVENT_PUBLISHED, msg_id=%d", event->msg_id); - break; - case MQTT_EVENT_DATA: - ESP_LOGI(TAG, "MQTT_EVENT_DATA"); - printf("TOPIC=%.*s\r\n", event->topic_len, event->topic); - printf("DATA=%.*s\r\n", event->data_len, event->data); - break; - case MQTT_EVENT_ERROR: - ESP_LOGI(TAG, "MQTT_EVENT_ERROR"); - break; - default: - ESP_LOGI(TAG, "Other event id:%d", event->event_id); - break; + case MQTT_EVENT_SUBSCRIBED: + ESP_LOGI(TAG, "MQTT_EVENT_SUBSCRIBED, msg_id=%d", event->msg_id); + msg_id = esp_mqtt_client_publish(client, "/topic/qos0", "data", 0, 0, 0); + ESP_LOGI(TAG, "sent publish successful, msg_id=%d", msg_id); + break; + case MQTT_EVENT_UNSUBSCRIBED: + ESP_LOGI(TAG, "MQTT_EVENT_UNSUBSCRIBED, msg_id=%d", event->msg_id); + break; + case MQTT_EVENT_PUBLISHED: + ESP_LOGI(TAG, "MQTT_EVENT_PUBLISHED, msg_id=%d", event->msg_id); + break; + case MQTT_EVENT_DATA: + ESP_LOGI(TAG, "MQTT_EVENT_DATA"); + printf("TOPIC=%.*s\r\n", event->topic_len, event->topic); + printf("DATA=%.*s\r\n", event->data_len, event->data); + break; + case MQTT_EVENT_ERROR: + ESP_LOGI(TAG, "MQTT_EVENT_ERROR"); + break; + default: + ESP_LOGI(TAG, "Other event id:%d", event->event_id); + break; } return ESP_OK; } -static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data) { +static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data) +{ + /* The argument passed to esp_mqtt_client_register_event can de accessed as handler_args*/ ESP_LOGD(TAG, "Event dispatched from event loop base=%s, event_id=%d", base, event_id); mqtt_event_handler_cb(event_data); } @@ -100,7 +102,8 @@ static void mqtt_app_start(void) ESP_LOGI(TAG, "[APP] Free memory: %d bytes", esp_get_free_heap_size()); esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg); - esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, client); + /* The last argument may be used to pass data to the event handler, in this example mqtt_event_handler */ + esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, NULL); esp_mqtt_client_start(client); } From ad00d740f41f4e831e3412728f49f9ef065b2f1b Mon Sep 17 00:00:00 2001 From: David Cermak Date: Mon, 1 Feb 2021 16:15:50 +0100 Subject: [PATCH 081/231] examples/mqtt: Use common transport for setting log severity --- examples/protocols/mqtt/ssl/main/app_main.c | 3 +-- examples/protocols/mqtt/ssl_ds/main/app_main.c | 3 +-- examples/protocols/mqtt/ssl_mutual_auth/main/app_main.c | 3 +-- examples/protocols/mqtt/ssl_psk/main/app_main.c | 3 +-- examples/protocols/mqtt/tcp/main/app_main.c | 4 ++-- examples/protocols/mqtt/ws/main/app_main.c | 3 +-- examples/protocols/mqtt/wss/main/app_main.c | 3 +-- .../mqtt/publish_connect_test/main/publish_connect_test.c | 3 +-- 8 files changed, 9 insertions(+), 16 deletions(-) diff --git a/examples/protocols/mqtt/ssl/main/app_main.c b/examples/protocols/mqtt/ssl/main/app_main.c index 0f4d26a..3e8775d 100644 --- a/examples/protocols/mqtt/ssl/main/app_main.c +++ b/examples/protocols/mqtt/ssl/main/app_main.c @@ -144,8 +144,7 @@ void app_main(void) esp_log_level_set("esp-tls", ESP_LOG_VERBOSE); esp_log_level_set("MQTT_CLIENT", ESP_LOG_VERBOSE); esp_log_level_set("MQTT_EXAMPLE", ESP_LOG_VERBOSE); - esp_log_level_set("TRANSPORT_TCP", ESP_LOG_VERBOSE); - esp_log_level_set("TRANSPORT_SSL", ESP_LOG_VERBOSE); + esp_log_level_set("TRANSPORT_BASE", ESP_LOG_VERBOSE); esp_log_level_set("TRANSPORT", ESP_LOG_VERBOSE); esp_log_level_set("OUTBOX", ESP_LOG_VERBOSE); diff --git a/examples/protocols/mqtt/ssl_ds/main/app_main.c b/examples/protocols/mqtt/ssl_ds/main/app_main.c index ca288f8..502dce4 100644 --- a/examples/protocols/mqtt/ssl_ds/main/app_main.c +++ b/examples/protocols/mqtt/ssl_ds/main/app_main.c @@ -194,8 +194,7 @@ void app_main(void) esp_log_level_set("*", ESP_LOG_INFO); esp_log_level_set("MQTT_CLIENT", ESP_LOG_VERBOSE); - esp_log_level_set("TRANSPORT_TCP", ESP_LOG_VERBOSE); - esp_log_level_set("TRANSPORT_SSL", ESP_LOG_VERBOSE); + esp_log_level_set("TRANSPORT_BASE", ESP_LOG_VERBOSE); esp_log_level_set("TRANSPORT", ESP_LOG_VERBOSE); esp_log_level_set("OUTBOX", ESP_LOG_VERBOSE); diff --git a/examples/protocols/mqtt/ssl_mutual_auth/main/app_main.c b/examples/protocols/mqtt/ssl_mutual_auth/main/app_main.c index a5add84..48ad78d 100644 --- a/examples/protocols/mqtt/ssl_mutual_auth/main/app_main.c +++ b/examples/protocols/mqtt/ssl_mutual_auth/main/app_main.c @@ -133,8 +133,7 @@ void app_main(void) esp_log_level_set("*", ESP_LOG_INFO); esp_log_level_set("MQTT_CLIENT", ESP_LOG_VERBOSE); - esp_log_level_set("TRANSPORT_TCP", ESP_LOG_VERBOSE); - esp_log_level_set("TRANSPORT_SSL", ESP_LOG_VERBOSE); + esp_log_level_set("TRANSPORT_BASE", ESP_LOG_VERBOSE); esp_log_level_set("TRANSPORT", ESP_LOG_VERBOSE); esp_log_level_set("OUTBOX", ESP_LOG_VERBOSE); diff --git a/examples/protocols/mqtt/ssl_psk/main/app_main.c b/examples/protocols/mqtt/ssl_psk/main/app_main.c index 3ca754c..cbb4526 100644 --- a/examples/protocols/mqtt/ssl_psk/main/app_main.c +++ b/examples/protocols/mqtt/ssl_psk/main/app_main.c @@ -121,8 +121,7 @@ void app_main(void) esp_log_level_set("*", ESP_LOG_INFO); esp_log_level_set("MQTT_CLIENT", ESP_LOG_VERBOSE); - esp_log_level_set("TRANSPORT_TCP", ESP_LOG_VERBOSE); - esp_log_level_set("TRANSPORT_SSL", ESP_LOG_VERBOSE); + esp_log_level_set("TRANSPORT_BASE", ESP_LOG_VERBOSE); esp_log_level_set("esp-tls", ESP_LOG_VERBOSE); esp_log_level_set("TRANSPORT", ESP_LOG_VERBOSE); esp_log_level_set("OUTBOX", ESP_LOG_VERBOSE); diff --git a/examples/protocols/mqtt/tcp/main/app_main.c b/examples/protocols/mqtt/tcp/main/app_main.c index 3f63ce4..48b1c47 100644 --- a/examples/protocols/mqtt/tcp/main/app_main.c +++ b/examples/protocols/mqtt/tcp/main/app_main.c @@ -152,8 +152,8 @@ void app_main(void) esp_log_level_set("*", ESP_LOG_INFO); esp_log_level_set("MQTT_CLIENT", ESP_LOG_VERBOSE); esp_log_level_set("MQTT_EXAMPLE", ESP_LOG_VERBOSE); - esp_log_level_set("TRANSPORT_TCP", ESP_LOG_VERBOSE); - esp_log_level_set("TRANSPORT_SSL", ESP_LOG_VERBOSE); + esp_log_level_set("TRANSPORT_BASE", ESP_LOG_VERBOSE); + esp_log_level_set("esp-tls", ESP_LOG_VERBOSE); esp_log_level_set("TRANSPORT", ESP_LOG_VERBOSE); esp_log_level_set("OUTBOX", ESP_LOG_VERBOSE); diff --git a/examples/protocols/mqtt/ws/main/app_main.c b/examples/protocols/mqtt/ws/main/app_main.c index 09a81b5..32410e8 100644 --- a/examples/protocols/mqtt/ws/main/app_main.c +++ b/examples/protocols/mqtt/ws/main/app_main.c @@ -125,8 +125,7 @@ void app_main(void) esp_log_level_set("*", ESP_LOG_INFO); esp_log_level_set("MQTT_CLIENT", ESP_LOG_VERBOSE); esp_log_level_set("MQTT_EXAMPLE", ESP_LOG_VERBOSE); - esp_log_level_set("TRANSPORT_TCP", ESP_LOG_VERBOSE); - esp_log_level_set("TRANSPORT_SSL", ESP_LOG_VERBOSE); + esp_log_level_set("TRANSPORT_BASE", ESP_LOG_VERBOSE); esp_log_level_set("TRANSPORT_WS", ESP_LOG_VERBOSE); esp_log_level_set("TRANSPORT", ESP_LOG_VERBOSE); esp_log_level_set("OUTBOX", ESP_LOG_VERBOSE); diff --git a/examples/protocols/mqtt/wss/main/app_main.c b/examples/protocols/mqtt/wss/main/app_main.c index 3c56890..d0495e2 100644 --- a/examples/protocols/mqtt/wss/main/app_main.c +++ b/examples/protocols/mqtt/wss/main/app_main.c @@ -117,8 +117,7 @@ void app_main(void) esp_log_level_set("*", ESP_LOG_INFO); esp_log_level_set("MQTT_CLIENT", ESP_LOG_VERBOSE); esp_log_level_set("MQTT_EXAMPLE", ESP_LOG_VERBOSE); - esp_log_level_set("TRANSPORT_TCP", ESP_LOG_VERBOSE); - esp_log_level_set("TRANSPORT_SSL", ESP_LOG_VERBOSE); + esp_log_level_set("TRANSPORT_BASE", ESP_LOG_VERBOSE); esp_log_level_set("TRANSPORT", ESP_LOG_VERBOSE); esp_log_level_set("OUTBOX", ESP_LOG_VERBOSE); diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/main/publish_connect_test.c b/tools/test_apps/protocols/mqtt/publish_connect_test/main/publish_connect_test.c index a7a2504..a112834 100644 --- a/tools/test_apps/protocols/mqtt/publish_connect_test/main/publish_connect_test.c +++ b/tools/test_apps/protocols/mqtt/publish_connect_test/main/publish_connect_test.c @@ -46,8 +46,7 @@ void app_main(void) esp_log_level_set("*", ESP_LOG_INFO); esp_log_level_set("MQTT_CLIENT", ESP_LOG_VERBOSE); - esp_log_level_set("TRANSPORT_TCP", ESP_LOG_VERBOSE); - esp_log_level_set("TRANSPORT_SSL", ESP_LOG_VERBOSE); + esp_log_level_set("TRANSPORT_BASE", ESP_LOG_VERBOSE); esp_log_level_set("TRANSPORT", ESP_LOG_VERBOSE); esp_log_level_set("OUTBOX", ESP_LOG_VERBOSE); From c500e7c930c016dfb74c30593df97f25a978345f Mon Sep 17 00:00:00 2001 From: yuanjm Date: Tue, 23 Feb 2021 15:29:07 +0800 Subject: [PATCH 082/231] examples: Update mqtt open source test server address --- examples/protocols/mqtt/ssl/README.md | 8 ++++---- examples/protocols/mqtt/ssl/main/Kconfig.projbuild | 2 +- examples/protocols/mqtt/tcp/main/Kconfig.projbuild | 2 +- examples/protocols/mqtt/ws/README.md | 2 +- examples/protocols/mqtt/ws/main/Kconfig.projbuild | 2 +- examples/protocols/mqtt/wss/README.md | 10 +++++----- examples/protocols/mqtt/wss/main/Kconfig.projbuild | 2 +- 7 files changed, 14 insertions(+), 14 deletions(-) diff --git a/examples/protocols/mqtt/ssl/README.md b/examples/protocols/mqtt/ssl/README.md index aa96a68..680fc8a 100644 --- a/examples/protocols/mqtt/ssl/README.md +++ b/examples/protocols/mqtt/ssl/README.md @@ -2,7 +2,7 @@ (See the README.md file in the upper level 'examples' directory for more information about examples.) -This example connects to the broker mqtt.eclipse.org using ssl transport and as a demonstration subscribes/unsubscribes and send a message on certain topic. +This example connects to the broker mqtt.eclipseprojects.io using ssl transport and as a demonstration subscribes/unsubscribes and send a message on certain topic. (Please note that the public broker is maintained by the community so may not be always available, for details please see this [disclaimer](https://iot.eclipse.org/getting-started/#sandboxes)) It uses ESP-MQTT library which implements mqtt client to connect to mqtt broker. @@ -19,13 +19,13 @@ This example can be executed on any ESP32 board, the only required interface is * Configure Wi-Fi or Ethernet under "Example Connection Configuration" menu. See "Establishing Wi-Fi or Ethernet Connection" section in [examples/protocols/README.md](../../README.md) for more details. * When using Make build system, set `Default serial port` under `Serial flasher config`. -PEM certificate for this example could be extracted from an openssl `s_client` command connecting to mqtt.eclipse.org. +PEM certificate for this example could be extracted from an openssl `s_client` command connecting to mqtt.eclipseprojects.io. In case a host operating system has `openssl` and `sed` packages installed, one could execute the following command to download and save the root certificate to a file (Note for Windows users: Both Linux like environment or Windows native packages may be used). ``` -echo "" | openssl s_client -showcerts -connect mqtt.eclipse.org:8883 | sed -n "1,/Root/d; /BEGIN/,/END/p" | openssl x509 -outform PEM >mqtt_eclipse_org.pem +echo "" | openssl s_client -showcerts -connect mqtt.eclipseprojects.io:8883 | sed -n "1,/Root/d; /BEGIN/,/END/p" | openssl x509 -outform PEM >mqtt_eclipse_org.pem ``` Please note that this is not a general command for downloading a root certificate for an arbitrary host; -this command works with mqtt.eclipse.org as the site provides root certificate in the chain, which then could be extracted +this command works with mqtt.eclipseprojects.io as the site provides root certificate in the chain, which then could be extracted with text operation. ### Build and Flash diff --git a/examples/protocols/mqtt/ssl/main/Kconfig.projbuild b/examples/protocols/mqtt/ssl/main/Kconfig.projbuild index b1acbc5..5e9357d 100644 --- a/examples/protocols/mqtt/ssl/main/Kconfig.projbuild +++ b/examples/protocols/mqtt/ssl/main/Kconfig.projbuild @@ -2,7 +2,7 @@ menu "Example Configuration" config BROKER_URI string "Broker URL" - default "mqtts://mqtt.eclipse.org:8883" + default "mqtts://mqtt.eclipseprojects.io:8883" help URL of an mqtt broker which this example connects to. diff --git a/examples/protocols/mqtt/tcp/main/Kconfig.projbuild b/examples/protocols/mqtt/tcp/main/Kconfig.projbuild index fc6e8a1..c11539f 100644 --- a/examples/protocols/mqtt/tcp/main/Kconfig.projbuild +++ b/examples/protocols/mqtt/tcp/main/Kconfig.projbuild @@ -2,7 +2,7 @@ menu "Example Configuration" config BROKER_URL string "Broker URL" - default "mqtt://mqtt.eclipse.org" + default "mqtt://mqtt.eclipseprojects.io" help URL of the broker to connect to diff --git a/examples/protocols/mqtt/ws/README.md b/examples/protocols/mqtt/ws/README.md index 3b2a05a..c9ca6ea 100644 --- a/examples/protocols/mqtt/ws/README.md +++ b/examples/protocols/mqtt/ws/README.md @@ -2,7 +2,7 @@ (See the README.md file in the upper level 'examples' directory for more information about examples.) -This example connects to the broker mqtt.eclipse.org over web sockets as a demonstration subscribes/unsubscribes and send a message on certain topic. +This example connects to the broker mqtt.eclipseprojects.io over web sockets as a demonstration subscribes/unsubscribes and send a message on certain topic. (Please note that the public broker is maintained by the community so may not be always available, for details please see this [disclaimer](https://iot.eclipse.org/getting-started/#sandboxes)) It uses ESP-MQTT library which implements mqtt client to connect to mqtt broker. diff --git a/examples/protocols/mqtt/ws/main/Kconfig.projbuild b/examples/protocols/mqtt/ws/main/Kconfig.projbuild index c298f5d..e547a51 100644 --- a/examples/protocols/mqtt/ws/main/Kconfig.projbuild +++ b/examples/protocols/mqtt/ws/main/Kconfig.projbuild @@ -2,7 +2,7 @@ menu "Example Configuration" config BROKER_URI string "Broker URL" - default "ws://mqtt.eclipse.org:80/mqtt" + default "ws://mqtt.eclipseprojects.io:80/mqtt" help URL of an mqtt broker which this example connects to. diff --git a/examples/protocols/mqtt/wss/README.md b/examples/protocols/mqtt/wss/README.md index c07477f..3d2fa2b 100644 --- a/examples/protocols/mqtt/wss/README.md +++ b/examples/protocols/mqtt/wss/README.md @@ -1,7 +1,7 @@ # ESP-MQTT MQTT over WSS Sample application (See the README.md file in the upper level 'examples' directory for more information about examples.) -This example connects to the broker mqtt.eclipse.org over secure websockets and as a demonstration subscribes/unsubscribes and send a message on certain topic. +This example connects to the broker mqtt.eclipseprojects.io over secure websockets and as a demonstration subscribes/unsubscribes and send a message on certain topic. (Please note that the public broker is maintained by the community so may not be always available, for details please see this [disclaimer](https://iot.eclipse.org/getting-started/#sandboxes)) It uses ESP-MQTT library which implements mqtt client to connect to mqtt broker. @@ -18,15 +18,15 @@ This example can be executed on any ESP32 board, the only required interface is * Configure Wi-Fi or Ethernet under "Example Connection Configuration" menu. See "Establishing Wi-Fi or Ethernet Connection" section in [examples/protocols/README.md](../../README.md) for more details. * When using Make build system, set `Default serial port` under `Serial flasher config`. -Note how to create a PEM certificate for mqtt.eclipse.org: +Note how to create a PEM certificate for mqtt.eclipseprojects.io: -PEM certificate for this example could be extracted from an openssl `s_client` command connecting to mqtt.eclipse.org. +PEM certificate for this example could be extracted from an openssl `s_client` command connecting to mqtt.eclipseprojects.io. In case a host operating system has `openssl` and `sed` packages installed, one could execute the following command to download and save the root certificate to a file (Note for Windows users: Both Linux like environment or Windows native packages may be used). ``` -echo "" | openssl s_client -showcerts -connect mqtt.eclipse.org:443 | sed -n "1,/Root/d; /BEGIN/,/END/p" | openssl x509 -outform PEM >mqtt_eclipse_org.pem +echo "" | openssl s_client -showcerts -connect mqtt.eclipseprojects.io:443 | sed -n "1,/Root/d; /BEGIN/,/END/p" | openssl x509 -outform PEM >mqtt_eclipse_org.pem ``` Please note that this is not a general command for downloading a root certificate for an arbitrary host; -this command works with mqtt.eclipse.org as the site provides root certificate in the chain, which then could be extracted +this command works with mqtt.eclipseprojects.io as the site provides root certificate in the chain, which then could be extracted with text operation. ### Build and Flash diff --git a/examples/protocols/mqtt/wss/main/Kconfig.projbuild b/examples/protocols/mqtt/wss/main/Kconfig.projbuild index 5e43c89..b6d1f59 100644 --- a/examples/protocols/mqtt/wss/main/Kconfig.projbuild +++ b/examples/protocols/mqtt/wss/main/Kconfig.projbuild @@ -2,7 +2,7 @@ menu "Example Configuration" config BROKER_URI string "Broker URL" - default "wss://mqtt.eclipse.org:443/mqtt" + default "wss://mqtt.eclipseprojects.io:443/mqtt" help URL of an mqtt broker which this example connects to. From f6629ff7e19556a5578ef6d2665a42e149858c48 Mon Sep 17 00:00:00 2001 From: yuanjm Date: Tue, 23 Feb 2021 15:30:06 +0800 Subject: [PATCH 083/231] doc: Update mqtt open source test server address --- docs/en/api-reference/protocols/mqtt.rst | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/docs/en/api-reference/protocols/mqtt.rst b/docs/en/api-reference/protocols/mqtt.rst index 47021b6..7d00a03 100644 --- a/docs/en/api-reference/protocols/mqtt.rst +++ b/docs/en/api-reference/protocols/mqtt.rst @@ -33,30 +33,30 @@ URI - Curently support ``mqtt``, ``mqtts``, ``ws``, ``wss`` schemes - MQTT over TCP samples: - - ``mqtt://mqtt.eclipse.org``: MQTT over TCP, default port 1883: - - ``mqtt://mqtt.eclipse.org:1884`` MQTT over TCP, port 1884: - - ``mqtt://username:password@mqtt.eclipse.org:1884`` MQTT over TCP, + - ``mqtt://mqtt.eclipseprojects.io``: MQTT over TCP, default port 1883: + - ``mqtt://mqtt.eclipseprojects.io:1884`` MQTT over TCP, port 1884: + - ``mqtt://username:password@mqtt.eclipseprojects.io:1884`` MQTT over TCP, port 1884, with username and password - MQTT over SSL samples: - - ``mqtts://mqtt.eclipse.org``: MQTT over SSL, port 8883 - - ``mqtts://mqtt.eclipse.org:8884``: MQTT over SSL, port 8884 + - ``mqtts://mqtt.eclipseprojects.io``: MQTT over SSL, port 8883 + - ``mqtts://mqtt.eclipseprojects.io:8884``: MQTT over SSL, port 8884 - MQTT over Websocket samples: - - ``ws://mqtt.eclipse.org:80/mqtt`` + - ``ws://mqtt.eclipseprojects.io:80/mqtt`` - MQTT over Websocket Secure samples: - - ``wss://mqtt.eclipse.org:443/mqtt`` + - ``wss://mqtt.eclipseprojects.io:443/mqtt`` - Minimal configurations: .. code:: c const esp_mqtt_client_config_t mqtt_cfg = { - .uri = "mqtt://mqtt.eclipse.org", + .uri = "mqtt://mqtt.eclipseprojects.io", // .user_context = (void *)your_context }; esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg); @@ -69,15 +69,15 @@ URI SSL ^^^ -- Get certificate from server, example: ``mqtt.eclipse.org`` - ``openssl s_client -showcerts -connect mqtt.eclipse.org:8883 /dev/null|openssl x509 -outform PEM >mqtt_eclipse_org.pem`` +- Get certificate from server, example: ``mqtt.eclipseprojects.io`` + ``openssl s_client -showcerts -connect mqtt.eclipseprojects.io:8883 /dev/null|openssl x509 -outform PEM >mqtt_eclipse_org.pem`` - Check the sample application: ``examples/mqtt_ssl`` - Configuration: .. code:: c const esp_mqtt_client_config_t mqtt_cfg = { - .uri = "mqtts://mqtt.eclipse.org:8883", + .uri = "mqtts://mqtt.eclipseprojects.io:8883", .event_handle = mqtt_event_handler, .cert_pem = (const char *)mqtt_eclipse_org_pem_start, }; From a9c5cd15d1219958081f5252ddb407d9c6f577b0 Mon Sep 17 00:00:00 2001 From: yuanjm Date: Tue, 23 Feb 2021 15:30:29 +0800 Subject: [PATCH 084/231] tools: Update mqtt open source test server address --- .../mqtt/publish_connect_test/main/Kconfig.projbuild | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/main/Kconfig.projbuild b/tools/test_apps/protocols/mqtt/publish_connect_test/main/Kconfig.projbuild index 874db25..f2c3843 100644 --- a/tools/test_apps/protocols/mqtt/publish_connect_test/main/Kconfig.projbuild +++ b/tools/test_apps/protocols/mqtt/publish_connect_test/main/Kconfig.projbuild @@ -2,25 +2,25 @@ menu "Example Configuration" config EXAMPLE_BROKER_SSL_URI string "Broker SSL URL" - default "mqtts://mqtt.eclipse.org:8883" + default "mqtts://mqtt.eclipseprojects.io:8883" help URL of an mqtt broker for ssl transport config EXAMPLE_BROKER_TCP_URI string "Broker TCP URL" - default "mqtt://mqtt.eclipse.org:1883" + default "mqtt://mqtt.eclipseprojects.io:1883" help URL of an mqtt broker for tcp transport config EXAMPLE_BROKER_WS_URI string "Broker WS URL" - default "ws://mqtt.eclipse.org:80/mqtt" + default "ws://mqtt.eclipseprojects.io:80/mqtt" help URL of an mqtt broker for ws transport config EXAMPLE_BROKER_WSS_URI string "Broker WSS URL" - default "wss://mqtt.eclipse.org:443/mqtt" + default "wss://mqtt.eclipseprojects.io:443/mqtt" help URL of an mqtt broker for wss transport From 38cb3195e1f690f120e50c53392174cf883f2b5f Mon Sep 17 00:00:00 2001 From: Marius Vikhammer Date: Wed, 17 Mar 2021 18:48:05 +0800 Subject: [PATCH 085/231] Support ESP32S3 Beta 3 target Update ROM API. Port changes from bringup branch. --- tools/test_apps/protocols/mqtt/publish_connect_test/README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/README.md b/tools/test_apps/protocols/mqtt/publish_connect_test/README.md index 2d926b2..3ed0ff0 100644 --- a/tools/test_apps/protocols/mqtt/publish_connect_test/README.md +++ b/tools/test_apps/protocols/mqtt/publish_connect_test/README.md @@ -1,3 +1,6 @@ +| Supported Targets | ESP32 | ESP32-S2 | ESP32-C3 | +| ----------------- | ----- | -------- | -------- | + # ESP-MQTT advanced publish and connect test project Main purpose of this application is to test the MQTT library to correctly publish and receive messages (of different size and sequences) over different transports. From 5fafa05f923c15ba78bb28f846fafd54d939d68a Mon Sep 17 00:00:00 2001 From: Euripedes Rocha Date: Mon, 29 Mar 2021 10:52:18 +0100 Subject: [PATCH 086/231] bugfix/mqtt examples: Updates the CA certificates used. - Updates the CA certificates. - Updates the URI names to reflect the new URI in the service used in the examples. Closes IDFGH-4986 Closes https://github.com/espressif/esp-idf/issues/6776 --- examples/protocols/mqtt/ssl/CMakeLists.txt | 2 +- examples/protocols/mqtt/ssl/main/app_main.c | 8 ++--- examples/protocols/mqtt/ssl/main/component.mk | 2 +- .../mqtt/ssl/main/mqtt_eclipse_org.pem | 27 ----------------- .../mqtt/ssl/main/mqtt_eclipseprojects_io.pem | 30 +++++++++++++++++++ examples/protocols/mqtt/wss/CMakeLists.txt | 2 +- examples/protocols/mqtt/wss/main/app_main.c | 8 ++--- examples/protocols/mqtt/wss/main/component.mk | 2 +- .../mqtt/wss/main/mqtt_eclipse_org.pem | 27 ----------------- .../mqtt/wss/main/mqtt_eclipseprojects_io.pem | 30 +++++++++++++++++++ 10 files changed, 72 insertions(+), 66 deletions(-) delete mode 100644 examples/protocols/mqtt/ssl/main/mqtt_eclipse_org.pem create mode 100644 examples/protocols/mqtt/ssl/main/mqtt_eclipseprojects_io.pem delete mode 100644 examples/protocols/mqtt/wss/main/mqtt_eclipse_org.pem create mode 100644 examples/protocols/mqtt/wss/main/mqtt_eclipseprojects_io.pem diff --git a/examples/protocols/mqtt/ssl/CMakeLists.txt b/examples/protocols/mqtt/ssl/CMakeLists.txt index 2b78a84..bb945b4 100644 --- a/examples/protocols/mqtt/ssl/CMakeLists.txt +++ b/examples/protocols/mqtt/ssl/CMakeLists.txt @@ -9,4 +9,4 @@ set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_exam include($ENV{IDF_PATH}/tools/cmake/project.cmake) project(mqtt_ssl) -target_add_binary_data(mqtt_ssl.elf "main/mqtt_eclipse_org.pem" TEXT) +target_add_binary_data(mqtt_ssl.elf "main/mqtt_eclipseprojects_io.pem" TEXT) diff --git a/examples/protocols/mqtt/ssl/main/app_main.c b/examples/protocols/mqtt/ssl/main/app_main.c index 3e8775d..1cbf589 100644 --- a/examples/protocols/mqtt/ssl/main/app_main.c +++ b/examples/protocols/mqtt/ssl/main/app_main.c @@ -27,11 +27,11 @@ static const char *TAG = "MQTTS_EXAMPLE"; #if CONFIG_BROKER_CERTIFICATE_OVERRIDDEN == 1 -static const uint8_t mqtt_eclipse_org_pem_start[] = "-----BEGIN CERTIFICATE-----\n" CONFIG_BROKER_CERTIFICATE_OVERRIDE "\n-----END CERTIFICATE-----"; +static const uint8_t mqtt_eclipseprojects_io_pem_start[] = "-----BEGIN CERTIFICATE-----\n" CONFIG_BROKER_CERTIFICATE_OVERRIDE "\n-----END CERTIFICATE-----"; #else -extern const uint8_t mqtt_eclipse_org_pem_start[] asm("_binary_mqtt_eclipse_org_pem_start"); +extern const uint8_t mqtt_eclipseprojects_io_pem_start[] asm("_binary_mqtt_eclipseprojects_io_pem_start"); #endif -extern const uint8_t mqtt_eclipse_org_pem_end[] asm("_binary_mqtt_eclipse_org_pem_end"); +extern const uint8_t mqtt_eclipseprojects_io_pem_end[] asm("_binary_mqtt_eclipseprojects_io_pem_end"); // // Note: this function is for testing purposes only publishing part of the active partition @@ -124,7 +124,7 @@ static void mqtt_app_start(void) { const esp_mqtt_client_config_t mqtt_cfg = { .uri = CONFIG_BROKER_URI, - .cert_pem = (const char *)mqtt_eclipse_org_pem_start, + .cert_pem = (const char *)mqtt_eclipseprojects_io_pem_start, }; ESP_LOGI(TAG, "[APP] Free memory: %d bytes", esp_get_free_heap_size()); diff --git a/examples/protocols/mqtt/ssl/main/component.mk b/examples/protocols/mqtt/ssl/main/component.mk index 597752f..c0c0f87 100644 --- a/examples/protocols/mqtt/ssl/main/component.mk +++ b/examples/protocols/mqtt/ssl/main/component.mk @@ -1 +1 @@ -COMPONENT_EMBED_TXTFILES := mqtt_eclipse_org.pem +COMPONENT_EMBED_TXTFILES := mqtt_eclipseprojects_io.pem diff --git a/examples/protocols/mqtt/ssl/main/mqtt_eclipse_org.pem b/examples/protocols/mqtt/ssl/main/mqtt_eclipse_org.pem deleted file mode 100644 index 0002462..0000000 --- a/examples/protocols/mqtt/ssl/main/mqtt_eclipse_org.pem +++ /dev/null @@ -1,27 +0,0 @@ ------BEGIN CERTIFICATE----- -MIIEkjCCA3qgAwIBAgIQCgFBQgAAAVOFc2oLheynCDANBgkqhkiG9w0BAQsFADA/ -MSQwIgYDVQQKExtEaWdpdGFsIFNpZ25hdHVyZSBUcnVzdCBDby4xFzAVBgNVBAMT -DkRTVCBSb290IENBIFgzMB4XDTE2MDMxNzE2NDA0NloXDTIxMDMxNzE2NDA0Nlow -SjELMAkGA1UEBhMCVVMxFjAUBgNVBAoTDUxldCdzIEVuY3J5cHQxIzAhBgNVBAMT -GkxldCdzIEVuY3J5cHQgQXV0aG9yaXR5IFgzMIIBIjANBgkqhkiG9w0BAQEFAAOC -AQ8AMIIBCgKCAQEAnNMM8FrlLke3cl03g7NoYzDq1zUmGSXhvb418XCSL7e4S0EF -q6meNQhY7LEqxGiHC6PjdeTm86dicbp5gWAf15Gan/PQeGdxyGkOlZHP/uaZ6WA8 -SMx+yk13EiSdRxta67nsHjcAHJyse6cF6s5K671B5TaYucv9bTyWaN8jKkKQDIZ0 -Z8h/pZq4UmEUEz9l6YKHy9v6Dlb2honzhT+Xhq+w3Brvaw2VFn3EK6BlspkENnWA -a6xK8xuQSXgvopZPKiAlKQTGdMDQMc2PMTiVFrqoM7hD8bEfwzB/onkxEz0tNvjj -/PIzark5McWvxI0NHWQWM6r6hCm21AvA2H3DkwIDAQABo4IBfTCCAXkwEgYDVR0T -AQH/BAgwBgEB/wIBADAOBgNVHQ8BAf8EBAMCAYYwfwYIKwYBBQUHAQEEczBxMDIG -CCsGAQUFBzABhiZodHRwOi8vaXNyZy50cnVzdGlkLm9jc3AuaWRlbnRydXN0LmNv -bTA7BggrBgEFBQcwAoYvaHR0cDovL2FwcHMuaWRlbnRydXN0LmNvbS9yb290cy9k -c3Ryb290Y2F4My5wN2MwHwYDVR0jBBgwFoAUxKexpHsscfrb4UuQdf/EFWCFiRAw -VAYDVR0gBE0wSzAIBgZngQwBAgEwPwYLKwYBBAGC3xMBAQEwMDAuBggrBgEFBQcC -ARYiaHR0cDovL2Nwcy5yb290LXgxLmxldHNlbmNyeXB0Lm9yZzA8BgNVHR8ENTAz -MDGgL6AthitodHRwOi8vY3JsLmlkZW50cnVzdC5jb20vRFNUUk9PVENBWDNDUkwu -Y3JsMB0GA1UdDgQWBBSoSmpjBH3duubRObemRWXv86jsoTANBgkqhkiG9w0BAQsF -AAOCAQEA3TPXEfNjWDjdGBX7CVW+dla5cEilaUcne8IkCJLxWh9KEik3JHRRHGJo -uM2VcGfl96S8TihRzZvoroed6ti6WqEBmtzw3Wodatg+VyOeph4EYpr/1wXKtx8/ -wApIvJSwtmVi4MFU5aMqrSDE6ea73Mj2tcMyo5jMd6jmeWUHK8so/joWUoHOUgwu -X4Po1QYz+3dszkDqMp4fklxBwXRsW10KXzPMTZ+sOPAveyxindmjkW8lGy+QsRlG -PfZ+G6Z6h7mjem0Y+iWlkYcV4PIWL1iwBi8saCbGS5jN2p8M+X+Q7UNKEkROb3N6 -KOqkqm57TH2H3eDJAkSnh6/DNFu0Qg== ------END CERTIFICATE----- diff --git a/examples/protocols/mqtt/ssl/main/mqtt_eclipseprojects_io.pem b/examples/protocols/mqtt/ssl/main/mqtt_eclipseprojects_io.pem new file mode 100644 index 0000000..43b222a --- /dev/null +++ b/examples/protocols/mqtt/ssl/main/mqtt_eclipseprojects_io.pem @@ -0,0 +1,30 @@ +-----BEGIN CERTIFICATE----- +MIIFFjCCAv6gAwIBAgIRAJErCErPDBinU/bWLiWnX1owDQYJKoZIhvcNAQELBQAw +TzELMAkGA1UEBhMCVVMxKTAnBgNVBAoTIEludGVybmV0IFNlY3VyaXR5IFJlc2Vh +cmNoIEdyb3VwMRUwEwYDVQQDEwxJU1JHIFJvb3QgWDEwHhcNMjAwOTA0MDAwMDAw +WhcNMjUwOTE1MTYwMDAwWjAyMQswCQYDVQQGEwJVUzEWMBQGA1UEChMNTGV0J3Mg +RW5jcnlwdDELMAkGA1UEAxMCUjMwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEK +AoIBAQC7AhUozPaglNMPEuyNVZLD+ILxmaZ6QoinXSaqtSu5xUyxr45r+XXIo9cP +R5QUVTVXjJ6oojkZ9YI8QqlObvU7wy7bjcCwXPNZOOftz2nwWgsbvsCUJCWH+jdx +sxPnHKzhm+/b5DtFUkWWqcFTzjTIUu61ru2P3mBw4qVUq7ZtDpelQDRrK9O8Zutm +NHz6a4uPVymZ+DAXXbpyb/uBxa3Shlg9F8fnCbvxK/eG3MHacV3URuPMrSXBiLxg +Z3Vms/EY96Jc5lP/Ooi2R6X/ExjqmAl3P51T+c8B5fWmcBcUr2Ok/5mzk53cU6cG +/kiFHaFpriV1uxPMUgP17VGhi9sVAgMBAAGjggEIMIIBBDAOBgNVHQ8BAf8EBAMC +AYYwHQYDVR0lBBYwFAYIKwYBBQUHAwIGCCsGAQUFBwMBMBIGA1UdEwEB/wQIMAYB +Af8CAQAwHQYDVR0OBBYEFBQusxe3WFbLrlAJQOYfr52LFMLGMB8GA1UdIwQYMBaA +FHm0WeZ7tuXkAXOACIjIGlj26ZtuMDIGCCsGAQUFBwEBBCYwJDAiBggrBgEFBQcw +AoYWaHR0cDovL3gxLmkubGVuY3Iub3JnLzAnBgNVHR8EIDAeMBygGqAYhhZodHRw +Oi8veDEuYy5sZW5jci5vcmcvMCIGA1UdIAQbMBkwCAYGZ4EMAQIBMA0GCysGAQQB +gt8TAQEBMA0GCSqGSIb3DQEBCwUAA4ICAQCFyk5HPqP3hUSFvNVneLKYY611TR6W +PTNlclQtgaDqw+34IL9fzLdwALduO/ZelN7kIJ+m74uyA+eitRY8kc607TkC53wl +ikfmZW4/RvTZ8M6UK+5UzhK8jCdLuMGYL6KvzXGRSgi3yLgjewQtCPkIVz6D2QQz +CkcheAmCJ8MqyJu5zlzyZMjAvnnAT45tRAxekrsu94sQ4egdRCnbWSDtY7kh+BIm +lJNXoB1lBMEKIq4QDUOXoRgffuDghje1WrG9ML+Hbisq/yFOGwXD9RiX8F6sw6W4 +avAuvDszue5L3sz85K+EC4Y/wFVDNvZo4TYXao6Z0f+lQKc0t8DQYzk1OXVu8rp2 +yJMC6alLbBfODALZvYH7n7do1AZls4I9d1P4jnkDrQoxB3UqQ9hVl3LEKQ73xF1O +yK5GhDDX8oVfGKF5u+decIsH4YaTw7mP3GFxJSqv3+0lUFJoi5Lc5da149p90Ids +hCExroL1+7mryIkXPeFM5TgO9r0rvZaBFOvV2z0gp35Z0+L4WPlbuEjN/lxPFin+ +HlUjr8gRsI3qfJOQFy/9rKIJR0Y/8Omwt/8oTWgy1mdeHmmjk7j1nYsvC9JSQ6Zv +MldlTTKB3zhThV1+XWYp6rjd5JW1zbVWEkLNxE7GJThEUG3szgBVGP7pSWTUTsqX +nLRbwHOoq7hHwg== +-----END CERTIFICATE----- diff --git a/examples/protocols/mqtt/wss/CMakeLists.txt b/examples/protocols/mqtt/wss/CMakeLists.txt index 2350bd8..2534732 100644 --- a/examples/protocols/mqtt/wss/CMakeLists.txt +++ b/examples/protocols/mqtt/wss/CMakeLists.txt @@ -9,4 +9,4 @@ set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_exam include($ENV{IDF_PATH}/tools/cmake/project.cmake) project(mqtt_websocket_secure) -target_add_binary_data(mqtt_websocket_secure.elf "main/mqtt_eclipse_org.pem" TEXT) +target_add_binary_data(mqtt_websocket_secure.elf "main/mqtt_eclipseprojects_io.pem" TEXT) diff --git a/examples/protocols/mqtt/wss/main/app_main.c b/examples/protocols/mqtt/wss/main/app_main.c index d0495e2..63cb688 100644 --- a/examples/protocols/mqtt/wss/main/app_main.c +++ b/examples/protocols/mqtt/wss/main/app_main.c @@ -33,11 +33,11 @@ static const char *TAG = "MQTTWSS_EXAMPLE"; #if CONFIG_BROKER_CERTIFICATE_OVERRIDDEN == 1 -static const uint8_t mqtt_eclipse_org_pem_start[] = "-----BEGIN CERTIFICATE-----\n" CONFIG_BROKER_CERTIFICATE_OVERRIDE "\n-----END CERTIFICATE-----"; +static const uint8_t mqtt_eclipseprojects_io_pem_start[] = "-----BEGIN CERTIFICATE-----\n" CONFIG_BROKER_CERTIFICATE_OVERRIDE "\n-----END CERTIFICATE-----"; #else -extern const uint8_t mqtt_eclipse_org_pem_start[] asm("_binary_mqtt_eclipse_org_pem_start"); +extern const uint8_t mqtt_eclipseprojects_io_pem_start[] asm("_binary_mqtt_eclipseprojects_io_pem_start"); #endif -extern const uint8_t mqtt_eclipse_org_pem_end[] asm("_binary_mqtt_eclipse_org_pem_end"); +extern const uint8_t mqtt_eclipseprojects_io_pem_end[] asm("_binary_mqtt_eclipseprojects_io_pem_end"); static esp_err_t mqtt_event_handler_cb(esp_mqtt_event_handle_t event) { @@ -97,7 +97,7 @@ static void mqtt_app_start(void) { const esp_mqtt_client_config_t mqtt_cfg = { .uri = CONFIG_BROKER_URI, - .cert_pem = (const char *)mqtt_eclipse_org_pem_start, + .cert_pem = (const char *)mqtt_eclipseprojects_io_pem_start, }; ESP_LOGI(TAG, "[APP] Free memory: %d bytes", esp_get_free_heap_size()); diff --git a/examples/protocols/mqtt/wss/main/component.mk b/examples/protocols/mqtt/wss/main/component.mk index 597752f..c0c0f87 100644 --- a/examples/protocols/mqtt/wss/main/component.mk +++ b/examples/protocols/mqtt/wss/main/component.mk @@ -1 +1 @@ -COMPONENT_EMBED_TXTFILES := mqtt_eclipse_org.pem +COMPONENT_EMBED_TXTFILES := mqtt_eclipseprojects_io.pem diff --git a/examples/protocols/mqtt/wss/main/mqtt_eclipse_org.pem b/examples/protocols/mqtt/wss/main/mqtt_eclipse_org.pem deleted file mode 100644 index 0002462..0000000 --- a/examples/protocols/mqtt/wss/main/mqtt_eclipse_org.pem +++ /dev/null @@ -1,27 +0,0 @@ ------BEGIN CERTIFICATE----- -MIIEkjCCA3qgAwIBAgIQCgFBQgAAAVOFc2oLheynCDANBgkqhkiG9w0BAQsFADA/ -MSQwIgYDVQQKExtEaWdpdGFsIFNpZ25hdHVyZSBUcnVzdCBDby4xFzAVBgNVBAMT -DkRTVCBSb290IENBIFgzMB4XDTE2MDMxNzE2NDA0NloXDTIxMDMxNzE2NDA0Nlow -SjELMAkGA1UEBhMCVVMxFjAUBgNVBAoTDUxldCdzIEVuY3J5cHQxIzAhBgNVBAMT -GkxldCdzIEVuY3J5cHQgQXV0aG9yaXR5IFgzMIIBIjANBgkqhkiG9w0BAQEFAAOC -AQ8AMIIBCgKCAQEAnNMM8FrlLke3cl03g7NoYzDq1zUmGSXhvb418XCSL7e4S0EF -q6meNQhY7LEqxGiHC6PjdeTm86dicbp5gWAf15Gan/PQeGdxyGkOlZHP/uaZ6WA8 -SMx+yk13EiSdRxta67nsHjcAHJyse6cF6s5K671B5TaYucv9bTyWaN8jKkKQDIZ0 -Z8h/pZq4UmEUEz9l6YKHy9v6Dlb2honzhT+Xhq+w3Brvaw2VFn3EK6BlspkENnWA -a6xK8xuQSXgvopZPKiAlKQTGdMDQMc2PMTiVFrqoM7hD8bEfwzB/onkxEz0tNvjj -/PIzark5McWvxI0NHWQWM6r6hCm21AvA2H3DkwIDAQABo4IBfTCCAXkwEgYDVR0T -AQH/BAgwBgEB/wIBADAOBgNVHQ8BAf8EBAMCAYYwfwYIKwYBBQUHAQEEczBxMDIG -CCsGAQUFBzABhiZodHRwOi8vaXNyZy50cnVzdGlkLm9jc3AuaWRlbnRydXN0LmNv -bTA7BggrBgEFBQcwAoYvaHR0cDovL2FwcHMuaWRlbnRydXN0LmNvbS9yb290cy9k -c3Ryb290Y2F4My5wN2MwHwYDVR0jBBgwFoAUxKexpHsscfrb4UuQdf/EFWCFiRAw -VAYDVR0gBE0wSzAIBgZngQwBAgEwPwYLKwYBBAGC3xMBAQEwMDAuBggrBgEFBQcC -ARYiaHR0cDovL2Nwcy5yb290LXgxLmxldHNlbmNyeXB0Lm9yZzA8BgNVHR8ENTAz -MDGgL6AthitodHRwOi8vY3JsLmlkZW50cnVzdC5jb20vRFNUUk9PVENBWDNDUkwu -Y3JsMB0GA1UdDgQWBBSoSmpjBH3duubRObemRWXv86jsoTANBgkqhkiG9w0BAQsF -AAOCAQEA3TPXEfNjWDjdGBX7CVW+dla5cEilaUcne8IkCJLxWh9KEik3JHRRHGJo -uM2VcGfl96S8TihRzZvoroed6ti6WqEBmtzw3Wodatg+VyOeph4EYpr/1wXKtx8/ -wApIvJSwtmVi4MFU5aMqrSDE6ea73Mj2tcMyo5jMd6jmeWUHK8so/joWUoHOUgwu -X4Po1QYz+3dszkDqMp4fklxBwXRsW10KXzPMTZ+sOPAveyxindmjkW8lGy+QsRlG -PfZ+G6Z6h7mjem0Y+iWlkYcV4PIWL1iwBi8saCbGS5jN2p8M+X+Q7UNKEkROb3N6 -KOqkqm57TH2H3eDJAkSnh6/DNFu0Qg== ------END CERTIFICATE----- diff --git a/examples/protocols/mqtt/wss/main/mqtt_eclipseprojects_io.pem b/examples/protocols/mqtt/wss/main/mqtt_eclipseprojects_io.pem new file mode 100644 index 0000000..43b222a --- /dev/null +++ b/examples/protocols/mqtt/wss/main/mqtt_eclipseprojects_io.pem @@ -0,0 +1,30 @@ +-----BEGIN CERTIFICATE----- +MIIFFjCCAv6gAwIBAgIRAJErCErPDBinU/bWLiWnX1owDQYJKoZIhvcNAQELBQAw +TzELMAkGA1UEBhMCVVMxKTAnBgNVBAoTIEludGVybmV0IFNlY3VyaXR5IFJlc2Vh +cmNoIEdyb3VwMRUwEwYDVQQDEwxJU1JHIFJvb3QgWDEwHhcNMjAwOTA0MDAwMDAw +WhcNMjUwOTE1MTYwMDAwWjAyMQswCQYDVQQGEwJVUzEWMBQGA1UEChMNTGV0J3Mg +RW5jcnlwdDELMAkGA1UEAxMCUjMwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEK +AoIBAQC7AhUozPaglNMPEuyNVZLD+ILxmaZ6QoinXSaqtSu5xUyxr45r+XXIo9cP +R5QUVTVXjJ6oojkZ9YI8QqlObvU7wy7bjcCwXPNZOOftz2nwWgsbvsCUJCWH+jdx +sxPnHKzhm+/b5DtFUkWWqcFTzjTIUu61ru2P3mBw4qVUq7ZtDpelQDRrK9O8Zutm +NHz6a4uPVymZ+DAXXbpyb/uBxa3Shlg9F8fnCbvxK/eG3MHacV3URuPMrSXBiLxg +Z3Vms/EY96Jc5lP/Ooi2R6X/ExjqmAl3P51T+c8B5fWmcBcUr2Ok/5mzk53cU6cG +/kiFHaFpriV1uxPMUgP17VGhi9sVAgMBAAGjggEIMIIBBDAOBgNVHQ8BAf8EBAMC +AYYwHQYDVR0lBBYwFAYIKwYBBQUHAwIGCCsGAQUFBwMBMBIGA1UdEwEB/wQIMAYB +Af8CAQAwHQYDVR0OBBYEFBQusxe3WFbLrlAJQOYfr52LFMLGMB8GA1UdIwQYMBaA +FHm0WeZ7tuXkAXOACIjIGlj26ZtuMDIGCCsGAQUFBwEBBCYwJDAiBggrBgEFBQcw +AoYWaHR0cDovL3gxLmkubGVuY3Iub3JnLzAnBgNVHR8EIDAeMBygGqAYhhZodHRw +Oi8veDEuYy5sZW5jci5vcmcvMCIGA1UdIAQbMBkwCAYGZ4EMAQIBMA0GCysGAQQB +gt8TAQEBMA0GCSqGSIb3DQEBCwUAA4ICAQCFyk5HPqP3hUSFvNVneLKYY611TR6W +PTNlclQtgaDqw+34IL9fzLdwALduO/ZelN7kIJ+m74uyA+eitRY8kc607TkC53wl +ikfmZW4/RvTZ8M6UK+5UzhK8jCdLuMGYL6KvzXGRSgi3yLgjewQtCPkIVz6D2QQz +CkcheAmCJ8MqyJu5zlzyZMjAvnnAT45tRAxekrsu94sQ4egdRCnbWSDtY7kh+BIm +lJNXoB1lBMEKIq4QDUOXoRgffuDghje1WrG9ML+Hbisq/yFOGwXD9RiX8F6sw6W4 +avAuvDszue5L3sz85K+EC4Y/wFVDNvZo4TYXao6Z0f+lQKc0t8DQYzk1OXVu8rp2 +yJMC6alLbBfODALZvYH7n7do1AZls4I9d1P4jnkDrQoxB3UqQ9hVl3LEKQ73xF1O +yK5GhDDX8oVfGKF5u+decIsH4YaTw7mP3GFxJSqv3+0lUFJoi5Lc5da149p90Ids +hCExroL1+7mryIkXPeFM5TgO9r0rvZaBFOvV2z0gp35Z0+L4WPlbuEjN/lxPFin+ +HlUjr8gRsI3qfJOQFy/9rKIJR0Y/8Omwt/8oTWgy1mdeHmmjk7j1nYsvC9JSQ6Zv +MldlTTKB3zhThV1+XWYp6rjd5JW1zbVWEkLNxE7GJThEUG3szgBVGP7pSWTUTsqX +nLRbwHOoq7hHwg== +-----END CERTIFICATE----- From 17ae8434f434268087dac38e9cbc54f4d3eafb03 Mon Sep 17 00:00:00 2001 From: Shubham Kulkarni Date: Thu, 25 Mar 2021 15:20:30 +0530 Subject: [PATCH 087/231] Split example_tests with Example_WIFI tag group into Example_OTA and Example_Protocols --- examples/protocols/mqtt/ssl/mqtt_ssl_example_test.py | 2 +- examples/protocols/mqtt/tcp/mqtt_tcp_example_test.py | 2 +- examples/protocols/mqtt/ws/mqtt_ws_example_test.py | 2 +- examples/protocols/mqtt/wss/mqtt_wss_example_test.py | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/examples/protocols/mqtt/ssl/mqtt_ssl_example_test.py b/examples/protocols/mqtt/ssl/mqtt_ssl_example_test.py index 34bd2c7..cecb9a9 100644 --- a/examples/protocols/mqtt/ssl/mqtt_ssl_example_test.py +++ b/examples/protocols/mqtt/ssl/mqtt_ssl_example_test.py @@ -58,7 +58,7 @@ def on_message(client, userdata, msg): message_log += 'Received data:' + msg.topic + ' ' + payload + '\n' -@ttfw_idf.idf_example_test(env_tag='Example_WIFI') +@ttfw_idf.idf_example_test(env_tag='Example_WIFI_Protocols') def test_examples_protocol_mqtt_ssl(env, extra_data): broker_url = '' broker_port = 0 diff --git a/examples/protocols/mqtt/tcp/mqtt_tcp_example_test.py b/examples/protocols/mqtt/tcp/mqtt_tcp_example_test.py index 7acb1dc..47e156b 100644 --- a/examples/protocols/mqtt/tcp/mqtt_tcp_example_test.py +++ b/examples/protocols/mqtt/tcp/mqtt_tcp_example_test.py @@ -53,7 +53,7 @@ def mqqt_server_sketch(my_ip, port): print('server closed') -@ttfw_idf.idf_example_test(env_tag='Example_WIFI') +@ttfw_idf.idf_example_test(env_tag='Example_WIFI_Protocols') def test_examples_protocol_mqtt_qos1(env, extra_data): global msgid """ diff --git a/examples/protocols/mqtt/ws/mqtt_ws_example_test.py b/examples/protocols/mqtt/ws/mqtt_ws_example_test.py index 69c5dc9..74f3fc8 100644 --- a/examples/protocols/mqtt/ws/mqtt_ws_example_test.py +++ b/examples/protocols/mqtt/ws/mqtt_ws_example_test.py @@ -39,7 +39,7 @@ def on_message(client, userdata, msg): message_log += 'Received data:' + msg.topic + ' ' + payload + '\n' -@ttfw_idf.idf_example_test(env_tag='Example_WIFI') +@ttfw_idf.idf_example_test(env_tag='Example_WIFI_Protocols') def test_examples_protocol_mqtt_ws(env, extra_data): broker_url = '' broker_port = 0 diff --git a/examples/protocols/mqtt/wss/mqtt_wss_example_test.py b/examples/protocols/mqtt/wss/mqtt_wss_example_test.py index 5e2fa8a..0b1fbf5 100644 --- a/examples/protocols/mqtt/wss/mqtt_wss_example_test.py +++ b/examples/protocols/mqtt/wss/mqtt_wss_example_test.py @@ -40,7 +40,7 @@ def on_message(client, userdata, msg): message_log += 'Received data:' + msg.topic + ' ' + payload + '\n' -@ttfw_idf.idf_example_test(env_tag='Example_WIFI') +@ttfw_idf.idf_example_test(env_tag='Example_WIFI_Protocols') def test_examples_protocol_mqtt_wss(env, extra_data): broker_url = '' broker_port = 0 From 9bc1e1a0112252ed2101a71cdf210b04e1c8d50d Mon Sep 17 00:00:00 2001 From: David Cermak Date: Thu, 10 Jun 2021 09:09:14 +0200 Subject: [PATCH 088/231] mqtt: Moved weekend tests to test apps --- .../mqtt/weekend_test/mqtt_publish_test.py | 178 ------------------ .../mqtt/weekend_test/test_weekend_mqtt_.yml | 2 - .../mqtt/publish_connect_test}/env.yml | 0 .../publish_connect_mqtt_.yml | 2 + .../publish_connect_mqtt_qemu.yml | 2 +- 5 files changed, 3 insertions(+), 181 deletions(-) delete mode 100644 components/mqtt/weekend_test/mqtt_publish_test.py delete mode 100644 components/mqtt/weekend_test/test_weekend_mqtt_.yml rename {components/mqtt/weekend_test => tools/test_apps/protocols/mqtt/publish_connect_test}/env.yml (100%) create mode 100644 tools/test_apps/protocols/mqtt/publish_connect_test/publish_connect_mqtt_.yml rename components/mqtt/weekend_test/test_weekend_mqtt_qemu.yml => tools/test_apps/protocols/mqtt/publish_connect_test/publish_connect_mqtt_qemu.yml (64%) 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/env.yml b/tools/test_apps/protocols/mqtt/publish_connect_test/env.yml similarity index 100% rename from components/mqtt/weekend_test/env.yml rename to tools/test_apps/protocols/mqtt/publish_connect_test/env.yml diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/publish_connect_mqtt_.yml b/tools/test_apps/protocols/mqtt/publish_connect_test/publish_connect_mqtt_.yml new file mode 100644 index 0000000..70e13ef --- /dev/null +++ b/tools/test_apps/protocols/mqtt/publish_connect_test/publish_connect_mqtt_.yml @@ -0,0 +1,2 @@ +CaseConfig: +- name: test_app_protocol_mqtt_publish_connect diff --git a/components/mqtt/weekend_test/test_weekend_mqtt_qemu.yml b/tools/test_apps/protocols/mqtt/publish_connect_test/publish_connect_mqtt_qemu.yml similarity index 64% rename from components/mqtt/weekend_test/test_weekend_mqtt_qemu.yml rename to tools/test_apps/protocols/mqtt/publish_connect_test/publish_connect_mqtt_qemu.yml index bd6f0d6..06f1235 100644 --- a/components/mqtt/weekend_test/test_weekend_mqtt_qemu.yml +++ b/tools/test_apps/protocols/mqtt/publish_connect_test/publish_connect_mqtt_qemu.yml @@ -1,5 +1,5 @@ CaseConfig: -- name: test_weekend_mqtt_publish +- name: test_app_protocol_mqtt_publish_connect overwrite: dut: class: ESP32QEMUDUT From f962ac035fedd1b2e2f2ed1970f632477ef03c50 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Thu, 10 Jun 2021 16:46:12 +0200 Subject: [PATCH 089/231] CI: Enable publish tests only when started from weekend pipeline --- .../protocols/mqtt/publish_connect_test/app_test.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/app_test.py b/tools/test_apps/protocols/mqtt/publish_connect_test/app_test.py index 2cb19f5..fb53747 100644 --- a/tools/test_apps/protocols/mqtt/publish_connect_test/app_test.py +++ b/tools/test_apps/protocols/mqtt/publish_connect_test/app_test.py @@ -352,7 +352,10 @@ def test_app_protocol_mqtt_publish_connect(env, extra_data): raise Exception('Unexpected negotiated protocol {}'.format(s.get_negotiated_protocol())) # - # start publish tests + # start publish tests only if enabled in the environment (for weekend tests only) + if not os.getenv('MQTT_PUBLISH_TEST'): + return + def start_publish_case(transport, qos, repeat, published, queue): print('Starting Publish test: transport:{}, qos:{}, nr_of_msgs:{}, msg_size:{}, enqueue:{}' .format(transport, qos, published, repeat * DEFAULT_MSG_SIZE, queue)) From 181e90fe5a2c828658635618e3993adf0fa8441a Mon Sep 17 00:00:00 2001 From: David Cermak Date: Wed, 16 Jun 2021 09:58:20 +0200 Subject: [PATCH 090/231] CI: Prepare mqtt app test for QEMU tests Adds qemu configuration Generalize the get_dut() to enable choosing DUT class per configuration --- .../test_apps/protocols/mqtt/publish_connect_test/app_test.py | 2 +- .../protocols/mqtt/publish_connect_test/sdkconfig.qemu | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/app_test.py b/tools/test_apps/protocols/mqtt/publish_connect_test/app_test.py index fb53747..8f0bd95 100644 --- a/tools/test_apps/protocols/mqtt/publish_connect_test/app_test.py +++ b/tools/test_apps/protocols/mqtt/publish_connect_test/app_test.py @@ -247,7 +247,7 @@ def test_app_protocol_mqtt_publish_connect(env, extra_data): 2. connect to uri specified in the config 3. send and receive data """ - dut1 = env.get_dut('mqtt_publish_connect_test', 'tools/test_apps/protocols/mqtt/publish_connect_test', dut_class=ttfw_idf.ESP32DUT) + dut1 = env.get_dut('mqtt_publish_connect_test', 'tools/test_apps/protocols/mqtt/publish_connect_test') # check and log bin size binary_file = os.path.join(dut1.app.binary_path, 'mqtt_publish_connect_test.bin') bin_size = os.path.getsize(binary_file) diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/sdkconfig.qemu b/tools/test_apps/protocols/mqtt/publish_connect_test/sdkconfig.qemu index e0d289d..62d7f5b 100644 --- a/tools/test_apps/protocols/mqtt/publish_connect_test/sdkconfig.qemu +++ b/tools/test_apps/protocols/mqtt/publish_connect_test/sdkconfig.qemu @@ -17,3 +17,7 @@ CONFIG_EXAMPLE_BROKER_CERTIFICATE_OVERRIDE="${EXAMPLE_MQTT_BROKER_CERTIFICATE}" CONFIG_MBEDTLS_HARDWARE_AES=n CONFIG_MBEDTLS_HARDWARE_MPI=n CONFIG_MBEDTLS_HARDWARE_SHA=n +CONFIG_ETH_USE_SPI_ETHERNET=n +CONFIG_EXAMPLE_CONNECT_WIFI=n +CONFIG_ESP_TLS_INSECURE=y +CONFIG_ESP_TLS_SKIP_SERVER_CERT_VERIFY=y From 531329d262e1ccf245f744a8fdc9c21ac4f432e4 Mon Sep 17 00:00:00 2001 From: liuhan Date: Fri, 16 Apr 2021 17:23:18 +0800 Subject: [PATCH 091/231] 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 5cf3dad79a6675ff11e55074d04604871f43649f Mon Sep 17 00:00:00 2001 From: Euripedes Rocha Filho Date: Tue, 16 Feb 2021 15:47:10 +0000 Subject: [PATCH 092/231] mqtt: Adds host tests for mqtt client --- 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 + 10 files changed, 574 insertions(+) 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/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 43cca8e25cf9f1d47d4586d60fec6b5443b5a18f Mon Sep 17 00:00:00 2001 From: David Cermak Date: Mon, 26 Jul 2021 09:53:26 +0200 Subject: [PATCH 093/231] CI: mqtt test apps: removed unnecessary env.yml --- tools/test_apps/protocols/mqtt/publish_connect_test/env.yml | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 tools/test_apps/protocols/mqtt/publish_connect_test/env.yml diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/env.yml b/tools/test_apps/protocols/mqtt/publish_connect_test/env.yml deleted file mode 100644 index e69de29..0000000 From 0d4f84fe7a86dc7c9a40ed67952cf1eb20626b4f Mon Sep 17 00:00:00 2001 From: Zim Kalinowski Date: Tue, 24 Aug 2021 12:06:21 +0800 Subject: [PATCH 094/231] 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 380f031a98ed9ef24d0be44e2ee648b9e4e541d5 Mon Sep 17 00:00:00 2001 From: Sachin Billore Date: Fri, 27 Aug 2021 21:28:02 +0530 Subject: [PATCH 095/231] ESP32S3 support for ESP-MQTT SSL Mutual Authentication with Digital Signature Closes IDF-3859 --- examples/protocols/mqtt/ssl_ds/README.md | 10 +++++----- examples/protocols/mqtt/ssl_ds/configure_ds.py | 6 +++--- examples/protocols/mqtt/ssl_ds/main/CMakeLists.txt | 2 +- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/examples/protocols/mqtt/ssl_ds/README.md b/examples/protocols/mqtt/ssl_ds/README.md index 20a06d0..b186469 100644 --- a/examples/protocols/mqtt/ssl_ds/README.md +++ b/examples/protocols/mqtt/ssl_ds/README.md @@ -1,8 +1,8 @@ -| Supported Targets | ESP32-S2 | ESP32-C3 | +| Supported Targets | ESP32-S2 | ESP32-C3 | ESP32-S3 | # ESP-MQTT SSL Mutual Authentication with Digital Signature (See the README.md file in the upper level 'examples' directory for more information about examples.) -Espressif's ESP32-S2 and ESP32-C3 MCU have a built-in Digital Signature (DS) Peripheral, which provides hardware acceleration for RSA signature. More details can be found at [Digital Signature with ESP-TLS](https://docs.espressif.com/projects/esp-idf/en/latest/esp32s2/api-reference/protocols/esp_tls.html#digital-signature-with-esp-tls). +Espressif's ESP32-S2, ESP32-S3 and ESP32-C3 MCU have a built-in Digital Signature (DS) Peripheral, which provides hardware acceleration for RSA signature. More details can be found at [Digital Signature with ESP-TLS](https://docs.espressif.com/projects/esp-idf/en/latest/esp32s2/api-reference/protocols/esp_tls.html#digital-signature-with-esp-tls). This example connects to the broker test.mosquitto.org using ssl transport with client certificate(RSA) and as a demonstration subscribes/unsubscribes and sends a message on certain topic.The RSA signature operation required in the ssl connection is performed with help of the Digital Signature (DS) peripheral. (Please note that the public broker is maintained by the community so may not be always available, for details please visit http://test.mosquitto.org) @@ -12,12 +12,12 @@ It uses ESP-MQTT library which implements mqtt client to connect to mqtt broker. ### Hardware Required -This example can be executed on any ESP32-S2, ESP32-C3 board (which has a built-in DS peripheral), the only required interface is WiFi and connection to internet. +This example can be executed on any ESP32-S2, ESP32-S3, ESP32-C3 board (which has a built-in DS peripheral), the only required interface is WiFi and connection to internet. ### Configure the project #### 1) Selecting the target -As the project is to be built for the target ESP32-S2, ESP32-C3 it should be selected with the following command +As the project is to be built for the target ESP32-S2, ESP32-S3, ESP32-C3 it should be selected with the following command ``` idf.py set-target /* target */ ``` @@ -99,7 +99,7 @@ DATA=data ### configure_ds.py -The script [configure_ds.py](./configure_ds.py) is used for configuring the DS peripheral on the ESP32-S2/ESP32-C3 SoC. The steps in the script are based on technical details of certain operations in the Digital Signature calculation, which can be found at Digital Signature Section of [ESP32-S2 TRM](https://www.espressif.com/sites/default/files/documentation/esp32-s2_technical_reference_manual_en.pdf) +The script [configure_ds.py](./configure_ds.py) is used for configuring the DS peripheral on the ESP32-S2/ESP32-S3/ESP32-C3 SoC. The steps in the script are based on technical details of certain operations in the Digital Signature calculation, which can be found at Digital Signature Section of [ESP32-S2 TRM](https://www.espressif.com/sites/default/files/documentation/esp32-s2_technical_reference_manual_en.pdf) The configuration script performs the following steps - diff --git a/examples/protocols/mqtt/ssl_ds/configure_ds.py b/examples/protocols/mqtt/ssl_ds/configure_ds.py index b718052..44f2c9e 100644 --- a/examples/protocols/mqtt/ssl_ds/configure_ds.py +++ b/examples/protocols/mqtt/ssl_ds/configure_ds.py @@ -46,8 +46,8 @@ csv_filename = esp_ds_data_dir + '/pre_prov.csv' bin_filename = esp_ds_data_dir + '/pre_prov.bin' expected_json_path = os.path.join('build', 'config', 'sdkconfig.json') # Targets supported by the script -supported_targets = {'esp32s2', 'esp32c3'} -supported_key_size = {'esp32s2':[1024, 2048, 3072, 4096], 'esp32c3':[1024, 2048, 3072]} +supported_targets = {'esp32s2', 'esp32c3', 'esp32s3'} +supported_key_size = {'esp32s2':[1024, 2048, 3072, 4096], 'esp32c3':[1024, 2048, 3072], 'esp32s3':[1024, 2048, 3072, 4096]} # @return @@ -89,7 +89,7 @@ def number_as_bytes(number, pad_bits=None): # privkey : path to the RSA private key # priv_key_pass : path to the RSA privaete key password # hmac_key : HMAC key value ( to calculate DS params) -# idf_target : The target chip for the script (e.g. esp32s2, esp32c3) +# idf_target : The target chip for the script (e.g. esp32s2, esp32c3, esp32s3) # @info # The function calculates the encrypted private key parameters. # Consult the DS documentation (available for the ESP32-S2) in the esp-idf programming guide for more details about the variables and calculations. diff --git a/examples/protocols/mqtt/ssl_ds/main/CMakeLists.txt b/examples/protocols/mqtt/ssl_ds/main/CMakeLists.txt index d1e3ea1..b8ef6ea 100644 --- a/examples/protocols/mqtt/ssl_ds/main/CMakeLists.txt +++ b/examples/protocols/mqtt/ssl_ds/main/CMakeLists.txt @@ -1,3 +1,3 @@ idf_component_register(SRCS "app_main.c" INCLUDE_DIRS "." - REQUIRED_IDF_TARGETS esp32s2 esp32c3) + REQUIRED_IDF_TARGETS esp32s2 esp32c3 esp32s3) From 699dd4fbce383fd1b139174530747b7d9d5876e9 Mon Sep 17 00:00:00 2001 From: Euripedes Rocha Date: Tue, 20 Jul 2021 11:11:54 -0300 Subject: [PATCH 096/231] [MQTT/test] Fix publish connect test - Mqtt client will return error on wrong parameters. This fix the test code to setup the client for each test case. --- .../mqtt/publish_connect_test/CMakeLists.txt | 2 +- .../mqtt/publish_connect_test/app_test.py | 178 +++++++++--------- .../publish_connect_test/main/connect_test.c | 134 ++++++------- .../main/mqtt_eclipse_org.pem | 27 --- .../main/mqtt_eclipseprojects_io.pem | 30 +++ .../main/publish_connect_test.c | 4 +- .../publish_connect_test/main/publish_test.c | 132 ++++++++----- 7 files changed, 281 insertions(+), 226 deletions(-) delete mode 100644 tools/test_apps/protocols/mqtt/publish_connect_test/main/mqtt_eclipse_org.pem create mode 100644 tools/test_apps/protocols/mqtt/publish_connect_test/main/mqtt_eclipseprojects_io.pem diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/CMakeLists.txt b/tools/test_apps/protocols/mqtt/publish_connect_test/CMakeLists.txt index 6d1ef5a..6fcb74b 100644 --- a/tools/test_apps/protocols/mqtt/publish_connect_test/CMakeLists.txt +++ b/tools/test_apps/protocols/mqtt/publish_connect_test/CMakeLists.txt @@ -10,7 +10,7 @@ include($ENV{IDF_PATH}/tools/cmake/project.cmake) project(mqtt_publish_connect_test) -target_add_binary_data(mqtt_publish_connect_test.elf "main/mqtt_eclipse_org.pem" TEXT) +target_add_binary_data(mqtt_publish_connect_test.elf "main/mqtt_eclipseprojects_io.pem" TEXT) target_add_binary_data(mqtt_publish_connect_test.elf "ca.crt" TEXT) target_add_binary_data(mqtt_publish_connect_test.elf "ca.der" TEXT) target_add_binary_data(mqtt_publish_connect_test.elf "client_pwd.key" TEXT) diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/app_test.py b/tools/test_apps/protocols/mqtt/publish_connect_test/app_test.py index 8f0bd95..580e285 100644 --- a/tools/test_apps/protocols/mqtt/publish_connect_test/app_test.py +++ b/tools/test_apps/protocols/mqtt/publish_connect_test/app_test.py @@ -8,6 +8,7 @@ import socket import ssl import string import subprocess +import sys from threading import Event, Thread import paho.mqtt.client as mqtt @@ -239,6 +240,68 @@ class TlsServer: self.shutdown.set() +def connection_tests(dut, cases): + ip = get_my_ip() + set_server_cert_cn(ip) + server_port = 2222 + + def start_connection_case(case, desc): + print('Starting {}: {}'.format(case, desc)) + case_id = cases[case] + dut.write('conn {} {} {}'.format(ip, server_port, case_id)) + dut.expect('Test case:{} started'.format(case_id)) + return case_id + + for case in ['CONFIG_EXAMPLE_CONNECT_CASE_NO_CERT', 'CONFIG_EXAMPLE_CONNECT_CASE_SERVER_CERT', 'CONFIG_EXAMPLE_CONNECT_CASE_SERVER_DER_CERT']: + # All these cases connect to the server with no server verification or with server only verification + with TlsServer(server_port): + test_nr = start_connection_case(case, 'default server - expect to connect normally') + dut.expect('MQTT_EVENT_CONNECTED: Test={}'.format(test_nr), timeout=30) + with TlsServer(server_port, refuse_connection=True): + test_nr = start_connection_case(case, 'ssl shall connect, but mqtt sends connect refusal') + dut.expect('MQTT_EVENT_ERROR: Test={}'.format(test_nr), timeout=30) + dut.expect('MQTT ERROR: 0x5') # expecting 0x5 ... connection not authorized error + with TlsServer(server_port, client_cert=True) as s: + test_nr = start_connection_case(case, 'server with client verification - handshake error since client presents no client certificate') + dut.expect('MQTT_EVENT_ERROR: Test={}'.format(test_nr), timeout=30) + dut.expect('ESP-TLS ERROR: 0x8010') # expect ... handshake error (PEER_DID_NOT_RETURN_A_CERTIFICATE) + if 'PEER_DID_NOT_RETURN_A_CERTIFICATE' not in s.get_last_ssl_error(): + raise('Unexpected ssl error from the server {}'.format(s.get_last_ssl_error())) + + for case in ['CONFIG_EXAMPLE_CONNECT_CASE_MUTUAL_AUTH', 'CONFIG_EXAMPLE_CONNECT_CASE_MUTUAL_AUTH_KEY_PWD']: + # These cases connect to server with both server and client verification (client key might be password protected) + with TlsServer(server_port, client_cert=True): + test_nr = start_connection_case(case, 'server with client verification - expect to connect normally') + dut.expect('MQTT_EVENT_CONNECTED: Test={}'.format(test_nr), timeout=30) + + case = 'CONFIG_EXAMPLE_CONNECT_CASE_INVALID_SERVER_CERT' + with TlsServer(server_port) as s: + test_nr = start_connection_case(case, 'invalid server certificate on default server - expect ssl handshake error') + dut.expect('MQTT_EVENT_ERROR: Test={}'.format(test_nr), timeout=30) + dut.expect('ESP-TLS ERROR: 0x8010') # expect ... handshake error (TLSV1_ALERT_UNKNOWN_CA) + if 'alert unknown ca' not in s.get_last_ssl_error(): + raise Exception('Unexpected ssl error from the server {}'.format(s.get_last_ssl_error())) + + case = 'CONFIG_EXAMPLE_CONNECT_CASE_MUTUAL_AUTH_BAD_CRT' + with TlsServer(server_port, client_cert=True) as s: + test_nr = start_connection_case(case, 'Invalid client certificate on server with client verification - expect ssl handshake error') + dut.expect('MQTT_EVENT_ERROR: Test={}'.format(test_nr), timeout=30) + dut.expect('ESP-TLS ERROR: 0x8010') # expect ... handshake error (CERTIFICATE_VERIFY_FAILED) + if 'CERTIFICATE_VERIFY_FAILED' not in s.get_last_ssl_error(): + raise Exception('Unexpected ssl error from the server {}'.format(s.get_last_ssl_error())) + + for case in ['CONFIG_EXAMPLE_CONNECT_CASE_NO_CERT', 'CONFIG_EXAMPLE_CONNECT_CASE_NO_CERT_ALPN']: + with TlsServer(server_port, use_alpn=True) as s: + test_nr = start_connection_case(case, 'server with alpn - expect connect, check resolved protocol') + dut.expect('MQTT_EVENT_CONNECTED: Test={}'.format(test_nr), timeout=30) + if case == 'CONFIG_EXAMPLE_CONNECT_CASE_NO_CERT' and s.get_negotiated_protocol() is None: + print(' - client with alpn off, no negotiated protocol: OK') + elif case == 'CONFIG_EXAMPLE_CONNECT_CASE_NO_CERT_ALPN' and s.get_negotiated_protocol() == 'mymqtt': + print(' - client with alpn on, negotiated protocol resolved: OK') + else: + raise Exception('Unexpected negotiated protocol {}'.format(s.get_negotiated_protocol())) + + @ttfw_idf.idf_custom_test(env_tag='Example_WIFI', group='test-apps') def test_app_protocol_mqtt_publish_connect(env, extra_data): """ @@ -256,6 +319,35 @@ def test_app_protocol_mqtt_publish_connect(env, extra_data): # Look for test case symbolic names and publish configs cases = {} publish_cfg = {} + try: + + # Get connection test cases configuration: symbolic names for test cases + for case in ['CONFIG_EXAMPLE_CONNECT_CASE_NO_CERT', + 'CONFIG_EXAMPLE_CONNECT_CASE_SERVER_CERT', + 'CONFIG_EXAMPLE_CONNECT_CASE_MUTUAL_AUTH', + 'CONFIG_EXAMPLE_CONNECT_CASE_INVALID_SERVER_CERT', + 'CONFIG_EXAMPLE_CONNECT_CASE_SERVER_DER_CERT', + 'CONFIG_EXAMPLE_CONNECT_CASE_MUTUAL_AUTH_KEY_PWD', + 'CONFIG_EXAMPLE_CONNECT_CASE_MUTUAL_AUTH_BAD_CRT', + 'CONFIG_EXAMPLE_CONNECT_CASE_NO_CERT_ALPN']: + cases[case] = dut1.app.get_sdkconfig()[case] + except Exception: + print('ENV_TEST_FAILURE: Some mandatory CONNECTION test case not found in sdkconfig') + raise + + dut1.start_app() + esp_ip = dut1.expect(re.compile(r' IPv4 address: ([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)'), timeout=30) + print('Got IP={}'.format(esp_ip[0])) + + if not os.getenv('MQTT_SKIP_CONNECT_TEST'): + connection_tests(dut1,cases) + + # + # start publish tests only if enabled in the environment (for weekend tests only) + if not os.getenv('MQTT_PUBLISH_TEST'): + return + + # Get publish test configuration try: def get_host_port_from_dut(dut1, config_option): value = re.search(r'\:\/\/([^:]+)\:([0-9]+)', dut1.app.get_sdkconfig()[config_option]) @@ -263,17 +355,6 @@ def test_app_protocol_mqtt_publish_connect(env, extra_data): return None, None return value.group(1), int(value.group(2)) - # Get connection test cases configuration: symbolic names for test cases - for i in ['CONFIG_EXAMPLE_CONNECT_CASE_NO_CERT', - 'CONFIG_EXAMPLE_CONNECT_CASE_SERVER_CERT', - 'CONFIG_EXAMPLE_CONNECT_CASE_MUTUAL_AUTH', - 'CONFIG_EXAMPLE_CONNECT_CASE_INVALID_SERVER_CERT', - 'CONFIG_EXAMPLE_CONNECT_CASE_SERVER_DER_CERT', - 'CONFIG_EXAMPLE_CONNECT_CASE_MUTUAL_AUTH_KEY_PWD', - 'CONFIG_EXAMPLE_CONNECT_CASE_MUTUAL_AUTH_BAD_CRT', - 'CONFIG_EXAMPLE_CONNECT_CASE_NO_CERT_ALPN']: - cases[i] = dut1.app.get_sdkconfig()[i] - # Get publish test configuration publish_cfg['publish_topic'] = dut1.app.get_sdkconfig()['CONFIG_EXAMPLE_SUBSCIBE_TOPIC'].replace('"','') publish_cfg['subscribe_topic'] = dut1.app.get_sdkconfig()['CONFIG_EXAMPLE_PUBLISH_TOPIC'].replace('"','') publish_cfg['broker_host_ssl'], publish_cfg['broker_port_ssl'] = get_host_port_from_dut(dut1, 'CONFIG_EXAMPLE_BROKER_SSL_URI') @@ -282,80 +363,9 @@ def test_app_protocol_mqtt_publish_connect(env, extra_data): publish_cfg['broker_host_wss'], publish_cfg['broker_port_wss'] = get_host_port_from_dut(dut1, 'CONFIG_EXAMPLE_BROKER_WSS_URI') except Exception: - print('ENV_TEST_FAILURE: Some mandatory test case not found in sdkconfig') + print('ENV_TEST_FAILURE: Some mandatory PUBLISH test case not found in sdkconfig') raise - dut1.start_app() - esp_ip = dut1.expect(re.compile(r' IPv4 address: ([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)'), timeout=30) - print('Got IP={}'.format(esp_ip[0])) - - # - # start connection test - ip = get_my_ip() - set_server_cert_cn(ip) - server_port = 2222 - - def start_connection_case(case, desc): - print('Starting {}: {}'.format(case, desc)) - case_id = cases[case] - dut1.write('conn {} {} {}'.format(ip, server_port, case_id)) - dut1.expect('Test case:{} started'.format(case_id)) - return case_id - - for case in ['CONFIG_EXAMPLE_CONNECT_CASE_NO_CERT', 'CONFIG_EXAMPLE_CONNECT_CASE_SERVER_CERT', 'CONFIG_EXAMPLE_CONNECT_CASE_SERVER_DER_CERT']: - # All these cases connect to the server with no server verification or with server only verification - with TlsServer(server_port): - test_nr = start_connection_case(case, 'default server - expect to connect normally') - dut1.expect('MQTT_EVENT_CONNECTED: Test={}'.format(test_nr), timeout=30) - with TlsServer(server_port, refuse_connection=True): - test_nr = start_connection_case(case, 'ssl shall connect, but mqtt sends connect refusal') - dut1.expect('MQTT_EVENT_ERROR: Test={}'.format(test_nr), timeout=30) - dut1.expect('MQTT ERROR: 0x5') # expecting 0x5 ... connection not authorized error - with TlsServer(server_port, client_cert=True) as s: - test_nr = start_connection_case(case, 'server with client verification - handshake error since client presents no client certificate') - dut1.expect('MQTT_EVENT_ERROR: Test={}'.format(test_nr), timeout=30) - dut1.expect('ESP-TLS ERROR: 0x8010') # expect ... handshake error (PEER_DID_NOT_RETURN_A_CERTIFICATE) - if 'PEER_DID_NOT_RETURN_A_CERTIFICATE' not in s.get_last_ssl_error(): - raise('Unexpected ssl error from the server {}'.format(s.get_last_ssl_error())) - - for case in ['CONFIG_EXAMPLE_CONNECT_CASE_MUTUAL_AUTH', 'CONFIG_EXAMPLE_CONNECT_CASE_MUTUAL_AUTH_KEY_PWD']: - # These cases connect to server with both server and client verification (client key might be password protected) - with TlsServer(server_port, client_cert=True): - test_nr = start_connection_case(case, 'server with client verification - expect to connect normally') - dut1.expect('MQTT_EVENT_CONNECTED: Test={}'.format(test_nr), timeout=30) - - case = 'CONFIG_EXAMPLE_CONNECT_CASE_INVALID_SERVER_CERT' - with TlsServer(server_port) as s: - test_nr = start_connection_case(case, 'invalid server certificate on default server - expect ssl handshake error') - dut1.expect('MQTT_EVENT_ERROR: Test={}'.format(test_nr), timeout=30) - dut1.expect('ESP-TLS ERROR: 0x8010') # expect ... handshake error (TLSV1_ALERT_UNKNOWN_CA) - if 'alert unknown ca' not in s.get_last_ssl_error(): - raise Exception('Unexpected ssl error from the server {}'.format(s.get_last_ssl_error())) - - case = 'CONFIG_EXAMPLE_CONNECT_CASE_MUTUAL_AUTH_BAD_CRT' - with TlsServer(server_port, client_cert=True) as s: - test_nr = start_connection_case(case, 'Invalid client certificate on server with client verification - expect ssl handshake error') - dut1.expect('MQTT_EVENT_ERROR: Test={}'.format(test_nr), timeout=30) - dut1.expect('ESP-TLS ERROR: 0x8010') # expect ... handshake error (CERTIFICATE_VERIFY_FAILED) - if 'CERTIFICATE_VERIFY_FAILED' not in s.get_last_ssl_error(): - raise Exception('Unexpected ssl error from the server {}'.format(s.get_last_ssl_error())) - - for case in ['CONFIG_EXAMPLE_CONNECT_CASE_NO_CERT', 'CONFIG_EXAMPLE_CONNECT_CASE_NO_CERT_ALPN']: - with TlsServer(server_port, use_alpn=True) as s: - test_nr = start_connection_case(case, 'server with alpn - expect connect, check resolved protocol') - dut1.expect('MQTT_EVENT_CONNECTED: Test={}'.format(test_nr), timeout=30) - if case == 'CONFIG_EXAMPLE_CONNECT_CASE_NO_CERT' and s.get_negotiated_protocol() is None: - print(' - client with alpn off, no negotiated protocol: OK') - elif case == 'CONFIG_EXAMPLE_CONNECT_CASE_NO_CERT_ALPN' and s.get_negotiated_protocol() == 'mymqtt': - print(' - client with alpn on, negotiated protocol resolved: OK') - else: - raise Exception('Unexpected negotiated protocol {}'.format(s.get_negotiated_protocol())) - - # - # start publish tests only if enabled in the environment (for weekend tests only) - if not os.getenv('MQTT_PUBLISH_TEST'): - return - def start_publish_case(transport, qos, repeat, published, queue): print('Starting Publish test: transport:{}, qos:{}, nr_of_msgs:{}, msg_size:{}, enqueue:{}' .format(transport, qos, published, repeat * DEFAULT_MSG_SIZE, queue)) @@ -375,4 +385,4 @@ def test_app_protocol_mqtt_publish_connect(env, extra_data): if __name__ == '__main__': - test_app_protocol_mqtt_publish_connect() + test_app_protocol_mqtt_publish_connect(dut=ttfw_idf.ESP32QEMUDUT if sys.argv[1:] == ['qemu'] else ttfw_idf.ESP32DUT) diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/main/connect_test.c b/tools/test_apps/protocols/mqtt/publish_connect_test/main/connect_test.c index a5817eb..112c000 100644 --- a/tools/test_apps/protocols/mqtt/publish_connect_test/main/connect_test.c +++ b/tools/test_apps/protocols/mqtt/publish_connect_test/main/connect_test.c @@ -42,29 +42,29 @@ static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_ esp_mqtt_event_handle_t event = event_data; ESP_LOGD(TAG, "Event: %d, Test case: %d", event->event_id, running_test_case); switch (event->event_id) { - case MQTT_EVENT_CONNECTED: - ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED: Test=%d", running_test_case); - break; - case MQTT_EVENT_ERROR: - ESP_LOGI(TAG, "MQTT_EVENT_ERROR: Test=%d", running_test_case); - if (event->error_handle->error_type == MQTT_ERROR_TYPE_ESP_TLS) { - ESP_LOGI(TAG, "ESP-TLS ERROR: 0x%x", event->error_handle->esp_tls_last_esp_err); - } else if (event->error_handle->error_type == MQTT_ERROR_TYPE_CONNECTION_REFUSED) { - ESP_LOGI(TAG, "MQTT ERROR: 0x%x", event->error_handle->connect_return_code); - } else { - ESP_LOGW(TAG, "Unknown error type: 0x%x", event->error_handle->error_type); - } - break; - default: - ESP_LOGI(TAG, "Other event id:%d", event->event_id); - break; + case MQTT_EVENT_CONNECTED: + ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED: Test=%d", running_test_case); + break; + case MQTT_EVENT_ERROR: + ESP_LOGI(TAG, "MQTT_EVENT_ERROR: Test=%d", running_test_case); + if (event->error_handle->error_type == MQTT_ERROR_TYPE_ESP_TLS) { + ESP_LOGI(TAG, "ESP-TLS ERROR: 0x%x", event->error_handle->esp_tls_last_esp_err); + } else if (event->error_handle->error_type == MQTT_ERROR_TYPE_CONNECTION_REFUSED) { + ESP_LOGI(TAG, "MQTT ERROR: 0x%x", event->error_handle->connect_return_code); + } else { + ESP_LOGW(TAG, "Unknown error type: 0x%x", event->error_handle->error_type); + } + break; + default: + ESP_LOGI(TAG, "Other event id:%d", event->event_id); + break; } } static void create_client(void) { const esp_mqtt_client_config_t mqtt_cfg = { - .uri = "mqtts://127.0.0.1:1234" + .uri = "mqtts://127.0.0.1:1234" }; esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg); esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, client); @@ -77,7 +77,7 @@ static void connect_no_certs(const char *host, const int port) char uri[64]; sprintf(uri, "mqtts://%s:%d", host, port); const esp_mqtt_client_config_t mqtt_cfg = { - .uri = uri + .uri = uri }; esp_mqtt_set_config(mqtt_client, &mqtt_cfg); esp_mqtt_client_disconnect(mqtt_client); @@ -89,12 +89,12 @@ static void connect_with_client_key_password(const char *host, const int port) char uri[64]; sprintf(uri, "mqtts://%s:%d", host, port); const esp_mqtt_client_config_t mqtt_cfg = { - .uri = uri, - .cert_pem = (const char *)ca_local_crt, - .client_cert_pem = (const char *)client_pwd_crt, - .client_key_pem = (const char *)client_pwd_key, - .clientkey_password = "esp32", - .clientkey_password_len = 5 + .uri = uri, + .cert_pem = (const char *)ca_local_crt, + .client_cert_pem = (const char *)client_pwd_crt, + .client_key_pem = (const char *)client_pwd_key, + .clientkey_password = "esp32", + .clientkey_password_len = 5 }; esp_mqtt_set_config(mqtt_client, &mqtt_cfg); esp_mqtt_client_disconnect(mqtt_client); @@ -106,11 +106,11 @@ static void connect_with_server_der_cert(const char *host, const int port) char uri[64]; sprintf(uri, "mqtts://%s:%d", host, port); const esp_mqtt_client_config_t mqtt_cfg = { - .uri = uri, - .cert_pem = (const char *)ca_der_start, - .cert_len = ca_der_end - ca_der_start, - .client_cert_pem = "NULL", - .client_key_pem = "NULL" + .uri = uri, + .cert_pem = (const char *)ca_der_start, + .cert_len = ca_der_end - ca_der_start, + .client_cert_pem = "NULL", + .client_key_pem = "NULL" }; esp_mqtt_set_config(mqtt_client, &mqtt_cfg); esp_mqtt_client_disconnect(mqtt_client); @@ -122,10 +122,10 @@ static void connect_with_wrong_server_cert(const char *host, const int port) char uri[64]; sprintf(uri, "mqtts://%s:%d", host, port); const esp_mqtt_client_config_t mqtt_cfg = { - .uri = uri, - .cert_pem = (const char *)client_pwd_crt, - .client_cert_pem = "NULL", - .client_key_pem = "NULL" + .uri = uri, + .cert_pem = (const char *)client_pwd_crt, + .client_cert_pem = "NULL", + .client_key_pem = "NULL" }; esp_mqtt_set_config(mqtt_client, &mqtt_cfg); esp_mqtt_client_disconnect(mqtt_client); @@ -165,10 +165,10 @@ static void connect_with_invalid_client_certs(const char *host, const int port) char uri[64]; sprintf(uri, "mqtts://%s:%d", host, port); const esp_mqtt_client_config_t mqtt_cfg = { - .uri = uri, - .cert_pem = (const char *)ca_local_crt, - .client_cert_pem = (const char *)client_inv_crt, - .client_key_pem = (const char *)client_no_pwd_key + .uri = uri, + .cert_pem = (const char *)ca_local_crt, + .client_cert_pem = (const char *)client_inv_crt, + .client_key_pem = (const char *)client_no_pwd_key }; esp_mqtt_set_config(mqtt_client, &mqtt_cfg); esp_mqtt_client_disconnect(mqtt_client); @@ -181,15 +181,15 @@ static void connect_with_alpn(const char *host, const int port) const char *alpn_protos[] = { "mymqtt", NULL }; sprintf(uri, "mqtts://%s:%d", host, port); const esp_mqtt_client_config_t mqtt_cfg = { - .uri = uri, - .alpn_protos = alpn_protos + .uri = uri, + .alpn_protos = alpn_protos }; esp_mqtt_set_config(mqtt_client, &mqtt_cfg); esp_mqtt_client_disconnect(mqtt_client); esp_mqtt_client_reconnect(mqtt_client); } -void connection_test(const char* line) +void connection_test(const char *line) { char test_type[32]; char host[32]; @@ -203,33 +203,33 @@ void connection_test(const char* line) ESP_LOGI(TAG, "CASE:%d, connecting to mqtts://%s:%d ", test_case, host, port); running_test_case = test_case; switch (test_case) { - case CONFIG_EXAMPLE_CONNECT_CASE_NO_CERT: - connect_no_certs(host, port); - break; - case CONFIG_EXAMPLE_CONNECT_CASE_SERVER_CERT: - connect_with_server_cert(host, port); - break; - case CONFIG_EXAMPLE_CONNECT_CASE_MUTUAL_AUTH: - connect_with_server_client_certs(host, port); - break; - case CONFIG_EXAMPLE_CONNECT_CASE_INVALID_SERVER_CERT: - connect_with_wrong_server_cert(host, port); - break; - case CONFIG_EXAMPLE_CONNECT_CASE_SERVER_DER_CERT: - connect_with_server_der_cert(host, port); - break; - case CONFIG_EXAMPLE_CONNECT_CASE_MUTUAL_AUTH_KEY_PWD: - connect_with_client_key_password(host, port); - break; - case CONFIG_EXAMPLE_CONNECT_CASE_MUTUAL_AUTH_BAD_CRT: - connect_with_invalid_client_certs(host, port); - break; - case CONFIG_EXAMPLE_CONNECT_CASE_NO_CERT_ALPN: - connect_with_alpn(host, port); - break; - default: - ESP_LOGE(TAG, "Unknown test case %d ", test_case); - break; + case CONFIG_EXAMPLE_CONNECT_CASE_NO_CERT: + connect_no_certs(host, port); + break; + case CONFIG_EXAMPLE_CONNECT_CASE_SERVER_CERT: + connect_with_server_cert(host, port); + break; + case CONFIG_EXAMPLE_CONNECT_CASE_MUTUAL_AUTH: + connect_with_server_client_certs(host, port); + break; + case CONFIG_EXAMPLE_CONNECT_CASE_INVALID_SERVER_CERT: + connect_with_wrong_server_cert(host, port); + break; + case CONFIG_EXAMPLE_CONNECT_CASE_SERVER_DER_CERT: + connect_with_server_der_cert(host, port); + break; + case CONFIG_EXAMPLE_CONNECT_CASE_MUTUAL_AUTH_KEY_PWD: + connect_with_client_key_password(host, port); + break; + case CONFIG_EXAMPLE_CONNECT_CASE_MUTUAL_AUTH_BAD_CRT: + connect_with_invalid_client_certs(host, port); + break; + case CONFIG_EXAMPLE_CONNECT_CASE_NO_CERT_ALPN: + connect_with_alpn(host, port); + break; + default: + ESP_LOGE(TAG, "Unknown test case %d ", test_case); + break; } ESP_LOGI(TAG, "Test case:%d started", test_case); } diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/main/mqtt_eclipse_org.pem b/tools/test_apps/protocols/mqtt/publish_connect_test/main/mqtt_eclipse_org.pem deleted file mode 100644 index 0002462..0000000 --- a/tools/test_apps/protocols/mqtt/publish_connect_test/main/mqtt_eclipse_org.pem +++ /dev/null @@ -1,27 +0,0 @@ ------BEGIN CERTIFICATE----- -MIIEkjCCA3qgAwIBAgIQCgFBQgAAAVOFc2oLheynCDANBgkqhkiG9w0BAQsFADA/ -MSQwIgYDVQQKExtEaWdpdGFsIFNpZ25hdHVyZSBUcnVzdCBDby4xFzAVBgNVBAMT -DkRTVCBSb290IENBIFgzMB4XDTE2MDMxNzE2NDA0NloXDTIxMDMxNzE2NDA0Nlow -SjELMAkGA1UEBhMCVVMxFjAUBgNVBAoTDUxldCdzIEVuY3J5cHQxIzAhBgNVBAMT -GkxldCdzIEVuY3J5cHQgQXV0aG9yaXR5IFgzMIIBIjANBgkqhkiG9w0BAQEFAAOC -AQ8AMIIBCgKCAQEAnNMM8FrlLke3cl03g7NoYzDq1zUmGSXhvb418XCSL7e4S0EF -q6meNQhY7LEqxGiHC6PjdeTm86dicbp5gWAf15Gan/PQeGdxyGkOlZHP/uaZ6WA8 -SMx+yk13EiSdRxta67nsHjcAHJyse6cF6s5K671B5TaYucv9bTyWaN8jKkKQDIZ0 -Z8h/pZq4UmEUEz9l6YKHy9v6Dlb2honzhT+Xhq+w3Brvaw2VFn3EK6BlspkENnWA -a6xK8xuQSXgvopZPKiAlKQTGdMDQMc2PMTiVFrqoM7hD8bEfwzB/onkxEz0tNvjj -/PIzark5McWvxI0NHWQWM6r6hCm21AvA2H3DkwIDAQABo4IBfTCCAXkwEgYDVR0T -AQH/BAgwBgEB/wIBADAOBgNVHQ8BAf8EBAMCAYYwfwYIKwYBBQUHAQEEczBxMDIG -CCsGAQUFBzABhiZodHRwOi8vaXNyZy50cnVzdGlkLm9jc3AuaWRlbnRydXN0LmNv -bTA7BggrBgEFBQcwAoYvaHR0cDovL2FwcHMuaWRlbnRydXN0LmNvbS9yb290cy9k -c3Ryb290Y2F4My5wN2MwHwYDVR0jBBgwFoAUxKexpHsscfrb4UuQdf/EFWCFiRAw -VAYDVR0gBE0wSzAIBgZngQwBAgEwPwYLKwYBBAGC3xMBAQEwMDAuBggrBgEFBQcC -ARYiaHR0cDovL2Nwcy5yb290LXgxLmxldHNlbmNyeXB0Lm9yZzA8BgNVHR8ENTAz -MDGgL6AthitodHRwOi8vY3JsLmlkZW50cnVzdC5jb20vRFNUUk9PVENBWDNDUkwu -Y3JsMB0GA1UdDgQWBBSoSmpjBH3duubRObemRWXv86jsoTANBgkqhkiG9w0BAQsF -AAOCAQEA3TPXEfNjWDjdGBX7CVW+dla5cEilaUcne8IkCJLxWh9KEik3JHRRHGJo -uM2VcGfl96S8TihRzZvoroed6ti6WqEBmtzw3Wodatg+VyOeph4EYpr/1wXKtx8/ -wApIvJSwtmVi4MFU5aMqrSDE6ea73Mj2tcMyo5jMd6jmeWUHK8so/joWUoHOUgwu -X4Po1QYz+3dszkDqMp4fklxBwXRsW10KXzPMTZ+sOPAveyxindmjkW8lGy+QsRlG -PfZ+G6Z6h7mjem0Y+iWlkYcV4PIWL1iwBi8saCbGS5jN2p8M+X+Q7UNKEkROb3N6 -KOqkqm57TH2H3eDJAkSnh6/DNFu0Qg== ------END CERTIFICATE----- diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/main/mqtt_eclipseprojects_io.pem b/tools/test_apps/protocols/mqtt/publish_connect_test/main/mqtt_eclipseprojects_io.pem new file mode 100644 index 0000000..43b222a --- /dev/null +++ b/tools/test_apps/protocols/mqtt/publish_connect_test/main/mqtt_eclipseprojects_io.pem @@ -0,0 +1,30 @@ +-----BEGIN CERTIFICATE----- +MIIFFjCCAv6gAwIBAgIRAJErCErPDBinU/bWLiWnX1owDQYJKoZIhvcNAQELBQAw +TzELMAkGA1UEBhMCVVMxKTAnBgNVBAoTIEludGVybmV0IFNlY3VyaXR5IFJlc2Vh +cmNoIEdyb3VwMRUwEwYDVQQDEwxJU1JHIFJvb3QgWDEwHhcNMjAwOTA0MDAwMDAw +WhcNMjUwOTE1MTYwMDAwWjAyMQswCQYDVQQGEwJVUzEWMBQGA1UEChMNTGV0J3Mg +RW5jcnlwdDELMAkGA1UEAxMCUjMwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEK +AoIBAQC7AhUozPaglNMPEuyNVZLD+ILxmaZ6QoinXSaqtSu5xUyxr45r+XXIo9cP +R5QUVTVXjJ6oojkZ9YI8QqlObvU7wy7bjcCwXPNZOOftz2nwWgsbvsCUJCWH+jdx +sxPnHKzhm+/b5DtFUkWWqcFTzjTIUu61ru2P3mBw4qVUq7ZtDpelQDRrK9O8Zutm +NHz6a4uPVymZ+DAXXbpyb/uBxa3Shlg9F8fnCbvxK/eG3MHacV3URuPMrSXBiLxg +Z3Vms/EY96Jc5lP/Ooi2R6X/ExjqmAl3P51T+c8B5fWmcBcUr2Ok/5mzk53cU6cG +/kiFHaFpriV1uxPMUgP17VGhi9sVAgMBAAGjggEIMIIBBDAOBgNVHQ8BAf8EBAMC +AYYwHQYDVR0lBBYwFAYIKwYBBQUHAwIGCCsGAQUFBwMBMBIGA1UdEwEB/wQIMAYB +Af8CAQAwHQYDVR0OBBYEFBQusxe3WFbLrlAJQOYfr52LFMLGMB8GA1UdIwQYMBaA +FHm0WeZ7tuXkAXOACIjIGlj26ZtuMDIGCCsGAQUFBwEBBCYwJDAiBggrBgEFBQcw +AoYWaHR0cDovL3gxLmkubGVuY3Iub3JnLzAnBgNVHR8EIDAeMBygGqAYhhZodHRw +Oi8veDEuYy5sZW5jci5vcmcvMCIGA1UdIAQbMBkwCAYGZ4EMAQIBMA0GCysGAQQB +gt8TAQEBMA0GCSqGSIb3DQEBCwUAA4ICAQCFyk5HPqP3hUSFvNVneLKYY611TR6W +PTNlclQtgaDqw+34IL9fzLdwALduO/ZelN7kIJ+m74uyA+eitRY8kc607TkC53wl +ikfmZW4/RvTZ8M6UK+5UzhK8jCdLuMGYL6KvzXGRSgi3yLgjewQtCPkIVz6D2QQz +CkcheAmCJ8MqyJu5zlzyZMjAvnnAT45tRAxekrsu94sQ4egdRCnbWSDtY7kh+BIm +lJNXoB1lBMEKIq4QDUOXoRgffuDghje1WrG9ML+Hbisq/yFOGwXD9RiX8F6sw6W4 +avAuvDszue5L3sz85K+EC4Y/wFVDNvZo4TYXao6Z0f+lQKc0t8DQYzk1OXVu8rp2 +yJMC6alLbBfODALZvYH7n7do1AZls4I9d1P4jnkDrQoxB3UqQ9hVl3LEKQ73xF1O +yK5GhDDX8oVfGKF5u+decIsH4YaTw7mP3GFxJSqv3+0lUFJoi5Lc5da149p90Ids +hCExroL1+7mryIkXPeFM5TgO9r0rvZaBFOvV2z0gp35Z0+L4WPlbuEjN/lxPFin+ +HlUjr8gRsI3qfJOQFy/9rKIJR0Y/8Omwt/8oTWgy1mdeHmmjk7j1nYsvC9JSQ6Zv +MldlTTKB3zhThV1+XWYp6rjd5JW1zbVWEkLNxE7GJThEUG3szgBVGP7pSWTUTsqX +nLRbwHOoq7hHwg== +-----END CERTIFICATE----- diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/main/publish_connect_test.c b/tools/test_apps/protocols/mqtt/publish_connect_test/main/publish_connect_test.c index a112834..84da251 100644 --- a/tools/test_apps/protocols/mqtt/publish_connect_test/main/publish_connect_test.c +++ b/tools/test_apps/protocols/mqtt/publish_connect_test/main/publish_connect_test.c @@ -18,8 +18,8 @@ static const char *TAG = "PUBLISH_CONNECT_TEST"; -void connection_test(const char* line); -void publish_test(const char* line); +void connection_test(const char *line); +void publish_test(const char *line); static void get_string(char *line, size_t size) { diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/main/publish_test.c b/tools/test_apps/protocols/mqtt/publish_connect_test/main/publish_test.c index d4863e5..8801a49 100644 --- a/tools/test_apps/protocols/mqtt/publish_connect_test/main/publish_test.c +++ b/tools/test_apps/protocols/mqtt/publish_connect_test/main/publish_test.c @@ -17,6 +17,7 @@ #include "esp_log.h" #include "mqtt_client.h" +#include "sdkconfig.h" static const char *TAG = "PUBLISH_TEST"; @@ -32,25 +33,27 @@ static size_t expected_published = 0; static size_t actual_published = 0; static int qos_test = 0; - #if CONFIG_EXAMPLE_BROKER_CERTIFICATE_OVERRIDDEN == 1 - static const uint8_t mqtt_eclipse_org_pem_start[] = "-----BEGIN CERTIFICATE-----\n" CONFIG_EXAMPLE_BROKER_CERTIFICATE_OVERRIDE "\n-----END CERTIFICATE-----"; - #else - extern const uint8_t mqtt_eclipse_org_pem_start[] asm("_binary_mqtt_eclipse_org_pem_start"); - #endif - extern const uint8_t mqtt_eclipse_org_pem_end[] asm("_binary_mqtt_eclipse_org_pem_end"); +#if CONFIG_EXAMPLE_BROKER_CERTIFICATE_OVERRIDDEN == 1 +static const uint8_t mqtt_eclipseprojects_io_pem_start[] = "-----BEGIN CERTIFICATE-----\n" CONFIG_EXAMPLE_BROKER_CERTIFICATE_OVERRIDE "\n-----END CERTIFICATE-----"; +#else +extern const uint8_t mqtt_eclipseprojects_io_pem_start[] asm("_binary_mqtt_eclipseprojects_io_pem_start"); +#endif +extern const uint8_t mqtt_eclipseprojects_io_pem_end[] asm("_binary_mqtt_eclipseprojects_io_pem_end"); -static esp_err_t mqtt_event_handler(esp_mqtt_event_handle_t event) +static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data) { + esp_mqtt_event_handle_t event = event_data; esp_mqtt_client_handle_t client = event->client; static int msg_id = 0; static int actual_len = 0; - // your_context_t *context = event->context; switch (event->event_id) { + case MQTT_EVENT_BEFORE_CONNECT: + break; case MQTT_EVENT_CONNECTED: ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED"); xEventGroupSetBits(mqtt_event_group, CONNECTED_BIT); msg_id = esp_mqtt_client_subscribe(client, CONFIG_EXAMPLE_SUBSCIBE_TOPIC, qos_test); - ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id); + ESP_LOGI(TAG, "sent subscribe successful %s , msg_id=%d", CONFIG_EXAMPLE_SUBSCIBE_TOPIC, msg_id); break; case MQTT_EVENT_DISCONNECTED: @@ -76,7 +79,7 @@ static esp_err_t mqtt_event_handler(esp_mqtt_event_handle_t event) msg_id = event->msg_id; } else { actual_len += event->data_len; - // check consisency with msg_id across multiple data events for single msg + // check consistency with msg_id across multiple data events for single msg if (msg_id != event->msg_id) { ESP_LOGI(TAG, "Wrong msg_id in chunked message %d != %d", msg_id, event->msg_id); abort(); @@ -105,36 +108,24 @@ static esp_err_t mqtt_event_handler(esp_mqtt_event_handle_t event) ESP_LOGI(TAG, "Other event id:%d", event->event_id); break; } - return ESP_OK; } +typedef enum {NONE, TCP, SSL, WS, WSS} transport_t; +static transport_t current_transport; -static void mqtt_app_start(void) + +void test_init(void) { mqtt_event_group = xEventGroupCreate(); - const esp_mqtt_client_config_t mqtt_cfg = { - .event_handle = mqtt_event_handler, - .cert_pem = (const char *)mqtt_eclipse_org_pem_start, - }; - + esp_mqtt_client_config_t config = {0}; + mqtt_client = esp_mqtt_client_init(&config); + current_transport = NONE; + esp_mqtt_client_register_event(mqtt_client, ESP_EVENT_ANY_ID, mqtt_event_handler, NULL); ESP_LOGI(TAG, "[APP] Free memory: %d bytes", esp_get_free_heap_size()); - mqtt_client = esp_mqtt_client_init(&mqtt_cfg); } -void publish_test(const char* line) +void pattern_setup(char *pattern, int repeat) { - char pattern[32]; - char transport[32]; - int repeat = 0; - int enqueue = 0; - static bool is_mqtt_init = false; - - if (!is_mqtt_init) { - mqtt_app_start(); - is_mqtt_init = true; - } - sscanf(line, "%s %s %d %d %d %d", transport, pattern, &repeat, &expected_published, &qos_test, &enqueue); - ESP_LOGI(TAG, "PATTERN:%s REPEATED:%d PUBLISHED:%d\n", pattern, repeat, expected_published); int pattern_size = strlen(pattern); free(expected_data); free(actual_data); @@ -146,29 +137,80 @@ void publish_test(const char* line) memcpy(expected_data + i * pattern_size, pattern, pattern_size); } printf("EXPECTED STRING %.*s, SIZE:%d\n", expected_size, expected_data, expected_size); - esp_mqtt_client_stop(mqtt_client); +} +static void configure_client(char *transport) +{ + ESP_LOGI(TAG, "Configuration"); + transport_t selected_transport; if (0 == strcmp(transport, "tcp")) { - ESP_LOGI(TAG, "[TCP transport] Startup.."); - esp_mqtt_client_set_uri(mqtt_client, CONFIG_EXAMPLE_BROKER_TCP_URI); + selected_transport = TCP; } else if (0 == strcmp(transport, "ssl")) { - ESP_LOGI(TAG, "[SSL transport] Startup.."); - esp_mqtt_client_set_uri(mqtt_client, CONFIG_EXAMPLE_BROKER_SSL_URI); + selected_transport = SSL; } else if (0 == strcmp(transport, "ws")) { - ESP_LOGI(TAG, "[WS transport] Startup.."); - esp_mqtt_client_set_uri(mqtt_client, CONFIG_EXAMPLE_BROKER_WS_URI); + selected_transport = WS; } else if (0 == strcmp(transport, "wss")) { - ESP_LOGI(TAG, "[WSS transport] Startup.."); - esp_mqtt_client_set_uri(mqtt_client, CONFIG_EXAMPLE_BROKER_WSS_URI); + selected_transport = WSS; } else { - ESP_LOGE(TAG, "Unexpected transport"); + ESP_LOGE(TAG, "Unexpected transport %s", transport); abort(); } - xEventGroupClearBits(mqtt_event_group, CONNECTED_BIT); - esp_mqtt_client_start(mqtt_client); - ESP_LOGI(TAG, "Note free memory: %d bytes", esp_get_free_heap_size()); - xEventGroupWaitBits(mqtt_event_group, CONNECTED_BIT, false, true, portMAX_DELAY); + + if (selected_transport != current_transport) { + esp_mqtt_client_config_t config = {0}; + if (selected_transport == SSL || selected_transport == WSS) { + ESP_LOGI(TAG, "Set certificate"); + config.cert_pem = (const char *)mqtt_eclipseprojects_io_pem_start; + esp_mqtt_set_config(mqtt_client, &config); + } + switch (selected_transport) { + case NONE: + break; + case TCP: + ESP_LOGI(TAG, "[TCP transport] Startup.."); + esp_mqtt_client_set_uri(mqtt_client, CONFIG_EXAMPLE_BROKER_TCP_URI); + break; + case SSL: + ESP_LOGI(TAG, "[SSL transport] Startup.."); + config.uri = CONFIG_EXAMPLE_BROKER_SSL_URI; + break; + case WS: + ESP_LOGI(TAG, "[WS transport] Startup.."); + esp_mqtt_client_set_uri(mqtt_client, CONFIG_EXAMPLE_BROKER_WS_URI); + break; + case WSS: + ESP_LOGI(TAG, "[WSS transport] Startup.."); + esp_mqtt_client_set_uri(mqtt_client, CONFIG_EXAMPLE_BROKER_WSS_URI); + break; + } + } +} +void publish_test(const char *line) +{ + char pattern[32]; + char transport[32]; + int repeat = 0; + int enqueue = 0; + + esp_mqtt_client_stop(mqtt_client); + + static bool is_test_init = false; + if (!is_test_init) { + test_init(); + is_test_init = true; + } + + sscanf(line, "%s %s %d %d %d %d", transport, pattern, &repeat, &expected_published, &qos_test, &enqueue); + ESP_LOGI(TAG, "PATTERN:%s REPEATED:%d PUBLISHED:%d\n", pattern, repeat, expected_published); + pattern_setup(pattern, repeat); + xEventGroupClearBits(mqtt_event_group, CONNECTED_BIT); + configure_client(transport); + esp_mqtt_client_start(mqtt_client); + + ESP_LOGI(TAG, "Note free memory: %d bytes", esp_get_free_heap_size()); + + xEventGroupWaitBits(mqtt_event_group, CONNECTED_BIT, false, true, portMAX_DELAY); for (int i = 0; i < expected_published; i++) { int msg_id; if (enqueue) { From 523934decff25bfbd4530e930ff31d8ca6bb915b Mon Sep 17 00:00:00 2001 From: David Cermak Date: Tue, 7 Sep 2021 11:26:59 +0200 Subject: [PATCH 097/231] CI: Update mqtt test to cleanup connect-publish-URI interaction --- .../mqtt/publish_connect_test/app_test.py | 5 +++++ .../publish_connect_test/main/connect_test.c | 14 ++++++++++++ .../publish_connect_test/main/publish_test.c | 22 ++++++++++--------- 3 files changed, 31 insertions(+), 10 deletions(-) diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/app_test.py b/tools/test_apps/protocols/mqtt/publish_connect_test/app_test.py index 580e285..738951b 100644 --- a/tools/test_apps/protocols/mqtt/publish_connect_test/app_test.py +++ b/tools/test_apps/protocols/mqtt/publish_connect_test/app_test.py @@ -245,6 +245,9 @@ def connection_tests(dut, cases): set_server_cert_cn(ip) server_port = 2222 + def teardown_connection_suite(): + dut.write('conn teardown 0 0') + def start_connection_case(case, desc): print('Starting {}: {}'.format(case, desc)) case_id = cases[case] @@ -301,6 +304,8 @@ def connection_tests(dut, cases): else: raise Exception('Unexpected negotiated protocol {}'.format(s.get_negotiated_protocol())) + teardown_connection_suite() + @ttfw_idf.idf_custom_test(env_tag='Example_WIFI', group='test-apps') def test_app_protocol_mqtt_publish_connect(env, extra_data): diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/main/connect_test.c b/tools/test_apps/protocols/mqtt/publish_connect_test/main/connect_test.c index 112c000..aa03f19 100644 --- a/tools/test_apps/protocols/mqtt/publish_connect_test/main/connect_test.c +++ b/tools/test_apps/protocols/mqtt/publish_connect_test/main/connect_test.c @@ -70,6 +70,17 @@ static void create_client(void) esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, client); mqtt_client = client; esp_mqtt_client_start(client); + ESP_LOGI(TAG, "mqtt client created for connection tests"); +} + +static void destroy_client(void) +{ + if (mqtt_client) { + esp_mqtt_client_stop(mqtt_client); + esp_mqtt_client_destroy(mqtt_client); + mqtt_client = NULL; + ESP_LOGI(TAG, "mqtt client for connection tests destroyed"); + } } static void connect_no_certs(const char *host, const int port) @@ -200,6 +211,9 @@ void connection_test(const char *line) if (mqtt_client == NULL) { create_client(); } + if (strcmp(host, "teardown") == 0) { + destroy_client();; + } ESP_LOGI(TAG, "CASE:%d, connecting to mqtts://%s:%d ", test_case, host, port); running_test_case = test_case; switch (test_case) { diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/main/publish_test.c b/tools/test_apps/protocols/mqtt/publish_connect_test/main/publish_test.c index 8801a49..38cd136 100644 --- a/tools/test_apps/protocols/mqtt/publish_connect_test/main/publish_test.c +++ b/tools/test_apps/protocols/mqtt/publish_connect_test/main/publish_test.c @@ -159,17 +159,12 @@ static void configure_client(char *transport) if (selected_transport != current_transport) { esp_mqtt_client_config_t config = {0}; - if (selected_transport == SSL || selected_transport == WSS) { - ESP_LOGI(TAG, "Set certificate"); - config.cert_pem = (const char *)mqtt_eclipseprojects_io_pem_start; - esp_mqtt_set_config(mqtt_client, &config); - } switch (selected_transport) { case NONE: break; case TCP: ESP_LOGI(TAG, "[TCP transport] Startup.."); - esp_mqtt_client_set_uri(mqtt_client, CONFIG_EXAMPLE_BROKER_TCP_URI); + config.uri = CONFIG_EXAMPLE_BROKER_TCP_URI; break; case SSL: ESP_LOGI(TAG, "[SSL transport] Startup.."); @@ -177,14 +172,21 @@ static void configure_client(char *transport) break; case WS: ESP_LOGI(TAG, "[WS transport] Startup.."); - esp_mqtt_client_set_uri(mqtt_client, CONFIG_EXAMPLE_BROKER_WS_URI); + config.uri = CONFIG_EXAMPLE_BROKER_WS_URI; break; case WSS: ESP_LOGI(TAG, "[WSS transport] Startup.."); - esp_mqtt_client_set_uri(mqtt_client, CONFIG_EXAMPLE_BROKER_WSS_URI); + config.uri = CONFIG_EXAMPLE_BROKER_WSS_URI; break; } + if (selected_transport == SSL || selected_transport == WSS) { + ESP_LOGI(TAG, "Set certificate"); + config.cert_pem = (const char *)mqtt_eclipseprojects_io_pem_start; + } + esp_mqtt_set_config(mqtt_client, &config); + } + } void publish_test(const char *line) { @@ -193,12 +195,12 @@ void publish_test(const char *line) int repeat = 0; int enqueue = 0; - esp_mqtt_client_stop(mqtt_client); - static bool is_test_init = false; if (!is_test_init) { test_init(); is_test_init = true; + } else { + esp_mqtt_client_stop(mqtt_client); } sscanf(line, "%s %s %d %d %d %d", transport, pattern, &repeat, &expected_published, &qos_test, &enqueue); From c7ce57bb52ade5b25e295acd9c9750c06a60003a Mon Sep 17 00:00:00 2001 From: Aditya Patwardhan Date: Tue, 21 Sep 2021 17:56:50 +0530 Subject: [PATCH 098/231] mqtt_test_app: Fix the invalid esp-tls error code values in mqtt test app --- .../protocols/mqtt/publish_connect_test/app_test.py | 6 +++--- .../protocols/mqtt/publish_connect_test/main/connect_test.c | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/app_test.py b/tools/test_apps/protocols/mqtt/publish_connect_test/app_test.py index 738951b..2a0e2b5 100644 --- a/tools/test_apps/protocols/mqtt/publish_connect_test/app_test.py +++ b/tools/test_apps/protocols/mqtt/publish_connect_test/app_test.py @@ -267,7 +267,7 @@ def connection_tests(dut, cases): with TlsServer(server_port, client_cert=True) as s: test_nr = start_connection_case(case, 'server with client verification - handshake error since client presents no client certificate') dut.expect('MQTT_EVENT_ERROR: Test={}'.format(test_nr), timeout=30) - dut.expect('ESP-TLS ERROR: 0x8010') # expect ... handshake error (PEER_DID_NOT_RETURN_A_CERTIFICATE) + dut.expect('ESP-TLS ERROR: ESP_ERR_MBEDTLS_SSL_HANDSHAKE_FAILED') # expect ... handshake error (PEER_DID_NOT_RETURN_A_CERTIFICATE) if 'PEER_DID_NOT_RETURN_A_CERTIFICATE' not in s.get_last_ssl_error(): raise('Unexpected ssl error from the server {}'.format(s.get_last_ssl_error())) @@ -281,7 +281,7 @@ def connection_tests(dut, cases): with TlsServer(server_port) as s: test_nr = start_connection_case(case, 'invalid server certificate on default server - expect ssl handshake error') dut.expect('MQTT_EVENT_ERROR: Test={}'.format(test_nr), timeout=30) - dut.expect('ESP-TLS ERROR: 0x8010') # expect ... handshake error (TLSV1_ALERT_UNKNOWN_CA) + dut.expect('ESP-TLS ERROR: ESP_ERR_MBEDTLS_SSL_HANDSHAKE_FAILED') # expect ... handshake error (TLSV1_ALERT_UNKNOWN_CA) if 'alert unknown ca' not in s.get_last_ssl_error(): raise Exception('Unexpected ssl error from the server {}'.format(s.get_last_ssl_error())) @@ -289,7 +289,7 @@ def connection_tests(dut, cases): with TlsServer(server_port, client_cert=True) as s: test_nr = start_connection_case(case, 'Invalid client certificate on server with client verification - expect ssl handshake error') dut.expect('MQTT_EVENT_ERROR: Test={}'.format(test_nr), timeout=30) - dut.expect('ESP-TLS ERROR: 0x8010') # expect ... handshake error (CERTIFICATE_VERIFY_FAILED) + dut.expect('ESP-TLS ERROR: ESP_ERR_MBEDTLS_SSL_HANDSHAKE_FAILED') # expect ... handshake error (CERTIFICATE_VERIFY_FAILED) if 'CERTIFICATE_VERIFY_FAILED' not in s.get_last_ssl_error(): raise Exception('Unexpected ssl error from the server {}'.format(s.get_last_ssl_error())) diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/main/connect_test.c b/tools/test_apps/protocols/mqtt/publish_connect_test/main/connect_test.c index aa03f19..86f48fd 100644 --- a/tools/test_apps/protocols/mqtt/publish_connect_test/main/connect_test.c +++ b/tools/test_apps/protocols/mqtt/publish_connect_test/main/connect_test.c @@ -48,7 +48,7 @@ static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_ case MQTT_EVENT_ERROR: ESP_LOGI(TAG, "MQTT_EVENT_ERROR: Test=%d", running_test_case); if (event->error_handle->error_type == MQTT_ERROR_TYPE_ESP_TLS) { - ESP_LOGI(TAG, "ESP-TLS ERROR: 0x%x", event->error_handle->esp_tls_last_esp_err); + ESP_LOGI(TAG, "ESP-TLS ERROR: %s", esp_err_to_name(event->error_handle->esp_tls_last_esp_err)); } else if (event->error_handle->error_type == MQTT_ERROR_TYPE_CONNECTION_REFUSED) { ESP_LOGI(TAG, "MQTT ERROR: 0x%x", event->error_handle->connect_return_code); } else { From 426f028d845d06f684a70de92170556aef6deef8 Mon Sep 17 00:00:00 2001 From: Roland Dobai Date: Thu, 4 Nov 2021 15:28:07 +0100 Subject: [PATCH 099/231] Build & config: Remove the "make" build system The "make" build system was deprecated in v4.0 in favor of idf.py (cmake). The remaining support is removed in v5.0. --- examples/protocols/mqtt/ssl/README.md | 1 - examples/protocols/mqtt/ssl_ds/README.md | 1 - examples/protocols/mqtt/ssl_mutual_auth/README.md | 1 - examples/protocols/mqtt/ssl_psk/README.md | 3 +-- examples/protocols/mqtt/tcp/README.md | 1 - examples/protocols/mqtt/ws/README.md | 1 - examples/protocols/mqtt/wss/README.md | 1 - 7 files changed, 1 insertion(+), 8 deletions(-) diff --git a/examples/protocols/mqtt/ssl/README.md b/examples/protocols/mqtt/ssl/README.md index 680fc8a..754c693 100644 --- a/examples/protocols/mqtt/ssl/README.md +++ b/examples/protocols/mqtt/ssl/README.md @@ -17,7 +17,6 @@ This example can be executed on any ESP32 board, the only required interface is * Open the project configuration menu (`idf.py menuconfig`) * Configure Wi-Fi or Ethernet under "Example Connection Configuration" menu. See "Establishing Wi-Fi or Ethernet Connection" section in [examples/protocols/README.md](../../README.md) for more details. -* When using Make build system, set `Default serial port` under `Serial flasher config`. PEM certificate for this example could be extracted from an openssl `s_client` command connecting to mqtt.eclipseprojects.io. In case a host operating system has `openssl` and `sed` packages installed, one could execute the following command to download and save the root certificate to a file (Note for Windows users: Both Linux like environment or Windows native packages may be used). diff --git a/examples/protocols/mqtt/ssl_ds/README.md b/examples/protocols/mqtt/ssl_ds/README.md index b186469..31647bc 100644 --- a/examples/protocols/mqtt/ssl_ds/README.md +++ b/examples/protocols/mqtt/ssl_ds/README.md @@ -57,7 +57,6 @@ RSA private key is nothing but the client private key ( RSA ) generated in Step #### 4) Connection cofiguration * Open the project configuration menu (`idf.py menuconfig`) * Configure Wi-Fi or Ethernet under "Example Connection Configuration" menu. See "Establishing Wi-Fi or Ethernet Connection" section in [examples/protocols/README.md](../../README.md) for more details. -* When using Make build system, set `Default serial port` under `Serial flasher config`. ### Build and Flash diff --git a/examples/protocols/mqtt/ssl_mutual_auth/README.md b/examples/protocols/mqtt/ssl_mutual_auth/README.md index 50472d6..74beb89 100644 --- a/examples/protocols/mqtt/ssl_mutual_auth/README.md +++ b/examples/protocols/mqtt/ssl_mutual_auth/README.md @@ -17,7 +17,6 @@ This example can be executed on any ESP32 board, the only required interface is * Open the project configuration menu (`idf.py menuconfig`) * Configure Wi-Fi or Ethernet under "Example Connection Configuration" menu. See "Establishing Wi-Fi or Ethernet Connection" section in [examples/protocols/README.md](../../README.md) for more details. -* When using Make build system, set `Default serial port` under `Serial flasher config`. * Generate your client keys and certificate diff --git a/examples/protocols/mqtt/ssl_psk/README.md b/examples/protocols/mqtt/ssl_psk/README.md index c46688f..486fc5e 100644 --- a/examples/protocols/mqtt/ssl_psk/README.md +++ b/examples/protocols/mqtt/ssl_psk/README.md @@ -30,9 +30,8 @@ as required by MQTT API. (See the example source for details: `"BAD123" -> 0xBA, ### Configure the project -* Run `make menuconfig` (or `idf.py menuconfig` if using CMake build system) +* Run `idf.py menuconfig` * Configure Wi-Fi or Ethernet under "Example Connection Configuration" menu. See "Establishing Wi-Fi or Ethernet Connection" section in [examples/protocols/README.md](../../README.md) for more details. -* When using Make build system, set `Default serial port` under `Serial flasher config`. ### Build and Flash diff --git a/examples/protocols/mqtt/tcp/README.md b/examples/protocols/mqtt/tcp/README.md index 7fe788c..e12a161 100644 --- a/examples/protocols/mqtt/tcp/README.md +++ b/examples/protocols/mqtt/tcp/README.md @@ -18,7 +18,6 @@ This example can be executed on any ESP32 board, the only required interface is * Open the project configuration menu (`idf.py menuconfig`) * Configure Wi-Fi or Ethernet under "Example Connection Configuration" menu. See "Establishing Wi-Fi or Ethernet Connection" section in [examples/protocols/README.md](../../README.md) for more details. -* When using Make build system, set `Default serial port` under `Serial flasher config`. ### Build and Flash diff --git a/examples/protocols/mqtt/ws/README.md b/examples/protocols/mqtt/ws/README.md index c9ca6ea..2bceb3c 100644 --- a/examples/protocols/mqtt/ws/README.md +++ b/examples/protocols/mqtt/ws/README.md @@ -17,7 +17,6 @@ This example can be executed on any ESP32 board, the only required interface is * Open the project configuration menu (`idf.py menuconfig`) * Configure Wi-Fi or Ethernet under "Example Connection Configuration" menu. See "Establishing Wi-Fi or Ethernet Connection" section in [examples/protocols/README.md](../../README.md) for more details. -* When using Make build system, set `Default serial port` under `Serial flasher config`. ### Build and Flash diff --git a/examples/protocols/mqtt/wss/README.md b/examples/protocols/mqtt/wss/README.md index 3d2fa2b..fb4b6c1 100644 --- a/examples/protocols/mqtt/wss/README.md +++ b/examples/protocols/mqtt/wss/README.md @@ -16,7 +16,6 @@ This example can be executed on any ESP32 board, the only required interface is * Open the project configuration menu (`idf.py menuconfig`) * Configure Wi-Fi or Ethernet under "Example Connection Configuration" menu. See "Establishing Wi-Fi or Ethernet Connection" section in [examples/protocols/README.md](../../README.md) for more details. -* When using Make build system, set `Default serial port` under `Serial flasher config`. Note how to create a PEM certificate for mqtt.eclipseprojects.io: From 5a8152580b5251409b25b243b9c15049b95f0da5 Mon Sep 17 00:00:00 2001 From: Roland Dobai Date: Fri, 5 Nov 2021 15:38:25 +0100 Subject: [PATCH 100/231] Build & config: Remove leftover files from the unsupported "make" build system --- components/mqtt/component.mk | 4 ---- components/mqtt/test/component.mk | 4 ---- examples/protocols/mqtt/ssl/Makefile | 9 --------- examples/protocols/mqtt/ssl/main/component.mk | 1 - examples/protocols/mqtt/ssl_mutual_auth/Makefile | 9 --------- .../protocols/mqtt/ssl_mutual_auth/main/component.mk | 1 - examples/protocols/mqtt/ssl_psk/Makefile | 9 --------- examples/protocols/mqtt/ssl_psk/main/component.mk | 0 examples/protocols/mqtt/tcp/Makefile | 9 --------- examples/protocols/mqtt/tcp/main/component.mk | 0 examples/protocols/mqtt/ws/Makefile | 9 --------- examples/protocols/mqtt/ws/main/component.mk | 0 examples/protocols/mqtt/wss/Makefile | 9 --------- examples/protocols/mqtt/wss/main/component.mk | 1 - .../protocols/mqtt/publish_connect_test/Makefile | 9 --------- .../mqtt/publish_connect_test/main/component.mk | 1 - 16 files changed, 75 deletions(-) delete mode 100644 components/mqtt/component.mk delete mode 100644 components/mqtt/test/component.mk delete mode 100644 examples/protocols/mqtt/ssl/Makefile delete mode 100644 examples/protocols/mqtt/ssl/main/component.mk delete mode 100644 examples/protocols/mqtt/ssl_mutual_auth/Makefile delete mode 100644 examples/protocols/mqtt/ssl_mutual_auth/main/component.mk delete mode 100644 examples/protocols/mqtt/ssl_psk/Makefile delete mode 100644 examples/protocols/mqtt/ssl_psk/main/component.mk delete mode 100644 examples/protocols/mqtt/tcp/Makefile delete mode 100644 examples/protocols/mqtt/tcp/main/component.mk delete mode 100644 examples/protocols/mqtt/ws/Makefile delete mode 100644 examples/protocols/mqtt/ws/main/component.mk delete mode 100644 examples/protocols/mqtt/wss/Makefile delete mode 100644 examples/protocols/mqtt/wss/main/component.mk delete mode 100644 tools/test_apps/protocols/mqtt/publish_connect_test/Makefile delete mode 100644 tools/test_apps/protocols/mqtt/publish_connect_test/main/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 diff --git a/examples/protocols/mqtt/ssl/Makefile b/examples/protocols/mqtt/ssl/Makefile deleted file mode 100644 index efe7f7d..0000000 --- a/examples/protocols/mqtt/ssl/Makefile +++ /dev/null @@ -1,9 +0,0 @@ -# -# This is a project Makefile. It is assumed the directory this Makefile resides in is a -# project subdirectory. -# -PROJECT_NAME := mqtt_ssl - -EXTRA_COMPONENT_DIRS = $(IDF_PATH)/examples/common_components/protocol_examples_common - -include $(IDF_PATH)/make/project.mk diff --git a/examples/protocols/mqtt/ssl/main/component.mk b/examples/protocols/mqtt/ssl/main/component.mk deleted file mode 100644 index c0c0f87..0000000 --- a/examples/protocols/mqtt/ssl/main/component.mk +++ /dev/null @@ -1 +0,0 @@ -COMPONENT_EMBED_TXTFILES := mqtt_eclipseprojects_io.pem diff --git a/examples/protocols/mqtt/ssl_mutual_auth/Makefile b/examples/protocols/mqtt/ssl_mutual_auth/Makefile deleted file mode 100644 index 1394f99..0000000 --- a/examples/protocols/mqtt/ssl_mutual_auth/Makefile +++ /dev/null @@ -1,9 +0,0 @@ -# -# This is a project Makefile. It is assumed the directory this Makefile resides in is a -# project subdirectory. -# -PROJECT_NAME := mqtt_ssl_mutual_auth - -EXTRA_COMPONENT_DIRS = $(IDF_PATH)/examples/common_components/protocol_examples_common - -include $(IDF_PATH)/make/project.mk diff --git a/examples/protocols/mqtt/ssl_mutual_auth/main/component.mk b/examples/protocols/mqtt/ssl_mutual_auth/main/component.mk deleted file mode 100644 index aaed44c..0000000 --- a/examples/protocols/mqtt/ssl_mutual_auth/main/component.mk +++ /dev/null @@ -1 +0,0 @@ -COMPONENT_EMBED_TXTFILES := client.crt client.key mosquitto.org.crt diff --git a/examples/protocols/mqtt/ssl_psk/Makefile b/examples/protocols/mqtt/ssl_psk/Makefile deleted file mode 100644 index 24bec17..0000000 --- a/examples/protocols/mqtt/ssl_psk/Makefile +++ /dev/null @@ -1,9 +0,0 @@ -# -# This is a project Makefile. It is assumed the directory this Makefile resides in is a -# project subdirectory. -# -PROJECT_NAME := mqtt_ssl_psk - -EXTRA_COMPONENT_DIRS = $(IDF_PATH)/examples/common_components/protocol_examples_common - -include $(IDF_PATH)/make/project.mk diff --git a/examples/protocols/mqtt/ssl_psk/main/component.mk b/examples/protocols/mqtt/ssl_psk/main/component.mk deleted file mode 100644 index e69de29..0000000 diff --git a/examples/protocols/mqtt/tcp/Makefile b/examples/protocols/mqtt/tcp/Makefile deleted file mode 100644 index 5302765..0000000 --- a/examples/protocols/mqtt/tcp/Makefile +++ /dev/null @@ -1,9 +0,0 @@ -# -# This is a project Makefile. It is assumed the directory this Makefile resides in is a -# project subdirectory. -# -PROJECT_NAME := mqtt_tcp - -EXTRA_COMPONENT_DIRS = $(IDF_PATH)/examples/common_components/protocol_examples_common - -include $(IDF_PATH)/make/project.mk diff --git a/examples/protocols/mqtt/tcp/main/component.mk b/examples/protocols/mqtt/tcp/main/component.mk deleted file mode 100644 index e69de29..0000000 diff --git a/examples/protocols/mqtt/ws/Makefile b/examples/protocols/mqtt/ws/Makefile deleted file mode 100644 index 2b97fa4..0000000 --- a/examples/protocols/mqtt/ws/Makefile +++ /dev/null @@ -1,9 +0,0 @@ -# -# This is a project Makefile. It is assumed the directory this Makefile resides in is a -# project subdirectory. -# -PROJECT_NAME := mqtt_websocket - -EXTRA_COMPONENT_DIRS = $(IDF_PATH)/examples/common_components/protocol_examples_common - -include $(IDF_PATH)/make/project.mk diff --git a/examples/protocols/mqtt/ws/main/component.mk b/examples/protocols/mqtt/ws/main/component.mk deleted file mode 100644 index e69de29..0000000 diff --git a/examples/protocols/mqtt/wss/Makefile b/examples/protocols/mqtt/wss/Makefile deleted file mode 100644 index 50ed13d..0000000 --- a/examples/protocols/mqtt/wss/Makefile +++ /dev/null @@ -1,9 +0,0 @@ -# -# This is a project Makefile. It is assumed the directory this Makefile resides in is a -# project subdirectory. -# -PROJECT_NAME := mqtt_websocket_secure - -EXTRA_COMPONENT_DIRS = $(IDF_PATH)/examples/common_components/protocol_examples_common - -include $(IDF_PATH)/make/project.mk diff --git a/examples/protocols/mqtt/wss/main/component.mk b/examples/protocols/mqtt/wss/main/component.mk deleted file mode 100644 index c0c0f87..0000000 --- a/examples/protocols/mqtt/wss/main/component.mk +++ /dev/null @@ -1 +0,0 @@ -COMPONENT_EMBED_TXTFILES := mqtt_eclipseprojects_io.pem diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/Makefile b/tools/test_apps/protocols/mqtt/publish_connect_test/Makefile deleted file mode 100644 index 49c193d..0000000 --- a/tools/test_apps/protocols/mqtt/publish_connect_test/Makefile +++ /dev/null @@ -1,9 +0,0 @@ -# -# This is a project Makefile. It is assumed the directory this Makefile resides in is a -# project subdirectory. -# -PROJECT_NAME := mqtt_publish_connect_test - -EXTRA_COMPONENT_DIRS = $(IDF_PATH)/examples/common_components/protocol_examples_common - -include $(IDF_PATH)/make/project.mk diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/main/component.mk b/tools/test_apps/protocols/mqtt/publish_connect_test/main/component.mk deleted file mode 100644 index ba6d986..0000000 --- a/tools/test_apps/protocols/mqtt/publish_connect_test/main/component.mk +++ /dev/null @@ -1 +0,0 @@ -COMPONENT_EMBED_TXTFILES := mqtt_eclipse_org.pem ../ca.crt ../ca.der ../client_pwd.key ../client_pwd.crt ../client_no_pwd.key ../client_inv.crt From 947a4f0908d8bc7b5838704d2dba3e122deb073f Mon Sep 17 00:00:00 2001 From: Darian Leung Date: Sat, 30 Oct 2021 00:48:19 +0800 Subject: [PATCH 101/231] 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 afb3c084f19e7ff296645adeb0c64d630e4b400a Mon Sep 17 00:00:00 2001 From: David Cermak Date: Mon, 4 Oct 2021 10:24:31 +0200 Subject: [PATCH 102/231] 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 98d60c9a005c843e339c76367640bc78a5c811ad Mon Sep 17 00:00:00 2001 From: David Cermak Date: Wed, 6 Oct 2021 17:52:22 +0200 Subject: [PATCH 103/231] 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 92320ebdda6185fea58b0b9c17572542ae0e3ad6 Mon Sep 17 00:00:00 2001 From: Jakob Hasse Date: Thu, 18 Nov 2021 14:27:30 +0800 Subject: [PATCH 104/231] 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 e6130a46cd1cd6aaa19cb461161bcbb746048a2d Mon Sep 17 00:00:00 2001 From: Cao Sen Miao Date: Fri, 26 Nov 2021 17:03:47 +0800 Subject: [PATCH 105/231] CI: Enable ESP8684 build stage CI on master --- tools/test_apps/protocols/mqtt/build_test/README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tools/test_apps/protocols/mqtt/build_test/README.md b/tools/test_apps/protocols/mqtt/build_test/README.md index 70fb332..6fa99ab 100644 --- a/tools/test_apps/protocols/mqtt/build_test/README.md +++ b/tools/test_apps/protocols/mqtt/build_test/README.md @@ -1,3 +1,8 @@ +| Supported Targets | ESP32 | ESP32-S2 | ESP32-S3 | ESP32-C3 | +| ----------------- | ----- | -------- | -------- | -------- | + +Not support on ESP8684 yet, waiting esp_wifi supported. TODO: IDF-3905 + # Build only test for C++ This test app ensures that calling all mqtt-client API could be called from C++ From 05231a9912ee697c0f5769489a00e6de719b0e88 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Mon, 17 Jan 2022 10:47:59 +0100 Subject: [PATCH 106/231] 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 e052687fc629f372b6f7d97d8a2013c3c81d7356 Mon Sep 17 00:00:00 2001 From: laokaiyao Date: Tue, 18 Jan 2022 10:32:56 +0800 Subject: [PATCH 107/231] esp8684: rename target to esp32c2 --- tools/test_apps/protocols/mqtt/build_test/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/test_apps/protocols/mqtt/build_test/README.md b/tools/test_apps/protocols/mqtt/build_test/README.md index 6fa99ab..f698cdf 100644 --- a/tools/test_apps/protocols/mqtt/build_test/README.md +++ b/tools/test_apps/protocols/mqtt/build_test/README.md @@ -1,7 +1,7 @@ | Supported Targets | ESP32 | ESP32-S2 | ESP32-S3 | ESP32-C3 | | ----------------- | ----- | -------- | -------- | -------- | -Not support on ESP8684 yet, waiting esp_wifi supported. TODO: IDF-3905 +Not support on ESP32-C2 yet, waiting esp_wifi supported. TODO: IDF-3905 # Build only test for C++ From c0871b5be9fd4b6bee3385e2b141be84e76d7d13 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Tue, 11 Jan 2022 10:35:42 +0100 Subject: [PATCH 108/231] ci/mqtt: Move mqtt example tests to ethernet runners --- examples/protocols/mqtt/ssl/mqtt_ssl_example_test.py | 4 ++-- examples/protocols/mqtt/ssl/sdkconfig.ci | 9 +++++++++ examples/protocols/mqtt/tcp/mqtt_tcp_example_test.py | 4 ++-- examples/protocols/mqtt/tcp/sdkconfig.ci | 9 +++++++++ examples/protocols/mqtt/ws/mqtt_ws_example_test.py | 4 ++-- examples/protocols/mqtt/ws/sdkconfig.ci | 9 +++++++++ examples/protocols/mqtt/wss/mqtt_wss_example_test.py | 4 ++-- examples/protocols/mqtt/wss/sdkconfig.ci | 9 +++++++++ 8 files changed, 44 insertions(+), 8 deletions(-) diff --git a/examples/protocols/mqtt/ssl/mqtt_ssl_example_test.py b/examples/protocols/mqtt/ssl/mqtt_ssl_example_test.py index cecb9a9..7970541 100644 --- a/examples/protocols/mqtt/ssl/mqtt_ssl_example_test.py +++ b/examples/protocols/mqtt/ssl/mqtt_ssl_example_test.py @@ -58,7 +58,7 @@ def on_message(client, userdata, msg): message_log += 'Received data:' + msg.topic + ' ' + payload + '\n' -@ttfw_idf.idf_example_test(env_tag='Example_WIFI_Protocols') +@ttfw_idf.idf_example_test(env_tag='Example_EthKitV1') def test_examples_protocol_mqtt_ssl(env, extra_data): broker_url = '' broker_port = 0 @@ -110,7 +110,7 @@ def test_examples_protocol_mqtt_ssl(env, extra_data): raise ValueError('ENV_TEST_FAILURE: Test script cannot connect to broker: {}'.format(broker_url)) dut1.start_app() try: - ip_address = dut1.expect(re.compile(r' sta ip: ([^,]+),'), timeout=30) + ip_address = dut1.expect(re.compile(r' eth ip: ([^,]+),'), timeout=30) print('Connected to AP with IP: {}'.format(ip_address)) except DUT.ExpectTimeout: print('ENV_TEST_FAILURE: Cannot connect to AP') diff --git a/examples/protocols/mqtt/ssl/sdkconfig.ci b/examples/protocols/mqtt/ssl/sdkconfig.ci index 716fb5b..e9793ec 100644 --- a/examples/protocols/mqtt/ssl/sdkconfig.ci +++ b/examples/protocols/mqtt/ssl/sdkconfig.ci @@ -11,3 +11,12 @@ CONFIG_MBEDTLS_ASYMMETRIC_CONTENT_LEN=y CONFIG_MBEDTLS_SSL_IN_CONTENT_LEN=16384 CONFIG_MBEDTLS_SSL_OUT_CONTENT_LEN=4096 CONFIG_ESP_NETIF_TCPIP_ADAPTER_COMPATIBLE_LAYER=n +CONFIG_EXAMPLE_CONNECT_ETHERNET=y +CONFIG_EXAMPLE_CONNECT_WIFI=n +CONFIG_EXAMPLE_USE_INTERNAL_ETHERNET=y +CONFIG_EXAMPLE_ETH_PHY_IP101=y +CONFIG_EXAMPLE_ETH_MDC_GPIO=23 +CONFIG_EXAMPLE_ETH_MDIO_GPIO=18 +CONFIG_EXAMPLE_ETH_PHY_RST_GPIO=5 +CONFIG_EXAMPLE_ETH_PHY_ADDR=1 +CONFIG_EXAMPLE_CONNECT_IPV6=y diff --git a/examples/protocols/mqtt/tcp/mqtt_tcp_example_test.py b/examples/protocols/mqtt/tcp/mqtt_tcp_example_test.py index 47e156b..616ca65 100644 --- a/examples/protocols/mqtt/tcp/mqtt_tcp_example_test.py +++ b/examples/protocols/mqtt/tcp/mqtt_tcp_example_test.py @@ -53,7 +53,7 @@ def mqqt_server_sketch(my_ip, port): print('server closed') -@ttfw_idf.idf_example_test(env_tag='Example_WIFI_Protocols') +@ttfw_idf.idf_example_test(env_tag='Example_EthKitV1') def test_examples_protocol_mqtt_qos1(env, extra_data): global msgid """ @@ -76,7 +76,7 @@ def test_examples_protocol_mqtt_qos1(env, extra_data): dut1.start_app() # waiting for getting the IP address try: - ip_address = dut1.expect(re.compile(r' sta ip: ([^,]+),'), timeout=30) + ip_address = dut1.expect(re.compile(r' eth ip: ([^,]+),'), timeout=30) print('Connected to AP with IP: {}'.format(ip_address)) except DUT.ExpectTimeout: raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP') diff --git a/examples/protocols/mqtt/tcp/sdkconfig.ci b/examples/protocols/mqtt/tcp/sdkconfig.ci index 43a5f63..a035c07 100644 --- a/examples/protocols/mqtt/tcp/sdkconfig.ci +++ b/examples/protocols/mqtt/tcp/sdkconfig.ci @@ -1,3 +1,12 @@ CONFIG_LOG_DEFAULT_LEVEL_DEBUG=y CONFIG_BROKER_URL="FROM_STDIN" CONFIG_ESP_NETIF_TCPIP_ADAPTER_COMPATIBLE_LAYER=n +CONFIG_EXAMPLE_CONNECT_ETHERNET=y +CONFIG_EXAMPLE_CONNECT_WIFI=n +CONFIG_EXAMPLE_USE_INTERNAL_ETHERNET=y +CONFIG_EXAMPLE_ETH_PHY_IP101=y +CONFIG_EXAMPLE_ETH_MDC_GPIO=23 +CONFIG_EXAMPLE_ETH_MDIO_GPIO=18 +CONFIG_EXAMPLE_ETH_PHY_RST_GPIO=5 +CONFIG_EXAMPLE_ETH_PHY_ADDR=1 +CONFIG_EXAMPLE_CONNECT_IPV6=y diff --git a/examples/protocols/mqtt/ws/mqtt_ws_example_test.py b/examples/protocols/mqtt/ws/mqtt_ws_example_test.py index 74f3fc8..60da603 100644 --- a/examples/protocols/mqtt/ws/mqtt_ws_example_test.py +++ b/examples/protocols/mqtt/ws/mqtt_ws_example_test.py @@ -39,7 +39,7 @@ def on_message(client, userdata, msg): message_log += 'Received data:' + msg.topic + ' ' + payload + '\n' -@ttfw_idf.idf_example_test(env_tag='Example_WIFI_Protocols') +@ttfw_idf.idf_example_test(env_tag='Example_EthKitV1') def test_examples_protocol_mqtt_ws(env, extra_data): broker_url = '' broker_port = 0 @@ -83,7 +83,7 @@ def test_examples_protocol_mqtt_ws(env, extra_data): raise ValueError('ENV_TEST_FAILURE: Test script cannot connect to broker: {}'.format(broker_url)) dut1.start_app() try: - ip_address = dut1.expect(re.compile(r' sta ip: ([^,]+),'), timeout=30) + ip_address = dut1.expect(re.compile(r' eth ip: ([^,]+),'), timeout=30) print('Connected to AP with IP: {}'.format(ip_address)) except DUT.ExpectTimeout: print('ENV_TEST_FAILURE: Cannot connect to AP') diff --git a/examples/protocols/mqtt/ws/sdkconfig.ci b/examples/protocols/mqtt/ws/sdkconfig.ci index caa4f0c..1a0c919 100644 --- a/examples/protocols/mqtt/ws/sdkconfig.ci +++ b/examples/protocols/mqtt/ws/sdkconfig.ci @@ -1,2 +1,11 @@ CONFIG_BROKER_URI="ws://${EXAMPLE_MQTT_BROKER_WS}/ws" CONFIG_ESP_NETIF_TCPIP_ADAPTER_COMPATIBLE_LAYER=n +CONFIG_EXAMPLE_CONNECT_ETHERNET=y +CONFIG_EXAMPLE_CONNECT_WIFI=n +CONFIG_EXAMPLE_USE_INTERNAL_ETHERNET=y +CONFIG_EXAMPLE_ETH_PHY_IP101=y +CONFIG_EXAMPLE_ETH_MDC_GPIO=23 +CONFIG_EXAMPLE_ETH_MDIO_GPIO=18 +CONFIG_EXAMPLE_ETH_PHY_RST_GPIO=5 +CONFIG_EXAMPLE_ETH_PHY_ADDR=1 +CONFIG_EXAMPLE_CONNECT_IPV6=y diff --git a/examples/protocols/mqtt/wss/mqtt_wss_example_test.py b/examples/protocols/mqtt/wss/mqtt_wss_example_test.py index 0b1fbf5..a7825e8 100644 --- a/examples/protocols/mqtt/wss/mqtt_wss_example_test.py +++ b/examples/protocols/mqtt/wss/mqtt_wss_example_test.py @@ -40,7 +40,7 @@ def on_message(client, userdata, msg): message_log += 'Received data:' + msg.topic + ' ' + payload + '\n' -@ttfw_idf.idf_example_test(env_tag='Example_WIFI_Protocols') +@ttfw_idf.idf_example_test(env_tag='Example_EthKitV1') def test_examples_protocol_mqtt_wss(env, extra_data): broker_url = '' broker_port = 0 @@ -87,7 +87,7 @@ def test_examples_protocol_mqtt_wss(env, extra_data): raise ValueError('ENV_TEST_FAILURE: Test script cannot connect to broker: {}'.format(broker_url)) dut1.start_app() try: - ip_address = dut1.expect(re.compile(r' sta ip: ([^,]+),'), timeout=30) + ip_address = dut1.expect(re.compile(r' eth ip: ([^,]+),'), timeout=30) print('Connected to AP with IP: {}'.format(ip_address)) except DUT.ExpectTimeout: print('ENV_TEST_FAILURE: Cannot connect to AP') diff --git a/examples/protocols/mqtt/wss/sdkconfig.ci b/examples/protocols/mqtt/wss/sdkconfig.ci index d088025..1a91b85 100644 --- a/examples/protocols/mqtt/wss/sdkconfig.ci +++ b/examples/protocols/mqtt/wss/sdkconfig.ci @@ -1,3 +1,12 @@ CONFIG_BROKER_URI="wss://${EXAMPLE_MQTT_BROKER_WSS}/ws" CONFIG_BROKER_CERTIFICATE_OVERRIDE="${EXAMPLE_MQTT_BROKER_CERTIFICATE}" CONFIG_ESP_NETIF_TCPIP_ADAPTER_COMPATIBLE_LAYER=n +CONFIG_EXAMPLE_CONNECT_ETHERNET=y +CONFIG_EXAMPLE_CONNECT_WIFI=n +CONFIG_EXAMPLE_USE_INTERNAL_ETHERNET=y +CONFIG_EXAMPLE_ETH_PHY_IP101=y +CONFIG_EXAMPLE_ETH_MDC_GPIO=23 +CONFIG_EXAMPLE_ETH_MDIO_GPIO=18 +CONFIG_EXAMPLE_ETH_PHY_RST_GPIO=5 +CONFIG_EXAMPLE_ETH_PHY_ADDR=1 +CONFIG_EXAMPLE_CONNECT_IPV6=y From d649af04a8d8dbc62a0667badeb75164b5fca131 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Tue, 11 Jan 2022 10:37:50 +0100 Subject: [PATCH 109/231] ci/mqtt: Move publish-connect test to ethernet runners --- .../protocols/mqtt/publish_connect_test/app_test.py | 2 +- .../mqtt/publish_connect_test/sdkconfig.ci.default | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/app_test.py b/tools/test_apps/protocols/mqtt/publish_connect_test/app_test.py index 2a0e2b5..bb4e9dc 100644 --- a/tools/test_apps/protocols/mqtt/publish_connect_test/app_test.py +++ b/tools/test_apps/protocols/mqtt/publish_connect_test/app_test.py @@ -307,7 +307,7 @@ def connection_tests(dut, cases): teardown_connection_suite() -@ttfw_idf.idf_custom_test(env_tag='Example_WIFI', group='test-apps') +@ttfw_idf.idf_custom_test(env_tag='Example_EthKitV1', group='test-apps') def test_app_protocol_mqtt_publish_connect(env, extra_data): """ steps: diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/sdkconfig.ci.default b/tools/test_apps/protocols/mqtt/publish_connect_test/sdkconfig.ci.default index fbfe696..5c3b80a 100644 --- a/tools/test_apps/protocols/mqtt/publish_connect_test/sdkconfig.ci.default +++ b/tools/test_apps/protocols/mqtt/publish_connect_test/sdkconfig.ci.default @@ -9,3 +9,12 @@ CONFIG_MBEDTLS_SSL_OUT_CONTENT_LEN=16384 CONFIG_ESP_NETIF_TCPIP_ADAPTER_COMPATIBLE_LAYER=n CONFIG_ESP_TLS_INSECURE=y CONFIG_ESP_TLS_SKIP_SERVER_CERT_VERIFY=y +CONFIG_EXAMPLE_CONNECT_ETHERNET=y +CONFIG_EXAMPLE_CONNECT_WIFI=n +CONFIG_EXAMPLE_USE_INTERNAL_ETHERNET=y +CONFIG_EXAMPLE_ETH_PHY_IP101=y +CONFIG_EXAMPLE_ETH_MDC_GPIO=23 +CONFIG_EXAMPLE_ETH_MDIO_GPIO=18 +CONFIG_EXAMPLE_ETH_PHY_RST_GPIO=5 +CONFIG_EXAMPLE_ETH_PHY_ADDR=1 +CONFIG_EXAMPLE_CONNECT_IPV6=y From 0fe8646fd40331c3273e7bb93154adb42a415ff2 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Wed, 26 Jan 2022 16:38:31 +0100 Subject: [PATCH 110/231] ci/mqtt: Fix weekend test publish-connect on target * Fix thread safe issue in paho-mqtt library * Move the weekend test to ethernet runner --- .../mqtt/publish_connect_test/app_test.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/app_test.py b/tools/test_apps/protocols/mqtt/publish_connect_test/app_test.py index bb4e9dc..cf4c612 100644 --- a/tools/test_apps/protocols/mqtt/publish_connect_test/app_test.py +++ b/tools/test_apps/protocols/mqtt/publish_connect_test/app_test.py @@ -9,7 +9,8 @@ import ssl import string import subprocess import sys -from threading import Event, Thread +import time +from threading import Event, Lock, Thread import paho.mqtt.client as mqtt import ttfw_idf @@ -59,6 +60,7 @@ class MqttPublisher: self.publish_cfg['qos'] = qos self.publish_cfg['queue'] = queue self.publish_cfg['transport'] = transport + self.lock = Lock() # static variables used to pass options to and from static callbacks of paho-mqtt client MqttPublisher.event_client_connected = Event() MqttPublisher.event_client_got_all = Event() @@ -71,9 +73,11 @@ class MqttPublisher: if self.log_details: print(text) - def mqtt_client_task(self, client): + def mqtt_client_task(self, client, lock): while not self.event_stop_client.is_set(): - client.loop() + with lock: + client.loop() + time.sleep(0.001) # yield to other threads # The callback for when the client receives a CONNACK response from the server (needs to be static) @staticmethod @@ -120,18 +124,20 @@ class MqttPublisher: self.print_details('ENV_TEST_FAILURE: Unexpected error while connecting to broker {}'.format(broker_host)) raise # Starting a py-client in a separate thread - thread1 = Thread(target=self.mqtt_client_task, args=(self.client,)) + thread1 = Thread(target=self.mqtt_client_task, args=(self.client, self.lock)) thread1.start() self.print_details('Connecting py-client to broker {}:{}...'.format(broker_host, broker_port)) if not MqttPublisher.event_client_connected.wait(timeout=30): raise ValueError('ENV_TEST_FAILURE: Test script cannot connect to broker: {}'.format(broker_host)) - self.client.subscribe(self.publish_cfg['subscribe_topic'], qos) + with self.lock: + self.client.subscribe(self.publish_cfg['subscribe_topic'], qos) self.dut.write(' '.join(str(x) for x in (transport, self.sample_string, self.repeat, MqttPublisher.published, qos, queue)), eol='\n') try: # waiting till subscribed to defined topic self.dut.expect(re.compile(r'MQTT_EVENT_SUBSCRIBED'), timeout=30) for _ in range(MqttPublisher.published): - self.client.publish(self.publish_cfg['publish_topic'], self.sample_string * self.repeat, qos) + with self.lock: + self.client.publish(self.publish_cfg['publish_topic'], self.sample_string * self.repeat, qos) self.print_details('Publishing...') self.print_details('Checking esp-client received msg published from py-client...') self.dut.expect(re.compile(r'Correct pattern received exactly x times'), timeout=60) From 07f7376d1efde9561ab5dd87f7de4fa1633cec6c Mon Sep 17 00:00:00 2001 From: David Cermak Date: Thu, 27 Jan 2022 07:43:05 +0100 Subject: [PATCH 111/231] ci/mqtt: Make publish test-app message properties configurable Also increase the default sizes and repeat-counts to send more data and exercise the library more intensly --- .../mqtt/publish_connect_test/app_test.py | 24 +++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/app_test.py b/tools/test_apps/protocols/mqtt/publish_connect_test/app_test.py index cf4c612..da22edb 100644 --- a/tools/test_apps/protocols/mqtt/publish_connect_test/app_test.py +++ b/tools/test_apps/protocols/mqtt/publish_connect_test/app_test.py @@ -10,6 +10,7 @@ import string import subprocess import sys import time +from itertools import count from threading import Event, Lock, Thread import paho.mqtt.client as mqtt @@ -383,16 +384,31 @@ def test_app_protocol_mqtt_publish_connect(env, extra_data): with MqttPublisher(dut1, transport, qos, repeat, published, queue, publish_cfg): pass + # Initialize message sizes and repeat counts (if defined in the environment) + messages = [] + for i in count(0): + # Check env variable: MQTT_PUBLISH_MSG_{len|repeat}_{x} + env_dict = {var:'MQTT_PUBLISH_MSG_' + var + '_' + str(i) for var in ['len', 'repeat']} + if os.getenv(env_dict['len']) and os.getenv(env_dict['repeat']): + messages.append({var: int(os.getenv(env_dict[var])) for var in ['len', 'repeat']}) + continue + break + if not messages: # No message sizes present in the env - set defaults + messages = [{'len':0, 'repeat':5}, # zero-sized messages + {'len':2, 'repeat':10}, # short messages + {'len':200, 'repeat':3}, # long messages + {'len':20, 'repeat':50} # many medium sized + ] + + # Iterate over all publish message properties for qos in [0, 1, 2]: for transport in ['tcp', 'ssl', 'ws', 'wss']: for q in [0, 1]: if publish_cfg['broker_host_' + transport] is None: print('Skipping transport: {}...'.format(transport)) continue - start_publish_case(transport, qos, 0, 5, q) - start_publish_case(transport, qos, 2, 5, q) - start_publish_case(transport, qos, 50, 1, q) - start_publish_case(transport, qos, 10, 20, q) + for msg in messages: + start_publish_case(transport, qos, msg['len'], msg['repeat'], q) if __name__ == '__main__': From 841e8d7c602adb268d2190fbb6123d01b4c865ec Mon Sep 17 00:00:00 2001 From: Aditya Patwardhan Date: Mon, 31 Jan 2022 14:11:07 +0530 Subject: [PATCH 112/231] configure_ds.py: Fix the error for prod mode. They script now only verifies the purpose in the prod mode. Closes https://github.com/espressif/esp-idf/issues/8285 --- .../protocols/mqtt/ssl_ds/configure_ds.py | 46 +++++++++---------- 1 file changed, 22 insertions(+), 24 deletions(-) diff --git a/examples/protocols/mqtt/ssl_ds/configure_ds.py b/examples/protocols/mqtt/ssl_ds/configure_ds.py index 44f2c9e..d847db2 100644 --- a/examples/protocols/mqtt/ssl_ds/configure_ds.py +++ b/examples/protocols/mqtt/ssl_ds/configure_ds.py @@ -1,16 +1,6 @@ #!/usr/bin/env python -# Copyright 2020 Espressif Systems (Shanghai) Co., Ltd. -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. +# SPDX-FileCopyrightText: 2020-2022 Espressif Systems (Shanghai) CO LTD +# SPDX-License-Identifier: Apache-2.0 import argparse import hashlib import hmac @@ -263,18 +253,26 @@ def configure_efuse_key_block(args, idf_target): key_file.write(new_hmac_key) # Burn efuse key efuse_burn_key(args, idf_target) - # Read fresh summary of the efuse to read the key value from efuse. - # If the key read from efuse matches with the key generated - # on host then burn_key operation was successfull - new_efuse_summary_json = get_efuse_summary_json(args, idf_target) - hmac_key_read = new_efuse_summary_json[key_blk]['value'] - hmac_key_read = bytes.fromhex(hmac_key_read) - if new_hmac_key == hmac_key_read: - print('Key was successfully written to the efuse (KEY BLOCK %1d)' % (args.efuse_key_id)) + if args.production is False: + # Read fresh summary of the efuse to read the key value from efuse. + # If the key read from efuse matches with the key generated + # on host then burn_key operation was successfull + new_efuse_summary_json = get_efuse_summary_json(args, idf_target) + hmac_key_read = new_efuse_summary_json[key_blk]['value'] + print(hmac_key_read) + hmac_key_read = bytes.fromhex(hmac_key_read) + if new_hmac_key == hmac_key_read: + print('Key was successfully written to the efuse (KEY BLOCK %1d)' % (args.efuse_key_id)) + else: + print('ERROR: Failed to burn the hmac key to efuse (KEY BLOCK %1d),' + '\nPlease execute the script again using a different key id' % (args.efuse_key_id)) + return None else: - print('ERROR: Failed to burn the hmac key to efuse (KEY BLOCK %1d),' - '\nPlease execute the script again using a different key id' % (args.efuse_key_id)) - return None + new_efuse_summary_json = get_efuse_summary_json(args, idf_target) + if new_efuse_summary_json[key_purpose]['value'] != 'HMAC_DOWN_DIGITAL_SIGNATURE': + print('ERROR: Failed to verify the key purpose of the key block{})'.format(args.efuse_key_id)) + return None + hmac_key_read = new_hmac_key else: # If the efuse key block is redable, then read the key from efuse block and use it for encrypting the RSA private key parameters. # If the efuse key block is not redable or it has key purpose set to a different @@ -297,7 +295,7 @@ def configure_efuse_key_block(args, idf_target): '\nplease execute the script again with a different value of the efuse key id.' % (args.efuse_key_id)) return None - # Return the hmac key read from the efuse + # Return the hmac key burned into the efuse return hmac_key_read From 64993a3270b4992f67cb57585577520d2561c9f8 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Mon, 24 Jan 2022 15:40:11 +0100 Subject: [PATCH 113/231] 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 c1bc52fa313d2d32936dad90eada90de39166f5b Mon Sep 17 00:00:00 2001 From: Darian Leung Date: Thu, 17 Feb 2022 16:40:52 +0800 Subject: [PATCH 114/231] 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 af6b3bb9febb68c2fe0fa1900f546c1933bd8dac Mon Sep 17 00:00:00 2001 From: Sudeep Mohanty Date: Wed, 12 Jan 2022 12:23:47 +0530 Subject: [PATCH 115/231] 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/test/test_mqtt.c | 3 ++- components/mqtt/test/test_mqtt_client_broker.c | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) 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 3a3457eb6658369a07ba9a31e553f32564f834ba Mon Sep 17 00:00:00 2001 From: Sagar Bijwe Date: Thu, 2 Dec 2021 15:49:28 +0530 Subject: [PATCH 116/231] 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 b390e07833e04f2f3ad377f089ac42ada8748743 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Tue, 8 Feb 2022 17:08:04 +0100 Subject: [PATCH 117/231] esp_netif: Remove tcpip_adapter compatibility layer --- examples/protocols/mqtt/ssl/sdkconfig.ci | 1 - examples/protocols/mqtt/tcp/sdkconfig.ci | 1 - examples/protocols/mqtt/ws/sdkconfig.ci | 1 - examples/protocols/mqtt/wss/sdkconfig.ci | 1 - .../protocols/mqtt/publish_connect_test/sdkconfig.ci.default | 1 - 5 files changed, 5 deletions(-) diff --git a/examples/protocols/mqtt/ssl/sdkconfig.ci b/examples/protocols/mqtt/ssl/sdkconfig.ci index e9793ec..3271d96 100644 --- a/examples/protocols/mqtt/ssl/sdkconfig.ci +++ b/examples/protocols/mqtt/ssl/sdkconfig.ci @@ -10,7 +10,6 @@ CONFIG_MQTT_TASK_STACK_SIZE=6144 CONFIG_MBEDTLS_ASYMMETRIC_CONTENT_LEN=y CONFIG_MBEDTLS_SSL_IN_CONTENT_LEN=16384 CONFIG_MBEDTLS_SSL_OUT_CONTENT_LEN=4096 -CONFIG_ESP_NETIF_TCPIP_ADAPTER_COMPATIBLE_LAYER=n CONFIG_EXAMPLE_CONNECT_ETHERNET=y CONFIG_EXAMPLE_CONNECT_WIFI=n CONFIG_EXAMPLE_USE_INTERNAL_ETHERNET=y diff --git a/examples/protocols/mqtt/tcp/sdkconfig.ci b/examples/protocols/mqtt/tcp/sdkconfig.ci index a035c07..28ce9ac 100644 --- a/examples/protocols/mqtt/tcp/sdkconfig.ci +++ b/examples/protocols/mqtt/tcp/sdkconfig.ci @@ -1,6 +1,5 @@ CONFIG_LOG_DEFAULT_LEVEL_DEBUG=y CONFIG_BROKER_URL="FROM_STDIN" -CONFIG_ESP_NETIF_TCPIP_ADAPTER_COMPATIBLE_LAYER=n CONFIG_EXAMPLE_CONNECT_ETHERNET=y CONFIG_EXAMPLE_CONNECT_WIFI=n CONFIG_EXAMPLE_USE_INTERNAL_ETHERNET=y diff --git a/examples/protocols/mqtt/ws/sdkconfig.ci b/examples/protocols/mqtt/ws/sdkconfig.ci index 1a0c919..2f8e425 100644 --- a/examples/protocols/mqtt/ws/sdkconfig.ci +++ b/examples/protocols/mqtt/ws/sdkconfig.ci @@ -1,5 +1,4 @@ CONFIG_BROKER_URI="ws://${EXAMPLE_MQTT_BROKER_WS}/ws" -CONFIG_ESP_NETIF_TCPIP_ADAPTER_COMPATIBLE_LAYER=n CONFIG_EXAMPLE_CONNECT_ETHERNET=y CONFIG_EXAMPLE_CONNECT_WIFI=n CONFIG_EXAMPLE_USE_INTERNAL_ETHERNET=y diff --git a/examples/protocols/mqtt/wss/sdkconfig.ci b/examples/protocols/mqtt/wss/sdkconfig.ci index 1a91b85..f355614 100644 --- a/examples/protocols/mqtt/wss/sdkconfig.ci +++ b/examples/protocols/mqtt/wss/sdkconfig.ci @@ -1,6 +1,5 @@ CONFIG_BROKER_URI="wss://${EXAMPLE_MQTT_BROKER_WSS}/ws" CONFIG_BROKER_CERTIFICATE_OVERRIDE="${EXAMPLE_MQTT_BROKER_CERTIFICATE}" -CONFIG_ESP_NETIF_TCPIP_ADAPTER_COMPATIBLE_LAYER=n CONFIG_EXAMPLE_CONNECT_ETHERNET=y CONFIG_EXAMPLE_CONNECT_WIFI=n CONFIG_EXAMPLE_USE_INTERNAL_ETHERNET=y diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/sdkconfig.ci.default b/tools/test_apps/protocols/mqtt/publish_connect_test/sdkconfig.ci.default index 5c3b80a..25a7c80 100644 --- a/tools/test_apps/protocols/mqtt/publish_connect_test/sdkconfig.ci.default +++ b/tools/test_apps/protocols/mqtt/publish_connect_test/sdkconfig.ci.default @@ -6,7 +6,6 @@ CONFIG_EXAMPLE_BROKER_CERTIFICATE_OVERRIDE="${EXAMPLE_MQTT_BROKER_CERTIFICATE}" CONFIG_MBEDTLS_ASYMMETRIC_CONTENT_LEN=y CONFIG_MBEDTLS_SSL_IN_CONTENT_LEN=16384 CONFIG_MBEDTLS_SSL_OUT_CONTENT_LEN=16384 -CONFIG_ESP_NETIF_TCPIP_ADAPTER_COMPATIBLE_LAYER=n CONFIG_ESP_TLS_INSECURE=y CONFIG_ESP_TLS_SKIP_SERVER_CERT_VERIFY=y CONFIG_EXAMPLE_CONNECT_ETHERNET=y From 6b188eab14dd7ab9f8ffa2fab631b995aa8a17b3 Mon Sep 17 00:00:00 2001 From: Laukik Hase Date: Thu, 10 Mar 2022 14:26:37 +0530 Subject: [PATCH 118/231] 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 0edf4bee237314955f572422facb78c15ad28847 Mon Sep 17 00:00:00 2001 From: Anton Maklakov Date: Tue, 22 Feb 2022 12:42:56 +0700 Subject: [PATCH 119/231] 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 537b53ad60270cdc027f73e40e88bf19d9997681 Mon Sep 17 00:00:00 2001 From: Mahavir Jain Date: Thu, 24 Mar 2022 21:05:05 +0530 Subject: [PATCH 120/231] docs: Add esp_tls_errors.h to API reference list Closes DOC-2797 --- docs/en/api-reference/protocols/mqtt.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/en/api-reference/protocols/mqtt.rst b/docs/en/api-reference/protocols/mqtt.rst index 7d00a03..5b84107 100644 --- a/docs/en/api-reference/protocols/mqtt.rst +++ b/docs/en/api-reference/protocols/mqtt.rst @@ -85,7 +85,7 @@ SSL If the certificate is not null-terminated then ``cert_len`` should also be set. Other SSL related configuration parameters are: - * ``use_global_ca_store``: use the global certificate store to verify server certificate, see ``esp-tls.h`` for more information + * ``use_global_ca_store``: use the global certificate store to verify server certificate, see :component_file:`esp-tls/esp_tls.h` for more information * ``client_cert_pem``: pointer to certificate data in PEM or DER format for SSL mutual authentication, default is NULL, not required if mutual authentication is not needed. * ``client_cert_len``: length of the buffer pointed to by client_cert_pem. May be 0 for null-terminated pem. * ``client_key_pem``: pointer to private key data in PEM or DER format for SSL mutual authentication, default is NULL, not required if mutual authentication is not needed. From b2e3cc620a7f0b1cb765240c19a92e2c41593d54 Mon Sep 17 00:00:00 2001 From: Jessy Chen Date: Tue, 10 May 2022 04:00:01 -0400 Subject: [PATCH 121/231] esp_wifi: optimize wifi kconfig --- tools/test_apps/protocols/mqtt/build_test/README.md | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/tools/test_apps/protocols/mqtt/build_test/README.md b/tools/test_apps/protocols/mqtt/build_test/README.md index f698cdf..0f6c3a9 100644 --- a/tools/test_apps/protocols/mqtt/build_test/README.md +++ b/tools/test_apps/protocols/mqtt/build_test/README.md @@ -1,7 +1,5 @@ -| Supported Targets | ESP32 | ESP32-S2 | ESP32-S3 | ESP32-C3 | -| ----------------- | ----- | -------- | -------- | -------- | - -Not support on ESP32-C2 yet, waiting esp_wifi supported. TODO: IDF-3905 +| Supported Targets | ESP32 | ESP32-S2 | ESP32-S3 | ESP32-C3 | ESP32-C2 | +| ----------------- | ----- | -------- | -------- | -------- | -------- | # Build only test for C++ From 37ff11d0edb0f61dc9a833fa1c1b8ace52d2090d Mon Sep 17 00:00:00 2001 From: Djordje Nedic Date: Fri, 27 May 2022 10:10:51 +0200 Subject: [PATCH 122/231] 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 +- examples/protocols/mqtt/ssl/CMakeLists.txt | 2 +- examples/protocols/mqtt/ssl_ds/CMakeLists.txt | 2 +- examples/protocols/mqtt/ssl_mutual_auth/CMakeLists.txt | 2 +- examples/protocols/mqtt/ssl_psk/CMakeLists.txt | 2 +- examples/protocols/mqtt/tcp/CMakeLists.txt | 2 +- examples/protocols/mqtt/ws/CMakeLists.txt | 2 +- examples/protocols/mqtt/wss/CMakeLists.txt | 2 +- tools/test_apps/protocols/mqtt/build_test/CMakeLists.txt | 2 +- .../protocols/mqtt/publish_connect_test/CMakeLists.txt | 2 +- 10 files changed, 10 insertions(+), 10 deletions(-) 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) diff --git a/examples/protocols/mqtt/ssl/CMakeLists.txt b/examples/protocols/mqtt/ssl/CMakeLists.txt index bb945b4..3d2fba1 100644 --- a/examples/protocols/mqtt/ssl/CMakeLists.txt +++ b/examples/protocols/mqtt/ssl/CMakeLists.txt @@ -1,6 +1,6 @@ # The following four lines of boilerplate have to be in your project's CMakeLists # in this exact order for cmake to work correctly -cmake_minimum_required(VERSION 3.5) +cmake_minimum_required(VERSION 3.16) # (Not part of the boilerplate) # This example uses an extra component for common functions such as Wi-Fi and Ethernet connection. diff --git a/examples/protocols/mqtt/ssl_ds/CMakeLists.txt b/examples/protocols/mqtt/ssl_ds/CMakeLists.txt index 974fc2d..c45c2a0 100644 --- a/examples/protocols/mqtt/ssl_ds/CMakeLists.txt +++ b/examples/protocols/mqtt/ssl_ds/CMakeLists.txt @@ -1,6 +1,6 @@ # The following four lines of boilerplate have to be in your project's CMakeLists # in this exact order for cmake to work correctly -cmake_minimum_required(VERSION 3.5) +cmake_minimum_required(VERSION 3.16) # (Not part of the boilerplate) # This example uses an extra component for common functions such as Wi-Fi and Ethernet connection. diff --git a/examples/protocols/mqtt/ssl_mutual_auth/CMakeLists.txt b/examples/protocols/mqtt/ssl_mutual_auth/CMakeLists.txt index c392535..14512ad 100644 --- a/examples/protocols/mqtt/ssl_mutual_auth/CMakeLists.txt +++ b/examples/protocols/mqtt/ssl_mutual_auth/CMakeLists.txt @@ -1,6 +1,6 @@ # The following four lines of boilerplate have to be in your project's CMakeLists # in this exact order for cmake to work correctly -cmake_minimum_required(VERSION 3.5) +cmake_minimum_required(VERSION 3.16) # (Not part of the boilerplate) # This example uses an extra component for common functions such as Wi-Fi and Ethernet connection. diff --git a/examples/protocols/mqtt/ssl_psk/CMakeLists.txt b/examples/protocols/mqtt/ssl_psk/CMakeLists.txt index 77934fa..a4e8aa6 100644 --- a/examples/protocols/mqtt/ssl_psk/CMakeLists.txt +++ b/examples/protocols/mqtt/ssl_psk/CMakeLists.txt @@ -1,6 +1,6 @@ # The following four lines of boilerplate have to be in your project's CMakeLists # in this exact order for cmake to work correctly -cmake_minimum_required(VERSION 3.5) +cmake_minimum_required(VERSION 3.16) # (Not part of the boilerplate) # This example uses an extra component for common functions such as Wi-Fi and Ethernet connection. diff --git a/examples/protocols/mqtt/tcp/CMakeLists.txt b/examples/protocols/mqtt/tcp/CMakeLists.txt index 3dd15c0..7dd960d 100644 --- a/examples/protocols/mqtt/tcp/CMakeLists.txt +++ b/examples/protocols/mqtt/tcp/CMakeLists.txt @@ -1,6 +1,6 @@ # The following four lines of boilerplate have to be in your project's CMakeLists # in this exact order for cmake to work correctly -cmake_minimum_required(VERSION 3.5) +cmake_minimum_required(VERSION 3.16) # (Not part of the boilerplate) # This example uses an extra component for common functions such as Wi-Fi and Ethernet connection. diff --git a/examples/protocols/mqtt/ws/CMakeLists.txt b/examples/protocols/mqtt/ws/CMakeLists.txt index f049006..6c84eb7 100644 --- a/examples/protocols/mqtt/ws/CMakeLists.txt +++ b/examples/protocols/mqtt/ws/CMakeLists.txt @@ -1,6 +1,6 @@ # The following four lines of boilerplate have to be in your project's CMakeLists # in this exact order for cmake to work correctly -cmake_minimum_required(VERSION 3.5) +cmake_minimum_required(VERSION 3.16) # (Not part of the boilerplate) # This example uses an extra component for common functions such as Wi-Fi and Ethernet connection. diff --git a/examples/protocols/mqtt/wss/CMakeLists.txt b/examples/protocols/mqtt/wss/CMakeLists.txt index 2534732..2a62440 100644 --- a/examples/protocols/mqtt/wss/CMakeLists.txt +++ b/examples/protocols/mqtt/wss/CMakeLists.txt @@ -1,6 +1,6 @@ # The following four lines of boilerplate have to be in your project's CMakeLists # in this exact order for cmake to work correctly -cmake_minimum_required(VERSION 3.5) +cmake_minimum_required(VERSION 3.16) # (Not part of the boilerplate) # This example uses an extra component for common functions such as Wi-Fi and Ethernet connection. diff --git a/tools/test_apps/protocols/mqtt/build_test/CMakeLists.txt b/tools/test_apps/protocols/mqtt/build_test/CMakeLists.txt index 3dd15c0..7dd960d 100644 --- a/tools/test_apps/protocols/mqtt/build_test/CMakeLists.txt +++ b/tools/test_apps/protocols/mqtt/build_test/CMakeLists.txt @@ -1,6 +1,6 @@ # The following four lines of boilerplate have to be in your project's CMakeLists # in this exact order for cmake to work correctly -cmake_minimum_required(VERSION 3.5) +cmake_minimum_required(VERSION 3.16) # (Not part of the boilerplate) # This example uses an extra component for common functions such as Wi-Fi and Ethernet connection. diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/CMakeLists.txt b/tools/test_apps/protocols/mqtt/publish_connect_test/CMakeLists.txt index 6fcb74b..ee3e421 100644 --- a/tools/test_apps/protocols/mqtt/publish_connect_test/CMakeLists.txt +++ b/tools/test_apps/protocols/mqtt/publish_connect_test/CMakeLists.txt @@ -1,6 +1,6 @@ # The following four lines of boilerplate have to be in your project's CMakeLists # in this exact order for cmake to work correctly -cmake_minimum_required(VERSION 3.5) +cmake_minimum_required(VERSION 3.16) # (Not part of the boilerplate) # This example uses an extra component for common functions such as Wi-Fi and Ethernet connection. From c2d902062ef3bdc89d5c9b2a4168d98cc8f540fc Mon Sep 17 00:00:00 2001 From: "Michael (XIAO Xufeng)" Date: Fri, 20 May 2022 17:50:08 +0800 Subject: [PATCH 123/231] ci: partially enable example build for esp32c2 --- examples/protocols/mqtt/ssl/README.md | 3 +++ examples/protocols/mqtt/ssl_mutual_auth/README.md | 3 +++ examples/protocols/mqtt/ssl_psk/README.md | 9 ++++++--- examples/protocols/mqtt/tcp/README.md | 3 +++ examples/protocols/mqtt/ws/README.md | 3 +++ examples/protocols/mqtt/wss/README.md | 3 +++ 6 files changed, 21 insertions(+), 3 deletions(-) diff --git a/examples/protocols/mqtt/ssl/README.md b/examples/protocols/mqtt/ssl/README.md index 754c693..a87f208 100644 --- a/examples/protocols/mqtt/ssl/README.md +++ b/examples/protocols/mqtt/ssl/README.md @@ -1,3 +1,6 @@ +| Supported Targets | ESP32 | ESP32-S2 | ESP32-S3 | ESP32-C3 | +| ----------------- | ----- | -------- | -------- | -------- | + # ESP-MQTT SSL Sample application (See the README.md file in the upper level 'examples' directory for more information about examples.) diff --git a/examples/protocols/mqtt/ssl_mutual_auth/README.md b/examples/protocols/mqtt/ssl_mutual_auth/README.md index 74beb89..be0c9b6 100644 --- a/examples/protocols/mqtt/ssl_mutual_auth/README.md +++ b/examples/protocols/mqtt/ssl_mutual_auth/README.md @@ -1,3 +1,6 @@ +| Supported Targets | ESP32 | ESP32-S2 | ESP32-S3 | ESP32-C3 | +| ----------------- | ----- | -------- | -------- | -------- | + # ESP-MQTT SSL Sample application (mutual authentication) (See the README.md file in the upper level 'examples' directory for more information about examples.) diff --git a/examples/protocols/mqtt/ssl_psk/README.md b/examples/protocols/mqtt/ssl_psk/README.md index 486fc5e..cec38fe 100644 --- a/examples/protocols/mqtt/ssl_psk/README.md +++ b/examples/protocols/mqtt/ssl_psk/README.md @@ -1,4 +1,7 @@ -# ESP-MQTT SSL example with PSK verification +| Supported Targets | ESP32 | ESP32-S2 | ESP32-S3 | ESP32-C3 | +| ----------------- | ----- | -------- | -------- | -------- | + +# ESP-MQTT SSL example with PSK verification (See the README.md file in the upper level 'examples' directory for more information about examples.) @@ -8,11 +11,11 @@ This example connects to a local broker configured to PSK authentication ### Hardware Required -This example can be executed on any ESP32 board, the only required interface is WiFi (or ethernet) to connect to a MQTT +This example can be executed on any ESP32 board, the only required interface is WiFi (or ethernet) to connect to a MQTT broker with preconfigured PSK verification method. #### Mosquitto settings -In case of using mosquitto broker, here is how to enable PSK authentication in `mosquitto.config`, +In case of using mosquitto broker, here is how to enable PSK authentication in `mosquitto.config`, ``` psk_hint hint psk_file path_to_your_psk_file diff --git a/examples/protocols/mqtt/tcp/README.md b/examples/protocols/mqtt/tcp/README.md index e12a161..be6d70a 100644 --- a/examples/protocols/mqtt/tcp/README.md +++ b/examples/protocols/mqtt/tcp/README.md @@ -1,3 +1,6 @@ +| Supported Targets | ESP32 | ESP32-S2 | ESP32-S3 | ESP32-C3 | +| ----------------- | ----- | -------- | -------- | -------- | + # ESP-MQTT sample application (See the README.md file in the upper level 'examples' directory for more information about examples.) diff --git a/examples/protocols/mqtt/ws/README.md b/examples/protocols/mqtt/ws/README.md index 2bceb3c..c2620b3 100644 --- a/examples/protocols/mqtt/ws/README.md +++ b/examples/protocols/mqtt/ws/README.md @@ -1,3 +1,6 @@ +| Supported Targets | ESP32 | ESP32-S2 | ESP32-S3 | ESP32-C3 | +| ----------------- | ----- | -------- | -------- | -------- | + # ESP-MQTT MQTT over Websocket (See the README.md file in the upper level 'examples' directory for more information about examples.) diff --git a/examples/protocols/mqtt/wss/README.md b/examples/protocols/mqtt/wss/README.md index fb4b6c1..f836920 100644 --- a/examples/protocols/mqtt/wss/README.md +++ b/examples/protocols/mqtt/wss/README.md @@ -1,3 +1,6 @@ +| Supported Targets | ESP32 | ESP32-S2 | ESP32-S3 | ESP32-C3 | +| ----------------- | ----- | -------- | -------- | -------- | + # ESP-MQTT MQTT over WSS Sample application (See the README.md file in the upper level 'examples' directory for more information about examples.) From 8f8090e4ac8046775021052df1ecaf1079b6dd88 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Mon, 2 May 2022 15:47:05 +0200 Subject: [PATCH 124/231] 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 7d9a22b4d472e5e1f213d9b3a77af3853521f143 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Fri, 13 May 2022 16:55:22 +0200 Subject: [PATCH 125/231] mqtt: Fix and add mqtt host test to CI --- 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 ++++++ 4 files changed, 16 insertions(+), 9 deletions(-) create mode 100644 components/mqtt/host_test/mocks/include/local_FreeRTOS_config.h 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 c6951764a20e0461a7c83e0717fc3088ada88e76 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Thu, 9 Jun 2022 11:08:45 +0200 Subject: [PATCH 126/231] 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 ee67d87a906bcd3fec8bd60214e1c7c77c1fcb50 Mon Sep 17 00:00:00 2001 From: Laukik Hase Date: Thu, 9 Jun 2022 14:22:14 +0530 Subject: [PATCH 127/231] ci: Fix `mqtt_qos1` example test failure - Updated log levels of some tags in mqtt/tcp example - Updated mqtt/tcp example test to work with WiFi --- examples/protocols/mqtt/tcp/main/app_main.c | 4 ++-- examples/protocols/mqtt/tcp/mqtt_tcp_example_test.py | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/examples/protocols/mqtt/tcp/main/app_main.c b/examples/protocols/mqtt/tcp/main/app_main.c index 48b1c47..47ce74b 100644 --- a/examples/protocols/mqtt/tcp/main/app_main.c +++ b/examples/protocols/mqtt/tcp/main/app_main.c @@ -150,12 +150,12 @@ void app_main(void) ESP_LOGI(TAG, "[APP] IDF version: %s", esp_get_idf_version()); esp_log_level_set("*", ESP_LOG_INFO); - esp_log_level_set("MQTT_CLIENT", ESP_LOG_VERBOSE); + esp_log_level_set("mqtt_client", ESP_LOG_VERBOSE); esp_log_level_set("MQTT_EXAMPLE", ESP_LOG_VERBOSE); esp_log_level_set("TRANSPORT_BASE", ESP_LOG_VERBOSE); esp_log_level_set("esp-tls", ESP_LOG_VERBOSE); esp_log_level_set("TRANSPORT", ESP_LOG_VERBOSE); - esp_log_level_set("OUTBOX", ESP_LOG_VERBOSE); + esp_log_level_set("outbox", ESP_LOG_VERBOSE); ESP_ERROR_CHECK(nvs_flash_init()); ESP_ERROR_CHECK(esp_netif_init()); diff --git a/examples/protocols/mqtt/tcp/mqtt_tcp_example_test.py b/examples/protocols/mqtt/tcp/mqtt_tcp_example_test.py index 616ca65..acf78ed 100644 --- a/examples/protocols/mqtt/tcp/mqtt_tcp_example_test.py +++ b/examples/protocols/mqtt/tcp/mqtt_tcp_example_test.py @@ -76,7 +76,7 @@ def test_examples_protocol_mqtt_qos1(env, extra_data): dut1.start_app() # waiting for getting the IP address try: - ip_address = dut1.expect(re.compile(r' eth ip: ([^,]+),'), timeout=30) + ip_address = dut1.expect(re.compile(r' (sta|eth) ip: ([^,]+),'), timeout=30) print('Connected to AP with IP: {}'.format(ip_address)) except DUT.ExpectTimeout: raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP') @@ -86,8 +86,8 @@ def test_examples_protocol_mqtt_qos1(env, extra_data): thread1.join() print('Message id received from server: {}'.format(msgid)) # 3. check the message id was enqueued and then deleted - msgid_enqueued = dut1.expect(re.compile(r'OUTBOX: ENQUEUE msgid=([0-9]+)'), timeout=30) - msgid_deleted = dut1.expect(re.compile(r'OUTBOX: DELETED msgid=([0-9]+)'), timeout=30) + msgid_enqueued = dut1.expect(re.compile(r'outbox: ENQUEUE msgid=([0-9]+)'), timeout=30) + msgid_deleted = dut1.expect(re.compile(r'outbox: DELETED msgid=([0-9]+)'), timeout=30) # 4. check the msgid of received data are the same as that of enqueued and deleted from outbox if (msgid_enqueued[0] == str(msgid) and msgid_deleted[0] == str(msgid)): print('PASS: Received correct msg id') From 4b884c421a1ac29e59baf2954c5f403ef7c69984 Mon Sep 17 00:00:00 2001 From: Euripedes Rocha Date: Tue, 7 Jun 2022 08:10:38 -0300 Subject: [PATCH 128/231] [MQTT] - Adds esp-timer as dependency and change version. - Current time is now from esp_timer. --- 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 +++++--- 3 files changed, 9 insertions(+), 5 deletions(-) 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 4ebf448bcf65c18f1777ba551f7f47c4198d2c66 Mon Sep 17 00:00:00 2001 From: Fu Hanxi Date: Wed, 13 Jul 2022 10:41:36 +0800 Subject: [PATCH 129/231] docs: changes docs supported targets tables --- components/mqtt/host_test/README.md | 3 +++ examples/protocols/mqtt/ssl/README.md | 2 +- examples/protocols/mqtt/ssl_ds/README.md | 4 +++- examples/protocols/mqtt/ssl_mutual_auth/README.md | 2 +- examples/protocols/mqtt/ssl_psk/README.md | 2 +- examples/protocols/mqtt/tcp/README.md | 2 +- examples/protocols/mqtt/ws/README.md | 2 +- examples/protocols/mqtt/wss/README.md | 2 +- tools/test_apps/protocols/mqtt/build_test/README.md | 2 +- tools/test_apps/protocols/mqtt/publish_connect_test/README.md | 2 +- 10 files changed, 14 insertions(+), 9 deletions(-) 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. diff --git a/examples/protocols/mqtt/ssl/README.md b/examples/protocols/mqtt/ssl/README.md index a87f208..eb53ced 100644 --- a/examples/protocols/mqtt/ssl/README.md +++ b/examples/protocols/mqtt/ssl/README.md @@ -1,4 +1,4 @@ -| Supported Targets | ESP32 | ESP32-S2 | ESP32-S3 | ESP32-C3 | +| Supported Targets | ESP32 | ESP32-C3 | ESP32-S2 | ESP32-S3 | | ----------------- | ----- | -------- | -------- | -------- | # ESP-MQTT SSL Sample application diff --git a/examples/protocols/mqtt/ssl_ds/README.md b/examples/protocols/mqtt/ssl_ds/README.md index 31647bc..065bcfc 100644 --- a/examples/protocols/mqtt/ssl_ds/README.md +++ b/examples/protocols/mqtt/ssl_ds/README.md @@ -1,4 +1,6 @@ -| Supported Targets | ESP32-S2 | ESP32-C3 | ESP32-S3 | +| Supported Targets | ESP32-C3 | ESP32-S2 | ESP32-S3 | +| ----------------- | -------- | -------- | -------- | + # ESP-MQTT SSL Mutual Authentication with Digital Signature (See the README.md file in the upper level 'examples' directory for more information about examples.) diff --git a/examples/protocols/mqtt/ssl_mutual_auth/README.md b/examples/protocols/mqtt/ssl_mutual_auth/README.md index be0c9b6..9274aa4 100644 --- a/examples/protocols/mqtt/ssl_mutual_auth/README.md +++ b/examples/protocols/mqtt/ssl_mutual_auth/README.md @@ -1,4 +1,4 @@ -| Supported Targets | ESP32 | ESP32-S2 | ESP32-S3 | ESP32-C3 | +| Supported Targets | ESP32 | ESP32-C3 | ESP32-S2 | ESP32-S3 | | ----------------- | ----- | -------- | -------- | -------- | # ESP-MQTT SSL Sample application (mutual authentication) diff --git a/examples/protocols/mqtt/ssl_psk/README.md b/examples/protocols/mqtt/ssl_psk/README.md index cec38fe..3029ccd 100644 --- a/examples/protocols/mqtt/ssl_psk/README.md +++ b/examples/protocols/mqtt/ssl_psk/README.md @@ -1,4 +1,4 @@ -| Supported Targets | ESP32 | ESP32-S2 | ESP32-S3 | ESP32-C3 | +| Supported Targets | ESP32 | ESP32-C3 | ESP32-S2 | ESP32-S3 | | ----------------- | ----- | -------- | -------- | -------- | # ESP-MQTT SSL example with PSK verification diff --git a/examples/protocols/mqtt/tcp/README.md b/examples/protocols/mqtt/tcp/README.md index be6d70a..0589091 100644 --- a/examples/protocols/mqtt/tcp/README.md +++ b/examples/protocols/mqtt/tcp/README.md @@ -1,4 +1,4 @@ -| Supported Targets | ESP32 | ESP32-S2 | ESP32-S3 | ESP32-C3 | +| Supported Targets | ESP32 | ESP32-C3 | ESP32-S2 | ESP32-S3 | | ----------------- | ----- | -------- | -------- | -------- | # ESP-MQTT sample application diff --git a/examples/protocols/mqtt/ws/README.md b/examples/protocols/mqtt/ws/README.md index c2620b3..a5f36c9 100644 --- a/examples/protocols/mqtt/ws/README.md +++ b/examples/protocols/mqtt/ws/README.md @@ -1,4 +1,4 @@ -| Supported Targets | ESP32 | ESP32-S2 | ESP32-S3 | ESP32-C3 | +| Supported Targets | ESP32 | ESP32-C3 | ESP32-S2 | ESP32-S3 | | ----------------- | ----- | -------- | -------- | -------- | # ESP-MQTT MQTT over Websocket diff --git a/examples/protocols/mqtt/wss/README.md b/examples/protocols/mqtt/wss/README.md index f836920..ca2e98e 100644 --- a/examples/protocols/mqtt/wss/README.md +++ b/examples/protocols/mqtt/wss/README.md @@ -1,4 +1,4 @@ -| Supported Targets | ESP32 | ESP32-S2 | ESP32-S3 | ESP32-C3 | +| Supported Targets | ESP32 | ESP32-C3 | ESP32-S2 | ESP32-S3 | | ----------------- | ----- | -------- | -------- | -------- | # ESP-MQTT MQTT over WSS Sample application diff --git a/tools/test_apps/protocols/mqtt/build_test/README.md b/tools/test_apps/protocols/mqtt/build_test/README.md index 0f6c3a9..29d3251 100644 --- a/tools/test_apps/protocols/mqtt/build_test/README.md +++ b/tools/test_apps/protocols/mqtt/build_test/README.md @@ -1,4 +1,4 @@ -| Supported Targets | ESP32 | ESP32-S2 | ESP32-S3 | ESP32-C3 | ESP32-C2 | +| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-S2 | ESP32-S3 | | ----------------- | ----- | -------- | -------- | -------- | -------- | # Build only test for C++ diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/README.md b/tools/test_apps/protocols/mqtt/publish_connect_test/README.md index 3ed0ff0..ec2cd41 100644 --- a/tools/test_apps/protocols/mqtt/publish_connect_test/README.md +++ b/tools/test_apps/protocols/mqtt/publish_connect_test/README.md @@ -1,4 +1,4 @@ -| Supported Targets | ESP32 | ESP32-S2 | ESP32-C3 | +| Supported Targets | ESP32 | ESP32-C3 | ESP32-S2 | | ----------------- | ----- | -------- | -------- | # ESP-MQTT advanced publish and connect test project From 729363e4cc808a0e0a71817afd2138b17fdf54f1 Mon Sep 17 00:00:00 2001 From: Chen Yudong Date: Sun, 3 Jul 2022 19:44:00 +0800 Subject: [PATCH 130/231] CI: fix ip address checker and trace log --- examples/protocols/mqtt/ssl/mqtt_ssl_example_test.py | 2 +- examples/protocols/mqtt/tcp/mqtt_tcp_example_test.py | 6 +++--- examples/protocols/mqtt/ws/mqtt_ws_example_test.py | 2 +- examples/protocols/mqtt/wss/mqtt_wss_example_test.py | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/examples/protocols/mqtt/ssl/mqtt_ssl_example_test.py b/examples/protocols/mqtt/ssl/mqtt_ssl_example_test.py index 7970541..dc879d1 100644 --- a/examples/protocols/mqtt/ssl/mqtt_ssl_example_test.py +++ b/examples/protocols/mqtt/ssl/mqtt_ssl_example_test.py @@ -110,7 +110,7 @@ def test_examples_protocol_mqtt_ssl(env, extra_data): raise ValueError('ENV_TEST_FAILURE: Test script cannot connect to broker: {}'.format(broker_url)) dut1.start_app() try: - ip_address = dut1.expect(re.compile(r' eth ip: ([^,]+),'), timeout=30) + ip_address = dut1.expect(re.compile(r'IPv4 address: ([^,]+),'), timeout=30) print('Connected to AP with IP: {}'.format(ip_address)) except DUT.ExpectTimeout: print('ENV_TEST_FAILURE: Cannot connect to AP') diff --git a/examples/protocols/mqtt/tcp/mqtt_tcp_example_test.py b/examples/protocols/mqtt/tcp/mqtt_tcp_example_test.py index acf78ed..35a719a 100644 --- a/examples/protocols/mqtt/tcp/mqtt_tcp_example_test.py +++ b/examples/protocols/mqtt/tcp/mqtt_tcp_example_test.py @@ -76,10 +76,10 @@ def test_examples_protocol_mqtt_qos1(env, extra_data): dut1.start_app() # waiting for getting the IP address try: - ip_address = dut1.expect(re.compile(r' (sta|eth) ip: ([^,]+),'), timeout=30) - print('Connected to AP with IP: {}'.format(ip_address)) + ip_address = dut1.expect(re.compile(r'IPv4 address: ([^,]+),'), timeout=30) + print('Connected to AP/Ethernet with IP: {}'.format(ip_address)) except DUT.ExpectTimeout: - raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP') + raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP/Ethernet') print('writing to device: {}'.format('mqtt://' + host_ip + '\n')) dut1.write('mqtt://' + host_ip + '\n') diff --git a/examples/protocols/mqtt/ws/mqtt_ws_example_test.py b/examples/protocols/mqtt/ws/mqtt_ws_example_test.py index 60da603..b4fc2fa 100644 --- a/examples/protocols/mqtt/ws/mqtt_ws_example_test.py +++ b/examples/protocols/mqtt/ws/mqtt_ws_example_test.py @@ -83,7 +83,7 @@ def test_examples_protocol_mqtt_ws(env, extra_data): raise ValueError('ENV_TEST_FAILURE: Test script cannot connect to broker: {}'.format(broker_url)) dut1.start_app() try: - ip_address = dut1.expect(re.compile(r' eth ip: ([^,]+),'), timeout=30) + ip_address = dut1.expect(re.compile(r'IPv4 address: ([^,]+),'), timeout=30) print('Connected to AP with IP: {}'.format(ip_address)) except DUT.ExpectTimeout: print('ENV_TEST_FAILURE: Cannot connect to AP') diff --git a/examples/protocols/mqtt/wss/mqtt_wss_example_test.py b/examples/protocols/mqtt/wss/mqtt_wss_example_test.py index a7825e8..b00a154 100644 --- a/examples/protocols/mqtt/wss/mqtt_wss_example_test.py +++ b/examples/protocols/mqtt/wss/mqtt_wss_example_test.py @@ -87,7 +87,7 @@ def test_examples_protocol_mqtt_wss(env, extra_data): raise ValueError('ENV_TEST_FAILURE: Test script cannot connect to broker: {}'.format(broker_url)) dut1.start_app() try: - ip_address = dut1.expect(re.compile(r' eth ip: ([^,]+),'), timeout=30) + ip_address = dut1.expect(re.compile(r'IPv4 address: ([^,]+),'), timeout=30) print('Connected to AP with IP: {}'.format(ip_address)) except DUT.ExpectTimeout: print('ENV_TEST_FAILURE: Cannot connect to AP') From debc9962a1b44bfeb4c8959a445a45896c70d3dc Mon Sep 17 00:00:00 2001 From: Chen Yudong Date: Sun, 3 Jul 2022 16:22:20 +0800 Subject: [PATCH 131/231] CI: update test cases to use different environment variables change test environments optimize asio udp server test fix icmp echo test case use ethernet_router env to run iperf test cases --- .../mqtt/tcp/mqtt_tcp_example_test.py | 22 ++++++---------- .../mqtt/publish_connect_test/app_test.py | 26 +++++-------------- 2 files changed, 15 insertions(+), 33 deletions(-) diff --git a/examples/protocols/mqtt/tcp/mqtt_tcp_example_test.py b/examples/protocols/mqtt/tcp/mqtt_tcp_example_test.py index 35a719a..06c5691 100644 --- a/examples/protocols/mqtt/tcp/mqtt_tcp_example_test.py +++ b/examples/protocols/mqtt/tcp/mqtt_tcp_example_test.py @@ -7,19 +7,12 @@ import time from threading import Thread import ttfw_idf +from common_test_methods import get_my_ip4_by_dest_ip from tiny_test_fw import DUT msgid = -1 -def get_my_ip(): - s1 = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) - s1.connect(('8.8.8.8', 80)) - my_ip = s1.getsockname()[0] - s1.close() - return my_ip - - def mqqt_server_sketch(my_ip, port): global msgid print('Starting the server on {}'.format(my_ip)) @@ -68,19 +61,20 @@ def test_examples_protocol_mqtt_qos1(env, extra_data): binary_file = os.path.join(dut1.app.binary_path, 'mqtt_tcp.bin') bin_size = os.path.getsize(binary_file) ttfw_idf.log_performance('mqtt_tcp_bin_size', '{}KB'.format(bin_size // 1024)) - # 1. start mqtt broker sketch - host_ip = get_my_ip() - thread1 = Thread(target=mqqt_server_sketch, args=(host_ip,1883)) - thread1.start() - # 2. start the dut test and wait till client gets IP address + # 1. start the dut test and wait till client gets IP address dut1.start_app() # waiting for getting the IP address try: - ip_address = dut1.expect(re.compile(r'IPv4 address: ([^,]+),'), timeout=30) + ip_address = dut1.expect(re.compile(r'IPv4 address: ([^,]+),'), timeout=30)[0] print('Connected to AP/Ethernet with IP: {}'.format(ip_address)) except DUT.ExpectTimeout: raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP/Ethernet') + # 2. start mqtt broker sketch + host_ip = get_my_ip4_by_dest_ip(ip_address) + thread1 = Thread(target=mqqt_server_sketch, args=(host_ip,1883)) + thread1.start() + print('writing to device: {}'.format('mqtt://' + host_ip + '\n')) dut1.write('mqtt://' + host_ip + '\n') thread1.join() diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/app_test.py b/tools/test_apps/protocols/mqtt/publish_connect_test/app_test.py index da22edb..2e99d02 100644 --- a/tools/test_apps/protocols/mqtt/publish_connect_test/app_test.py +++ b/tools/test_apps/protocols/mqtt/publish_connect_test/app_test.py @@ -15,6 +15,7 @@ from threading import Event, Lock, Thread import paho.mqtt.client as mqtt import ttfw_idf +from common_test_methods import get_my_ip4_by_dest_ip DEFAULT_MSG_SIZE = 16 @@ -33,19 +34,6 @@ def set_server_cert_cn(ip): raise('openssl command {} failed'.format(args)) -def get_my_ip(): - s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) - try: - # doesn't even have to be reachable - s.connect(('10.255.255.255', 1)) - IP = s.getsockname()[0] - except Exception: - IP = '127.0.0.1' - finally: - s.close() - return IP - - # Publisher class creating a python client to send/receive published data from esp-mqtt client class MqttPublisher: @@ -247,8 +235,8 @@ class TlsServer: self.shutdown.set() -def connection_tests(dut, cases): - ip = get_my_ip() +def connection_tests(dut, cases, dut_ip): + ip = get_my_ip4_by_dest_ip(dut_ip) set_server_cert_cn(ip) server_port = 2222 @@ -314,7 +302,7 @@ def connection_tests(dut, cases): teardown_connection_suite() -@ttfw_idf.idf_custom_test(env_tag='Example_EthKitV1', group='test-apps') +@ttfw_idf.idf_custom_test(env_tag='ethernet_router', group='test-apps') def test_app_protocol_mqtt_publish_connect(env, extra_data): """ steps: @@ -348,11 +336,11 @@ def test_app_protocol_mqtt_publish_connect(env, extra_data): raise dut1.start_app() - esp_ip = dut1.expect(re.compile(r' IPv4 address: ([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)'), timeout=30) - print('Got IP={}'.format(esp_ip[0])) + esp_ip = dut1.expect(re.compile(r' IPv4 address: ([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)'), timeout=30)[0] + print('Got IP={}'.format(esp_ip)) if not os.getenv('MQTT_SKIP_CONNECT_TEST'): - connection_tests(dut1,cases) + connection_tests(dut1,cases,esp_ip) # # start publish tests only if enabled in the environment (for weekend tests only) From fade26cf27de4755c1f55be4e35231d2bacb8a5e Mon Sep 17 00:00:00 2001 From: Chen Yudong Date: Thu, 7 Jul 2022 00:34:06 +0800 Subject: [PATCH 132/231] CI: Improve common test methods also fix ota test cases --- examples/protocols/mqtt/ssl/mqtt_ssl_example_test.py | 4 ++-- examples/protocols/mqtt/tcp/mqtt_tcp_example_test.py | 8 ++++---- examples/protocols/mqtt/ws/mqtt_ws_example_test.py | 4 ++-- examples/protocols/mqtt/wss/mqtt_wss_example_test.py | 4 ++-- .../protocols/mqtt/publish_connect_test/app_test.py | 6 +++--- 5 files changed, 13 insertions(+), 13 deletions(-) diff --git a/examples/protocols/mqtt/ssl/mqtt_ssl_example_test.py b/examples/protocols/mqtt/ssl/mqtt_ssl_example_test.py index dc879d1..0bdd6d3 100644 --- a/examples/protocols/mqtt/ssl/mqtt_ssl_example_test.py +++ b/examples/protocols/mqtt/ssl/mqtt_ssl_example_test.py @@ -58,7 +58,7 @@ def on_message(client, userdata, msg): message_log += 'Received data:' + msg.topic + ' ' + payload + '\n' -@ttfw_idf.idf_example_test(env_tag='Example_EthKitV1') +@ttfw_idf.idf_example_test(env_tag='ethernet_router') def test_examples_protocol_mqtt_ssl(env, extra_data): broker_url = '' broker_port = 0 @@ -110,7 +110,7 @@ def test_examples_protocol_mqtt_ssl(env, extra_data): raise ValueError('ENV_TEST_FAILURE: Test script cannot connect to broker: {}'.format(broker_url)) dut1.start_app() try: - ip_address = dut1.expect(re.compile(r'IPv4 address: ([^,]+),'), timeout=30) + ip_address = dut1.expect(re.compile(r'IPv4 address: (\d+\.\d+\.\d+\.\d+)'), timeout=30)[0] print('Connected to AP with IP: {}'.format(ip_address)) except DUT.ExpectTimeout: print('ENV_TEST_FAILURE: Cannot connect to AP') diff --git a/examples/protocols/mqtt/tcp/mqtt_tcp_example_test.py b/examples/protocols/mqtt/tcp/mqtt_tcp_example_test.py index 06c5691..735c6ad 100644 --- a/examples/protocols/mqtt/tcp/mqtt_tcp_example_test.py +++ b/examples/protocols/mqtt/tcp/mqtt_tcp_example_test.py @@ -7,7 +7,7 @@ import time from threading import Thread import ttfw_idf -from common_test_methods import get_my_ip4_by_dest_ip +from common_test_methods import get_host_ip4_by_dest_ip from tiny_test_fw import DUT msgid = -1 @@ -46,7 +46,7 @@ def mqqt_server_sketch(my_ip, port): print('server closed') -@ttfw_idf.idf_example_test(env_tag='Example_EthKitV1') +@ttfw_idf.idf_example_test(env_tag='ethernet_router') def test_examples_protocol_mqtt_qos1(env, extra_data): global msgid """ @@ -65,13 +65,13 @@ def test_examples_protocol_mqtt_qos1(env, extra_data): dut1.start_app() # waiting for getting the IP address try: - ip_address = dut1.expect(re.compile(r'IPv4 address: ([^,]+),'), timeout=30)[0] + ip_address = dut1.expect(re.compile(r'IPv4 address: (\d+\.\d+\.\d+\.\d+)'), timeout=30)[0] print('Connected to AP/Ethernet with IP: {}'.format(ip_address)) except DUT.ExpectTimeout: raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP/Ethernet') # 2. start mqtt broker sketch - host_ip = get_my_ip4_by_dest_ip(ip_address) + host_ip = get_host_ip4_by_dest_ip(ip_address) thread1 = Thread(target=mqqt_server_sketch, args=(host_ip,1883)) thread1.start() diff --git a/examples/protocols/mqtt/ws/mqtt_ws_example_test.py b/examples/protocols/mqtt/ws/mqtt_ws_example_test.py index b4fc2fa..f8d87d9 100644 --- a/examples/protocols/mqtt/ws/mqtt_ws_example_test.py +++ b/examples/protocols/mqtt/ws/mqtt_ws_example_test.py @@ -39,7 +39,7 @@ def on_message(client, userdata, msg): message_log += 'Received data:' + msg.topic + ' ' + payload + '\n' -@ttfw_idf.idf_example_test(env_tag='Example_EthKitV1') +@ttfw_idf.idf_example_test(env_tag='ethernet_router') def test_examples_protocol_mqtt_ws(env, extra_data): broker_url = '' broker_port = 0 @@ -83,7 +83,7 @@ def test_examples_protocol_mqtt_ws(env, extra_data): raise ValueError('ENV_TEST_FAILURE: Test script cannot connect to broker: {}'.format(broker_url)) dut1.start_app() try: - ip_address = dut1.expect(re.compile(r'IPv4 address: ([^,]+),'), timeout=30) + ip_address = dut1.expect(re.compile(r'IPv4 address: (\d+\.\d+\.\d+\.\d+)'), timeout=30)[0] print('Connected to AP with IP: {}'.format(ip_address)) except DUT.ExpectTimeout: print('ENV_TEST_FAILURE: Cannot connect to AP') diff --git a/examples/protocols/mqtt/wss/mqtt_wss_example_test.py b/examples/protocols/mqtt/wss/mqtt_wss_example_test.py index b00a154..4d204d6 100644 --- a/examples/protocols/mqtt/wss/mqtt_wss_example_test.py +++ b/examples/protocols/mqtt/wss/mqtt_wss_example_test.py @@ -40,7 +40,7 @@ def on_message(client, userdata, msg): message_log += 'Received data:' + msg.topic + ' ' + payload + '\n' -@ttfw_idf.idf_example_test(env_tag='Example_EthKitV1') +@ttfw_idf.idf_example_test(env_tag='ethernet_router') def test_examples_protocol_mqtt_wss(env, extra_data): broker_url = '' broker_port = 0 @@ -87,7 +87,7 @@ def test_examples_protocol_mqtt_wss(env, extra_data): raise ValueError('ENV_TEST_FAILURE: Test script cannot connect to broker: {}'.format(broker_url)) dut1.start_app() try: - ip_address = dut1.expect(re.compile(r'IPv4 address: ([^,]+),'), timeout=30) + ip_address = dut1.expect(re.compile(r'IPv4 address: (\d+\.\d+\.\d+\.\d+)'), timeout=30)[0] print('Connected to AP with IP: {}'.format(ip_address)) except DUT.ExpectTimeout: print('ENV_TEST_FAILURE: Cannot connect to AP') diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/app_test.py b/tools/test_apps/protocols/mqtt/publish_connect_test/app_test.py index 2e99d02..0b30d06 100644 --- a/tools/test_apps/protocols/mqtt/publish_connect_test/app_test.py +++ b/tools/test_apps/protocols/mqtt/publish_connect_test/app_test.py @@ -15,7 +15,7 @@ from threading import Event, Lock, Thread import paho.mqtt.client as mqtt import ttfw_idf -from common_test_methods import get_my_ip4_by_dest_ip +from common_test_methods import get_host_ip4_by_dest_ip DEFAULT_MSG_SIZE = 16 @@ -236,7 +236,7 @@ class TlsServer: def connection_tests(dut, cases, dut_ip): - ip = get_my_ip4_by_dest_ip(dut_ip) + ip = get_host_ip4_by_dest_ip(dut_ip) set_server_cert_cn(ip) server_port = 2222 @@ -336,7 +336,7 @@ def test_app_protocol_mqtt_publish_connect(env, extra_data): raise dut1.start_app() - esp_ip = dut1.expect(re.compile(r' IPv4 address: ([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)'), timeout=30)[0] + esp_ip = dut1.expect(re.compile(r'IPv4 address: (\d+\.\d+\.\d+\.\d+)'), timeout=30)[0] print('Got IP={}'.format(esp_ip)) if not os.getenv('MQTT_SKIP_CONNECT_TEST'): From 119d7240172382acb0831fc5f036c68680429ef2 Mon Sep 17 00:00:00 2001 From: Chen Yudong Date: Thu, 14 Jul 2022 22:55:50 +0800 Subject: [PATCH 133/231] CI: make sure that reading of the ipv4 address has finished --- examples/protocols/mqtt/ssl/mqtt_ssl_example_test.py | 2 +- examples/protocols/mqtt/tcp/mqtt_tcp_example_test.py | 2 +- examples/protocols/mqtt/ws/mqtt_ws_example_test.py | 2 +- examples/protocols/mqtt/wss/mqtt_wss_example_test.py | 2 +- tools/test_apps/protocols/mqtt/publish_connect_test/app_test.py | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/examples/protocols/mqtt/ssl/mqtt_ssl_example_test.py b/examples/protocols/mqtt/ssl/mqtt_ssl_example_test.py index 0bdd6d3..3309ca4 100644 --- a/examples/protocols/mqtt/ssl/mqtt_ssl_example_test.py +++ b/examples/protocols/mqtt/ssl/mqtt_ssl_example_test.py @@ -110,7 +110,7 @@ def test_examples_protocol_mqtt_ssl(env, extra_data): raise ValueError('ENV_TEST_FAILURE: Test script cannot connect to broker: {}'.format(broker_url)) dut1.start_app() try: - ip_address = dut1.expect(re.compile(r'IPv4 address: (\d+\.\d+\.\d+\.\d+)'), timeout=30)[0] + ip_address = dut1.expect(re.compile(r'IPv4 address: (\d+\.\d+\.\d+\.\d+)[^\d]'), timeout=30)[0] print('Connected to AP with IP: {}'.format(ip_address)) except DUT.ExpectTimeout: print('ENV_TEST_FAILURE: Cannot connect to AP') diff --git a/examples/protocols/mqtt/tcp/mqtt_tcp_example_test.py b/examples/protocols/mqtt/tcp/mqtt_tcp_example_test.py index 735c6ad..76dc4a4 100644 --- a/examples/protocols/mqtt/tcp/mqtt_tcp_example_test.py +++ b/examples/protocols/mqtt/tcp/mqtt_tcp_example_test.py @@ -65,7 +65,7 @@ def test_examples_protocol_mqtt_qos1(env, extra_data): dut1.start_app() # waiting for getting the IP address try: - ip_address = dut1.expect(re.compile(r'IPv4 address: (\d+\.\d+\.\d+\.\d+)'), timeout=30)[0] + ip_address = dut1.expect(re.compile(r'IPv4 address: (\d+\.\d+\.\d+\.\d+)[^\d]'), timeout=30)[0] print('Connected to AP/Ethernet with IP: {}'.format(ip_address)) except DUT.ExpectTimeout: raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP/Ethernet') diff --git a/examples/protocols/mqtt/ws/mqtt_ws_example_test.py b/examples/protocols/mqtt/ws/mqtt_ws_example_test.py index f8d87d9..72d4fa7 100644 --- a/examples/protocols/mqtt/ws/mqtt_ws_example_test.py +++ b/examples/protocols/mqtt/ws/mqtt_ws_example_test.py @@ -83,7 +83,7 @@ def test_examples_protocol_mqtt_ws(env, extra_data): raise ValueError('ENV_TEST_FAILURE: Test script cannot connect to broker: {}'.format(broker_url)) dut1.start_app() try: - ip_address = dut1.expect(re.compile(r'IPv4 address: (\d+\.\d+\.\d+\.\d+)'), timeout=30)[0] + ip_address = dut1.expect(re.compile(r'IPv4 address: (\d+\.\d+\.\d+\.\d+)[^\d]'), timeout=30)[0] print('Connected to AP with IP: {}'.format(ip_address)) except DUT.ExpectTimeout: print('ENV_TEST_FAILURE: Cannot connect to AP') diff --git a/examples/protocols/mqtt/wss/mqtt_wss_example_test.py b/examples/protocols/mqtt/wss/mqtt_wss_example_test.py index 4d204d6..9f8ab12 100644 --- a/examples/protocols/mqtt/wss/mqtt_wss_example_test.py +++ b/examples/protocols/mqtt/wss/mqtt_wss_example_test.py @@ -87,7 +87,7 @@ def test_examples_protocol_mqtt_wss(env, extra_data): raise ValueError('ENV_TEST_FAILURE: Test script cannot connect to broker: {}'.format(broker_url)) dut1.start_app() try: - ip_address = dut1.expect(re.compile(r'IPv4 address: (\d+\.\d+\.\d+\.\d+)'), timeout=30)[0] + ip_address = dut1.expect(re.compile(r'IPv4 address: (\d+\.\d+\.\d+\.\d+)[^\d]'), timeout=30)[0] print('Connected to AP with IP: {}'.format(ip_address)) except DUT.ExpectTimeout: print('ENV_TEST_FAILURE: Cannot connect to AP') diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/app_test.py b/tools/test_apps/protocols/mqtt/publish_connect_test/app_test.py index 0b30d06..a2366d3 100644 --- a/tools/test_apps/protocols/mqtt/publish_connect_test/app_test.py +++ b/tools/test_apps/protocols/mqtt/publish_connect_test/app_test.py @@ -336,7 +336,7 @@ def test_app_protocol_mqtt_publish_connect(env, extra_data): raise dut1.start_app() - esp_ip = dut1.expect(re.compile(r'IPv4 address: (\d+\.\d+\.\d+\.\d+)'), timeout=30)[0] + esp_ip = dut1.expect(re.compile(r'IPv4 address: (\d+\.\d+\.\d+\.\d+)[^\d]'), timeout=30)[0] print('Got IP={}'.format(esp_ip)) if not os.getenv('MQTT_SKIP_CONNECT_TEST'): From dd40fc3af265db9bb456bb50fa907907120a1032 Mon Sep 17 00:00:00 2001 From: Euripedes Rocha Date: Mon, 13 Jun 2022 07:59:11 -0300 Subject: [PATCH 134/231] [MQTT] - Updates esp_mqtt configuration struct - Layered config struct - Fix examples. --- .../mqtt/host_test/main/test_mqtt_client.cpp | 2 +- components/mqtt/test/test_mqtt.c | 6 +- .../mqtt/test/test_mqtt_client_broker.c | 26 ++-- docs/en/api-reference/protocols/mqtt.rst | 122 ++++++++++-------- examples/protocols/mqtt/ssl/main/app_main.c | 8 +- .../protocols/mqtt/ssl_ds/main/app_main.c | 35 +++-- .../mqtt/ssl_mutual_auth/main/app_main.c | 16 ++- .../protocols/mqtt/ssl_psk/main/app_main.c | 25 +++- examples/protocols/mqtt/tcp/main/app_main.c | 6 +- examples/protocols/mqtt/ws/main/app_main.c | 2 +- examples/protocols/mqtt/wss/main/app_main.c | 4 +- .../mqtt/publish_connect_test/app_test.py | 2 +- .../main/Kconfig.projbuild | 2 +- .../publish_connect_test/main/connect_test.c | 58 ++++----- .../publish_connect_test/main/publish_test.c | 14 +- 15 files changed, 191 insertions(+), 137 deletions(-) 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); diff --git a/docs/en/api-reference/protocols/mqtt.rst b/docs/en/api-reference/protocols/mqtt.rst index 5b84107..1346305 100644 --- a/docs/en/api-reference/protocols/mqtt.rst +++ b/docs/en/api-reference/protocols/mqtt.rst @@ -4,7 +4,7 @@ ESP-MQTT Overview -------- -ESP-MQTT is an implementation of MQTT protocol client (MQTT is a lightweight publish/subscribe messaging protocol). +ESP-MQTT is an implementation of [MQTT](mqtt.org) protocol client (MQTT is a lightweight publish/subscribe messaging protocol). Features @@ -19,17 +19,40 @@ Application Example ------------------- * :example:`protocols/mqtt/tcp`: MQTT over tcp, default port 1883 - * :example:`protocols/mqtt/ssl`: MQTT over tcp, default port 8883 - * :example:`protocols/mqtt/ssl_psk`: MQTT over tcp using pre-shared keys for authentication, default port 8883 + * :example:`protocols/mqtt/ssl`: MQTT over tls, default port 8883 + * :example:`protocols/mqtt/ssl_ds`: MQTT over tls using digital signature peripheral for authentication, default port 8883. + * :example:`protocols/mqtt/ssl_mutual_auth`: MQTT over tls using certificates for authentication, default port 8883 + * :example:`protocols/mqtt/ssl_psk`: MQTT over tls using pre-shared keys for authentication, default port 8883. * :example:`protocols/mqtt/ws`: MQTT over Websocket, default port 80 * :example:`protocols/mqtt/wss`: MQTT over Websocket Secure, default port 443 Configuration ------------- -URI -^^^ +The configuration is made by setting fields in ``esp_mqtt_client_config_t`` struct. The configuration struct has the following sub structs to configure different aspects of the client operation. + + * :cpp:member:`broker` - Allow to set address and security verification. + * :cpp:member:`credentials` - Client credentials for authentication. + * :cpp:member:`session` - Configuration for MQTT session aspects. + * :cpp:member:`network` - Networking related configuration. + * :cpp:member:`task` - Allow to configure FreeRTOS task. + * :cpp:member:`buffer` - Buffer size for input and output. + +In the following session the most common aspects are detailed. + +Broker +^^^^^^^^^^^ + +=========== +Address +=========== + +Broker address can be set by usage of ``broker.address`` struct. The configuration can be made by usage of ``uri`` field +or the combination of ``hostname``, ``transport`` and ``port``. Optionally, `path` could be set, this field is useful in +websocket connections. + +The ``uri`` field is used in the following format ``scheme://hostname:port/path``. - Curently support ``mqtt``, ``mqtts``, ``ws``, ``wss`` schemes - MQTT over TCP samples: @@ -56,8 +79,7 @@ URI .. code:: c const esp_mqtt_client_config_t mqtt_cfg = { - .uri = "mqtt://mqtt.eclipseprojects.io", - // .user_context = (void *)your_context + .broker.address.uri = "mqtt://mqtt.eclipseprojects.io", }; esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg); esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, client); @@ -65,9 +87,13 @@ URI - Note: By default mqtt client uses event loop library to post related mqtt events (connected, subscribed, published, etc.) +============ +Verification +============ -SSL -^^^ +For secure connections TLS is used, and to guarantee Broker's identity the ``broker.verification`` struct must be set. +The broker certificate may be set in PEM or DER format. To select DER the equivalent ``_len`` field must be set, +otherwise a NULL terminated string in PEM format should be provided to ``certificate`` field. - Get certificate from server, example: ``mqtt.eclipseprojects.io`` ``openssl s_client -showcerts -connect mqtt.eclipseprojects.io:8883 /dev/null|openssl x509 -outform PEM >mqtt_eclipse_org.pem`` @@ -77,58 +103,54 @@ SSL .. code:: c const esp_mqtt_client_config_t mqtt_cfg = { - .uri = "mqtts://mqtt.eclipseprojects.io:8883", - .event_handle = mqtt_event_handler, - .cert_pem = (const char *)mqtt_eclipse_org_pem_start, + .broker = { + .address.uri = "mqtts://mqtt.eclipseprojects.io:8883", + .verification.certificate = (const char *)mqtt_eclipse_org_pem_start, + }, }; -If the certificate is not null-terminated then ``cert_len`` should also be set. -Other SSL related configuration parameters are: +To details on other fields check the Reference API and :ref:`esp_tls_server_verification`. - * ``use_global_ca_store``: use the global certificate store to verify server certificate, see :component_file:`esp-tls/esp_tls.h` for more information - * ``client_cert_pem``: pointer to certificate data in PEM or DER format for SSL mutual authentication, default is NULL, not required if mutual authentication is not needed. - * ``client_cert_len``: length of the buffer pointed to by client_cert_pem. May be 0 for null-terminated pem. - * ``client_key_pem``: pointer to private key data in PEM or DER format for SSL mutual authentication, default is NULL, not required if mutual authentication is not needed. - * ``client_key_len``: length of the buffer pointed to by client_key_pem. May be 0 for null-terminated pem. - * ``psk_hint_key``: pointer to PSK struct defined in esp_tls.h to enable PSK authentication (as alternative to certificate verification). If not NULL and server/client certificates are NULL, PSK is enabled - * ``alpn_protos``: NULL-terminated list of protocols to be used for ALPN. +Client Credentials +^^^^^^^^^^^^^^^^^^ -Last Will and Testament -^^^^^^^^^^^^^^^^^^^^^^^ -MQTT allows for a last will and testament (LWT) message to notify other clients when a client ungracefully disconnects. This is configured by the following fields -in the ``esp_mqtt_client_config_t``-struct. +All client related credentials are under the ``credentials`` field. - * ``lwt_topic``: pointer to the LWT message topic - * ``lwt_msg``: pointer to the LWT message - * ``lwt_msg_len``: length of the LWT message, required if ``lwt_msg`` is not null-terminated - * ``lwt_qos``: quality of service for the LWT message - * ``lwt_retain``: specifies the retain flag of the LWT message - -Other Configuration Parameters -^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - * ``disable_clean_session``: determines the clean session flag for the connect message, defaults to a clean session - * ``keepalive``: determines how many seconds the client will wait for a ping response before disconnecting, default is 120 seconds. - * ``disable_auto_reconnect``: enable to stop the client from reconnecting to server after errors or disconnects - * ``user_context``: custom context that will be passed to the event handler - * ``task_prio``: MQTT task priority, defaults to 5 - * ``task_stack``: MQTT task stack size, defaults to 6144 bytes, setting this will override setting from menuconfig - * ``buffer_size``: size of MQTT send/receive buffer, default is 1024 bytes - * ``username``: pointer to the username used for connecting to the broker - * ``password``: pointer to the password used for connecting to the broker + * ``username``: pointer to the username used for connecting to the broker, can also be set by URI. * ``client_id``: pointer to the client id, defaults to ``ESP32_%CHIPID%`` where %CHIPID% are the last 3 bytes of MAC address in hex format - * ``host``: MQTT broker domain (ipv4 as string), setting the uri will override this - * ``port``: MQTT broker port, specifying the port in the uri will override this - * ``transport``: sets the transport protocol, setting the uri will override this - * ``refresh_connection_after_ms``: refresh connection after this value (in milliseconds) - * ``event_handle``: handle for MQTT events as a callback in legacy mode - * ``event_loop_handle``: handle for MQTT event loop library +============== +Authentication +============== +It's possible to set authentication parameters through the ``authentication`` field. The client supports the following authentication methods: -For more options on ``esp_mqtt_client_config_t``, please refer to API reference below + * Using a password by setting ``authentication.password``. + * Muthual authentication with TLS by setting ``authentication.certificate`` and ``authentication.key``, both can be provided in PEM or DER format. + * Using secure element available in ESP32-WROOM-32SE, setting ``authentication.use_secure_element``. + * Using Digital Signature Peripheral available in some Espressif devices, setting ``authentication.ds_data``. + +Session +^^^^^^^^^^^ + +For MQTT session related configurations ``section`` fields should be used. + +======================= +Last Will and Testament +======================= + +MQTT allows for a last will and testament (LWT) message to notify other clients when a client ungracefully disconnects. This is configured by the following fields +in the ``esp_mqtt_client_config_t.session.last_will``-struct. + + * ``topic``: pointer to the LWT message topic + * ``msg``: pointer to the LWT message + * ``msg_len``: length of the LWT message, required if ``msg`` is not null-terminated + * ``qos``: quality of service for the LWT message + * ``retain``: specifies the retain flag of the LWT message Change settings in Project Configuration Menu ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + The settings for MQTT can be found using ``idf.py menuconfig``, under Component config -> ESP-MQTT Configuration The following settings are available: @@ -153,8 +175,6 @@ The following events may be posted by the MQTT client: * ``MQTT_EVENT_DATA``: The client has received a publish message. The event data contains: message ID, name of the topic it was published to, received data and its length. For data that exceeds the internal buffer multiple `MQTT_EVENT_DATA` will be posted and `current_data_offset` and `total_data_len` from event data updated to keep track of the fragmented message. * ``MQTT_EVENT_ERROR``: The client has encountered an error. `esp_mqtt_error_type_t` from `error_handle` in the event data can be used to further determine the type of the error. The type of error will determine which parts of the `error_handle` struct is filled. - - API Reference ------------- diff --git a/examples/protocols/mqtt/ssl/main/app_main.c b/examples/protocols/mqtt/ssl/main/app_main.c index 1cbf589..3e6e863 100644 --- a/examples/protocols/mqtt/ssl/main/app_main.c +++ b/examples/protocols/mqtt/ssl/main/app_main.c @@ -44,7 +44,7 @@ static void send_binary(esp_mqtt_client_handle_t client) 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); // sending only the configured portion of the partition (if it's less than the partition size) - int binary_size = MIN(CONFIG_BROKER_BIN_SIZE_TO_SEND,partition->size); + int binary_size = MIN(CONFIG_BROKER_BIN_SIZE_TO_SEND, partition->size); int msg_id = esp_mqtt_client_publish(client, "/topic/binary", binary_address, binary_size, 0, 0); ESP_LOGI(TAG, "binary sent with msg_id=%d", msg_id); } @@ -123,8 +123,10 @@ static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_ static void mqtt_app_start(void) { const esp_mqtt_client_config_t mqtt_cfg = { - .uri = CONFIG_BROKER_URI, - .cert_pem = (const char *)mqtt_eclipseprojects_io_pem_start, + .broker = { + .address.uri = CONFIG_BROKER_URI, + .verification.certificate = (const char *)mqtt_eclipseprojects_io_pem_start + }, }; ESP_LOGI(TAG, "[APP] Free memory: %d bytes", esp_get_free_heap_size()); diff --git a/examples/protocols/mqtt/ssl_ds/main/app_main.c b/examples/protocols/mqtt/ssl_ds/main/app_main.c index 502dce4..79b28bf 100644 --- a/examples/protocols/mqtt/ssl_ds/main/app_main.c +++ b/examples/protocols/mqtt/ssl_ds/main/app_main.c @@ -51,12 +51,24 @@ extern const uint8_t client_cert_pem_end[] asm("_binary_client_crt_end"); extern const uint8_t server_cert_pem_start[] asm("_binary_mosquitto_org_crt_start"); extern const uint8_t server_cert_pem_end[] asm("_binary_mosquitto_org_crt_end"); -static esp_err_t mqtt_event_handler(esp_mqtt_event_handle_t event) +/* + * @brief Event handler registered to receive MQTT events + * + * This function is called by the MQTT client event loop. + * + * @param handler_args user data registered to the event. + * @param base Event base for the handler(always MQTT Base in this example). + * @param event_id The id for the received event. + * @param event_data The data for the event, esp_mqtt_event_handle_t. + */ +static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data) { + ESP_LOGD(TAG, "Event dispatched from event loop base=%s, event_id=%d", base, event_id); + esp_mqtt_event_handle_t event = event_data; esp_mqtt_client_handle_t client = event->client; int msg_id; // your_context_t *context = event->context; - switch (event->event_id) { + switch ((esp_mqtt_event_id_t)event_id) { case MQTT_EVENT_CONNECTED: ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED"); msg_id = esp_mqtt_client_subscribe(client, "/topic/qos0", 0); @@ -95,7 +107,6 @@ static esp_err_t mqtt_event_handler(esp_mqtt_event_handle_t event) ESP_LOGI(TAG, "Other event id:%d", event->event_id); break; } - return ESP_OK; } void *esp_read_ds_data_from_nvs(void) @@ -173,16 +184,22 @@ static void mqtt_app_start(void) vTaskDelete(NULL); } const esp_mqtt_client_config_t mqtt_cfg = { - .uri = "mqtts://test.mosquitto.org:8884", - .event_handle = mqtt_event_handler, - .cert_pem = (const char *)server_cert_pem_start, - .client_cert_pem = (const char *)client_cert_pem_start, - .client_key_pem = NULL, - .ds_data = ds_data, + .broker = { + .address.uri = "mqtts://test.mosquitto.org:8884", + .verification.certificate = (const char *)server_cert_pem_start, + }, + .credentials = { + .authentication = { + .certificate = (const char *)client_cert_pem_start, + .key = NULL, + .ds_data = ds_data + }, + }, }; ESP_LOGI(TAG, "[APP] Free memory: %d bytes", esp_get_free_heap_size()); esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg); + esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, NULL); esp_mqtt_client_start(client); } diff --git a/examples/protocols/mqtt/ssl_mutual_auth/main/app_main.c b/examples/protocols/mqtt/ssl_mutual_auth/main/app_main.c index 48ad78d..76cc455 100644 --- a/examples/protocols/mqtt/ssl_mutual_auth/main/app_main.c +++ b/examples/protocols/mqtt/ssl_mutual_auth/main/app_main.c @@ -111,12 +111,16 @@ static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_ static void mqtt_app_start(void) { - const esp_mqtt_client_config_t mqtt_cfg = { - .uri = "mqtts://test.mosquitto.org:8884", - .client_cert_pem = (const char *)client_cert_pem_start, - .client_key_pem = (const char *)client_key_pem_start, - .cert_pem = (const char *)server_cert_pem_start, - }; + const esp_mqtt_client_config_t mqtt_cfg = { + .broker.address.uri = "mqtts://test.mosquitto.org:8884", + .broker.verification.certificate = (const char *)server_cert_pem_start, + .credentials = { + .authentication = { + .certificate = (const char *)client_cert_pem_start, + .key = (const char *)client_key_pem_start, + }, + } + }; ESP_LOGI(TAG, "[APP] Free memory: %d bytes", esp_get_free_heap_size()); esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg); diff --git a/examples/protocols/mqtt/ssl_psk/main/app_main.c b/examples/protocols/mqtt/ssl_psk/main/app_main.c index cbb4526..f024e13 100644 --- a/examples/protocols/mqtt/ssl_psk/main/app_main.c +++ b/examples/protocols/mqtt/ssl_psk/main/app_main.c @@ -52,12 +52,23 @@ static const psk_hint_key_t psk_hint_key = { .hint = "hint" }; -static esp_err_t mqtt_event_handler(esp_mqtt_event_handle_t event) +/* + * @brief Event handler registered to receive MQTT events + * + * This function is called by the MQTT client event loop. + * + * @param handler_args user data registered to the event. + * @param base Event base for the handler(always MQTT Base in this example). + * @param event_id The id for the received event. + * @param event_data The data for the event, esp_mqtt_event_handle_t. + */ +static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data) { + ESP_LOGD(TAG, "Event dispatched from event loop base=%s, event_id=%d", base, event_id); + esp_mqtt_event_handle_t event = event_data; esp_mqtt_client_handle_t client = event->client; int msg_id; - // your_context_t *context = event->context; - switch (event->event_id) { + switch ((esp_mqtt_event_id_t)event_id) { case MQTT_EVENT_CONNECTED: ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED"); msg_id = esp_mqtt_client_subscribe(client, "/topic/qos0", 0); @@ -96,20 +107,20 @@ static esp_err_t mqtt_event_handler(esp_mqtt_event_handle_t event) ESP_LOGI(TAG, "Other event id:%d", event->event_id); break; } - return ESP_OK; } static void mqtt_app_start(void) { const esp_mqtt_client_config_t mqtt_cfg = { - .uri = EXAMPLE_BROKER_URI, - .event_handle = mqtt_event_handler, - .psk_hint_key = &psk_hint_key, + .broker.address.uri = EXAMPLE_BROKER_URI, + .broker.verification.psk_hint_key = &psk_hint_key, }; ESP_LOGI(TAG, "[APP] Free memory: %d bytes", esp_get_free_heap_size()); esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg); + /* The last argument may be used to pass data to the event handler, in this example mqtt_event_handler */ + esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, NULL); esp_mqtt_client_start(client); } diff --git a/examples/protocols/mqtt/tcp/main/app_main.c b/examples/protocols/mqtt/tcp/main/app_main.c index 47ce74b..f405812 100644 --- a/examples/protocols/mqtt/tcp/main/app_main.c +++ b/examples/protocols/mqtt/tcp/main/app_main.c @@ -110,12 +110,12 @@ static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_ static void mqtt_app_start(void) { esp_mqtt_client_config_t mqtt_cfg = { - .uri = CONFIG_BROKER_URL, + .broker.address.uri = CONFIG_BROKER_URL, }; #if CONFIG_BROKER_URL_FROM_STDIN char line[128]; - if (strcmp(mqtt_cfg.uri, "FROM_STDIN") == 0) { + if (strcmp(mqtt_cfg.broker.address.uri, "FROM_STDIN") == 0) { int count = 0; printf("Please enter url of mqtt broker\n"); while (count < 128) { @@ -129,7 +129,7 @@ static void mqtt_app_start(void) } vTaskDelay(10 / portTICK_PERIOD_MS); } - mqtt_cfg.uri = line; + mqtt_cfg.broker.address.uri = line; printf("Broker url: %s\n", line); } else { ESP_LOGE(TAG, "Configuration mismatch: wrong broker url"); diff --git a/examples/protocols/mqtt/ws/main/app_main.c b/examples/protocols/mqtt/ws/main/app_main.c index 32410e8..247f305 100644 --- a/examples/protocols/mqtt/ws/main/app_main.c +++ b/examples/protocols/mqtt/ws/main/app_main.c @@ -107,7 +107,7 @@ static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_ static void mqtt_app_start(void) { const esp_mqtt_client_config_t mqtt_cfg = { - .uri = CONFIG_BROKER_URI, + .broker.address.uri = CONFIG_BROKER_URI, }; esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg); diff --git a/examples/protocols/mqtt/wss/main/app_main.c b/examples/protocols/mqtt/wss/main/app_main.c index 63cb688..0e570b1 100644 --- a/examples/protocols/mqtt/wss/main/app_main.c +++ b/examples/protocols/mqtt/wss/main/app_main.c @@ -96,8 +96,8 @@ static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_ static void mqtt_app_start(void) { const esp_mqtt_client_config_t mqtt_cfg = { - .uri = CONFIG_BROKER_URI, - .cert_pem = (const char *)mqtt_eclipseprojects_io_pem_start, + .broker.address.uri = CONFIG_BROKER_URI, + .broker.verification.certificate = (const char *)mqtt_eclipseprojects_io_pem_start, }; ESP_LOGI(TAG, "[APP] Free memory: %d bytes", esp_get_free_heap_size()); diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/app_test.py b/tools/test_apps/protocols/mqtt/publish_connect_test/app_test.py index a2366d3..c4f28d2 100644 --- a/tools/test_apps/protocols/mqtt/publish_connect_test/app_test.py +++ b/tools/test_apps/protocols/mqtt/publish_connect_test/app_test.py @@ -355,7 +355,7 @@ def test_app_protocol_mqtt_publish_connect(env, extra_data): return None, None return value.group(1), int(value.group(2)) - publish_cfg['publish_topic'] = dut1.app.get_sdkconfig()['CONFIG_EXAMPLE_SUBSCIBE_TOPIC'].replace('"','') + publish_cfg['publish_topic'] = dut1.app.get_sdkconfig()['CONFIG_EXAMPLE_SUBSCRIBE_TOPIC'].replace('"','') publish_cfg['subscribe_topic'] = dut1.app.get_sdkconfig()['CONFIG_EXAMPLE_PUBLISH_TOPIC'].replace('"','') publish_cfg['broker_host_ssl'], publish_cfg['broker_port_ssl'] = get_host_port_from_dut(dut1, 'CONFIG_EXAMPLE_BROKER_SSL_URI') publish_cfg['broker_host_tcp'], publish_cfg['broker_port_tcp'] = get_host_port_from_dut(dut1, 'CONFIG_EXAMPLE_BROKER_TCP_URI') diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/main/Kconfig.projbuild b/tools/test_apps/protocols/mqtt/publish_connect_test/main/Kconfig.projbuild index f2c3843..3bea21c 100644 --- a/tools/test_apps/protocols/mqtt/publish_connect_test/main/Kconfig.projbuild +++ b/tools/test_apps/protocols/mqtt/publish_connect_test/main/Kconfig.projbuild @@ -30,7 +30,7 @@ menu "Example Configuration" help topic to which esp32 client publishes - config EXAMPLE_SUBSCIBE_TOPIC + config EXAMPLE_SUBSCRIBE_TOPIC string "subscribe topic" default "/topic/subscribe/py2esp" help diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/main/connect_test.c b/tools/test_apps/protocols/mqtt/publish_connect_test/main/connect_test.c index 86f48fd..be07e5a 100644 --- a/tools/test_apps/protocols/mqtt/publish_connect_test/main/connect_test.c +++ b/tools/test_apps/protocols/mqtt/publish_connect_test/main/connect_test.c @@ -64,7 +64,7 @@ static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_ static void create_client(void) { const esp_mqtt_client_config_t mqtt_cfg = { - .uri = "mqtts://127.0.0.1:1234" + .broker.address.uri = "mqtts://127.0.0.1:1234" }; esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg); esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, client); @@ -88,7 +88,7 @@ static void connect_no_certs(const char *host, const int port) char uri[64]; sprintf(uri, "mqtts://%s:%d", host, port); const esp_mqtt_client_config_t mqtt_cfg = { - .uri = uri + .broker.address.uri = uri }; esp_mqtt_set_config(mqtt_client, &mqtt_cfg); esp_mqtt_client_disconnect(mqtt_client); @@ -100,12 +100,12 @@ static void connect_with_client_key_password(const char *host, const int port) char uri[64]; sprintf(uri, "mqtts://%s:%d", host, port); const esp_mqtt_client_config_t mqtt_cfg = { - .uri = uri, - .cert_pem = (const char *)ca_local_crt, - .client_cert_pem = (const char *)client_pwd_crt, - .client_key_pem = (const char *)client_pwd_key, - .clientkey_password = "esp32", - .clientkey_password_len = 5 + .broker.address.uri = uri, + .broker.verification.certificate = (const char *)ca_local_crt, + .credentials.authentication.certificate = (const char *)client_pwd_crt, + .credentials.authentication.key = (const char *)client_pwd_key, + .credentials.authentication.key_password = "esp32", + .credentials.authentication.key_password_len = 5 }; esp_mqtt_set_config(mqtt_client, &mqtt_cfg); esp_mqtt_client_disconnect(mqtt_client); @@ -117,11 +117,11 @@ static void connect_with_server_der_cert(const char *host, const int port) char uri[64]; sprintf(uri, "mqtts://%s:%d", host, port); const esp_mqtt_client_config_t mqtt_cfg = { - .uri = uri, - .cert_pem = (const char *)ca_der_start, - .cert_len = ca_der_end - ca_der_start, - .client_cert_pem = "NULL", - .client_key_pem = "NULL" + .broker.address.uri = uri, + .broker.verification.certificate = (const char *)ca_der_start, + .broker.verification.certificate_len = ca_der_end - ca_der_start, + .credentials.authentication.certificate = "NULL", + .credentials.authentication.key = "NULL" }; esp_mqtt_set_config(mqtt_client, &mqtt_cfg); esp_mqtt_client_disconnect(mqtt_client); @@ -133,10 +133,10 @@ static void connect_with_wrong_server_cert(const char *host, const int port) char uri[64]; sprintf(uri, "mqtts://%s:%d", host, port); const esp_mqtt_client_config_t mqtt_cfg = { - .uri = uri, - .cert_pem = (const char *)client_pwd_crt, - .client_cert_pem = "NULL", - .client_key_pem = "NULL" + .broker.address.uri = uri, + .broker.verification.certificate = (const char *)client_pwd_crt, + .credentials.authentication.certificate = "NULL", + .credentials.authentication.key = "NULL" }; esp_mqtt_set_config(mqtt_client, &mqtt_cfg); esp_mqtt_client_disconnect(mqtt_client); @@ -148,8 +148,8 @@ static void connect_with_server_cert(const char *host, const int port) char uri[64]; sprintf(uri, "mqtts://%s:%d", host, port); const esp_mqtt_client_config_t mqtt_cfg = { - .uri = uri, - .cert_pem = (const char *)ca_local_crt, + .broker.address.uri = uri, + .broker.verification.certificate = (const char *)ca_local_crt, }; esp_mqtt_set_config(mqtt_client, &mqtt_cfg); esp_mqtt_client_disconnect(mqtt_client); @@ -161,10 +161,10 @@ static void connect_with_server_client_certs(const char *host, const int port) char uri[64]; sprintf(uri, "mqtts://%s:%d", host, port); const esp_mqtt_client_config_t mqtt_cfg = { - .uri = uri, - .cert_pem = (const char *)ca_local_crt, - .client_cert_pem = (const char *)client_pwd_crt, - .client_key_pem = (const char *)client_no_pwd_key + .broker.address.uri = uri, + .broker.verification.certificate = (const char *)ca_local_crt, + .credentials.authentication.certificate = (const char *)client_pwd_crt, + .credentials.authentication.key = (const char *)client_no_pwd_key }; esp_mqtt_set_config(mqtt_client, &mqtt_cfg); esp_mqtt_client_disconnect(mqtt_client); @@ -176,10 +176,10 @@ static void connect_with_invalid_client_certs(const char *host, const int port) char uri[64]; sprintf(uri, "mqtts://%s:%d", host, port); const esp_mqtt_client_config_t mqtt_cfg = { - .uri = uri, - .cert_pem = (const char *)ca_local_crt, - .client_cert_pem = (const char *)client_inv_crt, - .client_key_pem = (const char *)client_no_pwd_key + .broker.address.uri = uri, + .broker.verification.certificate = (const char *)ca_local_crt, + .credentials.authentication.certificate = (const char *)client_inv_crt, + .credentials.authentication.key = (const char *)client_no_pwd_key }; esp_mqtt_set_config(mqtt_client, &mqtt_cfg); esp_mqtt_client_disconnect(mqtt_client); @@ -192,8 +192,8 @@ static void connect_with_alpn(const char *host, const int port) const char *alpn_protos[] = { "mymqtt", NULL }; sprintf(uri, "mqtts://%s:%d", host, port); const esp_mqtt_client_config_t mqtt_cfg = { - .uri = uri, - .alpn_protos = alpn_protos + .broker.address.uri = uri, + .broker.verification.alpn_protos = alpn_protos }; esp_mqtt_set_config(mqtt_client, &mqtt_cfg); esp_mqtt_client_disconnect(mqtt_client); diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/main/publish_test.c b/tools/test_apps/protocols/mqtt/publish_connect_test/main/publish_test.c index 38cd136..f49904d 100644 --- a/tools/test_apps/protocols/mqtt/publish_connect_test/main/publish_test.c +++ b/tools/test_apps/protocols/mqtt/publish_connect_test/main/publish_test.c @@ -52,8 +52,8 @@ static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_ case MQTT_EVENT_CONNECTED: ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED"); xEventGroupSetBits(mqtt_event_group, CONNECTED_BIT); - msg_id = esp_mqtt_client_subscribe(client, CONFIG_EXAMPLE_SUBSCIBE_TOPIC, qos_test); - ESP_LOGI(TAG, "sent subscribe successful %s , msg_id=%d", CONFIG_EXAMPLE_SUBSCIBE_TOPIC, msg_id); + msg_id = esp_mqtt_client_subscribe(client, CONFIG_EXAMPLE_SUBSCRIBE_TOPIC, qos_test); + ESP_LOGI(TAG, "sent subscribe successful %s , msg_id=%d", CONFIG_EXAMPLE_SUBSCRIBE_TOPIC, msg_id); break; case MQTT_EVENT_DISCONNECTED: @@ -164,24 +164,24 @@ static void configure_client(char *transport) break; case TCP: ESP_LOGI(TAG, "[TCP transport] Startup.."); - config.uri = CONFIG_EXAMPLE_BROKER_TCP_URI; + config.broker.address.uri = CONFIG_EXAMPLE_BROKER_TCP_URI; break; case SSL: ESP_LOGI(TAG, "[SSL transport] Startup.."); - config.uri = CONFIG_EXAMPLE_BROKER_SSL_URI; + config.broker.address.uri = CONFIG_EXAMPLE_BROKER_SSL_URI; break; case WS: ESP_LOGI(TAG, "[WS transport] Startup.."); - config.uri = CONFIG_EXAMPLE_BROKER_WS_URI; + config.broker.address.uri = CONFIG_EXAMPLE_BROKER_WS_URI; break; case WSS: ESP_LOGI(TAG, "[WSS transport] Startup.."); - config.uri = CONFIG_EXAMPLE_BROKER_WSS_URI; + config.broker.address.uri = CONFIG_EXAMPLE_BROKER_WSS_URI; break; } if (selected_transport == SSL || selected_transport == WSS) { ESP_LOGI(TAG, "Set certificate"); - config.cert_pem = (const char *)mqtt_eclipseprojects_io_pem_start; + config.broker.verification.certificate = (const char *)mqtt_eclipseprojects_io_pem_start; } esp_mqtt_set_config(mqtt_client, &config); From 30b05ecead6b60a877402ec6b7da9337328036a8 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Wed, 11 May 2022 16:01:48 +0200 Subject: [PATCH 135/231] 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/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 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 30c80f963110b93caaa1cd33341c1e29f6a3dcde Mon Sep 17 00:00:00 2001 From: yuanjm Date: Thu, 3 Mar 2022 15:40:03 +0800 Subject: [PATCH 136/231] mqtt: Add mqtt5 Kconfig --- components/mqtt/Kconfig | 6 ++++++ 1 file changed, 6 insertions(+) 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 186dd8a18be5e67e3fb7542fd345031347b47362 Mon Sep 17 00:00:00 2001 From: yuanjm Date: Thu, 3 Mar 2022 15:41:07 +0800 Subject: [PATCH 137/231] 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 17df9effcde33f470c0c7f64be3864bd623a3acd Mon Sep 17 00:00:00 2001 From: yuanjm Date: Thu, 3 Mar 2022 15:41:43 +0800 Subject: [PATCH 138/231] example: Add mqtt5 example --- examples/protocols/mqtt5/CMakeLists.txt | 10 + examples/protocols/mqtt5/README.md | 78 +++++ examples/protocols/mqtt5/main/CMakeLists.txt | 2 + .../protocols/mqtt5/main/Kconfig.projbuild | 13 + examples/protocols/mqtt5/main/app_main.c | 289 ++++++++++++++++++ examples/protocols/mqtt5/pytest_mqtt5.py | 65 ++++ examples/protocols/mqtt5/sdkconfig.ci | 9 + examples/protocols/mqtt5/sdkconfig.defaults | 1 + 8 files changed, 467 insertions(+) create mode 100644 examples/protocols/mqtt5/CMakeLists.txt create mode 100644 examples/protocols/mqtt5/README.md create mode 100644 examples/protocols/mqtt5/main/CMakeLists.txt create mode 100644 examples/protocols/mqtt5/main/Kconfig.projbuild create mode 100644 examples/protocols/mqtt5/main/app_main.c create mode 100644 examples/protocols/mqtt5/pytest_mqtt5.py create mode 100644 examples/protocols/mqtt5/sdkconfig.ci create mode 100644 examples/protocols/mqtt5/sdkconfig.defaults diff --git a/examples/protocols/mqtt5/CMakeLists.txt b/examples/protocols/mqtt5/CMakeLists.txt new file mode 100644 index 0000000..7764590 --- /dev/null +++ b/examples/protocols/mqtt5/CMakeLists.txt @@ -0,0 +1,10 @@ +# The following four lines of boilerplate have to be in your project's CMakeLists +# in this exact order for cmake to work correctly +cmake_minimum_required(VERSION 3.16) + +# (Not part of the boilerplate) +# This example uses an extra component for common functions such as Wi-Fi and Ethernet connection. +set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common) + +include($ENV{IDF_PATH}/tools/cmake/project.cmake) +project(mqtt5) diff --git a/examples/protocols/mqtt5/README.md b/examples/protocols/mqtt5/README.md new file mode 100644 index 0000000..1ac3eaf --- /dev/null +++ b/examples/protocols/mqtt5/README.md @@ -0,0 +1,78 @@ +| Supported Targets | ESP32 | ESP32-C3 | ESP32-S2 | ESP32-S3 | +| ----------------- | ----- | -------- | -------- | -------- | + +# ESP-MQTT sample application +(See the README.md file in the upper level 'examples' directory for more information about examples.) + +This example connects to the broker URI selected using `idf.py menuconfig` (using mqtt tcp transport) and as a demonstration subscribes/unsubscribes and send a message on certain topic. +(Please note that the public broker is maintained by the community so may not be always available, for details please see this [disclaimer](https://iot.eclipse.org/getting-started/#sandboxes)) + +Note: If the URI equals `FROM_STDIN` then the broker address is read from stdin upon application startup (used for testing) + +It uses ESP-MQTT library which implements mqtt client to connect to mqtt broker with MQTT version 5. + +The more details about MQTT v5, please refer to [official website](https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html) + +## How to use example + +### Hardware Required + +This example can be executed on any ESP32 board, the only required interface is WiFi and connection to internet. + +### Configure the project + +* Open the project configuration menu (`idf.py menuconfig`) +* Configure Wi-Fi or Ethernet under "Example Connection Configuration" menu. See "Establishing Wi-Fi or Ethernet Connection" section in [examples/protocols/README.md](../../README.md) for more details. +* MQTT v5 protocol (`CONFIG_MQTT_PROTOCOL_5`) under "ESP-MQTT Configurations" menu is enabled by `sdkconfig.defaults`. + +### Build and Flash + +Build the project and flash it to the board, then run monitor tool to view serial output: + +``` +idf.py -p PORT flash monitor +``` + +(To exit the serial monitor, type ``Ctrl-]``.) + +See the Getting Started Guide for full steps to configure and use ESP-IDF to build projects. + +## Example Output + +``` +I (5119) esp_netif_handlers: example_connect: sta ip: 192.168.3.143, mask: 255.255.255.0, gw: 192.168.3.1 +I (5119) example_connect: Got IPv4 event: Interface "example_connect: sta" address: 192.168.3.143 +I (5619) example_connect: Got IPv6 event: Interface "example_connect: sta" address: fe80:0000:0000:0000:c64f:33ff:fe24:6645, type: ESP_IP6_ADDR_IS_LINK_LOCAL +I (5619) example_connect: Connected to example_connect: sta +I (5629) example_connect: - IPv4 address: 192.168.3.143 +I (5629) example_connect: - IPv6 address: fe80:0000:0000:0000:c64f:33ff:fe24:6645, type: ESP_IP6_ADDR_IS_LINK_LOCAL +I (5649) MQTT5_EXAMPLE: Other event id:7 +W (6299) wifi:idx:0 (ifx:0, 34:29:12:43:c5:40), tid:7, ssn:0, winSize:64 +I (7439) MQTT5_EXAMPLE: MQTT_EVENT_CONNECTED +I (7439) MQTT5_EXAMPLE: sent publish successful, msg_id=53118 +I (7439) MQTT5_EXAMPLE: sent subscribe successful, msg_id=41391 +I (7439) MQTT5_EXAMPLE: sent subscribe successful, msg_id=13695 +I (7449) MQTT5_EXAMPLE: sent unsubscribe successful, msg_id=55594 +I (7649) mqtt5_client: MQTT_MSG_TYPE_PUBACK return code is -1 +I (7649) MQTT5_EXAMPLE: MQTT_EVENT_PUBLISHED, msg_id=53118 +I (8039) mqtt5_client: MQTT_MSG_TYPE_SUBACK return code is 0 +I (8049) MQTT5_EXAMPLE: MQTT_EVENT_SUBSCRIBED, msg_id=41391 +I (8049) MQTT5_EXAMPLE: sent publish successful, msg_id=0 +I (8059) mqtt5_client: MQTT_MSG_TYPE_SUBACK return code is 2 +I (8059) MQTT5_EXAMPLE: MQTT_EVENT_SUBSCRIBED, msg_id=13695 +I (8069) MQTT5_EXAMPLE: sent publish successful, msg_id=0 +I (8079) MQTT5_EXAMPLE: MQTT_EVENT_DATA +I (8079) MQTT5_EXAMPLE: key is board, value is esp32 +I (8079) MQTT5_EXAMPLE: key is u, value is user +I (8089) MQTT5_EXAMPLE: key is p, value is password +I (8089) MQTT5_EXAMPLE: payload_format_indicator is 1 +I (8099) MQTT5_EXAMPLE: response_topic is /topic/test/response +I (8109) MQTT5_EXAMPLE: correlation_data is 123456 +I (8109) MQTT5_EXAMPLE: content_type is +I (8119) MQTT5_EXAMPLE: TOPIC=/topic/qos1 +I (8119) MQTT5_EXAMPLE: DATA=data_3 +I (8129) mqtt5_client: MQTT_MSG_TYPE_UNSUBACK return code is 0 +I (8129) MQTT5_EXAMPLE: MQTT_EVENT_UNSUBSCRIBED, msg_id=55594 +I (8139) mqtt_client: Client asked to disconnect +I (9159) MQTT5_EXAMPLE: MQTT_EVENT_DISCONNECTED +``` diff --git a/examples/protocols/mqtt5/main/CMakeLists.txt b/examples/protocols/mqtt5/main/CMakeLists.txt new file mode 100644 index 0000000..61fac40 --- /dev/null +++ b/examples/protocols/mqtt5/main/CMakeLists.txt @@ -0,0 +1,2 @@ +idf_component_register(SRCS "app_main.c" + INCLUDE_DIRS ".") diff --git a/examples/protocols/mqtt5/main/Kconfig.projbuild b/examples/protocols/mqtt5/main/Kconfig.projbuild new file mode 100644 index 0000000..c11539f --- /dev/null +++ b/examples/protocols/mqtt5/main/Kconfig.projbuild @@ -0,0 +1,13 @@ +menu "Example Configuration" + + config BROKER_URL + string "Broker URL" + default "mqtt://mqtt.eclipseprojects.io" + help + URL of the broker to connect to + + config BROKER_URL_FROM_STDIN + bool + default y if BROKER_URL = "FROM_STDIN" + +endmenu diff --git a/examples/protocols/mqtt5/main/app_main.c b/examples/protocols/mqtt5/main/app_main.c new file mode 100644 index 0000000..a0f7ee8 --- /dev/null +++ b/examples/protocols/mqtt5/main/app_main.c @@ -0,0 +1,289 @@ +/* + * SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include +#include +#include +#include "esp_system.h" +#include "nvs_flash.h" +#include "esp_event.h" +#include "esp_netif.h" +#include "protocol_examples_common.h" +#include "esp_log.h" +#include "mqtt_client.h" + +static const char *TAG = "MQTT5_EXAMPLE"; + +static void log_error_if_nonzero(const char *message, int error_code) +{ + if (error_code != 0) { + ESP_LOGE(TAG, "Last error %s: 0x%x", message, error_code); + } +} + +static esp_mqtt5_user_property_item_t user_property_arr[] = { + {"board", "esp32"}, + {"u", "user"}, + {"p", "password"} + }; + +#define USE_PROPERTY_ARR_SIZE sizeof(user_property_arr)/sizeof(esp_mqtt5_user_property_item_t) + +static 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, +}; + +static esp_mqtt5_subscribe_property_config_t subscribe_property = { + .subscribe_id = 25555, + .no_local_flag = false, + .retain_as_published_flag = false, + .retain_handle = 0, + .is_share_subscribe = true, + .share_name = "group1", +}; + +static esp_mqtt5_subscribe_property_config_t subscribe1_property = { + .subscribe_id = 25555, + .no_local_flag = true, + .retain_as_published_flag = false, + .retain_handle = 0, +}; + +static esp_mqtt5_unsubscribe_property_config_t unsubscribe_property = { + .is_share_subscribe = true, + .share_name = "group1", +}; + +static esp_mqtt5_disconnect_property_config_t disconnect_property = { + .session_expiry_interval = 60, + .disconnect_reason = 0, +}; + +static void print_user_property(mqtt5_user_property_handle_t user_property) +{ + if (user_property) { + uint8_t count = esp_mqtt5_client_get_user_property_count(user_property); + if (count) { + esp_mqtt5_user_property_item_t *item = malloc(count * sizeof(esp_mqtt5_user_property_item_t)); + if (esp_mqtt5_client_get_user_property(user_property, item, &count) == ESP_OK) { + for (int i = 0; i < count; i ++) { + esp_mqtt5_user_property_item_t *t = &item[i]; + ESP_LOGI(TAG, "key is %s, value is %s", t->key, t->value); + free((char *)t->key); + free((char *)t->value); + } + } + free(item); + } + } +} + +/* + * @brief Event handler registered to receive MQTT events + * + * This function is called by the MQTT client event loop. + * + * @param handler_args user data registered to the event. + * @param base Event base for the handler(always MQTT Base in this example). + * @param event_id The id for the received event. + * @param event_data The data for the event, esp_mqtt_event_handle_t. + */ +static void mqtt5_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data) +{ + ESP_LOGD(TAG, "Event dispatched from event loop base=%s, event_id=%d", base, event_id); + esp_mqtt_event_handle_t event = event_data; + esp_mqtt_client_handle_t client = event->client; + int msg_id; + + ESP_LOGD(TAG, "free heap size is %d, maxminu %d", esp_get_free_heap_size(), esp_get_minimum_free_heap_size()); + switch ((esp_mqtt_event_id_t)event_id) { + case MQTT_EVENT_CONNECTED: + ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED"); + print_user_property(event->property->user_property); + esp_mqtt5_client_set_user_property(&publish_property.user_property, user_property_arr, USE_PROPERTY_ARR_SIZE); + esp_mqtt5_client_set_publish_property(client, &publish_property); + msg_id = esp_mqtt_client_publish(client, "/topic/qos1", "data_3", 0, 1, 1); + esp_mqtt5_client_delete_user_property(publish_property.user_property); + publish_property.user_property = NULL; + ESP_LOGI(TAG, "sent publish successful, msg_id=%d", msg_id); + + esp_mqtt5_client_set_user_property(&subscribe_property.user_property, user_property_arr, USE_PROPERTY_ARR_SIZE); + esp_mqtt5_client_set_subscribe_property(client, &subscribe_property); + msg_id = esp_mqtt_client_subscribe(client, "/topic/qos0", 0); + esp_mqtt5_client_delete_user_property(subscribe_property.user_property); + subscribe_property.user_property = NULL; + ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id); + + esp_mqtt5_client_set_user_property(&subscribe1_property.user_property, user_property_arr, USE_PROPERTY_ARR_SIZE); + esp_mqtt5_client_set_subscribe_property(client, &subscribe1_property); + msg_id = esp_mqtt_client_subscribe(client, "/topic/qos1", 2); + esp_mqtt5_client_delete_user_property(subscribe1_property.user_property); + subscribe1_property.user_property = NULL; + ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id); + + esp_mqtt5_client_set_user_property(&unsubscribe_property.user_property, user_property_arr, USE_PROPERTY_ARR_SIZE); + esp_mqtt5_client_set_unsubscribe_property(client, &unsubscribe_property); + msg_id = esp_mqtt_client_unsubscribe(client, "/topic/qos0"); + ESP_LOGI(TAG, "sent unsubscribe successful, msg_id=%d", msg_id); + esp_mqtt5_client_delete_user_property(unsubscribe_property.user_property); + unsubscribe_property.user_property = NULL; + break; + case MQTT_EVENT_DISCONNECTED: + ESP_LOGI(TAG, "MQTT_EVENT_DISCONNECTED"); + print_user_property(event->property->user_property); + break; + case MQTT_EVENT_SUBSCRIBED: + ESP_LOGI(TAG, "MQTT_EVENT_SUBSCRIBED, msg_id=%d", event->msg_id); + print_user_property(event->property->user_property); + esp_mqtt5_client_set_publish_property(client, &publish_property); + msg_id = esp_mqtt_client_publish(client, "/topic/qos0", "data", 0, 0, 0); + ESP_LOGI(TAG, "sent publish successful, msg_id=%d", msg_id); + break; + case MQTT_EVENT_UNSUBSCRIBED: + ESP_LOGI(TAG, "MQTT_EVENT_UNSUBSCRIBED, msg_id=%d", event->msg_id); + print_user_property(event->property->user_property); + esp_mqtt5_client_set_user_property(&disconnect_property.user_property, user_property_arr, USE_PROPERTY_ARR_SIZE); + esp_mqtt5_client_set_disconnect_property(client, &disconnect_property); + esp_mqtt5_client_delete_user_property(disconnect_property.user_property); + disconnect_property.user_property = NULL; + esp_mqtt_client_disconnect(client); + break; + case MQTT_EVENT_PUBLISHED: + ESP_LOGI(TAG, "MQTT_EVENT_PUBLISHED, msg_id=%d", event->msg_id); + print_user_property(event->property->user_property); + break; + case MQTT_EVENT_DATA: + ESP_LOGI(TAG, "MQTT_EVENT_DATA"); + print_user_property(event->property->user_property); + ESP_LOGI(TAG, "payload_format_indicator is %d", event->property->payload_format_indicator); + ESP_LOGI(TAG, "response_topic is %.*s", event->property->response_topic_len, event->property->response_topic); + ESP_LOGI(TAG, "correlation_data is %.*s", event->property->correlation_data_len, event->property->correlation_data); + ESP_LOGI(TAG, "content_type is %.*s", event->property->content_type_len, event->property->content_type); + ESP_LOGI(TAG, "TOPIC=%.*s", event->topic_len, event->topic); + ESP_LOGI(TAG, "DATA=%.*s", event->data_len, event->data); + break; + case MQTT_EVENT_ERROR: + ESP_LOGI(TAG, "MQTT_EVENT_ERROR"); + print_user_property(event->property->user_property); + ESP_LOGI(TAG, "MQTT5 return code is %d", event->error_handle->connect_return_code); + if (event->error_handle->error_type == MQTT_ERROR_TYPE_TCP_TRANSPORT) { + log_error_if_nonzero("reported from esp-tls", event->error_handle->esp_tls_last_esp_err); + log_error_if_nonzero("reported from tls stack", event->error_handle->esp_tls_stack_err); + log_error_if_nonzero("captured as transport's socket errno", event->error_handle->esp_transport_sock_errno); + ESP_LOGI(TAG, "Last errno string (%s)", strerror(event->error_handle->esp_transport_sock_errno)); + } + break; + default: + ESP_LOGI(TAG, "Other event id:%d", event->event_id); + break; + } +} + +static void mqtt5_app_start(void) +{ + 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, + .response_topic = "/test/response", + .correlation_data = "123456", + .correlation_data_len = 6, + }; + + esp_mqtt_client_config_t mqtt5_cfg = { + .broker.address.uri = CONFIG_BROKER_URL, + .session.protocol_ver = MQTT_PROTOCOL_V_5, + .network.disable_auto_reconnect = true, + .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, + }; + +#if CONFIG_BROKER_URL_FROM_STDIN + char line[128]; + + if (strcmp(mqtt5_cfg.uri, "FROM_STDIN") == 0) { + int count = 0; + printf("Please enter url of mqtt broker\n"); + while (count < 128) { + int c = fgetc(stdin); + if (c == '\n') { + line[count] = '\0'; + break; + } else if (c > 0 && c < 127) { + line[count] = c; + ++count; + } + vTaskDelay(10 / portTICK_PERIOD_MS); + } + mqtt5_cfg.broker.address.uri = line; + printf("Broker url: %s\n", line); + } else { + ESP_LOGE(TAG, "Configuration mismatch: wrong broker url"); + abort(); + } +#endif /* CONFIG_BROKER_URL_FROM_STDIN */ + + esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt5_cfg); + + /* Set connection properties and user properties */ + esp_mqtt5_client_set_user_property(&connect_property.user_property, user_property_arr, USE_PROPERTY_ARR_SIZE); + esp_mqtt5_client_set_user_property(&connect_property.will_user_property, user_property_arr, USE_PROPERTY_ARR_SIZE); + esp_mqtt5_client_set_connect_property(client, &connect_property); + + /* If you call esp_mqtt5_client_set_user_property to set user properties, DO NOT forget to delete them. + * esp_mqtt5_client_set_connect_property will malloc buffer to store the user_property and you can delete it after + */ + esp_mqtt5_client_delete_user_property(connect_property.user_property); + esp_mqtt5_client_delete_user_property(connect_property.will_user_property); + + /* The last argument may be used to pass data to the event handler, in this example mqtt_event_handler */ + esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt5_event_handler, NULL); + esp_mqtt_client_start(client); +} + +void app_main(void) +{ + ESP_LOGI(TAG, "[APP] Startup.."); + ESP_LOGI(TAG, "[APP] Free memory: %d bytes", esp_get_free_heap_size()); + ESP_LOGI(TAG, "[APP] IDF version: %s", esp_get_idf_version()); + + esp_log_level_set("*", ESP_LOG_INFO); + esp_log_level_set("MQTT_CLIENT", ESP_LOG_VERBOSE); + esp_log_level_set("MQTT_EXAMPLE", ESP_LOG_VERBOSE); + esp_log_level_set("TRANSPORT_BASE", ESP_LOG_VERBOSE); + esp_log_level_set("esp-tls", ESP_LOG_VERBOSE); + esp_log_level_set("TRANSPORT", ESP_LOG_VERBOSE); + esp_log_level_set("OUTBOX", ESP_LOG_VERBOSE); + + ESP_ERROR_CHECK(nvs_flash_init()); + ESP_ERROR_CHECK(esp_netif_init()); + ESP_ERROR_CHECK(esp_event_loop_create_default()); + + /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig. + * Read "Establishing Wi-Fi or Ethernet Connection" section in + * examples/protocols/README.md for more information about this function. + */ + ESP_ERROR_CHECK(example_connect()); + + mqtt5_app_start(); +} diff --git a/examples/protocols/mqtt5/pytest_mqtt5.py b/examples/protocols/mqtt5/pytest_mqtt5.py new file mode 100644 index 0000000..045ec40 --- /dev/null +++ b/examples/protocols/mqtt5/pytest_mqtt5.py @@ -0,0 +1,65 @@ +#!/usr/bin/env python +# +# SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD +# SPDX-License-Identifier: Apache-2.0 + +import logging +import os + +import pytest +from pytest_embedded import Dut + + +@pytest.mark.esp32 +@pytest.mark.ethernet +def test_examples_protocol_mqtt5(dut: Dut) -> None: + """ + steps: | + 1. join AP + 2. connect to mqtt://mqtt.eclipseprojects.io + 3. check conneciton success + """ + # check and log bin size + binary_file = os.path.join(dut.app.binary_path, 'mqtt5.bin') + bin_size = os.path.getsize(binary_file) + logging.info('mqtt5_bin_size : {}KB'.format(bin_size // 1024)) + # check if connected or not + dut.expect_exact('MQTT_EVENT_CONNECTED', timeout=30) + # check log + res = dut.expect(r'sent publish successful, msg_id=(\d+)') + msgid_pub1 = res.group(1).decode('utf8') + res = dut.expect(r'sent subscribe successful, msg_id=(\d+)') + msgid_sub1 = res.group(1).decode('utf8') + res = dut.expect(r'sent subscribe successful, msg_id=(\d+)') + msgid_sub2 = res.group(1).decode('utf8') + res = dut.expect(r'sent unsubscribe successful, msg_id=(\d+)') + msgid_unsub = res.group(1).decode('utf8') + res = dut.expect(r'MQTT_EVENT_PUBLISHED, msg_id=(\d+)') + msgid_pubd = res.group(1).decode('utf8') + assert msgid_pubd == msgid_pub1 + + res = dut.expect(r'MQTT_EVENT_SUBSCRIBED, msg_id=(\d+)') + msgid_subd = res.group(1).decode('utf8') + assert msgid_subd == msgid_sub1 + + dut.expect_exact('sent publish successful, msg_id=0') + res = dut.expect(r'MQTT_EVENT_SUBSCRIBED, msg_id=(\d+)') + msgid_subd = res.group(1).decode('utf8') + assert msgid_subd == msgid_sub2 + + dut.expect_exact('sent publish successful, msg_id=0') + dut.expect_exact('MQTT_EVENT_DATA') + dut.expect_exact('key is board, value is esp32') + dut.expect_exact('key is u, value is user') + dut.expect_exact('key is p, value is password') + dut.expect_exact('payload_format_indicator is 1') + dut.expect_exact('response_topic is /topic/test/response') + dut.expect_exact('correlation_data is 123456') + dut.expect_exact('TOPIC=/topic/qos1') + dut.expect_exact('DATA=data_3') + res = dut.expect(r'MQTT_EVENT_UNSUBSCRIBED, msg_id=(\d+)') + msgid_unsubd = res.group(1).decode('utf8') + assert msgid_unsubd == msgid_unsub + + dut.expect_exact('MQTT_EVENT_DISCONNECTED') + logging.info('MQTT5 pytest pass') diff --git a/examples/protocols/mqtt5/sdkconfig.ci b/examples/protocols/mqtt5/sdkconfig.ci new file mode 100644 index 0000000..0673297 --- /dev/null +++ b/examples/protocols/mqtt5/sdkconfig.ci @@ -0,0 +1,9 @@ +CONFIG_EXAMPLE_CONNECT_ETHERNET=y +CONFIG_EXAMPLE_CONNECT_WIFI=n +CONFIG_EXAMPLE_USE_INTERNAL_ETHERNET=y +CONFIG_EXAMPLE_ETH_PHY_IP101=y +CONFIG_EXAMPLE_ETH_MDC_GPIO=23 +CONFIG_EXAMPLE_ETH_MDIO_GPIO=18 +CONFIG_EXAMPLE_ETH_PHY_RST_GPIO=5 +CONFIG_EXAMPLE_ETH_PHY_ADDR=1 +CONFIG_MQTT_PROTOCOL_5=y diff --git a/examples/protocols/mqtt5/sdkconfig.defaults b/examples/protocols/mqtt5/sdkconfig.defaults new file mode 100644 index 0000000..db60a2a --- /dev/null +++ b/examples/protocols/mqtt5/sdkconfig.defaults @@ -0,0 +1 @@ +CONFIG_MQTT_PROTOCOL_5=y From 54de87142ccf4757bfd974a855e3d4b6fed8ba1c Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Mon, 31 Jan 2022 19:45:31 +0100 Subject: [PATCH 139/231] build system: re-add -Wno-format as private flag for some components --- components/mqtt/test/CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) 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 649f6d369ed7cd9412e98fca7984492e51689c3a Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Mon, 31 Jan 2022 23:05:03 +0100 Subject: [PATCH 140/231] build system: re-add -Wno-format as private flag for some example components --- examples/protocols/mqtt/ssl/main/CMakeLists.txt | 1 + examples/protocols/mqtt/ssl_ds/main/CMakeLists.txt | 1 + examples/protocols/mqtt/ssl_mutual_auth/main/CMakeLists.txt | 1 + examples/protocols/mqtt/ssl_psk/main/CMakeLists.txt | 1 + examples/protocols/mqtt/tcp/main/CMakeLists.txt | 1 + examples/protocols/mqtt/ws/main/CMakeLists.txt | 1 + examples/protocols/mqtt/wss/main/CMakeLists.txt | 1 + examples/protocols/mqtt5/main/CMakeLists.txt | 1 + 8 files changed, 8 insertions(+) diff --git a/examples/protocols/mqtt/ssl/main/CMakeLists.txt b/examples/protocols/mqtt/ssl/main/CMakeLists.txt index 61fac40..c31750a 100644 --- a/examples/protocols/mqtt/ssl/main/CMakeLists.txt +++ b/examples/protocols/mqtt/ssl/main/CMakeLists.txt @@ -1,2 +1,3 @@ idf_component_register(SRCS "app_main.c" INCLUDE_DIRS ".") +target_compile_options(${COMPONENT_LIB} PRIVATE "-Wno-format") diff --git a/examples/protocols/mqtt/ssl_ds/main/CMakeLists.txt b/examples/protocols/mqtt/ssl_ds/main/CMakeLists.txt index b8ef6ea..7a48b53 100644 --- a/examples/protocols/mqtt/ssl_ds/main/CMakeLists.txt +++ b/examples/protocols/mqtt/ssl_ds/main/CMakeLists.txt @@ -1,3 +1,4 @@ idf_component_register(SRCS "app_main.c" INCLUDE_DIRS "." REQUIRED_IDF_TARGETS esp32s2 esp32c3 esp32s3) +target_compile_options(${COMPONENT_LIB} PRIVATE "-Wno-format") diff --git a/examples/protocols/mqtt/ssl_mutual_auth/main/CMakeLists.txt b/examples/protocols/mqtt/ssl_mutual_auth/main/CMakeLists.txt index 61fac40..c31750a 100644 --- a/examples/protocols/mqtt/ssl_mutual_auth/main/CMakeLists.txt +++ b/examples/protocols/mqtt/ssl_mutual_auth/main/CMakeLists.txt @@ -1,2 +1,3 @@ idf_component_register(SRCS "app_main.c" INCLUDE_DIRS ".") +target_compile_options(${COMPONENT_LIB} PRIVATE "-Wno-format") diff --git a/examples/protocols/mqtt/ssl_psk/main/CMakeLists.txt b/examples/protocols/mqtt/ssl_psk/main/CMakeLists.txt index 61fac40..c31750a 100644 --- a/examples/protocols/mqtt/ssl_psk/main/CMakeLists.txt +++ b/examples/protocols/mqtt/ssl_psk/main/CMakeLists.txt @@ -1,2 +1,3 @@ idf_component_register(SRCS "app_main.c" INCLUDE_DIRS ".") +target_compile_options(${COMPONENT_LIB} PRIVATE "-Wno-format") diff --git a/examples/protocols/mqtt/tcp/main/CMakeLists.txt b/examples/protocols/mqtt/tcp/main/CMakeLists.txt index 61fac40..c31750a 100644 --- a/examples/protocols/mqtt/tcp/main/CMakeLists.txt +++ b/examples/protocols/mqtt/tcp/main/CMakeLists.txt @@ -1,2 +1,3 @@ idf_component_register(SRCS "app_main.c" INCLUDE_DIRS ".") +target_compile_options(${COMPONENT_LIB} PRIVATE "-Wno-format") diff --git a/examples/protocols/mqtt/ws/main/CMakeLists.txt b/examples/protocols/mqtt/ws/main/CMakeLists.txt index 61fac40..c31750a 100644 --- a/examples/protocols/mqtt/ws/main/CMakeLists.txt +++ b/examples/protocols/mqtt/ws/main/CMakeLists.txt @@ -1,2 +1,3 @@ idf_component_register(SRCS "app_main.c" INCLUDE_DIRS ".") +target_compile_options(${COMPONENT_LIB} PRIVATE "-Wno-format") diff --git a/examples/protocols/mqtt/wss/main/CMakeLists.txt b/examples/protocols/mqtt/wss/main/CMakeLists.txt index 61fac40..c31750a 100644 --- a/examples/protocols/mqtt/wss/main/CMakeLists.txt +++ b/examples/protocols/mqtt/wss/main/CMakeLists.txt @@ -1,2 +1,3 @@ idf_component_register(SRCS "app_main.c" INCLUDE_DIRS ".") +target_compile_options(${COMPONENT_LIB} PRIVATE "-Wno-format") diff --git a/examples/protocols/mqtt5/main/CMakeLists.txt b/examples/protocols/mqtt5/main/CMakeLists.txt index 61fac40..c31750a 100644 --- a/examples/protocols/mqtt5/main/CMakeLists.txt +++ b/examples/protocols/mqtt5/main/CMakeLists.txt @@ -1,2 +1,3 @@ idf_component_register(SRCS "app_main.c" INCLUDE_DIRS ".") +target_compile_options(${COMPONENT_LIB} PRIVATE "-Wno-format") From caa59d13d768c84c9c6b0a032609596249871071 Mon Sep 17 00:00:00 2001 From: Alexey Lapshin Date: Thu, 7 Jul 2022 00:17:27 +0400 Subject: [PATCH 141/231] build system: re-add -Wno-format as private flag for some test_apps --- tools/test_apps/protocols/mqtt/build_test/main/CMakeLists.txt | 1 + .../protocols/mqtt/publish_connect_test/main/CMakeLists.txt | 1 + 2 files changed, 2 insertions(+) diff --git a/tools/test_apps/protocols/mqtt/build_test/main/CMakeLists.txt b/tools/test_apps/protocols/mqtt/build_test/main/CMakeLists.txt index fabd82e..2e8369c 100644 --- a/tools/test_apps/protocols/mqtt/build_test/main/CMakeLists.txt +++ b/tools/test_apps/protocols/mqtt/build_test/main/CMakeLists.txt @@ -1,2 +1,3 @@ idf_component_register(SRCS "mqtt_app.cpp" INCLUDE_DIRS ".") +target_compile_options(${COMPONENT_LIB} PRIVATE "-Wno-format") diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/main/CMakeLists.txt b/tools/test_apps/protocols/mqtt/publish_connect_test/main/CMakeLists.txt index d03e617..657a574 100644 --- a/tools/test_apps/protocols/mqtt/publish_connect_test/main/CMakeLists.txt +++ b/tools/test_apps/protocols/mqtt/publish_connect_test/main/CMakeLists.txt @@ -1,2 +1,3 @@ idf_component_register(SRCS "publish_test.c" "connect_test.c" "publish_connect_test.c" INCLUDE_DIRS ".") +target_compile_options(${COMPONENT_LIB} PRIVATE "-Wno-format") From 1cf692ec34f4806f67134af2529700e39069b57f Mon Sep 17 00:00:00 2001 From: "simon.chupin" Date: Thu, 7 Jul 2022 14:45:09 +0200 Subject: [PATCH 142/231] tools: remove the dependency on the future package --- examples/protocols/mqtt/ssl/mqtt_ssl_example_test.py | 3 --- examples/protocols/mqtt/ws/mqtt_ws_example_test.py | 3 --- examples/protocols/mqtt/wss/mqtt_wss_example_test.py | 3 --- 3 files changed, 9 deletions(-) diff --git a/examples/protocols/mqtt/ssl/mqtt_ssl_example_test.py b/examples/protocols/mqtt/ssl/mqtt_ssl_example_test.py index 3309ca4..51ac4e9 100644 --- a/examples/protocols/mqtt/ssl/mqtt_ssl_example_test.py +++ b/examples/protocols/mqtt/ssl/mqtt_ssl_example_test.py @@ -1,10 +1,7 @@ -from __future__ import print_function, unicode_literals - import os import re import ssl import sys -from builtins import str from threading import Event, Thread import paho.mqtt.client as mqtt diff --git a/examples/protocols/mqtt/ws/mqtt_ws_example_test.py b/examples/protocols/mqtt/ws/mqtt_ws_example_test.py index 72d4fa7..c24cfec 100644 --- a/examples/protocols/mqtt/ws/mqtt_ws_example_test.py +++ b/examples/protocols/mqtt/ws/mqtt_ws_example_test.py @@ -1,9 +1,6 @@ -from __future__ import print_function, unicode_literals - import os import re import sys -from builtins import str from threading import Event, Thread import paho.mqtt.client as mqtt diff --git a/examples/protocols/mqtt/wss/mqtt_wss_example_test.py b/examples/protocols/mqtt/wss/mqtt_wss_example_test.py index 9f8ab12..c0a1454 100644 --- a/examples/protocols/mqtt/wss/mqtt_wss_example_test.py +++ b/examples/protocols/mqtt/wss/mqtt_wss_example_test.py @@ -1,10 +1,7 @@ -from __future__ import unicode_literals - import os import re import ssl import sys -from builtins import str from threading import Event, Thread import paho.mqtt.client as mqtt From 3aa8307f03261128723dd23ded6f2398de98bdce Mon Sep 17 00:00:00 2001 From: Roland Dobai Date: Wed, 10 Aug 2022 09:01:57 +0200 Subject: [PATCH 143/231] Tools: Fix flake8 version 5 warnings --- .../test_apps/protocols/mqtt/publish_connect_test/app_test.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/app_test.py b/tools/test_apps/protocols/mqtt/publish_connect_test/app_test.py index c4f28d2..f87c376 100644 --- a/tools/test_apps/protocols/mqtt/publish_connect_test/app_test.py +++ b/tools/test_apps/protocols/mqtt/publish_connect_test/app_test.py @@ -31,7 +31,7 @@ def set_server_cert_cn(ip): '-CAkey', _path('ca.key'), '-CAcreateserial', '-out', _path('srv.crt'), '-days', '360']] for args in arg_list: if subprocess.check_call(args) != 0: - raise('openssl command {} failed'.format(args)) + raise RuntimeError('openssl command {} failed'.format(args)) # Publisher class creating a python client to send/receive published data from esp-mqtt client @@ -264,7 +264,7 @@ def connection_tests(dut, cases, dut_ip): dut.expect('MQTT_EVENT_ERROR: Test={}'.format(test_nr), timeout=30) dut.expect('ESP-TLS ERROR: ESP_ERR_MBEDTLS_SSL_HANDSHAKE_FAILED') # expect ... handshake error (PEER_DID_NOT_RETURN_A_CERTIFICATE) if 'PEER_DID_NOT_RETURN_A_CERTIFICATE' not in s.get_last_ssl_error(): - raise('Unexpected ssl error from the server {}'.format(s.get_last_ssl_error())) + raise RuntimeError('Unexpected ssl error from the server {}'.format(s.get_last_ssl_error())) for case in ['CONFIG_EXAMPLE_CONNECT_CASE_MUTUAL_AUTH', 'CONFIG_EXAMPLE_CONNECT_CASE_MUTUAL_AUTH_KEY_PWD']: # These cases connect to server with both server and client verification (client key might be password protected) From bfa9a90d993d2a8ff2220c3541b75fe81537fbde Mon Sep 17 00:00:00 2001 From: Aditya Patwardhan Date: Wed, 24 Aug 2022 23:19:51 +0530 Subject: [PATCH 144/231] mqtt/ssl_ds example: Remove configure_ds.py script, use esp-secure-cert-tool instread --- examples/protocols/mqtt/ssl_ds/README.md | 14 +- .../protocols/mqtt/ssl_ds/configure_ds.py | 393 ------------------ .../mqtt/ssl_ds/main/idf_component.yml | 3 + 3 files changed, 12 insertions(+), 398 deletions(-) delete mode 100644 examples/protocols/mqtt/ssl_ds/configure_ds.py create mode 100644 examples/protocols/mqtt/ssl_ds/main/idf_component.yml diff --git a/examples/protocols/mqtt/ssl_ds/README.md b/examples/protocols/mqtt/ssl_ds/README.md index 065bcfc..cbb7f0c 100644 --- a/examples/protocols/mqtt/ssl_ds/README.md +++ b/examples/protocols/mqtt/ssl_ds/README.md @@ -40,22 +40,26 @@ openssl genrsa -out client.key openssl req -out client.csr -key client.key -new ``` -Paste the generated CSR in the [Mosquitto test certificate signer](https://test.mosquitto.org/ssl/index.php), click Submit and copy the downloaded `client.crt` in the `main` directory. - -Please note, that the supplied file `client.crt` in the `main` directory is only a placeholder for your client certificate (i.e. the example "as is" would compile but would not connect to the broker) +Paste the generated CSR in the [Mosquitto test certificate signer](https://test.mosquitto.org/ssl/index.php), click Submit and downloaded the `client.crt`. This `client.crt` file shall be used as the device certificate. #### 3) Configure the DS peripheral -* The DS peripheral can be configured with the python script [configure_ds.py](README.md#configure_ds-py) by executing the following command +* i) Install the [esp_secure_cert configuration utility](https://github.com/espressif/esp_secure_cert_mgr/tree/main/tools#esp_secure_cert-configuration-tool) with following command: +``` +pip install esp-secure-cert-tool +``` +* The DS peripheral can be configured by executing the following command: ``` - python configure_ds.py --port /* USB COM port */ --private-key /* RSA priv key */ +configure_esp_secure_cert.py -p /* Serial port */ --device-cert /* Device cert */ --private-key /* RSA priv key */ --target_chip /* target chip */ --configure_ds ``` In the command USB COM port is nothing but the serial port to which the ESP chip is connected. see [check serial port](https://docs.espressif.com/projects/esp-idf/en/latest/esp32/get-started/establish-serial-connection.html#check-port-on-windows) for more details. RSA private key is nothing but the client private key ( RSA ) generated in Step 2. +> Note: More details about the `esp-secure-cert-tool` utility can be found [here](https://github.com/espressif/esp_secure_cert_mgr/tree/main/tools). + #### 4) Connection cofiguration * Open the project configuration menu (`idf.py menuconfig`) * Configure Wi-Fi or Ethernet under "Example Connection Configuration" menu. See "Establishing Wi-Fi or Ethernet Connection" section in [examples/protocols/README.md](../../README.md) for more details. diff --git a/examples/protocols/mqtt/ssl_ds/configure_ds.py b/examples/protocols/mqtt/ssl_ds/configure_ds.py deleted file mode 100644 index d847db2..0000000 --- a/examples/protocols/mqtt/ssl_ds/configure_ds.py +++ /dev/null @@ -1,393 +0,0 @@ -#!/usr/bin/env python -# SPDX-FileCopyrightText: 2020-2022 Espressif Systems (Shanghai) CO LTD -# SPDX-License-Identifier: Apache-2.0 -import argparse -import hashlib -import hmac -import json -import os -import struct -import subprocess -import sys - -from cryptography.hazmat.backends import default_backend -from cryptography.hazmat.primitives import serialization -from cryptography.hazmat.primitives.asymmetric import rsa -from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes -from cryptography.utils import int_to_bytes - -try: - import nvs_partition_gen as nvs_gen -except ImportError: - idf_path = os.getenv('IDF_PATH') - if not idf_path or not os.path.exists(idf_path): - raise Exception('IDF_PATH not found') - sys.path.insert(0, os.path.join(idf_path, 'components', 'nvs_flash', 'nvs_partition_generator')) - import nvs_partition_gen as nvs_gen - -# Check python version is proper or not to avoid script failure -assert sys.version_info >= (3, 6, 0), 'Python version too low.' - -esp_ds_data_dir = 'esp_ds_data' -# hmac_key_file is generated when HMAC_KEY is calculated, it is used when burning HMAC_KEY to efuse -hmac_key_file = esp_ds_data_dir + '/hmac_key.bin' -# csv and bin filenames are default filenames for nvs partition files created with this script -csv_filename = esp_ds_data_dir + '/pre_prov.csv' -bin_filename = esp_ds_data_dir + '/pre_prov.bin' -expected_json_path = os.path.join('build', 'config', 'sdkconfig.json') -# Targets supported by the script -supported_targets = {'esp32s2', 'esp32c3', 'esp32s3'} -supported_key_size = {'esp32s2':[1024, 2048, 3072, 4096], 'esp32c3':[1024, 2048, 3072], 'esp32s3':[1024, 2048, 3072, 4096]} - - -# @return -# on success idf_target - value of the IDF_TARGET read from build/config/sdkconfig.json -# on failure None -def get_idf_target(): - if os.path.exists(expected_json_path): - sdkconfig = json.load(open(expected_json_path)) - idf_target_read = sdkconfig['IDF_TARGET'] - return idf_target_read - else: - print('ERROR: IDF_TARGET has not been set for the supported targets,' - "\nplase execute command \"idf.py set-target {TARGET}\" in the example directory") - return None - - -def load_privatekey(key_file_path, password=None): - key_file = open(key_file_path, 'rb') - key = key_file.read() - key_file.close() - return serialization.load_pem_private_key(key, password=password, backend=default_backend()) - - -def number_as_bytes(number, pad_bits=None): - """ - Given a number, format as a little endian array of bytes - """ - result = int_to_bytes(number)[::-1] - while pad_bits is not None and len(result) < (pad_bits // 8): - result += b'\x00' - return result - - -# @return -# c : ciphertext_c -# iv : initialization vector -# key_size : key size of the RSA private key in bytes. -# @input -# privkey : path to the RSA private key -# priv_key_pass : path to the RSA privaete key password -# hmac_key : HMAC key value ( to calculate DS params) -# idf_target : The target chip for the script (e.g. esp32s2, esp32c3, esp32s3) -# @info -# The function calculates the encrypted private key parameters. -# Consult the DS documentation (available for the ESP32-S2) in the esp-idf programming guide for more details about the variables and calculations. -def calculate_ds_parameters(privkey, priv_key_pass, hmac_key, idf_target): - private_key = load_privatekey(privkey, priv_key_pass) - if not isinstance(private_key, rsa.RSAPrivateKey): - print('ERROR: Only RSA private keys are supported') - sys.exit(-1) - if hmac_key is None: - print('ERROR: hmac_key cannot be None') - sys.exit(-2) - - priv_numbers = private_key.private_numbers() - pub_numbers = private_key.public_key().public_numbers() - Y = priv_numbers.d - M = pub_numbers.n - key_size = private_key.key_size - if key_size not in supported_key_size[idf_target]: - print('ERROR: Private key size {0} not supported for the target {1},\nthe supported key sizes are {2}' - .format(key_size, idf_target, str(supported_key_size[idf_target]))) - sys.exit(-1) - - iv = os.urandom(16) - - rr = 1 << (key_size * 2) - rinv = rr % pub_numbers.n - mprime = - rsa._modinv(M, 1 << 32) - mprime &= 0xFFFFFFFF - length = key_size // 32 - 1 - - # get max supported key size for the respective target - max_len = max(supported_key_size[idf_target]) - aes_key = hmac.HMAC(hmac_key, b'\xFF' * 32, hashlib.sha256).digest() - - md_in = number_as_bytes(Y, max_len) + \ - number_as_bytes(M, max_len) + \ - number_as_bytes(rinv, max_len) + \ - struct.pack(' Date: Wed, 24 Aug 2022 23:21:02 +0530 Subject: [PATCH 145/231] mqtt/ssl_ds example: Update the code to use `esp_secure_cert` component --- examples/protocols/mqtt/ssl_ds/CMakeLists.txt | 8 -- .../protocols/mqtt/ssl_ds/main/app_main.c | 98 +++---------------- .../protocols/mqtt/ssl_ds/main/client.crt | 1 - examples/protocols/mqtt/ssl_ds/partitions.csv | 6 +- 4 files changed, 16 insertions(+), 97 deletions(-) delete mode 100644 examples/protocols/mqtt/ssl_ds/main/client.crt diff --git a/examples/protocols/mqtt/ssl_ds/CMakeLists.txt b/examples/protocols/mqtt/ssl_ds/CMakeLists.txt index c45c2a0..e821859 100644 --- a/examples/protocols/mqtt/ssl_ds/CMakeLists.txt +++ b/examples/protocols/mqtt/ssl_ds/CMakeLists.txt @@ -9,12 +9,4 @@ set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_exam include($ENV{IDF_PATH}/tools/cmake/project.cmake) project(mqtt_ssl_ds) -# Flash the custom partition named `pre_prov`. -set(partition pre_prov) -idf_build_get_property(project_dir PROJECT_DIR) -set(image_file ${project_dir}/esp_ds_data/${partition}.bin) -partition_table_get_partition_info(offset "--partition-name ${partition}" "offset") -esptool_py_flash_target_image(flash "${partition}" "${offset}" "${image_file}") - -target_add_binary_data(${CMAKE_PROJECT_NAME}.elf "main/client.crt" TEXT) target_add_binary_data(${CMAKE_PROJECT_NAME}.elf "main/mosquitto.org.crt" TEXT) diff --git a/examples/protocols/mqtt/ssl_ds/main/app_main.c b/examples/protocols/mqtt/ssl_ds/main/app_main.c index 79b28bf..ba94cf0 100644 --- a/examples/protocols/mqtt/ssl_ds/main/app_main.c +++ b/examples/protocols/mqtt/ssl_ds/main/app_main.c @@ -29,25 +29,10 @@ #include "esp_log.h" #include "mqtt_client.h" #include "rsa_sign_alt.h" +#include "esp_secure_cert_read.h" -/* pre_prov - name of partition containing encrypted prv key parameters ( It is set as such to synchronize it with the pre provisioning service */ -#define NVS_PARTITION_NAME "pre_prov" -/* esp_ds_ns - namespace used for defining values in esp_ds_nvs */ -#define NVS_NAMESPACE "esp_ds_ns" -/* esp_ds_key_id - efuse key block id where 256 bit key is stored, which will be read by - * DS module to perform DS operation */ -#define NVS_EFUSE_KEY_ID "esp_ds_key_id" -/* esp_ds_rsa_len - length of RSA private key (in bits) which is encrypted */ -#define NVS_RSA_LEN "esp_ds_rsa_len" -/* following entries denote key(ASCII string) for particular value in key-value pair of esp_ds_nvs (which are defined in esp_ds_ns) */ -/* ciphertext_c - encrypted RSA private key, see ESP32-S2 Techincal Reference Manual for more details */ -#define NVS_CIPHER_C "esp_ds_c" -/* initialization vector (iv) - 256 bit value used to encrypt RSA private key (to generate ciphertext_c) */ -#define NVS_IV "esp_ds_iv" static const char *TAG = "MQTTS_EXAMPLE"; -extern const uint8_t client_cert_pem_start[] asm("_binary_client_crt_start"); -extern const uint8_t client_cert_pem_end[] asm("_binary_client_crt_end"); extern const uint8_t server_cert_pem_start[] asm("_binary_mosquitto_org_crt_start"); extern const uint8_t server_cert_pem_end[] asm("_binary_mosquitto_org_crt_end"); @@ -109,80 +94,23 @@ static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_ } } -void *esp_read_ds_data_from_nvs(void) -{ - esp_ds_data_ctx_t *ds_data_ctx; - ds_data_ctx = (esp_ds_data_ctx_t *)malloc(sizeof(esp_ds_data_ctx_t)); - if (ds_data_ctx == NULL) { - ESP_LOGE(TAG, "Error in allocating memory for esp_ds_data_context"); - goto exit; - } - - ds_data_ctx->esp_ds_data = (esp_ds_data_t *)calloc(1, sizeof(esp_ds_data_t)); - if (ds_data_ctx->esp_ds_data == NULL) { - ESP_LOGE(TAG, "Could not allocate memory for DS data handle "); - goto exit; - } - - nvs_handle_t esp_ds_nvs_handle; - esp_err_t esp_ret; - esp_ret = nvs_flash_init_partition(NVS_PARTITION_NAME); - if (esp_ret != ESP_OK) { - ESP_LOGE(TAG, "Error in esp_ds_nvs partition init,\nreturned %02x (%s)", esp_ret, esp_err_to_name(esp_ret)); - goto exit; - } - - esp_ret = nvs_open_from_partition(NVS_PARTITION_NAME, NVS_NAMESPACE, - NVS_READONLY, &esp_ds_nvs_handle); - if (esp_ret != ESP_OK) { - ESP_LOGE(TAG, "Error in esp_ds_nvs partition open,\nreturned %02x (%s)", esp_ret, esp_err_to_name(esp_ret)); - goto exit; - } - - esp_ret = nvs_get_u8(esp_ds_nvs_handle, NVS_EFUSE_KEY_ID, &ds_data_ctx->efuse_key_id); - if (esp_ret != ESP_OK) { - ESP_LOGE(TAG, "Error in efuse_key_id value from nvs,\nreturned %02x (%s)", esp_ret, esp_err_to_name(esp_ret)); - goto exit; - } - - esp_ret = nvs_get_u16(esp_ds_nvs_handle, NVS_RSA_LEN, &ds_data_ctx->rsa_length_bits); - if (esp_ret != ESP_OK) { - ESP_LOGE(TAG, "Error in reading rsa key length value from nvs,\nreturned %02x (%s)", esp_ret, esp_err_to_name(esp_ret)); - goto exit; - } - - size_t blob_length = ESP_DS_C_LEN; - esp_ret = nvs_get_blob(esp_ds_nvs_handle, NVS_CIPHER_C, (void *)(ds_data_ctx->esp_ds_data->c), &blob_length); - if ((esp_ret != ESP_OK) || (blob_length != ESP_DS_C_LEN)) { - ESP_LOGE(TAG, "Error in reading ciphertext_c value from nvs,bytes_read = %d,\nreturned %02x (%s)", blob_length, esp_ret, esp_err_to_name(esp_ret)); - goto exit; - } - - blob_length = ESP_DS_IV_LEN; - esp_ret = nvs_get_blob(esp_ds_nvs_handle, NVS_IV, (void *)(ds_data_ctx->esp_ds_data->iv), &blob_length); - if ((esp_ret != ESP_OK) || (blob_length != ESP_DS_IV_LEN)) { - ESP_LOGE(TAG, "Error in reading initialization vector value from nvs,bytes_read = %d,\nreturned %02x (%s)", blob_length, esp_ret, esp_err_to_name(esp_ret)); - goto exit; - } - - return (void *)ds_data_ctx; -exit: - if (ds_data_ctx != NULL) { - free(ds_data_ctx->esp_ds_data); - } - free(ds_data_ctx); - return NULL; -} - static void mqtt_app_start(void) { - /* The context is used by the DS peripheral, should not be freed */ - void *ds_data = esp_read_ds_data_from_nvs(); + esp_ds_data_ctx_t *ds_data = esp_secure_cert_get_ds_ctx(); if (ds_data == NULL) { ESP_LOGE(TAG, "Error in reading DS data from NVS"); vTaskDelete(NULL); } + char *device_cert = NULL; + esp_err_t ret; + uint32_t len; + ret = esp_secure_cert_get_device_cert(&device_cert, &len); + if (ret != ESP_OK) { + ESP_LOGE(TAG, "Failed to obtain the device certificate"); + vTaskDelete(NULL); + } + const esp_mqtt_client_config_t mqtt_cfg = { .broker = { .address.uri = "mqtts://test.mosquitto.org:8884", @@ -190,9 +118,9 @@ static void mqtt_app_start(void) }, .credentials = { .authentication = { - .certificate = (const char *)client_cert_pem_start, + .certificate = (const char *)device_cert, .key = NULL, - .ds_data = ds_data + .ds_data = (void *)ds_data }, }, }; diff --git a/examples/protocols/mqtt/ssl_ds/main/client.crt b/examples/protocols/mqtt/ssl_ds/main/client.crt deleted file mode 100644 index 7a3074b..0000000 --- a/examples/protocols/mqtt/ssl_ds/main/client.crt +++ /dev/null @@ -1 +0,0 @@ -Please paste your client certificate here (follow instructions in README.md) diff --git a/examples/protocols/mqtt/ssl_ds/partitions.csv b/examples/protocols/mqtt/ssl_ds/partitions.csv index 2e1b23d..d59df06 100644 --- a/examples/protocols/mqtt/ssl_ds/partitions.csv +++ b/examples/protocols/mqtt/ssl_ds/partitions.csv @@ -1,6 +1,6 @@ # ESP-IDF Partition Table # Name, Type, SubType, Offset, Size, Flags -nvs,data,nvs,0x9000,24K, -phy_init,data,phy,0xf000,4K, -pre_prov,data,nvs,0x10000,0x3000, +esp_secure_cert,0x3F,,0xD000,0x2000, +nvs,data,nvs,,24K, +phy_init,data,phy,,4K, factory,app,factory,0x20000,1M, From f4f32e7b51e4d3291b23da215a3ed22df5d0c25c Mon Sep 17 00:00:00 2001 From: Aditya Patwardhan Date: Thu, 25 Aug 2022 09:11:30 +0530 Subject: [PATCH 146/231] mqtt/ssl_ds: Add documentation on how to use esp-secure-cert-tool with the example --- examples/protocols/mqtt/ssl_ds/CMakeLists.txt | 7 +++++++ examples/protocols/mqtt/ssl_ds/README.md | 5 +++-- examples/protocols/mqtt/ssl_ds/partitions.csv | 2 +- examples/protocols/mqtt/ssl_ds/sdkconfig.defaults | 6 ++++++ 4 files changed, 17 insertions(+), 3 deletions(-) diff --git a/examples/protocols/mqtt/ssl_ds/CMakeLists.txt b/examples/protocols/mqtt/ssl_ds/CMakeLists.txt index e821859..144cf8b 100644 --- a/examples/protocols/mqtt/ssl_ds/CMakeLists.txt +++ b/examples/protocols/mqtt/ssl_ds/CMakeLists.txt @@ -9,4 +9,11 @@ set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_exam include($ENV{IDF_PATH}/tools/cmake/project.cmake) project(mqtt_ssl_ds) +# Flash the custom partition named `esp_secure_cert`. +set(partition esp_secure_cert) +idf_build_get_property(project_dir PROJECT_DIR) +set(image_file ${project_dir}/esp_secure_cert_data/${partition}.bin) +partition_table_get_partition_info(offset "--partition-name ${partition}" "offset") +esptool_py_flash_target_image(flash "${partition}" "${offset}" "${image_file}") + target_add_binary_data(${CMAKE_PROJECT_NAME}.elf "main/mosquitto.org.crt" TEXT) diff --git a/examples/protocols/mqtt/ssl_ds/README.md b/examples/protocols/mqtt/ssl_ds/README.md index cbb7f0c..168cfcb 100644 --- a/examples/protocols/mqtt/ssl_ds/README.md +++ b/examples/protocols/mqtt/ssl_ds/README.md @@ -48,11 +48,12 @@ Paste the generated CSR in the [Mosquitto test certificate signer](https://test. ``` pip install esp-secure-cert-tool ``` -* The DS peripheral can be configured by executing the following command: +* ii) The DS peripheral can be configured by executing the following command: ``` -configure_esp_secure_cert.py -p /* Serial port */ --device-cert /* Device cert */ --private-key /* RSA priv key */ --target_chip /* target chip */ --configure_ds +configure_esp_secure_cert.py -p /* Serial port */ --device-cert /* Device cert */ --private-key /* RSA priv key */ --target_chip /* target chip */ --configure_ds --skip_flash ``` +This command shall generate a partition named `esp_secure_cert.bin` in the `esp_secure_cert_data` directory. This partition would be aumatically detected by the build system and flashed at appropriate offset when `idf.py flash` command is used. For this process, the command must be executed in the current folder only. In the command USB COM port is nothing but the serial port to which the ESP chip is connected. see [check serial port](https://docs.espressif.com/projects/esp-idf/en/latest/esp32/get-started/establish-serial-connection.html#check-port-on-windows) for more details. diff --git a/examples/protocols/mqtt/ssl_ds/partitions.csv b/examples/protocols/mqtt/ssl_ds/partitions.csv index d59df06..0c4ad77 100644 --- a/examples/protocols/mqtt/ssl_ds/partitions.csv +++ b/examples/protocols/mqtt/ssl_ds/partitions.csv @@ -1,6 +1,6 @@ # ESP-IDF Partition Table # Name, Type, SubType, Offset, Size, Flags -esp_secure_cert,0x3F,,0xD000,0x2000, +esp_secure_cert,0x3F,,,0x2000, nvs,data,nvs,,24K, phy_init,data,phy,,4K, factory,app,factory,0x20000,1M, diff --git a/examples/protocols/mqtt/ssl_ds/sdkconfig.defaults b/examples/protocols/mqtt/ssl_ds/sdkconfig.defaults index 4b0421e..94e282f 100644 --- a/examples/protocols/mqtt/ssl_ds/sdkconfig.defaults +++ b/examples/protocols/mqtt/ssl_ds/sdkconfig.defaults @@ -1 +1,7 @@ CONFIG_PARTITION_TABLE_CUSTOM=y +# Setting partition table offset to 0xC000 would make the address of +# `esp_secure_cert` partition as 0xD000 (comes next in the sequence). +# Modules that are programmed with Espressif Secure Pre Provisioining service +# uses this offset for `esp_secure_cert` and hence this change aligns this example +# to work on those modules. +CONFIG_PARTITION_TABLE_OFFSET=0xC000 From 41782c4f173300859c154b1223ed2935360a73fd Mon Sep 17 00:00:00 2001 From: Aditya Patwardhan Date: Mon, 29 Aug 2022 18:11:05 +0530 Subject: [PATCH 147/231] mqtt/ssl_ds: Remove unwanted references to configure_ds.py, Also updated the DS documentation --- examples/protocols/mqtt/ssl_ds/README.md | 46 ------------------------ 1 file changed, 46 deletions(-) diff --git a/examples/protocols/mqtt/ssl_ds/README.md b/examples/protocols/mqtt/ssl_ds/README.md index 168cfcb..6a321bd 100644 --- a/examples/protocols/mqtt/ssl_ds/README.md +++ b/examples/protocols/mqtt/ssl_ds/README.md @@ -102,49 +102,3 @@ I (5194) MQTTS_EXAMPLE: MQTT_EVENT_DATA TOPIC=/topic/qos0 DATA=data ``` - - -### configure_ds.py -The script [configure_ds.py](./configure_ds.py) is used for configuring the DS peripheral on the ESP32-S2/ESP32-S3/ESP32-C3 SoC. The steps in the script are based on technical details of certain operations in the Digital Signature calculation, which can be found at Digital Signature Section of [ESP32-S2 TRM](https://www.espressif.com/sites/default/files/documentation/esp32-s2_technical_reference_manual_en.pdf) - -The configuration script performs the following steps - - -1. Take the client private key ( RSA key ) as input. - (*required parameter for the script) -can be provided with -``` - python configure_ds.py --private-key /* path to client (rsa) prv key */ -``` - -2. Randomly Calculate the `HMAC_KEY` and the `initialization vector`(IV). Then calculate the encrypted private key parameters from client private key (step i) and newly generated parameters. These encrypted private key parameters are required for the DS peripheral to perform the Digital Signature operation. - -3. Store the `HMAC_KEY` in one of the efuse key blocks (in the hardware). - The ID of the efuse key block ( should be in range 1-5) can be provided with the following option. (default value of 1 is used if not provided), -``` - python configure_ds.py --efuse_key_id /* key id in range 1-5 */ -``` - -Currently for development purposes, the `HMAC_KEY` is stored in the efuse key block without read protection so that read operation can be performed on the same key block. -> You can burn (write) a key on an efuse key block only once. Please use a different key block ID if you want to use a different `HMAC_KEY` for the DS operation. - -4. Create an NVS partition of the name `pre_prov.csv` (in `esp_ds_data` folder) which contains the required encrypted private key parameters. A bin file of the nvs partition (`pre_prov.bin`) is also created. As we have added a custom partition, the example is set to use the custom partition table by adding the required option in `sdkconfig.defaults`. - -5. (optional) The script can be made to print the summary of the efuse on the chip by providing the following option. When this option is enabled, no other operations in the script are performed. -``` - python configure_ds.py --summary -``` - -6. (optional) If the user wants to keep the encrypted private key data and the randomly generated `HMAC_KEY` on the host machine for testing purpose. The following option may be used. -``` - python configure_ds.py --keep_ds_data_on_host -``` - The respective files will be stored in the `esp_ds_data` folder which is generated by the script in the same directory. The contents of the `esp_ds_data` folder may be overwritten when the `configure_ds.py` script is executed again. - -7. (optional) If the user wants to use the script for production usecase then this option can be used. -Currently for development purpose, the script disables the read protection on the efuse key block by default. -In case of a production usecase it is recommeneded to enable the read protection for the efuse key block. It can be done by providing following option along with other required options: -``` - python configure_ds.py --production -``` - -> A list of all the supported options in the script can be obtained by executing `python configure_ds.py --help`. From 3ff7b48680ec90befc799c539ec90e0c0b7d7b2a Mon Sep 17 00:00:00 2001 From: Euripedes Rocha Date: Mon, 5 Sep 2022 15:46:10 +0200 Subject: [PATCH 148/231] [MQTT] Simplifies MQTT CMakeLists file - Uses mocks from tools instead of creating them. - Move host based definition to test code. --- components/mqtt/host_test/CMakeLists.txt | 10 ++++++++-- components/mqtt/host_test/main/CMakeLists.txt | 2 +- components/mqtt/host_test/main/test_mqtt_client.cpp | 3 --- .../host_test/mocks/include/local_FreeRTOS_config.h | 6 ------ components/mqtt/test/test_mqtt.c | 1 + 5 files changed, 10 insertions(+), 12 deletions(-) delete mode 100644 components/mqtt/host_test/mocks/include/local_FreeRTOS_config.h 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 81854fdf757f0f98c65192548a8904a44aafce0e Mon Sep 17 00:00:00 2001 From: yuanjianmin Date: Thu, 29 Sep 2022 15:35:27 +0800 Subject: [PATCH 149/231] ci: Fix mqtt broker is not available --- examples/protocols/mqtt5/sdkconfig.ci | 1 + 1 file changed, 1 insertion(+) diff --git a/examples/protocols/mqtt5/sdkconfig.ci b/examples/protocols/mqtt5/sdkconfig.ci index 0673297..f4a0a7e 100644 --- a/examples/protocols/mqtt5/sdkconfig.ci +++ b/examples/protocols/mqtt5/sdkconfig.ci @@ -7,3 +7,4 @@ CONFIG_EXAMPLE_ETH_MDIO_GPIO=18 CONFIG_EXAMPLE_ETH_PHY_RST_GPIO=5 CONFIG_EXAMPLE_ETH_PHY_ADDR=1 CONFIG_MQTT_PROTOCOL_5=y +CONFIG_BROKER_URL="mqtt://${EXAMPLE_MQTTV5_BROKER_TCP}" From a394a3d70c78a7e20f90e8ff03c883e61e19a521 Mon Sep 17 00:00:00 2001 From: Song Ruo Jing Date: Thu, 27 Oct 2022 18:55:07 +0800 Subject: [PATCH 150/231] ci: Enable esp32c6 example, test_apps, and unit tests CI build stage --- tools/test_apps/protocols/mqtt/build_test/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/test_apps/protocols/mqtt/build_test/README.md b/tools/test_apps/protocols/mqtt/build_test/README.md index 29d3251..2a2585d 100644 --- a/tools/test_apps/protocols/mqtt/build_test/README.md +++ b/tools/test_apps/protocols/mqtt/build_test/README.md @@ -1,5 +1,5 @@ -| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-S2 | ESP32-S3 | -| ----------------- | ----- | -------- | -------- | -------- | -------- | +| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C6 | ESP32-S2 | ESP32-S3 | +| ----------------- | ----- | -------- | -------- | -------- | -------- | -------- | # Build only test for C++ From 0ec688bce65c244ee4196a8d3c4d6a96afaa6b65 Mon Sep 17 00:00:00 2001 From: Wang Zi Yan Date: Tue, 25 Oct 2022 16:30:50 +0800 Subject: [PATCH 151/231] docs: add a blank line in ble-mesh-index and translate mqtt --- docs/en/api-reference/protocols/mqtt.rst | 91 +++++----- docs/zh_CN/api-reference/protocols/mqtt.rst | 179 +++++++++++++++++++- 2 files changed, 222 insertions(+), 48 deletions(-) diff --git a/docs/en/api-reference/protocols/mqtt.rst b/docs/en/api-reference/protocols/mqtt.rst index 1346305..4109c37 100644 --- a/docs/en/api-reference/protocols/mqtt.rst +++ b/docs/en/api-reference/protocols/mqtt.rst @@ -4,42 +4,42 @@ ESP-MQTT Overview -------- -ESP-MQTT is an implementation of [MQTT](mqtt.org) protocol client (MQTT is a lightweight publish/subscribe messaging protocol). +ESP-MQTT is an implementation of `MQTT `_ protocol client. MQTT is a lightweight publish/subscribe messaging protocol. Features -------- - * Supports MQTT over TCP, SSL with mbedtls, MQTT over Websocket, MQTT over Websocket Secure. + * Support MQTT over TCP, SSL with Mbed TLS, MQTT over WebSocket, and MQTT over WebSocket Secure * Easy to setup with URI - * Multiple instances (Multiple clients in one application) - * Support subscribing, publishing, authentication, last will messages, keep alive pings and all 3 QoS levels (it should be a fully functional client). + * Multiple instances (multiple clients in one application) + * Support subscribing, publishing, authentication, last will messages, keep alive pings, and all 3 Quality of Service (QoS) levels (it should be a fully functional client) -Application Example -------------------- +Application Examples +--------------------- - * :example:`protocols/mqtt/tcp`: MQTT over tcp, default port 1883 - * :example:`protocols/mqtt/ssl`: MQTT over tls, default port 8883 - * :example:`protocols/mqtt/ssl_ds`: MQTT over tls using digital signature peripheral for authentication, default port 8883. - * :example:`protocols/mqtt/ssl_mutual_auth`: MQTT over tls using certificates for authentication, default port 8883 - * :example:`protocols/mqtt/ssl_psk`: MQTT over tls using pre-shared keys for authentication, default port 8883. - * :example:`protocols/mqtt/ws`: MQTT over Websocket, default port 80 - * :example:`protocols/mqtt/wss`: MQTT over Websocket Secure, default port 443 + * :example:`protocols/mqtt/tcp`: MQTT over TCP, default port 1883 + * :example:`protocols/mqtt/ssl`: MQTT over TLS, default port 8883 + * :example:`protocols/mqtt/ssl_ds`: MQTT over TLS using digital signature peripheral for authentication, default port 8883 + * :example:`protocols/mqtt/ssl_mutual_auth`: MQTT over TLS using certificates for authentication, default port 8883 + * :example:`protocols/mqtt/ssl_psk`: MQTT over TLS using pre-shared keys for authentication, default port 8883 + * :example:`protocols/mqtt/ws`: MQTT over WebSocket, default port 80 + * :example:`protocols/mqtt/wss`: MQTT over WebSocket Secure, default port 443 Configuration ------------- -The configuration is made by setting fields in ``esp_mqtt_client_config_t`` struct. The configuration struct has the following sub structs to configure different aspects of the client operation. +The configuration is made by setting fields in ``esp_mqtt_client_config_t`` struct. The configuration struct has the following sub structs to configure different aspects of the client operation. * :cpp:member:`broker` - Allow to set address and security verification. * :cpp:member:`credentials` - Client credentials for authentication. * :cpp:member:`session` - Configuration for MQTT session aspects. - * :cpp:member:`network` - Networking related configuration. + * :cpp:member:`network` - Networking related configuration. * :cpp:member:`task` - Allow to configure FreeRTOS task. * :cpp:member:`buffer` - Buffer size for input and output. -In the following session the most common aspects are detailed. +In the following sections, the most common aspects are detailed. Broker ^^^^^^^^^^^ @@ -48,17 +48,16 @@ Broker Address =========== -Broker address can be set by usage of ``broker.address`` struct. The configuration can be made by usage of ``uri`` field -or the combination of ``hostname``, ``transport`` and ``port``. Optionally, `path` could be set, this field is useful in -websocket connections. +Broker address can be set by usage of ``broker.address`` struct. The configuration can be made by usage of ``uri`` field or the combination of ``hostname``, ``transport`` and ``port``. Optionally, ``path`` could be set, this field is useful in WebSocket connections. + +The ``uri`` field is used in the format ``scheme://hostname:port/path``. -The ``uri`` field is used in the following format ``scheme://hostname:port/path``. - Curently support ``mqtt``, ``mqtts``, ``ws``, ``wss`` schemes - MQTT over TCP samples: - - ``mqtt://mqtt.eclipseprojects.io``: MQTT over TCP, default port 1883: - - ``mqtt://mqtt.eclipseprojects.io:1884`` MQTT over TCP, port 1884: - - ``mqtt://username:password@mqtt.eclipseprojects.io:1884`` MQTT over TCP, + - ``mqtt://mqtt.eclipseprojects.io``: MQTT over TCP, default port 1883 + - ``mqtt://mqtt.eclipseprojects.io:1884``: MQTT over TCP, port 1884 + - ``mqtt://username:password@mqtt.eclipseprojects.io:1884``: MQTT over TCP, port 1884, with username and password - MQTT over SSL samples: @@ -66,11 +65,11 @@ The ``uri`` field is used in the following format ``scheme://hostname:port/path` - ``mqtts://mqtt.eclipseprojects.io``: MQTT over SSL, port 8883 - ``mqtts://mqtt.eclipseprojects.io:8884``: MQTT over SSL, port 8884 -- MQTT over Websocket samples: +- MQTT over WebSocket samples: - ``ws://mqtt.eclipseprojects.io:80/mqtt`` -- MQTT over Websocket Secure samples: +- MQTT over WebSocket Secure samples: - ``wss://mqtt.eclipseprojects.io:443/mqtt`` @@ -85,15 +84,14 @@ The ``uri`` field is used in the following format ``scheme://hostname:port/path` esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, client); esp_mqtt_client_start(client); -- Note: By default mqtt client uses event loop library to post related mqtt events (connected, subscribed, published, etc.) +.. note:: By default MQTT client uses event loop library to post related MQTT events (connected, subscribed, published, etc.). ============ Verification ============ -For secure connections TLS is used, and to guarantee Broker's identity the ``broker.verification`` struct must be set. -The broker certificate may be set in PEM or DER format. To select DER the equivalent ``_len`` field must be set, -otherwise a NULL terminated string in PEM format should be provided to ``certificate`` field. +For secure connections with TLS used, and to guarantee Broker's identity, the ``broker.verification`` struct must be set. +The broker certificate may be set in PEM or DER format. To select DER, the equivalent ``_len`` field must be set. Otherwise, a null-terminated string in PEM format should be provided to ``certificate`` field. - Get certificate from server, example: ``mqtt.eclipseprojects.io`` ``openssl s_client -showcerts -connect mqtt.eclipseprojects.io:8883 /dev/null|openssl x509 -outform PEM >mqtt_eclipse_org.pem`` @@ -109,15 +107,15 @@ otherwise a NULL terminated string in PEM format should be provided to ``certifi }, }; -To details on other fields check the Reference API and :ref:`esp_tls_server_verification`. +For details about other fields, please check the `API Reference`_ and :ref:`esp_tls_server_verification`. Client Credentials ^^^^^^^^^^^^^^^^^^ All client related credentials are under the ``credentials`` field. - * ``username``: pointer to the username used for connecting to the broker, can also be set by URI. - * ``client_id``: pointer to the client id, defaults to ``ESP32_%CHIPID%`` where %CHIPID% are the last 3 bytes of MAC address in hex format + * ``username``: pointer to the username used for connecting to the broker, can also be set by URI + * ``client_id``: pointer to the client ID, defaults to ``ESP32_%CHIPID%`` where ``%CHIPID%`` are the last 3 bytes of MAC address in hex format ============== Authentication @@ -125,22 +123,21 @@ Authentication It's possible to set authentication parameters through the ``authentication`` field. The client supports the following authentication methods: - * Using a password by setting ``authentication.password``. - * Muthual authentication with TLS by setting ``authentication.certificate`` and ``authentication.key``, both can be provided in PEM or DER format. - * Using secure element available in ESP32-WROOM-32SE, setting ``authentication.use_secure_element``. - * Using Digital Signature Peripheral available in some Espressif devices, setting ``authentication.ds_data``. + * ``authentication.password``: use a password by setting + * ``authentication.certificate`` and ``authentication.key``: mutual authentication with TLS, and both can be provided in PEM or DER format + * ``authentication.use_secure_element``: use secure element available in ESP32-WROOM-32SE + * ``authentication.ds_data``: use Digital Signature Peripheral available in some Espressif devices Session ^^^^^^^^^^^ -For MQTT session related configurations ``section`` fields should be used. +For MQTT session related configurations, ``section`` fields should be used. ======================= Last Will and Testament ======================= -MQTT allows for a last will and testament (LWT) message to notify other clients when a client ungracefully disconnects. This is configured by the following fields -in the ``esp_mqtt_client_config_t.session.last_will``-struct. +MQTT allows for a last will and testament (LWT) message to notify other clients when a client ungracefully disconnects. This is configured by the following fields in the ``esp_mqtt_client_config_t.session.last_will`` struct. * ``topic``: pointer to the LWT message topic * ``msg``: pointer to the LWT message @@ -148,18 +145,18 @@ in the ``esp_mqtt_client_config_t.session.last_will``-struct. * ``qos``: quality of service for the LWT message * ``retain``: specifies the retain flag of the LWT message -Change settings in Project Configuration Menu +Change Settings in Project Configuration Menu ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -The settings for MQTT can be found using ``idf.py menuconfig``, under Component config -> ESP-MQTT Configuration +The settings for MQTT can be found using ``idf.py menuconfig``, under ``Component config`` > ``ESP-MQTT Configuration``. The following settings are available: -- :ref:`CONFIG_MQTT_PROTOCOL_311`: Enables 3.1.1 version of MQTT protocol +- :ref:`CONFIG_MQTT_PROTOCOL_311`: enable 3.1.1 version of MQTT protocol -- :ref:`CONFIG_MQTT_TRANSPORT_SSL`, :ref:`CONFIG_MQTT_TRANSPORT_WEBSOCKET`: Enables specific MQTT transport layer, such as SSL, WEBSOCKET, WEBSOCKET_SECURE +- :ref:`CONFIG_MQTT_TRANSPORT_SSL` and :ref:`CONFIG_MQTT_TRANSPORT_WEBSOCKET`: enable specific MQTT transport layer, such as SSL, WEBSOCKET, and WEBSOCKET_SECURE -- :ref:`CONFIG_MQTT_CUSTOM_OUTBOX`: Disables default implementation of mqtt_outbox, so a specific implementaion can be supplied +- :ref:`CONFIG_MQTT_CUSTOM_OUTBOX`: disable default implementation of mqtt_outbox, so a specific implementation can be supplied Events @@ -171,9 +168,9 @@ The following events may be posted by the MQTT client: * ``MQTT_EVENT_DISCONNECTED``: The client has aborted the connection due to being unable to read or write data, e.g. because the server is unavailable. * ``MQTT_EVENT_SUBSCRIBED``: The broker has acknowledged the client's subscribe request. The event data will contain the message ID of the subscribe message. * ``MQTT_EVENT_UNSUBSCRIBED``: The broker has acknowledged the client's unsubscribe request. The event data will contain the message ID of the unsubscribe message. -* ``MQTT_EVENT_PUBLISHED``: The broker has acknowledged the client's publish message. This will only be posted for Quality of Service level 1 and 2, as level 0 does not use acknowledgements. The event data will contain the message ID of the publish message. -* ``MQTT_EVENT_DATA``: The client has received a publish message. The event data contains: message ID, name of the topic it was published to, received data and its length. For data that exceeds the internal buffer multiple `MQTT_EVENT_DATA` will be posted and `current_data_offset` and `total_data_len` from event data updated to keep track of the fragmented message. -* ``MQTT_EVENT_ERROR``: The client has encountered an error. `esp_mqtt_error_type_t` from `error_handle` in the event data can be used to further determine the type of the error. The type of error will determine which parts of the `error_handle` struct is filled. +* ``MQTT_EVENT_PUBLISHED``: The broker has acknowledged the client's publish message. This will only be posted for QoS level 1 and 2, as level 0 does not use acknowledgements. The event data will contain the message ID of the publish message. +* ``MQTT_EVENT_DATA``: The client has received a publish message. The event data contains: message ID, name of the topic it was published to, received data and its length. For data that exceeds the internal buffer, multiple ``MQTT_EVENT_DATA`` will be posted and ``current_data_offset`` and ``total_data_len`` from event data updated to keep track of the fragmented message. +* ``MQTT_EVENT_ERROR``: The client has encountered an error. ``esp_mqtt_error_type_t`` from ``error_handle`` in the event data can be used to further determine the type of the error. The type of error will determine which parts of the ``error_handle`` struct is filled. API Reference ------------- diff --git a/docs/zh_CN/api-reference/protocols/mqtt.rst b/docs/zh_CN/api-reference/protocols/mqtt.rst index 4f5ad7d..8f3eefb 100644 --- a/docs/zh_CN/api-reference/protocols/mqtt.rst +++ b/docs/zh_CN/api-reference/protocols/mqtt.rst @@ -1 +1,178 @@ -.. include:: ../../../en/api-reference/protocols/mqtt.rst \ No newline at end of file +ESP-MQTT +======== + +概述 +-------- + +ESP-MQTT 是 `MQTT `_ 协议客户端的实现。MQTT 是一种基于发布/订阅模式的轻量级消息传输协议。 + + +特性 +-------- + * 支持基于 TCP 的 MQTT、基于 Mbed TLS 的 SSL、基于 WebSocket 的 MQTT 以及基于 WebSocket Secure 的 MQTT + * 通过 URI 简化配置流程 + * 多个实例(一个应用程序中有多个客户端) + * 支持订阅、发布、认证、遗嘱消息、保持连接心跳机制以及 3 个服务质量 (QoS) 级别(组成全功能客户端) + + +应用示例 +------------------- + + * :example:`protocols/mqtt/tcp`:基于 TCP 的 MQTT,默认端口 1883 + * :example:`protocols/mqtt/ssl`:基于 TLS 的 MQTT,默认端口 8883 + * :example:`protocols/mqtt/ssl_ds`:基于 TLS 的 MQTT,使用数字签名外设进行身份验证,默认端口 8883 + * :example:`protocols/mqtt/ssl_mutual_auth`:基于 TLS 的 MQTT,使用证书进行身份验证,默认端口 8883 + * :example:`protocols/mqtt/ssl_psk`:基于 TLS 的 MQTT,使用预共享密钥进行身份验证,默认端口 8883 + * :example:`protocols/mqtt/ws`:基于 WebSocket 的 MQTT,默认端口 80 + * :example:`protocols/mqtt/wss`:基于 WebSocket Secure 的 MQTT,默认端口 443 + + +配置 +------------- + +通过设置 ``esp_mqtt_client_config_t`` 结构体中的字段来进行配置。配置结构体包含以下子结构体,用于配置客户端的多种操作。 + + * :cpp:member:`broker` - 允许设置地址和安全验证。 + * :cpp:member:`credentials` - 用于身份验证的客户端凭据。 + * :cpp:member:`session` - MQTT 会话相关配置。 + * :cpp:member:`network` - 网络相关配置。 + * :cpp:member:`task` - 允许配置 FreeRTOS 任务。 + * :cpp:member:`buffer` - 输入输出的缓冲区大小。 + +下文将详细介绍不同配置。 + +服务器 +^^^^^^^^^^^^ + +=========== +地址 +=========== + +通过 ``broker.address`` 结构体的 ``uri`` 字段或者 ``hostname``、``transport`` 以及 ``port`` 的组合,可以设置服务器地址。您也可以选择设置 ``path``,该字段对 WebSocket 连接而言非常有用。 + +使用 ``uri`` 字段的格式为 ``scheme://hostname:port/path``。 + +- 当前支持 ``mqtt``、``mqtts``、``ws`` 和 ``wss`` 协议 +- 基于 TCP 的 MQTT 示例: + + - ``mqtt://mqtt.eclipseprojects.io``:基于 TCP 的 MQTT,默认端口 1883 + - ``mqtt://mqtt.eclipseprojects.io:1884``:基于 TCP 的 MQTT,端口 1884 + - ``mqtt://username:password@mqtt.eclipseprojects.io:1884``:基于 TCP 的 MQTT, + 端口 1884,带有用户名和密码 + +- 基于 SSL 的 MQTT 示例: + + - ``mqtts://mqtt.eclipseprojects.io``:基于 SSL 的 MQTT,端口 8883 + - ``mqtts://mqtt.eclipseprojects.io:8884``:基于 SSL 的 MQTT,端口 8884 + +- 基于 WebSocket 的 MQTT 示例: + + - ``ws://mqtt.eclipseprojects.io:80/mqtt`` + +- 基于 WebSocket Secure 的 MQTT 示例: + + - ``wss://mqtt.eclipseprojects.io:443/mqtt`` + +- 最简配置: + +.. code:: c + + const esp_mqtt_client_config_t mqtt_cfg = { + .broker.address.uri = "mqtt://mqtt.eclipseprojects.io", + }; + esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg); + esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, client); + esp_mqtt_client_start(client); + +.. note:: 默认情况下,MQTT 客户端使用事件循环库来发布相关 MQTT 事件(已连接、已订阅、已发布等)。 + +============= +验证 +============= + +为验证服务器身份,对于使用 TLS 的安全链接,必须设置 ``broker.verification`` 结构体。 +服务器证书可设置为 PEM 或 DER 格式。如要选择 DER 格式,必须设置等效 ``_len`` 字段,否则应在 ``certificate`` 字段传入以空字符结尾的 PEM 格式字符串。 + +- 从服务器获取证书,例如:``mqtt.eclipseprojects.io`` + ``openssl s_client -showcerts -connect mqtt.eclipseprojects.io:8883 /dev/null|openssl x509 -outform PEM >mqtt_eclipse_org.pem`` +- 检查示例应用程序:``examples/mqtt_ssl`` +- 配置: + +.. code:: c + + const esp_mqtt_client_config_t mqtt_cfg = { + .broker = { + .address.uri = "mqtts://mqtt.eclipseprojects.io:8883", + .verification.certificate = (const char *)mqtt_eclipse_org_pem_start, + }, + }; + +了解其他字段的详细信息,请查看 `API 参考`_ 以及 :ref:`esp_tls_server_verification`。 + +客户端凭据 +^^^^^^^^^^^^^^^^^^^^^^^^ + +``credentials`` 字段下包含所有客户端相关凭据。 + + * ``username``:指向用于连接服务器用户名的指针,也可通过 URI 设置 + * ``client_id``:指向客户端 ID 的指针,默认为 ``ESP32_%CHIPID%``,其中 ``%CHIPID%`` 是十六进制 MAC 地址的最后 3 个字节 + +=============== +认证 +=============== + +可以通过 ``authentication`` 字段设置认证参数。客户端支持以下认证方式: + + * ``authentication.password``:使用密码 + * ``authentication.certificate`` 和 ``authentication.key``:进行双向 TLS 身份验证,PEM 或 DER 格式均可 + * ``authentication.use_secure_element``:使用 ESP32-WROOM-32SE 中的安全元素 + * ``authentication.ds_data``:使用某些乐鑫设备的数字签名外设 + +会话 +^^^^^^^^^^^^ + +使用 ``section`` 字段进行 MQTT 会话相关配置。 + +======================== +遗嘱消息 (LWT) +======================== + +通过设置 ``esp_mqtt_client_config_t.session.last_will`` 结构体的以下字段,MQTT 会在一个客户端意外断开连接时通过遗嘱消息通知其他客户端。 + + * ``topic``:指向 LWT 消息主题的指针 + * ``msg``:指向 LWT 消息的指针 + * ``msg_len``:LWT 消息的长度,``msg`` 不以空字符结尾时需要该字段 + * ``qos``:LWT 消息的服务质量 + * ``retain``:指定 LWT 消息的保留标志 + +在项目配置菜单中设置 MQTT +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +通过 ``idf.py menuconfig``,可以在 ``Component config`` > ``ESP-MQTT Configuration`` 中找到 MQTT 设置。 + +相关设置如下: + +- :ref:`CONFIG_MQTT_PROTOCOL_311`:启用 MQTT 协议 3.1.1 版本 + +- :ref:`CONFIG_MQTT_TRANSPORT_SSL` 和 :ref:`CONFIG_MQTT_TRANSPORT_WEBSOCKET`:启用特定 MQTT 传输层,例如 SSL、WEBSOCKET 和 WEBSOCKET_SECURE + +- :ref:`CONFIG_MQTT_CUSTOM_OUTBOX`:禁用 mqtt_outbox 默认实现,因此可以提供特定实现 + + +事件 +------------ +MQTT 客户端可能会发布以下事件: + +* ``MQTT_EVENT_BEFORE_CONNECT``:客户端已初始化并即将开始连接至服务器。 +* ``MQTT_EVENT_CONNECTED``:客户端已成功连接至服务器。客户端已准备好收发数据。 +* ``MQTT_EVENT_DISCONNECTED``:由于无法读取或写入数据,例如因为服务器无法使用,客户端已终止连接。 +* ``MQTT_EVENT_SUBSCRIBED``:服务器已确认客户端的订阅请求。事件数据将包含订阅消息的消息 ID。 +* ``MQTT_EVENT_UNSUBSCRIBED``:服务器已确认客户端的退订请求。事件数据将包含退订消息的消息 ID。 +* ``MQTT_EVENT_PUBLISHED``:服务器已确认客户端的发布消息。消息将仅针对 QoS 级别 1 和 2 发布,因为级别 0 不会进行确认。事件数据将包含发布消息的消息 ID。 +* ``MQTT_EVENT_DATA``:客户端已收到发布消息。事件数据包含:消息 ID、发布消息所属主题名称、收到的数据及其长度。对于超出内部缓冲区的数据,将发布多个 ``MQTT_EVENT_DATA``,并更新事件数据的 ``current_data_offset`` 和 ``total_data_len`` 以跟踪碎片化消息。 +* ``MQTT_EVENT_ERROR``:客户端遇到错误。使用事件数据 ``error_handle`` 中的 ``esp_mqtt_error_type_t``,可以进一步判断错误类型。错误类型决定 ``error_handle`` 结构体的哪些部分会被填充。 + +API 参考 +------------- + +.. include-build-file:: inc/mqtt_client.inc From cc7d9b4b7c5247b60812308b05d6028fb4607736 Mon Sep 17 00:00:00 2001 From: Martin Vychodil Date: Fri, 14 Oct 2022 14:15:32 +0200 Subject: [PATCH 152/231] Storage: Partition APIs moved to the new component 'esp_partition' All the partition handling API functions and data-types were moved from the 'spi_flash' component to the new one named 'esp_partition'. See Storage 5.x migration guide for more details --- components/mqtt/test/CMakeLists.txt | 2 +- components/mqtt/test/test_mqtt.c | 1 + components/mqtt/test/test_mqtt5.c | 1 + examples/protocols/mqtt/ssl/main/app_main.c | 2 ++ examples/protocols/mqtt/ssl_ds/main/idf_component.yml | 2 +- 5 files changed, 6 insertions(+), 2 deletions(-) diff --git a/components/mqtt/test/CMakeLists.txt b/components/mqtt/test/CMakeLists.txt index 616d362..3261bec 100644 --- a/components/mqtt/test/CMakeLists.txt +++ b/components/mqtt/test/CMakeLists.txt @@ -5,5 +5,5 @@ if(CONFIG_MQTT_PROTOCOL_5) endif() idf_component_register(SRCS "${srcs}" - PRIV_REQUIRES cmock test_utils mqtt nvs_flash app_update esp_eth esp_netif) + PRIV_REQUIRES cmock test_utils mqtt nvs_flash app_update esp_eth esp_netif spi_flash) target_compile_options(${COMPONENT_LIB} PRIVATE "-Wno-format") diff --git a/components/mqtt/test/test_mqtt.c b/components/mqtt/test/test_mqtt.c index d997bce..3bda6c0 100644 --- a/components/mqtt/test/test_mqtt.c +++ b/components/mqtt/test/test_mqtt.c @@ -23,6 +23,7 @@ #include "test_mqtt_client_broker.h" #include "test_mqtt_connection.h" #include "esp_mac.h" +#include "spi_flash_mmap.h" static void test_leak_setup(const char * file, long line) { diff --git a/components/mqtt/test/test_mqtt5.c b/components/mqtt/test/test_mqtt5.c index 01c33e7..502cd1c 100644 --- a/components/mqtt/test/test_mqtt5.c +++ b/components/mqtt/test/test_mqtt5.c @@ -17,6 +17,7 @@ #include "test_mqtt5_client_broker.h" #include "test_mqtt_connection.h" #include "esp_mac.h" +#include "spi_flash_mmap.h" static esp_mqtt5_user_property_item_t user_property_arr[3] = { {"board", "esp32"}, diff --git a/examples/protocols/mqtt/ssl/main/app_main.c b/examples/protocols/mqtt/ssl/main/app_main.c index 3e6e863..a978111 100644 --- a/examples/protocols/mqtt/ssl/main/app_main.c +++ b/examples/protocols/mqtt/ssl/main/app_main.c @@ -12,6 +12,8 @@ #include #include #include "esp_system.h" +#include "esp_partition.h" +#include "spi_flash_mmap.h" #include "nvs_flash.h" #include "esp_event.h" #include "esp_netif.h" diff --git a/examples/protocols/mqtt/ssl_ds/main/idf_component.yml b/examples/protocols/mqtt/ssl_ds/main/idf_component.yml index 80c7c82..8dd90a3 100644 --- a/examples/protocols/mqtt/ssl_ds/main/idf_component.yml +++ b/examples/protocols/mqtt/ssl_ds/main/idf_component.yml @@ -1,3 +1,3 @@ ## IDF Component Manager Manifest File dependencies: - espressif/esp_secure_cert_mgr: "^2.0.0" + espressif/esp_secure_cert_mgr: "^2.0.2" From f643b951183eae292d364ecfd96622333aa849d8 Mon Sep 17 00:00:00 2001 From: Armando Date: Mon, 7 Nov 2022 19:31:29 +0800 Subject: [PATCH 153/231] partition: use esp_partition_munmap instead of spi_flash_munmap --- components/mqtt/test/test_mqtt.c | 6 +++--- components/mqtt/test/test_mqtt5.c | 6 +++--- examples/protocols/mqtt/ssl/main/app_main.c | 5 ++--- 3 files changed, 8 insertions(+), 9 deletions(-) diff --git a/components/mqtt/test/test_mqtt.c b/components/mqtt/test/test_mqtt.c index 3bda6c0..41a5516 100644 --- a/components/mqtt/test/test_mqtt.c +++ b/components/mqtt/test/test_mqtt.c @@ -23,7 +23,7 @@ #include "test_mqtt_client_broker.h" #include "test_mqtt_connection.h" #include "esp_mac.h" -#include "spi_flash_mmap.h" +#include "esp_partition.h" static void test_leak_setup(const char * file, long line) { @@ -59,10 +59,10 @@ TEST_CASE("mqtt init and deinit", "[mqtt][leaks=0]") static const char* this_bin_addr(void) { - spi_flash_mmap_handle_t out_handle; + esp_partition_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); + esp_partition_mmap(partition, 0, partition->size, ESP_PARTITION_MMAP_DATA, &binary_address, &out_handle); return binary_address; } diff --git a/components/mqtt/test/test_mqtt5.c b/components/mqtt/test/test_mqtt5.c index 502cd1c..1ea75e1 100644 --- a/components/mqtt/test/test_mqtt5.c +++ b/components/mqtt/test/test_mqtt5.c @@ -17,7 +17,7 @@ #include "test_mqtt5_client_broker.h" #include "test_mqtt_connection.h" #include "esp_mac.h" -#include "spi_flash_mmap.h" +#include "esp_partition.h" static esp_mqtt5_user_property_item_t user_property_arr[3] = { {"board", "esp32"}, @@ -89,10 +89,10 @@ TEST_CASE("mqtt5 init and deinit", "[mqtt5][leaks=0]") static const char* this_bin_addr(void) { - spi_flash_mmap_handle_t out_handle; + esp_partition_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); + esp_partition_mmap(partition, 0, partition->size, ESP_PARTITION_MMAP_DATA, &binary_address, &out_handle); return binary_address; } diff --git a/examples/protocols/mqtt/ssl/main/app_main.c b/examples/protocols/mqtt/ssl/main/app_main.c index a978111..acfcb3f 100644 --- a/examples/protocols/mqtt/ssl/main/app_main.c +++ b/examples/protocols/mqtt/ssl/main/app_main.c @@ -13,7 +13,6 @@ #include #include "esp_system.h" #include "esp_partition.h" -#include "spi_flash_mmap.h" #include "nvs_flash.h" #include "esp_event.h" #include "esp_netif.h" @@ -41,10 +40,10 @@ extern const uint8_t mqtt_eclipseprojects_io_pem_end[] asm("_binary_mqtt_eclip // static void send_binary(esp_mqtt_client_handle_t client) { - spi_flash_mmap_handle_t out_handle; + esp_partition_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); + esp_partition_mmap(partition, 0, partition->size, ESP_PARTITION_MMAP_DATA, &binary_address, &out_handle); // sending only the configured portion of the partition (if it's less than the partition size) int binary_size = MIN(CONFIG_BROKER_BIN_SIZE_TO_SEND, partition->size); int msg_id = esp_mqtt_client_publish(client, "/topic/binary", binary_address, binary_size, 0, 0); From c54d7cfe434a939cf40ff6c4c7efbdd1948fb7cd Mon Sep 17 00:00:00 2001 From: Euripedes Rocha Date: Tue, 11 Oct 2022 15:40:55 +0200 Subject: [PATCH 154/231] [MQTT] clarification of message retransmission - Adds description of the message retransmission process for QoS 1 and 2. --- docs/en/api-reference/protocols/mqtt.rst | 79 ++++++++++++++---------- 1 file changed, 48 insertions(+), 31 deletions(-) diff --git a/docs/en/api-reference/protocols/mqtt.rst b/docs/en/api-reference/protocols/mqtt.rst index 4109c37..3138550 100644 --- a/docs/en/api-reference/protocols/mqtt.rst +++ b/docs/en/api-reference/protocols/mqtt.rst @@ -26,18 +26,31 @@ Application Examples * :example:`protocols/mqtt/ws`: MQTT over WebSocket, default port 80 * :example:`protocols/mqtt/wss`: MQTT over WebSocket Secure, default port 443 +MQTT message retransmission +--------------------------- + +A new mqtt message is created by calling :cpp:func:`esp_mqtt_client_publish ` or its non blocking +counterpart :cpp:func:`esp_mqtt_client_enqueue `. + +Messages with QoS 0 will be sent only once, QoS 1 and 2 have a different behavior since the protocol requires extra steps to complete the process. + +The ESP-MQTT library opts to always retransmit unacknowledged QoS 1 and 2 PUBLISH messages to avoid losses in faulty connections, even though the MQTT specification +requires the re-transmission only on reconnect with Clean Session flag been set to 0 (set :cpp:member:`disable_clean_session ` to true for this behavior). + + +Messages that could need retransmission, QoS 1 and 2, are always enqueued, but first transmission try occurs immediately if :cpp:func:`esp_mqtt_client_publish ` is used. A transmission retry for unacknowledged messages will occur after :cpp:member:`message_retransmit_timeout `. After :ref:`CONFIG_MQTT_OUTBOX_EXPIRED_TIMEOUT_MS` messages will expire and deleted. If :ref:`CONFIG_MQTT_REPORT_DELETED_MESSAGES` is set an event is sent to notify the user. Configuration ------------- -The configuration is made by setting fields in ``esp_mqtt_client_config_t`` struct. The configuration struct has the following sub structs to configure different aspects of the client operation. +The configuration is made by setting fields in :cpp:class:`esp_mqtt_client_config_t` struct. The configuration struct has the following sub structs to configure different aspects of the client operation. - * :cpp:member:`broker` - Allow to set address and security verification. - * :cpp:member:`credentials` - Client credentials for authentication. - * :cpp:member:`session` - Configuration for MQTT session aspects. - * :cpp:member:`network` - Networking related configuration. - * :cpp:member:`task` - Allow to configure FreeRTOS task. - * :cpp:member:`buffer` - Buffer size for input and output. + * :cpp:class:`esp_mqtt_client_config_t::broker_t` - Allow to set address and security verification. + * :cpp:class:`esp_mqtt_client_config_t::credentials_t` - Client credentials for authentication. + * :cpp:class:`esp_mqtt_client_config_t::session_t` - Configuration for MQTT session aspects. + * :cpp:class:`esp_mqtt_client_config_t::network_t` - Networking related configuration. + * :cpp:class:`esp_mqtt_client_config_t::task_t` - Allow to configure FreeRTOS task. + * :cpp:class:`esp_mqtt_client_config_t::buffer_t` - Buffer size for input and output. In the following sections, the most common aspects are detailed. @@ -48,9 +61,9 @@ Broker Address =========== -Broker address can be set by usage of ``broker.address`` struct. The configuration can be made by usage of ``uri`` field or the combination of ``hostname``, ``transport`` and ``port``. Optionally, ``path`` could be set, this field is useful in WebSocket connections. +Broker address can be set by usage of :cpp:class:`address ` struct. The configuration can be made by usage of :cpp:member:`uri ` field or the combination of :cpp:member:`hostname `, :cpp:member:`transport ` and :cpp:member:`port `. Optionally, :cpp:member:`path ` could be set, this field is useful in WebSocket connections. -The ``uri`` field is used in the format ``scheme://hostname:port/path``. +The :cpp:member:`uri ` field is used in the format ``scheme://hostname:port/path``. - Curently support ``mqtt``, ``mqtts``, ``ws``, ``wss`` schemes - MQTT over TCP samples: @@ -90,12 +103,16 @@ The ``uri`` field is used in the format ``scheme://hostname:port/path``. Verification ============ -For secure connections with TLS used, and to guarantee Broker's identity, the ``broker.verification`` struct must be set. -The broker certificate may be set in PEM or DER format. To select DER, the equivalent ``_len`` field must be set. Otherwise, a null-terminated string in PEM format should be provided to ``certificate`` field. +For secure connections with TLS used, and to guarantee Broker's identity, the :cpp:class:`verification ` struct must be set. +The broker certificate may be set in PEM or DER format. To select DER, the equivalent :cpp:member:`certificate_len ` field must be set. Otherwise, a null-terminated string in PEM format should be provided to :cpp:member:`certificate ` field. - Get certificate from server, example: ``mqtt.eclipseprojects.io`` - ``openssl s_client -showcerts -connect mqtt.eclipseprojects.io:8883 /dev/null|openssl x509 -outform PEM >mqtt_eclipse_org.pem`` -- Check the sample application: ``examples/mqtt_ssl`` + .. code:: + + openssl s_client -showcerts -connect mqtt.eclipseprojects.io:8883 < /dev/null \ + 2> /dev/null | openssl x509 -outform PEM > mqtt_eclipse_org.pem + +- Check the sample application: :example:`protocols/mqtt/ssl` - Configuration: .. code:: c @@ -112,43 +129,43 @@ For details about other fields, please check the `API Reference`_ and :ref:`esp_ Client Credentials ^^^^^^^^^^^^^^^^^^ -All client related credentials are under the ``credentials`` field. +All client related credentials are under the :cpp:class:`credentials ` field. - * ``username``: pointer to the username used for connecting to the broker, can also be set by URI - * ``client_id``: pointer to the client ID, defaults to ``ESP32_%CHIPID%`` where ``%CHIPID%`` are the last 3 bytes of MAC address in hex format + * :cpp:member:`username ` pointer to the username used for connecting to the broker, can also be set by URI + * :cpp:member:`client_id `: pointer to the client ID, defaults to ``ESP32_%CHIPID%`` where ``%CHIPID%`` are the last 3 bytes of MAC address in hex format ============== Authentication ============== -It's possible to set authentication parameters through the ``authentication`` field. The client supports the following authentication methods: +It's possible to set authentication parameters through the :cpp:class:`authentication ` field. The client supports the following authentication methods: - * ``authentication.password``: use a password by setting - * ``authentication.certificate`` and ``authentication.key``: mutual authentication with TLS, and both can be provided in PEM or DER format - * ``authentication.use_secure_element``: use secure element available in ESP32-WROOM-32SE - * ``authentication.ds_data``: use Digital Signature Peripheral available in some Espressif devices + * :cpp:member:`password `: use a password by setting + * :cpp:member:`certificate ` and :cpp:member:`key `: mutual authentication with TLS, and both can be provided in PEM or DER format + * :cpp:member:`use_secure_element `: use secure element available in ESP32-WROOM-32SE + * :cpp:member:`ds_data `: use Digital Signature Peripheral available in some Espressif devices Session ^^^^^^^^^^^ -For MQTT session related configurations, ``section`` fields should be used. +For MQTT session related configurations, :cpp:class:`session ` fields should be used. ======================= Last Will and Testament ======================= -MQTT allows for a last will and testament (LWT) message to notify other clients when a client ungracefully disconnects. This is configured by the following fields in the ``esp_mqtt_client_config_t.session.last_will`` struct. +MQTT allows for a last will and testament (LWT) message to notify other clients when a client ungracefully disconnects. This is configured by the following fields in the :cpp:class:`last_will ` struct. - * ``topic``: pointer to the LWT message topic - * ``msg``: pointer to the LWT message - * ``msg_len``: length of the LWT message, required if ``msg`` is not null-terminated - * ``qos``: quality of service for the LWT message - * ``retain``: specifies the retain flag of the LWT message + * :cpp:member:`topic `: pointer to the LWT message topic + * :cpp:member:`msg `: pointer to the LWT message + * :cpp:member:`msg_len `: length of the LWT message, required if :cpp:member:`msg ` is not null-terminated + * :cpp:member:`qos `: quality of service for the LWT message + * :cpp:member:`retain `: specifies the retain flag of the LWT message Change Settings in Project Configuration Menu ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -The settings for MQTT can be found using ``idf.py menuconfig``, under ``Component config`` > ``ESP-MQTT Configuration``. +The settings for MQTT can be found using :code:`idf.py menuconfig`, under ``Component config`` > ``ESP-MQTT Configuration``. The following settings are available: @@ -169,8 +186,8 @@ The following events may be posted by the MQTT client: * ``MQTT_EVENT_SUBSCRIBED``: The broker has acknowledged the client's subscribe request. The event data will contain the message ID of the subscribe message. * ``MQTT_EVENT_UNSUBSCRIBED``: The broker has acknowledged the client's unsubscribe request. The event data will contain the message ID of the unsubscribe message. * ``MQTT_EVENT_PUBLISHED``: The broker has acknowledged the client's publish message. This will only be posted for QoS level 1 and 2, as level 0 does not use acknowledgements. The event data will contain the message ID of the publish message. -* ``MQTT_EVENT_DATA``: The client has received a publish message. The event data contains: message ID, name of the topic it was published to, received data and its length. For data that exceeds the internal buffer, multiple ``MQTT_EVENT_DATA`` will be posted and ``current_data_offset`` and ``total_data_len`` from event data updated to keep track of the fragmented message. -* ``MQTT_EVENT_ERROR``: The client has encountered an error. ``esp_mqtt_error_type_t`` from ``error_handle`` in the event data can be used to further determine the type of the error. The type of error will determine which parts of the ``error_handle`` struct is filled. +* ``MQTT_EVENT_DATA``: The client has received a publish message. The event data contains: message ID, name of the topic it was published to, received data and its length. For data that exceeds the internal buffer, multiple ``MQTT_EVENT_DATA`` will be posted and :cpp:member:`current_data_offset ` and :cpp:member:`total_data_len` from event data updated to keep track of the fragmented message. +* ``MQTT_EVENT_ERROR``: The client has encountered an error. The field :cpp:type:`error_handle ` in the event data contains :cpp:type:`error_type ` that can be used to identify the error. The type of error will determine which parts of the :cpp:type:`error_handle ` struct is filled. API Reference ------------- From f43591d970b298bb68e1ec4ee0261f9d0c3174ec Mon Sep 17 00:00:00 2001 From: Song Ruo Jing Date: Wed, 9 Nov 2022 18:23:49 +0800 Subject: [PATCH 155/231] ci: Disable some unit-test-apps for esp32c6 to pass ci build stage --- components/mqtt/test/test_mqtt5.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/components/mqtt/test/test_mqtt5.c b/components/mqtt/test/test_mqtt5.c index 1ea75e1..6b05275 100644 --- a/components/mqtt/test/test_mqtt5.c +++ b/components/mqtt/test/test_mqtt5.c @@ -19,6 +19,7 @@ #include "esp_mac.h" #include "esp_partition.h" +#if !TEMPORARY_DISABLED_FOR_TARGETS(ESP32C6) static esp_mqtt5_user_property_item_t user_property_arr[3] = { {"board", "esp32"}, {"u", "user"}, @@ -150,3 +151,4 @@ TEST_CASE("mqtt5 broker tests", "[mqtt5][test_env=UT_T2_Ethernet]") connect_test_fixture_teardown(); } #endif // SOC_EMAC_SUPPORTED +#endif //!TEMPORARY_DISABLED_FOR_TARGETS(ESP32C6) From bfd413589abc8ce10f348b4f352b88dee96f3ff2 Mon Sep 17 00:00:00 2001 From: Euripedes Rocha Date: Wed, 2 Nov 2022 16:25:43 +0100 Subject: [PATCH 156/231] mqtt: Set state on stoping; Add error code to Subscribed event * Update submodule: git log --oneline ae53d799da294f03ef65c33e88fa33648e638134..fde00340f19b9f5ae81fff02ccfa9926f0e33687 Detailed description of the changes: * Fix the default configuration for event queue - See merge request espressif/esp-mqtt!153 - See commit https://github.com/espressif/esp-mqtt/commit/fb42588 * Adds missing header. - See merge request espressif/esp-mqtt!152 - See commit https://github.com/espressif/esp-mqtt/commit/8a60057 * Moves state change when stopping the client - See merge request espressif/esp-mqtt!150 - Closes https://github.com/espressif/esp-mqtt/issues/239 - See commit https://github.com/espressif/esp-mqtt/commit/3738fcd * Adds error code to MQTT_EVENT_SUBSCRIBED in case of failure - See merge request espressif/esp-mqtt!143 - - Closes https://github.com/espressif/esp-mqtt/issues/233 - See commit https://github.com/espressif/esp-mqtt/commit/9af5c26 * Adds debug information on sending dup messages - See merge request espressif/esp-mqtt!145 - See commit https://github.com/espressif/esp-mqtt/commit/47b3f9b * ci: Fix qemu build - See merge request espressif/esp-mqtt!147 - See commit https://github.com/espressif/esp-mqtt/commit/68e8c4f * ci: Build and Test QEMU on v5.0 - See merge request espressif/esp-mqtt!142 - See commit https://github.com/espressif/esp-mqtt/commit/9db9ee7 * client: Add support for user events - See merge request espressif/esp-mqtt!140 - Closes https://github.com/espressif/esp-mqtt/issues/230 - See commit https://github.com/espressif/esp-mqtt/commit/97503cc * Adds unregister event API - See merge request espressif/esp-mqtt!139 - Closes https://github.com/espressif/esp-idf/issues/9194 - See commit https://github.com/espressif/esp-mqtt/commit/a9a9fe7 --- components/mqtt/Kconfig | 7 +++++++ components/mqtt/host_test/CMakeLists.txt | 1 + .../mqtt/host_test/mocks/heap/CMakeLists.txt | 4 ++++ components/mqtt/host_test/mocks/heap/heap_mock.c | 16 ++++++++++++++++ 4 files changed, 28 insertions(+) create mode 100644 components/mqtt/host_test/mocks/heap/CMakeLists.txt create mode 100644 components/mqtt/host_test/mocks/heap/heap_mock.c diff --git a/components/mqtt/Kconfig b/components/mqtt/Kconfig index 4db6e15..113bac7 100644 --- a/components/mqtt/Kconfig +++ b/components/mqtt/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/host_test/CMakeLists.txt b/components/mqtt/host_test/CMakeLists.txt index d1c1ac6..db98733 100644 --- a/components/mqtt/host_test/CMakeLists.txt +++ b/components/mqtt/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/mocks/heap/CMakeLists.txt b/components/mqtt/host_test/mocks/heap/CMakeLists.txt new file mode 100644 index 0000000..0a0c8a6 --- /dev/null +++ b/components/mqtt/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/components/mqtt/host_test/mocks/heap/heap_mock.c b/components/mqtt/host_test/mocks/heap/heap_mock.c new file mode 100644 index 0000000..e0fc666 --- /dev/null +++ b/components/mqtt/host_test/mocks/heap/heap_mock.c @@ -0,0 +1,16 @@ +/* + * SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ +#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); + +} From 1efdb52330b2b4e87fe12347b83063a50e0c8993 Mon Sep 17 00:00:00 2001 From: Suren Gabrielyan Date: Mon, 7 Nov 2022 10:46:46 +0400 Subject: [PATCH 157/231] Examples: migration mqtt examples to pytest --- ...ssl_example_test.py => pytest_mqtt_ssl.py} | 44 +++--- ...tcp_example_test.py => pytest_mqtt_tcp.py} | 46 +++---- ...mple_test.py => pytest_mqtt_ws_example.py} | 41 +++--- ...ple_test.py => pytest_mqtt_wss_example.py} | 42 +++--- .../{app_test.py => pytest_mqtt_app.py} | 130 ++++++++++-------- 5 files changed, 160 insertions(+), 143 deletions(-) rename examples/protocols/mqtt/ssl/{mqtt_ssl_example_test.py => pytest_mqtt_ssl.py} (79%) rename examples/protocols/mqtt/tcp/{mqtt_tcp_example_test.py => pytest_mqtt_tcp.py} (65%) rename examples/protocols/mqtt/ws/{mqtt_ws_example_test.py => pytest_mqtt_ws_example.py} (72%) rename examples/protocols/mqtt/wss/{mqtt_wss_example_test.py => pytest_mqtt_wss_example.py} (73%) rename tools/test_apps/protocols/mqtt/publish_connect_test/{app_test.py => pytest_mqtt_app.py} (78%) diff --git a/examples/protocols/mqtt/ssl/mqtt_ssl_example_test.py b/examples/protocols/mqtt/ssl/pytest_mqtt_ssl.py similarity index 79% rename from examples/protocols/mqtt/ssl/mqtt_ssl_example_test.py rename to examples/protocols/mqtt/ssl/pytest_mqtt_ssl.py index 51ac4e9..c16b89c 100644 --- a/examples/protocols/mqtt/ssl/mqtt_ssl_example_test.py +++ b/examples/protocols/mqtt/ssl/pytest_mqtt_ssl.py @@ -1,3 +1,6 @@ +# SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD +# SPDX-License-Identifier: Unlicense OR CC0-1.0 +import logging import os import re import ssl @@ -5,8 +8,9 @@ import sys from threading import Event, Thread import paho.mqtt.client as mqtt -import ttfw_idf -from tiny_test_fw import DUT +import pexpect +import pytest +from pytest_embedded import Dut event_client_connected = Event() event_stop_client = Event() @@ -16,19 +20,20 @@ message_log = '' # The callback for when the client receives a CONNACK response from the server. -def on_connect(client, userdata, flags, rc): +def on_connect(client, userdata, flags, rc): # type: (mqtt.Client, str, bool, str) -> None + _ = (userdata, flags) print('Connected with result code ' + str(rc)) event_client_connected.set() client.subscribe('/topic/qos0') -def mqtt_client_task(client): +def mqtt_client_task(client): # type: (mqtt.Client) -> None while not event_stop_client.is_set(): client.loop() # The callback for when a PUBLISH message is received from the server. -def on_message(client, userdata, msg): +def on_message(client, userdata, msg): # type: (mqtt.Client, tuple, mqtt.client.MQTTMessage) -> None global message_log global event_client_received_correct global event_client_received_binary @@ -55,8 +60,9 @@ def on_message(client, userdata, msg): message_log += 'Received data:' + msg.topic + ' ' + payload + '\n' -@ttfw_idf.idf_example_test(env_tag='ethernet_router') -def test_examples_protocol_mqtt_ssl(env, extra_data): +@pytest.mark.esp32 +@pytest.mark.ethernet +def test_examples_protocol_mqtt_ssl(dut): # type: (Dut) -> None broker_url = '' broker_port = 0 """ @@ -67,18 +73,17 @@ def test_examples_protocol_mqtt_ssl(env, extra_data): 4. Test ESP32 client received correct qos0 message 5. Test python client receives binary data from running partition and compares it with the binary """ - dut1 = env.get_dut('mqtt_ssl', 'examples/protocols/mqtt/ssl', dut_class=ttfw_idf.ESP32DUT) - # check and log bin size - binary_file = os.path.join(dut1.app.binary_path, 'mqtt_ssl.bin') + binary_file = os.path.join(dut.app.binary_path, 'mqtt_ssl.bin') bin_size = os.path.getsize(binary_file) - ttfw_idf.log_performance('mqtt_ssl_bin_size', '{}KB' - .format(bin_size // 1024)) + logging.info('[Performance][mqtt_ssl_bin_size]: %s KB', bin_size // 1024) + # Look for host:port in sdkconfig try: - value = re.search(r'\:\/\/([^:]+)\:([0-9]+)', dut1.app.get_sdkconfig()['CONFIG_BROKER_URI']) + value = re.search(r'\:\/\/([^:]+)\:([0-9]+)', dut.app.sdkconfig.get('BROKER_URI')) + assert value is not None broker_url = value.group(1) broker_port = int(value.group(2)) - bin_size = min(int(dut1.app.get_sdkconfig()['CONFIG_BROKER_BIN_SIZE_TO_SEND']), bin_size) + bin_size = min(int(dut.app.sdkconfig.get('BROKER_BIN_SIZE_TO_SEND')), bin_size) except Exception: print('ENV_TEST_FAILURE: Cannot find broker url in sdkconfig') raise @@ -105,25 +110,20 @@ def test_examples_protocol_mqtt_ssl(env, extra_data): print('Connecting py-client to broker {}:{}...'.format(broker_url, broker_port)) if not event_client_connected.wait(timeout=30): raise ValueError('ENV_TEST_FAILURE: Test script cannot connect to broker: {}'.format(broker_url)) - dut1.start_app() try: - ip_address = dut1.expect(re.compile(r'IPv4 address: (\d+\.\d+\.\d+\.\d+)[^\d]'), timeout=30)[0] + ip_address = dut.expect(r'IPv4 address: (\d+\.\d+\.\d+\.\d+)[^\d]', timeout=30)[0] print('Connected to AP with IP: {}'.format(ip_address)) - except DUT.ExpectTimeout: + except pexpect.TIMEOUT: print('ENV_TEST_FAILURE: Cannot connect to AP') raise print('Checking py-client received msg published from esp...') if not event_client_received_correct.wait(timeout=30): raise ValueError('Wrong data received, msg log: {}'.format(message_log)) print('Checking esp-client received msg published from py-client...') - dut1.expect(re.compile(r'DATA=send binary please'), timeout=30) + dut.expect(r'DATA=send binary please', timeout=30) print('Receiving binary data from running partition...') if not event_client_received_binary.wait(timeout=30): raise ValueError('Binary not received within timeout') finally: event_stop_client.set() thread1.join() - - -if __name__ == '__main__': - test_examples_protocol_mqtt_ssl() diff --git a/examples/protocols/mqtt/tcp/mqtt_tcp_example_test.py b/examples/protocols/mqtt/tcp/pytest_mqtt_tcp.py similarity index 65% rename from examples/protocols/mqtt/tcp/mqtt_tcp_example_test.py rename to examples/protocols/mqtt/tcp/pytest_mqtt_tcp.py index 76dc4a4..2786a76 100644 --- a/examples/protocols/mqtt/tcp/mqtt_tcp_example_test.py +++ b/examples/protocols/mqtt/tcp/pytest_mqtt_tcp.py @@ -1,19 +1,22 @@ +# SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD +# SPDX-License-Identifier: Unlicense OR CC0-1.0 +import logging import os -import re import socket import struct import sys import time from threading import Thread -import ttfw_idf +import pexpect +import pytest from common_test_methods import get_host_ip4_by_dest_ip -from tiny_test_fw import DUT +from pytest_embedded import Dut msgid = -1 -def mqqt_server_sketch(my_ip, port): +def mqqt_server_sketch(my_ip, port): # type: (str, str) -> None global msgid print('Starting the server on {}'.format(my_ip)) s = None @@ -32,13 +35,13 @@ def mqqt_server_sketch(my_ip, port): raise data = q.recv(1024) # check if received initial empty message - print('received from client {}'.format(data)) + print('received from client {!r}'.format(data)) data = bytearray([0x20, 0x02, 0x00, 0x00]) q.send(data) # try to receive qos1 data = q.recv(1024) msgid = struct.unpack('>H', data[15:17])[0] - print('received from client {}, msgid: {}'.format(data, msgid)) + print('received from client {!r}, msgid: {}'.format(data, msgid)) data = bytearray([0x40, 0x02, data[15], data[16]]) q.send(data) time.sleep(5) @@ -46,8 +49,9 @@ def mqqt_server_sketch(my_ip, port): print('server closed') -@ttfw_idf.idf_example_test(env_tag='ethernet_router') -def test_examples_protocol_mqtt_qos1(env, extra_data): +@pytest.mark.esp32 +@pytest.mark.ethernet +def test_examples_protocol_mqtt_qos1(dut: Dut) -> None: global msgid """ steps: (QoS1: Happy flow) @@ -56,18 +60,15 @@ def test_examples_protocol_mqtt_qos1(env, extra_data): 3. Test evaluates that qos1 message is queued and removed from queued after ACK received 4. Test the broker received the same message id evaluated in step 3 """ - dut1 = env.get_dut('mqtt_tcp', 'examples/protocols/mqtt/tcp', dut_class=ttfw_idf.ESP32DUT) # check and log bin size - binary_file = os.path.join(dut1.app.binary_path, 'mqtt_tcp.bin') + binary_file = os.path.join(dut.app.binary_path, 'mqtt_tcp.bin') bin_size = os.path.getsize(binary_file) - ttfw_idf.log_performance('mqtt_tcp_bin_size', '{}KB'.format(bin_size // 1024)) - # 1. start the dut test and wait till client gets IP address - dut1.start_app() + logging.info('[Performance][mqtt_tcp_bin_size]: %s KB', bin_size // 1024) # waiting for getting the IP address try: - ip_address = dut1.expect(re.compile(r'IPv4 address: (\d+\.\d+\.\d+\.\d+)[^\d]'), timeout=30)[0] + ip_address = dut.expect(r'IPv4 address: (\d+\.\d+\.\d+\.\d+)', timeout=30).group(1).decode() print('Connected to AP/Ethernet with IP: {}'.format(ip_address)) - except DUT.ExpectTimeout: + except pexpect.TIMEOUT: raise ValueError('ENV_TEST_FAILURE: Cannot connect to AP/Ethernet') # 2. start mqtt broker sketch @@ -75,20 +76,17 @@ def test_examples_protocol_mqtt_qos1(env, extra_data): thread1 = Thread(target=mqqt_server_sketch, args=(host_ip,1883)) thread1.start() - print('writing to device: {}'.format('mqtt://' + host_ip + '\n')) - dut1.write('mqtt://' + host_ip + '\n') + data_write = 'mqtt://' + host_ip + print('writing to device: {}'.format(data_write)) + dut.write(data_write) thread1.join() print('Message id received from server: {}'.format(msgid)) # 3. check the message id was enqueued and then deleted - msgid_enqueued = dut1.expect(re.compile(r'outbox: ENQUEUE msgid=([0-9]+)'), timeout=30) - msgid_deleted = dut1.expect(re.compile(r'outbox: DELETED msgid=([0-9]+)'), timeout=30) + msgid_enqueued = dut.expect(b'outbox: ENQUEUE msgid=([0-9]+)', timeout=30).group(1).decode() + msgid_deleted = dut.expect(b'outbox: DELETED msgid=([0-9]+)', timeout=30).group(1).decode() # 4. check the msgid of received data are the same as that of enqueued and deleted from outbox - if (msgid_enqueued[0] == str(msgid) and msgid_deleted[0] == str(msgid)): + if (msgid_enqueued == str(msgid) and msgid_deleted == str(msgid)): print('PASS: Received correct msg id') else: print('Failure!') raise ValueError('Mismatch of msgid: received: {}, enqueued {}, deleted {}'.format(msgid, msgid_enqueued, msgid_deleted)) - - -if __name__ == '__main__': - test_examples_protocol_mqtt_qos1() diff --git a/examples/protocols/mqtt/ws/mqtt_ws_example_test.py b/examples/protocols/mqtt/ws/pytest_mqtt_ws_example.py similarity index 72% rename from examples/protocols/mqtt/ws/mqtt_ws_example_test.py rename to examples/protocols/mqtt/ws/pytest_mqtt_ws_example.py index c24cfec..392139c 100644 --- a/examples/protocols/mqtt/ws/mqtt_ws_example_test.py +++ b/examples/protocols/mqtt/ws/pytest_mqtt_ws_example.py @@ -1,11 +1,16 @@ +#!/usr/bin/env python +# +# SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD +# SPDX-License-Identifier: Unlicense OR CC0-1.0 +import logging import os import re import sys from threading import Event, Thread import paho.mqtt.client as mqtt -import ttfw_idf -from tiny_test_fw import DUT +import pytest +from pytest_embedded import Dut event_client_connected = Event() event_stop_client = Event() @@ -14,19 +19,21 @@ message_log = '' # The callback for when the client receives a CONNACK response from the server. -def on_connect(client, userdata, flags, rc): +def on_connect(client, userdata, flags, rc): # type: (mqtt.Client, tuple, bool, str) -> None + _ = (userdata, flags) print('Connected with result code ' + str(rc)) event_client_connected.set() client.subscribe('/topic/qos0') -def mqtt_client_task(client): +def mqtt_client_task(client): # type: (mqtt.Client) -> None while not event_stop_client.is_set(): client.loop() # The callback for when a PUBLISH message is received from the server. -def on_message(client, userdata, msg): +def on_message(client, userdata, msg): # type: (mqtt.Client, tuple, mqtt.client.MQTTMessage) -> None + _ = userdata global message_log payload = msg.payload.decode() if not event_client_received_correct.is_set() and payload == 'data': @@ -36,8 +43,9 @@ def on_message(client, userdata, msg): message_log += 'Received data:' + msg.topic + ' ' + payload + '\n' -@ttfw_idf.idf_example_test(env_tag='ethernet_router') -def test_examples_protocol_mqtt_ws(env, extra_data): +@pytest.mark.esp32 +@pytest.mark.ethernet +def test_examples_protocol_mqtt_ws(dut): # type: (Dut) -> None broker_url = '' broker_port = 0 """ @@ -47,14 +55,14 @@ def test_examples_protocol_mqtt_ws(env, extra_data): 3. Test evaluates it received correct qos0 message 4. Test ESP32 client received correct qos0 message """ - dut1 = env.get_dut('mqtt_websocket', 'examples/protocols/mqtt/ws', dut_class=ttfw_idf.ESP32DUT) # check and log bin size - binary_file = os.path.join(dut1.app.binary_path, 'mqtt_websocket.bin') + binary_file = os.path.join(dut.app.binary_path, 'mqtt_websocket.bin') bin_size = os.path.getsize(binary_file) - ttfw_idf.log_performance('mqtt_websocket_bin_size', '{}KB'.format(bin_size // 1024)) + logging.info('[Performance][mqtt_websocket_bin_size]: %s KB', bin_size // 1024) # Look for host:port in sdkconfig try: - value = re.search(r'\:\/\/([^:]+)\:([0-9]+)', dut1.app.get_sdkconfig()['CONFIG_BROKER_URI']) + value = re.search(r'\:\/\/([^:]+)\:([0-9]+)', dut.app.sdkconfig.get('BROKER_URI')) + assert value is not None broker_url = value.group(1) broker_port = int(value.group(2)) except Exception: @@ -78,22 +86,17 @@ def test_examples_protocol_mqtt_ws(env, extra_data): print('Connecting py-client to broker {}:{}...'.format(broker_url, broker_port)) if not event_client_connected.wait(timeout=30): raise ValueError('ENV_TEST_FAILURE: Test script cannot connect to broker: {}'.format(broker_url)) - dut1.start_app() try: - ip_address = dut1.expect(re.compile(r'IPv4 address: (\d+\.\d+\.\d+\.\d+)[^\d]'), timeout=30)[0] + ip_address = dut.expect(r'IPv4 address: (\d+\.\d+\.\d+\.\d+)[^\d]', timeout=30)[0] print('Connected to AP with IP: {}'.format(ip_address)) - except DUT.ExpectTimeout: + except Dut.ExpectTimeout: print('ENV_TEST_FAILURE: Cannot connect to AP') raise print('Checking py-client received msg published from esp...') if not event_client_received_correct.wait(timeout=30): raise ValueError('Wrong data received, msg log: {}'.format(message_log)) print('Checking esp-client received msg published from py-client...') - dut1.expect(re.compile(r'DATA=data_to_esp32'), timeout=30) + dut.expect(r'DATA=data_to_esp32', timeout=30) finally: event_stop_client.set() thread1.join() - - -if __name__ == '__main__': - test_examples_protocol_mqtt_ws() diff --git a/examples/protocols/mqtt/wss/mqtt_wss_example_test.py b/examples/protocols/mqtt/wss/pytest_mqtt_wss_example.py similarity index 73% rename from examples/protocols/mqtt/wss/mqtt_wss_example_test.py rename to examples/protocols/mqtt/wss/pytest_mqtt_wss_example.py index c0a1454..05eff5e 100644 --- a/examples/protocols/mqtt/wss/mqtt_wss_example_test.py +++ b/examples/protocols/mqtt/wss/pytest_mqtt_wss_example.py @@ -1,3 +1,8 @@ +#!/usr/bin/env python +# +# SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD +# SPDX-License-Identifier: Unlicense OR CC0-1.0 +import logging import os import re import ssl @@ -5,8 +10,9 @@ import sys from threading import Event, Thread import paho.mqtt.client as mqtt -import ttfw_idf -from tiny_test_fw import DUT +import pexpect +import pytest +from pytest_embedded import Dut event_client_connected = Event() event_stop_client = Event() @@ -15,19 +21,21 @@ message_log = '' # The callback for when the client receives a CONNACK response from the server. -def on_connect(client, userdata, flags, rc): +def on_connect(client, userdata, flags, rc): # type: (mqtt.Client, tuple, bool, str) -> None + _ = (userdata, flags) print('Connected with result code ' + str(rc)) event_client_connected.set() client.subscribe('/topic/qos0') -def mqtt_client_task(client): +def mqtt_client_task(client): # type: (mqtt.Client) -> None while not event_stop_client.is_set(): client.loop() # The callback for when a PUBLISH message is received from the server. -def on_message(client, userdata, msg): +def on_message(client, userdata, msg): # type: (mqtt.Client, tuple, mqtt.client.MQTTMessage) -> None + _ = userdata global message_log payload = msg.payload.decode() if not event_client_received_correct.is_set() and payload == 'data': @@ -37,8 +45,9 @@ def on_message(client, userdata, msg): message_log += 'Received data:' + msg.topic + ' ' + payload + '\n' -@ttfw_idf.idf_example_test(env_tag='ethernet_router') -def test_examples_protocol_mqtt_wss(env, extra_data): +@pytest.mark.esp32 +@pytest.mark.ethernet +def test_examples_protocol_mqtt_wss(dut): # type: (Dut) -> None broker_url = '' broker_port = 0 """ @@ -48,14 +57,14 @@ def test_examples_protocol_mqtt_wss(env, extra_data): 3. Test evaluates it received correct qos0 message 4. Test ESP32 client received correct qos0 message """ - dut1 = env.get_dut('mqtt_websocket_secure', 'examples/protocols/mqtt/wss', dut_class=ttfw_idf.ESP32DUT) # check and log bin size - binary_file = os.path.join(dut1.app.binary_path, 'mqtt_websocket_secure.bin') + binary_file = os.path.join(dut.app.binary_path, 'mqtt_websocket_secure.bin') bin_size = os.path.getsize(binary_file) - ttfw_idf.log_performance('mqtt_websocket_secure_bin_size', '{}KB'.format(bin_size // 1024)) + logging.info('[Performance][mqtt_websocket_secure_bin_size]: %s KB', bin_size // 1024) # Look for host:port in sdkconfig try: - value = re.search(r'\:\/\/([^:]+)\:([0-9]+)', dut1.app.get_sdkconfig()['CONFIG_BROKER_URI']) + value = re.search(r'\:\/\/([^:]+)\:([0-9]+)', dut.app.sdkconfig.get('BROKER_URI')) + assert value is not None broker_url = value.group(1) broker_port = int(value.group(2)) except Exception: @@ -82,22 +91,17 @@ def test_examples_protocol_mqtt_wss(env, extra_data): print('Connecting py-client to broker {}:{}...'.format(broker_url, broker_port)) if not event_client_connected.wait(timeout=30): raise ValueError('ENV_TEST_FAILURE: Test script cannot connect to broker: {}'.format(broker_url)) - dut1.start_app() try: - ip_address = dut1.expect(re.compile(r'IPv4 address: (\d+\.\d+\.\d+\.\d+)[^\d]'), timeout=30)[0] + ip_address = dut.expect(r'IPv4 address: (\d+\.\d+\.\d+\.\d+)[^\d]', timeout=30)[0] print('Connected to AP with IP: {}'.format(ip_address)) - except DUT.ExpectTimeout: + except pexpect.TIMEOUT: print('ENV_TEST_FAILURE: Cannot connect to AP') raise print('Checking py-client received msg published from esp...') if not event_client_received_correct.wait(timeout=30): raise ValueError('Wrong data received, msg log: {}'.format(message_log)) print('Checking esp-client received msg published from py-client...') - dut1.expect(re.compile(r'DATA=data_to_esp32'), timeout=30) + dut.expect(r'DATA=data_to_esp32', timeout=30) finally: event_stop_client.set() thread1.join() - - -if __name__ == '__main__': - test_examples_protocol_mqtt_wss() diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/app_test.py b/tools/test_apps/protocols/mqtt/publish_connect_test/pytest_mqtt_app.py similarity index 78% rename from tools/test_apps/protocols/mqtt/publish_connect_test/app_test.py rename to tools/test_apps/protocols/mqtt/publish_connect_test/pytest_mqtt_app.py index f87c376..fd6133f 100644 --- a/tools/test_apps/protocols/mqtt/publish_connect_test/app_test.py +++ b/tools/test_apps/protocols/mqtt/publish_connect_test/pytest_mqtt_app.py @@ -1,5 +1,8 @@ +# SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD +# SPDX-License-Identifier: Unlicense OR CC0-1.0 from __future__ import print_function, unicode_literals +import logging import os import random import re @@ -10,21 +13,25 @@ import string import subprocess import sys import time +import typing from itertools import count from threading import Event, Lock, Thread +from typing import Any import paho.mqtt.client as mqtt -import ttfw_idf +import pytest from common_test_methods import get_host_ip4_by_dest_ip +from pytest_embedded import Dut +from pytest_embedded_qemu.dut import QemuDut DEFAULT_MSG_SIZE = 16 -def _path(f): +def _path(f): # type: (str) -> str return os.path.join(os.path.dirname(os.path.realpath(__file__)),f) -def set_server_cert_cn(ip): +def set_server_cert_cn(ip): # type: (str) -> None arg_list = [ ['openssl', 'req', '-out', _path('srv.csr'), '-key', _path('server.key'),'-subj', '/CN={}'.format(ip), '-new'], ['openssl', 'x509', '-req', '-in', _path('srv.csr'), '-CA', _path('ca.crt'), @@ -36,8 +43,13 @@ def set_server_cert_cn(ip): # Publisher class creating a python client to send/receive published data from esp-mqtt client class MqttPublisher: + event_client_connected = Event() + event_client_got_all = Event() + expected_data = '' + published = 0 - def __init__(self, dut, transport, qos, repeat, published, queue, publish_cfg, log_details=False): + def __init__(self, dut, transport, + qos, repeat, published, queue, publish_cfg, log_details=False): # type: (MqttPublisher, Dut, str, int, int, int, int, dict, bool) -> None # instance variables used as parameters of the publish test self.event_stop_client = Event() self.sample_string = ''.join(random.choice(string.ascii_uppercase + string.ascii_lowercase + string.digits) for _ in range(DEFAULT_MSG_SIZE)) @@ -58,11 +70,11 @@ class MqttPublisher: MqttPublisher.event_client_got_all.clear() MqttPublisher.expected_data = self.sample_string * self.repeat - def print_details(self, text): + def print_details(self, text): # type: (str) -> None if self.log_details: print(text) - def mqtt_client_task(self, client, lock): + def mqtt_client_task(self, client, lock): # type: (MqttPublisher, mqtt.Client, Lock) -> None while not self.event_stop_client.is_set(): with lock: client.loop() @@ -70,12 +82,12 @@ class MqttPublisher: # The callback for when the client receives a CONNACK response from the server (needs to be static) @staticmethod - def on_connect(_client, _userdata, _flags, _rc): + def on_connect(_client, _userdata, _flags, _rc): # type: (mqtt.Client, tuple, bool, str) -> None MqttPublisher.event_client_connected.set() # The callback for when a PUBLISH message is received from the server (needs to be static) @staticmethod - def on_message(client, userdata, msg): + def on_message(client, userdata, msg): # type: (mqtt.Client, int, mqtt.client.MQTTMessage) -> None payload = msg.payload.decode() if payload == MqttPublisher.expected_data: userdata += 1 @@ -83,7 +95,7 @@ class MqttPublisher: if userdata == MqttPublisher.published: MqttPublisher.event_client_got_all.set() - def __enter__(self): + def __enter__(self): # type: (MqttPublisher) -> None qos = self.publish_cfg['qos'] queue = self.publish_cfg['queue'] @@ -100,6 +112,7 @@ class MqttPublisher: self.client = mqtt.Client(transport='websockets') else: self.client = mqtt.Client() + assert self.client is not None self.client.on_connect = MqttPublisher.on_connect self.client.on_message = MqttPublisher.on_message self.client.user_data_set(0) @@ -137,7 +150,8 @@ class MqttPublisher: self.event_stop_client.set() thread1.join() - def __exit__(self, exc_type, exc_value, traceback): + def __exit__(self, exc_type, exc_value, traceback): # type: (MqttPublisher, str, str, dict) -> None + assert self.client is not None self.client.disconnect() self.event_stop_client.clear() @@ -145,7 +159,7 @@ class MqttPublisher: # Simple server for mqtt over TLS connection class TlsServer: - def __init__(self, port, client_cert=False, refuse_connection=False, use_alpn=False): + def __init__(self, port, client_cert=False, refuse_connection=False, use_alpn=False): # type: (TlsServer, int, bool, bool, bool) -> None self.port = port self.socket = socket.socket() self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) @@ -153,11 +167,9 @@ class TlsServer: self.shutdown = Event() self.client_cert = client_cert self.refuse_connection = refuse_connection - self.ssl_error = None self.use_alpn = use_alpn - self.negotiated_protocol = None - def __enter__(self): + def __enter__(self): # type: (TlsServer) -> TlsServer try: self.socket.bind(('', self.port)) except socket.error as e: @@ -170,20 +182,21 @@ class TlsServer: return self - def __exit__(self, exc_type, exc_value, traceback): + def __exit__(self, exc_type, exc_value, traceback): # type: (TlsServer, str, str, str) -> None self.shutdown.set() self.server_thread.join() self.socket.close() if (self.conn is not None): self.conn.close() - def get_last_ssl_error(self): + def get_last_ssl_error(self): # type: (TlsServer) -> str return self.ssl_error + @typing.no_type_check def get_negotiated_protocol(self): return self.negotiated_protocol - def run_server(self): + def run_server(self) -> None: context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH) if self.client_cert: context.verify_mode = ssl.CERT_REQUIRED @@ -201,11 +214,10 @@ class TlsServer: print(' - negotiated_protocol: {}'.format(self.negotiated_protocol)) self.handle_conn() except ssl.SSLError as e: - self.conn = None self.ssl_error = str(e) print(' - SSLError: {}'.format(str(e))) - def handle_conn(self): + def handle_conn(self) -> None: while not self.shutdown.is_set(): r,w,e = select.select([self.conn], [], [], 1) try: @@ -216,7 +228,7 @@ class TlsServer: print(' - error: {}'.format(err)) raise - def process_mqtt_connect(self): + def process_mqtt_connect(self) -> None: try: data = bytearray(self.conn.recv(1024)) message = ''.join(format(x, '02x') for x in data) @@ -235,22 +247,22 @@ class TlsServer: self.shutdown.set() -def connection_tests(dut, cases, dut_ip): +def connection_tests(dut, cases, dut_ip): # type: (Dut, dict, str) -> None ip = get_host_ip4_by_dest_ip(dut_ip) set_server_cert_cn(ip) server_port = 2222 - def teardown_connection_suite(): + def teardown_connection_suite() -> None: dut.write('conn teardown 0 0') - def start_connection_case(case, desc): + def start_connection_case(case, desc): # type: (str, str) -> Any print('Starting {}: {}'.format(case, desc)) case_id = cases[case] dut.write('conn {} {} {}'.format(ip, server_port, case_id)) dut.expect('Test case:{} started'.format(case_id)) return case_id - for case in ['CONFIG_EXAMPLE_CONNECT_CASE_NO_CERT', 'CONFIG_EXAMPLE_CONNECT_CASE_SERVER_CERT', 'CONFIG_EXAMPLE_CONNECT_CASE_SERVER_DER_CERT']: + for case in ['EXAMPLE_CONNECT_CASE_NO_CERT', 'EXAMPLE_CONNECT_CASE_SERVER_CERT', 'EXAMPLE_CONNECT_CASE_SERVER_DER_CERT']: # All these cases connect to the server with no server verification or with server only verification with TlsServer(server_port): test_nr = start_connection_case(case, 'default server - expect to connect normally') @@ -266,13 +278,13 @@ def connection_tests(dut, cases, dut_ip): if 'PEER_DID_NOT_RETURN_A_CERTIFICATE' not in s.get_last_ssl_error(): raise RuntimeError('Unexpected ssl error from the server {}'.format(s.get_last_ssl_error())) - for case in ['CONFIG_EXAMPLE_CONNECT_CASE_MUTUAL_AUTH', 'CONFIG_EXAMPLE_CONNECT_CASE_MUTUAL_AUTH_KEY_PWD']: + for case in ['EXAMPLE_CONNECT_CASE_MUTUAL_AUTH', 'EXAMPLE_CONNECT_CASE_MUTUAL_AUTH_KEY_PWD']: # These cases connect to server with both server and client verification (client key might be password protected) with TlsServer(server_port, client_cert=True): test_nr = start_connection_case(case, 'server with client verification - expect to connect normally') dut.expect('MQTT_EVENT_CONNECTED: Test={}'.format(test_nr), timeout=30) - case = 'CONFIG_EXAMPLE_CONNECT_CASE_INVALID_SERVER_CERT' + case = 'EXAMPLE_CONNECT_CASE_INVALID_SERVER_CERT' with TlsServer(server_port) as s: test_nr = start_connection_case(case, 'invalid server certificate on default server - expect ssl handshake error') dut.expect('MQTT_EVENT_ERROR: Test={}'.format(test_nr), timeout=30) @@ -280,7 +292,7 @@ def connection_tests(dut, cases, dut_ip): if 'alert unknown ca' not in s.get_last_ssl_error(): raise Exception('Unexpected ssl error from the server {}'.format(s.get_last_ssl_error())) - case = 'CONFIG_EXAMPLE_CONNECT_CASE_MUTUAL_AUTH_BAD_CRT' + case = 'EXAMPLE_CONNECT_CASE_MUTUAL_AUTH_BAD_CRT' with TlsServer(server_port, client_cert=True) as s: test_nr = start_connection_case(case, 'Invalid client certificate on server with client verification - expect ssl handshake error') dut.expect('MQTT_EVENT_ERROR: Test={}'.format(test_nr), timeout=30) @@ -288,13 +300,13 @@ def connection_tests(dut, cases, dut_ip): if 'CERTIFICATE_VERIFY_FAILED' not in s.get_last_ssl_error(): raise Exception('Unexpected ssl error from the server {}'.format(s.get_last_ssl_error())) - for case in ['CONFIG_EXAMPLE_CONNECT_CASE_NO_CERT', 'CONFIG_EXAMPLE_CONNECT_CASE_NO_CERT_ALPN']: + for case in ['EXAMPLE_CONNECT_CASE_NO_CERT', 'EXAMPLE_CONNECT_CASE_NO_CERT_ALPN']: with TlsServer(server_port, use_alpn=True) as s: test_nr = start_connection_case(case, 'server with alpn - expect connect, check resolved protocol') dut.expect('MQTT_EVENT_CONNECTED: Test={}'.format(test_nr), timeout=30) - if case == 'CONFIG_EXAMPLE_CONNECT_CASE_NO_CERT' and s.get_negotiated_protocol() is None: + if case == 'EXAMPLE_CONNECT_CASE_NO_CERT' and s.get_negotiated_protocol() is None: print(' - client with alpn off, no negotiated protocol: OK') - elif case == 'CONFIG_EXAMPLE_CONNECT_CASE_NO_CERT_ALPN' and s.get_negotiated_protocol() == 'mymqtt': + elif case == 'EXAMPLE_CONNECT_CASE_NO_CERT_ALPN' and s.get_negotiated_protocol() == 'mymqtt': print(' - client with alpn on, negotiated protocol resolved: OK') else: raise Exception('Unexpected negotiated protocol {}'.format(s.get_negotiated_protocol())) @@ -302,19 +314,19 @@ def connection_tests(dut, cases, dut_ip): teardown_connection_suite() -@ttfw_idf.idf_custom_test(env_tag='ethernet_router', group='test-apps') -def test_app_protocol_mqtt_publish_connect(env, extra_data): +@pytest.mark.esp32 +@pytest.mark.ethernet +def test_app_protocol_mqtt_publish_connect(dut: Dut) -> None: """ steps: 1. join AP 2. connect to uri specified in the config 3. send and receive data """ - dut1 = env.get_dut('mqtt_publish_connect_test', 'tools/test_apps/protocols/mqtt/publish_connect_test') # check and log bin size - binary_file = os.path.join(dut1.app.binary_path, 'mqtt_publish_connect_test.bin') + binary_file = os.path.join(dut.app.binary_path, 'mqtt_publish_connect_test.bin') bin_size = os.path.getsize(binary_file) - ttfw_idf.log_performance('mqtt_publish_connect_test_bin_size', '{}KB'.format(bin_size // 1024)) + logging.info('[Performance][mqtt_publish_connect_test_bin_size]: %s KB', bin_size // 1024) # Look for test case symbolic names and publish configs cases = {} @@ -322,25 +334,24 @@ def test_app_protocol_mqtt_publish_connect(env, extra_data): try: # Get connection test cases configuration: symbolic names for test cases - for case in ['CONFIG_EXAMPLE_CONNECT_CASE_NO_CERT', - 'CONFIG_EXAMPLE_CONNECT_CASE_SERVER_CERT', - 'CONFIG_EXAMPLE_CONNECT_CASE_MUTUAL_AUTH', - 'CONFIG_EXAMPLE_CONNECT_CASE_INVALID_SERVER_CERT', - 'CONFIG_EXAMPLE_CONNECT_CASE_SERVER_DER_CERT', - 'CONFIG_EXAMPLE_CONNECT_CASE_MUTUAL_AUTH_KEY_PWD', - 'CONFIG_EXAMPLE_CONNECT_CASE_MUTUAL_AUTH_BAD_CRT', - 'CONFIG_EXAMPLE_CONNECT_CASE_NO_CERT_ALPN']: - cases[case] = dut1.app.get_sdkconfig()[case] + for case in ['EXAMPLE_CONNECT_CASE_NO_CERT', + 'EXAMPLE_CONNECT_CASE_SERVER_CERT', + 'EXAMPLE_CONNECT_CASE_MUTUAL_AUTH', + 'EXAMPLE_CONNECT_CASE_INVALID_SERVER_CERT', + 'EXAMPLE_CONNECT_CASE_SERVER_DER_CERT', + 'EXAMPLE_CONNECT_CASE_MUTUAL_AUTH_KEY_PWD', + 'EXAMPLE_CONNECT_CASE_MUTUAL_AUTH_BAD_CRT', + 'EXAMPLE_CONNECT_CASE_NO_CERT_ALPN']: + cases[case] = dut.app.sdkconfig.get(case) except Exception: print('ENV_TEST_FAILURE: Some mandatory CONNECTION test case not found in sdkconfig') raise - dut1.start_app() - esp_ip = dut1.expect(re.compile(r'IPv4 address: (\d+\.\d+\.\d+\.\d+)[^\d]'), timeout=30)[0] + esp_ip = dut.expect(r'IPv4 address: (\d+\.\d+\.\d+\.\d+)[^\d]', timeout=30).group(1).decode() print('Got IP={}'.format(esp_ip)) if not os.getenv('MQTT_SKIP_CONNECT_TEST'): - connection_tests(dut1,cases,esp_ip) + connection_tests(dut,cases,esp_ip) # # start publish tests only if enabled in the environment (for weekend tests only) @@ -349,27 +360,28 @@ def test_app_protocol_mqtt_publish_connect(env, extra_data): # Get publish test configuration try: - def get_host_port_from_dut(dut1, config_option): - value = re.search(r'\:\/\/([^:]+)\:([0-9]+)', dut1.app.get_sdkconfig()[config_option]) + @typing.no_type_check + def get_host_port_from_dut(dut, config_option): + value = re.search(r'\:\/\/([^:]+)\:([0-9]+)', dut.app.sdkconfig.get(config_option)) if value is None: return None, None return value.group(1), int(value.group(2)) - publish_cfg['publish_topic'] = dut1.app.get_sdkconfig()['CONFIG_EXAMPLE_SUBSCRIBE_TOPIC'].replace('"','') - publish_cfg['subscribe_topic'] = dut1.app.get_sdkconfig()['CONFIG_EXAMPLE_PUBLISH_TOPIC'].replace('"','') - publish_cfg['broker_host_ssl'], publish_cfg['broker_port_ssl'] = get_host_port_from_dut(dut1, 'CONFIG_EXAMPLE_BROKER_SSL_URI') - publish_cfg['broker_host_tcp'], publish_cfg['broker_port_tcp'] = get_host_port_from_dut(dut1, 'CONFIG_EXAMPLE_BROKER_TCP_URI') - publish_cfg['broker_host_ws'], publish_cfg['broker_port_ws'] = get_host_port_from_dut(dut1, 'CONFIG_EXAMPLE_BROKER_WS_URI') - publish_cfg['broker_host_wss'], publish_cfg['broker_port_wss'] = get_host_port_from_dut(dut1, 'CONFIG_EXAMPLE_BROKER_WSS_URI') + publish_cfg['publish_topic'] = dut.app.sdkconfig.get('EXAMPLE_SUBSCRIBE_TOPIC').replace('"','') + publish_cfg['subscribe_topic'] = dut.app.sdkconfig.get('EXAMPLE_PUBLISH_TOPIC').replace('"','') + publish_cfg['broker_host_ssl'], publish_cfg['broker_port_ssl'] = get_host_port_from_dut(dut, 'EXAMPLE_BROKER_SSL_URI') + publish_cfg['broker_host_tcp'], publish_cfg['broker_port_tcp'] = get_host_port_from_dut(dut, 'EXAMPLE_BROKER_TCP_URI') + publish_cfg['broker_host_ws'], publish_cfg['broker_port_ws'] = get_host_port_from_dut(dut, 'EXAMPLE_BROKER_WS_URI') + publish_cfg['broker_host_wss'], publish_cfg['broker_port_wss'] = get_host_port_from_dut(dut, 'EXAMPLE_BROKER_WSS_URI') except Exception: print('ENV_TEST_FAILURE: Some mandatory PUBLISH test case not found in sdkconfig') raise - def start_publish_case(transport, qos, repeat, published, queue): + def start_publish_case(transport, qos, repeat, published, queue): # type: (str, int, int, int, int) -> None print('Starting Publish test: transport:{}, qos:{}, nr_of_msgs:{}, msg_size:{}, enqueue:{}' .format(transport, qos, published, repeat * DEFAULT_MSG_SIZE, queue)) - with MqttPublisher(dut1, transport, qos, repeat, published, queue, publish_cfg): + with MqttPublisher(dut, transport, qos, repeat, published, queue, publish_cfg): pass # Initialize message sizes and repeat counts (if defined in the environment) @@ -378,7 +390,7 @@ def test_app_protocol_mqtt_publish_connect(env, extra_data): # Check env variable: MQTT_PUBLISH_MSG_{len|repeat}_{x} env_dict = {var:'MQTT_PUBLISH_MSG_' + var + '_' + str(i) for var in ['len', 'repeat']} if os.getenv(env_dict['len']) and os.getenv(env_dict['repeat']): - messages.append({var: int(os.getenv(env_dict[var])) for var in ['len', 'repeat']}) + messages.append({var: int(os.getenv(env_dict[var])) for var in ['len', 'repeat']}) # type: ignore continue break if not messages: # No message sizes present in the env - set defaults @@ -400,4 +412,4 @@ def test_app_protocol_mqtt_publish_connect(env, extra_data): if __name__ == '__main__': - test_app_protocol_mqtt_publish_connect(dut=ttfw_idf.ESP32QEMUDUT if sys.argv[1:] == ['qemu'] else ttfw_idf.ESP32DUT) + test_app_protocol_mqtt_publish_connect(dut=QemuDut if sys.argv[1:] == ['qemu'] else Dut) From 339e438840d70a5436bd56dc28c2e689bf94f95e Mon Sep 17 00:00:00 2001 From: Chen Yudong Date: Wed, 21 Dec 2022 10:50:11 +0800 Subject: [PATCH 158/231] CI: fix pattern in test case test_examples_protocol_mqtt5 --- examples/protocols/mqtt5/pytest_mqtt5.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/examples/protocols/mqtt5/pytest_mqtt5.py b/examples/protocols/mqtt5/pytest_mqtt5.py index 045ec40..603be68 100644 --- a/examples/protocols/mqtt5/pytest_mqtt5.py +++ b/examples/protocols/mqtt5/pytest_mqtt5.py @@ -26,24 +26,24 @@ def test_examples_protocol_mqtt5(dut: Dut) -> None: # check if connected or not dut.expect_exact('MQTT_EVENT_CONNECTED', timeout=30) # check log - res = dut.expect(r'sent publish successful, msg_id=(\d+)') + res = dut.expect(r'sent publish successful, msg_id=(\d+)[^\d]') msgid_pub1 = res.group(1).decode('utf8') - res = dut.expect(r'sent subscribe successful, msg_id=(\d+)') + res = dut.expect(r'sent subscribe successful, msg_id=(\d+)[^\d]') msgid_sub1 = res.group(1).decode('utf8') - res = dut.expect(r'sent subscribe successful, msg_id=(\d+)') + res = dut.expect(r'sent subscribe successful, msg_id=(\d+)[^\d]') msgid_sub2 = res.group(1).decode('utf8') - res = dut.expect(r'sent unsubscribe successful, msg_id=(\d+)') + res = dut.expect(r'sent unsubscribe successful, msg_id=(\d+)[^\d]') msgid_unsub = res.group(1).decode('utf8') - res = dut.expect(r'MQTT_EVENT_PUBLISHED, msg_id=(\d+)') + res = dut.expect(r'MQTT_EVENT_PUBLISHED, msg_id=(\d+)[^\d]') msgid_pubd = res.group(1).decode('utf8') assert msgid_pubd == msgid_pub1 - res = dut.expect(r'MQTT_EVENT_SUBSCRIBED, msg_id=(\d+)') + res = dut.expect(r'MQTT_EVENT_SUBSCRIBED, msg_id=(\d+)[^\d]') msgid_subd = res.group(1).decode('utf8') assert msgid_subd == msgid_sub1 dut.expect_exact('sent publish successful, msg_id=0') - res = dut.expect(r'MQTT_EVENT_SUBSCRIBED, msg_id=(\d+)') + res = dut.expect(r'MQTT_EVENT_SUBSCRIBED, msg_id=(\d+)[^\d]') msgid_subd = res.group(1).decode('utf8') assert msgid_subd == msgid_sub2 @@ -57,7 +57,7 @@ def test_examples_protocol_mqtt5(dut: Dut) -> None: dut.expect_exact('correlation_data is 123456') dut.expect_exact('TOPIC=/topic/qos1') dut.expect_exact('DATA=data_3') - res = dut.expect(r'MQTT_EVENT_UNSUBSCRIBED, msg_id=(\d+)') + res = dut.expect(r'MQTT_EVENT_UNSUBSCRIBED, msg_id=(\d+)[^\d]') msgid_unsubd = res.group(1).decode('utf8') assert msgid_unsubd == msgid_unsub From 8f2a725a867616d955895131e077f9aff77c7203 Mon Sep 17 00:00:00 2001 From: Wang Zi Yan Date: Tue, 20 Dec 2022 12:28:38 +0800 Subject: [PATCH 159/231] docs: update mqtt wifi coexist --- docs/en/api-reference/protocols/mqtt.rst | 41 +++++----- docs/zh_CN/api-reference/protocols/mqtt.rst | 90 ++++++++++++--------- 2 files changed, 71 insertions(+), 60 deletions(-) diff --git a/docs/en/api-reference/protocols/mqtt.rst b/docs/en/api-reference/protocols/mqtt.rst index 3138550..ae95fc3 100644 --- a/docs/en/api-reference/protocols/mqtt.rst +++ b/docs/en/api-reference/protocols/mqtt.rst @@ -18,39 +18,36 @@ Features Application Examples --------------------- - * :example:`protocols/mqtt/tcp`: MQTT over TCP, default port 1883 - * :example:`protocols/mqtt/ssl`: MQTT over TLS, default port 8883 - * :example:`protocols/mqtt/ssl_ds`: MQTT over TLS using digital signature peripheral for authentication, default port 8883 - * :example:`protocols/mqtt/ssl_mutual_auth`: MQTT over TLS using certificates for authentication, default port 8883 - * :example:`protocols/mqtt/ssl_psk`: MQTT over TLS using pre-shared keys for authentication, default port 8883 - * :example:`protocols/mqtt/ws`: MQTT over WebSocket, default port 80 - * :example:`protocols/mqtt/wss`: MQTT over WebSocket Secure, default port 443 + * :example:`protocols/mqtt/tcp`: MQTT over TCP, default port 1883 + * :example:`protocols/mqtt/ssl`: MQTT over TLS, default port 8883 + * :example:`protocols/mqtt/ssl_ds`: MQTT over TLS using digital signature peripheral for authentication, default port 8883 + * :example:`protocols/mqtt/ssl_mutual_auth`: MQTT over TLS using certificates for authentication, default port 8883 + * :example:`protocols/mqtt/ssl_psk`: MQTT over TLS using pre-shared keys for authentication, default port 8883 + * :example:`protocols/mqtt/ws`: MQTT over WebSocket, default port 80 + * :example:`protocols/mqtt/wss`: MQTT over WebSocket Secure, default port 443 -MQTT message retransmission +MQTT Message Retransmission --------------------------- -A new mqtt message is created by calling :cpp:func:`esp_mqtt_client_publish ` or its non blocking -counterpart :cpp:func:`esp_mqtt_client_enqueue `. +A new MQTT message is created by calling :cpp:func:`esp_mqtt_client_publish ` or its non blocking counterpart :cpp:func:`esp_mqtt_client_enqueue `. -Messages with QoS 0 will be sent only once, QoS 1 and 2 have a different behavior since the protocol requires extra steps to complete the process. +Messages with QoS 0 will be sent only once. QoS 1 and 2 have different behaviors since the protocol requires extra steps to complete the process. -The ESP-MQTT library opts to always retransmit unacknowledged QoS 1 and 2 PUBLISH messages to avoid losses in faulty connections, even though the MQTT specification -requires the re-transmission only on reconnect with Clean Session flag been set to 0 (set :cpp:member:`disable_clean_session ` to true for this behavior). +The ESP-MQTT library opts to always retransmit unacknowledged QoS 1 and 2 publish messages to avoid losses in faulty connections, even though the MQTT specification requires the re-transmission only on reconnect with Clean Session flag been set to 0 (set :cpp:member:`disable_clean_session ` to true for this behavior). - -Messages that could need retransmission, QoS 1 and 2, are always enqueued, but first transmission try occurs immediately if :cpp:func:`esp_mqtt_client_publish ` is used. A transmission retry for unacknowledged messages will occur after :cpp:member:`message_retransmit_timeout `. After :ref:`CONFIG_MQTT_OUTBOX_EXPIRED_TIMEOUT_MS` messages will expire and deleted. If :ref:`CONFIG_MQTT_REPORT_DELETED_MESSAGES` is set an event is sent to notify the user. +QoS 1 and 2 messages that may need retransmission are always enqueued, but first transmission try occurs immediately if :cpp:func:`esp_mqtt_client_publish ` is used. A transmission retry for unacknowledged messages will occur after :cpp:member:`message_retransmit_timeout `. After :ref:`CONFIG_MQTT_OUTBOX_EXPIRED_TIMEOUT_MS` messages will expire and be deleted. If :ref:`CONFIG_MQTT_REPORT_DELETED_MESSAGES` is set, an event will be sent to notify the user. Configuration ------------- The configuration is made by setting fields in :cpp:class:`esp_mqtt_client_config_t` struct. The configuration struct has the following sub structs to configure different aspects of the client operation. - * :cpp:class:`esp_mqtt_client_config_t::broker_t` - Allow to set address and security verification. - * :cpp:class:`esp_mqtt_client_config_t::credentials_t` - Client credentials for authentication. - * :cpp:class:`esp_mqtt_client_config_t::session_t` - Configuration for MQTT session aspects. - * :cpp:class:`esp_mqtt_client_config_t::network_t` - Networking related configuration. - * :cpp:class:`esp_mqtt_client_config_t::task_t` - Allow to configure FreeRTOS task. - * :cpp:class:`esp_mqtt_client_config_t::buffer_t` - Buffer size for input and output. + * :cpp:class:`esp_mqtt_client_config_t::broker_t` - Allow to set address and security verification. + * :cpp:class:`esp_mqtt_client_config_t::credentials_t` - Client credentials for authentication. + * :cpp:class:`esp_mqtt_client_config_t::session_t` - Configuration for MQTT session aspects. + * :cpp:class:`esp_mqtt_client_config_t::network_t` - Networking related configuration. + * :cpp:class:`esp_mqtt_client_config_t::task_t` - Allow to configure FreeRTOS task. + * :cpp:class:`esp_mqtt_client_config_t::buffer_t` - Buffer size for input and output. In the following sections, the most common aspects are detailed. @@ -131,7 +128,7 @@ Client Credentials All client related credentials are under the :cpp:class:`credentials ` field. - * :cpp:member:`username ` pointer to the username used for connecting to the broker, can also be set by URI + * :cpp:member:`username `: pointer to the username used for connecting to the broker, can also be set by URI * :cpp:member:`client_id `: pointer to the client ID, defaults to ``ESP32_%CHIPID%`` where ``%CHIPID%`` are the last 3 bytes of MAC address in hex format ============== diff --git a/docs/zh_CN/api-reference/protocols/mqtt.rst b/docs/zh_CN/api-reference/protocols/mqtt.rst index 8f3eefb..d164a86 100644 --- a/docs/zh_CN/api-reference/protocols/mqtt.rst +++ b/docs/zh_CN/api-reference/protocols/mqtt.rst @@ -18,26 +18,36 @@ ESP-MQTT 是 `MQTT `_ 协议客户端的实现。MQTT 是一 应用示例 ------------------- - * :example:`protocols/mqtt/tcp`:基于 TCP 的 MQTT,默认端口 1883 - * :example:`protocols/mqtt/ssl`:基于 TLS 的 MQTT,默认端口 8883 - * :example:`protocols/mqtt/ssl_ds`:基于 TLS 的 MQTT,使用数字签名外设进行身份验证,默认端口 8883 - * :example:`protocols/mqtt/ssl_mutual_auth`:基于 TLS 的 MQTT,使用证书进行身份验证,默认端口 8883 - * :example:`protocols/mqtt/ssl_psk`:基于 TLS 的 MQTT,使用预共享密钥进行身份验证,默认端口 8883 - * :example:`protocols/mqtt/ws`:基于 WebSocket 的 MQTT,默认端口 80 - * :example:`protocols/mqtt/wss`:基于 WebSocket Secure 的 MQTT,默认端口 443 + * :example:`protocols/mqtt/tcp`:基于 TCP 的 MQTT,默认端口 1883 + * :example:`protocols/mqtt/ssl`:基于 TLS 的 MQTT,默认端口 8883 + * :example:`protocols/mqtt/ssl_ds`:基于 TLS 的 MQTT,使用数字签名外设进行身份验证,默认端口 8883 + * :example:`protocols/mqtt/ssl_mutual_auth`:基于 TLS 的 MQTT,使用证书进行身份验证,默认端口 8883 + * :example:`protocols/mqtt/ssl_psk`:基于 TLS 的 MQTT,使用预共享密钥进行身份验证,默认端口 8883 + * :example:`protocols/mqtt/ws`:基于 WebSocket 的 MQTT,默认端口 80 + * :example:`protocols/mqtt/wss`:基于 WebSocket Secure 的 MQTT,默认端口 443 +MQTT 消息重传 +-------------------------- + +调用 :cpp:func:`esp_mqtt_client_publish ` 或其非阻塞形式 :cpp:func:`esp_mqtt_client_enqueue `,可以创建新的 MQTT 消息。 + +QoS 0 的消息将只发送一次,QoS 1 和 2 具有不同行为,因为协议需要执行额外步骤来完成该过程。 + +ESP-MQTT 库将始终重新传输未确认的 QoS 1 和 2 发布消息,以避免连接错误导致信息丢失,虽然 MQTT 规范要求仅在重新连接且 Clean Session 标志设置为 0 时重新传输(针对此行为,将 :cpp:member:`disable_clean_session ` 设置为 true)。 + +可能需要重传的 QoS 1 和 2 消息总是处于排队状态,但若使用 :cpp:func:`esp_mqtt_client_publish ` 则会立即进行第一次传输尝试。未确认消息的重传将在 :cpp:member:`message_retransmit_timeout ` 之后进行。在 :ref:`CONFIG_MQTT_OUTBOX_EXPIRED_TIMEOUT_MS` 之后,消息会过期并被删除。如已设置 :ref:`CONFIG_MQTT_REPORT_DELETED_MESSAGES`,则会发送事件来通知用户。 配置 ------------- -通过设置 ``esp_mqtt_client_config_t`` 结构体中的字段来进行配置。配置结构体包含以下子结构体,用于配置客户端的多种操作。 +通过设置 :cpp:class:`esp_mqtt_client_config_t` 结构体中的字段来进行配置。配置结构体包含以下子结构体,用于配置客户端的多种操作。 - * :cpp:member:`broker` - 允许设置地址和安全验证。 - * :cpp:member:`credentials` - 用于身份验证的客户端凭据。 - * :cpp:member:`session` - MQTT 会话相关配置。 - * :cpp:member:`network` - 网络相关配置。 - * :cpp:member:`task` - 允许配置 FreeRTOS 任务。 - * :cpp:member:`buffer` - 输入输出的缓冲区大小。 + * :cpp:class:`esp_mqtt_client_config_t::broker_t` - 允许设置地址和安全验证。 + * :cpp:class:`esp_mqtt_client_config_t::credentials_t` - 用于身份验证的客户端凭据。 + * :cpp:class:`esp_mqtt_client_config_t::session_t` - MQTT 会话相关配置。 + * :cpp:class:`esp_mqtt_client_config_t::network_t` - 网络相关配置。 + * :cpp:class:`esp_mqtt_client_config_t::task_t` - 允许配置 FreeRTOS 任务。 + * :cpp:class:`esp_mqtt_client_config_t::buffer_t` - 输入输出的缓冲区大小。 下文将详细介绍不同配置。 @@ -48,9 +58,9 @@ ESP-MQTT 是 `MQTT `_ 协议客户端的实现。MQTT 是一 地址 =========== -通过 ``broker.address`` 结构体的 ``uri`` 字段或者 ``hostname``、``transport`` 以及 ``port`` 的组合,可以设置服务器地址。您也可以选择设置 ``path``,该字段对 WebSocket 连接而言非常有用。 +通过 :cpp:class:`address ` 结构体的 :cpp:member:`uri ` 字段或者 :cpp:member:`hostname `、:cpp:member:`transport ` 以及 :cpp:member:`port ` 的组合,可以设置服务器地址。您也可以选择设置 :cpp:member:`path `,该字段对 WebSocket 连接而言非常有用。 -使用 ``uri`` 字段的格式为 ``scheme://hostname:port/path``。 +使用 :cpp:member:`uri ` 字段的格式为 ``scheme://hostname:port/path``。 - 当前支持 ``mqtt``、``mqtts``、``ws`` 和 ``wss`` 协议 - 基于 TCP 的 MQTT 示例: @@ -90,12 +100,16 @@ ESP-MQTT 是 `MQTT `_ 协议客户端的实现。MQTT 是一 验证 ============= -为验证服务器身份,对于使用 TLS 的安全链接,必须设置 ``broker.verification`` 结构体。 -服务器证书可设置为 PEM 或 DER 格式。如要选择 DER 格式,必须设置等效 ``_len`` 字段,否则应在 ``certificate`` 字段传入以空字符结尾的 PEM 格式字符串。 +为验证服务器身份,对于使用 TLS 的安全链接,必须设置 :cpp:class:`verification ` 结构体。 +服务器证书可设置为 PEM 或 DER 格式。如要选择 DER 格式,必须设置等效 :cpp:member:`certificate_len ` 字段,否则应在 :cpp:member:`certificate ` 字段传入以空字符结尾的 PEM 格式字符串。 - 从服务器获取证书,例如:``mqtt.eclipseprojects.io`` - ``openssl s_client -showcerts -connect mqtt.eclipseprojects.io:8883 /dev/null|openssl x509 -outform PEM >mqtt_eclipse_org.pem`` -- 检查示例应用程序:``examples/mqtt_ssl`` + .. code:: + + openssl s_client -showcerts -connect mqtt.eclipseprojects.io:8883 < /dev/null \ + 2> /dev/null | openssl x509 -outform PEM > mqtt_eclipse_org.pem + +- 检查示例应用程序::example:`protocols/mqtt/ssl` - 配置: .. code:: c @@ -112,43 +126,43 @@ ESP-MQTT 是 `MQTT `_ 协议客户端的实现。MQTT 是一 客户端凭据 ^^^^^^^^^^^^^^^^^^^^^^^^ -``credentials`` 字段下包含所有客户端相关凭据。 +:cpp:class:`credentials ` 字段下包含所有客户端相关凭据。 - * ``username``:指向用于连接服务器用户名的指针,也可通过 URI 设置 - * ``client_id``:指向客户端 ID 的指针,默认为 ``ESP32_%CHIPID%``,其中 ``%CHIPID%`` 是十六进制 MAC 地址的最后 3 个字节 + * :cpp:member:`username `:指向用于连接服务器用户名的指针,也可通过 URI 设置 + * :cpp:member:`client_id `:指向客户端 ID 的指针,默认为 ``ESP32_%CHIPID%``,其中 ``%CHIPID%`` 是十六进制 MAC 地址的最后 3 个字节 =============== 认证 =============== -可以通过 ``authentication`` 字段设置认证参数。客户端支持以下认证方式: +可以通过 :cpp:class:`authentication ` 字段设置认证参数。客户端支持以下认证方式: - * ``authentication.password``:使用密码 - * ``authentication.certificate`` 和 ``authentication.key``:进行双向 TLS 身份验证,PEM 或 DER 格式均可 - * ``authentication.use_secure_element``:使用 ESP32-WROOM-32SE 中的安全元素 - * ``authentication.ds_data``:使用某些乐鑫设备的数字签名外设 + * :cpp:member:`password `:使用密码 + * * :cpp:member:`certificate ` 和 :cpp:member:`key `:进行双向 TLS 身份验证,PEM 或 DER 格式均可 + * :cpp:member:`use_secure_element `:使用 ESP32-WROOM-32SE 中的安全元素 + * :cpp:member:`ds_data `:使用某些乐鑫设备的数字签名外设 会话 ^^^^^^^^^^^^ -使用 ``section`` 字段进行 MQTT 会话相关配置。 +使用 :cpp:class:`session ` 字段进行 MQTT 会话相关配置。 ======================== 遗嘱消息 (LWT) ======================== -通过设置 ``esp_mqtt_client_config_t.session.last_will`` 结构体的以下字段,MQTT 会在一个客户端意外断开连接时通过遗嘱消息通知其他客户端。 +通过设置 :cpp:class:`last_will ` 结构体的以下字段,MQTT 会在一个客户端意外断开连接时通过遗嘱消息通知其他客户端。 - * ``topic``:指向 LWT 消息主题的指针 - * ``msg``:指向 LWT 消息的指针 - * ``msg_len``:LWT 消息的长度,``msg`` 不以空字符结尾时需要该字段 - * ``qos``:LWT 消息的服务质量 - * ``retain``:指定 LWT 消息的保留标志 + * :cpp:member:`topic `:指向 LWT 消息主题的指针 + * :cpp:member:`msg `:指向 LWT 消息的指针 + * :cpp:member:`msg_len `:LWT 消息的长度,:cpp:member:`msg ` 不以空字符结尾时需要该字段 + * :cpp:member:`qos `:LWT 消息的服务质量 + * :cpp:member:`retain `:指定 LWT 消息的保留标志 在项目配置菜单中设置 MQTT ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -通过 ``idf.py menuconfig``,可以在 ``Component config`` > ``ESP-MQTT Configuration`` 中找到 MQTT 设置。 +通过 :code:`idf.py menuconfig`,可以在 ``Component config`` > ``ESP-MQTT Configuration`` 中找到 MQTT 设置。 相关设置如下: @@ -169,8 +183,8 @@ MQTT 客户端可能会发布以下事件: * ``MQTT_EVENT_SUBSCRIBED``:服务器已确认客户端的订阅请求。事件数据将包含订阅消息的消息 ID。 * ``MQTT_EVENT_UNSUBSCRIBED``:服务器已确认客户端的退订请求。事件数据将包含退订消息的消息 ID。 * ``MQTT_EVENT_PUBLISHED``:服务器已确认客户端的发布消息。消息将仅针对 QoS 级别 1 和 2 发布,因为级别 0 不会进行确认。事件数据将包含发布消息的消息 ID。 -* ``MQTT_EVENT_DATA``:客户端已收到发布消息。事件数据包含:消息 ID、发布消息所属主题名称、收到的数据及其长度。对于超出内部缓冲区的数据,将发布多个 ``MQTT_EVENT_DATA``,并更新事件数据的 ``current_data_offset`` 和 ``total_data_len`` 以跟踪碎片化消息。 -* ``MQTT_EVENT_ERROR``:客户端遇到错误。使用事件数据 ``error_handle`` 中的 ``esp_mqtt_error_type_t``,可以进一步判断错误类型。错误类型决定 ``error_handle`` 结构体的哪些部分会被填充。 +* ``MQTT_EVENT_DATA``:客户端已收到发布消息。事件数据包含:消息 ID、发布消息所属主题名称、收到的数据及其长度。对于超出内部缓冲区的数据,将发布多个 ``MQTT_EVENT_DATA``,并更新事件数据的 :cpp:member:`current_data_offset ` 和 :cpp:member:`total_data_len` 以跟踪碎片化消息。 +* ``MQTT_EVENT_ERROR``:客户端遇到错误。使用事件数据 :cpp:type:`error_handle ` 字段中的 :cpp:type:`error_type `,可以发现错误。错误类型决定 :cpp:type:`error_handle ` 结构体的哪些部分会被填充。 API 参考 ------------- From 13c56ef79142da9953c376717d534b643b222d9f Mon Sep 17 00:00:00 2001 From: Euripedes Rocha Date: Mon, 17 Oct 2022 14:59:56 +0200 Subject: [PATCH 160/231] [MQTT] - Moves compile definition out of idf - MQTT build process is to be managed in esp-mqtt repository - Componenf directory points directly to submodule, usage must be unchanged --- components/mqtt/Kconfig | 166 ---------------- components/mqtt/host_test/CMakeLists.txt | 17 -- components/mqtt/host_test/README.md | 30 --- components/mqtt/host_test/main/CMakeLists.txt | 3 - .../mqtt/host_test/main/test_mqtt_client.cpp | 121 ------------ 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 - .../mqtt/host_test/mocks/include/sys/queue.h | 66 ------- components/mqtt/host_test/sdkconfig.defaults | 6 - 11 files changed, 746 deletions(-) delete mode 100644 components/mqtt/Kconfig delete mode 100644 components/mqtt/host_test/CMakeLists.txt delete mode 100644 components/mqtt/host_test/README.md delete mode 100644 components/mqtt/host_test/main/CMakeLists.txt delete mode 100644 components/mqtt/host_test/main/test_mqtt_client.cpp 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/host_test/mocks/include/sys/queue.h delete mode 100644 components/mqtt/host_test/sdkconfig.defaults diff --git a/components/mqtt/Kconfig b/components/mqtt/Kconfig deleted file mode 100644 index 113bac7..0000000 --- a/components/mqtt/Kconfig +++ /dev/null @@ -1,166 +0,0 @@ -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_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 - help - Enable MQTT transport over SSL with mbedtls - - config MQTT_TRANSPORT_WEBSOCKET - bool "Enable MQTT over Websocket" - default y - depends on WS_TRANSPORT - 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_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 - 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_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_PRIORITY - int "MQTT task priority" - default 5 - depends on MQTT_USE_CUSTOM_CONFIG - 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 - This will enable core selection - - 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. 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]" - 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. - -endmenu diff --git a/components/mqtt/host_test/CMakeLists.txt b/components/mqtt/host_test/CMakeLists.txt deleted file mode 100644 index db98733..0000000 --- a/components/mqtt/host_test/CMakeLists.txt +++ /dev/null @@ -1,17 +0,0 @@ -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/" - "$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/" - ) - -project(host_mqtt_client_test) diff --git a/components/mqtt/host_test/README.md b/components/mqtt/host_test/README.md deleted file mode 100644 index a62087a..0000000 --- a/components/mqtt/host_test/README.md +++ /dev/null @@ -1,30 +0,0 @@ -| Supported Targets | Linux | -| ----------------- | ----- | - -# 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 deleted file mode 100644 index 6c4a9c7..0000000 --- a/components/mqtt/host_test/main/CMakeLists.txt +++ /dev/null @@ -1,3 +0,0 @@ -idf_component_register(SRCS "test_mqtt_client.cpp" - INCLUDE_DIRS "$ENV{IDF_PATH}/tools/catch" - 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 deleted file mode 100644 index 8ebbf1e..0000000 --- a/components/mqtt/host_test/main/test_mqtt_client.cpp +++ /dev/null @@ -1,121 +0,0 @@ -#define CATCH_CONFIG_MAIN // This tells the catch header to generate a main -#include "catch.hpp" - -extern "C" { -#include "Mockesp_event.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" -#include "Mockesp_timer.h" - - /* - * The following functions are not directly called but the generation of them - * from cmock is broken, so we need to define them here. - */ - 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 ESP_OK; - } -} - -#include "mqtt_client.h" - -struct ClientInitializedFixture { - esp_mqtt_client_handle_t client; - ClientInitializedFixture() - { - [[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_timer_get_time_IgnoreAndReturn(0); - xQueueTakeMutexRecursive_IgnoreAndReturn(true); - xQueueGiveMutexRecursive_IgnoreAndReturn(true); - xQueueCreateMutex_ExpectAnyArgsAndReturn( - reinterpret_cast(&mtx)); - xEventGroupCreate_IgnoreAndReturn(reinterpret_cast(&event_group)); - 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 = { - .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); - 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") { - esp_mqtt_client_config_t config{}; - config.broker.address.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_set_config(client, &config); - REQUIRE(res == ESP_OK); - 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 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/host_test/mocks/include/sys/queue.h b/components/mqtt/host_test/mocks/include/sys/queue.h deleted file mode 100644 index 5ec7fec..0000000 --- a/components/mqtt/host_test/mocks/include/sys/queue.h +++ /dev/null @@ -1,66 +0,0 @@ -#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 deleted file mode 100644 index c126429..0000000 --- a/components/mqtt/host_test/sdkconfig.defaults +++ /dev/null @@ -1,6 +0,0 @@ -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 ffff8b9361ac260d79d5a458f908900e2fe48328 Mon Sep 17 00:00:00 2001 From: Linda Date: Wed, 21 Dec 2022 15:56:13 +0800 Subject: [PATCH 161/231] update ESP-MQTT by including the mqtt5 example --- docs/en/api-reference/protocols/mqtt.rst | 4 +++- docs/zh_CN/api-reference/protocols/mqtt.rst | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/docs/en/api-reference/protocols/mqtt.rst b/docs/en/api-reference/protocols/mqtt.rst index ae95fc3..b6e9233 100644 --- a/docs/en/api-reference/protocols/mqtt.rst +++ b/docs/en/api-reference/protocols/mqtt.rst @@ -1,10 +1,11 @@ ESP-MQTT ======== +:link_to_translation:`zh_CN:[中文]` Overview -------- -ESP-MQTT is an implementation of `MQTT `_ protocol client. MQTT is a lightweight publish/subscribe messaging protocol. +ESP-MQTT is an implementation of `MQTT `__ protocol client, which is a lightweight publish/subscribe messaging protocol. Now ESP-MQTT supports `MQTT v5.0 `__. Features @@ -25,6 +26,7 @@ Application Examples * :example:`protocols/mqtt/ssl_psk`: MQTT over TLS using pre-shared keys for authentication, default port 8883 * :example:`protocols/mqtt/ws`: MQTT over WebSocket, default port 80 * :example:`protocols/mqtt/wss`: MQTT over WebSocket Secure, default port 443 + * :example:`protocols/mqtt5`: Uses ESP-MQTT library to connect to broker with MQTT v5.0 MQTT Message Retransmission --------------------------- diff --git a/docs/zh_CN/api-reference/protocols/mqtt.rst b/docs/zh_CN/api-reference/protocols/mqtt.rst index d164a86..b3e5cfc 100644 --- a/docs/zh_CN/api-reference/protocols/mqtt.rst +++ b/docs/zh_CN/api-reference/protocols/mqtt.rst @@ -1,10 +1,11 @@ ESP-MQTT ======== +:link_to_translation:`en:[English]` 概述 -------- -ESP-MQTT 是 `MQTT `_ 协议客户端的实现。MQTT 是一种基于发布/订阅模式的轻量级消息传输协议。 +ESP-MQTT 是 `MQTT `__ 协议客户端的实现,MQTT 是一种基于发布/订阅模式的轻量级消息传输协议。ESP-MQTT 当前支持 `MQTT v5.0 `__。 特性 @@ -25,6 +26,7 @@ ESP-MQTT 是 `MQTT `_ 协议客户端的实现。MQTT 是一 * :example:`protocols/mqtt/ssl_psk`:基于 TLS 的 MQTT,使用预共享密钥进行身份验证,默认端口 8883 * :example:`protocols/mqtt/ws`:基于 WebSocket 的 MQTT,默认端口 80 * :example:`protocols/mqtt/wss`:基于 WebSocket Secure 的 MQTT,默认端口 443 + * :example:`protocols/mqtt5`: 使用 ESP-MQTT 库连接 MQTT v5.0 的服务器 MQTT 消息重传 -------------------------- From 8f90dab8d80273c50e6acb61e012770ad015bdc7 Mon Sep 17 00:00:00 2001 From: Chen Yudong Date: Mon, 9 Jan 2023 20:58:27 +0800 Subject: [PATCH 162/231] examples: enable build esp32c6 for wifi and network examples --- examples/protocols/mqtt/ssl/README.md | 4 ++-- examples/protocols/mqtt/ssl_ds/main/CMakeLists.txt | 3 +-- examples/protocols/mqtt/ssl_mutual_auth/README.md | 4 ++-- examples/protocols/mqtt/ssl_psk/README.md | 4 ++-- examples/protocols/mqtt/tcp/README.md | 4 ++-- examples/protocols/mqtt/ws/README.md | 4 ++-- examples/protocols/mqtt/wss/README.md | 4 ++-- examples/protocols/mqtt5/README.md | 4 ++-- 8 files changed, 15 insertions(+), 16 deletions(-) diff --git a/examples/protocols/mqtt/ssl/README.md b/examples/protocols/mqtt/ssl/README.md index eb53ced..d597c6b 100644 --- a/examples/protocols/mqtt/ssl/README.md +++ b/examples/protocols/mqtt/ssl/README.md @@ -1,5 +1,5 @@ -| Supported Targets | ESP32 | ESP32-C3 | ESP32-S2 | ESP32-S3 | -| ----------------- | ----- | -------- | -------- | -------- | +| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C6 | ESP32-S2 | ESP32-S3 | +| ----------------- | ----- | -------- | -------- | -------- | -------- | -------- | # ESP-MQTT SSL Sample application diff --git a/examples/protocols/mqtt/ssl_ds/main/CMakeLists.txt b/examples/protocols/mqtt/ssl_ds/main/CMakeLists.txt index 7a48b53..c31750a 100644 --- a/examples/protocols/mqtt/ssl_ds/main/CMakeLists.txt +++ b/examples/protocols/mqtt/ssl_ds/main/CMakeLists.txt @@ -1,4 +1,3 @@ idf_component_register(SRCS "app_main.c" - INCLUDE_DIRS "." - REQUIRED_IDF_TARGETS esp32s2 esp32c3 esp32s3) + INCLUDE_DIRS ".") target_compile_options(${COMPONENT_LIB} PRIVATE "-Wno-format") diff --git a/examples/protocols/mqtt/ssl_mutual_auth/README.md b/examples/protocols/mqtt/ssl_mutual_auth/README.md index 9274aa4..f50c449 100644 --- a/examples/protocols/mqtt/ssl_mutual_auth/README.md +++ b/examples/protocols/mqtt/ssl_mutual_auth/README.md @@ -1,5 +1,5 @@ -| Supported Targets | ESP32 | ESP32-C3 | ESP32-S2 | ESP32-S3 | -| ----------------- | ----- | -------- | -------- | -------- | +| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C6 | ESP32-S2 | ESP32-S3 | +| ----------------- | ----- | -------- | -------- | -------- | -------- | -------- | # ESP-MQTT SSL Sample application (mutual authentication) diff --git a/examples/protocols/mqtt/ssl_psk/README.md b/examples/protocols/mqtt/ssl_psk/README.md index 3029ccd..9df73be 100644 --- a/examples/protocols/mqtt/ssl_psk/README.md +++ b/examples/protocols/mqtt/ssl_psk/README.md @@ -1,5 +1,5 @@ -| Supported Targets | ESP32 | ESP32-C3 | ESP32-S2 | ESP32-S3 | -| ----------------- | ----- | -------- | -------- | -------- | +| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C6 | ESP32-S2 | ESP32-S3 | +| ----------------- | ----- | -------- | -------- | -------- | -------- | -------- | # ESP-MQTT SSL example with PSK verification diff --git a/examples/protocols/mqtt/tcp/README.md b/examples/protocols/mqtt/tcp/README.md index 0589091..63c0f0f 100644 --- a/examples/protocols/mqtt/tcp/README.md +++ b/examples/protocols/mqtt/tcp/README.md @@ -1,5 +1,5 @@ -| Supported Targets | ESP32 | ESP32-C3 | ESP32-S2 | ESP32-S3 | -| ----------------- | ----- | -------- | -------- | -------- | +| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C6 | ESP32-S2 | ESP32-S3 | +| ----------------- | ----- | -------- | -------- | -------- | -------- | -------- | # ESP-MQTT sample application (See the README.md file in the upper level 'examples' directory for more information about examples.) diff --git a/examples/protocols/mqtt/ws/README.md b/examples/protocols/mqtt/ws/README.md index a5f36c9..5209b0d 100644 --- a/examples/protocols/mqtt/ws/README.md +++ b/examples/protocols/mqtt/ws/README.md @@ -1,5 +1,5 @@ -| Supported Targets | ESP32 | ESP32-C3 | ESP32-S2 | ESP32-S3 | -| ----------------- | ----- | -------- | -------- | -------- | +| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C6 | ESP32-S2 | ESP32-S3 | +| ----------------- | ----- | -------- | -------- | -------- | -------- | -------- | # ESP-MQTT MQTT over Websocket diff --git a/examples/protocols/mqtt/wss/README.md b/examples/protocols/mqtt/wss/README.md index ca2e98e..94a2e02 100644 --- a/examples/protocols/mqtt/wss/README.md +++ b/examples/protocols/mqtt/wss/README.md @@ -1,5 +1,5 @@ -| Supported Targets | ESP32 | ESP32-C3 | ESP32-S2 | ESP32-S3 | -| ----------------- | ----- | -------- | -------- | -------- | +| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C6 | ESP32-S2 | ESP32-S3 | +| ----------------- | ----- | -------- | -------- | -------- | -------- | -------- | # ESP-MQTT MQTT over WSS Sample application (See the README.md file in the upper level 'examples' directory for more information about examples.) diff --git a/examples/protocols/mqtt5/README.md b/examples/protocols/mqtt5/README.md index 1ac3eaf..afe65ad 100644 --- a/examples/protocols/mqtt5/README.md +++ b/examples/protocols/mqtt5/README.md @@ -1,5 +1,5 @@ -| Supported Targets | ESP32 | ESP32-C3 | ESP32-S2 | ESP32-S3 | -| ----------------- | ----- | -------- | -------- | -------- | +| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C6 | ESP32-S2 | ESP32-S3 | +| ----------------- | ----- | -------- | -------- | -------- | -------- | -------- | # ESP-MQTT sample application (See the README.md file in the upper level 'examples' directory for more information about examples.) From 22e548f3ab0f6fbc414c2c5baa48bd32ffe6f9d5 Mon Sep 17 00:00:00 2001 From: Cao Sen Miao Date: Tue, 10 Jan 2023 13:59:46 +0800 Subject: [PATCH 163/231] esp32h2: add build test --- components/mqtt/test/test_mqtt5.c | 6 +++--- tools/test_apps/protocols/mqtt/build_test/README.md | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/components/mqtt/test/test_mqtt5.c b/components/mqtt/test/test_mqtt5.c index 6b05275..d9bfe82 100644 --- a/components/mqtt/test/test_mqtt5.c +++ b/components/mqtt/test/test_mqtt5.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -19,7 +19,7 @@ #include "esp_mac.h" #include "esp_partition.h" -#if !TEMPORARY_DISABLED_FOR_TARGETS(ESP32C6) +#if !TEMPORARY_DISABLED_FOR_TARGETS(ESP32C6, ESP32H2) static esp_mqtt5_user_property_item_t user_property_arr[3] = { {"board", "esp32"}, {"u", "user"}, @@ -151,4 +151,4 @@ TEST_CASE("mqtt5 broker tests", "[mqtt5][test_env=UT_T2_Ethernet]") connect_test_fixture_teardown(); } #endif // SOC_EMAC_SUPPORTED -#endif //!TEMPORARY_DISABLED_FOR_TARGETS(ESP32C6) +#endif //!TEMPORARY_DISABLED_FOR_TARGETS(ESP32C6, ESP32H2) diff --git a/tools/test_apps/protocols/mqtt/build_test/README.md b/tools/test_apps/protocols/mqtt/build_test/README.md index 2a2585d..9a4c5c5 100644 --- a/tools/test_apps/protocols/mqtt/build_test/README.md +++ b/tools/test_apps/protocols/mqtt/build_test/README.md @@ -1,5 +1,5 @@ -| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C6 | ESP32-S2 | ESP32-S3 | -| ----------------- | ----- | -------- | -------- | -------- | -------- | -------- | +| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C6 | ESP32-H2 | ESP32-S2 | ESP32-S3 | +| ----------------- | ----- | -------- | -------- | -------- | -------- | -------- | -------- | # Build only test for C++ From 15805501f7ee57ff5e325cd2cbb3e6ba56f56145 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Thu, 27 Oct 2022 19:07:07 +0200 Subject: [PATCH 164/231] esp_netif/lwip: Fix core-locking config * Fix thread safety issues in non-core locking * Add option to verify thread safety issues in lwip (core-lock assertion) * Make esp_sntp.h thread safe API * Fix sntp examples * Fix openthread libs Closes https://github.com/espressif/esp-idf/issues/9908 Closes https://github.com/espressif/esp-idf/issues/10502 Closes https://github.com/espressif/esp-idf/issues/10466 --- examples/protocols/mqtt/ssl/sdkconfig.ci | 1 + examples/protocols/mqtt/tcp/sdkconfig.ci | 2 ++ examples/protocols/mqtt/ws/sdkconfig.ci | 1 + examples/protocols/mqtt/wss/sdkconfig.ci | 2 ++ 4 files changed, 6 insertions(+) diff --git a/examples/protocols/mqtt/ssl/sdkconfig.ci b/examples/protocols/mqtt/ssl/sdkconfig.ci index 3271d96..e8d4a52 100644 --- a/examples/protocols/mqtt/ssl/sdkconfig.ci +++ b/examples/protocols/mqtt/ssl/sdkconfig.ci @@ -19,3 +19,4 @@ CONFIG_EXAMPLE_ETH_MDIO_GPIO=18 CONFIG_EXAMPLE_ETH_PHY_RST_GPIO=5 CONFIG_EXAMPLE_ETH_PHY_ADDR=1 CONFIG_EXAMPLE_CONNECT_IPV6=y +CONFIG_LWIP_CHECK_THREAD_SAFETY=y diff --git a/examples/protocols/mqtt/tcp/sdkconfig.ci b/examples/protocols/mqtt/tcp/sdkconfig.ci index 28ce9ac..acf084a 100644 --- a/examples/protocols/mqtt/tcp/sdkconfig.ci +++ b/examples/protocols/mqtt/tcp/sdkconfig.ci @@ -9,3 +9,5 @@ CONFIG_EXAMPLE_ETH_MDIO_GPIO=18 CONFIG_EXAMPLE_ETH_PHY_RST_GPIO=5 CONFIG_EXAMPLE_ETH_PHY_ADDR=1 CONFIG_EXAMPLE_CONNECT_IPV6=y +CONFIG_LWIP_TCPIP_CORE_LOCKING=y +CONFIG_LWIP_CHECK_THREAD_SAFETY=y diff --git a/examples/protocols/mqtt/ws/sdkconfig.ci b/examples/protocols/mqtt/ws/sdkconfig.ci index 2f8e425..eebcafd 100644 --- a/examples/protocols/mqtt/ws/sdkconfig.ci +++ b/examples/protocols/mqtt/ws/sdkconfig.ci @@ -8,3 +8,4 @@ CONFIG_EXAMPLE_ETH_MDIO_GPIO=18 CONFIG_EXAMPLE_ETH_PHY_RST_GPIO=5 CONFIG_EXAMPLE_ETH_PHY_ADDR=1 CONFIG_EXAMPLE_CONNECT_IPV6=y +CONFIG_LWIP_CHECK_THREAD_SAFETY=y diff --git a/examples/protocols/mqtt/wss/sdkconfig.ci b/examples/protocols/mqtt/wss/sdkconfig.ci index f355614..c8e8ef9 100644 --- a/examples/protocols/mqtt/wss/sdkconfig.ci +++ b/examples/protocols/mqtt/wss/sdkconfig.ci @@ -9,3 +9,5 @@ CONFIG_EXAMPLE_ETH_MDIO_GPIO=18 CONFIG_EXAMPLE_ETH_PHY_RST_GPIO=5 CONFIG_EXAMPLE_ETH_PHY_ADDR=1 CONFIG_EXAMPLE_CONNECT_IPV6=y +CONFIG_LWIP_TCPIP_CORE_LOCKING=y +CONFIG_LWIP_CHECK_THREAD_SAFETY=y From 1253a8995aa9858dcba38d46c4d298483ef5712d Mon Sep 17 00:00:00 2001 From: Laukik Hase Date: Thu, 19 Jan 2023 10:00:00 +0530 Subject: [PATCH 165/231] ci: Enable build stage for `mqtt/ssl_ds` example for esp32c6 --- examples/protocols/mqtt/ssl_ds/README.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/examples/protocols/mqtt/ssl_ds/README.md b/examples/protocols/mqtt/ssl_ds/README.md index 6a321bd..7658f5b 100644 --- a/examples/protocols/mqtt/ssl_ds/README.md +++ b/examples/protocols/mqtt/ssl_ds/README.md @@ -1,10 +1,10 @@ -| Supported Targets | ESP32-C3 | ESP32-S2 | ESP32-S3 | -| ----------------- | -------- | -------- | -------- | +| Supported Targets | ESP32-C3 | ESP32-C6 | ESP32-S2 | ESP32-S3 | +| ----------------- | -------- | -------- | -------- | -------- | # ESP-MQTT SSL Mutual Authentication with Digital Signature (See the README.md file in the upper level 'examples' directory for more information about examples.) -Espressif's ESP32-S2, ESP32-S3 and ESP32-C3 MCU have a built-in Digital Signature (DS) Peripheral, which provides hardware acceleration for RSA signature. More details can be found at [Digital Signature with ESP-TLS](https://docs.espressif.com/projects/esp-idf/en/latest/esp32s2/api-reference/protocols/esp_tls.html#digital-signature-with-esp-tls). +Espressif's ESP32-S2, ESP32-S3, ESP32-C3 and ESP32-C6 MCU have a built-in Digital Signature (DS) Peripheral, which provides hardware acceleration for RSA signature. More details can be found at [Digital Signature with ESP-TLS](https://docs.espressif.com/projects/esp-idf/en/latest/esp32s2/api-reference/protocols/esp_tls.html#digital-signature-with-esp-tls). This example connects to the broker test.mosquitto.org using ssl transport with client certificate(RSA) and as a demonstration subscribes/unsubscribes and sends a message on certain topic.The RSA signature operation required in the ssl connection is performed with help of the Digital Signature (DS) peripheral. (Please note that the public broker is maintained by the community so may not be always available, for details please visit http://test.mosquitto.org) @@ -14,12 +14,12 @@ It uses ESP-MQTT library which implements mqtt client to connect to mqtt broker. ### Hardware Required -This example can be executed on any ESP32-S2, ESP32-S3, ESP32-C3 board (which has a built-in DS peripheral), the only required interface is WiFi and connection to internet. +This example can be executed on any ESP32-S2, ESP32-S3, ESP32-C3 or ESP32-C6 board (which has a built-in DS peripheral), the only required interface is WiFi and connection to internet. ### Configure the project #### 1) Selecting the target -As the project is to be built for the target ESP32-S2, ESP32-S3, ESP32-C3 it should be selected with the following command +As the project is to be built for the target ESP32-S2, ESP32-S3, ESP32-C3 or ESP32-C6 it should be selected with the following command ``` idf.py set-target /* target */ ``` From 7aaed493fc5ba9319f6a6fb6e2b0204240955a60 Mon Sep 17 00:00:00 2001 From: Mahavir Jain Date: Fri, 10 Feb 2023 10:30:45 +0530 Subject: [PATCH 166/231] mqtt/ssl_ds: enable the example for ESP32-H2 target --- examples/protocols/mqtt/ssl_ds/README.md | 8 ++++---- examples/protocols/mqtt/ssl_ds/main/app_main.c | 1 - examples/protocols/mqtt/ssl_ds/sdkconfig.defaults.esp32h2 | 2 ++ 3 files changed, 6 insertions(+), 5 deletions(-) create mode 100644 examples/protocols/mqtt/ssl_ds/sdkconfig.defaults.esp32h2 diff --git a/examples/protocols/mqtt/ssl_ds/README.md b/examples/protocols/mqtt/ssl_ds/README.md index 7658f5b..cd0486b 100644 --- a/examples/protocols/mqtt/ssl_ds/README.md +++ b/examples/protocols/mqtt/ssl_ds/README.md @@ -1,10 +1,10 @@ -| Supported Targets | ESP32-C3 | ESP32-C6 | ESP32-S2 | ESP32-S3 | -| ----------------- | -------- | -------- | -------- | -------- | +| Supported Targets | ESP32-C3 | ESP32-C6 | ESP32-H2 | ESP32-S2 | ESP32-S3 | +| ----------------- | -------- | -------- | -------- | -------- | -------- | # ESP-MQTT SSL Mutual Authentication with Digital Signature (See the README.md file in the upper level 'examples' directory for more information about examples.) -Espressif's ESP32-S2, ESP32-S3, ESP32-C3 and ESP32-C6 MCU have a built-in Digital Signature (DS) Peripheral, which provides hardware acceleration for RSA signature. More details can be found at [Digital Signature with ESP-TLS](https://docs.espressif.com/projects/esp-idf/en/latest/esp32s2/api-reference/protocols/esp_tls.html#digital-signature-with-esp-tls). +Espressif's ESP32-S2, ESP32-S3, ESP32-C3, ESP32-C6 and ESP32-H2 MCU have a built-in Digital Signature (DS) Peripheral, which provides hardware acceleration for RSA signature. More details can be found at [Digital Signature with ESP-TLS](https://docs.espressif.com/projects/esp-idf/en/latest/esp32s2/api-reference/protocols/esp_tls.html#digital-signature-with-esp-tls). This example connects to the broker test.mosquitto.org using ssl transport with client certificate(RSA) and as a demonstration subscribes/unsubscribes and sends a message on certain topic.The RSA signature operation required in the ssl connection is performed with help of the Digital Signature (DS) peripheral. (Please note that the public broker is maintained by the community so may not be always available, for details please visit http://test.mosquitto.org) @@ -19,7 +19,7 @@ This example can be executed on any ESP32-S2, ESP32-S3, ESP32-C3 or ESP32-C6 boa ### Configure the project #### 1) Selecting the target -As the project is to be built for the target ESP32-S2, ESP32-S3, ESP32-C3 or ESP32-C6 it should be selected with the following command +As the project is to be built for the target ESP32-S2, ESP32-S3, ESP32-C3, ESP32-C6 or ESP32-H2 it should be selected with the following command ``` idf.py set-target /* target */ ``` diff --git a/examples/protocols/mqtt/ssl_ds/main/app_main.c b/examples/protocols/mqtt/ssl_ds/main/app_main.c index ba94cf0..709143c 100644 --- a/examples/protocols/mqtt/ssl_ds/main/app_main.c +++ b/examples/protocols/mqtt/ssl_ds/main/app_main.c @@ -10,7 +10,6 @@ #include #include #include -#include "esp_wifi.h" #include "esp_system.h" #include "nvs_flash.h" #include "esp_event.h" diff --git a/examples/protocols/mqtt/ssl_ds/sdkconfig.defaults.esp32h2 b/examples/protocols/mqtt/ssl_ds/sdkconfig.defaults.esp32h2 new file mode 100644 index 0000000..dcbc3a5 --- /dev/null +++ b/examples/protocols/mqtt/ssl_ds/sdkconfig.defaults.esp32h2 @@ -0,0 +1,2 @@ +CONFIG_EXAMPLE_CONNECT_WIFI=n +CONFIG_EXAMPLE_CONNECT_ETHERNET=y From ecfe26da47c8b9274a7467914f5ce6043d22cd44 Mon Sep 17 00:00:00 2001 From: Bogdan Kolendovskyy Date: Thu, 15 Dec 2022 16:15:39 +0100 Subject: [PATCH 167/231] examples: fix format errors, remove -Wno-format Logging in series of examples has misuse of "%d" instead of type-appropriate format specifiers. Fix by changing "%d" to PRIxx macros corresponding to type. Remove -Wno-format compile flag in those examples that are affected. --- examples/protocols/mqtt/ssl/main/CMakeLists.txt | 1 - examples/protocols/mqtt/ssl/main/app_main.c | 6 +++--- examples/protocols/mqtt/ssl_ds/main/CMakeLists.txt | 4 ++-- examples/protocols/mqtt/ssl_ds/main/app_main.c | 6 +++--- .../protocols/mqtt/ssl_mutual_auth/main/CMakeLists.txt | 1 - examples/protocols/mqtt/ssl_mutual_auth/main/app_main.c | 6 +++--- examples/protocols/mqtt/ssl_psk/main/CMakeLists.txt | 1 - examples/protocols/mqtt/ssl_psk/main/app_main.c | 6 +++--- examples/protocols/mqtt/tcp/main/CMakeLists.txt | 1 - examples/protocols/mqtt/tcp/main/app_main.c | 4 ++-- examples/protocols/mqtt/ws/main/CMakeLists.txt | 1 - examples/protocols/mqtt/ws/main/app_main.c | 4 ++-- examples/protocols/mqtt/wss/main/CMakeLists.txt | 1 - examples/protocols/mqtt/wss/main/app_main.c | 6 +++--- examples/protocols/mqtt5/main/CMakeLists.txt | 1 - examples/protocols/mqtt5/main/app_main.c | 9 +++++---- 16 files changed, 26 insertions(+), 32 deletions(-) diff --git a/examples/protocols/mqtt/ssl/main/CMakeLists.txt b/examples/protocols/mqtt/ssl/main/CMakeLists.txt index c31750a..61fac40 100644 --- a/examples/protocols/mqtt/ssl/main/CMakeLists.txt +++ b/examples/protocols/mqtt/ssl/main/CMakeLists.txt @@ -1,3 +1,2 @@ idf_component_register(SRCS "app_main.c" INCLUDE_DIRS ".") -target_compile_options(${COMPONENT_LIB} PRIVATE "-Wno-format") diff --git a/examples/protocols/mqtt/ssl/main/app_main.c b/examples/protocols/mqtt/ssl/main/app_main.c index acfcb3f..570bdd3 100644 --- a/examples/protocols/mqtt/ssl/main/app_main.c +++ b/examples/protocols/mqtt/ssl/main/app_main.c @@ -62,7 +62,7 @@ static void send_binary(esp_mqtt_client_handle_t client) */ static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data) { - ESP_LOGD(TAG, "Event dispatched from event loop base=%s, event_id=%d", base, event_id); + ESP_LOGD(TAG, "Event dispatched from event loop base=%s, event_id=%" PRIi32, base, event_id); esp_mqtt_event_handle_t event = event_data; esp_mqtt_client_handle_t client = event->client; int msg_id; @@ -130,7 +130,7 @@ static void mqtt_app_start(void) }, }; - ESP_LOGI(TAG, "[APP] Free memory: %d bytes", esp_get_free_heap_size()); + ESP_LOGI(TAG, "[APP] Free memory: %" PRIu32 " bytes", esp_get_free_heap_size()); esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg); /* The last argument may be used to pass data to the event handler, in this example mqtt_event_handler */ esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, NULL); @@ -140,7 +140,7 @@ static void mqtt_app_start(void) void app_main(void) { ESP_LOGI(TAG, "[APP] Startup.."); - ESP_LOGI(TAG, "[APP] Free memory: %d bytes", esp_get_free_heap_size()); + ESP_LOGI(TAG, "[APP] Free memory: %" PRIu32 " bytes", esp_get_free_heap_size()); ESP_LOGI(TAG, "[APP] IDF version: %s", esp_get_idf_version()); esp_log_level_set("*", ESP_LOG_INFO); diff --git a/examples/protocols/mqtt/ssl_ds/main/CMakeLists.txt b/examples/protocols/mqtt/ssl_ds/main/CMakeLists.txt index c31750a..b8ef6ea 100644 --- a/examples/protocols/mqtt/ssl_ds/main/CMakeLists.txt +++ b/examples/protocols/mqtt/ssl_ds/main/CMakeLists.txt @@ -1,3 +1,3 @@ idf_component_register(SRCS "app_main.c" - INCLUDE_DIRS ".") -target_compile_options(${COMPONENT_LIB} PRIVATE "-Wno-format") + INCLUDE_DIRS "." + REQUIRED_IDF_TARGETS esp32s2 esp32c3 esp32s3) diff --git a/examples/protocols/mqtt/ssl_ds/main/app_main.c b/examples/protocols/mqtt/ssl_ds/main/app_main.c index 709143c..160392a 100644 --- a/examples/protocols/mqtt/ssl_ds/main/app_main.c +++ b/examples/protocols/mqtt/ssl_ds/main/app_main.c @@ -47,7 +47,7 @@ extern const uint8_t server_cert_pem_end[] asm("_binary_mosquitto_org_crt_end"); */ static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data) { - ESP_LOGD(TAG, "Event dispatched from event loop base=%s, event_id=%d", base, event_id); + ESP_LOGD(TAG, "Event dispatched from event loop base=%s, event_id=%" PRIi32, base, event_id); esp_mqtt_event_handle_t event = event_data; esp_mqtt_client_handle_t client = event->client; int msg_id; @@ -124,7 +124,7 @@ static void mqtt_app_start(void) }, }; - ESP_LOGI(TAG, "[APP] Free memory: %d bytes", esp_get_free_heap_size()); + ESP_LOGI(TAG, "[APP] Free memory: %" PRIu32 " bytes", esp_get_free_heap_size()); esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg); esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, NULL); esp_mqtt_client_start(client); @@ -133,7 +133,7 @@ static void mqtt_app_start(void) void app_main(void) { ESP_LOGI(TAG, "[APP] Startup.."); - ESP_LOGI(TAG, "[APP] Free memory: %d bytes", esp_get_free_heap_size()); + ESP_LOGI(TAG, "[APP] Free memory: %" PRIu32 " bytes", esp_get_free_heap_size()); ESP_LOGI(TAG, "[APP] IDF version: %s", esp_get_idf_version()); esp_log_level_set("*", ESP_LOG_INFO); diff --git a/examples/protocols/mqtt/ssl_mutual_auth/main/CMakeLists.txt b/examples/protocols/mqtt/ssl_mutual_auth/main/CMakeLists.txt index c31750a..61fac40 100644 --- a/examples/protocols/mqtt/ssl_mutual_auth/main/CMakeLists.txt +++ b/examples/protocols/mqtt/ssl_mutual_auth/main/CMakeLists.txt @@ -1,3 +1,2 @@ idf_component_register(SRCS "app_main.c" INCLUDE_DIRS ".") -target_compile_options(${COMPONENT_LIB} PRIVATE "-Wno-format") diff --git a/examples/protocols/mqtt/ssl_mutual_auth/main/app_main.c b/examples/protocols/mqtt/ssl_mutual_auth/main/app_main.c index 76cc455..9798cb9 100644 --- a/examples/protocols/mqtt/ssl_mutual_auth/main/app_main.c +++ b/examples/protocols/mqtt/ssl_mutual_auth/main/app_main.c @@ -57,7 +57,7 @@ static void log_error_if_nonzero(const char *message, int error_code) */ static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data) { - ESP_LOGD(TAG, "Event dispatched from event loop base=%s, event_id=%d", base, event_id); + ESP_LOGD(TAG, "Event dispatched from event loop base=%s, event_id=%" PRIi32, base, event_id); esp_mqtt_event_handle_t event = event_data; esp_mqtt_client_handle_t client = event->client; int msg_id; @@ -122,7 +122,7 @@ static void mqtt_app_start(void) } }; - ESP_LOGI(TAG, "[APP] Free memory: %d bytes", esp_get_free_heap_size()); + ESP_LOGI(TAG, "[APP] Free memory: %" PRIu32 " bytes", esp_get_free_heap_size()); esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg); /* The last argument may be used to pass data to the event handler, in this example mqtt_event_handler */ esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, NULL); @@ -132,7 +132,7 @@ static void mqtt_app_start(void) void app_main(void) { ESP_LOGI(TAG, "[APP] Startup.."); - ESP_LOGI(TAG, "[APP] Free memory: %d bytes", esp_get_free_heap_size()); + ESP_LOGI(TAG, "[APP] Free memory: %" PRIu32 " bytes", esp_get_free_heap_size()); ESP_LOGI(TAG, "[APP] IDF version: %s", esp_get_idf_version()); esp_log_level_set("*", ESP_LOG_INFO); diff --git a/examples/protocols/mqtt/ssl_psk/main/CMakeLists.txt b/examples/protocols/mqtt/ssl_psk/main/CMakeLists.txt index c31750a..61fac40 100644 --- a/examples/protocols/mqtt/ssl_psk/main/CMakeLists.txt +++ b/examples/protocols/mqtt/ssl_psk/main/CMakeLists.txt @@ -1,3 +1,2 @@ idf_component_register(SRCS "app_main.c" INCLUDE_DIRS ".") -target_compile_options(${COMPONENT_LIB} PRIVATE "-Wno-format") diff --git a/examples/protocols/mqtt/ssl_psk/main/app_main.c b/examples/protocols/mqtt/ssl_psk/main/app_main.c index f024e13..67df863 100644 --- a/examples/protocols/mqtt/ssl_psk/main/app_main.c +++ b/examples/protocols/mqtt/ssl_psk/main/app_main.c @@ -64,7 +64,7 @@ static const psk_hint_key_t psk_hint_key = { */ static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data) { - ESP_LOGD(TAG, "Event dispatched from event loop base=%s, event_id=%d", base, event_id); + ESP_LOGD(TAG, "Event dispatched from event loop base=%s, event_id=%" PRIi32 "", base, event_id); esp_mqtt_event_handle_t event = event_data; esp_mqtt_client_handle_t client = event->client; int msg_id; @@ -117,7 +117,7 @@ static void mqtt_app_start(void) .broker.verification.psk_hint_key = &psk_hint_key, }; - ESP_LOGI(TAG, "[APP] Free memory: %d bytes", esp_get_free_heap_size()); + ESP_LOGI(TAG, "[APP] Free memory: %" PRIu32 " bytes", esp_get_free_heap_size()); esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg); /* The last argument may be used to pass data to the event handler, in this example mqtt_event_handler */ esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, NULL); @@ -127,7 +127,7 @@ static void mqtt_app_start(void) void app_main(void) { ESP_LOGI(TAG, "[APP] Startup.."); - ESP_LOGI(TAG, "[APP] Free memory: %d bytes", esp_get_free_heap_size()); + ESP_LOGI(TAG, "[APP] Free memory: %" PRIu32 " bytes", esp_get_free_heap_size()); ESP_LOGI(TAG, "[APP] IDF version: %s", esp_get_idf_version()); esp_log_level_set("*", ESP_LOG_INFO); diff --git a/examples/protocols/mqtt/tcp/main/CMakeLists.txt b/examples/protocols/mqtt/tcp/main/CMakeLists.txt index c31750a..61fac40 100644 --- a/examples/protocols/mqtt/tcp/main/CMakeLists.txt +++ b/examples/protocols/mqtt/tcp/main/CMakeLists.txt @@ -1,3 +1,2 @@ idf_component_register(SRCS "app_main.c" INCLUDE_DIRS ".") -target_compile_options(${COMPONENT_LIB} PRIVATE "-Wno-format") diff --git a/examples/protocols/mqtt/tcp/main/app_main.c b/examples/protocols/mqtt/tcp/main/app_main.c index f405812..06ff1ad 100644 --- a/examples/protocols/mqtt/tcp/main/app_main.c +++ b/examples/protocols/mqtt/tcp/main/app_main.c @@ -52,7 +52,7 @@ static void log_error_if_nonzero(const char *message, int error_code) */ static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data) { - ESP_LOGD(TAG, "Event dispatched from event loop base=%s, event_id=%d", base, event_id); + ESP_LOGD(TAG, "Event dispatched from event loop base=%s, event_id=%" PRIi32 "", base, event_id); esp_mqtt_event_handle_t event = event_data; esp_mqtt_client_handle_t client = event->client; int msg_id; @@ -146,7 +146,7 @@ static void mqtt_app_start(void) void app_main(void) { ESP_LOGI(TAG, "[APP] Startup.."); - ESP_LOGI(TAG, "[APP] Free memory: %d bytes", esp_get_free_heap_size()); + ESP_LOGI(TAG, "[APP] Free memory: %" PRIu32 " bytes", esp_get_free_heap_size()); ESP_LOGI(TAG, "[APP] IDF version: %s", esp_get_idf_version()); esp_log_level_set("*", ESP_LOG_INFO); diff --git a/examples/protocols/mqtt/ws/main/CMakeLists.txt b/examples/protocols/mqtt/ws/main/CMakeLists.txt index c31750a..61fac40 100644 --- a/examples/protocols/mqtt/ws/main/CMakeLists.txt +++ b/examples/protocols/mqtt/ws/main/CMakeLists.txt @@ -1,3 +1,2 @@ idf_component_register(SRCS "app_main.c" INCLUDE_DIRS ".") -target_compile_options(${COMPONENT_LIB} PRIVATE "-Wno-format") diff --git a/examples/protocols/mqtt/ws/main/app_main.c b/examples/protocols/mqtt/ws/main/app_main.c index 247f305..59f09cf 100644 --- a/examples/protocols/mqtt/ws/main/app_main.c +++ b/examples/protocols/mqtt/ws/main/app_main.c @@ -50,7 +50,7 @@ static void log_error_if_nonzero(const char *message, int error_code) */ static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data) { - ESP_LOGD(TAG, "Event dispatched from event loop base=%s, event_id=%d", base, event_id); + ESP_LOGD(TAG, "Event dispatched from event loop base=%s, event_id=%" PRIi32, base, event_id); esp_mqtt_event_handle_t event = event_data; esp_mqtt_client_handle_t client = event->client; int msg_id; @@ -119,7 +119,7 @@ static void mqtt_app_start(void) void app_main(void) { ESP_LOGI(TAG, "[APP] Startup.."); - ESP_LOGI(TAG, "[APP] Free memory: %d bytes", esp_get_free_heap_size()); + ESP_LOGI(TAG, "[APP] Free memory: %" PRIu32 " bytes", esp_get_free_heap_size()); ESP_LOGI(TAG, "[APP] IDF version: %s", esp_get_idf_version()); esp_log_level_set("*", ESP_LOG_INFO); diff --git a/examples/protocols/mqtt/wss/main/CMakeLists.txt b/examples/protocols/mqtt/wss/main/CMakeLists.txt index c31750a..61fac40 100644 --- a/examples/protocols/mqtt/wss/main/CMakeLists.txt +++ b/examples/protocols/mqtt/wss/main/CMakeLists.txt @@ -1,3 +1,2 @@ idf_component_register(SRCS "app_main.c" INCLUDE_DIRS ".") -target_compile_options(${COMPONENT_LIB} PRIVATE "-Wno-format") diff --git a/examples/protocols/mqtt/wss/main/app_main.c b/examples/protocols/mqtt/wss/main/app_main.c index 0e570b1..f913051 100644 --- a/examples/protocols/mqtt/wss/main/app_main.c +++ b/examples/protocols/mqtt/wss/main/app_main.c @@ -89,7 +89,7 @@ static esp_err_t mqtt_event_handler_cb(esp_mqtt_event_handle_t event) static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data) { /* The argument passed to esp_mqtt_client_register_event can de accessed as handler_args*/ - ESP_LOGD(TAG, "Event dispatched from event loop base=%s, event_id=%d", base, event_id); + ESP_LOGD(TAG, "Event dispatched from event loop base=%s, event_id=%" PRIi32, base, event_id); mqtt_event_handler_cb(event_data); } @@ -100,7 +100,7 @@ static void mqtt_app_start(void) .broker.verification.certificate = (const char *)mqtt_eclipseprojects_io_pem_start, }; - ESP_LOGI(TAG, "[APP] Free memory: %d bytes", esp_get_free_heap_size()); + ESP_LOGI(TAG, "[APP] Free memory: %" PRIu32 " bytes", esp_get_free_heap_size()); esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg); /* The last argument may be used to pass data to the event handler, in this example mqtt_event_handler */ esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, NULL); @@ -111,7 +111,7 @@ static void mqtt_app_start(void) void app_main(void) { ESP_LOGI(TAG, "[APP] Startup.."); - ESP_LOGI(TAG, "[APP] Free memory: %d bytes", esp_get_free_heap_size()); + ESP_LOGI(TAG, "[APP] Free memory: %" PRIu32 " bytes", esp_get_free_heap_size()); ESP_LOGI(TAG, "[APP] IDF version: %s", esp_get_idf_version()); esp_log_level_set("*", ESP_LOG_INFO); diff --git a/examples/protocols/mqtt5/main/CMakeLists.txt b/examples/protocols/mqtt5/main/CMakeLists.txt index c31750a..61fac40 100644 --- a/examples/protocols/mqtt5/main/CMakeLists.txt +++ b/examples/protocols/mqtt5/main/CMakeLists.txt @@ -1,3 +1,2 @@ idf_component_register(SRCS "app_main.c" INCLUDE_DIRS ".") -target_compile_options(${COMPONENT_LIB} PRIVATE "-Wno-format") diff --git a/examples/protocols/mqtt5/main/app_main.c b/examples/protocols/mqtt5/main/app_main.c index a0f7ee8..461ca4c 100644 --- a/examples/protocols/mqtt5/main/app_main.c +++ b/examples/protocols/mqtt5/main/app_main.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -99,12 +99,12 @@ static void print_user_property(mqtt5_user_property_handle_t user_property) */ static void mqtt5_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data) { - ESP_LOGD(TAG, "Event dispatched from event loop base=%s, event_id=%d", base, event_id); + ESP_LOGD(TAG, "Event dispatched from event loop base=%s, event_id=%" PRIi32, base, event_id); esp_mqtt_event_handle_t event = event_data; esp_mqtt_client_handle_t client = event->client; int msg_id; - ESP_LOGD(TAG, "free heap size is %d, maxminu %d", esp_get_free_heap_size(), esp_get_minimum_free_heap_size()); + ESP_LOGD(TAG, "free heap size is %" PRIu32 ", minimum %" PRIu32, esp_get_free_heap_size(), esp_get_minimum_free_heap_size()); switch ((esp_mqtt_event_id_t)event_id) { case MQTT_EVENT_CONNECTED: ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED"); @@ -263,8 +263,9 @@ static void mqtt5_app_start(void) void app_main(void) { + ESP_LOGI(TAG, "[APP] Startup.."); - ESP_LOGI(TAG, "[APP] Free memory: %d bytes", esp_get_free_heap_size()); + ESP_LOGI(TAG, "[APP] Free memory: %" PRIu32 " bytes", esp_get_free_heap_size()); ESP_LOGI(TAG, "[APP] IDF version: %s", esp_get_idf_version()); esp_log_level_set("*", ESP_LOG_INFO); From 8d88b6ea9d33282cb5253dcf48a332a47e9a5b56 Mon Sep 17 00:00:00 2001 From: Bogdan Kolendovskyy Date: Fri, 17 Mar 2023 16:03:52 +0100 Subject: [PATCH 168/231] Updated CMakeList.txt for main component of mqtt/ssl_ds. --- examples/protocols/mqtt/ssl_ds/main/CMakeLists.txt | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/examples/protocols/mqtt/ssl_ds/main/CMakeLists.txt b/examples/protocols/mqtt/ssl_ds/main/CMakeLists.txt index b8ef6ea..61fac40 100644 --- a/examples/protocols/mqtt/ssl_ds/main/CMakeLists.txt +++ b/examples/protocols/mqtt/ssl_ds/main/CMakeLists.txt @@ -1,3 +1,2 @@ idf_component_register(SRCS "app_main.c" - INCLUDE_DIRS "." - REQUIRED_IDF_TARGETS esp32s2 esp32c3 esp32s3) + INCLUDE_DIRS ".") From c0cd2199c9c0ef385fdb6dfcc57c06e2bfaf80a1 Mon Sep 17 00:00:00 2001 From: Euripedes Rocha Date: Wed, 15 Mar 2023 07:18:14 +0100 Subject: [PATCH 169/231] ci: Migrateunit test for mqtt test to unit test app - Split mqtt and mqtt5 tests - Move common test code to a common component - Add necessary python scripts --- components/mqtt/.build-test-rules.yml | 7 ++ .../mqtt/test_apps/common/CMakeLists.txt | 4 + .../common/include}/test_mqtt_connection.h | 2 +- .../common}/test_mqtt_connection.c | 0 .../mqtt/test_apps/test_mqtt/CMakeLists.txt | 9 ++ components/mqtt/test_apps/test_mqtt/README.md | 2 + .../test_mqtt/main}/CMakeLists.txt | 4 +- .../test_mqtt/main/Kconfig.projbuild} | 0 .../test_mqtt/main}/test_mqtt.c | 55 +++++---- .../test_mqtt/main}/test_mqtt_client_broker.c | 0 .../test_mqtt/main}/test_mqtt_client_broker.h | 2 +- .../test_apps/test_mqtt/pytest_mqtt_ut.py | 11 ++ .../test_apps/test_mqtt/sdkconfig.defaults | 3 + .../mqtt/test_apps/test_mqtt5/CMakeLists.txt | 9 ++ .../mqtt/test_apps/test_mqtt5/README.md | 2 + .../test_apps/test_mqtt5/main/CMakeLists.txt | 5 + .../test_mqtt5/main/Kconfig.projbuild | 14 +++ .../test_mqtt5/main}/test_mqtt5.c | 106 ++++++++++-------- .../main}/test_mqtt5_client_broker.c | 2 +- .../main}/test_mqtt5_client_broker.h | 2 +- .../test_apps/test_mqtt5/pytest_mqtt5_ut.py | 11 ++ .../test_apps/test_mqtt5/sdkconfig.defaults | 4 + 22 files changed, 183 insertions(+), 71 deletions(-) create mode 100644 components/mqtt/.build-test-rules.yml create mode 100644 components/mqtt/test_apps/common/CMakeLists.txt rename components/mqtt/{test => test_apps/common/include}/test_mqtt_connection.h (80%) rename components/mqtt/{test => test_apps/common}/test_mqtt_connection.c (100%) create mode 100644 components/mqtt/test_apps/test_mqtt/CMakeLists.txt create mode 100644 components/mqtt/test_apps/test_mqtt/README.md rename components/mqtt/{test => test_apps/test_mqtt/main}/CMakeLists.txt (70%) rename components/mqtt/{test/Kconfig => test_apps/test_mqtt/main/Kconfig.projbuild} (100%) rename components/mqtt/{test => test_apps/test_mqtt/main}/test_mqtt.c (72%) rename components/mqtt/{test => test_apps/test_mqtt/main}/test_mqtt_client_broker.c (100%) rename components/mqtt/{test => test_apps/test_mqtt/main}/test_mqtt_client_broker.h (95%) create mode 100644 components/mqtt/test_apps/test_mqtt/pytest_mqtt_ut.py create mode 100644 components/mqtt/test_apps/test_mqtt/sdkconfig.defaults create mode 100644 components/mqtt/test_apps/test_mqtt5/CMakeLists.txt create mode 100644 components/mqtt/test_apps/test_mqtt5/README.md create mode 100644 components/mqtt/test_apps/test_mqtt5/main/CMakeLists.txt create mode 100644 components/mqtt/test_apps/test_mqtt5/main/Kconfig.projbuild rename components/mqtt/{test => test_apps/test_mqtt5/main}/test_mqtt5.c (62%) rename components/mqtt/{test => test_apps/test_mqtt5/main}/test_mqtt5_client_broker.c (99%) rename components/mqtt/{test => test_apps/test_mqtt5/main}/test_mqtt5_client_broker.h (95%) create mode 100644 components/mqtt/test_apps/test_mqtt5/pytest_mqtt5_ut.py create mode 100644 components/mqtt/test_apps/test_mqtt5/sdkconfig.defaults diff --git a/components/mqtt/.build-test-rules.yml b/components/mqtt/.build-test-rules.yml new file mode 100644 index 0000000..5a5f690 --- /dev/null +++ b/components/mqtt/.build-test-rules.yml @@ -0,0 +1,7 @@ +# Documentation: .gitlab/ci/README.md#manifest-file-to-control-the-buildtest-apps + +components/mqtt/test_apps: + disable_test: + - if: IDF_TARGET not in ["esp32", "esp32c2"] + temporary: false + reason: Not needed to test on all targets (chosen two, one for each architecture) diff --git a/components/mqtt/test_apps/common/CMakeLists.txt b/components/mqtt/test_apps/common/CMakeLists.txt new file mode 100644 index 0000000..97ab3b5 --- /dev/null +++ b/components/mqtt/test_apps/common/CMakeLists.txt @@ -0,0 +1,4 @@ + +idf_component_register(SRCS test_mqtt_connection.c + INCLUDE_DIRS ${CMAKE_CURRENT_LIST_DIR}/include + PRIV_REQUIRES unity esp_event esp_netif esp_eth) diff --git a/components/mqtt/test/test_mqtt_connection.h b/components/mqtt/test_apps/common/include/test_mqtt_connection.h similarity index 80% rename from components/mqtt/test/test_mqtt_connection.h rename to components/mqtt/test_apps/common/include/test_mqtt_connection.h index 5322ddf..460becc 100644 --- a/components/mqtt/test/test_mqtt_connection.h +++ b/components/mqtt/test_apps/common/include/test_mqtt_connection.h @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2021 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2021-2023 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ diff --git a/components/mqtt/test/test_mqtt_connection.c b/components/mqtt/test_apps/common/test_mqtt_connection.c similarity index 100% rename from components/mqtt/test/test_mqtt_connection.c rename to components/mqtt/test_apps/common/test_mqtt_connection.c diff --git a/components/mqtt/test_apps/test_mqtt/CMakeLists.txt b/components/mqtt/test_apps/test_mqtt/CMakeLists.txt new file mode 100644 index 0000000..bc86bdb --- /dev/null +++ b/components/mqtt/test_apps/test_mqtt/CMakeLists.txt @@ -0,0 +1,9 @@ +#This is the project CMakeLists.txt file for the test subproject +cmake_minimum_required(VERSION 3.16) + +set(EXTRA_COMPONENT_DIRS "$ENV{IDF_PATH}/tools/unit-test-app/components" + "../common") + +set(COMPONENTS main) +include($ENV{IDF_PATH}/tools/cmake/project.cmake) +project(esp_mqtt_client_test) diff --git a/components/mqtt/test_apps/test_mqtt/README.md b/components/mqtt/test_apps/test_mqtt/README.md new file mode 100644 index 0000000..a8b7833 --- /dev/null +++ b/components/mqtt/test_apps/test_mqtt/README.md @@ -0,0 +1,2 @@ +| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C6 | ESP32-H2 | ESP32-S2 | ESP32-S3 | +| ----------------- | ----- | -------- | -------- | -------- | -------- | -------- | -------- | diff --git a/components/mqtt/test/CMakeLists.txt b/components/mqtt/test_apps/test_mqtt/main/CMakeLists.txt similarity index 70% rename from components/mqtt/test/CMakeLists.txt rename to components/mqtt/test_apps/test_mqtt/main/CMakeLists.txt index 3261bec..e7499a2 100644 --- a/components/mqtt/test/CMakeLists.txt +++ b/components/mqtt/test_apps/test_mqtt/main/CMakeLists.txt @@ -1,9 +1,9 @@ -set(srcs test_mqtt_client_broker.c test_mqtt_connection.c test_mqtt.c) +set(srcs test_mqtt_client_broker.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 spi_flash) + PRIV_REQUIRES cmock test_utils mqtt nvs_flash app_update esp_eth esp_netif spi_flash common) target_compile_options(${COMPONENT_LIB} PRIVATE "-Wno-format") diff --git a/components/mqtt/test/Kconfig b/components/mqtt/test_apps/test_mqtt/main/Kconfig.projbuild similarity index 100% rename from components/mqtt/test/Kconfig rename to components/mqtt/test_apps/test_mqtt/main/Kconfig.projbuild diff --git a/components/mqtt/test/test_mqtt.c b/components/mqtt/test_apps/test_mqtt/main/test_mqtt.c similarity index 72% rename from components/mqtt/test/test_mqtt.c rename to components/mqtt/test_apps/test_mqtt/main/test_mqtt.c index 41a5516..cb56a5e 100644 --- a/components/mqtt/test/test_mqtt.c +++ b/components/mqtt/test_apps/test_mqtt/main/test_mqtt.c @@ -11,33 +11,30 @@ */ #include -#include "freertos/FreeRTOS.h" -#include "freertos/event_groups.h" -#include "unity.h" +#include "unity_fixture.h" +#include "unity_fixture_extras.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" #include "esp_partition.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_GROUP(mqtt); + +TEST_SETUP(mqtt){ test_utils_record_free_mem(); + TEST_ESP_OK(test_utils_set_leak_level(0, ESP_LEAK_TYPE_CRITICAL, ESP_COMP_LEAK_GENERAL)); } -TEST_CASE("mqtt init with invalid url", "[mqtt][leaks=0]") +TEST_TEAR_DOWN(mqtt){ + test_utils_finish_and_evaluate_leaks(test_utils_get_leak_level(ESP_LEAK_TYPE_WARNING, ESP_COMP_LEAK_ALL), + test_utils_get_leak_level(ESP_LEAK_TYPE_CRITICAL, ESP_COMP_LEAK_ALL)); +} + +TEST(mqtt, init_with_invalid_url) { - test_leak_setup(__FILE__, __LINE__); const esp_mqtt_client_config_t mqtt_cfg = { .broker.address.uri = "INVALID", }; @@ -45,9 +42,8 @@ TEST_CASE("mqtt init with invalid url", "[mqtt][leaks=0]") TEST_ASSERT_EQUAL(NULL, client ); } -TEST_CASE("mqtt init and deinit", "[mqtt][leaks=0]") +TEST(mqtt, init_and_deinit) { - 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", @@ -66,10 +62,13 @@ static const char* this_bin_addr(void) return binary_address; } -TEST_CASE("mqtt enqueue and destroy outbox", "[mqtt][leaks=0]") +TEST(mqtt, enqueue_and_destroy_outbox) { const char * bin_addr = this_bin_addr(); - test_leak_setup(__FILE__, __LINE__); + + // Reseting leak detection since this_bin_addr adds to allocated memory. + test_utils_record_free_mem(); + TEST_ESP_OK(test_utils_set_leak_level(0, ESP_LEAK_TYPE_CRITICAL, ESP_COMP_LEAK_GENERAL)); const int messages = 20; const int size = 2000; const esp_mqtt_client_config_t mqtt_cfg = { @@ -93,7 +92,7 @@ TEST_CASE("mqtt enqueue and destroy outbox", "[mqtt][leaks=0]") /** * 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]") +TEST(mqtt, broker_tests) { test_case_uses_tcpip(); connect_test_fixture_setup(); @@ -105,5 +104,19 @@ TEST_CASE("mqtt broker tests", "[mqtt][test_env=UT_T2_Ethernet]") connect_test_fixture_teardown(); } - #endif // SOC_EMAC_SUPPORTED + + +TEST_GROUP_RUNNER(mqtt) { +RUN_TEST_CASE(mqtt, init_with_invalid_url); +RUN_TEST_CASE(mqtt, init_and_deinit); +RUN_TEST_CASE(mqtt, enqueue_and_destroy_outbox); + +#if SOC_EMAC_SUPPORTED +RUN_TEST_CASE(mqtt, broker_tests); +#endif // SOC_EMAC_SUPPORTED +} + +void app_main(void){ + UNITY_MAIN(mqtt); +} diff --git a/components/mqtt/test/test_mqtt_client_broker.c b/components/mqtt/test_apps/test_mqtt/main/test_mqtt_client_broker.c similarity index 100% rename from components/mqtt/test/test_mqtt_client_broker.c rename to components/mqtt/test_apps/test_mqtt/main/test_mqtt_client_broker.c diff --git a/components/mqtt/test/test_mqtt_client_broker.h b/components/mqtt/test_apps/test_mqtt/main/test_mqtt_client_broker.h similarity index 95% rename from components/mqtt/test/test_mqtt_client_broker.h rename to components/mqtt/test_apps/test_mqtt/main/test_mqtt_client_broker.h index 6bfd5d8..63e7856 100644 --- a/components/mqtt/test/test_mqtt_client_broker.h +++ b/components/mqtt/test_apps/test_mqtt/main/test_mqtt_client_broker.h @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2021 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2021-2023 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ diff --git a/components/mqtt/test_apps/test_mqtt/pytest_mqtt_ut.py b/components/mqtt/test_apps/test_mqtt/pytest_mqtt_ut.py new file mode 100644 index 0000000..f3333fa --- /dev/null +++ b/components/mqtt/test_apps/test_mqtt/pytest_mqtt_ut.py @@ -0,0 +1,11 @@ +# SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD +# SPDX-License-Identifier: Unlicense OR CC0-1.0 +import pytest +from pytest_embedded import Dut + + +@pytest.mark.esp32 +@pytest.mark.esp32c2 +@pytest.mark.ethernet +def test_mqtt_client(dut: Dut) -> None: + dut.expect_unity_test_output() diff --git a/components/mqtt/test_apps/test_mqtt/sdkconfig.defaults b/components/mqtt/test_apps/test_mqtt/sdkconfig.defaults new file mode 100644 index 0000000..2bcae3f --- /dev/null +++ b/components/mqtt/test_apps/test_mqtt/sdkconfig.defaults @@ -0,0 +1,3 @@ +# General options for additional checks +CONFIG_ESP_TASK_WDT_EN=n +CONFIG_UNITY_ENABLE_FIXTURE=y diff --git a/components/mqtt/test_apps/test_mqtt5/CMakeLists.txt b/components/mqtt/test_apps/test_mqtt5/CMakeLists.txt new file mode 100644 index 0000000..1c7881c --- /dev/null +++ b/components/mqtt/test_apps/test_mqtt5/CMakeLists.txt @@ -0,0 +1,9 @@ +#This is the project CMakeLists.txt file for the test subproject +cmake_minimum_required(VERSION 3.16) + +set(EXTRA_COMPONENT_DIRS "$ENV{IDF_PATH}/tools/unit-test-app/components" + "../common") + +set(COMPONENTS main) +include($ENV{IDF_PATH}/tools/cmake/project.cmake) +project(esp_mqtt5_client_test) diff --git a/components/mqtt/test_apps/test_mqtt5/README.md b/components/mqtt/test_apps/test_mqtt5/README.md new file mode 100644 index 0000000..a8b7833 --- /dev/null +++ b/components/mqtt/test_apps/test_mqtt5/README.md @@ -0,0 +1,2 @@ +| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C6 | ESP32-H2 | ESP32-S2 | ESP32-S3 | +| ----------------- | ----- | -------- | -------- | -------- | -------- | -------- | -------- | diff --git a/components/mqtt/test_apps/test_mqtt5/main/CMakeLists.txt b/components/mqtt/test_apps/test_mqtt5/main/CMakeLists.txt new file mode 100644 index 0000000..326b8fa --- /dev/null +++ b/components/mqtt/test_apps/test_mqtt5/main/CMakeLists.txt @@ -0,0 +1,5 @@ +set(srcs test_mqtt5_client_broker.c test_mqtt5.c) + +idf_component_register(SRCS "${srcs}" + PRIV_REQUIRES cmock test_utils mqtt nvs_flash app_update esp_eth esp_netif spi_flash common) + target_compile_options(${COMPONENT_LIB} PRIVATE "-Wno-format") diff --git a/components/mqtt/test_apps/test_mqtt5/main/Kconfig.projbuild b/components/mqtt/test_apps/test_mqtt5/main/Kconfig.projbuild new file mode 100644 index 0000000..30b2c10 --- /dev/null +++ b/components/mqtt/test_apps/test_mqtt5/main/Kconfig.projbuild @@ -0,0 +1,14 @@ +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_mqtt5.c b/components/mqtt/test_apps/test_mqtt5/main/test_mqtt5.c similarity index 62% rename from components/mqtt/test/test_mqtt5.c rename to components/mqtt/test_apps/test_mqtt5/main/test_mqtt5.c index d9bfe82..9d8705d 100644 --- a/components/mqtt/test/test_mqtt5.c +++ b/components/mqtt/test_apps/test_mqtt5/main/test_mqtt5.c @@ -5,62 +5,60 @@ */ #include -#include "freertos/FreeRTOS.h" -#include "freertos/event_groups.h" -#include "unity.h" +#include "unity_fixture.h" +#include "unity_fixture_extras.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" #include "esp_partition.h" -#if !TEMPORARY_DISABLED_FOR_TARGETS(ESP32C6, ESP32H2) + +TEST_GROUP(mqtt5); + +TEST_SETUP(mqtt5) +{ + test_utils_record_free_mem(); + TEST_ESP_OK(test_utils_set_leak_level(0, ESP_LEAK_TYPE_CRITICAL, ESP_COMP_LEAK_GENERAL)); +} + +TEST_TEAR_DOWN(mqtt5) +{ + test_utils_finish_and_evaluate_leaks(test_utils_get_leak_level(ESP_LEAK_TYPE_WARNING, ESP_COMP_LEAK_ALL), + test_utils_get_leak_level(ESP_LEAK_TYPE_CRITICAL, ESP_COMP_LEAK_ALL)); +} + 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) +TEST(mqtt5, init_with_invalid_url) { - 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, + .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(mqtt5, init_and_deinit) { - 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, + // 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, @@ -88,25 +86,27 @@ TEST_CASE("mqtt5 init and deinit", "[mqtt5][leaks=0]") esp_mqtt_client_destroy(client); } -static const char* this_bin_addr(void) +static const char *this_bin_addr(void) { esp_partition_mmap_handle_t out_handle; const void *binary_address; - const esp_partition_t* partition = esp_ota_get_running_partition(); + const esp_partition_t *partition = esp_ota_get_running_partition(); esp_partition_mmap(partition, 0, partition->size, ESP_PARTITION_MMAP_DATA, &binary_address, &out_handle); return binary_address; } -TEST_CASE("mqtt5 enqueue and destroy outbox", "[mqtt5][leaks=0]") +TEST(mqtt5, enqueue_and_destroy_outbox) { - const char * bin_addr = this_bin_addr(); - test_leak_setup(__FILE__, __LINE__); + const char *bin_addr = this_bin_addr(); + // Reseting leak detection since this_bin_addr adds to allocated memory. + test_utils_record_free_mem(); + TEST_ESP_OK(test_utils_set_leak_level(0, ESP_LEAK_TYPE_CRITICAL, ESP_COMP_LEAK_GENERAL)); 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, + // 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, @@ -129,7 +129,7 @@ TEST_CASE("mqtt5 enqueue and destroy outbox", "[mqtt5][leaks=0]") } 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); + TEST_ASSERT_GREATER_OR_EQUAL(messages * size, bytes_before - bytes_after); esp_mqtt_client_destroy(client); } @@ -138,7 +138,7 @@ TEST_CASE("mqtt5 enqueue and destroy outbox", "[mqtt5][leaks=0]") /** * 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(mqtt5, broker_tests) { test_case_uses_tcpip(); connect_test_fixture_setup(); @@ -151,4 +151,22 @@ TEST_CASE("mqtt5 broker tests", "[mqtt5][test_env=UT_T2_Ethernet]") connect_test_fixture_teardown(); } #endif // SOC_EMAC_SUPPORTED -#endif //!TEMPORARY_DISABLED_FOR_TARGETS(ESP32C6, ESP32H2) + +TEST_GROUP_RUNNER(mqtt5) +{ +#if !DISABLED_FOR_TARGETS(ESP32H2) + RUN_TEST_CASE(mqtt5, init_with_invalid_url); + RUN_TEST_CASE(mqtt5, init_and_deinit); + RUN_TEST_CASE(mqtt5, enqueue_and_destroy_outbox); + +#if SOC_EMAC_SUPPORTED + RUN_TEST_CASE(mqtt5, broker_tests); +#endif // SOC_EMAC_SUPPORTED +#endif //!DISABLED_FOR_TARGETS(ESP32H2) +} + + +void app_main(void) +{ + UNITY_MAIN(mqtt5); +} diff --git a/components/mqtt/test/test_mqtt5_client_broker.c b/components/mqtt/test_apps/test_mqtt5/main/test_mqtt5_client_broker.c similarity index 99% rename from components/mqtt/test/test_mqtt5_client_broker.c rename to components/mqtt/test_apps/test_mqtt5/main/test_mqtt5_client_broker.c index ced44e9..0ac4c3d 100644 --- a/components/mqtt/test/test_mqtt5_client_broker.c +++ b/components/mqtt/test_apps/test_mqtt5/main/test_mqtt5_client_broker.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ diff --git a/components/mqtt/test/test_mqtt5_client_broker.h b/components/mqtt/test_apps/test_mqtt5/main/test_mqtt5_client_broker.h similarity index 95% rename from components/mqtt/test/test_mqtt5_client_broker.h rename to components/mqtt/test_apps/test_mqtt5/main/test_mqtt5_client_broker.h index 52b6ab8..a181dd8 100644 --- a/components/mqtt/test/test_mqtt5_client_broker.h +++ b/components/mqtt/test_apps/test_mqtt5/main/test_mqtt5_client_broker.h @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ diff --git a/components/mqtt/test_apps/test_mqtt5/pytest_mqtt5_ut.py b/components/mqtt/test_apps/test_mqtt5/pytest_mqtt5_ut.py new file mode 100644 index 0000000..35766cb --- /dev/null +++ b/components/mqtt/test_apps/test_mqtt5/pytest_mqtt5_ut.py @@ -0,0 +1,11 @@ +# SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD +# SPDX-License-Identifier: Unlicense OR CC0-1.0 +import pytest +from pytest_embedded import Dut + + +@pytest.mark.esp32 +@pytest.mark.esp32c2 +@pytest.mark.ethernet +def test_mqtt5_client(dut: Dut) -> None: + dut.expect_unity_test_output() diff --git a/components/mqtt/test_apps/test_mqtt5/sdkconfig.defaults b/components/mqtt/test_apps/test_mqtt5/sdkconfig.defaults new file mode 100644 index 0000000..06c58d7 --- /dev/null +++ b/components/mqtt/test_apps/test_mqtt5/sdkconfig.defaults @@ -0,0 +1,4 @@ +# General options for additional checks +CONFIG_ESP_TASK_WDT_EN=n +CONFIG_MQTT_PROTOCOL_5=y +CONFIG_UNITY_ENABLE_FIXTURE=y From 80bd386d3bf3510571b000ababe14eff17b612ae Mon Sep 17 00:00:00 2001 From: Fu Hanxi Date: Thu, 6 Apr 2023 11:01:20 +0800 Subject: [PATCH 170/231] ci: remove ttfw related info in tools/test_apps --- .../mqtt/publish_connect_test/publish_connect_mqtt_.yml | 2 -- .../mqtt/publish_connect_test/publish_connect_mqtt_qemu.yml | 6 ------ 2 files changed, 8 deletions(-) delete mode 100644 tools/test_apps/protocols/mqtt/publish_connect_test/publish_connect_mqtt_.yml delete mode 100644 tools/test_apps/protocols/mqtt/publish_connect_test/publish_connect_mqtt_qemu.yml diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/publish_connect_mqtt_.yml b/tools/test_apps/protocols/mqtt/publish_connect_test/publish_connect_mqtt_.yml deleted file mode 100644 index 70e13ef..0000000 --- a/tools/test_apps/protocols/mqtt/publish_connect_test/publish_connect_mqtt_.yml +++ /dev/null @@ -1,2 +0,0 @@ -CaseConfig: -- name: test_app_protocol_mqtt_publish_connect diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/publish_connect_mqtt_qemu.yml b/tools/test_apps/protocols/mqtt/publish_connect_test/publish_connect_mqtt_qemu.yml deleted file mode 100644 index 06f1235..0000000 --- a/tools/test_apps/protocols/mqtt/publish_connect_test/publish_connect_mqtt_qemu.yml +++ /dev/null @@ -1,6 +0,0 @@ -CaseConfig: -- name: test_app_protocol_mqtt_publish_connect - overwrite: - dut: - class: ESP32QEMUDUT - package: ttfw_idf From b8047f0dada554fbc30beae62d32a6671e4394ed Mon Sep 17 00:00:00 2001 From: David Cermak Date: Mon, 27 Mar 2023 12:54:25 +0200 Subject: [PATCH 171/231] esp_netif: Make esp_netif_receive() return value configurable --- .../protocols/mqtt/publish_connect_test/sdkconfig.ci.default | 1 + 1 file changed, 1 insertion(+) diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/sdkconfig.ci.default b/tools/test_apps/protocols/mqtt/publish_connect_test/sdkconfig.ci.default index 25a7c80..cfc39a4 100644 --- a/tools/test_apps/protocols/mqtt/publish_connect_test/sdkconfig.ci.default +++ b/tools/test_apps/protocols/mqtt/publish_connect_test/sdkconfig.ci.default @@ -10,6 +10,7 @@ CONFIG_ESP_TLS_INSECURE=y CONFIG_ESP_TLS_SKIP_SERVER_CERT_VERIFY=y CONFIG_EXAMPLE_CONNECT_ETHERNET=y CONFIG_EXAMPLE_CONNECT_WIFI=n +CONFIG_ESP_NETIF_RECEIVE_REPORT_ERRORS=y CONFIG_EXAMPLE_USE_INTERNAL_ETHERNET=y CONFIG_EXAMPLE_ETH_PHY_IP101=y CONFIG_EXAMPLE_ETH_MDC_GPIO=23 From 7e352dcd85b68186011f03d8153556db75f7e30c Mon Sep 17 00:00:00 2001 From: Euripedes Rocha Date: Mon, 29 May 2023 14:47:43 +0200 Subject: [PATCH 172/231] protocols/examples: Disable Wifi connection if not supported - Disable Kconfig option for Wifi if not supported by the SoC - Enable building mqtt examples when target is set to esp32h2 --- examples/protocols/mqtt/ssl/README.md | 4 ++-- examples/protocols/mqtt/ssl_mutual_auth/README.md | 4 ++-- examples/protocols/mqtt/ssl_psk/README.md | 4 ++-- examples/protocols/mqtt/tcp/README.md | 4 ++-- examples/protocols/mqtt/ws/README.md | 4 ++-- examples/protocols/mqtt/wss/README.md | 4 ++-- examples/protocols/mqtt5/README.md | 4 ++-- 7 files changed, 14 insertions(+), 14 deletions(-) diff --git a/examples/protocols/mqtt/ssl/README.md b/examples/protocols/mqtt/ssl/README.md index d597c6b..ff97ae7 100644 --- a/examples/protocols/mqtt/ssl/README.md +++ b/examples/protocols/mqtt/ssl/README.md @@ -1,5 +1,5 @@ -| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C6 | ESP32-S2 | ESP32-S3 | -| ----------------- | ----- | -------- | -------- | -------- | -------- | -------- | +| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C6 | ESP32-H2 | ESP32-S2 | ESP32-S3 | +| ----------------- | ----- | -------- | -------- | -------- | -------- | -------- | -------- | # ESP-MQTT SSL Sample application diff --git a/examples/protocols/mqtt/ssl_mutual_auth/README.md b/examples/protocols/mqtt/ssl_mutual_auth/README.md index f50c449..71de8e3 100644 --- a/examples/protocols/mqtt/ssl_mutual_auth/README.md +++ b/examples/protocols/mqtt/ssl_mutual_auth/README.md @@ -1,5 +1,5 @@ -| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C6 | ESP32-S2 | ESP32-S3 | -| ----------------- | ----- | -------- | -------- | -------- | -------- | -------- | +| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C6 | ESP32-H2 | ESP32-S2 | ESP32-S3 | +| ----------------- | ----- | -------- | -------- | -------- | -------- | -------- | -------- | # ESP-MQTT SSL Sample application (mutual authentication) diff --git a/examples/protocols/mqtt/ssl_psk/README.md b/examples/protocols/mqtt/ssl_psk/README.md index 9df73be..ce0fa6d 100644 --- a/examples/protocols/mqtt/ssl_psk/README.md +++ b/examples/protocols/mqtt/ssl_psk/README.md @@ -1,5 +1,5 @@ -| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C6 | ESP32-S2 | ESP32-S3 | -| ----------------- | ----- | -------- | -------- | -------- | -------- | -------- | +| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C6 | ESP32-H2 | ESP32-S2 | ESP32-S3 | +| ----------------- | ----- | -------- | -------- | -------- | -------- | -------- | -------- | # ESP-MQTT SSL example with PSK verification diff --git a/examples/protocols/mqtt/tcp/README.md b/examples/protocols/mqtt/tcp/README.md index 63c0f0f..52888e3 100644 --- a/examples/protocols/mqtt/tcp/README.md +++ b/examples/protocols/mqtt/tcp/README.md @@ -1,5 +1,5 @@ -| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C6 | ESP32-S2 | ESP32-S3 | -| ----------------- | ----- | -------- | -------- | -------- | -------- | -------- | +| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C6 | ESP32-H2 | ESP32-S2 | ESP32-S3 | +| ----------------- | ----- | -------- | -------- | -------- | -------- | -------- | -------- | # ESP-MQTT sample application (See the README.md file in the upper level 'examples' directory for more information about examples.) diff --git a/examples/protocols/mqtt/ws/README.md b/examples/protocols/mqtt/ws/README.md index 5209b0d..69b66ac 100644 --- a/examples/protocols/mqtt/ws/README.md +++ b/examples/protocols/mqtt/ws/README.md @@ -1,5 +1,5 @@ -| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C6 | ESP32-S2 | ESP32-S3 | -| ----------------- | ----- | -------- | -------- | -------- | -------- | -------- | +| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C6 | ESP32-H2 | ESP32-S2 | ESP32-S3 | +| ----------------- | ----- | -------- | -------- | -------- | -------- | -------- | -------- | # ESP-MQTT MQTT over Websocket diff --git a/examples/protocols/mqtt/wss/README.md b/examples/protocols/mqtt/wss/README.md index 94a2e02..7b1222f 100644 --- a/examples/protocols/mqtt/wss/README.md +++ b/examples/protocols/mqtt/wss/README.md @@ -1,5 +1,5 @@ -| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C6 | ESP32-S2 | ESP32-S3 | -| ----------------- | ----- | -------- | -------- | -------- | -------- | -------- | +| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C6 | ESP32-H2 | ESP32-S2 | ESP32-S3 | +| ----------------- | ----- | -------- | -------- | -------- | -------- | -------- | -------- | # ESP-MQTT MQTT over WSS Sample application (See the README.md file in the upper level 'examples' directory for more information about examples.) diff --git a/examples/protocols/mqtt5/README.md b/examples/protocols/mqtt5/README.md index afe65ad..efc2b7b 100644 --- a/examples/protocols/mqtt5/README.md +++ b/examples/protocols/mqtt5/README.md @@ -1,5 +1,5 @@ -| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C6 | ESP32-S2 | ESP32-S3 | -| ----------------- | ----- | -------- | -------- | -------- | -------- | -------- | +| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C6 | ESP32-H2 | ESP32-S2 | ESP32-S3 | +| ----------------- | ----- | -------- | -------- | -------- | -------- | -------- | -------- | # ESP-MQTT sample application (See the README.md file in the upper level 'examples' directory for more information about examples.) From 95741c16821f679209787e872adc7822b8cb3e32 Mon Sep 17 00:00:00 2001 From: KonstantinKondrashov Date: Fri, 9 Jun 2023 02:56:11 +0800 Subject: [PATCH 173/231] all: Removes unnecessary newline character in logs Closes https://github.com/espressif/esp-idf/issues/11465 --- .../protocols/mqtt/publish_connect_test/main/publish_test.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/main/publish_test.c b/tools/test_apps/protocols/mqtt/publish_connect_test/main/publish_test.c index f49904d..8b74f4e 100644 --- a/tools/test_apps/protocols/mqtt/publish_connect_test/main/publish_test.c +++ b/tools/test_apps/protocols/mqtt/publish_connect_test/main/publish_test.c @@ -204,7 +204,7 @@ void publish_test(const char *line) } sscanf(line, "%s %s %d %d %d %d", transport, pattern, &repeat, &expected_published, &qos_test, &enqueue); - ESP_LOGI(TAG, "PATTERN:%s REPEATED:%d PUBLISHED:%d\n", pattern, repeat, expected_published); + ESP_LOGI(TAG, "PATTERN:%s REPEATED:%d PUBLISHED:%d", pattern, repeat, expected_published); pattern_setup(pattern, repeat); xEventGroupClearBits(mqtt_event_group, CONNECTED_BIT); configure_client(transport); From 07db66c6692871b7c29393ecd33792818764f6ce Mon Sep 17 00:00:00 2001 From: Euripedes Rocha Date: Fri, 30 Jun 2023 10:59:10 +0200 Subject: [PATCH 174/231] fix(test/mqtt): Removes no-format from test_apps build Allows warnings on mismatching format strings. --- components/mqtt/test_apps/test_mqtt/main/CMakeLists.txt | 1 - components/mqtt/test_apps/test_mqtt5/main/CMakeLists.txt | 1 - 2 files changed, 2 deletions(-) diff --git a/components/mqtt/test_apps/test_mqtt/main/CMakeLists.txt b/components/mqtt/test_apps/test_mqtt/main/CMakeLists.txt index e7499a2..7a06624 100644 --- a/components/mqtt/test_apps/test_mqtt/main/CMakeLists.txt +++ b/components/mqtt/test_apps/test_mqtt/main/CMakeLists.txt @@ -6,4 +6,3 @@ endif() idf_component_register(SRCS "${srcs}" PRIV_REQUIRES cmock test_utils mqtt nvs_flash app_update esp_eth esp_netif spi_flash common) - target_compile_options(${COMPONENT_LIB} PRIVATE "-Wno-format") diff --git a/components/mqtt/test_apps/test_mqtt5/main/CMakeLists.txt b/components/mqtt/test_apps/test_mqtt5/main/CMakeLists.txt index 326b8fa..f152df9 100644 --- a/components/mqtt/test_apps/test_mqtt5/main/CMakeLists.txt +++ b/components/mqtt/test_apps/test_mqtt5/main/CMakeLists.txt @@ -2,4 +2,3 @@ set(srcs test_mqtt5_client_broker.c test_mqtt5.c) idf_component_register(SRCS "${srcs}" PRIV_REQUIRES cmock test_utils mqtt nvs_flash app_update esp_eth esp_netif spi_flash common) - target_compile_options(${COMPONENT_LIB} PRIVATE "-Wno-format") From fcaf10581c84de3687c920913bf466c9ffc35d59 Mon Sep 17 00:00:00 2001 From: caixinying-git Date: Mon, 31 Jul 2023 16:13:41 +0800 Subject: [PATCH 175/231] docs: update format issues for EN and CN under api-reference/protocols and migration-guides --- docs/en/api-reference/protocols/mqtt.rst | 24 ++++++++++++--------- docs/zh_CN/api-reference/protocols/mqtt.rst | 10 ++++++--- 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/docs/en/api-reference/protocols/mqtt.rst b/docs/en/api-reference/protocols/mqtt.rst index b6e9233..7e8392e 100644 --- a/docs/en/api-reference/protocols/mqtt.rst +++ b/docs/en/api-reference/protocols/mqtt.rst @@ -1,5 +1,6 @@ ESP-MQTT ======== + :link_to_translation:`zh_CN:[中文]` Overview @@ -10,6 +11,7 @@ ESP-MQTT is an implementation of `MQTT `__ protocol client, w Features -------- + * Support MQTT over TCP, SSL with Mbed TLS, MQTT over WebSocket, and MQTT over WebSocket Secure * Easy to setup with URI * Multiple instances (multiple clients in one application) @@ -33,7 +35,7 @@ MQTT Message Retransmission A new MQTT message is created by calling :cpp:func:`esp_mqtt_client_publish ` or its non blocking counterpart :cpp:func:`esp_mqtt_client_enqueue `. -Messages with QoS 0 will be sent only once. QoS 1 and 2 have different behaviors since the protocol requires extra steps to complete the process. +Messages with QoS 0 is sent only once. QoS 1 and 2 have different behaviors since the protocol requires extra steps to complete the process. The ESP-MQTT library opts to always retransmit unacknowledged QoS 1 and 2 publish messages to avoid losses in faulty connections, even though the MQTT specification requires the re-transmission only on reconnect with Clean Session flag been set to 0 (set :cpp:member:`disable_clean_session ` to true for this behavior). @@ -87,7 +89,7 @@ The :cpp:member:`uri ` field - Minimal configurations: -.. code:: c +.. code-block:: c const esp_mqtt_client_config_t mqtt_cfg = { .broker.address.uri = "mqtt://mqtt.eclipseprojects.io", @@ -96,7 +98,9 @@ The :cpp:member:`uri ` field esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, client); esp_mqtt_client_start(client); -.. note:: By default MQTT client uses event loop library to post related MQTT events (connected, subscribed, published, etc.). +.. note:: + + By default MQTT client uses event loop library to post related MQTT events (connected, subscribed, published, etc.). ============ Verification @@ -137,7 +141,7 @@ All client related credentials are under the :cpp:class:`credentials ` field. The client supports the following authentication methods: +It is possible to set authentication parameters through the :cpp:class:`authentication ` field. The client supports the following authentication methods: * :cpp:member:`password `: use a password by setting * :cpp:member:`certificate ` and :cpp:member:`key `: mutual authentication with TLS, and both can be provided in PEM or DER format @@ -181,12 +185,12 @@ The following events may be posted by the MQTT client: * ``MQTT_EVENT_BEFORE_CONNECT``: The client is initialized and about to start connecting to the broker. * ``MQTT_EVENT_CONNECTED``: The client has successfully established a connection to the broker. The client is now ready to send and receive data. -* ``MQTT_EVENT_DISCONNECTED``: The client has aborted the connection due to being unable to read or write data, e.g. because the server is unavailable. -* ``MQTT_EVENT_SUBSCRIBED``: The broker has acknowledged the client's subscribe request. The event data will contain the message ID of the subscribe message. -* ``MQTT_EVENT_UNSUBSCRIBED``: The broker has acknowledged the client's unsubscribe request. The event data will contain the message ID of the unsubscribe message. -* ``MQTT_EVENT_PUBLISHED``: The broker has acknowledged the client's publish message. This will only be posted for QoS level 1 and 2, as level 0 does not use acknowledgements. The event data will contain the message ID of the publish message. -* ``MQTT_EVENT_DATA``: The client has received a publish message. The event data contains: message ID, name of the topic it was published to, received data and its length. For data that exceeds the internal buffer, multiple ``MQTT_EVENT_DATA`` will be posted and :cpp:member:`current_data_offset ` and :cpp:member:`total_data_len` from event data updated to keep track of the fragmented message. -* ``MQTT_EVENT_ERROR``: The client has encountered an error. The field :cpp:type:`error_handle ` in the event data contains :cpp:type:`error_type ` that can be used to identify the error. The type of error will determine which parts of the :cpp:type:`error_handle ` struct is filled. +* ``MQTT_EVENT_DISCONNECTED``: The client has aborted the connection due to being unable to read or write data, e.g., because the server is unavailable. +* ``MQTT_EVENT_SUBSCRIBED``: The broker has acknowledged the client's subscribe request. The event data contains the message ID of the subscribe message. +* ``MQTT_EVENT_UNSUBSCRIBED``: The broker has acknowledged the client's unsubscribe request. The event data contains the message ID of the unsubscribe message. +* ``MQTT_EVENT_PUBLISHED``: The broker has acknowledged the client's publish message. This is only posted for QoS level 1 and 2, as level 0 does not use acknowledgements. The event data contains the message ID of the publish message. +* ``MQTT_EVENT_DATA``: The client has received a publish message. The event data contains: message ID, name of the topic it was published to, received data and its length. For data that exceeds the internal buffer, multiple ``MQTT_EVENT_DATA`` events are posted and :cpp:member:`current_data_offset ` and :cpp:member:`total_data_len` from event data updated to keep track of the fragmented message. +* ``MQTT_EVENT_ERROR``: The client has encountered an error. The field :cpp:type:`error_handle ` in the event data contains :cpp:type:`error_type ` that can be used to identify the error. The type of error determines which parts of the :cpp:type:`error_handle ` struct is filled. API Reference ------------- diff --git a/docs/zh_CN/api-reference/protocols/mqtt.rst b/docs/zh_CN/api-reference/protocols/mqtt.rst index b3e5cfc..fa0a47b 100644 --- a/docs/zh_CN/api-reference/protocols/mqtt.rst +++ b/docs/zh_CN/api-reference/protocols/mqtt.rst @@ -1,5 +1,6 @@ ESP-MQTT ======== + :link_to_translation:`en:[English]` 概述 @@ -10,6 +11,7 @@ ESP-MQTT 是 `MQTT `__ 协议客户端的实现,MQTT 是一 特性 -------- + * 支持基于 TCP 的 MQTT、基于 Mbed TLS 的 SSL、基于 WebSocket 的 MQTT 以及基于 WebSocket Secure 的 MQTT * 通过 URI 简化配置流程 * 多个实例(一个应用程序中有多个客户端) @@ -60,7 +62,7 @@ ESP-MQTT 库将始终重新传输未确认的 QoS 1 和 2 发布消息,以避 地址 =========== -通过 :cpp:class:`address ` 结构体的 :cpp:member:`uri ` 字段或者 :cpp:member:`hostname `、:cpp:member:`transport ` 以及 :cpp:member:`port ` 的组合,可以设置服务器地址。您也可以选择设置 :cpp:member:`path `,该字段对 WebSocket 连接而言非常有用。 +通过 :cpp:class:`address ` 结构体的 :cpp:member:`uri ` 字段或者 :cpp:member:`hostname `、:cpp:member:`transport ` 以及 :cpp:member:`port ` 的组合,可以设置服务器地址。也可以选择设置 :cpp:member:`path `,该字段对 WebSocket 连接而言非常有用。 使用 :cpp:member:`uri ` 字段的格式为 ``scheme://hostname:port/path``。 @@ -87,7 +89,7 @@ ESP-MQTT 库将始终重新传输未确认的 QoS 1 和 2 发布消息,以避 - 最简配置: -.. code:: c +.. code-block:: c const esp_mqtt_client_config_t mqtt_cfg = { .broker.address.uri = "mqtt://mqtt.eclipseprojects.io", @@ -96,7 +98,9 @@ ESP-MQTT 库将始终重新传输未确认的 QoS 1 和 2 发布消息,以避 esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, client); esp_mqtt_client_start(client); -.. note:: 默认情况下,MQTT 客户端使用事件循环库来发布相关 MQTT 事件(已连接、已订阅、已发布等)。 +.. note:: + + 默认情况下,MQTT 客户端使用事件循环库来发布相关 MQTT 事件(已连接、已订阅、已发布等)。 ============= 验证 From 3f1c0181eb6bc268fa4a19616b8e904a2d1d92ba Mon Sep 17 00:00:00 2001 From: Euripedes Rocha Date: Tue, 22 Aug 2023 16:46:21 +0200 Subject: [PATCH 176/231] ci: Fix publish connect mqtt test Minor changes necessary to make the test run correctly --- .../publish_connect_test/pytest_mqtt_app.py | 79 ++++++++++--------- 1 file changed, 43 insertions(+), 36 deletions(-) diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/pytest_mqtt_app.py b/tools/test_apps/protocols/mqtt/publish_connect_test/pytest_mqtt_app.py index fd6133f..6b27adc 100644 --- a/tools/test_apps/protocols/mqtt/publish_connect_test/pytest_mqtt_app.py +++ b/tools/test_apps/protocols/mqtt/publish_connect_test/pytest_mqtt_app.py @@ -1,7 +1,8 @@ -# SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD +# SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD # SPDX-License-Identifier: Unlicense OR CC0-1.0 from __future__ import print_function, unicode_literals +import difflib import logging import os import random @@ -47,6 +48,7 @@ class MqttPublisher: event_client_got_all = Event() expected_data = '' published = 0 + sample = '' def __init__(self, dut, transport, qos, repeat, published, queue, publish_cfg, log_details=False): # type: (MqttPublisher, Dut, str, int, int, int, int, dict, bool) -> None @@ -68,11 +70,12 @@ class MqttPublisher: MqttPublisher.published = published MqttPublisher.event_client_connected.clear() MqttPublisher.event_client_got_all.clear() - MqttPublisher.expected_data = self.sample_string * self.repeat + MqttPublisher.expected_data = f'{self.sample_string * self.repeat}' + MqttPublisher.sample = self.sample_string def print_details(self, text): # type: (str) -> None if self.log_details: - print(text) + logging.info(text) def mqtt_client_task(self, client, lock): # type: (MqttPublisher, mqtt.Client, Lock) -> None while not self.event_stop_client.is_set(): @@ -88,12 +91,23 @@ class MqttPublisher: # The callback for when a PUBLISH message is received from the server (needs to be static) @staticmethod def on_message(client, userdata, msg): # type: (mqtt.Client, int, mqtt.client.MQTTMessage) -> None - payload = msg.payload.decode() + payload = msg.payload.decode('utf-8') if payload == MqttPublisher.expected_data: userdata += 1 client.user_data_set(userdata) if userdata == MqttPublisher.published: MqttPublisher.event_client_got_all.set() + else: + differences = len(list(filter(lambda data: data[0] != data[1], zip(payload, MqttPublisher.expected_data)))) + logging.error(f'Payload differ in {differences} positions from expected data. received size: {len(payload)} expected size:' + f'{len(MqttPublisher.expected_data)}') + logging.info(f'Repetitions: {payload.count(MqttPublisher.sample)}') + logging.info(f'Pattern: {MqttPublisher.sample}') + logging.info(f'First : {payload[:DEFAULT_MSG_SIZE]}') + logging.info(f'Last : {payload[-DEFAULT_MSG_SIZE:]}') + matcher = difflib.SequenceMatcher(a=payload, b=MqttPublisher.expected_data) + for match in matcher.get_matching_blocks(): + logging.info(f'Match: {match}') def __enter__(self): # type: (MqttPublisher) -> None @@ -104,8 +118,8 @@ class MqttPublisher: broker_port = self.publish_cfg['broker_port_' + transport] # Start the test - self.print_details("PUBLISH TEST: transport:{}, qos:{}, sequence:{}, enqueue:{}, sample msg:'{}'" - .format(transport, qos, MqttPublisher.published, queue, MqttPublisher.expected_data)) + self.print_details(f'PUBLISH TEST: transport:{transport}, qos:{qos}, sequence:{MqttPublisher.published},' + f"enqueue:{queue}, sample msg:'{MqttPublisher.expected_data}'") try: if transport in ['ws', 'wss']: @@ -123,29 +137,29 @@ class MqttPublisher: self.print_details('Connecting...') self.client.connect(broker_host, broker_port, 60) except Exception: - self.print_details('ENV_TEST_FAILURE: Unexpected error while connecting to broker {}'.format(broker_host)) + self.print_details(f'ENV_TEST_FAILURE: Unexpected error while connecting to broker {broker_host}') raise # Starting a py-client in a separate thread thread1 = Thread(target=self.mqtt_client_task, args=(self.client, self.lock)) thread1.start() self.print_details('Connecting py-client to broker {}:{}...'.format(broker_host, broker_port)) if not MqttPublisher.event_client_connected.wait(timeout=30): - raise ValueError('ENV_TEST_FAILURE: Test script cannot connect to broker: {}'.format(broker_host)) + raise ValueError(f'ENV_TEST_FAILURE: Test script cannot connect to broker: {broker_host}') with self.lock: self.client.subscribe(self.publish_cfg['subscribe_topic'], qos) - self.dut.write(' '.join(str(x) for x in (transport, self.sample_string, self.repeat, MqttPublisher.published, qos, queue)), eol='\n') + self.dut.write(f'{transport} {self.sample_string} {self.repeat} {MqttPublisher.published} {qos} {queue}') try: # waiting till subscribed to defined topic - self.dut.expect(re.compile(r'MQTT_EVENT_SUBSCRIBED'), timeout=30) + self.dut.expect(re.compile(rb'MQTT_EVENT_SUBSCRIBED'), timeout=60) for _ in range(MqttPublisher.published): with self.lock: self.client.publish(self.publish_cfg['publish_topic'], self.sample_string * self.repeat, qos) self.print_details('Publishing...') self.print_details('Checking esp-client received msg published from py-client...') - self.dut.expect(re.compile(r'Correct pattern received exactly x times'), timeout=60) + self.dut.expect(re.compile(rb'Correct pattern received exactly x times'), timeout=60) if not MqttPublisher.event_client_got_all.wait(timeout=60): - raise ValueError('Not all data received from ESP32') - print(' - all data received from ESP32') + raise ValueError('Not all data received from ESP32: {}'.format(transport)) + logging.info(' - all data received from ESP32') finally: self.event_stop_client.set() thread1.join() @@ -168,6 +182,8 @@ class TlsServer: self.client_cert = client_cert self.refuse_connection = refuse_connection self.use_alpn = use_alpn + self.conn = socket.socket() + self.ssl_error = '' def __enter__(self): # type: (TlsServer) -> TlsServer try: @@ -253,12 +269,12 @@ def connection_tests(dut, cases, dut_ip): # type: (Dut, dict, str) -> None server_port = 2222 def teardown_connection_suite() -> None: - dut.write('conn teardown 0 0') + dut.write('conn teardown 0 0\n') def start_connection_case(case, desc): # type: (str, str) -> Any print('Starting {}: {}'.format(case, desc)) case_id = cases[case] - dut.write('conn {} {} {}'.format(ip, server_port, case_id)) + dut.write('conn {} {} {}\n'.format(ip, server_port, case_id)) dut.expect('Test case:{} started'.format(case_id)) return case_id @@ -350,13 +366,7 @@ def test_app_protocol_mqtt_publish_connect(dut: Dut) -> None: esp_ip = dut.expect(r'IPv4 address: (\d+\.\d+\.\d+\.\d+)[^\d]', timeout=30).group(1).decode() print('Got IP={}'.format(esp_ip)) - if not os.getenv('MQTT_SKIP_CONNECT_TEST'): - connection_tests(dut,cases,esp_ip) - - # - # start publish tests only if enabled in the environment (for weekend tests only) - if not os.getenv('MQTT_PUBLISH_TEST'): - return + connection_tests(dut,cases,esp_ip) # Get publish test configuration try: @@ -375,15 +385,9 @@ def test_app_protocol_mqtt_publish_connect(dut: Dut) -> None: publish_cfg['broker_host_wss'], publish_cfg['broker_port_wss'] = get_host_port_from_dut(dut, 'EXAMPLE_BROKER_WSS_URI') except Exception: - print('ENV_TEST_FAILURE: Some mandatory PUBLISH test case not found in sdkconfig') + logging.error('ENV_TEST_FAILURE: Some mandatory PUBLISH test case not found in sdkconfig') raise - def start_publish_case(transport, qos, repeat, published, queue): # type: (str, int, int, int, int) -> None - print('Starting Publish test: transport:{}, qos:{}, nr_of_msgs:{}, msg_size:{}, enqueue:{}' - .format(transport, qos, published, repeat * DEFAULT_MSG_SIZE, queue)) - with MqttPublisher(dut, transport, qos, repeat, published, queue, publish_cfg): - pass - # Initialize message sizes and repeat counts (if defined in the environment) messages = [] for i in count(0): @@ -401,14 +405,17 @@ def test_app_protocol_mqtt_publish_connect(dut: Dut) -> None: ] # Iterate over all publish message properties - for qos in [0, 1, 2]: - for transport in ['tcp', 'ssl', 'ws', 'wss']: - for q in [0, 1]: - if publish_cfg['broker_host_' + transport] is None: - print('Skipping transport: {}...'.format(transport)) - continue + for transport in ['tcp', 'ssl', 'ws', 'wss']: + if publish_cfg['broker_host_' + transport] is None: + print('Skipping transport: {}...'.format(transport)) + continue + for enqueue in [0, 1]: + for qos in [0, 1, 2]: for msg in messages: - start_publish_case(transport, qos, msg['len'], msg['repeat'], q) + logging.info(f'Starting Publish test: transport:{transport}, qos:{qos}, nr_of_msgs:{msg["repeat"]},' + f'msg_size:{msg["len"] * DEFAULT_MSG_SIZE}, enqueue:{enqueue}') + with MqttPublisher(dut, transport, qos, msg['len'], msg['repeat'], enqueue, publish_cfg): + pass if __name__ == '__main__': From accb154b356f0c85e0a22a44624b2f7f4806dfc6 Mon Sep 17 00:00:00 2001 From: Armando Date: Thu, 17 Aug 2023 10:45:23 +0800 Subject: [PATCH 177/231] feat(ci): Enable p4 example, test_apps and unit tests CI build --- components/mqtt/test_apps/test_mqtt/README.md | 4 ++-- components/mqtt/test_apps/test_mqtt5/README.md | 4 ++-- tools/test_apps/protocols/mqtt/build_test/README.md | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/components/mqtt/test_apps/test_mqtt/README.md b/components/mqtt/test_apps/test_mqtt/README.md index a8b7833..bf47d80 100644 --- a/components/mqtt/test_apps/test_mqtt/README.md +++ b/components/mqtt/test_apps/test_mqtt/README.md @@ -1,2 +1,2 @@ -| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C6 | ESP32-H2 | ESP32-S2 | ESP32-S3 | -| ----------------- | ----- | -------- | -------- | -------- | -------- | -------- | -------- | +| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C6 | ESP32-H2 | ESP32-P4 | ESP32-S2 | ESP32-S3 | +| ----------------- | ----- | -------- | -------- | -------- | -------- | -------- | -------- | -------- | diff --git a/components/mqtt/test_apps/test_mqtt5/README.md b/components/mqtt/test_apps/test_mqtt5/README.md index a8b7833..bf47d80 100644 --- a/components/mqtt/test_apps/test_mqtt5/README.md +++ b/components/mqtt/test_apps/test_mqtt5/README.md @@ -1,2 +1,2 @@ -| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C6 | ESP32-H2 | ESP32-S2 | ESP32-S3 | -| ----------------- | ----- | -------- | -------- | -------- | -------- | -------- | -------- | +| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C6 | ESP32-H2 | ESP32-P4 | ESP32-S2 | ESP32-S3 | +| ----------------- | ----- | -------- | -------- | -------- | -------- | -------- | -------- | -------- | diff --git a/tools/test_apps/protocols/mqtt/build_test/README.md b/tools/test_apps/protocols/mqtt/build_test/README.md index 9a4c5c5..5b49857 100644 --- a/tools/test_apps/protocols/mqtt/build_test/README.md +++ b/tools/test_apps/protocols/mqtt/build_test/README.md @@ -1,5 +1,5 @@ -| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C6 | ESP32-H2 | ESP32-S2 | ESP32-S3 | -| ----------------- | ----- | -------- | -------- | -------- | -------- | -------- | -------- | +| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C6 | ESP32-H2 | ESP32-P4 | ESP32-S2 | ESP32-S3 | +| ----------------- | ----- | -------- | -------- | -------- | -------- | -------- | -------- | -------- | # Build only test for C++ From ca9ca161ace838ef8351f8e552faf61f732fadd3 Mon Sep 17 00:00:00 2001 From: Andrii Date: Thu, 10 Aug 2023 10:58:44 +0200 Subject: [PATCH 178/231] fix(mqtt): Unite supported components tags under common structure --- examples/protocols/mqtt/ssl/main/app_main.c | 12 ++++++------ examples/protocols/mqtt/ssl_ds/main/app_main.c | 10 +++++----- .../protocols/mqtt/ssl_mutual_auth/main/app_main.c | 10 +++++----- examples/protocols/mqtt/ssl_psk/main/app_main.c | 10 +++++----- examples/protocols/mqtt/tcp/main/app_main.c | 8 ++++---- examples/protocols/mqtt/ws/main/app_main.c | 14 +++++++------- examples/protocols/mqtt/wss/main/app_main.c | 12 ++++++------ examples/protocols/mqtt5/main/app_main.c | 12 ++++++------ .../protocols/mqtt/build_test/main/mqtt_app.cpp | 14 +++++++------- .../mqtt/publish_connect_test/main/connect_test.c | 2 +- .../main/publish_connect_test.c | 10 +++++----- .../mqtt/publish_connect_test/main/publish_test.c | 2 +- 12 files changed, 58 insertions(+), 58 deletions(-) diff --git a/examples/protocols/mqtt/ssl/main/app_main.c b/examples/protocols/mqtt/ssl/main/app_main.c index 570bdd3..20cf180 100644 --- a/examples/protocols/mqtt/ssl/main/app_main.c +++ b/examples/protocols/mqtt/ssl/main/app_main.c @@ -24,7 +24,7 @@ #include "esp_ota_ops.h" #include -static const char *TAG = "MQTTS_EXAMPLE"; +static const char *TAG = "mqtts_example"; #if CONFIG_BROKER_CERTIFICATE_OVERRIDDEN == 1 @@ -145,11 +145,11 @@ void app_main(void) esp_log_level_set("*", ESP_LOG_INFO); esp_log_level_set("esp-tls", ESP_LOG_VERBOSE); - esp_log_level_set("MQTT_CLIENT", ESP_LOG_VERBOSE); - esp_log_level_set("MQTT_EXAMPLE", ESP_LOG_VERBOSE); - esp_log_level_set("TRANSPORT_BASE", ESP_LOG_VERBOSE); - esp_log_level_set("TRANSPORT", ESP_LOG_VERBOSE); - esp_log_level_set("OUTBOX", ESP_LOG_VERBOSE); + esp_log_level_set("mqtt_client", ESP_LOG_VERBOSE); + esp_log_level_set("mqtt_example", ESP_LOG_VERBOSE); + esp_log_level_set("transport_base", ESP_LOG_VERBOSE); + esp_log_level_set("transport", ESP_LOG_VERBOSE); + esp_log_level_set("outbox", ESP_LOG_VERBOSE); ESP_ERROR_CHECK(nvs_flash_init()); ESP_ERROR_CHECK(esp_netif_init()); diff --git a/examples/protocols/mqtt/ssl_ds/main/app_main.c b/examples/protocols/mqtt/ssl_ds/main/app_main.c index 160392a..758995b 100644 --- a/examples/protocols/mqtt/ssl_ds/main/app_main.c +++ b/examples/protocols/mqtt/ssl_ds/main/app_main.c @@ -30,7 +30,7 @@ #include "rsa_sign_alt.h" #include "esp_secure_cert_read.h" -static const char *TAG = "MQTTS_EXAMPLE"; +static const char *TAG = "mqtts_example"; extern const uint8_t server_cert_pem_start[] asm("_binary_mosquitto_org_crt_start"); extern const uint8_t server_cert_pem_end[] asm("_binary_mosquitto_org_crt_end"); @@ -137,10 +137,10 @@ void app_main(void) ESP_LOGI(TAG, "[APP] IDF version: %s", esp_get_idf_version()); esp_log_level_set("*", ESP_LOG_INFO); - esp_log_level_set("MQTT_CLIENT", ESP_LOG_VERBOSE); - esp_log_level_set("TRANSPORT_BASE", ESP_LOG_VERBOSE); - esp_log_level_set("TRANSPORT", ESP_LOG_VERBOSE); - esp_log_level_set("OUTBOX", ESP_LOG_VERBOSE); + esp_log_level_set("mqtt_client", ESP_LOG_VERBOSE); + esp_log_level_set("transport_base", ESP_LOG_VERBOSE); + esp_log_level_set("transport", ESP_LOG_VERBOSE); + esp_log_level_set("outbox", ESP_LOG_VERBOSE); ESP_ERROR_CHECK(nvs_flash_init()); ESP_ERROR_CHECK(esp_netif_init()); diff --git a/examples/protocols/mqtt/ssl_mutual_auth/main/app_main.c b/examples/protocols/mqtt/ssl_mutual_auth/main/app_main.c index 9798cb9..02956f6 100644 --- a/examples/protocols/mqtt/ssl_mutual_auth/main/app_main.c +++ b/examples/protocols/mqtt/ssl_mutual_auth/main/app_main.c @@ -29,7 +29,7 @@ #include "esp_log.h" #include "mqtt_client.h" -static const char *TAG = "MQTTS_EXAMPLE"; +static const char *TAG = "mqtts_example"; extern const uint8_t client_cert_pem_start[] asm("_binary_client_crt_start"); extern const uint8_t client_cert_pem_end[] asm("_binary_client_crt_end"); @@ -136,10 +136,10 @@ void app_main(void) ESP_LOGI(TAG, "[APP] IDF version: %s", esp_get_idf_version()); esp_log_level_set("*", ESP_LOG_INFO); - esp_log_level_set("MQTT_CLIENT", ESP_LOG_VERBOSE); - esp_log_level_set("TRANSPORT_BASE", ESP_LOG_VERBOSE); - esp_log_level_set("TRANSPORT", ESP_LOG_VERBOSE); - esp_log_level_set("OUTBOX", ESP_LOG_VERBOSE); + esp_log_level_set("mqtt_client", ESP_LOG_VERBOSE); + esp_log_level_set("transport_base", ESP_LOG_VERBOSE); + esp_log_level_set("transport", ESP_LOG_VERBOSE); + esp_log_level_set("outbox", ESP_LOG_VERBOSE); ESP_ERROR_CHECK(nvs_flash_init()); ESP_ERROR_CHECK(esp_netif_init()); diff --git a/examples/protocols/mqtt/ssl_psk/main/app_main.c b/examples/protocols/mqtt/ssl_psk/main/app_main.c index 67df863..4d2bb10 100644 --- a/examples/protocols/mqtt/ssl_psk/main/app_main.c +++ b/examples/protocols/mqtt/ssl_psk/main/app_main.c @@ -36,7 +36,7 @@ */ #define EXAMPLE_BROKER_URI "mqtts://192.168.0.2" -static const char *TAG = "MQTTS_EXAMPLE"; +static const char *TAG = "mqtts_example"; /* * Define psk key and hint as defined in mqtt broker @@ -131,11 +131,11 @@ void app_main(void) ESP_LOGI(TAG, "[APP] IDF version: %s", esp_get_idf_version()); esp_log_level_set("*", ESP_LOG_INFO); - esp_log_level_set("MQTT_CLIENT", ESP_LOG_VERBOSE); - esp_log_level_set("TRANSPORT_BASE", ESP_LOG_VERBOSE); + esp_log_level_set("mqtt_client", ESP_LOG_VERBOSE); + esp_log_level_set("transport_base", ESP_LOG_VERBOSE); esp_log_level_set("esp-tls", ESP_LOG_VERBOSE); - esp_log_level_set("TRANSPORT", ESP_LOG_VERBOSE); - esp_log_level_set("OUTBOX", ESP_LOG_VERBOSE); + esp_log_level_set("transport", ESP_LOG_VERBOSE); + esp_log_level_set("outbox", ESP_LOG_VERBOSE); ESP_ERROR_CHECK(nvs_flash_init()); ESP_ERROR_CHECK(esp_netif_init()); diff --git a/examples/protocols/mqtt/tcp/main/app_main.c b/examples/protocols/mqtt/tcp/main/app_main.c index 06ff1ad..6fe652b 100644 --- a/examples/protocols/mqtt/tcp/main/app_main.c +++ b/examples/protocols/mqtt/tcp/main/app_main.c @@ -30,7 +30,7 @@ #include "esp_log.h" #include "mqtt_client.h" -static const char *TAG = "MQTT_EXAMPLE"; +static const char *TAG = "mqtt_example"; static void log_error_if_nonzero(const char *message, int error_code) @@ -151,10 +151,10 @@ void app_main(void) esp_log_level_set("*", ESP_LOG_INFO); esp_log_level_set("mqtt_client", ESP_LOG_VERBOSE); - esp_log_level_set("MQTT_EXAMPLE", ESP_LOG_VERBOSE); - esp_log_level_set("TRANSPORT_BASE", ESP_LOG_VERBOSE); + esp_log_level_set("mqtt_example", ESP_LOG_VERBOSE); + esp_log_level_set("transport_base", ESP_LOG_VERBOSE); esp_log_level_set("esp-tls", ESP_LOG_VERBOSE); - esp_log_level_set("TRANSPORT", ESP_LOG_VERBOSE); + esp_log_level_set("transport", ESP_LOG_VERBOSE); esp_log_level_set("outbox", ESP_LOG_VERBOSE); ESP_ERROR_CHECK(nvs_flash_init()); diff --git a/examples/protocols/mqtt/ws/main/app_main.c b/examples/protocols/mqtt/ws/main/app_main.c index 59f09cf..cebfb9a 100644 --- a/examples/protocols/mqtt/ws/main/app_main.c +++ b/examples/protocols/mqtt/ws/main/app_main.c @@ -29,7 +29,7 @@ #include "esp_log.h" #include "mqtt_client.h" -static const char *TAG = "MQTTWS_EXAMPLE"; +static const char *TAG = "mqttws_example"; static void log_error_if_nonzero(const char *message, int error_code) { @@ -123,12 +123,12 @@ void app_main(void) ESP_LOGI(TAG, "[APP] IDF version: %s", esp_get_idf_version()); esp_log_level_set("*", ESP_LOG_INFO); - esp_log_level_set("MQTT_CLIENT", ESP_LOG_VERBOSE); - esp_log_level_set("MQTT_EXAMPLE", ESP_LOG_VERBOSE); - esp_log_level_set("TRANSPORT_BASE", ESP_LOG_VERBOSE); - esp_log_level_set("TRANSPORT_WS", ESP_LOG_VERBOSE); - esp_log_level_set("TRANSPORT", ESP_LOG_VERBOSE); - esp_log_level_set("OUTBOX", ESP_LOG_VERBOSE); + esp_log_level_set("mqtt_client", ESP_LOG_VERBOSE); + esp_log_level_set("mqtt_example", ESP_LOG_VERBOSE); + esp_log_level_set("transport_base", ESP_LOG_VERBOSE); + esp_log_level_set("transport_ws", ESP_LOG_VERBOSE); + esp_log_level_set("transport", ESP_LOG_VERBOSE); + esp_log_level_set("outbox", ESP_LOG_VERBOSE); ESP_ERROR_CHECK(nvs_flash_init()); ESP_ERROR_CHECK(esp_netif_init()); diff --git a/examples/protocols/mqtt/wss/main/app_main.c b/examples/protocols/mqtt/wss/main/app_main.c index f913051..168b46a 100644 --- a/examples/protocols/mqtt/wss/main/app_main.c +++ b/examples/protocols/mqtt/wss/main/app_main.c @@ -29,7 +29,7 @@ #include "esp_log.h" #include "mqtt_client.h" -static const char *TAG = "MQTTWSS_EXAMPLE"; +static const char *TAG = "mqttwss_example"; #if CONFIG_BROKER_CERTIFICATE_OVERRIDDEN == 1 @@ -115,11 +115,11 @@ void app_main(void) ESP_LOGI(TAG, "[APP] IDF version: %s", esp_get_idf_version()); esp_log_level_set("*", ESP_LOG_INFO); - esp_log_level_set("MQTT_CLIENT", ESP_LOG_VERBOSE); - esp_log_level_set("MQTT_EXAMPLE", ESP_LOG_VERBOSE); - esp_log_level_set("TRANSPORT_BASE", ESP_LOG_VERBOSE); - esp_log_level_set("TRANSPORT", ESP_LOG_VERBOSE); - esp_log_level_set("OUTBOX", ESP_LOG_VERBOSE); + esp_log_level_set("mqtt_client", ESP_LOG_VERBOSE); + esp_log_level_set("mqtt_example", ESP_LOG_VERBOSE); + esp_log_level_set("transport_base", ESP_LOG_VERBOSE); + esp_log_level_set("transport", ESP_LOG_VERBOSE); + esp_log_level_set("outbox", ESP_LOG_VERBOSE); ESP_ERROR_CHECK(nvs_flash_init()); ESP_ERROR_CHECK(esp_netif_init()); diff --git a/examples/protocols/mqtt5/main/app_main.c b/examples/protocols/mqtt5/main/app_main.c index 461ca4c..746017c 100644 --- a/examples/protocols/mqtt5/main/app_main.c +++ b/examples/protocols/mqtt5/main/app_main.c @@ -16,7 +16,7 @@ #include "esp_log.h" #include "mqtt_client.h" -static const char *TAG = "MQTT5_EXAMPLE"; +static const char *TAG = "mqtt5_example"; static void log_error_if_nonzero(const char *message, int error_code) { @@ -269,12 +269,12 @@ void app_main(void) ESP_LOGI(TAG, "[APP] IDF version: %s", esp_get_idf_version()); esp_log_level_set("*", ESP_LOG_INFO); - esp_log_level_set("MQTT_CLIENT", ESP_LOG_VERBOSE); - esp_log_level_set("MQTT_EXAMPLE", ESP_LOG_VERBOSE); - esp_log_level_set("TRANSPORT_BASE", ESP_LOG_VERBOSE); + esp_log_level_set("mqtt_client", ESP_LOG_VERBOSE); + esp_log_level_set("mqtt_example", ESP_LOG_VERBOSE); + esp_log_level_set("transport_base", ESP_LOG_VERBOSE); esp_log_level_set("esp-tls", ESP_LOG_VERBOSE); - esp_log_level_set("TRANSPORT", ESP_LOG_VERBOSE); - esp_log_level_set("OUTBOX", ESP_LOG_VERBOSE); + esp_log_level_set("transport", ESP_LOG_VERBOSE); + esp_log_level_set("outbox", ESP_LOG_VERBOSE); ESP_ERROR_CHECK(nvs_flash_init()); ESP_ERROR_CHECK(esp_netif_init()); diff --git a/tools/test_apps/protocols/mqtt/build_test/main/mqtt_app.cpp b/tools/test_apps/protocols/mqtt/build_test/main/mqtt_app.cpp index 4a3f8a5..2960f41 100644 --- a/tools/test_apps/protocols/mqtt/build_test/main/mqtt_app.cpp +++ b/tools/test_apps/protocols/mqtt/build_test/main/mqtt_app.cpp @@ -15,7 +15,7 @@ #include "esp_log.h" #include "mqtt_client.h" -static const char *TAG = "MQTT_EXAMPLE"; +static const char *TAG = "mqtt_example"; static esp_err_t mqtt_event_handler_cb(esp_mqtt_event_handle_t event) @@ -86,12 +86,12 @@ extern "C" void app_main(void) ESP_LOGI(TAG, "[APP] IDF version: %s", esp_get_idf_version()); esp_log_level_set("*", ESP_LOG_INFO); - esp_log_level_set("MQTT_CLIENT", ESP_LOG_VERBOSE); - esp_log_level_set("MQTT_EXAMPLE", ESP_LOG_VERBOSE); - esp_log_level_set("TRANSPORT_TCP", ESP_LOG_VERBOSE); - esp_log_level_set("TRANSPORT_SSL", ESP_LOG_VERBOSE); - esp_log_level_set("TRANSPORT", ESP_LOG_VERBOSE); - esp_log_level_set("OUTBOX", ESP_LOG_VERBOSE); + esp_log_level_set("mqtt_client", ESP_LOG_VERBOSE); + esp_log_level_set("mqtt_example", ESP_LOG_VERBOSE); + esp_log_level_set("transport_tcp", ESP_LOG_VERBOSE); + esp_log_level_set("transport_ssl", ESP_LOG_VERBOSE); + esp_log_level_set("transport", ESP_LOG_VERBOSE); + esp_log_level_set("outbox", ESP_LOG_VERBOSE); ESP_ERROR_CHECK(nvs_flash_init()); ESP_ERROR_CHECK(esp_netif_init()); diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/main/connect_test.c b/tools/test_apps/protocols/mqtt/publish_connect_test/main/connect_test.c index be07e5a..e8132ef 100644 --- a/tools/test_apps/protocols/mqtt/publish_connect_test/main/connect_test.c +++ b/tools/test_apps/protocols/mqtt/publish_connect_test/main/connect_test.c @@ -33,7 +33,7 @@ extern const uint8_t client_pwd_key[] asm("_binary_client_pwd_key_start"); extern const uint8_t client_inv_crt[] asm("_binary_client_inv_crt_start"); extern const uint8_t client_no_pwd_key[] asm("_binary_client_no_pwd_key_start"); -static const char *TAG = "CONNECT_TEST"; +static const char *TAG = "connect_test"; static esp_mqtt_client_handle_t mqtt_client = NULL; static int running_test_case = 0; diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/main/publish_connect_test.c b/tools/test_apps/protocols/mqtt/publish_connect_test/main/publish_connect_test.c index 84da251..b793215 100644 --- a/tools/test_apps/protocols/mqtt/publish_connect_test/main/publish_connect_test.c +++ b/tools/test_apps/protocols/mqtt/publish_connect_test/main/publish_connect_test.c @@ -16,7 +16,7 @@ #include "protocol_examples_common.h" #include "esp_log.h" -static const char *TAG = "PUBLISH_CONNECT_TEST"; +static const char *TAG = "publish_connect_test"; void connection_test(const char *line); void publish_test(const char *line); @@ -45,10 +45,10 @@ void app_main(void) ESP_LOGI(TAG, "[APP] IDF version: %s", esp_get_idf_version()); esp_log_level_set("*", ESP_LOG_INFO); - esp_log_level_set("MQTT_CLIENT", ESP_LOG_VERBOSE); - esp_log_level_set("TRANSPORT_BASE", ESP_LOG_VERBOSE); - esp_log_level_set("TRANSPORT", ESP_LOG_VERBOSE); - esp_log_level_set("OUTBOX", ESP_LOG_VERBOSE); + esp_log_level_set("mqtt_client", ESP_LOG_VERBOSE); + esp_log_level_set("transport_base", ESP_LOG_VERBOSE); + esp_log_level_set("transport", ESP_LOG_VERBOSE); + esp_log_level_set("outbox", ESP_LOG_VERBOSE); ESP_ERROR_CHECK(nvs_flash_init()); ESP_ERROR_CHECK(esp_netif_init()); diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/main/publish_test.c b/tools/test_apps/protocols/mqtt/publish_connect_test/main/publish_test.c index 8b74f4e..d9ddf08 100644 --- a/tools/test_apps/protocols/mqtt/publish_connect_test/main/publish_test.c +++ b/tools/test_apps/protocols/mqtt/publish_connect_test/main/publish_test.c @@ -19,7 +19,7 @@ #include "mqtt_client.h" #include "sdkconfig.h" -static const char *TAG = "PUBLISH_TEST"; +static const char *TAG = "publish_test"; static EventGroupHandle_t mqtt_event_group; const static int CONNECTED_BIT = BIT0; From 22ce1828530d92850175f502bf8bd8e26d1caa04 Mon Sep 17 00:00:00 2001 From: caixinying-git Date: Wed, 13 Sep 2023 17:05:23 +0800 Subject: [PATCH 179/231] docs: update format issues left in EN docs --- docs/en/api-reference/protocols/mqtt.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/en/api-reference/protocols/mqtt.rst b/docs/en/api-reference/protocols/mqtt.rst index 7e8392e..4160e8e 100644 --- a/docs/en/api-reference/protocols/mqtt.rst +++ b/docs/en/api-reference/protocols/mqtt.rst @@ -189,7 +189,7 @@ The following events may be posted by the MQTT client: * ``MQTT_EVENT_SUBSCRIBED``: The broker has acknowledged the client's subscribe request. The event data contains the message ID of the subscribe message. * ``MQTT_EVENT_UNSUBSCRIBED``: The broker has acknowledged the client's unsubscribe request. The event data contains the message ID of the unsubscribe message. * ``MQTT_EVENT_PUBLISHED``: The broker has acknowledged the client's publish message. This is only posted for QoS level 1 and 2, as level 0 does not use acknowledgements. The event data contains the message ID of the publish message. -* ``MQTT_EVENT_DATA``: The client has received a publish message. The event data contains: message ID, name of the topic it was published to, received data and its length. For data that exceeds the internal buffer, multiple ``MQTT_EVENT_DATA`` events are posted and :cpp:member:`current_data_offset ` and :cpp:member:`total_data_len` from event data updated to keep track of the fragmented message. +* ``MQTT_EVENT_DATA``: The client has received a publish message. The event data contains: message ID, name of the topic it was published to, received data and its length. For data that exceeds the internal buffer, multiple ``MQTT_EVENT_DATA`` events are posted and :cpp:member:`current_data_offset ` and :cpp:member:`total_data_len ` from event data updated to keep track of the fragmented message. * ``MQTT_EVENT_ERROR``: The client has encountered an error. The field :cpp:type:`error_handle ` in the event data contains :cpp:type:`error_type ` that can be used to identify the error. The type of error determines which parts of the :cpp:type:`error_handle ` struct is filled. API Reference From c462db569ed9ac208e784128dd99b9e001f6675f Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Fri, 4 Aug 2023 15:00:15 +0200 Subject: [PATCH 180/231] feat(examples): add local components via idf_component.yml Specifying all the dependencies (managed and local) in the manifest makes it easier for users to see every component the example depends on. --- examples/protocols/mqtt/ssl/CMakeLists.txt | 3 --- examples/protocols/mqtt/ssl/main/idf_component.yml | 3 +++ examples/protocols/mqtt/ssl_ds/CMakeLists.txt | 3 --- examples/protocols/mqtt/ssl_ds/main/idf_component.yml | 2 ++ examples/protocols/mqtt/ssl_mutual_auth/CMakeLists.txt | 3 --- examples/protocols/mqtt/ssl_mutual_auth/main/idf_component.yml | 3 +++ examples/protocols/mqtt/ssl_psk/CMakeLists.txt | 3 --- examples/protocols/mqtt/ssl_psk/main/idf_component.yml | 3 +++ examples/protocols/mqtt/tcp/CMakeLists.txt | 3 --- examples/protocols/mqtt/tcp/main/idf_component.yml | 3 +++ examples/protocols/mqtt/ws/CMakeLists.txt | 3 --- examples/protocols/mqtt/ws/main/idf_component.yml | 3 +++ examples/protocols/mqtt/wss/CMakeLists.txt | 3 --- examples/protocols/mqtt/wss/main/idf_component.yml | 3 +++ examples/protocols/mqtt5/CMakeLists.txt | 3 --- examples/protocols/mqtt5/main/idf_component.yml | 3 +++ 16 files changed, 23 insertions(+), 24 deletions(-) create mode 100644 examples/protocols/mqtt/ssl/main/idf_component.yml create mode 100644 examples/protocols/mqtt/ssl_mutual_auth/main/idf_component.yml create mode 100644 examples/protocols/mqtt/ssl_psk/main/idf_component.yml create mode 100644 examples/protocols/mqtt/tcp/main/idf_component.yml create mode 100644 examples/protocols/mqtt/ws/main/idf_component.yml create mode 100644 examples/protocols/mqtt/wss/main/idf_component.yml create mode 100644 examples/protocols/mqtt5/main/idf_component.yml diff --git a/examples/protocols/mqtt/ssl/CMakeLists.txt b/examples/protocols/mqtt/ssl/CMakeLists.txt index 3d2fba1..994f2f2 100644 --- a/examples/protocols/mqtt/ssl/CMakeLists.txt +++ b/examples/protocols/mqtt/ssl/CMakeLists.txt @@ -2,9 +2,6 @@ # in this exact order for cmake to work correctly cmake_minimum_required(VERSION 3.16) -# (Not part of the boilerplate) -# This example uses an extra component for common functions such as Wi-Fi and Ethernet connection. -set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common) include($ENV{IDF_PATH}/tools/cmake/project.cmake) project(mqtt_ssl) diff --git a/examples/protocols/mqtt/ssl/main/idf_component.yml b/examples/protocols/mqtt/ssl/main/idf_component.yml new file mode 100644 index 0000000..7181948 --- /dev/null +++ b/examples/protocols/mqtt/ssl/main/idf_component.yml @@ -0,0 +1,3 @@ +dependencies: + protocol_examples_common: + path: ${IDF_PATH}/examples/common_components/protocol_examples_common diff --git a/examples/protocols/mqtt/ssl_ds/CMakeLists.txt b/examples/protocols/mqtt/ssl_ds/CMakeLists.txt index 144cf8b..ce04bb0 100644 --- a/examples/protocols/mqtt/ssl_ds/CMakeLists.txt +++ b/examples/protocols/mqtt/ssl_ds/CMakeLists.txt @@ -2,9 +2,6 @@ # in this exact order for cmake to work correctly cmake_minimum_required(VERSION 3.16) -# (Not part of the boilerplate) -# This example uses an extra component for common functions such as Wi-Fi and Ethernet connection. -set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common) include($ENV{IDF_PATH}/tools/cmake/project.cmake) project(mqtt_ssl_ds) diff --git a/examples/protocols/mqtt/ssl_ds/main/idf_component.yml b/examples/protocols/mqtt/ssl_ds/main/idf_component.yml index 8dd90a3..947ec18 100644 --- a/examples/protocols/mqtt/ssl_ds/main/idf_component.yml +++ b/examples/protocols/mqtt/ssl_ds/main/idf_component.yml @@ -1,3 +1,5 @@ ## IDF Component Manager Manifest File dependencies: espressif/esp_secure_cert_mgr: "^2.0.2" + protocol_examples_common: + path: ${IDF_PATH}/examples/common_components/protocol_examples_common diff --git a/examples/protocols/mqtt/ssl_mutual_auth/CMakeLists.txt b/examples/protocols/mqtt/ssl_mutual_auth/CMakeLists.txt index 14512ad..fc3d759 100644 --- a/examples/protocols/mqtt/ssl_mutual_auth/CMakeLists.txt +++ b/examples/protocols/mqtt/ssl_mutual_auth/CMakeLists.txt @@ -2,9 +2,6 @@ # in this exact order for cmake to work correctly cmake_minimum_required(VERSION 3.16) -# (Not part of the boilerplate) -# This example uses an extra component for common functions such as Wi-Fi and Ethernet connection. -set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common) include($ENV{IDF_PATH}/tools/cmake/project.cmake) project(mqtt_ssl_mutual_auth) diff --git a/examples/protocols/mqtt/ssl_mutual_auth/main/idf_component.yml b/examples/protocols/mqtt/ssl_mutual_auth/main/idf_component.yml new file mode 100644 index 0000000..7181948 --- /dev/null +++ b/examples/protocols/mqtt/ssl_mutual_auth/main/idf_component.yml @@ -0,0 +1,3 @@ +dependencies: + protocol_examples_common: + path: ${IDF_PATH}/examples/common_components/protocol_examples_common diff --git a/examples/protocols/mqtt/ssl_psk/CMakeLists.txt b/examples/protocols/mqtt/ssl_psk/CMakeLists.txt index a4e8aa6..d9c1a5f 100644 --- a/examples/protocols/mqtt/ssl_psk/CMakeLists.txt +++ b/examples/protocols/mqtt/ssl_psk/CMakeLists.txt @@ -2,9 +2,6 @@ # in this exact order for cmake to work correctly cmake_minimum_required(VERSION 3.16) -# (Not part of the boilerplate) -# This example uses an extra component for common functions such as Wi-Fi and Ethernet connection. -set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common) include($ENV{IDF_PATH}/tools/cmake/project.cmake) project(mqtt_ssl_psk) diff --git a/examples/protocols/mqtt/ssl_psk/main/idf_component.yml b/examples/protocols/mqtt/ssl_psk/main/idf_component.yml new file mode 100644 index 0000000..7181948 --- /dev/null +++ b/examples/protocols/mqtt/ssl_psk/main/idf_component.yml @@ -0,0 +1,3 @@ +dependencies: + protocol_examples_common: + path: ${IDF_PATH}/examples/common_components/protocol_examples_common diff --git a/examples/protocols/mqtt/tcp/CMakeLists.txt b/examples/protocols/mqtt/tcp/CMakeLists.txt index 7dd960d..d6d9432 100644 --- a/examples/protocols/mqtt/tcp/CMakeLists.txt +++ b/examples/protocols/mqtt/tcp/CMakeLists.txt @@ -2,9 +2,6 @@ # in this exact order for cmake to work correctly cmake_minimum_required(VERSION 3.16) -# (Not part of the boilerplate) -# This example uses an extra component for common functions such as Wi-Fi and Ethernet connection. -set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common) include($ENV{IDF_PATH}/tools/cmake/project.cmake) project(mqtt_tcp) diff --git a/examples/protocols/mqtt/tcp/main/idf_component.yml b/examples/protocols/mqtt/tcp/main/idf_component.yml new file mode 100644 index 0000000..7181948 --- /dev/null +++ b/examples/protocols/mqtt/tcp/main/idf_component.yml @@ -0,0 +1,3 @@ +dependencies: + protocol_examples_common: + path: ${IDF_PATH}/examples/common_components/protocol_examples_common diff --git a/examples/protocols/mqtt/ws/CMakeLists.txt b/examples/protocols/mqtt/ws/CMakeLists.txt index 6c84eb7..475dd9e 100644 --- a/examples/protocols/mqtt/ws/CMakeLists.txt +++ b/examples/protocols/mqtt/ws/CMakeLists.txt @@ -2,9 +2,6 @@ # in this exact order for cmake to work correctly cmake_minimum_required(VERSION 3.16) -# (Not part of the boilerplate) -# This example uses an extra component for common functions such as Wi-Fi and Ethernet connection. -set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common) include($ENV{IDF_PATH}/tools/cmake/project.cmake) project(mqtt_websocket) diff --git a/examples/protocols/mqtt/ws/main/idf_component.yml b/examples/protocols/mqtt/ws/main/idf_component.yml new file mode 100644 index 0000000..7181948 --- /dev/null +++ b/examples/protocols/mqtt/ws/main/idf_component.yml @@ -0,0 +1,3 @@ +dependencies: + protocol_examples_common: + path: ${IDF_PATH}/examples/common_components/protocol_examples_common diff --git a/examples/protocols/mqtt/wss/CMakeLists.txt b/examples/protocols/mqtt/wss/CMakeLists.txt index 2a62440..20c1fde 100644 --- a/examples/protocols/mqtt/wss/CMakeLists.txt +++ b/examples/protocols/mqtt/wss/CMakeLists.txt @@ -2,9 +2,6 @@ # in this exact order for cmake to work correctly cmake_minimum_required(VERSION 3.16) -# (Not part of the boilerplate) -# This example uses an extra component for common functions such as Wi-Fi and Ethernet connection. -set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common) include($ENV{IDF_PATH}/tools/cmake/project.cmake) project(mqtt_websocket_secure) diff --git a/examples/protocols/mqtt/wss/main/idf_component.yml b/examples/protocols/mqtt/wss/main/idf_component.yml new file mode 100644 index 0000000..7181948 --- /dev/null +++ b/examples/protocols/mqtt/wss/main/idf_component.yml @@ -0,0 +1,3 @@ +dependencies: + protocol_examples_common: + path: ${IDF_PATH}/examples/common_components/protocol_examples_common diff --git a/examples/protocols/mqtt5/CMakeLists.txt b/examples/protocols/mqtt5/CMakeLists.txt index 7764590..19304b4 100644 --- a/examples/protocols/mqtt5/CMakeLists.txt +++ b/examples/protocols/mqtt5/CMakeLists.txt @@ -2,9 +2,6 @@ # in this exact order for cmake to work correctly cmake_minimum_required(VERSION 3.16) -# (Not part of the boilerplate) -# This example uses an extra component for common functions such as Wi-Fi and Ethernet connection. -set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common) include($ENV{IDF_PATH}/tools/cmake/project.cmake) project(mqtt5) diff --git a/examples/protocols/mqtt5/main/idf_component.yml b/examples/protocols/mqtt5/main/idf_component.yml new file mode 100644 index 0000000..7181948 --- /dev/null +++ b/examples/protocols/mqtt5/main/idf_component.yml @@ -0,0 +1,3 @@ +dependencies: + protocol_examples_common: + path: ${IDF_PATH}/examples/common_components/protocol_examples_common From 7f7dc2b3c6311ce262e0e6188fab8ca8cd30d51d Mon Sep 17 00:00:00 2001 From: Chen Yudong Date: Wed, 20 Sep 2023 19:09:47 +0800 Subject: [PATCH 181/231] fix(ci): change build-test-rules files folder --- components/mqtt/{ => test_apps}/.build-test-rules.yml | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename components/mqtt/{ => test_apps}/.build-test-rules.yml (100%) diff --git a/components/mqtt/.build-test-rules.yml b/components/mqtt/test_apps/.build-test-rules.yml similarity index 100% rename from components/mqtt/.build-test-rules.yml rename to components/mqtt/test_apps/.build-test-rules.yml From ff66474d63bcc56dac70b35d12d6d8a18a265c4c Mon Sep 17 00:00:00 2001 From: Euripedes Rocha Date: Thu, 7 Sep 2023 11:35:04 +0200 Subject: [PATCH 182/231] ci(mqtt): Refactor publish connect test --- .../publish_connect_test/main/connect_test.c | 138 ++--- .../main/publish_connect_test.c | 312 +++++++++-- .../main/publish_connect_test.h | 55 ++ .../publish_connect_test/main/publish_test.c | 132 ++--- .../publish_connect_test/pytest_mqtt_app.py | 521 ++++++------------ .../pytest_mqtt_publish_app.py | 229 ++++++++ 6 files changed, 834 insertions(+), 553 deletions(-) create mode 100644 tools/test_apps/protocols/mqtt/publish_connect_test/main/publish_connect_test.h create mode 100644 tools/test_apps/protocols/mqtt/publish_connect_test/pytest_mqtt_publish_app.py diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/main/connect_test.c b/tools/test_apps/protocols/mqtt/publish_connect_test/main/connect_test.c index e8132ef..88db721 100644 --- a/tools/test_apps/protocols/mqtt/publish_connect_test/main/connect_test.c +++ b/tools/test_apps/protocols/mqtt/publish_connect_test/main/connect_test.c @@ -8,11 +8,11 @@ */ #include -#include "esp_netif.h" +#include "esp_console.h" #include "esp_log.h" #include "mqtt_client.h" -#include "esp_tls.h" +#include "publish_connect_test.h" #if (!defined(CONFIG_EXAMPLE_CONNECT_CASE_NO_CERT)) || \ (!defined(CONFIG_EXAMPLE_CONNECT_CASE_SERVER_CERT)) || \ @@ -34,17 +34,23 @@ extern const uint8_t client_inv_crt[] asm("_binary_client_inv_crt_start"); extern const uint8_t client_no_pwd_key[] asm("_binary_client_no_pwd_key_start"); static const char *TAG = "connect_test"; -static esp_mqtt_client_handle_t mqtt_client = NULL; static int running_test_case = 0; static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data) { + (void)handler_args; + (void)base; + (void)event_id; esp_mqtt_event_handle_t event = event_data; ESP_LOGD(TAG, "Event: %d, Test case: %d", event->event_id, running_test_case); switch (event->event_id) { + case MQTT_EVENT_BEFORE_CONNECT: + break; case MQTT_EVENT_CONNECTED: ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED: Test=%d", running_test_case); break; + case MQTT_EVENT_DISCONNECTED: + break; case MQTT_EVENT_ERROR: ESP_LOGI(TAG, "MQTT_EVENT_ERROR: Test=%d", running_test_case); if (event->error_handle->error_type == MQTT_ERROR_TYPE_ESP_TLS) { @@ -61,44 +67,17 @@ static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_ } } -static void create_client(void) +static void connect_no_certs(esp_mqtt_client_handle_t client, const char *uri) { - const esp_mqtt_client_config_t mqtt_cfg = { - .broker.address.uri = "mqtts://127.0.0.1:1234" - }; - esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg); - esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, client); - mqtt_client = client; - esp_mqtt_client_start(client); - ESP_LOGI(TAG, "mqtt client created for connection tests"); -} - -static void destroy_client(void) -{ - if (mqtt_client) { - esp_mqtt_client_stop(mqtt_client); - esp_mqtt_client_destroy(mqtt_client); - mqtt_client = NULL; - ESP_LOGI(TAG, "mqtt client for connection tests destroyed"); - } -} - -static void connect_no_certs(const char *host, const int port) -{ - char uri[64]; - sprintf(uri, "mqtts://%s:%d", host, port); + ESP_LOGI(TAG, "Runnning :CONFIG_EXAMPLE_CONNECT_CASE_NO_CERT"); const esp_mqtt_client_config_t mqtt_cfg = { .broker.address.uri = uri }; - esp_mqtt_set_config(mqtt_client, &mqtt_cfg); - esp_mqtt_client_disconnect(mqtt_client); - esp_mqtt_client_reconnect(mqtt_client); + esp_mqtt_set_config(client, &mqtt_cfg); } -static void connect_with_client_key_password(const char *host, const int port) +static void connect_with_client_key_password(esp_mqtt_client_handle_t client, const char *uri) { - char uri[64]; - sprintf(uri, "mqtts://%s:%d", host, port); const esp_mqtt_client_config_t mqtt_cfg = { .broker.address.uri = uri, .broker.verification.certificate = (const char *)ca_local_crt, @@ -107,15 +86,11 @@ static void connect_with_client_key_password(const char *host, const int port) .credentials.authentication.key_password = "esp32", .credentials.authentication.key_password_len = 5 }; - esp_mqtt_set_config(mqtt_client, &mqtt_cfg); - esp_mqtt_client_disconnect(mqtt_client); - esp_mqtt_client_reconnect(mqtt_client); + esp_mqtt_set_config(client, &mqtt_cfg); } -static void connect_with_server_der_cert(const char *host, const int port) +static void connect_with_server_der_cert(esp_mqtt_client_handle_t client, const char *uri) { - char uri[64]; - sprintf(uri, "mqtts://%s:%d", host, port); const esp_mqtt_client_config_t mqtt_cfg = { .broker.address.uri = uri, .broker.verification.certificate = (const char *)ca_der_start, @@ -123,123 +98,96 @@ static void connect_with_server_der_cert(const char *host, const int port) .credentials.authentication.certificate = "NULL", .credentials.authentication.key = "NULL" }; - esp_mqtt_set_config(mqtt_client, &mqtt_cfg); - esp_mqtt_client_disconnect(mqtt_client); - esp_mqtt_client_reconnect(mqtt_client); + esp_mqtt_set_config(client, &mqtt_cfg); } -static void connect_with_wrong_server_cert(const char *host, const int port) +static void connect_with_wrong_server_cert(esp_mqtt_client_handle_t client, const char *uri) { - char uri[64]; - sprintf(uri, "mqtts://%s:%d", host, port); const esp_mqtt_client_config_t mqtt_cfg = { .broker.address.uri = uri, .broker.verification.certificate = (const char *)client_pwd_crt, .credentials.authentication.certificate = "NULL", .credentials.authentication.key = "NULL" }; - esp_mqtt_set_config(mqtt_client, &mqtt_cfg); - esp_mqtt_client_disconnect(mqtt_client); - esp_mqtt_client_reconnect(mqtt_client); + esp_mqtt_set_config(client, &mqtt_cfg); } -static void connect_with_server_cert(const char *host, const int port) +static void connect_with_server_cert(esp_mqtt_client_handle_t client, const char *uri) { - char uri[64]; - sprintf(uri, "mqtts://%s:%d", host, port); const esp_mqtt_client_config_t mqtt_cfg = { .broker.address.uri = uri, .broker.verification.certificate = (const char *)ca_local_crt, }; - esp_mqtt_set_config(mqtt_client, &mqtt_cfg); - esp_mqtt_client_disconnect(mqtt_client); - esp_mqtt_client_reconnect(mqtt_client); + esp_mqtt_set_config(client, &mqtt_cfg); } -static void connect_with_server_client_certs(const char *host, const int port) +static void connect_with_server_client_certs(esp_mqtt_client_handle_t client, const char *uri) { - char uri[64]; - sprintf(uri, "mqtts://%s:%d", host, port); const esp_mqtt_client_config_t mqtt_cfg = { .broker.address.uri = uri, .broker.verification.certificate = (const char *)ca_local_crt, .credentials.authentication.certificate = (const char *)client_pwd_crt, .credentials.authentication.key = (const char *)client_no_pwd_key }; - esp_mqtt_set_config(mqtt_client, &mqtt_cfg); - esp_mqtt_client_disconnect(mqtt_client); - esp_mqtt_client_reconnect(mqtt_client); + esp_mqtt_set_config(client, &mqtt_cfg); } -static void connect_with_invalid_client_certs(const char *host, const int port) +static void connect_with_invalid_client_certs(esp_mqtt_client_handle_t client, const char *uri) { - char uri[64]; - sprintf(uri, "mqtts://%s:%d", host, port); const esp_mqtt_client_config_t mqtt_cfg = { .broker.address.uri = uri, .broker.verification.certificate = (const char *)ca_local_crt, .credentials.authentication.certificate = (const char *)client_inv_crt, .credentials.authentication.key = (const char *)client_no_pwd_key }; - esp_mqtt_set_config(mqtt_client, &mqtt_cfg); - esp_mqtt_client_disconnect(mqtt_client); - esp_mqtt_client_reconnect(mqtt_client); + esp_mqtt_set_config(client, &mqtt_cfg); } -static void connect_with_alpn(const char *host, const int port) +static void connect_with_alpn(esp_mqtt_client_handle_t client, const char *uri) { - char uri[64]; const char *alpn_protos[] = { "mymqtt", NULL }; - sprintf(uri, "mqtts://%s:%d", host, port); const esp_mqtt_client_config_t mqtt_cfg = { .broker.address.uri = uri, .broker.verification.alpn_protos = alpn_protos }; - esp_mqtt_set_config(mqtt_client, &mqtt_cfg); - esp_mqtt_client_disconnect(mqtt_client); - esp_mqtt_client_reconnect(mqtt_client); + esp_mqtt_set_config(client, &mqtt_cfg); } -void connection_test(const char *line) -{ - char test_type[32]; - char host[32]; - int port; - int test_case; +void connect_setup(command_context_t * ctx) { + esp_mqtt_client_register_event(ctx->mqtt_client, ESP_EVENT_ANY_ID, mqtt_event_handler, ctx->data); +} - sscanf(line, "%s %s %d %d", test_type, host, &port, &test_case); - if (mqtt_client == NULL) { - create_client(); - } - if (strcmp(host, "teardown") == 0) { - destroy_client();; - } - ESP_LOGI(TAG, "CASE:%d, connecting to mqtts://%s:%d ", test_case, host, port); +void connect_teardown(command_context_t * ctx) { + esp_mqtt_client_unregister_event(ctx->mqtt_client, ESP_EVENT_ANY_ID, mqtt_event_handler); +} +void connection_test(command_context_t * ctx, const char *uri, int test_case) +{ + ESP_LOGI(TAG, "CASE:%d, connecting to %s", test_case, uri); running_test_case = test_case; switch (test_case) { case CONFIG_EXAMPLE_CONNECT_CASE_NO_CERT: - connect_no_certs(host, port); + connect_no_certs(ctx->mqtt_client, uri); break; case CONFIG_EXAMPLE_CONNECT_CASE_SERVER_CERT: - connect_with_server_cert(host, port); + connect_with_server_cert(ctx->mqtt_client, uri); break; case CONFIG_EXAMPLE_CONNECT_CASE_MUTUAL_AUTH: - connect_with_server_client_certs(host, port); + connect_with_server_client_certs(ctx->mqtt_client, uri); break; case CONFIG_EXAMPLE_CONNECT_CASE_INVALID_SERVER_CERT: - connect_with_wrong_server_cert(host, port); + connect_with_wrong_server_cert(ctx->mqtt_client, uri); break; case CONFIG_EXAMPLE_CONNECT_CASE_SERVER_DER_CERT: - connect_with_server_der_cert(host, port); + connect_with_server_der_cert(ctx->mqtt_client, uri); break; case CONFIG_EXAMPLE_CONNECT_CASE_MUTUAL_AUTH_KEY_PWD: - connect_with_client_key_password(host, port); + connect_with_client_key_password(ctx->mqtt_client, uri); break; case CONFIG_EXAMPLE_CONNECT_CASE_MUTUAL_AUTH_BAD_CRT: - connect_with_invalid_client_certs(host, port); + connect_with_invalid_client_certs(ctx->mqtt_client, uri); break; case CONFIG_EXAMPLE_CONNECT_CASE_NO_CERT_ALPN: - connect_with_alpn(host, port); + connect_with_alpn(ctx->mqtt_client, uri); break; default: ESP_LOGE(TAG, "Unknown test case %d ", test_case); diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/main/publish_connect_test.c b/tools/test_apps/protocols/mqtt/publish_connect_test/main/publish_connect_test.c index b793215..d92a1fa 100644 --- a/tools/test_apps/protocols/mqtt/publish_connect_test/main/publish_connect_test.c +++ b/tools/test_apps/protocols/mqtt/publish_connect_test/main/publish_connect_test.c @@ -8,68 +8,310 @@ */ #include #include +#include #include #include "esp_system.h" +#include "mqtt_client.h" #include "nvs_flash.h" #include "esp_event.h" #include "esp_netif.h" #include "protocol_examples_common.h" +#include "esp_console.h" +#include "argtable3/argtable3.h" #include "esp_log.h" +#include "publish_connect_test.h" static const char *TAG = "publish_connect_test"; -void connection_test(const char *line); -void publish_test(const char *line); +command_context_t command_context; +connection_args_t connection_args; +publish_setup_args_t publish_setup_args; +publish_args_t publish_args; -static void get_string(char *line, size_t size) -{ - int count = 0; - while (count < size) { - int c = fgetc(stdin); - if (c == '\n') { - line[count] = '\0'; - break; - } else if (c > 0 && c < 127) { - line[count] = c; - ++count; - } - vTaskDelay(10 / portTICK_PERIOD_MS); +#define RETURN_ON_PARSE_ERROR(args) do { \ + int nerrors = arg_parse(argc, argv, (void **) &(args)); \ + if (nerrors != 0) { \ + arg_print_errors(stderr, (args).end, argv[0]); \ + return 1; \ + }} while(0) + + +static int do_free_heap(int argc, char **argv) { + (void)argc; + (void)argv; + ESP_LOGI(TAG, "Note free memory: %d bytes", esp_get_free_heap_size()); + return 0; +} + +static int do_init(int argc, char **argv) { + (void)argc; + (void)argv; + const esp_mqtt_client_config_t mqtt_cfg = { + .broker.address.uri = "mqtts://127.0.0.1:1234", + .network.disable_auto_reconnect = true + }; + command_context.mqtt_client = esp_mqtt_client_init(&mqtt_cfg); + if(!command_context.mqtt_client) { + ESP_LOGE(TAG, "Failed to initialize client"); + return 1; } + publish_init_flags(); + ESP_LOGI(TAG, "Mqtt client initialized"); + return 0; +} + +static int do_start(int argc, char **argv) { + (void)argc; + (void)argv; + if(esp_mqtt_client_start(command_context.mqtt_client) != ESP_OK) { + ESP_LOGE(TAG, "Failed to start mqtt client task"); + return 1; + } + ESP_LOGI(TAG, "Mqtt client started"); + return 0; +} + +static int do_stop(int argc, char **argv) { + (void)argc; + (void)argv; + if(esp_mqtt_client_stop(command_context.mqtt_client) != ESP_OK) { + ESP_LOGE(TAG, "Failed to stop mqtt client task"); + return 1; + } + ESP_LOGI(TAG, "Mqtt client stoped"); + return 0; +} + +static int do_disconnect(int argc, char **argv) { + (void)argc; + (void)argv; + if(esp_mqtt_client_disconnect(command_context.mqtt_client) != ESP_OK) { + ESP_LOGE(TAG, "Failed to request disconnection"); + return 1; + } + ESP_LOGI(TAG, "Mqtt client disconnected"); + return 0; +} + +static int do_connect_setup(int argc, char **argv) { + (void)argc; + (void)argv; + connect_setup(&command_context); + return 0; +} + +static int do_connect_teardown(int argc, char **argv) { + (void)argc; + (void)argv; + connect_teardown(&command_context); + return 0; +} + +static int do_reconnect(int argc, char **argv) { + (void)argc; + (void)argv; + if(esp_mqtt_client_reconnect(command_context.mqtt_client) != ESP_OK) { + ESP_LOGE(TAG, "Failed to request reconnection"); + return 1; + } + ESP_LOGI(TAG, "Mqtt client will reconnect"); + return 0; + ; +} + +static int do_destroy(int argc, char **argv) { + (void)argc; + (void)argv; + esp_mqtt_client_destroy(command_context.mqtt_client); + command_context.mqtt_client = NULL; + ESP_LOGI(TAG, "mqtt client for tests destroyed"); + return 0; +} + +static int do_connect(int argc, char **argv) +{ + int nerrors = arg_parse(argc, argv, (void **) &connection_args); + if (nerrors != 0) { + arg_print_errors(stderr, connection_args.end, argv[0]); + return 1; + } + if(!command_context.mqtt_client) { + ESP_LOGE(TAG, "MQTT client not initialized, call init first"); + return 1; + } + connection_test(&command_context, *connection_args.uri->sval, *connection_args.test_case->ival); + return 0; +} + +static int do_publish_setup(int argc, char **argv) { + RETURN_ON_PARSE_ERROR(publish_setup_args); + if(command_context.data) { + free(command_context.data); + } + command_context.data = calloc(1, sizeof(publish_context_t)); + ((publish_context_t*)command_context.data)->pattern = strdup(*publish_setup_args.pattern->sval); + ((publish_context_t*)command_context.data)->pattern_repetitions = *publish_setup_args.pattern_repetitions->ival; + publish_setup(&command_context, *publish_setup_args.transport->sval); + return 0; +} + +static int do_publish(int argc, char **argv) { + RETURN_ON_PARSE_ERROR(publish_args); + publish_test(&command_context, publish_args.expected_to_publish->ival[0], publish_args.qos->ival[0], publish_args.enqueue->ival[0]); + return 0; +} + +static int do_publish_report(int argc, char **argv) { + (void)argc; + (void)argv; + publish_context_t * ctx = command_context.data; + ESP_LOGI(TAG,"Test Report : Messages received %d, %d expected", ctx->nr_of_msg_received, ctx->nr_of_msg_expected); + + return 0; +} +void register_common_commands(void) { + const esp_console_cmd_t init = { + .command = "init", + .help = "Run inition test\n", + .hint = NULL, + .func = &do_init, + }; + + const esp_console_cmd_t start = { + .command = "start", + .help = "Run startion test\n", + .hint = NULL, + .func = &do_start, + }; + const esp_console_cmd_t stop = { + .command = "stop", + .help = "Run stopion test\n", + .hint = NULL, + .func = &do_stop, + }; + const esp_console_cmd_t destroy = { + .command = "destroy", + .help = "Run destroyion test\n", + .hint = NULL, + .func = &do_destroy, + }; + const esp_console_cmd_t free_heap = { + .command = "free_heap", + .help = "Run destroyion test\n", + .hint = NULL, + .func = &do_free_heap, + }; + ESP_ERROR_CHECK(esp_console_cmd_register(&init)); + ESP_ERROR_CHECK(esp_console_cmd_register(&start)); + ESP_ERROR_CHECK(esp_console_cmd_register(&stop)); + ESP_ERROR_CHECK(esp_console_cmd_register(&destroy)); + ESP_ERROR_CHECK(esp_console_cmd_register(&free_heap)); +} +void register_publish_commands(void) { + publish_setup_args.transport = arg_str1(NULL,NULL,"", "Selected transport to test"); + publish_setup_args.pattern = arg_str1(NULL,NULL,"", "Message pattern repeated to build big messages"); + publish_setup_args.pattern_repetitions = arg_int1(NULL,NULL,"", "How many times the pattern is repeated"); + publish_setup_args.end = arg_end(1); + + publish_args.expected_to_publish = arg_int1(NULL,NULL,"", "How many times the pattern is repeated"); + publish_args.qos = arg_int1(NULL,NULL,"", "How many times the pattern is repeated"); + publish_args.enqueue = arg_int1(NULL,NULL,"", "How many times the pattern is repeated"); + publish_args.end = arg_end(1); + const esp_console_cmd_t publish_setup = { + .command = "publish_setup", + .help = "Run publish test\n", + .hint = NULL, + .func = &do_publish_setup, + .argtable = &publish_setup_args + }; + + const esp_console_cmd_t publish = { + .command = "publish", + .help = "Run publish test\n", + .hint = NULL, + .func = &do_publish, + .argtable = &publish_args + }; + const esp_console_cmd_t publish_report = { + .command = "publish_report", + .help = "Run destroyion test\n", + .hint = NULL, + .func = &do_publish_report, + }; + ESP_ERROR_CHECK(esp_console_cmd_register(&publish_setup)); + ESP_ERROR_CHECK(esp_console_cmd_register(&publish)); + ESP_ERROR_CHECK(esp_console_cmd_register(&publish_report)); +} +void register_connect_commands(void){ + connection_args.uri = arg_str1(NULL,NULL,"", "Broker address"); + connection_args.test_case = arg_int1(NULL, NULL, "","Selected test case"); + connection_args.end = arg_end(1); + + const esp_console_cmd_t connect = { + .command = "connect", + .help = "Run connection test\n", + .hint = NULL, + .func = &do_connect, + .argtable = &connection_args + }; + + const esp_console_cmd_t reconnect = { + .command = "reconnect", + .help = "Run reconnection test\n", + .hint = NULL, + .func = &do_reconnect, + }; + const esp_console_cmd_t connection_setup = { + .command = "connection_setup", + .help = "Run reconnection test\n", + .hint = NULL, + .func = &do_connect_setup, + }; + const esp_console_cmd_t connection_teardown = { + .command = "connection_teardown", + .help = "Run reconnection test\n", + .hint = NULL, + .func = &do_connect_teardown, + }; + const esp_console_cmd_t disconnect = { + .command = "disconnect", + .help = "Run disconnection test\n", + .hint = NULL, + .func = &do_disconnect, + }; + ESP_ERROR_CHECK(esp_console_cmd_register(&connect)); + ESP_ERROR_CHECK(esp_console_cmd_register(&disconnect)); + ESP_ERROR_CHECK(esp_console_cmd_register(&reconnect)); + ESP_ERROR_CHECK(esp_console_cmd_register(&connection_setup)); + ESP_ERROR_CHECK(esp_console_cmd_register(&connection_teardown)); } void app_main(void) { - char line[256]; + static const size_t max_line = 256; ESP_LOGI(TAG, "[APP] Free memory: %d bytes", esp_get_free_heap_size()); ESP_LOGI(TAG, "[APP] IDF version: %s", esp_get_idf_version()); esp_log_level_set("*", ESP_LOG_INFO); + esp_log_level_set("wifi", ESP_LOG_ERROR); esp_log_level_set("mqtt_client", ESP_LOG_VERBOSE); - esp_log_level_set("transport_base", ESP_LOG_VERBOSE); - esp_log_level_set("transport", ESP_LOG_VERBOSE); esp_log_level_set("outbox", ESP_LOG_VERBOSE); ESP_ERROR_CHECK(nvs_flash_init()); ESP_ERROR_CHECK(esp_netif_init()); ESP_ERROR_CHECK(esp_event_loop_create_default()); - - /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig. - * Read "Establishing Wi-Fi or Ethernet Connection" section in - * examples/protocols/README.md for more information about this function. - */ ESP_ERROR_CHECK(example_connect()); + esp_console_repl_t *repl = NULL; + esp_console_repl_config_t repl_config = ESP_CONSOLE_REPL_CONFIG_DEFAULT(); + repl_config.prompt = "mqtt>"; + repl_config.max_cmdline_length = max_line; + esp_console_register_help_command(); + register_common_commands(); + register_connect_commands(); + register_publish_commands(); - while (1) { - get_string(line, sizeof(line)); - if (memcmp(line, "conn", 4) == 0) { - // line starting with "conn" indicate connection tests - connection_test(line); - get_string(line, sizeof(line)); - continue; - } else { - publish_test(line); - } - } - + esp_console_dev_uart_config_t hw_config = ESP_CONSOLE_DEV_UART_CONFIG_DEFAULT(); + ESP_ERROR_CHECK(esp_console_new_repl_uart(&hw_config, &repl_config, &repl)); + ESP_ERROR_CHECK(esp_console_start_repl(repl)); } diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/main/publish_connect_test.h b/tools/test_apps/protocols/mqtt/publish_connect_test/main/publish_connect_test.h new file mode 100644 index 0000000..454e82e --- /dev/null +++ b/tools/test_apps/protocols/mqtt/publish_connect_test/main/publish_connect_test.h @@ -0,0 +1,55 @@ +/* + * SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Unlicense OR CC0-1.0 + */ +#pragma once + +#include "mqtt_client.h" + +typedef enum {NONE, TCP, SSL, WS, WSS} transport_t; + +typedef struct { + esp_mqtt_client_handle_t mqtt_client; + void * data; +} command_context_t; + +typedef struct { + transport_t selected_transport; + char *pattern; + int pattern_repetitions; + int qos; + char *expected; + size_t expected_size; + size_t nr_of_msg_received; + size_t nr_of_msg_expected; + char * received_data; +} publish_context_t ; + +typedef struct { + struct arg_str *uri; + struct arg_int *test_case; + struct arg_end *end; +} connection_args_t; + +typedef struct { + struct arg_int *expected_to_publish; + struct arg_int *qos; + struct arg_int *enqueue; + struct arg_end *end; +} publish_args_t; + +typedef struct { + struct arg_str *transport; + struct arg_str *pattern; + struct arg_int *pattern_repetitions; + struct arg_end *end; +} publish_setup_args_t; + +void publish_init_flags(void); +void publish_setup(command_context_t * ctx, char const * transport); +void publish_teardown(command_context_t * ctx); +void publish_test(command_context_t * ctx, int expect_to_publish, int qos, bool enqueue); +void connection_test(command_context_t * ctx, const char *uri, int test_case); +void connect_setup(command_context_t * ctx); +void connect_teardown(command_context_t * ctx); diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/main/publish_test.c b/tools/test_apps/protocols/mqtt/publish_connect_test/main/publish_test.c index d9ddf08..3bdefb6 100644 --- a/tools/test_apps/protocols/mqtt/publish_connect_test/main/publish_test.c +++ b/tools/test_apps/protocols/mqtt/publish_connect_test/main/publish_test.c @@ -6,33 +6,25 @@ software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */ +#include #include #include #include #include -#include "esp_system.h" #include "freertos/FreeRTOS.h" -#include "freertos/task.h" -#include "freertos/event_groups.h" +#include +#include "esp_system.h" #include "esp_log.h" #include "mqtt_client.h" #include "sdkconfig.h" +#include "publish_connect_test.h" static const char *TAG = "publish_test"; static EventGroupHandle_t mqtt_event_group; const static int CONNECTED_BIT = BIT0; -static esp_mqtt_client_handle_t mqtt_client = NULL; - -static char *expected_data = NULL; -static char *actual_data = NULL; -static size_t expected_size = 0; -static size_t expected_published = 0; -static size_t actual_published = 0; -static int qos_test = 0; - #if CONFIG_EXAMPLE_BROKER_CERTIFICATE_OVERRIDDEN == 1 static const uint8_t mqtt_eclipseprojects_io_pem_start[] = "-----BEGIN CERTIFICATE-----\n" CONFIG_EXAMPLE_BROKER_CERTIFICATE_OVERRIDE "\n-----END CERTIFICATE-----"; #else @@ -42,6 +34,7 @@ extern const uint8_t mqtt_eclipseprojects_io_pem_end[] asm("_binary_mqtt_eclip static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data) { + publish_context_t * test_data = handler_args; esp_mqtt_event_handle_t event = event_data; esp_mqtt_client_handle_t client = event->client; static int msg_id = 0; @@ -52,7 +45,7 @@ static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_ case MQTT_EVENT_CONNECTED: ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED"); xEventGroupSetBits(mqtt_event_group, CONNECTED_BIT); - msg_id = esp_mqtt_client_subscribe(client, CONFIG_EXAMPLE_SUBSCRIBE_TOPIC, qos_test); + msg_id = esp_mqtt_client_subscribe(client, CONFIG_EXAMPLE_SUBSCRIBE_TOPIC, test_data->qos); ESP_LOGI(TAG, "sent subscribe successful %s , msg_id=%d", CONFIG_EXAMPLE_SUBSCRIBE_TOPIC, msg_id); break; @@ -67,13 +60,12 @@ static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_ ESP_LOGI(TAG, "MQTT_EVENT_UNSUBSCRIBED, msg_id=%d", event->msg_id); break; case MQTT_EVENT_PUBLISHED: - ESP_LOGI(TAG, "MQTT_EVENT_PUBLISHED, msg_id=%d", event->msg_id); + ESP_LOGD(TAG, "MQTT_EVENT_PUBLISHED, msg_id=%d", event->msg_id); break; case MQTT_EVENT_DATA: ESP_LOGI(TAG, "MQTT_EVENT_DATA"); - printf("TOPIC=%.*s\r\n", event->topic_len, event->topic); - printf("DATA=%.*s\r\n", event->data_len, event->data); - printf("ID=%d, total_len=%d, data_len=%d, current_data_offset=%d\n", event->msg_id, event->total_data_len, event->data_len, event->current_data_offset); + ESP_LOGI(TAG, "TOPIC=%.*s", event->topic_len, event->topic); + ESP_LOGI(TAG, "ID=%d, total_len=%d, data_len=%d, current_data_offset=%d", event->msg_id, event->total_data_len, event->data_len, event->current_data_offset); if (event->topic) { actual_len = event->data_len; msg_id = event->msg_id; @@ -85,24 +77,23 @@ static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_ abort(); } } - memcpy(actual_data + event->current_data_offset, event->data, event->data_len); + memcpy(test_data->received_data + event->current_data_offset, event->data, event->data_len); if (actual_len == event->total_data_len) { - if (0 == memcmp(actual_data, expected_data, expected_size)) { - printf("OK!"); - memset(actual_data, 0, expected_size); - actual_published ++; - if (actual_published == expected_published) { - printf("Correct pattern received exactly x times\n"); + if (0 == memcmp(test_data->received_data, test_data->expected, test_data->expected_size)) { + memset(test_data->received_data, 0, test_data->expected_size); + test_data->nr_of_msg_received ++; + if (test_data->nr_of_msg_received == test_data->nr_of_msg_expected) { + ESP_LOGI(TAG, "Correct pattern received exactly x times"); ESP_LOGI(TAG, "Test finished correctly!"); } } else { - printf("FAILED!"); + ESP_LOGE(TAG, "FAILED!"); abort(); } } break; case MQTT_EVENT_ERROR: - ESP_LOGI(TAG, "MQTT_EVENT_ERROR"); + ESP_LOGE(TAG, "MQTT_EVENT_ERROR"); break; default: ESP_LOGI(TAG, "Other event id:%d", event->event_id); @@ -110,37 +101,31 @@ static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_ } } -typedef enum {NONE, TCP, SSL, WS, WSS} transport_t; -static transport_t current_transport; void test_init(void) { - mqtt_event_group = xEventGroupCreate(); - esp_mqtt_client_config_t config = {0}; - mqtt_client = esp_mqtt_client_init(&config); - current_transport = NONE; - esp_mqtt_client_register_event(mqtt_client, ESP_EVENT_ANY_ID, mqtt_event_handler, NULL); ESP_LOGI(TAG, "[APP] Free memory: %d bytes", esp_get_free_heap_size()); } -void pattern_setup(char *pattern, int repeat) +void pattern_setup(publish_context_t * test_data) { - int pattern_size = strlen(pattern); - free(expected_data); - free(actual_data); - actual_published = 0; - expected_size = pattern_size * repeat; - expected_data = malloc(expected_size); - actual_data = malloc(expected_size); - for (int i = 0; i < repeat; i++) { - memcpy(expected_data + i * pattern_size, pattern, pattern_size); + int pattern_size = strlen(test_data->pattern); + free(test_data->expected); + free(test_data->received_data); + test_data->nr_of_msg_received = 0; + test_data->expected_size = (size_t)(pattern_size) * test_data->pattern_repetitions; + test_data->expected = malloc(test_data->expected_size); + test_data->received_data = malloc(test_data->expected_size); + for (int i = 0; i < test_data->pattern_repetitions; i++) { + memcpy(test_data->expected + (ptrdiff_t)(i * pattern_size), test_data->pattern, pattern_size); } - printf("EXPECTED STRING %.*s, SIZE:%d\n", expected_size, expected_data, expected_size); + ESP_LOGI(TAG, "EXPECTED STRING %.*s, SIZE:%d", test_data->expected_size, test_data->expected, test_data->expected_size); } -static void configure_client(char *transport) +static void configure_client(command_context_t * ctx, const char *transport) { + publish_context_t * test_data = ctx->data; ESP_LOGI(TAG, "Configuration"); transport_t selected_transport; if (0 == strcmp(transport, "tcp")) { @@ -157,7 +142,8 @@ static void configure_client(char *transport) } - if (selected_transport != current_transport) { + if (selected_transport != test_data->selected_transport) { + test_data->selected_transport = selected_transport; esp_mqtt_client_config_t config = {0}; switch (selected_transport) { case NONE: @@ -183,43 +169,45 @@ static void configure_client(char *transport) ESP_LOGI(TAG, "Set certificate"); config.broker.verification.certificate = (const char *)mqtt_eclipseprojects_io_pem_start; } - esp_mqtt_set_config(mqtt_client, &config); - + esp_mqtt_set_config(ctx->mqtt_client, &config); } - } -void publish_test(const char *line) -{ - char pattern[32]; - char transport[32]; - int repeat = 0; - int enqueue = 0; - static bool is_test_init = false; - if (!is_test_init) { - test_init(); - is_test_init = true; - } else { - esp_mqtt_client_stop(mqtt_client); - } +void publish_init_flags(void) { + mqtt_event_group = xEventGroupCreate(); +} - sscanf(line, "%s %s %d %d %d %d", transport, pattern, &repeat, &expected_published, &qos_test, &enqueue); - ESP_LOGI(TAG, "PATTERN:%s REPEATED:%d PUBLISHED:%d", pattern, repeat, expected_published); - pattern_setup(pattern, repeat); +void publish_setup(command_context_t * ctx, char const * const transport) { xEventGroupClearBits(mqtt_event_group, CONNECTED_BIT); - configure_client(transport); - esp_mqtt_client_start(mqtt_client); + publish_context_t * data = (publish_context_t*)ctx->data; + pattern_setup(data); + configure_client(ctx, transport); + esp_mqtt_client_register_event(ctx->mqtt_client, ESP_EVENT_ANY_ID, mqtt_event_handler, data); +} - ESP_LOGI(TAG, "Note free memory: %d bytes", esp_get_free_heap_size()); +void publish_teardown(command_context_t * ctx) +{ + esp_mqtt_client_unregister_event(ctx->mqtt_client, ESP_EVENT_ANY_ID, mqtt_event_handler); +} + +void publish_test(command_context_t * ctx, int expect_to_publish, int qos, bool enqueue) +{ + publish_context_t * data = (publish_context_t*)ctx->data; + data->nr_of_msg_expected = expect_to_publish; + ESP_LOGI(TAG, "PATTERN:%s REPEATED:%d PUBLISHED:%d", data->pattern, data->pattern_repetitions, data->nr_of_msg_expected); xEventGroupWaitBits(mqtt_event_group, CONNECTED_BIT, false, true, portMAX_DELAY); - for (int i = 0; i < expected_published; i++) { + for (int i = 0; i < data->nr_of_msg_expected; i++) { int msg_id; if (enqueue) { - msg_id = esp_mqtt_client_enqueue(mqtt_client, CONFIG_EXAMPLE_PUBLISH_TOPIC, expected_data, expected_size, qos_test, 0, true); + msg_id = esp_mqtt_client_enqueue(ctx->mqtt_client, CONFIG_EXAMPLE_PUBLISH_TOPIC, data->expected, data->expected_size, qos, 0, true); } else { - msg_id = esp_mqtt_client_publish(mqtt_client, CONFIG_EXAMPLE_PUBLISH_TOPIC, expected_data, expected_size, qos_test, 0); + msg_id = esp_mqtt_client_publish(ctx->mqtt_client, CONFIG_EXAMPLE_PUBLISH_TOPIC, data->expected, data->expected_size, qos, 0); + if(msg_id < 0) { + ESP_LOGE(TAG, "Failed to publish"); + break; + } } - ESP_LOGI(TAG, "[%d] Publishing...", msg_id); + ESP_LOGD(TAG, "Publishing msg_id=%d", msg_id); } } diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/pytest_mqtt_app.py b/tools/test_apps/protocols/mqtt/publish_connect_test/pytest_mqtt_app.py index 6b27adc..2377018 100644 --- a/tools/test_apps/protocols/mqtt/publish_connect_test/pytest_mqtt_app.py +++ b/tools/test_apps/protocols/mqtt/publish_connect_test/pytest_mqtt_app.py @@ -1,31 +1,21 @@ # SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD # SPDX-License-Identifier: Unlicense OR CC0-1.0 -from __future__ import print_function, unicode_literals -import difflib +import contextlib import logging import os -import random import re -import select -import socket +import socketserver import ssl -import string import subprocess -import sys -import time -import typing -from itertools import count -from threading import Event, Lock, Thread -from typing import Any +from threading import Thread +from typing import Any, Callable, Dict, Optional -import paho.mqtt.client as mqtt import pytest from common_test_methods import get_host_ip4_by_dest_ip from pytest_embedded import Dut -from pytest_embedded_qemu.dut import QemuDut -DEFAULT_MSG_SIZE = 16 +SERVER_PORT = 2222 def _path(f): # type: (str) -> str @@ -42,313 +32,98 @@ def set_server_cert_cn(ip): # type: (str) -> None raise RuntimeError('openssl command {} failed'.format(args)) -# Publisher class creating a python client to send/receive published data from esp-mqtt client -class MqttPublisher: - event_client_connected = Event() - event_client_got_all = Event() - expected_data = '' - published = 0 - sample = '' +class MQTTHandler(socketserver.StreamRequestHandler): - def __init__(self, dut, transport, - qos, repeat, published, queue, publish_cfg, log_details=False): # type: (MqttPublisher, Dut, str, int, int, int, int, dict, bool) -> None - # instance variables used as parameters of the publish test - self.event_stop_client = Event() - self.sample_string = ''.join(random.choice(string.ascii_uppercase + string.ascii_lowercase + string.digits) for _ in range(DEFAULT_MSG_SIZE)) - self.client = None - self.dut = dut - self.log_details = log_details - self.repeat = repeat - self.publish_cfg = publish_cfg - self.publish_cfg['qos'] = qos - self.publish_cfg['queue'] = queue - self.publish_cfg['transport'] = transport - self.lock = Lock() - # static variables used to pass options to and from static callbacks of paho-mqtt client - MqttPublisher.event_client_connected = Event() - MqttPublisher.event_client_got_all = Event() - MqttPublisher.published = published - MqttPublisher.event_client_connected.clear() - MqttPublisher.event_client_got_all.clear() - MqttPublisher.expected_data = f'{self.sample_string * self.repeat}' - MqttPublisher.sample = self.sample_string - - def print_details(self, text): # type: (str) -> None - if self.log_details: - logging.info(text) - - def mqtt_client_task(self, client, lock): # type: (MqttPublisher, mqtt.Client, Lock) -> None - while not self.event_stop_client.is_set(): - with lock: - client.loop() - time.sleep(0.001) # yield to other threads - - # The callback for when the client receives a CONNACK response from the server (needs to be static) - @staticmethod - def on_connect(_client, _userdata, _flags, _rc): # type: (mqtt.Client, tuple, bool, str) -> None - MqttPublisher.event_client_connected.set() - - # The callback for when a PUBLISH message is received from the server (needs to be static) - @staticmethod - def on_message(client, userdata, msg): # type: (mqtt.Client, int, mqtt.client.MQTTMessage) -> None - payload = msg.payload.decode('utf-8') - if payload == MqttPublisher.expected_data: - userdata += 1 - client.user_data_set(userdata) - if userdata == MqttPublisher.published: - MqttPublisher.event_client_got_all.set() - else: - differences = len(list(filter(lambda data: data[0] != data[1], zip(payload, MqttPublisher.expected_data)))) - logging.error(f'Payload differ in {differences} positions from expected data. received size: {len(payload)} expected size:' - f'{len(MqttPublisher.expected_data)}') - logging.info(f'Repetitions: {payload.count(MqttPublisher.sample)}') - logging.info(f'Pattern: {MqttPublisher.sample}') - logging.info(f'First : {payload[:DEFAULT_MSG_SIZE]}') - logging.info(f'Last : {payload[-DEFAULT_MSG_SIZE:]}') - matcher = difflib.SequenceMatcher(a=payload, b=MqttPublisher.expected_data) - for match in matcher.get_matching_blocks(): - logging.info(f'Match: {match}') - - def __enter__(self): # type: (MqttPublisher) -> None - - qos = self.publish_cfg['qos'] - queue = self.publish_cfg['queue'] - transport = self.publish_cfg['transport'] - broker_host = self.publish_cfg['broker_host_' + transport] - broker_port = self.publish_cfg['broker_port_' + transport] - - # Start the test - self.print_details(f'PUBLISH TEST: transport:{transport}, qos:{qos}, sequence:{MqttPublisher.published},' - f"enqueue:{queue}, sample msg:'{MqttPublisher.expected_data}'") - - try: - if transport in ['ws', 'wss']: - self.client = mqtt.Client(transport='websockets') + def handle(self) -> None: + logging.info(' - connection from: {}'.format(self.client_address)) + data = bytearray(self.request.recv(1024)) + message = ''.join(format(x, '02x') for x in data) + if message[0:16] == '101800044d515454': + if self.server.refuse_connection is False: # type: ignore + logging.info(' - received mqtt connect, sending ACK') + self.request.send(bytearray.fromhex('20020000')) else: - self.client = mqtt.Client() - assert self.client is not None - self.client.on_connect = MqttPublisher.on_connect - self.client.on_message = MqttPublisher.on_message - self.client.user_data_set(0) - - if transport in ['ssl', 'wss']: - self.client.tls_set(None, None, None, cert_reqs=ssl.CERT_NONE, tls_version=ssl.PROTOCOL_TLSv1_2, ciphers=None) - self.client.tls_insecure_set(True) - self.print_details('Connecting...') - self.client.connect(broker_host, broker_port, 60) - except Exception: - self.print_details(f'ENV_TEST_FAILURE: Unexpected error while connecting to broker {broker_host}') - raise - # Starting a py-client in a separate thread - thread1 = Thread(target=self.mqtt_client_task, args=(self.client, self.lock)) - thread1.start() - self.print_details('Connecting py-client to broker {}:{}...'.format(broker_host, broker_port)) - if not MqttPublisher.event_client_connected.wait(timeout=30): - raise ValueError(f'ENV_TEST_FAILURE: Test script cannot connect to broker: {broker_host}') - with self.lock: - self.client.subscribe(self.publish_cfg['subscribe_topic'], qos) - self.dut.write(f'{transport} {self.sample_string} {self.repeat} {MqttPublisher.published} {qos} {queue}') - try: - # waiting till subscribed to defined topic - self.dut.expect(re.compile(rb'MQTT_EVENT_SUBSCRIBED'), timeout=60) - for _ in range(MqttPublisher.published): - with self.lock: - self.client.publish(self.publish_cfg['publish_topic'], self.sample_string * self.repeat, qos) - self.print_details('Publishing...') - self.print_details('Checking esp-client received msg published from py-client...') - self.dut.expect(re.compile(rb'Correct pattern received exactly x times'), timeout=60) - if not MqttPublisher.event_client_got_all.wait(timeout=60): - raise ValueError('Not all data received from ESP32: {}'.format(transport)) - logging.info(' - all data received from ESP32') - finally: - self.event_stop_client.set() - thread1.join() - - def __exit__(self, exc_type, exc_value, traceback): # type: (MqttPublisher, str, str, dict) -> None - assert self.client is not None - self.client.disconnect() - self.event_stop_client.clear() + # injecting connection not authorized error + logging.info(' - received mqtt connect, sending NAK') + self.request.send(bytearray.fromhex('20020005')) + else: + raise Exception(' - error process_mqtt_connect unexpected connect received: {}'.format(message)) # Simple server for mqtt over TLS connection -class TlsServer: +class TlsServer(socketserver.TCPServer): + timeout = 30.0 + allow_reuse_address = True + allow_reuse_port = True - def __init__(self, port, client_cert=False, refuse_connection=False, use_alpn=False): # type: (TlsServer, int, bool, bool, bool) -> None - self.port = port - self.socket = socket.socket() - self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) - self.socket.settimeout(10.0) - self.shutdown = Event() - self.client_cert = client_cert + def __init__(self, + port:int = SERVER_PORT, + ServerHandler: Callable[[Any, Any, Any], socketserver.BaseRequestHandler] = MQTTHandler, + client_cert:bool=False, + refuse_connection:bool=False, + use_alpn:bool=False): self.refuse_connection = refuse_connection - self.use_alpn = use_alpn - self.conn = socket.socket() + self.context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH) self.ssl_error = '' + self.alpn_protocol: Optional[str] = None + if client_cert: + self.context.verify_mode = ssl.CERT_REQUIRED + self.context.load_verify_locations(cafile=_path('ca.crt')) + self.context.load_cert_chain(certfile=_path('srv.crt'), keyfile=_path('server.key')) + if use_alpn: + self.context.set_alpn_protocols(['mymqtt', 'http/1.1']) + self.server_thread = Thread(target=self.serve_forever) + super().__init__(('',port), ServerHandler) - def __enter__(self): # type: (TlsServer) -> TlsServer - try: - self.socket.bind(('', self.port)) - except socket.error as e: - print('Bind failed:{}'.format(e)) - raise + def server_activate(self) -> None: + self.socket = self.context.wrap_socket(self.socket, server_side=True) + super().server_activate() - self.socket.listen(1) - self.server_thread = Thread(target=self.run_server) + def __enter__(self): # type: ignore self.server_thread.start() - return self - def __exit__(self, exc_type, exc_value, traceback): # type: (TlsServer, str, str, str) -> None - self.shutdown.set() - self.server_thread.join() - self.socket.close() - if (self.conn is not None): - self.conn.close() + def server_close(self) -> None: + try: + self.shutdown() + self.server_thread.join() + super().server_close() + except RuntimeError as e: + logging.exception(e) - def get_last_ssl_error(self): # type: (TlsServer) -> str + # We need to override it here to capture ssl.SSLError + # The implementation is a slightly modified version from cpython original code. + def _handle_request_noblock(self) -> None: + try: + request, client_address = self.get_request() + self.alpn_protocol = request.selected_alpn_protocol() # type: ignore + except ssl.SSLError as e: + self.ssl_error = e.reason + return + except OSError: + return + if self.verify_request(request, client_address): + try: + self.process_request(request, client_address) + except Exception: + self.handle_error(request, client_address) + self.shutdown_request(request) + except: # noqa: E722 + self.shutdown_request(request) + raise + else: + self.shutdown_request(request) + + def last_ssl_error(self): # type: (TlsServer) -> str return self.ssl_error - @typing.no_type_check - def get_negotiated_protocol(self): - return self.negotiated_protocol - - def run_server(self) -> None: - context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH) - if self.client_cert: - context.verify_mode = ssl.CERT_REQUIRED - context.load_verify_locations(cafile=_path('ca.crt')) - context.load_cert_chain(certfile=_path('srv.crt'), keyfile=_path('server.key')) - if self.use_alpn: - context.set_alpn_protocols(['mymqtt', 'http/1.1']) - self.socket = context.wrap_socket(self.socket, server_side=True) - try: - self.conn, address = self.socket.accept() # accept new connection - self.socket.settimeout(10.0) - print(' - connection from: {}'.format(address)) - if self.use_alpn: - self.negotiated_protocol = self.conn.selected_alpn_protocol() - print(' - negotiated_protocol: {}'.format(self.negotiated_protocol)) - self.handle_conn() - except ssl.SSLError as e: - self.ssl_error = str(e) - print(' - SSLError: {}'.format(str(e))) - - def handle_conn(self) -> None: - while not self.shutdown.is_set(): - r,w,e = select.select([self.conn], [], [], 1) - try: - if self.conn in r: - self.process_mqtt_connect() - - except socket.error as err: - print(' - error: {}'.format(err)) - raise - - def process_mqtt_connect(self) -> None: - try: - data = bytearray(self.conn.recv(1024)) - message = ''.join(format(x, '02x') for x in data) - if message[0:16] == '101800044d515454': - if self.refuse_connection is False: - print(' - received mqtt connect, sending ACK') - self.conn.send(bytearray.fromhex('20020000')) - else: - # injecting connection not authorized error - print(' - received mqtt connect, sending NAK') - self.conn.send(bytearray.fromhex('20020005')) - else: - raise Exception(' - error process_mqtt_connect unexpected connect received: {}'.format(message)) - finally: - # stop the server after the connect message in happy flow, or if any exception occur - self.shutdown.set() + def get_negotiated_protocol(self) -> Optional[str]: + return self.alpn_protocol -def connection_tests(dut, cases, dut_ip): # type: (Dut, dict, str) -> None - ip = get_host_ip4_by_dest_ip(dut_ip) - set_server_cert_cn(ip) - server_port = 2222 - - def teardown_connection_suite() -> None: - dut.write('conn teardown 0 0\n') - - def start_connection_case(case, desc): # type: (str, str) -> Any - print('Starting {}: {}'.format(case, desc)) - case_id = cases[case] - dut.write('conn {} {} {}\n'.format(ip, server_port, case_id)) - dut.expect('Test case:{} started'.format(case_id)) - return case_id - - for case in ['EXAMPLE_CONNECT_CASE_NO_CERT', 'EXAMPLE_CONNECT_CASE_SERVER_CERT', 'EXAMPLE_CONNECT_CASE_SERVER_DER_CERT']: - # All these cases connect to the server with no server verification or with server only verification - with TlsServer(server_port): - test_nr = start_connection_case(case, 'default server - expect to connect normally') - dut.expect('MQTT_EVENT_CONNECTED: Test={}'.format(test_nr), timeout=30) - with TlsServer(server_port, refuse_connection=True): - test_nr = start_connection_case(case, 'ssl shall connect, but mqtt sends connect refusal') - dut.expect('MQTT_EVENT_ERROR: Test={}'.format(test_nr), timeout=30) - dut.expect('MQTT ERROR: 0x5') # expecting 0x5 ... connection not authorized error - with TlsServer(server_port, client_cert=True) as s: - test_nr = start_connection_case(case, 'server with client verification - handshake error since client presents no client certificate') - dut.expect('MQTT_EVENT_ERROR: Test={}'.format(test_nr), timeout=30) - dut.expect('ESP-TLS ERROR: ESP_ERR_MBEDTLS_SSL_HANDSHAKE_FAILED') # expect ... handshake error (PEER_DID_NOT_RETURN_A_CERTIFICATE) - if 'PEER_DID_NOT_RETURN_A_CERTIFICATE' not in s.get_last_ssl_error(): - raise RuntimeError('Unexpected ssl error from the server {}'.format(s.get_last_ssl_error())) - - for case in ['EXAMPLE_CONNECT_CASE_MUTUAL_AUTH', 'EXAMPLE_CONNECT_CASE_MUTUAL_AUTH_KEY_PWD']: - # These cases connect to server with both server and client verification (client key might be password protected) - with TlsServer(server_port, client_cert=True): - test_nr = start_connection_case(case, 'server with client verification - expect to connect normally') - dut.expect('MQTT_EVENT_CONNECTED: Test={}'.format(test_nr), timeout=30) - - case = 'EXAMPLE_CONNECT_CASE_INVALID_SERVER_CERT' - with TlsServer(server_port) as s: - test_nr = start_connection_case(case, 'invalid server certificate on default server - expect ssl handshake error') - dut.expect('MQTT_EVENT_ERROR: Test={}'.format(test_nr), timeout=30) - dut.expect('ESP-TLS ERROR: ESP_ERR_MBEDTLS_SSL_HANDSHAKE_FAILED') # expect ... handshake error (TLSV1_ALERT_UNKNOWN_CA) - if 'alert unknown ca' not in s.get_last_ssl_error(): - raise Exception('Unexpected ssl error from the server {}'.format(s.get_last_ssl_error())) - - case = 'EXAMPLE_CONNECT_CASE_MUTUAL_AUTH_BAD_CRT' - with TlsServer(server_port, client_cert=True) as s: - test_nr = start_connection_case(case, 'Invalid client certificate on server with client verification - expect ssl handshake error') - dut.expect('MQTT_EVENT_ERROR: Test={}'.format(test_nr), timeout=30) - dut.expect('ESP-TLS ERROR: ESP_ERR_MBEDTLS_SSL_HANDSHAKE_FAILED') # expect ... handshake error (CERTIFICATE_VERIFY_FAILED) - if 'CERTIFICATE_VERIFY_FAILED' not in s.get_last_ssl_error(): - raise Exception('Unexpected ssl error from the server {}'.format(s.get_last_ssl_error())) - - for case in ['EXAMPLE_CONNECT_CASE_NO_CERT', 'EXAMPLE_CONNECT_CASE_NO_CERT_ALPN']: - with TlsServer(server_port, use_alpn=True) as s: - test_nr = start_connection_case(case, 'server with alpn - expect connect, check resolved protocol') - dut.expect('MQTT_EVENT_CONNECTED: Test={}'.format(test_nr), timeout=30) - if case == 'EXAMPLE_CONNECT_CASE_NO_CERT' and s.get_negotiated_protocol() is None: - print(' - client with alpn off, no negotiated protocol: OK') - elif case == 'EXAMPLE_CONNECT_CASE_NO_CERT_ALPN' and s.get_negotiated_protocol() == 'mymqtt': - print(' - client with alpn on, negotiated protocol resolved: OK') - else: - raise Exception('Unexpected negotiated protocol {}'.format(s.get_negotiated_protocol())) - - teardown_connection_suite() - - -@pytest.mark.esp32 -@pytest.mark.ethernet -def test_app_protocol_mqtt_publish_connect(dut: Dut) -> None: - """ - steps: - 1. join AP - 2. connect to uri specified in the config - 3. send and receive data - """ - # check and log bin size - binary_file = os.path.join(dut.app.binary_path, 'mqtt_publish_connect_test.bin') - bin_size = os.path.getsize(binary_file) - logging.info('[Performance][mqtt_publish_connect_test_bin_size]: %s KB', bin_size // 1024) - - # Look for test case symbolic names and publish configs +def get_test_cases(dut: Dut) -> Any: cases = {} - publish_cfg = {} try: - # Get connection test cases configuration: symbolic names for test cases for case in ['EXAMPLE_CONNECT_CASE_NO_CERT', 'EXAMPLE_CONNECT_CASE_SERVER_CERT', @@ -360,63 +135,107 @@ def test_app_protocol_mqtt_publish_connect(dut: Dut) -> None: 'EXAMPLE_CONNECT_CASE_NO_CERT_ALPN']: cases[case] = dut.app.sdkconfig.get(case) except Exception: - print('ENV_TEST_FAILURE: Some mandatory CONNECTION test case not found in sdkconfig') + logging.error('ENV_TEST_FAILURE: Some mandatory CONNECTION test case not found in sdkconfig') raise + return cases - esp_ip = dut.expect(r'IPv4 address: (\d+\.\d+\.\d+\.\d+)[^\d]', timeout=30).group(1).decode() - print('Got IP={}'.format(esp_ip)) - connection_tests(dut,cases,esp_ip) +def get_dut_ip(dut: Dut) -> Any: + dut_ip = dut.expect(r'IPv4 address: (\d+\.\d+\.\d+\.\d+)[^\d]', timeout=30).group(1).decode() + logging.info('Got IP={}'.format(dut_ip)) + return get_host_ip4_by_dest_ip(dut_ip) - # Get publish test configuration + +@contextlib.contextmanager +def connect_dut(dut: Dut, uri:str, case_id:int) -> Any: + dut.write('connection_setup') + dut.write(f'connect {uri} {case_id}') + dut.expect(f'Test case:{case_id} started') + dut.write('reconnect') + yield + dut.write('connection_teardown') + dut.write('disconnect') + + +def run_cases(dut:Dut, uri:str, cases:Dict[str, int]) -> None: try: - @typing.no_type_check - def get_host_port_from_dut(dut, config_option): - value = re.search(r'\:\/\/([^:]+)\:([0-9]+)', dut.app.sdkconfig.get(config_option)) - if value is None: - return None, None - return value.group(1), int(value.group(2)) + dut.write('init') + dut.write(f'start') + dut.write(f'disconnect') + for case in ['EXAMPLE_CONNECT_CASE_NO_CERT', 'EXAMPLE_CONNECT_CASE_SERVER_CERT', 'EXAMPLE_CONNECT_CASE_SERVER_DER_CERT']: + # All these cases connect to the server with no server verification or with server only verification + with TlsServer(), connect_dut(dut, uri, cases[case]): + logging.info(f'Running {case}: default server - expect to connect normally') + dut.expect(f'MQTT_EVENT_CONNECTED: Test={cases[case]}', timeout=30) + with TlsServer(refuse_connection=True), connect_dut(dut, uri, cases[case]): + logging.info(f'Running {case}: ssl shall connect, but mqtt sends connect refusal') + dut.expect(f'MQTT_EVENT_ERROR: Test={cases[case]}', timeout=30) + dut.expect('MQTT ERROR: 0x5') # expecting 0x5 ... connection not authorized error + with TlsServer(client_cert=True) as server, connect_dut(dut, uri, cases[case]): + logging.info(f'Running {case}: server with client verification - handshake error since client presents no client certificate') + dut.expect(f'MQTT_EVENT_ERROR: Test={cases[case]}', timeout=30) + dut.expect('ESP-TLS ERROR: ESP_ERR_MBEDTLS_SSL_HANDSHAKE_FAILED') # expect ... handshake error (PEER_DID_NOT_RETURN_A_CERTIFICATE) + assert 'PEER_DID_NOT_RETURN_A_CERTIFICATE' in server.last_ssl_error() - publish_cfg['publish_topic'] = dut.app.sdkconfig.get('EXAMPLE_SUBSCRIBE_TOPIC').replace('"','') - publish_cfg['subscribe_topic'] = dut.app.sdkconfig.get('EXAMPLE_PUBLISH_TOPIC').replace('"','') - publish_cfg['broker_host_ssl'], publish_cfg['broker_port_ssl'] = get_host_port_from_dut(dut, 'EXAMPLE_BROKER_SSL_URI') - publish_cfg['broker_host_tcp'], publish_cfg['broker_port_tcp'] = get_host_port_from_dut(dut, 'EXAMPLE_BROKER_TCP_URI') - publish_cfg['broker_host_ws'], publish_cfg['broker_port_ws'] = get_host_port_from_dut(dut, 'EXAMPLE_BROKER_WS_URI') - publish_cfg['broker_host_wss'], publish_cfg['broker_port_wss'] = get_host_port_from_dut(dut, 'EXAMPLE_BROKER_WSS_URI') + for case in ['EXAMPLE_CONNECT_CASE_MUTUAL_AUTH', 'EXAMPLE_CONNECT_CASE_MUTUAL_AUTH_KEY_PWD']: + # These cases connect to server with both server and client verification (client key might be password protected) + with TlsServer(client_cert=True), connect_dut(dut, uri, cases[case]): + logging.info(f'Running {case}: server with client verification - expect to connect normally') + dut.expect(f'MQTT_EVENT_CONNECTED: Test={cases[case]}', timeout=30) - except Exception: - logging.error('ENV_TEST_FAILURE: Some mandatory PUBLISH test case not found in sdkconfig') - raise + case = 'EXAMPLE_CONNECT_CASE_INVALID_SERVER_CERT' + with TlsServer() as s, connect_dut(dut, uri, cases[case]): + logging.info(f'Running {case}: invalid server certificate on default server - expect ssl handshake error') + dut.expect(f'MQTT_EVENT_ERROR: Test={cases[case]}', timeout=30) + dut.expect('ESP-TLS ERROR: ESP_ERR_MBEDTLS_SSL_HANDSHAKE_FAILED') # expect ... handshake error (TLSV1_ALERT_UNKNOWN_CA) + if re.match('.*alert.*unknown.*ca',s.last_ssl_error(), flags=re.I) is None: + raise Exception(f'Unexpected ssl error from the server: {s.last_ssl_error()}') - # Initialize message sizes and repeat counts (if defined in the environment) - messages = [] - for i in count(0): - # Check env variable: MQTT_PUBLISH_MSG_{len|repeat}_{x} - env_dict = {var:'MQTT_PUBLISH_MSG_' + var + '_' + str(i) for var in ['len', 'repeat']} - if os.getenv(env_dict['len']) and os.getenv(env_dict['repeat']): - messages.append({var: int(os.getenv(env_dict[var])) for var in ['len', 'repeat']}) # type: ignore - continue - break - if not messages: # No message sizes present in the env - set defaults - messages = [{'len':0, 'repeat':5}, # zero-sized messages - {'len':2, 'repeat':10}, # short messages - {'len':200, 'repeat':3}, # long messages - {'len':20, 'repeat':50} # many medium sized - ] + case = 'EXAMPLE_CONNECT_CASE_MUTUAL_AUTH_BAD_CRT' + with TlsServer(client_cert=True) as s, connect_dut(dut, uri, cases[case]): + logging.info(f'Running {case}: Invalid client certificate on server with client verification - expect ssl handshake error') + dut.expect(f'MQTT_EVENT_ERROR: Test={cases[case]}', timeout=30) + dut.expect('ESP-TLS ERROR: ESP_ERR_MBEDTLS_SSL_HANDSHAKE_FAILED') # expect ... handshake error (CERTIFICATE_VERIFY_FAILED) + if 'CERTIFICATE_VERIFY_FAILED' not in s.last_ssl_error(): + raise Exception('Unexpected ssl error from the server {}'.format(s.last_ssl_error())) - # Iterate over all publish message properties - for transport in ['tcp', 'ssl', 'ws', 'wss']: - if publish_cfg['broker_host_' + transport] is None: - print('Skipping transport: {}...'.format(transport)) - continue - for enqueue in [0, 1]: - for qos in [0, 1, 2]: - for msg in messages: - logging.info(f'Starting Publish test: transport:{transport}, qos:{qos}, nr_of_msgs:{msg["repeat"]},' - f'msg_size:{msg["len"] * DEFAULT_MSG_SIZE}, enqueue:{enqueue}') - with MqttPublisher(dut, transport, qos, msg['len'], msg['repeat'], enqueue, publish_cfg): - pass + for case in ['EXAMPLE_CONNECT_CASE_NO_CERT', 'EXAMPLE_CONNECT_CASE_NO_CERT_ALPN']: + with TlsServer(use_alpn=True) as s, connect_dut(dut, uri, cases[case]): + logging.info(f'Running {case}: server with alpn - expect connect, check resolved protocol') + dut.expect(f'MQTT_EVENT_CONNECTED: Test={cases[case]}', timeout=30) + if case == 'EXAMPLE_CONNECT_CASE_NO_CERT': + assert s.get_negotiated_protocol() is None + elif case == 'EXAMPLE_CONNECT_CASE_NO_CERT_ALPN': + assert s.get_negotiated_protocol() == 'mymqtt' + else: + assert False, f'Unexpected negotiated protocol {s.get_negotiated_protocol()}' + finally: + dut.write('stop') + dut.write('destroy') -if __name__ == '__main__': - test_app_protocol_mqtt_publish_connect(dut=QemuDut if sys.argv[1:] == ['qemu'] else Dut) +@pytest.mark.esp32 +@pytest.mark.ethernet +def test_mqtt_connect( + dut: Dut, + log_performance: Callable[[str, object], None], +) -> None: + """ + steps: + 1. join AP + 2. connect to uri specified in the config + 3. send and receive data + """ + # check and log bin size + binary_file = os.path.join(dut.app.binary_path, 'mqtt_publish_connect_test.bin') + bin_size = os.path.getsize(binary_file) + log_performance('mqtt_publish_connect_test_bin_size', f'{bin_size // 1024} KB') + + ip = get_dut_ip(dut) + set_server_cert_cn(ip) + uri = f'mqtts://{ip}:{SERVER_PORT}' + + # Look for test case symbolic names and publish configs + cases = get_test_cases(dut) + dut.expect_exact('mqtt>', timeout=30) + run_cases(dut, uri, cases) diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/pytest_mqtt_publish_app.py b/tools/test_apps/protocols/mqtt/publish_connect_test/pytest_mqtt_publish_app.py new file mode 100644 index 0000000..f6b77f7 --- /dev/null +++ b/tools/test_apps/protocols/mqtt/publish_connect_test/pytest_mqtt_publish_app.py @@ -0,0 +1,229 @@ +# SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD +# SPDX-License-Identifier: Unlicense OR CC0-1.0 + +import contextlib +import difflib +import logging +import os +import random +import re +import ssl +import string +from itertools import count, product +from threading import Event, Lock +from typing import Any, Dict, List, Tuple, no_type_check + +import paho.mqtt.client as mqtt +import pexpect +import pytest +from pytest_embedded import Dut + +DEFAULT_MSG_SIZE = 16 + + +# Publisher class creating a python client to send/receive published data from esp-mqtt client +class MqttPublisher(mqtt.Client): + + def __init__(self, repeat, published, publish_cfg, log_details=False): # type: (MqttPublisher, int, int, dict, bool) -> None + self.sample_string = ''.join(random.choice(string.ascii_uppercase + string.ascii_lowercase + string.digits) for _ in range(DEFAULT_MSG_SIZE)) + self.log_details = log_details + self.repeat = repeat + self.publish_cfg = publish_cfg + self.expected_data = f'{self.sample_string * self.repeat}' + self.published = published + self.received = 0 + self.lock = Lock() + self.event_client_connected = Event() + self.event_client_got_all = Event() + transport = 'websockets' if self.publish_cfg['transport'] in ['ws', 'wss'] else 'tcp' + super().__init__('MqttTestRunner', userdata=0, transport=transport) + + def print_details(self, text): # type: (str) -> None + if self.log_details: + logging.info(text) + + def on_connect(self, mqttc: Any, obj: Any, flags: Any, rc:int) -> None: + self.event_client_connected.set() + + def on_connect_fail(self, mqttc: Any, obj: Any) -> None: + logging.error('Connect failed') + + def on_message(self, mqttc: Any, userdata: Any, msg: mqtt.MQTTMessage) -> None: + payload = msg.payload.decode('utf-8') + if payload == self.expected_data: + userdata += 1 + self.user_data_set(userdata) + self.received = userdata + if userdata == self.published: + self.event_client_got_all.set() + else: + differences = len(list(filter(lambda data: data[0] != data[1], zip(payload, self.expected_data)))) + logging.error(f'Payload differ in {differences} positions from expected data. received size: {len(payload)} expected size:' + f'{len(self.expected_data)}') + logging.info(f'Repetitions: {payload.count(self.sample_string)}') + logging.info(f'Pattern: {self.sample_string}') + logging.info(f'First : {payload[:DEFAULT_MSG_SIZE]}') + logging.info(f'Last : {payload[-DEFAULT_MSG_SIZE:]}') + matcher = difflib.SequenceMatcher(a=payload, b=self.expected_data) + for match in matcher.get_matching_blocks(): + logging.info(f'Match: {match}') + + def __enter__(self) -> Any: + qos = self.publish_cfg['qos'] + broker_host = self.publish_cfg['broker_host_' + self.publish_cfg['transport']] + broker_port = self.publish_cfg['broker_port_' + self.publish_cfg['transport']] + + try: + self.print_details('Connecting...') + if self.publish_cfg['transport'] in ['ssl', 'wss']: + self.tls_set(None, None, None, cert_reqs=ssl.CERT_NONE, tls_version=ssl.PROTOCOL_TLSv1_2, ciphers=None) + self.tls_insecure_set(True) + self.event_client_connected.clear() + self.loop_start() + self.connect(broker_host, broker_port, 60) + except Exception: + self.print_details(f'ENV_TEST_FAILURE: Unexpected error while connecting to broker {broker_host}') + raise + self.print_details(f'Connecting py-client to broker {broker_host}:{broker_port}...') + + if not self.event_client_connected.wait(timeout=30): + raise ValueError(f'ENV_TEST_FAILURE: Test script cannot connect to broker: {broker_host}') + self.event_client_got_all.clear() + self.subscribe(self.publish_cfg['subscribe_topic'], qos) + return self + + def __exit__(self, exc_type, exc_value, traceback): # type: (MqttPublisher, str, str, dict) -> None + self.disconnect() + self.loop_stop() + + +def get_configurations(dut: Dut) -> Dict[str,Any]: + publish_cfg = {} + try: + @no_type_check + def get_broker_from_dut(dut, config_option): + # logging.info('Option:', config_option, dut.app.sdkconfig.get(config_option)) + value = re.search(r'\:\/\/([^:]+)\:([0-9]+)', dut.app.sdkconfig.get(config_option)) + if value is None: + return None, None + return value.group(1), int(value.group(2)) + # Get publish test configuration + publish_cfg['publish_topic'] = dut.app.sdkconfig.get('EXAMPLE_SUBSCRIBE_TOPIC').replace('"','') + publish_cfg['subscribe_topic'] = dut.app.sdkconfig.get('EXAMPLE_PUBLISH_TOPIC').replace('"','') + publish_cfg['broker_host_ssl'], publish_cfg['broker_port_ssl'] = get_broker_from_dut(dut, 'EXAMPLE_BROKER_SSL_URI') + publish_cfg['broker_host_tcp'], publish_cfg['broker_port_tcp'] = get_broker_from_dut(dut, 'EXAMPLE_BROKER_TCP_URI') + publish_cfg['broker_host_ws'], publish_cfg['broker_port_ws'] = get_broker_from_dut(dut, 'EXAMPLE_BROKER_WS_URI') + publish_cfg['broker_host_wss'], publish_cfg['broker_port_wss'] = get_broker_from_dut(dut, 'EXAMPLE_BROKER_WSS_URI') + + except Exception: + logging.info('ENV_TEST_FAILURE: Some mandatory PUBLISH test case not found in sdkconfig') + raise + logging.info(f'configuration: {publish_cfg}') + return publish_cfg + + +@contextlib.contextmanager +def connected_and_subscribed(dut:Dut, transport:str, pattern:str, pattern_repetitions:int) -> Any: + dut.write(f'publish_setup {transport} {pattern} {pattern_repetitions}') + dut.write(f'start') + dut.expect(re.compile(rb'MQTT_EVENT_SUBSCRIBED'), timeout=60) + yield + dut.write(f'stop') + + +def get_scenarios() -> List[Dict[str, int]]: + scenarios = [] + # Initialize message sizes and repeat counts (if defined in the environment) + for i in count(0): + # Check env variable: MQTT_PUBLISH_MSG_{len|repeat}_{x} + env_dict = {var:'MQTT_PUBLISH_MSG_' + var + '_' + str(i) for var in ['len', 'repeat']} + if os.getenv(env_dict['len']) and os.getenv(env_dict['repeat']): + scenarios.append({var: int(os.getenv(env_dict[var])) for var in ['len', 'repeat']}) # type: ignore + continue + break + if not scenarios: # No message sizes present in the env - set defaults + scenarios = [{'len':0, 'repeat':5}, # zero-sized messages + {'len':2, 'repeat':5}, # short messages + {'len':200, 'repeat':3}, # long messages + ] + return scenarios + + +def get_timeout(test_case: Any) -> int: + transport, qos, enqueue, scenario = test_case + if transport in ['ws', 'wss'] or qos == 2: + return 90 + return 60 + + +def run_publish_test_case(dut: Dut, test_case: Any, publish_cfg: Any) -> None: + transport, qos, enqueue, scenario = test_case + if publish_cfg['broker_host_' + transport] is None: + pytest.skip(f'Skipping transport: {transport}...') + repeat = scenario['len'] + published = scenario['repeat'] + publish_cfg['qos'] = qos + publish_cfg['queue'] = enqueue + publish_cfg['transport'] = transport + test_timeout = get_timeout(test_case) + logging.info(f'Starting Publish test: transport:{transport}, qos:{qos}, nr_of_msgs:{published},' + f' msg_size:{repeat*DEFAULT_MSG_SIZE}, enqueue:{enqueue}') + with MqttPublisher(repeat, published, publish_cfg) as publisher, connected_and_subscribed(dut, transport, publisher.sample_string, scenario['len']): + msgs_published: List[mqtt.MQTTMessageInfo] = [] + dut.write(f'publish {publisher.published} {qos} {enqueue}') + assert publisher.event_client_got_all.wait(timeout=test_timeout), (f'Not all data received from ESP32: {transport} ' + f'qos={qos} received: {publisher.received} ' + f'expected: {publisher.published}') + logging.info(' - all data received from ESP32') + payload = publisher.sample_string * publisher.repeat + for _ in range(publisher.published): + with publisher.lock: + msg = publisher.publish(topic=publisher.publish_cfg['publish_topic'], payload=payload, qos=qos) + if qos > 0: + msgs_published.append(msg) + logging.info(f'Published: {len(msgs_published)}') + while msgs_published: + msgs_published = [msg for msg in msgs_published if msg.is_published()] + + try: + dut.expect(re.compile(rb'Correct pattern received exactly x times'), timeout=test_timeout) + except pexpect.exceptions.ExceptionPexpect: + dut.write(f'publish_report') + dut.expect(re.compile(rb'Test Report'), timeout=30) + raise + logging.info('ESP32 received all data from runner') + + +stress_scenarios = [{'len':20, 'repeat':50}] # many medium sized +transport_cases = ['tcp', 'ws', 'wss', 'ssl'] +qos_cases = [0, 1, 2] +enqueue_cases = [0, 1] + + +def make_cases(scenarios: List[Dict[str, int]]) -> List[Tuple[str, int, int, Dict[str, int]]]: + return [test_case for test_case in product(transport_cases, qos_cases, enqueue_cases, scenarios)] + + +test_cases = make_cases(get_scenarios()) +stress_test_cases = make_cases(stress_scenarios) + + +@pytest.mark.esp32 +@pytest.mark.ethernet +@pytest.mark.parametrize('test_case', test_cases) +def test_mqtt_publish(dut: Dut, test_case: Any) -> None: + publish_cfg = get_configurations(dut) + dut.expect(re.compile(rb'mqtt>'), timeout=30) + dut.confirm_write('init', expect_pattern='init', timeout=30) + run_publish_test_case(dut, test_case, publish_cfg) + + +@pytest.mark.esp32 +@pytest.mark.ethernet +@pytest.mark.nightly_run +@pytest.mark.parametrize('test_case', stress_test_cases) +def test_mqtt_publish_stress(dut: Dut, test_case: Any) -> None: + publish_cfg = get_configurations(dut) + dut.expect(re.compile(rb'mqtt>'), timeout=30) + dut.write('init') + run_publish_test_case(dut, test_case, publish_cfg) From 24dd39433cde893396c55928ed375a18b7173c25 Mon Sep 17 00:00:00 2001 From: Euripedes Rocha Date: Thu, 4 May 2023 14:22:17 +0200 Subject: [PATCH 183/231] feat(mqtt/example): Adds custom outbox example Adds example presenting how to customize esp-mqtt outbox. --- .../mqtt/custom_outbox/CMakeLists.txt | 18 + .../protocols/mqtt/custom_outbox/README.md | 100 +++++ .../mqtt/custom_outbox/main/CMakeLists.txt | 3 + .../mqtt/custom_outbox/main/Kconfig.projbuild | 13 + .../mqtt/custom_outbox/main/app_main.c | 173 ++++++++ .../mqtt/custom_outbox/main/custom_outbox.cpp | 393 ++++++++++++++++++ .../mqtt/custom_outbox/main/idf_component.yml | 3 + .../mqtt/custom_outbox/sdkconfig.defaults | 2 + 8 files changed, 705 insertions(+) create mode 100644 examples/protocols/mqtt/custom_outbox/CMakeLists.txt create mode 100644 examples/protocols/mqtt/custom_outbox/README.md create mode 100644 examples/protocols/mqtt/custom_outbox/main/CMakeLists.txt create mode 100644 examples/protocols/mqtt/custom_outbox/main/Kconfig.projbuild create mode 100644 examples/protocols/mqtt/custom_outbox/main/app_main.c create mode 100644 examples/protocols/mqtt/custom_outbox/main/custom_outbox.cpp create mode 100644 examples/protocols/mqtt/custom_outbox/main/idf_component.yml create mode 100644 examples/protocols/mqtt/custom_outbox/sdkconfig.defaults diff --git a/examples/protocols/mqtt/custom_outbox/CMakeLists.txt b/examples/protocols/mqtt/custom_outbox/CMakeLists.txt new file mode 100644 index 0000000..4823b13 --- /dev/null +++ b/examples/protocols/mqtt/custom_outbox/CMakeLists.txt @@ -0,0 +1,18 @@ +# The following four lines of boilerplate have to be in your project's CMakeLists +# in this exact order for cmake to work correctly +cmake_minimum_required(VERSION 3.16) + +include($ENV{IDF_PATH}/tools/cmake/project.cmake) +project(mqtt_tcp_custom_outbox) + +# Add custom outbox implementation to mqtt component +idf_component_get_property(mqtt mqtt COMPONENT_LIB) +target_sources(${mqtt} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/main/custom_outbox.cpp) + +# Our C++ needs an extra dependency to mqtt component, so we add it to mqtt component. +# This is needed because we are adding another source to the mqtt component and the build +# system needs to be aware of it to be able to compile and link the mqtt component. +# First we get our dependency +idf_component_get_property(pthread pthread COMPONENT_LIB) +# And them we link the components +target_link_libraries(${mqtt} ${pthread}) diff --git a/examples/protocols/mqtt/custom_outbox/README.md b/examples/protocols/mqtt/custom_outbox/README.md new file mode 100644 index 0000000..29a3142 --- /dev/null +++ b/examples/protocols/mqtt/custom_outbox/README.md @@ -0,0 +1,100 @@ +| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C6 | ESP32-H2 | ESP32-S2 | ESP32-S3 | +| ----------------- | ----- | -------- | -------- | -------- | -------- | -------- | -------- | + +# ESP-MQTT custom outbox sample application +(See the README.md file in the upper level 'examples' directory for more information about examples.) + +This example is a slightly modified version of the tcp example to show how to configure a custom outbox. +This example connects to the broker URI selected using `idf.py menuconfig` (using mqtt tcp transport) and as a demonstration subscribes/unsubscribes and send a message on certain topic. +(Please note that the public broker is maintained by the community so may not be always available, for details please see this [disclaimer](https://iot.eclipse.org/getting-started/#sandboxes)) + +Note: If the URI equals `FROM_STDIN` then the broker address is read from stdin upon application startup (used for testing) + +It uses ESP-MQTT library which implements mqtt client to connect to mqtt broker. + +## Necessary changes to customize the outbox + +To customize the outbox the first step is to enable it in the menuconfig option. + +With this option enabled, the default implementation isn't defined and the function definition needs to be added to mqtt component. +Any extra dependencies needed by the new sources also need to be added to the mqtt component. Refer to the example CMakeLists.txt file +for the details on how to do it. + +## The custom outbox in the example + +For the sake of this example the customized outbox implements the same functionalits of the regular but using C++ as a language. + +The implementation uses [C++ Polymorphic memory resources]() to control memory allocations and limit the usage of the memory. + +## How to use example + +### Hardware Required + +This example can be executed on any ESP32 board, the only required interface is WiFi and connection to internet. + +### Configure the project + +* Open the project configuration menu (`idf.py menuconfig`) +* Configure Wi-Fi or Ethernet under "Example Connection Configuration" menu. See "Establishing Wi-Fi or Ethernet Connection" section in [examples/protocols/README.md](../../README.md) for more details. + +Note that the mandatory configurations for this example, mqtt custom outbox and C++ exceptions are automatically added by the `sdkconfig.defaults` file. +### Build and Flash + +Build the project and flash it to the board, then run monitor tool to view serial output: + +``` +idf.py -p PORT flash monitor +``` + +(To exit the serial monitor, type ``Ctrl-]``.) + +See the Getting Started Guide for full steps to configure and use ESP-IDF to build projects. + +## Example Output + +``` +I (4635) example_common: Connected to example_netif_sta +I (4645) example_common: - IPv4 address: 192.168.33.206, +I (4645) example_common: - IPv6 address: fe80:0000:0000:0000:7e9e:bdff:fecf:00c0, type: ESP_IP6_ADDR_IS_LINK_LOCAL +I (4655) Monotonic: Monotonic: 400 bytes allocated, 400 total bytes in use +I (4665) Monotonic: Monotonic: 1000 bytes allocated, 1400 total bytes in use +I (4675) Monotonic: Monotonic: 128 bytes allocated, 1528 total bytes in use +I (4685) Pool: Pool: 32 bytes allocated, 32 total bytes in use +I (4685) Monotonic: Monotonic: 7688 bytes allocated, 9216 total bytes in use +I (4695) Monotonic: Monotonic: 128 bytes allocated, 9344 total bytes in use +I (4705) Pool: Pool: 480 bytes allocated, 512 total bytes in use +I (4715) Monotonic: Monotonic: 992 bytes allocated, 10336 total bytes in use +I (4715) Monotonic: Monotonic: 128 bytes allocated, 10464 total bytes in use +I (4725) Pool: Pool: 23 bytes allocated, 535 total bytes in use +I (4735) MQTT_EXAMPLE: Enqueued msg_id=14345 +I (4735) Pool: Pool: 29 bytes allocated, 564 total bytes in use +I (4745) MQTT_EXAMPLE: Enqueued msg_id=3507 +I (4745) MQTT_EXAMPLE: Other event id:7 +I (4755) main_task: Returned from app_main() +I (5085) MQTT_EXAMPLE: MQTT_EVENT_CONNECTED +I (5085) Pool: Pool: 23 bytes allocated, 587 total bytes in use +I (5085) MQTT_EXAMPLE: sent publish successful, msg_id=47425 +I (5085) Pool: Pool: 18 bytes allocated, 605 total bytes in use +I (5095) MQTT_EXAMPLE: sent subscribe successful, msg_id=60709 +I (5105) Pool: Pool: 18 bytes allocated, 623 total bytes in use +I (5105) MQTT_EXAMPLE: sent subscribe successful, msg_id=33273 +I (5395) Pool: Pool: 23 bytes deallocated, 623 total bytes in use +I (5395) MQTT_EXAMPLE: MQTT_EVENT_PUBLISHED, msg_id=47425 +I (6005) Pool: Pool: 18 bytes deallocated, 623 total bytes in use +I (6005) MQTT_EXAMPLE: MQTT_EVENT_SUBSCRIBED, msg_id=60709 +I (6005) MQTT_EXAMPLE: sent publish successful, msg_id=0 +I (6015) Pool: Pool: 18 bytes deallocated, 623 total bytes in use +I (6015) MQTT_EXAMPLE: MQTT_EVENT_SUBSCRIBED, msg_id=33273 +I (6025) MQTT_EXAMPLE: sent publish successful, msg_id=0 +I (6035) MQTT_EXAMPLE: MQTT_EVENT_DATA +TOPIC=/topic/qos1 +DATA=data_3 +I (6315) MQTT_EXAMPLE: MQTT_EVENT_DATA +TOPIC=/topic/qos1 +DATA=data_3 +I (6315) Pool: Pool: 23 bytes deallocated, 623 total bytes in use +I (6315) MQTT_EXAMPLE: MQTT_EVENT_PUBLISHED, msg_id=14345 +I (6615) MQTT_EXAMPLE: MQTT_EVENT_DATA +TOPIC=/topic/qos0 +DATA=data +``` diff --git a/examples/protocols/mqtt/custom_outbox/main/CMakeLists.txt b/examples/protocols/mqtt/custom_outbox/main/CMakeLists.txt new file mode 100644 index 0000000..cb970c8 --- /dev/null +++ b/examples/protocols/mqtt/custom_outbox/main/CMakeLists.txt @@ -0,0 +1,3 @@ +idf_component_register(SRCS "app_main.c" + INCLUDE_DIRS "." + ) diff --git a/examples/protocols/mqtt/custom_outbox/main/Kconfig.projbuild b/examples/protocols/mqtt/custom_outbox/main/Kconfig.projbuild new file mode 100644 index 0000000..c11539f --- /dev/null +++ b/examples/protocols/mqtt/custom_outbox/main/Kconfig.projbuild @@ -0,0 +1,13 @@ +menu "Example Configuration" + + config BROKER_URL + string "Broker URL" + default "mqtt://mqtt.eclipseprojects.io" + help + URL of the broker to connect to + + config BROKER_URL_FROM_STDIN + bool + default y if BROKER_URL = "FROM_STDIN" + +endmenu diff --git a/examples/protocols/mqtt/custom_outbox/main/app_main.c b/examples/protocols/mqtt/custom_outbox/main/app_main.c new file mode 100644 index 0000000..b30bc46 --- /dev/null +++ b/examples/protocols/mqtt/custom_outbox/main/app_main.c @@ -0,0 +1,173 @@ +/* + * SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Unlicense OR CC0-1.0 + */ +/* MQTT (over TCP) Example with custom outbox + + This example 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 +#include +#include +#include "esp_system.h" +#include "nvs_flash.h" +#include "esp_event.h" +#include "esp_netif.h" +#include "protocol_examples_common.h" + +#include "esp_log.h" +#include "mqtt_client.h" + +static const char *TAG = "MQTT_EXAMPLE"; + + +static void log_error_if_nonzero(const char *message, int error_code) +{ + if (error_code != 0) { + ESP_LOGE(TAG, "Last error %s: 0x%x", message, error_code); + } +} + +/* + * @brief Event handler registered to receive MQTT events + * + * This function is called by the MQTT client event loop. + * + * @param handler_args user data registered to the event. + * @param base Event base for the handler(always MQTT Base in this example). + * @param event_id The id for the received event. + * @param event_data The data for the event, esp_mqtt_event_handle_t. + */ +static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data) +{ + ESP_LOGD(TAG, "Event dispatched from event loop base=%s, event_id=%" PRIi32 "", base, event_id); + esp_mqtt_event_handle_t event = event_data; + esp_mqtt_client_handle_t client = event->client; + int msg_id; + switch ((esp_mqtt_event_id_t)event_id) { + case MQTT_EVENT_CONNECTED: + ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED"); + msg_id = esp_mqtt_client_publish(client, "/topic/qos1", "data_3", 0, 1, 0); + ESP_LOGI(TAG, "sent publish successful, msg_id=%d", msg_id); + + msg_id = esp_mqtt_client_subscribe(client, "/topic/qos0", 0); + ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id); + + msg_id = esp_mqtt_client_subscribe(client, "/topic/qos1", 1); + ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id); + + break; + case MQTT_EVENT_DISCONNECTED: + ESP_LOGI(TAG, "MQTT_EVENT_DISCONNECTED"); + break; + + case MQTT_EVENT_SUBSCRIBED: + ESP_LOGI(TAG, "MQTT_EVENT_SUBSCRIBED, msg_id=%d", event->msg_id); + msg_id = esp_mqtt_client_publish(client, "/topic/qos0", "data", 0, 0, 0); + ESP_LOGI(TAG, "sent publish successful, msg_id=%d", msg_id); + break; + case MQTT_EVENT_UNSUBSCRIBED: + ESP_LOGI(TAG, "MQTT_EVENT_UNSUBSCRIBED, msg_id=%d", event->msg_id); + break; + case MQTT_EVENT_PUBLISHED: + ESP_LOGI(TAG, "MQTT_EVENT_PUBLISHED, msg_id=%d", event->msg_id); + break; + case MQTT_EVENT_DATA: + ESP_LOGI(TAG, "MQTT_EVENT_DATA"); + printf("TOPIC=%.*s\r\n", event->topic_len, event->topic); + printf("DATA=%.*s\r\n", event->data_len, event->data); + break; + case MQTT_EVENT_ERROR: + ESP_LOGI(TAG, "MQTT_EVENT_ERROR"); + if (event->error_handle->error_type == MQTT_ERROR_TYPE_TCP_TRANSPORT) { + log_error_if_nonzero("reported from esp-tls", event->error_handle->esp_tls_last_esp_err); + log_error_if_nonzero("reported from tls stack", event->error_handle->esp_tls_stack_err); + log_error_if_nonzero("captured as transport's socket errno", event->error_handle->esp_transport_sock_errno); + ESP_LOGI(TAG, "Last errno string (%s)", strerror(event->error_handle->esp_transport_sock_errno)); + + } + break; + default: + ESP_LOGI(TAG, "Other event id:%d", event->event_id); + break; + } +} + +static void mqtt_app_start(void) +{ + esp_mqtt_client_config_t mqtt_cfg = { + .broker.address.uri = CONFIG_BROKER_URL, + }; +#if CONFIG_BROKER_URL_FROM_STDIN + char line[128]; + + if (strcmp(mqtt_cfg.broker.address.uri, "FROM_STDIN") == 0) { + int count = 0; + printf("Please enter url of mqtt broker\n"); + while (count < 128) { + int c = fgetc(stdin); + if (c == '\n') { + line[count] = '\0'; + break; + } else if (c > 0 && c < 127) { + line[count] = c; + ++count; + } + vTaskDelay(10 / portTICK_PERIOD_MS); + } + mqtt_cfg.broker.address.uri = line; + printf("Broker url: %s\n", line); + } else { + ESP_LOGE(TAG, "Configuration mismatch: wrong broker url"); + abort(); + } +#endif /* CONFIG_BROKER_URL_FROM_STDIN */ + + esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg); + /* The last argument may be used to pass data to the event handler, in this example mqtt_event_handler */ + esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, NULL); + + /*Let's enqueue a few messages to the outbox to see the allocations*/ + int msg_id; + msg_id = esp_mqtt_client_enqueue(client, "/topic/qos1", "data_3", 0, 1, 0, true); + ESP_LOGI(TAG, "Enqueued msg_id=%d", msg_id); + msg_id = esp_mqtt_client_enqueue(client, "/topic/qos2", "QoS2 message", 0, 2, 0, true); + ESP_LOGI(TAG, "Enqueued msg_id=%d", msg_id); + + /* Now we start the client and it's possible to see the memory usage for the operations in the outbox. */ + esp_mqtt_client_start(client); +} + +void app_main(void) +{ + ESP_LOGI(TAG, "[APP] Startup.."); + ESP_LOGI(TAG, "[APP] Free memory: %" PRIu32 " bytes", esp_get_free_heap_size()); + ESP_LOGI(TAG, "[APP] IDF version: %s", esp_get_idf_version()); + + esp_log_level_set("*", ESP_LOG_INFO); + esp_log_level_set("mqtt_client", ESP_LOG_VERBOSE); + esp_log_level_set("MQTT_EXAMPLE", ESP_LOG_VERBOSE); + esp_log_level_set("TRANSPORT_BASE", ESP_LOG_VERBOSE); + esp_log_level_set("esp-tls", ESP_LOG_VERBOSE); + esp_log_level_set("TRANSPORT", ESP_LOG_VERBOSE); + esp_log_level_set("custom_outbox", ESP_LOG_VERBOSE); + + ESP_ERROR_CHECK(nvs_flash_init()); + ESP_ERROR_CHECK(esp_netif_init()); + ESP_ERROR_CHECK(esp_event_loop_create_default()); + + /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig. + * Read "Establishing Wi-Fi or Ethernet Connection" section in + * examples/protocols/README.md for more information about this function. + */ + ESP_ERROR_CHECK(example_connect()); + + mqtt_app_start(); +} diff --git a/examples/protocols/mqtt/custom_outbox/main/custom_outbox.cpp b/examples/protocols/mqtt/custom_outbox/main/custom_outbox.cpp new file mode 100644 index 0000000..2db9d7e --- /dev/null +++ b/examples/protocols/mqtt/custom_outbox/main/custom_outbox.cpp @@ -0,0 +1,393 @@ +/* + * SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Unlicense OR CC0-1.0 + */ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "esp_log.h" +#include "mqtt_outbox.h" + +constexpr auto TAG = "custom_outbox"; + +/* + * The trace resource class is created here as an example on how to build a custom memory resource + * The class is only needed to show where we are allocating from and to track allocations and deallocations. + */ +class trace_resource : public std::pmr::memory_resource { +public: + explicit trace_resource(std::string resource_name, std::pmr::memory_resource *upstream_resource = std::pmr::get_default_resource()) : upstream{upstream_resource}, name{std::move(resource_name)} {} + [[nodiscard]] std::string_view get_name() const noexcept + { + return std::string_view(name); + } + [[nodiscard]] auto upstream_resource() const + { + return upstream; + } +private: + void *do_allocate(std::size_t bytes, std::size_t alignment) override + { + auto *allocated = upstream->allocate(bytes, alignment); + allocated_total += bytes; + ESP_LOGI(name.c_str(), "%s: %zu bytes allocated, %zu total bytes in use", name.c_str(), bytes, allocated_total); + return allocated; + } + void do_deallocate(void *ptr, std::size_t bytes, std::size_t alignment) override + { + upstream->deallocate(ptr, bytes, alignment); + ESP_LOGI(name.c_str(), "%s: %zu bytes deallocated, %zu total bytes in use", name.c_str(), bytes, allocated_total); + } + + [[nodiscard]] bool do_is_equal(const std::pmr::memory_resource &other) const noexcept override + { + return this == &other; + } + size_t allocated_total{}; + std::pmr::memory_resource *upstream; + std::string name; +}; + +struct outbox_item { + /* Defining the allocator_type to let compiler know that our type is allocator aware, + * This way the allocator used for the outbox is propagated to the messages*/ + using allocator_type = std::pmr::polymorphic_allocator<>; + + /* Few strong types to diferetiate parameters*/ + enum class id_t : int {}; + enum class type_t : int {}; + enum class qos_t : int {}; + + /* Allocator aware constructors */ + outbox_item( + std::pmr::vector message, + id_t msg_id, + type_t msg_type, + qos_t msg_qos, + outbox_tick_t tick, + pending_state_t pending_state, + allocator_type alloc = {} + ) : message(std::move(message), alloc), id(msg_id), type(msg_type), qos(msg_qos), tick(tick), pending_state(pending_state) {} + + /*Copy and move constructors have an extra allocator parameter, for copy default and allocator aware are the same.*/ + outbox_item(const outbox_item &other, allocator_type alloc = {}) : message(other.message, alloc), id(other.id), type(other.type), qos(other.qos), tick(other.tick), pending_state(other.pending_state) {} + outbox_item(outbox_item &&other, allocator_type alloc) noexcept : message(std::move(other.message), alloc), id(other.id), type(other.type), qos(other.qos), tick(other.tick), pending_state(other.pending_state) + {} + + outbox_item(const outbox_item &) = default; + outbox_item(outbox_item &&other) = default; + outbox_item &operator=(const outbox_item &rhs) = default; + outbox_item &operator=(outbox_item &&other) = default; + ~outbox_item() = default; + + /* Getters to support outbox operation */ + [[nodiscard]] auto state() const noexcept + { + return pending_state; + } + + [[nodiscard]] allocator_type get_allocator() const + { + return message.get_allocator(); + } + + void set(pending_state state) noexcept + { + pending_state = state; + } + + void set(outbox_tick_t n_tick) noexcept + { + tick = n_tick; + } + + [[nodiscard]] auto get_id() const noexcept + { + return id; + } + + [[nodiscard]] auto get_type() const noexcept + { + return type; + } + + [[nodiscard]] auto get_tick() const noexcept + { + return tick; + } + + [[nodiscard]] auto get_data(size_t *len, uint16_t *msg_id, int *msg_type, int *msg_qos) + { + *len = message.size(); + *msg_id = static_cast(id); + *msg_type = static_cast(type); + *msg_qos = static_cast(qos); + return message.data(); + } + + [[nodiscard]] auto get_size() const noexcept + { + return message.size(); + } + +private: + std::pmr::vector message; + id_t id; + type_t type; + qos_t qos; + outbox_tick_t tick; + pending_state_t pending_state; +}; + +/* + * For the outbox_t we let the special member functions as default and + * we don't extend the allocator aware versions for the sake of the simplicity, since the operations are not needed in the usage. + */ +struct outbox_t { + using allocator_type = std::pmr::polymorphic_allocator<>; + explicit outbox_t(allocator_type alloc = {}) : queue(alloc) {} + + outbox_item_handle_t get(outbox_item::id_t msg_id) + { + if (auto item = std::ranges::find_if(queue, [msg_id](auto & item) { + return item.get_id() == msg_id; + }); + item != std::end(queue)) { + return &(*item); + } + return nullptr; + } + + int delete_expired(outbox_tick_t current_tick, outbox_tick_t timeout) + { + return std::erase_if(queue, [current_tick, timeout, this](const outbox_item & item) { + if (current_tick - item.get_tick() > timeout) { + total_size -= item.get_size(); + return true; + } + return false; + }); + } + + outbox_item::id_t delete_single_expired(outbox_tick_t current_tick, outbox_tick_t timeout) + { + if (auto erase = std::ranges::find_if(queue, [current_tick, timeout](auto & item) { + return (current_tick - item.get_tick() > timeout); + }); erase != std::end(queue)) { + auto msg_id = erase->get_id(); + total_size -= erase->get_size(); + queue.erase(erase); + return msg_id; + } + return outbox_item::id_t{-1}; + } + + auto erase(outbox_item_handle_t to_erase) + { + return erase_if([to_erase](auto & item) { + return &item == to_erase; + }); + } + + auto erase(outbox_item::id_t msg_id, outbox_item::type_t msg_type) + { + return erase_if([msg_id, msg_type](auto & item) { + return (item.get_id() == msg_id && (item.get_type() == msg_type)); + }); + } + + [[nodiscard]] auto size() const noexcept + { + return total_size; + } + + void clear() + { + queue.clear(); + } + + outbox_item_handle_t enqueue(outbox_message_handle_t message, outbox_tick_t tick) noexcept + { + try { + auto &item = + queue.emplace_back(std::pmr::vector {message->data, message->data + message->len}, + outbox_item::id_t{message->msg_id}, + outbox_item::type_t{message->msg_type}, + outbox_item::qos_t{message->msg_qos}, + tick, + QUEUED + ); + total_size += item.get_size(); + ESP_LOGD(TAG, "ENQUEUE msgid=%d, msg_type=%d, len=%d, size=%" PRIu64, message->msg_id, message->msg_type, message->len + message->remaining_len, outbox_get_size(this)); + return &item; + } catch (const std::exception &e) { + return nullptr; + } + } + + outbox_item_handle_t dequeue(pending_state_t state, outbox_tick_t *tick) + { + if (auto item = std::ranges::find_if(queue, [state](auto & item) { + return item.state() == state; + }); + item != std::end(queue)) { + if (tick != nullptr) { + *tick = item->get_tick(); + } + return &(*item); + } + return nullptr; + } + [[nodiscard]] allocator_type get_allocator() const + { + return queue.get_allocator(); + } +private: + [[nodiscard]] esp_err_t erase_if(std::predicate auto &&predicate) + { + if (auto to_erase = std::ranges::find_if(queue, predicate); to_erase != std::end(queue)) { + total_size -= to_erase->get_size(); + queue.erase(to_erase); + return ESP_OK; + } + return ESP_FAIL; + } + std::size_t total_size{}; + std::pmr::deque queue ; +}; + +extern "C" { + + outbox_handle_t outbox_init() + { + /* First we create a fixed size memory buffer to be used. */ + static constexpr auto work_memory_size = 16 * 1024; + static std::array resource_buffer{}; + try { + /* + * Since the outbox is managed by a C API we can't rely on C++ automatic cleanup and smart pointers but, on production code it would be better to add the + * memory resources to outbox_t, applying RAII principles, and make only outbox_item allocator aware. For the sake of the example we are keeping them + * separated to explictly show the relations. + * First we create the monotonic buffer and add null_memory_resource as upstream. This way if our working memory is exausted an exception is thrown. + */ + auto *monotonic_resource = new std::pmr::monotonic_buffer_resource{resource_buffer.data(), resource_buffer.size(), std::pmr::null_memory_resource()}; + /*Here we add our custom trace wrapper type to trace allocations and deallocations*/ + auto *trace_monotonic = new trace_resource("Monotonic", monotonic_resource); + + /* We compose monotonic buffer with pool resource, since the monotonic deallocate is a no-op and we need to remove messages to not go out of memory.*/ + auto *pool_resource = new std::pmr::unsynchronized_pool_resource{trace_monotonic}; + auto *trace_pool = new trace_resource("Pool", pool_resource); + /* Our outbox class is created using the trace_pool as memory resource */ + auto *outbox = new outbox_t{trace_pool}; + return outbox; + } catch (const std::exception &e) { + ESP_LOGD(TAG, "Not enough memory to construct the outbox, review the resource_buffer size"); + return nullptr; + + } + } + + outbox_item_handle_t outbox_enqueue(outbox_handle_t outbox, outbox_message_handle_t message, outbox_tick_t tick) + { + return outbox->enqueue(message, tick); + } + + outbox_item_handle_t outbox_get(outbox_handle_t outbox, int msg_id) + { + return outbox->get(outbox_item::id_t{msg_id}); + } + + outbox_item_handle_t outbox_dequeue(outbox_handle_t outbox, pending_state_t pending, outbox_tick_t *tick) + { + return outbox->dequeue(pending, tick); + } +} + +uint8_t *outbox_item_get_data(outbox_item_handle_t item, size_t *len, uint16_t *msg_id, int *msg_type, int *qos) +{ + if (item == nullptr) { + return nullptr; + } + return item->get_data(len, msg_id, msg_type, qos); +} + +esp_err_t outbox_delete_item(outbox_handle_t outbox, outbox_item_handle_t item_to_delete) +{ + return outbox->erase(item_to_delete); + +} + +esp_err_t outbox_delete(outbox_handle_t outbox, int msg_id, int msg_type) +{ + return outbox->erase(outbox_item::id_t{msg_id}, outbox_item::type_t{msg_type}); +} + +int outbox_delete_single_expired(outbox_handle_t outbox, outbox_tick_t current_tick, outbox_tick_t timeout) +{ + return static_cast(outbox->delete_single_expired(current_tick, timeout)); +} + +int outbox_delete_expired(outbox_handle_t outbox, outbox_tick_t current_tick, outbox_tick_t timeout) +{ + return outbox->delete_expired(current_tick, timeout); +} + +esp_err_t outbox_set_pending(outbox_handle_t outbox, int msg_id, pending_state_t pending) +{ + if (auto *item = outbox->get(outbox_item::id_t{msg_id}); item != nullptr) { + item->set(pending); + return ESP_OK; + } + return ESP_FAIL; +} + +pending_state_t outbox_item_get_pending(outbox_item_handle_t item) +{ + if (item != nullptr) { + return item->state(); + } + return QUEUED; +} + +esp_err_t outbox_set_tick(outbox_handle_t outbox, int msg_id, outbox_tick_t tick) +{ + if (auto *item = outbox->get(outbox_item::id_t{msg_id}); item != nullptr) { + item->set(tick); + return ESP_OK; + } + return ESP_FAIL; +} + +uint64_t outbox_get_size(outbox_handle_t outbox) +{ + return outbox->size(); +} + +void outbox_delete_all_items(outbox_handle_t outbox) +{ + outbox->clear(); +} + +void outbox_destroy(outbox_handle_t outbox) +{ + auto *trace_pool = static_cast(outbox->get_allocator().resource()); + auto *pool_resource = static_cast(trace_pool->upstream_resource()); + auto *trace_monotonic = static_cast(pool_resource->upstream_resource()); + auto *monotonic_resource = static_cast(trace_monotonic->upstream_resource()); + + delete monotonic_resource; + delete trace_monotonic; + delete pool_resource; + delete trace_pool; + delete outbox; +} diff --git a/examples/protocols/mqtt/custom_outbox/main/idf_component.yml b/examples/protocols/mqtt/custom_outbox/main/idf_component.yml new file mode 100644 index 0000000..7181948 --- /dev/null +++ b/examples/protocols/mqtt/custom_outbox/main/idf_component.yml @@ -0,0 +1,3 @@ +dependencies: + protocol_examples_common: + path: ${IDF_PATH}/examples/common_components/protocol_examples_common diff --git a/examples/protocols/mqtt/custom_outbox/sdkconfig.defaults b/examples/protocols/mqtt/custom_outbox/sdkconfig.defaults new file mode 100644 index 0000000..385da31 --- /dev/null +++ b/examples/protocols/mqtt/custom_outbox/sdkconfig.defaults @@ -0,0 +1,2 @@ +CONFIG_MQTT_CUSTOM_OUTBOX=y +CONFIG_COMPILER_CXX_EXCEPTIONS=y From ad3667bf3a7cb9451fe686413a5b2ef4234d2165 Mon Sep 17 00:00:00 2001 From: Euripedes Rocha Date: Fri, 20 Oct 2023 07:44:35 +0200 Subject: [PATCH 184/231] ci(mqtt): Move publish tests to nightly Publish test cases are prone to fail depending on Ci load. --- .../mqtt/publish_connect_test/pytest_mqtt_publish_app.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/pytest_mqtt_publish_app.py b/tools/test_apps/protocols/mqtt/publish_connect_test/pytest_mqtt_publish_app.py index f6b77f7..544c0b7 100644 --- a/tools/test_apps/protocols/mqtt/publish_connect_test/pytest_mqtt_publish_app.py +++ b/tools/test_apps/protocols/mqtt/publish_connect_test/pytest_mqtt_publish_app.py @@ -210,6 +210,7 @@ stress_test_cases = make_cases(stress_scenarios) @pytest.mark.esp32 @pytest.mark.ethernet +@pytest.mark.nightly_run @pytest.mark.parametrize('test_case', test_cases) def test_mqtt_publish(dut: Dut, test_case: Any) -> None: publish_cfg = get_configurations(dut) From 44750a277beff3feb4177cbe4fc55ba84840d921 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Thu, 12 Oct 2023 17:16:26 +0200 Subject: [PATCH 185/231] feat(example): Use PPP config in one MQTT example to be build in CI, but only running PPP on UART to avoid adding tinyusb dependency. --- examples/protocols/mqtt/tcp/sdkconfig.ci.ppp_connect | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 examples/protocols/mqtt/tcp/sdkconfig.ci.ppp_connect diff --git a/examples/protocols/mqtt/tcp/sdkconfig.ci.ppp_connect b/examples/protocols/mqtt/tcp/sdkconfig.ci.ppp_connect new file mode 100644 index 0000000..7366a90 --- /dev/null +++ b/examples/protocols/mqtt/tcp/sdkconfig.ci.ppp_connect @@ -0,0 +1,3 @@ +CONFIG_EXAMPLE_CONNECT_WIFI=n +CONFIG_EXAMPLE_CONNECT_PPP=y +CONFIG_EXAMPLE_CONNECT_PPP_DEVICE_UART=y From 0533275d61387d56a119574591ef11941c8161be Mon Sep 17 00:00:00 2001 From: Mahavir Jain Date: Mon, 4 Dec 2023 12:22:01 +0530 Subject: [PATCH 186/231] refactor: migrate atecc608a_ecdsa example to crypto authlib repository - ESP32-WROOM-32SE has been discontinued and marked as NRND - This change removes all references to ESP32-WROOM-32SE from IDF - The example has been migrated to esp-cryptoauthlib repository and it can be used through the component manager (https://components.espressif.com/components/espressif/esp-cryptoauthlib) --- docs/en/api-reference/protocols/mqtt.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/en/api-reference/protocols/mqtt.rst b/docs/en/api-reference/protocols/mqtt.rst index 4160e8e..12139d4 100644 --- a/docs/en/api-reference/protocols/mqtt.rst +++ b/docs/en/api-reference/protocols/mqtt.rst @@ -145,7 +145,7 @@ It is possible to set authentication parameters through the :cpp:class:`authenti * :cpp:member:`password `: use a password by setting * :cpp:member:`certificate ` and :cpp:member:`key `: mutual authentication with TLS, and both can be provided in PEM or DER format - * :cpp:member:`use_secure_element `: use secure element available in ESP32-WROOM-32SE + * :cpp:member:`use_secure_element `: use secure element (ATECC608A) interfaced to ESP32 * :cpp:member:`ds_data `: use Digital Signature Peripheral available in some Espressif devices Session From e9bcb95bc46dd7b51228c6c0a112e02344215bc6 Mon Sep 17 00:00:00 2001 From: Shang Zhou Date: Wed, 20 Dec 2023 15:29:39 +0800 Subject: [PATCH 187/231] docs: Update CN translation for esp_tls.rst, mqtt.rst, and peripherals.rst --- docs/en/api-reference/protocols/mqtt.rst | 2 +- docs/zh_CN/api-reference/protocols/mqtt.rst | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/en/api-reference/protocols/mqtt.rst b/docs/en/api-reference/protocols/mqtt.rst index 12139d4..0773d7d 100644 --- a/docs/en/api-reference/protocols/mqtt.rst +++ b/docs/en/api-reference/protocols/mqtt.rst @@ -151,7 +151,7 @@ It is possible to set authentication parameters through the :cpp:class:`authenti Session ^^^^^^^^^^^ -For MQTT session related configurations, :cpp:class:`session ` fields should be used. +For MQTT session-related configurations, :cpp:class:`session ` fields should be used. ======================= Last Will and Testament diff --git a/docs/zh_CN/api-reference/protocols/mqtt.rst b/docs/zh_CN/api-reference/protocols/mqtt.rst index fa0a47b..9365dfe 100644 --- a/docs/zh_CN/api-reference/protocols/mqtt.rst +++ b/docs/zh_CN/api-reference/protocols/mqtt.rst @@ -145,7 +145,7 @@ ESP-MQTT 库将始终重新传输未确认的 QoS 1 和 2 发布消息,以避 * :cpp:member:`password `:使用密码 * * :cpp:member:`certificate ` 和 :cpp:member:`key `:进行双向 TLS 身份验证,PEM 或 DER 格式均可 - * :cpp:member:`use_secure_element `:使用 ESP32-WROOM-32SE 中的安全元素 + * :cpp:member:`use_secure_element `:使用 ESP32 中的安全元素 (ATECC608A) * :cpp:member:`ds_data `:使用某些乐鑫设备的数字签名外设 会话 From 1614af31a17508de9eef61e14cc85a0492ec1343 Mon Sep 17 00:00:00 2001 From: Chen Yudong Date: Tue, 30 Jan 2024 18:42:22 +0800 Subject: [PATCH 188/231] ci: update protocol/ethernet pytest markers --- components/mqtt/test_apps/.build-test-rules.yml | 16 ++++++++++++++-- components/mqtt/test_apps/test_mqtt/README.md | 4 ++-- .../mqtt/test_apps/test_mqtt/pytest_mqtt_ut.py | 1 - components/mqtt/test_apps/test_mqtt5/README.md | 4 ++-- .../mqtt/test_apps/test_mqtt5/pytest_mqtt5_ut.py | 1 - 5 files changed, 18 insertions(+), 8 deletions(-) diff --git a/components/mqtt/test_apps/.build-test-rules.yml b/components/mqtt/test_apps/.build-test-rules.yml index 5a5f690..2e58826 100644 --- a/components/mqtt/test_apps/.build-test-rules.yml +++ b/components/mqtt/test_apps/.build-test-rules.yml @@ -1,7 +1,19 @@ # Documentation: .gitlab/ci/README.md#manifest-file-to-control-the-buildtest-apps components/mqtt/test_apps: + disable: + - if: IDF_TARGET not in ["esp32", "esp32c3"] + reason: component test apps, needn't build all targets (chosen two, one for each architecture) disable_test: - - if: IDF_TARGET not in ["esp32", "esp32c2"] + - if: IDF_TARGET != "esp32" temporary: false - reason: Not needed to test on all targets (chosen two, one for each architecture) + reason: Only esp32 target has ethernet runners + depends_components: + - mqtt + - tcp_transport + - app_update + - esp_eth + - esp_netif + - esp_event + depends_filepatterns: + - components/mqtt/test_apps/common/**/* diff --git a/components/mqtt/test_apps/test_mqtt/README.md b/components/mqtt/test_apps/test_mqtt/README.md index bf47d80..1fb88ef 100644 --- a/components/mqtt/test_apps/test_mqtt/README.md +++ b/components/mqtt/test_apps/test_mqtt/README.md @@ -1,2 +1,2 @@ -| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C6 | ESP32-H2 | ESP32-P4 | ESP32-S2 | ESP32-S3 | -| ----------------- | ----- | -------- | -------- | -------- | -------- | -------- | -------- | -------- | +| Supported Targets | ESP32 | ESP32-C3 | +| ----------------- | ----- | -------- | diff --git a/components/mqtt/test_apps/test_mqtt/pytest_mqtt_ut.py b/components/mqtt/test_apps/test_mqtt/pytest_mqtt_ut.py index f3333fa..300d53e 100644 --- a/components/mqtt/test_apps/test_mqtt/pytest_mqtt_ut.py +++ b/components/mqtt/test_apps/test_mqtt/pytest_mqtt_ut.py @@ -5,7 +5,6 @@ from pytest_embedded import Dut @pytest.mark.esp32 -@pytest.mark.esp32c2 @pytest.mark.ethernet def test_mqtt_client(dut: Dut) -> None: dut.expect_unity_test_output() diff --git a/components/mqtt/test_apps/test_mqtt5/README.md b/components/mqtt/test_apps/test_mqtt5/README.md index bf47d80..1fb88ef 100644 --- a/components/mqtt/test_apps/test_mqtt5/README.md +++ b/components/mqtt/test_apps/test_mqtt5/README.md @@ -1,2 +1,2 @@ -| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C6 | ESP32-H2 | ESP32-P4 | ESP32-S2 | ESP32-S3 | -| ----------------- | ----- | -------- | -------- | -------- | -------- | -------- | -------- | -------- | +| Supported Targets | ESP32 | ESP32-C3 | +| ----------------- | ----- | -------- | diff --git a/components/mqtt/test_apps/test_mqtt5/pytest_mqtt5_ut.py b/components/mqtt/test_apps/test_mqtt5/pytest_mqtt5_ut.py index 35766cb..df15c39 100644 --- a/components/mqtt/test_apps/test_mqtt5/pytest_mqtt5_ut.py +++ b/components/mqtt/test_apps/test_mqtt5/pytest_mqtt5_ut.py @@ -5,7 +5,6 @@ from pytest_embedded import Dut @pytest.mark.esp32 -@pytest.mark.esp32c2 @pytest.mark.ethernet def test_mqtt5_client(dut: Dut) -> None: dut.expect_unity_test_output() From ff8d77fc68280b1d9f6d58e5930f53083494da0f Mon Sep 17 00:00:00 2001 From: Mahavir Jain Date: Wed, 27 Mar 2024 10:56:13 +0530 Subject: [PATCH 189/231] feat(examples): add support for ESP32-P4 in ssl_ds example --- examples/protocols/mqtt/ssl_ds/README.md | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/examples/protocols/mqtt/ssl_ds/README.md b/examples/protocols/mqtt/ssl_ds/README.md index cd0486b..fa2d4c3 100644 --- a/examples/protocols/mqtt/ssl_ds/README.md +++ b/examples/protocols/mqtt/ssl_ds/README.md @@ -1,10 +1,10 @@ -| Supported Targets | ESP32-C3 | ESP32-C6 | ESP32-H2 | ESP32-S2 | ESP32-S3 | -| ----------------- | -------- | -------- | -------- | -------- | -------- | +| Supported Targets | ESP32-C3 | ESP32-C6 | ESP32-H2 | ESP32-P4 | ESP32-S2 | ESP32-S3 | +| ----------------- | -------- | -------- | -------- | -------- | -------- | -------- | # ESP-MQTT SSL Mutual Authentication with Digital Signature (See the README.md file in the upper level 'examples' directory for more information about examples.) -Espressif's ESP32-S2, ESP32-S3, ESP32-C3, ESP32-C6 and ESP32-H2 MCU have a built-in Digital Signature (DS) Peripheral, which provides hardware acceleration for RSA signature. More details can be found at [Digital Signature with ESP-TLS](https://docs.espressif.com/projects/esp-idf/en/latest/esp32s2/api-reference/protocols/esp_tls.html#digital-signature-with-esp-tls). +Espressif's ESP32-S2, ESP32-S3, ESP32-C3, ESP32-C6, ESP32-H2 and ESP32-P4 MCU have a built-in Digital Signature (DS) Peripheral, which provides hardware acceleration for RSA signature. More details can be found at [Digital Signature with ESP-TLS](https://docs.espressif.com/projects/esp-idf/en/latest/esp32s2/api-reference/protocols/esp_tls.html#digital-signature-with-esp-tls). This example connects to the broker test.mosquitto.org using ssl transport with client certificate(RSA) and as a demonstration subscribes/unsubscribes and sends a message on certain topic.The RSA signature operation required in the ssl connection is performed with help of the Digital Signature (DS) peripheral. (Please note that the public broker is maintained by the community so may not be always available, for details please visit http://test.mosquitto.org) @@ -14,12 +14,13 @@ It uses ESP-MQTT library which implements mqtt client to connect to mqtt broker. ### Hardware Required -This example can be executed on any ESP32-S2, ESP32-S3, ESP32-C3 or ESP32-C6 board (which has a built-in DS peripheral), the only required interface is WiFi and connection to internet. +This example can be executed on any of the supported ESP32 family board (which has a built-in DS peripheral), the only required interface is WiFi/Ethernet and connection to internet. ### Configure the project #### 1) Selecting the target -As the project is to be built for the target ESP32-S2, ESP32-S3, ESP32-C3, ESP32-C6 or ESP32-H2 it should be selected with the following command + +Please select the supported target with the following command: ``` idf.py set-target /* target */ ``` From 723ac184952bdbdfe8e2764038b6e9ae0ac947f8 Mon Sep 17 00:00:00 2001 From: Marius Vikhammer Date: Thu, 28 Mar 2024 11:50:58 +0800 Subject: [PATCH 190/231] docs(misc): fixed typos found with codespell --- docs/en/api-reference/protocols/mqtt.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/en/api-reference/protocols/mqtt.rst b/docs/en/api-reference/protocols/mqtt.rst index 0773d7d..7c5899d 100644 --- a/docs/en/api-reference/protocols/mqtt.rst +++ b/docs/en/api-reference/protocols/mqtt.rst @@ -66,7 +66,7 @@ Broker address can be set by usage of :cpp:class:`address ` field is used in the format ``scheme://hostname:port/path``. -- Curently support ``mqtt``, ``mqtts``, ``ws``, ``wss`` schemes +- Currently support ``mqtt``, ``mqtts``, ``ws``, ``wss`` schemes - MQTT over TCP samples: - ``mqtt://mqtt.eclipseprojects.io``: MQTT over TCP, default port 1883 From 151005718cc6c2cbf5d90d9b1b1877bf67a194b5 Mon Sep 17 00:00:00 2001 From: laokaiyao Date: Mon, 1 Apr 2024 12:25:38 +0800 Subject: [PATCH 191/231] ci(esp32c5mp): disable the unsupported tests --- tools/test_apps/protocols/mqtt/build_test/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/test_apps/protocols/mqtt/build_test/README.md b/tools/test_apps/protocols/mqtt/build_test/README.md index 5b49857..23dc2cf 100644 --- a/tools/test_apps/protocols/mqtt/build_test/README.md +++ b/tools/test_apps/protocols/mqtt/build_test/README.md @@ -1,5 +1,5 @@ -| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C6 | ESP32-H2 | ESP32-P4 | ESP32-S2 | ESP32-S3 | -| ----------------- | ----- | -------- | -------- | -------- | -------- | -------- | -------- | -------- | +| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C5 | ESP32-C6 | ESP32-H2 | ESP32-P4 | ESP32-S2 | ESP32-S3 | +| ----------------- | ----- | -------- | -------- | -------- | -------- | -------- | -------- | -------- | -------- | # Build only test for C++ From 32e2d251e55e27c37abae2edf90e02dc0450db02 Mon Sep 17 00:00:00 2001 From: Bogdan Kolendovskyy Date: Thu, 21 Mar 2024 11:33:57 +0100 Subject: [PATCH 192/231] feat(mqtt): Enable build test in mqtt examples for esp32p4 --- examples/protocols/mqtt/custom_outbox/README.md | 4 ++-- examples/protocols/mqtt/ssl/README.md | 4 ++-- examples/protocols/mqtt/ssl_mutual_auth/README.md | 4 ++-- examples/protocols/mqtt/ssl_psk/README.md | 4 ++-- examples/protocols/mqtt/tcp/README.md | 4 ++-- examples/protocols/mqtt/ws/README.md | 4 ++-- examples/protocols/mqtt/wss/README.md | 4 ++-- examples/protocols/mqtt5/README.md | 4 ++-- 8 files changed, 16 insertions(+), 16 deletions(-) diff --git a/examples/protocols/mqtt/custom_outbox/README.md b/examples/protocols/mqtt/custom_outbox/README.md index 29a3142..5698dae 100644 --- a/examples/protocols/mqtt/custom_outbox/README.md +++ b/examples/protocols/mqtt/custom_outbox/README.md @@ -1,5 +1,5 @@ -| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C6 | ESP32-H2 | ESP32-S2 | ESP32-S3 | -| ----------------- | ----- | -------- | -------- | -------- | -------- | -------- | -------- | +| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C6 | ESP32-H2 | ESP32-P4 | ESP32-S2 | ESP32-S3 | +| ----------------- | ----- | -------- | -------- | -------- | -------- | -------- | -------- | -------- | # ESP-MQTT custom outbox sample application (See the README.md file in the upper level 'examples' directory for more information about examples.) diff --git a/examples/protocols/mqtt/ssl/README.md b/examples/protocols/mqtt/ssl/README.md index ff97ae7..3da8c7e 100644 --- a/examples/protocols/mqtt/ssl/README.md +++ b/examples/protocols/mqtt/ssl/README.md @@ -1,5 +1,5 @@ -| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C6 | ESP32-H2 | ESP32-S2 | ESP32-S3 | -| ----------------- | ----- | -------- | -------- | -------- | -------- | -------- | -------- | +| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C6 | ESP32-H2 | ESP32-P4 | ESP32-S2 | ESP32-S3 | +| ----------------- | ----- | -------- | -------- | -------- | -------- | -------- | -------- | -------- | # ESP-MQTT SSL Sample application diff --git a/examples/protocols/mqtt/ssl_mutual_auth/README.md b/examples/protocols/mqtt/ssl_mutual_auth/README.md index 71de8e3..695da3e 100644 --- a/examples/protocols/mqtt/ssl_mutual_auth/README.md +++ b/examples/protocols/mqtt/ssl_mutual_auth/README.md @@ -1,5 +1,5 @@ -| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C6 | ESP32-H2 | ESP32-S2 | ESP32-S3 | -| ----------------- | ----- | -------- | -------- | -------- | -------- | -------- | -------- | +| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C6 | ESP32-H2 | ESP32-P4 | ESP32-S2 | ESP32-S3 | +| ----------------- | ----- | -------- | -------- | -------- | -------- | -------- | -------- | -------- | # ESP-MQTT SSL Sample application (mutual authentication) diff --git a/examples/protocols/mqtt/ssl_psk/README.md b/examples/protocols/mqtt/ssl_psk/README.md index ce0fa6d..f65175b 100644 --- a/examples/protocols/mqtt/ssl_psk/README.md +++ b/examples/protocols/mqtt/ssl_psk/README.md @@ -1,5 +1,5 @@ -| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C6 | ESP32-H2 | ESP32-S2 | ESP32-S3 | -| ----------------- | ----- | -------- | -------- | -------- | -------- | -------- | -------- | +| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C6 | ESP32-H2 | ESP32-P4 | ESP32-S2 | ESP32-S3 | +| ----------------- | ----- | -------- | -------- | -------- | -------- | -------- | -------- | -------- | # ESP-MQTT SSL example with PSK verification diff --git a/examples/protocols/mqtt/tcp/README.md b/examples/protocols/mqtt/tcp/README.md index 52888e3..1be468a 100644 --- a/examples/protocols/mqtt/tcp/README.md +++ b/examples/protocols/mqtt/tcp/README.md @@ -1,5 +1,5 @@ -| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C6 | ESP32-H2 | ESP32-S2 | ESP32-S3 | -| ----------------- | ----- | -------- | -------- | -------- | -------- | -------- | -------- | +| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C6 | ESP32-H2 | ESP32-P4 | ESP32-S2 | ESP32-S3 | +| ----------------- | ----- | -------- | -------- | -------- | -------- | -------- | -------- | -------- | # ESP-MQTT sample application (See the README.md file in the upper level 'examples' directory for more information about examples.) diff --git a/examples/protocols/mqtt/ws/README.md b/examples/protocols/mqtt/ws/README.md index 69b66ac..bf84e7a 100644 --- a/examples/protocols/mqtt/ws/README.md +++ b/examples/protocols/mqtt/ws/README.md @@ -1,5 +1,5 @@ -| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C6 | ESP32-H2 | ESP32-S2 | ESP32-S3 | -| ----------------- | ----- | -------- | -------- | -------- | -------- | -------- | -------- | +| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C6 | ESP32-H2 | ESP32-P4 | ESP32-S2 | ESP32-S3 | +| ----------------- | ----- | -------- | -------- | -------- | -------- | -------- | -------- | -------- | # ESP-MQTT MQTT over Websocket diff --git a/examples/protocols/mqtt/wss/README.md b/examples/protocols/mqtt/wss/README.md index 7b1222f..2edbaf5 100644 --- a/examples/protocols/mqtt/wss/README.md +++ b/examples/protocols/mqtt/wss/README.md @@ -1,5 +1,5 @@ -| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C6 | ESP32-H2 | ESP32-S2 | ESP32-S3 | -| ----------------- | ----- | -------- | -------- | -------- | -------- | -------- | -------- | +| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C6 | ESP32-H2 | ESP32-P4 | ESP32-S2 | ESP32-S3 | +| ----------------- | ----- | -------- | -------- | -------- | -------- | -------- | -------- | -------- | # ESP-MQTT MQTT over WSS Sample application (See the README.md file in the upper level 'examples' directory for more information about examples.) diff --git a/examples/protocols/mqtt5/README.md b/examples/protocols/mqtt5/README.md index efc2b7b..9cf3cef 100644 --- a/examples/protocols/mqtt5/README.md +++ b/examples/protocols/mqtt5/README.md @@ -1,5 +1,5 @@ -| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C6 | ESP32-H2 | ESP32-S2 | ESP32-S3 | -| ----------------- | ----- | -------- | -------- | -------- | -------- | -------- | -------- | +| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C6 | ESP32-H2 | ESP32-P4 | ESP32-S2 | ESP32-S3 | +| ----------------- | ----- | -------- | -------- | -------- | -------- | -------- | -------- | -------- | # ESP-MQTT sample application (See the README.md file in the upper level 'examples' directory for more information about examples.) From 99199a57c91967125ed1290c7005901b7f899eae Mon Sep 17 00:00:00 2001 From: Ondrej Kosta Date: Fri, 26 Apr 2024 12:27:54 +0200 Subject: [PATCH 193/231] feat(esp_eth): a new folder structure of the driver and other improvements Fixed memory leak in emac_esp_new_dma function. Polished ESP EMAC cache management. Added emac_periph definitions based on SoC features and improved(generalized) ESP EMAC GPIO initialization. Added ESP EMAC GPIO reservation. Added check for frame error condition indicated by EMAC DMA and created a target test. --- components/mqtt/test_apps/test_mqtt/main/CMakeLists.txt | 2 +- components/mqtt/test_apps/test_mqtt5/main/CMakeLists.txt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/components/mqtt/test_apps/test_mqtt/main/CMakeLists.txt b/components/mqtt/test_apps/test_mqtt/main/CMakeLists.txt index 7a06624..0647fe2 100644 --- a/components/mqtt/test_apps/test_mqtt/main/CMakeLists.txt +++ b/components/mqtt/test_apps/test_mqtt/main/CMakeLists.txt @@ -5,4 +5,4 @@ if(CONFIG_MQTT_PROTOCOL_5) endif() idf_component_register(SRCS "${srcs}" - PRIV_REQUIRES cmock test_utils mqtt nvs_flash app_update esp_eth esp_netif spi_flash common) + PRIV_REQUIRES cmock test_utils mqtt nvs_flash app_update spi_flash common) diff --git a/components/mqtt/test_apps/test_mqtt5/main/CMakeLists.txt b/components/mqtt/test_apps/test_mqtt5/main/CMakeLists.txt index f152df9..d33a86c 100644 --- a/components/mqtt/test_apps/test_mqtt5/main/CMakeLists.txt +++ b/components/mqtt/test_apps/test_mqtt5/main/CMakeLists.txt @@ -1,4 +1,4 @@ set(srcs test_mqtt5_client_broker.c test_mqtt5.c) idf_component_register(SRCS "${srcs}" - PRIV_REQUIRES cmock test_utils mqtt nvs_flash app_update esp_eth esp_netif spi_flash common) + PRIV_REQUIRES cmock test_utils mqtt nvs_flash app_update spi_flash common) From ab03f3f21a65213d0d9d518dc948d2daade64c03 Mon Sep 17 00:00:00 2001 From: Jakob Hasse Date: Thu, 16 May 2024 17:56:16 +0200 Subject: [PATCH 194/231] refactor(mqtt): removed unused includes from tcp example --- examples/protocols/mqtt/tcp/main/app_main.c | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/examples/protocols/mqtt/tcp/main/app_main.c b/examples/protocols/mqtt/tcp/main/app_main.c index 6fe652b..9a13adb 100644 --- a/examples/protocols/mqtt/tcp/main/app_main.c +++ b/examples/protocols/mqtt/tcp/main/app_main.c @@ -9,24 +9,15 @@ #include #include -#include #include -#include "esp_wifi.h" +#include +#include #include "esp_system.h" #include "nvs_flash.h" #include "esp_event.h" #include "esp_netif.h" #include "protocol_examples_common.h" -#include "freertos/FreeRTOS.h" -#include "freertos/task.h" -#include "freertos/semphr.h" -#include "freertos/queue.h" - -#include "lwip/sockets.h" -#include "lwip/dns.h" -#include "lwip/netdb.h" - #include "esp_log.h" #include "mqtt_client.h" From 08af0be8ce56c7d5abe090e07e66553379bc32bb Mon Sep 17 00:00:00 2001 From: David Cermak Date: Tue, 26 Sep 2023 07:20:51 +0200 Subject: [PATCH 195/231] fix(examples): Add wifi_remote option to common connect example * Add MQTT test configuration with WiFi on ESP32-P4 * Document esp_wifi_remote workflow in the example's README --- examples/protocols/mqtt/tcp/README.md | 171 ++++++++++++++++++ .../protocols/mqtt/tcp/main/idf_component.yml | 4 + .../protocols/mqtt/tcp/sdkconfig.ci.p4_wifi | 4 + 3 files changed, 179 insertions(+) create mode 100644 examples/protocols/mqtt/tcp/sdkconfig.ci.p4_wifi diff --git a/examples/protocols/mqtt/tcp/README.md b/examples/protocols/mqtt/tcp/README.md index 1be468a..8f5c4c5 100644 --- a/examples/protocols/mqtt/tcp/README.md +++ b/examples/protocols/mqtt/tcp/README.md @@ -59,3 +59,174 @@ I (5194) MQTT_EXAMPLE: MQTT_EVENT_DATA TOPIC=/topic/qos0 DATA=data ``` + +## Using Wi-Fi connection with ESP32P4 + +It is possible to use Wi-Fi connection on targets that do not support native Wi-Fi peripheral. This example demonstrates using `esp_wifi_remote` on ESP32P4 in the test configuration defined as `sdkconfig.ci.p4_wifi`. This configuration requires another ESP target with native Wi-Fi support physically connected to the ESP32-P4. + +### Configure master-slave verification + +In order to secure the physical connection between the ESP32-P4 (master) and the slave device, it is necessary to set certificates and keys for each side. +To bootstrap this step, you can use one-time generated self-signed RSA keys and certificates running: +``` +./managed_components/espressif__esp_wifi_remote/examples/test_certs/generate_test_certs.sh espressif.local +``` + +### Configure the slave project + +It is recommended to create a new project from `esp_wifi_remote` component's example with +``` +idf.py create-project-from-example "espressif/esp_wifi_remote:server" +``` +but you can also build and flash the slave project directly from the `managed_components` directory using: +``` +idf.py -C managed_components/espressif__esp_wifi_remote/examples/server/ -B build_slave +``` + +Please follow these steps to setup the slave application: +* `idf.py set-target` -- choose the slave target (must support Wi-Fi) +* `idf.py menuconfig` -- configure the physical connection and verification details: + - `CONFIG_ESP_WIFI_REMOTE_EPPP_CLIENT_CA` -- CA for verifying ESP32-P4 application + - `CONFIG_ESP_WIFI_REMOTE_EPPP_SERVER_CRT` -- slave's certificate + - `CONFIG_ESP_WIFI_REMOTE_EPPP_SERVER_KEY` -- slave's private key +* `idf.py build flash monitor` + +### Configure the master project (ESP32-P4) + +similarly to the slave project, we have to configure +* the physical connection +* the verification + - `CONFIG_ESP_WIFI_REMOTE_EPPP_SERVER_CA` -- CA for verifying the slave application + - `CONFIG_ESP_WIFI_REMOTE_EPPP_CLIENT_CRT` -- our own certificate + - `CONFIG_ESP_WIFI_REMOTE_EPPP_CLIENT_KEY` -- our own private key + +After project configuration, you build and flash the board with +``` +idf.py build flash monitor +``` + +### Example Output of the slave device + +``` +I (7982) main_task: Returned from app_main() +I (8242) rpc_server: Received header id 2 +I (8242) pp: pp rom version: 5b8dcfa +I (8242) net80211: net80211 rom version: 5b8dcfa +I (8252) wifi:wifi driver task: 4082be8c, prio:23, stack:6656, core=0 +I (8252) wifi:wifi firmware version: feaf82d +I (8252) wifi:wifi certification version: v7.0 +I (8252) wifi:config NVS flash: enabled +I (8262) wifi:config nano formatting: disabled +I (8262) wifi:mac_version:HAL_MAC_ESP32AX_761,ut_version:N, band:0x1 +I (8272) wifi:Init data frame dynamic rx buffer num: 32 +I (8272) wifi:Init static rx mgmt buffer num: 5 +I (8282) wifi:Init management short buffer num: 32 +I (8282) wifi:Init dynamic tx buffer num: 32 +I (8292) wifi:Init static tx FG buffer num: 2 +I (8292) wifi:Init static rx buffer size: 1700 (rxctrl:92, csi:512) +I (8302) wifi:Init static rx buffer num: 10 +I (8302) wifi:Init dynamic rx buffer num: 32 +I (8302) wifi_init: rx ba win: 6 +I (8312) wifi_init: accept mbox: 6 +I (8312) wifi_init: tcpip mbox: 32 +I (8322) wifi_init: udp mbox: 6 +I (8322) wifi_init: tcp mbox: 6 +I (8322) wifi_init: tcp tx win: 5760 +I (8332) wifi_init: tcp rx win: 5760 +I (8332) wifi_init: tcp mss: 1440 +I (8342) wifi_init: WiFi IRAM OP enabled +I (8342) wifi_init: WiFi RX IRAM OP enabled +I (8352) wifi_init: WiFi SLP IRAM OP enabled +I (8362) rpc_server: Received header id 11 +I (8362) rpc_server: Received header id 4 +I (8372) rpc_server: Received header id 6 +I (8372) phy_init: phy_version 270,339aa07,Apr 3 2024,16:36:11 +I (8492) wifi:enable tsf +I (8492) rpc_server: Received WIFI event 41 +I (8502) rpc_server: Received WIFI event 2 +I (8732) rpc_server: Received header id 10 +I (8742) rpc_server: Received header id 5 +I (8752) rpc_server: Received header id 8 +I (11452) wifi:new:<6,0>, old:<1,0>, ap:<255,255>, sta:<6,0>, prof:1, snd_ch_cfg:0x0 +I (11452) wifi:(connect)dot11_authmode:0x3, pairwise_cipher:0x3, group_cipher:0x1 +I (11452) wifi:state: init -> auth (0xb0) +I (11462) rpc_server: Received WIFI event 41 +I (11462) wifi:state: auth -> assoc (0x0) +I (11472) wifi:(assoc)RESP, Extended Capabilities length:8, operating_mode_notification:0 +I (11472) wifi:(assoc)RESP, Extended Capabilities, MBSSID:0, TWT Responder:0, OBSS Narrow Bandwidth RU In OFDMA Tolerance:0 +I (11482) wifi:Extended Capabilities length:8, operating_mode_notification:1 +I (11492) wifi:state: assoc -> run (0x10) +I (11492) wifi:(trc)phytype:CBW20-SGI, snr:50, maxRate:144, highestRateIdx:0 +W (11502) wifi:(trc)band:2G, phymode:3, highestRateIdx:0, lowestRateIdx:11, dataSchedTableSize:14 +I (11512) wifi:(trc)band:2G, rate(S-MCS7, rateIdx:0), ampdu(rate:S-MCS7, schedIdx(0, stop:8)), snr:50, ampduState:wait operational +I (11522) wifi:ifidx:0, rssi:-45, nf:-95, phytype(0x3, CBW20-SGI), phymode(0x3, 11bgn), max_rate:144, he:0, vht:0, ht:1 +I (11532) wifi:(ht)max.RxAMPDULenExponent:3(65535 bytes), MMSS:6(8 us) +W (11542) wifi:idx:0, ifx:0, tid:0, TAHI:0x1002cb4, TALO:0x1b942980, (ssn:0, win:64, cur_ssn:0), CONF:0xc0000005 +I (11572) wifi:connected with Cermakowifi, aid = 2, channel 6, BW20, bssid = 80:29:94:1b:b4:2c +I (11572) wifi:cipher(pairwise:0x3, group:0x1), pmf:0, security:WPA2-PSK, phy:11bgn, rssi:-45 +I (11582) wifi:pm start, type: 1, twt_start:0 + +I (11582) wifi:pm start, type:1, aid:0x2, trans-BSSID:80:29:94:1b:b4:2c, BSSID[5]:0x2c, mbssid(max-indicator:0, index:0), he:0 +I (11592) wifi:dp: 1, bi: 102400, li: 3, scale listen interval from 307200 us to 307200 us +I (11602) wifi:set rx beacon pti, rx_bcn_pti: 10, bcn_timeout: 25000, mt_pti: 10, mt_time: 10000 +I (11612) wifi:[ADDBA]TX addba request, tid:0, dialogtoken:1, bufsize:64, A-MSDU:0(not supported), policy:1(IMR), ssn:0(0x0) +I (11622) wifi:[ADDBA]TX addba request, tid:7, dialogtoken:2, bufsize:64, A-MSDU:0(not supported), policy:1(IMR), ssn:0(0x20) +I (11632) wifi:[ADDBA]TX addba request, tid:5, dialogtoken:3, bufsize:64, A-MSDU:0(not supported), policy:1(IMR), ssn:0(0x0) +I (11642) wifi:[ADDBA]RX addba response, status:0, tid:7/tb:0(0x1), bufsize:64, batimeout:0, txa_wnd:64 +I (11652) wifi:[ADDBA]RX addba response, status:0, tid:5/tb:0(0x1), bufsize:64, batimeout:0, txa_wnd:64 +I (11662) wifi:[ADDBA]RX addba response, status:0, tid:0/tb:1(0x1), bufsize:64, batimeout:0, txa_wnd:64 +I (11672) wifi:AP's beacon interval = 102400 us, DTIM period = 1 +I (11682) rpc_server: Received WIFI event 4 +I (15682) esp_netif_handlers: sta ip: 192.168.0.33, mask: 255.255.255.0, gw: 192.168.0.1 +I (15682) rpc_server: Received IP event 0 +I (15682) rpc_server: Main DNS:185.162.24.55 +I (15682) rpc_server: IP address:192.168.0.33 +``` + +### Example Output of the master device (ESP32-P4) + +``` +I (445) example_connect: Start example_connect. +I (455) uart: queue free spaces: 16 +I (455) eppp_link: Waiting for IP address 0 +I (3195) esp-netif_lwip-ppp: Connected +I (3195) eppp_link: Got IPv4 event: Interface "pppos_client(EPPP0)" address: 192.168.11.2 +I (3195) esp-netif_lwip-ppp: Connected +I (3195) eppp_link: Connected! 0 +I (5475) example_connect: Waiting for IP(s) +I (8405) esp_wifi_remote: esp_wifi_internal_reg_rxcb: sta: 0x4001c68a +I (9445) example_connect: Got IPv6 event: Interface "pppos_client" address: fe80:0000:0000:0000:5632:04ff:fe08:5054, type: ESP_IP6_ADDR_IS_LINK_LOCAL +I (12415) rpc_client: Main DNS:185.162.24.55 +I (12415) esp_netif_handlers: pppos_client ip: 192.168.11.2, mask: 255.255.255.255, gw: 192.168.11.1 +I (12415) rpc_client: EPPP IP:192.168.11.1 +I (12415) example_connect: Got IPv4 event: Interface "pppos_client" address: 192.168.11.2 +I (12425) rpc_client: WIFI IP:192.168.0.33 +I (12435) example_common: Connected to pppos_client +I (12445) rpc_client: WIFI GW:192.168.0.1 +I (12455) example_common: - IPv6 address: fe80:0000:0000:0000:5632:04ff:fe08:5054, type: ESP_IP6_ADDR_IS_LINK_LOCAL +I (12455) rpc_client: WIFI mask:255.255.255.0 +I (12465) example_common: Connected to pppos_client +I (12475) example_common: - IPv4 address: 192.168.11.2, +I (12475) example_common: - IPv6 address: fe80:0000:0000:0000:5c3b:1291:05ca:6dc8, type: ESP_IP6_ADDR_IS_LINK_LOCAL +I (12495) mqtt_example: Other event id:7 +I (12495) main_task: Returned from app_main() +I (12905) mqtt_example: MQTT_EVENT_CONNECTED +I (12905) mqtt_example: sent publish successful, msg_id=36013 +I (12905) mqtt_example: sent subscribe successful, msg_id=44233 +I (12905) mqtt_example: sent subscribe successful, msg_id=36633 +I (12915) mqtt_example: sent unsubscribe successful, msg_id=15480 +I (13115) mqtt_example: MQTT_EVENT_PUBLISHED, msg_id=36013 +I (13415) mqtt_example: MQTT_EVENT_SUBSCRIBED, msg_id=44233 +I (13415) mqtt_example: sent publish successful, msg_id=0 +I (13415) mqtt_example: MQTT_EVENT_SUBSCRIBED, msg_id=36633 +I (13415) mqtt_example: sent publish successful, msg_id=0 +I (13425) mqtt_example: MQTT_EVENT_DATA +TOPIC=/topic/qos1 +DATA=data_3 +I (13435) mqtt_example: MQTT_EVENT_UNSUBSCRIBED, msg_id=15480 +I (13615) mqtt_example: MQTT_EVENT_DATA +TOPIC=/topic/qos0 +DATA=data +I (13925) mqtt_example: MQTT_EVENT_DATA +TOPIC=/topic/qos0 +``` diff --git a/examples/protocols/mqtt/tcp/main/idf_component.yml b/examples/protocols/mqtt/tcp/main/idf_component.yml index 7181948..1b1f6db 100644 --- a/examples/protocols/mqtt/tcp/main/idf_component.yml +++ b/examples/protocols/mqtt/tcp/main/idf_component.yml @@ -1,3 +1,7 @@ dependencies: protocol_examples_common: path: ${IDF_PATH}/examples/common_components/protocol_examples_common + espressif/esp_wifi_remote: + version: ">=0.1.12" + rules: + - if: "target in [esp32p4, esp32h2]" diff --git a/examples/protocols/mqtt/tcp/sdkconfig.ci.p4_wifi b/examples/protocols/mqtt/tcp/sdkconfig.ci.p4_wifi new file mode 100644 index 0000000..7a5574c --- /dev/null +++ b/examples/protocols/mqtt/tcp/sdkconfig.ci.p4_wifi @@ -0,0 +1,4 @@ +CONFIG_IDF_TARGET="esp32p4" +CONFIG_EXAMPLE_CONNECT_WIFI=y +CONFIG_ESP_WIFI_REMOTE_EPPP_UART_TX_PIN=17 +CONFIG_ESP_WIFI_REMOTE_EPPP_UART_RX_PIN=16 From 54f86b9687e7ccb0a0d409d6f1a4855d24c78143 Mon Sep 17 00:00:00 2001 From: laokaiyao Date: Tue, 11 Jun 2024 15:31:33 +0800 Subject: [PATCH 196/231] remove(c5beta3): remove c5 beta3 doxy files --- examples/protocols/mqtt/ssl_ds/README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/examples/protocols/mqtt/ssl_ds/README.md b/examples/protocols/mqtt/ssl_ds/README.md index fa2d4c3..f89d172 100644 --- a/examples/protocols/mqtt/ssl_ds/README.md +++ b/examples/protocols/mqtt/ssl_ds/README.md @@ -1,5 +1,5 @@ -| Supported Targets | ESP32-C3 | ESP32-C6 | ESP32-H2 | ESP32-P4 | ESP32-S2 | ESP32-S3 | -| ----------------- | -------- | -------- | -------- | -------- | -------- | -------- | +| Supported Targets | ESP32-C3 | ESP32-C5 | ESP32-C6 | ESP32-H2 | ESP32-P4 | ESP32-S2 | ESP32-S3 | +| ----------------- | -------- | -------- | -------- | -------- | -------- | -------- | -------- | # ESP-MQTT SSL Mutual Authentication with Digital Signature (See the README.md file in the upper level 'examples' directory for more information about examples.) @@ -24,7 +24,7 @@ Please select the supported target with the following command: ``` idf.py set-target /* target */ ``` -More detials can be found at [Selecting the target](https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-guides/build-system.html#selecting-the-target). +More details can be found at [Selecting the target](https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-guides/build-system.html#selecting-the-target). #### 2) Generate your client key and certificate @@ -62,7 +62,7 @@ RSA private key is nothing but the client private key ( RSA ) generated in Step > Note: More details about the `esp-secure-cert-tool` utility can be found [here](https://github.com/espressif/esp_secure_cert_mgr/tree/main/tools). -#### 4) Connection cofiguration +#### 4) Connection configuration * Open the project configuration menu (`idf.py menuconfig`) * Configure Wi-Fi or Ethernet under "Example Connection Configuration" menu. See "Establishing Wi-Fi or Ethernet Connection" section in [examples/protocols/README.md](../../README.md) for more details. From 8b1fb7ff2fed3bce58f03f8eb229f0ff4d9acd1c Mon Sep 17 00:00:00 2001 From: wanlei Date: Mon, 29 Apr 2024 19:55:27 +0800 Subject: [PATCH 197/231] feat(esp32c61): disable unsupported build test --- tools/test_apps/protocols/mqtt/build_test/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/test_apps/protocols/mqtt/build_test/README.md b/tools/test_apps/protocols/mqtt/build_test/README.md index 23dc2cf..4f186a5 100644 --- a/tools/test_apps/protocols/mqtt/build_test/README.md +++ b/tools/test_apps/protocols/mqtt/build_test/README.md @@ -1,5 +1,5 @@ -| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C5 | ESP32-C6 | ESP32-H2 | ESP32-P4 | ESP32-S2 | ESP32-S3 | -| ----------------- | ----- | -------- | -------- | -------- | -------- | -------- | -------- | -------- | -------- | +| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C5 | ESP32-C6 | ESP32-C61 | ESP32-H2 | ESP32-P4 | ESP32-S2 | ESP32-S3 | +| ----------------- | ----- | -------- | -------- | -------- | -------- | --------- | -------- | -------- | -------- | -------- | # Build only test for C++ From 67b882b53c5d8ded61dbf2b1137602d474ebc334 Mon Sep 17 00:00:00 2001 From: xuxiao Date: Thu, 18 Jul 2024 16:02:13 +0800 Subject: [PATCH 198/231] fix(wifi): fix code comments --- examples/protocols/mqtt/custom_outbox/sdkconfig.defaults | 1 + 1 file changed, 1 insertion(+) diff --git a/examples/protocols/mqtt/custom_outbox/sdkconfig.defaults b/examples/protocols/mqtt/custom_outbox/sdkconfig.defaults index 385da31..11f90ce 100644 --- a/examples/protocols/mqtt/custom_outbox/sdkconfig.defaults +++ b/examples/protocols/mqtt/custom_outbox/sdkconfig.defaults @@ -1,2 +1,3 @@ CONFIG_MQTT_CUSTOM_OUTBOX=y CONFIG_COMPILER_CXX_EXCEPTIONS=y +CONFIG_COMPILER_OPTIMIZATION_SIZE=y From d75c586e7eaf834dd0a4e184fb9d450e07f0e7b9 Mon Sep 17 00:00:00 2001 From: Ondrej Kosta Date: Fri, 7 Jun 2024 10:43:14 +0200 Subject: [PATCH 199/231] feat(esp_eth): removed disable of C5 and P4 from examples .build-test-rules Updated examples to be able to build for C5 and P4. Added Ethernet support for static_ip example. --- examples/protocols/mqtt/custom_outbox/README.md | 4 ++-- examples/protocols/mqtt/ssl/README.md | 4 ++-- examples/protocols/mqtt/ssl_mutual_auth/README.md | 4 ++-- examples/protocols/mqtt/ssl_psk/README.md | 4 ++-- examples/protocols/mqtt/tcp/README.md | 4 ++-- examples/protocols/mqtt/ws/README.md | 4 ++-- examples/protocols/mqtt/wss/README.md | 4 ++-- examples/protocols/mqtt5/README.md | 4 ++-- 8 files changed, 16 insertions(+), 16 deletions(-) diff --git a/examples/protocols/mqtt/custom_outbox/README.md b/examples/protocols/mqtt/custom_outbox/README.md index 5698dae..94774ec 100644 --- a/examples/protocols/mqtt/custom_outbox/README.md +++ b/examples/protocols/mqtt/custom_outbox/README.md @@ -1,5 +1,5 @@ -| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C6 | ESP32-H2 | ESP32-P4 | ESP32-S2 | ESP32-S3 | -| ----------------- | ----- | -------- | -------- | -------- | -------- | -------- | -------- | -------- | +| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C5 | ESP32-C6 | ESP32-H2 | ESP32-P4 | ESP32-S2 | ESP32-S3 | +| ----------------- | ----- | -------- | -------- | -------- | -------- | -------- | -------- | -------- | -------- | # ESP-MQTT custom outbox sample application (See the README.md file in the upper level 'examples' directory for more information about examples.) diff --git a/examples/protocols/mqtt/ssl/README.md b/examples/protocols/mqtt/ssl/README.md index 3da8c7e..26e8374 100644 --- a/examples/protocols/mqtt/ssl/README.md +++ b/examples/protocols/mqtt/ssl/README.md @@ -1,5 +1,5 @@ -| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C6 | ESP32-H2 | ESP32-P4 | ESP32-S2 | ESP32-S3 | -| ----------------- | ----- | -------- | -------- | -------- | -------- | -------- | -------- | -------- | +| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C5 | ESP32-C6 | ESP32-H2 | ESP32-P4 | ESP32-S2 | ESP32-S3 | +| ----------------- | ----- | -------- | -------- | -------- | -------- | -------- | -------- | -------- | -------- | # ESP-MQTT SSL Sample application diff --git a/examples/protocols/mqtt/ssl_mutual_auth/README.md b/examples/protocols/mqtt/ssl_mutual_auth/README.md index 695da3e..1f1b446 100644 --- a/examples/protocols/mqtt/ssl_mutual_auth/README.md +++ b/examples/protocols/mqtt/ssl_mutual_auth/README.md @@ -1,5 +1,5 @@ -| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C6 | ESP32-H2 | ESP32-P4 | ESP32-S2 | ESP32-S3 | -| ----------------- | ----- | -------- | -------- | -------- | -------- | -------- | -------- | -------- | +| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C5 | ESP32-C6 | ESP32-H2 | ESP32-P4 | ESP32-S2 | ESP32-S3 | +| ----------------- | ----- | -------- | -------- | -------- | -------- | -------- | -------- | -------- | -------- | # ESP-MQTT SSL Sample application (mutual authentication) diff --git a/examples/protocols/mqtt/ssl_psk/README.md b/examples/protocols/mqtt/ssl_psk/README.md index f65175b..7281b48 100644 --- a/examples/protocols/mqtt/ssl_psk/README.md +++ b/examples/protocols/mqtt/ssl_psk/README.md @@ -1,5 +1,5 @@ -| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C6 | ESP32-H2 | ESP32-P4 | ESP32-S2 | ESP32-S3 | -| ----------------- | ----- | -------- | -------- | -------- | -------- | -------- | -------- | -------- | +| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C5 | ESP32-C6 | ESP32-H2 | ESP32-P4 | ESP32-S2 | ESP32-S3 | +| ----------------- | ----- | -------- | -------- | -------- | -------- | -------- | -------- | -------- | -------- | # ESP-MQTT SSL example with PSK verification diff --git a/examples/protocols/mqtt/tcp/README.md b/examples/protocols/mqtt/tcp/README.md index 8f5c4c5..26a7fd7 100644 --- a/examples/protocols/mqtt/tcp/README.md +++ b/examples/protocols/mqtt/tcp/README.md @@ -1,5 +1,5 @@ -| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C6 | ESP32-H2 | ESP32-P4 | ESP32-S2 | ESP32-S3 | -| ----------------- | ----- | -------- | -------- | -------- | -------- | -------- | -------- | -------- | +| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C5 | ESP32-C6 | ESP32-H2 | ESP32-P4 | ESP32-S2 | ESP32-S3 | +| ----------------- | ----- | -------- | -------- | -------- | -------- | -------- | -------- | -------- | -------- | # ESP-MQTT sample application (See the README.md file in the upper level 'examples' directory for more information about examples.) diff --git a/examples/protocols/mqtt/ws/README.md b/examples/protocols/mqtt/ws/README.md index bf84e7a..7e52baa 100644 --- a/examples/protocols/mqtt/ws/README.md +++ b/examples/protocols/mqtt/ws/README.md @@ -1,5 +1,5 @@ -| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C6 | ESP32-H2 | ESP32-P4 | ESP32-S2 | ESP32-S3 | -| ----------------- | ----- | -------- | -------- | -------- | -------- | -------- | -------- | -------- | +| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C5 | ESP32-C6 | ESP32-H2 | ESP32-P4 | ESP32-S2 | ESP32-S3 | +| ----------------- | ----- | -------- | -------- | -------- | -------- | -------- | -------- | -------- | -------- | # ESP-MQTT MQTT over Websocket diff --git a/examples/protocols/mqtt/wss/README.md b/examples/protocols/mqtt/wss/README.md index 2edbaf5..80a50bb 100644 --- a/examples/protocols/mqtt/wss/README.md +++ b/examples/protocols/mqtt/wss/README.md @@ -1,5 +1,5 @@ -| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C6 | ESP32-H2 | ESP32-P4 | ESP32-S2 | ESP32-S3 | -| ----------------- | ----- | -------- | -------- | -------- | -------- | -------- | -------- | -------- | +| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C5 | ESP32-C6 | ESP32-H2 | ESP32-P4 | ESP32-S2 | ESP32-S3 | +| ----------------- | ----- | -------- | -------- | -------- | -------- | -------- | -------- | -------- | -------- | # ESP-MQTT MQTT over WSS Sample application (See the README.md file in the upper level 'examples' directory for more information about examples.) diff --git a/examples/protocols/mqtt5/README.md b/examples/protocols/mqtt5/README.md index 9cf3cef..a004c5e 100644 --- a/examples/protocols/mqtt5/README.md +++ b/examples/protocols/mqtt5/README.md @@ -1,5 +1,5 @@ -| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C6 | ESP32-H2 | ESP32-P4 | ESP32-S2 | ESP32-S3 | -| ----------------- | ----- | -------- | -------- | -------- | -------- | -------- | -------- | -------- | +| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C5 | ESP32-C6 | ESP32-H2 | ESP32-P4 | ESP32-S2 | ESP32-S3 | +| ----------------- | ----- | -------- | -------- | -------- | -------- | -------- | -------- | -------- | -------- | # ESP-MQTT sample application (See the README.md file in the upper level 'examples' directory for more information about examples.) From 87e021c8d9473c25d45c4a341ada2c7b0adc3a31 Mon Sep 17 00:00:00 2001 From: Ondrej Kosta Date: Tue, 17 Sep 2024 11:27:43 +0200 Subject: [PATCH 200/231] ci(esp_eth): enabled build and test for c61 in eth/proto examples --- examples/protocols/mqtt/custom_outbox/README.md | 4 ++-- examples/protocols/mqtt/ssl/README.md | 4 ++-- examples/protocols/mqtt/ssl_mutual_auth/README.md | 4 ++-- examples/protocols/mqtt/ssl_psk/README.md | 4 ++-- examples/protocols/mqtt/tcp/README.md | 4 ++-- examples/protocols/mqtt/ws/README.md | 4 ++-- examples/protocols/mqtt/wss/README.md | 4 ++-- examples/protocols/mqtt5/README.md | 4 ++-- 8 files changed, 16 insertions(+), 16 deletions(-) diff --git a/examples/protocols/mqtt/custom_outbox/README.md b/examples/protocols/mqtt/custom_outbox/README.md index 94774ec..5b64bcd 100644 --- a/examples/protocols/mqtt/custom_outbox/README.md +++ b/examples/protocols/mqtt/custom_outbox/README.md @@ -1,5 +1,5 @@ -| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C5 | ESP32-C6 | ESP32-H2 | ESP32-P4 | ESP32-S2 | ESP32-S3 | -| ----------------- | ----- | -------- | -------- | -------- | -------- | -------- | -------- | -------- | -------- | +| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C5 | ESP32-C6 | ESP32-C61 | ESP32-H2 | ESP32-P4 | ESP32-S2 | ESP32-S3 | +| ----------------- | ----- | -------- | -------- | -------- | -------- | --------- | -------- | -------- | -------- | -------- | # ESP-MQTT custom outbox sample application (See the README.md file in the upper level 'examples' directory for more information about examples.) diff --git a/examples/protocols/mqtt/ssl/README.md b/examples/protocols/mqtt/ssl/README.md index 26e8374..0407eda 100644 --- a/examples/protocols/mqtt/ssl/README.md +++ b/examples/protocols/mqtt/ssl/README.md @@ -1,5 +1,5 @@ -| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C5 | ESP32-C6 | ESP32-H2 | ESP32-P4 | ESP32-S2 | ESP32-S3 | -| ----------------- | ----- | -------- | -------- | -------- | -------- | -------- | -------- | -------- | -------- | +| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C5 | ESP32-C6 | ESP32-C61 | ESP32-H2 | ESP32-P4 | ESP32-S2 | ESP32-S3 | +| ----------------- | ----- | -------- | -------- | -------- | -------- | --------- | -------- | -------- | -------- | -------- | # ESP-MQTT SSL Sample application diff --git a/examples/protocols/mqtt/ssl_mutual_auth/README.md b/examples/protocols/mqtt/ssl_mutual_auth/README.md index 1f1b446..4750ff1 100644 --- a/examples/protocols/mqtt/ssl_mutual_auth/README.md +++ b/examples/protocols/mqtt/ssl_mutual_auth/README.md @@ -1,5 +1,5 @@ -| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C5 | ESP32-C6 | ESP32-H2 | ESP32-P4 | ESP32-S2 | ESP32-S3 | -| ----------------- | ----- | -------- | -------- | -------- | -------- | -------- | -------- | -------- | -------- | +| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C5 | ESP32-C6 | ESP32-C61 | ESP32-H2 | ESP32-P4 | ESP32-S2 | ESP32-S3 | +| ----------------- | ----- | -------- | -------- | -------- | -------- | --------- | -------- | -------- | -------- | -------- | # ESP-MQTT SSL Sample application (mutual authentication) diff --git a/examples/protocols/mqtt/ssl_psk/README.md b/examples/protocols/mqtt/ssl_psk/README.md index 7281b48..d5dbb1b 100644 --- a/examples/protocols/mqtt/ssl_psk/README.md +++ b/examples/protocols/mqtt/ssl_psk/README.md @@ -1,5 +1,5 @@ -| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C5 | ESP32-C6 | ESP32-H2 | ESP32-P4 | ESP32-S2 | ESP32-S3 | -| ----------------- | ----- | -------- | -------- | -------- | -------- | -------- | -------- | -------- | -------- | +| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C5 | ESP32-C6 | ESP32-C61 | ESP32-H2 | ESP32-P4 | ESP32-S2 | ESP32-S3 | +| ----------------- | ----- | -------- | -------- | -------- | -------- | --------- | -------- | -------- | -------- | -------- | # ESP-MQTT SSL example with PSK verification diff --git a/examples/protocols/mqtt/tcp/README.md b/examples/protocols/mqtt/tcp/README.md index 26a7fd7..be63964 100644 --- a/examples/protocols/mqtt/tcp/README.md +++ b/examples/protocols/mqtt/tcp/README.md @@ -1,5 +1,5 @@ -| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C5 | ESP32-C6 | ESP32-H2 | ESP32-P4 | ESP32-S2 | ESP32-S3 | -| ----------------- | ----- | -------- | -------- | -------- | -------- | -------- | -------- | -------- | -------- | +| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C5 | ESP32-C6 | ESP32-C61 | ESP32-H2 | ESP32-P4 | ESP32-S2 | ESP32-S3 | +| ----------------- | ----- | -------- | -------- | -------- | -------- | --------- | -------- | -------- | -------- | -------- | # ESP-MQTT sample application (See the README.md file in the upper level 'examples' directory for more information about examples.) diff --git a/examples/protocols/mqtt/ws/README.md b/examples/protocols/mqtt/ws/README.md index 7e52baa..5d1724f 100644 --- a/examples/protocols/mqtt/ws/README.md +++ b/examples/protocols/mqtt/ws/README.md @@ -1,5 +1,5 @@ -| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C5 | ESP32-C6 | ESP32-H2 | ESP32-P4 | ESP32-S2 | ESP32-S3 | -| ----------------- | ----- | -------- | -------- | -------- | -------- | -------- | -------- | -------- | -------- | +| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C5 | ESP32-C6 | ESP32-C61 | ESP32-H2 | ESP32-P4 | ESP32-S2 | ESP32-S3 | +| ----------------- | ----- | -------- | -------- | -------- | -------- | --------- | -------- | -------- | -------- | -------- | # ESP-MQTT MQTT over Websocket diff --git a/examples/protocols/mqtt/wss/README.md b/examples/protocols/mqtt/wss/README.md index 80a50bb..b401d31 100644 --- a/examples/protocols/mqtt/wss/README.md +++ b/examples/protocols/mqtt/wss/README.md @@ -1,5 +1,5 @@ -| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C5 | ESP32-C6 | ESP32-H2 | ESP32-P4 | ESP32-S2 | ESP32-S3 | -| ----------------- | ----- | -------- | -------- | -------- | -------- | -------- | -------- | -------- | -------- | +| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C5 | ESP32-C6 | ESP32-C61 | ESP32-H2 | ESP32-P4 | ESP32-S2 | ESP32-S3 | +| ----------------- | ----- | -------- | -------- | -------- | -------- | --------- | -------- | -------- | -------- | -------- | # ESP-MQTT MQTT over WSS Sample application (See the README.md file in the upper level 'examples' directory for more information about examples.) diff --git a/examples/protocols/mqtt5/README.md b/examples/protocols/mqtt5/README.md index a004c5e..3e6c9c2 100644 --- a/examples/protocols/mqtt5/README.md +++ b/examples/protocols/mqtt5/README.md @@ -1,5 +1,5 @@ -| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C5 | ESP32-C6 | ESP32-H2 | ESP32-P4 | ESP32-S2 | ESP32-S3 | -| ----------------- | ----- | -------- | -------- | -------- | -------- | -------- | -------- | -------- | -------- | +| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C5 | ESP32-C6 | ESP32-C61 | ESP32-H2 | ESP32-P4 | ESP32-S2 | ESP32-S3 | +| ----------------- | ----- | -------- | -------- | -------- | -------- | --------- | -------- | -------- | -------- | -------- | # ESP-MQTT sample application (See the README.md file in the upper level 'examples' directory for more information about examples.) From 8b6951a969bbebf1f6f3d25430f0f2dea9307945 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Fri, 30 Aug 2024 08:33:06 +0200 Subject: [PATCH 201/231] fix(examples): esp_hosted as default for Wi-Fi connect on P4 --- examples/protocols/mqtt/tcp/README.md | 153 +++++++++++++++++- .../protocols/mqtt/tcp/sdkconfig.ci.p4_eppp | 5 + .../protocols/mqtt/tcp/sdkconfig.ci.p4_wifi | 3 +- 3 files changed, 154 insertions(+), 7 deletions(-) create mode 100644 examples/protocols/mqtt/tcp/sdkconfig.ci.p4_eppp diff --git a/examples/protocols/mqtt/tcp/README.md b/examples/protocols/mqtt/tcp/README.md index be63964..cb9f2cc 100644 --- a/examples/protocols/mqtt/tcp/README.md +++ b/examples/protocols/mqtt/tcp/README.md @@ -64,7 +64,150 @@ DATA=data It is possible to use Wi-Fi connection on targets that do not support native Wi-Fi peripheral. This example demonstrates using `esp_wifi_remote` on ESP32P4 in the test configuration defined as `sdkconfig.ci.p4_wifi`. This configuration requires another ESP target with native Wi-Fi support physically connected to the ESP32-P4. -### Configure master-slave verification +This uses [esp_hosted](https://components.espressif.com/components/espressif/esp_hosted) project by default, please refer to its documentation for more details. +Note, that `esp_hosted` library currently transmits Wi-Fi credentials in plain text. In case this is a concern, please choose the `eppp` option in `esp_wifi_remote` configuration menu (`CONFIG_ESP_WIFI_REMOTE_LIBRARY_EPPP=y`) and setup master-slave verification (please see [eppp: Configure master-slave verification](#eppp)). + +### esp-hosted: Configure the slave project + +You first need to build and flash the slave project. It's possible to perform this action directly from the host project, these commands can be used to set the slave target device (for example ESP32C6), build and flash the slave project. You will have to hold the RST button to keep the host device (ESP32-P4) in reset while flashing the slave device. +``` +idf.py -C managed_components/espressif__esp_hosted/slave/ -B build_slave set-target esp32c6 +idf.py -C managed_components/espressif__esp_hosted/slave/ -B build_slave build flash monitor +``` + +### esp-hosted: Example Output of the slave device + +``` +I (348) cpu_start: Unicore app +I (357) cpu_start: Pro cpu start user code +I (357) cpu_start: cpu freq: 160000000 Hz +I (357) app_init: Application information: +I (360) app_init: Project name: network_adapter +I (365) app_init: App version: qa-test-full-master-esp32c5-202 +I (372) app_init: Compile time: Aug 30 2024 08:10:15 +I (378) app_init: ELF file SHA256: 6220fafe8... +I (383) app_init: ESP-IDF: v5.4-dev-2600-g1157a27964c-dirt +I (390) efuse_init: Min chip rev: v0.0 +I (395) efuse_init: Max chip rev: v0.99 +I (400) efuse_init: Chip rev: v0.1 +I (405) heap_init: Initializing. RAM available for dynamic allocation: +I (412) heap_init: At 4082FCD0 len 0004C940 (306 KiB): RAM +I (418) heap_init: At 4087C610 len 00002F54 (11 KiB): RAM +I (424) heap_init: At 50000000 len 00003FE8 (15 KiB): RTCRAM +I (432) spi_flash: detected chip: generic +I (435) spi_flash: flash io: dio +I (440) sleep_gpio: Configure to isolate all GPIO pins in sleep state +I (447) sleep_gpio: Enable automatic switching of GPIO sleep configuration +I (454) coexist: coex firmware version: 8da3f50af +I (481) coexist: coexist rom version 5b8dcfa +I (481) main_task: Started on CPU0 +I (481) main_task: Calling app_main() +I (482) fg_mcu_slave: ********************************************************************* +I (491) fg_mcu_slave: ESP-Hosted-MCU Slave FW version :: 0.0.6 +I (501) fg_mcu_slave: Transport used :: SDIO only +I (510) fg_mcu_slave: ********************************************************************* +I (519) fg_mcu_slave: Supported features are: +I (524) fg_mcu_slave: - WLAN over SDIO +I (528) h_bt: - BT/BLE +I (531) h_bt: - HCI Over SDIO +I (535) h_bt: - BLE only +I (539) fg_mcu_slave: capabilities: 0xd +I (543) fg_mcu_slave: Supported extended features are: +I (549) h_bt: - BT/BLE (extended) +I (553) fg_mcu_slave: extended capabilities: 0x0 +I (563) h_bt: ESP Bluetooth MAC addr: 40:4c:ca:5b:a0:8a +I (564) BLE_INIT: Using main XTAL as clock source +I (574) BLE_INIT: ble controller commit:[7491a85] +I (575) BLE_INIT: Bluetooth MAC: 40:4c:ca:5b:a0:8a +I (581) phy_init: phy_version 310,dde1ba9,Jun 4 2024,16:38:11 +I (641) phy: libbtbb version: 04952fd, Jun 4 2024, 16:38:26 +I (642) SDIO_SLAVE: Using SDIO interface +I (642) SDIO_SLAVE: sdio_init: sending mode: SDIO_SLAVE_SEND_STREAM +I (648) SDIO_SLAVE: sdio_init: ESP32-C6 SDIO RxQ[20] timing[0] + +I (1155) fg_mcu_slave: Start Data Path +I (1165) fg_mcu_slave: Initial set up done +I (1165) slave_ctrl: event ESPInit +``` + +### esp_hosted: Example Output of the master device (ESP32-P4) + +``` +I (1833) sdio_wrapper: Function 0 Blocksize: 512 +I (1843) sdio_wrapper: Function 1 Blocksize: 512 +I (1843) H_SDIO_DRV: SDIO Host operating in STREAMING MODE +I (1853) H_SDIO_DRV: generate slave intr +I (1863) transport: Received INIT event from ESP32 peripheral +I (1873) transport: EVENT: 12 +I (1873) transport: EVENT: 11 +I (1873) transport: capabilities: 0xd +I (1873) transport: Features supported are: +I (1883) transport: * WLAN +I (1883) transport: - HCI over SDIO +I (1893) transport: - BLE only +I (1893) transport: EVENT: 13 +I (1893) transport: ESP board type is : 13 + +I (1903) transport: Base transport is set-up + +I (1903) transport: Slave chip Id[12] +I (1913) hci_stub_drv: Host BT Support: Disabled +I (1913) H_SDIO_DRV: Received INIT event +I (1923) rpc_evt: EVENT: ESP INIT + +I (1923) rpc_wrap: Received Slave ESP Init +I (2703) rpc_core: <-- RPC_Req [0x116], uid 1 +I (2823) rpc_rsp: --> RPC_Resp [0x216], uid 1 +I (2823) rpc_core: <-- RPC_Req [0x139], uid 2 +I (2833) rpc_rsp: --> RPC_Resp [0x239], uid 2 +I (2833) rpc_core: <-- RPC_Req [0x104], uid 3 +I (2843) rpc_rsp: --> RPC_Resp [0x204], uid 3 +I (2843) rpc_core: <-- RPC_Req [0x118], uid 4 +I (2933) rpc_rsp: --> RPC_Resp [0x218], uid 4 +I (2933) example_connect: Connecting to Cermakowifi... +I (2933) rpc_core: <-- RPC_Req [0x11c], uid 5 +I (2943) rpc_evt: Event [0x2b] received +I (2943) rpc_evt: Event [0x2] received +I (2953) rpc_evt: EVT rcvd: Wi-Fi Start +I (2953) rpc_core: <-- RPC_Req [0x101], uid 6 +I (2973) rpc_rsp: --> RPC_Resp [0x21c], uid 5 +I (2973) H_API: esp_wifi_remote_connect +I (2973) rpc_core: <-- RPC_Req [0x11a], uid 7 +I (2983) rpc_rsp: --> RPC_Resp [0x201], uid 6 +I (3003) rpc_rsp: --> RPC_Resp [0x21a], uid 7 +I (3003) example_connect: Waiting for IP(s) +I (5723) rpc_evt: Event [0x2b] received +I (5943) esp_wifi_remote: esp_wifi_internal_reg_rxcb: sta: 0x400309fe +0x400309fe: wifi_sta_receive at /home/david/esp/idf/components/esp_wifi/src/wifi_netif.c:38 + +I (7573) example_connect: Got IPv6 event: Interface "example_netif_sta" address: fe80:0000:0000:0000:424c:caff:fe5b:a088, type: ESP_IP6_ADDR_IS_LINK_LOCAL +I (9943) esp_netif_handlers: example_netif_sta ip: 192.168.0.29, mask: 255.255.255.0, gw: 192.168.0.1 +I (9943) example_connect: Got IPv4 event: Interface "example_netif_sta" address: 192.168.0.29 +I (9943) example_common: Connected to example_netif_sta +I (9953) example_common: - IPv4 address: 192.168.0.29, +I (9963) example_common: - IPv6 address: fe80:0000:0000:0000:424c:caff:fe5b:a088, type: ESP_IP6_ADDR_IS_LINK_LOCAL +I (9973) mqtt_example: Other event id:7 +I (9973) main_task: Returned from app_main() +I (10253) mqtt_example: MQTT_EVENT_CONNECTED +I (10253) mqtt_example: sent publish successful, msg_id=45053 +I (10253) mqtt_example: sent subscribe successful, msg_id=34643 +I (10263) mqtt_example: sent subscribe successful, msg_id=2358 +I (10263) mqtt_example: sent unsubscribe successful, msg_id=57769 +I (10453) mqtt_example: MQTT_EVENT_PUBLISHED, msg_id=45053 +I (10603) mqtt_example: MQTT_EVENT_SUBSCRIBED, msg_id=34643 +I (10603) mqtt_example: sent publish successful, msg_id=0 +I (10603) mqtt_example: MQTT_EVENT_SUBSCRIBED, msg_id=2358 +I (10613) mqtt_example: sent publish successful, msg_id=0 +I (10613) mqtt_example: MQTT_EVENT_UNSUBSCRIBED, msg_id=57769 +I (10713) mqtt_example: MQTT_EVENT_DATA +TOPIC=/topic/qos0 +DATA=data +I (10863) mqtt_example: MQTT_EVENT_DATA +TOPIC=/topic/qos0 +DATA=data +``` + +### eppp: Configure master-slave verification In order to secure the physical connection between the ESP32-P4 (master) and the slave device, it is necessary to set certificates and keys for each side. To bootstrap this step, you can use one-time generated self-signed RSA keys and certificates running: @@ -72,7 +215,7 @@ To bootstrap this step, you can use one-time generated self-signed RSA keys and ./managed_components/espressif__esp_wifi_remote/examples/test_certs/generate_test_certs.sh espressif.local ``` -### Configure the slave project +#### eppp: Configure the slave project It is recommended to create a new project from `esp_wifi_remote` component's example with ``` @@ -91,7 +234,7 @@ Please follow these steps to setup the slave application: - `CONFIG_ESP_WIFI_REMOTE_EPPP_SERVER_KEY` -- slave's private key * `idf.py build flash monitor` -### Configure the master project (ESP32-P4) +#### eppp: Configure the master project (ESP32-P4) similarly to the slave project, we have to configure * the physical connection @@ -105,7 +248,7 @@ After project configuration, you build and flash the board with idf.py build flash monitor ``` -### Example Output of the slave device +### eppp: Example Output of the slave device ``` I (7982) main_task: Returned from app_main() @@ -183,7 +326,7 @@ I (15682) rpc_server: Main DNS:185.162.24.55 I (15682) rpc_server: IP address:192.168.0.33 ``` -### Example Output of the master device (ESP32-P4) +### eppp: Example Output of the master device (ESP32-P4) ``` I (445) example_connect: Start example_connect. diff --git a/examples/protocols/mqtt/tcp/sdkconfig.ci.p4_eppp b/examples/protocols/mqtt/tcp/sdkconfig.ci.p4_eppp new file mode 100644 index 0000000..8d423df --- /dev/null +++ b/examples/protocols/mqtt/tcp/sdkconfig.ci.p4_eppp @@ -0,0 +1,5 @@ +CONFIG_IDF_TARGET="esp32p4" +CONFIG_EXAMPLE_CONNECT_WIFI=y +CONFIG_ESP_WIFI_REMOTE_LIBRARY_EPPP=y +CONFIG_ESP_WIFI_REMOTE_EPPP_UART_TX_PIN=17 +CONFIG_ESP_WIFI_REMOTE_EPPP_UART_RX_PIN=16 diff --git a/examples/protocols/mqtt/tcp/sdkconfig.ci.p4_wifi b/examples/protocols/mqtt/tcp/sdkconfig.ci.p4_wifi index 7a5574c..a2ea93a 100644 --- a/examples/protocols/mqtt/tcp/sdkconfig.ci.p4_wifi +++ b/examples/protocols/mqtt/tcp/sdkconfig.ci.p4_wifi @@ -1,4 +1,3 @@ CONFIG_IDF_TARGET="esp32p4" CONFIG_EXAMPLE_CONNECT_WIFI=y -CONFIG_ESP_WIFI_REMOTE_EPPP_UART_TX_PIN=17 -CONFIG_ESP_WIFI_REMOTE_EPPP_UART_RX_PIN=16 +CONFIG_ESP_WIFI_REMOTE_LIBRARY_HOSTED=y From 2a68028e5039d9379dbefb9bb16ba613af8d943c Mon Sep 17 00:00:00 2001 From: Euripedes Rocha Date: Thu, 10 Oct 2024 10:49:36 +0200 Subject: [PATCH 202/231] ci(mqtt): Set partition to large for c6 on custom outbox example Example failed to build on CI for c6 target due to partition size. --- examples/protocols/mqtt/custom_outbox/sdkconfig.ci.esp32c6 | 1 + 1 file changed, 1 insertion(+) create mode 100644 examples/protocols/mqtt/custom_outbox/sdkconfig.ci.esp32c6 diff --git a/examples/protocols/mqtt/custom_outbox/sdkconfig.ci.esp32c6 b/examples/protocols/mqtt/custom_outbox/sdkconfig.ci.esp32c6 new file mode 100644 index 0000000..1686559 --- /dev/null +++ b/examples/protocols/mqtt/custom_outbox/sdkconfig.ci.esp32c6 @@ -0,0 +1 @@ +CONFIG_PARTITION_TABLE_SINGLE_APP_LARGE=y From c5fb6c94366935478f7aaca85f74306cf5162bec Mon Sep 17 00:00:00 2001 From: Linda Date: Fri, 20 Sep 2024 12:17:36 +0800 Subject: [PATCH 203/231] docs: update application examples for modbus.rst, mqtt.rst and lwip.rst --- docs/en/api-reference/protocols/mqtt.rst | 27 ++++++++++++++------- docs/zh_CN/api-reference/protocols/mqtt.rst | 25 +++++++++++++------ 2 files changed, 35 insertions(+), 17 deletions(-) diff --git a/docs/en/api-reference/protocols/mqtt.rst b/docs/en/api-reference/protocols/mqtt.rst index 7c5899d..b408c3b 100644 --- a/docs/en/api-reference/protocols/mqtt.rst +++ b/docs/en/api-reference/protocols/mqtt.rst @@ -19,16 +19,25 @@ Features Application Examples ---------------------- +-------------------- - * :example:`protocols/mqtt/tcp`: MQTT over TCP, default port 1883 - * :example:`protocols/mqtt/ssl`: MQTT over TLS, default port 8883 - * :example:`protocols/mqtt/ssl_ds`: MQTT over TLS using digital signature peripheral for authentication, default port 8883 - * :example:`protocols/mqtt/ssl_mutual_auth`: MQTT over TLS using certificates for authentication, default port 8883 - * :example:`protocols/mqtt/ssl_psk`: MQTT over TLS using pre-shared keys for authentication, default port 8883 - * :example:`protocols/mqtt/ws`: MQTT over WebSocket, default port 80 - * :example:`protocols/mqtt/wss`: MQTT over WebSocket Secure, default port 443 - * :example:`protocols/mqtt5`: Uses ESP-MQTT library to connect to broker with MQTT v5.0 + - :example:`protocols/mqtt/tcp` demonstrates how to implement MQTT communication over TCP (default port 1883). + + - :example:`protocols/mqtt/ssl` demonstrates how to use SSL transport to implement MQTT communication over TLS (default port 8883). + + - :example:`protocols/mqtt/ssl_ds` demonstrates how to use digital signature peripheral for authentication to implement MQTT communication over TLS (default port 8883). + + - :example:`protocols/mqtt/ssl_mutual_auth` demonstrates how to use certificates for authentication to implement MQTT communication (default port 8883). + + - :example:`protocols/mqtt/ssl_psk` demonstrates how to use pre-shared keys for authentication to implement MQTT communication over TLS (default port 8883). + + - :example:`protocols/mqtt/ws` demonstrates how to implement MQTT communication over WebSocket (default port 80). + + - :example:`protocols/mqtt/wss` demonstrates how to implement MQTT communication over WebSocket Secure (default port 443). + + - :example:`protocols/mqtt5` demonstrates how to use ESP-MQTT library to connect to broker with MQTT v5.0. + + - :example:`protocols/mqtt/custom_outbox` demonstrates how to customize the outbox in the ESP-MQTT library. MQTT Message Retransmission --------------------------- diff --git a/docs/zh_CN/api-reference/protocols/mqtt.rst b/docs/zh_CN/api-reference/protocols/mqtt.rst index 9365dfe..7ad56ec 100644 --- a/docs/zh_CN/api-reference/protocols/mqtt.rst +++ b/docs/zh_CN/api-reference/protocols/mqtt.rst @@ -21,14 +21,23 @@ ESP-MQTT 是 `MQTT `__ 协议客户端的实现,MQTT 是一 应用示例 ------------------- - * :example:`protocols/mqtt/tcp`:基于 TCP 的 MQTT,默认端口 1883 - * :example:`protocols/mqtt/ssl`:基于 TLS 的 MQTT,默认端口 8883 - * :example:`protocols/mqtt/ssl_ds`:基于 TLS 的 MQTT,使用数字签名外设进行身份验证,默认端口 8883 - * :example:`protocols/mqtt/ssl_mutual_auth`:基于 TLS 的 MQTT,使用证书进行身份验证,默认端口 8883 - * :example:`protocols/mqtt/ssl_psk`:基于 TLS 的 MQTT,使用预共享密钥进行身份验证,默认端口 8883 - * :example:`protocols/mqtt/ws`:基于 WebSocket 的 MQTT,默认端口 80 - * :example:`protocols/mqtt/wss`:基于 WebSocket Secure 的 MQTT,默认端口 443 - * :example:`protocols/mqtt5`: 使用 ESP-MQTT 库连接 MQTT v5.0 的服务器 + - :example:`protocols/mqtt/tcp` 演示了如何通过 TCP 实现 MQTT 通信(默认端口 1883)。 + + - :example:`protocols/mqtt/ssl` 演示了如何使用 SSL 传输来实现基于 TLS 的 MQTT 通信(默认端口 8883)。 + + - :example:`protocols/mqtt/ssl_ds` 演示了如何使用数字签名外设进行身份验证,以实现基于 TLS 的 MQTT 通信(默认端口 8883)。 + + - :example:`protocols/mqtt/ssl_mutual_auth` 演示了如何使用证书进行身份验证实现 MQTT 通信(默认端口 8883)。 + + - :example:`protocols/mqtt/ssl_psk` 演示了如何使用预共享密钥进行身份验证,以实现基于 TLS 的 MQTT 通信(默认端口 8883)。 + + - :example:`protocols/mqtt/ws` 演示了如何通过 WebSocket 实现 MQTT 通信(默认端口 80)。 + + - :example:`protocols/mqtt/wss` 演示了如何通过 WebSocket Secure 实现 MQTT 通信(默认端口 443)。 + + - :example:`protocols/mqtt5` 演示了如何使用 ESP-MQTT 库通过 MQTT v5.0 连接到代理。 + + - :example:`protocols/mqtt/custom_outbox` 演示了如何自定义 ESP-MQTT 库中的 outbox。 MQTT 消息重传 -------------------------- From e71a595cf8a8e37d5bf1f4181ef78672054f6006 Mon Sep 17 00:00:00 2001 From: Frantisek Hrbata Date: Tue, 24 Sep 2024 14:22:10 +0200 Subject: [PATCH 204/231] change(examples): explicitly specify component dependencies for examples Currently, several examples do not explicitly state their component dependencies, relying instead on the default behavior that includes all registered components and commonly required ones in the build. Explicitly adding component dependencies can reduce build time when set(COMPONENTS main) is used. Signed-off-by: Frantisek Hrbata --- examples/protocols/mqtt/custom_outbox/main/CMakeLists.txt | 1 + examples/protocols/mqtt/ssl/main/CMakeLists.txt | 1 + examples/protocols/mqtt/ssl_ds/main/CMakeLists.txt | 1 + examples/protocols/mqtt/ssl_mutual_auth/main/CMakeLists.txt | 1 + examples/protocols/mqtt/ssl_psk/main/CMakeLists.txt | 1 + examples/protocols/mqtt/tcp/main/CMakeLists.txt | 1 + examples/protocols/mqtt/ws/main/CMakeLists.txt | 1 + examples/protocols/mqtt/wss/main/CMakeLists.txt | 1 + examples/protocols/mqtt5/main/CMakeLists.txt | 1 + 9 files changed, 9 insertions(+) diff --git a/examples/protocols/mqtt/custom_outbox/main/CMakeLists.txt b/examples/protocols/mqtt/custom_outbox/main/CMakeLists.txt index cb970c8..7423b60 100644 --- a/examples/protocols/mqtt/custom_outbox/main/CMakeLists.txt +++ b/examples/protocols/mqtt/custom_outbox/main/CMakeLists.txt @@ -1,3 +1,4 @@ idf_component_register(SRCS "app_main.c" INCLUDE_DIRS "." + PRIV_REQUIRES mqtt nvs_flash esp_netif ) diff --git a/examples/protocols/mqtt/ssl/main/CMakeLists.txt b/examples/protocols/mqtt/ssl/main/CMakeLists.txt index 61fac40..d6ca62d 100644 --- a/examples/protocols/mqtt/ssl/main/CMakeLists.txt +++ b/examples/protocols/mqtt/ssl/main/CMakeLists.txt @@ -1,2 +1,3 @@ idf_component_register(SRCS "app_main.c" + PRIV_REQUIRES mqtt esp_partition nvs_flash esp_netif app_update INCLUDE_DIRS ".") diff --git a/examples/protocols/mqtt/ssl_ds/main/CMakeLists.txt b/examples/protocols/mqtt/ssl_ds/main/CMakeLists.txt index 61fac40..055d4c5 100644 --- a/examples/protocols/mqtt/ssl_ds/main/CMakeLists.txt +++ b/examples/protocols/mqtt/ssl_ds/main/CMakeLists.txt @@ -1,2 +1,3 @@ idf_component_register(SRCS "app_main.c" + PRIV_REQUIRES mqtt esp_netif INCLUDE_DIRS ".") diff --git a/examples/protocols/mqtt/ssl_mutual_auth/main/CMakeLists.txt b/examples/protocols/mqtt/ssl_mutual_auth/main/CMakeLists.txt index 61fac40..31650f3 100644 --- a/examples/protocols/mqtt/ssl_mutual_auth/main/CMakeLists.txt +++ b/examples/protocols/mqtt/ssl_mutual_auth/main/CMakeLists.txt @@ -1,2 +1,3 @@ idf_component_register(SRCS "app_main.c" + PRIV_REQUIRES mqtt esp_wifi nvs_flash INCLUDE_DIRS ".") diff --git a/examples/protocols/mqtt/ssl_psk/main/CMakeLists.txt b/examples/protocols/mqtt/ssl_psk/main/CMakeLists.txt index 61fac40..31650f3 100644 --- a/examples/protocols/mqtt/ssl_psk/main/CMakeLists.txt +++ b/examples/protocols/mqtt/ssl_psk/main/CMakeLists.txt @@ -1,2 +1,3 @@ idf_component_register(SRCS "app_main.c" + PRIV_REQUIRES mqtt esp_wifi nvs_flash INCLUDE_DIRS ".") diff --git a/examples/protocols/mqtt/tcp/main/CMakeLists.txt b/examples/protocols/mqtt/tcp/main/CMakeLists.txt index 61fac40..3bf739c 100644 --- a/examples/protocols/mqtt/tcp/main/CMakeLists.txt +++ b/examples/protocols/mqtt/tcp/main/CMakeLists.txt @@ -1,2 +1,3 @@ idf_component_register(SRCS "app_main.c" + PRIV_REQUIRES mqtt nvs_flash esp_netif INCLUDE_DIRS ".") diff --git a/examples/protocols/mqtt/ws/main/CMakeLists.txt b/examples/protocols/mqtt/ws/main/CMakeLists.txt index 61fac40..31650f3 100644 --- a/examples/protocols/mqtt/ws/main/CMakeLists.txt +++ b/examples/protocols/mqtt/ws/main/CMakeLists.txt @@ -1,2 +1,3 @@ idf_component_register(SRCS "app_main.c" + PRIV_REQUIRES mqtt esp_wifi nvs_flash INCLUDE_DIRS ".") diff --git a/examples/protocols/mqtt/wss/main/CMakeLists.txt b/examples/protocols/mqtt/wss/main/CMakeLists.txt index 61fac40..31650f3 100644 --- a/examples/protocols/mqtt/wss/main/CMakeLists.txt +++ b/examples/protocols/mqtt/wss/main/CMakeLists.txt @@ -1,2 +1,3 @@ idf_component_register(SRCS "app_main.c" + PRIV_REQUIRES mqtt esp_wifi nvs_flash INCLUDE_DIRS ".") diff --git a/examples/protocols/mqtt5/main/CMakeLists.txt b/examples/protocols/mqtt5/main/CMakeLists.txt index 61fac40..3bf739c 100644 --- a/examples/protocols/mqtt5/main/CMakeLists.txt +++ b/examples/protocols/mqtt5/main/CMakeLists.txt @@ -1,2 +1,3 @@ idf_component_register(SRCS "app_main.c" + PRIV_REQUIRES mqtt nvs_flash esp_netif INCLUDE_DIRS ".") From b17444a109987e566ec61b08fb834c3be55aba15 Mon Sep 17 00:00:00 2001 From: Frantisek Hrbata Date: Wed, 25 Sep 2024 17:10:37 +0200 Subject: [PATCH 205/231] change(examples): switch examples to use a minimal build Currently, several example dependencies rely on the fact that all registered components are added to the build, along with components specified in common requirements. This results in longer build times because even unused components must be built. Switch all examples to use idf_minimal_build to compile only the components actually required by the example. Signed-off-by: Frantisek Hrbata --- examples/protocols/mqtt/custom_outbox/CMakeLists.txt | 2 ++ examples/protocols/mqtt/ssl/CMakeLists.txt | 2 ++ examples/protocols/mqtt/ssl_ds/CMakeLists.txt | 2 ++ examples/protocols/mqtt/ssl_mutual_auth/CMakeLists.txt | 2 ++ examples/protocols/mqtt/ssl_psk/CMakeLists.txt | 2 ++ examples/protocols/mqtt/tcp/CMakeLists.txt | 2 ++ examples/protocols/mqtt/ws/CMakeLists.txt | 2 ++ examples/protocols/mqtt/wss/CMakeLists.txt | 2 ++ examples/protocols/mqtt5/CMakeLists.txt | 2 ++ 9 files changed, 18 insertions(+) diff --git a/examples/protocols/mqtt/custom_outbox/CMakeLists.txt b/examples/protocols/mqtt/custom_outbox/CMakeLists.txt index 4823b13..746ab1c 100644 --- a/examples/protocols/mqtt/custom_outbox/CMakeLists.txt +++ b/examples/protocols/mqtt/custom_outbox/CMakeLists.txt @@ -3,6 +3,8 @@ cmake_minimum_required(VERSION 3.16) include($ENV{IDF_PATH}/tools/cmake/project.cmake) +# "Trim" the build. Include the minimal set of components, main, and anything it depends on. +idf_build_set_property(MINIMAL_BUILD ON) project(mqtt_tcp_custom_outbox) # Add custom outbox implementation to mqtt component diff --git a/examples/protocols/mqtt/ssl/CMakeLists.txt b/examples/protocols/mqtt/ssl/CMakeLists.txt index 994f2f2..699c86d 100644 --- a/examples/protocols/mqtt/ssl/CMakeLists.txt +++ b/examples/protocols/mqtt/ssl/CMakeLists.txt @@ -4,6 +4,8 @@ cmake_minimum_required(VERSION 3.16) include($ENV{IDF_PATH}/tools/cmake/project.cmake) +# "Trim" the build. Include the minimal set of components, main, and anything it depends on. +idf_build_set_property(MINIMAL_BUILD ON) project(mqtt_ssl) target_add_binary_data(mqtt_ssl.elf "main/mqtt_eclipseprojects_io.pem" TEXT) diff --git a/examples/protocols/mqtt/ssl_ds/CMakeLists.txt b/examples/protocols/mqtt/ssl_ds/CMakeLists.txt index ce04bb0..acf7743 100644 --- a/examples/protocols/mqtt/ssl_ds/CMakeLists.txt +++ b/examples/protocols/mqtt/ssl_ds/CMakeLists.txt @@ -4,6 +4,8 @@ cmake_minimum_required(VERSION 3.16) include($ENV{IDF_PATH}/tools/cmake/project.cmake) +# "Trim" the build. Include the minimal set of components, main, and anything it depends on. +idf_build_set_property(MINIMAL_BUILD ON) project(mqtt_ssl_ds) # Flash the custom partition named `esp_secure_cert`. diff --git a/examples/protocols/mqtt/ssl_mutual_auth/CMakeLists.txt b/examples/protocols/mqtt/ssl_mutual_auth/CMakeLists.txt index fc3d759..67ec43b 100644 --- a/examples/protocols/mqtt/ssl_mutual_auth/CMakeLists.txt +++ b/examples/protocols/mqtt/ssl_mutual_auth/CMakeLists.txt @@ -4,6 +4,8 @@ cmake_minimum_required(VERSION 3.16) include($ENV{IDF_PATH}/tools/cmake/project.cmake) +# "Trim" the build. Include the minimal set of components, main, and anything it depends on. +idf_build_set_property(MINIMAL_BUILD ON) project(mqtt_ssl_mutual_auth) target_add_binary_data(${CMAKE_PROJECT_NAME}.elf "main/client.crt" TEXT) diff --git a/examples/protocols/mqtt/ssl_psk/CMakeLists.txt b/examples/protocols/mqtt/ssl_psk/CMakeLists.txt index d9c1a5f..d30f796 100644 --- a/examples/protocols/mqtt/ssl_psk/CMakeLists.txt +++ b/examples/protocols/mqtt/ssl_psk/CMakeLists.txt @@ -4,4 +4,6 @@ cmake_minimum_required(VERSION 3.16) include($ENV{IDF_PATH}/tools/cmake/project.cmake) +# "Trim" the build. Include the minimal set of components, main, and anything it depends on. +idf_build_set_property(MINIMAL_BUILD ON) project(mqtt_ssl_psk) diff --git a/examples/protocols/mqtt/tcp/CMakeLists.txt b/examples/protocols/mqtt/tcp/CMakeLists.txt index d6d9432..d6f01d8 100644 --- a/examples/protocols/mqtt/tcp/CMakeLists.txt +++ b/examples/protocols/mqtt/tcp/CMakeLists.txt @@ -4,4 +4,6 @@ cmake_minimum_required(VERSION 3.16) include($ENV{IDF_PATH}/tools/cmake/project.cmake) +# "Trim" the build. Include the minimal set of components, main, and anything it depends on. +idf_build_set_property(MINIMAL_BUILD ON) project(mqtt_tcp) diff --git a/examples/protocols/mqtt/ws/CMakeLists.txt b/examples/protocols/mqtt/ws/CMakeLists.txt index 475dd9e..bc5e2f1 100644 --- a/examples/protocols/mqtt/ws/CMakeLists.txt +++ b/examples/protocols/mqtt/ws/CMakeLists.txt @@ -4,4 +4,6 @@ cmake_minimum_required(VERSION 3.16) include($ENV{IDF_PATH}/tools/cmake/project.cmake) +# "Trim" the build. Include the minimal set of components, main, and anything it depends on. +idf_build_set_property(MINIMAL_BUILD ON) project(mqtt_websocket) diff --git a/examples/protocols/mqtt/wss/CMakeLists.txt b/examples/protocols/mqtt/wss/CMakeLists.txt index 20c1fde..158bc62 100644 --- a/examples/protocols/mqtt/wss/CMakeLists.txt +++ b/examples/protocols/mqtt/wss/CMakeLists.txt @@ -4,6 +4,8 @@ cmake_minimum_required(VERSION 3.16) include($ENV{IDF_PATH}/tools/cmake/project.cmake) +# "Trim" the build. Include the minimal set of components, main, and anything it depends on. +idf_build_set_property(MINIMAL_BUILD ON) project(mqtt_websocket_secure) target_add_binary_data(mqtt_websocket_secure.elf "main/mqtt_eclipseprojects_io.pem" TEXT) diff --git a/examples/protocols/mqtt5/CMakeLists.txt b/examples/protocols/mqtt5/CMakeLists.txt index 19304b4..fe8bdc9 100644 --- a/examples/protocols/mqtt5/CMakeLists.txt +++ b/examples/protocols/mqtt5/CMakeLists.txt @@ -4,4 +4,6 @@ cmake_minimum_required(VERSION 3.16) include($ENV{IDF_PATH}/tools/cmake/project.cmake) +# "Trim" the build. Include the minimal set of components, main, and anything it depends on. +idf_build_set_property(MINIMAL_BUILD ON) project(mqtt5) From ec8a9ec2cbf5175e06acbc233cf4726c5efe4077 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Fri, 13 Sep 2024 18:09:19 +0200 Subject: [PATCH 206/231] feat(mqtt): Add publish stress test with local broker --- .../mqtt/publish_connect_test/CMakeLists.txt | 4 -- .../main/Kconfig.projbuild | 23 ++++++++++- .../main/idf_component.yml | 8 ++++ .../main/publish_connect_test.c | 17 +++++++- .../pytest_mqtt_publish_app.py | 40 ++++++++++++++++--- .../sdkconfig.ci.local_broker | 13 ++++++ 6 files changed, 92 insertions(+), 13 deletions(-) create mode 100644 tools/test_apps/protocols/mqtt/publish_connect_test/main/idf_component.yml create mode 100644 tools/test_apps/protocols/mqtt/publish_connect_test/sdkconfig.ci.local_broker diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/CMakeLists.txt b/tools/test_apps/protocols/mqtt/publish_connect_test/CMakeLists.txt index ee3e421..4776e74 100644 --- a/tools/test_apps/protocols/mqtt/publish_connect_test/CMakeLists.txt +++ b/tools/test_apps/protocols/mqtt/publish_connect_test/CMakeLists.txt @@ -2,10 +2,6 @@ # in this exact order for cmake to work correctly cmake_minimum_required(VERSION 3.16) -# (Not part of the boilerplate) -# This example uses an extra component for common functions such as Wi-Fi and Ethernet connection. -set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common) - include($ENV{IDF_PATH}/tools/cmake/project.cmake) project(mqtt_publish_connect_test) diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/main/Kconfig.projbuild b/tools/test_apps/protocols/mqtt/publish_connect_test/main/Kconfig.projbuild index 3bea21c..8e4cfe5 100644 --- a/tools/test_apps/protocols/mqtt/publish_connect_test/main/Kconfig.projbuild +++ b/tools/test_apps/protocols/mqtt/publish_connect_test/main/Kconfig.projbuild @@ -34,7 +34,7 @@ menu "Example Configuration" string "subscribe topic" default "/topic/subscribe/py2esp" help - topic to which esp32 client subsribes (and expects data) + topic to which esp32 client subscribes (and expects data) config EXAMPLE_BROKER_CERTIFICATE_OVERRIDE string "Broker certificate override" @@ -47,6 +47,27 @@ menu "Example Configuration" bool default y if EXAMPLE_BROKER_CERTIFICATE_OVERRIDE != "" + config EXAMPLE_RUN_LOCAL_BROKER + bool "Run local mosquitto" + default n + help + If enabled, this tests uses local mosquitto broker + running on the same endpoint as the client + + config EXAMPLE_BROKER_HOST + string "Broker host address" + default "0.0.0.0" + depends on EXAMPLE_RUN_LOCAL_BROKER + help + Host name of the endpoint to bind the mosquitto listener. + + config EXAMPLE_BROKER_PORT + int "Broker port" + default 1234 + depends on EXAMPLE_RUN_LOCAL_BROKER + help + Port of the endpoint to bind the mosquitto listener + config EXAMPLE_CONNECT_CASE_NO_CERT # Note: All the below config values (EXAMPLE_CONNECT_CASE...) are hidden and # used to give symbolic names to test cases, which are then referenced from both diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/main/idf_component.yml b/tools/test_apps/protocols/mqtt/publish_connect_test/main/idf_component.yml new file mode 100644 index 0000000..9eba86b --- /dev/null +++ b/tools/test_apps/protocols/mqtt/publish_connect_test/main/idf_component.yml @@ -0,0 +1,8 @@ +## IDF Component Manager Manifest File +dependencies: + espressif/mosquitto: + version: "*" + protocol_examples_common: + path: ${IDF_PATH}/examples/common_components/protocol_examples_common + idf: + version: ">=4.1.0" diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/main/publish_connect_test.c b/tools/test_apps/protocols/mqtt/publish_connect_test/main/publish_connect_test.c index d92a1fa..23a58a6 100644 --- a/tools/test_apps/protocols/mqtt/publish_connect_test/main/publish_connect_test.c +++ b/tools/test_apps/protocols/mqtt/publish_connect_test/main/publish_connect_test.c @@ -20,6 +20,7 @@ #include "argtable3/argtable3.h" #include "esp_log.h" #include "publish_connect_test.h" +#include "mosq_broker.h" static const char *TAG = "publish_connect_test"; @@ -47,7 +48,7 @@ static int do_init(int argc, char **argv) { (void)argc; (void)argv; const esp_mqtt_client_config_t mqtt_cfg = { - .broker.address.uri = "mqtts://127.0.0.1:1234", + .broker.address.uri = "mqtt://127.0.0.1:1234", .network.disable_auto_reconnect = true }; command_context.mqtt_client = esp_mqtt_client_init(&mqtt_cfg); @@ -78,7 +79,7 @@ static int do_stop(int argc, char **argv) { ESP_LOGE(TAG, "Failed to stop mqtt client task"); return 1; } - ESP_LOGI(TAG, "Mqtt client stoped"); + ESP_LOGI(TAG, "Mqtt client stopped"); return 0; } @@ -286,6 +287,15 @@ void register_connect_commands(void){ ESP_ERROR_CHECK(esp_console_cmd_register(&connection_teardown)); } +#ifdef CONFIG_EXAMPLE_RUN_LOCAL_BROKER +static void broker_task(void* ctx) +{ + // broker continues to run in this task + struct mosq_broker_config config = { .host = CONFIG_EXAMPLE_BROKER_HOST, .port = CONFIG_EXAMPLE_BROKER_PORT }; + mosq_broker_run(&config); +} +#endif // CONFIG_EXAMPLE_RUN_LOCAL_BROKER + void app_main(void) { static const size_t max_line = 256; @@ -301,6 +311,9 @@ void app_main(void) ESP_ERROR_CHECK(nvs_flash_init()); ESP_ERROR_CHECK(esp_netif_init()); ESP_ERROR_CHECK(esp_event_loop_create_default()); +#ifdef CONFIG_EXAMPLE_RUN_LOCAL_BROKER + xTaskCreate(broker_task, "broker", 4096, NULL, 4, NULL); +#endif ESP_ERROR_CHECK(example_connect()); esp_console_repl_t *repl = NULL; esp_console_repl_config_t repl_config = ESP_CONSOLE_REPL_CONFIG_DEFAULT(); diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/pytest_mqtt_publish_app.py b/tools/test_apps/protocols/mqtt/publish_connect_test/pytest_mqtt_publish_app.py index 544c0b7..763574c 100644 --- a/tools/test_apps/protocols/mqtt/publish_connect_test/pytest_mqtt_publish_app.py +++ b/tools/test_apps/protocols/mqtt/publish_connect_test/pytest_mqtt_publish_app.py @@ -1,6 +1,5 @@ -# SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD +# SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD # SPDX-License-Identifier: Unlicense OR CC0-1.0 - import contextlib import difflib import logging @@ -9,9 +8,15 @@ import random import re import ssl import string -from itertools import count, product -from threading import Event, Lock -from typing import Any, Dict, List, Tuple, no_type_check +from itertools import count +from itertools import product +from threading import Event +from threading import Lock +from typing import Any +from typing import Dict +from typing import List +from typing import no_type_check +from typing import Tuple import paho.mqtt.client as mqtt import pexpect @@ -152,7 +157,7 @@ def get_scenarios() -> List[Dict[str, int]]: def get_timeout(test_case: Any) -> int: transport, qos, enqueue, scenario = test_case if transport in ['ws', 'wss'] or qos == 2: - return 90 + return 120 return 60 @@ -198,6 +203,11 @@ stress_scenarios = [{'len':20, 'repeat':50}] # many medium sized transport_cases = ['tcp', 'ws', 'wss', 'ssl'] qos_cases = [0, 1, 2] enqueue_cases = [0, 1] +local_broker_supported_transports = ['tcp'] +local_broker_scenarios = [{'len':0, 'repeat':5}, # zero-sized messages + {'len':5, 'repeat':20}, # short messages + {'len':500, 'repeat':10}, # long messages + {'len':20, 'repeat':20}] # many medium sized def make_cases(scenarios: List[Dict[str, int]]) -> List[Tuple[str, int, int, Dict[str, int]]]: @@ -212,6 +222,7 @@ stress_test_cases = make_cases(stress_scenarios) @pytest.mark.ethernet @pytest.mark.nightly_run @pytest.mark.parametrize('test_case', test_cases) +@pytest.mark.parametrize('config', ['default'], indirect=True) def test_mqtt_publish(dut: Dut, test_case: Any) -> None: publish_cfg = get_configurations(dut) dut.expect(re.compile(rb'mqtt>'), timeout=30) @@ -223,8 +234,25 @@ def test_mqtt_publish(dut: Dut, test_case: Any) -> None: @pytest.mark.ethernet @pytest.mark.nightly_run @pytest.mark.parametrize('test_case', stress_test_cases) +@pytest.mark.parametrize('config', ['default'], indirect=True) def test_mqtt_publish_stress(dut: Dut, test_case: Any) -> None: publish_cfg = get_configurations(dut) dut.expect(re.compile(rb'mqtt>'), timeout=30) dut.write('init') run_publish_test_case(dut, test_case, publish_cfg) + + +@pytest.mark.esp32 +@pytest.mark.ethernet +@pytest.mark.parametrize('test_case', make_cases(local_broker_scenarios)) +@pytest.mark.parametrize('config', ['local_broker'], indirect=True) +def test_mqtt_publish_lcoal(dut: Dut, test_case: Any) -> None: + if test_case[0] not in local_broker_supported_transports: + pytest.skip(f'Skipping transport: {test_case[0]}...') + dut_ip = dut.expect(r'esp_netif_handlers: .+ ip: (\d+\.\d+\.\d+\.\d+),').group(1) + publish_cfg = get_configurations(dut) + publish_cfg['broker_host_tcp'] = dut_ip + publish_cfg['broker_port_tcp'] = 1234 + dut.expect(re.compile(rb'mqtt>'), timeout=30) + dut.confirm_write('init', expect_pattern='init', timeout=30) + run_publish_test_case(dut, test_case, publish_cfg) diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/sdkconfig.ci.local_broker b/tools/test_apps/protocols/mqtt/publish_connect_test/sdkconfig.ci.local_broker new file mode 100644 index 0000000..0cf44fb --- /dev/null +++ b/tools/test_apps/protocols/mqtt/publish_connect_test/sdkconfig.ci.local_broker @@ -0,0 +1,13 @@ +CONFIG_EXAMPLE_BROKER_SSL_URI="" +CONFIG_EXAMPLE_BROKER_TCP_URI="mqtt://127.0.0.1:1234" +CONFIG_EXAMPLE_BROKER_WS_URI="" +CONFIG_EXAMPLE_BROKER_WSS_URI="" +CONFIG_EXAMPLE_BROKER_CERTIFICATE_OVERRIDE="" +CONFIG_ESP_TLS_INSECURE=y +CONFIG_ESP_TLS_SKIP_SERVER_CERT_VERIFY=y +CONFIG_EXAMPLE_CONNECT_ETHERNET=y +CONFIG_EXAMPLE_CONNECT_WIFI=n +CONFIG_MQTT_USE_CUSTOM_CONFIG=y +CONFIG_MQTT_POLL_READ_TIMEOUT_MS=50 +CONFIG_PARTITION_TABLE_SINGLE_APP_LARGE=y +CONFIG_EXAMPLE_RUN_LOCAL_BROKER=y From cca3d4af9df3b7583d219560b22fb1378434583a Mon Sep 17 00:00:00 2001 From: Euripedes Rocha Date: Wed, 13 Nov 2024 16:15:18 +0100 Subject: [PATCH 207/231] change: Improve mqtt publish connect tests - Add random client id on each iteration - Make timeout configuration dependent on more scenario parameters --- .../publish_connect_test/main/publish_test.c | 9 +++++--- .../pytest_mqtt_publish_app.py | 21 ++++++++++++------- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/main/publish_test.c b/tools/test_apps/protocols/mqtt/publish_connect_test/main/publish_test.c index 3bdefb6..861f917 100644 --- a/tools/test_apps/protocols/mqtt/publish_connect_test/main/publish_test.c +++ b/tools/test_apps/protocols/mqtt/publish_connect_test/main/publish_test.c @@ -14,6 +14,7 @@ #include "freertos/FreeRTOS.h" #include #include "esp_system.h" +#include "esp_random.h" #include "esp_log.h" #include "mqtt_client.h" @@ -24,7 +25,7 @@ static const char *TAG = "publish_test"; static EventGroupHandle_t mqtt_event_group; const static int CONNECTED_BIT = BIT0; - +#define CLIENT_ID_SUFFIX_SIZE 12 #if CONFIG_EXAMPLE_BROKER_CERTIFICATE_OVERRIDDEN == 1 static const uint8_t mqtt_eclipseprojects_io_pem_start[] = "-----BEGIN CERTIFICATE-----\n" CONFIG_EXAMPLE_BROKER_CERTIFICATE_OVERRIDE "\n-----END CERTIFICATE-----"; #else @@ -101,8 +102,6 @@ static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_ } } - - void test_init(void) { ESP_LOGI(TAG, "[APP] Free memory: %d bytes", esp_get_free_heap_size()); @@ -169,6 +168,10 @@ static void configure_client(command_context_t * ctx, const char *transport) ESP_LOGI(TAG, "Set certificate"); config.broker.verification.certificate = (const char *)mqtt_eclipseprojects_io_pem_start; } + // Generate a random client id for each iteration + char client_id[CLIENT_ID_SUFFIX_SIZE] = {0}; + snprintf(client_id, sizeof(client_id), "esp32-%08X", esp_random()); + config.credentials.client_id = client_id; esp_mqtt_set_config(ctx->mqtt_client, &config); } } diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/pytest_mqtt_publish_app.py b/tools/test_apps/protocols/mqtt/publish_connect_test/pytest_mqtt_publish_app.py index 763574c..2a2c0c8 100644 --- a/tools/test_apps/protocols/mqtt/publish_connect_test/pytest_mqtt_publish_app.py +++ b/tools/test_apps/protocols/mqtt/publish_connect_test/pytest_mqtt_publish_app.py @@ -1,5 +1,6 @@ # SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD # SPDX-License-Identifier: Unlicense OR CC0-1.0 + import contextlib import difflib import logging @@ -41,7 +42,8 @@ class MqttPublisher(mqtt.Client): self.event_client_connected = Event() self.event_client_got_all = Event() transport = 'websockets' if self.publish_cfg['transport'] in ['ws', 'wss'] else 'tcp' - super().__init__('MqttTestRunner', userdata=0, transport=transport) + client_id = 'MqttTestRunner' + ''.join(random.choice(string.ascii_uppercase + string.ascii_lowercase) for _ in range(5)) + super().__init__(client_id, userdata=0, transport=transport) def print_details(self, text): # type: (str) -> None if self.log_details: @@ -156,9 +158,14 @@ def get_scenarios() -> List[Dict[str, int]]: def get_timeout(test_case: Any) -> int: transport, qos, enqueue, scenario = test_case - if transport in ['ws', 'wss'] or qos == 2: - return 120 - return 60 + timeout = int(scenario['repeat'] * 10) + if qos == 1: + timeout += 30 + if qos == 2: + timeout += 45 + if transport in ['ws', 'wss']: + timeout += 30 + return timeout def run_publish_test_case(dut: Dut, test_case: Any, publish_cfg: Any) -> None: @@ -172,7 +179,7 @@ def run_publish_test_case(dut: Dut, test_case: Any, publish_cfg: Any) -> None: publish_cfg['transport'] = transport test_timeout = get_timeout(test_case) logging.info(f'Starting Publish test: transport:{transport}, qos:{qos}, nr_of_msgs:{published},' - f' msg_size:{repeat*DEFAULT_MSG_SIZE}, enqueue:{enqueue}') + f' msg_size:{repeat * DEFAULT_MSG_SIZE}, enqueue:{enqueue}') with MqttPublisher(repeat, published, publish_cfg) as publisher, connected_and_subscribed(dut, transport, publisher.sample_string, scenario['len']): msgs_published: List[mqtt.MQTTMessageInfo] = [] dut.write(f'publish {publisher.published} {qos} {enqueue}') @@ -193,13 +200,13 @@ def run_publish_test_case(dut: Dut, test_case: Any, publish_cfg: Any) -> None: try: dut.expect(re.compile(rb'Correct pattern received exactly x times'), timeout=test_timeout) except pexpect.exceptions.ExceptionPexpect: - dut.write(f'publish_report') + dut.write('publish_report') dut.expect(re.compile(rb'Test Report'), timeout=30) raise logging.info('ESP32 received all data from runner') -stress_scenarios = [{'len':20, 'repeat':50}] # many medium sized +stress_scenarios = [{'len':20, 'repeat':30}] # many medium sized transport_cases = ['tcp', 'ws', 'wss', 'ssl'] qos_cases = [0, 1, 2] enqueue_cases = [0, 1] From e8e1ab57bb93bc100eb3de4b3969ba631a5799d5 Mon Sep 17 00:00:00 2001 From: Fu Hanxi Date: Tue, 26 Nov 2024 11:11:48 +0100 Subject: [PATCH 208/231] ci(target-test): support timeout 4h markers --- .../mqtt/publish_connect_test/pytest_mqtt_publish_app.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/pytest_mqtt_publish_app.py b/tools/test_apps/protocols/mqtt/publish_connect_test/pytest_mqtt_publish_app.py index 2a2c0c8..366c56f 100644 --- a/tools/test_apps/protocols/mqtt/publish_connect_test/pytest_mqtt_publish_app.py +++ b/tools/test_apps/protocols/mqtt/publish_connect_test/pytest_mqtt_publish_app.py @@ -238,7 +238,7 @@ def test_mqtt_publish(dut: Dut, test_case: Any) -> None: @pytest.mark.esp32 -@pytest.mark.ethernet +@pytest.mark.ethernet_stress @pytest.mark.nightly_run @pytest.mark.parametrize('test_case', stress_test_cases) @pytest.mark.parametrize('config', ['default'], indirect=True) From efebd8831f6cf79750d2a6eeac258761c4d3a454 Mon Sep 17 00:00:00 2001 From: Euripedes Rocha Date: Thu, 28 Nov 2024 17:35:33 +0100 Subject: [PATCH 209/231] change: Makes topic in mqtt publish test unique for case - Makes each case to subscribe/publish to unique topics - Makes test to wait for the successful subscription on the runner --- .../main/Kconfig.projbuild | 12 -- .../main/publish_connect_test.c | 6 +- .../main/publish_connect_test.h | 4 + .../publish_connect_test/main/publish_test.c | 8 +- .../pytest_mqtt_publish_app.py | 163 +++++++++--------- 5 files changed, 98 insertions(+), 95 deletions(-) diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/main/Kconfig.projbuild b/tools/test_apps/protocols/mqtt/publish_connect_test/main/Kconfig.projbuild index 8e4cfe5..dce9ce2 100644 --- a/tools/test_apps/protocols/mqtt/publish_connect_test/main/Kconfig.projbuild +++ b/tools/test_apps/protocols/mqtt/publish_connect_test/main/Kconfig.projbuild @@ -24,18 +24,6 @@ menu "Example Configuration" help URL of an mqtt broker for wss transport - config EXAMPLE_PUBLISH_TOPIC - string "publish topic" - default "/topic/publish/esp2py" - help - topic to which esp32 client publishes - - config EXAMPLE_SUBSCRIBE_TOPIC - string "subscribe topic" - default "/topic/subscribe/py2esp" - help - topic to which esp32 client subscribes (and expects data) - config EXAMPLE_BROKER_CERTIFICATE_OVERRIDE string "Broker certificate override" default "" diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/main/publish_connect_test.c b/tools/test_apps/protocols/mqtt/publish_connect_test/main/publish_connect_test.c index 23a58a6..d720a6d 100644 --- a/tools/test_apps/protocols/mqtt/publish_connect_test/main/publish_connect_test.c +++ b/tools/test_apps/protocols/mqtt/publish_connect_test/main/publish_connect_test.c @@ -152,6 +152,8 @@ static int do_publish_setup(int argc, char **argv) { command_context.data = calloc(1, sizeof(publish_context_t)); ((publish_context_t*)command_context.data)->pattern = strdup(*publish_setup_args.pattern->sval); ((publish_context_t*)command_context.data)->pattern_repetitions = *publish_setup_args.pattern_repetitions->ival; + ((publish_context_t*)command_context.data)->subscribe_to = strdup(*publish_setup_args.subscribe_to->sval); + ((publish_context_t*)command_context.data)->publish_to = strdup(*publish_setup_args.publish_to->sval); publish_setup(&command_context, *publish_setup_args.transport->sval); return 0; } @@ -210,6 +212,8 @@ void register_common_commands(void) { } void register_publish_commands(void) { publish_setup_args.transport = arg_str1(NULL,NULL,"", "Selected transport to test"); + publish_setup_args.publish_to = arg_str1(NULL,NULL,"", "Selected publish_to to publish"); + publish_setup_args.subscribe_to = arg_str1(NULL,NULL,"", "Selected subscribe_to to publish"); publish_setup_args.pattern = arg_str1(NULL,NULL,"", "Message pattern repeated to build big messages"); publish_setup_args.pattern_repetitions = arg_int1(NULL,NULL,"", "How many times the pattern is repeated"); publish_setup_args.end = arg_end(1); @@ -220,7 +224,7 @@ void register_publish_commands(void) { publish_args.end = arg_end(1); const esp_console_cmd_t publish_setup = { .command = "publish_setup", - .help = "Run publish test\n", + .help = "Set publish test parameters\n", .hint = NULL, .func = &do_publish_setup, .argtable = &publish_setup_args diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/main/publish_connect_test.h b/tools/test_apps/protocols/mqtt/publish_connect_test/main/publish_connect_test.h index 454e82e..c5311ec 100644 --- a/tools/test_apps/protocols/mqtt/publish_connect_test/main/publish_connect_test.h +++ b/tools/test_apps/protocols/mqtt/publish_connect_test/main/publish_connect_test.h @@ -17,6 +17,8 @@ typedef struct { typedef struct { transport_t selected_transport; char *pattern; + char *subscribe_to; + char *publish_to; int pattern_repetitions; int qos; char *expected; @@ -41,6 +43,8 @@ typedef struct { typedef struct { struct arg_str *transport; + struct arg_str *subscribe_to; + struct arg_str *publish_to; struct arg_str *pattern; struct arg_int *pattern_repetitions; struct arg_end *end; diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/main/publish_test.c b/tools/test_apps/protocols/mqtt/publish_connect_test/main/publish_test.c index 861f917..7d11ae3 100644 --- a/tools/test_apps/protocols/mqtt/publish_connect_test/main/publish_test.c +++ b/tools/test_apps/protocols/mqtt/publish_connect_test/main/publish_test.c @@ -46,8 +46,8 @@ static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_ case MQTT_EVENT_CONNECTED: ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED"); xEventGroupSetBits(mqtt_event_group, CONNECTED_BIT); - msg_id = esp_mqtt_client_subscribe(client, CONFIG_EXAMPLE_SUBSCRIBE_TOPIC, test_data->qos); - ESP_LOGI(TAG, "sent subscribe successful %s , msg_id=%d", CONFIG_EXAMPLE_SUBSCRIBE_TOPIC, msg_id); + msg_id = esp_mqtt_client_subscribe(client, test_data->subscribe_to, test_data->qos); + ESP_LOGI(TAG, "sent subscribe successful %s , msg_id=%d", test_data->subscribe_to, msg_id); break; case MQTT_EVENT_DISCONNECTED: @@ -203,9 +203,9 @@ void publish_test(command_context_t * ctx, int expect_to_publish, int qos, bool for (int i = 0; i < data->nr_of_msg_expected; i++) { int msg_id; if (enqueue) { - msg_id = esp_mqtt_client_enqueue(ctx->mqtt_client, CONFIG_EXAMPLE_PUBLISH_TOPIC, data->expected, data->expected_size, qos, 0, true); + msg_id = esp_mqtt_client_enqueue(ctx->mqtt_client, data->publish_to, data->expected, data->expected_size, qos, 0, true); } else { - msg_id = esp_mqtt_client_publish(ctx->mqtt_client, CONFIG_EXAMPLE_PUBLISH_TOPIC, data->expected, data->expected_size, qos, 0); + msg_id = esp_mqtt_client_publish(ctx->mqtt_client, data->publish_to, data->expected, data->expected_size, qos, 0); if(msg_id < 0) { ESP_LOGE(TAG, "Failed to publish"); break; diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/pytest_mqtt_publish_app.py b/tools/test_apps/protocols/mqtt/publish_connect_test/pytest_mqtt_publish_app.py index 366c56f..b2df800 100644 --- a/tools/test_apps/protocols/mqtt/publish_connect_test/pytest_mqtt_publish_app.py +++ b/tools/test_apps/protocols/mqtt/publish_connect_test/pytest_mqtt_publish_app.py @@ -1,6 +1,5 @@ # SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD # SPDX-License-Identifier: Unlicense OR CC0-1.0 - import contextlib import difflib import logging @@ -30,18 +29,17 @@ DEFAULT_MSG_SIZE = 16 # Publisher class creating a python client to send/receive published data from esp-mqtt client class MqttPublisher(mqtt.Client): - def __init__(self, repeat, published, publish_cfg, log_details=False): # type: (MqttPublisher, int, int, dict, bool) -> None - self.sample_string = ''.join(random.choice(string.ascii_uppercase + string.ascii_lowercase + string.digits) for _ in range(DEFAULT_MSG_SIZE)) + def __init__(self, config, log_details=False): # type: (MqttPublisher, dict, bool) -> None self.log_details = log_details - self.repeat = repeat - self.publish_cfg = publish_cfg - self.expected_data = f'{self.sample_string * self.repeat}' - self.published = published + self.config = config + self.expected_data = f'{config["pattern"] * config["scenario"]["msg_len"]}' self.received = 0 + self.subscribe_mid = 0 self.lock = Lock() self.event_client_connected = Event() + self.event_client_subscribed = Event() self.event_client_got_all = Event() - transport = 'websockets' if self.publish_cfg['transport'] in ['ws', 'wss'] else 'tcp' + transport = 'websockets' if self.config['transport'] in ['ws', 'wss'] else 'tcp' client_id = 'MqttTestRunner' + ''.join(random.choice(string.ascii_uppercase + string.ascii_lowercase) for _ in range(5)) super().__init__(client_id, userdata=0, transport=transport) @@ -49,6 +47,12 @@ class MqttPublisher(mqtt.Client): if self.log_details: logging.info(text) + def on_subscribe(self, client: Any, userdata: Any, mid: Any, granted_qos: Any) -> None: + """Verify successful subscription.""" + if mid == self.subscribe_mid: + logging.info(f'Subscribed to {self.config["subscribe_topic"]} successfully with QoS: {granted_qos}') + self.event_client_subscribed.set() + def on_connect(self, mqttc: Any, obj: Any, flags: Any, rc:int) -> None: self.event_client_connected.set() @@ -58,31 +62,29 @@ class MqttPublisher(mqtt.Client): def on_message(self, mqttc: Any, userdata: Any, msg: mqtt.MQTTMessage) -> None: payload = msg.payload.decode('utf-8') if payload == self.expected_data: - userdata += 1 - self.user_data_set(userdata) - self.received = userdata - if userdata == self.published: + self.received += 1 + if self.received == self.config['scenario']['nr_of_msgs']: self.event_client_got_all.set() else: differences = len(list(filter(lambda data: data[0] != data[1], zip(payload, self.expected_data)))) logging.error(f'Payload differ in {differences} positions from expected data. received size: {len(payload)} expected size:' f'{len(self.expected_data)}') - logging.info(f'Repetitions: {payload.count(self.sample_string)}') - logging.info(f'Pattern: {self.sample_string}') - logging.info(f'First : {payload[:DEFAULT_MSG_SIZE]}') - logging.info(f'Last : {payload[-DEFAULT_MSG_SIZE:]}') + logging.info(f'Repetitions: {payload.count(self.config["pattern"])}') + logging.info(f'Pattern: {self.config["pattern"]}') + logging.info(f'First: {payload[:DEFAULT_MSG_SIZE]}') + logging.info(f'Last: {payload[-DEFAULT_MSG_SIZE:]}') matcher = difflib.SequenceMatcher(a=payload, b=self.expected_data) for match in matcher.get_matching_blocks(): logging.info(f'Match: {match}') def __enter__(self) -> Any: - qos = self.publish_cfg['qos'] - broker_host = self.publish_cfg['broker_host_' + self.publish_cfg['transport']] - broker_port = self.publish_cfg['broker_port_' + self.publish_cfg['transport']] + qos = self.config['qos'] + broker_host = self.config['broker_host_' + self.config['transport']] + broker_port = self.config['broker_port_' + self.config['transport']] try: self.print_details('Connecting...') - if self.publish_cfg['transport'] in ['ssl', 'wss']: + if self.config['transport'] in ['ssl', 'wss']: self.tls_set(None, None, None, cert_reqs=ssl.CERT_NONE, tls_version=ssl.PROTOCOL_TLSv1_2, ciphers=None) self.tls_insecure_set(True) self.event_client_connected.clear() @@ -96,7 +98,8 @@ class MqttPublisher(mqtt.Client): if not self.event_client_connected.wait(timeout=30): raise ValueError(f'ENV_TEST_FAILURE: Test script cannot connect to broker: {broker_host}') self.event_client_got_all.clear() - self.subscribe(self.publish_cfg['subscribe_topic'], qos) + result, self.subscribe_mid = self.subscribe(self.config['subscribe_topic'], qos) + assert result == 0 return self def __exit__(self, exc_type, exc_value, traceback): # type: (MqttPublisher, str, str, dict) -> None @@ -104,38 +107,47 @@ class MqttPublisher(mqtt.Client): self.loop_stop() -def get_configurations(dut: Dut) -> Dict[str,Any]: +def get_configurations(dut: Dut, test_case: Any) -> Dict[str,Any]: publish_cfg = {} try: @no_type_check - def get_broker_from_dut(dut, config_option): + def get_config_from_dut(dut, config_option): # logging.info('Option:', config_option, dut.app.sdkconfig.get(config_option)) value = re.search(r'\:\/\/([^:]+)\:([0-9]+)', dut.app.sdkconfig.get(config_option)) if value is None: return None, None return value.group(1), int(value.group(2)) # Get publish test configuration - publish_cfg['publish_topic'] = dut.app.sdkconfig.get('EXAMPLE_SUBSCRIBE_TOPIC').replace('"','') - publish_cfg['subscribe_topic'] = dut.app.sdkconfig.get('EXAMPLE_PUBLISH_TOPIC').replace('"','') - publish_cfg['broker_host_ssl'], publish_cfg['broker_port_ssl'] = get_broker_from_dut(dut, 'EXAMPLE_BROKER_SSL_URI') - publish_cfg['broker_host_tcp'], publish_cfg['broker_port_tcp'] = get_broker_from_dut(dut, 'EXAMPLE_BROKER_TCP_URI') - publish_cfg['broker_host_ws'], publish_cfg['broker_port_ws'] = get_broker_from_dut(dut, 'EXAMPLE_BROKER_WS_URI') - publish_cfg['broker_host_wss'], publish_cfg['broker_port_wss'] = get_broker_from_dut(dut, 'EXAMPLE_BROKER_WSS_URI') + publish_cfg['broker_host_ssl'], publish_cfg['broker_port_ssl'] = get_config_from_dut(dut, 'EXAMPLE_BROKER_SSL_URI') + publish_cfg['broker_host_tcp'], publish_cfg['broker_port_tcp'] = get_config_from_dut(dut, 'EXAMPLE_BROKER_TCP_URI') + publish_cfg['broker_host_ws'], publish_cfg['broker_port_ws'] = get_config_from_dut(dut, 'EXAMPLE_BROKER_WS_URI') + publish_cfg['broker_host_wss'], publish_cfg['broker_port_wss'] = get_config_from_dut(dut, 'EXAMPLE_BROKER_WSS_URI') except Exception: logging.info('ENV_TEST_FAILURE: Some mandatory PUBLISH test case not found in sdkconfig') raise + transport, qos, enqueue, scenario = test_case + if publish_cfg['broker_host_' + transport] is None: + pytest.skip(f'Skipping transport: {transport}...') + publish_cfg['scenario'] = scenario + publish_cfg['qos'] = qos + publish_cfg['enqueue'] = enqueue + publish_cfg['transport'] = transport + publish_cfg['pattern'] = ''.join(random.choice(string.ascii_uppercase + string.ascii_lowercase + string.digits) for _ in range(DEFAULT_MSG_SIZE)) + publish_cfg['test_timeout'] = get_timeout(test_case) + unique_topic = ''.join(random.choice(string.ascii_uppercase + string.ascii_lowercase) for _ in range(DEFAULT_MSG_SIZE)) + publish_cfg['subscribe_topic'] = 'test/subscribe_to/' + unique_topic + publish_cfg['publish_topic'] = 'test/subscribe_to/' + unique_topic logging.info(f'configuration: {publish_cfg}') return publish_cfg @contextlib.contextmanager -def connected_and_subscribed(dut:Dut, transport:str, pattern:str, pattern_repetitions:int) -> Any: - dut.write(f'publish_setup {transport} {pattern} {pattern_repetitions}') - dut.write(f'start') +def connected_and_subscribed(dut:Dut) -> Any: + dut.write('start') dut.expect(re.compile(rb'MQTT_EVENT_SUBSCRIBED'), timeout=60) yield - dut.write(f'stop') + dut.write('stop') def get_scenarios() -> List[Dict[str, int]]: @@ -149,16 +161,16 @@ def get_scenarios() -> List[Dict[str, int]]: continue break if not scenarios: # No message sizes present in the env - set defaults - scenarios = [{'len':0, 'repeat':5}, # zero-sized messages - {'len':2, 'repeat':5}, # short messages - {'len':200, 'repeat':3}, # long messages + scenarios = [{'msg_len':0, 'nr_of_msgs':5}, # zero-sized messages + {'msg_len':2, 'nr_of_msgs':5}, # short messages + {'msg_len':200, 'nr_of_msgs':3}, # long messages ] return scenarios def get_timeout(test_case: Any) -> int: transport, qos, enqueue, scenario = test_case - timeout = int(scenario['repeat'] * 10) + timeout = int(scenario['nr_of_msgs'] * 20) if qos == 1: timeout += 30 if qos == 2: @@ -168,37 +180,33 @@ def get_timeout(test_case: Any) -> int: return timeout -def run_publish_test_case(dut: Dut, test_case: Any, publish_cfg: Any) -> None: - transport, qos, enqueue, scenario = test_case - if publish_cfg['broker_host_' + transport] is None: - pytest.skip(f'Skipping transport: {transport}...') - repeat = scenario['len'] - published = scenario['repeat'] - publish_cfg['qos'] = qos - publish_cfg['queue'] = enqueue - publish_cfg['transport'] = transport - test_timeout = get_timeout(test_case) - logging.info(f'Starting Publish test: transport:{transport}, qos:{qos}, nr_of_msgs:{published},' - f' msg_size:{repeat * DEFAULT_MSG_SIZE}, enqueue:{enqueue}') - with MqttPublisher(repeat, published, publish_cfg) as publisher, connected_and_subscribed(dut, transport, publisher.sample_string, scenario['len']): +def run_publish_test_case(dut: Dut, config: Any) -> None: + logging.info(f'Starting Publish test: transport:{config["transport"]}, qos:{config["qos"]},' + f'nr_of_msgs:{config["scenario"]["nr_of_msgs"]},' + f' msg_size:{config["scenario"]["msg_len"] * DEFAULT_MSG_SIZE}, enqueue:{config["enqueue"]}') + dut.write(f'publish_setup {config["transport"]} {config["publish_topic"]} {config["subscribe_topic"]} {config["pattern"]} {config["scenario"]["msg_len"]}') + with MqttPublisher(config) as publisher, connected_and_subscribed(dut): + assert publisher.event_client_subscribed.wait(timeout=config['test_timeout']), 'Runner failed to subscribe' msgs_published: List[mqtt.MQTTMessageInfo] = [] - dut.write(f'publish {publisher.published} {qos} {enqueue}') - assert publisher.event_client_got_all.wait(timeout=test_timeout), (f'Not all data received from ESP32: {transport} ' - f'qos={qos} received: {publisher.received} ' - f'expected: {publisher.published}') + dut.write(f'publish {config["scenario"]["nr_of_msgs"]} {config["qos"]} {config["enqueue"]}') + assert publisher.event_client_got_all.wait(timeout=config['test_timeout']), (f'Not all data received from ESP32: {config["transport"]} ' + f'qos={config["qos"]} received: {publisher.received} ' + f'expected: {config["scenario"]["nr_of_msgs"]}') logging.info(' - all data received from ESP32') - payload = publisher.sample_string * publisher.repeat - for _ in range(publisher.published): + payload = config['pattern'] * config['scenario']['msg_len'] + for _ in range(config['scenario']['nr_of_msgs']): with publisher.lock: - msg = publisher.publish(topic=publisher.publish_cfg['publish_topic'], payload=payload, qos=qos) - if qos > 0: + msg = publisher.publish(topic=config['publish_topic'], payload=payload, qos=config['qos']) + if config['qos'] > 0: msgs_published.append(msg) logging.info(f'Published: {len(msgs_published)}') while msgs_published: - msgs_published = [msg for msg in msgs_published if msg.is_published()] + msgs_published = [msg for msg in msgs_published if not msg.is_published()] + + logging.info('All messages from runner published') try: - dut.expect(re.compile(rb'Correct pattern received exactly x times'), timeout=test_timeout) + dut.expect(re.compile(rb'Correct pattern received exactly x times'), timeout=config['test_timeout']) except pexpect.exceptions.ExceptionPexpect: dut.write('publish_report') dut.expect(re.compile(rb'Test Report'), timeout=30) @@ -206,35 +214,34 @@ def run_publish_test_case(dut: Dut, test_case: Any, publish_cfg: Any) -> None: logging.info('ESP32 received all data from runner') -stress_scenarios = [{'len':20, 'repeat':30}] # many medium sized +stress_scenarios = [{'msg_len':20, 'nr_of_msgs':30}] # many medium sized transport_cases = ['tcp', 'ws', 'wss', 'ssl'] qos_cases = [0, 1, 2] enqueue_cases = [0, 1] local_broker_supported_transports = ['tcp'] -local_broker_scenarios = [{'len':0, 'repeat':5}, # zero-sized messages - {'len':5, 'repeat':20}, # short messages - {'len':500, 'repeat':10}, # long messages - {'len':20, 'repeat':20}] # many medium sized +local_broker_scenarios = [{'msg_len':0, 'nr_of_msgs':5}, # zero-sized messages + {'msg_len':5, 'nr_of_msgs':20}, # short messages + {'msg_len':500, 'nr_of_msgs':10}, # long messages + {'msg_len':20, 'nr_of_msgs':20}] # many medium sized -def make_cases(scenarios: List[Dict[str, int]]) -> List[Tuple[str, int, int, Dict[str, int]]]: - return [test_case for test_case in product(transport_cases, qos_cases, enqueue_cases, scenarios)] +def make_cases(transport: Any, scenarios: List[Dict[str, int]]) -> List[Tuple[str, int, int, Dict[str, int]]]: + return [test_case for test_case in product(transport, qos_cases, enqueue_cases, scenarios)] -test_cases = make_cases(get_scenarios()) -stress_test_cases = make_cases(stress_scenarios) +test_cases = make_cases(transport_cases, get_scenarios()) +stress_test_cases = make_cases(transport_cases, stress_scenarios) @pytest.mark.esp32 @pytest.mark.ethernet -@pytest.mark.nightly_run @pytest.mark.parametrize('test_case', test_cases) @pytest.mark.parametrize('config', ['default'], indirect=True) def test_mqtt_publish(dut: Dut, test_case: Any) -> None: - publish_cfg = get_configurations(dut) + publish_cfg = get_configurations(dut, test_case) dut.expect(re.compile(rb'mqtt>'), timeout=30) dut.confirm_write('init', expect_pattern='init', timeout=30) - run_publish_test_case(dut, test_case, publish_cfg) + run_publish_test_case(dut, publish_cfg) @pytest.mark.esp32 @@ -243,23 +250,23 @@ def test_mqtt_publish(dut: Dut, test_case: Any) -> None: @pytest.mark.parametrize('test_case', stress_test_cases) @pytest.mark.parametrize('config', ['default'], indirect=True) def test_mqtt_publish_stress(dut: Dut, test_case: Any) -> None: - publish_cfg = get_configurations(dut) + publish_cfg = get_configurations(dut, test_case) dut.expect(re.compile(rb'mqtt>'), timeout=30) dut.write('init') - run_publish_test_case(dut, test_case, publish_cfg) + run_publish_test_case(dut, publish_cfg) @pytest.mark.esp32 @pytest.mark.ethernet -@pytest.mark.parametrize('test_case', make_cases(local_broker_scenarios)) +@pytest.mark.parametrize('test_case', make_cases(local_broker_supported_transports, local_broker_scenarios)) @pytest.mark.parametrize('config', ['local_broker'], indirect=True) -def test_mqtt_publish_lcoal(dut: Dut, test_case: Any) -> None: +def test_mqtt_publish_local(dut: Dut, test_case: Any) -> None: if test_case[0] not in local_broker_supported_transports: pytest.skip(f'Skipping transport: {test_case[0]}...') dut_ip = dut.expect(r'esp_netif_handlers: .+ ip: (\d+\.\d+\.\d+\.\d+),').group(1) - publish_cfg = get_configurations(dut) + publish_cfg = get_configurations(dut, test_case) publish_cfg['broker_host_tcp'] = dut_ip publish_cfg['broker_port_tcp'] = 1234 dut.expect(re.compile(rb'mqtt>'), timeout=30) dut.confirm_write('init', expect_pattern='init', timeout=30) - run_publish_test_case(dut, test_case, publish_cfg) + run_publish_test_case(dut, publish_cfg) From a0f32ae1a0bfc8e7f26ccf5fcc630eca0130c38d Mon Sep 17 00:00:00 2001 From: Marek Fiala Date: Tue, 23 Jul 2024 15:59:09 +0200 Subject: [PATCH 210/231] feat(tools): Enforce utf-8 encoding with open() function --- examples/protocols/mqtt/ssl/pytest_mqtt_ssl.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/examples/protocols/mqtt/ssl/pytest_mqtt_ssl.py b/examples/protocols/mqtt/ssl/pytest_mqtt_ssl.py index c16b89c..d27cc65 100644 --- a/examples/protocols/mqtt/ssl/pytest_mqtt_ssl.py +++ b/examples/protocols/mqtt/ssl/pytest_mqtt_ssl.py @@ -1,11 +1,12 @@ -# SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD +# SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD # SPDX-License-Identifier: Unlicense OR CC0-1.0 import logging import os import re import ssl import sys -from threading import Event, Thread +from threading import Event +from threading import Thread import paho.mqtt.client as mqtt import pexpect @@ -47,7 +48,7 @@ def on_message(client, userdata, msg): # type: (mqtt.Client, tuple, mqtt.client event_client_received_binary.set() return recv_binary = binary + '.received' - with open(recv_binary, 'w') as fw: + with open(recv_binary, 'w', encoding='utf-8') as fw: fw.write(msg.payload) raise ValueError('Received binary (saved as: {}) does not match the original file: {}'.format(recv_binary, binary)) From d23102b47a7c530865bd5b717ed3050d834c980b Mon Sep 17 00:00:00 2001 From: David Cermak Date: Fri, 20 Dec 2024 15:54:12 +0100 Subject: [PATCH 211/231] fix(mqtt): Fix test with local mosquitto increasing stack size 4kB stack size for mosquitto runner task wasn't enough and with recent updates of sock_utils it caused stack overflows --- .../protocols/mqtt/publish_connect_test/main/idf_component.yml | 2 +- .../mqtt/publish_connect_test/main/publish_connect_test.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/main/idf_component.yml b/tools/test_apps/protocols/mqtt/publish_connect_test/main/idf_component.yml index 9eba86b..5d8a7d6 100644 --- a/tools/test_apps/protocols/mqtt/publish_connect_test/main/idf_component.yml +++ b/tools/test_apps/protocols/mqtt/publish_connect_test/main/idf_component.yml @@ -1,7 +1,7 @@ ## IDF Component Manager Manifest File dependencies: espressif/mosquitto: - version: "*" + version: ">=2.0.20" protocol_examples_common: path: ${IDF_PATH}/examples/common_components/protocol_examples_common idf: diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/main/publish_connect_test.c b/tools/test_apps/protocols/mqtt/publish_connect_test/main/publish_connect_test.c index d720a6d..f316439 100644 --- a/tools/test_apps/protocols/mqtt/publish_connect_test/main/publish_connect_test.c +++ b/tools/test_apps/protocols/mqtt/publish_connect_test/main/publish_connect_test.c @@ -316,7 +316,7 @@ void app_main(void) ESP_ERROR_CHECK(esp_netif_init()); ESP_ERROR_CHECK(esp_event_loop_create_default()); #ifdef CONFIG_EXAMPLE_RUN_LOCAL_BROKER - xTaskCreate(broker_task, "broker", 4096, NULL, 4, NULL); + xTaskCreate(broker_task, "broker", 8192, NULL, 4, NULL); #endif ESP_ERROR_CHECK(example_connect()); esp_console_repl_t *repl = NULL; From e06ed89fdfa5eb8cd4dc1c6ed15881286dada826 Mon Sep 17 00:00:00 2001 From: Euripedes Rocha Date: Tue, 10 Dec 2024 15:46:04 +0100 Subject: [PATCH 212/231] fix(mqtt): MQTT5 API header added to documentation MQTT5 API headers were missing. --- docs/en/api-reference/protocols/mqtt.rst | 1 + docs/zh_CN/api-reference/protocols/mqtt.rst | 1 + 2 files changed, 2 insertions(+) diff --git a/docs/en/api-reference/protocols/mqtt.rst b/docs/en/api-reference/protocols/mqtt.rst index b408c3b..b221a92 100644 --- a/docs/en/api-reference/protocols/mqtt.rst +++ b/docs/en/api-reference/protocols/mqtt.rst @@ -205,3 +205,4 @@ API Reference ------------- .. include-build-file:: inc/mqtt_client.inc +.. include-build-file:: inc/mqtt5_client.inc diff --git a/docs/zh_CN/api-reference/protocols/mqtt.rst b/docs/zh_CN/api-reference/protocols/mqtt.rst index 7ad56ec..b2a79f7 100644 --- a/docs/zh_CN/api-reference/protocols/mqtt.rst +++ b/docs/zh_CN/api-reference/protocols/mqtt.rst @@ -205,3 +205,4 @@ API 参考 ------------- .. include-build-file:: inc/mqtt_client.inc +.. include-build-file:: inc/mqtt5_client.inc From 73b773986f830fdc186c014b3b89e3b2a1c55fb5 Mon Sep 17 00:00:00 2001 From: Euripedes Rocha Date: Tue, 21 Jan 2025 13:43:40 +0100 Subject: [PATCH 213/231] fix(mqtt): Update test app certificates Certificates expired, so need to update for testing to pass. --- .../mqtt/publish_connect_test/ca.crt | 37 +++++------ .../mqtt/publish_connect_test/ca.der | Bin 919 -> 783 bytes .../mqtt/publish_connect_test/ca.key | 55 ++++++++-------- .../mqtt/publish_connect_test/client_inv.crt | 34 +++++----- .../publish_connect_test/client_no_pwd.key | 55 ++++++++-------- .../mqtt/publish_connect_test/client_pwd.crt | 32 +++++----- .../mqtt/publish_connect_test/client_pwd.key | 60 +++++++++--------- 7 files changed, 136 insertions(+), 137 deletions(-) diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/ca.crt b/tools/test_apps/protocols/mqtt/publish_connect_test/ca.crt index 894f295..08cde3c 100644 --- a/tools/test_apps/protocols/mqtt/publish_connect_test/ca.crt +++ b/tools/test_apps/protocols/mqtt/publish_connect_test/ca.crt @@ -1,22 +1,19 @@ -----BEGIN CERTIFICATE----- -MIIDkzCCAnugAwIBAgIUNI5wldYysh6rtCzYmda6H414aRswDQYJKoZIhvcNAQEL -BQAwWTELMAkGA1UEBhMCQVUxEzARBgNVBAgMClNvbWUtU3RhdGUxITAfBgNVBAoM -GEludGVybmV0IFdpZGdpdHMgUHR5IEx0ZDESMBAGA1UEAwwJRXNwcmVzc2lmMB4X -DTIwMDEyMTA5MDk0NloXDTI1MDEyMDA5MDk0NlowWTELMAkGA1UEBhMCQVUxEzAR -BgNVBAgMClNvbWUtU3RhdGUxITAfBgNVBAoMGEludGVybmV0IFdpZGdpdHMgUHR5 -IEx0ZDESMBAGA1UEAwwJRXNwcmVzc2lmMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8A -MIIBCgKCAQEAyadSpRnIQBVbEAsbpkrKrOMlBOMIUmA8AfNyOYPLfv0Oa5lBiMAV -3OQDu5tYyFYKwkCUqq65iAm50fPbSH71w1tkja6nZ1yAIM+TvpMlM/WiFGrhY+Tc -kAcLcKUJyPxrv/glzoVslbqUgIhuhCSKA8uk1+ILcn3nWzPcbcowLx31+AHeZj8h -bIAdj6vjqxMCFStp4IcA+ikmCk75LCN4vkkifdkebb/ZDNYCZZhpCBnCHyFAjPc4 -7C+FDVGT3/UUeeTy+Mtn+MqUAhB+W0sPDm1n2h59D4Z/MFm0hl6GQCAKeMJPzssU -BBsRm6zoyPQ4VTqG0uwfNNbORyIfKONMUwIDAQABo1MwUTAdBgNVHQ4EFgQUGYLV -EkgWzxjpltE6texha7zZVxowHwYDVR0jBBgwFoAUGYLVEkgWzxjpltE6texha7zZ -VxowDwYDVR0TAQH/BAUwAwEB/zANBgkqhkiG9w0BAQsFAAOCAQEAb2EF4Zg2XWNb -eZHnzupCDd9jAhwPqkt7F1OXvxJa/RFUSB9+2izGvikGGhuKY4f0iLuqF+bhExD9 -sapDcdFO2Suh4J3onbwEvmKvsv56K3xhapYg8WwPofpkVirnkwFjpQXGzrYxPujg -BPmSy3psQrhvOr/WH7SefJv2qr4ikaugfE+3enY4PL+C1dSQAuNo1QGgWsZIu0c8 -TZybNZ13vNVMA+tgj2CM8FR3Etaabwtu3TTcAnO7aoBTix/bLBTuZoczhN8/MhG3 -GylmDzFI8a6aKxQL3Fi4PsM82hRKWu3gfs39sR1Ci4V22v8uO5EWBPK0QZvDSc1a -KwwxI4zA0w== +MIIDCzCCAfOgAwIBAgIUAndDARsI5XIyIRRc64SttSSYDBgwDQYJKoZIhvcNAQEL +BQAwFDESMBAGA1UEAwwJRXNwcmVzc2lmMCAXDTI1MDEyMzEzMzU1MVoYDzIyOTgx +MTA3MTMzNTUxWjAUMRIwEAYDVQQDDAlFc3ByZXNzaWYwggEiMA0GCSqGSIb3DQEB +AQUAA4IBDwAwggEKAoIBAQCYL5KtnxfhwB/5OG/ILFS4+X59KmLyBvQ0rUz/H/ju +VLHBUb6RVm7EwEifZ1ZTMEdFg2hbSTX1ofSp2XxHdJc5oraszItgjw9nL2j4XdzI +4ox4084Ldcg6ZepH8j70qfInQqNGN8xwXz4Aov6szeM1wSidJpzAO0yYkxTRN77x +CG7InH3Du174EdQfD+Ug8C3HNHzFXudWPAH+vX4gdNRik4H2sIBuD2OMy37BHebn +8brr6KUoKBfIl0tRPYtbeYSCWXgqUFuGT4KJ3WZyr6ITW3bA+xweVFLwA0l9lo1Q +hf4wC231PSitapDEo1hZ5YCCykiC99b8LiM1JBuwXRcxAgMBAAGjUzBRMB0GA1Ud +DgQWBBTRHg79buxy/EuH5KGHz6oP1uo7nzAfBgNVHSMEGDAWgBTRHg79buxy/EuH +5KGHz6oP1uo7nzAPBgNVHRMBAf8EBTADAQH/MA0GCSqGSIb3DQEBCwUAA4IBAQB8 +cYzTD7sggGaboo+x7TZ/kv9XqxUfUkG/tlp/yz6xbL4CHNgN9/pXD+9BITmrw1iz +66+8ptPZyuJys0iO9TRifJWrMv1ZfE1vbsBmuxYOY3MGHN1uJTVpoEIbY2iZ5cSU +ZWzbxLh5QQqpU8GDjxnm4FQdB0F+lZR14yiL0hn+/Sdrc8dnwdD7odFXefae4eoR +plAUu8ojJFOCNUSbJmIp9fyptxRoq8o/YPq1qRwOzPL9eL5NSzoys76oiV6iWH08 +pXIBtNvmBupqU1OJf4yk7eHzu9dcGVdcYUSFuaxcAr59RFvWuzsKOurzkX8oCNtr +oUQpoeBEAoZL9OxoTwuY -----END CERTIFICATE----- diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/ca.der b/tools/test_apps/protocols/mqtt/publish_connect_test/ca.der index c5c3304e23747ca8143d9d92365c99ac752799ff..9a8f4f9a5852b67451a79bce76b1ef9e4ad1131d 100644 GIT binary patch delta 731 zcmbQv-p^KV(8SDb(8Tz80W%XL6O#y2xih0Q$I~JsMUj}-Eo--`%;1qQ;AP{~YV&CO z&dbQi&B|aPVkl%Fz{VWP!py_zT3k?+T3no&W}qO>Yh-F*Xk=_?Y;0<37$w1PWMpY! zXlP~L-*GCQME{S5s{Yv+qUJRtwmBL9R=$c~?NwOUD^*uI#o_4zOV<6X$cgMs@d zhUFbO;4wcvEZD%^wK*f&)AZ}YFDq}>xR*?~T(oV?neK#s{&f9}AF+2%JnE^qe2%;H zgjMP*_fK|TR(?`}TjrljWFKC+|Ik!xk}!RCI+XAeST*&XYq zPAPq?(S1qsU;SV8?Be6;2QU0ycrm>4+q{Rb1eXPf>^`Ne65M3!GFvT4^Xs3L+eI=~ zpR!N*wRNQo-(QZ=-iljZI^s+MW(a?FB_*;n@8JsUPeZ4 zRtAGeLv903Hs(+kHen{mP(xt@K@f+7hbuTgH&r*dB(WsbP|-jhB*?`h;h9>9lVs zq7a^$lAc*otPoIAso+zRVkl%F08+xt!|7UFP?TC+oS9}IC(dhRU|?uuXkckzX<`;7 z&TC|9U}$6j<k210{FgG#sGZ-{6axpbAGBTW89<)^Qgo9|b0JroquTyIt ztFk=i2uiSF{9I(&e7f#0U-nGLjsv21o-ps89dROz>yX2gRqJ+kaPGYL`L;*h*Tc~% zz3Y~z$22IMpS*9fs`1xFB3TcUpWKJ1{dw8gbKC~#FA@;`T4ghg6#_L>(bzF34>wOx86Z*uLNyOO-dW1nCqW=00a#lZ%F z2C{6-p|X4|Vk{z(O;?3H#Li2+oOaP_>zl;vJvYOpz!5L2%pzeR)_`3BKS;kYBjbM- zRs&{6#{bB{&J7NBMuz-E)`v69Vw0mQCq6&-%8B=WGLsDdD(`CX;OYB?qW%hoc*xh? z(mA$IlTAvxE4lqk$L>|)&mIa3{N1?9x$vUjP3?sb=DwJ_hh<;V`c40;v}+QxrYU^P z;a~VGB~0u2WX9yBtjEr6Gqij0faT|;(^WZ6JMyjeUz6W5uV(hQRr{1Cu3k{%zrCu= z!e)Qd)hiR29%o!-To854W4F7F@0{7DbIbQ!^g|KNvwt_rI(4^} c-TJR*JyDG1(-z0shds|mY4aE=_Z+wk03*6t4gdfE diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/ca.key b/tools/test_apps/protocols/mqtt/publish_connect_test/ca.key index d39ad7c..c87923d 100644 --- a/tools/test_apps/protocols/mqtt/publish_connect_test/ca.key +++ b/tools/test_apps/protocols/mqtt/publish_connect_test/ca.key @@ -1,27 +1,28 @@ ------BEGIN RSA PRIVATE KEY----- -MIIEowIBAAKCAQEAyadSpRnIQBVbEAsbpkrKrOMlBOMIUmA8AfNyOYPLfv0Oa5lB -iMAV3OQDu5tYyFYKwkCUqq65iAm50fPbSH71w1tkja6nZ1yAIM+TvpMlM/WiFGrh -Y+TckAcLcKUJyPxrv/glzoVslbqUgIhuhCSKA8uk1+ILcn3nWzPcbcowLx31+AHe -Zj8hbIAdj6vjqxMCFStp4IcA+ikmCk75LCN4vkkifdkebb/ZDNYCZZhpCBnCHyFA -jPc47C+FDVGT3/UUeeTy+Mtn+MqUAhB+W0sPDm1n2h59D4Z/MFm0hl6GQCAKeMJP -zssUBBsRm6zoyPQ4VTqG0uwfNNbORyIfKONMUwIDAQABAoIBAQDEKH7NWcohBGUj -sxp/ZcvH5+FP4qVqtHBLGYyohBsE+Zb4dgl4xBnAWRGEgrYXkxM+KOI1MmgJ/CQF -JujNmarqEVI8PIRdmG6O/D1lKfALnkq+/8Umji87745iUji1iU4rXHEydznMYMYq -TgzrgDu9O3CsDBhElFLktgsbxY2flhRiQ9s/MJTXlfkHYVMbzB+WzbgwglZmzRRB -V9P7GDc1RPs3iUuab2BC0ajWSVWPCIE+WRQ8OTxeSz/Trp0S1y1WtxdMUDhg6wIe -xbTCYF6L6CRjdnnAiaFZuW+ECaeLOyy8sOTtzxIU/8in++x3+CJBaMLsvAG1e2K9 -7OLzz4KZAoGBAObHebhxfC9ue3hGC5IbE7BOaXBBdyHboXvajJKtyqpIDq7QLk2j -ktTuatigrzyCA8GgJUZCuOeWe/JhuzpFAsXN/lVs7ugw/6vQ+RGtJZkxzsYQDQjw -/3f4uWevsj3b28idxdMgstsw12a92pmH3TtKu7mMX2jJhYSu3wqbnCj/AoGBAN+w -/6nH4jLhSobunV+JT3YxwsMdyffLcdwXTcwjJrwGLwNvhPfAGtCCXidneYELCwCF -TbemR5VZadZrCdURWqwyHw8YMHBfN1hArD3MKqck+XK5Spxxr3pfNHvU3YIKWI1l -2h5sKoaPNUTQerLsJh9XG/zyc2Nl88hlFZucTGitAoGAPnKf518eIZ+d3Y/mtaSK -EV1Gfs/YMttBuUgMXeyVOLrC7l77CJtIskHJu9DPWmo8Jfr12ytW6aP46j+z8DKY -a3owZmFRzJbluFKV80iNMgUeVM4nGNJN7uLpGLucWczSjljTHSxt+Y4f23doXb88 -CD1SywTHFI3jiWHgjPhKq3UCgYATBZUoFeRJWVkiEkZ1qlKEhYS/XNgg5z7+bUjj -VBXmJUx4KVKQUti05HEnPqhZbdv4pl1OgahSrfDPF/kVEk24mOaFPRRZae9l5NIA -y0zRO9auh80tcoluiYwH/7j6ZvDSzVd4ANC48pKgEG5uqqAvSBQMNX3UdQX/A4GL -4wWoXQKBgBqtbOYpsoAbLTNV4e7AMZCCKlPYFvRDALbO2E/8MjdaIqBWQSiZBkQ6 -ALFYQaKJOwiLVXVv5B1FHz8MblqDOIaUpaujoA4NxdhYFLh2ui3fzuGTdGiPzOry -QHcIGifGSef45KnrF1LGvGvLMzX7Jx3xnAFOblJikN10wt1MLdNG ------END RSA PRIVATE KEY----- +-----BEGIN PRIVATE KEY----- +MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCYL5KtnxfhwB/5 +OG/ILFS4+X59KmLyBvQ0rUz/H/juVLHBUb6RVm7EwEifZ1ZTMEdFg2hbSTX1ofSp +2XxHdJc5oraszItgjw9nL2j4XdzI4ox4084Ldcg6ZepH8j70qfInQqNGN8xwXz4A +ov6szeM1wSidJpzAO0yYkxTRN77xCG7InH3Du174EdQfD+Ug8C3HNHzFXudWPAH+ +vX4gdNRik4H2sIBuD2OMy37BHebn8brr6KUoKBfIl0tRPYtbeYSCWXgqUFuGT4KJ +3WZyr6ITW3bA+xweVFLwA0l9lo1Qhf4wC231PSitapDEo1hZ5YCCykiC99b8LiM1 +JBuwXRcxAgMBAAECggEABBCAswwAwvfq7rC2/Q5QirJoojqEoxzEDshaWiW6I/Dd +gLmQfLCVk6IESoQTtFPxslnDqqz4UMWtG3BuWxjyPUEDDpdFqWv2tAl6TOhSWEfj +KyqyegBAu00LolVmIKoNiGaFZm+g8Vcc2H7h7P7wcJgGn/hXF0P6LCjA9YO+8PBf +cr8cQrXw9bIVaimqEeFw5QfgotjRAYbhPHQjdM5V4WkH9yD7DsY+zlVy+kvLwAvL +VWlZLhmdkLO/6cjpOyTJ7fQ8woKRFlNGTtvph2s/zHcWgbtRdAdz7fekZ+e6qriq +bQ2qC6FSLnRYfJmZbuIuvBA4Dp90OEY0F2P+srx9IQKBgQDW5qKdHBH2Q+49Kqs1 +wvrpzakztESYONZkRl2v+yxfmGv2Uh52/eG7ox979P9Qj/6LsDkT8BKCVql5QZpo +uCDkwwg8DQUS7xZsr2yY4zFsV5mQgZYtQdH+zgXwyHrAl0MZNS/Y9l+Q63hpDSss +4OZYWpm6UPjtt4LaNra8UvEnoQKBgQC1SnX6UCaVEHWD9bBeJlm0oqdMEaF5c9W0 +MgaU8FcQZGhD6BPAQLfuvuY9nNjE2+NL3rO7PVx8gcw8spPuXqNJuGaDAUiG52ln +l8TWGtZ0cosszG6yPaX58FegIUskBT1/c+3gn6IxeQNlG6KwoFCBcJ9UWGfSFIPj +SLI6wc+FkQKBgQC9nAEeXgX7IzSxA1TMupxobe08kXF0XbHMB8nh29vq/LFFRG9c +zeS9ic/dru0WR/ZviazSyp+KIKynMBrtCoj7TpmVYmd3rTw76QwIWWziCuiTd8Lp +a4m9KEpViI3GH7A0LZlp9PedBiXXiqbtkgGrM0Uv/wGjvKbXoi7ZOtVMAQKBgDaM +I/lf+mvpmuJMn3eEpIMkWaAawfIwN0HTt4VC1394JqMgBilXj8BHMjGhqtt4qehf +JscUzGouB8zPkpEraog48qdCUJc+s8lWsgQV6Sb6fAPLsxbwU6tjdIoa2mgJJ5rc +v9tRTNUD61CVwxrP8ckwoNAZFYvxXkmZyv7A5/bxAoGAD/5J2ktSDgghGJCGsB6z +mi/oSm83jzx2ql6R8eLefqsPcChVF+T/kNmPtU1DUmAMrYn320EJB1qzHzzOvJVv +iw5czIBlp87R8HkqNyt81RU6SDi1QttcYKCyMaGXZ0UL8UwemAZOun3Za9v7g6O9 +k0QiaYa6YfhFOidpdmP3u5I= +-----END PRIVATE KEY----- diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/client_inv.crt b/tools/test_apps/protocols/mqtt/publish_connect_test/client_inv.crt index e5042f3..842ddab 100644 --- a/tools/test_apps/protocols/mqtt/publish_connect_test/client_inv.crt +++ b/tools/test_apps/protocols/mqtt/publish_connect_test/client_inv.crt @@ -1,19 +1,19 @@ -----BEGIN CERTIFICATE----- -MIIDGTCCAgECFBhepE4kbe+jKn9Qa4Fq7o73PL/AMA0GCSqGSIb3DQEBCwUAMIGD -MQswCQYDVQQGEwJDWjEOMAwGA1UECAwFQ3plY2gxDTALBgNVBAcMBEJybm8xEjAQ -BgNVBAoMCUVzcHJlc3NpZjELMAkGA1UECwwCU1cxFDASBgNVBAMMCzE5Mi4xNjgu -Mi4xMR4wHAYJKoZIhvcNAQkBFg9kYXZpZEBjZXJtYWsuY3owHhcNMjAwMTIyMTAy -MDM5WhcNMjkxMTMwMTAyMDM5WjAOMQwwCgYDVQQDDANlc3AwggEiMA0GCSqGSIb3 -DQEBAQUAA4IBDwAwggEKAoIBAQCtphtVYl80g2ldpmNY0NKcmabqWKGRC5PmabDU -wrqErvyOko68fpfw24bO2ndUyXdIgWtf4mbAFmIKajrNrACzMlDtfcZM2uYgJZdn -CYu/NNfDaFEb/ZyHIgkmVskLNxtv8QFTfaNCKK5Fhzz0iaFJnFAuQVX3kXIgOEN2 -8EmNxP53Eb9FeauVX+PBHhwJcGBruvWLPz8bfybHl51b80lCfddbow3z1zqGLESh -AzGc1j/5AEgEgL+iP3QX4qoLKYBgs/olv3rYor8I7oCYfsIpFCcvr/LEoaVYtCJP -PxbGu6KPx3p+r3MjaDStevdhLmzrKr35KczzTpUdOU3wgJA/AgMBAAEwDQYJKoZI -hvcNAQELBQADggEBABVwInNI7NPozIKXsshp6hfM0hchrAozYRT5sZSwusxQJ80t -Xhva0+RnBn3uaYG2YWULu+4QSgzjJ9vdRsFysuNN+7YpihwZrFHwvoRWes5C4ERE -Mzh5xeoZT1VNdz72KHvBytBUwb+h5FZQpfZ9O3yr2wBVHsi4d7/X5LVEmTxLte89 -sxoMGmSbqTKj7/UOfGZjBi2IuiHs1xOZxaZWW7RTgJiz9VGrhSpwb4j6G55HDhYA -4YODy3+epyVXAHVuy4zosQ8CCgdZgN0exB7pEwVQ21zIuyzrcQgQFPGQFbkXH0ow -1mgONA6PZ9YUZftJBmqBZoTVPLLQuE1aKbhBTAA= +MIIC/zCCAeegAwIBAgIUCVMRehYWWgySD1ZEybb7PqXzymgwDQYJKoZIhvcNAQEL +BQAwDjEMMAoGA1UEAwwDZXNwMCAXDTI1MDEyMzEzMzU1MVoYDzIyOTgxMTA3MTMz +NTUxWjAOMQwwCgYDVQQDDANlc3AwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEK +AoIBAQCwutiHYegFgY0SqnO9HbjeMDRyaDZNpUzwd2BXBaTDuW3eB39iz3Q9DfXJ +YPnwk1BQG4ZA/rslxLnEQaJNmXWQuGxXhscbXogRVNUJPXChjm5f3izoYQVwcq9h +5BM+3hQmojBMWUb0SMcTAU1OXqTKm83a8KnxROpSU2qb8X5SZvZ7OyjV3DBp0jBU +LQXzv92hxEtQiRCcnOb8/JF9hyrHVwvMHyxLosEgNErIOYFo2qqG8wiGd0aErggV +C8nAh95xgR7Dv8x7+/AkLNo5Fve6r4/ZHWkLPxVssLv4H4r6+UKKMMWHcbhw++uY +jEvUxldMcP8QK7KCVrlmigLiGNbPAgMBAAGjUzBRMB0GA1UdDgQWBBQ00vOWljyf +7m4oAq00nevWptMfTDAfBgNVHSMEGDAWgBQ00vOWljyf7m4oAq00nevWptMfTDAP +BgNVHRMBAf8EBTADAQH/MA0GCSqGSIb3DQEBCwUAA4IBAQBLjeV1MFVbyiRrHpJw +mvnQ/GdGBxCioMw9sPhMPhT8fbW6y3gFdKytZ/HAEyt6+zysoG9bA1DvFJZ+cCD9 +pfRg/lr+ifrPXU7mYDou1ofabOT5ItoSuFDFIGW+HkZbkS3II/S569BdvwfAHlyg +I81rNfz0mp3Htz6Cn44nMbe/8Yn5OFwTynOReSlqxqRsxIFpL33MRQNA40RoK7hj +Pe3En1KOtRe9rDliSm8jrtI6gQZMBjfl24AZq3ZZRSS7V0TOqKl3JNkZ9/S1ImPL +/OaGdwRcQMvYv2YCm3wnbqce2drOOVmmuUNGWGgbrehCRxTjgGoES5nkfXSoGGzM +EB/9 -----END CERTIFICATE----- diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/client_no_pwd.key b/tools/test_apps/protocols/mqtt/publish_connect_test/client_no_pwd.key index 0d9d016..ad3254f 100644 --- a/tools/test_apps/protocols/mqtt/publish_connect_test/client_no_pwd.key +++ b/tools/test_apps/protocols/mqtt/publish_connect_test/client_no_pwd.key @@ -1,27 +1,28 @@ ------BEGIN RSA PRIVATE KEY----- -MIIEogIBAAKCAQEAraYbVWJfNINpXaZjWNDSnJmm6lihkQuT5mmw1MK6hK78jpKO -vH6X8NuGztp3VMl3SIFrX+JmwBZiCmo6zawAszJQ7X3GTNrmICWXZwmLvzTXw2hR -G/2chyIJJlbJCzcbb/EBU32jQiiuRYc89ImhSZxQLkFV95FyIDhDdvBJjcT+dxG/ -RXmrlV/jwR4cCXBga7r1iz8/G38mx5edW/NJQn3XW6MN89c6hixEoQMxnNY/+QBI -BIC/oj90F+KqCymAYLP6Jb962KK/CO6AmH7CKRQnL6/yxKGlWLQiTz8Wxruij8d6 -fq9zI2g0rXr3YS5s6yq9+SnM806VHTlN8ICQPwIDAQABAoIBAA4G8wJMtgAZ9XL5 -M+FCzSCVUORxUGvVEZd1RjGJoWOCdyhVMm6Lk16DfTpMb4NL2vTib3gJY990b2sD -9cgTcuMG1363wEMJE7nZD4flP4KslBlW3eZy8CgCWdbc/9SGGRNL1p2V8pAvlRRM -vmHKlFrL47Y41ObwutVbdievdWGcPBdF/sjpzskemVrEIPg7knHJ+MyxuIsjyaLV -Xdf3RR8yqtRggl+EP6m7JP4mbdlb/key8JTOHhe135JbuIwjUKTvm7UI+r9HwrDF -/4ysd6uEqAq4vD73VJ+7UR1LSWJ1hqoVyNP96QWPsReID/PB8TxbCPNOMX8WKz7U -WXOVomECgYEA1+54rF8QT/cpuqxR3ChbPaRGfwform0/nXZY7Xna4SqifQ2/gd6S -I97cktKCQzsnxqxrq8lV8kBlVBnqYWib+546WNjez32DUTsSu/0cIat649izbhw/ -a9piOttup3OSAmc6LZGgPtLB/Jhupl9F25SbwAxnYyUUSomqK4zSOvkCgYEAzd8O -MNZUGHREjD43UuoM+qyMa1vanZk+0mcNt6cL4MTKVHNyEPdCOg01QcAGTH5pumbs -mxU4yEPGn6RylQuXDi1UwFBsPLQTDRCnbjWkYBSNY3qKD0MlN+UIQ6/zUMaYHVL8 -xEfvJKxMiMdU4FhgDqoCTsK7BjnJ/bzgEdrHevcCgYAS7pOh+UvC1xbPiSA8P0WQ -qACOTrE16do0AhZV6+Mm7sgEUtpBlrQVdQq9zLsjDeK05pUiIKrqbH712rfUBon2 -i67t70XJx2VmD9napZx7zz8dDvjcZJmi6SjHpEmVYOqiT06ohCYam/vqG6tH5v6G -/AaT1gKSjMO0rVFAND6ScQKBgF459ZjMwHjg3m8CGvhMP9yMFUkeJZV0iphqqpCg -WINsDt9QZ6j0Qs+nM/UAGuHwChxS94CT2gVvX/25mug1AdJvVRcguCmgkges07VR -wAZp4bziXUZXCTXoEjxI0CjsfLsPPLnp4r76TZ1c/rAgQvbzQVMjNc7HrHgCdtw1 -MpBJAoGAEdeW0t5kHrHHYlFqwUHYnwD6DbNl4TzTumZrI+uEkrH1EAQ4BpIk5Gwx -OOWw8m+x2AMtBk+XcElMGZbX98msB2KcHbQMuxLF3xbCB5ihm6sKxk0HLvf2kygP -k+DYz710yKq9zRwoGOeM52oTrA4hIpQKBnpjCXg9QigD0yoWOjI= ------END RSA PRIVATE KEY----- +-----BEGIN PRIVATE KEY----- +MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQC0Xu8EHu21sQSv +Z7qqg3Ae7Qt1LYwhOOv4U970gbUNH/1ZMDO3X4Fow2xiw2tk1YiMI0tdPot2IPMT +wTpVGYbQWXIKPCocYhmdZNBbKYGNq8ScMBY9DzzPxaul3uRcY9PHU3XdD/GKWL3G +Mc/ldMNbYTL3hZO6CJRxo+YMsLUkeFWqeYPS8v7TJr1EwgMJVXJVCsrqoP9uzmro +GFt8pDBZxsgAjyrZVTgoAP2bN8H3uRYKMEuad/g/iggwrNgymMrSFvL859DqP1ri +Y03UKFRr4FHw5WpYNn8KYR9GxDOPr9jwTdOUt7+8sqOmJ6LU25qVvZhb0dSPhOAS +YWDe0J59AgMBAAECggEAB+7m/1zm4qdrB9X9oVNPoqSVbO14bNumkVELDOEJmA6w +o3QZyjZyqCt0QHHM7u3vaA6DWZIPKz5uwo8spCKPyektOWMFDSOJceFFwsS0FIma +B66bw8rAR3kvwAdqLuAbJ/i1RehWHNojWqzFMpfsob/IMJfY9AU9ljP5wdQh2302 +jEk4/8VrZPJX+3f4Lr8O7SlHgYMe/UIr0eRxse5nuYjbNfbL0Fh6YHM2+cFFu1dK +RLya5vn05L++2wyTV6+0EvLpRSgwdyAHURKH929MacUl6fJsGK2gsJHGjPa5Dck+ +bQWlQ2wBZkCm3sZrP9JarxHkTDQOyZe3gPsxogHI0QKBgQDXGsDUy1svBnisvPdv +ynRF29KYZWULrcscesOVHlkas/3rpBXJVqdo8PAJTzkYdFXtiJz2YVrD9CvMNzki +quttabGYDdED3VlNPhyucXGcTWJkSSDGy8EUGmMCDqdd9XAo4qcDIFtlbLd2wz/2 +uDdODCM2EC3YyUUaXEGCPRUj6QKBgQDWqazB8naJePQktRbwnLAXAYcAwowmKlRX +FKEOcz51nTUDAqPxBIpPMwRk3lWnjJ8vblvm6V+lpPeDno8pXJlyqimQv3H0lo62 +SU08Ua7IuIfQhEIJikNLkcQyvBtpbdECZxQpIIr+rh36iVtkVKDhFPyIYyTlBnWs +KRLkm9VtdQKBgG+apuABXp8IeYYOSUdMQIWaZyrAuOuVUWPGYkG1TD/pkV44dUFQ +X224TKJX42THv2mNzvqaSnCO+EZthIxzJ9FN3GHrET4zE0lbZ6AupeaV0+36bzIV +t2jJxALmPXonhPkBl+usVx9Jh+rrpP1KWQ96hq5fOJpY2yyxHyAGtzg5AoGAAaTv +E9U9nzPz+swATvuERcjXNeRHmos8diZMTYYIdnIr08gTZWc5f/3ODM+/LDdhiMV5 +or0q5GPtGW7/fbud2UkAZIFy6AK3OSI6mzms8xCmyawZrWUHuu506EaUlywmK7SK +bP440laXGDIxJwWW7eJxRMItqCtyf00ZPfMLoXkCgYEAggEgT6w7FDDbbpbRdf7E +SIB45KiKLiJ/DBDJcTD6/AfqdVFmwooWadoOl/faQ7cp7rp03pdugEo+uzrVHBLp +373I05QZxNvyCwnb9YW5I4tZN9hZFh9nEG8NcbBaJvh0VmYEDEC3hjjpg3dJw0Lq +0l1V14UAqjelaR3byPlJ84Y= +-----END PRIVATE KEY----- diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/client_pwd.crt b/tools/test_apps/protocols/mqtt/publish_connect_test/client_pwd.crt index 50c291a..3cb8afe 100644 --- a/tools/test_apps/protocols/mqtt/publish_connect_test/client_pwd.crt +++ b/tools/test_apps/protocols/mqtt/publish_connect_test/client_pwd.crt @@ -1,18 +1,18 @@ -----BEGIN CERTIFICATE----- -MIIC7jCCAdYCFDLr5uidhcbJKmiKv+FoJ2lyh2ZkMA0GCSqGSIb3DQEBCwUAMFkx -CzAJBgNVBAYTAkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEwHwYDVQQKDBhJbnRl -cm5ldCBXaWRnaXRzIFB0eSBMdGQxEjAQBgNVBAMMCUVzcHJlc3NpZjAeFw0yMDAx -MjIxMDA1MThaFw0yOTExMzAxMDA1MThaMA4xDDAKBgNVBAMMA2VzcDCCASIwDQYJ -KoZIhvcNAQEBBQADggEPADCCAQoCggEBAK2mG1ViXzSDaV2mY1jQ0pyZpupYoZEL -k+ZpsNTCuoSu/I6Sjrx+l/Dbhs7ad1TJd0iBa1/iZsAWYgpqOs2sALMyUO19xkza -5iAll2cJi78018NoURv9nIciCSZWyQs3G2/xAVN9o0IorkWHPPSJoUmcUC5BVfeR -ciA4Q3bwSY3E/ncRv0V5q5Vf48EeHAlwYGu69Ys/Pxt/JseXnVvzSUJ911ujDfPX -OoYsRKEDMZzWP/kASASAv6I/dBfiqgspgGCz+iW/etiivwjugJh+wikUJy+v8sSh -pVi0Ik8/Fsa7oo/Hen6vcyNoNK1692EubOsqvfkpzPNOlR05TfCAkD8CAwEAATAN -BgkqhkiG9w0BAQsFAAOCAQEAxRyJBo1GP5K5BFvJDeXcAJjUbzFUqY4pUsbodkIZ -NJj69ZulxARISKsqaVhjR0m4CXZMRVhimRUJJBnjgTYidWF/xsbxgmEoURqV9SAL -puYMOm3t9Fi/81iQSzvT6ZLq5bQmxCFpZ6jl+nM28KqqldhRJApgXLOvc9i/N1FW -7ceZPw33vfzKxk91rVl9vv+wBLS/jeF++6wscIeFW+HjDj2F84JEENltoEpQz4Y1 -VWYJ2G5Fu0MaxObwMrEqIxKAq7X96pyIocO6vINg0UWNp3kbPBSzD1i5FLqx96fQ -kfykJJN1kUsrJk1PskxnWq2vckdSv+jLOiF7hoHRG7+69A== +MIIC9DCCAdygAwIBAgIUIhvazGcN2HKAutmxJrQOrMinqKgwDQYJKoZIhvcNAQEL +BQAwFDESMBAGA1UEAwwJRXNwcmVzc2lmMCAXDTI1MDEyMzEwMDIxMFoYDzIyOTgx +MTA3MTAwMjEwWjAOMQwwCgYDVQQDDANlc3AwggEiMA0GCSqGSIb3DQEBAQUAA4IB +DwAwggEKAoIBAQCZvxt/oSXjVQcy3e378SkPqcA/D6CkawY4GBrV7Veu53Yq5NIV +I++Us6P2NR+Eha/qMl981G1oHyrOfFQ4Bcugk/BeI6l7G2r6AbQ1r3rLuapdBLRe +wsdVdKN/MkuSq83C3Kt1T9P6naC2AHOOoGCiIlq0G6saR73DtJ1ESP03zQ6F7lyK ++savlg5/WUuaqf0rdWZU3fj8iDJhiS4SGg2GAK/k73TK20bBMN1McRMWBDl/wHzh +Xsh9qM5L2hxQOQmGu4K2cOZZo1SDBIm8ihIhKwFXMKc8CvoZu+KqA+nyE2pQf4H1 +MnF3QPEV2MGsXWa0Tu87Ou/A9Bo3g5VPjGwlAgMBAAGjQjBAMB0GA1UdDgQWBBQP +TGAuOu6rOwuUepgT+LWcZSU2PDAfBgNVHSMEGDAWgBSQUHU5QKmWEo55dLL0/+K/ +wQ9SBjANBgkqhkiG9w0BAQsFAAOCAQEAbNqcHGJ0OI2HfxZMdgxfjhy1eqU5AWZV +XmPFSNPbmzdXtzcmpKCrVIB5ghhCFTP0C7FqB++Jk/d65uBZMXiCBqC5bi9ZW8pV +TmOQVHZ6TrJKJkRMrXIk3lMrTM8vkEMQKn2n3JRwEJ92jDF1LXplAOy34R0E0b54 +BkILOefO5nuPetPhfpSoKFUs2pGCxx4o/hcdZ9jX8S8dobRqAcO4Xbe+JBYycUYU +Fz26uYnnoOQrnR4K+j/hNQd+WS47iFJo9DN0xDwmoJhBW0KYtlS7wAnBqHLYS99R +HDjhYgSNw/bSJ1EYpCqjLhNWekvQr86CoCi+3gKpPRqNvl+Gewxg7A== -----END CERTIFICATE----- diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/client_pwd.key b/tools/test_apps/protocols/mqtt/publish_connect_test/client_pwd.key index ce98682..c24b0db 100644 --- a/tools/test_apps/protocols/mqtt/publish_connect_test/client_pwd.key +++ b/tools/test_apps/protocols/mqtt/publish_connect_test/client_pwd.key @@ -1,30 +1,30 @@ ------BEGIN RSA PRIVATE KEY----- -Proc-Type: 4,ENCRYPTED -DEK-Info: AES-256-CBC,6FFF58C7A652F8E0D7E31F0CCAA27E98 - -ZUeJ8iCLa7bDf8xQsL3NtDK/oR2z8EWapoqGru/zOvxAeHWoLdMko1bouMkEXn+h -g4GigyXo9f3jdyFKeqf9TtXonL6vguWczFc+81ynE5w3iqqcmdP8SD8k3o07lBc/ -hfRIwsg4Ywda3luQBWT63LIx7GnQjHRjK/KkPkFkYG6LDwshWyf1L7NxRMCrkejA -3B7KqlRTEia9xR0WH//ZGOdipp2MSyUGC46BgXJQ1rDJo1D+3NUTZ8RmMkqZNmJK -z3HMMflB4khmh2HkdM10VaErpiphitP52aTbGu/aschDOvSHFYMNf916u8UlyGQJ -4m/pgSz+aAfReGRDqCcojkAf/BR4+n7QYfFtxjJ5P4ul4ZnhB30synknet6+9bKU -Ht1SkavyJmruW2sfIcvPadLVm+xROnGr3ZWs82BGdPPu4wIYRWBn4S7huELXSSC2 -qLCTWeLB+pV//+9E9EmrvwsTJBNMCkjAmmTdmofm8UisAGsZlvyIDJVlpUsUKOoU -cHGc2x9hj5OhgK++0JOY2ftowFX69FNRYNYHB5Vw6/5Teo3eGxVYoMeosADo7pXa -j17g7dywDLRBZuTfIkFcYyfRV4A/xcagZJ8m/rnSDphGHYkM1d+MoXAPhj6A+iAa -2qZkPyYdd0OkWvCY42F2xsjxzYjmxvkkNLDiYa7zj3ofb/1mqdNc8/k2AbFObX4v -LenDFyqmzeYfzHcJ2Z4edwx1TFFMlHb+knuYjSzcBceWoQ5nOi+uipgZVJEEKC8o -vTUBeaDnRblNIGlvuWnZFN0OHV1BduF5TyKUh2YGBw/X4qgCgrnCR2I8HGZMkUNe -UTDF9vpxnDke5BEhzkwb2R34Kvn7ac1p4NDdXOrCwEqZTt93FgQTgOuklPb39CcZ -yQHfl1Vr0EgfOlcE+ngbUrU93/qsEADXyjM8D4ZGB5+tQZGWlz9nc8PqldC3txux -Hce4bk98hOgZUy/fEozE6fODxBJCuIi2ab/6WU8j62UAIumw7fvgti+zor6eJdas -QCS+UVj5fJtYc5E/7Urih7Ixkltu0atqR5p81xn8U94/6KGYUCcsaZ7UZnWfD4dF -0qEkdHravD82Z38eBpt/kqIE7bHs3orvt7WSCBYZQtkdQysYy+lXEAwF3eIpCqAW -EcdHZ10zENvSHfzot1yK2rk9uuD+KqyK80xCSh9auLBSdJHCNeMtaozpCg5jLhYu -mW4zSQA4awuD58k/+xAnOzXtNCPg6TPQ7GNzX/33W9R2ceRtkbNmd68YEmkZ7tZo -s/3DAuxjid5W+wbsJHBvnUQh9g5BMF18k0SYS3bFPy62eycQziGfEQIwcgaHqAt5 -SX4URYqDLN0k+68CAQfm2JAS8kOBdo9TAKsluThp/uRC1oh3DW/D0xctDAApDbdd -oNnK44XSYfMsBaGjm+NFQYTndFDpSd4+eQcVWAaXTdyRGMC6jbc9XijdzJnnKbEP -M38+5dUaZFPyONieb7VfciH1WKM0GfxHAFxpiEgXd0dwS8Jqq9sLY6drKZP5+b9a -9KMXI3jBLVqPg9yvx8E7THjF9ihsheVhEcJ6Nywhopab9Et+wvFa9upl2TJ2xGIj ------END RSA PRIVATE KEY----- +-----BEGIN ENCRYPTED PRIVATE KEY----- +MIIFNTBfBgkqhkiG9w0BBQ0wUjAxBgkqhkiG9w0BBQwwJAQQfHxbCamtiRIZL9h4 +OAg27gICCAAwDAYIKoZIhvcNAgkFADAdBglghkgBZQMEASoEEP/5zadqi1x7zmHL +iKcmesMEggTQyAi0Ef1gl3xAZN2++wPZYOLaiFoQUmvq+lujIL8rjuVKjlyYEnXJ +WbR59x1SEqNPy99NsPqyvjdJP5hrA+HcGFfPJFhYj3D3h+0hCaGWNyhRUrC0nbdZ +LvoOiLV+HWkHWkpq9tcnC4wVZXxrYxifVD22uOcLNKDFz5H3eli1yM/VBGoS2iJZ +A/cBIbQ5/D3/1Q5/URf/6Cc/HE/y6wtRjnLcOXwK5ULjg2yMyOOrFSwmSkL8OPkf +8L9iIYGHmO9njRqNrjEuUQN+oS+bHO8pGvvBQxiS3ePQIP3b4gviaDCLs77B9bKO +9tSVYMGKSTh1ymBd6SF89uPdbDWpkljXw5pQ6kGix0CAR5oA3gvFKxR27+dSMJFD +GJjHXihQHozhTpnkOpGdQqIDrcNsGewdAELprCFpjgBrcRhzqRhWiKF3gyhv2s0L +YFWAb517Cf7ZYwsG90weuhD2emMmbxs9668MK0Gv+LSNs/WbbN/OQDVhhjfsZ235 +f3t5PZK+COrqG6LpaaGPTCFzWErLibQBVbnn+bfcjx9RneQeD65zv9Jw894xniYa +lOYkZNEZUuWoI3C7Q3wzphS2dRnPD1+AT6yMkvJMbb93KwHkhEmH2EoNiVjaqBer +ZsyTWkfP4W/OduGqGJrwAxCmcGq6xm9vomxzK/7Xdty190EEDMF0w5SsEtv7Zo4i +ckzx+ndqyFnpSvg05vSzf+5Og/X7G5uMbVanCIJ1lN5/W43LqF7OqqROTqJlrxZi +s6NFJuRyAdqQbldQi2UYXC+SR4px+ZAVOCkR6HfPIKvYWjHvoQlGF4bqtCB8eTML +ZfoArc6urDQemXE8vchM6YXxXAHPjE2jM/yhpzQhj25rmyNjb5WwAr0lZRb813Fu +IXiwQc6+UeXiBzm0KmJC4YcKlF49QR6M9UMuUJO7ceIjrCVummrq1/gvGPpzAVaP +i9kU81yEhfZYyESPZgwuZa2TdPAn4CXHTO1CqftrUHeine0bTPKPtOdZrX+UZiGR +njtw9DnZRmEmz2Ra4fwHrlB5RRSghaG5MaXvyERjo33q8nylECjG3jznyys4mxAc +y+pMd0VXnDrtgRKx7PvCOCmtLKuGg+T4bDoOIf/DswjWTrtKtceGfel5hGEusiuA +lsL0I6IJETFDDi9NeDzpnQTD4t6kjMIgs9b+TqN5ophjWEPjfn3IorM51loMemAs +ceTxqua5HT+exWKs0NCurIjE3CUu3WMDCPZLJzUiRbR9p9se4KMkOKduDy7++RMo +8yA+UlDwIVR2IcJBLs1I1i/Rh7qqNnvL/4YnNYOe2fi965l4FShPnw/Ghf6tUngz +U5GvKvknGxIvnX0jhr67y+3zmsb+ErK+3id64fB5jXKGgENkp3J31Amni5SgoWI1 +mGbv64VYleWljr8iKBX5cgE2VH1HSTOcfgABJB21lIg3XwzwGZ+eWLSE9wdR34hM +VtfNJQCcmEBm+ruoqw2OkddbiAZAgrW8NX5dIwm34lkCF9UG/eR85OkSqUF5JZlx +mlc7SpmmlmcEU+l+BMFdKKDlwRNnJNLIyciF0cMGtWlYRZ9oSatOMDm5ev6pGRuQ +tdL+gYAyro9s7wz7WCOz/JmUKJk8MWvYt9ECsIj8aVd/QXK8+M4f8ZE= +-----END ENCRYPTED PRIVATE KEY----- From aeb84de1a00d0172ce47e8c319f4b1e9c6325763 Mon Sep 17 00:00:00 2001 From: Euripedes Rocha Date: Tue, 28 Jan 2025 14:34:29 +0100 Subject: [PATCH 214/231] fix(mqtt): Regenerate certificates for testing - Previous fix ommited one of the client certificates by mistaque. - This regenerates all certificates to clean that up. --- .../mqtt/publish_connect_test/ca.crt | 34 +++++------ .../mqtt/publish_connect_test/ca.der | Bin 783 -> 783 bytes .../mqtt/publish_connect_test/ca.key | 52 ++++++++-------- .../mqtt/publish_connect_test/client_inv.crt | 34 +++++------ .../publish_connect_test/client_no_pwd.key | 52 ++++++++-------- .../mqtt/publish_connect_test/client_pwd.crt | 32 +++++----- .../mqtt/publish_connect_test/client_pwd.key | 56 +++++++++--------- 7 files changed, 130 insertions(+), 130 deletions(-) diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/ca.crt b/tools/test_apps/protocols/mqtt/publish_connect_test/ca.crt index 08cde3c..f53f647 100644 --- a/tools/test_apps/protocols/mqtt/publish_connect_test/ca.crt +++ b/tools/test_apps/protocols/mqtt/publish_connect_test/ca.crt @@ -1,19 +1,19 @@ -----BEGIN CERTIFICATE----- -MIIDCzCCAfOgAwIBAgIUAndDARsI5XIyIRRc64SttSSYDBgwDQYJKoZIhvcNAQEL -BQAwFDESMBAGA1UEAwwJRXNwcmVzc2lmMCAXDTI1MDEyMzEzMzU1MVoYDzIyOTgx -MTA3MTMzNTUxWjAUMRIwEAYDVQQDDAlFc3ByZXNzaWYwggEiMA0GCSqGSIb3DQEB -AQUAA4IBDwAwggEKAoIBAQCYL5KtnxfhwB/5OG/ILFS4+X59KmLyBvQ0rUz/H/ju -VLHBUb6RVm7EwEifZ1ZTMEdFg2hbSTX1ofSp2XxHdJc5oraszItgjw9nL2j4XdzI -4ox4084Ldcg6ZepH8j70qfInQqNGN8xwXz4Aov6szeM1wSidJpzAO0yYkxTRN77x -CG7InH3Du174EdQfD+Ug8C3HNHzFXudWPAH+vX4gdNRik4H2sIBuD2OMy37BHebn -8brr6KUoKBfIl0tRPYtbeYSCWXgqUFuGT4KJ3WZyr6ITW3bA+xweVFLwA0l9lo1Q -hf4wC231PSitapDEo1hZ5YCCykiC99b8LiM1JBuwXRcxAgMBAAGjUzBRMB0GA1Ud -DgQWBBTRHg79buxy/EuH5KGHz6oP1uo7nzAfBgNVHSMEGDAWgBTRHg79buxy/EuH -5KGHz6oP1uo7nzAPBgNVHRMBAf8EBTADAQH/MA0GCSqGSIb3DQEBCwUAA4IBAQB8 -cYzTD7sggGaboo+x7TZ/kv9XqxUfUkG/tlp/yz6xbL4CHNgN9/pXD+9BITmrw1iz -66+8ptPZyuJys0iO9TRifJWrMv1ZfE1vbsBmuxYOY3MGHN1uJTVpoEIbY2iZ5cSU -ZWzbxLh5QQqpU8GDjxnm4FQdB0F+lZR14yiL0hn+/Sdrc8dnwdD7odFXefae4eoR -plAUu8ojJFOCNUSbJmIp9fyptxRoq8o/YPq1qRwOzPL9eL5NSzoys76oiV6iWH08 -pXIBtNvmBupqU1OJf4yk7eHzu9dcGVdcYUSFuaxcAr59RFvWuzsKOurzkX8oCNtr -oUQpoeBEAoZL9OxoTwuY +MIIDCzCCAfOgAwIBAgIUN9fSo2J8makY6P64QfrO+EcLLm0wDQYJKoZIhvcNAQEL +BQAwFDESMBAGA1UEAwwJRXNwcmVzc2lmMCAXDTI1MDEyODEzMjkxMVoYDzIyOTgx +MTEyMTMyOTExWjAUMRIwEAYDVQQDDAlFc3ByZXNzaWYwggEiMA0GCSqGSIb3DQEB +AQUAA4IBDwAwggEKAoIBAQDBJj+nzMM/xbd/fvF8vWPL9ZxZU45LaaxZHLGOldAz +Z77roxxKSfyZUpNbn9pn3qwVBuYEG6GnfKmikf6QFAfEriSVrqugRw383H3CkLxf +riVT0So+Y69wgbwcSURjTkvNn1HNzgsgFESr+wzwUTn5gc7bNrys3ZchoyKLSabZ +qV+jUwUSrWeQcF4kqVeSTp5kPBxD2SotYHP1hBbKATamYMKN0Khj3v91mmACO+Ss +AY31Riej1mY05h58vU/VmZW10FFb/EMZMNJkIkZgc6u298PxVLpyC446X6uzLehs +V2EDgCAYUQW191j/7Z8kcWSfo96nqP7uHx/2fz2qlLCzAgMBAAGjUzBRMB0GA1Ud +DgQWBBQn5CyOO5LXQ60QRxTRaHZeFdNrVjAfBgNVHSMEGDAWgBQn5CyOO5LXQ60Q +RxTRaHZeFdNrVjAPBgNVHRMBAf8EBTADAQH/MA0GCSqGSIb3DQEBCwUAA4IBAQAq +hKN1jrogQLY7AtnUaXb7eKFwI7k9NR6BZeXmE+xGCz02sUHNpzjuwo2oRacDZHZY +oEuwEM+zzWGV4x6cKriNUJ0G9uJwfFkCBZfWaNhyZ+r+3DqL4pH5kCcbQ3ZM+Mfk +dBOr9BUs7q4yZHRngmsi5lff0K6GC/uKC8bd9AKNs9I11g7CnJL2arf/GlZJrvTg +Lk7H7MT65SsAeX2MhifWl0urb20PNjDooQaki3qtApCgrUaIAtTUAP9p3jf8H0RT +rkfoVBxQtTDZltA+xelilUHcmj7pM9105/U6n0hD4yRQgemqaeSCbEo4ST2zm0pE +B4mjbFam7oBWdXdEDF1p -----END CERTIFICATE----- diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/ca.der b/tools/test_apps/protocols/mqtt/publish_connect_test/ca.der index 9a8f4f9a5852b67451a79bce76b1ef9e4ad1131d..309539220877cfc621e7497288868677881ea46b 100644 GIT binary patch delta 650 zcmeBY>t|CoXkz9zXkz@lfSHMriAlu#`lZE5H8WRAy!f}n@z=Q@?%aC06P5MEEewr~ zEDa5#B>0VtEG-NT4UND86EiI9531QOKXcgr==S=$k2QOfPk)^g8QkZcxh7I(W8c&Z z#_9WBFP8E0{4+CXa`gOL>G#%%vOQywUbwtw<)VrICWx>fS*J2}-RcGIynpW09-6Qx zew}LYMJ>DJ^#zT4WISDx{k+f44?KI0TS3HS^>3aJftEiT&)qiLv*zw}#l=e9p3835 zuZ&+D%qp}teL_K;%F6Ibe)CdnWSnnm=_V9^Z4o=gXtpfjQ168m$@l)3&PrgiezJzK z_p6)w;%jLp&*W8k0 zW|YN=Ud|3PkY{5Kl~rbuFc51H!KPsHD@N6Nt(L{5eY+GKwplaXypmb=yJBI1@=jY* zxyIC|&xGH&aod`0bUeG<;@zR%6|T#fQ_3P1cyACmzxiz9)W>pjw086c%w_xbsGug2 ziFNw5j2lJium0V!>V7ow=LB_W=Q5uk$Dfo4ul^#c^KP9{N=bTCw$iik`xn-=asTe( zK6dvDQ*Zs|OQzTO4$YbLEo=LKsW8uVUmocB9e;D=*HdkV%G#bb^=s3;SLf&Qn;E=V z$hM@rYAw@*1#8_pn66x5_@8;t{Exg#@H+PwAu<754Q@`mV0ZLo(p1Mgv+Q0P-z|Cm s)oQ+n^JA5O#+R!ypETuoS$NuRp6%tr-nlp@Y}vbpu+nlD9;Vn#06SPQBme*a delta 650 zcmeBY>t|CoXkz9zXkz@lfSHMriAjX1+?i3D<7ttRqDai^mbF_|X7ET%RMrzWHZ(Ri zH8qTq;5Ra|v@kR@Fb4}v%&@GVp+9NueDQ||&SmzK``B zc_-%79^M`IL-2|`|5Jqzy2njwj>bI?vtj(Vw@#ttO48)UZyOr&_>+51*Bz97_Wa|n z*Dsc8Xo#Pf?j2~`9bMVd6j`Aa5Z&hA)Ok0pX#FDL=&}R9W#mGFJ}`UMPU{V5{b#_P z`_)!sZPtV%iz6bRHZ+~`X!?HbkDjuriu8t9CUL{b3mHXBFUs-#&3jYy$GiQ>!uIp4 z_^-XPo^K$}#vCfE%pzeR)*ymS!Q@wrs`WL6J(u}+D>S6dUev$wty%q~|KY1e<%1me zZ;PrwZMQLJACt@t-tWJ{`QJM#TCP4EvHA7-Jz|d|MKV^OvQPN6 zb)^j7nNNQ!_W62S8ExLTqBCw$M6J!zBE~JZpRv8l3J&h9?^*Kp;pg4gVoquTXD@WoT=>9+sm=S#n+$&@?im1h0yFLa diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/ca.key b/tools/test_apps/protocols/mqtt/publish_connect_test/ca.key index c87923d..8a0404f 100644 --- a/tools/test_apps/protocols/mqtt/publish_connect_test/ca.key +++ b/tools/test_apps/protocols/mqtt/publish_connect_test/ca.key @@ -1,28 +1,28 @@ -----BEGIN PRIVATE KEY----- -MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCYL5KtnxfhwB/5 -OG/ILFS4+X59KmLyBvQ0rUz/H/juVLHBUb6RVm7EwEifZ1ZTMEdFg2hbSTX1ofSp -2XxHdJc5oraszItgjw9nL2j4XdzI4ox4084Ldcg6ZepH8j70qfInQqNGN8xwXz4A -ov6szeM1wSidJpzAO0yYkxTRN77xCG7InH3Du174EdQfD+Ug8C3HNHzFXudWPAH+ -vX4gdNRik4H2sIBuD2OMy37BHebn8brr6KUoKBfIl0tRPYtbeYSCWXgqUFuGT4KJ -3WZyr6ITW3bA+xweVFLwA0l9lo1Qhf4wC231PSitapDEo1hZ5YCCykiC99b8LiM1 -JBuwXRcxAgMBAAECggEABBCAswwAwvfq7rC2/Q5QirJoojqEoxzEDshaWiW6I/Dd -gLmQfLCVk6IESoQTtFPxslnDqqz4UMWtG3BuWxjyPUEDDpdFqWv2tAl6TOhSWEfj -KyqyegBAu00LolVmIKoNiGaFZm+g8Vcc2H7h7P7wcJgGn/hXF0P6LCjA9YO+8PBf -cr8cQrXw9bIVaimqEeFw5QfgotjRAYbhPHQjdM5V4WkH9yD7DsY+zlVy+kvLwAvL -VWlZLhmdkLO/6cjpOyTJ7fQ8woKRFlNGTtvph2s/zHcWgbtRdAdz7fekZ+e6qriq -bQ2qC6FSLnRYfJmZbuIuvBA4Dp90OEY0F2P+srx9IQKBgQDW5qKdHBH2Q+49Kqs1 -wvrpzakztESYONZkRl2v+yxfmGv2Uh52/eG7ox979P9Qj/6LsDkT8BKCVql5QZpo -uCDkwwg8DQUS7xZsr2yY4zFsV5mQgZYtQdH+zgXwyHrAl0MZNS/Y9l+Q63hpDSss -4OZYWpm6UPjtt4LaNra8UvEnoQKBgQC1SnX6UCaVEHWD9bBeJlm0oqdMEaF5c9W0 -MgaU8FcQZGhD6BPAQLfuvuY9nNjE2+NL3rO7PVx8gcw8spPuXqNJuGaDAUiG52ln -l8TWGtZ0cosszG6yPaX58FegIUskBT1/c+3gn6IxeQNlG6KwoFCBcJ9UWGfSFIPj -SLI6wc+FkQKBgQC9nAEeXgX7IzSxA1TMupxobe08kXF0XbHMB8nh29vq/LFFRG9c -zeS9ic/dru0WR/ZviazSyp+KIKynMBrtCoj7TpmVYmd3rTw76QwIWWziCuiTd8Lp -a4m9KEpViI3GH7A0LZlp9PedBiXXiqbtkgGrM0Uv/wGjvKbXoi7ZOtVMAQKBgDaM -I/lf+mvpmuJMn3eEpIMkWaAawfIwN0HTt4VC1394JqMgBilXj8BHMjGhqtt4qehf -JscUzGouB8zPkpEraog48qdCUJc+s8lWsgQV6Sb6fAPLsxbwU6tjdIoa2mgJJ5rc -v9tRTNUD61CVwxrP8ckwoNAZFYvxXkmZyv7A5/bxAoGAD/5J2ktSDgghGJCGsB6z -mi/oSm83jzx2ql6R8eLefqsPcChVF+T/kNmPtU1DUmAMrYn320EJB1qzHzzOvJVv -iw5czIBlp87R8HkqNyt81RU6SDi1QttcYKCyMaGXZ0UL8UwemAZOun3Za9v7g6O9 -k0QiaYa6YfhFOidpdmP3u5I= +MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQDBJj+nzMM/xbd/ +fvF8vWPL9ZxZU45LaaxZHLGOldAzZ77roxxKSfyZUpNbn9pn3qwVBuYEG6GnfKmi +kf6QFAfEriSVrqugRw383H3CkLxfriVT0So+Y69wgbwcSURjTkvNn1HNzgsgFESr ++wzwUTn5gc7bNrys3ZchoyKLSabZqV+jUwUSrWeQcF4kqVeSTp5kPBxD2SotYHP1 +hBbKATamYMKN0Khj3v91mmACO+SsAY31Riej1mY05h58vU/VmZW10FFb/EMZMNJk +IkZgc6u298PxVLpyC446X6uzLehsV2EDgCAYUQW191j/7Z8kcWSfo96nqP7uHx/2 +fz2qlLCzAgMBAAECggEAA5+hkxbS1OfzLNenVSVCcYmekMhyA3UskBFrPVcPLF+2 +Y7pQJg6kSAYWEKJ7mPZC+TQDbbFNA5Tho9SkV3Sgvq5brlk0DBI5L3S9gUBw4ESm +m59xcPEy0eQFMOyo9Un7PxTNlpRWfhgTP7y6femwMj+er1skCAQyOSXT2JQdpriv +5BnQquVSeoPbFuuObxerG878BDvMEIFzkZGmjWHhWD9P3+2sHcU6R6aZexRnkMrw +A8gagVzIi35QPDtWYLWcybdghJW7BdW8Lsx7HvMzRvNlBxMFnqtzNspuXEyZVlXF +J7cMpktiMQ+dNk18Utq9n19Y5ZWWi5g+XY3V0dHs3QKBgQDgrmdRWD3lLwY0equ6 +/kk6Pf+GwgRLTVQy4DfUHPFCUY3+fZgYTL7InLjgcLynSdcHZ6hXjdIsAxwgeD6U +eBjjAhIyRNDTrSD91NQb99kmjSB9PRnPsERFiol5mR/JGUIs67y1a9oGPvhXW/9f +nu5jIacqF5sGZhmVmssYc/yXbQKBgQDcEqcKF+0WxHSNPMBtSFBOf/Uz6cT79lLS +1EoMiIjhQM1TrnRPqOicRegHmW7dWvtnsReprW6/mjz7t8C2kQm0Mf2UAtEm2Hzu +XxHM1Jmj1/oNc5Gzv5PhDMY8XEafzogj2uwL9++On74XNTkt20hl7qM4lcibu86U +f67cuOK0nwKBgQC3e7XEKFvjndNjaAp2WtNSTO8wDaGUHUJ1icYN4tTjY9ahzc83 +iPUEv3f7UhW+R/7ifTRsy9SnPKLroUb52Fn6iOZzRt+C/g/DOts9O9qKMRYnMI6z +nS8j208JjpL2lzoDlUA2qC0UjLgiH28dl5z2N7VcGorvXtHl6tOQ1KeiwQKBgQCI +kq7FDcodyfdGuz/z0d+8h55FBDsx3lDR89qsYoMHvy5tUyNtWDZa3Os85BQwHMlO +NVGpBC9piq9zyzo2UlYCVM+4bfMcN1d4mtkyE3HxgxP0CxeNxENic4oGZYGSpRpJ +ng/E8a3iBfJy9p2wfpg0Yd87O4EOXIO8Fm0PH1HZowKBgQCBmYxw+gv7zbY6ejg1 +prJsVvaVp5D8jhZ8agqRseWpeUEIrTzACNZ8L+loFMwsAIm5piLMziso1b6ktDmk +A9c7eOOh/kec8T4LXhIglLL49kmb0ZOCMotG1n9+1n/ZuMV8bMNTGWof5QjOMMKE +tC0yOkTvoIqHMbwnLrLLMOxh1w== -----END PRIVATE KEY----- diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/client_inv.crt b/tools/test_apps/protocols/mqtt/publish_connect_test/client_inv.crt index 842ddab..76f576e 100644 --- a/tools/test_apps/protocols/mqtt/publish_connect_test/client_inv.crt +++ b/tools/test_apps/protocols/mqtt/publish_connect_test/client_inv.crt @@ -1,19 +1,19 @@ -----BEGIN CERTIFICATE----- -MIIC/zCCAeegAwIBAgIUCVMRehYWWgySD1ZEybb7PqXzymgwDQYJKoZIhvcNAQEL -BQAwDjEMMAoGA1UEAwwDZXNwMCAXDTI1MDEyMzEzMzU1MVoYDzIyOTgxMTA3MTMz -NTUxWjAOMQwwCgYDVQQDDANlc3AwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEK -AoIBAQCwutiHYegFgY0SqnO9HbjeMDRyaDZNpUzwd2BXBaTDuW3eB39iz3Q9DfXJ -YPnwk1BQG4ZA/rslxLnEQaJNmXWQuGxXhscbXogRVNUJPXChjm5f3izoYQVwcq9h -5BM+3hQmojBMWUb0SMcTAU1OXqTKm83a8KnxROpSU2qb8X5SZvZ7OyjV3DBp0jBU -LQXzv92hxEtQiRCcnOb8/JF9hyrHVwvMHyxLosEgNErIOYFo2qqG8wiGd0aErggV -C8nAh95xgR7Dv8x7+/AkLNo5Fve6r4/ZHWkLPxVssLv4H4r6+UKKMMWHcbhw++uY -jEvUxldMcP8QK7KCVrlmigLiGNbPAgMBAAGjUzBRMB0GA1UdDgQWBBQ00vOWljyf -7m4oAq00nevWptMfTDAfBgNVHSMEGDAWgBQ00vOWljyf7m4oAq00nevWptMfTDAP -BgNVHRMBAf8EBTADAQH/MA0GCSqGSIb3DQEBCwUAA4IBAQBLjeV1MFVbyiRrHpJw -mvnQ/GdGBxCioMw9sPhMPhT8fbW6y3gFdKytZ/HAEyt6+zysoG9bA1DvFJZ+cCD9 -pfRg/lr+ifrPXU7mYDou1ofabOT5ItoSuFDFIGW+HkZbkS3II/S569BdvwfAHlyg -I81rNfz0mp3Htz6Cn44nMbe/8Yn5OFwTynOReSlqxqRsxIFpL33MRQNA40RoK7hj -Pe3En1KOtRe9rDliSm8jrtI6gQZMBjfl24AZq3ZZRSS7V0TOqKl3JNkZ9/S1ImPL -/OaGdwRcQMvYv2YCm3wnbqce2drOOVmmuUNGWGgbrehCRxTjgGoES5nkfXSoGGzM -EB/9 +MIIC/zCCAeegAwIBAgIUCpDNnOm701EkM8UghGHxDAXcVWEwDQYJKoZIhvcNAQEL +BQAwDjEMMAoGA1UEAwwDZXNwMCAXDTI1MDEyODEzMjkxMloYDzIyOTgxMTEyMTMy +OTEyWjAOMQwwCgYDVQQDDANlc3AwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEK +AoIBAQDVLhyorBPIbIS5hi7BCyv+UsrEAr17zfMu31zrj65wrRI2NPjeT/4UXTHB +WLXevsBmxngqa1HfBeJiwbjr0HNt7OAAIePnW0v0qfED6S6SG3ySbL7wOTQiUeZE +JiDmcjBiu3ttBimOeRAYl2RAHVrVzV01ByQ8QZZ5FFynBl6mOnu0lGSwImDCNoVk +g+zDNf9vbZWGUwvjxBoFmjvEdIEVh58c2Bu3Vzlblb/XR4XnQb0hR18VzHTibHdz +UKRDDzgoz1CSThmsNSJxOesS8e2wufHyqBQ1uXW2kJDIHwq+Ew7RkyhPLx6hXhN6 +QS99SN5Sbgpucqc8pjJYFv3oHevXAgMBAAGjUzBRMB0GA1UdDgQWBBT5Ja5qUdUf +QRdQBur2UvcJmBmFVzAfBgNVHSMEGDAWgBT5Ja5qUdUfQRdQBur2UvcJmBmFVzAP +BgNVHRMBAf8EBTADAQH/MA0GCSqGSIb3DQEBCwUAA4IBAQBrVzCP7OoyVZyZ80+c ++B7Cz1vWmNsyCLHVSuUqr3DuvJa0V71VdMGvr+zfR2/jLW7nGy2I+5d3Yaxs/AIZ +GF4XuDfuiJXjRDgFY8Xh+PgM7L4AxVvml8ZWaj9kurbIWKcsizCygoL4ckIA10QO +wAg7Au1RZT6iDeGW47inz/bP8V/sQnFmupTPN4VipMCUo61c+L0tnKPGbMySnmGq +mf9f/luAh/pgiW/HRjA4bppE5hCxUB8F5wyzAzFMnRVSYptpZdj/AYyAooV7Iit0 +Ewjtrnxlr7zhmw4p32FuRDNU5AzbQMxDS4delGHMrNtehmSLNeZ3V7tAyG7EmJZd +y4YP -----END CERTIFICATE----- diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/client_no_pwd.key b/tools/test_apps/protocols/mqtt/publish_connect_test/client_no_pwd.key index ad3254f..d359ca2 100644 --- a/tools/test_apps/protocols/mqtt/publish_connect_test/client_no_pwd.key +++ b/tools/test_apps/protocols/mqtt/publish_connect_test/client_no_pwd.key @@ -1,28 +1,28 @@ -----BEGIN PRIVATE KEY----- -MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQC0Xu8EHu21sQSv -Z7qqg3Ae7Qt1LYwhOOv4U970gbUNH/1ZMDO3X4Fow2xiw2tk1YiMI0tdPot2IPMT -wTpVGYbQWXIKPCocYhmdZNBbKYGNq8ScMBY9DzzPxaul3uRcY9PHU3XdD/GKWL3G -Mc/ldMNbYTL3hZO6CJRxo+YMsLUkeFWqeYPS8v7TJr1EwgMJVXJVCsrqoP9uzmro -GFt8pDBZxsgAjyrZVTgoAP2bN8H3uRYKMEuad/g/iggwrNgymMrSFvL859DqP1ri -Y03UKFRr4FHw5WpYNn8KYR9GxDOPr9jwTdOUt7+8sqOmJ6LU25qVvZhb0dSPhOAS -YWDe0J59AgMBAAECggEAB+7m/1zm4qdrB9X9oVNPoqSVbO14bNumkVELDOEJmA6w -o3QZyjZyqCt0QHHM7u3vaA6DWZIPKz5uwo8spCKPyektOWMFDSOJceFFwsS0FIma -B66bw8rAR3kvwAdqLuAbJ/i1RehWHNojWqzFMpfsob/IMJfY9AU9ljP5wdQh2302 -jEk4/8VrZPJX+3f4Lr8O7SlHgYMe/UIr0eRxse5nuYjbNfbL0Fh6YHM2+cFFu1dK -RLya5vn05L++2wyTV6+0EvLpRSgwdyAHURKH929MacUl6fJsGK2gsJHGjPa5Dck+ -bQWlQ2wBZkCm3sZrP9JarxHkTDQOyZe3gPsxogHI0QKBgQDXGsDUy1svBnisvPdv -ynRF29KYZWULrcscesOVHlkas/3rpBXJVqdo8PAJTzkYdFXtiJz2YVrD9CvMNzki -quttabGYDdED3VlNPhyucXGcTWJkSSDGy8EUGmMCDqdd9XAo4qcDIFtlbLd2wz/2 -uDdODCM2EC3YyUUaXEGCPRUj6QKBgQDWqazB8naJePQktRbwnLAXAYcAwowmKlRX -FKEOcz51nTUDAqPxBIpPMwRk3lWnjJ8vblvm6V+lpPeDno8pXJlyqimQv3H0lo62 -SU08Ua7IuIfQhEIJikNLkcQyvBtpbdECZxQpIIr+rh36iVtkVKDhFPyIYyTlBnWs -KRLkm9VtdQKBgG+apuABXp8IeYYOSUdMQIWaZyrAuOuVUWPGYkG1TD/pkV44dUFQ -X224TKJX42THv2mNzvqaSnCO+EZthIxzJ9FN3GHrET4zE0lbZ6AupeaV0+36bzIV -t2jJxALmPXonhPkBl+usVx9Jh+rrpP1KWQ96hq5fOJpY2yyxHyAGtzg5AoGAAaTv -E9U9nzPz+swATvuERcjXNeRHmos8diZMTYYIdnIr08gTZWc5f/3ODM+/LDdhiMV5 -or0q5GPtGW7/fbud2UkAZIFy6AK3OSI6mzms8xCmyawZrWUHuu506EaUlywmK7SK -bP440laXGDIxJwWW7eJxRMItqCtyf00ZPfMLoXkCgYEAggEgT6w7FDDbbpbRdf7E -SIB45KiKLiJ/DBDJcTD6/AfqdVFmwooWadoOl/faQ7cp7rp03pdugEo+uzrVHBLp -373I05QZxNvyCwnb9YW5I4tZN9hZFh9nEG8NcbBaJvh0VmYEDEC3hjjpg3dJw0Lq -0l1V14UAqjelaR3byPlJ84Y= +MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCQ8NP82CE2OJaZ +A07nbHyfhOdv/dnZXcemxFtoiEw1v29t5pldxBPUwy9/eg85+z1H1CTuFfHj2pzK +yyB1eJojTtQlJRZn1cZJ/0/LZu7naNACsndXmQcc0b6Cvn/jOuiqvAqFgLknLdEl +Iun2NSgeot3lafXy/yuLbKWHRDzsZIEpu/1iUlGx6UAQTBL+HVj2VM3hejdt33gn +PwdS+otmv+2A4ZBhbrL7EmtDJLM1wISo5PRp3YSknLJc8PdH5DQE5kaxh8RN/MDe +cm7kTeNeL2eN+nj9HvDUJ2yVF2P3ZaW6RtCwFlj0E3VXfKUNz2yTIowDtSkDeG7T +zjZ8V5cnAgMBAAECggEAB6ZeWfMCVb1I3YyaJobIURcPrA6G5/0WI4wrkunWs+OF +uxQ3tgY6Ubl3kU/c69+BzX+570MDQFZyfhWYgfq6j/CCK++4LgTPcWpjSMYHB/m1 +ON84g0wVXwUG/BmX4GNkklZYa9FudE3rwv0DwjXuByfrdEdSbt/e6X3zgp8sZAGZ +oJnIWVvGxqIS1EvPHWkYlmDEnfNk1iYaQyNuYDxCxwwrLdyvhrsh1eiiGbabN7UK +CtqHgpZXXW/3hvCRJkl6EJVLXbegMEt73hN73Mn++iX0MVEDd+GfuEmojbM5rTpz +6CjD//CW43m9mJqq4Vx7QyZVQ9wJg9P4AvrD2gw9GQKBgQDJw+ThwWmmixXnsLxN +CpYqk77cWogTSEzyKnWYUMfiHUdCOVVEYmG4gnrC5NOuoNU2735rwDslUrGVpFRW +lPD8Ocptq6pbtKPlUVucJQVlgfrD2XWd4RuBhEX8mL6lGfTiW82wLeS7QgfDPhnh +YtBA+FASdZtsW34ce4+u6nbQqwKBgQC35qk5vXelRkp3gQpzfPLdmvR0UsSkEdD1 +DvEjixGm5VcNRCdRlxeYCPNt4ADe0EGLKQn+oCwIvrAz95ZUjcAy+nLQJA++AyGi +X8TmHhmpJS82Jo2H7h7ZFCBuep+xb4a77DnpK8feYBH7MgL1skwFOJEq3k4Iv40Y +Uydzc/irdQKBgE58q86+RHEK7eyBLd7yXVQWwUpK1WBa4cPajIBB/F+TcCLs1qSR +eN4js8mY5leqLQb/xzf3QYrVTkud70j3C3+yo3JiMxUVgiQ0r+Rf6meAVqJVN5HA +/cg2Qltut8rV/BMKD0uXrsDBgO+MetjYbZa4gb2MjndqZ/aAgnZSswGdAoGAN2Cv +PuLuH4feLsRd+E7893yM9mZiLqHq0ZCxTqm3JBHVt/n4+RnNkgvH+iNFP9NomVY/ +WzyyZeO+Pbflvgp9gRxn7IOfdfGNdE7whPc1dPjskZrkbbVn9qiX54znf8/8u6Q0 +ACId6rn1UDZMK96IA9534HfW+c1s2JFZxOt8S20CgYBtV6D3iPRzaaDJy7J2Csyu +p+4nIeudRfVmzf2VRQOFqyULWNU35I2tBjq5rzN1yAmJCgE2kW1Z4iyfmLhBrF51 +Wm3OY0R8Q5FvO9CCpL6XaZokEyWIcfEqjHKIIHf7lHeC4fs7YplRaxm7n1vV8hHK +UsP/rg6j/qZP+/WKNhsoBg== -----END PRIVATE KEY----- diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/client_pwd.crt b/tools/test_apps/protocols/mqtt/publish_connect_test/client_pwd.crt index 3cb8afe..f83eba5 100644 --- a/tools/test_apps/protocols/mqtt/publish_connect_test/client_pwd.crt +++ b/tools/test_apps/protocols/mqtt/publish_connect_test/client_pwd.crt @@ -1,18 +1,18 @@ -----BEGIN CERTIFICATE----- -MIIC9DCCAdygAwIBAgIUIhvazGcN2HKAutmxJrQOrMinqKgwDQYJKoZIhvcNAQEL -BQAwFDESMBAGA1UEAwwJRXNwcmVzc2lmMCAXDTI1MDEyMzEwMDIxMFoYDzIyOTgx -MTA3MTAwMjEwWjAOMQwwCgYDVQQDDANlc3AwggEiMA0GCSqGSIb3DQEBAQUAA4IB -DwAwggEKAoIBAQCZvxt/oSXjVQcy3e378SkPqcA/D6CkawY4GBrV7Veu53Yq5NIV -I++Us6P2NR+Eha/qMl981G1oHyrOfFQ4Bcugk/BeI6l7G2r6AbQ1r3rLuapdBLRe -wsdVdKN/MkuSq83C3Kt1T9P6naC2AHOOoGCiIlq0G6saR73DtJ1ESP03zQ6F7lyK -+savlg5/WUuaqf0rdWZU3fj8iDJhiS4SGg2GAK/k73TK20bBMN1McRMWBDl/wHzh -Xsh9qM5L2hxQOQmGu4K2cOZZo1SDBIm8ihIhKwFXMKc8CvoZu+KqA+nyE2pQf4H1 -MnF3QPEV2MGsXWa0Tu87Ou/A9Bo3g5VPjGwlAgMBAAGjQjBAMB0GA1UdDgQWBBQP -TGAuOu6rOwuUepgT+LWcZSU2PDAfBgNVHSMEGDAWgBSQUHU5QKmWEo55dLL0/+K/ -wQ9SBjANBgkqhkiG9w0BAQsFAAOCAQEAbNqcHGJ0OI2HfxZMdgxfjhy1eqU5AWZV -XmPFSNPbmzdXtzcmpKCrVIB5ghhCFTP0C7FqB++Jk/d65uBZMXiCBqC5bi9ZW8pV -TmOQVHZ6TrJKJkRMrXIk3lMrTM8vkEMQKn2n3JRwEJ92jDF1LXplAOy34R0E0b54 -BkILOefO5nuPetPhfpSoKFUs2pGCxx4o/hcdZ9jX8S8dobRqAcO4Xbe+JBYycUYU -Fz26uYnnoOQrnR4K+j/hNQd+WS47iFJo9DN0xDwmoJhBW0KYtlS7wAnBqHLYS99R -HDjhYgSNw/bSJ1EYpCqjLhNWekvQr86CoCi+3gKpPRqNvl+Gewxg7A== +MIIC9DCCAdygAwIBAgIUcPhqC08YW+4RYTuVucv4UXLd2kQwDQYJKoZIhvcNAQEL +BQAwFDESMBAGA1UEAwwJRXNwcmVzc2lmMCAXDTI1MDEyODEzMjkxMloYDzIyOTgx +MTEyMTMyOTEyWjAOMQwwCgYDVQQDDANlc3AwggEiMA0GCSqGSIb3DQEBAQUAA4IB +DwAwggEKAoIBAQCQ8NP82CE2OJaZA07nbHyfhOdv/dnZXcemxFtoiEw1v29t5pld +xBPUwy9/eg85+z1H1CTuFfHj2pzKyyB1eJojTtQlJRZn1cZJ/0/LZu7naNACsndX +mQcc0b6Cvn/jOuiqvAqFgLknLdElIun2NSgeot3lafXy/yuLbKWHRDzsZIEpu/1i +UlGx6UAQTBL+HVj2VM3hejdt33gnPwdS+otmv+2A4ZBhbrL7EmtDJLM1wISo5PRp +3YSknLJc8PdH5DQE5kaxh8RN/MDecm7kTeNeL2eN+nj9HvDUJ2yVF2P3ZaW6RtCw +Flj0E3VXfKUNz2yTIowDtSkDeG7TzjZ8V5cnAgMBAAGjQjBAMB0GA1UdDgQWBBRT +WBz6pzNCJ5iMbbxr26mwLic+5zAfBgNVHSMEGDAWgBQn5CyOO5LXQ60QRxTRaHZe +FdNrVjANBgkqhkiG9w0BAQsFAAOCAQEAYOfwiCz+pzl9uE0WMOYU0NlSmOchN05m +CInOXuU7d38+QGjbnjs5IFmvquf8Ink6Z9hJitpPWHCN6rudT2BbFzzFQVmckMke +rbTSxUgyG/Xy4YgvTz49MUlK2fZc0Uun20Fu3vnCWvEPEYAfFdaUfegXtdFkI31J +U6EiwwMPzdlX6qLc2h3nKjm5yURvhSlFhKI1DMDhHF0wi09WH0T3CufLY3AQ2npK +4Vg3lP4F5Dh3FsaVj0h7BWhd4RfE6oyHkxRRgTfLuANdv6wOt2IR5OZAPedTGD9e +W0F0EyQXxsKhfqBeZsKpcYGRPWpOfdwR8nMV4QPuTPwTONmIYLFU5w== -----END CERTIFICATE----- diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/client_pwd.key b/tools/test_apps/protocols/mqtt/publish_connect_test/client_pwd.key index c24b0db..a315282 100644 --- a/tools/test_apps/protocols/mqtt/publish_connect_test/client_pwd.key +++ b/tools/test_apps/protocols/mqtt/publish_connect_test/client_pwd.key @@ -1,30 +1,30 @@ -----BEGIN ENCRYPTED PRIVATE KEY----- -MIIFNTBfBgkqhkiG9w0BBQ0wUjAxBgkqhkiG9w0BBQwwJAQQfHxbCamtiRIZL9h4 -OAg27gICCAAwDAYIKoZIhvcNAgkFADAdBglghkgBZQMEASoEEP/5zadqi1x7zmHL -iKcmesMEggTQyAi0Ef1gl3xAZN2++wPZYOLaiFoQUmvq+lujIL8rjuVKjlyYEnXJ -WbR59x1SEqNPy99NsPqyvjdJP5hrA+HcGFfPJFhYj3D3h+0hCaGWNyhRUrC0nbdZ -LvoOiLV+HWkHWkpq9tcnC4wVZXxrYxifVD22uOcLNKDFz5H3eli1yM/VBGoS2iJZ -A/cBIbQ5/D3/1Q5/URf/6Cc/HE/y6wtRjnLcOXwK5ULjg2yMyOOrFSwmSkL8OPkf -8L9iIYGHmO9njRqNrjEuUQN+oS+bHO8pGvvBQxiS3ePQIP3b4gviaDCLs77B9bKO -9tSVYMGKSTh1ymBd6SF89uPdbDWpkljXw5pQ6kGix0CAR5oA3gvFKxR27+dSMJFD -GJjHXihQHozhTpnkOpGdQqIDrcNsGewdAELprCFpjgBrcRhzqRhWiKF3gyhv2s0L -YFWAb517Cf7ZYwsG90weuhD2emMmbxs9668MK0Gv+LSNs/WbbN/OQDVhhjfsZ235 -f3t5PZK+COrqG6LpaaGPTCFzWErLibQBVbnn+bfcjx9RneQeD65zv9Jw894xniYa -lOYkZNEZUuWoI3C7Q3wzphS2dRnPD1+AT6yMkvJMbb93KwHkhEmH2EoNiVjaqBer -ZsyTWkfP4W/OduGqGJrwAxCmcGq6xm9vomxzK/7Xdty190EEDMF0w5SsEtv7Zo4i -ckzx+ndqyFnpSvg05vSzf+5Og/X7G5uMbVanCIJ1lN5/W43LqF7OqqROTqJlrxZi -s6NFJuRyAdqQbldQi2UYXC+SR4px+ZAVOCkR6HfPIKvYWjHvoQlGF4bqtCB8eTML -ZfoArc6urDQemXE8vchM6YXxXAHPjE2jM/yhpzQhj25rmyNjb5WwAr0lZRb813Fu -IXiwQc6+UeXiBzm0KmJC4YcKlF49QR6M9UMuUJO7ceIjrCVummrq1/gvGPpzAVaP -i9kU81yEhfZYyESPZgwuZa2TdPAn4CXHTO1CqftrUHeine0bTPKPtOdZrX+UZiGR -njtw9DnZRmEmz2Ra4fwHrlB5RRSghaG5MaXvyERjo33q8nylECjG3jznyys4mxAc -y+pMd0VXnDrtgRKx7PvCOCmtLKuGg+T4bDoOIf/DswjWTrtKtceGfel5hGEusiuA -lsL0I6IJETFDDi9NeDzpnQTD4t6kjMIgs9b+TqN5ophjWEPjfn3IorM51loMemAs -ceTxqua5HT+exWKs0NCurIjE3CUu3WMDCPZLJzUiRbR9p9se4KMkOKduDy7++RMo -8yA+UlDwIVR2IcJBLs1I1i/Rh7qqNnvL/4YnNYOe2fi965l4FShPnw/Ghf6tUngz -U5GvKvknGxIvnX0jhr67y+3zmsb+ErK+3id64fB5jXKGgENkp3J31Amni5SgoWI1 -mGbv64VYleWljr8iKBX5cgE2VH1HSTOcfgABJB21lIg3XwzwGZ+eWLSE9wdR34hM -VtfNJQCcmEBm+ruoqw2OkddbiAZAgrW8NX5dIwm34lkCF9UG/eR85OkSqUF5JZlx -mlc7SpmmlmcEU+l+BMFdKKDlwRNnJNLIyciF0cMGtWlYRZ9oSatOMDm5ev6pGRuQ -tdL+gYAyro9s7wz7WCOz/JmUKJk8MWvYt9ECsIj8aVd/QXK8+M4f8ZE= +MIIFNTBfBgkqhkiG9w0BBQ0wUjAxBgkqhkiG9w0BBQwwJAQQHePIq3//PykNJRiP +8SJIiwICCAAwDAYIKoZIhvcNAgkFADAdBglghkgBZQMEASoEEGd76hqfyLtr9f/w +fETYk2QEggTQaLOK5NeAXOodW9eokEvmLeCSSHMfqXJWVSnHEbEJv5b5/lk6BoR0 +y7BYsWwRs0lvX4AZPflJJpM82VUt3HZdv7I6ANHV71BIkaHc8h5aWUoOFhKsGIqf +AibxMYDVs7st6YJFFzXidc0c8zCEDPjtDSdiaXz+lF6cbY7MUz2Ej7yJnECQiRqY +PC8tFJdQeUDbqhDSk4VyevgJPLIYILdg0m9PpJYJr97lFYjWyX6Q1FnAX8NPLnTw +yQ03OJNkuv1h5Y/nJgtAoISS3dPLn2KjDehJ9xbd6TiiBhyU2dRxKGmx71oOZvbj +yLs5TcJs76DcA/0ExxMZz2+pUOpXg/hGtdNCvHL2vrjcQ/f4qpMBd2FiazOzCTtJ +fu7DPYtirt+2xlt2htjWkXlIxE0sw4FxQYJPVVruiTfVpGtDuRGQUmAWFq5JASVi +2GguLex69JxeQb+Gv0SFwn/6eC14qWy+m3gCUdjW+qkSD+0Mk7mOxNnSIyyHXzhz +sXZ7ZncU/5Q3AEtlTYKoFV7BTmaHFRgPG6r7lKKNu2RdPRa8R3fa8HizhnIsdGe/ +m8H3ESKnpQLDiqA7cIM6eweErrOGMAwHBQSS3IdblZwdb6BPvpNUfp7lQ7YHdOYA +Vtl7gkwjniWQgSEOWqfvJ66Cr+u4wfV0rroTVP54pPeBGzort7L7Le3/ACO06ELN +Jdv6oTOsSZWs+AzwnU8/NQseB6xjvKafCBEbnYVoajTz29k52UNl/3fru3+e53Wu +WqJs8mCK9SGFLZfSp9Brecz0fpFNpQYAEEbd+HRkzT4lXCPPYGiPt+QdlcJtrwjq +NNSrCxsYxhsqEXpPzYj5ROvRBHjFGCpv6N33f05HoYQZf1hoXrABI/BQIZgXy8oD +AYKgBdrcCzJCuZyP9f66vSW7unxCAedhk4w7l6tozoPIFD2WXJ48ska9xvAjncMa +D3OAg95vEW4jfzTdLNSQnk5alOfqwX2CTHcXrzQn0OVnHG3xr1uCtTw605LoKEdf +6WQFihRnyIZOHAQyu8DVVGkhqpxH9d49VOrx5iVAhxSeK0vyhlKlZcq915VU6+mi +GEqjjywGJlN7Qk1bSfTr3AO6M8+Yd2YsBOzcGxtLkTxiUB94ss8QZzhb8qZWkSJ8 +XxhCYyL3brsnQJVjPLyJYwNaJGHcEOeSEFHichicont6tD/FdzS6hiMfVO/367f6 +q3tCLeaRYYBpM2600TjhgjfgupRJ3Y94QY0ukjwOPwx2xaciXR3RkXV0C0G4EiKH +zXJSUXxuBtdlHMyNlfh3EKRq17TkzacsbYuZ5jrGV7MIqpLF+dyqkeqMzsGFCnXN +S0gRAb2C1JwB3yWZnKtjZbUCwmwxna4YGEQZsGBxFkphvXZDJJpeDjGZHCKSkdRl +yY5UrKl6KW+n2h3CkWzuZPbAOs89o3N+pZdAxsaLUy3U4GAOev06z9fhwqjs7zsk +R/cDu/D7sIFlJfXW+QT/dXszDuLJmG/fZDBZOOshVmV8AD8nkQG802qPu47VtXLL +4NTb3XNO1ayqcrdEu/kdw9XnsXRvvRZ8Fc53qNp0zYF0/DqVnmGtHp9ttVXKDX7d +9grf6ltOzqlKfaabXTgaLkcYpeRXOLuDTPCUMRVJuxuhoWI6tQgzVx0= -----END ENCRYPTED PRIVATE KEY----- From e7abe55bc25488e8dee937a7746f63ffebf42cca Mon Sep 17 00:00:00 2001 From: gaoxu Date: Wed, 22 Jan 2025 10:41:07 +0800 Subject: [PATCH 215/231] feat(esp32h21): disable unsupported build test --- tools/test_apps/protocols/mqtt/build_test/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/test_apps/protocols/mqtt/build_test/README.md b/tools/test_apps/protocols/mqtt/build_test/README.md index 4f186a5..9c8a790 100644 --- a/tools/test_apps/protocols/mqtt/build_test/README.md +++ b/tools/test_apps/protocols/mqtt/build_test/README.md @@ -1,5 +1,5 @@ -| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C5 | ESP32-C6 | ESP32-C61 | ESP32-H2 | ESP32-P4 | ESP32-S2 | ESP32-S3 | -| ----------------- | ----- | -------- | -------- | -------- | -------- | --------- | -------- | -------- | -------- | -------- | +| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C5 | ESP32-C6 | ESP32-C61 | ESP32-H2 | ESP32-H21 | ESP32-P4 | ESP32-S2 | ESP32-S3 | +| ----------------- | ----- | -------- | -------- | -------- | -------- | --------- | -------- | --------- | -------- | -------- | -------- | # Build only test for C++ From b8d7b2bded664299b2e045ff217a8ffd34e15ecb Mon Sep 17 00:00:00 2001 From: "nilesh.kale" Date: Fri, 14 Feb 2025 15:06:15 +0530 Subject: [PATCH 216/231] docs: update document to remove dependency of esp32 on secure element usage --- docs/en/api-reference/protocols/mqtt.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/en/api-reference/protocols/mqtt.rst b/docs/en/api-reference/protocols/mqtt.rst index b221a92..9a918c7 100644 --- a/docs/en/api-reference/protocols/mqtt.rst +++ b/docs/en/api-reference/protocols/mqtt.rst @@ -154,7 +154,7 @@ It is possible to set authentication parameters through the :cpp:class:`authenti * :cpp:member:`password `: use a password by setting * :cpp:member:`certificate ` and :cpp:member:`key `: mutual authentication with TLS, and both can be provided in PEM or DER format - * :cpp:member:`use_secure_element `: use secure element (ATECC608A) interfaced to ESP32 + * :cpp:member:`use_secure_element `: use secure element (ATECC608A) interfaced to ESP32 series * :cpp:member:`ds_data `: use Digital Signature Peripheral available in some Espressif devices Session From d30dfd507445563a29299cfca8f4462809199f5e Mon Sep 17 00:00:00 2001 From: "igor.udot" Date: Mon, 24 Feb 2025 10:18:03 +0800 Subject: [PATCH 217/231] test: format all test scripts --- .../test_apps/test_mqtt/pytest_mqtt_ut.py | 5 +- .../test_apps/test_mqtt5/pytest_mqtt5_ut.py | 5 +- .../protocols/mqtt/ssl/pytest_mqtt_ssl.py | 19 ++-- .../protocols/mqtt/tcp/pytest_mqtt_tcp.py | 23 ++-- .../mqtt/ws/pytest_mqtt_ws_example.py | 14 ++- .../mqtt/wss/pytest_mqtt_wss_example.py | 18 ++-- examples/protocols/mqtt5/pytest_mqtt5.py | 8 +- .../publish_connect_test/pytest_mqtt_app.py | 101 ++++++++++++------ .../pytest_mqtt_publish_app.py | 93 ++++++++++------ 9 files changed, 184 insertions(+), 102 deletions(-) diff --git a/components/mqtt/test_apps/test_mqtt/pytest_mqtt_ut.py b/components/mqtt/test_apps/test_mqtt/pytest_mqtt_ut.py index 300d53e..429b857 100644 --- a/components/mqtt/test_apps/test_mqtt/pytest_mqtt_ut.py +++ b/components/mqtt/test_apps/test_mqtt/pytest_mqtt_ut.py @@ -1,10 +1,11 @@ -# SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD +# SPDX-FileCopyrightText: 2023-2025 Espressif Systems (Shanghai) CO LTD # SPDX-License-Identifier: Unlicense OR CC0-1.0 import pytest from pytest_embedded import Dut +from pytest_embedded_idf.utils import idf_parametrize -@pytest.mark.esp32 @pytest.mark.ethernet +@idf_parametrize('target', ['esp32'], indirect=['target']) def test_mqtt_client(dut: Dut) -> None: dut.expect_unity_test_output() diff --git a/components/mqtt/test_apps/test_mqtt5/pytest_mqtt5_ut.py b/components/mqtt/test_apps/test_mqtt5/pytest_mqtt5_ut.py index df15c39..7e38dfa 100644 --- a/components/mqtt/test_apps/test_mqtt5/pytest_mqtt5_ut.py +++ b/components/mqtt/test_apps/test_mqtt5/pytest_mqtt5_ut.py @@ -1,10 +1,11 @@ -# SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD +# SPDX-FileCopyrightText: 2023-2025 Espressif Systems (Shanghai) CO LTD # SPDX-License-Identifier: Unlicense OR CC0-1.0 import pytest from pytest_embedded import Dut +from pytest_embedded_idf.utils import idf_parametrize -@pytest.mark.esp32 @pytest.mark.ethernet +@idf_parametrize('target', ['esp32'], indirect=['target']) def test_mqtt5_client(dut: Dut) -> None: dut.expect_unity_test_output() diff --git a/examples/protocols/mqtt/ssl/pytest_mqtt_ssl.py b/examples/protocols/mqtt/ssl/pytest_mqtt_ssl.py index d27cc65..c34cea6 100644 --- a/examples/protocols/mqtt/ssl/pytest_mqtt_ssl.py +++ b/examples/protocols/mqtt/ssl/pytest_mqtt_ssl.py @@ -1,4 +1,4 @@ -# SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD +# SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD # SPDX-License-Identifier: Unlicense OR CC0-1.0 import logging import os @@ -12,6 +12,7 @@ import paho.mqtt.client as mqtt import pexpect import pytest from pytest_embedded import Dut +from pytest_embedded_idf.utils import idf_parametrize event_client_connected = Event() event_stop_client = Event() @@ -50,7 +51,9 @@ def on_message(client, userdata, msg): # type: (mqtt.Client, tuple, mqtt.client recv_binary = binary + '.received' with open(recv_binary, 'w', encoding='utf-8') as fw: fw.write(msg.payload) - raise ValueError('Received binary (saved as: {}) does not match the original file: {}'.format(recv_binary, binary)) + raise ValueError( + 'Received binary (saved as: {}) does not match the original file: {}'.format(recv_binary, binary) + ) payload = msg.payload.decode() if not event_client_received_correct.is_set() and payload == 'data': @@ -61,8 +64,8 @@ def on_message(client, userdata, msg): # type: (mqtt.Client, tuple, mqtt.client message_log += 'Received data:' + msg.topic + ' ' + payload + '\n' -@pytest.mark.esp32 @pytest.mark.ethernet +@idf_parametrize('target', ['esp32'], indirect=['target']) def test_examples_protocol_mqtt_ssl(dut): # type: (Dut) -> None broker_url = '' broker_port = 0 @@ -95,14 +98,16 @@ def test_examples_protocol_mqtt_ssl(dut): # type: (Dut) -> None client.on_connect = on_connect client.on_message = on_message client.user_data_set((binary_file, bin_size)) - client.tls_set(None, - None, - None, cert_reqs=ssl.CERT_NONE, tls_version=ssl.PROTOCOL_TLSv1_2, 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_url, broker_port, 60) except Exception: - print('ENV_TEST_FAILURE: Unexpected error while connecting to broker {}: {}:'.format(broker_url, sys.exc_info()[0])) + print( + 'ENV_TEST_FAILURE: Unexpected error while connecting to broker {}: {}:'.format( + broker_url, sys.exc_info()[0] + ) + ) raise # Starting a py-client in a separate thread thread1 = Thread(target=mqtt_client_task, args=(client,)) diff --git a/examples/protocols/mqtt/tcp/pytest_mqtt_tcp.py b/examples/protocols/mqtt/tcp/pytest_mqtt_tcp.py index 2786a76..f8deb23 100644 --- a/examples/protocols/mqtt/tcp/pytest_mqtt_tcp.py +++ b/examples/protocols/mqtt/tcp/pytest_mqtt_tcp.py @@ -1,4 +1,4 @@ -# SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD +# SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD # SPDX-License-Identifier: Unlicense OR CC0-1.0 import logging import os @@ -12,6 +12,7 @@ import pexpect import pytest from common_test_methods import get_host_ip4_by_dest_ip from pytest_embedded import Dut +from pytest_embedded_idf.utils import idf_parametrize msgid = -1 @@ -25,13 +26,15 @@ def mqqt_server_sketch(my_ip, port): # type: (str, str) -> None s.settimeout(60) s.bind((my_ip, port)) s.listen(1) - q,addr = s.accept() + q, addr = s.accept() q.settimeout(30) print('connection accepted') except Exception: - print('Local server on {}:{} listening/accepting failure: {}' - 'Possibly check permissions or firewall settings' - 'to accept connections on this address'.format(my_ip, port, sys.exc_info()[0])) + print( + 'Local server on {}:{} listening/accepting failure: {}' + 'Possibly check permissions or firewall settings' + 'to accept connections on this address'.format(my_ip, port, sys.exc_info()[0]) + ) raise data = q.recv(1024) # check if received initial empty message @@ -49,8 +52,8 @@ def mqqt_server_sketch(my_ip, port): # type: (str, str) -> None print('server closed') -@pytest.mark.esp32 @pytest.mark.ethernet +@idf_parametrize('target', ['esp32'], indirect=['target']) def test_examples_protocol_mqtt_qos1(dut: Dut) -> None: global msgid """ @@ -73,7 +76,7 @@ def test_examples_protocol_mqtt_qos1(dut: Dut) -> None: # 2. start mqtt broker sketch host_ip = get_host_ip4_by_dest_ip(ip_address) - thread1 = Thread(target=mqqt_server_sketch, args=(host_ip,1883)) + thread1 = Thread(target=mqqt_server_sketch, args=(host_ip, 1883)) thread1.start() data_write = 'mqtt://' + host_ip @@ -85,8 +88,10 @@ def test_examples_protocol_mqtt_qos1(dut: Dut) -> None: msgid_enqueued = dut.expect(b'outbox: ENQUEUE msgid=([0-9]+)', timeout=30).group(1).decode() msgid_deleted = dut.expect(b'outbox: DELETED msgid=([0-9]+)', timeout=30).group(1).decode() # 4. check the msgid of received data are the same as that of enqueued and deleted from outbox - if (msgid_enqueued == str(msgid) and msgid_deleted == str(msgid)): + if msgid_enqueued == str(msgid) and msgid_deleted == str(msgid): print('PASS: Received correct msg id') else: print('Failure!') - raise ValueError('Mismatch of msgid: received: {}, enqueued {}, deleted {}'.format(msgid, msgid_enqueued, msgid_deleted)) + raise ValueError( + 'Mismatch of msgid: received: {}, enqueued {}, deleted {}'.format(msgid, msgid_enqueued, msgid_deleted) + ) diff --git a/examples/protocols/mqtt/ws/pytest_mqtt_ws_example.py b/examples/protocols/mqtt/ws/pytest_mqtt_ws_example.py index 392139c..3ad6bd7 100644 --- a/examples/protocols/mqtt/ws/pytest_mqtt_ws_example.py +++ b/examples/protocols/mqtt/ws/pytest_mqtt_ws_example.py @@ -1,16 +1,18 @@ #!/usr/bin/env python # -# SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD +# SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD # SPDX-License-Identifier: Unlicense OR CC0-1.0 import logging import os import re import sys -from threading import Event, Thread +from threading import Event +from threading import Thread import paho.mqtt.client as mqtt import pytest from pytest_embedded import Dut +from pytest_embedded_idf.utils import idf_parametrize event_client_connected = Event() event_stop_client = Event() @@ -43,8 +45,8 @@ def on_message(client, userdata, msg): # type: (mqtt.Client, tuple, mqtt.client message_log += 'Received data:' + msg.topic + ' ' + payload + '\n' -@pytest.mark.esp32 @pytest.mark.ethernet +@idf_parametrize('target', ['esp32'], indirect=['target']) def test_examples_protocol_mqtt_ws(dut): # type: (Dut) -> None broker_url = '' broker_port = 0 @@ -77,7 +79,11 @@ def test_examples_protocol_mqtt_ws(dut): # type: (Dut) -> None print('Connecting...') client.connect(broker_url, broker_port, 60) except Exception: - print('ENV_TEST_FAILURE: Unexpected error while connecting to broker {}: {}:'.format(broker_url, sys.exc_info()[0])) + print( + 'ENV_TEST_FAILURE: Unexpected error while connecting to broker {}: {}:'.format( + broker_url, sys.exc_info()[0] + ) + ) raise # Starting a py-client in a separate thread thread1 = Thread(target=mqtt_client_task, args=(client,)) diff --git a/examples/protocols/mqtt/wss/pytest_mqtt_wss_example.py b/examples/protocols/mqtt/wss/pytest_mqtt_wss_example.py index 05eff5e..cb4a22e 100644 --- a/examples/protocols/mqtt/wss/pytest_mqtt_wss_example.py +++ b/examples/protocols/mqtt/wss/pytest_mqtt_wss_example.py @@ -1,18 +1,20 @@ #!/usr/bin/env python # -# SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD +# SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD # SPDX-License-Identifier: Unlicense OR CC0-1.0 import logging import os import re import ssl import sys -from threading import Event, Thread +from threading import Event +from threading import Thread import paho.mqtt.client as mqtt import pexpect import pytest from pytest_embedded import Dut +from pytest_embedded_idf.utils import idf_parametrize event_client_connected = Event() event_stop_client = Event() @@ -45,8 +47,8 @@ def on_message(client, userdata, msg): # type: (mqtt.Client, tuple, mqtt.client message_log += 'Received data:' + msg.topic + ' ' + payload + '\n' -@pytest.mark.esp32 @pytest.mark.ethernet +@idf_parametrize('target', ['esp32'], indirect=['target']) def test_examples_protocol_mqtt_wss(dut): # type: (Dut) -> None broker_url = '' broker_port = 0 @@ -76,13 +78,15 @@ def test_examples_protocol_mqtt_wss(dut): # type: (Dut) -> None client = mqtt.Client(transport='websockets') client.on_connect = on_connect client.on_message = on_message - client.tls_set(None, - None, - None, cert_reqs=ssl.CERT_NONE, tls_version=ssl.PROTOCOL_TLSv1_2, ciphers=None) + client.tls_set(None, None, None, cert_reqs=ssl.CERT_NONE, tls_version=ssl.PROTOCOL_TLSv1_2, ciphers=None) print('Connecting...') client.connect(broker_url, broker_port, 60) except Exception: - print('ENV_TEST_FAILURE: Unexpected error while connecting to broker {}: {}:'.format(broker_url, sys.exc_info()[0])) + print( + 'ENV_TEST_FAILURE: Unexpected error while connecting to broker {}: {}:'.format( + broker_url, sys.exc_info()[0] + ) + ) raise # Starting a py-client in a separate thread thread1 = Thread(target=mqtt_client_task, args=(client,)) diff --git a/examples/protocols/mqtt5/pytest_mqtt5.py b/examples/protocols/mqtt5/pytest_mqtt5.py index 603be68..dc7063e 100644 --- a/examples/protocols/mqtt5/pytest_mqtt5.py +++ b/examples/protocols/mqtt5/pytest_mqtt5.py @@ -1,23 +1,23 @@ #!/usr/bin/env python # -# SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD +# SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD # SPDX-License-Identifier: Apache-2.0 - import logging import os import pytest from pytest_embedded import Dut +from pytest_embedded_idf.utils import idf_parametrize -@pytest.mark.esp32 @pytest.mark.ethernet +@idf_parametrize('target', ['esp32'], indirect=['target']) def test_examples_protocol_mqtt5(dut: Dut) -> None: """ steps: | 1. join AP 2. connect to mqtt://mqtt.eclipseprojects.io - 3. check conneciton success + 3. check connection success """ # check and log bin size binary_file = os.path.join(dut.app.binary_path, 'mqtt5.bin') diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/pytest_mqtt_app.py b/tools/test_apps/protocols/mqtt/publish_connect_test/pytest_mqtt_app.py index 2377018..70da7eb 100644 --- a/tools/test_apps/protocols/mqtt/publish_connect_test/pytest_mqtt_app.py +++ b/tools/test_apps/protocols/mqtt/publish_connect_test/pytest_mqtt_app.py @@ -1,6 +1,5 @@ -# SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD +# SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD # SPDX-License-Identifier: Unlicense OR CC0-1.0 - import contextlib import logging import os @@ -9,31 +8,49 @@ import socketserver import ssl import subprocess from threading import Thread -from typing import Any, Callable, Dict, Optional +from typing import Any +from typing import Callable +from typing import Dict +from typing import Optional import pytest from common_test_methods import get_host_ip4_by_dest_ip from pytest_embedded import Dut +from pytest_embedded_idf.utils import idf_parametrize SERVER_PORT = 2222 def _path(f): # type: (str) -> str - return os.path.join(os.path.dirname(os.path.realpath(__file__)),f) + return os.path.join(os.path.dirname(os.path.realpath(__file__)), f) def set_server_cert_cn(ip): # type: (str) -> None arg_list = [ - ['openssl', 'req', '-out', _path('srv.csr'), '-key', _path('server.key'),'-subj', '/CN={}'.format(ip), '-new'], - ['openssl', 'x509', '-req', '-in', _path('srv.csr'), '-CA', _path('ca.crt'), - '-CAkey', _path('ca.key'), '-CAcreateserial', '-out', _path('srv.crt'), '-days', '360']] + ['openssl', 'req', '-out', _path('srv.csr'), '-key', _path('server.key'), '-subj', '/CN={}'.format(ip), '-new'], + [ + 'openssl', + 'x509', + '-req', + '-in', + _path('srv.csr'), + '-CA', + _path('ca.crt'), + '-CAkey', + _path('ca.key'), + '-CAcreateserial', + '-out', + _path('srv.crt'), + '-days', + '360', + ], + ] for args in arg_list: if subprocess.check_call(args) != 0: raise RuntimeError('openssl command {} failed'.format(args)) class MQTTHandler(socketserver.StreamRequestHandler): - def handle(self) -> None: logging.info(' - connection from: {}'.format(self.client_address)) data = bytearray(self.request.recv(1024)) @@ -56,12 +73,14 @@ class TlsServer(socketserver.TCPServer): allow_reuse_address = True allow_reuse_port = True - def __init__(self, - port:int = SERVER_PORT, - ServerHandler: Callable[[Any, Any, Any], socketserver.BaseRequestHandler] = MQTTHandler, - client_cert:bool=False, - refuse_connection:bool=False, - use_alpn:bool=False): + def __init__( + self, + port: int = SERVER_PORT, + ServerHandler: Callable[[Any, Any, Any], socketserver.BaseRequestHandler] = MQTTHandler, + client_cert: bool = False, + refuse_connection: bool = False, + use_alpn: bool = False, + ): self.refuse_connection = refuse_connection self.context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH) self.ssl_error = '' @@ -73,7 +92,7 @@ class TlsServer(socketserver.TCPServer): if use_alpn: self.context.set_alpn_protocols(['mymqtt', 'http/1.1']) self.server_thread = Thread(target=self.serve_forever) - super().__init__(('',port), ServerHandler) + super().__init__(('', port), ServerHandler) def server_activate(self) -> None: self.socket = self.context.wrap_socket(self.socket, server_side=True) @@ -125,14 +144,16 @@ def get_test_cases(dut: Dut) -> Any: cases = {} try: # Get connection test cases configuration: symbolic names for test cases - for case in ['EXAMPLE_CONNECT_CASE_NO_CERT', - 'EXAMPLE_CONNECT_CASE_SERVER_CERT', - 'EXAMPLE_CONNECT_CASE_MUTUAL_AUTH', - 'EXAMPLE_CONNECT_CASE_INVALID_SERVER_CERT', - 'EXAMPLE_CONNECT_CASE_SERVER_DER_CERT', - 'EXAMPLE_CONNECT_CASE_MUTUAL_AUTH_KEY_PWD', - 'EXAMPLE_CONNECT_CASE_MUTUAL_AUTH_BAD_CRT', - 'EXAMPLE_CONNECT_CASE_NO_CERT_ALPN']: + for case in [ + 'EXAMPLE_CONNECT_CASE_NO_CERT', + 'EXAMPLE_CONNECT_CASE_SERVER_CERT', + 'EXAMPLE_CONNECT_CASE_MUTUAL_AUTH', + 'EXAMPLE_CONNECT_CASE_INVALID_SERVER_CERT', + 'EXAMPLE_CONNECT_CASE_SERVER_DER_CERT', + 'EXAMPLE_CONNECT_CASE_MUTUAL_AUTH_KEY_PWD', + 'EXAMPLE_CONNECT_CASE_MUTUAL_AUTH_BAD_CRT', + 'EXAMPLE_CONNECT_CASE_NO_CERT_ALPN', + ]: cases[case] = dut.app.sdkconfig.get(case) except Exception: logging.error('ENV_TEST_FAILURE: Some mandatory CONNECTION test case not found in sdkconfig') @@ -147,7 +168,7 @@ def get_dut_ip(dut: Dut) -> Any: @contextlib.contextmanager -def connect_dut(dut: Dut, uri:str, case_id:int) -> Any: +def connect_dut(dut: Dut, uri: str, case_id: int) -> Any: dut.write('connection_setup') dut.write(f'connect {uri} {case_id}') dut.expect(f'Test case:{case_id} started') @@ -157,12 +178,16 @@ def connect_dut(dut: Dut, uri:str, case_id:int) -> Any: dut.write('disconnect') -def run_cases(dut:Dut, uri:str, cases:Dict[str, int]) -> None: +def run_cases(dut: Dut, uri: str, cases: Dict[str, int]) -> None: try: dut.write('init') dut.write(f'start') dut.write(f'disconnect') - for case in ['EXAMPLE_CONNECT_CASE_NO_CERT', 'EXAMPLE_CONNECT_CASE_SERVER_CERT', 'EXAMPLE_CONNECT_CASE_SERVER_DER_CERT']: + for case in [ + 'EXAMPLE_CONNECT_CASE_NO_CERT', + 'EXAMPLE_CONNECT_CASE_SERVER_CERT', + 'EXAMPLE_CONNECT_CASE_SERVER_DER_CERT', + ]: # All these cases connect to the server with no server verification or with server only verification with TlsServer(), connect_dut(dut, uri, cases[case]): logging.info(f'Running {case}: default server - expect to connect normally') @@ -172,9 +197,13 @@ def run_cases(dut:Dut, uri:str, cases:Dict[str, int]) -> None: dut.expect(f'MQTT_EVENT_ERROR: Test={cases[case]}', timeout=30) dut.expect('MQTT ERROR: 0x5') # expecting 0x5 ... connection not authorized error with TlsServer(client_cert=True) as server, connect_dut(dut, uri, cases[case]): - logging.info(f'Running {case}: server with client verification - handshake error since client presents no client certificate') + logging.info( + f'Running {case}: server with client verification - handshake error since client presents no client certificate' + ) dut.expect(f'MQTT_EVENT_ERROR: Test={cases[case]}', timeout=30) - dut.expect('ESP-TLS ERROR: ESP_ERR_MBEDTLS_SSL_HANDSHAKE_FAILED') # expect ... handshake error (PEER_DID_NOT_RETURN_A_CERTIFICATE) + dut.expect( + 'ESP-TLS ERROR: ESP_ERR_MBEDTLS_SSL_HANDSHAKE_FAILED' + ) # expect ... handshake error (PEER_DID_NOT_RETURN_A_CERTIFICATE) assert 'PEER_DID_NOT_RETURN_A_CERTIFICATE' in server.last_ssl_error() for case in ['EXAMPLE_CONNECT_CASE_MUTUAL_AUTH', 'EXAMPLE_CONNECT_CASE_MUTUAL_AUTH_KEY_PWD']: @@ -187,15 +216,21 @@ def run_cases(dut:Dut, uri:str, cases:Dict[str, int]) -> None: with TlsServer() as s, connect_dut(dut, uri, cases[case]): logging.info(f'Running {case}: invalid server certificate on default server - expect ssl handshake error') dut.expect(f'MQTT_EVENT_ERROR: Test={cases[case]}', timeout=30) - dut.expect('ESP-TLS ERROR: ESP_ERR_MBEDTLS_SSL_HANDSHAKE_FAILED') # expect ... handshake error (TLSV1_ALERT_UNKNOWN_CA) - if re.match('.*alert.*unknown.*ca',s.last_ssl_error(), flags=re.I) is None: + dut.expect( + 'ESP-TLS ERROR: ESP_ERR_MBEDTLS_SSL_HANDSHAKE_FAILED' + ) # expect ... handshake error (TLSV1_ALERT_UNKNOWN_CA) + if re.match('.*alert.*unknown.*ca', s.last_ssl_error(), flags=re.I) is None: raise Exception(f'Unexpected ssl error from the server: {s.last_ssl_error()}') case = 'EXAMPLE_CONNECT_CASE_MUTUAL_AUTH_BAD_CRT' with TlsServer(client_cert=True) as s, connect_dut(dut, uri, cases[case]): - logging.info(f'Running {case}: Invalid client certificate on server with client verification - expect ssl handshake error') + logging.info( + f'Running {case}: Invalid client certificate on server with client verification - expect ssl handshake error' + ) dut.expect(f'MQTT_EVENT_ERROR: Test={cases[case]}', timeout=30) - dut.expect('ESP-TLS ERROR: ESP_ERR_MBEDTLS_SSL_HANDSHAKE_FAILED') # expect ... handshake error (CERTIFICATE_VERIFY_FAILED) + dut.expect( + 'ESP-TLS ERROR: ESP_ERR_MBEDTLS_SSL_HANDSHAKE_FAILED' + ) # expect ... handshake error (CERTIFICATE_VERIFY_FAILED) if 'CERTIFICATE_VERIFY_FAILED' not in s.last_ssl_error(): raise Exception('Unexpected ssl error from the server {}'.format(s.last_ssl_error())) @@ -214,8 +249,8 @@ def run_cases(dut:Dut, uri:str, cases:Dict[str, int]) -> None: dut.write('destroy') -@pytest.mark.esp32 @pytest.mark.ethernet +@idf_parametrize('target', ['esp32'], indirect=['target']) def test_mqtt_connect( dut: Dut, log_performance: Callable[[str, object], None], diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/pytest_mqtt_publish_app.py b/tools/test_apps/protocols/mqtt/publish_connect_test/pytest_mqtt_publish_app.py index b2df800..4eb25d1 100644 --- a/tools/test_apps/protocols/mqtt/publish_connect_test/pytest_mqtt_publish_app.py +++ b/tools/test_apps/protocols/mqtt/publish_connect_test/pytest_mqtt_publish_app.py @@ -1,4 +1,4 @@ -# SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD +# SPDX-FileCopyrightText: 2023-2025 Espressif Systems (Shanghai) CO LTD # SPDX-License-Identifier: Unlicense OR CC0-1.0 import contextlib import difflib @@ -22,13 +22,13 @@ import paho.mqtt.client as mqtt import pexpect import pytest from pytest_embedded import Dut +from pytest_embedded_idf.utils import idf_parametrize DEFAULT_MSG_SIZE = 16 # Publisher class creating a python client to send/receive published data from esp-mqtt client class MqttPublisher(mqtt.Client): - def __init__(self, config, log_details=False): # type: (MqttPublisher, dict, bool) -> None self.log_details = log_details self.config = config @@ -40,7 +40,9 @@ class MqttPublisher(mqtt.Client): self.event_client_subscribed = Event() self.event_client_got_all = Event() transport = 'websockets' if self.config['transport'] in ['ws', 'wss'] else 'tcp' - client_id = 'MqttTestRunner' + ''.join(random.choice(string.ascii_uppercase + string.ascii_lowercase) for _ in range(5)) + client_id = 'MqttTestRunner' + ''.join( + random.choice(string.ascii_uppercase + string.ascii_lowercase) for _ in range(5) + ) super().__init__(client_id, userdata=0, transport=transport) def print_details(self, text): # type: (str) -> None @@ -53,7 +55,7 @@ class MqttPublisher(mqtt.Client): logging.info(f'Subscribed to {self.config["subscribe_topic"]} successfully with QoS: {granted_qos}') self.event_client_subscribed.set() - def on_connect(self, mqttc: Any, obj: Any, flags: Any, rc:int) -> None: + def on_connect(self, mqttc: Any, obj: Any, flags: Any, rc: int) -> None: self.event_client_connected.set() def on_connect_fail(self, mqttc: Any, obj: Any) -> None: @@ -67,8 +69,10 @@ class MqttPublisher(mqtt.Client): self.event_client_got_all.set() else: differences = len(list(filter(lambda data: data[0] != data[1], zip(payload, self.expected_data)))) - logging.error(f'Payload differ in {differences} positions from expected data. received size: {len(payload)} expected size:' - f'{len(self.expected_data)}') + logging.error( + f'Payload differ in {differences} positions from expected data. received size: {len(payload)} expected size:' + f'{len(self.expected_data)}' + ) logging.info(f'Repetitions: {payload.count(self.config["pattern"])}') logging.info(f'Pattern: {self.config["pattern"]}') logging.info(f'First: {payload[:DEFAULT_MSG_SIZE]}') @@ -107,9 +111,10 @@ class MqttPublisher(mqtt.Client): self.loop_stop() -def get_configurations(dut: Dut, test_case: Any) -> Dict[str,Any]: +def get_configurations(dut: Dut, test_case: Any) -> Dict[str, Any]: publish_cfg = {} try: + @no_type_check def get_config_from_dut(dut, config_option): # logging.info('Option:', config_option, dut.app.sdkconfig.get(config_option)) @@ -117,11 +122,18 @@ def get_configurations(dut: Dut, test_case: Any) -> Dict[str,Any]: if value is None: return None, None return value.group(1), int(value.group(2)) + # Get publish test configuration - publish_cfg['broker_host_ssl'], publish_cfg['broker_port_ssl'] = get_config_from_dut(dut, 'EXAMPLE_BROKER_SSL_URI') - publish_cfg['broker_host_tcp'], publish_cfg['broker_port_tcp'] = get_config_from_dut(dut, 'EXAMPLE_BROKER_TCP_URI') + publish_cfg['broker_host_ssl'], publish_cfg['broker_port_ssl'] = get_config_from_dut( + dut, 'EXAMPLE_BROKER_SSL_URI' + ) + publish_cfg['broker_host_tcp'], publish_cfg['broker_port_tcp'] = get_config_from_dut( + dut, 'EXAMPLE_BROKER_TCP_URI' + ) publish_cfg['broker_host_ws'], publish_cfg['broker_port_ws'] = get_config_from_dut(dut, 'EXAMPLE_BROKER_WS_URI') - publish_cfg['broker_host_wss'], publish_cfg['broker_port_wss'] = get_config_from_dut(dut, 'EXAMPLE_BROKER_WSS_URI') + publish_cfg['broker_host_wss'], publish_cfg['broker_port_wss'] = get_config_from_dut( + dut, 'EXAMPLE_BROKER_WSS_URI' + ) except Exception: logging.info('ENV_TEST_FAILURE: Some mandatory PUBLISH test case not found in sdkconfig') @@ -133,9 +145,13 @@ def get_configurations(dut: Dut, test_case: Any) -> Dict[str,Any]: publish_cfg['qos'] = qos publish_cfg['enqueue'] = enqueue publish_cfg['transport'] = transport - publish_cfg['pattern'] = ''.join(random.choice(string.ascii_uppercase + string.ascii_lowercase + string.digits) for _ in range(DEFAULT_MSG_SIZE)) + publish_cfg['pattern'] = ''.join( + random.choice(string.ascii_uppercase + string.ascii_lowercase + string.digits) for _ in range(DEFAULT_MSG_SIZE) + ) publish_cfg['test_timeout'] = get_timeout(test_case) - unique_topic = ''.join(random.choice(string.ascii_uppercase + string.ascii_lowercase) for _ in range(DEFAULT_MSG_SIZE)) + unique_topic = ''.join( + random.choice(string.ascii_uppercase + string.ascii_lowercase) for _ in range(DEFAULT_MSG_SIZE) + ) publish_cfg['subscribe_topic'] = 'test/subscribe_to/' + unique_topic publish_cfg['publish_topic'] = 'test/subscribe_to/' + unique_topic logging.info(f'configuration: {publish_cfg}') @@ -143,7 +159,7 @@ def get_configurations(dut: Dut, test_case: Any) -> Dict[str,Any]: @contextlib.contextmanager -def connected_and_subscribed(dut:Dut) -> Any: +def connected_and_subscribed(dut: Dut) -> Any: dut.write('start') dut.expect(re.compile(rb'MQTT_EVENT_SUBSCRIBED'), timeout=60) yield @@ -155,16 +171,17 @@ def get_scenarios() -> List[Dict[str, int]]: # Initialize message sizes and repeat counts (if defined in the environment) for i in count(0): # Check env variable: MQTT_PUBLISH_MSG_{len|repeat}_{x} - env_dict = {var:'MQTT_PUBLISH_MSG_' + var + '_' + str(i) for var in ['len', 'repeat']} + env_dict = {var: 'MQTT_PUBLISH_MSG_' + var + '_' + str(i) for var in ['len', 'repeat']} if os.getenv(env_dict['len']) and os.getenv(env_dict['repeat']): scenarios.append({var: int(os.getenv(env_dict[var])) for var in ['len', 'repeat']}) # type: ignore continue break - if not scenarios: # No message sizes present in the env - set defaults - scenarios = [{'msg_len':0, 'nr_of_msgs':5}, # zero-sized messages - {'msg_len':2, 'nr_of_msgs':5}, # short messages - {'msg_len':200, 'nr_of_msgs':3}, # long messages - ] + if not scenarios: # No message sizes present in the env - set defaults + scenarios = [ + {'msg_len': 0, 'nr_of_msgs': 5}, # zero-sized messages + {'msg_len': 2, 'nr_of_msgs': 5}, # short messages + {'msg_len': 200, 'nr_of_msgs': 3}, # long messages + ] return scenarios @@ -181,17 +198,23 @@ def get_timeout(test_case: Any) -> int: def run_publish_test_case(dut: Dut, config: Any) -> None: - logging.info(f'Starting Publish test: transport:{config["transport"]}, qos:{config["qos"]},' - f'nr_of_msgs:{config["scenario"]["nr_of_msgs"]},' - f' msg_size:{config["scenario"]["msg_len"] * DEFAULT_MSG_SIZE}, enqueue:{config["enqueue"]}') - dut.write(f'publish_setup {config["transport"]} {config["publish_topic"]} {config["subscribe_topic"]} {config["pattern"]} {config["scenario"]["msg_len"]}') + logging.info( + f'Starting Publish test: transport:{config["transport"]}, qos:{config["qos"]},' + f'nr_of_msgs:{config["scenario"]["nr_of_msgs"]},' + f' msg_size:{config["scenario"]["msg_len"] * DEFAULT_MSG_SIZE}, enqueue:{config["enqueue"]}' + ) + dut.write( + f'publish_setup {config["transport"]} {config["publish_topic"]} {config["subscribe_topic"]} {config["pattern"]} {config["scenario"]["msg_len"]}' + ) with MqttPublisher(config) as publisher, connected_and_subscribed(dut): assert publisher.event_client_subscribed.wait(timeout=config['test_timeout']), 'Runner failed to subscribe' msgs_published: List[mqtt.MQTTMessageInfo] = [] dut.write(f'publish {config["scenario"]["nr_of_msgs"]} {config["qos"]} {config["enqueue"]}') - assert publisher.event_client_got_all.wait(timeout=config['test_timeout']), (f'Not all data received from ESP32: {config["transport"]} ' - f'qos={config["qos"]} received: {publisher.received} ' - f'expected: {config["scenario"]["nr_of_msgs"]}') + assert publisher.event_client_got_all.wait(timeout=config['test_timeout']), ( + f'Not all data received from ESP32: {config["transport"]} ' + f'qos={config["qos"]} received: {publisher.received} ' + f'expected: {config["scenario"]["nr_of_msgs"]}' + ) logging.info(' - all data received from ESP32') payload = config['pattern'] * config['scenario']['msg_len'] for _ in range(config['scenario']['nr_of_msgs']): @@ -214,15 +237,17 @@ def run_publish_test_case(dut: Dut, config: Any) -> None: logging.info('ESP32 received all data from runner') -stress_scenarios = [{'msg_len':20, 'nr_of_msgs':30}] # many medium sized +stress_scenarios = [{'msg_len': 20, 'nr_of_msgs': 30}] # many medium sized transport_cases = ['tcp', 'ws', 'wss', 'ssl'] qos_cases = [0, 1, 2] enqueue_cases = [0, 1] local_broker_supported_transports = ['tcp'] -local_broker_scenarios = [{'msg_len':0, 'nr_of_msgs':5}, # zero-sized messages - {'msg_len':5, 'nr_of_msgs':20}, # short messages - {'msg_len':500, 'nr_of_msgs':10}, # long messages - {'msg_len':20, 'nr_of_msgs':20}] # many medium sized +local_broker_scenarios = [ + {'msg_len': 0, 'nr_of_msgs': 5}, # zero-sized messages + {'msg_len': 5, 'nr_of_msgs': 20}, # short messages + {'msg_len': 500, 'nr_of_msgs': 10}, # long messages + {'msg_len': 20, 'nr_of_msgs': 20}, +] # many medium sized def make_cases(transport: Any, scenarios: List[Dict[str, int]]) -> List[Tuple[str, int, int, Dict[str, int]]]: @@ -233,10 +258,10 @@ test_cases = make_cases(transport_cases, get_scenarios()) stress_test_cases = make_cases(transport_cases, stress_scenarios) -@pytest.mark.esp32 @pytest.mark.ethernet @pytest.mark.parametrize('test_case', test_cases) @pytest.mark.parametrize('config', ['default'], indirect=True) +@idf_parametrize('target', ['esp32'], indirect=['target']) def test_mqtt_publish(dut: Dut, test_case: Any) -> None: publish_cfg = get_configurations(dut, test_case) dut.expect(re.compile(rb'mqtt>'), timeout=30) @@ -244,11 +269,11 @@ def test_mqtt_publish(dut: Dut, test_case: Any) -> None: run_publish_test_case(dut, publish_cfg) -@pytest.mark.esp32 @pytest.mark.ethernet_stress @pytest.mark.nightly_run @pytest.mark.parametrize('test_case', stress_test_cases) @pytest.mark.parametrize('config', ['default'], indirect=True) +@idf_parametrize('target', ['esp32'], indirect=['target']) def test_mqtt_publish_stress(dut: Dut, test_case: Any) -> None: publish_cfg = get_configurations(dut, test_case) dut.expect(re.compile(rb'mqtt>'), timeout=30) @@ -256,10 +281,10 @@ def test_mqtt_publish_stress(dut: Dut, test_case: Any) -> None: run_publish_test_case(dut, publish_cfg) -@pytest.mark.esp32 @pytest.mark.ethernet @pytest.mark.parametrize('test_case', make_cases(local_broker_supported_transports, local_broker_scenarios)) @pytest.mark.parametrize('config', ['local_broker'], indirect=True) +@idf_parametrize('target', ['esp32'], indirect=['target']) def test_mqtt_publish_local(dut: Dut, test_case: Any) -> None: if test_case[0] not in local_broker_supported_transports: pytest.skip(f'Skipping transport: {test_case[0]}...') From 003ea16dfdcf3aeb2185c37818f888c4f74e4382 Mon Sep 17 00:00:00 2001 From: Zhang Shuxian Date: Wed, 26 Feb 2025 19:19:34 +0800 Subject: [PATCH 218/231] docs: Update CN translation in protocols --- docs/en/api-reference/protocols/mqtt.rst | 6 +++--- docs/zh_CN/api-reference/protocols/mqtt.rst | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/en/api-reference/protocols/mqtt.rst b/docs/en/api-reference/protocols/mqtt.rst index 9a918c7..cde304e 100644 --- a/docs/en/api-reference/protocols/mqtt.rst +++ b/docs/en/api-reference/protocols/mqtt.rst @@ -42,11 +42,11 @@ Application Examples MQTT Message Retransmission --------------------------- -A new MQTT message is created by calling :cpp:func:`esp_mqtt_client_publish ` or its non blocking counterpart :cpp:func:`esp_mqtt_client_enqueue `. +A new MQTT message can be created by calling :cpp:func:`esp_mqtt_client_publish ` or its non-blocking counterpart :cpp:func:`esp_mqtt_client_enqueue `. -Messages with QoS 0 is sent only once. QoS 1 and 2 have different behaviors since the protocol requires extra steps to complete the process. +Messages with QoS 0 are sent only once. QoS 1 and 2 behave differently since the protocol requires additional steps to complete the process. -The ESP-MQTT library opts to always retransmit unacknowledged QoS 1 and 2 publish messages to avoid losses in faulty connections, even though the MQTT specification requires the re-transmission only on reconnect with Clean Session flag been set to 0 (set :cpp:member:`disable_clean_session ` to true for this behavior). +The ESP-MQTT library opts to always retransmit unacknowledged QoS 1 and 2 publish messages to prevent data loss in faulty connections, even though the MQTT specification requires the re-transmission only on reconnect with Clean Session flag been set to 0 (set :cpp:member:`disable_clean_session ` to true for this behavior). QoS 1 and 2 messages that may need retransmission are always enqueued, but first transmission try occurs immediately if :cpp:func:`esp_mqtt_client_publish ` is used. A transmission retry for unacknowledged messages will occur after :cpp:member:`message_retransmit_timeout `. After :ref:`CONFIG_MQTT_OUTBOX_EXPIRED_TIMEOUT_MS` messages will expire and be deleted. If :ref:`CONFIG_MQTT_REPORT_DELETED_MESSAGES` is set, an event will be sent to notify the user. diff --git a/docs/zh_CN/api-reference/protocols/mqtt.rst b/docs/zh_CN/api-reference/protocols/mqtt.rst index b2a79f7..27d3641 100644 --- a/docs/zh_CN/api-reference/protocols/mqtt.rst +++ b/docs/zh_CN/api-reference/protocols/mqtt.rst @@ -154,7 +154,7 @@ ESP-MQTT 库将始终重新传输未确认的 QoS 1 和 2 发布消息,以避 * :cpp:member:`password `:使用密码 * * :cpp:member:`certificate ` 和 :cpp:member:`key `:进行双向 TLS 身份验证,PEM 或 DER 格式均可 - * :cpp:member:`use_secure_element `:使用 ESP32 中的安全元素 (ATECC608A) + * :cpp:member:`use_secure_element `:使用 ESP32 系列中的安全元素 (ATECC608A) * :cpp:member:`ds_data `:使用某些乐鑫设备的数字签名外设 会话 From 05b46d3ed08f54b1e96c2e5e096ecffd7c03df0b Mon Sep 17 00:00:00 2001 From: Chen Jichang Date: Thu, 20 Mar 2025 18:27:02 +0800 Subject: [PATCH 219/231] feat(esp32h4): enable ESP32H4 ci build --- examples/protocols/mqtt/custom_outbox/README.md | 4 ++-- examples/protocols/mqtt/ssl/README.md | 4 ++-- examples/protocols/mqtt/ssl_mutual_auth/README.md | 4 ++-- examples/protocols/mqtt/ssl_psk/README.md | 4 ++-- examples/protocols/mqtt/tcp/README.md | 4 ++-- examples/protocols/mqtt/ws/README.md | 4 ++-- examples/protocols/mqtt/wss/README.md | 4 ++-- examples/protocols/mqtt5/README.md | 4 ++-- tools/test_apps/protocols/mqtt/build_test/README.md | 4 ++-- 9 files changed, 18 insertions(+), 18 deletions(-) diff --git a/examples/protocols/mqtt/custom_outbox/README.md b/examples/protocols/mqtt/custom_outbox/README.md index 5b64bcd..e5c7e2a 100644 --- a/examples/protocols/mqtt/custom_outbox/README.md +++ b/examples/protocols/mqtt/custom_outbox/README.md @@ -1,5 +1,5 @@ -| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C5 | ESP32-C6 | ESP32-C61 | ESP32-H2 | ESP32-P4 | ESP32-S2 | ESP32-S3 | -| ----------------- | ----- | -------- | -------- | -------- | -------- | --------- | -------- | -------- | -------- | -------- | +| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C5 | ESP32-C6 | ESP32-C61 | ESP32-H2 | ESP32-H4 | ESP32-P4 | ESP32-S2 | ESP32-S3 | +| ----------------- | ----- | -------- | -------- | -------- | -------- | --------- | -------- | -------- | -------- | -------- | -------- | # ESP-MQTT custom outbox sample application (See the README.md file in the upper level 'examples' directory for more information about examples.) diff --git a/examples/protocols/mqtt/ssl/README.md b/examples/protocols/mqtt/ssl/README.md index 0407eda..e2db747 100644 --- a/examples/protocols/mqtt/ssl/README.md +++ b/examples/protocols/mqtt/ssl/README.md @@ -1,5 +1,5 @@ -| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C5 | ESP32-C6 | ESP32-C61 | ESP32-H2 | ESP32-P4 | ESP32-S2 | ESP32-S3 | -| ----------------- | ----- | -------- | -------- | -------- | -------- | --------- | -------- | -------- | -------- | -------- | +| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C5 | ESP32-C6 | ESP32-C61 | ESP32-H2 | ESP32-H4 | ESP32-P4 | ESP32-S2 | ESP32-S3 | +| ----------------- | ----- | -------- | -------- | -------- | -------- | --------- | -------- | -------- | -------- | -------- | -------- | # ESP-MQTT SSL Sample application diff --git a/examples/protocols/mqtt/ssl_mutual_auth/README.md b/examples/protocols/mqtt/ssl_mutual_auth/README.md index 4750ff1..c902864 100644 --- a/examples/protocols/mqtt/ssl_mutual_auth/README.md +++ b/examples/protocols/mqtt/ssl_mutual_auth/README.md @@ -1,5 +1,5 @@ -| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C5 | ESP32-C6 | ESP32-C61 | ESP32-H2 | ESP32-P4 | ESP32-S2 | ESP32-S3 | -| ----------------- | ----- | -------- | -------- | -------- | -------- | --------- | -------- | -------- | -------- | -------- | +| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C5 | ESP32-C6 | ESP32-C61 | ESP32-H2 | ESP32-H4 | ESP32-P4 | ESP32-S2 | ESP32-S3 | +| ----------------- | ----- | -------- | -------- | -------- | -------- | --------- | -------- | -------- | -------- | -------- | -------- | # ESP-MQTT SSL Sample application (mutual authentication) diff --git a/examples/protocols/mqtt/ssl_psk/README.md b/examples/protocols/mqtt/ssl_psk/README.md index d5dbb1b..b57ac22 100644 --- a/examples/protocols/mqtt/ssl_psk/README.md +++ b/examples/protocols/mqtt/ssl_psk/README.md @@ -1,5 +1,5 @@ -| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C5 | ESP32-C6 | ESP32-C61 | ESP32-H2 | ESP32-P4 | ESP32-S2 | ESP32-S3 | -| ----------------- | ----- | -------- | -------- | -------- | -------- | --------- | -------- | -------- | -------- | -------- | +| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C5 | ESP32-C6 | ESP32-C61 | ESP32-H2 | ESP32-H4 | ESP32-P4 | ESP32-S2 | ESP32-S3 | +| ----------------- | ----- | -------- | -------- | -------- | -------- | --------- | -------- | -------- | -------- | -------- | -------- | # ESP-MQTT SSL example with PSK verification diff --git a/examples/protocols/mqtt/tcp/README.md b/examples/protocols/mqtt/tcp/README.md index cb9f2cc..c8a11f8 100644 --- a/examples/protocols/mqtt/tcp/README.md +++ b/examples/protocols/mqtt/tcp/README.md @@ -1,5 +1,5 @@ -| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C5 | ESP32-C6 | ESP32-C61 | ESP32-H2 | ESP32-P4 | ESP32-S2 | ESP32-S3 | -| ----------------- | ----- | -------- | -------- | -------- | -------- | --------- | -------- | -------- | -------- | -------- | +| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C5 | ESP32-C6 | ESP32-C61 | ESP32-H2 | ESP32-H4 | ESP32-P4 | ESP32-S2 | ESP32-S3 | +| ----------------- | ----- | -------- | -------- | -------- | -------- | --------- | -------- | -------- | -------- | -------- | -------- | # ESP-MQTT sample application (See the README.md file in the upper level 'examples' directory for more information about examples.) diff --git a/examples/protocols/mqtt/ws/README.md b/examples/protocols/mqtt/ws/README.md index 5d1724f..d41c736 100644 --- a/examples/protocols/mqtt/ws/README.md +++ b/examples/protocols/mqtt/ws/README.md @@ -1,5 +1,5 @@ -| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C5 | ESP32-C6 | ESP32-C61 | ESP32-H2 | ESP32-P4 | ESP32-S2 | ESP32-S3 | -| ----------------- | ----- | -------- | -------- | -------- | -------- | --------- | -------- | -------- | -------- | -------- | +| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C5 | ESP32-C6 | ESP32-C61 | ESP32-H2 | ESP32-H4 | ESP32-P4 | ESP32-S2 | ESP32-S3 | +| ----------------- | ----- | -------- | -------- | -------- | -------- | --------- | -------- | -------- | -------- | -------- | -------- | # ESP-MQTT MQTT over Websocket diff --git a/examples/protocols/mqtt/wss/README.md b/examples/protocols/mqtt/wss/README.md index b401d31..06e596e 100644 --- a/examples/protocols/mqtt/wss/README.md +++ b/examples/protocols/mqtt/wss/README.md @@ -1,5 +1,5 @@ -| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C5 | ESP32-C6 | ESP32-C61 | ESP32-H2 | ESP32-P4 | ESP32-S2 | ESP32-S3 | -| ----------------- | ----- | -------- | -------- | -------- | -------- | --------- | -------- | -------- | -------- | -------- | +| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C5 | ESP32-C6 | ESP32-C61 | ESP32-H2 | ESP32-H4 | ESP32-P4 | ESP32-S2 | ESP32-S3 | +| ----------------- | ----- | -------- | -------- | -------- | -------- | --------- | -------- | -------- | -------- | -------- | -------- | # ESP-MQTT MQTT over WSS Sample application (See the README.md file in the upper level 'examples' directory for more information about examples.) diff --git a/examples/protocols/mqtt5/README.md b/examples/protocols/mqtt5/README.md index 3e6c9c2..f8874fa 100644 --- a/examples/protocols/mqtt5/README.md +++ b/examples/protocols/mqtt5/README.md @@ -1,5 +1,5 @@ -| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C5 | ESP32-C6 | ESP32-C61 | ESP32-H2 | ESP32-P4 | ESP32-S2 | ESP32-S3 | -| ----------------- | ----- | -------- | -------- | -------- | -------- | --------- | -------- | -------- | -------- | -------- | +| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C5 | ESP32-C6 | ESP32-C61 | ESP32-H2 | ESP32-H4 | ESP32-P4 | ESP32-S2 | ESP32-S3 | +| ----------------- | ----- | -------- | -------- | -------- | -------- | --------- | -------- | -------- | -------- | -------- | -------- | # ESP-MQTT sample application (See the README.md file in the upper level 'examples' directory for more information about examples.) diff --git a/tools/test_apps/protocols/mqtt/build_test/README.md b/tools/test_apps/protocols/mqtt/build_test/README.md index 9c8a790..b7a5eb4 100644 --- a/tools/test_apps/protocols/mqtt/build_test/README.md +++ b/tools/test_apps/protocols/mqtt/build_test/README.md @@ -1,5 +1,5 @@ -| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C5 | ESP32-C6 | ESP32-C61 | ESP32-H2 | ESP32-H21 | ESP32-P4 | ESP32-S2 | ESP32-S3 | -| ----------------- | ----- | -------- | -------- | -------- | -------- | --------- | -------- | --------- | -------- | -------- | -------- | +| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C5 | ESP32-C6 | ESP32-C61 | ESP32-H2 | ESP32-H21 | ESP32-H4 | ESP32-P4 | ESP32-S2 | ESP32-S3 | +| ----------------- | ----- | -------- | -------- | -------- | -------- | --------- | -------- | --------- | -------- | -------- | -------- | -------- | # Build only test for C++ From 204f57f15462aafae0ceee9da60990cf5b5e93bc Mon Sep 17 00:00:00 2001 From: Chen Jichang Date: Fri, 21 Mar 2025 11:54:08 +0800 Subject: [PATCH 220/231] feat(esp32h4): disable unsupported build --- examples/protocols/mqtt/custom_outbox/README.md | 4 ++-- examples/protocols/mqtt/ssl/README.md | 4 ++-- examples/protocols/mqtt/ssl_mutual_auth/README.md | 4 ++-- examples/protocols/mqtt/ssl_psk/README.md | 4 ++-- examples/protocols/mqtt/tcp/README.md | 4 ++-- examples/protocols/mqtt/ws/README.md | 4 ++-- examples/protocols/mqtt/wss/README.md | 4 ++-- examples/protocols/mqtt5/README.md | 4 ++-- 8 files changed, 16 insertions(+), 16 deletions(-) diff --git a/examples/protocols/mqtt/custom_outbox/README.md b/examples/protocols/mqtt/custom_outbox/README.md index e5c7e2a..5b64bcd 100644 --- a/examples/protocols/mqtt/custom_outbox/README.md +++ b/examples/protocols/mqtt/custom_outbox/README.md @@ -1,5 +1,5 @@ -| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C5 | ESP32-C6 | ESP32-C61 | ESP32-H2 | ESP32-H4 | ESP32-P4 | ESP32-S2 | ESP32-S3 | -| ----------------- | ----- | -------- | -------- | -------- | -------- | --------- | -------- | -------- | -------- | -------- | -------- | +| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C5 | ESP32-C6 | ESP32-C61 | ESP32-H2 | ESP32-P4 | ESP32-S2 | ESP32-S3 | +| ----------------- | ----- | -------- | -------- | -------- | -------- | --------- | -------- | -------- | -------- | -------- | # ESP-MQTT custom outbox sample application (See the README.md file in the upper level 'examples' directory for more information about examples.) diff --git a/examples/protocols/mqtt/ssl/README.md b/examples/protocols/mqtt/ssl/README.md index e2db747..0407eda 100644 --- a/examples/protocols/mqtt/ssl/README.md +++ b/examples/protocols/mqtt/ssl/README.md @@ -1,5 +1,5 @@ -| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C5 | ESP32-C6 | ESP32-C61 | ESP32-H2 | ESP32-H4 | ESP32-P4 | ESP32-S2 | ESP32-S3 | -| ----------------- | ----- | -------- | -------- | -------- | -------- | --------- | -------- | -------- | -------- | -------- | -------- | +| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C5 | ESP32-C6 | ESP32-C61 | ESP32-H2 | ESP32-P4 | ESP32-S2 | ESP32-S3 | +| ----------------- | ----- | -------- | -------- | -------- | -------- | --------- | -------- | -------- | -------- | -------- | # ESP-MQTT SSL Sample application diff --git a/examples/protocols/mqtt/ssl_mutual_auth/README.md b/examples/protocols/mqtt/ssl_mutual_auth/README.md index c902864..4750ff1 100644 --- a/examples/protocols/mqtt/ssl_mutual_auth/README.md +++ b/examples/protocols/mqtt/ssl_mutual_auth/README.md @@ -1,5 +1,5 @@ -| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C5 | ESP32-C6 | ESP32-C61 | ESP32-H2 | ESP32-H4 | ESP32-P4 | ESP32-S2 | ESP32-S3 | -| ----------------- | ----- | -------- | -------- | -------- | -------- | --------- | -------- | -------- | -------- | -------- | -------- | +| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C5 | ESP32-C6 | ESP32-C61 | ESP32-H2 | ESP32-P4 | ESP32-S2 | ESP32-S3 | +| ----------------- | ----- | -------- | -------- | -------- | -------- | --------- | -------- | -------- | -------- | -------- | # ESP-MQTT SSL Sample application (mutual authentication) diff --git a/examples/protocols/mqtt/ssl_psk/README.md b/examples/protocols/mqtt/ssl_psk/README.md index b57ac22..d5dbb1b 100644 --- a/examples/protocols/mqtt/ssl_psk/README.md +++ b/examples/protocols/mqtt/ssl_psk/README.md @@ -1,5 +1,5 @@ -| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C5 | ESP32-C6 | ESP32-C61 | ESP32-H2 | ESP32-H4 | ESP32-P4 | ESP32-S2 | ESP32-S3 | -| ----------------- | ----- | -------- | -------- | -------- | -------- | --------- | -------- | -------- | -------- | -------- | -------- | +| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C5 | ESP32-C6 | ESP32-C61 | ESP32-H2 | ESP32-P4 | ESP32-S2 | ESP32-S3 | +| ----------------- | ----- | -------- | -------- | -------- | -------- | --------- | -------- | -------- | -------- | -------- | # ESP-MQTT SSL example with PSK verification diff --git a/examples/protocols/mqtt/tcp/README.md b/examples/protocols/mqtt/tcp/README.md index c8a11f8..cb9f2cc 100644 --- a/examples/protocols/mqtt/tcp/README.md +++ b/examples/protocols/mqtt/tcp/README.md @@ -1,5 +1,5 @@ -| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C5 | ESP32-C6 | ESP32-C61 | ESP32-H2 | ESP32-H4 | ESP32-P4 | ESP32-S2 | ESP32-S3 | -| ----------------- | ----- | -------- | -------- | -------- | -------- | --------- | -------- | -------- | -------- | -------- | -------- | +| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C5 | ESP32-C6 | ESP32-C61 | ESP32-H2 | ESP32-P4 | ESP32-S2 | ESP32-S3 | +| ----------------- | ----- | -------- | -------- | -------- | -------- | --------- | -------- | -------- | -------- | -------- | # ESP-MQTT sample application (See the README.md file in the upper level 'examples' directory for more information about examples.) diff --git a/examples/protocols/mqtt/ws/README.md b/examples/protocols/mqtt/ws/README.md index d41c736..5d1724f 100644 --- a/examples/protocols/mqtt/ws/README.md +++ b/examples/protocols/mqtt/ws/README.md @@ -1,5 +1,5 @@ -| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C5 | ESP32-C6 | ESP32-C61 | ESP32-H2 | ESP32-H4 | ESP32-P4 | ESP32-S2 | ESP32-S3 | -| ----------------- | ----- | -------- | -------- | -------- | -------- | --------- | -------- | -------- | -------- | -------- | -------- | +| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C5 | ESP32-C6 | ESP32-C61 | ESP32-H2 | ESP32-P4 | ESP32-S2 | ESP32-S3 | +| ----------------- | ----- | -------- | -------- | -------- | -------- | --------- | -------- | -------- | -------- | -------- | # ESP-MQTT MQTT over Websocket diff --git a/examples/protocols/mqtt/wss/README.md b/examples/protocols/mqtt/wss/README.md index 06e596e..b401d31 100644 --- a/examples/protocols/mqtt/wss/README.md +++ b/examples/protocols/mqtt/wss/README.md @@ -1,5 +1,5 @@ -| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C5 | ESP32-C6 | ESP32-C61 | ESP32-H2 | ESP32-H4 | ESP32-P4 | ESP32-S2 | ESP32-S3 | -| ----------------- | ----- | -------- | -------- | -------- | -------- | --------- | -------- | -------- | -------- | -------- | -------- | +| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C5 | ESP32-C6 | ESP32-C61 | ESP32-H2 | ESP32-P4 | ESP32-S2 | ESP32-S3 | +| ----------------- | ----- | -------- | -------- | -------- | -------- | --------- | -------- | -------- | -------- | -------- | # ESP-MQTT MQTT over WSS Sample application (See the README.md file in the upper level 'examples' directory for more information about examples.) diff --git a/examples/protocols/mqtt5/README.md b/examples/protocols/mqtt5/README.md index f8874fa..3e6c9c2 100644 --- a/examples/protocols/mqtt5/README.md +++ b/examples/protocols/mqtt5/README.md @@ -1,5 +1,5 @@ -| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C5 | ESP32-C6 | ESP32-C61 | ESP32-H2 | ESP32-H4 | ESP32-P4 | ESP32-S2 | ESP32-S3 | -| ----------------- | ----- | -------- | -------- | -------- | -------- | --------- | -------- | -------- | -------- | -------- | -------- | +| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C5 | ESP32-C6 | ESP32-C61 | ESP32-H2 | ESP32-P4 | ESP32-S2 | ESP32-S3 | +| ----------------- | ----- | -------- | -------- | -------- | -------- | --------- | -------- | -------- | -------- | -------- | # ESP-MQTT sample application (See the README.md file in the upper level 'examples' directory for more information about examples.) From a38c9e1ee05603653f6198618e4cd6fa1d3c39ae Mon Sep 17 00:00:00 2001 From: "harshal.patil" Date: Wed, 2 Apr 2025 13:07:02 +0530 Subject: [PATCH 221/231] fix(examples): Example CA certs must contain the Key Usage parameter - Example CA certificates that are used for self-signed client certificates need to include the Key Usage parameter. - Python3.13 changed the default context of the SSL context that is generated using ssl.create_default_context() by enabling the VERIFY_X509_STRICT flag by default --- .../mqtt/publish_connect_test/ca.crt | 34 +++++++++--------- .../mqtt/publish_connect_test/ca.der | Bin 783 -> 797 bytes 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/ca.crt b/tools/test_apps/protocols/mqtt/publish_connect_test/ca.crt index f53f647..e371e07 100644 --- a/tools/test_apps/protocols/mqtt/publish_connect_test/ca.crt +++ b/tools/test_apps/protocols/mqtt/publish_connect_test/ca.crt @@ -1,19 +1,19 @@ -----BEGIN CERTIFICATE----- -MIIDCzCCAfOgAwIBAgIUN9fSo2J8makY6P64QfrO+EcLLm0wDQYJKoZIhvcNAQEL -BQAwFDESMBAGA1UEAwwJRXNwcmVzc2lmMCAXDTI1MDEyODEzMjkxMVoYDzIyOTgx -MTEyMTMyOTExWjAUMRIwEAYDVQQDDAlFc3ByZXNzaWYwggEiMA0GCSqGSIb3DQEB -AQUAA4IBDwAwggEKAoIBAQDBJj+nzMM/xbd/fvF8vWPL9ZxZU45LaaxZHLGOldAz -Z77roxxKSfyZUpNbn9pn3qwVBuYEG6GnfKmikf6QFAfEriSVrqugRw383H3CkLxf -riVT0So+Y69wgbwcSURjTkvNn1HNzgsgFESr+wzwUTn5gc7bNrys3ZchoyKLSabZ -qV+jUwUSrWeQcF4kqVeSTp5kPBxD2SotYHP1hBbKATamYMKN0Khj3v91mmACO+Ss -AY31Riej1mY05h58vU/VmZW10FFb/EMZMNJkIkZgc6u298PxVLpyC446X6uzLehs -V2EDgCAYUQW191j/7Z8kcWSfo96nqP7uHx/2fz2qlLCzAgMBAAGjUzBRMB0GA1Ud -DgQWBBQn5CyOO5LXQ60QRxTRaHZeFdNrVjAfBgNVHSMEGDAWgBQn5CyOO5LXQ60Q -RxTRaHZeFdNrVjAPBgNVHRMBAf8EBTADAQH/MA0GCSqGSIb3DQEBCwUAA4IBAQAq -hKN1jrogQLY7AtnUaXb7eKFwI7k9NR6BZeXmE+xGCz02sUHNpzjuwo2oRacDZHZY -oEuwEM+zzWGV4x6cKriNUJ0G9uJwfFkCBZfWaNhyZ+r+3DqL4pH5kCcbQ3ZM+Mfk -dBOr9BUs7q4yZHRngmsi5lff0K6GC/uKC8bd9AKNs9I11g7CnJL2arf/GlZJrvTg -Lk7H7MT65SsAeX2MhifWl0urb20PNjDooQaki3qtApCgrUaIAtTUAP9p3jf8H0RT -rkfoVBxQtTDZltA+xelilUHcmj7pM9105/U6n0hD4yRQgemqaeSCbEo4ST2zm0pE -B4mjbFam7oBWdXdEDF1p +MIIDGTCCAgGgAwIBAgIUY6kAA+U+ZPIJYIff8dlbi6NCzKswDQYJKoZIhvcNAQEL +BQAwFDESMBAGA1UEAwwJRXNwcmVzc2lmMB4XDTI1MDQwMjA1MjcwMloXDTM1MDMz +MTA1MjcwMlowFDESMBAGA1UEAwwJRXNwcmVzc2lmMIIBIjANBgkqhkiG9w0BAQEF +AAOCAQ8AMIIBCgKCAQEAwSY/p8zDP8W3f37xfL1jy/WcWVOOS2msWRyxjpXQM2e+ +66McSkn8mVKTW5/aZ96sFQbmBBuhp3ypopH+kBQHxK4kla6roEcN/Nx9wpC8X64l +U9EqPmOvcIG8HElEY05LzZ9Rzc4LIBREq/sM8FE5+YHO2za8rN2XIaMii0mm2alf +o1MFEq1nkHBeJKlXkk6eZDwcQ9kqLWBz9YQWygE2pmDCjdCoY97/dZpgAjvkrAGN +9UYno9ZmNOYefL1P1ZmVtdBRW/xDGTDSZCJGYHOrtvfD8VS6cguOOl+rsy3obFdh +A4AgGFEFtfdY/+2fJHFkn6Pep6j+7h8f9n89qpSwswIDAQABo2MwYTAdBgNVHQ4E +FgQUJ+QsjjuS10OtEEcU0Wh2XhXTa1YwHwYDVR0jBBgwFoAUJ+QsjjuS10OtEEcU +0Wh2XhXTa1YwDwYDVR0TAQH/BAUwAwEB/zAOBgNVHQ8BAf8EBAMCAoQwDQYJKoZI +hvcNAQELBQADggEBABKWpI+QdMYoDwyssIbfwpbqJxb5M1w3PLnMsPzg1d5aEqLh +zwN9EnEQ5TxfeC7Lxdv3hKEGtif/aVxBhs48wPSxD7Fuw17kX6P4l9Cu9Ro2+1Oy +0lUxHi61xXxf7zVkdPQ0JLXdSMUvSUuKfvBtHCwEfdC+lsamxIDmCJys69kDhsCM +VJzY8Yz4MA9WOY3Z2YYMRp6ryFBZ9UgSUEnFxSOpggymkcM5mNxod1jshvSJ3FDG +dmvfbmK0+dN3rCiooORsIYVbopAYxralavA9IY24oiULE+GyVt5pNSONmJ96Y7GK +dL72B8RxX+jUSzgu/N3D1DwbPHP/xRiI7EqaYvg= -----END CERTIFICATE----- diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/ca.der b/tools/test_apps/protocols/mqtt/publish_connect_test/ca.der index 309539220877cfc621e7497288868677881ea46b..8f0d12006aceaf0753b903aba07c6822dfbb2fe2 100644 GIT binary patch delta 380 zcmeBYo6Dwb(8Mfh(8R>JfSHMriAf}RB?I$QyOd9y3GMek-i+>E>~v=JL}fKOIdNVi zQv(wNBLh<-a|5F&ab9Cn17l-D2zO(q9V2tHLE_|9j6wB$Y|Npu{EUqMSy-5vm|6^Y z**LY@JlekVGBR?rGB7tWGBOBFThc$F3?@fCWfkKX>?vXE_?`v1%r$F_4e2fl3N-))HElzcH!*?QOGsJ^FnSKWtP86B3|3;U)WTXv-38ONM8uWvH9 z9q0*}bK_&r4+H)%%ifze+j!jOtv(SD`PD-x!1L%)<&{l5%O)PSoN*_kJmO8;m(Du@ z$I7zr=Ou0VdAWRz#)<_`aui#m7fp~jwry$F2V2G79g9@Cg&%GTyO(LIs@ywcepT|u ku9AJ<*pC#(zqsOUq4($R;VU-MHpTysN_4#Onw9he0B_r-hX4Qo delta 366 zcmbQs*3YJF(8SDb(8Tz80W%XL6O)Mf^-GJBYG$sKc=2zCPG3&Tl@OIQ6mI9IYL_0dv{DJu0Y)WMZ9uE#pQ}`m29;thygf{5e5g+PTc< z$MGj6!mGcC>bzTLlu}Zk-juEMEd2h3b#2_gySR_t{le6{`I6~1zC&{+eaqVZUnj+MTktmR)d? Date: Wed, 30 Apr 2025 18:35:39 +0800 Subject: [PATCH 222/231] docs: Fix some typos --- components/mqtt/test_apps/common/test_mqtt_connection.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/mqtt/test_apps/common/test_mqtt_connection.c b/components/mqtt/test_apps/common/test_mqtt_connection.c index 0e82003..a6cb702 100644 --- a/components/mqtt/test_apps/common/test_mqtt_connection.c +++ b/components/mqtt/test_apps/common/test_mqtt_connection.c @@ -112,7 +112,7 @@ void connect_test_fixture_setup(void) // 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 + // register user defined event handlers 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 From 1741b21561ce936a074b7228f17602e398ef94a6 Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Tue, 6 May 2025 20:35:48 +0200 Subject: [PATCH 223/231] change(examples): temp. disable examples which need esp_wifi_remote Will be re-enabled after adding IDF 6.0 compatibility in esp_wifi_remote. --- examples/protocols/mqtt/tcp/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/protocols/mqtt/tcp/README.md b/examples/protocols/mqtt/tcp/README.md index cb9f2cc..b77bfea 100644 --- a/examples/protocols/mqtt/tcp/README.md +++ b/examples/protocols/mqtt/tcp/README.md @@ -1,5 +1,5 @@ -| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C5 | ESP32-C6 | ESP32-C61 | ESP32-H2 | ESP32-P4 | ESP32-S2 | ESP32-S3 | -| ----------------- | ----- | -------- | -------- | -------- | -------- | --------- | -------- | -------- | -------- | -------- | +| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C5 | ESP32-C6 | ESP32-C61 | ESP32-S2 | ESP32-S3 | +| ----------------- | ----- | -------- | -------- | -------- | -------- | --------- | -------- | -------- | # ESP-MQTT sample application (See the README.md file in the upper level 'examples' directory for more information about examples.) From de8ab91c192d902c5be61f3b53c56167958806b9 Mon Sep 17 00:00:00 2001 From: Euripedes Rocha Date: Tue, 6 May 2025 12:35:15 +0200 Subject: [PATCH 224/231] change(mqtt): Adds retry on publish connect test case This test case fails mostly for network related issues, with retrys we might avoid the failure of the whole job. --- .../mqtt/publish_connect_test/CMakeLists.txt | 1 + .../publish_connect_test/main/CMakeLists.txt | 3 +- .../publish_connect_test/main/publish_test.c | 63 +++++++++------- .../pytest_mqtt_publish_app.py | 72 ++++++++++++++----- 4 files changed, 96 insertions(+), 43 deletions(-) diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/CMakeLists.txt b/tools/test_apps/protocols/mqtt/publish_connect_test/CMakeLists.txt index 4776e74..543259e 100644 --- a/tools/test_apps/protocols/mqtt/publish_connect_test/CMakeLists.txt +++ b/tools/test_apps/protocols/mqtt/publish_connect_test/CMakeLists.txt @@ -3,6 +3,7 @@ cmake_minimum_required(VERSION 3.16) include($ENV{IDF_PATH}/tools/cmake/project.cmake) +idf_build_set_property(MINIMAL_BUILD ON) project(mqtt_publish_connect_test) diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/main/CMakeLists.txt b/tools/test_apps/protocols/mqtt/publish_connect_test/main/CMakeLists.txt index 657a574..326ecd6 100644 --- a/tools/test_apps/protocols/mqtt/publish_connect_test/main/CMakeLists.txt +++ b/tools/test_apps/protocols/mqtt/publish_connect_test/main/CMakeLists.txt @@ -1,3 +1,4 @@ idf_component_register(SRCS "publish_test.c" "connect_test.c" "publish_connect_test.c" - INCLUDE_DIRS ".") + INCLUDE_DIRS "." + REQUIRES mqtt nvs_flash console esp_netif) target_compile_options(${COMPONENT_LIB} PRIVATE "-Wno-format") diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/main/publish_test.c b/tools/test_apps/protocols/mqtt/publish_connect_test/main/publish_test.c index 7d11ae3..fe373b6 100644 --- a/tools/test_apps/protocols/mqtt/publish_connect_test/main/publish_test.c +++ b/tools/test_apps/protocols/mqtt/publish_connect_test/main/publish_test.c @@ -67,31 +67,44 @@ static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_ ESP_LOGI(TAG, "MQTT_EVENT_DATA"); ESP_LOGI(TAG, "TOPIC=%.*s", event->topic_len, event->topic); ESP_LOGI(TAG, "ID=%d, total_len=%d, data_len=%d, current_data_offset=%d", event->msg_id, event->total_data_len, event->data_len, event->current_data_offset); - if (event->topic) { - actual_len = event->data_len; - msg_id = event->msg_id; - } else { - actual_len += event->data_len; - // check consistency with msg_id across multiple data events for single msg - if (msg_id != event->msg_id) { - ESP_LOGI(TAG, "Wrong msg_id in chunked message %d != %d", msg_id, event->msg_id); - abort(); - } - } - memcpy(test_data->received_data + event->current_data_offset, event->data, event->data_len); - if (actual_len == event->total_data_len) { - if (0 == memcmp(test_data->received_data, test_data->expected, test_data->expected_size)) { - memset(test_data->received_data, 0, test_data->expected_size); - test_data->nr_of_msg_received ++; - if (test_data->nr_of_msg_received == test_data->nr_of_msg_expected) { - ESP_LOGI(TAG, "Correct pattern received exactly x times"); - ESP_LOGI(TAG, "Test finished correctly!"); - } - } else { - ESP_LOGE(TAG, "FAILED!"); - abort(); - } - } + if (event->current_data_offset == 0) { + actual_len = event->data_len; + msg_id = event->msg_id; + if (event->total_data_len != test_data->expected_size) { + ESP_LOGE(TAG, "Incorrect message size: %d != %d", event->total_data_len, test_data->expected_size); + abort(); + } + } else { + actual_len += event->data_len; + // check consistency with msg_id across multiple data events for single msg + if (msg_id != event->msg_id) { + ESP_LOGE(TAG, "Wrong msg_id in chunked message %d != %d", msg_id, event->msg_id); + abort(); + } + } + if (event->current_data_offset + event->data_len > test_data->expected_size) { + ESP_LOGE(TAG, "Buffer overflow detected: offset %d + data_len %d > buffer size %d", event->current_data_offset, event->data_len, test_data->expected_size); + abort(); + } + if (memcmp(test_data->expected + event->current_data_offset, event->data, event->data_len) != 0) { + ESP_LOGE(TAG, "Data mismatch at offset %d: \n expected %.*s, \n got %.*s", event->current_data_offset, event->data_len, test_data->expected + event->current_data_offset, event->data_len, event->data); + abort(); + } + + memcpy(test_data->received_data + event->current_data_offset, event->data, event->data_len); + if (actual_len == event->total_data_len) { + if (0 == memcmp(test_data->received_data, test_data->expected, test_data->expected_size)) { + memset(test_data->received_data, 0, test_data->expected_size); + test_data->nr_of_msg_received++; + if (test_data->nr_of_msg_received == test_data->nr_of_msg_expected) { + ESP_LOGI(TAG, "Correct pattern received exactly x times"); + ESP_LOGI(TAG, "Test finished correctly!"); + } + } else { + ESP_LOGE(TAG, "FAILED!"); + abort(); + } + } break; case MQTT_EVENT_ERROR: ESP_LOGE(TAG, "MQTT_EVENT_ERROR"); diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/pytest_mqtt_publish_app.py b/tools/test_apps/protocols/mqtt/publish_connect_test/pytest_mqtt_publish_app.py index 4eb25d1..2c39034 100644 --- a/tools/test_apps/protocols/mqtt/publish_connect_test/pytest_mqtt_publish_app.py +++ b/tools/test_apps/protocols/mqtt/publish_connect_test/pytest_mqtt_publish_app.py @@ -8,6 +8,7 @@ import random import re import ssl import string +import time from itertools import count from itertools import product from threading import Event @@ -15,8 +16,8 @@ from threading import Lock from typing import Any from typing import Dict from typing import List -from typing import no_type_check from typing import Tuple +from typing import no_type_check import paho.mqtt.client as mqtt import pexpect @@ -61,7 +62,7 @@ class MqttPublisher(mqtt.Client): def on_connect_fail(self, mqttc: Any, obj: Any) -> None: logging.error('Connect failed') - def on_message(self, mqttc: Any, userdata: Any, msg: mqtt.MQTTMessage) -> None: + def on_message(self, mqttc: mqtt.Client, obj: Any, msg: mqtt.MQTTMessage) -> None: payload = msg.payload.decode('utf-8') if payload == self.expected_data: self.received += 1 @@ -70,8 +71,9 @@ class MqttPublisher(mqtt.Client): else: differences = len(list(filter(lambda data: data[0] != data[1], zip(payload, self.expected_data)))) logging.error( - f'Payload differ in {differences} positions from expected data. received size: {len(payload)} expected size:' - f'{len(self.expected_data)}' + f'Payload on topic "{msg.topic}" (QoS {msg.qos}) differs in {differences} positions ' + 'from expected data. ' + f'Received size: {len(payload)}, expected size: {len(self.expected_data)}.' ) logging.info(f'Repetitions: {payload.count(self.config["pattern"])}') logging.info(f'Pattern: {self.config["pattern"]}') @@ -85,6 +87,7 @@ class MqttPublisher(mqtt.Client): qos = self.config['qos'] broker_host = self.config['broker_host_' + self.config['transport']] broker_port = self.config['broker_port_' + self.config['transport']] + connect_timeout_seconds = self.config.get('client_connect_timeout', 30) try: self.print_details('Connecting...') @@ -93,14 +96,17 @@ class MqttPublisher(mqtt.Client): self.tls_insecure_set(True) self.event_client_connected.clear() self.loop_start() - self.connect(broker_host, broker_port, 60) + self.connect(broker_host, broker_port, 60) # paho's keepalive except Exception: self.print_details(f'ENV_TEST_FAILURE: Unexpected error while connecting to broker {broker_host}') raise self.print_details(f'Connecting py-client to broker {broker_host}:{broker_port}...') - if not self.event_client_connected.wait(timeout=30): - raise ValueError(f'ENV_TEST_FAILURE: Test script cannot connect to broker: {broker_host}') + if not self.event_client_connected.wait(timeout=connect_timeout_seconds): + raise ValueError( + f'ENV_TEST_FAILURE: Test script cannot connect to broker: {broker_host} ' + f'within {connect_timeout_seconds}s' + ) self.event_client_got_all.clear() result, self.subscribe_mid = self.subscribe(self.config['subscribe_topic'], qos) assert result == 0 @@ -148,7 +154,11 @@ def get_configurations(dut: Dut, test_case: Any) -> Dict[str, Any]: publish_cfg['pattern'] = ''.join( random.choice(string.ascii_uppercase + string.ascii_lowercase + string.digits) for _ in range(DEFAULT_MSG_SIZE) ) + publish_cfg['client_connect_timeout'] = 30 + publish_cfg['dut_subscribe_timeout'] = 60 + publish_cfg['publish_ack_timeout'] = 60 publish_cfg['test_timeout'] = get_timeout(test_case) + unique_topic = ''.join( random.choice(string.ascii_uppercase + string.ascii_lowercase) for _ in range(DEFAULT_MSG_SIZE) ) @@ -159,9 +169,10 @@ def get_configurations(dut: Dut, test_case: Any) -> Dict[str, Any]: @contextlib.contextmanager -def connected_and_subscribed(dut: Dut) -> Any: +def connected_and_subscribed(dut: Dut, config: Dict[str, Any]) -> Any: dut.write('start') - dut.expect(re.compile(rb'MQTT_EVENT_SUBSCRIBED'), timeout=60) + dut_subscribe_timeout = config.get('dut_subscribe_timeout', 60) + dut.expect(re.compile(rb'MQTT_EVENT_SUBSCRIBED'), timeout=dut_subscribe_timeout) yield dut.write('stop') @@ -177,6 +188,7 @@ def get_scenarios() -> List[Dict[str, int]]: continue break if not scenarios: # No message sizes present in the env - set defaults + logging.info('Using predefined cases') scenarios = [ {'msg_len': 0, 'nr_of_msgs': 5}, # zero-sized messages {'msg_len': 2, 'nr_of_msgs': 5}, # short messages @@ -201,13 +213,15 @@ def run_publish_test_case(dut: Dut, config: Any) -> None: logging.info( f'Starting Publish test: transport:{config["transport"]}, qos:{config["qos"]},' f'nr_of_msgs:{config["scenario"]["nr_of_msgs"]},' - f' msg_size:{config["scenario"]["msg_len"] * DEFAULT_MSG_SIZE}, enqueue:{config["enqueue"]}' + f' msg_size:{config["scenario"]["msg_len"]}, enqueue:{config["enqueue"]}' ) dut.write( - f'publish_setup {config["transport"]} {config["publish_topic"]} {config["subscribe_topic"]} {config["pattern"]} {config["scenario"]["msg_len"]}' + f'publish_setup {config["transport"]} {config["publish_topic"]}' + f' {config["subscribe_topic"]} {config["pattern"]} {config["scenario"]["msg_len"]}' ) - with MqttPublisher(config) as publisher, connected_and_subscribed(dut): - assert publisher.event_client_subscribed.wait(timeout=config['test_timeout']), 'Runner failed to subscribe' + with MqttPublisher(config) as publisher, connected_and_subscribed(dut, config): + py_client_subscribe_timeout = config.get('py_client_subscribe_timeout', config['test_timeout']) + assert publisher.event_client_subscribed.wait(timeout=py_client_subscribe_timeout), 'Runner failed to subscribe' msgs_published: List[mqtt.MQTTMessageInfo] = [] dut.write(f'publish {config["scenario"]["nr_of_msgs"]} {config["qos"]} {config["enqueue"]}') assert publisher.event_client_got_all.wait(timeout=config['test_timeout']), ( @@ -222,11 +236,33 @@ def run_publish_test_case(dut: Dut, config: Any) -> None: msg = publisher.publish(topic=config['publish_topic'], payload=payload, qos=config['qos']) if config['qos'] > 0: msgs_published.append(msg) - logging.info(f'Published: {len(msgs_published)}') - while msgs_published: - msgs_published = [msg for msg in msgs_published if not msg.is_published()] + logging.info(f'Published: {len(msgs_published)} messages from script with QoS > 0 needing ACK.') - logging.info('All messages from runner published') + if msgs_published: + publish_ack_timeout_seconds = config.get('publish_ack_timeout', 60) # Default 60s, make configurable + ack_wait_start_time = time.time() + initial_unacked_count = len(msgs_published) + logging.info(f'Waiting {initial_unacked_count} publish ack with timeout {publish_ack_timeout_seconds}s...') + + while msgs_published: + if time.time() - ack_wait_start_time > publish_ack_timeout_seconds: + unacked_mids = [msg.mid for msg in msgs_published if msg.mid is not None and not msg.is_published()] + logging.error( + f'Timeout waiting for publish acknowledgements. ' + f'{len(unacked_mids)} of {initial_unacked_count} messages remain unacknowledged. ' + f'Unacked MIDs: {unacked_mids}' + ) + # This will likely cause the test to fail at a later assertion, + # or you could raise an explicit error here. + # e.g. raise Exception('Timeout waiting for publish acknowledgements') + break + msgs_published = [msg for msg in msgs_published if not msg.is_published()] + if msgs_published: # Avoid busy-looping if list is not empty + time.sleep(0.1) # Brief pause + if not msgs_published: + logging.info('All script-published QoS > 0 messages acknowledged by broker.') + + logging.info('All messages from runner published (or timed out waiting for ACK).') try: dut.expect(re.compile(rb'Correct pattern received exactly x times'), timeout=config['test_timeout']) @@ -262,6 +298,7 @@ stress_test_cases = make_cases(transport_cases, stress_scenarios) @pytest.mark.parametrize('test_case', test_cases) @pytest.mark.parametrize('config', ['default'], indirect=True) @idf_parametrize('target', ['esp32'], indirect=['target']) +@pytest.mark.flaky(reruns=1, reruns_delay=1) def test_mqtt_publish(dut: Dut, test_case: Any) -> None: publish_cfg = get_configurations(dut, test_case) dut.expect(re.compile(rb'mqtt>'), timeout=30) @@ -273,6 +310,7 @@ def test_mqtt_publish(dut: Dut, test_case: Any) -> None: @pytest.mark.nightly_run @pytest.mark.parametrize('test_case', stress_test_cases) @pytest.mark.parametrize('config', ['default'], indirect=True) +@pytest.mark.flaky(reruns=1, reruns_delay=1) @idf_parametrize('target', ['esp32'], indirect=['target']) def test_mqtt_publish_stress(dut: Dut, test_case: Any) -> None: publish_cfg = get_configurations(dut, test_case) From ea6237dd8df16a8e3aa7b4ed65000a4b9a3152d3 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Tue, 13 May 2025 09:52:42 +0200 Subject: [PATCH 225/231] change(examples): Re-enable examples which need esp_wifi_remote This reverts commit b40958fd363733d2b0136f63e45016442b9baae9. --- examples/protocols/mqtt/tcp/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/protocols/mqtt/tcp/README.md b/examples/protocols/mqtt/tcp/README.md index b77bfea..cb9f2cc 100644 --- a/examples/protocols/mqtt/tcp/README.md +++ b/examples/protocols/mqtt/tcp/README.md @@ -1,5 +1,5 @@ -| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C5 | ESP32-C6 | ESP32-C61 | ESP32-S2 | ESP32-S3 | -| ----------------- | ----- | -------- | -------- | -------- | -------- | --------- | -------- | -------- | +| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C5 | ESP32-C6 | ESP32-C61 | ESP32-H2 | ESP32-P4 | ESP32-S2 | ESP32-S3 | +| ----------------- | ----- | -------- | -------- | -------- | -------- | --------- | -------- | -------- | -------- | -------- | # ESP-MQTT sample application (See the README.md file in the upper level 'examples' directory for more information about examples.) From 0cbd2fbcf3c02a11efa1e7a32272529645b3dfb3 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Tue, 13 May 2025 16:24:56 +0200 Subject: [PATCH 226/231] fix(examples): Update wifi-remote dependency to include IDF-v6 Use esp_wifi_remote version < v2.0 Planned release v1.0 to support remote and native coexistence, which should be still compatible with IDFv5.x and IDFv6.x --- examples/protocols/mqtt/tcp/main/idf_component.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/protocols/mqtt/tcp/main/idf_component.yml b/examples/protocols/mqtt/tcp/main/idf_component.yml index 1b1f6db..59805d8 100644 --- a/examples/protocols/mqtt/tcp/main/idf_component.yml +++ b/examples/protocols/mqtt/tcp/main/idf_component.yml @@ -2,6 +2,6 @@ dependencies: protocol_examples_common: path: ${IDF_PATH}/examples/common_components/protocol_examples_common espressif/esp_wifi_remote: - version: ">=0.1.12" + version: ">=0.10,<2.0" rules: - if: "target in [esp32p4, esp32h2]" From dfeb06f99f0cf1d7243d43194fe1aa4d00936fa4 Mon Sep 17 00:00:00 2001 From: "Hossein.M" Date: Tue, 12 Nov 2024 09:58:07 +0330 Subject: [PATCH 227/231] fix: fix build failure when project name is not mqtt_ssl fixes: #14870 Closes https://github.com/espressif/esp-idf/pull/14871 --- examples/protocols/mqtt/ssl/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/protocols/mqtt/ssl/CMakeLists.txt b/examples/protocols/mqtt/ssl/CMakeLists.txt index 699c86d..e41e1b8 100644 --- a/examples/protocols/mqtt/ssl/CMakeLists.txt +++ b/examples/protocols/mqtt/ssl/CMakeLists.txt @@ -8,4 +8,4 @@ include($ENV{IDF_PATH}/tools/cmake/project.cmake) idf_build_set_property(MINIMAL_BUILD ON) project(mqtt_ssl) -target_add_binary_data(mqtt_ssl.elf "main/mqtt_eclipseprojects_io.pem" TEXT) +target_add_binary_data(${PROJECT_NAME}.elf "main/mqtt_eclipseprojects_io.pem" TEXT) From 6b609bcdee950e859fbd7b21c7a400d2f5b2ee17 Mon Sep 17 00:00:00 2001 From: Euripedes Rocha Date: Wed, 18 Jun 2025 09:59:17 +0200 Subject: [PATCH 228/231] fix(mqtt): Make example binary data consistent Improves user experience when copying examples to use as starting point. --- examples/protocols/mqtt/wss/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/protocols/mqtt/wss/CMakeLists.txt b/examples/protocols/mqtt/wss/CMakeLists.txt index 158bc62..e62d88e 100644 --- a/examples/protocols/mqtt/wss/CMakeLists.txt +++ b/examples/protocols/mqtt/wss/CMakeLists.txt @@ -8,4 +8,4 @@ include($ENV{IDF_PATH}/tools/cmake/project.cmake) idf_build_set_property(MINIMAL_BUILD ON) project(mqtt_websocket_secure) -target_add_binary_data(mqtt_websocket_secure.elf "main/mqtt_eclipseprojects_io.pem" TEXT) +target_add_binary_data(${PROJECT_NAME}.elf "main/mqtt_eclipseprojects_io.pem" TEXT) From f116643d5aae66e5d2b6756841f16a3a43b05b02 Mon Sep 17 00:00:00 2001 From: Euripedes Rocha Date: Wed, 18 Jun 2025 14:19:46 +0200 Subject: [PATCH 229/231] feat(mqtt): Add reason code to SUBSCRIBED event in examples --- examples/protocols/mqtt/ssl/main/app_main.c | 2 +- examples/protocols/mqtt/ssl_ds/main/app_main.c | 2 +- examples/protocols/mqtt/ssl_mutual_auth/main/app_main.c | 2 +- examples/protocols/mqtt/ssl_psk/main/app_main.c | 2 +- examples/protocols/mqtt/tcp/main/app_main.c | 2 +- examples/protocols/mqtt/ws/main/app_main.c | 2 +- examples/protocols/mqtt/wss/main/app_main.c | 2 +- examples/protocols/mqtt5/main/app_main.c | 2 +- 8 files changed, 8 insertions(+), 8 deletions(-) diff --git a/examples/protocols/mqtt/ssl/main/app_main.c b/examples/protocols/mqtt/ssl/main/app_main.c index 20cf180..ed5e04f 100644 --- a/examples/protocols/mqtt/ssl/main/app_main.c +++ b/examples/protocols/mqtt/ssl/main/app_main.c @@ -83,7 +83,7 @@ static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_ break; case MQTT_EVENT_SUBSCRIBED: - ESP_LOGI(TAG, "MQTT_EVENT_SUBSCRIBED, msg_id=%d", event->msg_id); + ESP_LOGI(TAG, "MQTT_EVENT_SUBSCRIBED, msg_id=%d, return code=0x%02x ", event->msg_id, (uint8_t)*event->data); msg_id = esp_mqtt_client_publish(client, "/topic/qos0", "data", 0, 0, 0); ESP_LOGI(TAG, "sent publish successful, msg_id=%d", msg_id); break; diff --git a/examples/protocols/mqtt/ssl_ds/main/app_main.c b/examples/protocols/mqtt/ssl_ds/main/app_main.c index 758995b..7b54cfb 100644 --- a/examples/protocols/mqtt/ssl_ds/main/app_main.c +++ b/examples/protocols/mqtt/ssl_ds/main/app_main.c @@ -69,7 +69,7 @@ static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_ break; case MQTT_EVENT_SUBSCRIBED: - ESP_LOGI(TAG, "MQTT_EVENT_SUBSCRIBED, msg_id=%d", event->msg_id); + ESP_LOGI(TAG, "MQTT_EVENT_SUBSCRIBED, msg_id=%d, return code=0x%02x ", event->msg_id, (uint8_t)*event->data); msg_id = esp_mqtt_client_publish(client, "/topic/qos0", "data", 0, 0, 0); ESP_LOGI(TAG, "sent publish successful, msg_id=%d", msg_id); break; diff --git a/examples/protocols/mqtt/ssl_mutual_auth/main/app_main.c b/examples/protocols/mqtt/ssl_mutual_auth/main/app_main.c index 02956f6..90ba71c 100644 --- a/examples/protocols/mqtt/ssl_mutual_auth/main/app_main.c +++ b/examples/protocols/mqtt/ssl_mutual_auth/main/app_main.c @@ -78,7 +78,7 @@ static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_ break; case MQTT_EVENT_SUBSCRIBED: - ESP_LOGI(TAG, "MQTT_EVENT_SUBSCRIBED, msg_id=%d", event->msg_id); + ESP_LOGI(TAG, "MQTT_EVENT_SUBSCRIBED, msg_id=%d, return code=0x%02x ", event->msg_id, (uint8_t)*event->data); msg_id = esp_mqtt_client_publish(client, "/topic/qos0", "data", 0, 0, 0); ESP_LOGI(TAG, "sent publish successful, msg_id=%d", msg_id); break; diff --git a/examples/protocols/mqtt/ssl_psk/main/app_main.c b/examples/protocols/mqtt/ssl_psk/main/app_main.c index 4d2bb10..ee9e818 100644 --- a/examples/protocols/mqtt/ssl_psk/main/app_main.c +++ b/examples/protocols/mqtt/ssl_psk/main/app_main.c @@ -85,7 +85,7 @@ static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_ break; case MQTT_EVENT_SUBSCRIBED: - ESP_LOGI(TAG, "MQTT_EVENT_SUBSCRIBED, msg_id=%d", event->msg_id); + ESP_LOGI(TAG, "MQTT_EVENT_SUBSCRIBED, msg_id=%d, return code=0x%02x ", event->msg_id, (uint8_t)*event->data); msg_id = esp_mqtt_client_publish(client, "/topic/qos0", "data", 0, 0, 0); ESP_LOGI(TAG, "sent publish successful, msg_id=%d", msg_id); break; diff --git a/examples/protocols/mqtt/tcp/main/app_main.c b/examples/protocols/mqtt/tcp/main/app_main.c index 9a13adb..396aca6 100644 --- a/examples/protocols/mqtt/tcp/main/app_main.c +++ b/examples/protocols/mqtt/tcp/main/app_main.c @@ -67,7 +67,7 @@ static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_ break; case MQTT_EVENT_SUBSCRIBED: - ESP_LOGI(TAG, "MQTT_EVENT_SUBSCRIBED, msg_id=%d", event->msg_id); + ESP_LOGI(TAG, "MQTT_EVENT_SUBSCRIBED, msg_id=%d, return code=0x%02x ", event->msg_id, (uint8_t)*event->data); msg_id = esp_mqtt_client_publish(client, "/topic/qos0", "data", 0, 0, 0); ESP_LOGI(TAG, "sent publish successful, msg_id=%d", msg_id); break; diff --git a/examples/protocols/mqtt/ws/main/app_main.c b/examples/protocols/mqtt/ws/main/app_main.c index cebfb9a..f518fdf 100644 --- a/examples/protocols/mqtt/ws/main/app_main.c +++ b/examples/protocols/mqtt/ws/main/app_main.c @@ -73,7 +73,7 @@ static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_ break; case MQTT_EVENT_SUBSCRIBED: - ESP_LOGI(TAG, "MQTT_EVENT_SUBSCRIBED, msg_id=%d", event->msg_id); + ESP_LOGI(TAG, "MQTT_EVENT_SUBSCRIBED, msg_id=%d, return code=0x%02x ", event->msg_id, (uint8_t)*event->data); msg_id = esp_mqtt_client_publish(client, "/topic/qos0", "data", 0, 0, 0); ESP_LOGI(TAG, "sent publish successful, msg_id=%d", msg_id); break; diff --git a/examples/protocols/mqtt/wss/main/app_main.c b/examples/protocols/mqtt/wss/main/app_main.c index 168b46a..3de96c0 100644 --- a/examples/protocols/mqtt/wss/main/app_main.c +++ b/examples/protocols/mqtt/wss/main/app_main.c @@ -61,7 +61,7 @@ static esp_err_t mqtt_event_handler_cb(esp_mqtt_event_handle_t event) break; case MQTT_EVENT_SUBSCRIBED: - ESP_LOGI(TAG, "MQTT_EVENT_SUBSCRIBED, msg_id=%d", event->msg_id); + ESP_LOGI(TAG, "MQTT_EVENT_SUBSCRIBED, msg_id=%d, return code=0x%02x ", event->msg_id, (uint8_t)*event->data); msg_id = esp_mqtt_client_publish(client, "/topic/qos0", "data", 0, 0, 0); ESP_LOGI(TAG, "sent publish successful, msg_id=%d", msg_id); break; diff --git a/examples/protocols/mqtt5/main/app_main.c b/examples/protocols/mqtt5/main/app_main.c index 746017c..e82fd70 100644 --- a/examples/protocols/mqtt5/main/app_main.c +++ b/examples/protocols/mqtt5/main/app_main.c @@ -142,7 +142,7 @@ static void mqtt5_event_handler(void *handler_args, esp_event_base_t base, int32 print_user_property(event->property->user_property); break; case MQTT_EVENT_SUBSCRIBED: - ESP_LOGI(TAG, "MQTT_EVENT_SUBSCRIBED, msg_id=%d", event->msg_id); + ESP_LOGI(TAG, "MQTT_EVENT_SUBSCRIBED, msg_id=%d, reason code=0x%02x ", event->msg_id, (uint8_t)*event->data); print_user_property(event->property->user_property); esp_mqtt5_client_set_publish_property(client, &publish_property); msg_id = esp_mqtt_client_publish(client, "/topic/qos0", "data", 0, 0, 0); From 7edc15287cbcd98b3274f5447557cf704d7eb9db Mon Sep 17 00:00:00 2001 From: Euripedes Rocha Date: Mon, 7 Jul 2025 11:10:22 +0200 Subject: [PATCH 230/231] fix(mqtt): Adds sdkconfig to use test broker Changes the configuration to make test to use internal broker on CI runs. --- components/mqtt/test_apps/test_mqtt/sdkconfig.ci.default | 4 ++++ components/mqtt/test_apps/test_mqtt5/sdkconfig.ci.default | 4 ++++ 2 files changed, 8 insertions(+) create mode 100644 components/mqtt/test_apps/test_mqtt/sdkconfig.ci.default create mode 100644 components/mqtt/test_apps/test_mqtt5/sdkconfig.ci.default diff --git a/components/mqtt/test_apps/test_mqtt/sdkconfig.ci.default b/components/mqtt/test_apps/test_mqtt/sdkconfig.ci.default new file mode 100644 index 0000000..1d40815 --- /dev/null +++ b/components/mqtt/test_apps/test_mqtt/sdkconfig.ci.default @@ -0,0 +1,4 @@ +CONFIG_MQTT_TEST_BROKER_URI="mqtt://${EXAMPLE_MQTT_BROKER_TCP}" +CONFIG_MQTT5_TEST_BROKER_URI="mqtt://${EXAMPLE_MQTT_BROKER_TCP}" +CONFIG_ESP_TASK_WDT_EN=n +CONFIG_UNITY_ENABLE_FIXTURE=y diff --git a/components/mqtt/test_apps/test_mqtt5/sdkconfig.ci.default b/components/mqtt/test_apps/test_mqtt5/sdkconfig.ci.default new file mode 100644 index 0000000..1d40815 --- /dev/null +++ b/components/mqtt/test_apps/test_mqtt5/sdkconfig.ci.default @@ -0,0 +1,4 @@ +CONFIG_MQTT_TEST_BROKER_URI="mqtt://${EXAMPLE_MQTT_BROKER_TCP}" +CONFIG_MQTT5_TEST_BROKER_URI="mqtt://${EXAMPLE_MQTT_BROKER_TCP}" +CONFIG_ESP_TASK_WDT_EN=n +CONFIG_UNITY_ENABLE_FIXTURE=y From 2925288e514a61bdd27aa90e68545275b6f7eeb2 Mon Sep 17 00:00:00 2001 From: Euripedes Rocha Filho Date: Mon, 22 Sep 2025 12:39:29 +0200 Subject: [PATCH 231/231] chore: Reorganize file structure This just move files to their expected locations. --- .gitlab-ci.yml | 2 +- .../mqtt/host_test/mocks/heap/CMakeLists.txt | 4 ---- .../mqtt/host_test/mocks/heap/heap_mock.c | 16 ---------------- .../protocols/mqtt.rst => index.rst} | 0 .../protocols/mqtt.rst => index.rst} | 0 .../mqtt => }/custom_outbox/CMakeLists.txt | 0 .../mqtt => }/custom_outbox/README.md | 0 .../mqtt => }/custom_outbox/main/CMakeLists.txt | 0 .../custom_outbox/main/Kconfig.projbuild | 0 .../mqtt => }/custom_outbox/main/app_main.c | 0 .../custom_outbox/main/custom_outbox.cpp | 0 .../custom_outbox/main/idf_component.yml | 0 .../custom_outbox/sdkconfig.ci.esp32c6 | 0 .../mqtt => }/custom_outbox/sdkconfig.defaults | 0 examples/{protocols => }/mqtt5/CMakeLists.txt | 0 examples/{protocols => }/mqtt5/README.md | 0 .../mqtt/tcp => mqtt5}/main/CMakeLists.txt | 0 .../mqtt/tcp => mqtt5}/main/Kconfig.projbuild | 0 examples/{protocols => }/mqtt5/main/app_main.c | 0 .../mqtt/ssl => mqtt5}/main/idf_component.yml | 0 examples/{protocols => }/mqtt5/pytest_mqtt5.py | 0 examples/{protocols => }/mqtt5/sdkconfig.ci | 0 .../{protocols => }/mqtt5/sdkconfig.defaults | 0 .../{protocols/mqtt => }/ssl/CMakeLists.txt | 0 examples/{protocols/mqtt => }/ssl/README.md | 0 .../mqtt => }/ssl/main/CMakeLists.txt | 0 .../mqtt => }/ssl/main/Kconfig.projbuild | 0 .../{protocols/mqtt => }/ssl/main/app_main.c | 0 .../main/idf_component.yml | 0 .../ssl/main/mqtt_eclipseprojects_io.pem | 0 .../{protocols/mqtt => }/ssl/pytest_mqtt_ssl.py | 0 examples/{protocols/mqtt => }/ssl/sdkconfig.ci | 0 .../{protocols/mqtt => }/ssl_ds/CMakeLists.txt | 0 examples/{protocols/mqtt => }/ssl_ds/README.md | 0 .../mqtt => }/ssl_ds/main/CMakeLists.txt | 0 .../{protocols/mqtt => }/ssl_ds/main/app_main.c | 0 .../mqtt => }/ssl_ds/main/idf_component.yml | 0 .../mqtt => }/ssl_ds/main/mosquitto.org.crt | 0 .../{protocols/mqtt => }/ssl_ds/partitions.csv | 0 .../mqtt => }/ssl_ds/sdkconfig.defaults | 0 .../mqtt => }/ssl_ds/sdkconfig.defaults.esp32h2 | 0 .../mqtt => }/ssl_mutual_auth/CMakeLists.txt | 0 .../mqtt => }/ssl_mutual_auth/README.md | 0 .../ssl_mutual_auth/main/CMakeLists.txt | 0 .../mqtt => }/ssl_mutual_auth/main/app_main.c | 0 .../mqtt => }/ssl_mutual_auth/main/client.crt | 0 .../mqtt => }/ssl_mutual_auth/main/client.key | 0 .../main/idf_component.yml | 0 .../ssl_mutual_auth/main/mosquitto.org.crt | 0 .../{protocols/mqtt => }/ssl_psk/CMakeLists.txt | 0 examples/{protocols/mqtt => }/ssl_psk/README.md | 0 .../mqtt => }/ssl_psk/main/CMakeLists.txt | 0 .../mqtt => }/ssl_psk/main/app_main.c | 0 .../mqtt/ws => ssl_psk}/main/idf_component.yml | 0 .../mqtt => }/ssl_psk/sdkconfig.defaults | 0 .../{protocols/mqtt => }/tcp/CMakeLists.txt | 0 examples/{protocols/mqtt => }/tcp/README.md | 0 .../mqtt5 => tcp}/main/CMakeLists.txt | 0 .../mqtt5 => tcp}/main/Kconfig.projbuild | 0 .../{protocols/mqtt => }/tcp/main/app_main.c | 0 .../mqtt => }/tcp/main/idf_component.yml | 0 .../{protocols/mqtt => }/tcp/pytest_mqtt_tcp.py | 0 examples/{protocols/mqtt => }/tcp/sdkconfig.ci | 0 .../mqtt => }/tcp/sdkconfig.ci.p4_eppp | 0 .../mqtt => }/tcp/sdkconfig.ci.p4_wifi | 0 .../mqtt => }/tcp/sdkconfig.ci.ppp_connect | 0 examples/{protocols/mqtt => }/ws/CMakeLists.txt | 0 examples/{protocols/mqtt => }/ws/README.md | 0 .../{protocols/mqtt => }/ws/main/CMakeLists.txt | 0 .../mqtt => }/ws/main/Kconfig.projbuild | 0 .../{protocols/mqtt => }/ws/main/app_main.c | 0 .../mqtt/wss => ws}/main/idf_component.yml | 0 .../mqtt => }/ws/pytest_mqtt_ws_example.py | 0 examples/{protocols/mqtt => }/ws/sdkconfig.ci | 0 .../{protocols/mqtt => }/wss/CMakeLists.txt | 0 examples/{protocols/mqtt => }/wss/README.md | 0 .../mqtt => }/wss/main/CMakeLists.txt | 0 .../mqtt => }/wss/main/Kconfig.projbuild | 0 .../{protocols/mqtt => }/wss/main/app_main.c | 0 .../mqtt5 => wss}/main/idf_component.yml | 0 .../wss/main/mqtt_eclipseprojects_io.pem | 0 .../mqtt => }/wss/pytest_mqtt_wss_example.py | 0 examples/{protocols/mqtt => }/wss/sdkconfig.ci | 0 .../test_apps => test}/.build-test-rules.yml | 0 .../apps}/build_test/CMakeLists.txt | 0 .../mqtt => test/apps}/build_test/README.md | 0 .../apps}/build_test/main/CMakeLists.txt | 0 .../apps}/build_test/main/mqtt_app.cpp | 0 .../apps}/common/CMakeLists.txt | 0 .../apps}/common/include/test_mqtt_connection.h | 0 .../apps}/common/test_mqtt_connection.c | 0 .../test_mqtt => test/apps/mqtt}/CMakeLists.txt | 0 .../test_mqtt => test/apps/mqtt}/README.md | 0 .../apps/mqtt}/main/CMakeLists.txt | 0 .../apps/mqtt}/main/Kconfig.projbuild | 0 .../apps/mqtt}/main/test_mqtt.c | 0 .../apps/mqtt}/main/test_mqtt_client_broker.c | 0 .../apps/mqtt}/main/test_mqtt_client_broker.h | 0 .../apps/mqtt}/pytest_mqtt_ut.py | 0 .../apps/mqtt}/sdkconfig.ci.default | 0 .../apps/mqtt}/sdkconfig.defaults | 0 .../apps/mqtt5}/CMakeLists.txt | 0 .../test_mqtt5 => test/apps/mqtt5}/README.md | 0 .../apps/mqtt5}/main/CMakeLists.txt | 0 .../apps/mqtt5}/main/Kconfig.projbuild | 0 .../apps/mqtt5}/main/test_mqtt5.c | 0 .../apps/mqtt5}/main/test_mqtt5_client_broker.c | 0 .../apps/mqtt5}/main/test_mqtt5_client_broker.h | 0 .../apps/mqtt5}/pytest_mqtt5_ut.py | 0 .../apps/mqtt5}/sdkconfig.ci.default | 0 .../apps/mqtt5}/sdkconfig.defaults | 0 .../apps}/publish_connect_test/CMakeLists.txt | 0 .../apps}/publish_connect_test/README.md | 0 .../apps}/publish_connect_test/ca.crt | 0 .../apps}/publish_connect_test/ca.der | Bin .../apps}/publish_connect_test/ca.key | 0 .../apps}/publish_connect_test/client_inv.crt | 0 .../publish_connect_test/client_no_pwd.key | 0 .../apps}/publish_connect_test/client_pwd.crt | 0 .../apps}/publish_connect_test/client_pwd.key | 0 .../publish_connect_test/main/CMakeLists.txt | 0 .../publish_connect_test/main/Kconfig.projbuild | 0 .../publish_connect_test/main/connect_test.c | 0 .../publish_connect_test/main/idf_component.yml | 0 .../main/mqtt_eclipseprojects_io.pem | 0 .../main/publish_connect_test.c | 0 .../main/publish_connect_test.h | 0 .../publish_connect_test/main/publish_test.c | 0 .../publish_connect_test/pytest_mqtt_app.py | 0 .../pytest_mqtt_publish_app.py | 0 .../publish_connect_test/sdkconfig.ci.default | 0 .../sdkconfig.ci.local_broker | 0 .../apps}/publish_connect_test/sdkconfig.qemu | 0 .../apps}/publish_connect_test/server.key | 0 {host_test => test/host}/CMakeLists.txt | 0 {host_test => test/host}/README.md | 0 {host_test => test/host}/main/CMakeLists.txt | 0 {host_test => test/host}/main/Kconfig | 0 {host_test => test/host}/main/idf_component.yml | 0 .../host}/main/test_mqtt_client.cpp | 0 .../host}/mocks/include/sys/queue.h | 0 {host_test => test/host}/sdkconfig.ci.coverage | 0 {host_test => test/host}/sdkconfig.defaults | 0 143 files changed, 1 insertion(+), 21 deletions(-) delete mode 100644 components/mqtt/host_test/mocks/heap/CMakeLists.txt delete mode 100644 components/mqtt/host_test/mocks/heap/heap_mock.c rename docs/en/{api-reference/protocols/mqtt.rst => index.rst} (100%) rename docs/zh_CN/{api-reference/protocols/mqtt.rst => index.rst} (100%) rename examples/{protocols/mqtt => }/custom_outbox/CMakeLists.txt (100%) rename examples/{protocols/mqtt => }/custom_outbox/README.md (100%) rename examples/{protocols/mqtt => }/custom_outbox/main/CMakeLists.txt (100%) rename examples/{protocols/mqtt => }/custom_outbox/main/Kconfig.projbuild (100%) rename examples/{protocols/mqtt => }/custom_outbox/main/app_main.c (100%) rename examples/{protocols/mqtt => }/custom_outbox/main/custom_outbox.cpp (100%) rename examples/{protocols/mqtt => }/custom_outbox/main/idf_component.yml (100%) rename examples/{protocols/mqtt => }/custom_outbox/sdkconfig.ci.esp32c6 (100%) rename examples/{protocols/mqtt => }/custom_outbox/sdkconfig.defaults (100%) rename examples/{protocols => }/mqtt5/CMakeLists.txt (100%) rename examples/{protocols => }/mqtt5/README.md (100%) rename examples/{protocols/mqtt/tcp => mqtt5}/main/CMakeLists.txt (100%) rename examples/{protocols/mqtt/tcp => mqtt5}/main/Kconfig.projbuild (100%) rename examples/{protocols => }/mqtt5/main/app_main.c (100%) rename examples/{protocols/mqtt/ssl => mqtt5}/main/idf_component.yml (100%) rename examples/{protocols => }/mqtt5/pytest_mqtt5.py (100%) rename examples/{protocols => }/mqtt5/sdkconfig.ci (100%) rename examples/{protocols => }/mqtt5/sdkconfig.defaults (100%) rename examples/{protocols/mqtt => }/ssl/CMakeLists.txt (100%) rename examples/{protocols/mqtt => }/ssl/README.md (100%) rename examples/{protocols/mqtt => }/ssl/main/CMakeLists.txt (100%) rename examples/{protocols/mqtt => }/ssl/main/Kconfig.projbuild (100%) rename examples/{protocols/mqtt => }/ssl/main/app_main.c (100%) rename examples/{protocols/mqtt/ssl_mutual_auth => ssl}/main/idf_component.yml (100%) rename examples/{protocols/mqtt => }/ssl/main/mqtt_eclipseprojects_io.pem (100%) rename examples/{protocols/mqtt => }/ssl/pytest_mqtt_ssl.py (100%) rename examples/{protocols/mqtt => }/ssl/sdkconfig.ci (100%) rename examples/{protocols/mqtt => }/ssl_ds/CMakeLists.txt (100%) rename examples/{protocols/mqtt => }/ssl_ds/README.md (100%) rename examples/{protocols/mqtt => }/ssl_ds/main/CMakeLists.txt (100%) rename examples/{protocols/mqtt => }/ssl_ds/main/app_main.c (100%) rename examples/{protocols/mqtt => }/ssl_ds/main/idf_component.yml (100%) rename examples/{protocols/mqtt => }/ssl_ds/main/mosquitto.org.crt (100%) rename examples/{protocols/mqtt => }/ssl_ds/partitions.csv (100%) rename examples/{protocols/mqtt => }/ssl_ds/sdkconfig.defaults (100%) rename examples/{protocols/mqtt => }/ssl_ds/sdkconfig.defaults.esp32h2 (100%) rename examples/{protocols/mqtt => }/ssl_mutual_auth/CMakeLists.txt (100%) rename examples/{protocols/mqtt => }/ssl_mutual_auth/README.md (100%) rename examples/{protocols/mqtt => }/ssl_mutual_auth/main/CMakeLists.txt (100%) rename examples/{protocols/mqtt => }/ssl_mutual_auth/main/app_main.c (100%) rename examples/{protocols/mqtt => }/ssl_mutual_auth/main/client.crt (100%) rename examples/{protocols/mqtt => }/ssl_mutual_auth/main/client.key (100%) rename examples/{protocols/mqtt/ssl_psk => ssl_mutual_auth}/main/idf_component.yml (100%) rename examples/{protocols/mqtt => }/ssl_mutual_auth/main/mosquitto.org.crt (100%) rename examples/{protocols/mqtt => }/ssl_psk/CMakeLists.txt (100%) rename examples/{protocols/mqtt => }/ssl_psk/README.md (100%) rename examples/{protocols/mqtt => }/ssl_psk/main/CMakeLists.txt (100%) rename examples/{protocols/mqtt => }/ssl_psk/main/app_main.c (100%) rename examples/{protocols/mqtt/ws => ssl_psk}/main/idf_component.yml (100%) rename examples/{protocols/mqtt => }/ssl_psk/sdkconfig.defaults (100%) rename examples/{protocols/mqtt => }/tcp/CMakeLists.txt (100%) rename examples/{protocols/mqtt => }/tcp/README.md (100%) rename examples/{protocols/mqtt5 => tcp}/main/CMakeLists.txt (100%) rename examples/{protocols/mqtt5 => tcp}/main/Kconfig.projbuild (100%) rename examples/{protocols/mqtt => }/tcp/main/app_main.c (100%) rename examples/{protocols/mqtt => }/tcp/main/idf_component.yml (100%) rename examples/{protocols/mqtt => }/tcp/pytest_mqtt_tcp.py (100%) rename examples/{protocols/mqtt => }/tcp/sdkconfig.ci (100%) rename examples/{protocols/mqtt => }/tcp/sdkconfig.ci.p4_eppp (100%) rename examples/{protocols/mqtt => }/tcp/sdkconfig.ci.p4_wifi (100%) rename examples/{protocols/mqtt => }/tcp/sdkconfig.ci.ppp_connect (100%) rename examples/{protocols/mqtt => }/ws/CMakeLists.txt (100%) rename examples/{protocols/mqtt => }/ws/README.md (100%) rename examples/{protocols/mqtt => }/ws/main/CMakeLists.txt (100%) rename examples/{protocols/mqtt => }/ws/main/Kconfig.projbuild (100%) rename examples/{protocols/mqtt => }/ws/main/app_main.c (100%) rename examples/{protocols/mqtt/wss => ws}/main/idf_component.yml (100%) rename examples/{protocols/mqtt => }/ws/pytest_mqtt_ws_example.py (100%) rename examples/{protocols/mqtt => }/ws/sdkconfig.ci (100%) rename examples/{protocols/mqtt => }/wss/CMakeLists.txt (100%) rename examples/{protocols/mqtt => }/wss/README.md (100%) rename examples/{protocols/mqtt => }/wss/main/CMakeLists.txt (100%) rename examples/{protocols/mqtt => }/wss/main/Kconfig.projbuild (100%) rename examples/{protocols/mqtt => }/wss/main/app_main.c (100%) rename examples/{protocols/mqtt5 => wss}/main/idf_component.yml (100%) rename examples/{protocols/mqtt => }/wss/main/mqtt_eclipseprojects_io.pem (100%) rename examples/{protocols/mqtt => }/wss/pytest_mqtt_wss_example.py (100%) rename examples/{protocols/mqtt => }/wss/sdkconfig.ci (100%) rename {components/mqtt/test_apps => test}/.build-test-rules.yml (100%) rename {tools/test_apps/protocols/mqtt => test/apps}/build_test/CMakeLists.txt (100%) rename {tools/test_apps/protocols/mqtt => test/apps}/build_test/README.md (100%) rename {tools/test_apps/protocols/mqtt => test/apps}/build_test/main/CMakeLists.txt (100%) rename {tools/test_apps/protocols/mqtt => test/apps}/build_test/main/mqtt_app.cpp (100%) rename {components/mqtt/test_apps => test/apps}/common/CMakeLists.txt (100%) rename {components/mqtt/test_apps => test/apps}/common/include/test_mqtt_connection.h (100%) rename {components/mqtt/test_apps => test/apps}/common/test_mqtt_connection.c (100%) rename {components/mqtt/test_apps/test_mqtt => test/apps/mqtt}/CMakeLists.txt (100%) rename {components/mqtt/test_apps/test_mqtt => test/apps/mqtt}/README.md (100%) rename {components/mqtt/test_apps/test_mqtt => test/apps/mqtt}/main/CMakeLists.txt (100%) rename {components/mqtt/test_apps/test_mqtt => test/apps/mqtt}/main/Kconfig.projbuild (100%) rename {components/mqtt/test_apps/test_mqtt => test/apps/mqtt}/main/test_mqtt.c (100%) rename {components/mqtt/test_apps/test_mqtt => test/apps/mqtt}/main/test_mqtt_client_broker.c (100%) rename {components/mqtt/test_apps/test_mqtt => test/apps/mqtt}/main/test_mqtt_client_broker.h (100%) rename {components/mqtt/test_apps/test_mqtt => test/apps/mqtt}/pytest_mqtt_ut.py (100%) rename {components/mqtt/test_apps/test_mqtt => test/apps/mqtt}/sdkconfig.ci.default (100%) rename {components/mqtt/test_apps/test_mqtt => test/apps/mqtt}/sdkconfig.defaults (100%) rename {components/mqtt/test_apps/test_mqtt5 => test/apps/mqtt5}/CMakeLists.txt (100%) rename {components/mqtt/test_apps/test_mqtt5 => test/apps/mqtt5}/README.md (100%) rename {components/mqtt/test_apps/test_mqtt5 => test/apps/mqtt5}/main/CMakeLists.txt (100%) rename {components/mqtt/test_apps/test_mqtt5 => test/apps/mqtt5}/main/Kconfig.projbuild (100%) rename {components/mqtt/test_apps/test_mqtt5 => test/apps/mqtt5}/main/test_mqtt5.c (100%) rename {components/mqtt/test_apps/test_mqtt5 => test/apps/mqtt5}/main/test_mqtt5_client_broker.c (100%) rename {components/mqtt/test_apps/test_mqtt5 => test/apps/mqtt5}/main/test_mqtt5_client_broker.h (100%) rename {components/mqtt/test_apps/test_mqtt5 => test/apps/mqtt5}/pytest_mqtt5_ut.py (100%) rename {components/mqtt/test_apps/test_mqtt5 => test/apps/mqtt5}/sdkconfig.ci.default (100%) rename {components/mqtt/test_apps/test_mqtt5 => test/apps/mqtt5}/sdkconfig.defaults (100%) rename {tools/test_apps/protocols/mqtt => test/apps}/publish_connect_test/CMakeLists.txt (100%) rename {tools/test_apps/protocols/mqtt => test/apps}/publish_connect_test/README.md (100%) rename {tools/test_apps/protocols/mqtt => test/apps}/publish_connect_test/ca.crt (100%) rename {tools/test_apps/protocols/mqtt => test/apps}/publish_connect_test/ca.der (100%) rename {tools/test_apps/protocols/mqtt => test/apps}/publish_connect_test/ca.key (100%) rename {tools/test_apps/protocols/mqtt => test/apps}/publish_connect_test/client_inv.crt (100%) rename {tools/test_apps/protocols/mqtt => test/apps}/publish_connect_test/client_no_pwd.key (100%) rename {tools/test_apps/protocols/mqtt => test/apps}/publish_connect_test/client_pwd.crt (100%) rename {tools/test_apps/protocols/mqtt => test/apps}/publish_connect_test/client_pwd.key (100%) rename {tools/test_apps/protocols/mqtt => test/apps}/publish_connect_test/main/CMakeLists.txt (100%) rename {tools/test_apps/protocols/mqtt => test/apps}/publish_connect_test/main/Kconfig.projbuild (100%) rename {tools/test_apps/protocols/mqtt => test/apps}/publish_connect_test/main/connect_test.c (100%) rename {tools/test_apps/protocols/mqtt => test/apps}/publish_connect_test/main/idf_component.yml (100%) rename {tools/test_apps/protocols/mqtt => test/apps}/publish_connect_test/main/mqtt_eclipseprojects_io.pem (100%) rename {tools/test_apps/protocols/mqtt => test/apps}/publish_connect_test/main/publish_connect_test.c (100%) rename {tools/test_apps/protocols/mqtt => test/apps}/publish_connect_test/main/publish_connect_test.h (100%) rename {tools/test_apps/protocols/mqtt => test/apps}/publish_connect_test/main/publish_test.c (100%) rename {tools/test_apps/protocols/mqtt => test/apps}/publish_connect_test/pytest_mqtt_app.py (100%) rename {tools/test_apps/protocols/mqtt => test/apps}/publish_connect_test/pytest_mqtt_publish_app.py (100%) rename {tools/test_apps/protocols/mqtt => test/apps}/publish_connect_test/sdkconfig.ci.default (100%) rename {tools/test_apps/protocols/mqtt => test/apps}/publish_connect_test/sdkconfig.ci.local_broker (100%) rename {tools/test_apps/protocols/mqtt => test/apps}/publish_connect_test/sdkconfig.qemu (100%) rename {tools/test_apps/protocols/mqtt => test/apps}/publish_connect_test/server.key (100%) rename {host_test => test/host}/CMakeLists.txt (100%) rename {host_test => test/host}/README.md (100%) rename {host_test => test/host}/main/CMakeLists.txt (100%) rename {host_test => test/host}/main/Kconfig (100%) rename {host_test => test/host}/main/idf_component.yml (100%) rename {host_test => test/host}/main/test_mqtt_client.cpp (100%) rename {host_test => test/host}/mocks/include/sys/queue.h (100%) rename {host_test => test/host}/sdkconfig.ci.coverage (100%) rename {host_test => test/host}/sdkconfig.defaults (100%) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 9fc30b7..aadcf47 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -69,7 +69,7 @@ build_and_host_test: script: # Replace the IDF's default esp-mqtt with this version - rm -rf $IDF_PATH/components/mqtt/esp-mqtt && cp -r $MQTT_PATH $IDF_PATH/components/mqtt/ - - cd $IDF_PATH/components/mqtt/esp-mqtt/host_test + - cd $IDF_PATH/components/mqtt/esp-mqtt/test/host - idf.py build - build/host_mqtt_client_test.elf diff --git a/components/mqtt/host_test/mocks/heap/CMakeLists.txt b/components/mqtt/host_test/mocks/heap/CMakeLists.txt deleted file mode 100644 index 0a0c8a6..0000000 --- a/components/mqtt/host_test/mocks/heap/CMakeLists.txt +++ /dev/null @@ -1,4 +0,0 @@ -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/components/mqtt/host_test/mocks/heap/heap_mock.c b/components/mqtt/host_test/mocks/heap/heap_mock.c deleted file mode 100644 index e0fc666..0000000 --- a/components/mqtt/host_test/mocks/heap/heap_mock.c +++ /dev/null @@ -1,16 +0,0 @@ -/* - * SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD - * - * SPDX-License-Identifier: Apache-2.0 - */ -#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/docs/en/api-reference/protocols/mqtt.rst b/docs/en/index.rst similarity index 100% rename from docs/en/api-reference/protocols/mqtt.rst rename to docs/en/index.rst diff --git a/docs/zh_CN/api-reference/protocols/mqtt.rst b/docs/zh_CN/index.rst similarity index 100% rename from docs/zh_CN/api-reference/protocols/mqtt.rst rename to docs/zh_CN/index.rst diff --git a/examples/protocols/mqtt/custom_outbox/CMakeLists.txt b/examples/custom_outbox/CMakeLists.txt similarity index 100% rename from examples/protocols/mqtt/custom_outbox/CMakeLists.txt rename to examples/custom_outbox/CMakeLists.txt diff --git a/examples/protocols/mqtt/custom_outbox/README.md b/examples/custom_outbox/README.md similarity index 100% rename from examples/protocols/mqtt/custom_outbox/README.md rename to examples/custom_outbox/README.md diff --git a/examples/protocols/mqtt/custom_outbox/main/CMakeLists.txt b/examples/custom_outbox/main/CMakeLists.txt similarity index 100% rename from examples/protocols/mqtt/custom_outbox/main/CMakeLists.txt rename to examples/custom_outbox/main/CMakeLists.txt diff --git a/examples/protocols/mqtt/custom_outbox/main/Kconfig.projbuild b/examples/custom_outbox/main/Kconfig.projbuild similarity index 100% rename from examples/protocols/mqtt/custom_outbox/main/Kconfig.projbuild rename to examples/custom_outbox/main/Kconfig.projbuild diff --git a/examples/protocols/mqtt/custom_outbox/main/app_main.c b/examples/custom_outbox/main/app_main.c similarity index 100% rename from examples/protocols/mqtt/custom_outbox/main/app_main.c rename to examples/custom_outbox/main/app_main.c diff --git a/examples/protocols/mqtt/custom_outbox/main/custom_outbox.cpp b/examples/custom_outbox/main/custom_outbox.cpp similarity index 100% rename from examples/protocols/mqtt/custom_outbox/main/custom_outbox.cpp rename to examples/custom_outbox/main/custom_outbox.cpp diff --git a/examples/protocols/mqtt/custom_outbox/main/idf_component.yml b/examples/custom_outbox/main/idf_component.yml similarity index 100% rename from examples/protocols/mqtt/custom_outbox/main/idf_component.yml rename to examples/custom_outbox/main/idf_component.yml diff --git a/examples/protocols/mqtt/custom_outbox/sdkconfig.ci.esp32c6 b/examples/custom_outbox/sdkconfig.ci.esp32c6 similarity index 100% rename from examples/protocols/mqtt/custom_outbox/sdkconfig.ci.esp32c6 rename to examples/custom_outbox/sdkconfig.ci.esp32c6 diff --git a/examples/protocols/mqtt/custom_outbox/sdkconfig.defaults b/examples/custom_outbox/sdkconfig.defaults similarity index 100% rename from examples/protocols/mqtt/custom_outbox/sdkconfig.defaults rename to examples/custom_outbox/sdkconfig.defaults diff --git a/examples/protocols/mqtt5/CMakeLists.txt b/examples/mqtt5/CMakeLists.txt similarity index 100% rename from examples/protocols/mqtt5/CMakeLists.txt rename to examples/mqtt5/CMakeLists.txt diff --git a/examples/protocols/mqtt5/README.md b/examples/mqtt5/README.md similarity index 100% rename from examples/protocols/mqtt5/README.md rename to examples/mqtt5/README.md diff --git a/examples/protocols/mqtt/tcp/main/CMakeLists.txt b/examples/mqtt5/main/CMakeLists.txt similarity index 100% rename from examples/protocols/mqtt/tcp/main/CMakeLists.txt rename to examples/mqtt5/main/CMakeLists.txt diff --git a/examples/protocols/mqtt/tcp/main/Kconfig.projbuild b/examples/mqtt5/main/Kconfig.projbuild similarity index 100% rename from examples/protocols/mqtt/tcp/main/Kconfig.projbuild rename to examples/mqtt5/main/Kconfig.projbuild diff --git a/examples/protocols/mqtt5/main/app_main.c b/examples/mqtt5/main/app_main.c similarity index 100% rename from examples/protocols/mqtt5/main/app_main.c rename to examples/mqtt5/main/app_main.c diff --git a/examples/protocols/mqtt/ssl/main/idf_component.yml b/examples/mqtt5/main/idf_component.yml similarity index 100% rename from examples/protocols/mqtt/ssl/main/idf_component.yml rename to examples/mqtt5/main/idf_component.yml diff --git a/examples/protocols/mqtt5/pytest_mqtt5.py b/examples/mqtt5/pytest_mqtt5.py similarity index 100% rename from examples/protocols/mqtt5/pytest_mqtt5.py rename to examples/mqtt5/pytest_mqtt5.py diff --git a/examples/protocols/mqtt5/sdkconfig.ci b/examples/mqtt5/sdkconfig.ci similarity index 100% rename from examples/protocols/mqtt5/sdkconfig.ci rename to examples/mqtt5/sdkconfig.ci diff --git a/examples/protocols/mqtt5/sdkconfig.defaults b/examples/mqtt5/sdkconfig.defaults similarity index 100% rename from examples/protocols/mqtt5/sdkconfig.defaults rename to examples/mqtt5/sdkconfig.defaults diff --git a/examples/protocols/mqtt/ssl/CMakeLists.txt b/examples/ssl/CMakeLists.txt similarity index 100% rename from examples/protocols/mqtt/ssl/CMakeLists.txt rename to examples/ssl/CMakeLists.txt diff --git a/examples/protocols/mqtt/ssl/README.md b/examples/ssl/README.md similarity index 100% rename from examples/protocols/mqtt/ssl/README.md rename to examples/ssl/README.md diff --git a/examples/protocols/mqtt/ssl/main/CMakeLists.txt b/examples/ssl/main/CMakeLists.txt similarity index 100% rename from examples/protocols/mqtt/ssl/main/CMakeLists.txt rename to examples/ssl/main/CMakeLists.txt diff --git a/examples/protocols/mqtt/ssl/main/Kconfig.projbuild b/examples/ssl/main/Kconfig.projbuild similarity index 100% rename from examples/protocols/mqtt/ssl/main/Kconfig.projbuild rename to examples/ssl/main/Kconfig.projbuild diff --git a/examples/protocols/mqtt/ssl/main/app_main.c b/examples/ssl/main/app_main.c similarity index 100% rename from examples/protocols/mqtt/ssl/main/app_main.c rename to examples/ssl/main/app_main.c diff --git a/examples/protocols/mqtt/ssl_mutual_auth/main/idf_component.yml b/examples/ssl/main/idf_component.yml similarity index 100% rename from examples/protocols/mqtt/ssl_mutual_auth/main/idf_component.yml rename to examples/ssl/main/idf_component.yml diff --git a/examples/protocols/mqtt/ssl/main/mqtt_eclipseprojects_io.pem b/examples/ssl/main/mqtt_eclipseprojects_io.pem similarity index 100% rename from examples/protocols/mqtt/ssl/main/mqtt_eclipseprojects_io.pem rename to examples/ssl/main/mqtt_eclipseprojects_io.pem diff --git a/examples/protocols/mqtt/ssl/pytest_mqtt_ssl.py b/examples/ssl/pytest_mqtt_ssl.py similarity index 100% rename from examples/protocols/mqtt/ssl/pytest_mqtt_ssl.py rename to examples/ssl/pytest_mqtt_ssl.py diff --git a/examples/protocols/mqtt/ssl/sdkconfig.ci b/examples/ssl/sdkconfig.ci similarity index 100% rename from examples/protocols/mqtt/ssl/sdkconfig.ci rename to examples/ssl/sdkconfig.ci diff --git a/examples/protocols/mqtt/ssl_ds/CMakeLists.txt b/examples/ssl_ds/CMakeLists.txt similarity index 100% rename from examples/protocols/mqtt/ssl_ds/CMakeLists.txt rename to examples/ssl_ds/CMakeLists.txt diff --git a/examples/protocols/mqtt/ssl_ds/README.md b/examples/ssl_ds/README.md similarity index 100% rename from examples/protocols/mqtt/ssl_ds/README.md rename to examples/ssl_ds/README.md diff --git a/examples/protocols/mqtt/ssl_ds/main/CMakeLists.txt b/examples/ssl_ds/main/CMakeLists.txt similarity index 100% rename from examples/protocols/mqtt/ssl_ds/main/CMakeLists.txt rename to examples/ssl_ds/main/CMakeLists.txt diff --git a/examples/protocols/mqtt/ssl_ds/main/app_main.c b/examples/ssl_ds/main/app_main.c similarity index 100% rename from examples/protocols/mqtt/ssl_ds/main/app_main.c rename to examples/ssl_ds/main/app_main.c diff --git a/examples/protocols/mqtt/ssl_ds/main/idf_component.yml b/examples/ssl_ds/main/idf_component.yml similarity index 100% rename from examples/protocols/mqtt/ssl_ds/main/idf_component.yml rename to examples/ssl_ds/main/idf_component.yml diff --git a/examples/protocols/mqtt/ssl_ds/main/mosquitto.org.crt b/examples/ssl_ds/main/mosquitto.org.crt similarity index 100% rename from examples/protocols/mqtt/ssl_ds/main/mosquitto.org.crt rename to examples/ssl_ds/main/mosquitto.org.crt diff --git a/examples/protocols/mqtt/ssl_ds/partitions.csv b/examples/ssl_ds/partitions.csv similarity index 100% rename from examples/protocols/mqtt/ssl_ds/partitions.csv rename to examples/ssl_ds/partitions.csv diff --git a/examples/protocols/mqtt/ssl_ds/sdkconfig.defaults b/examples/ssl_ds/sdkconfig.defaults similarity index 100% rename from examples/protocols/mqtt/ssl_ds/sdkconfig.defaults rename to examples/ssl_ds/sdkconfig.defaults diff --git a/examples/protocols/mqtt/ssl_ds/sdkconfig.defaults.esp32h2 b/examples/ssl_ds/sdkconfig.defaults.esp32h2 similarity index 100% rename from examples/protocols/mqtt/ssl_ds/sdkconfig.defaults.esp32h2 rename to examples/ssl_ds/sdkconfig.defaults.esp32h2 diff --git a/examples/protocols/mqtt/ssl_mutual_auth/CMakeLists.txt b/examples/ssl_mutual_auth/CMakeLists.txt similarity index 100% rename from examples/protocols/mqtt/ssl_mutual_auth/CMakeLists.txt rename to examples/ssl_mutual_auth/CMakeLists.txt diff --git a/examples/protocols/mqtt/ssl_mutual_auth/README.md b/examples/ssl_mutual_auth/README.md similarity index 100% rename from examples/protocols/mqtt/ssl_mutual_auth/README.md rename to examples/ssl_mutual_auth/README.md diff --git a/examples/protocols/mqtt/ssl_mutual_auth/main/CMakeLists.txt b/examples/ssl_mutual_auth/main/CMakeLists.txt similarity index 100% rename from examples/protocols/mqtt/ssl_mutual_auth/main/CMakeLists.txt rename to examples/ssl_mutual_auth/main/CMakeLists.txt diff --git a/examples/protocols/mqtt/ssl_mutual_auth/main/app_main.c b/examples/ssl_mutual_auth/main/app_main.c similarity index 100% rename from examples/protocols/mqtt/ssl_mutual_auth/main/app_main.c rename to examples/ssl_mutual_auth/main/app_main.c diff --git a/examples/protocols/mqtt/ssl_mutual_auth/main/client.crt b/examples/ssl_mutual_auth/main/client.crt similarity index 100% rename from examples/protocols/mqtt/ssl_mutual_auth/main/client.crt rename to examples/ssl_mutual_auth/main/client.crt diff --git a/examples/protocols/mqtt/ssl_mutual_auth/main/client.key b/examples/ssl_mutual_auth/main/client.key similarity index 100% rename from examples/protocols/mqtt/ssl_mutual_auth/main/client.key rename to examples/ssl_mutual_auth/main/client.key diff --git a/examples/protocols/mqtt/ssl_psk/main/idf_component.yml b/examples/ssl_mutual_auth/main/idf_component.yml similarity index 100% rename from examples/protocols/mqtt/ssl_psk/main/idf_component.yml rename to examples/ssl_mutual_auth/main/idf_component.yml diff --git a/examples/protocols/mqtt/ssl_mutual_auth/main/mosquitto.org.crt b/examples/ssl_mutual_auth/main/mosquitto.org.crt similarity index 100% rename from examples/protocols/mqtt/ssl_mutual_auth/main/mosquitto.org.crt rename to examples/ssl_mutual_auth/main/mosquitto.org.crt diff --git a/examples/protocols/mqtt/ssl_psk/CMakeLists.txt b/examples/ssl_psk/CMakeLists.txt similarity index 100% rename from examples/protocols/mqtt/ssl_psk/CMakeLists.txt rename to examples/ssl_psk/CMakeLists.txt diff --git a/examples/protocols/mqtt/ssl_psk/README.md b/examples/ssl_psk/README.md similarity index 100% rename from examples/protocols/mqtt/ssl_psk/README.md rename to examples/ssl_psk/README.md diff --git a/examples/protocols/mqtt/ssl_psk/main/CMakeLists.txt b/examples/ssl_psk/main/CMakeLists.txt similarity index 100% rename from examples/protocols/mqtt/ssl_psk/main/CMakeLists.txt rename to examples/ssl_psk/main/CMakeLists.txt diff --git a/examples/protocols/mqtt/ssl_psk/main/app_main.c b/examples/ssl_psk/main/app_main.c similarity index 100% rename from examples/protocols/mqtt/ssl_psk/main/app_main.c rename to examples/ssl_psk/main/app_main.c diff --git a/examples/protocols/mqtt/ws/main/idf_component.yml b/examples/ssl_psk/main/idf_component.yml similarity index 100% rename from examples/protocols/mqtt/ws/main/idf_component.yml rename to examples/ssl_psk/main/idf_component.yml diff --git a/examples/protocols/mqtt/ssl_psk/sdkconfig.defaults b/examples/ssl_psk/sdkconfig.defaults similarity index 100% rename from examples/protocols/mqtt/ssl_psk/sdkconfig.defaults rename to examples/ssl_psk/sdkconfig.defaults diff --git a/examples/protocols/mqtt/tcp/CMakeLists.txt b/examples/tcp/CMakeLists.txt similarity index 100% rename from examples/protocols/mqtt/tcp/CMakeLists.txt rename to examples/tcp/CMakeLists.txt diff --git a/examples/protocols/mqtt/tcp/README.md b/examples/tcp/README.md similarity index 100% rename from examples/protocols/mqtt/tcp/README.md rename to examples/tcp/README.md diff --git a/examples/protocols/mqtt5/main/CMakeLists.txt b/examples/tcp/main/CMakeLists.txt similarity index 100% rename from examples/protocols/mqtt5/main/CMakeLists.txt rename to examples/tcp/main/CMakeLists.txt diff --git a/examples/protocols/mqtt5/main/Kconfig.projbuild b/examples/tcp/main/Kconfig.projbuild similarity index 100% rename from examples/protocols/mqtt5/main/Kconfig.projbuild rename to examples/tcp/main/Kconfig.projbuild diff --git a/examples/protocols/mqtt/tcp/main/app_main.c b/examples/tcp/main/app_main.c similarity index 100% rename from examples/protocols/mqtt/tcp/main/app_main.c rename to examples/tcp/main/app_main.c diff --git a/examples/protocols/mqtt/tcp/main/idf_component.yml b/examples/tcp/main/idf_component.yml similarity index 100% rename from examples/protocols/mqtt/tcp/main/idf_component.yml rename to examples/tcp/main/idf_component.yml diff --git a/examples/protocols/mqtt/tcp/pytest_mqtt_tcp.py b/examples/tcp/pytest_mqtt_tcp.py similarity index 100% rename from examples/protocols/mqtt/tcp/pytest_mqtt_tcp.py rename to examples/tcp/pytest_mqtt_tcp.py diff --git a/examples/protocols/mqtt/tcp/sdkconfig.ci b/examples/tcp/sdkconfig.ci similarity index 100% rename from examples/protocols/mqtt/tcp/sdkconfig.ci rename to examples/tcp/sdkconfig.ci diff --git a/examples/protocols/mqtt/tcp/sdkconfig.ci.p4_eppp b/examples/tcp/sdkconfig.ci.p4_eppp similarity index 100% rename from examples/protocols/mqtt/tcp/sdkconfig.ci.p4_eppp rename to examples/tcp/sdkconfig.ci.p4_eppp diff --git a/examples/protocols/mqtt/tcp/sdkconfig.ci.p4_wifi b/examples/tcp/sdkconfig.ci.p4_wifi similarity index 100% rename from examples/protocols/mqtt/tcp/sdkconfig.ci.p4_wifi rename to examples/tcp/sdkconfig.ci.p4_wifi diff --git a/examples/protocols/mqtt/tcp/sdkconfig.ci.ppp_connect b/examples/tcp/sdkconfig.ci.ppp_connect similarity index 100% rename from examples/protocols/mqtt/tcp/sdkconfig.ci.ppp_connect rename to examples/tcp/sdkconfig.ci.ppp_connect diff --git a/examples/protocols/mqtt/ws/CMakeLists.txt b/examples/ws/CMakeLists.txt similarity index 100% rename from examples/protocols/mqtt/ws/CMakeLists.txt rename to examples/ws/CMakeLists.txt diff --git a/examples/protocols/mqtt/ws/README.md b/examples/ws/README.md similarity index 100% rename from examples/protocols/mqtt/ws/README.md rename to examples/ws/README.md diff --git a/examples/protocols/mqtt/ws/main/CMakeLists.txt b/examples/ws/main/CMakeLists.txt similarity index 100% rename from examples/protocols/mqtt/ws/main/CMakeLists.txt rename to examples/ws/main/CMakeLists.txt diff --git a/examples/protocols/mqtt/ws/main/Kconfig.projbuild b/examples/ws/main/Kconfig.projbuild similarity index 100% rename from examples/protocols/mqtt/ws/main/Kconfig.projbuild rename to examples/ws/main/Kconfig.projbuild diff --git a/examples/protocols/mqtt/ws/main/app_main.c b/examples/ws/main/app_main.c similarity index 100% rename from examples/protocols/mqtt/ws/main/app_main.c rename to examples/ws/main/app_main.c diff --git a/examples/protocols/mqtt/wss/main/idf_component.yml b/examples/ws/main/idf_component.yml similarity index 100% rename from examples/protocols/mqtt/wss/main/idf_component.yml rename to examples/ws/main/idf_component.yml diff --git a/examples/protocols/mqtt/ws/pytest_mqtt_ws_example.py b/examples/ws/pytest_mqtt_ws_example.py similarity index 100% rename from examples/protocols/mqtt/ws/pytest_mqtt_ws_example.py rename to examples/ws/pytest_mqtt_ws_example.py diff --git a/examples/protocols/mqtt/ws/sdkconfig.ci b/examples/ws/sdkconfig.ci similarity index 100% rename from examples/protocols/mqtt/ws/sdkconfig.ci rename to examples/ws/sdkconfig.ci diff --git a/examples/protocols/mqtt/wss/CMakeLists.txt b/examples/wss/CMakeLists.txt similarity index 100% rename from examples/protocols/mqtt/wss/CMakeLists.txt rename to examples/wss/CMakeLists.txt diff --git a/examples/protocols/mqtt/wss/README.md b/examples/wss/README.md similarity index 100% rename from examples/protocols/mqtt/wss/README.md rename to examples/wss/README.md diff --git a/examples/protocols/mqtt/wss/main/CMakeLists.txt b/examples/wss/main/CMakeLists.txt similarity index 100% rename from examples/protocols/mqtt/wss/main/CMakeLists.txt rename to examples/wss/main/CMakeLists.txt diff --git a/examples/protocols/mqtt/wss/main/Kconfig.projbuild b/examples/wss/main/Kconfig.projbuild similarity index 100% rename from examples/protocols/mqtt/wss/main/Kconfig.projbuild rename to examples/wss/main/Kconfig.projbuild diff --git a/examples/protocols/mqtt/wss/main/app_main.c b/examples/wss/main/app_main.c similarity index 100% rename from examples/protocols/mqtt/wss/main/app_main.c rename to examples/wss/main/app_main.c diff --git a/examples/protocols/mqtt5/main/idf_component.yml b/examples/wss/main/idf_component.yml similarity index 100% rename from examples/protocols/mqtt5/main/idf_component.yml rename to examples/wss/main/idf_component.yml diff --git a/examples/protocols/mqtt/wss/main/mqtt_eclipseprojects_io.pem b/examples/wss/main/mqtt_eclipseprojects_io.pem similarity index 100% rename from examples/protocols/mqtt/wss/main/mqtt_eclipseprojects_io.pem rename to examples/wss/main/mqtt_eclipseprojects_io.pem diff --git a/examples/protocols/mqtt/wss/pytest_mqtt_wss_example.py b/examples/wss/pytest_mqtt_wss_example.py similarity index 100% rename from examples/protocols/mqtt/wss/pytest_mqtt_wss_example.py rename to examples/wss/pytest_mqtt_wss_example.py diff --git a/examples/protocols/mqtt/wss/sdkconfig.ci b/examples/wss/sdkconfig.ci similarity index 100% rename from examples/protocols/mqtt/wss/sdkconfig.ci rename to examples/wss/sdkconfig.ci diff --git a/components/mqtt/test_apps/.build-test-rules.yml b/test/.build-test-rules.yml similarity index 100% rename from components/mqtt/test_apps/.build-test-rules.yml rename to test/.build-test-rules.yml diff --git a/tools/test_apps/protocols/mqtt/build_test/CMakeLists.txt b/test/apps/build_test/CMakeLists.txt similarity index 100% rename from tools/test_apps/protocols/mqtt/build_test/CMakeLists.txt rename to test/apps/build_test/CMakeLists.txt diff --git a/tools/test_apps/protocols/mqtt/build_test/README.md b/test/apps/build_test/README.md similarity index 100% rename from tools/test_apps/protocols/mqtt/build_test/README.md rename to test/apps/build_test/README.md diff --git a/tools/test_apps/protocols/mqtt/build_test/main/CMakeLists.txt b/test/apps/build_test/main/CMakeLists.txt similarity index 100% rename from tools/test_apps/protocols/mqtt/build_test/main/CMakeLists.txt rename to test/apps/build_test/main/CMakeLists.txt diff --git a/tools/test_apps/protocols/mqtt/build_test/main/mqtt_app.cpp b/test/apps/build_test/main/mqtt_app.cpp similarity index 100% rename from tools/test_apps/protocols/mqtt/build_test/main/mqtt_app.cpp rename to test/apps/build_test/main/mqtt_app.cpp diff --git a/components/mqtt/test_apps/common/CMakeLists.txt b/test/apps/common/CMakeLists.txt similarity index 100% rename from components/mqtt/test_apps/common/CMakeLists.txt rename to test/apps/common/CMakeLists.txt diff --git a/components/mqtt/test_apps/common/include/test_mqtt_connection.h b/test/apps/common/include/test_mqtt_connection.h similarity index 100% rename from components/mqtt/test_apps/common/include/test_mqtt_connection.h rename to test/apps/common/include/test_mqtt_connection.h diff --git a/components/mqtt/test_apps/common/test_mqtt_connection.c b/test/apps/common/test_mqtt_connection.c similarity index 100% rename from components/mqtt/test_apps/common/test_mqtt_connection.c rename to test/apps/common/test_mqtt_connection.c diff --git a/components/mqtt/test_apps/test_mqtt/CMakeLists.txt b/test/apps/mqtt/CMakeLists.txt similarity index 100% rename from components/mqtt/test_apps/test_mqtt/CMakeLists.txt rename to test/apps/mqtt/CMakeLists.txt diff --git a/components/mqtt/test_apps/test_mqtt/README.md b/test/apps/mqtt/README.md similarity index 100% rename from components/mqtt/test_apps/test_mqtt/README.md rename to test/apps/mqtt/README.md diff --git a/components/mqtt/test_apps/test_mqtt/main/CMakeLists.txt b/test/apps/mqtt/main/CMakeLists.txt similarity index 100% rename from components/mqtt/test_apps/test_mqtt/main/CMakeLists.txt rename to test/apps/mqtt/main/CMakeLists.txt diff --git a/components/mqtt/test_apps/test_mqtt/main/Kconfig.projbuild b/test/apps/mqtt/main/Kconfig.projbuild similarity index 100% rename from components/mqtt/test_apps/test_mqtt/main/Kconfig.projbuild rename to test/apps/mqtt/main/Kconfig.projbuild diff --git a/components/mqtt/test_apps/test_mqtt/main/test_mqtt.c b/test/apps/mqtt/main/test_mqtt.c similarity index 100% rename from components/mqtt/test_apps/test_mqtt/main/test_mqtt.c rename to test/apps/mqtt/main/test_mqtt.c diff --git a/components/mqtt/test_apps/test_mqtt/main/test_mqtt_client_broker.c b/test/apps/mqtt/main/test_mqtt_client_broker.c similarity index 100% rename from components/mqtt/test_apps/test_mqtt/main/test_mqtt_client_broker.c rename to test/apps/mqtt/main/test_mqtt_client_broker.c diff --git a/components/mqtt/test_apps/test_mqtt/main/test_mqtt_client_broker.h b/test/apps/mqtt/main/test_mqtt_client_broker.h similarity index 100% rename from components/mqtt/test_apps/test_mqtt/main/test_mqtt_client_broker.h rename to test/apps/mqtt/main/test_mqtt_client_broker.h diff --git a/components/mqtt/test_apps/test_mqtt/pytest_mqtt_ut.py b/test/apps/mqtt/pytest_mqtt_ut.py similarity index 100% rename from components/mqtt/test_apps/test_mqtt/pytest_mqtt_ut.py rename to test/apps/mqtt/pytest_mqtt_ut.py diff --git a/components/mqtt/test_apps/test_mqtt/sdkconfig.ci.default b/test/apps/mqtt/sdkconfig.ci.default similarity index 100% rename from components/mqtt/test_apps/test_mqtt/sdkconfig.ci.default rename to test/apps/mqtt/sdkconfig.ci.default diff --git a/components/mqtt/test_apps/test_mqtt/sdkconfig.defaults b/test/apps/mqtt/sdkconfig.defaults similarity index 100% rename from components/mqtt/test_apps/test_mqtt/sdkconfig.defaults rename to test/apps/mqtt/sdkconfig.defaults diff --git a/components/mqtt/test_apps/test_mqtt5/CMakeLists.txt b/test/apps/mqtt5/CMakeLists.txt similarity index 100% rename from components/mqtt/test_apps/test_mqtt5/CMakeLists.txt rename to test/apps/mqtt5/CMakeLists.txt diff --git a/components/mqtt/test_apps/test_mqtt5/README.md b/test/apps/mqtt5/README.md similarity index 100% rename from components/mqtt/test_apps/test_mqtt5/README.md rename to test/apps/mqtt5/README.md diff --git a/components/mqtt/test_apps/test_mqtt5/main/CMakeLists.txt b/test/apps/mqtt5/main/CMakeLists.txt similarity index 100% rename from components/mqtt/test_apps/test_mqtt5/main/CMakeLists.txt rename to test/apps/mqtt5/main/CMakeLists.txt diff --git a/components/mqtt/test_apps/test_mqtt5/main/Kconfig.projbuild b/test/apps/mqtt5/main/Kconfig.projbuild similarity index 100% rename from components/mqtt/test_apps/test_mqtt5/main/Kconfig.projbuild rename to test/apps/mqtt5/main/Kconfig.projbuild diff --git a/components/mqtt/test_apps/test_mqtt5/main/test_mqtt5.c b/test/apps/mqtt5/main/test_mqtt5.c similarity index 100% rename from components/mqtt/test_apps/test_mqtt5/main/test_mqtt5.c rename to test/apps/mqtt5/main/test_mqtt5.c diff --git a/components/mqtt/test_apps/test_mqtt5/main/test_mqtt5_client_broker.c b/test/apps/mqtt5/main/test_mqtt5_client_broker.c similarity index 100% rename from components/mqtt/test_apps/test_mqtt5/main/test_mqtt5_client_broker.c rename to test/apps/mqtt5/main/test_mqtt5_client_broker.c diff --git a/components/mqtt/test_apps/test_mqtt5/main/test_mqtt5_client_broker.h b/test/apps/mqtt5/main/test_mqtt5_client_broker.h similarity index 100% rename from components/mqtt/test_apps/test_mqtt5/main/test_mqtt5_client_broker.h rename to test/apps/mqtt5/main/test_mqtt5_client_broker.h diff --git a/components/mqtt/test_apps/test_mqtt5/pytest_mqtt5_ut.py b/test/apps/mqtt5/pytest_mqtt5_ut.py similarity index 100% rename from components/mqtt/test_apps/test_mqtt5/pytest_mqtt5_ut.py rename to test/apps/mqtt5/pytest_mqtt5_ut.py diff --git a/components/mqtt/test_apps/test_mqtt5/sdkconfig.ci.default b/test/apps/mqtt5/sdkconfig.ci.default similarity index 100% rename from components/mqtt/test_apps/test_mqtt5/sdkconfig.ci.default rename to test/apps/mqtt5/sdkconfig.ci.default diff --git a/components/mqtt/test_apps/test_mqtt5/sdkconfig.defaults b/test/apps/mqtt5/sdkconfig.defaults similarity index 100% rename from components/mqtt/test_apps/test_mqtt5/sdkconfig.defaults rename to test/apps/mqtt5/sdkconfig.defaults diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/CMakeLists.txt b/test/apps/publish_connect_test/CMakeLists.txt similarity index 100% rename from tools/test_apps/protocols/mqtt/publish_connect_test/CMakeLists.txt rename to test/apps/publish_connect_test/CMakeLists.txt diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/README.md b/test/apps/publish_connect_test/README.md similarity index 100% rename from tools/test_apps/protocols/mqtt/publish_connect_test/README.md rename to test/apps/publish_connect_test/README.md diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/ca.crt b/test/apps/publish_connect_test/ca.crt similarity index 100% rename from tools/test_apps/protocols/mqtt/publish_connect_test/ca.crt rename to test/apps/publish_connect_test/ca.crt diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/ca.der b/test/apps/publish_connect_test/ca.der similarity index 100% rename from tools/test_apps/protocols/mqtt/publish_connect_test/ca.der rename to test/apps/publish_connect_test/ca.der diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/ca.key b/test/apps/publish_connect_test/ca.key similarity index 100% rename from tools/test_apps/protocols/mqtt/publish_connect_test/ca.key rename to test/apps/publish_connect_test/ca.key diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/client_inv.crt b/test/apps/publish_connect_test/client_inv.crt similarity index 100% rename from tools/test_apps/protocols/mqtt/publish_connect_test/client_inv.crt rename to test/apps/publish_connect_test/client_inv.crt diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/client_no_pwd.key b/test/apps/publish_connect_test/client_no_pwd.key similarity index 100% rename from tools/test_apps/protocols/mqtt/publish_connect_test/client_no_pwd.key rename to test/apps/publish_connect_test/client_no_pwd.key diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/client_pwd.crt b/test/apps/publish_connect_test/client_pwd.crt similarity index 100% rename from tools/test_apps/protocols/mqtt/publish_connect_test/client_pwd.crt rename to test/apps/publish_connect_test/client_pwd.crt diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/client_pwd.key b/test/apps/publish_connect_test/client_pwd.key similarity index 100% rename from tools/test_apps/protocols/mqtt/publish_connect_test/client_pwd.key rename to test/apps/publish_connect_test/client_pwd.key diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/main/CMakeLists.txt b/test/apps/publish_connect_test/main/CMakeLists.txt similarity index 100% rename from tools/test_apps/protocols/mqtt/publish_connect_test/main/CMakeLists.txt rename to test/apps/publish_connect_test/main/CMakeLists.txt diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/main/Kconfig.projbuild b/test/apps/publish_connect_test/main/Kconfig.projbuild similarity index 100% rename from tools/test_apps/protocols/mqtt/publish_connect_test/main/Kconfig.projbuild rename to test/apps/publish_connect_test/main/Kconfig.projbuild diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/main/connect_test.c b/test/apps/publish_connect_test/main/connect_test.c similarity index 100% rename from tools/test_apps/protocols/mqtt/publish_connect_test/main/connect_test.c rename to test/apps/publish_connect_test/main/connect_test.c diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/main/idf_component.yml b/test/apps/publish_connect_test/main/idf_component.yml similarity index 100% rename from tools/test_apps/protocols/mqtt/publish_connect_test/main/idf_component.yml rename to test/apps/publish_connect_test/main/idf_component.yml diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/main/mqtt_eclipseprojects_io.pem b/test/apps/publish_connect_test/main/mqtt_eclipseprojects_io.pem similarity index 100% rename from tools/test_apps/protocols/mqtt/publish_connect_test/main/mqtt_eclipseprojects_io.pem rename to test/apps/publish_connect_test/main/mqtt_eclipseprojects_io.pem diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/main/publish_connect_test.c b/test/apps/publish_connect_test/main/publish_connect_test.c similarity index 100% rename from tools/test_apps/protocols/mqtt/publish_connect_test/main/publish_connect_test.c rename to test/apps/publish_connect_test/main/publish_connect_test.c diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/main/publish_connect_test.h b/test/apps/publish_connect_test/main/publish_connect_test.h similarity index 100% rename from tools/test_apps/protocols/mqtt/publish_connect_test/main/publish_connect_test.h rename to test/apps/publish_connect_test/main/publish_connect_test.h diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/main/publish_test.c b/test/apps/publish_connect_test/main/publish_test.c similarity index 100% rename from tools/test_apps/protocols/mqtt/publish_connect_test/main/publish_test.c rename to test/apps/publish_connect_test/main/publish_test.c diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/pytest_mqtt_app.py b/test/apps/publish_connect_test/pytest_mqtt_app.py similarity index 100% rename from tools/test_apps/protocols/mqtt/publish_connect_test/pytest_mqtt_app.py rename to test/apps/publish_connect_test/pytest_mqtt_app.py diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/pytest_mqtt_publish_app.py b/test/apps/publish_connect_test/pytest_mqtt_publish_app.py similarity index 100% rename from tools/test_apps/protocols/mqtt/publish_connect_test/pytest_mqtt_publish_app.py rename to test/apps/publish_connect_test/pytest_mqtt_publish_app.py diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/sdkconfig.ci.default b/test/apps/publish_connect_test/sdkconfig.ci.default similarity index 100% rename from tools/test_apps/protocols/mqtt/publish_connect_test/sdkconfig.ci.default rename to test/apps/publish_connect_test/sdkconfig.ci.default diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/sdkconfig.ci.local_broker b/test/apps/publish_connect_test/sdkconfig.ci.local_broker similarity index 100% rename from tools/test_apps/protocols/mqtt/publish_connect_test/sdkconfig.ci.local_broker rename to test/apps/publish_connect_test/sdkconfig.ci.local_broker diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/sdkconfig.qemu b/test/apps/publish_connect_test/sdkconfig.qemu similarity index 100% rename from tools/test_apps/protocols/mqtt/publish_connect_test/sdkconfig.qemu rename to test/apps/publish_connect_test/sdkconfig.qemu diff --git a/tools/test_apps/protocols/mqtt/publish_connect_test/server.key b/test/apps/publish_connect_test/server.key similarity index 100% rename from tools/test_apps/protocols/mqtt/publish_connect_test/server.key rename to test/apps/publish_connect_test/server.key diff --git a/host_test/CMakeLists.txt b/test/host/CMakeLists.txt similarity index 100% rename from host_test/CMakeLists.txt rename to test/host/CMakeLists.txt diff --git a/host_test/README.md b/test/host/README.md similarity index 100% rename from host_test/README.md rename to test/host/README.md diff --git a/host_test/main/CMakeLists.txt b/test/host/main/CMakeLists.txt similarity index 100% rename from host_test/main/CMakeLists.txt rename to test/host/main/CMakeLists.txt diff --git a/host_test/main/Kconfig b/test/host/main/Kconfig similarity index 100% rename from host_test/main/Kconfig rename to test/host/main/Kconfig diff --git a/host_test/main/idf_component.yml b/test/host/main/idf_component.yml similarity index 100% rename from host_test/main/idf_component.yml rename to test/host/main/idf_component.yml diff --git a/host_test/main/test_mqtt_client.cpp b/test/host/main/test_mqtt_client.cpp similarity index 100% rename from host_test/main/test_mqtt_client.cpp rename to test/host/main/test_mqtt_client.cpp diff --git a/host_test/mocks/include/sys/queue.h b/test/host/mocks/include/sys/queue.h similarity index 100% rename from host_test/mocks/include/sys/queue.h rename to test/host/mocks/include/sys/queue.h diff --git a/host_test/sdkconfig.ci.coverage b/test/host/sdkconfig.ci.coverage similarity index 100% rename from host_test/sdkconfig.ci.coverage rename to test/host/sdkconfig.ci.coverage diff --git a/host_test/sdkconfig.defaults b/test/host/sdkconfig.defaults similarity index 100% rename from host_test/sdkconfig.defaults rename to test/host/sdkconfig.defaults