Ensure all data structures are properly initialized

This commit is contained in:
hedgecrw85 2019-11-12 11:21:55 -06:00
parent f06b03f186
commit 155ae7940c
26 changed files with 30 additions and 30 deletions

View File

@ -2,7 +2,7 @@
* SerialPort_Android.c * SerialPort_Android.c
* *
* Created on: Mar 13, 2015 * Created on: Mar 13, 2015
* Last Updated on: Nov 07, 2019 * Last Updated on: Nov 12, 2019
* Author: Will Hedgecock * Author: Will Hedgecock
* *
* Copyright (C) 2012-2019 Fazecast, Inc. * Copyright (C) 2012-2019 Fazecast, Inc.
@ -154,7 +154,7 @@ JNIEXPORT jlong JNICALL Java_com_fazecast_jSerialComm_SerialPort_openPortNative(
// Clear any serial port flags and set up raw, non-canonical port parameters // Clear any serial port flags and set up raw, non-canonical port parameters
if (isatty(serialPortFD)) if (isatty(serialPortFD))
{ {
struct termios options = { 0 }; struct termios options = {0};
fcntl(serialPortFD, F_SETFL, 0); fcntl(serialPortFD, F_SETFL, 0);
ioctl(serialPortFD, TCGETS, &options); ioctl(serialPortFD, TCGETS, &options);
options.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON); options.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON);
@ -188,8 +188,8 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_configPort(J
{ {
if (serialPortFD <= 0) if (serialPortFD <= 0)
return JNI_FALSE; return JNI_FALSE;
struct serial_struct serInfo; struct serial_struct serInfo = {0};
struct termios options = { 0 }; struct termios options = {0};
// Get port parameters from Java class // Get port parameters from Java class
int baudRate = (*env)->GetIntField(env, obj, baudRateField); int baudRate = (*env)->GetIntField(env, obj, baudRateField);
@ -267,13 +267,13 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_configPort(J
setBaudRate(serialPortFD, baudRate); setBaudRate(serialPortFD, baudRate);
// Attempt to set the requested RS-485 mode // Attempt to set the requested RS-485 mode
struct serial_rs485 rs485Conf; struct serial_rs485 rs485Conf = {0};
if (ioctl(serialPortFD, TIOCGRS485, &rs485Conf) == 0) if (ioctl(serialPortFD, TIOCGRS485, &rs485Conf) == 0)
{ {
if (rs485ModeEnabled) if (rs485ModeEnabled)
rs485Conf.flags |= SER_RS485_ENABLED; rs485Conf.flags |= SER_RS485_ENABLED; // Set these too? SER_RS485_RTS_ON_SEND | SER_RS485_RTS_AFTER_SEND
else else
rs485Conf.flags &= ~SER_RS485_ENABLED; rs485Conf.flags &= ~SER_RS485_ENABLED; // Clear these too? &= ~(SER_RS485_ENABLED | SER_RS485_RTS_ON_SEND | SER_RS485_RTS_AFTER_SEND);
rs485Conf.delay_rts_before_send = rs485DelayBefore; rs485Conf.delay_rts_before_send = rs485DelayBefore;
rs485Conf.delay_rts_after_send = rs485DelayAfter; rs485Conf.delay_rts_after_send = rs485DelayAfter;
ioctl(serialPortFD, TIOCSRS485, &rs485Conf); ioctl(serialPortFD, TIOCSRS485, &rs485Conf);
@ -292,7 +292,7 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_configTimeou
int readTimeout = (*env)->GetIntField(env, obj, readTimeoutField); int readTimeout = (*env)->GetIntField(env, obj, readTimeoutField);
// Retrieve existing port configuration // Retrieve existing port configuration
struct termios options = { 0 }; struct termios options = {0};
if (isatty(serialPortFD)) if (isatty(serialPortFD))
ioctl(serialPortFD, TCGETS, &options); ioctl(serialPortFD, TCGETS, &options);
else else
@ -383,7 +383,7 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_configEventF
jboolean retVal; jboolean retVal;
if ((eventsToMonitor & com_fazecast_jSerialComm_SerialPort_LISTENING_EVENT_DATA_RECEIVED) > 0) if ((eventsToMonitor & com_fazecast_jSerialComm_SerialPort_LISTENING_EVENT_DATA_RECEIVED) > 0)
{ {
struct termios options = { 0 }; struct termios options = {0};
ioctl(serialPortFD, TCGETS, &options); ioctl(serialPortFD, TCGETS, &options);
int flags = fcntl(serialPortFD, F_GETFL); int flags = fcntl(serialPortFD, F_GETFL);
if (flags == -1) if (flags == -1)
@ -424,7 +424,7 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_closePortNat
(*env)->SetBooleanField(env, obj, isOpenedField, JNI_FALSE); (*env)->SetBooleanField(env, obj, isOpenedField, JNI_FALSE);
// Force the port to enter non-blocking mode to ensure that any current reads return // Force the port to enter non-blocking mode to ensure that any current reads return
struct termios options; struct termios options = {0};
ioctl(serialPortFD, TCGETS, &options); ioctl(serialPortFD, TCGETS, &options);
int flags = fcntl(serialPortFD, F_GETFL); int flags = fcntl(serialPortFD, F_GETFL);
flags |= O_NONBLOCK; flags |= O_NONBLOCK;
@ -500,7 +500,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 else if ((timeoutMode & com_fazecast_jSerialComm_SerialPort_TIMEOUT_READ_BLOCKING) > 0) // Blocking mode, but not indefinitely
{ {
// Get current system time // Get current system time
struct timeval expireTime = { 0 }, currTime = { 0 }; struct timeval expireTime = {0}, currTime = {0};
gettimeofday(&expireTime, NULL); gettimeofday(&expireTime, NULL);
expireTime.tv_usec += (readTimeout * 1000); expireTime.tv_usec += (readTimeout * 1000);
if (expireTime.tv_usec > 1000000) if (expireTime.tv_usec > 1000000)

View File

@ -2,7 +2,7 @@
* SerialPort_Posix.c * SerialPort_Posix.c
* *
* Created on: Feb 25, 2012 * Created on: Feb 25, 2012
* Last Updated on: Nov 08, 2019 * Last Updated on: Nov 12, 2019
* Author: Will Hedgecock * Author: Will Hedgecock
* *
* Copyright (C) 2012-2019 Fazecast, Inc. * Copyright (C) 2012-2019 Fazecast, Inc.
@ -263,7 +263,7 @@ JNIEXPORT jlong JNICALL Java_com_fazecast_jSerialComm_SerialPort_openPortNative(
else else
{ {
// Clear any serial port flags and set up raw, non-canonical port parameters // Clear any serial port flags and set up raw, non-canonical port parameters
struct termios options = { 0 }; struct termios options = {0};
fcntl(serialPortFD, F_SETFL, 0); fcntl(serialPortFD, F_SETFL, 0);
tcgetattr(serialPortFD, &options); tcgetattr(serialPortFD, &options);
#if defined(__sun__) #if defined(__sun__)
@ -303,7 +303,7 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_configPort(J
{ {
if (serialPortFD <= 0) if (serialPortFD <= 0)
return JNI_FALSE; return JNI_FALSE;
struct termios options = { 0 }; struct termios options = {0};
// Get port parameters from Java class // Get port parameters from Java class
baud_rate baudRate = (*env)->GetIntField(env, obj, baudRateField); baud_rate baudRate = (*env)->GetIntField(env, obj, baudRateField);
@ -355,7 +355,7 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_configPort(J
// Attempt to set the transmit buffer size and any necessary custom baud rates // Attempt to set the transmit buffer size and any necessary custom baud rates
#if defined(__linux__) #if defined(__linux__)
struct serial_struct serInfo; struct serial_struct serInfo = {0};
if (ioctl(serialPortFD, TIOCGSERIAL, &serInfo) == 0) if (ioctl(serialPortFD, TIOCGSERIAL, &serInfo) == 0)
{ {
serInfo.xmit_fifo_size = sendDeviceQueueSize; serInfo.xmit_fifo_size = sendDeviceQueueSize;
@ -370,13 +370,13 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_configPort(J
// Attempt to set the requested RS-485 mode // Attempt to set the requested RS-485 mode
#if defined(__linux__) #if defined(__linux__)
struct serial_rs485 rs485Conf; struct serial_rs485 rs485Conf = {0};
if (ioctl(serialPortFD, TIOCGRS485, &rs485Conf) == 0) if (ioctl(serialPortFD, TIOCGRS485, &rs485Conf) == 0)
{ {
if (rs485ModeEnabled) if (rs485ModeEnabled)
rs485Conf.flags |= SER_RS485_ENABLED; rs485Conf.flags |= SER_RS485_ENABLED; // Set these too? SER_RS485_RTS_ON_SEND | SER_RS485_RTS_AFTER_SEND
else else
rs485Conf.flags &= ~SER_RS485_ENABLED; rs485Conf.flags &= ~SER_RS485_ENABLED; // Clear these too? &= ~(SER_RS485_ENABLED | SER_RS485_RTS_ON_SEND | SER_RS485_RTS_AFTER_SEND);
rs485Conf.delay_rts_before_send = rs485DelayBefore; rs485Conf.delay_rts_before_send = rs485DelayBefore;
rs485Conf.delay_rts_after_send = rs485DelayAfter; rs485Conf.delay_rts_after_send = rs485DelayAfter;
ioctl(serialPortFD, TIOCSRS485, &rs485Conf); ioctl(serialPortFD, TIOCSRS485, &rs485Conf);
@ -396,7 +396,7 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_configTimeou
int readTimeout = (*env)->GetIntField(env, obj, readTimeoutField); int readTimeout = (*env)->GetIntField(env, obj, readTimeoutField);
// Retrieve existing port configuration // Retrieve existing port configuration
struct termios options = { 0 }; struct termios options = {0};
tcgetattr(serialPortFD, &options); tcgetattr(serialPortFD, &options);
int flags = fcntl(serialPortFD, F_GETFL); int flags = fcntl(serialPortFD, F_GETFL);
if (flags == -1) if (flags == -1)
@ -463,7 +463,7 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_configEventF
jboolean retVal; jboolean retVal;
if ((eventsToMonitor & com_fazecast_jSerialComm_SerialPort_LISTENING_EVENT_DATA_RECEIVED) > 0) if ((eventsToMonitor & com_fazecast_jSerialComm_SerialPort_LISTENING_EVENT_DATA_RECEIVED) > 0)
{ {
struct termios options = { 0 }; struct termios options = {0};
tcgetattr(serialPortFD, &options); tcgetattr(serialPortFD, &options);
int flags = fcntl(serialPortFD, F_GETFL); int flags = fcntl(serialPortFD, F_GETFL);
if (flags == -1) if (flags == -1)
@ -507,7 +507,7 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_closePortNat
(*env)->SetBooleanField(env, obj, isOpenedField, JNI_FALSE); (*env)->SetBooleanField(env, obj, isOpenedField, JNI_FALSE);
// Force the port to enter non-blocking mode to ensure that any current reads return // Force the port to enter non-blocking mode to ensure that any current reads return
struct termios options = { 0 }; struct termios options = {0};
tcgetattr(serialPortFD, &options); tcgetattr(serialPortFD, &options);
int flags = fcntl(serialPortFD, F_GETFL); int flags = fcntl(serialPortFD, F_GETFL);
flags |= O_NONBLOCK; flags |= O_NONBLOCK;
@ -586,7 +586,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 else if ((timeoutMode & com_fazecast_jSerialComm_SerialPort_TIMEOUT_READ_BLOCKING) > 0) // Blocking mode, but not indefinitely
{ {
// Get current system time // Get current system time
struct timeval expireTime = { 0 }, currTime = { 0 }; struct timeval expireTime = {0}, currTime = {0};
gettimeofday(&expireTime, NULL); gettimeofday(&expireTime, NULL);
expireTime.tv_usec += (readTimeout * 1000); expireTime.tv_usec += (readTimeout * 1000);
if (expireTime.tv_usec > 1000000) if (expireTime.tv_usec > 1000000)

View File

@ -2,7 +2,7 @@
* SerialPort_Windows.c * SerialPort_Windows.c
* *
* Created on: Feb 25, 2012 * Created on: Feb 25, 2012
* Last Updated on: Nov 07, 2019 * Last Updated on: Nov 12, 2019
* Author: Will Hedgecock * Author: Will Hedgecock
* *
* Copyright (C) 2012-2019 Fazecast, Inc. * Copyright (C) 2012-2019 Fazecast, Inc.
@ -408,7 +408,7 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_configPort(J
HANDLE serialPortHandle = (HANDLE)serialPortFD; HANDLE serialPortHandle = (HANDLE)serialPortFD;
if (serialPortHandle == INVALID_HANDLE_VALUE) if (serialPortHandle == INVALID_HANDLE_VALUE)
return JNI_FALSE; return JNI_FALSE;
DCB dcbSerialParams = {0}; DCB dcbSerialParams{};
dcbSerialParams.DCBlength = sizeof(DCB); dcbSerialParams.DCBlength = sizeof(DCB);
// Get port parameters from Java class // Get port parameters from Java class
@ -476,7 +476,7 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_configTimeou
HANDLE serialPortHandle = (HANDLE)serialPortFD; HANDLE serialPortHandle = (HANDLE)serialPortFD;
if (serialPortHandle == INVALID_HANDLE_VALUE) if (serialPortHandle == INVALID_HANDLE_VALUE)
return JNI_FALSE; return JNI_FALSE;
COMMTIMEOUTS timeouts = {0}; COMMTIMEOUTS timeouts{};
int timeoutMode = env->GetIntField(obj, timeoutModeField); int timeoutMode = env->GetIntField(obj, timeoutModeField);
DWORD readTimeout = (DWORD)env->GetIntField(obj, readTimeoutField); DWORD readTimeout = (DWORD)env->GetIntField(obj, readTimeoutField);
DWORD writeTimeout = (DWORD)env->GetIntField(obj, writeTimeoutField); DWORD writeTimeout = (DWORD)env->GetIntField(obj, writeTimeoutField);
@ -537,7 +537,7 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_configEventF
// Change read timeouts if we are monitoring data received // Change read timeouts if we are monitoring data received
if ((eventsToMonitor & com_fazecast_jSerialComm_SerialPort_LISTENING_EVENT_DATA_RECEIVED) > 0) if ((eventsToMonitor & com_fazecast_jSerialComm_SerialPort_LISTENING_EVENT_DATA_RECEIVED) > 0)
{ {
COMMTIMEOUTS timeouts = {0}; COMMTIMEOUTS timeouts{};
timeouts.ReadIntervalTimeout = MAXDWORD; timeouts.ReadIntervalTimeout = MAXDWORD;
timeouts.ReadTotalTimeoutMultiplier = MAXDWORD; timeouts.ReadTotalTimeoutMultiplier = MAXDWORD;
timeouts.ReadTotalTimeoutConstant = 1000; timeouts.ReadTotalTimeoutConstant = 1000;
@ -557,7 +557,7 @@ JNIEXPORT jint JNICALL Java_com_fazecast_jSerialComm_SerialPort_waitForEvent(JNI
HANDLE serialPortHandle = (HANDLE)serialPortFD; HANDLE serialPortHandle = (HANDLE)serialPortFD;
if (serialPortHandle == INVALID_HANDLE_VALUE) if (serialPortHandle == INVALID_HANDLE_VALUE)
return 0; return 0;
OVERLAPPED overlappedStruct = {0}; OVERLAPPED overlappedStruct{};
overlappedStruct.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); overlappedStruct.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
if (overlappedStruct.hEvent == NULL) if (overlappedStruct.hEvent == NULL)
{ {
@ -607,7 +607,7 @@ JNIEXPORT jboolean JNICALL Java_com_fazecast_jSerialComm_SerialPort_closePortNat
PurgeComm(serialPortHandle, PURGE_RXABORT | PURGE_RXCLEAR | PURGE_TXABORT | PURGE_TXCLEAR); PurgeComm(serialPortHandle, PURGE_RXABORT | PURGE_RXCLEAR | PURGE_TXABORT | PURGE_TXCLEAR);
// Force the port to enter non-blocking mode to ensure that any current reads return // Force the port to enter non-blocking mode to ensure that any current reads return
COMMTIMEOUTS timeouts = {0}; COMMTIMEOUTS timeouts{};
timeouts.WriteTotalTimeoutMultiplier = 0; timeouts.WriteTotalTimeoutMultiplier = 0;
timeouts.ReadIntervalTimeout = MAXDWORD; timeouts.ReadIntervalTimeout = MAXDWORD;
timeouts.ReadTotalTimeoutMultiplier = 0; timeouts.ReadTotalTimeoutMultiplier = 0;
@ -689,7 +689,7 @@ JNIEXPORT jint JNICALL Java_com_fazecast_jSerialComm_SerialPort_readBytes(JNIEnv
} }
// Create an asynchronous result structure // Create an asynchronous result structure
OVERLAPPED overlappedStruct = {0}; OVERLAPPED overlappedStruct{};
overlappedStruct.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); overlappedStruct.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
if (overlappedStruct.hEvent == NULL) if (overlappedStruct.hEvent == NULL)
{ {
@ -751,7 +751,7 @@ JNIEXPORT jint JNICALL Java_com_fazecast_jSerialComm_SerialPort_writeBytes(JNIEn
} }
// Create an asynchronous result structure // Create an asynchronous result structure
OVERLAPPED overlappedStruct = {0}; OVERLAPPED overlappedStruct{};
overlappedStruct.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); overlappedStruct.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
if (overlappedStruct.hEvent == NULL) if (overlappedStruct.hEvent == NULL)
{ {