2018-05-11 12:38:23 -07:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2020-06-26 17:12:55 -07:00
|
|
|
/* Copyright (c) 2018-2020 FIRST. All Rights Reserved. */
|
2018-05-11 12:38:23 -07:00
|
|
|
/* 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. */
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
2018-05-13 17:09:56 -07:00
|
|
|
#include "SimulatorJNI.h"
|
|
|
|
|
|
2019-09-28 11:34:46 -07:00
|
|
|
#include <wpi/jni_util.h>
|
|
|
|
|
|
2018-05-11 12:38:23 -07:00
|
|
|
#include "BufferCallbackStore.h"
|
|
|
|
|
#include "CallbackStore.h"
|
|
|
|
|
#include "ConstBufferCallbackStore.h"
|
2019-09-28 11:34:46 -07:00
|
|
|
#include "SimDeviceDataJNI.h"
|
2018-05-11 12:38:23 -07:00
|
|
|
#include "SpiReadAutoReceiveBufferCallbackStore.h"
|
2020-06-27 22:11:24 -07:00
|
|
|
#include "edu_wpi_first_hal_simulation_SimulatorJNI.h"
|
2018-07-20 00:03:45 -07:00
|
|
|
#include "hal/HAL.h"
|
|
|
|
|
#include "hal/handles/HandlesInternal.h"
|
2020-06-27 22:11:24 -07:00
|
|
|
#include "hal/simulation/MockHooks.h"
|
2018-05-11 12:38:23 -07:00
|
|
|
|
|
|
|
|
using namespace wpi::java;
|
|
|
|
|
|
|
|
|
|
static JavaVM* jvm = nullptr;
|
|
|
|
|
static JClass notifyCallbackCls;
|
|
|
|
|
static JClass bufferCallbackCls;
|
|
|
|
|
static JClass constBufferCallbackCls;
|
|
|
|
|
static JClass spiReadAutoReceiveBufferCallbackCls;
|
|
|
|
|
static jmethodID notifyCallbackCallback;
|
|
|
|
|
static jmethodID bufferCallbackCallback;
|
|
|
|
|
static jmethodID constBufferCallbackCallback;
|
|
|
|
|
static jmethodID spiReadAutoReceiveBufferCallbackCallback;
|
|
|
|
|
|
2020-06-26 17:12:55 -07:00
|
|
|
namespace hal {
|
2018-05-11 12:38:23 -07:00
|
|
|
namespace sim {
|
|
|
|
|
jint SimOnLoad(JavaVM* vm, void* reserved) {
|
|
|
|
|
jvm = vm;
|
|
|
|
|
|
|
|
|
|
JNIEnv* env;
|
|
|
|
|
if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK)
|
|
|
|
|
return JNI_ERR;
|
|
|
|
|
|
2020-06-27 22:11:24 -07:00
|
|
|
notifyCallbackCls =
|
|
|
|
|
JClass(env, "edu/wpi/first/hal/simulation/NotifyCallback");
|
2018-05-11 12:38:23 -07:00
|
|
|
if (!notifyCallbackCls) return JNI_ERR;
|
|
|
|
|
|
|
|
|
|
notifyCallbackCallback = env->GetMethodID(notifyCallbackCls, "callbackNative",
|
|
|
|
|
"(Ljava/lang/String;IJD)V");
|
|
|
|
|
if (!notifyCallbackCallback) return JNI_ERR;
|
|
|
|
|
|
2020-06-27 22:11:24 -07:00
|
|
|
bufferCallbackCls =
|
|
|
|
|
JClass(env, "edu/wpi/first/hal/simulation/BufferCallback");
|
2018-05-11 12:38:23 -07:00
|
|
|
if (!bufferCallbackCls) return JNI_ERR;
|
|
|
|
|
|
|
|
|
|
bufferCallbackCallback = env->GetMethodID(bufferCallbackCls, "callback",
|
|
|
|
|
"(Ljava/lang/String;[BI)V");
|
|
|
|
|
if (!bufferCallbackCallback) return JNI_ERR;
|
|
|
|
|
|
|
|
|
|
constBufferCallbackCls =
|
2020-06-27 22:11:24 -07:00
|
|
|
JClass(env, "edu/wpi/first/hal/simulation/ConstBufferCallback");
|
2018-05-11 12:38:23 -07:00
|
|
|
if (!constBufferCallbackCls) return JNI_ERR;
|
|
|
|
|
|
|
|
|
|
constBufferCallbackCallback = env->GetMethodID(
|
|
|
|
|
constBufferCallbackCls, "callback", "(Ljava/lang/String;[BI)V");
|
|
|
|
|
if (!constBufferCallbackCallback) return JNI_ERR;
|
|
|
|
|
|
2020-06-27 22:11:24 -07:00
|
|
|
spiReadAutoReceiveBufferCallbackCls = JClass(
|
|
|
|
|
env, "edu/wpi/first/hal/simulation/SpiReadAutoReceiveBufferCallback");
|
2018-05-11 12:38:23 -07:00
|
|
|
if (!spiReadAutoReceiveBufferCallbackCls) return JNI_ERR;
|
|
|
|
|
|
|
|
|
|
spiReadAutoReceiveBufferCallbackCallback =
|
|
|
|
|
env->GetMethodID(spiReadAutoReceiveBufferCallbackCls, "callback",
|
2018-12-06 22:29:20 -08:00
|
|
|
"(Ljava/lang/String;[II)I");
|
2018-05-11 12:38:23 -07:00
|
|
|
if (!spiReadAutoReceiveBufferCallbackCallback) return JNI_ERR;
|
|
|
|
|
|
|
|
|
|
InitializeStore();
|
|
|
|
|
InitializeBufferStore();
|
|
|
|
|
InitializeConstBufferStore();
|
|
|
|
|
InitializeSpiBufferStore();
|
2019-09-28 11:34:46 -07:00
|
|
|
if (!InitializeSimDeviceDataJNI(env)) return JNI_ERR;
|
2018-05-11 12:38:23 -07:00
|
|
|
|
|
|
|
|
return JNI_VERSION_1_6;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SimOnUnload(JavaVM* vm, void* reserved) {
|
|
|
|
|
JNIEnv* env;
|
|
|
|
|
if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
notifyCallbackCls.free(env);
|
|
|
|
|
bufferCallbackCls.free(env);
|
|
|
|
|
constBufferCallbackCls.free(env);
|
|
|
|
|
spiReadAutoReceiveBufferCallbackCls.free(env);
|
2019-09-28 11:34:46 -07:00
|
|
|
FreeSimDeviceDataJNI(env);
|
2018-05-11 12:38:23 -07:00
|
|
|
jvm = nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
JavaVM* GetJVM() { return jvm; }
|
|
|
|
|
|
|
|
|
|
jmethodID GetNotifyCallback() { return notifyCallbackCallback; }
|
|
|
|
|
|
|
|
|
|
jmethodID GetBufferCallback() { return bufferCallbackCallback; }
|
|
|
|
|
|
|
|
|
|
jmethodID GetConstBufferCallback() { return constBufferCallbackCallback; }
|
|
|
|
|
|
|
|
|
|
jmethodID GetSpiReadAutoReceiveBufferCallback() {
|
|
|
|
|
return spiReadAutoReceiveBufferCallbackCallback;
|
|
|
|
|
}
|
|
|
|
|
} // namespace sim
|
2020-06-26 17:12:55 -07:00
|
|
|
} // namespace hal
|
2018-05-11 12:38:23 -07:00
|
|
|
|
|
|
|
|
extern "C" {
|
2020-06-26 20:46:13 -07:00
|
|
|
/*
|
2020-06-27 22:11:24 -07:00
|
|
|
* Class: edu_wpi_first_hal_simulation_SimulatorJNI
|
2020-06-26 20:46:13 -07:00
|
|
|
* Method: setRuntimeType
|
|
|
|
|
* Signature: (I)V
|
|
|
|
|
*/
|
|
|
|
|
JNIEXPORT void JNICALL
|
2020-06-27 22:11:24 -07:00
|
|
|
Java_edu_wpi_first_hal_simulation_SimulatorJNI_setRuntimeType
|
2020-06-26 20:46:13 -07:00
|
|
|
(JNIEnv*, jclass, jint type)
|
|
|
|
|
{
|
|
|
|
|
HALSIM_SetRuntimeType(static_cast<HAL_RuntimeType>(type));
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-11 12:38:23 -07:00
|
|
|
/*
|
2020-06-27 22:11:24 -07:00
|
|
|
* Class: edu_wpi_first_hal_simulation_SimulatorJNI
|
2018-05-11 12:38:23 -07:00
|
|
|
* Method: waitForProgramStart
|
|
|
|
|
* Signature: ()V
|
|
|
|
|
*/
|
|
|
|
|
JNIEXPORT void JNICALL
|
2020-06-27 22:11:24 -07:00
|
|
|
Java_edu_wpi_first_hal_simulation_SimulatorJNI_waitForProgramStart
|
2018-05-13 17:09:56 -07:00
|
|
|
(JNIEnv*, jclass)
|
|
|
|
|
{
|
2018-05-11 12:38:23 -07:00
|
|
|
HALSIM_WaitForProgramStart();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
2020-06-27 22:11:24 -07:00
|
|
|
* Class: edu_wpi_first_hal_simulation_SimulatorJNI
|
2018-05-11 12:38:23 -07:00
|
|
|
* Method: setProgramStarted
|
|
|
|
|
* Signature: ()V
|
|
|
|
|
*/
|
|
|
|
|
JNIEXPORT void JNICALL
|
2020-06-27 22:11:24 -07:00
|
|
|
Java_edu_wpi_first_hal_simulation_SimulatorJNI_setProgramStarted
|
2018-05-13 17:09:56 -07:00
|
|
|
(JNIEnv*, jclass)
|
|
|
|
|
{
|
2018-05-11 12:38:23 -07:00
|
|
|
HALSIM_SetProgramStarted();
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-04 00:44:37 -07:00
|
|
|
/*
|
|
|
|
|
* Class: edu_wpi_first_hal_simulation_SimulatorJNI
|
|
|
|
|
* Method: getProgramStarted
|
|
|
|
|
* Signature: ()Z
|
|
|
|
|
*/
|
|
|
|
|
JNIEXPORT jboolean JNICALL
|
|
|
|
|
Java_edu_wpi_first_hal_simulation_SimulatorJNI_getProgramStarted
|
|
|
|
|
(JNIEnv*, jclass)
|
|
|
|
|
{
|
|
|
|
|
return HALSIM_GetProgramStarted();
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-11 12:38:23 -07:00
|
|
|
/*
|
2020-06-27 22:11:24 -07:00
|
|
|
* Class: edu_wpi_first_hal_simulation_SimulatorJNI
|
2018-05-11 12:38:23 -07:00
|
|
|
* Method: restartTiming
|
|
|
|
|
* Signature: ()V
|
|
|
|
|
*/
|
|
|
|
|
JNIEXPORT void JNICALL
|
2020-06-27 22:11:24 -07:00
|
|
|
Java_edu_wpi_first_hal_simulation_SimulatorJNI_restartTiming
|
2018-05-13 17:09:56 -07:00
|
|
|
(JNIEnv*, jclass)
|
|
|
|
|
{
|
2018-05-11 12:38:23 -07:00
|
|
|
HALSIM_RestartTiming();
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-10 20:54:25 -08:00
|
|
|
/*
|
2020-06-27 22:11:24 -07:00
|
|
|
* Class: edu_wpi_first_hal_simulation_SimulatorJNI
|
2019-11-10 20:54:25 -08:00
|
|
|
* Method: pauseTiming
|
|
|
|
|
* Signature: ()V
|
|
|
|
|
*/
|
|
|
|
|
JNIEXPORT void JNICALL
|
2020-06-27 22:11:24 -07:00
|
|
|
Java_edu_wpi_first_hal_simulation_SimulatorJNI_pauseTiming
|
2019-11-10 20:54:25 -08:00
|
|
|
(JNIEnv*, jclass)
|
|
|
|
|
{
|
|
|
|
|
HALSIM_PauseTiming();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
2020-06-27 22:11:24 -07:00
|
|
|
* Class: edu_wpi_first_hal_simulation_SimulatorJNI
|
2019-11-10 20:54:25 -08:00
|
|
|
* Method: resumeTiming
|
|
|
|
|
* Signature: ()V
|
|
|
|
|
*/
|
|
|
|
|
JNIEXPORT void JNICALL
|
2020-06-27 22:11:24 -07:00
|
|
|
Java_edu_wpi_first_hal_simulation_SimulatorJNI_resumeTiming
|
2019-11-10 20:54:25 -08:00
|
|
|
(JNIEnv*, jclass)
|
|
|
|
|
{
|
|
|
|
|
HALSIM_ResumeTiming();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
2020-06-27 22:11:24 -07:00
|
|
|
* Class: edu_wpi_first_hal_simulation_SimulatorJNI
|
2019-11-10 20:54:25 -08:00
|
|
|
* Method: isTimingPaused
|
|
|
|
|
* Signature: ()Z
|
|
|
|
|
*/
|
|
|
|
|
JNIEXPORT jboolean JNICALL
|
2020-06-27 22:11:24 -07:00
|
|
|
Java_edu_wpi_first_hal_simulation_SimulatorJNI_isTimingPaused
|
2019-11-10 20:54:25 -08:00
|
|
|
(JNIEnv*, jclass)
|
|
|
|
|
{
|
|
|
|
|
return HALSIM_IsTimingPaused();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
2020-06-27 22:11:24 -07:00
|
|
|
* Class: edu_wpi_first_hal_simulation_SimulatorJNI
|
2019-11-10 20:54:25 -08:00
|
|
|
* Method: stepTiming
|
|
|
|
|
* Signature: (J)V
|
|
|
|
|
*/
|
|
|
|
|
JNIEXPORT void JNICALL
|
2020-06-27 22:11:24 -07:00
|
|
|
Java_edu_wpi_first_hal_simulation_SimulatorJNI_stepTiming
|
2019-11-10 20:54:25 -08:00
|
|
|
(JNIEnv*, jclass, jlong delta)
|
|
|
|
|
{
|
|
|
|
|
HALSIM_StepTiming(delta);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-27 13:27:53 -07:00
|
|
|
/*
|
|
|
|
|
* Class: edu_wpi_first_hal_simulation_SimulatorJNI
|
|
|
|
|
* Method: stepTimingAsync
|
|
|
|
|
* Signature: (J)V
|
|
|
|
|
*/
|
|
|
|
|
JNIEXPORT void JNICALL
|
|
|
|
|
Java_edu_wpi_first_hal_simulation_SimulatorJNI_stepTimingAsync
|
|
|
|
|
(JNIEnv*, jclass, jlong delta)
|
|
|
|
|
{
|
|
|
|
|
HALSIM_StepTimingAsync(delta);
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-11 12:38:23 -07:00
|
|
|
/*
|
2020-06-27 22:11:24 -07:00
|
|
|
* Class: edu_wpi_first_hal_simulation_SimulatorJNI
|
2018-05-11 12:38:23 -07:00
|
|
|
* Method: resetHandles
|
|
|
|
|
* Signature: ()V
|
|
|
|
|
*/
|
|
|
|
|
JNIEXPORT void JNICALL
|
2020-06-27 22:11:24 -07:00
|
|
|
Java_edu_wpi_first_hal_simulation_SimulatorJNI_resetHandles
|
2018-05-13 17:09:56 -07:00
|
|
|
(JNIEnv*, jclass)
|
|
|
|
|
{
|
2018-05-11 12:38:23 -07:00
|
|
|
hal::HandleBase::ResetGlobalHandles();
|
|
|
|
|
}
|
|
|
|
|
} // extern "C"
|