Exemple #1
0
        private T getValue <T>(byte[] bytes, Endianness endian)
        {
            var genericType = typeof(T);

            if (genericType == typeof(float) && bytes.Length == 4)
            {
                return((T)Convert.ChangeType(ByteParser.ParseFloat(bytes, endian), typeof(float)));
            }
            if (genericType == typeof(double) && bytes.Length == 8)
            {
                return((T)Convert.ChangeType(ByteParser.ParseDouble(bytes, endian), typeof(double)));
            }
            if (genericType == typeof(sbyte) && bytes.Length == 1)
            {
                return((T)Convert.ChangeType(bytes[0], typeof(sbyte)));
            }
            if (genericType == typeof(byte) && bytes.Length == 1)
            {
                return((T)Convert.ChangeType(bytes[0], typeof(byte)));
            }
            if (genericType == typeof(short) && bytes.Length == 2)
            {
                return((T)Convert.ChangeType(ByteParser.ParseShort(bytes, endian), typeof(short)));
            }
            if (genericType == typeof(ushort) && bytes.Length == 2)
            {
                return((T)Convert.ChangeType(ByteParser.ParseUnsignedShort(bytes, endian), typeof(ushort)));
            }
            if (genericType == typeof(int) && bytes.Length == 4)
            {
                return((T)Convert.ChangeType(ByteParser.ParseInt(bytes, endian), typeof(int)));
            }
            if (genericType == typeof(uint) && bytes.Length == 4)
            {
                return((T)Convert.ChangeType(ByteParser.ParseUnsignedInt(bytes, endian), typeof(uint)));
            }
            if (genericType == typeof(long) && bytes.Length == 8)
            {
                return((T)Convert.ChangeType(ByteParser.ParseLong(bytes, endian), typeof(long)));
            }
            if (genericType == typeof(ulong) && bytes.Length == 8)
            {
                return((T)Convert.ChangeType(ByteParser.ParseUnsignedLong(bytes, endian), typeof(ulong)));
            }

            throw new Exception("Invalid number of bytes for " + genericType.Name);
        }
Exemple #2
0
        public object GetDataFromRule(MemoryRule rule)
        {
            var    bytes = GetBytes(rule);
            object data  = null;

            if (rule.DataType == DataType.Data)
            {
                if (rule.NumBytes == 1)
                {
                    data = bytes[0];
                }
                else
                {
                    data = bytes;
                }
            }
            else if (rule.DataType == DataType.Decimal)
            {
                if (rule.NumBytes == 4)
                {
                    data = ByteParser.ParseFloat(bytes, rule.Endianness);
                }
                if (rule.NumBytes == 8)
                {
                    data = ByteParser.ParseDouble(bytes, rule.Endianness);
                }
            }
            else if (rule.DataType == DataType.SignedInteger)
            {
                if (rule.NumBytes == 1)
                {
                    data = (sbyte)bytes[0];
                }
                if (rule.NumBytes == 2)
                {
                    data = ByteParser.ParseShort(bytes, rule.Endianness);
                }
                if (rule.NumBytes == 4)
                {
                    data = ByteParser.ParseInt(bytes, rule.Endianness);
                }
                if (rule.NumBytes == 8)
                {
                    data = ByteParser.ParseLong(bytes, rule.Endianness);
                }
            }
            else if (rule.DataType == DataType.UnsignedInteger)
            {
                if (rule.NumBytes == 1)
                {
                    data = bytes[0];
                }
                if (rule.NumBytes == 2)
                {
                    data = ByteParser.ParseUnsignedShort(bytes, rule.Endianness);
                }
                if (rule.NumBytes == 4)
                {
                    data = ByteParser.ParseUnsignedInt(bytes, rule.Endianness);
                }
                if (rule.NumBytes == 8)
                {
                    data = ByteParser.ParseUnsignedLong(bytes, rule.Endianness);
                }
            }
            if (data == null)
            {
                throw new Exception("Could not parse data from rule");
            }
            return(data);
        }
