mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-20 00:51:42 +00:00
This is a major restructuring of the WPILib repository to simply build procedures and remove the remnants of Maven from everything except the eclipse plugins. Gradle files have been largely simplified or rewritten, taking advantage of splitting up parts of the build into separate build files for ease of reading. The eclipse plugins are now in a separate project, as is ntcore. All dependencies are resolved via Maven dependencies, with the Jenkins-maintained WPILib repo. Project structures have also been simplified: we no longer have separate subprojects inside wpilibc and wpilibj. Where possible, these changes hav been done with git renames, to make sure we still have full history for all repositories. Other unrelated subprojects have also been broken out: OutlineViewer is now a separate project. Change-Id: Ib4e2a6e1a2f66427a14f16612b0e0d69ed661878
221 lines
6.8 KiB
C++
221 lines
6.8 KiB
C++
/*----------------------------------------------------------------------------*/
|
|
/* Copyright (c) FIRST 2008. All Rights Reserved.
|
|
*/
|
|
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
|
/* must be accompanied by the FIRST BSD license file in $(WIND_BASE)/WPILib. */
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
#include "Utility.h"
|
|
|
|
//#include "NetworkCommunication/FRCComm.h"
|
|
#include "HAL/HAL.hpp"
|
|
#include "Task.h"
|
|
#include <iostream>
|
|
#include <sstream>
|
|
#include <cstdio>
|
|
#include <cstdlib>
|
|
#include <cstring>
|
|
#include <execinfo.h>
|
|
#include <cxxabi.h>
|
|
#include "nivision.h"
|
|
|
|
/**
|
|
* Assert implementation.
|
|
* This allows breakpoints to be set on an assert.
|
|
* The users don't call this, but instead use the wpi_assert macros in
|
|
* Utility.h.
|
|
*/
|
|
bool wpi_assert_impl(bool conditionValue, const std::string &conditionText,
|
|
const std::string &message, const std::string &fileName,
|
|
uint32_t lineNumber, const std::string &funcName) {
|
|
if (!conditionValue) {
|
|
std::stringstream errorStream;
|
|
|
|
errorStream << "Assertion \"" << conditionText << "\" ";
|
|
errorStream << "on line " << lineNumber << " ";
|
|
errorStream << "of " << basename(fileName.c_str()) << " ";
|
|
|
|
if (message.size() > 0) {
|
|
errorStream << "failed: " << message << std::endl;
|
|
} else {
|
|
errorStream << "failed." << std::endl;
|
|
}
|
|
|
|
errorStream << GetStackTrace(2);
|
|
|
|
std::string error = errorStream.str();
|
|
|
|
// Print the error and send it to the DriverStation
|
|
std::cout << error << std::endl;
|
|
HALSetErrorData(error.c_str(), error.size(), 100);
|
|
}
|
|
|
|
return conditionValue;
|
|
}
|
|
|
|
/**
|
|
* Common error routines for wpi_assertEqual_impl and wpi_assertNotEqual_impl
|
|
* This should not be called directly; it should only be used by
|
|
* wpi_assertEqual_impl
|
|
* and wpi_assertNotEqual_impl.
|
|
*/
|
|
void wpi_assertEqual_common_impl(const std::string &valueA, const std::string &valueB,
|
|
const std::string &equalityType, const std::string &message,
|
|
const std::string &fileName, uint32_t lineNumber,
|
|
const std::string &funcName) {
|
|
std::stringstream errorStream;
|
|
|
|
errorStream << "Assertion \"" << valueA << " " << equalityType << " "
|
|
<< valueB << "\" ";
|
|
errorStream << "on line " << lineNumber << " ";
|
|
errorStream << "of " << basename(fileName.c_str()) << " ";
|
|
|
|
if (message.size() > 0) {
|
|
errorStream << "failed: " << message << std::endl;
|
|
} else {
|
|
errorStream << "failed." << std::endl;
|
|
}
|
|
|
|
errorStream << GetStackTrace(3);
|
|
|
|
std::string error = errorStream.str();
|
|
|
|
// Print the error and send it to the DriverStation
|
|
std::cout << error << std::endl;
|
|
HALSetErrorData(error.c_str(), error.size(), 100);
|
|
}
|
|
|
|
/**
|
|
* Assert equal implementation.
|
|
* This determines whether the two given integers are equal. If not,
|
|
* the value of each is printed along with an optional message string.
|
|
* The users don't call this, but instead use the wpi_assertEqual macros in
|
|
* Utility.h.
|
|
*/
|
|
bool wpi_assertEqual_impl(int valueA, int valueB, const std::string &valueAString,
|
|
const std::string &valueBString, const std::string &message,
|
|
const std::string &fileName, uint32_t lineNumber,
|
|
const std::string &funcName) {
|
|
if (!(valueA == valueB)) {
|
|
wpi_assertEqual_common_impl(valueAString, valueBString, "==", message,
|
|
fileName, lineNumber, funcName);
|
|
}
|
|
return valueA == valueB;
|
|
}
|
|
|
|
/**
|
|
* Assert not equal implementation.
|
|
* This determines whether the two given integers are equal. If so,
|
|
* the value of each is printed along with an optional message string.
|
|
* The users don't call this, but instead use the wpi_assertNotEqual macros in
|
|
* Utility.h.
|
|
*/
|
|
bool wpi_assertNotEqual_impl(int valueA, int valueB, const std::string &valueAString,
|
|
const std::string &valueBString, const std::string &message,
|
|
const std::string &fileName, uint32_t lineNumber,
|
|
const std::string &funcName) {
|
|
if (!(valueA != valueB)) {
|
|
wpi_assertEqual_common_impl(valueAString, valueBString, "!=", message,
|
|
fileName, lineNumber, funcName);
|
|
}
|
|
return valueA != valueB;
|
|
}
|
|
|
|
/**
|
|
* Return the FPGA Version number.
|
|
* For now, expect this to be competition year.
|
|
* @return FPGA Version number.
|
|
*/
|
|
uint16_t GetFPGAVersion() {
|
|
int32_t status = 0;
|
|
uint16_t version = getFPGAVersion(&status);
|
|
wpi_setGlobalErrorWithContext(status, getHALErrorMessage(status));
|
|
return version;
|
|
}
|
|
|
|
/**
|
|
* Return the FPGA Revision number.
|
|
* The format of the revision is 3 numbers.
|
|
* The 12 most significant bits are the Major Revision.
|
|
* the next 8 bits are the Minor Revision.
|
|
* The 12 least significant bits are the Build Number.
|
|
* @return FPGA Revision number.
|
|
*/
|
|
uint32_t GetFPGARevision() {
|
|
int32_t status = 0;
|
|
uint32_t revision = getFPGARevision(&status);
|
|
wpi_setGlobalErrorWithContext(status, getHALErrorMessage(status));
|
|
return revision;
|
|
}
|
|
|
|
/**
|
|
* Read the microsecond-resolution timer on the FPGA.
|
|
*
|
|
* @return The current time in microseconds according to the FPGA (since FPGA
|
|
* reset).
|
|
*/
|
|
uint32_t GetFPGATime() {
|
|
int32_t status = 0;
|
|
uint32_t time = getFPGATime(&status);
|
|
wpi_setGlobalErrorWithContext(status, getHALErrorMessage(status));
|
|
return time;
|
|
}
|
|
|
|
/**
|
|
* Get the state of the "USER" button on the RoboRIO
|
|
* @return True if the button is currently pressed down
|
|
*/
|
|
bool GetUserButton() {
|
|
int32_t status = 0;
|
|
|
|
bool value = getFPGAButton(&status);
|
|
wpi_setGlobalError(status);
|
|
|
|
return value;
|
|
}
|
|
|
|
/**
|
|
* Demangle a C++ symbol, used for printing stack traces.
|
|
*/
|
|
static std::string demangle(char const *mangledSymbol) {
|
|
char buffer[256];
|
|
size_t length;
|
|
int status;
|
|
|
|
if (sscanf(mangledSymbol, "%*[^(]%*[(]%255[^)+]", buffer)) {
|
|
char *symbol = abi::__cxa_demangle(buffer, nullptr, &length, &status);
|
|
if (status == 0) {
|
|
return symbol;
|
|
} else {
|
|
// If the symbol couldn't be demangled, it's probably a C function,
|
|
// so just return it as-is.
|
|
return buffer;
|
|
}
|
|
}
|
|
|
|
// If everything else failed, just return the mangled symbol
|
|
return mangledSymbol;
|
|
}
|
|
|
|
/**
|
|
* Get a stack trace, ignoring the first "offset" symbols.
|
|
* @param offset The number of symbols at the top of the stack to ignore
|
|
*/
|
|
std::string GetStackTrace(uint32_t offset) {
|
|
void *stackTrace[128];
|
|
int stackSize = backtrace(stackTrace, 128);
|
|
char **mangledSymbols = backtrace_symbols(stackTrace, stackSize);
|
|
std::stringstream trace;
|
|
|
|
for (int i = offset; i < stackSize; i++) {
|
|
// Only print recursive functions once in a row.
|
|
if (i == 0 || stackTrace[i] != stackTrace[i - 1]) {
|
|
trace << "\tat " << demangle(mangledSymbols[i]) << std::endl;
|
|
}
|
|
}
|
|
|
|
free(mangledSymbols);
|
|
|
|
return trace.str();
|
|
}
|