Microchip® Advanced Software Framework

MQTTPacket.h File Reference
#include "MQTTConnect.h"
#include "MQTTPublish.h"
#include "MQTTSubscribe.h"
#include "MQTTUnsubscribe.h"
#include "MQTTFormat.h"

Data Structures

union  MQTTHeader
 Bitfields for the MQTT header byte. More...
 
struct  MQTTLenString
 
struct  MQTTString
 
struct  MQTTTransport
 

Macros

#define DLLExport
 
#define DLLImport
 
#define MQTTString_initializer   {NULL, {0, NULL}}
 

Enumerations

enum  errors {
  MQTTPACKET_BUFFER_TOO_SHORT = -2,
  MQTTPACKET_READ_ERROR = -1,
  MQTTPACKET_READ_COMPLETE
}
 
enum  msgTypes {
  CONNECT = 1,
  CONNACK,
  PUBLISH,
  PUBACK,
  PUBREC,
  PUBREL,
  PUBCOMP,
  SUBSCRIBE,
  SUBACK,
  UNSUBSCRIBE,
  UNSUBACK,
  PINGREQ,
  PINGRESP,
  DISCONNECT
}
 

Functions

int MQTTDeserialize_ack (unsigned char *packettype, unsigned char *dup, unsigned short *packetid, unsigned char *buf, int buflen)
 Deserializes the supplied (wire) buffer into an ack. More...
 
int MQTTPacket_decode (int(*getcharfn)(unsigned char *, int), int *value)
 Decodes the message length according to the MQTT algorithm. More...
 
int MQTTPacket_decodeBuf (unsigned char *buf, int *value)
 
int MQTTPacket_encode (unsigned char *buf, int length)
 Encodes the message length according to the MQTT algorithm. More...
 
int MQTTPacket_equals (MQTTString *a, char *b)
 Compares an MQTTString to a C string. More...
 
int MQTTPacket_len (int rem_len)
 
DLLExport int MQTTPacket_read (unsigned char *buf, int buflen, int(*getfn)(unsigned char *, int))
 Helper function to read packet data from some source into a buffer. More...
 
int MQTTPacket_readnb (unsigned char *buf, int buflen, MQTTTransport *trp)
 Helper function to read packet data from some source into a buffer, non-blocking. More...
 
int MQTTSerialize_ack (unsigned char *buf, int buflen, unsigned char type, unsigned char dup, unsigned short packetid)
 Serializes the ack packet into the supplied buffer. More...
 
int MQTTstrlen (MQTTString mqttstring)
 Return the length of the MQTTstring - C string if there is one, otherwise the length delimited string. More...
 
char readChar (unsigned char **pptr)
 Reads one character from the input buffer. More...
 
int readInt (unsigned char **pptr)
 Calculates an integer from two bytes read from the input buffer. More...
 
int readMQTTLenString (MQTTString *mqttstring, unsigned char **pptr, unsigned char *enddata)
 
void writeChar (unsigned char **pptr, char c)
 Writes one character to an output buffer. More...
 
void writeCString (unsigned char **pptr, const char *string)
 Writes a "UTF" string to an output buffer. More...
 
void writeInt (unsigned char **pptr, int anInt)
 Writes an integer as 2 bytes to an output buffer. More...
 
void writeMQTTString (unsigned char **pptr, MQTTString mqttstring)
 

#define DLLExport
#define DLLImport
#define MQTTString_initializer   {NULL, {0, NULL}}

enum errors
Enumerator
MQTTPACKET_BUFFER_TOO_SHORT 
MQTTPACKET_READ_ERROR 
MQTTPACKET_READ_COMPLETE 
enum msgTypes
Enumerator
CONNECT 
CONNACK 
PUBLISH 
PUBACK 
PUBREC 
PUBREL 
PUBCOMP 
SUBSCRIBE 
SUBACK 
UNSUBSCRIBE 
UNSUBACK 
PINGREQ 
PINGRESP 
DISCONNECT 

int MQTTDeserialize_ack ( unsigned char *  packettype,
unsigned char *  dup,
unsigned short *  packetid,
unsigned char *  buf,
int  buflen 
)

Deserializes the supplied (wire) buffer into an ack.

