示例#1
0
        public static void ParsePacket(byte[] packet, out byte command, out byte[] payload)
        {
            var          headerBytes = new ArraySegment <byte>(packet, 0, HEADER_SIZE).ToArray();
            HeaderStruct header      = StructInterop.ByteArrayToStruct <HeaderStruct>(headerBytes);

            // Payload
            if (header.length > 0)
            {
                payload = new ArraySegment <byte>(packet, HEADER_SIZE, header.length).ToArray();
            }
            else
            {
                payload = null;
            }

            var          footerBytes = new ArraySegment <byte>(packet, HEADER_SIZE + header.length, FOOTER_SIZE).ToArray();
            FooterStruct footer      = StructInterop.ByteArrayToStruct <FooterStruct>(footerBytes);

            // Detect protocol errors
            if (header.command == CMD_ERROR)
            {
                if (payload != null && payload.Length > 0)
                {
                    throw HLDCProtocolException.FromCode(payload[0]);
                }
                else
                {
                    throw new HLDCException("Protocol malformed error message");
                }
            }

            command = header.command;
        }
示例#2
0
        public static byte[] CreatePacket(byte command, byte[] payload = null)
        {
            List <byte> packetBuilder = new List <byte>();

            if (payload != null && payload.Length > MAX_PAYLOAD_LEN)
            {
                throw new Exception("HLDC payload too large");
            }

            packetBuilder.Add(HLDC_FRAME_DELIMITER);

            // Build the packet header
            HeaderStruct header = new HeaderStruct()
            {
                command = command,
                length  = (byte)((payload != null) ? payload.Length : 0)
            };

            packetBuilder.AddRange(EscapeBytes(StructInterop.StructToByteArray <HeaderStruct>(header)));

            // Build the packet payload
            UInt16 crc = 0;

            if (payload != null)
            {
                byte[] escapedPayload = EscapeBytes(payload);
                packetBuilder.AddRange(escapedPayload);
                crc = CalculateCrc(escapedPayload);
            }

            // BUild the packet footer
            FooterStruct footer = new FooterStruct()
            {
                crc = crc,
            };

            packetBuilder.AddRange(EscapeBytes(StructInterop.StructToByteArray <FooterStruct>(footer)));

            packetBuilder.Add(HLDC_FRAME_DELIMITER);

            if (packetBuilder.Count > MAX_PACKET_LEN)
            {
                throw new Exception("HLDC packet too large");
            }

            return(packetBuilder.ToArray());
        }
示例#3
0
        public static void ParsePacket(byte[] packet, out byte command, out byte[] payload)
        {
            // NOTE: Assumes packet has a complete HLDC frame (excluding the SOF/EOF bytes)
            // Use the HLDCFramer helper class to receive a complete HLDC frame

            var          headerBytes = new ArraySegment <byte>(packet, 0, HEADER_SIZE).ToArray();
            HeaderStruct header      = StructInterop.ByteArrayToStruct <HeaderStruct>(headerBytes);

            // Payload
            if (header.length > 0)
            {
                payload = new ArraySegment <byte>(packet, HEADER_SIZE, header.length).ToArray();
            }
            else
            {
                payload = null;
            }

            var          footerBytes = new ArraySegment <byte>(packet, HEADER_SIZE + header.length, FOOTER_SIZE).ToArray();
            FooterStruct footer      = StructInterop.ByteArrayToStruct <FooterStruct>(footerBytes);

            // Detect protocol errors
            if (header.command == CMD_ERROR)
            {
                if (payload != null && payload.Length > 0)
                {
                    throw HLDCProtocolException.FromCode(payload[0]);
                }
                else
                {
                    throw new HLDCException("HLDC Malformed Error Message");
                }
            }

            command = header.command;
        }