示例#1
0
        public async Task <bool> GetDevice(UsbContext context)
        {
            Console.WriteLine("Looking for device ...");
            // Dump all devices and descriptor information to console output.
            foreach (var usbDevice in context.List())
            {
                if (usbDevice.TryOpen())
                {
                    if (usbDevice.Info.Manufacturer == mfrName)
                    {
                        device = usbDevice;
                        if (usbDevice.ClaimInterface(interfaceID))
                        {
                            monitorBuddyDevice = usbDevice;

                            // Console.WriteLine($"Device open, interface 0 claimed: {device.ToString()}");
                            endpointReader = monitorBuddyDevice.OpenEndpointReader(ReadEndpointID.Ep01, endpointBufferSize, EndpointType.Interrupt);

                            return(true);
                        }
                    }
                }
            }

            await Task.Delay(2000);

            return(false);
        }
示例#2
0
        protected override async Task <TrezorManager> ConnectAsync()
        {
            var _UsbContext = new UsbContext();

            var trezorUsbDevice = _UsbContext.List().FirstOrDefault
                                  (
                d =>
                //Trezor One - 1.6.x
                (d.ProductId == 0x1 && d.VendorId == 0x534C) ||
                //Trezor One - 1.7.x
                (d.ProductId == 0x53C1 && d.VendorId == 0x1209) ||
                //Trezor Model T ?
                (d.ProductId == 0x53C0 && d.VendorId == 0x1209)
                                  );

            var _LibUsbDevice = new LibUsbDevice(trezorUsbDevice, 60000);
            await _LibUsbDevice.InitializeAsync();

            Console.WriteLine("Connected");

            var trezorManager = new TrezorManager(GetPin, GetPassphrase, _LibUsbDevice);
            await trezorManager.InitializeAsync();

            return(trezorManager);
        }
示例#3
0
        /// <inheritdoc />
        public IEnumerable <string> Discover()
        {
            lock (_discoveryLock)
            {
                UsbDeviceCollection?usbDeviceCollection = _context.List();
                _context.SetDebugLevel(LibUsbDotNet.LogLevel.Debug);

                // Filter out all but Concept2 Vendor
                var discoveredDevices = usbDeviceCollection.Where(d => d.VendorId == VendorId);

                // Discover disconnected device based on location
                foreach (UsbDevice detachedDevice in _devices.Values.Except(discoveredDevices, new IUsbDeviceComparer()))
                {
                    Location location = new (detachedDevice.BusNumber, detachedDevice.Address);

                    // If events are enabled, fire one for the device being lost
                    EventArgs args = new DeviceEventArgs(location);

                    DeviceLost?.Invoke(this, args);

                    // Clean up device
                    Destroy(detachedDevice);

                    // Remove from devices
                    _devices.Remove(location, out _);
                }

                // Discover new devices using Location as the comparer
                foreach (UsbDevice foundDevice in discoveredDevices.Except(_devices.Values, new IUsbDeviceComparer()))
                {
                    if (!IsValidDevice(foundDevice))
                    {
                        _logger.LogWarning("Invalid device encountered. Not being added to the device list.");
                    }

                    Location location = new (foundDevice.BusNumber, foundDevice.Address);

                    if (!foundDevice.IsOpen)
                    {
                        Initialize(foundDevice);
                    }

                    string serialNumber = foundDevice.Info.SerialNumber;

                    // If events are enabled, fire one for the device being found
                    EventArgs args = new DeviceEventArgs(location)
                    {
                        SerialNumber = serialNumber
                    };

                    DeviceFound?.Invoke(this, args);

                    // Add to devices
                    _devices.TryAdd(location, serialNumber, foundDevice);
                }

                return(_devices.SerialNumbers);
            }
        }
示例#4
0
        public static List <UsbDeviceId> All()
        {
            var result = new List <UsbDeviceId>();

            using (var context = new UsbContext())
            {
                foreach (var d in context.List())
                {
                    result.Add(new UsbDeviceId()
                    {
                        ProductId = d.ProductId, VendorId = d.VendorId
                    });
                }
            }

            return(result);
        }
示例#5
0
        public UsbDriver(int vid, int pid)
        {
            using UsbContext context = new UsbContext();
            var usbDeviceCollection = context.List();

            Driver = usbDeviceCollection.FirstOrDefault(d => d.ProductId == pid && d.VendorId == vid);

            if (Driver != null)
            {
                DeviceInfo = new UsbDriverInformation
                {
                    PID   = pid,
                    VID   = vid,
                    Ready = Driver.IsOpen
                };
            }
        }