Parameters
packettypereturned integer - the MQTT packet type
dupreturned integer - the MQTT dup flag
packetidreturned integer - the MQTT packet identifier
bufthe raw buffer data, of the correct length determined by the remaining length field
buflenthe length in bytes of the data in the supplied buffer
Returns
error code. 1 is success, 0 is failure

References MQTTHeader::bits, MQTTHeader::byte, MQTTHeader::dup, FUNC_ENTRY, FUNC_EXIT_RC, header, MQTTPacket_decodeBuf(), NULL, readChar(), readInt(), and MQTTHeader::type.

Referenced by cycle(), MQTTDeserialize_unsuback(), MQTTFormat_toClientString(), MQTTFormat_toServerString(), and MQTTPublish().

int MQTTPacket_decode ( int(*)(unsigned char *, int)  getcharfn,
int *  value 
)

Decodes the message length according to the MQTT algorithm.

Parameters
getcharfnpointer to function to read the next character from the data source
valuethe decoded length returned
Returns
the number of bytes read from the socket

References FUNC_ENTRY, FUNC_EXIT_RC, MAX_NO_OF_REMAINING_LENGTH_BYTES, and MQTTPACKET_READ_ERROR.

Referenced by MQTTPacket_decodeBuf(), and MQTTPacket_read().

int MQTTPacket_encode ( unsigned char *  buf,
int  length 
)

Encodes the message length according to the MQTT algorithm.

Parameters
bufthe buffer into which the encoded data is written
lengththe length to be encoded
Returns
the number of bytes written to buffer

References FUNC_ENTRY, and FUNC_EXIT_RC.

Referenced by MQTTPacket_read(), MQTTPacket_readnb(), MQTTSerialize_ack(), MQTTSerialize_connack(), MQTTSerialize_connect(), MQTTSerialize_publish(), MQTTSerialize_suback(), MQTTSerialize_subscribe(), MQTTSerialize_unsuback(), MQTTSerialize_unsubscribe(), MQTTSerialize_zero(), and readPacket().

int MQTTPacket_equals ( MQTTString a,
char *  bptr 
)

Compares an MQTTString to a C string.

Parameters
athe MQTTString to compare
bptrthe C string to compare
Returns
boolean - equal or not

References MQTTString::cstring, MQTTLenString::data, MQTTLenString::len, and MQTTString::lenstring.

Referenced by deliverMessage().

DLLExport int MQTTPacket_read ( unsigned char *  buf,
int  buflen,
int(*)(unsigned char *, int)  getfn 
)

Helper function to read packet data from some source into a buffer.

Parameters
bufthe buffer into which the packet will be serialized
buflenthe length in bytes of the supplied buffer
getfnpointer to a function which will read any number of bytes from the needed source
Returns
integer MQTT packet type, or -1 on error
Note
the whole message must fit into the caller's buffer

References MQTTHeader::bits, MQTTHeader::byte, header, MQTTPacket_decode(), MQTTPacket_encode(), and MQTTHeader::type.

int MQTTPacket_readnb ( unsigned char *  buf,
int  buflen,
MQTTTransport trp 
)

Helper function to read packet data from some source into a buffer, non-blocking.

Parameters
bufthe buffer into which the packet will be serialized
buflenthe length in bytes of the supplied buffer
trppointer to a transport structure holding what is needed to solve getting data from it
Returns
integer MQTT packet type, 0 for call again, or -1 on error
Note
the whole message must fit into the caller's buffer

References MQTTHeader::bits, MQTTHeader::byte, MQTTTransport::getfn, header, MQTTTransport::len, MQTTPacket_decodenb(), MQTTPacket_encode(), MQTTPACKET_READ_ERROR, MQTTTransport::rem_len, MQTTTransport::sck, MQTTTransport::state, and MQTTHeader::type.

int MQTTSerialize_ack ( unsigned char *  buf,
int  buflen,
unsigned char  packettype,
unsigned char  dup,
unsigned short  packetid 
)

Serializes the ack packet into the supplied buffer.

Parameters
bufthe buffer into which the packet will be serialized
buflenthe length in bytes of the supplied buffer
typethe MQTT packet type
dupthe MQTT dup flag
packetidthe MQTT packet identifier
Returns
serialized length, or error if 0

