[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

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