1 /*
2 * Copyright (c) 2006-2021, RT-Thread Development Team
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 *
6 * Change Logs:
7 * Date Author Notes
8 * 2017-08-17 armink first version.
9 */
10
11
12 #include <rtthread.h>
13 #include <unistd.h>
14 #include <sys/stat.h>
15 #include <sys/statfs.h>
16 #include <lwip/apps/tftp_server.h>
17
18 static struct tftp_context ctx;
19
tftp_open(const char * fname,const char * mode,u8_t write)20 static void* tftp_open(const char* fname, const char* mode, u8_t write)
21 {
22 int fd = -1;
23
24 if (!rt_strcmp(mode, "octet"))
25 {
26 if (write)
27 {
28 fd = open(fname, O_WRONLY | O_CREAT, 0);
29 }
30 else
31 {
32 fd = open(fname, O_RDONLY, 0);
33 }
34 }
35 else
36 {
37 rt_kprintf("tftp: No support this mode(%s).", mode);
38 }
39
40 return (void *) fd;
41 }
42
tftp_write(void * handle,struct pbuf * p)43 static int tftp_write(void* handle, struct pbuf* p)
44 {
45 int fd = (int) handle;
46
47 return write(fd, p->payload, p->len);
48 }
49
50 #if defined(RT_USING_FINSH)
51 #include <finsh.h>
52
tftp_server(uint8_t argc,char ** argv)53 static void tftp_server(uint8_t argc, char **argv)
54 {
55 ctx.open = tftp_open;
56 ctx.close = (void (*)(void *)) close;
57 ctx.read = (int (*)(void *, void *, int)) read;
58 ctx.write = tftp_write;
59
60 if (tftp_init(&ctx) == ERR_OK)
61 {
62 rt_kprintf("TFTP server start successfully.\n");
63 }
64 else
65 {
66 rt_kprintf("TFTP server start failed.\n");
67 }
68 }
69 FINSH_FUNCTION_EXPORT(tftp_server, start tftp server.);
70
71 MSH_CMD_EXPORT(tftp_server, start tftp server.);
72
73 #endif /* defined(RT_USING_FINSH) */
74