MQTT::respond

Description

This command transmits an MQTT message to sender of the incoming message. If called from MQTT_CLIENT_INGRESS message will be sent to the client. If called from MQTT_SERVER_INGRESS message will be sent to the server.
Please note that the current message will be forwarded to destination. Use MQTT::drop to drop the current message. This command is valid for all MQTT message types.

Syntax

MQTT::respond type CONNECT client_id <client_id>
    [keep_alive <time-in-seconds>]
    [clean_session <0 | 1>]
    [protocol_name <name-string>]
    [protocol_version <level-number>]
    [username <name-string>]
    [password <password-string>]
    [will_topic <topic-string>]
    [will_message <message-string>]
    [will_qos <0 | 1 | 2>]
    [will_retain <0 | 1>]
MQTT::respond type CONNACK return_code <0-5> [session_present <session-present-flag>]
MQTT::respond type PUBLISH topic <topic-name> payload <content>
    [<qos 0> | <qos 1 | 2> packet_id <packet-id-number>]
    [dup <0 | 1>]
    [retain <0 | 1>]
MQTT::respond type <PUBACK | PUBREC | PUBREL | PUBCOMP | UNSUBACK> packet_id <packet-id-number>
MQTT::respond type SUBSCRIBE packet_id <packet-id-number> topic_list {<topic-name> <qos>}+
MQTT::respond type SUBACK packet_id <packet-id-number> return_code_list {<return-code>}+
MQTT::respond type UNSUBSCRIBE packet_id <packet-id-number> topic_list {<topic-name> <qos>}+
MQTT::respond type <PINGREQ | PINGRESP | DISCONNECT>

MQTT::respond type CONNECT client_id
  • [keep_alive <time-in-seconds>] [clean_session <0 | 1>]
  • [protocol_name <name-string>] [protocol_version <level-number>]
  • [username <name-string>] [password <password-string>]
  • [will_topic <topic-string>] [will_message <message-string>]
  • [will_qos <0 | 1 |2>] [will_retain <0 | 1>]

The client_id is required parameter, keep_alive must be smaller than 65536. Default values for optional parameters are:
  • keep_alive -: 60 seconds.
  • clean_session -: 1
  • protocol_name -: MQTT
  • protocol_version -: 4
  • username -: “”
  • password -: “”
  • will_topic -: “”
  • will_message -: “”
  • will_qos -: 0
  • will_retain -: 0

MQTT::respond type CONNACK return_code
  • [ session_present <session-present-flag> ]

The values must be set to one of the following:.
  • 0 - Connection Accepted.
  • 1 - Connection Refused, unacceptable protocol version.
  • 2 - Connection Refused, identifier rejected.
  • 3 - Connection Refused, Server unavailable.
  • 4 - Connection Refused, bad username or password.
  • 5 - Connection Refused, not authorized. The <session-present-flag> can be set to 0 or 1. The default value is 0.

MQTT::respond type PUBLISH topic payload
  • [{qos 0} | {qos <1 | 2> packet_id <packet-id-number>} ]
  • [dup <0 | 1>]
  • [retain <0 | 1>]

For PUBLISH, the topic and payload are required parameters. If qos is 1 or 2, then packet_id must also be specified. must be smaller than 65536. Default values for optional parameters are:
  • qos -: 0
  • dup -: 0
  • retain -: 0

MQTT::respond type PUBACK packet_id
MQTT::respond type PUBREC packet_id
MQTT::respond type PUBREL packet_id
MQTT::respond type PUBCOMP packet_id
For PUBACK, PUBREC, PUBREL, and PUBCOMP, packet_id is a required parameter. No other parameter should be specified. must be smaller than 65536.
MQTT::respond type SUBSCRIBE packet_id topic_list { }+
For SUBSCRIBE, packet_id and topic_list are required parameters. No other parameter should be specified. must be smaller than 65536. can only be 0,1, or 2.
MQTT::respond type SUBACK packet_id return_code_list {}+
For SUBACK, packet_id and return_code_list are required parameters. No other parameter should be specified. must be smaller than 65536.
MQTT::respond type UNSUBSCRIBE packet_id topic_list { }+
For UNSUBSCRIBE, packet_id and topic_list are required parameters. No other parameter should be specified. Please note that values will be ignored. It is required to make format of topic_list same between SUBSCRIBE and UNSUBSCRIBE. UNSUBSCRIBE does not need the qos.
MQTT::respond type UNSUBACK packet_id
For UNSUBACK, packet_id is required parameter.
MQTT::respond type PINGREQ
For PINGREQ should be used with no parameter.
MQTT::respond type PINGRESP
For PINGRESP should be used with no parameter.
MQTT::respond type DISCONNECT
For DISCONNECT should be used with no parameter.

Examples

Enrich MQTT username with SSL client-certificate common name, reject unauthorized accesses:

when CLIENT_ACCEPTED {
    set cn ""
}

when CLIENTSSL_CLIENTCERT {
    set cn [ lindex [ split [lindex [ split [X509::subject [SSL::cert 0]] "," ] 0 ] "=" ] 1 ]
    log local0. "Client Cert Common Name: $cn"
}

when MQTT_CLIENT_INGRESS {
    if {[MQTT::type] == "CONNECT"} {
        if {$cn == ""} {
            MQTT::drop
            MQTT::respond type CONNACK return_code 5
        } else {
            set user [MQTT::username]
            MQTT::username "$cn:$user"
        }
    }
}