Exemple #1
0
        private T ReadPacket <T>(USBPipe pipe, UInt16 choice = 0, int maxDiscardedPackets = 0)
        {
            T result = default(T);

            lock (pipe)
            {
                byte[] rawHeaderLE = new byte[4];
                usbInterface.InPipe.Read(rawHeaderLE);
                GCHandle handle = GCHandle.Alloc(rawHeaderLE, GCHandleType.Pinned);
                APDU     apdu   = (APDU)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(APDU));
                handle.Free();

                if (apdu.Length > 0)
                {
                    byte[] rawBodyLE = new byte[apdu.Length];
                    usbInterface.InPipe.Read(rawBodyLE);

                    handle = GCHandle.Alloc(rawBodyLE, GCHandleType.Pinned);
                    result = (T)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(T));
                    handle.Free();
                }

                if ((choice != 0) && (apdu.Choice != choice) && (maxDiscardedPackets > 0))
                {
                    // this is not the packet we are looking for
                    return(ReadPacket <T>(pipe, choice, --maxDiscardedPackets));
                }
            }

            return(result);
        }
Exemple #2
0
        private DataPacket ReadIEEE11073Pakcet(USBPipe pipe)
        {
            DataPacket result = new DataPacket();

            lock (pipe)
            {
                byte[] rawHeaderBE = new byte[4];
                usbInterface.InPipe.Read(rawHeaderBE);

                result.APDU = Tools.GetBigEndian <APDU>(rawHeaderBE);


                if (result.APDU.Length > 0)
                {
                    byte[] rawBodyBE = new byte[result.APDU.Length];
                    usbInterface.InPipe.Read(rawBodyBE);
                    result.RawData = rawBodyBE;
                    result.Data    = new DataAPDU();

                    if (result.APDU.Choice == 0xE700)
                    {
                        result.Data = Tools.GetBigEndian <DataAPDU>(rawBodyBE);
                    }
                }
            }

            return(result);
        }
Exemple #3
0
        /// <summary>
        /// Initializes the device via LibUSB, and sets the refresh interval of the controller data
        /// </summary>
        /// <param name="Interval">The interval that the device is polled for information.</param>
        public void Init(int Interval)
        {
            //ErrorCode ec = ErrorCode.None;


            //HKEY_LOCAL_MACHINE\System\CurrentControlSet\Enum\USB\VID_0A7B&PID_D000\6&688a3b8&0&1\Device Parameters\DeviceInterfaceGUIDs
            USBDeviceInfo[] details = USBDevice.GetDevices("{5C2B3F1A-E9B8-4BD6-9D19-8A283B85726E}");
            USBDeviceInfo   match   = details.First(info => info.VID == 0x0A7B && info.PID == 0xD000);

            MyUsbDevice = new USBDevice(match);
            reader      = MyUsbDevice.Interfaces.First().InPipe;
            writer      = MyUsbDevice.Interfaces.First().OutPipe;

            byte[] buf = new byte[64];

            reader.Read(buf);//can't remember why I do this.

            ButtonMasks.InitializeMasks();

            SetPollingInterval(Interval);
            pollTimer.Elapsed += new ElapsedEventHandler(pollTimer_Elapsed);
            pollTimer.Start();

            TestLEDs();
            if (updateGearLights)
            {
                GearLightsRefresh((int)unchecked ((sbyte)buf[25]));
            }
            RefreshLEDState();
        }
 public UsbCommand(USBPipe ep, byte[] buffer, int offset, int length)
 {
     this.offset   = offset;
     this.length   = length;
     this.endPoint = ep;
     this.buffer   = buffer;
 }
        /// <summary>
        /// Initializes the device via LibUSB, and sets the refresh interval of the controller data
        /// </summary>
        /// <param name="Interval">The interval that the device is polled for information.</param>
        public void Init(int Interval)
        {
            //ErrorCode ec = ErrorCode.None;

            //zadig creates a new device GUID everytime you install, so I placed file in
            //C:\Users\salds_000\Documents\Visual Studio 2013\Projects\steel_batallion-64\steelbattalionnet\winusb\usb_driver
            //check inf file for guid. Right clicking the inf file installs it.

            //after installation you can find this in regedit
            //HKEY_LOCAL_MACHINE\System\CurrentControlSet\Enum\USB\VID_0A7B&PID_D000\6&688a3b8&0&1\Device Parameters\DeviceInterfaceGUIDs
            USBDeviceInfo[] details = USBDevice.GetDevices("{5C2B3F1A-E9B8-4BD6-9D19-8A283B85726E}");
            USBDeviceInfo   match   = details.First(info => info.VID == 0x0A7B && info.PID == 0xD000);

            MyUsbDevice = new USBDevice(match);
            reader      = MyUsbDevice.Interfaces.First().InPipe;
            writer      = MyUsbDevice.Interfaces.First().OutPipe;

            byte[] buf = new byte[64];

            reader.Read(buf);//can't remember why I do this.

            ButtonMasks.InitializeMasks();

            SetPollingInterval(Interval);
            pollTimer.Elapsed += new ElapsedEventHandler(pollTimer_Elapsed);
            pollTimer.Start();

            TestLEDs();
            if (updateGearLights)
            {
                GearLightsRefresh((int)unchecked ((sbyte)buf[25]));
            }
            RefreshLEDState();
        }
