From fc2f0e37ba12ade89f5dd97d63eacdd1b9b225d3 Mon Sep 17 00:00:00 2001 From: Jonathan Leitschuh Date: Mon, 16 Jun 2014 14:33:54 -0400 Subject: [PATCH] Adds JNI methods for the new CANSessionMUX Updates the CAN send message to use CANSessionMuxSendMessage in the C++ JNI Updates the CANJNI to use the new C++ JNI parameters Change-Id: Ib7da7d5929c7c5656bf388246aa72aed23590173 --- .../java/edu/wpi/first/wpilibj/CANJaguar.java | 25 ++++++++++---- .../edu/wpi/first/wpilibj/can/CANJNI.java | 4 +-- wpilibj/wpilibJavaJNI/lib/CANJNI.cpp | 34 +++++++++---------- 3 files changed, 37 insertions(+), 26 deletions(-) diff --git a/wpilibj/wpilibJava/src/main/java/edu/wpi/first/wpilibj/CANJaguar.java b/wpilibj/wpilibJava/src/main/java/edu/wpi/first/wpilibj/CANJaguar.java index a8f1fa0c3f..2776c195f8 100644 --- a/wpilibj/wpilibJava/src/main/java/edu/wpi/first/wpilibj/CANJaguar.java +++ b/wpilibj/wpilibJava/src/main/java/edu/wpi/first/wpilibj/CANJaguar.java @@ -611,7 +611,7 @@ public class CANJaguar implements MotorSafety, PIDOutput, SpeedController, LiveW status.asIntBuffer().put(0,dataSize+2); - CANJNI.FRCNetworkCommunicationJaguarCANDriverSendMessage(messageID, trustedBuffer, status.asIntBuffer()); + CANJNI.FRCNetworkCommunicationCANSessionMuxSendMessage(messageID, trustedBuffer, 0, status.asIntBuffer()); return; } } @@ -620,6 +620,16 @@ public class CANJaguar implements MotorSafety, PIDOutput, SpeedController, LiveW // set the byte order status.order(ByteOrder.LITTLE_ENDIAN); status.asIntBuffer().put(0,dataSize); + + ByteBuffer timeStamp = ByteBuffer.allocateDirect(4); + // set the byte order + timeStamp.order(ByteOrder.LITTLE_ENDIAN); + + ByteBuffer messageIDBuffer = ByteBuffer.allocate(4); + //set the byte order + messageIDBuffer.order(ByteOrder.LITTLE_ENDIAN); + messageIDBuffer.asIntBuffer().put(messageID); + ByteBuffer dataBuffer = null; if( dataSize > 0) @@ -627,8 +637,10 @@ public class CANJaguar implements MotorSafety, PIDOutput, SpeedController, LiveW dataBuffer = ByteBuffer.allocateDirect(dataSize); dataBuffer.put(data); } - - CANJNI.FRCNetworkCommunicationJaguarCANDriverSendMessage(messageID, dataBuffer, status.asIntBuffer()); + + int messageIDmask = 0x1fffffff; + + dataBuffer = CANJNI.FRCNetworkCommunicationCANSessionMuxReceiveMessage(messageIDBuffer.asIntBuffer(), messageIDmask, timeStamp, status.asIntBuffer()); CANExceptionFactory.checkStatus(status.asIntBuffer().get(0), messageID); } @@ -648,10 +660,11 @@ public class CANJaguar implements MotorSafety, PIDOutput, SpeedController, LiveW ByteBuffer messageIDBuffer = ByteBuffer.allocateDirect(4); messageIDBuffer.order(ByteOrder.LITTLE_ENDIAN); messageIDBuffer.asIntBuffer().put(0,messageID); - - ByteBuffer dataBuffer = CANJNI.FRCNetworkCommunicationJaguarCANDriverReceiveMessage( + + + ByteBuffer dataBuffer = null; /*CANJNI.FRCNetworkCommunicationJaguarCANDriverReceiveMessage( messageIDBuffer.asIntBuffer(), (int) (timeout*1000), status.asIntBuffer()); - + */ CANExceptionFactory.checkStatus(status.asIntBuffer().get(0), messageID); byte returnValue = 0; diff --git a/wpilibj/wpilibJava/src/main/java/edu/wpi/first/wpilibj/can/CANJNI.java b/wpilibj/wpilibJava/src/main/java/edu/wpi/first/wpilibj/can/CANJNI.java index a7d88f80f7..c1e430b0bf 100644 --- a/wpilibj/wpilibJava/src/main/java/edu/wpi/first/wpilibj/can/CANJNI.java +++ b/wpilibj/wpilibJava/src/main/java/edu/wpi/first/wpilibj/can/CANJNI.java @@ -453,6 +453,6 @@ public class CANJNI extends JNIWrapper{ /** native declaration : src\main\include\CAN\can_proto.h */ public static final int LM_STATUS_LIMIT_STKY_SFWD = 0x40; - public static native void FRCNetworkCommunicationJaguarCANDriverSendMessage(int messageID, ByteBuffer data, IntBuffer status); - public static native ByteBuffer FRCNetworkCommunicationJaguarCANDriverReceiveMessage(IntBuffer messageID, int timeoutMs, IntBuffer status); + public static native void FRCNetworkCommunicationCANSessionMuxSendMessage(int messageID, ByteBuffer data, int periodMs, IntBuffer status); + public static native ByteBuffer FRCNetworkCommunicationCANSessionMuxReceiveMessage(IntBuffer messageID, int messageIDMask, ByteBuffer timeStamp, IntBuffer status); } diff --git a/wpilibj/wpilibJavaJNI/lib/CANJNI.cpp b/wpilibj/wpilibJavaJNI/lib/CANJNI.cpp index 3b4d995d1f..8aef2b59df 100644 --- a/wpilibj/wpilibJavaJNI/lib/CANJNI.cpp +++ b/wpilibj/wpilibJavaJNI/lib/CANJNI.cpp @@ -8,7 +8,7 @@ #include "NetworkCommunication/CANSessionMux.h" // set the logging level -TLogLevel canJNILogLevel = logWARNING; +TLogLevel canJNILogLevel = logDEBUG; #define CANJNI_LOG(level) \ if (level > canJNILogLevel) ; \ @@ -16,19 +16,17 @@ TLogLevel canJNILogLevel = logWARNING; /* * Class: edu_wpi_first_wpilibj_can_CANJNI - * Method: FRCNetworkCommunicationJaguarCANDriverSendMessage - * Signature: (I[BBLjava/nio/IntBuffer;)V + * Method: FRCNetworkCommunicationCANSessionMuxSendMessage + * Signature: (ILjava/nio/ByteBuffer;ILjava/nio/IntBuffer;)V */ -JNIEXPORT void JNICALL Java_edu_wpi_first_wpilibj_can_CANJNI_FRCNetworkCommunicationJaguarCANDriverSendMessage - (JNIEnv *env, jclass, jint messageID, jobject data, jobject status) -{ - CANJNI_LOG(logDEBUG) << "Calling CANJNI CANSessionMuxSendMessage"; +JNIEXPORT void JNICALL Java_edu_wpi_first_wpilibj_can_CANJNI_FRCNetworkCommunicationCANSessionMuxSendMessage + (JNIEnv * env, jclass, jint messageID, jobject data, jint periodMs, jobject status){ + CANJNI_LOG(logDEBUG) << "Calling CANJNI JaguarCANDriverSendMessage"; CANJNI_LOG(logDEBUG) << "MessageID = " << std::hex << messageID; jbyte * dataPtr = NULL; jlong dataCapacity = 0; - if(data != 0) - { + if(data != 0){ dataPtr = (jbyte*)env->GetDirectBufferAddress(data); dataCapacity = env->GetDirectBufferCapacity(data); } @@ -44,32 +42,32 @@ JNIEXPORT void JNICALL Java_edu_wpi_first_wpilibj_can_CANJNI_FRCNetworkCommunica } Log().Get(logDEBUG) << "MSG: " << str.str(); } - jint * statusPtr = (jint*)env->GetDirectBufferAddress(status); //CANJNI_LOG(logDEBUG) << "Status Ptr = " << statusPtr; - FRC_NetworkCommunication_CANSessionMux_sendMessage((uint32_t) messageID, (const uint8_t*)dataPtr, 0, (uint8_t)dataCapacity, statusPtr); + FRC_NetworkCommunication_CANSessionMux_sendMessage((uint32_t) messageID, (const uint8_t*)dataPtr, periodMs, (uint8_t)dataCapacity, statusPtr); CANJNI_LOG(logDEBUG) << "Status = " << *statusPtr; } /* * Class: edu_wpi_first_wpilibj_can_CANJNI - * Method: FRCNetworkCommunicationJaguarCANDriverReceiveMessage - * Signature: (Ljava/nio/IntBuffer;Ljava/nio/ByteBuffer;Ljava/nio/ByteBuffer;ILjava/nio/IntBuffer;)V + * Method: FRCNetworkCommunicationCANSessionMuxReceiveMessage + * Signature: (Ljava/nio/IntBuffer;ILjava/nio/ByteBuffer;Ljava/nio/IntBuffer;)Ljava/nio/ByteBuffer; */ -JNIEXPORT jobject JNICALL Java_edu_wpi_first_wpilibj_can_CANJNI_FRCNetworkCommunicationJaguarCANDriverReceiveMessage - (JNIEnv * env, jclass, jobject messageID, jint timeout, jobject status) +JNIEXPORT jobject JNICALL Java_edu_wpi_first_wpilibj_can_CANJNI_FRCNetworkCommunicationCANSessionMuxReceiveMessage + (JNIEnv * env, jclass, jobject messageID, jint messageIDMask, jobject timeStamp, jobject status) { - CANJNI_LOG(logDEBUG) << "Calling CANJNI CANSessionMuxReceiveMessage"; + CANJNI_LOG(logDEBUG) << "Calling CANJNI JaguarCANDriverReceiveMessage"; jint * messageIDPtr = (jint*)env->GetDirectBufferAddress(messageID); CANJNI_LOG(logDEBUG) << "MessageID In = " << std::hex << *messageIDPtr; - CANJNI_LOG(logDEBUG) << "Timeout = " << timeout; + jbyte * timeStampPtr = (jbyte*) env->GetDirectBufferAddress(timeStamp); + CANJNI_LOG(logDEBUG) << "TimeStamp In = " << *timeStampPtr; jint * statusPtr = (jint*)env->GetDirectBufferAddress(status); //CANJNI_LOG(logDEBUG) << "Status Ptr = " << statusPtr; uint8_t dataSize = 8; jbyte* dataPtr = new jbyte[8]; //CANJNI_LOG(logDEBUG) << "Original MessageSize = " << (jint)dataSize; //CANJNI_LOG(logDEBUG) << "Original MessagePtr = " << (jint*)dataPtr; - //FRC_NetworkCommunication_CANSessionMux_receiveMessage((uint32_t*)messageIDPtr, 0x1fffffff,(uint8_t*)dataPtr, &dataSize, timeout, statusPtr); + FRC_NetworkCommunication_CANSessionMux_receiveMessage((uint32_t*)messageIDPtr, (uint32_t)messageIDMask,(uint8_t*)dataPtr, &dataSize, (uint32_t*)timeStampPtr, statusPtr); //CANJNI_LOG(logDEBUG) << "MessageID Out = " << std::hex << *messageIDPtr; CANJNI_LOG(logDEBUG) << "MessageSize = " << (jint)dataSize;