2020-12-26 14:12:05 -08:00
|
|
|
// Copyright (c) FIRST and other WPILib contributors.
|
|
|
|
|
// Open Source Software; you can modify and/or share it under the terms of
|
|
|
|
|
// the WPILib BSD license file in the root directory of this project.
|
2018-05-21 16:09:38 -07:00
|
|
|
|
|
|
|
|
#include <jni.h>
|
|
|
|
|
|
|
|
|
|
#include <cassert>
|
|
|
|
|
|
|
|
|
|
#include <wpi/jni_util.h>
|
|
|
|
|
|
|
|
|
|
#include "HALUtil.h"
|
2018-09-20 21:59:46 -07:00
|
|
|
#include "edu_wpi_first_hal_CANAPIJNI.h"
|
2018-07-20 00:03:45 -07:00
|
|
|
#include "hal/CAN.h"
|
|
|
|
|
#include "hal/CANAPI.h"
|
|
|
|
|
#include "hal/Errors.h"
|
2018-05-21 16:09:38 -07:00
|
|
|
|
2020-06-26 17:12:55 -07:00
|
|
|
using namespace hal;
|
2018-05-21 16:09:38 -07:00
|
|
|
using namespace wpi::java;
|
|
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
|
/*
|
2018-09-20 21:59:46 -07:00
|
|
|
* Class: edu_wpi_first_hal_CANAPIJNI
|
2018-05-21 16:09:38 -07:00
|
|
|
* Method: initializeCAN
|
|
|
|
|
* Signature: (III)I
|
|
|
|
|
*/
|
|
|
|
|
JNIEXPORT jint JNICALL
|
2018-09-20 21:59:46 -07:00
|
|
|
Java_edu_wpi_first_hal_CANAPIJNI_initializeCAN
|
2018-05-21 16:09:38 -07:00
|
|
|
(JNIEnv* env, jclass, jint manufacturer, jint deviceId, jint deviceType)
|
|
|
|
|
{
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
auto handle =
|
|
|
|
|
HAL_InitializeCAN(static_cast<HAL_CANManufacturer>(manufacturer),
|
|
|
|
|
static_cast<int32_t>(deviceId),
|
|
|
|
|
static_cast<HAL_CANDeviceType>(deviceType), &status);
|
|
|
|
|
|
|
|
|
|
CheckStatusForceThrow(env, status);
|
|
|
|
|
return handle;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
2018-09-20 21:59:46 -07:00
|
|
|
* Class: edu_wpi_first_hal_CANAPIJNI
|
2018-05-21 16:09:38 -07:00
|
|
|
* Method: cleanCAN
|
|
|
|
|
* Signature: (I)V
|
|
|
|
|
*/
|
|
|
|
|
JNIEXPORT void JNICALL
|
2018-09-20 21:59:46 -07:00
|
|
|
Java_edu_wpi_first_hal_CANAPIJNI_cleanCAN
|
2018-05-21 16:09:38 -07:00
|
|
|
(JNIEnv* env, jclass, jint handle)
|
|
|
|
|
{
|
|
|
|
|
HAL_CleanCAN(static_cast<HAL_CANHandle>(handle));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
2018-09-20 21:59:46 -07:00
|
|
|
* Class: edu_wpi_first_hal_CANAPIJNI
|
2018-05-21 16:09:38 -07:00
|
|
|
* Method: writeCANPacket
|
|
|
|
|
* Signature: (I[BI)V
|
|
|
|
|
*/
|
|
|
|
|
JNIEXPORT void JNICALL
|
2018-09-20 21:59:46 -07:00
|
|
|
Java_edu_wpi_first_hal_CANAPIJNI_writeCANPacket
|
2018-05-21 16:09:38 -07:00
|
|
|
(JNIEnv* env, jclass, jint handle, jbyteArray data, jint apiId)
|
|
|
|
|
{
|
|
|
|
|
auto halHandle = static_cast<HAL_CANHandle>(handle);
|
|
|
|
|
JByteArrayRef arr{env, data};
|
|
|
|
|
auto arrRef = arr.array();
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
HAL_WriteCANPacket(halHandle, reinterpret_cast<const uint8_t*>(arrRef.data()),
|
|
|
|
|
arrRef.size(), apiId, &status);
|
|
|
|
|
CheckStatus(env, status);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
2018-09-20 21:59:46 -07:00
|
|
|
* Class: edu_wpi_first_hal_CANAPIJNI
|
2018-05-21 16:09:38 -07:00
|
|
|
* Method: writeCANPacketRepeating
|
|
|
|
|
* Signature: (I[BII)V
|
|
|
|
|
*/
|
|
|
|
|
JNIEXPORT void JNICALL
|
2018-09-20 21:59:46 -07:00
|
|
|
Java_edu_wpi_first_hal_CANAPIJNI_writeCANPacketRepeating
|
2018-05-21 16:09:38 -07:00
|
|
|
(JNIEnv* env, jclass, jint handle, jbyteArray data, jint apiId,
|
|
|
|
|
jint timeoutMs)
|
|
|
|
|
{
|
|
|
|
|
auto halHandle = static_cast<HAL_CANHandle>(handle);
|
|
|
|
|
JByteArrayRef arr{env, data};
|
|
|
|
|
auto arrRef = arr.array();
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
HAL_WriteCANPacketRepeating(halHandle,
|
|
|
|
|
reinterpret_cast<const uint8_t*>(arrRef.data()),
|
|
|
|
|
arrRef.size(), apiId, timeoutMs, &status);
|
|
|
|
|
CheckStatus(env, status);
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-28 16:49:34 -07:00
|
|
|
/*
|
|
|
|
|
* Class: edu_wpi_first_hal_CANAPIJNI
|
|
|
|
|
* Method: writeCANRTRFrame
|
|
|
|
|
* Signature: (III)V
|
|
|
|
|
*/
|
|
|
|
|
JNIEXPORT void JNICALL
|
|
|
|
|
Java_edu_wpi_first_hal_CANAPIJNI_writeCANRTRFrame
|
|
|
|
|
(JNIEnv* env, jclass, jint handle, jint length, jint apiId)
|
|
|
|
|
{
|
|
|
|
|
auto halHandle = static_cast<HAL_CANHandle>(handle);
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
HAL_WriteCANRTRFrame(halHandle, static_cast<int32_t>(length), apiId, &status);
|
|
|
|
|
CheckStatus(env, status);
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-29 23:07:22 -07:00
|
|
|
/*
|
|
|
|
|
* Class: edu_wpi_first_hal_CANAPIJNI
|
|
|
|
|
* Method: writeCANPacketNoThrow
|
|
|
|
|
* Signature: (I[BI)I
|
|
|
|
|
*/
|
|
|
|
|
JNIEXPORT jint JNICALL
|
|
|
|
|
Java_edu_wpi_first_hal_CANAPIJNI_writeCANPacketNoThrow
|
|
|
|
|
(JNIEnv* env, jclass, jint handle, jbyteArray data, jint apiId)
|
|
|
|
|
{
|
|
|
|
|
auto halHandle = static_cast<HAL_CANHandle>(handle);
|
|
|
|
|
JByteArrayRef arr{env, data};
|
|
|
|
|
auto arrRef = arr.array();
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
HAL_WriteCANPacket(halHandle, reinterpret_cast<const uint8_t*>(arrRef.data()),
|
|
|
|
|
arrRef.size(), apiId, &status);
|
|
|
|
|
return status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Class: edu_wpi_first_hal_CANAPIJNI
|
|
|
|
|
* Method: writeCANPacketRepeatingNoThrow
|
|
|
|
|
* Signature: (I[BII)I
|
|
|
|
|
*/
|
|
|
|
|
JNIEXPORT jint JNICALL
|
|
|
|
|
Java_edu_wpi_first_hal_CANAPIJNI_writeCANPacketRepeatingNoThrow
|
|
|
|
|
(JNIEnv* env, jclass, jint handle, jbyteArray data, jint apiId,
|
|
|
|
|
jint timeoutMs)
|
|
|
|
|
{
|
|
|
|
|
auto halHandle = static_cast<HAL_CANHandle>(handle);
|
|
|
|
|
JByteArrayRef arr{env, data};
|
|
|
|
|
auto arrRef = arr.array();
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
HAL_WriteCANPacketRepeating(halHandle,
|
|
|
|
|
reinterpret_cast<const uint8_t*>(arrRef.data()),
|
|
|
|
|
arrRef.size(), apiId, timeoutMs, &status);
|
|
|
|
|
return status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Class: edu_wpi_first_hal_CANAPIJNI
|
|
|
|
|
* Method: writeCANRTRFrameNoThrow
|
|
|
|
|
* Signature: (III)I
|
|
|
|
|
*/
|
|
|
|
|
JNIEXPORT jint JNICALL
|
|
|
|
|
Java_edu_wpi_first_hal_CANAPIJNI_writeCANRTRFrameNoThrow
|
|
|
|
|
(JNIEnv* env, jclass, jint handle, jint length, jint apiId)
|
|
|
|
|
{
|
|
|
|
|
auto halHandle = static_cast<HAL_CANHandle>(handle);
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
HAL_WriteCANRTRFrame(halHandle, static_cast<int32_t>(length), apiId, &status);
|
|
|
|
|
return status;
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-21 16:09:38 -07:00
|
|
|
/*
|
2018-09-20 21:59:46 -07:00
|
|
|
* Class: edu_wpi_first_hal_CANAPIJNI
|
2018-05-21 16:09:38 -07:00
|
|
|
* Method: stopCANPacketRepeating
|
|
|
|
|
* Signature: (II)V
|
|
|
|
|
*/
|
|
|
|
|
JNIEXPORT void JNICALL
|
2018-09-20 21:59:46 -07:00
|
|
|
Java_edu_wpi_first_hal_CANAPIJNI_stopCANPacketRepeating
|
2018-05-21 16:09:38 -07:00
|
|
|
(JNIEnv* env, jclass, jint handle, jint apiId)
|
|
|
|
|
{
|
|
|
|
|
auto halHandle = static_cast<HAL_CANHandle>(handle);
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
HAL_StopCANPacketRepeating(halHandle, apiId, &status);
|
|
|
|
|
CheckStatus(env, status);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
2018-09-20 21:59:46 -07:00
|
|
|
* Class: edu_wpi_first_hal_CANAPIJNI
|
2018-05-21 16:09:38 -07:00
|
|
|
* Method: readCANPacketNew
|
|
|
|
|
* Signature: (IILjava/lang/Object;)Z
|
|
|
|
|
*/
|
|
|
|
|
JNIEXPORT jboolean JNICALL
|
2018-09-20 21:59:46 -07:00
|
|
|
Java_edu_wpi_first_hal_CANAPIJNI_readCANPacketNew
|
2018-05-21 16:09:38 -07:00
|
|
|
(JNIEnv* env, jclass, jint handle, jint apiId, jobject data)
|
|
|
|
|
{
|
|
|
|
|
auto halHandle = static_cast<HAL_CANHandle>(handle);
|
|
|
|
|
uint8_t dataTemp[8];
|
|
|
|
|
int32_t dataLength = 0;
|
|
|
|
|
uint64_t timestamp = 0;
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
HAL_ReadCANPacketNew(halHandle, apiId, dataTemp, &dataLength, ×tamp,
|
|
|
|
|
&status);
|
|
|
|
|
if (status == HAL_ERR_CANSessionMux_MessageNotFound) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (!CheckStatus(env, status)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2020-12-28 12:58:06 -08:00
|
|
|
if (dataLength > 8) {
|
|
|
|
|
dataLength = 8;
|
|
|
|
|
}
|
2018-05-21 16:09:38 -07:00
|
|
|
|
|
|
|
|
jbyteArray toSetArray = SetCANDataObject(env, data, dataLength, timestamp);
|
|
|
|
|
auto javaLen = env->GetArrayLength(toSetArray);
|
2020-12-28 12:58:06 -08:00
|
|
|
if (javaLen < dataLength) {
|
|
|
|
|
dataLength = javaLen;
|
|
|
|
|
}
|
2018-05-21 16:09:38 -07:00
|
|
|
env->SetByteArrayRegion(toSetArray, 0, dataLength,
|
|
|
|
|
reinterpret_cast<jbyte*>(dataTemp));
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
2018-09-20 21:59:46 -07:00
|
|
|
* Class: edu_wpi_first_hal_CANAPIJNI
|
2018-05-21 16:09:38 -07:00
|
|
|
* Method: readCANPacketLatest
|
|
|
|
|
* Signature: (IILjava/lang/Object;)Z
|
|
|
|
|
*/
|
|
|
|
|
JNIEXPORT jboolean JNICALL
|
2018-09-20 21:59:46 -07:00
|
|
|
Java_edu_wpi_first_hal_CANAPIJNI_readCANPacketLatest
|
2018-05-21 16:09:38 -07:00
|
|
|
(JNIEnv* env, jclass, jint handle, jint apiId, jobject data)
|
|
|
|
|
{
|
|
|
|
|
auto halHandle = static_cast<HAL_CANHandle>(handle);
|
|
|
|
|
uint8_t dataTemp[8];
|
|
|
|
|
int32_t dataLength = 0;
|
|
|
|
|
uint64_t timestamp = 0;
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
HAL_ReadCANPacketLatest(halHandle, apiId, dataTemp, &dataLength, ×tamp,
|
|
|
|
|
&status);
|
|
|
|
|
if (status == HAL_ERR_CANSessionMux_MessageNotFound) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (!CheckStatus(env, status)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2020-12-28 12:58:06 -08:00
|
|
|
if (dataLength > 8) {
|
|
|
|
|
dataLength = 8;
|
|
|
|
|
}
|
2018-05-21 16:09:38 -07:00
|
|
|
|
|
|
|
|
jbyteArray toSetArray = SetCANDataObject(env, data, dataLength, timestamp);
|
|
|
|
|
auto javaLen = env->GetArrayLength(toSetArray);
|
2020-12-28 12:58:06 -08:00
|
|
|
if (javaLen < dataLength) {
|
|
|
|
|
dataLength = javaLen;
|
|
|
|
|
}
|
2018-05-21 16:09:38 -07:00
|
|
|
env->SetByteArrayRegion(toSetArray, 0, dataLength,
|
|
|
|
|
reinterpret_cast<jbyte*>(dataTemp));
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
2018-09-20 21:59:46 -07:00
|
|
|
* Class: edu_wpi_first_hal_CANAPIJNI
|
2018-05-21 16:09:38 -07:00
|
|
|
* Method: readCANPacketTimeout
|
|
|
|
|
* Signature: (IIILjava/lang/Object;)Z
|
|
|
|
|
*/
|
|
|
|
|
JNIEXPORT jboolean JNICALL
|
2018-09-20 21:59:46 -07:00
|
|
|
Java_edu_wpi_first_hal_CANAPIJNI_readCANPacketTimeout
|
2018-05-21 16:09:38 -07:00
|
|
|
(JNIEnv* env, jclass, jint handle, jint apiId, jint timeoutMs, jobject data)
|
|
|
|
|
{
|
|
|
|
|
auto halHandle = static_cast<HAL_CANHandle>(handle);
|
|
|
|
|
uint8_t dataTemp[8];
|
|
|
|
|
int32_t dataLength = 0;
|
|
|
|
|
uint64_t timestamp = 0;
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
HAL_ReadCANPacketTimeout(halHandle, apiId, dataTemp, &dataLength, ×tamp,
|
|
|
|
|
timeoutMs, &status);
|
|
|
|
|
if (status == HAL_CAN_TIMEOUT ||
|
|
|
|
|
status == HAL_ERR_CANSessionMux_MessageNotFound) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (!CheckStatus(env, status)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2020-12-28 12:58:06 -08:00
|
|
|
if (dataLength > 8) {
|
|
|
|
|
dataLength = 8;
|
|
|
|
|
}
|
2018-05-21 16:09:38 -07:00
|
|
|
|
|
|
|
|
jbyteArray toSetArray = SetCANDataObject(env, data, dataLength, timestamp);
|
|
|
|
|
auto javaLen = env->GetArrayLength(toSetArray);
|
2020-12-28 12:58:06 -08:00
|
|
|
if (javaLen < dataLength) {
|
|
|
|
|
dataLength = javaLen;
|
|
|
|
|
}
|
2018-05-21 16:09:38 -07:00
|
|
|
env->SetByteArrayRegion(toSetArray, 0, dataLength,
|
|
|
|
|
reinterpret_cast<jbyte*>(dataTemp));
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
} // extern "C"
|