Fork of my MQTTGateway

Dependencies:   mbed-http

Committer:
vpcola
Date:
Sat Apr 08 14:45:51 2017 +0000
Revision:
0:f1d3878b8dd9
Initial commit

Who changed what in which revision?

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