Simplify JNI interfaces.

These changes both simplify the Java code and improve performance across the
JNI boundary.

This also fixes the AnalogCrossConnectTest by adding delays to
setInterruptHigh() and setInterruptLow() to ensure the change in voltage has
time to propagate and extends the timeouts in AbstractInterruptTest.

Detailed changes:

Hoisted status checks to C.  This avoids the need to create direct byte
buffers (expensive) and significantly simplifies the Java code.  The C code
now directly generates the exception or reports the error to the DS.

The JVM pointer is now a global across the JNI, initialized by the OnLoad
function, avoiding the need for some of the class-specific initializers to
get this pointer for callbacks.

Opaque pointers (such as ports) are now passed as long values rather than
with a ByteBuffer wrapper.

Added extern "C" to source files.  This allows earlier detection of JNI
definition mismatches to the Java source headers.

Changed JNI signatures to more closely match HAL signatures (in particular,
boolean is now universally used instead of byte for HAL bool, which cleans
up mapping back and forth to 1/0 from true/false).

Change-Id: I4ea0032cabb0871cd74106a3a70d947258c29d2d
This commit is contained in:
Peter Johnson
2015-11-01 09:11:52 -08:00
committed by Brad Miller (WPI)
parent 927400a43c
commit 7023013c4b
69 changed files with 2232 additions and 3118 deletions

View File

