[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

@@ -4,12 +4,14 @@
#include "hal/AnalogGyro.h"
#include <string>
#include <thread>
#include <wpi/raw_ostream.h>
#include "AnalogInternal.h"
#include "HALInitializer.h"
#include "HALInternal.h"
#include "hal/AnalogAccumulator.h"
#include "hal/AnalogInput.h"
#include "hal/handles/IndexedHandleResource.h"
@@ -21,6 +23,7 @@ struct AnalogGyro {
double voltsPerDegreePerSecond;
double offset;
int32_t center;
std::string previousAllocation;
};
} // namespace
@@ -55,36 +58,43 @@ static void Wait(double seconds) {
extern "C" {
HAL_GyroHandle HAL_InitializeAnalogGyro(HAL_AnalogInputHandle analogHandle,
const char* allocationLocation,
int32_t* status) {
hal::init::CheckInit();
// Handle will be type checked by HAL_IsAccumulatorChannel
int16_t channel = getHandleIndex(analogHandle);
if (!HAL_IsAccumulatorChannel(analogHandle, status)) {
if (*status == 0) {
*status = HAL_INVALID_ACCUMULATOR_CHANNEL;
hal::SetLastErrorIndexOutOfRange(status, "Invalid Index for Analog Gyro",
0, kNumAccumulators, channel);
}
return HAL_kInvalidHandle;
}
// handle known to be correct, so no need to type check
int16_t channel = getHandleIndex(analogHandle);
auto handle = analogGyroHandles->Allocate(channel, status);
HAL_GyroHandle handle;
auto gyro = analogGyroHandles->Allocate(channel, &handle, status);
if (*status != 0) {
if (gyro) {
hal::SetLastErrorPreviouslyAllocated(status, "Analog Gyro", channel,
gyro->previousAllocation);
} else {
hal::SetLastErrorIndexOutOfRange(status, "Invalid Index for Analog Gyro",
0, kNumAccumulators, channel);
}
return HAL_kInvalidHandle; // failed to allocate. Pass error back.
}
// Initialize port structure
auto gyro = analogGyroHandles->Get(handle);
if (gyro == nullptr) { // would only error on thread issue
*status = HAL_HANDLE_ERROR;
return HAL_kInvalidHandle;
}
gyro->handle = analogHandle;
gyro->voltsPerDegreePerSecond = 0;
gyro->offset = 0;
gyro->center = 0;
gyro->previousAllocation = allocationLocation ? allocationLocation : "";
return handle;
}

View File

@@ -9,6 +9,7 @@
#include "AnalogInternal.h"
#include "HALInitializer.h"
#include "HALInternal.h"
#include "PortsInternal.h"
#include "hal/AnalogAccumulator.h"
#include "hal/handles/HandlesInternal.h"
@@ -21,8 +22,9 @@ using namespace hal;
extern "C" {
HAL_AnalogInputHandle HAL_InitializeAnalogInputPort(HAL_PortHandle portHandle,
int32_t* status) {
HAL_AnalogInputHandle HAL_InitializeAnalogInputPort(
HAL_PortHandle portHandle, const char* allocationLocation,
int32_t* status) {
hal::init::CheckInit();
initializeAnalog(status);
@@ -31,23 +33,28 @@ HAL_AnalogInputHandle HAL_InitializeAnalogInputPort(HAL_PortHandle portHandle,
}
int16_t channel = getPortHandleChannel(portHandle);
if (channel == InvalidHandleIndex) {
*status = PARAMETER_OUT_OF_RANGE;
if (channel == InvalidHandleIndex || channel >= kNumAnalogInputs) {
*status = RESOURCE_OUT_OF_RANGE;
hal::SetLastErrorIndexOutOfRange(status, "Invalid Index for Analog Input",
0, kNumAnalogInputs, channel);
return HAL_kInvalidHandle;
}
HAL_AnalogInputHandle handle = analogInputHandles->Allocate(channel, status);
HAL_AnalogInputHandle handle;
auto analog_port = analogInputHandles->Allocate(channel, &handle, status);
if (*status != 0) {
if (analog_port) {
hal::SetLastErrorPreviouslyAllocated(status, "Analog Input", channel,
analog_port->previousAllocation);
} else {
hal::SetLastErrorIndexOutOfRange(status, "Invalid Index for Analog Input",
0, kNumAnalogInputs, channel);
}
return HAL_kInvalidHandle; // failed to allocate. Pass error back.
}
// Initialize port structure
auto analog_port = analogInputHandles->Get(handle);
if (analog_port == nullptr) { // would only error on thread issue
*status = HAL_HANDLE_ERROR;
return HAL_kInvalidHandle;
}
analog_port->channel = static_cast<uint8_t>(channel);
if (HAL_IsAccumulatorChannel(handle, status)) {
analog_port->accumulator.reset(tAccumulator::create(channel, status));
@@ -59,6 +66,8 @@ HAL_AnalogInputHandle HAL_InitializeAnalogInputPort(HAL_PortHandle portHandle,
analogInputSystem->writeScanList(channel, channel, status);
HAL_SetAnalogAverageBits(handle, kDefaultAverageBits, status);
HAL_SetAnalogOversampleBits(handle, kDefaultOversampleBits, status);
analog_port->previousAllocation =
allocationLocation ? allocationLocation : "";
return handle;
}

View File

@@ -7,6 +7,7 @@
#include <stdint.h>
#include <memory>
#include <string>
#include <wpi/mutex.h>
@@ -31,6 +32,7 @@ extern bool analogSampleRateSet;
struct AnalogPort {
uint8_t channel;
std::unique_ptr<tAccumulator> accumulator;
std::string previousAllocation;
};
extern IndexedHandleResource<HAL_AnalogInputHandle, hal::AnalogPort,

View File

@@ -4,8 +4,11 @@
#include "hal/AnalogOutput.h"
#include <string>
#include "AnalogInternal.h"
#include "HALInitializer.h"
#include "HALInternal.h"
#include "PortsInternal.h"
#include "hal/Errors.h"
#include "hal/handles/HandlesInternal.h"
@@ -17,6 +20,7 @@ namespace {
struct AnalogOutput {
uint8_t channel;
std::string previousAllocation;
};
} // namespace
@@ -36,8 +40,9 @@ void InitializeAnalogOutput() {
extern "C" {
HAL_AnalogOutputHandle HAL_InitializeAnalogOutputPort(HAL_PortHandle portHandle,
int32_t* status) {
HAL_AnalogOutputHandle HAL_InitializeAnalogOutputPort(
HAL_PortHandle portHandle, const char* allocationLocation,
int32_t* status) {
hal::init::CheckInit();
initializeAnalog(status);
@@ -46,25 +51,31 @@ HAL_AnalogOutputHandle HAL_InitializeAnalogOutputPort(HAL_PortHandle portHandle,
}
int16_t channel = getPortHandleChannel(portHandle);
if (channel == InvalidHandleIndex) {
*status = PARAMETER_OUT_OF_RANGE;
if (channel == InvalidHandleIndex || channel >= kNumAnalogOutputs) {
*status = RESOURCE_OUT_OF_RANGE;
hal::SetLastErrorIndexOutOfRange(status, "Invalid Index for Analog Output",
0, kNumAnalogOutputs, channel);
return HAL_kInvalidHandle;
}
HAL_AnalogOutputHandle handle =
analogOutputHandles->Allocate(channel, status);
HAL_AnalogOutputHandle handle;
auto port = analogOutputHandles->Allocate(channel, &handle, status);
if (*status != 0) {
if (port) {
hal::SetLastErrorPreviouslyAllocated(status, "Analog Output", channel,
port->previousAllocation);
} else {
hal::SetLastErrorIndexOutOfRange(status,
"Invalid Index for Analog Output", 0,
kNumAnalogOutputs, channel);
}
return HAL_kInvalidHandle; // failed to allocate. Pass error back.
}
auto port = analogOutputHandles->Get(handle);
if (port == nullptr) { // would only error on thread issue
*status = HAL_HANDLE_ERROR;
return HAL_kInvalidHandle;
}
port->channel = static_cast<uint8_t>(channel);
port->previousAllocation = allocationLocation ? allocationLocation : "";
return handle;
}

View File

@@ -11,6 +11,7 @@
#include "DigitalInternal.h"
#include "HALInitializer.h"
#include "HALInternal.h"
#include "PortsInternal.h"
#include "hal/cpp/fpga_clock.h"
#include "hal/handles/HandlesInternal.h"
@@ -38,7 +39,9 @@ void InitializeDIO() {
extern "C" {
HAL_DigitalHandle HAL_InitializeDIOPort(HAL_PortHandle portHandle,
HAL_Bool input, int32_t* status) {
HAL_Bool input,
const char* allocationLocation,
int32_t* status) {
hal::init::CheckInit();
initializeDigital(status);
@@ -48,23 +51,28 @@ HAL_DigitalHandle HAL_InitializeDIOPort(HAL_PortHandle portHandle,
int16_t channel = getPortHandleChannel(portHandle);
if (channel == InvalidHandleIndex || channel >= kNumDigitalChannels) {
*status = PARAMETER_OUT_OF_RANGE;
*status = RESOURCE_OUT_OF_RANGE;
hal::SetLastErrorIndexOutOfRange(status, "Invalid Index for DIO", 0,
kNumDigitalChannels, channel);
return HAL_kInvalidHandle;
}
auto handle =
digitalChannelHandles->Allocate(channel, HAL_HandleEnum::DIO, status);
HAL_DigitalHandle handle;
auto port = digitalChannelHandles->Allocate(channel, HAL_HandleEnum::DIO,
&handle, status);
if (*status != 0) {
if (port) {
hal::SetLastErrorPreviouslyAllocated(status, "PWM or DIO", channel,
port->previousAllocation);
} else {
hal::SetLastErrorIndexOutOfRange(status, "Invalid Index for DIO", 0,
kNumDigitalChannels, channel);
}
return HAL_kInvalidHandle; // failed to allocate. Pass error back.
}
auto port = digitalChannelHandles->Get(handle, HAL_HandleEnum::DIO);
if (port == nullptr) { // would only occur on thread issue.
*status = HAL_HANDLE_ERROR;
return HAL_kInvalidHandle;
}
port->channel = static_cast<uint8_t>(channel);
std::scoped_lock lock(digitalDIOMutex);
@@ -114,6 +122,7 @@ HAL_DigitalHandle HAL_InitializeDIOPort(HAL_PortHandle portHandle,
}
digitalSystem->writeOutputEnable(outputEnable, status);
port->previousAllocation = allocationLocation ? allocationLocation : "";
return handle;
}

View File

@@ -7,6 +7,7 @@
#include <stdint.h>
#include <memory>
#include <string>
#include <wpi/mutex.h>
@@ -70,6 +71,7 @@ struct DigitalPort {
int32_t centerPwm = 0;
int32_t deadbandMinPwm = 0;
int32_t minPwm = 0;
std::string previousAllocation;
};
extern DigitalHandleResource<HAL_DigitalHandle, DigitalPort,

View File

@@ -10,5 +10,11 @@
namespace hal {
void ReleaseFPGAInterrupt(int32_t interruptNumber);
void SetLastError(int32_t status, const wpi::Twine& value);
void SetLastError(int32_t* status, const wpi::Twine& value);
void SetLastErrorIndexOutOfRange(int32_t* status, const wpi::Twine& message,
int32_t minimum, int32_t maximum,
int32_t channel);
void SetLastErrorPreviouslyAllocated(int32_t* status, const wpi::Twine& message,
int32_t channel,
const wpi::Twine& previousAllocation);
} // namespace hal

View File

@@ -67,11 +67,11 @@ void HAL_InitializeI2C(HAL_I2CPort port, int32_t* status) {
return;
}
if ((i2CMXPDigitalHandle1 = HAL_InitializeDIOPort(
HAL_GetPort(24), false, status)) == HAL_kInvalidHandle) {
HAL_GetPort(24), false, nullptr, status)) == HAL_kInvalidHandle) {
return;
}
if ((i2CMXPDigitalHandle2 = HAL_InitializeDIOPort(
HAL_GetPort(25), false, status)) == HAL_kInvalidHandle) {
HAL_GetPort(25), false, nullptr, status)) == HAL_kInvalidHandle) {
HAL_FreeDIOPort(i2CMXPDigitalHandle1); // free the first port allocated
return;
}

View File

@@ -12,6 +12,7 @@
#include "ConstantsInternal.h"
#include "DigitalInternal.h"
#include "HALInitializer.h"
#include "HALInternal.h"
#include "PortsInternal.h"
#include "hal/cpp/fpga_clock.h"
#include "hal/handles/HandlesInternal.h"
@@ -65,6 +66,7 @@ void InitializePWM() {}
extern "C" {
HAL_DigitalHandle HAL_InitializePWMPort(HAL_PortHandle portHandle,
const char* allocationLocation,
int32_t* status) {
hal::init::CheckInit();
initializeDigital(status);
@@ -75,7 +77,9 @@ HAL_DigitalHandle HAL_InitializePWMPort(HAL_PortHandle portHandle,
int16_t channel = getPortHandleChannel(portHandle);
if (channel == InvalidHandleIndex || channel >= kNumPWMChannels) {
*status = PARAMETER_OUT_OF_RANGE;
*status = RESOURCE_OUT_OF_RANGE;
hal::SetLastErrorIndexOutOfRange(status, "Invalid Index for PWM", 0,
kNumPWMChannels, channel);
return HAL_kInvalidHandle;
}
@@ -87,19 +91,22 @@ HAL_DigitalHandle HAL_InitializePWMPort(HAL_PortHandle portHandle,
channel = remapMXPPWMChannel(channel) + 10; // remap MXP to proper channel
}
auto handle =
digitalChannelHandles->Allocate(channel, HAL_HandleEnum::PWM, status);
HAL_DigitalHandle handle;
auto port = digitalChannelHandles->Allocate(channel, HAL_HandleEnum::PWM,
&handle, status);
if (*status != 0) {
if (port) {
hal::SetLastErrorPreviouslyAllocated(status, "PWM or DIO", channel,
port->previousAllocation);
} else {
hal::SetLastErrorIndexOutOfRange(status, "Invalid Index for PWM", 0,
kNumPWMChannels, channel);
}
return HAL_kInvalidHandle; // failed to allocate. Pass error back.
}
auto port = digitalChannelHandles->Get(handle, HAL_HandleEnum::PWM);
if (port == nullptr) { // would only occur on thread issue.
*status = HAL_HANDLE_ERROR;
return HAL_kInvalidHandle;
}
port->channel = origChannel;
if (port->channel > tPWM::kNumHdrRegisters - 1) {
@@ -113,6 +120,8 @@ HAL_DigitalHandle HAL_InitializePWMPort(HAL_PortHandle portHandle,
// Defaults to allow an always valid config.
HAL_SetPWMConfig(handle, 2.0, 1.501, 1.5, 1.499, 1.0, status);
port->previousAllocation = allocationLocation ? allocationLocation : "";
return handle;
}
void HAL_FreePWMPort(HAL_DigitalHandle pwmPortHandle, int32_t* status) {

View File

@@ -4,8 +4,11 @@
#include "hal/Relay.h"
#include <string>
#include "DigitalInternal.h"
#include "HALInitializer.h"
#include "HALInternal.h"
#include "PortsInternal.h"
#include "hal/handles/IndexedHandleResource.h"
@@ -16,6 +19,7 @@ namespace {
struct Relay {
uint8_t channel;
bool fwd;
std::string previousAllocation;
};
} // namespace
@@ -38,6 +42,7 @@ void InitializeRelay() {
extern "C" {
HAL_RelayHandle HAL_InitializeRelayPort(HAL_PortHandle portHandle, HAL_Bool fwd,
const char* allocationLocation,
int32_t* status) {
hal::init::CheckInit();
initializeDigital(status);
@@ -47,8 +52,10 @@ HAL_RelayHandle HAL_InitializeRelayPort(HAL_PortHandle portHandle, HAL_Bool fwd,
}
int16_t channel = getPortHandleChannel(portHandle);
if (channel == InvalidHandleIndex) {
*status = PARAMETER_OUT_OF_RANGE;
if (channel == InvalidHandleIndex || channel >= kNumRelayChannels) {
*status = RESOURCE_OUT_OF_RANGE;
hal::SetLastErrorIndexOutOfRange(status, "Invalid Index for Relay", 0,
kNumRelayChannels, channel);
return HAL_kInvalidHandle;
}
@@ -56,18 +63,20 @@ HAL_RelayHandle HAL_InitializeRelayPort(HAL_PortHandle portHandle, HAL_Bool fwd,
channel += kNumRelayHeaders; // add 4 to reverse channels
}
auto handle = relayHandles->Allocate(channel, status);
HAL_RelayHandle handle;
auto port = relayHandles->Allocate(channel, &handle, status);
if (*status != 0) {
if (port) {
hal::SetLastErrorPreviouslyAllocated(status, "Relay", channel,
port->previousAllocation);
} else {
hal::SetLastErrorIndexOutOfRange(status, "Invalid Index for Relay", 0,
kNumRelayChannels, channel);
}
return HAL_kInvalidHandle; // failed to allocate. Pass error back.
}
auto port = relayHandles->Get(handle);
if (port == nullptr) { // would only occur on thread issue.
*status = HAL_HANDLE_ERROR;
return HAL_kInvalidHandle;
}
if (!fwd) {
// Subtract number of headers to put channel in range
channel -= kNumRelayHeaders;
@@ -78,6 +87,7 @@ HAL_RelayHandle HAL_InitializeRelayPort(HAL_PortHandle portHandle, HAL_Bool fwd,
}
port->channel = static_cast<uint8_t>(channel);
port->previousAllocation = allocationLocation ? allocationLocation : "";
return handle;
}

View File

@@ -75,14 +75,14 @@ static void CommonSPIPortInit(int32_t* status) {
}
// MISO
if ((digitalHandles[3] = HAL_InitializeDIOPort(createPortHandleForSPI(29),
false, status)) ==
false, nullptr, status)) ==
HAL_kInvalidHandle) {
std::printf("Failed to allocate DIO 29 (MISO)\n");
return;
}
// MOSI
if ((digitalHandles[4] = HAL_InitializeDIOPort(createPortHandleForSPI(30),
false, status)) ==
false, nullptr, status)) ==
HAL_kInvalidHandle) {
std::printf("Failed to allocate DIO 30 (MOSI)\n");
HAL_FreeDIOPort(digitalHandles[3]); // free the first port allocated
@@ -133,7 +133,7 @@ void HAL_InitializeSPI(HAL_SPIPort port, int32_t* status) {
}
// CS1, Allocate
if ((digitalHandles[0] = HAL_InitializeDIOPort(createPortHandleForSPI(26),
false, status)) ==
false, nullptr, status)) ==
HAL_kInvalidHandle) {
std::printf("Failed to allocate DIO 26 (CS1)\n");
CommonSPIPortFree();
@@ -156,7 +156,7 @@ void HAL_InitializeSPI(HAL_SPIPort port, int32_t* status) {
}
// CS2, Allocate
if ((digitalHandles[1] = HAL_InitializeDIOPort(createPortHandleForSPI(27),
false, status)) ==
false, nullptr, status)) ==
HAL_kInvalidHandle) {
std::printf("Failed to allocate DIO 27 (CS2)\n");
CommonSPIPortFree();
@@ -179,7 +179,7 @@ void HAL_InitializeSPI(HAL_SPIPort port, int32_t* status) {
}
// CS3, Allocate
if ((digitalHandles[2] = HAL_InitializeDIOPort(createPortHandleForSPI(28),
false, status)) ==
false, nullptr, status)) ==
HAL_kInvalidHandle) {
std::printf("Failed to allocate DIO 28 (CS3)\n");
CommonSPIPortFree();
@@ -201,20 +201,20 @@ void HAL_InitializeSPI(HAL_SPIPort port, int32_t* status) {
return;
}
if ((digitalHandles[5] = HAL_InitializeDIOPort(createPortHandleForSPI(14),
false, status)) ==
false, nullptr, status)) ==
HAL_kInvalidHandle) {
wpi::outs() << "Failed to allocate DIO 14\n";
return;
}
if ((digitalHandles[6] = HAL_InitializeDIOPort(createPortHandleForSPI(15),
false, status)) ==
false, nullptr, status)) ==
HAL_kInvalidHandle) {
wpi::outs() << "Failed to allocate DIO 15\n";
HAL_FreeDIOPort(digitalHandles[5]); // free the first port allocated
return;
}
if ((digitalHandles[7] = HAL_InitializeDIOPort(createPortHandleForSPI(16),
false, status)) ==
false, nullptr, status)) ==
HAL_kInvalidHandle) {
wpi::outs() << "Failed to allocate DIO 16\n";
HAL_FreeDIOPort(digitalHandles[5]); // free the first port allocated
@@ -222,7 +222,7 @@ void HAL_InitializeSPI(HAL_SPIPort port, int32_t* status) {
return;
}
if ((digitalHandles[8] = HAL_InitializeDIOPort(createPortHandleForSPI(17),
false, status)) ==
false, nullptr, status)) ==
HAL_kInvalidHandle) {
wpi::outs() << "Failed to allocate DIO 17\n";
HAL_FreeDIOPort(digitalHandles[5]); // free the first port allocated

View File

@@ -70,19 +70,14 @@ HAL_SerialPortHandle HAL_InitializeSerialPort(HAL_SerialPort port,
HAL_SerialPortHandle HAL_InitializeSerialPortDirect(HAL_SerialPort port,
const char* portName,
int32_t* status) {
auto handle = serialPortHandles->Allocate(static_cast<int16_t>(port), status);
HAL_SerialPortHandle handle;
auto serialPort =
serialPortHandles->Allocate(static_cast<int16_t>(port), &handle, status);
if (*status != 0) {
return HAL_kInvalidHandle;
}
auto serialPort = serialPortHandles->Get(handle);
if (serialPort == nullptr) {
*status = HAL_HANDLE_ERROR;
return HAL_kInvalidHandle;
}
serialPort->portId = open(portName, O_RDWR | O_NOCTTY);
if (serialPort->portId < 0) {
*status = errno;

View File

@@ -60,16 +60,12 @@ HAL_SolenoidHandle HAL_InitializeSolenoidPort(HAL_PortHandle portHandle,
return HAL_kInvalidHandle;
}
auto handle = solenoidHandles->Allocate(
module * kNumSolenoidChannels + channel, status);
HAL_SolenoidHandle handle;
auto solenoidPort = solenoidHandles->Allocate(
module * kNumSolenoidChannels + channel, &handle, status);
if (*status != 0) {
return HAL_kInvalidHandle;
}
auto solenoidPort = solenoidHandles->Get(handle);
if (solenoidPort == nullptr) { // would only occur on thread issues
*status = HAL_HANDLE_ERROR;
return HAL_kInvalidHandle;
}
solenoidPort->module = static_cast<uint8_t>(module);
solenoidPort->channel = static_cast<uint8_t>(channel);

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

View File

@@ -22,9 +22,12 @@ extern "C" {
* Initializes an analog gyro.
*
* @param handle handle to the analog port
* @param allocationLocation the location where the allocation is occuring
* (can be null)
* @return the initialized gyro handle
*/
HAL_GyroHandle HAL_InitializeAnalogGyro(HAL_AnalogInputHandle handle,
const char* allocationLocation,
int32_t* status);
/**

View File

@@ -22,10 +22,12 @@ extern "C" {
* Initializes the analog input port using the given port object.
*
* @param portHandle Handle to the port to initialize.
* @param allocationLocation the location where the allocation is occuring
* (can be null)
* @return the created analog input handle
*/
HAL_AnalogInputHandle HAL_InitializeAnalogInputPort(HAL_PortHandle portHandle,
int32_t* status);
HAL_AnalogInputHandle HAL_InitializeAnalogInputPort(
HAL_PortHandle portHandle, const char* allocationLocation, int32_t* status);
/**
* Frees an analog input port.

View File

@@ -22,10 +22,12 @@ extern "C" {
* Initializes the analog output port using the given port object.
*
* @param handle handle to the port
* @param allocationLocation the location where the allocation is occuring
* (can be null)
* @return the created analog output handle
*/
HAL_AnalogOutputHandle HAL_InitializeAnalogOutputPort(HAL_PortHandle portHandle,
int32_t* status);
HAL_AnalogOutputHandle HAL_InitializeAnalogOutputPort(
HAL_PortHandle portHandle, const char* allocationLocation, int32_t* status);
/**
* Frees an analog output port.

View File

@@ -21,12 +21,16 @@ extern "C" {
/**
* Creates a new instance of a digital port.
*
* @param portHandle the port handle to create from
* @param input true for input, false for output
* @return the created digital handle
* @param portHandle the port handle to create from
* @param input true for input, false for output
* @param allocationLocation the location where the allocation is occuring
* (can be null)
* @return the created digital handle
*/
HAL_DigitalHandle HAL_InitializeDIOPort(HAL_PortHandle portHandle,
HAL_Bool input, int32_t* status);
HAL_Bool input,
const char* allocationLocation,
int32_t* status);
/**
* Checks if a DIO channel is valid.

View File

@@ -41,6 +41,8 @@
#define ERR_FRCSystem_NoDSConnection_MESSAGE \
"FRCSystem: No driver station connected"
#define HAL_SUCCESS 0
#define SAMPLE_RATE_TOO_HIGH 1001
#define SAMPLE_RATE_TOO_HIGH_MESSAGE \
"HAL: Analog module sample rate is too high"

View File

@@ -22,9 +22,12 @@ extern "C" {
* Initializes a PWM port.
*
* @param portHandle the port to initialize
* @param allocationLocation the location where the allocation is occuring
* (can be null)
* @return the created pwm handle
*/
HAL_DigitalHandle HAL_InitializePWMPort(HAL_PortHandle portHandle,
const char* allocationLocation,
int32_t* status);
/**

View File

@@ -26,9 +26,12 @@ extern "C" {
*
* @param portHandle the port handle to initialize
* @param fwd true for the forward port, false for the reverse port
* @param allocationLocation the location where the allocation is occuring
* (can be null)
* @return the created relay handle
*/
HAL_RelayHandle HAL_InitializeRelayPort(HAL_PortHandle portHandle, HAL_Bool fwd,
const char* allocationLocation,
int32_t* status);
/**

View File

@@ -22,7 +22,7 @@ namespace hal {
* allows a limited number of handles that are allocated by index.
* The enum value is separate, as 2 enum values are allowed per handle
* Because they are allocated by index, each individual index holds its own
* mutex, which reduces contention heavily.]
* mutex, which reduces contention heavily.
*
* @tparam THandle The Handle Type (Must be typedefed from HAL_Handle)
* @tparam TStruct The struct type held by this resource
@@ -38,7 +38,8 @@ class DigitalHandleResource : public HandleBase {
DigitalHandleResource(const DigitalHandleResource&) = delete;
DigitalHandleResource& operator=(const DigitalHandleResource&) = delete;
THandle Allocate(int16_t index, HAL_HandleEnum enumValue, int32_t* status);
std::shared_ptr<TStruct> Allocate(int16_t index, HAL_HandleEnum enumValue,
THandle* handle, int32_t* status);
int16_t GetIndex(THandle handle, HAL_HandleEnum enumValue) {
return getHandleTypedIndex(handle, enumValue, m_version);
}
@@ -52,21 +53,27 @@ class DigitalHandleResource : public HandleBase {
};
template <typename THandle, typename TStruct, int16_t size>
THandle DigitalHandleResource<THandle, TStruct, size>::Allocate(
int16_t index, HAL_HandleEnum enumValue, int32_t* status) {
std::shared_ptr<TStruct>
DigitalHandleResource<THandle, TStruct, size>::Allocate(
int16_t index, HAL_HandleEnum enumValue, THandle* handle, int32_t* status) {
// don't acquire the lock if we can fail early.
if (index < 0 || index >= size) {
*handle = HAL_kInvalidHandle;
*status = RESOURCE_OUT_OF_RANGE;
return HAL_kInvalidHandle;
return nullptr;
}
std::scoped_lock lock(m_handleMutexes[index]);
// check for allocation, otherwise allocate and return a valid handle
if (m_structures[index] != nullptr) {
*handle = HAL_kInvalidHandle;
*status = RESOURCE_IS_ALLOCATED;
return HAL_kInvalidHandle;
return m_structures[index];
}
m_structures[index] = std::make_shared<TStruct>();
return static_cast<THandle>(hal::createHandle(index, enumValue, m_version));
*handle =
static_cast<THandle>(hal::createHandle(index, enumValue, m_version));
*status = HAL_SUCCESS;
return m_structures[index];
}
template <typename THandle, typename TStruct, int16_t size>

View File

@@ -39,7 +39,8 @@ class IndexedHandleResource : public HandleBase {
IndexedHandleResource(const IndexedHandleResource&) = delete;
IndexedHandleResource& operator=(const IndexedHandleResource&) = delete;
THandle Allocate(int16_t index, int32_t* status);
std::shared_ptr<TStruct> Allocate(int16_t index, THandle* handle,
int32_t* status);
int16_t GetIndex(THandle handle) {
return getHandleTypedIndex(handle, enumValue, m_version);
}
@@ -54,21 +55,27 @@ class IndexedHandleResource : public HandleBase {
template <typename THandle, typename TStruct, int16_t size,
HAL_HandleEnum enumValue>
THandle IndexedHandleResource<THandle, TStruct, size, enumValue>::Allocate(
int16_t index, int32_t* status) {
std::shared_ptr<TStruct>
IndexedHandleResource<THandle, TStruct, size, enumValue>::Allocate(
int16_t index, THandle* handle, int32_t* status) {
// don't acquire the lock if we can fail early.
if (index < 0 || index >= size) {
*status = RESOURCE_OUT_OF_RANGE;
return HAL_kInvalidHandle;
*handle = HAL_kInvalidHandle;
return nullptr;
}
std::scoped_lock lock(m_handleMutexes[index]);
// check for allocation, otherwise allocate and return a valid handle
if (m_structures[index] != nullptr) {
*status = RESOURCE_IS_ALLOCATED;
return HAL_kInvalidHandle;
*handle = HAL_kInvalidHandle;
return m_structures[index];
}
m_structures[index] = std::make_shared<TStruct>();
return static_cast<THandle>(hal::createHandle(index, enumValue, m_version));
*handle =
static_cast<THandle>(hal::createHandle(index, enumValue, m_version));
*status = HAL_SUCCESS;
return m_structures[index];
}
template <typename THandle, typename TStruct, int16_t size,

View File

@@ -4,7 +4,10 @@
#include "hal/AnalogGyro.h"
#include <string>
#include "HALInitializer.h"
#include "HALInternal.h"
#include "PortsInternal.h"
#include "hal/AnalogAccumulator.h"
#include "hal/Errors.h"
@@ -15,6 +18,7 @@ namespace {
struct AnalogGyro {
HAL_AnalogInputHandle handle;
uint8_t index;
std::string previousAllocation;
};
} // namespace
@@ -34,36 +38,41 @@ void InitializeAnalogGyro() {
extern "C" {
HAL_GyroHandle HAL_InitializeAnalogGyro(HAL_AnalogInputHandle analogHandle,
const char* allocationLocation,
int32_t* status) {
hal::init::CheckInit();
// Handle will be type checked by HAL_IsAccumulatorChannel
int16_t channel = getHandleIndex(analogHandle);
if (!HAL_IsAccumulatorChannel(analogHandle, status)) {
if (*status == 0) {
*status = HAL_INVALID_ACCUMULATOR_CHANNEL;
hal::SetLastErrorIndexOutOfRange(status, "Invalid Index for Analog Gyro",
0, kNumAccumulators, channel);
}
return HAL_kInvalidHandle;
}
// handle known to be correct, so no need to type check
int16_t channel = getHandleIndex(analogHandle);
auto handle = analogGyroHandles->Allocate(channel, status);
HAL_GyroHandle handle;
auto gyro = analogGyroHandles->Allocate(channel, &handle, status);
if (*status != 0) {
if (gyro) {
hal::SetLastErrorPreviouslyAllocated(status, "Analog Gyro", channel,
gyro->previousAllocation);
} else {
hal::SetLastErrorIndexOutOfRange(status, "Invalid Index for Analog Gyro",
0, kNumAccumulators, channel);
}
return HAL_kInvalidHandle; // failed to allocate. Pass error back.
}
// Initialize port structure
auto gyro = analogGyroHandles->Get(handle);
if (gyro == nullptr) { // would only error on thread issue
*status = HAL_HANDLE_ERROR;
return HAL_kInvalidHandle;
}
gyro->handle = analogHandle;
gyro->index = channel;
SimAnalogGyroData[channel].initialized = true;
gyro->previousAllocation = allocationLocation ? allocationLocation : "";
return handle;
}

View File

@@ -6,6 +6,7 @@
#include "AnalogInternal.h"
#include "HALInitializer.h"
#include "HALInternal.h"
#include "PortsInternal.h"
#include "hal/AnalogAccumulator.h"
#include "hal/handles/HandlesInternal.h"
@@ -18,28 +19,32 @@ void InitializeAnalogInput() {}
} // namespace hal::init
extern "C" {
HAL_AnalogInputHandle HAL_InitializeAnalogInputPort(HAL_PortHandle portHandle,
int32_t* status) {
HAL_AnalogInputHandle HAL_InitializeAnalogInputPort(
HAL_PortHandle portHandle, const char* allocationLocation,
int32_t* status) {
hal::init::CheckInit();
int16_t channel = getPortHandleChannel(portHandle);
if (channel == InvalidHandleIndex) {
*status = PARAMETER_OUT_OF_RANGE;
if (channel == InvalidHandleIndex || channel >= kNumAnalogInputs) {
*status = RESOURCE_OUT_OF_RANGE;
hal::SetLastErrorIndexOutOfRange(status, "Invalid Index for Analog Input",
0, kNumAnalogInputs, channel);
return HAL_kInvalidHandle;
}
HAL_AnalogInputHandle handle = analogInputHandles->Allocate(channel, status);
HAL_AnalogInputHandle handle;
auto analog_port = analogInputHandles->Allocate(channel, &handle, status);
if (*status != 0) {
if (analog_port) {
hal::SetLastErrorPreviouslyAllocated(status, "Analog Input", channel,
analog_port->previousAllocation);
} else {
hal::SetLastErrorIndexOutOfRange(status, "Invalid Index for Analog Input",
0, kNumAnalogInputs, channel);
}
return HAL_kInvalidHandle; // failed to allocate. Pass error back.
}
// Initialize port structure
auto analog_port = analogInputHandles->Get(handle);
if (analog_port == nullptr) { // would only error on thread issue
*status = HAL_HANDLE_ERROR;
return HAL_kInvalidHandle;
}
analog_port->channel = static_cast<uint8_t>(channel);
if (HAL_IsAccumulatorChannel(handle, status)) {
analog_port->isAccumulator = true;
@@ -51,6 +56,9 @@ HAL_AnalogInputHandle HAL_InitializeAnalogInputPort(HAL_PortHandle portHandle,
SimAnalogInData[channel].accumulatorInitialized = false;
SimAnalogInData[channel].simDevice = 0;
analog_port->previousAllocation =
allocationLocation ? allocationLocation : "";
return handle;
}
void HAL_FreeAnalogInputPort(HAL_AnalogInputHandle analogPortHandle) {

View File

@@ -6,6 +6,8 @@
#include <stdint.h>
#include <string>
#include "PortsInternal.h"
#include "hal/handles/HandlesInternal.h"
#include "hal/handles/IndexedHandleResource.h"
@@ -20,6 +22,7 @@ static constexpr uint32_t kAccumulatorChannels[] = {0, 1};
struct AnalogPort {
uint8_t channel;
bool isAccumulator;
std::string previousAllocation;
};
extern IndexedHandleResource<HAL_AnalogInputHandle, hal::AnalogPort,

View File

@@ -4,7 +4,10 @@
#include "hal/AnalogOutput.h"
#include <string>
#include "HALInitializer.h"
#include "HALInternal.h"
#include "PortsInternal.h"
#include "hal/Errors.h"
#include "hal/handles/HandlesInternal.h"
@@ -16,6 +19,7 @@ using namespace hal;
namespace {
struct AnalogOutput {
uint8_t channel;
std::string previousAllocation;
};
} // namespace
@@ -33,32 +37,39 @@ void InitializeAnalogOutput() {
} // namespace hal::init
extern "C" {
HAL_AnalogOutputHandle HAL_InitializeAnalogOutputPort(HAL_PortHandle portHandle,
int32_t* status) {
HAL_AnalogOutputHandle HAL_InitializeAnalogOutputPort(
HAL_PortHandle portHandle, const char* allocationLocation,
int32_t* status) {
hal::init::CheckInit();
int16_t channel = getPortHandleChannel(portHandle);
if (channel == InvalidHandleIndex) {
*status = PARAMETER_OUT_OF_RANGE;
if (channel == InvalidHandleIndex || channel >= kNumAnalogOutputs) {
*status = RESOURCE_OUT_OF_RANGE;
hal::SetLastErrorIndexOutOfRange(status, "Invalid Index for Analog Output",
0, kNumAnalogOutputs, channel);
return HAL_kInvalidHandle;
}
HAL_AnalogOutputHandle handle =
analogOutputHandles->Allocate(channel, status);
HAL_AnalogOutputHandle handle;
auto port = analogOutputHandles->Allocate(channel, &handle, status);
if (*status != 0) {
if (port) {
hal::SetLastErrorPreviouslyAllocated(status, "Analog Output", channel,
port->previousAllocation);
} else {
hal::SetLastErrorIndexOutOfRange(status,
"Invalid Index for Analog Output", 0,
kNumAnalogOutputs, channel);
}
return HAL_kInvalidHandle; // failed to allocate. Pass error back.
}
auto port = analogOutputHandles->Get(handle);
if (port == nullptr) { // would only error on thread issue
*status = HAL_HANDLE_ERROR;
return HAL_kInvalidHandle;
}
port->channel = static_cast<uint8_t>(channel);
// Initialize sim analog input
SimAnalogOutData[channel].initialized = true;
port->previousAllocation = allocationLocation ? allocationLocation : "";
return handle;
}

View File

@@ -6,6 +6,7 @@
#include "DigitalInternal.h"
#include "HALInitializer.h"
#include "HALInternal.h"
#include "PortsInternal.h"
#include "hal/handles/HandlesInternal.h"
#include "hal/handles/LimitedHandleResource.h"
@@ -31,36 +32,41 @@ void InitializeDIO() {
extern "C" {
HAL_DigitalHandle HAL_InitializeDIOPort(HAL_PortHandle portHandle,
HAL_Bool input, int32_t* status) {
HAL_Bool input,
const char* allocationLocation,
int32_t* status) {
hal::init::CheckInit();
if (*status != 0) {
return HAL_kInvalidHandle;
}
int16_t channel = getPortHandleChannel(portHandle);
if (channel == InvalidHandleIndex) {
*status = PARAMETER_OUT_OF_RANGE;
*status = RESOURCE_OUT_OF_RANGE;
hal::SetLastErrorIndexOutOfRange(status, "Invalid Index for DIO", 0,
kNumDigitalChannels, channel);
return HAL_kInvalidHandle;
}
auto handle =
digitalChannelHandles->Allocate(channel, HAL_HandleEnum::DIO, status);
HAL_DigitalHandle handle;
auto port = digitalChannelHandles->Allocate(channel, HAL_HandleEnum::DIO,
&handle, status);
if (*status != 0) {
if (port) {
hal::SetLastErrorPreviouslyAllocated(status, "PWM or DIO", channel,
port->previousAllocation);
} else {
hal::SetLastErrorIndexOutOfRange(status, "Invalid Index for DIO", 0,
kNumDigitalChannels, channel);
}
return HAL_kInvalidHandle; // failed to allocate. Pass error back.
}
auto port = digitalChannelHandles->Get(handle, HAL_HandleEnum::DIO);
if (port == nullptr) { // would only occur on thread issue.
*status = HAL_HANDLE_ERROR;
return HAL_kInvalidHandle;
}
port->channel = static_cast<uint8_t>(channel);
SimDIOData[channel].initialized = true;
SimDIOData[channel].isInput = input;
SimDIOData[channel].simDevice = 0;
port->previousAllocation = allocationLocation ? allocationLocation : "";
return handle;
}

View File

@@ -6,6 +6,8 @@
#include <stdint.h>
#include <string>
#include "PortsInternal.h"
#include "hal/AnalogTrigger.h"
#include "hal/handles/DigitalHandleResource.h"
@@ -56,6 +58,7 @@ struct DigitalPort {
int32_t centerPwm = 0;
int32_t deadbandMinPwm = 0;
int32_t minPwm = 0;
std::string previousAllocation;
};
extern DigitalHandleResource<HAL_DigitalHandle, DigitalPort,

View File

@@ -0,0 +1,19 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
#pragma once
#include <stdint.h>
#include <wpi/Twine.h>
namespace hal {
void SetLastError(int32_t* status, const wpi::Twine& value);
void SetLastErrorIndexOutOfRange(int32_t* status, const wpi::Twine& message,
int32_t minimum, int32_t maximum,
int32_t channel);
void SetLastErrorPreviouslyAllocated(int32_t* status, const wpi::Twine& message,
int32_t channel,
const wpi::Twine& previousAllocation);
} // namespace hal

View File

@@ -7,6 +7,7 @@
#include "ConstantsInternal.h"
#include "DigitalInternal.h"
#include "HALInitializer.h"
#include "HALInternal.h"
#include "PortsInternal.h"
#include "hal/handles/HandlesInternal.h"
#include "mockdata/PWMDataInternal.h"
@@ -20,15 +21,15 @@ void InitializePWM() {}
extern "C" {
HAL_DigitalHandle HAL_InitializePWMPort(HAL_PortHandle portHandle,
const char* allocationLocation,
int32_t* status) {
hal::init::CheckInit();
if (*status != 0) {
return HAL_kInvalidHandle;
}
int16_t channel = getPortHandleChannel(portHandle);
if (channel == InvalidHandleIndex) {
*status = PARAMETER_OUT_OF_RANGE;
*status = RESOURCE_OUT_OF_RANGE;
hal::SetLastErrorIndexOutOfRange(status, "Invalid Index for PWM", 0,
kNumPWMChannels, channel);
return HAL_kInvalidHandle;
}
@@ -40,19 +41,22 @@ HAL_DigitalHandle HAL_InitializePWMPort(HAL_PortHandle portHandle,
channel = remapMXPPWMChannel(channel) + 10; // remap MXP to proper channel
}
auto handle =
digitalChannelHandles->Allocate(channel, HAL_HandleEnum::PWM, status);
HAL_DigitalHandle handle;
auto port = digitalChannelHandles->Allocate(channel, HAL_HandleEnum::PWM,
&handle, status);
if (*status != 0) {
if (port) {
hal::SetLastErrorPreviouslyAllocated(status, "PWM or DIO", channel,
port->previousAllocation);
} else {
hal::SetLastErrorIndexOutOfRange(status, "Invalid Index for PWM", 0,
kNumPWMChannels, channel);
}
return HAL_kInvalidHandle; // failed to allocate. Pass error back.
}
auto port = digitalChannelHandles->Get(handle, HAL_HandleEnum::PWM);
if (port == nullptr) { // would only occur on thread issue.
*status = HAL_HANDLE_ERROR;
return HAL_kInvalidHandle;
}
port->channel = origChannel;
SimPWMData[origChannel].initialized = true;
@@ -60,6 +64,8 @@ HAL_DigitalHandle HAL_InitializePWMPort(HAL_PortHandle portHandle,
// Defaults to allow an always valid config.
HAL_SetPWMConfig(handle, 2.0, 1.501, 1.5, 1.499, 1.0, status);
port->previousAllocation = allocationLocation ? allocationLocation : "";
return handle;
}
void HAL_FreePWMPort(HAL_DigitalHandle pwmPortHandle, int32_t* status) {

View File

@@ -4,7 +4,10 @@
#include "hal/Relay.h"
#include <string>
#include "HALInitializer.h"
#include "HALInternal.h"
#include "PortsInternal.h"
#include "hal/handles/IndexedHandleResource.h"
#include "mockdata/RelayDataInternal.h"
@@ -15,6 +18,7 @@ namespace {
struct Relay {
uint8_t channel;
bool fwd;
std::string previousAllocation;
};
} // namespace
@@ -32,6 +36,7 @@ void InitializeRelay() {
extern "C" {
HAL_RelayHandle HAL_InitializeRelayPort(HAL_PortHandle portHandle, HAL_Bool fwd,
const char* allocationLocation,
int32_t* status) {
hal::init::CheckInit();
if (*status != 0) {
@@ -39,8 +44,10 @@ HAL_RelayHandle HAL_InitializeRelayPort(HAL_PortHandle portHandle, HAL_Bool fwd,
}
int16_t channel = getPortHandleChannel(portHandle);
if (channel == InvalidHandleIndex) {
*status = PARAMETER_OUT_OF_RANGE;
if (channel == InvalidHandleIndex || channel >= kNumRelayChannels) {
*status = RESOURCE_OUT_OF_RANGE;
hal::SetLastErrorIndexOutOfRange(status, "Invalid Index for Relay", 0,
kNumRelayChannels, channel);
return HAL_kInvalidHandle;
}
@@ -48,18 +55,20 @@ HAL_RelayHandle HAL_InitializeRelayPort(HAL_PortHandle portHandle, HAL_Bool fwd,
channel += kNumRelayHeaders; // add 4 to reverse channels
}
auto handle = relayHandles->Allocate(channel, status);
HAL_RelayHandle handle;
auto port = relayHandles->Allocate(channel, &handle, status);
if (*status != 0) {
if (port) {
hal::SetLastErrorPreviouslyAllocated(status, "Relay", channel,
port->previousAllocation);
} else {
hal::SetLastErrorIndexOutOfRange(status, "Invalid Index for Relay", 0,
kNumRelayChannels, channel);
}
return HAL_kInvalidHandle; // failed to allocate. Pass error back.
}
auto port = relayHandles->Get(handle);
if (port == nullptr) { // would only occur on thread issue.
*status = HAL_HANDLE_ERROR;
return HAL_kInvalidHandle;
}
if (!fwd) {
// Subtract number of headers to put channel in range
channel -= kNumRelayHeaders;
@@ -73,6 +82,7 @@ HAL_RelayHandle HAL_InitializeRelayPort(HAL_PortHandle portHandle, HAL_Bool fwd,
}
port->channel = static_cast<uint8_t>(channel);
port->previousAllocation = allocationLocation ? allocationLocation : "";
return handle;
}

View File

@@ -55,17 +55,13 @@ HAL_SolenoidHandle HAL_InitializeSolenoidPort(HAL_PortHandle portHandle,
return HAL_kInvalidHandle;
}
auto handle = solenoidHandles->Allocate(
module * kNumSolenoidChannels + channel, status);
HAL_SolenoidHandle handle;
auto solenoidPort = solenoidHandles->Allocate(
module * kNumSolenoidChannels + channel, &handle, status);
if (handle == HAL_kInvalidHandle) { // out of resources
*status = NO_AVAILABLE_RESOURCES;
return HAL_kInvalidHandle;
}
auto solenoidPort = solenoidHandles->Get(handle);
if (solenoidPort == nullptr) { // would only occur on thread issues
*status = HAL_HANDLE_ERROR;
return HAL_kInvalidHandle;
}
solenoidPort->module = static_cast<uint8_t>(module);
solenoidPort->channel = static_cast<uint8_t>(channel);