Ejemplo n.º 1
0
 /// <summary>
 /// Initializes a new instance of the LIB USB class.
 /// </summary>
 /// <param name="vendorId">Vendor identifier.</param>
 /// <param name="productId">Product identifier.</param>
 /// <param name="readId">Read identifier.</param>
 /// <param name="writeId">Write identifier.</param>
 public LibUsb(short vendorId, short productId, ReadEndpointID readId, WriteEndpointID writeId)
 {
     this.vendorId  = vendorId;
     this.productId = productId;
     this.readId    = readId;
     this.writeId   = writeId;
     myUsbFinder    = new UsbDeviceFinder(this.vendorId, this.productId);
 }
Ejemplo n.º 2
0
        public UsbDevStream(IUsbDevice dev, WriteEndpointID writePipe, ReadEndpointID readPipe)
        {
            device    = dev;
            WritePipe = writePipe;
            ReadPipe  = readPipe;

            writer = device.OpenEndpointWriter(writePipe);
            reader = device.OpenEndpointReader(readPipe);

            Flush();
        }
Ejemplo n.º 3
0
 public PTPDevice(UsbDevice dev)
 {
     _Device          = dev;
     PTPSupported     = false;
     _Name            = dev.Info.ProductString; // TODO: try get better name
     Reader           = null;
     Writer           = null;
     ConfigurationID  = 1;
     InterfaceID      = 0;
     ReaderEndpointID = ReadEndpointID.Ep01;
     WriterEndpointID = WriteEndpointID.Ep02;
 }
Ejemplo n.º 4
0
 public PTPDevice(UsbDevice dev)
 {
     this.Device = dev;
     this.PTPSupported = false;
     this._Name = dev.Info.ProductString; // TODO: try get better name
     this.Reader = null;
     this.Writer = null;
     this.ConfigurationID = 1;
     this.InterfaceID = 0;
     this.ReaderEndpointID = ReadEndpointID.Ep01;
     this.WriterEndpointID = WriteEndpointID.Ep02;
 }
Ejemplo n.º 5
0
 public PTPDevice(UsbDevice dev)
 {
     _Device = dev;
     PTPSupported = false;
     _Name = dev.Info.ProductString; // TODO: try get better name
     Reader = null;
     Writer = null;
     ConfigurationID = 1;
     InterfaceID = 0;
     ReaderEndpointID = ReadEndpointID.Ep01;
     WriterEndpointID = WriteEndpointID.Ep02;
 }
