MQTTPacket.c File Reference

functions to deal with reading and writing of MQTT packets from and to sockets More...

#include "MQTTPacket.h"
#include "Log.h"
#include "MQTTPersistence.h"
#include "Messages.h"
#include "StackTrace.h"
#include <stdlib.h>
#include <string.h>
#include "Heap.h"
Include dependency graph for MQTTPacket.c:

Defines

#define MAX_NO_OF_REMAINING_LENGTH_BYTES   4

Functions

const char * MQTTPacket_name (int ptype)
 Converts an MQTT packet code into its name.
static char * readUTFlen (char **pptr, char *enddata, int *len)
 Reads a "UTF" string from the input buffer.
static int MQTTPacket_send_ack (int type, int msgid, int dup, networkHandles *net)
 Send an MQTT acknowledgement packet down a socket.
void * MQTTPacket_Factory (networkHandles *net, int *error)
 Reads one MQTT packet from a socket.
int MQTTPacket_send (networkHandles *net, Header header, char *buffer, size_t buflen, int free)
 Sends an MQTT packet in one system call write.
int MQTTPacket_sends (networkHandles *net, Header header, int count, char **buffers, size_t *buflens, int *frees)
 Sends an MQTT packet from multiple buffers in one system call write.
int MQTTPacket_encode (char *buf, size_t length)
 Encodes the message length according to the MQTT algorithm.
int MQTTPacket_decode (networkHandles *net, size_t *value)
 Decodes the message length according to the MQTT algorithm.
int readInt (char **pptr)
 Calculates an integer from two bytes read from the input buffer.
char * readUTF (char **pptr, char *enddata)
 Reads a "UTF" string from the input buffer.
unsigned char readChar (char **pptr)
 Reads one character from the input buffer.
void writeChar (char **pptr, char c)
 Writes one character to an output buffer.
void writeInt (char **pptr, int anInt)
 Writes an integer as 2 bytes to an output buffer.
void writeUTF (char **pptr, const char *string)
 Writes a "UTF" string to an output buffer.
void writeData (char **pptr, const void *data, int datalen)
 Writes length delimited data to an output buffer.
void * MQTTPacket_header_only (unsigned char aHeader, char *data, size_t datalen)
 Function used in the new packets table to create packets which have only a header.
int MQTTPacket_send_disconnect (networkHandles *net, const char *clientID)
 Send an MQTT disconnect packet down a socket.
void * MQTTPacket_publish (unsigned char aHeader, char *data, size_t datalen)
 Function used in the new packets table to create publish packets.
void MQTTPacket_freePublish (Publish *pack)
 Free allocated storage for a publish packet.
int MQTTPacket_send_puback (int msgid, networkHandles *net, const char *clientID)
 Send an MQTT PUBACK packet down a socket.
void MQTTPacket_freeSuback (Suback *pack)
 Free allocated storage for a suback packet.
int MQTTPacket_send_pubrec (int msgid, networkHandles *net, const char *clientID)
 Send an MQTT PUBREC packet down a socket.
int MQTTPacket_send_pubrel (int msgid, int dup, networkHandles *net, const char *clientID)
 Send an MQTT PUBREL packet down a socket.
int MQTTPacket_send_pubcomp (int msgid, networkHandles *net, const char *clientID)
 Send an MQTT PUBCOMP packet down a socket.
void * MQTTPacket_ack (unsigned char aHeader, char *data, size_t datalen)
 Function used in the new packets table to create acknowledgement packets.
int MQTTPacket_send_publish (Publish *pack, int dup, int qos, int retained, networkHandles *net, const char *clientID)
 Send an MQTT PUBLISH packet down a socket.
void MQTTPacket_free_packet (MQTTPacket *pack)
 Free allocated storage for a various packet tyoes.

Variables

static const char * packet_names []
 List of the predefined MQTT v3 packet names.
const char ** MQTTClient_packet_names = packet_names
pf new_packets []
 Array of functions to build packets, indexed according to packet code.

Detailed Description

functions to deal with reading and writing of MQTT packets from and to sockets

Some other related functions are in the MQTTPacketOut module


Function Documentation

void* MQTTPacket_ack ( unsigned char  aHeader,
char *  data,
size_t  datalen 
)

Function used in the new packets table to create acknowledgement packets.

Parameters:
aHeader the MQTT header byte
data the rest of the packet
datalen the length of the rest of the packet
Returns:
pointer to the packet structure

Here is the call graph for this function:

int MQTTPacket_decode ( networkHandles net,
size_t *  value 
)

Decodes the message length according to the MQTT algorithm.

Parameters:
socket the socket from which to read the bytes
value the decoded length returned
Returns:
the number of bytes read from the socket

Here is the call graph for this function:

