116 MQTT_CONTROL_CONNECT=1u,
117 MQTT_CONTROL_CONNACK=2u,
118 MQTT_CONTROL_PUBLISH=3u,
119 MQTT_CONTROL_PUBACK=4u,
120 MQTT_CONTROL_PUBREC=5u,
121 MQTT_CONTROL_PUBREL=6u,
122 MQTT_CONTROL_PUBCOMP=7u,
123 MQTT_CONTROL_SUBSCRIBE=8u,
124 MQTT_CONTROL_SUBACK=9u,
125 MQTT_CONTROL_UNSUBSCRIBE=10u,
126 MQTT_CONTROL_UNSUBACK=11u,
127 MQTT_CONTROL_PINGREQ=12u,
128 MQTT_CONTROL_PINGRESP=13u,
129 MQTT_CONTROL_DISCONNECT=14u
159 #define MQTT_PROTOCOL_LEVEL 0x04 165 #define __ALL_MQTT_ERRORS(MQTT_ERROR) \ 166 MQTT_ERROR(MQTT_ERROR_NULLPTR) \ 167 MQTT_ERROR(MQTT_ERROR_CONTROL_FORBIDDEN_TYPE) \ 168 MQTT_ERROR(MQTT_ERROR_CONTROL_INVALID_FLAGS) \ 169 MQTT_ERROR(MQTT_ERROR_CONTROL_WRONG_TYPE) \ 170 MQTT_ERROR(MQTT_ERROR_CONNECT_NULL_CLIENT_ID) \ 171 MQTT_ERROR(MQTT_ERROR_CONNECT_NULL_WILL_MESSAGE) \ 172 MQTT_ERROR(MQTT_ERROR_CONNECT_FORBIDDEN_WILL_QOS) \ 173 MQTT_ERROR(MQTT_ERROR_CONNACK_FORBIDDEN_FLAGS) \ 174 MQTT_ERROR(MQTT_ERROR_CONNACK_FORBIDDEN_CODE) \ 175 MQTT_ERROR(MQTT_ERROR_PUBLISH_FORBIDDEN_QOS) \ 176 MQTT_ERROR(MQTT_ERROR_SUBSCRIBE_TOO_MANY_TOPICS) \ 177 MQTT_ERROR(MQTT_ERROR_MALFORMED_RESPONSE) \ 178 MQTT_ERROR(MQTT_ERROR_UNSUBSCRIBE_TOO_MANY_TOPICS) \ 179 MQTT_ERROR(MQTT_ERROR_RESPONSE_INVALID_CONTROL_TYPE) \ 180 MQTT_ERROR(MQTT_ERROR_CONNECT_NOT_CALLED) \ 181 MQTT_ERROR(MQTT_ERROR_SEND_BUFFER_IS_FULL) \ 182 MQTT_ERROR(MQTT_ERROR_SOCKET_ERROR) \ 183 MQTT_ERROR(MQTT_ERROR_MALFORMED_REQUEST) \ 184 MQTT_ERROR(MQTT_ERROR_RECV_BUFFER_TOO_SMALL) \ 185 MQTT_ERROR(MQTT_ERROR_ACK_OF_UNKNOWN) \ 186 MQTT_ERROR(MQTT_ERROR_NOT_IMPLEMENTED) \ 187 MQTT_ERROR(MQTT_ERROR_CONNECTION_REFUSED) \ 188 MQTT_ERROR(MQTT_ERROR_SUBSCRIBE_FAILED) \ 189 MQTT_ERROR(MQTT_ERROR_CONNECTION_CLOSED) \ 190 MQTT_ERROR(MQTT_ERROR_INITIAL_RECONNECT) \ 191 MQTT_ERROR(MQTT_ERROR_INVALID_REMAINING_LENGTH) 200 #define GENERATE_ENUM(ENUM) ENUM, 207 #define GENERATE_STRING(STRING) #STRING, 217 MQTT_ERROR_UNKNOWN=INT_MIN,
268 #define __mqtt_packed_cstrlen(x) (2 + strlen(x)) 281 MQTT_CONNACK_ACCEPTED = 0u,
282 MQTT_CONNACK_REFUSED_PROTOCOL_VERSION = 1u,
283 MQTT_CONNACK_REFUSED_IDENTIFIER_REJECTED = 2u,
284 MQTT_CONNACK_REFUSED_SERVER_UNAVAILABLE = 3u,
285 MQTT_CONNACK_REFUSED_BAD_USER_NAME_OR_PASSWORD = 4u,
286 MQTT_CONNACK_REFUSED_NOT_AUTHORIZED = 5u
302 uint8_t session_present_flag;
343 uint16_t topic_name_size;
350 const void* topic_name;
356 const void* application_message;
359 size_t application_message_size;
427 MQTT_SUBACK_SUCCESS_MAX_QOS_0 = 0u,
428 MQTT_SUBACK_SUCCESS_MAX_QOS_1 = 1u,
429 MQTT_SUBACK_SUCCESS_MAX_QOS_2 = 2u,
430 MQTT_SUBACK_FAILURE = 128u
450 const uint8_t *return_codes;
453 size_t num_return_codes;
561 ssize_t mqtt_unpack_publish_response (
struct mqtt_response *mqtt_response,
const uint8_t *buf);
579 ssize_t mqtt_unpack_pubxxx_response(
struct mqtt_response *mqtt_response,
const uint8_t *buf);
596 ssize_t mqtt_unpack_suback_response(
struct mqtt_response *mqtt_response,
const uint8_t *buf);
613 ssize_t mqtt_unpack_unsuback_response(
struct mqtt_response *mqtt_response,
const uint8_t *buf);
628 ssize_t mqtt_unpack_response(
struct mqtt_response* response,
const uint8_t *buf,
size_t bufsz);
657 MQTT_CONNECT_RESERVED = 1u,
658 MQTT_CONNECT_CLEAN_SESSION = 2u,
659 MQTT_CONNECT_WILL_FLAG = 4u,
660 MQTT_CONNECT_WILL_QOS_0 = (0u & 0x03) << 3,
661 MQTT_CONNECT_WILL_QOS_1 = (1u & 0x03) << 3,
662 MQTT_CONNECT_WILL_QOS_2 = (2u & 0x03) << 3,
663 MQTT_CONNECT_WILL_RETAIN = 32u,
664 MQTT_CONNECT_PASSWORD = 64u,
665 MQTT_CONNECT_USER_NAME = 128u
707 const char* client_id,
708 const char* will_topic,
709 const void* will_message,
710 size_t will_message_size,
711 const char* user_name,
712 const char* password,
713 uint8_t connect_flags,
714 uint16_t keep_alive);
725 MQTT_PUBLISH_DUP = 8u,
726 MQTT_PUBLISH_QOS_0 = ((0u << 1) & 0x06),
727 MQTT_PUBLISH_QOS_1 = ((1u << 1) & 0x06),
728 MQTT_PUBLISH_QOS_2 = ((2u << 1) & 0x06),
729 MQTT_PUBLISH_QOS_MASK = ((3u << 1) & 0x06),
730 MQTT_PUBLISH_RETAIN = 0x01
757 const char* topic_name,
759 void* application_message,
760 size_t application_message_size,
761 uint8_t publish_flags);
802 #define MQTT_SUBSCRIBE_REQUEST_MAX_NUM_TOPICS 8 827 unsigned int packet_id,
837 #define MQTT_UNSUBSCRIBE_REQUEST_MAX_NUM_TOPICS 8 861 unsigned int packet_id,
903 MQTT_QUEUED_AWAITING_ACK,
927 mqtt_pal_time_t time_sent;
1052 #define mqtt_mq_get(mq_ptr, index) (((struct mqtt_queued_message*) ((mq_ptr)->mem_end)) - 1 - index) 1058 #define mqtt_mq_length(mq_ptr) (((struct mqtt_queued_message*) ((mq_ptr)->mem_end)) - (mq_ptr)->queue_tail) 1064 #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 1076 mqtt_pal_socket_handle socketfd;
1082 uint16_t keep_alive;
1088 int number_of_keep_alives;
1097 mqtt_pal_time_t time_of_last_send;
1117 int response_timeout;
1120 int number_of_timeouts;
1128 double typical_response_time;
1149 void* publish_response_callback_state;
1206 mqtt_pal_mutex_t mutex;
1306 mqtt_pal_socket_handle sockfd,
1307 uint8_t *sendbuf,
size_t sendbufsz,
1308 uint8_t *recvbuf,
size_t recvbufsz,
1356 void (*reconnect_callback)(
struct mqtt_client *client,
void** state),
1357 void *reconnect_state,
1383 mqtt_pal_socket_handle socketfd,
1384 uint8_t *sendbuf,
size_t sendbufsz,
1385 uint8_t *recvbuf,
size_t recvbufsz);
1414 const char* client_id,
1415 const char* will_topic,
1416 const void* will_message,
1417 size_t will_message_size,
1418 const char* user_name,
1419 const char* password,
1420 uint8_t connect_flags,
1421 uint16_t keep_alive);
1446 const char* topic_name,
1447 void* application_message,
1448 size_t application_message_size,
1449 uint8_t publish_flags);
1510 const char* topic_name,
1525 const char* topic_name);
MQTTConnackReturnCode
An enumeration of the return codes returned in a CONNACK packet.
Definition: mqtt.h:279
void * reconnect_state
A pointer to some state. A pointer to this member is passed to mqtt_client::reconnect_callback.
Definition: mqtt.h:1180
MQTTControlPacketType
An enumeration of the MQTT control packet types.
Definition: mqtt.h:115
MQTTQueuedMessageState
An enumeration of queued message states.
Definition: mqtt.h:900
void(* reconnect_callback)(struct mqtt_client *, void **)
A callback that is called whenever the client is in an error state.
Definition: mqtt.h:1174
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.This function also clears the c...
enum MQTTErrors __mqtt_ping(struct mqtt_client *client)
Ping the broker without locking/unlocking the mutex.
ssize_t __mqtt_recv(struct mqtt_client *client)
Handles ingress client traffic.
The brokers response to a UNSUBSCRIBE request.
Definition: mqtt.h:463
A connection response datastructure.
Definition: mqtt.h:296
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.This function must be called before any other API function calls...
uint16_t packet_id
The packet id of the message.
Definition: mqtt.h:939
ssize_t __mqtt_send(struct mqtt_client *client)
Handles egress client traffic.
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.
The response to a ping request.
Definition: mqtt.h:478
uint16_t __mqtt_next_pid(struct mqtt_client *client)
Generate a new next packet ID.Packet ID's are generated using a max-length LFSR.
The response packet to a PUBLISH packet with QoS level 2.
Definition: mqtt.h:384
uint16_t __mqtt_unpack_uint16(const uint8_t *buf)
Unpack a MQTT 16 bit integer to a native 16 bit integer.
The response to a PUBREC packet.
Definition: mqtt.h:398
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.An alternative to mqtt_init that allow...
#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:165
ssize_t __mqtt_pack_str(uint8_t *buf, const char *str)
Pack a MQTT string, given a c-string str.
enum MQTTErrors mqtt_ping(struct mqtt_client *client)
Ping the broker.
enum MQTTErrors mqtt_unsubscribe(struct mqtt_client *client, const char *topic_name)
Unsubscribe from a topic.
ssize_t mqtt_pack_disconnect(uint8_t *buf, size_t bufsz)
Serialize a DISCONNECT and put it into buf.
A publish packet received from the broker.A publish packet is received from the broker when a client ...
Definition: mqtt.h:322
ssize_t __mqtt_pack_uint16(uint8_t *buf, uint16_t integer)
Pack a MQTT 16 bit integer, given a native 16 bit integer .
MQTTErrors
An enumeration of error codes. Error messages can be retrieved by calling mqtt_error_str.
Definition: mqtt.h:216
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.
A publish acknowledgement for messages that were published with QoS level 1.
Definition: mqtt.h:370
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_ping_request(uint8_t *buf, size_t bufsz)
Serialize a PINGREQ and put it into buf.
ssize_t __mqtt_pubrec(struct mqtt_client *client, uint16_t packet_id)
Acknowledge an ingree publish with QOS==2.
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.
enum MQTTErrors mqtt_subscribe(struct mqtt_client *client, const char *topic_name, int max_qos_level)
Subscribe to a topic.
ssize_t __mqtt_pubcomp(struct mqtt_client *client, uint16_t packet_id)
Acknowledge an ingree PUBREL packet.
const char * mqtt_error_str(enum MQTTErrors error)
Returns an error message for error code, error.
#define GENERATE_ENUM(ENUM)
A macro used to generate the enum MQTTErrors from __ALL_MQTT_ERRORS.
Definition: mqtt.h:200
enum MQTTErrors mqtt_publish(struct mqtt_client *client, const char *topic_name, void *application_message, size_t application_message_size, uint8_t publish_flags)
Publish an application message.Publishes an application message to the MQTT broker.
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_puback(struct mqtt_client *client, uint16_t packet_id)
Acknowledge an ingree publish with QOS==1.
Includes/supports the types/calls required by the MQTT-C client.
MQTTPublishFlags
An enumeration of the PUBLISH flags.
Definition: mqtt.h:723
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.
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.
enum MQTTErrors mqtt_sync(struct mqtt_client *client)
Function that does the actual sending and receiving of traffic from the network.All the other functio...
The response to a subscription request.
Definition: mqtt.h:440
enum MQTTErrors mqtt_disconnect(struct mqtt_client *client)
Terminate the session with the MQTT broker.
ssize_t __mqtt_pubrel(struct mqtt_client *client, uint16_t packet_id)
Acknowledge an ingree PUBREC packet.
A message queue.
Definition: mqtt.h:949
MQTTSubackReturnCodes
An enumeration of subscription acknowledgement return codes.
Definition: mqtt.h:425
MQTTConnectFlags
An enumeration of CONNECT packet flags.
Definition: mqtt.h:655
A struct used to deserialize/interpret an incoming packet from the broker.
Definition: mqtt.h:486
ssize_t mqtt_pack_publish_request(uint8_t *buf, size_t bufsz, const char *topic_name, uint16_t packet_id, void *application_message, size_t application_message_size, uint8_t publish_flags)
Serialize a PUBLISH request and put it in buf.
The response to a PUBREL packet.
Definition: mqtt.h:412
An MQTT client.
Definition: mqtt.h:1073
A message in a mqtt_message_queue.
Definition: mqtt.h:910