Пример #1
0
        public byte[] HandleSetupPacket(SetupPacket packet)
        {
            var result = BitStream.Empty;

            switch (packet.Recipient)
            {
            case PacketRecipient.Device:
                result = HandleRequest(packet);
                break;

            case PacketRecipient.Interface:
                if (SelectedConfiguration == null)
                {
                    device.Log(LogLevel.Warning, "Trying to access interface before selecting a configuration");
                    return(new byte[0]);
                }
                var iface = SelectedConfiguration.Interfaces.FirstOrDefault(x => x.Identifier == packet.Index);
                if (iface == null)
                {
                    device.Log(LogLevel.Warning, "Trying to access a non-existing interface #{0}", packet.Index);
                }
                result = iface.HandleRequest(packet);
                break;

            default:
                device.Log(LogLevel.Warning, "Unsupported recipient type: 0x{0:X}", packet.Recipient);
                break;
            }

            return(result.AsByteArray(packet.Count * 8u));
        }
        public void HandleSetupPacket(SetupPacket packet, Action <byte[]> resultCallback, byte[] additionalData = null)
        {
            var result = BitStream.Empty;

            device.Log(LogLevel.Noisy, "Handling setup packet: {0}", packet);

            if (customSetupPacketHandler != null)
            {
                customSetupPacketHandler(packet, additionalData, receivedBytes =>
                {
                    SendSetupResult(resultCallback, receivedBytes);
                });
            }
            else
            {
                switch (packet.Recipient)
                {
                case PacketRecipient.Device:
                    result = HandleRequest(packet);
                    break;

                case PacketRecipient.Interface:
                    if (SelectedConfiguration == null)
                    {
                        device.Log(LogLevel.Warning, "Trying to access interface before selecting a configuration");
                        resultCallback(new byte[0]);
                        return;
                    }
                    var iface = SelectedConfiguration.Interfaces.FirstOrDefault(x => x.Identifier == packet.Index);
                    if (iface == null)
                    {
                        device.Log(LogLevel.Warning, "Trying to access a non-existing interface #{0}", packet.Index);
                    }
                    result = iface.HandleRequest(packet);
                    break;

                default:
                    device.Log(LogLevel.Warning, "Unsupported recipient type: 0x{0:X}", packet.Recipient);
                    break;
                }

                SendSetupResult(resultCallback, result.AsByteArray(packet.Count * 8u));
            }
        }
        private BitStream HandleRequest(SetupPacket packet)
        {
            if (packet.Type != PacketType.Standard)
            {
                device.Log(LogLevel.Warning, "Non standard requests are not supported");
            }
            else
            {
                switch ((StandardRequest)packet.Request)
                {
                case StandardRequest.SetAddress:
                    Address = checked ((byte)packet.Value);
                    break;

                case StandardRequest.GetDescriptor:
                    if (packet.Direction != Direction.DeviceToHost)
                    {
                        device.Log(LogLevel.Warning, "Wrong direction of Get Descriptor Standard Request");
                        break;
                    }
                    return(HandleGetDescriptor(packet.Value));

                case StandardRequest.SetConfiguration:
                    SelectedConfiguration = Configurations.SingleOrDefault(x => x.Identifier == packet.Value);
                    if (SelectedConfiguration == null)
                    {
                        device.Log(LogLevel.Warning, "Tried to select a non-existing configuration #{0}", packet.Value);
                    }
                    break;

                default:
                    device.Log(LogLevel.Warning, "Unsupported standard request: 0x{0:X}", packet.Request);
                    break;
                }
            }

            return(BitStream.Empty);
        }
 public virtual BitStream HandleRequest(SetupPacket packet)
 {
     return(BitStream.Empty);
 }