TFTP
Add custom fseek and fread Add test framework gtest and added my custom fread and fseek tests + functions
This commit is contained in:
@@ -37,6 +37,50 @@ int str_cat(char* dest, size_t dest_size, char c)
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief tftp custom file functions to set the offset and read the data
|
||||
* @param[in,out] handle Custom file handles
|
||||
* @param[in] offset The offset to set
|
||||
* @param[in] whence The origin of the offset
|
||||
*/
|
||||
void tftp_custom_fseek(tftp_custom_file_t* handle, size_t offset, int whence) {
|
||||
switch (whence) {
|
||||
case SEEK_SET:
|
||||
handle->ofset = offset;
|
||||
break;
|
||||
case SEEK_CUR:
|
||||
handle->ofset += offset;
|
||||
break;
|
||||
case SEEK_END:
|
||||
break;
|
||||
}
|
||||
if (handle->ofset > handle->len) {
|
||||
handle->ofset = handle->len;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief tftp custom file functions to read the data
|
||||
* auto rolling over the offset
|
||||
* if the bytes to read is bigger than the remaining bytes
|
||||
* it will read the remaining bytes and set the bytes to 0
|
||||
* @param[out] buf The buffer to write the data to
|
||||
* @param[in] bytes The number of bytes to read
|
||||
* @param[in,out] handle Custom file handles
|
||||
*/
|
||||
size_t tftp_custom_fread(void* buf, size_t bytes, tftp_custom_file_t* handle) {
|
||||
if (handle->ofset + bytes > handle->len) {
|
||||
bytes = handle->len - handle->ofset;
|
||||
}
|
||||
memcpy(buf, handle->data + handle->ofset, bytes);
|
||||
handle->ofset += bytes;
|
||||
((char*)buf)[bytes] = '\0';
|
||||
if (handle->ofset > handle->len) {
|
||||
bytes = 0;
|
||||
}
|
||||
return bytes;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief tftp helper functions
|
||||
*/
|
||||
@@ -94,6 +138,7 @@ void tftp_close(void* handle) {
|
||||
* @return int >= 0: Success; < 0: Error
|
||||
*/
|
||||
int tftp_read(void* handle, void* buf, int bytes) {
|
||||
int ret = 0;
|
||||
LOG_INFO(TAG, "reading file");
|
||||
if (handle == NULL) {
|
||||
LOG_CRIT(TAG, "handle is null");
|
||||
@@ -102,17 +147,18 @@ int tftp_read(void* handle, void* buf, int bytes) {
|
||||
FILE* file = (FILE*)handle;
|
||||
|
||||
if (file == &virt_file[0]) {
|
||||
// TODO: read index.txt using tftp_custom_file
|
||||
LOG_CRIT(TAG, "Reading from index.txt is not implemented yet");
|
||||
return -1;
|
||||
|
||||
ret = tftp_custom_fread(buf, bytes, file);
|
||||
return ret;
|
||||
} else if (file == &virt_file[1]) {
|
||||
LOG_CRIT(TAG, "Exception: Trying to read a write only file");
|
||||
return -1;
|
||||
}
|
||||
// TODO: waiting on Lorentz to finish creating f* functions for LLFS
|
||||
fread(buf, sizeof(uint8_t), bytes, file);
|
||||
return 0;
|
||||
ret = fread(buf, sizeof(uint8_t), bytes, file);
|
||||
if (ret <= 0) {
|
||||
return -1;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -144,7 +190,7 @@ void init_index(void)
|
||||
len += strlen(root->name) + 1;
|
||||
root = root->next;
|
||||
}
|
||||
|
||||
len++; // +1 for the \0
|
||||
virt_file[0].data = malloc(len);
|
||||
virt_file[0].len = len;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user