Implements Better Error Messages from the HAL (#172)

* Makes the HAL provide a better error message for certain things.

* Makes Java error messages better

* Updates C++ errors.

* Moves handles header folder to HAL directory
This commit is contained in:
Thad House
2016-07-13 20:29:28 -07:00
committed by Peter Johnson
parent 05c00430b3
commit d2aa168f66
51 changed files with 175 additions and 70 deletions

View File

@@ -13,6 +13,7 @@
#include "HAL/AnalogGyro.h"
#include "HALUtil.h"
#include "HAL/handles/HandlesInternal.h"
// set the logging level
TLogLevel analogGyroJNILogLevel = logWARNING;
@@ -37,6 +38,8 @@ JNIEXPORT jint JNICALL Java_edu_wpi_first_wpilibj_hal_AnalogGyroJNI_initializeAn
HAL_GyroHandle handle = HAL_InitializeAnalogGyro((HAL_AnalogInputHandle)id, &status);
ANALOGGYROJNI_LOG(logDEBUG) << "Status = " << status;
ANALOGGYROJNI_LOG(logDEBUG) << "Gyro Handle = " << handle;
// Analog input does range checking, so we don't need to do so.
CheckStatus(env, status);
return (jint) handle;
}

View File

@@ -15,7 +15,9 @@
#include "HAL/AnalogOutput.h"
#include "HAL/AnalogAccumulator.h"
#include "HAL/AnalogTrigger.h"
#include "HAL/Ports.h"
#include "HALUtil.h"
#include "HAL/handles/HandlesInternal.h"
// set the logging level
TLogLevel analogJNILogLevel = logWARNING;
@@ -41,7 +43,8 @@ Java_edu_wpi_first_wpilibj_hal_AnalogJNI_initializeAnalogInputPort(
auto analog = HAL_InitializeAnalogInputPort((HAL_PortHandle)id, &status);
ANALOGJNI_LOG(logDEBUG) << "Status = " << status;
ANALOGJNI_LOG(logDEBUG) << "Analog Handle = " << analog;
CheckStatus(env, status);
CheckStatusRange(env, 0, HAL_GetNumAnalogInputs(),
hal::getPortHandlePin((HAL_PortHandle)id), status);
return (jint)analog;
}
@@ -70,7 +73,8 @@ Java_edu_wpi_first_wpilibj_hal_AnalogJNI_initializeAnalogOutputPort(
HAL_AnalogOutputHandle analog = HAL_InitializeAnalogOutputPort((HAL_PortHandle)id, &status);
ANALOGJNI_LOG(logDEBUG) << "Status = " << status;
ANALOGJNI_LOG(logDEBUG) << "Analog Handle = " << analog;
CheckStatus(env, status);
CheckStatusRange(env, 0, HAL_GetNumAnalogOutputs(),
hal::getPortHandlePin((HAL_PortHandle)id), status);
return (jlong)analog;
}

View File

