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.
|
2019-09-26 22:53:21 -07:00
|
|
|
|
|
|
|
|
#include <jni.h>
|
|
|
|
|
|
2021-06-12 01:17:09 -07:00
|
|
|
#include "edu_wpi_first_util_WPIUtilJNI.h"
|
2019-09-26 22:53:21 -07:00
|
|
|
#include "wpi/PortForwarder.h"
|
2021-08-03 00:05:47 -07:00
|
|
|
#include "wpi/Synchronization.h"
|
2019-09-26 22:53:21 -07:00
|
|
|
#include "wpi/jni_util.h"
|
2020-08-06 21:37:38 -07:00
|
|
|
#include "wpi/timestamp.h"
|
2019-09-26 22:53:21 -07:00
|
|
|
|
|
|
|
|
using namespace wpi::java;
|
|
|
|
|
|
2021-05-31 10:35:54 -07:00
|
|
|
static bool mockTimeEnabled = false;
|
|
|
|
|
static uint64_t mockNow = 0;
|
|
|
|
|
|
2021-08-03 00:05:47 -07:00
|
|
|
static JException interruptedEx;
|
|
|
|
|
|
2019-09-26 22:53:21 -07:00
|
|
|
extern "C" {
|
|
|
|
|
|
|
|
|
|
JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void* reserved) {
|
|
|
|
|
JNIEnv* env;
|
2020-12-28 12:58:06 -08:00
|
|
|
if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK) {
|
2019-09-26 22:53:21 -07:00
|
|
|
return JNI_ERR;
|
2020-12-28 12:58:06 -08:00
|
|
|
}
|
2019-09-26 22:53:21 -07:00
|
|
|
|
2021-08-03 00:05:47 -07:00
|
|
|
interruptedEx = JException(env, "java/lang/InterruptedException");
|
|
|
|
|
if (!interruptedEx) {
|
|
|
|
|
return JNI_ERR;
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-26 22:53:21 -07:00
|
|
|
return JNI_VERSION_1_6;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-03 00:05:47 -07:00
|
|
|
JNIEXPORT void JNICALL JNI_OnUnload(JavaVM* vm, void* reserved) {
|
|
|
|
|
JNIEnv* env;
|
|
|
|
|
if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
interruptedEx.free(env);
|
|
|
|
|
}
|
2019-09-26 22:53:21 -07:00
|
|
|
|
2021-05-31 10:35:54 -07:00
|
|
|
/*
|
2021-06-12 01:17:09 -07:00
|
|
|
* Class: edu_wpi_first_util_WPIUtilJNI
|
2021-05-31 10:35:54 -07:00
|
|
|
* Method: enableMockTime
|
|
|
|
|
* Signature: ()V
|
|
|
|
|
*/
|
|
|
|
|
JNIEXPORT void JNICALL
|
2021-06-12 01:17:09 -07:00
|
|
|
Java_edu_wpi_first_util_WPIUtilJNI_enableMockTime
|
2021-05-31 10:35:54 -07:00
|
|
|
(JNIEnv*, jclass)
|
|
|
|
|
{
|
|
|
|
|
mockTimeEnabled = true;
|
|
|
|
|
wpi::SetNowImpl([] { return mockNow; });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
2021-06-12 01:17:09 -07:00
|
|
|
* Class: edu_wpi_first_util_WPIUtilJNI
|
2021-05-31 10:35:54 -07:00
|
|
|
* Method: setMockTime
|
|
|
|
|
* Signature: (J)V
|
|
|
|
|
*/
|
|
|
|
|
JNIEXPORT void JNICALL
|
2021-06-12 01:17:09 -07:00
|
|
|
Java_edu_wpi_first_util_WPIUtilJNI_setMockTime
|
2021-05-31 10:35:54 -07:00
|
|
|
(JNIEnv*, jclass, jlong time)
|
|
|
|
|
{
|
|
|
|
|
mockNow = time;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-06 21:37:38 -07:00
|
|
|
/*
|
2021-06-12 01:17:09 -07:00
|
|
|
* Class: edu_wpi_first_util_WPIUtilJNI
|
2020-08-06 21:37:38 -07:00
|
|
|
* Method: now
|
|
|
|
|
* Signature: ()J
|
|
|
|
|
*/
|
|
|
|
|
JNIEXPORT jlong JNICALL
|
2021-06-12 01:17:09 -07:00
|
|
|
Java_edu_wpi_first_util_WPIUtilJNI_now
|
2020-08-06 21:37:38 -07:00
|
|
|
(JNIEnv*, jclass)
|
|
|
|
|
{
|
2021-05-31 10:35:54 -07:00
|
|
|
if (mockTimeEnabled) {
|
|
|
|
|
return mockNow;
|
|
|
|
|
} else {
|
|
|
|
|
return wpi::Now();
|
|
|
|
|
}
|
2020-08-06 21:37:38 -07:00
|
|
|
}
|
|
|
|
|
|
2019-09-26 22:53:21 -07:00
|
|
|
/*
|
2021-06-12 01:17:09 -07:00
|
|
|
* Class: edu_wpi_first_util_WPIUtilJNI
|
2019-09-26 22:53:21 -07:00
|
|
|
* Method: addPortForwarder
|
|
|
|
|
* Signature: (ILjava/lang/String;I)V
|
|
|
|
|
*/
|
|
|
|
|
JNIEXPORT void JNICALL
|
2021-06-12 01:17:09 -07:00
|
|
|
Java_edu_wpi_first_util_WPIUtilJNI_addPortForwarder
|
2019-09-26 22:53:21 -07:00
|
|
|
(JNIEnv* env, jclass, jint port, jstring remoteHost, jint remotePort)
|
|
|
|
|
{
|
|
|
|
|
wpi::PortForwarder::GetInstance().Add(static_cast<unsigned int>(port),
|
|
|
|
|
JStringRef{env, remoteHost}.str(),
|
|
|
|
|
static_cast<unsigned int>(remotePort));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
2021-06-12 01:17:09 -07:00
|
|
|
* Class: edu_wpi_first_util_WPIUtilJNI
|
2019-09-26 22:53:21 -07:00
|
|
|
* Method: removePortForwarder
|
|
|
|
|
* Signature: (I)V
|
|
|
|
|
*/
|
|
|
|
|
JNIEXPORT void JNICALL
|
2021-06-12 01:17:09 -07:00
|
|
|
Java_edu_wpi_first_util_WPIUtilJNI_removePortForwarder
|
2019-09-26 22:53:21 -07:00
|
|
|
(JNIEnv* env, jclass, jint port)
|
|
|
|
|
{
|
|
|
|
|
wpi::PortForwarder::GetInstance().Remove(port);
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-03 00:05:47 -07:00
|
|
|
/*
|
|
|
|
|
* Class: edu_wpi_first_util_WPIUtilJNI
|
|
|
|
|
* Method: createEvent
|
|
|
|
|
* Signature: (ZZ)I
|
|
|
|
|
*/
|
|
|
|
|
JNIEXPORT jint JNICALL
|
|
|
|
|
Java_edu_wpi_first_util_WPIUtilJNI_createEvent
|
|
|
|
|
(JNIEnv*, jclass, jboolean manualReset, jboolean initialState)
|
|
|
|
|
{
|
|
|
|
|
return wpi::CreateEvent(manualReset, initialState);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Class: edu_wpi_first_util_WPIUtilJNI
|
|
|
|
|
* Method: destroyEvent
|
|
|
|
|
* Signature: (I)V
|
|
|
|
|
*/
|
|
|
|
|
JNIEXPORT void JNICALL
|
|
|
|
|
Java_edu_wpi_first_util_WPIUtilJNI_destroyEvent
|
|
|
|
|
(JNIEnv*, jclass, jint eventHandle)
|
|
|
|
|
{
|
|
|
|
|
wpi::DestroyEvent(eventHandle);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Class: edu_wpi_first_util_WPIUtilJNI
|
|
|
|
|
* Method: setEvent
|
|
|
|
|
* Signature: (I)V
|
|
|
|
|
*/
|
|
|
|
|
JNIEXPORT void JNICALL
|
|
|
|
|
Java_edu_wpi_first_util_WPIUtilJNI_setEvent
|
|
|
|
|
(JNIEnv*, jclass, jint eventHandle)
|
|
|
|
|
{
|
|
|
|
|
wpi::SetEvent(eventHandle);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Class: edu_wpi_first_util_WPIUtilJNI
|
|
|
|
|
* Method: resetEvent
|
|
|
|
|
* Signature: (I)V
|
|
|
|
|
*/
|
|
|
|
|
JNIEXPORT void JNICALL
|
|
|
|
|
Java_edu_wpi_first_util_WPIUtilJNI_resetEvent
|
|
|
|
|
(JNIEnv*, jclass, jint eventHandle)
|
|
|
|
|
{
|
|
|
|
|
wpi::ResetEvent(eventHandle);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Class: edu_wpi_first_util_WPIUtilJNI
|
|
|
|
|
* Method: createSemaphore
|
|
|
|
|
* Signature: (II)I
|
|
|
|
|
*/
|
|
|
|
|
JNIEXPORT jint JNICALL
|
|
|
|
|
Java_edu_wpi_first_util_WPIUtilJNI_createSemaphore
|
|
|
|
|
(JNIEnv*, jclass, jint initialCount, jint maximumCount)
|
|
|
|
|
{
|
|
|
|
|
return wpi::CreateSemaphore(initialCount, maximumCount);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Class: edu_wpi_first_util_WPIUtilJNI
|
|
|
|
|
* Method: destroySemaphore
|
|
|
|
|
* Signature: (I)V
|
|
|
|
|
*/
|
|
|
|
|
JNIEXPORT void JNICALL
|
|
|
|
|
Java_edu_wpi_first_util_WPIUtilJNI_destroySemaphore
|
|
|
|
|
(JNIEnv*, jclass, jint semHandle)
|
|
|
|
|
{
|
|
|
|
|
wpi::DestroySemaphore(semHandle);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Class: edu_wpi_first_util_WPIUtilJNI
|
|
|
|
|
* Method: releaseSemaphore
|
|
|
|
|
* Signature: (II)Z
|
|
|
|
|
*/
|
|
|
|
|
JNIEXPORT jboolean JNICALL
|
|
|
|
|
Java_edu_wpi_first_util_WPIUtilJNI_releaseSemaphore
|
|
|
|
|
(JNIEnv*, jclass, jint semHandle, jint releaseCount)
|
|
|
|
|
{
|
|
|
|
|
return wpi::ReleaseSemaphore(semHandle, releaseCount);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Class: edu_wpi_first_util_WPIUtilJNI
|
|
|
|
|
* Method: waitForObject
|
|
|
|
|
* Signature: (I)V
|
|
|
|
|
*/
|
|
|
|
|
JNIEXPORT void JNICALL
|
|
|
|
|
Java_edu_wpi_first_util_WPIUtilJNI_waitForObject
|
|
|
|
|
(JNIEnv* env, jclass, jint handle)
|
|
|
|
|
{
|
|
|
|
|
if (!wpi::WaitForObject(handle)) {
|
|
|
|
|
interruptedEx.Throw(env, "WaitForObject interrupted");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Class: edu_wpi_first_util_WPIUtilJNI
|
|
|
|
|
* Method: waitForObjectTimeout
|
|
|
|
|
* Signature: (ID)Z
|
|
|
|
|
*/
|
|
|
|
|
JNIEXPORT jboolean JNICALL
|
|
|
|
|
Java_edu_wpi_first_util_WPIUtilJNI_waitForObjectTimeout
|
|
|
|
|
(JNIEnv* env, jclass, jint handle, jdouble timeout)
|
|
|
|
|
{
|
|
|
|
|
bool timedOut;
|
|
|
|
|
if (!wpi::WaitForObject(handle, timeout, &timedOut) && !timedOut) {
|
|
|
|
|
interruptedEx.Throw(env, "WaitForObject interrupted");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return timedOut;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Class: edu_wpi_first_util_WPIUtilJNI
|
|
|
|
|
* Method: waitForObjects
|
|
|
|
|
* Signature: ([I)[I
|
|
|
|
|
*/
|
|
|
|
|
JNIEXPORT jintArray JNICALL
|
|
|
|
|
Java_edu_wpi_first_util_WPIUtilJNI_waitForObjects
|
|
|
|
|
(JNIEnv* env, jclass, jintArray handles)
|
|
|
|
|
{
|
|
|
|
|
JIntArrayRef handlesArr{env, handles};
|
|
|
|
|
wpi::SmallVector<WPI_Handle, 8> signaledBuf;
|
|
|
|
|
signaledBuf.resize(handlesArr.size());
|
|
|
|
|
wpi::span<const WPI_Handle> handlesArr2{
|
|
|
|
|
reinterpret_cast<const WPI_Handle*>(handlesArr.array().data()),
|
|
|
|
|
handlesArr.size()};
|
|
|
|
|
|
|
|
|
|
auto signaled = wpi::WaitForObjects(handlesArr2, signaledBuf);
|
|
|
|
|
if (signaled.empty()) {
|
|
|
|
|
interruptedEx.Throw(env, "WaitForObjects interrupted");
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
return MakeJIntArray(env, signaled);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Class: edu_wpi_first_util_WPIUtilJNI
|
|
|
|
|
* Method: waitForObjectsTimeout
|
|
|
|
|
* Signature: ([ID)[I
|
|
|
|
|
*/
|
|
|
|
|
JNIEXPORT jintArray JNICALL
|
|
|
|
|
Java_edu_wpi_first_util_WPIUtilJNI_waitForObjectsTimeout
|
|
|
|
|
(JNIEnv* env, jclass, jintArray handles, jdouble timeout)
|
|
|
|
|
{
|
|
|
|
|
JIntArrayRef handlesArr{env, handles};
|
|
|
|
|
wpi::SmallVector<WPI_Handle, 8> signaledBuf;
|
|
|
|
|
signaledBuf.resize(handlesArr.size());
|
|
|
|
|
wpi::span<const WPI_Handle> handlesArr2{
|
|
|
|
|
reinterpret_cast<const WPI_Handle*>(handlesArr.array().data()),
|
|
|
|
|
handlesArr.size()};
|
|
|
|
|
|
|
|
|
|
bool timedOut;
|
|
|
|
|
auto signaled =
|
|
|
|
|
wpi::WaitForObjects(handlesArr2, signaledBuf, timeout, &timedOut);
|
|
|
|
|
if (signaled.empty() && !timedOut) {
|
|
|
|
|
interruptedEx.Throw(env, "WaitForObjects interrupted");
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
return MakeJIntArray(env, signaled);
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-26 22:53:21 -07:00
|
|
|
} // extern "C"
|