diff --git a/wpilibj/src/athena/cpp/lib/AnalogGyroJNI.cpp b/wpilibj/src/athena/cpp/lib/AnalogGyroJNI.cpp index 1d6da1b3f1..d8c6407d94 100644 --- a/wpilibj/src/athena/cpp/lib/AnalogGyroJNI.cpp +++ b/wpilibj/src/athena/cpp/lib/AnalogGyroJNI.cpp @@ -39,7 +39,7 @@ JNIEXPORT jint JNICALL Java_edu_wpi_first_wpilibj_hal_AnalogGyroJNI_initializeAn 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); + CheckStatusForceThrow(env, status); return (jint) handle; } diff --git a/wpilibj/src/athena/cpp/lib/CanTalonJNI.cpp b/wpilibj/src/athena/cpp/lib/CanTalonJNI.cpp index 1829144ae2..5773863ce4 100644 --- a/wpilibj/src/athena/cpp/lib/CanTalonJNI.cpp +++ b/wpilibj/src/athena/cpp/lib/CanTalonJNI.cpp @@ -17,7 +17,7 @@ extern "C" { inline bool CheckCTRStatus(JNIEnv *env, CTR_Code status) { - if (status != CTR_OKAY) ReportError(env, (int32_t)status, 0, 0, 0, false); + if (status != CTR_OKAY) ReportError(env, (int32_t)status, false); return status == CTR_OKAY; } diff --git a/wpilibj/src/athena/cpp/lib/CounterJNI.cpp b/wpilibj/src/athena/cpp/lib/CounterJNI.cpp index 87f943e0fc..b14ddb3ff2 100644 --- a/wpilibj/src/athena/cpp/lib/CounterJNI.cpp +++ b/wpilibj/src/athena/cpp/lib/CounterJNI.cpp @@ -43,7 +43,7 @@ Java_edu_wpi_first_wpilibj_hal_CounterJNI_initializeCounter( COUNTERJNI_LOG(logDEBUG) << "Index = " << *indexPtr; COUNTERJNI_LOG(logDEBUG) << "Status = " << status; COUNTERJNI_LOG(logDEBUG) << "COUNTER Handle = " << counter; - CheckStatus(env, status); + CheckStatusForceThrow(env, status); return (jint)counter; } diff --git a/wpilibj/src/athena/cpp/lib/EncoderJNI.cpp b/wpilibj/src/athena/cpp/lib/EncoderJNI.cpp index d8e07451db..0f0129bb8b 100644 --- a/wpilibj/src/athena/cpp/lib/EncoderJNI.cpp +++ b/wpilibj/src/athena/cpp/lib/EncoderJNI.cpp @@ -53,7 +53,7 @@ Java_edu_wpi_first_wpilibj_hal_EncoderJNI_initializeEncoder( ENCODERJNI_LOG(logDEBUG) << "Status = " << status; ENCODERJNI_LOG(logDEBUG) << "ENCODER Handle = " << encoder; - CheckStatus(env, status); + CheckStatusForceThrow(env, status); return (jint)encoder; } diff --git a/wpilibj/src/athena/cpp/lib/HALUtil.cpp b/wpilibj/src/athena/cpp/lib/HALUtil.cpp index 24368ffe32..62e5845dda 100644 --- a/wpilibj/src/athena/cpp/lib/HALUtil.cpp +++ b/wpilibj/src/athena/cpp/lib/HALUtil.cpp @@ -141,14 +141,8 @@ void ThrowHalHandleException(JNIEnv *env, int32_t status) { delete[] buf; } -void ReportError(JNIEnv *env, int32_t status, int32_t minRange, int32_t maxRange, - int32_t requestedValue, bool do_throw) { +void ReportError(JNIEnv *env, int32_t status, bool do_throw) { if (status == 0) return; - if (status == NO_AVAILABLE_RESOURCES || - status == RESOURCE_IS_ALLOCATED || - status == RESOURCE_OUT_OF_RANGE) { - ThrowAllocationException(env, minRange, maxRange, requestedValue, status); - } if (status == HAL_HANDLE_ERROR) { ThrowHalHandleException(env, status); } @@ -166,7 +160,25 @@ void ReportError(JNIEnv *env, int32_t status, int32_t minRange, int32_t maxRange } } -void ReportCANError(JNIEnv *env, int32_t status, int32_t message_id) { +void ThrowError(JNIEnv *env, int32_t status, int32_t minRange, int32_t maxRange, + int32_t requestedValue) { + if (status == 0) return; + if (status == NO_AVAILABLE_RESOURCES || + status == RESOURCE_IS_ALLOCATED || + status == RESOURCE_OUT_OF_RANGE) { + ThrowAllocationException(env, minRange, maxRange, requestedValue, status); + } + if (status == HAL_HANDLE_ERROR) { + ThrowHalHandleException(env, status); + } + const char *message = HAL_GetErrorMessage(status); + char *buf = new char[strlen(message) + 30]; + sprintf(buf, " Code: %d. %s", status, message); + env->ThrowNew(runtimeExCls, buf); + delete[] buf; +} + +void ReportCANError(JNIEnv *env, int32_t status, int message_id) { if (status >= 0) return; switch (status) { case kRioStatusSuccess: diff --git a/wpilibj/src/athena/cpp/lib/HALUtil.h b/wpilibj/src/athena/cpp/lib/HALUtil.h index 75524fed3d..36b9bf39d5 100644 --- a/wpilibj/src/athena/cpp/lib/HALUtil.h +++ b/wpilibj/src/athena/cpp/lib/HALUtil.h @@ -14,19 +14,24 @@ extern JavaVM *jvm; -void ReportError(JNIEnv *env, int32_t status, int32_t minRange, int32_t maxRange, - int32_t requestedValue, bool do_throw = true); +void ReportError(JNIEnv *env, int32_t status, bool do_throw = true); + +void ThrowError(JNIEnv *env, int32_t status, int32_t minRange, int32_t maxRange, + int32_t requestedValue); inline bool CheckStatus(JNIEnv *env, int32_t status, bool do_throw = true) { - if (status != 0) ReportError(env, status, 0, 0, 0, do_throw); + if (status != 0) ReportError(env, status, 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); + int32_t maxRange, int32_t requestedValue) { + if (status != 0) ThrowError(env, status, minRange, maxRange, requestedValue); + return status == 0; +} + +inline bool CheckStatusForceThrow(JNIEnv *env, int32_t status) { + if (status != 0) ThrowError(env, status, 0, 0, 0); return status == 0; } diff --git a/wpilibj/src/athena/cpp/lib/I2CJNI.cpp b/wpilibj/src/athena/cpp/lib/I2CJNI.cpp index 51fae582e9..35d45eb9a9 100644 --- a/wpilibj/src/athena/cpp/lib/I2CJNI.cpp +++ b/wpilibj/src/athena/cpp/lib/I2CJNI.cpp @@ -37,7 +37,7 @@ JNIEXPORT void JNICALL Java_edu_wpi_first_wpilibj_hal_I2CJNI_i2CInitialize( int32_t status = 0; HAL_InitializeI2C(value, &status); I2CJNI_LOG(logDEBUG) << "Status = " << status; - CheckStatus(env, status); + CheckStatusForceThrow(env, status); } /* diff --git a/wpilibj/src/athena/cpp/lib/InterruptJNI.cpp b/wpilibj/src/athena/cpp/lib/InterruptJNI.cpp index 30e8a07651..3f11ccbc09 100644 --- a/wpilibj/src/athena/cpp/lib/InterruptJNI.cpp +++ b/wpilibj/src/athena/cpp/lib/InterruptJNI.cpp @@ -134,7 +134,7 @@ Java_edu_wpi_first_wpilibj_hal_InterruptJNI_initializeInterrupts( INTERRUPTJNI_LOG(logDEBUG) << "Interrupt Handle = " << interrupt; INTERRUPTJNI_LOG(logDEBUG) << "Status = " << status; - CheckStatus(env, status); + CheckStatusForceThrow(env, status); return (jint)interrupt; } diff --git a/wpilibj/src/athena/cpp/lib/NotifierJNI.cpp b/wpilibj/src/athena/cpp/lib/NotifierJNI.cpp index bc7070f80f..bb8bed3627 100644 --- a/wpilibj/src/athena/cpp/lib/NotifierJNI.cpp +++ b/wpilibj/src/athena/cpp/lib/NotifierJNI.cpp @@ -150,7 +150,7 @@ Java_edu_wpi_first_wpilibj_hal_NotifierJNI_initializeNotifier( NOTIFIERJNI_LOG(logDEBUG) << "Notifier Handle = " << notifierHandle; NOTIFIERJNI_LOG(logDEBUG) << "Status = " << status; - if (notifierHandle <= 0 || !CheckStatus(env, status)) { + if (notifierHandle <= 0 || !CheckStatusForceThrow(env, status)) { // something went wrong in HAL, clean up delete notify; } diff --git a/wpilibj/src/athena/cpp/lib/SPIJNI.cpp b/wpilibj/src/athena/cpp/lib/SPIJNI.cpp index 3988847cc8..5c64d0d09e 100644 --- a/wpilibj/src/athena/cpp/lib/SPIJNI.cpp +++ b/wpilibj/src/athena/cpp/lib/SPIJNI.cpp @@ -37,7 +37,7 @@ JNIEXPORT void JNICALL Java_edu_wpi_first_wpilibj_hal_SPIJNI_spiInitialize( int32_t status = 0; HAL_InitializeSPI(port, &status); SPIJNI_LOG(logDEBUG) << "Status = " << status; - CheckStatus(env, status); + CheckStatusForceThrow(env, status); } /* diff --git a/wpilibj/src/athena/cpp/lib/SerialPortJNI.cpp b/wpilibj/src/athena/cpp/lib/SerialPortJNI.cpp index c92aeb637c..fcc669e7fa 100644 --- a/wpilibj/src/athena/cpp/lib/SerialPortJNI.cpp +++ b/wpilibj/src/athena/cpp/lib/SerialPortJNI.cpp @@ -38,7 +38,7 @@ Java_edu_wpi_first_wpilibj_hal_SerialPortJNI_serialInitializePort( int32_t status = 0; HAL_InitializeSerialPort(port, &status); SERIALJNI_LOG(logDEBUG) << "Status = " << status; - CheckStatus(env, status); + CheckStatusForceThrow(env, status); } /*