+ All Categories
Home > Documents > MQTT for Sensor Networks (MQTT-SN) Version 1.3 · Web viewFeb 22, 2021  · A MQTT-SN GW may or may...

MQTT for Sensor Networks (MQTT-SN) Version 1.3 · Web viewFeb 22, 2021  · A MQTT-SN GW may or may...

Date post: 16-Apr-2021
Category:
Upload: others
View: 13 times
Download: 0 times
Share this document with a friend
72
MQTT for Sensor Networks (MQTT- SN) Version 2.0 1.3 Working Draft 0 5 4 3 2 16 April 04 March 2020 17 22 February 2021 Technical Committee: OASIS Message Queuing Telemetry Transport (MQTT) TC Chairs: Ian Craggs ( [email protected] ), Individual member Allan Stockdill-Mander ([email protected]), IBM Editors: Ian Craggs ( [email protected] ), Individual member Andrew Banks ([email protected]), IBM Simon Johnson ([email protected] [email protected] ), Thingstream AG ublox AG Rahul Gupta ([email protected]), IBM Additional artifacts: This prose specification is one component of a Work Product that also includes: XML schemas: (list file names or directory name) Other parts (list titles and/or file names) ( Note: Any normative computer language definitions that are part of the Work Product, such as XML instances, schemas and Java(TM) code, including fragments of such, must be (a) well formed and valid, (b) provided in separate plain text files, (c) referenced from the Work Product; and (d) where any definition in these separate files disagrees with the definition found in the specification, the definition in the separate file prevails. Remove this note before submitting for publication.) Related work: This specification is related to: MQTT Version 5.0. Edited by Andrew Banks, Ed Briggs, Ken Borgendale, and Rahul Gupta. OASIS Standard. Latest version: https://docs.oasis-open.org/mqtt/mqtt/v5.0/mqtt-v5.0.html. MQTT Version 3.1.1. Edited by Andrew Banks and Rahul Gupta. OASIS Standard. Latest version: http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/mqtt- v3.1.1.html. Abstract: This specification defines the MQTT for Sensor Networks protocol (MQTT-SN). It is closely related to the MQTT v3.1.1 and MQTT v5.0 standards. mqtt-sn-v2.0 v1.3 -wd05 3 2 Working Draft 05 3 2 22 February 16 April 04 March 2020 2021 Standards Track Draft Copyright © OASIS Open 2021 0 . All Rights Reserved. Page 1 of 72
Transcript
Page 1: MQTT for Sensor Networks (MQTT-SN) Version 1.3 · Web viewFeb 22, 2021  · A MQTT-SN GW may or may not be integrated with a MQTT server. In case of a stand-alone GW the MQTT protocol

MQTT for Sensor Networks (MQTT-SN) Version 2.01.3Working Draft 054 32

16 April 04 March 20201722 February 2021Technical Committee:OASIS Message Queuing Telemetry Transport (MQTT) TC

Chairs:Ian Craggs ([email protected]), Individual memberAllan Stockdill-Mander ([email protected]), IBM

Editors:Ian Craggs ([email protected]), Individual memberAndrew Banks ([email protected]), IBMSimon Johnson ([email protected]@thingstream.io), Thingstream AGublox AGRahul Gupta ([email protected]), IBM

Additional artifacts:This prose specification is one component of a Work Product that also includes: XML schemas: (list file names or directory name) Other parts (list titles and/or file names) (Note: Any normative computer language definitions that are part of the Work Product, such as XML

instances, schemas and Java(TM) code, including fragments of such, must be (a) well formed and valid, (b) provided in separate plain text files, (c) referenced from the Work Product; and (d) where any definition in these separate files disagrees with the definition found in the specification, the definition in the separate file prevails. Remove this note before submitting for publication.)

Related work:This specification is related to: MQTT Version 5.0. Edited by Andrew Banks, Ed Briggs, Ken Borgendale, and Rahul Gupta. OASIS

Standard. Latest version: https://docs.oasis-open.org/mqtt/mqtt/v5.0/mqtt-v5.0.html. MQTT Version 3.1.1. Edited by Andrew Banks and Rahul Gupta. OASIS Standard. Latest version:

http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/mqtt-v3.1.1.html.

Abstract:This specification defines the MQTT for Sensor Networks protocol (MQTT-SN). It is closely related to the MQTT v3.1.1 and MQTT v5.0 standards.

