ry_sy.c
5.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*
* Copyright (c) 2019 Fuzhou Rockchip Electronics Co., Ltd
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2019-12-09 Steven Liu the first version
* 2021-04-14 Meco Man Check the file path's legitimacy of 'sy' command
*/
#include <rtthread.h>
#include <ymodem.h>
#include <dfs_file.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/statfs.h>
#include <stdlib.h>
#include <string.h>
#ifndef DFS_USING_POSIX
#error "Please enable DFS_USING_POSIX"
#endif
struct custom_ctx
{
struct rym_ctx parent;
int fd;
int flen;
char fpath[DFS_PATH_MAX];
};
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;
cctx->fpath[0] = '/';
rt_strncpy(&(cctx->fpath[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();
rt_kprintf("error creating file: %d\n", err);
return RYM_CODE_CAN;
}
cctx->flen = atoi(1 + (const char *)buf + rt_strnlen((const char *)buf, len - 1));
if (cctx->flen == 0)
cctx->flen = -1;
return RYM_CODE_ACK;
}
static enum rym_code _rym_recv_data(
struct rym_ctx *ctx,
rt_uint8_t *buf,
rt_size_t len)
{
struct custom_ctx *cctx = (struct custom_ctx *)ctx;
RT_ASSERT(cctx->fd >= 0);
if (cctx->flen == -1)
{
write(cctx->fd, buf, len);
}
else
{
int wlen = len > cctx->flen ? cctx->flen : len;
write(cctx->fd, buf, wlen);
cctx->flen -= wlen;
}
return RYM_CODE_ACK;
}
static enum rym_code _rym_recv_end(
struct rym_ctx *ctx,
rt_uint8_t *buf,
rt_size_t len)
{
struct custom_ctx *cctx = (struct custom_ctx *)ctx;
RT_ASSERT(cctx->fd >= 0);
close(cctx->fd);
cctx->fd = -1;
return RYM_CODE_ACK;
}
static enum rym_code _rym_send_begin(
struct rym_ctx *ctx,
rt_uint8_t *buf,
rt_size_t len)
{
struct custom_ctx *cctx = (struct custom_ctx *)ctx;
struct stat file_buf;
char insert_0 = '\0';
rt_err_t err;
cctx->fd = open(cctx->fpath, O_RDONLY);
if (cctx->fd < 0)
{
err = rt_get_errno();
rt_kprintf("error open file: %d\n", err);
return RYM_ERR_FILE;
}
rt_memset(buf, 0, len);
err = stat(cctx->fpath, &file_buf);
if (err != RT_EOK)
{
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);
return RYM_CODE_SOH;
}
static enum rym_code _rym_send_data(
struct rym_ctx *ctx,
rt_uint8_t *buf,
rt_size_t len)
{
struct custom_ctx *cctx = (struct custom_ctx *)ctx;
rt_size_t read_size;
int retry_read;
read_size = 0;
for (retry_read = 0; retry_read < 10; retry_read++)
{
read_size += read(cctx->fd, buf + read_size, len - read_size);
if (read_size == len)
break;
}
if (read_size < len)
{
rt_memset(buf + read_size, 0x1A, len - read_size);
ctx->stage = RYM_STAGE_FINISHING;
}
return RYM_CODE_SOH;
}
static enum rym_code _rym_send_end(
struct rym_ctx *ctx,
rt_uint8_t *buf,
rt_size_t len)
{
rt_memset(buf, 0, len);
return RYM_CODE_SOH;
}
static rt_err_t rym_download_file(rt_device_t idev)
{
rt_err_t res;
struct custom_ctx *ctx = rt_calloc(1, sizeof(*ctx));
if (!ctx)
{
rt_kprintf("rt_malloc failed\n");
return RT_ENOMEM;
}
ctx->fd = -1;
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);
rt_free(ctx);
return res;
}
static rt_err_t rym_upload_file(rt_device_t idev, const char *file_path)
{
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;
}
ctx->fd = -1;
rt_strncpy(ctx->fpath, file_path, DFS_PATH_MAX);
RT_ASSERT(idev);
res = rym_send_on_device(&ctx->parent, idev,
RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
_rym_send_begin, _rym_send_data, _rym_send_end, 1000);
rt_free(ctx);
return res;
}
#ifdef RT_USING_FINSH
#include <finsh.h>
static rt_err_t ry(uint8_t argc, char **argv)
{
rt_err_t res;
rt_device_t dev;
if (argc > 1)
dev = rt_device_find(argv[1]);
else
dev = rt_console_get_device();
if (!dev)
{
rt_kprintf("could not find device.\n");
return -RT_ERROR;
}
res = rym_download_file(dev);
return res;
}
MSH_CMD_EXPORT(ry, YMODEM Receive e.g: ry [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;
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)
{
rt_kprintf("could not find device.\n");
return -RT_ERROR;
}
file_path = argv[1];
res = rym_upload_file(dev, file_path);
return res;
}
MSH_CMD_EXPORT(sy, YMODEM Send e.g: sy file_path [uart0] default by console.);
#endif /* RT_USING_FINSH */