示例#6
0
        public Switch()
        {
            Console.WriteLine("Connecting to Switch");
            this.LibUsbContext = new UsbContext();
            var usbDeviceCollection = LibUsbContext.List();

            NX = usbDeviceCollection.FirstOrDefault(d => d.ProductId == PID && d.VendorId == VID);

            if (NX == null)
            {
                throw new Exception("Unable to find Switch. Ensure Switch is connected and USB Install is open.");
            }

            NX.Open();
            NX.ClaimInterface(NX.Configs[0].Interfaces[0].Number);

            Writer = NX.OpenEndpointWriter(WriteEndpointID.Ep01);
            Reader = NX.OpenEndpointReader(ReadEndpointID.Ep01);
        }
示例#7
0
        public static void Main(string[] args)
        {
            using (var context = new UsbContext())
            {
                context.SetDebugLevel(LogLevel.Info);

                //Get a list of all connected devices
                var usbDeviceCollection = context.List();

                //Narrow down the device by vendor and pid
                var selectedDevice = usbDeviceCollection.FirstOrDefault(d => d.ProductId == ProductId && d.VendorId == VendorId);

                if (selectedDevice != null)
                {
                    Console.WriteLine("Found USB device in DFU mode...");

                    //Open the device
                    selectedDevice.TryOpen();

                    //Get the first config number of the interface
                    selectedDevice.ClaimInterface(selectedDevice.Configs[0].Interfaces[0].Number);

                    Console.WriteLine(GetDeviceInfo(selectedDevice));

                    dfu.SetUsb(selectedDevice);

                    //do DFU stuff!!!
                    try
                    {
                        dfu.ProgramFirmware(@"F:\nanobooter-nanoclr.dfu");
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine($"Failed!!! - {e}");
                    }
                }
                else
                {
                    Console.WriteLine("No USB device found!");
                }
            }
        }
示例#8
0
        static UsbDevice GetDevice(LibUsbDotNet.LogLevel level)
        {
            if (LibUsbCtx != null)
            {
                throw new Exception("Libusb has already been initialized");
            }

            LibUsbCtx = new UsbContext();
            LibUsbCtx.SetDebugLevel(level);
            var usbDeviceCollection = LibUsbCtx.List();
            var selectedDevice      = usbDeviceCollection.FirstOrDefault(d => d.ProductId == 0x3006 && d.VendorId == 0x057e);

            if (selectedDevice == null)
            {
                return(null);
            }

            selectedDevice.Open();

            return(new UsbDevice(selectedDevice));
        }
示例#9
0
        public static void Main(string[] args)
        {
            // Dump all devices and descriptor information to console output.
            using (UsbContext context = new UsbContext())
            {
                var allDevices = context.List();
                foreach (var usbRegistry in allDevices)
                {
                    Console.WriteLine(usbRegistry.Info.ToString());

                    if (usbRegistry.TryOpen())
                    {
                        for (int iConfig = 0; iConfig < usbRegistry.Configs.Count; iConfig++)
                        {
                            UsbConfigInfo configInfo = usbRegistry.Configs[iConfig];
                            Console.WriteLine(configInfo.ToString());

                            ReadOnlyCollection <UsbInterfaceInfo> interfaceList = configInfo.Interfaces;
                            for (int iInterface = 0; iInterface < interfaceList.Count; iInterface++)
                            {
                                UsbInterfaceInfo interfaceInfo = interfaceList[iInterface];
                                Console.WriteLine(interfaceInfo.ToString());

                                ReadOnlyCollection <UsbEndpointInfo> endpointList = interfaceInfo.Endpoints;
                                for (int iEndpoint = 0; iEndpoint < endpointList.Count; iEndpoint++)
                                {
                                    Console.WriteLine(endpointList[iEndpoint].ToString());
                                }
                            }
                        }

                        usbRegistry.Close();
                    }
                }
            }

            // Wait for user input..
            Console.WriteLine("Press any key to exit");
            Console.ReadKey();
        }