@@ -17,7 +17,7 @@
extern "C" {
inline bool CheckCTRStatus(JNIEnv *env, CTR_Code status) {
if (status != CTR_OKAY) ReportError(env, (int32_t)status, false);
if (status != CTR_OKAY) ReportError(env, (int32_t)status, 0, 0, 0, false);
return status == CTR_OKAY;
}

View File

@@ -22,9 +22,7 @@ Java_edu_wpi_first_wpilibj_hal_CompressorJNI_initializeCompressor(
JNIEnv *env, jclass, jbyte module) {
int32_t status = 0;
auto handle = HAL_InitializeCompressor(module, &status);
if (status == PARAMETER_OUT_OF_RANGE) {
ThrowBoundaryException(env, module, 0, HAL_GetNumPCMModules());
}
CheckStatusRange(env, 0, HAL_GetNumPCMModules(), module, status);
return (jint)handle;
}

View File

@@ -14,6 +14,8 @@
#include "HAL/DIO.h"
#include "HAL/PWM.h"
#include "HALUtil.h"
#include "HAL/Ports.h"
#include "HAL/handles/HandlesInternal.h"
// set the logging level
TLogLevel dioJNILogLevel = logWARNING;
@@ -41,7 +43,8 @@ Java_edu_wpi_first_wpilibj_hal_DIOJNI_initializeDIOPort(
auto dio = HAL_InitializeDIOPort((HAL_PortHandle)id, (uint8_t)input, &status);
DIOJNI_LOG(logDEBUG) << "Status = " << status;
DIOJNI_LOG(logDEBUG) << "DIO Handle = " << dio;
CheckStatus(env, status);
CheckStatusRange(env, 0, HAL_GetNumDigitalPins(),
hal::getPortHandlePin((HAL_PortHandle)id), status);
return (jint)dio;
}

View File

@@ -120,10 +120,13 @@ static void GetStackTrace(JNIEnv *env, std::string &res, std::string &func) {
env->DeleteLocalRef(stackTrace);
}
void ThrowAllocationException(JNIEnv *env, int32_t status) {
void ThrowAllocationException(JNIEnv *env, int32_t minRange, int32_t maxRange,
int32_t requestedValue, int32_t status) {
const char *message = HAL_GetErrorMessage(status);
char *buf = new char[strlen(message) + 30];
sprintf(buf, " Code: $%d. %s", status, message);
char *buf = new char[strlen(message) + 100];
sprintf(buf,
" Code: $%d. %s, Minimum Value: %d, Maximum Value: %d, Requested Value: %d",
status, message, minRange, maxRange, requestedValue);
env->ThrowNew(allocationExCls, buf);
delete[] buf;
}
@@ -136,11 +139,13 @@ void ThrowHalHandleException(JNIEnv *env, int32_t status) {
delete[] buf;
}
void ReportError(JNIEnv *env, int32_t status, bool do_throw) {
void ReportError(JNIEnv *env, int32_t status, int32_t minRange, int32_t maxRange,
int32_t requestedValue, bool do_throw) {
if (status == 0) return;
if (status == NO_AVAILABLE_RESOURCES ||
status == RESOURCE_IS_ALLOCATED) {
ThrowAllocationException(env, status);
status == RESOURCE_IS_ALLOCATED ||
status == RESOURCE_OUT_OF_RANGE) {
ThrowAllocationException(env, minRange, maxRange, requestedValue, status);
}
if (status == HAL_HANDLE_ERROR) {
ThrowHalHandleException(env, status);

View File

@@ -14,10 +14,19 @@
extern JavaVM *jvm;
void ReportError(JNIEnv *env, int32_t status, bool do_throw = true);
void ReportError(JNIEnv *env, int32_t status, int32_t minRange, int32_t maxRange,
int32_t requestedValue, bool do_throw = true);
inline bool CheckStatus(JNIEnv *env, int32_t status, bool do_throw = true) {
if (status != 0) ReportError(env, status, do_throw);
if (status != 0) ReportError(env, status, 0, 0, 0, do_throw);
return status == 0;
}
inline bool CheckStatusRange(JNIEnv *env, int32_t status, int32_t minRange,
int32_t maxRange, int32_t requestedValue,
bool do_throw = true) {
if (status != 0) ReportError(env, status, minRange, maxRange, requestedValue,
do_throw);
return status == 0;
}

View File

@@ -6,6 +6,7 @@
/*----------------------------------------------------------------------------*/
#include "HAL/PDP.h"
#include "HAL/Ports.h"
#include "HALUtil.h"
#include "edu_wpi_first_wpilibj_hal_PDPJNI.h"
@@ -20,7 +21,7 @@ JNIEXPORT void JNICALL Java_edu_wpi_first_wpilibj_hal_PDPJNI_initializePDP(
JNIEnv *env, jclass, jint module) {
int32_t status = 0;
HAL_InitializePDP(module, &status);
CheckStatus(env, status);
CheckStatusRange(env, 0, HAL_GetNumPDPModules(), module, status);
}
/*

View File

@@ -13,7 +13,9 @@
#include "HAL/DIO.h"
#include "HAL/PWM.h"
#include "HAL/Ports.h"
#include "HALUtil.h"
#include "HAL/handles/HandlesInternal.h"
// set the logging level
TLogLevel pwmJNILogLevel = logWARNING;
@@ -40,7 +42,8 @@ Java_edu_wpi_first_wpilibj_hal_PWMJNI_initializePWMPort(
auto pwm = HAL_InitializePWMPort((HAL_PortHandle)id, &status);
PWMJNI_LOG(logDEBUG) << "Status = " << status;
PWMJNI_LOG(logDEBUG) << "PWM Handle = " << pwm;
CheckStatus(env, status);
CheckStatusRange(env, 0, HAL_GetNumPWMPins(),
hal::getPortHandlePin((HAL_PortHandle)id), status);
return (jint)pwm;
}

View File

@@ -12,7 +12,9 @@
#include "edu_wpi_first_wpilibj_hal_RelayJNI.h"
#include "HAL/Relay.h"
#include "HAL/Ports.h"
#include "HALUtil.h"
#include "HAL/handles/HandlesInternal.h"
// set the logging level
TLogLevel relayJNILogLevel = logWARNING;
@@ -39,6 +41,8 @@ JNIEXPORT jint JNICALL Java_edu_wpi_first_wpilibj_hal_RelayJNI_initializeRelayPo
HAL_RelayHandle handle = HAL_InitializeRelayPort((HAL_PortHandle)id, (uint8_t) fwd, &status);
RELAYJNI_LOG(logDEBUG) << "Status = " << status;
RELAYJNI_LOG(logDEBUG) << "Relay Handle = " << handle;
CheckStatusRange(env, 0, HAL_GetNumRelayPins(),
hal::getPortHandlePin((HAL_PortHandle)id), status);
return (jint) handle;
}

View File

@@ -7,6 +7,7 @@
#include <jni.h>
#include "HAL/HAL.h"
#include "HAL/handles/HandlesInternal.h"
#include "Log.h"
#include "edu_wpi_first_wpilibj_hal_SolenoidJNI.h"
@@ -30,19 +31,21 @@ extern "C" {
*/
JNIEXPORT jint JNICALL
Java_edu_wpi_first_wpilibj_hal_SolenoidJNI_initializeSolenoidPort(
JNIEnv *env, jclass, jint port_handle) {
JNIEnv *env, jclass, jint id) {
SOLENOIDJNI_LOG(logDEBUG) << "Calling SolenoidJNI initializeSolenoidPort";
SOLENOIDJNI_LOG(logDEBUG) << "Port Handle = " << (HAL_PortHandle)port_handle;
SOLENOIDJNI_LOG(logDEBUG) << "Port Handle = " << (HAL_PortHandle)id;
int32_t status = 0;
HAL_SolenoidHandle handle =
HAL_InitializeSolenoidPort((HAL_PortHandle)port_handle, &status);
HAL_InitializeSolenoidPort((HAL_PortHandle)id, &status);
SOLENOIDJNI_LOG(logDEBUG) << "Status = " << status;
SOLENOIDJNI_LOG(logDEBUG) << "Solenoid Port Handle = " << handle;
CheckStatus(env, status);
// Use solenoid pins, as we have to pick one.
CheckStatusRange(env, 0, HAL_GetNumSolenoidPins(),
hal::getPortHandlePin((HAL_PortHandle)id), status);;
return (jint)handle;
}