From 0ee4784d0cf92e399727efb92a1d8469e33d43b0 Mon Sep 17 00:00:00 2001 From: Simon Goldschmidt Date: Wed, 17 Oct 2018 21:37:46 +0200 Subject: [PATCH] tftp client: pass 'mode' as an enum, not as a string constant --- src/apps/tftp/tftp.c | 27 +++++++++++++++++++++++---- src/include/lwip/apps/tftp_client.h | 10 ++++++++-- 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/src/apps/tftp/tftp.c b/src/apps/tftp/tftp.c index 3c175a6a..dc41085c 100644 --- a/src/apps/tftp/tftp.c +++ b/src/apps/tftp/tftp.c @@ -503,26 +503,45 @@ void tftp_cleanup(void) memset(&tftp_state, 0, sizeof(tftp_state)); } +const char * +mode_to_string(enum tftp_transfer_mode mode) +{ + if (mode == TFTP_MODE_OCTET) { + return "octet"; + } + if (mode == TFTP_MODE_NETASCII) { + return "netascii"; + } + if (mode == TFTP_MODE_BINARY) { + return "binary"; + } + return NULL; +} + err_t -tftp_get(void* handle, const ip_addr_t *addr, u16_t port, const char* fname, const char* mode) +tftp_get(void* handle, const ip_addr_t *addr, u16_t port, const char* fname, enum tftp_transfer_mode mode) { LWIP_ERROR("TFTP client is not enabled (tftp_init)", (tftp_state.tftp_mode & LWIP_TFTP_MODE_CLIENT) != 0, return ERR_VAL); + LWIP_ERROR("tftp_get: invalid file name", fname != NULL, return ERR_VAL); + LWIP_ERROR("tftp_get: invalid mode", mode <= TFTP_MODE_BINARY, return ERR_VAL); tftp_state.handle = handle; tftp_state.blknum = 1; tftp_state.mode_write = 1; /* We want to receive data */ - return send_request(addr, port, TFTP_RRQ, fname, mode); + return send_request(addr, port, TFTP_RRQ, fname, mode_to_string(mode)); } err_t -tftp_put(void* handle, const ip_addr_t *addr, u16_t port, const char* fname, const char* mode) +tftp_put(void* handle, const ip_addr_t *addr, u16_t port, const char* fname, enum tftp_transfer_mode mode) { LWIP_ERROR("TFTP client is not enabled (tftp_init)", (tftp_state.tftp_mode & LWIP_TFTP_MODE_CLIENT) != 0, return ERR_VAL); + LWIP_ERROR("tftp_put: invalid file name", fname != NULL, return ERR_VAL); + LWIP_ERROR("tftp_put: invalid mode", mode <= TFTP_MODE_BINARY, return ERR_VAL); tftp_state.handle = handle; tftp_state.blknum = 1; tftp_state.mode_write = 0; /* We want to send data */ - return send_request(addr, port, TFTP_WRQ, fname, mode); + return send_request(addr, port, TFTP_WRQ, fname, mode_to_string(mode)); } #endif /* LWIP_UDP */ diff --git a/src/include/lwip/apps/tftp_client.h b/src/include/lwip/apps/tftp_client.h index c65364ee..24dbda6a 100644 --- a/src/include/lwip/apps/tftp_client.h +++ b/src/include/lwip/apps/tftp_client.h @@ -37,8 +37,14 @@ #include "lwip/apps/tftp_common.h" +enum tftp_transfer_mode { + TFTP_MODE_OCTET, + TFTP_MODE_NETASCII, + TFTP_MODE_BINARY /* used in old versions only */ +}; + err_t tftp_init_client(const struct tftp_context* ctx); -err_t tftp_get(void* handle, const ip_addr_t *addr, u16_t port, const char* fname, const char* mode); -err_t tftp_put(void* handle, const ip_addr_t *addr, u16_t port, const char* fname, const char* mode); +err_t tftp_get(void* handle, const ip_addr_t *addr, u16_t port, const char* fname, enum tftp_transfer_mode mode); +err_t tftp_put(void* handle, const ip_addr_t *addr, u16_t port, const char* fname, enum tftp_transfer_mode mode); #endif /* LWIP_HDR_APPS_TFTP_CLIENT_H */