Exemplo n.º 1
0
        public static Tuple <ProtocolVersion, Type> FindForType(MacroOperationType opId)
        {
            if (macroOpTypes == null)
            {
                macroOpTypes = FindAllTypes();
            }

            return(macroOpTypes.TryGetValue(opId, out Tuple <ProtocolVersion, Type> res) ? res : null);
        }
Exemplo n.º 2
0
        public static Type FindForType(MacroOperationType opId)
        {
            if (macroOpTypes == null)
            {
                macroOpTypes = FindAllTypes();
            }

            return(macroOpTypes.TryGetValue(opId, out Type res) ? res : null);
        }
Exemplo n.º 3
0
        private static Tuple <List <Operation>, List <Field> > FakeSpec()
        {
            var operations = new List <Operation>();
            var fields     = new List <Field>();

            IReadOnlyDictionary <MacroOperationType, Tuple <ProtocolVersion, Type> > types = MacroOpManager.FindAll();

            foreach (var t in types)
            {
                MacroOperationType id = t.Key;
                Type type             = t.Value.Item2;

                IEnumerable <PropertyInfo> props = type.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).Where(prop => prop.GetCustomAttribute <NoSerializeAttribute>() == null);

                var opFields = new List <OperationField>();
                foreach (PropertyInfo prop in props)
                {
                    var fieldAttr = prop.GetCustomAttribute <MacroFieldAttribute>();
                    if (fieldAttr == null)
                    {
                        continue;
                    }


                    Tuple <string, bool> mappedType = TypeMappings.MapTypeFull(prop.PropertyType);
                    fields.Add(new Field(fieldAttr.Id, fieldAttr.Name, mappedType.Item1, mappedType.Item2, prop.PropertyType.GetCustomAttributes <FlagsAttribute>().Any() || prop.PropertyType.GetCustomAttributes <XmlAsStringAttribute>().Any()));

                    opFields.Add(new OperationField(fieldAttr.Id, fieldAttr.Name, prop.Name, prop.PropertyType.ToString()));
                }

                operations.Add(new Operation(id.ToString(), type.FullName, opFields, type.GetCustomAttributes <NoMacroFieldsAttribute>().Any()));
            }

            fields     = fields.Distinct().OrderBy(f => f.Id).ToList();
            operations = operations.OrderBy(o => o.Id).ToList();

            var res = new List <Field>();

            foreach (IGrouping <string, Field> grp in fields.GroupBy(f => f.Id))
            {
                FieldEntry[] newTypes = grp.SelectMany(g => g.Entries).ToArray();
                Field        f        = grp.First();
                res.Add(new Field(f.Id, newTypes, f.EnumAsString, f.IsEnum));
            }

            return(Tuple.Create(operations, res));
        }
Exemplo n.º 4
0
 public MacroOperationAttribute(MacroOperationType op, int length) : base(length)
 {
     Operation = op;
 }
Exemplo n.º 5
0
        public static MacroOpBase CreateFromData(byte[] arr, bool safe)
        {
            int opId = (arr[3] << 8) | arr[2];
            MacroOperationType macroOp = (MacroOperationType)opId;

            try
            {
                if (!macroOp.IsValid())
                {
                    throw new SerializationException("FTDa", "Invalid MacroOperationType: {0}", opId);
                }

                var parsed = new ParsedByteArray(arr, false);

                Type type = FindForType(macroOp);
                if (type == null)
                {
                    throw new SerializationException("FTDa", "Failed to find MacroOperationType: {0}", macroOp);
                }

                MacroOpBase cmd = (MacroOpBase)Activator.CreateInstance(type);

                if (!safe)
                {
                    cmd.Deserialize(parsed);
                }
                else
                {
                    AutoSerializeBase.CommandPropertySpec info = AutoSerializeBase.GetPropertySpecForType(cmd.GetType());

                    int attrLength = info.Length;
                    if (attrLength != -1 && attrLength != parsed.BodyLength)
                    {
                        Log.WarnFormat("{0}: Auto deserialize length mismatch", cmd.GetType().Name);
                    }

                    foreach (AutoSerializeBase.PropertySpec prop in info.Properties)
                    {
                        try
                        {
                            prop.Setter?.DynamicInvoke(cmd, prop.SerAttr.Deserialize(parsed.ReverseBytes, parsed.Body, prop.Attr.StartByte, prop.PropInfo));
                        }
                        catch (Exception e)
                        {
                            Log.WarnFormat("{0}: Failed to deserialize property {1}: {2}", cmd.GetType().Name, prop.PropInfo.Name, e.ToString());
                        }
                    }
                }

                return(cmd);
            }
            catch (Exception)
            {
                if (safe)
                {
                    return(null);
                }

                throw;
            }
        }
