public bool Supports(MesgDefinition mesgDef)
        {
            if (mesgDef == null)
            {
                return(false);
            }

            if (GlobalMesgNum != mesgDef.GlobalMesgNum)
            {
                return(false);
            }

            if (LocalMesgNum != mesgDef.LocalMesgNum)
            {
                return(false);
            }

            foreach (FieldDefinition fieldDef in mesgDef.GetFields())
            {
                FieldDefinition supportedFieldDef = GetField(fieldDef.Num);

                if (supportedFieldDef == null)
                {
                    return(false);
                }
                if (fieldDef.Size > supportedFieldDef.Size)
                {
                    return(false);
                }
            }
            return(true);
        }
Beispiel #2
0
        public void Read(Stream inStream, MesgDefinition defnMesg)
        {
            inStream.Position = 1;
            EndianBinaryReader mesgReader = new EndianBinaryReader(inStream, defnMesg.IsBigEndian);

            LocalNum = defnMesg.LocalMesgNum;

            foreach (FieldDefinition fieldDef in defnMesg.GetFields())
            {
                // It's possible the field type found in the field definition may
                // not agree with the type defined in the profile.  The profile
                // type will be preferred for decode.
                Field field = GetField(fieldDef.Num);
                if (field == null)
                {
                    // We normally won't have fields attached to our skeleton message,
                    // as we add values we need to add the fields too based on the mesg,field
                    // combo in the profile.  Must derive from the profile so the scale etc
                    // is correct
                    field = new Field(Profile.GetMesg(this.Num).GetField(fieldDef.Num));
                    if (field.Num == Fit.FieldNumInvalid)
                    {
                        // If there was no info in the profile the FieldNum will get set to invalid
                        // so preserve the unknown fields info while we know it
                        field.Num  = fieldDef.Num;
                        field.Type = fieldDef.Type;
                    }
                    SetField(field);
                }

                object value;

                // strings may be an array and are of variable length
                if ((field.Type & Fit.BaseTypeNumMask) == Fit.String)
                {
                    List <byte> utf8Bytes = new List <byte>();
                    byte        b         = new byte();

                    for (int i = 0; i < fieldDef.Size; i++)
                    {
                        b = mesgReader.ReadByte();
                        if (b == 0x00)
                        {
                            field.AddValue(utf8Bytes.ToArray());
                            utf8Bytes.Clear();
                        }
                        else
                        {
                            utf8Bytes.Add(b);
                        }
                    }
                    if (utf8Bytes.Count != 0)
                    {
                        field.AddValue(utf8Bytes.ToArray());
                        utf8Bytes.Clear();
                    }
                }
                else
                {
                    int numElements = (int)fieldDef.Size / Fit.BaseType[field.Type & Fit.BaseTypeNumMask].size;
                    for (int i = 0; i < numElements; i++)
                    {
                        switch (field.Type & Fit.BaseTypeNumMask)
                        {
                        case Fit.Enum:
                        case Fit.Byte:
                        case Fit.UInt8:
                        case Fit.UInt8z:
                            value = mesgReader.ReadByte();
                            break;

                        case Fit.SInt8:
                            value = mesgReader.ReadSByte();
                            break;

                        case Fit.SInt16:
                            value = mesgReader.ReadInt16();
                            break;

                        case Fit.UInt16:
                        case Fit.UInt16z:
                            value = mesgReader.ReadUInt16();
                            break;

                        case Fit.SInt32:
                            value = mesgReader.ReadInt32();
                            break;

                        case Fit.UInt32:
                        case Fit.UInt32z:
                            value = mesgReader.ReadUInt32();
                            break;

                        case Fit.Float32:
                            value = mesgReader.ReadSingle();
                            break;

                        case Fit.Float64:
                            value = mesgReader.ReadDouble();
                            break;

                        default:
                            value = mesgReader.ReadBytes(fieldDef.Size);
                            break;
                        }
                        field.SetRawValue(i, value);
                    }
                }
            }
        }