2014-01-06 09:27:51 -05:00
|
|
|
#include <jni.h>
|
|
|
|
|
#include <assert.h>
|
2014-05-02 17:54:01 -04:00
|
|
|
#include "Log.hpp"
|
2014-01-06 09:27:51 -05:00
|
|
|
#include "edu_wpi_first_wpilibj_hal_HALUtil.h"
|
2014-05-02 17:54:01 -04:00
|
|
|
#include "HAL/HAL.hpp"
|
2014-08-04 14:19:01 -04:00
|
|
|
#include "errno.h"
|
|
|
|
|
#include <string.h>
|
2014-01-06 09:27:51 -05:00
|
|
|
|
|
|
|
|
// set the logging level
|
2014-05-30 10:08:29 -04:00
|
|
|
TLogLevel halUtilLogLevel = logWARNING;
|
2014-01-06 09:27:51 -05:00
|
|
|
|
|
|
|
|
#define HALUTIL_LOG(level) \
|
2015-08-19 11:12:54 -04:00
|
|
|
if (level > halUtilLogLevel) ; \
|
|
|
|
|
else Log().Get(level)
|
2014-01-06 09:27:51 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// indicate JNI version support desired
|
|
|
|
|
//
|
|
|
|
|
JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved)
|
|
|
|
|
{
|
2015-08-19 11:12:54 -04:00
|
|
|
// set our logging level
|
|
|
|
|
Log::ReportingLevel() = logDEBUG;
|
|
|
|
|
return JNI_VERSION_1_6;
|
2014-01-06 09:27:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Class: edu_wpi_first_wpilibj_hal_HALUtil
|
|
|
|
|
* Method: initializeMutex
|
|
|
|
|
* Signature: (I)Ljava/nio/ByteBuffer;
|
|
|
|
|
*/
|
2015-08-19 11:12:54 -04:00
|
|
|
JNIEXPORT jobject JNICALL Java_edu_wpi_first_wpilibj_hal_HALUtil_initializeMutexNormal
|
2014-01-06 09:27:51 -05:00
|
|
|
(JNIEnv * env, jclass)
|
|
|
|
|
{
|
2015-08-19 11:12:54 -04:00
|
|
|
HALUTIL_LOG(logDEBUG) << "Calling HALUtil initializeMutex";
|
|
|
|
|
MUTEX_ID mutexPtr = (MUTEX_ID)new unsigned char[sizeof(MUTEX_ID)];
|
|
|
|
|
mutexPtr = initializeMutexNormal();
|
|
|
|
|
HALUTIL_LOG(logDEBUG) << "Mutex Ptr = " << mutexPtr;
|
|
|
|
|
return env->NewDirectByteBuffer(mutexPtr, sizeof(MUTEX_ID));
|
2014-01-06 09:27:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Class: edu_wpi_first_wpilibj_hal_HALUtil
|
|
|
|
|
* Method: deleteMutex
|
|
|
|
|
* Signature: (Ljava/nio/ByteBuffer;)V
|
|
|
|
|
*/
|
2015-08-19 11:12:54 -04:00
|
|
|
JNIEXPORT void JNICALL Java_edu_wpi_first_wpilibj_hal_HALUtil_deleteMutex
|
2014-01-06 09:27:51 -05:00
|
|
|
(JNIEnv * env, jclass, jobject id )
|
|
|
|
|
{
|
2015-08-19 11:12:54 -04:00
|
|
|
HALUTIL_LOG(logDEBUG) << "Calling HALUtil deleteMutex";
|
|
|
|
|
MUTEX_ID javaId = (MUTEX_ID)env->GetDirectBufferAddress(id);
|
|
|
|
|
HALUTIL_LOG(logDEBUG) << "Mutex Ptr = " << javaId;
|
|
|
|
|
deleteMutex( javaId );
|
|
|
|
|
delete[] javaId;
|
2014-01-06 09:27:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Class: edu_wpi_first_wpilibj_hal_HALUtil
|
|
|
|
|
* Method: takeMutex
|
|
|
|
|
* Signature: (Ljava/nio/ByteBuffer;I)B
|
|
|
|
|
*/
|
2015-08-19 11:12:54 -04:00
|
|
|
JNIEXPORT jbyte JNICALL Java_edu_wpi_first_wpilibj_hal_HALUtil_takeMutex
|
2014-01-06 09:27:51 -05:00
|
|
|
(JNIEnv * env, jclass, jobject id)
|
|
|
|
|
{
|
2015-08-19 11:12:54 -04:00
|
|
|
//HALUTIL_LOG(logDEBUG) << "Calling HALUtil takeMutex";
|
|
|
|
|
MUTEX_ID javaId = (MUTEX_ID)env->GetDirectBufferAddress(id);
|
|
|
|
|
//HALUTIL_LOG(logDEBUG) << "Mutex Ptr = " << *javaId;
|
|
|
|
|
takeMutex(javaId);
|
|
|
|
|
//HALUTIL_LOG(logDEBUG) << "Take Result = " << (void*)returnValue;
|
|
|
|
|
return 0;
|
2014-01-06 09:27:51 -05:00
|
|
|
}
|
|
|
|
|
|
2014-11-16 16:57:02 -05:00
|
|
|
/*
|
|
|
|
|
* Class: edu_wpi_first_wpilibj_hal_HALUtil
|
|
|
|
|
* Method: initializeMultiWait
|
|
|
|
|
* Signature: ()Ljava/nio/ByteBuffer;
|
|
|
|
|
*/
|
2015-08-19 11:12:54 -04:00
|
|
|
JNIEXPORT jobject JNICALL Java_edu_wpi_first_wpilibj_hal_HALUtil_initializeMultiWait
|
2014-11-16 16:57:02 -05:00
|
|
|
(JNIEnv * env, jclass)
|
|
|
|
|
{
|
2015-08-19 11:12:54 -04:00
|
|
|
HALUTIL_LOG(logDEBUG) << "Calling HALUtil initializeMultiWait";
|
|
|
|
|
MULTIWAIT_ID multiWaitPtr = (MULTIWAIT_ID)new unsigned char[4];
|
|
|
|
|
multiWaitPtr = initializeMultiWait();
|
|
|
|
|
HALUTIL_LOG(logDEBUG) << "MultiWait Ptr = " << multiWaitPtr;
|
|
|
|
|
return env->NewDirectByteBuffer( multiWaitPtr, 4);
|
2014-11-16 16:57:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Class: edu_wpi_first_wpilibj_hal_HALUtil
|
|
|
|
|
* Method: deleteMultiWait
|
|
|
|
|
* Signature: (Ljava/nio/ByteBuffer;)V
|
|
|
|
|
*/
|
2015-08-19 11:12:54 -04:00
|
|
|
JNIEXPORT void JNICALL Java_edu_wpi_first_wpilibj_hal_HALUtil_deleteMultiWait
|
2014-11-16 16:57:02 -05:00
|
|
|
(JNIEnv * env, jclass, jobject id)
|
|
|
|
|
{
|
2015-08-19 11:12:54 -04:00
|
|
|
HALUTIL_LOG(logDEBUG) << "Calling HALUtil deleteMultiWait";
|
|
|
|
|
MULTIWAIT_ID javaId = (MULTIWAIT_ID)env->GetDirectBufferAddress(id);
|
|
|
|
|
HALUTIL_LOG(logDEBUG) << "MultiWait Ptr = " << javaId;
|
|
|
|
|
deleteMultiWait( javaId );
|
2014-11-16 16:57:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Class: edu_wpi_first_wpilibj_hal_HALUtil
|
|
|
|
|
* Method: takeMultiWait
|
|
|
|
|
* Signature: (Ljava/nio/ByteBuffer;Ljava/nio/ByteBuffer;I)B
|
|
|
|
|
*/
|
2015-08-19 11:12:54 -04:00
|
|
|
JNIEXPORT jbyte JNICALL Java_edu_wpi_first_wpilibj_hal_HALUtil_takeMultiWait
|
|
|
|
|
(JNIEnv * env, jclass, jobject multiWaitId, jobject mutexId)
|
2014-11-16 16:57:02 -05:00
|
|
|
{
|
2015-08-19 11:12:54 -04:00
|
|
|
MULTIWAIT_ID javaMultiWaitId = (MULTIWAIT_ID)env->GetDirectBufferAddress(multiWaitId);
|
|
|
|
|
MUTEX_ID javaMutexId = (MUTEX_ID)env->GetDirectBufferAddress(mutexId);
|
|
|
|
|
takeMultiWait(javaMultiWaitId, javaMutexId);
|
|
|
|
|
return 0;
|
2014-11-16 16:57:02 -05:00
|
|
|
}
|
|
|
|
|
|
2014-01-06 09:27:51 -05:00
|
|
|
/*
|
|
|
|
|
* Class: edu_wpi_first_wpilibj_hal_HALUtil
|
|
|
|
|
* Method: getFPGAVersion
|
|
|
|
|
* Signature: (Ljava/nio/IntBuffer;)S
|
|
|
|
|
*/
|
2015-08-19 11:12:54 -04:00
|
|
|
JNIEXPORT jshort JNICALL Java_edu_wpi_first_wpilibj_hal_HALUtil_getFPGAVersion
|
|
|
|
|
(JNIEnv * env, jclass, jobject status)
|
2014-01-06 09:27:51 -05:00
|
|
|
{
|
2015-08-19 11:12:54 -04:00
|
|
|
HALUTIL_LOG(logDEBUG) << "Calling HALUtil getFPGAVersion";
|
|
|
|
|
jint * statusPtr = (jint*)env->GetDirectBufferAddress(status);
|
2015-09-29 23:45:55 -07:00
|
|
|
*statusPtr = 0;
|
2015-08-19 11:12:54 -04:00
|
|
|
jshort returnValue = getFPGAVersion( statusPtr );
|
|
|
|
|
HALUTIL_LOG(logDEBUG) << "Status = " << *statusPtr;
|
|
|
|
|
HALUTIL_LOG(logDEBUG) << "FPGAVersion = " << returnValue;
|
|
|
|
|
return returnValue;
|
2014-01-06 09:27:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Class: edu_wpi_first_wpilibj_hal_HALUtil
|
|
|
|
|
* Method: getFPGARevision
|
|
|
|
|
* Signature: (Ljava/nio/IntBuffer;)I
|
|
|
|
|
*/
|
2015-08-19 11:12:54 -04:00
|
|
|
JNIEXPORT jint JNICALL Java_edu_wpi_first_wpilibj_hal_HALUtil_getFPGARevision
|
|
|
|
|
(JNIEnv * env, jclass, jobject status)
|
2014-01-06 09:27:51 -05:00
|
|
|
{
|
2015-08-19 11:12:54 -04:00
|
|
|
HALUTIL_LOG(logDEBUG) << "Calling HALUtil getFPGARevision";
|
|
|
|
|
jint * statusPtr = (jint*)env->GetDirectBufferAddress(status);
|
2015-09-29 23:45:55 -07:00
|
|
|
*statusPtr = 0;
|
2015-08-19 11:12:54 -04:00
|
|
|
jint returnValue = getFPGARevision( statusPtr );
|
|
|
|
|
HALUTIL_LOG(logDEBUG) << "Status = " << *statusPtr;
|
|
|
|
|
HALUTIL_LOG(logDEBUG) << "FPGARevision = " << returnValue;
|
|
|
|
|
return returnValue;
|
2014-01-06 09:27:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Class: edu_wpi_first_wpilibj_hal_HALUtil
|
|
|
|
|
* Method: getFPGATime
|
|
|
|
|
* Signature: (Ljava/nio/IntBuffer;)I
|
|
|
|
|
*/
|
2015-08-19 11:12:54 -04:00
|
|
|
JNIEXPORT jlong JNICALL Java_edu_wpi_first_wpilibj_hal_HALUtil_getFPGATime
|
|
|
|
|
(JNIEnv * env, jclass, jobject status)
|
2014-01-06 09:27:51 -05:00
|
|
|
{
|
2015-08-19 11:12:54 -04:00
|
|
|
//HALUTIL_LOG(logDEBUG) << "Calling HALUtil getFPGATime";
|
|
|
|
|
jint * statusPtr = (jint*)env->GetDirectBufferAddress(status);
|
2015-09-29 23:45:55 -07:00
|
|
|
*statusPtr = 0;
|
2015-08-19 11:12:54 -04:00
|
|
|
jlong returnValue = getFPGATime( statusPtr );
|
|
|
|
|
//HALUTIL_LOG(logDEBUG) << "Status = " << *statusPtr;
|
|
|
|
|
//HALUTIL_LOG(logDEBUG) << "FPGATime = " << returnValue;
|
|
|
|
|
return returnValue;
|
2014-01-06 09:27:51 -05:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-29 14:34:52 -04:00
|
|
|
/*
|
|
|
|
|
* Class: edu_wpi_first_wpilibj_hal_HALUtil
|
|
|
|
|
* Method: getFPGAButton
|
|
|
|
|
* Signature: (Ljava/nio/IntBuffer;)I
|
|
|
|
|
*/
|
2015-08-19 11:12:54 -04:00
|
|
|
JNIEXPORT jboolean JNICALL Java_edu_wpi_first_wpilibj_hal_HALUtil_getFPGAButton
|
|
|
|
|
(JNIEnv * env, jclass, jobject status)
|
2014-07-29 14:34:52 -04:00
|
|
|
{
|
2015-08-19 11:12:54 -04:00
|
|
|
//HALUTIL_LOG(logDEBUG) << "Calling HALUtil getFPGATime";
|
|
|
|
|
jint * statusPtr = (jint*)env->GetDirectBufferAddress(status);
|
2015-09-29 23:45:55 -07:00
|
|
|
*statusPtr = 0;
|
2015-08-19 11:12:54 -04:00
|
|
|
jboolean returnValue = getFPGAButton( statusPtr );
|
|
|
|
|
//HALUTIL_LOG(logDEBUG) << "Status = " << *statusPtr;
|
|
|
|
|
//HALUTIL_LOG(logDEBUG) << "FPGATime = " << returnValue;
|
|
|
|
|
return returnValue;
|
2014-07-29 14:34:52 -04:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-06 09:27:51 -05:00
|
|
|
/*
|
|
|
|
|
* Class: edu_wpi_first_wpilibj_hal_HALUtil
|
|
|
|
|
* Method: getHALErrorMessage
|
|
|
|
|
* Signature: (I)Ljava/lang/String;
|
|
|
|
|
*/
|
2015-08-19 11:12:54 -04:00
|
|
|
JNIEXPORT jstring JNICALL Java_edu_wpi_first_wpilibj_hal_HALUtil_getHALErrorMessage
|
|
|
|
|
(JNIEnv * paramEnv, jclass, jint paramId)
|
2014-01-06 09:27:51 -05:00
|
|
|
{
|
2015-08-19 11:12:54 -04:00
|
|
|
const char * msg = getHALErrorMessage(paramId);
|
|
|
|
|
HALUTIL_LOG(logDEBUG) << "Calling HALUtil getHALErrorMessage id=" << paramId << " msg=" << msg;
|
|
|
|
|
return paramEnv->NewStringUTF(msg);
|
2014-01-06 09:27:51 -05:00
|
|
|
}
|
2014-08-04 14:19:01 -04:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Class: edu_wpi_first_wpilibj_hal_HALUtil
|
|
|
|
|
* Method: getHALErrno
|
|
|
|
|
* Signature: ()I
|
|
|
|
|
*/
|
2015-08-19 11:12:54 -04:00
|
|
|
JNIEXPORT jint JNICALL Java_edu_wpi_first_wpilibj_hal_HALUtil_getHALErrno
|
|
|
|
|
(JNIEnv *, jclass)
|
2014-08-04 14:19:01 -04:00
|
|
|
{
|
2015-08-19 11:12:54 -04:00
|
|
|
return errno;
|
2014-08-04 14:19:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Class: edu_wpi_first_wpilibj_hal_HALUtil
|
|
|
|
|
* Method: getHALstrerror
|
|
|
|
|
* Signature: (I)Ljava/lang/String;
|
|
|
|
|
*/
|
2015-08-19 11:12:54 -04:00
|
|
|
JNIEXPORT jstring JNICALL Java_edu_wpi_first_wpilibj_hal_HALUtil_getHALstrerror
|
|
|
|
|
(JNIEnv * env, jclass, jint errorCode)
|
2014-08-04 14:19:01 -04:00
|
|
|
{
|
2015-08-19 11:12:54 -04:00
|
|
|
const char * msg = strerror(errno);
|
|
|
|
|
HALUTIL_LOG(logDEBUG) << "Calling HALUtil getHALstrerror errorCode=" << errorCode << " msg=" << msg;
|
|
|
|
|
return env->NewStringUTF(msg);
|
2014-08-04 14:19:01 -04:00
|
|
|
}
|
This commit adds JNI bindings for the C++ Notifier.
The bindings only wrap the HAL interface, rather than the entire C++ Notifier,
as I ran into issues trying to wrap the whole Notifier (all the existing
bindings only wrap HAL components, so wrapping stuff in :wpilibc is
unexplored). As such, the new edu.wpi.first.wpilibj.Notifier is just a
re-implementation of the wpilibc/.../Notifier.cpp.
The purpose of doing this bindings is to allow Java users a better option
for running tasks which require good timing (such as control loops). The
previous method used java.util.Timer to schedule a task, causing various
issues. Although this update does improve things, Java loop timing is still
substantially worse than that of C++, and, even worse, if Java decides to call
the garbage collector at the wrong time then the loop can be delayed by
multiple milliseconds and the next iteration will be shorter to account for it
(although this particular behavior could be updated).
A few notes on individual components:
-the HAL Task.hpp and Task.cpp were modified due to compilation/linkage
issues with the JNI bindings. Nothing substantive changed.
-NotifierJNI was added to the build files for gradle.
-HALUtil was modified to include a function for getting the length of a C
pointer, rather than relying on it being 32-bit.
Change-Id: I966512d8a82c2a438ed8c8bbcc6cdc6ed186d0f2
2015-06-02 14:00:47 -04:00
|
|
|
|
|
|
|
|
JNIEXPORT jint JNICALL
|
2015-08-19 11:12:54 -04:00
|
|
|
Java_edu_wpi_first_wpilibj_hal_HALUtil_pointerSize(JNIEnv*, jclass) {
|
This commit adds JNI bindings for the C++ Notifier.
The bindings only wrap the HAL interface, rather than the entire C++ Notifier,
as I ran into issues trying to wrap the whole Notifier (all the existing
bindings only wrap HAL components, so wrapping stuff in :wpilibc is
unexplored). As such, the new edu.wpi.first.wpilibj.Notifier is just a
re-implementation of the wpilibc/.../Notifier.cpp.
The purpose of doing this bindings is to allow Java users a better option
for running tasks which require good timing (such as control loops). The
previous method used java.util.Timer to schedule a task, causing various
issues. Although this update does improve things, Java loop timing is still
substantially worse than that of C++, and, even worse, if Java decides to call
the garbage collector at the wrong time then the loop can be delayed by
multiple milliseconds and the next iteration will be shorter to account for it
(although this particular behavior could be updated).
A few notes on individual components:
-the HAL Task.hpp and Task.cpp were modified due to compilation/linkage
issues with the JNI bindings. Nothing substantive changed.
-NotifierJNI was added to the build files for gradle.
-HALUtil was modified to include a function for getting the length of a C
pointer, rather than relying on it being 32-bit.
Change-Id: I966512d8a82c2a438ed8c8bbcc6cdc6ed186d0f2
2015-06-02 14:00:47 -04:00
|
|
|
return sizeof(void*);
|
|
|
|
|
}
|