try to fix a bug Lorenz told me about
in fwrite
This commit is contained in:
2023-12-05 22:29:15 +01:00
parent 5fd8bb3044
commit a8537ba6d4
4 changed files with 24 additions and 6 deletions

View File

@@ -100,12 +100,12 @@ size_t tftp_custom_fread(void* buf, size_t bytes, tftp_custom_file_t* handle) {
* @return The number of bytes written
*/
size_t tftp_custom_fwrite(const void* buf, size_t bytes, tftp_custom_file_t* handle) {
if (handle->offset + bytes > handle->len) {
bytes = handle->len - handle->offset;
if (handle->offset + bytes >= handle->len) {
bytes = handle->len - handle->offset - 1;
}
memcpy(handle->data + handle->offset, buf, bytes);
handle->offset += bytes;
if (handle->offset > handle->len) {
if (handle->offset >= handle->len) {
bytes = 0;
}
return bytes;
@@ -171,7 +171,7 @@ void tftp_close(void* handle) {
if (handle == &virt_file[VIRT_TEXT_TXT]) {
lcd_clear_images();
lcd_clear_text();
lcd_display_text((const char*)virt_file[VIRT_TEXT_TXT].data, 0, 0, LCD_COLOR_WHITE, LCD_TRANSPARENT, LCD_FONT16);
lcd_display_text((char*)virt_file[VIRT_TEXT_TXT].data, 0, 0, LCD_COLOR_WHITE, LCD_TRANSPARENT, LCD_FONT16);
}
if (handle == &virt_file[VIRT_INDEX_TXT] || handle == &virt_file[VIRT_IMAGE_BMP]

View File

@@ -12,7 +12,7 @@ int tftp_init(struct tftp_context* context) {
return 0;
}
void lcd_display_text(uint8_t* text, uint16_t x_pos, uint16_t y_pos, uint32_t color, uint32_t bg_color, sFONT* font) {
void lcd_display_text(const char* text, uint16_t x_pos, uint16_t y_pos, uint32_t color, uint32_t bg_color, sFONT* font) {
UNUSED(color);
UNUSED(bg_color);
UNUSED(font);

View File

@@ -93,7 +93,7 @@ typedef err_t (*tcp_accept_fn)(void *arg, struct tcp_pcb *newpcb, err_t err);
uint32_t logger_get_timestamp(void);
void lcd_display_text(uint8_t* text, uint16_t x_pos, uint16_t y_pos, uint32_t color, uint32_t bg_color, sFONT *font);
void lcd_display_text(const char* text, uint16_t x_pos, uint16_t y_pos, uint32_t color, uint32_t bg_color, sFONT *font);
void lcd_draw_img_from_fs(char* filename, uint32_t x_pos, uint32_t y_pos);
void lcd_draw_gif_from_fs(char* filename, uint32_t x_pos, uint32_t y_pos);
void lcd_draw_bmp_img(uint8_t* bmp_buff, uint32_t x_pos, uint32_t y_pos);

View File

@@ -74,4 +74,22 @@ TEST(TFTP, custom_fwrite) {
free(write_file.data);
write_file.data = NULL;
write_file.len = 0;
}
TEST(TFTP, custom_fread_fwrite_suggested_by_Lorentz) {
write_file.data = (char*)malloc(21 * sizeof(char));
write_file.len = 21;
write_file.offset = 0;
tftp_custom_fwrite("1234567890123456789", 19, &write_file);
tftp_custom_fwrite("0987654321", 10, &write_file);
EXPECT_EQ(write_file.offset, 20);
EXPECT_EQ(write_file.len, 21);
EXPECT_EQ(memcmp(write_file.data, "12345678901234567890", 20), 0);
free(write_file.data);
char buf[21];
file.offset = 9;
tftp_custom_fread(buf, 20, &file);
EXPECT_EQ(memcmp(buf, "0", 1), 0);
}