Files
allwpilib/wpilibj/wpilibJavaJNI/lib/DIOJNI.cpp
Peter Johnson 7023013c4b 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
2015-11-06 09:04:22 -08:00

198 lines
5.5 KiB
C++

#include <jni.h>
#include <assert.h>
#include "Log.hpp"
#include "edu_wpi_first_wpilibj_hal_DIOJNI.h"
#include "HAL/Digital.hpp"
#include "HALUtil.h"
// set the logging level
TLogLevel dioJNILogLevel = logWARNING;
#define DIOJNI_LOG(level) \
if (level > dioJNILogLevel) ; \
else Log().Get(level)
extern "C" {
/*
* Class: edu_wpi_first_wpilibj_hal_DIOJNI
* Method: initializeDigitalPort
* Signature: (J)J;
*/
JNIEXPORT jlong JNICALL Java_edu_wpi_first_wpilibj_hal_DIOJNI_initializeDigitalPort
(JNIEnv * env, jclass, jlong id)
{
DIOJNI_LOG(logDEBUG) << "Calling DIOJNI initializeDigitalPort";
DIOJNI_LOG(logDEBUG) << "Port Ptr = " << (void*)id;
int32_t status = 0;
void* dio = initializeDigitalPort((void*)id, &status);
DIOJNI_LOG(logDEBUG) << "Status = " << status;
DIOJNI_LOG(logDEBUG) << "DIO Ptr = " << dio;
CheckStatus(env, status);
return (jlong)dio;
}
/*
* Class: edu_wpi_first_wpilibj_hal_DIOJNI
* Method: allocateDIO
* Signature: (JZ)Z
*/
JNIEXPORT jboolean JNICALL Java_edu_wpi_first_wpilibj_hal_DIOJNI_allocateDIO
(JNIEnv * env, jclass, jlong id, jboolean value)
{
DIOJNI_LOG(logDEBUG) << "Calling DIOJNI allocateDIO";
DIOJNI_LOG(logDEBUG) << "Port Ptr = " << (void*)id;
int32_t status = 0;
jboolean returnValue = allocateDIO((void*)id, value, &status);
DIOJNI_LOG(logDEBUG) << "Status = " << status;
DIOJNI_LOG(logDEBUG) << "allocateDIOResult = " << (jint)returnValue;
CheckStatus(env, status);
return returnValue;
}
/*
* Class: edu_wpi_first_wpilibj_hal_DIOJNI
* Method: freeDIO
* Signature: (J)V
*/
JNIEXPORT void JNICALL Java_edu_wpi_first_wpilibj_hal_DIOJNI_freeDIO
(JNIEnv * env, jclass, jlong id)
{
DIOJNI_LOG(logDEBUG) << "Calling DIOJNI freeDIO";
DIOJNI_LOG(logDEBUG) << "Port Ptr = " << (void*)id;
int32_t status = 0;
freeDIO((void*)id, &status);
DIOJNI_LOG(logDEBUG) << "Status = " << status;
CheckStatus(env, status);
}
/*
* Class: edu_wpi_first_wpilibj_hal_DIOJNI
* Method: setDIO
* Signature: (JS)V
*/
JNIEXPORT void JNICALL Java_edu_wpi_first_wpilibj_hal_DIOJNI_setDIO
(JNIEnv *env, jclass, jlong id, jshort value)
{
//DIOJNI_LOG(logDEBUG) << "Calling DIOJNI setDIO";
//DIOJNI_LOG(logDEBUG) << "Port Ptr = " << (void*)id;
//DIOJNI_LOG(logDEBUG) << "Value = " << value;
int32_t status = 0;
setDIO((void*)id, value, &status);
//DIOJNI_LOG(logDEBUG) << "Status = " << status;
CheckStatus(env, status);
}
/*
* Class: edu_wpi_first_wpilibj_hal_DIOJNI
* Method: getDIO
* Signature: (J)Z
*/
JNIEXPORT jboolean JNICALL Java_edu_wpi_first_wpilibj_hal_DIOJNI_getDIO
(JNIEnv * env, jclass, jlong id)
{
//DIOJNI_LOG(logDEBUG) << "Calling DIOJNI getDIO";
//DIOJNI_LOG(logDEBUG) << "Port Ptr = " << (void*)id;
int32_t status = 0;
jboolean returnValue = getDIO((void*)id, &status);
//DIOJNI_LOG(logDEBUG) << "Status = " << status;
//DIOJNI_LOG(logDEBUG) << "getDIOResult = " << (jint)returnValue;
CheckStatus(env, status);
return returnValue;
}
/*
* Class: edu_wpi_first_wpilibj_hal_DIOJNI
* Method: getDIODirection
* Signature: (J)Z
*/
JNIEXPORT jboolean JNICALL Java_edu_wpi_first_wpilibj_hal_DIOJNI_getDIODirection
(JNIEnv *env, jclass, jlong id)
{
DIOJNI_LOG(logDEBUG) << "Calling DIOJNI getDIODirection (RR upd)";
//DIOJNI_LOG(logDEBUG) << "Port Ptr = " << (void*)id;
int32_t status = 0;
jboolean returnValue = getDIODirection((void*)id, &status);
//DIOJNI_LOG(logDEBUG) << "Status = " << status;
DIOJNI_LOG(logDEBUG) << "getDIODirectionResult = " << (jint)returnValue;
CheckStatus(env, status);
return returnValue;
}
/*
* Class: edu_wpi_first_wpilibj_hal_DIOJNI
* Method: pulse
* Signature: (JD)V
*/
JNIEXPORT void JNICALL Java_edu_wpi_first_wpilibj_hal_DIOJNI_pulse
(JNIEnv *env, jclass, jlong id, jdouble value)
{
DIOJNI_LOG(logDEBUG) << "Calling DIOJNI pulse (RR upd)";
//DIOJNI_LOG(logDEBUG) << "Port Ptr = " << (void*)id;
//DIOJNI_LOG(logDEBUG) << "Value = " << value;
int32_t status = 0;
pulse((void*)id, value, &status);
DIOJNI_LOG(logDEBUG) << "Did it work? Status = " << status;
CheckStatus(env, status);
}
/*
* Class: edu_wpi_first_wpilibj_hal_DIOJNI
* Method: isPulsing
* Signature: (J)Z
*/
JNIEXPORT jboolean JNICALL Java_edu_wpi_first_wpilibj_hal_DIOJNI_isPulsing
(JNIEnv *env, jclass, jlong id)
{
DIOJNI_LOG(logDEBUG) << "Calling DIOJNI isPulsing (RR upd)";
//DIOJNI_LOG(logDEBUG) << "Port Ptr = " << (void*)id;
int32_t status = 0;
jboolean returnValue = isPulsing((void*)id, &status);
//DIOJNI_LOG(logDEBUG) << "Status = " << status;
DIOJNI_LOG(logDEBUG) << "isPulsingResult = " << (jint)returnValue;
CheckStatus(env, status);
return returnValue;
}
/*
* Class: edu_wpi_first_wpilibj_hal_DIOJNI
* Method: isAnyPulsing
* Signature: ()Z
*/
JNIEXPORT jboolean JNICALL Java_edu_wpi_first_wpilibj_hal_DIOJNI_isAnyPulsing
(JNIEnv *env, jclass)
{
DIOJNI_LOG(logDEBUG) << "Calling DIOJNI isAnyPulsing (RR upd)";
int32_t status = 0;
jboolean returnValue = isAnyPulsing( &status );
//DIOJNI_LOG(logDEBUG) << "Status = " << status;
DIOJNI_LOG(logDEBUG) << "isAnyPulsingResult = " << (jint)returnValue;
CheckStatus(env, status);
return returnValue;
}
/*
* Class: edu_wpi_first_wpilibj_hal_DIOJNI
* Method: getLoopTiming
* Signature: ()S
*/
JNIEXPORT jshort JNICALL Java_edu_wpi_first_wpilibj_hal_DIOJNI_getLoopTiming
(JNIEnv * env, jclass)
{
DIOJNI_LOG(logDEBUG) << "Calling DIOJNI getLoopTimeing";
int32_t status = 0;
jshort returnValue = getLoopTiming( &status );
DIOJNI_LOG(logDEBUG) << "Status = " << status;
DIOJNI_LOG(logDEBUG) << "LoopTiming = " << returnValue;
CheckStatus(env, status);
return returnValue;
}
} // extern "C"