MQTT Client源码分析

MQTT Client源码分析

之前基于杰杰的mqttclient代码韦东山老师的教程把MQTTClient程序移植到STM32F103开发板,F103的开发板串口连接ESP8266模组实现终端连接到MQTT服务器的功能,仅仅是对着韦老师的教程移植和使用杰杰的mqttclient代码,简单的将mqttclient\platform\FreeRTOS\platform_net_socket.c文件中的接口绑定到ESP8266的TCP AT命令,使用ESP8266的Socket,对于杰杰的mqttclient代码并没有深入分析和理解。

1. mqttclient架构

如下图:

在这里插入图片描述

1.1 API

mqttclient的API接口:

int mqtt_init(mqtt_client_t* c, client_init_params_t* init);
int mqtt_release(mqtt_client_t* c);
int mqtt_connect(mqtt_client_t* c);
int mqtt_disconnect(mqtt_client_t* c);
int mqtt_subscribe(mqtt_client_t* c, const char* topic_filter, mqtt_qos_t qos, message_handler_t msg_handler);
int mqtt_unsubscribe(mqtt_client_t* c, const char* topic_filter);
int mqtt_publish(mqtt_client_t* c, const char* topic_filter, mqtt_message_t* msg);

int mqtt_keep_alive(mqtt_client_t* c);
int mqtt_yield(mqtt_client_t* c, int timeout_ms);

1.2 mqtt_client_t结构体

typedef struct mqtt_client {
    char                        *mqtt_client_id; //由用户传入,在连接MQTT时(mqtt_connect_with_results)序列化连接报文
    char                        *mqtt_user_name; //由用户传入,在连接MQTT时(mqtt_connect_with_results)序列化连接报文
    char                        *mqtt_password; //由用户传入,在连接MQTT时(mqtt_connect_with_results)序列化连接报文
    char                        *mqtt_host; //由用户传入,在连接MQTT时(mqtt_connect_with_results)序列化连接报文
    char                        *mqtt_port; //由用户传入,在连接MQTT时(mqtt_connect_with_results)序列化连接报文
    char                        *mqtt_ca; //TLS才会用到,暂时不分析
    void                        *mqtt_reconnect_data; //MQTT需要重连服务器时用到
    uint8_t                     *mqtt_read_buf; //读数据缓冲区
    uint8_t                     *mqtt_write_buf; //写数据缓冲区
    uint16_t                    mqtt_keep_alive_interval; //MQTT保活超时时间
    uint16_t                    mqtt_packet_id; //报文标识符
    uint32_t                    mqtt_will_flag          : 1; //遗嘱标志
    uint32_t                    mqtt_clean_session      : 1; //清理会话标志
    uint32_t                    mqtt_ping_outstanding   : 2; //PINGREQ后是否正在等待PINGRESP标志
    uint32_t                    mqtt_version            : 4; //MQTT协议版本
    uint32_t                    mqtt_ack_handler_number : 24; //用于QOS1和QOS2中ACK记录
    uint32_t                    mqtt_cmd_timeout; //命令超时时间(主要是读写阻塞时间、等待响
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

studyingdda

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值