6 Protocol Binding

6.1 Introduction

In this clause the use of MQTT is profiled and the key elements of the binding are defined:

  1. How a CSE or AE connects to MQTT.
  2. How an Originator (CSE or AE) formulates a Request as an MQTT message, and transmits it to its intended Receiver.
  3. How a Receiver listens for incoming Requests, and how it formulates and transmits a Response.
  4. How the Mca and Mcc CRUD operations map to MQTT messages.

For more information on MQTT itself see Annex A or refer to the MQTT specification [1].

6.2 Use of MQTT

MQTT includes reliability features which allow recovery from loss of network connectivity without requiring explicit involvement of the applications that are using it, however to do this it requires an underlying network protocol that provides ordered, lossless, bi-directional connections. The MQTT standard [1] allows a choice of underlying protocol. The present document restricts this choice: it shall be one of the following:

  • TCP/IP.
  • TCP/IP with Transport Level Security (TLS) 1.2
  • WebSocket - either with or without the use of TLS 1.2.

See the MQTT standard [1] for considerations on how to use these protocols with MQTT.

6.3 Connecting to MQTT

6.3.0 Introduction

In order to communicate, the two client parties (AE and CSE or CSE and CSE) shall connect to a common MQTT server. The MQTT server shall be hosted in one of the two nodes or shall exist as an independent external entity, following one of the two scenarios shown in clause 5.2.

Once each party has located the address of the MQTT server, it then connects to it using the standard MQTT CONNECT Control Packet.

An MQTT Control Packet consists of up to three parts: a fixed header, a variable header, and a payload as shown in Figure 6.3.0‑1.

Figure 6.3.0-1: Format of MQTT Control PacketFigure 6.3.0-1: Format of MQTT Control Packet
Figure 6.3.0‑1: Format of MQTT Control Packet

Figure 6.3.0‑1: Format of MQTT Control Packet

6.3.1 Variable header of MQTT CONNECT Packet

A variable header for the MQTT CONNECT Packet consists of four fields in the following order: Protocol Name, Protocol Level, Connect Flags and Keep Alive as shown in Figure 6.3.1‑1.

Figure 6.3.1-1: Variable header of MQTT CONNECT PacketFigure 6.3.1-1: Variable header of MQTT CONNECT Packet
Figure 6.3.1‑1: Variable header of MQTT CONNECT Packet

Figure 6.3.1‑1: Variable header of MQTT CONNECT Packet

The value of Protocol Name field is "MQTT". The value of the Protocol Level field for the MQTT version 3.1.1 of the protocol is 4. The Connect Flags is shown in Figure 6.3.1‑2. The Keep Alive is a time interval measured in seconds.

Figure 6.3.1-2: Connect Flags of variable header for MQTT CONNECT PacketFigure 6.3.1-2: Connect Flags of variable header for MQTT CONNECT Packet
Figure 6.3.1‑2: Connect Flags of variable header for MQTT CONNECT Packet

Figure 6.3.1‑2: Connect Flags of variable header for MQTT CONNECT Packet

6.3.2 Payload of MQTT CONNECT Packet

A payload for the MQTT CONNECT Packet is determined by the Connect Flags in the variable header. These fields may consist of Client Identifier, Will Topic, Will Message, User Name, Password.

Mandatory fields to establish an MQTT session for oneM2M are:

  • Client Identifier

Optional fields to establish an MQTT session for oneM2M are:

  • User Name
  • Password

An example of payload of an MQTT CONNECT Packet is shown in Figure 6.3.2‑1.

Figure 6.3.2-1: Example of an MQTT CONNECT PacketFigure 6.3.2-1: Example of an MQTT CONNECT Packet
Figure 6.3.2‑1: Example of an MQTT CONNECT Packet

Figure 6.3.2‑1: Example of an MQTT CONNECT Packet

6.3.3 Application of MQTT CONNECT Packet

