Low level MQTTSN packet library, part of the Eclipse Paho project: http://eclipse.org/paho

Dependents:   MQTTSN sara-n200-hello-mqtt-sn MQTTSN_2

The master source for this project is held at: https://github.com/eclipse/paho.mqtt-sn.embedded-c

Committer:
icraggs
Date:
Thu Feb 26 15:59:36 2015 +0000
Revision:
0:c524a894b5e8
Child:
1:7fa362fa563f
First version that works nicely :-)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
icraggs 0:c524a894b5e8 1 /*******************************************************************************
icraggs 0:c524a894b5e8 2 * Copyright (c) 2014 IBM Corp.
icraggs 0:c524a894b5e8 3 *
icraggs 0:c524a894b5e8 4 * All rights reserved. This program and the accompanying materials
icraggs 0:c524a894b5e8 5 * are made available under the terms of the Eclipse Public License v1.0
icraggs 0:c524a894b5e8 6 * and Eclipse Distribution License v1.0 which accompany this distribution.
icraggs 0:c524a894b5e8 7 *
icraggs 0:c524a894b5e8 8 * The Eclipse Public License is available at
icraggs 0:c524a894b5e8 9 * http://www.eclipse.org/legal/epl-v10.html
icraggs 0:c524a894b5e8 10 * and the Eclipse Distribution License is available at
icraggs 0:c524a894b5e8 11 * http://www.eclipse.org/org/documents/edl-v10.php.
icraggs 0:c524a894b5e8 12 *
icraggs 0:c524a894b5e8 13 * Contributors:
icraggs 0:c524a894b5e8 14 * Ian Craggs - initial API and implementation and/or initial documentation
icraggs 0:c524a894b5e8 15 *******************************************************************************/
icraggs 0:c524a894b5e8 16
icraggs 0:c524a894b5e8 17 #include "MQTTSNPacket.h"
icraggs 0:c524a894b5e8 18 #include "StackTrace.h"
icraggs 0:c524a894b5e8 19
icraggs 0:c524a894b5e8 20 #include <string.h>
icraggs 0:c524a894b5e8 21
icraggs 0:c524a894b5e8 22
icraggs 0:c524a894b5e8 23 /**
icraggs 0:c524a894b5e8 24 * Determines the length of the MQTT publish packet that would be produced using the supplied parameters
icraggs 0:c524a894b5e8 25 * @param qos the MQTT QoS of the publish (packetid is omitted for QoS 0)
icraggs 0:c524a894b5e8 26 * @param topicName the topic name to be used in the publish
icraggs 0:c524a894b5e8 27 * @param payloadlen the length of the payload to be sent
icraggs 0:c524a894b5e8 28 * @return the length of buffer needed to contain the serialized version of the packet
icraggs 0:c524a894b5e8 29 */
icraggs 0:c524a894b5e8 30 int MQTTSNSerialize_publishLength(int payloadlen, MQTTSN_topicid topic, int qos)
icraggs 0:c524a894b5e8 31 {
icraggs 0:c524a894b5e8 32 int len = 6;
icraggs 0:c524a894b5e8 33
icraggs 0:c524a894b5e8 34 if (topic.type == MQTTSN_TOPIC_TYPE_NORMAL && qos == 3)
icraggs 0:c524a894b5e8 35 len += topic.data.long_.len;
icraggs 0:c524a894b5e8 36
icraggs 0:c524a894b5e8 37 return payloadlen + len;
icraggs 0:c524a894b5e8 38 }
icraggs 0:c524a894b5e8 39
icraggs 0:c524a894b5e8 40
icraggs 0:c524a894b5e8 41 /**
icraggs 0:c524a894b5e8 42 * Serializes the supplied publish data into the supplied buffer, ready for sending
icraggs 0:c524a894b5e8 43 * @param buf the buffer into which the packet will be serialized
icraggs 0:c524a894b5e8 44 * @param buflen the length in bytes of the supplied buffer
icraggs 0:c524a894b5e8 45 * @param dup integer - the MQTT dup flag
icraggs 0:c524a894b5e8 46 * @param qos integer - the MQTT QoS value
icraggs 0:c524a894b5e8 47 * @param retained integer - the MQTT retained flag
icraggs 0:c524a894b5e8 48 * @param packetid integer - the MQTT packet identifier
icraggs 0:c524a894b5e8 49 * @param topic MQTTSN_topicid - the MQTT topic in the publish
icraggs 0:c524a894b5e8 50 * @param payload byte buffer - the MQTT publish payload
icraggs 0:c524a894b5e8 51 * @param payloadlen integer - the length of the MQTT payload
icraggs 0:c524a894b5e8 52 * @return the length of the serialized data. <= 0 indicates error
icraggs 0:c524a894b5e8 53 */
icraggs 0:c524a894b5e8 54 int MQTTSNSerialize_publish(unsigned char* buf, int buflen, unsigned char dup, int qos, unsigned char retained, unsigned short packetid,
icraggs 0:c524a894b5e8 55 MQTTSN_topicid topic, unsigned char* payload, int payloadlen)
icraggs 0:c524a894b5e8 56 {
icraggs 0:c524a894b5e8 57 unsigned char *ptr = buf;
icraggs 0:c524a894b5e8 58 MQTTSNFlags flags;
icraggs 0:c524a894b5e8 59 int len = 0;
icraggs 0:c524a894b5e8 60 int rc = 0;
icraggs 0:c524a894b5e8 61
icraggs 0:c524a894b5e8 62 FUNC_ENTRY;
icraggs 0:c524a894b5e8 63 if ((len = MQTTSNPacket_len(MQTTSNSerialize_publishLength(payloadlen, topic, qos))) > buflen)
icraggs 0:c524a894b5e8 64 {
icraggs 0:c524a894b5e8 65 rc = MQTTSNPACKET_BUFFER_TOO_SHORT;
icraggs 0:c524a894b5e8 66 goto exit;
icraggs 0:c524a894b5e8 67 }
icraggs 0:c524a894b5e8 68 ptr += MQTTSNPacket_encode(ptr, len); /* write length */
icraggs 0:c524a894b5e8 69 writeChar(&ptr, MQTTSN_PUBLISH); /* write message type */
icraggs 0:c524a894b5e8 70
icraggs 0:c524a894b5e8 71 flags.all = 0;
icraggs 0:c524a894b5e8 72 flags.bits.dup = dup;
icraggs 0:c524a894b5e8 73 flags.bits.QoS = qos;
icraggs 0:c524a894b5e8 74 flags.bits.retain = retained;
icraggs 0:c524a894b5e8 75 flags.bits.topicIdType = topic.type;
icraggs 0:c524a894b5e8 76 writeChar(&ptr, flags.all);
icraggs 0:c524a894b5e8 77
icraggs 0:c524a894b5e8 78 if (topic.type == MQTTSN_TOPIC_TYPE_NORMAL && qos == 3)
icraggs 0:c524a894b5e8 79 {
icraggs 0:c524a894b5e8 80 /* special arrangement for long topic names in QoS -1 publishes. The length of the topic is in the topicid field */
icraggs 0:c524a894b5e8 81 writeInt(&ptr, topic.data.long_.len); /* topic length */
icraggs 0:c524a894b5e8 82 }
icraggs 0:c524a894b5e8 83 else if (topic.type == MQTTSN_TOPIC_TYPE_NORMAL || topic.type == MQTTSN_TOPIC_TYPE_PREDEFINED)
icraggs 0:c524a894b5e8 84 writeInt(&ptr, topic.data.id);
icraggs 0:c524a894b5e8 85 else
icraggs 0:c524a894b5e8 86 {
icraggs 0:c524a894b5e8 87 writeChar(&ptr, topic.data.short_name[0]);
icraggs 0:c524a894b5e8 88 writeChar(&ptr, topic.data.short_name[1]);
icraggs 0:c524a894b5e8 89 }
icraggs 0:c524a894b5e8 90 writeInt(&ptr, packetid);
icraggs 0:c524a894b5e8 91 if (topic.type == MQTTSN_TOPIC_TYPE_NORMAL && qos == 3)
icraggs 0:c524a894b5e8 92 {
icraggs 0:c524a894b5e8 93 memcpy(ptr, topic.data.long_.name, topic.data.long_.len);
icraggs 0:c524a894b5e8 94 ptr += topic.data.long_.len;
icraggs 0:c524a894b5e8 95 }
icraggs 0:c524a894b5e8 96 memcpy(ptr, payload, payloadlen);
icraggs 0:c524a894b5e8 97 ptr += payloadlen;
icraggs 0:c524a894b5e8 98
icraggs 0:c524a894b5e8 99 rc = ptr - buf;
icraggs 0:c524a894b5e8 100 exit:
icraggs 0:c524a894b5e8 101 FUNC_EXIT_RC(rc);
icraggs 0:c524a894b5e8 102 return rc;
icraggs 0:c524a894b5e8 103 }
icraggs 0:c524a894b5e8 104
icraggs 0:c524a894b5e8 105
icraggs 0:c524a894b5e8 106 int MQTTSNSerialize_puback(unsigned char* buf, int buflen, unsigned short topicid, unsigned short packetid,
icraggs 0:c524a894b5e8 107 unsigned char returncode)
icraggs 0:c524a894b5e8 108 {
icraggs 0:c524a894b5e8 109 unsigned char *ptr = buf;
icraggs 0:c524a894b5e8 110 int len = 0;
icraggs 0:c524a894b5e8 111 int rc = 0;
icraggs 0:c524a894b5e8 112
icraggs 0:c524a894b5e8 113 FUNC_ENTRY;
icraggs 0:c524a894b5e8 114 if ((len = MQTTSNPacket_len(6)) > buflen)
icraggs 0:c524a894b5e8 115 {
icraggs 0:c524a894b5e8 116 rc = MQTTSNPACKET_BUFFER_TOO_SHORT;
icraggs 0:c524a894b5e8 117 goto exit;
icraggs 0:c524a894b5e8 118 }
icraggs 0:c524a894b5e8 119 ptr += MQTTSNPacket_encode(ptr, len); /* write length */
icraggs 0:c524a894b5e8 120 writeChar(&ptr, MQTTSN_PUBACK); /* write message type */
icraggs 0:c524a894b5e8 121
icraggs 0:c524a894b5e8 122 writeInt(&ptr, topicid);
icraggs 0:c524a894b5e8 123 writeInt(&ptr, packetid);
icraggs 0:c524a894b5e8 124 writeChar(&ptr, returncode);
icraggs 0:c524a894b5e8 125
icraggs 0:c524a894b5e8 126 rc = ptr - buf;
icraggs 0:c524a894b5e8 127 exit:
icraggs 0:c524a894b5e8 128 FUNC_EXIT_RC(rc);
icraggs 0:c524a894b5e8 129 return rc;
icraggs 0:c524a894b5e8 130
icraggs 0:c524a894b5e8 131 }
icraggs 0:c524a894b5e8 132
icraggs 0:c524a894b5e8 133
icraggs 0:c524a894b5e8 134
icraggs 0:c524a894b5e8 135 /**
icraggs 0:c524a894b5e8 136 * Serializes the ack packet into the supplied buffer.
icraggs 0:c524a894b5e8 137 * @param buf the buffer into which the packet will be serialized
icraggs 0:c524a894b5e8 138 * @param buflen the length in bytes of the supplied buffer
icraggs 0:c524a894b5e8 139 * @param type the MQTT-SN packet type
icraggs 0:c524a894b5e8 140 * @param packetid the MQTT-SN packet identifier
icraggs 0:c524a894b5e8 141 * @return serialized length, or error if 0
icraggs 0:c524a894b5e8 142 */
icraggs 0:c524a894b5e8 143 int MQTTSNSerialize_ack(unsigned char* buf, int buflen, unsigned short packet_type, unsigned short packetid)
icraggs 0:c524a894b5e8 144 {
icraggs 0:c524a894b5e8 145 int rc = 0;
icraggs 0:c524a894b5e8 146 unsigned char *ptr = buf;
icraggs 0:c524a894b5e8 147 int len = 4; /* ack packet length */
icraggs 0:c524a894b5e8 148
icraggs 0:c524a894b5e8 149 FUNC_ENTRY;
icraggs 0:c524a894b5e8 150 if (len > buflen)
icraggs 0:c524a894b5e8 151 {
icraggs 0:c524a894b5e8 152 rc = MQTTSNPACKET_BUFFER_TOO_SHORT;
icraggs 0:c524a894b5e8 153 goto exit;
icraggs 0:c524a894b5e8 154 }
icraggs 0:c524a894b5e8 155 ptr += MQTTSNPacket_encode(ptr, len); /* write length */
icraggs 0:c524a894b5e8 156 writeChar(&ptr, packet_type); /* write packet type */
icraggs 0:c524a894b5e8 157
icraggs 0:c524a894b5e8 158 writeInt(&ptr, packetid);
icraggs 0:c524a894b5e8 159
icraggs 0:c524a894b5e8 160 rc = ptr - buf;
icraggs 0:c524a894b5e8 161 exit:
icraggs 0:c524a894b5e8 162 FUNC_EXIT_RC(rc);
icraggs 0:c524a894b5e8 163 return rc;
icraggs 0:c524a894b5e8 164 }
icraggs 0:c524a894b5e8 165
icraggs 0:c524a894b5e8 166
icraggs 0:c524a894b5e8 167 /**
icraggs 0:c524a894b5e8 168 * Serializes a puback packet into the supplied buffer.
icraggs 0:c524a894b5e8 169 * @param buf the buffer into which the packet will be serialized
icraggs 0:c524a894b5e8 170 * @param buflen the length in bytes of the supplied buffer
icraggs 0:c524a894b5e8 171 * @param packetid integer - the MQTT packet identifier
icraggs 0:c524a894b5e8 172 * @return serialized length, or error if 0
icraggs 0:c524a894b5e8 173 */
icraggs 0:c524a894b5e8 174 int MQTTSNSerialize_pubrec(unsigned char* buf, int buflen, unsigned short packetid)
icraggs 0:c524a894b5e8 175 {
icraggs 0:c524a894b5e8 176 return MQTTSNSerialize_ack(buf, buflen, MQTTSN_PUBREC, packetid);
icraggs 0:c524a894b5e8 177 }
icraggs 0:c524a894b5e8 178
icraggs 0:c524a894b5e8 179
icraggs 0:c524a894b5e8 180 /**
icraggs 0:c524a894b5e8 181 * Serializes a pubrel packet into the supplied buffer.
icraggs 0:c524a894b5e8 182 * @param buf the buffer into which the packet will be serialized
icraggs 0:c524a894b5e8 183 * @param buflen the length in bytes of the supplied buffer
icraggs 0:c524a894b5e8 184 * @param dup integer - the MQTT dup flag
icraggs 0:c524a894b5e8 185 * @param packetid integer - the MQTT packet identifier
icraggs 0:c524a894b5e8 186 * @return serialized length, or error if 0
icraggs 0:c524a894b5e8 187 */
icraggs 0:c524a894b5e8 188 int MQTTSNSerialize_pubrel(unsigned char* buf, int buflen, unsigned short packetid)
icraggs 0:c524a894b5e8 189 {
icraggs 0:c524a894b5e8 190 return MQTTSNSerialize_ack(buf, buflen, MQTTSN_PUBREL, packetid);
icraggs 0:c524a894b5e8 191 }
icraggs 0:c524a894b5e8 192
icraggs 0:c524a894b5e8 193
icraggs 0:c524a894b5e8 194 /**
icraggs 0:c524a894b5e8 195 * Serializes a pubrel packet into the supplied buffer.
icraggs 0:c524a894b5e8 196 * @param buf the buffer into which the packet will be serialized
icraggs 0:c524a894b5e8 197 * @param buflen the length in bytes of the supplied buffer
icraggs 0:c524a894b5e8 198 * @param packetid integer - the MQTT packet identifier
icraggs 0:c524a894b5e8 199 * @return serialized length, or error if 0
icraggs 0:c524a894b5e8 200 */
icraggs 0:c524a894b5e8 201 int MQTTSNSerialize_pubcomp(unsigned char* buf, int buflen, unsigned short packetid)
icraggs 0:c524a894b5e8 202 {
icraggs 0:c524a894b5e8 203 return MQTTSNSerialize_ack(buf, buflen, MQTTSN_PUBCOMP, packetid);
icraggs 0:c524a894b5e8 204 }
icraggs 0:c524a894b5e8 205
icraggs 0:c524a894b5e8 206
icraggs 0:c524a894b5e8 207 /**
icraggs 0:c524a894b5e8 208 * Determines the length of the MQTT register packet that would be produced using the supplied parameters
icraggs 0:c524a894b5e8 209 * @param topicnamelen the length of the topic name to be used in the register
icraggs 0:c524a894b5e8 210 * @return the length of buffer needed to contain the serialized version of the packet
icraggs 0:c524a894b5e8 211 */
icraggs 0:c524a894b5e8 212 int MQTTSNSerialize_registerLength(int topicnamelen)
icraggs 0:c524a894b5e8 213 {
icraggs 0:c524a894b5e8 214 return topicnamelen + 5;
icraggs 0:c524a894b5e8 215 }
icraggs 0:c524a894b5e8 216
icraggs 0:c524a894b5e8 217 /**
icraggs 0:c524a894b5e8 218 * Serializes the supplied register data into the supplied buffer, ready for sending
icraggs 0:c524a894b5e8 219 * @param buf the buffer into which the packet will be serialized
icraggs 0:c524a894b5e8 220 * @param buflen the length in bytes of the supplied buffer
icraggs 0:c524a894b5e8 221 * @param topicid if sent by a gateway, contains the id for the topicname, otherwise 0
icraggs 0:c524a894b5e8 222 * @param packetid integer - the MQTT packet identifier
icraggs 0:c524a894b5e8 223 * @param topicname null-terminated topic name string
icraggs 0:c524a894b5e8 224 * @return the length of the serialized data. <= 0 indicates error
icraggs 0:c524a894b5e8 225 */
icraggs 0:c524a894b5e8 226 int MQTTSNSerialize_register(unsigned char* buf, int buflen, unsigned short topicid, unsigned short packetid,
icraggs 0:c524a894b5e8 227 MQTTSNString* topicname)
icraggs 0:c524a894b5e8 228 {
icraggs 0:c524a894b5e8 229 unsigned char *ptr = buf;
icraggs 0:c524a894b5e8 230 int len = 0;
icraggs 0:c524a894b5e8 231 int rc = 0;
icraggs 0:c524a894b5e8 232 int topicnamelen = 0;
icraggs 0:c524a894b5e8 233
icraggs 0:c524a894b5e8 234 FUNC_ENTRY;
icraggs 0:c524a894b5e8 235 topicnamelen = (topicname->cstring) ? strlen(topicname->cstring) : topicname->lenstring.len;
icraggs 0:c524a894b5e8 236 if ((len = MQTTSNPacket_len(MQTTSNSerialize_registerLength(topicnamelen))) > buflen)
icraggs 0:c524a894b5e8 237 {
icraggs 0:c524a894b5e8 238 rc = MQTTSNPACKET_BUFFER_TOO_SHORT;
icraggs 0:c524a894b5e8 239 goto exit;
icraggs 0:c524a894b5e8 240 }
icraggs 0:c524a894b5e8 241 ptr += MQTTSNPacket_encode(ptr, len); /* write length */
icraggs 0:c524a894b5e8 242 writeChar(&ptr, MQTTSN_REGISTER); /* write message type */
icraggs 0:c524a894b5e8 243
icraggs 0:c524a894b5e8 244 writeInt(&ptr, topicid);
icraggs 0:c524a894b5e8 245 writeInt(&ptr, packetid);
icraggs 0:c524a894b5e8 246
icraggs 0:c524a894b5e8 247 memcpy(ptr, (topicname->cstring) ? topicname->cstring : topicname->lenstring.data, topicnamelen);
icraggs 0:c524a894b5e8 248 ptr += topicnamelen;
icraggs 0:c524a894b5e8 249
icraggs 0:c524a894b5e8 250 rc = ptr - buf;
icraggs 0:c524a894b5e8 251 exit:
icraggs 0:c524a894b5e8 252 FUNC_EXIT_RC(rc);
icraggs 0:c524a894b5e8 253 return rc;
icraggs 0:c524a894b5e8 254 }
icraggs 0:c524a894b5e8 255
icraggs 0:c524a894b5e8 256
icraggs 0:c524a894b5e8 257 /**
icraggs 0:c524a894b5e8 258 * Serializes the supplied register data into the supplied buffer, ready for sending
icraggs 0:c524a894b5e8 259 * @param buf the buffer into which the packet will be serialized
icraggs 0:c524a894b5e8 260 * @param buflen the length in bytes of the supplied buffer
icraggs 0:c524a894b5e8 261 * @param topicid if sent by a gateway, contains the id for the topicname, otherwise 0
icraggs 0:c524a894b5e8 262 * @param packetid integer - the MQTT packet identifier
icraggs 0:c524a894b5e8 263 * @param return_code integer return code
icraggs 0:c524a894b5e8 264 * @return the length of the serialized data. <= 0 indicates error
icraggs 0:c524a894b5e8 265 */
icraggs 0:c524a894b5e8 266 int MQTTSNSerialize_regack(unsigned char* buf, int buflen, unsigned short topicid, unsigned short packetid,
icraggs 0:c524a894b5e8 267 unsigned char return_code)
icraggs 0:c524a894b5e8 268 {
icraggs 0:c524a894b5e8 269 unsigned char *ptr = buf;
icraggs 0:c524a894b5e8 270 int len = 0;
icraggs 0:c524a894b5e8 271 int rc = 0;
icraggs 0:c524a894b5e8 272
icraggs 0:c524a894b5e8 273 FUNC_ENTRY;
icraggs 0:c524a894b5e8 274 if ((len = MQTTSNPacket_len(6)) > buflen)
icraggs 0:c524a894b5e8 275 {
icraggs 0:c524a894b5e8 276 rc = MQTTSNPACKET_BUFFER_TOO_SHORT;
icraggs 0:c524a894b5e8 277 goto exit;
icraggs 0:c524a894b5e8 278 }
icraggs 0:c524a894b5e8 279 ptr += MQTTSNPacket_encode(ptr, len); /* write length */
icraggs 0:c524a894b5e8 280 writeChar(&ptr, MQTTSN_REGACK); /* write message type */
icraggs 0:c524a894b5e8 281
icraggs 0:c524a894b5e8 282 writeInt(&ptr, topicid);
icraggs 0:c524a894b5e8 283 writeInt(&ptr, packetid);
icraggs 0:c524a894b5e8 284 writeChar(&ptr, return_code);
icraggs 0:c524a894b5e8 285
icraggs 0:c524a894b5e8 286 rc = ptr - buf;
icraggs 0:c524a894b5e8 287 exit:
icraggs 0:c524a894b5e8 288 FUNC_EXIT_RC(rc);
icraggs 0:c524a894b5e8 289 return rc;
icraggs 0:c524a894b5e8 290 }
icraggs 0:c524a894b5e8 291