Skip to content

Commit 358ace0

Browse files
committed
Fix f-2752 client connack failure return codes
1 parent 511c7e0 commit 358ace0

3 files changed

Lines changed: 18 additions & 1 deletion

File tree

src/mqtt_client.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1867,6 +1867,14 @@ int MqttClient_Connect(MqttClient *client, MqttConnect *mc_connect)
18671867
/* reset state */
18681868
mc_connect->stat.write = MQTT_MSG_BEGIN;
18691869

1870+
/* CONNACK was received and decoded, but the broker refused the
1871+
* connection. The specific reason is in mc_connect->ack.return_code
1872+
* (MqttConnectAckReturnCodes for v3.1.1, MqttReasonCodes for v5). */
1873+
if (rc == MQTT_CODE_SUCCESS &&
1874+
mc_connect->ack.return_code != MQTT_CONNECT_ACK_CODE_ACCEPTED) {
1875+
rc = MQTT_TRACE_ERROR(MQTT_CODE_ERROR_CONNECT_REFUSED);
1876+
}
1877+
18701878
return rc;
18711879
}
18721880

@@ -3055,6 +3063,8 @@ const char* MqttClient_ReturnCodeToString(int return_code)
30553063
return "Error (System resource failed)";
30563064
case MQTT_CODE_ERROR_NOT_FOUND:
30573065
return "Error (Not found)";
3066+
case MQTT_CODE_ERROR_CONNECT_REFUSED:
3067+
return "Error (Broker refused connection)";
30583068
#if defined(ENABLE_MQTT_CURL)
30593069
case MQTT_CODE_ERROR_CURL:
30603070
return "Error (libcurl)";

wolfmqtt/mqtt_client.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,11 @@ WOLFMQTT_API int MqttClient_SetPropertyCallback(
290290
* \param client Pointer to MqttClient structure
291291
* \param connect Pointer to MqttConnect structure initialized
292292
with connect parameters
293-
* \return MQTT_CODE_SUCCESS or MQTT_CODE_ERROR_*
293+
* \return MQTT_CODE_SUCCESS if the broker accepted the connection,
294+
MQTT_CODE_ERROR_CONNECT_REFUSED if the broker returned a
295+
non-zero CONNACK return_code (check
296+
connect->ack.return_code for the specific reason), or
297+
another MQTT_CODE_ERROR_* for transport/protocol failures
294298
(see enum MqttPacketResponseCodes)
295299
*/
296300
WOLFMQTT_API int MqttClient_Connect(

wolfmqtt/mqtt_types.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,9 @@ enum MqttPacketResponseCodes {
203203
* a network, memory, TLS, or system error. */
204204
#endif
205205
MQTT_CODE_ERROR_PROPERTY_MISMATCH = -17,
206+
MQTT_CODE_ERROR_CONNECT_REFUSED = -18, /* Broker rejected the CONNECT;
207+
see MqttConnect.ack.return_code
208+
for the specific reason. */
206209

207210
MQTT_CODE_CONTINUE = -101,
208211
MQTT_CODE_STDIN_WAKE = -102,

0 commit comments

Comments
 (0)