mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-23 01:21:42 +00:00
Forces exceptions to throw on HAL handle creation functions (#209)
Fixes #199
This commit is contained in:
committed by
Peter Johnson
parent
81e63ea3a5
commit
a656207220
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
Reference in New Issue
Block a user