Skip to content

Commit

Permalink
fix: native crash in serial logic
Browse files Browse the repository at this point in the history
  • Loading branch information
feelfreelinux committed Mar 26, 2021

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent d4b1360 commit 566183b
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions app/app/src/main/jni/vsp-pty.cpp
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@
#include <android/log.h>
#include <stdio.h>
#include <pty.h>
#include <pthread.h>
#include <termios.h>
#include <fcntl.h>
#include <sys/ioctl.h>
@@ -23,6 +24,9 @@ static JavaVM *jvm = NULL;
JNIEnv *storedEnv;
jmethodID stringCallback;
jweak storeWeakListener;
jclass serialDataClass;
jmethodID serialDataConstructor;
pthread_t ptyThreadHandle = NULL;

static jint getBaudrate(speed_t baudrate)
{
@@ -135,9 +139,7 @@ extern "C"

jbyteArray serialDataArr = gEnv->NewByteArray(dataSize);
gEnv->SetByteArrayRegion(serialDataArr, 0, dataSize, (jbyte *)val);
jclass cls = gEnv->FindClass("com/octo4a/serial/SerialData");
jmethodID constructor = gEnv->GetMethodID(cls, "<init>", "([BIIIII)V");
jobject object = gEnv->NewObject(cls, constructor, serialDataArr, baudrate);
jobject object = gEnv->NewObject(serialDataClass, serialDataConstructor, serialDataArr, baudrate);

gEnv->CallVoidMethod(storeWeakListener, stringCallback, object);
gEnv->DeleteLocalRef(object);
@@ -147,14 +149,12 @@ extern "C"
gEnv->ExceptionDescribe();
}

if (getEnvStat != JNI_EDETACHED)
{
jvm->DetachCurrentThread();
}

jvm->DetachCurrentThread();
}
}

void ptyThread()
static void* ptyThread(void* irrelevant)
{

int slave;
@@ -295,6 +295,8 @@ extern "C"
storedEnv = env;

storeWeakListener = env->NewWeakGlobalRef(listener);
serialDataClass = (jclass) env->NewGlobalRef(env->FindClass("com/octo4a/serial/SerialData"));
serialDataConstructor = env->GetMethodID(serialDataClass, "<init>", "([BIIIII)V");
jclass clazz = env->GetObjectClass(storeWeakListener);
stringCallback = env->GetMethodID(clazz, "onDataReceived", "(Lcom/octo4a/serial/SerialData;)V");

@@ -305,6 +307,8 @@ extern "C"

JNIEXPORT void JNICALL Java_com_octo4a_serial_VSPPty_runPtyThread(JNIEnv *env, jobject instance)
{
ptyThread();
if (ptyThreadHandle == NULL) {
pthread_create(&ptyThreadHandle, NULL, &ptyThread, NULL);
}
}
}

0 comments on commit 566183b

Please sign in to comment.