esp32s2_bare_board/main/components/msq_queue/imp_msg_queue.c

139 lines
4.4 KiB
C

/**
* @file imp_msg_queue.c
* @author Alvin Young (impressionyang@outlook.com)
* @brief
* @version 0.1
* @date 2024-12-04
*
* _ _
* (_)_ _ ___ _______ ___ ___ (_)__ ___ __ _____ ____ ___ _
* / / ' \/ _ \/ __/ -_|_-<(_-</ / _ \/ _ \/ // / _ `/ _ \/ _ `/
* /_/_/_/_/ .__/_/ \__/___/___/_/\___/_//_/\_, /\_,_/_//_/\_, /
* /_/ /___/ /___/
* @copyright Copyright (c) 2024 impressionyang
*
* @par 修改日志:
* <table>
* <tr><th>Date <th>Version <th>Author <th>Description
* <tr><td>2024-12-04 <td>v1.0 <td>Alvin Young <td>首次创建
* </table>
*
*/
/* Define to prevent recursive inclusion -------------------------------------*/
/* Includes ------------------------------------------------------------------*/
#include "imp_msg_queue.h"
#include "main_common.h"
#include <stdlib.h>
#include <string.h>
/* define --------------------------------------------------------------------*/
/* typedef -------------------------------------------------------------------*/
/* variables -----------------------------------------------------------------*/
/// @brief 消息队列的freeRTOS句柄
static QueueHandle_t sg_imp_msg_queue = NULL;
/* Private function(only *.c) -----------------------------------------------*/
/* Exported functions --------------------------------------------------------*/
uint8_t imp_msg_queue_init()
{
sg_imp_msg_queue =
xQueueCreate(IMP_MSG_QUEUE_MAX_ITEM_COUNT, sizeof(imp_msg_item_t));
if (sg_imp_msg_queue != NULL) {
cdc_printf("msg queue init ok!\r\n");
} else {
cdc_printf("msg queue init failed!\r\n");
return 1;
}
return 0;
}
imp_msg_queue_t* imp_msg_queue_create_handle(uint16_t task_id)
{
if (NULL == sg_imp_msg_queue) {
cdc_printf("msg queue create failed!\r\n");
return NULL;
}
imp_msg_queue_t* ptr = (imp_msg_queue_t*)malloc(sizeof(imp_msg_queue_t*));
ptr->is_init = 1;
ptr->msg_queue_handle = sg_imp_msg_queue;
ptr->task_id = task_id;
ptr->task_name = imp_main_task_table[task_id];
return ptr;
}
uint8_t imp_msg_queue_recv_msg(imp_msg_queue_t* handle, imp_msg_item_t* msg,
uint16_t timeout)
{
if (NULL == sg_imp_msg_queue || NULL == handle) {
cdc_printf("handle or msg queue is NULL\r\n");
return 1;
}
BaseType_t msg_queue_return;
imp_msg_item_t item = { 0 };
static uint16_t msg_peek_cnt = 0;
static uint16_t last_task_id = 0;
msg_queue_return = xQueuePeek(handle->msg_queue_handle, &item, timeout);
if (msg_queue_return != pdTRUE) {
return 2;
} else {
// read succeed
if (item.recv_id == handle->task_id) {
xQueueReceive(handle->msg_queue_handle, msg, 0);
msg_peek_cnt = 0;
if (msg->recv_id == handle->task_id) {
return 0;
}
return 3;
} else {
if (last_task_id != handle->task_id) {
msg_peek_cnt++;
}
last_task_id = handle->task_id;
if (msg_peek_cnt >= IMP_MSG_QUEUE_MAX_PEEK_CNT) {
// drop a msg
xQueueReceive(handle->msg_queue_handle, &item, 0);
msg_peek_cnt = 0;
cdc_printf("[warning] msg queue drop msg from %d to %d\r\n",
item.send_id, item.recv_id);
}
}
}
return 4;
}
uint8_t imp_msg_queue_send_msg(imp_msg_queue_t* handle, uint16_t recv_id,
imp_msg_item_t* msg)
{
if (NULL == sg_imp_msg_queue || NULL == handle) {
cdc_printf("handle or msg queue is NULL\r\n");
return 1;
}
BaseType_t msg_queue_return;
msg->send_id = handle->task_id;
msg->recv_id = recv_id;
msg_queue_return = xQueueSend(handle->msg_queue_handle, msg, portMAX_DELAY);
return 0;
}
uint8_t imp_msg_queue_destroy_handle(imp_msg_queue_t* handle)
{
if (NULL == sg_imp_msg_queue || NULL == handle) {
cdc_printf("handle or msg queue is NULL\r\n");
return 1;
}
handle->is_init = 0;
handle->msg_queue_handle = NULL;
handle->task_name = NULL;
handle->task_id = 0;
free(handle);
return 0;
}
/*
* EOF
*/