Disable event-based line status changes on OSs for which it is unavailable

This commit is contained in:
Will Hedgecock 2022-01-04 18:53:07 -06:00
parent 144c9928cd
commit 7778b924e5
2 changed files with 13 additions and 1 deletions

View File

@ -61,7 +61,9 @@ serialPort* pushBack(serialPortVector* vector, const char* key, const char* frie
pthread_mutex_init(&port->eventMutex, NULL); pthread_mutex_init(&port->eventMutex, NULL);
pthread_condattr_t conditionVariableAttributes; pthread_condattr_t conditionVariableAttributes;
pthread_condattr_init(&conditionVariableAttributes); pthread_condattr_init(&conditionVariableAttributes);
#if !defined(__APPLE__)
pthread_condattr_setclock(&conditionVariableAttributes, CLOCK_MONOTONIC); pthread_condattr_setclock(&conditionVariableAttributes, CLOCK_MONOTONIC);
#endif
pthread_cond_init(&port->eventReceived, &conditionVariableAttributes); pthread_cond_init(&port->eventReceived, &conditionVariableAttributes);
pthread_condattr_destroy(&conditionVariableAttributes); pthread_condattr_destroy(&conditionVariableAttributes);

View File

@ -79,6 +79,8 @@ jfieldID eventFlagsField;
// List of available serial ports // List of available serial ports
serialPortVector serialPorts = { NULL, 0, 0 }; serialPortVector serialPorts = { NULL, 0, 0 };
#if defined(__linux__) && !defined(__ANDROID__)
// Event listening threads // Event listening threads
void* eventReadingThread1(void *serialPortPointer) void* eventReadingThread1(void *serialPortPointer)
{ {
@ -180,6 +182,8 @@ void* eventReadingThread2(void *serialPortPointer)
return NULL; return NULL;
} }
#endif // #if defined(__linux__)
JNIEXPORT jobjectArray JNICALL Java_com_fazecast_jSerialComm_SerialPort_getCommPorts(JNIEnv *env, jclass serialComm) JNIEXPORT jobjectArray JNICALL Java_com_fazecast_jSerialComm_SerialPort_getCommPorts(JNIEnv *env, jclass serialComm)
{ {
// Reset the enumerated flag on all non-open serial ports // 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 // Initialize the local variables
int pollResult; 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; 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 }; struct pollfd waitingSet = { port->handle, pollEventsMask, 0 };
#if defined(__linux__)
struct serial_icounter_struct oldSerialLineInterrupts, newSerialLineInterrupts;
ioctl(port->handle, TIOCGICOUNT, &oldSerialLineInterrupts); ioctl(port->handle, TIOCGICOUNT, &oldSerialLineInterrupts);
#endif // #if defined(__linux__)
// Wait for a serial port event // Wait for a serial port event
do do
@ -621,6 +627,7 @@ JNIEXPORT jint JNICALL Java_com_fazecast_jSerialComm_SerialPort_waitForEvent(JNI
// Return the detected port events // Return the detected port events
if (waitingSet.revents & POLLIN) if (waitingSet.revents & POLLIN)
event |= com_fazecast_jSerialComm_SerialPort_LISTENING_EVENT_DATA_AVAILABLE; event |= com_fazecast_jSerialComm_SerialPort_LISTENING_EVENT_DATA_AVAILABLE;
#if defined(__linux__)
if (waitingSet.revents & POLLERR) if (waitingSet.revents & POLLERR)
if (!ioctl(port->handle, TIOCGICOUNT, &newSerialLineInterrupts)) 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) if (oldSerialLineInterrupts.buf_overrun != newSerialLineInterrupts.buf_overrun)
event |= com_fazecast_jSerialComm_SerialPort_LISTENING_EVENT_SOFTWARE_OVERRUN_ERROR; event |= com_fazecast_jSerialComm_SerialPort_LISTENING_EVENT_SOFTWARE_OVERRUN_ERROR;
} }
#endif // #if defined(__linux__)
} }
return event; 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 // Create or cancel a separate event listening thread if required
serialPort *port = (serialPort*)(intptr_t)serialPortPointer; serialPort *port = (serialPort*)(intptr_t)serialPortPointer;
port->eventListenerRunning = eventListenerRunning; 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) || 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))) (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); pthread_cancel(port->eventsThread2);
port->eventsThread2 = 0; port->eventsThread2 = 0;
} }
#endif // #if defined(__linux__)
} }
JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_setBreak(JNIEnv *env, jobject obj, jlong serialPortPointer) JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_setBreak(JNIEnv *env, jobject obj, jlong serialPortPointer)