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 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 "StackTrace.h"
icraggs 0:c524a894b5e8 18 #include "MQTTSNPacket.h"
icraggs 0:c524a894b5e8 19 #include <string.h>
icraggs 0:c524a894b5e8 20
icraggs 0:c524a894b5e8 21 #define min(a, b) ((a < b) ? 1 : 0)
icraggs 0:c524a894b5e8 22
icraggs 0:c524a894b5e8 23
icraggs 0:c524a894b5e8 24 /**
icraggs 0:c524a894b5e8 25 * Deserializes the supplied (wire) buffer into connect data structure
icraggs 0:c524a894b5e8 26 * @param data the connect data structure to be filled out
icraggs 0:c524a894b5e8 27 * @param buf the raw buffer data, of the correct length determined by the remaining length field
icraggs 0:c524a894b5e8 28 * @param len the length in bytes of the data in the supplied buffer
icraggs 0:c524a894b5e8 29 * @return error code. 1 is success, 0 is failure
icraggs 0:c524a894b5e8 30 */
icraggs 0:c524a894b5e8 31 int MQTTSNDeserialize_connect(MQTTSNPacket_connectData* data, unsigned char* buf, int len)
icraggs 0:c524a894b5e8 32 {
icraggs 0:c524a894b5e8 33 MQTTSNFlags flags = {0};
icraggs 0:c524a894b5e8 34 unsigned char* curdata = buf;
icraggs 0:c524a894b5e8 35 unsigned char* enddata = &buf[len];
icraggs 0:c524a894b5e8 36 int rc = 0;
icraggs 0:c524a894b5e8 37 int version = 0;
icraggs 0:c524a894b5e8 38 int mylen = 0;
icraggs 0:c524a894b5e8 39
icraggs 0:c524a894b5e8 40 FUNC_ENTRY;
icraggs 0:c524a894b5e8 41 curdata += (rc = MQTTSNPacket_decode(curdata, len, &mylen)); /* read length */
icraggs 0:c524a894b5e8 42 enddata = buf + mylen;
icraggs 0:c524a894b5e8 43 if (enddata - curdata < 2)
icraggs 0:c524a894b5e8 44 goto exit;
icraggs 0:c524a894b5e8 45
icraggs 1:7fa362fa563f 46 if (MQTTSNPacket_readChar(&curdata) != MQTTSN_CONNECT)
icraggs 0:c524a894b5e8 47 goto exit;
icraggs 0:c524a894b5e8 48
icraggs 1:7fa362fa563f 49 flags.all = MQTTSNPacket_readChar(&curdata);
icraggs 0:c524a894b5e8 50 data->cleansession = flags.bits.cleanSession;
icraggs 0:c524a894b5e8 51 data->willFlag = flags.bits.will;
icraggs 0:c524a894b5e8 52
icraggs 1:7fa362fa563f 53 if ((version = (int)MQTTSNPacket_readChar(&curdata)) != 1) /* Protocol version */
icraggs 0:c524a894b5e8 54 goto exit;
icraggs 0:c524a894b5e8 55
icraggs 1:7fa362fa563f 56 data->duration = MQTTSNPacket_readInt(&curdata);
icraggs 0:c524a894b5e8 57
icraggs 0:c524a894b5e8 58 if (!readMQTTSNString(&data->clientID, &curdata, enddata))
icraggs 0:c524a894b5e8 59 goto exit;
icraggs 0:c524a894b5e8 60
icraggs 0:c524a894b5e8 61 rc = 1;
icraggs 0:c524a894b5e8 62 exit:
icraggs 0:c524a894b5e8 63 FUNC_EXIT_RC(rc);
icraggs 0:c524a894b5e8 64 return rc;
icraggs 0:c524a894b5e8 65 }
icraggs 0:c524a894b5e8 66
icraggs 0:c524a894b5e8 67
icraggs 0:c524a894b5e8 68 /**
icraggs 0:c524a894b5e8 69 * Serializes the connack packet into the supplied buffer.
icraggs 0:c524a894b5e8 70 * @param buf the buffer into which the packet will be serialized
icraggs 0:c524a894b5e8 71 * @param buflen the length in bytes of the supplied buffer
icraggs 0:c524a894b5e8 72 * @param connack_rc the integer connack return code to be used
icraggs 0:c524a894b5e8 73 * @return serialized length, or error if 0
icraggs 0:c524a894b5e8 74 */
icraggs 0:c524a894b5e8 75 int MQTTSNSerialize_connack(unsigned char* buf, int buflen, int connack_rc)
icraggs 0:c524a894b5e8 76 {
icraggs 0:c524a894b5e8 77 int rc = 0;
icraggs 0:c524a894b5e8 78 unsigned char *ptr = buf;
icraggs 0:c524a894b5e8 79
icraggs 0:c524a894b5e8 80 FUNC_ENTRY;
icraggs 0:c524a894b5e8 81 if (buflen < 3)
icraggs 0:c524a894b5e8 82 {
icraggs 0:c524a894b5e8 83 rc = MQTTSNPACKET_BUFFER_TOO_SHORT;
icraggs 0:c524a894b5e8 84 goto exit;
icraggs 0:c524a894b5e8 85 }
icraggs 0:c524a894b5e8 86
icraggs 0:c524a894b5e8 87 ptr += MQTTSNPacket_encode(ptr, 3); /* write length */
icraggs 1:7fa362fa563f 88 MQTTSNPacket_writeChar(&ptr, MQTTSN_CONNACK);
icraggs 1:7fa362fa563f 89 MQTTSNPacket_writeChar(&ptr, connack_rc);
icraggs 0:c524a894b5e8 90
icraggs 0:c524a894b5e8 91 rc = ptr - buf;
icraggs 0:c524a894b5e8 92 exit:
icraggs 0:c524a894b5e8 93 FUNC_EXIT_RC(rc);
icraggs 0:c524a894b5e8 94 return rc;
icraggs 0:c524a894b5e8 95 }
icraggs 0:c524a894b5e8 96
icraggs 0:c524a894b5e8 97
icraggs 0:c524a894b5e8 98 /**
icraggs 0:c524a894b5e8 99 * Deserializes the supplied (wire) buffer into disconnect data - optional duration
icraggs 0:c524a894b5e8 100 * @param duration returned integer value of the duration field, -1 if no duration was specified
icraggs 0:c524a894b5e8 101 * @param buf the raw buffer data, of the correct length determined by the remaining length field
icraggs 0:c524a894b5e8 102 * @param len the length in bytes of the data in the supplied buffer
icraggs 0:c524a894b5e8 103 * @return error code. 1 is success, 0 is failure
icraggs 0:c524a894b5e8 104 */
icraggs 0:c524a894b5e8 105 int MQTTSNDeserialize_disconnect(int* duration, unsigned char* buf, int buflen)
icraggs 0:c524a894b5e8 106 {
icraggs 0:c524a894b5e8 107 unsigned char* curdata = buf;
icraggs 0:c524a894b5e8 108 unsigned char* enddata = NULL;
icraggs 0:c524a894b5e8 109 int rc = -1;
icraggs 0:c524a894b5e8 110 int mylen;
icraggs 0:c524a894b5e8 111
icraggs 0:c524a894b5e8 112 FUNC_ENTRY;
icraggs 0:c524a894b5e8 113 curdata += (rc = MQTTSNPacket_decode(curdata, buflen, &mylen)); /* read length */
icraggs 0:c524a894b5e8 114 enddata = buf + mylen;
icraggs 0:c524a894b5e8 115 if (enddata - curdata < 1)
icraggs 0:c524a894b5e8 116 goto exit;
icraggs 0:c524a894b5e8 117
icraggs 1:7fa362fa563f 118 if (MQTTSNPacket_readChar(&curdata) != MQTTSN_DISCONNECT)
icraggs 0:c524a894b5e8 119 goto exit;
icraggs 0:c524a894b5e8 120
icraggs 0:c524a894b5e8 121 if (enddata - curdata == 2)
icraggs 1:7fa362fa563f 122 *duration = MQTTSNPacket_readInt(&curdata);
icraggs 0:c524a894b5e8 123 else if (enddata != curdata)
icraggs 0:c524a894b5e8 124 goto exit;
icraggs 0:c524a894b5e8 125
icraggs 0:c524a894b5e8 126 rc = 1;
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 * Serializes a willtopicreq packet into the supplied buffer.
icraggs 0:c524a894b5e8 135 * @param buf the buffer into which the packet will be serialized
icraggs 0:c524a894b5e8 136 * @param buflen the length in bytes of the supplied buffer
icraggs 0:c524a894b5e8 137 * @return serialized length, or error if 0
icraggs 0:c524a894b5e8 138 */
icraggs 0:c524a894b5e8 139 int MQTTSNSerialize_willtopicreq(unsigned char* buf, int buflen)
icraggs 0:c524a894b5e8 140 {
icraggs 0:c524a894b5e8 141 int rc = 0;
icraggs 0:c524a894b5e8 142 unsigned char *ptr = buf;
icraggs 0:c524a894b5e8 143
icraggs 0:c524a894b5e8 144 FUNC_ENTRY;
icraggs 0:c524a894b5e8 145 if (buflen < 2)
icraggs 0:c524a894b5e8 146 {
icraggs 0:c524a894b5e8 147 rc = MQTTSNPACKET_BUFFER_TOO_SHORT;
icraggs 0:c524a894b5e8 148 goto exit;
icraggs 0:c524a894b5e8 149 }
icraggs 0:c524a894b5e8 150
icraggs 0:c524a894b5e8 151 ptr += MQTTSNPacket_encode(ptr, 2); /* write length */
icraggs 1:7fa362fa563f 152 MQTTSNPacket_writeChar(&ptr, MQTTSN_WILLTOPICREQ);
icraggs 0:c524a894b5e8 153
icraggs 0:c524a894b5e8 154 rc = ptr - buf;
icraggs 0:c524a894b5e8 155 exit:
icraggs 0:c524a894b5e8 156 FUNC_EXIT_RC(rc);
icraggs 0:c524a894b5e8 157 return rc;
icraggs 0:c524a894b5e8 158 }
icraggs 0:c524a894b5e8 159
icraggs 0:c524a894b5e8 160
icraggs 0:c524a894b5e8 161 /**
icraggs 0:c524a894b5e8 162 * Serializes a willmsgreq packet into the supplied buffer.
icraggs 0:c524a894b5e8 163 * @param buf the buffer into which the packet will be serialized
icraggs 0:c524a894b5e8 164 * @param buflen the length in bytes of the supplied buffer
icraggs 0:c524a894b5e8 165 * @return serialized length, or error if 0
icraggs 0:c524a894b5e8 166 */
icraggs 0:c524a894b5e8 167 int MQTTSNSerialize_willmsgreq(unsigned char* buf, int buflen)
icraggs 0:c524a894b5e8 168 {
icraggs 0:c524a894b5e8 169 int rc = 0;
icraggs 0:c524a894b5e8 170 unsigned char *ptr = buf;
icraggs 0:c524a894b5e8 171
icraggs 0:c524a894b5e8 172 FUNC_ENTRY;
icraggs 0:c524a894b5e8 173 if (buflen < 2)
icraggs 0:c524a894b5e8 174 {
icraggs 0:c524a894b5e8 175 rc = MQTTSNPACKET_BUFFER_TOO_SHORT;
icraggs 0:c524a894b5e8 176 goto exit;
icraggs 0:c524a894b5e8 177 }
icraggs 0:c524a894b5e8 178
icraggs 0:c524a894b5e8 179 ptr += MQTTSNPacket_encode(ptr, 2); /* write length */
icraggs 1:7fa362fa563f 180 MQTTSNPacket_writeChar(&ptr, MQTTSN_WILLMSGREQ);
icraggs 0:c524a894b5e8 181
icraggs 0:c524a894b5e8 182 rc = ptr - buf;
icraggs 0:c524a894b5e8 183 exit:
icraggs 0:c524a894b5e8 184 FUNC_EXIT_RC(rc);
icraggs 0:c524a894b5e8 185 return rc;
icraggs 0:c524a894b5e8 186 }
icraggs 0:c524a894b5e8 187
icraggs 0:c524a894b5e8 188
icraggs 0:c524a894b5e8 189
icraggs 0:c524a894b5e8 190 /**
icraggs 0:c524a894b5e8 191 * Deserializes the supplied (wire) buffer into pingreq data
icraggs 0:c524a894b5e8 192 * @param clientID the connect data structure to be filled out
icraggs 0:c524a894b5e8 193 * @param buf the raw buffer data, of the correct length determined by the remaining length field
icraggs 0:c524a894b5e8 194 * @param len the length in bytes of the data in the supplied buffer
icraggs 0:c524a894b5e8 195 * @return error code. 1 is success, 0 is failure
icraggs 0:c524a894b5e8 196 */
icraggs 0:c524a894b5e8 197 int MQTTSNDeserialize_pingreq(MQTTSNString* clientID, unsigned char* buf, int len)
icraggs 0:c524a894b5e8 198 {
icraggs 0:c524a894b5e8 199 unsigned char* curdata = buf;
icraggs 0:c524a894b5e8 200 unsigned char* enddata = &buf[len];
icraggs 0:c524a894b5e8 201 int rc = 0;
icraggs 0:c524a894b5e8 202 int mylen = 0;
icraggs 0:c524a894b5e8 203
icraggs 0:c524a894b5e8 204 FUNC_ENTRY;
icraggs 0:c524a894b5e8 205 curdata += (rc = MQTTSNPacket_decode(curdata, len, &mylen)); /* read length */
icraggs 0:c524a894b5e8 206 enddata = buf + mylen;
icraggs 0:c524a894b5e8 207 if (enddata - curdata < 1)
icraggs 0:c524a894b5e8 208 goto exit;
icraggs 0:c524a894b5e8 209
icraggs 1:7fa362fa563f 210 if (MQTTSNPacket_readChar(&curdata) != MQTTSN_PINGREQ)
icraggs 0:c524a894b5e8 211 goto exit;
icraggs 0:c524a894b5e8 212
icraggs 0:c524a894b5e8 213 if (!readMQTTSNString(clientID, &curdata, enddata))
icraggs 0:c524a894b5e8 214 goto exit;
icraggs 0:c524a894b5e8 215
icraggs 0:c524a894b5e8 216 rc = 1;
icraggs 0:c524a894b5e8 217 exit:
icraggs 0:c524a894b5e8 218 FUNC_EXIT_RC(rc);
icraggs 0:c524a894b5e8 219 return rc;
icraggs 0:c524a894b5e8 220 }
icraggs 0:c524a894b5e8 221
icraggs 0:c524a894b5e8 222
icraggs 0:c524a894b5e8 223 /**
icraggs 0:c524a894b5e8 224 * Serializes a pingresp packet into the supplied buffer.
icraggs 0:c524a894b5e8 225 * @param buf the buffer into which the packet will be serialized
icraggs 0:c524a894b5e8 226 * @param buflen the length in bytes of the supplied buffer
icraggs 0:c524a894b5e8 227 * @return serialized length, or error if 0
icraggs 0:c524a894b5e8 228 */
icraggs 0:c524a894b5e8 229 int MQTTSNSerialize_pingresp(unsigned char* buf, int buflen)
icraggs 0:c524a894b5e8 230 {
icraggs 0:c524a894b5e8 231 int rc = 0;
icraggs 0:c524a894b5e8 232 unsigned char *ptr = buf;
icraggs 0:c524a894b5e8 233
icraggs 0:c524a894b5e8 234 FUNC_ENTRY;
icraggs 0:c524a894b5e8 235 if (buflen < 2)
icraggs 0:c524a894b5e8 236 {
icraggs 0:c524a894b5e8 237 rc = MQTTSNPACKET_BUFFER_TOO_SHORT;
icraggs 0:c524a894b5e8 238 goto exit;
icraggs 0:c524a894b5e8 239 }
icraggs 0:c524a894b5e8 240
icraggs 0:c524a894b5e8 241 ptr += MQTTSNPacket_encode(ptr, 2); /* write length */
icraggs 1:7fa362fa563f 242 MQTTSNPacket_writeChar(&ptr, MQTTSN_PINGRESP);
icraggs 0:c524a894b5e8 243
icraggs 0:c524a894b5e8 244 rc = ptr - buf;
icraggs 0:c524a894b5e8 245 exit:
icraggs 0:c524a894b5e8 246 FUNC_EXIT_RC(rc);
icraggs 0:c524a894b5e8 247 return rc;
icraggs 0:c524a894b5e8 248 }
icraggs 0:c524a894b5e8 249
icraggs 0:c524a894b5e8 250
icraggs 0:c524a894b5e8 251 /**
icraggs 0:c524a894b5e8 252 * Deserializes the supplied (wire) buffer into willtopic or willtopicupd data structure
icraggs 0:c524a894b5e8 253 * @param data the connect data structure to be filled out
icraggs 0:c524a894b5e8 254 * @param buf the raw buffer data, of the correct length determined by the remaining length field
icraggs 0:c524a894b5e8 255 * @param len the length in bytes of the data in the supplied buffer
icraggs 0:c524a894b5e8 256 * @return error code. 1 is success, 0 is failure
icraggs 0:c524a894b5e8 257 */
icraggs 0:c524a894b5e8 258 int MQTTSNDeserialize_willtopic1(int *willQoS, unsigned char *willRetain, MQTTSNString* willTopic, unsigned char* buf, int len,
icraggs 0:c524a894b5e8 259 enum MQTTSN_msgTypes packet_type)
icraggs 0:c524a894b5e8 260 {
icraggs 0:c524a894b5e8 261 MQTTSNFlags flags = {0};
icraggs 0:c524a894b5e8 262 unsigned char* curdata = buf;
icraggs 0:c524a894b5e8 263 unsigned char* enddata = &buf[len];
icraggs 0:c524a894b5e8 264 int rc = 0;
icraggs 0:c524a894b5e8 265 int mylen = 0;
icraggs 0:c524a894b5e8 266
icraggs 0:c524a894b5e8 267 FUNC_ENTRY;
icraggs 0:c524a894b5e8 268 curdata += (rc = MQTTSNPacket_decode(curdata, len, &mylen)); /* read length */
icraggs 0:c524a894b5e8 269 enddata = buf + mylen;
icraggs 0:c524a894b5e8 270 if (enddata > buf + len)
icraggs 0:c524a894b5e8 271 goto exit;
icraggs 0:c524a894b5e8 272
icraggs 1:7fa362fa563f 273 if (MQTTSNPacket_readChar(&curdata) != packet_type)
icraggs 0:c524a894b5e8 274 goto exit;
icraggs 0:c524a894b5e8 275
icraggs 1:7fa362fa563f 276 flags.all = MQTTSNPacket_readChar(&curdata);
icraggs 0:c524a894b5e8 277 *willQoS = flags.bits.QoS;
icraggs 0:c524a894b5e8 278 *willRetain = flags.bits.retain;
icraggs 0:c524a894b5e8 279
icraggs 0:c524a894b5e8 280 if (!readMQTTSNString(willTopic, &curdata, enddata))
icraggs 0:c524a894b5e8 281 goto exit;
icraggs 0:c524a894b5e8 282
icraggs 0:c524a894b5e8 283 rc = 1;
icraggs 0:c524a894b5e8 284 exit:
icraggs 0:c524a894b5e8 285 FUNC_EXIT_RC(rc);
icraggs 0:c524a894b5e8 286 return rc;
icraggs 0:c524a894b5e8 287 }
icraggs 0:c524a894b5e8 288
icraggs 0:c524a894b5e8 289
icraggs 0:c524a894b5e8 290 /**
icraggs 0:c524a894b5e8 291 * Deserializes the supplied (wire) buffer into willtopic data structure
icraggs 0:c524a894b5e8 292 * @param data the connect data structure to be filled out
icraggs 0:c524a894b5e8 293 * @param buf the raw buffer data, of the correct length determined by the remaining length field
icraggs 0:c524a894b5e8 294 * @param len the length in bytes of the data in the supplied buffer
icraggs 0:c524a894b5e8 295 * @return error code. 1 is success, 0 is failure
icraggs 0:c524a894b5e8 296 */
icraggs 0:c524a894b5e8 297 int MQTTSNDeserialize_willtopic(int *willQoS, unsigned char *willRetain, MQTTSNString* willTopic, unsigned char* buf, int len)
icraggs 0:c524a894b5e8 298 {
icraggs 0:c524a894b5e8 299 return MQTTSNDeserialize_willtopic1(willQoS, willRetain, willTopic, buf, len, MQTTSN_WILLTOPIC);
icraggs 0:c524a894b5e8 300 }
icraggs 0:c524a894b5e8 301
icraggs 0:c524a894b5e8 302 /**
icraggs 0:c524a894b5e8 303 * Deserializes the supplied (wire) buffer into willtopic data structure
icraggs 0:c524a894b5e8 304 * @param data the connect data structure to be filled out
icraggs 0:c524a894b5e8 305 * @param buf the raw buffer data, of the correct length determined by the remaining length field
icraggs 0:c524a894b5e8 306 * @param len the length in bytes of the data in the supplied buffer
icraggs 0:c524a894b5e8 307 * @return error code. 1 is success, 0 is failure
icraggs 0:c524a894b5e8 308 */
icraggs 0:c524a894b5e8 309 int MQTTSNDeserialize_willtopicupd(int *willQoS, unsigned char *willRetain, MQTTSNString* willTopic, unsigned char* buf, int len)
icraggs 0:c524a894b5e8 310 {
icraggs 0:c524a894b5e8 311 return MQTTSNDeserialize_willtopic1(willQoS, willRetain, willTopic, buf, len, MQTTSN_WILLTOPICUPD);
icraggs 0:c524a894b5e8 312 }
icraggs 0:c524a894b5e8 313
icraggs 0:c524a894b5e8 314
icraggs 0:c524a894b5e8 315 /**
icraggs 0:c524a894b5e8 316 * Deserializes the supplied (wire) buffer into willmsg or willmsgupd data
icraggs 0:c524a894b5e8 317 * @param willMsg the will message to be retrieved
icraggs 0:c524a894b5e8 318 * @param buf the raw buffer data, of the correct length determined by the remaining length field
icraggs 0:c524a894b5e8 319 * @param len the length in bytes of the data in the supplied buffer
icraggs 0:c524a894b5e8 320 * @return error code. 1 is success, 0 is failure
icraggs 0:c524a894b5e8 321 */
icraggs 0:c524a894b5e8 322 int MQTTSNDeserialize_willmsg1(MQTTSNString* willMsg, unsigned char* buf, int len, enum MQTTSN_msgTypes packet_type)
icraggs 0:c524a894b5e8 323 {
icraggs 0:c524a894b5e8 324 unsigned char* curdata = buf;
icraggs 0:c524a894b5e8 325 unsigned char* enddata = &buf[len];
icraggs 0:c524a894b5e8 326 int rc = 0;
icraggs 0:c524a894b5e8 327 int mylen = 0;
icraggs 0:c524a894b5e8 328
icraggs 0:c524a894b5e8 329 FUNC_ENTRY;
icraggs 0:c524a894b5e8 330 curdata += (rc = MQTTSNPacket_decode(curdata, len, &mylen)); /* read length */
icraggs 0:c524a894b5e8 331 enddata = buf + mylen;
icraggs 0:c524a894b5e8 332 if (enddata > buf + len)
icraggs 0:c524a894b5e8 333 goto exit;
icraggs 0:c524a894b5e8 334
icraggs 1:7fa362fa563f 335 if (MQTTSNPacket_readChar(&curdata) != packet_type)
icraggs 0:c524a894b5e8 336 goto exit;
icraggs 0:c524a894b5e8 337
icraggs 0:c524a894b5e8 338 if (!readMQTTSNString(willMsg, &curdata, enddata))
icraggs 0:c524a894b5e8 339 goto exit;
icraggs 0:c524a894b5e8 340
icraggs 0:c524a894b5e8 341 rc = 1;
icraggs 0:c524a894b5e8 342 exit:
icraggs 0:c524a894b5e8 343 FUNC_EXIT_RC(rc);
icraggs 0:c524a894b5e8 344 return rc;
icraggs 0:c524a894b5e8 345 }
icraggs 0:c524a894b5e8 346
icraggs 0:c524a894b5e8 347
icraggs 0:c524a894b5e8 348 /**
icraggs 0:c524a894b5e8 349 * Deserializes the supplied (wire) buffer into willmsg data
icraggs 0:c524a894b5e8 350 * @param willMsg the will message to be retrieved
icraggs 0:c524a894b5e8 351 * @param buf the raw buffer data, of the correct length determined by the remaining length field
icraggs 0:c524a894b5e8 352 * @param len the length in bytes of the data in the supplied buffer
icraggs 0:c524a894b5e8 353 * @return error code. 1 is success, 0 is failure
icraggs 0:c524a894b5e8 354 */
icraggs 0:c524a894b5e8 355 int MQTTSNDeserialize_willmsg(MQTTSNString* willMsg, unsigned char* buf, int len)
icraggs 0:c524a894b5e8 356 {
icraggs 0:c524a894b5e8 357 return MQTTSNDeserialize_willmsg1(willMsg, buf, len, MQTTSN_WILLMSG);
icraggs 0:c524a894b5e8 358 }
icraggs 0:c524a894b5e8 359
icraggs 0:c524a894b5e8 360 /**
icraggs 0:c524a894b5e8 361 * Deserializes the supplied (wire) buffer into willmsgupd data
icraggs 0:c524a894b5e8 362 * @param willMsg the will message to be retrieved
icraggs 0:c524a894b5e8 363 * @param buf the raw buffer data, of the correct length determined by the remaining length field
icraggs 0:c524a894b5e8 364 * @param len the length in bytes of the data in the supplied buffer
icraggs 0:c524a894b5e8 365 * @return error code. 1 is success, 0 is failure
icraggs 0:c524a894b5e8 366 */
icraggs 0:c524a894b5e8 367 int MQTTSNDeserialize_willmsgupd(MQTTSNString* willMsg, unsigned char* buf, int len)
icraggs 0:c524a894b5e8 368 {
icraggs 0:c524a894b5e8 369 return MQTTSNDeserialize_willmsg1(willMsg, buf, len, MQTTSN_WILLMSGUPD);
icraggs 0:c524a894b5e8 370 }
icraggs 0:c524a894b5e8 371
icraggs 0:c524a894b5e8 372
icraggs 0:c524a894b5e8 373 /**
icraggs 0:c524a894b5e8 374 * Serializes the willtopicresp packet into the supplied buffer.
icraggs 0:c524a894b5e8 375 * @param buf the buffer into which the packet will be serialized
icraggs 0:c524a894b5e8 376 * @param buflen the length in bytes of the supplied buffer
icraggs 0:c524a894b5e8 377 * @param rc the integer return code to be used
icraggs 0:c524a894b5e8 378 * @return serialized length, or error if 0
icraggs 0:c524a894b5e8 379 */
icraggs 0:c524a894b5e8 380 int MQTTSNSerialize_willtopicresp(unsigned char* buf, int buflen, int resp_rc)
icraggs 0:c524a894b5e8 381 {
icraggs 0:c524a894b5e8 382 int rc = 0;
icraggs 0:c524a894b5e8 383 unsigned char *ptr = buf;
icraggs 0:c524a894b5e8 384
icraggs 0:c524a894b5e8 385 FUNC_ENTRY;
icraggs 0:c524a894b5e8 386 if (buflen < 3)
icraggs 0:c524a894b5e8 387 {
icraggs 0:c524a894b5e8 388 rc = MQTTSNPACKET_BUFFER_TOO_SHORT;
icraggs 0:c524a894b5e8 389 goto exit;
icraggs 0:c524a894b5e8 390 }
icraggs 0:c524a894b5e8 391
icraggs 0:c524a894b5e8 392 ptr += MQTTSNPacket_encode(ptr, 3); /* write length */
icraggs 1:7fa362fa563f 393 MQTTSNPacket_writeChar(&ptr, MQTTSN_WILLTOPICRESP);
icraggs 1:7fa362fa563f 394 MQTTSNPacket_writeChar(&ptr, resp_rc);
icraggs 0:c524a894b5e8 395
icraggs 0:c524a894b5e8 396 rc = ptr - buf;
icraggs 0:c524a894b5e8 397 exit:
icraggs 0:c524a894b5e8 398 FUNC_EXIT_RC(rc);
icraggs 0:c524a894b5e8 399 return rc;
icraggs 0:c524a894b5e8 400 }
icraggs 0:c524a894b5e8 401
icraggs 0:c524a894b5e8 402
icraggs 0:c524a894b5e8 403 /**
icraggs 0:c524a894b5e8 404 * Serializes the willmsgresp packet into the supplied buffer.
icraggs 0:c524a894b5e8 405 * @param buf the buffer into which the packet will be serialized
icraggs 0:c524a894b5e8 406 * @param buflen the length in bytes of the supplied buffer
icraggs 0:c524a894b5e8 407 * @param rc the integer return code to be used
icraggs 0:c524a894b5e8 408 * @return serialized length, or error if 0
icraggs 0:c524a894b5e8 409 */
icraggs 0:c524a894b5e8 410 int MQTTSNSerialize_willmsgresp(unsigned char* buf, int buflen, int resp_rc)
icraggs 0:c524a894b5e8 411 {
icraggs 0:c524a894b5e8 412 int rc = 0;
icraggs 0:c524a894b5e8 413 unsigned char *ptr = buf;
icraggs 0:c524a894b5e8 414
icraggs 0:c524a894b5e8 415 FUNC_ENTRY;
icraggs 0:c524a894b5e8 416 if (buflen < 3)
icraggs 0:c524a894b5e8 417 {
icraggs 0:c524a894b5e8 418 rc = MQTTSNPACKET_BUFFER_TOO_SHORT;
icraggs 0:c524a894b5e8 419 goto exit;
icraggs 0:c524a894b5e8 420 }
icraggs 0:c524a894b5e8 421
icraggs 0:c524a894b5e8 422 ptr += MQTTSNPacket_encode(ptr, 3); /* write length */
icraggs 1:7fa362fa563f 423 MQTTSNPacket_writeChar(&ptr, MQTTSN_WILLMSGRESP);
icraggs 1:7fa362fa563f 424 MQTTSNPacket_writeChar(&ptr, resp_rc);
icraggs 0:c524a894b5e8 425
icraggs 0:c524a894b5e8 426 rc = ptr - buf;
icraggs 0:c524a894b5e8 427 exit:
icraggs 0:c524a894b5e8 428 FUNC_EXIT_RC(rc);
icraggs 0:c524a894b5e8 429 return rc;
icraggs 0:c524a894b5e8 430 }
icraggs 0:c524a894b5e8 431