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