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 Sep 22, 2021
1 parent 301af46 commit c45bdab
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 0 deletions.
130 changes: 130 additions & 0 deletions libraries/AP_Filesystem/AP_Filesystem_Sys.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,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 @@ -30,12 +31,20 @@ 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"},
{"general_metadata.json"},
#if HAL_LOG_EVENT_METADATA
{"events_metadata.json"},
#endif
#if HAL_MAX_CAN_PROTOCOL_DRIVERS
{"can_log.txt"},
#endif
Expand All @@ -57,6 +66,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)
{
if ((flags & O_ACCMODE) != O_RDONLY) {
Expand Down Expand Up @@ -106,7 +227,16 @@ int AP_Filesystem_Sys::open(const char *fname, int flags)
if (strcmp(fname, "uarts.txt") == 0) {
hal.util->uart_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
int8_t can_stats_num = -1;
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 @@ -47,4 +47,7 @@ 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);
};

0 comments on commit c45bdab

Please sign in to comment.