1. 修改检查参数的接口, 使其兼容嵌入式编译器

This commit is contained in:
coffee 2025-09-11 15:54:40 +08:00
parent 2b118cc7aa
commit 22b508ad31
2 changed files with 38 additions and 6 deletions

View File

@ -122,6 +122,13 @@ typedef struct __attribute__((packed)) HDRPCCallBuffer {
HDRPCCallback callback; ///< 回调 HDRPCCallback callback; ///< 回调
} HDRPCCallBuffer; } HDRPCCallBuffer;
#ifndef __COUNT_ARGS_IMPL
#define __COUNT_ARGS_IMPL(_, _1, _2, _3, _4, _5, _6, _7, _8, _9, N, ...) N
#endif
#ifndef __COUNT_ARGS
#define __COUNT_ARGS(...) __COUNT_ARGS_IMPL(dummy, ##__VA_ARGS__, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
#endif
/** /**
* @brief RPC会话 * @brief RPC会话
* @param session * @param session
@ -208,14 +215,16 @@ uint8_t HDRPCAddArgs(HDRPCSession *session, HDRPCArgs *args);
uint8_t HDRPCParseArgs(void *data, uint16_t len, HDRPCSession *session); uint8_t HDRPCParseArgs(void *data, uint16_t len, HDRPCSession *session);
/** /**
* @brief * @brief , 9
* @param session * @param session
* @param type * @param type
* @param index
* @param len * @param len
* @return 1, 0 * @return 1, 0
*/ */
uint8_t _HDRPCCheckArgs(HDRPCSession *session, uint8_t *type, uint8_t len); uint8_t _HDRPCCheckArgs(HDRPCSession *session, uint8_t *type, uint8_t index, uint8_t len);
#define HDRPCCheckArgs(session, ...) ({ uint8_t __type[] = {__VA_ARGS__}; uint8_t __ret = _HDRPCCheckArgs(session, __type, sizeof(__type) / sizeof(__type[0])); __ret; }) uint8_t __HDRPCCheckArgs(HDRPCSession *session, int len, ...);
#define HDRPCCheckArgs(session, ...) __HDRPCCheckArgs((session), __COUNT_ARGS(__VA_ARGS__), __VA_ARGS__)
/** /**
* @brief * @brief

View File

@ -2,6 +2,7 @@
#include "HDRPC.h" #include "HDRPC.h"
#include "HDLog.h" #include "HDLog.h"
#include <stdarg.h>
#include <stdint.h> #include <stdint.h>
#include <string.h> #include <string.h>
@ -459,11 +460,11 @@ uint8_t HDRPCParseArgs(void *data, uint16_t len, HDRPCSession *session)
return 1; return 1;
} }
uint8_t _HDRPCCheckArgs(HDRPCSession *session, uint8_t *type, uint8_t len) uint8_t _HDRPCCheckArgs(HDRPCSession *session, uint8_t *type, uint8_t index, uint8_t len)
{ {
for (uint16_t i = 0; i < len; ++i) { for (uint8_t i = index; i < len; ++i) {
if (session->args[i].type != type[i]) { if (session->args[i].type != type[i]) {
LogD("Check index[%d] Type[0x%x] faild", i, type[i]); LogD("Check index[%d] Type[0x%x][0x%x] faild", i, session->args[i].type, type[i]);
return 0; return 0;
} }
} }
@ -471,6 +472,28 @@ uint8_t _HDRPCCheckArgs(HDRPCSession *session, uint8_t *type, uint8_t len)
return 1; return 1;
} }
uint8_t __HDRPCCheckArgs(HDRPCSession *session, int len, ...)
{
uint8_t type[10];
int i = 0;
va_list args;
va_start(args, len);
for (; i < len; i += sizeof(type)) {
const uint8_t argsLen = (len - i) > sizeof(type) ? sizeof(type) : (len - i);
for (uint8_t j = 0; j < argsLen; ++j) {
type[j] = va_arg(args, int);
}
if (!_HDRPCCheckArgs(session, type, i, argsLen)) {
va_end(args);
return 0;
}
}
va_end(args);
return 1;
}
HDRPCValue_t _HDRPCGetValue(HDRPCSession *session, uint8_t type, uint8_t index) HDRPCValue_t _HDRPCGetValue(HDRPCSession *session, uint8_t type, uint8_t index)
{ {
if (session == NULL) { if (session == NULL) {