1 /*
2 * File : tftp_port.c
3 * This file is part of RT-Thread RTOS
4 * COPYRIGHT (C) 2006 - 2017, RT-Thread Development Team
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 *
20 * Change Logs:
21 * Date Author Notes
22 * 2017-08-17 armink first version.
23 */
24
25
26 #include <rtthread.h>
27 #include <dfs_posix.h>
28 #include <lwip/apps/tftp_server.h>
29
30 static struct tftp_context ctx;
31
tftp_open(const char * fname,const char * mode,u8_t write)32 static void* tftp_open(const char* fname, const char* mode, u8_t write)
33 {
34 int fd = -1;
35
36 if (!rt_strcmp(mode, "octet"))
37 {
38 if (write)
39 {
40 fd = open(fname, O_WRONLY | O_CREAT, 0);
41 }
42 else
43 {
44 fd = open(fname, O_RDONLY, 0);
45 }
46 }
47 else
48 {
49 rt_kprintf("tftp: No support this mode(%s).", mode);
50 }
51
52 return (void *) fd;
53 }
54
tftp_write(void * handle,struct pbuf * p)55 static int tftp_write(void* handle, struct pbuf* p)
56 {
57 int fd = (int) handle;
58
59 return write(fd, p->payload, p->len);
60 }
61
62 #if defined(RT_USING_FINSH)
63 #include <finsh.h>
64
tftp_server(uint8_t argc,char ** argv)65 static void tftp_server(uint8_t argc, char **argv)
66 {
67 ctx.open = tftp_open;
68 ctx.close = (void (*)(void *)) close;
69 ctx.read = (int (*)(void *, void *, int)) read;
70 ctx.write = tftp_write;
71
72 if (tftp_init(&ctx) == ERR_OK)
73 {
74 rt_kprintf("TFTP server start successfully.\n");
75 }
76 else
77 {
78 rt_kprintf("TFTP server start failed.\n");
79 }
80 }
81 FINSH_FUNCTION_EXPORT(tftp_server, start tftp server.);
82
83 #if defined(FINSH_USING_MSH)
84 MSH_CMD_EXPORT(tftp_server, start tftp server.);
85 #endif /* defined(FINSH_USING_MSH) */
86
87 #endif /* defined(RT_USING_FINSH) */
88