Passing struct to a FreeRTOS task
Posted: Tue Jan 25, 2022 6:59 am
Hello. I need to pass struct to FreeRTOS task and I have a question. My struct is declared:
In my BLE message parser, I parse a message and then depending on the message I start a one shot task. For example:
However, inside the task, the conn_id and gatt_if are different. Is this because I have created my long_packet_s message_struct on the ble parser and it is no longer available inside my task?
1. What is the correct way of accesing the structure from the task. I have seen 2 methods:
and
Could someone help me understand what is the difference of the above two and which one should I use?
2. If I cannot pass the structure with the method described above. What are the other ways to pass a structure? Should I declare it as global?
Code: Select all
struct __attribute__((__packed__))long_packet_s
{
uint8_t* payload;
uint8_t length;
uint16_t conn_id;
uint8_t op_code;
uint8_t message_id;
esp_gatt_if_t gatt_if;
interface_select_e interface;
bool heap_allocation;
};
Code: Select all
case(GET_EVENTS):
{
printf("get events \n");
if(payload_length != 0){
printf("payload length invalid \n");
break;
}
long_packet_s message_struct;
message_struct.conn_id = conn_id;
message_struct.gatt_if = gatts_if;
message_struct.message_id = message_id;
message_struct.op_code = op_code;
message_struct.heap_allocation = false;
message_struct.interface = BLE;
printf("message_struct.conn_id before task =%u \n",message_struct.conn_id);
printf("message_struct.gatt_if before task =%u \n",message_struct.gatt_if);
if(xSemaphoreTake(spi_mutex,100)==pdTRUE){
uint16_t alarms_written_since_last_read = EEPROM_get_alarms_written_since_last_read(external_eeprom);
printf("events written since last read = %u \n",alarms_written_since_last_read);
if(alarms_written_since_last_read>=1){
xTaskCreate(BLE_long_packet_task_alarms,"ALARM TASK",10000,&message_struct,1,&BLE_long_alarms_handle);
}
else{
printf("no alarms registered \n");
xSemaphoreGive(spi_mutex);
}
}
else{
printf("semaphore is busy alarms \n");
}
break;
}
1. What is the correct way of accesing the structure from the task. I have seen 2 methods:
Code: Select all
void BLE_long_packet_task_alarms(void* param){
long_packet_s* packet = (long_packet_s*)param;
..
..
..
Code: Select all
void BLE_long_packet_task_alarms(void* param){
long_packet_s packet = *(long_packet_s*)param;
..
..
..
2. If I cannot pass the structure with the method described above. What are the other ways to pass a structure? Should I declare it as global?