Giter Club home page Giter Club logo

Comments (11)

daishengdong avatar daishengdong commented on July 17, 2024

int tos_mqtt_receive(char *topic, int topic_len, unsigned char *payload, int payload_len)
{

unsigned char buffer[BUFFER_LEN];

这里在函数栈上定义了一个256个字节的buffer,可能会导致栈溢出,建议在此buffer前加上static修饰一下,然后观察现象是否依旧存在,有问题继续交流。

from tobudos.

yintianlan avatar yintianlan commented on July 17, 2024

将buffer前加上static之后,还是会硬复位,只是长度变成了256数组被溢出,BUFFER_LEN的长度就是被定为了256,我的payload_len也是256(这个大小应该足够了)

#define BUFFER_LEN 256
`int tos_mqtt_receive(char *topic, int topic_len, unsigned char *payload, int payload_len)
{
int topic_copy_len, qos;
unsigned char dup, retained;
unsigned short packet_id;
unsigned char *incoming_data;
int incoming_data_len, payload_copy_len;
MQTTString incoming_topic;
static unsigned char buffer[BUFFER_LEN];

if (MQTTPacket_read(buffer, sizeof(buffer), transport_getdata) != PUBLISH)	 {
    return -1;
}

if (MQTTDeserialize_publish(&dup, &qos, &retained, &packet_id,
                                &incoming_topic, &incoming_data, &incoming_data_len,
                                buffer, sizeof(buffer)) != 1) {
    return -1;
}

if (payload) {
    payload_copy_len = incoming_data_len < payload_len ? incoming_data_len : payload_len;
    printf("copy_len:%d\n",payload_copy_len);
    memcpy(payload, incoming_data, payload_copy_len);
}

if (topic) {
    topic_copy_len = incoming_topic.lenstring.len < topic_len ? incoming_topic.lenstring.len : topic_len;
    strncpy(topic, incoming_topic.lenstring.data, topic_copy_len);
    if (topic_copy_len <= topic_len - 1) {
        topic[topic_copy_len] = '\0';
    }   
}

return incoming_data_len;

}
`
出错LOG:
[10:52:43.387]收←◆V:len_1:27

[10:52:43.430]收←◆V:len_1:11
V:copy_len:256
E:System HardFault!
I:Reset!
I:==============================
I: HELLO WORLD
I:==============================
I:--------SYSTEM RESET STATE CHECK--------
E:System Software reset

from tobudos.

daishengdong avatar daishengdong commented on July 17, 2024

你log里len_1是什么含义

from tobudos.

daishengdong avatar daishengdong commented on July 17, 2024

顺便在copy_len那里把incoming_data_len也打印出来

from tobudos.

yintianlan avatar yintianlan commented on July 17, 2024

len_1是消息的模块接收从服务器过来的数据长度,从LOG看数据已经接收到,总共是38个字节,我copy_len打印的就是incoming_data_len,payload_len是传入的固定参数256,incoming_data_len正确应该是3,但解析出来的长度对不上

from tobudos.

daishengdong avatar daishengdong commented on July 17, 2024

你copy_len打印的是运算后的长度,我是想让你把没运算前mqtt的协议栈deserialize出来的长度打印出来。

from tobudos.

yintianlan avatar yintianlan commented on July 17, 2024

修改代码:
`int tos_mqtt_receive(char *topic, int topic_len, unsigned char *payload, int payload_len)
{
int topic_copy_len, qos;
unsigned char dup, retained;
unsigned short packet_id;
unsigned char *incoming_data;
int incoming_data_len, payload_copy_len;
MQTTString incoming_topic;
static unsigned char buffer[BUFFER_LEN];

if (MQTTPacket_read(buffer, sizeof(buffer), transport_getdata) != PUBLISH)	 {
    return -1;
}


printf("in_len_1:%d\n",incoming_data_len);//运算前
if (MQTTDeserialize_publish(&dup, &qos, &retained, &packet_id,
                                &incoming_topic, &incoming_data, &incoming_data_len,
                                buffer, sizeof(buffer)) != 1) {
    return -1;
}

if (payload) {
    payload_copy_len = incoming_data_len < payload_len ? incoming_data_len : payload_len;

    printf("in_len_2:%d\n",incoming_data_len);//运算后
    memcpy(payload, incoming_data, payload_copy_len);
}

if (topic) {
    topic_copy_len = incoming_topic.lenstring.len < topic_len ? incoming_topic.lenstring.len : topic_len;
    strncpy(topic, incoming_topic.lenstring.data, topic_copy_len);
    if (topic_copy_len <= topic_len - 1) {
        topic[topic_copy_len] = '\0';
    }   
}

return incoming_data_len;

}`

正常LOG:
V:data_len:38
V:in_len_1:-1410436952
V:mqtt publish ok:len 3
V:AppLin: Clear...
V:in_len_2:3
I:mqtt publish:ext
V:---------->topic: vehicle/541235795641023/control, payload: 020301, payload_len: 3

复位前LOG:

V:data_len:38

[14:53:44.737]收←◆V:data_len:38

[14:53:44.780]收←◆V:mqtt publish ok:len 3
I:mqtt publish:ext
V:AT CMD:
AT+CIPSEND=0,40

V:Got host: 0x90
V:AppLin: Clear...
V:in_len_1:-1410436952
V:AppLin: Clear...
V:in_len_2:3
V:---------->topic: vehicle/541235795641023/control, payload: 020302, payload_len: 3
V:in_len_1:-1410436952
V:in_len_2:-1410436952
I:==============================
I: HELLO WORLD
I:==============================
I:--------SYSTEM RESET STATE CHECK--------
E:System Software reset

from tobudos.

daishengdong avatar daishengdong commented on July 17, 2024

你在这个地方打印in_len_1只可能是随机值吧,你应该是MQTTDeserialize_publish之后打印

from tobudos.

yintianlan avatar yintianlan commented on July 17, 2024

V:in_len_1:-1410436952//运算前
V:in_len_2:-1410436952//运算后
看代码,两个都已经打印出来了。

1.现在测试只是把服务器的发送速率加快,而且报错的时候,payload_copy_len的值为负数或者256,出现的时候消息肯定是无法成功解析的;

2.从模块的发送串口数据的时间间隔来看,出现这种问题的时候大多是模块的串口数据无间隔的发送过来(内存是足够的,数据格式是正确的);

3.复现的方法就是加快服务器数据的发送(时间间隔100-200ms),本地接收到一帧后响应发送一帧消息,执行一段时间很容易复现。

4.现在我在长度解析不正确的时候返回,可以规避硬复位,但是我想知道长度解析失败的原因。

from tobudos.

daishengdong avatar daishengdong commented on July 17, 2024

我是说,你打印的“运算前 in_len_1”的解析长度根本没有意义,是个随机值。in_len_2是经过运算后的,我想知道的是MQTTDeserialize_publish这个接口调用后,incoming_data_len的值是多少。

from tobudos.

daishengdong avatar daishengdong commented on July 17, 2024

你这里应该是用的at框架操作的4G模组?如果是AT框架,得确定一下AT框架中的buffer缓冲是否太短,如果服务器很高频率的发送数据,或者数据一次回的比较大,可能会导致AT框架里的buffer不够放服务器回的数据。

from tobudos.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.