private StateChanges PassToken() { BacnetMstpFrameTypes frame_type; byte destination_address; byte source_address; int msg_length; for (int i = 0; i <= m_retry_token; i++) { //send SendFrame(BacnetMstpFrameTypes.FRAME_TYPE_TOKEN, m_NS); //wait for it to be used GetMessageStatus status = GetNextMessage(T_USAGE_TIMEOUT, out frame_type, out destination_address, out source_address, out msg_length); if (status == GetMessageStatus.Good || status == GetMessageStatus.DecodeError) { return(StateChanges.SawTokenUser); //don't remove current message } } //give up m_PS = (byte)((m_NS + 1) % (m_max_master + 1)); m_NS = (byte)m_TS; m_token_count = 0; return(StateChanges.FindNewSuccessor); }
private StateChanges PollForMaster() { BacnetMstpFrameTypes frame_type; byte destination_address; byte source_address; int msg_length; while (true) { //send SendFrame(BacnetMstpFrameTypes.FRAME_TYPE_POLL_FOR_MASTER, m_PS); //wait GetMessageStatus status = GetNextMessage(T_USAGE_TIMEOUT, out frame_type, out destination_address, out source_address, out msg_length); if (status == GetMessageStatus.Good) { try { if (frame_type == BacnetMstpFrameTypes.FRAME_TYPE_REPLY_TO_POLL_FOR_MASTER && destination_address == m_TS) { m_sole_master = false; m_NS = source_address; m_PS = (byte)m_TS; m_token_count = 0; return(StateChanges.ReceivedReplyToPFM); } else { return(StateChanges.ReceivedUnexpectedFrame); } } finally { RemoveCurrentMessage(msg_length); } } if (m_sole_master) { /* SoleMaster */ m_frame_count = 0; return(StateChanges.SoleMaster); } if (m_NS != m_TS) { /* DoneWithPFM */ return(StateChanges.DoneWithPFM); } if ((m_PS + 1) % (m_max_master + 1) != m_TS) { /* SendNextPFM */ m_PS = (byte)((m_PS + 1) % (m_max_master + 1)); continue; } /* DeclareSoleMaster */ m_sole_master = true; m_frame_count = 0; return(StateChanges.DeclareSoleMaster); } }
private StateChanges WaitForReply() { BacnetMstpFrameTypes frame_type; byte destination_address; byte source_address; int msg_length; //fetch message GetMessageStatus status = GetNextMessage(T_REPLY_TIMEOUT, out frame_type, out destination_address, out source_address, out msg_length); if (status == GetMessageStatus.Good) { try { if (destination_address == (byte)m_TS && (frame_type == BacnetMstpFrameTypes.FRAME_TYPE_TEST_RESPONSE || frame_type == BacnetMstpFrameTypes.FRAME_TYPE_BACNET_DATA_NOT_EXPECTING_REPLY)) { //signal upper layer if (MessageRecieved != null && frame_type != BacnetMstpFrameTypes.FRAME_TYPE_TEST_RESPONSE) { BacnetAddress remote_address = new BacnetAddress(BacnetAddressTypes.MSTP, 0, new byte[] { source_address }); try { MessageRecieved(this, m_local_buffer, MSTP.MSTP_HEADER_LENGTH, msg_length, remote_address); } catch (Exception ex) { Trace.TraceError("Exception in MessageRecieved event: " + ex.Message); } } /* ReceivedReply */ return(StateChanges.ReceivedReply); } else if (frame_type == BacnetMstpFrameTypes.FRAME_TYPE_REPLY_POSTPONED) { /* ReceivedPostpone */ return(StateChanges.ReceivedPostpone); } else { /* ReceivedUnexpectedFrame */ return(StateChanges.ReceivedUnexpectedFrame); } } finally { RemoveCurrentMessage(msg_length); } } if (status == GetMessageStatus.Timeout) { /* ReplyTimeout */ m_frame_count = m_max_info_frames; return(StateChanges.ReplyTimeOut); } /* InvalidFrame */ return(StateChanges.InvalidFrame); }
// Just Sniffer mode, no Bacnet activity generated here // Modif FC private void mstp_thread_sniffer() { for (;;) { BacnetMstpFrameTypes frame_type; byte destination_address; byte source_address; int msg_length; try { GetMessageStatus status = GetNextMessage(T_NO_TOKEN, out frame_type, out destination_address, out source_address, out msg_length); if (status == GetMessageStatus.ConnectionClose) { m_port = null; return; } if (status == GetMessageStatus.Good) { // frame event client ? if (RawMessageRecieved != null) { int length = msg_length + MSTP.MSTP_HEADER_LENGTH + (msg_length > 0 ? 2 : 0); // Array copy // after that it could be put asynchronously another time in the Main message loop // without any problem byte[] packet = new byte[length]; Array.Copy(m_local_buffer, 0, packet, 0, length); // No need to use the thread pool, if the pipe is too slow // frames task list will grow infinitly RawMessageRecieved(packet, 0, length); } RemoveCurrentMessage(msg_length); } } catch { m_port = null; } } }
public bool CheckDeviceMessageStatus(int serverAccountId, string messageIdentifier, out bool processedSuccess) { var request = new GetMessageStatus { MessageIdentifier = messageIdentifier }; var response = request.GetResponse(GetApiClient(serverAccountId)); if (!response.Processed) { processedSuccess = false; return false; } processedSuccess = response.ProcessedSuccess; return true; }
private GetMessageStatus GetNextMessage(int timeout_ms, out BacnetMstpFrameTypes frame_type, out byte destination_address, out byte source_address, out int msg_length) { int timeout; frame_type = BacnetMstpFrameTypes.FRAME_TYPE_TOKEN; destination_address = 0; source_address = 0; msg_length = 0; //fetch header while (m_local_offset < MSTP.MSTP_HEADER_LENGTH) { if (m_port == null) { return(GetMessageStatus.ConnectionClose); } if (m_local_offset > 0) { timeout = T_FRAME_ABORT; //set sub timeout } else { timeout = timeout_ms; //set big silence timeout } //read int rx = m_port.Read(m_local_buffer, m_local_offset, MSTP.MSTP_HEADER_LENGTH - m_local_offset, timeout); if (rx == -ETIMEDOUT) { //drop message GetMessageStatus status = m_local_offset == 0 ? GetMessageStatus.Timeout : GetMessageStatus.SubTimeout; m_local_buffer[0] = 0xFF; RemoveGarbage(); return(status); } if (rx < 0) { //drop message m_local_buffer[0] = 0xFF; RemoveGarbage(); return(GetMessageStatus.ConnectionError); } if (rx == 0) { //drop message m_local_buffer[0] = 0xFF; RemoveGarbage(); return(GetMessageStatus.ConnectionClose); } m_local_offset += rx; //remove paddings & garbage RemoveGarbage(); } //decode if (MSTP.Decode(m_local_buffer, 0, m_local_offset, out frame_type, out destination_address, out source_address, out msg_length) < 0) { //drop message m_local_buffer[0] = 0xFF; RemoveGarbage(); return(GetMessageStatus.DecodeError); } //valid length? int full_msg_length = msg_length + MSTP.MSTP_HEADER_LENGTH + (msg_length > 0 ? 2 : 0); if (msg_length > MaxBufferLength) { //drop message m_local_buffer[0] = 0xFF; RemoveGarbage(); return(GetMessageStatus.DecodeError); } //fetch data if (msg_length > 0) { timeout = T_FRAME_ABORT; //set sub timeout while (m_local_offset < full_msg_length) { //read int rx = m_port.Read(m_local_buffer, m_local_offset, full_msg_length - m_local_offset, timeout); if (rx == -ETIMEDOUT) { //drop message GetMessageStatus status = m_local_offset == 0 ? GetMessageStatus.Timeout : GetMessageStatus.SubTimeout; m_local_buffer[0] = 0xFF; RemoveGarbage(); return(status); } if (rx < 0) { //drop message m_local_buffer[0] = 0xFF; RemoveGarbage(); return(GetMessageStatus.ConnectionError); } if (rx == 0) { //drop message m_local_buffer[0] = 0xFF; RemoveGarbage(); return(GetMessageStatus.ConnectionClose); } m_local_offset += rx; } //verify data crc if (MSTP.Decode(m_local_buffer, 0, m_local_offset, out frame_type, out destination_address, out source_address, out msg_length) < 0) { //drop message m_local_buffer[0] = 0xFF; RemoveGarbage(); return(GetMessageStatus.DecodeError); } } //signal frame event if (FrameRecieved != null) { BacnetMstpFrameTypes _frame_type = frame_type; byte _destination_address = destination_address; byte _source_address = source_address; int _msg_length = msg_length; ThreadPool.QueueUserWorkItem((o) => { FrameRecieved(this, _frame_type, _destination_address, _source_address, _msg_length); }, null); } if (StateLogging) { Trace.WriteLine("" + frame_type + " " + destination_address.ToString("X2") + " "); } //done return(GetMessageStatus.Good); }
private StateChanges Idle() { int no_token_timeout = T_NO_TOKEN + 10 * m_TS; BacnetMstpFrameTypes frame_type; byte destination_address; byte source_address; int msg_length; while (m_port != null) { //get message GetMessageStatus status = GetNextMessage(no_token_timeout, out frame_type, out destination_address, out source_address, out msg_length); if (status == GetMessageStatus.Good) { try { if (destination_address == m_TS || destination_address == 0xFF) { switch (frame_type) { case BacnetMstpFrameTypes.FRAME_TYPE_POLL_FOR_MASTER: if (destination_address == 0xFF) { QueueFrame(BacnetMstpFrameTypes.FRAME_TYPE_REPLY_TO_POLL_FOR_MASTER, source_address); } else { //respond to PFM SendFrame(BacnetMstpFrameTypes.FRAME_TYPE_REPLY_TO_POLL_FOR_MASTER, source_address); } break; case BacnetMstpFrameTypes.FRAME_TYPE_TOKEN: if (destination_address != 0xFF) { m_frame_count = 0; m_sole_master = false; return(StateChanges.ReceivedToken); } break; case BacnetMstpFrameTypes.FRAME_TYPE_TEST_REQUEST: if (destination_address == 0xFF) { QueueFrame(BacnetMstpFrameTypes.FRAME_TYPE_TEST_RESPONSE, source_address); } else { //respond to test SendFrame(BacnetMstpFrameTypes.FRAME_TYPE_TEST_RESPONSE, source_address); } break; case BacnetMstpFrameTypes.FRAME_TYPE_BACNET_DATA_NOT_EXPECTING_REPLY: case BacnetMstpFrameTypes.FRAME_TYPE_BACNET_DATA_EXPECTING_REPLY: //signal upper layer if (MessageRecieved != null) { BacnetAddress remote_address = new BacnetAddress(BacnetAddressTypes.MSTP, 0, new byte[] { source_address }); try { MessageRecieved(this, m_local_buffer, MSTP.MSTP_HEADER_LENGTH, msg_length, remote_address); } catch (Exception ex) { Trace.TraceError("Exception in MessageRecieved event: " + ex.Message); } } if (frame_type == BacnetMstpFrameTypes.FRAME_TYPE_BACNET_DATA_EXPECTING_REPLY) { m_reply_source = source_address; m_reply = null; m_reply_mutex.Reset(); return(StateChanges.ReceivedDataNeedingReply); } break; } } } finally { RemoveCurrentMessage(msg_length); } } else if (status == GetMessageStatus.Timeout) { /* GenerateToken */ m_PS = (byte)((m_TS + 1) % (m_max_master + 1)); m_NS = (byte)m_TS; m_token_count = 0; return(StateChanges.GenerateToken); } else if (status == GetMessageStatus.ConnectionClose) { Trace.WriteLine("No connection", null); } else if (status == GetMessageStatus.ConnectionError) { Trace.WriteLine("Connection Error", null); } else { Trace.WriteLine("Garbage", null); } } return(StateChanges.Reset); }
private bool ProcessSharesCheckReceived(string messageIdentifier) { var apiClient = GetApiClient(); var processedSuccess = false; var request = new GetMessageStatus { MessageIdentifier = messageIdentifier }; var startTime = DateTime.UtcNow; while (startTime.AddSeconds(120000) > DateTime.UtcNow) { Thread.Sleep(2000); GetMessageStatus.ResponseParams response; try { response = request.GetResponse(apiClient); } catch (RequestException) { continue; } if (!response.ProcessedSuccess) continue; processedSuccess = true; break; } return processedSuccess; }