示例#10
0
        private static IUsbDevice GetSysDVRDevice()
        {
            if (device != null)
            {
                return(device);
            }

            LibUsbCtx = new UsbContext();
            LibUsbCtx.SetDebugLevel(LogLevel);
            var usbDeviceCollection = LibUsbCtx.List();
            var dev = usbDeviceCollection.FirstOrDefault(d => d.ProductId == 0x3006 && d.VendorId == 0x057e);

            if (dev == null)
            {
                throw new Exception("Device not found");
            }

            dev.Open();

            device = dev;
            return(dev);
        }
        protected override async Task <IDevice> Connect()
        {
            _UsbContext = new UsbContext();

            var trezorUsbDevice = _UsbContext.List().FirstOrDefault
                                  (
                d =>
                //Trezor One - 1.6.x
                (d.ProductId == 0x1 && d.VendorId == 0x534C) ||
                //Trezor One - 1.7.x
                (d.ProductId == 0x53C1 && d.VendorId == 0x1209) ||
                //Trezor Model T ?
                (d.ProductId == 0x53C0 && d.VendorId == 0x1209)
                                  );

            _LibUsbDevice = new LibUsbDevice(trezorUsbDevice, 60000);
            await _LibUsbDevice.InitializeAsync();

            Console.WriteLine("Connected");

            return(_LibUsbDevice);
        }
示例#12
0
        public static void Main(string[] args)
        {
            using (var context = new UsbContext())
            {
                context.SetDebugLevel(LogLevel.Info);

                //Get a list of all connected devices
                var usbDeviceCollection = context.List();

                //Narrow down the device by vendor and pid
                var selectedDevice = usbDeviceCollection.FirstOrDefault(d => d.ProductId == ProductId && d.VendorId == VendorId);

                //Open the device
                selectedDevice.Open();

                //Get the first config number of the interface
                selectedDevice.ClaimInterface(selectedDevice.Configs[0].Interfaces[0].Number);

                //Open up the endpoints
                var writeEndpoint = selectedDevice.OpenEndpointWriter(WriteEndpointID.Ep01);
                var readEnpoint   = selectedDevice.OpenEndpointReader(ReadEndpointID.Ep01);

                //Create a buffer with some data in it
                var buffer = new byte[64];
                buffer[0] = 0x3f;
                buffer[1] = 0x23;
                buffer[2] = 0x23;

                //Write three bytes
                writeEndpoint.Write(buffer, 3000, out var bytesWritten);

                var readBuffer = new byte[64];

                //Read some data
                readEnpoint.Read(readBuffer, 3000, out var readBytes);
            }
        }
示例#13
0
        static void Main(string[] args)
        {
            try
            {
                using var context = new UsbContext();
                var devices = context.List();
                Console.WriteLine();
                var selectedDeviceNum = 0;

                while (true)
                {
                    Console.WriteLine();
                    Console.WriteLine("Select a device (or X to exit):");
                    ListDevices(devices);
                    var input = Console.ReadLine();
                    if (input.ToLower().Trim() == "x")
                    {
                        return;
                    }
                    if (int.TryParse(input, out selectedDeviceNum))
                    {
                        break;
                    }
                    Console.WriteLine($"Invalid number: select a device from 0 to {devices.Count}");
                }

                var selectedDevice = devices[selectedDeviceNum];
                selectedDevice.Open();
                var writeEndpoint = selectedDevice.OpenEndpointWriter(WriteEndpointID.Ep01);
                var readEnpoint   = selectedDevice.OpenEndpointReader(ReadEndpointID.Ep01);

                while (true)
                {
                    Console.WriteLine();
                    Console.WriteLine("Enter the data to send (or a blank line to exit):");
                    var input = Console.ReadLine();
                    if (string.IsNullOrEmpty(input))
                    {
                        break;
                    }

                    var buffer     = new byte[1];
                    var readBuffer = new byte[64];

                    buffer[0] = 0x2B;

                    writeEndpoint.Write(buffer, timeout, out var bytesWritten);
                    readEnpoint.Read(readBuffer, timeout, out var readBytes);

                    if (readBytes == 0)
                    {
                        Console.WriteLine("null");
                    }
                    Console.WriteLine(string.Join(" ", readBuffer.Take(readBytes).Select(b => b.ToString())));
                }
            }
            catch (Exception error)
            {
                Console.WriteLine($"An {error.GetType()} occurred:");
                while (error != null)
                {
                    Console.WriteLine(error.Message);
                    error = error.InnerException;
                }
            }
        }