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.
|
2018-05-11 12:38:23 -07:00
|
|
|
|
|
|
|
|
#include "ConstBufferCallbackStore.h"
|
|
|
|
|
|
|
|
|
|
#include <jni.h>
|
2018-05-13 17:09:56 -07:00
|
|
|
|
2021-06-06 16:13:58 -07:00
|
|
|
#include <cstdio>
|
2024-09-20 17:43:39 -07:00
|
|
|
#include <memory>
|
2021-06-06 16:13:58 -07:00
|
|
|
|
2018-05-11 12:38:23 -07:00
|
|
|
#include "SimulatorJNI.h"
|
2025-11-07 19:56:21 -05:00
|
|
|
#include "wpi/hal/Types.h"
|
|
|
|
|
#include "wpi/hal/handles/UnlimitedHandleResource.h"
|
2025-11-07 19:57:55 -05:00
|
|
|
#include "wpi/util/jni_util.hpp"
|
2018-05-11 12:38:23 -07:00
|
|
|
|
2025-11-07 20:00:05 -05:00
|
|
|
using namespace wpi::hal;
|
|
|
|
|
using namespace wpi::hal::sim;
|
|
|
|
|
using namespace wpi::util::java;
|
2018-05-11 12:38:23 -07:00
|
|
|
|
2025-11-07 20:01:58 -05:00
|
|
|
static wpi::hal::UnlimitedHandleResource<
|
|
|
|
|
SIM_JniHandle, ConstBufferCallbackStore,
|
|
|
|
|
wpi::hal::HAL_HandleEnum::SimulationJni>* callbackHandles;
|
2018-05-11 12:38:23 -07:00
|
|
|
|
2025-11-07 20:00:05 -05:00
|
|
|
namespace wpi::hal::sim {
|
2018-05-11 12:38:23 -07:00
|
|
|
void InitializeConstBufferStore() {
|
2025-11-07 20:01:58 -05:00
|
|
|
static wpi::hal::UnlimitedHandleResource<
|
|
|
|
|
SIM_JniHandle, ConstBufferCallbackStore,
|
|
|
|
|
wpi::hal::HAL_HandleEnum::SimulationJni>
|
2018-05-11 12:38:23 -07:00
|
|
|
cb;
|
|
|
|
|
callbackHandles = &cb;
|
|
|
|
|
}
|
2025-11-07 20:00:05 -05:00
|
|
|
} // namespace wpi::hal::sim
|
2018-05-11 12:38:23 -07:00
|
|
|
|
|
|
|
|
void ConstBufferCallbackStore::create(JNIEnv* env, jobject obj) {
|
|
|
|
|
m_call = JGlobal<jobject>(env, obj);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ConstBufferCallbackStore::performCallback(const char* name,
|
|
|
|
|
const uint8_t* buffer,
|
|
|
|
|
uint32_t length) {
|
|
|
|
|
JNIEnv* env;
|
|
|
|
|
JavaVM* vm = sim::GetJVM();
|
|
|
|
|
bool didAttachThread = false;
|
|
|
|
|
int tryGetEnv = vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6);
|
|
|
|
|
if (tryGetEnv == JNI_EDETACHED) {
|
|
|
|
|
// Thread not attached
|
|
|
|
|
didAttachThread = true;
|
|
|
|
|
if (vm->AttachCurrentThread(reinterpret_cast<void**>(&env), nullptr) != 0) {
|
|
|
|
|
// Failed to attach, log and return
|
2021-06-06 16:13:58 -07:00
|
|
|
std::puts("Failed to attach");
|
|
|
|
|
std::fflush(stdout);
|
2018-05-11 12:38:23 -07:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
} else if (tryGetEnv == JNI_EVERSION) {
|
2021-06-06 16:13:58 -07:00
|
|
|
std::puts("Invalid JVM Version requested");
|
|
|
|
|
std::fflush(stdout);
|
2018-05-11 12:38:23 -07:00
|
|
|
}
|
|
|
|
|
|
2022-10-02 06:28:50 -07:00
|
|
|
auto toCallbackArr =
|
|
|
|
|
MakeJByteArray(env, {buffer, static_cast<size_t>(length)});
|
2018-05-11 12:38:23 -07:00
|
|
|
|
|
|
|
|
env->CallVoidMethod(m_call, sim::GetConstBufferCallback(),
|
2020-12-28 11:04:20 -08:00
|
|
|
MakeJString(env, name), toCallbackArr,
|
|
|
|
|
static_cast<jint>(length));
|
2018-05-11 12:38:23 -07:00
|
|
|
|
|
|
|
|
if (env->ExceptionCheck()) {
|
|
|
|
|
env->ExceptionDescribe();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (didAttachThread) {
|
|
|
|
|
vm->DetachCurrentThread();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
void ConstBufferCallbackStore::free(JNIEnv* env) {
|
|
|
|
|
m_call.free(env);
|
|
|
|
|
}
|
2018-05-11 12:38:23 -07:00
|
|
|
|
|
|
|
|
SIM_JniHandle sim::AllocateConstBufferCallback(
|
|
|
|
|
JNIEnv* env, jint index, jobject callback,
|
|
|
|
|
RegisterConstBufferCallbackFunc createCallback) {
|
|
|
|
|
auto callbackStore = std::make_shared<ConstBufferCallbackStore>();
|
|
|
|
|
|
|
|
|
|
auto handle = callbackHandles->Allocate(callbackStore);
|
|
|
|
|
|
|
|
|
|
if (handle == HAL_kInvalidHandle) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uintptr_t handleAsPtr = static_cast<uintptr_t>(handle);
|
|
|
|
|
void* handleAsVoidPtr = reinterpret_cast<void*>(handleAsPtr);
|
|
|
|
|
|
|
|
|
|
callbackStore->create(env, callback);
|
|
|
|
|
|
|
|
|
|
auto callbackFunc = [](const char* name, void* param, const uint8_t* buffer,
|
|
|
|
|
uint32_t length) {
|
|
|
|
|
uintptr_t handleTmp = reinterpret_cast<uintptr_t>(param);
|
|
|
|
|
SIM_JniHandle handle = static_cast<SIM_JniHandle>(handleTmp);
|
|
|
|
|
auto data = callbackHandles->Get(handle);
|
2020-12-28 12:58:06 -08:00
|
|
|
if (!data) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2018-05-11 12:38:23 -07:00
|
|
|
|
|
|
|
|
data->performCallback(name, buffer, length);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
auto id = createCallback(index, callbackFunc, handleAsVoidPtr);
|
|
|
|
|
|
|
|
|
|
callbackStore->setCallbackId(id);
|
|
|
|
|
|
|
|
|
|
return handle;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void sim::FreeConstBufferCallback(JNIEnv* env, SIM_JniHandle handle, jint index,
|
|
|
|
|
FreeConstBufferCallbackFunc freeCallback) {
|
|
|
|
|
auto callback = callbackHandles->Free(handle);
|
2023-01-02 02:19:04 -05:00
|
|
|
if (callback == nullptr) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2018-05-11 12:38:23 -07:00
|
|
|
freeCallback(index, callback->getCallbackId());
|
|
|
|
|
callback->free(env);
|
|
|
|
|
}
|