Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[mosquitto-dev] Session present flag in libmosquitto

Hello,

We needed to have support for MQTT 3.1.1 session present flag on CONNACK so we implemented a patch to libmosquitto to support this. I attached to this mail a patch that adds a getter method that returns 1 when the flag is set and I also added it to the C++ wrapper. I would like to upstream this patch so I would like to have some feedback in order to upstream it.

The patch has been developed for mosquitto 1.4.8, but it should apply also on newer versions.

Regards,
Davide Bettio.
From d3c37ce1b6e778535bb0186a217a21e87ed2e072 Mon Sep 17 00:00:00 2001
From: Davide Bettio <davide.bettio@xxxxxxxxxxxx>
Date: Wed, 19 Oct 2016 11:28:06 +0200
Subject: [PATCH 2/2] mosquittopp: wrap mosquitto_is_session_present

---
 lib/cpp/mosquittopp.cpp | 5 +++++
 lib/cpp/mosquittopp.h   | 1 +
 2 files changed, 6 insertions(+)

diff --git a/lib/cpp/mosquittopp.cpp b/lib/cpp/mosquittopp.cpp
index 0330181..0c778ff 100644
--- a/lib/cpp/mosquittopp.cpp
+++ b/lib/cpp/mosquittopp.cpp
@@ -269,6 +269,11 @@ int mosquittopp::opts_set(enum mosq_opt_t option, void *value)
 	return mosquitto_opts_set(m_mosq, option, value);
 }
 
+bool mosquittopp::is_session_present_on_connect()
+{
+	return mosquitto_is_session_present(m_mosq);
+}
+
 int mosquittopp::threaded_set(bool threaded)
 {
 	return mosquitto_threaded_set(m_mosq, threaded);
diff --git a/lib/cpp/mosquittopp.h b/lib/cpp/mosquittopp.h
index d3d6f13..8750d3f 100644
--- a/lib/cpp/mosquittopp.h
+++ b/lib/cpp/mosquittopp.h
@@ -79,6 +79,7 @@ class mosqpp_EXPORT mosquittopp {
 		int tls_insecure_set(bool value);
 		int tls_psk_set(const char *psk, const char *identity, const char *ciphers=NULL);
 		int opts_set(enum mosq_opt_t option, void *value);
+                bool is_session_present_on_connect();
 
 		int loop(int timeout=-1, int max_packets=1);
 		int loop_misc();
-- 
2.7.4

From 237259f1d91055d574c078bbf4bfeea86a55ad46 Mon Sep 17 00:00:00 2001
From: Davide Bettio <davide.bettio@xxxxxxxxxxxx>
Date: Wed, 19 Oct 2016 11:25:50 +0200
Subject: [PATCH 1/2] Add mosquitto_is_session_present function

mosquitto_is_session_present allows to know if CONNACK session present flag is set.
mosquitto_is_session_present should be called from a on_connect handler.
---
 lib/mosquitto.c          |  4 ++++
 lib/mosquitto.h          | 13 +++++++++++++
 lib/mosquitto_internal.h |  2 ++
 lib/read_handle_client.c |  5 +++--
 4 files changed, 22 insertions(+), 2 deletions(-)

diff --git a/lib/mosquitto.c b/lib/mosquitto.c
index cd40f77..ddac621 100644
--- a/lib/mosquitto.c
+++ b/lib/mosquitto.c
@@ -1418,3 +1418,7 @@ int mosquitto_sub_topic_tokens_free(char ***topics, int count)
 	return MOSQ_ERR_SUCCESS;
 }
 
+int mosquitto_is_session_present(struct mosquitto *mosq)
+{
+    return (mosq->connack_flags & 0x1) != 0;
+}
diff --git a/lib/mosquitto.h b/lib/mosquitto.h
index a43924f..9fa9557 100644
--- a/lib/mosquitto.h
+++ b/lib/mosquitto.h
@@ -1528,6 +1528,19 @@ libmosq_EXPORT int mosquitto_pub_topic_check(const char *topic);
  */
 libmosq_EXPORT int mosquitto_sub_topic_check(const char *topic);
 
+/*
+ * Function: mosquitto_is_session_present
+ *
+ * Return 1 is session present flag is set, otherwise 0
+ * This function is safe only on a connect callback.
+ *
+ * Parameters:
+ * 	mosq -       a valid mosquitto instance.
+ * Returns:
+ * 	session present flag (1 or 0)
+ */
+libmosq_EXPORT int mosquitto_is_session_present(struct mosquitto *mosq);
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/lib/mosquitto_internal.h b/lib/mosquitto_internal.h
index da36916..8b34d5a 100644
--- a/lib/mosquitto_internal.h
+++ b/lib/mosquitto_internal.h
@@ -259,6 +259,8 @@ struct mosquitto {
 	UT_hash_handle hh_sock;
 	struct mosquitto *for_free_next;
 #endif
+
+        uint8_t connack_flags;
 };
 
 #define STREMPTY(str) (str[0] == '\0')
diff --git a/lib/read_handle_client.c b/lib/read_handle_client.c
index 71c3231..7f945f2 100644
--- a/lib/read_handle_client.c
+++ b/lib/read_handle_client.c
@@ -24,19 +24,20 @@ Contributors:
 
 int _mosquitto_handle_connack(struct mosquitto *mosq)
 {
-	uint8_t byte;
+	uint8_t flags;
 	uint8_t result;
 	int rc;
 
 	assert(mosq);
 	_mosquitto_log_printf(mosq, MOSQ_LOG_DEBUG, "Client %s received CONNACK", mosq->id);
-	rc = _mosquitto_read_byte(&mosq->in_packet, &byte); // Reserved byte, not used
+	rc = _mosquitto_read_byte(&mosq->in_packet, &flags); // flags byte
 	if(rc) return rc;
 	rc = _mosquitto_read_byte(&mosq->in_packet, &result);
 	if(rc) return rc;
 	pthread_mutex_lock(&mosq->callback_mutex);
 	if(mosq->on_connect){
 		mosq->in_callback = true;
+                mosq->connack_flags = flags;
 		mosq->on_connect(mosq, mosq->userdata, result);
 		mosq->in_callback = false;
 	}
-- 
2.7.4


Back to the top