Issues/issue 811 mstpcap considers valid frames as invalid if used as a standalone tool (#812)
* Fixed mstpcap utility by setting This-Station to the MS/TP promiscuous mode address 255. * Fixed MS/TP receive filter for valid data frames which was missing. * Fixed MS/TP zero-config duplicate node detection * Reset silence during MS/TP capture after writing packet to prevent endless packets when stream is interrupted.
This commit is contained in:
+5
-2
@@ -972,7 +972,8 @@ static void print_help(const char *filename)
|
|||||||
filename);
|
filename);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* initialize some of the variables in the MS/TP Receive structure */
|
/* initialize some of the variables in the MS/TP Receive structure
|
||||||
|
after each packet write */
|
||||||
static void mstp_structure_init(struct mstp_port_struct_t *mstp_port)
|
static void mstp_structure_init(struct mstp_port_struct_t *mstp_port)
|
||||||
{
|
{
|
||||||
if (mstp_port) {
|
if (mstp_port) {
|
||||||
@@ -983,9 +984,11 @@ static void mstp_structure_init(struct mstp_port_struct_t *mstp_port)
|
|||||||
mstp_port->HeaderCRCActual = 0;
|
mstp_port->HeaderCRCActual = 0;
|
||||||
mstp_port->Index = 0;
|
mstp_port->Index = 0;
|
||||||
mstp_port->EventCount = 0;
|
mstp_port->EventCount = 0;
|
||||||
|
mstp_port->DataRegister = 0xFF;
|
||||||
mstp_port->ReceivedInvalidFrame = false;
|
mstp_port->ReceivedInvalidFrame = false;
|
||||||
mstp_port->ReceivedValidFrame = false;
|
mstp_port->ReceivedValidFrame = false;
|
||||||
mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE;
|
mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE;
|
||||||
|
mstp_port->SilenceTimerReset(NULL);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1003,7 +1006,7 @@ int main(int argc, char *argv[])
|
|||||||
MSTP_Port.InputBufferSize = sizeof(RxBuffer);
|
MSTP_Port.InputBufferSize = sizeof(RxBuffer);
|
||||||
MSTP_Port.OutputBuffer = &TxBuffer[0];
|
MSTP_Port.OutputBuffer = &TxBuffer[0];
|
||||||
MSTP_Port.OutputBufferSize = sizeof(TxBuffer);
|
MSTP_Port.OutputBufferSize = sizeof(TxBuffer);
|
||||||
MSTP_Port.This_Station = 127;
|
MSTP_Port.This_Station = MSTP_BROADCAST_ADDRESS;
|
||||||
MSTP_Port.Nmax_info_frames = 1;
|
MSTP_Port.Nmax_info_frames = 1;
|
||||||
MSTP_Port.Nmax_master = 127;
|
MSTP_Port.Nmax_master = 127;
|
||||||
MSTP_Port.SilenceTimer = Timer_Silence;
|
MSTP_Port.SilenceTimer = Timer_Silence;
|
||||||
|
|||||||
+41
-40
@@ -274,6 +274,17 @@ void MSTP_Create_And_Send_Frame(
|
|||||||
/* FIXME: be sure to reset SilenceTimer() after each octet is sent! */
|
/* FIXME: be sure to reset SilenceTimer() after each octet is sent! */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static bool MSTP_Frame_For_Us(struct mstp_port_struct_t *mstp_port)
|
||||||
|
{
|
||||||
|
if ((mstp_port->DestinationAddress == mstp_port->This_Station) ||
|
||||||
|
(mstp_port->DestinationAddress == MSTP_BROADCAST_ADDRESS) ||
|
||||||
|
(mstp_port->This_Station == MSTP_BROADCAST_ADDRESS)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Finite State Machine for receiving an MSTP frame
|
* @brief Finite State Machine for receiving an MSTP frame
|
||||||
* @param mstp_port MSTP port context data
|
* @param mstp_port MSTP port context data
|
||||||
@@ -429,12 +440,7 @@ void MSTP_Receive_Frame_FSM(struct mstp_port_struct_t *mstp_port)
|
|||||||
} else {
|
} else {
|
||||||
if (mstp_port->DataLength == 0) {
|
if (mstp_port->DataLength == 0) {
|
||||||
/* NoData */
|
/* NoData */
|
||||||
if ((mstp_port->DestinationAddress ==
|
if (MSTP_Frame_For_Us(mstp_port)) {
|
||||||
mstp_port->This_Station) ||
|
|
||||||
(mstp_port->DestinationAddress ==
|
|
||||||
MSTP_BROADCAST_ADDRESS) ||
|
|
||||||
(mstp_port->This_Station ==
|
|
||||||
MSTP_BROADCAST_ADDRESS)) {
|
|
||||||
printf_receive_data(
|
printf_receive_data(
|
||||||
"%s",
|
"%s",
|
||||||
mstptext_frame_type(
|
mstptext_frame_type(
|
||||||
@@ -442,17 +448,14 @@ void MSTP_Receive_Frame_FSM(struct mstp_port_struct_t *mstp_port)
|
|||||||
/* indicate that a frame with no data has been
|
/* indicate that a frame with no data has been
|
||||||
* received */
|
* received */
|
||||||
mstp_port->ReceivedValidFrame = true;
|
mstp_port->ReceivedValidFrame = true;
|
||||||
|
} else {
|
||||||
|
/* NotForUs */
|
||||||
|
mstp_port->ReceivedInvalidFrame = true;
|
||||||
}
|
}
|
||||||
/* wait for the start of the next frame. */
|
/* wait for the start of the next frame. */
|
||||||
mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE;
|
mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE;
|
||||||
} else {
|
} else {
|
||||||
/* receive the data portion of the frame. */
|
if (MSTP_Frame_For_Us(mstp_port)) {
|
||||||
if ((mstp_port->DestinationAddress ==
|
|
||||||
mstp_port->This_Station) ||
|
|
||||||
(mstp_port->DestinationAddress ==
|
|
||||||
MSTP_BROADCAST_ADDRESS) ||
|
|
||||||
(mstp_port->This_Station ==
|
|
||||||
MSTP_BROADCAST_ADDRESS)) {
|
|
||||||
if (mstp_port->DataLength <=
|
if (mstp_port->DataLength <=
|
||||||
mstp_port->InputBufferSize) {
|
mstp_port->InputBufferSize) {
|
||||||
/* Data */
|
/* Data */
|
||||||
@@ -467,7 +470,7 @@ void MSTP_Receive_Frame_FSM(struct mstp_port_struct_t *mstp_port)
|
|||||||
MSTP_RECEIVE_STATE_SKIP_DATA;
|
MSTP_RECEIVE_STATE_SKIP_DATA;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
/* NotForUs */
|
/* DataNotForUs */
|
||||||
mstp_port->receive_state =
|
mstp_port->receive_state =
|
||||||
MSTP_RECEIVE_STATE_SKIP_DATA;
|
MSTP_RECEIVE_STATE_SKIP_DATA;
|
||||||
}
|
}
|
||||||
@@ -560,18 +563,28 @@ void MSTP_Receive_Frame_FSM(struct mstp_port_struct_t *mstp_port)
|
|||||||
&mstp_port->InputBuffer[mstp_port->Index + 1],
|
&mstp_port->InputBuffer[mstp_port->Index + 1],
|
||||||
mstp_port->InputBufferSize, mstp_port->InputBuffer,
|
mstp_port->InputBufferSize, mstp_port->InputBuffer,
|
||||||
mstp_port->Index + 1);
|
mstp_port->Index + 1);
|
||||||
if (mstp_port->DataLength > 0) {
|
if ((mstp_port->DataLength > 0) &&
|
||||||
|
(mstp_port->receive_state ==
|
||||||
|
MSTP_RECEIVE_STATE_DATA)) {
|
||||||
|
/* GoodCRC */
|
||||||
mstp_port->ReceivedValidFrame = true;
|
mstp_port->ReceivedValidFrame = true;
|
||||||
} else {
|
} else {
|
||||||
|
/* Done */
|
||||||
mstp_port->ReceivedInvalidFrame = true;
|
mstp_port->ReceivedInvalidFrame = true;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
/* STATE DATA CRC - no need for new state */
|
/* STATE DATA CRC - no need for new state */
|
||||||
if (mstp_port->DataCRC == 0xF0B8) {
|
if (mstp_port->DataCRC == 0xF0B8) {
|
||||||
/* indicate the complete reception of a
|
if (mstp_port->receive_state ==
|
||||||
valid frame */
|
MSTP_RECEIVE_STATE_DATA) {
|
||||||
mstp_port->ReceivedValidFrame = true;
|
/* GoodCRC */
|
||||||
|
mstp_port->ReceivedValidFrame = true;
|
||||||
|
} else {
|
||||||
|
/* Done */
|
||||||
|
mstp_port->ReceivedInvalidFrame = true;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
|
/* BadCRC */
|
||||||
mstp_port->ReceivedInvalidFrame = true;
|
mstp_port->ReceivedInvalidFrame = true;
|
||||||
printf_receive_error(
|
printf_receive_error(
|
||||||
"MSTP: Rx Data: BadCRC [%02X]\n",
|
"MSTP: Rx Data: BadCRC [%02X]\n",
|
||||||
@@ -623,28 +636,16 @@ bool MSTP_Master_Node_FSM(struct mstp_port_struct_t *mstp_port)
|
|||||||
(mstp_port->This_Station + 1) % (mstp_port->Nmax_master + 1);
|
(mstp_port->This_Station + 1) % (mstp_port->Nmax_master + 1);
|
||||||
next_next_station =
|
next_next_station =
|
||||||
(mstp_port->Next_Station + 1) % (mstp_port->Nmax_master + 1);
|
(mstp_port->Next_Station + 1) % (mstp_port->Nmax_master + 1);
|
||||||
/* The zero config receive state machine does not
|
/* The zero config checks before running FSM */
|
||||||
filter DestinationAddress for This_Station or Broadcast.
|
if ((mstp_port->ZeroConfigEnabled) &&
|
||||||
Check before running FSM: */
|
(mstp_port->master_state != MSTP_MASTER_STATE_INITIALIZE) &&
|
||||||
if (mstp_port->ReceivedValidFrame == true) {
|
(mstp_port->ReceivedValidFrame == true) &&
|
||||||
if ((mstp_port->master_state == MSTP_MASTER_STATE_INITIALIZE) &&
|
(mstp_port->SourceAddress == mstp_port->This_Station)) {
|
||||||
(mstp_port->ZeroConfigEnabled)) {
|
/* DuplicateNode */
|
||||||
/* zero config initialization processes all frames */
|
mstp_port->Zero_Config_State = MSTP_ZERO_CONFIG_STATE_INIT;
|
||||||
} else {
|
mstp_port->master_state = MSTP_MASTER_STATE_INITIALIZE;
|
||||||
if ((mstp_port->DestinationAddress == mstp_port->This_Station) ||
|
/* ignore the frame */
|
||||||
(mstp_port->DestinationAddress == MSTP_BROADCAST_ADDRESS)) {
|
mstp_port->ReceivedValidFrame = false;
|
||||||
/* process as-is */
|
|
||||||
} else {
|
|
||||||
if ((mstp_port->ZeroConfigEnabled) &&
|
|
||||||
(mstp_port->SourceAddress == mstp_port->This_Station)) {
|
|
||||||
/* DuplicateNode */
|
|
||||||
mstp_port->Zero_Config_State = MSTP_ZERO_CONFIG_STATE_INIT;
|
|
||||||
mstp_port->master_state = MSTP_MASTER_STATE_INITIALIZE;
|
|
||||||
}
|
|
||||||
/* ignore the frame */
|
|
||||||
mstp_port->ReceivedValidFrame = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
switch (mstp_port->master_state) {
|
switch (mstp_port->master_state) {
|
||||||
case MSTP_MASTER_STATE_INITIALIZE:
|
case MSTP_MASTER_STATE_INITIALIZE:
|
||||||
|
|||||||
Reference in New Issue
Block a user