Fix mstp on bsd port (#1193)
* fix mstp on macos NS_PER_S is 1s and 0ns * fix compile on freebsd * port bsd: add compile warnings and comments * port bsd: limit pthred prio to OS defined min/max value e.g. FreeBSD min=0 max=31 * port bsd: fix clangformat
This commit is contained in:
+66
-122
@@ -36,6 +36,7 @@
|
||||
#include "bacnet/datalink/mstp.h"
|
||||
#include "rs485.h"
|
||||
#include "bacnet/basic/sys/fifo.h"
|
||||
#include "bacnet/basic/sys/debug.h"
|
||||
|
||||
#include <sys/select.h>
|
||||
#include <sys/time.h>
|
||||
@@ -398,72 +399,45 @@ bool RS485_Set_Baud_Rate(uint32_t baud)
|
||||
void RS485_Send_Frame(
|
||||
struct mstp_port_struct_t *mstp_port, /* port specific data */
|
||||
const uint8_t *buffer, /* frame to send (up to 501 bytes of data) */
|
||||
uint16_t nbytes)
|
||||
{ /* number of bytes of data (up to 501) */
|
||||
uint16_t nbytes /* number of bytes of data (up to 501) */)
|
||||
{
|
||||
uint32_t turnaround_time_usec = Tturnaround * 1000000UL;
|
||||
uint32_t baud;
|
||||
uint32_t baud = RS485_Baud;
|
||||
int handle = RS485_Handle;
|
||||
ssize_t written = 0;
|
||||
int greska;
|
||||
SHARED_MSTP_DATA *poSharedData = NULL;
|
||||
const SHARED_MSTP_DATA *poSharedData = NULL;
|
||||
|
||||
if (mstp_port) {
|
||||
if (mstp_port && mstp_port->UserData) {
|
||||
poSharedData = (SHARED_MSTP_DATA *)mstp_port->UserData;
|
||||
}
|
||||
if (!poSharedData) {
|
||||
baud = RS485_Get_Baud_Rate();
|
||||
/* sleeping for turnaround time is necessary to give other devices
|
||||
time to change from sending to receiving state. */
|
||||
usleep(turnaround_time_usec / baud);
|
||||
/*
|
||||
On success, the number of bytes written are returned (zero
|
||||
indicates nothing was written). On error, -1 is returned, and
|
||||
errno is set appropriately. If count is zero and the file
|
||||
descriptor refers to a regular file, 0 will be returned without
|
||||
causing any other effect. For a special file, the results are not
|
||||
portable.
|
||||
*/
|
||||
written = write(RS485_Handle, buffer, nbytes);
|
||||
greska = errno;
|
||||
if (written <= 0) {
|
||||
printf("write error: %s\n", strerror(greska));
|
||||
} else {
|
||||
/* wait until all output has been transmitted. */
|
||||
tcdrain(RS485_Handle);
|
||||
}
|
||||
/* tcdrain(RS485_Handle); */
|
||||
/* per MSTP spec, sort of */
|
||||
if (mstp_port) {
|
||||
mstp_port->SilenceTimerReset((void *)mstp_port);
|
||||
}
|
||||
} else {
|
||||
baud = RS485_Get_Port_Baud_Rate(mstp_port);
|
||||
/* sleeping for turnaround time is necessary to give other devices
|
||||
time to change from sending to receiving state. */
|
||||
usleep(turnaround_time_usec / baud);
|
||||
/*
|
||||
On success, the number of bytes written are returned (zero
|
||||
indicates nothing was written). On error, -1 is returned, and
|
||||
errno is set appropriately. If count is zero and the file
|
||||
descriptor refers to a regular file, 0 will be returned without
|
||||
causing any other effect. For a special file, the results are not
|
||||
portable.
|
||||
*/
|
||||
written = write(poSharedData->RS485_Handle, buffer, nbytes);
|
||||
greska = errno;
|
||||
if (written <= 0) {
|
||||
printf("write error: %s\n", strerror(greska));
|
||||
} else {
|
||||
/* wait until all output has been transmitted. */
|
||||
tcdrain(poSharedData->RS485_Handle);
|
||||
}
|
||||
/* tcdrain(RS485_Handle); */
|
||||
/* per MSTP spec, sort of */
|
||||
if (mstp_port) {
|
||||
mstp_port->SilenceTimerReset((void *)mstp_port);
|
||||
}
|
||||
baud = poSharedData->RS485_Baud;
|
||||
handle = poSharedData->RS485_Handle;
|
||||
}
|
||||
|
||||
return;
|
||||
/* sleeping for turnaround time is necessary to give other devices
|
||||
time to change from sending to receiving state. */
|
||||
usleep(turnaround_time_usec / baud);
|
||||
/*
|
||||
On success, the number of bytes written are returned (zero
|
||||
indicates nothing was written). On error, -1 is returned, and
|
||||
errno is set appropriately. If count is zero and the file
|
||||
descriptor refers to a regular file, 0 will be returned without
|
||||
causing any other effect. For a special file, the results are not
|
||||
portable.
|
||||
*/
|
||||
written = write(handle, buffer, nbytes);
|
||||
greska = errno;
|
||||
if (written <= 0) {
|
||||
printf("write error: %s\n", strerror(greska));
|
||||
} else {
|
||||
/* wait until all output has been transmitted. */
|
||||
tcdrain(handle);
|
||||
}
|
||||
/* tcdrain(RS485_Handle); */
|
||||
/* per MSTP spec, sort of */
|
||||
if (mstp_port) {
|
||||
mstp_port->SilenceTimerReset((void *)mstp_port);
|
||||
}
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
@@ -477,72 +451,42 @@ void RS485_Check_UART_Data(struct mstp_port_struct_t *mstp_port)
|
||||
fd_set input;
|
||||
struct timeval waiter;
|
||||
uint8_t buf[2048];
|
||||
int n;
|
||||
ssize_t n;
|
||||
int handle = RS485_Handle;
|
||||
SHARED_MSTP_DATA *poSharedData;
|
||||
FIFO_BUFFER *fifo = &Rx_FIFO;
|
||||
|
||||
SHARED_MSTP_DATA *poSharedData = (SHARED_MSTP_DATA *)mstp_port->UserData;
|
||||
if (!poSharedData) {
|
||||
if (mstp_port->ReceiveError == true) {
|
||||
/* do nothing but wait for state machine to clear the error */
|
||||
/* burning time, so wait a longer time */
|
||||
waiter.tv_sec = 0;
|
||||
waiter.tv_usec = 5000;
|
||||
poSharedData = (SHARED_MSTP_DATA *)mstp_port->UserData;
|
||||
if (poSharedData) {
|
||||
handle = poSharedData->RS485_Handle;
|
||||
fifo = &poSharedData->Rx_FIFO;
|
||||
}
|
||||
if (mstp_port->ReceiveError == true) {
|
||||
/* do nothing but wait for state machine to clear the error */
|
||||
} else if (mstp_port->DataAvailable == false) {
|
||||
/* wait for state machine to read from the DataRegister */
|
||||
if (FIFO_Count(fifo) > 0) {
|
||||
/* data is available */
|
||||
mstp_port->DataRegister = FIFO_Get(fifo);
|
||||
mstp_port->DataAvailable = true;
|
||||
/* FIFO is giving data - just poll */
|
||||
waiter.tv_sec = 0;
|
||||
waiter.tv_usec = 5000;
|
||||
} else if (mstp_port->DataAvailable == false) {
|
||||
/* wait for state machine to read from the DataRegister */
|
||||
if (FIFO_Count(&Rx_FIFO) > 0) {
|
||||
/* data is available */
|
||||
mstp_port->DataRegister = FIFO_Get(&Rx_FIFO);
|
||||
mstp_port->DataAvailable = true;
|
||||
/* FIFO is giving data - just poll */
|
||||
waiter.tv_sec = 0;
|
||||
waiter.tv_usec = 0;
|
||||
} else {
|
||||
/* FIFO is empty - wait a longer time */
|
||||
waiter.tv_sec = 0;
|
||||
waiter.tv_usec = 5000;
|
||||
}
|
||||
waiter.tv_usec = 0;
|
||||
}
|
||||
/* grab bytes and stuff them into the FIFO every time */
|
||||
FD_ZERO(&input);
|
||||
FD_SET(RS485_Handle, &input);
|
||||
n = select(RS485_Handle + 1, &input, NULL, NULL, &waiter);
|
||||
if (n < 0) {
|
||||
return;
|
||||
}
|
||||
if (FD_ISSET(RS485_Handle, &input)) {
|
||||
n = read(RS485_Handle, buf, sizeof(buf));
|
||||
FIFO_Add(&Rx_FIFO, &buf[0], n);
|
||||
}
|
||||
} else {
|
||||
if (mstp_port->ReceiveError == true) {
|
||||
/* do nothing but wait for state machine to clear the error */
|
||||
/* burning time, so wait a longer time */
|
||||
waiter.tv_sec = 0;
|
||||
waiter.tv_usec = 5000;
|
||||
} else if (mstp_port->DataAvailable == false) {
|
||||
/* wait for state machine to read from the DataRegister */
|
||||
if (FIFO_Count(&poSharedData->Rx_FIFO) > 0) {
|
||||
/* data is available */
|
||||
mstp_port->DataRegister = FIFO_Get(&poSharedData->Rx_FIFO);
|
||||
mstp_port->DataAvailable = true;
|
||||
/* FIFO is giving data - just poll */
|
||||
waiter.tv_sec = 0;
|
||||
waiter.tv_usec = 0;
|
||||
} else {
|
||||
/* FIFO is empty - wait a longer time */
|
||||
waiter.tv_sec = 0;
|
||||
waiter.tv_usec = 5000;
|
||||
}
|
||||
}
|
||||
/* grab bytes and stuff them into the FIFO every time */
|
||||
FD_ZERO(&input);
|
||||
FD_SET(poSharedData->RS485_Handle, &input);
|
||||
n = select(poSharedData->RS485_Handle + 1, &input, NULL, NULL, &waiter);
|
||||
if (n < 0) {
|
||||
return;
|
||||
}
|
||||
if (FD_ISSET(poSharedData->RS485_Handle, &input)) {
|
||||
n = read(poSharedData->RS485_Handle, buf, sizeof(buf));
|
||||
FIFO_Add(&poSharedData->Rx_FIFO, &buf[0], n);
|
||||
}
|
||||
/* grab bytes and stuff them into the FIFO every time */
|
||||
FD_ZERO(&input);
|
||||
FD_SET(handle, &input);
|
||||
n = select(handle + 1, &input, NULL, NULL, &waiter);
|
||||
if (n < 0) {
|
||||
return;
|
||||
}
|
||||
if (FD_ISSET(handle, &input)) {
|
||||
n = read(handle, buf, sizeof(buf));
|
||||
if (n > 0) {
|
||||
FIFO_Add(fifo, &buf[0], n);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user