Exemple #1
0
 public CH34xSerialDevice(UsbDevice device, UsbDeviceConnection connection, int iface) : base(device, connection)
 {
     hexData       = new HexData();
     rtsCtsEnabled = false;
     dtrDsrEnabled = false;
     mInterface    = device.GetInterface(iface >= 0 ? iface : 0);
 }
Exemple #2
0
        public SmartScopeUsbInterfaceXamarin(Context context, UsbManager usbManager, UsbDevice device)
        {
            Destroyed = false;
            if (!usbManager.HasPermission(device))
            {
                Logger.Error("Permission denied");
                throw new Exception("Device permission not obtained");
            }

            UsbInterface interf = device.GetInterface(0);

            for (int i = 0; i < interf.EndpointCount; i++)
            {
                if (interf.GetEndpoint(i).EndpointNumber == 1)
                {
                    dataEndpoint = interf.GetEndpoint(i);
                }
                else if (interf.GetEndpoint(i).EndpointNumber == 2)
                {
                    commandWriteEndpoint = interf.GetEndpoint(i);
                }
                else if (interf.GetEndpoint(i).EndpointNumber == 3)
                {
                    commandReadEndpoint = interf.GetEndpoint(i);
                }
            }
            usbConnection = usbManager.OpenDevice(device);
            usbConnection.ClaimInterface(interf, true);
        }
        public override void Open(UsbDeviceConnection connection)
        {
            _connection = connection;

            _controlInterface = Device.GetInterface(0);
            if (!connection.ClaimInterface(_controlInterface, true))
            {
                throw new Exception("Could not claim control interface.");
            }

            _controlEndpoint = _controlInterface.GetEndpoint(0);

            _dataInterface = Device.GetInterface(1);
            if (!connection.ClaimInterface(_dataInterface, true))
            {
                throw new Exception("Could not claim data interface.");
            }

            if (Device.VendorId == 0x1cbe)
            {
                _writeEndpoint = _dataInterface.GetEndpoint(1);
                _readEndpoint  = _dataInterface.GetEndpoint(0);
            }
            else
            {
                _writeEndpoint = _dataInterface.GetEndpoint(0);
                _readEndpoint  = _dataInterface.GetEndpoint(1);
            }

            Init();
            var buadSet = SetBaudrate(960000);
        }
        internal virtual void OpenInterface()
        {
            Log.Debug(nameof(CdcAcmSerialDriver), "claiming interfaces, count=" + _driver.Device.InterfaceCount);

            _controlInterface = _driver.Device.GetInterface(0);
            Log.Debug(nameof(CdcAcmSerialDriver), "Control iface=" + _controlInterface);

            if (!_connection.ClaimInterface(_controlInterface, true))
            {
                throw new UsbSerialException("Could not claim control interface.");
            }

            _controlEndpoint = _controlInterface.GetEndpoint(0);
            Log.Debug(nameof(CdcAcmSerialDriver), "Control endpoint direction: " + _controlEndpoint.Direction);

            Log.Debug(nameof(CdcAcmSerialDriver), "Claiming data interface.");
            _dataInterface = _driver.Device.GetInterface(1);
            Log.Debug(nameof(CdcAcmSerialDriver), "data iface=" + _dataInterface);

            if (!_connection.ClaimInterface(_dataInterface, true))
            {
                throw new UsbSerialException("Could not claim data interface.");
            }
            _readEndpoint = _dataInterface.GetEndpoint(1);
            Log.Debug(nameof(CdcAcmSerialDriver), "Read endpoint direction: " + _readEndpoint.Direction);
            _writeEndpoint = _dataInterface.GetEndpoint(0);
            Log.Debug(nameof(CdcAcmSerialDriver), "Write endpoint direction: " + _writeEndpoint.Direction);
        }
