From 7778b924e59b0a44484fed05105933af30d3b769 Mon Sep 17 00:00:00 2001 From: Will Hedgecock Date: Tue, 4 Jan 2022 18:53:07 -0600 Subject: [PATCH] Disable event-based line status changes on OSs for which it is unavailable --- src/main/c/Posix/PosixHelperFunctions.c | 2 ++ src/main/c/Posix/SerialPort_Posix.c | 12 +++++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/main/c/Posix/PosixHelperFunctions.c b/src/main/c/Posix/PosixHelperFunctions.c index 731d225..bcc7f12 100644 --- a/src/main/c/Posix/PosixHelperFunctions.c +++ b/src/main/c/Posix/PosixHelperFunctions.c @@ -61,7 +61,9 @@ serialPort* pushBack(serialPortVector* vector, const char* key, const char* frie pthread_mutex_init(&port->eventMutex, NULL); pthread_condattr_t conditionVariableAttributes; pthread_condattr_init(&conditionVariableAttributes); +#if !defined(__APPLE__) pthread_condattr_setclock(&conditionVariableAttributes, CLOCK_MONOTONIC); +#endif pthread_cond_init(&port->eventReceived, &conditionVariableAttributes); pthread_condattr_destroy(&conditionVariableAttributes); diff --git a/src/main/c/Posix/SerialPort_Posix.c b/src/main/c/Posix/SerialPort_Posix.c index 2fc3f89..d331b37 100644 --- a/src/main/c/Posix/SerialPort_Posix.c +++ b/src/main/c/Posix/SerialPort_Posix.c @@ -79,6 +79,8 @@ jfieldID eventFlagsField; // List of available serial ports serialPortVector serialPorts = { NULL, 0, 0 }; +#if defined(__linux__) && !defined(__ANDROID__) + // Event listening threads void* eventReadingThread1(void *serialPortPointer) { @@ -180,6 +182,8 @@ void* eventReadingThread2(void *serialPortPointer) return NULL; } +#endif // #if defined(__linux__) + JNIEXPORT jobjectArray JNICALL Java_com_fazecast_jSerialComm_SerialPort_getCommPorts(JNIEnv *env, jclass serialComm) { // Reset the enumerated flag on all non-open serial ports @@ -605,10 +609,12 @@ JNIEXPORT jint JNICALL Java_com_fazecast_jSerialComm_SerialPort_waitForEvent(JNI { // Initialize the local variables int pollResult; - struct serial_icounter_struct oldSerialLineInterrupts, newSerialLineInterrupts; short pollEventsMask = ((port->eventsMask & com_fazecast_jSerialComm_SerialPort_LISTENING_EVENT_DATA_AVAILABLE) || (port->eventsMask & com_fazecast_jSerialComm_SerialPort_LISTENING_EVENT_DATA_RECEIVED)) ? (POLLIN | POLLERR) : POLLERR; struct pollfd waitingSet = { port->handle, pollEventsMask, 0 }; +#if defined(__linux__) + struct serial_icounter_struct oldSerialLineInterrupts, newSerialLineInterrupts; ioctl(port->handle, TIOCGICOUNT, &oldSerialLineInterrupts); +#endif // #if defined(__linux__) // Wait for a serial port event do @@ -621,6 +627,7 @@ JNIEXPORT jint JNICALL Java_com_fazecast_jSerialComm_SerialPort_waitForEvent(JNI // Return the detected port events if (waitingSet.revents & POLLIN) event |= com_fazecast_jSerialComm_SerialPort_LISTENING_EVENT_DATA_AVAILABLE; +#if defined(__linux__) if (waitingSet.revents & POLLERR) if (!ioctl(port->handle, TIOCGICOUNT, &newSerialLineInterrupts)) { @@ -635,6 +642,7 @@ JNIEXPORT jint JNICALL Java_com_fazecast_jSerialComm_SerialPort_waitForEvent(JNI if (oldSerialLineInterrupts.buf_overrun != newSerialLineInterrupts.buf_overrun) event |= com_fazecast_jSerialComm_SerialPort_LISTENING_EVENT_SOFTWARE_OVERRUN_ERROR; } +#endif // #if defined(__linux__) } return event; } @@ -790,6 +798,7 @@ JNIEXPORT void JNICALL Java_com_fazecast_jSerialComm_SerialPort_setEventListenin // Create or cancel a separate event listening thread if required serialPort *port = (serialPort*)(intptr_t)serialPortPointer; port->eventListenerRunning = eventListenerRunning; +#if defined(__linux__) && !defined(__ANDROID__) if (eventListenerRunning && ((port->eventsMask & com_fazecast_jSerialComm_SerialPort_LISTENING_EVENT_CARRIER_DETECT) || (port->eventsMask & com_fazecast_jSerialComm_SerialPort_LISTENING_EVENT_CTS) || (port->eventsMask & com_fazecast_jSerialComm_SerialPort_LISTENING_EVENT_DSR) || (port->eventsMask & com_fazecast_jSerialComm_SerialPort_LISTENING_EVENT_RING_INDICATOR))) { @@ -818,6 +827,7 @@ JNIEXPORT void JNICALL Java_com_fazecast_jSerialComm_SerialPort_setEventListenin pthread_cancel(port->eventsThread2); port->eventsThread2 = 0; } +#endif // #if defined(__linux__) } JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_setBreak(JNIEnv *env, jobject obj, jlong serialPortPointer)