From a6f8716fffd78f10f22b78ad4b7a826602439d3b Mon Sep 17 00:00:00 2001 From: Tuan Date: Tue, 2 Apr 2019 14:54:50 +0700 Subject: [PATCH 1/2] Add mqtt sub protocol for websocket --- mqtt_client.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/mqtt_client.c b/mqtt_client.c index db86724..a28aca9 100644 --- a/mqtt_client.c +++ b/mqtt_client.c @@ -368,6 +368,7 @@ esp_mqtt_client_handle_t esp_mqtt_client_init(const esp_mqtt_client_config_t *co esp_transport_handle_t ws = esp_transport_ws_init(tcp); ESP_MEM_CHECK(TAG, ws, goto _mqtt_init_failed); esp_transport_set_default_port(ws, MQTT_WS_DEFAULT_PORT); + esp_transport_ws_set_subprotocol(ws, "mqtt"); esp_transport_list_add(client->transport_list, ws, "ws"); if (config->transport == MQTT_TRANSPORT_OVER_WS) { client->config->scheme = create_string("ws", 2); @@ -398,6 +399,7 @@ esp_mqtt_client_handle_t esp_mqtt_client_init(const esp_mqtt_client_config_t *co #if MQTT_ENABLE_WSS esp_transport_handle_t wss = esp_transport_ws_init(ssl); ESP_MEM_CHECK(TAG, wss, goto _mqtt_init_failed); + esp_transport_ws_set_subprotocol(wss, "mqtt"); esp_transport_set_default_port(wss, MQTT_WSS_DEFAULT_PORT); esp_transport_list_add(client->transport_list, wss, "wss"); if (config->transport == MQTT_TRANSPORT_OVER_WSS) { From 48cd04baf14ad61e9496206aeba8f7ef96d0c283 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Fri, 24 May 2019 11:04:42 +0200 Subject: [PATCH 2/2] defined macros for supported features in esp-idf based on idf version --- include/mqtt_supported_features.h | 38 +++++++++++++++++++++++++++++++ mqtt_client.c | 6 +++++ 2 files changed, 44 insertions(+) create mode 100644 include/mqtt_supported_features.h diff --git a/include/mqtt_supported_features.h b/include/mqtt_supported_features.h new file mode 100644 index 0000000..9a3ab04 --- /dev/null +++ b/include/mqtt_supported_features.h @@ -0,0 +1,38 @@ +// Copyright 2015-2019 Espressif Systems (Shanghai) PTE 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. + +#ifndef _MQTT_SUPPORTED_FEATURES_H_ +#define _MQTT_SUPPORTED_FEATURES_H_ + +#if __has_include("esp_idf_version.h") +#include "esp_idf_version.h" +#endif + +/** + * @brief This header defines supported features of IDF which mqtt module + * could use depending on specific version of ESP-IDF. + * In case "esp_idf_version.h" were not found, all additional + * features would be disabled + */ + +#ifdef ESP_IDF_VERSION +#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 0, 0) + +#define MQTT_SUPPORTED_FEATURE_WS_SUBPROTOCOL + +#endif +#endif + + +#endif // _MQTT_SUPPORTED_FEATURES_H_ \ No newline at end of file diff --git a/mqtt_client.c b/mqtt_client.c index a28aca9..a3e2fce 100644 --- a/mqtt_client.c +++ b/mqtt_client.c @@ -9,6 +9,7 @@ #include "esp_transport_ws.h" #include "platform.h" #include "mqtt_outbox.h" +#include "mqtt_supported_features.h" /* using uri parser */ #include "http_parser.h" @@ -26,6 +27,7 @@ # define MQTT_API_UNLOCK_FROM_OTHER_TASK(c) { if (c->task_handle != xTaskGetCurrentTaskHandle()) { xSemaphoreGive(c->api_lock); } } #endif /* MQTT_USE_API_LOCKS */ + static const char *TAG = "MQTT_CLIENT"; /** @@ -368,7 +370,9 @@ esp_mqtt_client_handle_t esp_mqtt_client_init(const esp_mqtt_client_config_t *co esp_transport_handle_t ws = esp_transport_ws_init(tcp); ESP_MEM_CHECK(TAG, ws, goto _mqtt_init_failed); esp_transport_set_default_port(ws, MQTT_WS_DEFAULT_PORT); +#ifdef MQTT_SUPPORTED_FEATURE_WS_SUBPROTOCOL esp_transport_ws_set_subprotocol(ws, "mqtt"); +#endif esp_transport_list_add(client->transport_list, ws, "ws"); if (config->transport == MQTT_TRANSPORT_OVER_WS) { client->config->scheme = create_string("ws", 2); @@ -399,7 +403,9 @@ esp_mqtt_client_handle_t esp_mqtt_client_init(const esp_mqtt_client_config_t *co #if MQTT_ENABLE_WSS esp_transport_handle_t wss = esp_transport_ws_init(ssl); ESP_MEM_CHECK(TAG, wss, goto _mqtt_init_failed); +#ifdef MQTT_SUPPORTED_FEATURE_WS_SUBPROTOCOL esp_transport_ws_set_subprotocol(wss, "mqtt"); +#endif esp_transport_set_default_port(wss, MQTT_WSS_DEFAULT_PORT); esp_transport_list_add(client->transport_list, wss, "wss"); if (config->transport == MQTT_TRANSPORT_OVER_WSS) {