Added new MS/TP skip data state.

Added ring buffer for incoming PDU to fix WhoIs followed by ReadProperty situation.
This commit is contained in:
skarg
2010-09-16 16:08:10 +00:00
parent c8f4aac1b7
commit 6e739e06d5
4 changed files with 110 additions and 119 deletions
@@ -88,6 +88,7 @@ DEMOSRC = $(BACNET_DEMO)/handler/txbuf.c \
# core BACnet stack files # core BACnet stack files
CORESRC = \ CORESRC = \
$(BACNET_CORE)/fifo.c \ $(BACNET_CORE)/fifo.c \
$(BACNET_CORE)/ringbuf.c \
$(BACNET_CORE)/memcopy.c \ $(BACNET_CORE)/memcopy.c \
$(BACNET_CORE)/crc.c \ $(BACNET_CORE)/crc.c \
$(BACNET_CORE)/apdu.c \ $(BACNET_CORE)/apdu.c \
+1 -1
View File
@@ -1 +1 @@
<AVRStudio><MANAGEMENT><ProjectName>bacnet</ProjectName><Created>29-Apr-2009 08:16:53</Created><LastEdit>11-Feb-2010 10:52:30</LastEdit><ICON>241</ICON><ProjectType>0</ProjectType><Created>29-Apr-2009 08:16:53</Created><Version>4</Version><Build>4, 15, 0, 623</Build><ProjectTypeName>AVR GCC</ProjectTypeName></MANAGEMENT><CODE_CREATION><ObjectFile>bacnet.elf</ObjectFile><EntryFile></EntryFile><SaveFolder>D:\code\bacnet-stack\ports\bdk-atxx4-mstp\</SaveFolder></CODE_CREATION><DEBUG_TARGET><CURRENT_TARGET>JTAGICE mkII</CURRENT_TARGET><CURRENT_PART>ATmega644P.xml</CURRENT_PART><BREAKPOINTS></BREAKPOINTS><IO_EXPAND><HIDE>false</HIDE></IO_EXPAND><REGISTERNAMES><Register>R00</Register><Register>R01</Register><Register>R02</Register><Register>R03</Register><Register>R04</Register><Register>R05</Register><Register>R06</Register><Register>R07</Register><Register>R08</Register><Register>R09</Register><Register>R10</Register><Register>R11</Register><Register>R12</Register><Register>R13</Register><Register>R14</Register><Register>R15</Register><Register>R16</Register><Register>R17</Register><Register>R18</Register><Register>R19</Register><Register>R20</Register><Register>R21</Register><Register>R22</Register><Register>R23</Register><Register>R24</Register><Register>R25</Register><Register>R26</Register><Register>R27</Register><Register>R28</Register><Register>R29</Register><Register>R30</Register><Register>R31</Register></REGISTERNAMES><COM>Auto</COM><COMType>0</COMType><WATCHNUM>0</WATCHNUM><WATCHNAMES><Pane0></Pane0><Pane1></Pane1><Pane2></Pane2><Pane3></Pane3></WATCHNAMES><BreakOnTrcaeFull>0</BreakOnTrcaeFull></DEBUG_TARGET><Debugger><modules><module></module></modules><Triggers></Triggers></Debugger><AVRGCCPLUGIN><FILES><SOURCEFILE>main.c</SOURCEFILE><SOURCEFILE>timer2.c</SOURCEFILE><SOURCEFILE>eeprom.c</SOURCEFILE><SOURCEFILE>init.c</SOURCEFILE><SOURCEFILE>input.c</SOURCEFILE><SOURCEFILE>led.c</SOURCEFILE><SOURCEFILE>rs485.c</SOURCEFILE><SOURCEFILE>seeprom.c</SOURCEFILE><SOURCEFILE>serial.c</SOURCEFILE><SOURCEFILE>stack.c</SOURCEFILE><SOURCEFILE>dlmstp.c</SOURCEFILE><SOURCEFILE>bo.c</SOURCEFILE><SOURCEFILE>bi.c</SOURCEFILE><SOURCEFILE>ai.c</SOURCEFILE><SOURCEFILE>device.c</SOURCEFILE><HEADERFILE>timer.h</HEADERFILE><HEADERFILE>eeprom.h</HEADERFILE><HEADERFILE>hardware.h</HEADERFILE><HEADERFILE>iar2gcc.h</HEADERFILE><HEADERFILE>init.h</HEADERFILE><HEADERFILE>input.h</HEADERFILE><HEADERFILE>led.h</HEADERFILE><HEADERFILE>nvdata.h</HEADERFILE><HEADERFILE>rs485.h</HEADERFILE><HEADERFILE>seeprom.h</HEADERFILE><HEADERFILE>serial.h</HEADERFILE><OTHERFILE>Makefile</OTHERFILE></FILES><CONFIGS><CONFIG><NAME>default</NAME><USESEXTERNALMAKEFILE>YES</USESEXTERNALMAKEFILE><EXTERNALMAKEFILE>Makefile</EXTERNALMAKEFILE><PART>atmega644p</PART><HEX>1</HEX><LIST>1</LIST><MAP>1</MAP><OUTPUTFILENAME>bacnet.elf</OUTPUTFILENAME><OUTPUTDIR>default\</OUTPUTDIR><ISDIRTY>1</ISDIRTY><OPTIONS/><INCDIRS/><LIBDIRS/><LIBS/><LINKOBJECTS/><OPTIONSFORALL>-Wall -gdwarf-2 -std=gnu99 -Os -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums</OPTIONSFORALL><LINKEROPTIONS></LINKEROPTIONS><SEGMENTS/></CONFIG></CONFIGS><LASTCONFIG>default</LASTCONFIG><USES_WINAVR>1</USES_WINAVR><GCC_LOC>C:\WinAVR-20090313\bin\avr-gcc.exe</GCC_LOC><MAKE_LOC>C:\WinAVR-20090313\utils\bin\make.exe</MAKE_LOC></AVRGCCPLUGIN><IOView><usergroups/><sort sorted="0" column="0" ordername="1" orderaddress="1" ordergroup="1"/></IOView><Files><File00000><FileId>00000</FileId><FileName>main.c</FileName><Status>259</Status></File00000><File00001><FileId>00001</FileId><FileName>Makefile</FileName><Status>1</Status></File00001></Files><Events><Bookmarks></Bookmarks></Events><Trace><Filters></Filters></Trace></AVRStudio> <AVRStudio><MANAGEMENT><ProjectName>bacnet</ProjectName><Created>29-Apr-2009 08:16:53</Created><LastEdit>12-Sep-2010 23:23:02</LastEdit><ICON>241</ICON><ProjectType>0</ProjectType><Created>29-Apr-2009 08:16:53</Created><Version>4</Version><Build>4, 15, 0, 623</Build><ProjectTypeName>AVR GCC</ProjectTypeName></MANAGEMENT><CODE_CREATION><ObjectFile>bacnet.elf</ObjectFile><EntryFile></EntryFile><SaveFolder>D:\code\bacnet-stack\ports\bdk-atxx4-mstp\</SaveFolder></CODE_CREATION><DEBUG_TARGET><CURRENT_TARGET>JTAGICE mkII</CURRENT_TARGET><CURRENT_PART>ATmega644P.xml</CURRENT_PART><BREAKPOINTS></BREAKPOINTS><IO_EXPAND><HIDE>false</HIDE></IO_EXPAND><REGISTERNAMES><Register>R00</Register><Register>R01</Register><Register>R02</Register><Register>R03</Register><Register>R04</Register><Register>R05</Register><Register>R06</Register><Register>R07</Register><Register>R08</Register><Register>R09</Register><Register>R10</Register><Register>R11</Register><Register>R12</Register><Register>R13</Register><Register>R14</Register><Register>R15</Register><Register>R16</Register><Register>R17</Register><Register>R18</Register><Register>R19</Register><Register>R20</Register><Register>R21</Register><Register>R22</Register><Register>R23</Register><Register>R24</Register><Register>R25</Register><Register>R26</Register><Register>R27</Register><Register>R28</Register><Register>R29</Register><Register>R30</Register><Register>R31</Register></REGISTERNAMES><COM>Auto</COM><COMType>0</COMType><WATCHNUM>0</WATCHNUM><WATCHNAMES><Pane0></Pane0><Pane1></Pane1><Pane2></Pane2><Pane3></Pane3></WATCHNAMES><BreakOnTrcaeFull>0</BreakOnTrcaeFull></DEBUG_TARGET><Debugger><modules><module></module></modules><Triggers></Triggers></Debugger><AVRGCCPLUGIN><FILES><SOURCEFILE>main.c</SOURCEFILE><SOURCEFILE>timer2.c</SOURCEFILE><SOURCEFILE>eeprom.c</SOURCEFILE><SOURCEFILE>init.c</SOURCEFILE><SOURCEFILE>input.c</SOURCEFILE><SOURCEFILE>led.c</SOURCEFILE><SOURCEFILE>rs485.c</SOURCEFILE><SOURCEFILE>seeprom.c</SOURCEFILE><SOURCEFILE>serial.c</SOURCEFILE><SOURCEFILE>stack.c</SOURCEFILE><SOURCEFILE>dlmstp.c</SOURCEFILE><SOURCEFILE>bo.c</SOURCEFILE><SOURCEFILE>bi.c</SOURCEFILE><SOURCEFILE>ai.c</SOURCEFILE><SOURCEFILE>device.c</SOURCEFILE><HEADERFILE>timer.h</HEADERFILE><HEADERFILE>eeprom.h</HEADERFILE><HEADERFILE>hardware.h</HEADERFILE><HEADERFILE>iar2gcc.h</HEADERFILE><HEADERFILE>init.h</HEADERFILE><HEADERFILE>input.h</HEADERFILE><HEADERFILE>led.h</HEADERFILE><HEADERFILE>nvdata.h</HEADERFILE><HEADERFILE>rs485.h</HEADERFILE><HEADERFILE>seeprom.h</HEADERFILE><HEADERFILE>serial.h</HEADERFILE><OTHERFILE>Makefile</OTHERFILE></FILES><CONFIGS><CONFIG><NAME>default</NAME><USESEXTERNALMAKEFILE>YES</USESEXTERNALMAKEFILE><EXTERNALMAKEFILE>Makefile</EXTERNALMAKEFILE><PART>atmega644p</PART><HEX>1</HEX><LIST>1</LIST><MAP>1</MAP><OUTPUTFILENAME>bacnet.elf</OUTPUTFILENAME><OUTPUTDIR>default\</OUTPUTDIR><ISDIRTY>1</ISDIRTY><OPTIONS/><INCDIRS/><LIBDIRS/><LIBS/><LINKOBJECTS/><OPTIONSFORALL>-Wall -gdwarf-2 -std=gnu99 -Os -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums</OPTIONSFORALL><LINKEROPTIONS></LINKEROPTIONS><SEGMENTS/></CONFIG></CONFIGS><LASTCONFIG>default</LASTCONFIG><USES_WINAVR>1</USES_WINAVR><GCC_LOC>C:\WinAVR-20100110\bin\avr-gcc.exe</GCC_LOC><MAKE_LOC>C:\WinAVR-20100110\utils\bin\make.exe</MAKE_LOC></AVRGCCPLUGIN><IOView><usergroups/><sort sorted="0" column="0" ordername="0" orderaddress="0" ordergroup="0"/></IOView><Files><File00000><FileId>00000</FileId><FileName>main.c</FileName><Status>259</Status></File00000><File00001><FileId>00001</FileId><FileName>Makefile</FileName><Status>1</Status></File00001></Files><Events><Bookmarks></Bookmarks></Events><Trace><Filters></Filters></Trace></AVRStudio>
@@ -2041,6 +2041,9 @@
<file> <file>
<name>$PROJ_DIR$\..\..\src\reject.c</name> <name>$PROJ_DIR$\..\..\src\reject.c</name>
</file> </file>
<file>
<name>$PROJ_DIR$\..\..\src\ringbuf.c</name>
</file>
<file> <file>
<name>$PROJ_DIR$\..\..\src\rp.c</name> <name>$PROJ_DIR$\..\..\src\rp.c</name>
</file> </file>
+105 -118
View File
@@ -38,12 +38,14 @@
#include <string.h> #include <string.h>
#include "bacdef.h" #include "bacdef.h"
#include "dlmstp.h" #include "dlmstp.h"
#include "mstpdef.h"
#include "rs485.h" #include "rs485.h"
#include "crc.h" #include "crc.h"
#include "npdu.h" #include "npdu.h"
#include "bits.h" #include "bits.h"
#include "bytes.h" #include "bytes.h"
#include "bacaddr.h" #include "bacaddr.h"
#include "ringbuf.h"
#include "timer.h" #include "timer.h"
#include "eeprom.h" #include "eeprom.h"
#include "nvdata.h" #include "nvdata.h"
@@ -53,52 +55,6 @@
Only one MS/TP datalink layer Only one MS/TP datalink layer
*/ */
/* The value 255 is used to denote broadcast when used as a */
/* destination address but is not allowed as a value for a station. */
/* Station addresses for master nodes can be 0-127. */
/* Station addresses for slave nodes can be 127-254. */
#define MSTP_BROADCAST_ADDRESS 255
/* MS/TP Frame Type */
/* Frame Types 8 through 127 are reserved by ASHRAE. */
#define FRAME_TYPE_TOKEN 0
#define FRAME_TYPE_POLL_FOR_MASTER 1
#define FRAME_TYPE_REPLY_TO_POLL_FOR_MASTER 2
#define FRAME_TYPE_TEST_REQUEST 3
#define FRAME_TYPE_TEST_RESPONSE 4
#define FRAME_TYPE_BACNET_DATA_EXPECTING_REPLY 5
#define FRAME_TYPE_BACNET_DATA_NOT_EXPECTING_REPLY 6
#define FRAME_TYPE_REPLY_POSTPONED 7
/* Frame Types 128 through 255: Proprietary Frames */
/* These frames are available to vendors as proprietary (non-BACnet) frames. */
/* The first two octets of the Data field shall specify the unique vendor */
/* identification code, most significant octet first, for the type of */
/* vendor-proprietary frame to be conveyed. The length of the data portion */
/* of a Proprietary frame shall be in the range of 2 to 501 octets. */
#define FRAME_TYPE_PROPRIETARY_MIN 128
#define FRAME_TYPE_PROPRIETARY_MAX 255
/* receive FSM states */
typedef enum {
MSTP_RECEIVE_STATE_IDLE = 0,
MSTP_RECEIVE_STATE_PREAMBLE = 1,
MSTP_RECEIVE_STATE_HEADER = 2,
MSTP_RECEIVE_STATE_DATA = 3
} MSTP_RECEIVE_STATE;
/* master node FSM states */
typedef enum {
MSTP_MASTER_STATE_INITIALIZE = 0,
MSTP_MASTER_STATE_IDLE = 1,
MSTP_MASTER_STATE_USE_TOKEN = 2,
MSTP_MASTER_STATE_WAIT_FOR_REPLY = 3,
MSTP_MASTER_STATE_DONE_WITH_TOKEN = 4,
MSTP_MASTER_STATE_PASS_TOKEN = 5,
MSTP_MASTER_STATE_NO_TOKEN = 6,
MSTP_MASTER_STATE_POLL_FOR_MASTER = 7,
MSTP_MASTER_STATE_ANSWER_DATA_REQUEST = 8
} MSTP_MASTER_STATE;
/* The state of the Receive State Machine */ /* The state of the Receive State Machine */
static MSTP_RECEIVE_STATE Receive_State; static MSTP_RECEIVE_STATE Receive_State;
/* When a master node is powered up or reset, */ /* When a master node is powered up or reset, */
@@ -114,22 +70,18 @@ static struct mstp_flag_t {
/* if a valid frame is received. */ /* if a valid frame is received. */
/* Set to FALSE by the main state machine. */ /* Set to FALSE by the main state machine. */
unsigned ReceivedValidFrame:1; unsigned ReceivedValidFrame:1;
/* set to TRUE when we get a frame not for us */
unsigned ReceivedValidFrameNotForUs:1;
/* A Boolean flag set to TRUE by the master machine if this node is the */ /* A Boolean flag set to TRUE by the master machine if this node is the */
/* only known master node. */ /* only known master node. */
unsigned SoleMaster:1; unsigned SoleMaster:1;
/* A Boolean flag set TRUE by the datalink transmit if a
frame is pending */
unsigned TransmitPacketPending:1;
/* A Boolean flag set TRUE by the datalink transmit if a
pending packet is DataExpectingReply */
unsigned TransmitPacketDER:1;
/* A Boolean flag set TRUE by the datalink if a /* A Boolean flag set TRUE by the datalink if a
packet has been received, but not processed. */ packet has been received, but not processed. */
unsigned ReceivePacketPending:1; unsigned ReceivePacketPending:1;
} MSTP_Flag; } MSTP_Flag;
/* Used to store the data length of a received frame. */ /* Used to store the data length of a received frame. */
static uint16_t DataLength; static uint32_t DataLength;
/* Used to store the destination address of a received frame. */ /* Used to store the destination address of a received frame. */
static uint8_t DestinationAddress; static uint8_t DestinationAddress;
/* Used to count the number of received octets or errors. */ /* Used to count the number of received octets or errors. */
@@ -160,23 +112,13 @@ static uint8_t This_Station;
/* nodes. This may be used to allocate more or less of the available link */ /* nodes. This may be used to allocate more or less of the available link */
/* bandwidth to particular nodes. If Max_Info_Frames is not writable in a */ /* bandwidth to particular nodes. If Max_Info_Frames is not writable in a */
/* node, its value shall be 1. */ /* node, its value shall be 1. */
#define Nmax_info_frames 1 static uint8_t Nmax_info_frames = 1;
/* This parameter represents the value of the Max_Master property of the */ /* This parameter represents the value of the Max_Master property of the */
/* node's Device object. The value of Max_Master specifies the highest */ /* node's Device object. The value of Max_Master specifies the highest */
/* allowable address for master nodes. The value of Max_Master shall be */ /* allowable address for master nodes. The value of Max_Master shall be */
/* less than or equal to 127. If Max_Master is not writable in a node, */ /* less than or equal to 127. If Max_Master is not writable in a node, */
/* its value shall be 127. */ /* its value shall be 127. */
static uint8_t Nmax_master = 127; static uint8_t Nmax_master = 127;
/* An array of octets, used to store octets for transmitting */
/* OutputBuffer is indexed from 0 to OutputBufferSize-1. */
/* The MAX_PDU size of a frame is MAX_APDU + MAX_NPDU octets. */
/* FIXME: assign this to an actual array of bytes! */
/* Note: the buffer is designed as a pointer since some compilers
and microcontroller architectures have limits as to places to
hold contiguous memory. */
static uint8_t *TransmitPacket;
static uint16_t TransmitPacketLen;
static uint8_t TransmitPacketDest;
/* The time without a DataAvailable or ReceiveError event before declaration */ /* The time without a DataAvailable or ReceiveError event before declaration */
/* of loss of token: 500 milliseconds. */ /* of loss of token: 500 milliseconds. */
@@ -239,6 +181,20 @@ static uint8_t TransmitPacketDest;
/* we need to be able to increment without rolling over */ /* we need to be able to increment without rolling over */
#define INCREMENT_AND_LIMIT_UINT8(x) {if (x < 0xFF) x++;} #define INCREMENT_AND_LIMIT_UINT8(x) {if (x < 0xFF) x++;}
/* data structure for MS/TP PDU Queue */
struct mstp_pdu_packet {
bool data_expecting_reply;
uint8_t destination_mac;
uint16_t length;
uint8_t buffer[MAX_MPDU];
};
/* count must be a power of 2 for ringbuf library */
#ifndef MSTP_PDU_PACKET_COUNT
#define MSTP_PDU_PACKET_COUNT 2
#endif
static struct mstp_pdu_packet PDU_Buffer[MSTP_PDU_PACKET_COUNT];
static RING_BUFFER PDU_Queue;
bool dlmstp_init( bool dlmstp_init(
char *ifname) char *ifname)
{ {
@@ -248,6 +204,11 @@ bool dlmstp_init(
if (Nmax_master > 127) { if (Nmax_master > 127) {
Nmax_master = 127; Nmax_master = 127;
} }
Ringbuf_Init(
&PDU_Queue,
(uint8_t *)&PDU_Buffer,
sizeof(struct mstp_pdu_packet),
MSTP_PDU_PACKET_COUNT);
return true; return true;
} }
@@ -572,37 +533,38 @@ static void MSTP_Receive_Frame_FSM(
/* wait for the start of the next frame. */ /* wait for the start of the next frame. */
Receive_State = MSTP_RECEIVE_STATE_IDLE; Receive_State = MSTP_RECEIVE_STATE_IDLE;
} else { } else {
/* Note: proposed change to BACnet MSTP state machine! if (DataLength == 0) {
If we don't decode data that is not for us, we could /* NoData */
get confused about the start if the Preamble 55 FF if ((DestinationAddress == This_Station) ||
is part of the data. */ (DestinationAddress ==
if ((DataLength) && (DataLength <= InputBufferSize)) { MSTP_BROADCAST_ADDRESS)) {
/* Data */ /* ForUs */
Index = 0; /* indicate that a frame with
DataCRC = 0xFFFF; no data has been received */
/* receive the data portion of the frame. */ MSTP_Flag.ReceivedValidFrame = true;
Receive_State = MSTP_RECEIVE_STATE_DATA;
} else {
if (DataLength == 0) {
/* NoData */
if ((DestinationAddress == This_Station) ||
(DestinationAddress ==
MSTP_BROADCAST_ADDRESS)) {
/* ForUs */
/* indicate that a frame with
no data has been received */
MSTP_Flag.ReceivedValidFrame = true;
} else {
/* NotForUs - drop */
}
} else { } else {
/* FrameTooLong */ /* NotForUs */
/* indicate that a frame with an illegal or */ MSTP_Flag.ReceivedValidFrameNotForUs = true;
/* unacceptable data length has been received */
MSTP_Flag.ReceivedInvalidFrame = true;
} }
/* wait for the start of the next frame. */ /* wait for the start of the next frame. */
Receive_State = MSTP_RECEIVE_STATE_IDLE; Receive_State = MSTP_RECEIVE_STATE_IDLE;
} else {
/* receive the data portion of the frame. */
if ((DestinationAddress == This_Station) ||
(DestinationAddress == MSTP_BROADCAST_ADDRESS)) {
if (DataLength <= InputBufferSize) {
/* Data */
Receive_State = MSTP_RECEIVE_STATE_DATA;
} else {
/* FrameTooLong */
Receive_State = MSTP_RECEIVE_STATE_SKIP_DATA;
}
} else {
/* NotForUs */
Receive_State = MSTP_RECEIVE_STATE_SKIP_DATA;
}
Index = 0;
DataCRC = 0xFFFF;
} }
} }
} else { } else {
@@ -615,6 +577,7 @@ static void MSTP_Receive_Frame_FSM(
} }
break; break;
case MSTP_RECEIVE_STATE_DATA: case MSTP_RECEIVE_STATE_DATA:
case MSTP_RECEIVE_STATE_SKIP_DATA:
/* In the DATA state, the node waits /* In the DATA state, the node waits
for the data portion of a frame. */ for the data portion of a frame. */
if (rs485_silence_time_elapsed(Tframe_abort)) { if (rs485_silence_time_elapsed(Tframe_abort)) {
@@ -639,7 +602,9 @@ static void MSTP_Receive_Frame_FSM(
if (Index < DataLength) { if (Index < DataLength) {
/* DataOctet */ /* DataOctet */
DataCRC = CRC_Calc_Data(DataRegister, DataCRC); DataCRC = CRC_Calc_Data(DataRegister, DataCRC);
InputBuffer[Index] = DataRegister; if (Index < InputBufferSize) {
InputBuffer[Index] = DataRegister;
}
Index++; Index++;
} else if (Index == DataLength) { } else if (Index == DataLength) {
/* CRC1 */ /* CRC1 */
@@ -651,17 +616,21 @@ static void MSTP_Receive_Frame_FSM(
/* STATE DATA CRC - no need for new state */ /* STATE DATA CRC - no need for new state */
/* indicate the complete reception of a valid frame */ /* indicate the complete reception of a valid frame */
if (DataCRC == 0xF0B8) { if (DataCRC == 0xF0B8) {
if ((DestinationAddress == This_Station) || if (Receive_State == MSTP_RECEIVE_STATE_DATA) {
(DestinationAddress == MSTP_BROADCAST_ADDRESS)) {
/* ForUs */ /* ForUs */
/* indicate that a frame with no data
has been received */
MSTP_Flag.ReceivedValidFrame = true; MSTP_Flag.ReceivedValidFrame = true;
} else {
/* NotForUs */
MSTP_Flag.ReceivedValidFrameNotForUs = true;
} }
} else { } else {
MSTP_Flag.ReceivedInvalidFrame = true; MSTP_Flag.ReceivedInvalidFrame = true;
} }
Receive_State = MSTP_RECEIVE_STATE_IDLE; Receive_State = MSTP_RECEIVE_STATE_IDLE;
} else {
MSTP_Flag.ReceivedInvalidFrame = true;
Receive_State = MSTP_RECEIVE_STATE_IDLE;
} }
} }
break; break;
@@ -722,6 +691,8 @@ static bool MSTP_Master_Node_FSM(
bool matched = false; bool matched = false;
/* transition immediately to the next state */ /* transition immediately to the next state */
bool transition_now = false; bool transition_now = false;
/* packet from the PDU Queue */
struct mstp_pdu_packet *pkt;
/* some calculations that several states need */ /* some calculations that several states need */
next_poll_station = (Poll_Station + 1) % (Nmax_master + 1); next_poll_station = (Poll_Station + 1) % (Nmax_master + 1);
@@ -752,6 +723,8 @@ static bool MSTP_Master_Node_FSM(
some bytes and had a timeout for some reason */ some bytes and had a timeout for some reason */
MSTP_Flag.ReceivedValidFrame = false; MSTP_Flag.ReceivedValidFrame = false;
MSTP_Flag.ReceivedInvalidFrame = false; MSTP_Flag.ReceivedInvalidFrame = false;
MSTP_Flag.ReceivedValidFrameNotForUs = false;
Master_State = MSTP_MASTER_STATE_NO_TOKEN;
transition_now = true; transition_now = true;
} else if (MSTP_Flag.ReceivedInvalidFrame == true) { } else if (MSTP_Flag.ReceivedInvalidFrame == true) {
/* ReceivedInvalidFrame */ /* ReceivedInvalidFrame */
@@ -810,26 +783,27 @@ static bool MSTP_Master_Node_FSM(
/* proprietary frames. */ /* proprietary frames. */
case MSTP_MASTER_STATE_USE_TOKEN: case MSTP_MASTER_STATE_USE_TOKEN:
/* Note: We could wait for up to Tusage_delay */ /* Note: We could wait for up to Tusage_delay */
if (!MSTP_Flag.TransmitPacketPending) { if (Ringbuf_Empty(&PDU_Queue)) {
/* NothingToSend */ /* NothingToSend */
FrameCount = Nmax_info_frames; FrameCount = Nmax_info_frames;
Master_State = MSTP_MASTER_STATE_DONE_WITH_TOKEN; Master_State = MSTP_MASTER_STATE_DONE_WITH_TOKEN;
transition_now = true; transition_now = true;
} else { } else {
uint8_t frame_type; uint8_t frame_type;
if (MSTP_Flag.TransmitPacketDER) { pkt = (struct mstp_pdu_packet *)Ringbuf_Pop_Front(
&PDU_Queue);
if (pkt->data_expecting_reply) {
frame_type = FRAME_TYPE_BACNET_DATA_EXPECTING_REPLY; frame_type = FRAME_TYPE_BACNET_DATA_EXPECTING_REPLY;
} else { } else {
frame_type = FRAME_TYPE_BACNET_DATA_NOT_EXPECTING_REPLY; frame_type = FRAME_TYPE_BACNET_DATA_NOT_EXPECTING_REPLY;
} }
MSTP_Send_Frame(frame_type, TransmitPacketDest, This_Station, MSTP_Send_Frame(frame_type, pkt->destination_mac, This_Station,
(uint8_t *) & TransmitPacket[0], TransmitPacketLen); (uint8_t *) & pkt->buffer[0], pkt->length);
MSTP_Flag.TransmitPacketPending = false;
FrameCount++; FrameCount++;
switch (frame_type) { switch (frame_type) {
case FRAME_TYPE_BACNET_DATA_EXPECTING_REPLY: case FRAME_TYPE_BACNET_DATA_EXPECTING_REPLY:
/* SendAndWait */ /* SendAndWait */
if (TransmitPacketDest == MSTP_BROADCAST_ADDRESS) if (pkt->destination_mac == MSTP_BROADCAST_ADDRESS)
Master_State = MSTP_MASTER_STATE_DONE_WITH_TOKEN; Master_State = MSTP_MASTER_STATE_DONE_WITH_TOKEN;
else else
Master_State = MSTP_MASTER_STATE_WAIT_FOR_REPLY; Master_State = MSTP_MASTER_STATE_WAIT_FOR_REPLY;
@@ -1140,13 +1114,16 @@ static bool MSTP_Master_Node_FSM(
/* a proprietary frame that expects a reply is received. */ /* a proprietary frame that expects a reply is received. */
case MSTP_MASTER_STATE_ANSWER_DATA_REQUEST: case MSTP_MASTER_STATE_ANSWER_DATA_REQUEST:
/* Note: we could wait for up to Treply_delay */ /* Note: we could wait for up to Treply_delay */
if (MSTP_Flag.TransmitPacketPending) { matched = false;
if (!Ringbuf_Empty(&PDU_Queue)) {
pkt = (struct mstp_pdu_packet *)Ringbuf_Get_Front(
&PDU_Queue);
matched = matched =
dlmstp_compare_data_expecting_reply(&InputBuffer[0], dlmstp_compare_data_expecting_reply(&InputBuffer[0],
DataLength, SourceAddress, &TransmitPacket[0], DataLength, SourceAddress, &pkt->buffer[0],
TransmitPacketLen, TransmitPacketDest); pkt->length, pkt->destination_mac);
} }
if (MSTP_Flag.TransmitPacketPending && matched) { if (matched) {
/* Reply */ /* Reply */
/* If a reply is available from the higher layers */ /* If a reply is available from the higher layers */
/* within Treply_delay after the reception of the */ /* within Treply_delay after the reception of the */
@@ -1155,14 +1132,15 @@ static bool MSTP_Master_Node_FSM(
/* then call MSTP_Send_Frame to transmit the reply frame */ /* then call MSTP_Send_Frame to transmit the reply frame */
/* and enter the IDLE state to wait for the next frame. */ /* and enter the IDLE state to wait for the next frame. */
uint8_t frame_type; uint8_t frame_type;
if (MSTP_Flag.TransmitPacketDER) { pkt = (struct mstp_pdu_packet *)Ringbuf_Pop_Front(
&PDU_Queue);
if (pkt->data_expecting_reply) {
frame_type = FRAME_TYPE_BACNET_DATA_EXPECTING_REPLY; frame_type = FRAME_TYPE_BACNET_DATA_EXPECTING_REPLY;
} else { } else {
frame_type = FRAME_TYPE_BACNET_DATA_NOT_EXPECTING_REPLY; frame_type = FRAME_TYPE_BACNET_DATA_NOT_EXPECTING_REPLY;
} }
MSTP_Send_Frame(frame_type, TransmitPacketDest, This_Station, MSTP_Send_Frame(frame_type, pkt->destination_mac, This_Station,
(uint8_t *) & TransmitPacket[0], TransmitPacketLen); (uint8_t *) & pkt->buffer[0], pkt->length);
MSTP_Flag.TransmitPacketPending = false;
Master_State = MSTP_MASTER_STATE_IDLE; Master_State = MSTP_MASTER_STATE_IDLE;
} else { } else {
/* DeferredReply */ /* DeferredReply */
@@ -1197,14 +1175,18 @@ int dlmstp_send_pdu(
unsigned pdu_len) unsigned pdu_len)
{ /* number of bytes of data */ { /* number of bytes of data */
int bytes_sent = 0; int bytes_sent = 0;
struct mstp_pdu_packet *pkt;
uint16_t i = 0;
if (MSTP_Flag.TransmitPacketPending == false) { pkt = (struct mstp_pdu_packet *)Ringbuf_Alloc(&PDU_Queue);
MSTP_Flag.TransmitPacketDER = npdu_data->data_expecting_reply; if (pkt) {
TransmitPacket = pdu; pkt->data_expecting_reply = npdu_data->data_expecting_reply;
TransmitPacketLen = pdu_len; for (i = 0; i < pdu_len; i++) {
pkt->buffer[i] = pdu[i];
}
pkt->length = pdu_len;
pkt->destination_mac = dest->mac[0];
bytes_sent = pdu_len; bytes_sent = pdu_len;
TransmitPacketDest = dest->mac[0];
MSTP_Flag.TransmitPacketPending = true;
} }
return bytes_sent; return bytes_sent;
@@ -1226,6 +1208,7 @@ uint16_t dlmstp_receive(
} }
/* only do receive state machine while we don't have a frame */ /* only do receive state machine while we don't have a frame */
if ((MSTP_Flag.ReceivedValidFrame == false) && if ((MSTP_Flag.ReceivedValidFrame == false) &&
(MSTP_Flag.ReceivedValidFrameNotForUs == false) &&
(MSTP_Flag.ReceivedInvalidFrame == false)) { (MSTP_Flag.ReceivedInvalidFrame == false)) {
MSTP_Receive_Frame_FSM(); MSTP_Receive_Frame_FSM();
} }
@@ -1259,7 +1242,7 @@ void dlmstp_set_mac_address(
mac_address, mac_address,
EEPROM_MSTP_MAC_ADDR); */ EEPROM_MSTP_MAC_ADDR); */
if (mac_address > Nmax_master) if (mac_address > Nmax_master)
dlmstp_set_max_master(mac_address); dlmstp_set_max_master(127);
} }
return; return;
@@ -1281,7 +1264,11 @@ uint8_t dlmstp_mac_address(
void dlmstp_set_max_info_frames( void dlmstp_set_max_info_frames(
uint8_t max_info_frames) uint8_t max_info_frames)
{ {
/* not writable - fixed at 1 */ if (max_info_frames >= 1) {
Nmax_info_frames = max_info_frames;
}
return;
} }
uint8_t dlmstp_max_info_frames( uint8_t dlmstp_max_info_frames(