protected override void FillDescriptor(BitStream buffer)
 {
     buffer
     .Append((short)RecursiveDescriptorLength)
     .Append((byte)Interfaces.Count)
     .Append(Identifier)
     .Append(USBString.FromString(Description).Index)
     .Append((byte)(((SelfPowered ? 1 : 0) << 6) | ((RemoteWakeup ? 1 : 0) << 5)))
     .Append((byte)((MaximalPower + 1) / 2));
 }
 protected override void FillDescriptor(BitStream buffer)
 {
     buffer
     .Append(Identifier)
     .Append(0)     // TODO: implement alternate setting
     .Append((byte)Endpoints.Count)
     .Append((byte)Class)
     .Append(SubClass)
     .Append(Protocol)
     .Append(USBString.FromString(Description).Index);
 }
 protected override void FillDescriptor(BitStream buffer)
 {
     buffer
     .Append((short)CompatibleProtocolVersion)
     .Append((byte)Class)
     .Append(SubClass)
     .Append(Protocol)
     .Append((byte)MaximalPacketSize)
     .Append(VendorId)
     .Append(ProductId)
     .Append(DeviceReleaseNumber)
     .Append(USBString.FromString(ManufacturerName).Index)
     .Append(USBString.FromString(ProductName).Index)
     .Append(USBString.FromString(SerialNumber).Index)
     .Append((byte)Configurations.Count);
 }
        public static USBString FromString(string s)
        {
            if (string.IsNullOrWhiteSpace(s))
            {
                return(Empty);
            }

            var usbString = strings.FirstOrDefault(x => x.Value == s);

            if (usbString == null)
            {
                usbString = new USBString(s, checked ((byte)(strings.Count + 1)));
                strings.Add(usbString);
            }

            return(usbString);
        }
        private BitStream HandleGetDescriptor(ushort value)
        {
            var descriptorType  = (DescriptorType)(value >> 8);
            var descriptorIndex = (byte)value;

            switch (descriptorType)
            {
            case DescriptorType.Device:
                return(GetDescriptor(false));

            case DescriptorType.Configuration:
                if (Configurations.Count < descriptorIndex)
                {
                    device.Log(LogLevel.Warning, "Tried to access a non-existing configuration #{0}", descriptorIndex);
                    return(BitStream.Empty);
                }
                return(Configurations.ElementAt(descriptorIndex).GetDescriptor(true));

            case DescriptorType.String:
            {
                if (descriptorIndex == 0)
                {
                    // special String Index returning a list of supported languages
                    return(USBString.GetSupportedLanguagesDescriptor());
                }
                else
                {
                    var usbString = USBString.FromId(descriptorIndex);
                    if (usbString == null)
                    {
                        device.Log(LogLevel.Warning, "Tried to get non-existing string #{0}", descriptorIndex);
                        return(BitStream.Empty);
                    }

                    return(usbString.GetDescriptor(false));
                }
            }

            default:
                device.Log(LogLevel.Warning, "Unsupported descriptor type: 0x{0:X}", descriptorType);
                return(BitStream.Empty);
            }
        }
 static USBString()
 {
     Empty   = new USBString(string.Empty, 0);
     strings = new List <USBString>();
 }