【mbed OS5対応バージョン】データの保存、更新、取得ができるWebサービス「milkcocoa」に接続し、データのプッシュ、送信、取得ができるライブラリです。 https://mlkcca.com/

Dependents:   mbed-os-example-wifi-milkcocoa MilkcocoaOsSample_Eth MilkcocoaOsSample_ESP8266 MilkcocoaOsSample_Eth_DigitalIn

Committer:
jksoft
Date:
Thu Feb 09 07:26:57 2017 +0000
Revision:
0:0a2f634d3324
??

Who changed what in which revision?

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