Ejemplo n.º 1
0
        public void IntelHexRecordAssertionThrowsExceptionWhenFalse()
        {
            var    record = HexFileLineParser.ParseLine(":10010000214601360121470136007EFE09D2190140");
            Action act    = () => record.Assert(rec => true == false, "Impossible!");

            act.ShouldThrow <IOException>().WithMessage("Impossible*");
        }
        public void HexFileLineParserThrowsExceptionWhenMinimumLengthIsNotReached()
        {
            Action emptyLineAction      = () => HexFileLineParser.ParseLine(string.Empty);
            Action just1CharShortAction = () => HexFileLineParser.ParseLine(":000010015");

            emptyLineAction.ShouldThrow <IOException>().WithMessage("*is too short*");
            just1CharShortAction.ShouldThrow <IOException>().WithMessage("*is too short*");
        }
        public void HexFileLineParserThrowsExceptionForInvalidRecordTypeEnumValue()
        {
            const string invalidRecordType      = ":01ffff06ffe5"; // '06' is not valid a valid recordType
            Action       parseInvalidRecordType = () => HexFileLineParser.ParseLine(invalidRecordType);

            parseInvalidRecordType
            .ShouldThrow <IOException>()
            .WithMessage("Invalid record type value*");
        }
        public void HexFileLineParserThrowsExceptionForInvalidCharactersInRecordType()
        {
            const string invalidRecordType      = ":01ffffGUffe5"; // 'GU' is not valid in hex representation of recordType
            Action       parseInvalidRecordType = () => HexFileLineParser.ParseLine(invalidRecordType);

            parseInvalidRecordType
            .ShouldThrow <IOException>()
            .WithInnerException <FormatException>()
            .WithMessage("Unable to extract record type*");
        }
        public void HexFileLineParserThrowsExceptionForInvalidCharactersInAddress()
        {
            const string invalidAddress      = ":010frf03ffe5"; // 'r' is not valid in hex representation of address
            Action       parseInvalidAddress = () => HexFileLineParser.ParseLine(invalidAddress);

            parseInvalidAddress
            .ShouldThrow <IOException>()
            .WithInnerException <FormatException>()
            .WithMessage("Unable to extract address*");
        }
        public void HexFileLineParserThrowsExceptionForInvalidCharactersInByteCount()
        {
            const string illegalByteCount      = ":uy00000110"; // no legal hex representation for byte count
            Action       parseIllegalByteCount = () => HexFileLineParser.ParseLine(illegalByteCount);

            parseIllegalByteCount
            .ShouldThrow <IOException>()
            .WithInnerException <FormatException>()
            .WithMessage("Unable to extract byte count*");
        }
        public void HexFileLineParserThrowsExceptionWhenCalculatedRequiredLengthIsWrong()
        {
            const string invalidRecord1 = ":100130003F015672B5E712B722B732146013421C7";   // one char too short
            const string invalidRecord2 = ":100130003F0156702B5E712B722B7321460133421C7"; // one char too long

            Action parseInvalidRecord1 = () => HexFileLineParser.ParseLine(invalidRecord1);
            Action parseInvalidRecord2 = () => HexFileLineParser.ParseLine(invalidRecord2);

            parseInvalidRecord1.ShouldThrow <IOException>().WithMessage("*required record length*");
            parseInvalidRecord2.ShouldThrow <IOException>().WithMessage("*required record length*");
        }
        public void HexFileLineParserThrowsExceptionForInvalidCharactersInCheckSum()
        {
            const string invalidChecksum = ":04001000FFAAFFFFt1";
            // 't1' is not valid for a hex representation of a checksum
            Action parseInvalidCheckSum = () => HexFileLineParser.ParseLine(invalidChecksum);

            parseInvalidCheckSum
            .ShouldThrow <IOException>()
            .WithInnerException <FormatException>()
            .WithMessage("Unable to extract checksum for*");
        }
        public void HexFileLineParserThrowsExceptionForInvalidCharactersInByteSequence()
        {
            const string invalidBytes = ":04001000FFAAFFuy01";
            // 'uy' (last byte) is not valid in a hex representation of a byte sequence
            Action parseInvalidBytes = () => HexFileLineParser.ParseLine(invalidBytes);

            parseInvalidBytes
            .ShouldThrow <IOException>()
            .WithInnerException <FormatException>()
            .WithMessage("Unable to extract bytes for*");
        }
        public void HexFileLineParserThrowsExceptionWhenFirstCharacterIsNoColon()
        {
            string randomLine;

            do
            {
                randomLine = Guid.NewGuid().ToString();
            }while (randomLine.StartsWith(":"));

            Action randomStringAction = () => HexFileLineParser.ParseLine(randomLine);

            randomStringAction.ShouldThrow <IOException>().WithMessage("*Illegal line start character*");
        }
        public void HexFileLineParserThrowsExceptionForInvalidCheckSum()
        {
            const string invalidCheckSum1 = ":10010000214601360121470136007EFE09D2190141";
            const string invalidCheckSum2 = ":100130003F0156702B5E712B722B732146013421C3";
            const string invalidCheckSum3 = ":00000001F1";

            Action parseInvalidRecord1 = () => HexFileLineParser.ParseLine(invalidCheckSum1);
            Action parseInvalidRecord2 = () => HexFileLineParser.ParseLine(invalidCheckSum2);
            Action parseInvalidRecord3 = () => HexFileLineParser.ParseLine(invalidCheckSum3);

            parseInvalidRecord1.ShouldThrow <IOException>().WithMessage("Checksum for line*is incorrect*");
            parseInvalidRecord2.ShouldThrow <IOException>().WithMessage("Checksum for line*is incorrect*");
            parseInvalidRecord3.ShouldThrow <IOException>().WithMessage("Checksum for line*is incorrect*");
        }
        public void HexFileLineParserThrowsExceptionLineIsWhenNull()
        {
            Action nullArgAction = () => HexFileLineParser.ParseLine(null);

            nullArgAction.ShouldThrow <IOException>().WithMessage("*can not be null*");
        }
