Implements Better Error Messages from the HAL (#172)

* Makes the HAL provide a better error message for certain things.

* Makes Java error messages better

* Updates C++ errors.

* Moves handles header folder to HAL directory
This commit is contained in:
Thad House
2016-07-13 20:29:28 -07:00
committed by Peter Johnson
parent 05c00430b3
commit d2aa168f66
51 changed files with 175 additions and 70 deletions

View File

@@ -67,6 +67,12 @@
#define PARAMETER_OUT_OF_RANGE_MESSAGE "HAL: A parameter is out of range."
#define RESOURCE_IS_ALLOCATED -1029
#define RESOURCE_IS_ALLOCATED_MESSAGE "HAL: Resource already allocated"
#define RESOURCE_OUT_OF_RANGE -1030
#define RESOURCE_OUT_OF_RANGE_MESSAGE \
"HAL: The requested resource is out of range."
#define HAL_INVALID_ACCUMULATOR_CHANNEL -1035
#define HAL_INVALID_ACCUMULATOR_CHANNEL_MESSAGE \
"HAL: The requested input is not an accumulator channel"
#define HAL_COUNTER_NOT_SUPPORTED -1058
#define HAL_COUNTER_NOT_SUPPORTED_MESSAGE \
"HAL: Counter mode not supported for encoder method"

View File

@@ -15,7 +15,7 @@
#include "HAL/Errors.h"
#include "HAL/Types.h"
#include "HAL/cpp/priority_mutex.h"
#include "HandlesInternal.h"
#include "HAL/handles/HandlesInternal.h"
namespace hal {
@@ -67,7 +67,7 @@ THandle DigitalHandleResource<THandle, TStruct, size>::Allocate(
int16_t index, HAL_HandleEnum enumValue, int32_t* status) {
// don't aquire the lock if we can fail early.
if (index < 0 || index >= size) {
*status = PARAMETER_OUT_OF_RANGE;
*status = RESOURCE_OUT_OF_RANGE;
return HAL_kInvalidHandle;
}
std::lock_guard<priority_mutex> sync(m_handleMutexes[index]);

View File

@@ -15,7 +15,7 @@
#include "HAL/Errors.h"
#include "HAL/Types.h"
#include "HAL/cpp/priority_mutex.h"
#include "HandlesInternal.h"
#include "HAL/handles/HandlesInternal.h"
namespace hal {
@@ -73,7 +73,7 @@ THandle IndexedHandleResource<THandle, TStruct, size, enumValue>::Allocate(
int16_t index, int32_t* status) {
// don't aquire the lock if we can fail early.
if (index < 0 || index >= size) {
*status = PARAMETER_OUT_OF_RANGE;
*status = RESOURCE_OUT_OF_RANGE;
return HAL_kInvalidHandle;
}
std::lock_guard<priority_mutex> sync(m_handleMutexes[index]);

View File

@@ -14,7 +14,7 @@
#include "HAL/Types.h"
#include "HAL/cpp/priority_mutex.h"
#include "HandlesInternal.h"
#include "HAL/handles/HandlesInternal.h"
namespace hal {

View File

@@ -14,7 +14,7 @@
#include "HAL/Types.h"
#include "HAL/cpp/priority_mutex.h"
#include "handles/HandlesInternal.h"
#include "HAL/handles/HandlesInternal.h"
namespace hal {

View File

@@ -36,6 +36,10 @@ HAL_Bool HAL_IsAccumulatorChannel(HAL_AnalogInputHandle analog_port_handle,
*/
void HAL_InitAccumulator(HAL_AnalogInputHandle analog_port_handle,
int32_t* status) {
if (!HAL_IsAccumulatorChannel(analog_port_handle, status)) {
*status = HAL_INVALID_ACCUMULATOR_CHANNEL;
return;
}
HAL_SetAccumulatorCenter(analog_port_handle, 0, status);
HAL_ResetAccumulator(analog_port_handle, status);
}

View File

@@ -13,7 +13,7 @@
#include "AnalogInternal.h"
#include "HAL/AnalogAccumulator.h"
#include "HAL/AnalogInput.h"
#include "handles/IndexedHandleResource.h"
#include "HAL/handles/IndexedHandleResource.h"
namespace {
struct AnalogGyro {
@@ -46,7 +46,7 @@ HAL_GyroHandle HAL_InitializeAnalogGyro(HAL_AnalogInputHandle analog_handle,
int32_t* status) {
if (!HAL_IsAccumulatorChannel(analog_handle, status)) {
if (*status == 0) {
*status = PARAMETER_OUT_OF_RANGE;
*status = HAL_INVALID_ACCUMULATOR_CHANNEL;
}
return HAL_kInvalidHandle;
}

View File

@@ -14,8 +14,8 @@
#include "HAL/AnalogAccumulator.h"
#include "HAL/HAL.h"
#include "HAL/cpp/priority_mutex.h"
#include "HAL/handles/HandlesInternal.h"
#include "PortsInternal.h"
#include "handles/HandlesInternal.h"
using namespace hal;

View File

@@ -12,8 +12,8 @@
#include "ChipObject.h"
#include "HAL/Ports.h"
#include "HAL/cpp/priority_mutex.h"
#include "HAL/handles/IndexedHandleResource.h"
#include "PortsInternal.h"
#include "handles/IndexedHandleResource.h"
namespace hal {
constexpr int32_t kTimebase = 40000000; ///< 40 MHz clock

View File

@@ -9,9 +9,9 @@
#include "AnalogInternal.h"
#include "HAL/Errors.h"
#include "HAL/handles/HandlesInternal.h"
#include "HAL/handles/IndexedHandleResource.h"
#include "PortsInternal.h"
#include "handles/HandlesInternal.h"
#include "handles/IndexedHandleResource.h"
using namespace hal;

View File

@@ -10,9 +10,9 @@
#include "AnalogInternal.h"
#include "HAL/AnalogInput.h"
#include "HAL/Errors.h"
#include "HAL/handles/HandlesInternal.h"
#include "HAL/handles/LimitedHandleResource.h"
#include "PortsInternal.h"
#include "handles/HandlesInternal.h"
#include "handles/LimitedHandleResource.h"
using namespace hal;

View File

@@ -8,10 +8,10 @@
#include "HAL/Compressor.h"
#include "HAL/Errors.h"
#include "HAL/handles/HandlesInternal.h"
#include "PCMInternal.h"
#include "PortsInternal.h"
#include "ctre/PCM.h"
#include "handles/HandlesInternal.h"
using namespace hal;

View File

@@ -10,8 +10,8 @@
#include "ConstantsInternal.h"
#include "DigitalInternal.h"
#include "HAL/HAL.h"
#include "HAL/handles/LimitedHandleResource.h"
#include "PortsInternal.h"
#include "handles/LimitedHandleResource.h"
using namespace hal;

View File

@@ -10,9 +10,9 @@
#include <cmath>
#include "DigitalInternal.h"
#include "HAL/handles/HandlesInternal.h"
#include "HAL/handles/LimitedHandleResource.h"
#include "PortsInternal.h"
#include "handles/HandlesInternal.h"
#include "handles/LimitedHandleResource.h"
using namespace hal;

View File

@@ -13,9 +13,9 @@
#include "HAL/AnalogTrigger.h"
#include "HAL/Ports.h"
#include "HAL/Types.h"
#include "HAL/handles/DigitalHandleResource.h"
#include "HAL/handles/HandlesInternal.h"
#include "PortsInternal.h"
#include "handles/DigitalHandleResource.h"
#include "handles/HandlesInternal.h"
namespace hal {
constexpr uint32_t kMXPDigitalPWMOffset = 6; // MXP pins when used as digital

View File

@@ -12,8 +12,8 @@
#include "FPGAEncoder.h"
#include "HAL/Counter.h"
#include "HAL/Errors.h"
#include "HAL/handles/LimitedClassedHandleResource.h"
#include "PortsInternal.h"
#include "handles/LimitedClassedHandleResource.h"
using namespace hal;

View File

@@ -8,8 +8,8 @@
#include "FPGAEncoder.h"
#include "DigitalInternal.h"
#include "HAL/handles/LimitedHandleResource.h"
#include "PortsInternal.h"
#include "handles/LimitedHandleResource.h"
using namespace hal;

View File

@@ -25,8 +25,8 @@
#include "HAL/Errors.h"
#include "HAL/cpp/priority_condition_variable.h"
#include "HAL/cpp/priority_mutex.h"
#include "HAL/handles/HandlesInternal.h"
#include "ctre/ctre.h"
#include "handles/HandlesInternal.h"
#include "visa/visa.h"
static tGlobal* global = nullptr;
@@ -113,6 +113,10 @@ const char* HAL_GetErrorMessage(int32_t code) {
return NO_AVAILABLE_RESOURCES_MESSAGE;
case RESOURCE_IS_ALLOCATED:
return RESOURCE_IS_ALLOCATED_MESSAGE;
case RESOURCE_OUT_OF_RANGE:
return RESOURCE_OUT_OF_RANGE_MESSAGE;
case HAL_INVALID_ACCUMULATOR_CHANNEL:
return HAL_INVALID_ACCUMULATOR_CHANNEL_MESSAGE;
case HAL_HANDLE_ERROR:
return HAL_HANDLE_ERROR_MESSAGE;
case NULL_PARAMETER:

View File

@@ -13,9 +13,9 @@
#include "DigitalInternal.h"
#include "HAL/Errors.h"
#include "HAL/handles/HandlesInternal.h"
#include "HAL/handles/LimitedHandleResource.h"
#include "PortsInternal.h"
#include "handles/HandlesInternal.h"
#include "handles/LimitedHandleResource.h"
using namespace hal;

View File

@@ -17,7 +17,7 @@
#include "ChipObject.h"
#include "HAL/HAL.h"
#include "HAL/cpp/priority_mutex.h"
#include "handles/UnlimitedHandleResource.h"
#include "HAL/handles/UnlimitedHandleResource.h"
static const uint32_t kTimerInterruptNumber = 28;

View File

@@ -16,7 +16,7 @@ PCM* PCM_modules[kNumPCMModules] = {nullptr};
void initializePCM(int32_t module, int32_t* status) {
if (!HAL_CheckSolenoidModule(module)) {
*status = PARAMETER_OUT_OF_RANGE;
*status = RESOURCE_OUT_OF_RANGE;
return;
}
if (!PCM_modules[module]) {

View File

@@ -20,7 +20,7 @@ extern "C" {
void HAL_InitializePDP(int32_t module, int32_t* status) {
if (!HAL_CheckPDPModule(module)) {
*status = PARAMETER_OUT_OF_RANGE;
*status = RESOURCE_OUT_OF_RANGE;
return;
}
if (!pdp[module]) {

View File

@@ -9,8 +9,8 @@
#include "ConstantsInternal.h"
#include "DigitalInternal.h"
#include "HAL/handles/HandlesInternal.h"
#include "PortsInternal.h"
#include "handles/HandlesInternal.h"
using namespace hal;

View File

@@ -8,8 +8,8 @@
#include "HAL/Relay.h"
#include "DigitalInternal.h"
#include "HAL/handles/IndexedHandleResource.h"
#include "PortsInternal.h"
#include "handles/IndexedHandleResource.h"
using namespace hal;

View File

@@ -11,11 +11,11 @@
#include "FRC_NetworkCommunication/LoadOut.h"
#include "HAL/Errors.h"
#include "HAL/Ports.h"
#include "HAL/handles/HandlesInternal.h"
#include "HAL/handles/IndexedHandleResource.h"
#include "PCMInternal.h"
#include "PortsInternal.h"
#include "ctre/PCM.h"
#include "handles/HandlesInternal.h"
#include "handles/IndexedHandleResource.h"
namespace {
struct Solenoid {
@@ -44,7 +44,7 @@ HAL_SolenoidHandle HAL_InitializeSolenoidPort(HAL_PortHandle port_handle,
// initializePCM will check the module
if (!HAL_CheckSolenoidPin(pin)) {
*status = PARAMETER_OUT_OF_RANGE;
*status = RESOURCE_OUT_OF_RANGE;
return HAL_kInvalidHandle;
}

View File

@@ -5,10 +5,7 @@
/* the project. */
/*----------------------------------------------------------------------------*/
#include "HandlesInternal.h"
#include "IndexedHandleResource.h"
#include "LimitedHandleResource.h"
#include "UnlimitedHandleResource.h"
#include "HAL/handles/HandlesInternal.h"
namespace hal {
HAL_PortHandle createPortHandle(uint8_t pin, uint8_t module) {

View File

@@ -38,7 +38,8 @@ AnalogInput::AnalogInput(uint32_t channel) {
int32_t status = 0;
m_port = HAL_InitializeAnalogInputPort(port, &status);
if (status != 0) {
wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
wpi_setErrorWithContextRange(status, 0, HAL_GetNumAnalogInputs(), channel,
HAL_GetErrorMessage(status));
m_channel = std::numeric_limits<uint32_t>::max();
m_port = HAL_kInvalidHandle;
return;

View File

@@ -37,7 +37,8 @@ AnalogOutput::AnalogOutput(uint32_t channel) {
int32_t status = 0;
m_port = HAL_InitializeAnalogOutputPort(port, &status);
if (status != 0) {
wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
wpi_setErrorWithContextRange(status, 0, HAL_GetNumAnalogOutputs(), channel,
HAL_GetErrorMessage(status));
m_channel = std::numeric_limits<uint32_t>::max();
m_port = HAL_kInvalidHandle;
return;

View File

@@ -19,7 +19,8 @@ Compressor::Compressor(uint8_t pcmID) : m_module(pcmID) {
int32_t status = 0;
m_compressorHandle = HAL_InitializeCompressor(m_module, &status);
if (status != 0) {
wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
wpi_setErrorWithContextRange(status, 0, HAL_GetNumPCMModules(), pcmID,
HAL_GetErrorMessage(status));
return;
}
SetClosedLoopControl(true);

View File

@@ -35,7 +35,8 @@ DigitalInput::DigitalInput(uint32_t channel) {
int32_t status = 0;
m_handle = HAL_InitializeDIOPort(HAL_GetPort(channel), true, &status);
if (status != 0) {
wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
wpi_setErrorWithContextRange(status, 0, HAL_GetNumDigitalPins(), channel,
HAL_GetErrorMessage(status));
m_handle = HAL_kInvalidHandle;
m_channel = std::numeric_limits<uint32_t>::max();
return;

View File

@@ -36,7 +36,8 @@ DigitalOutput::DigitalOutput(uint32_t channel) {
int32_t status = 0;
m_handle = HAL_InitializeDIOPort(HAL_GetPort(channel), false, &status);
if (status != 0) {
wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
wpi_setErrorWithContextRange(status, 0, HAL_GetNumDigitalPins(), channel,
HAL_GetErrorMessage(status));
m_channel = std::numeric_limits<uint32_t>::max();
m_handle = HAL_kInvalidHandle;
return;

View File

@@ -56,7 +56,8 @@ DoubleSolenoid::DoubleSolenoid(uint8_t moduleNumber, uint32_t forwardChannel,
m_forwardHandle = HAL_InitializeSolenoidPort(
HAL_GetPortWithModule(moduleNumber, m_forwardChannel), &status);
if (status != 0) {
wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
wpi_setErrorWithContextRange(status, 0, HAL_GetNumSolenoidPins(),
forwardChannel, HAL_GetErrorMessage(status));
m_forwardHandle = HAL_kInvalidHandle;
m_reverseHandle = HAL_kInvalidHandle;
return;
@@ -65,7 +66,8 @@ DoubleSolenoid::DoubleSolenoid(uint8_t moduleNumber, uint32_t forwardChannel,
m_reverseHandle = HAL_InitializeSolenoidPort(
HAL_GetPortWithModule(moduleNumber, m_reverseChannel), &status);
if (status != 0) {
wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
wpi_setErrorWithContextRange(status, 0, HAL_GetNumSolenoidPins(),
reverseChannel, HAL_GetErrorMessage(status));
// free forward solenoid
HAL_FreeSolenoidPort(m_forwardHandle);
m_forwardHandle = HAL_kInvalidHandle;

View File

@@ -35,7 +35,8 @@ PWM::PWM(uint32_t channel) {
int32_t status = 0;
m_handle = HAL_InitializePWMPort(HAL_GetPort(channel), &status);
if (status != 0) {
wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
wpi_setErrorWithContextRange(status, 0, HAL_GetNumPWMPins(), channel,
HAL_GetErrorMessage(status));
m_channel = std::numeric_limits<uint32_t>::max();
m_handle = HAL_kInvalidHandle;
return;

View File

@@ -22,7 +22,8 @@ PowerDistributionPanel::PowerDistributionPanel(uint8_t module)
int32_t status = 0;
HAL_InitializePDP(m_module, &status);
if (status != 0) {
wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
wpi_setErrorWithContextRange(status, 0, HAL_GetNumPDPModules(), module,
HAL_GetErrorMessage(status));
m_module = -1;
return;
}

View File

@@ -38,7 +38,8 @@ Relay::Relay(uint32_t channel, Relay::Direction direction)
int32_t status = 0;
m_forwardHandle = HAL_InitializeRelayPort(portHandle, true, &status);
if (status != 0) {
wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
wpi_setErrorWithContextRange(status, 0, HAL_GetNumRelayPins(), channel,
HAL_GetErrorMessage(status));
m_forwardHandle = HAL_kInvalidHandle;
m_reverseHandle = HAL_kInvalidHandle;
return;
@@ -49,7 +50,8 @@ Relay::Relay(uint32_t channel, Relay::Direction direction)
int32_t status = 0;
m_reverseHandle = HAL_InitializeRelayPort(portHandle, false, &status);
if (status != 0) {
wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
wpi_setErrorWithContextRange(status, 0, HAL_GetNumRelayPins(), channel,
HAL_GetErrorMessage(status));
m_forwardHandle = HAL_kInvalidHandle;
m_reverseHandle = HAL_kInvalidHandle;
return;

View File

@@ -44,7 +44,8 @@ Solenoid::Solenoid(uint8_t moduleNumber, uint32_t channel)
m_solenoidHandle = HAL_InitializeSolenoidPort(
HAL_GetPortWithModule(moduleNumber, channel), &status);
if (status != 0) {
wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
wpi_setErrorWithContextRange(status, 0, HAL_GetNumSolenoidPins(), channel,
HAL_GetErrorMessage(status));
m_solenoidHandle = HAL_kInvalidHandle;
return;
}

View File

@@ -26,6 +26,12 @@
if ((code) != 0) \
this->SetError((code), (context), __FILE__, __FUNCTION__, __LINE__); \
} while (0)
#define wpi_setErrorWithContextRange(code, min, max, req, context) \
do { \
if ((code) != 0) \
this->SetErrorRange((code), (min), (max), (req), (context), __FILE__, \
__FUNCTION__, __LINE__); \
} while (0)
#define wpi_setError(code) wpi_setErrorWithContext(code, "")
#define wpi_setStaticErrorWithContext(object, code, context) \
do { \
@@ -82,6 +88,11 @@ class ErrorBase {
virtual void SetError(Error::Code code, llvm::StringRef contextMessage,
llvm::StringRef filename, llvm::StringRef function,
uint32_t lineNumber) const;
virtual void SetErrorRange(Error::Code code, int32_t minRange,
int32_t maxRange, int32_t requestedValue,
llvm::StringRef contextMessage,
llvm::StringRef filename, llvm::StringRef function,
uint32_t lineNumber) const;
virtual void SetWPIError(llvm::StringRef errorMessage, Error::Code code,
llvm::StringRef contextMessage,
llvm::StringRef filename, llvm::StringRef function,

View File

@@ -119,6 +119,44 @@ void ErrorBase::SetError(Error::Code code, llvm::StringRef contextMessage,
}
}
/**
* @brief Set the current error information associated with this sensor.
* Range versions use for initialization code.
*
* @param code The error code
* @param minRange The minimum allowed allocation range
* @param maxRange The maximum allowed allocation range
* @param requestedValue The requested value to allocate
* @param contextMessage A custom message from the code that set the error.
* @param filename Filename of the error source
* @param function Function of the error source
* @param lineNumber Line number of the error source
*/
void ErrorBase::SetErrorRange(Error::Code code, int32_t minRange,
int32_t maxRange, int32_t requestedValue,
llvm::StringRef contextMessage,
llvm::StringRef filename,
llvm::StringRef function,
uint32_t lineNumber) const {
// If there was an error
if (code != 0) {
size_t size = contextMessage.size() + 100;
char* buf = new char[size];
snprintf(buf, size,
"%s, Minimum Value: %d, Maximum Value: %d, Requested Value: %d",
contextMessage.data(), minRange, maxRange, requestedValue);
// Set the current error information for this object.
m_error.Set(code, buf, filename, function, lineNumber, this);
delete[] buf;
// Update the global error if there is not one already set.
std::lock_guard<priority_mutex> mutex(_globalErrorMutex);
if (_globalError.GetCode() == 0) {
_globalError.Clone(m_error);
}
}
}
/**
* @brief Set the current error information associated with this sensor.
*

View File

@@ -13,6 +13,7 @@
#include "HAL/AnalogGyro.h"
#include "HALUtil.h"
#include "HAL/handles/HandlesInternal.h"
// set the logging level
TLogLevel analogGyroJNILogLevel = logWARNING;
@@ -37,6 +38,8 @@ JNIEXPORT jint JNICALL Java_edu_wpi_first_wpilibj_hal_AnalogGyroJNI_initializeAn
HAL_GyroHandle handle = HAL_InitializeAnalogGyro((HAL_AnalogInputHandle)id, &status);
ANALOGGYROJNI_LOG(logDEBUG) << "Status = " << status;
ANALOGGYROJNI_LOG(logDEBUG) << "Gyro Handle = " << handle;
// Analog input does range checking, so we don't need to do so.
CheckStatus(env, status);
return (jint) handle;
}

View File

@@ -15,7 +15,9 @@
#include "HAL/AnalogOutput.h"
#include "HAL/AnalogAccumulator.h"
#include "HAL/AnalogTrigger.h"
#include "HAL/Ports.h"
#include "HALUtil.h"
#include "HAL/handles/HandlesInternal.h"
// set the logging level
TLogLevel analogJNILogLevel = logWARNING;
@@ -41,7 +43,8 @@ Java_edu_wpi_first_wpilibj_hal_AnalogJNI_initializeAnalogInputPort(
auto analog = HAL_InitializeAnalogInputPort((HAL_PortHandle)id, &status);
ANALOGJNI_LOG(logDEBUG) << "Status = " << status;
ANALOGJNI_LOG(logDEBUG) << "Analog Handle = " << analog;
CheckStatus(env, status);
CheckStatusRange(env, 0, HAL_GetNumAnalogInputs(),
hal::getPortHandlePin((HAL_PortHandle)id), status);
return (jint)analog;
}
@@ -70,7 +73,8 @@ Java_edu_wpi_first_wpilibj_hal_AnalogJNI_initializeAnalogOutputPort(
HAL_AnalogOutputHandle analog = HAL_InitializeAnalogOutputPort((HAL_PortHandle)id, &status);
ANALOGJNI_LOG(logDEBUG) << "Status = " << status;
ANALOGJNI_LOG(logDEBUG) << "Analog Handle = " << analog;
CheckStatus(env, status);
CheckStatusRange(env, 0, HAL_GetNumAnalogOutputs(),
hal::getPortHandlePin((HAL_PortHandle)id), status);
return (jlong)analog;
}

View File

@@ -17,7 +17,7 @@
extern "C" {
inline bool CheckCTRStatus(JNIEnv *env, CTR_Code status) {
if (status != CTR_OKAY) ReportError(env, (int32_t)status, false);
if (status != CTR_OKAY) ReportError(env, (int32_t)status, 0, 0, 0, false);
return status == CTR_OKAY;
}

View File

@@ -22,9 +22,7 @@ Java_edu_wpi_first_wpilibj_hal_CompressorJNI_initializeCompressor(
JNIEnv *env, jclass, jbyte module) {
int32_t status = 0;
auto handle = HAL_InitializeCompressor(module, &status);
if (status == PARAMETER_OUT_OF_RANGE) {
ThrowBoundaryException(env, module, 0, HAL_GetNumPCMModules());
}
CheckStatusRange(env, 0, HAL_GetNumPCMModules(), module, status);
return (jint)handle;
}

View File

@@ -14,6 +14,8 @@
#include "HAL/DIO.h"
#include "HAL/PWM.h"
#include "HALUtil.h"
#include "HAL/Ports.h"
#include "HAL/handles/HandlesInternal.h"
// set the logging level
TLogLevel dioJNILogLevel = logWARNING;
@@ -41,7 +43,8 @@ Java_edu_wpi_first_wpilibj_hal_DIOJNI_initializeDIOPort(
auto dio = HAL_InitializeDIOPort((HAL_PortHandle)id, (uint8_t)input, &status);
DIOJNI_LOG(logDEBUG) << "Status = " << status;
DIOJNI_LOG(logDEBUG) << "DIO Handle = " << dio;
CheckStatus(env, status);
CheckStatusRange(env, 0, HAL_GetNumDigitalPins(),
hal::getPortHandlePin((HAL_PortHandle)id), status);
return (jint)dio;
}

View File

@@ -120,10 +120,13 @@ static void GetStackTrace(JNIEnv *env, std::string &res, std::string &func) {
env->DeleteLocalRef(stackTrace);
}
void ThrowAllocationException(JNIEnv *env, int32_t status) {
void ThrowAllocationException(JNIEnv *env, int32_t minRange, int32_t maxRange,
int32_t requestedValue, int32_t status) {
const char *message = HAL_GetErrorMessage(status);
char *buf = new char[strlen(message) + 30];
sprintf(buf, " Code: $%d. %s", status, message);
char *buf = new char[strlen(message) + 100];
sprintf(buf,
" Code: $%d. %s, Minimum Value: %d, Maximum Value: %d, Requested Value: %d",
status, message, minRange, maxRange, requestedValue);
env->ThrowNew(allocationExCls, buf);
delete[] buf;
}
@@ -136,11 +139,13 @@ void ThrowHalHandleException(JNIEnv *env, int32_t status) {
delete[] buf;
}
void ReportError(JNIEnv *env, int32_t status, bool do_throw) {
void ReportError(JNIEnv *env, int32_t status, int32_t minRange, int32_t maxRange,
int32_t requestedValue, bool do_throw) {
if (status == 0) return;
if (status == NO_AVAILABLE_RESOURCES ||
status == RESOURCE_IS_ALLOCATED) {
ThrowAllocationException(env, status);
status == RESOURCE_IS_ALLOCATED ||
status == RESOURCE_OUT_OF_RANGE) {
ThrowAllocationException(env, minRange, maxRange, requestedValue, status);
}
if (status == HAL_HANDLE_ERROR) {
ThrowHalHandleException(env, status);

View File

@@ -14,10 +14,19 @@
extern JavaVM *jvm;
void ReportError(JNIEnv *env, int32_t status, bool do_throw = true);
void ReportError(JNIEnv *env, int32_t status, int32_t minRange, int32_t maxRange,
int32_t requestedValue, bool do_throw = true);
inline bool CheckStatus(JNIEnv *env, int32_t status, bool do_throw = true) {
if (status != 0) ReportError(env, status, do_throw);
if (status != 0) ReportError(env, status, 0, 0, 0, do_throw);
return status == 0;
}
inline bool CheckStatusRange(JNIEnv *env, int32_t status, int32_t minRange,
int32_t maxRange, int32_t requestedValue,
bool do_throw = true) {
if (status != 0) ReportError(env, status, minRange, maxRange, requestedValue,
do_throw);
return status == 0;
}

View File

@@ -6,6 +6,7 @@
/*----------------------------------------------------------------------------*/
#include "HAL/PDP.h"
#include "HAL/Ports.h"
#include "HALUtil.h"
#include "edu_wpi_first_wpilibj_hal_PDPJNI.h"
@@ -20,7 +21,7 @@ JNIEXPORT void JNICALL Java_edu_wpi_first_wpilibj_hal_PDPJNI_initializePDP(
JNIEnv *env, jclass, jint module) {
int32_t status = 0;
HAL_InitializePDP(module, &status);
CheckStatus(env, status);
CheckStatusRange(env, 0, HAL_GetNumPDPModules(), module, status);
}
/*

View File

@@ -13,7 +13,9 @@
#include "HAL/DIO.h"
#include "HAL/PWM.h"
#include "HAL/Ports.h"
#include "HALUtil.h"
#include "HAL/handles/HandlesInternal.h"
// set the logging level
TLogLevel pwmJNILogLevel = logWARNING;
@@ -40,7 +42,8 @@ Java_edu_wpi_first_wpilibj_hal_PWMJNI_initializePWMPort(
auto pwm = HAL_InitializePWMPort((HAL_PortHandle)id, &status);
PWMJNI_LOG(logDEBUG) << "Status = " << status;
PWMJNI_LOG(logDEBUG) << "PWM Handle = " << pwm;
CheckStatus(env, status);
CheckStatusRange(env, 0, HAL_GetNumPWMPins(),
hal::getPortHandlePin((HAL_PortHandle)id), status);
return (jint)pwm;
}

View File

@@ -12,7 +12,9 @@
#include "edu_wpi_first_wpilibj_hal_RelayJNI.h"
#include "HAL/Relay.h"
#include "HAL/Ports.h"
#include "HALUtil.h"
#include "HAL/handles/HandlesInternal.h"
// set the logging level
TLogLevel relayJNILogLevel = logWARNING;
@@ -39,6 +41,8 @@ JNIEXPORT jint JNICALL Java_edu_wpi_first_wpilibj_hal_RelayJNI_initializeRelayPo
HAL_RelayHandle handle = HAL_InitializeRelayPort((HAL_PortHandle)id, (uint8_t) fwd, &status);
RELAYJNI_LOG(logDEBUG) << "Status = " << status;
RELAYJNI_LOG(logDEBUG) << "Relay Handle = " << handle;
CheckStatusRange(env, 0, HAL_GetNumRelayPins(),
hal::getPortHandlePin((HAL_PortHandle)id), status);
return (jint) handle;
}

View File

@@ -7,6 +7,7 @@
#include <jni.h>
#include "HAL/HAL.h"
#include "HAL/handles/HandlesInternal.h"
#include "Log.h"
#include "edu_wpi_first_wpilibj_hal_SolenoidJNI.h"
@@ -30,19 +31,21 @@ extern "C" {
*/
JNIEXPORT jint JNICALL
Java_edu_wpi_first_wpilibj_hal_SolenoidJNI_initializeSolenoidPort(
JNIEnv *env, jclass, jint port_handle) {
JNIEnv *env, jclass, jint id) {
SOLENOIDJNI_LOG(logDEBUG) << "Calling SolenoidJNI initializeSolenoidPort";
SOLENOIDJNI_LOG(logDEBUG) << "Port Handle = " << (HAL_PortHandle)port_handle;
SOLENOIDJNI_LOG(logDEBUG) << "Port Handle = " << (HAL_PortHandle)id;
int32_t status = 0;
HAL_SolenoidHandle handle =
HAL_InitializeSolenoidPort((HAL_PortHandle)port_handle, &status);
HAL_InitializeSolenoidPort((HAL_PortHandle)id, &status);
SOLENOIDJNI_LOG(logDEBUG) << "Status = " << status;
SOLENOIDJNI_LOG(logDEBUG) << "Solenoid Port Handle = " << handle;
CheckStatus(env, status);
// Use solenoid pins, as we have to pick one.
CheckStatusRange(env, 0, HAL_GetNumSolenoidPins(),
hal::getPortHandlePin((HAL_PortHandle)id), status);;
return (jint)handle;
}