// Simplest technic static byte SetReplyPackets(string PcapFile, int NumberofFrames, int NPDU_offset) { // Read into the Wireshark or tcpdump export file // retrieve the service call InvokeId if any // assume NPDU_offset is the same in all the consecutive response frames, normaly it does PcapDevice pcap = new CaptureFileReaderDevice(PcapFile); RawCapture raw; byte InvokeId = 0; debug_transport.MsgToSendBack = new byte[NumberofFrames][]; for (int i = 0; i < NumberofFrames; i++) { raw = pcap.GetNextPacket(); debug_transport.MsgToSendBack[i] = new byte[raw.Data.Length - NPDU_offset]; Array.Copy(raw.Data, NPDU_offset, debug_transport.MsgToSendBack[i], 0, raw.Data.Length - NPDU_offset); if (i == 0) { BacnetNpduControls npdu_function; BacnetAddress destination, source; byte hop_count; BacnetNetworkMessageTypes nmt; ushort vendor_id; // Find the InvokeId in the first frame // skip the NPDU int offset = NPDU.Decode(debug_transport.MsgToSendBack[0], 0, out npdu_function, out destination, out source, out hop_count, out nmt, out vendor_id); // if -1 we don't care InvokeId = (byte)APDU.GetDecodedInvokeId(debug_transport.MsgToSendBack[0], offset); } } return(InvokeId); // invokeId }
public int Send(byte[] buffer, int offset, int data_length, BacnetAddress address, bool wait_for_transmission, int timeout) { if (m_TS == -1) { throw new Exception("Source address must be set up before sending messages"); } //add to queue BacnetNpduControls function = NPDU.DecodeFunction(buffer, offset); BacnetMstpFrameTypes frame_type = (function & BacnetNpduControls.ExpectingReply) == BacnetNpduControls.ExpectingReply ? BacnetMstpFrameTypes.FRAME_TYPE_BACNET_DATA_EXPECTING_REPLY : BacnetMstpFrameTypes.FRAME_TYPE_BACNET_DATA_NOT_EXPECTING_REPLY; byte[] copy = new byte[data_length + MSTP.MSTP_HEADER_LENGTH + 2]; Array.Copy(buffer, offset, copy, MSTP.MSTP_HEADER_LENGTH, data_length); MessageFrame f = new MessageFrame(frame_type, address.adr[0], copy, data_length); lock (m_send_queue) m_send_queue.AddLast(f); if (m_reply == null) { m_reply = f; m_reply_mutex.Set(); } //wait for message to be sent if (wait_for_transmission) { if (!f.send_mutex.WaitOne(timeout)) { return(-ETIMEDOUT); } } return(data_length); }
public override int Send(byte[] buffer, int offset, int dataLength, BacnetAddress address, bool waitForTransmission, int timeout) { if (SourceAddress == -1) { throw new Exception("Source address must be set up before sending messages"); } //add to queue var function = NPDU.DecodeFunction(buffer, offset); var frameType = (function & BacnetNpduControls.ExpectingReply) == BacnetNpduControls.ExpectingReply ? BacnetMstpFrameTypes.FRAME_TYPE_BACNET_DATA_EXPECTING_REPLY : BacnetMstpFrameTypes.FRAME_TYPE_BACNET_DATA_NOT_EXPECTING_REPLY; var copy = new byte[dataLength + MSTP.MSTP_HEADER_LENGTH + 2]; Array.Copy(buffer, offset, copy, MSTP.MSTP_HEADER_LENGTH, dataLength); var f = new MessageFrame(frameType, address.adr[0], copy, dataLength); lock (_sendQueue) _sendQueue.AddLast(f); if (_reply == null) { _reply = f; _replyMutex.Set(); } if (!waitForTransmission) { return(dataLength); } //wait for message to be sent if (!f.SendMutex.WaitOne(timeout)) { return(-ETIMEDOUT); } return(dataLength); }
private void OnNPDUReceived(IPEndPoint from, Address fromAddress, OctetString linkService, NPDU npdu, ByteStream raw) { try { APDU.APDU apdu = APDU.APDU.Parse(raw); incomingApdu(apdu, fromAddress, linkService); // TODO return APDU.createAPDU(servicesSupported, queue); } catch (System.Exception e) { // If it's already a BACnetException, don't bother wrapping it. //throw e; Debug.Print(e.Message); Debug.Print(e.StackTrace); } /*if (inBuffer.Length > 20) * { * string result = printRouterInfo(inBuffer); * Debug.Print("Sending> " + result); * * using ( * Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, * ProtocolType.Udp)) * { * //EndPoint remoteEP = new IPEndPoint(IPAddress.Parse("192.168.0.14"), 55056); * byte[] messageBytes = Encoding.UTF8.GetBytes(result); * clientSocket.SendTo(messageBytes, remoteEndPoint); * } * * } * else * { * string message = new string(Encoding.UTF8.GetChars(inBuffer)); * Debug.Print("Received> " + message); * }*/ }