Skip to content

Commit

Permalink
Fix CAN ID validation and error reporting for power and pneumatics de…
Browse files Browse the repository at this point in the history
…vices

REV: 1-63, 63 devices
CTRE: 0-62, 63 devices
  • Loading branch information
rzblue committed Oct 10, 2024
1 parent f150b36 commit 7425c09
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 9 deletions.
2 changes: 1 addition & 1 deletion hal/src/main/native/athena/CTREPCM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ HAL_CTREPCMHandle HAL_InitializeCTREPCM(int32_t module,
pcm->previousAllocation);
} else {
hal::SetLastErrorIndexOutOfRange(status, "Invalid Index for CTRE PCM", 0,
kNumCTREPCMModules, module);
kNumCTREPCMModules - 1, module);
}
return HAL_kInvalidHandle; // failed to allocate. Pass error back.
}
Expand Down
7 changes: 4 additions & 3 deletions hal/src/main/native/athena/CTREPDP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,9 @@ HAL_PDPHandle HAL_InitializePDP(int32_t module, const char* allocationLocation,
int32_t* status) {
hal::init::CheckInit();
if (!HAL_CheckPDPModule(module)) {
*status = PARAMETER_OUT_OF_RANGE;
hal::SetLastError(status, fmt::format("Invalid pdp module {}", module));
*status = RESOURCE_OUT_OF_RANGE;
hal::SetLastErrorIndexOutOfRange(status, "Invalid Index for CTRE PDP", 0,
kNumCTREPDPModules - 1, module);
return HAL_kInvalidHandle;
}

Expand All @@ -147,7 +148,7 @@ HAL_PDPHandle HAL_InitializePDP(int32_t module, const char* allocationLocation,
pdp->previousAllocation);
} else {
hal::SetLastErrorIndexOutOfRange(status, "Invalid Index for CTRE PDP", 0,
kNumCTREPDPModules, module);
kNumCTREPDPModules - 1, module);
}
return HAL_kInvalidHandle; // failed to allocate. Pass error back.
}
Expand Down
9 changes: 6 additions & 3 deletions hal/src/main/native/athena/REVPDH.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,17 +205,20 @@ HAL_REVPDHHandle HAL_InitializeREVPDH(int32_t module,
hal::init::CheckInit();
if (!HAL_CheckREVPDHModuleNumber(module)) {
*status = RESOURCE_OUT_OF_RANGE;
hal::SetLastErrorIndexOutOfRange(status, "Invalid Index for REV PDH", 1,
kNumREVPDHModules, module);
return HAL_kInvalidHandle;
}

HAL_REVPDHHandle handle;
auto hpdh = REVPDHHandles->Allocate(module, &handle, status);
// Module starts at 1
auto hpdh = REVPDHHandles->Allocate(module - 1, &handle, status);
if (*status != 0) {
if (hpdh) {
hal::SetLastErrorPreviouslyAllocated(status, "REV PDH", module,
hpdh->previousAllocation);
} else {
hal::SetLastErrorIndexOutOfRange(status, "Invalid Index for REV PDH", 0,
hal::SetLastErrorIndexOutOfRange(status, "Invalid Index for REV PDH", 1,
kNumREVPDHModules, module);
}
return HAL_kInvalidHandle; // failed to allocate. Pass error back.
Expand Down Expand Up @@ -253,7 +256,7 @@ int32_t HAL_GetREVPDHModuleNumber(HAL_REVPDHHandle handle, int32_t* status) {
}

HAL_Bool HAL_CheckREVPDHModuleNumber(int32_t module) {
return ((module >= 1) && (module < kNumREVPDHModules)) ? 1 : 0;
return ((module >= 1) && (module <= kNumREVPDHModules)) ? 1 : 0;
}

HAL_Bool HAL_CheckREVPDHChannelNumber(int32_t channel) {
Expand Down
6 changes: 4 additions & 2 deletions hal/src/main/native/athena/REVPH.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,13 +196,15 @@ HAL_REVPHHandle HAL_InitializeREVPH(int32_t module,
int32_t* status) {
hal::init::CheckInit();
if (!HAL_CheckREVPHModuleNumber(module)) {
*status = RESOURCE_OUT_OF_RANGE;
hal::SetLastErrorIndexOutOfRange(status, "Invalid Index for REV PH", 1,
kNumREVPHModules, module);
return HAL_kInvalidHandle;
}

HAL_REVPHHandle handle;
auto hph = REVPHHandles->Allocate(module, &handle, status);
// Module starts at 1
auto hph = REVPHHandles->Allocate(module - 1, &handle, status);
if (*status != 0) {
if (hph) {
hal::SetLastErrorPreviouslyAllocated(status, "REV PH", module,
Expand Down Expand Up @@ -247,7 +249,7 @@ void HAL_FreeREVPH(HAL_REVPHHandle handle) {
}

HAL_Bool HAL_CheckREVPHModuleNumber(int32_t module) {
return module >= 1 && module < kNumREVPDHModules;
return module >= 1 && module <= kNumREVPDHModules;
}

HAL_Bool HAL_CheckREVPHSolenoidChannel(int32_t channel) {
Expand Down

0 comments on commit 7425c09

Please sign in to comment.