1. 完善RPC注释

This commit is contained in:
coffee 2025-09-08 14:58:58 +08:00
parent 4887930ea2
commit 3a20363f23
2 changed files with 61 additions and 28 deletions

View File

@ -2,6 +2,15 @@
* : 2025-09-04
* : coffee
* : ARM和MCU交互通信模块
* demo:
* RPC调用
* HDRPCInitCall(, RPC回调缓存, , RPC会话);
* :
* HDRPCReadData(...);
*
* HDRPCSendData(sesion, , );
*
* HDRPCSendDataNotCall(sesion, );
*/
#ifndef __HDRPC_H__
@ -9,17 +18,11 @@
#include <stdint.h>
// 无效类型
#define HDRPC_INVAID_TYPE 0xFF
// 无效功能
#define HDRPC_INVAID_FUNC 0xFFFF
// 默认超时
// 设置超时时间
#define HDRPC_TIMEOUT 3000
// 如果使用栈参数解析, 则最大支持的参数个数
#define HDRPC_USE_SESSION_ARGS (5)
#define HDRPC_USE_SESSION_ARGS (9)
// 使用64位
// #define HDRPC_USE_64
@ -42,7 +45,6 @@ typedef uint32_t HDRPCValue_t;
* 0x11: UTF-16BE字符串,
* 0x20: , u16类型
*/
enum eHDRPCType
{
kHDRPCUserData = 0x00, ///< 用户数据
@ -55,6 +57,14 @@ enum eHDRPCType
kHDRPCException = 0x20, ///< 异常, u16
};
// 协议异常指令
enum eHDRPCException {
kHDRPCNotException = 0x00, ///< 无异常
kHDRPCNotSupportFunction = 0x01, ///< 对应功能不支持
kHDRPCArgsTypeNotMatch = 0x02, ///< 参数类型不匹配
};
typedef struct __attribute__((packed)) _HDRPCBuffer
{
uint8_t len; ///< 数据长度
@ -62,7 +72,7 @@ typedef struct __attribute__((packed)) _HDRPCBuffer
} _HDRPCBuffer;
///< 参数包
typedef struct __attribute__((packed)) _HDRPCArgs
typedef struct __attribute__((packed)) HDRPCArgs
{
uint8_t type; ///< 类型数据
union __attribute__((packed))
@ -79,29 +89,32 @@ typedef struct __attribute__((packed)) _HDRPCArgs
typedef struct __attribute__((packed)) HDRPCSession
{
uint8_t index; ///< 当前添加到的参数索引
uint8_t len; ///< 参数总长度
uint16_t func; ///< 调用功能, 用于解析数据得到
HDRPCArgs *args;
uint16_t func; ///< 调用功能, 用于解析数据得到
uint8_t index; ///< 当前添加到的参数索引
uint8_t exceptionSkip : 1; ///< 异常时不要调用回调(默认0: 调用回调, 1: 不调用回调)
uint8_t len : 7; ///< 参数总长度
HDRPCArgs *args; ///< 参数
} HDRPCSession;
// 此宏用于其他回调的参数声明
#define HDRPC_CALL_ARGS HDRPCSession *session
typedef void (*HDRPCCallback)(HDRPCSession *session);
#define HDRPC_CALL_ARGS HDRPCSession *session, uint8_t exception
#define HDRPC_IS_EXCEPTION() (exception != kHDRPCNotException)
typedef void (*HDRPCCallback)(HDRPCSession *session, uint8_t exception);
typedef void (*HDRPCSendData)(HDRPCSession *session);
// 调用缓存, 数据由用户提供
// 调用缓存, 内存由用户提供
typedef struct __attribute__((packed)) HDRPCCallBuffer {
uint8_t enbale : 1;
uint8_t nameLen : 7;
uint32_t time;
const char *name;
HDRPCCallback callback;
uint8_t enbale : 1; ///< 是否启用
uint8_t nameLen : 7; ///< 函数名长度
uint32_t time; ///< 上次调用时间
const char *name; ///< 函数名
HDRPCCallback callback; ///< 回调
} HDRPCCallBuffer;
/**
* @brief RPC会话
* @param session
* @param args ,
* @param len
*/
void HDRPCInitSession(HDRPCSession *session, HDRPCArgs *args, uint8_t len);
@ -109,14 +122,14 @@ void HDRPCInitSession(HDRPCSession *session, HDRPCArgs *args, uint8_t len);
/**
* @brief RPC调用
* @param sendCall
* @param callback , RPC回调数量
* @param callBuff , RPC回调数量, ,
* @param callLen
* @param buffer (使)
* @param buffer RPC会(使)
*/
void HDRPCInitCall(HDRPCSendData sendCall, HDRPCCallBuffer *callBuff, uint8_t callLen, HDRPCSession *buffer);
/**
* @brief 使, 使
* @brief 使,
* @param _name
* @param _len
*/

View File

@ -10,6 +10,9 @@
#define LogD(format, ...) printf("[%s:%s:%d]"format "\r\n", __FILE_NAME__, __FUNCTION__, __LINE__, ##__VA_ARGS__)
#endif
// 无效功能
#define HDRPC_INVAID_FUNC 0xFFFF
struct RPCInfo {
HDRPCSendData writeCall;
HDRPCCallBuffer *callback;
@ -29,13 +32,15 @@ static void UpdateTimeout(HDRPCCallBuffer *callBuff) {
}
uint32_t curr = HDLogGetTime();
#ifdef HDRPC_TIMEOUT
if (curr - callBuff->time > HDRPC_TIMEOUT) {
callBuff->enbale = 0;
LogD("Call[%s] Timeout", callBuff->name);
}
#endif
}
static void Call(HDRPCSession *session) {
static void Call(HDRPCSession *session, uint16_t exception) {
if (session == NULL) {
LogD("Session is nullptr");
return ;
@ -63,7 +68,7 @@ static void Call(HDRPCSession *session) {
}
LogD("Call[%s]", sInfo.callback[i].name);
sInfo.callback[i].callback(session);
sInfo.callback[i].callback(session, exception);
LogD("Call[%s] End", sInfo.callback[i].name);
sInfo.callback[i].enbale = 0;
@ -238,7 +243,22 @@ static void ReadCall(HDRPCSession *session, void *data, uint16_t len) {
return ;
}
Call(session);
uint16_t exception = kHDRPCNotException;;
for (uint8_t i = 0; i < session->index; ++i) {
if (session->args[i].type != kHDRPCException) {
continue;
}
exception = session->args[i].dataU16;
LogD("Call type[0x%x] Exception[0x%x]", session->args[i].type, exception);
// 开启异常跳过后, 出现异常不回调
if (session->exceptionSkip) {
return ;
}
}
Call(session, exception);
}