mbed OS5に対応したMilkcocoaライブラリのテストバージョンです。

Fork of mbed-os-example-mbed5-blinky by mbed-os-examples

Committer:
jksoft
Date:
Tue Jan 24 13:41:36 2017 +0000
Revision:
24:6ba1245bf049
??????????

Who changed what in which revision?

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