Exemple #5
0
        /// <summary>
        ///
        /// </summary>
        public virtual UsbInterface FindInterface(UsbDevice device)
        {
            UsbInterface        usbIntf = null;
            List <UsbInterface> usbIntfs = device.GetInterfaces();
            int i = 0, imax = usbIntfs.Count;

            if (m_Class != -1 && m_Subclass != -1 && m_Protocol != -1)  // Get the given UsbInterface.
            {
                for (; i < imax; ++i)
                {
                    usbIntf = usbIntfs[i];
                    if (usbIntf.GetInterfaceClass() == m_Class)
                    {
                        if (usbIntf.GetInterfaceSubclass() == m_Subclass)
                        {
                            if (usbIntf.GetInterfaceProtocol() == m_Protocol)
                            {
                                break;
                            }
                        }
                    }
                }
            }
            else
            {
                usbIntf = usbIntfs[0];
            }
            //m_Class=m_Subclass=m_Protocol=-1;//Reset filters.???

            return(usbIntf);
        }
Exemple #6
0
            private void openInterface()
            {
                Log.Debug(TAG, "claiming interfaces, count=" + mDevice.InterfaceCount);

                mControlInterface = mDevice.GetInterface(0);
                Log.Debug(TAG, "Control iface=" + mControlInterface);
                // class should be USB_CLASS_COMM

                if (!mConnection.ClaimInterface(mControlInterface, true))
                {
                    throw new IOException("Could not claim control interface.");
                }

                mControlEndpoint = mControlInterface.GetEndpoint(0);
                Log.Debug(TAG, "Control endpoint direction: " + mControlEndpoint.Direction);

                Log.Debug(TAG, "Claiming data interface.");
                mDataInterface = mDevice.GetInterface(1);
                Log.Debug(TAG, "data iface=" + mDataInterface);
                // class should be USB_CLASS_CDC_DATA

                if (!mConnection.ClaimInterface(mDataInterface, true))
                {
                    throw new IOException("Could not claim data interface.");
                }
                mReadEndpoint = mDataInterface.GetEndpoint(1);
                Log.Debug(TAG, "Read endpoint direction: " + mReadEndpoint.Direction);
                mWriteEndpoint = mDataInterface.GetEndpoint(0);
                Log.Debug(TAG, "Write endpoint direction: " + mWriteEndpoint.Direction);
            }
        /// <summary>
        /// Retreive bulk Enpoints from the interface
        /// </summary>
        /// <param name="usbIface"></param>
        /// <param name="epIn"></param>
        /// <param name="epOut"></param>
        /// <returns></returns>
        protected int InterfaceGetBulkEndpoints(UsbInterface usbIface, ref UsbEndpoint epIn, ref UsbEndpoint epOut)
        {
            int i;
            int ret;

            // We iterate over over the interface endpoint to find bulk in and out
            for (i = 0, ret = 0; (i < usbIface.EndpointCount) && (ret < 2); i++)
            {
                UsbEndpoint ep = usbIface.GetEndpoint(i);
                if (ep.Type == UsbAddressing.XferBulk)
                {
                    if (ep.Direction == UsbAddressing.In)
                    {
                        epIn = ep;
                        ret++;
                    }
                    else if (ep.Direction == UsbAddressing.Out)
                    {
                        epOut = ep;
                        ret++;
                    }
                }
            }
            // Did we find the two needed endpoints ?
            return((ret == 2) ? 0 : -1);
        }
