Indented with indent.sh script.
This commit is contained in:
@@ -127,7 +127,8 @@ int dlmstp_send_pdu(
|
||||
packet.pdu_len = pdu_len;
|
||||
memmove(&packet.pdu[0], &pdu[0], pdu_len);
|
||||
memmove(&packet.address, dest, sizeof(packet.address));
|
||||
rc = mq_send(NPDU_Transmit_Queue, (const char *)&packet, sizeof(packet), 0);
|
||||
rc = mq_send(NPDU_Transmit_Queue, (const char *) &packet,
|
||||
sizeof(packet), 0);
|
||||
if (rc > 0)
|
||||
bytes_sent = rc;
|
||||
}
|
||||
@@ -146,10 +147,10 @@ uint16_t dlmstp_receive(
|
||||
uint16_t pdu_len = 0;
|
||||
mqd_t received_bytes = 0;
|
||||
DLMSTP_PACKET packet;
|
||||
struct timespec queue_timeout = {0};
|
||||
struct timespec queue_timeout = { 0 };
|
||||
time_t epoch_time = 0;
|
||||
unsigned msg_prio = 0;
|
||||
char buffer[sizeof(struct dlmstp_packet)+1];
|
||||
char buffer[sizeof(struct dlmstp_packet) + 1];
|
||||
|
||||
if (NPDU_Receive_Queue == -1) {
|
||||
return 0;
|
||||
@@ -166,13 +167,10 @@ uint16_t dlmstp_receive(
|
||||
}
|
||||
/* get current time */
|
||||
epoch_time = time(NULL);
|
||||
queue_timeout.tv_sec += epoch_time;
|
||||
queue_timeout.tv_sec += epoch_time;
|
||||
|
||||
received_bytes = mq_timedreceive(
|
||||
NPDU_Receive_Queue,
|
||||
buffer,
|
||||
sizeof(buffer),
|
||||
&msg_prio,
|
||||
received_bytes =
|
||||
mq_timedreceive(NPDU_Receive_Queue, buffer, sizeof(buffer), &msg_prio,
|
||||
&queue_timeout);
|
||||
|
||||
/* See if there is a problem */
|
||||
@@ -182,10 +180,9 @@ uint16_t dlmstp_receive(
|
||||
/* was immediately available for reading. */
|
||||
if ((errno != EAGAIN) && (errno != ETIMEDOUT)) {
|
||||
#if PRINT_ENABLED
|
||||
fprintf(stderr, "MS/TP: NPDU Receive: %s\n",
|
||||
strerror(errno));
|
||||
fprintf(stderr, "MS/TP: NPDU Receive: %s\n", strerror(errno));
|
||||
#endif
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -193,7 +190,7 @@ uint16_t dlmstp_receive(
|
||||
return 0;
|
||||
|
||||
/* copy the buffer into the PDU */
|
||||
memmove(&packet,buffer,sizeof(packet));
|
||||
memmove(&packet, buffer, sizeof(packet));
|
||||
pdu_len = packet.pdu_len;
|
||||
memmove(&pdu[0], &packet.pdu[0], pdu_len);
|
||||
memmove(src, &packet.address, sizeof(packet.address));
|
||||
@@ -285,7 +282,7 @@ uint16_t MSTP_Put_Receive(
|
||||
pdu_len = sizeof(packet.pdu);
|
||||
if (pdu_len) {
|
||||
#if PRINT_ENABLED
|
||||
fprintf(stderr,"MSTP: packet from FSM.\n");
|
||||
fprintf(stderr, "MSTP: packet from FSM.\n");
|
||||
#endif
|
||||
MSTP_Packets++;
|
||||
memmove(&packet.pdu[0], (void *) &mstp_port->InputBuffer[0], pdu_len);
|
||||
@@ -293,7 +290,7 @@ uint16_t MSTP_Put_Receive(
|
||||
packet.pdu_len = pdu_len;
|
||||
/* ready is not used in this scheme */
|
||||
packet.ready = true;
|
||||
mq_send(NPDU_Receive_Queue, (const char *)&packet, sizeof(packet), 0);
|
||||
mq_send(NPDU_Receive_Queue, (const char *) &packet, sizeof(packet), 0);
|
||||
}
|
||||
|
||||
return pdu_len;
|
||||
@@ -306,10 +303,10 @@ int dlmstp_get_transmit_packet(
|
||||
unsigned timeout)
|
||||
{ /* milliseconds to wait for a packet */
|
||||
int received_bytes = 0; /* return value */
|
||||
struct timespec queue_timeout = {0};
|
||||
struct timespec queue_timeout = { 0 };
|
||||
time_t epoch_time = 0;
|
||||
unsigned msg_prio = 0;
|
||||
char buffer[sizeof(struct dlmstp_packet)+1];
|
||||
char buffer[sizeof(struct dlmstp_packet) + 1];
|
||||
|
||||
/* Make sure the socket is open */
|
||||
if (NPDU_Transmit_Queue == -1)
|
||||
@@ -325,13 +322,10 @@ int dlmstp_get_transmit_packet(
|
||||
}
|
||||
/* get current time */
|
||||
epoch_time = time(NULL);
|
||||
queue_timeout.tv_sec += epoch_time;
|
||||
queue_timeout.tv_sec += epoch_time;
|
||||
|
||||
received_bytes = mq_timedreceive(
|
||||
NPDU_Transmit_Queue,
|
||||
buffer,
|
||||
sizeof(buffer),
|
||||
&msg_prio,
|
||||
received_bytes =
|
||||
mq_timedreceive(NPDU_Transmit_Queue, buffer, sizeof(buffer), &msg_prio,
|
||||
&queue_timeout);
|
||||
|
||||
/* See if there is a problem */
|
||||
@@ -341,13 +335,14 @@ int dlmstp_get_transmit_packet(
|
||||
/* was immediately available for reading. */
|
||||
if ((errno != EAGAIN) && (errno != ETIMEDOUT)) {
|
||||
#if PRINT_ENABLED
|
||||
fprintf(stderr, "MS/TP: Read error in Transmit_Client packet: %s\n",
|
||||
fprintf(stderr,
|
||||
"MS/TP: Read error in Transmit_Client packet: %s\n",
|
||||
strerror(errno));
|
||||
#endif
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
memmove(packet,buffer,sizeof(packet));
|
||||
memmove(packet, buffer, sizeof(packet));
|
||||
|
||||
return (received_bytes);
|
||||
}
|
||||
@@ -376,7 +371,7 @@ uint16_t MSTP_Get_Send(
|
||||
return 0;
|
||||
}
|
||||
#if PRINT_ENABLED
|
||||
fprintf(stderr,"MS/TP: sending packet to FSM.\n");
|
||||
fprintf(stderr, "MS/TP: sending packet to FSM.\n");
|
||||
#endif
|
||||
/* convert the PDU into the MSTP Frame */
|
||||
pdu_len = MSTP_Create_Frame(&mstp_port->OutputBuffer[0], /* <-- loading this */
|
||||
@@ -521,23 +516,23 @@ uint16_t MSTP_Get_Reply(
|
||||
/* is this the reply to the DER? */
|
||||
matched =
|
||||
dlmstp_compare_data_expecting_reply(&mstp_port->InputBuffer[0],
|
||||
mstp_port->DataLength, mstp_port->SourceAddress,
|
||||
&packet.pdu[0], packet.pdu_len,
|
||||
&packet.address);
|
||||
mstp_port->DataLength, mstp_port->SourceAddress, &packet.pdu[0],
|
||||
packet.pdu_len, &packet.address);
|
||||
if (matched) {
|
||||
#if PRINT_ENABLED
|
||||
fprintf(stderr,"MSTP: sending packet to FSM.\n");
|
||||
fprintf(stderr, "MSTP: sending packet to FSM.\n");
|
||||
#endif
|
||||
/* convert the PDU into the MSTP Frame */
|
||||
pdu_len = MSTP_Create_Frame(&mstp_port->OutputBuffer[0],
|
||||
mstp_port->OutputBufferSize, packet.frame_type,
|
||||
destination, mstp_port->This_Station, &packet.pdu[0],
|
||||
packet.pdu_len);
|
||||
pdu_len =
|
||||
MSTP_Create_Frame(&mstp_port->OutputBuffer[0],
|
||||
mstp_port->OutputBufferSize, packet.frame_type, destination,
|
||||
mstp_port->This_Station, &packet.pdu[0], packet.pdu_len);
|
||||
/* not used here, but setting it anyway */
|
||||
packet.ready = false;
|
||||
} else {
|
||||
/* put it back into the queue */
|
||||
(void)mq_send(NPDU_Transmit_Queue, (char *)&packet, sizeof(packet), 1);
|
||||
(void) mq_send(NPDU_Transmit_Queue, (char *) &packet, sizeof(packet),
|
||||
1);
|
||||
}
|
||||
|
||||
return pdu_len;
|
||||
@@ -687,27 +682,23 @@ bool dlmstp_init(
|
||||
char mqname[32];
|
||||
struct mq_attr mqattr;
|
||||
|
||||
mqattr.mq_flags = 0;
|
||||
mqattr.mq_maxmsg = 5;
|
||||
mqattr.mq_flags = 0;
|
||||
mqattr.mq_maxmsg = 5;
|
||||
mqattr.mq_msgsize = sizeof(struct dlmstp_packet);
|
||||
/* create a queue for the NDPU data between MS/TP threads */
|
||||
snprintf(mqname, sizeof(mqname), "/MSTP_Rx_%d", getpid());
|
||||
NPDU_Transmit_Queue = mq_open(mqname,
|
||||
O_RDWR | O_CREAT, 0600, &mqattr);
|
||||
NPDU_Transmit_Queue = mq_open(mqname, O_RDWR | O_CREAT, 0600, &mqattr);
|
||||
if (NPDU_Transmit_Queue == -1) {
|
||||
#if PRINT_ENABLED
|
||||
fprintf(stderr, "MS/TP: Create NPDU Transmit Queue %s: %s\n",
|
||||
mqname,
|
||||
fprintf(stderr, "MS/TP: Create NPDU Transmit Queue %s: %s\n", mqname,
|
||||
strerror(errno));
|
||||
#endif
|
||||
}
|
||||
snprintf(mqname, sizeof(mqname), "/MSTP_Tx_%d", getpid());
|
||||
NPDU_Receive_Queue = mq_open(mqname,
|
||||
O_RDWR | O_CREAT, 0600, &mqattr);
|
||||
NPDU_Receive_Queue = mq_open(mqname, O_RDWR | O_CREAT, 0600, &mqattr);
|
||||
if (NPDU_Receive_Queue == -1) {
|
||||
#if PRINT_ENABLED
|
||||
fprintf(stderr, "MS/TP: Create NPDU Receive Queue %s: %s\n",
|
||||
mqname,
|
||||
fprintf(stderr, "MS/TP: Create NPDU Receive Queue %s: %s\n", mqname,
|
||||
strerror(errno));
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -91,7 +91,8 @@ void RS485_Set_Interface(
|
||||
* ALGORITHM: none
|
||||
* NOTES: none
|
||||
*********************************************************************/
|
||||
const char *RS485_Interface(void)
|
||||
const char *RS485_Interface(
|
||||
void)
|
||||
{
|
||||
return RS485_Port_Name;
|
||||
}
|
||||
|
||||
@@ -45,7 +45,8 @@ extern "C" {
|
||||
|
||||
void RS485_Set_Interface(
|
||||
char *ifname);
|
||||
const char * RS485_Interface(void);
|
||||
const char *RS485_Interface(
|
||||
void);
|
||||
|
||||
void RS485_Initialize(
|
||||
void);
|
||||
|
||||
@@ -45,7 +45,7 @@
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
#include <pthread.h>
|
||||
#include <signal.h> /* signal handling functions */
|
||||
#include <signal.h> /* signal handling functions */
|
||||
|
||||
/* local includes */
|
||||
#include "bytes.h"
|
||||
@@ -146,58 +146,59 @@ int timestamp_ms(
|
||||
}
|
||||
|
||||
static const char *Capture_Filename = "mstp.cap";
|
||||
static FILE *pFile = NULL; /* stream pointer */
|
||||
static FILE *pFile = NULL; /* stream pointer */
|
||||
|
||||
/* write packet to file in libpcap format */
|
||||
static void write_global_header(void)
|
||||
static void write_global_header(
|
||||
void)
|
||||
{
|
||||
uint32_t magic_number = 0xa1b2c3d4; /* magic number */
|
||||
uint16_t version_major = 2; /* major version number */
|
||||
uint16_t version_minor = 4; /* minor version number */
|
||||
int32_t thiszone = 0; /* GMT to local correction */
|
||||
uint32_t sigfigs = 0; /* accuracy of timestamps */
|
||||
uint32_t snaplen = 65535; /* max length of captured packets, in octets */
|
||||
uint32_t network = 165; /* data link type - BACNET_MS_TP */
|
||||
int32_t thiszone = 0; /* GMT to local correction */
|
||||
uint32_t sigfigs = 0; /* accuracy of timestamps */
|
||||
uint32_t snaplen = 65535; /* max length of captured packets, in octets */
|
||||
uint32_t network = 165; /* data link type - BACNET_MS_TP */
|
||||
|
||||
/* create a new file. */
|
||||
pFile = fopen(Capture_Filename, "wb");
|
||||
if (pFile) {
|
||||
fwrite(&magic_number,sizeof(magic_number),1,pFile);
|
||||
fwrite(&version_major,sizeof(version_major),1,pFile);
|
||||
fwrite(&version_minor,sizeof(version_minor),1,pFile);
|
||||
fwrite(&thiszone,sizeof(thiszone),1,pFile);
|
||||
fwrite(&sigfigs,sizeof(sigfigs),1,pFile);
|
||||
fwrite(&snaplen,sizeof(snaplen),1,pFile);
|
||||
fwrite(&network,sizeof(network),1,pFile);
|
||||
fwrite(&magic_number, sizeof(magic_number), 1, pFile);
|
||||
fwrite(&version_major, sizeof(version_major), 1, pFile);
|
||||
fwrite(&version_minor, sizeof(version_minor), 1, pFile);
|
||||
fwrite(&thiszone, sizeof(thiszone), 1, pFile);
|
||||
fwrite(&sigfigs, sizeof(sigfigs), 1, pFile);
|
||||
fwrite(&snaplen, sizeof(snaplen), 1, pFile);
|
||||
fwrite(&network, sizeof(network), 1, pFile);
|
||||
} else {
|
||||
fprintf(stderr,"rx_fsm: failed to open %s: %s\n",
|
||||
Capture_Filename, strerror(errno));
|
||||
fprintf(stderr, "rx_fsm: failed to open %s: %s\n", Capture_Filename,
|
||||
strerror(errno));
|
||||
}
|
||||
}
|
||||
|
||||
static void write_received_packet(
|
||||
volatile struct mstp_port_struct_t *mstp_port)
|
||||
{
|
||||
uint32_t ts_sec; /* timestamp seconds */
|
||||
uint32_t ts_usec; /* timestamp microseconds */
|
||||
uint32_t incl_len; /* number of octets of packet saved in file */
|
||||
uint32_t orig_len; /* actual length of packet */
|
||||
uint8_t header[8]; /* MS/TP header */
|
||||
uint32_t ts_sec; /* timestamp seconds */
|
||||
uint32_t ts_usec; /* timestamp microseconds */
|
||||
uint32_t incl_len; /* number of octets of packet saved in file */
|
||||
uint32_t orig_len; /* actual length of packet */
|
||||
uint8_t header[8]; /* MS/TP header */
|
||||
struct timeval tv;
|
||||
|
||||
if (pFile) {
|
||||
gettimeofday(&tv, NULL);
|
||||
ts_sec = tv.tv_sec;
|
||||
ts_usec = tv.tv_usec;
|
||||
fwrite(&ts_sec,sizeof(ts_sec),1,pFile);
|
||||
fwrite(&ts_usec,sizeof(ts_usec),1,pFile);
|
||||
fwrite(&ts_sec, sizeof(ts_sec), 1, pFile);
|
||||
fwrite(&ts_usec, sizeof(ts_usec), 1, pFile);
|
||||
if (mstp_port->DataLength) {
|
||||
incl_len = orig_len = 8 + mstp_port->DataLength + 2;
|
||||
} else {
|
||||
incl_len = orig_len = 8;
|
||||
}
|
||||
fwrite(&incl_len,sizeof(incl_len),1,pFile);
|
||||
fwrite(&orig_len,sizeof(orig_len),1,pFile);
|
||||
fwrite(&incl_len, sizeof(incl_len), 1, pFile);
|
||||
fwrite(&orig_len, sizeof(orig_len), 1, pFile);
|
||||
header[0] = 0x55;
|
||||
header[1] = 0xFF;
|
||||
header[2] = mstp_port->FrameType;
|
||||
@@ -206,23 +207,24 @@ static void write_received_packet(
|
||||
header[5] = HI_BYTE(mstp_port->DataLength);
|
||||
header[6] = LO_BYTE(mstp_port->DataLength);
|
||||
header[7] = mstp_port->HeaderCRCActual;
|
||||
fwrite(header,sizeof(header),1,pFile);
|
||||
fwrite(header, sizeof(header), 1, pFile);
|
||||
if (mstp_port->DataLength) {
|
||||
fwrite(mstp_port->InputBuffer,mstp_port->DataLength,1,pFile);
|
||||
fwrite(&(mstp_port->DataCRCActualMSB),1,1,pFile);
|
||||
fwrite(&(mstp_port->DataCRCActualLSB),1,1,pFile);
|
||||
fwrite(mstp_port->InputBuffer, mstp_port->DataLength, 1, pFile);
|
||||
fwrite(&(mstp_port->DataCRCActualMSB), 1, 1, pFile);
|
||||
fwrite(&(mstp_port->DataCRCActualLSB), 1, 1, pFile);
|
||||
}
|
||||
} else {
|
||||
fprintf(stderr,"rx_fsm: failed to open %s: %s\n",
|
||||
Capture_Filename, strerror(errno));
|
||||
fprintf(stderr, "rx_fsm: failed to open %s: %s\n", Capture_Filename,
|
||||
strerror(errno));
|
||||
}
|
||||
}
|
||||
|
||||
static void cleanup(void)
|
||||
static void cleanup(
|
||||
void)
|
||||
{
|
||||
if (pFile) {
|
||||
fflush(pFile); /* stream pointer */
|
||||
fclose(pFile); /* stream pointer */
|
||||
fflush(pFile); /* stream pointer */
|
||||
fclose(pFile); /* stream pointer */
|
||||
}
|
||||
pFile = NULL;
|
||||
}
|
||||
@@ -262,15 +264,17 @@ static void print_received_packet(
|
||||
}
|
||||
#endif
|
||||
|
||||
static void sig_int(int signo)
|
||||
static void sig_int(
|
||||
int signo)
|
||||
{
|
||||
(void)signo;
|
||||
(void) signo;
|
||||
|
||||
cleanup();
|
||||
exit(0);
|
||||
}
|
||||
|
||||
void signal_init(void)
|
||||
void signal_init(
|
||||
void)
|
||||
{
|
||||
signal(SIGINT, sig_int);
|
||||
signal(SIGHUP, sig_int);
|
||||
@@ -319,7 +323,7 @@ int main(
|
||||
rc = pthread_create(&hThread, NULL, milliseconds_task, NULL);
|
||||
atexit(cleanup);
|
||||
write_global_header();
|
||||
fflush(pFile); /* stream pointer */
|
||||
fflush(pFile); /* stream pointer */
|
||||
/* run forever */
|
||||
for (;;) {
|
||||
RS485_Check_UART_Data(mstp_port);
|
||||
@@ -331,7 +335,7 @@ int main(
|
||||
print_received_packet(mstp_port);
|
||||
#endif
|
||||
write_received_packet(mstp_port);
|
||||
fflush(pFile); /* stream pointer */
|
||||
fflush(pFile); /* stream pointer */
|
||||
} else if (mstp_port->ReceivedInvalidFrame) {
|
||||
mstp_port->ReceivedInvalidFrame = false;
|
||||
fprintf(stderr, "ReceivedInvalidFrame\n");
|
||||
@@ -339,7 +343,7 @@ int main(
|
||||
print_received_packet(mstp_port);
|
||||
#endif
|
||||
write_received_packet(mstp_port);
|
||||
fflush(pFile); /* stream pointer */
|
||||
fflush(pFile); /* stream pointer */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user