The following additional considerations apply:

  • Client Identifier: The CONNECT Packet contains a Client Identifier as described in clause 6.3.2. The Client Identifiers have to be unique at least among all clients that connect to a given MQTT server instance (this is a requirement imposed by the MQTT protocol). This condition will be satisfied if an AE uses its AE-ID and a CSE uses its CSE-ID. See clause 7 of oneM2M TS-0001 [2] for a discussion of these Identifiers. The prefix A:: or C:: shall be added to the ID to show whether it is an AE-ID or a CSE-ID as these ID spaces are not distinct.
    The AE-ID or CSE-ID may not be known during the initial registration process, in which case the client shall use some other appropriate unique ID.
  • Connect Flags:
    • A client shall set the "Clean Session" flag in the MQTT CONNECT Packet to false. This means that MQTT Session state related to that client will be retained by the MQTT Server in the event of a disconnection (deliberate or otherwise) of that client.
    • A client shall not set the "Will Flag", "Will QoS", or "Will Retain" so Will Message and Will Topic shall not be present in the payload.
  • Keep Alive: A client may choose to provide a non-zero MQTT Keep Alive value or to provide a Keep Alive of 0 (this disables the MQTT Keep Alive).
  • User Name and Password: The MQTT server may require that a client provides a User Name and a password (or other credential). If the MQTT server authenticates by user name and password, the corresponding user name flag and password flag in the CONNECT shall be set to 1. For more information see clause 7 Security.

A client might choose to keep the MQTT connection open permanently (restarting it as soon as possible after any unforeseen connection loss), it might choose to connect only when it wants to act as an Originator, or it might choose to connect based on the associated with a relevant oneM2M resource.

Once a client has connected to the MQTT server it can then communicate (subject to authorization policies) with any other client connected to its server. There is no need for it to create another connection if it wants to communicate with a different counter-party.

When a client determines that it no longer wishes to participate in an MQTT Session with its MQTT Server it shall perform the following steps:

  • Disconnect from that server, if it is currently connected.
  • Reconnect with the cleanSession flag set to true.
  • Disconnect again.

These steps delete any state that the MQTT server might be holding on behalf of the client.

6.4 Sending and Receiving Messages

6.4.1 Request and Response Messages

6.4.1.0 Introduction

MQTT does not have a data model to describe or constrain the content of its Application Message payloads (to that extent it is similar to a TCP socket). Mca, Mcc and Mcc' request messages shall be serialized into XML or JSON or CBOR following the serialization process defined in clause 6.5.

The packet type field in Figure 6.4.1.1‑1 is used to define the MQTT Control Packet type. It is a 4-bit field which possible values are listed in Table 6.4.1.0‑1. When a oneM2M Request/Response message is bound to MQTT, its packet type shall have value 3, i.e. the Request/Response message is delivered in an MQTT PUBLISH Packet.

Table 6.4.1.0‑1: MQTT Control Packet Types

Reserved
0
Reserved for future use
CONNECT
1
Client request to connect to server
CONNACK
2
Connect acknowledgement
PUBLISH
3
Publish message
PUBACK
4
Publish message acknowledgement
PUBREC
5
Publish received (QoS=2)
PUBREL
6
Publish release (QoS=2)
PUBCOMP
7
Publish complete (QoS=2)
SUBSCRIBE
8
Client subscribe request
SUBACK
9
Subscribe acknowledgement
UNSUBSCRIBE
10
Client unsubscribe request
UNSUBACK
11
Unsubscribe acknowledgement
PINGREQ
12
Ping request
PINGRESP
13
Ping response
DISCONNECT
14
Client disconnection request
Reserved
15
Reserved for future use

6.4.1.1 Fixed header of MQTT PUBLISH Packet

The fixed header of the MQTT PUBLISH Packet consists of RETAIN, QoS Level, DUP flag, Packet Type, Remaining Length fields as shown in Figure 6.4.1.1‑1. The QoS Level represents the QoS level of the MQTT PUBLISH Packet with possible values of 0, 1 or 2. However, since oneM2M messages are idempotent, the QoS Level should not be set to QoS 2.

Figure 6.4.1.1-1: Fixed header of MQTT PUBLISH PacketFigure 6.4.1.1-1: Fixed header of MQTT PUBLISH Packet
Figure 6.4.1.1‑1: Fixed header of MQTT PUBLISH Packet

Figure 6.4.1.1‑1: Fixed header of MQTT PUBLISH Packet

