tftp_port.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. #include <rtthread.h>
  11. #include <dfs_posix.h>
  12. #include <lwip/apps/tftp_server.h>
  13. static struct tftp_context ctx;
  14. static void* tftp_open(const char* fname, const char* mode, u8_t write)
  15. {
  16. int fd = -1;
  17. if (!rt_strcmp(mode, "octet"))
  18. {
  19. if (write)
  20. {
  21. fd = open(fname, O_WRONLY | O_CREAT, 0);
  22. }
  23. else
  24. {
  25. fd = open(fname, O_RDONLY, 0);
  26. }
  27. }
  28. else
  29. {
  30. rt_kprintf("tftp: No support this mode(%s).", mode);
  31. }
  32. return (void *) fd;
  33. }
  34. static int tftp_write(void* handle, struct pbuf* p)
  35. {
  36. int fd = (int) handle;
  37. return write(fd, p->payload, p->len);
  38. }
  39. #if defined(RT_USING_FINSH)
  40. #include <finsh.h>
  41. static void tftp_server(uint8_t argc, char **argv)
  42. {
  43. ctx.open = tftp_open;
  44. ctx.close = (void (*)(void *)) close;
  45. ctx.read = (int (*)(void *, void *, int)) read;
  46. ctx.write = tftp_write;
  47. if (tftp_init(&ctx) == ERR_OK)
  48. {
  49. rt_kprintf("TFTP server start successfully.\n");
  50. }
  51. else
  52. {
  53. rt_kprintf("TFTP server start failed.\n");
  54. }
  55. }
  56. FINSH_FUNCTION_EXPORT(tftp_server, start tftp server.);
  57. #if defined(FINSH_USING_MSH)
  58. MSH_CMD_EXPORT(tftp_server, start tftp server.);
  59. #endif /* defined(FINSH_USING_MSH) */
  60. #endif /* defined(RT_USING_FINSH) */