107 lines
2.8 KiB
C
107 lines
2.8 KiB
C
/**
|
|
* 日期: 2025-10-18
|
|
* 作者: coffee
|
|
* 描述: 协议绑定回调, 用于等待数据回送再执行, 避免阻塞主进程
|
|
*/
|
|
|
|
#ifndef _H_PROTOCOL_BIND_H_
|
|
#define _H_PROTOCOL_BIND_H_
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
// 需要处理的最大数量
|
|
#ifndef HPROTOCOL_BIND_MAX
|
|
#define HPROTOCOL_BIND_MAX (2)
|
|
#endif
|
|
|
|
// 协议绑定无效值
|
|
#ifndef HPROTOCOL_BIND_INVALID
|
|
#define HPROTOCOL_BIND_INVALID (-1)
|
|
#endif
|
|
|
|
// 协议绑定默认超时时间
|
|
#ifndef HPROTOCOL_BIND_TIMEOUT
|
|
#define HPROTOCOL_BIND_TIMEOUT (3000)
|
|
#endif
|
|
|
|
///< 协议绑定索引
|
|
typedef int16_t HProtocolBind_t;
|
|
|
|
///< 协议ID
|
|
typedef uint8_t HProtocolBindId_t;
|
|
|
|
///< 功能码
|
|
typedef uint32_t HProtocolBindFunc_t;
|
|
|
|
///< 协议回调
|
|
typedef void (*HProtocolBindCallback)(HProtocolBind_t index, uint8_t* data, uint16_t len);
|
|
#define HPROTOCOL_BIND_CALL_ARGS HProtocolBind_t index, uint8_t* data, uint16_t len
|
|
#define HPROTOCOL_IS_TIMEOUT() (data == NULL && len == 0)
|
|
|
|
///< 协议会话
|
|
typedef struct HProtocolBindInfo
|
|
{
|
|
uint32_t enable : 2; ///< 是否启用
|
|
uint32_t delayMs : 20; ///< 超时时间(ms)
|
|
uint32_t isTimeout : 1; ///< 是否超时
|
|
uint32_t keepAlive : 1; ///< 是否保持会话
|
|
HProtocolBindFunc_t func; ///< 功能码
|
|
uint32_t lastTime; ///< 发送时间
|
|
HProtocolBindCallback callback; ///< 回调, 超时时执行的数据和长度为空
|
|
} HProtocolBindInfo;
|
|
|
|
/**
|
|
* @brief 注册协议绑定信息
|
|
* @param id 协议ID
|
|
* @param info 协议绑定信息
|
|
* @param infoLen 协议绑定信息长度
|
|
*/
|
|
uint8_t HProtocolBindRegister(uint8_t id, HProtocolBindInfo *info, uint8_t infoLen);
|
|
|
|
/**
|
|
* @brief 绑定协议功能码等待回调
|
|
* @param id 协议ID
|
|
* @param func 功能码
|
|
* @param callback 回调
|
|
* @param keepAlive 是否保持会话, 保持会话则不超时
|
|
* @return HProtocolBind_t -1失败
|
|
*/
|
|
HProtocolBind_t HProtocolBindAddTask(HProtocolBindId_t id, HProtocolBindFunc_t func, HProtocolBindCallback callback, uint8_t keepAlive);
|
|
|
|
/**
|
|
* @brief 设置超时时间
|
|
* @param index 绑定索引
|
|
* @param delayMs 超时时间
|
|
*/
|
|
void HProtocolBindSetTimeout(HProtocolBind_t index, uint32_t delayMs);
|
|
|
|
/**
|
|
* @brief 删除协议绑定
|
|
* @param index 绑定索引
|
|
*/
|
|
void HProtocolBindRemoveTask(HProtocolBind_t index);
|
|
|
|
/**
|
|
* @brief 查询是否超时, 用于超时回调检查
|
|
* @param index 绑定索引
|
|
* @return true超时 false未超时
|
|
*/
|
|
uint8_t HProtocolBindIsTimeout(HProtocolBind_t index);
|
|
|
|
/**
|
|
* @brief 更新协议数据
|
|
* @param id 协议ID
|
|
* @param func 功能码
|
|
* @param data 数据
|
|
* @param len 数据长度
|
|
*/
|
|
void HProtocolBindUpdateData(HProtocolBindId_t id, HProtocolBindFunc_t func, void* data, uint16_t len);
|
|
|
|
/**
|
|
* @brief 调度协议绑定
|
|
*/
|
|
void HProtocolBindRun(HProtocolBindId_t id);
|
|
|
|
#endif // _H_PROTOCOL_BIND_H_
|