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:
Wed Jan 06 14:19:27 2016 +0000
Revision:
1:7fa362fa563f
Parent:
0:c524a894b5e8
Internal function name changes to avoid name clashes

Who changed what in which revision?

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