Exemple #8
0
        public CardReader(UsbDevice device)
        {
            _device    = device;
            _interface = device.GetInterface(0);

            for (var i = 0; i < _interface.EndpointCount; i++)
            {
                var endpoint = _interface.GetEndpoint(i);
                switch (endpoint.Direction)
                {
                case UsbAddressing.In when endpoint.Type == UsbAddressing.XferInterrupt:
                    _interruptPipe = endpoint;
                    break;

                case UsbAddressing.In when endpoint.Type == UsbAddressing.XferBulk:
                    _bulkInPipe    = endpoint;
                    _receiveBuffer = new byte[_bulkInPipe.MaxPacketSize];
                    break;

                case UsbAddressing.Out when endpoint.Type == UsbAddressing.XferBulk:
                    _bulkOutPipe = endpoint;
                    break;
                }
            }

            SupportsInterrupt = _interruptPipe != null;
        }
            public override void Open(UsbDeviceConnection connection)
            {
                if (mConnection != null)
                {
                    throw new IOException("Already opened.");
                }

                mConnection = connection;
                bool opened = false;
                bool controlInterfaceFound = false;

                try
                {
                    for (var i = 0; i < mDevice.InterfaceCount; i++)
                    {
                        mControlInterface = mDevice.GetInterface(i);
                        if (mControlInterface.InterfaceClass == UsbClass.Comm)
                        {
                            if (!mConnection.ClaimInterface(mControlInterface, true))
                            {
                                throw new IOException("Could not claim control interface");
                            }
                            (Driver as STM32SerialDriver).mCtrlInterf = i;
                            controlInterfaceFound = true;
                            break;
                        }
                    }
                    if (!controlInterfaceFound)
                    {
                        throw new IOException("Could not claim control interface");
                    }
                    for (var i = 0; i < mDevice.InterfaceCount; i++)
                    {
                        mDataInterface = mDevice.GetInterface(i);
                        if (mDataInterface.InterfaceClass == UsbClass.CdcData)
                        {
                            if (!mConnection.ClaimInterface(mDataInterface, true))
                            {
                                throw new IOException("Could not claim data interface");
                            }
                            mReadEndpoint  = mDataInterface.GetEndpoint(1);
                            mWriteEndpoint = mDataInterface.GetEndpoint(0);
                            opened         = true;
                            break;
                        }
                    }
                    if (!opened)
                    {
                        throw new IOException("Could not claim data interface.");
                    }
                }
                finally
                {
                    if (!opened)
                    {
                        mConnection = null;
                    }
                }
            }
