File fix-reason-code-handling.patch of Package paho-mqtt-c

From 9ef8f4d865657434ff36b24af2b8980c942bacaa Mon Sep 17 00:00:00 2001
From: Ian Craggs <icraggs@gmail.com>
Date: Tue, 6 May 2025 15:36:26 +0100
Subject: [PATCH] Put MQTT 5 reason code into the connect failure reason code
 field #1596

---
 src/MQTTAsyncUtils.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/src/MQTTAsyncUtils.c b/src/MQTTAsyncUtils.c
index c084a11e3..d8315514d 100644
--- a/src/MQTTAsyncUtils.c
+++ b/src/MQTTAsyncUtils.c
@@ -1703,7 +1703,15 @@ static void nextOrClose(MQTTAsyncs* m, int rc, char* message)
 			MQTTAsync_failureData5 data = MQTTAsync_failureData5_initializer;
 
 			data.token = 0;
-			data.code = rc;
+			if (rc > 0) /* MQTT Reason Codes are > 0; C client return codes are < 0 */
+			{
+				/* MQTT 5 reason codes >= 0x00 and < 0x80 are successful,
+				 * but in that case we should not get here but be calling
+				 * onSuccess instead. */
+				data.reasonCode = rc;
+				data.code = MQTTASYNC_FAILURE;
+			} else
+				data.code = rc;
 			data.message = message;
 			Log(TRACE_MIN, -1, "Calling connect failure for client %s", m->c->clientID);
 			(*(m->connect.onFailure5))(m->connect.context, &data);
openSUSE Build Service is sponsored by