diff --git a/project/Core/Inc/tftp.h b/project/Core/Inc/tftp.h index e33e855..5a99147 100644 --- a/project/Core/Inc/tftp.h +++ b/project/Core/Inc/tftp.h @@ -30,6 +30,7 @@ void tftp_server_init(void); void tftp_server_deinit(void); void tftp_custom_fseek(tftp_custom_file_t* handle, size_t offset, int whence); size_t tftp_custom_fread(void* buf, size_t bytes, tftp_custom_file_t* handle); +size_t tftp_custom_fwrite(void* buf, size_t bytes, tftp_custom_file_t* handle); #ifdef __cplusplus } diff --git a/project/Core/Src/tftp.c b/project/Core/Src/tftp.c index 6a4b937..1a46967 100644 --- a/project/Core/Src/tftp.c +++ b/project/Core/Src/tftp.c @@ -82,6 +82,27 @@ size_t tftp_custom_fread(void* buf, size_t bytes, tftp_custom_file_t* handle) { return bytes; } +/** + * @brief tftp custom file functions to write the data + * auto rolling over the offset + * + * @param buf The buffer to write the data to + * @param bytes The number of bytes to write + * @param handle The handle to the file to write to + * @return The number of bytes written + */ +size_t tftp_custom_fwrite(void* buf, size_t bytes, tftp_custom_file_t* handle) { + if (handle->ofset + bytes > handle->len) { + bytes = handle->len - handle->ofset; + } + memcpy(handle->data + handle->ofset, buf, bytes); + handle->ofset += bytes; + if (handle->ofset > handle->len) { + bytes = 0; + } + return bytes; +} + /** * @brief tftp helper functions */