private USB_STRING_DESCRIPTOR ParseStringDescriptor(EusbPdu pdu)
        {
            if (pdu is EusbUrbCompletionNoDataPdu)
            {
                Site.Log.Add(
                    LogEntryKind.Debug,
                    "Unexpectedly received an EusbUrbCompletionNoDataPdu message: {0}",
                    pdu
                    );
                return(null);
            }

            Site.Assert.IsInstanceOfType(
                pdu,
                typeof(EusbUrbCompletionPdu),
                "Must receive an EusbUrbCompletionPdu message."
                );

            EusbUrbCompletionPdu completionPdu = (EusbUrbCompletionPdu)pdu;

            Site.Assert.IsSuccess(
                (int)completionPdu.HResult,
                "the HResult member of the EusbUrbCompletionPdu must be a successful code."
                );

            USB_STRING_DESCRIPTOR res = UsbStructParser.Parse <USB_STRING_DESCRIPTOR>(completionPdu);

            Site.Assert.IsNotNull(res, "USB_STRING_DESCRIPTOR cannot be parsed from EusbUrbCompletionPdu");
            return(res);
        }
Пример #2
0
        /// <summary>
        /// This method is used to verify the URB_COMPLETION PDU.
        /// </summary>
        /// <param name="responsePdu">The PDU from the client.</param>
        /// <param name="tsUrb">The TS_URB in the request.</param>
        /// <param name="requestCompletion">A unique InterfaceID to be set in the Register Request Callback Message.</param>
        public static void VerifyUrbCompletion(EusbUrbCompletionPdu responsePdu, TS_URB tsUrb, uint requestCompletion)
        {
            Site.Assert.AreEqual <uint>(
                requestCompletion,
                responsePdu.InterfaceId,
                "Expect that the InterfaceId in the response PDU equals the RequestCompletion field of the REGISTER_REQUEST_CALLBACK PDU. The actual value is 0x{0:x8}.",
                responsePdu.InterfaceId);

            Site.Assert.AreEqual <Mask_Values>(
                Mask_Values.STREAM_ID_PROXY,
                responsePdu.Mask,
                "Expect that the Mask in the response PDU is STREAM_ID_PROXY.");

            Site.Assert.AreEqual <FunctionId_Values>(
                FunctionId_Values.URB_COMPLETION,
                (FunctionId_Values)responsePdu.FunctionId,
                "Expect that the FunctionId in the response PDU is CHANNEL_CREATED. The actual value is 0x{0:x8}.",
                responsePdu.FunctionId);

            Site.Assert.AreEqual <uint>(
                tsUrb.Header.RequestId,
                responsePdu.RequestId,
                "Expect that the RequestId in the response PDU equals the RequestId in the request PDU. The actual value is 0x{0:x8}.",
                responsePdu.RequestId);
        }
        public override EusbPdu ParsePdu(byte[] data)
        {
            EusbPdu pdu;

            switch (GetPduType(data))
            {
            case EusbType.RIM_EXCHANGE_CAPABILITY_REQUEST:
                pdu = new EusbRimExchangeCapResponsePdu();
                break;

            case EusbType.ADD_VIRTUAL_CHANNEL:
                pdu = new EusbAddVirtualChannelPdu();
                break;

            case EusbType.ADD_DEVICE:
                pdu = new EusbAddDevicePdu();
                break;

            case EusbType.CHANNEL_CREATED:
                pdu = new EusbChannelCreatedPdu(false);
                break;

            case EusbType.QUERY_DEVICE_TEXT:
                pdu = new EusbQueryDeviceTextResponsePdu();
                break;

            case EusbType.IOCONTROL_COMPLETION:
                pdu = new EusbIoControlCompletionPdu();
                break;

            case EusbType.URB_COMPLETION:
                pdu = new EusbUrbCompletionPdu();
                break;

            case EusbType.URB_COMPLETION_NO_DATA:
                pdu = new EusbUrbCompletionNoDataPdu();
                break;

            default:
                return(base.ParsePdu(data));
            }

            if (!PduMarshaler.Unmarshal(data, pdu))
            {
                pdu = new EusbUnknownPdu();
                PduMarshaler.Unmarshal(data, pdu);
            }
            return(pdu);
        }
        /// <summary>
        /// Parses the structure instance from an EusbUrbCompletionPdu object.
        /// </summary>
        /// <typeparam name="T">The type of the structure</typeparam>
        /// <param name="pdu">The an EusbUrbCompletionPdu object containing the buffer to be parsed</param>
        /// <returns>The structure instance.</returns>
        public static T Parse <T>(EusbUrbCompletionPdu pdu) where T : UsbStructure, new()
        {
            if (null == pdu.OutputBuffer ||
                pdu.OutputBufferSize <= 0 ||
                pdu.HResult != (uint)HRESULT_FROM_WIN32.ERROR_SUCCESS)
            {
                return(default(T));
            }

            T us = new T();

            if (!PduMarshaler.Unmarshal(pdu.OutputBuffer, us))
            {
                return(default(T));
            }
            return(us);
        }
        /// <summary>
        /// Parses all information of the configuration result.
        /// </summary>
        /// <param name="pdu">The EusbUrbCompletionPdu to be parsed.</param>
        /// <returns>true indicates successful.</returns>
        public bool ParseAll(EusbUrbCompletionPdu pdu)
        {
            if (pdu.OutputBuffer == null)
            {
                return(false);
            }
            ms = new MemoryStream(pdu.OutputBuffer);

            USB_CONFIGURATION_DESCRIPTOR cd = UsbStructParser.Parse <USB_CONFIGURATION_DESCRIPTOR>(pdu);

            ms.Seek(USB_CONFIGURATION_DESCRIPTOR.DefaultSize, SeekOrigin.Begin);

            if (ms.Length != cd.wTotalLength)
            {
                return(false);
            }

            configDescriptor = pdu.OutputBuffer;

            USB_INTERFACE_DESCRIPTOR interfaceDes = (USB_INTERFACE_DESCRIPTOR)GetNextDescriptor(UsbDescriptorTypes.USB_INTERFACE_DESCRIPTOR_TYPE);

            while (null != interfaceDes)
            {
                TS_USBD_INTERFACE_INFORMATION interfaceInfo = new TS_USBD_INTERFACE_INFORMATION();
                interfaceInfo.NumberOfPipesExpected = interfaceDes.bNumEndpoints;
                interfaceInfo.InterfaceNumber       = interfaceDes.bInterfaceNumber;
                interfaceInfo.AlternateSetting      = interfaceDes.bAlternateSetting;
                // Padding can be set to any value and MUST be ignored upon receipt.
                interfaceInfo.Padding = PaddingGenerator.GeneratePadding();
                GeneratePipeInformation(interfaceDes, interfaceInfo);
                interfaces.Add(interfaceInfo);

                interfaceDes = (USB_INTERFACE_DESCRIPTOR)GetNextDescriptor(UsbDescriptorTypes.USB_INTERFACE_DESCRIPTOR_TYPE);
            }

            return(true);
        }
        private bool SelectConfiguration(EusbDeviceContext device, byte configIndex)
        {
            // 6. Sends TS_URB_CONTROL_DESCRIPTOR_REQUEST with the descriptor type of USB_DEVICE_DESCRIPTOR_TYPE.
            uint requestId = IdGenerator.NewId();
            TS_URB_CONTROL_DESCRIPTOR_REQUEST des = new UrbBuilder(
                URB_FUNCTIONID.URB_FUNCTION_GET_DESCRIPTOR_FROM_DEVICE,
                requestId,
                0).BuildDeviceDescriptorRequest();

            rdpeusbAdapter.TransferInRequest(device, des, USB_DEVICE_DESCRIPTOR.DefaultSize);

            // 7. Receives a completion message with the result for USB_DEVICE_DESCRIPTOR.");
            EusbUrbCompletionPdu pdu = (EusbUrbCompletionPdu)rdpeusbAdapter.ExpectCompletion(device.VirtualChannel);

            if (null == pdu || pdu.HResult != (uint)HRESULT_FROM_WIN32.ERROR_SUCCESS)
            {
                return(false);
            }
            USB_DEVICE_DESCRIPTOR desDevice = UsbStructParser.Parse <USB_DEVICE_DESCRIPTOR>(pdu);

            // 8. Sends TS_URB_CONTROL_DESCRIPTOR_REQUEST to retrieve the total length of the configuration.
            requestId = IdGenerator.NewId();
            des       = new UrbBuilder(
                URB_FUNCTIONID.URB_FUNCTION_GET_DESCRIPTOR_FROM_DEVICE,
                requestId,
                0).BuildConfigurationDescriptorRequest(configIndex);
            rdpeusbAdapter.TransferInRequest(device, des, USB_CONFIGURATION_DESCRIPTOR.DefaultSize);

            // 9. Receives a completion message with the result for USB_CONFIGURATION_DESCRIPTOR.");
            pdu = (EusbUrbCompletionPdu)rdpeusbAdapter.ExpectCompletion(device.VirtualChannel);
            if (null == pdu || pdu.HResult != (uint)HRESULT_FROM_WIN32.ERROR_SUCCESS)
            {
                return(false);
            }
            USB_CONFIGURATION_DESCRIPTOR desConfig = UsbStructParser.Parse <USB_CONFIGURATION_DESCRIPTOR>(pdu);

            // 10. Sends TS_URB_CONTROL_DESCRIPTOR_REQUEST with the actual length of USB_CONFIGURATION_DESCRIPTOR result.
            requestId = IdGenerator.NewId();
            des       = new UrbBuilder(
                URB_FUNCTIONID.URB_FUNCTION_GET_DESCRIPTOR_FROM_DEVICE,
                requestId,
                0).BuildConfigurationDescriptorRequest(configIndex);
            rdpeusbAdapter.TransferInRequest(device, des, desConfig.wTotalLength);

            // 11. Receives a completion message with the complete result for USB_CONFIGURATION_DESCRIPTOR.");
            pdu = (EusbUrbCompletionPdu)rdpeusbAdapter.ExpectCompletion(device.VirtualChannel);

            // 12. Sends TS_URB_SELECT_CONFIGURATION URB request.
            UsbConfigurationParser configParser = new UsbConfigurationParser();

            configParser.ParseAll(pdu);
            requestId = IdGenerator.NewId();
            TS_URB_SELECT_CONFIGURATION sel = new UrbBuilder(
                URB_FUNCTIONID.URB_FUNCTION_GET_DESCRIPTOR_FROM_DEVICE,
                requestId,
                0).BuildSelectConfigRequest(configParser.Interfaces, configParser.configDescriptor);

            rdpeusbAdapter.TransferInRequest(device, sel, 0);

            // 13. Receives a completion message with the result for configuration selection.");
            EusbUrbCompletionNoDataPdu pduRes = (EusbUrbCompletionNoDataPdu)rdpeusbAdapter.ExpectCompletion(device.VirtualChannel);

            if (null == pduRes || pduRes.HResult != (uint)HRESULT_FROM_WIN32.ERROR_SUCCESS)
            {
                return(false);
            }

            TS_URB_SELECT_CONFIGURATION_RESULT urb = new TS_URB_SELECT_CONFIGURATION_RESULT();

            if (!PduMarshaler.Unmarshal(pduRes.TsUrbResult, urb))
            {
                return(false);
            }

            context.SelectedConfig = urb;
            return(true);
        }