124 lines
3.8 KiB
C
124 lines
3.8 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!\n");
|
|
} else {
|
|
cdc_printf("msg queue init failed!\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!\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\n");
|
|
return 1;
|
|
}
|
|
BaseType_t msg_queue_return;
|
|
imp_msg_item_t item = { 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);
|
|
if (msg->recv_id == handle->task_id) {
|
|
return 0;
|
|
}
|
|
return 3;
|
|
}
|
|
}
|
|
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\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\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
|
|
*/ |