由于现存版本的mqtt代码都是针对嵌入式linux系统的,直接copy过来是不能使用的,必须对其中的动态数组定义顺序等问题进行处理,本例程定义topic最大长度为64,message最大长度为1024。本例程包含3个文件分别是emqtt.h
#ifndef __LIBEMQTT_H__
#define __LIBEMQTT_H__
#include <stdint.h>
#include <string.h>
#ifndef MQTT_CONF_USERNAME_LENGTH
#define MQTT_CONF_USERNAME_LENGTH 13 // Recommended by MQTT Specification (12 + '\0')
#endif
#ifndef MQTT_CONF_PASSWORD_LENGTH
#define MQTT_CONF_PASSWORD_LENGTH 13 // Recommended by MQTT Specification (12 + '\0')
#endif
#define MQTT_MSG_CONNECT 1<<4
#define MQTT_MSG_CONNACK 2<<4
#define MQTT_MSG_PUBLISH 3<<4
#define MQTT_MSG_PUBACK 4<<4
#define MQTT_MSG_PUBREC 5<<4
#define MQTT_MSG_PUBREL 6<<4
#define MQTT_MSG_PUBCOMP 7<<4
#define MQTT_MSG_SUBSCRIBE 8<<4
#define MQTT_MSG_SUBACK 9<<4
#define MQTT_MSG_UNSUBSCRIBE 10<<4
#define MQTT_MSG_UNSUBACK 11<<4
#define MQTT_MSG_PINGREQ 12<<4
#define MQTT_MSG_PINGRESP 13<<4
#define MQTT_MSG_DISCONNECT 14<<4
#define MQTTParseMessageType(buffer) ( *buffer & 0xF0 )
#define MQTTParseMessageDuplicate(buffer) ( *buffer & 0x08 )
#define MQTTParseMessageQos(buffer) ( (*buffer & 0x06) >> 1 )
#define MQTTParseMessageRetain(buffer) ( *buffer & 0x01 )
typedef struct {
// Connection info
char clientid[50];
// Auth fields
char username[MQTT_CONF_USERNAME_LENGTH];
char password[MQTT_CONF_PASSWORD_LENGTH];
// Will topic
uint8_t will_retain;
uint8_t will_qos;
uint8_t clean_session;
// Management fields
uint16_t seq;
uint16_t alive;
} mqtt_broker_handle_t;
uint8_t mqtt_num_rem_len_bytes(const uint8_t* buf);
uint16_t mqtt_parse_rem_len(const uint8_t* buf);
uint16_t mqtt_parse_msg_id(const uint8_t* buf);
uint16_t mqtt_parse_pub_topic(const uint8_t* buf, uint8_t* topic);
uint16_t mqtt_parse_pub_topic_ptr(const uint8_t* buf, const uint8_t** topic_ptr);
uint16_t mqtt_parse_publish_msg(const uint8_t* buf, uint8_t* msg);
uint16_t mqtt_parse_pub_msg_ptr(const uint8_t* buf, const uint8_t** msg_ptr);
/** Initialize the information to connect to the broker.
* @param broker Data structure that contains the connection information with the broker.
* @param clientid A string that identifies the client id.
*
* @note Only has effect before to call mqtt_connect
*/
void mqtt_init(mqtt_broker_handle_t* broker, const char* clientid);
/** Enable the authentication to connect to the broker.
* @param broker Data structure that contains the connection information with the broker.
* @param username A string that contains the username.
* @param password A string that contains the password.
*
* @note Only has effect before to call mqtt_connect
*/
void mqtt_init_auth(mqtt_broker_handle_t* broker, const char* username, const char* password);
/** Set the keep alive timer.
* @param broker Data structure that contains the connection information with the broker.
* @param alive Keep aliver timer value (in seconds).
*
* @note Only has effect before to call mqtt_connect
*/
void mqtt_set_alive(mqtt_broker_handle_t* broker, uint16_t alive);
/** Connect to the broker.
* @param broker Data structure that contains the connection information with the broker.
*
* @retval 1 On success.
* @retval 0 On connection error.
* @retval -1 On IO error.
*/
int mqtt_connect(mqtt_broker_handle_t* broker);
/** Disconnect to the broker.
* @param broker Data structure that contains the connection information with the broker.
*
* @note The socket must also be closed.
*
* @retval 1 On success.
* @retval 0 On connection error.
* @retval -1 On IO error.
*/
int mqtt_disconnect(mqtt_broker_handle_t* broker);
/** Publish a message on a topic. This message will be published with 0 Qos level.
* @param broker Data structure that contains the connection information with the broker.
* @param topic The topic name.
* @param msg The message.
* @param retain Enable or disable the Retain flag (values: 0 or 1).
*
* @retval 1 On success.
* @retval 0 On connection error.
* @retval -1 On IO error.
*/
int mqtt_publish(mqtt_broker_handle_t* broker, const char* topic, const char* msg, uint8_t retain);
/** Publish a message on a topic.
* @param broker Data structure that contains the connection information with the broker.
* @param topic The topic name.
* @param msg The message.
* @param retain Enable or disable the Retain flag (values: 0 or 1).
* @param qos Quality of Service (values: 0, 1 or 2)
* @param message_id Variable that will store the Message ID, if the pointer is not NULL.
*
* @retval 1 On success.
* @retval 0 On connection error.
* @retval -1 On IO error.
*/
int mqtt_publish_with_qos(mqtt_broker_handle_t* broker, const char* topic, const char* msg, uint8_t retain, uint8_t qos, uint16_t* message_id);
/** Send a PUBREL message. It's used for PUBLISH message with 2 QoS level.
* @param broker Data structure that contains the connection information with the broker.
* @param message_id Message ID
*
* @retval 1 On success.
* @retval 0 On connection error.
* @retval -1 On IO error.
*/
int mqtt_pubrel(mqtt_broker_handle_t* broker, uint16_t message_id);
/** Subscribe to a topic.
* @param broker Data structure that contains the connection information with the broker.
* @param topic The topic name.
* @param message_id Variable that will store the Message ID, if the pointer is not NULL.
*
* @retval 1 On success.
* @retval 0 On connection error.
* @retval -1 On IO error.
*/
int mqtt_subscribe(mqtt_broker_handle_t* broker, const char* topic, uint16_t* message_id);
/** Unsubscribe from a topic.
* @param broker Data structure that contains the connection information with the broker.
* @param topic The topic name.
* @param message_id Variable that will store the Message ID, if t