Exemple #10
0
        public override async Task InitializeAsync()
        {
            if (Disposed)
            {
                throw new Exception(DeviceDisposedErrorMessage);
            }

            await GetDeviceAsync(DeviceId);

            if (ConnectedDevice != null)
            {
                var usbInterface = ConnectedDevice.Configuration.UsbInterfaces.FirstOrDefault();

                if (usbInterface == null)
                {
                    ConnectedDevice.Dispose();
                    throw new Exception("There was no Usb Interface found for the device.");
                }

                var interruptPipe = usbInterface.InterruptInPipes.FirstOrDefault();

                if (interruptPipe == null)
                {
                    throw new Exception("There was no interrupt pipe found on the interface");
                }

                interruptPipe.DataReceived += InterruptPipe_DataReceived;

                //TODO: Fill in the DeviceDefinition...

                // TODO: It should be possible to select a different configurations, interface, and pipes

                _DefaultConfigurationInterface = ConnectedDevice.Configuration.UsbInterfaces.FirstOrDefault();

                //TODO: Clean up this messaging and move down to a base class across platforms
                if (_DefaultConfigurationInterface == null)
                {
                    throw new Exception("Could not get the default interface configuration for the USB device");
                }

                _DefaultOutPipe = _DefaultConfigurationInterface.InterruptOutPipes.FirstOrDefault();

                if (_DefaultOutPipe == null)
                {
                    throw new Exception("Could not get the default out pipe for the default USB interface");
                }

                _DefaultInPipe = _DefaultConfigurationInterface.InterruptInPipes.FirstOrDefault();

                if (_DefaultOutPipe == null)
                {
                    throw new Exception("Could not get the default in pipe for the default USB interface");
                }
            }
            else
            {
                throw new Exception($"Could not connect to device with Device Id {DeviceId}. Check that the package manifest has been configured to allow this device.");
            }
        }
        private void ParseForUsbDevices()
        {
            Int32 deviceIndex    = 0;
            Int32 deviceCount    = 0;
            Int32 interfaceIndex = 0;
            Int32 endpointIndex  = 0;

            UsbDevice[]       usbDevices    = null;
            UsbInterface[][]  usbInterfaces = null;
            UsbEndpoint[][][] usbEndpoints  = null;

            // Get the USB devices (normally one in an Android device):
            usbDevices = new UsbDevice[Manager.DeviceList.Count];
            foreach (KeyValuePair <string, UsbDevice> keyValuePair in Manager.DeviceList)
            {
                usbDevices[deviceIndex] = keyValuePair.Value;
                deviceIndex++;
            }
            deviceCount = deviceIndex;

            // Create usbInterface and usbEnpont lists:
            usbInterfaces = new UsbInterface[deviceCount][];
            usbEndpoints  = new UsbEndpoint[deviceCount][][];

            // Loop all (one) devices and look for interfaces and endpoints:
            for (deviceIndex = 0; deviceIndex < deviceCount; deviceIndex++)
            {
                if (usbDevices[deviceIndex].InterfaceCount > 0)
                {
                    if (usbDevices[deviceIndex].ProductName == "INTEGRA-7")
                    {
                        Device = usbDevices[deviceIndex];
                        usbInterfaces[deviceIndex] = new UsbInterface[usbDevices[deviceIndex].InterfaceCount];
                        for (interfaceIndex = 0; interfaceIndex < usbDevices[deviceIndex].InterfaceCount; interfaceIndex++)
                        {
                            usbInterfaces[deviceIndex][interfaceIndex] = usbDevices[deviceIndex].GetInterface(interfaceIndex);
                        }
                        usbEndpoints[deviceIndex] = new UsbEndpoint[usbDevices[deviceIndex].InterfaceCount][];
                        for (interfaceIndex = 0; interfaceIndex < usbDevices[deviceIndex].InterfaceCount; interfaceIndex++)
                        {
                            usbEndpoints[deviceIndex][interfaceIndex] = new UsbEndpoint[usbInterfaces[deviceIndex][interfaceIndex].EndpointCount];
                            Interface = usbInterfaces[deviceIndex][interfaceIndex];
                            for (endpointIndex = 0; endpointIndex < usbInterfaces[deviceIndex][interfaceIndex].EndpointCount; endpointIndex++)
                            {
                                usbEndpoints[deviceIndex][interfaceIndex][endpointIndex] = usbInterfaces[deviceIndex][interfaceIndex].GetEndpoint(endpointIndex);
                                if (usbEndpoints[deviceIndex][interfaceIndex][endpointIndex].Direction == Android.Hardware.Usb.UsbAddressing.Out)
                                {
                                    OutputEndpoint = usbEndpoints[deviceIndex][interfaceIndex][endpointIndex];
                                }
                                else
                                {
                                    InputEndpoint = usbEndpoints[deviceIndex][interfaceIndex][endpointIndex];
                                }
                            }
                        }
                    }
                }
            }
        }
        public override void Open(UsbDeviceConnection connection)
        {
            if (_connection != null)
            {
                throw new UsbSerialException("Already opened.");
            }

            _connection = connection;
            bool opened = false;

            try
            {
                for (int i = 0; i < _driver.Device.InterfaceCount; i++)
                {
                    UsbInterface usbIface = _driver.Device.GetInterface(i);
                    Log.Debug(nameof(Ch34xSerialDriver),
                              _connection.ClaimInterface(usbIface, true)
                            ? $"claimInterface {i} SUCCESS"
                            : $"claimInterface {i} FAIL");
                }

                UsbInterface dataIface = _driver.Device.GetInterface(_driver.Device.InterfaceCount - 1);
                for (int i = 0; i < dataIface.EndpointCount; i++)
                {
                    UsbEndpoint ep = dataIface.GetEndpoint(i);
                    if (ep.Type.IsEqualTo(ExtendedUsbConstants.USB_ENDPOINT_XFER_BULK))
                    {
                        if (ep.Direction.IsEqualTo(ExtendedUsbConstants.USB_DIR_IN))
                        {
                            _readEndpoint = ep;
                        }
                        else
                        {
                            _writeEndpoint = ep;
                        }
                    }
                }

                Initialize();
                BaudRate = DEFAULT_BAUD_RATE;

                opened = true;
            }
            finally
            {
                if (!opened)
                {
                    try
                    {
                        Close();
                    }
                    catch (UsbSerialException)
                    {
                        //Swallowing this exception for now
                    }
                }
            }
        }
        public override void Open(UsbDeviceConnection connection)
        {
            if (_connection != null)
            {
                throw new UsbSerialException("Already opened.");
            }

            _connection = connection;
            bool opened = false;

            try
            {
                for (int i = 0; i < _driver.Device.InterfaceCount; i++)
                {
                    UsbInterface usbIface = _driver.Device.GetInterface(i);
                    Log.Debug(nameof(Cp21xxSerialDriver),
                              _connection.ClaimInterface(usbIface, true)
                            ? $"claimInterface {i} SUCCESS"
                            : $"claimInterface {i} FAIL");
                }

                UsbInterface dataIface = _driver.Device.GetInterface(_driver.Device.InterfaceCount - 1);
                for (int i = 0; i < dataIface.EndpointCount; i++)
                {
                    UsbEndpoint ep = dataIface.GetEndpoint(i);
                    if (ep.Type.IsEqualTo(ExtendedUsbConstants.USB_ENDPOINT_XFER_BULK))
                    {
                        if (ep.Direction.IsEqualTo(ExtendedUsbConstants.USB_DIR_IN))
                        {
                            _readEndpoint = ep;
                        }
                        else
                        {
                            _writeEndpoint = ep;
                        }
                    }
                }

                SetConfigSingle(SILABSER_IFC_ENABLE_REQUEST_CODE, UART_ENABLE);
                SetConfigSingle(SILABSER_SET_MHS_REQUEST_CODE, MCR_ALL | CONTROL_WRITE_DTR | CONTROL_WRITE_RTS);
                SetConfigSingle(SILABSER_SET_BAUDDIV_REQUEST_CODE, BAUD_RATE_GEN_FREQ / DEFAULT_BAUD_RATE);
                opened = true;
            }
            finally
            {
                if (!opened)
                {
                    try
                    {
                        Close();
                    }
                    catch (UsbSerialException)
                    {
                        //Swallowing this exception for now
                    }
                }
            }
        }
        public override void Open()
        {
            bool openedSuccessfully = false;

            try
            {
                CreateConnection();

                for (int i = 0; i < UsbDevice.InterfaceCount; i++)
                {
                    UsbInterface usbIface = UsbDevice.GetInterface(i);
                    if (Connection.ClaimInterface(usbIface, true))
                    {
                        Log.Debug(TAG, "ClaimInterface " + i + " SUCCESS");
                    }
                    else
                    {
                        Log.Debug(TAG, "ClaimInterface " + i + " FAIL");
                    }
                }

                UsbInterface dataIface = UsbDevice.GetInterface(UsbDevice.InterfaceCount - 1);
                for (int i = 0; i < dataIface.EndpointCount; i++)
                {
                    UsbEndpoint ep = dataIface.GetEndpoint(i);
                    if (ep.Type == UsbAddressing.XferBulk)
                    {     // UsbConstants.USB_ENDPOINT_XFER_BULK
                        if (ep.Direction == UsbAddressing.In)
                        { // UsbConstants.USB_DIR_IN
                            ReadEndpoint = ep;
                        }
                        else
                        {
                            WriteEndpoint = ep;
                        }
                    }
                }

                setConfigSingle(SILABSER_IFC_ENABLE_REQUEST_CODE, UART_ENABLE);
                setConfigSingle(SILABSER_SET_MHS_REQUEST_CODE, MCR_ALL | CONTROL_WRITE_DTR | CONTROL_WRITE_RTS);
                setConfigSingle(SILABSER_SET_BAUDDIV_REQUEST_CODE, BAUD_RATE_GEN_FREQ / DEFAULT_BAUD_RATE);
                ResetParameters();
                openedSuccessfully = true;
            }
            finally
            {
                if (openedSuccessfully)
                {
                    IsOpened = true;
                    StartUpdating();
                }
                else
                {
                    CloseConnection();
                }
            }
        }
        internal virtual void OpenSingleInterface()
        {
            _controlInterface = _driver.Device.GetInterface(0);
            Log.Debug(nameof(CdcAcmSerialDriver), "Control iface=" + _controlInterface);

            _dataInterface = _driver.Device.GetInterface(0);
            Log.Debug(nameof(CdcAcmSerialDriver), "data iface=" + _dataInterface);

            if (!_connection.ClaimInterface(_controlInterface, true))
            {
                throw new UsbSerialException("Could not claim shared control/data interface.");
            }

            int endCount = _controlInterface.EndpointCount;

            if (endCount < 3)
            {
                Log.Debug(nameof(CdcAcmSerialDriver), "not enough endpoints - need 3. count=" + _controlInterface.EndpointCount);
                throw new UsbSerialException("Insufficient number of endpoints(" + _controlInterface.EndpointCount + ")");
            }

            _controlEndpoint = null;
            _readEndpoint    = null;
            _writeEndpoint   = null;
            for (int i = 0; i < endCount; ++i)
            {
                UsbEndpoint ep = _controlInterface.GetEndpoint(i);
                if ((ep.Direction.IsEqualTo(ExtendedUsbConstants.USB_DIR_IN)) && (ep.Type.IsEqualTo(ExtendedUsbConstants.USB_ENDPOINT_XFER_INT)))
                {
                    Log.Debug(nameof(CdcAcmSerialDriver), "Found controlling endpoint");
                    _controlEndpoint = ep;
                }
                else if ((ep.Direction.IsEqualTo(ExtendedUsbConstants.USB_DIR_IN)) && (ep.Type.IsEqualTo(ExtendedUsbConstants.USB_ENDPOINT_XFER_BULK)))
                {
                    Log.Debug(nameof(CdcAcmSerialDriver), "Found reading endpoint");
                    _readEndpoint = ep;
                }
                else if ((ep.Direction.IsEqualTo(ExtendedUsbConstants.USB_DIR_OUT)) && (ep.Type.IsEqualTo(ExtendedUsbConstants.USB_ENDPOINT_XFER_BULK)))
                {
                    Log.Debug(nameof(CdcAcmSerialDriver), "Found writing endpoint");
                    _writeEndpoint = ep;
                }


                if ((_controlEndpoint != null) && (_readEndpoint != null) && (_writeEndpoint != null))
                {
                    Log.Debug(nameof(CdcAcmSerialDriver), "Found all required endpoints");
                    break;
                }
            }

            if ((_controlEndpoint == null) || (_readEndpoint == null) || (_writeEndpoint == null))
            {
                Log.Debug(nameof(CdcAcmSerialDriver), "Could not establish all endpoints");
                throw new UsbSerialException("Could not establish all endpoints");
            }
        }