Exemple #6
0
        internal WinusbAppiDev(USBDeviceInfo di)
        {
            // TODO: Отлавливать исключения WinUsbNet
            try
            {
                Device     = new USBDevice(di);
                DevicePath = di.DevicePath;

                ReadPipe = Device.Pipes.First(p => p.IsIn);
                ReadPipe.Policy.PipeTransferTimeout = 100;
                ReadPipe.Policy.AutoClearStall      = true;
                ReadPipe.Flush();

                WritePipe = Device.Pipes.First(p => p.IsOut);
                WritePipe.Policy.PipeTransferTimeout = 100;

                lock (OpenedDevicesLocker)
                {
                    OpenedDevices.Add(this);
                }
            }
            catch (USBException e)
            {
                throw new AppiConnectoinException(e);
            }
        }
Exemple #7
0
        public TransferQueue(USBPipe pipe, int depth, int bufferSize)
        {
            this.pipe       = pipe;
            this.depth      = depth;
            this.bufferSize = bufferSize;

            this.queue   = new IAsyncResult[depth];
            this.buffers = new byte[depth][];
        }
Exemple #8
0
 public BackendWinUsb(MadWizard.WinUSBNet.USBDeviceInfo deviceInfo)
 {
     // Vendor page and usage_id = 2
     this.packet_size   = 0x200;
     this.vendor_name   = deviceInfo.Manufacturer;
     this.product_name  = deviceInfo.DeviceDescription.First().ToString();
     this.serial_number = ""; // deviceInfo.;
     this.vid           = (UInt16)deviceInfo.VID;
     this.pid           = (UInt16)deviceInfo.PID;
     this.device_info   = deviceInfo;
     this.deviceOut     = null;
     this.deviceIn      = null;
 }
Exemple #9
0
        private byte[] ReadAllFromPipe(USBPipe pipe)
        {
            List <byte> result = new List <byte>();

            while (result.Count < 2 * 1024)
            {
                var task = Task.Run(() => UsbPipeReaderMethod(pipe)); //you can pass parameters to the method as well
                if (task.Wait(TimeSpan.FromSeconds(0.1)))
                {
                    result.Add(task.Result); //the method returns elegantly
                }
                else
                {
                    break; //the method timed-out
                }
            }

            return(result.ToArray());
        }
Exemple #10
0
 public void open()
 {
     try
     {
         // open_path(this.device_info["path"]);
         // this.device.OpenDevice(DeviceMode.NonOverlapped, DeviceMode.NonOverlapped, ShareMode.Exclusive);
         var usbInterface = new MadWizard.WinUSBNet.USBDevice(this.device_info).Interfaces.First(
             usbIf =>
             usbIf.BaseClass == USBBaseClass.VendorSpecific &&
             usbIf.Protocol == 0
             );
         this.deviceOut = usbInterface.Pipes.First(p => p.IsOut);
         this.deviceIn  = usbInterface.Pipes.First(p => p.IsIn);
     }
     catch                                             // (IOError)
     {
         throw new Exception("Unable to open device"); //DAPAccessIntf.DeviceError
     }
 }
Exemple #11
0
        private void WritePacket <T>(USBPipe pipe, UInt16 choice, T dataToSend)
        {
            List <byte> rawData = new List <byte>();

            rawData.AddRange(ObjectToBinaryData <T>(dataToSend));


            // APDU must be converted to little endian so we need some trickery here
            APDU apdu = new APDU()
            {
                Choice = choice, Length = (ushort)rawData.Count
            };

            byte[] apduBE = ObjectToBinaryData <APDU>(apdu);

            rawData.InsertRange(0, new byte[] { apduBE[1], apduBE[0], apduBE[3], apduBE[2] });

            lock (pipe)
            {
                usbInterface.OutPipe.Write(rawData.ToArray());
            }
        }
Exemple #12
0
        private DataPacket ReadPacketRaw(USBPipe pipe)
        {
            DataPacket result = new DataPacket();

            lock (pipe)
            {
                byte[] rawHeaderLE = new byte[4];
                usbInterface.InPipe.Read(rawHeaderLE);
                GCHandle handle = GCHandle.Alloc(rawHeaderLE, GCHandleType.Pinned);
                result.APDU = (APDU)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(APDU));
                handle.Free();

                if (result.APDU.Length > 0)
                {
                    byte[] rawBodyLE = new byte[result.APDU.Length];
                    usbInterface.InPipe.Read(rawBodyLE);
                    result.RawData = rawBodyLE;
                    result.Data    = new DataAPDU();
                }
            }

            return(result);
        }
 public SmartScopeUsbInterfaceWinUsb(USBDevice usbDevice)
 {
     Destroyed = false;
     device    = usbDevice;
     serial    = usbDevice.Descriptor.SerialNumber;
     foreach (USBPipe p in device.Pipes)
     {
         p.Abort();
         USBPipePolicy pol = p.Policy;
         pol.PipeTransferTimeout = USB_TIMEOUT;
         if (p.IsIn)
         {
             p.Flush();
             pol.AllowPartialReads  = true;
             pol.IgnoreShortPackets = false;
         }
     }
     USBPipe[] pipes = device.Pipes.ToArray();
     dataEndpoint         = pipes[0];
     commandWriteEndpoint = pipes[1];
     commandReadEndpoint  = pipes[2];
     LabNation.Common.Logger.Debug("Created new WinUSB ScopeUsbInterface");
 }
Exemple #14
0
 private byte UsbPipeReaderMethod(USBPipe pipe)
 {
     byte[] rawByte = new byte[1];
     pipe.Read(rawByte);
     return(rawByte[0]);
 }
 public UsbCommand(USBPipe ep, byte[] buffer)
 {
     this.endPoint         = ep;
     this.buffer           = buffer;
     this.bytesTransferred = -1;
 }
Exemple #16
0
 //
 //         close the interface
 //
 public void close()
 {
     Trace.TraceInformation("closing Win USB interface");
     this.deviceOut = null;
     this.deviceIn  = null;
 }