Skip to content

Commit

Permalink
AP_FileSystem: add ability to emit generate_metadata.json
Browse files Browse the repository at this point in the history
  • Loading branch information
peterbarker committed Aug 7, 2024
1 parent c1ca503 commit 9f5f109
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 0 deletions.
129 changes: 129 additions & 0 deletions libraries/AP_Filesystem/AP_Filesystem_Sys.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <AP_Math/AP_Math.h>
#include <AP_CANManager/AP_CANManager.h>
#include <AP_Scheduler/AP_Scheduler.h>
#include <AP_Logger/AP_Logger.h>
#include <AP_Common/ExpandingString.h>

extern const AP_HAL::HAL& hal;
Expand All @@ -33,13 +34,21 @@ struct SysFileList {
const char* name;
};

#ifndef HAL_LOG_EVENT_METADATA
#define HAL_LOG_EVENT_METADATA 1
#endif

static const SysFileList sysfs_file_list[] = {
{"threads.txt"},
{"tasks.txt"},
{"dma.txt"},
{"memory.txt"},
{"uarts.txt"},
{"timers.txt"},
{"general_metadata.json"},
#if HAL_LOG_EVENT_METADATA
{"events_metadata.json"},
#endif
#if HAL_MAX_CAN_PROTOCOL_DRIVERS
{"can_log.txt"},
#endif
Expand All @@ -66,6 +75,118 @@ int8_t AP_Filesystem_Sys::file_in_sysfs(const char *fname) {
return -1;
}

void AP_Filesystem_Sys::general_metadata(ExpandingString &str)
{
const struct MetaDataInfo {
int type_id;
const char * uri;
} metadata[] = {
3, // COMP_METADATA_TYPE_EVENTS
#if HAL_LOG_EVENT_METADATA
"mftp:/@SYS/events_metadata.json"
#else
"https://firmware.ardupilot.org/GITHASH/events_metadata.json"
#endif
};
// a header to allow for machine parsers to determine format
str.printf(
"{"
" \"version\": 1,"
" \"metadataTypes\": ["
);

// FIXME: too many [?
const char *joiner = "";
for (const MetaDataInfo &info : metadata) {
str.printf("%s{\"type\": %d, \"uri\": \"%s\", \"fileCrc\": 133761337}",
joiner,
info.type_id,
info.uri);
joiner = ",";
}

str.printf(
" ]}"
);

}

#if HAL_LOG_EVENT_METADATA

void AP_Filesystem_Sys::events_metadata(ExpandingString &str)
{
static const struct {
LogEvent value;
const char *name;
const char *description;
} logevent_metadata[] {
{ LogEvent::ARMED, "Armed", "Vehicle was armed" },
{ LogEvent::DISARMED, "Disarmed", "Vehicle was disarmed" },
{ LogEvent::SET_HOME, "SetHome", "Vehicle home location was set" },
};

// FIXME: this is really the wrong schema for the LogError stuff.
static const struct {
LogErrorSubsystem value;
const char *name;
const char *description;
} logerror_metadata[] {
{ LogErrorSubsystem::MAIN, "Main", "Bogus generic bucket for everything unclassified elsewhere" },
};

// a header to allow for machine parsers to determine format
str.printf(
"{ "
" \"version\": 1, "
" \"components\": [ "
" { "
" \"component_id\": %u, "
" \"namespace\": \"common\", "
" \"enums\": [ "
" { "
" \"name\": \"ardupilot_event\", "
" \"type\": \"uint8_t\", "
" \"description\": \"Generic ArduPilot events from AP_Logger::LogEvent\", "
" \"entries\": [ ",
1); // FIXME: should be mavlink component ID of autopilot

const char *joiner = "";
for (auto &x : logevent_metadata) {
str.printf(
"%s{\"value\":%u, \"name\":\"%s\", \"description\":\"%s\"}\n",
joiner,
(unsigned)x.value,
x.name,
x.description
);
joiner = ",";
}
str.printf("]}, [{");
str.printf(
" \"name\": \"ardupilot_errors\", "
" \"type\": \"uint8_t\", "
" \"description\": \"Generic ArduPilot errors from AP_Logger::LogErrorSubsystem\", "
" \"entries\": [ "
);

joiner = "";
for (auto &x : logerror_metadata) {
str.printf(
"%s{\"value\":%u, \"name\":\"%s\", \"description\":\"%s\"}\n",
joiner,
(unsigned)x.value,
x.name,
x.description
);
joiner = ",";
}

str.printf("]}]");
str.printf("]}]}");
}

#endif

int AP_Filesystem_Sys::open(const char *fname, int flags, bool allow_absolute_paths)
{
if ((flags & O_ACCMODE) != O_RDONLY) {
Expand Down Expand Up @@ -120,6 +241,14 @@ int AP_Filesystem_Sys::open(const char *fname, int flags, bool allow_absolute_pa
if (strcmp(fname, "timers.txt") == 0) {
hal.util->timer_info(*r.str);
}
if (strcmp(fname, "general_metadata.json") == 0) {
general_metadata(*r.str);
}
#if HAL_LOG_EVENT_METADATA
if (strcmp(fname, "events_metadata.json") == 0) {
events_metadata(*r.str);
}
#endif
#if HAL_CANMANAGER_ENABLED
if (strcmp(fname, "can_log.txt") == 0) {
AP::can().log_retrieve(*r.str);
Expand Down
3 changes: 3 additions & 0 deletions libraries/AP_Filesystem/AP_Filesystem_Sys.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ class AP_Filesystem_Sys : public AP_Filesystem_Backend
uint32_t file_ofs;
ExpandingString *str;
} file[max_open_file];

void general_metadata(ExpandingString &str);
void events_metadata(ExpandingString &str);
};

#endif // AP_FILESYSTEM_SYS_ENABLED

0 comments on commit 9f5f109

Please sign in to comment.