tftp_port.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. #include <rtthread.h>
  25. #include <dfs_posix.h>
  26. #include <lwip/apps/tftp_server.h>
  27. #if defined(RT_USING_LWIP) && (RT_LWIP_TCPTHREAD_STACKSIZE < 1408)
  28. #error The lwIP tcpip thread stack size(RT_LWIP_TCPTHREAD_STACKSIZE) must more than 1408
  29. #endif
  30. static struct tftp_context ctx;
  31. static void* tftp_open(const char* fname, const char* mode, u8_t write)
  32. {
  33. int fd = -1;
  34. if (!rt_strcmp(mode, "octet"))
  35. {
  36. if (write)
  37. {
  38. fd = open(fname, O_WRONLY | O_CREAT, 0);
  39. }
  40. else
  41. {
  42. fd = open(fname, O_RDONLY, 0);
  43. }
  44. }
  45. else
  46. {
  47. rt_kprintf("tftp: No support this mode(%s).", mode);
  48. }
  49. return (void *) fd;
  50. }
  51. static int tftp_write(void* handle, struct pbuf* p)
  52. {
  53. int fd = (int) handle;
  54. return write(fd, p->payload, p->len);
  55. }
  56. #if defined(RT_USING_FINSH)
  57. #include <finsh.h>
  58. static void tftp_server(uint8_t argc, char **argv)
  59. {
  60. ctx.open = tftp_open;
  61. ctx.close = (void (*)(void *)) close;
  62. ctx.read = (int (*)(void *, void *, int)) read;
  63. ctx.write = tftp_write;
  64. if (tftp_init(&ctx) == ERR_OK)
  65. {
  66. rt_kprintf("TFTP server start successfully.\n");
  67. }
  68. else
  69. {
  70. rt_kprintf("TFTP server start failed.\n");
  71. }
  72. }
  73. FINSH_FUNCTION_EXPORT(tftp_server, start tftp server.);
  74. #if defined(FINSH_USING_MSH)
  75. MSH_CMD_EXPORT(tftp_server, start tftp server.);
  76. #endif /* defined(FINSH_USING_MSH) */
  77. #endif /* defined(RT_USING_FINSH) */