Fork of my original MQTTGateway

Dependencies:   mbed-http

Committer:
vpcola
Date:
Sat Apr 08 14:43:14 2017 +0000
Revision:
0:a1734fe1ec4b
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
vpcola 0:a1734fe1ec4b 1 /*******************************************************************************
vpcola 0:a1734fe1ec4b 2 * Copyright (c) 2014 IBM Corp.
vpcola 0:a1734fe1ec4b 3 *
vpcola 0:a1734fe1ec4b 4 * All rights reserved. This program and the accompanying materials
vpcola 0:a1734fe1ec4b 5 * are made available under the terms of the Eclipse Public License v1.0
vpcola 0:a1734fe1ec4b 6 * and Eclipse Distribution License v1.0 which accompany this distribution.
vpcola 0:a1734fe1ec4b 7 *
vpcola 0:a1734fe1ec4b 8 * The Eclipse Public License is available at
vpcola 0:a1734fe1ec4b 9 * http://www.eclipse.org/legal/epl-v10.html
vpcola 0:a1734fe1ec4b 10 * and the Eclipse Distribution License is available at
vpcola 0:a1734fe1ec4b 11 * http://www.eclipse.org/org/documents/edl-v10.php.
vpcola 0:a1734fe1ec4b 12 *
vpcola 0:a1734fe1ec4b 13 * Contributors:
vpcola 0:a1734fe1ec4b 14 * Ian Craggs - initial API and implementation and/or initial documentation
vpcola 0:a1734fe1ec4b 15 *******************************************************************************/
vpcola 0:a1734fe1ec4b 16
vpcola 0:a1734fe1ec4b 17 #include "StackTrace.h"
vpcola 0:a1734fe1ec4b 18 #include "MQTTPacket.h"
vpcola 0:a1734fe1ec4b 19
vpcola 0:a1734fe1ec4b 20 #include <string.h>
vpcola 0:a1734fe1ec4b 21
vpcola 0:a1734fe1ec4b 22 /**
vpcola 0:a1734fe1ec4b 23 * Encodes the message length according to the MQTT algorithm
vpcola 0:a1734fe1ec4b 24 * @param buf the buffer into which the encoded data is written
vpcola 0:a1734fe1ec4b 25 * @param length the length to be encoded
vpcola 0:a1734fe1ec4b 26 * @return the number of bytes written to buffer
vpcola 0:a1734fe1ec4b 27 */
vpcola 0:a1734fe1ec4b 28 int MQTTPacket_encode(unsigned char* buf, int length)
vpcola 0:a1734fe1ec4b 29 {
vpcola 0:a1734fe1ec4b 30 int rc = 0;
vpcola 0:a1734fe1ec4b 31
vpcola 0:a1734fe1ec4b 32 FUNC_ENTRY;
vpcola 0:a1734fe1ec4b 33 do
vpcola 0:a1734fe1ec4b 34 {
vpcola 0:a1734fe1ec4b 35 char d = length % 128;
vpcola 0:a1734fe1ec4b 36 length /= 128;
vpcola 0:a1734fe1ec4b 37 /* if there are more digits to encode, set the top bit of this digit */
vpcola 0:a1734fe1ec4b 38 if (length > 0)
vpcola 0:a1734fe1ec4b 39 d |= 0x80;
vpcola 0:a1734fe1ec4b 40 buf[rc++] = d;
vpcola 0:a1734fe1ec4b 41 } while (length > 0);
vpcola 0:a1734fe1ec4b 42 FUNC_EXIT_RC(rc);
vpcola 0:a1734fe1ec4b 43 return rc;
vpcola 0:a1734fe1ec4b 44 }
vpcola 0:a1734fe1ec4b 45
vpcola 0:a1734fe1ec4b 46
vpcola 0:a1734fe1ec4b 47 /**
vpcola 0:a1734fe1ec4b 48 * Decodes the message length according to the MQTT algorithm
vpcola 0:a1734fe1ec4b 49 * @param getcharfn pointer to function to read the next character from the data source
vpcola 0:a1734fe1ec4b 50 * @param value the decoded length returned
vpcola 0:a1734fe1ec4b 51 * @return the number of bytes read from the socket
vpcola 0:a1734fe1ec4b 52 */
vpcola 0:a1734fe1ec4b 53 int MQTTPacket_decode(int (*getcharfn)(unsigned char*, int), int* value)
vpcola 0:a1734fe1ec4b 54 {
vpcola 0:a1734fe1ec4b 55 unsigned char c;
vpcola 0:a1734fe1ec4b 56 int multiplier = 1;
vpcola 0:a1734fe1ec4b 57 int len = 0;
vpcola 0:a1734fe1ec4b 58 #define MAX_NO_OF_REMAINING_LENGTH_BYTES 4
vpcola 0:a1734fe1ec4b 59
vpcola 0:a1734fe1ec4b 60 FUNC_ENTRY;
vpcola 0:a1734fe1ec4b 61 *value = 0;
vpcola 0:a1734fe1ec4b 62 do
vpcola 0:a1734fe1ec4b 63 {
vpcola 0:a1734fe1ec4b 64 int rc = MQTTPACKET_READ_ERROR;
vpcola 0:a1734fe1ec4b 65
vpcola 0:a1734fe1ec4b 66 if (++len > MAX_NO_OF_REMAINING_LENGTH_BYTES)
vpcola 0:a1734fe1ec4b 67 {
vpcola 0:a1734fe1ec4b 68 rc = MQTTPACKET_READ_ERROR; /* bad data */
vpcola 0:a1734fe1ec4b 69 goto exit;
vpcola 0:a1734fe1ec4b 70 }
vpcola 0:a1734fe1ec4b 71 rc = (*getcharfn)(&c, 1);
vpcola 0:a1734fe1ec4b 72 if (rc != 1)
vpcola 0:a1734fe1ec4b 73 goto exit;
vpcola 0:a1734fe1ec4b 74 *value += (c & 127) * multiplier;
vpcola 0:a1734fe1ec4b 75 multiplier *= 128;
vpcola 0:a1734fe1ec4b 76 } while ((c & 128) != 0);
vpcola 0:a1734fe1ec4b 77 exit:
vpcola 0:a1734fe1ec4b 78 FUNC_EXIT_RC(len);
vpcola 0:a1734fe1ec4b 79 return len;
vpcola 0:a1734fe1ec4b 80 }
vpcola 0:a1734fe1ec4b 81
vpcola 0:a1734fe1ec4b 82
vpcola 0:a1734fe1ec4b 83 int MQTTPacket_len(int rem_len)
vpcola 0:a1734fe1ec4b 84 {
vpcola 0:a1734fe1ec4b 85 rem_len += 1; /* header byte */
vpcola 0:a1734fe1ec4b 86
vpcola 0:a1734fe1ec4b 87 /* now remaining_length field */
vpcola 0:a1734fe1ec4b 88 if (rem_len < 128)
vpcola 0:a1734fe1ec4b 89 rem_len += 1;
vpcola 0:a1734fe1ec4b 90 else if (rem_len < 16384)
vpcola 0:a1734fe1ec4b 91 rem_len += 2;
vpcola 0:a1734fe1ec4b 92 else if (rem_len < 2097151)
vpcola 0:a1734fe1ec4b 93 rem_len += 3;
vpcola 0:a1734fe1ec4b 94 else
vpcola 0:a1734fe1ec4b 95 rem_len += 4;
vpcola 0:a1734fe1ec4b 96 return rem_len;
vpcola 0:a1734fe1ec4b 97 }
vpcola 0:a1734fe1ec4b 98
vpcola 0:a1734fe1ec4b 99
vpcola 0:a1734fe1ec4b 100 static unsigned char* bufptr;
vpcola 0:a1734fe1ec4b 101
vpcola 0:a1734fe1ec4b 102 int bufchar(unsigned char* c, int count)
vpcola 0:a1734fe1ec4b 103 {
vpcola 0:a1734fe1ec4b 104 int i;
vpcola 0:a1734fe1ec4b 105
vpcola 0:a1734fe1ec4b 106 for (i = 0; i < count; ++i)
vpcola 0:a1734fe1ec4b 107 *c = *bufptr++;
vpcola 0:a1734fe1ec4b 108 return count;
vpcola 0:a1734fe1ec4b 109 }
vpcola 0:a1734fe1ec4b 110
vpcola 0:a1734fe1ec4b 111
vpcola 0:a1734fe1ec4b 112 int MQTTPacket_decodeBuf(unsigned char* buf, int* value)
vpcola 0:a1734fe1ec4b 113 {
vpcola 0:a1734fe1ec4b 114 bufptr = buf;
vpcola 0:a1734fe1ec4b 115 return MQTTPacket_decode(bufchar, value);
vpcola 0:a1734fe1ec4b 116 }
vpcola 0:a1734fe1ec4b 117
vpcola 0:a1734fe1ec4b 118
vpcola 0:a1734fe1ec4b 119 /**
vpcola 0:a1734fe1ec4b 120 * Calculates an integer from two bytes read from the input buffer
vpcola 0:a1734fe1ec4b 121 * @param pptr pointer to the input buffer - incremented by the number of bytes used & returned
vpcola 0:a1734fe1ec4b 122 * @return the integer value calculated
vpcola 0:a1734fe1ec4b 123 */
vpcola 0:a1734fe1ec4b 124 int readInt(unsigned char** pptr)
vpcola 0:a1734fe1ec4b 125 {
vpcola 0:a1734fe1ec4b 126 unsigned char* ptr = *pptr;
vpcola 0:a1734fe1ec4b 127 int len = 256*(*ptr) + (*(ptr+1));
vpcola 0:a1734fe1ec4b 128 *pptr += 2;
vpcola 0:a1734fe1ec4b 129 return len;
vpcola 0:a1734fe1ec4b 130 }
vpcola 0:a1734fe1ec4b 131
vpcola 0:a1734fe1ec4b 132
vpcola 0:a1734fe1ec4b 133 /**
vpcola 0:a1734fe1ec4b 134 * Reads one character from the input buffer.
vpcola 0:a1734fe1ec4b 135 * @param pptr pointer to the input buffer - incremented by the number of bytes used & returned
vpcola 0:a1734fe1ec4b 136 * @return the character read
vpcola 0:a1734fe1ec4b 137 */
vpcola 0:a1734fe1ec4b 138 char readChar(unsigned char** pptr)
vpcola 0:a1734fe1ec4b 139 {
vpcola 0:a1734fe1ec4b 140 char c = **pptr;
vpcola 0:a1734fe1ec4b 141 (*pptr)++;
vpcola 0:a1734fe1ec4b 142 return c;
vpcola 0:a1734fe1ec4b 143 }
vpcola 0:a1734fe1ec4b 144
vpcola 0:a1734fe1ec4b 145
vpcola 0:a1734fe1ec4b 146 /**
vpcola 0:a1734fe1ec4b 147 * Writes one character to an output buffer.
vpcola 0:a1734fe1ec4b 148 * @param pptr pointer to the output buffer - incremented by the number of bytes used & returned
vpcola 0:a1734fe1ec4b 149 * @param c the character to write
vpcola 0:a1734fe1ec4b 150 */
vpcola 0:a1734fe1ec4b 151 void writeChar(unsigned char** pptr, char c)
vpcola 0:a1734fe1ec4b 152 {
vpcola 0:a1734fe1ec4b 153 **pptr = c;
vpcola 0:a1734fe1ec4b 154 (*pptr)++;
vpcola 0:a1734fe1ec4b 155 }
vpcola 0:a1734fe1ec4b 156
vpcola 0:a1734fe1ec4b 157
vpcola 0:a1734fe1ec4b 158 /**
vpcola 0:a1734fe1ec4b 159 * Writes an integer as 2 bytes to an output buffer.
vpcola 0:a1734fe1ec4b 160 * @param pptr pointer to the output buffer - incremented by the number of bytes used & returned
vpcola 0:a1734fe1ec4b 161 * @param anInt the integer to write
vpcola 0:a1734fe1ec4b 162 */
vpcola 0:a1734fe1ec4b 163 void writeInt(unsigned char** pptr, int anInt)
vpcola 0:a1734fe1ec4b 164 {
vpcola 0:a1734fe1ec4b 165 **pptr = (unsigned char)(anInt / 256);
vpcola 0:a1734fe1ec4b 166 (*pptr)++;
vpcola 0:a1734fe1ec4b 167 **pptr = (unsigned char)(anInt % 256);
vpcola 0:a1734fe1ec4b 168 (*pptr)++;
vpcola 0:a1734fe1ec4b 169 }
vpcola 0:a1734fe1ec4b 170
vpcola 0:a1734fe1ec4b 171
vpcola 0:a1734fe1ec4b 172 /**
vpcola 0:a1734fe1ec4b 173 * Writes a "UTF" string to an output buffer. Converts C string to length-delimited.
vpcola 0:a1734fe1ec4b 174 * @param pptr pointer to the output buffer - incremented by the number of bytes used & returned
vpcola 0:a1734fe1ec4b 175 * @param string the C string to write
vpcola 0:a1734fe1ec4b 176 */
vpcola 0:a1734fe1ec4b 177 void writeCString(unsigned char** pptr, const char* string)
vpcola 0:a1734fe1ec4b 178 {
vpcola 0:a1734fe1ec4b 179 int len = strlen(string);
vpcola 0:a1734fe1ec4b 180 writeInt(pptr, len);
vpcola 0:a1734fe1ec4b 181 memcpy(*pptr, string, len);
vpcola 0:a1734fe1ec4b 182 *pptr += len;
vpcola 0:a1734fe1ec4b 183 }
vpcola 0:a1734fe1ec4b 184
vpcola 0:a1734fe1ec4b 185
vpcola 0:a1734fe1ec4b 186 int getLenStringLen(char* ptr)
vpcola 0:a1734fe1ec4b 187 {
vpcola 0:a1734fe1ec4b 188 int len = 256*((unsigned char)(*ptr)) + (unsigned char)(*(ptr+1));
vpcola 0:a1734fe1ec4b 189 return len;
vpcola 0:a1734fe1ec4b 190 }
vpcola 0:a1734fe1ec4b 191
vpcola 0:a1734fe1ec4b 192
vpcola 0:a1734fe1ec4b 193 void writeMQTTString(unsigned char** pptr, MQTTString mqttstring)
vpcola 0:a1734fe1ec4b 194 {
vpcola 0:a1734fe1ec4b 195 if (mqttstring.lenstring.len > 0)
vpcola 0:a1734fe1ec4b 196 {
vpcola 0:a1734fe1ec4b 197 writeInt(pptr, mqttstring.lenstring.len);
vpcola 0:a1734fe1ec4b 198 memcpy(*pptr, mqttstring.lenstring.data, mqttstring.lenstring.len);
vpcola 0:a1734fe1ec4b 199 *pptr += mqttstring.lenstring.len;
vpcola 0:a1734fe1ec4b 200 }
vpcola 0:a1734fe1ec4b 201 else if (mqttstring.cstring)
vpcola 0:a1734fe1ec4b 202 writeCString(pptr, mqttstring.cstring);
vpcola 0:a1734fe1ec4b 203 else
vpcola 0:a1734fe1ec4b 204 writeInt(pptr, 0);
vpcola 0:a1734fe1ec4b 205 }
vpcola 0:a1734fe1ec4b 206
vpcola 0:a1734fe1ec4b 207
vpcola 0:a1734fe1ec4b 208 /**
vpcola 0:a1734fe1ec4b 209 * @param mqttstring the MQTTString structure into which the data is to be read
vpcola 0:a1734fe1ec4b 210 * @param pptr pointer to the output buffer - incremented by the number of bytes used & returned
vpcola 0:a1734fe1ec4b 211 * @param enddata pointer to the end of the data: do not read beyond
vpcola 0:a1734fe1ec4b 212 * @return 1 if successful, 0 if not
vpcola 0:a1734fe1ec4b 213 */
vpcola 0:a1734fe1ec4b 214 int readMQTTLenString(MQTTString* mqttstring, unsigned char** pptr, unsigned char* enddata)
vpcola 0:a1734fe1ec4b 215 {
vpcola 0:a1734fe1ec4b 216 int rc = 0;
vpcola 0:a1734fe1ec4b 217
vpcola 0:a1734fe1ec4b 218 FUNC_ENTRY;
vpcola 0:a1734fe1ec4b 219 /* the first two bytes are the length of the string */
vpcola 0:a1734fe1ec4b 220 if (enddata - (*pptr) > 1) /* enough length to read the integer? */
vpcola 0:a1734fe1ec4b 221 {
vpcola 0:a1734fe1ec4b 222 mqttstring->lenstring.len = readInt(pptr); /* increments pptr to point past length */
vpcola 0:a1734fe1ec4b 223 if (&(*pptr)[mqttstring->lenstring.len] <= enddata)
vpcola 0:a1734fe1ec4b 224 {
vpcola 0:a1734fe1ec4b 225 mqttstring->lenstring.data = (char*)*pptr;
vpcola 0:a1734fe1ec4b 226 *pptr += mqttstring->lenstring.len;
vpcola 0:a1734fe1ec4b 227 rc = 1;
vpcola 0:a1734fe1ec4b 228 }
vpcola 0:a1734fe1ec4b 229 }
vpcola 0:a1734fe1ec4b 230 mqttstring->cstring = NULL;
vpcola 0:a1734fe1ec4b 231 FUNC_EXIT_RC(rc);
vpcola 0:a1734fe1ec4b 232 return rc;
vpcola 0:a1734fe1ec4b 233 }
vpcola 0:a1734fe1ec4b 234
vpcola 0:a1734fe1ec4b 235
vpcola 0:a1734fe1ec4b 236 /**
vpcola 0:a1734fe1ec4b 237 * Return the length of the MQTTstring - C string if there is one, otherwise the length delimited string
vpcola 0:a1734fe1ec4b 238 * @param mqttstring the string to return the length of
vpcola 0:a1734fe1ec4b 239 * @return the length of the string
vpcola 0:a1734fe1ec4b 240 */
vpcola 0:a1734fe1ec4b 241 int MQTTstrlen(MQTTString mqttstring)
vpcola 0:a1734fe1ec4b 242 {
vpcola 0:a1734fe1ec4b 243 int rc = 0;
vpcola 0:a1734fe1ec4b 244
vpcola 0:a1734fe1ec4b 245 if (mqttstring.cstring)
vpcola 0:a1734fe1ec4b 246 rc = strlen(mqttstring.cstring);
vpcola 0:a1734fe1ec4b 247 else
vpcola 0:a1734fe1ec4b 248 rc = mqttstring.lenstring.len;
vpcola 0:a1734fe1ec4b 249 return rc;
vpcola 0:a1734fe1ec4b 250 }
vpcola 0:a1734fe1ec4b 251
vpcola 0:a1734fe1ec4b 252
vpcola 0:a1734fe1ec4b 253 /**
vpcola 0:a1734fe1ec4b 254 * Compares an MQTTString to a C string
vpcola 0:a1734fe1ec4b 255 * @param a the MQTTString to compare
vpcola 0:a1734fe1ec4b 256 * @param bptr the C string to compare
vpcola 0:a1734fe1ec4b 257 * @return boolean - equal or not
vpcola 0:a1734fe1ec4b 258 */
vpcola 0:a1734fe1ec4b 259 int MQTTPacket_equals(MQTTString* a, char* bptr)
vpcola 0:a1734fe1ec4b 260 {
vpcola 0:a1734fe1ec4b 261 int alen = 0,
vpcola 0:a1734fe1ec4b 262 blen = 0;
vpcola 0:a1734fe1ec4b 263 char *aptr;
vpcola 0:a1734fe1ec4b 264
vpcola 0:a1734fe1ec4b 265 if (a->cstring)
vpcola 0:a1734fe1ec4b 266 {
vpcola 0:a1734fe1ec4b 267 aptr = a->cstring;
vpcola 0:a1734fe1ec4b 268 alen = strlen(a->cstring);
vpcola 0:a1734fe1ec4b 269 }
vpcola 0:a1734fe1ec4b 270 else
vpcola 0:a1734fe1ec4b 271 {
vpcola 0:a1734fe1ec4b 272 aptr = a->lenstring.data;
vpcola 0:a1734fe1ec4b 273 alen = a->lenstring.len;
vpcola 0:a1734fe1ec4b 274 }
vpcola 0:a1734fe1ec4b 275 blen = strlen(bptr);
vpcola 0:a1734fe1ec4b 276
vpcola 0:a1734fe1ec4b 277 return (alen == blen) && (strncmp(aptr, bptr, alen) == 0);
vpcola 0:a1734fe1ec4b 278 }
vpcola 0:a1734fe1ec4b 279
vpcola 0:a1734fe1ec4b 280
vpcola 0:a1734fe1ec4b 281 /**
vpcola 0:a1734fe1ec4b 282 * Helper function to read packet data from some source into a buffer
vpcola 0:a1734fe1ec4b 283 * @param buf the buffer into which the packet will be serialized
vpcola 0:a1734fe1ec4b 284 * @param buflen the length in bytes of the supplied buffer
vpcola 0:a1734fe1ec4b 285 * @param getfn pointer to a function which will read any number of bytes from the needed source
vpcola 0:a1734fe1ec4b 286 * @return integer MQTT packet type, or -1 on error
vpcola 0:a1734fe1ec4b 287 */
vpcola 0:a1734fe1ec4b 288 int MQTTPacket_read(unsigned char* buf, int buflen, int (*getfn)(unsigned char*, int))
vpcola 0:a1734fe1ec4b 289 {
vpcola 0:a1734fe1ec4b 290 int rc = -1;
vpcola 0:a1734fe1ec4b 291 MQTTHeader header = {0};
vpcola 0:a1734fe1ec4b 292 int len = 0;
vpcola 0:a1734fe1ec4b 293 int rem_len = 0;
vpcola 0:a1734fe1ec4b 294
vpcola 0:a1734fe1ec4b 295 /* 1. read the header byte. This has the packet type in it */
vpcola 0:a1734fe1ec4b 296 if ((*getfn)(buf, 1) != 1)
vpcola 0:a1734fe1ec4b 297 goto exit;
vpcola 0:a1734fe1ec4b 298
vpcola 0:a1734fe1ec4b 299 len = 1;
vpcola 0:a1734fe1ec4b 300 /* 2. read the remaining length. This is variable in itself */
vpcola 0:a1734fe1ec4b 301 MQTTPacket_decode(getfn, &rem_len);
vpcola 0:a1734fe1ec4b 302 len += MQTTPacket_encode(buf + 1, rem_len); /* put the original remaining length back into the buffer */
vpcola 0:a1734fe1ec4b 303
vpcola 0:a1734fe1ec4b 304 /* 3. read the rest of the buffer using a callback to supply the rest of the data */
vpcola 0:a1734fe1ec4b 305 if ((*getfn)(buf + len, rem_len) != rem_len)
vpcola 0:a1734fe1ec4b 306 goto exit;
vpcola 0:a1734fe1ec4b 307
vpcola 0:a1734fe1ec4b 308 header.byte = buf[0];
vpcola 0:a1734fe1ec4b 309 rc = header.bits.type;
vpcola 0:a1734fe1ec4b 310 exit:
vpcola 0:a1734fe1ec4b 311 return rc;
vpcola 0:a1734fe1ec4b 312 }
vpcola 0:a1734fe1ec4b 313
vpcola 0:a1734fe1ec4b 314
vpcola 0:a1734fe1ec4b 315 const char* MQTTPacket_names[] =
vpcola 0:a1734fe1ec4b 316 {
vpcola 0:a1734fe1ec4b 317 "RESERVED", "CONNECT", "CONNACK", "PUBLISH", "PUBACK", "PUBREC", "PUBREL",
vpcola 0:a1734fe1ec4b 318 "PUBCOMP", "SUBSCRIBE", "SUBACK", "UNSUBSCRIBE", "UNSUBACK",
vpcola 0:a1734fe1ec4b 319 "PINGREQ", "PINGRESP", "DISCONNECT"
vpcola 0:a1734fe1ec4b 320 };
vpcola 0:a1734fe1ec4b 321
vpcola 0:a1734fe1ec4b 322
vpcola 0:a1734fe1ec4b 323 char* MQTTPacket_toString(char* strbuf, int strbuflen, unsigned char* buf, int buflen)
vpcola 0:a1734fe1ec4b 324 {
vpcola 0:a1734fe1ec4b 325 int index = 0;
vpcola 0:a1734fe1ec4b 326 int rem_length = 0;
vpcola 0:a1734fe1ec4b 327 MQTTHeader header = {0};
vpcola 0:a1734fe1ec4b 328 int strindex = 0;
vpcola 0:a1734fe1ec4b 329
vpcola 0:a1734fe1ec4b 330 header.byte = buf[index++];
vpcola 0:a1734fe1ec4b 331 index += MQTTPacket_decodeBuf(&buf[index], &rem_length);
vpcola 0:a1734fe1ec4b 332
vpcola 0:a1734fe1ec4b 333 switch (header.bits.type)
vpcola 0:a1734fe1ec4b 334 {
vpcola 0:a1734fe1ec4b 335 case CONNECT:
vpcola 0:a1734fe1ec4b 336 {
vpcola 0:a1734fe1ec4b 337 MQTTPacket_connectData data;
vpcola 0:a1734fe1ec4b 338 if (MQTTDeserialize_connect(&data, buf, buflen) == 1)
vpcola 0:a1734fe1ec4b 339 {
vpcola 0:a1734fe1ec4b 340 strindex = snprintf(strbuf, strbuflen,
vpcola 0:a1734fe1ec4b 341 "CONNECT MQTT version %d, client id %.*s, clean session %d, keep alive %hd",
vpcola 0:a1734fe1ec4b 342 (int)data.MQTTVersion, data.clientID.lenstring.len, data.clientID.lenstring.data,
vpcola 0:a1734fe1ec4b 343 (int)data.cleansession, data.keepAliveInterval);
vpcola 0:a1734fe1ec4b 344 if (data.willFlag)
vpcola 0:a1734fe1ec4b 345 strindex += snprintf(&strbuf[strindex], strbuflen - strindex,
vpcola 0:a1734fe1ec4b 346 ", will QoS %d, will retain %d, will topic %.*s, will message %.*s",
vpcola 0:a1734fe1ec4b 347 data.will.qos, data.will.retained,
vpcola 0:a1734fe1ec4b 348 data.will.topicName.lenstring.len, data.will.topicName.lenstring.data,
vpcola 0:a1734fe1ec4b 349 data.will.message.lenstring.len, data.will.message.lenstring.data);
vpcola 0:a1734fe1ec4b 350 if (data.username.lenstring.data && data.username.lenstring.len > 0)
vpcola 0:a1734fe1ec4b 351 {
vpcola 0:a1734fe1ec4b 352 printf("user name\n");
vpcola 0:a1734fe1ec4b 353 strindex += snprintf(&strbuf[strindex], strbuflen - strindex,
vpcola 0:a1734fe1ec4b 354 ", user name %.*s", data.username.lenstring.len, data.username.lenstring.data);
vpcola 0:a1734fe1ec4b 355 }
vpcola 0:a1734fe1ec4b 356 if (data.password.lenstring.data && data.password.lenstring.len > 0)
vpcola 0:a1734fe1ec4b 357 strindex += snprintf(&strbuf[strindex], strbuflen - strindex,
vpcola 0:a1734fe1ec4b 358 ", password %.*s", data.password.lenstring.len, data.password.lenstring.data);
vpcola 0:a1734fe1ec4b 359 }
vpcola 0:a1734fe1ec4b 360 }
vpcola 0:a1734fe1ec4b 361 break;
vpcola 0:a1734fe1ec4b 362 case CONNACK:
vpcola 0:a1734fe1ec4b 363 {
vpcola 0:a1734fe1ec4b 364 unsigned char sessionPresent, connack_rc;
vpcola 0:a1734fe1ec4b 365 if (MQTTDeserialize_connack(&sessionPresent, &connack_rc, buf, buflen) == 1)
vpcola 0:a1734fe1ec4b 366 strindex = snprintf(strbuf, strbuflen,
vpcola 0:a1734fe1ec4b 367 "CONNACK session present %d, rc %d", sessionPresent, connack_rc);
vpcola 0:a1734fe1ec4b 368 }
vpcola 0:a1734fe1ec4b 369 break;
vpcola 0:a1734fe1ec4b 370 case PUBLISH:
vpcola 0:a1734fe1ec4b 371 {
vpcola 0:a1734fe1ec4b 372 unsigned char dup, retained, *payload;
vpcola 0:a1734fe1ec4b 373 unsigned short packetid;
vpcola 0:a1734fe1ec4b 374 int qos, payloadlen;
vpcola 0:a1734fe1ec4b 375 MQTTString topicName = MQTTString_initializer;
vpcola 0:a1734fe1ec4b 376 if (MQTTDeserialize_publish(&dup, &qos, &retained, &packetid, &topicName,
vpcola 0:a1734fe1ec4b 377 &payload, &payloadlen, buf, buflen) == 1)
vpcola 0:a1734fe1ec4b 378 strindex = snprintf(strbuf, strbuflen,
vpcola 0:a1734fe1ec4b 379 "PUBLISH dup %d, QoS %d, retained %d, packet id %d, topic %.*s, payload length %d, payload %.*s",
vpcola 0:a1734fe1ec4b 380 dup, qos, retained, packetid,
vpcola 0:a1734fe1ec4b 381 (topicName.lenstring.len < 20) ? topicName.lenstring.len : 20, topicName.lenstring.data,
vpcola 0:a1734fe1ec4b 382 payloadlen, (payloadlen < 20) ? payloadlen : 20, payload);
vpcola 0:a1734fe1ec4b 383 }
vpcola 0:a1734fe1ec4b 384 break;
vpcola 0:a1734fe1ec4b 385 case PUBACK:
vpcola 0:a1734fe1ec4b 386 case PUBREC:
vpcola 0:a1734fe1ec4b 387 case PUBREL:
vpcola 0:a1734fe1ec4b 388 case PUBCOMP:
vpcola 0:a1734fe1ec4b 389 {
vpcola 0:a1734fe1ec4b 390 unsigned char packettype, dup;
vpcola 0:a1734fe1ec4b 391 unsigned short packetid;
vpcola 0:a1734fe1ec4b 392 if (MQTTDeserialize_ack(&packettype, &dup, &packetid, buf, buflen) == 1)
vpcola 0:a1734fe1ec4b 393 strindex = snprintf(strbuf, strbuflen,
vpcola 0:a1734fe1ec4b 394 "%s dup %d, packet id %d",
vpcola 0:a1734fe1ec4b 395 MQTTPacket_names[packettype], dup, packetid);
vpcola 0:a1734fe1ec4b 396 }
vpcola 0:a1734fe1ec4b 397 break;
vpcola 0:a1734fe1ec4b 398 case SUBSCRIBE:
vpcola 0:a1734fe1ec4b 399 {
vpcola 0:a1734fe1ec4b 400 unsigned char dup;
vpcola 0:a1734fe1ec4b 401 unsigned short packetid;
vpcola 0:a1734fe1ec4b 402 int maxcount = 1, count = 0;
vpcola 0:a1734fe1ec4b 403 MQTTString topicFilters[1];
vpcola 0:a1734fe1ec4b 404 int requestedQoSs[1];
vpcola 0:a1734fe1ec4b 405 if (MQTTDeserialize_subscribe(&dup, &packetid, maxcount, &count,
vpcola 0:a1734fe1ec4b 406 topicFilters, requestedQoSs, buf, buflen) == 1)
vpcola 0:a1734fe1ec4b 407 strindex = snprintf(strbuf, strbuflen,
vpcola 0:a1734fe1ec4b 408 "SUBSCRIBE dup %d, packet id %d count %d topic %.*s qos %d",
vpcola 0:a1734fe1ec4b 409 dup, packetid, count,
vpcola 0:a1734fe1ec4b 410 topicFilters[0].lenstring.len, topicFilters[0].lenstring.data,
vpcola 0:a1734fe1ec4b 411 requestedQoSs[0]);
vpcola 0:a1734fe1ec4b 412 }
vpcola 0:a1734fe1ec4b 413 break;
vpcola 0:a1734fe1ec4b 414 case SUBACK:
vpcola 0:a1734fe1ec4b 415 {
vpcola 0:a1734fe1ec4b 416 unsigned short packetid;
vpcola 0:a1734fe1ec4b 417 int maxcount = 1, count = 0;
vpcola 0:a1734fe1ec4b 418 int grantedQoSs[1];
vpcola 0:a1734fe1ec4b 419 if (MQTTDeserialize_suback(&packetid, maxcount, &count, grantedQoSs, buf, buflen) == 1)
vpcola 0:a1734fe1ec4b 420 strindex = snprintf(strbuf, strbuflen,
vpcola 0:a1734fe1ec4b 421 "SUBACK packet id %d count %d granted qos %d",
vpcola 0:a1734fe1ec4b 422 packetid, count, grantedQoSs[0]);
vpcola 0:a1734fe1ec4b 423 }
vpcola 0:a1734fe1ec4b 424 break;
vpcola 0:a1734fe1ec4b 425 case UNSUBSCRIBE:
vpcola 0:a1734fe1ec4b 426 {
vpcola 0:a1734fe1ec4b 427 unsigned char dup;
vpcola 0:a1734fe1ec4b 428 unsigned short packetid;
vpcola 0:a1734fe1ec4b 429 int maxcount = 1, count = 0;
vpcola 0:a1734fe1ec4b 430 MQTTString topicFilters[1];
vpcola 0:a1734fe1ec4b 431 if (MQTTDeserialize_unsubscribe(&dup, &packetid, maxcount, &count, topicFilters, buf, buflen) == 1)
vpcola 0:a1734fe1ec4b 432 strindex = snprintf(strbuf, strbuflen,
vpcola 0:a1734fe1ec4b 433 "UNSUBSCRIBE dup %d, packet id %d count %d topic %.*s",
vpcola 0:a1734fe1ec4b 434 dup, packetid, count,
vpcola 0:a1734fe1ec4b 435 topicFilters[0].lenstring.len, topicFilters[0].lenstring.data);
vpcola 0:a1734fe1ec4b 436 }
vpcola 0:a1734fe1ec4b 437 break;
vpcola 0:a1734fe1ec4b 438 case UNSUBACK:
vpcola 0:a1734fe1ec4b 439 {
vpcola 0:a1734fe1ec4b 440 unsigned short packetid;
vpcola 0:a1734fe1ec4b 441 if (MQTTDeserialize_unsuback(&packetid, buf, buflen) == 1)
vpcola 0:a1734fe1ec4b 442 strindex = snprintf(strbuf, strbuflen,
vpcola 0:a1734fe1ec4b 443 "UNSUBACK packet id %d", packetid);
vpcola 0:a1734fe1ec4b 444 }
vpcola 0:a1734fe1ec4b 445 break;
vpcola 0:a1734fe1ec4b 446 case PINGREQ:
vpcola 0:a1734fe1ec4b 447 case PINGRESP:
vpcola 0:a1734fe1ec4b 448 case DISCONNECT:
vpcola 0:a1734fe1ec4b 449 strindex = snprintf(strbuf, strbuflen, "%s", MQTTPacket_names[header.bits.type]);
vpcola 0:a1734fe1ec4b 450 break;
vpcola 0:a1734fe1ec4b 451 }
vpcola 0:a1734fe1ec4b 452 return strbuf;
vpcola 0:a1734fe1ec4b 453 }