MQTT::qos

Description

This command gets or sets the qos of MQTT PUBLISH messages.

Syntax

MQTT::qos [<0 | 1 | 2>]

MQTT::qos

  • Get the qos field of MQTT PUBLISH message

MQTT::qos 0

  • Set the qos of MQTT PUBLISH message to at most once delivery. When QoS is changed to 0 from 1 or 2, the packet-id field of the PUBLISH message will be removed. Call MQTT::packet_id before calling MQTT::qos 0 to save original packet-id if needed.

MQTT::qos 1

  • Set the qos of MQTT PUBLISH message to at least once delivery. When changing from 0, this call must be followed by MQTT::packet_id to maintain MQTT protocol correctness. This is because packet-id is not present in PUBLISH messages at QoS 0, while it is required at levels 1 and 2.

MQTT::qos 2

  • Set the qos field of MQTT PUBLISH message to exactly once delivery. When changing from 0, this call must be followed by MQTT::packet_id to maintain MQTT protocol correctness. This is because packet-id is not present in PUBLISH messages at QoS 0, while it is required at levels 1 and 2.

Examples

Downgrading QoS to 0:

when MQTT_CLIENT_INGRESS {
    set mtype [MQTT::type]
    if {$mtype == "PUBLISH"} {
        set in_qos [MQTT::qos]
        if { $in_qos > 0 } {
            set pktid [MQTT::packet_id]
        }
        MQTT::qos 0
        if { $in_qos == 1 } {
            MQTT::respond type PUBACK packet_id $pktid
        } elseif { $in_qos == 2 } {
            MQTT::respond type PUBREC packet_id $pktid
        }
    } elseif {$mtype == "PUBREL"} {
        set pktid [MQTT::packet_id]
        MQTT::drop
        MQTT::respond type PUBCOMP packet_id $pktid
    }
}

Changing QoS to 1:
when CLIENT_ACCEPTED {
   set self_pktid 1
}
when MQTT_CLIENT_INGRESS {
    set cmtype [MQTT::type]
    if {$cmtype == "PUBLISH"} {
        set in_qos [MQTT::qos]
        MQTT::qos 1
        if { $in_qos == 0 } {
            MQTT::packet_id $self_pktid
            set self_pktid [$self_pktid + 1]
        }
    } elseif {$cmtype == "PUBREL"} {
        set pktid [MQTT::packet_id]
        MQTT::drop
        MQTT::respond type PUBCOMP packet_id $pktid
    }
}
when MQTT_SERVER_INGRESS {
    set smtype [MQTT::type]
    if {$smtype == "PUBACK"} {
       if {$in_qos == 0} {
           MQTT::drop
       } elseif {$in_qos == 2} {
           set pktid [MQTT::packet_id]
           MQTT::replace type PUBREC packet_id $pktid
       }
    }
}