MQTT C Client Libraries Internals: MQTTPacket.c File Reference

| MQTT C Client Libraries Internals

Loading...
Searching...
No Matches
Macros | Functions | Variables
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 <ctype.h>`
`#include "Messages.h"`
`#include "StackTrace.h"`
`#include "WebSocket.h"`
`#include "MQTTTime.h"`
`#include <stdlib.h>`
`#include <string.h>`
`#include "Heap.h"`
Include dependency graph for MQTTPacket.c:

## Macros
#define | min(A, B)
#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 MQTTVersion, int type, int msgid, int dup, networkHandles *net)
| Send an MQTT acknowledgement packet down a socket.
void * | MQTTPacket_Factory (int MQTTVersion, networkHandles *net, int *error)
| Reads one MQTT packet from a socket.
int | MQTTPacket_send (networkHandles *net, Header header, char *buffer, size_t buflen, int freeData, int MQTTVersion)
| Sends an MQTT packet in one system call write.
int | MQTTPacket_sends (networkHandles *net, Header header, PacketBuffers *bufs, int MQTTVersion)
| 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 (int MQTTVersion, 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 (Clients *client, enum MQTTReasonCodes reason, MQTTProperties *props)
| Send an MQTT disconnect packet down a socket.
void * | MQTTPacket_publish (int MQTTVersion, 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.
void | MQTTPacket_freeAck (Ack *pack)
| Free allocated storage for an ack packet.
int | MQTTPacket_send_puback (int MQTTVersion, 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.
void | MQTTPacket_freeUnsuback (Unsuback *pack)
| Free allocated storage for a suback packet.
int | MQTTPacket_send_pubrec (int MQTTVersion, int msgid, networkHandles *net, const char *clientID)
| Send an MQTT PUBREC packet down a socket.
int | MQTTPacket_send_pubrel (int MQTTVersion, int msgid, int dup, networkHandles *net, const char *clientID)
| Send an MQTT PUBREL packet down a socket.
int | MQTTPacket_send_pubcomp (int MQTTVersion, int msgid, networkHandles *net, const char *clientID)
| Send an MQTT PUBCOMP packet down a socket.
void * | MQTTPacket_ack (int MQTTVersion, unsigned char aHeader, char *data, size_t datalen)
| Function used in the new packets table to create acknowledgement packets.
int | MQTTPacket_formatPayload (int buflen, char *buf, int payloadlen, char *payload)
| Format the payload for printing in the trace.
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.
void | writeInt4 (char **pptr, unsigned int anInt)
| Writes an integer as 4 bytes to an output buffer.
unsigned int | readInt4 (char **pptr)
| Calculates an integer from two bytes read from the input buffer.
void | writeMQTTLenString (char **pptr, MQTTLenString lenstring)
int | MQTTLenStringRead (MQTTLenString *lenstring, char **pptr, char *enddata)
int | MQTTPacket_VBIlen (int rem_len)
int | MQTTPacket_VBIdecode (int(*getcharfn)(char *, int), unsigned int *value)
| Decodes the message length according to the MQTT algorithm.
int | bufchar (char *c, int count)
int | MQTTPacket_decodeBuf (char *buf, unsigned int *value)

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

## 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

## Macro Definition Documentation

## ◆ min

#define min | ( |  | A,
|  |  | B )

Value:
( (A) < (B) ? (A):(B))

## Function Documentation

## ◆ MQTTPacket_ack()

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

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

Parameters

  MQTTVersion | the version of MQTT being used
  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:

## ◆ MQTTPacket_decode()

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

## ◆ MQTTPacket_encode()

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

## ◆ MQTTPacket_Factory()

void * MQTTPacket_Factory | ( | int | MQTTVersion,
|  | 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:

## ◆ MQTTPacket_formatPayload()

int MQTTPacket_formatPayload | ( | int | buflen,
|  | char * | buf,
|  | int | payloadlen,
|  | char * | payload )

Format the payload for printing in the trace.

Any unprintable characters output as hex.

Parameters

  buflen | the length of the supplied print buffer
  buf | the supplied print buffer
  payloadlen | the length of the payload to be printed
  payload | the payload data to be printed

Returns
  the length of the data output to the print buffer

## ◆ MQTTPacket_free_packet()

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:

## ◆ MQTTPacket_freeAck()

void MQTTPacket_freeAck | ( | Ack * | pack | ) |

Free allocated storage for an ack packet.

Parameters

  pack | pointer to the publish packet structure

## ◆ MQTTPacket_freePublish()

void MQTTPacket_freePublish | ( | Publish * | pack | ) |

Free allocated storage for a publish packet.

Parameters

  pack | pointer to the publish packet structure

## ◆ MQTTPacket_freeSuback()

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:

## ◆ MQTTPacket_freeUnsuback()

void MQTTPacket_freeUnsuback | ( | Unsuback * | pack | ) |

Free allocated storage for a suback packet.

Parameters

  pack | pointer to the suback packet structure

Here is the call graph for this function:

## ◆ MQTTPacket_header_only()

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

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

Parameters

  MQTTVersion | the version of MQTT
  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

## ◆ MQTTPacket_name()

const char * MQTTPacket_name | ( | int | ptype | ) |

Converts an MQTT packet code into its name.

Parameters

  ptype | packet code

Returns
  the corresponding string, or "UNKNOWN"

## ◆ MQTTPacket_publish()

void * MQTTPacket_publish | ( | int | MQTTVersion,
|  | unsigned char | aHeader,
|  | char * | data,
|  | size_t | datalen )

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

Parameters

  MQTTVersion |
  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:

## ◆ MQTTPacket_send()

int MQTTPacket_send | ( | networkHandles * | net,
|  | Header | header,
|  | char * | buffer,
|  | size_t | buflen,
|  | int | freeData,
|  | int | MQTTVersion )

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
  MQTTVersion | the version of MQTT being used

Returns
  the completion code (TCPSOCKET_COMPLETE etc)

Here is the call graph for this function:

## ◆ MQTTPacket_send_ack()

int MQTTPacket_send_ack | ( | int | MQTTVersion,
|  | int | type,
|  | int | msgid,
|  | int | dup,
|  | networkHandles * | net )
| static

Send an MQTT acknowledgement packet down a socket.

Parameters

  MQTTVersion | the version of MQTT being used
  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:

## ◆ MQTTPacket_send_disconnect()

int MQTTPacket_send_disconnect | ( | Clients * | client,
|  | enum MQTTReasonCodes | reason,
|  | MQTTProperties * | props )

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:

## ◆ MQTTPacket_send_puback()

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

Send an MQTT PUBACK packet down a socket.

Parameters

  MQTTVersion | the version of MQTT being used
  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:

## ◆ MQTTPacket_send_pubcomp()

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

Send an MQTT PUBCOMP packet down a socket.

Parameters

  MQTTVersion | the version of MQTT being used
  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:

## ◆ MQTTPacket_send_publish()

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:

## ◆ MQTTPacket_send_pubrec()

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

Send an MQTT PUBREC packet down a socket.

Parameters

  MQTTVersion | the version of MQTT being used
  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:

## ◆ MQTTPacket_send_pubrel()

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

Send an MQTT PUBREL packet down a socket.

Parameters

  MQTTVersion | the version of MQTT being used
  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:

## ◆ MQTTPacket_sends()

int MQTTPacket_sends | ( | networkHandles * | net,
|  | Header | header,
|  | PacketBuffers * | bufs,
|  | int | MQTTVersion )

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
  the | MQTT version being used

Returns
  the completion code (TCPSOCKET_COMPLETE etc)

Here is the call graph for this function:

## ◆ MQTTPacket_VBIdecode()

int MQTTPacket_VBIdecode | ( | int(* | getcharfn )(char *, int),
|  | unsigned int * | value )

Decodes the message length according to the MQTT algorithm.

Parameters

  getcharfn | pointer to function to read the next character from the data source
  value | the decoded length returned

Returns
  the number of bytes read from the socket

## ◆ readChar()

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

## ◆ readInt()

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

## ◆ readInt4()

unsigned int readInt4 | ( | 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

## ◆ readUTF()

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:

## ◆ readUTFlen()

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:

## ◆ writeChar()

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

## ◆ writeData()

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:

## ◆ writeInt()

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

## ◆ writeInt4()

void writeInt4 | ( | char ** | pptr,
|  | unsigned int | anInt )

Writes an integer as 4 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

## ◆ writeUTF()

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

## ◆ new_packets

pf new_packets[]

Initial value:
=
{
NULL,
NULL,
MQTTPacket_connack,
MQTTPacket_publish,
MQTTPacket_ack,
MQTTPacket_ack,
MQTTPacket_ack,
MQTTPacket_ack,
NULL,
MQTTPacket_suback,
NULL,
MQTTPacket_unsuback,
MQTTPacket_header_only,
MQTTPacket_header_only,
MQTTPacket_ack,
MQTTPacket_ack
}
MQTTPacket_connack
void * MQTTPacket_connack(int MQTTVersion, unsigned char aHeader, char *data, size_t datalen)
Function used in the new packets table to create connack packets.
Definition MQTTPacketOut.c:142
MQTTPacket_suback
void * MQTTPacket_suback(int MQTTVersion, unsigned char aHeader, char *data, size_t datalen)
Function used in the new packets table to create suback packets.
Definition MQTTPacketOut.c:293
MQTTPacket_unsuback
void * MQTTPacket_unsuback(int MQTTVersion, unsigned char aHeader, char *data, size_t datalen)
Function used in the new packets table to create unsuback packets.
Definition MQTTPacketOut.c:414
MQTTPacket_header_only
void * MQTTPacket_header_only(int MQTTVersion, unsigned char aHeader, char *data, size_t datalen)
Function used in the new packets table to create packets which have only a header.
Definition MQTTPacket.c:500
MQTTPacket_ack
void * MQTTPacket_ack(int MQTTVersion, unsigned char aHeader, char *data, size_t datalen)
Function used in the new packets table to create acknowledgement packets.
Definition MQTTPacket.c:796
MQTTPacket_publish
void * MQTTPacket_publish(int MQTTVersion, unsigned char aHeader, char *data, size_t datalen)
Function used in the new packets table to create publish packets.
Definition MQTTPacket.c:558

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

## ◆ packet_names

const char* packet_names[]
| static

Initial value:
=
{
"RESERVED", "CONNECT", "CONNACK", "PUBLISH", "PUBACK", "PUBREC", "PUBREL",
"PUBCOMP", "SUBSCRIBE", "SUBACK", "UNSUBSCRIBE", "UNSUBACK",
"PINGREQ", "PINGRESP", "DISCONNECT", "AUTH"
}

List of the predefined MQTT v3/v5 packet names.