mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-07-02 02:51:42 +00:00
[hal, wpilib] Add RobotController.getComments() (#4463)
This commit is contained in:
@@ -19,6 +19,7 @@
|
||||
#include <FRC_NetworkCommunication/UsageReporting.h>
|
||||
#include <fmt/format.h>
|
||||
#include <wpi/MemoryBuffer.h>
|
||||
#include <wpi/SmallString.h>
|
||||
#include <wpi/StringExtras.h>
|
||||
#include <wpi/fs.h>
|
||||
#include <wpi/mutex.h>
|
||||
@@ -41,6 +42,10 @@ static std::unique_ptr<tGlobal> global;
|
||||
static std::unique_ptr<tSysWatchdog> watchdog;
|
||||
static uint64_t dsStartTime;
|
||||
|
||||
static char roboRioCommentsString[64];
|
||||
static size_t roboRioCommentsStringSize;
|
||||
static bool roboRioCommentsStringInitialized;
|
||||
|
||||
using namespace hal;
|
||||
|
||||
namespace hal {
|
||||
@@ -287,6 +292,67 @@ size_t HAL_GetSerialNumber(char* buffer, size_t size) {
|
||||
}
|
||||
}
|
||||
|
||||
void InitializeRoboRioComments(void) {
|
||||
if (!roboRioCommentsStringInitialized) {
|
||||
std::error_code ec;
|
||||
std::unique_ptr<wpi::MemoryBuffer> fileBuffer =
|
||||
wpi::MemoryBuffer::GetFile("/etc/machine-info", ec);
|
||||
|
||||
std::string_view fileContents;
|
||||
if (fileBuffer && !ec) {
|
||||
fileContents =
|
||||
std::string_view(reinterpret_cast<const char*>(fileBuffer->begin()),
|
||||
fileBuffer->size());
|
||||
} else {
|
||||
roboRioCommentsStringSize = 0;
|
||||
roboRioCommentsStringInitialized = true;
|
||||
return;
|
||||
}
|
||||
std::string_view searchString = "PRETTY_HOSTNAME=\"";
|
||||
|
||||
size_t start = fileContents.find(searchString);
|
||||
if (start == std::string_view::npos) {
|
||||
roboRioCommentsStringSize = 0;
|
||||
roboRioCommentsStringInitialized = true;
|
||||
return;
|
||||
}
|
||||
start += searchString.size();
|
||||
size_t end = fileContents.find("\"", start);
|
||||
if (end == std::string_view::npos) {
|
||||
end = fileContents.size();
|
||||
}
|
||||
std::string_view escapedComments = wpi::slice(fileContents, start, end);
|
||||
wpi::SmallString<64> buf;
|
||||
auto [unescapedComments, rem] = wpi::UnescapeCString(escapedComments, buf);
|
||||
unescapedComments.copy(roboRioCommentsString,
|
||||
sizeof(roboRioCommentsString));
|
||||
|
||||
if (unescapedComments.size() > sizeof(roboRioCommentsString)) {
|
||||
roboRioCommentsStringSize = sizeof(roboRioCommentsString);
|
||||
} else {
|
||||
roboRioCommentsStringSize = unescapedComments.size();
|
||||
}
|
||||
roboRioCommentsStringInitialized = true;
|
||||
}
|
||||
}
|
||||
|
||||
size_t HAL_GetComments(char* buffer, size_t size) {
|
||||
if (!roboRioCommentsStringInitialized) {
|
||||
InitializeRoboRioComments();
|
||||
}
|
||||
size_t toCopy = size;
|
||||
if (size > roboRioCommentsStringSize) {
|
||||
toCopy = roboRioCommentsStringSize;
|
||||
}
|
||||
std::memcpy(buffer, roboRioCommentsString, toCopy);
|
||||
if (toCopy < size) {
|
||||
buffer[toCopy] = '\0';
|
||||
} else {
|
||||
buffer[toCopy - 1] = '\0';
|
||||
}
|
||||
return toCopy;
|
||||
}
|
||||
|
||||
uint64_t HAL_GetFPGATime(int32_t* status) {
|
||||
hal::init::CheckInit();
|
||||
if (!global) {
|
||||
|
||||
@@ -42,6 +42,19 @@ size_t HALSIM_GetRoboRioSerialNumber(char* buffer, size_t size) {
|
||||
}
|
||||
void HALSIM_SetRoboRioSerialNumber(const char* buffer, size_t size) {}
|
||||
|
||||
int32_t HALSIM_RegisterRoboRioCommentsCallback(
|
||||
HAL_RoboRioStringCallback callback, void* param, HAL_Bool initialNotify) {
|
||||
return 0;
|
||||
}
|
||||
void HALSIM_CancelRoboRioCommentsCallback(int32_t uid) {}
|
||||
size_t HALSIM_GetRoboRioComments(char* buffer, size_t size) {
|
||||
if (size > 0) {
|
||||
buffer[0] = '\0';
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
void HALSIM_SetRoboRioComments(const char* buffer, size_t size) {}
|
||||
|
||||
void HALSIM_RegisterRoboRioAllCallbacks(HAL_NotifyCallback callback,
|
||||
void* param, HAL_Bool initialNotify) {}
|
||||
} // extern "C"
|
||||
|
||||
Reference in New Issue
Block a user