From 0e49c75cd4873f1d249daadd44a9c1018d31c65f Mon Sep 17 00:00:00 2001 From: zhangzheng <1358745329@qq.com> Date: Sun, 24 Sep 2023 19:26:05 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Drpc=20bug=EF=BC=8Cfatfs?= =?UTF-8?q?=E6=9C=8D=E5=8A=A1=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .vscode/settings.json | 3 +- mkrtos_user/lib/sys_svr/inc/fs_cli.h | 4 + mkrtos_user/lib/sys_svr/inc/fs_svr.h | 4 + mkrtos_user/lib/sys_svr/inc/rpc_prot.h | 1 + mkrtos_user/lib/sys_svr/src/fs_cli.c | 303 ++++++++++++++++++++ mkrtos_user/lib/sys_svr/src/fs_svr.c | 226 ++++++++++++++- mkrtos_user/lib/sys_svr/src/ns_cli.c | 3 +- mkrtos_user/lib/sys_util/inc/u_rpc.h | 106 ++++++- mkrtos_user/lib/sys_util/inc/u_rpc_1.h | 2 +- mkrtos_user/lib/sys_util/src/u_app_loader.c | 5 +- mkrtos_user/server/fs/fatfs/fs_rpc.c | 99 ++++++- mkrtos_user/server/fs/fatfs/fs_rpc.h | 1 + mkrtos_user/server/fs/fatfs/heap_stack.c | 2 +- mkrtos_user/server/init/src/namespace.c | 46 --- mkrtos_user/server/shell/src/main.c | 12 +- 15 files changed, 745 insertions(+), 72 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 8469e101f..e55cb361a 100755 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -144,7 +144,8 @@ "ns_svr.h": "c", "u_rpc_2.h": "c", "ns_cli.h": "c", - "fs_cli.h": "c" + "fs_cli.h": "c", + "u_rpc_1.h": "c" }, "cortex-debug.showRTOS": false, } \ No newline at end of file diff --git a/mkrtos_user/lib/sys_svr/inc/fs_cli.h b/mkrtos_user/lib/sys_svr/inc/fs_cli.h index 7239bd831..6309d1bc3 100644 --- a/mkrtos_user/lib/sys_svr/inc/fs_cli.h +++ b/mkrtos_user/lib/sys_svr/inc/fs_cli.h @@ -1,3 +1,7 @@ #pragma once int fs_open(const char *path, int flags, int mode); +int fs_read(int fd, void *buf, size_t len); +int fs_write(int fd, void *buf, size_t len); +int fs_close(int fd); +int fs_lseek(int fd, int offs, int whence); \ No newline at end of file diff --git a/mkrtos_user/lib/sys_svr/inc/fs_svr.h b/mkrtos_user/lib/sys_svr/inc/fs_svr.h index 4de3300bb..7a499e1bd 100644 --- a/mkrtos_user/lib/sys_svr/inc/fs_svr.h +++ b/mkrtos_user/lib/sys_svr/inc/fs_svr.h @@ -12,3 +12,7 @@ typedef struct fs void fs_init(fs_t *fs); int fs_svr_open(const char *path, int flags, int mode); +int fs_svr_read(int fd, void *buf, size_t len); +int fs_svr_write(int fd, void *buf, size_t len); +void fs_svr_close(int fd); +int fs_svr_lseek(int fd, int offs, int whence); diff --git a/mkrtos_user/lib/sys_svr/inc/rpc_prot.h b/mkrtos_user/lib/sys_svr/inc/rpc_prot.h index afa889341..bef9b2593 100644 --- a/mkrtos_user/lib/sys_svr/inc/rpc_prot.h +++ b/mkrtos_user/lib/sys_svr/inc/rpc_prot.h @@ -9,3 +9,4 @@ #define FS_CLOSE ((uint16_t)1) #define FS_READ ((uint16_t)2) #define FS_WRITE ((uint16_t)3) +#define FS_LSEEK ((uint16_t)4) diff --git a/mkrtos_user/lib/sys_svr/src/fs_cli.c b/mkrtos_user/lib/sys_svr/src/fs_cli.c index 806cea6e8..d2e23ee05 100644 --- a/mkrtos_user/lib/sys_svr/src/fs_cli.c +++ b/mkrtos_user/lib/sys_svr/src/fs_cli.c @@ -14,7 +14,176 @@ RPC_GENERATION_CALL3(fs_t, FS_OPEN, open, rpc_ref_array_uint32_t_uint8_t_32_t, rpc_array_uint32_t_uint8_t_32_t, RPC_DIR_IN, RPC_TYPE_DATA, path, rpc_int_t, rpc_int_t, RPC_DIR_IN, RPC_TYPE_DATA, flags, rpc_int_t, rpc_int_t, RPC_DIR_IN, RPC_TYPE_DATA, mode) +// RPC_GENERATION_CALL3(fs_t, FS_READ, read, +// rpc_int_t, rpc_int_t, RPC_DIR_IN, RPC_TYPE_DATA, fd, +// rpc_ref_array_uint32_t_uint8_t_32_t, rpc_array_uint32_t_uint8_t_32_t, RPC_DIR_OUT, RPC_TYPE_DATA, buf, +// rpc_int_t, rpc_int_t, RPC_DIR_IN, RPC_TYPE_DATA, len) +msg_tag_t fs_t_read_call(obj_handler_t hd, rpc_int_t *var0, rpc_ref_array_uint32_t_uint8_t_32_t *var1, rpc_int_t *var2) +{ + 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 off_buf = 0; + int ret = -1; + size_t op_val = ((uint16_t)2); + rpc_memcpy(msg_ipc->msg_buf, &op_val, sizeof(op_val)); + off += rpc_align(sizeof(op_val), __alignof(((uint16_t)2))); + 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->map_buf), off_buf); + if (ret < 0) + { + return ((msg_tag_t){.flags = (0), .msg_buf_len = (0), .map_buf_len = (0), .prot = (ret)}); + } + off_buf = ret; + } + } + } while (0); + do + { + if (1 == 1) + { + if (2 == 1 || 2 == 4) + { + int ret = rpc_cli_msg_to_buf_rpc_ref_array_uint32_t_uint8_t_32_t(var1, (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 (2 == 1 || 2 == 4) + { + int ret = rpc_cli_msg_to_buf_rpc_ref_array_uint32_t_uint8_t_32_t(var1, (uint8_t *)((uint8_t *)msg_ipc->map_buf), off_buf); + if (ret < 0) + { + return ((msg_tag_t){.flags = (0), .msg_buf_len = (0), .map_buf_len = (0), .prot = (ret)}); + } + off_buf = ret; + } + } + } while (0); + do + { + if (1 == 1) + { + if (1 == 1 || 1 == 4) + { + int ret = rpc_cli_msg_to_buf_rpc_int_t(var2, (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(var2, (uint8_t *)((uint8_t *)msg_ipc->map_buf), off_buf); + if (ret < 0) + { + return ((msg_tag_t){.flags = (0), .msg_buf_len = (0), .map_buf_len = (0), .prot = (ret)}); + } + off_buf = 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 = ((((off_buf) / ((sizeof(void *)))) + (((off_buf) % ((sizeof(void *)))) ? 1 : 0))), .prot = (0)}), ipc_timeout_create2(0, 0)); + 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); + do + { + if (1 == 1) + { + if (2 == 2 || 2 == 4) + { + int ret = rpc_cli_buf_to_msg_rpc_ref_array_uint32_t_uint8_t_32_t(var1, (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); + do + { + if (1 == 1) + { + if (1 == 2 || 1 == 4) + { + int ret = rpc_cli_buf_to_msg_rpc_int_t(var2, (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; +} +RPC_GENERATION_CALL3(fs_t, FS_WRITE, write, + rpc_int_t, rpc_int_t, RPC_DIR_IN, RPC_TYPE_DATA, fd, + rpc_ref_array_uint32_t_uint8_t_32_t, rpc_array_uint32_t_uint8_t_32_t, RPC_DIR_IN, RPC_TYPE_DATA, buf, + rpc_int_t, rpc_int_t, RPC_DIR_IN, RPC_TYPE_DATA, len) +RPC_GENERATION_CALL1(fs_t, FS_CLOSE, close, + rpc_int_t, rpc_int_t, RPC_DIR_IN, RPC_TYPE_DATA, fd) +RPC_GENERATION_CALL3(fs_t, FS_LSEEK, lseek, + rpc_int_t, rpc_int_t, RPC_DIR_IN, RPC_TYPE_DATA, fd, + rpc_int_t, rpc_int_t, RPC_DIR_IN, RPC_TYPE_DATA, offs, + rpc_int_t, rpc_int_t, RPC_DIR_IN, RPC_TYPE_DATA, whence) int fs_open(const char *path, int flags, int mode) { obj_handler_t hd; @@ -44,3 +213,137 @@ int fs_open(const char *path, int flags, int mode) return msg_tag_get_val(tag); } +int fs_read(int fd, void *buf, size_t len) +{ + obj_handler_t hd; + int ret = ns_query("fs", &hd); + + if (ret < 0) + { + return ret; + } + + rpc_int_t rpc_fd = { + .data = fd, + }; + + int rlen = 0; + while (rlen < len) + { + int r_once_len = 0; + + r_once_len = MIN(32, len - rlen); + rpc_ref_array_uint32_t_uint8_t_32_t rpc_buf = { + .data = buf + rlen, + .len = r_once_len, + }; + rpc_int_t rpc_len = { + .data = r_once_len, + }; + msg_tag_t tag = fs_t_read_call(hd, &rpc_fd, &rpc_buf, &rpc_len); + + if (msg_tag_get_val(tag) < 0) + { + return msg_tag_get_val(tag); + } + rlen += msg_tag_get_val(tag); + if (msg_tag_get_val(tag) != r_once_len) + { + break; + } + } + + return rlen; +} + +int fs_write(int fd, void *buf, size_t len) +{ + obj_handler_t hd; + int ret = ns_query("fs", &hd); + + if (ret < 0) + { + return ret; + } + + rpc_int_t rpc_fd = { + .data = fd, + }; + int wlen = 0; + while (wlen < len) + { + int w_once_len = 0; + + w_once_len = MIN(32, len - wlen); + rpc_ref_array_uint32_t_uint8_t_32_t rpc_buf = { + .data = buf + wlen, + .len = w_once_len, + }; + rpc_int_t rpc_len = { + .data = w_once_len, + }; + msg_tag_t tag = fs_t_write_call(hd, &rpc_fd, &rpc_buf, &rpc_len); + + if (msg_tag_get_val(tag) < 0) + { + return msg_tag_get_val(tag); + } + wlen += msg_tag_get_val(tag); + if (msg_tag_get_val(tag) != w_once_len) + { + break; + } + } + + return wlen; +} +int fs_close(int fd) +{ + obj_handler_t hd; + int ret = ns_query("fs", &hd); + + if (ret < 0) + { + return ret; + } + + rpc_int_t rpc_fd = { + .data = fd, + }; + msg_tag_t tag = fs_t_close_call(hd, &rpc_fd); + + if (msg_tag_get_val(tag) < 0) + { + return msg_tag_get_val(tag); + } + + return msg_tag_get_val(tag); +} +int fs_lseek(int fd, int offs, int whence) +{ + obj_handler_t hd; + int ret = ns_query("fs", &hd); + + if (ret < 0) + { + return ret; + } + + rpc_int_t rpc_fd = { + .data = fd, + }; + rpc_int_t rpc_offs = { + .data = offs, + }; + rpc_int_t rpc_whence = { + .data = whence, + }; + msg_tag_t tag = fs_t_lseek_call(hd, &rpc_fd, &rpc_offs, &rpc_whence); + + if (msg_tag_get_val(tag) < 0) + { + return msg_tag_get_val(tag); + } + + return msg_tag_get_val(tag); +} \ No newline at end of file diff --git a/mkrtos_user/lib/sys_svr/src/fs_svr.c b/mkrtos_user/lib/sys_svr/src/fs_svr.c index f43fd74bc..1196faee7 100644 --- a/mkrtos_user/lib/sys_svr/src/fs_svr.c +++ b/mkrtos_user/lib/sys_svr/src/fs_svr.c @@ -5,6 +5,7 @@ #include "fs_svr.h" #include +/*open*/ RPC_GENERATION_OP3(fs_t, FS_OPEN, open, rpc_ref_array_uint32_t_uint8_t_32_t, rpc_array_uint32_t_uint8_t_32_t, RPC_DIR_IN, RPC_TYPE_DATA, path, rpc_int_t, rpc_int_t, RPC_DIR_IN, RPC_TYPE_DATA, flags, @@ -19,8 +20,231 @@ RPC_GENERATION_DISPATCH3(fs_t, FS_OPEN, open, rpc_ref_array_uint32_t_uint8_t_32_t, rpc_array_uint32_t_uint8_t_32_t, RPC_DIR_IN, RPC_TYPE_DATA, path, rpc_int_t, rpc_int_t, RPC_DIR_IN, RPC_TYPE_DATA, flags, rpc_int_t, rpc_int_t, RPC_DIR_IN, RPC_TYPE_DATA, mode) +/*read*/ +RPC_GENERATION_OP3(fs_t, FS_READ, read, + rpc_int_t, rpc_int_t, RPC_DIR_IN, RPC_TYPE_DATA, fd, + rpc_ref_array_uint32_t_uint8_t_32_t, rpc_array_uint32_t_uint8_t_32_t, RPC_DIR_OUT, RPC_TYPE_DATA, buf, + rpc_int_t, rpc_int_t, RPC_DIR_IN, RPC_TYPE_DATA, len) +{ + int ret = fs_svr_read(fd->data, buf->data, len->data); -RPC_DISPATCH1(fs_t, typeof(FS_OPEN), FS_OPEN, open) + if (ret >= 0) + { + buf->len = ret; + } + return ret; +} + +// RPC_GENERATION_DISPATCH3(fs_t, FS_READ, read, +// rpc_int_t, rpc_int_t, RPC_DIR_IN, RPC_TYPE_DATA, fd, +// rpc_ref_array_uint32_t_uint8_t_32_t, rpc_array_uint32_t_uint8_t_32_t, RPC_DIR_OUT, RPC_TYPE_DATA, buf, +// rpc_int_t, rpc_int_t, RPC_DIR_IN, RPC_TYPE_DATA, len) +msg_tag_t fs_t_read_dispatch(fs_t *obj, msg_tag_t tag, ipc_msg_t *ipc_msg) +{ + rpc_int_t var0; + rpc_array_uint32_t_uint8_t_32_t var1; + rpc_int_t var2; + size_t op_val; + uint8_t *value = (uint8_t *)(ipc_msg->msg_buf); + int off = 0; + rpc_var_rpc_int_t_init(&var0); + rpc_var_rpc_array_uint32_t_uint8_t_32_t_init(&var1); + rpc_var_rpc_int_t_init(&var2); + op_val = *((typeof(((uint16_t)2)) *)value); + if (op_val != ((uint16_t)2)) + { + return ((msg_tag_t){.flags = (0), .msg_buf_len = (0), .map_buf_len = (0), .prot = (-71)}); + } + off += sizeof(typeof(((uint16_t)2))); + off = rpc_align(off, __alignof(typeof(((uint16_t)2)))); + 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); + do + { + if (1 == 1) + { + if (2 == 1 || 2 == 4) + { + int ret = rpc_svr_buf_to_msg_rpc_array_uint32_t_uint8_t_32_t(&var1, (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); + do + { + if (1 == 1) + { + if (1 == 1 || 1 == 4) + { + int ret = rpc_svr_buf_to_msg_rpc_int_t(&var2, (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 = fs_t_read_op(obj, &var0, &var1, &var2); + if (ret_val < 0) + { + return ((msg_tag_t){.flags = (0), .msg_buf_len = (0), .map_buf_len = (0), .prot = (ret_val)}); + } + off = 0; + int off_map = 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); + do + { + if (1 == 1) + { + if (2 == 2 || 2 == 4) + { + int ret = rpc_svr_msg_to_buf_rpc_array_uint32_t_uint8_t_32_t(&var1, (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); + do + { + if (1 == 1) + { + if (1 == 2 || 1 == 4) + { + int ret = rpc_svr_msg_to_buf_rpc_int_t(&var2, (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); + do + { + if (1 == 2) + { + if (1 == 2 || 1 == 4) + { + int ret = rpc_svr_msg_to_buf_rpc_int_t(&var0, (uint8_t *)(ipc_msg->map_buf), off_map); + if (ret < 0) + { + return ((msg_tag_t){.flags = (0), .msg_buf_len = (0), .map_buf_len = (0), .prot = (ret)}); + } + off_map = ret; + } + } + } while (0); + do + { + if (1 == 2) + { + if (2 == 2 || 2 == 4) + { + int ret = rpc_svr_msg_to_buf_rpc_array_uint32_t_uint8_t_32_t(&var1, (uint8_t *)(ipc_msg->map_buf), off_map); + if (ret < 0) + { + return ((msg_tag_t){.flags = (0), .msg_buf_len = (0), .map_buf_len = (0), .prot = (ret)}); + } + off_map = ret; + } + } + } while (0); + do + { + if (1 == 2) + { + if (1 == 2 || 1 == 4) + { + int ret = rpc_svr_msg_to_buf_rpc_int_t(&var2, (uint8_t *)(ipc_msg->map_buf), off_map); + if (ret < 0) + { + return ((msg_tag_t){.flags = (0), .msg_buf_len = (0), .map_buf_len = (0), .prot = (ret)}); + } + off_map = ret; + } + } + } while (0); + return ((msg_tag_t){.flags = (0), .msg_buf_len = ((((off) / ((sizeof(void *)))) + (((off) % ((sizeof(void *)))) ? 1 : 0))), .map_buf_len = ((((off_map) / ((sizeof(void *)))) + (((off_map) % ((sizeof(void *)))) ? 1 : 0))), .prot = (ret_val)}); +} +/*write*/ +RPC_GENERATION_OP3(fs_t, FS_WRITE, write, + rpc_int_t, rpc_int_t, RPC_DIR_IN, RPC_TYPE_DATA, fd, + rpc_ref_array_uint32_t_uint8_t_32_t, rpc_array_uint32_t_uint8_t_32_t, RPC_DIR_IN, RPC_TYPE_DATA, buf, + rpc_int_t, rpc_int_t, RPC_DIR_IN, RPC_TYPE_DATA, len) +{ + int ret = fs_svr_write(fd->data, buf->data, len->data); + return ret; +} + +RPC_GENERATION_DISPATCH3(fs_t, FS_WRITE, write, + rpc_int_t, rpc_int_t, RPC_DIR_IN, RPC_TYPE_DATA, fd, + rpc_ref_array_uint32_t_uint8_t_32_t, rpc_array_uint32_t_uint8_t_32_t, RPC_DIR_IN, RPC_TYPE_DATA, buf, + rpc_int_t, rpc_int_t, RPC_DIR_IN, RPC_TYPE_DATA, len) + +/*close*/ +RPC_GENERATION_OP1(fs_t, FS_CLOSE, close, + rpc_int_t, rpc_int_t, RPC_DIR_IN, RPC_TYPE_DATA, fd) +{ + fs_svr_close(fd->data); + return 0; +} + +RPC_GENERATION_DISPATCH1(fs_t, FS_CLOSE, close, + rpc_int_t, rpc_int_t, RPC_DIR_IN, RPC_TYPE_DATA, fd) + +/*lseek*/ +RPC_GENERATION_OP3(fs_t, FS_LSEEK, lseek, + rpc_int_t, rpc_int_t, RPC_DIR_IN, RPC_TYPE_DATA, fd, + rpc_int_t, rpc_int_t, RPC_DIR_IN, RPC_TYPE_DATA, offs, + rpc_int_t, rpc_int_t, RPC_DIR_IN, RPC_TYPE_DATA, whence) +{ + int ret = fs_svr_lseek(fd->data, offs->data, whence->data); + return ret; +} + +RPC_GENERATION_DISPATCH3(fs_t, FS_LSEEK, lseek, + rpc_int_t, rpc_int_t, RPC_DIR_IN, RPC_TYPE_DATA, fd, + rpc_int_t, rpc_int_t, RPC_DIR_IN, RPC_TYPE_DATA, len, + rpc_int_t, rpc_int_t, RPC_DIR_IN, RPC_TYPE_DATA, whence) + +/*dispatch*/ +RPC_DISPATCH5(fs_t, typeof(FS_OPEN), FS_OPEN, open, FS_READ, read, + FS_WRITE, write, FS_CLOSE, close, FS_LSEEK, lseek) void fs_init(fs_t *fs) { diff --git a/mkrtos_user/lib/sys_svr/src/ns_cli.c b/mkrtos_user/lib/sys_svr/src/ns_cli.c index 9cd080c84..f313a98cf 100644 --- a/mkrtos_user/lib/sys_svr/src/ns_cli.c +++ b/mkrtos_user/lib/sys_svr/src/ns_cli.c @@ -198,7 +198,8 @@ int ns_query(const char *path, obj_handler_t *svr_hd) newfd = find_hd(path); if (newfd != HANDLER_INVALID) { - return newfd; + *svr_hd = newfd; + return 0; } newfd = handler_alloc(); diff --git a/mkrtos_user/lib/sys_util/inc/u_rpc.h b/mkrtos_user/lib/sys_util/inc/u_rpc.h index b5a0a1cc2..bbe152878 100644 --- a/mkrtos_user/lib/sys_util/inc/u_rpc.h +++ b/mkrtos_user/lib/sys_util/inc/u_rpc.h @@ -193,7 +193,8 @@ RPC_TYPE_DEF_ALL(int) //!< 定义所有的 RPC_ARRAY_TYPE_DEF(len_type, data_type, length); \ RPC_SVR_MSG_TO_BUF_WITHOUT_IMPL(rpc_array_##len_type##_##data_type##_##length##_t, int) \ { \ - if (rpc_align(d->len, __alignof(d->len) + sizeof(d->len)) > IPC_MSG_SIZE) \ + if (rpc_align(len, __alignof(d->len)) + d->len > \ + IPC_MSG_SIZE) \ { \ return -ETOLONG; \ } \ @@ -214,7 +215,8 @@ RPC_TYPE_DEF_ALL(int) //!< 定义所有的 } \ RPC_SVR_BUF_TO_MSG_WITHOUT_IMPL(rpc_array_##len_type##_##data_type##_##length##_t, int) \ { \ - if (rpc_align(d->len, __alignof(d->len) + sizeof(d->len)) > max) \ + if (rpc_align(len, __alignof(d->len)) + d->len > \ + max) \ { \ return -ETOLONG; \ } \ @@ -235,7 +237,8 @@ RPC_TYPE_DEF_ALL(int) //!< 定义所有的 } \ RPC_CLI_MSG_TO_BUF_WITHOUT_IMPL(rpc_array_##len_type##_##data_type##_##length##_t, int) \ { \ - if (rpc_align(d->len, __alignof(d->len) + sizeof(d->len)) > IPC_MSG_SIZE) \ + if (rpc_align(len, __alignof(d->len)) + d->len > \ + IPC_MSG_SIZE) \ { \ return -ETOLONG; \ } \ @@ -256,7 +259,8 @@ RPC_TYPE_DEF_ALL(int) //!< 定义所有的 } \ RPC_CLI_BUF_TO_MSG_WITHOUT_IMPL(rpc_array_##len_type##_##data_type##_##length##_t, int) \ { \ - if (rpc_align(d->len, __alignof(d->len) + sizeof(d->len)) > max) \ + if (rpc_align(len, __alignof(d->len)) + d->len > \ + max) \ { \ return -ETOLONG; \ } \ @@ -304,7 +308,7 @@ RPC_ARRAY_DEF(uint32_t, uint8_t, 32) RPC_REF_ARRAY_TYPE_DEF(len_type, data_type, length); \ RPC_CLI_MSG_TO_BUF_WITHOUT_IMPL(rpc_ref_array_##len_type##_##data_type##_##length##_t, int) \ { \ - if (rpc_align(d->len, __alignof(d->len) + sizeof(d->len)) >= IPC_MSG_SIZE) \ + if (rpc_align(len, __alignof(d->len)) + d->len > IPC_MSG_SIZE) \ { \ return -ETOLONG; \ } \ @@ -325,13 +329,14 @@ RPC_ARRAY_DEF(uint32_t, uint8_t, 32) } \ 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) \ + if (rpc_align(len, __alignof(d->len)) + d->len > \ + max) \ { \ return -ETOLONG; \ } \ len = rpc_align(len, __alignof(d->len)); \ d->len = *((typeof(d->len) *)(&buf[len])); \ - if (rpc_align(d->len, __alignof(d->data[0]) + d->len * sizeof(d->data[0])) >= max) \ + if (rpc_align(d->len, __alignof(d->data[0]) + d->len * sizeof(d->data[0])) > max) \ { \ return -ETOLONG; \ } \ @@ -539,7 +544,7 @@ RPC_TYPE_INIT_WITHOUT_IMPL(rpc_obj_handler_t_t) } \ } while (0) -#define RPC_DISPATCH1(struct_type, op_type, func0_op, func0_name) \ +#define RPC_DISPATCH1(struct_type, op_type, func0_op, func0_name) \ msg_tag_t rpc_##struct_type##_dispatch(struct_type *obj, msg_tag_t in_tag, ipc_msg_t *ipc_msg) \ { \ msg_tag_t tag = msg_tag_init4(0, 0, 0, -EPROTO); \ @@ -550,14 +555,13 @@ RPC_TYPE_INIT_WITHOUT_IMPL(rpc_obj_handler_t_t) { \ case func0_op: \ { \ - tag = struct_type##_##func0_name##_dispatch(obj, in_tag, ipc_msg); \ + tag = struct_type##_##func0_name##_dispatch(obj, in_tag, ipc_msg); \ } \ break; \ } \ return tag; \ } - #define RPC_DISPATCH2(struct_type, op_type, func0_op, func0_name, func1_op, func1_name) \ msg_tag_t rpc_##struct_type##_dispatch(struct_type *obj, msg_tag_t in_tag, ipc_msg_t *ipc_msg) \ { \ @@ -569,12 +573,12 @@ RPC_TYPE_INIT_WITHOUT_IMPL(rpc_obj_handler_t_t) { \ case func0_op: \ { \ - tag = struct_type##_##func0_name##_dispatch(obj, in_tag, ipc_msg); \ + tag = struct_type##_##func0_name##_dispatch(obj, in_tag, ipc_msg); \ } \ break; \ case func1_op: \ { \ - tag = struct_type##_##func1_name##_dispatch(obj, in_tag, ipc_msg); \ + tag = struct_type##_##func1_name##_dispatch(obj, in_tag, ipc_msg); \ } \ break; \ } \ @@ -592,23 +596,95 @@ RPC_TYPE_INIT_WITHOUT_IMPL(rpc_obj_handler_t_t) { \ case func0_op: \ { \ - tag = struct_type##_##func0_name##_dispatch(obj, in_tag, ipc_msg); \ + tag = struct_type##_##func0_name##_dispatch(obj, in_tag, ipc_msg); \ } \ break; \ case func1_op: \ { \ - tag = struct_type##_##func1_name##_dispatch(obj, in_tag, ipc_msg); \ + tag = struct_type##_##func1_name##_dispatch(obj, in_tag, ipc_msg); \ } \ break; \ case func2_op: \ { \ - tag = struct_type##_##func2_name##_dispatch(obj, in_tag, ipc_msg); \ + tag = struct_type##_##func2_name##_dispatch(obj, in_tag, ipc_msg); \ } \ break; \ } \ return tag; \ } +#define RPC_DISPATCH4(struct_type, op_type, func0_op, func0_name, func1_op, func1_name, func2_op, func2_name, func3_op, func3_name) \ + msg_tag_t rpc_##struct_type##_dispatch(struct_type *obj, msg_tag_t in_tag, ipc_msg_t *ipc_msg) \ + { \ + msg_tag_t tag = msg_tag_init4(0, 0, 0, -EPROTO); \ + size_t op_val; \ + \ + op_val = *((op_type *)(ipc_msg->msg_buf)); \ + switch (op_val) \ + { \ + case func0_op: \ + { \ + tag = struct_type##_##func0_name##_dispatch(obj, in_tag, ipc_msg); \ + } \ + break; \ + case func1_op: \ + { \ + tag = struct_type##_##func1_name##_dispatch(obj, in_tag, ipc_msg); \ + } \ + break; \ + case func2_op: \ + { \ + tag = struct_type##_##func2_name##_dispatch(obj, in_tag, ipc_msg); \ + } \ + break; \ + case func3_op: \ + { \ + tag = struct_type##_##func3_name##_dispatch(obj, in_tag, ipc_msg); \ + } \ + break; \ + } \ + return tag; \ + } + +#define RPC_DISPATCH5(struct_type, op_type, func0_op, func0_name, func1_op, func1_name, \ + func2_op, func2_name, func3_op, func3_name, \ + func4_op, func4_name) \ + msg_tag_t rpc_##struct_type##_dispatch(struct_type *obj, msg_tag_t in_tag, ipc_msg_t *ipc_msg) \ + { \ + msg_tag_t tag = msg_tag_init4(0, 0, 0, -EPROTO); \ + size_t op_val; \ + \ + op_val = *((op_type *)(ipc_msg->msg_buf)); \ + switch (op_val) \ + { \ + case func0_op: \ + { \ + tag = struct_type##_##func0_name##_dispatch(obj, in_tag, ipc_msg); \ + } \ + break; \ + case func1_op: \ + { \ + tag = struct_type##_##func1_name##_dispatch(obj, in_tag, ipc_msg); \ + } \ + break; \ + case func2_op: \ + { \ + tag = struct_type##_##func2_name##_dispatch(obj, in_tag, ipc_msg); \ + } \ + break; \ + case func3_op: \ + { \ + tag = struct_type##_##func3_name##_dispatch(obj, in_tag, ipc_msg); \ + } \ + break; \ + case func4_op: \ + { \ + tag = struct_type##_##func4_name##_dispatch(obj, in_tag, ipc_msg); \ + } \ + break; \ + } \ + return tag; \ + } #include "u_rpc_1.h" #include "u_rpc_2.h" diff --git a/mkrtos_user/lib/sys_util/inc/u_rpc_1.h b/mkrtos_user/lib/sys_util/inc/u_rpc_1.h index 614b9e90a..bd3e21eea 100644 --- a/mkrtos_user/lib/sys_util/inc/u_rpc_1.h +++ b/mkrtos_user/lib/sys_util/inc/u_rpc_1.h @@ -31,7 +31,7 @@ RPC_CLI_MSG_TO_BUF_IN(rpc_type0, cli_type0, var0, dir0, (uint8_t *)msg_ipc->msg_buf, off); \ PRC_CLI_FILL_MAP_BUF(rpc_type0, cli_type0, var0, dir0, (uint8_t *)msg_ipc->map_buf, off_map); \ /*msg_tag_t tag = dispatch_test(msg_tag_init4(0, ROUND_UP(off, WORD_BYTES), ROUND_UP(off_map, WORD_BYTES), 0), msg_ipc);*/ \ - msg_tag_t tag = ipc_call(hd, msg_tag_init4(0, ROUND_UP(off, WORD_BYTES), ROUND_UP(off_buf, WORD_BYTES), 0), \ + msg_tag_t tag = ipc_call(hd, msg_tag_init4(0, ROUND_UP(off, WORD_BYTES), ROUND_UP(off_map, WORD_BYTES), 0), \ ipc_timeout_create2(0, 0)); \ \ if (msg_tag_get_val(tag) < 0) \ diff --git a/mkrtos_user/lib/sys_util/src/u_app_loader.c b/mkrtos_user/lib/sys_util/src/u_app_loader.c index 142cac6da..65c887cd2 100644 --- a/mkrtos_user/lib/sys_util/src/u_app_loader.c +++ b/mkrtos_user/lib/sys_util/src/u_app_loader.c @@ -56,11 +56,12 @@ int app_load(const char *name, uenv_t *cur_env) app_info_t *app = app_info_get((void *)addr); - if (app == NULL) { + if (app == NULL) + { printf("app format is error.\n"); return -1; } - printf("app addr is 0x%x\n", app); + printf("%s addr is 0x%x\n", name, app); umword_t ram_base; obj_handler_t hd_task = handler_alloc(); obj_handler_t hd_thread = handler_alloc(); diff --git a/mkrtos_user/server/fs/fatfs/fs_rpc.c b/mkrtos_user/server/fs/fatfs/fs_rpc.c index a10cff1dc..675c9b764 100644 --- a/mkrtos_user/server/fs/fatfs/fs_rpc.c +++ b/mkrtos_user/server/fs/fatfs/fs_rpc.c @@ -15,10 +15,10 @@ typedef struct file_desc FIL fp; } file_desc_t; -#define FILE_DESC_NR 16 +#define FILE_DESC_NR 3 static file_desc_t files[FILE_DESC_NR]; -file_desc_t *alloc_file(int *fd) +static file_desc_t *alloc_file(int *fd) { for (int i = 0; i < FILE_DESC_NR; i++) { @@ -30,10 +30,19 @@ file_desc_t *alloc_file(int *fd) } return NULL; } -void free_file(int fd) +static void free_file(int fd) { files[fd].fp.obj.fs = NULL; } +static file_desc_t *file_get(int fd) +{ + if (fd < 0 || fd >= FILE_DESC_NR) + { + return NULL; + } + return files + fd; +} + int fs_svr_open(const char *path, int flags, int mode) { printf("open %s.\n", path); @@ -55,6 +64,90 @@ int fs_svr_open(const char *path, int flags, int mode) return -ret; } +int fs_svr_read(int fd, void *buf, size_t len) +{ + UINT br; + file_desc_t *file = file_get(fd); + + if (!file) + { + return -ENOENT; + } + FRESULT ret = f_read(&file->fp, buf, len, &br); + + if (ret != FR_OK) + { + return -ret; + } + return br; +} +int fs_svr_write(int fd, void *buf, size_t len) +{ + UINT bw; + file_desc_t *file = file_get(fd); + + if (!file) + { + return -ENOENT; + } + FRESULT ret = f_write(&file->fp, buf, len, &bw); + + if (ret != FR_OK) + { + return -ret; + } + return bw; +} +void fs_svr_close(int fd) +{ + file_desc_t *file = file_get(fd); + + if (!file) + { + return; + } + f_close(&file->fp); +} +int fs_svr_lseek(int fd, int offs, int whence) +{ + UINT bw; + file_desc_t *file = file_get(fd); + int new_offs = 0; + + if (!file) + { + return -ENOENT; + } + switch (whence) + { + case SEEK_SET: + new_offs = offs; + break; + case SEEK_END: + { + new_offs = f_size(&file->fp) + offs; + } + break; + case SEEK_CUR: + { + new_offs = offs + f_tell(&file->fp); + } + break; + default: + return -EINVAL; + } + if (new_offs > f_size(&file->fp)) + { + new_offs = f_size(&file->fp); + } + if (new_offs < 0) + { + new_offs = 0; + } + FRESULT ret = f_lseek(&file->fp, new_offs); + + return -ret; +} void fs_svr_loop(void) { rpc_loop(fs.ipc, &fs.svr); diff --git a/mkrtos_user/server/fs/fatfs/fs_rpc.h b/mkrtos_user/server/fs/fatfs/fs_rpc.h index 2c73ec5a2..97a025e99 100644 --- a/mkrtos_user/server/fs/fatfs/fs_rpc.h +++ b/mkrtos_user/server/fs/fatfs/fs_rpc.h @@ -2,3 +2,4 @@ void fs_svr_init(obj_handler_t ipc); void fs_svr_loop(void); + diff --git a/mkrtos_user/server/fs/fatfs/heap_stack.c b/mkrtos_user/server/fs/fatfs/heap_stack.c index 2f21cffba..e384461b1 100644 --- a/mkrtos_user/server/fs/fatfs/heap_stack.c +++ b/mkrtos_user/server/fs/fatfs/heap_stack.c @@ -1,6 +1,6 @@ #define HEAP_SIZE 4 -#define STACK_SIZE 1024 * 2 +#define STACK_SIZE 1024 * 3 #if defined(__CC_ARM) #define HEAP_ATTR SECTION("HEAP") __attribute__((zero_init)) diff --git a/mkrtos_user/server/init/src/namespace.c b/mkrtos_user/server/init/src/namespace.c index 783029da0..408298c51 100644 --- a/mkrtos_user/server/init/src/namespace.c +++ b/mkrtos_user/server/init/src/namespace.c @@ -97,49 +97,3 @@ void namespace_loop(void) { rpc_loop(ns.ipc_hd, &ns.svr); } - -// msg_tag_t ns_dispatch(ipc_msg_t *msg) -// { -// msg_tag_t tag = msg_tag_init4(0, 0, 0, -ENOSYS); -// assert(msg); - -// switch (msg->msg_buf[0]) -// { -// case OP_REGISTER: //!< 服务注册 -// { -// size_t len = msg->msg_buf[1]; -// if (len > NAMESPACE_PATH_LEN) -// { -// tag = msg_tag_init4(0, 0, 0, -EINVAL); -// break; -// } -// ((char *)&(msg->msg_buf[2]))[len] = 0; -// int ret = ns_register((char *)(&msg->msg_buf[2]), pre_alloc_hd); -// ns_pre_alloc_map_fd(msg); -// tag = msg_tag_init4(0, 0, 0, ret); -// } -// break; -// case OP_QUERY: //!< 服务申请 -// { -// obj_handler_t hd; -// int ret = ns_query(((char *)&(msg->msg_buf[2])), &hd); -// if (ret < 0) -// { -// tag = msg_tag_init4(0, 0, 0, ret); -// } -// else -// { -// size_t len = msg->msg_buf[1]; - -// msg->map_buf[0] = vpage_create_raw3(0, 0, hd).raw; -// printf("query..\n"); -// tag = msg_tag_init4(0, 0, 1, ret); -// } -// } -// break; -// default: -// tag = msg_tag_init4(0, 0, 0, -ENOSYS); -// break; -// } -// return tag; -// } diff --git a/mkrtos_user/server/shell/src/main.c b/mkrtos_user/server/shell/src/main.c index c8c02e340..d83050977 100644 --- a/mkrtos_user/server/shell/src/main.c +++ b/mkrtos_user/server/shell/src/main.c @@ -64,7 +64,17 @@ void ns_test(void) #include "fs_cli.h" void fs_test(void) { - fs_open("/test", 0, 0); + char tmp[4] = "123"; + int fd = fs_open("/test", 0, 0x1 | 0x2 | 0x8); + assert(fd >= 0); + int wlen = fs_write(fd, tmp, 4); + assert(wlen == 4); + int ret = fs_lseek(fd, 0, SEEK_SET); + assert(ret >= 0); + int rlen = fs_read(fd, tmp, 4); + assert(rlen == 4); + assert(strcmp(tmp, "123") == 0); + fs_close(fd); } int main(int argc, char *args[]) {