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 "BufferCallbackStore.h"
|
|
|
|
|
|
|
|
|
|
#include <jni.h>
|
2018-05-13 17:09:56 -07:00
|
|
|
|
2018-05-11 12:38:23 -07:00
|
|
|
#include <wpi/jni_util.h>
|
|
|
|
|
|
|
|
|
|
#include "SimulatorJNI.h"
|
2018-07-20 00:03:45 -07:00
|
|
|
#include "hal/Types.h"
|
2019-09-23 23:28:49 -07:00
|
|
|
#include "hal/Value.h"
|
2018-07-20 00:03:45 -07:00
|
|
|
#include "hal/handles/UnlimitedHandleResource.h"
|
2020-06-27 22:11:24 -07:00
|
|
|
#include "hal/simulation/NotifyListener.h"
|
2018-05-11 12:38:23 -07:00
|
|
|
|
2020-06-26 17:12:55 -07:00
|
|
|
using namespace hal;
|
|
|
|
|
using namespace hal::sim;
|
2018-05-11 12:38:23 -07:00
|
|
|
using namespace wpi::java;
|
|
|
|
|
|
|
|
|
|
static hal::UnlimitedHandleResource<SIM_JniHandle, BufferCallbackStore,
|
|
|
|
|
hal::HAL_HandleEnum::SimulationJni>*
|
|
|
|
|
callbackHandles;
|
|
|
|
|
|
2020-12-28 01:19:59 -08:00
|
|
|
namespace hal::sim {
|
2018-05-11 12:38:23 -07:00
|
|
|
void InitializeBufferStore() {
|
|
|
|
|
static hal::UnlimitedHandleResource<SIM_JniHandle, BufferCallbackStore,
|
|
|
|
|
hal::HAL_HandleEnum::SimulationJni>
|
|
|
|
|
cb;
|
|
|
|
|
callbackHandles = &cb;
|
|
|
|
|
}
|
2020-12-28 01:19:59 -08:00
|
|
|
} // namespace hal::sim
|
2018-05-11 12:38:23 -07:00
|
|
|
|
|
|
|
|
void BufferCallbackStore::create(JNIEnv* env, jobject obj) {
|
|
|
|
|
m_call = JGlobal<jobject>(env, obj);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BufferCallbackStore::performCallback(const char* name, 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
|
|
|
|
|
wpi::outs() << "Failed to attach\n";
|
|
|
|
|
wpi::outs().flush();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
} else if (tryGetEnv == JNI_EVERSION) {
|
|
|
|
|
wpi::outs() << "Invalid JVM Version requested\n";
|
|
|
|
|
wpi::outs().flush();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto toCallbackArr =
|
|
|
|
|
MakeJByteArray(env, wpi::StringRef{reinterpret_cast<const char*>(buffer),
|
|
|
|
|
static_cast<size_t>(length)});
|
|
|
|
|
|
|
|
|
|
env->CallVoidMethod(m_call, sim::GetBufferCallback(), MakeJString(env, name),
|
2020-12-28 11:04:20 -08:00
|
|
|
toCallbackArr, static_cast<jint>(length));
|
2018-05-11 12:38:23 -07:00
|
|
|
|
|
|
|
|
jbyte* fromCallbackArr = reinterpret_cast<jbyte*>(
|
|
|
|
|
env->GetPrimitiveArrayCritical(toCallbackArr, nullptr));
|
|
|
|
|
|
|
|
|
|
for (size_t i = 0; i < length; i++) {
|
|
|
|
|
buffer[i] = fromCallbackArr[i];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
env->ReleasePrimitiveArrayCritical(toCallbackArr, fromCallbackArr, JNI_ABORT);
|
|
|
|
|
|
|
|
|
|
if (env->ExceptionCheck()) {
|
|
|
|
|
env->ExceptionDescribe();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (didAttachThread) {
|
|
|
|
|
vm->DetachCurrentThread();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
void BufferCallbackStore::free(JNIEnv* env) {
|
|
|
|
|
m_call.free(env);
|
|
|
|
|
}
|
2018-05-11 12:38:23 -07:00
|
|
|
|
|
|
|
|
SIM_JniHandle sim::AllocateBufferCallback(
|
|
|
|
|
JNIEnv* env, jint index, jobject callback,
|
|
|
|
|
RegisterBufferCallbackFunc createCallback) {
|
|
|
|
|
auto callbackStore = std::make_shared<BufferCallbackStore>();
|
|
|
|
|
|
|
|
|
|
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, 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::FreeBufferCallback(JNIEnv* env, SIM_JniHandle handle, jint index,
|
|
|
|
|
FreeBufferCallbackFunc freeCallback) {
|
|
|
|
|
auto callback = callbackHandles->Free(handle);
|
|
|
|
|
freeCallback(index, callback->getCallbackId());
|
|
|
|
|
callback->free(env);
|
|
|
|
|
}
|