123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- #include "MQTTPacket.h"
- #include "StackTrace.h"
- #include <string.h>
- int MQTTSerialize_connectLength(MQTTPacket_connectData* options)
- {
- int len = 0;
- FUNC_ENTRY;
- if (options->MQTTVersion == 3)
- len = 12;
- else if (options->MQTTVersion == 4)
- len = 10;
- len += MQTTstrlen(options->clientID)+2;
- if (options->willFlag)
- len += MQTTstrlen(options->will.topicName)+2 + MQTTstrlen(options->will.message)+2;
- if (options->username.cstring || options->username.lenstring.data)
- len += MQTTstrlen(options->username)+2;
- if (options->password.cstring || options->password.lenstring.data)
- len += MQTTstrlen(options->password)+2;
- FUNC_EXIT_RC(len);
- return len;
- }
- int MQTTSerialize_connect(unsigned char* buf, int buflen, MQTTPacket_connectData* options)
- {
- unsigned char *ptr = buf;
- MQTTHeader header = {0};
- MQTTConnectFlags flags = {0};
- int len = 0;
- int rc = -1;
- FUNC_ENTRY;
- if (MQTTPacket_len(len = MQTTSerialize_connectLength(options)) > buflen)
- {
- rc = MQTTPACKET_BUFFER_TOO_SHORT;
- goto exit;
- }
- header.byte = 0;
- header.bits.type = CONNECT;
- writeChar(&ptr, header.byte);
- ptr += MQTTPacket_encode(ptr, len);
- if (options->MQTTVersion == 4)
- {
- writeCString(&ptr, "MQTT");
- writeChar(&ptr, (char) 4);
- }
- else
- {
- writeCString(&ptr, "MQIsdp");
- writeChar(&ptr, (char) 3);
- }
- flags.all = 0;
- flags.bits.cleansession = options->cleansession;
- flags.bits.will = (options->willFlag) ? 1 : 0;
- if (flags.bits.will)
- {
- flags.bits.willQoS = options->will.qos;
- flags.bits.willRetain = options->will.retained;
- }
- if (options->username.cstring || options->username.lenstring.data)
- flags.bits.username = 1;
- if (options->password.cstring || options->password.lenstring.data)
- flags.bits.password = 1;
- writeChar(&ptr, flags.all);
- writeInt(&ptr, options->keepAliveInterval);
- writeMQTTString(&ptr, options->clientID);
- if (options->willFlag)
- {
- writeMQTTString(&ptr, options->will.topicName);
- writeMQTTString(&ptr, options->will.message);
- }
- if (flags.bits.username)
- writeMQTTString(&ptr, options->username);
- if (flags.bits.password)
- writeMQTTString(&ptr, options->password);
- rc = ptr - buf;
- exit: FUNC_EXIT_RC(rc);
- return rc;
- }
- int MQTTDeserialize_connack(unsigned char* sessionPresent, unsigned char* connack_rc, unsigned char* buf, int buflen)
- {
- MQTTHeader header = {0};
- unsigned char* curdata = buf;
- unsigned char* enddata = NULL;
- int rc = 0;
- int mylen;
- MQTTConnackFlags flags = {0};
- FUNC_ENTRY;
- header.byte = readChar(&curdata);
- if (header.bits.type != CONNACK)
- goto exit;
- curdata += (rc = MQTTPacket_decodeBuf(curdata, &mylen));
- enddata = curdata + mylen;
- if (enddata - curdata < 2)
- goto exit;
- flags.all = readChar(&curdata);
- *sessionPresent = flags.bits.sessionpresent;
- *connack_rc = readChar(&curdata);
- rc = 1;
- exit:
- FUNC_EXIT_RC(rc);
- return rc;
- }
- int MQTTSerialize_zero(unsigned char* buf, int buflen, unsigned char packettype)
- {
- MQTTHeader header = {0};
- int rc = -1;
- unsigned char *ptr = buf;
- FUNC_ENTRY;
- if (buflen < 2)
- {
- rc = MQTTPACKET_BUFFER_TOO_SHORT;
- goto exit;
- }
- header.byte = 0;
- header.bits.type = packettype;
- writeChar(&ptr, header.byte);
- ptr += MQTTPacket_encode(ptr, 0);
- rc = ptr - buf;
- exit:
- FUNC_EXIT_RC(rc);
- return rc;
- }
- int MQTTSerialize_disconnect(unsigned char* buf, int buflen)
- {
- return MQTTSerialize_zero(buf, buflen, DISCONNECT);
- }
- int MQTTSerialize_pingreq(unsigned char* buf, int buflen)
- {
- return MQTTSerialize_zero(buf, buflen, PINGREQ);
- }
|