Status:This Working Draft (WD) has been produced by one or more TC Members; it has not yet been voted on by the TC or approved as a Committee Draft (Committee Specification Draft or a Committee Note Draft). The OASIS document Approval Process begins officially with a TC vote to approve a WD as a Committee Draft. A TC may approve a Working Draft, revise it, and re-approve it any number of times as a Committee Draft.This specification is provided under the Non-Assertion Mode of the OASIS IPR Policy, the mode chosen when the Technical Committee was established. For information on whether any patents have been disclosed that may be essential to implementing this specification, and any offers of patent licensing terms, please refer to the Intellectual Property Rights section of the TC’s web page (https://www.oasis-open.org/committees/mqtt/ipr.php).Note that any machine-readable content (Computer Language Definitions) declared Normative for this Work Product is provided in separate plain text files. In the event of a discrepancy between any such plain text file and display content in the Work Product's prose narrative document(s), the content in the separate plain text file prevails.mqtt-sn-v2.0v1.3-wd0532 Working Draft 0532 22 February16 April04 March 20202021Standards Track Draft Copyright © OASIS Open 20210. All Rights Reserved. Page 1 of 58

Page 2: MQTT for Sensor Networks (MQTT-SN) Version 1.3 · Web viewFeb 22, 2021  · A MQTT-SN GW may or may not be integrated with a MQTT server. In case of a stand-alone GW the MQTT protocol

URI patterns:Initial publication URI:https://docs.oasis-open.org/mqtt/mqtt-sn/v1.3/csd01/mqtt-sn-v1.3-csd01.docxPermanent "Latest stage" URI:https://docs.oasis-open.org/mqtt/mqtt-sn/v1.3/mqtt-sn-v1.3.docx(Note: Publication URIs are managed by OASIS TC Administration; please don't modify. The OASIS TC Process requires that Work Products at any level of approval must use the OASIS file naming scheme, and must include the OASIS copyright notice. The URIs above have been constructed according to the file naming scheme. Remove this note before submitting for publication.)

Copyright © OASIS Open 2020. All Rights Reserved.All capitalized terms in the following text have the meanings assigned to them in the OASIS Intellectual Property Rights Policy (the "OASIS IPR Policy"). The full Policy may be found at the OASIS website.This document and translations of it may be copied and furnished to others, and derivative works that comment on or otherwise explain it or assist in its implementation may be prepared, copied, published, and distributed, in whole or in part, without restriction of any kind, provided that the above copyright notice and this section are included on all such copies and derivative works. However, this document itself may not be modified in any way, including by removing the copyright notice or references to OASIS, except as needed for the purpose of developing any document or deliverable produced by an OASIS Technical Committee (in which case the rules applicable to copyrights, as set forth in the OASIS IPR Policy, must be followed) or as required to translate it into languages other than English.The limited permissions granted above are perpetual and will not be revoked by OASIS or its successors or assigns.This document and the information contained herein is provided on an "AS IS" basis and OASIS DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTY THAT THE USE OF THE INFORMATION HEREIN WILL NOT INFRINGE ANY OWNERSHIP RIGHTS OR ANY IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.

mqtt-sn-v2.0v1.3-wd0532 Working Draft 0532 22 February16 April04 March 20202021Standards Track Draft Copyright © OASIS Open 20210. All Rights Reserved. Page 2 of 58

Page 3: MQTT for Sensor Networks (MQTT-SN) Version 1.3 · Web viewFeb 22, 2021  · A MQTT-SN GW may or may not be integrated with a MQTT server. In case of a stand-alone GW the MQTT protocol

Table of Contents1 Introduction......................................................................................................................................... 5

1.1 IPR Policy.......................................................................................................................................... 51.2 Terminology....................................................................................................................................... 51.3 Normative References....................................................................................................................... 51.4 Non-Normative References...............................................................................................................51.5 Background....................................................................................................................................... 61.6 MQTT-SN comparson with MQTT.....................................................................................................71.7 MQTT-SN Architecture...................................................................................................................... 8

1.7.1 Transparent Gateway................................................................................................................. 91.7.2 Aggregating Gateway................................................................................................................. 9

2 Data representation........................................................................................................................... 101.1.1 Bits(Byte).................................................................................................................................. 101.1.2 Two Byte Integer......................................................................................................................101.1.3 Three Byte Integer................................................................................................................... 10

3 MQTT-SN Control Packet format......................................................................................................111.2 Structure of an MQTT-SN Control Packet.......................................................................................11

1.2.1 Header..................................................................................................................................... 113.1.1 Length...................................................................................................................................... 113.1.2 MQTT-SN Control Packeyt Type..............................................................................................12

3.2 MQTT-SN Control Packet Variable Part..........................................................................................133.2.1 ClientId..................................................................................................................................... 133.2.2 Data......................................................................................................................................... 133.2.3 Duration.................................................................................................................................... 133.2.4 Flags........................................................................................................................................ 133.2.5 GwAdd..................................................................................................................................... 133.2.6 GwId......................................................................................................................................... 143.2.7 MsgId....................................................................................................................................... 143.2.8 ProtocolId................................................................................................................................. 143.2.9 Radius...................................................................................................................................... 143.2.10 ReturnCode............................................................................................................................ 143.2.11 TopicId................................................................................................................................... 143.2.12 TopicName............................................................................................................................. 143.2.13 WillMsg................................................................................................................................... 143.2.14 WillTopic................................................................................................................................. 15

4 MQTT-SN Control Packets...............................................................................................................164.1 Format of Individual Messages........................................................................................................16

4.1.1 ADVERTISE............................................................................................................................. 164.1.2 SEARCHGW............................................................................................................................ 164.1.3 GWINFO.................................................................................................................................. 164.1.4 CONNECT............................................................................................................................... 174.1.5 CONNACK............................................................................................................................... 174.1.6 WILLTOPICREQ......................................................................................................................184.1.7 WILLTOPIC.............................................................................................................................. 184.1.8 WILLMSGREQ......................................................................................................................... 18

mqtt-sn-v2.0v1.3-wd0532 Working Draft 0532 22 February16 April04 March 20202021Standards Track Draft Copyright © OASIS Open 20210. All Rights Reserved. Page 3 of 58

Page 4: MQTT for Sensor Networks (MQTT-SN) Version 1.3 · Web viewFeb 22, 2021  · A MQTT-SN GW may or may not be integrated with a MQTT server. In case of a stand-alone GW the MQTT protocol

4.1.9 WILLMSG................................................................................................................................. 184.1.10 REGISTER............................................................................................................................. 194.1.11 REGACK................................................................................................................................ 194.1.12 PUBLISH................................................................................................................................ 194.1.13 PUBACK................................................................................................................................ 204.1.14 PUBREC, PUBREL, and PUBCOMP.....................................................................................204.1.15 SUBSCRIBE..........................................................................................................................214.1.16 SUBACK................................................................................................................................ 214.1.17 UNSUBSCRIBE..................................................................................................................... 224.1.18 UNSUBACK........................................................................................................................... 224.1.19 PINGREQ............................................................................................................................... 224.1.20 PINGRESP............................................................................................................................. 234.1.21 DISCONNECT....................................................................................................................... 234.1.22 WILLTOPICUPD....................................................................................................................234.1.23 WILLMSGUPD....................................................................................................................... 244.1.24 WILLTOPICRESP..................................................................................................................244.1.25 WILLMSGRESP..................................................................................................................... 24

4.2 Forwarder Encapsulation................................................................................................................. 242 Operational behavior......................................................................................................................... 26

4.3 Gateway Advertisement and Discovery...........................................................................................264.4 Client’s Connection Setup...............................................................................................................274.5 Clean session.................................................................................................................................. 274.6 Procedure for updating the Will data...............................................................................................284.7 Topic Name Registration Procedure................................................................................................284.8 Client’s Publish Procedure..............................................................................................................284.9 Pre-defined topic ids and short topic names....................................................................................294.10 PUBLISH with QoS Level -1..........................................................................................................294.11 Client’s Topic Subscribe/Un-subscribe Procedure........................................................................294.12 Gateway’s Publish Procedure.......................................................................................................304.13 Keep Alive and PING Procedure...................................................................................................304.14 Client’s Disconnect Procedure......................................................................................................314.15 Client’s Retransmission Procedure................................................................................................314.16 Support of sleeping clients............................................................................................................31

5 Safety, Security, and Data Protection Considerations.......................................................................336 Conformance..................................................................................................................................... 34Appendix A. Acknowledgments................................................................................................................. 35Appendix B. Implementation Notes............................................................................................................36

B.1 Support of QoS Level -1.................................................................................................................. 36B.2 “Best practice” values for timers and counters................................................................................36B.3 Mapping of Topic Ids to Topic Names.............................................................................................36B.4 ZigBee related issues...................................................................................................................... 36

Appendix C. Revision History.................................................................................................................... 38

mqtt-sn-v2.0v1.3-wd0532 Working Draft 0532 22 February16 April04 March 20202021Standards Track Draft Copyright © OASIS Open 20210. All Rights Reserved. Page 4 of 58

Page 5: MQTT for Sensor Networks (MQTT-SN) Version 1.3 · Web viewFeb 22, 2021  · A MQTT-SN GW may or may not be integrated with a MQTT server. In case of a stand-alone GW the MQTT protocol

1 Introduction[All text is normative unless otherwise labeled]

1.1 IPR PolicyThis specification is provided under the Non-Assertion Mode of the OASIS IPR Policy, the mode chosen when the Technical Committee was established. For information on whether any patents have been disclosed that may be essential to implementing this specification, and any offers of patent licensing terms, please refer to the Intellectual Property Rights section of the TC’s web page (https://www.oasis-open.org/committees/mqtt/ipr.php).

1.2 TerminologyThe key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be interpreted as described in [RFC2119] and [RFC8174] when, and only when, they appear in all capitals, as shown here.

1.3 Normative References[RFC2119] Bradner, S., "Key words for use in RFCs to Indicate Requirement Levels", BCP

14, RFC 2119, DOI 10.17487/RFC2119, March 1997, <http://www.rfc-editor.org/info/rfc2119>.

[RFC8174] Leiba, B., "Ambiguity of Uppercase vs Lowercase in RFC 2119 Key Words", BCP 14, RFC 8174, DOI 10.17487/RFC8174, May 2017, <http://www.rfc-editor.org/info/rfc8174>.

[Reference] [Full reference citation]

1.4 Non-Normative References[RFC3552] Rescorla, E. and B. Korver, "Guidelines for Writing RFC Text on Security

Considerations", BCP 72, RFC 3552, DOI 10.17487/RFC3552, July 2003, <https://www.rfc-editor.org/info/rfc3552>.

[Reference] [Full reference citation]

(Note: Any work cited in the body of the text as needed to implement the specification must be listed here. Each reference to a separate document or artifact in this work must be listed here and must be identified as either a Normative or a Non-Normative Reference.For all References – Normative and Non-Normative:Recommended approach: Set up [Reference] label elements as "Bookmarks", then create hyperlinks to them within the document. (Here's how: Insert hyperlinkàPlace in this documentàscroll down to Bookmarks, select appropriate one.)Use the "Ref term" and "Ref" paragraph styles to format references.

The proper format for citation of technical work produced by an OASIS TC (whether Standards Track or Non-Standards Track) is:

[Citation Label] Work Product title (italicized). Edited by Albert Alston, Bob Ballston, and Calvin Carlson. Approval date (DD Month YYYY). OASIS Stage Identifier and Revision Number (e.g., OASIS Committee Specification Draft 01). Principal URI (stage-specific URI, e.g., with stage component: somespec-v1.0-csd01.html). Latest stage: (latest stage URI, without stage identifiers).

For example:mqtt-sn-v2.0v1.3-wd0532 Working Draft 0532 22 February16 April04 March 20202021Standards Track Draft Copyright © OASIS Open 20210. All Rights Reserved. Page 5 of 58

Page 6: MQTT for Sensor Networks (MQTT-SN) Version 1.3 · Web viewFeb 22, 2021  · A MQTT-SN GW may or may not be integrated with a MQTT server. In case of a stand-alone GW the MQTT protocol

[OpenDoc-1.2] Open Document Format for Office Applications (OpenDocument) Version 1.2. Edited by Patrick Durusau and Michael Brauer. 19 January 2011. OASIS Committee Specification Draft 07. https://docs.oasis-open.org/office/v1.2/csd07/OpenDocument-v1.2-csd07.html. Latest stage: https://docs.oasis-open.org/office/v1.2/OpenDocument-v1.2.html.

Reference sources:For references to IETF RFCs, use the approved citation formats at:https://docs.oasis-open.org/templates/ietf-rfc-list/ietf-rfc-list.html.For references to W3C Recommendations, use the approved citation formats at:https://docs.oasis-open.org/templates/w3c-recommendations-list/w3c-recommendations-list.html.Remove this note before submitting for publication.)

1.5 BackgroundThere is a recent increase of interest in Wireless Sensor Networks (WSNs), both from commercial and technical point of view, due to their simplicity, low cost and easy deployment. Those networks can serve different purposes, from measurement and detection, to automation and process control. A typical WSN consists of a large number of battery-operated sensors and actuators (SAs), which are usually equipped with a limited amount of storage and processing capabilities. It is important that those devices communicate wirelessly, since the number of SA-nodes is typically very large, and the cost of deployment of a wired infrastructure is prohibitively expensive. Such a network is by nature very dynamic: the wireless links may temporarily break at any time, and nodes may fail and be replaced very often. In such situations the conventional approach of using addresses for communicating with the individual nodes may become a nightmare. Applications residing on the fixed network and requiring interactions with the wireless SA devices would need to manage and maintain means to communicate with a large number of nodes. In most cases they do not need to know the address or identity of the devices which deliver the information but are more interested in the content of the data. For example, an asset tracking application is more interested in the current location of a certain asset than in the network address of the GPS receivers that deliver that information. In addition, several applications may have interest in the same sensor data but for different purposes. In this case the SA nodes would need to manage and maintain communication means with multiple applications in parallel. This might exceed the limited capabilities of the simple and low-cost SA devices.

Another problem is the difference in the addressing schemes between the networks involved. For example, how does an application residing on a TCP/IP-based network address

a SA device running on a ZigBee R 1-based wireless network?

The problem described above may be overcome by using a data-centric communication approach, in which information is delivered to the receivers not based on their network addresses but rather as a function of their contents and interests. One well-known example of data-centric communication is the “Publish/Subscribe” (pub/sub) messaging system which is already being widely used in enterprise networks, mainly due to their scalability and support of dynamic network topology. Extending the enterprise pub/sub system into the WSNs also enables a seamless integration of the WSNs into the enterprise network, thus making the field data collected by the SAs available to all applications as any other enterprise information and enabling the control of the SAs from any enterprise application. This can be for example achieved by using the MQTT protocol, which is an open and lightweight publish/subscribe protocol designed specifically for machine-to-machine and mobile applications. It is optimized for communications over networks where bandwidth is at a premium or where the network connection could be intermittent. However MQTT requires an underlying network, such as

1 ZigBee is a trademark of ZigBee Alliance in the US, other countries or both. Other company, product or service names may be trademarks or service marks of others.

mqtt-sn-v2.0v1.3-wd0532 Working Draft 0532 22 February16 April04 March 20202021Standards Track Draft Copyright © OASIS Open 20210. All Rights Reserved. Page 6 of 58

Page 7: MQTT for Sensor Networks (MQTT-SN) Version 1.3 · Web viewFeb 22, 2021  · A MQTT-SN GW may or may not be integrated with a MQTT server. In case of a stand-alone GW the MQTT protocol

TCP/IP, that provides an ordered lossless connection capability and this is too complex for very simple, small footprint, and low-cost devices such as wireless SAs.

The purpose of this document is to specify MQTT-SN, a pub/sub protocol for wireless sensor networks. MQTT-SN can be considered as a version of MQTT which is adapted to the peculiarities of a wireless communication environment. Wireless radio links have in general a higher failure rates than wired ones due to their susceptibility to fading and interference disturbances. They have also a lower transmission rate. For example, WSNs based on the IEEE 802.15.4 standard provide a maximum bandwidth of 250 kbit/s in the 2.4 GHz band. Moreover, to be resistant against transmission errors, their packets have a very short length. In the case of IEEE 802.15.4, the packet length at the physical layer is limited to 128 bytes. Half of these 128 bytes could be taken away by the overhead information required by supporting functions such as MAC layer, networking, security, etc.

MQTT-SN is also optimized for implementation on low-cost, battery-operated devices with limited processing and storage resources.

MQTT-SN was originally developed for running on top of the ZigBee APS layer. ZigBee is an open industrial consortium with the aim of defining an open and global communication

standard for WSNs. To be global ZigBee has selected the IEEE standard 802.15.4 as the protocol for the PHY and MAC layers, and adds on top on this standard the required network, security and application layers, thus providing interoperability between products of different vendors.

MQTT-SN is designed in such a way that it is agnostic of the underlying networking services. Any network which provides a bi-directional data transfer service between any node and a particular one (a gateway) should be able to support MQTT-SN. For example a simple datagram service which allows a source endpoint to send a data message to a specific destination endpoint should be sufficient. A broadcast data transfer service is only required if the gateway discovery procedure is employed. To reduce the broadcast traffic created by the discovery procedure, it is desirable that MQTT-SN could indicate the required broadcast radius to the underlying layer.

ZigBee is a trademark of ZigBee Alliance in the US, other countries or both. Other company, product or service names may be trademarks or service marks of others.

1.6 MQTT-SN comparison with MQTTMQTT-SN is designed to be as close as possible to MQTT, but is adapted to the peculiarities of a wireless communication environment such as low bandwidth, high link failures, short message length, etc. It is also optimized for the implementation on low-cost, battery-operated devices with limited processing and storage resources. Compared to MQTT, MQTT-SN is characterized by the following differences:1. The CONNECT message is split into three messages. The two additional ones are

optional and used to transfer the Will topic and the Will message to the server.

2. To cope with the short message length and the limited transmission bandwidth in wireless networks, the topic name in the PUBLISH messages is replaced by a short, two-byte long “topic id”. A registration procedure is defined to allow clients to register their topic names with the server/gateway and obtain the corresponding topic ids. It is also used in the opposite direction to inform the client about the topic name and the corresponding topic id that will be included in a following PUBLISH message.

3. “Pre-defined” topic ids and “short” topic names are introduced, for which no registration is required. Predefined topic ids are also a two-byte long replacement of the topic name, their mapping to the topic names is however known in advance by both the client’s application and the gateway/server. Therefore both sides can start using pre-defined

mqtt-sn-v2.0v1.3-wd0532 Working Draft 0532 22 February16 April04 March 20202021Standards Track Draft Copyright © OASIS Open 20210. All Rights Reserved. Page 7 of 58

Page 8: MQTT for Sensor Networks (MQTT-SN) Version 1.3 · Web viewFeb 22, 2021  · A MQTT-SN GW may or may not be integrated with a MQTT server. In case of a stand-alone GW the MQTT protocol

topic ids; there is no need for a registration as in the case of “normal” topic ids mentioned above.

Short topic names are topic names that have a fixed length of two bytes. They are short enough for being carried together with the data within PUBLISH messages. As for pre-defined topic ids, there is also no need for a registration for short topic names.

4. A discovery procedure helps clients without a pre-configured server/gateway’s address to discover the actual network address of an operating server/gateway. Multiple gateways may be present at the same time within a single wireless network and can co-operate in a load-sharing or stand-by mode.

5. The semantic of a “clean session” is extended to the Will feature, i.e. not only client’s subscriptions are persistent, but also Will topic and Will message. A client can also modify its Will topic and Will message during a session.

6. A new offline keep-alive procedure is defined for the support of sleeping clients. With this procedure, battery-operated devices can go to a sleeping state during which all messages destined to them are buffered at the server/gateway and delivered later to them when they wake up.

1.7 MQTT-SN Architecture

Figure 1: MQTT-SN Architecture

The architecture of MQTT-SN is shown in Fig. 1. There are three kinds of MQTT-SN components, MQTT-SN clients, MQTT-SN gateways (GW), and MQTT-SN forwarders. MQTT-SN clients connect themselves to a MQTT server via a MQTT-SN GW using the MQTT-SN protocol. A MQTT-SN GW may or may not be integrated with a MQTT server. In case of a stand-alone GW the MQTT protocol is used between the MQTT server and the MQTT-SN GW. Its main function is the translation between MQTT and MQTT-SN.

MQTT-SN clients can also access a GW via a forwarder in case the GW is not directly attached to their network. The forwarder simply encapsulates the MQTT-SN frames it receives on the wireless side and forwards them unchanged to the GW; in the opposite direction, it

mqtt-sn-v2.0v1.3-wd0532 Working Draft 0532 22 February16 April04 March 20202021Standards Track Draft Copyright © OASIS Open 20210. All Rights Reserved. Page 8 of 58

Page 9: MQTT for Sensor Networks (MQTT-SN) Version 1.3 · Web viewFeb 22, 2021  · A MQTT-SN GW may or may not be integrated with a MQTT server. In case of a stand-alone GW the MQTT protocol

decapsulates the frames it receives from the gateway and sends them to the clients, unchanged too.

Depending on how a GW performs the protocol translation between MQTT and MQTT-SN, we can differentiate between two types of GWs, namely transparent and aggregating GWs, see Fig. 2. They are explained in the following sections.

1.7.1 Transparent GatewayFor each connected MQTT-SN client a transparent GW will setup and maintain a MQTT connection to the MQTT server. This MQTT connection is reserved exclusively for the end-to-end and almost transparent message exchange between the client and the server. There will be as many MQTT connections between the GW and the server as MQTT-SN clients connected to the GW. The transparent GW will perform a “syntax” translation between the two protocols. Since all message exchanges are end-to-end between the MQTT-SN client and the MQTT server, all functions and features that are implemented by the server can be offered to the client.

Although the implementation of the transparent GW is simpler when compared to the one of an aggregating GW, it requires the MQTT server to support a separate connection for each active client. Some MQTT server implementations might impose a limitation on the number of concurrent connections that they support.

Figure 2: Transparent and Aggregating Gateways

1.7.2 Aggregating GatewayInstead of having a MQTT connection for each connected client, an aggregating GW will have only one MQTT connection to the server. All message exchanges between a MQTT-SN client and an aggregating GW end at the GW. The GW then decides which information will be given further to the server. Although its implementation is more complex than the one of a transparent GW, an aggregating GW may be helpful in case of WSNs with very large number of SAs because it reduces the number of MQTT connections that the server has to support concurrently.

mqtt-sn-v2.0v1.3-wd0532 Working Draft 0532 22 February16 April04 March 20202021Standards Track Draft Copyright © OASIS Open 20210. All Rights Reserved. Page 9 of 58

Page 10: MQTT for Sensor Networks (MQTT-SN) Version 1.3 · Web viewFeb 22, 2021  · A MQTT-SN GW may or may not be integrated with a MQTT server. In case of a stand-alone GW the MQTT protocol

2 Data representation1.1.1 Bits(Byte)Bits in a byte are labelled 7 to 0. Bit number 7 is the most significant bit, the least significant bit is assigned bit number 0.

1.1.2 Two Byte IntegerTwo Byte Integer data values are 16-bit unsigned integers in big-endian order: the high order byte precedes the lower order byte. This means that a 16-bit word is presented on the network as Most Significant Byte (MSB), followed by Least Significant Byte (LSB).

1.1.3 UTF-8 Encoded StringText fields within the MQTT-SN Control Packets described later are encoded as UTF-8 strings. UTF-8 [RFC3629] is an efficient encoding of Unicode [Unicode] characters that optimizes the encoding of ASCII characters in support of text-based communications. Each of these strings is prefixed with a Two Byte Integer length field that gives the number of bytes in a UTF-8 encoded string itself, as illustrated in Figure 1.1 Structure of UTF-8 Encoded Strings below. Consequently, the maximum size of a UTF-8 Encoded String is 65,535 bytes. Unless stated otherwise all UTF-8 encoded strings can have any length in the range 0 to 65,535 bytes. Figure 1-1 Structure of UTF-8 Encoded Strings

Bit 7 6 5 4 3 2 1 0

byte 1 String length MSB

byte 2 String length LSB

byte 3 …. UTF-8 encoded character data, if length > 0.

 The character data in a UTF-8 Encoded String MUST be well-formed UTF-8 as defined by the Unicode specification [Unicode] and restated in RFC 3629 [RFC3629]. In particular, the character data MUST NOT include encodings of code points between U+D800 and U+DFFF [MQTT-1.5.4-1]. If the Client or Server receives an MQTT Control Packet containing ill-formed UTF-8 it is a Malformed Packet. Refer to section 4.13 for information about handling errors. A UTF-8 Encoded String MUST NOT include an encoding of the null character U+0000. [MQTT-1.5.4-2]. If a receiver (Server or Client) receives an MQTT Control Packet containing U+0000 it is a Malformed Packet. Refer to section 4.13 for information about handling errors. 

mqtt-sn-v2.0v1.3-wd0532 Working Draft 0532 22 February16 April04 March 20202021Standards Track Draft Copyright © OASIS Open 20210. All Rights Reserved. Page 10 of 58

Andrew Banks, 01/22/21,
Data representation is part of section 1 in the MQTT Sopecfication.
Page 11: MQTT for Sensor Networks (MQTT-SN) Version 1.3 · Web viewFeb 22, 2021  · A MQTT-SN GW may or may not be integrated with a MQTT server. In case of a stand-alone GW the MQTT protocol

The data SHOULD NOT include encodings of the Unicode [Unicode] code points listed below. If a receiver (Server or Client) receives an MQTT Control Packet containing any of them it MAY treat it as a Malformed Packet. These are the Disallowed Unicode code points. Refer to section 5.4.9 for more information about handling Disallowed Unicode code points. 

         U+0001..U+001F control characters         U+007F..U+009F control characters         Code points defined in the Unicode specification [Unicode] to be non-characters (for example

U+0FFFF) A UTF-8 encoded sequence 0xEF 0xBB 0xBF is always interpreted as U+FEFF ("ZERO WIDTH NO-BREAK SPACE") wherever it appears in a string and MUST NOT be skipped over or stripped off by a packet receiver [MQTT-1.5.4-3]. 

mqtt-sn-v2.0v1.3-wd0532 Working Draft 0532 22 February16 April04 March 20202021Standards Track Draft Copyright © OASIS Open 20210. All Rights Reserved. Page 11 of 58

Page 12: MQTT for Sensor Networks (MQTT-SN) Version 1.3 · Web viewFeb 22, 2021  · A MQTT-SN GW may or may not be integrated with a MQTT server. In case of a stand-alone GW the MQTT protocol

3 MQTT-SN Control Packet format1.2 Structure of an MQTT-SN Control PacketThe MQTT-SN protocol operates by exchanging a series of MQTT-SN Control Packets in a defined way. This section describes the format of these packets.

An MQTT-SN Control Packet consists of up to two parts, always in the following order as shown below.

Figure 2-1 Structure of an MQTT-SN Control Packet

Control Packet Header, present in all MQTT-SN Control Packets

Control Packet Variable Part, present in some MQTT-SN Control Packets

1.2.1 HeaderEach MQTT-SN Control Packet contains a Header of format1 or format2 as shown below.

Figure 2-2 Header format1

Byte Use

1 Length

2 MQTT-SN Control Packet Type

Figure 2-3 Header format2

Byte Use

1 Length MSB 0x01

2 Length MSB

3 Length LSB

4 MQTT-SN Control Packet Type

3.1.1 LengthThe Length field is either 1-byte or 3-byte integer and specifies the total number of bytes contained in the packet (including the Length field itself).

If the first byte of the Length field is coded “0x01” then the Length field is 3-bytes long; in this case, the two following octets specify the total number of octets of the message (most-significant octet first). Otherwise, the Length field is only 1-byte long and specifies itself the total number of bytessbytes contained in the packet.

mqtt-sn-v2.0v1.3-wd0532 Working Draft 0532 22 February16 April04 March 20202021Standards Track Draft Copyright © OASIS Open 20210. All Rights Reserved. Page 12 of 58

Andrew Banks, 03/02/20,
Was “Message Formats”
Page 13: MQTT for Sensor Networks (MQTT-SN) Version 1.3 · Web viewFeb 22, 2021  · A MQTT-SN GW may or may not be integrated with a MQTT server. In case of a stand-alone GW the MQTT protocol

The 3-bytebyte format allows the encoding of packet lengths up to 65535 bytesbyte. Messages with lengths up to and includingsmaller than 2556 bytesbyte MUST may use the shorter Bytebyte format.

Non-normative commentNote that because MQTT-SN does not support message fragmentation and reassembly,

the maximum message length that could be used in a network is governed by the maximum packet size that is supported by that network, and not by the maximum length that could be encoded by MQTT-SN.

3.1.2 MQTT-SN Control Packet TypeThe MQTT-SN Control Packet TypeMsgType field is 1-byte long and specifies the MQTT-SN Control Packet type which is one of the values shown below.

Table 2-1 MQTT Control Packet types

Name Value Direction of flow Description

ADVERTISE 0x00 Gateway broadcast Advertise the gateway presence

SEARCHGW 0x01 Client broadcast Client GWINFO request

GWINFO 0x02 Gateway to Client Response to a SEARCHGW

reserved 0x03 Forbidden

CONNECT 0x04 Client to Gateway Connection request

CONNACK 0x05 Gateway to Client Connection acknowledgement

WILLTOPICREQ 0x06 Gateway to Client Request the will topicname

WILLTOPIC 0x07 Client to Gateway Supply te will topicname

WILLMSGREQ 0x08 Gateway to Client Request the will message

WILLMSG 0x09 Client to Gateway Supply the will message

REGISTER 0x0A Client to Gateway Request topic identifier

REGACK 0x0B Gateway to Client Supply topic identifier

PUBLISH 0x0C Client to Gateway orGateway to Client

Publish message

PUBACK 0x0D Client to Gateway orGateway to Client

Publish acknowledgment (QoS 1)

PUBCOMP 0x0E Client to Gateway orGateway to Client

Publish complete (QoS 2 delivery part 3)

PUBREC 0x0F Client to Gateway orGateway to Client

Publish received (QoS 2 delivery part 1)

PUBREL 0x01 Client to Gateway or Publish release (QoS 2 delivery part 2)

mqtt-sn-v2.0v1.3-wd0532 Working Draft 0532 22 February16 April04 March 20202021Standards Track Draft Copyright © OASIS Open 20210. All Rights Reserved. Page 13 of 58

Page 14: MQTT for Sensor Networks (MQTT-SN) Version 1.3 · Web viewFeb 22, 2021  · A MQTT-SN GW may or may not be integrated with a MQTT server. In case of a stand-alone GW the MQTT protocol

Gateway to Client

reserved 0x11 Forbidden

SUBSCRIBE 0x12 Client to Gateway Subscribe request

SUBACK 0x13 Gateway to Client Subscribe acknowledgment

UNSUBSCRIBE 0x14 Client to Gateway Unsubscribe request

UNSUBACK 0x15 Gateway to Client Unsubscribe acknowledgment

PINGREQ 0x16 Client to Gateway PING request

PINGRESP 0x17 Gateway to Client PING response

DISCONNECT 0x18 Client to Gateway orGateway to Client

Disconnect notification

reserved 0x19 Forbidden

WILLTOPICUPD 0x1A Client to Gateway Modify the will topicname

WILLTOPICRESP 0x1B Gateway to Client Acknowledge the will topicname modification

WILLMSGUPD 0x1C Client to Gateway Modify the will message

WILLMSGRESP 0x1D Gateway to Client Acknowledge the will message modification

reserved 0x1E-0xFD

Forbidden

Encapsulated message

0xFE Forwarder to Client orForwarder to Gateway

Encapsulated MQTT-SN packet

reserved 0xFF Forbidden

Table 3 .

MsgType Field Value

MsgType MsgType Field Value

MsgType

0x00 ADVERTISE 0x01 SEARCHGW

0x02 GWINFO 0x03 reserved

0x04 CONNECT 0x05 CONNACK

0x06 WILLTOPICREQ 0x07 WILLTOPIC

0x08 WILLMSGREQ 0x09 WILLMSG

0x0A REGISTER 0x0B REGACK

0x0C PUBLISH 0x0D PUBACK

0x0E PUBCOMP 0x0F PUBREC

0x10 PUBREL 0x11 reserved

0x12 SUBSCRIBE 0x13 SUBACK

mqtt-sn-v2.0v1.3-wd0532 Working Draft 0532 22 February16 April04 March 20202021Standards Track Draft Copyright © OASIS Open 20210. All Rights Reserved. Page 14 of 58

Page 15: MQTT for Sensor Networks (MQTT-SN) Version 1.3 · Web viewFeb 22, 2021  · A MQTT-SN GW may or may not be integrated with a MQTT server. In case of a stand-alone GW the MQTT protocol

0x14 UNSUBSCRIBE 0x15 UNSUBACK

0x16 PINGREQ 0x17 PINGRESP

0x18 DISCONNECT 0x19 reserved

0x1A WILLTOPICUPD 0x1B WILLTOPICRESP

0x1C WILLMSGUPD 0x1D WILLMSGRESP

0x1E-0xFD reserved 0xFE Encapsulated message

0xFF reserved

Table 3: Values of the MsgType field

3.2 MQTT-SN Control Packet Variable PartThe content of the MQTT-SN Control Packet variable part depends on the type of the Control Packet. The following fields are defined for the message variable part.

[3.2.1] Client Identifier (ClientId)As with MQTT, the ClientId field has a variable length and contains a 1-23 character long string that uniquely identifies the client to the server.

[3.2.2] DataThe Data field corresponds to payload of an MQTT PUBLISH message. It has a variable length and contains the application data that is being published

[3.2.3] DurationThe Duration field is 2-byte long and specifies the duration of a time period in seconds. The maximum value that can be encoded is approximately 18 hours.

[3.2.4] Flags

Bit 7 6 5 4 3 2 1 0

DUP QoS Retain Will Clean Session

Topic Id Type

mqtt-sn-v2.0v1.3-wd0532 Working Draft 0532 22 February16 April04 March 20202021Standards Track Draft Copyright © OASIS Open 20210. All Rights Reserved. Page 15 of 58

Page 16: MQTT for Sensor Networks (MQTT-SN) Version 1.3 · Web viewFeb 22, 2021  · A MQTT-SN GW may or may not be integrated with a MQTT server. In case of a stand-alone GW the MQTT protocol

Table 4: Flags field

DUP

QoS

Retain

Will

CleanSession

TopicIdType

(bit 7)

(6,5)

(4) (3) (2) (1,0)

Table 4: Flags field

The Flags field is 1-byte and contains the following flags (see Table 4):• DUP: same meaning as with MQTT, i.e. set to “0” if message is sent for the first time; set

to “1” if retransmitted (only relevant within PUBLISH messages);

• QoS: meaning as with MQTT for QoS level 0, 1, and 2; set to “0b00” for QoS level 0, “0b01” for QoS level 1, “0b10” for QoS level 2, and “0b11” for new QoS level -1 (only relevant within PUBLISH messages sent by a client);

• Retain: same meaning as with MQTT (only relevant within PUBLISH messages);

• Will: if set, indicates that client is asking for Will topic and Will message prompting (only relevant within CONNECT message);

• Clean Session: same meaning as with MQTT, however extended for Will topic and Will message (only relevant within CONNECT message);

• Topic Id Type: indicates whether the field TopicId or TopicName included in this message contains a normal topic id (set to “0b00”), a pre-defined topic id (set to “0b01”), or a short topic name (set to “0b10”). The value “0b11” is reserved. Refer to sections 3 and 6.7 for the definition of the various types of topic ids.

[3.2.5] GwAddThe GwAdd field has a variable length and contains the address of a GW. Its depends on the network over which MQTT-SN operates and is indicated in the first byte of this field. For example, in a ZigBee network the network address is 2-byte long.

[3.2.6] GwIdThe GwId field is 1-byte long and uniquely identifies a gateway.

[3.2.7] MsgIdThe MsgId field is 2-byte long and corresponds to the MQTT ‘Message ID’ parameter. It allows the sender to match a message with its corresponding acknowledgment.

3.2.1[3.2.8] ProtocolIdThe ProtocolId is 1-byte long. It is only present in a CONNECT message and corresponds to the MQTT ‘protocol name’ and ‘protocol version’.

It is coded 0x021. 0x01 was used for MQTT-SN 1.2. All other values are reserved.

mqtt-sn-v2.0v1.3-wd0532 Working Draft 0532 22 February16 April04 March 20202021Standards Track Draft Copyright © OASIS Open 20210. All Rights Reserved. Page 16 of 58

Page 17: MQTT for Sensor Networks (MQTT-SN) Version 1.3 · Web viewFeb 22, 2021  · A MQTT-SN GW may or may not be integrated with a MQTT server. In case of a stand-alone GW the MQTT protocol

3.2.2[3.2.9] RadiusThe Radius field is 1-byte long and indicates the value of the broadcast radius. The value 0x00 means “broadcast to all nodes in the network”.

3.2.3[3.2.10] ReturnCodeThe value and meaning of the 1-byte long ReturnCode field is shown in Table 5.

Identifier Description Packet

Dec Hex

0 0x00 Accepted CONNACK, SUBACK, UNSUBACK, REGACK, PUBACK, WILLTOPICRESP, WILLMSGRESP

1 0x01 Rejected: congestion CONNACK, SUBACK, UNSUBACK, REGACK, PUBACK, WILLTOPICRESP, WILLMSGRESP

2 0x02 Rejected: invalid topicId SUBACK, UNSUBACK, REGACK, PUBACK, WILLTOPICRESP, WILLMSGRESP

3 0x03 Rejected: not supported CONNACK, SUBACK, UNSUBACK, REGACK, PUBACK, WILLTOPICRESP, WILLMSGRESP

4 - 255 0x40 – 0xFF Reserved

Table 5: Return Code Values

ReturnCode Value

Meaning

0x00 Accepted

0x01 Rejected: congestion

0x02 Rejected: invalid topic ID

0x03 Rejected: not supported

0x04 - 0xFF reservedTable 5: Return Code Values

mqtt-sn-v2.0v1.3-wd0532 Working Draft 0532 22 February16 April04 March 20202021Standards Track Draft Copyright © OASIS Open 20210. All Rights Reserved. Page 17 of 58

Page 18: MQTT for Sensor Networks (MQTT-SN) Version 1.3 · Web viewFeb 22, 2021  · A MQTT-SN GW may or may not be integrated with a MQTT server. In case of a stand-alone GW the MQTT protocol

[3.2.11] TopicIdThe TopicId field is 2-byte long and contains the value of the topic id. The values “0x0000” and “0xFFFF” are reserved and therefore should not be used.

3.2.4[3.2.12] TopicNameThe TopicName field has a variable length and contains an UTF8-encoded string that specifies the topic name.

3.2.5[3.2.13] WillMsgThe WillMsg field has a variable length and contains the Will message.

3.2.6[3.2.14] WillTopicThe WillTopic field has a variable length and contains the Will topic name.

mqtt-sn-v2.0v1.3-wd0532 Working Draft 0532 22 February16 April04 March 20202021Standards Track Draft Copyright © OASIS Open 20210. All Rights Reserved. Page 18 of 58

Page 19: MQTT for Sensor Networks (MQTT-SN) Version 1.3 · Web viewFeb 22, 2021  · A MQTT-SN GW may or may not be integrated with a MQTT server. In case of a stand-alone GW the MQTT protocol

4 MQTT-SN Control Packets

4.1 Format of Individual MessagesThis section specifies the format of the individual MQTT-SN messages. All messages are described with the 1-byte Length field. The message formats in case of the 3-byte Length field could be derived straightforwardly and are therefore not mentioned.

4.1.1 ADVERTISE

Bit 7 6 5 4 3 2 1 0

Byte 1 Length

Byte 2 Packet Type

Byte 3 GwId

Byte 4 Duration MSB

Byte 5 Duration LSB

Length

MsgType

GwId

Duration

(byte 0)

(1) (2) (3,4)

Table 6: ADVERTISE PacketMessage

The ADVERTISE packetmessage is broadcasted periodically by thea gateway to advertise its presence. The time interval until the next broadcast time is indicated byin the Duration field of this message. Its format is illustrated in Table 6:

4.1.1.1 Length & Packet TypeThe first 2 or 4 bytes of the packet are encoded according to the variable length packet header format. Please refer to figure 1.2.1 for a detailed description.Length and MsgType: see Section 5.2.

GwId: the id of the gateway which sends this message.

[4.1.1.2] GwIdThe GwId field is 1-byte long and uniquely identifies a gateway which is advertising itself in the networkDuration: time interval until the next ADVERTISE is broadcasted by this gateway.

mqtt-sn-v2.0v1.3-wd0532 Working Draft 0532 22 February16 April04 March 20202021Standards Track Draft Copyright © OASIS Open 20210. All Rights Reserved. Page 19 of 58

Page 20: MQTT for Sensor Networks (MQTT-SN) Version 1.3 · Web viewFeb 22, 2021  · A MQTT-SN GW may or may not be integrated with a MQTT server. In case of a stand-alone GW the MQTT protocol

4.1.1.2 DurationThe Duration field is a 2-byte long. It and specifies the time interval in seconds until the next ADVERTISE packet is broadcasted by this gateway duration of a time period. The maximum value that can be encoded is approximately 18 hours

Non-normative comment The maximum value that can be encoded is approximately 18 hours.

[4.1.1.3] GwIdThe GwId field is 1-byte long and uniquely identifies a gateway which is advertising itself in the network.

4.1.2 SEARCHGW

Bit 7 6 5 4 3 2 1 0

Byte 1 Length

Byte 2 Packet Type

Byte 3 Radius

Length

MsgType

Radius

(byte 0)

(1) (2)

Table 7: SEARCHGW Message

The SEARCHGW message is broadcasted by a client when it searches for a GW. The broadcast radius of the SEARCHGW is limited and depends on the density of the clients deployment, e.g. only 1-hop broadcast in case of a very dense network in which every MQTT-SN client is reachable from each other within 1-hop transmission. The format of a SEARCHGW message is illustrated in Table 7:

4.1.2.1 Length & Packet TypeThe first 2 or 4 bytes of the packet are encoded according to the variable length packet header format. Please refer to figure 1.2.1 for a detailed description.

• Length and MsgType: see Section 5.2.

[4.1.2.2] Radius• Radius: the broadcast radius of this message.

The broadcast radius is also indicated to the underlying network layer when MQTT-SN gives this message for transmission.

4.1.3 GWINFO

mqtt-sn-v2.0v1.3-wd0532 Working Draft 0532 22 February16 April04 March 20202021Standards Track Draft Copyright © OASIS Open 20210. All Rights Reserved. Page 20 of 58

Page 21: MQTT for Sensor Networks (MQTT-SN) Version 1.3 · Web viewFeb 22, 2021  · A MQTT-SN GW may or may not be integrated with a MQTT server. In case of a stand-alone GW the MQTT protocol

Bit 7 6 5 4 3 2 1 0

Byte 1 Length

Byte 2 Packet Type

Byte 3 GwId

Byte 4 … N GwAddress (optional)

Length MsgType GwId GwAdd*

(byte 0)

(1) (2) (3:n)

(*) only present if message is sent by a client

Table 8: GWINFO Message

The GWINFO message is sent as response to a SEARCHGW message using the broadcast service of the underlying layer, with the radius as indicated in the SEARCHGW message. If sent by a GW, it contains only the id of the sending GW; otherwise, if sent by a client, it also includes the address of the GW, see Table 8:

4.1.3.1 Length & Packet TypeThe first 2 or 4 bytes of the packet are encoded according to the variable length packet header format. Please refer to figure 1.2.1 for a detailed description.

• Length and MsgType: see Section 5.2.

[4.1.3.2] GwId: the id of a GW.GwId• GwAdd: address of the indicated GW; optional, only included if message is sent by a client.

The GwId field is 1-byte long and uniquely identifies a gateway in the network.Like the SEARCHGW message the broadcast radius for this message is also indicated to the underlying network layer when MQTT-SN gives this message for transmission.

[4.1.3.3] GwAddThe GwAdd field has a variable length and contains the address of a Gateway. Its length depends on the type of network over which MQTT-SN operates and is specified by the Length byteindicated in the first byte of this field. Optional, only included if message is sent by a client.

Non-normative comment

4.1.3.2 For example, in a ZigBee network the network address is 2-byte long.

[4.1.3.4] GwIdThe GwId field is 1-byte long and uniquely identifies a gateway in the network.

mqtt-sn-v2.0v1.3-wd0532 Working Draft 0532 22 February16 April04 March 20202021Standards Track Draft Copyright © OASIS Open 20210. All Rights Reserved. Page 21 of 58

Andrew Banks, 01/22/21,
We need to supply an authorative list of network address lengths.
Andrew Banks, 01/22/21,
Surely this is only sent by a gateway, since it is responding to SEARCGHGW sent by the client?
Page 22: MQTT for Sensor Networks (MQTT-SN) Version 1.3 · Web viewFeb 22, 2021  · A MQTT-SN GW may or may not be integrated with a MQTT server. In case of a stand-alone GW the MQTT protocol

4.1.4 CONNECTAfter a Network Connection is established by a Client to a Server, the first packet sent from the Client to the Server MUST be a CONNECT packet [MQTT-3.1.0-1].

A Client can only send the CONNECT packet once over a Network Connection. The Server MUST process a second CONNECT packet sent from a Client as a Protocol Error and close the Network Connection [MQTT-3.1.0-2]. Refer to section 4.13 for information about handling errors.

Length

MsgType Flags ProtocolId Duration ClientId

(byte 0)

(1) (2) (3) (4,5) (6:n)

Table 9: CONNECT Packet

The CONNECT packet is sent from theby a Cclient to the server to setup a connection. Its format is shown in Table 9:

Figure 3-1 - CONNECT packet

Bit 7 6 5 4 3 2 1 0

Byte 1 Length

Byte 2 Packet Type

Reserved Will Flag Clean Start

Reserved

Byte 3 0 0 0 0 0 X X 0

Byte 4 Protocol Version

Byte 5 Keep Alive MSB

Byte 6 Keep Alive LSB

Byte 7 Session Expiry Interval MSB

Byte 8 Session Expiry Interval

Byte 9 Session Expiry Interval

Byte 10 Session Expiry Interval LSB

Byte 11 . N Client Identifier

After a Network Connection is established by a Client to a Server, the first packet sent from the Client to the Server MUST be a CONNECT packet [MQTT-3.1.0-1].

A Client can only send the CONNECT packet once over a Network Connection. The Server MUST process a second CONNECT packet sent from a Client as a Protocol Error and close the Network Connection [MQTT-3.1.0-2]. Refer to section 4.13 for information about handling errors. The CONNECT packet is sent from the Client to the server to setup a connection.

mqtt-sn-v2.0v1.3-wd0532 Working Draft 0532 22 February16 April04 March 20202021Standards Track Draft Copyright © OASIS Open 20210. All Rights Reserved. Page 22 of 58

Page 23: MQTT for Sensor Networks (MQTT-SN) Version 1.3 · Web viewFeb 22, 2021  · A MQTT-SN GW may or may not be integrated with a MQTT server. In case of a stand-alone GW the MQTT protocol

4.1.4.1 Length & Packet TypeThe first 2 or 4 bytes of the packet are encoded according to the variable length packet header format. Please refer to figure 1.2.1 for a detailed description.

Byte

0 Length

1 Packet Type 0x04

2 Connect Flags

3 Protocol version 0x024

4,5 Keep Alive

6..N6..9 ClientIdentifierSessionExpiryInterval

10..N ClientIdentifier

[4.1.4.1] Connect FlagsThe Connect Flags byte contains several parameters specifying the behavior of the MQTT-SN connection. It also indicates the presence or absence of fields in the Payload.

Figure 3-4 - Connect Flag bits

Bit 7 6 5 4 3 2 1 0

Reserved Reserved Reserved Reserved Will Flag Clean Start

Reserved

0 0 0 0 0 X X 0

The Server MUST validate that the reserved flags in the CONNECT packet are set to 0 [MQTT-3.1.2-3]. If any of the reserved flags is not 0 it is a Malformed Packet. Refer to section 4.13 for information about handling errors.

4.1.4.2 Protocol VersionThe one byte unsigned value that represents the revision level of the protocol used by the Client. The value of the Protocol Version field for MQTT-SN version 2.01.3 is 24 (0x024).

A Server which supports multiple versions of the MQTT-SN protocol uses the Protocol Version to determine which version of MQTT-SN the Client is using. If the Protocol Version is not 24 and the Server does not want to accept the CONNECT packet, the Server MAY send a CONNACK packet with Reason Code 0x84 (Unsupported Protocol Version) and then MUST close the Network Connection [MQTT-3.1.2-2].

4.1.4.3 Keep Alive TimerThe Keep Alive is a Two Byte Integer which is a time interval measured in seconds. It is the maximum time interval that is permitted to elapse between the point at which the Client finishes transmitting one

mqtt-sn-v2.0v1.3-wd0532 Working Draft 0532 22 February16 April04 March 20202021Standards Track Draft Copyright © OASIS Open 20210. All Rights Reserved. Page 23 of 58

Simon Johnson, 03/01/21,
Do we need an explicit flags table if we’re embedding the flags into the packet table above?
Page 24: MQTT for Sensor Networks (MQTT-SN) Version 1.3 · Web viewFeb 22, 2021  · A MQTT-SN GW may or may not be integrated with a MQTT server. In case of a stand-alone GW the MQTT protocol

MQTT-SN Control Packet and the point it starts sending the next. It is the responsibility of the Client to ensure that the interval between MQTT Control Packets being sent does not exceed the Keep Alive value. If Keep Alive is non-zero and in the absence of sending any other MQTT-SN Control Packets, the Client MUST send a PINGREQ packet [MQTT-3.1.2-20].

If the Server returns a Server Keep Alive on the CONNACK packet, the Client MUST use that value instead of the value it sent as the Keep Alive [MQTT-3.1.2-21].

The Client can send PINGREQ at any time, irrespective of the Keep Alive value, and check for a corresponding PINGRESP to determine that the network and the Server are available.

If the Keep Alive value is non-zero and the Server does not receive an MQTT-SN Control Packet from the Client within one and a half times the Keep Alive time period, it MUST close the Network Connection to the Client as if the network had failed [MQTT-3.1.2-22].

If a Client does not receive a PINGRESP packet within a reasonable amount of time after it has sent a PINGREQ, it SHOULD close the Network Connection to the Server.

A Keep Alive value of 0 has the effect of turning off the Keep Alive mechanism. If Keep Alive is 0 the Client is not obliged to send MQTT-SN Control Packets on any particular schedule. 

Non-normative commentThe Server may have other reasons to disconnect the Client, for instance because it is shutting down. Setting Keep Alive does not guarantee that the Client will remain connected.

 Non-normative commentThe actual value of the Keep Alive is application specific; typically, this is a few minutes. The maximum value of 65,535 is 18 hours 12 minutes and 15 seconds.

• Length and MsgType: see Section 5.2.

• Flags:

– DUP, QoS, Retain, TopicIdType: not used.

– Will: if set, indicates that client is requesting for Will topic and Will message prompting;

– CleanSession: same meaning as with MQTT, however extended for Will topic and Will message (see Section 6.3).

• ProtocolId: corresponds to the “Protocol Name” and “Protocol Version” of the MQTT CONNECT message.

• Duration: same as with MQTT, contains the value of the Keep Alive timer.

[4.1.4.2] Session Expiry Interval:If the Session Expiry Interval is absent the value 0 is used. If it is set to 0, or is absent, the Session ends when the Network Connection is closed.

mqtt-sn-v2.0v1.3-wd0532 Working Draft 0532 22 February16 April04 March 20202021Standards Track Draft Copyright © OASIS Open 20210. All Rights Reserved. Page 24 of 58

Andrew Banks, 16/04/20,
Should we remove this? MQTT-SN does not have a mechanism for the server t override the Keep Alive time.
Page 25: MQTT for Sensor Networks (MQTT-SN) Version 1.3 · Web viewFeb 22, 2021  · A MQTT-SN GW may or may not be integrated with a MQTT server. In case of a stand-alone GW the MQTT protocol

If the Session Expiry Interval is 0xFFFFFFFF (UINT_MAX), the Session does not expire. The Client and Server MUST store the Session State after the Network Connection is closed if the Session Expiry Interval is greater than 0. Non-normative commentThe clock in the Client or Server may not be running for part of the time interval, for instance because the Client or Server are not running. This might cause the deletion of the state to be delayed.

When the Session expires the Client and Server need not process the deletion of state atomically.

4.1.4.4 Client Identifier:

The Client Identifier (ClientID) identifies the Client to the Server. Each Client connecting to the Server has a unique ClientID. The ClientID MUST be used by Clients and by Servers to identify state that they hold relating to this MQTT-SN Session between the Client and the Server [MQTT-3.1.3-2]. Refer to section xxx4.1 for more information about Session State.

The ClientID MUST be a UTF-8 Encoded String as defined in section 1.5.4 [MQTT-3.1.3-4]. between 1 and 23 UTF-8 encoded bytes in length.

4.1.5 Client Identifier (ClientId)As with MQTT, the ClientId field has a variable length and contains a 1-23 character long string that uniquely identifies the client to the server.

[4.1.4.1] DurationThe Duration field is a 2-byte long. It specifies the time Kep Alive interval in seconds.

4.1.6[4.1.5] CONNACKBit 7 6 5 4 3 2 1 0

Byte 1 Length

Byte 2 Packet Type

Byte 3 Return Code

Byte 4 … N Client Identifier (optional)

Table 10: CONNACK Message

The CONNACK message is sent by the server in response to a connection request from a client. Its format is shown in Table 10:

mqtt-sn-v2.0v1.3-wd0532 Working Draft 0532 22 February16 April04 March 20202021Standards Track Draft Copyright © OASIS Open 20210. All Rights Reserved. Page 25 of 58

Andrew Banks, 04/16/20,
We need to consider what constraints are place in ClientId’s in MQTT-SN, and to what extent valid OUF-8 must be used.
Andrew Banks, 01/22/21,
Describe the session state.
Page 26: MQTT for Sensor Networks (MQTT-SN) Version 1.3 · Web viewFeb 22, 2021  · A MQTT-SN GW may or may not be integrated with a MQTT server. In case of a stand-alone GW the MQTT protocol

4.1.6.1[4.1.5.1] Length & Packet TypeThe first 2 or 4 bytes of the packet are encoded according to the variable length packet header format. Please refer to figure 1.2.1 for a detailed description.

• Length and MsgType: see Section 5.2.

[4.1.5.2] ReturnCode: encoded according to Table 5.Return Code“Accepted” or rejected reason.

4.1.6.2 Client Identifier

ClientIdentifier: The Client Identifier assigned by the gateway when the associated CONNECT message contained no Client Identifier.

For more details of the connection procedure, refer to section 6.2.

4.1.7[4.1.6] WILLTOPICREQ and WILLMSGREQBit 7 6 5 4 3 2 1 0

Byte 1 Length

Byte 2 Packet Type

Length

MsgType

(byte 0)

(1)

Table 11: WILLTOPICREQ and WILLMSGREQ Messages

The WILLTOPICREQ message is sent by the GW to request a client for sending the Will topic name. Its format is shown in Table 11: it has only a header and no variable part.

4.1.8[4.1.7] WILLTOPICBit 7 6 5 4 3 2 1 0

Byte 1 Length

Byte 2 Packet Type

DUP(

Reserved)

QoS Retain Will(

Reserved)

Clean(

Reserved)

Topic Id Type(Reserved)

Byte 3 0 X X X 0 0 0 0

Byte 4.. N Will Topic

Table 12: WILLTOPIC Message

mqtt-sn-v2.0v1.3-wd0532 Working Draft 0532 22 February16 April04 March 20202021Standards Track Draft Copyright © OASIS Open 20210. All Rights Reserved. Page 26 of 58

Page 27: MQTT for Sensor Networks (MQTT-SN) Version 1.3 · Web viewFeb 22, 2021  · A MQTT-SN GW may or may not be integrated with a MQTT server. In case of a stand-alone GW the MQTT protocol

The WILLTOPIC message is sent by a client as response to the WILLTOPICREQ message for transferring its Will topic name to the GW. Its format is shown in Table 12:

4.1.8.1[4.1.7.1] Length & Packet TypeThe first 2 or 4 bytes of the packet are encoded according to the variable length packet header format. Please refer to figure 1.2.1 for a detailed description.

• Length and MsgType: see Section 5.2.

• Flags:

– DUP: not used.

– QoS: same as MQTT, contains the Will QoS

– Retain: same as MQTT, contains the Will Retain flag

– Will: not used – CleanSession: not used – TopicIdType: not used.

[4.1.7.2] WillTopic: contains the Will topic name.Flags

Bit 7 6 5 4 3 2 1 0

DUP(Reserved)

Will QoS Retain Will(Reserved)

Clean(Reserved)

Topic Id Type(Reserved)

0 X X X 0 0 0 0

– Will QoS: same as MQTT, contains the Will QoS

– Retain: same as MQTT, contains the Will Retain flag

4.1.8.2 Will TopicContains the Will topic name.

An empty WILLTOPIC message is a WILLTOPIC message without Flags and WillTopic field (i.e. it is exactly 2 bytes long). It is used by a client to delete the Will topic and the Will message stored in the server, see Section 6.4.

4.1.9[4.1.8] WILLMSGREQThe WILLMSGREQ message is sent by the GW to request a client for sending the Will message. Its format is shown in Table 11: it has only a header and no variable part.

4.1.10[4.1.9] WILLMSGBit 7 6 5 4 3 2 1 0

Byte 1 Length

Byte 2 Packet Type

mqtt-sn-v2.0v1.3-wd0532 Working Draft 0532 22 February16 April04 March 20202021Standards Track Draft Copyright © OASIS Open 20210. All Rights Reserved. Page 27 of 58

Page 28: MQTT for Sensor Networks (MQTT-SN) Version 1.3 · Web viewFeb 22, 2021  · A MQTT-SN GW may or may not be integrated with a MQTT server. In case of a stand-alone GW the MQTT protocol

Byte 3 .. N Will Msg

Table 13: WILLMSG Message

The WILLMSG message is sent by a client as response to a WILLMSGREQ for transferring its Will message to the GW. Its format is shown in Table 13:

4.1.10.1[4.1.9.1] Length & Packet TypeThe first 2 or 4 bytes of the packet are encoded according to the variable length packet header format. Please refer to figure 1.2.1 for a detailed description.

4.1.10.2 Length and MsgType: see Section 5.2.Will Msg• Contains the will message

WillMsg: contains the Will message.

Length

MsgType TopicId

MsgId TopicName

(byte 0)

(1) (2,3) (4:5) (6:n)

Table 14: REGISTER Message

[4.1.10] REGISTER

Bit 7 6 5 4 3 2 1 0

Byte 1 Length

Byte 2 Packet Type

Byte 3 Topic Id MSB

Byte 4 Topic Id LSB

Byte 5 Msg Id MSB

Byte 6 Msg Id LSB

Byte 7 … N Topic Name

Table 14: REGISTER Message

The REGISTER message is sent by a client to a GW for requesting a topic id value for the included topic name. It is also sent by a GW to inform a client about the topic id value it has assigned to the included topic name. Its format is illustrated in Table 14:

4.1.10.3[4.1.10.1] Length & Packet TypeThe first 2 or 4 bytes of the packet are encoded according to the variable length packet header format. Please refer to figure 1.2.1 for a detailed description.

mqtt-sn-v2.0v1.3-wd0532 Working Draft 0532 22 February16 April04 March 20202021Standards Track Draft Copyright © OASIS Open 20210. All Rights Reserved. Page 28 of 58

Page 29: MQTT for Sensor Networks (MQTT-SN) Version 1.3 · Web viewFeb 22, 2021  · A MQTT-SN GW may or may not be integrated with a MQTT server. In case of a stand-alone GW the MQTT protocol

4.1.10.4 Length and MsgType: see Section 5.2.Topic Id

TopicId: ifIf sent by a client, it is coded 0x0000 and is not relevant; if sent by a GW, it contains the topic id value assigned to the topic name included in the Topic Name field;

4.1.10.5 Msg Id

MsgId: sShould be coded such that it can be used to identify the corresponding REGACK message.

4.1.10.6[4.1.10.2] Topic NameCTopicName: contains the fully qualified topic name.

4.1.11 REGACKBit 7 6 5 4 3 2 1 0

Byte 1 Length

Byte 2 Packet Type

Byte 3 Topic Id MSB

Byte 4 Topic Id LSB

Byte 5 Msg Id MSB

Byte 6 Msg Id LSB

Byte 7 Return Code

Table 15: REGACK Message

The REGACK message is sent by a client or by a GW as an acknowledgment to the receipt and processing of a REGISTER message. Its format is illustrated in Table 15:

4.1.11.1 Length & Packet TypeThe first 2 or 4 bytes of the packet are encoded according to the variable length packet header format. Please refer to figure 1.2.1 for a detailed description.

4.1.11.2 Length and MsgType: see Section 5.2.Topic IdThe value that shall be used as the topic Id in the PUBLISH packets.

4.1.11.3 Msg IdThe same value as the one contained in the corresponding REGISTER message.

4.1.11.4 Return Code“Accepted” or rejected reason.

mqtt-sn-v2.0v1.3-wd0532 Working Draft 0532 22 February16 April04 March 20202021Standards Track Draft Copyright © OASIS Open 20210. All Rights Reserved. Page 29 of 58

Page 30: MQTT for Sensor Networks (MQTT-SN) Version 1.3 · Web viewFeb 22, 2021  · A MQTT-SN GW may or may not be integrated with a MQTT server. In case of a stand-alone GW the MQTT protocol

• TopicId: the value that shall be used as topic id in the PUBLISH messages; • MsgId:

same value as the one contained in the corresponding REGISTER message.

• ReturnCode: “accepted”, or rejection reason.

[4.1.12] PUBLISHBit 7 6 5 4 3 2 1 0

Byte 1 Length

Byte 2 Packet Type

DUP QoS Retain Will(

Reserved)

Clean(

Reserved)

Topic Id Type

Byte 3 X X X X 0 0 X X

Byte 4 Topic Id MSB

Byte 5 Topic Id LSB

Byte 6 Msg Id MSB

Byte 7 Msg Id LSB

Byte 8 ..N Data

Table 16: PUBLISH Message

This message is used by both clients and gateways to publish data for a certain topic. Its format is illustrated in Table 16:

4.1.11.5[4.1.12.1] Length & Packet TypeThe first 2 or 4 bytes of the packet are encoded according to the variable length packet header format. Please refer to figure 1.2.1 for a detailed description.

4.1.11.6 Length and MsgType: see Section 5.2.Flags

Bit 7 6 5 4 3 2 1 0

DUP QoS Retain Will(Reserved)

Clean(Reserved)

Topic Id Type

X X X X 0 0 X X

• Flags:

– DUP: same as MQTT, indicates whether message is sent for the first time or not.

mqtt-sn-v2.0v1.3-wd0532 Working Draft 0532 22 February16 April04 March 20202021Standards Track Draft Copyright © OASIS Open 20210. All Rights Reserved. Page 30 of 58

Page 31: MQTT for Sensor Networks (MQTT-SN) Version 1.3 · Web viewFeb 22, 2021  · A MQTT-SN GW may or may not be integrated with a MQTT server. In case of a stand-alone GW the MQTT protocol

– QoS: same as MQTT, contains the QoS level for this PUBLISH message.

– Retain: same as MQTT, contains the Retain flag.

– Will: not used

– CleanSession: not used

– TopicIdType: indicates the type of the topic id contained in the TopicId field.

4.1.11.7 Topic Id

TopicId: cContains the topic id value or the short topic name for which the data is published.

4.1.11.8 Msg Id

MsgId: sSame meaning as the MQTT “Message ID”; only relevant in case of QoS levels 1 and 2, otherwise coded 0x0000.

4.1.11.9[4.1.12.2] DataData: the published data.

[4.1.13] DataThe Data field corresponds to payload of an MQTT PUBLISH message. It has a variable length and contains the application data that is being published

[4.1.14] PUBACKBit 7 6 5 4 3 2 1 0

Byte 1 Length

Byte 2 Packet Type

Byte 3 Msg Id MSB

Byte 4 Msg Id LSB

Byte 5 Return Code

Table 17: PUBACK message

The PUBACK message is sent by a gateway or a client as an acknowledgment to the receipt and processing of a PUBLISH message in case of QoS levels 1 or 2. It can also be sent as response to a PUBLISH message in case of an error; the error reason is then indicated in the ReturnCode field. Its format is illustrated in Table 17:

4.1.11.10[4.1.14.1] Length & Packet TypeThe first 2 or 4 bytes of the packet are encoded according to the variable length packet header format. Please refer to figure 1.2.1 for a detailed description.

mqtt-sn-v2.0v1.3-wd0532 Working Draft 0532 22 February16 April04 March 20202021Standards Track Draft Copyright © OASIS Open 20210. All Rights Reserved. Page 31 of 58

Page 32: MQTT for Sensor Networks (MQTT-SN) Version 1.3 · Web viewFeb 22, 2021  · A MQTT-SN GW may or may not be integrated with a MQTT server. In case of a stand-alone GW the MQTT protocol

4.1.11.11 Length and MsgType: see Section 5.2.Msg Id

TopicId: same value the one contained in the corresponding PUBLISH message.

MsgId: sSame value as the one contained in the corresponding PUBLISH message.

4.1.11.12[4.1.14.2] Return Code“Accepted” or rejected reason.

• ReturnCode: “accepted”, or rejection reason.

[4.1.15] PUBREC, PUBREL, and PUBCOMP

Bit 7 6 5 4 3 2 1 0

Byte 1 Length

Byte 2 Packet Type

Byte 3 Msg Id MSB

Byte 4 Msg Id LSB

Table 18: PUBREC, PUBREL, and PUBCOMP Messages

As with MQTT, the PUBREC, PUBREL, and PUBCOMP messages are used in conjunction with a PUBLISH message with QoS level 2. Their format is illustrated in Table 18:

4.1.11.13[4.1.15.1] Length & Packet TypeThe first 2 or 4 bytes of the packet are encoded according to the variable length packet header format. Please refer to figure 1.2.1 for a detailed description.

• Length and MsgType: see Section 5.2.

[4.1.15.2] Msg IdSame value as the one contained in the corresponding PUBLISH message.

Length MsgType

Flags MsgId TopicName or TopicId

(byte 0)

(1) (2) (3-4) (5:n) or (5-6)

Table 19: SUBSCRIBE and UNSUBSCRIBE Messages

[4.1.16] SUBSCRIBE

Bit 7 6 5 4 3 2 1 0

mqtt-sn-v2.0v1.3-wd0532 Working Draft 0532 22 February16 April04 March 20202021Standards Track Draft Copyright © OASIS Open 20210. All Rights Reserved. Page 32 of 58

Page 33: MQTT for Sensor Networks (MQTT-SN) Version 1.3 · Web viewFeb 22, 2021  · A MQTT-SN GW may or may not be integrated with a MQTT server. In case of a stand-alone GW the MQTT protocol

Byte 1 Length

Byte 2 Packet Type

DUP QoS Retain(

Reserved)

Will(

Reserved)

Clean(

Reserved)

Topic Id Type

Byte 3 X X X 0 0 0 X X

Byte 4 Msg Id MSB

Byte 5 Msg Id LSB

Byte 6 Topic Id MSB Topic NameByte 6 … NByte 7 Topic Id LSB

Table 19: SUBSCRIBE Message

The SUBSCRIBE message is used by a client to subscribe to a certain topic name. Its format is illustrated in Table 19:

4.1.11.14[4.1.16.1] Length & Packet TypeThe first 2 or 4 bytes of the packet are encoded according to the variable length packet header format. Please refer to figure 1.2.1 for a detailed description.

4.1.11.15 Length and MsgType: see Section 5.2.Flags

Bit 7 6 5 4 3 2 1 0

DUP QoS Retain(Reserved)

Will(Reserved)

Clean(Reserved)

Topic Id Type

X X X 0 0 0 X X

• Flags:

– DUP: same as MQTT, indicates whether message is sent for first time or not.

– QoS: same as MQTT, contains the requested QoS level for this topic.

– Retain: not used

– Will: not used

– CleanSession: not used

– TopicIdType: indicates the type of information included at the end of the message, namely “0b00” topic name, “0b01” pre-defined topic id, “0b10” short topic name, and “0b11” reserved.

4.1.11.16 Msg Id

mqtt-sn-v2.0v1.3-wd0532 Working Draft 0532 22 February16 April04 March 20202021Standards Track Draft Copyright © OASIS Open 20210. All Rights Reserved. Page 33 of 58

Page 34: MQTT for Sensor Networks (MQTT-SN) Version 1.3 · Web viewFeb 22, 2021  · A MQTT-SN GW may or may not be integrated with a MQTT server. In case of a stand-alone GW the MQTT protocol

MsgId: sShould be coded such that it can be used to identify the corresponding SUBACK message.

4.1.11.17 Topic Id or Topic Name

TopicName or TopicId: cContains topic name, topic id, or short topic name as indicated in the TopicIdType field in flags.

4.1.12[4.1.17] SUBACKBit 7 6 5 4 3 2 1 0

Byte 1 Length

Byte 2 Packet Type

DUP QoS Retain(

Reserved)

Will(

Reserved)

Clean(

Reserved)

Topic Id Type

Byte 3 0 X X 0 0 0 X X

Byte 4 Topic Id MSB

Byte 5 Topic Id LSB

Byte 6 Msg Id MSB

Byte 7 Msg Id LSB

Byte 8 Return Code

Table 20:

SUBACK

Message

Length

MsgType

Flags

TopicId

MsgId

ReturnCode

(byte 0) (1) (2) (3,4) (5,6) (7)Table 20: SUBACK Message

Table 20: SUBACK Message

The SUBACK message is sent by a gateway to a client as an acknowledgment to the receipt and processing of a SUBSCRIBE message. Its format is illustrated in Table 20:

4.1.12.1[4.1.17.1] Length & Packet TypeThe first 2 or 4 bytes of the packet are encoded according to the variable length packet header format. Please refer to figure 1.2.1 for a detailed description.

mqtt-sn-v2.0v1.3-wd0532 Working Draft 0532 22 February16 April04 March 20202021Standards Track Draft Copyright © OASIS Open 20210. All Rights Reserved. Page 34 of 58

Page 35: MQTT for Sensor Networks (MQTT-SN) Version 1.3 · Web viewFeb 22, 2021  · A MQTT-SN GW may or may not be integrated with a MQTT server. In case of a stand-alone GW the MQTT protocol

4.1.12.2 Length and MsgType: see Section 5.2.Flags

Bit 7 6 5 4 3 2 1 0

DUP(Reserved)

QoS Retain(

Reserved)

Will(Reserved)

Clean(Reserved)

Topic Id Type

0 X X 0 0 0 X X

• Flags:

– DUP: not used.

– QoS: same as MQTT, contains the granted QoS level.

4.1.12.3 Topic IdIn

Retain: not used.

Will: not used

CleanSession: not used

TopicIdType: not used

TopicId: in case of “accepted” the value that will be used as topic id by the gateway when sending PUBLISH messages to the client (not relevant in case of subscriptions to a short topic name or to a topic name which contains wildcard characters)

4.1.12.4 Msg Id

MsgId: sSame value as the one contained in the corresponding SUBSCRIBE message.

4.1.12.5 Return Code

ReturnCode: A“accepted”, or rejection reason.

4.1.13[4.1.18] UNSUBSCRIBE

Bit 7 6 5 4 3 2 1 0

Byte 1 Length

Byte 2 Packet Type

DUP(

Reserved)

QoS(Reserved)

Retain(

Reserved)

Will(

Reserved)

Clean(

Reserved)

Topic Id Type

Byte 3 0 0 0 0 0 0 X X

mqtt-sn-v2.0v1.3-wd0532 Working Draft 0532 22 February16 April04 March 20202021Standards Track Draft Copyright © OASIS Open 20210. All Rights Reserved. Page 35 of 58

Page 36: MQTT for Sensor Networks (MQTT-SN) Version 1.3 · Web viewFeb 22, 2021  · A MQTT-SN GW may or may not be integrated with a MQTT server. In case of a stand-alone GW the MQTT protocol

Byte 4 Msg Id MSB

Byte 5 Msg Id LSB

Byte 6 Topic Id MSB Topic NameByte 6 … NByte 7 Topic Id LSB

Table 21: UNSUBSCRIBE Message

An UNSUBSCRIBE message is sent by the client to the GW to unsubscribe from named topics. Its format is illustrated in Table 2119:

4.1.13.1[4.1.18.1] Length & Packet TypeThe first 2 or 4 bytes of the packet are encoded according to the variable length packet header format. Please refer to figure 1.2.1 for a detailed description.

4.1.13.2 Length and MsgType: see Section 5.2.Flags

Bit 7 6 5 4 3 2 1 0

DUP(Reserved)

QoS(Reserved)

Retain(Reserved)

Will(Reserved)

Clean(Reserved)

Topic Id Type

0 0 0 0 0 0 X X

• Flags:

– DUP: not used.– QoS: not used.– Retain: not used.– Will: not used

– CleanSession: not used– TopicIdType: indicates the type of information included at the end of the message,

namely “0b00” topic name, “0b01” pre-defined topic id, “0b10” short topic name, and “0b11” reserved.

4.1.13.3 Msg Id

MsgId: sShould be coded such that it can be used to identify the corresponding SUBACK message.

4.1.13.4 Topic Id or Topic Name

TopicName or TopicId: cContains topic name, pre-defined topic id, or short topic name as indicated in the TopicIdType field.

4.1.14[4.1.19] UNSUBACK

mqtt-sn-v2.0v1.3-wd0532 Working Draft 0532 22 February16 April04 March 20202021Standards Track Draft Copyright © OASIS Open 20210. All Rights Reserved. Page 36 of 58

Page 37: MQTT for Sensor Networks (MQTT-SN) Version 1.3 · Web viewFeb 22, 2021  · A MQTT-SN GW may or may not be integrated with a MQTT server. In case of a stand-alone GW the MQTT protocol

Bit 7 6 5 4 3 2 1 0

Byte 1 Length

Byte 2 Packet Type

Byte 3 Msg Id MSB

Byte 4 Msg Id LSB

Byte 5 Return Code

Length MsgType MsgId ReturnCode(4)

(byte 0) (1) (2-3)

Table 221: UNSUBACK Message

An UNSUBACK message is sent by a GW to acknowledge the receipt and processing of an UNSUBSCRIBE message. Its format is illustrated in Table 21:

4.1.14.1[4.1.19.1] Length & Packet TypeThe first 2 or 4 bytes of the packet are encoded according to the variable length packet header format. Please refer to figure 1.2.1 for a detailed description.

4.1.14.2 Length and MsgType: see Section 5.2.Msg Id

MsgId: sSame value as the one contained in the corresponding UNSUBSCRIBE message.

4.1.14.3 Return Code

ReturnCode: "aAccepted," or rejection reason. See section 5.3.10.

4.1.15[4.1.20] PINGREQBit 7 6 5 4 3 2 1 0

Byte 1 Length

Byte 2 Packet Type

Byte 3 … N Client Identifier (optional)

Length

MsgType ClientId (optional)

(byte 0)

(1) (2:n)

mqtt-sn-v2.0v1.3-wd0532 Working Draft 0532 22 February16 April04 March 20202021Standards Track Draft Copyright © OASIS Open 20210. All Rights Reserved. Page 37 of 58

Page 38: MQTT for Sensor Networks (MQTT-SN) Version 1.3 · Web viewFeb 22, 2021  · A MQTT-SN GW may or may not be integrated with a MQTT server. In case of a stand-alone GW the MQTT protocol

Table 232: PINGREQ Message

As with MQTT, the PINGREQ message is an ”are you alive” message that is sent from or received by a connected client. Its format is illustrated in Table 22:

4.1.15.1[4.1.20.1] Length & Packet TypeThe first 2 or 4 bytes of the packet are encoded according to the variable length packet header format. Please refer to figure 1.2.1 for a detailed description.

4.1.15.2 Length and MsgType: see Section 5.2.Client Identifier

ClientId: cContains the client identifier (client id); this field is optional and is included by a “sleeping” client when it goes to the “awake” state and is waiting for messages sent by the server/gateway, see Section 6.14 for further details.

4.1.16[4.1.21] PINGRESPBit 7 6 5 4 3 2 1 0

Byte 1 Length

Byte 2 Packet Type

Length

MsgType

(byte 0)

(1)

Table 243: PINGRESP Message

As with MQTT, a PINGRESP message is the response to a PINGREQ message and means ”yes I am alive”. Keep Alive messages flow in either direction, sent either by a connected client or the gateway. Its format is illustrated in Table 23: it has only a header and no variable part.

Moreover, a PINGRESP message is sent by a gateway to inform a sleeping client that it has no more buffered messages for that client, see Section 6.14 for further details.

4.1.17[4.1.22] DISCONNECTBit 7 6 5 4 3 2 1 0

Byte 1 Length

Byte 2 Packet Type

Byte 3 Return Code (optional)

Byte 4 Session Expiry Interval MSB (optional)

Byte 5 Session Expiry Interval (optional)

Byte 6 Session Expiry Interval (optional)

mqtt-sn-v2.0v1.3-wd0532 Working Draft 0532 22 February16 April04 March 20202021Standards Track Draft Copyright © OASIS Open 20210. All Rights Reserved. Page 38 of 58

Page 39: MQTT for Sensor Networks (MQTT-SN) Version 1.3 · Web viewFeb 22, 2021  · A MQTT-SN GW may or may not be integrated with a MQTT server. In case of a stand-alone GW the MQTT protocol

Byte 7 Session Expiry Interval LSB (optional)

Byte 8 … N Reason String (optional)

Length

MsgType

ReturnCode

(2)

SessionExpiryIntervalDuration

(optional)

ReasonString

(7:n)

(byte 0)

(1) (2-33:6)

Table 254: DISCONNECT Message

As with MQTT, the DISCONNECT message is sent by a client to indicate that it wants to close the connection. The gateway will acknowledge the receipt of that message by returning a DISCONNECT to the client. A server or gateway may also sends a DISCONNECT to a client, e.g. in case a gateway, due to an error, cannot map a received message to a client. Upon receiving such a DISCONNECT message, a client should try to setup the connection again by sending a CONNECT message to the gateway or server. In all these cases the DISCONNECT message does not contain the Duration field.

A DISCONNECT message with a Session Expiry Interval field is sent by a client when it wants to go to the “asleep” state. The receipt of this message is also acknowledged by the gateway by means of a DISCONNECT message (without a duration field).

The format of the DISCONNECT message is illustrated in Table 24:

[4.1.22.1] Length & Packet TypeThe first 2 or 4 bytes of the packet are encoded according to the variable length packet header format. Please refer to figure 1.2.1 for a detailed description.

4.1.17.1 Length and MsgType: see Section 5.2.Return Code

ReturnCode: (optional) Reason for disconnection. See section 5.3.10.

4.1.17.2 Session Expiry Interval

Session Expiry Interval: (optional) cContains the value of the Session Expiry Interval timer; when the value of this field is greater than zero, it is deemed to be sent by a client that wants to transition to the “asleep” state, see Section 6.14 for further details.

4.1.17.3 Reason StringA string representing a clear text description of disconnection.

• ReasonString: (optional) A string representing a clear text description of disconnection.

mqtt-sn-v2.0v1.3-wd0532 Working Draft 0532 22 February16 April04 March 20202021Standards Track Draft Copyright © OASIS Open 20210. All Rights Reserved. Page 39 of 58

Page 40: MQTT for Sensor Networks (MQTT-SN) Version 1.3 · Web viewFeb 22, 2021  · A MQTT-SN GW may or may not be integrated with a MQTT server. In case of a stand-alone GW the MQTT protocol

• Duration: contains the value of the sleep timer; this field is optional and is included by a “sleeping” client that wants to go the “asleep” state, see Section 6.14 for further details.

As with MQTT, the DISCONNECT message is sent by a client to indicate that it wants to close the connection. The gateway will acknowledge the receipt of that message by returning a DISCONNECT to the client. A server or gateway may also sends a DISCONNECT to a client, e.g. in case a gateway, due to an error, cannot map a received message to a client. Upon receiving such a DISCONNECT message, a client should try to setup the connection again by sending a CONNECT message to the gateway or server. In all these cases the DISCONNECT message does not contain the Duration field.A DISCONNECT message with a Duration SessionExpiryInterval field is sent by a client when it wants to go to the “asleep” state. The receipt of this message is also acknowledged by the gateway by means of a DISCONNECT message (without a duration field).

[4.1.22.2] Duration• The Duration field is a 2-byte long. If present, it specifies Duration: contains the value of the sleep time in seconds. If this field is r; this field omitted the client will not wake.is optional and is included by a “sleeping” client that wants to go the “asleep” state, see Section 6.14 for further details.

4.1.18[4.1.23] WILLTOPICUPDBit 7 6 5 4 3 2 1 0

Byte 1 Length

Byte 2 Packet Type

DUP(

Reserved)

QoS Retain Will(

Reserved)

Clean(

Reserved)

Topic Id Type(Reserved)

Byte 3 0 X X X 0 0 0 0

Byte 4.. N Will Topic

Length

MsgType

Flags

WillTopic

(byte 0)

(1) (2) (3:n)

Table 265: WILLTOPICUPD Message

The WILLTOPICUPD message is sent by a client to update its Will topic name stored in the GW/server. Its format is shown in Table 265:

4.1.18.1[4.1.23.1] Length & Packet TypeThe first 2 or 4 bytes of the packet are encoded according to the variable length packet header format. Please refer to figure 1.2.1 for a detailed description.

mqtt-sn-v2.0v1.3-wd0532 Working Draft 0532 22 February16 April04 March 20202021Standards Track Draft Copyright © OASIS Open 20210. All Rights Reserved. Page 40 of 58

Andrew Banks, 01/22/21,
Rename to Sleep Interval?
Page 41: MQTT for Sensor Networks (MQTT-SN) Version 1.3 · Web viewFeb 22, 2021  · A MQTT-SN GW may or may not be integrated with a MQTT server. In case of a stand-alone GW the MQTT protocol

4.1.18.2 Length and MsgType: see Section 5.2.Flags

Bit 7 6 5 4 3 2 1 0

DUP(Reserved)

QoS Retain Will(Reserved)

Clean(Reserved)

Topic Id Type(Reserved)

0 X X X 0 0 0 0

• Flags:

– DUP: not used.

– QoS: same as MQTT, contains the Will QoS

– Retain: same as MQTT, contains the Will Retain flag

– Will: not used – CleanSession: not used – TopicIdType: not used.

4.1.18.3 Will Topic

WillTopic: cContains the Will topic name.

An empty WILLTOPICUPD message is a WILLTOPICUPD message without Flags and WillTopic field (i.e. it is exactly 2 bytes long). It is used by a client to delete its Will topic and Will message stored in the GW/server.

4.1.19[4.1.24] WILLMSGUPDBit 7 6 5 4 3 2 1 0

Byte 1 Length

Byte 2 Packet Type

Byte 3 .. N Will Msg

Table 276: WILLMSGUPD Message

The WILLMSGUPD message is sent by a client to update its Will message stored in the GW/server. Its format is shown in Table 26:

4.1.19.1 Length & Packet Type

Length and MsgType: see Section 5.2.The first 2 or 4 bytes of the packet are encoded according to the variable length packet header format. Please refer to figure 1.2.1 for a detailed description.

4.1.19.2 Will Msg

mqtt-sn-v2.0v1.3-wd0532 Working Draft 0532 22 February16 April04 March 20202021Standards Track Draft Copyright © OASIS Open 20210. All Rights Reserved. Page 41 of 58

Page 42: MQTT for Sensor Networks (MQTT-SN) Version 1.3 · Web viewFeb 22, 2021  · A MQTT-SN GW may or may not be integrated with a MQTT server. In case of a stand-alone GW the MQTT protocol

WillMsg: cContains the Will message.

4.1.20[4.1.25] WILLTOPICRESPBit 7 6 5 4 3 2 1 0

Byte 1 Length

Byte 2 Packet Type

Byte 3 Return Code

Length

MsgType

ReturnCode

(byte 0)

(1) (2)

Table 2728: WILLTOPICRESP and WILLMSGRESP Messages

The WILLTOPICRESP message is sent by a GW to acknowledge the receipt and processing of an WILLTOPICUPD message. Its format is illustrated in Table 287:

4.1.20.1[4.1.25.1] Length & Packet TypeThe first 2 or 4 bytes of the packet are encoded according to the variable length packet header format. Please refer to figure 1.2.1 for a detailed description.

4.1.20.2 Length and MsgType: see Section 5.2.Return Code

ReturnCode: “aAccepted,”, or rejection reason

4.1.21[4.1.26] WILLMSGRESPThe WILLMSGRESP message is sent by a GW to acknowledge the receipt and processing of an WILLMSGUPD message. Its format is illustrated in Table 287:

4.1.21.1[4.1.26.1] Length & Packet TypeThe first 2 or 4 bytes of the packet are encoded according to the variable length packet header format. Please refer to figure 1.2.1 for a detailed description.

• Length and MsgType: see Section 5.2.

[4.1.26.2] Return CodeAccepted, or rejection reason

• ReturnCode: “accepted”, or rejection reason

mqtt-sn-v2.0v1.3-wd0532 Working Draft 0532 22 February16 April04 March 20202021Standards Track Draft Copyright © OASIS Open 20210. All Rights Reserved. Page 42 of 58

Page 43: MQTT for Sensor Networks (MQTT-SN) Version 1.3 · Web viewFeb 22, 2021  · A MQTT-SN GW may or may not be integrated with a MQTT server. In case of a stand-alone GW the MQTT protocol

[4.2] Forwarder EncapsulationAs mentioned in Section 4, MQTT-SN clients can also access a GW via a forwarder in case the GW is not directly attached to their WSNs. The forwarder simply encapsulates the MQTT-SN frames it receives on the wireless side and forwards them unchanged to the GW; in the opposite direction, it decapsulates the frames it receives from the gateway and sends them to the clients, unchanged too.

Bit 7 6 5 4 3 2 1 0

Byte 1 Length

Byte 2 Packet Type

Byte 3 Ctrl

Byte 4 .. N Wireless Node Id

Byte (N + 1 ,,, M) MQTT SN Message

Table 298: Format of an encapsulated MQTT-SN frame

As mentioned in Section 4, MQTT-SN clients can also access a GW via a forwarder in case the GW is not directly attached to their WSNs. The forwarder simply encapsulates the MQTT-SN frames it receives on the wireless side and forwards them unchanged to the GW; in the opposite direction, it decapsulates the frames it receives from the gateway and sends them to the clients, unchanged too.

4.1.21.2[4.2.1.1] LengthThe format of an encapsulated MQTT-SN frame is shown in Table 28:

Length: 1-byte long, specifies the number of bytes up to the end of the “Wireless Node Id” field (incl. the Length byte itself)

4.1.21.3 Packet Type

MsgType: cCoded “0xFE”, see Table 3

4.1.21.4 CtrlThe Ctrl byte contains control information exchanged between the GW and the forwarder. Its format is shown in Table 30:

Bit 7 6 5 4 3 2 1 0

Reserved Radius

0 0 0 0 0 0 X X

Table 29: Format of the ctrl byte

- Table 29: Format of the Ctrl byte-

mqtt-sn-v2.0v1.3-wd0532 Working Draft 0532 22 February16 April04 March 20202021Standards Track Draft Copyright © OASIS Open 20210. All Rights Reserved. Page 43 of 58

Page 44: MQTT for Sensor Networks (MQTT-SN) Version 1.3 · Web viewFeb 22, 2021  · A MQTT-SN GW may or may not be integrated with a MQTT server. In case of a stand-alone GW the MQTT protocol

- Ctrl: The Ctrl byte contains control information exchanged between the GW and the forwarder. Its format is shown in Table 29:

- Radius: broadcast radius (only relevant in direction GW to forwarder)

4.1.21.5 Wireless Node Id

All remaing bits are reserved

Wireless Node Id: iIdentifies the wireless node which has sent or should receive the encapsulated MQTT-SN message. The mapping between this Id and the address of the wireless node is implemented by the forwarder, if needed.

4.1.21.6 MQTT SN Message

MQTT-SN message: tThe MQTT-SN message, encoded according to Table 1.

Table 29: Format of the Ctrl byte

mqtt-sn-v2.0v1.3-wd0532 Working Draft 0532 22 February16 April04 March 20202021Standards Track Draft Copyright © OASIS Open 20210. All Rights Reserved. Page 44 of 58

Page 45: MQTT for Sensor Networks (MQTT-SN) Version 1.3 · Web viewFeb 22, 2021  · A MQTT-SN GW may or may not be integrated with a MQTT server. In case of a stand-alone GW the MQTT protocol

2 Operational behaviorAn important design point of MQTT-SN is to be as close as possible to MQTT. Therefore, all protocol semantics should remain, as far as possible, the same as those defined by MQTT. In the following we will focus on those points that either are new to or deviate from MQTT.

4.2[4.3] Gateway Advertisement and DiscoveryThis procedure is new and does not exist in MQTT.

A gateway may announce its presence by broadcasting periodically an ADVERTISE message to all devices that are currently parts of the network. A gateway should only advertise its presence if it is connected to a server (or is itself a server).

Multiple gateways may be active at the same time in the same network. In this case they will have different ids. It is up to the client to decide to which gateway it wants to connect. However at any point in time a client is allowed to be connected to only one gateway.

A client should maintain a list of active gateways together with their network addresses. This list is populated by means of the ADVERTISE and GWINFO messages received.

The time duration TADV until the gateway sends the next ADVERTISE message is indicated in the Duration field of the ADVERTISE messages. A client may use this information to monitor the availability of a gateway. For example, if it does not receive ADVERTISE messages from a gateway for NADV consecutive times, it may assume that the gateway is down and removes it from its list of active gateways. Similarly, gateways in stand-by mode will become active (i.e. start sending ADVERTISE messages) if they miss successively a couple of times advertisements from a certain gateway.

Since the ADVERTISE messages are broadcasted into the whole wireless network, the time interval TADV between two ADVERTISE messages sent by a gateway should be large enough (e.g. greater than 15 min) to avoid bandwidth congestion in the network.

The large value of TADV will lead to a long waiting time for new clients which are looking for a gateway. To shorten this waiting time a client may broadcast a SEARCHGW message. To prevent broadcast storms when multiple clients start searching for GW almost at the same time, the sending of the SEARCHGW message is delayed by a random time between 0 and TSEARCHGW. A client will cancel its transmission of the SEARCHGW message if it receives during this delay time a SEARCHGW message sent by another client and identical to the one it wants to send, and behaves as if the SEARCHGW message was sent by itself.

The broadcast radius Rb of the SEARCHGW message is limited, e.g. to a single hop in case of a dense deployment of MQTT-SN clients.

Upon receiving a SEARCHGW message a gateway replies with a GWINFO message containing its id. Similarly, a client answers with a GWINFO message if it has at least one active gateway in its list of active gateways. If the client has multiple GWs in its list, it selects one GW out of its list and includes that information into the GWINFO message.

Like the SEARCHGW message, the GWINFO message is broadcast with the same radius Rb, which is indicated in the SEARCHGW message. The radius Rb is also given to the underlying layer when these two messages are passed down for transmission.

To give priority to the gateways a client will delay its sending of the GWINFO message for a random time

TGWINFO. If during this delay time the client receives a GWINFO message it will cancel the transmission of its GWINFO message.

In case of no response the SEARCHGW message may be retransmitted. In this case the time intervals between two consecutive SEARCHGW messages should be increased exponentially.

mqtt-sn-v2.0v1.3-wd0532 Working Draft 0532 22 February16 April04 March 20202021Standards Track Draft Copyright © OASIS Open 20210. All Rights Reserved. Page 45 of 58

Andrew Banks, 03/02/20,
Was “Functional Description”
Page 46: MQTT for Sensor Networks (MQTT-SN) Version 1.3 · Web viewFeb 22, 2021  · A MQTT-SN GW may or may not be integrated with a MQTT server. In case of a stand-alone GW the MQTT protocol

4.3[4.4] Client’s Connection SetupAs with MQTT, a MQTT-SN client needs to setup a connection to a GW before it can exchange information with that GW. The procedure for setting up a connection with a GW is illustrated in Fig. 3, in which it is assumed that the client requests the gateway to prompt for the transfer of Will topic and Will message. This request is indicated by setting the Will flag of the CONNECT message. The client then sends these two pieces of information to the GW upon receiving the corresponding request messages WILLTOPICREQ and WILLMSGREQ. The procedure is terminated with the CONNACK message sent by the GW.

If Will flag is not set then the GW answers directly with a CONNACK message.

Figure 3: Connect procedure

In case the GW could not accept the connection request (e.g. because of congestion or it does not support a feature indicated in the CONNECT message), the GW returns a CONNACK message with the rejection reason.

In the case where the client provides a zero length client identifier, the Server MUST respond with a CONNACK containing an Assigned Client Identifier. The Assigned Client Identifier MUST be a new Client Identifier not used by any other Session currently in the Server.

[4.5] Clean startessionWith MQTT, when a client disconnects, its subscriptions are not deleted. They are persistent and valid for new connections, until either they are explicitly un-subscribed by the client, or the client establishes a new connection with the “clean startession” flag set.

In MQTT-SN the meaning of a “clean startession” is extended to the Will feature, i.e. not only the subscriptions are persistent, but also the Will topic and the Will message. The two flags “CleanStartession” and “Will” in the CONNECT have then the following meanings:

• CleanStartession=true, Will=true: The GW will delete all subscriptions and Will data related to the client, and starts prompting for new Will topic and Will message.

mqtt-sn-v2.0v1.3-wd0532 Working Draft 0532 22 February16 April04 March 20202021Standards Track Draft Copyright © OASIS Open 20210. All Rights Reserved. Page 46 of 58

Page 47: MQTT for Sensor Networks (MQTT-SN) Version 1.3 · Web viewFeb 22, 2021  · A MQTT-SN GW may or may not be integrated with a MQTT server. In case of a stand-alone GW the MQTT protocol

• CleanStartession=true, Will=false: The GW will delete all subscriptions and Will data related to the client, and returns CONNACK (no prompting for Will topic and Will message).

• CleanStartession=false, Will=true: The GW keeps all stored client’s data, but prompts for new Will topic and Will message. The newly received Will data will overwrite the stored Will data.

• CleanStartession=false, Will=false: The GW keeps all stored client’s data and returns CONNACK (no prompting for Will topic and Will message).

Note that if a client wants to delete only its Will data at connection setup, it could send a CONNECT message with “CleanStartession=false” and “Will=true”, and sends an empty WILLTOPIC message to the GW when prompted to do so. It could also send a CONNECT message with “CleanStartession=false” and “Will=false”, and use the procedure of Section 6.4 to delete or modify the Will data.

4.4[4.6] Procedure for updating the Will dataAt any time during a connection a client could update its Will data stored in the gateway by sending a WILLTOPICUPD or a WILLMSGUPD message. The information contained in these two messages will overwrite the corresponding ones stored in the gateway. Both messages are acknowledged by the gateway. Both messages can be used independently from each other.

Note that an empty WILLTOPICUPD message will delete both the Will topic and Will message stored at the gateway.

4.5[4.7] Topic Name Registration ProcedureBecause of the limited bandwidth and the small message payload in wireless sensor networks, data is not published together with its topic name as in MQTT. A registration procedure is introduced which allows both a client and a GW to inform its peer about the short topic id and its corresponding topic name before it can start sending PUBLISH messages using the short topic id.

To register a topic name a client sends a REGISTER message to the GW. If the registration could be accepted, the gateway assigns a topicId to the received topic name and returns it with a REGACK message to the client. If the registration could not be accepted, a REGACK is also returned to the client with the failure reason encoded in the ReturnCode field.

After having received the REGACK message with ReturnCode=“accepted”, the client shall use the assigned topicId to publish data of the corresponding topic name. If however the REGACK contains a rejection code, the client may try to register later again. If the return code was “rejected: congestion”, the client should wait for a time TWAIT before restarting the registration procedure.

At any point in time a client may have only one REGISTER message outstanding, i.e. it has to wait for a REGACK message before it can register another topic name.

A GW sends a REGISTER message to a client if it wants to inform that client about the topic name and the assigned topic id that it will use later on when sending PUBLISH messages of the corresponding topic name. This happens for example when the client re-connects without having set the “CleanSession” flag or the client has subscribed to topic names that contain wildcard characters such as # or +.

mqtt-sn-v2.0v1.3-wd0532 Working Draft 0532 22 February16 April04 March 20202021Standards Track Draft Copyright © OASIS Open 20210. All Rights Reserved. Page 47 of 58

Page 48: MQTT for Sensor Networks (MQTT-SN) Version 1.3 · Web viewFeb 22, 2021  · A MQTT-SN GW may or may not be integrated with a MQTT server. In case of a stand-alone GW the MQTT protocol

4.6[4.8] Client’s Publish ProcedureAfter having registered successfully a topic name with the gateway, the client can start publishing data relating to the registered topic name by sending PUBLISH messages to the gateway. The PUBLISH messages contain the assigned topic id.

All three QoS levels and their corresponding message flows are supported as defined in MQTT. The only difference is the use of topic ids instead of topic names in the PUBLISH messages.

Regardless of the requested QoS level the client may receive in response to its PUBLISH a PUBACK message which contains either

• the ReturnCode= “Rejection: invalid topic Id”: in this case the client needs to register the topic name again before it can publish data related to that topic name; or

• the ReturnCode= “Rejection: congestion”: in this the client shall stop publishing toward the gateway for at least the time TWAIT.

At any point in time a client may have only one QoS level 1 or 2 PUBLISH message outstanding, i.e. it has to wait for the termination of this PUBLISH message exchange before it could start a new level 1 or 2 transaction.

4.7[4.9] Pre-defined topic ids and short topic namesAs described in Section 6.5, a topic id is a two-byte long replacement of the string-based topic name. A client needs to use the REGISTER procedure to inform the gateway about the topic name it wants to employ and gets from the gateway the corresponding topic id. It then will use this topic id in the PUBLISH messages it sends to the gateway. In the opposite direction, the PUBLISH messages also contain a 2-byte topic id (instead of the string-based topic name). The client is informed about the relation between topic id and topic name by means of either a former SUBSCRIBE procedure or a REGISTER procedure started by the gateway.

A “pre-defined” topic id is a topic id whose mapping to a topic name is known in advance by both the client’s application and the gateway. This is indicated in the Flags field of the message. When using pre-defined topic ids, both sides can start immediately with the sending of PUBLISH messages; there is no need for the REGISTER procedure as in the case of ”normal” topic ids. When receiving a PUBLISH message with a pre-defined topic id, of which the mapping to a topic name is unknown, the receiver should return a PUBACK with the ReturnCode= “Rejection: invalid topic Id”. Note that this error situation cannot be resolved by means of re-registering as in the case of normal topic id.

A client is still required to subscribe to a pre-defined topic id, if it wants to receive PUBLISH messages relating to that topic id. To avoid confusion between a pre-defined topic id and a two-byte short topic name, the SUBSCRIBE message contains a flag indicating whether it is subscribing for a short topic name or a pre-defined topic id.

A “short” topic name is a topic name that has a fixed length of two bytes. It could be carried together with the data within a PUBLISH message, thus no REGISTER procedure is needed for a short topic name. Otherwise, all rules that apply to normal topic names also apply to short topic names. Note however that it does not make sense to do wildcarding in subscriptions to short topic names, because it is not possible to define a meaningful name hierarchy with only two characters.

4.8[4.10] PUBLISH with QoS Level -1This feature is defined for very simple client implementations which do not support any other features except this one. There is no connection setup nor tear down, no registration nor subscription. The client just sends its PUBLISH messages to a GW (whose address is known a-priori by the client) and forgets them. It does not care whether the GW address is correct,

mqtt-sn-v2.0v1.3-wd0532 Working Draft 0532 22 February16 April04 March 20202021Standards Track Draft Copyright © OASIS Open 20210. All Rights Reserved. Page 48 of 58

Page 49: MQTT for Sensor Networks (MQTT-SN) Version 1.3 · Web viewFeb 22, 2021  · A MQTT-SN GW may or may not be integrated with a MQTT server. In case of a stand-alone GW the MQTT protocol

whether the GW is alive, or whether the messages arrive at the GW. Only the following parameter values are allowed for a PUBLISH message with QoS level -1:

• QoS flag: set to “0b11”;

• TopicIdType flag: either “0b01” for pre-defined topic id or “0b10” for short topic name;

• TopicId field: value of the pre-defined topic id or of the short topic name;

• Data field: the data to be published.

4.9[4.11] Client’s Topic Subscribe/Un-subscribe ProcedureTo subscribe to a topic name, a client sends a SUBSCRIBE message to the gateway with the topic name included in that message. If the gateway is able accept the subscription, it assigns a topic id to the received topic name and returns it within a SUBACK message to the client. If the subscription can not be accepted, then a SUBACK message is also returned to the client with the rejection cause encoded in the ReturnCode field. If the rejection cause is “rejected: congestion”, the client should wait for the time TWAIT before resending the SUBSCRIBE message to the gateway.

If the client subscribes to a topic name which contains a wildcard character, the returning SUBACK message will contain the topic id value 0x0000. The GW will the use the registration procedure to inform the client about the to-be-used topic id value when it has the first PUBLISH message with a matching topic name to be sent to the client, see also Section 6.10.

Similar to the client’s PUBLISH procedure, topic ids may also be pre-defined for certain topic names. Short topic names may be used as well. In those two cases the client still needs to subscribe to those pre-defined topic ids or short topic names.

To unsubscribe, a client sends an UNSUBSCRIBE message to the gateway, which will then be answered by means of an UNSUBACK message.

As for the REGISTER procedure, a client may have only one SUBSCRIBE or one UNSUBCRIBE transaction open at a time.

4.10[4.12] Gateway’s Publish ProcedureSimilar to the client’s PUBLISH procedure described in Section 6.6, the gateway sends PUBLISH messages with the topic id value that was returned in the SUBACK message to the client.

Preceding the PUBLISH message the GW may send a REGISTER message to inform the client about the topic name and its assigned topic id value. This will happen for example when the client re-connects without clean startession or has subscribed to topic names with wildcard characters. Upon receiving a REGISTER message the client replies with a REGACK message. The GW will wait for the REGACK message before it sends the PUBLISH message to the client.

The client could reject the REGISTER message with a REGACK message indicating the rejection reason; this corresponds to an unsubscribe to the topic name indicated in the REGISTER message. Note that unsubcribe to a topic name with wildcard characters can only be done with the unsubscribe procedure described in Section 6.9 and not with the rejection of a REGISTER message, since a REGISTER message never contains a topic name with wildcard characters.

If the client receives a PUBLISH message with an unknown topic id value, it shall respond with a PUBACK message with the ReturnCode=“Rejected: invalid Topic ID”. This will trigger the gateway to delete or correct the wrong topic id assignment.

Note that in case either the topic name or the data is too long to fit into a REGISTER or a PUBLISH message, the gateway silently aborts the publish procedure, i.e. no warning is sent to the affected subscribers.

mqtt-sn-v2.0v1.3-wd0532 Working Draft 0532 22 February16 April04 March 20202021Standards Track Draft Copyright © OASIS Open 20210. All Rights Reserved. Page 49 of 58

Page 50: MQTT for Sensor Networks (MQTT-SN) Version 1.3 · Web viewFeb 22, 2021  · A MQTT-SN GW may or may not be integrated with a MQTT server. In case of a stand-alone GW the MQTT protocol

4.11[4.13] Keep Alive and PING ProcedureAs with MQTT, the value of the Keep Alive timer is indicated in the CONNECT message. The client should send a PINGREQ message within each Keep Alive time period, which the GW acknowledges with a PINGRESP message.

Similarly, a client shall answer with a PINGRESP message when it receives a PINGREQ message from the GW to which it is connected. Otherwise the received PINGREQ message is ignored.

Clients should use this procedure to supervise the liveliness of the gateway to which they are connected. If a client does not receive a PINGRESP from the gateway even after multiple retransmissions of the PINGREQ message, it should first try to connect to another gateway before trying to re-connect to this gateway (see also section 6.13). Note that because the clients’ keep alive timers are not synchronized with each other, in case of a gateway failure there is practically no danger for a storm of CONNECT messages sent almost at the same time by all affected clients towards a new gateway.

4.12[4.14] Client’s Disconnect ProcedureA client sends a DISCONNECT message to the GW to indicate that it is about to close its connection. After this point, the client is then required to establish a new connection with the GW before it can exchange information with that GW again. Similar to MQTT, sending the DISCONNECT message does not affect existing subscriptions and Will data if the CleanSession flag is set. They are persistent until they are either explicitly un-subscribed, or deleted, or modified by the client, or if the client establishes a new connection with the CleanSession flag set. The gateway acknowledges the receipt of the DISCONNECT message by returning a DISCONNECT to the client.

A client may also receive an unsolicited DISCONNECT sent by the gateway. This may happen for example when the gateway, due to an error, cannot identify the client to which a received message belongs. Upon receiving such a DISCONNECT message a client should retry to setup the connection again by sending a CONNECT message to the gateway.

4.13[4.15] Client’s Retransmission ProcedureAll messages that are “unicasted” to the GW (i.e. sent using the GW’s unicast address and not broadcasted) and for which a GW’s reply is expected are supervised by a retry timer Tretry and a retry counter Nretry. The retry timer Tretry is started by the client when the message is sent and stopped when the expected GW’s reply is received. If Tretry times out and the expected GW’s reply is not received, the client retransmits the message. After Nretry retransmissions, the client aborts the procedure and assumes that its MQTT-SN connection to the gateway is disconnected. It should then try to connect to another gateway, and only if it fails to re-connect again to the former gateway.

4.14[4.16] Support of sleeping clientsSleeping clients are clients residing on (battery-operated) devices that want to save as much energy as possible. These devices need to enter a sleep mode whenever they are not active, and will wake up whenever they have data to send or to receive. The server/gateway needs to be aware of the sleeping state of these clients and will buffer messages destined to them for later delivery when they wake up.

mqtt-sn-v2.0v1.3-wd0532 Working Draft 0532 22 February16 April04 March 20202021Standards Track Draft Copyright © OASIS Open 20210. All Rights Reserved. Page 50 of 58

Page 51: MQTT for Sensor Networks (MQTT-SN) Version 1.3 · Web viewFeb 22, 2021  · A MQTT-SN GW may or may not be integrated with a MQTT server. In case of a stand-alone GW the MQTT protocol

Figure 4: Client’s state transition diagram

As illustrated in Fig 4, from the perspective of the server/gateway, a client may be in one of the following states: active, asleep, awake, disconnected, or lost. A client is in the active state when the server/gateway receives a CONNECT message from that client, as described in section 6.2. This state is supervised by the server/gateway with the “keep alive” timer as described in section 6.11. If the server/gateway does not receive any message from the client for a period longer than the keep alive duration (indicated in the CONNECT message), the gateway will consider that client as lost and activates for example the Will feature for that client.

A client goes to the disconnected state when the server/gateway receives a DISCONNECT without a duration field. This state is not time-supervised by the server/gateway.

If a client want to sleep, it sends a DISCONNECT message which contains a sleep duration. The server/gateway acknowledges that message with a DISCONNECT message and considers the client for being in asleep state, see also Fig. 5. The asleep state is supervised by the server/gateway with the indicated sleep duration. If the server/gateway does not receive any message from the client for a period longer than the sleep duration, the server/gateway will consider that client as lost and - as with the keep alive procedure - activates for example the Will feature. During the asleep state, all messages that need to be sent to the client are buffered at the server/gateway.

mqtt-sn-v2.0v1.3-wd0532 Working Draft 0532 22 February16 April04 March 20202021Standards Track Draft Copyright © OASIS Open 20210. All Rights Reserved. Page 51 of 58

Page 52: MQTT for Sensor Networks (MQTT-SN) Version 1.3 · Web viewFeb 22, 2021  · A MQTT-SN GW may or may not be integrated with a MQTT server. In case of a stand-alone GW the MQTT protocol

5 Retained MessagesIf the RETAIN flag is set to 1 in a PUBLISH packet sent by a Client to a Server, the Server MUST replace any existing retained message for this topic and store the Publish Data, so that it can be delivered to future subscribers whose subscriptions match its Topic Name. If the Publish Data contains zero bytes it is processed normally by the Server but any retained message with the same topic name MUST be removed and any future subscribers for the topic will not receive a retained message. A retained message with Publish Data containing zero bytes MUST NOT be stored as a retained message on the Server.

mqtt-sn-v2.0v1.3-wd0532 Working Draft 0532 22 February16 April04 March 20202021Standards Track Draft Copyright © OASIS Open 20210. All Rights Reserved. Page 52 of 58

Page 53: MQTT for Sensor Networks (MQTT-SN) Version 1.3 · Web viewFeb 22, 2021  · A MQTT-SN GW may or may not be integrated with a MQTT server. In case of a stand-alone GW the MQTT protocol

[5] Safety, Security, and Data Protection Considerations

(Note: OASIS strongly recommends that Technical Committees consider issues that might affect safety, security, privacy, and/or data protection in implementations of their specification and document these for implementers and adopters. For some purposes, you may find it required, e.g. if you apply for IANA registration.While it may not be immediately obvious how your specification might make systems vulnerable to attack, most specifications, because they involve communications between systems, message formats, or system settings, open potential channels for exploit. For example, IETF [RFC3552] lists “eavesdropping, replay, message insertion, deletion, modification, and man-in-the-middle” as well as potential denial of service attacks as threats that must be considered and, if appropriate, addressed in IETF RFCs.In addition to considering and describing foreseeable risks, this section should include guidance on how implementers and adopters can protect against these risks.We encourage editors and TC members concerned with this subject to read Guidelines for Writing RFC Text on Security Considerations, IETF [RFC3552], for more information.)

mqtt-sn-v2.0v1.3-wd0532 Working Draft 0532 22 February16 April04 March 20202021Standards Track Draft Copyright © OASIS Open 20210. All Rights Reserved. Page 53 of 58

Page 54: MQTT for Sensor Networks (MQTT-SN) Version 1.3 · Web viewFeb 22, 2021  · A MQTT-SN GW may or may not be integrated with a MQTT server. In case of a stand-alone GW the MQTT protocol

6 Conformance(Note: The OASIS TC Process requires that a specification approved by the TC at the Committee Specification Public Review Draft, Committee Specification or OASIS Standard level must include a separate section, listing a set of numbered conformance clauses, to which any implementation of the specification must adhere in order to claim conformance to the specification (or any optional portion thereof). This is done by listing the conformance clauses here.For the definition of "conformance clause," see OASIS Defined Terms.See "Guidelines to Writing Conformance Clauses": https://docs.oasis-open.org/templates/TCHandbook/ConformanceGuidelines.html.Remove this note before submitting for publication.)

mqtt-sn-v2.0v1.3-wd0532 Working Draft 0532 22 February16 April04 March 20202021Standards Track Draft Copyright © OASIS Open 20210. All Rights Reserved. Page 54 of 58

Page 55: MQTT for Sensor Networks (MQTT-SN) Version 1.3 · Web viewFeb 22, 2021  · A MQTT-SN GW may or may not be integrated with a MQTT server. In case of a stand-alone GW the MQTT protocol

Appendix A. Acknowledgments(Note: A Work Product approved by the TC must include a list of people who participated in the development of the Work Product. This is generally done by collecting the list of names in this appendix. This list shall be initially compiled by the Chair, and any Member of the TC may add or remove their names from the list by request.Remove this note before submitting for publication.)The following individuals have participated in the creation of this specification and are gratefully acknowledged:

Participants:!!br0ken!![Participant Name, Affiliation | Individual Member][Participant Name, Affiliation | Individual Member]

mqtt-sn-v2.0v1.3-wd0532 Working Draft 0532 22 February16 April04 March 20202021Standards Track Draft Copyright © OASIS Open 20210. All Rights Reserved. Page 55 of 58

Page 56: MQTT for Sensor Networks (MQTT-SN) Version 1.3 · Web viewFeb 22, 2021  · A MQTT-SN GW may or may not be integrated with a MQTT server. In case of a stand-alone GW the MQTT protocol

Appendix B. Implementation NotesB.1 Support of QoS Level -1Because PUBLISH messages with QoS level -1 could be sent at any time by clients (even with no connection setup) a transparent GW needs to maintain for those messages a dedicated MQTT connection with the server. An aggregating or hybrid GW may use any aggregating MQTT connection to forward those messages to the server.

B.2 “Best practice” values for timers and countersTable 30 shows the “best practice” values for the timers and counters defined in this specification.

Timer/Counter

Recommended value

TADV greater than 15 minutes

NADV 2 -3

TSEARCHGW 5 seconds

TGWINFO 5 seconds

TWAIT greater than 5 minutes

Tretry 10 - 15 seconds

Nretry 3 - 5Table 30: “Best practice” values for timers and counters

The “tolerance” of the sleep and keep-alive timers at the server/gateway depends on the duration indicated by the clients. For example, the timer values should be 10% higher than the indicated values for durations larger than 1 minute, and 50% higher if less.

B.3 Mapping of Topic Ids to Topic NamesIt is strongly recommended that in the gateway the mapping table between topic ids and topic names is implemented per client (and not by a single shared pool between all clients), to reduce the risk of an incorrect topic id from a client matching another client’s valid topic, and thus causing a publication to the wrong topic, which could potentially have disastrous consequences.

B.4 ZigBee related issues• In a ZigBee network a gateway need not be hosted by a coordinator node. It should

however reside on an always-on router node to be able to receive client messages at any time.

• Due to short payload length of the ZigBee network/APS layer, the maximum length of a MQTT-SN message is restricted to 60 bytes.

mqtt-sn-v2.0v1.3-wd0532 Working Draft 0532 22 February16 April04 March 20202021Standards Track Draft Copyright © OASIS Open 20210. All Rights Reserved. Page 56 of 58

Page 57: MQTT for Sensor Networks (MQTT-SN) Version 1.3 · Web viewFeb 22, 2021  · A MQTT-SN GW may or may not be integrated with a MQTT server. In case of a stand-alone GW the MQTT protocol

mqtt-sn-v2.0v1.3-wd0532 Working Draft 0532 22 February16 April04 March 20202021Standards Track Draft Copyright © OASIS Open 20210. All Rights Reserved. Page 57 of 58

Page 58: MQTT for Sensor Networks (MQTT-SN) Version 1.3 · Web viewFeb 22, 2021  · A MQTT-SN GW may or may not be integrated with a MQTT server. In case of a stand-alone GW the MQTT protocol

Appendix C. Revision HistoryRevision Date Editor Changes Made

WD-01 [27th February 2020] [Andrew Banks] [Merge Initial Document and Input Specification]

WD-02 [4th April 2020] [Andrew Banks][Rahul Gupta]

[Terminology, DataTypes, CONNECT packet][Specification Diagrams]

mqtt-sn-v2.0v1.3-wd0532 Working Draft 0532 22 February16 April04 March 20202021Standards Track Draft Copyright © OASIS Open 20210. All Rights Reserved. Page 58 of 58


Recommended