NOTE: MQTT packets are subjected to a theoretical maximum message size of 256 MB, but it is good practice not to send packets that are bigger than a 100 kB. If a larger amount of data needs to be sent, it should be segmented into multiple PUBLISH packets.

6.4.1.2 Variable header of MQTT PUBLISH Packet

The variable header for the MQTT PUBLISH Packet consists of two fields in the following order: Topic Name, Packet Identifier.

Figure 6.4.1.2-1: Variable header for PUBLISH PacketFigure 6.4.1.2-1: Variable header for PUBLISH Packet
Figure 6.4.1.2‑1: Variable header for PUBLISH Packet

Figure 6.4.1.2‑1: Variable header for PUBLISH Packet

The Topic Name identifies the information channel to which payload data is published. The Topic Name for a oneM2M Request Message is specified at clause 6.4.2. The Topic Filter used to listen for and respond to a Request is specified at clause 6.4.3.

The Packet Identifier field is only present in PUBLISH Packets where the QoS level is 1 or 2.

6.4.1.3 Payload of MQTT Control PUBLISH Packet

The payload for the MQTT PUBLISH Packet is a oneM2M Request Message or Response Message as specified in clause 6.5.

6.4.2 Topic Name for Requests

A request is transmitted by sending it as an MQTT PUBLISH Packet to the MQTT Server. The MQTT Publish Packet uses a Topic Name that identifies both the Originator and the Receiver of the request as follows:

  • /oneM2M/req/

    • "oneM2M" is a literal string identifying the topic as being used by oneM2M.
    • is the SP-relative-AE-ID or SP-relative-CSE-ID of the entity that sends the request on the Mca or Mcc reference point, omitting any leading "/"s and replacing any other "/" characters with ":" characters.
    • is the Absolute-CSE-ID of an IN-CSE on the Mcc' reference point, replacing the leading "//" with ":" and replacing any other "/" characters with ":" characters.
    • is the SP-relative-AE-ID or SP-relative-CSE-ID of the Receiver (AE, Transit CSE or Hosting CSE) on the Mca or Mcc reference point, omitting any leading "/"s and replacing any other "/" characters with ":" characters.
    • is the Absolute-CSE-ID of an IN-CSE on the Mcc' reference point, replacing the leading "//" with ":" and replacing any other "/" characters with ":" characters.
    • "req" is a literal string identifying this as a request.
    • is "xml", "json" or "cbor" indicating the MQTT payload data type as described in clause 6.5.4.

6.4.3 Listening for and responding to a Request

A Receiver listens for requests arriving via MQTT by subscribing using a wildcarded Topic Filter of the following form:

  • /oneM2M/req/+/
    • "oneM2M" is a literal string identifying the topic as being used by oneM2M.
    • + is a wildcard which matches any entity.
    • is the SP-relative-AE-ID or SP-relative-CSE-ID of the Receiver (AE, Transit CSE or Hosting CSE) on the Mca or Mcc reference point, omitting any leading "/"s and replacing any other "/" characters with ":" characters.
    • is the Absolute-CSE-ID of an IN-CSE on the Mcc' reference point, replacing the leading "//" with ":" and replacing any other "/" characters with ":" characters.
    • "req" is a literal string identifying this as a request.
    • is "xml", "json" or "cbor" indicating the MQTT payload data type as described in clause 6.5.4. A wildcard may be used to listen for any type.

When it receives a request, the Receiver shall perform the Core Transport operations associated with the request, including any access control policy checks. In particular it shall check the request expiration timestamp (if any) contained in the request, since it is possible that that time might have passed while the message was being stored by MQTT.

It transmits a response by sending an MQTT PUBLISH Packet to a response topic. This takes the form:

  • /oneM2M/resp/
    • "oneM2M" is a literal string identifying the topic as being used by oneM2M.
    • is the SP-relative-AE-ID or SP-relative-CSE-ID of the Receiver (AE, Transit CSE or Hosting CSE) on the Mca or Mcc reference point, omitting any leading "/"s and replacing any other "/" characters with ":" characters.
    • is the Absolute-CSE-ID of an IN-CSE on the Mcc' reference point, replacing the leading "//" with ":" and replacing any other "/" characters with ":" characters.
    • is the SP-relative-AE-ID or SP-relative-CSE-ID of the entity that sent the corresponding request on the Mca or Mcc reference point, omitting any leading "/"s and replacing any other "/" characters with ":" characters.
    • is the Absolute-CSE-ID of an IN-CSE on the Mcc' reference point, replacing the leading "//" with ":" and replacing any other "/" characters with ":" characters.
    • "resp" is a literal string identifying this as a response.
    • is "xml", "json" or "cbor" indicating the MQTT payload data type as described in clause 6.5.4.

