diff --git a/hal/src/main/native/athena/AddressableLED.cpp b/hal/src/main/native/athena/AddressableLED.cpp index 39db71c039..864c08ce07 100644 --- a/hal/src/main/native/athena/AddressableLED.cpp +++ b/hal/src/main/native/athena/AddressableLED.cpp @@ -11,6 +11,7 @@ #include "ConstantsInternal.h" #include "DigitalInternal.h" #include "HALInitializer.h" +#include "HALInternal.h" #include "PortsInternal.h" #include "hal/AddressableLEDTypes.h" #include "hal/ChipObject.h" @@ -142,8 +143,11 @@ void HAL_SetAddressableLEDLength(HAL_AddressableLEDHandle handle, return; } - if (length > HAL_kAddressableLEDMaxLength) { + if (length > HAL_kAddressableLEDMaxLength || length < 0) { *status = PARAMETER_OUT_OF_RANGE; + hal::SetLastError(status, "LED length must be less than or equal to " + + wpi::Twine(HAL_kAddressableLEDMaxLength) + + ". " + wpi::Twine(length) + " was requested"); return; } @@ -173,8 +177,11 @@ void HAL_WriteAddressableLEDData(HAL_AddressableLEDHandle handle, return; } - if (length > led->stringLength) { + if (length > led->stringLength || length < 0) { *status = PARAMETER_OUT_OF_RANGE; + hal::SetLastError(status, "Data length must be less than or equal to " + + wpi::Twine(led->stringLength) + ". " + + wpi::Twine(length) + " was requested"); return; } diff --git a/hal/src/main/native/athena/AnalogTrigger.cpp b/hal/src/main/native/athena/AnalogTrigger.cpp index 7d438572e8..d9e2b9251c 100644 --- a/hal/src/main/native/athena/AnalogTrigger.cpp +++ b/hal/src/main/native/athena/AnalogTrigger.cpp @@ -7,6 +7,7 @@ #include "AnalogInternal.h" #include "DutyCycleInternal.h" #include "HALInitializer.h" +#include "HALInternal.h" #include "PortsInternal.h" #include "hal/AnalogInput.h" #include "hal/DutyCycle.h" @@ -138,6 +139,11 @@ void HAL_SetAnalogTriggerLimitsDutyCycle( if (lower < 0.0 || upper > 1.0) { *status = PARAMETER_OUT_OF_RANGE; + auto lowerStr = std::to_string(lower); + auto upperStr = std::to_string(upper); + hal::SetLastError( + status, "Lower must be >= 0 and upper must be <=1. Requested lower " + + lowerStr + " Requested upper " + upperStr); return; } diff --git a/hal/src/main/native/athena/Counter.cpp b/hal/src/main/native/athena/Counter.cpp index 2605279ed3..d6a278ce74 100644 --- a/hal/src/main/native/athena/Counter.cpp +++ b/hal/src/main/native/athena/Counter.cpp @@ -7,6 +7,7 @@ #include "ConstantsInternal.h" #include "DigitalInternal.h" #include "HALInitializer.h" +#include "HALInternal.h" #include "PortsInternal.h" #include "hal/HAL.h" #include "hal/handles/LimitedHandleResource.h" @@ -146,6 +147,9 @@ void HAL_SetCounterDownSource(HAL_CounterHandle counterHandle, // TODO: wpi_setWPIErrorWithContext(ParameterOutOfRange, "Counter only // supports DownSource in TwoPulse and ExternalDirection modes."); *status = PARAMETER_OUT_OF_RANGE; + hal::SetLastError(status, + "Counter only supports DownSource in TwoPulse and " + "ExternalDirection mode."); return; } @@ -260,6 +264,11 @@ void HAL_SetCounterSamplesToAverage(HAL_CounterHandle counterHandle, } if (samplesToAverage < 1 || samplesToAverage > 127) { *status = PARAMETER_OUT_OF_RANGE; + hal::SetLastError( + status, + "Samples to average must be between 1 and 127 inclusive. Requested " + + wpi::Twine(samplesToAverage)); + return; } counter->counter->writeTimerConfig_AverageSize(samplesToAverage, status); } diff --git a/hal/src/main/native/athena/DMA.cpp b/hal/src/main/native/athena/DMA.cpp index 31ca1c27bc..40eaca6f4b 100644 --- a/hal/src/main/native/athena/DMA.cpp +++ b/hal/src/main/native/athena/DMA.cpp @@ -13,6 +13,7 @@ #include "AnalogInternal.h" #include "DigitalInternal.h" #include "EncoderInternal.h" +#include "HALInternal.h" #include "PortsInternal.h" #include "hal/AnalogAccumulator.h" #include "hal/AnalogGyro.h" @@ -528,6 +529,9 @@ void HAL_SetDMAExternalTrigger(HAL_DMAHandle handle, if (!success) { *status = PARAMETER_OUT_OF_RANGE; + hal::SetLastError(status, + "Digital Source unabled to be mapped properly. Likely " + "invalid handle passed."); return; } diff --git a/hal/src/main/native/athena/Encoder.cpp b/hal/src/main/native/athena/Encoder.cpp index e63eede3d1..10556844f6 100644 --- a/hal/src/main/native/athena/Encoder.cpp +++ b/hal/src/main/native/athena/Encoder.cpp @@ -7,6 +7,7 @@ #include "EncoderInternal.h" #include "FPGAEncoder.h" #include "HALInitializer.h" +#include "HALInternal.h" #include "PortsInternal.h" #include "hal/ChipObject.h" #include "hal/Counter.h" @@ -46,6 +47,9 @@ Encoder::Encoder(HAL_Handle digitalSourceHandleA, } default: *status = PARAMETER_OUT_OF_RANGE; + hal::SetLastError(status, "Encoding type " + + wpi::Twine(static_cast(encodingType)) + + " invalid."); return; } } @@ -181,6 +185,10 @@ void Encoder::SetReverseDirection(bool reverseDirection, int32_t* status) { void Encoder::SetSamplesToAverage(int32_t samplesToAverage, int32_t* status) { if (samplesToAverage < 1 || samplesToAverage > 127) { *status = PARAMETER_OUT_OF_RANGE; + hal::SetLastError( + status, + "Samples to average must be between 1 and 127 inclusive. Requested " + + wpi::Twine(samplesToAverage)); return; } if (m_counter) { diff --git a/hal/src/main/native/athena/FPGAEncoder.cpp b/hal/src/main/native/athena/FPGAEncoder.cpp index 8709139924..3bfe9d9a41 100644 --- a/hal/src/main/native/athena/FPGAEncoder.cpp +++ b/hal/src/main/native/athena/FPGAEncoder.cpp @@ -8,6 +8,7 @@ #include "DigitalInternal.h" #include "HALInitializer.h" +#include "HALInternal.h" #include "PortsInternal.h" #include "hal/handles/LimitedHandleResource.h" @@ -193,6 +194,11 @@ void HAL_SetFPGAEncoderSamplesToAverage(HAL_FPGAEncoderHandle fpgaEncoderHandle, } if (samplesToAverage < 1 || samplesToAverage > 127) { *status = PARAMETER_OUT_OF_RANGE; + hal::SetLastError( + status, + "Samples to average must be between 1 and 127 inclusive. Requested " + + wpi::Twine(samplesToAverage)); + return; } encoder->encoder->writeTimerConfig_AverageSize(samplesToAverage, status); } diff --git a/hal/src/main/native/athena/PDP.cpp b/hal/src/main/native/athena/PDP.cpp index 7db4dd2db5..0cdf30f831 100644 --- a/hal/src/main/native/athena/PDP.cpp +++ b/hal/src/main/native/athena/PDP.cpp @@ -7,6 +7,7 @@ #include #include "HALInitializer.h" +#include "HALInternal.h" #include "PortsInternal.h" #include "hal/CANAPI.h" #include "hal/Errors.h" @@ -117,6 +118,7 @@ HAL_PDPHandle HAL_InitializePDP(int32_t module, int32_t* status) { hal::init::CheckInit(); if (!HAL_CheckPDPModule(module)) { *status = PARAMETER_OUT_OF_RANGE; + hal::SetLastError(status, "Invalid pdp module " + wpi::Twine(module)); return HAL_kInvalidHandle; } @@ -192,6 +194,7 @@ double HAL_GetPDPChannelCurrent(HAL_PDPHandle handle, int32_t channel, int32_t* status) { if (!HAL_CheckPDPChannel(channel)) { *status = PARAMETER_OUT_OF_RANGE; + hal::SetLastError(status, "Invalid pdp channel " + wpi::Twine(channel)); return 0; } diff --git a/hal/src/main/native/athena/SPI.cpp b/hal/src/main/native/athena/SPI.cpp index 43843ebfed..090c53378e 100644 --- a/hal/src/main/native/athena/SPI.cpp +++ b/hal/src/main/native/athena/SPI.cpp @@ -18,6 +18,7 @@ #include "DigitalInternal.h" #include "HALInitializer.h" +#include "HALInternal.h" #include "hal/DIO.h" #include "hal/HAL.h" #include "hal/handles/HandlesInternal.h" @@ -103,6 +104,9 @@ void HAL_InitializeSPI(HAL_SPIPort port, int32_t* status) { hal::init::CheckInit(); if (port < 0 || port >= kSpiMaxHandles) { *status = PARAMETER_OUT_OF_RANGE; + hal::SetLastError(status, "Serial port must be between 0 and " + + wpi::Twine(kSpiMaxHandles) + ". Requested " + + wpi::Twine(static_cast(port))); return; } @@ -246,6 +250,8 @@ void HAL_InitializeSPI(HAL_SPIPort port, int32_t* status) { break; default: *status = PARAMETER_OUT_OF_RANGE; + hal::SetLastError( + status, "Invalid SPI port " + wpi::Twine(static_cast(port))); break; } } @@ -374,6 +380,9 @@ void HAL_SetSPIOpts(HAL_SPIPort port, HAL_Bool msbFirst, void HAL_SetSPIChipSelectActiveHigh(HAL_SPIPort port, int32_t* status) { if (port < 0 || port >= kSpiMaxHandles) { *status = PARAMETER_OUT_OF_RANGE; + hal::SetLastError(status, "Serial port must be between 0 and " + + wpi::Twine(kSpiMaxHandles) + ". Requested " + + wpi::Twine(static_cast(port))); return; } @@ -389,6 +398,9 @@ void HAL_SetSPIChipSelectActiveHigh(HAL_SPIPort port, int32_t* status) { void HAL_SetSPIChipSelectActiveLow(HAL_SPIPort port, int32_t* status) { if (port < 0 || port >= kSpiMaxHandles) { *status = PARAMETER_OUT_OF_RANGE; + hal::SetLastError(status, "Serial port must be between 0 and " + + wpi::Twine(kSpiMaxHandles) + ". Requested " + + wpi::Twine(static_cast(port))); return; } @@ -453,6 +465,9 @@ void HAL_SetSPIHandle(HAL_SPIPort port, int32_t handle) { void HAL_InitSPIAuto(HAL_SPIPort port, int32_t bufferSize, int32_t* status) { if (port < 0 || port >= kSpiMaxHandles) { *status = PARAMETER_OUT_OF_RANGE; + hal::SetLastError(status, "Serial port must be between 0 and " + + wpi::Twine(kSpiMaxHandles) + ". Requested " + + wpi::Twine(static_cast(port))); return; } @@ -484,6 +499,9 @@ void HAL_InitSPIAuto(HAL_SPIPort port, int32_t bufferSize, int32_t* status) { void HAL_FreeSPIAuto(HAL_SPIPort port, int32_t* status) { if (port < 0 || port >= kSpiMaxHandles) { *status = PARAMETER_OUT_OF_RANGE; + hal::SetLastError(status, "Serial port must be between 0 and " + + wpi::Twine(kSpiMaxHandles) + ". Requested " + + wpi::Twine(static_cast(port))); return; } @@ -586,11 +604,17 @@ void HAL_SetSPIAutoTransmitData(HAL_SPIPort port, const uint8_t* dataToSend, int32_t* status) { if (dataSize < 0 || dataSize > 32) { *status = PARAMETER_OUT_OF_RANGE; + hal::SetLastError( + status, "Data size must be between 0 and 32 inclusive. Requested " + + wpi::Twine(dataSize)); return; } if (zeroSize < 0 || zeroSize > 127) { *status = PARAMETER_OUT_OF_RANGE; + hal::SetLastError( + status, "Zero size must be between 0 and 127 inclusive. Requested " + + wpi::Twine(zeroSize)); return; } diff --git a/hal/src/main/native/athena/SerialPort.cpp b/hal/src/main/native/athena/SerialPort.cpp index cf34d7b5c0..dfacbecc7c 100644 --- a/hal/src/main/native/athena/SerialPort.cpp +++ b/hal/src/main/native/athena/SerialPort.cpp @@ -19,6 +19,7 @@ #include #include +#include "HALInternal.h" #include "hal/cpp/SerialHelper.h" #include "hal/handles/HandlesInternal.h" #include "hal/handles/IndexedHandleResource.h" @@ -183,6 +184,7 @@ void HAL_SetSerialBaudRate(HAL_SerialPortHandle handle, int32_t baud, BAUDCASE(4000000) default: *status = PARAMETER_OUT_OF_RANGE; + hal::SetLastError(status, "Invalid BaudRate: " + wpi::Twine(baud)); return; } int err = cfsetospeed(&port->tty, static_cast(port->baudRate)); @@ -225,6 +227,7 @@ void HAL_SetSerialDataBits(HAL_SerialPortHandle handle, int32_t bits, break; default: *status = PARAMETER_OUT_OF_RANGE; + hal::SetLastError(status, "Invalid data bits: " + wpi::Twine(bits)); return; } @@ -272,6 +275,7 @@ void HAL_SetSerialParity(HAL_SerialPortHandle handle, int32_t parity, break; default: *status = PARAMETER_OUT_OF_RANGE; + hal::SetLastError(status, "Invalid parity bits: " + wpi::Twine(parity)); return; } @@ -299,6 +303,7 @@ void HAL_SetSerialStopBits(HAL_SerialPortHandle handle, int32_t stopBits, break; default: *status = PARAMETER_OUT_OF_RANGE; + hal::SetLastError(status, "Invalid stop bits: " + wpi::Twine(stopBits)); return; } @@ -334,6 +339,7 @@ void HAL_SetSerialFlowControl(HAL_SerialPortHandle handle, int32_t flow, break; default: *status = PARAMETER_OUT_OF_RANGE; + hal::SetLastError(status, "Invalid fc bits: " + wpi::Twine(flow)); return; } diff --git a/hal/src/main/native/cpp/jni/CounterJNI.cpp b/hal/src/main/native/cpp/jni/CounterJNI.cpp index 07718a6cd5..0565d5b847 100644 --- a/hal/src/main/native/cpp/jni/CounterJNI.cpp +++ b/hal/src/main/native/cpp/jni/CounterJNI.cpp @@ -119,12 +119,6 @@ Java_edu_wpi_first_hal_CounterJNI_setCounterDownSource HAL_SetCounterDownSource((HAL_CounterHandle)id, (HAL_Handle)digitalSourceHandle, (HAL_AnalogTriggerType)analogTriggerType, &status); - if (status == PARAMETER_OUT_OF_RANGE) { - ThrowIllegalArgumentException(env, - "Counter only supports DownSource in " - "TwoPulse and ExternalDirection modes."); - return; - } CheckStatus(env, status); } @@ -240,10 +234,6 @@ Java_edu_wpi_first_hal_CounterJNI_setCounterSamplesToAverage { int32_t status = 0; HAL_SetCounterSamplesToAverage((HAL_CounterHandle)id, value, &status); - if (status == PARAMETER_OUT_OF_RANGE) { - ThrowBoundaryException(env, value, 1, 127); - return; - } CheckStatus(env, status); } diff --git a/hal/src/main/native/cpp/jni/EncoderJNI.cpp b/hal/src/main/native/cpp/jni/EncoderJNI.cpp index 80673ab49e..f93e064ac4 100644 --- a/hal/src/main/native/cpp/jni/EncoderJNI.cpp +++ b/hal/src/main/native/cpp/jni/EncoderJNI.cpp @@ -267,10 +267,6 @@ Java_edu_wpi_first_hal_EncoderJNI_setEncoderSamplesToAverage { int32_t status = 0; HAL_SetEncoderSamplesToAverage((HAL_EncoderHandle)id, value, &status); - if (status == PARAMETER_OUT_OF_RANGE) { - ThrowBoundaryException(env, value, 1, 127); - return; - } CheckStatus(env, status); } diff --git a/hal/src/main/native/sim/AddressableLED.cpp b/hal/src/main/native/sim/AddressableLED.cpp index b4a3ed80ae..2ddc38271a 100644 --- a/hal/src/main/native/sim/AddressableLED.cpp +++ b/hal/src/main/native/sim/AddressableLED.cpp @@ -6,6 +6,7 @@ #include "DigitalInternal.h" #include "HALInitializer.h" +#include "HALInternal.h" #include "PortsInternal.h" #include "hal/Errors.h" #include "hal/handles/HandlesInternal.h" @@ -110,8 +111,11 @@ void HAL_SetAddressableLEDLength(HAL_AddressableLEDHandle handle, *status = HAL_HANDLE_ERROR; return; } - if (length > HAL_kAddressableLEDMaxLength) { + if (length > HAL_kAddressableLEDMaxLength || length < 0) { *status = PARAMETER_OUT_OF_RANGE; + hal::SetLastError(status, "LED length must be less than or equal to " + + wpi::Twine(HAL_kAddressableLEDMaxLength) + + ". " + wpi::Twine(length) + " was requested"); return; } SimAddressableLEDData[led->index].length = length; @@ -127,6 +131,10 @@ void HAL_WriteAddressableLEDData(HAL_AddressableLEDHandle handle, } if (length > SimAddressableLEDData[led->index].length) { *status = PARAMETER_OUT_OF_RANGE; + hal::SetLastError(status, + "Data length must be less than or equal to " + + wpi::Twine(SimAddressableLEDData[led->index].length) + + ". " + wpi::Twine(length) + " was requested"); return; } SimAddressableLEDData[led->index].SetData(data, length); diff --git a/hal/src/main/native/sim/Encoder.cpp b/hal/src/main/native/sim/Encoder.cpp index 765288caf6..f34f651686 100644 --- a/hal/src/main/native/sim/Encoder.cpp +++ b/hal/src/main/native/sim/Encoder.cpp @@ -6,6 +6,7 @@ #include "CounterInternal.h" #include "HALInitializer.h" +#include "HALInternal.h" #include "PortsInternal.h" #include "hal/Errors.h" #include "hal/handles/HandlesInternal.h" @@ -246,6 +247,7 @@ void HAL_SetEncoderMinRate(HAL_EncoderHandle encoderHandle, double minRate, if (minRate == 0.0) { *status = PARAMETER_OUT_OF_RANGE; + hal::SetLastError(status, "minRate must not be 0"); return; } @@ -262,6 +264,7 @@ void HAL_SetEncoderDistancePerPulse(HAL_EncoderHandle encoderHandle, if (distancePerPulse == 0.0) { *status = PARAMETER_OUT_OF_RANGE; + hal::SetLastError(status, "distancePerPulse must not be 0"); return; } encoder->distancePerPulse = distancePerPulse; diff --git a/hal/src/main/native/sim/PDP.cpp b/hal/src/main/native/sim/PDP.cpp index 36fb50e45d..aa08ca0dfa 100644 --- a/hal/src/main/native/sim/PDP.cpp +++ b/hal/src/main/native/sim/PDP.cpp @@ -6,6 +6,7 @@ #include "CANAPIInternal.h" #include "HALInitializer.h" +#include "HALInternal.h" #include "PortsInternal.h" #include "hal/CANAPI.h" #include "hal/Errors.h" @@ -27,6 +28,7 @@ extern "C" { HAL_PDPHandle HAL_InitializePDP(int32_t module, int32_t* status) { if (!HAL_CheckPDPModule(module)) { *status = PARAMETER_OUT_OF_RANGE; + hal::SetLastError(status, "Invalid pdp module " + wpi::Twine(module)); return HAL_kInvalidHandle; } hal::init::CheckInit(); diff --git a/wpilibc/src/main/native/cpp/AnalogGyro.cpp b/wpilibc/src/main/native/cpp/AnalogGyro.cpp index cdbf0e0254..47a9429855 100644 --- a/wpilibc/src/main/native/cpp/AnalogGyro.cpp +++ b/wpilibc/src/main/native/cpp/AnalogGyro.cpp @@ -115,10 +115,6 @@ void AnalogGyro::InitGyro() { std::string stackTrace = wpi::GetStackTrace(1); m_gyroHandle = HAL_InitializeAnalogGyro(m_analog->m_port, stackTrace.c_str(), &status); - if (status == PARAMETER_OUT_OF_RANGE) { - throw FRC_MakeError(err::ParameterOutOfRange, - "channel must be accumulator channel"); - } FRC_CheckErrorStatus(status, "InitializeAnalogGyro"); }