예제 #1
0
        public void Start()
        {
            var manager = (UsbManager)Application.Context.GetSystemService(Context.UsbService);

            _connection = manager.OpenDevice(_device);
            var claimed = _connection.ClaimInterface(_interface, true);

            if (!claimed)
            {
                throw new Exception("Interface could not be claimed");
            }

            var rawDescriptors = _connection.GetRawDescriptors();

            Descriptor = new CcidDescriptor(rawDescriptors);

            if (SupportsInterrupt)
            {
                _interruptListenerThread = new Thread(StartListeningOnInterruptEndpoint);
                _interruptListenerThread.Start();
            }
        }
            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);
                    }
                }
            }
예제 #3
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);
                }
            }
        }