Keep event-based reading loop in C for Posix

This commit is contained in:
Will Hedgecock 2021-11-29 21:55:54 -06:00
parent ef97fc62bb
commit be704fb60d
24 changed files with 42 additions and 23 deletions

View File

@ -2,7 +2,7 @@
* PosixHelperFunctions.h
*
* Created on: Mar 10, 2015
* Last Updated on: Nov 14, 2021
* Last Updated on: Nov 29, 2021
* Author: Will Hedgecock
*
* Copyright (C) 2012-2021 Fazecast, Inc.
@ -35,6 +35,7 @@ typedef struct serialPort
char *portPath, *friendlyName, *portDescription, *readBuffer;
int errorLineNumber, errorNumber, handle, readBufferLength;
volatile char enumerated, eventListenerRunning;
short eventsMask;
} serialPort;
// Common storage functionality

View File

@ -361,6 +361,11 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_configTimeou
baud_rate baudRate = (*env)->GetIntField(env, obj, baudRateField);
tcgetattr(port->handle, &options);
// Set up the requested event flags
port->eventsMask = 0;
if ((eventsToMonitor & com_fazecast_jSerialComm_SerialPort_LISTENING_EVENT_DATA_AVAILABLE) || (eventsToMonitor & com_fazecast_jSerialComm_SerialPort_LISTENING_EVENT_DATA_RECEIVED))
port->eventsMask |= POLLIN;
// Set updated port timeouts
if ((eventsToMonitor & com_fazecast_jSerialComm_SerialPort_LISTENING_EVENT_DATA_RECEIVED) > 0)
{
@ -378,7 +383,7 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_configTimeou
options.c_cc[VMIN] = 1;
options.c_cc[VTIME] = 0;
}
else if (((timeoutMode & com_fazecast_jSerialComm_SerialPort_TIMEOUT_READ_BLOCKING) > 0) && (readTimeout > 0)) // Read Blocking with timeout
else if (((timeoutMode & com_fazecast_jSerialComm_SerialPort_TIMEOUT_READ_BLOCKING) > 0) && (readTimeout > 0)) // Read Blocking with timeout
{
options.c_cc[VMIN] = 0;
options.c_cc[VTIME] = readTimeout / 100;
@ -424,30 +429,30 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_configTimeou
JNIEXPORT jint JNICALL Java_com_fazecast_jSerialComm_SerialPort_waitForEvent(JNIEnv *env, jobject obj, jlong serialPortPointer)
{
// Initialize the waiting set
// Initialize the local variables
int pollResult;
serialPort *port = (serialPort*)(intptr_t)serialPortPointer;
struct pollfd waitingSet = { port->handle, POLLIN, 0 };
struct pollfd waitingSet = { port->handle, port->eventsMask, 0 };
jint event = com_fazecast_jSerialComm_SerialPort_LISTENING_EVENT_TIMED_OUT;
// TODO: START THREAD TO HANDLE ALL OF THIS IN C
// TODO: LISTEN FOR ERROR EVENTS IN CASE SERIAL PORT GETS UNPLUGGED? TEST IF WORKS?
// TODO: STORE ALL AVAILABLE PORTS IN C, RECHECK AVAILABLE PORTS UPON RESCAN SO ALREADY OPEN PORTS DON'T GET MESSED UP
/*
ioctl: TIOCMIWAIT
ioctl: TIOCGICOUNT
static final public int LISTENING_EVENT_BREAK_INTERRUPT = 0x00010000;
static final public int LISTENING_EVENT_CARRIER_DETECT = 0x00020000;
static final public int LISTENING_EVENT_CTS = 0x00040000;
static final public int LISTENING_EVENT_DSR = 0x00080000;
static final public int LISTENING_EVENT_RING_INDICATOR = 0x00100000;
static final public int LISTENING_EVENT_FRAMING_ERROR = 0x00200000;
static final public int LISTENING_EVENT_OVERRUN_ERROR = 0x00400000;
static final public int LISTENING_EVENT_PARITY_ERROR = 0x00800000;
static final public int LISTENING_EVENT_OUTPUT_EMPTY = 0x01000000;*/
*/
// Wait for a serial port event
if (poll(&waitingSet, 1, 500) <= 0)
return 0;
return (waitingSet.revents & POLLIN) ? com_fazecast_jSerialComm_SerialPort_LISTENING_EVENT_DATA_AVAILABLE : 0;
do
{
waitingSet.revents = 0;
pollResult = poll(&waitingSet, 1, 500);
}
while ((pollResult == 0) && port->eventListenerRunning);
// Return the detected port events
if (waitingSet.revents & POLLIN)
event |= com_fazecast_jSerialComm_SerialPort_LISTENING_EVENT_DATA_AVAILABLE;
return event;
}
JNIEXPORT jlong JNICALL Java_com_fazecast_jSerialComm_SerialPort_closePortNative(JNIEnv *env, jobject obj, jlong serialPortPointer)
@ -588,6 +593,11 @@ JNIEXPORT jint JNICALL Java_com_fazecast_jSerialComm_SerialPort_writeBytes(JNIEn
return numBytesWritten;
}
JNIEXPORT void JNICALL Java_com_fazecast_jSerialComm_SerialPort_setEventListeningStatus(JNIEnv *env, jobject obj, jlong serialPortPointer, jboolean eventListenerRunning)
{
((serialPort*)(intptr_t)serialPortPointer)->eventListenerRunning = eventListenerRunning;
}
JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_setBreak(JNIEnv *env, jobject obj, jlong serialPortPointer)
{
serialPort *port = (serialPort*)(intptr_t)serialPortPointer;

View File

@ -67,12 +67,12 @@ extern "C" {
#define com_fazecast_jSerialComm_SerialPort_LISTENING_EVENT_RING_INDICATOR 1048576L
#undef com_fazecast_jSerialComm_SerialPort_LISTENING_EVENT_FRAMING_ERROR
#define com_fazecast_jSerialComm_SerialPort_LISTENING_EVENT_FRAMING_ERROR 2097152L
#undef com_fazecast_jSerialComm_SerialPort_LISTENING_EVENT_OVERRUN_ERROR
#define com_fazecast_jSerialComm_SerialPort_LISTENING_EVENT_OVERRUN_ERROR 4194304L
#undef com_fazecast_jSerialComm_SerialPort_LISTENING_EVENT_FIRMWARE_OVERRUN_ERROR
#define com_fazecast_jSerialComm_SerialPort_LISTENING_EVENT_FIRMWARE_OVERRUN_ERROR 4194304L
#undef com_fazecast_jSerialComm_SerialPort_LISTENING_EVENT_SOFTWARE_OVERRUN_ERROR
#define com_fazecast_jSerialComm_SerialPort_LISTENING_EVENT_SOFTWARE_OVERRUN_ERROR 8388608L
#undef com_fazecast_jSerialComm_SerialPort_LISTENING_EVENT_PARITY_ERROR
#define com_fazecast_jSerialComm_SerialPort_LISTENING_EVENT_PARITY_ERROR 8388608L
#undef com_fazecast_jSerialComm_SerialPort_LISTENING_EVENT_OUTPUT_EMPTY
#define com_fazecast_jSerialComm_SerialPort_LISTENING_EVENT_OUTPUT_EMPTY 16777216L
#define com_fazecast_jSerialComm_SerialPort_LISTENING_EVENT_PARITY_ERROR 16777216L
/*
* Class: com_fazecast_jSerialComm_SerialPort
* Method: getCommPorts
@ -169,6 +169,14 @@ JNIEXPORT jint JNICALL Java_com_fazecast_jSerialComm_SerialPort_readBytes
JNIEXPORT jint JNICALL Java_com_fazecast_jSerialComm_SerialPort_writeBytes
(JNIEnv *, jobject, jlong, jbyteArray, jlong, jlong, jint);
/*
* Class: com_fazecast_jSerialComm_SerialPort
* Method: setEventListeningStatus
* Signature: (JZ)V
*/
JNIEXPORT void JNICALL Java_com_fazecast_jSerialComm_SerialPort_setEventListeningStatus
(JNIEnv *, jobject, jlong, jboolean);
/*
* Class: com_fazecast_jSerialComm_SerialPort
* Method: setBreak