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)
80#define MQTT_PROTOCOL_LEVEL 0x04
83#define __ALL_MQTT_ERRORS(MQTT_ERROR) \
84 MQTT_ERROR(MQTT_ERROR_NULLPTR) \
85 MQTT_ERROR(MQTT_ERROR_CONTROL_FORBIDDEN_TYPE) \
86 MQTT_ERROR(MQTT_ERROR_CONTROL_INVALID_FLAGS) \
87 MQTT_ERROR(MQTT_ERROR_CONTROL_WRONG_TYPE) \
88 MQTT_ERROR(MQTT_ERROR_CONNECT_CLIENT_ID_REFUSED) \
89 MQTT_ERROR(MQTT_ERROR_CONNECT_NULL_WILL_MESSAGE) \
90 MQTT_ERROR(MQTT_ERROR_CONNECT_FORBIDDEN_WILL_QOS) \
91 MQTT_ERROR(MQTT_ERROR_CONNACK_FORBIDDEN_FLAGS) \
92 MQTT_ERROR(MQTT_ERROR_CONNACK_FORBIDDEN_CODE) \
93 MQTT_ERROR(MQTT_ERROR_PUBLISH_FORBIDDEN_QOS) \
94 MQTT_ERROR(MQTT_ERROR_SUBSCRIBE_TOO_MANY_TOPICS) \
95 MQTT_ERROR(MQTT_ERROR_MALFORMED_RESPONSE) \
96 MQTT_ERROR(MQTT_ERROR_UNSUBSCRIBE_TOO_MANY_TOPICS) \
97 MQTT_ERROR(MQTT_ERROR_RESPONSE_INVALID_CONTROL_TYPE) \
98 MQTT_ERROR(MQTT_ERROR_CONNECT_NOT_CALLED) \
99 MQTT_ERROR(MQTT_ERROR_SEND_BUFFER_IS_FULL) \
100 MQTT_ERROR(MQTT_ERROR_SOCKET_ERROR) \
101 MQTT_ERROR(MQTT_ERROR_MALFORMED_REQUEST) \
102 MQTT_ERROR(MQTT_ERROR_RECV_BUFFER_TOO_SMALL) \
103 MQTT_ERROR(MQTT_ERROR_ACK_OF_UNKNOWN) \
104 MQTT_ERROR(MQTT_ERROR_NOT_IMPLEMENTED) \
105 MQTT_ERROR(MQTT_ERROR_CONNECTION_REFUSED) \
106 MQTT_ERROR(MQTT_ERROR_SUBSCRIBE_FAILED) \
107 MQTT_ERROR(MQTT_ERROR_CONNECTION_CLOSED) \
108 MQTT_ERROR(MQTT_ERROR_INITIAL_RECONNECT) \
109 MQTT_ERROR(MQTT_ERROR_INVALID_REMAINING_LENGTH) \
110 MQTT_ERROR(MQTT_ERROR_CLEAN_SESSION_IS_REQUIRED) \
111 MQTT_ERROR(MQTT_ERROR_RECONNECTING)
116#define GENERATE_ENUM(ENUM) ENUM,
119#define GENERATE_STRING(STRING) #STRING,
142#define __mqtt_packed_cstrlen(x) (2 + (unsigned int)strlen(x))
304 const char* client_id,
305 const char* will_topic,
306 const void* will_message,
307 size_t will_message_size,
308 const char* user_name,
309 const char* password,
310 uint8_t connect_flags,
311 uint16_t keep_alive);
325 const char* topic_name,
327 const void* application_message,
328 size_t application_message_size,
329 uint8_t publish_flags);
337#define MQTT_SUBSCRIBE_REQUEST_MAX_NUM_TOPICS 8
341 unsigned int packet_id,
345#define MQTT_UNSUBSCRIBE_REQUEST_MAX_NUM_TOPICS 8
349 unsigned int packet_id,
419#define mqtt_mq_get(mq_ptr, index) (((struct mqtt_queued_message*) ((mq_ptr)->mem_end)) - 1 - index)
422#define mqtt_mq_length(mq_ptr) (((struct mqtt_queued_message*) ((mq_ptr)->mem_end)) - (mq_ptr)->queue_tail)
425#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
512 mqtt_pal_socket_handle sockfd,
513 uint8_t *sendbuf,
size_t sendbufsz,
514 uint8_t *recvbuf,
size_t recvbufsz,
519 void (*reconnect_callback)(
struct mqtt_client *client,
void** state),
520 void *reconnect_state,
525 mqtt_pal_socket_handle socketfd,
526 uint8_t *sendbuf,
size_t sendbufsz,
527 uint8_t *recvbuf,
size_t recvbufsz);
531 const char* client_id,
532 const char* will_topic,
533 const void* will_message,
534 size_t will_message_size,
535 const char* user_name,
536 const char* password,
537 uint8_t connect_flags,
538 uint16_t keep_alive);
546 const char* topic_name,
547 const void* application_message,
548 size_t application_message_size,
549 uint8_t publish_flags);
566 const char* topic_name,
571 const char* topic_name);
585#if defined(__cplusplus)
uint16_t __mqtt_unpack_uint16(const uint8_t *buf)
MQTTConnackReturnCode
RESPONSES.
@ MQTT_CONNACK_REFUSED_IDENTIFIER_REJECTED
@ MQTT_CONNACK_REFUSED_NOT_AUTHORIZED
@ MQTT_CONNACK_REFUSED_PROTOCOL_VERSION
@ MQTT_CONNACK_REFUSED_BAD_USER_NAME_OR_PASSWORD
@ MQTT_CONNACK_REFUSED_SERVER_UNAVAILABLE
enum MQTTErrors mqtt_unsubscribe(struct mqtt_client *client, const char *topic_name)
void mqtt_mq_init(struct mqtt_message_queue *mq, void *buf, size_t bufsz)
struct mqtt_queued_message * mqtt_mq_find(struct mqtt_message_queue *mq, enum MQTTControlPacketType control_type, uint16_t *packet_id)
ssize_t __mqtt_pack_str(uint8_t *buf, const char *str)
@ MQTT_QUEUED_AWAITING_ACK
@ MQTT_SUBACK_SUCCESS_MAX_QOS_0
@ MQTT_SUBACK_SUCCESS_MAX_QOS_2
@ MQTT_SUBACK_SUCCESS_MAX_QOS_1
enum MQTTErrors mqtt_reconnect(struct mqtt_client *client)
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)
ssize_t mqtt_pack_subscribe_request(uint8_t *buf, size_t bufsz, unsigned int packet_id,...)
ssize_t __mqtt_pack_uint16(uint8_t *buf, uint16_t integer)
ssize_t __mqtt_pubrel(struct mqtt_client *client, uint16_t packet_id)
enum MQTTErrors __mqtt_ping(struct mqtt_client *client)
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))
ssize_t __mqtt_recv(struct mqtt_client *client)
ssize_t mqtt_unpack_suback_response(struct mqtt_response *mqtt_response, const uint8_t *buf)
ssize_t mqtt_pack_fixed_header(uint8_t *buf, size_t bufsz, const struct mqtt_fixed_header *fixed_header)
REQUESTS.
ssize_t mqtt_pack_disconnect(uint8_t *buf, size_t bufsz)
uint16_t __mqtt_next_pid(struct mqtt_client *client)
#define __ALL_MQTT_ERRORS(MQTT_ERROR)
ssize_t __mqtt_pubcomp(struct mqtt_client *client, uint16_t packet_id)
ssize_t __mqtt_pubrec(struct mqtt_client *client, uint16_t packet_id)
ssize_t mqtt_unpack_pubxxx_response(struct mqtt_response *mqtt_response, const uint8_t *buf)
enum MQTTErrors mqtt_disconnect(struct mqtt_client *client)
ssize_t mqtt_pack_pubxxx_request(uint8_t *buf, size_t bufsz, enum MQTTControlPacketType control_type, uint16_t packet_id)
ssize_t mqtt_unpack_connack_response(struct mqtt_response *mqtt_response, const uint8_t *buf)
struct mqtt_queued_message * mqtt_mq_register(struct mqtt_message_queue *mq, size_t nbytes)
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))
ssize_t mqtt_pack_unsubscribe_request(uint8_t *buf, size_t bufsz, unsigned int packet_id,...)
ssize_t mqtt_unpack_response(struct mqtt_response *response, const uint8_t *buf, size_t bufsz)
ssize_t mqtt_pack_ping_request(uint8_t *buf, size_t bufsz)
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)
@ MQTT_CONTROL_DISCONNECT
@ MQTT_CONTROL_UNSUBSCRIBE
ssize_t __mqtt_send(struct mqtt_client *client)
const char * mqtt_error_str(enum MQTTErrors error)
ssize_t mqtt_unpack_fixed_header(struct mqtt_response *response, const uint8_t *buf, size_t bufsz)
@ MQTT_CONNECT_WILL_RETAIN
@ MQTT_CONNECT_WILL_QOS_0
@ MQTT_CONNECT_CLEAN_SESSION
@ MQTT_CONNECT_WILL_QOS_2
@ MQTT_CONNECT_WILL_QOS_1
void mqtt_mq_clean(struct mqtt_message_queue *mq)
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)
ssize_t __mqtt_puback(struct mqtt_client *client, uint16_t packet_id)
enum MQTTErrors mqtt_sync(struct mqtt_client *client)
enum MQTTErrors mqtt_ping(struct mqtt_client *client)
ssize_t mqtt_unpack_publish_response(struct mqtt_response *mqtt_response, const uint8_t *buf)
enum MQTTErrors mqtt_subscribe(struct mqtt_client *client, const char *topic_name, int max_qos_level)
#define GENERATE_ENUM(ENUM)
todo: add more connection refused errors
void mqtt_reinit(struct mqtt_client *client, mqtt_pal_socket_handle socketfd, uint8_t *sendbuf, size_t sendbufsz, uint8_t *recvbuf, size_t recvbufsz)
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)
todo: will_message should be a void*
void(* reconnect_callback)(struct mqtt_client *, void **)
enum MQTTErrors(* inspector_callback)(struct mqtt_client *)
void(* publish_response_callback)(void **state, struct mqtt_response_publish *publish)
mqtt_pal_socket_handle socketfd
double typical_response_time
struct mqtt_message_queue mq
void * publish_response_callback_state
mqtt_pal_time_t time_of_last_send
int number_of_keep_alives
struct mqtt_client::@9 recv_buffer
struct mqtt_queued_message * queue_tail
enum MQTTQueuedMessageState state
mqtt_pal_time_t time_sent
enum MQTTControlPacketType control_type
enum MQTTConnackReturnCode return_code
uint8_t session_present_flag
size_t application_message_size
const void * application_message
const uint8_t * return_codes
struct mqtt_fixed_header fixed_header
union mqtt_response::@8 decoded