AP_Filesystem: use ExpandingString class

This commit is contained in:
Andrew Tridgell 2020-12-30 17:23:26 +11:00 committed by Peter Barker
parent 0c6b4a1045
commit c924a9917e
2 changed files with 33 additions and 57 deletions

View File

@ -22,26 +22,22 @@
#include <AP_Math/AP_Math.h> #include <AP_Math/AP_Math.h>
#include <AP_CANManager/AP_CANManager.h> #include <AP_CANManager/AP_CANManager.h>
#include <AP_Scheduler/AP_Scheduler.h> #include <AP_Scheduler/AP_Scheduler.h>
#include <AP_Common/ExpandingString.h>
extern const AP_HAL::HAL& hal; extern const AP_HAL::HAL& hal;
struct SysFileList { struct SysFileList {
const char* name; const char* name;
uint32_t filesize;
}; };
static const SysFileList sysfs_file_list[] = { static const SysFileList sysfs_file_list[] = {
#if HAL_ENABLE_THREAD_STATISTICS {"threads.txt"},
{"threads.txt", 2048}, {"tasks.txt"},
#else {"dma.txt"},
{"threads.txt", 1024},
#endif
{"tasks.txt", 6500},
{"dma.txt", 1024},
#if HAL_MAX_CAN_PROTOCOL_DRIVERS #if HAL_MAX_CAN_PROTOCOL_DRIVERS
{"can_log.txt", 1024}, {"can_log.txt"},
{"can0_stats.txt", 1024}, {"can0_stats.txt"},
{"can1_stats.txt", 1024}, {"can1_stats.txt"},
#endif #endif
}; };
@ -71,51 +67,34 @@ int AP_Filesystem_Sys::open(const char *fname, int flags)
return -1; return -1;
} }
struct rfile &r = file[idx]; struct rfile &r = file[idx];
r.data = new file_data; r.str = new ExpandingString;
if (r.data == nullptr) { if (r.str == nullptr) {
errno = ENOMEM; errno = ENOMEM;
return -1; return -1;
} }
// This ensure that whenever new sys file is added its also added to list above // This ensure that whenever new sys file is added its also added to list above
int8_t pos = file_in_sysfs(fname); int8_t pos = file_in_sysfs(fname);
uint32_t max_size = 0; if (pos < 0) {
if (pos >= 0) { delete r.str;
max_size = sysfs_file_list[pos].filesize; r.str = nullptr;
} else {
errno = ENOENT; errno = ENOENT;
return -1; return -1;
} }
if (strcmp(fname, "threads.txt") == 0) { if (strcmp(fname, "threads.txt") == 0) {
r.data->data = (char *)malloc(max_size); hal.util->thread_info(*r.str);
if (r.data->data) {
r.data->length = hal.util->thread_info(r.data->data, max_size);
}
} }
if (strcmp(fname, "tasks.txt") == 0) { if (strcmp(fname, "tasks.txt") == 0) {
r.data->data = (char *)malloc(max_size); AP::scheduler().task_info(*r.str);
if (r.data->data) {
r.data->length = AP::scheduler().task_info(r.data->data, max_size);
if (r.data->length == 0) { // the feature may be disabled
free(r.data->data);
r.data->data = nullptr;
}
}
} }
if (strcmp(fname, "dma.txt") == 0) { if (strcmp(fname, "dma.txt") == 0) {
r.data->data = (char *)malloc(max_size); hal.util->dma_info(*r.str);
if (r.data->data) {
r.data->length = hal.util->dma_info(r.data->data, max_size);
}
} }
#if HAL_MAX_CAN_PROTOCOL_DRIVERS #if HAL_MAX_CAN_PROTOCOL_DRIVERS
int8_t can_stats_num = -1; int8_t can_stats_num = -1;
if (strcmp(fname, "can_log.txt") == 0) { if (strcmp(fname, "can_log.txt") == 0) {
r.data->data = (char *)malloc(max_size); AP::can().log_retrieve(*r.str);
if (r.data->data) {
r.data->length = AP::can().log_retrieve(r.data->data, max_size);
}
} else if (strcmp(fname, "can0_stats.txt") == 0) { } else if (strcmp(fname, "can0_stats.txt") == 0) {
can_stats_num = 0; can_stats_num = 0;
} else if (strcmp(fname, "can1_stats.txt") == 0) { } else if (strcmp(fname, "can1_stats.txt") == 0) {
@ -123,16 +102,14 @@ int AP_Filesystem_Sys::open(const char *fname, int flags)
} }
if (can_stats_num != -1 && can_stats_num < HAL_NUM_CAN_IFACES) { if (can_stats_num != -1 && can_stats_num < HAL_NUM_CAN_IFACES) {
if (hal.can[can_stats_num] != nullptr) { if (hal.can[can_stats_num] != nullptr) {
r.data->data = (char *)malloc(max_size); hal.can[can_stats_num]->get_stats(*r.str);
if (r.data->data) {
r.data->length = hal.can[can_stats_num]->get_stats(r.data->data, max_size);
}
} }
} }
#endif #endif
if (r.data->data == nullptr) { if (r.str->get_length() == 0) {
delete r.data; errno = r.str->has_failed_allocation()?ENOMEM:ENOENT;
errno = ENOENT; delete r.str;
r.str = nullptr;
return -1; return -1;
} }
r.file_ofs = 0; r.file_ofs = 0;
@ -148,8 +125,8 @@ int AP_Filesystem_Sys::close(int fd)
} }
struct rfile &r = file[fd]; struct rfile &r = file[fd];
r.open = false; r.open = false;
free(r.data->data); delete r.str;
delete r.data; r.str = nullptr;
return 0; return 0;
} }
@ -160,8 +137,8 @@ int32_t AP_Filesystem_Sys::read(int fd, void *buf, uint32_t count)
return -1; return -1;
} }
struct rfile &r = file[fd]; struct rfile &r = file[fd];
count = MIN(count, r.data->length - r.file_ofs); count = MIN(count, r.str->get_length() - r.file_ofs);
memcpy(buf, &r.data->data[r.file_ofs], count); memcpy(buf, &r.str->get_string()[r.file_ofs], count);
r.file_ofs += count; r.file_ofs += count;
return count; return count;
} }
@ -175,10 +152,10 @@ int32_t AP_Filesystem_Sys::lseek(int fd, int32_t offset, int seek_from)
struct rfile &r = file[fd]; struct rfile &r = file[fd];
switch (seek_from) { switch (seek_from) {
case SEEK_SET: case SEEK_SET:
r.file_ofs = MIN(offset, int32_t(r.data->length)); r.file_ofs = MIN(offset, int32_t(r.str->get_length()));
break; break;
case SEEK_CUR: case SEEK_CUR:
r.file_ofs = MIN(r.data->length, offset+r.file_ofs); r.file_ofs = MIN(r.str->get_length(), offset+r.file_ofs);
break; break;
case SEEK_END: case SEEK_END:
errno = EINVAL; errno = EINVAL;
@ -246,6 +223,8 @@ int AP_Filesystem_Sys::stat(const char *pathname, struct stat *stbuf)
errno = ENOENT; errno = ENOENT;
return -1; return -1;
} }
stbuf->st_size = sysfs_file_list[pos].filesize; // give a fixed size for stat. It is too expensive to
// read every file for a directory listing
stbuf->st_size = 100000;
return 0; return 0;
} }

View File

@ -17,6 +17,8 @@
#include "AP_Filesystem_backend.h" #include "AP_Filesystem_backend.h"
class ExpandingString;
class AP_Filesystem_Sys : public AP_Filesystem_Backend class AP_Filesystem_Sys : public AP_Filesystem_Backend
{ {
public: public:
@ -35,11 +37,6 @@ private:
static constexpr uint8_t max_open_file = 4; static constexpr uint8_t max_open_file = 4;
int8_t file_in_sysfs(const char *fname); int8_t file_in_sysfs(const char *fname);
struct file_data {
char *data;
size_t length;
};
struct DirReadTracker { struct DirReadTracker {
size_t file_offset; size_t file_offset;
struct dirent curr_file; struct dirent curr_file;
@ -48,6 +45,6 @@ private:
struct rfile { struct rfile {
bool open; bool open;
uint32_t file_ofs; uint32_t file_ofs;
struct file_data *data; ExpandingString *str;
} file[max_open_file]; } file[max_open_file];
}; };