The Originator shall subscribe to this Topic (either explicitly or using a wildcarded filter) in order to see the response.

The payload of the MQTT PUBLISH packet is used to carry the response primitive, as described in clause 6.5.

6.4.4 Initial Registration

In some security scenarios, an Originator might not initially know its AE-ID or CSE-ID. Initial registration exchanges can use the communication pattern described in clauses6.4.1 and 6.4.2 except that they use Topics containing a credential ID rather than an AE-ID or CSE-ID, as follows:

  • /oneM2M/reg_req/
    • "oneM2M" is a literal string identifying the topic as being used by oneM2M.
    • is the Credential-ID. Any "/" characters embedded in the ID shall be replaced with ":" characters.
    • is the SP-relative-CSE-ID of the Receiver (Transit or Hosting CSE) specified in the corresponding request, omitting any leading "/".
    • "reg_req" is a literal string identifying it as a registration request.
    • is "xml", "json" or "cbor" indicating the MQTT payload data type as described in clause 6.5.4.

and

  • /oneM2M/reg_resp/
    • "oneM2M" is a literal string identifying the topic as being used by oneM2M.
    • is the Credential-ID of the Originator in the corresponding request. Any "/" characters embedded in the ID shall be replaced with ":" characters.
    • is the SP-relative-CSE-ID of the Receiver (Transit or Hosting CSE) in the corresponding request, omitting any leading "/".
    • "reg_resp" is a literal string identifying it as a registration response.
    • is "xml", "json" or "cbor" indicating the MQTT payload data type as described in clause 6.5.4.

6.4.5 Request/Response Message Flow (Mca or Mcc)

Figure 6.4.5-1: Initiating Process in MQTT bindingFigure 6.4.5-1: Initiating Process in MQTT binding
Figure 6.4.5‑1: Initiating Process in MQTT binding

Figure 6.4.5‑1: Initiating Process in MQTT binding

In the MQTT protocol, each client shall subscribe to the MQTT server to receive messages. As shown in figure 6.4.5‑1, the AE or CSE initiates the MQTT binding process by trying to connect to the MQTT server, as described in clause 6.3.

After each MQTT client successfully connects to the server, it shall subscribe to the MQTT server. The Topic Filters with which each MQTT client subscribes are "/oneM2M/req/+/

Accordingly the plus sign ('+' U+002B) wildcard and the SP-relative-AE-ID or SP-relative-CSE-ID are used in the Topic Filter within the MQTT SUBSCRIBE Packet. This enables the MQTT client to receive the PUBLISH Packets whose target it is. Therefore, through this process, the AE or CSE receives messages if the request/response messages are published to "/oneM2M/req/

Figure 6.4.5-2: Request/Response message delivery over MQTTFigure 6.4.5-2: Request/Response message delivery over MQTT
Figure 6.4.5‑2: Request/Response message delivery over MQTT

Figure 6.4.5‑2: Request/Response message delivery over MQTT

As an example, Figure 6.4.5‑2 illustrates the Request / Response message delivery over MQTT protocol between Originator and Receiver via the Mca/Mcc reference point in oneM2M. The message flow is as follows.

In this flow, the Originator wants to send a Request message to the Receiver. The Originator's MQTT client library sends an MQTT PUBLISH Packet to the MQTT server with "/oneM2M/req/SP-relative-AE-ID/SP-relative-CSE-ID/" as the Topic Name.

The MQTT PUBLISH packet shall include {"op", "fr", "to", "rqi"} and any optional parameters in accordance with the operation (CREATE, RETRIEVE, UPDATE, DELETE, NOTIFY) as specified in clause 7.2.1.1 of oneM2M TS-0004 [3] in its payload.

When the MQTT server receives the MQTT PUBLISH Packet from the MQTT client, the server refers to the Topic Name and delivers the message to the intended MQTT client. Finally, the MQTT client library delivers the message to the Receiver.

