Example #1
0
        private byte[] getFrame()
        {
            int addressOffset = parameters.serverAddress.Length + 1;

            stream.WriteByte(0x00); //fcs
            stream.WriteByte(0x00); //fcs
            stream.WriteByte((byte)HDLC_FLAG);
            byte[] data = stream.ToArray();
            data[1] = (byte)(data[1] | ((data.Length - 2) >> 8));
            data[2] = (byte)(data.Length - 2);
            short fcs = HdlcLinkLayer.calcFcs(data, 1, addressOffset + 3);

            data[4 + addressOffset] = (byte)fcs;
            data[5 + addressOffset] = (byte)(fcs >> 8);
            if (data.Length > (addressOffset + 7))
            {
                fcs = HdlcLinkLayer.calcFcs(data, 1, data.Length - 4);
                data[data.Length - 3] = (byte)fcs;
                data[data.Length - 2] = (byte)(fcs >> 8);
            }
            return(data);
        }
Example #2
0
        private void verifyReceivedData(byte[] data)
        {
            if ((data[1] & 0xF0) != HDLC_FORMAT)
            {
                throw new LinkLayerException(LinkLayerExceptionReason.RECEIVED_INVALID_FRAME_FORMAT);
            }
            int frameSize = ((data[1] & 0x0F) << 8) | (data[2] & 0xFF);

            if (frameSize != data.Length - 2)
            {
                throw new LinkLayerException(LinkLayerExceptionReason.RECEIVED_INVALID_FRAME_FORMAT);
            }
            if (parameters.clientAddress != data[3])
            {
                throw new LinkLayerException(LinkLayerExceptionReason.RECEIVED_INVALID_ADDRESS);
            }
            int offset = 4;

            while (offset < frameSize && (data[offset++] & 0x01) != 0x01)
            {
            }

            connection.receivedControl = data[offset++] & 0xFF;
            if (data.Length - offset > 3)
            {
                connection.receivedData = helper.extensions.copyOfRange(data, offset + 2, data.Length - 3);
            }
            else
            {
                connection.receivedData = new byte[0];
            }

            short fcs = BitConverter.ToInt16((new byte[] { data[offset], data[offset + 1] }), 0);

            if (fcs != HdlcLinkLayer.calcFcs(data, 1, offset - 1))
            {
                throw new LinkLayerException(LinkLayerExceptionReason.RECEIVED_INVALID_CHECK_SEQUENCE);
            }

            fcs = BitConverter.ToInt16((new byte[] { data[data.Length - 3], data[data.Length - 3 + 1] }), 0);
            if (fcs != HdlcLinkLayer.calcFcs(data, 1, data.Length - 4))
            {
                throw new LinkLayerException(LinkLayerExceptionReason.RECEIVED_INVALID_CHECK_SEQUENCE);
            }
            connection.isFinalPoll      = (connection.receivedControl & 0x10) == 0x10;
            connection.receivedControl &= 0xEF; //remove p/f bit from control
            connection.receivedRrr      = 0;
            connection.receivedSss      = 0;
            if ((connection.receivedControl & 0x01) == 0x00)
            {
                connection.receivedRrr      = ((int)((uint)connection.receivedControl >> 5)) & 0x07;
                connection.receivedSss      = ((int)((uint)connection.receivedControl >> 1)) & 0x07;
                connection.receivedControl &= 0x01; //remove sss and rrr bits from control
                connection.lastFrameHadSss  = true;
            }
            else if ((connection.receivedControl & 0x02) == 0x00)
            {
                connection.receivedRrr      = ((int)((uint)connection.receivedControl >> 5)) & 0x07;
                connection.receivedControl &= 0x0F; //remove rrr bits from control
            }
            if (connection.receivedControl == FRMR_CONTROL)
            {
                throw new LinkLayerException(LinkLayerExceptionReason.SERVER_REPORTS_FRAME_REJECTED);
            }
        }