更新rpc

This commit is contained in:
zhangzheng
2023-09-22 21:56:15 +08:00
parent 5a8dd8f34f
commit 7e704d5ce2
7 changed files with 299 additions and 29 deletions

View File

@@ -29,10 +29,28 @@ static inline void rpc_memcpy(void *dst, void *src, size_t size)
*_dst = *_src;
}
}
/*定义变量初始化宏*/
#define RPC_TYPE_INIT(data_type) \
static inline void rpc_var_##data_type##_init(data_type *d) \
{ \
uint8_t *tmp = (uint8_t *)(&(d->data)); \
for (int i = 0; i < sizeof(d->data); i++) \
{ \
tmp[i] = 0; \
} \
}
#define RPC_TYPE_INIT_WITHOUT_IMPL(data_type) \
static inline void rpc_var_##data_type##_init(data_type *d)
#define RPC_TYPE_INIT_FUNC_CALL(data_type, d) \
rpc_var_##data_type##_init(d)
/*end*/
/*普通数据定义*/
#define RPC_CLI_MSG_TO_BUF(data_type, len_type) \
static inline int rpc_cli_msg_to_buf_##data_type(data_type *d, uint8_t *buf, len_type len) \
{ \
if (sizeof(d->data) + rpc_align(len, __alignof(d->data)) >= IPC_MSG_SIZE) \
if (sizeof(d->data) + rpc_align(len, __alignof(d->data)) > IPC_MSG_SIZE) \
{ \
return -ETOLONG; \
} \
@@ -43,10 +61,10 @@ static inline void rpc_memcpy(void *dst, void *src, size_t size)
#define RPC_CLI_MSG_TO_BUF_WITHOUT_IMPL(data_type, len_type) \
static inline int rpc_cli_msg_to_buf_##data_type(data_type *d, uint8_t *buf, len_type len)
#define RPC_CLI_BUF_TO_MSG(data_type, len_type) \
static inline int rpc_cli_buf_to_msg_##data_type(data_type *d, uint8_t *buf, len_type len, len_type max) \
#define RPC_SVR_BUF_TO_MSG(data_type, len_type) \
static inline int rpc_svr_buf_to_msg_##data_type(data_type *d, uint8_t *buf, len_type len, len_type max) \
{ \
if (sizeof(d->data) + rpc_align(len, __alignof(d->data)) >= max) \
if (sizeof(d->data) + rpc_align(len, __alignof(d->data)) > max) \
{ \
return -ETOLONG; \
} \
@@ -54,13 +72,47 @@ static inline void rpc_memcpy(void *dst, void *src, size_t size)
d->data = *((typeof(d->data) *)(buf + len)); \
return sizeof(d->data) + rpc_align(len, __alignof(d->data)); \
}
#define RPC_CLI_BUF_TOMSG_WITHOUT_IMPL(data_type, len_type) \
#define RPC_SVR_BUF_TO_MSG_WITHOUT_IMPL(data_type, len_type) \
static inline int rpc_svr_buf_to_msg_##data_type(data_type *d, uint8_t *buf, len_type len, len_type max)
#define RPC_CLI_BUF_TO_MSG(data_type, len_type) \
static inline int rpc_cli_buf_to_msg_##data_type(data_type *d, uint8_t *buf, len_type len, len_type max) \
{ \
if (sizeof(d->data) + rpc_align(len, __alignof(d->data)) > max) \
{ \
return -ETOLONG; \
} \
len = rpc_align(len, __alignof(d->data)); \
d->data = *((typeof(d->data) *)(buf + len)); \
return sizeof(d->data) + rpc_align(len, __alignof(d->data)); \
}
#define RPC_CLI_BUF_TO_MSG_WITHOUT_IMPL(data_type, len_type) \
static inline int rpc_cli_buf_to_msg_##data_type(data_type *d, uint8_t *buf, len_type len, len_type max)
#define RPC_SVR_MSG_TO_BUF(data_type, len_type) \
static inline int rpc_svr_msg_to_buf_##data_type(data_type *d, uint8_t *buf, len_type len) \
{ \
if (sizeof(d->data) + rpc_align(len, __alignof(d->data)) > IPC_MSG_SIZE) \
{ \
return -ETOLONG; \
} \
len = rpc_align(len, __alignof(d->data)); \
*((typeof(d->data) *)(buf + len)) = d->data; \
return sizeof(d->data) + rpc_align(len, __alignof(d->data)); \
}
#define RPC_SVR_MSG_TO_BUF_WITHOUT_IMPL(data_type, len_type) \
static inline int rpc_svr_msg_to_buf_##data_type(data_type *d, uint8_t *buf, len_type len)
/*end*/
#define RPC_TYPE_DEF_ALL(type) \
RPC_TYPE_DEF(type); \
RPC_CLI_MSG_TO_BUF(rpc_##type##_t, int) \
RPC_CLI_BUF_TO_MSG(rpc_##type##_t, int)
RPC_CLI_BUF_TO_MSG(rpc_##type##_t, int) \
RPC_SVR_BUF_TO_MSG(rpc_##type##_t, int) \
RPC_SVR_MSG_TO_BUF(rpc_##type##_t, int) \
RPC_TYPE_INIT(rpc_##type##_t)
RPC_TYPE_DEF_ALL(int) //!< 定义所有的
@@ -94,7 +146,7 @@ RPC_CLI_MSG_TO_BUF_WITHOUT_IMPL(rpc_array_uint32_t_uint8_t_32_t, int)
len += d->len * sizeof(d->data[0]);
return len;
}
RPC_CLI_BUF_TOMSG_WITHOUT_IMPL(rpc_array_uint32_t_uint8_t_32_t, int)
RPC_CLI_BUF_TO_MSG_WITHOUT_IMPL(rpc_array_uint32_t_uint8_t_32_t, int)
{
if (rpc_align(d->len, __alignof(d->len) + sizeof(d->len)) >= max)
{
@@ -147,7 +199,7 @@ RPC_CLI_MSG_TO_BUF_WITHOUT_IMPL(rpc_ref_array_uint32_t_uint8_t_32_t, int)
len += d->len * sizeof(d->data[0]);
return len;
}
RPC_CLI_BUF_TOMSG_WITHOUT_IMPL(rpc_ref_array_uint32_t_uint8_t_32_t, int)
RPC_CLI_BUF_TO_MSG_WITHOUT_IMPL(rpc_ref_array_uint32_t_uint8_t_32_t, int)
{
if (rpc_align(d->len, __alignof(d->len) + sizeof(d->len)) >= max)
{
@@ -182,6 +234,18 @@ RPC_CLI_MSG_TO_BUF_WITHOUT_IMPL(rpc_obj_handler_t_t, int)
*((umword_t *)(buf + len)) = vpage_create_raw3(KOBJ_ALL_RIGHTS, 0, d->data).raw;
return sizeof(d->data) + rpc_align(len, __alignof(d->data));
}
RPC_CLI_BUF_TO_MSG_WITHOUT_IMPL(rpc_obj_handler_t_t, int)
{
return len;
}
RPC_SVR_MSG_TO_BUF_WITHOUT_IMPL(rpc_obj_handler_t_t, int)
{
return len;
}
RPC_SVR_BUF_TO_MSG_WITHOUT_IMPL(rpc_obj_handler_t_t, int)
{
return len;
}
//!< end
#define RPC_DIR_IN 1
@@ -191,6 +255,7 @@ RPC_CLI_MSG_TO_BUF_WITHOUT_IMPL(rpc_obj_handler_t_t, int)
#define RPC_TYPE_DATA 1
#define RPC_TYPE_BUF 2
/*客户端吧数据放到buf里面去*/
#define RPC_CLI_MSG_TO_BUF_IN(rpc_type, var_type, var, dir, buf, off) \
do \
{ \
@@ -208,6 +273,7 @@ RPC_CLI_MSG_TO_BUF_WITHOUT_IMPL(rpc_obj_handler_t_t, int)
} \
} while (0)
/*客户端从buf里面取出数据*/
#define RPC_CLI_BUF_TO_MSG_OUT(rpc_type, var_type, var, dir, buf, off, max) \
do \
{ \
@@ -225,7 +291,8 @@ RPC_CLI_MSG_TO_BUF_WITHOUT_IMPL(rpc_obj_handler_t_t, int)
} \
} while (0)
#define PRC_CLI_FILL_BUF(rpc_type, var_type, var, dir, buf, off) \
/*客户端往映射缓冲区里面填充数据*/
#define PRC_CLI_FILL_MAP_BUF(rpc_type, var_type, var, dir, buf, off) \
do \
{ \
if (rpc_type == RPC_TYPE_BUF) \
@@ -242,6 +309,42 @@ RPC_CLI_MSG_TO_BUF_WITHOUT_IMPL(rpc_obj_handler_t_t, int)
} \
} while (0)
/*服务端吧数据放到buf里面去*/
#define RPC_SVR_MSG_TO_BUF_IN(rpc_type, var_type, var, dir, buf, off) \
do \
{ \
if (rpc_type == RPC_TYPE_DATA) \
{ \
if (dir == RPC_DIR_OUT || dir == RPC_DIR_INOUT) \
{ \
int ret = rpc_svr_msg_to_buf_##var_type(var, (uint8_t *)(buf), off); \
if (ret < 0) \
{ \
return msg_tag_init4(0, 0, 0, ret); \
} \
off = ret; \
} \
} \
} while (0)
/*服务端从buf里面取出数据*/
#define RPC_SVR_BUF_TO_MSG_OUT(rpc_type, var_type, var, dir, buf, off, max) \
do \
{ \
if (rpc_type == RPC_TYPE_DATA) \
{ \
if (dir == RPC_DIR_IN || dir == RPC_DIR_INOUT) \
{ \
int ret = rpc_svr_buf_to_msg_##var_type(var, (uint8_t *)(buf), off, max); \
if (ret < 0) \
{ \
return msg_tag_init4(0, 0, 0, ret); \
} \
off = ret; \
} \
} \
} while (0)
#define RPC_GENERATION_CALL1(struct_type, op, func_name, type0, dir0, rpc_type0, name0) \
msg_tag_t struct_type##_##func_name##_call(obj_handler_t hd, type0 *var0) \
{ \
@@ -259,11 +362,11 @@ RPC_CLI_MSG_TO_BUF_WITHOUT_IMPL(rpc_obj_handler_t_t, int)
off += rpc_align(sizeof(op_val), __alignof(op)); \
\
RPC_CLI_MSG_TO_BUF_IN(rpc_type0, type0, var0, dir0, (uint8_t *)msg_ipc->msg_buf, off); \
PRC_CLI_FILL_BUF(rpc_type0, type0, var0, dir0, (uint8_t *)msg_ipc->msg_buf, off); \
PRC_CLI_FILL_MAP_BUF(rpc_type0, type0, var0, dir0, (uint8_t *)msg_ipc->msg_buf, off); \
msg_tag_t tag = ipc_call(hd, msg_tag_init4(0, ROUND_UP(off, WORD_BYTES), 0, 0), \
ipc_timeout_create2(0, 0)); \
\
if (msg_tag_get_val(tag)) \
if (msg_tag_get_val(tag) < 0) \
{ \
return tag; \
} /*拷贝返回的数据*/ \
@@ -271,14 +374,38 @@ RPC_CLI_MSG_TO_BUF_WITHOUT_IMPL(rpc_obj_handler_t_t, int)
RPC_CLI_BUF_TO_MSG_OUT(rpc_type0, type0, var0, dir0, (uint8_t *)msg_ipc->msg_buf, off, tag.msg_buf_len *WORD_BYTES); \
return tag; \
}
// #define RPC_GENERATION_DISPATCH1(struct_type, op, func_name, \
// type0, dir0, rpc_type0, name0)
// msg_tag_t tt_dispatch(msg_tag_t tag, ipc_msg_t *ipc_msg)
// {
// type0 var0;
#define RPC_GENERATION_DISPATCH1(struct_type, op, func_name, \
type0, dir0, rpc_type0, name0) \
msg_tag_t struct_type##_##func_name##_dispatch(struct_type *obj, msg_tag_t tag, ipc_msg_t *ipc_msg) \
{ \
type0 var0; \
size_t op_val; \
uint8_t *value = (uint8_t *)(ipc_msg->msg_buf); \
int off = 0; \
\
RPC_TYPE_INIT_FUNC_CALL(type0, &var0); \
\
/*取得op*/ \
op_val = *((typeof(op) *)value); \
off += sizeof(typeof(op)); \
off = rpc_align(off, __alignof(typeof(op))); \
\
RPC_SVR_BUF_TO_MSG_OUT(rpc_type0, type0, &var0, dir0, value, off, tag.msg_buf_len *WORD_BYTES); \
\
short ret_val = struct_type##_##func_name##_op(obj, &var0); \
\
if (ret_val < 0) \
{ \
return msg_tag_init4(0, 0, 0, ret_val); \
} \
off = 0; \
RPC_SVR_MSG_TO_BUF_IN(rpc_type0, type0, &var0, dir0, value, off); \
return msg_tag_init4(0, ROUND_UP(off, WORD_BYTES), 0, ret_val); \
}
#define RPC_GENERATION_OP1(struct_type, op, func_name, type0, dir0, rpc_type0, name0) \
short struct_type##_##func_name##_op(struct_type *obj, type0 *name0)
// }
#define RPC_GENERATION_CALL2(struct_type, op, func_name, \
type0, dir0, rpc_type0, name0, \
type1, dir1, rpc_type1, name1) \
@@ -299,13 +426,13 @@ RPC_CLI_MSG_TO_BUF_WITHOUT_IMPL(rpc_obj_handler_t_t, int)
off += rpc_align(sizeof(op_val), __alignof(op)); \
\
RPC_CLI_MSG_TO_BUF_IN(rpc_type0, type0, var0, dir0, (uint8_t *)msg_ipc->msg_buf, off); \
PRC_CLI_FILL_BUF(rpc_type0, type0, var0, dir0, (uint8_t *)msg_ipc->msg_buf, off_buf); \
PRC_CLI_FILL_MAP_BUF(rpc_type0, type0, var0, dir0, (uint8_t *)msg_ipc->msg_buf, off_buf); \
RPC_CLI_MSG_TO_BUF_IN(rpc_type1, type1, var1, dir1, (uint8_t *)msg_ipc->msg_buf, off); \
PRC_CLI_FILL_BUF(rpc_type1, type1, var1, dir1, (uint8_t *)msg_ipc->msg_buf, off_buf); \
PRC_CLI_FILL_MAP_BUF(rpc_type1, type1, var1, dir1, (uint8_t *)msg_ipc->msg_buf, off_buf); \
msg_tag_t tag = ipc_call(hd, msg_tag_init4(0, ROUND_UP(off, WORD_BYTES), 0, 0), \
ipc_timeout_create2(0, 0)); \
\
if (msg_tag_get_val(tag)) \
if (msg_tag_get_val(tag) < 0) \
{ \
return tag; \
} /*拷贝返回的数据*/ \

View File

@@ -40,7 +40,8 @@ add_dependencies(
)
set_target_properties(
fatfs.elf PROPERTIES LINK_FLAGS
"-T ${CMAKE_CURRENT_SOURCE_DIR}/link.lds -pie --gc-section -no-dynamic-linker --no-warn-rwx-segments"
"-T ${CMAKE_CURRENT_SOURCE_DIR}/link.lds -pie --gc-section -no-dynamic-linker "
#--no-warn-rwx-segments
)
add_custom_target(
fatfs_dump ALL

View File

@@ -33,7 +33,8 @@ add_dependencies(init.elf
muslc
)
set_target_properties(init.elf PROPERTIES LINK_FLAGS
"-T ${CMAKE_CURRENT_SOURCE_DIR}/link.lds -pie --gc-section -no-dynamic-linker --no-warn-rwx-segments"
"-T ${CMAKE_CURRENT_SOURCE_DIR}/link.lds -pie --gc-section -no-dynamic-linker "
#--no-warn-rwx-segments
)
#-T ${CMAKE_CURRENT_SOURCE_DIR}/link.lds
add_custom_target(

View File

@@ -38,7 +38,8 @@ add_dependencies(shell.elf
muslc
)
set_target_properties(shell.elf PROPERTIES LINK_FLAGS
"-T ${CMAKE_CURRENT_SOURCE_DIR}/link.lds -pie --gc-section -no-dynamic-linker --no-warn-rwx-segments"
"-T ${CMAKE_CURRENT_SOURCE_DIR}/link.lds -pie --gc-section -no-dynamic-linker "
#--no-warn-rwx-segments
)
add_custom_target(
shell_dump ALL

View File

@@ -1,15 +1,140 @@
#include "u_rpc.h"
#include <string.h>
#include <stdio.h>
typedef struct test_svr
{
} test_svr_t;
RPC_GENERATION_CALL1(test_svr_t, 0, register, rpc_int_t, RPC_DIR_IN, RPC_TYPE_BUF, var0)
RPC_GENERATION_CALL2(test_svr_t, 1, query,
rpc_ref_array_uint32_t_uint8_t_32_t, RPC_DIR_IN, RPC_TYPE_DATA, var0, rpc_int_t, RPC_DIR_IN, RPC_TYPE_DATA, var1)
static test_svr_t test;
RPC_GENERATION_OP1(test_svr_t, 0, register, rpc_int_t, RPC_DIR_IN, RPC_TYPE_DATA, var0)
{
printf("var0 val is %d\n", var0->data);
return 1;
}
// RPC_GENERATION_DISPATCH1(test_svr_t, 0, register, rpc_int_t, RPC_DIR_IN, RPC_TYPE_DATA, var0)
msg_tag_t test_svr_t_register_dispatch(test_svr_t *obj, msg_tag_t tag, ipc_msg_t *ipc_msg)
{
rpc_int_t var0;
size_t op_val;
uint8_t *value = (uint8_t *)(ipc_msg->msg_buf);
int off = 0;
rpc_var_rpc_int_t_init(&var0);
op_val = *((typeof(0) *)value);
off += sizeof(typeof(0));
off = rpc_align(off, __alignof(typeof(0)));
do
{
if (1 == 1)
{
if (1 == 1 || 1 == 4)
{
int ret = rpc_svr_buf_to_msg_rpc_int_t(&var0, (uint8_t *)(value), off, tag.msg_buf_len * (sizeof(void *)));
if (ret < 0)
{
return ((msg_tag_t){.flags = (0), .msg_buf_len = (0), .map_buf_len = (0), .prot = (ret)});
}
off = ret;
}
}
} while (0);
short ret_val = test_svr_t_register_op(obj, &var0);
if (ret_val < 0)
{
return ((msg_tag_t){.flags = (0), .msg_buf_len = (0), .map_buf_len = (0), .prot = (ret_val)});
}
off = 0;
do
{
if (1 == 1)
{
if (1 == 2 || 1 == 4)
{
int ret = rpc_svr_msg_to_buf_rpc_int_t(&var0, (uint8_t *)(value), off);
if (ret < 0)
{
return ((msg_tag_t){.flags = (0), .msg_buf_len = (0), .map_buf_len = (0), .prot = (ret)});
}
off = ret;
}
}
} while (0);
return ((msg_tag_t){.flags = (0), .msg_buf_len = ((((off) / ((sizeof(void *)))) + (((off) % ((sizeof(void *)))) ? 1 : 0))), .map_buf_len = (0), .prot = (ret_val)});
}
// RPC_GENERATION_CALL1(test_svr_t, 0, register, rpc_int_t, RPC_DIR_IN, RPC_TYPE_DATA, var0)
msg_tag_t test_svr_t_register_call(obj_handler_t hd, rpc_int_t *var0)
{
void *buf;
ipc_msg_t *msg_ipc;
thread_msg_buf_get(2, (umword_t *)(&buf), ((void *)0));
msg_ipc = (ipc_msg_t *)buf;
int off = 0;
int ret = -1;
int op_val = 0;
rpc_memcpy(msg_ipc->msg_buf, &op_val, sizeof(op_val));
off += rpc_align(sizeof(op_val), __alignof(0));
do
{
if (1 == 1)
{
if (1 == 1 || 1 == 4)
{
int ret = rpc_cli_msg_to_buf_rpc_int_t(var0, (uint8_t *)((uint8_t *)msg_ipc->msg_buf), off);
if (ret < 0)
{
return ((msg_tag_t){.flags = (0), .msg_buf_len = (0), .map_buf_len = (0), .prot = (ret)});
}
off = ret;
}
}
} while (0);
do
{
if (1 == 2)
{
if (1 == 1 || 1 == 4)
{
int ret = rpc_cli_msg_to_buf_rpc_int_t(var0, (uint8_t *)((uint8_t *)msg_ipc->msg_buf), off);
if (ret < 0)
{
return ((msg_tag_t){.flags = (0), .msg_buf_len = (0), .map_buf_len = (0), .prot = (ret)});
}
off = ret;
}
}
} while (0);
// msg_tag_t tag = ipc_call(hd, ((msg_tag_t){.flags = (0), .msg_buf_len = ((((off) / ((sizeof(void *)))) + (((off) % ((sizeof(void *)))) ? 1 : 0))), .map_buf_len = (0), .prot = (0)}), ipc_timeout_create2(0, 0));
msg_tag_t tag = test_svr_t_register_dispatch(&test, ((msg_tag_t){.flags = (0), .msg_buf_len = ((((off) / ((sizeof(void *)))) + (((off) % ((sizeof(void *)))) ? 1 : 0))), .map_buf_len = (0), .prot = (0)}), msg_ipc);
if (((int16_t)((tag).prot)) < 0)
{
return tag;
}
off = 0;
do
{
if (1 == 1)
{
if (1 == 2 || 1 == 4)
{
int ret = rpc_cli_buf_to_msg_rpc_int_t(var0, (uint8_t *)((uint8_t *)msg_ipc->msg_buf), off, tag.msg_buf_len * (sizeof(void *)));
if (ret < 0)
{
return ((msg_tag_t){.flags = (0), .msg_buf_len = (0), .map_buf_len = (0), .prot = (ret)});
}
off = ret;
}
}
} while (0);
return tag;
}
// msg_tag_t tag = test_svr_t_register_dispatch(&test, ((msg_tag_t){.flags = (0), .msg_buf_len = ((((off) / ((sizeof(void *)))) + (((off) % ((sizeof(void *)))) ? 1 : 0))), .map_buf_len = (0), .prot = (0)}), msg_ipc);
// RPC_GENERATION_CALL2(test_svr_t, 1, query,
// rpc_ref_array_uint32_t_uint8_t_32_t, RPC_DIR_IN, RPC_TYPE_DATA, var0, rpc_int_t, RPC_DIR_IN, RPC_TYPE_DATA, var1)
#include <string.h>
void rpc_test(void)
{
rpc_int_t var0 = {.data = 1};
@@ -19,5 +144,5 @@ void rpc_test(void)
.data = "test",
.len = strlen("test") + 1};
test_svr_t_query_call(15, &str_tst, &var0);
// test_svr_t_query_call(15, &str_tst, &var0);
}