Exemple #16
0
        /// <summary>
        /// Dispose is called when the program is shutting
        /// down so that LEGO brick can be cleaned up.
        /// </summary>
        public void Dispose()
        {
            //Clean up the brick
            this.CleanupNXTBrick();

            //Clean up the UsbInterface
            this._device.Dispose();
            this._device = null;
        }
 public UsbDataBinder(UsbManager manager, UsbDevice device)
 {
     // TODO Auto-generated constructor stub
     mUsbManager = manager;
     mDevice     = device;
     mIntf       = mDevice.GetInterface(0);
     mEndpoint   = mIntf.GetEndpoint(0);
     mConnection = mUsbManager.OpenDevice(mDevice);
 }
        public PL2303SerialDevice(UsbDevice device, UsbDeviceConnection connection, int iface) : base(device, connection)
        {
            hexData = new HexData();
            if (iface > 1)
            {
                throw new ArgumentException("Multi-interface PL2303 devices not supported!");
            }

            mInterface = device.GetInterface(iface >= 0 ? iface : 0);
        }
Exemple #19
0
 public FTDISerialDevice(UsbDevice device, UsbDeviceConnection connection, int iface) : base(device, connection)
 {
     ftdiUtilities = new FTDIUtilities(this);
     rtsCtsEnabled = false;
     dtrDsrEnabled = false;
     ctsState      = true;
     dsrState      = true;
     firstTime     = true;
     mInterface    = device.GetInterface(iface >= 0 ? iface : 0);
 }
 public void Close()
 {
     connection   = null;
     fd           = -1;
     usbdev       = null;
     usbIface     = null;
     ep_in        = null;
     ep_out       = null;
     max_pkt_size = 0;
 }
