Change system calls from select() to poll() to overcome max FD limit of 1024

This commit is contained in:
hedgecrw85 2018-12-11 16:07:29 -06:00
parent c5741a23e4
commit 5a6dbfd820
21 changed files with 19 additions and 33 deletions

View File

@ -2,7 +2,7 @@
* SerialPort_Android.c
*
* Created on: Mar 13, 2015
* Last Updated on: Oct 08, 2018
* Last Updated on: Dec 07, 2018
* Author: Will Hedgecock
*
* Copyright (C) 2012-2018 Fazecast, Inc.
@ -34,6 +34,7 @@
#include <fcntl.h>
#include <dirent.h>
#include <errno.h>
#include <poll.h>
#include <unistd.h>
#include <termios.h>
#include <sys/time.h>
@ -380,24 +381,15 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_configEventF
JNIEXPORT jint JNICALL Java_com_fazecast_jSerialComm_SerialPort_waitForEvent(JNIEnv *env, jobject obj, jlong serialPortFD)
{
// Initialize the waiting set
if (serialPortFD <= 0)
return 0;
// Initialize the waiting set
fd_set waitingSet;
FD_ZERO(&waitingSet);
FD_SET(serialPortFD, &waitingSet);
struct pollfd waitingSet = { serialPortFD, POLLIN, 0 };
// Wait for a serial port event
int retVal;
do
{
struct timeval timeout = { 1, 0 };
retVal = select(serialPortFD + 1, &waitingSet, NULL, NULL, &timeout);
} while ((retVal < 0) && ((errno == EINTR) || (errno == EAGAIN)));
if (retVal <= 0)
if (poll(&waitingSet, 1, 1000) <= 0)
return 0;
return (FD_ISSET(serialPortFD, &waitingSet)) ? com_fazecast_jSerialComm_SerialPort_LISTENING_EVENT_DATA_AVAILABLE : 0;
return (waitingSet.revents & POLLIN) ? com_fazecast_jSerialComm_SerialPort_LISTENING_EVENT_DATA_AVAILABLE : 0;
}
JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_closePortNative(JNIEnv *env, jobject obj, jlong serialPortFD)

View File

@ -2,7 +2,7 @@
* PosixHelperFunctions.c
*
* Created on: Mar 10, 2015
* Last Updated on: Nov 12, 2018
* Last Updated on: Dec 07, 2018
* Author: Will Hedgecock
*
* Copyright (C) 2012-2018 Fazecast, Inc.
@ -511,7 +511,8 @@ int setBaudRateCustom(int portFD, baud_rate baudRate)
#else
struct serial_struct serInfo;
int retVal = ioctl(portFD, TIOCGSERIAL, &serInfo);
if (retVal == 0) {
if (retVal == 0)
{
serInfo.flags &= ~ASYNC_SPD_MASK;
serInfo.flags |= ASYNC_SPD_CUST | ASYNC_LOW_LATENCY;
serInfo.custom_divisor = serInfo.baud_base / baudRate;

View File

@ -2,7 +2,7 @@
* SerialPort_Posix.c
*
* Created on: Feb 25, 2012
* Last Updated on: Nov 12, 2018
* Last Updated on: Dec 07, 2018
* Author: Will Hedgecock
*
* Copyright (C) 2012-2018 Fazecast, Inc.
@ -25,6 +25,7 @@
#include <errno.h>
#include <fcntl.h>
#include <poll.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
@ -336,8 +337,9 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_configPort(J
// Attempt to set the transmit buffer size and any necessary custom baud rates
#if defined(__linux__)
struct serial_struct serInfo;
int tiocgserialRetVal = ioctl(serialPortFD, TIOCGSERIAL, &serInfo);
if (tiocgserialRetVal == 0) {
int ioctlRetVal = ioctl(serialPortFD, TIOCGSERIAL, &serInfo);
if (ioctlRetVal == 0)
{
serInfo.xmit_fifo_size = sendDeviceQueueSize;
ioctl(serialPortFD, TIOCSSERIAL, &serInfo);
}
@ -447,24 +449,15 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_configEventF
JNIEXPORT jint JNICALL Java_com_fazecast_jSerialComm_SerialPort_waitForEvent(JNIEnv *env, jobject obj, jlong serialPortFD)
{
// Initialize the waiting set
if (serialPortFD <= 0)
return 0;
// Initialize the waiting set
fd_set waitingSet;
FD_ZERO(&waitingSet);
FD_SET(serialPortFD, &waitingSet);
struct pollfd waitingSet = { serialPortFD, POLLIN, 0 };
// Wait for a serial port event
int retVal;
do
{
struct timeval timeout = { 1, 0 };
retVal = select(serialPortFD + 1, &waitingSet, NULL, NULL, &timeout);
} while ((retVal < 0) && ((errno == EINTR) || (errno == EAGAIN)));
if (retVal <= 0)
if (poll(&waitingSet, 1, 1000) <= 0)
return 0;
return (FD_ISSET(serialPortFD, &waitingSet)) ? com_fazecast_jSerialComm_SerialPort_LISTENING_EVENT_DATA_AVAILABLE : 0;
return (waitingSet.revents & POLLIN) ? com_fazecast_jSerialComm_SerialPort_LISTENING_EVENT_DATA_AVAILABLE : 0;
}
JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_closePortNative(JNIEnv *env, jobject obj, jlong serialPortFD)

View File

@ -32,7 +32,7 @@ import java.util.Scanner;
* This class provides a test case for the jSerialComm library.
*
* @author Will Hedgecock &lt;will.hedgecock@gmail.com&gt;
* @version 2.3.0
* @version 2.4.0
* @see java.io.InputStream
* @see java.io.OutputStream
*/