Microchip® Advanced Software Framework

MQTTPacket.c File Reference
#include "StackTrace.h"
#include "MQTTPacket.h"
#include <string.h>

Macros

#define MAX_NO_OF_REMAINING_LENGTH_BYTES   4
 

Functions

int bufchar (unsigned char *c, int count)
 
int getLenStringLen (char *ptr)
 
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)
 
static int MQTTPacket_decodenb (MQTTTransport *trp)
 Decodes the message length according to the MQTT algorithm, non-blocking. More...
 
int MQTTPacket_encode (unsigned char *buf, int length)
 Encodes the message length according to the MQTT algorithm. More...
 
int MQTTPacket_equals (MQTTString *a, char *bptr)
 Compares an MQTTString to a C string. More...
 
int MQTTPacket_len (int rem_len)
 
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 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)
 

Variables

static unsigned char * bufptr
 

#define MAX_NO_OF_REMAINING_LENGTH_BYTES   4

int bufchar ( unsigned char *  c,
int  count 
)

References count, and i.

Referenced by MQTTPacket_decodeBuf().

int getLenStringLen ( char *  ptr)
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().

static int MQTTPacket_decodenb ( MQTTTransport trp)
static

Decodes the message length according to the MQTT algorithm, non-blocking.

Parameters
trppointer to a transport structure holding what is needed to solve getting data from it
valuethe decoded length returned
Returns
integer the number of bytes read from the socket, 0 for call again, or -1 on error

References FUNC_ENTRY, FUNC_EXIT_RC, MQTTTransport::getfn, MQTTTransport::len, MAX_NO_OF_REMAINING_LENGTH_BYTES, MQTTPACKET_READ_ERROR, MQTTTransport::multiplier, MQTTTransport::rem_len, and MQTTTransport::sck.

Referenced by MQTTPacket_readnb().

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().

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 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().

unsigned char* bufptr
static