Exemple #21
0
            private void openInterface()
            {
                Log.Debug(TAG, "claiming interfaces, count=" + mDevice.InterfaceCount);

                mControlInterface = mDevice.GetInterface(0);
                Log.Debug(TAG, "Control iface=" + mControlInterface);
                // class should be USB_CLASS_COMM

                if (!mConnection.ClaimInterface(mControlInterface, true))
                {
                    throw new IOException("Could not claim control interface.");
                }


                mControlEndpoint = mControlInterface.GetEndpoint(0);
                Log.Debug(TAG, "Control endpoint direction: " + mControlEndpoint.Direction);

                Log.Debug(TAG, "Claiming data interface.");
                mDataInterface = mDevice.GetInterface(1);
                Log.Debug(TAG, "data iface=" + mDataInterface);
                // class should be USB_CLASS_CDC_DATA

                if (!mConnection.ClaimInterface(mDataInterface, true))
                {
                    throw new IOException("Could not claim data interface.");
                }

                for (int i = 0; i < mDataInterface.EndpointCount; ++i)
                {
                    UsbEndpoint ep = mDataInterface.GetEndpoint(i);
                    if ((ep.Direction == UsbAddressing.In) &&
                        (ep.Type == UsbAddressing.XferBulk))
                    {
                        Log.Debug(TAG, "Found reading endpoint");
                        mReadEndpoint = ep;
                    }
                    else if ((ep.Direction == UsbAddressing.Out) &&
                             (ep.Type == UsbAddressing.XferBulk))
                    {
                        Log.Debug(TAG, "Found writing endpoint");
                        mWriteEndpoint = ep;
                    }
                }


                //mReadEndpoint = mDataInterface.GetEndpoint(1);
                //Log.Debug(TAG, "Read endpoint direction: " + mReadEndpoint.Direction);
                //mWriteEndpoint = mDataInterface.GetEndpoint(0);
                //Log.Debug(TAG, "Write endpoint direction: " + mWriteEndpoint.Direction);

                //mReadEndpoint = mDataInterface.GetEndpoint(0);
                //Log.Debug(TAG, "Read endpoint direction: " + mReadEndpoint.Direction);
                //mWriteEndpoint = mDataInterface.GetEndpoint(1);
                //Log.Debug(TAG, "Write endpoint direction: " + mWriteEndpoint.Direction);
            }
 private void getEndpoint(UsbDeviceConnection connection, UsbInterface intf)
 {
     if (intf.GetEndpoint(1) != null)
     {
         epOut = intf.GetEndpoint(1);
     }
     if (intf.GetEndpoint(0) != null)
     {
         epIn = intf.GetEndpoint(0);
     }
 }
        private UsbEndpoint GetEndpoint(UsbInterface interf, UsbAddressing direction)
        {
            for (var i = 0; i < interf.EndpointCount; i++)
            {
                var endpoint = interf.GetEndpoint(i);
                if (endpoint.Direction.HasFlag(direction))
                {
                    return(endpoint);
                }
            }

            return(null);
        }
