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