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 "WebSocket.h"
#include "MQTTTime.h"
#include <stdlib.h>
#include <string.h>
#include "Heap.h"
Go to the source code of this file.
Macros | |
#define | MAX_NO_OF_REMAINING_LENGTH_BYTES 4 |
#define | MAX_NO_OF_REMAINING_LENGTH_BYTES 4 |
#define | min(A, B) ( (A) < (B) ? (A):(B)) |
Functions | |
int | bufchar (char *c, int count) |
int | MQTTLenStringRead (MQTTLenString *lenstring, char **pptr, char *enddata) |
void * | MQTTPacket_ack (int MQTTVersion, unsigned char aHeader, char *data, size_t datalen) |
int | MQTTPacket_decode (networkHandles *net, size_t *value) |
int | MQTTPacket_decodeBuf (char *buf, unsigned int *value) |
int | MQTTPacket_encode (char *buf, size_t length) |
void * | MQTTPacket_Factory (int MQTTVersion, networkHandles *net, int *error) |
void | MQTTPacket_free_packet (MQTTPacket *pack) |
void | MQTTPacket_freeAck (Ack *pack) |
void | MQTTPacket_freePublish (Publish *pack) |
void | MQTTPacket_freeSuback (Suback *pack) |
void | MQTTPacket_freeUnsuback (Unsuback *pack) |
void * | MQTTPacket_header_only (int MQTTVersion, unsigned char aHeader, char *data, size_t datalen) |
const char * | MQTTPacket_name (int ptype) |
void * | MQTTPacket_publish (int MQTTVersion, unsigned char aHeader, char *data, size_t datalen) |
int | MQTTPacket_send (networkHandles *net, Header header, char *buffer, size_t buflen, int freeData, int MQTTVersion) |
static int | MQTTPacket_send_ack (int MQTTVersion, int type, int msgid, int dup, networkHandles *net) |
int | MQTTPacket_send_disconnect (Clients *client, enum MQTTReasonCodes reason, MQTTProperties *props) |
int | MQTTPacket_send_puback (int MQTTVersion, int msgid, networkHandles *net, const char *clientID) |
int | MQTTPacket_send_pubcomp (int MQTTVersion, int msgid, networkHandles *net, const char *clientID) |
int | MQTTPacket_send_publish (Publish *pack, int dup, int qos, int retained, networkHandles *net, const char *clientID) |
int | MQTTPacket_send_pubrec (int MQTTVersion, int msgid, networkHandles *net, const char *clientID) |
int | MQTTPacket_send_pubrel (int MQTTVersion, int msgid, int dup, networkHandles *net, const char *clientID) |
int | MQTTPacket_sends (networkHandles *net, Header header, PacketBuffers *bufs, int MQTTVersion) |
int | MQTTPacket_VBIdecode (int(*getcharfn)(char *, int), unsigned int *value) |
int | MQTTPacket_VBIlen (int rem_len) |
unsigned char | readChar (char **pptr) |
int | readInt (char **pptr) |
int | readInt4 (char **pptr) |
char * | readUTF (char **pptr, char *enddata) |
static char * | readUTFlen (char **pptr, char *enddata, int *len) |
void | writeChar (char **pptr, char c) |
void | writeData (char **pptr, const void *data, int datalen) |
void | writeInt (char **pptr, int anInt) |
void | writeInt4 (char **pptr, int anInt) |
void | writeMQTTLenString (char **pptr, MQTTLenString lenstring) |
void | writeUTF (char **pptr, const char *string) |
Variables | |
static char * | bufptr |
const char ** | MQTTClient_packet_names = packet_names |
pf | new_packets [] |
static const char * | packet_names [] |
functions to deal with reading and writing of MQTT packets from and to sockets
Some other related functions are in the MQTTPacketOut module
Definition in file MQTTPacket.c.
#define MAX_NO_OF_REMAINING_LENGTH_BYTES 4 |
#define MAX_NO_OF_REMAINING_LENGTH_BYTES 4 |
#define min | ( | A, | |
B | |||
) | ( (A) < (B) ? (A):(B)) |
Definition at line 44 of file MQTTPacket.c.
int bufchar | ( | char * | c, |
int | count | ||
) |
Definition at line 1042 of file MQTTPacket.c.
int MQTTLenStringRead | ( | MQTTLenString * | lenstring, |
char ** | pptr, | ||
char * | enddata | ||
) |
Definition at line 962 of file MQTTPacket.c.
void* MQTTPacket_ack | ( | int | MQTTVersion, |
unsigned char | aHeader, | ||
char * | data, | ||
size_t | datalen | ||
) |
Function used in the new packets table to create acknowledgement packets.
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 |
Definition at line 783 of file MQTTPacket.c.
int MQTTPacket_decode | ( | networkHandles * | net, |
size_t * | value | ||
) |
Decodes the message length according to the MQTT algorithm
socket | the socket from which to read the bytes |
value | the decoded length returned |
Definition at line 325 of file MQTTPacket.c.
int MQTTPacket_decodeBuf | ( | char * | buf, |
unsigned int * | value | ||
) |
Definition at line 1052 of file MQTTPacket.c.
int MQTTPacket_encode | ( | char * | buf, |
size_t | length | ||
) |
Encodes the message length according to the MQTT algorithm
buf | the buffer into which the encoded data is written |
length | the length to be encoded |
Definition at line 297 of file MQTTPacket.c.
void* MQTTPacket_Factory | ( | int | MQTTVersion, |
networkHandles * | net, | ||
int * | error | ||
) |
Reads one MQTT packet from a socket.
socket | a socket from which to read an MQTT packet |
error | pointer to the error code which is completed if no packet is returned |
Definition at line 103 of file MQTTPacket.c.
void MQTTPacket_free_packet | ( | MQTTPacket * | pack | ) |
Free allocated storage for a various packet tyoes
pack | pointer to the suback packet structure |
Definition at line 905 of file MQTTPacket.c.
void MQTTPacket_freeAck | ( | Ack * | pack | ) |
Free allocated storage for an ack packet.
pack | pointer to the publish packet structure |
Definition at line 617 of file MQTTPacket.c.
void MQTTPacket_freePublish | ( | Publish * | pack | ) |
Free allocated storage for a publish packet.
pack | pointer to the publish packet structure |
Definition at line 601 of file MQTTPacket.c.
void MQTTPacket_freeSuback | ( | Suback * | pack | ) |
Free allocated storage for a suback packet.
pack | pointer to the suback packet structure |
Definition at line 684 of file MQTTPacket.c.
void MQTTPacket_freeUnsuback | ( | Unsuback * | pack | ) |
Free allocated storage for a suback packet.
pack | pointer to the suback packet structure |
Definition at line 700 of file MQTTPacket.c.
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.
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 |
Definition at line 495 of file MQTTPacket.c.
const char* MQTTPacket_name | ( | int | ptype | ) |
Converts an MQTT packet code into its name
ptype | packet code |
Definition at line 65 of file MQTTPacket.c.
void* MQTTPacket_publish | ( | int | MQTTVersion, |
unsigned char | aHeader, | ||
char * | data, | ||
size_t | datalen | ||
) |
Function used in the new packets table to create publish packets.
MQTTVersion | |
aHeader | the MQTT header byte |
data | the rest of the packet |
datalen | the length of the rest of the packet |
Definition at line 553 of file MQTTPacket.c.
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
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 |
Definition at line 190 of file MQTTPacket.c.
|
static |
Send an MQTT acknowledgement packet down a socket.
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 |
Definition at line 636 of file MQTTPacket.c.
int MQTTPacket_send_disconnect | ( | Clients * | client, |
enum MQTTReasonCodes | reason, | ||
MQTTProperties * | props | ||
) |
Send an MQTT disconnect packet down a socket.
socket | the open socket to send the data to |
Definition at line 508 of file MQTTPacket.c.
int MQTTPacket_send_puback | ( | int | MQTTVersion, |
int | msgid, | ||
networkHandles * | net, | ||
const char * | clientID | ||
) |
Send an MQTT PUBACK packet down a socket.
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 |
Definition at line 668 of file MQTTPacket.c.
int MQTTPacket_send_pubcomp | ( | int | MQTTVersion, |
int | msgid, | ||
networkHandles * | net, | ||
const char * | clientID | ||
) |
Send an MQTT PUBCOMP packet down a socket.
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 |
Definition at line 763 of file MQTTPacket.c.
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.
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 |
Definition at line 835 of file MQTTPacket.c.
int MQTTPacket_send_pubrec | ( | int | MQTTVersion, |
int | msgid, | ||
networkHandles * | net, | ||
const char * | clientID | ||
) |
Send an MQTT PUBREC packet down a socket.
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 |
Definition at line 722 of file MQTTPacket.c.
int MQTTPacket_send_pubrel | ( | int | MQTTVersion, |
int | msgid, | ||
int | dup, | ||
networkHandles * | net, | ||
const char * | clientID | ||
) |
Send an MQTT PUBREL packet down a socket.
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 |
Definition at line 743 of file MQTTPacket.c.
int MQTTPacket_sends | ( | networkHandles * | net, |
Header | header, | ||
PacketBuffers * | bufs, | ||
int | MQTTVersion | ||
) |
Sends an MQTT packet from multiple buffers in one system call write
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 |
Definition at line 250 of file MQTTPacket.c.
int MQTTPacket_VBIdecode | ( | int(*)(char *, int) | getcharfn, |
unsigned int * | value | ||
) |
Decodes the message length according to the MQTT algorithm
getcharfn | pointer to function to read the next character from the data source |
value | the decoded length returned |
Definition at line 1012 of file MQTTPacket.c.
int MQTTPacket_VBIlen | ( | int | rem_len | ) |
Definition at line 990 of file MQTTPacket.c.
unsigned char readChar | ( | char ** | pptr | ) |
Reads one character from the input buffer.
pptr | pointer to the input buffer - incremented by the number of bytes used & returned |
Definition at line 425 of file MQTTPacket.c.
int readInt | ( | char ** | pptr | ) |
Calculates an integer from two bytes read from the input buffer
pptr | pointer to the input buffer - incremented by the number of bytes used & returned |
Definition at line 359 of file MQTTPacket.c.
int readInt4 | ( | char ** | pptr | ) |
Calculates an integer from two bytes read from the input buffer
pptr | pointer to the input buffer - incremented by the number of bytes used & returned |
Definition at line 945 of file MQTTPacket.c.
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.
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 |
Definition at line 413 of file MQTTPacket.c.
|
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.
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 |
Definition at line 380 of file MQTTPacket.c.
void writeChar | ( | char ** | pptr, |
char | c | ||
) |
Writes one character to an output buffer.
pptr | pointer to the output buffer - incremented by the number of bytes used & returned |
c | the character to write |
Definition at line 438 of file MQTTPacket.c.
void writeData | ( | char ** | pptr, |
const void * | data, | ||
int | datalen | ||
) |
Writes length delimited data to an output buffer
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 |
Definition at line 479 of file MQTTPacket.c.
void writeInt | ( | char ** | pptr, |
int | anInt | ||
) |
Writes an integer as 2 bytes to an output buffer.
pptr | pointer to the output buffer - incremented by the number of bytes used & returned |
anInt | the integer to write |
Definition at line 450 of file MQTTPacket.c.
void writeInt4 | ( | char ** | pptr, |
int | anInt | ||
) |
Writes an integer as 4 bytes to an output buffer.
pptr | pointer to the output buffer - incremented by the number of bytes used & returned |
anInt | the integer to write |
Definition at line 925 of file MQTTPacket.c.
void writeMQTTLenString | ( | char ** | pptr, |
MQTTLenString | lenstring | ||
) |
Definition at line 954 of file MQTTPacket.c.
void writeUTF | ( | char ** | pptr, |
const char * | string | ||
) |
Writes a "UTF" string to an output buffer. Converts C string to length-delimited.
pptr | pointer to the output buffer - incremented by the number of bytes used & returned |
string | the C string to write |
Definition at line 464 of file MQTTPacket.c.
|
static |
Definition at line 1040 of file MQTTPacket.c.
const char** MQTTClient_packet_names = packet_names |
Definition at line 57 of file MQTTPacket.c.
pf new_packets[] |
Array of functions to build packets, indexed according to packet code
Definition at line 73 of file MQTTPacket.c.
|
static |
List of the predefined MQTT v3/v5 packet names.
Definition at line 50 of file MQTTPacket.c.