[hal] Report previous allocation location for indexed resource duplicates (#3322)

This commit is contained in:
Thad House
2021-05-01 10:28:30 -07:00
committed by GitHub
parent e338f9f190
commit 23d2326d1d
54 changed files with 573 additions and 251 deletions

View File

@@ -21,11 +21,31 @@ static LastErrorStorage& GetThreadLastError() {
}
namespace hal {
void SetLastError(int32_t status, const wpi::Twine& value) {
void SetLastError(int32_t* status, const wpi::Twine& value) {
LastErrorStorage& lastError = GetThreadLastError();
lastError.message.clear();
value.toVector(lastError.message);
lastError.status = status;
lastError.status = *status;
*status = HAL_USE_LAST_ERROR;
}
void SetLastErrorIndexOutOfRange(int32_t* status, const wpi::Twine& message,
int32_t minimum, int32_t maximum,
int32_t requested) {
SetLastError(status, message + "\n Status: " + wpi::Twine(*status) +
"\n Minimum: " + wpi::Twine(minimum) +
" Maximum: " + wpi::Twine(maximum) +
" Reequested: " + wpi::Twine(requested));
}
void SetLastErrorPreviouslyAllocated(int32_t* status, const wpi::Twine& message,
int32_t channel,
const wpi::Twine& previousAllocation) {
hal::SetLastError(
status,
message + " " + wpi::Twine(channel) +
" previously allocated.\nLocation of the previous allocation:\n" +
previousAllocation + "\nLocation of the current allocation:");
}
} // namespace hal

View File

@@ -6,6 +6,8 @@
#include <cassert>
#include <wpi/jni_util.h>
#include "HALUtil.h"
#include "edu_wpi_first_hal_AnalogGyroJNI.h"
#include "hal/AnalogGyro.h"
@@ -24,8 +26,9 @@ Java_edu_wpi_first_hal_AnalogGyroJNI_initializeAnalogGyro
(JNIEnv* env, jclass, jint id)
{
int32_t status = 0;
HAL_GyroHandle handle =
HAL_InitializeAnalogGyro((HAL_AnalogInputHandle)id, &status);
auto stack = wpi::java::GetJavaStackTrace(env, "edu.wpi.first");
HAL_GyroHandle handle = HAL_InitializeAnalogGyro((HAL_AnalogInputHandle)id,
stack.c_str(), &status);
// Analog input does range checking, so we don't need to do so.
CheckStatusForceThrow(env, status);
return (jint)handle;

View File

@@ -6,6 +6,8 @@
#include <cassert>
#include <wpi/jni_util.h>
#include "HALUtil.h"
#include "edu_wpi_first_hal_AnalogJNI.h"
#include "hal/AnalogAccumulator.h"
@@ -29,9 +31,10 @@ Java_edu_wpi_first_hal_AnalogJNI_initializeAnalogInputPort
(JNIEnv* env, jclass, jint id)
{
int32_t status = 0;
auto analog = HAL_InitializeAnalogInputPort((HAL_PortHandle)id, &status);
CheckStatusRange(env, status, 0, HAL_GetNumAnalogInputs(),
hal::getPortHandleChannel((HAL_PortHandle)id));
auto stack = wpi::java::GetJavaStackTrace(env, "edu.wpi.first");
auto analog =
HAL_InitializeAnalogInputPort((HAL_PortHandle)id, stack.c_str(), &status);
CheckStatusForceThrow(env, status);
return (jint)analog;
}
@@ -57,10 +60,10 @@ Java_edu_wpi_first_hal_AnalogJNI_initializeAnalogOutputPort
(JNIEnv* env, jclass, jint id)
{
int32_t status = 0;
HAL_AnalogOutputHandle analog =
HAL_InitializeAnalogOutputPort((HAL_PortHandle)id, &status);
CheckStatusRange(env, status, 0, HAL_GetNumAnalogOutputs(),
hal::getPortHandleChannel((HAL_PortHandle)id));
auto stack = wpi::java::GetJavaStackTrace(env, "edu.wpi.first");
HAL_AnalogOutputHandle analog = HAL_InitializeAnalogOutputPort(
(HAL_PortHandle)id, stack.c_str(), &status);
CheckStatusForceThrow(env, status);
return (jlong)analog;
}

View File

@@ -6,6 +6,8 @@
#include <cassert>
#include <wpi/jni_util.h>
#include "HALUtil.h"
#include "edu_wpi_first_hal_DIOJNI.h"
#include "hal/DIO.h"
@@ -27,10 +29,10 @@ Java_edu_wpi_first_hal_DIOJNI_initializeDIOPort
(JNIEnv* env, jclass, jint id, jboolean input)
{
int32_t status = 0;
auto dio = HAL_InitializeDIOPort((HAL_PortHandle)id,
static_cast<uint8_t>(input), &status);
CheckStatusRange(env, status, 0, HAL_GetNumDigitalChannels(),
hal::getPortHandleChannel((HAL_PortHandle)id));
auto stack = wpi::java::GetJavaStackTrace(env, "edu.wpi.first");
auto dio = HAL_InitializeDIOPort(
(HAL_PortHandle)id, static_cast<uint8_t>(input), stack.c_str(), &status);
CheckStatusForceThrow(env, status);
return (jint)dio;
}

View File

@@ -83,15 +83,13 @@ void ThrowUncleanStatusException(JNIEnv* env, wpi::StringRef msg,
env->Throw(static_cast<jthrowable>(exception));
}
void ThrowAllocationException(JNIEnv* env, int32_t minRange, int32_t maxRange,
int32_t requestedValue, int32_t status) {
const char* message = HAL_GetLastError(&status);
void ThrowAllocationException(JNIEnv* env, const char* lastError,
int32_t status) {
wpi::SmallString<1024> buf;
wpi::raw_svector_ostream oss(buf);
oss << " Code: " << status << ". " << message
<< ", Minimum Value: " << minRange << ", Maximum Value: " << maxRange
<< ", Requested Value: " << requestedValue;
env->ThrowNew(allocationExCls, buf.c_str());
oss << "Code: " << status << '\n' << lastError;
allocationExCls.Throw(env, buf.c_str());
}
@@ -107,10 +105,11 @@ void ReportError(JNIEnv* env, int32_t status, bool doThrow) {
if (status == 0) {
return;
}
const char* message = HAL_GetLastError(&status);
if (status == HAL_HANDLE_ERROR) {
ThrowHalHandleException(env, status);
return;
}
const char* message = HAL_GetLastError(&status);
if (doThrow && status < 0) {
wpi::SmallString<1024> buf;
wpi::raw_svector_ostream oss(buf);
@@ -132,17 +131,19 @@ void ThrowError(JNIEnv* env, int32_t status, int32_t minRange, int32_t maxRange,
if (status == 0) {
return;
}
const char* lastError = HAL_GetLastError(&status);
if (status == NO_AVAILABLE_RESOURCES || status == RESOURCE_IS_ALLOCATED ||
status == RESOURCE_OUT_OF_RANGE) {
ThrowAllocationException(env, minRange, maxRange, requestedValue, status);
ThrowAllocationException(env, lastError, status);
return;
}
if (status == HAL_HANDLE_ERROR) {
ThrowHalHandleException(env, status);
return;
}
const char* message = HAL_GetErrorMessage(status);
wpi::SmallString<1024> buf;
wpi::raw_svector_ostream oss(buf);
oss << " Code: " << status << ". " << message;
oss << " Code: " << status << ". " << lastError;
ThrowUncleanStatusException(env, buf.c_str(), status);
}

View File

@@ -6,6 +6,8 @@
#include <cassert>
#include <wpi/jni_util.h>
#include "HALUtil.h"
#include "edu_wpi_first_hal_PWMJNI.h"
#include "hal/DIO.h"
@@ -27,9 +29,9 @@ Java_edu_wpi_first_hal_PWMJNI_initializePWMPort
(JNIEnv* env, jclass, jint id)
{
int32_t status = 0;
auto pwm = HAL_InitializePWMPort((HAL_PortHandle)id, &status);
CheckStatusRange(env, status, 0, HAL_GetNumPWMChannels(),
hal::getPortHandleChannel((HAL_PortHandle)id));
auto stack = wpi::java::GetJavaStackTrace(env, "edu.wpi.first");
auto pwm = HAL_InitializePWMPort((HAL_PortHandle)id, stack.c_str(), &status);
CheckStatusForceThrow(env, status);
return (jint)pwm;
}

View File

@@ -6,6 +6,8 @@
#include <cassert>
#include <wpi/jni_util.h>
#include "HALUtil.h"
#include "edu_wpi_first_hal_RelayJNI.h"
#include "hal/Ports.h"
@@ -26,10 +28,10 @@ Java_edu_wpi_first_hal_RelayJNI_initializeRelayPort
(JNIEnv* env, jclass, jint id, jboolean fwd)
{
int32_t status = 0;
auto stack = wpi::java::GetJavaStackTrace(env, "edu.wpi.first");
HAL_RelayHandle handle = HAL_InitializeRelayPort(
(HAL_PortHandle)id, static_cast<uint8_t>(fwd), &status);
CheckStatusRange(env, status, 0, HAL_GetNumRelayChannels(),
hal::getPortHandleChannel((HAL_PortHandle)id));
(HAL_PortHandle)id, static_cast<uint8_t>(fwd), stack.c_str(), &status);
CheckStatusForceThrow(env, status);
return (jint)handle;
}