Esempio n. 1
0
        public static PreloaderCommand Deserialize(byte[] packet, int length)
        {
            if (packet[0] != DELIMITER || packet[length - 1] != DELIMITER)
            {
                throw new ArgumentException("Not a valid Preloader command");
            }
            byte[] alldata = unescapeBuf(packet, 1, length - 2);
            byte[] payload = new byte[alldata.Length - 2];
            Array.Copy(alldata, payload, payload.Length);

            ushort crc       = Utilities.crc16(payload);
            ushort packetCrc = (ushort)(alldata[alldata.Length - 2] << 8 | (alldata[alldata.Length - 1] & 0xFF));

            if (crc != packetCrc)
            {
                Console.WriteLine("Crc not equal!? {0} {1}", crc, packetCrc);
            }

            CommandType       commandtype = (CommandType)Enum.ToObject(typeof(CommandType), payload[0]);
            IPreloaderPayload preloaderPayload;

            switch (commandtype)
            {
            case CommandType.MagicRsp:
                preloaderPayload = MagicPayload.Deserialize(payload);
                break;

            case CommandType.WriteFlashCmd:
            case CommandType.WriteFlashRsp:
                preloaderPayload = WriteFlashPayload.Deserialize(payload);
                break;

            case CommandType.MessageRsp:
                preloaderPayload = MessagePayload.Deserialize(payload);
                break;

            case CommandType.ErrorRsp:
                preloaderPayload = ErrorPayload.Deserialize(payload);
                break;

            default:
                preloaderPayload = BytePayload.Deserialize(payload);
                break;
            }

            return(new PreloaderCommand(preloaderPayload));
        }