Пример #1
0
        internal static void ControlWrite(byte request, int value, short index, byte[] data, int dataLength)
        {
            int            xferLen = 0;
            UsbSetupPacket p       = new UsbSetupPacket(0x40, request, (short)value, index, 100);

            RadioBase.ControlTransfer(ref p, (object)data, dataLength, out xferLen);
            Debug.WriteLine("ControlWrite() writing out: { 40 " + Utility.StringValueOfByte(request) +
                            " " + Utility.StringValueOfByte(BitConverter.GetBytes((short)value), ' ') +
                            " " + Utility.StringValueOfByte(BitConverter.GetBytes(index), ' ') +
                            " " + (data != null && data.Length > 0 ? "[" + Utility.StringValueOfByte(data, ' ') + "]" : "[]") +
                            " }");
        }
Пример #2
0
        private bool Read(IUsbDevice device, int report, byte[] buffer)
        {
            // setup
            var setup = new UsbSetupPacket(
                (byte)UsbCtrlFlags.Direction_In | (byte)UsbCtrlFlags.RequestType_Class | (byte)UsbCtrlFlags.Recipient_Interface, // type
                USB_HID_GET_REPORT,                                                                                              // request
                (short)(USB_HID_INPUT_REPORT | report),                                                                          // wValue
                0,                                                                                                               // windex
                buffer.Length                                                                                                    // wlength
                );

            // request
            var tries = 3;

            do
            {
                try
                {
                    // request control transfer
                    var ret = device.ControlTransfer(setup, buffer, offset: 0, buffer.Length);
                    return(ret == buffer.Length);
                }
                catch (Exception e)
                {
                    Console.WriteLine($"{DateTime.Now:o}: Reading from device failed {e.Message}");
                }

                System.Threading.Thread.Sleep(500);
            }while (--tries > 0);

            return(false);
        }
Пример #3
0
        private int SendControlTransfer(byte[] buffer, byte request, short value, short index, short outLength)
        {
            LibUsbDotNet.Main.UsbSetupPacket setupPacket = new LibUsbDotNet.Main.UsbSetupPacket((byte)UsbRequestType.TypeVendor | BM_REQUEST_DEVICE_TO_HOST, request, value, index, outLength);
            int bytesRead = 0;

            dev.ControlTransfer(ref setupPacket, buffer, 8, out bytesRead);
            return(bytesRead);
        }
Пример #4
0
 public int ReadCommand(UsbCommand cmd, byte[] data, short value = 0, short index = 0)
 {
     return(Device.ControlTransfer(new UsbSetupPacket {
         RequestType = USB_CTRL_IN,
         Request = (byte)cmd,
         Value = value,
         Index = index
     }, data, 0, data.Length));
 }
Пример #5
0
        public void OnControlRequestReceived(ControlRequestEventArgs e)
        {
            if ((e.bmRequestType == 0x80) && (e.bRequest == 0x06))
            {
                //Descriptor request, let the other event handle it
            }
            else if ((e.bmRequestType == 0x00) && (e.bRequest == 0x05))
            {
                //Let the library handle it, needs it to set the address in the Teensy
            }
            else if ((e.bmRequestType == 0x00) && (e.bRequest == 0x09))
            {
                //Let the library handle it, needs it to configure the endpoints in the Teensy
            }
            else
            {
                //Issue the request to the real device, and return whatever it did
                var setup = new UsbSetupPacket((byte)e.bmRequestType, (byte)e.bRequest,
                                               (short)e.wValue, (short)e.wIndex, (short)e.wLength);
                int transferred;

                if ((e.bmRequestType & 0x80) > 0)
                {
                    var ret = new byte[e.wLength];
                    _forwardee.ControlTransfer(ref setup, ret, ret.Length, out transferred);
                    e.ReturnData = new byte[transferred];
                    Array.Copy(ret, 0, e.ReturnData, 0, e.ReturnData.Length);
                }
                else
                {
                    _forwardee.ControlTransfer(ref setup, e.AttachedData, e.AttachedData.Length, out transferred);
                }

                e.Ignore = false;
            }
        }
Пример #6
0
 public int ControlTransfer(UsbSetupPacket setupPacket)
 {
     return(_device.ControlTransfer(setupPacket));
 }