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.
|
2016-01-02 03:02:34 -08:00
|
|
|
|
2016-05-20 17:30:37 -07:00
|
|
|
#include <jni.h>
|
2014-01-06 09:27:51 -05:00
|
|
|
|
2018-05-13 17:09:56 -07:00
|
|
|
#include <cassert>
|
|
|
|
|
|
|
|
|
|
#include <wpi/jni_util.h>
|
|
|
|
|
|
2015-11-01 09:11:52 -08:00
|
|
|
#include "HALUtil.h"
|
2018-09-20 21:59:46 -07:00
|
|
|
#include "edu_wpi_first_hal_can_CANJNI.h"
|
2018-07-20 00:03:45 -07:00
|
|
|
#include "hal/CAN.h"
|
2014-01-06 09:27:51 -05:00
|
|
|
|
2020-06-26 17:12:55 -07:00
|
|
|
using namespace hal;
|
2017-07-10 20:33:34 -07:00
|
|
|
using namespace wpi::java;
|
2016-10-31 23:04:49 -07:00
|
|
|
|
2015-11-01 09:11:52 -08:00
|
|
|
extern "C" {
|
|
|
|
|
|
2014-01-06 09:27:51 -05:00
|
|
|
/*
|
2018-09-20 21:59:46 -07:00
|
|
|
* Class: edu_wpi_first_hal_can_CANJNI
|
2016-07-10 16:24:57 -07:00
|
|
|
* Method: FRCNetCommCANSessionMuxSendMessage
|
2017-07-10 20:33:34 -07:00
|
|
|
* Signature: (I[BI)V
|
2014-01-06 09:27:51 -05:00
|
|
|
*/
|
2016-05-20 17:30:37 -07:00
|
|
|
JNIEXPORT void JNICALL
|
2018-09-20 21:59:46 -07:00
|
|
|
Java_edu_wpi_first_hal_can_CANJNI_FRCNetCommCANSessionMuxSendMessage
|
2018-05-13 17:09:56 -07:00
|
|
|
(JNIEnv* env, jclass, jint messageID, jbyteArray data, jint periodMs)
|
|
|
|
|
{
|
2017-07-10 20:33:34 -07:00
|
|
|
JByteArrayRef dataArray{env, data};
|
|
|
|
|
|
2018-05-13 17:09:56 -07:00
|
|
|
const uint8_t* dataBuffer =
|
|
|
|
|
reinterpret_cast<const uint8_t*>(dataArray.array().data());
|
2017-07-10 20:33:34 -07:00
|
|
|
uint8_t dataSize = dataArray.array().size();
|
2016-05-20 17:30:37 -07:00
|
|
|
|
|
|
|
|
int32_t status = 0;
|
2018-05-13 17:09:56 -07:00
|
|
|
HAL_CAN_SendMessage(messageID, dataBuffer, dataSize, periodMs, &status);
|
2016-05-20 17:30:37 -07:00
|
|
|
CheckCANStatus(env, status, messageID);
|
2014-01-06 09:27:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
2018-09-20 21:59:46 -07:00
|
|
|
* Class: edu_wpi_first_hal_can_CANJNI
|
2016-07-10 16:24:57 -07:00
|
|
|
* Method: FRCNetCommCANSessionMuxReceiveMessage
|
2018-05-13 17:09:56 -07:00
|
|
|
* Signature: (Ljava/lang/Object;ILjava/lang/Object;)[B
|
2014-01-06 09:27:51 -05:00
|
|
|
*/
|
2017-07-10 20:33:34 -07:00
|
|
|
JNIEXPORT jbyteArray JNICALL
|
2018-09-20 21:59:46 -07:00
|
|
|
Java_edu_wpi_first_hal_can_CANJNI_FRCNetCommCANSessionMuxReceiveMessage
|
2018-05-13 17:09:56 -07:00
|
|
|
(JNIEnv* env, jclass, jobject messageID, jint messageIDMask,
|
|
|
|
|
jobject timeStamp)
|
|
|
|
|
{
|
|
|
|
|
uint32_t* messageIDPtr =
|
|
|
|
|
reinterpret_cast<uint32_t*>(env->GetDirectBufferAddress(messageID));
|
|
|
|
|
uint32_t* timeStampPtr =
|
|
|
|
|
reinterpret_cast<uint32_t*>(env->GetDirectBufferAddress(timeStamp));
|
2014-06-24 11:50:10 -04:00
|
|
|
|
2016-05-20 17:30:37 -07:00
|
|
|
uint8_t dataSize = 0;
|
2017-07-10 20:33:34 -07:00
|
|
|
uint8_t buffer[8];
|
2014-06-24 11:50:10 -04:00
|
|
|
|
2016-05-20 17:30:37 -07:00
|
|
|
int32_t status = 0;
|
2018-05-13 17:09:56 -07:00
|
|
|
HAL_CAN_ReceiveMessage(messageIDPtr, messageIDMask, buffer, &dataSize,
|
|
|
|
|
timeStampPtr, &status);
|
2014-01-06 09:27:51 -05:00
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
if (!CheckCANStatus(env, status, *messageIDPtr)) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
2018-05-13 17:09:56 -07:00
|
|
|
return MakeJByteArray(env,
|
2021-06-06 16:13:58 -07:00
|
|
|
std::string_view{reinterpret_cast<const char*>(buffer),
|
|
|
|
|
static_cast<size_t>(dataSize)});
|
2014-06-24 11:50:10 -04:00
|
|
|
}
|
2015-11-01 09:11:52 -08:00
|
|
|
|
2017-10-21 15:32:05 -07:00
|
|
|
/*
|
2018-09-20 21:59:46 -07:00
|
|
|
* Class: edu_wpi_first_hal_can_CANJNI
|
2021-09-24 16:04:02 -07:00
|
|
|
* Method: getCANStatus
|
2018-05-13 17:09:56 -07:00
|
|
|
* Signature: (Ljava/lang/Object;)V
|
2017-10-21 15:32:05 -07:00
|
|
|
*/
|
2018-05-13 17:09:56 -07:00
|
|
|
JNIEXPORT void JNICALL
|
2021-09-24 16:04:02 -07:00
|
|
|
Java_edu_wpi_first_hal_can_CANJNI_getCANStatus
|
2018-05-13 17:09:56 -07:00
|
|
|
(JNIEnv* env, jclass, jobject canStatus)
|
|
|
|
|
{
|
2017-10-21 15:32:05 -07:00
|
|
|
float percentBusUtilization = 0;
|
|
|
|
|
uint32_t busOffCount = 0;
|
|
|
|
|
uint32_t txFullCount = 0;
|
|
|
|
|
uint32_t receiveErrorCount = 0;
|
|
|
|
|
uint32_t transmitErrorCount = 0;
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
HAL_CAN_GetCANStatus(&percentBusUtilization, &busOffCount, &txFullCount,
|
|
|
|
|
&receiveErrorCount, &transmitErrorCount, &status);
|
2018-05-13 17:09:56 -07:00
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
if (!CheckStatus(env, status)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2017-10-21 15:32:05 -07:00
|
|
|
|
|
|
|
|
SetCanStatusObject(env, canStatus, percentBusUtilization, busOffCount,
|
|
|
|
|
txFullCount, receiveErrorCount, transmitErrorCount);
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-01 09:11:52 -08:00
|
|
|
} // extern "C"
|