Ejemplo n.º 6
0
        public UsbLibDotNetHIDDevice()
        {
            this.m_vendorID      = -1;
            this.m_productID     = -1;
            this.m_configID      = 255;
            this.m_interfaceID   = 255;
            this.m_readEndpoint  = ReadEndpointID.Ep01;
            this.m_writeEndpoint = WriteEndpointID.Ep01;

            this.m_usbDevice = null;
            this.m_usbReader = null;
            this.m_usbWriter = null;
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Opens an endpoint for reading
        /// </summary>
        /// <param name="readEndpointID">Endpoint number for read operations.</param>
        /// <param name="readBufferSize">Size of the read buffer allocated for the <see cref="UsbEndpointReader.DataReceived"/> event.</param>
        /// <param name="endpointType">The type of endpoint to open.</param>
        /// <returns>A <see cref="UsbEndpointReader"/> class ready for reading. If the specified endpoint is already been opened, the original <see cref="UsbEndpointReader"/> class is returned.</returns>
        public virtual UsbEndpointReader OpenEndpointReader(ReadEndpointID readEndpointID, int readBufferSize, EndpointType endpointType)
        {
            foreach (UsbEndpointBase activeEndpoint in mActiveEndpoints)
            {
                if (activeEndpoint.EpNum == (byte)readEndpointID)
                {
                    return((UsbEndpointReader)activeEndpoint);
                }
            }

            UsbEndpointReader epNew = new UsbEndpointReader(this, readBufferSize, readEndpointID, endpointType);

            return((UsbEndpointReader)mActiveEndpoints.Add(epNew));
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Opens an endpoint for reading
        /// </summary>
        /// <param name="readEndpointID">Endpoint number for read operations.</param>
        /// <param name="readBufferSize">Size of the read buffer allocated for the <see cref="UsbEndpointReader.DataReceived"/> event.</param>
        /// <param name="endpointType">The type of endpoint to open.</param>
        /// <returns>A <see cref="UsbEndpointReader"/> class ready for reading. If the specified endpoint is already been opened, the original <see cref="UsbEndpointReader"/> class is returned.</returns>
        public virtual UsbEndpointReader OpenEndpointReader(ReadEndpointID readEndpointID, int readBufferSize, EndpointType endpointType)
        {
            foreach (UsbEndpointBase activeEndpoint in mActiveEndpoints)
            {
                if (activeEndpoint.EpNum == (byte)readEndpointID)
                {
                    return((UsbEndpointReader)activeEndpoint);
                }
            }

            byte altIntefaceID = mClaimedInterfaces.Count == 0 ? UsbAltInterfaceSettings[0] : UsbAltInterfaceSettings[mClaimedInterfaces[mClaimedInterfaces.Count - 1]];

            UsbEndpointReader epNew = new UsbEndpointReader(this, readBufferSize, altIntefaceID, readEndpointID, endpointType);

            return((UsbEndpointReader)mActiveEndpoints.Add(epNew));
        }
        public static bool TryExtractEndpointPair(this UsbDevice device, out ReadEndpointID readId, out WriteEndpointID writeId)
        {
            var ids = (
                from info in device.Configs.SelectMany(config => config.InterfaceInfoList)
                let r = info.EndpointInfoList.Select(ep => ep.Descriptor.EndpointID).FirstOrDefault(id => (id & 0x80) > 0)
                        let w = info.EndpointInfoList.Select(ep => ep.Descriptor.EndpointID).FirstOrDefault(id => (id & 0x80) == 0)
                                where r > 0 && w > 0
                                select new
            {
                ReadId = (ReadEndpointID)r,
                WriteId = (WriteEndpointID)w,
            }).FirstOrDefault();

            readId  = ids?.ReadId ?? 0;
            writeId = ids?.WriteId ?? 0;
            return(ids != null);
        }
Ejemplo n.º 10
0
 /// <summary>
 /// ctor.
 /// </summary>
 /// <param name="shouter">The shouter instance.</param>
 /// <param name="vendorId">The vendor id.</param>
 /// <param name="productId">The product id.</param>
 /// <param name="configuration">The configuration id.</param>
 /// <param name="@interface">The interface id.</param>
 /// <param name="readEndpointInt">The read endpoint id.</param>
 /// <param name="writeEndpointInt">The write end point id.</param>
 public RawUsbBulkDevice(
     IShouter shouter,
     int vendorId,
     int productId,
     byte configuration,
     int @interface,
     int readEndpointInt,
     int writeEndpointInt)
 {
     Shouter         = shouter;
     VendorId        = vendorId;
     ProductId       = productId;
     Configuration   = configuration;
     Interface       = @interface;
     ReadEndpointID  = (ReadEndpointID)readEndpointInt;
     WriteEndpointID = (WriteEndpointID)writeEndpointInt;
     Init();
 }
Ejemplo n.º 11
0
 public UsbDevStream OpenStream(WriteEndpointID WriteEp, ReadEndpointID ReadEp) => new UsbDevStream(dev, WriteEp, ReadEp);
Ejemplo n.º 12
0
 /// <summary>
 /// Gets endpoint policies for the specified endpoint id.
 /// </summary>
 /// <param name="epNum">The endpoint ID to retrieve <see cref="PipePolicies"/> for.</param>
 /// <returns>A <see cref="PipePolicies"/> class.</returns>
 public PipePolicies EndpointPolicies(ReadEndpointID epNum)
 {
     return(new PipePolicies(mUsbHandle, (byte)epNum));
 }
Ejemplo n.º 13
0
 internal UsbEndpointReader(UsbDevice usbDevice, int readBufferSize, ReadEndpointID readEndpointID, EndpointType endpointType)
     : base(usbDevice, (Byte)readEndpointID, endpointType)
 {
     mReadBufferSize = readBufferSize;
 }
Ejemplo n.º 14
0
 public abstract UsbEndpointReader OpenEndpointReader(ReadEndpointID readEndpointID);
Ejemplo n.º 15
0
 /// <summary>
 /// Opens a <see cref="EndpointType.Bulk"/> endpoint for reading
 /// </summary>
 /// <param name="readEndpointID">Endpoint number for read operations.</param>
 /// <param name="readBufferSize">Size of the read buffer allocated for the <see cref="UsbEndpointReader.DataReceived"/> event.</param>
 /// <returns>A <see cref="UsbEndpointReader"/> class ready for reading. If the specified endpoint is already been opened, the original <see cref="UsbEndpointReader"/> class is returned.</returns>
 public UsbEndpointReader OpenEndpointReader(ReadEndpointID readEndpointID, int readBufferSize)
 {
     return OpenEndpointReader(readEndpointID, readBufferSize, EndpointType.Bulk);
 }
Ejemplo n.º 16
0
        public static void USBPrint(string printerName, byte[] document)
        {
            _log.Debug(String.Format("USBPrint to printerName: [{0}], document: [{1}]", printerName, document.ToString()));

            UsbDevice         usbDevice;
            UsbEndpointWriter usbWriter;
            ErrorCode         usbErrorCode = ErrorCode.None;

            //int vid = Convert.ToInt32(ConfigurationManager.AppSettings["printerVID"], 16);
            //int pid = Convert.ToInt32(ConfigurationManager.AppSettings["printerPID"], 16);
            //WriteEndpointID endpoint = GetEnumFromString(ConfigurationManager.AppSettings["printerEndPoint"]);
            //string printerPort = ConfigurationManager.AppSettings["printerPort"];
            //string[] printerParam = printerPort.Split('|');
            string[]        printerParam  = printerName.Split('|');
            int             vid           = Convert.ToInt32(printerParam[0], 16);
            int             pid           = Convert.ToInt32(printerParam[1], 16);
            WriteEndpointID endpointWrite = GetWriteEndpointFromString(printerParam[2]);
            ReadEndpointID  endpointRead  = GetReadEndpointFromString(printerParam[2]);

            try
            {
                //Init Usb Finder
                UsbDeviceFinder usbFinder = new UsbDeviceFinder(vid, pid);

                // Find and open the usb device.
                usbDevice = UsbDevice.OpenUsbDevice(usbFinder);

                // If the device is open and ready
                if (usbDevice == null)
                {
                    throw new Exception("Device Not Found.");
                }

                // If this is a "whole" usb device (libusb-win32, linux libusb)
                // it will have an IUsbDevice interface. If not (WinUSB) the
                // variable will be null indicating this is an interface of a
                // device.
                IUsbDevice wholeUsbDevice = usbDevice as IUsbDevice;
                if (!ReferenceEquals(wholeUsbDevice, null))
                {
                    // This is a "whole" USB device. Before it can be used,
                    // the desired configuration and interface must be selected.

                    // Select config
                    wholeUsbDevice.SetConfiguration(1);

                    // Claim interface
                    wholeUsbDevice.ClaimInterface(1);
                }

                // open read endpoint
                UsbEndpointReader reader = usbDevice.OpenEndpointReader(endpointRead);
                reader.DataReceived += Reader_DataReceived;

                // open write endpoint
                usbWriter = usbDevice.OpenEndpointWriter(endpointWrite);

                //byte[] document = Encoding.ASCII.GetBytes("Hello World");

                try
                {
                    // write data, read data
                    int bytesWritten;
                    usbErrorCode = usbWriter.Write(document, 2000, out bytesWritten);

                    if (usbErrorCode != ErrorCode.None)
                    {
                        Close(usbDevice);
                        // Write that output to the console.
                        throw new Exception(UsbDevice.LastErrorString);
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
            catch (Exception ex)
            {
                _log.Error((usbErrorCode != ErrorCode.None ? usbErrorCode + ":" : String.Empty) + ex.Message);
            }
        }
Ejemplo n.º 17
0
 internal UsbEndpointReader(UsbDevice usbDevice, int readBufferSize, byte alternateInterfaceID, ReadEndpointID readEndpointID, EndpointType endpointType)
     : base(usbDevice, alternateInterfaceID, (Byte)readEndpointID, endpointType)
 {
     mReadBufferSize = readBufferSize;
 }
Ejemplo n.º 18
0
 public UsbEndpointReader OpenEndpointReader(ReadEndpointID readEndpointID)
 {
     return usbDevice.OpenEndpointReader(readEndpointID);
 }
Ejemplo n.º 19
0
 /// <summary>
 /// Gets endpoint policies for the specified endpoint id.
 /// </summary>
 /// <param name="epNum">The endpoint ID to retrieve <see cref="PipePolicies"/> for.</param>
 /// <returns>A <see cref="PipePolicies"/> class.</returns>
 public PipePolicies EndpointPolicies(ReadEndpointID epNum)
 {
     return new PipePolicies(mUsbHandle, (byte) epNum);
 }
 public abstract UsbEndpointReader OpenEndpointReader(ReadEndpointID readEndpointID);
Ejemplo n.º 21
0
 public UsbEndpointReader OpenEndpointReader(ReadEndpointID readEndpointID)
 {
     return(usbDevice.OpenEndpointReader(readEndpointID));
 }
Ejemplo n.º 22
0
 internal MonoUsbEndpointReader(UsbDevice usbDevice, int readBufferSize, ReadEndpointID readEndpointID, EndpointType endpointType)
     : base(usbDevice, readBufferSize, readEndpointID, endpointType)
 {
 }
Ejemplo n.º 23
0
 /// <summary>
 /// Opens a <see cref="EndpointType.Bulk"/> endpoint for reading
 /// </summary>
 /// <param name="readEndpointID">Endpoint number for read operations.</param>
 /// <returns>A <see cref="UsbEndpointReader"/> class ready for reading. If the specified endpoint is already been opened, the original <see cref="UsbEndpointReader"/> class is returned.</returns>
 public UsbEndpointReader OpenEndpointReader(ReadEndpointID readEndpointID)
 {
     return OpenEndpointReader(readEndpointID, UsbEndpointReader.DefReadBufferSize);
 }
Ejemplo n.º 24
0
 public UsbEndpointReader OpenEndpointReader(ReadEndpointID readEndpointID, int readBufferSize, EndpointType endpointType)
 {
     return(_device.OpenEndpointReader(readEndpointID, readBufferSize, endpointType));
 }
Ejemplo n.º 25
0
        /// <summary>
        /// Opens an endpoint for reading
        /// </summary>
        /// <param name="readEndpointID">Endpoint number for read operations.</param>
        /// <param name="readBufferSize">Size of the read buffer allocated for the <see cref="UsbEndpointReader.DataReceived"/> event.</param>
        /// <param name="endpointType">The type of endpoint to open.</param>
        /// <returns>A <see cref="UsbEndpointReader"/> class ready for reading. If the specified endpoint is already been opened, the original <see cref="UsbEndpointReader"/> class is returned.</returns>
        public virtual UsbEndpointReader OpenEndpointReader(ReadEndpointID readEndpointID, int readBufferSize, EndpointType endpointType)
        {
            foreach (UsbEndpointBase activeEndpoint in mActiveEndpoints)
                if (activeEndpoint.EpNum == (byte) readEndpointID)
                    return (UsbEndpointReader) activeEndpoint;

            byte altIntefaceID = mClaimedInterfaces.Count == 0 ? UsbAltInterfaceSettings[0] : UsbAltInterfaceSettings[mClaimedInterfaces[mClaimedInterfaces.Count - 1]];

            UsbEndpointReader epNew = new UsbEndpointReader(this, readBufferSize, altIntefaceID, readEndpointID, endpointType);
            return (UsbEndpointReader) mActiveEndpoints.Add(epNew);
        }
Ejemplo n.º 26
0
 public UsbEndpointReader(IUsbDevice usbDevice, int readBufferSize, byte alternateInterfaceID, ReadEndpointID readEndpointID, EndpointType endpointType)
     : base(usbDevice, alternateInterfaceID, (byte)readEndpointID, endpointType)
 {
     this.mReadBufferSize = readBufferSize;
 }
 internal MonoUsbEndpointReader(UsbDevice usbDevice, int readBufferSize, byte alternateInterfaceID, ReadEndpointID readEndpointID, EndpointType endpointType)
     : base(usbDevice, readBufferSize, alternateInterfaceID, readEndpointID, endpointType)
 {
 }
Ejemplo n.º 28
0
        /// <summary>
        /// Opens an endpoint for reading
        /// </summary>
        /// <param name="readEndpointID">Endpoint number for read operations.</param>
        /// <param name="readBufferSize">Size of the read buffer allocated for the <see cref="UsbEndpointReader.DataReceived"/> event.</param>
        /// <param name="endpointType">The type of endpoint to open.</param>
        /// <returns>A <see cref="UsbEndpointReader"/> class ready for reading. If the specified endpoint is already been opened, the original <see cref="UsbEndpointReader"/> class is returned.</returns>
        public UsbEndpointReader OpenEndpointReader(ReadEndpointID readEndpointID, int readBufferSize, EndpointType endpointType)
        {
            byte altIntefaceID = this.mClaimedInterfaces.Count == 0 ? this.usbAltInterfaceSettings[0] : this.usbAltInterfaceSettings[this.mClaimedInterfaces[this.mClaimedInterfaces.Count - 1]];

            return(new UsbEndpointReader(this, readBufferSize, altIntefaceID, readEndpointID, endpointType));
        }
 internal MonoUsbEndpointReader(UsbDevice usbDevice, int readBufferSize, ReadEndpointID readEndpointID, EndpointType endpointType)
     : base(usbDevice, readBufferSize, readEndpointID, endpointType) { }
Ejemplo n.º 30
0
 /// <summary>
 /// Opens a <see cref="EndpointType.Bulk"/> endpoint for reading
 /// </summary>
 /// <param name="readEndpointID">Endpoint number for read operations.</param>
 /// <returns>A <see cref="UsbEndpointReader"/> class ready for reading. If the specified endpoint is already been opened, the original <see cref="UsbEndpointReader"/> class is returned.</returns>
 public UsbEndpointReader OpenEndpointReader(ReadEndpointID readEndpointID)
 {
     return(OpenEndpointReader(readEndpointID, UsbEndpointReader.DefReadBufferSize));
 }
        /// <summary>
        /// Opens an endpoint for reading
        /// </summary>
        /// <param name="readEndpointID">Endpoint number for read operations.</param>
        /// <param name="readBufferSize">Size of the read buffer allocated for the <see cref="UsbEndpointReader.DataReceived"/> event.</param>
        /// <param name="endpointType">The type of endpoint to open.</param>
        /// <returns>A <see cref="UsbEndpointReader"/> class ready for reading. If the specified endpoint is already been opened, the original <see cref="UsbEndpointReader"/> class is returned.</returns>
        public override UsbEndpointReader OpenEndpointReader(ReadEndpointID readEndpointID, int readBufferSize, EndpointType endpointType)
        {
            foreach (UsbEndpointBase activeEndpoint in mActiveEndpoints)
                if (activeEndpoint.EpNum == (byte)readEndpointID) 
                    return (UsbEndpointReader)activeEndpoint;

            UsbEndpointReader epNew = new LibUsbEndpointReader(this, readBufferSize, readEndpointID, endpointType);
            return (UsbEndpointReader) ActiveEndpoints.Add(epNew);
        }
Ejemplo n.º 32
0
 /// <summary>
 /// Opens a <see cref="EndpointType.Bulk"/> endpoint for reading
 /// </summary>
 /// <param name="readEndpointID">Endpoint number for read operations.</param>
 /// <param name="readBufferSize">Size of the read buffer allocated for the <see cref="UsbEndpointReader.DataReceived"/> event.</param>
 /// <returns>A <see cref="UsbEndpointReader"/> class ready for reading. If the specified endpoint is already been opened, the original <see cref="UsbEndpointReader"/> class is returned.</returns>
 public UsbEndpointReader OpenEndpointReader(ReadEndpointID readEndpointID, int readBufferSize)
 {
     return(OpenEndpointReader(readEndpointID, readBufferSize, EndpointType.Bulk));
 }
Ejemplo n.º 33
0
 internal UsbEndpointReader(UsbDevice usbDevice, int readBufferSize, ReadEndpointID readEndpointID, EndpointType endpointType)
     : base(usbDevice, (Byte) readEndpointID, endpointType) { mReadBufferSize = readBufferSize; }
 internal UsbEndpointReader(UsbDevice usbDevice, int readBufferSize, byte alternateInterfaceID, ReadEndpointID readEndpointID, EndpointType endpointType)
     : base(usbDevice, alternateInterfaceID, (Byte)readEndpointID, endpointType)
 {
     mReadBufferSize = readBufferSize;
 }
Ejemplo n.º 35
0
 internal MonoUsbEndpointReader(UsbDevice usbDevice, int readBufferSize, byte alternateInterfaceID, ReadEndpointID readEndpointID, EndpointType endpointType)
     : base(usbDevice, readBufferSize, alternateInterfaceID, readEndpointID, endpointType)
 {
 }
Ejemplo n.º 36
0
        /// <summary>
        /// Get Result Of Latest CMD.
        /// </summary>
        /// <param name="cmd">The Expected Command.</param>
        /// <param name="inPipe">1 For CMD Response and 0 for Image Data.</param>
        /// <returns></returns>
        public async Task <byte[]> GetResults(byte cmd, string cmdName, ReadEndpointID inPipe = ClearShotDevice.GET_COMMAND_RESULT_INPOINT)
        {
            return(await Task.Run(() =>
            {
                if (_targetDevice == null)
                {
                    return null;
                }
                lock (_myLock)
                {
                    //Reader.
                    var reader = _targetDevice.OpenEndpointReader(inPipe);

                    //Get First data.
                    byte[] result = new byte[64];
                    int length; ErrorCode eReturn;
                    if ((eReturn = reader.Read(result, 10000, out length)) == ErrorCode.None)
                    {
                        Debug.WriteLine($"* {cmdName} - {length} bytes read.");
                    }
                    else
                    {
                        Debug.WriteLine($"* {cmdName} - No data to read! " + eReturn);
                    }

                    //Make sure this is result of command that we want.
                    if (result[0] != cmd)
                    {
                        Debug.WriteLine($"* {cmdName} - Result not for expected command.");
                    }
                    else
                    {
                        //Change result depending on size.
                        var size = BitConverter.ToUInt32(result, 1);

                        if (size > result.Count())
                        {
                            Debug.WriteLine($"* {cmdName} - Getting extra data.....");

                            byte[] newResult = new byte[size - result.Length];
                            eReturn = reader.Read(newResult, 10000, out length);
                            if (eReturn == ErrorCode.None)
                            {
                                var newRes = ConcatArrays(result, newResult);
                                result = newRes;
                            }
                            else
                            {
                                Debug.WriteLine($"* {cmdName} - Error reading extra data! " + eReturn);
                            }

                            Debug.WriteLine($"* {cmdName} - Done getting extra data.");
                        }

                        //Remove padded zeros and return.
                        if (size < result.Count())
                        {
                            return result.Take((int)size).ToArray();
                        }
                    }
                    return result;
                }
            }));
        }