using System; using System.Collections.Generic; using System.Text; namespace DirectUSB.VirtualPackets { public class VariableHeaderPacket : VirtualPacket { #region Declarations private string _variableName = String.Empty; private List _attributes; #endregion #region Constructors / Teardown public VariableHeaderPacket(byte[] data) : base(0x000A, data) { int index = 0; int nameLength = (data[0] << 8) | data[1]; for (int i = 0; i < nameLength; i++) _variableName += (char)data[i + 2]; index += 2 + nameLength + 1 + 2; int count = (data[index - 2] << 8) | data[index - 1]; //Interpret the raw data as a list of attributes _attributes = new List(); for (int i = 0; i < count; i++) { short type = (short)((data[index] << 8) | data[index + 1]); bool valid = data[index + 2] == 0 ? true : false; int length; //Apparently if the valid byte is 0x02, there is no data... if (data[index + 2] != 0x00) length = 0; else length = (data[index + 3] << 8) | data[index + 4]; var d = new byte[length]; for (int j = 0; j < d.Length; j++) d[j] = data[index + 5 + j]; //Apparently if the valid byte is 0x02, there is no size bytes... if (data[index + 2] != 0x00) index += 3; else index += 5 + d.Length; Attributes.Attribute a; switch ((DirectUSB.Attributes.Attribute.AttributeType)type) { case DirectUSB.Attributes.Attribute.AttributeType.VariableSize: { a = new Attributes.VariableSizeAttribute(type, valid, d); break; } case DirectUSB.Attributes.Attribute.AttributeType.VariableType: { a = new Attributes.VariableTypeAttribute(type, valid, d); break; } case DirectUSB.Attributes.Attribute.AttributeType.ArchivedStatus: { a = new Attributes.ArchivedStatusAttribute(type, valid, d); break; } default: { a = new Attributes.Attribute(type, valid, d); break; } } _attributes.Add(a); } } #endregion #region Public Properties public string VariableName { get { return _variableName; } } public List Attributes { get { return _attributes; } } #endregion } }