[hal] Use last error reporting instead of PARAMETER_OUT_OF_RANGE (#3328)

Makes the error messages much more specific to each error.
This commit is contained in:
Thad House
2021-05-01 13:22:08 -07:00
committed by GitHub
parent 23d2326d1d
commit 3c08461685
15 changed files with 89 additions and 21 deletions

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -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);
}

View File

@@ -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;
}

View File

@@ -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<int>(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) {

View File

@@ -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);
}

View File

@@ -7,6 +7,7 @@
#include <wpi/mutex.h>
#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;
}

View File

@@ -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<int>(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<int>(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<int>(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<int>(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<int>(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<int>(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;
}

View File

@@ -19,6 +19,7 @@
#include <string>
#include <thread>
#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<speed_t>(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;
}

View File

@@ -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);
}

View File

@@ -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);
}

View File

@@ -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);

View File

@@ -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;

View File

@@ -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();