1#if !defined(__MQTT_H__)
28#if defined(__cplusplus)
38#if defined(MQTTC_PAL_FILE)
39#define MQTTC_STR2(x) #x
40#define MQTTC_STR(x) MQTTC_STR2(x)
41#include MQTTC_STR(MQTTC_PAL_FILE)
132 MQTT_CONTROL_CONNECT=1u,
133 MQTT_CONTROL_CONNACK=2u,
134 MQTT_CONTROL_PUBLISH=3u,
135 MQTT_CONTROL_PUBACK=4u,
136 MQTT_CONTROL_PUBREC=5u,
137 MQTT_CONTROL_PUBREL=6u,
138 MQTT_CONTROL_PUBCOMP=7u,
139 MQTT_CONTROL_SUBSCRIBE=8u,
140 MQTT_CONTROL_SUBACK=9u,
141 MQTT_CONTROL_UNSUBSCRIBE=10u,
142 MQTT_CONTROL_UNSUBACK=11u,
143 MQTT_CONTROL_PINGREQ=12u,
144 MQTT_CONTROL_PINGRESP=13u,
145 MQTT_CONTROL_DISCONNECT=14u
175#define MQTT_PROTOCOL_LEVEL 0x04
181#define __ALL_MQTT_ERRORS(MQTT_ERROR) \
182 MQTT_ERROR(MQTT_ERROR_NULLPTR) \
183 MQTT_ERROR(MQTT_ERROR_CONTROL_FORBIDDEN_TYPE) \
184 MQTT_ERROR(MQTT_ERROR_CONTROL_INVALID_FLAGS) \
185 MQTT_ERROR(MQTT_ERROR_CONTROL_WRONG_TYPE) \
186 MQTT_ERROR(MQTT_ERROR_CONNECT_CLIENT_ID_REFUSED) \
187 MQTT_ERROR(MQTT_ERROR_CONNECT_NULL_WILL_MESSAGE) \
188 MQTT_ERROR(MQTT_ERROR_CONNECT_FORBIDDEN_WILL_QOS) \
189 MQTT_ERROR(MQTT_ERROR_CONNACK_FORBIDDEN_FLAGS) \
190 MQTT_ERROR(MQTT_ERROR_CONNACK_FORBIDDEN_CODE) \
191 MQTT_ERROR(MQTT_ERROR_PUBLISH_FORBIDDEN_QOS) \
192 MQTT_ERROR(MQTT_ERROR_SUBSCRIBE_TOO_MANY_TOPICS) \
193 MQTT_ERROR(MQTT_ERROR_MALFORMED_RESPONSE) \
194 MQTT_ERROR(MQTT_ERROR_UNSUBSCRIBE_TOO_MANY_TOPICS) \
195 MQTT_ERROR(MQTT_ERROR_RESPONSE_INVALID_CONTROL_TYPE) \
196 MQTT_ERROR(MQTT_ERROR_CONNECT_NOT_CALLED) \
197 MQTT_ERROR(MQTT_ERROR_SEND_BUFFER_IS_FULL) \
198 MQTT_ERROR(MQTT_ERROR_SOCKET_ERROR) \
199 MQTT_ERROR(MQTT_ERROR_MALFORMED_REQUEST) \
200 MQTT_ERROR(MQTT_ERROR_RECV_BUFFER_TOO_SMALL) \
201 MQTT_ERROR(MQTT_ERROR_ACK_OF_UNKNOWN) \
202 MQTT_ERROR(MQTT_ERROR_NOT_IMPLEMENTED) \
203 MQTT_ERROR(MQTT_ERROR_CONNECTION_REFUSED) \
204 MQTT_ERROR(MQTT_ERROR_SUBSCRIBE_FAILED) \
205 MQTT_ERROR(MQTT_ERROR_CONNECTION_CLOSED) \
206 MQTT_ERROR(MQTT_ERROR_INITIAL_RECONNECT) \
207 MQTT_ERROR(MQTT_ERROR_INVALID_REMAINING_LENGTH) \
208 MQTT_ERROR(MQTT_ERROR_CLEAN_SESSION_IS_REQUIRED) \
209 MQTT_ERROR(MQTT_ERROR_RECONNECT_FAILED) \
210 MQTT_ERROR(MQTT_ERROR_RECONNECTING)
219#define GENERATE_ENUM(ENUM) ENUM,
226#define GENERATE_STRING(STRING) #STRING,
236 MQTT_ERROR_UNKNOWN=INT_MIN,
287#define __mqtt_packed_cstrlen(x) (2 + (unsigned int)strlen(x))
300 MQTT_CONNACK_ACCEPTED = 0u,
301 MQTT_CONNACK_REFUSED_PROTOCOL_VERSION = 1u,
302 MQTT_CONNACK_REFUSED_IDENTIFIER_REJECTED = 2u,
303 MQTT_CONNACK_REFUSED_SERVER_UNAVAILABLE = 3u,
304 MQTT_CONNACK_REFUSED_BAD_USER_NAME_OR_PASSWORD = 4u,
305 MQTT_CONNACK_REFUSED_NOT_AUTHORIZED = 5u
447 MQTT_SUBACK_SUCCESS_MAX_QOS_0 = 0u,
448 MQTT_SUBACK_SUCCESS_MAX_QOS_1 = 1u,
449 MQTT_SUBACK_SUCCESS_MAX_QOS_2 = 2u,
450 MQTT_SUBACK_FAILURE = 128u
677 MQTT_CONNECT_RESERVED = 1u,
678 MQTT_CONNECT_CLEAN_SESSION = 2u,
679 MQTT_CONNECT_WILL_FLAG = 4u,
680 MQTT_CONNECT_WILL_QOS_0 = (0u & 0x03) << 3,
681 MQTT_CONNECT_WILL_QOS_1 = (1u & 0x03) << 3,
682 MQTT_CONNECT_WILL_QOS_2 = (2u & 0x03) << 3,
683 MQTT_CONNECT_WILL_RETAIN = 32u,
684 MQTT_CONNECT_PASSWORD = 64u,
685 MQTT_CONNECT_USER_NAME = 128u
727 const char* client_id,
728 const char* will_topic,
729 const void* will_message,
730 size_t will_message_size,
731 const char* user_name,
732 const char* password,
733 uint8_t connect_flags,
734 uint16_t keep_alive);
745 MQTT_PUBLISH_DUP = 8u,
746 MQTT_PUBLISH_QOS_0 = ((0u << 1) & 0x06),
747 MQTT_PUBLISH_QOS_1 = ((1u << 1) & 0x06),
748 MQTT_PUBLISH_QOS_2 = ((2u << 1) & 0x06),
749 MQTT_PUBLISH_QOS_MASK = ((3u << 1) & 0x06),
750 MQTT_PUBLISH_RETAIN = 0x01
777 const char* topic_name,
779 const void* application_message,
780 size_t application_message_size,
781 uint8_t publish_flags);
822#define MQTT_SUBSCRIBE_REQUEST_MAX_NUM_TOPICS 8
847 unsigned int packet_id,
857#define MQTT_UNSUBSCRIBE_REQUEST_MAX_NUM_TOPICS 8
881 unsigned int packet_id,
923 MQTT_QUEUED_AWAITING_ACK,
1073#define mqtt_mq_get(mq_ptr, index) (((struct mqtt_queued_message*) ((mq_ptr)->mem_end)) - 1 - index)
1079#define mqtt_mq_length(mq_ptr) (((struct mqtt_queued_message*) ((mq_ptr)->mem_end)) - (mq_ptr)->queue_tail)
1085#define mqtt_mq_currsz(mq_ptr) (((mq_ptr)->curr >= (uint8_t*) ((mq_ptr)->queue_tail - 1)) ? 0 : ((uint8_t*) ((mq_ptr)->queue_tail - 1)) - (mq_ptr)->curr)
1340 mqtt_pal_socket_handle sockfd,
1341 uint8_t *sendbuf,
size_t sendbufsz,
1342 uint8_t *recvbuf,
size_t recvbufsz,
1390 void (*reconnect_callback)(
struct mqtt_client *client,
void** state),
1391 void *reconnect_state,
1417 mqtt_pal_socket_handle socketfd,
1418 uint8_t *sendbuf,
size_t sendbufsz,
1419 uint8_t *recvbuf,
size_t recvbufsz);
1448 const char* client_id,
1449 const char* will_topic,
1450 const void* will_message,
1451 size_t will_message_size,
1452 const char* user_name,
1453 const char* password,
1454 uint8_t connect_flags,
1455 uint16_t keep_alive);
1480 const char* topic_name,
1481 const void* application_message,
1482 size_t application_message_size,
1483 uint8_t publish_flags);
1544 const char* topic_name,
1559 const char* topic_name);
1610#if defined(__cplusplus)
enum MQTTErrors mqtt_unsubscribe(struct mqtt_client *client, const char *topic_name)
Unsubscribe from a topic.
enum MQTTErrors mqtt_reconnect(struct mqtt_client *client)
Terminate the session with the MQTT broker and prepare to reconnect. Client code should call mqtt_syn...
void mqtt_init_reconnect(struct mqtt_client *client, void(*reconnect_callback)(struct mqtt_client *client, void **state), void *reconnect_state, void(*publish_response_callback)(void **state, struct mqtt_response_publish *publish))
Initializes an MQTT client and enables automatic reconnections.
enum MQTTErrors mqtt_disconnect(struct mqtt_client *client)
Terminate the session with the MQTT broker.
enum MQTTErrors mqtt_init(struct mqtt_client *client, mqtt_pal_socket_handle sockfd, uint8_t *sendbuf, size_t sendbufsz, uint8_t *recvbuf, size_t recvbufsz, void(*publish_response_callback)(void **state, struct mqtt_response_publish *publish))
Initializes an MQTT client.
MQTTErrors
An enumeration of error codes. Error messages can be retrieved by calling mqtt_error_str.
Definition mqtt.h:235
const char * mqtt_error_str(enum MQTTErrors error)
Returns an error message for error code, error.
MQTTPublishFlags
An enumeration of the PUBLISH flags.
Definition mqtt.h:744
MQTTConnectFlags
An enumeration of CONNECT packet flags.
Definition mqtt.h:676
enum MQTTErrors mqtt_connect(struct mqtt_client *client, const char *client_id, const char *will_topic, const void *will_message, size_t will_message_size, const char *user_name, const char *password, uint8_t connect_flags, uint16_t keep_alive)
Establishes a session with the MQTT broker.
enum MQTTErrors mqtt_sync(struct mqtt_client *client)
Function that does the actual sending and receiving of traffic from the network.
enum MQTTErrors mqtt_ping(struct mqtt_client *client)
Ping the broker.
enum MQTTErrors mqtt_subscribe(struct mqtt_client *client, const char *topic_name, int max_qos_level)
Subscribe to a topic.
void mqtt_reinit(struct mqtt_client *client, mqtt_pal_socket_handle socketfd, uint8_t *sendbuf, size_t sendbufsz, uint8_t *recvbuf, size_t recvbufsz)
Safely assign/reassign a socket and buffers to an new/existing client.
enum MQTTErrors mqtt_publish(struct mqtt_client *client, const char *topic_name, const void *application_message, size_t application_message_size, uint8_t publish_flags)
Publish an application message.
void mqtt_mq_init(struct mqtt_message_queue *mq, void *buf, size_t bufsz)
Initialize a message queue.
MQTTQueuedMessageState
An enumeration of queued message states.
Definition mqtt.h:921
ssize_t __mqtt_pubrel(struct mqtt_client *client, uint16_t packet_id)
Acknowledge an ingree PUBREC packet.
ssize_t __mqtt_recv(struct mqtt_client *client)
Handles ingress client traffic.
uint16_t __mqtt_next_pid(struct mqtt_client *client)
Generate a new next packet ID.
struct mqtt_queued_message * mqtt_mq_find(const struct mqtt_message_queue *mq, enum MQTTControlPacketType control_type, const uint16_t *packet_id)
Find a message in the message queue.
ssize_t __mqtt_pubcomp(struct mqtt_client *client, uint16_t packet_id)
Acknowledge an ingree PUBREL packet.
ssize_t __mqtt_pubrec(struct mqtt_client *client, uint16_t packet_id)
Acknowledge an ingree publish with QOS==2.
struct mqtt_queued_message * mqtt_mq_register(struct mqtt_message_queue *mq, size_t nbytes)
Register a message that was just added to the buffer.
ssize_t __mqtt_send(struct mqtt_client *client)
Handles egress client traffic.
void mqtt_mq_clean(struct mqtt_message_queue *mq)
Clear as many messages from the front of the queue as possible.
ssize_t __mqtt_puback(struct mqtt_client *client, uint16_t packet_id)
Acknowledge an ingree publish with QOS==1.
ssize_t mqtt_pack_connection_request(uint8_t *buf, size_t bufsz, const char *client_id, const char *will_topic, const void *will_message, size_t will_message_size, const char *user_name, const char *password, uint8_t connect_flags, uint16_t keep_alive)
Serialize a connection request into a buffer.
ssize_t mqtt_pack_subscribe_request(uint8_t *buf, size_t bufsz, unsigned int packet_id,...)
Serialize a SUBSCRIBE packet and put it in buf.
ssize_t mqtt_pack_fixed_header(uint8_t *buf, size_t bufsz, const struct mqtt_fixed_header *fixed_header)
Serialize an mqtt_fixed_header and write it to buf.
ssize_t mqtt_pack_disconnect(uint8_t *buf, size_t bufsz)
Serialize a DISCONNECT and put it into buf.
ssize_t mqtt_pack_pubxxx_request(uint8_t *buf, size_t bufsz, enum MQTTControlPacketType control_type, uint16_t packet_id)
Serialize a PUBACK, PUBREC, PUBREL, or PUBCOMP packet and put it in buf.
ssize_t mqtt_pack_unsubscribe_request(uint8_t *buf, size_t bufsz, unsigned int packet_id,...)
Serialize a UNSUBSCRIBE packet and put it in buf.
ssize_t mqtt_pack_ping_request(uint8_t *buf, size_t bufsz)
Serialize a PINGREQ and put it into buf.
ssize_t mqtt_pack_publish_request(uint8_t *buf, size_t bufsz, const char *topic_name, uint16_t packet_id, const void *application_message, size_t application_message_size, uint8_t publish_flags)
Serialize a PUBLISH request and put it in buf.
void mqtt_pal_mutex_t
type of the argument that is passed to MQTT_PAL_MUTEX_INIT(), MQTT_PAL_MUTEX_LOCK() and MQTT_PAL_MUTE...
Definition mqtt_pal.h:117
void mqtt_pal_time_t
return type of MQTT_PAL_TIME()
Definition mqtt_pal.h:110
MQTTConnackReturnCode
An enumeration of the return codes returned in a CONNACK packet.
Definition mqtt.h:299
MQTTSubackReturnCodes
An enumeration of subscription acknowledgement return codes.
Definition mqtt.h:446
ssize_t mqtt_unpack_pubxxx_response(struct mqtt_response *mqtt_response, const uint8_t *buf)
Deserialize a PUBACK/PUBREC/PUBREL/PUBCOMP packet from buf.
ssize_t mqtt_unpack_connack_response(struct mqtt_response *mqtt_response, const uint8_t *buf)
Deserialize a CONNACK response from buf.
ssize_t mqtt_unpack_response(struct mqtt_response *response, const uint8_t *buf, size_t bufsz)
Deserialize a packet from the broker.
MQTTControlPacketType
An enumeration of the MQTT control packet types.
Definition mqtt.h:131
ssize_t mqtt_unpack_fixed_header(struct mqtt_response *response, const uint8_t *buf, size_t bufsz)
Deserialize the contents of buf into an mqtt_fixed_header object.
ssize_t mqtt_unpack_publish_response(struct mqtt_response *mqtt_response, const uint8_t *buf)
Deserialize a publish response from buf.
uint16_t __mqtt_unpack_uint16(const uint8_t *buf)
Unpack a MQTT 16 bit integer to a native 16 bit integer.
ssize_t __mqtt_pack_str(uint8_t *buf, const char *str)
Pack a MQTT string, given a c-string str.
ssize_t __mqtt_pack_uint16(uint8_t *buf, uint16_t integer)
Pack a MQTT 16 bit integer, given a native 16 bit integer .
enum MQTTErrors __mqtt_ping(struct mqtt_client *client)
Ping the broker without locking/unlocking the mutex.
#define __ALL_MQTT_ERRORS(MQTT_ERROR)
A macro used to declare the enum MQTTErrors and associated error messages (the members of the num) at...
Definition mqtt.h:181
#define GENERATE_ENUM(ENUM)
A macro used to generate the enum MQTTErrors from __ALL_MQTT_ERRORS.
Definition mqtt.h:219
Includes/supports the types/calls required by the MQTT-C client.
An MQTT client.
Definition mqtt.h:1096
struct mqtt_client::@101300130277142200034304023172167257062204236336 recv_buffer
The buffer where ingress data is temporarily stored.
void(* reconnect_callback)(struct mqtt_client *, void **)
A callback that is called whenever the client is in an error state.
Definition mqtt.h:1204
enum MQTTErrors(* inspector_callback)(struct mqtt_client *)
A user-specified callback, triggered on each mqtt_sync, allowing the user to perform state inspection...
Definition mqtt.h:1195
uint8_t * mem_start
The start of the receive buffer's memory.
Definition mqtt.h:1217
void(* publish_response_callback)(void **state, struct mqtt_response_publish *publish)
The callback that is called whenever a publish is received from the broker.
Definition mqtt.h:1170
mqtt_pal_socket_handle socketfd
The socket connecting to the MQTT broker.
Definition mqtt.h:1098
int response_timeout
The timeout period in seconds.
Definition mqtt.h:1146
struct mqtt_message_queue mq
The sending message queue.
Definition mqtt.h:1238
float typical_response_time
Approximately much time it has typically taken to receive responses from the broker.
Definition mqtt.h:1157
mqtt_pal_mutex_t mutex
A variable passed to support thread-safety.
Definition mqtt.h:1235
uint16_t pid_lfsr
The LFSR state used to generate packet ID's.
Definition mqtt.h:1101
void * publish_response_callback_state
A pointer to any publish_response_callback state information you need.
Definition mqtt.h:1178
enum MQTTErrors error
The error state of the client.
Definition mqtt.h:1136
mqtt_pal_time_t time_of_last_send
The timestamp of the last message sent to the buffer.
Definition mqtt.h:1126
void * reconnect_state
A pointer to some state. A pointer to this member is passed to mqtt_client::reconnect_callback.
Definition mqtt.h:1210
size_t send_offset
The current sent offset.
Definition mqtt.h:1117
int number_of_keep_alives
A counter counting pings that have been sent to keep the connection alive.
Definition mqtt.h:1110
size_t mem_size
The size of the receive buffer's memory.
Definition mqtt.h:1220
int number_of_timeouts
A counter counting the number of timeouts that have occurred.
Definition mqtt.h:1149
uint8_t * curr
A pointer to the next writable location in the receive buffer.
Definition mqtt.h:1223
size_t curr_sz
The number of bytes that are still writable at curr.
Definition mqtt.h:1226
uint16_t keep_alive
The keep-alive time in seconds.
Definition mqtt.h:1104
A message queue.
Definition mqtt.h:971
void * mem_end
The end of the message queue's memory block.
Definition mqtt.h:980
size_t curr_sz
The number of bytes that can be written to curr.
Definition mqtt.h:998
void * mem_start
The start of the message queue's memory block.
Definition mqtt.h:977
uint8_t * curr
A pointer to the position in the buffer you can pack bytes at.
Definition mqtt.h:988
struct mqtt_queued_message * queue_tail
The tail of the array of mqtt_queued_messages's.
Definition mqtt.h:1005
A message in a mqtt_message_queue.
Definition mqtt.h:931
enum MQTTQueuedMessageState state
The state of the message.
Definition mqtt.h:940
mqtt_pal_time_t time_sent
The time at which the message was sent..
Definition mqtt.h:948
size_t size
The number of bytes in the message.
Definition mqtt.h:936
enum MQTTControlPacketType control_type
The control type of the message.
Definition mqtt.h:953
uint8_t * start
A pointer to the start of the message.
Definition mqtt.h:933
uint16_t packet_id
The packet id of the message.
Definition mqtt.h:961
A connection response datastructure.
Definition mqtt.h:316
enum MQTTConnackReturnCode return_code
The return code of the connection request.
Definition mqtt.h:328
uint8_t session_present_flag
Allows client and broker to check if they have a consistent view about whether there is already a sto...
Definition mqtt.h:321
The response to a ping request.
Definition mqtt.h:499
A publish acknowledgement for messages that were published with QoS level 1.
Definition mqtt.h:391
uint16_t packet_id
The published messages packet ID.
Definition mqtt.h:393
The response to a PUBREL packet.
Definition mqtt.h:433
uint16_t packet_id
he published messages packet ID.
Definition mqtt.h:435
A publish packet received from the broker.
Definition mqtt.h:343
size_t application_message_size
The size of the application message in bytes.
Definition mqtt.h:379
const void * topic_name
The topic name.
Definition mqtt.h:370
uint16_t packet_id
The publish message's packet ID.
Definition mqtt.h:373
const void * application_message
The publish message's application message.
Definition mqtt.h:376
uint8_t qos_level
The quality of service level.
Definition mqtt.h:357
uint16_t topic_name_size
Size of the topic name (number of characters).
Definition mqtt.h:363
uint8_t retain_flag
The retain flag of this publish message.
Definition mqtt.h:360
uint8_t dup_flag
The DUP flag. DUP flag is 0 if its the first attempt to send this publish packet. A DUP flag of 1 mea...
Definition mqtt.h:348
The response packet to a PUBLISH packet with QoS level 2.
Definition mqtt.h:405
uint16_t packet_id
The published messages packet ID.
Definition mqtt.h:407
The response to a PUBREC packet.
Definition mqtt.h:419
uint16_t packet_id
The published messages packet ID.
Definition mqtt.h:421
The response to a subscription request.
Definition mqtt.h:461
uint16_t packet_id
The published messages packet ID.
Definition mqtt.h:463
ssize_t mqtt_unpack_suback_response(struct mqtt_response *mqtt_response, const uint8_t *buf)
Deserialize a SUBACK packet from buf.
size_t num_return_codes
Definition mqtt.h:473
const uint8_t * return_codes
Definition mqtt.h:470
The brokers response to a UNSUBSCRIBE request.
Definition mqtt.h:484
uint16_t packet_id
The published messages packet ID.
Definition mqtt.h:486
ssize_t mqtt_unpack_unsuback_response(struct mqtt_response *mqtt_response, const uint8_t *buf)
Deserialize an UNSUBACK packet from buf.
A struct used to deserialize/interpret an incoming packet from the broker.
Definition mqtt.h:507
struct mqtt_fixed_header fixed_header
The mqtt_fixed_header of the deserialized packet.
Definition mqtt.h:509
union mqtt_response::@310276021004350124372131057163031221322016065344 decoded
A union of the possible responses from the broker.