After that, the Receiver builds a Topic Name for the Response message of the form "/oneM2M/resp/Originator-ID/Receiver-ID/". The payload of the MQTT PUBLISH Packet shall include {"rsc", "rqi"} and any optional message parameters in accordance with operation (CREATE, RETRIEVE, UPDATE, DELETE, NOTIFY) as specified in clause 7.2.1.2 of oneM2M TS-0004 [3] in its payload.

6.5 Primitive Mapping

6.5.1 Request primitives

A oneM2M request primitive is made up of a number of control parameters and (optionally) a content part. All the parameters in these parts are serialized into the payload of an MQTT Publish Packet, using the rules given in clause 8 of oneM2M TS-0004 [3] applied to m2m:requestPrimitive defined in clause 6.4.1 of oneM2M TS-0004 [3].

All the parameters that are present in the primitive shall be serialized, in particular the request shall contain the mandatory parameters such as Operation, To, From, Request Identifier as specified in clause 8.1.2 of oneM2M TS-0001 [2] and 7.1.1.1 of oneM2M TS-0004 [3].

Figure 6.5.1-1: MQTT Request exampleFigure 6.5.1-1: MQTT Request example
Figure 6.5.1‑1: MQTT Request example

Figure 6.5.1‑1: MQTT Request example

An example of an MQTT Request message serialized using JSON is:

{
 "op": 1,
 "to": "//xxxxx/2345",
 "fr": "//xxxxx/99",
 "rqi": "A1234",
 "ty": 18,
 "pc": {
        "m2m:sch":{
                  "rn": "schedule1",
                  "se": {
                         "sce": ["* 0-5 2,6,10 * * * *"]
                        }
                  }
        },
 "ot": 20150910T062032
}
  • op: short name of Operation parameter specified as m2m:operation in oneM2M TS-0004 [3].
  • to: short name of To parameter specified either xs:anyURI [3] or m2m:nhURI [3]. It is an URI of the target resource.
  • fr: short name of From parameter which is an ID of the Originator e.g. either the AE or CSE.
  • rqi: short name of Request Identifier specified as m2m:requestID [3].
  • ty: short name of Resource Type parameter specified as m2m:resourceType [3].
  • pc: short name of Content parameter specified in oneM2M TS-0004 [3].
  • ot: short name of Originating Timestamp parameter specified as m2m:timestamp [3].

6.5.2 Response primitives

A oneM2M response primitive is serialized using the rules given in clause 8 of oneM2M TS-0004 [3] applied to m2m:responsePrimitive defined in clause 6.4.2 of oneM2M TS-0004 [3].

In particular, each response primitive shall include the Response Status Code parameter to indicate success or failure of the operation and the Request Identifier parameter.

Figure 6.5.2-1: MQTT Response exampleFigure 6.5.2-1: MQTT Response example
Figure 6.5.2‑1: MQTT Response example

Figure 6.5.2‑1: MQTT Response example

An example of an MQTT Response message serialized using JSON is:

{
 "rsc": 2000,
 "rqi": "A1234",
 "pc": {
         "m2m:sch": {
                    "se": {
                            "sce": ["* 0-5 2,6,10 * * * *"]
                          }
                    }
        },
 "to": "//xxxxx/2345",
 "fr": "//xxxxx/99"
}
  • rsc: short name of Response Status Code parameter specified as m2m:responseStatusCode in oneM2M TS-0004 [3].
  • rqi: short name of Request Identifier specified as m2m:requestID in oneM2M TS-0004 [3].
  • pc: short name of Content parameter specified in oneM2M TS-0004 [3].
  • to: short name of To parameter specified either xs:anyURI [3] or m2m:nhURI [3]. It is an URI of the target resource.
  • fr: short name of From parameter which is an ID of the Originator e.g. either the AE or CSE.

6.5.3 Serialization Format Negotiation

When sending a response primitive over MQTT, the Receiver should use the same serialization that was used in the corresponding request primitive.

6.5.4 Content-type

An MQTT message payload contains a oneM2M request or response primitive which is serialized using the XML, the JSON or the CBOR encoding given in clause 8 of oneM2M TS-0004 [3].

