/// <summary>
        /// try to retreive an endpoint with the device
        /// Is seems that  usb-serial-for-android does this job
        /// </summary>
        /// <param name="connection"> The created connection</param>
        /// <param name="epIn"> Endpoint found for the interface for device to host transmission </param>
        /// <param name="epOut"> Endpoint found for the interface for host to device transmission </param>
        /// <returns> operation result </returns>
        protected int OpenInterface(UsbDevice usbdev, UsbDeviceConnection connection, ref UsbInterface usbIface, ref UsbEndpoint epIn, ref UsbEndpoint epOut)
        {
            int ret = -1;
            int i;

            // Search and claim the first interface available
            for (i = 0; i < usbdev.InterfaceCount; i++)
            {
                usbIface = usbdev.GetInterface(i);
                if (connection.ClaimInterface(usbIface, true))
                {
                    // Now search IO endpoints
                    if (InterfaceGetBulkEndpoints(usbIface, ref epIn, ref epOut) == 0)
                    {
                        connection.SetInterface(usbIface);
                        return(0);
                    }
                    else
                    {
                        connection.ReleaseInterface(usbIface);
                    }
                }
            }
            return(ret);
        }
 public void onDestroy()
 {
     if (mConnection != null)
     {
         mConnection.ReleaseInterface(mIntf);
         mConnection.Close();
     }
 }
Beispiel #3
0
        /// <summary>
        ///
        /// </summary>
        public override void Close()
        {
            // Forcibly close the read thread.
            if (m_ReadThread != null)
            {
                m_ReadThread.Abort(); m_ReadThread = null;
            }
            if (m_IsOpen)
            {
                //
                m_UsbConnection.ReleaseInterface(m_UsbIntf);
                m_UsbConnection.Close();

                // Clean up
                UsbManager.main.onUsbDeviceAttached -= OnUsbDeviceAttached;
                UsbManager.main.onUsbDeviceDetached -= OnUsbDeviceDetached;
                AndroidPtr.Free(ref m_UsbDevice);
                AndroidPtr.Free(ref m_UsbIntf);
                AndroidPtr.Free(ref m_UsbConnection);
                AndroidPtr.Free(ref m_UsbReqIn);
                //
                m_IsOpen = false;
            }
        }
            public override void Open(UsbDeviceConnection connection)
            {
                if (mConnection != null)
                {
                    throw new IOException("Already open");
                }

                UsbInterface usbInterface = mDevice.GetInterface(0);

                if (!connection.ClaimInterface(usbInterface, true))
                {
                    throw new IOException("Error claiming Prolific interface 0");
                }
                mConnection = connection;
                Boolean opened = false;

                try
                {
                    for (int i = 0; i < usbInterface.EndpointCount; ++i)
                    {
                        UsbEndpoint currentEndpoint = usbInterface.GetEndpoint(i);

                        switch (currentEndpoint.Address)
                        {
                        case (UsbAddressing)READ_ENDPOINT:
                            mReadEndpoint = currentEndpoint;
                            break;

                        case (UsbAddressing)WRITE_ENDPOINT:
                            mWriteEndpoint = currentEndpoint;
                            break;

                        case (UsbAddressing)INTERRUPT_ENDPOINT:
                            mInterruptEndpoint = currentEndpoint;
                            break;
                        }
                    }

                    if (mDevice.DeviceClass == (UsbClass)0x02)
                    {
                        mDeviceType = DEVICE_TYPE_0;
                    }
                    else
                    {
                        try
                        {
                            //Method getRawDescriptorsMethod
                            //    = mConnection.getClass().getMethod("getRawDescriptors");
                            //byte[] rawDescriptors
                            //    = (byte[])getRawDescriptorsMethod.invoke(mConnection);

                            byte[] rawDescriptors = mConnection.GetRawDescriptors();

                            byte maxPacketSize0 = rawDescriptors[7];
                            if (maxPacketSize0 == 64)
                            {
                                mDeviceType = DEVICE_TYPE_HX;
                            }
                            else if ((mDevice.DeviceClass == 0x00) ||
                                     (mDevice.DeviceClass == (UsbClass)0xff))
                            {
                                mDeviceType = DEVICE_TYPE_1;
                            }
                            else
                            {
                                Log.Warn(TAG, "Could not detect PL2303 subtype, "
                                         + "Assuming that it is a HX device");
                                mDeviceType = DEVICE_TYPE_HX;
                            }
                        }
                        catch (NoSuchMethodException e)
                        {
                            Log.Warn(TAG, "Method UsbDeviceConnection.getRawDescriptors, "
                                     + "required for PL2303 subtype detection, not "
                                     + "available! Assuming that it is a HX device");
                            mDeviceType = DEVICE_TYPE_HX;
                        }
                        catch (Exception e)
                        {
                            Log.Error(TAG, "An unexpected exception occured while trying "
                                      + "to detect PL2303 subtype", e);
                        }
                    }

                    SetControlLines(mControlLinesValue);
                    ResetDevice();

                    DoBlackMagic();
                    opened = true;
                }
                finally
                {
                    if (!opened)
                    {
                        mConnection = null;
                        connection.ReleaseInterface(usbInterface);
                    }
                }
            }