int MQTTPacket_encode ( char *  buf,
size_t  length 
)

Encodes the message length according to the MQTT algorithm.

Parameters:
buf the buffer into which the encoded data is written
length the length to be encoded
Returns:
the number of bytes written to buffer
void* MQTTPacket_Factory ( networkHandles net,
int *  error 
)

Reads one MQTT packet from a socket.

Parameters:
socket a socket from which to read an MQTT packet
error pointer to the error code which is completed if no packet is returned
Returns:
the packet structure or NULL if there was an error

Here is the call graph for this function:

void MQTTPacket_free_packet ( MQTTPacket pack  ) 

Free allocated storage for a various packet tyoes.

Parameters:
pack pointer to the suback packet structure

Here is the call graph for this function:

void MQTTPacket_freePublish ( Publish pack  ) 

Free allocated storage for a publish packet.

Parameters:
pack pointer to the publish packet structure
void MQTTPacket_freeSuback ( Suback pack  ) 

Free allocated storage for a suback packet.

Parameters:
pack pointer to the suback packet structure

Here is the call graph for this function:

void* MQTTPacket_header_only ( unsigned char  aHeader,
char *  data,
size_t  datalen 
)

Function used in the new packets table to create packets which have only a header.

Parameters:
aHeader the MQTT header byte
data the rest of the packet
datalen the length of the rest of the packet
Returns:
pointer to the packet structure
const char* MQTTPacket_name ( int  ptype  ) 

Converts an MQTT packet code into its name.

Parameters:
ptype packet code
Returns:
the corresponding string, or "UNKNOWN"
void* MQTTPacket_publish ( unsigned char  aHeader,
char *  data,
size_t  datalen 
)

Function used in the new packets table to create publish packets.

Parameters:
aHeader the MQTT header byte
data the rest of the packet
datalen the length of the rest of the packet
Returns:
pointer to the packet structure

Here is the call graph for this function:

int MQTTPacket_send ( networkHandles net,
Header  header,
char *  buffer,
size_t  buflen,
int  free 
)

Sends an MQTT packet in one system call write.

Parameters:
socket the socket to which to write the data
header the one-byte MQTT header
buffer the rest of the buffer to write (not including remaining length)
buflen the length of the data in buffer to be written
Returns:
the completion code (TCPSOCKET_COMPLETE etc)

Here is the call graph for this function:

static int MQTTPacket_send_ack ( int  type,
int  msgid,
int  dup,
networkHandles net 
) [static]

Send an MQTT acknowledgement packet down a socket.

Parameters:
type the MQTT packet type e.g. SUBACK
msgid the MQTT message id to use
dup boolean - whether to set the MQTT DUP flag
net the network handle to send the data to
Returns:
the completion code (e.g. TCPSOCKET_COMPLETE)

Here is the call graph for this function:

int MQTTPacket_send_disconnect ( networkHandles net,
const char *  clientID 
)

Send an MQTT disconnect packet down a socket.

Parameters:
socket the open socket to send the data to
Returns:
the completion code (e.g. TCPSOCKET_COMPLETE)

Here is the call graph for this function:

int MQTTPacket_send_puback ( int  msgid,
networkHandles net,
const char *  clientID 
)

Send an MQTT PUBACK packet down a socket.

Parameters:
msgid the MQTT message id to use
socket the open socket to send the data to
clientID the string client identifier, only used for tracing
Returns:
the completion code (e.g. TCPSOCKET_COMPLETE)

Here is the call graph for this function:

int MQTTPacket_send_pubcomp ( int  msgid,
networkHandles net,
const char *  clientID 
)

Send an MQTT PUBCOMP packet down a socket.

Parameters:
msgid the MQTT message id to use
socket the open socket to send the data to
clientID the string client identifier, only used for tracing
Returns:
the completion code (e.g. TCPSOCKET_COMPLETE)

Here is the call graph for this function:

int MQTTPacket_send_publish ( Publish pack,
int  dup,
int  qos,
int  retained,
networkHandles net,
const char *  clientID 
)

Send an MQTT PUBLISH packet down a socket.

Parameters:
pack a structure from which to get some values to use, e.g topic, payload
dup boolean - whether to set the MQTT DUP flag
qos the value to use for the MQTT QoS setting
retained boolean - whether to set the MQTT retained flag
socket the open socket to send the data to
clientID the string client identifier, only used for tracing
Returns:
the completion code (e.g. TCPSOCKET_COMPLETE)

Here is the call graph for this function:

int MQTTPacket_send_pubrec ( int  msgid,
networkHandles net,
const char *  clientID 
)

Send an MQTT PUBREC packet down a socket.

Parameters:
msgid the MQTT message id to use
socket the open socket to send the data to
clientID the string client identifier, only used for tracing
Returns:
the completion code (e.g. TCPSOCKET_COMPLETE)

