Indented with indent.sh script.
This commit is contained in:
@@ -117,61 +117,61 @@ uint16_t MSTP_Get_Reply(
|
|||||||
}
|
}
|
||||||
|
|
||||||
static const char *Capture_Filename = "mstp.cap";
|
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 */
|
/* 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 */
|
uint32_t magic_number = 0xa1b2c3d4; /* magic number */
|
||||||
uint16_t version_major = 2; /* major version number */
|
uint16_t version_major = 2; /* major version number */
|
||||||
uint16_t version_minor = 4; /* minor version number */
|
uint16_t version_minor = 4; /* minor version number */
|
||||||
int32_t thiszone = 0; /* GMT to local correction */
|
int32_t thiszone = 0; /* GMT to local correction */
|
||||||
uint32_t sigfigs = 0; /* accuracy of timestamps */
|
uint32_t sigfigs = 0; /* accuracy of timestamps */
|
||||||
uint32_t snaplen = 65535; /* max length of captured packets, in octets */
|
uint32_t snaplen = 65535; /* max length of captured packets, in octets */
|
||||||
uint32_t network = 165; /* data link type - BACNET_MS_TP */
|
uint32_t network = 165; /* data link type - BACNET_MS_TP */
|
||||||
|
|
||||||
/* create a new file. */
|
/* create a new file. */
|
||||||
pFile = fopen(Capture_Filename, "wb");
|
pFile = fopen(Capture_Filename, "wb");
|
||||||
if (pFile) {
|
if (pFile) {
|
||||||
fwrite(&magic_number,sizeof(magic_number),1,pFile);
|
fwrite(&magic_number, sizeof(magic_number), 1, pFile);
|
||||||
fwrite(&version_major,sizeof(version_major),1,pFile);
|
fwrite(&version_major, sizeof(version_major), 1, pFile);
|
||||||
fwrite(&version_minor,sizeof(version_minor),1,pFile);
|
fwrite(&version_minor, sizeof(version_minor), 1, pFile);
|
||||||
fwrite(&thiszone,sizeof(thiszone),1,pFile);
|
fwrite(&thiszone, sizeof(thiszone), 1, pFile);
|
||||||
fwrite(&sigfigs,sizeof(sigfigs),1,pFile);
|
fwrite(&sigfigs, sizeof(sigfigs), 1, pFile);
|
||||||
fwrite(&snaplen,sizeof(snaplen),1,pFile);
|
fwrite(&snaplen, sizeof(snaplen), 1, pFile);
|
||||||
fwrite(&network,sizeof(network),1,pFile);
|
fwrite(&network, sizeof(network), 1, pFile);
|
||||||
fflush(pFile);
|
fflush(pFile);
|
||||||
fprintf(stdout,"mstpcap: saving capture to %s\n",
|
fprintf(stdout, "mstpcap: saving capture to %s\n", Capture_Filename);
|
||||||
Capture_Filename);
|
|
||||||
} else {
|
} else {
|
||||||
fprintf(stderr,"mstpcap: failed to open %s: %s\n",
|
fprintf(stderr, "mstpcap: failed to open %s: %s\n", Capture_Filename,
|
||||||
Capture_Filename, strerror(errno));
|
strerror(errno));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void write_received_packet(
|
static void write_received_packet(
|
||||||
volatile struct mstp_port_struct_t *mstp_port)
|
volatile struct mstp_port_struct_t *mstp_port)
|
||||||
{
|
{
|
||||||
uint32_t ts_sec; /* timestamp seconds */
|
uint32_t ts_sec; /* timestamp seconds */
|
||||||
uint32_t ts_usec; /* timestamp microseconds */
|
uint32_t ts_usec; /* timestamp microseconds */
|
||||||
uint32_t incl_len; /* number of octets of packet saved in file */
|
uint32_t incl_len; /* number of octets of packet saved in file */
|
||||||
uint32_t orig_len; /* actual length of packet */
|
uint32_t orig_len; /* actual length of packet */
|
||||||
uint8_t header[8]; /* MS/TP header */
|
uint8_t header[8]; /* MS/TP header */
|
||||||
struct timeval tv;
|
struct timeval tv;
|
||||||
|
|
||||||
if (pFile) {
|
if (pFile) {
|
||||||
gettimeofday(&tv, NULL);
|
gettimeofday(&tv, NULL);
|
||||||
ts_sec = tv.tv_sec;
|
ts_sec = tv.tv_sec;
|
||||||
ts_usec = tv.tv_usec;
|
ts_usec = tv.tv_usec;
|
||||||
fwrite(&ts_sec,sizeof(ts_sec),1,pFile);
|
fwrite(&ts_sec, sizeof(ts_sec), 1, pFile);
|
||||||
fwrite(&ts_usec,sizeof(ts_usec),1,pFile);
|
fwrite(&ts_usec, sizeof(ts_usec), 1, pFile);
|
||||||
if (mstp_port->DataLength) {
|
if (mstp_port->DataLength) {
|
||||||
incl_len = orig_len = 8 + mstp_port->DataLength + 2;
|
incl_len = orig_len = 8 + mstp_port->DataLength + 2;
|
||||||
} else {
|
} else {
|
||||||
incl_len = orig_len = 8;
|
incl_len = orig_len = 8;
|
||||||
}
|
}
|
||||||
fwrite(&incl_len,sizeof(incl_len),1,pFile);
|
fwrite(&incl_len, sizeof(incl_len), 1, pFile);
|
||||||
fwrite(&orig_len,sizeof(orig_len),1,pFile);
|
fwrite(&orig_len, sizeof(orig_len), 1, pFile);
|
||||||
header[0] = 0x55;
|
header[0] = 0x55;
|
||||||
header[1] = 0xFF;
|
header[1] = 0xFF;
|
||||||
header[2] = mstp_port->FrameType;
|
header[2] = mstp_port->FrameType;
|
||||||
@@ -180,37 +180,40 @@ static void write_received_packet(
|
|||||||
header[5] = HI_BYTE(mstp_port->DataLength);
|
header[5] = HI_BYTE(mstp_port->DataLength);
|
||||||
header[6] = LO_BYTE(mstp_port->DataLength);
|
header[6] = LO_BYTE(mstp_port->DataLength);
|
||||||
header[7] = mstp_port->HeaderCRCActual;
|
header[7] = mstp_port->HeaderCRCActual;
|
||||||
fwrite(header,sizeof(header),1,pFile);
|
fwrite(header, sizeof(header), 1, pFile);
|
||||||
if (mstp_port->DataLength) {
|
if (mstp_port->DataLength) {
|
||||||
fwrite(mstp_port->InputBuffer,mstp_port->DataLength,1,pFile);
|
fwrite(mstp_port->InputBuffer, mstp_port->DataLength, 1, pFile);
|
||||||
fwrite((char *)&mstp_port->DataCRCActualMSB,1,1,pFile);
|
fwrite((char *) &mstp_port->DataCRCActualMSB, 1, 1, pFile);
|
||||||
fwrite((char *)&mstp_port->DataCRCActualLSB,1,1,pFile);
|
fwrite((char *) &mstp_port->DataCRCActualLSB, 1, 1, pFile);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
fprintf(stderr,"mstpcap: failed to open %s: %s\n",
|
fprintf(stderr, "mstpcap: failed to open %s: %s\n", Capture_Filename,
|
||||||
Capture_Filename, strerror(errno));
|
strerror(errno));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void cleanup(void)
|
static void cleanup(
|
||||||
|
void)
|
||||||
{
|
{
|
||||||
if (pFile) {
|
if (pFile) {
|
||||||
fflush(pFile); /* stream pointer */
|
fflush(pFile); /* stream pointer */
|
||||||
fclose(pFile); /* stream pointer */
|
fclose(pFile); /* stream pointer */
|
||||||
}
|
}
|
||||||
pFile = NULL;
|
pFile = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
#if (!defined(_WIN32))
|
#if (!defined(_WIN32))
|
||||||
static void sig_int(int signo)
|
static void sig_int(
|
||||||
|
int signo)
|
||||||
{
|
{
|
||||||
(void)signo;
|
(void) signo;
|
||||||
|
|
||||||
cleanup();
|
cleanup();
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
void signal_init(void)
|
void signal_init(
|
||||||
|
void)
|
||||||
{
|
{
|
||||||
signal(SIGINT, sig_int);
|
signal(SIGINT, sig_int);
|
||||||
signal(SIGHUP, sig_int);
|
signal(SIGHUP, sig_int);
|
||||||
@@ -256,8 +259,8 @@ int main(
|
|||||||
MSTP_Port.SilenceTimerReset = Timer_Silence_Reset;
|
MSTP_Port.SilenceTimerReset = Timer_Silence_Reset;
|
||||||
MSTP_Init(mstp_port);
|
MSTP_Init(mstp_port);
|
||||||
mstp_port->Lurking = true;
|
mstp_port->Lurking = true;
|
||||||
fprintf(stdout,"mstpcap: Using %s for capture at %ld bps.\n",
|
fprintf(stdout, "mstpcap: Using %s for capture at %ld bps.\n",
|
||||||
RS485_Interface(), (long)RS485_Get_Baud_Rate());
|
RS485_Interface(), (long) RS485_Get_Baud_Rate());
|
||||||
#if defined(_WIN32)
|
#if defined(_WIN32)
|
||||||
hThread = _beginthread(milliseconds_task, 4096, &arg_value);
|
hThread = _beginthread(milliseconds_task, 4096, &arg_value);
|
||||||
if (hThread == 0) {
|
if (hThread == 0) {
|
||||||
@@ -288,7 +291,7 @@ int main(
|
|||||||
packet_count++;
|
packet_count++;
|
||||||
}
|
}
|
||||||
if (!(packet_count % 100)) {
|
if (!(packet_count % 100)) {
|
||||||
fprintf(stdout,"\r%hu packets",packet_count);
|
fprintf(stdout, "\r%hu packets", packet_count);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -212,8 +212,8 @@ int main(int argc, char *argv[]) {
|
|||||||
filename_remove_path(argv[0]), filename_remove_path(argv[0]));
|
filename_remove_path(argv[0]), filename_remove_path(argv[0]));
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
/* decode the command line parameters */
|
/* decode the command line parameters */ cov_data.
|
||||||
cov_data.subscriberProcessIdentifier = strtol(argv[1], NULL, 0);
|
subscriberProcessIdentifier = strtol(argv[1], NULL, 0);
|
||||||
cov_data.initiatingDeviceIdentifier = strtol(argv[2], NULL, 0);
|
cov_data.initiatingDeviceIdentifier = strtol(argv[2], NULL, 0);
|
||||||
cov_data.monitoredObjectIdentifier.type = strtol(argv[3], NULL, 0);
|
cov_data.monitoredObjectIdentifier.type = strtol(argv[3], NULL, 0);
|
||||||
cov_data.monitoredObjectIdentifier.instance = strtol(argv[4], NULL, 0);
|
cov_data.monitoredObjectIdentifier.instance = strtol(argv[4], NULL, 0);
|
||||||
|
|||||||
@@ -127,7 +127,8 @@ int dlmstp_send_pdu(
|
|||||||
packet.pdu_len = pdu_len;
|
packet.pdu_len = pdu_len;
|
||||||
memmove(&packet.pdu[0], &pdu[0], pdu_len);
|
memmove(&packet.pdu[0], &pdu[0], pdu_len);
|
||||||
memmove(&packet.address, dest, sizeof(packet.address));
|
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)
|
if (rc > 0)
|
||||||
bytes_sent = rc;
|
bytes_sent = rc;
|
||||||
}
|
}
|
||||||
@@ -146,10 +147,10 @@ uint16_t dlmstp_receive(
|
|||||||
uint16_t pdu_len = 0;
|
uint16_t pdu_len = 0;
|
||||||
mqd_t received_bytes = 0;
|
mqd_t received_bytes = 0;
|
||||||
DLMSTP_PACKET packet;
|
DLMSTP_PACKET packet;
|
||||||
struct timespec queue_timeout = {0};
|
struct timespec queue_timeout = { 0 };
|
||||||
time_t epoch_time = 0;
|
time_t epoch_time = 0;
|
||||||
unsigned msg_prio = 0;
|
unsigned msg_prio = 0;
|
||||||
char buffer[sizeof(struct dlmstp_packet)+1];
|
char buffer[sizeof(struct dlmstp_packet) + 1];
|
||||||
|
|
||||||
if (NPDU_Receive_Queue == -1) {
|
if (NPDU_Receive_Queue == -1) {
|
||||||
return 0;
|
return 0;
|
||||||
@@ -168,11 +169,8 @@ uint16_t dlmstp_receive(
|
|||||||
epoch_time = time(NULL);
|
epoch_time = time(NULL);
|
||||||
queue_timeout.tv_sec += epoch_time;
|
queue_timeout.tv_sec += epoch_time;
|
||||||
|
|
||||||
received_bytes = mq_timedreceive(
|
received_bytes =
|
||||||
NPDU_Receive_Queue,
|
mq_timedreceive(NPDU_Receive_Queue, buffer, sizeof(buffer), &msg_prio,
|
||||||
buffer,
|
|
||||||
sizeof(buffer),
|
|
||||||
&msg_prio,
|
|
||||||
&queue_timeout);
|
&queue_timeout);
|
||||||
|
|
||||||
/* See if there is a problem */
|
/* See if there is a problem */
|
||||||
@@ -182,8 +180,7 @@ uint16_t dlmstp_receive(
|
|||||||
/* was immediately available for reading. */
|
/* was immediately available for reading. */
|
||||||
if ((errno != EAGAIN) && (errno != ETIMEDOUT)) {
|
if ((errno != EAGAIN) && (errno != ETIMEDOUT)) {
|
||||||
#if PRINT_ENABLED
|
#if PRINT_ENABLED
|
||||||
fprintf(stderr, "MS/TP: NPDU Receive: %s\n",
|
fprintf(stderr, "MS/TP: NPDU Receive: %s\n", strerror(errno));
|
||||||
strerror(errno));
|
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
@@ -193,7 +190,7 @@ uint16_t dlmstp_receive(
|
|||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
/* copy the buffer into the PDU */
|
/* copy the buffer into the PDU */
|
||||||
memmove(&packet,buffer,sizeof(packet));
|
memmove(&packet, buffer, sizeof(packet));
|
||||||
pdu_len = packet.pdu_len;
|
pdu_len = packet.pdu_len;
|
||||||
memmove(&pdu[0], &packet.pdu[0], pdu_len);
|
memmove(&pdu[0], &packet.pdu[0], pdu_len);
|
||||||
memmove(src, &packet.address, sizeof(packet.address));
|
memmove(src, &packet.address, sizeof(packet.address));
|
||||||
@@ -285,7 +282,7 @@ uint16_t MSTP_Put_Receive(
|
|||||||
pdu_len = sizeof(packet.pdu);
|
pdu_len = sizeof(packet.pdu);
|
||||||
if (pdu_len) {
|
if (pdu_len) {
|
||||||
#if PRINT_ENABLED
|
#if PRINT_ENABLED
|
||||||
fprintf(stderr,"MSTP: packet from FSM.\n");
|
fprintf(stderr, "MSTP: packet from FSM.\n");
|
||||||
#endif
|
#endif
|
||||||
MSTP_Packets++;
|
MSTP_Packets++;
|
||||||
memmove(&packet.pdu[0], (void *) &mstp_port->InputBuffer[0], pdu_len);
|
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;
|
packet.pdu_len = pdu_len;
|
||||||
/* ready is not used in this scheme */
|
/* ready is not used in this scheme */
|
||||||
packet.ready = true;
|
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;
|
return pdu_len;
|
||||||
@@ -306,10 +303,10 @@ int dlmstp_get_transmit_packet(
|
|||||||
unsigned timeout)
|
unsigned timeout)
|
||||||
{ /* milliseconds to wait for a packet */
|
{ /* milliseconds to wait for a packet */
|
||||||
int received_bytes = 0; /* return value */
|
int received_bytes = 0; /* return value */
|
||||||
struct timespec queue_timeout = {0};
|
struct timespec queue_timeout = { 0 };
|
||||||
time_t epoch_time = 0;
|
time_t epoch_time = 0;
|
||||||
unsigned msg_prio = 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 */
|
/* Make sure the socket is open */
|
||||||
if (NPDU_Transmit_Queue == -1)
|
if (NPDU_Transmit_Queue == -1)
|
||||||
@@ -327,11 +324,8 @@ int dlmstp_get_transmit_packet(
|
|||||||
epoch_time = time(NULL);
|
epoch_time = time(NULL);
|
||||||
queue_timeout.tv_sec += epoch_time;
|
queue_timeout.tv_sec += epoch_time;
|
||||||
|
|
||||||
received_bytes = mq_timedreceive(
|
received_bytes =
|
||||||
NPDU_Transmit_Queue,
|
mq_timedreceive(NPDU_Transmit_Queue, buffer, sizeof(buffer), &msg_prio,
|
||||||
buffer,
|
|
||||||
sizeof(buffer),
|
|
||||||
&msg_prio,
|
|
||||||
&queue_timeout);
|
&queue_timeout);
|
||||||
|
|
||||||
/* See if there is a problem */
|
/* See if there is a problem */
|
||||||
@@ -341,13 +335,14 @@ int dlmstp_get_transmit_packet(
|
|||||||
/* was immediately available for reading. */
|
/* was immediately available for reading. */
|
||||||
if ((errno != EAGAIN) && (errno != ETIMEDOUT)) {
|
if ((errno != EAGAIN) && (errno != ETIMEDOUT)) {
|
||||||
#if PRINT_ENABLED
|
#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));
|
strerror(errno));
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
memmove(packet,buffer,sizeof(packet));
|
memmove(packet, buffer, sizeof(packet));
|
||||||
|
|
||||||
return (received_bytes);
|
return (received_bytes);
|
||||||
}
|
}
|
||||||
@@ -376,7 +371,7 @@ uint16_t MSTP_Get_Send(
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
#if PRINT_ENABLED
|
#if PRINT_ENABLED
|
||||||
fprintf(stderr,"MS/TP: sending packet to FSM.\n");
|
fprintf(stderr, "MS/TP: sending packet to FSM.\n");
|
||||||
#endif
|
#endif
|
||||||
/* convert the PDU into the MSTP Frame */
|
/* convert the PDU into the MSTP Frame */
|
||||||
pdu_len = MSTP_Create_Frame(&mstp_port->OutputBuffer[0], /* <-- loading this */
|
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? */
|
/* is this the reply to the DER? */
|
||||||
matched =
|
matched =
|
||||||
dlmstp_compare_data_expecting_reply(&mstp_port->InputBuffer[0],
|
dlmstp_compare_data_expecting_reply(&mstp_port->InputBuffer[0],
|
||||||
mstp_port->DataLength, mstp_port->SourceAddress,
|
mstp_port->DataLength, mstp_port->SourceAddress, &packet.pdu[0],
|
||||||
&packet.pdu[0], packet.pdu_len,
|
packet.pdu_len, &packet.address);
|
||||||
&packet.address);
|
|
||||||
if (matched) {
|
if (matched) {
|
||||||
#if PRINT_ENABLED
|
#if PRINT_ENABLED
|
||||||
fprintf(stderr,"MSTP: sending packet to FSM.\n");
|
fprintf(stderr, "MSTP: sending packet to FSM.\n");
|
||||||
#endif
|
#endif
|
||||||
/* convert the PDU into the MSTP Frame */
|
/* convert the PDU into the MSTP Frame */
|
||||||
pdu_len = MSTP_Create_Frame(&mstp_port->OutputBuffer[0],
|
pdu_len =
|
||||||
mstp_port->OutputBufferSize, packet.frame_type,
|
MSTP_Create_Frame(&mstp_port->OutputBuffer[0],
|
||||||
destination, mstp_port->This_Station, &packet.pdu[0],
|
mstp_port->OutputBufferSize, packet.frame_type, destination,
|
||||||
packet.pdu_len);
|
mstp_port->This_Station, &packet.pdu[0], packet.pdu_len);
|
||||||
/* not used here, but setting it anyway */
|
/* not used here, but setting it anyway */
|
||||||
packet.ready = false;
|
packet.ready = false;
|
||||||
} else {
|
} else {
|
||||||
/* put it back into the queue */
|
/* 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;
|
return pdu_len;
|
||||||
@@ -687,27 +682,23 @@ bool dlmstp_init(
|
|||||||
char mqname[32];
|
char mqname[32];
|
||||||
struct mq_attr mqattr;
|
struct mq_attr mqattr;
|
||||||
|
|
||||||
mqattr.mq_flags = 0;
|
mqattr.mq_flags = 0;
|
||||||
mqattr.mq_maxmsg = 5;
|
mqattr.mq_maxmsg = 5;
|
||||||
mqattr.mq_msgsize = sizeof(struct dlmstp_packet);
|
mqattr.mq_msgsize = sizeof(struct dlmstp_packet);
|
||||||
/* create a queue for the NDPU data between MS/TP threads */
|
/* create a queue for the NDPU data between MS/TP threads */
|
||||||
snprintf(mqname, sizeof(mqname), "/MSTP_Rx_%d", getpid());
|
snprintf(mqname, sizeof(mqname), "/MSTP_Rx_%d", getpid());
|
||||||
NPDU_Transmit_Queue = mq_open(mqname,
|
NPDU_Transmit_Queue = mq_open(mqname, O_RDWR | O_CREAT, 0600, &mqattr);
|
||||||
O_RDWR | O_CREAT, 0600, &mqattr);
|
|
||||||
if (NPDU_Transmit_Queue == -1) {
|
if (NPDU_Transmit_Queue == -1) {
|
||||||
#if PRINT_ENABLED
|
#if PRINT_ENABLED
|
||||||
fprintf(stderr, "MS/TP: Create NPDU Transmit Queue %s: %s\n",
|
fprintf(stderr, "MS/TP: Create NPDU Transmit Queue %s: %s\n", mqname,
|
||||||
mqname,
|
|
||||||
strerror(errno));
|
strerror(errno));
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
snprintf(mqname, sizeof(mqname), "/MSTP_Tx_%d", getpid());
|
snprintf(mqname, sizeof(mqname), "/MSTP_Tx_%d", getpid());
|
||||||
NPDU_Receive_Queue = mq_open(mqname,
|
NPDU_Receive_Queue = mq_open(mqname, O_RDWR | O_CREAT, 0600, &mqattr);
|
||||||
O_RDWR | O_CREAT, 0600, &mqattr);
|
|
||||||
if (NPDU_Receive_Queue == -1) {
|
if (NPDU_Receive_Queue == -1) {
|
||||||
#if PRINT_ENABLED
|
#if PRINT_ENABLED
|
||||||
fprintf(stderr, "MS/TP: Create NPDU Receive Queue %s: %s\n",
|
fprintf(stderr, "MS/TP: Create NPDU Receive Queue %s: %s\n", mqname,
|
||||||
mqname,
|
|
||||||
strerror(errno));
|
strerror(errno));
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -91,7 +91,8 @@ void RS485_Set_Interface(
|
|||||||
* ALGORITHM: none
|
* ALGORITHM: none
|
||||||
* NOTES: none
|
* NOTES: none
|
||||||
*********************************************************************/
|
*********************************************************************/
|
||||||
const char *RS485_Interface(void)
|
const char *RS485_Interface(
|
||||||
|
void)
|
||||||
{
|
{
|
||||||
return RS485_Port_Name;
|
return RS485_Port_Name;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -45,7 +45,8 @@ extern "C" {
|
|||||||
|
|
||||||
void RS485_Set_Interface(
|
void RS485_Set_Interface(
|
||||||
char *ifname);
|
char *ifname);
|
||||||
const char * RS485_Interface(void);
|
const char *RS485_Interface(
|
||||||
|
void);
|
||||||
|
|
||||||
void RS485_Initialize(
|
void RS485_Initialize(
|
||||||
void);
|
void);
|
||||||
|
|||||||
@@ -45,7 +45,7 @@
|
|||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
#include <signal.h> /* signal handling functions */
|
#include <signal.h> /* signal handling functions */
|
||||||
|
|
||||||
/* local includes */
|
/* local includes */
|
||||||
#include "bytes.h"
|
#include "bytes.h"
|
||||||
@@ -146,58 +146,59 @@ int timestamp_ms(
|
|||||||
}
|
}
|
||||||
|
|
||||||
static const char *Capture_Filename = "mstp.cap";
|
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 */
|
/* 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 */
|
uint32_t magic_number = 0xa1b2c3d4; /* magic number */
|
||||||
uint16_t version_major = 2; /* major version number */
|
uint16_t version_major = 2; /* major version number */
|
||||||
uint16_t version_minor = 4; /* minor version number */
|
uint16_t version_minor = 4; /* minor version number */
|
||||||
int32_t thiszone = 0; /* GMT to local correction */
|
int32_t thiszone = 0; /* GMT to local correction */
|
||||||
uint32_t sigfigs = 0; /* accuracy of timestamps */
|
uint32_t sigfigs = 0; /* accuracy of timestamps */
|
||||||
uint32_t snaplen = 65535; /* max length of captured packets, in octets */
|
uint32_t snaplen = 65535; /* max length of captured packets, in octets */
|
||||||
uint32_t network = 165; /* data link type - BACNET_MS_TP */
|
uint32_t network = 165; /* data link type - BACNET_MS_TP */
|
||||||
|
|
||||||
/* create a new file. */
|
/* create a new file. */
|
||||||
pFile = fopen(Capture_Filename, "wb");
|
pFile = fopen(Capture_Filename, "wb");
|
||||||
if (pFile) {
|
if (pFile) {
|
||||||
fwrite(&magic_number,sizeof(magic_number),1,pFile);
|
fwrite(&magic_number, sizeof(magic_number), 1, pFile);
|
||||||
fwrite(&version_major,sizeof(version_major),1,pFile);
|
fwrite(&version_major, sizeof(version_major), 1, pFile);
|
||||||
fwrite(&version_minor,sizeof(version_minor),1,pFile);
|
fwrite(&version_minor, sizeof(version_minor), 1, pFile);
|
||||||
fwrite(&thiszone,sizeof(thiszone),1,pFile);
|
fwrite(&thiszone, sizeof(thiszone), 1, pFile);
|
||||||
fwrite(&sigfigs,sizeof(sigfigs),1,pFile);
|
fwrite(&sigfigs, sizeof(sigfigs), 1, pFile);
|
||||||
fwrite(&snaplen,sizeof(snaplen),1,pFile);
|
fwrite(&snaplen, sizeof(snaplen), 1, pFile);
|
||||||
fwrite(&network,sizeof(network),1,pFile);
|
fwrite(&network, sizeof(network), 1, pFile);
|
||||||
} else {
|
} else {
|
||||||
fprintf(stderr,"rx_fsm: failed to open %s: %s\n",
|
fprintf(stderr, "rx_fsm: failed to open %s: %s\n", Capture_Filename,
|
||||||
Capture_Filename, strerror(errno));
|
strerror(errno));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void write_received_packet(
|
static void write_received_packet(
|
||||||
volatile struct mstp_port_struct_t *mstp_port)
|
volatile struct mstp_port_struct_t *mstp_port)
|
||||||
{
|
{
|
||||||
uint32_t ts_sec; /* timestamp seconds */
|
uint32_t ts_sec; /* timestamp seconds */
|
||||||
uint32_t ts_usec; /* timestamp microseconds */
|
uint32_t ts_usec; /* timestamp microseconds */
|
||||||
uint32_t incl_len; /* number of octets of packet saved in file */
|
uint32_t incl_len; /* number of octets of packet saved in file */
|
||||||
uint32_t orig_len; /* actual length of packet */
|
uint32_t orig_len; /* actual length of packet */
|
||||||
uint8_t header[8]; /* MS/TP header */
|
uint8_t header[8]; /* MS/TP header */
|
||||||
struct timeval tv;
|
struct timeval tv;
|
||||||
|
|
||||||
if (pFile) {
|
if (pFile) {
|
||||||
gettimeofday(&tv, NULL);
|
gettimeofday(&tv, NULL);
|
||||||
ts_sec = tv.tv_sec;
|
ts_sec = tv.tv_sec;
|
||||||
ts_usec = tv.tv_usec;
|
ts_usec = tv.tv_usec;
|
||||||
fwrite(&ts_sec,sizeof(ts_sec),1,pFile);
|
fwrite(&ts_sec, sizeof(ts_sec), 1, pFile);
|
||||||
fwrite(&ts_usec,sizeof(ts_usec),1,pFile);
|
fwrite(&ts_usec, sizeof(ts_usec), 1, pFile);
|
||||||
if (mstp_port->DataLength) {
|
if (mstp_port->DataLength) {
|
||||||
incl_len = orig_len = 8 + mstp_port->DataLength + 2;
|
incl_len = orig_len = 8 + mstp_port->DataLength + 2;
|
||||||
} else {
|
} else {
|
||||||
incl_len = orig_len = 8;
|
incl_len = orig_len = 8;
|
||||||
}
|
}
|
||||||
fwrite(&incl_len,sizeof(incl_len),1,pFile);
|
fwrite(&incl_len, sizeof(incl_len), 1, pFile);
|
||||||
fwrite(&orig_len,sizeof(orig_len),1,pFile);
|
fwrite(&orig_len, sizeof(orig_len), 1, pFile);
|
||||||
header[0] = 0x55;
|
header[0] = 0x55;
|
||||||
header[1] = 0xFF;
|
header[1] = 0xFF;
|
||||||
header[2] = mstp_port->FrameType;
|
header[2] = mstp_port->FrameType;
|
||||||
@@ -206,23 +207,24 @@ static void write_received_packet(
|
|||||||
header[5] = HI_BYTE(mstp_port->DataLength);
|
header[5] = HI_BYTE(mstp_port->DataLength);
|
||||||
header[6] = LO_BYTE(mstp_port->DataLength);
|
header[6] = LO_BYTE(mstp_port->DataLength);
|
||||||
header[7] = mstp_port->HeaderCRCActual;
|
header[7] = mstp_port->HeaderCRCActual;
|
||||||
fwrite(header,sizeof(header),1,pFile);
|
fwrite(header, sizeof(header), 1, pFile);
|
||||||
if (mstp_port->DataLength) {
|
if (mstp_port->DataLength) {
|
||||||
fwrite(mstp_port->InputBuffer,mstp_port->DataLength,1,pFile);
|
fwrite(mstp_port->InputBuffer, mstp_port->DataLength, 1, pFile);
|
||||||
fwrite(&(mstp_port->DataCRCActualMSB),1,1,pFile);
|
fwrite(&(mstp_port->DataCRCActualMSB), 1, 1, pFile);
|
||||||
fwrite(&(mstp_port->DataCRCActualLSB),1,1,pFile);
|
fwrite(&(mstp_port->DataCRCActualLSB), 1, 1, pFile);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
fprintf(stderr,"rx_fsm: failed to open %s: %s\n",
|
fprintf(stderr, "rx_fsm: failed to open %s: %s\n", Capture_Filename,
|
||||||
Capture_Filename, strerror(errno));
|
strerror(errno));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void cleanup(void)
|
static void cleanup(
|
||||||
|
void)
|
||||||
{
|
{
|
||||||
if (pFile) {
|
if (pFile) {
|
||||||
fflush(pFile); /* stream pointer */
|
fflush(pFile); /* stream pointer */
|
||||||
fclose(pFile); /* stream pointer */
|
fclose(pFile); /* stream pointer */
|
||||||
}
|
}
|
||||||
pFile = NULL;
|
pFile = NULL;
|
||||||
}
|
}
|
||||||
@@ -262,15 +264,17 @@ static void print_received_packet(
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
static void sig_int(int signo)
|
static void sig_int(
|
||||||
|
int signo)
|
||||||
{
|
{
|
||||||
(void)signo;
|
(void) signo;
|
||||||
|
|
||||||
cleanup();
|
cleanup();
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
void signal_init(void)
|
void signal_init(
|
||||||
|
void)
|
||||||
{
|
{
|
||||||
signal(SIGINT, sig_int);
|
signal(SIGINT, sig_int);
|
||||||
signal(SIGHUP, sig_int);
|
signal(SIGHUP, sig_int);
|
||||||
@@ -319,7 +323,7 @@ int main(
|
|||||||
rc = pthread_create(&hThread, NULL, milliseconds_task, NULL);
|
rc = pthread_create(&hThread, NULL, milliseconds_task, NULL);
|
||||||
atexit(cleanup);
|
atexit(cleanup);
|
||||||
write_global_header();
|
write_global_header();
|
||||||
fflush(pFile); /* stream pointer */
|
fflush(pFile); /* stream pointer */
|
||||||
/* run forever */
|
/* run forever */
|
||||||
for (;;) {
|
for (;;) {
|
||||||
RS485_Check_UART_Data(mstp_port);
|
RS485_Check_UART_Data(mstp_port);
|
||||||
@@ -331,7 +335,7 @@ int main(
|
|||||||
print_received_packet(mstp_port);
|
print_received_packet(mstp_port);
|
||||||
#endif
|
#endif
|
||||||
write_received_packet(mstp_port);
|
write_received_packet(mstp_port);
|
||||||
fflush(pFile); /* stream pointer */
|
fflush(pFile); /* stream pointer */
|
||||||
} else if (mstp_port->ReceivedInvalidFrame) {
|
} else if (mstp_port->ReceivedInvalidFrame) {
|
||||||
mstp_port->ReceivedInvalidFrame = false;
|
mstp_port->ReceivedInvalidFrame = false;
|
||||||
fprintf(stderr, "ReceivedInvalidFrame\n");
|
fprintf(stderr, "ReceivedInvalidFrame\n");
|
||||||
@@ -339,7 +343,7 @@ int main(
|
|||||||
print_received_packet(mstp_port);
|
print_received_packet(mstp_port);
|
||||||
#endif
|
#endif
|
||||||
write_received_packet(mstp_port);
|
write_received_packet(mstp_port);
|
||||||
fflush(pFile); /* stream pointer */
|
fflush(pFile); /* stream pointer */
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -49,11 +49,13 @@ typedef HANDLE sem_t;
|
|||||||
|
|
||||||
struct timespec {
|
struct timespec {
|
||||||
time_t tv_sec; /* Seconds */
|
time_t tv_sec; /* Seconds */
|
||||||
long tv_nsec; /* Nanoseconds [0 .. 999999999] */
|
long tv_nsec; /* Nanoseconds [0 .. 999999999] */
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
static inline int gettimeofday(struct timeval *tp, void *tzp)
|
static inline int gettimeofday(
|
||||||
|
struct timeval *tp,
|
||||||
|
void *tzp)
|
||||||
{
|
{
|
||||||
struct _timeb timebuffer;
|
struct _timeb timebuffer;
|
||||||
|
|
||||||
@@ -65,20 +67,21 @@ static inline int gettimeofday(struct timeval *tp, void *tzp)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* FIXME: not a complete implementation of the posix function */
|
/* FIXME: not a complete implementation of the posix function */
|
||||||
static inline int sem_timedwait(sem_t *sem,
|
static inline int sem_timedwait(
|
||||||
|
sem_t * sem,
|
||||||
const struct timespec *abs_timeout)
|
const struct timespec *abs_timeout)
|
||||||
{
|
{
|
||||||
struct timeval tp;
|
struct timeval tp;
|
||||||
DWORD wait_status = 0;
|
DWORD wait_status = 0;
|
||||||
DWORD dwMilliseconds = (abs_timeout->tv_sec * 1000) +
|
DWORD dwMilliseconds =
|
||||||
(abs_timeout->tv_nsec / 1000);
|
(abs_timeout->tv_sec * 1000) + (abs_timeout->tv_nsec / 1000);
|
||||||
|
|
||||||
gettimeofday(&tp,NULL);
|
gettimeofday(&tp, NULL);
|
||||||
if (abs_timeout->tv_sec >= tp.tv_sec) {
|
if (abs_timeout->tv_sec >= tp.tv_sec) {
|
||||||
dwMilliseconds = (abs_timeout->tv_sec - tp.tv_sec) * 1000;
|
dwMilliseconds = (abs_timeout->tv_sec - tp.tv_sec) * 1000;
|
||||||
if (abs_timeout->tv_nsec >= (tp.tv_usec*1000)) {
|
if (abs_timeout->tv_nsec >= (tp.tv_usec * 1000)) {
|
||||||
dwMilliseconds +=
|
dwMilliseconds +=
|
||||||
((abs_timeout->tv_nsec - (tp.tv_usec*1000)) / (1000*1000));
|
((abs_timeout->tv_nsec - (tp.tv_usec * 1000)) / (1000 * 1000));
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
dwMilliseconds = 0;
|
dwMilliseconds = 0;
|
||||||
@@ -91,14 +94,16 @@ static inline int sem_timedwait(sem_t *sem,
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline int sem_init(sem_t *sem, int pshared, unsigned int value)
|
static inline int sem_init(
|
||||||
|
sem_t * sem,
|
||||||
|
int pshared,
|
||||||
|
unsigned int value)
|
||||||
{
|
{
|
||||||
(void)pshared;
|
(void) pshared;
|
||||||
*sem = CreateSemaphore(
|
*sem = CreateSemaphore(NULL /*lpSecurityDescriptor */ ,
|
||||||
NULL/*lpSecurityDescriptor*/,
|
value /* lInitialCount */ ,
|
||||||
value /* lInitialCount */,
|
1 /* lMaximumCount */ ,
|
||||||
1 /* lMaximumCount */,
|
NULL /* lpName */ );
|
||||||
NULL /* lpName */);
|
|
||||||
if ((*sem) == NULL) {
|
if ((*sem) == NULL) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@@ -106,10 +111,11 @@ static inline int sem_init(sem_t *sem, int pshared, unsigned int value)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline int nanosleep(const struct timespec *rqtp, struct timespec *rmtp)
|
static inline int nanosleep(
|
||||||
|
const struct timespec *rqtp,
|
||||||
|
struct timespec *rmtp)
|
||||||
{
|
{
|
||||||
DWORD dwMilliseconds = (rqtp->tv_sec * 1000) +
|
DWORD dwMilliseconds = (rqtp->tv_sec * 1000) + (rqtp->tv_nsec / 1000);
|
||||||
(rqtp->tv_nsec / 1000);
|
|
||||||
|
|
||||||
Sleep(dwMilliseconds);
|
Sleep(dwMilliseconds);
|
||||||
|
|
||||||
|
|||||||
@@ -97,7 +97,8 @@ void RS485_Set_Interface(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const char *RS485_Interface(void)
|
const char *RS485_Interface(
|
||||||
|
void)
|
||||||
{
|
{
|
||||||
return RS485_Port_Name;
|
return RS485_Port_Name;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -45,7 +45,8 @@ extern "C" {
|
|||||||
|
|
||||||
void RS485_Set_Interface(
|
void RS485_Set_Interface(
|
||||||
char *ifname);
|
char *ifname);
|
||||||
const char * RS485_Interface(void);
|
const char *RS485_Interface(
|
||||||
|
void);
|
||||||
|
|
||||||
void RS485_Initialize(
|
void RS485_Initialize(
|
||||||
void);
|
void);
|
||||||
|
|||||||
@@ -55,20 +55,20 @@
|
|||||||
/* file format for libpcap/winpcap */
|
/* file format for libpcap/winpcap */
|
||||||
/* from http://wiki.wireshark.org/Development/LibpcapFileFormat */
|
/* from http://wiki.wireshark.org/Development/LibpcapFileFormat */
|
||||||
typedef struct pcap_hdr_s {
|
typedef struct pcap_hdr_s {
|
||||||
uint32_t magic_number; /* magic number */
|
uint32_t magic_number; /* magic number */
|
||||||
uint16_t version_major; /* major version number */
|
uint16_t version_major; /* major version number */
|
||||||
uint16_t version_minor; /* minor version number */
|
uint16_t version_minor; /* minor version number */
|
||||||
int32_t thiszone; /* GMT to local correction */
|
int32_t thiszone; /* GMT to local correction */
|
||||||
uint32_t sigfigs; /* accuracy of timestamps */
|
uint32_t sigfigs; /* accuracy of timestamps */
|
||||||
uint32_t snaplen; /* max length of captured packets, in octets */
|
uint32_t snaplen; /* max length of captured packets, in octets */
|
||||||
uint32_t network; /* data link type */
|
uint32_t network; /* data link type */
|
||||||
} pcap_hdr_t;
|
} pcap_hdr_t;
|
||||||
|
|
||||||
typedef struct pcaprec_hdr_s {
|
typedef struct pcaprec_hdr_s {
|
||||||
uint32_t ts_sec; /* timestamp seconds */
|
uint32_t ts_sec; /* timestamp seconds */
|
||||||
uint32_t ts_usec; /* timestamp microseconds */
|
uint32_t ts_usec; /* timestamp microseconds */
|
||||||
uint32_t incl_len; /* number of octets of packet saved in file */
|
uint32_t incl_len; /* number of octets of packet saved in file */
|
||||||
uint32_t orig_len; /* actual length of packet */
|
uint32_t orig_len; /* actual length of packet */
|
||||||
} pcaprec_hdr_t;
|
} pcaprec_hdr_t;
|
||||||
|
|
||||||
/* local port data - shared with RS-485 */
|
/* local port data - shared with RS-485 */
|
||||||
@@ -169,9 +169,9 @@ static void print_received_packet(
|
|||||||
|
|
||||||
/* returns a delta timestamp */
|
/* returns a delta timestamp */
|
||||||
void timestamp(
|
void timestamp(
|
||||||
uint32_t *ts_sec, /* timestamp seconds since epoch (Unix) */
|
uint32_t * ts_sec, /* timestamp seconds since epoch (Unix) */
|
||||||
uint32_t *ts_usec) /* timestamp microseconds (unix) */
|
uint32_t * ts_usec)
|
||||||
{
|
{ /* timestamp microseconds (unix) */
|
||||||
DWORD ticks = 0;
|
DWORD ticks = 0;
|
||||||
static DWORD initial_ticks = 0;
|
static DWORD initial_ticks = 0;
|
||||||
static time_t initial_seconds = 0;
|
static time_t initial_seconds = 0;
|
||||||
@@ -185,7 +185,7 @@ void timestamp(
|
|||||||
}
|
}
|
||||||
ticks = GetTickCount();
|
ticks = GetTickCount();
|
||||||
/* how much total time has passed? */
|
/* how much total time has passed? */
|
||||||
elapsed_ticks = ticks - initial_ticks;
|
elapsed_ticks = ticks - initial_ticks;
|
||||||
seconds = elapsed_ticks / 1000;
|
seconds = elapsed_ticks / 1000;
|
||||||
milliseconds = elapsed_ticks - (seconds * 1000);
|
milliseconds = elapsed_ticks - (seconds * 1000);
|
||||||
*ts_sec = initial_seconds + seconds;
|
*ts_sec = initial_seconds + seconds;
|
||||||
@@ -195,55 +195,56 @@ void timestamp(
|
|||||||
}
|
}
|
||||||
|
|
||||||
static const char *Capture_Filename = "mstp.cap";
|
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 */
|
/* 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 */
|
uint32_t magic_number = 0xa1b2c3d4; /* magic number */
|
||||||
uint16_t version_major = 2; /* major version number */
|
uint16_t version_major = 2; /* major version number */
|
||||||
uint16_t version_minor = 4; /* minor version number */
|
uint16_t version_minor = 4; /* minor version number */
|
||||||
int32_t thiszone = 0; /* GMT to local correction */
|
int32_t thiszone = 0; /* GMT to local correction */
|
||||||
uint32_t sigfigs = 0; /* accuracy of timestamps */
|
uint32_t sigfigs = 0; /* accuracy of timestamps */
|
||||||
uint32_t snaplen = 65535; /* max length of captured packets, in octets */
|
uint32_t snaplen = 65535; /* max length of captured packets, in octets */
|
||||||
uint32_t network = 165; /* data link type */
|
uint32_t network = 165; /* data link type */
|
||||||
|
|
||||||
/* create a new file. */
|
/* create a new file. */
|
||||||
pFile = fopen(Capture_Filename, "wb");
|
pFile = fopen(Capture_Filename, "wb");
|
||||||
if (pFile) {
|
if (pFile) {
|
||||||
fwrite(&magic_number,sizeof(magic_number),1,pFile);
|
fwrite(&magic_number, sizeof(magic_number), 1, pFile);
|
||||||
fwrite(&version_major,sizeof(version_major),1,pFile);
|
fwrite(&version_major, sizeof(version_major), 1, pFile);
|
||||||
fwrite(&version_minor,sizeof(version_minor),1,pFile);
|
fwrite(&version_minor, sizeof(version_minor), 1, pFile);
|
||||||
fwrite(&thiszone,sizeof(thiszone),1,pFile);
|
fwrite(&thiszone, sizeof(thiszone), 1, pFile);
|
||||||
fwrite(&sigfigs,sizeof(sigfigs),1,pFile);
|
fwrite(&sigfigs, sizeof(sigfigs), 1, pFile);
|
||||||
fwrite(&snaplen,sizeof(snaplen),1,pFile);
|
fwrite(&snaplen, sizeof(snaplen), 1, pFile);
|
||||||
fwrite(&network,sizeof(network),1,pFile);
|
fwrite(&network, sizeof(network), 1, pFile);
|
||||||
} else {
|
} else {
|
||||||
fprintf(stderr,"rx_fsm: failed to open %s: %s\n",
|
fprintf(stderr, "rx_fsm: failed to open %s: %s\n", Capture_Filename,
|
||||||
Capture_Filename, strerror(errno));
|
strerror(errno));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void write_received_packet(
|
static void write_received_packet(
|
||||||
volatile struct mstp_port_struct_t *mstp_port)
|
volatile struct mstp_port_struct_t *mstp_port)
|
||||||
{
|
{
|
||||||
uint32_t ts_sec; /* timestamp seconds */
|
uint32_t ts_sec; /* timestamp seconds */
|
||||||
uint32_t ts_usec; /* timestamp microseconds */
|
uint32_t ts_usec; /* timestamp microseconds */
|
||||||
uint32_t incl_len; /* number of octets of packet saved in file */
|
uint32_t incl_len; /* number of octets of packet saved in file */
|
||||||
uint32_t orig_len; /* actual length of packet */
|
uint32_t orig_len; /* actual length of packet */
|
||||||
uint8_t header[8]; /* MS/TP header */
|
uint8_t header[8]; /* MS/TP header */
|
||||||
|
|
||||||
if (pFile) {
|
if (pFile) {
|
||||||
timestamp(&ts_sec, &ts_usec);
|
timestamp(&ts_sec, &ts_usec);
|
||||||
fwrite(&ts_sec,sizeof(ts_sec),1,pFile);
|
fwrite(&ts_sec, sizeof(ts_sec), 1, pFile);
|
||||||
fwrite(&ts_usec,sizeof(ts_usec),1,pFile);
|
fwrite(&ts_usec, sizeof(ts_usec), 1, pFile);
|
||||||
if (mstp_port->DataLength) {
|
if (mstp_port->DataLength) {
|
||||||
incl_len = orig_len = 8 + mstp_port->DataLength + 2;
|
incl_len = orig_len = 8 + mstp_port->DataLength + 2;
|
||||||
} else {
|
} else {
|
||||||
incl_len = orig_len = 8;
|
incl_len = orig_len = 8;
|
||||||
}
|
}
|
||||||
fwrite(&incl_len,sizeof(incl_len),1,pFile);
|
fwrite(&incl_len, sizeof(incl_len), 1, pFile);
|
||||||
fwrite(&orig_len,sizeof(orig_len),1,pFile);
|
fwrite(&orig_len, sizeof(orig_len), 1, pFile);
|
||||||
header[0] = 0x55;
|
header[0] = 0x55;
|
||||||
header[1] = 0xFF;
|
header[1] = 0xFF;
|
||||||
header[2] = mstp_port->FrameType;
|
header[2] = mstp_port->FrameType;
|
||||||
@@ -252,25 +253,26 @@ static void write_received_packet(
|
|||||||
header[5] = HI_BYTE(mstp_port->DataLength);
|
header[5] = HI_BYTE(mstp_port->DataLength);
|
||||||
header[6] = LO_BYTE(mstp_port->DataLength);
|
header[6] = LO_BYTE(mstp_port->DataLength);
|
||||||
header[7] = mstp_port->HeaderCRCActual;
|
header[7] = mstp_port->HeaderCRCActual;
|
||||||
fwrite(header,sizeof(header),1,pFile);
|
fwrite(header, sizeof(header), 1, pFile);
|
||||||
if (mstp_port->DataLength) {
|
if (mstp_port->DataLength) {
|
||||||
fwrite(mstp_port->InputBuffer,mstp_port->DataLength,1,pFile);
|
fwrite(mstp_port->InputBuffer, mstp_port->DataLength, 1, pFile);
|
||||||
fwrite(&(mstp_port->DataCRCActualMSB),1,1,pFile);
|
fwrite(&(mstp_port->DataCRCActualMSB), 1, 1, pFile);
|
||||||
fwrite(&(mstp_port->DataCRCActualLSB),1,1,pFile);
|
fwrite(&(mstp_port->DataCRCActualLSB), 1, 1, pFile);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
fprintf(stderr,"rx_fsm: failed to open %s: %s\n",
|
fprintf(stderr, "rx_fsm: failed to open %s: %s\n", Capture_Filename,
|
||||||
Capture_Filename, strerror(errno));
|
strerror(errno));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static char *Network_Interface = NULL;
|
static char *Network_Interface = NULL;
|
||||||
|
|
||||||
static void cleanup(void)
|
static void cleanup(
|
||||||
|
void)
|
||||||
{
|
{
|
||||||
if (pFile) {
|
if (pFile) {
|
||||||
fflush(pFile); /* stream pointer */
|
fflush(pFile); /* stream pointer */
|
||||||
fclose(pFile); /* stream pointer */
|
fclose(pFile); /* stream pointer */
|
||||||
}
|
}
|
||||||
pFile = NULL;
|
pFile = NULL;
|
||||||
}
|
}
|
||||||
|
|||||||
+11
-11
@@ -663,7 +663,7 @@ INDTEXT_DATA bacnet_property_names[] = {
|
|||||||
{PROP_TRIGGER, "trigger"}
|
{PROP_TRIGGER, "trigger"}
|
||||||
,
|
,
|
||||||
{PROP_UTC_TIME_SYNCHRONIZATION_RECIPIENTS,
|
{PROP_UTC_TIME_SYNCHRONIZATION_RECIPIENTS,
|
||||||
"utc-time-synchronization-recipients"}
|
"utc-time-synchronization-recipients"}
|
||||||
,
|
,
|
||||||
{PROP_NODE_SUBTYPE, "node-subtype"}
|
{PROP_NODE_SUBTYPE, "node-subtype"}
|
||||||
,
|
,
|
||||||
@@ -748,7 +748,7 @@ INDTEXT_DATA bacnet_property_names[] = {
|
|||||||
{PROP_ACCESS_EVENT, "access-event"}
|
{PROP_ACCESS_EVENT, "access-event"}
|
||||||
,
|
,
|
||||||
{PROP_ACCESS_EVENT_AUTHENTICATION_FACTOR,
|
{PROP_ACCESS_EVENT_AUTHENTICATION_FACTOR,
|
||||||
"access-event-authentication-factor"}
|
"access-event-authentication-factor"}
|
||||||
,
|
,
|
||||||
{PROP_ACCESS_EVENT_CREDENTIAL, "access-event-credential"}
|
{PROP_ACCESS_EVENT_CREDENTIAL, "access-event-credential"}
|
||||||
,
|
,
|
||||||
@@ -835,7 +835,7 @@ INDTEXT_DATA bacnet_property_names[] = {
|
|||||||
{PROP_MASTER_POINT, "muster-point"}
|
{PROP_MASTER_POINT, "muster-point"}
|
||||||
,
|
,
|
||||||
{PROP_NUMBER_OF_AUTHENTICATION_POLICIES,
|
{PROP_NUMBER_OF_AUTHENTICATION_POLICIES,
|
||||||
"number-of-authentication-policies"}
|
"number-of-authentication-policies"}
|
||||||
,
|
,
|
||||||
{PROP_OCCUPANCY_COUNT, "occupancy-count"}
|
{PROP_OCCUPANCY_COUNT, "occupancy-count"}
|
||||||
,
|
,
|
||||||
@@ -846,7 +846,7 @@ INDTEXT_DATA bacnet_property_names[] = {
|
|||||||
{PROP_OCCUPANCY_LOWER_THRESHOLD, "occupancy-lower-threshold"}
|
{PROP_OCCUPANCY_LOWER_THRESHOLD, "occupancy-lower-threshold"}
|
||||||
,
|
,
|
||||||
{PROP_OCCUPANCY_LOWER_THRESHOLD_ENFORCED,
|
{PROP_OCCUPANCY_LOWER_THRESHOLD_ENFORCED,
|
||||||
"occupancy-lower-threshold-enforced"}
|
"occupancy-lower-threshold-enforced"}
|
||||||
,
|
,
|
||||||
{PROP_OCCUPANCY_STATE, "occupancy-state"}
|
{PROP_OCCUPANCY_STATE, "occupancy-state"}
|
||||||
,
|
,
|
||||||
@@ -1522,13 +1522,13 @@ INDTEXT_DATA bacnet_error_code_names[] = {
|
|||||||
{ERROR_CODE_ABORT_BUFFER_OVERFLOW, "abort-buffer-overflow"}
|
{ERROR_CODE_ABORT_BUFFER_OVERFLOW, "abort-buffer-overflow"}
|
||||||
,
|
,
|
||||||
{ERROR_CODE_ABORT_INVALID_APDU_IN_THIS_STATE,
|
{ERROR_CODE_ABORT_INVALID_APDU_IN_THIS_STATE,
|
||||||
"abort-invalid-apdu-in-this-state"}
|
"abort-invalid-apdu-in-this-state"}
|
||||||
,
|
,
|
||||||
{ERROR_CODE_ABORT_PREEMPTED_BY_HIGHER_PRIORITY_TASK,
|
{ERROR_CODE_ABORT_PREEMPTED_BY_HIGHER_PRIORITY_TASK,
|
||||||
"abort-preempted-by-higher-priority-task"}
|
"abort-preempted-by-higher-priority-task"}
|
||||||
,
|
,
|
||||||
{ERROR_CODE_ABORT_SEGMENTATION_NOT_SUPPORTED,
|
{ERROR_CODE_ABORT_SEGMENTATION_NOT_SUPPORTED,
|
||||||
"abort-segmentation-not-supported"}
|
"abort-segmentation-not-supported"}
|
||||||
,
|
,
|
||||||
{ERROR_CODE_ABORT_PROPRIETARY, "abort-proprietary"}
|
{ERROR_CODE_ABORT_PROPRIETARY, "abort-proprietary"}
|
||||||
,
|
,
|
||||||
@@ -1541,15 +1541,15 @@ INDTEXT_DATA bacnet_error_code_names[] = {
|
|||||||
{ERROR_CODE_REJECT_BUFFER_OVERFLOW, "reject-buffer-overflow"}
|
{ERROR_CODE_REJECT_BUFFER_OVERFLOW, "reject-buffer-overflow"}
|
||||||
,
|
,
|
||||||
{ERROR_CODE_REJECT_INCONSISTENT_PARAMETERS,
|
{ERROR_CODE_REJECT_INCONSISTENT_PARAMETERS,
|
||||||
"reject-inconsistent-parameters"}
|
"reject-inconsistent-parameters"}
|
||||||
,
|
,
|
||||||
{ERROR_CODE_REJECT_INVALID_PARAMETER_DATA_TYPE,
|
{ERROR_CODE_REJECT_INVALID_PARAMETER_DATA_TYPE,
|
||||||
"reject-invalid-parameter-data-type"}
|
"reject-invalid-parameter-data-type"}
|
||||||
,
|
,
|
||||||
{ERROR_CODE_REJECT_INVALID_TAG, "reject-invalid-tag"}
|
{ERROR_CODE_REJECT_INVALID_TAG, "reject-invalid-tag"}
|
||||||
,
|
,
|
||||||
{ERROR_CODE_REJECT_MISSING_REQUIRED_PARAMETER,
|
{ERROR_CODE_REJECT_MISSING_REQUIRED_PARAMETER,
|
||||||
"reject-missing-required-parameter"}
|
"reject-missing-required-parameter"}
|
||||||
,
|
,
|
||||||
{ERROR_CODE_REJECT_PARAMETER_OUT_OF_RANGE, "reject-parameter-out-of-range"}
|
{ERROR_CODE_REJECT_PARAMETER_OUT_OF_RANGE, "reject-parameter-out-of-range"}
|
||||||
,
|
,
|
||||||
@@ -1580,7 +1580,7 @@ INDTEXT_DATA bacnet_error_code_names[] = {
|
|||||||
{ERROR_CODE_NO_PROPERTY_SPECIFIED, "no-property-specified"}
|
{ERROR_CODE_NO_PROPERTY_SPECIFIED, "no-property-specified"}
|
||||||
,
|
,
|
||||||
{ERROR_CODE_NOT_CONFIGURED_FOR_TRIGGERED_LOGGING,
|
{ERROR_CODE_NOT_CONFIGURED_FOR_TRIGGERED_LOGGING,
|
||||||
"not-configured-for-triggered-logging"}
|
"not-configured-for-triggered-logging"}
|
||||||
,
|
,
|
||||||
{ERROR_CODE_UNKNOWN_SUBSCRIPTION, "unknown-subscription"}
|
{ERROR_CODE_UNKNOWN_SUBSCRIPTION, "unknown-subscription"}
|
||||||
,
|
,
|
||||||
|
|||||||
@@ -135,18 +135,18 @@ uint16_t dlmstp_receive(
|
|||||||
(void) max_pdu;
|
(void) max_pdu;
|
||||||
/* see if there is a packet available, and a place
|
/* see if there is a packet available, and a place
|
||||||
to put the reply (if necessary) and process it */
|
to put the reply (if necessary) and process it */
|
||||||
gettimeofday(&tp,NULL);
|
gettimeofday(&tp, NULL);
|
||||||
if (timeout < 1000) {
|
if (timeout < 1000) {
|
||||||
tp.tv_nsec += timeout * 1000;
|
tp.tv_nsec += timeout * 1000;
|
||||||
while (tp.tv_nsec > (1000*1000)) {
|
while (tp.tv_nsec > (1000 * 1000)) {
|
||||||
tp.tv_sec++;
|
tp.tv_sec++;
|
||||||
tp.tv_nsec-= (1000*1000);
|
tp.tv_nsec -= (1000 * 1000);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
tp.tv_sec += timeout / 1000;
|
tp.tv_sec += timeout / 1000;
|
||||||
tp.tv_nsec += timeout - (tp.tv_sec * 1000);
|
tp.tv_nsec += timeout - (tp.tv_sec * 1000);
|
||||||
}
|
}
|
||||||
if (!sem_timedwait(&Receive_Packet_Flag,tp)) {
|
if (!sem_timedwait(&Receive_Packet_Flag, tp)) {
|
||||||
if (Receive_Packet.ready) {
|
if (Receive_Packet.ready) {
|
||||||
if (Receive_Packet.pdu_len) {
|
if (Receive_Packet.pdu_len) {
|
||||||
MSTP_Packets++;
|
MSTP_Packets++;
|
||||||
@@ -621,8 +621,7 @@ bool dlmstp_init(
|
|||||||
rv = sem_init(&Receive_Packet_Flag, 0, 0);
|
rv = sem_init(&Receive_Packet_Flag, 0, 0);
|
||||||
Receive_Packet_Flag = CreateSemaphore(NULL, 0, 1, "dlmstpReceivePacket");
|
Receive_Packet_Flag = CreateSemaphore(NULL, 0, 1, "dlmstpReceivePacket");
|
||||||
if (rv) {
|
if (rv) {
|
||||||
fprintf(stderr,
|
fprintf(stderr, "MS/TP Interface: %s\n cannot allocate semaphore.",
|
||||||
"MS/TP Interface: %s\n cannot allocate semaphore.",
|
|
||||||
ifname);
|
ifname);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|||||||
+734
-596
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user