@@ -5,6 +5,7 @@
#include "edu_wpi_first_wpilibj_hal_RelayJNI.h"
#include "HAL/Digital.hpp"
#include "HALUtil.h"
// set the logging level
TLogLevel relayJNILogLevel = logWARNING;
@@ -13,80 +14,76 @@ TLogLevel relayJNILogLevel = logWARNING;
if (level > relayJNILogLevel) ; \
else Log().Get(level)
extern "C" {
/*
* Class: edu_wpi_first_wpilibj_hal_RelayJNI
* Method: setRelayForward
* Signature: (Ljava/nio/ByteBuffer;BLjava/nio/IntBuffer;)V
* Signature: (JZ)V
*/
JNIEXPORT void JNICALL Java_edu_wpi_first_wpilibj_hal_RelayJNI_setRelayForward
(JNIEnv * env, jclass, jobject id, jbyte value, jobject status)
(JNIEnv * env, jclass, jlong id, jboolean value)
{
RELAYJNI_LOG(logDEBUG) << "Calling RELAYJNI setRelayForward";
void ** javaId = (void**)env->GetDirectBufferAddress(id);
RELAYJNI_LOG(logDEBUG) << "Port Ptr = " << *javaId;
RELAYJNI_LOG(logDEBUG) << "Port Ptr = " << (void*)id;
RELAYJNI_LOG(logDEBUG) << "Flag = " << (jint)value;
jint * statusPtr = (jint*)env->GetDirectBufferAddress(status);
RELAYJNI_LOG(logDEBUG) << "Status Ptr = " << statusPtr;
*statusPtr = 0;
setRelayForward(*javaId, value, statusPtr);
RELAYJNI_LOG(logDEBUG) << "Status = " << *statusPtr;
int32_t status = 0;
setRelayForward((void*)id, value, &status);
RELAYJNI_LOG(logDEBUG) << "Status = " << status;
CheckStatus(env, status);
}
/*
* Class: edu_wpi_first_wpilibj_hal_RelayJNI
* Method: setRelayReverse
* Signature: (Ljava/nio/ByteBuffer;BLjava/nio/IntBuffer;)V
* Signature: (JZ)V
*/
JNIEXPORT void JNICALL Java_edu_wpi_first_wpilibj_hal_RelayJNI_setRelayReverse
(JNIEnv * env, jclass, jobject id, jbyte value, jobject status)
(JNIEnv * env, jclass, jlong id, jboolean value)
{
RELAYJNI_LOG(logDEBUG) << "Calling RELAYJNI setRelayReverse";
void ** javaId = (void**)env->GetDirectBufferAddress(id);
RELAYJNI_LOG(logDEBUG) << "Port Ptr = " << *javaId;
RELAYJNI_LOG(logDEBUG) << "Port Ptr = " << (void*)id;
RELAYJNI_LOG(logDEBUG) << "Flag = " << (jint)value;
jint * statusPtr = (jint*)env->GetDirectBufferAddress(status);
RELAYJNI_LOG(logDEBUG) << "Status Ptr = " << statusPtr;
*statusPtr = 0;
setRelayReverse(*javaId, value, statusPtr);
RELAYJNI_LOG(logDEBUG) << "Status = " << *statusPtr;
int32_t status = 0;
setRelayReverse((void*)id, value, &status);
RELAYJNI_LOG(logDEBUG) << "Status = " << status;
CheckStatus(env, status);
}
/*
* Class: edu_wpi_first_wpilibj_hal_RelayJNI
* Method: getRelayForward
* Signature: (Ljava/nio/ByteBuffer;Ljava/nio/IntBuffer;)B
* Signature: (J)Z
*/
JNIEXPORT jbyte JNICALL Java_edu_wpi_first_wpilibj_hal_RelayJNI_getRelayForward
(JNIEnv * env, jclass, jobject id, jobject status)
JNIEXPORT jboolean JNICALL Java_edu_wpi_first_wpilibj_hal_RelayJNI_getRelayForward
(JNIEnv * env, jclass, jlong id)
{
RELAYJNI_LOG(logDEBUG) << "Calling RELAYJNI getRelayForward";
void ** javaId = (void**)env->GetDirectBufferAddress(id);
RELAYJNI_LOG(logDEBUG) << "Port Ptr = " << *javaId;
jint * statusPtr = (jint*)env->GetDirectBufferAddress(status);
RELAYJNI_LOG(logDEBUG) << "Status Ptr = " << statusPtr;
*statusPtr = 0;
jbyte returnValue = getRelayForward(*javaId, statusPtr);
RELAYJNI_LOG(logDEBUG) << "Status = " << *statusPtr;
RELAYJNI_LOG(logDEBUG) << "Port Ptr = " << (void*)id;
int32_t status = 0;
jboolean returnValue = getRelayForward((void*)id, &status);
RELAYJNI_LOG(logDEBUG) << "Status = " << status;
RELAYJNI_LOG(logDEBUG) << "getRelayForwardResult = " << (jint)returnValue;
CheckStatus(env, status);
return returnValue;
}
/*
* Class: edu_wpi_first_wpilibj_hal_RelayJNI
* Method: getRelayReverse
* Signature: (Ljava/nio/ByteBuffer;Ljava/nio/IntBuffer;)B
* Signature: (J)Z
*/
JNIEXPORT jbyte JNICALL Java_edu_wpi_first_wpilibj_hal_RelayJNI_getRelayReverse
(JNIEnv * env, jclass, jobject id, jobject status)
JNIEXPORT jboolean JNICALL Java_edu_wpi_first_wpilibj_hal_RelayJNI_getRelayReverse
(JNIEnv * env, jclass, jlong id)
{
RELAYJNI_LOG(logDEBUG) << "Calling RELAYJNI getRelayReverse";
void ** javaId = (void**)env->GetDirectBufferAddress(id);
RELAYJNI_LOG(logDEBUG) << "Port Ptr = " << *javaId;
jint * statusPtr = (jint*)env->GetDirectBufferAddress(status);
RELAYJNI_LOG(logDEBUG) << "Status Ptr = " << statusPtr;
*statusPtr = 0;
jbyte returnValue = getRelayReverse(*javaId, statusPtr);
RELAYJNI_LOG(logDEBUG) << "Status = " << *statusPtr;
RELAYJNI_LOG(logDEBUG) << "Port Ptr = " << (void*)id;
int32_t status = 0;
jboolean returnValue = getRelayReverse((void*)id, &status);
RELAYJNI_LOG(logDEBUG) << "Status = " << status;
RELAYJNI_LOG(logDEBUG) << "getRelayReverseResult = " << (jint)returnValue;
CheckStatus(env, status);
return returnValue;
}
} // extern "C"