public bool UpdateDeviceConnectionStatus()
        {
            var hwDeviceStatus = hw.CheckConnectionStatus();

            if (hwDeviceStatus == UsbDeviceConnectionStatus.NotConnected)
            {
                connectedDevice = null;
                return(true);
            }
            else if (hwDeviceStatus == UsbDeviceConnectionStatus.Changed)
            {
                InitializeDevice();
                return(true);
            }
            else if (hwDeviceStatus == UsbDeviceConnectionStatus.Error)
            {
                throw new UsbTransferException("Error on USB bus reset", UsbPacketResult.OtherError);
            }

            return(false);
        }
        private UsbPacketResult InitializeDevice()
        {
            byte[]            data = new byte[255];
            UsbTransferResult result;
            UsbSetupPacket    getDescriptorSetupPacket = new UsbSetupPacket(UsbStandardRequest.GET_DESCRIPTOR, 0x80);
            byte endpointZeroMaxPacketSize             = 8;

            //InitializeHub();

            //Here device is in DEFAULT state (hw did bus reset already):

            result = GetDescriptor(0, UsbDescriptorType.DEVICE, 0, 0, endpointZeroMaxPacketSize, out data);
            if (result.IsError)
            {
                throw new UsbTransferException($"When getting device descriptor: {result.TransactionResult}", result.TransactionResult);
            }

            endpointZeroMaxPacketSize = data[7];

            var deviceDescriptorBytes = data.Take(result.TransferredDataCount).ToArray();

            var setAddressSetupPacket = new UsbSetupPacket(UsbStandardRequest.SET_ADDRESS, 0);

            setAddressSetupPacket.wValue = UsbDeviceAddress;
            result = hw.ExecuteControlTransfer(setAddressSetupPacket, null, 0, 0, endpointZeroMaxPacketSize);
            if (result.IsError)
            {
                throw new UsbTransferException($"When setting device address: {result.TransactionResult}", result.TransactionResult);
            }

            //Here device is in ADDRESS state

            getDescriptorSetupPacket.wValueH = UsbDescriptorType.CONFIGURATION;
            getDescriptorSetupPacket.wValueL = 0; //We're interested in the first pconfiguration available
            getDescriptorSetupPacket.wLength = (short)data.Length;

            result = GetDescriptor(UsbDeviceAddress, UsbDescriptorType.CONFIGURATION, 0, 0, endpointZeroMaxPacketSize, out data);
            if (result.IsError)
            {
                throw new UsbTransferException($"When getting configuration descriptor: {result.TransactionResult}", result.TransactionResult);
            }

            var bConfigurationValue           = data[5];
            var configurationDescriptorLength = result.TransferredDataCount;

            var setConfigSetupPacket = new UsbSetupPacket(UsbStandardRequest.SET_CONFIGURATION, 0);

            setConfigSetupPacket.wValueL = bConfigurationValue;
            result = hw.ExecuteControlTransfer(setConfigSetupPacket, null, 0, UsbDeviceAddress, endpointZeroMaxPacketSize);
            if (result.IsError)
            {
                throw new UsbTransferException($"When setting device configuration: {result.TransactionResult}", result.TransactionResult);
            }

            //Here device is in CONFIGURED state

            var interfacesInfo = GetInterfacesInfo(data.Take(configurationDescriptorLength).ToArray());

            connectedDevice = new UsbConnectedDevice(
                endpointZeroMaxPacketSize,
                deviceDescriptorBytes[4],
                deviceDescriptorBytes[5],
                deviceDescriptorBytes[6],
                (int)(deviceDescriptorBytes[8] | (deviceDescriptorBytes[9] << 8)),
                (int)(deviceDescriptorBytes[10] | (deviceDescriptorBytes[11] << 8)),
                deviceDescriptorBytes[14],
                deviceDescriptorBytes[15],
                interfacesInfo);

            return(UsbPacketResult.Ok);
        }