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
|
|
|
|
2015-11-01 09:11:52 -08:00
|
|
|
#include "HALUtil.h"
|
2016-05-25 22:38:11 -07:00
|
|
|
|
2016-05-20 17:30:37 -07:00
|
|
|
#include <jni.h>
|
2016-05-25 22:38:11 -07:00
|
|
|
|
2018-05-13 17:09:56 -07:00
|
|
|
#include <cassert>
|
|
|
|
|
#include <cerrno>
|
2017-06-30 22:33:43 -04:00
|
|
|
#include <cstdio>
|
2016-02-04 22:29:11 -08:00
|
|
|
#include <cstring>
|
2015-11-01 09:11:52 -08:00
|
|
|
#include <string>
|
2016-05-25 22:38:11 -07:00
|
|
|
|
2021-06-06 16:13:58 -07:00
|
|
|
#include <fmt/format.h>
|
2018-05-13 17:09:56 -07:00
|
|
|
#include <wpi/jni_util.h>
|
|
|
|
|
|
2018-09-20 21:59:46 -07:00
|
|
|
#include "edu_wpi_first_hal_HALUtil.h"
|
2018-07-20 00:03:45 -07:00
|
|
|
#include "hal/CAN.h"
|
|
|
|
|
#include "hal/DriverStation.h"
|
|
|
|
|
#include "hal/Errors.h"
|
|
|
|
|
#include "hal/HAL.h"
|
2016-12-23 11:13:17 -08:00
|
|
|
|
|
|
|
|
using namespace wpi::java;
|
2014-01-06 09:27:51 -05:00
|
|
|
|
2015-11-01 09:11:52 -08:00
|
|
|
#define kRioStatusOffset -63000
|
|
|
|
|
#define kRioStatusSuccess 0
|
|
|
|
|
#define kRIOStatusBufferInvalidSize (kRioStatusOffset - 80)
|
|
|
|
|
#define kRIOStatusOperationTimedOut -52007
|
|
|
|
|
#define kRIOStatusFeatureNotSupported (kRioStatusOffset - 193)
|
|
|
|
|
#define kRIOStatusResourceNotInitialized -52010
|
|
|
|
|
|
2021-09-13 22:05:38 -07:00
|
|
|
static_assert(edu_wpi_first_hal_HALUtil_RUNTIME_ROBORIO == HAL_Runtime_RoboRIO);
|
|
|
|
|
static_assert(edu_wpi_first_hal_HALUtil_RUNTIME_ROBORIO2 ==
|
|
|
|
|
HAL_Runtime_RoboRIO2);
|
|
|
|
|
static_assert(edu_wpi_first_hal_HALUtil_RUNTIME_SIMULATION ==
|
|
|
|
|
HAL_Runtime_Simulation);
|
|
|
|
|
|
2018-05-13 17:09:56 -07:00
|
|
|
static JavaVM* jvm = nullptr;
|
2016-12-23 11:13:17 -08:00
|
|
|
static JException illegalArgExCls;
|
|
|
|
|
static JException boundaryExCls;
|
|
|
|
|
static JException allocationExCls;
|
|
|
|
|
static JException halHandleExCls;
|
|
|
|
|
static JException canInvalidBufferExCls;
|
|
|
|
|
static JException canMessageNotFoundExCls;
|
|
|
|
|
static JException canMessageNotAllowedExCls;
|
|
|
|
|
static JException canNotInitializedExCls;
|
|
|
|
|
static JException uncleanStatusExCls;
|
2021-12-19 13:42:49 -08:00
|
|
|
static JClass powerDistributionVersionCls;
|
2016-12-23 11:13:17 -08:00
|
|
|
static JClass pwmConfigDataResultCls;
|
2017-10-21 15:32:05 -07:00
|
|
|
static JClass canStatusCls;
|
2017-11-09 19:59:29 -08:00
|
|
|
static JClass matchInfoDataCls;
|
2017-11-14 00:00:45 -08:00
|
|
|
static JClass accumulatorResultCls;
|
2018-05-21 16:09:38 -07:00
|
|
|
static JClass canDataCls;
|
2022-12-06 23:58:09 -06:00
|
|
|
static JClass canStreamMessageCls;
|
2019-09-28 11:34:46 -07:00
|
|
|
static JClass halValueCls;
|
2021-06-14 19:56:42 -07:00
|
|
|
static JClass baseStoreCls;
|
2021-12-19 13:41:35 -08:00
|
|
|
static JClass revPHVersionCls;
|
2015-11-01 09:11:52 -08:00
|
|
|
|
2018-05-29 15:44:16 -07:00
|
|
|
static const JClassInit classes[] = {
|
2021-12-19 13:42:49 -08:00
|
|
|
{"edu/wpi/first/hal/PowerDistributionVersion",
|
|
|
|
|
&powerDistributionVersionCls},
|
2018-09-20 21:59:46 -07:00
|
|
|
{"edu/wpi/first/hal/PWMConfigDataResult", &pwmConfigDataResultCls},
|
|
|
|
|
{"edu/wpi/first/hal/can/CANStatus", &canStatusCls},
|
|
|
|
|
{"edu/wpi/first/hal/MatchInfoData", &matchInfoDataCls},
|
|
|
|
|
{"edu/wpi/first/hal/AccumulatorResult", &accumulatorResultCls},
|
2019-09-28 11:34:46 -07:00
|
|
|
{"edu/wpi/first/hal/CANData", &canDataCls},
|
2022-12-06 23:58:09 -06:00
|
|
|
{"edu/wpi/first/hal/CANStreamMessage", &canStreamMessageCls},
|
2021-06-14 19:56:42 -07:00
|
|
|
{"edu/wpi/first/hal/HALValue", &halValueCls},
|
2021-12-19 13:41:35 -08:00
|
|
|
{"edu/wpi/first/hal/DMAJNISample$BaseStore", &baseStoreCls},
|
|
|
|
|
{"edu/wpi/first/hal/REVPHVersion", &revPHVersionCls}};
|
2018-05-29 15:44:16 -07:00
|
|
|
|
|
|
|
|
static const JExceptionInit exceptions[] = {
|
|
|
|
|
{"java/lang/IllegalArgumentException", &illegalArgExCls},
|
2018-09-20 21:59:46 -07:00
|
|
|
{"edu/wpi/first/hal/util/BoundaryException", &boundaryExCls},
|
|
|
|
|
{"edu/wpi/first/hal/util/AllocationException", &allocationExCls},
|
|
|
|
|
{"edu/wpi/first/hal/util/HalHandleException", &halHandleExCls},
|
|
|
|
|
{"edu/wpi/first/hal/can/CANInvalidBufferException", &canInvalidBufferExCls},
|
|
|
|
|
{"edu/wpi/first/hal/can/CANMessageNotFoundException",
|
2018-05-29 15:44:16 -07:00
|
|
|
&canMessageNotFoundExCls},
|
2018-09-20 21:59:46 -07:00
|
|
|
{"edu/wpi/first/hal/can/CANMessageNotAllowedException",
|
2018-05-29 15:44:16 -07:00
|
|
|
&canMessageNotAllowedExCls},
|
2018-09-20 21:59:46 -07:00
|
|
|
{"edu/wpi/first/hal/can/CANNotInitializedException",
|
2018-05-29 15:44:16 -07:00
|
|
|
&canNotInitializedExCls},
|
2018-09-20 21:59:46 -07:00
|
|
|
{"edu/wpi/first/hal/util/UncleanStatusException", &uncleanStatusExCls}};
|
2018-05-29 15:44:16 -07:00
|
|
|
|
2020-06-26 17:12:55 -07:00
|
|
|
namespace hal {
|
2016-10-31 23:04:49 -07:00
|
|
|
|
2021-06-06 16:13:58 -07:00
|
|
|
void ThrowUncleanStatusException(JNIEnv* env, std::string_view msg,
|
2018-05-30 23:36:40 -07:00
|
|
|
int32_t status) {
|
|
|
|
|
static jmethodID func =
|
|
|
|
|
env->GetMethodID(uncleanStatusExCls, "<init>", "(ILjava/lang/String;)V");
|
|
|
|
|
|
|
|
|
|
jobject exception =
|
|
|
|
|
env->NewObject(uncleanStatusExCls, func, static_cast<jint>(status),
|
|
|
|
|
MakeJString(env, msg));
|
|
|
|
|
env->Throw(static_cast<jthrowable>(exception));
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-01 10:28:30 -07:00
|
|
|
void ThrowAllocationException(JNIEnv* env, const char* lastError,
|
|
|
|
|
int32_t status) {
|
2021-06-06 16:13:58 -07:00
|
|
|
allocationExCls.Throw(env,
|
|
|
|
|
fmt::format("Code: {}\n{}", status, lastError).c_str());
|
2016-06-30 21:39:55 -07:00
|
|
|
}
|
|
|
|
|
|
2018-05-13 17:09:56 -07:00
|
|
|
void ThrowHalHandleException(JNIEnv* env, int32_t status) {
|
2021-04-29 09:56:54 -07:00
|
|
|
const char* message = HAL_GetLastError(&status);
|
2021-06-06 16:13:58 -07:00
|
|
|
halHandleExCls.Throw(env,
|
|
|
|
|
fmt::format(" Code: {}. {}", status, message).c_str());
|
2016-07-03 17:27:06 -07:00
|
|
|
}
|
|
|
|
|
|
2018-05-13 17:09:56 -07:00
|
|
|
void ReportError(JNIEnv* env, int32_t status, bool doThrow) {
|
2020-12-28 12:58:06 -08:00
|
|
|
if (status == 0) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-05-01 10:28:30 -07:00
|
|
|
const char* message = HAL_GetLastError(&status);
|
2016-07-03 17:27:06 -07:00
|
|
|
if (status == HAL_HANDLE_ERROR) {
|
2016-07-09 01:12:37 -07:00
|
|
|
ThrowHalHandleException(env, status);
|
2021-05-01 10:28:30 -07:00
|
|
|
return;
|
2016-07-03 17:27:06 -07:00
|
|
|
}
|
2017-08-07 17:36:34 -07:00
|
|
|
if (doThrow && status < 0) {
|
2021-06-06 16:13:58 -07:00
|
|
|
ThrowUncleanStatusException(
|
|
|
|
|
env, fmt::format(" Code: {}. {}", status, message).c_str(), status);
|
2015-11-01 09:11:52 -08:00
|
|
|
} else {
|
2016-02-04 22:29:11 -08:00
|
|
|
std::string func;
|
2019-02-11 20:17:31 -08:00
|
|
|
auto stack = GetJavaStackTrace(env, &func, "edu.wpi.first");
|
2021-04-29 09:56:54 -07:00
|
|
|
// Make a copy of message for safety, calling back into the HAL might
|
|
|
|
|
// invalidate the string.
|
2021-06-06 16:13:58 -07:00
|
|
|
std::string lastMessage{message};
|
2021-04-29 09:56:54 -07:00
|
|
|
HAL_SendError(1, status, 0, lastMessage.c_str(), func.c_str(),
|
|
|
|
|
stack.c_str(), 1);
|
2015-11-01 09:11:52 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-13 17:09:56 -07:00
|
|
|
void ThrowError(JNIEnv* env, int32_t status, int32_t minRange, int32_t maxRange,
|
2016-09-29 20:18:40 -07:00
|
|
|
int32_t requestedValue) {
|
2020-12-28 12:58:06 -08:00
|
|
|
if (status == 0) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-05-01 10:28:30 -07:00
|
|
|
const char* lastError = HAL_GetLastError(&status);
|
2018-05-13 17:09:56 -07:00
|
|
|
if (status == NO_AVAILABLE_RESOURCES || status == RESOURCE_IS_ALLOCATED ||
|
2016-09-29 20:18:40 -07:00
|
|
|
status == RESOURCE_OUT_OF_RANGE) {
|
2021-05-01 10:28:30 -07:00
|
|
|
ThrowAllocationException(env, lastError, status);
|
|
|
|
|
return;
|
2016-09-29 20:18:40 -07:00
|
|
|
}
|
|
|
|
|
if (status == HAL_HANDLE_ERROR) {
|
|
|
|
|
ThrowHalHandleException(env, status);
|
2021-05-01 10:28:30 -07:00
|
|
|
return;
|
2016-09-29 20:18:40 -07:00
|
|
|
}
|
2021-06-06 16:13:58 -07:00
|
|
|
ThrowUncleanStatusException(
|
|
|
|
|
env, fmt::format(" Code: {}. {}", status, lastError).c_str(), status);
|
2016-09-29 20:18:40 -07:00
|
|
|
}
|
|
|
|
|
|
2018-05-13 17:09:56 -07:00
|
|
|
void ReportCANError(JNIEnv* env, int32_t status, int message_id) {
|
2020-12-28 12:58:06 -08:00
|
|
|
if (status >= 0) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2015-11-01 09:11:52 -08:00
|
|
|
switch (status) {
|
|
|
|
|
case kRioStatusSuccess:
|
|
|
|
|
// Everything is ok... don't throw.
|
|
|
|
|
break;
|
2017-09-29 01:40:04 -04:00
|
|
|
case HAL_ERR_CANSessionMux_InvalidBuffer:
|
2015-11-01 09:11:52 -08:00
|
|
|
case kRIOStatusBufferInvalidSize: {
|
|
|
|
|
static jmethodID invalidBufConstruct = nullptr;
|
2020-12-28 12:58:06 -08:00
|
|
|
if (!invalidBufConstruct) {
|
2015-11-01 09:11:52 -08:00
|
|
|
invalidBufConstruct =
|
|
|
|
|
env->GetMethodID(canInvalidBufferExCls, "<init>", "()V");
|
2020-12-28 12:58:06 -08:00
|
|
|
}
|
2015-11-01 09:11:52 -08:00
|
|
|
jobject exception =
|
|
|
|
|
env->NewObject(canInvalidBufferExCls, invalidBufConstruct);
|
|
|
|
|
env->Throw(static_cast<jthrowable>(exception));
|
|
|
|
|
break;
|
|
|
|
|
}
|
2017-09-29 01:40:04 -04:00
|
|
|
case HAL_ERR_CANSessionMux_MessageNotFound:
|
2015-11-01 09:11:52 -08:00
|
|
|
case kRIOStatusOperationTimedOut: {
|
|
|
|
|
static jmethodID messageNotFoundConstruct = nullptr;
|
2020-12-28 12:58:06 -08:00
|
|
|
if (!messageNotFoundConstruct) {
|
2015-11-01 09:11:52 -08:00
|
|
|
messageNotFoundConstruct =
|
|
|
|
|
env->GetMethodID(canMessageNotFoundExCls, "<init>", "()V");
|
2020-12-28 12:58:06 -08:00
|
|
|
}
|
2015-11-01 09:11:52 -08:00
|
|
|
jobject exception =
|
|
|
|
|
env->NewObject(canMessageNotFoundExCls, messageNotFoundConstruct);
|
|
|
|
|
env->Throw(static_cast<jthrowable>(exception));
|
|
|
|
|
break;
|
|
|
|
|
}
|
2017-09-29 01:40:04 -04:00
|
|
|
case HAL_ERR_CANSessionMux_NotAllowed:
|
2015-11-01 09:11:52 -08:00
|
|
|
case kRIOStatusFeatureNotSupported: {
|
2021-06-06 16:13:58 -07:00
|
|
|
canMessageNotAllowedExCls.Throw(
|
|
|
|
|
env, fmt::format("MessageID = {}", message_id).c_str());
|
2015-11-01 09:11:52 -08:00
|
|
|
break;
|
|
|
|
|
}
|
2017-09-29 01:40:04 -04:00
|
|
|
case HAL_ERR_CANSessionMux_NotInitialized:
|
2015-11-01 09:11:52 -08:00
|
|
|
case kRIOStatusResourceNotInitialized: {
|
|
|
|
|
static jmethodID notInitConstruct = nullptr;
|
2020-12-28 12:58:06 -08:00
|
|
|
if (!notInitConstruct) {
|
2015-11-01 09:11:52 -08:00
|
|
|
notInitConstruct =
|
|
|
|
|
env->GetMethodID(canNotInitializedExCls, "<init>", "()V");
|
2020-12-28 12:58:06 -08:00
|
|
|
}
|
2015-11-01 09:11:52 -08:00
|
|
|
jobject exception =
|
|
|
|
|
env->NewObject(canNotInitializedExCls, notInitConstruct);
|
|
|
|
|
env->Throw(static_cast<jthrowable>(exception));
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
default: {
|
2021-06-06 16:13:58 -07:00
|
|
|
uncleanStatusExCls.Throw(
|
|
|
|
|
env, fmt::format("Fatal status code detected: {}", status).c_str());
|
2015-11-01 09:11:52 -08:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-06 16:13:58 -07:00
|
|
|
void ThrowIllegalArgumentException(JNIEnv* env, std::string_view msg) {
|
2016-12-23 11:13:17 -08:00
|
|
|
illegalArgExCls.Throw(env, msg);
|
2015-11-01 09:11:52 -08:00
|
|
|
}
|
|
|
|
|
|
2018-05-13 17:09:56 -07:00
|
|
|
void ThrowBoundaryException(JNIEnv* env, double value, double lower,
|
2015-11-01 09:11:52 -08:00
|
|
|
double upper) {
|
|
|
|
|
static jmethodID getMessage = nullptr;
|
2020-12-28 12:58:06 -08:00
|
|
|
if (!getMessage) {
|
2015-11-01 09:11:52 -08:00
|
|
|
getMessage = env->GetStaticMethodID(boundaryExCls, "getMessage",
|
|
|
|
|
"(DDD)Ljava/lang/String;");
|
2020-12-28 12:58:06 -08:00
|
|
|
}
|
2015-11-01 09:11:52 -08:00
|
|
|
|
|
|
|
|
static jmethodID constructor = nullptr;
|
2020-12-28 12:58:06 -08:00
|
|
|
if (!constructor) {
|
2015-11-01 09:11:52 -08:00
|
|
|
constructor =
|
|
|
|
|
env->GetMethodID(boundaryExCls, "<init>", "(Ljava/lang/String;)V");
|
2020-12-28 12:58:06 -08:00
|
|
|
}
|
2015-11-01 09:11:52 -08:00
|
|
|
|
2018-05-13 17:09:56 -07:00
|
|
|
jobject msg = env->CallStaticObjectMethod(
|
|
|
|
|
boundaryExCls, getMessage, static_cast<jdouble>(value),
|
|
|
|
|
static_cast<jdouble>(lower), static_cast<jdouble>(upper));
|
2015-11-01 09:11:52 -08:00
|
|
|
jobject ex = env->NewObject(boundaryExCls, constructor, msg);
|
|
|
|
|
env->Throw(static_cast<jthrowable>(ex));
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-13 17:09:56 -07:00
|
|
|
jobject CreatePWMConfigDataResult(JNIEnv* env, int32_t maxPwm,
|
|
|
|
|
int32_t deadbandMaxPwm, int32_t centerPwm,
|
|
|
|
|
int32_t deadbandMinPwm, int32_t minPwm) {
|
2016-07-08 21:29:29 -07:00
|
|
|
static jmethodID constructor =
|
2018-05-13 17:09:56 -07:00
|
|
|
env->GetMethodID(pwmConfigDataResultCls, "<init>", "(IIIII)V");
|
2020-12-28 11:04:20 -08:00
|
|
|
return env->NewObject(
|
|
|
|
|
pwmConfigDataResultCls, constructor, static_cast<jint>(maxPwm),
|
|
|
|
|
static_cast<jint>(deadbandMaxPwm), static_cast<jint>(centerPwm),
|
|
|
|
|
static_cast<jint>(deadbandMinPwm), static_cast<jint>(minPwm));
|
2016-07-08 21:29:29 -07:00
|
|
|
}
|
|
|
|
|
|
2021-12-19 13:41:35 -08:00
|
|
|
jobject CreateREVPHVersion(JNIEnv* env, uint32_t firmwareMajor,
|
|
|
|
|
uint32_t firmwareMinor, uint32_t firmwareFix,
|
|
|
|
|
uint32_t hardwareMinor, uint32_t hardwareMajor,
|
|
|
|
|
uint32_t uniqueId) {
|
|
|
|
|
static jmethodID constructor =
|
|
|
|
|
env->GetMethodID(revPHVersionCls, "<init>", "(IIIIII)V");
|
|
|
|
|
return env->NewObject(
|
|
|
|
|
revPHVersionCls, constructor, static_cast<jint>(firmwareMajor),
|
|
|
|
|
static_cast<jint>(firmwareMinor), static_cast<jint>(firmwareFix),
|
|
|
|
|
static_cast<jint>(hardwareMinor), static_cast<jint>(hardwareMajor),
|
|
|
|
|
static_cast<jint>(uniqueId));
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-13 17:09:56 -07:00
|
|
|
void SetCanStatusObject(JNIEnv* env, jobject canStatus,
|
|
|
|
|
float percentBusUtilization, uint32_t busOffCount,
|
|
|
|
|
uint32_t txFullCount, uint32_t receiveErrorCount,
|
2017-10-21 15:32:05 -07:00
|
|
|
uint32_t transmitErrorCount) {
|
2018-05-13 17:09:56 -07:00
|
|
|
static jmethodID func =
|
|
|
|
|
env->GetMethodID(canStatusCls, "setStatus", "(DIIII)V");
|
2020-12-28 11:04:20 -08:00
|
|
|
env->CallVoidMethod(
|
|
|
|
|
canStatus, func, static_cast<jdouble>(percentBusUtilization),
|
|
|
|
|
static_cast<jint>(busOffCount), static_cast<jint>(txFullCount),
|
|
|
|
|
static_cast<jint>(receiveErrorCount),
|
|
|
|
|
static_cast<jint>(transmitErrorCount));
|
2017-11-09 19:59:29 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SetMatchInfoObject(JNIEnv* env, jobject matchStatus,
|
|
|
|
|
const HAL_MatchInfo& matchInfo) {
|
2018-05-13 17:09:56 -07:00
|
|
|
static jmethodID func =
|
|
|
|
|
env->GetMethodID(matchInfoDataCls, "setData",
|
|
|
|
|
"(Ljava/lang/String;Ljava/lang/String;III)V");
|
|
|
|
|
|
2018-07-18 22:22:41 -07:00
|
|
|
env->CallVoidMethod(
|
|
|
|
|
matchStatus, func, MakeJString(env, matchInfo.eventName),
|
2021-06-06 16:13:58 -07:00
|
|
|
MakeJString(env,
|
|
|
|
|
{reinterpret_cast<const char*>(matchInfo.gameSpecificMessage),
|
|
|
|
|
matchInfo.gameSpecificMessageSize}),
|
2020-12-28 11:04:20 -08:00
|
|
|
static_cast<jint>(matchInfo.matchNumber),
|
|
|
|
|
static_cast<jint>(matchInfo.replayNumber),
|
|
|
|
|
static_cast<jint>(matchInfo.matchType));
|
2017-11-09 19:59:29 -08:00
|
|
|
}
|
2017-10-21 15:32:05 -07:00
|
|
|
|
2017-11-14 00:00:45 -08:00
|
|
|
void SetAccumulatorResultObject(JNIEnv* env, jobject accumulatorResult,
|
|
|
|
|
int64_t value, int64_t count) {
|
2018-05-13 17:09:56 -07:00
|
|
|
static jmethodID func =
|
|
|
|
|
env->GetMethodID(accumulatorResultCls, "set", "(JJ)V");
|
2017-11-14 00:00:45 -08:00
|
|
|
|
2020-12-28 11:04:20 -08:00
|
|
|
env->CallVoidMethod(accumulatorResult, func, static_cast<jlong>(value),
|
|
|
|
|
static_cast<jlong>(count));
|
2017-11-14 00:00:45 -08:00
|
|
|
}
|
|
|
|
|
|
2018-05-21 16:09:38 -07:00
|
|
|
jbyteArray SetCANDataObject(JNIEnv* env, jobject canData, int32_t length,
|
|
|
|
|
uint64_t timestamp) {
|
|
|
|
|
static jmethodID func = env->GetMethodID(canDataCls, "setData", "(IJ)[B");
|
|
|
|
|
|
2020-12-28 11:04:20 -08:00
|
|
|
jbyteArray retVal = static_cast<jbyteArray>(env->CallObjectMethod(
|
|
|
|
|
canData, func, static_cast<jint>(length), static_cast<jlong>(timestamp)));
|
2018-05-21 16:09:38 -07:00
|
|
|
return retVal;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-06 23:58:09 -06:00
|
|
|
jbyteArray SetCANStreamObject(JNIEnv* env, jobject canStreamData,
|
|
|
|
|
int32_t length, uint32_t messageID,
|
|
|
|
|
uint64_t timestamp) {
|
|
|
|
|
static jmethodID func =
|
|
|
|
|
env->GetMethodID(canStreamMessageCls, "setStreamData", "(IIJ)[B");
|
|
|
|
|
|
|
|
|
|
jbyteArray retVal = static_cast<jbyteArray>(env->CallObjectMethod(
|
|
|
|
|
canStreamData, func, static_cast<jint>(length),
|
|
|
|
|
static_cast<jint>(messageID), static_cast<jlong>(timestamp)));
|
|
|
|
|
return retVal;
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-28 11:34:46 -07:00
|
|
|
jobject CreateHALValue(JNIEnv* env, const HAL_Value& value) {
|
|
|
|
|
static jmethodID fromNative = env->GetStaticMethodID(
|
|
|
|
|
halValueCls, "fromNative", "(IJD)Ledu/wpi/first/hal/HALValue;");
|
|
|
|
|
jlong value1 = 0;
|
|
|
|
|
jdouble value2 = 0.0;
|
|
|
|
|
switch (value.type) {
|
|
|
|
|
case HAL_BOOLEAN:
|
|
|
|
|
value1 = value.data.v_boolean;
|
|
|
|
|
break;
|
|
|
|
|
case HAL_DOUBLE:
|
|
|
|
|
value2 = value.data.v_double;
|
|
|
|
|
break;
|
|
|
|
|
case HAL_ENUM:
|
|
|
|
|
value1 = value.data.v_enum;
|
|
|
|
|
break;
|
|
|
|
|
case HAL_INT:
|
|
|
|
|
value1 = value.data.v_int;
|
|
|
|
|
break;
|
|
|
|
|
case HAL_LONG:
|
|
|
|
|
value1 = value.data.v_long;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
2020-12-28 11:04:20 -08:00
|
|
|
return env->CallStaticObjectMethod(
|
|
|
|
|
halValueCls, fromNative, static_cast<jint>(value.type), value1, value2);
|
2019-09-28 11:34:46 -07:00
|
|
|
}
|
|
|
|
|
|
2021-06-14 19:56:42 -07:00
|
|
|
jobject CreateDMABaseStore(JNIEnv* env, jint valueType, jint index) {
|
|
|
|
|
static jmethodID ctor = env->GetMethodID(baseStoreCls, "<init>", "(II)V");
|
|
|
|
|
return env->NewObject(baseStoreCls, ctor, valueType, index);
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-19 13:42:49 -08:00
|
|
|
jobject CreatePowerDistributionVersion(JNIEnv* env, uint32_t firmwareMajor,
|
|
|
|
|
uint32_t firmwareMinor,
|
|
|
|
|
uint32_t firmwareFix,
|
|
|
|
|
uint32_t hardwareMinor,
|
|
|
|
|
uint32_t hardwareMajor,
|
|
|
|
|
uint32_t uniqueId) {
|
|
|
|
|
static jmethodID constructor =
|
|
|
|
|
env->GetMethodID(powerDistributionVersionCls, "<init>", "(IIIIII)V");
|
|
|
|
|
return env->NewObject(
|
|
|
|
|
powerDistributionVersionCls, constructor,
|
|
|
|
|
static_cast<jint>(firmwareMajor), static_cast<jint>(firmwareMinor),
|
|
|
|
|
static_cast<jint>(firmwareFix), static_cast<jint>(hardwareMinor),
|
|
|
|
|
static_cast<jint>(hardwareMajor), static_cast<jint>(uniqueId));
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
JavaVM* GetJVM() {
|
|
|
|
|
return jvm;
|
|
|
|
|
}
|
2018-04-29 13:29:07 -07:00
|
|
|
|
|
|
|
|
namespace sim {
|
2018-05-13 17:09:56 -07:00
|
|
|
jint SimOnLoad(JavaVM* vm, void* reserved);
|
|
|
|
|
void SimOnUnload(JavaVM* vm, void* reserved);
|
|
|
|
|
} // namespace sim
|
2018-04-29 13:29:07 -07:00
|
|
|
|
2020-06-26 17:12:55 -07:00
|
|
|
} // namespace hal
|
|
|
|
|
|
|
|
|
|
using namespace hal;
|
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
|
|
|
|
|
|
|
|
//
|
2015-11-01 09:11:52 -08:00
|
|
|
// indicate JNI version support desired and load classes
|
2014-01-06 09:27:51 -05:00
|
|
|
//
|
2018-05-13 17:09:56 -07:00
|
|
|
JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void* reserved) {
|
2015-11-01 09:11:52 -08:00
|
|
|
jvm = vm;
|
|
|
|
|
|
2018-05-13 17:09:56 -07:00
|
|
|
JNIEnv* env;
|
2020-12-28 12:58:06 -08:00
|
|
|
if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK) {
|
2015-11-01 09:11:52 -08:00
|
|
|
return JNI_ERR;
|
2020-12-28 12:58:06 -08:00
|
|
|
}
|
2015-11-01 09:11:52 -08:00
|
|
|
|
2018-05-29 15:44:16 -07:00
|
|
|
for (auto& c : classes) {
|
|
|
|
|
*c.cls = JClass(env, c.name);
|
2020-12-28 12:58:06 -08:00
|
|
|
if (!*c.cls) {
|
|
|
|
|
return JNI_ERR;
|
|
|
|
|
}
|
2018-05-29 15:44:16 -07:00
|
|
|
}
|
2017-11-14 00:00:45 -08:00
|
|
|
|
2018-05-29 15:44:16 -07:00
|
|
|
for (auto& c : exceptions) {
|
|
|
|
|
*c.cls = JException(env, c.name);
|
2020-12-28 12:58:06 -08:00
|
|
|
if (!*c.cls) {
|
|
|
|
|
return JNI_ERR;
|
|
|
|
|
}
|
2018-05-29 15:44:16 -07:00
|
|
|
}
|
2018-05-21 16:09:38 -07:00
|
|
|
|
2018-04-29 13:29:07 -07:00
|
|
|
return sim::SimOnLoad(vm, reserved);
|
2014-01-06 09:27:51 -05:00
|
|
|
}
|
|
|
|
|
|
2018-05-13 17:09:56 -07:00
|
|
|
JNIEXPORT void JNICALL JNI_OnUnload(JavaVM* vm, void* reserved) {
|
2018-04-29 13:29:07 -07:00
|
|
|
sim::SimOnUnload(vm, reserved);
|
|
|
|
|
|
2018-05-13 17:09:56 -07:00
|
|
|
JNIEnv* env;
|
2020-12-28 12:58:06 -08:00
|
|
|
if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK) {
|
2015-11-01 09:11:52 -08:00
|
|
|
return;
|
2020-12-28 12:58:06 -08:00
|
|
|
}
|
2015-11-01 09:11:52 -08:00
|
|
|
// Delete global references
|
2018-05-29 15:44:16 -07:00
|
|
|
|
|
|
|
|
for (auto& c : classes) {
|
|
|
|
|
c.cls->free(env);
|
|
|
|
|
}
|
|
|
|
|
for (auto& c : exceptions) {
|
|
|
|
|
c.cls->free(env);
|
|
|
|
|
}
|
2015-11-01 09:11:52 -08:00
|
|
|
jvm = nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-06 09:27:51 -05:00
|
|
|
/*
|
2018-09-20 21:59:46 -07:00
|
|
|
* Class: edu_wpi_first_hal_HALUtil
|
2014-01-06 09:27:51 -05:00
|
|
|
* Method: getFPGAVersion
|
2015-11-01 09:11:52 -08:00
|
|
|
* Signature: ()S
|
2014-01-06 09:27:51 -05:00
|
|
|
*/
|
2016-05-20 17:30:37 -07:00
|
|
|
JNIEXPORT jshort JNICALL
|
2018-09-20 21:59:46 -07:00
|
|
|
Java_edu_wpi_first_hal_HALUtil_getFPGAVersion
|
2018-05-13 17:09:56 -07:00
|
|
|
(JNIEnv* env, jclass)
|
|
|
|
|
{
|
2015-11-01 09:11:52 -08:00
|
|
|
int32_t status = 0;
|
2016-07-09 00:24:26 -07:00
|
|
|
jshort returnValue = HAL_GetFPGAVersion(&status);
|
2015-11-01 09:11:52 -08:00
|
|
|
CheckStatus(env, status);
|
2015-08-19 11:12:54 -04:00
|
|
|
return returnValue;
|
2014-01-06 09:27:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
2018-09-20 21:59:46 -07:00
|
|
|
* Class: edu_wpi_first_hal_HALUtil
|
2014-01-06 09:27:51 -05:00
|
|
|
* Method: getFPGARevision
|
2015-11-01 09:11:52 -08:00
|
|
|
* Signature: ()I
|
2014-01-06 09:27:51 -05:00
|
|
|
*/
|
2016-05-20 17:30:37 -07:00
|
|
|
JNIEXPORT jint JNICALL
|
2018-09-20 21:59:46 -07:00
|
|
|
Java_edu_wpi_first_hal_HALUtil_getFPGARevision
|
2018-05-13 17:09:56 -07:00
|
|
|
(JNIEnv* env, jclass)
|
|
|
|
|
{
|
2015-11-01 09:11:52 -08:00
|
|
|
int32_t status = 0;
|
2016-07-09 00:24:26 -07:00
|
|
|
jint returnValue = HAL_GetFPGARevision(&status);
|
2015-11-01 09:11:52 -08:00
|
|
|
CheckStatus(env, status);
|
2015-08-19 11:12:54 -04:00
|
|
|
return returnValue;
|
2014-01-06 09:27:51 -05:00
|
|
|
}
|
|
|
|
|
|
2022-12-09 00:58:55 -05:00
|
|
|
/*
|
|
|
|
|
* Class: edu_wpi_first_hal_HALUtil
|
|
|
|
|
* Method: getSerialNumber
|
|
|
|
|
* Signature: ()Ljava/lang/String;
|
|
|
|
|
*/
|
|
|
|
|
JNIEXPORT jstring JNICALL
|
|
|
|
|
Java_edu_wpi_first_hal_HALUtil_getSerialNumber
|
|
|
|
|
(JNIEnv* env, jclass)
|
|
|
|
|
{
|
|
|
|
|
char serialNum[9];
|
|
|
|
|
size_t len = HAL_GetSerialNumber(serialNum, sizeof(serialNum));
|
|
|
|
|
return MakeJString(env, std::string_view(serialNum, len));
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-26 14:39:51 -05:00
|
|
|
/*
|
|
|
|
|
* Class: edu_wpi_first_hal_HALUtil
|
|
|
|
|
* Method: getComments
|
|
|
|
|
* Signature: ()Ljava/lang/String;
|
|
|
|
|
*/
|
|
|
|
|
JNIEXPORT jstring JNICALL
|
|
|
|
|
Java_edu_wpi_first_hal_HALUtil_getComments
|
|
|
|
|
(JNIEnv* env, jclass)
|
|
|
|
|
{
|
|
|
|
|
char comments[65];
|
|
|
|
|
size_t len = HAL_GetComments(comments, sizeof(comments));
|
|
|
|
|
return MakeJString(env, std::string_view(comments, len));
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-02 02:34:18 -04:00
|
|
|
/*
|
|
|
|
|
* Class: edu_wpi_first_hal_HALUtil
|
|
|
|
|
* Method: getTeamNumber
|
|
|
|
|
* Signature: ()I
|
|
|
|
|
*/
|
|
|
|
|
JNIEXPORT jint JNICALL
|
|
|
|
|
Java_edu_wpi_first_hal_HALUtil_getTeamNumber
|
|
|
|
|
(JNIEnv* env, jclass)
|
|
|
|
|
{
|
|
|
|
|
return HAL_GetTeamNumber();
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-06 09:27:51 -05:00
|
|
|
/*
|
2018-09-20 21:59:46 -07:00
|
|
|
* Class: edu_wpi_first_hal_HALUtil
|
2014-01-06 09:27:51 -05:00
|
|
|
* Method: getFPGATime
|
2015-11-01 09:11:52 -08:00
|
|
|
* Signature: ()J
|
2014-01-06 09:27:51 -05:00
|
|
|
*/
|
2016-05-20 17:30:37 -07:00
|
|
|
JNIEXPORT jlong JNICALL
|
2018-09-20 21:59:46 -07:00
|
|
|
Java_edu_wpi_first_hal_HALUtil_getFPGATime
|
2018-05-13 17:09:56 -07:00
|
|
|
(JNIEnv* env, jclass)
|
|
|
|
|
{
|
2015-11-01 09:11:52 -08:00
|
|
|
int32_t status = 0;
|
2016-07-09 00:24:26 -07:00
|
|
|
jlong returnValue = HAL_GetFPGATime(&status);
|
2015-11-01 09:11:52 -08:00
|
|
|
CheckStatus(env, status);
|
2015-08-19 11:12:54 -04:00
|
|
|
return returnValue;
|
2014-01-06 09:27:51 -05:00
|
|
|
}
|
|
|
|
|
|
2016-09-13 21:21:57 -07:00
|
|
|
/*
|
2018-09-20 21:59:46 -07:00
|
|
|
* Class: edu_wpi_first_hal_HALUtil
|
2016-09-13 21:21:57 -07:00
|
|
|
* Method: getHALRuntimeType
|
|
|
|
|
* Signature: ()I
|
|
|
|
|
*/
|
|
|
|
|
JNIEXPORT jint JNICALL
|
2018-09-20 21:59:46 -07:00
|
|
|
Java_edu_wpi_first_hal_HALUtil_getHALRuntimeType
|
2018-05-13 17:09:56 -07:00
|
|
|
(JNIEnv* env, jclass)
|
|
|
|
|
{
|
2016-09-13 21:21:57 -07:00
|
|
|
jint returnValue = HAL_GetRuntimeType();
|
|
|
|
|
return returnValue;
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-29 14:34:52 -04:00
|
|
|
/*
|
2018-09-20 21:59:46 -07:00
|
|
|
* Class: edu_wpi_first_hal_HALUtil
|
2014-07-29 14:34:52 -04:00
|
|
|
* Method: getFPGAButton
|
2018-05-13 17:09:56 -07:00
|
|
|
* Signature: ()Z
|
2014-07-29 14:34:52 -04:00
|
|
|
*/
|
2016-05-20 17:30:37 -07:00
|
|
|
JNIEXPORT jboolean JNICALL
|
2018-09-20 21:59:46 -07:00
|
|
|
Java_edu_wpi_first_hal_HALUtil_getFPGAButton
|
2018-05-13 17:09:56 -07:00
|
|
|
(JNIEnv* env, jclass)
|
|
|
|
|
{
|
2015-11-01 09:11:52 -08:00
|
|
|
int32_t status = 0;
|
2016-07-09 00:24:26 -07:00
|
|
|
jboolean returnValue = HAL_GetFPGAButton(&status);
|
2015-11-01 09:11:52 -08:00
|
|
|
CheckStatus(env, status);
|
2015-08-19 11:12:54 -04:00
|
|
|
return returnValue;
|
2014-07-29 14:34:52 -04:00
|
|
|
}
|
|
|
|
|
|
2014-01-06 09:27:51 -05:00
|
|
|
/*
|
2018-09-20 21:59:46 -07:00
|
|
|
* Class: edu_wpi_first_hal_HALUtil
|
2016-11-24 21:49:34 -08:00
|
|
|
* Method: getHALErrorMessage
|
2014-01-06 09:27:51 -05:00
|
|
|
* Signature: (I)Ljava/lang/String;
|
|
|
|
|
*/
|
2016-05-20 17:30:37 -07:00
|
|
|
JNIEXPORT jstring JNICALL
|
2018-09-20 21:59:46 -07:00
|
|
|
Java_edu_wpi_first_hal_HALUtil_getHALErrorMessage
|
2018-05-13 17:09:56 -07:00
|
|
|
(JNIEnv* paramEnv, jclass, jint paramId)
|
|
|
|
|
{
|
|
|
|
|
const char* msg = HAL_GetErrorMessage(paramId);
|
2016-12-23 11:13:17 -08:00
|
|
|
return MakeJString(paramEnv, msg);
|
2014-01-06 09:27:51 -05:00
|
|
|
}
|
2014-08-04 14:19:01 -04:00
|
|
|
|
|
|
|
|
/*
|
2018-09-20 21:59:46 -07:00
|
|
|
* Class: edu_wpi_first_hal_HALUtil
|
2014-08-04 14:19:01 -04:00
|
|
|
* Method: getHALErrno
|
|
|
|
|
* Signature: ()I
|
|
|
|
|
*/
|
2016-05-20 17:30:37 -07:00
|
|
|
JNIEXPORT jint JNICALL
|
2018-09-20 21:59:46 -07:00
|
|
|
Java_edu_wpi_first_hal_HALUtil_getHALErrno
|
2018-05-13 17:09:56 -07:00
|
|
|
(JNIEnv*, jclass)
|
|
|
|
|
{
|
2015-08-19 11:12:54 -04:00
|
|
|
return errno;
|
2014-08-04 14:19:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
2018-09-20 21:59:46 -07:00
|
|
|
* Class: edu_wpi_first_hal_HALUtil
|
2014-08-04 14:19:01 -04:00
|
|
|
* Method: getHALstrerror
|
|
|
|
|
* Signature: (I)Ljava/lang/String;
|
|
|
|
|
*/
|
2018-05-13 17:09:56 -07:00
|
|
|
JNIEXPORT jstring JNICALL
|
2018-09-20 21:59:46 -07:00
|
|
|
Java_edu_wpi_first_hal_HALUtil_getHALstrerror
|
2018-05-13 17:09:56 -07:00
|
|
|
(JNIEnv* env, jclass, jint errorCode)
|
|
|
|
|
{
|
|
|
|
|
const char* msg = std::strerror(errno);
|
2016-12-23 11:13:17 -08:00
|
|
|
return MakeJString(env, 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
|
|
|
|
2015-11-01 09:11:52 -08:00
|
|
|
} // extern "C"
|