Beispiel #5
0
 public void Dispose()
 {
     _interruptListenerThread?.Abort();
     _connection?.ReleaseInterface(_interface);
     _connection?.Close();
 }
Beispiel #6
0
        public override void Open(UsbDeviceConnection connection)
        {
            if (_connection != null)
            {
                throw new UsbSerialException("Already open");
            }

            UsbInterface usbInterface = _driver.Device.GetInterface(0);

            if (!connection.ClaimInterface(usbInterface, true))
            {
                throw new UsbSerialException("Error claiming Prolific interface 0");
            }

            _connection = connection;
            bool opened = false;

            try
            {
                for (int i = 0; i < usbInterface.EndpointCount; ++i)
                {
                    UsbEndpoint currentEndpoint = usbInterface.GetEndpoint(i);

                    if (currentEndpoint.Address.IsEqualTo(READ_ENDPOINT))
                    {
                        _readEndpoint = currentEndpoint;
                    }
                    else if (currentEndpoint.Address.IsEqualTo(WRITE_ENDPOINT))
                    {
                        _writeEndpoint = currentEndpoint;
                    }
                    else if (currentEndpoint.Address.IsEqualTo(INTERRUPT_ENDPOINT))
                    {
                        _interruptEndpoint = currentEndpoint;
                    }
                }

                if ((int)_driver.Device.DeviceClass == 0x02)
                {
                    _deviceType = DEVICE_TYPE_0;
                }
                else
                {
                    try
                    {
                        byte[] rawDescriptors = _connection.GetRawDescriptors();
                        byte   maxPacketSize0 = rawDescriptors[7];
                        if (maxPacketSize0 == 64)
                        {
                            _deviceType = DEVICE_TYPE_HX;
                        }
                        else if ((_driver.Device.DeviceClass == 0x00) || ((int)_driver.Device.DeviceClass == 0xff))
                        {
                            _deviceType = DEVICE_TYPE_1;
                        }
                        else
                        {
                            Log.Warn(nameof(ProlificSerialDriver), "Could not detect PL2303 subtype, " + "Assuming that it is a HX device");
                            _deviceType = DEVICE_TYPE_HX;
                        }
                    }
                    catch (NoSuchMethodException)
                    {
                        Log.Warn(nameof(ProlificSerialDriver), "Method UsbDeviceConnection.getRawDescriptors, " + "required for PL2303 subtype detection, not " + "available! Assuming that it is a HX device");
                        _deviceType = DEVICE_TYPE_HX;
                    }
                    catch (Exception e)
                    {
                        Log.Warn(nameof(ProlificSerialDriver), "An unexpected exception occured while trying " + "to detect PL2303 subtype", e);
                    }
                }

                ControlLines = _controlLinesValue;
                ResetDevice();

                PerformInitializationSequence();
                opened = true;
            }
            finally
            {
                if (!opened)
                {
                    _connection = null;
                    connection.ReleaseInterface(usbInterface);
                }
            }
        }
        public override void Open(UsbDeviceConnection connection)
        {
            if (Connection != null)
            {
                throw new IOException("Already open");
            }

            Connection = connection;

            UsbInterface usbInterface = Device.GetInterface(0);

            if (!connection.ClaimInterface(usbInterface, true))
            {
                throw new IOException("Error claiming Prolific interface 0");
            }

            var opened = false;

            try
            {
                for (int i = 0; i < usbInterface.EndpointCount; ++i)
                {
                    UsbEndpoint currentEndpoint = usbInterface.GetEndpoint(i);

                    switch (currentEndpoint.Address)
                    {
                    case READ_ENDPOINT:
                        mReadEndpoint = currentEndpoint;
                        break;

                    case WRITE_ENDPOINT:
                        mWriteEndpoint = currentEndpoint;
                        break;

                    case INTERRUPT_ENDPOINT:
                        mInterruptEndpoint = currentEndpoint;
                        break;
                    }
                }

                if (Device.DeviceClass == (UsbClass)0x02)
                {
                    mDeviceType = DEVICE_TYPE_0;
                }
                else
                {
                    if ((Device.DeviceClass == 0x00) ||
                        (Device.DeviceClass == (UsbClass)0xff))
                    {
                        mDeviceType = DEVICE_TYPE_1;
                    }
                    else
                    {
                        mDeviceType = DEVICE_TYPE_HX;
                    }
                }

                SetControlLines(mControlLinesValue);

                DoBlackMagic();
                ResetDevice();
                opened = true;
            }
            finally
            {
                if (!opened)
                {
                    Connection = null;
                    connection.ReleaseInterface(usbInterface);
                }
            }
        }