Exemple #24
0
        public static bool IsCdcDevice(UsbDevice device)
        {
            int iIndex = device.InterfaceCount;

            for (int i = 0; i <= iIndex - 1; i++)
            {
                UsbInterface iface = device.GetInterface(i);
                if (iface.InterfaceClass == UsbClass.CdcData)
                {
                    return(true);
                }
            }
            return(false);
        }
 /**
  * 找设备接口
  */
 private void findInterface()
 {
     if (myUsbDevice != null)
     {
         /*
          * //Log.d(TAG, "interfaceCounts : " + myUsbDevice.InterfaceCount);
          * for (int i = 0; i < myUsbDevice.InterfaceCount; i++)
          * {
          *
          *  break;
          * }
          */
         UsbInterface intf = myUsbDevice.GetInterface(1);
         myInterface = intf;
     }
 }
        public void Stop()
        {
            _cts.Cancel();
            _cts = null;
            _handler.SendMessage(_handler.ObtainMessage((int)USBDeviceStatus.DeviceConnectionClosed));
            _usbReadEndpoint  = null;
            _usbWriteEndpoint = null;
            _usbConnection.Close();
            _usbInterface  = null;
            _usbConnection = null;
            _thread        = null;

            //Device.DataReceived -= Device_DataReceived;
            //Device.Dispose();
            //Device = null;
        }
        public async Task <bool> OpenAsync()
        {
            if (connected)
            {
                return(false);
            }

            //usbManager.RequestPermission(usbDevice, PendingIntent.GetBroadcast(ConnectionService.Instance.Context, 0, new Intent(ConnectionService.Instance.ActionUsbPermission), PendingIntentFlags.CancelCurrent));

            UsbInterface intf = usbDevice.GetInterface(0);

            pinReportEndpoint          = intf.GetEndpoint(0);
            peripheralResponseEndpoint = intf.GetEndpoint(1);
            pinConfigEndpoint          = intf.GetEndpoint(2);
            peripheralConfigEndpoint   = intf.GetEndpoint(3);
            connection = usbManager.OpenDevice(usbDevice);
            if (connection != null)
            {
                bool intfClaimed = connection.ClaimInterface(intf, true);
                if (intfClaimed)
                {
                    connected = true;

                    pinListenerThread = new Thread(
                        () =>
                    {
                        byte[] data = new byte[64];
                        pinListenerThreadRunning = true;
                        while (pinListenerThreadRunning)
                        {
                            int res =
                                connection.BulkTransfer(pinReportEndpoint, data, 41,
                                                        100); // pin reports are 41 bytes long now
                            if (res > 0)
                            {
                                this.PinEventDataReceived?.Invoke(data);
                            }
                        }
                    });

                    pinListenerThread.Start();
                    return(true);
                }
            }
            return(false);
        }
