Sending struct information to the queue

zazas321
Posts: 231
Joined: Mon Feb 01, 2021 9:41 am

Sending struct information to the queue

Postby zazas321 » Thu Jan 13, 2022 10:20 am

Hello. In my program, I need to send some information via the queue. I have built a structure of the information that I want to pass to the queue and it looks as following;

Code: Select all

struct __attribute__((__packed__))long_packet_s
{
    esp_gatt_if_t gatt_if;
    uint8_t* payload;
    uint8_t length;
    uint16_t conn_id;
    uint8_t op_code;
    uint8_t message_id;
    interface_select_e interface;
};

Code: Select all

              long_packet_s message_struct;
                message_struct.gatt_if = gatts_if;
                message_struct.conn_id = conn_id;
                message_struct.message_id = message_id;
                message_struct.op_code = op_code;
                data_buf[0] = GET_FW_VERSION;
                data_buf[1] = 1;
                data_buf[2] = strlen(device_info.FW_version) >> 8;
                data_buf[3] = strlen(device_info.FW_version) & 0x00FF;
                printf("strlen(device_info.FW_version)=%u \n",strlen(device_info.FW_version));
                memcpy((data_buf+4),(uint8_t*)(device_info.FW_version),strlen(device_info.FW_version));
                message_struct.payload = data_buf;
                message_struct.length = strlen(device_info.FW_version)+4;
                for(int i = 0; i<= 4+strlen(device_info.FW_version);i++){
                    printf("global_message.payload[%i]=%u\n",i,message_struct.payload[i]);
                }
                xQueueSend(ble_parser_queue,(void*)&message_struct, 10);
              
I build a structure message_structwith all the relevant information that I want to pass to my queue.

And then in my queue task:

Code: Select all

static void BLE_queue_parser_task(void *argument)
{
    long_packet_s data_received;
    for(;;)
    {   
         
            if (xQueueReceive(ble_parser_queue, &data_received, 10) == pdTRUE) {

                printf("local message length = %u \n",data_received.length);
                for(int i = 0; i <10;i++){
                    printf("local payload[%i]=%u\n",i,data_received.payload[i]);
                }

        
            }
        
    }
}

When I try to print the values, they are different and I cannot understand why

Code: Select all

strlen(device_info.FW_version)=5
global_message.payload[0]=2
global_message.payload[1]=1
global_message.payload[2]=0
global_message.payload[3]=5
global_message.payload[4]=49
global_message.payload[5]=46
global_message.payload[6]=48
global_message.payload[7]=48
global_message.payload[8]=48
global_message.payload[9]=0
local message length = 9
local payload[0]=233
local payload[1]=124
local payload[2]=16
local payload[3]=128
local payload[4]=96
local payload[5]=194
local payload[6]=252
local payload[7]=63
local payload[8]=128
local payload[9]=219
I cannot wrap my head around what is it that I am doing wrong here. Why would I not be able to send the data over the queue properly

markkuk
Posts: 38
Joined: Wed Mar 27, 2019 11:50 am

Re: Sending struct information to the queue

Postby markkuk » Thu Jan 13, 2022 12:37 pm

Where is the array data_buf allocated? If it's a local variable to the sending function, it won't exist when the message is read and the space may have been overwritten by other data.

zazas321
Posts: 231
Joined: Mon Feb 01, 2021 9:41 am

Re: Sending struct information to the queue

Postby zazas321 » Fri Jan 14, 2022 6:50 am

Yes you are totally right. data_buf was allocated locally in the function. I allocated it using malloc and then clear it after the queue has sucesfully received it. This works fine. Thank you very much

Who is online

Users browsing this forum: No registered users and 163 guests