From 1ece141c52132aacdaf6c56d680ef91678a70dc6 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Fri, 16 Nov 2018 10:42:58 +0000 Subject: [PATCH 1/2] mqtt: support for BEFORE_CONNECT event in idf Updated examples to use new event id, idf to use mqtt with fixed retained, oversized messages --- components/mqtt/esp-mqtt | 2 +- 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 +++ 6 files changed, 16 insertions(+), 1 deletion(-) diff --git a/components/mqtt/esp-mqtt b/components/mqtt/esp-mqtt index a7b1cea5b..f08f3b678 160000 --- a/components/mqtt/esp-mqtt +++ b/components/mqtt/esp-mqtt @@ -1 +1 @@ -Subproject commit a7b1cea5b3e246298607a8c64447765297626f36 +Subproject commit f08f3b678717865234637164a29ed3a63e756ca7 diff --git a/examples/protocols/mqtt/ssl/main/app_main.c b/examples/protocols/mqtt/ssl/main/app_main.c index 913ba6cfe..86697a03c 100644 --- a/examples/protocols/mqtt/ssl/main/app_main.c +++ b/examples/protocols/mqtt/ssl/main/app_main.c @@ -116,6 +116,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 2ebce48b9..62b970056 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 0d294bd46..ce1640adb 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 1db332788..024be48bd 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 e337ea106..50595495a 100644 --- a/examples/protocols/mqtt/wss/main/app_main.c +++ b/examples/protocols/mqtt/wss/main/app_main.c @@ -116,6 +116,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 9ebaf36c288b325a0ab52058ce7381d030585089 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Tue, 20 Nov 2018 03:31:19 +0100 Subject: [PATCH 2/2] tcp_transport: modification of ws to read headers first in order to read the exact payload closes https://github.com/espressif/esp-mqtt/issues/69 --- components/tcp_transport/transport_ws.c | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/components/tcp_transport/transport_ws.c b/components/tcp_transport/transport_ws.c index b453a3e55..55ea6d115 100644 --- a/components/tcp_transport/transport_ws.c +++ b/components/tcp_transport/transport_ws.c @@ -191,7 +191,10 @@ static int ws_read(esp_transport_handle_t t, char *buffer, int len, int timeout_ if ((poll_read = esp_transport_poll_read(ws->parent, timeout_ms)) <= 0) { return poll_read; } - if ((rlen = esp_transport_read(ws->parent, buffer, len, timeout_ms)) <= 0) { + + // Receive and process header first (based on header size) + int header = 2; + if ((rlen = esp_transport_read(ws->parent, buffer, header, timeout_ms)) <= 0) { ESP_LOGE(TAG, "Error read data"); return rlen; } @@ -203,11 +206,20 @@ static int ws_read(esp_transport_handle_t t, char *buffer, int len, int timeout_ ESP_LOGD(TAG, "Opcode: %d, mask: %d, len: %d\r\n", opcode, mask, payload_len); if (payload_len == 126) { // headerLen += 2; + if ((rlen = esp_transport_read(ws->parent, data_ptr, header, timeout_ms)) <= 0) { + ESP_LOGE(TAG, "Error read data"); + return rlen; + } payload_len = data_ptr[0] << 8 | data_ptr[1]; payload_len_buff = len - 4; data_ptr += 2; } else if (payload_len == 127) { // headerLen += 8; + header = 8; + if ((rlen = esp_transport_read(ws->parent, data_ptr, header, timeout_ms)) <= 0) { + ESP_LOGE(TAG, "Error read data"); + return rlen; + } if (data_ptr[0] != 0 || data_ptr[1] != 0 || data_ptr[2] != 0 || data_ptr[3] != 0) { // really too big! @@ -218,6 +230,11 @@ static int ws_read(esp_transport_handle_t t, char *buffer, int len, int timeout_ data_ptr += 8; payload_len_buff = len - 10; } + // Then receive and process payload + if ((rlen = esp_transport_read(ws->parent, data_ptr, payload_len, timeout_ms)) <= 0) { + ESP_LOGE(TAG, "Error read data"); + return rlen; + } if (payload_len > payload_len_buff) { ESP_LOGD(TAG, "Actual data received (%d) are longer than mqtt buffer (%d)", payload_len, payload_len_buff); payload_len = payload_len_buff;