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

@@ -6,6 +6,7 @@
#include "HAL/CAN.hpp"
#include "FRC_NetworkCommunication/CANSessionMux.h"
#include "HALUtil.h"
// set the logging level
//TLogLevel canJNILogLevel = logDEBUG;
@@ -15,19 +16,20 @@ TLogLevel canJNILogLevel = logERROR;
if (level > canJNILogLevel) ; \
else Log().Get(level)
extern "C" {
/*
* Class: edu_wpi_first_wpilibj_can_CANJNI
* Method: FRCNetworkCommunicationCANSessionMuxSendMessage
* Signature: (ILjava/nio/ByteBuffer;ILjava/nio/IntBuffer;)V
* Signature: (ILjava/nio/ByteBuffer;I)V
*/
JNIEXPORT void JNICALL Java_edu_wpi_first_wpilibj_can_CANJNI_FRCNetworkCommunicationCANSessionMuxSendMessage
(JNIEnv * env, jclass, jint messageID, jobject data, jint periodMs, jobject status)
(JNIEnv * env, jclass, jint messageID, jobject data, jint periodMs)
{
CANJNI_LOG(logDEBUG) << "Calling CANJNI FRCNetworkCommunicationCANSessionMuxSendMessage";
uint8_t *dataBuffer = (uint8_t *)(data? env->GetDirectBufferAddress(data) : 0);
uint8_t dataSize = (uint8_t)(data? env->GetDirectBufferCapacity(data) : 0);
int32_t *statusPtr = (int32_t *)env->GetDirectBufferAddress(status);
CANJNI_LOG(logDEBUG) << "Message ID " << std::hex << messageID;
@@ -52,10 +54,11 @@ JNIEXPORT void JNICALL Java_edu_wpi_first_wpilibj_can_CANJNI_FRCNetworkCommunica
CANJNI_LOG(logDEBUG) << "Period: " << periodMs;
*statusPtr = 0;
FRC_NetworkCommunication_CANSessionMux_sendMessage(messageID, dataBuffer, dataSize, periodMs, statusPtr);
int32_t status = 0;
FRC_NetworkCommunication_CANSessionMux_sendMessage(messageID, dataBuffer, dataSize, periodMs, &status);
CANJNI_LOG(logDEBUG) << "Status: " << *statusPtr;
CANJNI_LOG(logDEBUG) << "Status: " << status;
CheckCANStatus(env, status, messageID);
}
static uint8_t buffer[8];
@@ -63,21 +66,20 @@ static uint8_t buffer[8];
/*
* Class: edu_wpi_first_wpilibj_can_CANJNI
* Method: FRCNetworkCommunicationCANSessionMuxReceiveMessage
* Signature: (Ljava/nio/IntBuffer;ILjava/nio/ByteBuffer;Ljava/nio/IntBuffer;)Ljava/nio/ByteBuffer;
* Signature: (Ljava/nio/IntBuffer;ILjava/nio/ByteBuffer;)Ljava/nio/ByteBuffer;
*/
JNIEXPORT jobject JNICALL Java_edu_wpi_first_wpilibj_can_CANJNI_FRCNetworkCommunicationCANSessionMuxReceiveMessage
(JNIEnv * env, jclass, jobject messageID, jint messageIDMask, jobject timeStamp, jobject status)
(JNIEnv * env, jclass, jobject messageID, jint messageIDMask, jobject timeStamp)
{
CANJNI_LOG(logDEBUG) << "Calling CANJNI FRCNetworkCommunicationCANSessionMuxReceiveMessage";
uint32_t *messageIDPtr = (uint32_t *)env->GetDirectBufferAddress(messageID);
uint32_t *timeStampPtr = (uint32_t *)env->GetDirectBufferAddress(timeStamp);
int32_t *statusPtr = (int32_t *)env->GetDirectBufferAddress(status);
uint8_t dataSize = 0;
*statusPtr = 0;
FRC_NetworkCommunication_CANSessionMux_receiveMessage(messageIDPtr, messageIDMask, buffer, &dataSize, timeStampPtr, statusPtr);
int32_t status = 0;
FRC_NetworkCommunication_CANSessionMux_receiveMessage(messageIDPtr, messageIDMask, buffer, &dataSize, timeStampPtr, &status);
CANJNI_LOG(logDEBUG) << "Message ID " << std::hex << *messageIDPtr;
@@ -94,7 +96,10 @@ JNIEXPORT jobject JNICALL Java_edu_wpi_first_wpilibj_can_CANJNI_FRCNetworkCommun
}
CANJNI_LOG(logDEBUG) << "Timestamp: " << *timeStampPtr;
CANJNI_LOG(logDEBUG) << "Status: " << *statusPtr;
CANJNI_LOG(logDEBUG) << "Status: " << status;
if (!CheckCANStatus(env, status, *messageIDPtr)) return nullptr;
return env->NewDirectByteBuffer(buffer, dataSize);
}
} // extern "C"