From 097e0fbed2b2fd32146b8993f57eb72287f443a2 Mon Sep 17 00:00:00 2001 From: Peter Barker Date: Tue, 27 Apr 2021 14:38:45 +1000 Subject: [PATCH] more support mavlink event interface --- libraries/AP_Filesystem/AP_Filesystem_Sys.cpp | 102 +++++++++++++++++- libraries/AP_Filesystem/AP_Filesystem_Sys.h | 1 + 2 files changed, 101 insertions(+), 2 deletions(-) diff --git a/libraries/AP_Filesystem/AP_Filesystem_Sys.cpp b/libraries/AP_Filesystem/AP_Filesystem_Sys.cpp index 74855707ddcf1a..6261f938e3457c 100644 --- a/libraries/AP_Filesystem/AP_Filesystem_Sys.cpp +++ b/libraries/AP_Filesystem/AP_Filesystem_Sys.cpp @@ -22,6 +22,7 @@ #include #include #include +#include #include extern const AP_HAL::HAL& hal; @@ -30,6 +31,10 @@ 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"}, @@ -37,6 +42,9 @@ static const SysFileList sysfs_file_list[] = { {"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"}, {"can0_stats.txt"}, @@ -63,7 +71,11 @@ void AP_Filesystem_Sys::general_metadata(ExpandingString &str) 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( @@ -71,18 +83,99 @@ void AP_Filesystem_Sys::general_metadata(ExpandingString &str) " \"version\": 1," " \"metadataTypes\": [" ); + + // FIXME: too many [? + const char *joiner = ""; for (const MetaDataInfo &info : metadata) { - str.printf("[ {\"type\": %d, \"uri\": \"%s\", \"fileCrc\": 133761337} ],", + 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) { @@ -133,6 +226,11 @@ int AP_Filesystem_Sys::open(const char *fname, int flags) 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_MAX_CAN_PROTOCOL_DRIVERS int8_t can_stats_num = -1; if (strcmp(fname, "can_log.txt") == 0) { diff --git a/libraries/AP_Filesystem/AP_Filesystem_Sys.h b/libraries/AP_Filesystem/AP_Filesystem_Sys.h index 85b497ac999d6e..882bb26331a647 100644 --- a/libraries/AP_Filesystem/AP_Filesystem_Sys.h +++ b/libraries/AP_Filesystem/AP_Filesystem_Sys.h @@ -49,4 +49,5 @@ class AP_Filesystem_Sys : public AP_Filesystem_Backend } file[max_open_file]; void general_metadata(ExpandingString &str); + void events_metadata(ExpandingString &str); };