Files
lockstep/app/src/main/cpp/jni_stepdetector.cpp

52 lines
1.6 KiB
C++
Raw Normal View History

2026-03-04 01:31:35 +01:00
//
// Created by david on 03.03.2026.
//
#include <jni.h>
#include "StepDetector.h"
#include <new>
#include <vector>
extern "C" {
jint throwIllegalArgumentException(JNIEnv *env, const char *message)
{
jclass exClass;
const char *className = "java/lang/IllegalArgumentException";
exClass = env->FindClass(className);
if (exClass == nullptr) {
return -1;
}
return env->ThrowNew(exClass, message);
}
JNIEXPORT jlong JNICALL
Java_at_lockstep_filter_StepDetector_native_1create(
JNIEnv *env,
jclass /*unused*/, jlong engineHandle) {
auto *listener = reinterpret_cast<StepListener *>(engineHandle);
// We use std::nothrow so `new` returns a nullptr if the engine creation fails
auto *detector = new(std::nothrow) StepDetector(listener);
return reinterpret_cast<jlong>(detector);
}
JNIEXPORT void JNICALL
Java_at_lockstep_filter_StepDetector_native_1delete(
JNIEnv *env,
jclass /*unused*/, jlong handle) {
delete reinterpret_cast<StepDetector *>(handle);
}
JNIEXPORT void JNICALL
Java_at_lockstep_filter_StepDetector_native_1filter(
JNIEnv *env,
jclass /*unused*/, jlong handle, jlong timestamp, jfloatArray values) {
if(values == nullptr) throwIllegalArgumentException(env, "values == null");
float* nativeValues = (float *)env->GetFloatArrayElements(values, 0);
jsize length = env->GetArrayLength(values);
std::vector<float> vecValues(nativeValues, nativeValues + length);
auto *detector = reinterpret_cast<StepDetector *>(handle);
detector->filter(vecValues);
}
}