2016-01-02 03:02:34 -08:00
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
/* Copyright (c) FIRST 2016. All Rights Reserved. */
|
|
|
|
|
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
|
|
|
|
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
|
|
|
|
/* the project. */
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
2015-11-01 09:11:52 -08:00
|
|
|
#include "HALUtil.h"
|
2016-05-25 22:38:11 -07:00
|
|
|
|
2014-01-06 09:27:51 -05:00
|
|
|
#include <assert.h>
|
2016-05-25 22:38:11 -07:00
|
|
|
#include <errno.h>
|
2016-05-20 17:30:37 -07:00
|
|
|
#include <jni.h>
|
2016-05-25 22:38:11 -07:00
|
|
|
|
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
|
|
|
|
2017-09-29 01:40:04 -04:00
|
|
|
#include "HAL/CAN.h"
|
2016-05-22 21:41:22 -07:00
|
|
|
#include "HAL/HAL.h"
|
2016-12-21 21:55:31 -08:00
|
|
|
#include "HAL/DriverStation.h"
|
|
|
|
|
#include "HAL/Errors.h"
|
2016-07-14 00:17:29 -07:00
|
|
|
#include "HAL/cpp/Log.h"
|
2016-05-20 17:30:37 -07:00
|
|
|
#include "edu_wpi_first_wpilibj_hal_HALUtil.h"
|
2018-04-29 23:33:19 -07:00
|
|
|
#include "wpi/SmallString.h"
|
|
|
|
|
#include "wpi/jni_util.h"
|
|
|
|
|
#include "wpi/raw_ostream.h"
|
2016-12-23 11:13:17 -08:00
|
|
|
|
|
|
|
|
using namespace wpi::java;
|
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
|
|
|
|
2016-05-20 17:30:37 -07:00
|
|
|
#define HALUTIL_LOG(level) \
|
|
|
|
|
if (level > halUtilLogLevel) \
|
|
|
|
|
; \
|
|
|
|
|
else \
|
|
|
|
|
Log().Get(level)
|
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
|
|
|
|
|
|
2018-04-29 13:29:07 -07:00
|
|
|
static JavaVM *jvm = nullptr;
|
2016-12-23 11:13:17 -08:00
|
|
|
static JException runtimeExCls;
|
|
|
|
|
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;
|
|
|
|
|
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;
|
2015-11-01 09:11:52 -08:00
|
|
|
|
2016-10-31 23:04:49 -07:00
|
|
|
namespace frc {
|
|
|
|
|
|
2017-06-30 22:33:43 -04:00
|
|
|
void ThrowAllocationException(JNIEnv *env, int32_t minRange, int32_t maxRange,
|
2016-07-13 20:29:28 -07:00
|
|
|
int32_t requestedValue, int32_t status) {
|
2016-07-09 00:24:26 -07:00
|
|
|
const char *message = HAL_GetErrorMessage(status);
|
2018-04-29 23:33:19 -07:00
|
|
|
wpi::SmallString<1024> buf;
|
|
|
|
|
wpi::raw_svector_ostream oss(buf);
|
2017-05-15 23:10:40 -07:00
|
|
|
oss << " Code: " << status << ". " << message << ", Minimum Value: "
|
|
|
|
|
<< minRange << ", Maximum Value: " << maxRange << ", Requested Value: "
|
|
|
|
|
<< requestedValue;
|
|
|
|
|
env->ThrowNew(allocationExCls, buf.c_str());
|
|
|
|
|
allocationExCls.Throw(env, buf.c_str());
|
2016-06-30 21:39:55 -07:00
|
|
|
}
|
|
|
|
|
|
2016-07-09 01:12:37 -07:00
|
|
|
void ThrowHalHandleException(JNIEnv *env, int32_t status) {
|
2016-07-09 00:24:26 -07:00
|
|
|
const char *message = HAL_GetErrorMessage(status);
|
2018-04-29 23:33:19 -07:00
|
|
|
wpi::SmallString<1024> buf;
|
|
|
|
|
wpi::raw_svector_ostream oss(buf);
|
2017-05-15 23:10:40 -07:00
|
|
|
oss << " Code: " << status << ". " << message;
|
|
|
|
|
halHandleExCls.Throw(env, buf.c_str());
|
2016-07-03 17:27:06 -07:00
|
|
|
}
|
|
|
|
|
|
2017-08-07 17:36:34 -07:00
|
|
|
void ReportError(JNIEnv *env, int32_t status, bool doThrow) {
|
2015-11-01 09:11:52 -08:00
|
|
|
if (status == 0) return;
|
2016-07-03 17:27:06 -07:00
|
|
|
if (status == HAL_HANDLE_ERROR) {
|
2016-07-09 01:12:37 -07:00
|
|
|
ThrowHalHandleException(env, status);
|
2016-07-03 17:27:06 -07:00
|
|
|
}
|
2016-07-09 00:24:26 -07:00
|
|
|
const char *message = HAL_GetErrorMessage(status);
|
2017-08-07 17:36:34 -07:00
|
|
|
if (doThrow && status < 0) {
|
2018-04-29 23:33:19 -07:00
|
|
|
wpi::SmallString<1024> buf;
|
|
|
|
|
wpi::raw_svector_ostream oss(buf);
|
2017-05-15 23:10:40 -07:00
|
|
|
oss << " Code: " << status << ". " << message;
|
|
|
|
|
runtimeExCls.Throw(env, buf.c_str());
|
2015-11-01 09:11:52 -08:00
|
|
|
} else {
|
2016-02-04 22:29:11 -08:00
|
|
|
std::string func;
|
2017-08-22 20:53:01 -07:00
|
|
|
auto stack = GetJavaStackTrace(env, &func, "edu.wpi.first.wpilibj");
|
2016-07-09 00:24:26 -07:00
|
|
|
HAL_SendError(1, status, 0, message, func.c_str(), stack.c_str(), 1);
|
2015-11-01 09:11:52 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-30 22:33:43 -04: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) {
|
|
|
|
|
if (status == 0) return;
|
2017-06-30 22:33:43 -04:00
|
|
|
if (status == NO_AVAILABLE_RESOURCES ||
|
|
|
|
|
status == RESOURCE_IS_ALLOCATED ||
|
2016-09-29 20:18:40 -07:00
|
|
|
status == RESOURCE_OUT_OF_RANGE) {
|
|
|
|
|
ThrowAllocationException(env, minRange, maxRange, requestedValue, status);
|
|
|
|
|
}
|
|
|
|
|
if (status == HAL_HANDLE_ERROR) {
|
|
|
|
|
ThrowHalHandleException(env, status);
|
|
|
|
|
}
|
|
|
|
|
const char *message = HAL_GetErrorMessage(status);
|
2018-04-29 23:33:19 -07:00
|
|
|
wpi::SmallString<1024> buf;
|
|
|
|
|
wpi::raw_svector_ostream oss(buf);
|
2017-05-15 23:10:40 -07:00
|
|
|
oss << " Code: " << status << ". " << message;
|
|
|
|
|
runtimeExCls.Throw(env, buf.c_str());
|
2016-09-29 20:18:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ReportCANError(JNIEnv *env, int32_t status, int message_id) {
|
2015-11-01 09:11:52 -08:00
|
|
|
if (status >= 0) return;
|
|
|
|
|
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;
|
|
|
|
|
if (!invalidBufConstruct)
|
|
|
|
|
invalidBufConstruct =
|
|
|
|
|
env->GetMethodID(canInvalidBufferExCls, "<init>", "()V");
|
|
|
|
|
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;
|
|
|
|
|
if (!messageNotFoundConstruct)
|
|
|
|
|
messageNotFoundConstruct =
|
|
|
|
|
env->GetMethodID(canMessageNotFoundExCls, "<init>", "()V");
|
|
|
|
|
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: {
|
2018-04-29 23:33:19 -07:00
|
|
|
wpi::SmallString<100> buf;
|
|
|
|
|
wpi::raw_svector_ostream oss(buf);
|
2017-05-15 23:10:40 -07:00
|
|
|
oss << "MessageID = " << message_id;
|
|
|
|
|
canMessageNotAllowedExCls.Throw(env, buf.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;
|
|
|
|
|
if (!notInitConstruct)
|
|
|
|
|
notInitConstruct =
|
|
|
|
|
env->GetMethodID(canNotInitializedExCls, "<init>", "()V");
|
|
|
|
|
jobject exception =
|
|
|
|
|
env->NewObject(canNotInitializedExCls, notInitConstruct);
|
|
|
|
|
env->Throw(static_cast<jthrowable>(exception));
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
default: {
|
2018-04-29 23:33:19 -07:00
|
|
|
wpi::SmallString<100> buf;
|
|
|
|
|
wpi::raw_svector_ostream oss(buf);
|
2017-05-15 23:10:40 -07:00
|
|
|
oss << "Fatal status code detected: " << status;
|
|
|
|
|
uncleanStatusExCls.Throw(env, buf.c_str());
|
2015-11-01 09:11:52 -08:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ThrowIllegalArgumentException(JNIEnv *env, const char *msg) {
|
2016-12-23 11:13:17 -08:00
|
|
|
illegalArgExCls.Throw(env, msg);
|
2015-11-01 09:11:52 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ThrowBoundaryException(JNIEnv *env, double value, double lower,
|
|
|
|
|
double upper) {
|
|
|
|
|
static jmethodID getMessage = nullptr;
|
|
|
|
|
if (!getMessage)
|
|
|
|
|
getMessage = env->GetStaticMethodID(boundaryExCls, "getMessage",
|
|
|
|
|
"(DDD)Ljava/lang/String;");
|
|
|
|
|
|
|
|
|
|
static jmethodID constructor = nullptr;
|
|
|
|
|
if (!constructor)
|
|
|
|
|
constructor =
|
|
|
|
|
env->GetMethodID(boundaryExCls, "<init>", "(Ljava/lang/String;)V");
|
|
|
|
|
|
|
|
|
|
jobject msg =
|
2016-08-24 21:39:16 -07:00
|
|
|
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));
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-08 21:29:29 -07:00
|
|
|
jobject CreatePWMConfigDataResult(JNIEnv *env, int32_t maxPwm,
|
|
|
|
|
int32_t deadbandMaxPwm, int32_t centerPwm,
|
|
|
|
|
int32_t deadbandMinPwm, int32_t minPwm) {
|
|
|
|
|
static jmethodID constructor =
|
|
|
|
|
env->GetMethodID(pwmConfigDataResultCls, "<init>",
|
|
|
|
|
"(IIIII)V");
|
|
|
|
|
return env->NewObject(pwmConfigDataResultCls, constructor, maxPwm,
|
|
|
|
|
deadbandMaxPwm, centerPwm, deadbandMinPwm,
|
|
|
|
|
minPwm);
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-09 19:59:29 -08:00
|
|
|
void SetCanStatusObject(JNIEnv *env, jobject canStatus,
|
2017-10-21 15:32:05 -07:00
|
|
|
float percentBusUtilization,
|
2017-11-09 19:59:29 -08:00
|
|
|
uint32_t busOffCount, uint32_t txFullCount,
|
|
|
|
|
uint32_t receiveErrorCount,
|
2017-10-21 15:32:05 -07:00
|
|
|
uint32_t transmitErrorCount) {
|
2017-11-09 19:59:29 -08:00
|
|
|
static jmethodID func = env->GetMethodID(canStatusCls, "setStatus",
|
|
|
|
|
"(DIIII)V");
|
|
|
|
|
env->CallObjectMethod(canStatus, func, (jdouble)percentBusUtilization,
|
|
|
|
|
(jint)busOffCount, (jint)txFullCount,
|
|
|
|
|
(jint)receiveErrorCount, (jint)transmitErrorCount);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SetMatchInfoObject(JNIEnv* env, jobject matchStatus,
|
|
|
|
|
const HAL_MatchInfo& matchInfo) {
|
|
|
|
|
static jmethodID func = env->GetMethodID(matchInfoDataCls, "setData",
|
|
|
|
|
"(Ljava/lang/String;Ljava/lang/String;III)V");
|
|
|
|
|
|
|
|
|
|
env->CallObjectMethod(matchStatus, func,
|
|
|
|
|
MakeJString(env, matchInfo.eventName),
|
|
|
|
|
MakeJString(env, matchInfo.gameSpecificMessage),
|
|
|
|
|
(jint)matchInfo.matchNumber,
|
|
|
|
|
(jint)matchInfo.replayNumber,
|
|
|
|
|
(jint)matchInfo.matchType);
|
|
|
|
|
}
|
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) {
|
|
|
|
|
static jmethodID func = env->GetMethodID(accumulatorResultCls, "set",
|
|
|
|
|
"(JJ)V");
|
|
|
|
|
|
|
|
|
|
env->CallObjectMethod(accumulatorResult, func, (jlong)value, (jlong)count);
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-29 13:29:07 -07:00
|
|
|
JavaVM* GetJVM() {
|
|
|
|
|
return jvm;
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-31 23:04:49 -07:00
|
|
|
} // namespace frc
|
|
|
|
|
|
2018-04-29 13:29:07 -07:00
|
|
|
namespace sim {
|
|
|
|
|
jint SimOnLoad(JavaVM* vm, void* reserved);
|
|
|
|
|
void SimOnUnload(JavaVM* vm, void* reserved);
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-31 23:04:49 -07:00
|
|
|
using namespace frc;
|
|
|
|
|
|
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
|
|
|
//
|
2016-05-20 17:30:37 -07:00
|
|
|
JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) {
|
2015-11-01 09:11:52 -08:00
|
|
|
jvm = vm;
|
|
|
|
|
|
2015-08-19 11:12:54 -04:00
|
|
|
// set our logging level
|
|
|
|
|
Log::ReportingLevel() = logDEBUG;
|
2015-11-01 09:11:52 -08:00
|
|
|
|
|
|
|
|
JNIEnv *env;
|
|
|
|
|
if (vm->GetEnv(reinterpret_cast<void **>(&env), JNI_VERSION_1_6) != JNI_OK)
|
|
|
|
|
return JNI_ERR;
|
|
|
|
|
|
2016-12-23 11:13:17 -08:00
|
|
|
runtimeExCls = JException(env, "java/lang/RuntimeException");
|
2015-11-01 09:11:52 -08:00
|
|
|
if (!runtimeExCls) return JNI_ERR;
|
|
|
|
|
|
2016-12-23 11:13:17 -08:00
|
|
|
illegalArgExCls = JException(env, "java/lang/IllegalArgumentException");
|
2015-11-01 09:11:52 -08:00
|
|
|
if (!illegalArgExCls) return JNI_ERR;
|
|
|
|
|
|
2016-12-23 11:13:17 -08:00
|
|
|
boundaryExCls = JException(env, "edu/wpi/first/wpilibj/util/BoundaryException");
|
2015-11-01 09:11:52 -08:00
|
|
|
if (!boundaryExCls) return JNI_ERR;
|
2016-12-23 11:13:17 -08:00
|
|
|
|
|
|
|
|
allocationExCls = JException(env, "edu/wpi/first/wpilibj/util/AllocationException");
|
2016-06-30 21:39:55 -07:00
|
|
|
if (!allocationExCls) return JNI_ERR;
|
2016-12-23 11:13:17 -08:00
|
|
|
|
|
|
|
|
halHandleExCls = JException(env, "edu/wpi/first/wpilibj/util/HalHandleException");
|
2016-07-03 17:27:06 -07:00
|
|
|
if (!halHandleExCls) return JNI_ERR;
|
2015-11-01 09:11:52 -08:00
|
|
|
|
2016-12-23 11:13:17 -08:00
|
|
|
canInvalidBufferExCls = JException(env, "edu/wpi/first/wpilibj/can/CANInvalidBufferException");
|
2015-11-01 09:11:52 -08:00
|
|
|
if (!canInvalidBufferExCls) return JNI_ERR;
|
|
|
|
|
|
2016-12-23 11:13:17 -08:00
|
|
|
canMessageNotFoundExCls = JException(env, "edu/wpi/first/wpilibj/can/CANMessageNotFoundException");
|
2015-11-01 09:11:52 -08:00
|
|
|
if (!canMessageNotFoundExCls) return JNI_ERR;
|
|
|
|
|
|
2016-12-23 11:13:17 -08:00
|
|
|
canMessageNotAllowedExCls = JException(env, "edu/wpi/first/wpilibj/can/CANMessageNotAllowedException");
|
2015-11-01 09:11:52 -08:00
|
|
|
if (!canMessageNotAllowedExCls) return JNI_ERR;
|
|
|
|
|
|
2016-12-23 11:13:17 -08:00
|
|
|
canNotInitializedExCls = JException(env, "edu/wpi/first/wpilibj/can/CANNotInitializedException");
|
2015-11-01 09:11:52 -08:00
|
|
|
if (!canNotInitializedExCls) return JNI_ERR;
|
|
|
|
|
|
2016-12-23 11:13:17 -08:00
|
|
|
uncleanStatusExCls = JException(env,"edu/wpi/first/wpilibj/util/UncleanStatusException");
|
2015-11-01 09:11:52 -08:00
|
|
|
if (!uncleanStatusExCls) return JNI_ERR;
|
2016-12-23 11:13:17 -08:00
|
|
|
|
|
|
|
|
pwmConfigDataResultCls = JClass(env, "edu/wpi/first/wpilibj/PWMConfigDataResult");
|
2016-07-08 21:29:29 -07:00
|
|
|
if (!pwmConfigDataResultCls) return JNI_ERR;
|
2015-11-01 09:11:52 -08:00
|
|
|
|
2017-10-21 15:32:05 -07:00
|
|
|
canStatusCls = JClass(env, "edu/wpi/first/wpilibj/can/CANStatus");
|
|
|
|
|
if (!canStatusCls) return JNI_ERR;
|
|
|
|
|
|
2017-11-09 19:59:29 -08:00
|
|
|
matchInfoDataCls = JClass(env, "edu/wpi/first/wpilibj/hal/MatchInfoData");
|
|
|
|
|
if (!matchInfoDataCls) return JNI_ERR;
|
|
|
|
|
|
2017-11-14 00:00:45 -08:00
|
|
|
accumulatorResultCls = JClass(env, "edu/wpi/first/wpilibj/AccumulatorResult");
|
|
|
|
|
if (!accumulatorResultCls) return JNI_ERR;
|
|
|
|
|
|
2018-04-29 13:29:07 -07:00
|
|
|
return sim::SimOnLoad(vm, reserved);
|
2014-01-06 09:27:51 -05:00
|
|
|
}
|
|
|
|
|
|
2016-05-20 17:30:37 -07:00
|
|
|
JNIEXPORT void JNICALL JNI_OnUnload(JavaVM *vm, void *reserved) {
|
2018-04-29 13:29:07 -07:00
|
|
|
sim::SimOnUnload(vm, reserved);
|
|
|
|
|
|
2015-11-01 09:11:52 -08:00
|
|
|
JNIEnv *env;
|
|
|
|
|
if (vm->GetEnv(reinterpret_cast<void **>(&env), JNI_VERSION_1_6) != JNI_OK)
|
|
|
|
|
return;
|
|
|
|
|
// Delete global references
|
2016-12-23 11:13:17 -08:00
|
|
|
runtimeExCls.free(env);
|
|
|
|
|
illegalArgExCls.free(env);
|
|
|
|
|
boundaryExCls.free(env);
|
|
|
|
|
allocationExCls.free(env);
|
|
|
|
|
halHandleExCls.free(env);
|
|
|
|
|
canInvalidBufferExCls.free(env);
|
|
|
|
|
canMessageNotFoundExCls.free(env);
|
|
|
|
|
canMessageNotAllowedExCls.free(env);
|
|
|
|
|
canNotInitializedExCls.free(env);
|
|
|
|
|
uncleanStatusExCls.free(env);
|
|
|
|
|
pwmConfigDataResultCls.free(env);
|
2017-10-21 15:32:05 -07:00
|
|
|
canStatusCls.free(env);
|
2017-11-09 19:59:29 -08:00
|
|
|
matchInfoDataCls.free(env);
|
2015-11-01 09:11:52 -08:00
|
|
|
jvm = nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-06 09:27:51 -05:00
|
|
|
/*
|
|
|
|
|
* Class: edu_wpi_first_wpilibj_hal_HALUtil
|
|
|
|
|
* 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
|
|
|
|
|
Java_edu_wpi_first_wpilibj_hal_HALUtil_getFPGAVersion(JNIEnv *env, jclass) {
|
2015-08-19 11:12:54 -04:00
|
|
|
HALUTIL_LOG(logDEBUG) << "Calling HALUtil getFPGAVersion";
|
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
|
|
|
HALUTIL_LOG(logDEBUG) << "Status = " << status;
|
2015-08-19 11:12:54 -04:00
|
|
|
HALUTIL_LOG(logDEBUG) << "FPGAVersion = " << returnValue;
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Class: edu_wpi_first_wpilibj_hal_HALUtil
|
|
|
|
|
* 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
|
|
|
|
|
Java_edu_wpi_first_wpilibj_hal_HALUtil_getFPGARevision(JNIEnv *env, jclass) {
|
2015-08-19 11:12:54 -04:00
|
|
|
HALUTIL_LOG(logDEBUG) << "Calling HALUtil getFPGARevision";
|
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
|
|
|
HALUTIL_LOG(logDEBUG) << "Status = " << status;
|
2015-08-19 11:12:54 -04:00
|
|
|
HALUTIL_LOG(logDEBUG) << "FPGARevision = " << returnValue;
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Class: edu_wpi_first_wpilibj_hal_HALUtil
|
|
|
|
|
* 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
|
|
|
|
|
Java_edu_wpi_first_wpilibj_hal_HALUtil_getFPGATime(JNIEnv *env, jclass) {
|
|
|
|
|
// HALUTIL_LOG(logDEBUG) << "Calling HALUtil getFPGATime";
|
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);
|
2016-05-20 17:30:37 -07:00
|
|
|
// HALUTIL_LOG(logDEBUG) << "Status = " << status;
|
|
|
|
|
// HALUTIL_LOG(logDEBUG) << "FPGATime = " << returnValue;
|
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
|
|
|
/*
|
|
|
|
|
* Class: edu_wpi_first_wpilibj_hal_HALUtil
|
|
|
|
|
* Method: getHALRuntimeType
|
|
|
|
|
* Signature: ()I
|
|
|
|
|
*/
|
|
|
|
|
JNIEXPORT jint JNICALL
|
|
|
|
|
Java_edu_wpi_first_wpilibj_hal_HALUtil_getHALRuntimeType(JNIEnv *env, jclass) {
|
|
|
|
|
// HALUTIL_LOG(logDEBUG) << "Calling HALUtil getHALRuntimeType";
|
|
|
|
|
jint returnValue = HAL_GetRuntimeType();
|
|
|
|
|
// HALUTIL_LOG(logDEBUG) << "RuntimeType = " << returnValue;
|
|
|
|
|
return returnValue;
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-29 14:34:52 -04:00
|
|
|
/*
|
|
|
|
|
* Class: edu_wpi_first_wpilibj_hal_HALUtil
|
|
|
|
|
* Method: getFPGAButton
|
2015-11-01 09:11:52 -08:00
|
|
|
* Signature: ()I
|
2014-07-29 14:34:52 -04:00
|
|
|
*/
|
2016-05-20 17:30:37 -07:00
|
|
|
JNIEXPORT jboolean JNICALL
|
|
|
|
|
Java_edu_wpi_first_wpilibj_hal_HALUtil_getFPGAButton(JNIEnv *env, jclass) {
|
|
|
|
|
// HALUTIL_LOG(logDEBUG) << "Calling HALUtil getFPGATime";
|
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);
|
2016-05-20 17:30:37 -07:00
|
|
|
// HALUTIL_LOG(logDEBUG) << "Status = " << status;
|
|
|
|
|
// HALUTIL_LOG(logDEBUG) << "FPGATime = " << returnValue;
|
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
|
|
|
/*
|
|
|
|
|
* Class: edu_wpi_first_wpilibj_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
|
2016-11-24 21:49:34 -08:00
|
|
|
Java_edu_wpi_first_wpilibj_hal_HALUtil_getHALErrorMessage(
|
2016-05-20 17:30:37 -07:00
|
|
|
JNIEnv *paramEnv, jclass, jint paramId) {
|
2016-07-09 00:24:26 -07:00
|
|
|
const char *msg = HAL_GetErrorMessage(paramId);
|
|
|
|
|
HALUTIL_LOG(logDEBUG) << "Calling HALUtil HAL_GetErrorMessage id=" << paramId
|
2016-05-20 17:30:37 -07:00
|
|
|
<< " msg=" << msg;
|
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
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Class: edu_wpi_first_wpilibj_hal_HALUtil
|
|
|
|
|
* Method: getHALErrno
|
|
|
|
|
* Signature: ()I
|
|
|
|
|
*/
|
2016-05-20 17:30:37 -07:00
|
|
|
JNIEXPORT jint JNICALL
|
|
|
|
|
Java_edu_wpi_first_wpilibj_hal_HALUtil_getHALErrno(JNIEnv *, jclass) {
|
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;
|
|
|
|
|
*/
|
2016-05-20 17:30:37 -07:00
|
|
|
JNIEXPORT jstring JNICALL Java_edu_wpi_first_wpilibj_hal_HALUtil_getHALstrerror(
|
|
|
|
|
JNIEnv *env, jclass, jint errorCode) {
|
|
|
|
|
const char *msg = strerror(errno);
|
|
|
|
|
HALUTIL_LOG(logDEBUG) << "Calling HALUtil getHALstrerror errorCode="
|
|
|
|
|
<< errorCode << " msg=" << msg;
|
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"
|