Exemple #3
0
        private bool isNumberChangedAccordingToRule(byte[] bytes, MemoryRule rule, out IChangedDataContainer changedBytes)
        {
            byte[] diff           = null;
            bool   numbersChanged = false;

            if (rule.DataType == DataType.Decimal)
            {
                if (bytes.Length == 4)
                {
                    var ov = ByteParser.ParseFloat(rule.Bytes, rule.Endianness);
                    var nv = ByteParser.ParseFloat(bytes, rule.Endianness);

                    diff           = BitConverter.GetBytes(nv - ov);
                    numbersChanged = (rule.ChangeTrigger == ChangeTrigger.AnyChange && !ov.Equals(nv) ||
                                      rule.ChangeTrigger == ChangeTrigger.Decrease && ov > nv ||
                                      rule.ChangeTrigger == ChangeTrigger.Increase && ov < nv);
                }
                else if (bytes.Length == 8)
                {
                    var ov = ByteParser.ParseDouble(rule.Bytes, rule.Endianness);
                    var nv = ByteParser.ParseDouble(bytes, rule.Endianness);

                    diff           = BitConverter.GetBytes(nv - ov);
                    numbersChanged = (rule.ChangeTrigger == ChangeTrigger.AnyChange && !ov.Equals(nv) ||
                                      rule.ChangeTrigger == ChangeTrigger.Decrease && ov > nv ||
                                      rule.ChangeTrigger == ChangeTrigger.Increase && ov < nv);
                }
                else
                {
                    throw new Exception("Can not parse decimal. Expected 4 or 8 bytes");
                }
            }
            else if (rule.DataType == DataType.SignedInteger || rule.DataType == DataType.UnsignedInteger)
            {
                bool signed = rule.DataType == DataType.SignedInteger;
                if (bytes.Length == 1)
                {
                    if (signed)
                    {
                        var ov = (sbyte)rule.Bytes[0];
                        var nv = (sbyte)bytes[0];

                        diff           = BitConverter.GetBytes(nv - ov);
                        numbersChanged = (rule.ChangeTrigger == ChangeTrigger.AnyChange && !ov.Equals(nv) ||
                                          rule.ChangeTrigger == ChangeTrigger.Decrease && ov > nv ||
                                          rule.ChangeTrigger == ChangeTrigger.Increase && ov < nv);
                    }
                    else
                    {
                        var ov = rule.Bytes[0];
                        var nv = bytes[0];

                        diff           = BitConverter.GetBytes(nv - ov);
                        numbersChanged = (rule.ChangeTrigger == ChangeTrigger.AnyChange && !ov.Equals(nv) ||
                                          rule.ChangeTrigger == ChangeTrigger.Decrease && ov > nv ||
                                          rule.ChangeTrigger == ChangeTrigger.Increase && ov < nv);
                    }
                }

                else if (bytes.Length == 2)
                {
                    if (signed)
                    {
                        var ov = ByteParser.ParseShort(rule.Bytes, rule.Endianness);
                        var nv = ByteParser.ParseShort(bytes, rule.Endianness);

                        diff           = BitConverter.GetBytes(nv - ov);
                        numbersChanged = (rule.ChangeTrigger == ChangeTrigger.AnyChange && !ov.Equals(nv) ||
                                          rule.ChangeTrigger == ChangeTrigger.Decrease && ov > nv ||
                                          rule.ChangeTrigger == ChangeTrigger.Increase && ov < nv);
                    }
                    else
                    {
                        var ov = ByteParser.ParseUnsignedShort(rule.Bytes, rule.Endianness);
                        var nv = ByteParser.ParseUnsignedShort(bytes, rule.Endianness);

                        diff           = BitConverter.GetBytes(nv - ov);
                        numbersChanged = (rule.ChangeTrigger == ChangeTrigger.AnyChange && !ov.Equals(nv) ||
                                          rule.ChangeTrigger == ChangeTrigger.Decrease && ov > nv ||
                                          rule.ChangeTrigger == ChangeTrigger.Increase && ov < nv);
                    }
                }
                else if (bytes.Length == 4)
                {
                    if (signed)
                    {
                        var ov = ByteParser.ParseInt(rule.Bytes, rule.Endianness);
                        var nv = ByteParser.ParseInt(bytes, rule.Endianness);

                        diff           = BitConverter.GetBytes(nv - ov);
                        numbersChanged = (rule.ChangeTrigger == ChangeTrigger.AnyChange && !ov.Equals(nv) ||
                                          rule.ChangeTrigger == ChangeTrigger.Decrease && ov > nv ||
                                          rule.ChangeTrigger == ChangeTrigger.Increase && ov < nv);
                    }
                    else
                    {
                        var  ov    = ByteParser.ParseUnsignedInt(rule.Bytes, rule.Endianness);
                        var  nv    = ByteParser.ParseUnsignedInt(bytes, rule.Endianness);
                        long diff2 = (long)nv - (long)ov;
                        diff           = BitConverter.GetBytes((int)diff2);
                        numbersChanged = (rule.ChangeTrigger == ChangeTrigger.AnyChange && !ov.Equals(nv) ||
                                          rule.ChangeTrigger == ChangeTrigger.Decrease && ov > nv ||
                                          rule.ChangeTrigger == ChangeTrigger.Increase && ov < nv);
                    }
                }
                else if (bytes.Length == 8)
                {
                    if (signed)
                    {
                        var ov = ByteParser.ParseLong(rule.Bytes, rule.Endianness);
                        var nv = ByteParser.ParseLong(bytes, rule.Endianness);

                        long diff2 = (long)nv - (long)ov;
                        diff           = BitConverter.GetBytes((int)diff2);
                        numbersChanged = (rule.ChangeTrigger == ChangeTrigger.AnyChange && !ov.Equals(nv) ||
                                          rule.ChangeTrigger == ChangeTrigger.Decrease && ov > nv ||
                                          rule.ChangeTrigger == ChangeTrigger.Increase && ov < nv);
                    }
                    else
                    {
                        var ov = ByteParser.ParseUnsignedLong(rule.Bytes, rule.Endianness);
                        var nv = ByteParser.ParseUnsignedLong(bytes, rule.Endianness);

                        long diff2 = (long)nv - (long)ov;
                        diff           = BitConverter.GetBytes((int)diff2);
                        numbersChanged = (rule.ChangeTrigger == ChangeTrigger.AnyChange && !ov.Equals(nv) ||
                                          rule.ChangeTrigger == ChangeTrigger.Decrease && ov > nv ||
                                          rule.ChangeTrigger == ChangeTrigger.Increase && ov < nv);
                    }
                }
                else
                {
                    throw new Exception("Can not parse number. Expected 1, 2, 4 or 8 bytes");
                }
            }
            if (numbersChanged)
            {
                if (rule.TransferType == TransferType.AllBytes)
                {
                    changedBytes = new BytesContainer(bytes, rule.MemoryOffset64);
                    return(true);
                }
                else
                {
                    changedBytes = new NumberDifferenceContainer(diff, rule.MemoryOffset64);
                    return(true);
                }
            }
            changedBytes = null;
            return(false);
        }