From 223d47af0e3fa450780104f16adab22cd6ae1e82 Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Sun, 23 Jun 2019 11:48:04 -0700 Subject: [PATCH] HALSIM: support mocking of HAL_SendError() (#1728) --- hal/src/main/native/include/mockdata/MockHooks.h | 8 +++++++- hal/src/main/native/sim/DriverStation.cpp | 6 +++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/hal/src/main/native/include/mockdata/MockHooks.h b/hal/src/main/native/include/mockdata/MockHooks.h index 37eb0e7e0d..e484727088 100644 --- a/hal/src/main/native/include/mockdata/MockHooks.h +++ b/hal/src/main/native/include/mockdata/MockHooks.h @@ -1,5 +1,5 @@ /*----------------------------------------------------------------------------*/ -/* Copyright (c) 2017-2018 FIRST. All Rights Reserved. */ +/* Copyright (c) 2017-2019 FIRST. All Rights Reserved. */ /* Open Source Software - may be modified and shared by FRC teams. The code */ /* must be accompanied by the FIRST BSD license file in the root directory of */ /* the project. */ @@ -16,6 +16,12 @@ void HALSIM_WaitForProgramStart(void); void HALSIM_SetProgramStarted(void); HAL_Bool HALSIM_GetProgramStarted(void); void HALSIM_RestartTiming(void); + +typedef int32_t (*HALSIM_SendErrorHandler)( + HAL_Bool isError, int32_t errorCode, HAL_Bool isLVCode, const char* details, + const char* location, const char* callStack, HAL_Bool printMsg); +void HALSIM_SetSendError(HALSIM_SendErrorHandler handler); + } // extern "C" #endif diff --git a/hal/src/main/native/sim/DriverStation.cpp b/hal/src/main/native/sim/DriverStation.cpp index 0c9c028405..0146d19b92 100644 --- a/hal/src/main/native/sim/DriverStation.cpp +++ b/hal/src/main/native/sim/DriverStation.cpp @@ -1,5 +1,5 @@ /*----------------------------------------------------------------------------*/ -/* Copyright (c) 2017-2018 FIRST. All Rights Reserved. */ +/* Copyright (c) 2017-2019 FIRST. All Rights Reserved. */ /* Open Source Software - may be modified and shared by FRC teams. The code */ /* must be accompanied by the FIRST BSD license file in the root directory of */ /* the project. */ @@ -28,6 +28,7 @@ static wpi::condition_variable* newDSDataAvailableCond; static wpi::mutex newDSDataAvailableMutex; static int newDSDataAvailableCounter{0}; static std::atomic_bool isFinalized{false}; +static HALSIM_SendErrorHandler sendErrorHandler{nullptr}; namespace hal { namespace init { @@ -44,6 +45,9 @@ extern "C" { int32_t HAL_SendError(HAL_Bool isError, int32_t errorCode, HAL_Bool isLVCode, const char* details, const char* location, const char* callStack, HAL_Bool printMsg) { + if (sendErrorHandler) + return sendErrorHandler(isError, errorCode, isLVCode, details, location, + callStack, printMsg); // Avoid flooding console by keeping track of previous 5 error // messages and only printing again if they're longer than 1 second old. static constexpr int KEEP_MSGS = 5;