2016-09-11 10:10:24 +07:00
|
|
|
#ifndef _MQTT_H_
|
|
|
|
|
#define _MQTT_H_
|
|
|
|
|
#include <stdint.h>
|
2016-09-11 21:42:34 +07:00
|
|
|
#include <string.h>
|
2016-09-11 10:10:24 +07:00
|
|
|
#include "mqtt_config.h"
|
|
|
|
|
#include "mqtt_msg.h"
|
2016-09-12 17:05:28 +07:00
|
|
|
#include "ringbuf.h"
|
2016-09-12 21:15:28 +07:00
|
|
|
|
2017-07-21 21:23:41 +02:00
|
|
|
#if defined(CONFIG_MQTT_SECURITY_ON)
|
|
|
|
|
#include "openssl/ssl.h"
|
|
|
|
|
#endif
|
2016-09-12 21:15:28 +07:00
|
|
|
|
2017-02-18 19:06:18 -05:00
|
|
|
typedef struct mqtt_client mqtt_client;
|
|
|
|
|
typedef struct mqtt_event_data_t mqtt_event_data_t;
|
|
|
|
|
|
2017-02-18 19:42:31 -05:00
|
|
|
/**
|
|
|
|
|
* \return True on connect success, false on error
|
|
|
|
|
*/
|
|
|
|
|
typedef bool (* mqtt_connect_callback)(mqtt_client *client);
|
|
|
|
|
/**
|
|
|
|
|
*/
|
|
|
|
|
typedef void (* mqtt_disconnect_callback)(mqtt_client *client);
|
|
|
|
|
/**
|
|
|
|
|
* \param[out] buffer Pointer to buffer to fill
|
|
|
|
|
* \param[in] len Number of bytes to read
|
|
|
|
|
* \param[in] timeout_ms Time to wait for completion, or 0 for no timeout
|
|
|
|
|
* \return Number of bytes read, less than 0 on error
|
|
|
|
|
*/
|
|
|
|
|
typedef int (* mqtt_read_callback)(mqtt_client *client, void *buffer, int len, int timeout_ms);
|
|
|
|
|
/**
|
|
|
|
|
* \param[in] buffer Pointer to buffer to write
|
|
|
|
|
* \param[in] len Number of bytes to write
|
|
|
|
|
* \param[in] timeout_ms Time to wait for completion, or 0 for no timeout
|
|
|
|
|
* \return Number of bytes written, less than 0 on error
|
|
|
|
|
*/
|
|
|
|
|
typedef int (* mqtt_write_callback)(mqtt_client *client, const void *buffer, int len, int timeout_ms);
|
2017-02-18 19:06:18 -05:00
|
|
|
typedef void (* mqtt_event_callback)(mqtt_client *client, mqtt_event_data_t *event_data);
|
2016-09-11 21:42:34 +07:00
|
|
|
|
2017-02-18 18:20:47 -05:00
|
|
|
typedef struct mqtt_settings {
|
2017-02-18 19:42:31 -05:00
|
|
|
mqtt_connect_callback connect_cb;
|
|
|
|
|
mqtt_disconnect_callback disconnect_cb;
|
|
|
|
|
|
|
|
|
|
mqtt_read_callback read_cb;
|
|
|
|
|
mqtt_write_callback write_cb;
|
|
|
|
|
|
2017-02-18 19:06:18 -05:00
|
|
|
mqtt_event_callback connected_cb;
|
|
|
|
|
mqtt_event_callback disconnected_cb; // unused
|
|
|
|
|
mqtt_event_callback reconnect_cb; // unused
|
2016-09-11 10:10:24 +07:00
|
|
|
|
2017-02-18 19:06:18 -05:00
|
|
|
mqtt_event_callback subscribe_cb;
|
|
|
|
|
mqtt_event_callback publish_cb;
|
|
|
|
|
mqtt_event_callback data_cb;
|
2016-09-11 10:10:24 +07:00
|
|
|
|
|
|
|
|
char host[CONFIG_MQTT_MAX_HOST_LEN];
|
|
|
|
|
uint32_t port;
|
|
|
|
|
char client_id[CONFIG_MQTT_MAX_CLIENT_LEN];
|
|
|
|
|
char username[CONFIG_MQTT_MAX_USERNAME_LEN];
|
|
|
|
|
char password[CONFIG_MQTT_MAX_PASSWORD_LEN];
|
|
|
|
|
char lwt_topic[CONFIG_MQTT_MAX_LWT_TOPIC];
|
|
|
|
|
char lwt_msg[CONFIG_MQTT_MAX_LWT_MSG];
|
2016-09-11 21:42:34 +07:00
|
|
|
uint32_t lwt_qos;
|
2016-09-11 10:10:24 +07:00
|
|
|
uint32_t lwt_retain;
|
|
|
|
|
uint32_t clean_session;
|
|
|
|
|
uint32_t keepalive;
|
|
|
|
|
} mqtt_settings;
|
|
|
|
|
|
2016-09-12 21:15:28 +07:00
|
|
|
typedef struct mqtt_event_data_t
|
|
|
|
|
{
|
|
|
|
|
uint8_t type;
|
|
|
|
|
const char* topic;
|
|
|
|
|
const char* data;
|
|
|
|
|
uint16_t topic_length;
|
|
|
|
|
uint16_t data_length;
|
|
|
|
|
uint16_t data_offset;
|
|
|
|
|
uint16_t data_total_length;
|
|
|
|
|
} mqtt_event_data_t;
|
|
|
|
|
|
2016-09-11 21:42:34 +07:00
|
|
|
typedef struct mqtt_state_t
|
|
|
|
|
{
|
|
|
|
|
uint16_t port;
|
|
|
|
|
int auto_reconnect;
|
|
|
|
|
mqtt_connect_info_t* connect_info;
|
|
|
|
|
uint8_t* in_buffer;
|
|
|
|
|
uint8_t* out_buffer;
|
|
|
|
|
int in_buffer_length;
|
|
|
|
|
int out_buffer_length;
|
|
|
|
|
uint16_t message_length;
|
|
|
|
|
uint16_t message_length_read;
|
|
|
|
|
mqtt_message_t* outbound_message;
|
|
|
|
|
mqtt_connection_t mqtt_connection;
|
|
|
|
|
uint16_t pending_msg_id;
|
|
|
|
|
int pending_msg_type;
|
|
|
|
|
int pending_publish_qos;
|
|
|
|
|
} mqtt_state_t;
|
|
|
|
|
|
2017-02-18 18:20:47 -05:00
|
|
|
typedef struct mqtt_client {
|
2016-09-11 10:10:24 +07:00
|
|
|
int socket;
|
2016-12-28 01:57:17 +01:00
|
|
|
|
|
|
|
|
#if defined(CONFIG_MQTT_SECURITY_ON) // ENABLE MQTT OVER SSL
|
|
|
|
|
SSL_CTX *ctx;
|
|
|
|
|
SSL *ssl;
|
|
|
|
|
#endif
|
|
|
|
|
|
2016-09-11 10:10:24 +07:00
|
|
|
mqtt_settings *settings;
|
|
|
|
|
mqtt_state_t mqtt_state;
|
|
|
|
|
mqtt_connect_info_t connect_info;
|
2016-09-12 17:05:28 +07:00
|
|
|
QueueHandle_t xSendingQueue;
|
|
|
|
|
RINGBUF send_rb;
|
|
|
|
|
uint32_t keepalive_tick;
|
2016-09-11 10:10:24 +07:00
|
|
|
} mqtt_client;
|
|
|
|
|
|
2016-09-12 21:15:28 +07:00
|
|
|
mqtt_client *mqtt_start(mqtt_settings *mqtt_info);
|
2016-12-28 01:57:17 +01:00
|
|
|
void mqtt_stop();
|
2016-09-11 21:42:34 +07:00
|
|
|
void mqtt_task(void *pvParameters);
|
2017-05-14 20:04:22 +02:00
|
|
|
void mqtt_subscribe(mqtt_client *client, const char *topic, uint8_t qos);
|
|
|
|
|
void mqtt_publish(mqtt_client* client, const char *topic, const char *data, int len, int qos, int retain);
|
2017-02-25 16:35:02 +07:00
|
|
|
void mqtt_destroy();
|
2016-09-11 10:10:24 +07:00
|
|
|
#endif
|