References MQTTHeader::bits, MQTTHeader::byte, MQTTHeader::dup, FUNC_ENTRY, FUNC_EXIT_RC, header, MQTTPACKET_BUFFER_TOO_SHORT, MQTTPacket_encode(), ptr, PUBREL, MQTTHeader::qos, MQTTHeader::type, writeChar(), and writeInt().

Referenced by cycle(), MQTTSerialize_puback(), MQTTSerialize_pubcomp(), and MQTTSerialize_pubrel().

int MQTTstrlen ( MQTTString  mqttstring)

Return the length of the MQTTstring - C string if there is one, otherwise the length delimited string.

Parameters
mqttstringthe string to return the length of
Returns
the length of the string

References MQTTString::cstring, MQTTLenString::len, and MQTTString::lenstring.

Referenced by MQTTSerialize_connectLength(), MQTTSerialize_publishLength(), MQTTSerialize_subscribeLength(), and MQTTSerialize_unsubscribeLength().

char readChar ( unsigned char **  pptr)

Reads one character from the input buffer.

Parameters
pptrpointer to the input buffer - incremented by the number of bytes used & returned
Returns
the character read

Referenced by MQTTDeserialize_ack(), MQTTDeserialize_connack(), MQTTDeserialize_connect(), MQTTDeserialize_publish(), MQTTDeserialize_suback(), MQTTDeserialize_subscribe(), and MQTTDeserialize_unsubscribe().

int readInt ( unsigned char **  pptr)

Calculates an integer from two bytes read from the input buffer.

Parameters
pptrpointer to the input buffer - incremented by the number of bytes used & returned
Returns
the integer value calculated

References ptr.

Referenced by MQTTDeserialize_ack(), MQTTDeserialize_connect(), MQTTDeserialize_publish(), MQTTDeserialize_suback(), MQTTDeserialize_subscribe(), MQTTDeserialize_unsubscribe(), and readMQTTLenString().

int readMQTTLenString ( MQTTString mqttstring,
unsigned char **  pptr,
unsigned char *  enddata 
)
Parameters
mqttstringthe MQTTString structure into which the data is to be read
pptrpointer to the output buffer - incremented by the number of bytes used & returned
enddatapointer to the end of the data: do not read beyond
Returns
1 if successful, 0 if not

References MQTTString::cstring, MQTTLenString::data, FUNC_ENTRY, FUNC_EXIT_RC, MQTTLenString::len, MQTTString::lenstring, NULL, and readInt().

Referenced by MQTTDeserialize_connect(), MQTTDeserialize_publish(), MQTTDeserialize_subscribe(), and MQTTDeserialize_unsubscribe().

void writeChar ( unsigned char **  pptr,
char  c 
)

Writes one character to an output buffer.

Parameters
pptrpointer to the output buffer - incremented by the number of bytes used & returned
cthe character to write

Referenced by MQTTSerialize_ack(), MQTTSerialize_connack(), MQTTSerialize_connect(), MQTTSerialize_publish(), MQTTSerialize_suback(), MQTTSerialize_subscribe(), MQTTSerialize_unsuback(), MQTTSerialize_unsubscribe(), and MQTTSerialize_zero().

void writeCString ( unsigned char **  pptr,
const char *  string 
)

Writes a "UTF" string to an output buffer.

Converts C string to length-delimited.

Parameters
pptrpointer to the output buffer - incremented by the number of bytes used & returned
stringthe C string to write

References writeInt().

Referenced by MQTTSerialize_connect(), and writeMQTTString().

void writeInt ( unsigned char **  pptr,
int  anInt 
)

Writes an integer as 2 bytes to an output buffer.

Parameters
pptrpointer to the output buffer - incremented by the number of bytes used & returned
anIntthe integer to write

Referenced by MQTTSerialize_ack(), MQTTSerialize_connect(), MQTTSerialize_publish(), MQTTSerialize_suback(), MQTTSerialize_subscribe(), MQTTSerialize_unsuback(), MQTTSerialize_unsubscribe(), writeCString(), and writeMQTTString().