哎禹供 发表于 7 小时前

MQTT协议

//请求连接
void mqtt_connect(char *clientid,char *username,char *passwd)
{
        uint32_t clientid_len = strlen(clientid);
        uint32_t username_len = strlen(username);
        uint32_t passwd_len = strlen(passwd);
        //用于存储剩余长度 = 可变包头+有效载荷(客户端id+2+用户名+2+密码+2);
        uint32_t remain_len = 10 + (clientid_len+2)+ (username_len+2)+ (passwd_len+2);
        //1 对发送缓冲区进行初始化
        memset((char *)sendbuf,0,512);
        sendcnt = 0;
        //2 固定包头
        sendbuf = 0x10;//connect
        //3 对剩余长度进行编码
        do
        {
                uint8_t encodeByte = remain_len %128;
                remain_len = remain_len /128;
                if(remain_len >0)
                {
                        encodeByte = encodeByte |128;
                }
                sendbuf = encodeByte;
        }
        while(remain_len >0);
        //4 可变包头
        sendbuf = 0;
        sendbuf = 4;
        sendbuf = 'M';//协议名称
        sendbuf = 'Q';
        sendbuf = 'T';
        sendbuf = 'T';
        sendbuf = 4;//协议版本
        sendbuf = 0xc2 ;//连接标志
        sendbuf = 0 ;//保持连接
        sendbuf = 120 ;//2分钟
        //5 有效载荷 客户端id 用户名 密码
        sendbuf = BYTE1(clientid_len); //msb
        sendbuf = BYTE0(clientid_len);        //lsb
       
        memcpy(&sendbuf ,clientid,clientid_len);
        sendcnt +=clientid_len;
       
        sendbuf = BYTE1(username_len); //msb
        sendbuf = BYTE0(username_len);        //lsb
       
        memcpy(&sendbuf ,username,username_len);
        sendcnt +=username_len;
       
        sendbuf = BYTE1(passwd_len); //msb
        sendbuf = BYTE0(passwd_len);        //lsb
       
        memcpy(&sendbuf ,passwd,passwd_len);
        sendcnt +=passwd_len;
        //6 把connect报文发送出去
        Serial_Sendstring(sendbuf,sendcnt);
}//发布消息
void mqtt_publish(char *topic,uint8_t Qos,char *databuf)
{
        uint32_t remain_len;
        uint32_t topic_len = strlen(topic);
        uint32_t databuf_len = strlen(databuf);//有效载荷
        //用于存储剩余长度 = 可变包头+有效载荷(客户端id+2+用户名+2+密码+2);
        if(Qos == 0)
        {
                remain_len = (topic_len+2)+(databuf_len);
        }
        else
        {
                remain_len = (topic_len+2)+(databuf_len) + 2;
        }
       
        //1 对发送缓冲区进行初始化
        memset((char *)sendbuf,0,512);
        sendcnt = 0;
        //2 固定包头
        sendbuf = 0x30;//connect
        //3 对剩余长度进行编码
        do
        {
                uint8_t encodeByte = remain_len %128;
                remain_len = remain_len /128;
                if(remain_len >0)
                {
                        encodeByte = encodeByte |128;
                }
                sendbuf = encodeByte;
        }
        while(remain_len >0);
        //可变包头
        sendbuf = BYTE1(topic_len); //msb
        sendbuf = BYTE0(topic_len);        //lsb
       
        memcpy(&sendbuf ,topic,topic_len);
        sendcnt +=topic_len;
        //有效载荷
//        sendbuf = BYTE1(databuf_len); //msb
//        sendbuf = BYTE0(databuf_len);        //lsb
       
        memcpy(&sendbuf ,databuf,databuf_len);
        sendcnt +=databuf_len;
       
        //6 把connect报文发送出去
        Serial_Sendstring(sendbuf,sendcnt);
}//订阅消息
void mqtt_subscribe(const char *Filter, uint16_t msg_id, uint8_t qos)
{
char data = {0};
    int cnt = 0;
    uint32_t remain;

    // 固定包头
    data = 0x82; // 订阅消息类型

    // 计算剩余字节长度
    uint32_t Filter_len = strlen(Filter);
    remain = 2 + Filter_len + 2 + 1; // 消息 ID (2 字节) + 主题长度 (2 字节) + 主题 (Filter_len 字节) + QoS (1 字节)

    // 编码剩余字节
    do
    {
      uint8_t encodeByte = remain % 128;
      remain = remain / 128;
      if (remain > 0)
      {
            encodeByte |= 128;
      }
      data = encodeByte;
    }
    while (remain > 0);

    // 可变包头
    data = BYTE1(msg_id); // 消息 ID 高字节
    data = BYTE0(msg_id); // 消息 ID 低字节

    // 有效载荷
    data = BYTE1(Filter_len); // 主题长度高字节
    data = BYTE0(Filter_len); // 主题长度低字节
    memcpy(&data, Filter, Filter_len); // 主题
    cnt += Filter_len;
    data = qos; // QoS 级别

    // 发送构建的 MQTT 消息
    Serial_Sendstring(data, cnt);
}//保持心跳请求
void mqtt_keepalive()
{
        uint8_t buf = {0xc0,0x00};
       
}
来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
页: [1]
查看完整版本: MQTT协议