protected virtual byte[] Encapsulate(byte[] frame)
        {
            var result = new List <byte>();

            foreach (var value in frame)
            {
                switch (value)
                {
                case END:
                    result.Add(ESC);
                    result.Add(ESC_END);
                    break;

                case ESC:
                    result.Add(ESC);
                    result.Add(ESC_ESC);
                    break;

                default:
                    result.Add(value);
                    break;
                }
            }
            var engine = new CRCEngine(CRCType.CRC32CCITTPolynomial);
            var crc    = engine.CalculateCrc32(result, 0);

            result.AddRange(new byte[] { (byte)(crc & 0xFF), (byte)((crc >> 8) & 0xFF), (byte)((crc >> 16) & 0xFF), (byte)((crc >> 24) & 0xFF) });
            result.Add(END);
            return(result.ToArray());
        }
Esempio n. 2
0
        public EFR32_GPCRC()
        {
            gpcrc = new CRCEngine(DefaultPolynomial);
            var registerMap = new Dictionary <long, DoubleWordRegister>
            {
                { (long)Registers.Control, new DoubleWordRegister(this)
                  .WithFlag(0, changeCallback: (_, enable) => { isEnabled = enable; }, name: "EN")
                  .WithReservedBits(1, 3)
                  .WithFlag(4, changeCallback: (_, polySel) =>
                    {
                        if (polySel)
                        {
                            this.Log(LogLevel.Warning, "16-bit polynomial is not supported");
                        }
                    }, name: "POLYSEL")
                  .WithReservedBits(5, 3)
                  .WithTaggedFlag("BYTEMODE", 8)
                  .WithTag("BITEREVERSE", 9, 1)
                  .WithTaggedFlag("BYTEREVERSE", 10)
                  .WithReservedBits(11, 2)
                  .WithTaggedFlag("AUTOINIT", 13)
                  .WithReservedBits(14, 18) },
                { (long)Registers.Command, new DoubleWordRegister(this)
                  .WithFlag(0, FieldMode.Write, changeCallback: (_, value) => { if (value)
                                                                                {
                                                                                    UpdateInitVal();
                                                                                }
                            }, name: "INIT")
                  .WithReservedBits(1, 31) },
                { (long)Registers.Init, new DoubleWordRegister(this)
                  .WithValueField(0, 32, out initDataField, name: "INIT") },
                { (long)Registers.InputData, new DoubleWordRegister(this)
                  .WithValueField(0, 32, FieldMode.Write, writeCallback: (_, value) =>
                    {
                        crc.Value = gpcrc.CalculateCrc32(BitConverter.GetBytes(value), crc.Value);
                    }, name: "INPUTDATA") },
                { (long)Registers.Data, new DoubleWordRegister(this)
                  .WithValueField(0, 32, out crc, FieldMode.Read, name: "DATA") },
            };

            registers = new DoubleWordRegisterCollection(this, registerMap);
        }
        public static byte[] CalculateCRC(IEnumerable <byte> bytes, uint crcInitialValue = 0x0, CRCType crcPolynomial = CRCType.CRC16CCITTPolynomial)
        {
            uint      crc       = crcInitialValue;
            CRCEngine crcEngine = new CRCEngine(crcPolynomial);
            var       crcLength = crcPolynomial.GetLength();

            // Byte little endian order
            switch (crcLength)
            {
            case 2:
                crc = crcEngine.CalculateCrc16(bytes, (ushort)crc);
                return(new[] { (byte)crc, (byte)(crc >> 8) });

            case 4:
                crc = crcEngine.CalculateCrc32(bytes, crc);
                return(new[] { (byte)crc, (byte)(crc >> 8), (byte)(crc >> 16), (byte)(crc >> 24) });

            default:
                Logger.Log(LogLevel.Error, "Cannot calculate CRC of invalid length {0}", crcLength);
                return(new byte[crcLength]);
            }
        }