mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-07-01 02:41:48 +00:00
[hal] Report previous allocation location for indexed resource duplicates (#3322)
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user