Exemple #28
0
        /// <summary>
        ///
        /// </summary>
        public virtual void OpenDevice(UsbDevice device)
        {
            //
            if ((m_UsbIntf = FindInterface(device)) == null)
            {
                Log.e("UsbStream", "Can't find the UsbInterface" + device.ToString() + ".");
                OnOpenFailure();
                return;
            }
            //
            m_UsbConnection = m_UsbMgr.OpenDevice(m_UsbDevice = device);
            m_UsbConnection.ClaimInterface(m_UsbIntf, true);
            //
            m_UsbReqIn = new UsbRequest();
            m_UsbReqIn.Initialize(m_UsbConnection, m_UsbIntf.intIn);

            // Check read buffer size.
            if (m_SizeRead == -1)
            {
                Log.i("UsbStream", "Use default read buffer size:" + (m_SizeRead = 64).ToString());
            }

#if USB_HANDLER_IN_UNITY
            //
            m_UsbBufferIn = ByteBuffer.Allocate(m_SizeRead);
            //
            m_ReadThread = new NativeThread(ReadThread_Step); //Looper
            m_ReadThread.StartLoop();                         //
#elif USB_HANDLER_IN_ANDROID
            AndroidJavaObject handler = new AndroidJavaObject
                                            ("android.unity.SafeUsbHandler", m_UsbConnection.m_SealedPtr, m_UsbReqIn.m_SealedPtr, m_SizeRead);
            handler.Call("setCallback", new CallbackProxy(this));
            m_ReadThread = new NativeThread(handler);
            m_ReadThread.Start2();
#endif
            //
            UsbManager.main.onUsbDeviceDetached += OnUsbDeviceDetached;
            if (m_OpenCallback != null)
            {
                m_OpenCallback.OnStreamOpenSuccess(this);
            }
            //
            m_IsOpen = true;
        }
        /**
         * 找设备接口
         */
        private void findInterface()
        {
            if (myUsbDevice != null)
            {
                for (int i = 0; i < myUsbDevice.InterfaceCount; i++)
                {
                    UsbInterface intf = myUsbDevice.GetInterface(i);
                    myInterface = intf;
                    //openDevice();
                    //assignEndpoint();

                    if (intf.EndpointCount >= 2)
                    {
                        myInterface = intf;
                        break;
                    }
                }
            }
        }
        public void Start()
        {
            _usbConnection = _usbManager.OpenDevice(_usbDevice);
            if (_usbConnection == null)
            {
                throw new NullReferenceException("connection");
            }

            _usbInterface = _usbDevice.GetInterface(1);

            if (!_usbConnection.ClaimInterface(_usbInterface, true))
            {
                throw new Exception("claim interface");
            }

            _usbWriteEndpoint = _usbInterface.GetEndpoint(0);
            _usbReadEndpoint  = _usbInterface.GetEndpoint(1);

            ScanNetworks();
        }
 public ConfigurationDescriptor(ushort MaxPower_mA, UsbInterface[] Interfaces)
     : base(0)
 {
     bMaxPower = (byte)(MaxPower_mA / PowerFactor);
     interfaces = Interfaces;
     iConfiguration = 0;             // Default to no Configuration string
     bmAttributes = ATTRIB_Base;   // Default to no attributes
 }