Exemplo n.º 6
0
 private static T GetDefaultForField <T>(DeviceProfile profile, MacroOperationType op, MacroFieldSpec field)
 {
     return((T)AvailabilityChecker.GetMaxForProperty(profile, string.Format("{0}.{1}", op.ToString(), field.Name)));
 }
Exemplo n.º 7
0
        private static void SetNumericProps(DeviceProfile profile, MacroOperationType op, MacroFieldSpec field, PropertyInfo prop)
        {
            var uint16range = prop.GetCustomAttribute <UInt16RangeAttribute>();

            if (uint16range != null)
            {
                field.Type = MacroFieldType.Int;
                field.Min  = uint16range.Min;
                field.Max  = uint16range.Max;
                return;
            }
            var uint8range = prop.GetCustomAttribute <UInt8RangeAttribute>();

            if (uint8range != null)
            {
                field.Type = MacroFieldType.Int;
                field.Min  = uint8range.Min;
                field.Max  = uint8range.Max;
                return;
            }

            var int16d = prop.GetCustomAttribute <Int16DAttribute>();

            if (int16d != null)
            {
                field.Type  = MacroFieldType.Double;
                field.Min   = int16d.ScaledMin;
                field.Max   = int16d.ScaledMax;
                field.Scale = int16d.Scale;
                return;
            }
            var int32d = prop.GetCustomAttribute <Int32DAttribute>();

            if (int32d != null)
            {
                field.Type  = MacroFieldType.Double;
                field.Min   = int32d.ScaledMin;
                field.Max   = int32d.ScaledMax;
                field.Scale = int32d.Scale;
                return;
            }

            var uint32d = prop.GetCustomAttribute <UInt32DAttribute>();

            if (uint32d != null)
            {
                field.Type  = MacroFieldType.Double;
                field.Min   = (int)uint32d.ScaledMin;
                field.Max   = (int)uint32d.ScaledMax;
                field.Scale = uint32d.Scale;
                return;
            }
            var uint16 = prop.GetCustomAttribute <UInt16Attribute>();

            if (uint16 != null)
            {
                field.Type = MacroFieldType.Int;
                field.Min  = 0;
                field.Max  = (int)(GetDefaultForField <uint?>(profile, op, field) ?? (uint)Math.Pow(2, 16) - 1);
                return;
            }
            var uint8 = prop.GetCustomAttribute <UInt8Attribute>();

            if (uint8 != null)
            {
                field.Type = MacroFieldType.Int;
                field.Min  = 0;
                field.Max  = (int)(GetDefaultForField <uint?>(profile, op, field) ?? (uint)Math.Pow(2, 8) - 1);
                return;
            }

            throw new Exception(string.Format("Unknown field type: {0}.{1}", field.Name, prop.Name));
        }
Exemplo n.º 8
0
 public MacroOperationAttribute(MacroOperationType op, ProtocolVersion minimumVersion, int length) : base(length)
 {
     Operation      = op;
     MinimumVersion = minimumVersion;
 }
Exemplo n.º 9
0
 public MacroOperationAttribute(MacroOperationType op, int length) : this(op, ProtocolVersion.Minimum, length)
 {
 }