public void ReceivedReadPropertyMultipleAck(BacNetRawMessage msg) { ReadPropertyMultipleAck apdu; try { apdu = new ReadPropertyMultipleAck(msg.Apdu); } catch (Exception ex) { _logger.WarnException("Malformed ReadPropertyMultipleAck: ", ex); BacNetDevice.Instance.Waiter = new List <BacNetObject>(); return; } if (BacNetDevice.Instance.Waiter is int && Convert.ToInt32(BacNetDevice.Instance.Waiter) == apdu.InvokeId) { if (apdu.ObjectList == null) { _logger.Warn("Received empty object list"); } BacNetDevice.Instance.Waiter = apdu.ObjectList; } //RpmE BacNetDevice.Instance.Services.Confirmed.RpmCallBack(apdu.InvokeId, apdu.ObjectList); }
public void ReceivedWhoIs(BacNetRawMessage msg) { WhoIs apdu = new WhoIs(msg.Apdu); uint devId = BacNetDevice.Instance.DeviceId; if ((apdu.LowLimit != null && apdu.HighLimit != null && apdu.LowLimit.Value < devId && apdu.HighLimit.Value > devId) || (apdu.LowLimit == null || apdu.HighLimit == null)) { BacNetDevice.Instance.Services.Unconfirmed.IAm(); } }
public void ReceivedEventNotification(BacNetRawMessage msg) { UnconfirmedEventNotification apdu; try { apdu = new UnconfirmedEventNotification(msg.Apdu); } catch { return; } BacNetDevice.Instance.OnNotificationEvent(apdu); }
public void ReceivedSimpleAck(BacNetRawMessage msg) { var npdu = new BacNetIpNpdu(msg.Npdu); var apdu = new SimpleAck(msg.Apdu); //WritePropertyOk if (apdu.ServiceChoise == 15) { BacNetDevice.Instance.Services.Confirmed.WritePropertyCallBack(apdu.InvokeId, "Ok"); } }
private void ParseConfirmed(BacNetRawMessage msg, IPEndPoint endPoint) { if (msg.Apdu.Length > 3) { switch (msg.Apdu[3]) { case 12: BacNetDevice.Instance.Response.ReceivedReadProperty(msg); break; } } }
public void ReceivedReadProperty(BacNetRawMessage msg) { try { ReadProperty apdu = new ReadProperty(msg.Apdu); } catch (Exception ex) { if (ex.Message == "Reject.Missing_required_paramter") { //Отправляем сообщение об ошибке } throw; } }
private void ParseUncofirmed(BacNetRawMessage msg, IPEndPoint endPoint) { switch (msg.Apdu[1]) { case 0: BacNetDevice.Instance.Response.ReceivedIAm(msg, endPoint); break; case 1: BacNetDevice.Instance.Response.ReceivedIHave(msg); break; case 2: BacNetDevice.Instance.Response.ReceivedCovNotification(msg); break; case 3: BacNetDevice.Instance.Response.ReceivedEventNotification(msg); break; case 4: BacNetDevice.Instance.Response.ReceivedPrivateTransfer(msg); break; case 5: BacNetDevice.Instance.Response.ReceivedTextMessage(msg); break; case 6: BacNetDevice.Instance.Response.ReceivedTimeSynchronization(msg); break; case 7: BacNetDevice.Instance.Response.ReceivedWhoHas(msg); break; case 8: BacNetDevice.Instance.Response.ReceivedWhoIs(msg); break; case 9: BacNetDevice.Instance.Response.ReceivedUtcTimeSynchronization(msg); break; } }
public void ReceivedIAm(BacNetRawMessage msg, IPEndPoint endPoint) { BacNetRemoteDevice newDevice = new BacNetRemoteDevice(); newDevice.EndPoint = endPoint; BacNetIpNpdu npdu; IAm apdu; try { npdu = new BacNetIpNpdu(msg.Npdu); apdu = new IAm(msg.Apdu); } catch (Exception ex) { _logger.WarnException("Received malformed I-am", ex); return; } if (npdu.Source != null) { newDevice.BacAddress = npdu.Source; } newDevice.MaxApduLength = apdu.MaxApduLength; newDevice.InstanceNumber = apdu.deviceObject.ObjectId; newDevice.Segmentation = apdu.SegmentationSupported; newDevice.VendorId = apdu.VendorId; if (newDevice.InstanceNumber == BacNetDevice.Instance.DeviceId) { return; } BacNetRemoteDevice rem = BacNetDevice.Instance.Remote.FirstOrDefault(s => s.InstanceNumber == newDevice.InstanceNumber); if (rem != null) { BacNetDevice.Instance.Remote.Remove(rem); } BacNetDevice.Instance.Remote.Add(newDevice); }
public void ReceivedCovNotification(BacNetRawMessage msg) { UnconfirmedCOVnotification apdu; try { apdu = new UnconfirmedCOVnotification(msg.Apdu); } catch { throw; } if (BacNetDevice.Instance.Waiter is int && Convert.ToInt32(BacNetDevice.Instance.Waiter) == apdu.InvokeId) { if (apdu.Object == null) { _logger.Warn("Received empty object"); } BacNetDevice.Instance.Waiter = apdu; } }
public void ReceivedErrorAck(BacNetRawMessage msg) { ErrorAck apdu = new ErrorAck(msg.Apdu); if (apdu.ServiceChoise == 12) { ArrayList res = new ArrayList(); res.Add(apdu.ErrorCode); if (BacNetDevice.Instance.Waiter is int && Convert.ToInt32(BacNetDevice.Instance.Waiter) == apdu.InvokeId) { BacNetDevice.Instance.Waiter = res; } } if (apdu.ServiceChoise == 15) { BacNetDevice.Instance.Services.Confirmed.WritePropertyCallBack(apdu.InvokeId, BacNetEnums.GetErrorMessage((byte)apdu.ErrorCode)); } }
public void ReceivedReadPropertyAck(BacNetRawMessage msg) { ReadPropertyAck apdu; try { apdu = new ReadPropertyAck(msg.Apdu); } catch (Exception ex) { _logger.WarnException("Malformed ReadPropertyAck: ", ex); BacNetDevice.Instance.Waiter = new ArrayList(); return; } if (BacNetDevice.Instance.Waiter is int && Convert.ToInt32(BacNetDevice.Instance.Waiter) == apdu.InvokeId) { BacNetDevice.Instance.Waiter = apdu.Obj.Properties[0].Values; } }
private BacNetRawMessage ReassembleMessage(ArrayList messageList, byte invokeId) { bool firstMsg = true; ArrayList resMsg = new ArrayList(); foreach (var message in messageList) { BacNetRawMessage msg = message as BacNetRawMessage; if (msg == null || msg.Apdu[1] != invokeId) { continue; } if (firstMsg) { resMsg.AddRange(msg.Bvlc); resMsg.AddRange(msg.Npdu); byte[] firstApdu = new byte[msg.Apdu.Length - 2]; firstApdu[0] = msg.Apdu[0]; firstApdu[1] = msg.Apdu[1]; for (int i = 4; i < msg.Apdu.Length; i++) { firstApdu[i - 2] = msg.Apdu[i]; } resMsg.AddRange(firstApdu); firstMsg = false; } else { byte[] anyApdu = new byte[msg.Apdu.Length - 5]; Array.Copy(msg.Apdu, 5, anyApdu, 0, anyApdu.Length); resMsg.AddRange(anyApdu); } } BacNetRawMessage res = new BacNetRawMessage(); byte[] length = ByteConverter.GetBytes((ushort)resMsg.Count); resMsg[2] = length[0]; resMsg[3] = length.Length == 2 ? length[1] : (byte)0; res.All = (byte[])resMsg.ToArray(typeof(byte)); return(res); }
private void ParseBacNetMessage(byte[] bytes, IPEndPoint endPoint) { if (bytes[0] != BacNetEnums.BACNET_BVLC_TYPE_BIP || bytes[4] != BacNetEnums.BACNET_PROTOCOL_VERSION) { return; } BacNetRawMessage msg = new BacNetRawMessage(); msg.All = bytes; if (msg.All == null) { _logger.Info("Malformed packet received."); return; } byte type = (byte)(msg.Apdu[0] >> 4); switch (type) { case 0: ParseConfirmed(msg, endPoint); break; case 1: ParseUncofirmed(msg, endPoint); break; case 2: ParseSimpleAck(msg, endPoint); break; case 3: ParseComplexAck(msg, endPoint); break; case 5: ParseErrorAck(msg, endPoint); break; } }
private void ParseComplexAck(BacNetRawMessage msg, IPEndPoint endPoint) { if (msg.Apdu.Length > 2) { //Сегментированный ответ if ((msg.Apdu[0] >> 3 & 1) == 1) { /*BacNetRawMessage segmentedMsg = null; * foreach (object m in messagePool) * { * BacNetRawMessage ms = m as BacNetRawMessage; * //Если в пуле есть сообщение с таким же InvokeId как у поступившего используем его * if (ms != null && ms.Apdu[1] == msg.Apdu[1]) * segmentedMsg = ms; * } * if (segmentedMsg == null) segmentedMsg = msg;*/ messagePool.Add(msg); BacNetIpNpdu npdu = new BacNetIpNpdu(msg.Npdu); BacNetDevice.Instance.Services.Acknowledgement.SegmentAck(endPoint, npdu.Source, msg.Apdu[1], msg.Apdu[2], msg.Apdu[3]); //Если есть ещё сегменты if ((msg.Apdu[0] >> 2 & 1) == 1) { return; } msg = ReassembleMessage(messagePool, msg.Apdu[1]); } switch (msg.Apdu[2]) { case 12: BacNetDevice.Instance.Response.ReceivedReadPropertyAck(msg); break; case 14: BacNetDevice.Instance.Response.ReceivedReadPropertyMultipleAck(msg); break; } } }
public void ReceivedPrivateTransfer(BacNetRawMessage msg) { //todo: implement method }
public void ReceivedUtcTimeSynchronization(BacNetRawMessage msg) { //todo: implement method }
private void ParseSimpleAck(BacNetRawMessage msg, IPEndPoint endPoint) { BacNetDevice.Instance.Response.ReceivedSimpleAck(msg); }
public void ReceivedWhoHas(BacNetRawMessage msg) { //todo: implement method }
public void ReceivedIHave(BacNetRawMessage msg) { //todo: implement method }