From d2bcdd84a1938f2e9c191cc6bf9b915b8dbf8c9d Mon Sep 17 00:00:00 2001 From: Riccardo Binetti Date: Mon, 6 Aug 2018 16:29:41 +0200 Subject: [PATCH 1/3] Add mutual SSL auth config to mqtt_client picked from master --- include/mqtt_client.h | 2 ++ mqtt_client.c | 6 ++++++ 2 files changed, 8 insertions(+) diff --git a/include/mqtt_client.h b/include/mqtt_client.h index 6e8d23c..93f0d7e 100755 --- a/include/mqtt_client.h +++ b/include/mqtt_client.h @@ -83,6 +83,8 @@ typedef struct { int task_stack; /*!< MQTT task stack size, default is 6144 bytes, can be changed in ``make menuconfig`` */ int buffer_size; /*!< size of MQTT send/receive buffer, default is 1024 */ const char *cert_pem; /*!< pointer to CERT file for server verify (with SSL), default is NULL, not required to verify the server */ + const char *client_cert_pem; /*!< pointer to CERT file for SSL mutual authentication, default is NULL, not required if mutual authentication is not needed. If it is not NULL, also `client_key_pem` has to be provided. */ + const char *client_key_pem; /*!< pointer to PEM private key file for SSL mutual authentication, default is NULL, not required if mutual authentication is not needed. If it is not NULL, also `client_cert_pem` has to be provided. */ esp_mqtt_transport_t transport; /*!< overrides URI transport */ } esp_mqtt_client_config_t; diff --git a/mqtt_client.c b/mqtt_client.c index 74ceb9f..17e78fa 100644 --- a/mqtt_client.c +++ b/mqtt_client.c @@ -299,6 +299,12 @@ esp_mqtt_client_handle_t esp_mqtt_client_init(const esp_mqtt_client_config_t *co if (config->cert_pem) { esp_transport_ssl_set_cert_data(ssl, config->cert_pem, strlen(config->cert_pem)); } + if (config->client_cert_pem) { + transport_ssl_set_client_cert_data(ssl, config->client_cert_pem, strlen(config->client_cert_pem)); + } + if (config->client_key_pem) { + transport_ssl_set_client_key_data(ssl, config->client_key_pem, strlen(config->client_key_pem)); + } esp_transport_list_add(client->transport_list, ssl, "mqtts"); if (config->transport == MQTT_TRANSPORT_OVER_SSL) { client->config->scheme = create_string("mqtts", 5); From 8b45c25fdca6e4bfe693321c368080db86b9c8ae Mon Sep 17 00:00:00 2001 From: David Cermak Date: Thu, 11 Oct 2018 17:29:19 +0200 Subject: [PATCH 2/3] corrections per renaming transports to esp_ prefixed --- mqtt_client.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mqtt_client.c b/mqtt_client.c index 17e78fa..7ee0126 100644 --- a/mqtt_client.c +++ b/mqtt_client.c @@ -300,10 +300,10 @@ esp_mqtt_client_handle_t esp_mqtt_client_init(const esp_mqtt_client_config_t *co esp_transport_ssl_set_cert_data(ssl, config->cert_pem, strlen(config->cert_pem)); } if (config->client_cert_pem) { - transport_ssl_set_client_cert_data(ssl, config->client_cert_pem, strlen(config->client_cert_pem)); + esp_transport_ssl_set_client_cert_data(ssl, config->client_cert_pem, strlen(config->client_cert_pem)); } if (config->client_key_pem) { - transport_ssl_set_client_key_data(ssl, config->client_key_pem, strlen(config->client_key_pem)); + esp_transport_ssl_set_client_key_data(ssl, config->client_key_pem, strlen(config->client_key_pem)); } esp_transport_list_add(client->transport_list, ssl, "mqtts"); if (config->transport == MQTT_TRANSPORT_OVER_SSL) { From 85f2eddabd80152264dd7362c8219306df3314ef Mon Sep 17 00:00:00 2001 From: David Cermak Date: Tue, 16 Oct 2018 09:15:44 +0200 Subject: [PATCH 3/3] commented event fields, added description of supplied user data to user event handler closes #66 --- include/mqtt_client.h | 35 ++++++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/include/mqtt_client.h b/include/mqtt_client.h index 93f0d7e..630ba17 100755 --- a/include/mqtt_client.h +++ b/include/mqtt_client.h @@ -20,14 +20,31 @@ extern "C" { typedef struct esp_mqtt_client* esp_mqtt_client_handle_t; +/** + * @brief MQTT event types. + * + * User event handler receives context data in `esp_mqtt_event_t` structure with + * - `user_context` - user data from `esp_mqtt_client_config_t` + * - `client` - mqtt client handle + * - various other data depending on event type + * + */ typedef enum { MQTT_EVENT_ERROR = 0, - MQTT_EVENT_CONNECTED, - MQTT_EVENT_DISCONNECTED, - MQTT_EVENT_SUBSCRIBED, - MQTT_EVENT_UNSUBSCRIBED, - MQTT_EVENT_PUBLISHED, - MQTT_EVENT_DATA, + MQTT_EVENT_CONNECTED, /*!< connected event, additional context: session_present flag */ + MQTT_EVENT_DISCONNECTED, /*!< disconnected event */ + MQTT_EVENT_SUBSCRIBED, /*!< subscribed event, additional context: msg_id */ + MQTT_EVENT_UNSUBSCRIBED, /*!< unsubscribed event */ + MQTT_EVENT_PUBLISHED, /*!< published event, additional context: msg_id */ + MQTT_EVENT_DATA, /*!< data event, additional context: + - msg_id message id + - topic pointer to the received topic + - topic_len length of the topic + - data pointer to the received data + - data_len length of the data for this event + - current_data_offset offset of the current data for this event + - total_data_len total length of the data received + */ } esp_mqtt_event_id_t; typedef enum { @@ -82,9 +99,9 @@ typedef struct { int task_prio; /*!< MQTT task priority, default is 5, can be changed in ``make menuconfig`` */ int task_stack; /*!< MQTT task stack size, default is 6144 bytes, can be changed in ``make menuconfig`` */ int buffer_size; /*!< size of MQTT send/receive buffer, default is 1024 */ - const char *cert_pem; /*!< pointer to CERT file for server verify (with SSL), default is NULL, not required to verify the server */ - const char *client_cert_pem; /*!< pointer to CERT file for SSL mutual authentication, default is NULL, not required if mutual authentication is not needed. If it is not NULL, also `client_key_pem` has to be provided. */ - const char *client_key_pem; /*!< pointer to PEM private key file for SSL mutual authentication, default is NULL, not required if mutual authentication is not needed. If it is not NULL, also `client_cert_pem` has to be provided. */ + const char *cert_pem; /*!< Pointer to certificate data in PEM format for server verify (with SSL), default is NULL, not required to verify the server */ + const char *client_cert_pem; /*!< Pointer to certificate data in PEM format for SSL mutual authentication, default is NULL, not required if mutual authentication is not needed. If it is not NULL, also `client_key_pem` has to be provided. */ + const char *client_key_pem; /*!< Pointer to private key data in PEM format for SSL mutual authentication, default is NULL, not required if mutual authentication is not needed. If it is not NULL, also `client_cert_pem` has to be provided. */ esp_mqtt_transport_t transport; /*!< overrides URI transport */ } esp_mqtt_client_config_t;