There are four ways in which an MQTT-SN packet can be published.
Using registered Topic Id – The client has to register the topic name with the gateway and then start publishing using the received topic id.
Using Short Topics – The client can directly publish using a topic name when the length of the topic name is less than 2 bytes.
Using Predefined Topics -The gateway will have a predefined topic whose topic id can be used by the client to publish. The predefined topic id in which the topic id of a topic is known for both server and client so there is no need to register
Adhoc Topic – This is a special one where the client does not have to create a connection to the gateway it can simply start publishing by setting the QoS flag to -1 no connection establishment or termination the gateway will maintain a separate MQTT connection to the broker to handle this kind of publish. In QoS -1 publish the client can only use predefined topic id or short topic name, there is no connection setup so registering topic is not possible
Packet Structure for the Register and publishing are as follows.
The Topicid has to be coded as 0 when the register message is sent by the client.
The Client can get the topic id from regack using the following packet structure.
|Msgtype(0x0B)||Topic id||Msgid||Return Code|
The client has to match the appropriate REGACK message to the register packet by using message-id.
The Client has to check the return code of REGACK and if its 0 the client can use the Topic id otherwise it has to Sent the register packet again.