This commit is contained in:
刘可亮
2024-09-30 17:06:01 +08:00
parent 2ce4d6bb89
commit 0ef85b55da
9363 changed files with 843557 additions and 428518 deletions

View File

@@ -30,19 +30,42 @@ struct custom_ctx
char fpath[DFS_PATH_MAX];
};
static const char *_get_path_lastname(const char *path)
{
char *ptr;
if ((ptr = (char *)strrchr(path, '/')) == NULL)
return path;
/* skip the '/' then return */
return ++ptr;
}
static enum rym_code _rym_recv_begin(
struct rym_ctx *ctx,
rt_uint8_t *buf,
rt_size_t len)
{
struct custom_ctx *cctx = (struct custom_ctx *)ctx;
char insert_0 = '\0';
rt_err_t err;
char *ret;
cctx->fpath[0] = '/';
rt_strncpy(&(cctx->fpath[1]), (const char *)buf, len - 1);
ret = strchr(cctx->fpath, insert_0);
if (ret)
{
*ret = '/';
}
else
{
rt_kprintf("No end character\n");
return RYM_ERR_ACK;
}
rt_strncpy(ret + 1, (const char *)buf, len - 1);
cctx->fd = open(cctx->fpath, O_CREAT | O_WRONLY | O_TRUNC, 0);
if (cctx->fd < 0)
{
rt_err_t err = rt_get_errno();
err = rt_get_errno();
rt_kprintf("error creating file: %d\n", err);
return RYM_CODE_CAN;
}
@@ -97,6 +120,7 @@ static enum rym_code _rym_send_begin(
struct custom_ctx *cctx = (struct custom_ctx *)ctx;
struct stat file_buf;
char insert_0 = '\0';
const char *fdst;
rt_err_t err;
cctx->fd = open(cctx->fpath, O_RDONLY);
@@ -113,7 +137,18 @@ static enum rym_code _rym_send_begin(
rt_kprintf("error open file.\n");
return RYM_ERR_FILE;
}
rt_sprintf((char *)buf, "%s%c%d", (char *) & (cctx->fpath[1]), insert_0, file_buf.st_size);
fdst = _get_path_lastname(cctx->fpath);
if (fdst != cctx->fpath)
{
fdst = dfs_normalize_path(RT_NULL, fdst);
if (fdst == RT_NULL)
{
return RYM_ERR_FILE;
}
}
rt_sprintf((char *)buf, "%s%c%d", fdst, insert_0, file_buf.st_size);
return RYM_CODE_SOH;
}
@@ -141,6 +176,10 @@ static enum rym_code _rym_send_data(
ctx->stage = RYM_STAGE_FINISHING;
}
if (read_size > 128)
{
return RYM_CODE_STX;
}
return RYM_CODE_SOH;
}
@@ -149,22 +188,27 @@ static enum rym_code _rym_send_end(
rt_uint8_t *buf,
rt_size_t len)
{
struct custom_ctx *cctx = (struct custom_ctx *)ctx;
rt_memset(buf, 0, len);
close(cctx->fd);
cctx->fd = -1;
return RYM_CODE_SOH;
}
static rt_err_t rym_download_file(rt_device_t idev)
static rt_err_t rym_download_file(rt_device_t idev, const char *file_path)
{
rt_err_t res;
struct custom_ctx *ctx = rt_calloc(1, sizeof(*ctx));
rt_err_t res;
if (!ctx)
{
rt_kprintf("rt_malloc failed\n");
return RT_ENOMEM;
return -RT_ENOMEM;
}
ctx->fd = -1;
rt_strncpy(ctx->fpath, file_path, DFS_PATH_MAX);
RT_ASSERT(idev);
res = rym_recv_on_device(&ctx->parent, idev, RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
_rym_recv_begin, _rym_recv_data, _rym_recv_end, 1000);
@@ -175,13 +219,13 @@ static rt_err_t rym_download_file(rt_device_t idev)
static rt_err_t rym_upload_file(rt_device_t idev, const char *file_path)
{
struct custom_ctx *ctx = rt_calloc(1, sizeof(*ctx));
rt_err_t res = 0;
struct custom_ctx *ctx = rt_calloc(1, sizeof(*ctx));
if (!ctx)
{
rt_kprintf("rt_malloc failed\n");
return RT_ENOMEM;
return -RT_ENOMEM;
}
ctx->fd = -1;
rt_strncpy(ctx->fpath, file_path, DFS_PATH_MAX);
@@ -199,11 +243,18 @@ static rt_err_t rym_upload_file(rt_device_t idev, const char *file_path)
static rt_err_t ry(uint8_t argc, char **argv)
{
rt_err_t res;
/* temporarily support 1 file*/
const char *file_path;
rt_device_t dev;
rt_err_t res;
if (argc > 1)
dev = rt_device_find(argv[1]);
if (argc < 2)
{
rt_kprintf("invalid file path.\n");
return -RT_ERROR;
}
if (argc > 2)
dev = rt_device_find(argv[2]);
else
dev = rt_console_get_device();
if (!dev)
@@ -211,18 +262,19 @@ static rt_err_t ry(uint8_t argc, char **argv)
rt_kprintf("could not find device.\n");
return -RT_ERROR;
}
res = rym_download_file(dev);
file_path = argv[1];
res = rym_download_file(dev, file_path);
return res;
}
MSH_CMD_EXPORT(ry, YMODEM Receive e.g: ry [uart0] default by console.);
MSH_CMD_EXPORT(ry, YMODEM Receive e.g: ry file_path [uart0] default by console.);
static rt_err_t sy(uint8_t argc, char **argv)
{
rt_err_t res;
/* temporarily support 1 file*/
const char *file_path;
rt_device_t dev;
rt_err_t res;
if (argc < 2)
{