When an MQTT client publishes a message, it shall use a Topic name to indicate the format of the payload included in a PUBLISH message. As defined in clause 8 of oneM2M TS-0004 [3], the payload of request or response message shall include oneM2M primitives serialized using XML, JSON or CBOR.

The Topic name takes this form:

  • /oneM2M/req/
  • /oneM2M/resp/

In order to receive a PUBLISH message with the Topic name, each MQTT client shall subscribe to the Topic name as follows:

  • /oneM2M/req/+//#
  • /oneM2M/resp//#

In some figures in the present document, topic strings are shown that do not end with /json, /xml or /cbor. However the suffix /json, /xml, or /cbor shall always be used.

6.6 URL format

6.6.1 Introduction

oneM2M defines an MQTT URL format to be used in the pointOfAccess attributes in several entity resource types (e.g. notificationURI attributes of the resource type.

6.6.2 MQTT URL formats

oneM2M supports the following MQTT URL formats:

  • mqtt://
  • mqtts://
  • mqtt+ws://
  • mqtt+wss://
  • mqtt://
  • mqtts://
  • mqtt+ws://
  • mqtt+wss://

The component is defined in clause 3.2 of IETF RFC 3986 [8] and includes the host and optionally the port of the MQTT Server that is to be used.

The formats with the scheme mqtt: indicate that TLS shall not be used when connecting to the MQTT server.

The formats with the scheme mqtts: indicate that TLS shall be used when connecting to the MQTT server.

The formats with the scheme mqtt+ws: indicate that MQTT over WebSocket (no TLS) shall be used when connecting to the MQTT server.

The formats with the scheme mqtt+wss: indicate that MQTT over WebSocket with TLS shall be used when connecting to the MQTT server.

If the does not contain a port component, then the IANA-registered TCP/IP ports shown in Table 6.6‑1 shall be used.

Table 6.6‑1: Default port numbers

Scheme
Port Number
mqtt:
1883
mqtts:
8883
mqtt+ws:
80
mqtt+wss:
443

The , if present, contains one or more MQTT topic levels (separated by a "/" if there are multiple levels). If the path is present it is preceded by a "/" character to separate it from the authority component. This "/" character is not treated as being part of the path. If the path itself starts with a "/" character this results in the authority component being followed by two "/" characters. A "/" character inside the path may be repeated.

6.6.3 Using an MQTT URL in a pointOfAccess attribute

A pointOfAccess attribute contains a list of one or more strings, each of which indicates a way in which that entity can be addressed. An entity can indicate support for MQTT by including one or more strings that use the formats defined in clause 6.6.2.

If there is a path component in a pointOfAccess string, the path is prepended to the topic strings defined in the present document.

EXAMPLES: An AE with AE-ID of "CAE01" would subscribe to MQTT topic strings as follows:

  1. If it has a pointOfAccess string mqtt://myserver.example it would subscribe to oneM2M/req/+/CAE01/# (see clause 6.5.4).
  2. If it has a pointOfAccess string mqtt://myserver.example/abc/def it would subscribe to abc/def/oneM2M/req/+/CAE01/#.
  3. If it has a pointOfAccess string mqtt://myserver.example//def/ghi it would subscribe to /def/ghi/oneM2M/req/+/CAE01/#.
  4. If it has a pointOfAccess string mqtt://myserver.example///uvw//xyz it would subscribe to //uvw//xyz/oneM2M/req/+/CAE01/#.

6.6.4 Using an MQTT URL other than in a pointOfAccess attribute

An MQTT URL that is used anywhere other than in a pointOfAccess attribute shall contain a path component. That path gives the entire MQTT topic string that is to be used (note that the "/" character preceding the path does not form part of the topic string). The structure of that topic string does not have to conform to any of the topic string patterns defined in the present document.

EXAMPLES:

  1. The URL mqtt://myserver.example/abc/def refers to the Topic abc/def on the MQTT server hosted on myserver.example.
  2. The URL mqtt://myserver.example//def/ghi refers to the Topic /def/ghi on the MQTT server hosted on myserver.example.
  3. The URL mqtt://myserver.example///uvw//xyz refers to the Topic //uvw//xyz on the MQTT server hosted on myserver.example.