Consolidate all Posix-based OSs into one platform

This commit is contained in:
hedgecrw85 2018-11-13 10:08:19 -06:00
parent 0260a0961a
commit b4d3898355
27 changed files with 713 additions and 1106 deletions

View File

@ -597,7 +597,7 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_presetRTS(JN
// Send a system command to preset the RTS mode of the serial port
char commandString[64];
sprintf(commandString, "stty -f %s hupcl", portName);
sprintf(commandString, "stty -F %s hupcl", portName);
int result = system(commandString);
(*env)->ReleaseStringUTFChars(env, portNameJString, portName);
@ -611,7 +611,7 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_preclearRTS(
// Send a system command to preset the RTS mode of the serial port
char commandString[64];
sprintf(commandString, "stty -f %s -hupcl", portName);
sprintf(commandString, "stty -F %s -hupcl", portName);
int result = system(commandString);
(*env)->ReleaseStringUTFChars(env, portNameJString, portName);
@ -641,7 +641,7 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_presetDTR(JN
// Send a system command to preset the DTR mode of the serial port
char commandString[64];
sprintf(commandString, "stty -f %s hupcl", portName);
sprintf(commandString, "stty -F %s hupcl", portName);
int result = system(commandString);
(*env)->ReleaseStringUTFChars(env, portNameJString, portName);
@ -655,7 +655,7 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_preclearDTR(
// Send a system command to preset the DTR mode of the serial port
char commandString[64];
sprintf(commandString, "stty -f %s -hupcl", portName);
sprintf(commandString, "stty -F %s -hupcl", portName);
int result = system(commandString);
(*env)->ReleaseStringUTFChars(env, portNameJString, portName);

View File

@ -1,168 +0,0 @@
# Linux specific library variables
COMPILE := gcc
COMPILE_ARM32 := arm32-unknown-linux-gnueabi-gcc
COMPILE_ARM32HF := arm32hf-unknown-linux-gnueabi-gcc
COMPILE_ARM64 := aarch64-unknown-linux-gnueabi-gcc
LINK := gcc
LINK_ARM32 := arm32-unknown-linux-gnueabi-gcc
LINK_ARM32HF := arm32hf-unknown-linux-gnueabi-gcc
LINK_ARM64 := aarch64-unknown-linux-gnueabi-gcc
CFLAGS := -fPIC -static-libgcc -Os -flto -fuse-linker-plugin -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=0
LDFLAGS := -shared -static-libgcc -flto -fuse-linker-plugin -Os
INCLUDES := -I"$(JDK_HOME)/include" -I"$(JDK_HOME)/include/linux"
LIBRARIES :=
DELETE := @rm
MKDIR := @mkdir
COPY := @cp
MOVE := @mv
PRINT := @echo
FULL_CLASS := com.fazecast.jSerialComm.SerialPort
JAVAC := "$(JDK_HOME)/bin/javac"
JAVAH := "$(JDK_HOME)/bin/javah" -jni
JFLAGS := -source 1.6 -target 1.6 -Xlint:-options
LIBRARY_NAME := libjSerialComm.so
JAVA_SOURCE_DIR = ../../../../src/main/java/com/fazecast/jSerialComm
RESOURCE_DIR = ../../../../src/main/resources/Linux
BUILD_DIR = ../../../../bin/Linux
JAVA_CLASS_DIR = $(BUILD_DIR)/../com/fazecast/jSerialComm
OBJECTSx86 = $(BUILD_DIR)/x86/SerialPort_Linux.o $(BUILD_DIR)/x86/LinuxHelperFunctions.o
OBJECTSx86_64 = $(BUILD_DIR)/x86_64/SerialPort_Linux.o $(BUILD_DIR)/x86_64/LinuxHelperFunctions.o
OBJECTSarmv5 = $(BUILD_DIR)/armv5/SerialPort_Linux.o $(BUILD_DIR)/armv5/LinuxHelperFunctions.o
OBJECTSarmv6 = $(BUILD_DIR)/armv6/SerialPort_Linux.o $(BUILD_DIR)/armv6/LinuxHelperFunctions.o
OBJECTSarmv6-hf = $(BUILD_DIR)/armv6-hf/SerialPort_Linux.o $(BUILD_DIR)/armv6-hf/LinuxHelperFunctions.o
OBJECTSarmv7 = $(BUILD_DIR)/armv7/SerialPort_Linux.o $(BUILD_DIR)/armv7/LinuxHelperFunctions.o
OBJECTSarmv7-hf = $(BUILD_DIR)/armv7-hf/SerialPort_Linux.o $(BUILD_DIR)/armv7-hf/LinuxHelperFunctions.o
OBJECTSarmv8_32 = $(BUILD_DIR)/armv8_32/SerialPort_Linux.o $(BUILD_DIR)/armv8_32/LinuxHelperFunctions.o
OBJECTSarmv8_64 = $(BUILD_DIR)/armv8_64/SerialPort_Linux.o $(BUILD_DIR)/armv8_64/LinuxHelperFunctions.o
FLAGSarmv5 = -marm -mthumb-interwork -march=armv5 -mfloat-abi=soft
FLAGSarmv6 = -marm -mthumb-interwork -march=armv6 -mfloat-abi=soft
FLAGSarmv6hf = -marm -mthumb-interwork -march=armv6 -mfloat-abi=hard
FLAGSarmv7 = -mthumb -mthumb-interwork -march=armv7 -mfloat-abi=soft
FLAGSarmv7hf = -mthumb -mthumb-interwork -march=armv7 -mfloat-abi=hard
FLAGSarmv8_32 = -mthumb -mthumb-interwork -march=armv8-a
FLAGSarmv8_64 = -march=armv8-a
JNI_HEADER = ../com_fazecast_jSerialComm_SerialPort.h
JAVA_CLASS = $(JAVA_CLASS_DIR)/SerialPort.class
# Define phony and suffix rules
.PHONY: all checkdirs linux32 linux64 arm armv5 armv6 armv6-hf armv7 armv7-hf armv8_32 armv8_64
.SUFFIXES:
.SUFFIXES: .cpp .c .o .class .java .h
# Default build target builds both 32 and 64 bit libraries
all : linux32 linux64
$(DELETE) -rf `readlink -f $(BUILD_DIR)/..`
$(DELETE) -rf ../*.h
# Builds 32-bit Linux libraries
linux32 : ARCH = -m32
linux32 : checkdirs $(BUILD_DIR)/x86/$(LIBRARY_NAME)
$(DELETE) -rf $(BUILD_DIR)/x86/*.o
$(COPY) $(BUILD_DIR)/x86/*.* $(RESOURCE_DIR)/x86/
# Builds 64-bit Linux libraries
linux64 : ARCH = -m64
linux64 : checkdirs $(BUILD_DIR)/x86_64/$(LIBRARY_NAME)
$(DELETE) -rf $(BUILD_DIR)/x86_64/*.o
$(COPY) $(BUILD_DIR)/x86_64/*.* $(RESOURCE_DIR)/x86_64/
# Build all ARM libraries
arm : armv5 armv6 armv6-hf armv7 armv7-hf armv8_32 armv8_64
$(DELETE) -rf `readlink -f $(BUILD_DIR)/..`
$(DELETE) -rf ../*.h
armv5 : checkdirs $(BUILD_DIR)/armv5/$(LIBRARY_NAME)
$(DELETE) -rf $(BUILD_DIR)/armv5/*.o
$(COPY) $(BUILD_DIR)/armv5/*.* $(RESOURCE_DIR)/armv5/
armv6 : checkdirs $(BUILD_DIR)/armv6/$(LIBRARY_NAME)
$(DELETE) -rf $(BUILD_DIR)/armv6/*.o
$(COPY) $(BUILD_DIR)/armv6/*.* $(RESOURCE_DIR)/armv6/
armv6-hf : checkdirs $(BUILD_DIR)/armv6-hf/$(LIBRARY_NAME)
$(DELETE) -rf $(BUILD_DIR)/armv6-hf/*.o
$(COPY) $(BUILD_DIR)/armv6-hf/*.* $(RESOURCE_DIR)/armv6-hf/
armv7 : checkdirs $(BUILD_DIR)/armv7/$(LIBRARY_NAME)
$(DELETE) -rf $(BUILD_DIR)/armv7/*.o
$(COPY) $(BUILD_DIR)/armv7/*.* $(RESOURCE_DIR)/armv7/
armv7-hf : checkdirs $(BUILD_DIR)/armv7-hf/$(LIBRARY_NAME)
$(DELETE) -rf $(BUILD_DIR)/armv7-hf/*.o
$(COPY) $(BUILD_DIR)/armv7-hf/*.* $(RESOURCE_DIR)/armv7-hf/
armv8_32 : checkdirs $(BUILD_DIR)/armv8_32/$(LIBRARY_NAME)
$(DELETE) -rf $(BUILD_DIR)/armv8_32/*.o
$(COPY) $(BUILD_DIR)/armv8_32/*.* $(RESOURCE_DIR)/armv8_32/
armv8_64 : checkdirs $(BUILD_DIR)/armv8_64/$(LIBRARY_NAME)
$(DELETE) -rf $(BUILD_DIR)/armv8_64/*.o
$(COPY) $(BUILD_DIR)/armv8_64/*.* $(RESOURCE_DIR)/armv8_64/
# Rule to create build directories
checkdirs : $(BUILD_DIR)/x86 $(BUILD_DIR)/x86_64 $(BUILD_DIR)/armv5 $(BUILD_DIR)/armv6 $(BUILD_DIR)/armv6-hf $(BUILD_DIR)/armv7 $(BUILD_DIR)/armv7-hf $(BUILD_DIR)/armv8_32 $(BUILD_DIR)/armv8_64 $(JAVA_CLASS_DIR)
$(BUILD_DIR)/x86 :
$(MKDIR) -p $@
$(BUILD_DIR)/x86_64 :
$(MKDIR) -p $@
$(BUILD_DIR)/armv5 :
$(MKDIR) -p $@
$(BUILD_DIR)/armv6 :
$(MKDIR) -p $@
$(BUILD_DIR)/armv6-hf :
$(MKDIR) -p $@
$(BUILD_DIR)/armv7 :
$(MKDIR) -p $@
$(BUILD_DIR)/armv7-hf :
$(MKDIR) -p $@
$(BUILD_DIR)/armv8_32 :
$(MKDIR) -p $@
$(BUILD_DIR)/armv8_64 :
$(MKDIR) -p $@
$(JAVA_CLASS_DIR) :
$(MKDIR) -p $@
# Rule to build 32-bit library
$(BUILD_DIR)/x86/$(LIBRARY_NAME) : $(JNI_HEADER) $(OBJECTSx86)
$(LINK) $(LDFLAGS) $(ARCH) -o $@ $(OBJECTSx86) $(LIBRARIES)
# Rule to build 64-bit library
$(BUILD_DIR)/x86_64/$(LIBRARY_NAME) : $(JNI_HEADER) $(OBJECTSx86_64)
$(LINK) $(LDFLAGS) $(ARCH) -o $@ $(OBJECTSx86_64) $(LIBRARIES)
# Rule to build ARM libraries
$(BUILD_DIR)/armv5/$(LIBRARY_NAME) : $(JNI_HEADER) $(OBJECTSarmv5)
$(LINK_ARM32) $(LDFLAGS) $(FLAGSarmv5) -o $@ $(OBJECTSarmv5) $(LIBRARIES)
$(BUILD_DIR)/armv6/$(LIBRARY_NAME) : $(JNI_HEADER) $(OBJECTSarmv6)
$(LINK_ARM32) $(LDFLAGS) $(FLAGSarmv6) -o $@ $(OBJECTSarmv6) $(LIBRARIES)
$(BUILD_DIR)/armv6-hf/$(LIBRARY_NAME) : $(JNI_HEADER) $(OBJECTSarmv6-hf)
$(LINK_ARM32HF) $(LDFLAGS) $(FLAGSarmv6-hf) -o $@ $(OBJECTSarmv6-hf) $(LIBRARIES)
$(BUILD_DIR)/armv7/$(LIBRARY_NAME) : $(JNI_HEADER) $(OBJECTSarmv7)
$(LINK_ARM32) $(LDFLAGS) $(FLAGSarmv7) -o $@ $(OBJECTSarmv7) $(LIBRARIES)
$(BUILD_DIR)/armv7-hf/$(LIBRARY_NAME) : $(JNI_HEADER) $(OBJECTSarmv7-hf)
$(LINK_ARM32HF) $(LDFLAGS) $(FLAGSarmv7-hf) -o $@ $(OBJECTSarmv7-hf) $(LIBRARIES)
$(BUILD_DIR)/armv8_32/$(LIBRARY_NAME) : $(JNI_HEADER) $(OBJECTSarmv8_32)
$(LINK_ARM32) $(LDFLAGS) $(FLAGSarmv8_32) -o $@ $(OBJECTSarmv8_32) $(LIBRARIES)
$(BUILD_DIR)/armv8_64/$(LIBRARY_NAME) : $(JNI_HEADER) $(OBJECTSarmv8_64)
$(LINK_ARM64) $(LDFLAGS) $(FLAGSarmv8_64) -o $@ $(OBJECTSarmv8_64) $(LIBRARIES)
# Suffix rules to get from *.c -> *.o
$(BUILD_DIR)/x86/%.o : %.c
$(COMPILE) $(INCLUDES) $(CFLAGS) $(ARCH) -c $< -o $@
$(BUILD_DIR)/x86_64/%.o : %.c
$(COMPILE) $(INCLUDES) $(CFLAGS) $(ARCH) -c $< -o $@
$(BUILD_DIR)/armv5/%.o : %.c
$(COMPILE_ARM32) $(INCLUDES) $(CFLAGS) $(FLAGSarmv5) -c $< -o $@
$(BUILD_DIR)/armv6/%.o : %.c
$(COMPILE_ARM32) $(INCLUDES) $(CFLAGS) $(FLAGSarmv6) -c $< -o $@
$(BUILD_DIR)/armv6-hf/%.o : %.c
$(COMPILE_ARM32HF) $(INCLUDES) $(CFLAGS) $(FLAGSarmv6-hf) -c $< -o $@
$(BUILD_DIR)/armv7/%.o : %.c
$(COMPILE_ARM32) $(INCLUDES) $(CFLAGS) $(FLAGSarmv7) -c $< -o $@
$(BUILD_DIR)/armv7-hf/%.o : %.c
$(COMPILE_ARM32HF) $(INCLUDES) $(CFLAGS) $(FLAGSarmv7-hf) -c $< -o $@
$(BUILD_DIR)/armv8_32/%.o : %.c
$(COMPILE_ARM32) $(INCLUDES) $(CFLAGS) $(FLAGSarmv8_32) -c $< -o $@
$(BUILD_DIR)/armv8_64/%.o : %.c
$(COMPILE_ARM64) $(INCLUDES) $(CFLAGS) $(FLAGSarmv8_64) -c $< -o $@
# Rule to build JNI header file
$(JNI_HEADER) : $(JAVA_CLASS)
$(JAVAH) -d .. -classpath $(JAVA_CLASS_DIR)/../../.. $(FULL_CLASS)
# Suffix rule to get from *.java -> *.class
$(JAVA_CLASS) :
$(JAVAC) $(JFLAGS) -d $(JAVA_CLASS_DIR)/../../.. -cp $(JAVA_SOURCE_DIR)/../../.. $(JAVA_SOURCE_DIR)/$(basename $(notdir $@)).java

View File

@ -1,655 +0,0 @@
/*
* SerialPort_Linux.c
*
* Created on: Feb 25, 2012
* Last Updated on: Nov 07, 2018
* Author: Will Hedgecock
*
* Copyright (C) 2012-2018 Fazecast, Inc.
*
* This file is part of jSerialComm.
*
* jSerialComm is free software: you can redistribute it and/or modify
* it under the terms of either the Apache Software License, version 2, or
* the GNU Lesser General Public License as published by the Free Software
* Foundation, version 3 or above.
*
* jSerialComm is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of both the GNU Lesser General Public
* License and the Apache Software License along with jSerialComm. If not,
* see <http://www.gnu.org/licenses/> and <http://www.apache.org/licenses/>.
*/
#ifdef __linux__
#ifndef CMSPAR
#define CMSPAR 010000000000
#endif
#include <stdlib.h>
#include <sys/ioctl.h>
#include <linux/serial.h>
#include <fcntl.h>
#include <dirent.h>
#include <errno.h>
#include <unistd.h>
#include <termios.h>
#include <sys/time.h>
#include "../com_fazecast_jSerialComm_SerialPort.h"
#include "LinuxHelperFunctions.h"
// Cached class, method, and field IDs
jclass serialCommClass;
jmethodID serialCommConstructor;
jfieldID serialPortFdField;
jfieldID comPortField;
jfieldID friendlyNameField;
jfieldID portDescriptionField;
jfieldID isOpenedField;
jfieldID disableConfigField;
jfieldID isDtrEnabledField;
jfieldID isRtsEnabledField;
jfieldID baudRateField;
jfieldID dataBitsField;
jfieldID stopBitsField;
jfieldID parityField;
jfieldID flowControlField;
jfieldID sendDeviceQueueSizeField;
jfieldID receiveDeviceQueueSizeField;
jfieldID timeoutModeField;
jfieldID readTimeoutField;
jfieldID writeTimeoutField;
jfieldID eventFlagsField;
JNIEXPORT jobjectArray JNICALL Java_com_fazecast_jSerialComm_SerialPort_getCommPorts(JNIEnv *env, jclass serialComm)
{
// Enumerate serial ports on machine
charTupleVector serialPorts = { (char**)malloc(1), (char**)malloc(1), (char**)malloc(1), 0 };
recursiveSearchForComPorts(&serialPorts, "/sys/devices/");
lastDitchSearchForComPorts(&serialPorts);
jobjectArray arrayObject = (*env)->NewObjectArray(env, serialPorts.length, serialCommClass, 0);
int i;
for (i = 0; i < serialPorts.length; ++i)
{
// Create new SerialComm object containing the enumerated values
jobject serialCommObject = (*env)->NewObject(env, serialCommClass, serialCommConstructor);
(*env)->SetObjectField(env, serialCommObject, portDescriptionField, (*env)->NewStringUTF(env, serialPorts.third[i]));
(*env)->SetObjectField(env, serialCommObject, friendlyNameField, (*env)->NewStringUTF(env, serialPorts.second[i]));
(*env)->SetObjectField(env, serialCommObject, comPortField, (*env)->NewStringUTF(env, serialPorts.first[i]));
free(serialPorts.first[i]);
free(serialPorts.second[i]);
free(serialPorts.third[i]);
// Add new SerialComm object to array
(*env)->SetObjectArrayElement(env, arrayObject, i, serialCommObject);
}
free(serialPorts.first);
free(serialPorts.second);
free(serialPorts.third);
return arrayObject;
}
JNIEXPORT void JNICALL Java_com_fazecast_jSerialComm_SerialPort_initializeLibrary(JNIEnv *env, jclass serialComm)
{
// Cache class and method ID as global references
serialCommClass = (jclass)(*env)->NewGlobalRef(env, serialComm);
serialCommConstructor = (*env)->GetMethodID(env, serialCommClass, "<init>", "()V");
// Cache
serialPortFdField = (*env)->GetFieldID(env, serialCommClass, "portHandle", "J");
comPortField = (*env)->GetFieldID(env, serialCommClass, "comPort", "Ljava/lang/String;");
friendlyNameField = (*env)->GetFieldID(env, serialCommClass, "friendlyName", "Ljava/lang/String;");
portDescriptionField = (*env)->GetFieldID(env, serialCommClass, "portDescription", "Ljava/lang/String;");
isOpenedField = (*env)->GetFieldID(env, serialCommClass, "isOpened", "Z");
disableConfigField = (*env)->GetFieldID(env, serialCommClass, "disableConfig", "Z");
isDtrEnabledField = (*env)->GetFieldID(env, serialCommClass, "isDtrEnabled", "Z");
isRtsEnabledField = (*env)->GetFieldID(env, serialCommClass, "isRtsEnabled", "Z");
baudRateField = (*env)->GetFieldID(env, serialCommClass, "baudRate", "I");
dataBitsField = (*env)->GetFieldID(env, serialCommClass, "dataBits", "I");
stopBitsField = (*env)->GetFieldID(env, serialCommClass, "stopBits", "I");
parityField = (*env)->GetFieldID(env, serialCommClass, "parity", "I");
flowControlField = (*env)->GetFieldID(env, serialCommClass, "flowControl", "I");
sendDeviceQueueSizeField = (*env)->GetFieldID(env, serialCommClass, "sendDeviceQueueSize", "I");
receiveDeviceQueueSizeField = (*env)->GetFieldID(env, serialCommClass, "receiveDeviceQueueSize", "I");
timeoutModeField = (*env)->GetFieldID(env, serialCommClass, "timeoutMode", "I");
readTimeoutField = (*env)->GetFieldID(env, serialCommClass, "readTimeout", "I");
writeTimeoutField = (*env)->GetFieldID(env, serialCommClass, "writeTimeout", "I");
eventFlagsField = (*env)->GetFieldID(env, serialCommClass, "eventFlags", "I");
}
JNIEXPORT void JNICALL Java_com_fazecast_jSerialComm_SerialPort_uninitializeLibrary(JNIEnv *env, jclass serialComm)
{
// Delete the cache global reference
(*env)->DeleteGlobalRef(env, serialCommClass);
}
JNIEXPORT jlong JNICALL Java_com_fazecast_jSerialComm_SerialPort_openPortNative(JNIEnv *env, jobject obj)
{
jstring portNameJString = (jstring)(*env)->GetObjectField(env, obj, comPortField);
const char *portName = (*env)->GetStringUTFChars(env, portNameJString, NULL);
unsigned char isDtrEnabled = (*env)->GetBooleanField(env, obj, isDtrEnabledField);
unsigned char isRtsEnabled = (*env)->GetBooleanField(env, obj, isRtsEnabledField);
// Try to open existing serial port with read/write access
int serialPortFD = -1;
if ((serialPortFD = open(portName, O_RDWR | O_NOCTTY | O_NONBLOCK)) > 0)
{
// Clear any serial port flags and set up raw, non-canonical port parameters
struct termios options = { 0 };
fcntl(serialPortFD, F_SETFL, 0);
tcgetattr(serialPortFD, &options);
cfmakeraw(&options);
if (!isDtrEnabled || !isRtsEnabled)
options.c_cflag &= ~HUPCL;
options.c_iflag |= BRKINT;
tcsetattr(serialPortFD, TCSANOW, &options);
// Configure the port parameters and timeouts
if (Java_com_fazecast_jSerialComm_SerialPort_configPort(env, obj, serialPortFD))
(*env)->SetBooleanField(env, obj, isOpenedField, JNI_TRUE);
else
{
// Close the port if there was a problem setting the parameters
ioctl(serialPortFD, TIOCNXCL);
tcdrain(serialPortFD);
while ((close(serialPortFD) == -1) && (errno != EBADF));
serialPortFD = -1;
(*env)->SetBooleanField(env, obj, isOpenedField, JNI_FALSE);
}
}
(*env)->ReleaseStringUTFChars(env, portNameJString, portName);
return serialPortFD;
}
JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_configPort(JNIEnv *env, jobject obj, jlong serialPortFD)
{
if (serialPortFD <= 0)
return JNI_FALSE;
struct serial_struct serInfo;
struct termios options = { 0 };
// Get port parameters from Java class
int baudRate = (*env)->GetIntField(env, obj, baudRateField);
int byteSizeInt = (*env)->GetIntField(env, obj, dataBitsField);
int stopBitsInt = (*env)->GetIntField(env, obj, stopBitsField);
int parityInt = (*env)->GetIntField(env, obj, parityField);
int flowControl = (*env)->GetIntField(env, obj, flowControlField);
int sendDeviceQueueSize = (*env)->GetIntField(env, obj, sendDeviceQueueSizeField);
int receiveDeviceQueueSize = (*env)->GetIntField(env, obj, receiveDeviceQueueSizeField);
unsigned char configDisabled = (*env)->GetBooleanField(env, obj, disableConfigField);
unsigned char isDtrEnabled = (*env)->GetBooleanField(env, obj, isDtrEnabledField);
unsigned char isRtsEnabled = (*env)->GetBooleanField(env, obj, isRtsEnabledField);
tcflag_t byteSize = (byteSizeInt == 5) ? CS5 : (byteSizeInt == 6) ? CS6 : (byteSizeInt == 7) ? CS7 : CS8;
tcflag_t stopBits = ((stopBitsInt == com_fazecast_jSerialComm_SerialPort_ONE_STOP_BIT) || (stopBitsInt == com_fazecast_jSerialComm_SerialPort_ONE_POINT_FIVE_STOP_BITS)) ? 0 : CSTOPB;
tcflag_t parity = (parityInt == com_fazecast_jSerialComm_SerialPort_NO_PARITY) ? 0 : (parityInt == com_fazecast_jSerialComm_SerialPort_ODD_PARITY) ? (PARENB | PARODD) : (parityInt == com_fazecast_jSerialComm_SerialPort_EVEN_PARITY) ? PARENB : (parityInt == com_fazecast_jSerialComm_SerialPort_MARK_PARITY) ? (PARENB | CMSPAR | PARODD) : (PARENB | CMSPAR);
tcflag_t CTSRTSEnabled = (((flowControl & com_fazecast_jSerialComm_SerialPort_FLOW_CONTROL_CTS_ENABLED) > 0) ||
((flowControl & com_fazecast_jSerialComm_SerialPort_FLOW_CONTROL_RTS_ENABLED) > 0)) ? CRTSCTS : 0;
tcflag_t XonXoffInEnabled = ((flowControl & com_fazecast_jSerialComm_SerialPort_FLOW_CONTROL_XONXOFF_IN_ENABLED) > 0) ? IXOFF : 0;
tcflag_t XonXoffOutEnabled = ((flowControl & com_fazecast_jSerialComm_SerialPort_FLOW_CONTROL_XONXOFF_OUT_ENABLED) > 0) ? IXON : 0;
// Set updated port parameters
tcgetattr(serialPortFD, &options);
options.c_cflag = (byteSize | stopBits | parity | CLOCAL | CREAD | CTSRTSEnabled);
if (parityInt == com_fazecast_jSerialComm_SerialPort_SPACE_PARITY)
options.c_cflag &= ~PARODD;
if (!isDtrEnabled || !isRtsEnabled)
options.c_cflag &= ~HUPCL;
options.c_iflag &= ~(INPCK | IGNPAR | PARMRK | ISTRIP);
if (byteSizeInt < 8)
options.c_iflag |= ISTRIP;
if (parityInt != 0)
options.c_iflag |= (INPCK | IGNPAR);
options.c_iflag |= (XonXoffInEnabled | XonXoffOutEnabled);
// Set baud rate
unsigned int baudRateCode = getBaudRateCode(baudRate);
if (baudRateCode != 0)
{
cfsetispeed(&options, baudRateCode);
cfsetospeed(&options, baudRateCode);
}
// Apply changes and block non-root users from opening this port
int retVal = configDisabled ? 0 : tcsetattr(serialPortFD, TCSANOW, &options);
ioctl(serialPortFD, TIOCEXCL);
// Attempt to set the transmit buffer size and any necessary custom baud rates
ioctl(serialPortFD, TIOCGSERIAL, &serInfo);
serInfo.xmit_fifo_size = sendDeviceQueueSize;
ioctl(serialPortFD, TIOCSSERIAL, &serInfo);
if (baudRateCode == 0)
setBaudRate(serialPortFD, baudRate);
return ((retVal == 0) && Java_com_fazecast_jSerialComm_SerialPort_configEventFlags(env, obj, serialPortFD) ? JNI_TRUE : JNI_FALSE);
}
JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_configTimeouts(JNIEnv *env, jobject obj, jlong serialPortFD)
{
// Get port timeouts from Java class
if (serialPortFD <= 0)
return JNI_FALSE;
int baudRate = (*env)->GetIntField(env, obj, baudRateField);
unsigned int baudRateCode = getBaudRateCode(baudRate);
int timeoutMode = (*env)->GetIntField(env, obj, timeoutModeField);
int readTimeout = (*env)->GetIntField(env, obj, readTimeoutField);
// Retrieve existing port configuration
struct termios options = { 0 };
tcgetattr(serialPortFD, &options);
int flags = fcntl(serialPortFD, F_GETFL);
if (flags == -1)
return JNI_FALSE;
// Set updated port timeouts
if (((timeoutMode & com_fazecast_jSerialComm_SerialPort_TIMEOUT_READ_SEMI_BLOCKING) > 0) && (readTimeout > 0)) // Read Semi-blocking with timeout
{
flags &= ~O_NONBLOCK;
options.c_cc[VMIN] = 0;
options.c_cc[VTIME] = readTimeout / 100;
}
else if ((timeoutMode & com_fazecast_jSerialComm_SerialPort_TIMEOUT_READ_SEMI_BLOCKING) > 0) // Read Semi-blocking without timeout
{
flags &= ~O_NONBLOCK;
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
{
flags &= ~O_NONBLOCK;
options.c_cc[VMIN] = 0;
options.c_cc[VTIME] = readTimeout / 100;
}
else if ((timeoutMode & com_fazecast_jSerialComm_SerialPort_TIMEOUT_READ_BLOCKING) > 0) // Read Blocking without timeout
{
flags &= ~O_NONBLOCK;
options.c_cc[VMIN] = 1;
options.c_cc[VTIME] = 0;
}
else if ((timeoutMode & com_fazecast_jSerialComm_SerialPort_TIMEOUT_SCANNER) > 0) // Scanner Mode
{
flags &= ~O_NONBLOCK;
options.c_cc[VMIN] = 1;
options.c_cc[VTIME] = 1;
}
else // Non-blocking
{
flags |= O_NONBLOCK;
options.c_cc[VMIN] = 0;
options.c_cc[VTIME] = 0;
}
// Apply changes
int retVal = fcntl(serialPortFD, F_SETFL, flags);
if (retVal != -1)
retVal = tcsetattr(serialPortFD, TCSANOW, &options);
if (baudRateCode == 0) // Set custom baud rate
setBaudRate(serialPortFD, baudRate);
return ((retVal == 0) ? JNI_TRUE : JNI_FALSE);
}
JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_configEventFlags(JNIEnv *env, jobject obj, jlong serialPortFD)
{
if (serialPortFD <= 0)
return JNI_FALSE;
// Get event flags from Java class
int baudRate = (*env)->GetIntField(env, obj, baudRateField);
unsigned int baudRateCode = getBaudRateCode(baudRate);
int eventsToMonitor = (*env)->GetIntField(env, obj, eventFlagsField);
// Change read timeouts if we are monitoring data received
jboolean retVal;
if ((eventsToMonitor & com_fazecast_jSerialComm_SerialPort_LISTENING_EVENT_DATA_RECEIVED) > 0)
{
struct termios options = { 0 };
tcgetattr(serialPortFD, &options);
int flags = fcntl(serialPortFD, F_GETFL);
if (flags == -1)
return JNI_FALSE;
flags &= ~O_NONBLOCK;
options.c_cc[VMIN] = 0;
options.c_cc[VTIME] = 10;
retVal = ((fcntl(serialPortFD, F_SETFL, flags) == -1) || (tcsetattr(serialPortFD, TCSANOW, &options) == -1)) ?
JNI_FALSE : JNI_TRUE;
if (baudRateCode == 0) // Set custom baud rate
setBaudRate(serialPortFD, baudRate);
}
else
retVal = Java_com_fazecast_jSerialComm_SerialPort_configTimeouts(env, obj, serialPortFD);
// Apply changes
return retVal;
}
JNIEXPORT jint JNICALL Java_com_fazecast_jSerialComm_SerialPort_waitForEvent(JNIEnv *env, jobject obj, jlong serialPortFD)
{
if (serialPortFD <= 0)
return 0;
// Initialize the waiting set
fd_set waitingSet;
FD_ZERO(&waitingSet);
FD_SET(serialPortFD, &waitingSet);
// 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)
return 0;
return (FD_ISSET(serialPortFD, &waitingSet)) ? com_fazecast_jSerialComm_SerialPort_LISTENING_EVENT_DATA_AVAILABLE : 0;
}
JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_closePortNative(JNIEnv *env, jobject obj, jlong serialPortFD)
{
// Ensure that the port is open
if (serialPortFD <= 0)
return JNI_TRUE;
// Allow others to open the port
ioctl(serialPortFD, TIOCNXCL);
tcdrain(serialPortFD);
(*env)->SetBooleanField(env, obj, isOpenedField, JNI_FALSE);
// Force the port to enter non-blocking mode to ensure that any current reads return
struct termios options;
tcgetattr(serialPortFD, &options);
int flags = fcntl(serialPortFD, F_GETFL);
flags |= O_NONBLOCK;
options.c_cc[VMIN] = 0;
options.c_cc[VTIME] = 0;
int retVal = fcntl(serialPortFD, F_SETFL, flags);
tcsetattr(serialPortFD, TCSANOW, &options);
// Close the port
while ((close(serialPortFD) == -1) && (errno != EBADF));
(*env)->SetLongField(env, obj, serialPortFdField, -1l);
return JNI_TRUE;
}
JNIEXPORT jint JNICALL Java_com_fazecast_jSerialComm_SerialPort_bytesAvailable(JNIEnv *env, jobject obj, jlong serialPortFD)
{
int numBytesAvailable = -1;
if (serialPortFD > 0)
ioctl(serialPortFD, FIONREAD, &numBytesAvailable);
return numBytesAvailable;
}
JNIEXPORT jint JNICALL Java_com_fazecast_jSerialComm_SerialPort_bytesAwaitingWrite(JNIEnv *env, jobject obj, jlong serialPortFD)
{
int numBytesToWrite = -1;
if (serialPortFD > 0)
ioctl(serialPortFD, TIOCOUTQ, &numBytesToWrite);
return numBytesToWrite;
}
JNIEXPORT jint JNICALL Java_com_fazecast_jSerialComm_SerialPort_readBytes(JNIEnv *env, jobject obj, jlong serialPortFD, jbyteArray buffer, jlong bytesToRead, jlong offset)
{
// Get port handle and read timeout from Java class
if (serialPortFD <= 0)
return -1;
int timeoutMode = (*env)->GetIntField(env, obj, timeoutModeField);
int readTimeout = (*env)->GetIntField(env, obj, readTimeoutField);
int numBytesRead, numBytesReadTotal = 0, bytesRemaining = bytesToRead;
char* readBuffer = (char*)malloc(bytesToRead);
// Infinite blocking mode specified, don't return until we have completely finished the read
if (((timeoutMode & com_fazecast_jSerialComm_SerialPort_TIMEOUT_READ_BLOCKING) > 0) && (readTimeout == 0))
{
// While there are more bytes we are supposed to read
while (bytesRemaining > 0)
{
do { numBytesRead = read(serialPortFD, readBuffer+numBytesReadTotal, bytesRemaining); } while ((numBytesRead < 0) && (errno == EINTR));
if (numBytesRead == -1)
{
// Problem reading, allow others to open the port and close it ourselves
ioctl(serialPortFD, TIOCNXCL);
tcdrain(serialPortFD);
while ((close(serialPortFD) == -1) && (errno != EBADF));
serialPortFD = -1;
(*env)->SetLongField(env, obj, serialPortFdField, -1l);
(*env)->SetBooleanField(env, obj, isOpenedField, JNI_FALSE);
break;
}
// Fix index variables
numBytesReadTotal += numBytesRead;
bytesRemaining -= numBytesRead;
}
}
else if ((timeoutMode & com_fazecast_jSerialComm_SerialPort_TIMEOUT_READ_BLOCKING) > 0) // Blocking mode, but not indefinitely
{
// Get current system time
struct timeval expireTime = { 0 }, currTime = { 0 };
gettimeofday(&expireTime, NULL);
expireTime.tv_usec += (readTimeout * 1000);
if (expireTime.tv_usec > 1000000)
{
expireTime.tv_sec += (expireTime.tv_usec * 0.000001);
expireTime.tv_usec = (expireTime.tv_usec % 1000000);
}
// While there are more bytes we are supposed to read and the timeout has not elapsed
do
{
do { numBytesRead = read(serialPortFD, readBuffer+numBytesReadTotal, bytesRemaining); } while ((numBytesRead < 0) && (errno == EINTR));
if (numBytesRead == -1)
{
// Problem reading, allow others to open the port and close it ourselves
ioctl(serialPortFD, TIOCNXCL);
tcdrain(serialPortFD);
while ((close(serialPortFD) == -1) && (errno != EBADF));
serialPortFD = -1;
(*env)->SetLongField(env, obj, serialPortFdField, -1l);
(*env)->SetBooleanField(env, obj, isOpenedField, JNI_FALSE);
break;
}
// Fix index variables
numBytesReadTotal += numBytesRead;
bytesRemaining -= numBytesRead;
// Get current system time
gettimeofday(&currTime, NULL);
} while ((bytesRemaining > 0) && ((expireTime.tv_sec > currTime.tv_sec) ||
((expireTime.tv_sec == currTime.tv_sec) && (expireTime.tv_usec > currTime.tv_usec))));
}
else // Semi- or non-blocking specified
{
// Read from port
do { numBytesRead = read(serialPortFD, readBuffer, bytesToRead); } while ((numBytesRead < 0) && (errno == EINTR));
if (numBytesRead == -1)
{
// Problem reading, allow others to open the port and close it ourselves
ioctl(serialPortFD, TIOCNXCL);
tcdrain(serialPortFD);
while ((close(serialPortFD) == -1) && (errno != EBADF));
serialPortFD = -1;
(*env)->SetLongField(env, obj, serialPortFdField, -1l);
(*env)->SetBooleanField(env, obj, isOpenedField, JNI_FALSE);
}
else
numBytesReadTotal = numBytesRead;
}
// Return number of bytes read if successful
(*env)->SetByteArrayRegion(env, buffer, offset, numBytesReadTotal, (jbyte*)readBuffer);
free(readBuffer);
return (numBytesRead == -1) || !((*env)->GetBooleanField(env, obj, isOpenedField)) ? -1 : numBytesReadTotal;
}
JNIEXPORT jint JNICALL Java_com_fazecast_jSerialComm_SerialPort_writeBytes(JNIEnv *env, jobject obj, jlong serialPortFD, jbyteArray buffer, jlong bytesToWrite, jlong offset)
{
if (serialPortFD <= 0)
return -1;
int timeoutMode = (*env)->GetIntField(env, obj, timeoutModeField);
jbyte *writeBuffer = (*env)->GetByteArrayElements(env, buffer, 0);
int numBytesWritten, result = 0;
// Set the DTR line to high if using RS-422
//ioctl(serialPortFD, TIOCMGET, &result);
//result |= TIOCM_DTR;
//ioctl(serialPortFD, TIOCMSET, &result);
// Write to port
do { numBytesWritten = write(serialPortFD, writeBuffer+offset, bytesToWrite); } while ((numBytesWritten < 0) && (errno == EINTR));
if (numBytesWritten == -1)
{
// Problem writing, allow others to open the port and close it ourselves
ioctl(serialPortFD, TIOCNXCL);
tcdrain(serialPortFD);
while ((close(serialPortFD) == -1) && (errno != EBADF));
serialPortFD = -1;
(*env)->SetLongField(env, obj, serialPortFdField, -1l);
(*env)->SetBooleanField(env, obj, isOpenedField, JNI_FALSE);
}
// Wait until all bytes were written in write-blocking mode
if ((timeoutMode & com_fazecast_jSerialComm_SerialPort_TIMEOUT_WRITE_BLOCKING) > 0)
tcdrain(serialPortFD);
// Clear the DTR line if using RS-422
//#ifdef TIOCSERGETLSR
//do
//{
//result = ioctl(serialPortFD, TIOCSERGETLSR);
//if (result != TIOCSER_TEMT)
//usleep(100);
//} while (result != TIOCSER_TEMT);
//#endif
//ioctl(serialPortFD, TIOCMGET, &result);
//result &= ~TIOCM_DTR;
//ioctl(serialPortFD, TIOCMSET, &result);
//do { result = tcflush(serialPortFD, TCIFLUSH); } while ((result < 0) && (errno == EINTR));
// Return number of bytes written if successful
(*env)->ReleaseByteArrayElements(env, buffer, writeBuffer, JNI_ABORT);
return numBytesWritten;
}
JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_setBreak(JNIEnv *env, jobject obj, jlong serialPortFD)
{
if (serialPortFD <= 0)
return JNI_FALSE;
return (ioctl(serialPortFD, TIOCSBRK) == 0);
}
JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_clearBreak(JNIEnv *env, jobject obj, jlong serialPortFD)
{
if (serialPortFD <= 0)
return JNI_FALSE;
return (ioctl(serialPortFD, TIOCCBRK) == 0);
}
JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_setRTS(JNIEnv *env, jobject obj, jlong serialPortFD)
{
if (serialPortFD <= 0)
return JNI_FALSE;
int modemBits = TIOCM_RTS;
return (ioctl(serialPortFD, TIOCMBIS, &modemBits) == 0);
}
JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_clearRTS(JNIEnv *env, jobject obj, jlong serialPortFD)
{
if (serialPortFD <= 0)
return JNI_FALSE;
int modemBits = TIOCM_RTS;
return (ioctl(serialPortFD, TIOCMBIC, &modemBits) == 0);
}
JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_presetRTS(JNIEnv *env, jobject obj)
{
jstring portNameJString = (jstring)(*env)->GetObjectField(env, obj, comPortField);
const char *portName = (*env)->GetStringUTFChars(env, portNameJString, NULL);
// Send a system command to preset the RTS mode of the serial port
char commandString[64];
sprintf(commandString, "stty -F %s hupcl", portName);
int result = system(commandString);
(*env)->ReleaseStringUTFChars(env, portNameJString, portName);
return (result == 0);
}
JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_preclearRTS(JNIEnv *env, jobject obj)
{
jstring portNameJString = (jstring)(*env)->GetObjectField(env, obj, comPortField);
const char *portName = (*env)->GetStringUTFChars(env, portNameJString, NULL);
// Send a system command to preset the RTS mode of the serial port
char commandString[64];
sprintf(commandString, "stty -F %s -hupcl", portName);
int result = system(commandString);
(*env)->ReleaseStringUTFChars(env, portNameJString, portName);
return (result == 0);
}
JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_setDTR(JNIEnv *env, jobject obj, jlong serialPortFD)
{
if (serialPortFD <= 0)
return JNI_FALSE;
int modemBits = TIOCM_DTR;
return (ioctl(serialPortFD, TIOCMBIS, &modemBits) == 0);
}
JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_clearDTR(JNIEnv *env, jobject obj, jlong serialPortFD)
{
if (serialPortFD <= 0)
return JNI_FALSE;
int modemBits = TIOCM_DTR;
return (ioctl(serialPortFD, TIOCMBIC, &modemBits) == 0);
}
JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_presetDTR(JNIEnv *env, jobject obj)
{
jstring portNameJString = (jstring)(*env)->GetObjectField(env, obj, comPortField);
const char *portName = (*env)->GetStringUTFChars(env, portNameJString, NULL);
// Send a system command to preset the DTR mode of the serial port
char commandString[64];
sprintf(commandString, "stty -F %s hupcl", portName);
int result = system(commandString);
(*env)->ReleaseStringUTFChars(env, portNameJString, portName);
return (result == 0);
}
JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_preclearDTR(JNIEnv *env, jobject obj)
{
jstring portNameJString = (jstring)(*env)->GetObjectField(env, obj, comPortField);
const char *portName = (*env)->GetStringUTFChars(env, portNameJString, NULL);
// Send a system command to preset the DTR mode of the serial port
char commandString[64];
sprintf(commandString, "stty -F %s -hupcl", portName);
int result = system(commandString);
(*env)->ReleaseStringUTFChars(env, portNameJString, portName);
return (result == 0);
}
JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_getCTS(JNIEnv *env, jobject obj, jlong serialPortFD)
{
if (serialPortFD <= 0)
return JNI_FALSE;
int modemBits = 0;
return (ioctl(serialPortFD, TIOCMGET, &modemBits) == 0) && (modemBits & TIOCM_CTS);
}
JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_getDSR(JNIEnv *env, jobject obj, jlong serialPortFD)
{
if (serialPortFD <= 0)
return JNI_FALSE;
int modemBits = 0;
return (ioctl(serialPortFD, TIOCMGET, &modemBits) == 0) && (modemBits & TIOCM_LE);
}
#endif

View File

@ -1,79 +0,0 @@
# OSX specific library variables
COMPILE := gcc
LINK := gcc
CFLAGS :=
LDFLAGS := -dynamiclib
JDK_HOME = `/usr/libexec/java_home`
INCLUDES := -I"$(JDK_HOME)/include" -I"$(JDK_HOME)/include/darwin"
LIBRARIES := -framework Cocoa -framework IOKit -framework JavaVM
DELETE := @rm
MKDIR := @mkdir
COPY := @cp
MOVE := @mv
PRINT := @echo
FULL_CLASS := com.fazecast.jSerialComm.SerialPort
JAVAC := javac
JAVAH := javah -jni
JFLAGS := -source 1.6 -target 1.6 -Xlint:-options
LIBRARY_NAME := libjSerialComm.jnilib
JAVA_SOURCE_DIR = ../../../../src/main/java/com/fazecast/jSerialComm
RESOURCE_DIR = ../../../../src/main/resources/OSX
BUILD_DIR = ../../../../bin/OSX
JAVA_CLASS_DIR = $(BUILD_DIR)/../com/fazecast/jSerialComm
OBJECTSx86 = $(BUILD_DIR)/x86/SerialPort_OSX.o $(BUILD_DIR)/x86/OSXHelperFunctions.o
OBJECTSx86_64 = $(BUILD_DIR)/x86_64/SerialPort_OSX.o $(BUILD_DIR)/x86_64/OSXHelperFunctions.o
JNI_HEADER = ../com_fazecast_jSerialComm_SerialPort.h
JAVA_CLASS = $(JAVA_CLASS_DIR)/SerialPort.class
# Define phony and suffix rules
.PHONY: all osx32 osx64 checkdirs
.SUFFIXES:
.SUFFIXES: .cpp .c .o .class .java .h
# Default build target builds both 32 and 64 bit libraries
all : osx32 osx64
$(DELETE) -rf $(BUILD_DIR)
$(DELETE) -rf ../*.h
# Builds 32-bit OSX libraries
osx32 : ARCH = -m32
osx32 : checkdirs $(BUILD_DIR)/x86/$(LIBRARY_NAME)
$(DELETE) -rf $(BUILD_DIR)/x86/*.o
$(COPY) $(BUILD_DIR)/x86/*.* $(RESOURCE_DIR)/x86
# Builds 64-bit OSX libraries
osx64 : ARCH = -m64
osx64 : checkdirs $(BUILD_DIR)/x86_64/$(LIBRARY_NAME)
$(DELETE) -rf $(BUILD_DIR)/x86_64/*.o
$(COPY) $(BUILD_DIR)/x86_64/*.* $(RESOURCE_DIR)/x86_64
# Rule to create build directories
checkdirs : $(BUILD_DIR)/x86 $(BUILD_DIR)/x86_64 $(JAVA_CLASS_DIR)
$(BUILD_DIR)/x86 :
$(MKDIR) -p $@
$(BUILD_DIR)/x86_64 :
$(MKDIR) -p $@
$(JAVA_CLASS_DIR) :
$(MKDIR) -p $@
# Rule to build 32-bit library
$(BUILD_DIR)/x86/$(LIBRARY_NAME) : $(JNI_HEADER) $(OBJECTSx86)
$(LINK) $(LDFLAGS) $(ARCH) -o $@ $(OBJECTSx86) $(LIBRARIES)
# Rule to build 64-bit library
$(BUILD_DIR)/x86_64/$(LIBRARY_NAME) : $(JNI_HEADER) $(OBJECTSx86_64)
$(LINK) $(LDFLAGS) $(ARCH) -o $@ $(OBJECTSx86_64) $(LIBRARIES)
# Suffix rules to get from *.c -> *.o
$(BUILD_DIR)/x86/%.o : %.c
$(COMPILE) $(INCLUDES) $(CFLAGS) $(ARCH) -c $< -o $@
$(BUILD_DIR)/x86_64/%.o : %.c
$(COMPILE) $(INCLUDES) $(CFLAGS) $(ARCH) -c $< -o $@
# Rule to build JNI header file
$(JNI_HEADER) : $(JAVA_CLASS)
$(JAVAH) -d .. -classpath $(JAVA_CLASS_DIR)/../../.. $(FULL_CLASS)
# Suffix rule to get from *.java -> *.class
$(JAVA_CLASS) :
$(JAVAC) $(JFLAGS) -d $(JAVA_CLASS_DIR)/../../.. -cp $(JAVA_SOURCE_DIR)/../../.. $(JAVA_SOURCE_DIR)/$(basename $(notdir $@)).java

View File

@ -1,72 +0,0 @@
/*
* OSXHelperFunctions.c
*
* Created on: Jul 1, 2015
* Last Updated on: Mar 25, 2016
* Author: Will Hedgecock
*
* Copyright (C) 2012-2018 Fazecast, Inc.
*
* This file is part of jSerialComm.
*
* jSerialComm is free software: you can redistribute it and/or modify
* it under the terms of either the Apache Software License, version 2, or
* the GNU Lesser General Public License as published by the Free Software
* Foundation, version 3 or above.
*
* jSerialComm is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of both the GNU Lesser General Public
* License and the Apache Software License along with jSerialComm. If not,
* see <http://www.gnu.org/licenses/> and <http://www.apache.org/licenses/>.
*/
#ifdef __APPLE__
#include <sys/types.h>
#include <fcntl.h>
#include "OSXHelperFunctions.h"
speed_t getBaudRateCode(speed_t baudRate)
{
switch (baudRate)
{
case 50:
return B50;
case 75:
return B75;
case 110:
return B110;
case 134:
return B134;
case 150:
return B150;
case 200:
return B200;
case 300:
return B300;
case 600:
return B600;
case 1200:
return B1200;
case 1800:
return B1800;
case 2400:
return B2400;
case 4800:
return B4800;
case 9600:
return B9600;
case 19200:
return B19200;
case 38400:
return B38400;
default:
return 0;
}
return 0;
}
#endif

View File

@ -1,33 +0,0 @@
/*
* OSXHelperFunctions.h
*
* Created on: Jul 1, 2015
* Last Updated on: Mar 25, 2016
* Author: Will Hedgecock
*
* Copyright (C) 2012-2018 Fazecast, Inc.
*
* This file is part of jSerialComm.
*
* jSerialComm is free software: you can redistribute it and/or modify
* it under the terms of either the Apache Software License, version 2, or
* the GNU Lesser General Public License as published by the Free Software
* Foundation, version 3 or above.
*
* jSerialComm is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of both the GNU Lesser General Public
* License and the Apache Software License along with jSerialComm. If not,
* see <http://www.gnu.org/licenses/> and <http://www.apache.org/licenses/>.
*/
#ifndef __OSX_HELPER_FUNCTIONS_HEADER_H__
#define __OSX_HELPER_FUNCTIONS_HEADER_H__
#include <termios.h>
speed_t getBaudRateCode(speed_t baudRate);
#endif // #ifndef __OSX_HELPER_FUNCTIONS_HEADER_H__

265
src/main/c/Posix/Makefile Normal file
View File

@ -0,0 +1,265 @@
# Architecture-dependent library variables
COMPILE := gcc
COMPILE_ARM32 := arm32-unknown-linux-gnueabi-gcc
COMPILE_ARM32HF := arm32hf-unknown-linux-gnueabi-gcc
COMPILE_ARM64 := aarch64-unknown-linux-gnueabi-gcc
COMPILE_SOLARIS_X86 := x86_64-sun-solaris2.10-gcc
COMPILE_SOLARIS_SPARC := sparc-sun-solaris2.10-gcc
COMPILE_APPLE := /usr/local/bin/gcc-8
LINK := gcc
LINK_ARM32 := arm32-unknown-linux-gnueabi-gcc
LINK_ARM32HF := arm32hf-unknown-linux-gnueabi-gcc
LINK_ARM64 := aarch64-unknown-linux-gnueabi-gcc
LINK_SOLARIS_X86 := x86_64-sun-solaris2.10-gcc
LINK_SOLARIS_SPARC := sparc-sun-solaris2.10-gcc
LINK_APPLE := /usr/local/bin/gcc-8
CFLAGS := -fPIC -Os -flto -static-libgcc -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=0
CFLAGS_POSIX := -fuse-linker-plugin
CFLAGS_APPLE :=
LDFLAGS := -Os -flto -static-libgcc
LDFLAGS_POSIX := -shared -fuse-linker-plugin -s
LDFLAGS_APPLE := -dynamiclib
JDK_HOME := $(shell if [ "`uname`" = "Darwin" ]; then echo "`/usr/libexec/java_home`"; else echo "$$JDK_HOME"; fi)
INCLUDES := -I"$(JDK_HOME)/include" -I"$(JDK_HOME)/include/linux" -I"$(JDK_HOME)/include/darwin" -I"$(JDK_HOME)/include/solaris"
LIBRARIES_POSIX :=
LIBRARIES_APPLE := -framework Cocoa -framework IOKit -framework JavaVM
DELETE := @rm
MKDIR := @mkdir
COPY := @cp
MOVE := @mv
PRINT := @echo
FULL_CLASS := com.fazecast.jSerialComm.SerialPort
JAVAC := "$(JDK_HOME)/bin/javac"
JAVAH := "$(JDK_HOME)/bin/javah" -jni
JFLAGS := -source 1.6 -target 1.6 -Xlint:-options
LIBRARY_NAME_POSIX := libjSerialComm.so
LIBRARY_NAME_APPLE := libjSerialComm.jnilib
JAVA_SOURCE_DIR := ../../../../src/main/java/com/fazecast/jSerialComm
RESOURCE_DIR_LINUX := ../../../../src/main/resources/Linux
RESOURCE_DIR_SOLARIS := ../../../../src/main/resources/Solaris
RESOURCE_DIR_OSX := ../../../../src/main/resources/OSX
BUILD_DIR := ../../../../build
JAVA_CLASS_DIR := $(BUILD_DIR)/com/fazecast/jSerialComm
OBJECTSlinux32 := $(BUILD_DIR)/linux_x86/SerialPort_Posix.o $(BUILD_DIR)/linux_x86/PosixHelperFunctions.o
OBJECTSlinux64 := $(BUILD_DIR)/linux_x86_64/SerialPort_Posix.o $(BUILD_DIR)/linux_x86_64/PosixHelperFunctions.o
OBJECTSarmv5 := $(BUILD_DIR)/armv5/SerialPort_Posix.o $(BUILD_DIR)/armv5/PosixHelperFunctions.o
OBJECTSarmv6 := $(BUILD_DIR)/armv6/SerialPort_Posix.o $(BUILD_DIR)/armv6/PosixHelperFunctions.o
OBJECTSarmv6-hf := $(BUILD_DIR)/armv6-hf/SerialPort_Posix.o $(BUILD_DIR)/armv6-hf/PosixHelperFunctions.o
OBJECTSarmv7 := $(BUILD_DIR)/armv7/SerialPort_Posix.o $(BUILD_DIR)/armv7/PosixHelperFunctions.o
OBJECTSarmv7-hf := $(BUILD_DIR)/armv7-hf/SerialPort_Posix.o $(BUILD_DIR)/armv7-hf/PosixHelperFunctions.o
OBJECTSarmv8_32 := $(BUILD_DIR)/armv8_32/SerialPort_Posix.o $(BUILD_DIR)/armv8_32/PosixHelperFunctions.o
OBJECTSarmv8_64 := $(BUILD_DIR)/armv8_64/SerialPort_Posix.o $(BUILD_DIR)/armv8_64/PosixHelperFunctions.o
OBJECTSsolaris32 := $(BUILD_DIR)/solaris_x86/SerialPort_Posix.o $(BUILD_DIR)/solaris_x86/PosixHelperFunctions.o
OBJECTSsolaris64 := $(BUILD_DIR)/solaris_x86_64/SerialPort_Posix.o $(BUILD_DIR)/solaris_x86_64/PosixHelperFunctions.o
OBJECTSsolarisSparc32 := $(BUILD_DIR)/solaris_sparc32/SerialPort_Posix.o $(BUILD_DIR)/solaris_sparc32/PosixHelperFunctions.o
OBJECTSsolarisSparc64 := $(BUILD_DIR)/solaris_sparc64/SerialPort_Posix.o $(BUILD_DIR)/solaris_sparc64/PosixHelperFunctions.o
OBJECTSosx32 := $(BUILD_DIR)/osx_x86/SerialPort_Posix.o $(BUILD_DIR)/osx_x86/PosixHelperFunctions.o
OBJECTSosx64 := $(BUILD_DIR)/osx_x86_64/SerialPort_Posix.o $(BUILD_DIR)/osx_x86_64/PosixHelperFunctions.o
FLAGSlinux32 := -m32
FLAGSlinux64 := -m64
FLAGSarmv5 := -marm -mthumb-interwork -march=armv5 -mfloat-abi=soft
FLAGSarmv6 := -marm -mthumb-interwork -march=armv6 -mfloat-abi=soft
FLAGSarmv6hf := -marm -mthumb-interwork -march=armv6 -mfloat-abi=hard
FLAGSarmv7 := -mthumb -mthumb-interwork -march=armv7 -mfloat-abi=soft
FLAGSarmv7hf := -mthumb -mthumb-interwork -march=armv7 -mfloat-abi=hard
FLAGSarmv8_32 := -mthumb -mthumb-interwork -march=armv8-a
FLAGSarmv8_64 := -march=armv8-a
FLAGSsolaris32 := -m32
FLAGSsolaris64 := -m64
FLAGSsolarisSparc32 := -mno-app-regs -mcpu=v8 -mv8plus -m32
FLAGSsolarisSparc64 := -mno-app-regs -mcpu=v9 -m64
FLAGSosx32 := -m32
FLAGSosx64 := -m64
JNI_HEADER := ../com_fazecast_jSerialComm_SerialPort.h
JAVA_CLASS := $(JAVA_CLASS_DIR)/SerialPort.class
# Define phony and suffix rules
.PHONY: all clean linux linux32 linux64 arm armv5 armv6 armv6-hf armv7 armv7-hf armv8_32 armv8_64 solaris solaris32 solaris64 solarisSparc32 solarisSparc64 osx osx32 osx64
.SUFFIXES:
.SUFFIXES: .cpp .c .o .class .java .h
# Default build target does nothing
all :
$(PRINT) You must specify either linux, arm, solaris, or osx!
clean :
$(DELETE) -rf "$(BUILD_DIR)"
$(DELETE) -rf ../*.h
# Build all Linux libraries
linux : linux32 linux64
$(DELETE) -rf "`readlink -f $(BUILD_DIR)`"
$(DELETE) -rf ../*.h
# Build all ARM libraries
arm : armv5 armv6 armv6-hf armv7 armv7-hf armv8_32 armv8_64
$(DELETE) -rf "`readlink -f $(BUILD_DIR)`"
$(DELETE) -rf ../*.h
# Build all Solaris libraries
solaris : solaris32 solaris64 solarisSparc32 solarisSparc64
$(DELETE) -rf "`readlink -f $(BUILD_DIR)`"
$(DELETE) -rf ../*.h
# Build all OSX libraries
osx : osx64
$(DELETE) -rf "$(BUILD_DIR)"
$(DELETE) -rf ../*.h
# Build architecture-specific Linux libraries
linux32 : $(JAVA_CLASS_DIR) $(BUILD_DIR)/linux_x86 $(BUILD_DIR)/linux_x86/$(LIBRARY_NAME_POSIX)
$(DELETE) -rf $(BUILD_DIR)/linux_x86/*.o
$(COPY) $(BUILD_DIR)/linux_x86/*.* $(RESOURCE_DIR_LINUX)/x86/
linux64 : $(JAVA_CLASS_DIR) $(BUILD_DIR)/linux_x86_64 $(BUILD_DIR)/linux_x86_64/$(LIBRARY_NAME_POSIX)
$(DELETE) -rf $(BUILD_DIR)/linux_x86_64/*.o
$(COPY) $(BUILD_DIR)/linux_x86_64/*.* $(RESOURCE_DIR_LINUX)/x86_64/
# Build architecture-specific ARM libraries
armv5 : $(JAVA_CLASS_DIR) $(BUILD_DIR)/armv5 $(BUILD_DIR)/armv5/$(LIBRARY_NAME_POSIX)
$(DELETE) -rf $(BUILD_DIR)/armv5/*.o
$(COPY) $(BUILD_DIR)/armv5/*.* $(RESOURCE_DIR_LINUX)/armv5/
armv6 : $(JAVA_CLASS_DIR) $(BUILD_DIR)/armv6 $(BUILD_DIR)/armv6/$(LIBRARY_NAME_POSIX)
$(DELETE) -rf $(BUILD_DIR)/armv6/*.o
$(COPY) $(BUILD_DIR)/armv6/*.* $(RESOURCE_DIR_LINUX)/armv6/
armv6-hf : $(JAVA_CLASS_DIR) $(BUILD_DIR)/armv6-hf $(BUILD_DIR)/armv6-hf/$(LIBRARY_NAME_POSIX)
$(DELETE) -rf $(BUILD_DIR)/armv6-hf/*.o
$(COPY) $(BUILD_DIR)/armv6-hf/*.* $(RESOURCE_DIR_LINUX)/armv6-hf/
armv7 : $(JAVA_CLASS_DIR) $(BUILD_DIR)/armv7 $(BUILD_DIR)/armv7/$(LIBRARY_NAME_POSIX)
$(DELETE) -rf $(BUILD_DIR)/armv7/*.o
$(COPY) $(BUILD_DIR)/armv7/*.* $(RESOURCE_DIR_LINUX)/armv7/
armv7-hf : $(JAVA_CLASS_DIR) $(BUILD_DIR)/armv7-hf $(BUILD_DIR)/armv7-hf/$(LIBRARY_NAME_POSIX)
$(DELETE) -rf $(BUILD_DIR)/armv7-hf/*.o
$(COPY) $(BUILD_DIR)/armv7-hf/*.* $(RESOURCE_DIR_LINUX)/armv7-hf/
armv8_32 : $(JAVA_CLASS_DIR) $(BUILD_DIR)/armv8_32 $(BUILD_DIR)/armv8_32/$(LIBRARY_NAME_POSIX)
$(DELETE) -rf $(BUILD_DIR)/armv8_32/*.o
$(COPY) $(BUILD_DIR)/armv8_32/*.* $(RESOURCE_DIR_LINUX)/armv8_32/
armv8_64 : $(JAVA_CLASS_DIR) $(BUILD_DIR)/armv8_64 $(BUILD_DIR)/armv8_64/$(LIBRARY_NAME_POSIX)
$(DELETE) -rf $(BUILD_DIR)/armv8_64/*.o
$(COPY) $(BUILD_DIR)/armv8_64/*.* $(RESOURCE_DIR_LINUX)/armv8_64/
# Build architecture-specific Solaris libraries
solaris32 : $(JAVA_CLASS_DIR) $(BUILD_DIR)/solaris_x86 $(BUILD_DIR)/solaris_x86/$(LIBRARY_NAME_POSIX)
$(DELETE) -rf $(BUILD_DIR)/solaris_x86/*.o
$(COPY) $(BUILD_DIR)/solaris_x86/*.* $(RESOURCE_DIR_SOLARIS)/x86/
solaris64 : $(JAVA_CLASS_DIR) $(BUILD_DIR)/solaris_x86_64 $(BUILD_DIR)/solaris_x86_64/$(LIBRARY_NAME_POSIX)
$(DELETE) -rf $(BUILD_DIR)/solaris_x86_64/*.o
$(COPY) $(BUILD_DIR)/solaris_x86_64/*.* $(RESOURCE_DIR_SOLARIS)/x86_64/
solarisSparc32 : $(JAVA_CLASS_DIR) $(BUILD_DIR)/solaris_sparc32 $(BUILD_DIR)/solaris_sparc32/$(LIBRARY_NAME_POSIX)
$(DELETE) -rf $(BUILD_DIR)/solaris_sparc32/*.o
$(COPY) $(BUILD_DIR)/solaris_sparc32/*.* $(RESOURCE_DIR_SOLARIS)/sparcv8plus_32/
solarisSparc64 : $(JAVA_CLASS_DIR) $(BUILD_DIR)/solaris_sparc64 $(BUILD_DIR)/solaris_sparc64/$(LIBRARY_NAME_POSIX)
$(DELETE) -rf $(BUILD_DIR)/solaris_sparc64/*.o
$(COPY) $(BUILD_DIR)/solaris_sparc64/*.* $(RESOURCE_DIR_SOLARIS)/sparcv9_64/
# Build architecture-specific OSX libraries
osx32 : $(JAVA_CLASS_DIR) $(BUILD_DIR)/osx_x86 $(BUILD_DIR)/osx_x86/$(LIBRARY_NAME_APPLE)
$(DELETE) -rf $(BUILD_DIR)/osx_x86/*.o
$(COPY) $(BUILD_DIR)/osx_x86/*.* $(RESOURCE_DIR_OSX)/x86
osx64 : $(JAVA_CLASS_DIR) $(BUILD_DIR)/osx_x86_64 $(BUILD_DIR)/osx_x86_64/$(LIBRARY_NAME_APPLE)
$(DELETE) -rf $(BUILD_DIR)/osx_x86_64/*.o
$(COPY) $(BUILD_DIR)/osx_x86_64/*.* $(RESOURCE_DIR_OSX)/x86_64
# Rule to create build directories
$(BUILD_DIR)/linux_x86 :
$(MKDIR) -p $@
$(BUILD_DIR)/linux_x86_64 :
$(MKDIR) -p $@
$(BUILD_DIR)/armv5 :
$(MKDIR) -p $@
$(BUILD_DIR)/armv6 :
$(MKDIR) -p $@
$(BUILD_DIR)/armv6-hf :
$(MKDIR) -p $@
$(BUILD_DIR)/armv7 :
$(MKDIR) -p $@
$(BUILD_DIR)/armv7-hf :
$(MKDIR) -p $@
$(BUILD_DIR)/armv8_32 :
$(MKDIR) -p $@
$(BUILD_DIR)/armv8_64 :
$(MKDIR) -p $@
$(BUILD_DIR)/solaris_x86 :
$(MKDIR) -p $@
$(BUILD_DIR)/solaris_x86_64 :
$(MKDIR) -p $@
$(BUILD_DIR)/solaris_sparc32 :
$(MKDIR) -p $@
$(BUILD_DIR)/solaris_sparc64 :
$(MKDIR) -p $@
$(BUILD_DIR)/osx_x86 :
$(MKDIR) -p $@
$(BUILD_DIR)/osx_x86_64 :
$(MKDIR) -p $@
$(JAVA_CLASS_DIR) :
$(MKDIR) -p $@
# Build rules for all libraries
$(BUILD_DIR)/linux_x86/$(LIBRARY_NAME_POSIX) : $(JNI_HEADER) $(OBJECTSlinux32)
$(LINK) $(LDFLAGS) $(LDFLAGS_POSIX) $(FLAGSlinux32) -o $@ $(OBJECTSlinux32) $(LIBRARIES_POSIX)
$(BUILD_DIR)/linux_x86_64/$(LIBRARY_NAME_POSIX) : $(JNI_HEADER) $(OBJECTSlinux64)
$(LINK) $(LDFLAGS) $(LDFLAGS_POSIX) $(FLAGSlinux64) -o $@ $(OBJECTSlinux64) $(LIBRARIES_POSIX)
$(BUILD_DIR)/armv5/$(LIBRARY_NAME_POSIX) : $(JNI_HEADER) $(OBJECTSarmv5)
$(LINK_ARM32) $(LDFLAGS) $(LDFLAGS_POSIX) $(FLAGSarmv5) -o $@ $(OBJECTSarmv5) $(LIBRARIES_POSIX)
$(BUILD_DIR)/armv6/$(LIBRARY_NAME_POSIX) : $(JNI_HEADER) $(OBJECTSarmv6)
$(LINK_ARM32) $(LDFLAGS) $(LDFLAGS_POSIX) $(FLAGSarmv6) -o $@ $(OBJECTSarmv6) $(LIBRARIES_POSIX)
$(BUILD_DIR)/armv6-hf/$(LIBRARY_NAME_POSIX) : $(JNI_HEADER) $(OBJECTSarmv6-hf)
$(LINK_ARM32HF) $(LDFLAGS) $(LDFLAGS_POSIX) $(FLAGSarmv6-hf) -o $@ $(OBJECTSarmv6-hf) $(LIBRARIES_POSIX)
$(BUILD_DIR)/armv7/$(LIBRARY_NAME_POSIX) : $(JNI_HEADER) $(OBJECTSarmv7)
$(LINK_ARM32) $(LDFLAGS) $(LDFLAGS_POSIX) $(FLAGSarmv7) -o $@ $(OBJECTSarmv7) $(LIBRARIES_POSIX)
$(BUILD_DIR)/armv7-hf/$(LIBRARY_NAME_POSIX) : $(JNI_HEADER) $(OBJECTSarmv7-hf)
$(LINK_ARM32HF) $(LDFLAGS) $(LDFLAGS_POSIX) $(FLAGSarmv7-hf) -o $@ $(OBJECTSarmv7-hf) $(LIBRARIES_POSIX)
$(BUILD_DIR)/armv8_32/$(LIBRARY_NAME_POSIX) : $(JNI_HEADER) $(OBJECTSarmv8_32)
$(LINK_ARM32) $(LDFLAGS) $(LDFLAGS_POSIX) $(FLAGSarmv8_32) -o $@ $(OBJECTSarmv8_32) $(LIBRARIES_POSIX)
$(BUILD_DIR)/armv8_64/$(LIBRARY_NAME_POSIX) : $(JNI_HEADER) $(OBJECTSarmv8_64)
$(LINK_ARM64) $(LDFLAGS) $(LDFLAGS_POSIX) $(FLAGSarmv8_64) -o $@ $(OBJECTSarmv8_64) $(LIBRARIES_POSIX)
$(BUILD_DIR)/solaris_x86/$(LIBRARY_NAME_POSIX) : $(JNI_HEADER) $(OBJECTSsolaris32)
$(LINK_SOLARIS_X86) $(LDFLAGS) $(LDFLAGS_POSIX) $(FLAGSsolaris32) -o $@ $(OBJECTSsolaris32) $(LIBRARIES_POSIX)
$(BUILD_DIR)/solaris_x86_64/$(LIBRARY_NAME_POSIX) : $(JNI_HEADER) $(OBJECTSsolaris64)
$(LINK_SOLARIS_X86) $(LDFLAGS) $(LDFLAGS_POSIX) $(FLAGSsolaris64) -o $@ $(OBJECTSsolaris64) $(LIBRARIES_POSIX)
$(BUILD_DIR)/solaris_sparc32/$(LIBRARY_NAME_POSIX) : $(JNI_HEADER) $(OBJECTSsolarisSparc32)
$(LINK_SOLARIS_SPARC) $(LDFLAGS) $(LDFLAGS_POSIX) $(FLAGSsolarisSparc32) -o $@ $(OBJECTSsolarisSparc32) $(LIBRARIES_POSIX)
$(BUILD_DIR)/solaris_sparc64/$(LIBRARY_NAME_POSIX) : $(JNI_HEADER) $(OBJECTSsolarisSparc64)
$(LINK_SOLARIS_SPARC) $(LDFLAGS) $(LDFLAGS_POSIX) $(FLAGSsolarisSparc64) -o $@ $(OBJECTSsolarisSparc64) $(LIBRARIES_POSIX)
$(BUILD_DIR)/osx_x86/$(LIBRARY_NAME_APPLE) : $(JNI_HEADER) $(OBJECTSosx32)
$(LINK_APPLE) $(LDFLAGS) $(LDFLAGS_APPLE) $(FLAGSosx32) -o $@ $(OBJECTSosx32) $(LIBRARIES_APPLE)
$(BUILD_DIR)/osx_x86_64/$(LIBRARY_NAME_APPLE) : $(JNI_HEADER) $(OBJECTSosx64)
$(LINK_APPLE) $(LDFLAGS) $(LDFLAGS_APPLE) $(FLAGSosx64) -o $@ $(OBJECTSosx64) $(LIBRARIES_APPLE)
# Suffix rules to get from *.c -> *.o
$(BUILD_DIR)/linux_x86/%.o : %.c
$(COMPILE) $(INCLUDES) $(CFLAGS) $(CFLAGS_POSIX) $(FLAGSlinux32) -c $< -o $@
$(BUILD_DIR)/linux_x86_64/%.o : %.c
$(COMPILE) $(INCLUDES) $(CFLAGS) $(CFLAGS_POSIX) $(FLAGSlinux64) -c $< -o $@
$(BUILD_DIR)/armv5/%.o : %.c
$(COMPILE_ARM32) $(INCLUDES) $(CFLAGS) $(CFLAGS_POSIX) $(FLAGSarmv5) -c $< -o $@
$(BUILD_DIR)/armv6/%.o : %.c
$(COMPILE_ARM32) $(INCLUDES) $(CFLAGS) $(CFLAGS_POSIX) $(FLAGSarmv6) -c $< -o $@
$(BUILD_DIR)/armv6-hf/%.o : %.c
$(COMPILE_ARM32HF) $(INCLUDES) $(CFLAGS) $(CFLAGS_POSIX) $(FLAGSarmv6-hf) -c $< -o $@
$(BUILD_DIR)/armv7/%.o : %.c
$(COMPILE_ARM32) $(INCLUDES) $(CFLAGS) $(CFLAGS_POSIX) $(FLAGSarmv7) -c $< -o $@
$(BUILD_DIR)/armv7-hf/%.o : %.c
$(COMPILE_ARM32HF) $(INCLUDES) $(CFLAGS) $(CFLAGS_POSIX) $(FLAGSarmv7-hf) -c $< -o $@
$(BUILD_DIR)/armv8_32/%.o : %.c
$(COMPILE_ARM32) $(INCLUDES) $(CFLAGS) $(CFLAGS_POSIX) $(FLAGSarmv8_32) -c $< -o $@
$(BUILD_DIR)/armv8_64/%.o : %.c
$(COMPILE_ARM64) $(INCLUDES) $(CFLAGS) $(CFLAGS_POSIX) $(FLAGSarmv8_64) -c $< -o $@
$(BUILD_DIR)/solaris_x86/%.o : %.c
$(COMPILE_SOLARIS_X86) $(INCLUDES) $(CFLAGS) $(CFLAGS_POSIX) $(FLAGSsolaris32) -c $< -o $@
$(BUILD_DIR)/solaris_x86_64/%.o : %.c
$(COMPILE_SOLARIS_X86) $(INCLUDES) $(CFLAGS) $(CFLAGS_POSIX) $(FLAGSsolaris64) -c $< -o $@
$(BUILD_DIR)/solaris_sparc32/%.o : %.c
$(COMPILE_SOLARIS_SPARC) $(INCLUDES) $(CFLAGS) $(CFLAGS_POSIX) $(FLAGSsolarisSparc32) -c $< -o $@
$(BUILD_DIR)/solaris_sparc64/%.o : %.c
$(COMPILE_SOLARIS_SPARC) $(INCLUDES) $(CFLAGS) $(CFLAGS_POSIX) $(FLAGSsolarisSparc64) -c $< -o $@
$(BUILD_DIR)/osx_x86/%.o : %.c
$(COMPILE_APPLE) $(INCLUDES) $(CFLAGS) $(CFLAGS_APPLE) $(FLAGSosx32) -c $< -o $@
$(BUILD_DIR)/osx_x86_64/%.o : %.c
$(COMPILE_APPLE) $(INCLUDES) $(CFLAGS) $(CFLAGS_APPLE) $(FLAGSosx64) -c $< -o $@
# Rule to build JNI header file
$(JNI_HEADER) : $(JAVA_CLASS)
$(JAVAH) -d .. -classpath $(JAVA_CLASS_DIR)/../../.. $(FULL_CLASS)
# Suffix rule to get from *.java -> *.class
$(JAVA_CLASS) :
$(JAVAC) $(JFLAGS) -d $(JAVA_CLASS_DIR)/../../.. -cp $(JAVA_SOURCE_DIR)/../../.. $(JAVA_SOURCE_DIR)/$(basename $(notdir $@)).java

View File

@ -1,8 +1,8 @@
/*
* LinuxHelperFunctions.c
* PosixHelperFunctions.c
*
* Created on: Mar 10, 2015
* Last Updated on: Nov 01, 2018
* Last Updated on: Nov 12, 2018
* Author: Will Hedgecock
*
* Copyright (C) 2012-2018 Fazecast, Inc.
@ -23,23 +23,17 @@
* see <http://www.gnu.org/licenses/> and <http://www.apache.org/licenses/>.
*/
#ifdef __linux__
#include <dirent.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <linux/serial.h>
#include <dirent.h>
#include <asm/termios.h>
#include <asm/ioctls.h>
#include <sys/types.h>
#include <unistd.h>
#include "LinuxHelperFunctions.h"
// Explicitly define the ioctl function signature
extern int ioctl(int __fd, unsigned long int __request, ...);
#include "PosixHelperFunctions.h"
// Common functionality
void push_back(struct charTupleVector* vector, const char* firstString, const char* secondString, const char* thirdString)
{
// Allocate memory for new string storage
@ -72,6 +66,45 @@ char keyExists(struct charTupleVector* vector, const char* key)
return 0;
}
// Linux-specific functionality
#if defined(__linux__)
#include <linux/serial.h>
#include <asm/termios.h>
#include <asm/ioctls.h>
void getDriverName(const char* directoryToSearch, char* friendlyName)
{
friendlyName[0] = '\0';
// Open the directory
DIR *directoryIterator = opendir(directoryToSearch);
if (!directoryIterator)
return;
// Read all sub-directories in the current directory
struct dirent *directoryEntry = readdir(directoryIterator);
while (directoryEntry)
{
// Check if entry is a valid sub-directory
if (directoryEntry->d_name[0] != '.')
{
// Get the readable part of the driver name
strcpy(friendlyName, "USB-to-Serial Port (");
char *startingPoint = strchr(directoryEntry->d_name, ':');
if (startingPoint != NULL)
strcat(friendlyName, startingPoint+1);
else
strcat(friendlyName, directoryEntry->d_name);
strcat(friendlyName, ")");
break;
}
directoryEntry = readdir(directoryIterator);
}
// Close the directory
closedir(directoryIterator);
}
void getFriendlyName(const char* productFile, char* friendlyName)
{
int friendlyNameLength = 0;
@ -110,39 +143,6 @@ void getInterfaceDescription(const char* interfaceFile, char* interfaceDescripti
}
}
void getDriverName(const char* directoryToSearch, char* friendlyName)
{
friendlyName[0] = '\0';
// Open the directory
DIR *directoryIterator = opendir(directoryToSearch);
if (!directoryIterator)
return;
// Read all sub-directories in the current directory
struct dirent *directoryEntry = readdir(directoryIterator);
while (directoryEntry)
{
// Check if entry is a valid sub-directory
if (directoryEntry->d_name[0] != '.')
{
// Get the readable part of the driver name
strcpy(friendlyName, "USB-to-Serial Port (");
char *startingPoint = strchr(directoryEntry->d_name, ':');
if (startingPoint != NULL)
strcat(friendlyName, startingPoint+1);
else
strcat(friendlyName, directoryEntry->d_name);
strcat(friendlyName, ")");
break;
}
directoryEntry = readdir(directoryIterator);
}
// Close the directory
closedir(directoryIterator);
}
void recursiveSearchForComPorts(charTupleVector* comPorts, const char* fullPathToSearch)
{
// Open the directory
@ -414,8 +414,9 @@ void driverBasedSearchForComPorts(charTupleVector* comPorts)
}
}
unsigned int getBaudRateCode(int baudRate)
baud_rate getBaudRateCode(baud_rate baudRate)
{
// Translate a raw baud rate into a system-specified one
switch (baudRate)
{
case 50:
@ -497,7 +498,7 @@ unsigned int getBaudRateCode(int baudRate)
return 0;
}
void setBaudRate(int portFD, int baudRate)
int setBaudRateCustom(int portFD, baud_rate baudRate)
{
#ifdef TCSETS2
struct termios2 options = { 0 };
@ -506,7 +507,7 @@ void setBaudRate(int portFD, int baudRate)
options.c_cflag |= BOTHER;
options.c_ispeed = baudRate;
options.c_ospeed = baudRate;
ioctl(portFD, TCSETS2, &options);
int retVal = ioctl(portFD, TCSETS2, &options);
#else
struct serial_struct serInfo;
ioctl(portFD, TIOCGSERIAL, &serInfo);
@ -515,8 +516,216 @@ void setBaudRate(int portFD, int baudRate)
serInfo.custom_divisor = serInfo.baud_base / baudRate;
if (sersInfo.custom_divisor == 0)
serInfo.custom_divisor = 1;
ioctl(portFD, TIOCSSERIAL, &serInfo);
int retVal = ioctl(portFD, TIOCSSERIAL, &serInfo);
#endif
return (retVal == 0);
}
// Solaris-specific functionality
#elif defined(__sun__)
#include <termios.h>
void searchForComPorts(charTupleVector* comPorts)
{
// Open the Solaris callout dev directory
DIR *directoryIterator = opendir("/dev/cua/");
if (directoryIterator)
{
// Read all files in the current directory
struct dirent *directoryEntry = readdir(directoryIterator);
while (directoryEntry)
{
// See if the file names a potential serial port
if ((strlen(directoryEntry->d_name) >= 1) && (directoryEntry->d_name[0] != '.'))
{
// Determine system name of port
char* systemName = (char*)malloc(256);
strcpy(systemName, "/dev/cua/");
strcat(systemName, directoryEntry->d_name);
// Set static friendly name
char* friendlyName = (char*)malloc(256);
strcpy(friendlyName, "Serial Port");
// Ensure that the file is not a directory
struct stat fileStats;
stat(systemName, &fileStats);
if (!S_ISDIR(fileStats.st_mode))
{
// Determine if port is already in the list, and add it if not
if (!keyExists(comPorts, systemName))
push_back(comPorts, systemName, friendlyName, friendlyName);
}
// Clean up memory
free(systemName);
free(friendlyName);
}
directoryEntry = readdir(directoryIterator);
}
// Close the directory
closedir(directoryIterator);
}
// Open the Solaris dial-in dev directory
directoryIterator = opendir("/dev/term/");
if (directoryIterator)
{
// Read all files in the current directory
struct dirent *directoryEntry = readdir(directoryIterator);
while (directoryEntry)
{
// See if the file names a potential serial port
if ((strlen(directoryEntry->d_name) >= 1) && (directoryEntry->d_name[0] != '.'))
{
// Determine system name of port
char* systemName = (char*)malloc(256);
strcpy(systemName, "/dev/term/");
strcat(systemName, directoryEntry->d_name);
// Set static friendly name
char* friendlyName = (char*)malloc(256);
strcpy(friendlyName, "Serial Port (Dial-In)");
// Ensure that the file is not a directory
struct stat fileStats;
stat(systemName, &fileStats);
if (!S_ISDIR(fileStats.st_mode))
{
// Determine if port is already in the list, and add it if not
if (!keyExists(comPorts, systemName))
push_back(comPorts, systemName, friendlyName, friendlyName);
}
// Clean up memory
free(systemName);
free(friendlyName);
}
directoryEntry = readdir(directoryIterator);
}
// Close the directory
closedir(directoryIterator);
}
}
baud_rate getBaudRateCode(baud_rate baudRate)
{
// Translate a raw baud rate into a system-specified one
switch (baudRate)
{
case 50:
return B50;
case 75:
return B75;
case 110:
return B110;
case 134:
return B134;
case 150:
return B150;
case 200:
return B200;
case 300:
return B300;
case 600:
return B600;
case 1200:
return B1200;
case 1800:
return B1800;
case 2400:
return B2400;
case 4800:
return B4800;
case 9600:
return B9600;
case 19200:
return B19200;
case 38400:
return B38400;
case 57600:
return B57600;
case 76800:
return B76800;
case 115200:
return B115200;
case 153600:
return B153600;
case 230400:
return B230400;
case 307200:
return B307200;
case 460800:
return B460800;
default:
return 0;
}
return 0;
}
int setBaudRateCustom(int portFD, baud_rate baudRate)
{
// No way to set custom baud rates on this OS
return 0;
}
// Apple-specific functionality
#elif defined(__APPLE__)
#include <sys/ioctl.h>
#include <IOKit/serial/ioss.h>
baud_rate getBaudRateCode(baud_rate baudRate)
{
// Translate a raw baud rate into a system-specified one
switch (baudRate)
{
case 50:
return B50;
case 75:
return B75;
case 110:
return B110;
case 134:
return B134;
case 150:
return B150;
case 200:
return B200;
case 300:
return B300;
case 600:
return B600;
case 1200:
return B1200;
case 1800:
return B1800;
case 2400:
return B2400;
case 4800:
return B4800;
case 9600:
return B9600;
case 19200:
return B19200;
case 38400:
return B38400;
default:
return 0;
}
return 0;
}
int setBaudRateCustom(int portFD, baud_rate baudRate)
{
// Use OSX-specific ioctls to set a custom baud rate
unsigned long microseconds = 1000;
int retVal = ioctl(portFD, IOSSIOSPEED, &baudRate);
ioctl(portFD, IOSSDATALAT, &microseconds);
return (retVal == 0);
}
#endif

View File

@ -1,8 +1,8 @@
/*
* LinuxHelperFunctions.h
* PosixHelperFunctions.h
*
* Created on: Mar 10, 2015
* Last Updated on: Nov 01, 2018
* Last Updated on: Nov 12, 2018
* Author: Will Hedgecock
*
* Copyright (C) 2012-2018 Fazecast, Inc.
@ -23,9 +23,10 @@
* see <http://www.gnu.org/licenses/> and <http://www.apache.org/licenses/>.
*/
#ifndef __LINUX_HELPER_FUNCTIONS_HEADER_H__
#define __LINUX_HELPER_FUNCTIONS_HEADER_H__
#ifndef __POSIX_HELPER_FUNCTIONS_HEADER_H__
#define __POSIX_HELPER_FUNCTIONS_HEADER_H__
// Common functionality
typedef struct charTupleVector
{
char **first, **second, **third;
@ -34,13 +35,37 @@ typedef struct charTupleVector
void push_back(struct charTupleVector* vector, const char* firstString, const char* secondString, const char* thirdString);
char keyExists(struct charTupleVector* vector, const char* key);
// Forced definitions
#ifndef CMSPAR
#define CMSPAR 010000000000
#endif
// Linux-specific functionality
#if defined(__linux__)
typedef int baud_rate;
extern int ioctl(int __fd, unsigned long int __request, ...);
void getDriverName(const char* directoryToSearch, char* friendlyName);
void getFriendlyName(const char* productFile, char* friendlyName);
void getInterfaceDescription(const char* interfaceFile, char* interfaceDescription);
void recursiveSearchForComPorts(charTupleVector* comPorts, const char* fullPathToSearch);
void lastDitchSearchForComPorts(charTupleVector* comPorts);
void driverBasedSearchForComPorts(charTupleVector* comPorts);
void getFriendlyName(const char* productFile, char* friendlyName);
void getInterfaceDescription(const char* interfaceFile, char* interfaceDescription);
unsigned int getBaudRateCode(int baudRate);
void setBaudRate(int portFD, int baudRate);
#endif // #ifndef __LINUX_HELPER_FUNCTIONS_HEADER_H__
// Solaris-specific functionality
#elif defined(__sun__)
typedef int baud_rate;
extern int ioctl(int __fd, int __request, ...);
void searchForComPorts(charTupleVector* comPorts);
// Apple-specific functionality
#elif defined(__APPLE__)
#include <termios.h>
typedef speed_t baud_rate;
#endif
// Common baud rate functionality
baud_rate getBaudRateCode(baud_rate baudRate);
int setBaudRateCustom(int portFD, baud_rate baudRate);
#endif // #ifndef __POSIX_HELPER_FUNCTIONS_HEADER_H__

View File

@ -1,8 +1,8 @@
/*
* SerialPort_OSX.c
* SerialPort_Posix.c
*
* Created on: Feb 25, 2012
* Last Updated on: Oct 08, 2018
* Last Updated on: Nov 12, 2018
* Author: Will Hedgecock
*
* Copyright (C) 2012-2018 Fazecast, Inc.
@ -23,22 +23,25 @@
* see <http://www.gnu.org/licenses/> and <http://www.apache.org/licenses/>.
*/
#ifdef __APPLE__
#ifndef CMSPAR
#define CMSPAR 010000000000
#endif
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <termios.h>
#include <unistd.h>
#if defined(__linux__)
#include <linux/serial.h>
#elif defined(__sun__)
#include <sys/filio.h>
#elif defined(__APPLE__)
#include <CoreFoundation/CoreFoundation.h>
#include <IOKit/IOKitLib.h>
#include <IOKit/serial/IOSerialKeys.h>
#include <IOKit/serial/ioss.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>
#include <sys/time.h>
#include "OSXHelperFunctions.h"
#endif
#include "PosixHelperFunctions.h"
#include "../com_fazecast_jSerialComm_SerialPort.h"
// Cached class, method, and field IDs
@ -46,10 +49,10 @@ jclass serialCommClass;
jmethodID serialCommConstructor;
jfieldID serialPortFdField;
jfieldID comPortField;
jfieldID disableConfigField;
jfieldID friendlyNameField;
jfieldID portDescriptionField;
jfieldID isOpenedField;
jfieldID disableConfigField;
jfieldID isDtrEnabledField;
jfieldID isRtsEnabledField;
jfieldID baudRateField;
@ -65,6 +68,62 @@ jfieldID writeTimeoutField;
jfieldID eventFlagsField;
JNIEXPORT jobjectArray JNICALL Java_com_fazecast_jSerialComm_SerialPort_getCommPorts(JNIEnv *env, jclass serialComm)
#if defined(__linux__)
{
// Enumerate serial ports on machine
charTupleVector serialPorts = { (char**)malloc(1), (char**)malloc(1), (char**)malloc(1), 0 };
recursiveSearchForComPorts(&serialPorts, "/sys/devices/");
lastDitchSearchForComPorts(&serialPorts);
jobjectArray arrayObject = (*env)->NewObjectArray(env, serialPorts.length, serialCommClass, 0);
int i;
for (i = 0; i < serialPorts.length; ++i)
{
// Create new SerialComm object containing the enumerated values
jobject serialCommObject = (*env)->NewObject(env, serialCommClass, serialCommConstructor);
(*env)->SetObjectField(env, serialCommObject, portDescriptionField, (*env)->NewStringUTF(env, serialPorts.third[i]));
(*env)->SetObjectField(env, serialCommObject, friendlyNameField, (*env)->NewStringUTF(env, serialPorts.second[i]));
(*env)->SetObjectField(env, serialCommObject, comPortField, (*env)->NewStringUTF(env, serialPorts.first[i]));
free(serialPorts.first[i]);
free(serialPorts.second[i]);
free(serialPorts.third[i]);
// Add new SerialComm object to array
(*env)->SetObjectArrayElement(env, arrayObject, i, serialCommObject);
}
free(serialPorts.first);
free(serialPorts.second);
free(serialPorts.third);
return arrayObject;
}
#elif defined(__sun__)
{
// Enumerate serial ports on machine
charTupleVector serialPorts = { (char**)malloc(1), (char**)malloc(1), (char**)malloc(1), 0 };
searchForComPorts(&serialPorts);
jobjectArray arrayObject = (*env)->NewObjectArray(env, serialPorts.length, serialCommClass, 0);
int i;
for (i = 0; i < serialPorts.length; ++i)
{
// Create new SerialComm object containing the enumerated values
jobject serialCommObject = (*env)->NewObject(env, serialCommClass, serialCommConstructor);
(*env)->SetObjectField(env, serialCommObject, portDescriptionField, (*env)->NewStringUTF(env, serialPorts.third[i]));
(*env)->SetObjectField(env, serialCommObject, friendlyNameField, (*env)->NewStringUTF(env, serialPorts.second[i]));
(*env)->SetObjectField(env, serialCommObject, comPortField, (*env)->NewStringUTF(env, serialPorts.first[i]));
free(serialPorts.first[i]);
free(serialPorts.second[i]);
free(serialPorts.third[i]);
// Add new SerialComm object to array
(*env)->SetObjectArrayElement(env, arrayObject, i, serialCommObject);
}
free(serialPorts.first);
free(serialPorts.second);
free(serialPorts.third);
return arrayObject;
}
#elif defined(__APPLE__)
{
io_object_t serialPort;
io_iterator_t serialPortIterator;
@ -140,6 +199,7 @@ JNIEXPORT jobjectArray JNICALL Java_com_fazecast_jSerialComm_SerialPort_getCommP
return arrayObject;
}
#endif
JNIEXPORT void JNICALL Java_com_fazecast_jSerialComm_SerialPort_initializeLibrary(JNIEnv *env, jclass serialComm)
{
@ -190,7 +250,15 @@ JNIEXPORT jlong JNICALL Java_com_fazecast_jSerialComm_SerialPort_openPortNative(
struct termios options = { 0 };
fcntl(serialPortFD, F_SETFL, 0);
tcgetattr(serialPortFD, &options);
#if defined(__sun__)
options.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON);
options.c_oflag &= ~OPOST;
options.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
options.c_cflag &= ~(CSIZE | PARENB);
options.c_cflag |= CS8;
#else
cfmakeraw(&options);
#endif
if (!isDtrEnabled || !isRtsEnabled)
options.c_cflag &= ~HUPCL;
options.c_iflag |= BRKINT;
@ -221,7 +289,7 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_configPort(J
struct termios options = { 0 };
// Get port parameters from Java class
speed_t baudRate = (*env)->GetIntField(env, obj, baudRateField);
baud_rate baudRate = (*env)->GetIntField(env, obj, baudRateField);
int byteSizeInt = (*env)->GetIntField(env, obj, dataBitsField);
int stopBitsInt = (*env)->GetIntField(env, obj, stopBitsField);
int parityInt = (*env)->GetIntField(env, obj, parityField);
@ -254,23 +322,26 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_configPort(J
options.c_iflag |= (XonXoffInEnabled | XonXoffOutEnabled);
// Set baud rate
speed_t baudRateCode = getBaudRateCode(baudRate);
if (baudRateCode != 0)
{
cfsetispeed(&options, baudRateCode);
cfsetospeed(&options, baudRateCode);
}
baud_rate baudRateCode = getBaudRateCode(baudRate);
unsigned char nonStandardBaudRate = (baudRateCode == 0);
if (nonStandardBaudRate)
baudRateCode = B38400;
cfsetispeed(&options, baudRateCode);
cfsetospeed(&options, baudRateCode);
// Apply changes and block non-root users from opening this port
int retVal = configDisabled ? 0 : tcsetattr(serialPortFD, TCSANOW, &options);
ioctl(serialPortFD, TIOCEXCL);
// Attempt to set any necessary custom baud rates
if (baudRateCode == 0)
{
speed_t speed = (speed_t)baudRate;
ioctl(serialPortFD, IOSSIOSPEED, &speed);
}
// Attempt to set the transmit buffer size and any necessary custom baud rates
#if defined(__linux__)
struct serial_struct serInfo;
ioctl(serialPortFD, TIOCGSERIAL, &serInfo);
serInfo.xmit_fifo_size = sendDeviceQueueSize;
ioctl(serialPortFD, TIOCSSERIAL, &serInfo);
#endif
if (nonStandardBaudRate)
setBaudRateCustom(serialPortFD, baudRate);
return ((retVal == 0) && Java_com_fazecast_jSerialComm_SerialPort_configEventFlags(env, obj, serialPortFD) ? JNI_TRUE : JNI_FALSE);
}
@ -279,11 +350,13 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_configTimeou
// Get port timeouts from Java class
if (serialPortFD <= 0)
return JNI_FALSE;
baud_rate baudRate = (*env)->GetIntField(env, obj, baudRateField);
baud_rate baudRateCode = getBaudRateCode(baudRate);
int timeoutMode = (*env)->GetIntField(env, obj, timeoutModeField);
int readTimeout = (*env)->GetIntField(env, obj, readTimeoutField);
// Retrieve existing port configuration
struct termios options;
struct termios options = { 0 };
tcgetattr(serialPortFD, &options);
int flags = fcntl(serialPortFD, F_GETFL);
if (flags == -1)
@ -329,7 +402,11 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_configTimeou
// Apply changes
int retVal = fcntl(serialPortFD, F_SETFL, flags);
return ((retVal != -1) && (tcsetattr(serialPortFD, TCSANOW, &options) == 0)) ? JNI_TRUE : JNI_FALSE;
if (retVal != -1)
retVal = tcsetattr(serialPortFD, TCSANOW, &options);
if (baudRateCode == 0)
setBaudRateCustom(serialPortFD, baudRate);
return ((retVal == 0) ? JNI_TRUE : JNI_FALSE);
}
JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_configEventFlags(JNIEnv *env, jobject obj, jlong serialPortFD)
@ -338,13 +415,15 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_configEventF
return JNI_FALSE;
// Get event flags from Java class
baud_rate baudRate = (*env)->GetIntField(env, obj, baudRateField);
baud_rate baudRateCode = getBaudRateCode(baudRate);
int eventsToMonitor = (*env)->GetIntField(env, obj, eventFlagsField);
jboolean retVal = JNI_FALSE;
// Change read timeouts if we are monitoring data received
jboolean retVal;
if ((eventsToMonitor & com_fazecast_jSerialComm_SerialPort_LISTENING_EVENT_DATA_RECEIVED) > 0)
{
struct termios options;
struct termios options = { 0 };
tcgetattr(serialPortFD, &options);
int flags = fcntl(serialPortFD, F_GETFL);
if (flags == -1)
@ -352,12 +431,15 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_configEventF
flags &= ~O_NONBLOCK;
options.c_cc[VMIN] = 0;
options.c_cc[VTIME] = 10;
retVal = ((fcntl(serialPortFD, F_SETFL, flags) != -1) && (tcsetattr(serialPortFD, TCSANOW, &options) != -1)) ?
JNI_TRUE : JNI_FALSE;
retVal = ((fcntl(serialPortFD, F_SETFL, flags) == -1) || (tcsetattr(serialPortFD, TCSANOW, &options) == -1)) ?
JNI_FALSE : JNI_TRUE;
if (baudRateCode == 0)
setBaudRateCustom(serialPortFD, baudRate);
}
else
retVal = Java_com_fazecast_jSerialComm_SerialPort_configTimeouts(env, obj, serialPortFD);
// Apply changes
return retVal;
}
@ -465,7 +547,7 @@ JNIEXPORT jint JNICALL Java_com_fazecast_jSerialComm_SerialPort_readBytes(JNIEnv
else if ((timeoutMode & com_fazecast_jSerialComm_SerialPort_TIMEOUT_READ_BLOCKING) > 0) // Blocking mode, but not indefinitely
{
// Get current system time
struct timeval expireTime, currTime;
struct timeval expireTime = { 0 }, currTime = { 0 };
gettimeofday(&expireTime, NULL);
expireTime.tv_usec += (readTimeout * 1000);
if (expireTime.tv_usec > 1000000)
@ -529,7 +611,12 @@ JNIEXPORT jint JNICALL Java_com_fazecast_jSerialComm_SerialPort_writeBytes(JNIEn
return -1;
int timeoutMode = (*env)->GetIntField(env, obj, timeoutModeField);
jbyte *writeBuffer = (*env)->GetByteArrayElements(env, buffer, 0);
int numBytesWritten;
int numBytesWritten, result = 0;
// Set the DTR line to high if using RS-422
//ioctl(serialPortFD, TIOCMGET, &result);
//result |= TIOCM_DTR;
//ioctl(serialPortFD, TIOCMSET, &result);
// Write to port
do { numBytesWritten = write(serialPortFD, writeBuffer+offset, bytesToWrite); } while ((numBytesWritten < 0) && (errno == EINTR));
@ -548,6 +635,20 @@ JNIEXPORT jint JNICALL Java_com_fazecast_jSerialComm_SerialPort_writeBytes(JNIEn
if ((timeoutMode & com_fazecast_jSerialComm_SerialPort_TIMEOUT_WRITE_BLOCKING) > 0)
tcdrain(serialPortFD);
// Clear the DTR line if using RS-422
//#ifdef TIOCSERGETLSR
//do
//{
//result = ioctl(serialPortFD, TIOCSERGETLSR);
//if (result != TIOCSER_TEMT)
//usleep(100);
//} while (result != TIOCSER_TEMT);
//#endif
//ioctl(serialPortFD, TIOCMGET, &result);
//result &= ~TIOCM_DTR;
//ioctl(serialPortFD, TIOCMSET, &result);
//do { result = tcflush(serialPortFD, TCIFLUSH); } while ((result < 0) && (errno == EINTR));
// Return number of bytes written if successful
(*env)->ReleaseByteArrayElements(env, buffer, writeBuffer, JNI_ABORT);
return numBytesWritten;
@ -590,7 +691,11 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_presetRTS(JN
// Send a system command to preset the RTS mode of the serial port
char commandString[64];
#if defined(__linux__)
sprintf(commandString, "stty -F %s hupcl", portName);
#else
sprintf(commandString, "stty -f %s hupcl", portName);
#endif
int result = system(commandString);
(*env)->ReleaseStringUTFChars(env, portNameJString, portName);
@ -604,7 +709,11 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_preclearRTS(
// Send a system command to preset the RTS mode of the serial port
char commandString[64];
#if defined(__linux__)
sprintf(commandString, "stty -F %s -hupcl", portName);
#else
sprintf(commandString, "stty -f %s -hupcl", portName);
#endif
int result = system(commandString);
(*env)->ReleaseStringUTFChars(env, portNameJString, portName);
@ -634,7 +743,11 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_presetDTR(JN
// Send a system command to preset the DTR mode of the serial port
char commandString[64];
#if defined(__linux__)
sprintf(commandString, "stty -F %s hupcl", portName);
#else
sprintf(commandString, "stty -f %s hupcl", portName);
#endif
int result = system(commandString);
(*env)->ReleaseStringUTFChars(env, portNameJString, portName);
@ -648,7 +761,11 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_preclearDTR(
// Send a system command to preset the DTR mode of the serial port
char commandString[64];
#if defined(__linux__)
sprintf(commandString, "stty -F %s -hupcl", portName);
#else
sprintf(commandString, "stty -f %s -hupcl", portName);
#endif
int result = system(commandString);
(*env)->ReleaseStringUTFChars(env, portNameJString, portName);
@ -670,5 +787,3 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_getDSR(JNIEn
int modemBits = 0;
return (ioctl(serialPortFD, TIOCMGET, &modemBits) == 0) && (modemBits & TIOCM_LE);
}
#endif

Binary file not shown.

Binary file not shown.