wolfSSL / wolfMQTT

wolfMQTT is a small, fast, portable MQTT client implementation, including support for TLS 1.3.
https://www.wolfssl.com
GNU General Public License v2.0
518 stars 156 forks source link

Call disconn CB with MQTTv5 reason code #314

Closed embhorn closed 1 year ago

embhorn commented 1 year ago

Functional extension of the WolfMQTT library with an interface providing the Reason Code on MQTT disconnect triggered by the broker.

If the broker sends a DISCONNECT with a Reason Code allow client to be able to read the corresponding Reason Code value as numerical value using the disconnect callback.

This addresses ZD15147

Test with mqttclient adding an invalid publish to the # topic:

    {
        MqttPublish publish;
        /* Publish an invalid topic */
        XMEMSET(&publish, 0, sizeof(MqttPublish));
        publish.qos = mqttCtx->qos;
        publish.topic_name = "####";
        publish.packet_id = mqtt_get_packetid();
        publish.buffer = (byte*)mqttCtx->message;
        publish.total_len = (word16)XSTRLEN(mqttCtx->message);
        rc = MqttClient_Publish(&mqttCtx->client, &publish);
        if (rc != MQTT_CODE_SUCCESS) {
            goto exit;
        }
        PRINTF("MQTT Publish invalid: Topic %s, Qos %d, Message %s",
            publish.topic_name, publish.qos, publish.buffer);
    }

Build wolfMQTT with:

./configure --enable-v5
make

Connect to the test broker with:

./examples/mqttclient/mqttclient -h 833f87e253304692bd2b911f0c18dba1.s1.eu.hivemq.cloud -t -S -u wolf1 -w NEZjcm7i8eRjFKF -p 8883

In the output you'll see:

MQTT Publish invalid: Topic ####, Qos 0, Message test
MQTT Waiting for message...
Property CB: Type 31
Reason String: PUBLISH with wildcard character (#/+) was sent.
Network Error Callback: Unknown (error 144)
Network Error Callback: Error (Network) (error -8)
MQTT Message Wait: Error (Network) (-8)
MQTT Disconnect: Success (0)
MQTT Socket Disconnect: Success (0)
embhorn commented 1 year ago

Waiting for customer feedback before merge.

embhorn commented 1 year ago

Customer confirmed functionality.