Skip to content

Commit

Permalink
Forces exceptions to throw on HAL handle creation functions
Browse files Browse the repository at this point in the history
  • Loading branch information
ThadHouse committed Aug 21, 2016
1 parent 40365fa commit 1f73add
Show file tree
Hide file tree
Showing 11 changed files with 39 additions and 16 deletions.
2 changes: 1 addition & 1 deletion wpilibj/src/athena/cpp/lib/AnalogGyroJNI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion wpilibj/src/athena/cpp/lib/CanTalonJNI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion wpilibj/src/athena/cpp/lib/CounterJNI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion wpilibj/src/athena/cpp/lib/EncoderJNI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
18 changes: 18 additions & 0 deletions wpilibj/src/athena/cpp/lib/HALUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,24 @@ void ReportError(JNIEnv *env, int32_t status, int32_t minRange, int32_t maxRange
}
}

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) {
Expand Down
19 changes: 12 additions & 7 deletions wpilibj/src/athena/cpp/lib/HALUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion wpilibj/src/athena/cpp/lib/I2CJNI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/*
Expand Down
2 changes: 1 addition & 1 deletion wpilibj/src/athena/cpp/lib/InterruptJNI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,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;
}

Expand Down
2 changes: 1 addition & 1 deletion wpilibj/src/athena/cpp/lib/NotifierJNI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,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;
}
Expand Down
2 changes: 1 addition & 1 deletion wpilibj/src/athena/cpp/lib/SPIJNI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/*
Expand Down
2 changes: 1 addition & 1 deletion wpilibj/src/athena/cpp/lib/SerialPortJNI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/*
Expand Down

0 comments on commit 1f73add

Please sign in to comment.