mcu_hi3321_watch/tjd/ate/ate_msg_manager.c
2025-05-26 20:15:20 +08:00

111 lines
3.3 KiB
C

#include "ate_msg_manager.h"
#include "cmsis_os2.h"
#include "common_def.h"
#include "debug_print.h"
#include "osal_task.h"
#include "soc_errno.h"
#include "soc_osal.h"
static osTimerId_t g_timer_handle;
static osThreadId_t g_task_handle = NULL;
static unsigned long g_ate_msg_queue;
static void process_success_ret_msg(result_t result)
{
wstp_print("\r\nOK");
if (result.len != 0) {
wstp_print(", %s\r\n", result.msg);
}
wstp_print("\r\n");
}
static void out_time_break(void *param) { osal_msg_queue_write_copy(g_ate_msg_queue, param, sizeof(osThreadId_t), 0); }
static void ate_callback_task(void *param)
{
((ate_callback *)param)(NULL);
/* 任务执行完成 */
osTimerStop(g_timer_handle);
osTimerDelete(g_timer_handle);
osThreadDetach(osThreadGetId());
}
static void process_ack_ret_msg(result_t result)
{
do {
if (result.out_time == 0 || result.callback == NULL) {
result.code = EXT_ERR_ISR_INVALID_PARAM;
break;
}
wstp_print("\r\nACK, %d\r\n", result.out_time);
/* 开始任务和定时器 */
g_task_handle = osThreadNew(ate_callback_task, (ate_callback *)result.callback, NULL);
if (g_task_handle == NULL) {
break;
}
g_timer_handle = osTimerNew(out_time_break, osTimerOnce, (void *)&g_task_handle, NULL);
if (g_timer_handle == NULL) {
osThreadTerminate(g_task_handle);
break;
}
result.code = osTimerStart(g_timer_handle, result.out_time);
if (result.code != 0) {
break;
}
return;
} while (0);
wstp_print("\r\nFAIL, 0x%x\r\n", result.code);
}
static void ate_msg_task_process_handler(void *param)
{
unused(param);
int32_t ret = OSAL_FAILURE;
osThreadId_t msg_data;
osStatus_t status = osOK;
uint32_t msg_size = sizeof(osThreadId_t);
wstp_print("ate_msg_task_process_handler start\n");
while (1) {
(void)memset_s(&msg_data, sizeof(msg_data), 0, sizeof(msg_data));
ret = osal_msg_queue_read_copy(g_ate_msg_queue, &msg_data, &msg_size, OSAL_MSGQ_WAIT_FOREVER);
if (ret != OSAL_SUCCESS) {
continue;
}
wstp_print("\r\nFAIL, TIMEOUT\r\n");
status = osThreadTerminate(msg_data);
if (status != osOK) {
wstp_print("Terminate thread failed, status = %d\r\n", status);
}
}
}
void tjd_ate_msg_task_init(void)
{
osThreadAttr_t task_attr = {"tjd_ate_msg_task", 0, NULL, 0, NULL, 0x700, 17, 0, 0};
task_attr.stack_mem = memalign(16, task_attr.stack_size); // add
osal_msg_queue_create("ate_at_msg_service_queue", 16, &g_ate_msg_queue, 0, sizeof(osThreadId_t));
osThreadNew(ate_msg_task_process_handler, NULL, &task_attr);
}
void process_at_cmd_ret(result_t result)
{
uint32_t irq_sts = osal_irq_lock();
/* 具有超时时间的为延时任务 */
if (result.out_time != 0) {
process_ack_ret_msg(result);
} else if (result.code == 0) {
/* 成功回复 */
process_success_ret_msg(result);
} else {
/* 失败回复 */
wstp_print("\r\nFAIL, 0x%x\r\n", result.code);
}
osal_irq_restore(irq_sts);
}