Here is the call graph for this function:

int MQTTPacket_send_pubrel ( int  msgid,
int  dup,
networkHandles net,
const char *  clientID 
)

Send an MQTT PUBREL packet down a socket.

Parameters:
msgid the MQTT message id to use
dup boolean - whether to set the MQTT DUP flag
socket the open socket to send the data to
clientID the string client identifier, only used for tracing
Returns:
the completion code (e.g. TCPSOCKET_COMPLETE)

Here is the call graph for this function:

int MQTTPacket_sends ( networkHandles net,
Header  header,
int  count,
char **  buffers,
size_t *  buflens,
int *  frees 
)

Sends an MQTT packet from multiple buffers in one system call write.

Parameters:
socket the socket to which to write the data
header the one-byte MQTT header
count the number of buffers
buffers the rest of the buffers to write (not including remaining length)
buflens the lengths of the data in the array of buffers to be written
Returns:
the completion code (TCPSOCKET_COMPLETE etc)

Here is the call graph for this function:

unsigned char readChar ( char **  pptr  ) 

Reads one character from the input buffer.

Parameters:
pptr pointer to the input buffer - incremented by the number of bytes used & returned
Returns:
the character read
int readInt ( char **  pptr  ) 

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

Parameters:
pptr pointer to the input buffer - incremented by the number of bytes used & returned
Returns:
the integer value calculated
char* readUTF ( char **  pptr,
char *  enddata 
)

Reads a "UTF" string from the input buffer.

UTF as in the MQTT v3 spec which really means a length delimited string. So it reads the two byte length then the data according to that length. The end of the buffer is provided too, so we can prevent buffer overruns caused by an incorrect length.

Parameters:
pptr pointer to the input buffer - incremented by the number of bytes used & returned
enddata pointer to the end of the buffer not to be read beyond
Returns:
an allocated C string holding the characters read, or NULL if the length read would have caused an overrun.

Here is the call graph for this function:

static char * readUTFlen ( char **  pptr,
char *  enddata,
int *  len 
) [static]

Reads a "UTF" string from the input buffer.

UTF as in the MQTT v3 spec which really means a length delimited string. So it reads the two byte length then the data according to that length. The end of the buffer is provided too, so we can prevent buffer overruns caused by an incorrect length.

Parameters:
pptr pointer to the input buffer - incremented by the number of bytes used & returned
enddata pointer to the end of the buffer not to be read beyond
len returns the calculcated value of the length bytes read
Returns:
an allocated C string holding the characters read, or NULL if the length read would have caused an overrun.

Here is the call graph for this function:

void writeChar ( char **  pptr,
char  c 
)

Writes one character to an output buffer.

Parameters:
pptr pointer to the output buffer - incremented by the number of bytes used & returned
c the character to write
void writeData ( char **  pptr,
const void *  data,
int  datalen 
)

Writes length delimited data to an output buffer.

Parameters:
pptr pointer to the output buffer - incremented by the number of bytes used & returned
data the data to write
datalen the length of the data to write

Here is the call graph for this function:

void writeInt ( char **  pptr,
int  anInt 
)

Writes an integer as 2 bytes to an output buffer.

Parameters:
pptr pointer to the output buffer - incremented by the number of bytes used & returned
anInt the integer to write
void writeUTF ( char **  pptr,
const char *  string 
)

Writes a "UTF" string to an output buffer.

Converts C string to length-delimited.

Parameters:
pptr pointer to the output buffer - incremented by the number of bytes used & returned
string the C string to write

Here is the call graph for this function:


Variable Documentation

Initial value:
{
        NULL,   
        NULL,   
        MQTTPacket_connack, 
        MQTTPacket_publish,     
        MQTTPacket_ack, 
        MQTTPacket_ack, 
        MQTTPacket_ack, 
        MQTTPacket_ack, 
        NULL, 
        MQTTPacket_suback, 
        NULL, 
        MQTTPacket_ack, 
        MQTTPacket_header_only, 
        MQTTPacket_header_only, 
        MQTTPacket_header_only  
}

Array of functions to build packets, indexed according to packet code.

const char* packet_names[] [static]
Initial value:
{
        "RESERVED", "CONNECT", "CONNACK", "PUBLISH", "PUBACK", "PUBREC", "PUBREL",
        "PUBCOMP", "SUBSCRIBE", "SUBACK", "UNSUBSCRIBE", "UNSUBACK",
        "PINGREQ", "PINGRESP", "DISCONNECT"
}

List of the predefined MQTT v3 packet names.

 All Data Structures Files Functions Variables Typedefs Defines

Generated on 30 Jul 2017 for MQTT C Client Libraries Internals by  doxygen 1.6.1