1. 修改检查参数的接口, 使其兼容嵌入式编译器
This commit is contained in:
parent
2b118cc7aa
commit
22b508ad31
@ -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 获取指定位置数值数据
|
||||||
|
|||||||
29
src/HDRPC.c
29
src/HDRPC.c
@ -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) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user