luat_soc_sdk_2024 V1.0
luat_mqtt.h
浏览该文件的文档.
1#ifndef LUAT_MQTT_H
2#define LUAT_MQTT_H
7#define MQTT_MSG_RELEASE 0
8#define MQTT_MSG_TCP_TX_DONE 1
9#define MQTT_MSG_TIMER_PING 2
10#define MQTT_MSG_RECONNECT 3
11#define MQTT_MSG_CLOSE 4
13#define MQTT_ERROR_STATE_SOCKET -1
14#define MQTT_ERROR_STATE_DISCONNECT -2
15
16#ifndef MQTT_RECV_BUF_LEN_MAX
17#if defined(CHIP_EC618) || defined(CHIP_EC718)|| defined(CHIP_EC716)
18#define MQTT_RECV_BUF_LEN_MAX (80*1024)
19#else
20#define MQTT_RECV_BUF_LEN_MAX 4096
21#endif
22#endif
23
24
34
38typedef struct{
39 mqtt_broker_handle_t broker;
40 network_ctrl_t *netc;
41 luat_ip_addr_t ip_addr;
42 char host[192];
43 uint32_t buffer_offset;
44 uint32_t rxbuff_size;
46 void* mqtt_cb;
47 int8_t error_state;
48 uint16_t remote_port;
49 uint32_t keepalive;
50 uint8_t adapter_index;
52 uint8_t reconnect;
53 uint32_t reconnect_time;
55 void* ping_timer;
57 void* userdata;
59
60typedef struct{
61 uint16_t topic_len;
62 uint16_t payload_len;
63 uint8_t data[];
65
69typedef struct luat_mqtt_connopts
70{
71 const char* host;
72 uint16_t port;
73 uint8_t is_tls;
74 uint8_t is_ipv6;
75 uint8_t verify;
76 const char* server_cert;
78 const char* client_cert;
80 const char* client_key;
82 const char* client_password;
85
86typedef void (*luat_mqtt_cb_t)(luat_mqtt_ctrl_t *luat_mqtt_ctrl, uint16_t event);
87
94// static int luat_mqtt_msg_cb(luat_mqtt_ctrl_t *mqtt_ctrl);
95// int l_mqtt_callback(lua_State *L, void* ptr);
103int l_luat_mqtt_msg_cb(luat_mqtt_ctrl_t * ctrl, int arg1, int arg2);
110int32_t luat_mqtt_callback(void *data, void *param);
114LUAT_RT_RET_TYPE luat_mqtt_timer_callback(LUAT_RT_CB_PARAM);
115// int luat_mqtt_read_packet(luat_mqtt_ctrl_t *mqtt_ctrl);
123int luat_mqtt_send_packet(void* socket_info, const void* buf, unsigned int count);
124
131
138
145
152int luat_mqtt_init(luat_mqtt_ctrl_t *mqtt_ctrl, int adapter_index);
153
161
168int luat_mqtt_set_rxbuff_size(luat_mqtt_ctrl_t *mqtt_ctrl, uint32_t rxbuff_size);
169
183
190
201int luat_mqtt_set_will(luat_mqtt_ctrl_t *mqtt_ctrl, const char* topic, const char* payload, size_t payload_len, uint8_t qos, size_t retain);
210#endif
int luat_mqtt_init(luat_mqtt_ctrl_t *mqtt_ctrl, int adapter_index)
初始化luatos_mqtt(初始化MQTT)
LUAT_MQTT_STATE_E luat_mqtt_state_get(luat_mqtt_ctrl_t *mqtt_ctrl)
获取MQTT连接状态
struct luat_mqtt_connopts luat_mqtt_connopts_t
设置MQTT服务端服务器信息、加密信息
int luat_mqtt_set_connopts(luat_mqtt_ctrl_t *mqtt_ctrl, luat_mqtt_connopts_t *opts)
设置MQTT服务器信息、加密信息函数
int luat_mqtt_set_rxbuff_size(luat_mqtt_ctrl_t *mqtt_ctrl, uint32_t rxbuff_size)
设置MQTT服务器接收buff大小
int luat_mqtt_set_cb(luat_mqtt_ctrl_t *mqtt_ctrl, luat_mqtt_cb_t mqtt_cb)
设置MQTT事件回调函数
int luat_mqtt_reconnect(luat_mqtt_ctrl_t *mqtt_ctrl)
设置MQTT服务器信息、加密信息函数
int luat_mqtt_ping(luat_mqtt_ctrl_t *mqtt_ctrl)
发送ping包
LUAT_RT_RET_TYPE luat_mqtt_timer_callback(LUAT_RT_CB_PARAM)
MQTT定时器内部回调(用户无需关心)
int32_t luat_mqtt_callback(void *data, void *param)
MQTT报文解析内部回调(用户无需关心)
LUAT_MQTT_STATE_E
mqtt状态
int l_luat_mqtt_msg_cb(luat_mqtt_ctrl_t *ctrl, int arg1, int arg2)
MQTT内部回调(用户无需关心)
void(* luat_mqtt_cb_t)(luat_mqtt_ctrl_t *luat_mqtt_ctrl, uint16_t event)
int luat_mqtt_connect(luat_mqtt_ctrl_t *mqtt_ctrl)
发起MQTT连接
int luat_mqtt_set_will(luat_mqtt_ctrl_t *mqtt_ctrl, const char *topic, const char *payload, size_t payload_len, uint8_t qos, size_t retain)
设置遗嘱消息
int luat_mqtt_send_packet(void *socket_info, const void *buf, unsigned int count)
MQTT报文发送(用户无需关心)
void luat_mqtt_close_socket(luat_mqtt_ctrl_t *mqtt_ctrl)
关闭MQTT连接,如果设置了自动重连,回重新自动连接
void luat_mqtt_release_socket(luat_mqtt_ctrl_t *mqtt_ctrl)
释放MQTT资源,释放后luatos_mqtt对象不可用
@ MQTT_STATE_MQTT
@ MQTT_STATE_SCONNECT
@ MQTT_STATE_DISCONNECT
@ MQTT_STATE_READY
设置MQTT服务端服务器信息、加密信息
const char * client_key
const char * client_cert
const char * client_password
const char * server_cert
设置MQTT客户端的配置参数
LUAT_MQTT_STATE_E mqtt_state
luat_ip_addr_t ip_addr
mqtt_broker_handle_t broker
uint8_t * mqtt_packet_buffer
network_ctrl_t * netc