增加事件驱动和业务回滚处理方式

This commit is contained in:
冯佳
2026-03-04 08:50:04 +08:00
parent cc4c361df6
commit 47a9dff6ef
25 changed files with 2626 additions and 1780 deletions

101
app/event_queue.h Normal file
View File

@ -0,0 +1,101 @@
/*
* event_queue.h
*
* Created on: 2026-03-04
* Author: RT-Thread
*
* 功能: 事件队列模块头文件
* 依赖: RT-Thread Nano, osal
* 跨平台适配: 基于RT-Thread Nano使用标准API
*/
#ifndef EVENT_QUEUE_H
#define EVENT_QUEUE_H
#include <rtthread.h>
#include "osal.h"
/* 事件类型定义 */
typedef enum {
EVENT_TYPE_NONE = 0,
EVENT_TYPE_SENSOR_DATA,
EVENT_TYPE_NETWORK_CONNECTED,
EVENT_TYPE_NETWORK_DISCONNECTED,
EVENT_TYPE_TCP_CLIENT_CONNECTED,
EVENT_TYPE_TCP_CLIENT_DISCONNECTED,
EVENT_TYPE_TCP_DATA_RECEIVED,
EVENT_TYPE_TIMER,
EVENT_TYPE_ERROR,
EVENT_TYPE_MAX
} event_type_t;
/* 事件优先级定义 */
typedef enum {
EVENT_PRIORITY_LOW = 0,
EVENT_PRIORITY_NORMAL,
EVENT_PRIORITY_HIGH,
EVENT_PRIORITY_CRITICAL
} event_priority_t;
/* 事件结构体 */
typedef struct {
event_type_t type; /* 事件类型 */
event_priority_t priority; /* 事件优先级 */
void *data; /* 事件数据 */
size_t data_size; /* 事件数据大小 */
rt_tick_t timestamp; /* 事件时间戳 */
} event_t;
/* 事件队列大小 */
#define EVENT_QUEUE_SIZE 64
/* 事件队列结构体 */
typedef struct {
event_t events[EVENT_QUEUE_SIZE]; /* 事件数组 */
int head; /* 队头索引 */
int tail; /* 队尾索引 */
int count; /* 事件数量 */
osal_mutex_t mutex; /* 互斥锁 */
osal_sem_t sem; /* 信号量 */
} event_queue_t;
/**
* @brief 初始化事件队列
* @return 0 成功非0 失败
*/
int event_queue_init(void);
/**
* @brief 销毁事件队列
* @return 0 成功非0 失败
*/
int event_queue_deinit(void);
/**
* @brief 向事件队列中添加事件
* @param event 事件指针
* @return 0 成功非0 失败
*/
int event_queue_push(event_t *event);
/**
* @brief 从事件队列中获取事件
* @param event 事件指针,用于存储获取的事件
* @param timeout 超时时间单位ms
* @return 0 成功非0 失败
*/
int event_queue_pop(event_t *event, int timeout);
/**
* @brief 获取事件队列中的事件数量
* @return 事件数量
*/
int event_queue_get_count(void);
/**
* @brief 清空事件队列
* @return 0 成功非0 失败
*/
int event_queue_clear(void);
#endif /* EVENT_QUEUE_H */