public static int Decode(byte[] buffer, int offset, int max_length, out BacnetPtpFrameTypes frame_type, out int msg_length) { frame_type = (BacnetPtpFrameTypes)buffer[offset + 2]; msg_length = (buffer[offset + 3] << 8) | (buffer[offset + 4] << 0); byte crc_header = buffer[offset + 5]; ushort crc_data = 0; if (max_length < PTP_HEADER_LENGTH) { return(-1); //not enough data } if (msg_length > 0) { crc_data = (ushort)((buffer[offset + 6 + msg_length + 1] << 8) | (buffer[offset + 6 + msg_length + 0] << 0)); } if (buffer[offset + 0] != PTP_PREAMBLE1) { return(-1); } if (buffer[offset + 1] != PTP_PREAMBLE2) { return(-1); } if (MSTP.CRC_Calc_Header(buffer, offset + 2, 3) != crc_header) { return(-1); } if (msg_length > 0 && max_length >= (PTP_HEADER_LENGTH + msg_length + 2) && MSTP.CRC_Calc_Data(buffer, offset + 6, msg_length) != crc_data) { return(-1); } return(8 + (msg_length) + (msg_length > 0 ? 2 : 0)); }
public int Send(byte[] buffer, int offset, int data_length, BacnetAddress address, bool wait_for_transmission, int timeout) { BacnetPtpFrameTypes frame_type = BacnetPtpFrameTypes.FRAME_TYPE_DATA0; if (m_sequence_counter) { frame_type = BacnetPtpFrameTypes.FRAME_TYPE_DATA1; } m_sequence_counter = !m_sequence_counter; //add header int full_length = PTP.Encode(buffer, offset - PTP.PTP_HEADER_LENGTH, frame_type, data_length); //wait for send allowed if (!m_may_send.WaitOne(timeout)) { return(-BacnetMstpProtocolTransport.ETIMEDOUT); } //debug if (StateLogging) { Trace.WriteLine(" " + frame_type, null); } //send SendWithXonXoff(buffer, offset - HeaderLength, full_length); return(data_length); }
public static int Decode(byte[] buffer, int offset, int maxLength, out BacnetPtpFrameTypes frameType, out int msgLength) { if (maxLength < PTP_HEADER_LENGTH) // not enough data { frameType = BacnetPtpFrameTypes.FRAME_TYPE_CONNECT_REQUEST; // don't care msgLength = 0; return(-1); //not enough data } frameType = (BacnetPtpFrameTypes)buffer[offset + 2]; msgLength = (buffer[offset + 3] << 8) | (buffer[offset + 4] << 0); var crcHeader = buffer[offset + 5]; ushort crcData = 0; if (msgLength > 0) { if (offset + 6 + msgLength + 1 >= buffer.Length) { return(-1); } crcData = (ushort)((buffer[offset + 6 + msgLength + 1] << 8) | (buffer[offset + 6 + msgLength + 0] << 0)); } if (buffer[offset + 0] != PTP_PREAMBLE1) { return(-1); } if (buffer[offset + 1] != PTP_PREAMBLE2) { return(-1); } if (MSTP.CRC_Calc_Header(buffer, offset + 2, 3) != crcHeader) { return(-1); } if (msgLength > 0 && maxLength >= PTP_HEADER_LENGTH + msgLength + 2 && MSTP.CRC_Calc_Data(buffer, offset + 6, msgLength) != crcData) { return(-1); } return(8 + msgLength + (msgLength > 0 ? 2 : 0)); }
public static int Encode(byte[] buffer, int offset, BacnetPtpFrameTypes frameType, int msgLength) { buffer[offset + 0] = PTP_PREAMBLE1; buffer[offset + 1] = PTP_PREAMBLE2; buffer[offset + 2] = (byte)frameType; buffer[offset + 3] = (byte)((msgLength & 0xFF00) >> 8); buffer[offset + 4] = (byte)((msgLength & 0x00FF) >> 0); buffer[offset + 5] = MSTP.CRC_Calc_Header(buffer, offset + 2, 3); if (msgLength > 0) { //calculate data crc var dataCrc = MSTP.CRC_Calc_Data(buffer, offset + 6, msgLength); buffer[offset + 6 + msgLength + 0] = (byte)(dataCrc & 0xFF); //LSB first! buffer[offset + 6 + msgLength + 1] = (byte)(dataCrc >> 8); } return(PTP_HEADER_LENGTH + msgLength + (msgLength > 0 ? 2 : 0)); }
private void SendFrame(BacnetPtpFrameTypes frameType, byte[] buffer = null, int msgLength = 0) { if (_port == null) { return; } var fullLength = PTP.PTP_HEADER_LENGTH + msgLength + (msgLength > 0 ? 2 : 0); if (buffer == null) { buffer = new byte[fullLength]; } PTP.Encode(buffer, 0, frameType, msgLength); Log.AddCustomEvent(LagoVista.Core.PlatformSupport.LogLevel.Message, "BacnetPtpProtocol", frameType.ToString()); //send SendWithXonXoff(buffer, 0, fullLength); }
private void SendFrame(BacnetPtpFrameTypes frameType, byte[] buffer = null, int msgLength = 0) { if (_port == null) { return; } var fullLength = PTP.PTP_HEADER_LENGTH + msgLength + (msgLength > 0 ? 2 : 0); if (buffer == null) { buffer = new byte[fullLength]; } PTP.Encode(buffer, 0, frameType, msgLength); Log.Debug(frameType); //send SendWithXonXoff(buffer, 0, fullLength); }
private void SendFrame(BacnetPtpFrameTypes frame_type, byte[] buffer = null, int msg_length = 0) { if (m_port == null) { return; } int full_length = PTP.PTP_HEADER_LENGTH + msg_length + (msg_length > 0 ? 2 : 0); if (buffer == null) { buffer = new byte[full_length]; } PTP.Encode(buffer, 0, frame_type, msg_length); //debug if (StateLogging) { Trace.WriteLine(" " + frame_type, null); } //send SendWithXonXoff(buffer, 0, full_length); }
private void SendFrame(BacnetPtpFrameTypes frame_type, byte[] buffer = null, int msg_length = 0) { if (m_port == null) return; int full_length = PTP.PTP_HEADER_LENGTH + msg_length + (msg_length > 0 ? 2 : 0); if(buffer == null) buffer = new byte[full_length]; PTP.Encode(buffer, 0, frame_type, msg_length); //debug if (StateLogging) Trace.WriteLine(" " + frame_type, null); //send SendWithXonXoff(buffer, 0, full_length); }
private void SendDisconnect(BacnetPtpFrameTypes bacnetPtpFrameTypes, BacnetPtpDisconnectReasons bacnetPtpDisconnectReasons) { byte[] buffer = new byte[PTP.PTP_HEADER_LENGTH + 1 + 2]; buffer[PTP.PTP_HEADER_LENGTH] = (byte)bacnetPtpDisconnectReasons; SendFrame(BacnetPtpFrameTypes.FRAME_TYPE_DISCONNECT_REQUEST, buffer, 1); }
private BacnetMstpProtocolTransport.GetMessageStatus GetNextMessage(byte[]buffer, ref int offset, int timeout_ms, out BacnetPtpFrameTypes frame_type, out int msg_length) { BacnetMstpProtocolTransport.GetMessageStatus status; int timeout = timeout_ms; frame_type = BacnetPtpFrameTypes.FRAME_TYPE_HEARTBEAT_XOFF; msg_length = 0; bool compliment_next = false; //fetch header while (offset < PTP.PTP_HEADER_LENGTH) { if (m_port == null) return BacnetMstpProtocolTransport.GetMessageStatus.ConnectionClose; if (offset > 0) timeout = T_FRAME_ABORT; //set sub timeout else timeout = timeout_ms; //set big silence timeout //read int rx = m_port.Read(buffer, offset, PTP.PTP_HEADER_LENGTH - offset, timeout); status = ProcessRxStatus(buffer, ref offset, rx); if (status != BacnetMstpProtocolTransport.GetMessageStatus.Good) return status; //remove XON/XOFF int new_offset = offset + rx; RemoveXonOff(buffer, offset, ref new_offset, ref compliment_next); offset = new_offset; //remove garbage RemoveGarbage(buffer, ref offset); } //greeting if (IsGreeting(buffer, 0, offset)) { //get last byte int rx = m_port.Read(buffer, offset, 1, timeout); status = ProcessRxStatus(buffer, ref offset, rx); if (status != BacnetMstpProtocolTransport.GetMessageStatus.Good) return status; offset += 1; if (IsGreeting(buffer, 0, offset)) { frame_type = BacnetPtpFrameTypes.FRAME_TYPE_GREETING; if (StateLogging) Trace.WriteLine(frame_type, null); return BacnetMstpProtocolTransport.GetMessageStatus.Good; } else { //drop message buffer[0] = 0xFF; RemoveGarbage(buffer, ref offset); return BacnetMstpProtocolTransport.GetMessageStatus.DecodeError; } } //decode if (PTP.Decode(buffer, 0, offset, out frame_type, out msg_length) < 0) { //drop message buffer[0] = 0xFF; RemoveGarbage(buffer, ref offset); return BacnetMstpProtocolTransport.GetMessageStatus.DecodeError; } //valid length? int full_msg_length = msg_length + PTP.PTP_HEADER_LENGTH + (msg_length > 0 ? 2 : 0); if (msg_length > MaxBufferLength) { //drop message buffer[0] = 0xFF; RemoveGarbage(buffer, ref offset); return BacnetMstpProtocolTransport.GetMessageStatus.DecodeError; } //fetch data if (msg_length > 0) { timeout = T_FRAME_ABORT; //set sub timeout while (offset < full_msg_length) { //read int rx = m_port.Read(buffer, offset, full_msg_length - offset, timeout); status = ProcessRxStatus(buffer, ref offset, rx); if (status != BacnetMstpProtocolTransport.GetMessageStatus.Good) return status; //remove XON/XOFF int new_offset = offset + rx; RemoveXonOff(buffer, offset, ref new_offset, ref compliment_next); offset = new_offset; } //verify data crc if (PTP.Decode(buffer, 0, offset, out frame_type, out msg_length) < 0) { //drop message buffer[0] = 0xFF; RemoveGarbage(buffer, ref offset); return BacnetMstpProtocolTransport.GetMessageStatus.DecodeError; } } //debug if (StateLogging) Trace.WriteLine(frame_type, null); //done return BacnetMstpProtocolTransport.GetMessageStatus.Good; }
private BacnetMstpProtocolTransport.GetMessageStatus GetNextMessage(byte[] buffer, ref int offset, int timeoutMs, out BacnetPtpFrameTypes frameType, out int msgLength) { BacnetMstpProtocolTransport.GetMessageStatus status; var timeout = timeoutMs; frameType = BacnetPtpFrameTypes.FRAME_TYPE_HEARTBEAT_XOFF; msgLength = 0; var complimentNext = false; //fetch header while (offset < PTP.PTP_HEADER_LENGTH) { if (_port == null) { return(BacnetMstpProtocolTransport.GetMessageStatus.ConnectionClose); } timeout = offset > 0 ? T_FRAME_ABORT : timeoutMs; //read var rx = _port.Read(buffer, offset, PTP.PTP_HEADER_LENGTH - offset, timeout); status = ProcessRxStatus(buffer, ref offset, rx); if (status != BacnetMstpProtocolTransport.GetMessageStatus.Good) { return(status); } //remove XON/XOFF var newOffset = offset + rx; RemoveXonOff(buffer, offset, ref newOffset, ref complimentNext); offset = newOffset; //remove garbage RemoveGarbage(buffer, ref offset); } //greeting if (IsGreeting(buffer, 0, offset)) { //get last byte var rx = _port.Read(buffer, offset, 1, timeout); status = ProcessRxStatus(buffer, ref offset, rx); if (status != BacnetMstpProtocolTransport.GetMessageStatus.Good) { return(status); } offset += 1; if (IsGreeting(buffer, 0, offset)) { frameType = BacnetPtpFrameTypes.FRAME_TYPE_GREETING; if (StateLogging) { Trace.WriteLine(frameType, null); } return(BacnetMstpProtocolTransport.GetMessageStatus.Good); } //drop message buffer[0] = 0xFF; RemoveGarbage(buffer, ref offset); return(BacnetMstpProtocolTransport.GetMessageStatus.DecodeError); } //decode if (PTP.Decode(buffer, 0, offset, out frameType, out msgLength) < 0) { //drop message buffer[0] = 0xFF; RemoveGarbage(buffer, ref offset); return(BacnetMstpProtocolTransport.GetMessageStatus.DecodeError); } //valid length? var full_msg_length = msgLength + PTP.PTP_HEADER_LENGTH + (msgLength > 0 ? 2 : 0); if (msgLength > MaxBufferLength) { //drop message buffer[0] = 0xFF; RemoveGarbage(buffer, ref offset); return(BacnetMstpProtocolTransport.GetMessageStatus.DecodeError); } //fetch data if (msgLength > 0) { timeout = T_FRAME_ABORT; //set sub timeout while (offset < full_msg_length) { //read var rx = _port.Read(buffer, offset, full_msg_length - offset, timeout); status = ProcessRxStatus(buffer, ref offset, rx); if (status != BacnetMstpProtocolTransport.GetMessageStatus.Good) { return(status); } //remove XON/XOFF var newOffset = offset + rx; RemoveXonOff(buffer, offset, ref newOffset, ref complimentNext); offset = newOffset; } //verify data crc if (PTP.Decode(buffer, 0, offset, out frameType, out msgLength) < 0) { //drop message buffer[0] = 0xFF; RemoveGarbage(buffer, ref offset); return(BacnetMstpProtocolTransport.GetMessageStatus.DecodeError); } } //debug if (StateLogging) { Trace.WriteLine(frameType, null); } //done return(BacnetMstpProtocolTransport.GetMessageStatus.Good); }