Ejemplo n.º 13
0
    /// <summary>
    /// Load a firmware file from disk.
    /// </summary>
    /// <param name="path"></param>
    /// <returns></returns>
    protected async Task <Firmware?> LoadFromFileAsync(string path, CancellationToken cancellationToken = default)
    {
        if (!File.Exists(path))
        {
            this.Logger.LogError("File does not exist {path}", path);
            return(null);
        }

        var data  = new List <byte>();
        var start = 0;
        var end   = 0;

        using var reader = new StreamReader(path);
        while (!cancellationToken.IsCancellationRequested)
        {
            var line = await reader.ReadLineAsync();

            if (line == null)
            {
                break;
            }

            var record = HexFileLineParser.ParseLine(line);
            if (record.RecordType != RecordType.Data)
            {
                continue;
            }

            if (start == 0 && end == 0)
            {
                start = record.Address;
                end   = record.Address;
            }

            while (record.Address > end)
            {
                data.Add(255);
                end += 1;
            }

            data.AddRange(record.Bytes);
            end += record.Bytes.Length;
        }

        var pad = end % 128;

        foreach (var _ in Enumerable.Range(0, 128 - pad))
        {
            data.Add(255);
            end += 1;
        }

        var blocks = ((end - start) / Const.FirmwareBlockSize);
        var crc    = 0xFFFF;

        foreach (var b in data)
        {
            crc = (crc ^ (b & 0xFF));
            foreach (var j in Enumerable.Range(0, 8))
            {
                var a001 = (crc & 1) > 0;
                crc = (crc >> 1);
                if (a001)
                {
                    crc = (crc ^ 0xA001);
                }
            }
        }

        return(new Firmware
        {
            Blocks = (ushort)blocks,
            Crc = (ushort)crc,
            Data = data,
        });
    }